Index: llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h =================================================================== --- llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h +++ llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h @@ -17,6 +17,7 @@ #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/Support/InstructionCost.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h" #include "llvm/Analysis/ScalarEvolutionNormalization.h" #include "llvm/Analysis/TargetFolder.h" @@ -235,7 +236,7 @@ return true; // by always claiming to be high-cost. SmallVector Worklist; SmallPtrSet Processed; - int BudgetRemaining = Budget * TargetTransformInfo::TCC_Basic; + InstructionCost BudgetRemaining(Budget * TargetTransformInfo::TCC_Basic); Worklist.emplace_back(-1, -1, Expr); while (!Worklist.empty()) { const SCEVOperand WorkItem = Worklist.pop_back_val(); @@ -243,6 +244,7 @@ *TTI, Processed, Worklist)) return true; } + assert(BudgetRemaining.isValid() && "Expected a valid cost"); assert(BudgetRemaining >= 0 && "Should have returned from inner loop."); return false; } @@ -409,7 +411,7 @@ /// Recursive helper function for isHighCostExpansion. bool isHighCostExpansionHelper( const SCEVOperand &WorkItem, Loop *L, const Instruction &At, - int &BudgetRemaining, const TargetTransformInfo &TTI, + InstructionCost &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,13 +2199,13 @@ return None; } -template static int costAndCollectOperands( +template static InstructionCost costAndCollectOperands( const SCEVOperand &WorkItem, const TargetTransformInfo &TTI, TargetTransformInfo::TargetCostKind CostKind, SmallVectorImpl &Worklist) { const T *S = cast(WorkItem.S); - int Cost = 0; + InstructionCost Cost = 0; // Object to help map SCEV operands to expanded IR instructions. struct OperationIndices { OperationIndices(unsigned Opc, size_t min, size_t max) : @@ -2340,10 +2340,10 @@ bool SCEVExpander::isHighCostExpansionHelper( const SCEVOperand &WorkItem, Loop *L, const Instruction &At, - int &BudgetRemaining, const TargetTransformInfo &TTI, + InstructionCost &BudgetRemaining, const TargetTransformInfo &TTI, SmallPtrSetImpl &Processed, SmallVectorImpl &Worklist) { - if (BudgetRemaining < 0) + if (!BudgetRemaining.isValid() || BudgetRemaining < 0) return true; // Already run out of budget, give up. const SCEV *S = WorkItem.S; @@ -2375,16 +2375,15 @@ Type *Ty = S->getType(); BudgetRemaining -= TTI.getIntImmCostInst( WorkItem.ParentOpcode, WorkItem.OperandIdx, Imm, Ty, CostKind); - return BudgetRemaining < 0; + return !BudgetRemaining.isValid() || BudgetRemaining < 0; } case scTruncate: case scPtrToInt: case scZeroExtend: case scSignExtend: { - int Cost = + BudgetRemaining -= costAndCollectOperands(WorkItem, TTI, CostKind, Worklist); - BudgetRemaining -= Cost; - return false; // Will answer upon next entry into this function. + return !BudgetRemaining.isValid() || BudgetRemaining < 0; } case scUDivExpr: { // UDivExpr is very likely a UDiv that ScalarEvolution's HowFarToZero or @@ -2399,11 +2398,9 @@ SE.getAddExpr(S, SE.getConstant(S->getType(), 1)), &At, L)) return false; // Consider it to be free. - int Cost = + BudgetRemaining -= 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 !BudgetRemaining.isValid() || BudgetRemaining < 0; } case scAddExpr: case scMulExpr: @@ -2415,17 +2412,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 = + BudgetRemaining -= costAndCollectOperands(WorkItem, TTI, CostKind, Worklist); - BudgetRemaining -= Cost; - return BudgetRemaining < 0; + return !BudgetRemaining.isValid() || BudgetRemaining < 0; } case scAddRecExpr: { assert(cast(S)->getNumOperands() >= 2 && "Polynomial should be at least linear"); BudgetRemaining -= costAndCollectOperands( WorkItem, TTI, CostKind, Worklist); - return BudgetRemaining < 0; + return !BudgetRemaining.isValid() || BudgetRemaining < 0; } } llvm_unreachable("Unknown SCEV kind!");