Skip to content

Commit a2240f5

Browse files
committed
[InstCombine] simplify fcmp+select canonicalization; NFCI
We had 2 blocks of code that are nearly identical. Existing regression tests should cover both of the patterns.
1 parent 984fad2 commit a2240f5

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,9 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
23332333

23342334
// See if we are selecting two values based on a comparison of the two values.
23352335
if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
2336-
if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
2336+
Value *Cmp0 = FCI->getOperand(0), *Cmp1 = FCI->getOperand(1);
2337+
if ((Cmp0 == TrueVal && Cmp1 == FalseVal) ||
2338+
(Cmp0 == FalseVal && Cmp1 == TrueVal)) {
23372339
// Canonicalize to use ordered comparisons by swapping the select
23382340
// operands.
23392341
//
@@ -2343,25 +2345,7 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
23432345
FCmpInst::Predicate InvPred = FCI->getInversePredicate();
23442346
IRBuilder<>::FastMathFlagGuard FMFG(Builder);
23452347
Builder.setFastMathFlags(FCI->getFastMathFlags());
2346-
Value *NewCond = Builder.CreateFCmp(InvPred, TrueVal, FalseVal,
2347-
FCI->getName() + ".inv");
2348-
2349-
return SelectInst::Create(NewCond, FalseVal, TrueVal,
2350-
SI.getName() + ".p");
2351-
}
2352-
2353-
// NOTE: if we wanted to, this is where to detect MIN/MAX
2354-
} else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
2355-
// Canonicalize to use ordered comparisons by swapping the select
2356-
// operands.
2357-
//
2358-
// e.g.
2359-
// (X ugt Y) ? X : Y -> (X ole Y) ? X : Y
2360-
if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) {
2361-
FCmpInst::Predicate InvPred = FCI->getInversePredicate();
2362-
IRBuilder<>::FastMathFlagGuard FMFG(Builder);
2363-
Builder.setFastMathFlags(FCI->getFastMathFlags());
2364-
Value *NewCond = Builder.CreateFCmp(InvPred, FalseVal, TrueVal,
2348+
Value *NewCond = Builder.CreateFCmp(InvPred, Cmp0, Cmp1,
23652349
FCI->getName() + ".inv");
23662350

23672351
return SelectInst::Create(NewCond, FalseVal, TrueVal,

0 commit comments

Comments
 (0)