Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,12 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
if (Offset.VariableOffsets.size() != 1)
return false;

uint64_t BitWidth = Offset.ConstantOffset.getBitWidth();
auto &[Index, Scale] = Offset.VariableOffsets.front();
// Bail out on non-canonical GEPs.
if (Index->getType()->getScalarSizeInBits() != BitWidth)
return false;

ObjectSizeOpts Opts;
// Workaround for gep inbounds, ptr null, idx.
Opts.NullIsUnknownSize = true;
Expand All @@ -1140,8 +1146,6 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
// With nuw flag, we know that the index addition doesn't have unsigned wrap.
// If (AllocSize - (ConstOffset + AccessSize)) wraps around, there is no valid
// value for Index.
uint64_t BitWidth = Offset.ConstantOffset.getBitWidth();
auto &[Index, Scale] = Offset.VariableOffsets.front();
APInt MaxIndex = (APInt(BitWidth, Size->getFixedValue() - AccessSize,
/*isSigned=*/false, /*implicitTrunc=*/true) -
Offset.ConstantOffset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,21 @@ define i8 @load_from_null(i64 %idx) {
%add = add i8 %load, %zext
ret i8 %add
}

define i8 @load_global_non_canonical_gep(i32 %idx) {
; CHECK-LABEL: define i8 @load_global_non_canonical_gep(
; CHECK-SAME: i32 [[IDX:%.*]]) {
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr @g, i32 [[IDX]]
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[IDX]], 5
; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[CMP]] to i8
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD]], [[ZEXT]]
; CHECK-NEXT: ret i8 [[ADD]]
;
%gep = getelementptr nuw i8, ptr @g, i32 %idx
%load = load i8, ptr %gep
%cmp = icmp ult i32 %idx, 5
%zext = zext i1 %cmp to i8
%add = add i8 %load, %zext
ret i8 %add
}