Skip to content

[ConstraintElim] Pick NUWSub fix. #7925

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 3 commits into from
Jan 3, 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
17 changes: 13 additions & 4 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ struct Decomposition {
append_range(Vars, Other.Vars);
}

void sub(const Decomposition &Other) {
Decomposition Tmp = Other;
Tmp.mul(-1);
add(Tmp.Offset);
append_range(Vars, Tmp.Vars);
}

void mul(int64_t Factor) {
Offset = multiplyWithOverflow(Offset, Factor);
for (auto &Var : Vars)
Expand Down Expand Up @@ -542,10 +549,12 @@ static Decomposition decompose(Value *V,
return Result;
}

if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI))) && canUseSExt(CI))
return {-1 * CI->getSExtValue(), {{1, Op0}}};
if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1))))
return {0, {{1, Op0}, {-1, Op1}}};
if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1)))) {
auto ResA = decompose(Op0, Preconditions, IsSigned, DL);
auto ResB = decompose(Op1, Preconditions, IsSigned, DL);
ResA.sub(ResB);
return ResA;
}

return {V, IsKnownNonNegative};
}
Expand Down
120 changes: 117 additions & 3 deletions llvm/test/Transforms/ConstraintElimination/sub-nuw.ll
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ define i1 @sub_nuw_neg_i16(i16 %a) {
; CHECK-NEXT: [[C_1:%.*]] = icmp ugt i16 0, [[NEG2]]
; CHECK-NEXT: br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
; CHECK: exit.1:
; CHECK-NEXT: ret i1 false
; CHECK-NEXT: [[C_2:%.*]] = icmp ugt i16 [[A]], 0
; CHECK-NEXT: ret i1 [[C_2]]
; CHECK: exit.2:
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i16 [[A]], 0
; CHECK-NEXT: ret i1 [[C_3]]
; CHECK-NEXT: ret i1 true
;
entry:
%neg2 = sub nuw i16 %a, -305
Expand Down Expand Up @@ -380,3 +380,117 @@ entry:
%c = icmp ugt i64 %neg2, 0
ret i1 %c
}

define i1 @pr76713(i16 %i1, i16 %i3) {
; CHECK-LABEL: @pr76713(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[C1:%.*]] = icmp ult i16 [[I1:%.*]], -1
; CHECK-NEXT: [[C2:%.*]] = icmp uge i16 [[I1]], -3
; CHECK-NEXT: [[C3:%.*]] = icmp ult i16 [[I3:%.*]], 2
; CHECK-NEXT: [[AND:%.*]] = and i1 [[C1]], [[C2]]
; CHECK-NEXT: [[AND_2:%.*]] = and i1 [[AND]], [[C3]]
; CHECK-NEXT: br i1 [[AND]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[SUB:%.*]] = sub nuw nsw i16 [[I1]], -3
; CHECK-NEXT: [[ARRAYIDX_IDX:%.*]] = mul nuw nsw i16 [[I3]], 4
; CHECK-NEXT: [[I6:%.*]] = add nuw nsw i16 [[ARRAYIDX_IDX]], [[SUB]]
; CHECK-NEXT: [[C4:%.*]] = icmp ult i16 12, [[I6]]
; CHECK-NEXT: ret i1 [[C4]]
; CHECK: else:
; CHECK-NEXT: ret i1 false
;
entry:
%c1 = icmp ult i16 %i1, -1
%c2 = icmp uge i16 %i1, -3
%c3 = icmp ult i16 %i3, 2
%and = and i1 %c1, %c2
%and.2 = and i1 %and, %c3
br i1 %and, label %then, label %else

then:
%sub = sub nuw nsw i16 %i1, -3
%arrayidx.idx = mul nuw nsw i16 %i3, 4
%i6 = add nuw nsw i16 %arrayidx.idx, %sub
%c4 = icmp ult i16 12, %i6
ret i1 %c4

else:
ret i1 0
}

define void @sub_nuw_chained_positive_constants(i16 %a) {
; CHECK-LABEL: @sub_nuw_chained_positive_constants(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[SUB1:%.*]] = sub nuw i16 [[A:%.*]], 10
; CHECK-NEXT: [[SUB2:%.*]] = sub nuw i16 [[SUB1]], 20
; CHECK-NEXT: [[C_1:%.*]] = icmp ugt i16 [[SUB2]], 90
; CHECK-NEXT: br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
; CHECK: exit.1:
; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i16 [[A]], 121
; CHECK-NEXT: call void @use(i1 [[C_3]])
; CHECK-NEXT: ret void
; CHECK: exit.2:
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: ret void
;
entry:
%sub1 = sub nuw i16 %a, 10
%sub2 = sub nuw i16 %sub1, 20
%c.1 = icmp ugt i16 %sub2, 90
br i1 %c.1, label %exit.1, label %exit.2

exit.1:
%c.2 = icmp ugt i16 %a, 120
call void @use(i1 %c.2)
%c.3 = icmp ugt i16 %a, 121
call void @use(i1 %c.3)
ret void

exit.2:
%c.4 = icmp ugt i16 %a, 120
call void @use(i1 %c.4)
%c.5 = icmp ugt i16 %a, 121
call void @use(i1 %c.5)
ret void
}

define void @sub_nuw_chained_negative_constants(i8 %a) {
; CHECK-LABEL: @sub_nuw_chained_negative_constants(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[SUB1:%.*]] = sub nuw i8 [[A:%.*]], 10
; CHECK-NEXT: [[SUB2:%.*]] = sub nuw i8 [[SUB1]], -126
; CHECK-NEXT: [[C_1:%.*]] = icmp ugt i8 [[SUB2]], 20
; CHECK-NEXT: br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
; CHECK: exit.1:
; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i8 [[A]], -95
; CHECK-NEXT: call void @use(i1 [[C_3]])
; CHECK-NEXT: ret void
; CHECK: exit.2:
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: ret void
;
entry:
%sub1 = sub nuw i8 %a, 10
%sub2 = sub nuw i8 %sub1, 130
%c.1 = icmp ugt i8 %sub2, 20
br i1 %c.1, label %exit.1, label %exit.2

exit.1:
%c.2 = icmp ugt i8 %a, 160
call void @use(i1 %c.2)
%c.3 = icmp ugt i8 %a, 161
call void @use(i1 %c.3)
ret void


exit.2:
%c.4 = icmp ugt i8 %a, 160
call void @use(i1 %c.4)
%c.5 = icmp ugt i8 %a, 161
call void @use(i1 %c.5)
ret void
}