Skip to content

[ConstraintElim] Add support for decomposing gep nuw #118639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2024
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
16 changes: 10 additions & 6 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,9 @@ static Decomposition decomposeGEP(GEPOperator &GEP,
"unsigned predicates at the moment.");
const auto &[BasePtr, ConstantOffset, VariableOffsets, NW] =
collectOffsets(GEP, DL);
if (!BasePtr || !NW.hasNoUnsignedSignedWrap())
// We support either plain gep nuw, or gep nusw with non-negative offset,
// which implies gep nuw.
if (!BasePtr || NW == GEPNoWrapFlags::none())
return &GEP;

Decomposition Result(ConstantOffset.getSExtValue(), DecompEntry(1, BasePtr));
Expand All @@ -461,11 +463,13 @@ static Decomposition decomposeGEP(GEPOperator &GEP,
IdxResult.mul(Scale.getSExtValue());
Result.add(IdxResult);

// If Op0 is signed non-negative, the GEP is increasing monotonically and
// can be de-composed.
if (!isKnownNonNegative(Index, DL))
Preconditions.emplace_back(CmpInst::ICMP_SGE, Index,
ConstantInt::get(Index->getType(), 0));
if (!NW.hasNoUnsignedWrap()) {
// Try to prove nuw from nusw and nneg.
assert(NW.hasNoUnsignedSignedWrap() && "Must have nusw flag");
if (!isKnownNonNegative(Index, DL))
Preconditions.emplace_back(CmpInst::ICMP_SGE, Index,
ConstantInt::get(Index->getType(), 0));
}
}
return Result;
}
Expand Down
6 changes: 2 additions & 4 deletions llvm/test/Transforms/ConstraintElimination/gep-arithmetic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,7 @@ define i1 @test_nuw(ptr %p, i64 %x, i64 %y) {
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP1]])
; CHECK-NEXT: [[GEP_X:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[X]]
; CHECK-NEXT: [[GEP_Y:%.*]] = getelementptr nuw i8, ptr [[P]], i64 [[Y]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt ptr [[GEP_X]], [[GEP_Y]]
; CHECK-NEXT: ret i1 [[CMP2]]
; CHECK-NEXT: ret i1 true
;
%cmp1 = icmp ugt i64 %x, %y
call void @llvm.assume(i1 %cmp1)
Expand All @@ -720,8 +719,7 @@ define i1 @test_nuw_nested(ptr %p, i64 %x, i64 %y) {
; CHECK-NEXT: [[GEP_X:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[X]]
; CHECK-NEXT: [[GEP_X1:%.*]] = getelementptr nuw i8, ptr [[GEP_X]], i64 1
; CHECK-NEXT: [[GEP_Y:%.*]] = getelementptr nuw i8, ptr [[P]], i64 [[Y]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt ptr [[GEP_X1]], [[GEP_Y]]
; CHECK-NEXT: ret i1 [[CMP2]]
; CHECK-NEXT: ret i1 true
;
%cmp1 = icmp ugt i64 %x, %y
call void @llvm.assume(i1 %cmp1)
Expand Down
Loading