diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -150,11 +150,6 @@ cl::desc("Allow exactly one expensive instruction to be speculatively " "executed")); -static cl::opt MaxSpeculationDepth( - "max-speculation-depth", cl::Hidden, cl::init(10), - cl::desc("Limit maximum recursion depth when calculating costs of " - "speculatively executed instructions")); - static cl::opt MaxSmallBlockSize("simplifycfg-max-small-block-size", cl::Hidden, cl::init(10), @@ -370,69 +365,65 @@ SmallPtrSetImpl &AggressiveInsts, InstructionCost &Cost, InstructionCost Budget, - const TargetTransformInfo &TTI, - unsigned Depth = 0) { - // It is possible to hit a zero-cost cycle (phi/gep instructions for example), - // so limit the recursion depth. - // TODO: While this recursion limit does prevent pathological behavior, it - // would be better to track visited instructions to avoid cycles. - if (Depth == MaxSpeculationDepth) - return false; + const TargetTransformInfo &TTI) { + SmallVector Stack; - Instruction *I = dyn_cast(V); - if (!I) { - // Non-instructions all dominate instructions, but not all constantexprs - // can be executed unconditionally. - if (ConstantExpr *C = dyn_cast(V)) - if (C->canTrap()) - return false; - return true; - } - BasicBlock *PBB = I->getParent(); + Stack.emplace_back(V); - // We don't want to allow weird loops that might have the "if condition" in - // the bottom of this block. - if (PBB == BB) - return false; + while (Stack.size() != 0){ + Value * CurV = Stack.pop_back_val(); + Instruction *I = dyn_cast(CurV); - // If this instruction is defined in a block that contains an unconditional - // branch to BB, then it must be in the 'conditional' part of the "if - // statement". If not, it definitely dominates the region. - BranchInst *BI = dyn_cast(PBB->getTerminator()); - if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB) - return true; + if (!I) { + // Non-instructions all dominate instructions, but not all constantexprs + // can be executed unconditionally. + if (ConstantExpr *C = dyn_cast(V)) + if (C->canTrap()) + return false; + continue; + } + BasicBlock *PBB = I->getParent(); - // If we have seen this instruction before, don't count it again. - if (AggressiveInsts.count(I)) - return true; + if (PBB == BB) + return false; - // Okay, it looks like the instruction IS in the "condition". Check to - // see if it's a cheap instruction to unconditionally compute, and if it - // only uses stuff defined outside of the condition. If so, hoist it out. - if (!isSafeToSpeculativelyExecute(I)) - return false; + // If this instruction is defined in a block that contains an unconditional + // branch to BB, then it must be in the 'conditional' part of the "if + // statement". If not, it definitely dominates the region. + BranchInst *BI = dyn_cast(PBB->getTerminator()); + if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB) + continue; - Cost += computeSpeculationCost(I, TTI); - - // Allow exactly one instruction to be speculated regardless of its cost - // (as long as it is safe to do so). - // This is intended to flatten the CFG even if the instruction is a division - // or other expensive operation. The speculation of an expensive instruction - // is expected to be undone in CodeGenPrepare if the speculation has not - // enabled further IR optimizations. - if (Cost > Budget && - (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || Depth > 0 || - !Cost.isValid())) - return false; + // If we have seen this instruction before, don't count it again. + if (AggressiveInsts.contains(I)) + continue; - // Okay, we can only really hoist these out if their operands do - // not take us over the cost threshold. - for (Use &Op : I->operands()) - if (!dominatesMergePoint(Op, BB, AggressiveInsts, Cost, Budget, TTI, - Depth + 1)) + // Okay, it looks like the instruction IS in the "condition". Check to + // see if it's a cheap instruction to unconditionally compute, and if it + // only uses stuff defined outside of the condition. If so, hoist it out. + if (!isSafeToSpeculativelyExecute(I)) return false; - // Okay, it's safe to do this! Remember this instruction. - AggressiveInsts.insert(I); + + Cost += computeSpeculationCost(I, TTI); + + // Allow exactly one instruction to be speculated regardless of its cost + // (as long as it is safe to do so). + // This is intended to flatten the CFG even if the instruction is a division + // or other expensive operation. The speculation of an expensive instruction + // is expected to be undone in CodeGenPrepare if the speculation has not + // enabled further IR optimizations. + if (Cost > Budget && + (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || + !Cost.isValid())) + return false; + + // Okay, we can only really hoist these out if their operands do + // not take us over the cost threshold. + for (Use &Op : I->operands()) + Stack.emplace_back(Op); + // Okay, it's safe to do this! Remember this instruction. + AggressiveInsts.insert(I); + } return true; }