[PM] Properly require and preserve OptimizationRemarkEmitter. NFCI.
[polly-mirror.git] / include / polly / ScopDetection.h
blob7e61c06cf9cef81ece33b6ed1f024f8d0a7770af
1 //===- ScopDetection.h - Detect Scops ---------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Detect the maximal Scops of a function.
12 // A static control part (Scop) is a subgraph of the control flow graph (CFG)
13 // that only has statically known control flow and can therefore be described
14 // within the polyhedral model.
16 // Every Scop fulfills these restrictions:
18 // * It is a single entry single exit region
20 // * Only affine linear bounds in the loops
22 // Every natural loop in a Scop must have a number of loop iterations that can
23 // be described as an affine linear function in surrounding loop iterators or
24 // parameters. (A parameter is a scalar that does not change its value during
25 // execution of the Scop).
27 // * Only comparisons of affine linear expressions in conditions
29 // * All loops and conditions perfectly nested
31 // The control flow needs to be structured such that it could be written using
32 // just 'for' and 'if' statements, without the need for any 'goto', 'break' or
33 // 'continue'.
35 // * Side effect free functions call
37 // Only function calls and intrinsics that do not have side effects are allowed
38 // (readnone).
40 // The Scop detection finds the largest Scops by checking if the largest
41 // region is a Scop. If this is not the case, its canonical subregions are
42 // checked until a region is a Scop. It is now tried to extend this Scop by
43 // creating a larger non canonical region.
45 //===----------------------------------------------------------------------===//
47 #ifndef POLLY_SCOPDETECTION_H
48 #define POLLY_SCOPDETECTION_H
50 #include "polly/ScopDetectionDiagnostic.h"
51 #include "polly/Support/ScopHelper.h"
52 #include "llvm/ADT/DenseMap.h"
53 #include "llvm/ADT/SetVector.h"
54 #include "llvm/ADT/SmallVector.h"
55 #include "llvm/ADT/StringRef.h"
56 #include "llvm/Analysis/AliasAnalysis.h"
57 #include "llvm/Analysis/AliasSetTracker.h"
58 #include "llvm/Analysis/RegionInfo.h"
59 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
60 #include "llvm/Pass.h"
61 #include <algorithm>
62 #include <map>
63 #include <memory>
64 #include <set>
65 #include <string>
66 #include <utility>
67 #include <vector>
69 using namespace llvm;
71 namespace llvm {
73 class BasicBlock;
74 class BranchInst;
75 class CallInst;
76 class DebugLoc;
77 class DominatorTree;
78 class Function;
79 class Instruction;
80 class IntrinsicInst;
81 class Loop;
82 class LoopInfo;
83 class OptimizationRemarkEmitter;
84 class PassRegistry;
85 class raw_ostream;
86 class ScalarEvolution;
87 class SCEV;
88 class SCEVUnknown;
89 class SwitchInst;
90 class Value;
92 void initializeScopDetectionWrapperPassPass(PassRegistry &);
94 } // namespace llvm
96 namespace polly {
98 using ParamSetType = std::set<const SCEV *>;
100 // Description of the shape of an array.
101 struct ArrayShape {
102 // Base pointer identifying all accesses to this array.
103 const SCEVUnknown *BasePointer;
105 // Sizes of each delinearized dimension.
106 SmallVector<const SCEV *, 4> DelinearizedSizes;
108 ArrayShape(const SCEVUnknown *B) : BasePointer(B) {}
111 struct MemAcc {
112 const Instruction *Insn;
114 // A pointer to the shape description of the array.
115 std::shared_ptr<ArrayShape> Shape;
117 // Subscripts computed by delinearization.
118 SmallVector<const SCEV *, 4> DelinearizedSubscripts;
120 MemAcc(const Instruction *I, std::shared_ptr<ArrayShape> S)
121 : Insn(I), Shape(S) {}
124 using MapInsnToMemAcc = std::map<const Instruction *, MemAcc>;
125 using PairInstSCEV = std::pair<const Instruction *, const SCEV *>;
126 using AFs = std::vector<PairInstSCEV>;
127 using BaseToAFs = std::map<const SCEVUnknown *, AFs>;
128 using BaseToElSize = std::map<const SCEVUnknown *, const SCEV *>;
130 extern bool PollyTrackFailures;
131 extern bool PollyDelinearize;
132 extern bool PollyUseRuntimeAliasChecks;
133 extern bool PollyProcessUnprofitable;
134 extern bool PollyInvariantLoadHoisting;
135 extern bool PollyAllowUnsignedOperations;
136 extern bool PollyAllowFullFunction;
138 /// A function attribute which will cause Polly to skip the function
139 extern StringRef PollySkipFnAttr;
141 //===----------------------------------------------------------------------===//
142 /// Pass to detect the maximal static control parts (Scops) of a
143 /// function.
144 class ScopDetection {
145 public:
146 using RegionSet = SetVector<const Region *>;
148 // Remember the valid regions
149 RegionSet ValidRegions;
151 /// Context variables for SCoP detection.
152 struct DetectionContext {
153 Region &CurRegion; // The region to check.
154 AliasSetTracker AST; // The AliasSetTracker to hold the alias information.
155 bool Verifying; // If we are in the verification phase?
157 /// Container to remember rejection reasons for this region.
158 RejectLog Log;
160 /// Map a base pointer to all access functions accessing it.
162 /// This map is indexed by the base pointer. Each element of the map
163 /// is a list of memory accesses that reference this base pointer.
164 BaseToAFs Accesses;
166 /// The set of base pointers with non-affine accesses.
168 /// This set contains all base pointers and the locations where they are
169 /// used for memory accesses that can not be detected as affine accesses.
170 SetVector<std::pair<const SCEVUnknown *, Loop *>> NonAffineAccesses;
171 BaseToElSize ElementSize;
173 /// The region has at least one load instruction.
174 bool hasLoads = false;
176 /// The region has at least one store instruction.
177 bool hasStores = false;
179 /// Flag to indicate the region has at least one unknown access.
180 bool HasUnknownAccess = false;
182 /// The set of non-affine subregions in the region we analyze.
183 RegionSet NonAffineSubRegionSet;
185 /// The set of loops contained in non-affine regions.
186 BoxedLoopsSetTy BoxedLoopsSet;
188 /// Loads that need to be invariant during execution.
189 InvariantLoadsSetTy RequiredILS;
191 /// Map to memory access description for the corresponding LLVM
192 /// instructions.
193 MapInsnToMemAcc InsnToMemAcc;
195 /// Initialize a DetectionContext from scratch.
196 DetectionContext(Region &R, AliasAnalysis &AA, bool Verify)
197 : CurRegion(R), AST(AA), Verifying(Verify), Log(&R) {}
199 /// Initialize a DetectionContext with the data from @p DC.
200 DetectionContext(const DetectionContext &&DC)
201 : CurRegion(DC.CurRegion), AST(DC.AST.getAliasAnalysis()),
202 Verifying(DC.Verifying), Log(std::move(DC.Log)),
203 Accesses(std::move(DC.Accesses)),
204 NonAffineAccesses(std::move(DC.NonAffineAccesses)),
205 ElementSize(std::move(DC.ElementSize)), hasLoads(DC.hasLoads),
206 hasStores(DC.hasStores), HasUnknownAccess(DC.HasUnknownAccess),
207 NonAffineSubRegionSet(std::move(DC.NonAffineSubRegionSet)),
208 BoxedLoopsSet(std::move(DC.BoxedLoopsSet)),
209 RequiredILS(std::move(DC.RequiredILS)) {
210 AST.add(DC.AST);
214 /// Helper data structure to collect statistics about loop counts.
215 struct LoopStats {
216 int NumLoops;
217 int MaxDepth;
220 private:
221 //===--------------------------------------------------------------------===//
223 /// Analyses used
224 //@{
225 const DominatorTree &DT;
226 ScalarEvolution &SE;
227 LoopInfo &LI;
228 RegionInfo &RI;
229 AliasAnalysis &AA;
230 //@}
232 /// Map to remember detection contexts for all regions.
233 using DetectionContextMapTy = DenseMap<BBPair, DetectionContext>;
234 mutable DetectionContextMapTy DetectionContextMap;
236 /// Remove cached results for @p R.
237 void removeCachedResults(const Region &R);
239 /// Remove cached results for the children of @p R recursively.
240 void removeCachedResultsRecursively(const Region &R);
242 /// Check if @p S0 and @p S1 do contain multiple possibly aliasing pointers.
244 /// @param S0 A expression to check.
245 /// @param S1 Another expression to check or nullptr.
246 /// @param Scope The loop/scope the expressions are checked in.
248 /// @returns True, if multiple possibly aliasing pointers are used in @p S0
249 /// (and @p S1 if given).
250 bool involvesMultiplePtrs(const SCEV *S0, const SCEV *S1, Loop *Scope) const;
252 /// Add the region @p AR as over approximated sub-region in @p Context.
254 /// @param AR The non-affine subregion.
255 /// @param Context The current detection context.
257 /// @returns True if the subregion can be over approximated, false otherwise.
258 bool addOverApproximatedRegion(Region *AR, DetectionContext &Context) const;
260 /// Find for a given base pointer terms that hint towards dimension
261 /// sizes of a multi-dimensional array.
263 /// @param Context The current detection context.
264 /// @param BasePointer A base pointer indicating the virtual array we are
265 /// interested in.
266 SmallVector<const SCEV *, 4>
267 getDelinearizationTerms(DetectionContext &Context,
268 const SCEVUnknown *BasePointer) const;
270 /// Check if the dimension size of a delinearized array is valid.
272 /// @param Context The current detection context.
273 /// @param Sizes The sizes of the different array dimensions.
274 /// @param BasePointer The base pointer we are interested in.
275 /// @param Scope The location where @p BasePointer is being used.
276 /// @returns True if one or more array sizes could be derived - meaning: we
277 /// see this array as multi-dimensional.
278 bool hasValidArraySizes(DetectionContext &Context,
279 SmallVectorImpl<const SCEV *> &Sizes,
280 const SCEVUnknown *BasePointer, Loop *Scope) const;
282 /// Derive access functions for a given base pointer.
284 /// @param Context The current detection context.
285 /// @param Sizes The sizes of the different array dimensions.
286 /// @param BasePointer The base pointer of all the array for which to compute
287 /// access functions.
288 /// @param Shape The shape that describes the derived array sizes and
289 /// which should be filled with newly computed access
290 /// functions.
291 /// @returns True if a set of affine access functions could be derived.
292 bool computeAccessFunctions(DetectionContext &Context,
293 const SCEVUnknown *BasePointer,
294 std::shared_ptr<ArrayShape> Shape) const;
296 /// Check if all accesses to a given BasePointer are affine.
298 /// @param Context The current detection context.
299 /// @param BasePointer the base pointer we are interested in.
300 /// @param Scope The location where @p BasePointer is being used.
301 /// @param True if consistent (multi-dimensional) array accesses could be
302 /// derived for this array.
303 bool hasBaseAffineAccesses(DetectionContext &Context,
304 const SCEVUnknown *BasePointer, Loop *Scope) const;
306 // Delinearize all non affine memory accesses and return false when there
307 // exists a non affine memory access that cannot be delinearized. Return true
308 // when all array accesses are affine after delinearization.
309 bool hasAffineMemoryAccesses(DetectionContext &Context) const;
311 // Try to expand the region R. If R can be expanded return the expanded
312 // region, NULL otherwise.
313 Region *expandRegion(Region &R);
315 /// Find the Scops in this region tree.
317 /// @param The region tree to scan for scops.
318 void findScops(Region &R);
320 /// Check if all basic block in the region are valid.
322 /// @param Context The context of scop detection.
324 /// @return True if all blocks in R are valid, false otherwise.
325 bool allBlocksValid(DetectionContext &Context) const;
327 /// Check if a region has sufficient compute instructions.
329 /// This function checks if a region has a non-trivial number of instructions
330 /// in each loop. This can be used as an indicator whether a loop is worth
331 /// optimizing.
333 /// @param Context The context of scop detection.
334 /// @param NumLoops The number of loops in the region.
336 /// @return True if region is has sufficient compute instructions,
337 /// false otherwise.
338 bool hasSufficientCompute(DetectionContext &Context,
339 int NumAffineLoops) const;
341 /// Check if the unique affine loop might be amendable to distribution.
343 /// This function checks if the number of non-trivial blocks in the unique
344 /// affine loop in Context.CurRegion is at least two, thus if the loop might
345 /// be amendable to distribution.
347 /// @param Context The context of scop detection.
349 /// @return True only if the affine loop might be amendable to distributable.
350 bool hasPossiblyDistributableLoop(DetectionContext &Context) const;
352 /// Check if a region is profitable to optimize.
354 /// Regions that are unlikely to expose interesting optimization opportunities
355 /// are called 'unprofitable' and may be skipped during scop detection.
357 /// @param Context The context of scop detection.
359 /// @return True if region is profitable to optimize, false otherwise.
360 bool isProfitableRegion(DetectionContext &Context) const;
362 /// Check if a region is a Scop.
364 /// @param Context The context of scop detection.
366 /// @return True if R is a Scop, false otherwise.
367 bool isValidRegion(DetectionContext &Context) const;
369 /// Check if an intrinsic call can be part of a Scop.
371 /// @param II The intrinsic call instruction to check.
372 /// @param Context The current detection context.
374 /// @return True if the call instruction is valid, false otherwise.
375 bool isValidIntrinsicInst(IntrinsicInst &II, DetectionContext &Context) const;
377 /// Check if a call instruction can be part of a Scop.
379 /// @param CI The call instruction to check.
380 /// @param Context The current detection context.
382 /// @return True if the call instruction is valid, false otherwise.
383 bool isValidCallInst(CallInst &CI, DetectionContext &Context) const;
385 /// Check if the given loads could be invariant and can be hoisted.
387 /// If true is returned the loads are added to the required invariant loads
388 /// contained in the @p Context.
390 /// @param RequiredILS The loads to check.
391 /// @param Context The current detection context.
393 /// @return True if all loads can be assumed invariant.
394 bool onlyValidRequiredInvariantLoads(InvariantLoadsSetTy &RequiredILS,
395 DetectionContext &Context) const;
397 /// Check if a value is invariant in the region Reg.
399 /// @param Val Value to check for invariance.
400 /// @param Reg The region to consider for the invariance of Val.
401 /// @param Ctx The current detection context.
403 /// @return True if the value represented by Val is invariant in the region
404 /// identified by Reg.
405 bool isInvariant(Value &Val, const Region &Reg, DetectionContext &Ctx) const;
407 /// Check if the memory access caused by @p Inst is valid.
409 /// @param Inst The access instruction.
410 /// @param AF The access function.
411 /// @param BP The access base pointer.
412 /// @param Context The current detection context.
413 bool isValidAccess(Instruction *Inst, const SCEV *AF, const SCEVUnknown *BP,
414 DetectionContext &Context) const;
416 /// Check if a memory access can be part of a Scop.
418 /// @param Inst The instruction accessing the memory.
419 /// @param Context The context of scop detection.
421 /// @return True if the memory access is valid, false otherwise.
422 bool isValidMemoryAccess(MemAccInst Inst, DetectionContext &Context) const;
424 /// Check if an instruction has any non trivial scalar dependencies as part of
425 /// a Scop.
427 /// @param Inst The instruction to check.
428 /// @param RefRegion The region in respect to which we check the access
429 /// function.
431 /// @return True if the instruction has scalar dependences, false otherwise.
432 bool hasScalarDependency(Instruction &Inst, Region &RefRegion) const;
434 /// Check if an instruction can be part of a Scop.
436 /// @param Inst The instruction to check.
437 /// @param Context The context of scop detection.
439 /// @return True if the instruction is valid, false otherwise.
440 bool isValidInstruction(Instruction &Inst, DetectionContext &Context) const;
442 /// Check if the switch @p SI with condition @p Condition is valid.
444 /// @param BB The block to check.
445 /// @param SI The switch to check.
446 /// @param Condition The switch condition.
447 /// @param IsLoopBranch Flag to indicate the branch is a loop exit/latch.
448 /// @param Context The context of scop detection.
450 /// @return True if the branch @p BI is valid.
451 bool isValidSwitch(BasicBlock &BB, SwitchInst *SI, Value *Condition,
452 bool IsLoopBranch, DetectionContext &Context) const;
454 /// Check if the branch @p BI with condition @p Condition is valid.
456 /// @param BB The block to check.
457 /// @param BI The branch to check.
458 /// @param Condition The branch condition.
459 /// @param IsLoopBranch Flag to indicate the branch is a loop exit/latch.
460 /// @param Context The context of scop detection.
462 /// @return True if the branch @p BI is valid.
463 bool isValidBranch(BasicBlock &BB, BranchInst *BI, Value *Condition,
464 bool IsLoopBranch, DetectionContext &Context) const;
466 /// Check if the SCEV @p S is affine in the current @p Context.
468 /// This will also use a heuristic to decide if we want to require loads to be
469 /// invariant to make the expression affine or if we want to treat is as
470 /// non-affine.
472 /// @param S The expression to be checked.
473 /// @param Scope The loop nest in which @p S is used.
474 /// @param Context The context of scop detection.
475 bool isAffine(const SCEV *S, Loop *Scope, DetectionContext &Context) const;
477 /// Check if the control flow in a basic block is valid.
479 /// This function checks if a certain basic block is terminated by a
480 /// Terminator instruction we can handle or, if this is not the case,
481 /// registers this basic block as the start of a non-affine region.
483 /// This function optionally allows unreachable statements.
485 /// @param BB The BB to check the control flow.
486 /// @param IsLoopBranch Flag to indicate the branch is a loop exit/latch.
487 // @param AllowUnreachable Allow unreachable statements.
488 /// @param Context The context of scop detection.
490 /// @return True if the BB contains only valid control flow.
491 bool isValidCFG(BasicBlock &BB, bool IsLoopBranch, bool AllowUnreachable,
492 DetectionContext &Context) const;
494 /// Is a loop valid with respect to a given region.
496 /// @param L The loop to check.
497 /// @param Context The context of scop detection.
499 /// @return True if the loop is valid in the region.
500 bool isValidLoop(Loop *L, DetectionContext &Context) const;
502 /// Count the number of loops and the maximal loop depth in @p L.
504 /// @param L The loop to check.
505 /// @param SE The scalar evolution analysis.
506 /// @param MinProfitableTrips The minimum number of trip counts from which
507 /// a loop is assumed to be profitable and
508 /// consequently is counted.
509 /// returns A tuple of number of loops and their maximal depth.
510 static ScopDetection::LoopStats
511 countBeneficialSubLoops(Loop *L, ScalarEvolution &SE,
512 unsigned MinProfitableTrips);
514 /// Check if the function @p F is marked as invalid.
516 /// @note An OpenMP subfunction will be marked as invalid.
517 bool isValidFunction(Function &F);
519 /// Can ISL compute the trip count of a loop.
521 /// @param L The loop to check.
522 /// @param Context The context of scop detection.
524 /// @return True if ISL can compute the trip count of the loop.
525 bool canUseISLTripCount(Loop *L, DetectionContext &Context) const;
527 /// Print the locations of all detected scops.
528 void printLocations(Function &F);
530 /// Check if a region is reducible or not.
532 /// @param Region The region to check.
533 /// @param DbgLoc Parameter to save the location of instruction that
534 /// causes irregular control flow if the region is irreducible.
536 /// @return True if R is reducible, false otherwise.
537 bool isReducibleRegion(Region &R, DebugLoc &DbgLoc) const;
539 /// Track diagnostics for invalid scops.
541 /// @param Context The context of scop detection.
542 /// @param Assert Throw an assert in verify mode or not.
543 /// @param Args Argument list that gets passed to the constructor of RR.
544 template <class RR, typename... Args>
545 inline bool invalid(DetectionContext &Context, bool Assert,
546 Args &&... Arguments) const;
548 public:
549 ScopDetection(Function &F, const DominatorTree &DT, ScalarEvolution &SE,
550 LoopInfo &LI, RegionInfo &RI, AliasAnalysis &AA,
551 OptimizationRemarkEmitter &ORE);
553 /// Get the RegionInfo stored in this pass.
555 /// This was added to give the DOT printer easy access to this information.
556 RegionInfo *getRI() const { return &RI; }
558 /// Get the LoopInfo stored in this pass.
559 LoopInfo *getLI() const { return &LI; }
561 /// Is the region is the maximum region of a Scop?
563 /// @param R The Region to test if it is maximum.
564 /// @param Verify Rerun the scop detection to verify SCoP was not invalidated
565 /// meanwhile.
567 /// @return Return true if R is the maximum Region in a Scop, false otherwise.
568 bool isMaxRegionInScop(const Region &R, bool Verify = true) const;
570 /// Return the detection context for @p R, nullptr if @p R was invalid.
571 DetectionContext *getDetectionContext(const Region *R) const;
573 /// Return the set of rejection causes for @p R.
574 const RejectLog *lookupRejectionLog(const Region *R) const;
576 /// Return true if @p SubR is a non-affine subregion in @p ScopR.
577 bool isNonAffineSubRegion(const Region *SubR, const Region *ScopR) const;
579 /// Get a message why a region is invalid
581 /// @param R The region for which we get the error message
583 /// @return The error or "" if no error appeared.
584 std::string regionIsInvalidBecause(const Region *R) const;
586 /// @name Maximum Region In Scops Iterators
588 /// These iterators iterator over all maximum region in Scops of this
589 /// function.
590 //@{
591 using iterator = RegionSet::iterator;
592 using const_iterator = RegionSet::const_iterator;
594 iterator begin() { return ValidRegions.begin(); }
595 iterator end() { return ValidRegions.end(); }
597 const_iterator begin() const { return ValidRegions.begin(); }
598 const_iterator end() const { return ValidRegions.end(); }
599 //@}
601 /// Emit rejection remarks for all rejected regions.
603 /// @param F The function to emit remarks for.
604 void emitMissedRemarks(const Function &F);
606 /// Mark the function as invalid so we will not extract any scop from
607 /// the function.
609 /// @param F The function to mark as invalid.
610 static void markFunctionAsInvalid(Function *F);
612 /// Verify if all valid Regions in this Function are still valid
613 /// after some transformations.
614 void verifyAnalysis() const;
616 /// Verify if R is still a valid part of Scop after some transformations.
618 /// @param R The Region to verify.
619 void verifyRegion(const Region &R) const;
621 /// Count the number of loops and the maximal loop depth in @p R.
623 /// @param R The region to check
624 /// @param SE The scalar evolution analysis.
625 /// @param MinProfitableTrips The minimum number of trip counts from which
626 /// a loop is assumed to be profitable and
627 /// consequently is counted.
628 /// returns A tuple of number of loops and their maximal depth.
629 static ScopDetection::LoopStats
630 countBeneficialLoops(Region *R, ScalarEvolution &SE, LoopInfo &LI,
631 unsigned MinProfitableTrips);
633 private:
634 /// OptimizationRemarkEmitter object used to emit diagnostic remarks
635 OptimizationRemarkEmitter &ORE;
638 struct ScopAnalysis : public AnalysisInfoMixin<ScopAnalysis> {
639 static AnalysisKey Key;
641 using Result = ScopDetection;
643 ScopAnalysis();
645 Result run(Function &F, FunctionAnalysisManager &FAM);
648 struct ScopAnalysisPrinterPass : public PassInfoMixin<ScopAnalysisPrinterPass> {
649 ScopAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
651 PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
653 raw_ostream &OS;
656 struct ScopDetectionWrapperPass : public FunctionPass {
657 static char ID;
658 std::unique_ptr<ScopDetection> Result;
660 ScopDetectionWrapperPass();
662 /// @name FunctionPass interface
663 //@{
664 void getAnalysisUsage(AnalysisUsage &AU) const override;
665 void releaseMemory() override;
666 bool runOnFunction(Function &F) override;
667 void print(raw_ostream &OS, const Module *) const override;
668 //@}
670 ScopDetection &getSD() { return *Result; }
671 const ScopDetection &getSD() const { return *Result; }
674 } // namespace polly
676 #endif // POLLY_SCOPDETECTION_H