annotate polly/lib/Support/SCEVValidator.cpp @ 227:21e6aa2e49ef

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 19 Jul 2021 06:57:16 +0900
parents 2e18cbf3894f
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1
anatofuz
parents:
diff changeset
2 #include "polly/Support/SCEVValidator.h"
anatofuz
parents:
diff changeset
3 #include "polly/ScopDetection.h"
anatofuz
parents:
diff changeset
4 #include "llvm/Analysis/RegionInfo.h"
anatofuz
parents:
diff changeset
5 #include "llvm/Analysis/ScalarEvolution.h"
anatofuz
parents:
diff changeset
6 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
anatofuz
parents:
diff changeset
7 #include "llvm/Support/Debug.h"
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 using namespace llvm;
anatofuz
parents:
diff changeset
10 using namespace polly;
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 #define DEBUG_TYPE "polly-scev-validator"
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 namespace SCEVType {
anatofuz
parents:
diff changeset
15 /// The type of a SCEV
anatofuz
parents:
diff changeset
16 ///
anatofuz
parents:
diff changeset
17 /// To check for the validity of a SCEV we assign to each SCEV a type. The
anatofuz
parents:
diff changeset
18 /// possible types are INT, PARAM, IV and INVALID. The order of the types is
anatofuz
parents:
diff changeset
19 /// important. The subexpressions of SCEV with a type X can only have a type
anatofuz
parents:
diff changeset
20 /// that is smaller or equal than X.
anatofuz
parents:
diff changeset
21 enum TYPE {
anatofuz
parents:
diff changeset
22 // An integer value.
anatofuz
parents:
diff changeset
23 INT,
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 // An expression that is constant during the execution of the Scop,
anatofuz
parents:
diff changeset
26 // but that may depend on parameters unknown at compile time.
anatofuz
parents:
diff changeset
27 PARAM,
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 // An expression that may change during the execution of the SCoP.
anatofuz
parents:
diff changeset
30 IV,
anatofuz
parents:
diff changeset
31
anatofuz
parents:
diff changeset
32 // An invalid expression.
anatofuz
parents:
diff changeset
33 INVALID
anatofuz
parents:
diff changeset
34 };
anatofuz
parents:
diff changeset
35 } // namespace SCEVType
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 /// The result the validator returns for a SCEV expression.
anatofuz
parents:
diff changeset
38 class ValidatorResult {
anatofuz
parents:
diff changeset
39 /// The type of the expression
anatofuz
parents:
diff changeset
40 SCEVType::TYPE Type;
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 /// The set of Parameters in the expression.
anatofuz
parents:
diff changeset
43 ParameterSetTy Parameters;
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 public:
anatofuz
parents:
diff changeset
46 /// The copy constructor
anatofuz
parents:
diff changeset
47 ValidatorResult(const ValidatorResult &Source) {
anatofuz
parents:
diff changeset
48 Type = Source.Type;
anatofuz
parents:
diff changeset
49 Parameters = Source.Parameters;
anatofuz
parents:
diff changeset
50 }
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 /// Construct a result with a certain type and no parameters.
anatofuz
parents:
diff changeset
53 ValidatorResult(SCEVType::TYPE Type) : Type(Type) {
anatofuz
parents:
diff changeset
54 assert(Type != SCEVType::PARAM && "Did you forget to pass the parameter");
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 /// Construct a result with a certain type and a single parameter.
anatofuz
parents:
diff changeset
58 ValidatorResult(SCEVType::TYPE Type, const SCEV *Expr) : Type(Type) {
anatofuz
parents:
diff changeset
59 Parameters.insert(Expr);
anatofuz
parents:
diff changeset
60 }
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 /// Get the type of the ValidatorResult.
anatofuz
parents:
diff changeset
63 SCEVType::TYPE getType() { return Type; }
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 /// Is the analyzed SCEV constant during the execution of the SCoP.
anatofuz
parents:
diff changeset
66 bool isConstant() { return Type == SCEVType::INT || Type == SCEVType::PARAM; }
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 /// Is the analyzed SCEV valid.
anatofuz
parents:
diff changeset
69 bool isValid() { return Type != SCEVType::INVALID; }
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 /// Is the analyzed SCEV of Type IV.
anatofuz
parents:
diff changeset
72 bool isIV() { return Type == SCEVType::IV; }
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 /// Is the analyzed SCEV of Type INT.
anatofuz
parents:
diff changeset
75 bool isINT() { return Type == SCEVType::INT; }
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 /// Is the analyzed SCEV of Type PARAM.
anatofuz
parents:
diff changeset
78 bool isPARAM() { return Type == SCEVType::PARAM; }
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 /// Get the parameters of this validator result.
anatofuz
parents:
diff changeset
81 const ParameterSetTy &getParameters() { return Parameters; }
anatofuz
parents:
diff changeset
82
anatofuz
parents:
diff changeset
83 /// Add the parameters of Source to this result.
anatofuz
parents:
diff changeset
84 void addParamsFrom(const ValidatorResult &Source) {
anatofuz
parents:
diff changeset
85 Parameters.insert(Source.Parameters.begin(), Source.Parameters.end());
anatofuz
parents:
diff changeset
86 }
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 /// Merge a result.
anatofuz
parents:
diff changeset
89 ///
anatofuz
parents:
diff changeset
90 /// This means to merge the parameters and to set the Type to the most
anatofuz
parents:
diff changeset
91 /// specific Type that matches both.
anatofuz
parents:
diff changeset
92 void merge(const ValidatorResult &ToMerge) {
anatofuz
parents:
diff changeset
93 Type = std::max(Type, ToMerge.Type);
anatofuz
parents:
diff changeset
94 addParamsFrom(ToMerge);
anatofuz
parents:
diff changeset
95 }
anatofuz
parents:
diff changeset
96
anatofuz
parents:
diff changeset
97 void print(raw_ostream &OS) {
anatofuz
parents:
diff changeset
98 switch (Type) {
anatofuz
parents:
diff changeset
99 case SCEVType::INT:
anatofuz
parents:
diff changeset
100 OS << "SCEVType::INT";
anatofuz
parents:
diff changeset
101 break;
anatofuz
parents:
diff changeset
102 case SCEVType::PARAM:
anatofuz
parents:
diff changeset
103 OS << "SCEVType::PARAM";
anatofuz
parents:
diff changeset
104 break;
anatofuz
parents:
diff changeset
105 case SCEVType::IV:
anatofuz
parents:
diff changeset
106 OS << "SCEVType::IV";
anatofuz
parents:
diff changeset
107 break;
anatofuz
parents:
diff changeset
108 case SCEVType::INVALID:
anatofuz
parents:
diff changeset
109 OS << "SCEVType::INVALID";
anatofuz
parents:
diff changeset
110 break;
anatofuz
parents:
diff changeset
111 }
anatofuz
parents:
diff changeset
112 }
anatofuz
parents:
diff changeset
113 };
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 raw_ostream &operator<<(raw_ostream &OS, class ValidatorResult &VR) {
anatofuz
parents:
diff changeset
116 VR.print(OS);
anatofuz
parents:
diff changeset
117 return OS;
anatofuz
parents:
diff changeset
118 }
anatofuz
parents:
diff changeset
119
anatofuz
parents:
diff changeset
120 bool polly::isConstCall(llvm::CallInst *Call) {
anatofuz
parents:
diff changeset
121 if (Call->mayReadOrWriteMemory())
anatofuz
parents:
diff changeset
122 return false;
anatofuz
parents:
diff changeset
123
anatofuz
parents:
diff changeset
124 for (auto &Operand : Call->arg_operands())
anatofuz
parents:
diff changeset
125 if (!isa<ConstantInt>(&Operand))
anatofuz
parents:
diff changeset
126 return false;
anatofuz
parents:
diff changeset
127
anatofuz
parents:
diff changeset
128 return true;
anatofuz
parents:
diff changeset
129 }
anatofuz
parents:
diff changeset
130
anatofuz
parents:
diff changeset
131 /// Check if a SCEV is valid in a SCoP.
anatofuz
parents:
diff changeset
132 struct SCEVValidator
anatofuz
parents:
diff changeset
133 : public SCEVVisitor<SCEVValidator, class ValidatorResult> {
anatofuz
parents:
diff changeset
134 private:
anatofuz
parents:
diff changeset
135 const Region *R;
anatofuz
parents:
diff changeset
136 Loop *Scope;
anatofuz
parents:
diff changeset
137 ScalarEvolution &SE;
anatofuz
parents:
diff changeset
138 InvariantLoadsSetTy *ILS;
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 public:
anatofuz
parents:
diff changeset
141 SCEVValidator(const Region *R, Loop *Scope, ScalarEvolution &SE,
anatofuz
parents:
diff changeset
142 InvariantLoadsSetTy *ILS)
anatofuz
parents:
diff changeset
143 : R(R), Scope(Scope), SE(SE), ILS(ILS) {}
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 class ValidatorResult visitConstant(const SCEVConstant *Constant) {
anatofuz
parents:
diff changeset
146 return ValidatorResult(SCEVType::INT);
anatofuz
parents:
diff changeset
147 }
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 class ValidatorResult visitZeroExtendOrTruncateExpr(const SCEV *Expr,
anatofuz
parents:
diff changeset
150 const SCEV *Operand) {
anatofuz
parents:
diff changeset
151 ValidatorResult Op = visit(Operand);
anatofuz
parents:
diff changeset
152 auto Type = Op.getType();
anatofuz
parents:
diff changeset
153
anatofuz
parents:
diff changeset
154 // If unsigned operations are allowed return the operand, otherwise
anatofuz
parents:
diff changeset
155 // check if we can model the expression without unsigned assumptions.
anatofuz
parents:
diff changeset
156 if (PollyAllowUnsignedOperations || Type == SCEVType::INVALID)
anatofuz
parents:
diff changeset
157 return Op;
anatofuz
parents:
diff changeset
158
anatofuz
parents:
diff changeset
159 if (Type == SCEVType::IV)
anatofuz
parents:
diff changeset
160 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
161 return ValidatorResult(SCEVType::PARAM, Expr);
anatofuz
parents:
diff changeset
162 }
anatofuz
parents:
diff changeset
163
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
164 class ValidatorResult visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
165 return visit(Expr->getOperand());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
166 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
167
150
anatofuz
parents:
diff changeset
168 class ValidatorResult visitTruncateExpr(const SCEVTruncateExpr *Expr) {
anatofuz
parents:
diff changeset
169 return visitZeroExtendOrTruncateExpr(Expr, Expr->getOperand());
anatofuz
parents:
diff changeset
170 }
anatofuz
parents:
diff changeset
171
anatofuz
parents:
diff changeset
172 class ValidatorResult visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
anatofuz
parents:
diff changeset
173 return visitZeroExtendOrTruncateExpr(Expr, Expr->getOperand());
anatofuz
parents:
diff changeset
174 }
anatofuz
parents:
diff changeset
175
anatofuz
parents:
diff changeset
176 class ValidatorResult visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
anatofuz
parents:
diff changeset
177 return visit(Expr->getOperand());
anatofuz
parents:
diff changeset
178 }
anatofuz
parents:
diff changeset
179
anatofuz
parents:
diff changeset
180 class ValidatorResult visitAddExpr(const SCEVAddExpr *Expr) {
anatofuz
parents:
diff changeset
181 ValidatorResult Return(SCEVType::INT);
anatofuz
parents:
diff changeset
182
anatofuz
parents:
diff changeset
183 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
anatofuz
parents:
diff changeset
184 ValidatorResult Op = visit(Expr->getOperand(i));
anatofuz
parents:
diff changeset
185 Return.merge(Op);
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 // Early exit.
anatofuz
parents:
diff changeset
188 if (!Return.isValid())
anatofuz
parents:
diff changeset
189 break;
anatofuz
parents:
diff changeset
190 }
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 return Return;
anatofuz
parents:
diff changeset
193 }
anatofuz
parents:
diff changeset
194
anatofuz
parents:
diff changeset
195 class ValidatorResult visitMulExpr(const SCEVMulExpr *Expr) {
anatofuz
parents:
diff changeset
196 ValidatorResult Return(SCEVType::INT);
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 bool HasMultipleParams = false;
anatofuz
parents:
diff changeset
199
anatofuz
parents:
diff changeset
200 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
anatofuz
parents:
diff changeset
201 ValidatorResult Op = visit(Expr->getOperand(i));
anatofuz
parents:
diff changeset
202
anatofuz
parents:
diff changeset
203 if (Op.isINT())
anatofuz
parents:
diff changeset
204 continue;
anatofuz
parents:
diff changeset
205
anatofuz
parents:
diff changeset
206 if (Op.isPARAM() && Return.isPARAM()) {
anatofuz
parents:
diff changeset
207 HasMultipleParams = true;
anatofuz
parents:
diff changeset
208 continue;
anatofuz
parents:
diff changeset
209 }
anatofuz
parents:
diff changeset
210
anatofuz
parents:
diff changeset
211 if ((Op.isIV() || Op.isPARAM()) && !Return.isINT()) {
anatofuz
parents:
diff changeset
212 LLVM_DEBUG(
anatofuz
parents:
diff changeset
213 dbgs() << "INVALID: More than one non-int operand in MulExpr\n"
anatofuz
parents:
diff changeset
214 << "\tExpr: " << *Expr << "\n"
anatofuz
parents:
diff changeset
215 << "\tPrevious expression type: " << Return << "\n"
anatofuz
parents:
diff changeset
216 << "\tNext operand (" << Op << "): " << *Expr->getOperand(i)
anatofuz
parents:
diff changeset
217 << "\n");
anatofuz
parents:
diff changeset
218
anatofuz
parents:
diff changeset
219 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
220 }
anatofuz
parents:
diff changeset
221
anatofuz
parents:
diff changeset
222 Return.merge(Op);
anatofuz
parents:
diff changeset
223 }
anatofuz
parents:
diff changeset
224
anatofuz
parents:
diff changeset
225 if (HasMultipleParams && Return.isValid())
anatofuz
parents:
diff changeset
226 return ValidatorResult(SCEVType::PARAM, Expr);
anatofuz
parents:
diff changeset
227
anatofuz
parents:
diff changeset
228 return Return;
anatofuz
parents:
diff changeset
229 }
anatofuz
parents:
diff changeset
230
anatofuz
parents:
diff changeset
231 class ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) {
anatofuz
parents:
diff changeset
232 if (!Expr->isAffine()) {
anatofuz
parents:
diff changeset
233 LLVM_DEBUG(dbgs() << "INVALID: AddRec is not affine");
anatofuz
parents:
diff changeset
234 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
235 }
anatofuz
parents:
diff changeset
236
anatofuz
parents:
diff changeset
237 ValidatorResult Start = visit(Expr->getStart());
anatofuz
parents:
diff changeset
238 ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE));
anatofuz
parents:
diff changeset
239
anatofuz
parents:
diff changeset
240 if (!Start.isValid())
anatofuz
parents:
diff changeset
241 return Start;
anatofuz
parents:
diff changeset
242
anatofuz
parents:
diff changeset
243 if (!Recurrence.isValid())
anatofuz
parents:
diff changeset
244 return Recurrence;
anatofuz
parents:
diff changeset
245
anatofuz
parents:
diff changeset
246 auto *L = Expr->getLoop();
anatofuz
parents:
diff changeset
247 if (R->contains(L) && (!Scope || !L->contains(Scope))) {
anatofuz
parents:
diff changeset
248 LLVM_DEBUG(
anatofuz
parents:
diff changeset
249 dbgs() << "INVALID: Loop of AddRec expression boxed in an a "
anatofuz
parents:
diff changeset
250 "non-affine subregion or has a non-synthesizable exit "
anatofuz
parents:
diff changeset
251 "value.");
anatofuz
parents:
diff changeset
252 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
253 }
anatofuz
parents:
diff changeset
254
anatofuz
parents:
diff changeset
255 if (R->contains(L)) {
anatofuz
parents:
diff changeset
256 if (Recurrence.isINT()) {
anatofuz
parents:
diff changeset
257 ValidatorResult Result(SCEVType::IV);
anatofuz
parents:
diff changeset
258 Result.addParamsFrom(Start);
anatofuz
parents:
diff changeset
259 return Result;
anatofuz
parents:
diff changeset
260 }
anatofuz
parents:
diff changeset
261
anatofuz
parents:
diff changeset
262 LLVM_DEBUG(dbgs() << "INVALID: AddRec within scop has non-int"
anatofuz
parents:
diff changeset
263 "recurrence part");
anatofuz
parents:
diff changeset
264 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
265 }
anatofuz
parents:
diff changeset
266
anatofuz
parents:
diff changeset
267 assert(Recurrence.isConstant() && "Expected 'Recurrence' to be constant");
anatofuz
parents:
diff changeset
268
anatofuz
parents:
diff changeset
269 // Directly generate ValidatorResult for Expr if 'start' is zero.
anatofuz
parents:
diff changeset
270 if (Expr->getStart()->isZero())
anatofuz
parents:
diff changeset
271 return ValidatorResult(SCEVType::PARAM, Expr);
anatofuz
parents:
diff changeset
272
anatofuz
parents:
diff changeset
273 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
anatofuz
parents:
diff changeset
274 // if 'start' is not zero.
anatofuz
parents:
diff changeset
275 const SCEV *ZeroStartExpr = SE.getAddRecExpr(
anatofuz
parents:
diff changeset
276 SE.getConstant(Expr->getStart()->getType(), 0),
anatofuz
parents:
diff changeset
277 Expr->getStepRecurrence(SE), Expr->getLoop(), Expr->getNoWrapFlags());
anatofuz
parents:
diff changeset
278
anatofuz
parents:
diff changeset
279 ValidatorResult ZeroStartResult =
anatofuz
parents:
diff changeset
280 ValidatorResult(SCEVType::PARAM, ZeroStartExpr);
anatofuz
parents:
diff changeset
281 ZeroStartResult.addParamsFrom(Start);
anatofuz
parents:
diff changeset
282
anatofuz
parents:
diff changeset
283 return ZeroStartResult;
anatofuz
parents:
diff changeset
284 }
anatofuz
parents:
diff changeset
285
anatofuz
parents:
diff changeset
286 class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) {
anatofuz
parents:
diff changeset
287 ValidatorResult Return(SCEVType::INT);
anatofuz
parents:
diff changeset
288
anatofuz
parents:
diff changeset
289 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
anatofuz
parents:
diff changeset
290 ValidatorResult Op = visit(Expr->getOperand(i));
anatofuz
parents:
diff changeset
291
anatofuz
parents:
diff changeset
292 if (!Op.isValid())
anatofuz
parents:
diff changeset
293 return Op;
anatofuz
parents:
diff changeset
294
anatofuz
parents:
diff changeset
295 Return.merge(Op);
anatofuz
parents:
diff changeset
296 }
anatofuz
parents:
diff changeset
297
anatofuz
parents:
diff changeset
298 return Return;
anatofuz
parents:
diff changeset
299 }
anatofuz
parents:
diff changeset
300
anatofuz
parents:
diff changeset
301 class ValidatorResult visitSMinExpr(const SCEVSMinExpr *Expr) {
anatofuz
parents:
diff changeset
302 ValidatorResult Return(SCEVType::INT);
anatofuz
parents:
diff changeset
303
anatofuz
parents:
diff changeset
304 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
anatofuz
parents:
diff changeset
305 ValidatorResult Op = visit(Expr->getOperand(i));
anatofuz
parents:
diff changeset
306
anatofuz
parents:
diff changeset
307 if (!Op.isValid())
anatofuz
parents:
diff changeset
308 return Op;
anatofuz
parents:
diff changeset
309
anatofuz
parents:
diff changeset
310 Return.merge(Op);
anatofuz
parents:
diff changeset
311 }
anatofuz
parents:
diff changeset
312
anatofuz
parents:
diff changeset
313 return Return;
anatofuz
parents:
diff changeset
314 }
anatofuz
parents:
diff changeset
315
anatofuz
parents:
diff changeset
316 class ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) {
anatofuz
parents:
diff changeset
317 // We do not support unsigned max operations. If 'Expr' is constant during
anatofuz
parents:
diff changeset
318 // Scop execution we treat this as a parameter, otherwise we bail out.
anatofuz
parents:
diff changeset
319 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
anatofuz
parents:
diff changeset
320 ValidatorResult Op = visit(Expr->getOperand(i));
anatofuz
parents:
diff changeset
321
anatofuz
parents:
diff changeset
322 if (!Op.isConstant()) {
anatofuz
parents:
diff changeset
323 LLVM_DEBUG(dbgs() << "INVALID: UMaxExpr has a non-constant operand");
anatofuz
parents:
diff changeset
324 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
325 }
anatofuz
parents:
diff changeset
326 }
anatofuz
parents:
diff changeset
327
anatofuz
parents:
diff changeset
328 return ValidatorResult(SCEVType::PARAM, Expr);
anatofuz
parents:
diff changeset
329 }
anatofuz
parents:
diff changeset
330
anatofuz
parents:
diff changeset
331 class ValidatorResult visitUMinExpr(const SCEVUMinExpr *Expr) {
anatofuz
parents:
diff changeset
332 // We do not support unsigned min operations. If 'Expr' is constant during
anatofuz
parents:
diff changeset
333 // Scop execution we treat this as a parameter, otherwise we bail out.
anatofuz
parents:
diff changeset
334 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
anatofuz
parents:
diff changeset
335 ValidatorResult Op = visit(Expr->getOperand(i));
anatofuz
parents:
diff changeset
336
anatofuz
parents:
diff changeset
337 if (!Op.isConstant()) {
anatofuz
parents:
diff changeset
338 LLVM_DEBUG(dbgs() << "INVALID: UMinExpr has a non-constant operand");
anatofuz
parents:
diff changeset
339 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
340 }
anatofuz
parents:
diff changeset
341 }
anatofuz
parents:
diff changeset
342
anatofuz
parents:
diff changeset
343 return ValidatorResult(SCEVType::PARAM, Expr);
anatofuz
parents:
diff changeset
344 }
anatofuz
parents:
diff changeset
345
anatofuz
parents:
diff changeset
346 ValidatorResult visitGenericInst(Instruction *I, const SCEV *S) {
anatofuz
parents:
diff changeset
347 if (R->contains(I)) {
anatofuz
parents:
diff changeset
348 LLVM_DEBUG(dbgs() << "INVALID: UnknownExpr references an instruction "
anatofuz
parents:
diff changeset
349 "within the region\n");
anatofuz
parents:
diff changeset
350 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
351 }
anatofuz
parents:
diff changeset
352
anatofuz
parents:
diff changeset
353 return ValidatorResult(SCEVType::PARAM, S);
anatofuz
parents:
diff changeset
354 }
anatofuz
parents:
diff changeset
355
anatofuz
parents:
diff changeset
356 ValidatorResult visitCallInstruction(Instruction *I, const SCEV *S) {
anatofuz
parents:
diff changeset
357 assert(I->getOpcode() == Instruction::Call && "Call instruction expected");
anatofuz
parents:
diff changeset
358
anatofuz
parents:
diff changeset
359 if (R->contains(I)) {
anatofuz
parents:
diff changeset
360 auto Call = cast<CallInst>(I);
anatofuz
parents:
diff changeset
361
anatofuz
parents:
diff changeset
362 if (!isConstCall(Call))
anatofuz
parents:
diff changeset
363 return ValidatorResult(SCEVType::INVALID, S);
anatofuz
parents:
diff changeset
364 }
anatofuz
parents:
diff changeset
365 return ValidatorResult(SCEVType::PARAM, S);
anatofuz
parents:
diff changeset
366 }
anatofuz
parents:
diff changeset
367
anatofuz
parents:
diff changeset
368 ValidatorResult visitLoadInstruction(Instruction *I, const SCEV *S) {
anatofuz
parents:
diff changeset
369 if (R->contains(I) && ILS) {
anatofuz
parents:
diff changeset
370 ILS->insert(cast<LoadInst>(I));
anatofuz
parents:
diff changeset
371 return ValidatorResult(SCEVType::PARAM, S);
anatofuz
parents:
diff changeset
372 }
anatofuz
parents:
diff changeset
373
anatofuz
parents:
diff changeset
374 return visitGenericInst(I, S);
anatofuz
parents:
diff changeset
375 }
anatofuz
parents:
diff changeset
376
anatofuz
parents:
diff changeset
377 ValidatorResult visitDivision(const SCEV *Dividend, const SCEV *Divisor,
anatofuz
parents:
diff changeset
378 const SCEV *DivExpr,
anatofuz
parents:
diff changeset
379 Instruction *SDiv = nullptr) {
anatofuz
parents:
diff changeset
380
anatofuz
parents:
diff changeset
381 // First check if we might be able to model the division, thus if the
anatofuz
parents:
diff changeset
382 // divisor is constant. If so, check the dividend, otherwise check if
anatofuz
parents:
diff changeset
383 // the whole division can be seen as a parameter.
anatofuz
parents:
diff changeset
384 if (isa<SCEVConstant>(Divisor) && !Divisor->isZero())
anatofuz
parents:
diff changeset
385 return visit(Dividend);
anatofuz
parents:
diff changeset
386
anatofuz
parents:
diff changeset
387 // For signed divisions use the SDiv instruction to check for a parameter
anatofuz
parents:
diff changeset
388 // division, for unsigned divisions check the operands.
anatofuz
parents:
diff changeset
389 if (SDiv)
anatofuz
parents:
diff changeset
390 return visitGenericInst(SDiv, DivExpr);
anatofuz
parents:
diff changeset
391
anatofuz
parents:
diff changeset
392 ValidatorResult LHS = visit(Dividend);
anatofuz
parents:
diff changeset
393 ValidatorResult RHS = visit(Divisor);
anatofuz
parents:
diff changeset
394 if (LHS.isConstant() && RHS.isConstant())
anatofuz
parents:
diff changeset
395 return ValidatorResult(SCEVType::PARAM, DivExpr);
anatofuz
parents:
diff changeset
396
anatofuz
parents:
diff changeset
397 LLVM_DEBUG(
anatofuz
parents:
diff changeset
398 dbgs() << "INVALID: unsigned division of non-constant expressions");
anatofuz
parents:
diff changeset
399 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
400 }
anatofuz
parents:
diff changeset
401
anatofuz
parents:
diff changeset
402 ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) {
anatofuz
parents:
diff changeset
403 if (!PollyAllowUnsignedOperations)
anatofuz
parents:
diff changeset
404 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
405
anatofuz
parents:
diff changeset
406 auto *Dividend = Expr->getLHS();
anatofuz
parents:
diff changeset
407 auto *Divisor = Expr->getRHS();
anatofuz
parents:
diff changeset
408 return visitDivision(Dividend, Divisor, Expr);
anatofuz
parents:
diff changeset
409 }
anatofuz
parents:
diff changeset
410
anatofuz
parents:
diff changeset
411 ValidatorResult visitSDivInstruction(Instruction *SDiv, const SCEV *Expr) {
anatofuz
parents:
diff changeset
412 assert(SDiv->getOpcode() == Instruction::SDiv &&
anatofuz
parents:
diff changeset
413 "Assumed SDiv instruction!");
anatofuz
parents:
diff changeset
414
anatofuz
parents:
diff changeset
415 auto *Dividend = SE.getSCEV(SDiv->getOperand(0));
anatofuz
parents:
diff changeset
416 auto *Divisor = SE.getSCEV(SDiv->getOperand(1));
anatofuz
parents:
diff changeset
417 return visitDivision(Dividend, Divisor, Expr, SDiv);
anatofuz
parents:
diff changeset
418 }
anatofuz
parents:
diff changeset
419
anatofuz
parents:
diff changeset
420 ValidatorResult visitSRemInstruction(Instruction *SRem, const SCEV *S) {
anatofuz
parents:
diff changeset
421 assert(SRem->getOpcode() == Instruction::SRem &&
anatofuz
parents:
diff changeset
422 "Assumed SRem instruction!");
anatofuz
parents:
diff changeset
423
anatofuz
parents:
diff changeset
424 auto *Divisor = SRem->getOperand(1);
anatofuz
parents:
diff changeset
425 auto *CI = dyn_cast<ConstantInt>(Divisor);
anatofuz
parents:
diff changeset
426 if (!CI || CI->isZeroValue())
anatofuz
parents:
diff changeset
427 return visitGenericInst(SRem, S);
anatofuz
parents:
diff changeset
428
anatofuz
parents:
diff changeset
429 auto *Dividend = SRem->getOperand(0);
anatofuz
parents:
diff changeset
430 auto *DividendSCEV = SE.getSCEV(Dividend);
anatofuz
parents:
diff changeset
431 return visit(DividendSCEV);
anatofuz
parents:
diff changeset
432 }
anatofuz
parents:
diff changeset
433
anatofuz
parents:
diff changeset
434 ValidatorResult visitUnknown(const SCEVUnknown *Expr) {
anatofuz
parents:
diff changeset
435 Value *V = Expr->getValue();
anatofuz
parents:
diff changeset
436
anatofuz
parents:
diff changeset
437 if (!Expr->getType()->isIntegerTy() && !Expr->getType()->isPointerTy()) {
anatofuz
parents:
diff changeset
438 LLVM_DEBUG(dbgs() << "INVALID: UnknownExpr is not an integer or pointer");
anatofuz
parents:
diff changeset
439 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
440 }
anatofuz
parents:
diff changeset
441
anatofuz
parents:
diff changeset
442 if (isa<UndefValue>(V)) {
anatofuz
parents:
diff changeset
443 LLVM_DEBUG(dbgs() << "INVALID: UnknownExpr references an undef value");
anatofuz
parents:
diff changeset
444 return ValidatorResult(SCEVType::INVALID);
anatofuz
parents:
diff changeset
445 }
anatofuz
parents:
diff changeset
446
anatofuz
parents:
diff changeset
447 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
anatofuz
parents:
diff changeset
448 switch (I->getOpcode()) {
anatofuz
parents:
diff changeset
449 case Instruction::IntToPtr:
anatofuz
parents:
diff changeset
450 return visit(SE.getSCEVAtScope(I->getOperand(0), Scope));
anatofuz
parents:
diff changeset
451 case Instruction::Load:
anatofuz
parents:
diff changeset
452 return visitLoadInstruction(I, Expr);
anatofuz
parents:
diff changeset
453 case Instruction::SDiv:
anatofuz
parents:
diff changeset
454 return visitSDivInstruction(I, Expr);
anatofuz
parents:
diff changeset
455 case Instruction::SRem:
anatofuz
parents:
diff changeset
456 return visitSRemInstruction(I, Expr);
anatofuz
parents:
diff changeset
457 case Instruction::Call:
anatofuz
parents:
diff changeset
458 return visitCallInstruction(I, Expr);
anatofuz
parents:
diff changeset
459 default:
anatofuz
parents:
diff changeset
460 return visitGenericInst(I, Expr);
anatofuz
parents:
diff changeset
461 }
anatofuz
parents:
diff changeset
462 }
anatofuz
parents:
diff changeset
463
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
464 if (Expr->getType()->isPointerTy()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
465 if (isa<ConstantPointerNull>(V))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
466 return ValidatorResult(SCEVType::INT); // "int"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
467 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
468
150
anatofuz
parents:
diff changeset
469 return ValidatorResult(SCEVType::PARAM, Expr);
anatofuz
parents:
diff changeset
470 }
anatofuz
parents:
diff changeset
471 };
anatofuz
parents:
diff changeset
472
anatofuz
parents:
diff changeset
473 class SCEVHasIVParams {
anatofuz
parents:
diff changeset
474 bool HasIVParams = false;
anatofuz
parents:
diff changeset
475
anatofuz
parents:
diff changeset
476 public:
anatofuz
parents:
diff changeset
477 SCEVHasIVParams() {}
anatofuz
parents:
diff changeset
478
anatofuz
parents:
diff changeset
479 bool follow(const SCEV *S) {
anatofuz
parents:
diff changeset
480 const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(S);
anatofuz
parents:
diff changeset
481 if (!Unknown)
anatofuz
parents:
diff changeset
482 return true;
anatofuz
parents:
diff changeset
483
anatofuz
parents:
diff changeset
484 CallInst *Call = dyn_cast<CallInst>(Unknown->getValue());
anatofuz
parents:
diff changeset
485
anatofuz
parents:
diff changeset
486 if (!Call)
anatofuz
parents:
diff changeset
487 return true;
anatofuz
parents:
diff changeset
488
anatofuz
parents:
diff changeset
489 if (isConstCall(Call)) {
anatofuz
parents:
diff changeset
490 HasIVParams = true;
anatofuz
parents:
diff changeset
491 return false;
anatofuz
parents:
diff changeset
492 }
anatofuz
parents:
diff changeset
493
anatofuz
parents:
diff changeset
494 return true;
anatofuz
parents:
diff changeset
495 }
anatofuz
parents:
diff changeset
496
anatofuz
parents:
diff changeset
497 bool isDone() { return HasIVParams; }
anatofuz
parents:
diff changeset
498 bool hasIVParams() { return HasIVParams; }
anatofuz
parents:
diff changeset
499 };
anatofuz
parents:
diff changeset
500
anatofuz
parents:
diff changeset
501 /// Check whether a SCEV refers to an SSA name defined inside a region.
anatofuz
parents:
diff changeset
502 class SCEVInRegionDependences {
anatofuz
parents:
diff changeset
503 const Region *R;
anatofuz
parents:
diff changeset
504 Loop *Scope;
anatofuz
parents:
diff changeset
505 const InvariantLoadsSetTy &ILS;
anatofuz
parents:
diff changeset
506 bool AllowLoops;
anatofuz
parents:
diff changeset
507 bool HasInRegionDeps = false;
anatofuz
parents:
diff changeset
508
anatofuz
parents:
diff changeset
509 public:
anatofuz
parents:
diff changeset
510 SCEVInRegionDependences(const Region *R, Loop *Scope, bool AllowLoops,
anatofuz
parents:
diff changeset
511 const InvariantLoadsSetTy &ILS)
anatofuz
parents:
diff changeset
512 : R(R), Scope(Scope), ILS(ILS), AllowLoops(AllowLoops) {}
anatofuz
parents:
diff changeset
513
anatofuz
parents:
diff changeset
514 bool follow(const SCEV *S) {
anatofuz
parents:
diff changeset
515 if (auto Unknown = dyn_cast<SCEVUnknown>(S)) {
anatofuz
parents:
diff changeset
516 Instruction *Inst = dyn_cast<Instruction>(Unknown->getValue());
anatofuz
parents:
diff changeset
517
anatofuz
parents:
diff changeset
518 CallInst *Call = dyn_cast<CallInst>(Unknown->getValue());
anatofuz
parents:
diff changeset
519
anatofuz
parents:
diff changeset
520 if (Call && isConstCall(Call))
anatofuz
parents:
diff changeset
521 return false;
anatofuz
parents:
diff changeset
522
anatofuz
parents:
diff changeset
523 if (Inst) {
anatofuz
parents:
diff changeset
524 // When we invariant load hoist a load, we first make sure that there
anatofuz
parents:
diff changeset
525 // can be no dependences created by it in the Scop region. So, we should
anatofuz
parents:
diff changeset
526 // not consider scalar dependences to `LoadInst`s that are invariant
anatofuz
parents:
diff changeset
527 // load hoisted.
anatofuz
parents:
diff changeset
528 //
anatofuz
parents:
diff changeset
529 // If this check is not present, then we create data dependences which
anatofuz
parents:
diff changeset
530 // are strictly not necessary by tracking the invariant load as a
anatofuz
parents:
diff changeset
531 // scalar.
anatofuz
parents:
diff changeset
532 LoadInst *LI = dyn_cast<LoadInst>(Inst);
anatofuz
parents:
diff changeset
533 if (LI && ILS.count(LI) > 0)
anatofuz
parents:
diff changeset
534 return false;
anatofuz
parents:
diff changeset
535 }
anatofuz
parents:
diff changeset
536
anatofuz
parents:
diff changeset
537 // Return true when Inst is defined inside the region R.
anatofuz
parents:
diff changeset
538 if (!Inst || !R->contains(Inst))
anatofuz
parents:
diff changeset
539 return true;
anatofuz
parents:
diff changeset
540
anatofuz
parents:
diff changeset
541 HasInRegionDeps = true;
anatofuz
parents:
diff changeset
542 return false;
anatofuz
parents:
diff changeset
543 }
anatofuz
parents:
diff changeset
544
anatofuz
parents:
diff changeset
545 if (auto AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
anatofuz
parents:
diff changeset
546 if (AllowLoops)
anatofuz
parents:
diff changeset
547 return true;
anatofuz
parents:
diff changeset
548
anatofuz
parents:
diff changeset
549 auto *L = AddRec->getLoop();
anatofuz
parents:
diff changeset
550 if (R->contains(L) && !L->contains(Scope)) {
anatofuz
parents:
diff changeset
551 HasInRegionDeps = true;
anatofuz
parents:
diff changeset
552 return false;
anatofuz
parents:
diff changeset
553 }
anatofuz
parents:
diff changeset
554 }
anatofuz
parents:
diff changeset
555
anatofuz
parents:
diff changeset
556 return true;
anatofuz
parents:
diff changeset
557 }
anatofuz
parents:
diff changeset
558 bool isDone() { return false; }
anatofuz
parents:
diff changeset
559 bool hasDependences() { return HasInRegionDeps; }
anatofuz
parents:
diff changeset
560 };
anatofuz
parents:
diff changeset
561
anatofuz
parents:
diff changeset
562 namespace polly {
anatofuz
parents:
diff changeset
563 /// Find all loops referenced in SCEVAddRecExprs.
anatofuz
parents:
diff changeset
564 class SCEVFindLoops {
anatofuz
parents:
diff changeset
565 SetVector<const Loop *> &Loops;
anatofuz
parents:
diff changeset
566
anatofuz
parents:
diff changeset
567 public:
anatofuz
parents:
diff changeset
568 SCEVFindLoops(SetVector<const Loop *> &Loops) : Loops(Loops) {}
anatofuz
parents:
diff changeset
569
anatofuz
parents:
diff changeset
570 bool follow(const SCEV *S) {
anatofuz
parents:
diff changeset
571 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S))
anatofuz
parents:
diff changeset
572 Loops.insert(AddRec->getLoop());
anatofuz
parents:
diff changeset
573 return true;
anatofuz
parents:
diff changeset
574 }
anatofuz
parents:
diff changeset
575 bool isDone() { return false; }
anatofuz
parents:
diff changeset
576 };
anatofuz
parents:
diff changeset
577
anatofuz
parents:
diff changeset
578 void findLoops(const SCEV *Expr, SetVector<const Loop *> &Loops) {
anatofuz
parents:
diff changeset
579 SCEVFindLoops FindLoops(Loops);
anatofuz
parents:
diff changeset
580 SCEVTraversal<SCEVFindLoops> ST(FindLoops);
anatofuz
parents:
diff changeset
581 ST.visitAll(Expr);
anatofuz
parents:
diff changeset
582 }
anatofuz
parents:
diff changeset
583
anatofuz
parents:
diff changeset
584 /// Find all values referenced in SCEVUnknowns.
anatofuz
parents:
diff changeset
585 class SCEVFindValues {
anatofuz
parents:
diff changeset
586 ScalarEvolution &SE;
anatofuz
parents:
diff changeset
587 SetVector<Value *> &Values;
anatofuz
parents:
diff changeset
588
anatofuz
parents:
diff changeset
589 public:
anatofuz
parents:
diff changeset
590 SCEVFindValues(ScalarEvolution &SE, SetVector<Value *> &Values)
anatofuz
parents:
diff changeset
591 : SE(SE), Values(Values) {}
anatofuz
parents:
diff changeset
592
anatofuz
parents:
diff changeset
593 bool follow(const SCEV *S) {
anatofuz
parents:
diff changeset
594 const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(S);
anatofuz
parents:
diff changeset
595 if (!Unknown)
anatofuz
parents:
diff changeset
596 return true;
anatofuz
parents:
diff changeset
597
anatofuz
parents:
diff changeset
598 Values.insert(Unknown->getValue());
anatofuz
parents:
diff changeset
599 Instruction *Inst = dyn_cast<Instruction>(Unknown->getValue());
anatofuz
parents:
diff changeset
600 if (!Inst || (Inst->getOpcode() != Instruction::SRem &&
anatofuz
parents:
diff changeset
601 Inst->getOpcode() != Instruction::SDiv))
anatofuz
parents:
diff changeset
602 return false;
anatofuz
parents:
diff changeset
603
anatofuz
parents:
diff changeset
604 auto *Dividend = SE.getSCEV(Inst->getOperand(1));
anatofuz
parents:
diff changeset
605 if (!isa<SCEVConstant>(Dividend))
anatofuz
parents:
diff changeset
606 return false;
anatofuz
parents:
diff changeset
607
anatofuz
parents:
diff changeset
608 auto *Divisor = SE.getSCEV(Inst->getOperand(0));
anatofuz
parents:
diff changeset
609 SCEVFindValues FindValues(SE, Values);
anatofuz
parents:
diff changeset
610 SCEVTraversal<SCEVFindValues> ST(FindValues);
anatofuz
parents:
diff changeset
611 ST.visitAll(Dividend);
anatofuz
parents:
diff changeset
612 ST.visitAll(Divisor);
anatofuz
parents:
diff changeset
613
anatofuz
parents:
diff changeset
614 return false;
anatofuz
parents:
diff changeset
615 }
anatofuz
parents:
diff changeset
616 bool isDone() { return false; }
anatofuz
parents:
diff changeset
617 };
anatofuz
parents:
diff changeset
618
anatofuz
parents:
diff changeset
619 void findValues(const SCEV *Expr, ScalarEvolution &SE,
anatofuz
parents:
diff changeset
620 SetVector<Value *> &Values) {
anatofuz
parents:
diff changeset
621 SCEVFindValues FindValues(SE, Values);
anatofuz
parents:
diff changeset
622 SCEVTraversal<SCEVFindValues> ST(FindValues);
anatofuz
parents:
diff changeset
623 ST.visitAll(Expr);
anatofuz
parents:
diff changeset
624 }
anatofuz
parents:
diff changeset
625
anatofuz
parents:
diff changeset
626 bool hasIVParams(const SCEV *Expr) {
anatofuz
parents:
diff changeset
627 SCEVHasIVParams HasIVParams;
anatofuz
parents:
diff changeset
628 SCEVTraversal<SCEVHasIVParams> ST(HasIVParams);
anatofuz
parents:
diff changeset
629 ST.visitAll(Expr);
anatofuz
parents:
diff changeset
630 return HasIVParams.hasIVParams();
anatofuz
parents:
diff changeset
631 }
anatofuz
parents:
diff changeset
632
anatofuz
parents:
diff changeset
633 bool hasScalarDepsInsideRegion(const SCEV *Expr, const Region *R,
anatofuz
parents:
diff changeset
634 llvm::Loop *Scope, bool AllowLoops,
anatofuz
parents:
diff changeset
635 const InvariantLoadsSetTy &ILS) {
anatofuz
parents:
diff changeset
636 SCEVInRegionDependences InRegionDeps(R, Scope, AllowLoops, ILS);
anatofuz
parents:
diff changeset
637 SCEVTraversal<SCEVInRegionDependences> ST(InRegionDeps);
anatofuz
parents:
diff changeset
638 ST.visitAll(Expr);
anatofuz
parents:
diff changeset
639 return InRegionDeps.hasDependences();
anatofuz
parents:
diff changeset
640 }
anatofuz
parents:
diff changeset
641
anatofuz
parents:
diff changeset
642 bool isAffineExpr(const Region *R, llvm::Loop *Scope, const SCEV *Expr,
anatofuz
parents:
diff changeset
643 ScalarEvolution &SE, InvariantLoadsSetTy *ILS) {
anatofuz
parents:
diff changeset
644 if (isa<SCEVCouldNotCompute>(Expr))
anatofuz
parents:
diff changeset
645 return false;
anatofuz
parents:
diff changeset
646
anatofuz
parents:
diff changeset
647 SCEVValidator Validator(R, Scope, SE, ILS);
anatofuz
parents:
diff changeset
648 LLVM_DEBUG({
anatofuz
parents:
diff changeset
649 dbgs() << "\n";
anatofuz
parents:
diff changeset
650 dbgs() << "Expr: " << *Expr << "\n";
anatofuz
parents:
diff changeset
651 dbgs() << "Region: " << R->getNameStr() << "\n";
anatofuz
parents:
diff changeset
652 dbgs() << " -> ";
anatofuz
parents:
diff changeset
653 });
anatofuz
parents:
diff changeset
654
anatofuz
parents:
diff changeset
655 ValidatorResult Result = Validator.visit(Expr);
anatofuz
parents:
diff changeset
656
anatofuz
parents:
diff changeset
657 LLVM_DEBUG({
anatofuz
parents:
diff changeset
658 if (Result.isValid())
anatofuz
parents:
diff changeset
659 dbgs() << "VALID\n";
anatofuz
parents:
diff changeset
660 dbgs() << "\n";
anatofuz
parents:
diff changeset
661 });
anatofuz
parents:
diff changeset
662
anatofuz
parents:
diff changeset
663 return Result.isValid();
anatofuz
parents:
diff changeset
664 }
anatofuz
parents:
diff changeset
665
anatofuz
parents:
diff changeset
666 static bool isAffineExpr(Value *V, const Region *R, Loop *Scope,
anatofuz
parents:
diff changeset
667 ScalarEvolution &SE, ParameterSetTy &Params) {
anatofuz
parents:
diff changeset
668 auto *E = SE.getSCEV(V);
anatofuz
parents:
diff changeset
669 if (isa<SCEVCouldNotCompute>(E))
anatofuz
parents:
diff changeset
670 return false;
anatofuz
parents:
diff changeset
671
anatofuz
parents:
diff changeset
672 SCEVValidator Validator(R, Scope, SE, nullptr);
anatofuz
parents:
diff changeset
673 ValidatorResult Result = Validator.visit(E);
anatofuz
parents:
diff changeset
674 if (!Result.isValid())
anatofuz
parents:
diff changeset
675 return false;
anatofuz
parents:
diff changeset
676
anatofuz
parents:
diff changeset
677 auto ResultParams = Result.getParameters();
anatofuz
parents:
diff changeset
678 Params.insert(ResultParams.begin(), ResultParams.end());
anatofuz
parents:
diff changeset
679
anatofuz
parents:
diff changeset
680 return true;
anatofuz
parents:
diff changeset
681 }
anatofuz
parents:
diff changeset
682
anatofuz
parents:
diff changeset
683 bool isAffineConstraint(Value *V, const Region *R, llvm::Loop *Scope,
anatofuz
parents:
diff changeset
684 ScalarEvolution &SE, ParameterSetTy &Params,
anatofuz
parents:
diff changeset
685 bool OrExpr) {
anatofuz
parents:
diff changeset
686 if (auto *ICmp = dyn_cast<ICmpInst>(V)) {
anatofuz
parents:
diff changeset
687 return isAffineConstraint(ICmp->getOperand(0), R, Scope, SE, Params,
anatofuz
parents:
diff changeset
688 true) &&
anatofuz
parents:
diff changeset
689 isAffineConstraint(ICmp->getOperand(1), R, Scope, SE, Params, true);
anatofuz
parents:
diff changeset
690 } else if (auto *BinOp = dyn_cast<BinaryOperator>(V)) {
anatofuz
parents:
diff changeset
691 auto Opcode = BinOp->getOpcode();
anatofuz
parents:
diff changeset
692 if (Opcode == Instruction::And || Opcode == Instruction::Or)
anatofuz
parents:
diff changeset
693 return isAffineConstraint(BinOp->getOperand(0), R, Scope, SE, Params,
anatofuz
parents:
diff changeset
694 false) &&
anatofuz
parents:
diff changeset
695 isAffineConstraint(BinOp->getOperand(1), R, Scope, SE, Params,
anatofuz
parents:
diff changeset
696 false);
anatofuz
parents:
diff changeset
697 /* Fall through */
anatofuz
parents:
diff changeset
698 }
anatofuz
parents:
diff changeset
699
anatofuz
parents:
diff changeset
700 if (!OrExpr)
anatofuz
parents:
diff changeset
701 return false;
anatofuz
parents:
diff changeset
702
anatofuz
parents:
diff changeset
703 return isAffineExpr(V, R, Scope, SE, Params);
anatofuz
parents:
diff changeset
704 }
anatofuz
parents:
diff changeset
705
anatofuz
parents:
diff changeset
706 ParameterSetTy getParamsInAffineExpr(const Region *R, Loop *Scope,
anatofuz
parents:
diff changeset
707 const SCEV *Expr, ScalarEvolution &SE) {
anatofuz
parents:
diff changeset
708 if (isa<SCEVCouldNotCompute>(Expr))
anatofuz
parents:
diff changeset
709 return ParameterSetTy();
anatofuz
parents:
diff changeset
710
anatofuz
parents:
diff changeset
711 InvariantLoadsSetTy ILS;
anatofuz
parents:
diff changeset
712 SCEVValidator Validator(R, Scope, SE, &ILS);
anatofuz
parents:
diff changeset
713 ValidatorResult Result = Validator.visit(Expr);
anatofuz
parents:
diff changeset
714 assert(Result.isValid() && "Requested parameters for an invalid SCEV!");
anatofuz
parents:
diff changeset
715
anatofuz
parents:
diff changeset
716 return Result.getParameters();
anatofuz
parents:
diff changeset
717 }
anatofuz
parents:
diff changeset
718
anatofuz
parents:
diff changeset
719 std::pair<const SCEVConstant *, const SCEV *>
anatofuz
parents:
diff changeset
720 extractConstantFactor(const SCEV *S, ScalarEvolution &SE) {
anatofuz
parents:
diff changeset
721 auto *ConstPart = cast<SCEVConstant>(SE.getConstant(S->getType(), 1));
anatofuz
parents:
diff changeset
722
anatofuz
parents:
diff changeset
723 if (auto *Constant = dyn_cast<SCEVConstant>(S))
anatofuz
parents:
diff changeset
724 return std::make_pair(Constant, SE.getConstant(S->getType(), 1));
anatofuz
parents:
diff changeset
725
anatofuz
parents:
diff changeset
726 auto *AddRec = dyn_cast<SCEVAddRecExpr>(S);
anatofuz
parents:
diff changeset
727 if (AddRec) {
anatofuz
parents:
diff changeset
728 auto *StartExpr = AddRec->getStart();
anatofuz
parents:
diff changeset
729 if (StartExpr->isZero()) {
anatofuz
parents:
diff changeset
730 auto StepPair = extractConstantFactor(AddRec->getStepRecurrence(SE), SE);
anatofuz
parents:
diff changeset
731 auto *LeftOverAddRec =
anatofuz
parents:
diff changeset
732 SE.getAddRecExpr(StartExpr, StepPair.second, AddRec->getLoop(),
anatofuz
parents:
diff changeset
733 AddRec->getNoWrapFlags());
anatofuz
parents:
diff changeset
734 return std::make_pair(StepPair.first, LeftOverAddRec);
anatofuz
parents:
diff changeset
735 }
anatofuz
parents:
diff changeset
736 return std::make_pair(ConstPart, S);
anatofuz
parents:
diff changeset
737 }
anatofuz
parents:
diff changeset
738
anatofuz
parents:
diff changeset
739 if (auto *Add = dyn_cast<SCEVAddExpr>(S)) {
anatofuz
parents:
diff changeset
740 SmallVector<const SCEV *, 4> LeftOvers;
anatofuz
parents:
diff changeset
741 auto Op0Pair = extractConstantFactor(Add->getOperand(0), SE);
anatofuz
parents:
diff changeset
742 auto *Factor = Op0Pair.first;
anatofuz
parents:
diff changeset
743 if (SE.isKnownNegative(Factor)) {
anatofuz
parents:
diff changeset
744 Factor = cast<SCEVConstant>(SE.getNegativeSCEV(Factor));
anatofuz
parents:
diff changeset
745 LeftOvers.push_back(SE.getNegativeSCEV(Op0Pair.second));
anatofuz
parents:
diff changeset
746 } else {
anatofuz
parents:
diff changeset
747 LeftOvers.push_back(Op0Pair.second);
anatofuz
parents:
diff changeset
748 }
anatofuz
parents:
diff changeset
749
anatofuz
parents:
diff changeset
750 for (unsigned u = 1, e = Add->getNumOperands(); u < e; u++) {
anatofuz
parents:
diff changeset
751 auto OpUPair = extractConstantFactor(Add->getOperand(u), SE);
anatofuz
parents:
diff changeset
752 // TODO: Use something smarter than equality here, e.g., gcd.
anatofuz
parents:
diff changeset
753 if (Factor == OpUPair.first)
anatofuz
parents:
diff changeset
754 LeftOvers.push_back(OpUPair.second);
anatofuz
parents:
diff changeset
755 else if (Factor == SE.getNegativeSCEV(OpUPair.first))
anatofuz
parents:
diff changeset
756 LeftOvers.push_back(SE.getNegativeSCEV(OpUPair.second));
anatofuz
parents:
diff changeset
757 else
anatofuz
parents:
diff changeset
758 return std::make_pair(ConstPart, S);
anatofuz
parents:
diff changeset
759 }
anatofuz
parents:
diff changeset
760
anatofuz
parents:
diff changeset
761 auto *NewAdd = SE.getAddExpr(LeftOvers, Add->getNoWrapFlags());
anatofuz
parents:
diff changeset
762 return std::make_pair(Factor, NewAdd);
anatofuz
parents:
diff changeset
763 }
anatofuz
parents:
diff changeset
764
anatofuz
parents:
diff changeset
765 auto *Mul = dyn_cast<SCEVMulExpr>(S);
anatofuz
parents:
diff changeset
766 if (!Mul)
anatofuz
parents:
diff changeset
767 return std::make_pair(ConstPart, S);
anatofuz
parents:
diff changeset
768
anatofuz
parents:
diff changeset
769 SmallVector<const SCEV *, 4> LeftOvers;
anatofuz
parents:
diff changeset
770 for (auto *Op : Mul->operands())
anatofuz
parents:
diff changeset
771 if (isa<SCEVConstant>(Op))
anatofuz
parents:
diff changeset
772 ConstPart = cast<SCEVConstant>(SE.getMulExpr(ConstPart, Op));
anatofuz
parents:
diff changeset
773 else
anatofuz
parents:
diff changeset
774 LeftOvers.push_back(Op);
anatofuz
parents:
diff changeset
775
anatofuz
parents:
diff changeset
776 return std::make_pair(ConstPart, SE.getMulExpr(LeftOvers));
anatofuz
parents:
diff changeset
777 }
anatofuz
parents:
diff changeset
778
anatofuz
parents:
diff changeset
779 const SCEV *tryForwardThroughPHI(const SCEV *Expr, Region &R,
anatofuz
parents:
diff changeset
780 ScalarEvolution &SE, LoopInfo &LI,
anatofuz
parents:
diff changeset
781 const DominatorTree &DT) {
anatofuz
parents:
diff changeset
782 if (auto *Unknown = dyn_cast<SCEVUnknown>(Expr)) {
anatofuz
parents:
diff changeset
783 Value *V = Unknown->getValue();
anatofuz
parents:
diff changeset
784 auto *PHI = dyn_cast<PHINode>(V);
anatofuz
parents:
diff changeset
785 if (!PHI)
anatofuz
parents:
diff changeset
786 return Expr;
anatofuz
parents:
diff changeset
787
anatofuz
parents:
diff changeset
788 Value *Final = nullptr;
anatofuz
parents:
diff changeset
789
anatofuz
parents:
diff changeset
790 for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
anatofuz
parents:
diff changeset
791 BasicBlock *Incoming = PHI->getIncomingBlock(i);
anatofuz
parents:
diff changeset
792 if (isErrorBlock(*Incoming, R, LI, DT) && R.contains(Incoming))
anatofuz
parents:
diff changeset
793 continue;
anatofuz
parents:
diff changeset
794 if (Final)
anatofuz
parents:
diff changeset
795 return Expr;
anatofuz
parents:
diff changeset
796 Final = PHI->getIncomingValue(i);
anatofuz
parents:
diff changeset
797 }
anatofuz
parents:
diff changeset
798
anatofuz
parents:
diff changeset
799 if (Final)
anatofuz
parents:
diff changeset
800 return SE.getSCEV(Final);
anatofuz
parents:
diff changeset
801 }
anatofuz
parents:
diff changeset
802 return Expr;
anatofuz
parents:
diff changeset
803 }
anatofuz
parents:
diff changeset
804
anatofuz
parents:
diff changeset
805 Value *getUniqueNonErrorValue(PHINode *PHI, Region *R, LoopInfo &LI,
anatofuz
parents:
diff changeset
806 const DominatorTree &DT) {
anatofuz
parents:
diff changeset
807 Value *V = nullptr;
anatofuz
parents:
diff changeset
808 for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
anatofuz
parents:
diff changeset
809 BasicBlock *BB = PHI->getIncomingBlock(i);
anatofuz
parents:
diff changeset
810 if (!isErrorBlock(*BB, *R, LI, DT)) {
anatofuz
parents:
diff changeset
811 if (V)
anatofuz
parents:
diff changeset
812 return nullptr;
anatofuz
parents:
diff changeset
813 V = PHI->getIncomingValue(i);
anatofuz
parents:
diff changeset
814 }
anatofuz
parents:
diff changeset
815 }
anatofuz
parents:
diff changeset
816
anatofuz
parents:
diff changeset
817 return V;
anatofuz
parents:
diff changeset
818 }
anatofuz
parents:
diff changeset
819 } // namespace polly