@@ -815,12 +815,11 @@ static bool canBeCheaplyTransformed(ScalarEvolution &SE,
815
815
const SCEVAddRecExpr *Requested,
816
816
bool &InvertStep) {
817
817
// 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 ())
819
821
return false ;
820
822
821
- Type *PhiTy = SE.getEffectiveSCEVType (Phi->getType ());
822
- Type *RequestedTy = SE.getEffectiveSCEVType (Requested->getType ());
823
-
824
823
if (RequestedTy->getIntegerBitWidth () > PhiTy->getIntegerBitWidth ())
825
824
return false ;
826
825
@@ -877,12 +876,10 @@ static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) {
877
876
// / values, and return the PHI.
878
877
PHINode *
879
878
SCEVExpander::getAddRecExprPHILiterally (const SCEVAddRecExpr *Normalized,
880
- const Loop *L,
881
- Type *ExpandTy,
882
- Type *IntTy,
883
- Type *&TruncTy,
879
+ const Loop *L, Type *&TruncTy,
884
880
bool &InvertStep) {
885
- assert ((!IVIncInsertLoop||IVIncInsertPos) && " Uninitialized insert position" );
881
+ assert ((!IVIncInsertLoop || IVIncInsertPos) &&
882
+ " Uninitialized insert position" );
886
883
887
884
// Reuse a previously-inserted PHI, if present.
888
885
BasicBlock *LatchBlock = L->getLoopLatch ();
@@ -953,7 +950,7 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
953
950
// later.
954
951
AddRecPhiMatch = &PN;
955
952
IncV = TempIncV;
956
- TruncTy = SE. getEffectiveSCEVType ( Normalized->getType () );
953
+ TruncTy = Normalized->getType ();
957
954
}
958
955
}
959
956
@@ -986,8 +983,8 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
986
983
// Expand code for the start value into the loop preheader.
987
984
assert (L->getLoopPreheader () &&
988
985
" 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 ());
991
988
992
989
// StartV must have been be inserted into L's preheader to dominate the new
993
990
// phi.
@@ -998,15 +995,15 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
998
995
// Expand code for the step value. Do this before creating the PHI so that PHI
999
996
// reuse code doesn't see an incomplete PHI.
1000
997
const SCEV *Step = Normalized->getStepRecurrence (SE);
998
+ Type *ExpandTy = Normalized->getType ();
1001
999
// If the stride is negative, insert a sub instead of an add for the increment
1002
1000
// (unless it's a constant, because subtracts of constants are canonicalized
1003
1001
// to adds).
1004
1002
bool useSubtract = !ExpandTy->isPointerTy () && Step->isNonConstantNegative ();
1005
1003
if (useSubtract)
1006
1004
Step = SE.getNegativeSCEV (Step);
1007
1005
// 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 ());
1010
1007
1011
1008
// The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if
1012
1009
// we actually do emit an addition. It does not apply if we emit a
@@ -1060,8 +1057,6 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
1060
1057
}
1061
1058
1062
1059
Value *SCEVExpander::expandAddRecExprLiterally (const SCEVAddRecExpr *S) {
1063
- Type *STy = S->getType ();
1064
- Type *IntTy = SE.getEffectiveSCEVType (STy);
1065
1060
const Loop *L = S->getLoop ();
1066
1061
1067
1062
// Determine a normalized form of this expression, which is the expression
@@ -1080,19 +1075,11 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
1080
1075
" Start does not properly dominate loop header" );
1081
1076
assert (SE.dominates (Step, L->getHeader ()) && " Step not dominate loop header" );
1082
1077
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
-
1090
1078
// In some cases, we decide to reuse an existing phi node but need to truncate
1091
1079
// it and/or invert the step.
1092
1080
Type *TruncTy = nullptr ;
1093
1081
bool InvertStep = false ;
1094
- PHINode *PN = getAddRecExprPHILiterally (Normalized, L, AddRecPHIExpandTy,
1095
- IntTy, TruncTy, InvertStep);
1082
+ PHINode *PN = getAddRecExprPHILiterally (Normalized, L, TruncTy, InvertStep);
1096
1083
1097
1084
// Accommodate post-inc mode, if necessary.
1098
1085
Value *Result;
@@ -1131,15 +1118,14 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
1131
1118
// inserting an extra IV increment. StepV might fold into PostLoopOffset,
1132
1119
// but hopefully expandCodeFor handles that.
1133
1120
bool useSubtract =
1134
- !ExpandTy ->isPointerTy () && Step->isNonConstantNegative ();
1121
+ !S-> getType () ->isPointerTy () && Step->isNonConstantNegative ();
1135
1122
if (useSubtract)
1136
1123
Step = SE.getNegativeSCEV (Step);
1137
1124
Value *StepV;
1138
1125
{
1139
1126
// Expand the step somewhere that dominates the loop header.
1140
1127
SCEVInsertPointGuard Guard (Builder, this );
1141
- StepV =
1142
- expandCodeFor (Step, IntTy, L->getHeader ()->getFirstInsertionPt ());
1128
+ StepV = expand (Step, L->getHeader ()->getFirstInsertionPt ());
1143
1129
}
1144
1130
Result = expandIVInc (PN, StepV, L, useSubtract);
1145
1131
}
@@ -1148,18 +1134,13 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
1148
1134
// We have decided to reuse an induction variable of a dominating loop. Apply
1149
1135
// truncation and/or inversion of the step.
1150
1136
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));
1155
1137
// Truncate the result.
1156
1138
if (TruncTy != Result->getType ())
1157
1139
Result = Builder.CreateTrunc (Result, TruncTy);
1158
1140
1159
1141
// Invert the result.
1160
1142
if (InvertStep)
1161
- Result = Builder.CreateSub (expandCodeFor (Normalized->getStart (), TruncTy),
1162
- Result);
1143
+ Result = Builder.CreateSub (expand (Normalized->getStart ()), Result);
1163
1144
}
1164
1145
1165
1146
return Result;
@@ -1200,8 +1181,7 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
1200
1181
S->getNoWrapFlags (SCEV::FlagNW)));
1201
1182
BasicBlock::iterator NewInsertPt =
1202
1183
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);
1205
1185
return V;
1206
1186
}
1207
1187
@@ -1329,7 +1309,7 @@ Value *SCEVExpander::expandMinMaxExpr(const SCEVNAryExpr *S,
1329
1309
if (IsSequential)
1330
1310
LHS = Builder.CreateFreeze (LHS);
1331
1311
for (int i = S->getNumOperands () - 2 ; i >= 0 ; --i) {
1332
- Value *RHS = expandCodeFor (S->getOperand (i), Ty );
1312
+ Value *RHS = expand (S->getOperand (i));
1333
1313
if (IsSequential && i != 0 )
1334
1314
RHS = Builder.CreateFreeze (RHS);
1335
1315
Value *Sel;
@@ -2034,8 +2014,8 @@ Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred,
2034
2014
2035
2015
Value *SCEVExpander::expandComparePredicate (const SCEVComparePredicate *Pred,
2036
2016
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);
2039
2019
2040
2020
Builder.SetInsertPoint (IP);
2041
2021
auto InvPred = ICmpInst::getInversePredicate (Pred->getPredicate ());
@@ -2067,16 +2047,15 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
2067
2047
// Step >= 0, Start + |Step| * Backedge > Start
2068
2048
// and |Step| * Backedge doesn't unsigned overflow.
2069
2049
2070
- IntegerType *CountTy = IntegerType::get (Loc->getContext (), SrcBits);
2071
2050
Builder.SetInsertPoint (Loc);
2072
- Value *TripCountVal = expandCodeFor (ExitCount, CountTy , Loc);
2051
+ Value *TripCountVal = expand (ExitCount, Loc);
2073
2052
2074
2053
IntegerType *Ty =
2075
2054
IntegerType::get (Loc->getContext (), SE.getTypeSizeInBits (ARTy));
2076
2055
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);
2080
2059
2081
2060
ConstantInt *Zero =
2082
2061
ConstantInt::get (Loc->getContext (), APInt::getZero (DstBits));
@@ -2123,7 +2102,6 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
2123
2102
bool NeedNegCheck = !SE.isKnownPositive (Step);
2124
2103
2125
2104
if (PointerType *ARPtrTy = dyn_cast<PointerType>(ARTy)) {
2126
- StartValue = InsertNoopCastOfTo (StartValue, ARPtrTy);
2127
2105
Value *NegMulV = Builder.CreateNeg (MulV);
2128
2106
if (NeedPosCheck)
2129
2107
Add = Builder.CreateGEP (Builder.getInt8Ty (), StartValue, MulV);
@@ -2156,7 +2134,7 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
2156
2134
// If the backedge taken count type is larger than the AR type,
2157
2135
// check that we don't drop any bits by truncating it. If we are
2158
2136
// dropping bits, then we have overflow (unless the step is zero).
2159
- if (SE. getTypeSizeInBits (CountTy) > SE. getTypeSizeInBits (Ty) ) {
2137
+ if (SrcBits > DstBits ) {
2160
2138
auto MaxVal = APInt::getMaxValue (DstBits).zext (SrcBits);
2161
2139
auto *BackedgeCheck =
2162
2140
Builder.CreateICmp (ICmpInst::ICMP_UGT, TripCountVal,
0 commit comments