summaryrefslogtreecommitdiff
path: root/lib/Transforms/InstCombine/InstCombineShifts.cpp
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2010-11-23 18:52:42 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2010-11-23 18:52:42 +0000
commitb70ebd2aa3b6f4546d4734e7bcdbed2017036b4d (patch)
tree7a1cb693711ee656cedc1b3c67fddc8f6bf61aa0 /lib/Transforms/InstCombine/InstCombineShifts.cpp
parente104f1bccbf828dcbe4e82709c8ca97d8bf018d5 (diff)
downloadllvm-b70ebd2aa3b6f4546d4734e7bcdbed2017036b4d.tar.gz
llvm-b70ebd2aa3b6f4546d4734e7bcdbed2017036b4d.tar.bz2
llvm-b70ebd2aa3b6f4546d4734e7bcdbed2017036b4d.tar.xz
InstCombine: Reduce "X shift (A srem B)" to "X shift (A urem B)" iff B is positive.
This allows to transform the rem in "1 << ((int)x % 8);" to an and. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120028 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/InstCombine/InstCombineShifts.cpp')
-rw-r--r--lib/Transforms/InstCombine/InstCombineShifts.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/Transforms/InstCombine/InstCombineShifts.cpp b/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 9f7d98ed79..7969dbfec0 100644
--- a/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -53,6 +53,21 @@ Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
return Res;
+
+ // X shift (A srem B) -> X shift (A urem B) iff B is positive.
+ // Because shifts by negative values are undefined.
+ if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op1))
+ if (BO->getOpcode() == Instruction::SRem && BO->getType()->isIntegerTy()) {
+ // Make sure the divisor's sign bit is zero.
+ APInt Mask = APInt::getSignBit(BO->getType()->getPrimitiveSizeInBits());
+ if (MaskedValueIsZero(BO->getOperand(1), Mask)) {
+ Value *URem = Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
+ BO->getName());
+ I.setOperand(1, URem);
+ return &I;
+ }
+ }
+
return 0;
}