Skip to content

Commit cc158d4

Browse files
committed
Reapply [ConstantFold] Remove non-trivial gep-of-gep fold (#93823)
Reapply after #93956, which changed clang array initialization codegen to avoid size regressions for unoptimized builds. ----- This fold is subtly incorrect, because DL-unaware constant folding does not know the correct index type to use, and just performs the addition in the type that happens to already be there. This is incorrect, since sext(X)+sext(Y) is generally not the same as sext(X+Y). See the `@constexpr_gep_of_gep_with_narrow_type()` for a miscompile with the current implementation. One could try to restrict the fold to cases where no overflow occurs, but I'm not bothering with that here, because the DL-aware constant folding will take care of this anyway. I've only kept the straightforward zero-index case, where we just concatenate two GEPs.
1 parent e58f830 commit cc158d4

File tree

5 files changed

+15
-64
lines changed

5 files changed

+15
-64
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,64 +1416,16 @@ static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
14161416
if (GEP->getInRange())
14171417
return nullptr;
14181418

1419+
// Only handle simple case with leading zero index. We cannot perform an
1420+
// actual addition as we don't know the correct index type size to use.
14191421
Constant *Idx0 = cast<Constant>(Idxs[0]);
1420-
if (Idx0->isNullValue()) {
1421-
// Handle the simple case of a zero index.
1422-
SmallVector<Value*, 16> NewIndices;
1423-
NewIndices.reserve(Idxs.size() + GEP->getNumIndices());
1424-
NewIndices.append(GEP->idx_begin(), GEP->idx_end());
1425-
NewIndices.append(Idxs.begin() + 1, Idxs.end());
1426-
return ConstantExpr::getGetElementPtr(
1427-
GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()),
1428-
NewIndices, InBounds && GEP->isInBounds());
1429-
}
1430-
1431-
gep_type_iterator LastI = gep_type_end(GEP);
1432-
for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
1433-
I != E; ++I)
1434-
LastI = I;
1435-
1436-
// We can't combine GEPs if the last index is a struct type.
1437-
if (!LastI.isSequential())
1438-
return nullptr;
1439-
// We could perform the transform with non-constant index, but prefer leaving
1440-
// it as GEP of GEP rather than GEP of add for now.
1441-
ConstantInt *CI = dyn_cast<ConstantInt>(Idx0);
1442-
if (!CI)
1443-
return nullptr;
1444-
1445-
// TODO: This code may be extended to handle vectors as well.
1446-
auto *LastIdx = cast<Constant>(GEP->getOperand(GEP->getNumOperands()-1));
1447-
Type *LastIdxTy = LastIdx->getType();
1448-
if (LastIdxTy->isVectorTy())
1422+
if (!Idx0->isNullValue())
14491423
return nullptr;
14501424

14511425
SmallVector<Value*, 16> NewIndices;
14521426
NewIndices.reserve(Idxs.size() + GEP->getNumIndices());
1453-
NewIndices.append(GEP->idx_begin(), GEP->idx_end() - 1);
1454-
1455-
// Add the last index of the source with the first index of the new GEP.
1456-
// Make sure to handle the case when they are actually different types.
1457-
if (LastIdxTy != Idx0->getType()) {
1458-
unsigned CommonExtendedWidth =
1459-
std::max(LastIdxTy->getIntegerBitWidth(),
1460-
Idx0->getType()->getIntegerBitWidth());
1461-
CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1462-
1463-
Type *CommonTy =
1464-
Type::getIntNTy(LastIdxTy->getContext(), CommonExtendedWidth);
1465-
if (Idx0->getType() != CommonTy)
1466-
Idx0 = ConstantFoldCastInstruction(Instruction::SExt, Idx0, CommonTy);
1467-
if (LastIdx->getType() != CommonTy)
1468-
LastIdx =
1469-
ConstantFoldCastInstruction(Instruction::SExt, LastIdx, CommonTy);
1470-
if (!Idx0 || !LastIdx)
1471-
return nullptr;
1472-
}
1473-
1474-
NewIndices.push_back(ConstantExpr::get(Instruction::Add, Idx0, LastIdx));
1427+
NewIndices.append(GEP->idx_begin(), GEP->idx_end());
14751428
NewIndices.append(Idxs.begin() + 1, Idxs.end());
1476-
14771429
return ConstantExpr::getGetElementPtr(
14781430
GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()),
14791431
NewIndices, InBounds && GEP->isInBounds());

llvm/test/Other/constant-fold-gep.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
; Fold GEP of a GEP. Very simple cases are folded without targetdata.
106106

107-
; PLAIN: @Y = global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 2)
107+
; PLAIN: @Y = global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 1), i64 1)
108108
; PLAIN: @Z = global ptr getelementptr inbounds (i32, ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 0, i64 1, i32 0), i64 1)
109109
; OPT: @Y = local_unnamed_addr global ptr getelementptr inbounds (i8, ptr @ext, i64 48)
110110
; OPT: @Z = local_unnamed_addr global ptr getelementptr inbounds (i8, ptr @ext, i64 12)

llvm/test/Transforms/InstCombine/gepgep.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare void @use(ptr)
1010

1111
define void @f() {
1212
; CHECK-LABEL: define void @f() {
13-
; CHECK-NEXT: call void @use(ptr getelementptr (i8, ptr @buffer, i64 add (i64 sub (i64 0, i64 ptrtoint (ptr @buffer to i64)), i64 127)))
13+
; CHECK-NEXT: call void @use(ptr getelementptr (i8, ptr getelementptr (i8, ptr @buffer, i64 add (i64 sub (i64 0, i64 ptrtoint (ptr @buffer to i64)), i64 63)), i64 64))
1414
; CHECK-NEXT: ret void
1515
;
1616
call void @use(ptr getelementptr (i8, ptr getelementptr (i8, ptr @buffer, i64 add (i64 sub (i64 0, i64 ptrtoint (ptr @buffer to i64)), i64 63)), i64 64))

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,10 +1716,9 @@ if.else:
17161716

17171717
@g = external global i8
17181718

1719-
; FIXME: This is a miscompile
17201719
define ptr @constexpr_gep_of_gep_with_narrow_type() {
17211720
; CHECK-LABEL: @constexpr_gep_of_gep_with_narrow_type(
1722-
; CHECK-NEXT: ret ptr getelementptr (i8, ptr @g, i64 -2)
1721+
; CHECK-NEXT: ret ptr getelementptr (i8, ptr @g, i64 254)
17231722
;
17241723
ret ptr getelementptr (i8, ptr getelementptr (i8, ptr @g, i8 127), i8 127)
17251724
}

llvm/test/Transforms/InstCombine/ptrtoint-nullgep.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ define void @constant_fold_ptrtoint_of_gep_of_nullgep() {
6868
; LLPARSER-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) null, i64 1234) to i64))
6969
; LLPARSER-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 1234) to i64))
7070
; LLPARSER-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 1234) to i64))
71-
; LLPARSER-NEXT: call void @use_i64(i64 0)
72-
; LLPARSER-NEXT: call void @use_i64(i64 0)
73-
; LLPARSER-NEXT: call void @use_i64(i64 0)
74-
; LLPARSER-NEXT: call void @use_i64(i64 0)
71+
; LLPARSER-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
72+
; LLPARSER-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
73+
; LLPARSER-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
74+
; LLPARSER-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
7575
; LLPARSER-NEXT: ret void
7676
;
7777
; INSTSIMPLIFY-LABEL: define {{[^@]+}}@constant_fold_ptrtoint_of_gep_of_nullgep() {
@@ -83,10 +83,10 @@ define void @constant_fold_ptrtoint_of_gep_of_nullgep() {
8383
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) null, i64 1234) to i64))
8484
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 1234) to i64))
8585
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 1234) to i64))
86-
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 0)
87-
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 0)
88-
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 0)
89-
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 0)
86+
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
87+
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
88+
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
89+
; INSTSIMPLIFY-NEXT: call void @use_i64(i64 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i64 -1), i64 1) to i64))
9090
; INSTSIMPLIFY-NEXT: ret void
9191
;
9292
; INSTCOMBINE-LABEL: define {{[^@]+}}@constant_fold_ptrtoint_of_gep_of_nullgep() {

0 commit comments

Comments
 (0)