Skip to content

Commit 4577860

Browse files
committed
[SCEVExpander] Clarify absence of no-op casts (NFC)
Remove all the expandCodeFor() uses that specify an explicit type, as well as InsertNoopCastOfTo() calls and most uses of getEffectiveSCEVType(). The only place where no-op casts can now be inserted are public expandCodeFor() uses.
1 parent 058222b commit 4577860

File tree

2 files changed

+34
-48
lines changed

2 files changed

+34
-48
lines changed

llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
434434
SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts);
435435

436436
Value *expand(const SCEV *S);
437+
Value *expand(const SCEV *S, BasicBlock::iterator I) {
438+
setInsertPoint(I);
439+
return expand(S);
440+
}
441+
Value *expand(const SCEV *S, Instruction *I) {
442+
setInsertPoint(I);
443+
return expand(S);
444+
}
437445

438446
/// Determine the most "relevant" loop for the given SCEV.
439447
const Loop *getRelevantLoop(const SCEV *);
@@ -481,8 +489,8 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
481489

482490
Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
483491
PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
484-
const Loop *L, Type *ExpandTy, Type *IntTy,
485-
Type *&TruncTy, bool &InvertStep);
492+
const Loop *L, Type *&TruncTy,
493+
bool &InvertStep);
486494
Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
487495
bool useSubtract);
488496

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -815,12 +815,11 @@ static bool canBeCheaplyTransformed(ScalarEvolution &SE,
815815
const SCEVAddRecExpr *Requested,
816816
bool &InvertStep) {
817817
// We can't transform to match a pointer PHI.
818-
if (Phi->getType()->isPointerTy())
818+
Type *PhiTy = Phi->getType();
819+
Type *RequestedTy = Requested->getType();
820+
if (PhiTy->isPointerTy() || RequestedTy->isPointerTy())
819821
return false;
820822

821-
Type *PhiTy = SE.getEffectiveSCEVType(Phi->getType());
822-
Type *RequestedTy = SE.getEffectiveSCEVType(Requested->getType());
823-
824823
if (RequestedTy->getIntegerBitWidth() > PhiTy->getIntegerBitWidth())
825824
return false;
826825

@@ -877,12 +876,10 @@ static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) {
877876
/// values, and return the PHI.
878877
PHINode *
879878
SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
880-
const Loop *L,
881-
Type *ExpandTy,
882-
Type *IntTy,
883-
Type *&TruncTy,
879+
const Loop *L, Type *&TruncTy,
884880
bool &InvertStep) {
885-
assert((!IVIncInsertLoop||IVIncInsertPos) && "Uninitialized insert position");
881+
assert((!IVIncInsertLoop || IVIncInsertPos) &&
882+
"Uninitialized insert position");
886883

887884
// Reuse a previously-inserted PHI, if present.
888885
BasicBlock *LatchBlock = L->getLoopLatch();
@@ -953,7 +950,7 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
953950
// later.
954951
AddRecPhiMatch = &PN;
955952
IncV = TempIncV;
956-
TruncTy = SE.getEffectiveSCEVType(Normalized->getType());
953+
TruncTy = Normalized->getType();
957954
}
958955
}
959956

@@ -986,8 +983,8 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
986983
// Expand code for the start value into the loop preheader.
987984
assert(L->getLoopPreheader() &&
988985
"Can't expand add recurrences without a loop preheader!");
989-
Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy,
990-
L->getLoopPreheader()->getTerminator());
986+
Value *StartV =
987+
expand(Normalized->getStart(), L->getLoopPreheader()->getTerminator());
991988

992989
// StartV must have been be inserted into L's preheader to dominate the new
993990
// phi.
@@ -998,15 +995,15 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
998995
// Expand code for the step value. Do this before creating the PHI so that PHI
999996
// reuse code doesn't see an incomplete PHI.
1000997
const SCEV *Step = Normalized->getStepRecurrence(SE);
998+
Type *ExpandTy = Normalized->getType();
1001999
// If the stride is negative, insert a sub instead of an add for the increment
10021000
// (unless it's a constant, because subtracts of constants are canonicalized
10031001
// to adds).
10041002
bool useSubtract = !ExpandTy->isPointerTy() && Step->isNonConstantNegative();
10051003
if (useSubtract)
10061004
Step = SE.getNegativeSCEV(Step);
10071005
// Expand the step somewhere that dominates the loop header.
1008-
Value *StepV =
1009-
expandCodeFor(Step, IntTy, L->getHeader()->getFirstInsertionPt());
1006+
Value *StepV = expand(Step, L->getHeader()->getFirstInsertionPt());
10101007

10111008
// The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if
10121009
// we actually do emit an addition. It does not apply if we emit a
@@ -1060,8 +1057,6 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
10601057
}
10611058

10621059
Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
1063-
Type *STy = S->getType();
1064-
Type *IntTy = SE.getEffectiveSCEVType(STy);
10651060
const Loop *L = S->getLoop();
10661061

10671062
// Determine a normalized form of this expression, which is the expression
@@ -1080,19 +1075,11 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
10801075
"Start does not properly dominate loop header");
10811076
assert(SE.dominates(Step, L->getHeader()) && "Step not dominate loop header");
10821077

1083-
// Expand the core addrec.
1084-
Type *ExpandTy = STy;
1085-
// We can't use a pointer type for the addrec if the pointer type is
1086-
// non-integral.
1087-
Type *AddRecPHIExpandTy =
1088-
DL.isNonIntegralPointerType(STy) ? Normalized->getType() : ExpandTy;
1089-
10901078
// In some cases, we decide to reuse an existing phi node but need to truncate
10911079
// it and/or invert the step.
10921080
Type *TruncTy = nullptr;
10931081
bool InvertStep = false;
1094-
PHINode *PN = getAddRecExprPHILiterally(Normalized, L, AddRecPHIExpandTy,
1095-
IntTy, TruncTy, InvertStep);
1082+
PHINode *PN = getAddRecExprPHILiterally(Normalized, L, TruncTy, InvertStep);
10961083

10971084
// Accommodate post-inc mode, if necessary.
10981085
Value *Result;
@@ -1131,15 +1118,14 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
11311118
// inserting an extra IV increment. StepV might fold into PostLoopOffset,
11321119
// but hopefully expandCodeFor handles that.
11331120
bool useSubtract =
1134-
!ExpandTy->isPointerTy() && Step->isNonConstantNegative();
1121+
!S->getType()->isPointerTy() && Step->isNonConstantNegative();
11351122
if (useSubtract)
11361123
Step = SE.getNegativeSCEV(Step);
11371124
Value *StepV;
11381125
{
11391126
// Expand the step somewhere that dominates the loop header.
11401127
SCEVInsertPointGuard Guard(Builder, this);
1141-
StepV =
1142-
expandCodeFor(Step, IntTy, L->getHeader()->getFirstInsertionPt());
1128+
StepV = expand(Step, L->getHeader()->getFirstInsertionPt());
11431129
}
11441130
Result = expandIVInc(PN, StepV, L, useSubtract);
11451131
}
@@ -1148,18 +1134,13 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
11481134
// We have decided to reuse an induction variable of a dominating loop. Apply
11491135
// truncation and/or inversion of the step.
11501136
if (TruncTy) {
1151-
Type *ResTy = Result->getType();
1152-
// Normalize the result type.
1153-
if (ResTy != SE.getEffectiveSCEVType(ResTy))
1154-
Result = InsertNoopCastOfTo(Result, SE.getEffectiveSCEVType(ResTy));
11551137
// Truncate the result.
11561138
if (TruncTy != Result->getType())
11571139
Result = Builder.CreateTrunc(Result, TruncTy);
11581140

11591141
// Invert the result.
11601142
if (InvertStep)
1161-
Result = Builder.CreateSub(expandCodeFor(Normalized->getStart(), TruncTy),
1162-
Result);
1143+
Result = Builder.CreateSub(expand(Normalized->getStart()), Result);
11631144
}
11641145

11651146
return Result;
@@ -1200,8 +1181,7 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
12001181
S->getNoWrapFlags(SCEV::FlagNW)));
12011182
BasicBlock::iterator NewInsertPt =
12021183
findInsertPointAfter(cast<Instruction>(V), &*Builder.GetInsertPoint());
1203-
V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr,
1204-
NewInsertPt);
1184+
V = expand(SE.getTruncateExpr(SE.getUnknown(V), Ty), NewInsertPt);
12051185
return V;
12061186
}
12071187

@@ -1329,7 +1309,7 @@ Value *SCEVExpander::expandMinMaxExpr(const SCEVNAryExpr *S,
13291309
if (IsSequential)
13301310
LHS = Builder.CreateFreeze(LHS);
13311311
for (int i = S->getNumOperands() - 2; i >= 0; --i) {
1332-
Value *RHS = expandCodeFor(S->getOperand(i), Ty);
1312+
Value *RHS = expand(S->getOperand(i));
13331313
if (IsSequential && i != 0)
13341314
RHS = Builder.CreateFreeze(RHS);
13351315
Value *Sel;
@@ -2034,8 +2014,8 @@ Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred,
20342014

20352015
Value *SCEVExpander::expandComparePredicate(const SCEVComparePredicate *Pred,
20362016
Instruction *IP) {
2037-
Value *Expr0 = expandCodeFor(Pred->getLHS(), Pred->getLHS()->getType(), IP);
2038-
Value *Expr1 = expandCodeFor(Pred->getRHS(), Pred->getRHS()->getType(), IP);
2017+
Value *Expr0 = expand(Pred->getLHS(), IP);
2018+
Value *Expr1 = expand(Pred->getRHS(), IP);
20392019

20402020
Builder.SetInsertPoint(IP);
20412021
auto InvPred = ICmpInst::getInversePredicate(Pred->getPredicate());
@@ -2067,16 +2047,15 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
20672047
// Step >= 0, Start + |Step| * Backedge > Start
20682048
// and |Step| * Backedge doesn't unsigned overflow.
20692049

2070-
IntegerType *CountTy = IntegerType::get(Loc->getContext(), SrcBits);
20712050
Builder.SetInsertPoint(Loc);
2072-
Value *TripCountVal = expandCodeFor(ExitCount, CountTy, Loc);
2051+
Value *TripCountVal = expand(ExitCount, Loc);
20732052

20742053
IntegerType *Ty =
20752054
IntegerType::get(Loc->getContext(), SE.getTypeSizeInBits(ARTy));
20762055

2077-
Value *StepValue = expandCodeFor(Step, Ty, Loc);
2078-
Value *NegStepValue = expandCodeFor(SE.getNegativeSCEV(Step), Ty, Loc);
2079-
Value *StartValue = expandCodeFor(Start, ARTy, Loc);
2056+
Value *StepValue = expand(Step, Loc);
2057+
Value *NegStepValue = expand(SE.getNegativeSCEV(Step), Loc);
2058+
Value *StartValue = expand(Start, Loc);
20802059

20812060
ConstantInt *Zero =
20822061
ConstantInt::get(Loc->getContext(), APInt::getZero(DstBits));
@@ -2123,7 +2102,6 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
21232102
bool NeedNegCheck = !SE.isKnownPositive(Step);
21242103

21252104
if (PointerType *ARPtrTy = dyn_cast<PointerType>(ARTy)) {
2126-
StartValue = InsertNoopCastOfTo(StartValue, ARPtrTy);
21272105
Value *NegMulV = Builder.CreateNeg(MulV);
21282106
if (NeedPosCheck)
21292107
Add = Builder.CreateGEP(Builder.getInt8Ty(), StartValue, MulV);
@@ -2156,7 +2134,7 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
21562134
// If the backedge taken count type is larger than the AR type,
21572135
// check that we don't drop any bits by truncating it. If we are
21582136
// dropping bits, then we have overflow (unless the step is zero).
2159-
if (SE.getTypeSizeInBits(CountTy) > SE.getTypeSizeInBits(Ty)) {
2137+
if (SrcBits > DstBits) {
21602138
auto MaxVal = APInt::getMaxValue(DstBits).zext(SrcBits);
21612139
auto *BackedgeCheck =
21622140
Builder.CreateICmp(ICmpInst::ICMP_UGT, TripCountVal,

0 commit comments

Comments
 (0)