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

Chris Lattner lattner at cs.uiuc.edu
Tue Sep 28 20:09:32 PDT 2004



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.255 -> 1.256
---
Log message:

Remove debugging printout
Instcombine (setcc (truncate X), C1).

This occurs THOUSANDS of times in many benchmarks.  Particularlly common
seem to be things like (seteq (cast bool X to int), int 0)

This turns it into (seteq bool %X, false), which then becomes (not %X).


---
Diffs of the changes:  (+38 -1)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.255 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.256
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.255	Tue Sep 28 17:33:08 2004
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Tue Sep 28 22:09:18 2004
@@ -837,7 +837,6 @@
     if (Instruction *LHS = dyn_cast<Instruction>(I.getOperand(0)))
       if (LHS->getOpcode() == Instruction::Div)
         if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
-          std::cerr << "DIV: " << *LHS << "   : " << I;
           // (X / C1) / C2  -> X / (C1*C2)
           return BinaryOperator::createDiv(LHS->getOperand(0),
                                            ConstantExpr::getMul(RHS, LHSRHS));
@@ -1764,6 +1763,44 @@
         }
         break;
 
+      case Instruction::Cast: {       // (setcc (cast X to larger), CI)
+        const Type *SrcTy = LHSI->getOperand(0)->getType();
+        if (SrcTy->isIntegral() && LHSI->getType()->isIntegral()) {
+          unsigned SrcBits = SrcTy->getPrimitiveSize();
+          if (SrcTy == Type::BoolTy) SrcBits = 1;
+          unsigned DestBits = LHSI->getType()->getPrimitiveSize();
+          if (LHSI->getType() == Type::BoolTy) DestBits = 1;
+          if (SrcBits < DestBits) {
+            // Check to see if the comparison is always true or false.
+            Constant *NewCst = ConstantExpr::getCast(CI, SrcTy);
+            if (ConstantExpr::getCast(NewCst, LHSI->getType()) != CI) {
+              Constant *Min = ConstantIntegral::getMinValue(SrcTy);
+              Constant *Max = ConstantIntegral::getMaxValue(SrcTy);
+              Min = ConstantExpr::getCast(Min, LHSI->getType());
+              Max = ConstantExpr::getCast(Max, LHSI->getType());
+              switch (I.getOpcode()) {
+              default: assert(0 && "unknown integer comparison");
+              case Instruction::SetEQ:
+                return ReplaceInstUsesWith(I, ConstantBool::False);
+              case Instruction::SetNE:
+                return ReplaceInstUsesWith(I, ConstantBool::True);
+              case Instruction::SetLT:
+                return ReplaceInstUsesWith(I, ConstantExpr::getSetLT(Max, CI));
+              case Instruction::SetLE:
+                return ReplaceInstUsesWith(I, ConstantExpr::getSetLE(Max, CI));
+              case Instruction::SetGT:
+                return ReplaceInstUsesWith(I, ConstantExpr::getSetGT(Min, CI));
+              case Instruction::SetGE:
+                return ReplaceInstUsesWith(I, ConstantExpr::getSetGE(Min, CI));
+              }
+            }
+
+            return new SetCondInst(I.getOpcode(), LHSI->getOperand(0),
+                                   ConstantExpr::getCast(CI, SrcTy));
+          }
+        }
+        break;
+      }
       case Instruction::Shl:         // (setcc (shl X, ShAmt), CI)
         if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) {
           switch (I.getOpcode()) {






More information about the llvm-commits mailing list