From 1e2d2f5d66ab64f2f8931d37334c4bad6c7ccc1f Mon Sep 17 00:00:00 2001 From: River Riddle Date: Sun, 19 May 2019 15:42:49 -0700 Subject: [PATCH] Add a utility function 'Operation::replaceUsesOfWith' to replace uses of a value within a single operation. -- PiperOrigin-RevId: 248961779 --- mlir/include/mlir/IR/Operation.h | 3 +++ mlir/lib/IR/Operation.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h index a4af5de..232c18c 100644 --- a/mlir/include/mlir/IR/Operation.h +++ b/mlir/include/mlir/IR/Operation.h @@ -127,6 +127,9 @@ public: /// Returns nullptr if the operation is unlinked. Function *getFunction(); + /// Replace any uses of 'from' with 'to' within this operation. + void replaceUsesOfWith(Value *from, Value *to); + /// Destroys this operation and its subclass data. void destroy(); diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp index df6a6cf..47f9b11 100644 --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -293,6 +293,15 @@ Function *Operation::getFunction() { return block ? block->getFunction() : nullptr; } +/// Replace any uses of 'from' with 'to' within this operation. +void Operation::replaceUsesOfWith(Value *from, Value *to) { + if (from == to) + return; + for (auto &operand : getOpOperands()) + if (operand.get() == from) + operand.set(to); +} + //===----------------------------------------------------------------------===// // Operation Walkers //===----------------------------------------------------------------------===// -- 2.7.4