github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/IR/Value.cpp (about)

     1  //===- Value.cpp - MLIR Value Classes -------------------------------------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  
    18  #include "mlir/IR/Value.h"
    19  #include "mlir/IR/Block.h"
    20  #include "mlir/IR/Operation.h"
    21  using namespace mlir;
    22  
    23  /// If this value is the result of an Operation, return the operation that
    24  /// defines it.
    25  Operation *Value::getDefiningOp() {
    26    if (auto *result = dyn_cast<OpResult>(this))
    27      return result->getOwner();
    28    return nullptr;
    29  }
    30  
    31  Location Value::getLoc() {
    32    if (auto *op = getDefiningOp())
    33      return op->getLoc();
    34    return UnknownLoc::get(getContext());
    35  }
    36  
    37  /// Return the Region in which this Value is defined.
    38  Region *Value::getParentRegion() {
    39    switch (getKind()) {
    40    case Value::Kind::BlockArgument:
    41      return cast<BlockArgument>(this)->getOwner()->getParent();
    42    case Value::Kind::OpResult:
    43      return getDefiningOp()->getParentRegion();
    44    }
    45    llvm_unreachable("Unknown Value Kind");
    46  }
    47  
    48  //===----------------------------------------------------------------------===//
    49  // IRObjectWithUseList implementation.
    50  //===----------------------------------------------------------------------===//
    51  
    52  /// Replace all uses of 'this' value with the new value, updating anything in
    53  /// the IR that uses 'this' to use the other value instead.  When this returns
    54  /// there are zero uses of 'this'.
    55  void IRObjectWithUseList::replaceAllUsesWith(IRObjectWithUseList *newValue) {
    56    assert(this != newValue && "cannot RAUW a value with itself");
    57    while (!use_empty()) {
    58      use_begin()->set(newValue);
    59    }
    60  }
    61  
    62  /// Drop all uses of this object from their respective owners.
    63  void IRObjectWithUseList::dropAllUses() {
    64    while (!use_empty()) {
    65      use_begin()->drop();
    66    }
    67  }