llvm.org GIT mirror llvm / cda9706
Change getelementptr folding to use APInt instead of uint64_t for offset computations. This fixes a truncation bug on targets that don't have 64-bit pointers. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79639 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
2 changed file(s) with 34 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
128128 Constant *Ptr = Ops[0];
129129 if (!TD || !cast(Ptr->getType())->getElementType()->isSized())
130130 return 0;
131
132 uint64_t BasePtr = 0;
131
132 unsigned BitWidth = TD->getTypeSizeInBits(TD->getIntPtrType(Context));
133 APInt BasePtr(BitWidth, 0);
133134 bool BaseIsInt = true;
134135 if (!Ptr->isNullValue()) {
135136 // If this is a inttoptr from a constant int, we can fold this as the base,
137138 if (ConstantExpr *CE = dyn_cast(Ptr))
138139 if (CE->getOpcode() == Instruction::IntToPtr)
139140 if (ConstantInt *Base = dyn_cast(CE->getOperand(0)))
140 BasePtr = Base->getZExtValue();
141 BasePtr = Base->getValue();
141142
142143 if (BasePtr == 0)
143144 BaseIsInt = false;
149150 if (!isa(Ops[i]))
150151 return 0;
151152
152 uint64_t Offset = TD->getIndexedOffset(Ptr->getType(),
153 (Value**)Ops+1, NumOps-1);
153 APInt Offset = APInt(BitWidth,
154 TD->getIndexedOffset(Ptr->getType(),
155 (Value**)Ops+1, NumOps-1));
154156 // If the base value for this address is a literal integer value, fold the
155157 // getelementptr to the resulting integer value casted to the pointer type.
156158 if (BaseIsInt) {
157 Constant *C = ConstantInt::get(TD->getIntPtrType(Context), Offset+BasePtr);
159 Constant *C = ConstantInt::get(Context, Offset+BasePtr);
158160 return ConstantExpr::getIntToPtr(C, ResultTy);
159161 }
160162
170172 if (isa(ATy) && ATy != Ptr->getType())
171173 break;
172174 // Determine which element of the array the offset points into.
173 uint64_t ElemSize = TD->getTypeAllocSize(ATy->getElementType());
175 APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
174176 if (ElemSize == 0)
175177 return 0;
176 uint64_t NewIdx = Offset / ElemSize;
178 APInt NewIdx = Offset.udiv(ElemSize);
177179 Offset -= NewIdx * ElemSize;
178180 NewIdxs.push_back(ConstantInt::get(TD->getIntPtrType(Context), NewIdx));
179181 Ty = ATy->getElementType();
180182 } else if (const StructType *STy = dyn_cast(Ty)) {
181 // Determine which field of the struct the offset points into.
183 // Determine which field of the struct the offset points into. The
184 // getZExtValue is at least as safe as the StructLayout API because we
185 // know the offset is within the struct at this point.
182186 const StructLayout &SL = *TD->getStructLayout(STy);
183 unsigned ElIdx = SL.getElementContainingOffset(Offset);
187 unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
184188 NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), ElIdx));
185 Offset -= SL.getElementOffset(ElIdx);
189 Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
186190 Ty = STy->getTypeAtIndex(ElIdx);
187191 } else {
188192 // We've reached some non-indexable type.
None ; RUN: llvm-as < %s | opt -instcombine
0 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | FileCheck %s
1
12 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"
23 target triple = "i386-apple-darwin10.0"
34
45 %0 = type { i8*, [19 x i8] }
56 %1 = type { i8*, [0 x i8] }
67
8 @array = external global [11 x i8]
9
710 @s = external global %0 ; <%0*> [#uses=1]
811 @"\01LC8" = external constant [17 x i8] ; <[17 x i8]*> [#uses=1]
912
13 ; Instcombine should be able to fold this getelementptr.
14
1015 define i32 @main() nounwind {
11 entry:
12 %0 = call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0), i8* undef, i8* getelementptr (%1* bitcast (%0* @s to %1*), i32 0, i32 1, i32 0)) nounwind ; [#uses=0]
16 ; CHECK: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @"\01LC8", i32 0, i32 0), i8* undef, i8* bitcast (i8** getelementptr (%1* bitcast (%0* @s to %1*), i32 1, i32 0) to i8*)) nounwind
17
18 call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0), i8* undef, i8* getelementptr (%1* bitcast (%0* @s to %1*), i32 0, i32 1, i32 0)) nounwind ; [#uses=0]
1319 ret i32 0
1420 }
1521
22 ; Instcombine should constant-fold the GEP so that indices that have
23 ; static array extents are within bounds of those array extents.
24 ; In the below, -1 is not in the range [0,11). After the transformation,
25 ; the same address is computed, but 3 is in the range of [0,11).
26
27 define i8* @foo() nounwind {
28 ; CHECK: ret i8* getelementptr ([11 x i8]* @array, i32 390451572, i32 3)
29 ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
30 }
31
1632 declare i32 @printf(i8*, ...) nounwind