[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Chris Lattner lattner at cs.uiuc.edu
Sat Apr 23 08:32:09 PDT 2005



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.324 -> 1.325
---
Log message:

Generalize the setcc -> PHI and Select  folding optimizations to work with
any constant RHS, not just a constant integer RHS.  This implements
select.ll:test17


---
Diffs of the changes:  (+37 -30)

 InstructionCombining.cpp |   67 +++++++++++++++++++++++++----------------------
 1 files changed, 37 insertions(+), 30 deletions(-)


Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.324 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.325
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.324	Thu Apr 21 18:45:12 2005
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Sat Apr 23 10:31:55 2005
@@ -2450,10 +2450,6 @@
 
     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
       switch (LHSI->getOpcode()) {
-      case Instruction::PHI:
-        if (Instruction *NV = FoldOpIntoPhi(I))
-          return NV;
-        break;
       case Instruction::And:
         if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
             LHSI->getOperand(0)->hasOneUse()) {
@@ -2705,32 +2701,6 @@
           }
         }
         break;
-      case Instruction::Select:
-        // If either operand of the select is a constant, we can fold the
-        // comparison into the select arms, which will cause one to be
-        // constant folded and the select turned into a bitwise or.
-        Value *Op1 = 0, *Op2 = 0;
-        if (LHSI->hasOneUse()) {
-          if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
-            // Fold the known value into the constant operand.
-            Op1 = ConstantExpr::get(I.getOpcode(), C, CI);
-            // Insert a new SetCC of the other select operand.
-            Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
-                                                      LHSI->getOperand(2), CI,
-                                                      I.getName()), I);
-          } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
-            // Fold the known value into the constant operand.
-            Op2 = ConstantExpr::get(I.getOpcode(), C, CI);
-            // Insert a new SetCC of the other select operand.
-            Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
-                                                      LHSI->getOperand(1), CI,
-                                                      I.getName()), I);
-          }
-        }
-
-        if (Op1)
-          return new SelectInst(LHSI->getOperand(0), Op1, Op2);
-        break;
       }
 
     // Simplify seteq and setne instructions...
@@ -2896,6 +2866,43 @@
     }
   }
 
+  // Handle setcc with constant RHS's that can be integer, FP or pointer.
+  if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
+    if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
+      switch (LHSI->getOpcode()) {
+      case Instruction::PHI:
+        if (Instruction *NV = FoldOpIntoPhi(I))
+          return NV;
+        break;
+      case Instruction::Select:
+        // If either operand of the select is a constant, we can fold the
+        // comparison into the select arms, which will cause one to be
+        // constant folded and the select turned into a bitwise or.
+        Value *Op1 = 0, *Op2 = 0;
+        if (LHSI->hasOneUse()) {
+          if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
+            // Fold the known value into the constant operand.
+            Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC);
+            // Insert a new SetCC of the other select operand.
+            Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
+                                                      LHSI->getOperand(2), RHSC,
+                                                      I.getName()), I);
+          } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
+            // Fold the known value into the constant operand.
+            Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC);
+            // Insert a new SetCC of the other select operand.
+            Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
+                                                      LHSI->getOperand(1), RHSC,
+                                                      I.getName()), I);
+          }
+        }
+        
+        if (Op1)
+          return new SelectInst(LHSI->getOperand(0), Op1, Op2);
+        break;
+      }
+  }
+
   // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now.
   if (User *GEP = dyn_castGetElementPtr(Op0))
     if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I))






More information about the llvm-commits mailing list