@@ -8181,79 +8181,93 @@ static Instruction *foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI,
8181
8181
static Instruction *foldFCmpWithFloorAndCeil (FCmpInst &I,
8182
8182
InstCombinerImpl &CI) {
8183
8183
Value *LHS = I.getOperand (0 ), *RHS = I.getOperand (1 );
8184
- const CmpInst::Predicate Pred = I.getPredicate ();
8185
8184
Type *OpType = LHS->getType ();
8185
+ const CmpInst::Predicate Pred = I.getPredicate ();
8186
8186
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)));
8204
8191
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)
8212
8197
return new FCmpInst (FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero (OpType),
8213
8198
" " , &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),
8231
8201
" " , &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)
8237
8207
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)
8248
8216
return new FCmpInst (FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero (OpType),
8249
8217
" " , &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)
8255
8223
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 ;
8257
8271
}
8258
8272
8259
8273
return nullptr ;
0 commit comments