llvm.org GIT mirror llvm / b80a2a6
Remove the code which constant-folded ptrtoint(inttoptr(x)+c) to getelementptr. Despite only doing so in the case where x is a known array object and c can be converted to an index within range, this could still be invalid if c is actually the address of an object allocated outside of LLVM. Also, SCEVExpander, the original motivation for this code, has since been improved to avoid inttoptr+ptroint in more cases. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96950 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
2 changed file(s) with 5 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
782782 // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
783783 // the int size is >= the ptr size. This requires knowing the width of a
784784 // pointer, so it can't be done in ConstantExpr::getCast.
785 if (ConstantExpr *CE = dyn_cast(Ops[0])) {
785 if (ConstantExpr *CE = dyn_cast(Ops[0]))
786786 if (TD &&
787 TD->getPointerSizeInBits() <=
788 CE->getType()->getScalarSizeInBits()) {
789 if (CE->getOpcode() == Instruction::PtrToInt)
790 return FoldBitCast(CE->getOperand(0), DestTy, *TD);
791
792 // If there's a constant offset added to the integer value before
793 // it is casted back to a pointer, see if the expression can be
794 // converted into a GEP.
795 if (CE->getOpcode() == Instruction::Add)
796 if (ConstantInt *L = dyn_cast(CE->getOperand(1)))
797 if (ConstantExpr *R = dyn_cast(CE->getOperand(0)))
798 if (R->getOpcode() == Instruction::PtrToInt)
799 if (GlobalVariable *GV =
800 dyn_cast(R->getOperand(0))) {
801 const PointerType *GVTy = cast(GV->getType());
802 if (const ArrayType *AT =
803 dyn_cast(GVTy->getElementType())) {
804 const Type *ElTy = AT->getElementType();
805 uint64_t AllocSize = TD->getTypeAllocSize(ElTy);
806 APInt PSA(L->getValue().getBitWidth(), AllocSize);
807 if (ElTy == cast(DestTy)->getElementType() &&
808 L->getValue().urem(PSA) == 0) {
809 APInt ElemIdx = L->getValue().udiv(PSA);
810 if (ElemIdx.ult(APInt(ElemIdx.getBitWidth(),
811 AT->getNumElements()))) {
812 Constant *Index[] = {
813 Constant::getNullValue(CE->getType()),
814 ConstantInt::get(ElTy->getContext(), ElemIdx)
815 };
816 return ConstantExpr::getGetElementPtr(GV, &Index[0], 2);
817 }
818 }
819 }
820 }
821 }
822 }
787 TD->getPointerSizeInBits() <= CE->getType()->getScalarSizeInBits() &&
788 CE->getOpcode() == Instruction::PtrToInt)
789 return FoldBitCast(CE->getOperand(0), DestTy, *TD);
790
823791 return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
824792 case Instruction::Trunc:
825793 case Instruction::ZExt:
+0
-27
test/Transforms/InstCombine/constant-fold-ptr-casts.ll less more
None ; RUN: opt < %s -instcombine -S | grep {ret i32 2143034560} | count 2
1
2 ; Instcombine should be able to completely fold this code.
3
4 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
5 target triple = "i686-apple-darwin8"
6
7 @bar = constant [3 x i64] [i64 9220983451228067448, i64 9220983451228067449, i64 9220983450959631991], align 8
8
9 define i32 @foo() nounwind {
10 entry:
11 %tmp87.2 = load i64* inttoptr (i32 add (i32 16, i32 ptrtoint ([3 x i64]* @bar to i32)) to i64*), align 8
12 %t0 = bitcast i64 %tmp87.2 to double
13 %tmp9192.2 = fptrunc double %t0 to float
14 %t1 = bitcast float %tmp9192.2 to i32
15 ret i32 %t1
16 }
17
18 define i32 @goo() nounwind {
19 entry:
20 %tmp87.2 = load i64* inttoptr (i32 add (i32 ptrtoint ([3 x i64]* @bar to i32), i32 16) to i64*), align 8
21 %t0 = bitcast i64 %tmp87.2 to double
22 %tmp9192.2 = fptrunc double %t0 to float
23 %t1 = bitcast float %tmp9192.2 to i32
24 ret i32 %t1
25 }
26