Fix SimplifyLibcalls and ValueTracking to check mayBeOverridden
authorDan Gohman <gohman@apple.com>
Wed, 19 Aug 2009 00:11:12 +0000 (00:11 +0000)
committerDan Gohman <gohman@apple.com>
Wed, 19 Aug 2009 00:11:12 +0000 (00:11 +0000)
before performing optimizations based on constant string values.

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

lib/Analysis/ValueTracking.cpp
lib/Transforms/Scalar/SimplifyLibCalls.cpp
test/Transforms/SimplifyLibCalls/weak-symbols.ll [new file with mode: 0644]

index 604d10c68b94df42e889a4e1a4dcfd48998a8356..3ab10c55d81c5a91a99bddd0ab8c915dce77fcf5 100644 (file)
@@ -1061,7 +1061,8 @@ bool llvm::GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset,
   // variable that is a constant and is initialized. The referenced constant
   // initializer is the array that we'll use for optimization.
   GlobalVariable* GV = dyn_cast<GlobalVariable>(V);
-  if (!GV || !GV->isConstant() || !GV->hasInitializer())
+  if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
+      GV->mayBeOverridden())
     return false;
   Constant *GlobalInit = GV->getInitializer();
   
index 02b26bdac2be659d90715fb7cf23d6d15ae2fbd2..761c309c2004d803df38e2fa42397f544296ad7a 100644 (file)
@@ -438,7 +438,8 @@ static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
   // variable that is a constant and is initialized. The referenced constant
   // initializer is the array that we'll use for optimization.
   GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
-  if (!GV || !GV->isConstant() || !GV->hasInitializer())
+  if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
+      GV->mayBeOverridden())
     return 0;
   Constant *GlobalInit = GV->getInitializer();
   
diff --git a/test/Transforms/SimplifyLibCalls/weak-symbols.ll b/test/Transforms/SimplifyLibCalls/weak-symbols.ll
new file mode 100644 (file)
index 0000000..970ee0d
--- /dev/null
@@ -0,0 +1,26 @@
+; RUN: llvm-as < %s | opt -simplify-libcalls | llvm-dis | FileCheck %s
+; PR4738
+
+; SimplifyLibcalls shouldn't assume anything about weak symbols.
+
+@real_init = weak_odr constant [2 x i8] c"y\00"
+@fake_init = weak constant [2 x i8] c"y\00"
+@.str = private constant [2 x i8] c"y\00"
+
+; CHECK: define i32 @foo
+; CHECK: call i32 @strcmp
+define i32 @foo() nounwind {
+entry:
+  %t0 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8]* @fake_init, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8]* @.str, i64 0, i64 0)) nounwind readonly
+  ret i32 %t0
+}
+
+; CHECK: define i32 @bar
+; CHECK: ret i32 0
+define i32 @bar() nounwind {
+entry:
+  %t0 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8]* @real_init, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8]* @.str, i64 0, i64 0)) nounwind readonly
+  ret i32 %t0
+}
+
+declare i32 @strcmp(i8*, i8*) nounwind readonly