Skip to content

Commit 35e2a2f

Browse files
committed
[SCCP] Add nneg flag to uitofp if its operand is non-negative
Similiar to the `InstCombine` changes, just furthering the support of the `uitofp nneg` support.
1 parent 5fb1ec2 commit 35e2a2f

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static bool refineInstruction(SCCPSolver &Solver,
140140
Changed = true;
141141
}
142142
}
143-
} else if (isa<ZExtInst>(Inst) && !Inst.hasNonNeg()) {
143+
} else if (isa<PossiblyNonNegInst>(Inst) && !Inst.hasNonNeg()) {
144144
auto Range = GetRange(Inst.getOperand(0));
145145
if (Range.isAllNonNegative()) {
146146
Inst.setNonNeg();
@@ -170,14 +170,16 @@ static bool replaceSignedInst(SCCPSolver &Solver,
170170

171171
Instruction *NewInst = nullptr;
172172
switch (Inst.getOpcode()) {
173-
// Note: We do not fold sitofp -> uitofp here because that could be more
174-
// expensive in codegen and may not be reversible in the backend.
173+
case Instruction::SIToFP:
175174
case Instruction::SExt: {
176-
// If the source value is not negative, this is a zext.
175+
// If the source value is not negative, this is a zext/uitofp.
177176
Value *Op0 = Inst.getOperand(0);
178177
if (InsertedValues.count(Op0) || !isNonNegative(Op0))
179178
return false;
180-
NewInst = new ZExtInst(Op0, Inst.getType(), "", Inst.getIterator());
179+
NewInst = CastInst::Create(Inst.getOpcode() == Instruction::SExt
180+
? Instruction::ZExt
181+
: Instruction::UIToFP,
182+
Op0, Inst.getType(), "", Inst.getIterator());
181183
NewInst->setNonNeg();
182184
break;
183185
}

llvm/test/Transforms/SCCP/ip-ranges-casts.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ define i1 @caller.sext() {
163163
; There's nothing we can do besides going to the full range or overdefined.
164164
define internal i1 @f.fptosi(i32 %x) {
165165
; CHECK-LABEL: @f.fptosi(
166-
; CHECK-NEXT: [[TO_DOUBLE:%.*]] = sitofp i32 [[X:%.*]] to double
166+
; CHECK-NEXT: [[TO_DOUBLE:%.*]] = uitofp nneg i32 [[X:%.*]] to double
167167
; CHECK-NEXT: [[ADD:%.*]] = fadd double 0.000000e+00, [[TO_DOUBLE]]
168168
; CHECK-NEXT: [[TO_I32:%.*]] = fptosi double [[ADD]] to i32
169169
; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i32 [[TO_I32]], 300
@@ -204,7 +204,7 @@ define i1 @caller.fptosi() {
204204
; There's nothing we can do besides going to the full range or overdefined.
205205
define internal i1 @f.fpext(i16 %x) {
206206
; CHECK-LABEL: @f.fpext(
207-
; CHECK-NEXT: [[TO_FLOAT:%.*]] = sitofp i16 [[X:%.*]] to float
207+
; CHECK-NEXT: [[TO_FLOAT:%.*]] = uitofp nneg i16 [[X:%.*]] to float
208208
; CHECK-NEXT: [[TO_DOUBLE:%.*]] = fpext float [[TO_FLOAT]] to double
209209
; CHECK-NEXT: [[TO_I64:%.*]] = fptoui float [[TO_FLOAT]] to i64
210210
; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i64 [[TO_I64]], 300
@@ -286,7 +286,7 @@ define i1 @caller.inttoptr.ptrtoint() {
286286
define i1 @int_range_to_double_cast(i32 %a) {
287287
; CHECK-LABEL: @int_range_to_double_cast(
288288
; CHECK-NEXT: [[R:%.*]] = and i32 [[A:%.*]], 255
289-
; CHECK-NEXT: [[T4:%.*]] = sitofp i32 [[R]] to double
289+
; CHECK-NEXT: [[T4:%.*]] = uitofp nneg i32 [[R]] to double
290290
; CHECK-NEXT: [[T10:%.*]] = fadd double 0.000000e+00, [[T4]]
291291
; CHECK-NEXT: [[T11:%.*]] = fcmp olt double [[T4]], [[T10]]
292292
; CHECK-NEXT: ret i1 [[T11]]
@@ -329,7 +329,7 @@ define i64 @caller.sext_to_zext(i32 %i) {
329329
; CHECK-LABEL: @caller.sext_to_zext(
330330
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[I:%.*]], 9
331331
; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32
332-
; CHECK-NEXT: [[T:%.*]] = call i64 @f.sext_to_zext(i32 [[CONV]])
332+
; CHECK-NEXT: [[T:%.*]] = call i64 @f.sext_to_zext(i32 [[CONV]]), !range [[RNG0:![0-9]+]]
333333
; CHECK-NEXT: ret i64 [[T]]
334334
;
335335
%cmp = icmp sle i32 %i, 9

llvm/test/Transforms/SCCP/sitofp.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
define float @sitofp_and(i8 %x) {
55
; CHECK-LABEL: @sitofp_and(
66
; CHECK-NEXT: [[PX:%.*]] = and i8 [[X:%.*]], 127
7-
; CHECK-NEXT: [[R:%.*]] = sitofp i8 [[PX]] to float
7+
; CHECK-NEXT: [[R:%.*]] = uitofp nneg i8 [[PX]] to float
88
; CHECK-NEXT: ret float [[R]]
99
;
1010
%px = and i8 %x, 127
@@ -23,7 +23,7 @@ define half @sitofp_const(i8 %x) {
2323
define double @sitofp_zext(i7 %x) {
2424
; CHECK-LABEL: @sitofp_zext(
2525
; CHECK-NEXT: [[PX:%.*]] = zext i7 [[X:%.*]] to i8
26-
; CHECK-NEXT: [[R:%.*]] = sitofp i8 [[PX]] to double
26+
; CHECK-NEXT: [[R:%.*]] = uitofp nneg i8 [[PX]] to double
2727
; CHECK-NEXT: ret double [[R]]
2828
;
2929
%px = zext i7 %x to i8
@@ -52,7 +52,7 @@ define float @dominating_condition(i32 %x) {
5252
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[X:%.*]], 0
5353
; CHECK-NEXT: br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
5454
; CHECK: t:
55-
; CHECK-NEXT: [[A:%.*]] = sitofp i32 [[X]] to float
55+
; CHECK-NEXT: [[A:%.*]] = uitofp nneg i32 [[X]] to float
5656
; CHECK-NEXT: br label [[EXIT:%.*]]
5757
; CHECK: f:
5858
; CHECK-NEXT: br label [[EXIT]]
@@ -86,7 +86,7 @@ define float @dominating_condition_alt(i32 %x) {
8686
; CHECK: t:
8787
; CHECK-NEXT: br label [[EXIT:%.*]]
8888
; CHECK: f:
89-
; CHECK-NEXT: [[A:%.*]] = sitofp i32 [[X]] to float
89+
; CHECK-NEXT: [[A:%.*]] = uitofp nneg i32 [[X]] to float
9090
; CHECK-NEXT: br label [[EXIT]]
9191
; CHECK: exit:
9292
; CHECK-NEXT: [[COND:%.*]] = phi float [ -4.200000e+01, [[T]] ], [ [[A]], [[F]] ]

0 commit comments

Comments
 (0)