Skip to content

Commit 8e8d259

Browse files
authored
[ConstantFolding] Canonicalize constexpr GEPs to i8 (#89872)
This patch canonicalizes constant expression GEPs to use i8 source element type, aka ptradd. This is the ConstantFolding equivalent of the InstCombine canonicalization introduced in #68882. I believe all our optimizations working on constant expression GEPs (like GlobalOpt etc) have already been switched to work on offsets, so I don't expect any significant fallout from this change. This is part of: https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699
1 parent 2217d17 commit 8e8d259

File tree

98 files changed

+561
-585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+561
-585
lines changed

clang/test/CodeGen/RISCV/riscv-inline-asm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ extern int var, arr[2][2];
4949
struct Pair { int a, b; } pair;
5050

5151
// CHECK-LABEL: test_s(
52-
// CHECK: call void asm sideeffect "// $0 $1 $2", "s,s,s"(ptr nonnull @var, ptr nonnull getelementptr inbounds ([2 x [2 x i32]], ptr @arr, {{.*}}), ptr nonnull @test_s)
53-
// CHECK: call void asm sideeffect "// $0", "s"(ptr nonnull getelementptr inbounds (%struct.Pair, ptr @pair, {{.*}}))
54-
// CHECK: call void asm sideeffect "// $0 $1 $2", "S,S,S"(ptr nonnull @var, ptr nonnull getelementptr inbounds ([2 x [2 x i32]], ptr @arr, {{.*}}), ptr nonnull @test_s)
52+
// CHECK: call void asm sideeffect "// $0 $1 $2", "s,s,s"(ptr nonnull @var, ptr nonnull getelementptr inbounds (i8, ptr @arr, {{.*}}), ptr nonnull @test_s)
53+
// CHECK: call void asm sideeffect "// $0", "s"(ptr nonnull getelementptr inbounds (i8, ptr @pair, {{.*}}))
54+
// CHECK: call void asm sideeffect "// $0 $1 $2", "S,S,S"(ptr nonnull @var, ptr nonnull getelementptr inbounds (i8, ptr @arr, {{.*}}), ptr nonnull @test_s)
5555
void test_s(void) {
5656
asm("// %0 %1 %2" :: "s"(&var), "s"(&arr[1][1]), "s"(test_s));
5757
asm("// %0" :: "s"(&pair.b));

clang/test/CodeGen/attr-counted-by.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ int test12_a, test12_b;
10981098
// SANITIZE-WITH-ATTR-NEXT: tail call void @__ubsan_handle_out_of_bounds_abort(ptr nonnull @[[GLOB20:[0-9]+]], i64 0) #[[ATTR10]], !nosanitize [[META2]]
10991099
// SANITIZE-WITH-ATTR-NEXT: unreachable, !nosanitize [[META2]]
11001100
// SANITIZE-WITH-ATTR: handler.type_mismatch6:
1101-
// SANITIZE-WITH-ATTR-NEXT: tail call void @__ubsan_handle_type_mismatch_v1_abort(ptr nonnull @[[GLOB21:[0-9]+]], i64 ptrtoint (ptr getelementptr inbounds ([[STRUCT_ANON_5:%.*]], ptr @test12_foo, i64 1, i32 0, i32 0, i32 0) to i64)) #[[ATTR10]], !nosanitize [[META2]]
1101+
// SANITIZE-WITH-ATTR-NEXT: tail call void @__ubsan_handle_type_mismatch_v1_abort(ptr nonnull @[[GLOB21:[0-9]+]], i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @test12_foo, i64 4) to i64)) #[[ATTR10]], !nosanitize [[META2]]
11021102
// SANITIZE-WITH-ATTR-NEXT: unreachable, !nosanitize [[META2]]
11031103
//
11041104
// NO-SANITIZE-WITH-ATTR-LABEL: define dso_local noundef i32 @test12(
@@ -1111,7 +1111,7 @@ int test12_a, test12_b;
11111111
// NO-SANITIZE-WITH-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [6 x i32], ptr [[BAZ]], i64 0, i64 [[IDXPROM]]
11121112
// NO-SANITIZE-WITH-ATTR-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
11131113
// NO-SANITIZE-WITH-ATTR-NEXT: store i32 [[TMP0]], ptr @test12_b, align 4, !tbaa [[TBAA2]]
1114-
// NO-SANITIZE-WITH-ATTR-NEXT: [[TMP1:%.*]] = load i32, ptr getelementptr inbounds ([[STRUCT_ANON_5:%.*]], ptr @test12_foo, i64 1, i32 0, i32 0, i32 0), align 4, !tbaa [[TBAA2]]
1114+
// NO-SANITIZE-WITH-ATTR-NEXT: [[TMP1:%.*]] = load i32, ptr getelementptr inbounds (i8, ptr @test12_foo, i64 4), align 4, !tbaa [[TBAA2]]
11151115
// NO-SANITIZE-WITH-ATTR-NEXT: store i32 [[TMP1]], ptr @test12_a, align 4, !tbaa [[TBAA2]]
11161116
// NO-SANITIZE-WITH-ATTR-NEXT: br label [[FOR_COND:%.*]]
11171117
// NO-SANITIZE-WITH-ATTR: for.cond:
@@ -1140,7 +1140,7 @@ int test12_a, test12_b;
11401140
// SANITIZE-WITHOUT-ATTR-NEXT: tail call void @__ubsan_handle_out_of_bounds_abort(ptr nonnull @[[GLOB4:[0-9]+]], i64 0) #[[ATTR8]], !nosanitize [[META9]]
11411141
// SANITIZE-WITHOUT-ATTR-NEXT: unreachable, !nosanitize [[META9]]
11421142
// SANITIZE-WITHOUT-ATTR: handler.type_mismatch6:
1143-
// SANITIZE-WITHOUT-ATTR-NEXT: tail call void @__ubsan_handle_type_mismatch_v1_abort(ptr nonnull @[[GLOB5:[0-9]+]], i64 ptrtoint (ptr getelementptr inbounds ([[STRUCT_ANON_5:%.*]], ptr @test12_foo, i64 1, i32 0, i32 0, i32 0) to i64)) #[[ATTR8]], !nosanitize [[META9]]
1143+
// SANITIZE-WITHOUT-ATTR-NEXT: tail call void @__ubsan_handle_type_mismatch_v1_abort(ptr nonnull @[[GLOB5:[0-9]+]], i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @test12_foo, i64 4) to i64)) #[[ATTR8]], !nosanitize [[META9]]
11441144
// SANITIZE-WITHOUT-ATTR-NEXT: unreachable, !nosanitize [[META9]]
11451145
//
11461146
// NO-SANITIZE-WITHOUT-ATTR-LABEL: define dso_local noundef i32 @test12(
@@ -1153,7 +1153,7 @@ int test12_a, test12_b;
11531153
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [6 x i32], ptr [[BAZ]], i64 0, i64 [[IDXPROM]]
11541154
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
11551155
// NO-SANITIZE-WITHOUT-ATTR-NEXT: store i32 [[TMP0]], ptr @test12_b, align 4, !tbaa [[TBAA2]]
1156-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[TMP1:%.*]] = load i32, ptr getelementptr inbounds ([[STRUCT_ANON_5:%.*]], ptr @test12_foo, i64 1, i32 0, i32 0, i32 0), align 4, !tbaa [[TBAA2]]
1156+
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[TMP1:%.*]] = load i32, ptr getelementptr inbounds (i8, ptr @test12_foo, i64 4), align 4, !tbaa [[TBAA2]]
11571157
// NO-SANITIZE-WITHOUT-ATTR-NEXT: store i32 [[TMP1]], ptr @test12_a, align 4, !tbaa [[TBAA2]]
11581158
// NO-SANITIZE-WITHOUT-ATTR-NEXT: br label [[FOR_COND:%.*]]
11591159
// NO-SANITIZE-WITHOUT-ATTR: for.cond:
@@ -1315,7 +1315,7 @@ int test14(int idx) {
13151315
// NO-SANITIZE-WITH-ATTR-SAME: i32 noundef [[IDX:%.*]]) local_unnamed_addr #[[ATTR4]] {
13161316
// NO-SANITIZE-WITH-ATTR-NEXT: entry:
13171317
// NO-SANITIZE-WITH-ATTR-NEXT: [[IDXPROM:%.*]] = sext i32 [[IDX]] to i64
1318-
// NO-SANITIZE-WITH-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr getelementptr inbounds ([[STRUCT_ANON_8:%.*]], ptr @__const.test15.foo, i64 1, i32 0), i64 0, i64 [[IDXPROM]]
1318+
// NO-SANITIZE-WITH-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr getelementptr inbounds (i8, ptr @__const.test15.foo, i64 8), i64 0, i64 [[IDXPROM]]
13191319
// NO-SANITIZE-WITH-ATTR-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
13201320
// NO-SANITIZE-WITH-ATTR-NEXT: ret i32 [[TMP0]]
13211321
//
@@ -1336,7 +1336,7 @@ int test14(int idx) {
13361336
// NO-SANITIZE-WITHOUT-ATTR-SAME: i32 noundef [[IDX:%.*]]) local_unnamed_addr #[[ATTR1]] {
13371337
// NO-SANITIZE-WITHOUT-ATTR-NEXT: entry:
13381338
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[IDXPROM:%.*]] = sext i32 [[IDX]] to i64
1339-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr getelementptr inbounds ([[STRUCT_ANON_8:%.*]], ptr @__const.test15.foo, i64 1, i32 0), i64 0, i64 [[IDXPROM]]
1339+
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr getelementptr inbounds (i8, ptr @__const.test15.foo, i64 8), i64 0, i64 [[IDXPROM]]
13401340
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
13411341
// NO-SANITIZE-WITHOUT-ATTR-NEXT: ret i32 [[TMP0]]
13421342
//

clang/test/CodeGenCXX/atomicinit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace PR18097 {
8686
};
8787
// CHECK-LABEL: define {{.*}} @__cxx_global_var_init
8888
// CHECK: call void @_ZN7PR180977dynamic1XC1Ei(ptr {{[^,]*}} @_ZN7PR180977dynamic1yE, i32 noundef 4)
89-
// CHECK: store i32 5, ptr getelementptr inbounds ({{.*}}, ptr @_ZN7PR180977dynamic1yE, i32 0, i32 1)
89+
// CHECK: store i32 5, ptr getelementptr inbounds (i8, ptr @_ZN7PR180977dynamic1yE, i32 4)
9090
Y y = { X(4), 5 };
9191
}
9292

@@ -110,7 +110,7 @@ namespace PR18097 {
110110
// CHECK-LABEL: define {{.*}} @__cxx_global_var_init
111111
// CHECK: tail call void @llvm.memcpy.p0.p0.i32(ptr{{.*}} @_ZN7PR180978constant2y2E, ptr{{.*}} @_ZN7PR180978constantL1xE, i32 3, i1 false)
112112
// CHECK: %0 = load i32, ptr @_ZN7PR180978constant1zE
113-
// CHECK: store i32 %0, ptr getelementptr inbounds (%"struct.PR18097::constant::Y", ptr @_ZN7PR180978constant2y2E, i32 0, i32 1)
113+
// CHECK: store i32 %0, ptr getelementptr inbounds (i8, ptr @_ZN7PR180978constant2y2E, i32 4)
114114
int z;
115115
constexpr X x{1};
116116
Y y2 = { x, z };

clang/test/CodeGenCXX/auto-var-init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ TEST_UNINIT(base, base);
13461346
// PATTERN-O0: call void @llvm.memcpy{{.*}} @__const.test_base_uninit.uninit{{.+}}), !annotation [[AUTO_INIT]]
13471347
// ZERO-LABEL: @test_base_uninit()
13481348
// ZERO-O0: call void @llvm.memset{{.*}}, i8 0,{{.+}}), !annotation [[AUTO_INIT]]
1349-
// ZERO-O1: store ptr getelementptr inbounds inrange(-16, 16) ({ [4 x ptr] }, ptr @_ZTV4base, i64 0, i32 0, i64 2), {{.*}}, align 8
1349+
// ZERO-O1: store ptr getelementptr inbounds inrange(-16, 16) (i8, ptr @_ZTV4base, i64 16), {{.*}}, align 8
13501350
// ZERO-O1-NOT: !annotation
13511351

13521352
TEST_BRACES(base, base);
@@ -1367,7 +1367,7 @@ TEST_UNINIT(derived, derived);
13671367
// ZERO-LABEL: @test_derived_uninit()
13681368
// ZERO-O0: call void @llvm.memset{{.*}}, i8 0, {{.+}}), !annotation [[AUTO_INIT]]
13691369
// ZERO-O1: store i64 0, {{.*}} align 8, !annotation [[AUTO_INIT]]
1370-
// ZERO-O1: store ptr getelementptr inbounds inrange(-16, 16) ({ [4 x ptr] }, ptr @_ZTV7derived, i64 0, i32 0, i64 2), {{.*}} align 8
1370+
// ZERO-O1: store ptr getelementptr inbounds inrange(-16, 16) (i8, ptr @_ZTV7derived, i64 16), {{.*}} align 8
13711371

13721372
TEST_BRACES(derived, derived);
13731373
// CHECK-LABEL: @test_derived_braces()

clang/test/Profile/c-unreachable-after-switch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
// CHECK-LABEL: @foo()
66
// CHECK: store {{.*}} @[[C]]
77
void foo(void) {
8-
// CHECK: store {{.*}} @[[C]], i64 0, i64 2
8+
// CHECK: store {{.*}} @[[C]], i64 16)
99
switch (0) {
1010
default:
1111
return;
1212
}
1313
// We shouldn't emit the unreachable counter. This used to crash in GlobalDCE.
14-
// CHECK-NOT: store {{.*}} @[[C]], i64 0, i64 1}
14+
// CHECK-NOT: store {{.*}} @[[C]], i64 8)
1515
}

compiler-rt/test/profile/Linux/counter_promo_for.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ __attribute__((noinline)) void bar(int i) { g += i; }
1919

2020
__attribute__((noinline)) void foo(int n, int N) {
2121
// PROMO-LABEL: @foo
22-
// PROMO: load{{.*}}@__profc_foo{{.*}} 3){{.*}}
22+
// PROMO: load{{.*}}@__profc_foo{{.*}} 24){{.*}}
2323
// PROMO-NEXT: add
24-
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 3){{.*}}
24+
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 24){{.*}}
2525
// PROMO: load{{.*}}@__profc_foo, align
2626
// PROMO-NEXT: add
2727
// PROMO-NEXT: store{{.*}}@__profc_foo, align
28-
// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 1){{.*}}
28+
// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
2929
// PROMO-NEXT: add
30-
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 1){{.*}}
31-
// PROMO: load{{.*}}@__profc_foo{{.*}} 2){{.*}}
30+
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
31+
// PROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
3232
// PROMO-NEXT: add
33-
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 2){{.*}}
33+
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
3434
//
3535
// NOPROMO-LABEL: @foo
3636
// NOPROMO: load{{.*}}@__profc_foo, align
3737
// NOPROMO-NEXT: add
3838
// NOPROMO-NEXT: store{{.*}}@__profc_foo, align
39-
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 1){{.*}}
39+
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
4040
// NOPROMO-NEXT: add
41-
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 1){{.*}}
42-
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 2){{.*}}
41+
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
42+
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
4343
// NOPROMO-NEXT: add
44-
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 2){{.*}}
44+
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
4545
int i;
4646
for (i = 0; i < N; i++) {
4747
if (i < n + 1)

compiler-rt/test/profile/Linux/counter_promo_while.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ __attribute__((noinline)) void foo(int n, int N) {
2020
// PROMO: load{{.*}}@__profc_foo, align
2121
// PROMO-NEXT: add
2222
// PROMO-NEXT: store{{.*}}@__profc_foo, align
23-
// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 1){{.*}}
23+
// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
2424
// PROMO-NEXT: add
25-
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 1){{.*}}
26-
// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 2){{.*}}
25+
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
26+
// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
2727
// PROMO-NEXT: add
28-
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 2){{.*}}
28+
// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
2929
//
3030
// NOPROMO-LABEL: @foo
3131
// NOPROMO: load{{.*}}@__profc_foo, align
3232
// NOPROMO-NEXT: add
3333
// NOPROMO-NEXT: store{{.*}}@__profc_foo, align
34-
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 1){{.*}}
34+
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
3535
// NOPROMO-NEXT: add
36-
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 1){{.*}}
37-
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 2){{.*}}
36+
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
37+
// NOPROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
3838
// NOPROMO-NEXT: add
39-
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 2){{.*}}
39+
// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
4040
int i = 0;
4141
while (i < N) {
4242
if (i < n + 1)

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,6 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
869869
bool InBounds = GEP->isInBounds();
870870

871871
Type *SrcElemTy = GEP->getSourceElementType();
872-
Type *ResElemTy = GEP->getResultElementType();
873872
Type *ResTy = GEP->getType();
874873
if (!SrcElemTy->isSized() || isa<ScalableVectorType>(SrcElemTy))
875874
return nullptr;
@@ -944,43 +943,18 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
944943
return ConstantExpr::getIntToPtr(C, ResTy);
945944
}
946945

947-
// Otherwise form a regular getelementptr. Recompute the indices so that
948-
// we eliminate over-indexing of the notional static type array bounds.
949-
// This makes it easy to determine if the getelementptr is "inbounds".
950-
951-
// For GEPs of GlobalValues, use the value type, otherwise use an i8 GEP.
952-
if (auto *GV = dyn_cast<GlobalValue>(Ptr))
953-
SrcElemTy = GV->getValueType();
954-
else
955-
SrcElemTy = Type::getInt8Ty(Ptr->getContext());
956-
957-
if (!SrcElemTy->isSized())
958-
return nullptr;
959-
960-
Type *ElemTy = SrcElemTy;
961-
SmallVector<APInt> Indices = DL.getGEPIndicesForOffset(ElemTy, Offset);
962-
if (Offset != 0)
963-
return nullptr;
964-
965-
// Try to add additional zero indices to reach the desired result element
966-
// type.
967-
// TODO: Should we avoid extra zero indices if ResElemTy can't be reached and
968-
// we'll have to insert a bitcast anyway?
969-
while (ElemTy != ResElemTy) {
970-
Type *NextTy = GetElementPtrInst::getTypeAtIndex(ElemTy, (uint64_t)0);
971-
if (!NextTy)
972-
break;
973-
974-
Indices.push_back(APInt::getZero(isa<StructType>(ElemTy) ? 32 : BitWidth));
975-
ElemTy = NextTy;
946+
// Try to infer inbounds for GEPs of globals.
947+
if (!InBounds && Offset.isNonNegative()) {
948+
bool CanBeNull, CanBeFreed;
949+
uint64_t DerefBytes =
950+
Ptr->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
951+
InBounds = DerefBytes != 0 && !CanBeNull && Offset.sle(DerefBytes);
976952
}
977953

978-
SmallVector<Constant *, 32> NewIdxs;
979-
for (const APInt &Index : Indices)
980-
NewIdxs.push_back(ConstantInt::get(
981-
Type::getIntNTy(Ptr->getContext(), Index.getBitWidth()), Index));
982-
983-
return ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs, InBounds,
954+
// Otherwise canonicalize this to a single ptradd.
955+
LLVMContext &Ctx = Ptr->getContext();
956+
return ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Ptr,
957+
ConstantInt::get(Ctx, Offset), InBounds,
984958
InRange);
985959
}
986960

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@
106106

107107
; PLAIN: @Y = global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 2)
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)
109-
; OPT: @Y = local_unnamed_addr global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 2)
110-
; OPT: @Z = local_unnamed_addr global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 0, i64 1, i32 1)
111-
; TO: @Y = local_unnamed_addr global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 2)
112-
; TO: @Z = local_unnamed_addr global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 0, i64 1, i32 1)
109+
; OPT: @Y = local_unnamed_addr global ptr getelementptr inbounds (i8, ptr @ext, i64 48)
110+
; OPT: @Z = local_unnamed_addr global ptr getelementptr inbounds (i8, ptr @ext, i64 12)
111+
; TO: @Y = local_unnamed_addr global ptr getelementptr inbounds (i8, ptr @ext, i64 48)
112+
; TO: @Z = local_unnamed_addr global ptr getelementptr inbounds (i8, ptr @ext, i64 12)
113113

114114
@ext = external global [3 x { i32, i32 }]
115115
@Y = global ptr getelementptr inbounds ([3 x { i32, i32 }], ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 1), i64 1)
@@ -433,10 +433,10 @@ define ptr @fO() nounwind {
433433
; PLAIN: ret ptr %t
434434
; PLAIN: }
435435
; OPT: define ptr @fZ() local_unnamed_addr #0 {
436-
; OPT: ret ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 0, i64 1, i32 1)
436+
; OPT: ret ptr getelementptr inbounds (i8, ptr @ext, i64 12)
437437
; OPT: }
438438
; TO: define ptr @fZ() local_unnamed_addr #0 {
439-
; TO: ret ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 0, i64 1, i32 1)
439+
; TO: ret ptr getelementptr inbounds (i8, ptr @ext, i64 12)
440440
; TO: }
441441
; SCEV: Classifying expressions for: @fZ
442442
; SCEV: %t = bitcast ptr getelementptr inbounds (i32, ptr getelementptr inbounds ([3 x { i32, i32 }], ptr @ext, i64 0, i64 1, i32 0), i64 1) to ptr
@@ -464,7 +464,7 @@ define ptr @same_addrspace() nounwind noinline {
464464
; OPT: same_addrspace
465465
%p = getelementptr inbounds i8, ptr @p0, i32 2
466466
ret ptr %p
467-
; OPT: ret ptr getelementptr inbounds ([4 x i8], ptr @p0, i64 0, i64 2)
467+
; OPT: ret ptr getelementptr inbounds (i8, ptr @p0, i64 2)
468468
}
469469

470470
@gv1 = internal global i32 1

llvm/test/Other/optimize-inrange-gep.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ define void @foo(ptr %p) {
2020
;
2121
; CHECK-LABEL: define void @foo(
2222
; CHECK-SAME: ptr nocapture writeonly [[P:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
23-
; CHECK-NEXT: store ptr getelementptr inbounds inrange(-24, 0) ({ [3 x ptr] }, ptr @vtable, i64 1, i32 0, i64 0), ptr [[P]], align 8
23+
; CHECK-NEXT: store ptr getelementptr inbounds inrange(-24, 0) (i8, ptr @vtable, i64 24), ptr [[P]], align 8
2424
; CHECK-NEXT: ret void
2525
;
2626
store ptr getelementptr inrange(-24, 0) ({ [3 x ptr], [3 x ptr] }, ptr @vtable, i32 0, i32 0, i32 3), ptr %p

llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ define internal i64 @zoo(i1 %flag) {
3030
; CHECK-NEXT: entry:
3131
; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[PLUS:%.*]], label [[MINUS:%.*]]
3232
; CHECK: plus:
33-
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @func2.specialized.2(ptr getelementptr inbounds ([[STRUCT:%.*]], ptr @Global, i64 0, i32 3))
33+
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @func2.specialized.2(ptr getelementptr inbounds (i8, ptr @Global, i64 8))
3434
; CHECK-NEXT: br label [[MERGE:%.*]]
3535
; CHECK: minus:
36-
; CHECK-NEXT: [[TMP1:%.*]] = call i64 @func2.specialized.1(ptr getelementptr inbounds ([[STRUCT]], ptr @Global, i64 0, i32 4))
36+
; CHECK-NEXT: [[TMP1:%.*]] = call i64 @func2.specialized.1(ptr getelementptr inbounds (i8, ptr @Global, i64 16))
3737
; CHECK-NEXT: br label [[MERGE]]
3838
; CHECK: merge:
39-
; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ ptrtoint (ptr getelementptr inbounds ([[STRUCT]], ptr @Global, i64 0, i32 3) to i64), [[PLUS]] ], [ ptrtoint (ptr getelementptr inbounds ([[STRUCT]], ptr @Global, i64 0, i32 4) to i64), [[MINUS]] ]
39+
; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ ptrtoint (ptr getelementptr inbounds (i8, ptr @Global, i64 8) to i64), [[PLUS]] ], [ ptrtoint (ptr getelementptr inbounds (i8, ptr @Global, i64 16) to i64), [[MINUS]] ]
4040
; CHECK-NEXT: ret i64 [[TMP2]]
4141
;
4242
entry:

llvm/test/Transforms/GVN/PRE/load-pre-licm.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ target triple = "i386-apple-darwin11.0.0"
88
define void @Bubble() nounwind noinline {
99
; CHECK-LABEL: @Bubble(
1010
; CHECK-NEXT: entry:
11-
; CHECK-NEXT: [[TMP7_PRE:%.*]] = load i32, ptr getelementptr inbounds ([5001 x i32], ptr @sortlist, i32 0, i32 1), align 4
11+
; CHECK-NEXT: [[TMP7_PRE:%.*]] = load i32, ptr getelementptr inbounds (i8, ptr @sortlist, i32 4), align 4
1212
; CHECK-NEXT: br label [[WHILE_BODY5:%.*]]
1313
; CHECK: while.body5:
1414
; CHECK-NEXT: [[TMP7:%.*]] = phi i32 [ [[TMP7_PRE]], [[ENTRY:%.*]] ], [ [[TMP71:%.*]], [[IF_END:%.*]] ]

llvm/test/Transforms/GVN/PRE/phi-translate-2.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ define void @test2(i64 %i) {
6363
; CHECK: if.then:
6464
; CHECK-NEXT: [[CALL:%.*]] = tail call i64 (...) @goo()
6565
; CHECK-NEXT: store i64 [[CALL]], ptr @g2, align 8
66-
; CHECK-NEXT: [[T2_PRE:%.*]] = load i64, ptr getelementptr inbounds ([100 x i64], ptr @a, i64 0, i64 3), align 8
67-
; CHECK-NEXT: [[T3_PRE:%.*]] = load i64, ptr getelementptr inbounds ([100 x i64], ptr @b, i64 0, i64 3), align 8
66+
; CHECK-NEXT: [[T2_PRE:%.*]] = load i64, ptr getelementptr inbounds (i8, ptr @a, i64 24), align 8
67+
; CHECK-NEXT: [[T3_PRE:%.*]] = load i64, ptr getelementptr inbounds (i8, ptr @b, i64 24), align 8
6868
; CHECK-NEXT: [[DOTPRE:%.*]] = mul nsw i64 [[T3_PRE]], [[T2_PRE]]
6969
; CHECK-NEXT: br label [[IF_END]]
7070
; CHECK: if.end:

0 commit comments

Comments
 (0)