fix PR5978 by peeling the loop so that we avoid shifting the
authorChris Lattner <sabre@nondot.org>
Fri, 8 Jan 2010 19:02:23 +0000 (19:02 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 8 Jan 2010 19:02:23 +0000 (19:02 +0000)
result int by 8 for the first byte.  While normally harmless,
if the result is smaller than a byte, this shift is invalid.

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

lib/Analysis/ConstantFolding.cpp
test/Transforms/ConstProp/loads.ll

index 194cabaf978074b14107444cc38c4d32f06a9b18..4ae8859a257b1d4df1ee57fe7b7c2d28191b6bd7 100644 (file)
@@ -398,8 +398,8 @@ static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
                           BytesLoaded, TD))
     return 0;
 
-  APInt ResultVal(IntType->getBitWidth(), 0);
-  for (unsigned i = 0; i != BytesLoaded; ++i) {
+  APInt ResultVal = APInt(IntType->getBitWidth(), RawBytes[BytesLoaded-1]);
+  for (unsigned i = 1; i != BytesLoaded; ++i) {
     ResultVal <<= 8;
     ResultVal |= APInt(IntType->getBitWidth(), RawBytes[BytesLoaded-1-i]);
   }
index 9151d256b02499a3584157b4174b910016c9e776..9fbba2b355281e79f79d713b17a7d93c6e2eef95 100644 (file)
@@ -110,3 +110,13 @@ define i16 @test12() {
 ; CHECK: @test12
 ; CHECK: ret i16 98
 }
+
+
+; PR5978
+@g5 = constant i8 4
+define i1 @test13() {
+  %A = load i1* bitcast (i8* @g5 to i1*)
+  ret i1 %A
+; CHECK: @test13
+; CHECK: ret i1 false
+}