Skip to content

Commit 3355284

Browse files
committed
[NFC][SCEVExpander] isHighCostExpansionHelper(): rewrite as a switch
If we switch over an enum, compiler can easily issue a diagnostic if some case is not handled. However with an if cascade that isn't so. Experimental evidence suggests new behavior to be superior.
1 parent d605a11 commit 3355284

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,31 +2345,35 @@ bool SCEVExpander::isHighCostExpansionHelper(
23452345
if (getRelatedExistingExpansion(S, &At, L))
23462346
return false; // Consider the expression to be free.
23472347

2348-
// Assume to be zero-cost.
2349-
if (isa<SCEVUnknown>(S))
2350-
return false;
2351-
23522348
TargetTransformInfo::TargetCostKind CostKind =
2353-
L->getHeader()->getParent()->hasMinSize()
2354-
? TargetTransformInfo::TCK_CodeSize
2355-
: TargetTransformInfo::TCK_RecipThroughput;
2349+
L->getHeader()->getParent()->hasMinSize()
2350+
? TargetTransformInfo::TCK_CodeSize
2351+
: TargetTransformInfo::TCK_RecipThroughput;
23562352

2357-
if (auto *Constant = dyn_cast<SCEVConstant>(S)) {
2353+
switch (S->getSCEVType()) {
2354+
case scUnknown:
2355+
// Assume to be zero-cost.
2356+
return false;
2357+
case scConstant: {
2358+
auto *Constant = dyn_cast<SCEVConstant>(S);
23582359
// Only evalulate the costs of constants when optimizing for size.
23592360
if (CostKind != TargetTransformInfo::TCK_CodeSize)
23602361
return 0;
23612362
const APInt &Imm = Constant->getAPInt();
23622363
Type *Ty = S->getType();
2363-
BudgetRemaining -=
2364-
TTI.getIntImmCostInst(WorkItem.ParentOpcode, WorkItem.OperandIdx,
2365-
Imm, Ty, CostKind);
2364+
BudgetRemaining -= TTI.getIntImmCostInst(
2365+
WorkItem.ParentOpcode, WorkItem.OperandIdx, Imm, Ty, CostKind);
23662366
return BudgetRemaining < 0;
2367-
} else if (isa<SCEVIntegralCastExpr>(S)) {
2367+
}
2368+
case scTruncate:
2369+
case scZeroExtend:
2370+
case scSignExtend: {
23682371
int Cost = costAndCollectOperands<SCEVIntegralCastExpr>(WorkItem, TTI,
23692372
CostKind, Worklist);
23702373
BudgetRemaining -= Cost;
23712374
return false; // Will answer upon next entry into this function.
2372-
} else if (isa<SCEVUDivExpr>(S)) {
2375+
}
2376+
case scUDivExpr: {
23732377
// UDivExpr is very likely a UDiv that ScalarEvolution's HowFarToZero or
23742378
// HowManyLessThans produced to compute a precise expression, rather than a
23752379
// UDiv from the user's code. If we can't find a UDiv in the code with some
@@ -2383,27 +2387,36 @@ bool SCEVExpander::isHighCostExpansionHelper(
23832387
return false; // Consider it to be free.
23842388

23852389
int Cost =
2386-
costAndCollectOperands<SCEVUDivExpr>(WorkItem, TTI, CostKind, Worklist);
2390+
costAndCollectOperands<SCEVUDivExpr>(WorkItem, TTI, CostKind, Worklist);
23872391
// Need to count the cost of this UDiv.
23882392
BudgetRemaining -= Cost;
23892393
return false; // Will answer upon next entry into this function.
2390-
} else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(S)) {
2394+
}
2395+
case scAddExpr:
2396+
case scMulExpr:
2397+
case scUMaxExpr:
2398+
case scSMaxExpr:
2399+
case scUMinExpr:
2400+
case scSMinExpr: {
2401+
const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(S);
23912402
assert(NAry->getNumOperands() > 1 &&
23922403
"Nary expr should have more than 1 operand.");
23932404
// The simple nary expr will require one less op (or pair of ops)
23942405
// than the number of it's terms.
23952406
int Cost =
2396-
costAndCollectOperands<SCEVNAryExpr>(WorkItem, TTI, CostKind, Worklist);
2407+
costAndCollectOperands<SCEVNAryExpr>(WorkItem, TTI, CostKind, Worklist);
23972408
BudgetRemaining -= Cost;
23982409
return BudgetRemaining < 0;
2399-
} else if (isa<SCEVAddRecExpr>(S)) {
2410+
}
2411+
case scAddRecExpr: {
24002412
assert(cast<SCEVAddRecExpr>(S)->getNumOperands() >= 2 &&
24012413
"Polynomial should be at least linear");
24022414
BudgetRemaining -= costAndCollectOperands<SCEVAddRecExpr>(
2403-
WorkItem, TTI, CostKind, Worklist);
2415+
WorkItem, TTI, CostKind, Worklist);
24042416
return BudgetRemaining < 0;
2405-
} else
2406-
llvm_unreachable("No other scev expressions possible.");
2417+
}
2418+
}
2419+
llvm_unreachable("Switch is exaustive and we return in all of them.");
24072420
}
24082421

24092422
Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred,

0 commit comments

Comments
 (0)