Index: llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h =================================================================== --- llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h +++ llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h @@ -235,7 +235,7 @@ return true; // by always claiming to be high-cost. SmallVector Worklist; SmallPtrSet Processed; - int BudgetRemaining = Budget * TargetTransformInfo::TCC_Basic; + unsigned BudgetRemaining = Budget * TargetTransformInfo::TCC_Basic; Worklist.emplace_back(-1, -1, Expr); while (!Worklist.empty()) { const SCEVOperand WorkItem = Worklist.pop_back_val(); @@ -409,7 +409,7 @@ /// Recursive helper function for isHighCostExpansion. bool isHighCostExpansionHelper( const SCEVOperand &WorkItem, Loop *L, const Instruction &At, - int &BudgetRemaining, const TargetTransformInfo &TTI, + unsigned &BudgetRemaining, const TargetTransformInfo &TTI, SmallPtrSetImpl &Processed, SmallVectorImpl &Worklist); Index: llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp =================================================================== --- llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp +++ llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp @@ -2199,7 +2199,7 @@ return None; } -template static int costAndCollectOperands( +template static unsigned costAndCollectOperands( const SCEVOperand &WorkItem, const TargetTransformInfo &TTI, TargetTransformInfo::TargetCostKind CostKind, SmallVectorImpl &Worklist) { @@ -2340,12 +2340,9 @@ bool SCEVExpander::isHighCostExpansionHelper( const SCEVOperand &WorkItem, Loop *L, const Instruction &At, - int &BudgetRemaining, const TargetTransformInfo &TTI, + unsigned &BudgetRemaining, const TargetTransformInfo &TTI, SmallPtrSetImpl &Processed, SmallVectorImpl &Worklist) { - if (BudgetRemaining < 0) - return true; // Already run out of budget, give up. - const SCEV *S = WorkItem.S; // Was the cost of expansion of this expression already accounted for? if (!isa(S) && !Processed.insert(S).second) @@ -2361,6 +2358,17 @@ ? TargetTransformInfo::TCK_CodeSize : TargetTransformInfo::TCK_RecipThroughput; + // Convenience function to subtract from budget. Returns 'true' + // if the budget would be exceeded. + auto SubtractFromBudget = [](unsigned &BudgetRemaining, + const unsigned &Cost) -> bool { + if (Cost > BudgetRemaining) + return true; + + BudgetRemaining -= Cost; + return false; + }; + switch (S->getSCEVType()) { case scCouldNotCompute: llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); @@ -2373,18 +2381,17 @@ return 0; const APInt &Imm = cast(S)->getAPInt(); Type *Ty = S->getType(); - BudgetRemaining -= TTI.getIntImmCostInst( + unsigned Cost = TTI.getIntImmCostInst( WorkItem.ParentOpcode, WorkItem.OperandIdx, Imm, Ty, CostKind); - return BudgetRemaining < 0; + return SubtractFromBudget(BudgetRemaining, Cost); } case scTruncate: case scPtrToInt: case scZeroExtend: case scSignExtend: { - int Cost = + unsigned Cost = costAndCollectOperands(WorkItem, TTI, CostKind, Worklist); - BudgetRemaining -= Cost; - return false; // Will answer upon next entry into this function. + return SubtractFromBudget(BudgetRemaining, Cost); } case scUDivExpr: { // UDivExpr is very likely a UDiv that ScalarEvolution's HowFarToZero or @@ -2399,11 +2406,9 @@ SE.getAddExpr(S, SE.getConstant(S->getType(), 1)), &At, L)) return false; // Consider it to be free. - int Cost = + unsigned Cost = costAndCollectOperands(WorkItem, TTI, CostKind, Worklist); - // Need to count the cost of this UDiv. - BudgetRemaining -= Cost; - return false; // Will answer upon next entry into this function. + return SubtractFromBudget(BudgetRemaining, Cost); } case scAddExpr: case scMulExpr: @@ -2415,17 +2420,16 @@ "Nary expr should have more than 1 operand."); // The simple nary expr will require one less op (or pair of ops) // than the number of it's terms. - int Cost = + unsigned Cost = costAndCollectOperands(WorkItem, TTI, CostKind, Worklist); - BudgetRemaining -= Cost; - return BudgetRemaining < 0; + return SubtractFromBudget(BudgetRemaining, Cost); } case scAddRecExpr: { assert(cast(S)->getNumOperands() >= 2 && "Polynomial should be at least linear"); - BudgetRemaining -= costAndCollectOperands( + unsigned Cost = costAndCollectOperands( WorkItem, TTI, CostKind, Worklist); - return BudgetRemaining < 0; + return SubtractFromBudget(BudgetRemaining, Cost); } } llvm_unreachable("Unknown SCEV kind!");