diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2010-11-23 20:33:57 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2010-11-23 20:33:57 +0000 |
commit | c21a821e9f044004bc504dd7bfdfaecb9d4af16b (patch) | |
tree | e01a80c5fdfbe1e0a146ff48666d071091f90c62 /lib/Transforms | |
parent | b6133d148312a3ec15beb67c2f49a21e5082b666 (diff) | |
download | external_llvm-c21a821e9f044004bc504dd7bfdfaecb9d4af16b.zip external_llvm-c21a821e9f044004bc504dd7bfdfaecb9d4af16b.tar.gz external_llvm-c21a821e9f044004bc504dd7bfdfaecb9d4af16b.tar.bz2 |
The srem -> urem transform is not safe for any divisor that's not a power of two.
E.g. -5 % 5 is 0 with srem and 1 with urem.
Also addresses Frits van Bommel's comments.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120049 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/InstCombine/InstCombineShifts.cpp | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/lib/Transforms/InstCombine/InstCombineShifts.cpp b/lib/Transforms/InstCombine/InstCombineShifts.cpp index 7969dbf..e52f201 100644 --- a/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -54,19 +54,17 @@ Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) return Res; - // X shift (A srem B) -> X shift (A urem B) iff B is positive. + // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2. // 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; - } - } + if (BO->hasOneUse() && BO->getOpcode() == Instruction::SRem) + if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) + if (CI->getValue().isPowerOf2()) { + Constant *C = ConstantInt::get(BO->getType(), CI->getValue()-1); + Value *Rem = Builder->CreateAnd(BO->getOperand(0), C, BO->getName()); + I.setOperand(1, Rem); + return &I; + } return 0; } |