[llvm-commits] [llvm] r93400 - in /llvm/trunk: include/llvm/Metadata.h lib/VMCore/Metadata.cpp

Victor Hernandez vhernandez at apple.com
Wed Jan 13 17:45:14 PST 2010


Author: hernande
Date: Wed Jan 13 19:45:14 2010
New Revision: 93400

URL: http://llvm.org/viewvc/llvm-project?rev=93400&view=rev
Log:
Add MDNode::getFunction(), which figures out the metadata's function, if it has function that it is local to.

Modified:
    llvm/trunk/include/llvm/Metadata.h
    llvm/trunk/lib/VMCore/Metadata.cpp

Modified: llvm/trunk/include/llvm/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Metadata.h?rev=93400&r1=93399&r2=93400&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Metadata.h (original)
+++ llvm/trunk/include/llvm/Metadata.h Wed Jan 13 19:45:14 2010
@@ -151,6 +151,11 @@
   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();

Modified: llvm/trunk/lib/VMCore/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Metadata.cpp?rev=93400&r1=93399&r2=93400&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Metadata.cpp (original)
+++ llvm/trunk/lib/VMCore/Metadata.cpp Wed Jan 13 19:45:14 2010
@@ -121,6 +121,40 @@
     Op->~MDNodeOperand();
 }
 
+static Function *getFunctionHelper(const MDNode *N,
+                                   SmallPtrSet<const MDNode *, 32> &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<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;
+  }
+  
+  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<const MDNode *, 32> Visited;
+  return getFunctionHelper(this, Visited);
+}
+
 // destroy - Delete this node.  Only when there are no uses.
 void MDNode::destroy() {
   setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);





More information about the llvm-commits mailing list