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