Skip to content

Commit e056758

Browse files
committed
[NFCI][SCEV] Always refer to enum SCEVTypes as enum, not integer
The main tricky thing here is forward-declaring the enum: we have to specify it's underlying data type. In particular, this avoids the danger of switching over the SCEVTypes, but actually switching over an integer, and not being notified when some case is not handled. I have updated most of such switches to be exaustive and not have a default case, where it's pretty obvious to be the intent, however not all of them.
1 parent d4b0aa9 commit e056758

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class StructType;
7070
class TargetLibraryInfo;
7171
class Type;
7272
class Value;
73+
enum SCEVTypes : unsigned short;
7374

7475
/// This class represents an analyzed expression in the program. These are
7576
/// opaque objects that the client is not allowed to do much with directly.
@@ -82,7 +83,7 @@ class SCEV : public FoldingSetNode {
8283
FoldingSetNodeIDRef FastID;
8384

8485
// The SCEV baseclass this node corresponds to
85-
const unsigned short SCEVType;
86+
const SCEVTypes SCEVType;
8687

8788
protected:
8889
// Estimated complexity of this node's expression tree size.
@@ -119,13 +120,13 @@ class SCEV : public FoldingSetNode {
119120
NoWrapMask = (1 << 3) - 1
120121
};
121122

122-
explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy,
123+
explicit SCEV(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy,
123124
unsigned short ExpressionSize)
124125
: FastID(ID), SCEVType(SCEVTy), ExpressionSize(ExpressionSize) {}
125126
SCEV(const SCEV &) = delete;
126127
SCEV &operator=(const SCEV &) = delete;
127128

128-
unsigned getSCEVType() const { return SCEVType; }
129+
SCEVTypes getSCEVType() const { return SCEVType; }
129130

130131
/// Return the LLVM type of this SCEV expression.
131132
Type *getType() const;
@@ -574,7 +575,7 @@ class ScalarEvolution {
574575
const SmallVectorImpl<const SCEV *> &IndexExprs);
575576
const SCEV *getAbsExpr(const SCEV *Op, bool IsNSW);
576577
const SCEV *getSignumExpr(const SCEV *Op);
577-
const SCEV *getMinMaxExpr(unsigned Kind,
578+
const SCEV *getMinMaxExpr(SCEVTypes Kind,
578579
SmallVectorImpl<const SCEV *> &Operands);
579580
const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
580581
const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
@@ -1958,7 +1959,7 @@ class ScalarEvolution {
19581959
/// constructed to look up the SCEV and the third component is the insertion
19591960
/// point.
19601961
std::tuple<SCEV *, FoldingSetNodeID, void *>
1961-
findExistingSCEVInCache(int SCEVType, ArrayRef<const SCEV *> Ops);
1962+
findExistingSCEVInCache(SCEVTypes SCEVType, ArrayRef<const SCEV *> Ops);
19621963

19631964
FoldingSet<SCEV> UniqueSCEVs;
19641965
FoldingSet<SCEVPredicate> UniquePreds;

llvm/include/llvm/Analysis/ScalarEvolutionExpressions.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ConstantRange;
3535
class Loop;
3636
class Type;
3737

38-
enum SCEVTypes {
38+
enum SCEVTypes : unsigned short {
3939
// These should be ordered in terms of increasing complexity to make the
4040
// folders simpler.
4141
scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
@@ -77,7 +77,7 @@ class Type;
7777
std::array<const SCEV *, 1> Operands;
7878
Type *Ty;
7979

80-
SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, unsigned SCEVTy,
80+
SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy,
8181
const SCEV *op, Type *ty);
8282

8383
public:
@@ -412,7 +412,7 @@ class Type;
412412

413413
public:
414414
static bool classof(const SCEV *S) {
415-
return isMinMaxType(static_cast<SCEVTypes>(S->getSCEVType()));
415+
return isMinMaxType(S->getSCEVType());
416416
}
417417

418418
static enum SCEVTypes negate(enum SCEVTypes T) {
@@ -567,9 +567,8 @@ class Type;
567567
return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
568568
case scCouldNotCompute:
569569
return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
570-
default:
571-
llvm_unreachable("Unknown SCEV type!");
572570
}
571+
llvm_unreachable("Unknown SCEV kind!");
573572
}
574573

575574
RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
@@ -606,12 +605,12 @@ class Type;
606605
switch (S->getSCEVType()) {
607606
case scConstant:
608607
case scUnknown:
609-
break;
608+
continue;
610609
case scTruncate:
611610
case scZeroExtend:
612611
case scSignExtend:
613612
push(cast<SCEVIntegralCastExpr>(S)->getOperand());
614-
break;
613+
continue;
615614
case scAddExpr:
616615
case scMulExpr:
617616
case scSMaxExpr:
@@ -621,18 +620,17 @@ class Type;
621620
case scAddRecExpr:
622621
for (const auto *Op : cast<SCEVNAryExpr>(S)->operands())
623622
push(Op);
624-
break;
623+
continue;
625624
case scUDivExpr: {
626625
const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
627626
push(UDiv->getLHS());
628627
push(UDiv->getRHS());
629-
break;
628+
continue;
630629
}
631630
case scCouldNotCompute:
632631
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
633-
default:
634-
llvm_unreachable("Unknown SCEV kind!");
635632
}
633+
llvm_unreachable("Unknown SCEV kind!");
636634
}
637635
}
638636
};

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ LLVM_DUMP_METHOD void SCEV::dump() const {
243243
#endif
244244

245245
void SCEV::print(raw_ostream &OS) const {
246-
switch (static_cast<SCEVTypes>(getSCEVType())) {
246+
switch (getSCEVType()) {
247247
case scConstant:
248248
cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
249249
return;
@@ -304,6 +304,8 @@ void SCEV::print(raw_ostream &OS) const {
304304
case scSMinExpr:
305305
OpStr = " smin ";
306306
break;
307+
default:
308+
llvm_unreachable("There are no other nary expression types.");
307309
}
308310
OS << "(";
309311
for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
@@ -320,6 +322,10 @@ void SCEV::print(raw_ostream &OS) const {
320322
OS << "<nuw>";
321323
if (NAry->hasNoSignedWrap())
322324
OS << "<nsw>";
325+
break;
326+
default:
327+
// Nothing to print for other nary expressions.
328+
break;
323329
}
324330
return;
325331
}
@@ -361,7 +367,7 @@ void SCEV::print(raw_ostream &OS) const {
361367
}
362368

363369
Type *SCEV::getType() const {
364-
switch (static_cast<SCEVTypes>(getSCEVType())) {
370+
switch (getSCEVType()) {
365371
case scConstant:
366372
return cast<SCEVConstant>(this)->getType();
367373
case scTruncate:
@@ -446,7 +452,7 @@ ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
446452
}
447453

448454
SCEVIntegralCastExpr::SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID,
449-
unsigned SCEVTy, const SCEV *op,
455+
SCEVTypes SCEVTy, const SCEV *op,
450456
Type *ty)
451457
: SCEV(ID, SCEVTy, computeExpressionSize(op)), Ty(ty) {
452458
Operands[0] = op;
@@ -668,7 +674,7 @@ static int CompareSCEVComplexity(
668674
return 0;
669675

670676
// Primarily, sort the SCEVs by their getSCEVType().
671-
unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
677+
SCEVTypes LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
672678
if (LType != RType)
673679
return (int)LType - (int)RType;
674680

@@ -677,7 +683,7 @@ static int CompareSCEVComplexity(
677683
// Aside from the getSCEVType() ordering, the particular ordering
678684
// isn't very important except that it's beneficial to be consistent,
679685
// so that (a + b) and (b + a) don't end up as different expressions.
680-
switch (static_cast<SCEVTypes>(LType)) {
686+
switch (LType) {
681687
case scUnknown: {
682688
const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
683689
const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
@@ -3325,7 +3331,7 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
33253331
}
33263332

33273333
std::tuple<SCEV *, FoldingSetNodeID, void *>
3328-
ScalarEvolution::findExistingSCEVInCache(int SCEVType,
3334+
ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType,
33293335
ArrayRef<const SCEV *> Ops) {
33303336
FoldingSetNodeID ID;
33313337
void *IP = nullptr;
@@ -3346,7 +3352,7 @@ const SCEV *ScalarEvolution::getSignumExpr(const SCEV *Op) {
33463352
return getSMinExpr(getSMaxExpr(Op, getMinusOne(Ty)), getOne(Ty));
33473353
}
33483354

3349-
const SCEV *ScalarEvolution::getMinMaxExpr(unsigned Kind,
3355+
const SCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind,
33503356
SmallVectorImpl<const SCEV *> &Ops) {
33513357
assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!");
33523358
if (Ops.size() == 1) return Ops[0];
@@ -3470,8 +3476,8 @@ const SCEV *ScalarEvolution::getMinMaxExpr(unsigned Kind,
34703476
return ExistingSCEV;
34713477
const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
34723478
std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3473-
SCEV *S = new (SCEVAllocator) SCEVMinMaxExpr(
3474-
ID.Intern(SCEVAllocator), static_cast<SCEVTypes>(Kind), O, Ops.size());
3479+
SCEV *S = new (SCEVAllocator)
3480+
SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
34753481

34763482
UniqueSCEVs.InsertNode(S, IP);
34773483
addToLoopUseLists(S);
@@ -3792,9 +3798,8 @@ const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
37923798
return (const SCEV *)nullptr;
37933799
MatchedOperands.push_back(Matched);
37943800
}
3795-
return getMinMaxExpr(
3796-
SCEVMinMaxExpr::negate(static_cast<SCEVTypes>(MME->getSCEVType())),
3797-
MatchedOperands);
3801+
return getMinMaxExpr(SCEVMinMaxExpr::negate(MME->getSCEVType()),
3802+
MatchedOperands);
37983803
};
37993804
if (const SCEV *Replaced = MatchMinMaxNegation(MME))
38003805
return Replaced;
@@ -5036,7 +5041,7 @@ static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S,
50365041
// We do not try to smart about these at all.
50375042
return setUnavailable();
50385043
}
5039-
llvm_unreachable("switch should be fully covered!");
5044+
llvm_unreachable("Unknown SCEV kind!");
50405045
}
50415046

50425047
bool isDone() { return TraversalDone; }
@@ -7970,10 +7975,10 @@ const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
79707975
/// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
79717976
/// Returns NULL if the SCEV isn't representable as a Constant.
79727977
static Constant *BuildConstantFromSCEV(const SCEV *V) {
7973-
switch (static_cast<SCEVTypes>(V->getSCEVType())) {
7978+
switch (V->getSCEVType()) {
79747979
case scCouldNotCompute:
79757980
case scAddRecExpr:
7976-
break;
7981+
return nullptr;
79777982
case scConstant:
79787983
return cast<SCEVConstant>(V)->getValue();
79797984
case scUnknown:
@@ -7982,19 +7987,19 @@ static Constant *BuildConstantFromSCEV(const SCEV *V) {
79827987
const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
79837988
if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
79847989
return ConstantExpr::getSExt(CastOp, SS->getType());
7985-
break;
7990+
return nullptr;
79867991
}
79877992
case scZeroExtend: {
79887993
const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
79897994
if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
79907995
return ConstantExpr::getZExt(CastOp, SZ->getType());
7991-
break;
7996+
return nullptr;
79927997
}
79937998
case scTruncate: {
79947999
const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
79958000
if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
79968001
return ConstantExpr::getTrunc(CastOp, ST->getType());
7997-
break;
8002+
return nullptr;
79988003
}
79998004
case scAddExpr: {
80008005
const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
@@ -8034,7 +8039,7 @@ static Constant *BuildConstantFromSCEV(const SCEV *V) {
80348039
}
80358040
return C;
80368041
}
8037-
break;
8042+
return nullptr;
80388043
}
80398044
case scMulExpr: {
80408045
const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
@@ -8050,23 +8055,23 @@ static Constant *BuildConstantFromSCEV(const SCEV *V) {
80508055
}
80518056
return C;
80528057
}
8053-
break;
8058+
return nullptr;
80548059
}
80558060
case scUDivExpr: {
80568061
const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
80578062
if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
80588063
if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
80598064
if (LHS->getType() == RHS->getType())
80608065
return ConstantExpr::getUDiv(LHS, RHS);
8061-
break;
8066+
return nullptr;
80628067
}
80638068
case scSMaxExpr:
80648069
case scUMaxExpr:
80658070
case scSMinExpr:
80668071
case scUMinExpr:
8067-
break; // TODO: smax, umax, smin, umax.
8072+
return nullptr; // TODO: smax, umax, smin, umax.
80688073
}
8069-
return nullptr;
8074+
llvm_unreachable("Unknown SCEV kind!");
80708075
}
80718076

80728077
const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
@@ -11794,7 +11799,7 @@ ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) {
1179411799

1179511800
ScalarEvolution::LoopDisposition
1179611801
ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) {
11797-
switch (static_cast<SCEVTypes>(S->getSCEVType())) {
11802+
switch (S->getSCEVType()) {
1179811803
case scConstant:
1179911804
return LoopInvariant;
1180011805
case scTruncate:
@@ -11901,7 +11906,7 @@ ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) {
1190111906

1190211907
ScalarEvolution::BlockDisposition
1190311908
ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
11904-
switch (static_cast<SCEVTypes>(S->getSCEVType())) {
11909+
switch (S->getSCEVType()) {
1190511910
case scConstant:
1190611911
return ProperlyDominatesBlock;
1190711912
case scTruncate:

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@ static bool isHighCostExpansion(const SCEV *S,
937937
case scSignExtend:
938938
return isHighCostExpansion(cast<SCEVSignExtendExpr>(S)->getOperand(),
939939
Processed, SE);
940+
default:
941+
break;
940942
}
941943

942944
if (!Processed.insert(S).second)
@@ -2788,6 +2790,7 @@ static const SCEV *getExprBase(const SCEV *S) {
27882790
case scAddRecExpr:
27892791
return getExprBase(cast<SCEVAddRecExpr>(S)->getStart());
27902792
}
2793+
llvm_unreachable("Unknown SCEV kind!");
27912794
}
27922795

27932796
/// Return true if the chain increment is profitable to expand into a loop

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,8 +2236,8 @@ template<typename T> static int costAndCollectOperands(
22362236
};
22372237

22382238
switch (S->getSCEVType()) {
2239-
default:
2240-
llvm_unreachable("No other scev expressions possible.");
2239+
case scCouldNotCompute:
2240+
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
22412241
case scUnknown:
22422242
case scConstant:
22432243
return 0;
@@ -2351,6 +2351,8 @@ bool SCEVExpander::isHighCostExpansionHelper(
23512351
: TargetTransformInfo::TCK_RecipThroughput;
23522352

23532353
switch (S->getSCEVType()) {
2354+
case scCouldNotCompute:
2355+
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
23542356
case scUnknown:
23552357
// Assume to be zero-cost.
23562358
return false;
@@ -2416,7 +2418,7 @@ bool SCEVExpander::isHighCostExpansionHelper(
24162418
return BudgetRemaining < 0;
24172419
}
24182420
}
2419-
llvm_unreachable("Switch is exaustive and we return in all of them.");
2421+
llvm_unreachable("Unknown SCEV kind!");
24202422
}
24212423

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

0 commit comments

Comments
 (0)