Skip to content

Commit 5227b08

Browse files
committed
[ConstraintElim] Switch to getBaseObjectSize
1 parent 2c90fb4 commit 5227b08

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,9 +1131,9 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
11311131
// Be conservative since we are not clear on whether an out of bounds access
11321132
// to the padding is UB or not.
11331133
Opts.RoundToAlign = true;
1134-
ObjectSizeOffsetVisitor Visitor(DL, &TLI, GEP.getContext(), Opts);
1135-
SizeOffsetAPInt Data = Visitor.compute(Offset.BasePtr);
1136-
if (!Data.bothKnown() || !Data.Offset.isZero())
1134+
std::optional<TypeSize> Size =
1135+
getBaseObjectSize(Offset.BasePtr, DL, &TLI, Opts);
1136+
if (!Size || Size->isScalable())
11371137
return false;
11381138

11391139
// Index * Scale + ConstOffset + AccessSize <= AllocSize
@@ -1142,9 +1142,10 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
11421142
// value for Index.
11431143
uint64_t BitWidth = Offset.ConstantOffset.getBitWidth();
11441144
auto &[Index, Scale] = Offset.VariableOffsets.front();
1145-
APInt MaxIndex =
1146-
(Data.Size - APInt(BitWidth, AccessSize) - Offset.ConstantOffset)
1147-
.udiv(Scale);
1145+
APInt MaxIndex = (APInt(BitWidth, Size->getFixedValue() - AccessSize,
1146+
/*isSigned=*/false, /*implicitTrunc=*/true) -
1147+
Offset.ConstantOffset)
1148+
.udiv(Scale);
11481149
Pred = ICmpInst::ICMP_ULE;
11491150
A = Index;
11501151
B = ConstantInt::get(Index->getType(), MaxIndex);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ define i8 @load_global(i64 %idx) {
2626
ret i8 %add
2727
}
2828

29+
define i8 @load_global_const_offset(i64 %idx) {
30+
; CHECK-LABEL: define i8 @load_global_const_offset(
31+
; CHECK-SAME: i64 [[IDX:%.*]]) {
32+
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr nuw i8, ptr @g, i64 1
33+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr [[GEP1]], i64 [[IDX]]
34+
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
35+
; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 true to i8
36+
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD]], [[ZEXT]]
37+
; CHECK-NEXT: ret i8 [[ADD]]
38+
;
39+
%gep1 = getelementptr nuw i8, ptr @g, i64 1
40+
%gep = getelementptr nuw i8, ptr %gep1, i64 %idx
41+
%load = load i8, ptr %gep
42+
%cmp = icmp ult i64 %idx, 4
43+
%zext = zext i1 %cmp to i8
44+
%add = add i8 %load, %zext
45+
ret i8 %add
46+
}
47+
2948
define i8 @load_global_atomic(i64 %idx) {
3049
; CHECK-LABEL: define i8 @load_global_atomic(
3150
; CHECK-SAME: i64 [[IDX:%.*]]) {

0 commit comments

Comments
 (0)