Skip to content

Commit d91a5c3

Browse files
authored
[ConstraintElim] Bail out on non-canonical GEPs (llvm#156688)
In most cases, GEPs should be canonicalized by InstCombine. Bail out on non-canonical forms for simplicity. Fixes llvm#155253 (comment).
1 parent 7d6e72f commit d91a5c3

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,12 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
11251125
if (Offset.VariableOffsets.size() != 1)
11261126
return false;
11271127

1128+
uint64_t BitWidth = Offset.ConstantOffset.getBitWidth();
1129+
auto &[Index, Scale] = Offset.VariableOffsets.front();
1130+
// Bail out on non-canonical GEPs.
1131+
if (Index->getType()->getScalarSizeInBits() != BitWidth)
1132+
return false;
1133+
11281134
ObjectSizeOpts Opts;
11291135
// Workaround for gep inbounds, ptr null, idx.
11301136
Opts.NullIsUnknownSize = true;
@@ -1140,8 +1146,6 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
11401146
// With nuw flag, we know that the index addition doesn't have unsigned wrap.
11411147
// If (AllocSize - (ConstOffset + AccessSize)) wraps around, there is no valid
11421148
// value for Index.
1143-
uint64_t BitWidth = Offset.ConstantOffset.getBitWidth();
1144-
auto &[Index, Scale] = Offset.VariableOffsets.front();
11451149
APInt MaxIndex = (APInt(BitWidth, Size->getFixedValue() - AccessSize,
11461150
/*isSigned=*/false, /*implicitTrunc=*/true) -
11471151
Offset.ConstantOffset)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,21 @@ define i8 @load_from_null(i64 %idx) {
371371
%add = add i8 %load, %zext
372372
ret i8 %add
373373
}
374+
375+
define i8 @load_global_non_canonical_gep(i32 %idx) {
376+
; CHECK-LABEL: define i8 @load_global_non_canonical_gep(
377+
; CHECK-SAME: i32 [[IDX:%.*]]) {
378+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr @g, i32 [[IDX]]
379+
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
380+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[IDX]], 5
381+
; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[CMP]] to i8
382+
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD]], [[ZEXT]]
383+
; CHECK-NEXT: ret i8 [[ADD]]
384+
;
385+
%gep = getelementptr nuw i8, ptr @g, i32 %idx
386+
%load = load i8, ptr %gep
387+
%cmp = icmp ult i32 %idx, 5
388+
%zext = zext i1 %cmp to i8
389+
%add = add i8 %load, %zext
390+
ret i8 %add
391+
}

0 commit comments

Comments
 (0)