diff options
author | Victor Hernandez <vhernandez@apple.com> | 2010-01-18 20:36:54 +0000 |
---|---|---|
committer | Victor Hernandez <vhernandez@apple.com> | 2010-01-18 20:36:54 +0000 |
commit | 54630e1cef710e65751e2147cbdd2e018292c435 (patch) | |
tree | 329e1f8c3c41542e63b1ccad6ee6c63526e725e6 /lib/VMCore/Metadata.cpp | |
parent | eb231ce51dcbcbd464fa639f0dc0d2e39d487285 (diff) | |
download | llvm-54630e1cef710e65751e2147cbdd2e018292c435.tar.gz llvm-54630e1cef710e65751e2147cbdd2e018292c435.tar.bz2 llvm-54630e1cef710e65751e2147cbdd2e018292c435.tar.xz |
Simplify MDNode::getFunction() and assertLocalFunction() by avoiding extra Function* variable and smallptrset since function-local metadata cannot be cyclic
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93762 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/Metadata.cpp')
-rw-r--r-- | lib/VMCore/Metadata.cpp | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index 7988b446fb..8a40fed1cc 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -122,66 +122,59 @@ MDNode::~MDNode() { } #ifndef NDEBUG -static Function *assertLocalFunction(const MDNode *N, - SmallPtrSet<const MDNode *, 32> &Visited) { - Function *F = NULL; - // Only visit each MDNode once. - if (!Visited.insert(N)) return F; - - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { - Value *V = N->getOperand(i); - Function *NewF = NULL; - if (!V) continue; - if (Instruction *I = dyn_cast<Instruction>(V)) - NewF = I->getParent()->getParent(); - else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) - NewF = BB->getParent(); - else if (Argument *A = dyn_cast<Argument>(V)) - NewF = A->getParent(); - else if (MDNode *MD = dyn_cast<MDNode>(V)) - if (MD->isFunctionLocal()) - NewF = assertLocalFunction(MD, Visited); - if (F && NewF) assert(F == NewF && "inconsistent function-local metadata"); - if (!F) F = NewF; - } - return F; -} -#endif +static Function *assertLocalFunction(const MDNode *N) { + if (!N->isFunctionLocal()) return NULL; -static Function *getFunctionHelper(const MDNode *N, - SmallPtrSet<const MDNode *, 32> &Visited) { - assert(N->isFunctionLocal() && "Should only be called on function-local MD"); -#ifndef NDEBUG - return assertLocalFunction(N, Visited); -#endif Function *F = NULL; - // Only visit each MDNode once. - if (!Visited.insert(N)) return F; - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { Value *V = N->getOperand(i); if (!V) continue; - if (Instruction *I = dyn_cast<Instruction>(V)) - F = I->getParent()->getParent(); - else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) - F = BB->getParent(); - else if (Argument *A = dyn_cast<Argument>(V)) - F = A->getParent(); - else if (MDNode *MD = dyn_cast<MDNode>(V)) - if (MD->isFunctionLocal()) - F = getFunctionHelper(MD, Visited); - if (F) break; + if (Instruction *I = dyn_cast<Instruction>(V)) { + if (F) assert(F == I->getParent()->getParent() && + "inconsistent function-local metadata"); + else F = I->getParent()->getParent(); + } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { + if (F) assert(F == BB->getParent() && + "inconsistent function-local metadata"); + else F = BB->getParent(); + } else if (Argument *A = dyn_cast<Argument>(V)) { + if (F) assert(F == A->getParent() && + "inconsistent function-local metadata"); + else F = A->getParent(); + } else if (MDNode *MD = dyn_cast<MDNode>(V)) { + if (Function *NewF = assertLocalFunction(MD)) { + if (F) assert(F == NewF && "inconsistent function-local metadata"); + else F = NewF; + } + } } return F; } +#endif // getFunction - If this metadata is function-local and recursively has a // function-local operand, return the first such operand's parent function. // Otherwise, return null. Function *MDNode::getFunction() const { +#ifndef NDEBUG + return assertLocalFunction(this); +#endif + if (!isFunctionLocal()) return NULL; - SmallPtrSet<const MDNode *, 32> Visited; - return getFunctionHelper(this, Visited); + + for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { + Value *V = getOperand(i); + if (!V) continue; + if (Instruction *I = dyn_cast<Instruction>(V)) + return I->getParent()->getParent(); + if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) + return BB->getParent(); + if (Argument *A = dyn_cast<Argument>(V)) + return A->getParent(); + if (MDNode *MD = dyn_cast<MDNode>(V)) + if (Function *F = MD->getFunction()) return F; + } + return NULL; } // destroy - Delete this node. Only when there are no uses. |