From 8e77458578d601cf714c0f444667f83ea2840621 Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Mon, 17 Oct 2022 16:19:02 +0100 Subject: [PATCH] [DAG] visitShiftByConstant - replace constant detection with FoldConstantArithmetic Instead of checking that an operand is constant/opaque before calling getNode() and then checking that the result is a constant, just use FoldConstantArithmetic which will just early-out if the operands are not constant foldable. --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index ddd90c3ee8b4..33136452d4f2 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -8880,11 +8880,6 @@ SDValue DAGCombiner::visitShiftByConstant(SDNode *N) { break; } - // We require the RHS of the binop to be a constant and not opaque as well. - ConstantSDNode *BinOpCst = getAsNonOpaqueConstant(LHS.getOperand(1)); - if (!BinOpCst) - return SDValue(); - // FIXME: disable this unless the input to the binop is a shift by a constant // or is copy/select. Enable this in other cases when figure out it's exactly // profitable. @@ -8902,16 +8897,17 @@ SDValue DAGCombiner::visitShiftByConstant(SDNode *N) { if (IsCopyOrSelect && N->hasOneUse()) return SDValue(); - // Fold the constants, shifting the binop RHS by the shift amount. + // Attempt to fold the constants, shifting the binop RHS by the shift amount. SDLoc DL(N); EVT VT = N->getValueType(0); - SDValue NewRHS = DAG.getNode(N->getOpcode(), DL, VT, LHS.getOperand(1), - N->getOperand(1)); - assert(isa(NewRHS) && "Folding was not successful!"); + if (SDValue NewRHS = DAG.FoldConstantArithmetic( + N->getOpcode(), DL, VT, {LHS.getOperand(1), N->getOperand(1)})) { + SDValue NewShift = DAG.getNode(N->getOpcode(), DL, VT, LHS.getOperand(0), + N->getOperand(1)); + return DAG.getNode(LHS.getOpcode(), DL, VT, NewShift, NewRHS); + } - SDValue NewShift = DAG.getNode(N->getOpcode(), DL, VT, LHS.getOperand(0), - N->getOperand(1)); - return DAG.getNode(LHS.getOpcode(), DL, VT, NewShift, NewRHS); + return SDValue(); } SDValue DAGCombiner::distributeTruncateThroughAnd(SDNode *N) {