Index: include/llvm/CodeGen/SelectionDAG.h =================================================================== --- include/llvm/CodeGen/SelectionDAG.h +++ include/llvm/CodeGen/SelectionDAG.h @@ -388,7 +388,11 @@ Node->OperandList = nullptr; } void CreateTopologicalOrder(std::vector& Order); + public: + // Maximum depth for DAG recursion, used by computeKnownBits etc. + static constexpr unsigned MaxRecursionDepth = 6; + explicit SelectionDAG(const TargetMachine &TM, CodeGenOpt::Level); SelectionDAG(const SelectionDAG &) = delete; SelectionDAG &operator=(const SelectionDAG &) = delete; Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -806,7 +806,7 @@ return 0; // Don't recurse exponentially. - if (Depth > 6) + if (Depth > SelectionDAG::MaxRecursionDepth) return 0; switch (Op.getOpcode()) { @@ -913,7 +913,8 @@ if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0); - assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree"); + assert(Depth <= SelectionDAG::MaxRecursionDepth && + "GetNegatedExpression doesn't match isNegatibleForFree"); const TargetOptions &Options = DAG.getTarget().Options; const SDNodeFlags Flags = Op->getFlags(); Index: lib/CodeGen/SelectionDAG/SelectionDAG.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -2422,7 +2422,7 @@ return Known; } - if (Depth >= 6) + if (Depth >= MaxRecursionDepth) return Known; // Limit search depth. KnownBits Known2; @@ -3412,7 +3412,7 @@ return Val.getNumSignBits(); } - if (Depth >= 6) + if (Depth >= MaxRecursionDepth) return 1; // Limit search depth. if (!DemandedElts) @@ -3973,7 +3973,7 @@ if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs()) return true; - if (Depth >= 6) + if (Depth >= MaxRecursionDepth) return false; // Limit search depth. // TODO: Handle vectors. Index: lib/CodeGen/SelectionDAG/TargetLowering.cpp =================================================================== --- lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -585,7 +585,7 @@ SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, SelectionDAG &DAG, unsigned Depth) const { // Limit search depth. - if (Depth >= 6) + if (Depth >= SelectionDAG::MaxRecursionDepth) return SDValue(); // Ignore UNDEFs. @@ -798,7 +798,8 @@ } else if (OriginalDemandedBits == 0 || OriginalDemandedElts == 0) { // Not demanding any bits/elts from Op. return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT)); - } else if (Depth >= 6) { // Limit search depth. + } else if (Depth >= SelectionDAG::MaxRecursionDepth) { + // Limit search depth. return false; } @@ -2107,7 +2108,7 @@ } // Limit search depth. - if (Depth >= 6) + if (Depth >= SelectionDAG::MaxRecursionDepth) return false; SDLoc DL(Op); Index: lib/Target/AArch64/AArch64ISelDAGToDAG.cpp =================================================================== --- lib/Target/AArch64/AArch64ISelDAGToDAG.cpp +++ lib/Target/AArch64/AArch64ISelDAGToDAG.cpp @@ -2053,7 +2053,7 @@ } static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) { - if (Depth >= 6) + if (Depth >= SelectionDAG::MaxRecursionDepth) return; // Initialize UsefulBits if (!Depth) {