aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-04-20 22:11:30 +0000
committerChris Lattner <sabre@nondot.org>2008-04-20 22:11:30 +0000
commit7ae40e7d8ac43feaa5b3107e02c9b3a77fe729f2 (patch)
tree74a31eac6655e431590fb206c3f10035096e4cef
parentf9065a904fca1018d6ea3e1536b9b3368c084725 (diff)
add a handy helper method to instruction, useful for determining
whether it is used outside of some block. This can be used to see if there are any non-local references, for example. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50004 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Instruction.h7
-rw-r--r--lib/VMCore/Instruction.cpp23
2 files changed, 30 insertions, 0 deletions
diff --git a/include/llvm/Instruction.h b/include/llvm/Instruction.h
index fe688112f1..7933a4d3c2 100644
--- a/include/llvm/Instruction.h
+++ b/include/llvm/Instruction.h
@@ -72,6 +72,13 @@ public:
/// @brief Determine if one instruction is the same operation as another.
bool isSameOperationAs(Instruction *I) const;
+ /// isUsedOutsideOfBlock - Return true if there are any uses of this
+ /// instruction in blocks other than the specified block. Note that PHI nodes
+ /// are considered to evaluate their operands in the corresponding predecessor
+ /// block.
+ bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
+
+
/// use_back - Specialize the methods defined in Value, as we know that an
/// instruction can only be used by other instructions.
Instruction *use_back() { return cast<Instruction>(*use_begin());}
diff --git a/lib/VMCore/Instruction.cpp b/lib/VMCore/Instruction.cpp
index 6f09c90708..5344cf7bfe 100644
--- a/lib/VMCore/Instruction.cpp
+++ b/lib/VMCore/Instruction.cpp
@@ -198,6 +198,29 @@ bool Instruction::isSameOperationAs(Instruction *I) const {
return true;
}
+/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
+/// specified block. Note that PHI nodes are considered to evaluate their
+/// operands in the corresponding predecessor block.
+bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
+ for (use_const_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
+ // PHI nodes uses values in the corresponding predecessor block. For other
+ // instructions, just check to see whether the parent of the use matches up.
+ const PHINode *PN = dyn_cast<PHINode>(*UI);
+ if (PN == 0) {
+ if (cast<Instruction>(*UI)->getParent() != BB)
+ return true;
+ continue;
+ }
+
+ unsigned UseOperand = UI.getOperandNo();
+ if (PN->getIncomingBlock(UseOperand/2) != BB)
+ return true;
+ }
+ return false;
+}
+
+
+
/// mayWriteToMemory - Return true if this instruction may modify memory.
///
bool Instruction::mayWriteToMemory() const {