Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -1569,7 +1569,8 @@ return N1; // fold (add c1, c2) -> c1+c2 if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C)) + return Folded; // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0); @@ -1813,7 +1814,8 @@ 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 (SDValue Folded = DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C)) + return Folded; // fold (sub x, c) -> (add x, -c) if (N1C) return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, @@ -1967,8 +1969,8 @@ // fold (mul c1, c2) -> c1*c2 if (N0IsConst && N1IsConst) - return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode()); - + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode())) + return Folded; // canonicalize constant to RHS if (N0IsConst && !N1IsConst) return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0); @@ -2071,8 +2073,9 @@ } // fold (sdiv c1, c2) -> c1/c2 - if (N0C && N1C && !N1C->isNullValue()) - return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); + if (N0C && N1C) + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C)) + return Folded; // fold (sdiv X, 1) -> X if (N1C && N1C->getAPIntValue() == 1LL) return N0; @@ -2161,8 +2164,9 @@ } // fold (udiv c1, c2) -> c1/c2 - if (N0C && N1C && !N1C->isNullValue()) - return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); + if (N0C && N1C) + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C)) + 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 +2211,9 @@ 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) + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C)) + 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 +2254,13 @@ 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) + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C)) + 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))) { @@ -2706,7 +2712,8 @@ return DAG.getConstant(0, VT); // fold (and c1, c2) -> c1&c2 if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C)) + return Folded; // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0); @@ -3417,7 +3424,8 @@ } // fold (or c1, c2) -> c1|c2 if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C)) + return Folded; // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0); @@ -3826,7 +3834,8 @@ return N1; // fold (xor c1, c2) -> c1^c2 if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C)) + return Folded; // canonicalize constant to RHS if (N0C && !N1C) return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0); @@ -4082,7 +4091,8 @@ // fold (shl c1, c2) -> c1< 0 if (N0C && N0C->isNullValue()) return N0; @@ -4244,7 +4254,8 @@ // fold (sra c1, c2) -> (sra c1, c2) if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C)) + return Folded; // fold (sra 0, x) -> 0 if (N0C && N0C->isNullValue()) return N0; @@ -4390,7 +4401,8 @@ // fold (srl c1, c2) -> c1 >>u c2 if (N0C && N1C) - return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); + if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C)) + return Folded; // fold (srl 0, x) -> 0 if (N0C && N0C->isNullValue()) return N0;