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) {