Skip to content

Commit 2c90fb4

Browse files
committed
[ConstraintElim] Address review comments.
1 parent e1749b7 commit 2c90fb4

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
11281128
ObjectSizeOpts Opts;
11291129
// Workaround for gep inbounds, ptr null, idx.
11301130
Opts.NullIsUnknownSize = true;
1131+
// Be conservative since we are not clear on whether an out of bounds access
1132+
// to the padding is UB or not.
1133+
Opts.RoundToAlign = true;
11311134
ObjectSizeOffsetVisitor Visitor(DL, &TLI, GEP.getContext(), Opts);
11321135
SizeOffsetAPInt Data = Visitor.compute(Offset.BasePtr);
11331136
if (!Data.bothKnown() || !Data.Offset.isZero())
@@ -1191,11 +1194,11 @@ void State::addInfoFor(BasicBlock &BB) {
11911194
};
11921195

11931196
if (auto *LI = dyn_cast<LoadInst>(&I)) {
1194-
if (LI->isSimple())
1197+
if (!LI->isVolatile())
11951198
AddFactFromMemoryAccess(LI->getPointerOperand(), LI->getAccessType());
11961199
}
11971200
if (auto *SI = dyn_cast<StoreInst>(&I)) {
1198-
if (SI->isSimple())
1201+
if (!SI->isVolatile())
11991202
AddFactFromMemoryAccess(SI->getPointerOperand(), SI->getAccessType());
12001203
}
12011204

llvm/test/Transforms/ConstraintElimination/implied-by-bounded-memory-access.ll

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
33

44
@g = private unnamed_addr constant [5 x i8] c"test\00"
5+
@g_overaligned = private unnamed_addr constant [5 x i8] c"test\00", align 8
6+
@g_external = external global [5 x i8]
57

68
declare void @free(ptr allocptr noundef captures(none)) mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc"
79
declare ptr @malloc(i64) mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc"
@@ -24,6 +26,23 @@ define i8 @load_global(i64 %idx) {
2426
ret i8 %add
2527
}
2628

29+
define i8 @load_global_atomic(i64 %idx) {
30+
; CHECK-LABEL: define i8 @load_global_atomic(
31+
; CHECK-SAME: i64 [[IDX:%.*]]) {
32+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr @g, i64 [[IDX]]
33+
; CHECK-NEXT: [[LOAD:%.*]] = load atomic i8, ptr [[GEP]] unordered, align 1
34+
; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 true to i8
35+
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD]], [[ZEXT]]
36+
; CHECK-NEXT: ret i8 [[ADD]]
37+
;
38+
%gep = getelementptr nuw i8, ptr @g, i64 %idx
39+
%load = load atomic i8, ptr %gep unordered, align 1
40+
%cmp = icmp ult i64 %idx, 5
41+
%zext = zext i1 %cmp to i8
42+
%add = add i8 %load, %zext
43+
ret i8 %add
44+
}
45+
2746
define i1 @store_global(i64 %idx) {
2847
; CHECK-LABEL: define i1 @store_global(
2948
; CHECK-SAME: i64 [[IDX:%.*]]) {
@@ -37,6 +56,19 @@ define i1 @store_global(i64 %idx) {
3756
ret i1 %cmp
3857
}
3958

59+
define i1 @store_global_atomic(i64 %idx) {
60+
; CHECK-LABEL: define i1 @store_global_atomic(
61+
; CHECK-SAME: i64 [[IDX:%.*]]) {
62+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr @g, i64 [[IDX]]
63+
; CHECK-NEXT: store atomic i8 0, ptr [[GEP]] release, align 1
64+
; CHECK-NEXT: ret i1 true
65+
;
66+
%gep = getelementptr nuw i8, ptr @g, i64 %idx
67+
store atomic i8 0, ptr %gep release, align 1
68+
%cmp = icmp ult i64 %idx, 5
69+
ret i1 %cmp
70+
}
71+
4072
define i8 @load_byval(ptr byval([5 x i8]) %p, i64 %idx) {
4173
; CHECK-LABEL: define i8 @load_byval(
4274
; CHECK-SAME: ptr byval([5 x i8]) [[P:%.*]], i64 [[IDX:%.*]]) {
@@ -143,6 +175,42 @@ next:
143175

144176
; Negative tests.
145177

178+
define i8 @load_global_overaligned(i64 %idx) {
179+
; CHECK-LABEL: define i8 @load_global_overaligned(
180+
; CHECK-SAME: i64 [[IDX:%.*]]) {
181+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr @g_overaligned, i64 [[IDX]]
182+
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
183+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[IDX]], 5
184+
; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[CMP]] to i8
185+
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD]], [[ZEXT]]
186+
; CHECK-NEXT: ret i8 [[ADD]]
187+
;
188+
%gep = getelementptr nuw i8, ptr @g_overaligned, i64 %idx
189+
%load = load i8, ptr %gep
190+
%cmp = icmp ult i64 %idx, 5
191+
%zext = zext i1 %cmp to i8
192+
%add = add i8 %load, %zext
193+
ret i8 %add
194+
}
195+
196+
define i8 @load_global_external(i64 %idx) {
197+
; CHECK-LABEL: define i8 @load_global_external(
198+
; CHECK-SAME: i64 [[IDX:%.*]]) {
199+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr @g_external, i64 [[IDX]]
200+
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
201+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[IDX]], 5
202+
; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[CMP]] to i8
203+
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD]], [[ZEXT]]
204+
; CHECK-NEXT: ret i8 [[ADD]]
205+
;
206+
%gep = getelementptr nuw i8, ptr @g_external, i64 %idx
207+
%load = load i8, ptr %gep
208+
%cmp = icmp ult i64 %idx, 5
209+
%zext = zext i1 %cmp to i8
210+
%add = add i8 %load, %zext
211+
ret i8 %add
212+
}
213+
146214
define i8 @load_from_non_gep(ptr %p, i64 %idx) {
147215
; CHECK-LABEL: define i8 @load_from_non_gep(
148216
; CHECK-SAME: ptr [[P:%.*]], i64 [[IDX:%.*]]) {
@@ -233,6 +301,20 @@ define i8 @load_global_volatile(i64 %idx) {
233301
ret i8 %add
234302
}
235303

304+
define i1 @store_global_volatile(i64 %idx) {
305+
; CHECK-LABEL: define i1 @store_global_volatile(
306+
; CHECK-SAME: i64 [[IDX:%.*]]) {
307+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr @g, i64 [[IDX]]
308+
; CHECK-NEXT: store volatile i8 0, ptr [[GEP]], align 1
309+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[IDX]], 5
310+
; CHECK-NEXT: ret i1 [[CMP]]
311+
;
312+
%gep = getelementptr nuw i8, ptr @g, i64 %idx
313+
store volatile i8 0, ptr %gep
314+
%cmp = icmp ult i64 %idx, 5
315+
ret i1 %cmp
316+
}
317+
236318
define i8 @load_global_vscale(i64 %idx) {
237319
; CHECK-LABEL: define i8 @load_global_vscale(
238320
; CHECK-SAME: i64 [[IDX:%.*]]) {

0 commit comments

Comments
 (0)