From 9e3b7e8e656bdcf5aeafbac7b345a7b29272434c Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Thu, 28 Apr 2022 17:08:20 +0100 Subject: [PATCH] [X86] getTargetVShiftByConstNode - use SelectionDAG::FoldConstantArithmetic to perform constant folding. NFCI. Remove some unnecessary code duplication. --- llvm/lib/Target/X86/X86ISelLowering.cpp | 44 ++++++--------------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index c23c832..1faee7c 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -25797,53 +25797,23 @@ static SDValue getTargetVShiftByConstNode(unsigned Opc, const SDLoc &dl, MVT VT, // Fold this packed vector shift into a build vector if SrcOp is a // vector of Constants or UNDEFs. if (ISD::isBuildVectorOfConstantSDNodes(SrcOp.getNode())) { - SmallVector Elts; - unsigned NumElts = SrcOp->getNumOperands(); - + unsigned ShiftOpc; switch (Opc) { default: llvm_unreachable("Unknown opcode!"); case X86ISD::VSHLI: - for (unsigned i = 0; i != NumElts; ++i) { - SDValue CurrentOp = SrcOp->getOperand(i); - if (CurrentOp->isUndef()) { - // Must produce 0s in the correct bits. - Elts.push_back(DAG.getConstant(0, dl, ElementType)); - continue; - } - auto *ND = cast(CurrentOp); - const APInt &C = ND->getAPIntValue(); - Elts.push_back(DAG.getConstant(C.shl(ShiftAmt), dl, ElementType)); - } + ShiftOpc = ISD::SHL; break; case X86ISD::VSRLI: - for (unsigned i = 0; i != NumElts; ++i) { - SDValue CurrentOp = SrcOp->getOperand(i); - if (CurrentOp->isUndef()) { - // Must produce 0s in the correct bits. - Elts.push_back(DAG.getConstant(0, dl, ElementType)); - continue; - } - auto *ND = cast(CurrentOp); - const APInt &C = ND->getAPIntValue(); - Elts.push_back(DAG.getConstant(C.lshr(ShiftAmt), dl, ElementType)); - } + ShiftOpc = ISD::SRL; break; case X86ISD::VSRAI: - for (unsigned i = 0; i != NumElts; ++i) { - SDValue CurrentOp = SrcOp->getOperand(i); - if (CurrentOp->isUndef()) { - // All shifted in bits must be the same so use 0. - Elts.push_back(DAG.getConstant(0, dl, ElementType)); - continue; - } - auto *ND = cast(CurrentOp); - const APInt &C = ND->getAPIntValue(); - Elts.push_back(DAG.getConstant(C.ashr(ShiftAmt), dl, ElementType)); - } + ShiftOpc = ISD::SRA; break; } - return DAG.getBuildVector(VT, dl, Elts); + SDValue Amt = DAG.getConstant(ShiftAmt, dl, VT); + if (SDValue C = DAG.FoldConstantArithmetic(ShiftOpc, dl, VT, {SrcOp, Amt})) + return C; } return DAG.getNode(Opc, dl, VT, SrcOp, -- 2.7.4