Fix a constant folding address space place I missed.
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Tue, 17 Sep 2013 23:23:16 +0000 (23:23 +0000)
committerMatt Arsenault <Matthew.Arsenault@amd.com>
Tue, 17 Sep 2013 23:23:16 +0000 (23:23 +0000)
If address space 0 was smaller than the address space
in a constant inttoptr/ptrtoint pair, the wrong mask size
would be used.

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

lib/Analysis/ConstantFolding.cpp
test/Transforms/InstCombine/constant-fold-address-space-pointer.ll

index 9b7999c4e3f8aa90418d6734ff15950050675cc0..22f36e56bcf222db78810a7cbfe900a2aba83ee9 100644 (file)
@@ -974,10 +974,11 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
       if (TD && CE->getOpcode() == Instruction::IntToPtr) {
         Constant *Input = CE->getOperand(0);
         unsigned InWidth = Input->getType()->getScalarSizeInBits();
-        if (TD->getPointerTypeSizeInBits(CE->getType()) < InWidth) {
+        unsigned PtrWidth = TD->getPointerTypeSizeInBits(CE->getType());
+        if (PtrWidth < InWidth) {
           Constant *Mask =
-            ConstantInt::get(CE->getContext(), APInt::getLowBitsSet(InWidth,
-                                                  TD->getPointerSizeInBits()));
+            ConstantInt::get(CE->getContext(),
+                             APInt::getLowBitsSet(InWidth, PtrWidth));
           Input = ConstantExpr::getAnd(Input, Mask);
         }
         // Do a zext or trunc to get to the dest size.
index 9b17847cbd22fce0ffa3416de302472da4f523a2..f3a474a8514663f3c6561304e36b263761ae67d8 100644 (file)
@@ -74,6 +74,14 @@ define i8 @const_fold_ptrtoint_mask() {
   ret i8 ptrtoint (i32 addrspace(3)* inttoptr (i32 257 to i32 addrspace(3)*) to i8)
 }
 
+; Address space 0 is too small for the correct mask, should mask with
+; 64-bits instead of 32
+define i64 @const_fold_ptrtoint_mask_small_as0() {
+; CHECK-LABEL: @const_fold_ptrtoint_mask_small_as0(
+; CHECK: ret i64 -1
+  ret i64 ptrtoint (i32 addrspace(1)* inttoptr (i128 -1 to i32 addrspace(1)*) to i64)
+}
+
 define i32 addrspace(3)* @const_inttoptr() {
 ; CHECK-LABEL: @const_inttoptr(
 ; CHECK-NEXT: ret i32 addrspace(3)* inttoptr (i16 4 to i32 addrspace(3)*)