[llvm-commits] [llvm] r103007 - in /llvm/trunk: lib/VMCore/Metadata.cpp test/Transforms/GlobalOpt/metadata.ll

Duncan Sands baldrick at free.fr
Tue May 4 05:43:36 PDT 2010


Author: baldrick
Date: Tue May  4 07:43:36 2010
New Revision: 103007

URL: http://llvm.org/viewvc/llvm-project?rev=103007&view=rev
Log:
Fix a variant of PR6112 found by thinking about it: when doing
RAUW of a global variable with a local variable in function F,
if function local metadata M in function G was using the global
then M would become function-local to both F and G, which is not
allowed.  See the testcase for an example.  Fixed by detecting
this situation and zapping the metadata operand when it occurs.

Modified:
    llvm/trunk/lib/VMCore/Metadata.cpp
    llvm/trunk/test/Transforms/GlobalOpt/metadata.ll

Modified: llvm/trunk/lib/VMCore/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Metadata.cpp?rev=103007&r1=103006&r2=103007&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Metadata.cpp (original)
+++ llvm/trunk/lib/VMCore/Metadata.cpp Tue May  4 07:43:36 2010
@@ -115,14 +115,17 @@
 }
 
 static const Function *getFunctionForValue(Value *V) {
-  assert(!isa<MDNode>(V) && "does not iterate over metadata operands");
   if (!V) return NULL;
-  if (Instruction *I = dyn_cast<Instruction>(V))
-    return I->getParent()->getParent();
-  if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
-    return BB->getParent();
+  if (Instruction *I = dyn_cast<Instruction>(V)) {
+    BasicBlock *BB = I->getParent();
+    return BB ? BB->getParent() : 0;
+  }
   if (Argument *A = dyn_cast<Argument>(V))
     return A->getParent();
+  if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
+    return BB->getParent();
+  if (MDNode *MD = dyn_cast<MDNode>(V))
+    return MD->getFunction();
   return NULL;
 }
 
@@ -272,8 +275,19 @@
   // with an instruction or some other function-local object.  If this is a
   // non-function-local MDNode, it can't point to a function-local object.
   // Handle this case by implicitly dropping the MDNode reference to null.
-  if (!isFunctionLocal() && To && isFunctionLocalValue(To))
-    To = 0;
+  // Likewise if the MDNode is function-local but for a different function.
+  if (To && isFunctionLocalValue(To)) {
+    if (!isFunctionLocal())
+      To = 0;
+    else {
+      const Function *F = getFunction();
+      const Function *FV = getFunctionForValue(To);
+      // Metadata can be function-local without having an associated function.
+      // So only consider functions to have changed if non-null.
+      if (F && FV && F != FV)
+        To = 0;
+    }
+  }
   
   if (From == To)
     return;

Modified: llvm/trunk/test/Transforms/GlobalOpt/metadata.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/metadata.ll?rev=103007&r1=103006&r2=103007&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/metadata.ll (original)
+++ llvm/trunk/test/Transforms/GlobalOpt/metadata.ll Tue May  4 07:43:36 2010
@@ -1,7 +1,8 @@
 ; RUN: opt -S -globalopt < %s | FileCheck %s
 
 ; PR6112 - When globalopt does RAUW(@G, %G), the metadata reference should drop
-; to null.
+; to null.  Function local metadata that references @G from a different function
+; to that containing %G should likewise drop to null.
 @G = internal global i8** null
 
 define i32 @main(i32 %argc, i8** %argv) {
@@ -11,9 +12,16 @@
   ret i32 0
 }
 
-!named = !{!0}
+define void @foo(i32 %x) {
+  call void @llvm.dbg.value(metadata !{i8*** @G, i32 %x}, i64 0, metadata !1)
+; CHECK: call void @llvm.dbg.value(metadata !{null, i32 %x}, i64 0, metadata !1)
+  ret void
+}
 
-; CHECK: !0 = metadata !{null}
-!0 = metadata !{i8*** @G}
+declare void @llvm.dbg.value(metadata, i64, metadata) nounwind readnone
 
+!named = !{!0}
 
+!0 = metadata !{i8*** @G}
+; CHECK: !0 = metadata !{null}
+!1 = metadata !{i8* null}





More information about the llvm-commits mailing list