Skip to content

Commit cc2dc09

Browse files
committed
Reapply [ConstantFold] Drop gep of gep fold entirely (#95126)
Reapplying without changes. The flang+openmp buildbot failure should be addressed by #94541. ----- This is a followup to #93823 and drops the DataLayout-unaware GEP of GEP fold entirely. All cases are now left to the DataLayout-aware constant folder, which will fold everything to a single i8 GEP. We didn't have any test coverage for this fold in LLVM, but some Clang tests change.
1 parent 3d35b94 commit cc2dc09

10 files changed

+30
-63
lines changed

clang/test/CodeGenCUDA/managed-var.cu

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ __device__ __host__ float load2() {
127127

128128
// HOST-LABEL: define {{.*}}@_Z5load3v()
129129
// HOST: %ld.managed = load ptr, ptr @v2, align 16
130-
// HOST: %0 = getelementptr inbounds [100 x %struct.vec], ptr %ld.managed, i64 0, i64 1, i32 1
131-
// HOST: %1 = load float, ptr %0, align 4
132-
// HOST: ret float %1
130+
// HOST: %0 = getelementptr inbounds [100 x %struct.vec], ptr %ld.managed, i64 0, i64 1
131+
// HOST: %1 = getelementptr inbounds %struct.vec, ptr %0, i32 0, i32 1
132+
// HOST: %2 = load float, ptr %1, align 4
133+
// HOST: ret float %2
133134
float load3() {
134135
return v2[1].y;
135136
}
@@ -139,10 +140,11 @@ float load3() {
139140
// HOST: %0 = getelementptr inbounds [100 x %struct.vec], ptr %ld.managed, i64 0, i64 1
140141
// HOST: %1 = ptrtoint ptr %0 to i64
141142
// HOST: %ld.managed1 = load ptr, ptr @v2, align 16
142-
// HOST: %2 = getelementptr inbounds [100 x %struct.vec], ptr %ld.managed1, i64 0, i64 1, i32 1
143-
// HOST: %3 = ptrtoint ptr %2 to i64
144-
// HOST: %4 = sub i64 %3, %1
145-
// HOST: %sub.ptr.div = sdiv exact i64 %4, 4
143+
// HOST: %2 = getelementptr inbounds [100 x %struct.vec], ptr %ld.managed1, i64 0, i64 1
144+
// HOST: %3 = getelementptr inbounds %struct.vec, ptr %2, i32 0, i32 1
145+
// HOST: %4 = ptrtoint ptr %3 to i64
146+
// HOST: %5 = sub i64 %4, %1
147+
// HOST: %sub.ptr.div = sdiv exact i64 %5, 4
146148
// HOST: %conv = sitofp i64 %sub.ptr.div to float
147149
// HOST: ret float %conv
148150
float addr_taken2() {

clang/test/CodeGenCXX/2011-12-19-init-list-ctor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ struct S {
2121
// CHECK: store i32 0, ptr @arr
2222
// CHECK: call void @_ZN1AC1EPKc(ptr {{[^,]*}} getelementptr inbounds (%struct.S, ptr @arr, i32 0, i32 1), ptr noundef @.str)
2323
// CHECK: store i32 1, ptr getelementptr inbounds (%struct.S, ptr @arr, i64 1)
24-
// CHECK: call void @_ZN1AC1EPKc(ptr {{[^,]*}} getelementptr inbounds (%struct.S, ptr @arr, i64 1, i32 1), ptr noundef @.str.1)
24+
// CHECK: call void @_ZN1AC1EPKc(ptr {{[^,]*}} getelementptr inbounds (%struct.S, ptr getelementptr inbounds (%struct.S, ptr @arr, i64 1), i32 0, i32 1), ptr noundef @.str.1)
2525
// CHECK: store i32 2, ptr getelementptr inbounds (%struct.S, ptr @arr, i64 2)
26-
// CHECK: call void @_ZN1AC1EPKc(ptr {{[^,]*}} getelementptr inbounds (%struct.S, ptr @arr, i64 2, i32 1), ptr noundef @.str.2)
26+
// CHECK: call void @_ZN1AC1EPKc(ptr {{[^,]*}} getelementptr inbounds (%struct.S, ptr getelementptr inbounds (%struct.S, ptr @arr, i64 2), i32 0, i32 1), ptr noundef @.str.2)

clang/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist-pr12086.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ std::initializer_list<std::initializer_list<int>> nested = {
7979
// CHECK-DYNAMIC-BL: store i32 {{.*}}, ptr getelementptr inbounds (i32, ptr @_ZGR6nested1_, i64 1)
8080
// CHECK-DYNAMIC-BL: store ptr @_ZGR6nested1_,
8181
// CHECK-DYNAMIC-BL: ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 1), align 8
82-
// CHECK-DYNAMIC-BL: store i64 2, ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 1, i32 1), align 8
82+
// CHECK-DYNAMIC-BL: store i64 2, ptr getelementptr inbounds ({{.*}}, ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 1), i32 0, i32 1), align 8
8383
// CHECK-DYNAMIC-BL: store i32 5, ptr @_ZGR6nested2_
8484
// CHECK-DYNAMIC-BL: store i32 {{.*}}, ptr getelementptr inbounds (i32, ptr @_ZGR6nested2_, i64 1)
8585
// CHECK-DYNAMIC-BL: store ptr @_ZGR6nested2_,
8686
// CHECK-DYNAMIC-BL: ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 2), align 8
87-
// CHECK-DYNAMIC-BL: store i64 2, ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 2, i32 1), align 8
87+
// CHECK-DYNAMIC-BL: store i64 2, ptr getelementptr inbounds ({{.*}}, ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 2), i32 0, i32 1), align 8
8888
// CHECK-DYNAMIC-BL: store ptr @_ZGR6nested_,
8989
// CHECK-DYNAMIC-BL: ptr @nested, align 8
9090
// CHECK-DYNAMIC-BL: store i64 3, ptr getelementptr inbounds ({{.*}}, ptr @nested, i32 0, i32 1), align 8
@@ -119,13 +119,13 @@ std::initializer_list<std::initializer_list<int>> nested = {
119119
// CHECK-DYNAMIC-BE: store ptr @_ZGR6nested1_,
120120
// CHECK-DYNAMIC-BE: ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 1), align 8
121121
// CHECK-DYNAMIC-BE: store ptr getelementptr inbounds ([2 x i32], ptr @_ZGR6nested1_, i64 0, i64 2),
122-
// CHECK-DYNAMIC-BE: ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 1, i32 1), align 8
122+
// CHECK-DYNAMIC-BE: ptr getelementptr inbounds ({{.*}}, ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 1), i32 0, i32 1), align 8
123123
// CHECK-DYNAMIC-BE: store i32 5, ptr @_ZGR6nested2_
124124
// CHECK-DYNAMIC-BE: store i32 {{.*}}, ptr getelementptr inbounds (i32, ptr @_ZGR6nested2_, i64 1)
125125
// CHECK-DYNAMIC-BE: store ptr @_ZGR6nested2_,
126126
// CHECK-DYNAMIC-BE: ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 2), align 8
127127
// CHECK-DYNAMIC-BE: store ptr getelementptr inbounds ([2 x i32], ptr @_ZGR6nested2_, i64 0, i64 2),
128-
// CHECK-DYNAMIC-BE: ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 2, i32 1), align 8
128+
// CHECK-DYNAMIC-BE: ptr getelementptr inbounds ({{.*}}, ptr getelementptr inbounds ({{.*}}, ptr @_ZGR6nested_, i64 2), i32 0, i32 1), align 8
129129
// CHECK-DYNAMIC-BE: store ptr @_ZGR6nested_,
130130
// CHECK-DYNAMIC-BE: ptr @nested, align 8
131131
// CHECK-DYNAMIC-BE: store ptr getelementptr inbounds ([3 x {{.*}}], ptr @_ZGR6nested_, i64 0, i64 3),

clang/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ namespace partly_constant {
365365
//
366366
// Second init list.
367367
// CHECK: store ptr {{.*}}@[[PARTLY_CONSTANT_SECOND]]{{.*}}, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 1)
368-
// CHECK: store i64 2, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 1, i32 1)
368+
// CHECK: store i64 2, ptr getelementptr inbounds ({{.*}}, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 1), i32 0, i32 1)
369369
//
370370
// Third init list.
371371
// CHECK-NOT: @[[PARTLY_CONSTANT_THIRD]],
372372
// CHECK: store ptr {{.*}}@[[PARTLY_CONSTANT_THIRD]]{{.*}}, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 2)
373-
// CHECK: store i64 4, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 2, i32 1)
373+
// CHECK: store i64 4, ptr getelementptr inbounds ({{.*}}, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 2), i32 0, i32 1)
374374
// CHECK-NOT: @[[PARTLY_CONSTANT_THIRD]],
375375
//
376376
// Outer init list.

clang/test/CodeGenCXX/ms-inline-asm-fields.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" int test_param_field(A p) {
2424

2525
extern "C" int test_namespace_global() {
2626
// CHECK: define{{.*}} i32 @test_namespace_global()
27-
// CHECK: call i32 asm sideeffect inteldialect "mov eax, $1", "{{.*}}"(ptr elementtype(i32) getelementptr inbounds (%struct.A, ptr @_ZN4asdf8a_globalE, i32 0, i32 2, i32 1))
27+
// CHECK: call i32 asm sideeffect inteldialect "mov eax, $1", "{{.*}}"(ptr elementtype(i32) getelementptr inbounds (%"struct.A::B", ptr getelementptr inbounds (%struct.A, ptr @_ZN4asdf8a_globalE, i32 0, i32 2), i32 0, i32 1))
2828
// CHECK: ret i32
2929
__asm mov eax, asdf::a_global.a3.b2
3030
}

clang/test/OpenMP/threadprivate_codegen.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,7 +2586,7 @@ int foobar() {
25862586
// SIMD1-NEXT: [[TMP12:%.*]] = load i32, ptr [[RES]], align 4
25872587
// SIMD1-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP12]], [[TMP11]]
25882588
// SIMD1-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4
2589-
// SIMD1-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4
2589+
// SIMD1-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4
25902590
// SIMD1-NEXT: [[TMP14:%.*]] = load i32, ptr [[RES]], align 4
25912591
// SIMD1-NEXT: [[ADD4:%.*]] = add nsw i32 [[TMP14]], [[TMP13]]
25922592
// SIMD1-NEXT: store i32 [[ADD4]], ptr [[RES]], align 4
@@ -2663,7 +2663,7 @@ int foobar() {
26632663
// SIMD1-NEXT: [[TMP6:%.*]] = load i32, ptr [[RES]], align 4
26642664
// SIMD1-NEXT: [[ADD2:%.*]] = add nsw i32 [[TMP6]], [[TMP5]]
26652665
// SIMD1-NEXT: store i32 [[ADD2]], ptr [[RES]], align 4
2666-
// SIMD1-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4
2666+
// SIMD1-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4
26672667
// SIMD1-NEXT: [[TMP8:%.*]] = load i32, ptr [[RES]], align 4
26682668
// SIMD1-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP8]], [[TMP7]]
26692669
// SIMD1-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4
@@ -3052,7 +3052,7 @@ int foobar() {
30523052
// SIMD2-NEXT: [[TMP12:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG187:![0-9]+]]
30533053
// SIMD2-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP12]], [[TMP11]], !dbg [[DBG187]]
30543054
// SIMD2-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4, !dbg [[DBG187]]
3055-
// SIMD2-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4, !dbg [[DBG188:![0-9]+]]
3055+
// SIMD2-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4, !dbg [[DBG188:![0-9]+]]
30563056
// SIMD2-NEXT: [[TMP14:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG189:![0-9]+]]
30573057
// SIMD2-NEXT: [[ADD4:%.*]] = add nsw i32 [[TMP14]], [[TMP13]], !dbg [[DBG189]]
30583058
// SIMD2-NEXT: store i32 [[ADD4]], ptr [[RES]], align 4, !dbg [[DBG189]]
@@ -3133,7 +3133,7 @@ int foobar() {
31333133
// SIMD2-NEXT: [[TMP6:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG222:![0-9]+]]
31343134
// SIMD2-NEXT: [[ADD2:%.*]] = add nsw i32 [[TMP6]], [[TMP5]], !dbg [[DBG222]]
31353135
// SIMD2-NEXT: store i32 [[ADD2]], ptr [[RES]], align 4, !dbg [[DBG222]]
3136-
// SIMD2-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4, !dbg [[DBG223:![0-9]+]]
3136+
// SIMD2-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4, !dbg [[DBG223:![0-9]+]]
31373137
// SIMD2-NEXT: [[TMP8:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG224:![0-9]+]]
31383138
// SIMD2-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP8]], [[TMP7]], !dbg [[DBG224]]
31393139
// SIMD2-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4, !dbg [[DBG224]]
@@ -5707,7 +5707,7 @@ int foobar() {
57075707
// SIMD3-NEXT: [[TMP12:%.*]] = load i32, ptr [[RES]], align 4
57085708
// SIMD3-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP12]], [[TMP11]]
57095709
// SIMD3-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4
5710-
// SIMD3-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4
5710+
// SIMD3-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4
57115711
// SIMD3-NEXT: [[TMP14:%.*]] = load i32, ptr [[RES]], align 4
57125712
// SIMD3-NEXT: [[ADD4:%.*]] = add nsw i32 [[TMP14]], [[TMP13]]
57135713
// SIMD3-NEXT: store i32 [[ADD4]], ptr [[RES]], align 4
@@ -5784,7 +5784,7 @@ int foobar() {
57845784
// SIMD3-NEXT: [[TMP6:%.*]] = load i32, ptr [[RES]], align 4
57855785
// SIMD3-NEXT: [[ADD2:%.*]] = add nsw i32 [[TMP6]], [[TMP5]]
57865786
// SIMD3-NEXT: store i32 [[ADD2]], ptr [[RES]], align 4
5787-
// SIMD3-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4
5787+
// SIMD3-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4
57885788
// SIMD3-NEXT: [[TMP8:%.*]] = load i32, ptr [[RES]], align 4
57895789
// SIMD3-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP8]], [[TMP7]]
57905790
// SIMD3-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4
@@ -6173,7 +6173,7 @@ int foobar() {
61736173
// SIMD4-NEXT: [[TMP12:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG187:![0-9]+]]
61746174
// SIMD4-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP12]], [[TMP11]], !dbg [[DBG187]]
61756175
// SIMD4-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4, !dbg [[DBG187]]
6176-
// SIMD4-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4, !dbg [[DBG188:![0-9]+]]
6176+
// SIMD4-NEXT: [[TMP13:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4, !dbg [[DBG188:![0-9]+]]
61776177
// SIMD4-NEXT: [[TMP14:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG189:![0-9]+]]
61786178
// SIMD4-NEXT: [[ADD4:%.*]] = add nsw i32 [[TMP14]], [[TMP13]], !dbg [[DBG189]]
61796179
// SIMD4-NEXT: store i32 [[ADD4]], ptr [[RES]], align 4, !dbg [[DBG189]]
@@ -6254,7 +6254,7 @@ int foobar() {
62546254
// SIMD4-NEXT: [[TMP6:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG222:![0-9]+]]
62556255
// SIMD4-NEXT: [[ADD2:%.*]] = add nsw i32 [[TMP6]], [[TMP5]], !dbg [[DBG222]]
62566256
// SIMD4-NEXT: store i32 [[ADD2]], ptr [[RES]], align 4, !dbg [[DBG222]]
6257-
// SIMD4-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1, i64 1), align 4, !dbg [[DBG223:![0-9]+]]
6257+
// SIMD4-NEXT: [[TMP7:%.*]] = load i32, ptr getelementptr inbounds ([3 x %struct.S1], ptr getelementptr inbounds ([2 x [3 x %struct.S1]], ptr @arr_x, i64 0, i64 1), i64 0, i64 1), align 4, !dbg [[DBG223:![0-9]+]]
62586258
// SIMD4-NEXT: [[TMP8:%.*]] = load i32, ptr [[RES]], align 4, !dbg [[DBG224:![0-9]+]]
62596259
// SIMD4-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP8]], [[TMP7]], !dbg [[DBG224]]
62606260
// SIMD4-NEXT: store i32 [[ADD3]], ptr [[RES]], align 4, !dbg [[DBG224]]

llvm/include/llvm/IR/ConstantFold.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace llvm {
5252
Constant *V2);
5353
Constant *ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
5454
Constant *C1, Constant *C2);
55-
Constant *ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool InBounds,
55+
Constant *ConstantFoldGetElementPtr(Type *Ty, Constant *C,
5656
std::optional<ConstantRange> InRange,
5757
ArrayRef<Value *> Idxs);
5858
} // End llvm namespace

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5068,9 +5068,8 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
50685068
return nullptr;
50695069

50705070
if (!ConstantExpr::isSupportedGetElementPtr(SrcTy))
5071-
// TODO(gep_nowrap): Pass on the whole GEPNoWrapFlags.
5072-
return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr),
5073-
NW.isInBounds(), std::nullopt, Indices);
5071+
return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr), std::nullopt,
5072+
Indices);
50745073

50755074
auto *CE =
50765075
ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices, NW);

llvm/lib/IR/ConstantFold.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,35 +1404,7 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
14041404
return nullptr;
14051405
}
14061406

1407-
// Combine Indices - If the source pointer to this getelementptr instruction
1408-
// is a getelementptr instruction, combine the indices of the two
1409-
// getelementptr instructions into a single instruction.
1410-
static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
1411-
ArrayRef<Value *> Idxs) {
1412-
if (PointeeTy != GEP->getResultElementType())
1413-
return nullptr;
1414-
1415-
// Leave inrange handling to DL-aware constant folding.
1416-
if (GEP->getInRange())
1417-
return nullptr;
1418-
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.
1421-
Constant *Idx0 = cast<Constant>(Idxs[0]);
1422-
if (!Idx0->isNullValue())
1423-
return nullptr;
1424-
1425-
SmallVector<Value*, 16> NewIndices;
1426-
NewIndices.reserve(Idxs.size() + GEP->getNumIndices());
1427-
NewIndices.append(GEP->idx_begin(), GEP->idx_end());
1428-
NewIndices.append(Idxs.begin() + 1, Idxs.end());
1429-
return ConstantExpr::getGetElementPtr(
1430-
GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()),
1431-
NewIndices, InBounds && GEP->isInBounds());
1432-
}
1433-
14341407
Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
1435-
bool InBounds,
14361408
std::optional<ConstantRange> InRange,
14371409
ArrayRef<Value *> Idxs) {
14381410
if (Idxs.empty()) return C;
@@ -1462,10 +1434,5 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
14621434
cast<VectorType>(GEPTy)->getElementCount(), C)
14631435
: C;
14641436

1465-
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
1466-
if (auto *GEP = dyn_cast<GEPOperator>(CE))
1467-
if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
1468-
return C;
1469-
14701437
return nullptr;
14711438
}

llvm/lib/IR/Constants.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,8 +2438,7 @@ Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
24382438
assert(Ty && "Must specify element type");
24392439
assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!");
24402440

2441-
if (Constant *FC =
2442-
ConstantFoldGetElementPtr(Ty, C, NW.isInBounds(), InRange, Idxs))
2441+
if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InRange, Idxs))
24432442
return FC; // Fold a few common cases.
24442443

24452444
assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && "GEP indices invalid!");

0 commit comments

Comments
 (0)