Skip to content

Commit a028107

Browse files
committed
inf
1 parent f6ccb18 commit a028107

File tree

2 files changed

+97
-83
lines changed

2 files changed

+97
-83
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 77 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8181,79 +8181,93 @@ static Instruction *foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI,
81818181
static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,
81828182
InstCombinerImpl &CI) {
81838183
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
8184-
const CmpInst::Predicate Pred = I.getPredicate();
81858184
Type *OpType = LHS->getType();
8185+
const CmpInst::Predicate Pred = I.getPredicate();
81868186

8187-
// fcmp ole floor(x), x => fcmp ord x, 0
8188-
// fcmp ogt floor(x), x => false
8189-
if (match(LHS, m_Intrinsic<Intrinsic::floor>(m_Specific(RHS)))) {
8190-
if (Pred == FCmpInst::FCMP_OLE ||
8191-
Pred == FCmpInst::FCMP_ULE &&
8192-
isKnownNeverNaN(LHS, 0,
8193-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8194-
return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType),
8195-
"", &I);
8196-
}
8197-
if (Pred == FCmpInst::FCMP_OGT ||
8198-
Pred == FCmpInst::FCMP_UGT &&
8199-
isKnownNeverNaN(LHS, 0,
8200-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8201-
return CI.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8202-
}
8203-
}
8187+
bool floor_x = match(LHS, m_Intrinsic<Intrinsic::floor>(m_Specific(RHS)));
8188+
bool x_floor = match(RHS, m_Intrinsic<Intrinsic::floor>(m_Specific(LHS)));
8189+
bool ceil_x = match(LHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(RHS)));
8190+
bool x_ceil = match(RHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(LHS)));
82048191

8205-
// fcmp oge x, floor(x) => fcmp ord x, 0
8206-
// fcmp olt x, floor(x) => false
8207-
if (match(RHS, m_Intrinsic<Intrinsic::floor>(m_Specific(LHS)))) {
8208-
if (Pred == FCmpInst::FCMP_OGE ||
8209-
Pred == FCmpInst::FCMP_UGE &&
8210-
isKnownNeverNaN(RHS, 0,
8211-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8192+
switch (Pred) {
8193+
case FCmpInst::FCMP_OLE:
8194+
// fcmp ole floor(x), x => fcmp ord x, 0
8195+
// fcmp ole x, ceil(x) => fcmp ord x, 0
8196+
if (floor_x)
82128197
return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType),
82138198
"", &I);
8214-
}
8215-
if (Pred == FCmpInst::FCMP_OLT ||
8216-
Pred == FCmpInst::FCMP_ULT &&
8217-
isKnownNeverNaN(RHS, 0,
8218-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8219-
return CI.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8220-
}
8221-
}
8222-
8223-
// fcmp oge ceil(x), x => fcmp ord x, 0
8224-
// fcmp olt ceil(x), x => false
8225-
if (match(LHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(RHS)))) {
8226-
if (Pred == FCmpInst::FCMP_OGE ||
8227-
Pred == FCmpInst::FCMP_UGE &&
8228-
isKnownNeverNaN(LHS, 0,
8229-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8230-
return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType),
8199+
if (x_ceil)
8200+
return new FCmpInst(FCmpInst::FCMP_ORD, LHS, ConstantFP::getZero(OpType),
82318201
"", &I);
8232-
}
8233-
if (Pred == FCmpInst::FCMP_OLT ||
8234-
Pred == FCmpInst::FCMP_ULT &&
8235-
isKnownNeverNaN(LHS, 0,
8236-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8202+
break;
8203+
case FCmpInst::FCMP_OGT:
8204+
// fcmp ogt floor(x), x => false
8205+
// fcmp ogt x, ceil(x) => false
8206+
if (floor_x || x_ceil)
82378207
return CI.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8238-
}
8239-
}
8240-
8241-
// fcmp ole x, ceil(x) => fcmp ord x, 0
8242-
// fcmp ogt x, ceil(x) => false
8243-
if (match(RHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(LHS)))) {
8244-
if (Pred == FCmpInst::FCMP_OLE ||
8245-
Pred == FCmpInst::FCMP_ULE &&
8246-
isKnownNeverNaN(RHS, 0,
8247-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8208+
break;
8209+
case FCmpInst::FCMP_OGE:
8210+
// fcmp oge x, floor(x) => fcmp ord x, 0
8211+
// fcmp oge ceil(x), x => fcmp ord x, 0
8212+
if (x_floor)
8213+
return new FCmpInst(FCmpInst::FCMP_ORD, LHS, ConstantFP::getZero(OpType),
8214+
"", &I);
8215+
if (ceil_x)
82488216
return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType),
82498217
"", &I);
8250-
}
8251-
if (Pred == FCmpInst::FCMP_OGT ||
8252-
Pred == FCmpInst::FCMP_UGT &&
8253-
isKnownNeverNaN(RHS, 0,
8254-
CI.getSimplifyQuery().getWithInstruction(&I))) {
8218+
break;
8219+
case FCmpInst::FCMP_OLT:
8220+
// fcmp olt x, floor(x) => false
8221+
// fcmp olt ceil(x), x => false
8222+
if (x_floor || ceil_x)
82558223
return CI.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8256-
}
8224+
break;
8225+
case FCmpInst::FCMP_ULE:
8226+
// fcmp ule floor(x), x => fcmp ule -inf, x
8227+
// fcmp ule x, ceil(x) => fcmp ule x, inf
8228+
if (floor_x)
8229+
return new FCmpInst(FCmpInst::FCMP_ULE,
8230+
ConstantFP::getInfinity(RHS->getType(), true), RHS,
8231+
"", &I);
8232+
if (x_ceil)
8233+
return new FCmpInst(FCmpInst::FCMP_ULE, LHS,
8234+
ConstantFP::getInfinity(LHS->getType(), false), "",
8235+
&I);
8236+
case FCmpInst::FCMP_UGT:
8237+
// fcmp ugt floor(x), x => fcmp ugt -inf, x
8238+
// fcmp ugt x, ceil(x) => fcmp ugt x, inf
8239+
if (floor_x)
8240+
return new FCmpInst(FCmpInst::FCMP_UGT,
8241+
ConstantFP::getInfinity(RHS->getType(), true), RHS,
8242+
"", &I);
8243+
if (x_ceil)
8244+
return new FCmpInst(FCmpInst::FCMP_UGT, LHS,
8245+
ConstantFP::getInfinity(LHS->getType(), false), "",
8246+
&I);
8247+
case FCmpInst::FCMP_UGE:
8248+
// fcmp uge x, floor(x) => fcmp uge x, -inf
8249+
// fcmp uge ceil(x), x => fcmp uge inf, x
8250+
if (x_floor)
8251+
return new FCmpInst(FCmpInst::FCMP_UGE, LHS,
8252+
ConstantFP::getInfinity(LHS->getType(), true), "",
8253+
&I);
8254+
if (ceil_x)
8255+
return new FCmpInst(FCmpInst::FCMP_UGE,
8256+
ConstantFP::getInfinity(RHS->getType(), false), RHS,
8257+
"", &I);
8258+
case FCmpInst::FCMP_ULT:
8259+
// fcmp ult x, floor(x) => fcmp ult x, -inf
8260+
// fcmp ult ceil(x), x => fcmp ult inf, x
8261+
if (x_floor)
8262+
return new FCmpInst(FCmpInst::FCMP_ULT, LHS,
8263+
ConstantFP::getInfinity(LHS->getType(), true), "",
8264+
&I);
8265+
if (ceil_x)
8266+
return new FCmpInst(FCmpInst::FCMP_ULT,
8267+
ConstantFP::getInfinity(RHS->getType(), false), RHS,
8268+
"", &I);
8269+
default:
8270+
break;
82578271
}
82588272

82598273
return nullptr;

llvm/test/Transforms/InstCombine/fp-floor-ceil.ll

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ define i1 @floor_x_ole(float %x) {
1313

1414
define i1 @floor_x_ule(float %x) {
1515
; CHECK-LABEL: @floor_x_ule(
16-
; CHECK-NEXT: [[RET:%.*]] = fcmp ninf ord float [[X:%.*]], 0.000000e+00
17-
; CHECK-NEXT: ret i1 [[RET]]
16+
; CHECK-NEXT: ret i1 true
1817
;
19-
%floor = call nnan float @llvm.floor.f32(float %x)
18+
%floor = call float @llvm.floor.f32(float %x)
2019
%ret = fcmp ninf ule float %floor, %x
2120
ret i1 %ret
2221
}
@@ -32,17 +31,17 @@ define i1 @floor_x_ogt(float %x) {
3231

3332
define i1 @floor_x_ugt(float %x) {
3433
; CHECK-LABEL: @floor_x_ugt(
35-
; CHECK-NEXT: ret i1 false
34+
; CHECK-NEXT: [[RET:%.*]] = fcmp uno float [[X:%.*]], 0.000000e+00
35+
; CHECK-NEXT: ret i1 [[RET]]
3636
;
37-
%floor = call nnan float @llvm.floor.f32(float %x)
37+
%floor = call float @llvm.floor.f32(float %x)
3838
%ret = fcmp ugt float %floor, %x
3939
ret i1 %ret
4040
}
4141

4242
define i1 @x_floor_oge(float %x) {
4343
; CHECK-LABEL: @x_floor_oge(
44-
; CHECK-NEXT: [[FLOOR:%.*]] = call float @llvm.floor.f32(float [[X:%.*]])
45-
; CHECK-NEXT: [[RET:%.*]] = fcmp ord float [[FLOOR]], 0.000000e+00
44+
; CHECK-NEXT: [[RET:%.*]] = fcmp ord float [[X:%.*]], 0.000000e+00
4645
; CHECK-NEXT: ret i1 [[RET]]
4746
;
4847
%floor = call float @llvm.floor.f32(float %x)
@@ -54,7 +53,7 @@ define i1 @x_floor_uge(float %x) {
5453
; CHECK-LABEL: @x_floor_uge(
5554
; CHECK-NEXT: ret i1 true
5655
;
57-
%floor = call nnan float @llvm.floor.f32(float %x)
56+
%floor = call float @llvm.floor.f32(float %x)
5857
%ret = fcmp uge float %x, %floor
5958
ret i1 %ret
6059
}
@@ -68,9 +67,10 @@ define i1 @x_floor_olt(float %x) {
6867
ret i1 %ret
6968
}
7069

71-
define i1 @x_floor_ult(float nofpclass(nan) %x) {
70+
define i1 @x_floor_ult(float %x) {
7271
; CHECK-LABEL: @x_floor_ult(
73-
; CHECK-NEXT: ret i1 false
72+
; CHECK-NEXT: [[RET:%.*]] = fcmp uno float [[X:%.*]], 0.000000e+00
73+
; CHECK-NEXT: ret i1 [[RET]]
7474
;
7575
%floor = call float @llvm.floor.f32(float %x)
7676
%ret = fcmp ult float %x, %floor
@@ -109,10 +109,9 @@ define i1 @ceil_x_oge(float %x) {
109109

110110
define i1 @ceil_x_uge(float %x) {
111111
; CHECK-LABEL: @ceil_x_uge(
112-
; CHECK-NEXT: [[RET:%.*]] = fcmp ninf ord float [[X:%.*]], 0.000000e+00
113-
; CHECK-NEXT: ret i1 [[RET]]
112+
; CHECK-NEXT: ret i1 true
114113
;
115-
%ceil = call nnan float @llvm.ceil.f32(float %x)
114+
%ceil = call float @llvm.ceil.f32(float %x)
116115
%ret = fcmp ninf uge float %ceil, %x
117116
ret i1 %ret
118117
}
@@ -128,17 +127,17 @@ define i1 @ceil_x_olt(float %x) {
128127

129128
define i1 @ceil_x_ult(float %x) {
130129
; CHECK-LABEL: @ceil_x_ult(
131-
; CHECK-NEXT: ret i1 false
130+
; CHECK-NEXT: [[RET:%.*]] = fcmp uno float [[X:%.*]], 0.000000e+00
131+
; CHECK-NEXT: ret i1 [[RET]]
132132
;
133-
%ceil = call nnan float @llvm.ceil.f32(float %x)
133+
%ceil = call float @llvm.ceil.f32(float %x)
134134
%ret = fcmp ult float %ceil, %x
135135
ret i1 %ret
136136
}
137137

138138
define i1 @x_ceil_ole(float %x) {
139139
; CHECK-LABEL: @x_ceil_ole(
140-
; CHECK-NEXT: [[CEIL:%.*]] = call float @llvm.ceil.f32(float [[X:%.*]])
141-
; CHECK-NEXT: [[RET:%.*]] = fcmp ord float [[CEIL]], 0.000000e+00
140+
; CHECK-NEXT: [[RET:%.*]] = fcmp ord float [[X:%.*]], 0.000000e+00
142141
; CHECK-NEXT: ret i1 [[RET]]
143142
;
144143
%ceil = call float @llvm.ceil.f32(float %x)
@@ -150,7 +149,7 @@ define i1 @x_ceil_ule(float %x) {
150149
; CHECK-LABEL: @x_ceil_ule(
151150
; CHECK-NEXT: ret i1 true
152151
;
153-
%ceil = call nnan float @llvm.ceil.f32(float %x)
152+
%ceil = call float @llvm.ceil.f32(float %x)
154153
%ret = fcmp ule float %x, %ceil
155154
ret i1 %ret
156155
}
@@ -164,9 +163,10 @@ define i1 @x_ceil_ogt(float %x) {
164163
ret i1 %ret
165164
}
166165

167-
define i1 @x_ceil_ugt(float nofpclass(nan) %x) {
166+
define i1 @x_ceil_ugt(float %x) {
168167
; CHECK-LABEL: @x_ceil_ugt(
169-
; CHECK-NEXT: ret i1 false
168+
; CHECK-NEXT: [[RET:%.*]] = fcmp uno float [[X:%.*]], 0.000000e+00
169+
; CHECK-NEXT: ret i1 [[RET]]
170170
;
171171
%ceil = call float @llvm.ceil.f32(float %x)
172172
%ret = fcmp ugt float %x, %ceil

0 commit comments

Comments
 (0)