@@ -262,8 +262,8 @@ class Scorer
262
262
class Reducer
263
263
{
264
264
public:
265
- Reducer (const DataLayout &DL, DominatorTree &DT, Loop &L, LoopInfo &LI, ModuleMetaData &MMD, RegisterPressureEstimate &RPE, ScalarEvolution &SE, unsigned MaxPressure, bool AllowLICM)
266
- : DT(DT), L(L), LI(LI), RPE(RPE), SE(SE), E(SE, DL, " gep-loop-strength-reduction " ), MaxPressure(MaxPressure), AllowLICM(AllowLICM), Scorer(DL, L, MMD) {}
265
+ Reducer (const DataLayout &DL, DominatorTree &DT, Loop &L, LoopInfo &LI, ModuleMetaData &MMD, RegisterPressureEstimate &RPE, ScalarEvolution &SE, SCEVExpander &E, unsigned MaxPressure, bool AllowLICM)
266
+ : DT(DT), L(L), LI(LI), RPE(RPE), SE(SE), E(E ), MaxPressure(MaxPressure), AllowLICM(AllowLICM), Scorer(DL, L, MMD) {}
267
267
268
268
bool reduce ();
269
269
@@ -280,7 +280,7 @@ class Reducer
280
280
LoopInfo &LI;
281
281
RegisterPressureEstimate &RPE;
282
282
ScalarEvolution &SE;
283
- SCEVExpander E;
283
+ SCEVExpander & E;
284
284
285
285
Scorer Scorer;
286
286
@@ -305,22 +305,26 @@ namespace SCEVHelper
305
305
class SCEVAddBuilder
306
306
{
307
307
public:
308
- SCEVAddBuilder (ScalarEvolution &SE, Type *T) :
309
- SE (SE), T(T)
310
- {
311
- IGC_ASSERT_MESSAGE (T->isIntegerTy (), " builder requires integer type" );
312
- }
308
+ SCEVAddBuilder (ScalarEvolution &SE) : SE(SE) {}
313
309
314
310
SCEVAddBuilder &add (const SCEV *S, bool Negative = false );
315
311
316
312
SCEVAddBuilder &addNegative (const SCEV *S) { return add (S, true ); }
317
313
318
- const SCEV *build () { return SE. getAddExpr (Ops); }
314
+ const SCEV *build ();
319
315
320
316
private:
317
+
318
+ struct Op
319
+ {
320
+ Op (const SCEV *S, bool Negative) : S(S), Negative(Negative) {}
321
+
322
+ const SCEV *S;
323
+ bool Negative;
324
+ };
325
+
321
326
ScalarEvolution &SE;
322
- Type *T;
323
- SmallVector<const SCEV*, 16 > Ops;
327
+ SmallVector<Op, 16 > Ops;
324
328
};
325
329
};
326
330
@@ -406,7 +410,7 @@ bool ReductionCandidateGroup::addToGroup(ScalarEvolution &SE, GetElementPtrInst
406
410
// Can't use ScalarEvolution::computeConstantDifference, as it only
407
411
// supports SCEVAddExpr with two operands. Calculate difference as:
408
412
// new candidate's operands + (-1 * base's operands)
409
- SCEVHelper::SCEVAddBuilder Builder (SE, SE. getWiderType (S-> getType (), Base. S -> getType ()) );
413
+ SCEVHelper::SCEVAddBuilder Builder (SE);
410
414
const SCEVConstant *Sum = dyn_cast<SCEVConstant>(Builder.add (S).addNegative (Base.S ).build ());
411
415
if (!Sum)
412
416
return false ;
@@ -900,7 +904,7 @@ bool Reducer::deconstructSCEV(const SCEV *S, const SCEV *&Start, int64_t &Step)
900
904
901
905
const SCEV *OpSCEV = nullptr ;
902
906
int64_t OpStep = 0 ;
903
- SCEVHelper::SCEVAddBuilder Builder (SE, S-> getType () );
907
+ SCEVHelper::SCEVAddBuilder Builder (SE);
904
908
905
909
for (auto *Op : Add->operands ())
906
910
{
@@ -1036,16 +1040,33 @@ SCEVHelper::SCEVAddBuilder &SCEVHelper::SCEVAddBuilder::add(const SCEV *S, bool
1036
1040
return *this ;
1037
1041
}
1038
1042
1043
+ Ops.emplace_back (S, Negative);
1044
+
1045
+ return *this ;
1046
+ }
1047
+
1048
+
1049
+ const SCEV *SCEVHelper::SCEVAddBuilder::build ()
1050
+ {
1039
1051
// ScalarEvolution::getAddExpr requires all operands to have the same
1040
- // type. Extend type if required.
1041
- S = S->getType () == T ? S : SE.getSignExtendExpr (S, T);
1052
+ // type. First find the widest type.
1053
+ Type *T = nullptr ;
1054
+ for (auto *It = Ops.begin (); It != Ops.end (); ++It)
1055
+ {
1056
+ T = T ? SE.getWiderType (T, It->S ->getType ()) : It->S ->getType ();
1057
+ }
1042
1058
1043
- // Change expresion to "-1 * expression"
1044
- S = Negative ? SE. getNegativeSCEV (S) : S ;
1059
+ // Join list of operands, extending type if required.
1060
+ SmallVector< const SCEV*, 16 > FinalOps ;
1045
1061
1046
- Ops.push_back (S);
1062
+ for (auto *It = Ops.begin (); It != Ops.end (); ++It)
1063
+ {
1064
+ const SCEV *S = It->S ;
1065
+ S = S->getType () == T ? S : SE.getSignExtendExpr (S, T);
1066
+ FinalOps.push_back (It->Negative ? SE.getNegativeSCEV (S) : S);
1067
+ }
1047
1068
1048
- return * this ;
1069
+ return SE. getAddExpr (FinalOps) ;
1049
1070
}
1050
1071
1051
1072
@@ -1093,9 +1114,12 @@ bool GEPLoopStrengthReduction::runOnFunction(llvm::Function &F)
1093
1114
1094
1115
bool changed = false ;
1095
1116
1117
+ // Using one SCEV expander between all reductions reduces number of duplicated new instructions.
1118
+ auto E = SCEVExpander (SE, DL, " gep-loop-strength-reduction" );
1119
+
1096
1120
for (Loop *L : LI.getLoopsInPreorder ())
1097
1121
{
1098
- changed |= Reducer (DL, DT, *L, LI, MMD, RPE, SE, MaxPressure, AllowLICM).reduce ();
1122
+ changed |= Reducer (DL, DT, *L, LI, MMD, RPE, SE, E, MaxPressure, AllowLICM).reduce ();
1099
1123
}
1100
1124
1101
1125
return changed;
0 commit comments