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

Chris Lattner lattner at cs.uiuc.edu
Fri Nov 4 23:40:43 PST 2005



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.404 -> 1.405
---
Log message:

Turn sdiv into udiv if both operands have a clear sign bit.  This occurs
a few times in crafty:

OLD:    %tmp.36 = div int %tmp.35, 8            ; <int> [#uses=1]
NEW:    %tmp.36 = div uint %tmp.35, 8           ; <uint> [#uses=0]
OLD:    %tmp.19 = div int %tmp.18, 8            ; <int> [#uses=1]
NEW:    %tmp.19 = div uint %tmp.18, 8           ; <uint> [#uses=0]
OLD:    %tmp.117 = div int %tmp.116, 8          ; <int> [#uses=1]
NEW:    %tmp.117 = div uint %tmp.116, 8         ; <uint> [#uses=0]
OLD:    %tmp.92 = div int %tmp.91, 8            ; <int> [#uses=1]
NEW:    %tmp.92 = div uint %tmp.91, 8           ; <uint> [#uses=0]

Which all turn into shrs.



---
Diffs of the changes:  (+19 -0)

 InstructionCombining.cpp |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+)


Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.404 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.405
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.404	Sat Nov  5 01:28:37 2005
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Sat Nov  5 01:40:31 2005
@@ -1240,6 +1240,25 @@
     if (LHS->equalsInt(0))
       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
 
+  if (I.getType()->isSigned()) {
+    // If the top bits of both operands are zero (i.e. we can prove they are
+    // unsigned inputs), turn this into a udiv.
+    ConstantIntegral *MaskV = ConstantSInt::getMinValue(I.getType());
+    if (MaskedValueIsZero(Op1, MaskV) && MaskedValueIsZero(Op0, MaskV)) {
+      const Type *NTy = Op0->getType()->getUnsignedVersion();
+      Instruction *LHS = new CastInst(Op0, NTy, Op0->getName());
+      InsertNewInstBefore(LHS, I);
+      Value *RHS;
+      if (Constant *R = dyn_cast<Constant>(Op1))
+        RHS = ConstantExpr::getCast(R, NTy);
+      else
+        RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I);
+      Instruction *Div = BinaryOperator::createDiv(LHS, RHS, I.getName());
+      InsertNewInstBefore(Div, I);
+      return new CastInst(Div, I.getType());
+    }      
+  }
+  
   return 0;
 }
 






More information about the llvm-commits mailing list