From dd0c174082eda6cf40ae58c8218b43b253214e92 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 5 Nov 2005 07:40:31 +0000 Subject: [PATCH] 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 ; [#uses=1] NEW: %tmp.36 = div uint %tmp.35, 8 ; [#uses=0] OLD: %tmp.19 = div int %tmp.18, 8 ; [#uses=1] NEW: %tmp.19 = div uint %tmp.18, 8 ; [#uses=0] OLD: %tmp.117 = div int %tmp.116, 8 ; [#uses=1] NEW: %tmp.117 = div uint %tmp.116, 8 ; [#uses=0] OLD: %tmp.92 = div int %tmp.91, 8 ; [#uses=1] NEW: %tmp.92 = div uint %tmp.91, 8 ; [#uses=0] Which all turn into shrs. llvm-svn: 24190 --- .../Scalar/InstructionCombining.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index 4a764c996d7d..1164fb2e03e0 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp @@ -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(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; } -- GitLab