Skip to content

Commit f623b3a

Browse files
committed
[ConstantFold] Fix GEP of GEP fold with opaque pointers
This was previously combining indices even though they operate on different types. For non-opaque pointers, the condition is automatically satisfied based on the pointer types being equal.
1 parent 923727e commit f623b3a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,8 +2347,11 @@ static bool isIndexInRangeOfArrayType(uint64_t NumElements,
23472347
// Combine Indices - If the source pointer to this getelementptr instruction
23482348
// is a getelementptr instruction, combine the indices of the two
23492349
// getelementptr instructions into a single instruction.
2350-
static Constant *foldGEPOfGEP(GEPOperator *GEP, bool InBounds,
2350+
static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
23512351
ArrayRef<Value *> Idxs) {
2352+
if (PointeeTy != GEP->getResultElementType())
2353+
return nullptr;
2354+
23522355
Constant *Idx0 = cast<Constant>(Idxs[0]);
23532356
if (Idx0->isNullValue()) {
23542357
// Handle the simple case of a zero index.
@@ -2491,7 +2494,7 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
24912494

24922495
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
24932496
if (auto *GEP = dyn_cast<GEPOperator>(CE))
2494-
if (Constant *C = foldGEPOfGEP(GEP, InBounds, Idxs))
2497+
if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
24952498
return C;
24962499

24972500
// Attempt to fold casts to the same type away. For example, folding:

llvm/test/Other/force-opaque-ptrs.ll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ define void @remangle_intrinsic() {
6464
ret void
6565
}
6666

67+
define i32* @constexpr_gep() {
68+
; CHECK-LABEL: define {{[^@]+}}@constexpr_gep() {
69+
; CHECK-NEXT: ret ptr getelementptr (i32, ptr getelementptr (i8, ptr null, i64 4), i64 1)
70+
;
71+
ret i32* getelementptr(i32, i32* bitcast (i8* getelementptr (i8, i8* null, i64 4) to i32*), i64 1)
72+
}
73+
6774
declare i8* @llvm.stacksave()
6875
declare void @llvm.stackprotector(i8*, i8**)
6976
declare <2 x i64> @llvm.masked.expandload.v2i64(i64*, <2 x i1>, <2 x i64>)

0 commit comments

Comments
 (0)