Add simplification of floating point comparisons with the result
authorDuncan Sands <baldrick@free.fr>
Sun, 7 Nov 2010 16:46:25 +0000 (16:46 +0000)
committerDuncan Sands <baldrick@free.fr>
Sun, 7 Nov 2010 16:46:25 +0000 (16:46 +0000)
of a select instruction, the same as already exists for integer
comparisons.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@118379 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstCombine/select.ll

index 1955802cc6ed360e5c164d679660e67bfdafea6c..66452a5d9865fa8634817f97dee3612b2f895125 100644 (file)
@@ -350,6 +350,26 @@ Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
     }
   }
   
+  // If the comparison is with the result of a select instruction, check whether
+  // comparing with either branch of the select always yields the same value.
+  if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) {
+    // Make sure the select is on the LHS.
+    if (!isa<SelectInst>(LHS)) {
+      std::swap(LHS, RHS);
+      Pred = CmpInst::getSwappedPredicate(Pred);
+    }
+    SelectInst *SI = cast<SelectInst>(LHS);
+    // Now that we have "fcmp select(cond, TV, FV), RHS", analyse it.
+    // Does "fcmp TV, RHS" simplify?
+    if (Value *TCmp = SimplifyFCmpInst(Pred, SI->getTrueValue(), RHS, TD))
+      // It does!  Does "fcmp FV, RHS" simplify?
+      if (Value *FCmp = SimplifyFCmpInst(Pred, SI->getFalseValue(), RHS, TD))
+        // It does!  If they simplified to the same value, then use it as the
+        // result of the original comparison.
+        if (TCmp == FCmp)
+          return TCmp;
+  }
+
   return 0;
 }
 
index c641339cbb6db7d77407736504d6d31f319648d4..6738abc4680650770d3675498310f28275446e25 100644 (file)
@@ -480,3 +480,11 @@ define i1 @test38(i1 %cond) {
 ; CHECK: @test38
 ; CHECK: ret i1 false
 }
+
+define i1 @test39(i1 %cond, double %x) {
+  %s = select i1 %cond, double %x, double 0x7FF0000000000000 ; RHS = +infty
+  %cmp = fcmp ule double %x, %s
+  ret i1 %cmp
+; CHECK: @test39
+; CHECK: ret i1 true
+}