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.

llvm-svn: 24190
This commit is contained in:
Chris Lattner 2005-11-05 07:40:31 +00:00
parent e9ff0eaf5b
commit dd0c174082
1 changed files with 19 additions and 0 deletions
llvm/lib/Transforms/Scalar

View File

@ -1240,6 +1240,25 @@ Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
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;
}