GPGPU: Bail out of scops with hoisted invariant loads
[polly-mirror.git] / include / polly / ScopInfo.h
blobaecab88b59181c7194f1daa4ec5bb18a801f3e9d
1 //===------ polly/ScopInfo.h -----------------------------------*- 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 // Store the polyhedral model representation of a static control flow region,
11 // also called SCoP (Static Control Part).
13 // This representation is shared among several tools in the polyhedral
14 // community, which are e.g. CLooG, Pluto, Loopo, Graphite.
16 //===----------------------------------------------------------------------===//
18 #ifndef POLLY_SCOP_INFO_H
19 #define POLLY_SCOP_INFO_H
21 #include "polly/ScopDetection.h"
22 #include "polly/Support/SCEVAffinator.h"
24 #include "llvm/ADT/MapVector.h"
25 #include "llvm/Analysis/RegionPass.h"
26 #include "isl/aff.h"
27 #include "isl/ctx.h"
28 #include "isl/set.h"
30 #include <deque>
31 #include <forward_list>
33 using namespace llvm;
35 namespace llvm {
36 class AssumptionCache;
37 class Loop;
38 class LoopInfo;
39 class PHINode;
40 class ScalarEvolution;
41 class SCEV;
42 class SCEVAddRecExpr;
43 class Type;
44 } // namespace llvm
46 struct isl_ctx;
47 struct isl_map;
48 struct isl_basic_map;
49 struct isl_id;
50 struct isl_set;
51 struct isl_union_set;
52 struct isl_union_map;
53 struct isl_space;
54 struct isl_ast_build;
55 struct isl_constraint;
56 struct isl_pw_aff;
57 struct isl_pw_multi_aff;
58 struct isl_schedule;
60 namespace polly {
62 class MemoryAccess;
63 class Scop;
64 class ScopStmt;
65 class ScopBuilder;
67 //===---------------------------------------------------------------------===//
69 /// @brief Enumeration of assumptions Polly can take.
70 enum AssumptionKind {
71 ALIASING,
72 INBOUNDS,
73 WRAPPING,
74 UNSIGNED,
75 PROFITABLE,
76 ERRORBLOCK,
77 COMPLEXITY,
78 INFINITELOOP,
79 INVARIANTLOAD,
80 DELINEARIZATION,
83 /// @brief Enum to distinguish between assumptions and restrictions.
84 enum AssumptionSign { AS_ASSUMPTION, AS_RESTRICTION };
86 /// Maps from a loop to the affine function expressing its backedge taken count.
87 /// The backedge taken count already enough to express iteration domain as we
88 /// only allow loops with canonical induction variable.
89 /// A canonical induction variable is:
90 /// an integer recurrence that starts at 0 and increments by one each time
91 /// through the loop.
92 typedef std::map<const Loop *, const SCEV *> LoopBoundMapType;
94 typedef std::deque<MemoryAccess> AccFuncSetType;
95 typedef std::map<const BasicBlock *, AccFuncSetType> AccFuncMapType;
97 /// @brief A class to store information about arrays in the SCoP.
98 ///
99 /// Objects are accessible via the ScoP, MemoryAccess or the id associated with
100 /// the MemoryAccess access function.
102 class ScopArrayInfo {
103 public:
104 /// @brief The kind of a ScopArrayInfo memory object.
106 /// We distinguish between arrays and various scalar memory objects. We use
107 /// the term ``array'' to describe memory objects that consist of a set of
108 /// individual data elements arranged in a multi-dimensional grid. A scalar
109 /// memory object describes an individual data element and is used to model
110 /// the definition and uses of llvm::Values.
112 /// The polyhedral model does traditionally not reason about SSA values. To
113 /// reason about llvm::Values we model them "as if" they were zero-dimensional
114 /// memory objects, even though they were not actually allocated in (main)
115 /// memory. Memory for such objects is only alloca[ed] at CodeGeneration
116 /// time. To relate the memory slots used during code generation with the
117 /// llvm::Values they belong to the new names for these corresponding stack
118 /// slots are derived by appending suffixes (currently ".s2a" and ".phiops")
119 /// to the name of the original llvm::Value. To describe how def/uses are
120 /// modeled exactly we use these suffixes here as well.
122 /// There are currently four different kinds of memory objects:
123 enum MemoryKind {
124 /// MK_Array: Models a one or multi-dimensional array
126 /// A memory object that can be described by a multi-dimensional array.
127 /// Memory objects of this type are used to model actual multi-dimensional
128 /// arrays as they exist in LLVM-IR, but they are also used to describe
129 /// other objects:
130 /// - A single data element allocated on the stack using 'alloca' is
131 /// modeled as a one-dimensional, single-element array.
132 /// - A single data element allocated as a global variable is modeled as
133 /// one-dimensional, single-element array.
134 /// - Certain multi-dimensional arrays with variable size, which in
135 /// LLVM-IR are commonly expressed as a single-dimensional access with a
136 /// complicated access function, are modeled as multi-dimensional
137 /// memory objects (grep for "delinearization").
138 MK_Array,
140 /// MK_Value: Models an llvm::Value
142 /// Memory objects of type MK_Value are used to model the data flow
143 /// induced by llvm::Values. For each llvm::Value that is used across
144 /// BasicBocks one ScopArrayInfo object is created. A single memory WRITE
145 /// stores the llvm::Value at its definition into the memory object and at
146 /// each use of the llvm::Value (ignoring trivial intra-block uses) a
147 /// corresponding READ is added. For instance, the use/def chain of a
148 /// llvm::Value %V depicted below
149 /// ______________________
150 /// |DefBB: |
151 /// | %V = float op ... |
152 /// ----------------------
153 /// | |
154 /// _________________ _________________
155 /// |UseBB1: | |UseBB2: |
156 /// | use float %V | | use float %V |
157 /// ----------------- -----------------
159 /// is modeled as if the following memory accesses occured:
161 /// __________________________
162 /// |entry: |
163 /// | %V.s2a = alloca float |
164 /// --------------------------
165 /// |
166 /// ___________________________________
167 /// |DefBB: |
168 /// | store %float %V, float* %V.s2a |
169 /// -----------------------------------
170 /// | |
171 /// ____________________________________ ___________________________________
172 /// |UseBB1: | |UseBB2: |
173 /// | %V.reload1 = load float* %V.s2a | | %V.reload2 = load float* %V.s2a|
174 /// | use float %V.reload1 | | use float %V.reload2 |
175 /// ------------------------------------ -----------------------------------
177 MK_Value,
179 /// MK_PHI: Models PHI nodes within the SCoP
181 /// Besides the MK_Value memory object used to model the normal
182 /// llvm::Value dependences described above, PHI nodes require an additional
183 /// memory object of type MK_PHI to describe the forwarding of values to
184 /// the PHI node.
186 /// As an example, a PHIInst instructions
188 /// %PHI = phi float [ %Val1, %IncomingBlock1 ], [ %Val2, %IncomingBlock2 ]
190 /// is modeled as if the accesses occured this way:
192 /// _______________________________
193 /// |entry: |
194 /// | %PHI.phiops = alloca float |
195 /// -------------------------------
196 /// | |
197 /// __________________________________ __________________________________
198 /// |IncomingBlock1: | |IncomingBlock2: |
199 /// | ... | | ... |
200 /// | store float %Val1 %PHI.phiops | | store float %Val2 %PHI.phiops |
201 /// | br label % JoinBlock | | br label %JoinBlock |
202 /// ---------------------------------- ----------------------------------
203 /// \ /
204 /// \ /
205 /// _________________________________________
206 /// |JoinBlock: |
207 /// | %PHI = load float, float* PHI.phiops |
208 /// -----------------------------------------
210 /// Note that there can also be a scalar write access for %PHI if used in a
211 /// different BasicBlock, i.e. there can be a memory object %PHI.phiops as
212 /// well as a memory object %PHI.s2a.
213 MK_PHI,
215 /// MK_ExitPHI: Models PHI nodes in the SCoP's exit block
217 /// For PHI nodes in the Scop's exit block a special memory object kind is
218 /// used. The modeling used is identical to MK_PHI, with the exception
219 /// that there are no READs from these memory objects. The PHINode's
220 /// llvm::Value is treated as a value escaping the SCoP. WRITE accesses
221 /// write directly to the escaping value's ".s2a" alloca.
222 MK_ExitPHI
225 /// @brief Construct a ScopArrayInfo object.
227 /// @param BasePtr The array base pointer.
228 /// @param ElementType The type of the elements stored in the array.
229 /// @param IslCtx The isl context used to create the base pointer id.
230 /// @param DimensionSizes A vector containing the size of each dimension.
231 /// @param Kind The kind of the array object.
232 /// @param DL The data layout of the module.
233 /// @param S The scop this array object belongs to.
234 ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *IslCtx,
235 ArrayRef<const SCEV *> DimensionSizes, enum MemoryKind Kind,
236 const DataLayout &DL, Scop *S);
238 /// @brief Update the element type of the ScopArrayInfo object.
240 /// Memory accesses referencing this ScopArrayInfo object may use
241 /// different element sizes. This function ensures the canonical element type
242 /// stored is small enough to model accesses to the current element type as
243 /// well as to @p NewElementType.
245 /// @param NewElementType An element type that is used to access this array.
246 void updateElementType(Type *NewElementType);
248 /// @brief Update the sizes of the ScopArrayInfo object.
250 /// A ScopArrayInfo object may be created without all outer dimensions being
251 /// available. This function is called when new memory accesses are added for
252 /// this ScopArrayInfo object. It verifies that sizes are compatible and adds
253 /// additional outer array dimensions, if needed.
255 /// @param Sizes A vector of array sizes where the rightmost array
256 /// sizes need to match the innermost array sizes already
257 /// defined in SAI.
258 bool updateSizes(ArrayRef<const SCEV *> Sizes);
260 /// @brief Destructor to free the isl id of the base pointer.
261 ~ScopArrayInfo();
263 /// @brief Set the base pointer to @p BP.
264 void setBasePtr(Value *BP) { BasePtr = BP; }
266 /// @brief Return the base pointer.
267 Value *getBasePtr() const { return BasePtr; }
269 /// @brief For indirect accesses return the origin SAI of the BP, else null.
270 const ScopArrayInfo *getBasePtrOriginSAI() const { return BasePtrOriginSAI; }
272 /// @brief The set of derived indirect SAIs for this origin SAI.
273 const SmallPtrSetImpl<ScopArrayInfo *> &getDerivedSAIs() const {
274 return DerivedSAIs;
277 /// @brief Return the number of dimensions.
278 unsigned getNumberOfDimensions() const {
279 if (Kind == MK_PHI || Kind == MK_ExitPHI || Kind == MK_Value)
280 return 0;
281 return DimensionSizes.size() + 1;
284 /// @brief Return the size of dimension @p dim as SCEV*.
286 // Scalars do not have array dimensions and the first dimension of
287 // a (possibly multi-dimensional) array also does not carry any size
288 // information.
289 const SCEV *getDimensionSize(unsigned Dim) const {
290 assert(Dim > 0 && "Only dimensions larger than zero are sized.");
291 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
292 return DimensionSizes[Dim - 1];
295 /// @brief Return the size of dimension @p dim as isl_pw_aff.
297 // Scalars do not have array dimensions and the first dimension of
298 // a (possibly multi-dimensional) array also does not carry any size
299 // information.
300 __isl_give isl_pw_aff *getDimensionSizePw(unsigned Dim) const {
301 assert(Dim > 0 && "Only dimensions larger than zero are sized.");
302 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
303 return isl_pw_aff_copy(DimensionSizesPw[Dim - 1]);
306 /// @brief Get the canonical element type of this array.
308 /// @returns The canonical element type of this array.
309 Type *getElementType() const { return ElementType; }
311 /// @brief Get element size in bytes.
312 int getElemSizeInBytes() const;
314 /// @brief Get the name of this memory reference.
315 std::string getName() const;
317 /// @brief Return the isl id for the base pointer.
318 __isl_give isl_id *getBasePtrId() const;
320 /// @brief Is this array info modeling an llvm::Value?
321 bool isValueKind() const { return Kind == MK_Value; }
323 /// @brief Is this array info modeling special PHI node memory?
325 /// During code generation of PHI nodes, there is a need for two kinds of
326 /// virtual storage. The normal one as it is used for all scalar dependences,
327 /// where the result of the PHI node is stored and later loaded from as well
328 /// as a second one where the incoming values of the PHI nodes are stored
329 /// into and reloaded when the PHI is executed. As both memories use the
330 /// original PHI node as virtual base pointer, we have this additional
331 /// attribute to distinguish the PHI node specific array modeling from the
332 /// normal scalar array modeling.
333 bool isPHIKind() const { return Kind == MK_PHI; }
335 /// @brief Is this array info modeling an MK_ExitPHI?
336 bool isExitPHIKind() const { return Kind == MK_ExitPHI; }
338 /// @brief Is this array info modeling an array?
339 bool isArrayKind() const { return Kind == MK_Array; }
341 /// @brief Dump a readable representation to stderr.
342 void dump() const;
344 /// @brief Print a readable representation to @p OS.
346 /// @param SizeAsPwAff Print the size as isl_pw_aff
347 void print(raw_ostream &OS, bool SizeAsPwAff = false) const;
349 /// @brief Access the ScopArrayInfo associated with an access function.
350 static const ScopArrayInfo *
351 getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA);
353 /// @brief Access the ScopArrayInfo associated with an isl Id.
354 static const ScopArrayInfo *getFromId(__isl_take isl_id *Id);
356 /// @brief Get the space of this array access.
357 __isl_give isl_space *getSpace() const;
359 private:
360 void addDerivedSAI(ScopArrayInfo *DerivedSAI) {
361 DerivedSAIs.insert(DerivedSAI);
364 /// @brief For indirect accesses this is the SAI of the BP origin.
365 const ScopArrayInfo *BasePtrOriginSAI;
367 /// @brief For origin SAIs the set of derived indirect SAIs.
368 SmallPtrSet<ScopArrayInfo *, 2> DerivedSAIs;
370 /// @brief The base pointer.
371 AssertingVH<Value> BasePtr;
373 /// @brief The canonical element type of this array.
375 /// The canonical element type describes the minimal accessible element in
376 /// this array. Not all elements accessed, need to be of the very same type,
377 /// but the allocation size of the type of the elements loaded/stored from/to
378 /// this array needs to be a multiple of the allocation size of the canonical
379 /// type.
380 Type *ElementType;
382 /// @brief The isl id for the base pointer.
383 isl_id *Id;
385 /// @brief The sizes of each dimension as SCEV*.
386 SmallVector<const SCEV *, 4> DimensionSizes;
388 /// @brief The sizes of each dimension as isl_pw_aff.
389 SmallVector<isl_pw_aff *, 4> DimensionSizesPw;
391 /// @brief The type of this scop array info object.
393 /// We distinguish between SCALAR, PHI and ARRAY objects.
394 enum MemoryKind Kind;
396 /// @brief The data layout of the module.
397 const DataLayout &DL;
399 /// @brief The scop this SAI object belongs to.
400 Scop &S;
403 /// @brief Represent memory accesses in statements.
404 class MemoryAccess {
405 friend class Scop;
406 friend class ScopStmt;
408 public:
409 /// @brief The access type of a memory access
411 /// There are three kind of access types:
413 /// * A read access
415 /// A certain set of memory locations are read and may be used for internal
416 /// calculations.
418 /// * A must-write access
420 /// A certain set of memory locations is definitely written. The old value is
421 /// replaced by a newly calculated value. The old value is not read or used at
422 /// all.
424 /// * A may-write access
426 /// A certain set of memory locations may be written. The memory location may
427 /// contain a new value if there is actually a write or the old value may
428 /// remain, if no write happens.
429 enum AccessType {
430 READ = 0x1,
431 MUST_WRITE = 0x2,
432 MAY_WRITE = 0x3,
435 /// @brief Reduction access type
437 /// Commutative and associative binary operations suitable for reductions
438 enum ReductionType {
439 RT_NONE, ///< Indicate no reduction at all
440 RT_ADD, ///< Addition
441 RT_MUL, ///< Multiplication
442 RT_BOR, ///< Bitwise Or
443 RT_BXOR, ///< Bitwise XOr
444 RT_BAND, ///< Bitwise And
447 private:
448 MemoryAccess(const MemoryAccess &) = delete;
449 const MemoryAccess &operator=(const MemoryAccess &) = delete;
451 /// @brief A unique identifier for this memory access.
453 /// The identifier is unique between all memory accesses belonging to the same
454 /// scop statement.
455 isl_id *Id;
457 /// @brief What is modeled by this MemoryAccess.
458 /// @see ScopArrayInfo::MemoryKind
459 ScopArrayInfo::MemoryKind Kind;
461 /// @brief Whether it a reading or writing access, and if writing, whether it
462 /// is conditional (MAY_WRITE).
463 enum AccessType AccType;
465 /// @brief Reduction type for reduction like accesses, RT_NONE otherwise
467 /// An access is reduction like if it is part of a load-store chain in which
468 /// both access the same memory location (use the same LLVM-IR value
469 /// as pointer reference). Furthermore, between the load and the store there
470 /// is exactly one binary operator which is known to be associative and
471 /// commutative.
473 /// TODO:
475 /// We can later lift the constraint that the same LLVM-IR value defines the
476 /// memory location to handle scops such as the following:
478 /// for i
479 /// for j
480 /// sum[i+j] = sum[i] + 3;
482 /// Here not all iterations access the same memory location, but iterations
483 /// for which j = 0 holds do. After lifting the equality check in ScopBuilder,
484 /// subsequent transformations do not only need check if a statement is
485 /// reduction like, but they also need to verify that that the reduction
486 /// property is only exploited for statement instances that load from and
487 /// store to the same data location. Doing so at dependence analysis time
488 /// could allow us to handle the above example.
489 ReductionType RedType = RT_NONE;
491 /// @brief Parent ScopStmt of this access.
492 ScopStmt *Statement;
494 /// @brief The domain under which this access is not modeled precisely.
496 /// The invalid domain for an access describes all parameter combinations
497 /// under which the statement looks to be executed but is in fact not because
498 /// some assumption/restriction makes the access invalid.
499 isl_set *InvalidDomain;
501 // Properties describing the accessed array.
502 // TODO: It might be possible to move them to ScopArrayInfo.
503 // @{
505 /// @brief The base address (e.g., A for A[i+j]).
507 /// The #BaseAddr of a memory access of kind MK_Array is the base pointer
508 /// of the memory access.
509 /// The #BaseAddr of a memory access of kind MK_PHI or MK_ExitPHI is the
510 /// PHI node itself.
511 /// The #BaseAddr of a memory access of kind MK_Value is the instruction
512 /// defining the value.
513 AssertingVH<Value> BaseAddr;
515 /// @brief An unique name of the accessed array.
516 std::string BaseName;
518 /// @brief Type a single array element wrt. this access.
519 Type *ElementType;
521 /// @brief Size of each dimension of the accessed array.
522 SmallVector<const SCEV *, 4> Sizes;
523 // @}
525 // Properties describing the accessed element.
526 // @{
528 /// @brief The access instruction of this memory access.
530 /// For memory accesses of kind MK_Array the access instruction is the
531 /// Load or Store instruction performing the access.
533 /// For memory accesses of kind MK_PHI or MK_ExitPHI the access
534 /// instruction of a load access is the PHI instruction. The access
535 /// instruction of a PHI-store is the incoming's block's terminator
536 /// intruction.
538 /// For memory accesses of kind MK_Value the access instruction of a load
539 /// access is nullptr because generally there can be multiple instructions in
540 /// the statement using the same llvm::Value. The access instruction of a
541 /// write access is the instruction that defines the llvm::Value.
542 Instruction *AccessInstruction;
544 /// @brief Incoming block and value of a PHINode.
545 SmallVector<std::pair<BasicBlock *, Value *>, 4> Incoming;
547 /// @brief The value associated with this memory access.
549 /// - For array memory accesses (MK_Array) it is the loaded result or the
550 /// stored value. If the access instruction is a memory intrinsic it
551 /// the access value is also the memory intrinsic.
552 /// - For accesses of kind MK_Value it is the access instruction itself.
553 /// - For accesses of kind MK_PHI or MK_ExitPHI it is the PHI node itself
554 /// (for both, READ and WRITE accesses).
556 AssertingVH<Value> AccessValue;
558 /// @brief Are all the subscripts affine expression?
559 bool IsAffine;
561 /// @brief Subscript expression for each dimension.
562 SmallVector<const SCEV *, 4> Subscripts;
564 /// @brief Relation from statement instances to the accessed array elements.
566 /// In the common case this relation is a function that maps a set of loop
567 /// indices to the memory address from which a value is loaded/stored:
569 /// for i
570 /// for j
571 /// S: A[i + 3 j] = ...
573 /// => { S[i,j] -> A[i + 3j] }
575 /// In case the exact access function is not known, the access relation may
576 /// also be a one to all mapping { S[i,j] -> A[o] } describing that any
577 /// element accessible through A might be accessed.
579 /// In case of an access to a larger element belonging to an array that also
580 /// contains smaller elements, the access relation models the larger access
581 /// with multiple smaller accesses of the size of the minimal array element
582 /// type:
584 /// short *A;
586 /// for i
587 /// S: A[i] = *((double*)&A[4 * i]);
589 /// => { S[i] -> A[i]; S[i] -> A[o] : 4i <= o <= 4i + 3 }
590 isl_map *AccessRelation;
592 /// @brief Updated access relation read from JSCOP file.
593 isl_map *NewAccessRelation;
594 // @}
596 bool isAffine() const { return IsAffine; }
598 __isl_give isl_basic_map *createBasicAccessMap(ScopStmt *Statement);
600 void assumeNoOutOfBound();
602 /// @brief Compute bounds on an over approximated access relation.
604 /// @param ElementSize The size of one element accessed.
605 void computeBoundsOnAccessRelation(unsigned ElementSize);
607 /// @brief Get the original access function as read from IR.
608 __isl_give isl_map *getOriginalAccessRelation() const;
610 /// @brief Return the space in which the access relation lives in.
611 __isl_give isl_space *getOriginalAccessRelationSpace() const;
613 /// @brief Get the new access function imported or set by a pass
614 __isl_give isl_map *getNewAccessRelation() const;
616 /// @brief Fold the memory access to consider parameteric offsets
618 /// To recover memory accesses with array size parameters in the subscript
619 /// expression we post-process the delinearization results.
621 /// We would normally recover from an access A[exp0(i) * N + exp1(i)] into an
622 /// array A[][N] the 2D access A[exp0(i)][exp1(i)]. However, another valid
623 /// delinearization is A[exp0(i) - 1][exp1(i) + N] which - depending on the
624 /// range of exp1(i) - may be preferrable. Specifically, for cases where we
625 /// know exp1(i) is negative, we want to choose the latter expression.
627 /// As we commonly do not have any information about the range of exp1(i),
628 /// we do not choose one of the two options, but instead create a piecewise
629 /// access function that adds the (-1, N) offsets as soon as exp1(i) becomes
630 /// negative. For a 2D array such an access function is created by applying
631 /// the piecewise map:
633 /// [i,j] -> [i, j] : j >= 0
634 /// [i,j] -> [i-1, j+N] : j < 0
636 /// We can generalize this mapping to arbitrary dimensions by applying this
637 /// piecewise mapping pairwise from the rightmost to the leftmost access
638 /// dimension. It would also be possible to cover a wider range by introducing
639 /// more cases and adding multiple of Ns to these cases. However, this has
640 /// not yet been necessary.
641 /// The introduction of different cases necessarily complicates the memory
642 /// access function, but cases that can be statically proven to not happen
643 /// will be eliminated later on.
644 __isl_give isl_map *foldAccess(__isl_take isl_map *AccessRelation,
645 ScopStmt *Statement);
647 /// @brief Create the access relation for the underlying memory intrinsic.
648 void buildMemIntrinsicAccessRelation();
650 /// @brief Assemble the access relation from all availbale information.
652 /// In particular, used the information passes in the constructor and the
653 /// parent ScopStmt set by setStatment().
655 /// @param SAI Info object for the accessed array.
656 void buildAccessRelation(const ScopArrayInfo *SAI);
658 /// @brief Carry index overflows of dimensions with constant size to the next
659 /// higher dimension.
661 /// For dimensions that have constant size, modulo the index by the size and
662 /// add up the carry (floored division) to the next higher dimension. This is
663 /// how overflow is defined in row-major order.
664 /// It happens e.g. when ScalarEvolution computes the offset to the base
665 /// pointer and would algebraically sum up all lower dimensions' indices of
666 /// constant size.
668 /// Example:
669 /// float (*A)[4];
670 /// A[1][6] -> A[2][2]
671 void wrapConstantDimensions();
673 public:
674 /// @brief Create a new MemoryAccess.
676 /// @param Stmt The parent statement.
677 /// @param AccessInst The instruction doing the access.
678 /// @param BaseAddr The accessed array's address.
679 /// @param ElemType The type of the accessed array elements.
680 /// @param AccType Whether read or write access.
681 /// @param IsAffine Whether the subscripts are affine expressions.
682 /// @param Kind The kind of memory accessed.
683 /// @param Subscripts Subscipt expressions
684 /// @param Sizes Dimension lengths of the accessed array.
685 /// @param BaseName Name of the acessed array.
686 MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, AccessType AccType,
687 Value *BaseAddress, Type *ElemType, bool Affine,
688 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
689 Value *AccessValue, ScopArrayInfo::MemoryKind Kind,
690 StringRef BaseName);
691 ~MemoryAccess();
693 /// @brief Add a new incoming block/value pairs for this PHI/ExitPHI access.
695 /// @param IncomingBlock The PHI's incoming block.
696 /// @param IncomingValue The value when reacing the PHI from the @p
697 /// IncomingBlock.
698 void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue) {
699 assert(!isRead());
700 assert(isAnyPHIKind());
701 Incoming.emplace_back(std::make_pair(IncomingBlock, IncomingValue));
704 /// @brief Return the list of possible PHI/ExitPHI values.
706 /// After code generation moves some PHIs around during region simplification,
707 /// we cannot reliably locate the original PHI node and its incoming values
708 /// anymore. For this reason we remember these explicitely for all PHI-kind
709 /// accesses.
710 ArrayRef<std::pair<BasicBlock *, Value *>> getIncoming() const {
711 assert(isAnyPHIKind());
712 return Incoming;
715 /// @brief Get the type of a memory access.
716 enum AccessType getType() { return AccType; }
718 /// @brief Is this a reduction like access?
719 bool isReductionLike() const { return RedType != RT_NONE; }
721 /// @brief Is this a read memory access?
722 bool isRead() const { return AccType == MemoryAccess::READ; }
724 /// @brief Is this a must-write memory access?
725 bool isMustWrite() const { return AccType == MemoryAccess::MUST_WRITE; }
727 /// @brief Is this a may-write memory access?
728 bool isMayWrite() const { return AccType == MemoryAccess::MAY_WRITE; }
730 /// @brief Is this a write memory access?
731 bool isWrite() const { return isMustWrite() || isMayWrite(); }
733 /// @brief Check if a new access relation was imported or set by a pass.
734 bool hasNewAccessRelation() const { return NewAccessRelation; }
736 /// @brief Return the newest access relation of this access.
738 /// There are two possibilities:
739 /// 1) The original access relation read from the LLVM-IR.
740 /// 2) A new access relation imported from a json file or set by another
741 /// pass (e.g., for privatization).
743 /// As 2) is by construction "newer" than 1) we return the new access
744 /// relation if present.
746 __isl_give isl_map *getAccessRelation() const {
747 return hasNewAccessRelation() ? getNewAccessRelation()
748 : getOriginalAccessRelation();
751 /// @brief Get an isl map describing the memory address accessed.
753 /// In most cases the memory address accessed is well described by the access
754 /// relation obtained with getAccessRelation. However, in case of arrays
755 /// accessed with types of different size the access relation maps one access
756 /// to multiple smaller address locations. This method returns an isl map that
757 /// relates each dynamic statement instance to the unique memory location
758 /// that is loaded from / stored to.
760 /// For an access relation { S[i] -> A[o] : 4i <= o <= 4i + 3 } this method
761 /// will return the address function { S[i] -> A[4i] }.
763 /// @returns The address function for this memory access.
764 __isl_give isl_map *getAddressFunction() const;
766 /// @brief Return the access relation after the schedule was applied.
767 __isl_give isl_pw_multi_aff *
768 applyScheduleToAccessRelation(__isl_take isl_union_map *Schedule) const;
770 /// @brief Get an isl string representing the access function read from IR.
771 std::string getOriginalAccessRelationStr() const;
773 /// @brief Get an isl string representing a new access function, if available.
774 std::string getNewAccessRelationStr() const;
776 /// @brief Get the base address of this access (e.g. A for A[i+j]).
777 Value *getBaseAddr() const { return BaseAddr; }
779 /// @brief Get the base array isl_id for this access.
780 __isl_give isl_id *getArrayId() const;
782 /// @brief Get the ScopArrayInfo object for the base address.
783 const ScopArrayInfo *getScopArrayInfo() const;
785 /// @brief Return a string representation of the accesse's reduction type.
786 const std::string getReductionOperatorStr() const;
788 /// @brief Return a string representation of the reduction type @p RT.
789 static const std::string getReductionOperatorStr(ReductionType RT);
791 const std::string &getBaseName() const { return BaseName; }
793 /// @brief Return the element type of the accessed array wrt. this access.
794 Type *getElementType() const { return ElementType; }
796 /// @brief Return the access value of this memory access.
797 Value *getAccessValue() const { return AccessValue; }
799 /// @brief Return the access instruction of this memory access.
800 Instruction *getAccessInstruction() const { return AccessInstruction; }
802 /// @brief Return the number of access function subscript.
803 unsigned getNumSubscripts() const { return Subscripts.size(); }
805 /// @brief Return the access function subscript in the dimension @p Dim.
806 const SCEV *getSubscript(unsigned Dim) const { return Subscripts[Dim]; }
808 /// @brief Compute the isl representation for the SCEV @p E wrt. this access.
810 /// Note that this function will also adjust the invalid context accordingly.
811 __isl_give isl_pw_aff *getPwAff(const SCEV *E);
813 /// @brief Get the invalid domain for this access.
814 __isl_give isl_set *getInvalidDomain() const {
815 return isl_set_copy(InvalidDomain);
818 /// @brief Get the invalid context for this access.
819 __isl_give isl_set *getInvalidContext() const {
820 return isl_set_params(getInvalidDomain());
823 /// Get the stride of this memory access in the specified Schedule. Schedule
824 /// is a map from the statement to a schedule where the innermost dimension is
825 /// the dimension of the innermost loop containing the statement.
826 __isl_give isl_set *getStride(__isl_take const isl_map *Schedule) const;
828 /// Is the stride of the access equal to a certain width? Schedule is a map
829 /// from the statement to a schedule where the innermost dimension is the
830 /// dimension of the innermost loop containing the statement.
831 bool isStrideX(__isl_take const isl_map *Schedule, int StrideWidth) const;
833 /// Is consecutive memory accessed for a given statement instance set?
834 /// Schedule is a map from the statement to a schedule where the innermost
835 /// dimension is the dimension of the innermost loop containing the
836 /// statement.
837 bool isStrideOne(__isl_take const isl_map *Schedule) const;
839 /// Is always the same memory accessed for a given statement instance set?
840 /// Schedule is a map from the statement to a schedule where the innermost
841 /// dimension is the dimension of the innermost loop containing the
842 /// statement.
843 bool isStrideZero(__isl_take const isl_map *Schedule) const;
845 /// @brief Whether this is an access of an explicit load or store in the IR.
846 bool isArrayKind() const { return Kind == ScopArrayInfo::MK_Array; }
848 /// @brief Whether this access is an array to a scalar memory object.
850 /// Scalar accesses are accesses to MK_Value, MK_PHI or MK_ExitPHI.
851 bool isScalarKind() const { return !isArrayKind(); }
853 /// @brief Is this MemoryAccess modeling scalar dependences?
854 bool isValueKind() const { return Kind == ScopArrayInfo::MK_Value; }
856 /// @brief Is this MemoryAccess modeling special PHI node accesses?
857 bool isPHIKind() const { return Kind == ScopArrayInfo::MK_PHI; }
859 /// @brief Is this MemoryAccess modeling the accesses of a PHI node in the
860 /// SCoP's exit block?
861 bool isExitPHIKind() const { return Kind == ScopArrayInfo::MK_ExitPHI; }
863 /// @brief Does this access orginate from one of the two PHI types?
864 bool isAnyPHIKind() const { return isPHIKind() || isExitPHIKind(); }
866 /// @brief Get the statement that contains this memory access.
867 ScopStmt *getStatement() const { return Statement; }
869 /// @brief Get the reduction type of this access
870 ReductionType getReductionType() const { return RedType; }
872 /// @brief Set the updated access relation read from JSCOP file.
873 void setNewAccessRelation(__isl_take isl_map *NewAccessRelation);
875 /// @brief Mark this a reduction like access
876 void markAsReductionLike(ReductionType RT) { RedType = RT; }
878 /// @brief Align the parameters in the access relation to the scop context
879 void realignParams();
881 /// @brief Update the dimensionality of the memory access.
883 /// During scop construction some memory accesses may not be constructed with
884 /// their full dimensionality, but outer dimensions may have been omitted if
885 /// they took the value 'zero'. By updating the dimensionality of the
886 /// statement we add additional zero-valued dimensions to match the
887 /// dimensionality of the ScopArrayInfo object that belongs to this memory
888 /// access.
889 void updateDimensionality();
891 /// @brief Get identifier for the memory access.
893 /// This identifier is unique for all accesses that belong to the same scop
894 /// statement.
895 __isl_give isl_id *getId() const;
897 /// @brief Print the MemoryAccess.
899 /// @param OS The output stream the MemoryAccess is printed to.
900 void print(raw_ostream &OS) const;
902 /// @brief Print the MemoryAccess to stderr.
903 void dump() const;
906 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
907 MemoryAccess::ReductionType RT);
909 /// @brief Ordered list type to hold accesses.
910 using MemoryAccessList = std::forward_list<MemoryAccess *>;
912 /// @brief Helper structure for invariant memory accesses.
913 struct InvariantAccess {
914 /// @brief The memory access that is (partially) invariant.
915 MemoryAccess *MA;
917 /// @brief The context under which the access is not invariant.
918 isl_set *NonHoistableCtx;
921 /// @brief Ordered container type to hold invariant accesses.
922 using InvariantAccessesTy = SmallVector<InvariantAccess, 8>;
924 /// @brief Type for equivalent invariant accesses and their domain context.
925 struct InvariantEquivClassTy {
927 /// The pointer that identifies this equivalence class
928 const SCEV *IdentifyingPointer;
930 /// Memory accesses now treated invariant
932 /// These memory accesses access the pointer location that identifies
933 /// this equivalence class. They are treated as invariant and hoisted during
934 /// code generation.
935 MemoryAccessList InvariantAccesses;
937 /// The execution context under which the memory location is accessed
939 /// It is the union of the execution domains of the memory accesses in the
940 /// InvariantAccesses list.
941 isl_set *ExecutionContext;
943 /// The type of the invariant access
945 /// It is used to differentiate between differently typed invariant loads from
946 /// the same location.
947 Type *AccessType;
950 /// @brief Type for invariant accesses equivalence classes.
951 using InvariantEquivClassesTy = SmallVector<InvariantEquivClassTy, 8>;
953 /// @brief Statement of the Scop
955 /// A Scop statement represents an instruction in the Scop.
957 /// It is further described by its iteration domain, its schedule and its data
958 /// accesses.
959 /// At the moment every statement represents a single basic block of LLVM-IR.
960 class ScopStmt {
961 public:
962 ScopStmt(const ScopStmt &) = delete;
963 const ScopStmt &operator=(const ScopStmt &) = delete;
965 /// Create the ScopStmt from a BasicBlock.
966 ScopStmt(Scop &parent, BasicBlock &bb);
968 /// Create an overapproximating ScopStmt for the region @p R.
969 ScopStmt(Scop &parent, Region &R);
971 /// Initialize members after all MemoryAccesses have been added.
972 void init(LoopInfo &LI);
974 private:
975 /// Polyhedral description
976 //@{
978 /// The Scop containing this ScopStmt
979 Scop &Parent;
981 /// @brief The domain under which this statement is not modeled precisely.
983 /// The invalid domain for a statement describes all parameter combinations
984 /// under which the statement looks to be executed but is in fact not because
985 /// some assumption/restriction makes the statement/scop invalid.
986 isl_set *InvalidDomain;
988 /// The iteration domain describes the set of iterations for which this
989 /// statement is executed.
991 /// Example:
992 /// for (i = 0; i < 100 + b; ++i)
993 /// for (j = 0; j < i; ++j)
994 /// S(i,j);
996 /// 'S' is executed for different values of i and j. A vector of all
997 /// induction variables around S (i, j) is called iteration vector.
998 /// The domain describes the set of possible iteration vectors.
1000 /// In this case it is:
1002 /// Domain: 0 <= i <= 100 + b
1003 /// 0 <= j <= i
1005 /// A pair of statement and iteration vector (S, (5,3)) is called statement
1006 /// instance.
1007 isl_set *Domain;
1009 /// The memory accesses of this statement.
1011 /// The only side effects of a statement are its memory accesses.
1012 typedef SmallVector<MemoryAccess *, 8> MemoryAccessVec;
1013 MemoryAccessVec MemAccs;
1015 /// @brief Mapping from instructions to (scalar) memory accesses.
1016 DenseMap<const Instruction *, MemoryAccessList> InstructionToAccess;
1018 /// @brief The set of values defined elsewhere required in this ScopStmt and
1019 /// their MK_Value READ MemoryAccesses.
1020 DenseMap<Value *, MemoryAccess *> ValueReads;
1022 /// @brief The set of values defined in this ScopStmt that are required
1023 /// elsewhere, mapped to their MK_Value WRITE MemoryAccesses.
1024 DenseMap<Instruction *, MemoryAccess *> ValueWrites;
1026 /// @brief Map from PHI nodes to its incoming value when coming from this
1027 /// statement.
1029 /// Non-affine subregions can have multiple exiting blocks that are incoming
1030 /// blocks of the PHI nodes. This map ensures that there is only one write
1031 /// operation for the complete subregion. A PHI selecting the relevant value
1032 /// will be inserted.
1033 DenseMap<PHINode *, MemoryAccess *> PHIWrites;
1035 //@}
1037 /// @brief A SCoP statement represents either a basic block (affine/precise
1038 /// case) or a whole region (non-affine case). Only one of the
1039 /// following two members will therefore be set and indicate which
1040 /// kind of statement this is.
1042 ///{
1044 /// @brief The BasicBlock represented by this statement (in the affine case).
1045 BasicBlock *BB;
1047 /// @brief The region represented by this statement (in the non-affine case).
1048 Region *R;
1050 ///}
1052 /// @brief The isl AST build for the new generated AST.
1053 isl_ast_build *Build;
1055 SmallVector<Loop *, 4> NestLoops;
1057 std::string BaseName;
1059 /// Build the statement.
1060 //@{
1061 void buildDomain();
1063 /// @brief Fill NestLoops with loops surrounding this statement.
1064 void collectSurroundingLoops();
1066 /// @brief Build the access relation of all memory accesses.
1067 void buildAccessRelations();
1069 /// @brief Detect and mark reductions in the ScopStmt
1070 void checkForReductions();
1072 /// @brief Collect loads which might form a reduction chain with @p StoreMA
1073 void
1074 collectCandiateReductionLoads(MemoryAccess *StoreMA,
1075 llvm::SmallVectorImpl<MemoryAccess *> &Loads);
1076 //@}
1078 /// @brief Derive assumptions about parameter values from GetElementPtrInst
1080 /// In case a GEP instruction references into a fixed size array e.g., an
1081 /// access A[i][j] into an array A[100x100], LLVM-IR does not guarantee that
1082 /// the subscripts always compute values that are within array bounds. In this
1083 /// function we derive the set of parameter values for which all accesses are
1084 /// within bounds and add the assumption that the scop is only every executed
1085 /// with this set of parameter values.
1087 /// Example:
1089 /// void foo(float A[][20], long n, long m {
1090 /// for (long i = 0; i < n; i++)
1091 /// for (long j = 0; j < m; j++)
1092 /// A[i][j] = ...
1094 /// This loop yields out-of-bound accesses if m is at least 20 and at the same
1095 /// time at least one iteration of the outer loop is executed. Hence, we
1096 /// assume:
1098 /// n <= 0 or m <= 20.
1100 /// TODO: The location where the GEP instruction is executed is not
1101 /// necessarily the location where the memory is actually accessed. As a
1102 /// result scanning for GEP[s] is imprecise. Even though this is not a
1103 /// correctness problem, this imprecision may result in missed optimizations
1104 /// or non-optimal run-time checks.
1105 void deriveAssumptionsFromGEP(GetElementPtrInst *Inst, LoopInfo &LI);
1107 /// @brief Derive assumptions about parameter values.
1108 void deriveAssumptions(LoopInfo &LI);
1110 public:
1111 ~ScopStmt();
1113 /// @brief Get an isl_ctx pointer.
1114 isl_ctx *getIslCtx() const;
1116 /// @brief Get the iteration domain of this ScopStmt.
1118 /// @return The iteration domain of this ScopStmt.
1119 __isl_give isl_set *getDomain() const;
1121 /// @brief Get the space of the iteration domain
1123 /// @return The space of the iteration domain
1124 __isl_give isl_space *getDomainSpace() const;
1126 /// @brief Get the id of the iteration domain space
1128 /// @return The id of the iteration domain space
1129 __isl_give isl_id *getDomainId() const;
1131 /// @brief Get an isl string representing this domain.
1132 std::string getDomainStr() const;
1134 /// @brief Get the schedule function of this ScopStmt.
1136 /// @return The schedule function of this ScopStmt.
1137 __isl_give isl_map *getSchedule() const;
1139 /// @brief Get an isl string representing this schedule.
1140 std::string getScheduleStr() const;
1142 /// @brief Get the invalid domain for this statement.
1143 __isl_give isl_set *getInvalidDomain() const {
1144 return isl_set_copy(InvalidDomain);
1147 /// @brief Get the invalid context for this statement.
1148 __isl_give isl_set *getInvalidContext() const {
1149 return isl_set_params(getInvalidDomain());
1152 /// @brief Set the invalid context for this statement to @p ID.
1153 void setInvalidDomain(__isl_take isl_set *ID);
1155 /// @brief Get the BasicBlock represented by this ScopStmt (if any).
1157 /// @return The BasicBlock represented by this ScopStmt, or null if the
1158 /// statement represents a region.
1159 BasicBlock *getBasicBlock() const { return BB; }
1161 /// @brief Return true if this statement represents a single basic block.
1162 bool isBlockStmt() const { return BB != nullptr; }
1164 /// @brief Get the region represented by this ScopStmt (if any).
1166 /// @return The region represented by this ScopStmt, or null if the statement
1167 /// represents a basic block.
1168 Region *getRegion() const { return R; }
1170 /// @brief Return true if this statement represents a whole region.
1171 bool isRegionStmt() const { return R != nullptr; }
1173 /// @brief Return a BasicBlock from this statement.
1175 /// For block statements, it returns the BasicBlock itself. For subregion
1176 /// statements, return its entry block.
1177 BasicBlock *getEntryBlock() const;
1179 /// @brief Return true if this statement does not contain any accesses.
1180 bool isEmpty() const { return MemAccs.empty(); }
1182 /// @brief Return the only array access for @p Inst, if existing.
1184 /// @param Inst The instruction for which to look up the access.
1185 /// @returns The unique array memory access related to Inst or nullptr if
1186 /// no array access exists
1187 MemoryAccess *getArrayAccessOrNULLFor(const Instruction *Inst) const {
1188 auto It = InstructionToAccess.find(Inst);
1189 if (It == InstructionToAccess.end())
1190 return nullptr;
1192 MemoryAccess *ArrayAccess = nullptr;
1194 for (auto Access : It->getSecond()) {
1195 if (!Access->isArrayKind())
1196 continue;
1198 assert(!ArrayAccess && "More then one array access for instruction");
1200 ArrayAccess = Access;
1203 return ArrayAccess;
1206 /// @brief Return the only array access for @p Inst.
1208 /// @param Inst The instruction for which to look up the access.
1209 /// @returns The unique array memory access related to Inst.
1210 MemoryAccess &getArrayAccessFor(const Instruction *Inst) const {
1211 MemoryAccess *ArrayAccess = getArrayAccessOrNULLFor(Inst);
1213 assert(ArrayAccess && "No array access found for instruction!");
1214 return *ArrayAccess;
1217 /// @brief Return the MemoryAccess that writes the value of an instruction
1218 /// defined in this statement, or nullptr if not existing, respectively
1219 /// not yet added.
1220 MemoryAccess *lookupValueWriteOf(Instruction *Inst) const {
1221 assert((isRegionStmt() && R->contains(Inst)) ||
1222 (!isRegionStmt() && Inst->getParent() == BB));
1223 return ValueWrites.lookup(Inst);
1226 /// @brief Return the MemoryAccess that reloads a value, or nullptr if not
1227 /// existing, respectively not yet added.
1228 MemoryAccess *lookupValueReadOf(Value *Inst) const {
1229 return ValueReads.lookup(Inst);
1232 /// @brief Return the PHI write MemoryAccess for the incoming values from any
1233 /// basic block in this ScopStmt, or nullptr if not existing,
1234 /// respectively not yet added.
1235 MemoryAccess *lookupPHIWriteOf(PHINode *PHI) const {
1236 assert(isBlockStmt() || R->getExit() == PHI->getParent());
1237 return PHIWrites.lookup(PHI);
1240 /// @brief Add @p Access to this statement's list of accesses.
1241 void addAccess(MemoryAccess *Access);
1243 /// @brief Remove a MemoryAccess from this statement.
1245 /// Note that scalar accesses that are caused by MA will
1246 /// be eliminated too.
1247 void removeMemoryAccess(MemoryAccess *MA);
1249 typedef MemoryAccessVec::iterator iterator;
1250 typedef MemoryAccessVec::const_iterator const_iterator;
1252 iterator begin() { return MemAccs.begin(); }
1253 iterator end() { return MemAccs.end(); }
1254 const_iterator begin() const { return MemAccs.begin(); }
1255 const_iterator end() const { return MemAccs.end(); }
1256 size_t size() const { return MemAccs.size(); }
1258 unsigned getNumIterators() const;
1260 Scop *getParent() { return &Parent; }
1261 const Scop *getParent() const { return &Parent; }
1263 const char *getBaseName() const;
1265 /// @brief Set the isl AST build.
1266 void setAstBuild(__isl_keep isl_ast_build *B) { Build = B; }
1268 /// @brief Get the isl AST build.
1269 __isl_keep isl_ast_build *getAstBuild() const { return Build; }
1271 /// @brief Restrict the domain of the statement.
1273 /// @param NewDomain The new statement domain.
1274 void restrictDomain(__isl_take isl_set *NewDomain);
1276 /// @brief Compute the isl representation for the SCEV @p E in this stmt.
1278 /// @param E The SCEV that should be translated.
1279 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
1281 /// Note that this function will also adjust the invalid context accordingly.
1282 __isl_give isl_pw_aff *getPwAff(const SCEV *E, bool NonNegative = false);
1284 /// @brief Get the loop for a dimension.
1286 /// @param Dimension The dimension of the induction variable
1287 /// @return The loop at a certain dimension.
1288 Loop *getLoopForDimension(unsigned Dimension) const;
1290 /// @brief Align the parameters in the statement to the scop context
1291 void realignParams();
1293 /// @brief Print the ScopStmt.
1295 /// @param OS The output stream the ScopStmt is printed to.
1296 void print(raw_ostream &OS) const;
1298 /// @brief Print the ScopStmt to stderr.
1299 void dump() const;
1302 /// @brief Print ScopStmt S to raw_ostream O.
1303 static inline raw_ostream &operator<<(raw_ostream &O, const ScopStmt &S) {
1304 S.print(O);
1305 return O;
1308 /// @brief Static Control Part
1310 /// A Scop is the polyhedral representation of a control flow region detected
1311 /// by the Scop detection. It is generated by translating the LLVM-IR and
1312 /// abstracting its effects.
1314 /// A Scop consists of a set of:
1316 /// * A set of statements executed in the Scop.
1318 /// * A set of global parameters
1319 /// Those parameters are scalar integer values, which are constant during
1320 /// execution.
1322 /// * A context
1323 /// This context contains information about the values the parameters
1324 /// can take and relations between different parameters.
1325 class Scop {
1326 public:
1327 /// @brief Type to represent a pair of minimal/maximal access to an array.
1328 using MinMaxAccessTy = std::pair<isl_pw_multi_aff *, isl_pw_multi_aff *>;
1330 /// @brief Vector of minimal/maximal accesses to different arrays.
1331 using MinMaxVectorTy = SmallVector<MinMaxAccessTy, 4>;
1333 /// @brief Pair of minimal/maximal access vectors representing
1334 /// read write and read only accesses
1335 using MinMaxVectorPairTy = std::pair<MinMaxVectorTy, MinMaxVectorTy>;
1337 /// @brief Vector of pair of minimal/maximal access vectors representing
1338 /// non read only and read only accesses for each alias group.
1339 using MinMaxVectorPairVectorTy = SmallVector<MinMaxVectorPairTy, 4>;
1341 private:
1342 Scop(const Scop &) = delete;
1343 const Scop &operator=(const Scop &) = delete;
1345 ScalarEvolution *SE;
1347 /// The underlying Region.
1348 Region &R;
1350 // Access function of statements (currently BasicBlocks) .
1352 // This owns all the MemoryAccess objects of the Scop created in this pass.
1353 AccFuncMapType AccFuncMap;
1355 /// Flag to indicate that the scheduler actually optimized the SCoP.
1356 bool IsOptimized;
1358 /// @brief True if the underlying region has a single exiting block.
1359 bool HasSingleExitEdge;
1361 /// @brief Flag to remember if the SCoP contained an error block or not.
1362 bool HasErrorBlock;
1364 /// Max loop depth.
1365 unsigned MaxLoopDepth;
1367 typedef std::list<ScopStmt> StmtSet;
1368 /// The statements in this Scop.
1369 StmtSet Stmts;
1371 /// @brief Parameters of this Scop
1372 ParameterSetTy Parameters;
1374 /// @brief Mapping from parameters to their ids.
1375 DenseMap<const SCEV *, isl_id *> ParameterIds;
1377 /// @brief The context of the SCoP created during SCoP detection.
1378 ScopDetection::DetectionContext &DC;
1380 /// Isl context.
1382 /// We need a shared_ptr with reference counter to delete the context when all
1383 /// isl objects are deleted. We will distribute the shared_ptr to all objects
1384 /// that use the context to create isl objects, and increase the reference
1385 /// counter. By doing this, we guarantee that the context is deleted when we
1386 /// delete the last object that creates isl objects with the context.
1387 std::shared_ptr<isl_ctx> IslCtx;
1389 /// @brief A map from basic blocks to SCoP statements.
1390 DenseMap<BasicBlock *, ScopStmt *> StmtMap;
1392 /// @brief A map from basic blocks to their domains.
1393 DenseMap<BasicBlock *, isl_set *> DomainMap;
1395 /// Constraints on parameters.
1396 isl_set *Context;
1398 /// @brief The affinator used to translate SCEVs to isl expressions.
1399 SCEVAffinator Affinator;
1401 typedef MapVector<std::pair<AssertingVH<const Value>, int>,
1402 std::unique_ptr<ScopArrayInfo>>
1403 ArrayInfoMapTy;
1404 /// @brief A map to remember ScopArrayInfo objects for all base pointers.
1406 /// As PHI nodes may have two array info objects associated, we add a flag
1407 /// that distinguishes between the PHI node specific ArrayInfo object
1408 /// and the normal one.
1409 ArrayInfoMapTy ScopArrayInfoMap;
1411 /// @brief The assumptions under which this scop was built.
1413 /// When constructing a scop sometimes the exact representation of a statement
1414 /// or condition would be very complex, but there is a common case which is a
1415 /// lot simpler, but which is only valid under certain assumptions. The
1416 /// assumed context records the assumptions taken during the construction of
1417 /// this scop and that need to be code generated as a run-time test.
1418 isl_set *AssumedContext;
1420 /// @brief The restrictions under which this SCoP was built.
1422 /// The invalid context is similar to the assumed context as it contains
1423 /// constraints over the parameters. However, while we need the constraints
1424 /// in the assumed context to be "true" the constraints in the invalid context
1425 /// need to be "false". Otherwise they behave the same.
1426 isl_set *InvalidContext;
1428 /// @brief Helper struct to remember assumptions.
1429 struct Assumption {
1431 /// @brief The kind of the assumption (e.g., WRAPPING).
1432 AssumptionKind Kind;
1434 /// @brief Flag to distinguish assumptions and restrictions.
1435 AssumptionSign Sign;
1437 /// @brief The valid/invalid context if this is an assumption/restriction.
1438 isl_set *Set;
1440 /// @brief The location that caused this assumption.
1441 DebugLoc Loc;
1443 /// @brief An optional block whose domain can simplify the assumption.
1444 BasicBlock *BB;
1447 /// @brief Collection to hold taken assumptions.
1449 /// There are two reasons why we want to record assumptions first before we
1450 /// add them to the assumed/invalid context:
1451 /// 1) If the SCoP is not profitable or otherwise invalid without the
1452 /// assumed/invalid context we do not have to compute it.
1453 /// 2) Information about the context are gathered rather late in the SCoP
1454 /// construction (basically after we know all parameters), thus the user
1455 /// might see overly complicated assumptions to be taken while they will
1456 /// only be simplified later on.
1457 SmallVector<Assumption, 8> RecordedAssumptions;
1459 /// @brief The schedule of the SCoP
1461 /// The schedule of the SCoP describes the execution order of the statements
1462 /// in the scop by assigning each statement instance a possibly
1463 /// multi-dimensional execution time. The schedule is stored as a tree of
1464 /// schedule nodes.
1466 /// The most common nodes in a schedule tree are so-called band nodes. Band
1467 /// nodes map statement instances into a multi dimensional schedule space.
1468 /// This space can be seen as a multi-dimensional clock.
1470 /// Example:
1472 /// <S,(5,4)> may be mapped to (5,4) by this schedule:
1474 /// s0 = i (Year of execution)
1475 /// s1 = j (Day of execution)
1477 /// or to (9, 20) by this schedule:
1479 /// s0 = i + j (Year of execution)
1480 /// s1 = 20 (Day of execution)
1482 /// The order statement instances are executed is defined by the
1483 /// schedule vectors they are mapped to. A statement instance
1484 /// <A, (i, j, ..)> is executed before a statement instance <B, (i', ..)>, if
1485 /// the schedule vector of A is lexicographic smaller than the schedule
1486 /// vector of B.
1488 /// Besides band nodes, schedule trees contain additional nodes that specify
1489 /// a textual ordering between two subtrees or filter nodes that filter the
1490 /// set of statement instances that will be scheduled in a subtree. There
1491 /// are also several other nodes. A full description of the different nodes
1492 /// in a schedule tree is given in the isl manual.
1493 isl_schedule *Schedule;
1495 /// @brief The set of minimal/maximal accesses for each alias group.
1497 /// When building runtime alias checks we look at all memory instructions and
1498 /// build so called alias groups. Each group contains a set of accesses to
1499 /// different base arrays which might alias with each other. However, between
1500 /// alias groups there is no aliasing possible.
1502 /// In a program with int and float pointers annotated with tbaa information
1503 /// we would probably generate two alias groups, one for the int pointers and
1504 /// one for the float pointers.
1506 /// During code generation we will create a runtime alias check for each alias
1507 /// group to ensure the SCoP is executed in an alias free environment.
1508 MinMaxVectorPairVectorTy MinMaxAliasGroups;
1510 /// @brief Mapping from invariant loads to the representing invariant load of
1511 /// their equivalence class.
1512 ValueToValueMap InvEquivClassVMap;
1514 /// @brief List of invariant accesses.
1515 InvariantEquivClassesTy InvariantEquivClasses;
1517 /// @brief Scop constructor; invoked from ScopBuilder::buildScop.
1518 Scop(Region &R, ScalarEvolution &SE, LoopInfo &LI,
1519 ScopDetection::DetectionContext &DC);
1521 /// @brief Get or create the access function set in a BasicBlock
1522 AccFuncSetType &getOrCreateAccessFunctions(const BasicBlock *BB) {
1523 return AccFuncMap[BB];
1525 //@}
1527 /// @brief Initialize this ScopBuilder.
1528 void init(AliasAnalysis &AA, AssumptionCache &AC, DominatorTree &DT,
1529 LoopInfo &LI);
1531 /// @brief Propagate domains that are known due to graph properties.
1533 /// As a CFG is mostly structured we use the graph properties to propagate
1534 /// domains without the need to compute all path conditions. In particular, if
1535 /// a block A dominates a block B and B post-dominates A we know that the
1536 /// domain of B is a superset of the domain of A. As we do not have
1537 /// post-dominator information available here we use the less precise region
1538 /// information. Given a region R, we know that the exit is always executed if
1539 /// the entry was executed, thus the domain of the exit is a superset of the
1540 /// domain of the entry. In case the exit can only be reached from within the
1541 /// region the domains are in fact equal. This function will use this property
1542 /// to avoid the generation of condition constraints that determine when a
1543 /// branch is taken. If @p BB is a region entry block we will propagate its
1544 /// domain to the region exit block. Additionally, we put the region exit
1545 /// block in the @p FinishedExitBlocks set so we can later skip edges from
1546 /// within the region to that block.
1548 /// @param BB The block for which the domain is currently propagated.
1549 /// @param BBLoop The innermost affine loop surrounding @p BB.
1550 /// @param FinishedExitBlocks Set of region exits the domain was set for.
1551 /// @param LI The LoopInfo for the current function.
1553 void propagateDomainConstraintsToRegionExit(
1554 BasicBlock *BB, Loop *BBLoop,
1555 SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI);
1557 /// @brief Compute the union of predecessor domains for @p BB.
1559 /// To compute the union of all domains of predecessors of @p BB this
1560 /// function applies similar reasoning on the CFG structure as described for
1561 /// @see propagateDomainConstraintsToRegionExit
1563 /// @param BB The block for which the predecessor domains are collected.
1564 /// @param Domain The domain under which BB is executed.
1565 /// @param DT The DominatorTree for the current function.
1566 /// @param LI The LoopInfo for the current function.
1568 /// @returns The domain under which @p BB is executed.
1569 __isl_give isl_set *getPredecessorDomainConstraints(BasicBlock *BB,
1570 isl_set *Domain,
1571 DominatorTree &DT,
1572 LoopInfo &LI);
1574 /// @brief Add loop carried constraints to the header block of the loop @p L.
1576 /// @param L The loop to process.
1577 /// @param LI The LoopInfo for the current function.
1579 /// @returns True if there was no problem and false otherwise.
1580 bool addLoopBoundsToHeaderDomain(Loop *L, LoopInfo &LI);
1582 /// @brief Compute the branching constraints for each basic block in @p R.
1584 /// @param R The region we currently build branching conditions for.
1585 /// @param DT The DominatorTree for the current function.
1586 /// @param LI The LoopInfo for the current function.
1588 /// @returns True if there was no problem and false otherwise.
1589 bool buildDomainsWithBranchConstraints(Region *R, DominatorTree &DT,
1590 LoopInfo &LI);
1592 /// @brief Propagate the domain constraints through the region @p R.
1594 /// @param R The region we currently build branching conditions for.
1595 /// @param DT The DominatorTree for the current function.
1596 /// @param LI The LoopInfo for the current function.
1598 /// @returns True if there was no problem and false otherwise.
1599 bool propagateDomainConstraints(Region *R, DominatorTree &DT, LoopInfo &LI);
1601 /// @brief Propagate invalid domains of statements through @p R.
1603 /// This method will propagate invalid statement domains through @p R and at
1604 /// the same time add error block domains to them. Additionally, the domains
1605 /// of error statements and those only reachable via error statements will be
1606 /// replaced by an empty set. Later those will be removed completely.
1608 /// @param R The currently traversed region.
1609 /// @param DT The DominatorTree for the current function.
1610 /// @param LI The LoopInfo for the current function.
1612 /// @returns True if there was no problem and false otherwise.
1613 bool propagateInvalidStmtDomains(Region *R, DominatorTree &DT, LoopInfo &LI);
1615 /// @brief Compute the domain for each basic block in @p R.
1617 /// @param R The region we currently traverse.
1618 /// @param DT The DominatorTree for the current function.
1619 /// @param LI The LoopInfo for the current function.
1621 /// @returns True if there was no problem and false otherwise.
1622 bool buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI);
1624 /// @brief Add parameter constraints to @p C that imply a non-empty domain.
1625 __isl_give isl_set *addNonEmptyDomainConstraints(__isl_take isl_set *C) const;
1627 /// @brief Simplify the SCoP representation
1628 void simplifySCoP(bool AfterHoisting, DominatorTree &DT, LoopInfo &LI);
1630 /// @brief Return the access for the base ptr of @p MA if any.
1631 MemoryAccess *lookupBasePtrAccess(MemoryAccess *MA);
1633 /// @brief Check if the base ptr of @p MA is in the SCoP but not hoistable.
1634 bool hasNonHoistableBasePtrInScop(MemoryAccess *MA,
1635 __isl_keep isl_union_map *Writes);
1637 /// @brief Create equivalence classes for required invariant accesses.
1639 /// These classes will consolidate multiple required invariant loads from the
1640 /// same address in order to keep the number of dimensions in the SCoP
1641 /// description small. For each such class equivalence class only one
1642 /// representing element, hence one required invariant load, will be chosen
1643 /// and modeled as parameter. The method
1644 /// Scop::getRepresentingInvariantLoadSCEV() will replace each element from an
1645 /// equivalence class with the representing element that is modeled. As a
1646 /// consequence Scop::getIdForParam() will only return an id for the
1647 /// representing element of each equivalence class, thus for each required
1648 /// invariant location.
1649 void buildInvariantEquivalenceClasses();
1651 /// @brief Return the context under which the access cannot be hoisted.
1653 /// @param Access The access to check.
1654 /// @param Writes The set of all memory writes in the scop.
1656 /// @return Return the context under which the access cannot be hoisted or a
1657 /// nullptr if it cannot be hoisted at all.
1658 __isl_give isl_set *getNonHoistableCtx(MemoryAccess *Access,
1659 __isl_keep isl_union_map *Writes);
1661 /// @brief Verify that all required invariant loads have been hoisted.
1663 /// Invariant load hoisting is not guaranteed to hoist all loads that were
1664 /// assumed to be scop invariant during scop detection. This function checks
1665 /// for cases where the hoisting failed, but where it would have been
1666 /// necessary for our scop modeling to be correct. In case of insufficent
1667 /// hoisting the scop is marked as invalid.
1669 /// In the example below Bound[1] is required to be invariant:
1671 /// for (int i = 1; i < Bound[0]; i++)
1672 /// for (int j = 1; j < Bound[1]; j++)
1673 /// ...
1675 void verifyInvariantLoads();
1677 /// @brief Hoist invariant memory loads and check for required ones.
1679 /// We first identify "common" invariant loads, thus loads that are invariant
1680 /// and can be hoisted. Then we check if all required invariant loads have
1681 /// been identified as (common) invariant. A load is a required invariant load
1682 /// if it was assumed to be invariant during SCoP detection, e.g., to assume
1683 /// loop bounds to be affine or runtime alias checks to be placeable. In case
1684 /// a required invariant load was not identified as (common) invariant we will
1685 /// drop this SCoP. An example for both "common" as well as required invariant
1686 /// loads is given below:
1688 /// for (int i = 1; i < *LB[0]; i++)
1689 /// for (int j = 1; j < *LB[1]; j++)
1690 /// A[i][j] += A[0][0] + (*V);
1692 /// Common inv. loads: V, A[0][0], LB[0], LB[1]
1693 /// Required inv. loads: LB[0], LB[1], (V, if it may alias with A or LB)
1695 void hoistInvariantLoads();
1697 /// @brief Add invariant loads listed in @p InvMAs with the domain of @p Stmt.
1698 void addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs);
1700 /// @brief Create an id for @p Param and store it in the ParameterIds map.
1701 void createParameterId(const SCEV *Param);
1703 /// @brief Build the Context of the Scop.
1704 void buildContext();
1706 /// @brief Add user provided parameter constraints to context (source code).
1707 void addUserAssumptions(AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI);
1709 /// @brief Add user provided parameter constraints to context (command line).
1710 void addUserContext();
1712 /// @brief Add the bounds of the parameters to the context.
1713 void addParameterBounds();
1715 /// @brief Simplify the assumed and invalid context.
1716 void simplifyContexts();
1718 /// @brief Get the representing SCEV for @p S if applicable, otherwise @p S.
1720 /// Invariant loads of the same location are put in an equivalence class and
1721 /// only one of them is chosen as a representing element that will be
1722 /// modeled as a parameter. The others have to be normalized, i.e.,
1723 /// replaced by the representing element of their equivalence class, in order
1724 /// to get the correct parameter value, e.g., in the SCEVAffinator.
1726 /// @param S The SCEV to normalize.
1728 /// @return The representing SCEV for invariant loads or @p S if none.
1729 const SCEV *getRepresentingInvariantLoadSCEV(const SCEV *S);
1731 /// @brief Create a new SCoP statement for either @p BB or @p R.
1733 /// Either @p BB or @p R should be non-null. A new statement for the non-null
1734 /// argument will be created and added to the statement vector and map.
1736 /// @param BB The basic block we build the statement for (or null)
1737 /// @param R The region we build the statement for (or null).
1738 void addScopStmt(BasicBlock *BB, Region *R);
1740 /// @param Update access dimensionalities.
1742 /// When detecting memory accesses different accesses to the same array may
1743 /// have built with different dimensionality, as outer zero-values dimensions
1744 /// may not have been recognized as separate dimensions. This function goes
1745 /// again over all memory accesses and updates their dimensionality to match
1746 /// the dimensionality of the underlying ScopArrayInfo object.
1747 void updateAccessDimensionality();
1749 /// @brief Construct the schedule of this SCoP.
1751 /// @param LI The LoopInfo for the current function.
1752 void buildSchedule(LoopInfo &LI);
1754 /// @brief A loop stack element to keep track of per-loop information during
1755 /// schedule construction.
1756 typedef struct LoopStackElement {
1757 // The loop for which we keep information.
1758 Loop *L;
1760 // The (possibly incomplete) schedule for this loop.
1761 isl_schedule *Schedule;
1763 // The number of basic blocks in the current loop, for which a schedule has
1764 // already been constructed.
1765 unsigned NumBlocksProcessed;
1767 LoopStackElement(Loop *L, __isl_give isl_schedule *S,
1768 unsigned NumBlocksProcessed)
1769 : L(L), Schedule(S), NumBlocksProcessed(NumBlocksProcessed) {}
1770 } LoopStackElementTy;
1772 /// @brief The loop stack used for schedule construction.
1774 /// The loop stack keeps track of schedule information for a set of nested
1775 /// loops as well as an (optional) 'nullptr' loop that models the outermost
1776 /// schedule dimension. The loops in a loop stack always have a parent-child
1777 /// relation where the loop at position n is the parent of the loop at
1778 /// position n + 1.
1779 typedef SmallVector<LoopStackElementTy, 4> LoopStackTy;
1781 /// @brief Construct schedule information for a given Region and add the
1782 /// derived information to @p LoopStack.
1784 /// Given a Region we derive schedule information for all RegionNodes
1785 /// contained in this region ensuring that the assigned execution times
1786 /// correctly model the existing control flow relations.
1788 /// @param R The region which to process.
1789 /// @param LoopStack A stack of loops that are currently under
1790 /// construction.
1791 /// @param LI The LoopInfo for the current function.
1792 void buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI);
1794 /// @brief Build Schedule for the region node @p RN and add the derived
1795 /// information to @p LoopStack.
1797 /// In case @p RN is a BasicBlock or a non-affine Region, we construct the
1798 /// schedule for this @p RN and also finalize loop schedules in case the
1799 /// current @p RN completes the loop.
1801 /// In case @p RN is a not-non-affine Region, we delegate the construction to
1802 /// buildSchedule(Region *R, ...).
1804 /// @param RN The RegionNode region traversed.
1805 /// @param LoopStack A stack of loops that are currently under
1806 /// construction.
1807 /// @param LI The LoopInfo for the current function.
1808 void buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI);
1810 /// @brief Collect all memory access relations of a given type.
1812 /// @param Predicate A predicate function that returns true if an access is
1813 /// of a given type.
1815 /// @returns The set of memory accesses in the scop that match the predicate.
1816 __isl_give isl_union_map *
1817 getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate);
1819 /// @name Helper functions for printing the Scop.
1821 //@{
1822 void printContext(raw_ostream &OS) const;
1823 void printArrayInfo(raw_ostream &OS) const;
1824 void printStatements(raw_ostream &OS) const;
1825 void printAliasAssumptions(raw_ostream &OS) const;
1826 //@}
1828 friend class ScopBuilder;
1830 public:
1831 ~Scop();
1833 /// @brief Get all access functions in a BasicBlock
1835 /// @param BB The BasicBlock that containing the access functions.
1837 /// @return All access functions in BB
1839 AccFuncSetType *getAccessFunctions(const BasicBlock *BB) {
1840 AccFuncMapType::iterator at = AccFuncMap.find(BB);
1841 return at != AccFuncMap.end() ? &(at->second) : 0;
1844 ScalarEvolution *getSE() const;
1846 /// @brief Get the count of parameters used in this Scop.
1848 /// @return The count of parameters used in this Scop.
1849 size_t getNumParams() const { return Parameters.size(); }
1851 /// @brief Take a list of parameters and add the new ones to the scop.
1852 void addParams(const ParameterSetTy &NewParameters);
1854 /// @brief Return whether this scop is empty, i.e. contains no statements that
1855 /// could be executed.
1856 bool isEmpty() const { return Stmts.empty(); }
1858 typedef ArrayInfoMapTy::iterator array_iterator;
1859 typedef ArrayInfoMapTy::const_iterator const_array_iterator;
1860 typedef iterator_range<ArrayInfoMapTy::iterator> array_range;
1861 typedef iterator_range<ArrayInfoMapTy::const_iterator> const_array_range;
1863 inline array_iterator array_begin() { return ScopArrayInfoMap.begin(); }
1865 inline array_iterator array_end() { return ScopArrayInfoMap.end(); }
1867 inline const_array_iterator array_begin() const {
1868 return ScopArrayInfoMap.begin();
1871 inline const_array_iterator array_end() const {
1872 return ScopArrayInfoMap.end();
1875 inline array_range arrays() {
1876 return array_range(array_begin(), array_end());
1879 inline const_array_range arrays() const {
1880 return const_array_range(array_begin(), array_end());
1883 /// @brief Return the isl_id that represents a certain parameter.
1885 /// @param Parameter A SCEV that was recognized as a Parameter.
1887 /// @return The corresponding isl_id or NULL otherwise.
1888 isl_id *getIdForParam(const SCEV *Parameter);
1890 /// @brief Get the maximum region of this static control part.
1892 /// @return The maximum region of this static control part.
1893 inline const Region &getRegion() const { return R; }
1894 inline Region &getRegion() { return R; }
1896 /// @brief Return the function this SCoP is in.
1897 Function &getFunction() const { return *R.getEntry()->getParent(); }
1899 /// @brief Check if @p L is contained in the SCoP.
1900 bool contains(const Loop *L) const { return R.contains(L); }
1902 /// @brief Check if @p BB is contained in the SCoP.
1903 bool contains(const BasicBlock *BB) const { return R.contains(BB); }
1905 /// @brief Check if @p I is contained in the SCoP.
1906 bool contains(const Instruction *I) const { return R.contains(I); }
1908 /// @brief Return the unique exit block of the SCoP.
1909 BasicBlock *getExit() const { return R.getExit(); }
1911 /// @brief Return the unique exiting block of the SCoP if any.
1912 BasicBlock *getExitingBlock() const { return R.getExitingBlock(); }
1914 /// @brief Return the unique entry block of the SCoP.
1915 BasicBlock *getEntry() const { return R.getEntry(); }
1917 /// @brief Return the unique entering block of the SCoP if any.
1918 BasicBlock *getEnteringBlock() const { return R.getEnteringBlock(); }
1920 /// @brief Return true if @p BB is the exit block of the SCoP.
1921 bool isExit(BasicBlock *BB) const { return getExit() == BB; }
1923 /// @brief Return a range of all basic blocks in the SCoP.
1924 Region::block_range blocks() const { return R.blocks(); }
1926 /// @brief Return true if and only if @p BB dominates the SCoP.
1927 bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const;
1929 /// @brief Get the maximum depth of the loop.
1931 /// @return The maximum depth of the loop.
1932 inline unsigned getMaxLoopDepth() const { return MaxLoopDepth; }
1934 /// @brief Return the invariant equivalence class for @p Val if any.
1935 InvariantEquivClassTy *lookupInvariantEquivClass(Value *Val);
1937 /// @brief Return the set of invariant accesses.
1938 InvariantEquivClassesTy &getInvariantAccesses() {
1939 return InvariantEquivClasses;
1942 /// @brief Check if the scop has any invariant access.
1943 bool hasInvariantAccesses() { return !InvariantEquivClasses.empty(); }
1945 /// @brief Mark the SCoP as optimized by the scheduler.
1946 void markAsOptimized() { IsOptimized = true; }
1948 /// @brief Check if the SCoP has been optimized by the scheduler.
1949 bool isOptimized() const { return IsOptimized; }
1951 /// @brief Get the name of this Scop.
1952 std::string getNameStr() const;
1954 /// @brief Get the constraint on parameter of this Scop.
1956 /// @return The constraint on parameter of this Scop.
1957 __isl_give isl_set *getContext() const;
1958 __isl_give isl_space *getParamSpace() const;
1960 /// @brief Get the assumed context for this Scop.
1962 /// @return The assumed context of this Scop.
1963 __isl_give isl_set *getAssumedContext() const;
1965 /// @brief Return true if the optimized SCoP can be executed.
1967 /// In addition to the runtime check context this will also utilize the domain
1968 /// constraints to decide it the optimized version can actually be executed.
1970 /// @returns True if the optimized SCoP can be executed.
1971 bool hasFeasibleRuntimeContext() const;
1973 /// @brief Check if the assumption in @p Set is trivial or not.
1975 /// @param Set The relations between parameters that are assumed to hold.
1976 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
1977 /// (needed/assumptions) or negative (invalid/restrictions).
1979 /// @returns True if the assumption @p Set is not trivial.
1980 bool isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign);
1982 /// @brief Track and report an assumption.
1984 /// Use 'clang -Rpass-analysis=polly-scops' or 'opt
1985 /// -pass-remarks-analysis=polly-scops' to output the assumptions.
1987 /// @param Kind The assumption kind describing the underlying cause.
1988 /// @param Set The relations between parameters that are assumed to hold.
1989 /// @param Loc The location in the source that caused this assumption.
1990 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
1991 /// (needed/assumptions) or negative (invalid/restrictions).
1993 /// @returns True if the assumption is not trivial.
1994 bool trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set,
1995 DebugLoc Loc, AssumptionSign Sign);
1997 /// @brief Add assumptions to assumed context.
1999 /// The assumptions added will be assumed to hold during the execution of the
2000 /// scop. However, as they are generally not statically provable, at code
2001 /// generation time run-time checks will be generated that ensure the
2002 /// assumptions hold.
2004 /// WARNING: We currently exploit in simplifyAssumedContext the knowledge
2005 /// that assumptions do not change the set of statement instances
2006 /// executed.
2008 /// @param Kind The assumption kind describing the underlying cause.
2009 /// @param Set The relations between parameters that are assumed to hold.
2010 /// @param Loc The location in the source that caused this assumption.
2011 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2012 /// (needed/assumptions) or negative (invalid/restrictions).
2013 void addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, DebugLoc Loc,
2014 AssumptionSign Sign);
2016 /// @brief Record an assumption for later addition to the assumed context.
2018 /// This function will add the assumption to the RecordedAssumptions. This
2019 /// collection will be added (@see addAssumption) to the assumed context once
2020 /// all paramaters are known and the context is fully build.
2022 /// @param Kind The assumption kind describing the underlying cause.
2023 /// @param Set The relations between parameters that are assumed to hold.
2024 /// @param Loc The location in the source that caused this assumption.
2025 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2026 /// (needed/assumptions) or negative (invalid/restrictions).
2027 /// @param BB The block in which this assumption was taken. If it is
2028 /// set, the domain of that block will be used to simplify the
2029 /// actual assumption in @p Set once it is added. This is useful
2030 /// if the assumption was created prior to the domain.
2031 void recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set,
2032 DebugLoc Loc, AssumptionSign Sign,
2033 BasicBlock *BB = nullptr);
2035 /// @brief Add all recorded assumptions to the assumed context.
2036 void addRecordedAssumptions();
2038 /// @brief Mark the scop as invalid.
2040 /// This method adds an assumption to the scop that is always invalid. As a
2041 /// result, the scop will not be optimized later on. This function is commonly
2042 /// called when a condition makes it impossible (or too compile time
2043 /// expensive) to process this scop any further.
2045 /// @param Kind The assumption kind describing the underlying cause.
2046 /// @param Loc The location in the source that triggered .
2047 void invalidate(AssumptionKind Kind, DebugLoc Loc);
2049 /// @brief Get the invalid context for this Scop.
2051 /// @return The invalid context of this Scop.
2052 __isl_give isl_set *getInvalidContext() const;
2054 /// @brief Return true if and only if the InvalidContext is trivial (=empty).
2055 bool hasTrivialInvalidContext() const {
2056 return isl_set_is_empty(InvalidContext);
2059 /// @brief Build the alias checks for this SCoP.
2060 bool buildAliasChecks(AliasAnalysis &AA);
2062 /// @brief Build all alias groups for this SCoP.
2064 /// @returns True if __no__ error occurred, false otherwise.
2065 bool buildAliasGroups(AliasAnalysis &AA);
2067 /// @brief Return all alias groups for this SCoP.
2068 const MinMaxVectorPairVectorTy &getAliasGroups() const {
2069 return MinMaxAliasGroups;
2072 /// @brief Get an isl string representing the context.
2073 std::string getContextStr() const;
2075 /// @brief Get an isl string representing the assumed context.
2076 std::string getAssumedContextStr() const;
2078 /// @brief Get an isl string representing the invalid context.
2079 std::string getInvalidContextStr() const;
2081 /// @brief Return the ScopStmt for the given @p BB or nullptr if there is
2082 /// none.
2083 ScopStmt *getStmtFor(BasicBlock *BB) const;
2085 /// @brief Return the ScopStmt that represents the Region @p R, or nullptr if
2086 /// it is not represented by any statement in this Scop.
2087 ScopStmt *getStmtFor(Region *R) const;
2089 /// @brief Return the ScopStmt that represents @p RN; can return nullptr if
2090 /// the RegionNode is not within the SCoP or has been removed due to
2091 /// simplifications.
2092 ScopStmt *getStmtFor(RegionNode *RN) const;
2094 /// @brief Return the ScopStmt an instruction belongs to, or nullptr if it
2095 /// does not belong to any statement in this Scop.
2096 ScopStmt *getStmtFor(Instruction *Inst) const {
2097 return getStmtFor(Inst->getParent());
2100 /// @brief Return the number of statements in the SCoP.
2101 size_t getSize() const { return Stmts.size(); }
2103 /// @name Statements Iterators
2105 /// These iterators iterate over all statements of this Scop.
2106 //@{
2107 typedef StmtSet::iterator iterator;
2108 typedef StmtSet::const_iterator const_iterator;
2110 iterator begin() { return Stmts.begin(); }
2111 iterator end() { return Stmts.end(); }
2112 const_iterator begin() const { return Stmts.begin(); }
2113 const_iterator end() const { return Stmts.end(); }
2115 typedef StmtSet::reverse_iterator reverse_iterator;
2116 typedef StmtSet::const_reverse_iterator const_reverse_iterator;
2118 reverse_iterator rbegin() { return Stmts.rbegin(); }
2119 reverse_iterator rend() { return Stmts.rend(); }
2120 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
2121 const_reverse_iterator rend() const { return Stmts.rend(); }
2122 //@}
2124 /// @brief Return the set of required invariant loads.
2125 const InvariantLoadsSetTy &getRequiredInvariantLoads() const {
2126 return DC.RequiredILS;
2129 /// @brief Add @p LI to the set of required invariant loads.
2130 void addRequiredInvariantLoad(LoadInst *LI) { DC.RequiredILS.insert(LI); }
2132 /// @brief Return true if and only if @p LI is a required invariant load.
2133 bool isRequiredInvariantLoad(LoadInst *LI) const {
2134 return getRequiredInvariantLoads().count(LI);
2137 /// @brief Return the set of boxed (thus overapproximated) loops.
2138 const BoxedLoopsSetTy &getBoxedLoops() const { return DC.BoxedLoopsSet; }
2140 /// @brief Return true if and only if @p R is a non-affine subregion.
2141 bool isNonAffineSubRegion(const Region *R) {
2142 return DC.NonAffineSubRegionSet.count(R);
2145 const MapInsnToMemAcc &getInsnToMemAccMap() const { return DC.InsnToMemAcc; }
2147 /// @brief Return the (possibly new) ScopArrayInfo object for @p Access.
2149 /// @param ElementType The type of the elements stored in this array.
2150 /// @param Kind The kind of the array info object.
2151 const ScopArrayInfo *getOrCreateScopArrayInfo(Value *BasePtr,
2152 Type *ElementType,
2153 ArrayRef<const SCEV *> Sizes,
2154 ScopArrayInfo::MemoryKind Kind);
2156 /// @brief Return the cached ScopArrayInfo object for @p BasePtr.
2158 /// @param BasePtr The base pointer the object has been stored for.
2159 /// @param Kind The kind of array info object.
2160 const ScopArrayInfo *getScopArrayInfo(Value *BasePtr,
2161 ScopArrayInfo::MemoryKind Kind);
2163 /// @brief Invalidate ScopArrayInfo object for base address.
2165 /// @param BasePtr The base pointer of the ScopArrayInfo object to invalidate.
2166 /// @param Kind The Kind of the ScopArrayInfo object.
2167 void invalidateScopArrayInfo(Value *BasePtr, ScopArrayInfo::MemoryKind Kind) {
2168 ScopArrayInfoMap.erase(std::make_pair(BasePtr, Kind));
2171 void setContext(isl_set *NewContext);
2173 /// @brief Align the parameters in the statement to the scop context
2174 void realignParams();
2176 /// @brief Return true if this SCoP can be profitably optimized.
2177 bool isProfitable() const;
2179 /// @brief Return true if the SCoP contained at least one error block.
2180 bool hasErrorBlock() const { return HasErrorBlock; }
2182 /// @brief Return true if the underlying region has a single exiting block.
2183 bool hasSingleExitEdge() const { return HasSingleExitEdge; }
2185 /// @brief Print the static control part.
2187 /// @param OS The output stream the static control part is printed to.
2188 void print(raw_ostream &OS) const;
2190 /// @brief Print the ScopStmt to stderr.
2191 void dump() const;
2193 /// @brief Get the isl context of this static control part.
2195 /// @return The isl context of this static control part.
2196 isl_ctx *getIslCtx() const;
2198 /// @brief Directly return the shared_ptr of the context.
2199 const std::shared_ptr<isl_ctx> &getSharedIslCtx() const { return IslCtx; }
2201 /// @brief Compute the isl representation for the SCEV @p E
2203 /// @param E The SCEV that should be translated.
2204 /// @param BB An (optional) basic block in which the isl_pw_aff is computed.
2205 /// SCEVs known to not reference any loops in the SCoP can be
2206 /// passed without a @p BB.
2207 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
2209 /// Note that this function will always return a valid isl_pw_aff. However, if
2210 /// the translation of @p E was deemed to complex the SCoP is invalidated and
2211 /// a dummy value of appropriate dimension is returned. This allows to bail
2212 /// for complex cases without "error handling code" needed on the users side.
2213 __isl_give PWACtx getPwAff(const SCEV *E, BasicBlock *BB = nullptr,
2214 bool NonNegative = false);
2216 /// @brief Compute the isl representation for the SCEV @p E
2218 /// This function is like @see Scop::getPwAff() but strips away the invalid
2219 /// domain part associated with the piecewise affine function.
2220 __isl_give isl_pw_aff *getPwAffOnly(const SCEV *E, BasicBlock *BB = nullptr);
2222 /// @brief Return the domain of @p Stmt.
2224 /// @param Stmt The statement for which the conditions should be returned.
2225 __isl_give isl_set *getDomainConditions(const ScopStmt *Stmt) const;
2227 /// @brief Return the domain of @p BB.
2229 /// @param BB The block for which the conditions should be returned.
2230 __isl_give isl_set *getDomainConditions(BasicBlock *BB) const;
2232 /// @brief Get a union set containing the iteration domains of all statements.
2233 __isl_give isl_union_set *getDomains() const;
2235 /// @brief Get a union map of all may-writes performed in the SCoP.
2236 __isl_give isl_union_map *getMayWrites();
2238 /// @brief Get a union map of all must-writes performed in the SCoP.
2239 __isl_give isl_union_map *getMustWrites();
2241 /// @brief Get a union map of all writes performed in the SCoP.
2242 __isl_give isl_union_map *getWrites();
2244 /// @brief Get a union map of all reads performed in the SCoP.
2245 __isl_give isl_union_map *getReads();
2247 /// @brief Get a union map of all memory accesses performed in the SCoP.
2248 __isl_give isl_union_map *getAccesses();
2250 /// @brief Get the schedule of all the statements in the SCoP.
2251 __isl_give isl_union_map *getSchedule() const;
2253 /// @brief Get a schedule tree describing the schedule of all statements.
2254 __isl_give isl_schedule *getScheduleTree() const;
2256 /// @brief Update the current schedule
2258 /// @brief NewSchedule The new schedule (given as a flat union-map).
2259 void setSchedule(__isl_take isl_union_map *NewSchedule);
2261 /// @brief Update the current schedule
2263 /// @brief NewSchedule The new schedule (given as schedule tree).
2264 void setScheduleTree(__isl_take isl_schedule *NewSchedule);
2266 /// @brief Intersects the domains of all statements in the SCoP.
2268 /// @return true if a change was made
2269 bool restrictDomains(__isl_take isl_union_set *Domain);
2271 /// @brief Get the depth of a loop relative to the outermost loop in the Scop.
2273 /// This will return
2274 /// 0 if @p L is an outermost loop in the SCoP
2275 /// >0 for other loops in the SCoP
2276 /// -1 if @p L is nullptr or there is no outermost loop in the SCoP
2277 int getRelativeLoopDepth(const Loop *L) const;
2280 /// @brief Print Scop scop to raw_ostream O.
2281 static inline raw_ostream &operator<<(raw_ostream &O, const Scop &scop) {
2282 scop.print(O);
2283 return O;
2286 /// @brief The legacy pass manager's analysis pass to compute scop information
2287 /// for a region.
2288 class ScopInfoRegionPass : public RegionPass {
2289 /// @brief The Scop pointer which is used to construct a Scop.
2290 std::unique_ptr<Scop> S;
2292 public:
2293 static char ID; // Pass identification, replacement for typeid
2295 ScopInfoRegionPass() : RegionPass(ID) {}
2296 ~ScopInfoRegionPass() {}
2298 /// @brief Build Scop object, the Polly IR of static control
2299 /// part for the current SESE-Region.
2301 /// @return If the current region is a valid for a static control part,
2302 /// return the Polly IR representing this static control part,
2303 /// return null otherwise.
2304 Scop *getScop() { return S.get(); }
2305 const Scop *getScop() const { return S.get(); }
2307 /// @brief Calculate the polyhedral scop information for a given Region.
2308 bool runOnRegion(Region *R, RGPassManager &RGM) override;
2310 void releaseMemory() override { S.reset(); }
2312 void print(raw_ostream &O, const Module *M = nullptr) const override;
2314 void getAnalysisUsage(AnalysisUsage &AU) const override;
2317 //===----------------------------------------------------------------------===//
2318 /// @brief The legacy pass manager's analysis pass to compute scop information
2319 /// for the whole function.
2321 /// This pass will maintain a map of the maximal region within a scop to its
2322 /// scop object for all the feasible scops present in a function.
2323 /// This pass is an alternative to the ScopInfoRegionPass in order to avoid a
2324 /// region pass manager.
2325 class ScopInfoWrapperPass : public FunctionPass {
2327 public:
2328 using RegionToScopMapTy = DenseMap<Region *, std::unique_ptr<Scop>>;
2329 using iterator = RegionToScopMapTy::iterator;
2330 using const_iterator = RegionToScopMapTy::const_iterator;
2332 private:
2333 /// @brief A map of Region to its Scop object containing
2334 /// Polly IR of static control part
2335 RegionToScopMapTy RegionToScopMap;
2337 public:
2338 static char ID; // Pass identification, replacement for typeid
2340 ScopInfoWrapperPass() : FunctionPass(ID) {}
2341 ~ScopInfoWrapperPass() {}
2343 /// @brief Get the Scop object for the given Region
2345 /// @return If the given region is the maximal region within a scop, return
2346 /// the scop object. If the given region is a subregion, return a
2347 /// nullptr. Top level region containing the entry block of a function
2348 /// is not considered in the scop creation.
2349 Scop *getScop(Region *R) const {
2350 auto MapIt = RegionToScopMap.find(R);
2351 if (MapIt != RegionToScopMap.end())
2352 return MapIt->second.get();
2353 return nullptr;
2356 iterator begin() { return RegionToScopMap.begin(); }
2357 iterator end() { return RegionToScopMap.end(); }
2358 const_iterator begin() const { return RegionToScopMap.begin(); }
2359 const_iterator end() const { return RegionToScopMap.end(); }
2361 /// @brief Calculate all the polyhedral scops for a given function.
2362 bool runOnFunction(Function &F) override;
2364 void releaseMemory() override { RegionToScopMap.clear(); }
2366 void print(raw_ostream &O, const Module *M = nullptr) const override;
2368 void getAnalysisUsage(AnalysisUsage &AU) const override;
2371 } // end namespace polly
2373 namespace llvm {
2374 class PassRegistry;
2375 void initializeScopInfoRegionPassPass(llvm::PassRegistry &);
2376 void initializeScopInfoWrapperPassPass(llvm::PassRegistry &);
2377 } // namespace llvm
2379 #endif