[ScopInfo] Keep scalar acceess dictionaries up-to-data. NFC.
[polly-mirror.git] / include / polly / ScopInfo.h
blobfbaa5a83b597e1adff3b0272490c0f5a28be4121
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 extern bool UseInstructionNames;
71 /// Enumeration of assumptions Polly can take.
72 enum AssumptionKind {
73 ALIASING,
74 INBOUNDS,
75 WRAPPING,
76 UNSIGNED,
77 PROFITABLE,
78 ERRORBLOCK,
79 COMPLEXITY,
80 INFINITELOOP,
81 INVARIANTLOAD,
82 DELINEARIZATION,
85 /// Enum to distinguish between assumptions and restrictions.
86 enum AssumptionSign { AS_ASSUMPTION, AS_RESTRICTION };
88 /// The different memory kinds used in Polly.
89 ///
90 /// We distinguish between arrays and various scalar memory objects. We use
91 /// the term ``array'' to describe memory objects that consist of a set of
92 /// individual data elements arranged in a multi-dimensional grid. A scalar
93 /// memory object describes an individual data element and is used to model
94 /// the definition and uses of llvm::Values.
95 ///
96 /// The polyhedral model does traditionally not reason about SSA values. To
97 /// reason about llvm::Values we model them "as if" they were zero-dimensional
98 /// memory objects, even though they were not actually allocated in (main)
99 /// memory. Memory for such objects is only alloca[ed] at CodeGeneration
100 /// time. To relate the memory slots used during code generation with the
101 /// llvm::Values they belong to the new names for these corresponding stack
102 /// slots are derived by appending suffixes (currently ".s2a" and ".phiops")
103 /// to the name of the original llvm::Value. To describe how def/uses are
104 /// modeled exactly we use these suffixes here as well.
106 /// There are currently four different kinds of memory objects:
107 enum class MemoryKind {
108 /// MemoryKind::Array: Models a one or multi-dimensional array
110 /// A memory object that can be described by a multi-dimensional array.
111 /// Memory objects of this type are used to model actual multi-dimensional
112 /// arrays as they exist in LLVM-IR, but they are also used to describe
113 /// other objects:
114 /// - A single data element allocated on the stack using 'alloca' is
115 /// modeled as a one-dimensional, single-element array.
116 /// - A single data element allocated as a global variable is modeled as
117 /// one-dimensional, single-element array.
118 /// - Certain multi-dimensional arrays with variable size, which in
119 /// LLVM-IR are commonly expressed as a single-dimensional access with a
120 /// complicated access function, are modeled as multi-dimensional
121 /// memory objects (grep for "delinearization").
122 Array,
124 /// MemoryKind::Value: Models an llvm::Value
126 /// Memory objects of type MemoryKind::Value are used to model the data flow
127 /// induced by llvm::Values. For each llvm::Value that is used across
128 /// BasicBocks one ScopArrayInfo object is created. A single memory WRITE
129 /// stores the llvm::Value at its definition into the memory object and at
130 /// each use of the llvm::Value (ignoring trivial intra-block uses) a
131 /// corresponding READ is added. For instance, the use/def chain of a
132 /// llvm::Value %V depicted below
133 /// ______________________
134 /// |DefBB: |
135 /// | %V = float op ... |
136 /// ----------------------
137 /// | |
138 /// _________________ _________________
139 /// |UseBB1: | |UseBB2: |
140 /// | use float %V | | use float %V |
141 /// ----------------- -----------------
143 /// is modeled as if the following memory accesses occured:
145 /// __________________________
146 /// |entry: |
147 /// | %V.s2a = alloca float |
148 /// --------------------------
149 /// |
150 /// ___________________________________
151 /// |DefBB: |
152 /// | store %float %V, float* %V.s2a |
153 /// -----------------------------------
154 /// | |
155 /// ____________________________________ ___________________________________
156 /// |UseBB1: | |UseBB2: |
157 /// | %V.reload1 = load float* %V.s2a | | %V.reload2 = load float* %V.s2a|
158 /// | use float %V.reload1 | | use float %V.reload2 |
159 /// ------------------------------------ -----------------------------------
161 Value,
163 /// MemoryKind::PHI: Models PHI nodes within the SCoP
165 /// Besides the MemoryKind::Value memory object used to model the normal
166 /// llvm::Value dependences described above, PHI nodes require an additional
167 /// memory object of type MemoryKind::PHI to describe the forwarding of values
168 /// to
169 /// the PHI node.
171 /// As an example, a PHIInst instructions
173 /// %PHI = phi float [ %Val1, %IncomingBlock1 ], [ %Val2, %IncomingBlock2 ]
175 /// is modeled as if the accesses occured this way:
177 /// _______________________________
178 /// |entry: |
179 /// | %PHI.phiops = alloca float |
180 /// -------------------------------
181 /// | |
182 /// __________________________________ __________________________________
183 /// |IncomingBlock1: | |IncomingBlock2: |
184 /// | ... | | ... |
185 /// | store float %Val1 %PHI.phiops | | store float %Val2 %PHI.phiops |
186 /// | br label % JoinBlock | | br label %JoinBlock |
187 /// ---------------------------------- ----------------------------------
188 /// \ /
189 /// \ /
190 /// _________________________________________
191 /// |JoinBlock: |
192 /// | %PHI = load float, float* PHI.phiops |
193 /// -----------------------------------------
195 /// Note that there can also be a scalar write access for %PHI if used in a
196 /// different BasicBlock, i.e. there can be a memory object %PHI.phiops as
197 /// well as a memory object %PHI.s2a.
198 PHI,
200 /// MemoryKind::ExitPHI: Models PHI nodes in the SCoP's exit block
202 /// For PHI nodes in the Scop's exit block a special memory object kind is
203 /// used. The modeling used is identical to MemoryKind::PHI, with the
204 /// exception
205 /// that there are no READs from these memory objects. The PHINode's
206 /// llvm::Value is treated as a value escaping the SCoP. WRITE accesses
207 /// write directly to the escaping value's ".s2a" alloca.
208 ExitPHI
211 /// Maps from a loop to the affine function expressing its backedge taken count.
212 /// The backedge taken count already enough to express iteration domain as we
213 /// only allow loops with canonical induction variable.
214 /// A canonical induction variable is:
215 /// an integer recurrence that starts at 0 and increments by one each time
216 /// through the loop.
217 typedef std::map<const Loop *, const SCEV *> LoopBoundMapType;
219 typedef std::vector<std::unique_ptr<MemoryAccess>> AccFuncVector;
221 /// A class to store information about arrays in the SCoP.
223 /// Objects are accessible via the ScoP, MemoryAccess or the id associated with
224 /// the MemoryAccess access function.
226 class ScopArrayInfo {
227 public:
228 /// Construct a ScopArrayInfo object.
230 /// @param BasePtr The array base pointer.
231 /// @param ElementType The type of the elements stored in the array.
232 /// @param IslCtx The isl context used to create the base pointer id.
233 /// @param DimensionSizes A vector containing the size of each dimension.
234 /// @param Kind The kind of the array object.
235 /// @param DL The data layout of the module.
236 /// @param S The scop this array object belongs to.
237 /// @param BaseName The optional name of this memory reference.
238 ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *IslCtx,
239 ArrayRef<const SCEV *> DimensionSizes, MemoryKind Kind,
240 const DataLayout &DL, Scop *S, const char *BaseName = nullptr);
242 /// Update the element type of the ScopArrayInfo object.
244 /// Memory accesses referencing this ScopArrayInfo object may use
245 /// different element sizes. This function ensures the canonical element type
246 /// stored is small enough to model accesses to the current element type as
247 /// well as to @p NewElementType.
249 /// @param NewElementType An element type that is used to access this array.
250 void updateElementType(Type *NewElementType);
252 /// Update the sizes of the ScopArrayInfo object.
254 /// A ScopArrayInfo object may be created without all outer dimensions being
255 /// available. This function is called when new memory accesses are added for
256 /// this ScopArrayInfo object. It verifies that sizes are compatible and adds
257 /// additional outer array dimensions, if needed.
259 /// @param Sizes A vector of array sizes where the rightmost array
260 /// sizes need to match the innermost array sizes already
261 /// defined in SAI.
262 /// @param CheckConsistency Update sizes, even if new sizes are inconsistent
263 /// with old sizes
264 bool updateSizes(ArrayRef<const SCEV *> Sizes, bool CheckConsistency = true);
266 /// Destructor to free the isl id of the base pointer.
267 ~ScopArrayInfo();
269 /// Set the base pointer to @p BP.
270 void setBasePtr(Value *BP) { BasePtr = BP; }
272 /// Return the base pointer.
273 Value *getBasePtr() const { return BasePtr; }
275 /// For indirect accesses return the origin SAI of the BP, else null.
276 const ScopArrayInfo *getBasePtrOriginSAI() const { return BasePtrOriginSAI; }
278 /// The set of derived indirect SAIs for this origin SAI.
279 const SmallSetVector<ScopArrayInfo *, 2> &getDerivedSAIs() const {
280 return DerivedSAIs;
283 /// Return the number of dimensions.
284 unsigned getNumberOfDimensions() const {
285 if (Kind == MemoryKind::PHI || Kind == MemoryKind::ExitPHI ||
286 Kind == MemoryKind::Value)
287 return 0;
288 return DimensionSizes.size();
291 /// Return the size of dimension @p dim as SCEV*.
293 // Scalars do not have array dimensions and the first dimension of
294 // a (possibly multi-dimensional) array also does not carry any size
295 // information, in case the array is not newly created.
296 const SCEV *getDimensionSize(unsigned Dim) const {
297 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
298 return DimensionSizes[Dim];
301 /// Return the size of dimension @p dim as isl_pw_aff.
303 // Scalars do not have array dimensions and the first dimension of
304 // a (possibly multi-dimensional) array also does not carry any size
305 // information, in case the array is not newly created.
306 __isl_give isl_pw_aff *getDimensionSizePw(unsigned Dim) const {
307 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
308 return isl_pw_aff_copy(DimensionSizesPw[Dim]);
311 /// Get the canonical element type of this array.
313 /// @returns The canonical element type of this array.
314 Type *getElementType() const { return ElementType; }
316 /// Get element size in bytes.
317 int getElemSizeInBytes() const;
319 /// Get the name of this memory reference.
320 std::string getName() const;
322 /// Return the isl id for the base pointer.
323 __isl_give isl_id *getBasePtrId() const;
325 /// Return what kind of memory this represents.
326 MemoryKind getKind() const { return Kind; }
328 /// Is this array info modeling an llvm::Value?
329 bool isValueKind() const { return Kind == MemoryKind::Value; }
331 /// Is this array info modeling special PHI node memory?
333 /// During code generation of PHI nodes, there is a need for two kinds of
334 /// virtual storage. The normal one as it is used for all scalar dependences,
335 /// where the result of the PHI node is stored and later loaded from as well
336 /// as a second one where the incoming values of the PHI nodes are stored
337 /// into and reloaded when the PHI is executed. As both memories use the
338 /// original PHI node as virtual base pointer, we have this additional
339 /// attribute to distinguish the PHI node specific array modeling from the
340 /// normal scalar array modeling.
341 bool isPHIKind() const { return Kind == MemoryKind::PHI; }
343 /// Is this array info modeling an MemoryKind::ExitPHI?
344 bool isExitPHIKind() const { return Kind == MemoryKind::ExitPHI; }
346 /// Is this array info modeling an array?
347 bool isArrayKind() const { return Kind == MemoryKind::Array; }
349 /// Dump a readable representation to stderr.
350 void dump() const;
352 /// Print a readable representation to @p OS.
354 /// @param SizeAsPwAff Print the size as isl_pw_aff
355 void print(raw_ostream &OS, bool SizeAsPwAff = false) const;
357 /// Access the ScopArrayInfo associated with an access function.
358 static const ScopArrayInfo *
359 getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA);
361 /// Access the ScopArrayInfo associated with an isl Id.
362 static const ScopArrayInfo *getFromId(__isl_take isl_id *Id);
364 /// Get the space of this array access.
365 __isl_give isl_space *getSpace() const;
367 /// If the array is read only
368 bool isReadOnly();
370 /// Verify that @p Array is compatible to this ScopArrayInfo.
372 /// Two arrays are compatible if their dimensionality, the sizes of their
373 /// dimensions, and their element sizes match.
375 /// @param Array The array to compare against.
377 /// @returns True, if the arrays are compatible, False otherwise.
378 bool isCompatibleWith(const ScopArrayInfo *Array) const;
380 private:
381 void addDerivedSAI(ScopArrayInfo *DerivedSAI) {
382 DerivedSAIs.insert(DerivedSAI);
385 /// For indirect accesses this is the SAI of the BP origin.
386 const ScopArrayInfo *BasePtrOriginSAI;
388 /// For origin SAIs the set of derived indirect SAIs.
389 SmallSetVector<ScopArrayInfo *, 2> DerivedSAIs;
391 /// The base pointer.
392 AssertingVH<Value> BasePtr;
394 /// The canonical element type of this array.
396 /// The canonical element type describes the minimal accessible element in
397 /// this array. Not all elements accessed, need to be of the very same type,
398 /// but the allocation size of the type of the elements loaded/stored from/to
399 /// this array needs to be a multiple of the allocation size of the canonical
400 /// type.
401 Type *ElementType;
403 /// The isl id for the base pointer.
404 isl_id *Id;
406 /// The sizes of each dimension as SCEV*.
407 SmallVector<const SCEV *, 4> DimensionSizes;
409 /// The sizes of each dimension as isl_pw_aff.
410 SmallVector<isl_pw_aff *, 4> DimensionSizesPw;
412 /// The type of this scop array info object.
414 /// We distinguish between SCALAR, PHI and ARRAY objects.
415 MemoryKind Kind;
417 /// The data layout of the module.
418 const DataLayout &DL;
420 /// The scop this SAI object belongs to.
421 Scop &S;
424 /// Represent memory accesses in statements.
425 class MemoryAccess {
426 friend class Scop;
427 friend class ScopStmt;
429 public:
430 /// The access type of a memory access
432 /// There are three kind of access types:
434 /// * A read access
436 /// A certain set of memory locations are read and may be used for internal
437 /// calculations.
439 /// * A must-write access
441 /// A certain set of memory locations is definitely written. The old value is
442 /// replaced by a newly calculated value. The old value is not read or used at
443 /// all.
445 /// * A may-write access
447 /// A certain set of memory locations may be written. The memory location may
448 /// contain a new value if there is actually a write or the old value may
449 /// remain, if no write happens.
450 enum AccessType {
451 READ = 0x1,
452 MUST_WRITE = 0x2,
453 MAY_WRITE = 0x3,
456 /// Reduction access type
458 /// Commutative and associative binary operations suitable for reductions
459 enum ReductionType {
460 RT_NONE, ///< Indicate no reduction at all
461 RT_ADD, ///< Addition
462 RT_MUL, ///< Multiplication
463 RT_BOR, ///< Bitwise Or
464 RT_BXOR, ///< Bitwise XOr
465 RT_BAND, ///< Bitwise And
468 private:
469 MemoryAccess(const MemoryAccess &) = delete;
470 const MemoryAccess &operator=(const MemoryAccess &) = delete;
472 /// A unique identifier for this memory access.
474 /// The identifier is unique between all memory accesses belonging to the same
475 /// scop statement.
476 isl_id *Id;
478 /// What is modeled by this MemoryAccess.
479 /// @see MemoryKind
480 MemoryKind Kind;
482 /// Whether it a reading or writing access, and if writing, whether it
483 /// is conditional (MAY_WRITE).
484 enum AccessType AccType;
486 /// Reduction type for reduction like accesses, RT_NONE otherwise
488 /// An access is reduction like if it is part of a load-store chain in which
489 /// both access the same memory location (use the same LLVM-IR value
490 /// as pointer reference). Furthermore, between the load and the store there
491 /// is exactly one binary operator which is known to be associative and
492 /// commutative.
494 /// TODO:
496 /// We can later lift the constraint that the same LLVM-IR value defines the
497 /// memory location to handle scops such as the following:
499 /// for i
500 /// for j
501 /// sum[i+j] = sum[i] + 3;
503 /// Here not all iterations access the same memory location, but iterations
504 /// for which j = 0 holds do. After lifting the equality check in ScopBuilder,
505 /// subsequent transformations do not only need check if a statement is
506 /// reduction like, but they also need to verify that that the reduction
507 /// property is only exploited for statement instances that load from and
508 /// store to the same data location. Doing so at dependence analysis time
509 /// could allow us to handle the above example.
510 ReductionType RedType = RT_NONE;
512 /// Parent ScopStmt of this access.
513 ScopStmt *Statement;
515 /// The domain under which this access is not modeled precisely.
517 /// The invalid domain for an access describes all parameter combinations
518 /// under which the statement looks to be executed but is in fact not because
519 /// some assumption/restriction makes the access invalid.
520 isl_set *InvalidDomain;
522 // Properties describing the accessed array.
523 // TODO: It might be possible to move them to ScopArrayInfo.
524 // @{
526 /// The base address (e.g., A for A[i+j]).
528 /// The #BaseAddr of a memory access of kind MemoryKind::Array is the base
529 /// pointer of the memory access.
530 /// The #BaseAddr of a memory access of kind MemoryKind::PHI or
531 /// MemoryKind::ExitPHI is the PHI node itself.
532 /// The #BaseAddr of a memory access of kind MemoryKind::Value is the
533 /// instruction defining the value.
534 AssertingVH<Value> BaseAddr;
536 /// An unique name of the accessed array.
537 std::string BaseName;
539 /// Type a single array element wrt. this access.
540 Type *ElementType;
542 /// Size of each dimension of the accessed array.
543 SmallVector<const SCEV *, 4> Sizes;
544 // @}
546 // Properties describing the accessed element.
547 // @{
549 /// The access instruction of this memory access.
551 /// For memory accesses of kind MemoryKind::Array the access instruction is
552 /// the Load or Store instruction performing the access.
554 /// For memory accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI the
555 /// access instruction of a load access is the PHI instruction. The access
556 /// instruction of a PHI-store is the incoming's block's terminator
557 /// instruction.
559 /// For memory accesses of kind MemoryKind::Value the access instruction of a
560 /// load access is nullptr because generally there can be multiple
561 /// instructions in the statement using the same llvm::Value. The access
562 /// instruction of a write access is the instruction that defines the
563 /// llvm::Value.
564 Instruction *AccessInstruction;
566 /// Incoming block and value of a PHINode.
567 SmallVector<std::pair<BasicBlock *, Value *>, 4> Incoming;
569 /// The value associated with this memory access.
571 /// - For array memory accesses (MemoryKind::Array) it is the loaded result
572 /// or the stored value. If the access instruction is a memory intrinsic it
573 /// the access value is also the memory intrinsic.
574 /// - For accesses of kind MemoryKind::Value it is the access instruction
575 /// itself.
576 /// - For accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI it is the
577 /// PHI node itself (for both, READ and WRITE accesses).
579 AssertingVH<Value> AccessValue;
581 /// Are all the subscripts affine expression?
582 bool IsAffine;
584 /// Subscript expression for each dimension.
585 SmallVector<const SCEV *, 4> Subscripts;
587 /// Relation from statement instances to the accessed array elements.
589 /// In the common case this relation is a function that maps a set of loop
590 /// indices to the memory address from which a value is loaded/stored:
592 /// for i
593 /// for j
594 /// S: A[i + 3 j] = ...
596 /// => { S[i,j] -> A[i + 3j] }
598 /// In case the exact access function is not known, the access relation may
599 /// also be a one to all mapping { S[i,j] -> A[o] } describing that any
600 /// element accessible through A might be accessed.
602 /// In case of an access to a larger element belonging to an array that also
603 /// contains smaller elements, the access relation models the larger access
604 /// with multiple smaller accesses of the size of the minimal array element
605 /// type:
607 /// short *A;
609 /// for i
610 /// S: A[i] = *((double*)&A[4 * i]);
612 /// => { S[i] -> A[i]; S[i] -> A[o] : 4i <= o <= 4i + 3 }
613 isl_map *AccessRelation;
615 /// Updated access relation read from JSCOP file.
616 isl_map *NewAccessRelation;
618 /// Fortran arrays that are created using "Allocate" are stored in terms
619 /// of a descriptor struct. This maintains a raw pointer to the memory,
620 /// along with auxiliary fields with information such as dimensions.
621 /// We hold a reference to the descriptor corresponding to a MemoryAccess
622 /// into a Fortran array. FAD for "Fortran Array Descriptor"
623 AssertingVH<GlobalValue> FAD;
624 // @}
626 __isl_give isl_basic_map *createBasicAccessMap(ScopStmt *Statement);
628 void assumeNoOutOfBound();
630 /// Compute bounds on an over approximated access relation.
632 /// @param ElementSize The size of one element accessed.
633 void computeBoundsOnAccessRelation(unsigned ElementSize);
635 /// Get the original access function as read from IR.
636 __isl_give isl_map *getOriginalAccessRelation() const;
638 /// Return the space in which the access relation lives in.
639 __isl_give isl_space *getOriginalAccessRelationSpace() const;
641 /// Get the new access function imported or set by a pass
642 __isl_give isl_map *getNewAccessRelation() const;
644 /// Fold the memory access to consider parameteric offsets
646 /// To recover memory accesses with array size parameters in the subscript
647 /// expression we post-process the delinearization results.
649 /// We would normally recover from an access A[exp0(i) * N + exp1(i)] into an
650 /// array A[][N] the 2D access A[exp0(i)][exp1(i)]. However, another valid
651 /// delinearization is A[exp0(i) - 1][exp1(i) + N] which - depending on the
652 /// range of exp1(i) - may be preferrable. Specifically, for cases where we
653 /// know exp1(i) is negative, we want to choose the latter expression.
655 /// As we commonly do not have any information about the range of exp1(i),
656 /// we do not choose one of the two options, but instead create a piecewise
657 /// access function that adds the (-1, N) offsets as soon as exp1(i) becomes
658 /// negative. For a 2D array such an access function is created by applying
659 /// the piecewise map:
661 /// [i,j] -> [i, j] : j >= 0
662 /// [i,j] -> [i-1, j+N] : j < 0
664 /// We can generalize this mapping to arbitrary dimensions by applying this
665 /// piecewise mapping pairwise from the rightmost to the leftmost access
666 /// dimension. It would also be possible to cover a wider range by introducing
667 /// more cases and adding multiple of Ns to these cases. However, this has
668 /// not yet been necessary.
669 /// The introduction of different cases necessarily complicates the memory
670 /// access function, but cases that can be statically proven to not happen
671 /// will be eliminated later on.
672 void foldAccessRelation();
674 /// Create the access relation for the underlying memory intrinsic.
675 void buildMemIntrinsicAccessRelation();
677 /// Assemble the access relation from all available information.
679 /// In particular, used the information passes in the constructor and the
680 /// parent ScopStmt set by setStatment().
682 /// @param SAI Info object for the accessed array.
683 void buildAccessRelation(const ScopArrayInfo *SAI);
685 /// Carry index overflows of dimensions with constant size to the next higher
686 /// dimension.
688 /// For dimensions that have constant size, modulo the index by the size and
689 /// add up the carry (floored division) to the next higher dimension. This is
690 /// how overflow is defined in row-major order.
691 /// It happens e.g. when ScalarEvolution computes the offset to the base
692 /// pointer and would algebraically sum up all lower dimensions' indices of
693 /// constant size.
695 /// Example:
696 /// float (*A)[4];
697 /// A[1][6] -> A[2][2]
698 void wrapConstantDimensions();
700 public:
701 /// Create a new MemoryAccess.
703 /// @param Stmt The parent statement.
704 /// @param AccessInst The instruction doing the access.
705 /// @param BaseAddr The accessed array's address.
706 /// @param ElemType The type of the accessed array elements.
707 /// @param AccType Whether read or write access.
708 /// @param IsAffine Whether the subscripts are affine expressions.
709 /// @param Kind The kind of memory accessed.
710 /// @param Subscripts Subscipt expressions
711 /// @param Sizes Dimension lengths of the accessed array.
712 MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, AccessType AccType,
713 Value *BaseAddress, Type *ElemType, bool Affine,
714 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
715 Value *AccessValue, MemoryKind Kind);
717 /// Create a new MemoryAccess that corresponds to @p AccRel.
719 /// Along with @p Stmt and @p AccType it uses information about dimension
720 /// lengths of the accessed array, the type of the accessed array elements,
721 /// the name of the accessed array that is derived from the object accessible
722 /// via @p AccRel.
724 /// @param Stmt The parent statement.
725 /// @param AccType Whether read or write access.
726 /// @param AccRel The access relation that describes the memory access.
727 MemoryAccess(ScopStmt *Stmt, AccessType AccType, __isl_take isl_map *AccRel);
729 ~MemoryAccess();
731 /// Add a new incoming block/value pairs for this PHI/ExitPHI access.
733 /// @param IncomingBlock The PHI's incoming block.
734 /// @param IncomingValue The value when reacing the PHI from the @p
735 /// IncomingBlock.
736 void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue) {
737 assert(!isRead());
738 assert(isAnyPHIKind());
739 Incoming.emplace_back(std::make_pair(IncomingBlock, IncomingValue));
742 /// Return the list of possible PHI/ExitPHI values.
744 /// After code generation moves some PHIs around during region simplification,
745 /// we cannot reliably locate the original PHI node and its incoming values
746 /// anymore. For this reason we remember these explicitly for all PHI-kind
747 /// accesses.
748 ArrayRef<std::pair<BasicBlock *, Value *>> getIncoming() const {
749 assert(isAnyPHIKind());
750 return Incoming;
753 /// Get the type of a memory access.
754 enum AccessType getType() { return AccType; }
756 /// Is this a reduction like access?
757 bool isReductionLike() const { return RedType != RT_NONE; }
759 /// Is this a read memory access?
760 bool isRead() const { return AccType == MemoryAccess::READ; }
762 /// Is this a must-write memory access?
763 bool isMustWrite() const { return AccType == MemoryAccess::MUST_WRITE; }
765 /// Is this a may-write memory access?
766 bool isMayWrite() const { return AccType == MemoryAccess::MAY_WRITE; }
768 /// Is this a write memory access?
769 bool isWrite() const { return isMustWrite() || isMayWrite(); }
771 /// Is this a memory intrinsic access (memcpy, memset, memmove)?
772 bool isMemoryIntrinsic() const {
773 return isa<MemIntrinsic>(getAccessInstruction());
776 /// Check if a new access relation was imported or set by a pass.
777 bool hasNewAccessRelation() const { return NewAccessRelation; }
779 /// Return the newest access relation of this access.
781 /// There are two possibilities:
782 /// 1) The original access relation read from the LLVM-IR.
783 /// 2) A new access relation imported from a json file or set by another
784 /// pass (e.g., for privatization).
786 /// As 2) is by construction "newer" than 1) we return the new access
787 /// relation if present.
789 __isl_give isl_map *getLatestAccessRelation() const {
790 return hasNewAccessRelation() ? getNewAccessRelation()
791 : getOriginalAccessRelation();
794 /// Old name of getLatestAccessRelation().
795 __isl_give isl_map *getAccessRelation() const {
796 return getLatestAccessRelation();
799 /// Get an isl map describing the memory address accessed.
801 /// In most cases the memory address accessed is well described by the access
802 /// relation obtained with getAccessRelation. However, in case of arrays
803 /// accessed with types of different size the access relation maps one access
804 /// to multiple smaller address locations. This method returns an isl map that
805 /// relates each dynamic statement instance to the unique memory location
806 /// that is loaded from / stored to.
808 /// For an access relation { S[i] -> A[o] : 4i <= o <= 4i + 3 } this method
809 /// will return the address function { S[i] -> A[4i] }.
811 /// @returns The address function for this memory access.
812 __isl_give isl_map *getAddressFunction() const;
814 /// Return the access relation after the schedule was applied.
815 __isl_give isl_pw_multi_aff *
816 applyScheduleToAccessRelation(__isl_take isl_union_map *Schedule) const;
818 /// Get an isl string representing the access function read from IR.
819 std::string getOriginalAccessRelationStr() const;
821 /// Get an isl string representing a new access function, if available.
822 std::string getNewAccessRelationStr() const;
824 /// Get the original base address of this access (e.g. A for A[i+j]) when
825 /// detected.
827 /// This adress may differ from the base address referenced by the Original
828 /// ScopArrayInfo to which this array belongs, as this memory access may
829 /// have been unified to a ScopArray which has a different but identically
830 /// valued base pointer in case invariant load hoisting is enabled.
831 Value *getOriginalBaseAddr() const { return BaseAddr; }
833 /// Get the detection-time base array isl_id for this access.
834 __isl_give isl_id *getOriginalArrayId() const;
836 /// Get the base array isl_id for this access, modifiable through
837 /// setNewAccessRelation().
838 __isl_give isl_id *getLatestArrayId() const;
840 /// Old name of getOriginalArrayId().
841 __isl_give isl_id *getArrayId() const { return getOriginalArrayId(); }
843 /// Get the detection-time ScopArrayInfo object for the base address.
844 const ScopArrayInfo *getOriginalScopArrayInfo() const;
846 /// Get the ScopArrayInfo object for the base address, or the one set
847 /// by setNewAccessRelation().
848 const ScopArrayInfo *getLatestScopArrayInfo() const;
850 /// Legacy name of getOriginalScopArrayInfo().
851 const ScopArrayInfo *getScopArrayInfo() const {
852 return getOriginalScopArrayInfo();
855 /// Return a string representation of the access's reduction type.
856 const std::string getReductionOperatorStr() const;
858 /// Return a string representation of the reduction type @p RT.
859 static const std::string getReductionOperatorStr(ReductionType RT);
861 const std::string &getBaseName() const { return BaseName; }
863 /// Return the element type of the accessed array wrt. this access.
864 Type *getElementType() const { return ElementType; }
866 /// Return the access value of this memory access.
867 Value *getAccessValue() const { return AccessValue; }
869 /// Return the access instruction of this memory access.
870 Instruction *getAccessInstruction() const { return AccessInstruction; }
872 /// Return the number of access function subscript.
873 unsigned getNumSubscripts() const { return Subscripts.size(); }
875 /// Return the access function subscript in the dimension @p Dim.
876 const SCEV *getSubscript(unsigned Dim) const { return Subscripts[Dim]; }
878 /// Compute the isl representation for the SCEV @p E wrt. this access.
880 /// Note that this function will also adjust the invalid context accordingly.
881 __isl_give isl_pw_aff *getPwAff(const SCEV *E);
883 /// Get the invalid domain for this access.
884 __isl_give isl_set *getInvalidDomain() const {
885 return isl_set_copy(InvalidDomain);
888 /// Get the invalid context for this access.
889 __isl_give isl_set *getInvalidContext() const {
890 return isl_set_params(getInvalidDomain());
893 /// Get the stride of this memory access in the specified Schedule. Schedule
894 /// is a map from the statement to a schedule where the innermost dimension is
895 /// the dimension of the innermost loop containing the statement.
896 __isl_give isl_set *getStride(__isl_take const isl_map *Schedule) const;
898 /// Is the stride of the access equal to a certain width? Schedule is a map
899 /// from the statement to a schedule where the innermost dimension is the
900 /// dimension of the innermost loop containing the statement.
901 bool isStrideX(__isl_take const isl_map *Schedule, int StrideWidth) const;
903 /// Is consecutive memory accessed for a given statement instance set?
904 /// Schedule is a map from the statement to a schedule where the innermost
905 /// dimension is the dimension of the innermost loop containing the
906 /// statement.
907 bool isStrideOne(__isl_take const isl_map *Schedule) const;
909 /// Is always the same memory accessed for a given statement instance set?
910 /// Schedule is a map from the statement to a schedule where the innermost
911 /// dimension is the dimension of the innermost loop containing the
912 /// statement.
913 bool isStrideZero(__isl_take const isl_map *Schedule) const;
915 /// Return the kind when this access was first detected.
916 MemoryKind getOriginalKind() const {
917 assert(!getOriginalScopArrayInfo() /* not yet initialized */ ||
918 getOriginalScopArrayInfo()->getKind() == Kind);
919 return Kind;
922 /// Return the kind considering a potential setNewAccessRelation.
923 MemoryKind getLatestKind() const {
924 return getLatestScopArrayInfo()->getKind();
927 /// Whether this is an access of an explicit load or store in the IR.
928 bool isOriginalArrayKind() const {
929 return getOriginalKind() == MemoryKind::Array;
932 /// Whether storage memory is either an custom .s2a/.phiops alloca
933 /// (false) or an existing pointer into an array (true).
934 bool isLatestArrayKind() const {
935 return getLatestKind() == MemoryKind::Array;
938 /// Old name of isOriginalArrayKind.
939 bool isArrayKind() const { return isOriginalArrayKind(); }
941 /// Whether this access is an array to a scalar memory object, without
942 /// considering changes by setNewAccessRelation.
944 /// Scalar accesses are accesses to MemoryKind::Value, MemoryKind::PHI or
945 /// MemoryKind::ExitPHI.
946 bool isOriginalScalarKind() const {
947 return getOriginalKind() != MemoryKind::Array;
950 /// Whether this access is an array to a scalar memory object, also
951 /// considering changes by setNewAccessRelation.
952 bool isLatestScalarKind() const {
953 return getLatestKind() != MemoryKind::Array;
956 /// Old name of isOriginalScalarKind.
957 bool isScalarKind() const { return isOriginalScalarKind(); }
959 /// Was this MemoryAccess detected as a scalar dependences?
960 bool isOriginalValueKind() const {
961 return getOriginalKind() == MemoryKind::Value;
964 /// Is this MemoryAccess currently modeling scalar dependences?
965 bool isLatestValueKind() const {
966 return getLatestKind() == MemoryKind::Value;
969 /// Old name of isOriginalValueKind().
970 bool isValueKind() const { return isOriginalValueKind(); }
972 /// Was this MemoryAccess detected as a special PHI node access?
973 bool isOriginalPHIKind() const {
974 return getOriginalKind() == MemoryKind::PHI;
977 /// Is this MemoryAccess modeling special PHI node accesses, also
978 /// considering a potential change by setNewAccessRelation?
979 bool isLatestPHIKind() const { return getLatestKind() == MemoryKind::PHI; }
981 /// Old name of isOriginalPHIKind.
982 bool isPHIKind() const { return isOriginalPHIKind(); }
984 /// Was this MemoryAccess detected as the accesses of a PHI node in the
985 /// SCoP's exit block?
986 bool isOriginalExitPHIKind() const {
987 return getOriginalKind() == MemoryKind::ExitPHI;
990 /// Is this MemoryAccess modeling the accesses of a PHI node in the
991 /// SCoP's exit block? Can be changed to an array access using
992 /// setNewAccessRelation().
993 bool isLatestExitPHIKind() const {
994 return getLatestKind() == MemoryKind::ExitPHI;
997 /// Old name of isOriginalExitPHIKind().
998 bool isExitPHIKind() const { return isOriginalExitPHIKind(); }
1000 /// Was this access detected as one of the two PHI types?
1001 bool isOriginalAnyPHIKind() const {
1002 return isOriginalPHIKind() || isOriginalExitPHIKind();
1005 /// Does this access orginate from one of the two PHI types? Can be
1006 /// changed to an array access using setNewAccessRelation().
1007 bool isLatestAnyPHIKind() const {
1008 return isLatestPHIKind() || isLatestExitPHIKind();
1011 /// Old name of isOriginalAnyPHIKind().
1012 bool isAnyPHIKind() const { return isOriginalAnyPHIKind(); }
1014 /// Get the statement that contains this memory access.
1015 ScopStmt *getStatement() const { return Statement; }
1017 /// Get the reduction type of this access
1018 ReductionType getReductionType() const { return RedType; }
1020 /// Set the array descriptor corresponding to the Array on which the
1021 /// memory access is performed.
1022 void setFortranArrayDescriptor(GlobalValue *FAD);
1024 /// Update the original access relation.
1026 /// We need to update the original access relation during scop construction,
1027 /// when unifying the memory accesses that access the same scop array info
1028 /// object. After the scop has been constructed, the original access relation
1029 /// should not be changed any more. Instead setNewAccessRelation should
1030 /// be called.
1031 void setAccessRelation(__isl_take isl_map *AccessRelation);
1033 /// Set the updated access relation read from JSCOP file.
1034 void setNewAccessRelation(__isl_take isl_map *NewAccessRelation);
1036 /// Mark this a reduction like access
1037 void markAsReductionLike(ReductionType RT) { RedType = RT; }
1039 /// Align the parameters in the access relation to the scop context
1040 void realignParams();
1042 /// Update the dimensionality of the memory access.
1044 /// During scop construction some memory accesses may not be constructed with
1045 /// their full dimensionality, but outer dimensions may have been omitted if
1046 /// they took the value 'zero'. By updating the dimensionality of the
1047 /// statement we add additional zero-valued dimensions to match the
1048 /// dimensionality of the ScopArrayInfo object that belongs to this memory
1049 /// access.
1050 void updateDimensionality();
1052 /// Get identifier for the memory access.
1054 /// This identifier is unique for all accesses that belong to the same scop
1055 /// statement.
1056 __isl_give isl_id *getId() const;
1058 /// Print the MemoryAccess.
1060 /// @param OS The output stream the MemoryAccess is printed to.
1061 void print(raw_ostream &OS) const;
1063 /// Print the MemoryAccess to stderr.
1064 void dump() const;
1066 /// Is the memory access affine?
1067 bool isAffine() const { return IsAffine; }
1070 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1071 MemoryAccess::ReductionType RT);
1073 /// Ordered list type to hold accesses.
1074 using MemoryAccessList = std::forward_list<MemoryAccess *>;
1076 /// Helper structure for invariant memory accesses.
1077 struct InvariantAccess {
1078 /// The memory access that is (partially) invariant.
1079 MemoryAccess *MA;
1081 /// The context under which the access is not invariant.
1082 isl_set *NonHoistableCtx;
1085 /// Ordered container type to hold invariant accesses.
1086 using InvariantAccessesTy = SmallVector<InvariantAccess, 8>;
1088 /// Type for equivalent invariant accesses and their domain context.
1089 struct InvariantEquivClassTy {
1091 /// The pointer that identifies this equivalence class
1092 const SCEV *IdentifyingPointer;
1094 /// Memory accesses now treated invariant
1096 /// These memory accesses access the pointer location that identifies
1097 /// this equivalence class. They are treated as invariant and hoisted during
1098 /// code generation.
1099 MemoryAccessList InvariantAccesses;
1101 /// The execution context under which the memory location is accessed
1103 /// It is the union of the execution domains of the memory accesses in the
1104 /// InvariantAccesses list.
1105 isl_set *ExecutionContext;
1107 /// The type of the invariant access
1109 /// It is used to differentiate between differently typed invariant loads from
1110 /// the same location.
1111 Type *AccessType;
1114 /// Type for invariant accesses equivalence classes.
1115 using InvariantEquivClassesTy = SmallVector<InvariantEquivClassTy, 8>;
1117 /// Statement of the Scop
1119 /// A Scop statement represents an instruction in the Scop.
1121 /// It is further described by its iteration domain, its schedule and its data
1122 /// accesses.
1123 /// At the moment every statement represents a single basic block of LLVM-IR.
1124 class ScopStmt {
1125 public:
1126 ScopStmt(const ScopStmt &) = delete;
1127 const ScopStmt &operator=(const ScopStmt &) = delete;
1129 /// Create the ScopStmt from a BasicBlock.
1130 ScopStmt(Scop &parent, BasicBlock &bb, Loop *SurroundingLoop);
1132 /// Create an overapproximating ScopStmt for the region @p R.
1133 ScopStmt(Scop &parent, Region &R, Loop *SurroundingLoop);
1135 /// Create a copy statement.
1137 /// @param Stmt The parent statement.
1138 /// @param SourceRel The source location.
1139 /// @param TargetRel The target location.
1140 /// @param Domain The original domain under which copy statement whould
1141 /// be executed.
1142 ScopStmt(Scop &parent, __isl_take isl_map *SourceRel,
1143 __isl_take isl_map *TargetRel, __isl_take isl_set *Domain);
1145 /// Initialize members after all MemoryAccesses have been added.
1146 void init(LoopInfo &LI);
1148 private:
1149 /// Polyhedral description
1150 //@{
1152 /// The Scop containing this ScopStmt
1153 Scop &Parent;
1155 /// The domain under which this statement is not modeled precisely.
1157 /// The invalid domain for a statement describes all parameter combinations
1158 /// under which the statement looks to be executed but is in fact not because
1159 /// some assumption/restriction makes the statement/scop invalid.
1160 isl_set *InvalidDomain;
1162 /// The iteration domain describes the set of iterations for which this
1163 /// statement is executed.
1165 /// Example:
1166 /// for (i = 0; i < 100 + b; ++i)
1167 /// for (j = 0; j < i; ++j)
1168 /// S(i,j);
1170 /// 'S' is executed for different values of i and j. A vector of all
1171 /// induction variables around S (i, j) is called iteration vector.
1172 /// The domain describes the set of possible iteration vectors.
1174 /// In this case it is:
1176 /// Domain: 0 <= i <= 100 + b
1177 /// 0 <= j <= i
1179 /// A pair of statement and iteration vector (S, (5,3)) is called statement
1180 /// instance.
1181 isl_set *Domain;
1183 /// The memory accesses of this statement.
1185 /// The only side effects of a statement are its memory accesses.
1186 typedef SmallVector<MemoryAccess *, 8> MemoryAccessVec;
1187 MemoryAccessVec MemAccs;
1189 /// Mapping from instructions to (scalar) memory accesses.
1190 DenseMap<const Instruction *, MemoryAccessList> InstructionToAccess;
1192 /// The set of values defined elsewhere required in this ScopStmt and
1193 /// their MemoryKind::Value READ MemoryAccesses.
1194 DenseMap<Value *, MemoryAccess *> ValueReads;
1196 /// The set of values defined in this ScopStmt that are required
1197 /// elsewhere, mapped to their MemoryKind::Value WRITE MemoryAccesses.
1198 DenseMap<Instruction *, MemoryAccess *> ValueWrites;
1200 /// Map from PHI nodes to its incoming value when coming from this
1201 /// statement.
1203 /// Non-affine subregions can have multiple exiting blocks that are incoming
1204 /// blocks of the PHI nodes. This map ensures that there is only one write
1205 /// operation for the complete subregion. A PHI selecting the relevant value
1206 /// will be inserted.
1207 DenseMap<PHINode *, MemoryAccess *> PHIWrites;
1209 //@}
1211 /// A SCoP statement represents either a basic block (affine/precise case) or
1212 /// a whole region (non-affine case).
1214 /// Only one of the following two members will therefore be set and indicate
1215 /// which kind of statement this is.
1217 ///{
1219 /// The BasicBlock represented by this statement (in the affine case).
1220 BasicBlock *BB;
1222 /// The region represented by this statement (in the non-affine case).
1223 Region *R;
1225 ///}
1227 /// The isl AST build for the new generated AST.
1228 isl_ast_build *Build;
1230 SmallVector<Loop *, 4> NestLoops;
1232 std::string BaseName;
1234 /// The closest loop that contains this statement.
1235 Loop *SurroundingLoop;
1237 /// Build the statement.
1238 //@{
1239 void buildDomain();
1241 /// Fill NestLoops with loops surrounding this statement.
1242 void collectSurroundingLoops();
1244 /// Build the access relation of all memory accesses.
1245 void buildAccessRelations();
1247 /// Detect and mark reductions in the ScopStmt
1248 void checkForReductions();
1250 /// Collect loads which might form a reduction chain with @p StoreMA
1251 void
1252 collectCandiateReductionLoads(MemoryAccess *StoreMA,
1253 llvm::SmallVectorImpl<MemoryAccess *> &Loads);
1254 //@}
1256 /// Remove @p MA from dictionaries pointing to them.
1257 void removeAccessData(MemoryAccess *MA);
1259 public:
1260 ~ScopStmt();
1262 /// Get an isl_ctx pointer.
1263 isl_ctx *getIslCtx() const;
1265 /// Get the iteration domain of this ScopStmt.
1267 /// @return The iteration domain of this ScopStmt.
1268 __isl_give isl_set *getDomain() const;
1270 /// Get the space of the iteration domain
1272 /// @return The space of the iteration domain
1273 __isl_give isl_space *getDomainSpace() const;
1275 /// Get the id of the iteration domain space
1277 /// @return The id of the iteration domain space
1278 __isl_give isl_id *getDomainId() const;
1280 /// Get an isl string representing this domain.
1281 std::string getDomainStr() const;
1283 /// Get the schedule function of this ScopStmt.
1285 /// @return The schedule function of this ScopStmt, if it does not contain
1286 /// extension nodes, and nullptr, otherwise.
1287 __isl_give isl_map *getSchedule() const;
1289 /// Get an isl string representing this schedule.
1291 /// @return An isl string representing this schedule, if it does not contain
1292 /// extension nodes, and an empty string, otherwise.
1293 std::string getScheduleStr() const;
1295 /// Get the invalid domain for this statement.
1296 __isl_give isl_set *getInvalidDomain() const {
1297 return isl_set_copy(InvalidDomain);
1300 /// Get the invalid context for this statement.
1301 __isl_give isl_set *getInvalidContext() const {
1302 return isl_set_params(getInvalidDomain());
1305 /// Set the invalid context for this statement to @p ID.
1306 void setInvalidDomain(__isl_take isl_set *ID);
1308 /// Get the BasicBlock represented by this ScopStmt (if any).
1310 /// @return The BasicBlock represented by this ScopStmt, or null if the
1311 /// statement represents a region.
1312 BasicBlock *getBasicBlock() const { return BB; }
1314 /// Return true if this statement represents a single basic block.
1315 bool isBlockStmt() const { return BB != nullptr; }
1317 /// Return true if this is a copy statement.
1318 bool isCopyStmt() const { return BB == nullptr && R == nullptr; }
1320 /// Get the region represented by this ScopStmt (if any).
1322 /// @return The region represented by this ScopStmt, or null if the statement
1323 /// represents a basic block.
1324 Region *getRegion() const { return R; }
1326 /// Return true if this statement represents a whole region.
1327 bool isRegionStmt() const { return R != nullptr; }
1329 /// Return a BasicBlock from this statement.
1331 /// For block statements, it returns the BasicBlock itself. For subregion
1332 /// statements, return its entry block.
1333 BasicBlock *getEntryBlock() const;
1335 /// Return whether @p L is boxed within this statement.
1336 bool contains(const Loop *L) const {
1337 // Block statements never contain loops.
1338 if (isBlockStmt())
1339 return false;
1341 return getRegion()->contains(L);
1344 /// Return whether this statement contains @p BB.
1345 bool contains(BasicBlock *BB) const {
1346 if (isCopyStmt())
1347 return false;
1348 if (isBlockStmt())
1349 return BB == getBasicBlock();
1350 return getRegion()->contains(BB);
1353 /// Return the closest innermost loop that contains this statement, but is not
1354 /// contained in it.
1356 /// For block statement, this is just the loop that contains the block. Region
1357 /// statements can contain boxed loops, so getting the loop of one of the
1358 /// region's BBs might return such an inner loop. For instance, the region's
1359 /// entry could be a header of a loop, but the region might extend to BBs
1360 /// after the loop exit. Similarly, the region might only contain parts of the
1361 /// loop body and still include the loop header.
1363 /// Most of the time the surrounding loop is the top element of #NestLoops,
1364 /// except when it is empty. In that case it return the loop that the whole
1365 /// SCoP is contained in. That can be nullptr if there is no such loop.
1366 Loop *getSurroundingLoop() const {
1367 assert(!isCopyStmt() &&
1368 "No surrounding loop for artificially created statements");
1369 return SurroundingLoop;
1372 /// Return true if this statement does not contain any accesses.
1373 bool isEmpty() const { return MemAccs.empty(); }
1375 /// Return the only array access for @p Inst, if existing.
1377 /// @param Inst The instruction for which to look up the access.
1378 /// @returns The unique array memory access related to Inst or nullptr if
1379 /// no array access exists
1380 MemoryAccess *getArrayAccessOrNULLFor(const Instruction *Inst) const {
1381 auto It = InstructionToAccess.find(Inst);
1382 if (It == InstructionToAccess.end())
1383 return nullptr;
1385 MemoryAccess *ArrayAccess = nullptr;
1387 for (auto Access : It->getSecond()) {
1388 if (!Access->isArrayKind())
1389 continue;
1391 assert(!ArrayAccess && "More then one array access for instruction");
1393 ArrayAccess = Access;
1396 return ArrayAccess;
1399 /// Return the only array access for @p Inst.
1401 /// @param Inst The instruction for which to look up the access.
1402 /// @returns The unique array memory access related to Inst.
1403 MemoryAccess &getArrayAccessFor(const Instruction *Inst) const {
1404 MemoryAccess *ArrayAccess = getArrayAccessOrNULLFor(Inst);
1406 assert(ArrayAccess && "No array access found for instruction!");
1407 return *ArrayAccess;
1410 /// Return the MemoryAccess that writes the value of an instruction
1411 /// defined in this statement, or nullptr if not existing, respectively
1412 /// not yet added.
1413 MemoryAccess *lookupValueWriteOf(Instruction *Inst) const {
1414 assert((isRegionStmt() && R->contains(Inst)) ||
1415 (!isRegionStmt() && Inst->getParent() == BB));
1416 return ValueWrites.lookup(Inst);
1419 /// Return the MemoryAccess that reloads a value, or nullptr if not
1420 /// existing, respectively not yet added.
1421 MemoryAccess *lookupValueReadOf(Value *Inst) const {
1422 return ValueReads.lookup(Inst);
1425 /// Return the PHI write MemoryAccess for the incoming values from any
1426 /// basic block in this ScopStmt, or nullptr if not existing,
1427 /// respectively not yet added.
1428 MemoryAccess *lookupPHIWriteOf(PHINode *PHI) const {
1429 assert(isBlockStmt() || R->getExit() == PHI->getParent());
1430 return PHIWrites.lookup(PHI);
1433 /// Add @p Access to this statement's list of accesses.
1434 void addAccess(MemoryAccess *Access);
1436 /// Remove a MemoryAccess from this statement.
1438 /// Note that scalar accesses that are caused by MA will
1439 /// be eliminated too.
1440 void removeMemoryAccess(MemoryAccess *MA);
1442 /// Remove @p MA from this statement.
1444 /// In contrast to removeMemoryAccess(), no other access will be eliminated.
1445 void removeSingleMemoryAccess(MemoryAccess *MA);
1447 typedef MemoryAccessVec::iterator iterator;
1448 typedef MemoryAccessVec::const_iterator const_iterator;
1450 iterator begin() { return MemAccs.begin(); }
1451 iterator end() { return MemAccs.end(); }
1452 const_iterator begin() const { return MemAccs.begin(); }
1453 const_iterator end() const { return MemAccs.end(); }
1454 size_t size() const { return MemAccs.size(); }
1456 unsigned getNumIterators() const;
1458 Scop *getParent() { return &Parent; }
1459 const Scop *getParent() const { return &Parent; }
1461 const char *getBaseName() const;
1463 /// Set the isl AST build.
1464 void setAstBuild(__isl_keep isl_ast_build *B) { Build = B; }
1466 /// Get the isl AST build.
1467 __isl_keep isl_ast_build *getAstBuild() const { return Build; }
1469 /// Restrict the domain of the statement.
1471 /// @param NewDomain The new statement domain.
1472 void restrictDomain(__isl_take isl_set *NewDomain);
1474 /// Compute the isl representation for the SCEV @p E in this stmt.
1476 /// @param E The SCEV that should be translated.
1477 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
1479 /// Note that this function will also adjust the invalid context accordingly.
1480 __isl_give isl_pw_aff *getPwAff(const SCEV *E, bool NonNegative = false);
1482 /// Get the loop for a dimension.
1484 /// @param Dimension The dimension of the induction variable
1485 /// @return The loop at a certain dimension.
1486 Loop *getLoopForDimension(unsigned Dimension) const;
1488 /// Align the parameters in the statement to the scop context
1489 void realignParams();
1491 /// Print the ScopStmt.
1493 /// @param OS The output stream the ScopStmt is printed to.
1494 void print(raw_ostream &OS) const;
1496 /// Print the ScopStmt to stderr.
1497 void dump() const;
1500 /// Print ScopStmt S to raw_ostream O.
1501 static inline raw_ostream &operator<<(raw_ostream &O, const ScopStmt &S) {
1502 S.print(O);
1503 return O;
1506 /// Static Control Part
1508 /// A Scop is the polyhedral representation of a control flow region detected
1509 /// by the Scop detection. It is generated by translating the LLVM-IR and
1510 /// abstracting its effects.
1512 /// A Scop consists of a set of:
1514 /// * A set of statements executed in the Scop.
1516 /// * A set of global parameters
1517 /// Those parameters are scalar integer values, which are constant during
1518 /// execution.
1520 /// * A context
1521 /// This context contains information about the values the parameters
1522 /// can take and relations between different parameters.
1523 class Scop {
1524 public:
1525 /// Type to represent a pair of minimal/maximal access to an array.
1526 using MinMaxAccessTy = std::pair<isl_pw_multi_aff *, isl_pw_multi_aff *>;
1528 /// Vector of minimal/maximal accesses to different arrays.
1529 using MinMaxVectorTy = SmallVector<MinMaxAccessTy, 4>;
1531 /// Pair of minimal/maximal access vectors representing
1532 /// read write and read only accesses
1533 using MinMaxVectorPairTy = std::pair<MinMaxVectorTy, MinMaxVectorTy>;
1535 /// Vector of pair of minimal/maximal access vectors representing
1536 /// non read only and read only accesses for each alias group.
1537 using MinMaxVectorPairVectorTy = SmallVector<MinMaxVectorPairTy, 4>;
1539 private:
1540 Scop(const Scop &) = delete;
1541 const Scop &operator=(const Scop &) = delete;
1543 ScalarEvolution *SE;
1545 /// The underlying Region.
1546 Region &R;
1548 // Access functions of the SCoP.
1550 // This owns all the MemoryAccess objects of the Scop created in this pass.
1551 AccFuncVector AccessFunctions;
1553 /// Flag to indicate that the scheduler actually optimized the SCoP.
1554 bool IsOptimized;
1556 /// True if the underlying region has a single exiting block.
1557 bool HasSingleExitEdge;
1559 /// Flag to remember if the SCoP contained an error block or not.
1560 bool HasErrorBlock;
1562 /// Max loop depth.
1563 unsigned MaxLoopDepth;
1565 /// Number of copy statements.
1566 unsigned CopyStmtsNum;
1568 typedef std::list<ScopStmt> StmtSet;
1569 /// The statements in this Scop.
1570 StmtSet Stmts;
1572 /// Parameters of this Scop
1573 ParameterSetTy Parameters;
1575 /// Mapping from parameters to their ids.
1576 DenseMap<const SCEV *, isl_id *> ParameterIds;
1578 /// The context of the SCoP created during SCoP detection.
1579 ScopDetection::DetectionContext &DC;
1581 /// Isl context.
1583 /// We need a shared_ptr with reference counter to delete the context when all
1584 /// isl objects are deleted. We will distribute the shared_ptr to all objects
1585 /// that use the context to create isl objects, and increase the reference
1586 /// counter. By doing this, we guarantee that the context is deleted when we
1587 /// delete the last object that creates isl objects with the context.
1588 std::shared_ptr<isl_ctx> IslCtx;
1590 /// A map from basic blocks to SCoP statements.
1591 DenseMap<BasicBlock *, ScopStmt *> StmtMap;
1593 /// A map from basic blocks to their domains.
1594 DenseMap<BasicBlock *, isl_set *> DomainMap;
1596 /// Constraints on parameters.
1597 isl_set *Context;
1599 /// The affinator used to translate SCEVs to isl expressions.
1600 SCEVAffinator Affinator;
1602 typedef std::map<std::pair<AssertingVH<const Value>, MemoryKind>,
1603 std::unique_ptr<ScopArrayInfo>>
1604 ArrayInfoMapTy;
1606 typedef StringMap<std::unique_ptr<ScopArrayInfo>> ArrayNameMapTy;
1608 typedef SetVector<ScopArrayInfo *> ArrayInfoSetTy;
1610 /// A map to remember ScopArrayInfo objects for all base pointers.
1612 /// As PHI nodes may have two array info objects associated, we add a flag
1613 /// that distinguishes between the PHI node specific ArrayInfo object
1614 /// and the normal one.
1615 ArrayInfoMapTy ScopArrayInfoMap;
1617 /// A map to remember ScopArrayInfo objects for all names of memory
1618 /// references.
1619 ArrayNameMapTy ScopArrayNameMap;
1621 /// A set to remember ScopArrayInfo objects.
1622 /// @see Scop::ScopArrayInfoMap
1623 ArrayInfoSetTy ScopArrayInfoSet;
1625 /// The assumptions under which this scop was built.
1627 /// When constructing a scop sometimes the exact representation of a statement
1628 /// or condition would be very complex, but there is a common case which is a
1629 /// lot simpler, but which is only valid under certain assumptions. The
1630 /// assumed context records the assumptions taken during the construction of
1631 /// this scop and that need to be code generated as a run-time test.
1632 isl_set *AssumedContext;
1634 /// The restrictions under which this SCoP was built.
1636 /// The invalid context is similar to the assumed context as it contains
1637 /// constraints over the parameters. However, while we need the constraints
1638 /// in the assumed context to be "true" the constraints in the invalid context
1639 /// need to be "false". Otherwise they behave the same.
1640 isl_set *InvalidContext;
1642 /// Helper struct to remember assumptions.
1643 struct Assumption {
1645 /// The kind of the assumption (e.g., WRAPPING).
1646 AssumptionKind Kind;
1648 /// Flag to distinguish assumptions and restrictions.
1649 AssumptionSign Sign;
1651 /// The valid/invalid context if this is an assumption/restriction.
1652 isl_set *Set;
1654 /// The location that caused this assumption.
1655 DebugLoc Loc;
1657 /// An optional block whose domain can simplify the assumption.
1658 BasicBlock *BB;
1661 /// Collection to hold taken assumptions.
1663 /// There are two reasons why we want to record assumptions first before we
1664 /// add them to the assumed/invalid context:
1665 /// 1) If the SCoP is not profitable or otherwise invalid without the
1666 /// assumed/invalid context we do not have to compute it.
1667 /// 2) Information about the context are gathered rather late in the SCoP
1668 /// construction (basically after we know all parameters), thus the user
1669 /// might see overly complicated assumptions to be taken while they will
1670 /// only be simplified later on.
1671 SmallVector<Assumption, 8> RecordedAssumptions;
1673 /// The schedule of the SCoP
1675 /// The schedule of the SCoP describes the execution order of the statements
1676 /// in the scop by assigning each statement instance a possibly
1677 /// multi-dimensional execution time. The schedule is stored as a tree of
1678 /// schedule nodes.
1680 /// The most common nodes in a schedule tree are so-called band nodes. Band
1681 /// nodes map statement instances into a multi dimensional schedule space.
1682 /// This space can be seen as a multi-dimensional clock.
1684 /// Example:
1686 /// <S,(5,4)> may be mapped to (5,4) by this schedule:
1688 /// s0 = i (Year of execution)
1689 /// s1 = j (Day of execution)
1691 /// or to (9, 20) by this schedule:
1693 /// s0 = i + j (Year of execution)
1694 /// s1 = 20 (Day of execution)
1696 /// The order statement instances are executed is defined by the
1697 /// schedule vectors they are mapped to. A statement instance
1698 /// <A, (i, j, ..)> is executed before a statement instance <B, (i', ..)>, if
1699 /// the schedule vector of A is lexicographic smaller than the schedule
1700 /// vector of B.
1702 /// Besides band nodes, schedule trees contain additional nodes that specify
1703 /// a textual ordering between two subtrees or filter nodes that filter the
1704 /// set of statement instances that will be scheduled in a subtree. There
1705 /// are also several other nodes. A full description of the different nodes
1706 /// in a schedule tree is given in the isl manual.
1707 isl_schedule *Schedule;
1709 /// The set of minimal/maximal accesses for each alias group.
1711 /// When building runtime alias checks we look at all memory instructions and
1712 /// build so called alias groups. Each group contains a set of accesses to
1713 /// different base arrays which might alias with each other. However, between
1714 /// alias groups there is no aliasing possible.
1716 /// In a program with int and float pointers annotated with tbaa information
1717 /// we would probably generate two alias groups, one for the int pointers and
1718 /// one for the float pointers.
1720 /// During code generation we will create a runtime alias check for each alias
1721 /// group to ensure the SCoP is executed in an alias free environment.
1722 MinMaxVectorPairVectorTy MinMaxAliasGroups;
1724 /// Mapping from invariant loads to the representing invariant load of
1725 /// their equivalence class.
1726 ValueToValueMap InvEquivClassVMap;
1728 /// List of invariant accesses.
1729 InvariantEquivClassesTy InvariantEquivClasses;
1731 /// The smallest array index not yet assigned.
1732 long ArrayIdx = 0;
1734 /// The smallest statement index not yet assigned.
1735 long StmtIdx = 0;
1737 /// Scop constructor; invoked from ScopBuilder::buildScop.
1738 Scop(Region &R, ScalarEvolution &SE, LoopInfo &LI,
1739 ScopDetection::DetectionContext &DC);
1741 //@}
1743 /// Initialize this ScopBuilder.
1744 void init(AliasAnalysis &AA, AssumptionCache &AC, DominatorTree &DT,
1745 LoopInfo &LI);
1747 /// Propagate domains that are known due to graph properties.
1749 /// As a CFG is mostly structured we use the graph properties to propagate
1750 /// domains without the need to compute all path conditions. In particular, if
1751 /// a block A dominates a block B and B post-dominates A we know that the
1752 /// domain of B is a superset of the domain of A. As we do not have
1753 /// post-dominator information available here we use the less precise region
1754 /// information. Given a region R, we know that the exit is always executed if
1755 /// the entry was executed, thus the domain of the exit is a superset of the
1756 /// domain of the entry. In case the exit can only be reached from within the
1757 /// region the domains are in fact equal. This function will use this property
1758 /// to avoid the generation of condition constraints that determine when a
1759 /// branch is taken. If @p BB is a region entry block we will propagate its
1760 /// domain to the region exit block. Additionally, we put the region exit
1761 /// block in the @p FinishedExitBlocks set so we can later skip edges from
1762 /// within the region to that block.
1764 /// @param BB The block for which the domain is currently propagated.
1765 /// @param BBLoop The innermost affine loop surrounding @p BB.
1766 /// @param FinishedExitBlocks Set of region exits the domain was set for.
1767 /// @param LI The LoopInfo for the current function.
1769 void propagateDomainConstraintsToRegionExit(
1770 BasicBlock *BB, Loop *BBLoop,
1771 SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI);
1773 /// Compute the union of predecessor domains for @p BB.
1775 /// To compute the union of all domains of predecessors of @p BB this
1776 /// function applies similar reasoning on the CFG structure as described for
1777 /// @see propagateDomainConstraintsToRegionExit
1779 /// @param BB The block for which the predecessor domains are collected.
1780 /// @param Domain The domain under which BB is executed.
1781 /// @param DT The DominatorTree for the current function.
1782 /// @param LI The LoopInfo for the current function.
1784 /// @returns The domain under which @p BB is executed.
1785 __isl_give isl_set *
1786 getPredecessorDomainConstraints(BasicBlock *BB, __isl_keep isl_set *Domain,
1787 DominatorTree &DT, LoopInfo &LI);
1789 /// Add loop carried constraints to the header block of the loop @p L.
1791 /// @param L The loop to process.
1792 /// @param LI The LoopInfo for the current function.
1794 /// @returns True if there was no problem and false otherwise.
1795 bool addLoopBoundsToHeaderDomain(Loop *L, LoopInfo &LI);
1797 /// Compute the branching constraints for each basic block in @p R.
1799 /// @param R The region we currently build branching conditions for.
1800 /// @param DT The DominatorTree for the current function.
1801 /// @param LI The LoopInfo for the current function.
1803 /// @returns True if there was no problem and false otherwise.
1804 bool buildDomainsWithBranchConstraints(Region *R, DominatorTree &DT,
1805 LoopInfo &LI);
1807 /// Propagate the domain constraints through the region @p R.
1809 /// @param R The region we currently build branching conditions for.
1810 /// @param DT The DominatorTree for the current function.
1811 /// @param LI The LoopInfo for the current function.
1813 /// @returns True if there was no problem and false otherwise.
1814 bool propagateDomainConstraints(Region *R, DominatorTree &DT, LoopInfo &LI);
1816 /// Propagate invalid domains of statements through @p R.
1818 /// This method will propagate invalid statement domains through @p R and at
1819 /// the same time add error block domains to them. Additionally, the domains
1820 /// of error statements and those only reachable via error statements will be
1821 /// replaced by an empty set. Later those will be removed completely.
1823 /// @param R The currently traversed region.
1824 /// @param DT The DominatorTree for the current function.
1825 /// @param LI The LoopInfo for the current function.
1827 /// @returns True if there was no problem and false otherwise.
1828 bool propagateInvalidStmtDomains(Region *R, DominatorTree &DT, LoopInfo &LI);
1830 /// Compute the domain for each basic block in @p R.
1832 /// @param R The region we currently traverse.
1833 /// @param DT The DominatorTree for the current function.
1834 /// @param LI The LoopInfo for the current function.
1836 /// @returns True if there was no problem and false otherwise.
1837 bool buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI);
1839 /// Add parameter constraints to @p C that imply a non-empty domain.
1840 __isl_give isl_set *addNonEmptyDomainConstraints(__isl_take isl_set *C) const;
1842 /// Return the access for the base ptr of @p MA if any.
1843 MemoryAccess *lookupBasePtrAccess(MemoryAccess *MA);
1845 /// Check if the base ptr of @p MA is in the SCoP but not hoistable.
1846 bool hasNonHoistableBasePtrInScop(MemoryAccess *MA,
1847 __isl_keep isl_union_map *Writes);
1849 /// Create equivalence classes for required invariant accesses.
1851 /// These classes will consolidate multiple required invariant loads from the
1852 /// same address in order to keep the number of dimensions in the SCoP
1853 /// description small. For each such class equivalence class only one
1854 /// representing element, hence one required invariant load, will be chosen
1855 /// and modeled as parameter. The method
1856 /// Scop::getRepresentingInvariantLoadSCEV() will replace each element from an
1857 /// equivalence class with the representing element that is modeled. As a
1858 /// consequence Scop::getIdForParam() will only return an id for the
1859 /// representing element of each equivalence class, thus for each required
1860 /// invariant location.
1861 void buildInvariantEquivalenceClasses();
1863 /// Return the context under which the access cannot be hoisted.
1865 /// @param Access The access to check.
1866 /// @param Writes The set of all memory writes in the scop.
1868 /// @return Return the context under which the access cannot be hoisted or a
1869 /// nullptr if it cannot be hoisted at all.
1870 __isl_give isl_set *getNonHoistableCtx(MemoryAccess *Access,
1871 __isl_keep isl_union_map *Writes);
1873 /// Verify that all required invariant loads have been hoisted.
1875 /// Invariant load hoisting is not guaranteed to hoist all loads that were
1876 /// assumed to be scop invariant during scop detection. This function checks
1877 /// for cases where the hoisting failed, but where it would have been
1878 /// necessary for our scop modeling to be correct. In case of insufficent
1879 /// hoisting the scop is marked as invalid.
1881 /// In the example below Bound[1] is required to be invariant:
1883 /// for (int i = 1; i < Bound[0]; i++)
1884 /// for (int j = 1; j < Bound[1]; j++)
1885 /// ...
1887 void verifyInvariantLoads();
1889 /// Hoist invariant memory loads and check for required ones.
1891 /// We first identify "common" invariant loads, thus loads that are invariant
1892 /// and can be hoisted. Then we check if all required invariant loads have
1893 /// been identified as (common) invariant. A load is a required invariant load
1894 /// if it was assumed to be invariant during SCoP detection, e.g., to assume
1895 /// loop bounds to be affine or runtime alias checks to be placeable. In case
1896 /// a required invariant load was not identified as (common) invariant we will
1897 /// drop this SCoP. An example for both "common" as well as required invariant
1898 /// loads is given below:
1900 /// for (int i = 1; i < *LB[0]; i++)
1901 /// for (int j = 1; j < *LB[1]; j++)
1902 /// A[i][j] += A[0][0] + (*V);
1904 /// Common inv. loads: V, A[0][0], LB[0], LB[1]
1905 /// Required inv. loads: LB[0], LB[1], (V, if it may alias with A or LB)
1907 void hoistInvariantLoads();
1909 /// Canonicalize arrays with base pointers from the same equivalence class.
1911 /// Some context: in our normal model we assume that each base pointer is
1912 /// related to a single specific memory region, where memory regions
1913 /// associated with different base pointers are disjoint. Consequently we do
1914 /// not need to compute additional data dependences that model possible
1915 /// overlaps of these memory regions. To verify our assumption we compute
1916 /// alias checks that verify that modeled arrays indeed do not overlap. In
1917 /// case an overlap is detected the runtime check fails and we fall back to
1918 /// the original code.
1920 /// In case of arrays where the base pointers are know to be identical,
1921 /// because they are dynamically loaded by accesses that are in the same
1922 /// invariant load equivalence class, such run-time alias check would always
1923 /// be false.
1925 /// This function makes sure that we do not generate consistently failing
1926 /// run-time checks for code that contains distinct arrays with known
1927 /// equivalent base pointers. It identifies for each invariant load
1928 /// equivalence class a single canonical array and canonicalizes all memory
1929 /// accesses that reference arrays that have base pointers that are known to
1930 /// be equal to the base pointer of such a canonical array to this canonical
1931 /// array.
1933 /// We currently do not canonicalize arrays for which certain memory accesses
1934 /// have been hoisted as loop invariant.
1935 void canonicalizeDynamicBasePtrs();
1937 /// Add invariant loads listed in @p InvMAs with the domain of @p Stmt.
1938 void addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs);
1940 /// Create an id for @p Param and store it in the ParameterIds map.
1941 void createParameterId(const SCEV *Param);
1943 /// Build the Context of the Scop.
1944 void buildContext();
1946 /// Add user provided parameter constraints to context (source code).
1947 void addUserAssumptions(AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI);
1949 /// Add user provided parameter constraints to context (command line).
1950 void addUserContext();
1952 /// Add the bounds of the parameters to the context.
1953 void addParameterBounds();
1955 /// Simplify the assumed and invalid context.
1956 void simplifyContexts();
1958 /// Get the representing SCEV for @p S if applicable, otherwise @p S.
1960 /// Invariant loads of the same location are put in an equivalence class and
1961 /// only one of them is chosen as a representing element that will be
1962 /// modeled as a parameter. The others have to be normalized, i.e.,
1963 /// replaced by the representing element of their equivalence class, in order
1964 /// to get the correct parameter value, e.g., in the SCEVAffinator.
1966 /// @param S The SCEV to normalize.
1968 /// @return The representing SCEV for invariant loads or @p S if none.
1969 const SCEV *getRepresentingInvariantLoadSCEV(const SCEV *S);
1971 /// Create a new SCoP statement for @p BB.
1973 /// A new statement for @p BB will be created and added to the statement
1974 /// vector
1975 /// and map.
1977 /// @param BB The basic block we build the statement for.
1978 /// @param SurroundingLoop The loop the created statement is contained in.
1979 void addScopStmt(BasicBlock *BB, Loop *SurroundingLoop);
1981 /// Create a new SCoP statement for @p R.
1983 /// A new statement for @p R will be created and added to the statement vector
1984 /// and map.
1986 /// @param R The region we build the statement for.
1987 /// @param SurroundingLoop The loop the created statement is contained in.
1988 void addScopStmt(Region *R, Loop *SurroundingLoop);
1990 /// Update access dimensionalities.
1992 /// When detecting memory accesses different accesses to the same array may
1993 /// have built with different dimensionality, as outer zero-values dimensions
1994 /// may not have been recognized as separate dimensions. This function goes
1995 /// again over all memory accesses and updates their dimensionality to match
1996 /// the dimensionality of the underlying ScopArrayInfo object.
1997 void updateAccessDimensionality();
1999 /// Fold size constants to the right.
2001 /// In case all memory accesses in a given dimension are multiplied with a
2002 /// common constant, we can remove this constant from the individual access
2003 /// functions and move it to the size of the memory access. We do this as this
2004 /// increases the size of the innermost dimension, consequently widens the
2005 /// valid range the array subscript in this dimension can evaluate to, and
2006 /// as a result increases the likelyhood that our delinearization is
2007 /// correct.
2009 /// Example:
2011 /// A[][n]
2012 /// S[i,j] -> A[2i][2j+1]
2013 /// S[i,j] -> A[2i][2j]
2015 /// =>
2017 /// A[][2n]
2018 /// S[i,j] -> A[i][2j+1]
2019 /// S[i,j] -> A[i][2j]
2021 /// Constants in outer dimensions can arise when the elements of a parametric
2022 /// multi-dimensional array are not elementar data types, but e.g.,
2023 /// structures.
2024 void foldSizeConstantsToRight();
2026 /// Fold memory accesses to handle parametric offset.
2028 /// As a post-processing step, we 'fold' memory accesses to parameteric
2029 /// offsets in the access functions. @see MemoryAccess::foldAccess for
2030 /// details.
2031 void foldAccessRelations();
2033 /// Assume that all memory accesses are within bounds.
2035 /// After we have built a model of all memory accesses, we need to assume
2036 /// that the model we built matches reality -- aka. all modeled memory
2037 /// accesses always remain within bounds. We do this as last step, after
2038 /// all memory accesses have been modeled and canonicalized.
2039 void assumeNoOutOfBounds();
2041 /// Finalize all access relations.
2043 /// When building up access relations, temporary access relations that
2044 /// correctly represent each individual access are constructed. However, these
2045 /// access relations can be inconsistent or non-optimal when looking at the
2046 /// set of accesses as a whole. This function finalizes the memory accesses
2047 /// and constructs a globally consistent state.
2048 void finalizeAccesses();
2050 /// Construct the schedule of this SCoP.
2052 /// @param LI The LoopInfo for the current function.
2053 void buildSchedule(LoopInfo &LI);
2055 /// A loop stack element to keep track of per-loop information during
2056 /// schedule construction.
2057 typedef struct LoopStackElement {
2058 // The loop for which we keep information.
2059 Loop *L;
2061 // The (possibly incomplete) schedule for this loop.
2062 isl_schedule *Schedule;
2064 // The number of basic blocks in the current loop, for which a schedule has
2065 // already been constructed.
2066 unsigned NumBlocksProcessed;
2068 LoopStackElement(Loop *L, __isl_give isl_schedule *S,
2069 unsigned NumBlocksProcessed)
2070 : L(L), Schedule(S), NumBlocksProcessed(NumBlocksProcessed) {}
2071 } LoopStackElementTy;
2073 /// The loop stack used for schedule construction.
2075 /// The loop stack keeps track of schedule information for a set of nested
2076 /// loops as well as an (optional) 'nullptr' loop that models the outermost
2077 /// schedule dimension. The loops in a loop stack always have a parent-child
2078 /// relation where the loop at position n is the parent of the loop at
2079 /// position n + 1.
2080 typedef SmallVector<LoopStackElementTy, 4> LoopStackTy;
2082 /// Construct schedule information for a given Region and add the
2083 /// derived information to @p LoopStack.
2085 /// Given a Region we derive schedule information for all RegionNodes
2086 /// contained in this region ensuring that the assigned execution times
2087 /// correctly model the existing control flow relations.
2089 /// @param R The region which to process.
2090 /// @param LoopStack A stack of loops that are currently under
2091 /// construction.
2092 /// @param LI The LoopInfo for the current function.
2093 void buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI);
2095 /// Build Schedule for the region node @p RN and add the derived
2096 /// information to @p LoopStack.
2098 /// In case @p RN is a BasicBlock or a non-affine Region, we construct the
2099 /// schedule for this @p RN and also finalize loop schedules in case the
2100 /// current @p RN completes the loop.
2102 /// In case @p RN is a not-non-affine Region, we delegate the construction to
2103 /// buildSchedule(Region *R, ...).
2105 /// @param RN The RegionNode region traversed.
2106 /// @param LoopStack A stack of loops that are currently under
2107 /// construction.
2108 /// @param LI The LoopInfo for the current function.
2109 void buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI);
2111 /// Collect all memory access relations of a given type.
2113 /// @param Predicate A predicate function that returns true if an access is
2114 /// of a given type.
2116 /// @returns The set of memory accesses in the scop that match the predicate.
2117 __isl_give isl_union_map *
2118 getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate);
2120 /// @name Helper functions for printing the Scop.
2122 //@{
2123 void printContext(raw_ostream &OS) const;
2124 void printArrayInfo(raw_ostream &OS) const;
2125 void printStatements(raw_ostream &OS) const;
2126 void printAliasAssumptions(raw_ostream &OS) const;
2127 //@}
2129 friend class ScopBuilder;
2131 public:
2132 ~Scop();
2134 /// Get the count of copy statements added to this Scop.
2136 /// @return The count of copy statements added to this Scop.
2137 unsigned getCopyStmtsNum() { return CopyStmtsNum; }
2139 /// Create a new copy statement.
2141 /// A new statement will be created and added to the statement vector.
2143 /// @param Stmt The parent statement.
2144 /// @param SourceRel The source location.
2145 /// @param TargetRel The target location.
2146 /// @param Domain The original domain under which copy statement whould
2147 /// be executed.
2148 ScopStmt *addScopStmt(__isl_take isl_map *SourceRel,
2149 __isl_take isl_map *TargetRel,
2150 __isl_take isl_set *Domain);
2152 /// Add the access function to all MemoryAccess objects of the Scop
2153 /// created in this pass.
2154 void addAccessFunction(MemoryAccess *Access) {
2155 AccessFunctions.emplace_back(Access);
2158 ScalarEvolution *getSE() const;
2160 /// Get the count of parameters used in this Scop.
2162 /// @return The count of parameters used in this Scop.
2163 size_t getNumParams() const { return Parameters.size(); }
2165 /// Take a list of parameters and add the new ones to the scop.
2166 void addParams(const ParameterSetTy &NewParameters);
2168 /// Return an iterator range containing the scop parameters.
2169 iterator_range<ParameterSetTy::iterator> parameters() const {
2170 return make_range(Parameters.begin(), Parameters.end());
2173 /// Return whether this scop is empty, i.e. contains no statements that
2174 /// could be executed.
2175 bool isEmpty() const { return Stmts.empty(); }
2177 typedef ArrayInfoSetTy::iterator array_iterator;
2178 typedef ArrayInfoSetTy::const_iterator const_array_iterator;
2179 typedef iterator_range<ArrayInfoSetTy::iterator> array_range;
2180 typedef iterator_range<ArrayInfoSetTy::const_iterator> const_array_range;
2182 inline array_iterator array_begin() { return ScopArrayInfoSet.begin(); }
2184 inline array_iterator array_end() { return ScopArrayInfoSet.end(); }
2186 inline const_array_iterator array_begin() const {
2187 return ScopArrayInfoSet.begin();
2190 inline const_array_iterator array_end() const {
2191 return ScopArrayInfoSet.end();
2194 inline array_range arrays() {
2195 return array_range(array_begin(), array_end());
2198 inline const_array_range arrays() const {
2199 return const_array_range(array_begin(), array_end());
2202 /// Return the isl_id that represents a certain parameter.
2204 /// @param Parameter A SCEV that was recognized as a Parameter.
2206 /// @return The corresponding isl_id or NULL otherwise.
2207 __isl_give isl_id *getIdForParam(const SCEV *Parameter);
2209 /// Get the maximum region of this static control part.
2211 /// @return The maximum region of this static control part.
2212 inline const Region &getRegion() const { return R; }
2213 inline Region &getRegion() { return R; }
2215 /// Return the function this SCoP is in.
2216 Function &getFunction() const { return *R.getEntry()->getParent(); }
2218 /// Check if @p L is contained in the SCoP.
2219 bool contains(const Loop *L) const { return R.contains(L); }
2221 /// Check if @p BB is contained in the SCoP.
2222 bool contains(const BasicBlock *BB) const { return R.contains(BB); }
2224 /// Check if @p I is contained in the SCoP.
2225 bool contains(const Instruction *I) const { return R.contains(I); }
2227 /// Return the unique exit block of the SCoP.
2228 BasicBlock *getExit() const { return R.getExit(); }
2230 /// Return the unique exiting block of the SCoP if any.
2231 BasicBlock *getExitingBlock() const { return R.getExitingBlock(); }
2233 /// Return the unique entry block of the SCoP.
2234 BasicBlock *getEntry() const { return R.getEntry(); }
2236 /// Return the unique entering block of the SCoP if any.
2237 BasicBlock *getEnteringBlock() const { return R.getEnteringBlock(); }
2239 /// Return true if @p BB is the exit block of the SCoP.
2240 bool isExit(BasicBlock *BB) const { return getExit() == BB; }
2242 /// Return a range of all basic blocks in the SCoP.
2243 Region::block_range blocks() const { return R.blocks(); }
2245 /// Return true if and only if @p BB dominates the SCoP.
2246 bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const;
2248 /// Get the maximum depth of the loop.
2250 /// @return The maximum depth of the loop.
2251 inline unsigned getMaxLoopDepth() const { return MaxLoopDepth; }
2253 /// Return the invariant equivalence class for @p Val if any.
2254 InvariantEquivClassTy *lookupInvariantEquivClass(Value *Val);
2256 /// Return the set of invariant accesses.
2257 InvariantEquivClassesTy &getInvariantAccesses() {
2258 return InvariantEquivClasses;
2261 /// Check if the scop has any invariant access.
2262 bool hasInvariantAccesses() { return !InvariantEquivClasses.empty(); }
2264 /// Mark the SCoP as optimized by the scheduler.
2265 void markAsOptimized() { IsOptimized = true; }
2267 /// Check if the SCoP has been optimized by the scheduler.
2268 bool isOptimized() const { return IsOptimized; }
2270 /// Get the name of this Scop.
2271 std::string getNameStr() const;
2273 /// Get the constraint on parameter of this Scop.
2275 /// @return The constraint on parameter of this Scop.
2276 __isl_give isl_set *getContext() const;
2277 __isl_give isl_space *getParamSpace() const;
2279 /// Get the assumed context for this Scop.
2281 /// @return The assumed context of this Scop.
2282 __isl_give isl_set *getAssumedContext() const;
2284 /// Return true if the optimized SCoP can be executed.
2286 /// In addition to the runtime check context this will also utilize the domain
2287 /// constraints to decide it the optimized version can actually be executed.
2289 /// @returns True if the optimized SCoP can be executed.
2290 bool hasFeasibleRuntimeContext() const;
2292 /// Check if the assumption in @p Set is trivial or not.
2294 /// @param Set The relations between parameters that are assumed to hold.
2295 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2296 /// (needed/assumptions) or negative (invalid/restrictions).
2298 /// @returns True if the assumption @p Set is not trivial.
2299 bool isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign);
2301 /// Track and report an assumption.
2303 /// Use 'clang -Rpass-analysis=polly-scops' or 'opt
2304 /// -pass-remarks-analysis=polly-scops' to output the assumptions.
2306 /// @param Kind The assumption kind describing the underlying cause.
2307 /// @param Set The relations between parameters that are assumed to hold.
2308 /// @param Loc The location in the source that caused this assumption.
2309 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2310 /// (needed/assumptions) or negative (invalid/restrictions).
2312 /// @returns True if the assumption is not trivial.
2313 bool trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set,
2314 DebugLoc Loc, AssumptionSign Sign);
2316 /// Add assumptions to assumed context.
2318 /// The assumptions added will be assumed to hold during the execution of the
2319 /// scop. However, as they are generally not statically provable, at code
2320 /// generation time run-time checks will be generated that ensure the
2321 /// assumptions hold.
2323 /// WARNING: We currently exploit in simplifyAssumedContext the knowledge
2324 /// that assumptions do not change the set of statement instances
2325 /// executed.
2327 /// @param Kind The assumption kind describing the underlying cause.
2328 /// @param Set The relations between parameters that are assumed to hold.
2329 /// @param Loc The location in the source that caused this assumption.
2330 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2331 /// (needed/assumptions) or negative (invalid/restrictions).
2332 void addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, DebugLoc Loc,
2333 AssumptionSign Sign);
2335 /// Record an assumption for later addition to the assumed context.
2337 /// This function will add the assumption to the RecordedAssumptions. This
2338 /// collection will be added (@see addAssumption) to the assumed context once
2339 /// all paramaters are known and the context is fully build.
2341 /// @param Kind The assumption kind describing the underlying cause.
2342 /// @param Set The relations between parameters that are assumed to hold.
2343 /// @param Loc The location in the source that caused this assumption.
2344 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2345 /// (needed/assumptions) or negative (invalid/restrictions).
2346 /// @param BB The block in which this assumption was taken. If it is
2347 /// set, the domain of that block will be used to simplify the
2348 /// actual assumption in @p Set once it is added. This is useful
2349 /// if the assumption was created prior to the domain.
2350 void recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set,
2351 DebugLoc Loc, AssumptionSign Sign,
2352 BasicBlock *BB = nullptr);
2354 /// Add all recorded assumptions to the assumed context.
2355 void addRecordedAssumptions();
2357 /// Mark the scop as invalid.
2359 /// This method adds an assumption to the scop that is always invalid. As a
2360 /// result, the scop will not be optimized later on. This function is commonly
2361 /// called when a condition makes it impossible (or too compile time
2362 /// expensive) to process this scop any further.
2364 /// @param Kind The assumption kind describing the underlying cause.
2365 /// @param Loc The location in the source that triggered .
2366 void invalidate(AssumptionKind Kind, DebugLoc Loc);
2368 /// Get the invalid context for this Scop.
2370 /// @return The invalid context of this Scop.
2371 __isl_give isl_set *getInvalidContext() const;
2373 /// Return true if and only if the InvalidContext is trivial (=empty).
2374 bool hasTrivialInvalidContext() const {
2375 return isl_set_is_empty(InvalidContext);
2378 /// A vector of memory accesses that belong to an alias group.
2379 typedef SmallVector<MemoryAccess *, 4> AliasGroupTy;
2381 /// A vector of alias groups.
2382 typedef SmallVector<Scop::AliasGroupTy, 4> AliasGroupVectorTy;
2384 /// Build the alias checks for this SCoP.
2385 bool buildAliasChecks(AliasAnalysis &AA);
2387 /// Build all alias groups for this SCoP.
2389 /// @returns True if __no__ error occurred, false otherwise.
2390 bool buildAliasGroups(AliasAnalysis &AA);
2392 /// Build alias groups for all memory accesses in the Scop.
2394 /// Using the alias analysis and an alias set tracker we build alias sets
2395 /// for all memory accesses inside the Scop. For each alias set we then map
2396 /// the aliasing pointers back to the memory accesses we know, thus obtain
2397 /// groups of memory accesses which might alias. We also collect the set of
2398 /// arrays through which memory is written.
2400 /// @param AA A reference to the alias analysis.
2402 /// @returns A pair consistent of a vector of alias groups and a set of arrays
2403 /// through which memory is written.
2404 std::tuple<AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>>
2405 buildAliasGroupsForAccesses(AliasAnalysis &AA);
2407 /// Split alias groups by iteration domains.
2409 /// We split each group based on the domains of the minimal/maximal accesses.
2410 /// That means two minimal/maximal accesses are only in a group if their
2411 /// access domains intersect. Otherwise, they are in different groups.
2413 /// @param AliasGroups The alias groups to split
2414 void splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups);
2416 /// Build a given alias group and its access data.
2418 /// @param AliasGroup The alias group to build.
2419 /// @param HasWriteAccess A set of arrays through which memory is not only
2420 /// read, but also written.
2422 /// @returns True if __no__ error occurred, false otherwise.
2423 bool buildAliasGroup(Scop::AliasGroupTy &AliasGroup,
2424 DenseSet<const ScopArrayInfo *> HasWriteAccess);
2426 /// Return all alias groups for this SCoP.
2427 const MinMaxVectorPairVectorTy &getAliasGroups() const {
2428 return MinMaxAliasGroups;
2431 /// Get an isl string representing the context.
2432 std::string getContextStr() const;
2434 /// Get an isl string representing the assumed context.
2435 std::string getAssumedContextStr() const;
2437 /// Get an isl string representing the invalid context.
2438 std::string getInvalidContextStr() const;
2440 /// Return the ScopStmt for the given @p BB or nullptr if there is
2441 /// none.
2442 ScopStmt *getStmtFor(BasicBlock *BB) const;
2444 /// Return the ScopStmt that represents the Region @p R, or nullptr if
2445 /// it is not represented by any statement in this Scop.
2446 ScopStmt *getStmtFor(Region *R) const;
2448 /// Return the ScopStmt that represents @p RN; can return nullptr if
2449 /// the RegionNode is not within the SCoP or has been removed due to
2450 /// simplifications.
2451 ScopStmt *getStmtFor(RegionNode *RN) const;
2453 /// Return the ScopStmt an instruction belongs to, or nullptr if it
2454 /// does not belong to any statement in this Scop.
2455 ScopStmt *getStmtFor(Instruction *Inst) const {
2456 return getStmtFor(Inst->getParent());
2459 /// Return the number of statements in the SCoP.
2460 size_t getSize() const { return Stmts.size(); }
2462 /// @name Statements Iterators
2464 /// These iterators iterate over all statements of this Scop.
2465 //@{
2466 typedef StmtSet::iterator iterator;
2467 typedef StmtSet::const_iterator const_iterator;
2469 iterator begin() { return Stmts.begin(); }
2470 iterator end() { return Stmts.end(); }
2471 const_iterator begin() const { return Stmts.begin(); }
2472 const_iterator end() const { return Stmts.end(); }
2474 typedef StmtSet::reverse_iterator reverse_iterator;
2475 typedef StmtSet::const_reverse_iterator const_reverse_iterator;
2477 reverse_iterator rbegin() { return Stmts.rbegin(); }
2478 reverse_iterator rend() { return Stmts.rend(); }
2479 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
2480 const_reverse_iterator rend() const { return Stmts.rend(); }
2481 //@}
2483 /// Return the set of required invariant loads.
2484 const InvariantLoadsSetTy &getRequiredInvariantLoads() const {
2485 return DC.RequiredILS;
2488 /// Add @p LI to the set of required invariant loads.
2489 void addRequiredInvariantLoad(LoadInst *LI) { DC.RequiredILS.insert(LI); }
2491 /// Return true if and only if @p LI is a required invariant load.
2492 bool isRequiredInvariantLoad(LoadInst *LI) const {
2493 return getRequiredInvariantLoads().count(LI);
2496 /// Return the set of boxed (thus overapproximated) loops.
2497 const BoxedLoopsSetTy &getBoxedLoops() const { return DC.BoxedLoopsSet; }
2499 /// Return true if and only if @p R is a non-affine subregion.
2500 bool isNonAffineSubRegion(const Region *R) {
2501 return DC.NonAffineSubRegionSet.count(R);
2504 const MapInsnToMemAcc &getInsnToMemAccMap() const { return DC.InsnToMemAcc; }
2506 /// Return the (possibly new) ScopArrayInfo object for @p Access.
2508 /// @param ElementType The type of the elements stored in this array.
2509 /// @param Kind The kind of the array info object.
2510 /// @param BaseName The optional name of this memory reference.
2511 const ScopArrayInfo *getOrCreateScopArrayInfo(Value *BasePtr,
2512 Type *ElementType,
2513 ArrayRef<const SCEV *> Sizes,
2514 MemoryKind Kind,
2515 const char *BaseName = nullptr);
2517 /// Create an array and return the corresponding ScopArrayInfo object.
2519 /// @param ElementType The type of the elements stored in this array.
2520 /// @param BaseName The name of this memory reference.
2521 /// @param Sizes The sizes of dimensions.
2522 const ScopArrayInfo *createScopArrayInfo(Type *ElementType,
2523 const std::string &BaseName,
2524 const std::vector<unsigned> &Sizes);
2526 /// Return the cached ScopArrayInfo object for @p BasePtr.
2528 /// @param BasePtr The base pointer the object has been stored for.
2529 /// @param Kind The kind of array info object.
2531 /// @returns The ScopArrayInfo pointer or NULL if no such pointer is
2532 /// available.
2533 const ScopArrayInfo *getScopArrayInfoOrNull(Value *BasePtr, MemoryKind Kind);
2535 /// Return the cached ScopArrayInfo object for @p BasePtr.
2537 /// @param BasePtr The base pointer the object has been stored for.
2538 /// @param Kind The kind of array info object.
2540 /// @returns The ScopArrayInfo pointer (may assert if no such pointer is
2541 /// available).
2542 const ScopArrayInfo *getScopArrayInfo(Value *BasePtr, MemoryKind Kind);
2544 /// Invalidate ScopArrayInfo object for base address.
2546 /// @param BasePtr The base pointer of the ScopArrayInfo object to invalidate.
2547 /// @param Kind The Kind of the ScopArrayInfo object.
2548 void invalidateScopArrayInfo(Value *BasePtr, MemoryKind Kind) {
2549 auto It = ScopArrayInfoMap.find(std::make_pair(BasePtr, Kind));
2550 if (It == ScopArrayInfoMap.end())
2551 return;
2552 ScopArrayInfoSet.remove(It->second.get());
2553 ScopArrayInfoMap.erase(It);
2556 void setContext(__isl_take isl_set *NewContext);
2558 /// Align the parameters in the statement to the scop context
2559 void realignParams();
2561 /// Return true if this SCoP can be profitably optimized.
2563 /// @param ScalarsAreUnprofitable Never consider statements with scalar writes
2564 /// as profitably optimizable.
2566 /// @return Whether this SCoP can be profitably optimized.
2567 bool isProfitable(bool ScalarsAreUnprofitable) const;
2569 /// Return true if the SCoP contained at least one error block.
2570 bool hasErrorBlock() const { return HasErrorBlock; }
2572 /// Return true if the underlying region has a single exiting block.
2573 bool hasSingleExitEdge() const { return HasSingleExitEdge; }
2575 /// Print the static control part.
2577 /// @param OS The output stream the static control part is printed to.
2578 void print(raw_ostream &OS) const;
2580 /// Print the ScopStmt to stderr.
2581 void dump() const;
2583 /// Get the isl context of this static control part.
2585 /// @return The isl context of this static control part.
2586 isl_ctx *getIslCtx() const;
2588 /// Directly return the shared_ptr of the context.
2589 const std::shared_ptr<isl_ctx> &getSharedIslCtx() const { return IslCtx; }
2591 /// Compute the isl representation for the SCEV @p E
2593 /// @param E The SCEV that should be translated.
2594 /// @param BB An (optional) basic block in which the isl_pw_aff is computed.
2595 /// SCEVs known to not reference any loops in the SCoP can be
2596 /// passed without a @p BB.
2597 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
2599 /// Note that this function will always return a valid isl_pw_aff. However, if
2600 /// the translation of @p E was deemed to complex the SCoP is invalidated and
2601 /// a dummy value of appropriate dimension is returned. This allows to bail
2602 /// for complex cases without "error handling code" needed on the users side.
2603 __isl_give PWACtx getPwAff(const SCEV *E, BasicBlock *BB = nullptr,
2604 bool NonNegative = false);
2606 /// Compute the isl representation for the SCEV @p E
2608 /// This function is like @see Scop::getPwAff() but strips away the invalid
2609 /// domain part associated with the piecewise affine function.
2610 __isl_give isl_pw_aff *getPwAffOnly(const SCEV *E, BasicBlock *BB = nullptr);
2612 /// Return the domain of @p Stmt.
2614 /// @param Stmt The statement for which the conditions should be returned.
2615 __isl_give isl_set *getDomainConditions(const ScopStmt *Stmt) const;
2617 /// Return the domain of @p BB.
2619 /// @param BB The block for which the conditions should be returned.
2620 __isl_give isl_set *getDomainConditions(BasicBlock *BB) const;
2622 /// Get a union set containing the iteration domains of all statements.
2623 __isl_give isl_union_set *getDomains() const;
2625 /// Get a union map of all may-writes performed in the SCoP.
2626 __isl_give isl_union_map *getMayWrites();
2628 /// Get a union map of all must-writes performed in the SCoP.
2629 __isl_give isl_union_map *getMustWrites();
2631 /// Get a union map of all writes performed in the SCoP.
2632 __isl_give isl_union_map *getWrites();
2634 /// Get a union map of all reads performed in the SCoP.
2635 __isl_give isl_union_map *getReads();
2637 /// Get a union map of all memory accesses performed in the SCoP.
2638 __isl_give isl_union_map *getAccesses();
2640 /// Get the schedule of all the statements in the SCoP.
2642 /// @return The schedule of all the statements in the SCoP, if the schedule of
2643 /// the Scop does not contain extension nodes, and nullptr, otherwise.
2644 __isl_give isl_union_map *getSchedule() const;
2646 /// Get a schedule tree describing the schedule of all statements.
2647 __isl_give isl_schedule *getScheduleTree() const;
2649 /// Update the current schedule
2651 /// NewSchedule The new schedule (given as a flat union-map).
2652 void setSchedule(__isl_take isl_union_map *NewSchedule);
2654 /// Update the current schedule
2656 /// NewSchedule The new schedule (given as schedule tree).
2657 void setScheduleTree(__isl_take isl_schedule *NewSchedule);
2659 /// Intersects the domains of all statements in the SCoP.
2661 /// @return true if a change was made
2662 bool restrictDomains(__isl_take isl_union_set *Domain);
2664 /// Get the depth of a loop relative to the outermost loop in the Scop.
2666 /// This will return
2667 /// 0 if @p L is an outermost loop in the SCoP
2668 /// >0 for other loops in the SCoP
2669 /// -1 if @p L is nullptr or there is no outermost loop in the SCoP
2670 int getRelativeLoopDepth(const Loop *L) const;
2672 /// Find the ScopArrayInfo associated with an isl Id
2673 /// that has name @p Name.
2674 ScopArrayInfo *getArrayInfoByName(const std::string BaseName);
2676 /// Check whether @p Schedule contains extension nodes.
2678 /// @return true if @p Schedule contains extension nodes.
2679 static bool containsExtensionNode(__isl_keep isl_schedule *Schedule);
2681 /// Simplify the SCoP representation.
2683 /// @param AfterHoisting Whether it is called after invariant load hoisting.
2684 /// When true, also removes statements without
2685 /// side-effects.
2686 void simplifySCoP(bool AfterHoisting);
2688 /// Get the next free array index.
2690 /// This function returns a unique index which can be used to identify an
2691 /// array.
2692 long getNextArrayIdx() { return ArrayIdx++; }
2694 /// Get the next free statement index.
2696 /// This function returns a unique index which can be used to identify a
2697 /// statement.
2698 long getNextStmtIdx() { return StmtIdx++; }
2701 /// Print Scop scop to raw_ostream O.
2702 static inline raw_ostream &operator<<(raw_ostream &O, const Scop &scop) {
2703 scop.print(O);
2704 return O;
2707 /// The legacy pass manager's analysis pass to compute scop information
2708 /// for a region.
2709 class ScopInfoRegionPass : public RegionPass {
2710 /// The Scop pointer which is used to construct a Scop.
2711 std::unique_ptr<Scop> S;
2713 public:
2714 static char ID; // Pass identification, replacement for typeid
2716 ScopInfoRegionPass() : RegionPass(ID) {}
2717 ~ScopInfoRegionPass() {}
2719 /// Build Scop object, the Polly IR of static control
2720 /// part for the current SESE-Region.
2722 /// @return If the current region is a valid for a static control part,
2723 /// return the Polly IR representing this static control part,
2724 /// return null otherwise.
2725 Scop *getScop() { return S.get(); }
2726 const Scop *getScop() const { return S.get(); }
2728 /// Calculate the polyhedral scop information for a given Region.
2729 bool runOnRegion(Region *R, RGPassManager &RGM) override;
2731 void releaseMemory() override { S.reset(); }
2733 void print(raw_ostream &O, const Module *M = nullptr) const override;
2735 void getAnalysisUsage(AnalysisUsage &AU) const override;
2738 //===----------------------------------------------------------------------===//
2739 /// The legacy pass manager's analysis pass to compute scop information
2740 /// for the whole function.
2742 /// This pass will maintain a map of the maximal region within a scop to its
2743 /// scop object for all the feasible scops present in a function.
2744 /// This pass is an alternative to the ScopInfoRegionPass in order to avoid a
2745 /// region pass manager.
2746 class ScopInfoWrapperPass : public FunctionPass {
2748 public:
2749 using RegionToScopMapTy = DenseMap<Region *, std::unique_ptr<Scop>>;
2750 using iterator = RegionToScopMapTy::iterator;
2751 using const_iterator = RegionToScopMapTy::const_iterator;
2753 private:
2754 /// A map of Region to its Scop object containing
2755 /// Polly IR of static control part
2756 RegionToScopMapTy RegionToScopMap;
2758 public:
2759 static char ID; // Pass identification, replacement for typeid
2761 ScopInfoWrapperPass() : FunctionPass(ID) {}
2762 ~ScopInfoWrapperPass() {}
2764 /// Get the Scop object for the given Region
2766 /// @return If the given region is the maximal region within a scop, return
2767 /// the scop object. If the given region is a subregion, return a
2768 /// nullptr. Top level region containing the entry block of a function
2769 /// is not considered in the scop creation.
2770 Scop *getScop(Region *R) const {
2771 auto MapIt = RegionToScopMap.find(R);
2772 if (MapIt != RegionToScopMap.end())
2773 return MapIt->second.get();
2774 return nullptr;
2777 iterator begin() { return RegionToScopMap.begin(); }
2778 iterator end() { return RegionToScopMap.end(); }
2779 const_iterator begin() const { return RegionToScopMap.begin(); }
2780 const_iterator end() const { return RegionToScopMap.end(); }
2782 /// Calculate all the polyhedral scops for a given function.
2783 bool runOnFunction(Function &F) override;
2785 void releaseMemory() override { RegionToScopMap.clear(); }
2787 void print(raw_ostream &O, const Module *M = nullptr) const override;
2789 void getAnalysisUsage(AnalysisUsage &AU) const override;
2792 } // end namespace polly
2794 namespace llvm {
2795 class PassRegistry;
2796 void initializeScopInfoRegionPassPass(llvm::PassRegistry &);
2797 void initializeScopInfoWrapperPassPass(llvm::PassRegistry &);
2798 } // namespace llvm
2800 #endif