Make sure that ConstantExpr offsets also aren't off of extern
authorEric Christopher <echristo@apple.com>
Thu, 11 Feb 2010 17:44:04 +0000 (17:44 +0000)
committerEric Christopher <echristo@apple.com>
Thu, 11 Feb 2010 17:44:04 +0000 (17:44 +0000)
symbols.

Thanks to Duncan Sands for the testcase!

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

lib/Transforms/InstCombine/InstCombineCalls.cpp
test/Transforms/InstCombine/objsize.ll

index 1371fa7f1adcdb49ce1f3b7d92f45592cf124bfe..43419eb9fc09d0545f22f44b0b3e28b0dfc8149b 100644 (file)
@@ -331,9 +331,15 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
       if (CE->getOpcode() != Instruction::GetElementPtr) break;
       GEPOperator *GEP = cast<GEPOperator>(CE);
       
+      // Make sure we're not a constant offset from an external
+      // global.
+      Value *Operand = GEP->getPointerOperand();
+      if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand))
+        if (!GV->hasDefinitiveInitializer()) break;
+      
       // Get what we're pointing to and its size.
       const PointerType *PT = 
-        cast<PointerType>(GEP->getPointerOperand()->getType());
+        cast<PointerType>(Operand->getType());
       size_t Size = TD->getTypeAllocSize(PT->getElementType());
       
       // Get the current byte offset into the thing.
@@ -345,7 +351,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
       Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset);
       return ReplaceInstUsesWith(CI, RetVal);
       
-    } 
+    }
   }
   case Intrinsic::bswap:
     // bswap(bswap(x)) -> x
index 2bcdd914e33ebc4431a10adcc94a19b07929cc89..646baa0de0617456931efc949c93c6198bf8e71f 100644 (file)
@@ -48,4 +48,19 @@ define i1 @baz() nounwind {
   ret i1 %2
 }
 
+define void @test1(i8* %q, i32 %x) nounwind noinline {
+; CHECK: @test1
+; CHECK: objectsize.i32
+entry:
+  %0 = call i32 @llvm.objectsize.i32(i8* getelementptr inbounds ([0 x i8]* @window, i32 0, i32 10), i1 false) ; <i64> [#uses=1]
+  %1 = icmp eq i32 %0, -1                         ; <i1> [#uses=1]
+  br i1 %1, label %"47", label %"46"
+
+"46":                                             ; preds = %entry
+  unreachable
+
+"47":                                             ; preds = %entry
+  unreachable
+}
+
 declare i32 @llvm.objectsize.i32(i8*, i1) nounwind readonly
\ No newline at end of file