[llvm-commits] [llvm] r118378 - in /llvm/trunk: lib/Analysis/InstructionSimplify.cpp lib/Target/README.txt test/Transforms/InstCombine/select.ll

Duncan Sands baldrick at free.fr
Sun Nov 7 08:12:23 PST 2010


Author: baldrick
Date: Sun Nov  7 10:12:23 2010
New Revision: 118378

URL: http://llvm.org/viewvc/llvm-project?rev=118378&view=rev
Log:
Fix a README item: when doing a comparison with the result
of a select instruction, see if doing the compare with the
true and false values of the select gives the same result.
If so, that can be used as the value of the comparison.

Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/lib/Target/README.txt
    llvm/trunk/test/Transforms/InstCombine/select.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=118378&r1=118377&r2=118378&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Sun Nov  7 10:12:23 2010
@@ -252,8 +252,27 @@
       break;
     }
   }
-  
-  
+
+  // 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 "icmp select(cond, TV, FV), RHS", analyse it.
+    // Does "icmp TV, RHS" simplify?
+    if (Value *TCmp = SimplifyICmpInst(Pred, SI->getTrueValue(), RHS, TD))
+      // It does!  Does "icmp FV, RHS" simplify?
+      if (Value *FCmp = SimplifyICmpInst(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;
 }
 

Modified: llvm/trunk/lib/Target/README.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/README.txt?rev=118378&r1=118377&r2=118378&view=diff
==============================================================================
--- llvm/trunk/lib/Target/README.txt (original)
+++ llvm/trunk/lib/Target/README.txt Sun Nov  7 10:12:23 2010
@@ -1963,15 +1963,3 @@
         ret i32 %b
 }
 //===---------------------------------------------------------------------===//
-We should fold this code into "ret i1 false" since neither %zero nor %one can
-ever be null pointers.
-
-define i1 @foo(i1 %cond) {
-  %zero = alloca i32
-  %one = alloca i32
-
-  %ptr = select i1 %cond, i32* %zero, i32* %one
-  %isnull = icmp eq i32* %ptr, null
-  ret i1 %isnull
-}
-//===---------------------------------------------------------------------===//

Modified: llvm/trunk/test/Transforms/InstCombine/select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/select.ll?rev=118378&r1=118377&r2=118378&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/select.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/select.ll Sun Nov  7 10:12:23 2010
@@ -470,3 +470,13 @@
 ; CHECK: or i32 {{.*}}, 1
 ; CHECK: ret
 }
+
+define i1 @test38(i1 %cond) {
+  %zero = alloca i32
+  %one = alloca i32
+  %ptr = select i1 %cond, i32* %zero, i32* %one
+  %isnull = icmp eq i32* %ptr, null
+  ret i1 %isnull
+; CHECK: @test38
+; CHECK: ret i1 false
+}





More information about the llvm-commits mailing list