diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2019-03-17 19:08:00 +0000 |
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2019-03-17 19:08:00 +0000 |
| commit | b3bcd95771810f75736c1be0088618a03c9f0fe3 (patch) | |
| tree | 21063a0040e6b0b5e5bfe4d9025d39933038093f /llvm/lib/Transforms | |
| parent | a3a2f9424e0c45e0fefa1c81bd370bfa8d8ac1e2 (diff) | |
| download | bcm5719-llvm-b3bcd95771810f75736c1be0088618a03c9f0fe3.tar.gz bcm5719-llvm-b3bcd95771810f75736c1be0088618a03c9f0fe3.zip | |
[InstCombine] canonicalize rotate right by constant to rotate left
This was noted as a backend problem:
https://bugs.llvm.org/show_bug.cgi?id=41057
...and subsequently fixed for x86:
rL356121
But we should canonicalize these in IR for the benefit of all targets
and improve IR analysis such as CSE.
llvm-svn: 356338
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 1c292ea1334..bdc235f247d 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1993,22 +1993,36 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { case Intrinsic::fshl: case Intrinsic::fshr: { - // Canonicalize a shift amount constant operand to be modulo the bit-width. - unsigned BitWidth = II->getType()->getScalarSizeInBits(); + Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1); + Type *Ty = II->getType(); + unsigned BitWidth = Ty->getScalarSizeInBits(); Constant *ShAmtC; if (match(II->getArgOperand(2), m_Constant(ShAmtC)) && !isa<ConstantExpr>(ShAmtC) && !ShAmtC->containsConstantExpression()) { - Constant *WidthC = ConstantInt::get(II->getType(), BitWidth); + // Canonicalize a shift amount constant operand to modulo the bit-width. + Constant *WidthC = ConstantInt::get(Ty, BitWidth); Constant *ModuloC = ConstantExpr::getURem(ShAmtC, WidthC); if (ModuloC != ShAmtC) { II->setArgOperand(2, ModuloC); return II; } + // Canonicalize rotate right by constant to rotate left. This is not + // entirely arbitrary. For historical reasons, the backend may recognize + // rotate left patterns but miss rotate right patterns. + if (II->getIntrinsicID() == Intrinsic::fshr && Op0 == Op1) { + // fshr X, X, C --> fshl X, X, (BitWidth - C) + assert(ConstantExpr::getICmp(ICmpInst::ICMP_UGT, WidthC, ShAmtC) == + ConstantInt::getTrue(CmpInst::makeCmpResultType(Ty)) && + "Shift amount expected to be modulo bitwidth"); + Constant *LeftShiftC = ConstantExpr::getSub(WidthC, ShAmtC); + Module *Mod = II->getModule(); + Function *Fshl = Intrinsic::getDeclaration(Mod, Intrinsic::fshl, Ty); + return CallInst::Create(Fshl, { Op0, Op0, LeftShiftC }); + } } const APInt *SA; if (match(II->getArgOperand(2), m_APInt(SA))) { - Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1); uint64_t ShiftAmt = SA->urem(BitWidth); assert(ShiftAmt != 0 && "SimplifyCall should have handled zero shift"); // Normalize to funnel shift left. @@ -2018,14 +2032,13 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { // fshl(X, 0, C) -> shl X, C // fshl(X, undef, C) -> shl X, C if (match(Op1, m_Zero()) || match(Op1, m_Undef())) - return BinaryOperator::CreateShl( - Op0, ConstantInt::get(II->getType(), ShiftAmt)); + return BinaryOperator::CreateShl(Op0, ConstantInt::get(Ty, ShiftAmt)); // fshl(0, X, C) -> lshr X, (BW-C) // fshl(undef, X, C) -> lshr X, (BW-C) if (match(Op0, m_Zero()) || match(Op0, m_Undef())) return BinaryOperator::CreateLShr( - Op1, ConstantInt::get(II->getType(), BitWidth - ShiftAmt)); + Op1, ConstantInt::get(Ty, BitWidth - ShiftAmt)); } // The shift amount (operand 2) of a funnel shift is modulo the bitwidth, |

