Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -86,14 +86,13 @@ #define DEBUG_TYPE "simplifycfg" -// Chosen as 2 so as to be cheap, but still to have enough power to fold -// a select, so the "clamp" idiom (of a min followed by a max) will be caught. -// To catch this, we need to fold a compare and a select, hence '2' being the -// minimum reasonable default. +/// The cost of converting each phi to a select, with the total cost allowed +/// for the block is this value multiplied by the total number of phis, plus +/// one to allow the speculation of a single instruction. static cl::opt PHINodeFoldingThreshold( - "phi-node-folding-threshold", cl::Hidden, cl::init(2), + "phi-node-folding-threshold", cl::Hidden, cl::init(1), cl::desc( - "Control the amount of phi node folding to perform (default = 2)")); + "Control the amount of phi node folding to perform (default = 1)")); static cl::opt TwoEntryPHINodeFoldingThreshold( "two-entry-phi-node-folding-threshold", cl::Hidden, cl::init(4), @@ -2037,10 +2036,7 @@ return false; unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0; unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0; - unsigned MaxCost = - 2 * PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; - if (OrigCost + ThenCost > MaxCost) - return false; + BudgetRemaining -= OrigCost + ThenCost; // Account for the cost of an unfolded ConstantExpr which could end up // getting expanded into Instructions. @@ -2100,8 +2096,12 @@ BasicBlock *BB = BI->getParent(); BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0); - int BudgetRemaining = - PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; + + // Enable the cost of speculating a single basic instruction plus a basic cost + // for each phi in the block. + unsigned NumPHIs = std::distance(EndBB->phis().begin(), EndBB->phis().end()); + int BudgetRemaining = TargetTransformInfo::TCC_Basic + + (TargetTransformInfo::TCC_Basic * NumPHIs * PHINodeFoldingThreshold); // If ThenBB is actually on the false edge of the conditional branch, remember // to swap the select operands later. @@ -2145,10 +2145,11 @@ !(HoistCondStores && (SpeculatedStoreValue = isSafeToSpeculateStore( I, BB, ThenBB, EndBB)))) return false; - if (!SpeculatedStoreValue && - ComputeSpeculationCost(I, TTI) > - PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic) - return false; + if (!SpeculatedStoreValue) { + BudgetRemaining -= ComputeSpeculationCost(I, TTI); + if (BudgetRemaining < 0) + return false; + } // Store the store speculation candidate. if (SpeculatedStoreValue)