Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -1568,8 +1568,11 @@ if (N1.getOpcode() == ISD::UNDEF) return N1; // fold (add c1, c2) -> c1+c2 - if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0); @@ -1812,8 +1815,11 @@ if (N0 == N1) return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes); // fold (sub c1, c2) -> c1-c2 - if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // fold (sub x, c) -> (add x, -c) if (N1C) return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, @@ -1966,8 +1972,12 @@ } // fold (mul c1, c2) -> c1*c2 - if (N0IsConst && N1IsConst) - return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode()); + if (N0IsConst && N1IsConst) { + SDValue Folded = + DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode()); + if (Folded.getNode()) + return Folded; + } // canonicalize constant to RHS if (N0IsConst && !N1IsConst) @@ -2071,8 +2081,11 @@ } // fold (sdiv c1, c2) -> c1/c2 - if (N0C && N1C && !N1C->isNullValue()) - return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // fold (sdiv X, 1) -> X if (N1C && N1C->getAPIntValue() == 1LL) return N0; @@ -2161,8 +2174,11 @@ } // fold (udiv c1, c2) -> c1/c2 - if (N0C && N1C && !N1C->isNullValue()) - return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // fold (udiv x, (1 << c)) -> x >>u c if (N1C && N1C->getAPIntValue().isPowerOf2()) return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, @@ -2207,8 +2223,11 @@ EVT VT = N->getValueType(0); // fold (srem c1, c2) -> c1%c2 - if (N0C && N1C && !N1C->isNullValue()) - return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // If we know the sign bits of both operands are zero, strength reduce to a // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 if (!VT.isVector()) { @@ -2249,12 +2268,15 @@ EVT VT = N->getValueType(0); // fold (urem c1, c2) -> c1%c2 - if (N0C && N1C && !N1C->isNullValue()) - return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // fold (urem x, pow2) -> (and x, pow2-1) if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2()) return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, - DAG.getConstant(N1C->getAPIntValue()-1,VT)); + DAG.getConstant(N1C->getAPIntValue() - 1, VT)); // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) if (N1.getOpcode() == ISD::SHL) { if (ConstantSDNode *SHC = dyn_cast(N1.getOperand(0))) { @@ -2705,8 +2727,11 @@ if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) return DAG.getConstant(0, VT); // fold (and c1, c2) -> c1&c2 - if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0); @@ -3416,8 +3441,11 @@ return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); } // fold (or c1, c2) -> c1|c2 - if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0); @@ -3825,8 +3853,11 @@ if (N1.getOpcode() == ISD::UNDEF) return N1; // fold (xor c1, c2) -> c1^c2 - if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0); @@ -4081,8 +4112,11 @@ } // fold (shl c1, c2) -> c1< 0 if (N0C && N0C->isNullValue()) return N0; @@ -4243,8 +4277,11 @@ } // fold (sra c1, c2) -> (sra c1, c2) - if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // fold (sra 0, x) -> 0 if (N0C && N0C->isNullValue()) return N0; @@ -4389,8 +4426,11 @@ } // fold (srl c1, c2) -> c1 >>u c2 - if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); + if (N0C && N1C) { + SDValue Folded = DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); + if (Folded.getNode()) + return Folded; + } // fold (srl 0, x) -> 0 if (N0C && N0C->isNullValue()) return N0;