Remap metadata attached to instructions when remapping individual
authorDan Gohman <gohman@apple.com>
Wed, 25 Aug 2010 21:36:50 +0000 (21:36 +0000)
committerDan Gohman <gohman@apple.com>
Wed, 25 Aug 2010 21:36:50 +0000 (21:36 +0000)
instructions, not when remapping modules.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112091 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Linker/LinkModules.cpp
lib/Transforms/Utils/CloneModule.cpp
lib/Transforms/Utils/ValueMapper.cpp
test/Linker/metadata-a.ll [new file with mode: 0644]
test/Linker/metadata-b.ll [new file with mode: 0644]

index 89f4cdc22960796855de5d9ec7bd4e9ec6814df9..07089f7feeca78d7300197121972d98342349b22 100644 (file)
@@ -1005,13 +1005,31 @@ static bool LinkFunctionBody(Function *Dest, Function *Src,
   // the Source function as operands.  Loop through all of the operands of the
   // functions and patch them up to point to the local versions...
   //
+  // This is the same as RemapInstruction, except that it avoids remapping
+  // instruction and basic block operands.
+  //
   for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
-    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
+      // Remap operands.
       for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
            OI != OE; ++OI)
         if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI))
           *OI = MapValue(*OI, ValueMap);
 
+      // Remap attached metadata.
+      SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
+      I->getAllMetadata(MDs);
+      for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
+           MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
+        Value *Old = MI->second;
+        if (!isa<Instruction>(Old) && !isa<BasicBlock>(Old)) {
+          Value *New = MapValue(Old, ValueMap);
+          if (New != Old) 
+            I->setMetadata(MI->first, cast<MDNode>(New));
+        }
+      }
+    }
+
   // There is no need to map the arguments anymore.
   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
        I != E; ++I)
index 25083adb7ce7476c5aca2c7aafa259bcfbbe4225..c54edf63171157fe4a0e55e77d7621056e0f6079 100644 (file)
@@ -132,20 +132,5 @@ Module *llvm::CloneModule(const Module *M,
       NewNMD->addOperand(cast<MDNode>(MapValue(NMD.getOperand(i), VMap)));
   }
 
-  // Update metadata attach with instructions.
-  for (Module::iterator MI = New->begin(), ME = New->end(); MI != ME; ++MI)   
-    for (Function::iterator FI = MI->begin(), FE = MI->end(); 
-         FI != FE; ++FI)
-      for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 
-           BI != BE; ++BI) {
-        SmallVector<std::pair<unsigned, MDNode *>, 4 > MDs;
-        BI->getAllMetadata(MDs);
-        for (SmallVector<std::pair<unsigned, MDNode *>, 4>::iterator 
-               MDI = MDs.begin(), MDE = MDs.end(); MDI != MDE; ++MDI) {
-          Value *MappedValue = MapValue(MDI->second, VMap);
-          if (MDI->second != MappedValue && MappedValue)
-            BI->setMetadata(MDI->first, cast<MDNode>(MappedValue));
-        }
-      }
   return New;
 }
index df11cbbcb276b2fbd9e200e15c297ad1a5ea9e46..8b5ddb08621ba656b1624600b763298daa887cdb 100644 (file)
@@ -147,10 +147,21 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM) {
 /// current values into those specified by VMap.
 ///
 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
+  // Remap operands.
   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
     Value *V = MapValue(*op, VMap);
     assert(V && "Referenced value not in value map!");
     *op = V;
   }
-}
 
+  // Remap attached metadata.
+  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
+  I->getAllMetadata(MDs);
+  for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
+       MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
+    Value *Old = MI->second;
+    Value *New = MapValue(Old, VMap);
+    if (New != Old)
+      I->setMetadata(MI->first, cast<MDNode>(New));
+  }
+}
diff --git a/test/Linker/metadata-a.ll b/test/Linker/metadata-a.ll
new file mode 100644 (file)
index 0000000..5a9d2e4
--- /dev/null
@@ -0,0 +1,15 @@
+; RUN: llvm-link %s %p/metadata-b.ll -S -o - | FileCheck %s
+
+; CHECK: define void @foo(i32 %a)
+; CHECK: ret void, !attach !0, !also !{i32 %a}
+; CHECK: define void @goo(i32 %b)
+; CHECK: ret void, !attach !1, !and !{i32 %b}
+; CHECK: !0 = metadata !{i32 524334, void (i32)* @foo}
+; CHECK: !1 = metadata !{i32 524334, void (i32)* @goo}
+
+define void @foo(i32 %a) nounwind {
+entry:
+  ret void, !attach !0, !also !{ i32 %a }
+}
+
+!0 = metadata !{i32 524334, void (i32)* @foo}
diff --git a/test/Linker/metadata-b.ll b/test/Linker/metadata-b.ll
new file mode 100644 (file)
index 0000000..ef0270a
--- /dev/null
@@ -0,0 +1,9 @@
+; This file is for use with metadata-a.ll
+; RUN: true
+
+define void @goo(i32 %b) nounwind {
+entry:
+  ret void, !attach !0, !and !{ i32 %b }
+}
+
+!0 = metadata !{i32 524334, void (i32)* @goo}