From c7650b4d1907becbf2ed112074fd2c4a6f174aa8 Mon Sep 17 00:00:00 2001 From: Victor Hernandez Date: Thu, 14 Jan 2010 01:45:14 +0000 Subject: [PATCH] Add MDNode::getFunction(), which figures out the metadata's function, if it has function that it is local to. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93400 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Metadata.h | 5 +++++ lib/VMCore/Metadata.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index 84a37290f9f..179010b1657 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -151,6 +151,11 @@ public: bool isFunctionLocal() const { return (getSubclassDataFromValue() & FunctionLocalBit) != 0; } + + // 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 *getFunction() const; // destroy - Delete this node. Only when there are no uses. void destroy(); diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index bac89bfd7b6..1e767ffba69 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -121,6 +121,40 @@ MDNode::~MDNode() { Op->~MDNodeOperand(); } +static Function *getFunctionHelper(const MDNode *N, + SmallPtrSet &Visited) { + assert(N->isFunctionLocal() && "Should only be called on function-local MD"); + 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(V)) + F = I->getParent()->getParent(); + else if (BasicBlock *BB = dyn_cast(V)) + F = BB->getParent(); + else if (Argument *A = dyn_cast(V)) + F = A->getParent(); + else if (MDNode *MD = dyn_cast(V)) + if (MD->isFunctionLocal()) + F = getFunctionHelper(MD, Visited); + if (F) break; + } + + return F; +} + +// 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 { + if (!isFunctionLocal()) return NULL; + SmallPtrSet Visited; + return getFunctionHelper(this, Visited); +} + // destroy - Delete this node. Only when there are no uses. void MDNode::destroy() { setValueSubclassData(getSubclassDataFromValue() | DestroyFlag); -- 2.34.1