Add MDNode::getIfExists(), an efficient way to determine if a value is used by metada...
authorVictor Hernandez <vhernandez@apple.com>
Tue, 26 Jan 2010 02:36:35 +0000 (02:36 +0000)
committerVictor Hernandez <vhernandez@apple.com>
Tue, 26 Jan 2010 02:36:35 +0000 (02:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94492 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Metadata.h
lib/VMCore/Metadata.cpp

index f1e7a22a42fc33964c2a95e5617ee88f2c7f46a7..4e459bf7bf5c113d04ab2de4c8f165f8c9ea2f9e 100644 (file)
@@ -112,7 +112,7 @@ protected:
                   bool isFunctionLocal);
   
   static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
-                           FunctionLocalness FL);
+                           FunctionLocalness FL, bool Insert = true);
 public:
   // Constructors and destructors.
   static MDNode *get(LLVMContext &Context, Value *const *Vals,
@@ -121,6 +121,9 @@ public:
   // from isFunctionLocal argument, not by analyzing Vals.
   static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
                                        unsigned NumVals, bool isFunctionLocal);
+                                       
+  static MDNode *getIfExists(LLVMContext &Context, Value *const *Vals,
+                             unsigned NumVals);
   
   /// getOperand - Return specified operand.
   Value *getOperand(unsigned i) const;
index ee8e713fc1ea98b0e2e033bd146690e64d5a941c..07a5f3c539882ca8b7e98414b307f073b193952d 100644 (file)
@@ -186,43 +186,50 @@ void MDNode::destroy() {
 }
 
 MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
-                          unsigned NumVals, FunctionLocalness FL) {
+                          unsigned NumVals, FunctionLocalness FL,
+                          bool Insert) {
   LLVMContextImpl *pImpl = Context.pImpl;
   FoldingSetNodeID ID;
   for (unsigned i = 0; i != NumVals; ++i)
     ID.AddPointer(Vals[i]);
 
   void *InsertPoint;
-  MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
-  if (!N) {
-    bool isFunctionLocal = false;
-    switch (FL) {
-    case FL_Unknown:
-      for (unsigned i = 0; i != NumVals; ++i) {
-        Value *V = Vals[i];
-        if (!V) continue;
-        if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
-            (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
-          isFunctionLocal = true;
-          break;
-        }
+  MDNode *N = NULL;
+  
+  if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
+    return N;
+    
+  if (!Insert)
+    return NULL;
+    
+  bool isFunctionLocal = false;
+  switch (FL) {
+  case FL_Unknown:
+    for (unsigned i = 0; i != NumVals; ++i) {
+      Value *V = Vals[i];
+      if (!V) continue;
+      if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
+          (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
+        isFunctionLocal = true;
+        break;
       }
-      break;
-    case FL_No:
-      isFunctionLocal = false;
-      break;
-    case FL_Yes:
-      isFunctionLocal = true;
-      break;
     }
+    break;
+  case FL_No:
+    isFunctionLocal = false;
+    break;
+  case FL_Yes:
+    isFunctionLocal = true;
+    break;
+  }
 
-    // Coallocate space for the node and Operands together, then placement new.
-    void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
-    N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
+  // Coallocate space for the node and Operands together, then placement new.
+  void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
+  N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
+
+  // InsertPoint will have been set by the FindNodeOrInsertPos call.
+  pImpl->MDNodeSet.InsertNode(N, InsertPoint);
 
-    // InsertPoint will have been set by the FindNodeOrInsertPos call.
-    pImpl->MDNodeSet.InsertNode(N, InsertPoint);
-  }
   return N;
 }
 
@@ -230,11 +237,16 @@ MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
   return getMDNode(Context, Vals, NumVals, FL_Unknown);
 }
 
-MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
+MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
                                       unsigned NumVals, bool isFunctionLocal) {
   return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
 }
 
+MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
+                            unsigned NumVals) {
+  return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
+}
+
 /// getOperand - Return specified operand.
 Value *MDNode::getOperand(unsigned i) const {
   return *getOperandPtr(const_cast<MDNode*>(this), i);