From 411b86081e131e13ab4981e21e7752405fb9d49a Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 17 Aug 2018 13:23:44 +0000 Subject: [ConstantFolding] add simplifications for funnel shift intrinsics This is another step towards being able to canonicalize to the funnel shift intrinsics in IR (see D49242 for the initial patch). We should not have any loss of simplification power in IR between these and the equivalent IR constructs. Differential Revision: https://reviews.llvm.org/D50848 llvm-svn: 340022 --- llvm/lib/Analysis/ConstantFolding.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'llvm/lib') diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index f741f66d462..bbd812bf517 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -1389,6 +1389,8 @@ bool llvm::canConstantFoldCallTo(ImmutableCallSite CS, const Function *F) { case Intrinsic::ctpop: case Intrinsic::ctlz: case Intrinsic::cttz: + case Intrinsic::fshl: + case Intrinsic::fshr: case Intrinsic::fma: case Intrinsic::fmuladd: case Intrinsic::copysign: @@ -2081,6 +2083,29 @@ Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty, } } + if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) { + auto *C0 = dyn_cast(Operands[0]); + auto *C1 = dyn_cast(Operands[1]); + auto *C2 = dyn_cast(Operands[2]); + if (!(C0 && C1 && C2)) + return nullptr; + + // The shift amount is interpreted as modulo the bitwidth. If the shift + // amount is effectively 0, avoid UB due to oversized inverse shift below. + unsigned BitWidth = C0->getBitWidth(); + unsigned ShAmt = C2->getValue().urem(BitWidth); + bool IsRight = IntrinsicID == Intrinsic::fshr; + if (!ShAmt) + return IsRight ? C1 : C0; + + // (X << ShlAmt) | (Y >> LshrAmt) + const APInt &X = C0->getValue(); + const APInt &Y = C1->getValue(); + unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt; + unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt; + return ConstantInt::get(Ty->getContext(), X.shl(ShlAmt) | Y.lshr(LshrAmt)); + } + return nullptr; } -- cgit v1.2.3