[llvm-commits] [llvm] r67635 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/shift.ll

Chris Lattner sabre at nondot.org
Tue Mar 24 11:15:30 PDT 2009


Author: lattner
Date: Tue Mar 24 13:15:30 2009
New Revision: 67635

URL: http://llvm.org/viewvc/llvm-project?rev=67635&view=rev
Log:
two changes:
1. Make instcombine always canonicalize trunc x to i1 into an icmp(x&1).  This 
   exposes the AND to other instcombine xforms and is more of what the code
   generator expects.
2. Rewrite the remaining trunc pattern match to use 'match', which 
   simplifies it a lot.
   

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/shift.ll

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=67635&r1=67634&r2=67635&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Tue Mar 24 13:15:30 2009
@@ -6575,8 +6575,8 @@
       // preferable because it allows the C<<Y expression to be hoisted out
       // of a loop if Y is invariant and X is not.
       if (Shift && Shift->hasOneUse() && RHSV == 0 &&
-          ICI.isEquality() && !Shift->isArithmeticShift() &&
-          isa<Instruction>(Shift->getOperand(0))) {
+          ICI.isEquality() && !Shift->isArithmeticShift()/* &&
+          isa<Instruction>(Shift->getOperand(0))*/) {
         // Compute C << Y.
         Value *NS;
         if (Shift->getOpcode() == Instruction::LShr) {
@@ -8147,49 +8147,33 @@
   const Type *Ty = CI.getType();
   uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits();
   uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth();
-  
-  if (Instruction *SrcI = dyn_cast<Instruction>(Src)) {
-    switch (SrcI->getOpcode()) {
-    default: break;
-    case Instruction::LShr:
-      // We can shrink lshr to something smaller if we know the bits shifted in
-      // are already zeros.
-      if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) {
-        uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
-        
-        // Get a mask for the bits shifting in.
-        APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
-        Value* SrcIOp0 = SrcI->getOperand(0);
-        if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) {
-          if (ShAmt >= DestBitWidth)        // All zeros.
-            return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
-
-          // Okay, we can shrink this.  Truncate the input, then return a new
-          // shift.
-          Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI);
-          Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1),
-                                       Ty, CI);
-          return BinaryOperator::CreateLShr(V1, V2);
-        }
-      } else {     // This is a variable shr.
-        
-        // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'.  This is
-        // more LLVM instructions, but allows '1 << Y' to be hoisted if
-        // loop-invariant and CSE'd.
-        if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) {
-          Value *One = ConstantInt::get(SrcI->getType(), 1);
-
-          Value *V = InsertNewInstBefore(
-              BinaryOperator::CreateShl(One, SrcI->getOperand(1),
-                                     "tmp"), CI);
-          V = InsertNewInstBefore(BinaryOperator::CreateAnd(V,
-                                                            SrcI->getOperand(0),
-                                                            "tmp"), CI);
-          Value *Zero = Constant::getNullValue(V->getType());
-          return new ICmpInst(ICmpInst::ICMP_NE, V, Zero);
-        }
-      }
-      break;
+
+  // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0)
+  if (DestBitWidth == 1) {
+    Constant *One = ConstantInt::get(Src->getType(), 1);
+    Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI);
+    Value *Zero = Constant::getNullValue(Src->getType());
+    return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
+  }
+  
+  // Optimize trunc(lshr(), c) to pull the shift through the truncate.
+  ConstantInt *ShAmtV = 0;
+  Value *ShiftOp = 0;
+  if (Src->hasOneUse() &&
+      match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)))) {
+    uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
+    
+    // Get a mask for the bits shifting in.
+    APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
+    if (MaskedValueIsZero(ShiftOp, Mask)) {
+      if (ShAmt >= DestBitWidth)        // All zeros.
+        return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
+      
+      // Okay, we can shrink this.  Truncate the input, then return a new
+      // shift.
+      Value *V1 = InsertCastBefore(Instruction::Trunc, ShiftOp, Ty, CI);
+      Value *V2 = ConstantExpr::getTrunc(ShAmtV, Ty);
+      return BinaryOperator::CreateLShr(V1, V2);
     }
   }
   

Modified: llvm/trunk/test/Transforms/InstCombine/shift.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/shift.ll?rev=67635&r1=67634&r2=67635&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/shift.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/shift.ll Tue Mar 24 13:15:30 2009
@@ -206,4 +206,11 @@
         %D = shl i32 %C, 1              ; <i32> [#uses=1]
         ret i32 %D
 }
+
+
+define i1 @test27(i32 %x) nounwind {
+  %y = lshr i32 %x, 3
+  %z = trunc i32 %y to i1
+  ret i1 %z
+}
  





More information about the llvm-commits mailing list