[llvm] r174424 - ConstantFolding: Fix a crash when encoutering a truncating inttoptr.

Benjamin Kramer benny.kra at googlemail.com
Tue Feb 5 11:04:37 PST 2013


Author: d0k
Date: Tue Feb  5 13:04:36 2013
New Revision: 174424

URL: http://llvm.org/viewvc/llvm-project?rev=174424&view=rev
Log:
ConstantFolding: Fix a crash when encoutering a truncating inttoptr.

This was introduced in r173293.

Modified:
    llvm/trunk/lib/Analysis/ConstantFolding.cpp
    llvm/trunk/test/Transforms/InstSimplify/ptr_diff.ll

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=174424&r1=174423&r2=174424&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Tue Feb  5 13:04:36 2013
@@ -545,14 +545,18 @@ static Constant *SymbolicallyEvaluateBin
   // constant.  This happens frequently when iterating over a global array.
   if (Opc == Instruction::Sub && TD) {
     GlobalValue *GV1, *GV2;
-    APInt Offs1(TD->getPointerSizeInBits(), 0),
-          Offs2(TD->getPointerSizeInBits(), 0);
+    unsigned PtrSize = TD->getPointerSizeInBits();
+    unsigned OpSize = TD->getTypeSizeInBits(Op0->getType());
+    APInt Offs1(PtrSize, 0), Offs2(PtrSize, 0);
 
     if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD))
       if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
           GV1 == GV2) {
         // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
-        return ConstantInt::get(Op0->getType(), Offs1-Offs2);
+        // PtrToInt may change the bitwidth so we have convert to the right size
+        // first.
+        return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
+                                                Offs2.zextOrTrunc(OpSize));
       }
   }
 

Modified: llvm/trunk/test/Transforms/InstSimplify/ptr_diff.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/ptr_diff.ll?rev=174424&r1=174423&r2=174424&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/ptr_diff.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/ptr_diff.ll Tue Feb  5 13:04:36 2013
@@ -57,3 +57,22 @@ define <4 x i32> @ptrdiff4(<4 x i8*> %ar
   %sub = sub <4 x i32> %p1, %p2
   ret <4 x i32> %sub
 }
+
+%struct.ham = type { i32, [2 x [2 x i32]] }
+
+ at global = internal global %struct.ham zeroinitializer, align 4
+
+define i32 @ptrdiff5() nounwind {
+bb:
+  %tmp = getelementptr inbounds %struct.ham* @global, i32 0, i32 1
+  %tmp1 = getelementptr inbounds [2 x [2 x i32]]* %tmp, i32 0, i32 0
+  %tmp2 = bitcast [2 x i32]* %tmp1 to i32*
+  %tmp3 = ptrtoint i32* %tmp2 to i32
+  %tmp4 = getelementptr inbounds %struct.ham* @global, i32 0, i32 1
+  %tmp5 = getelementptr inbounds [2 x [2 x i32]]* %tmp4, i32 0, i32 0
+  %tmp6 = ptrtoint [2 x i32]* %tmp5 to i32
+  %tmp7 = sub i32 %tmp3, %tmp6
+  ret i32 %tmp7
+; CHECK: @ptrdiff5
+; CHECK: ret i32 0
+}





More information about the llvm-commits mailing list