Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -335,8 +335,8 @@ /// which is assumed to be safe to speculate. TCC_Free means cheap, /// TCC_Basic means less cheap, and TCC_Expensive means prohibitively /// expensive. -static unsigned ComputeSpeculationCost(const User *I, - const TargetTransformInfo &TTI) { +static InstructionCost ComputeSpeculationCost(const User *I, + const TargetTransformInfo &TTI) { assert(isSafeToSpeculativelyExecute(I) && "Instruction is not safe to speculatively execute!"); return TTI.getUserCost(I, TargetTransformInfo::TCK_SizeAndLatency); @@ -361,7 +361,7 @@ /// CostRemaining, false is returned and CostRemaining is undefined. static bool DominatesMergePoint(Value *V, BasicBlock *BB, SmallPtrSetImpl &AggressiveInsts, - int &BudgetRemaining, + InstructionBudget &BudgetRemaining, const TargetTransformInfo &TTI, unsigned Depth = 0) { // It is possible to hit a zero-cost cycle (phi/gep instructions for example), @@ -412,8 +412,9 @@ // 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 (BudgetRemaining < 0 && - (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || Depth > 0)) + if (BudgetRemaining.isExceeded() && + (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || Depth > 0 || + BudgetRemaining.isInvalidated())) return false; // Okay, we can only really hoist these out if their operands do @@ -2113,7 +2114,7 @@ static bool validateAndCostRequiredSelects(BasicBlock *BB, BasicBlock *ThenBB, BasicBlock *EndBB, unsigned &SpeculatedInstructions, - int &BudgetRemaining, + InstructionBudget &BudgetRemaining, const TargetTransformInfo &TTI) { TargetTransformInfo::TargetCostKind CostKind = BB->getParent()->hasMinSize() @@ -2148,9 +2149,9 @@ if ((ThenCE && !isSafeToSpeculativelyExecute(ThenCE)) || (OrigCE && !isSafeToSpeculativelyExecute(OrigCE))) return false; - unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0; - unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0; - unsigned MaxCost = + InstructionCost OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0; + InstructionCost ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0; + InstructionCost MaxCost = 2 * PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; if (OrigCost + ThenCost > MaxCost) return false; @@ -2213,7 +2214,7 @@ BasicBlock *BB = BI->getParent(); BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0); - int BudgetRemaining = + InstructionBudget BudgetRemaining = PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; // If ThenBB is actually on the false edge of the conditional branch, remember @@ -2306,7 +2307,7 @@ Convert |= validateAndCostRequiredSelects(BB, ThenBB, EndBB, SpeculatedInstructions, BudgetRemaining, TTI); - if (!Convert || BudgetRemaining < 0) + if (!Convert || BudgetRemaining.isExceeded()) return false; // If we get here, we can hoist the instruction and if-convert. @@ -2560,7 +2561,7 @@ // instructions. While we are at it, keep track of the instructions // that need to be moved to the dominating block. SmallPtrSet AggressiveInsts; - int BudgetRemaining = + InstructionBudget BudgetRemaining = TwoEntryPHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; bool Changed = false; @@ -3185,7 +3186,7 @@ // Heuristic: if the block can be if-converted/phi-folded and the // instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to // thread this store. - int BudgetRemaining = + InstructionBudget BudgetRemaining = PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; for (auto &I : BB->instructionsWithoutDebug()) { // Consider terminator instruction to be free. @@ -3202,10 +3203,10 @@ // And finally, if this is a non-free instruction that we are okay // speculating, ensure that we consider the speculation budget. BudgetRemaining -= TTI.getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency); - if (BudgetRemaining < 0) + if (BudgetRemaining.isExceeded()) return false; // Eagerly refuse to fold as soon as we're out of budget. } - assert(BudgetRemaining >= 0 && + assert(!BudgetRemaining.isExceeded() && "When we run out of budget we will eagerly return from within the " "per-instruction loop."); return true;