[ScopInfo] Fix assertion for PHIs not in a region stmts entry.
[polly-mirror.git] / include / polly / ScopInfo.h
blobf7473bee7c3d6ae3f10c482d3b841a63e5328831
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 "llvm/IR/PassManager.h"
27 #include "isl/aff.h"
28 #include "isl/ctx.h"
29 #include "isl/set.h"
31 #include "isl-noexceptions.h"
33 #include <deque>
34 #include <forward_list>
36 using namespace llvm;
38 namespace llvm {
39 class AssumptionCache;
40 class Loop;
41 class LoopInfo;
42 class PHINode;
43 class ScalarEvolution;
44 class SCEV;
45 class SCEVAddRecExpr;
46 class Type;
47 } // namespace llvm
49 struct isl_ctx;
50 struct isl_map;
51 struct isl_basic_map;
52 struct isl_id;
53 struct isl_set;
54 struct isl_union_set;
55 struct isl_union_map;
56 struct isl_space;
57 struct isl_ast_build;
58 struct isl_constraint;
59 struct isl_pw_aff;
60 struct isl_pw_multi_aff;
61 struct isl_schedule;
63 namespace polly {
65 class MemoryAccess;
66 class Scop;
67 class ScopStmt;
68 class ScopBuilder;
70 //===---------------------------------------------------------------------===//
72 extern bool UseInstructionNames;
74 /// Enumeration of assumptions Polly can take.
75 enum AssumptionKind {
76 ALIASING,
77 INBOUNDS,
78 WRAPPING,
79 UNSIGNED,
80 PROFITABLE,
81 ERRORBLOCK,
82 COMPLEXITY,
83 INFINITELOOP,
84 INVARIANTLOAD,
85 DELINEARIZATION,
88 /// Enum to distinguish between assumptions and restrictions.
89 enum AssumptionSign { AS_ASSUMPTION, AS_RESTRICTION };
91 /// The different memory kinds used in Polly.
92 ///
93 /// We distinguish between arrays and various scalar memory objects. We use
94 /// the term ``array'' to describe memory objects that consist of a set of
95 /// individual data elements arranged in a multi-dimensional grid. A scalar
96 /// memory object describes an individual data element and is used to model
97 /// the definition and uses of llvm::Values.
98 ///
99 /// The polyhedral model does traditionally not reason about SSA values. To
100 /// reason about llvm::Values we model them "as if" they were zero-dimensional
101 /// memory objects, even though they were not actually allocated in (main)
102 /// memory. Memory for such objects is only alloca[ed] at CodeGeneration
103 /// time. To relate the memory slots used during code generation with the
104 /// llvm::Values they belong to the new names for these corresponding stack
105 /// slots are derived by appending suffixes (currently ".s2a" and ".phiops")
106 /// to the name of the original llvm::Value. To describe how def/uses are
107 /// modeled exactly we use these suffixes here as well.
109 /// There are currently four different kinds of memory objects:
110 enum class MemoryKind {
111 /// MemoryKind::Array: Models a one or multi-dimensional array
113 /// A memory object that can be described by a multi-dimensional array.
114 /// Memory objects of this type are used to model actual multi-dimensional
115 /// arrays as they exist in LLVM-IR, but they are also used to describe
116 /// other objects:
117 /// - A single data element allocated on the stack using 'alloca' is
118 /// modeled as a one-dimensional, single-element array.
119 /// - A single data element allocated as a global variable is modeled as
120 /// one-dimensional, single-element array.
121 /// - Certain multi-dimensional arrays with variable size, which in
122 /// LLVM-IR are commonly expressed as a single-dimensional access with a
123 /// complicated access function, are modeled as multi-dimensional
124 /// memory objects (grep for "delinearization").
125 Array,
127 /// MemoryKind::Value: Models an llvm::Value
129 /// Memory objects of type MemoryKind::Value are used to model the data flow
130 /// induced by llvm::Values. For each llvm::Value that is used across
131 /// BasicBocks one ScopArrayInfo object is created. A single memory WRITE
132 /// stores the llvm::Value at its definition into the memory object and at
133 /// each use of the llvm::Value (ignoring trivial intra-block uses) a
134 /// corresponding READ is added. For instance, the use/def chain of a
135 /// llvm::Value %V depicted below
136 /// ______________________
137 /// |DefBB: |
138 /// | %V = float op ... |
139 /// ----------------------
140 /// | |
141 /// _________________ _________________
142 /// |UseBB1: | |UseBB2: |
143 /// | use float %V | | use float %V |
144 /// ----------------- -----------------
146 /// is modeled as if the following memory accesses occurred:
148 /// __________________________
149 /// |entry: |
150 /// | %V.s2a = alloca float |
151 /// --------------------------
152 /// |
153 /// ___________________________________
154 /// |DefBB: |
155 /// | store %float %V, float* %V.s2a |
156 /// -----------------------------------
157 /// | |
158 /// ____________________________________ ___________________________________
159 /// |UseBB1: | |UseBB2: |
160 /// | %V.reload1 = load float* %V.s2a | | %V.reload2 = load float* %V.s2a|
161 /// | use float %V.reload1 | | use float %V.reload2 |
162 /// ------------------------------------ -----------------------------------
164 Value,
166 /// MemoryKind::PHI: Models PHI nodes within the SCoP
168 /// Besides the MemoryKind::Value memory object used to model the normal
169 /// llvm::Value dependences described above, PHI nodes require an additional
170 /// memory object of type MemoryKind::PHI to describe the forwarding of values
171 /// to
172 /// the PHI node.
174 /// As an example, a PHIInst instructions
176 /// %PHI = phi float [ %Val1, %IncomingBlock1 ], [ %Val2, %IncomingBlock2 ]
178 /// is modeled as if the accesses occurred this way:
180 /// _______________________________
181 /// |entry: |
182 /// | %PHI.phiops = alloca float |
183 /// -------------------------------
184 /// | |
185 /// __________________________________ __________________________________
186 /// |IncomingBlock1: | |IncomingBlock2: |
187 /// | ... | | ... |
188 /// | store float %Val1 %PHI.phiops | | store float %Val2 %PHI.phiops |
189 /// | br label % JoinBlock | | br label %JoinBlock |
190 /// ---------------------------------- ----------------------------------
191 /// \ /
192 /// \ /
193 /// _________________________________________
194 /// |JoinBlock: |
195 /// | %PHI = load float, float* PHI.phiops |
196 /// -----------------------------------------
198 /// Note that there can also be a scalar write access for %PHI if used in a
199 /// different BasicBlock, i.e. there can be a memory object %PHI.phiops as
200 /// well as a memory object %PHI.s2a.
201 PHI,
203 /// MemoryKind::ExitPHI: Models PHI nodes in the SCoP's exit block
205 /// For PHI nodes in the Scop's exit block a special memory object kind is
206 /// used. The modeling used is identical to MemoryKind::PHI, with the
207 /// exception
208 /// that there are no READs from these memory objects. The PHINode's
209 /// llvm::Value is treated as a value escaping the SCoP. WRITE accesses
210 /// write directly to the escaping value's ".s2a" alloca.
211 ExitPHI
214 /// Maps from a loop to the affine function expressing its backedge taken count.
215 /// The backedge taken count already enough to express iteration domain as we
216 /// only allow loops with canonical induction variable.
217 /// A canonical induction variable is:
218 /// an integer recurrence that starts at 0 and increments by one each time
219 /// through the loop.
220 typedef std::map<const Loop *, const SCEV *> LoopBoundMapType;
222 typedef std::vector<std::unique_ptr<MemoryAccess>> AccFuncVector;
224 /// A class to store information about arrays in the SCoP.
226 /// Objects are accessible via the ScoP, MemoryAccess or the id associated with
227 /// the MemoryAccess access function.
229 class ScopArrayInfo {
230 public:
231 /// Construct a ScopArrayInfo object.
233 /// @param BasePtr The array base pointer.
234 /// @param ElementType The type of the elements stored in the array.
235 /// @param IslCtx The isl context used to create the base pointer id.
236 /// @param DimensionSizes A vector containing the size of each dimension.
237 /// @param Kind The kind of the array object.
238 /// @param DL The data layout of the module.
239 /// @param S The scop this array object belongs to.
240 /// @param BaseName The optional name of this memory reference.
241 ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *IslCtx,
242 ArrayRef<const SCEV *> DimensionSizes, MemoryKind Kind,
243 const DataLayout &DL, Scop *S, const char *BaseName = nullptr);
245 /// Update the element type of the ScopArrayInfo object.
247 /// Memory accesses referencing this ScopArrayInfo object may use
248 /// different element sizes. This function ensures the canonical element type
249 /// stored is small enough to model accesses to the current element type as
250 /// well as to @p NewElementType.
252 /// @param NewElementType An element type that is used to access this array.
253 void updateElementType(Type *NewElementType);
255 /// Update the sizes of the ScopArrayInfo object.
257 /// A ScopArrayInfo object may be created without all outer dimensions being
258 /// available. This function is called when new memory accesses are added for
259 /// this ScopArrayInfo object. It verifies that sizes are compatible and adds
260 /// additional outer array dimensions, if needed.
262 /// @param Sizes A vector of array sizes where the rightmost array
263 /// sizes need to match the innermost array sizes already
264 /// defined in SAI.
265 /// @param CheckConsistency Update sizes, even if new sizes are inconsistent
266 /// with old sizes
267 bool updateSizes(ArrayRef<const SCEV *> Sizes, bool CheckConsistency = true);
269 /// Make the ScopArrayInfo model a Fortran array.
270 /// It receives the Fortran array descriptor and stores this.
271 /// It also adds a piecewise expression for the outermost dimension
272 /// since this information is available for Fortran arrays at runtime.
273 void applyAndSetFAD(Value *FAD);
275 /// Destructor to free the isl id of the base pointer.
276 ~ScopArrayInfo();
278 /// Set the base pointer to @p BP.
279 void setBasePtr(Value *BP) { BasePtr = BP; }
281 /// Return the base pointer.
282 Value *getBasePtr() const { return BasePtr; }
284 // Set IsOnHeap to the value in parameter.
285 void setIsOnHeap(bool value) { IsOnHeap = value; }
287 /// For indirect accesses return the origin SAI of the BP, else null.
288 const ScopArrayInfo *getBasePtrOriginSAI() const { return BasePtrOriginSAI; }
290 /// The set of derived indirect SAIs for this origin SAI.
291 const SmallSetVector<ScopArrayInfo *, 2> &getDerivedSAIs() const {
292 return DerivedSAIs;
295 /// Return the number of dimensions.
296 unsigned getNumberOfDimensions() const {
297 if (Kind == MemoryKind::PHI || Kind == MemoryKind::ExitPHI ||
298 Kind == MemoryKind::Value)
299 return 0;
300 return DimensionSizes.size();
303 /// Return the size of dimension @p dim as SCEV*.
305 // Scalars do not have array dimensions and the first dimension of
306 // a (possibly multi-dimensional) array also does not carry any size
307 // information, in case the array is not newly created.
308 const SCEV *getDimensionSize(unsigned Dim) const {
309 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
310 return DimensionSizes[Dim];
313 /// Return the size of dimension @p dim as isl_pw_aff.
315 // Scalars do not have array dimensions and the first dimension of
316 // a (possibly multi-dimensional) array also does not carry any size
317 // information, in case the array is not newly created.
318 isl::pw_aff getDimensionSizePw(unsigned Dim) const {
319 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
320 return DimensionSizesPw[Dim];
323 /// Get the canonical element type of this array.
325 /// @returns The canonical element type of this array.
326 Type *getElementType() const { return ElementType; }
328 /// Get element size in bytes.
329 int getElemSizeInBytes() const;
331 /// Get the name of this memory reference.
332 std::string getName() const;
334 /// Return the isl id for the base pointer.
335 isl::id getBasePtrId() const;
337 /// Return what kind of memory this represents.
338 MemoryKind getKind() const { return Kind; }
340 /// Is this array info modeling an llvm::Value?
341 bool isValueKind() const { return Kind == MemoryKind::Value; }
343 /// Is this array info modeling special PHI node memory?
345 /// During code generation of PHI nodes, there is a need for two kinds of
346 /// virtual storage. The normal one as it is used for all scalar dependences,
347 /// where the result of the PHI node is stored and later loaded from as well
348 /// as a second one where the incoming values of the PHI nodes are stored
349 /// into and reloaded when the PHI is executed. As both memories use the
350 /// original PHI node as virtual base pointer, we have this additional
351 /// attribute to distinguish the PHI node specific array modeling from the
352 /// normal scalar array modeling.
353 bool isPHIKind() const { return Kind == MemoryKind::PHI; }
355 /// Is this array info modeling an MemoryKind::ExitPHI?
356 bool isExitPHIKind() const { return Kind == MemoryKind::ExitPHI; }
358 /// Is this array info modeling an array?
359 bool isArrayKind() const { return Kind == MemoryKind::Array; }
361 /// Is this array allocated on heap
363 /// This property is only relevant if the array is allocated by Polly instead
364 /// of pre-existing. If false, it is allocated using alloca instead malloca.
365 bool isOnHeap() const { return IsOnHeap; }
367 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
368 /// Dump a readable representation to stderr.
369 void dump() const;
370 #endif
372 /// Print a readable representation to @p OS.
374 /// @param SizeAsPwAff Print the size as isl_pw_aff
375 void print(raw_ostream &OS, bool SizeAsPwAff = false) const;
377 /// Access the ScopArrayInfo associated with an access function.
378 static const ScopArrayInfo *getFromAccessFunction(isl::pw_multi_aff PMA);
380 /// Access the ScopArrayInfo associated with an isl Id.
381 static const ScopArrayInfo *getFromId(isl::id Id);
383 /// Get the space of this array access.
384 isl::space getSpace() const;
386 /// If the array is read only
387 bool isReadOnly();
389 /// Verify that @p Array is compatible to this ScopArrayInfo.
391 /// Two arrays are compatible if their dimensionality, the sizes of their
392 /// dimensions, and their element sizes match.
394 /// @param Array The array to compare against.
396 /// @returns True, if the arrays are compatible, False otherwise.
397 bool isCompatibleWith(const ScopArrayInfo *Array) const;
399 private:
400 void addDerivedSAI(ScopArrayInfo *DerivedSAI) {
401 DerivedSAIs.insert(DerivedSAI);
404 /// For indirect accesses this is the SAI of the BP origin.
405 const ScopArrayInfo *BasePtrOriginSAI;
407 /// For origin SAIs the set of derived indirect SAIs.
408 SmallSetVector<ScopArrayInfo *, 2> DerivedSAIs;
410 /// The base pointer.
411 AssertingVH<Value> BasePtr;
413 /// The canonical element type of this array.
415 /// The canonical element type describes the minimal accessible element in
416 /// this array. Not all elements accessed, need to be of the very same type,
417 /// but the allocation size of the type of the elements loaded/stored from/to
418 /// this array needs to be a multiple of the allocation size of the canonical
419 /// type.
420 Type *ElementType;
422 /// The isl id for the base pointer.
423 isl::id Id;
425 /// True if the newly allocated array is on heap.
426 bool IsOnHeap;
428 /// The sizes of each dimension as SCEV*.
429 SmallVector<const SCEV *, 4> DimensionSizes;
431 /// The sizes of each dimension as isl_pw_aff.
432 SmallVector<isl::pw_aff, 4> DimensionSizesPw;
434 /// The type of this scop array info object.
436 /// We distinguish between SCALAR, PHI and ARRAY objects.
437 MemoryKind Kind;
439 /// The data layout of the module.
440 const DataLayout &DL;
442 /// The scop this SAI object belongs to.
443 Scop &S;
445 /// If this array models a Fortran array, then this points
446 /// to the Fortran array descriptor.
447 Value *FAD;
450 /// Represent memory accesses in statements.
451 class MemoryAccess {
452 friend class Scop;
453 friend class ScopStmt;
455 public:
456 /// The access type of a memory access
458 /// There are three kind of access types:
460 /// * A read access
462 /// A certain set of memory locations are read and may be used for internal
463 /// calculations.
465 /// * A must-write access
467 /// A certain set of memory locations is definitely written. The old value is
468 /// replaced by a newly calculated value. The old value is not read or used at
469 /// all.
471 /// * A may-write access
473 /// A certain set of memory locations may be written. The memory location may
474 /// contain a new value if there is actually a write or the old value may
475 /// remain, if no write happens.
476 enum AccessType {
477 READ = 0x1,
478 MUST_WRITE = 0x2,
479 MAY_WRITE = 0x3,
482 /// Reduction access type
484 /// Commutative and associative binary operations suitable for reductions
485 enum ReductionType {
486 RT_NONE, ///< Indicate no reduction at all
487 RT_ADD, ///< Addition
488 RT_MUL, ///< Multiplication
489 RT_BOR, ///< Bitwise Or
490 RT_BXOR, ///< Bitwise XOr
491 RT_BAND, ///< Bitwise And
494 private:
495 MemoryAccess(const MemoryAccess &) = delete;
496 const MemoryAccess &operator=(const MemoryAccess &) = delete;
498 /// A unique identifier for this memory access.
500 /// The identifier is unique between all memory accesses belonging to the same
501 /// scop statement.
502 isl::id Id;
504 /// What is modeled by this MemoryAccess.
505 /// @see MemoryKind
506 MemoryKind Kind;
508 /// Whether it a reading or writing access, and if writing, whether it
509 /// is conditional (MAY_WRITE).
510 enum AccessType AccType;
512 /// Reduction type for reduction like accesses, RT_NONE otherwise
514 /// An access is reduction like if it is part of a load-store chain in which
515 /// both access the same memory location (use the same LLVM-IR value
516 /// as pointer reference). Furthermore, between the load and the store there
517 /// is exactly one binary operator which is known to be associative and
518 /// commutative.
520 /// TODO:
522 /// We can later lift the constraint that the same LLVM-IR value defines the
523 /// memory location to handle scops such as the following:
525 /// for i
526 /// for j
527 /// sum[i+j] = sum[i] + 3;
529 /// Here not all iterations access the same memory location, but iterations
530 /// for which j = 0 holds do. After lifting the equality check in ScopBuilder,
531 /// subsequent transformations do not only need check if a statement is
532 /// reduction like, but they also need to verify that that the reduction
533 /// property is only exploited for statement instances that load from and
534 /// store to the same data location. Doing so at dependence analysis time
535 /// could allow us to handle the above example.
536 ReductionType RedType = RT_NONE;
538 /// Parent ScopStmt of this access.
539 ScopStmt *Statement;
541 /// The domain under which this access is not modeled precisely.
543 /// The invalid domain for an access describes all parameter combinations
544 /// under which the statement looks to be executed but is in fact not because
545 /// some assumption/restriction makes the access invalid.
546 isl::set InvalidDomain;
548 // Properties describing the accessed array.
549 // TODO: It might be possible to move them to ScopArrayInfo.
550 // @{
552 /// The base address (e.g., A for A[i+j]).
554 /// The #BaseAddr of a memory access of kind MemoryKind::Array is the base
555 /// pointer of the memory access.
556 /// The #BaseAddr of a memory access of kind MemoryKind::PHI or
557 /// MemoryKind::ExitPHI is the PHI node itself.
558 /// The #BaseAddr of a memory access of kind MemoryKind::Value is the
559 /// instruction defining the value.
560 AssertingVH<Value> BaseAddr;
562 /// Type a single array element wrt. this access.
563 Type *ElementType;
565 /// Size of each dimension of the accessed array.
566 SmallVector<const SCEV *, 4> Sizes;
567 // @}
569 // Properties describing the accessed element.
570 // @{
572 /// The access instruction of this memory access.
574 /// For memory accesses of kind MemoryKind::Array the access instruction is
575 /// the Load or Store instruction performing the access.
577 /// For memory accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI the
578 /// access instruction of a load access is the PHI instruction. The access
579 /// instruction of a PHI-store is the incoming's block's terminator
580 /// instruction.
582 /// For memory accesses of kind MemoryKind::Value the access instruction of a
583 /// load access is nullptr because generally there can be multiple
584 /// instructions in the statement using the same llvm::Value. The access
585 /// instruction of a write access is the instruction that defines the
586 /// llvm::Value.
587 Instruction *AccessInstruction;
589 /// Incoming block and value of a PHINode.
590 SmallVector<std::pair<BasicBlock *, Value *>, 4> Incoming;
592 /// The value associated with this memory access.
594 /// - For array memory accesses (MemoryKind::Array) it is the loaded result
595 /// or the stored value. If the access instruction is a memory intrinsic it
596 /// the access value is also the memory intrinsic.
597 /// - For accesses of kind MemoryKind::Value it is the access instruction
598 /// itself.
599 /// - For accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI it is the
600 /// PHI node itself (for both, READ and WRITE accesses).
602 AssertingVH<Value> AccessValue;
604 /// Are all the subscripts affine expression?
605 bool IsAffine;
607 /// Subscript expression for each dimension.
608 SmallVector<const SCEV *, 4> Subscripts;
610 /// Relation from statement instances to the accessed array elements.
612 /// In the common case this relation is a function that maps a set of loop
613 /// indices to the memory address from which a value is loaded/stored:
615 /// for i
616 /// for j
617 /// S: A[i + 3 j] = ...
619 /// => { S[i,j] -> A[i + 3j] }
621 /// In case the exact access function is not known, the access relation may
622 /// also be a one to all mapping { S[i,j] -> A[o] } describing that any
623 /// element accessible through A might be accessed.
625 /// In case of an access to a larger element belonging to an array that also
626 /// contains smaller elements, the access relation models the larger access
627 /// with multiple smaller accesses of the size of the minimal array element
628 /// type:
630 /// short *A;
632 /// for i
633 /// S: A[i] = *((double*)&A[4 * i]);
635 /// => { S[i] -> A[i]; S[i] -> A[o] : 4i <= o <= 4i + 3 }
636 isl::map AccessRelation;
638 /// Updated access relation read from JSCOP file.
639 isl::map NewAccessRelation;
641 /// Fortran arrays whose sizes are not statically known are stored in terms
642 /// of a descriptor struct. This maintains a raw pointer to the memory,
643 /// along with auxiliary fields with information such as dimensions.
644 /// We hold a reference to the descriptor corresponding to a MemoryAccess
645 /// into a Fortran array. FAD for "Fortran Array Descriptor"
646 AssertingVH<Value> FAD;
647 // @}
649 isl::basic_map createBasicAccessMap(ScopStmt *Statement);
651 void assumeNoOutOfBound();
653 /// Compute bounds on an over approximated access relation.
655 /// @param ElementSize The size of one element accessed.
656 void computeBoundsOnAccessRelation(unsigned ElementSize);
658 /// Get the original access function as read from IR.
659 isl::map getOriginalAccessRelation() const;
661 /// Return the space in which the access relation lives in.
662 isl::space getOriginalAccessRelationSpace() const;
664 /// Get the new access function imported or set by a pass
665 isl::map getNewAccessRelation() const;
667 /// Fold the memory access to consider parametric offsets
669 /// To recover memory accesses with array size parameters in the subscript
670 /// expression we post-process the delinearization results.
672 /// We would normally recover from an access A[exp0(i) * N + exp1(i)] into an
673 /// array A[][N] the 2D access A[exp0(i)][exp1(i)]. However, another valid
674 /// delinearization is A[exp0(i) - 1][exp1(i) + N] which - depending on the
675 /// range of exp1(i) - may be preferable. Specifically, for cases where we
676 /// know exp1(i) is negative, we want to choose the latter expression.
678 /// As we commonly do not have any information about the range of exp1(i),
679 /// we do not choose one of the two options, but instead create a piecewise
680 /// access function that adds the (-1, N) offsets as soon as exp1(i) becomes
681 /// negative. For a 2D array such an access function is created by applying
682 /// the piecewise map:
684 /// [i,j] -> [i, j] : j >= 0
685 /// [i,j] -> [i-1, j+N] : j < 0
687 /// We can generalize this mapping to arbitrary dimensions by applying this
688 /// piecewise mapping pairwise from the rightmost to the leftmost access
689 /// dimension. It would also be possible to cover a wider range by introducing
690 /// more cases and adding multiple of Ns to these cases. However, this has
691 /// not yet been necessary.
692 /// The introduction of different cases necessarily complicates the memory
693 /// access function, but cases that can be statically proven to not happen
694 /// will be eliminated later on.
695 void foldAccessRelation();
697 /// Create the access relation for the underlying memory intrinsic.
698 void buildMemIntrinsicAccessRelation();
700 /// Assemble the access relation from all available information.
702 /// In particular, used the information passes in the constructor and the
703 /// parent ScopStmt set by setStatment().
705 /// @param SAI Info object for the accessed array.
706 void buildAccessRelation(const ScopArrayInfo *SAI);
708 /// Carry index overflows of dimensions with constant size to the next higher
709 /// dimension.
711 /// For dimensions that have constant size, modulo the index by the size and
712 /// add up the carry (floored division) to the next higher dimension. This is
713 /// how overflow is defined in row-major order.
714 /// It happens e.g. when ScalarEvolution computes the offset to the base
715 /// pointer and would algebraically sum up all lower dimensions' indices of
716 /// constant size.
718 /// Example:
719 /// float (*A)[4];
720 /// A[1][6] -> A[2][2]
721 void wrapConstantDimensions();
723 public:
724 /// Create a new MemoryAccess.
726 /// @param Stmt The parent statement.
727 /// @param AccessInst The instruction doing the access.
728 /// @param BaseAddr The accessed array's address.
729 /// @param ElemType The type of the accessed array elements.
730 /// @param AccType Whether read or write access.
731 /// @param IsAffine Whether the subscripts are affine expressions.
732 /// @param Kind The kind of memory accessed.
733 /// @param Subscripts Subscript expressions
734 /// @param Sizes Dimension lengths of the accessed array.
735 MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, AccessType AccType,
736 Value *BaseAddress, Type *ElemType, bool Affine,
737 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
738 Value *AccessValue, MemoryKind Kind);
740 /// Create a new MemoryAccess that corresponds to @p AccRel.
742 /// Along with @p Stmt and @p AccType it uses information about dimension
743 /// lengths of the accessed array, the type of the accessed array elements,
744 /// the name of the accessed array that is derived from the object accessible
745 /// via @p AccRel.
747 /// @param Stmt The parent statement.
748 /// @param AccType Whether read or write access.
749 /// @param AccRel The access relation that describes the memory access.
750 MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel);
752 ~MemoryAccess();
754 /// Add a new incoming block/value pairs for this PHI/ExitPHI access.
756 /// @param IncomingBlock The PHI's incoming block.
757 /// @param IncomingValue The value when reaching the PHI from the @p
758 /// IncomingBlock.
759 void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue) {
760 assert(!isRead());
761 assert(isAnyPHIKind());
762 Incoming.emplace_back(std::make_pair(IncomingBlock, IncomingValue));
765 /// Return the list of possible PHI/ExitPHI values.
767 /// After code generation moves some PHIs around during region simplification,
768 /// we cannot reliably locate the original PHI node and its incoming values
769 /// anymore. For this reason we remember these explicitly for all PHI-kind
770 /// accesses.
771 ArrayRef<std::pair<BasicBlock *, Value *>> getIncoming() const {
772 assert(isAnyPHIKind());
773 return Incoming;
776 /// Get the type of a memory access.
777 enum AccessType getType() { return AccType; }
779 /// Is this a reduction like access?
780 bool isReductionLike() const { return RedType != RT_NONE; }
782 /// Is this a read memory access?
783 bool isRead() const { return AccType == MemoryAccess::READ; }
785 /// Is this a must-write memory access?
786 bool isMustWrite() const { return AccType == MemoryAccess::MUST_WRITE; }
788 /// Is this a may-write memory access?
789 bool isMayWrite() const { return AccType == MemoryAccess::MAY_WRITE; }
791 /// Is this a write memory access?
792 bool isWrite() const { return isMustWrite() || isMayWrite(); }
794 /// Is this a memory intrinsic access (memcpy, memset, memmove)?
795 bool isMemoryIntrinsic() const {
796 return isa<MemIntrinsic>(getAccessInstruction());
799 /// Check if a new access relation was imported or set by a pass.
800 bool hasNewAccessRelation() const { return !NewAccessRelation.is_null(); }
802 /// Return the newest access relation of this access.
804 /// There are two possibilities:
805 /// 1) The original access relation read from the LLVM-IR.
806 /// 2) A new access relation imported from a json file or set by another
807 /// pass (e.g., for privatization).
809 /// As 2) is by construction "newer" than 1) we return the new access
810 /// relation if present.
812 isl::map getLatestAccessRelation() const {
813 return hasNewAccessRelation() ? getNewAccessRelation()
814 : getOriginalAccessRelation();
817 /// Old name of getLatestAccessRelation().
818 isl::map getAccessRelation() const { return getLatestAccessRelation(); }
820 /// Get an isl map describing the memory address accessed.
822 /// In most cases the memory address accessed is well described by the access
823 /// relation obtained with getAccessRelation. However, in case of arrays
824 /// accessed with types of different size the access relation maps one access
825 /// to multiple smaller address locations. This method returns an isl map that
826 /// relates each dynamic statement instance to the unique memory location
827 /// that is loaded from / stored to.
829 /// For an access relation { S[i] -> A[o] : 4i <= o <= 4i + 3 } this method
830 /// will return the address function { S[i] -> A[4i] }.
832 /// @returns The address function for this memory access.
833 isl::map getAddressFunction() const;
835 /// Return the access relation after the schedule was applied.
836 isl::pw_multi_aff
837 applyScheduleToAccessRelation(isl::union_map Schedule) const;
839 /// Get an isl string representing the access function read from IR.
840 std::string getOriginalAccessRelationStr() const;
842 /// Get an isl string representing a new access function, if available.
843 std::string getNewAccessRelationStr() const;
845 /// Get an isl string representing the latest access relation.
846 std::string getAccessRelationStr() const;
848 /// Get the original base address of this access (e.g. A for A[i+j]) when
849 /// detected.
851 /// This adress may differ from the base address referenced by the Original
852 /// ScopArrayInfo to which this array belongs, as this memory access may
853 /// have been unified to a ScopArray which has a different but identically
854 /// valued base pointer in case invariant load hoisting is enabled.
855 Value *getOriginalBaseAddr() const { return BaseAddr; }
857 /// Get the detection-time base array isl_id for this access.
858 isl::id getOriginalArrayId() const;
860 /// Get the base array isl_id for this access, modifiable through
861 /// setNewAccessRelation().
862 isl::id getLatestArrayId() const;
864 /// Old name of getOriginalArrayId().
865 isl::id getArrayId() const { return getOriginalArrayId(); }
867 /// Get the detection-time ScopArrayInfo object for the base address.
868 const ScopArrayInfo *getOriginalScopArrayInfo() const;
870 /// Get the ScopArrayInfo object for the base address, or the one set
871 /// by setNewAccessRelation().
872 const ScopArrayInfo *getLatestScopArrayInfo() const;
874 /// Legacy name of getOriginalScopArrayInfo().
875 const ScopArrayInfo *getScopArrayInfo() const {
876 return getOriginalScopArrayInfo();
879 /// Return a string representation of the access's reduction type.
880 const std::string getReductionOperatorStr() const;
882 /// Return a string representation of the reduction type @p RT.
883 static const std::string getReductionOperatorStr(ReductionType RT);
885 /// Return the element type of the accessed array wrt. this access.
886 Type *getElementType() const { return ElementType; }
888 /// Return the access value of this memory access.
889 Value *getAccessValue() const { return AccessValue; }
891 /// Return llvm::Value that is stored by this access, if available.
893 /// PHI nodes may not have a unique value available that is stored, as in
894 /// case of region statements one out of possibly several llvm::Values
895 /// might be stored. In this case nullptr is returned.
896 Value *tryGetValueStored() {
897 assert(isWrite() && "Only write statement store values");
898 if (isAnyPHIKind()) {
899 if (Incoming.size() == 1)
900 return Incoming[0].second;
901 return nullptr;
903 return AccessValue;
906 /// Return the access instruction of this memory access.
907 Instruction *getAccessInstruction() const { return AccessInstruction; }
909 /// Return the number of access function subscript.
910 unsigned getNumSubscripts() const { return Subscripts.size(); }
912 /// Return the access function subscript in the dimension @p Dim.
913 const SCEV *getSubscript(unsigned Dim) const { return Subscripts[Dim]; }
915 /// Compute the isl representation for the SCEV @p E wrt. this access.
917 /// Note that this function will also adjust the invalid context accordingly.
918 isl::pw_aff getPwAff(const SCEV *E);
920 /// Get the invalid domain for this access.
921 isl::set getInvalidDomain() const { return InvalidDomain; }
923 /// Get the invalid context for this access.
924 isl::set getInvalidContext() const { return getInvalidDomain().params(); }
926 /// Get the stride of this memory access in the specified Schedule. Schedule
927 /// is a map from the statement to a schedule where the innermost dimension is
928 /// the dimension of the innermost loop containing the statement.
929 isl::set getStride(isl::map Schedule) const;
931 /// Get the FortranArrayDescriptor corresponding to this memory access if
932 /// it exists, and nullptr otherwise.
933 Value *getFortranArrayDescriptor() const { return this->FAD; };
935 /// Is the stride of the access equal to a certain width? Schedule is a map
936 /// from the statement to a schedule where the innermost dimension is the
937 /// dimension of the innermost loop containing the statement.
938 bool isStrideX(isl::map Schedule, int StrideWidth) const;
940 /// Is consecutive memory accessed for a given statement instance set?
941 /// Schedule is a map from the statement to a schedule where the innermost
942 /// dimension is the dimension of the innermost loop containing the
943 /// statement.
944 bool isStrideOne(isl::map Schedule) const;
946 /// Is always the same memory accessed for a given statement instance set?
947 /// Schedule is a map from the statement to a schedule where the innermost
948 /// dimension is the dimension of the innermost loop containing the
949 /// statement.
950 bool isStrideZero(isl::map Schedule) const;
952 /// Return the kind when this access was first detected.
953 MemoryKind getOriginalKind() const {
954 assert(!getOriginalScopArrayInfo() /* not yet initialized */ ||
955 getOriginalScopArrayInfo()->getKind() == Kind);
956 return Kind;
959 /// Return the kind considering a potential setNewAccessRelation.
960 MemoryKind getLatestKind() const {
961 return getLatestScopArrayInfo()->getKind();
964 /// Whether this is an access of an explicit load or store in the IR.
965 bool isOriginalArrayKind() const {
966 return getOriginalKind() == MemoryKind::Array;
969 /// Whether storage memory is either an custom .s2a/.phiops alloca
970 /// (false) or an existing pointer into an array (true).
971 bool isLatestArrayKind() const {
972 return getLatestKind() == MemoryKind::Array;
975 /// Old name of isOriginalArrayKind.
976 bool isArrayKind() const { return isOriginalArrayKind(); }
978 /// Whether this access is an array to a scalar memory object, without
979 /// considering changes by setNewAccessRelation.
981 /// Scalar accesses are accesses to MemoryKind::Value, MemoryKind::PHI or
982 /// MemoryKind::ExitPHI.
983 bool isOriginalScalarKind() const {
984 return getOriginalKind() != MemoryKind::Array;
987 /// Whether this access is an array to a scalar memory object, also
988 /// considering changes by setNewAccessRelation.
989 bool isLatestScalarKind() const {
990 return getLatestKind() != MemoryKind::Array;
993 /// Old name of isOriginalScalarKind.
994 bool isScalarKind() const { return isOriginalScalarKind(); }
996 /// Was this MemoryAccess detected as a scalar dependences?
997 bool isOriginalValueKind() const {
998 return getOriginalKind() == MemoryKind::Value;
1001 /// Is this MemoryAccess currently modeling scalar dependences?
1002 bool isLatestValueKind() const {
1003 return getLatestKind() == MemoryKind::Value;
1006 /// Old name of isOriginalValueKind().
1007 bool isValueKind() const { return isOriginalValueKind(); }
1009 /// Was this MemoryAccess detected as a special PHI node access?
1010 bool isOriginalPHIKind() const {
1011 return getOriginalKind() == MemoryKind::PHI;
1014 /// Is this MemoryAccess modeling special PHI node accesses, also
1015 /// considering a potential change by setNewAccessRelation?
1016 bool isLatestPHIKind() const { return getLatestKind() == MemoryKind::PHI; }
1018 /// Old name of isOriginalPHIKind.
1019 bool isPHIKind() const { return isOriginalPHIKind(); }
1021 /// Was this MemoryAccess detected as the accesses of a PHI node in the
1022 /// SCoP's exit block?
1023 bool isOriginalExitPHIKind() const {
1024 return getOriginalKind() == MemoryKind::ExitPHI;
1027 /// Is this MemoryAccess modeling the accesses of a PHI node in the
1028 /// SCoP's exit block? Can be changed to an array access using
1029 /// setNewAccessRelation().
1030 bool isLatestExitPHIKind() const {
1031 return getLatestKind() == MemoryKind::ExitPHI;
1034 /// Old name of isOriginalExitPHIKind().
1035 bool isExitPHIKind() const { return isOriginalExitPHIKind(); }
1037 /// Was this access detected as one of the two PHI types?
1038 bool isOriginalAnyPHIKind() const {
1039 return isOriginalPHIKind() || isOriginalExitPHIKind();
1042 /// Does this access originate from one of the two PHI types? Can be
1043 /// changed to an array access using setNewAccessRelation().
1044 bool isLatestAnyPHIKind() const {
1045 return isLatestPHIKind() || isLatestExitPHIKind();
1048 /// Old name of isOriginalAnyPHIKind().
1049 bool isAnyPHIKind() const { return isOriginalAnyPHIKind(); }
1051 /// Get the statement that contains this memory access.
1052 ScopStmt *getStatement() const { return Statement; }
1054 /// Get the reduction type of this access
1055 ReductionType getReductionType() const { return RedType; }
1057 /// Set the array descriptor corresponding to the Array on which the
1058 /// memory access is performed.
1059 void setFortranArrayDescriptor(Value *FAD);
1061 /// Update the original access relation.
1063 /// We need to update the original access relation during scop construction,
1064 /// when unifying the memory accesses that access the same scop array info
1065 /// object. After the scop has been constructed, the original access relation
1066 /// should not be changed any more. Instead setNewAccessRelation should
1067 /// be called.
1068 void setAccessRelation(__isl_take isl_map *AccessRelation);
1070 /// Set the updated access relation read from JSCOP file.
1071 void setNewAccessRelation(__isl_take isl_map *NewAccessRelation);
1073 /// Return whether the MemoryyAccess is a partial access. That is, the access
1074 /// is not executed in some instances of the parent statement's domain.
1075 bool isLatestPartialAccess() const;
1077 /// Mark this a reduction like access
1078 void markAsReductionLike(ReductionType RT) { RedType = RT; }
1080 /// Align the parameters in the access relation to the scop context
1081 void realignParams();
1083 /// Update the dimensionality of the memory access.
1085 /// During scop construction some memory accesses may not be constructed with
1086 /// their full dimensionality, but outer dimensions may have been omitted if
1087 /// they took the value 'zero'. By updating the dimensionality of the
1088 /// statement we add additional zero-valued dimensions to match the
1089 /// dimensionality of the ScopArrayInfo object that belongs to this memory
1090 /// access.
1091 void updateDimensionality();
1093 /// Get identifier for the memory access.
1095 /// This identifier is unique for all accesses that belong to the same scop
1096 /// statement.
1097 isl::id getId() const;
1099 /// Print the MemoryAccess.
1101 /// @param OS The output stream the MemoryAccess is printed to.
1102 void print(raw_ostream &OS) const;
1104 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1105 /// Print the MemoryAccess to stderr.
1106 void dump() const;
1107 #endif
1109 /// Is the memory access affine?
1110 bool isAffine() const { return IsAffine; }
1113 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1114 MemoryAccess::ReductionType RT);
1116 /// Ordered list type to hold accesses.
1117 using MemoryAccessList = std::forward_list<MemoryAccess *>;
1119 /// Helper structure for invariant memory accesses.
1120 struct InvariantAccess {
1121 /// The memory access that is (partially) invariant.
1122 MemoryAccess *MA;
1124 /// The context under which the access is not invariant.
1125 isl_set *NonHoistableCtx;
1128 /// Ordered container type to hold invariant accesses.
1129 using InvariantAccessesTy = SmallVector<InvariantAccess, 8>;
1131 /// Type for equivalent invariant accesses and their domain context.
1132 struct InvariantEquivClassTy {
1134 /// The pointer that identifies this equivalence class
1135 const SCEV *IdentifyingPointer;
1137 /// Memory accesses now treated invariant
1139 /// These memory accesses access the pointer location that identifies
1140 /// this equivalence class. They are treated as invariant and hoisted during
1141 /// code generation.
1142 MemoryAccessList InvariantAccesses;
1144 /// The execution context under which the memory location is accessed
1146 /// It is the union of the execution domains of the memory accesses in the
1147 /// InvariantAccesses list.
1148 isl_set *ExecutionContext;
1150 /// The type of the invariant access
1152 /// It is used to differentiate between differently typed invariant loads from
1153 /// the same location.
1154 Type *AccessType;
1157 /// Type for invariant accesses equivalence classes.
1158 using InvariantEquivClassesTy = SmallVector<InvariantEquivClassTy, 8>;
1160 /// Statement of the Scop
1162 /// A Scop statement represents an instruction in the Scop.
1164 /// It is further described by its iteration domain, its schedule and its data
1165 /// accesses.
1166 /// At the moment every statement represents a single basic block of LLVM-IR.
1167 class ScopStmt {
1168 public:
1169 ScopStmt(const ScopStmt &) = delete;
1170 const ScopStmt &operator=(const ScopStmt &) = delete;
1172 /// Create the ScopStmt from a BasicBlock.
1173 ScopStmt(Scop &parent, BasicBlock &bb, Loop *SurroundingLoop,
1174 std::vector<Instruction *> Instructions);
1176 /// Create an overapproximating ScopStmt for the region @p R.
1177 ScopStmt(Scop &parent, Region &R, Loop *SurroundingLoop);
1179 /// Create a copy statement.
1181 /// @param Stmt The parent statement.
1182 /// @param SourceRel The source location.
1183 /// @param TargetRel The target location.
1184 /// @param Domain The original domain under which the copy statement would
1185 /// be executed.
1186 ScopStmt(Scop &parent, __isl_take isl_map *SourceRel,
1187 __isl_take isl_map *TargetRel, __isl_take isl_set *Domain);
1189 /// Initialize members after all MemoryAccesses have been added.
1190 void init(LoopInfo &LI);
1192 private:
1193 /// Polyhedral description
1194 //@{
1196 /// The Scop containing this ScopStmt.
1197 Scop &Parent;
1199 /// The domain under which this statement is not modeled precisely.
1201 /// The invalid domain for a statement describes all parameter combinations
1202 /// under which the statement looks to be executed but is in fact not because
1203 /// some assumption/restriction makes the statement/scop invalid.
1204 isl_set *InvalidDomain;
1206 /// The iteration domain describes the set of iterations for which this
1207 /// statement is executed.
1209 /// Example:
1210 /// for (i = 0; i < 100 + b; ++i)
1211 /// for (j = 0; j < i; ++j)
1212 /// S(i,j);
1214 /// 'S' is executed for different values of i and j. A vector of all
1215 /// induction variables around S (i, j) is called iteration vector.
1216 /// The domain describes the set of possible iteration vectors.
1218 /// In this case it is:
1220 /// Domain: 0 <= i <= 100 + b
1221 /// 0 <= j <= i
1223 /// A pair of statement and iteration vector (S, (5,3)) is called statement
1224 /// instance.
1225 isl_set *Domain;
1227 /// The memory accesses of this statement.
1229 /// The only side effects of a statement are its memory accesses.
1230 typedef SmallVector<MemoryAccess *, 8> MemoryAccessVec;
1231 MemoryAccessVec MemAccs;
1233 /// Mapping from instructions to (scalar) memory accesses.
1234 DenseMap<const Instruction *, MemoryAccessList> InstructionToAccess;
1236 /// The set of values defined elsewhere required in this ScopStmt and
1237 /// their MemoryKind::Value READ MemoryAccesses.
1238 DenseMap<Value *, MemoryAccess *> ValueReads;
1240 /// The set of values defined in this ScopStmt that are required
1241 /// elsewhere, mapped to their MemoryKind::Value WRITE MemoryAccesses.
1242 DenseMap<Instruction *, MemoryAccess *> ValueWrites;
1244 /// Map from PHI nodes to its incoming value when coming from this
1245 /// statement.
1247 /// Non-affine subregions can have multiple exiting blocks that are incoming
1248 /// blocks of the PHI nodes. This map ensures that there is only one write
1249 /// operation for the complete subregion. A PHI selecting the relevant value
1250 /// will be inserted.
1251 DenseMap<PHINode *, MemoryAccess *> PHIWrites;
1253 /// Map from PHI nodes to its read access in this statement.
1254 DenseMap<PHINode *, MemoryAccess *> PHIReads;
1256 //@}
1258 /// A SCoP statement represents either a basic block (affine/precise case) or
1259 /// a whole region (non-affine case).
1261 /// Only one of the following two members will therefore be set and indicate
1262 /// which kind of statement this is.
1264 ///{
1266 /// The BasicBlock represented by this statement (in the affine case).
1267 BasicBlock *BB;
1269 /// The region represented by this statement (in the non-affine case).
1270 Region *R;
1272 ///}
1274 /// The isl AST build for the new generated AST.
1275 isl_ast_build *Build;
1277 SmallVector<Loop *, 4> NestLoops;
1279 std::string BaseName;
1281 /// The closest loop that contains this statement.
1282 Loop *SurroundingLoop;
1284 /// Vector for Instructions in a BB.
1285 std::vector<Instruction *> Instructions;
1287 /// Build the statement.
1288 //@{
1289 void buildDomain();
1291 /// Fill NestLoops with loops surrounding this statement.
1292 void collectSurroundingLoops();
1294 /// Build the access relation of all memory accesses.
1295 void buildAccessRelations();
1297 /// Detect and mark reductions in the ScopStmt
1298 void checkForReductions();
1300 /// Collect loads which might form a reduction chain with @p StoreMA
1301 void
1302 collectCandiateReductionLoads(MemoryAccess *StoreMA,
1303 llvm::SmallVectorImpl<MemoryAccess *> &Loads);
1304 //@}
1306 /// Remove @p MA from dictionaries pointing to them.
1307 void removeAccessData(MemoryAccess *MA);
1309 public:
1310 ~ScopStmt();
1312 /// Get an isl_ctx pointer.
1313 isl_ctx *getIslCtx() const;
1315 /// Get the iteration domain of this ScopStmt.
1317 /// @return The iteration domain of this ScopStmt.
1318 __isl_give isl_set *getDomain() const;
1320 /// Get the space of the iteration domain
1322 /// @return The space of the iteration domain
1323 __isl_give isl_space *getDomainSpace() const;
1325 /// Get the id of the iteration domain space
1327 /// @return The id of the iteration domain space
1328 __isl_give isl_id *getDomainId() const;
1330 /// Get an isl string representing this domain.
1331 std::string getDomainStr() const;
1333 /// Get the schedule function of this ScopStmt.
1335 /// @return The schedule function of this ScopStmt, if it does not contain
1336 /// extension nodes, and nullptr, otherwise.
1337 __isl_give isl_map *getSchedule() const;
1339 /// Get an isl string representing this schedule.
1341 /// @return An isl string representing this schedule, if it does not contain
1342 /// extension nodes, and an empty string, otherwise.
1343 std::string getScheduleStr() const;
1345 /// Get the invalid domain for this statement.
1346 __isl_give isl_set *getInvalidDomain() const {
1347 return isl_set_copy(InvalidDomain);
1350 /// Get the invalid context for this statement.
1351 __isl_give isl_set *getInvalidContext() const {
1352 return isl_set_params(getInvalidDomain());
1355 /// Set the invalid context for this statement to @p ID.
1356 void setInvalidDomain(__isl_take isl_set *ID);
1358 /// Get the BasicBlock represented by this ScopStmt (if any).
1360 /// @return The BasicBlock represented by this ScopStmt, or null if the
1361 /// statement represents a region.
1362 BasicBlock *getBasicBlock() const { return BB; }
1364 /// Return true if this statement represents a single basic block.
1365 bool isBlockStmt() const { return BB != nullptr; }
1367 /// Return true if this is a copy statement.
1368 bool isCopyStmt() const { return BB == nullptr && R == nullptr; }
1370 /// Get the region represented by this ScopStmt (if any).
1372 /// @return The region represented by this ScopStmt, or null if the statement
1373 /// represents a basic block.
1374 Region *getRegion() const { return R; }
1376 /// Return true if this statement represents a whole region.
1377 bool isRegionStmt() const { return R != nullptr; }
1379 /// Return a BasicBlock from this statement.
1381 /// For block statements, it returns the BasicBlock itself. For subregion
1382 /// statements, return its entry block.
1383 BasicBlock *getEntryBlock() const;
1385 /// Return whether @p L is boxed within this statement.
1386 bool contains(const Loop *L) const {
1387 // Block statements never contain loops.
1388 if (isBlockStmt())
1389 return false;
1391 return getRegion()->contains(L);
1394 /// Return whether this statement contains @p BB.
1395 bool contains(BasicBlock *BB) const {
1396 if (isCopyStmt())
1397 return false;
1398 if (isBlockStmt())
1399 return BB == getBasicBlock();
1400 return getRegion()->contains(BB);
1403 /// Return whether this statement contains @p Inst.
1404 bool contains(Instruction *Inst) const {
1405 if (!Inst)
1406 return false;
1407 return contains(Inst->getParent());
1410 /// Return the closest innermost loop that contains this statement, but is not
1411 /// contained in it.
1413 /// For block statement, this is just the loop that contains the block. Region
1414 /// statements can contain boxed loops, so getting the loop of one of the
1415 /// region's BBs might return such an inner loop. For instance, the region's
1416 /// entry could be a header of a loop, but the region might extend to BBs
1417 /// after the loop exit. Similarly, the region might only contain parts of the
1418 /// loop body and still include the loop header.
1420 /// Most of the time the surrounding loop is the top element of #NestLoops,
1421 /// except when it is empty. In that case it return the loop that the whole
1422 /// SCoP is contained in. That can be nullptr if there is no such loop.
1423 Loop *getSurroundingLoop() const {
1424 assert(!isCopyStmt() &&
1425 "No surrounding loop for artificially created statements");
1426 return SurroundingLoop;
1429 /// Return true if this statement does not contain any accesses.
1430 bool isEmpty() const { return MemAccs.empty(); }
1432 /// Find all array accesses for @p Inst.
1434 /// @param Inst The instruction accessing an array.
1436 /// @return A list of array accesses (MemoryKind::Array) accessed by @p Inst.
1437 /// If there is no such access, it returns nullptr.
1438 const MemoryAccessList *
1439 lookupArrayAccessesFor(const Instruction *Inst) const {
1440 auto It = InstructionToAccess.find(Inst);
1441 if (It == InstructionToAccess.end())
1442 return nullptr;
1443 if (It->second.empty())
1444 return nullptr;
1445 return &It->second;
1448 /// Return the only array access for @p Inst, if existing.
1450 /// @param Inst The instruction for which to look up the access.
1451 /// @returns The unique array memory access related to Inst or nullptr if
1452 /// no array access exists
1453 MemoryAccess *getArrayAccessOrNULLFor(const Instruction *Inst) const {
1454 auto It = InstructionToAccess.find(Inst);
1455 if (It == InstructionToAccess.end())
1456 return nullptr;
1458 MemoryAccess *ArrayAccess = nullptr;
1460 for (auto Access : It->getSecond()) {
1461 if (!Access->isArrayKind())
1462 continue;
1464 assert(!ArrayAccess && "More then one array access for instruction");
1466 ArrayAccess = Access;
1469 return ArrayAccess;
1472 /// Return the only array access for @p Inst.
1474 /// @param Inst The instruction for which to look up the access.
1475 /// @returns The unique array memory access related to Inst.
1476 MemoryAccess &getArrayAccessFor(const Instruction *Inst) const {
1477 MemoryAccess *ArrayAccess = getArrayAccessOrNULLFor(Inst);
1479 assert(ArrayAccess && "No array access found for instruction!");
1480 return *ArrayAccess;
1483 /// Return the MemoryAccess that writes the value of an instruction
1484 /// defined in this statement, or nullptr if not existing, respectively
1485 /// not yet added.
1486 MemoryAccess *lookupValueWriteOf(Instruction *Inst) const {
1487 assert((isRegionStmt() && R->contains(Inst)) ||
1488 (!isRegionStmt() && Inst->getParent() == BB));
1489 return ValueWrites.lookup(Inst);
1492 /// Return the MemoryAccess that reloads a value, or nullptr if not
1493 /// existing, respectively not yet added.
1494 MemoryAccess *lookupValueReadOf(Value *Inst) const {
1495 return ValueReads.lookup(Inst);
1498 /// Return the MemoryAccess that loads a PHINode value, or nullptr if not
1499 /// existing, respectively not yet added.
1500 MemoryAccess *lookupPHIReadOf(PHINode *PHI) const {
1501 return PHIReads.lookup(PHI);
1504 /// Return the PHI write MemoryAccess for the incoming values from any
1505 /// basic block in this ScopStmt, or nullptr if not existing,
1506 /// respectively not yet added.
1507 MemoryAccess *lookupPHIWriteOf(PHINode *PHI) const {
1508 assert(isBlockStmt() || R->getExit() == PHI->getParent());
1509 return PHIWrites.lookup(PHI);
1512 /// Return the input access of the value, or null if no such MemoryAccess
1513 /// exists.
1515 /// The input access is the MemoryAccess that makes an inter-statement value
1516 /// available in this statement by reading it at the start of this statement.
1517 /// This can be a MemoryKind::Value if defined in another statement or a
1518 /// MemoryKind::PHI if the value is a PHINode in this statement.
1519 MemoryAccess *lookupInputAccessOf(Value *Val) const {
1520 if (isa<PHINode>(Val))
1521 if (auto InputMA = lookupPHIReadOf(cast<PHINode>(Val))) {
1522 assert(!lookupValueReadOf(Val) && "input accesses must be unique; a "
1523 "statement cannot read a .s2a and "
1524 ".phiops simultaneously");
1525 return InputMA;
1528 if (auto *InputMA = lookupValueReadOf(Val))
1529 return InputMA;
1531 return nullptr;
1534 /// Add @p Access to this statement's list of accesses.
1535 void addAccess(MemoryAccess *Access);
1537 /// Remove a MemoryAccess from this statement.
1539 /// Note that scalar accesses that are caused by MA will
1540 /// be eliminated too.
1541 void removeMemoryAccess(MemoryAccess *MA);
1543 /// Remove @p MA from this statement.
1545 /// In contrast to removeMemoryAccess(), no other access will be eliminated.
1546 void removeSingleMemoryAccess(MemoryAccess *MA);
1548 typedef MemoryAccessVec::iterator iterator;
1549 typedef MemoryAccessVec::const_iterator const_iterator;
1551 iterator begin() { return MemAccs.begin(); }
1552 iterator end() { return MemAccs.end(); }
1553 const_iterator begin() const { return MemAccs.begin(); }
1554 const_iterator end() const { return MemAccs.end(); }
1555 size_t size() const { return MemAccs.size(); }
1557 unsigned getNumIterators() const;
1559 Scop *getParent() { return &Parent; }
1560 const Scop *getParent() const { return &Parent; }
1562 const std::vector<Instruction *> &getInstructions() const {
1563 return Instructions;
1566 /// Set the list of instructions for this statement. It replaces the current
1567 /// list.
1568 void setInstructions(ArrayRef<Instruction *> Range) {
1569 Instructions.assign(Range.begin(), Range.end());
1572 std::vector<Instruction *>::const_iterator insts_begin() const {
1573 return Instructions.begin();
1576 std::vector<Instruction *>::const_iterator insts_end() const {
1577 return Instructions.end();
1580 /// The range of instructions in this statement.
1581 llvm::iterator_range<std::vector<Instruction *>::const_iterator>
1582 insts() const {
1583 return {insts_begin(), insts_end()};
1586 /// Insert an instruction before all other instructions in this statement.
1587 void prependInstruction(Instruction *Inst) {
1588 assert(isBlockStmt() && "Only block statements support instruction lists");
1589 Instructions.insert(Instructions.begin(), Inst);
1592 const char *getBaseName() const;
1594 /// Set the isl AST build.
1595 void setAstBuild(__isl_keep isl_ast_build *B) { Build = B; }
1597 /// Get the isl AST build.
1598 __isl_keep isl_ast_build *getAstBuild() const { return Build; }
1600 /// Restrict the domain of the statement.
1602 /// @param NewDomain The new statement domain.
1603 void restrictDomain(__isl_take isl_set *NewDomain);
1605 /// Get the loop for a dimension.
1607 /// @param Dimension The dimension of the induction variable
1608 /// @return The loop at a certain dimension.
1609 Loop *getLoopForDimension(unsigned Dimension) const;
1611 /// Align the parameters in the statement to the scop context
1612 void realignParams();
1614 /// Print the ScopStmt.
1616 /// @param OS The output stream the ScopStmt is printed to.
1617 /// @param PrintInstructions Whether to print the statement's instructions as
1618 /// well.
1619 void print(raw_ostream &OS, bool PrintInstructions) const;
1621 /// Print the instructions in ScopStmt.
1623 void printInstructions(raw_ostream &OS) const;
1625 /// Check whether there is a value read access for @p V in this statement, and
1626 /// if not, create one.
1628 /// This allows to add MemoryAccesses after the initial creation of the Scop
1629 /// by ScopBuilder.
1631 /// @return The already existing or newly created MemoryKind::Value READ
1632 /// MemoryAccess.
1634 /// @see ScopBuilder::ensureValueRead(Value*,ScopStmt*)
1635 MemoryAccess *ensureValueRead(Value *V);
1637 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1638 /// Print the ScopStmt to stderr.
1639 void dump() const;
1640 #endif
1643 /// Print ScopStmt S to raw_ostream O.
1644 raw_ostream &operator<<(raw_ostream &O, const ScopStmt &S);
1646 /// Static Control Part
1648 /// A Scop is the polyhedral representation of a control flow region detected
1649 /// by the Scop detection. It is generated by translating the LLVM-IR and
1650 /// abstracting its effects.
1652 /// A Scop consists of a set of:
1654 /// * A set of statements executed in the Scop.
1656 /// * A set of global parameters
1657 /// Those parameters are scalar integer values, which are constant during
1658 /// execution.
1660 /// * A context
1661 /// This context contains information about the values the parameters
1662 /// can take and relations between different parameters.
1663 class Scop {
1664 public:
1665 /// Type to represent a pair of minimal/maximal access to an array.
1666 using MinMaxAccessTy = std::pair<isl_pw_multi_aff *, isl_pw_multi_aff *>;
1668 /// Vector of minimal/maximal accesses to different arrays.
1669 using MinMaxVectorTy = SmallVector<MinMaxAccessTy, 4>;
1671 /// Pair of minimal/maximal access vectors representing
1672 /// read write and read only accesses
1673 using MinMaxVectorPairTy = std::pair<MinMaxVectorTy, MinMaxVectorTy>;
1675 /// Vector of pair of minimal/maximal access vectors representing
1676 /// non read only and read only accesses for each alias group.
1677 using MinMaxVectorPairVectorTy = SmallVector<MinMaxVectorPairTy, 4>;
1679 private:
1680 Scop(const Scop &) = delete;
1681 const Scop &operator=(const Scop &) = delete;
1683 ScalarEvolution *SE;
1685 /// The underlying Region.
1686 Region &R;
1688 /// The name of the SCoP (identical to the regions name)
1689 std::string name;
1691 /// The ID to be assigned to the next Scop in a function
1692 static int NextScopID;
1694 /// The name of the function currently under consideration
1695 static std::string CurrentFunc;
1697 // Access functions of the SCoP.
1699 // This owns all the MemoryAccess objects of the Scop created in this pass.
1700 AccFuncVector AccessFunctions;
1702 /// Flag to indicate that the scheduler actually optimized the SCoP.
1703 bool IsOptimized;
1705 /// True if the underlying region has a single exiting block.
1706 bool HasSingleExitEdge;
1708 /// Flag to remember if the SCoP contained an error block or not.
1709 bool HasErrorBlock;
1711 /// Max loop depth.
1712 unsigned MaxLoopDepth;
1714 /// Number of copy statements.
1715 unsigned CopyStmtsNum;
1717 /// Flag to indicate if the Scop is to be skipped.
1718 bool SkipScop;
1720 typedef std::list<ScopStmt> StmtSet;
1721 /// The statements in this Scop.
1722 StmtSet Stmts;
1724 /// Parameters of this Scop
1725 ParameterSetTy Parameters;
1727 /// Mapping from parameters to their ids.
1728 DenseMap<const SCEV *, isl_id *> ParameterIds;
1730 /// The context of the SCoP created during SCoP detection.
1731 ScopDetection::DetectionContext &DC;
1733 /// OptimizationRemarkEmitter object for displaying diagnostic remarks
1734 OptimizationRemarkEmitter &ORE;
1736 /// Isl context.
1738 /// We need a shared_ptr with reference counter to delete the context when all
1739 /// isl objects are deleted. We will distribute the shared_ptr to all objects
1740 /// that use the context to create isl objects, and increase the reference
1741 /// counter. By doing this, we guarantee that the context is deleted when we
1742 /// delete the last object that creates isl objects with the context.
1743 std::shared_ptr<isl_ctx> IslCtx;
1745 /// A map from basic blocks to vector of SCoP statements. Currently this
1746 /// vector comprises only of a single statement.
1747 DenseMap<BasicBlock *, std::vector<ScopStmt *>> StmtMap;
1749 /// A map from basic blocks to their domains.
1750 DenseMap<BasicBlock *, isl::set> DomainMap;
1752 /// Constraints on parameters.
1753 isl_set *Context;
1755 /// The affinator used to translate SCEVs to isl expressions.
1756 SCEVAffinator Affinator;
1758 typedef std::map<std::pair<AssertingVH<const Value>, MemoryKind>,
1759 std::unique_ptr<ScopArrayInfo>>
1760 ArrayInfoMapTy;
1762 typedef StringMap<std::unique_ptr<ScopArrayInfo>> ArrayNameMapTy;
1764 typedef SetVector<ScopArrayInfo *> ArrayInfoSetTy;
1766 /// A map to remember ScopArrayInfo objects for all base pointers.
1768 /// As PHI nodes may have two array info objects associated, we add a flag
1769 /// that distinguishes between the PHI node specific ArrayInfo object
1770 /// and the normal one.
1771 ArrayInfoMapTy ScopArrayInfoMap;
1773 /// A map to remember ScopArrayInfo objects for all names of memory
1774 /// references.
1775 ArrayNameMapTy ScopArrayNameMap;
1777 /// A set to remember ScopArrayInfo objects.
1778 /// @see Scop::ScopArrayInfoMap
1779 ArrayInfoSetTy ScopArrayInfoSet;
1781 /// The assumptions under which this scop was built.
1783 /// When constructing a scop sometimes the exact representation of a statement
1784 /// or condition would be very complex, but there is a common case which is a
1785 /// lot simpler, but which is only valid under certain assumptions. The
1786 /// assumed context records the assumptions taken during the construction of
1787 /// this scop and that need to be code generated as a run-time test.
1788 isl_set *AssumedContext;
1790 /// The restrictions under which this SCoP was built.
1792 /// The invalid context is similar to the assumed context as it contains
1793 /// constraints over the parameters. However, while we need the constraints
1794 /// in the assumed context to be "true" the constraints in the invalid context
1795 /// need to be "false". Otherwise they behave the same.
1796 isl_set *InvalidContext;
1798 /// Helper struct to remember assumptions.
1799 struct Assumption {
1801 /// The kind of the assumption (e.g., WRAPPING).
1802 AssumptionKind Kind;
1804 /// Flag to distinguish assumptions and restrictions.
1805 AssumptionSign Sign;
1807 /// The valid/invalid context if this is an assumption/restriction.
1808 isl_set *Set;
1810 /// The location that caused this assumption.
1811 DebugLoc Loc;
1813 /// An optional block whose domain can simplify the assumption.
1814 BasicBlock *BB;
1817 /// Collection to hold taken assumptions.
1819 /// There are two reasons why we want to record assumptions first before we
1820 /// add them to the assumed/invalid context:
1821 /// 1) If the SCoP is not profitable or otherwise invalid without the
1822 /// assumed/invalid context we do not have to compute it.
1823 /// 2) Information about the context are gathered rather late in the SCoP
1824 /// construction (basically after we know all parameters), thus the user
1825 /// might see overly complicated assumptions to be taken while they will
1826 /// only be simplified later on.
1827 SmallVector<Assumption, 8> RecordedAssumptions;
1829 /// The schedule of the SCoP
1831 /// The schedule of the SCoP describes the execution order of the statements
1832 /// in the scop by assigning each statement instance a possibly
1833 /// multi-dimensional execution time. The schedule is stored as a tree of
1834 /// schedule nodes.
1836 /// The most common nodes in a schedule tree are so-called band nodes. Band
1837 /// nodes map statement instances into a multi dimensional schedule space.
1838 /// This space can be seen as a multi-dimensional clock.
1840 /// Example:
1842 /// <S,(5,4)> may be mapped to (5,4) by this schedule:
1844 /// s0 = i (Year of execution)
1845 /// s1 = j (Day of execution)
1847 /// or to (9, 20) by this schedule:
1849 /// s0 = i + j (Year of execution)
1850 /// s1 = 20 (Day of execution)
1852 /// The order statement instances are executed is defined by the
1853 /// schedule vectors they are mapped to. A statement instance
1854 /// <A, (i, j, ..)> is executed before a statement instance <B, (i', ..)>, if
1855 /// the schedule vector of A is lexicographic smaller than the schedule
1856 /// vector of B.
1858 /// Besides band nodes, schedule trees contain additional nodes that specify
1859 /// a textual ordering between two subtrees or filter nodes that filter the
1860 /// set of statement instances that will be scheduled in a subtree. There
1861 /// are also several other nodes. A full description of the different nodes
1862 /// in a schedule tree is given in the isl manual.
1863 isl_schedule *Schedule;
1865 /// The set of minimal/maximal accesses for each alias group.
1867 /// When building runtime alias checks we look at all memory instructions and
1868 /// build so called alias groups. Each group contains a set of accesses to
1869 /// different base arrays which might alias with each other. However, between
1870 /// alias groups there is no aliasing possible.
1872 /// In a program with int and float pointers annotated with tbaa information
1873 /// we would probably generate two alias groups, one for the int pointers and
1874 /// one for the float pointers.
1876 /// During code generation we will create a runtime alias check for each alias
1877 /// group to ensure the SCoP is executed in an alias free environment.
1878 MinMaxVectorPairVectorTy MinMaxAliasGroups;
1880 /// Mapping from invariant loads to the representing invariant load of
1881 /// their equivalence class.
1882 ValueToValueMap InvEquivClassVMap;
1884 /// List of invariant accesses.
1885 InvariantEquivClassesTy InvariantEquivClasses;
1887 /// The smallest array index not yet assigned.
1888 long ArrayIdx = 0;
1890 /// The smallest statement index not yet assigned.
1891 long StmtIdx = 0;
1893 /// A number that uniquely represents a Scop within its function
1894 const int ID;
1896 /// List of all uses (i.e. read MemoryAccesses) for a MemoryKind::Value
1897 /// scalar.
1898 DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>> ValueUseAccs;
1900 /// List of all incoming values (write MemoryAccess) of a MemoryKind::PHI or
1901 /// MemoryKind::ExitPHI scalar.
1902 DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>>
1903 PHIIncomingAccs;
1905 /// Return the ID for a new Scop within a function
1906 static int getNextID(std::string ParentFunc);
1908 /// Scop constructor; invoked from ScopBuilder::buildScop.
1909 Scop(Region &R, ScalarEvolution &SE, LoopInfo &LI,
1910 ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE);
1912 //@}
1914 /// Initialize this ScopBuilder.
1915 void init(AliasAnalysis &AA, AssumptionCache &AC, DominatorTree &DT,
1916 LoopInfo &LI);
1918 /// Propagate domains that are known due to graph properties.
1920 /// As a CFG is mostly structured we use the graph properties to propagate
1921 /// domains without the need to compute all path conditions. In particular, if
1922 /// a block A dominates a block B and B post-dominates A we know that the
1923 /// domain of B is a superset of the domain of A. As we do not have
1924 /// post-dominator information available here we use the less precise region
1925 /// information. Given a region R, we know that the exit is always executed if
1926 /// the entry was executed, thus the domain of the exit is a superset of the
1927 /// domain of the entry. In case the exit can only be reached from within the
1928 /// region the domains are in fact equal. This function will use this property
1929 /// to avoid the generation of condition constraints that determine when a
1930 /// branch is taken. If @p BB is a region entry block we will propagate its
1931 /// domain to the region exit block. Additionally, we put the region exit
1932 /// block in the @p FinishedExitBlocks set so we can later skip edges from
1933 /// within the region to that block.
1935 /// @param BB The block for which the domain is currently
1936 /// propagated.
1937 /// @param BBLoop The innermost affine loop surrounding @p BB.
1938 /// @param FinishedExitBlocks Set of region exits the domain was set for.
1939 /// @param LI The LoopInfo for the current function.
1940 /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
1941 /// region.
1942 void propagateDomainConstraintsToRegionExit(
1943 BasicBlock *BB, Loop *BBLoop,
1944 SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI,
1945 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
1947 /// Compute the union of predecessor domains for @p BB.
1949 /// To compute the union of all domains of predecessors of @p BB this
1950 /// function applies similar reasoning on the CFG structure as described for
1951 /// @see propagateDomainConstraintsToRegionExit
1953 /// @param BB The block for which the predecessor domains are collected.
1954 /// @param Domain The domain under which BB is executed.
1955 /// @param DT The DominatorTree for the current function.
1956 /// @param LI The LoopInfo for the current function.
1958 /// @returns The domain under which @p BB is executed.
1959 __isl_give isl_set *
1960 getPredecessorDomainConstraints(BasicBlock *BB, __isl_keep isl_set *Domain,
1961 DominatorTree &DT, LoopInfo &LI);
1963 /// Add loop carried constraints to the header block of the loop @p L.
1965 /// @param L The loop to process.
1966 /// @param LI The LoopInfo for the current function.
1967 /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
1968 /// region.
1970 /// @returns True if there was no problem and false otherwise.
1971 bool addLoopBoundsToHeaderDomain(
1972 Loop *L, LoopInfo &LI,
1973 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
1975 /// Compute the branching constraints for each basic block in @p R.
1977 /// @param R The region we currently build branching conditions
1978 /// for.
1979 /// @param DT The DominatorTree for the current function.
1980 /// @param LI The LoopInfo for the current function.
1981 /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
1982 /// region.
1984 /// @returns True if there was no problem and false otherwise.
1985 bool buildDomainsWithBranchConstraints(
1986 Region *R, DominatorTree &DT, LoopInfo &LI,
1987 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
1989 /// Propagate the domain constraints through the region @p R.
1991 /// @param R The region we currently build branching conditions
1992 /// for.
1993 /// @param DT The DominatorTree for the current function.
1994 /// @param LI The LoopInfo for the current function.
1995 /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
1996 /// region.
1998 /// @returns True if there was no problem and false otherwise.
1999 bool propagateDomainConstraints(
2000 Region *R, DominatorTree &DT, LoopInfo &LI,
2001 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
2003 /// Propagate invalid domains of statements through @p R.
2005 /// This method will propagate invalid statement domains through @p R and at
2006 /// the same time add error block domains to them. Additionally, the domains
2007 /// of error statements and those only reachable via error statements will be
2008 /// replaced by an empty set. Later those will be removed completely.
2010 /// @param R The currently traversed region.
2011 /// @param DT The DominatorTree for the current function.
2012 /// @param LI The LoopInfo for the current function.
2013 /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
2014 /// region.
2016 /// @returns True if there was no problem and false otherwise.
2017 bool propagateInvalidStmtDomains(
2018 Region *R, DominatorTree &DT, LoopInfo &LI,
2019 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
2021 /// Compute the domain for each basic block in @p R.
2023 /// @param R The region we currently traverse.
2024 /// @param DT The DominatorTree for the current function.
2025 /// @param LI The LoopInfo for the current function.
2026 /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
2027 /// region.
2029 /// @returns True if there was no problem and false otherwise.
2030 bool buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI,
2031 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
2033 /// Add parameter constraints to @p C that imply a non-empty domain.
2034 __isl_give isl_set *addNonEmptyDomainConstraints(__isl_take isl_set *C) const;
2036 /// Return the access for the base ptr of @p MA if any.
2037 MemoryAccess *lookupBasePtrAccess(MemoryAccess *MA);
2039 /// Check if the base ptr of @p MA is in the SCoP but not hoistable.
2040 bool hasNonHoistableBasePtrInScop(MemoryAccess *MA, isl::union_map Writes);
2042 /// Create equivalence classes for required invariant accesses.
2044 /// These classes will consolidate multiple required invariant loads from the
2045 /// same address in order to keep the number of dimensions in the SCoP
2046 /// description small. For each such class equivalence class only one
2047 /// representing element, hence one required invariant load, will be chosen
2048 /// and modeled as parameter. The method
2049 /// Scop::getRepresentingInvariantLoadSCEV() will replace each element from an
2050 /// equivalence class with the representing element that is modeled. As a
2051 /// consequence Scop::getIdForParam() will only return an id for the
2052 /// representing element of each equivalence class, thus for each required
2053 /// invariant location.
2054 void buildInvariantEquivalenceClasses();
2056 /// Return the context under which the access cannot be hoisted.
2058 /// @param Access The access to check.
2059 /// @param Writes The set of all memory writes in the scop.
2061 /// @return Return the context under which the access cannot be hoisted or a
2062 /// nullptr if it cannot be hoisted at all.
2063 isl::set getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes);
2065 /// Verify that all required invariant loads have been hoisted.
2067 /// Invariant load hoisting is not guaranteed to hoist all loads that were
2068 /// assumed to be scop invariant during scop detection. This function checks
2069 /// for cases where the hoisting failed, but where it would have been
2070 /// necessary for our scop modeling to be correct. In case of insufficient
2071 /// hoisting the scop is marked as invalid.
2073 /// In the example below Bound[1] is required to be invariant:
2075 /// for (int i = 1; i < Bound[0]; i++)
2076 /// for (int j = 1; j < Bound[1]; j++)
2077 /// ...
2079 void verifyInvariantLoads();
2081 /// Hoist invariant memory loads and check for required ones.
2083 /// We first identify "common" invariant loads, thus loads that are invariant
2084 /// and can be hoisted. Then we check if all required invariant loads have
2085 /// been identified as (common) invariant. A load is a required invariant load
2086 /// if it was assumed to be invariant during SCoP detection, e.g., to assume
2087 /// loop bounds to be affine or runtime alias checks to be placeable. In case
2088 /// a required invariant load was not identified as (common) invariant we will
2089 /// drop this SCoP. An example for both "common" as well as required invariant
2090 /// loads is given below:
2092 /// for (int i = 1; i < *LB[0]; i++)
2093 /// for (int j = 1; j < *LB[1]; j++)
2094 /// A[i][j] += A[0][0] + (*V);
2096 /// Common inv. loads: V, A[0][0], LB[0], LB[1]
2097 /// Required inv. loads: LB[0], LB[1], (V, if it may alias with A or LB)
2099 void hoistInvariantLoads();
2101 /// Canonicalize arrays with base pointers from the same equivalence class.
2103 /// Some context: in our normal model we assume that each base pointer is
2104 /// related to a single specific memory region, where memory regions
2105 /// associated with different base pointers are disjoint. Consequently we do
2106 /// not need to compute additional data dependences that model possible
2107 /// overlaps of these memory regions. To verify our assumption we compute
2108 /// alias checks that verify that modeled arrays indeed do not overlap. In
2109 /// case an overlap is detected the runtime check fails and we fall back to
2110 /// the original code.
2112 /// In case of arrays where the base pointers are know to be identical,
2113 /// because they are dynamically loaded by accesses that are in the same
2114 /// invariant load equivalence class, such run-time alias check would always
2115 /// be false.
2117 /// This function makes sure that we do not generate consistently failing
2118 /// run-time checks for code that contains distinct arrays with known
2119 /// equivalent base pointers. It identifies for each invariant load
2120 /// equivalence class a single canonical array and canonicalizes all memory
2121 /// accesses that reference arrays that have base pointers that are known to
2122 /// be equal to the base pointer of such a canonical array to this canonical
2123 /// array.
2125 /// We currently do not canonicalize arrays for which certain memory accesses
2126 /// have been hoisted as loop invariant.
2127 void canonicalizeDynamicBasePtrs();
2129 /// Add invariant loads listed in @p InvMAs with the domain of @p Stmt.
2130 void addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs);
2132 /// Create an id for @p Param and store it in the ParameterIds map.
2133 void createParameterId(const SCEV *Param);
2135 /// Build the Context of the Scop.
2136 void buildContext();
2138 /// Add user provided parameter constraints to context (source code).
2139 void addUserAssumptions(AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI,
2140 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
2142 /// Add user provided parameter constraints to context (command line).
2143 void addUserContext();
2145 /// Add the bounds of the parameters to the context.
2146 void addParameterBounds();
2148 /// Simplify the assumed and invalid context.
2149 void simplifyContexts();
2151 /// Get the representing SCEV for @p S if applicable, otherwise @p S.
2153 /// Invariant loads of the same location are put in an equivalence class and
2154 /// only one of them is chosen as a representing element that will be
2155 /// modeled as a parameter. The others have to be normalized, i.e.,
2156 /// replaced by the representing element of their equivalence class, in order
2157 /// to get the correct parameter value, e.g., in the SCEVAffinator.
2159 /// @param S The SCEV to normalize.
2161 /// @return The representing SCEV for invariant loads or @p S if none.
2162 const SCEV *getRepresentingInvariantLoadSCEV(const SCEV *S);
2164 /// Create a new SCoP statement for @p BB.
2166 /// A new statement for @p BB will be created and added to the statement
2167 /// vector
2168 /// and map.
2170 /// @param BB The basic block we build the statement for.
2171 /// @param SurroundingLoop The loop the created statement is contained in.
2172 /// @param Instructions The instructions in the basic block.
2173 void addScopStmt(BasicBlock *BB, Loop *SurroundingLoop,
2174 std::vector<Instruction *> Instructions);
2176 /// Create a new SCoP statement for @p R.
2178 /// A new statement for @p R will be created and added to the statement vector
2179 /// and map.
2181 /// @param R The region we build the statement for.
2182 /// @param SurroundingLoop The loop the created statement is contained in.
2183 void addScopStmt(Region *R, Loop *SurroundingLoop);
2185 /// Update access dimensionalities.
2187 /// When detecting memory accesses different accesses to the same array may
2188 /// have built with different dimensionality, as outer zero-values dimensions
2189 /// may not have been recognized as separate dimensions. This function goes
2190 /// again over all memory accesses and updates their dimensionality to match
2191 /// the dimensionality of the underlying ScopArrayInfo object.
2192 void updateAccessDimensionality();
2194 /// Fold size constants to the right.
2196 /// In case all memory accesses in a given dimension are multiplied with a
2197 /// common constant, we can remove this constant from the individual access
2198 /// functions and move it to the size of the memory access. We do this as this
2199 /// increases the size of the innermost dimension, consequently widens the
2200 /// valid range the array subscript in this dimension can evaluate to, and
2201 /// as a result increases the likelihood that our delinearization is
2202 /// correct.
2204 /// Example:
2206 /// A[][n]
2207 /// S[i,j] -> A[2i][2j+1]
2208 /// S[i,j] -> A[2i][2j]
2210 /// =>
2212 /// A[][2n]
2213 /// S[i,j] -> A[i][2j+1]
2214 /// S[i,j] -> A[i][2j]
2216 /// Constants in outer dimensions can arise when the elements of a parametric
2217 /// multi-dimensional array are not elementary data types, but e.g.,
2218 /// structures.
2219 void foldSizeConstantsToRight();
2221 /// Fold memory accesses to handle parametric offset.
2223 /// As a post-processing step, we 'fold' memory accesses to parametric
2224 /// offsets in the access functions. @see MemoryAccess::foldAccess for
2225 /// details.
2226 void foldAccessRelations();
2228 /// Assume that all memory accesses are within bounds.
2230 /// After we have built a model of all memory accesses, we need to assume
2231 /// that the model we built matches reality -- aka. all modeled memory
2232 /// accesses always remain within bounds. We do this as last step, after
2233 /// all memory accesses have been modeled and canonicalized.
2234 void assumeNoOutOfBounds();
2236 /// Remove statements from the list of scop statements.
2238 /// @param ShouldDelete A function that returns true if the statement passed
2239 /// to it should be deleted.
2240 void removeStmts(std::function<bool(ScopStmt &)> ShouldDelete);
2242 /// Removes @p Stmt from the StmtMap.
2243 void removeFromStmtMap(ScopStmt &Stmt);
2245 /// Removes all statements where the entry block of the statement does not
2246 /// have a corresponding domain in the domain map.
2247 void removeStmtNotInDomainMap();
2249 /// Mark arrays that have memory accesses with FortranArrayDescriptor.
2250 void markFortranArrays();
2252 /// Finalize all access relations.
2254 /// When building up access relations, temporary access relations that
2255 /// correctly represent each individual access are constructed. However, these
2256 /// access relations can be inconsistent or non-optimal when looking at the
2257 /// set of accesses as a whole. This function finalizes the memory accesses
2258 /// and constructs a globally consistent state.
2259 void finalizeAccesses();
2261 /// Construct the schedule of this SCoP.
2263 /// @param LI The LoopInfo for the current function.
2264 void buildSchedule(LoopInfo &LI);
2266 /// A loop stack element to keep track of per-loop information during
2267 /// schedule construction.
2268 typedef struct LoopStackElement {
2269 // The loop for which we keep information.
2270 Loop *L;
2272 // The (possibly incomplete) schedule for this loop.
2273 isl_schedule *Schedule;
2275 // The number of basic blocks in the current loop, for which a schedule has
2276 // already been constructed.
2277 unsigned NumBlocksProcessed;
2279 LoopStackElement(Loop *L, __isl_give isl_schedule *S,
2280 unsigned NumBlocksProcessed)
2281 : L(L), Schedule(S), NumBlocksProcessed(NumBlocksProcessed) {}
2282 } LoopStackElementTy;
2284 /// The loop stack used for schedule construction.
2286 /// The loop stack keeps track of schedule information for a set of nested
2287 /// loops as well as an (optional) 'nullptr' loop that models the outermost
2288 /// schedule dimension. The loops in a loop stack always have a parent-child
2289 /// relation where the loop at position n is the parent of the loop at
2290 /// position n + 1.
2291 typedef SmallVector<LoopStackElementTy, 4> LoopStackTy;
2293 /// Construct schedule information for a given Region and add the
2294 /// derived information to @p LoopStack.
2296 /// Given a Region we derive schedule information for all RegionNodes
2297 /// contained in this region ensuring that the assigned execution times
2298 /// correctly model the existing control flow relations.
2300 /// @param R The region which to process.
2301 /// @param LoopStack A stack of loops that are currently under
2302 /// construction.
2303 /// @param LI The LoopInfo for the current function.
2304 void buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI);
2306 /// Build Schedule for the region node @p RN and add the derived
2307 /// information to @p LoopStack.
2309 /// In case @p RN is a BasicBlock or a non-affine Region, we construct the
2310 /// schedule for this @p RN and also finalize loop schedules in case the
2311 /// current @p RN completes the loop.
2313 /// In case @p RN is a not-non-affine Region, we delegate the construction to
2314 /// buildSchedule(Region *R, ...).
2316 /// @param RN The RegionNode region traversed.
2317 /// @param LoopStack A stack of loops that are currently under
2318 /// construction.
2319 /// @param LI The LoopInfo for the current function.
2320 void buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI);
2322 /// Collect all memory access relations of a given type.
2324 /// @param Predicate A predicate function that returns true if an access is
2325 /// of a given type.
2327 /// @returns The set of memory accesses in the scop that match the predicate.
2328 __isl_give isl_union_map *
2329 getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate);
2331 /// @name Helper functions for printing the Scop.
2333 //@{
2334 void printContext(raw_ostream &OS) const;
2335 void printArrayInfo(raw_ostream &OS) const;
2336 void printStatements(raw_ostream &OS, bool PrintInstructions) const;
2337 void printAliasAssumptions(raw_ostream &OS) const;
2338 //@}
2340 friend class ScopBuilder;
2342 public:
2343 ~Scop();
2345 /// Get the count of copy statements added to this Scop.
2347 /// @return The count of copy statements added to this Scop.
2348 unsigned getCopyStmtsNum() { return CopyStmtsNum; }
2350 /// Create a new copy statement.
2352 /// A new statement will be created and added to the statement vector.
2354 /// @param Stmt The parent statement.
2355 /// @param SourceRel The source location.
2356 /// @param TargetRel The target location.
2357 /// @param Domain The original domain under which the copy statement would
2358 /// be executed.
2359 ScopStmt *addScopStmt(__isl_take isl_map *SourceRel,
2360 __isl_take isl_map *TargetRel,
2361 __isl_take isl_set *Domain);
2363 /// Add the access function to all MemoryAccess objects of the Scop
2364 /// created in this pass.
2365 void addAccessFunction(MemoryAccess *Access) {
2366 AccessFunctions.emplace_back(Access);
2369 /// Add metadata for @p Access.
2370 void addAccessData(MemoryAccess *Access);
2372 /// Remove the metadata stored for @p Access.
2373 void removeAccessData(MemoryAccess *Access);
2375 ScalarEvolution *getSE() const;
2377 /// Get the count of parameters used in this Scop.
2379 /// @return The count of parameters used in this Scop.
2380 size_t getNumParams() const { return Parameters.size(); }
2382 /// Take a list of parameters and add the new ones to the scop.
2383 void addParams(const ParameterSetTy &NewParameters);
2385 /// Return an iterator range containing the scop parameters.
2386 iterator_range<ParameterSetTy::iterator> parameters() const {
2387 return make_range(Parameters.begin(), Parameters.end());
2390 /// Return whether this scop is empty, i.e. contains no statements that
2391 /// could be executed.
2392 bool isEmpty() const { return Stmts.empty(); }
2394 const StringRef getName() const { return name; }
2396 typedef ArrayInfoSetTy::iterator array_iterator;
2397 typedef ArrayInfoSetTy::const_iterator const_array_iterator;
2398 typedef iterator_range<ArrayInfoSetTy::iterator> array_range;
2399 typedef iterator_range<ArrayInfoSetTy::const_iterator> const_array_range;
2401 inline array_iterator array_begin() { return ScopArrayInfoSet.begin(); }
2403 inline array_iterator array_end() { return ScopArrayInfoSet.end(); }
2405 inline const_array_iterator array_begin() const {
2406 return ScopArrayInfoSet.begin();
2409 inline const_array_iterator array_end() const {
2410 return ScopArrayInfoSet.end();
2413 inline array_range arrays() {
2414 return array_range(array_begin(), array_end());
2417 inline const_array_range arrays() const {
2418 return const_array_range(array_begin(), array_end());
2421 /// Return the isl_id that represents a certain parameter.
2423 /// @param Parameter A SCEV that was recognized as a Parameter.
2425 /// @return The corresponding isl_id or NULL otherwise.
2426 __isl_give isl_id *getIdForParam(const SCEV *Parameter);
2428 /// Get the maximum region of this static control part.
2430 /// @return The maximum region of this static control part.
2431 inline const Region &getRegion() const { return R; }
2432 inline Region &getRegion() { return R; }
2434 /// Return the function this SCoP is in.
2435 Function &getFunction() const { return *R.getEntry()->getParent(); }
2437 /// Check if @p L is contained in the SCoP.
2438 bool contains(const Loop *L) const { return R.contains(L); }
2440 /// Check if @p BB is contained in the SCoP.
2441 bool contains(const BasicBlock *BB) const { return R.contains(BB); }
2443 /// Check if @p I is contained in the SCoP.
2444 bool contains(const Instruction *I) const { return R.contains(I); }
2446 /// Return the unique exit block of the SCoP.
2447 BasicBlock *getExit() const { return R.getExit(); }
2449 /// Return the unique exiting block of the SCoP if any.
2450 BasicBlock *getExitingBlock() const { return R.getExitingBlock(); }
2452 /// Return the unique entry block of the SCoP.
2453 BasicBlock *getEntry() const { return R.getEntry(); }
2455 /// Return the unique entering block of the SCoP if any.
2456 BasicBlock *getEnteringBlock() const { return R.getEnteringBlock(); }
2458 /// Return true if @p BB is the exit block of the SCoP.
2459 bool isExit(BasicBlock *BB) const { return getExit() == BB; }
2461 /// Return a range of all basic blocks in the SCoP.
2462 Region::block_range blocks() const { return R.blocks(); }
2464 /// Return true if and only if @p BB dominates the SCoP.
2465 bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const;
2467 /// Get the maximum depth of the loop.
2469 /// @return The maximum depth of the loop.
2470 inline unsigned getMaxLoopDepth() const { return MaxLoopDepth; }
2472 /// Return the invariant equivalence class for @p Val if any.
2473 InvariantEquivClassTy *lookupInvariantEquivClass(Value *Val);
2475 /// Return the set of invariant accesses.
2476 InvariantEquivClassesTy &getInvariantAccesses() {
2477 return InvariantEquivClasses;
2480 /// Check if the scop has any invariant access.
2481 bool hasInvariantAccesses() { return !InvariantEquivClasses.empty(); }
2483 /// Mark the SCoP as optimized by the scheduler.
2484 void markAsOptimized() { IsOptimized = true; }
2486 /// Check if the SCoP has been optimized by the scheduler.
2487 bool isOptimized() const { return IsOptimized; }
2489 /// Mark the SCoP to be skipped by ScopPass passes.
2490 void markAsToBeSkipped() { SkipScop = true; }
2492 /// Check if the SCoP is to be skipped by ScopPass passes.
2493 bool isToBeSkipped() const { return SkipScop; }
2495 /// Return the ID of the Scop
2496 int getID() const { return ID; }
2498 /// Get the name of the entry and exit blocks of this Scop.
2500 /// These along with the function name can uniquely identify a Scop.
2502 /// @return std::pair whose first element is the entry name & second element
2503 /// is the exit name.
2504 std::pair<std::string, std::string> getEntryExitStr() const;
2506 /// Get the name of this Scop.
2507 std::string getNameStr() const;
2509 /// Get the constraint on parameter of this Scop.
2511 /// @return The constraint on parameter of this Scop.
2512 __isl_give isl_set *getContext() const;
2513 __isl_give isl_space *getParamSpace() const;
2515 /// Get the assumed context for this Scop.
2517 /// @return The assumed context of this Scop.
2518 __isl_give isl_set *getAssumedContext() const;
2520 /// Return true if the optimized SCoP can be executed.
2522 /// In addition to the runtime check context this will also utilize the domain
2523 /// constraints to decide it the optimized version can actually be executed.
2525 /// @returns True if the optimized SCoP can be executed.
2526 bool hasFeasibleRuntimeContext() const;
2528 /// Check if the assumption in @p Set is trivial or not.
2530 /// @param Set The relations between parameters that are assumed to hold.
2531 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2532 /// (needed/assumptions) or negative (invalid/restrictions).
2534 /// @returns True if the assumption @p Set is not trivial.
2535 bool isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign);
2537 /// Track and report an assumption.
2539 /// Use 'clang -Rpass-analysis=polly-scops' or 'opt
2540 /// -pass-remarks-analysis=polly-scops' to output the assumptions.
2542 /// @param Kind The assumption kind describing the underlying cause.
2543 /// @param Set The relations between parameters that are assumed to hold.
2544 /// @param Loc The location in the source that caused this assumption.
2545 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2546 /// (needed/assumptions) or negative (invalid/restrictions).
2547 /// @param BB The block in which this assumption was taken. Used to
2548 /// calculate hotness when emitting remark.
2550 /// @returns True if the assumption is not trivial.
2551 bool trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set,
2552 DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB);
2554 /// Add assumptions to assumed context.
2556 /// The assumptions added will be assumed to hold during the execution of the
2557 /// scop. However, as they are generally not statically provable, at code
2558 /// generation time run-time checks will be generated that ensure the
2559 /// assumptions hold.
2561 /// WARNING: We currently exploit in simplifyAssumedContext the knowledge
2562 /// that assumptions do not change the set of statement instances
2563 /// executed.
2565 /// @param Kind The assumption kind describing the underlying cause.
2566 /// @param Set The relations between parameters that are assumed to hold.
2567 /// @param Loc The location in the source that caused this assumption.
2568 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2569 /// (needed/assumptions) or negative (invalid/restrictions).
2570 /// @param BB The block in which this assumption was taken. Used to
2571 /// calculate hotness when emitting remark.
2572 void addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, DebugLoc Loc,
2573 AssumptionSign Sign, BasicBlock *BB);
2575 /// Record an assumption for later addition to the assumed context.
2577 /// This function will add the assumption to the RecordedAssumptions. This
2578 /// collection will be added (@see addAssumption) to the assumed context once
2579 /// all paramaters are known and the context is fully build.
2581 /// @param Kind The assumption kind describing the underlying cause.
2582 /// @param Set The relations between parameters that are assumed to hold.
2583 /// @param Loc The location in the source that caused this assumption.
2584 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2585 /// (needed/assumptions) or negative (invalid/restrictions).
2586 /// @param BB The block in which this assumption was taken. If it is
2587 /// set, the domain of that block will be used to simplify the
2588 /// actual assumption in @p Set once it is added. This is useful
2589 /// if the assumption was created prior to the domain.
2590 void recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set,
2591 DebugLoc Loc, AssumptionSign Sign,
2592 BasicBlock *BB = nullptr);
2594 /// Add all recorded assumptions to the assumed context.
2595 void addRecordedAssumptions();
2597 /// Mark the scop as invalid.
2599 /// This method adds an assumption to the scop that is always invalid. As a
2600 /// result, the scop will not be optimized later on. This function is commonly
2601 /// called when a condition makes it impossible (or too compile time
2602 /// expensive) to process this scop any further.
2604 /// @param Kind The assumption kind describing the underlying cause.
2605 /// @param Loc The location in the source that triggered .
2606 /// @param BB The BasicBlock where it was triggered.
2607 void invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB = nullptr);
2609 /// Get the invalid context for this Scop.
2611 /// @return The invalid context of this Scop.
2612 __isl_give isl_set *getInvalidContext() const;
2614 /// Return true if and only if the InvalidContext is trivial (=empty).
2615 bool hasTrivialInvalidContext() const {
2616 return isl_set_is_empty(InvalidContext);
2619 /// A vector of memory accesses that belong to an alias group.
2620 typedef SmallVector<MemoryAccess *, 4> AliasGroupTy;
2622 /// A vector of alias groups.
2623 typedef SmallVector<Scop::AliasGroupTy, 4> AliasGroupVectorTy;
2625 /// Build the alias checks for this SCoP.
2626 bool buildAliasChecks(AliasAnalysis &AA);
2628 /// Build all alias groups for this SCoP.
2630 /// @returns True if __no__ error occurred, false otherwise.
2631 bool buildAliasGroups(AliasAnalysis &AA);
2633 /// Build alias groups for all memory accesses in the Scop.
2635 /// Using the alias analysis and an alias set tracker we build alias sets
2636 /// for all memory accesses inside the Scop. For each alias set we then map
2637 /// the aliasing pointers back to the memory accesses we know, thus obtain
2638 /// groups of memory accesses which might alias. We also collect the set of
2639 /// arrays through which memory is written.
2641 /// @param AA A reference to the alias analysis.
2643 /// @returns A pair consistent of a vector of alias groups and a set of arrays
2644 /// through which memory is written.
2645 std::tuple<AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>>
2646 buildAliasGroupsForAccesses(AliasAnalysis &AA);
2648 /// Split alias groups by iteration domains.
2650 /// We split each group based on the domains of the minimal/maximal accesses.
2651 /// That means two minimal/maximal accesses are only in a group if their
2652 /// access domains intersect. Otherwise, they are in different groups.
2654 /// @param AliasGroups The alias groups to split
2655 void splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups);
2657 /// Build a given alias group and its access data.
2659 /// @param AliasGroup The alias group to build.
2660 /// @param HasWriteAccess A set of arrays through which memory is not only
2661 /// read, but also written.
2663 /// @returns True if __no__ error occurred, false otherwise.
2664 bool buildAliasGroup(Scop::AliasGroupTy &AliasGroup,
2665 DenseSet<const ScopArrayInfo *> HasWriteAccess);
2667 /// Return all alias groups for this SCoP.
2668 const MinMaxVectorPairVectorTy &getAliasGroups() const {
2669 return MinMaxAliasGroups;
2672 /// Get an isl string representing the context.
2673 std::string getContextStr() const;
2675 /// Get an isl string representing the assumed context.
2676 std::string getAssumedContextStr() const;
2678 /// Get an isl string representing the invalid context.
2679 std::string getInvalidContextStr() const;
2681 /// Return the ScopStmt for the given @p BB or nullptr if there is
2682 /// none.
2683 ScopStmt *getStmtFor(BasicBlock *BB) const;
2685 /// Return the list of ScopStmts that represent the given @p BB.
2686 ArrayRef<ScopStmt *> getStmtListFor(BasicBlock *BB) const;
2688 /// Return the last statement representing @p BB.
2690 /// Of the sequence of statements that represent a @p BB, this is the last one
2691 /// to be executed. It is typically used to determine which instruction to add
2692 /// a MemoryKind::PHI WRITE to. For this purpose, it is not strictly required
2693 /// to be executed last, only that the incoming value is available in it.
2694 ScopStmt *getLastStmtFor(BasicBlock *BB) const;
2696 /// Return the ScopStmts that represents the Region @p R, or nullptr if
2697 /// it is not represented by any statement in this Scop.
2698 ArrayRef<ScopStmt *> getStmtListFor(Region *R) const;
2700 /// Return the ScopStmts that represents @p RN; can return nullptr if
2701 /// the RegionNode is not within the SCoP or has been removed due to
2702 /// simplifications.
2703 ArrayRef<ScopStmt *> getStmtListFor(RegionNode *RN) const;
2705 /// Return the ScopStmt an instruction belongs to, or nullptr if it
2706 /// does not belong to any statement in this Scop.
2707 ScopStmt *getStmtFor(Instruction *Inst) const {
2708 return getStmtFor(Inst->getParent());
2711 /// Return the number of statements in the SCoP.
2712 size_t getSize() const { return Stmts.size(); }
2714 /// @name Statements Iterators
2716 /// These iterators iterate over all statements of this Scop.
2717 //@{
2718 typedef StmtSet::iterator iterator;
2719 typedef StmtSet::const_iterator const_iterator;
2721 iterator begin() { return Stmts.begin(); }
2722 iterator end() { return Stmts.end(); }
2723 const_iterator begin() const { return Stmts.begin(); }
2724 const_iterator end() const { return Stmts.end(); }
2726 typedef StmtSet::reverse_iterator reverse_iterator;
2727 typedef StmtSet::const_reverse_iterator const_reverse_iterator;
2729 reverse_iterator rbegin() { return Stmts.rbegin(); }
2730 reverse_iterator rend() { return Stmts.rend(); }
2731 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
2732 const_reverse_iterator rend() const { return Stmts.rend(); }
2733 //@}
2735 /// Return the set of required invariant loads.
2736 const InvariantLoadsSetTy &getRequiredInvariantLoads() const {
2737 return DC.RequiredILS;
2740 /// Add @p LI to the set of required invariant loads.
2741 void addRequiredInvariantLoad(LoadInst *LI) { DC.RequiredILS.insert(LI); }
2743 /// Return true if and only if @p LI is a required invariant load.
2744 bool isRequiredInvariantLoad(LoadInst *LI) const {
2745 return getRequiredInvariantLoads().count(LI);
2748 /// Return the set of boxed (thus overapproximated) loops.
2749 const BoxedLoopsSetTy &getBoxedLoops() const { return DC.BoxedLoopsSet; }
2751 /// Return true if and only if @p R is a non-affine subregion.
2752 bool isNonAffineSubRegion(const Region *R) {
2753 return DC.NonAffineSubRegionSet.count(R);
2756 const MapInsnToMemAcc &getInsnToMemAccMap() const { return DC.InsnToMemAcc; }
2758 /// Return the (possibly new) ScopArrayInfo object for @p Access.
2760 /// @param ElementType The type of the elements stored in this array.
2761 /// @param Kind The kind of the array info object.
2762 /// @param BaseName The optional name of this memory reference.
2763 ScopArrayInfo *getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType,
2764 ArrayRef<const SCEV *> Sizes,
2765 MemoryKind Kind,
2766 const char *BaseName = nullptr);
2768 /// Create an array and return the corresponding ScopArrayInfo object.
2770 /// @param ElementType The type of the elements stored in this array.
2771 /// @param BaseName The name of this memory reference.
2772 /// @param Sizes The sizes of dimensions.
2773 ScopArrayInfo *createScopArrayInfo(Type *ElementType,
2774 const std::string &BaseName,
2775 const std::vector<unsigned> &Sizes);
2777 /// Return the cached ScopArrayInfo object for @p BasePtr.
2779 /// @param BasePtr The base pointer the object has been stored for.
2780 /// @param Kind The kind of array info object.
2782 /// @returns The ScopArrayInfo pointer or NULL if no such pointer is
2783 /// available.
2784 const ScopArrayInfo *getScopArrayInfoOrNull(Value *BasePtr, MemoryKind Kind);
2786 /// Return the cached ScopArrayInfo object for @p BasePtr.
2788 /// @param BasePtr The base pointer the object has been stored for.
2789 /// @param Kind The kind of array info object.
2791 /// @returns The ScopArrayInfo pointer (may assert if no such pointer is
2792 /// available).
2793 const ScopArrayInfo *getScopArrayInfo(Value *BasePtr, MemoryKind Kind);
2795 /// Invalidate ScopArrayInfo object for base address.
2797 /// @param BasePtr The base pointer of the ScopArrayInfo object to invalidate.
2798 /// @param Kind The Kind of the ScopArrayInfo object.
2799 void invalidateScopArrayInfo(Value *BasePtr, MemoryKind Kind) {
2800 auto It = ScopArrayInfoMap.find(std::make_pair(BasePtr, Kind));
2801 if (It == ScopArrayInfoMap.end())
2802 return;
2803 ScopArrayInfoSet.remove(It->second.get());
2804 ScopArrayInfoMap.erase(It);
2807 void setContext(__isl_take isl_set *NewContext);
2809 /// Align the parameters in the statement to the scop context
2810 void realignParams();
2812 /// Return true if this SCoP can be profitably optimized.
2814 /// @param ScalarsAreUnprofitable Never consider statements with scalar writes
2815 /// as profitably optimizable.
2817 /// @return Whether this SCoP can be profitably optimized.
2818 bool isProfitable(bool ScalarsAreUnprofitable) const;
2820 /// Return true if the SCoP contained at least one error block.
2821 bool hasErrorBlock() const { return HasErrorBlock; }
2823 /// Return true if the underlying region has a single exiting block.
2824 bool hasSingleExitEdge() const { return HasSingleExitEdge; }
2826 /// Print the static control part.
2828 /// @param OS The output stream the static control part is printed to.
2829 /// @param PrintInstructions Whether to print the statement's instructions as
2830 /// well.
2831 void print(raw_ostream &OS, bool PrintInstructions) const;
2833 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2834 /// Print the ScopStmt to stderr.
2835 void dump() const;
2836 #endif
2838 /// Get the isl context of this static control part.
2840 /// @return The isl context of this static control part.
2841 isl_ctx *getIslCtx() const;
2843 /// Directly return the shared_ptr of the context.
2844 const std::shared_ptr<isl_ctx> &getSharedIslCtx() const { return IslCtx; }
2846 /// Compute the isl representation for the SCEV @p E
2848 /// @param E The SCEV that should be translated.
2849 /// @param BB An (optional) basic block in which the isl_pw_aff is computed.
2850 /// SCEVs known to not reference any loops in the SCoP can be
2851 /// passed without a @p BB.
2852 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
2854 /// Note that this function will always return a valid isl_pw_aff. However, if
2855 /// the translation of @p E was deemed to complex the SCoP is invalidated and
2856 /// a dummy value of appropriate dimension is returned. This allows to bail
2857 /// for complex cases without "error handling code" needed on the users side.
2858 __isl_give PWACtx getPwAff(const SCEV *E, BasicBlock *BB = nullptr,
2859 bool NonNegative = false);
2861 /// Compute the isl representation for the SCEV @p E
2863 /// This function is like @see Scop::getPwAff() but strips away the invalid
2864 /// domain part associated with the piecewise affine function.
2865 __isl_give isl_pw_aff *getPwAffOnly(const SCEV *E, BasicBlock *BB = nullptr);
2867 /// Return the domain of @p Stmt.
2869 /// @param Stmt The statement for which the conditions should be returned.
2870 __isl_give isl_set *getDomainConditions(const ScopStmt *Stmt) const;
2872 /// Return the domain of @p BB.
2874 /// @param BB The block for which the conditions should be returned.
2875 __isl_give isl_set *getDomainConditions(BasicBlock *BB) const;
2877 /// Get a union set containing the iteration domains of all statements.
2878 __isl_give isl_union_set *getDomains() const;
2880 /// Get a union map of all may-writes performed in the SCoP.
2881 __isl_give isl_union_map *getMayWrites();
2883 /// Get a union map of all must-writes performed in the SCoP.
2884 __isl_give isl_union_map *getMustWrites();
2886 /// Get a union map of all writes performed in the SCoP.
2887 __isl_give isl_union_map *getWrites();
2889 /// Get a union map of all reads performed in the SCoP.
2890 __isl_give isl_union_map *getReads();
2892 /// Get a union map of all memory accesses performed in the SCoP.
2893 __isl_give isl_union_map *getAccesses();
2895 /// Get the schedule of all the statements in the SCoP.
2897 /// @return The schedule of all the statements in the SCoP, if the schedule of
2898 /// the Scop does not contain extension nodes, and nullptr, otherwise.
2899 __isl_give isl_union_map *getSchedule() const;
2901 /// Get a schedule tree describing the schedule of all statements.
2902 __isl_give isl_schedule *getScheduleTree() const;
2904 /// Update the current schedule
2906 /// NewSchedule The new schedule (given as a flat union-map).
2907 void setSchedule(__isl_take isl_union_map *NewSchedule);
2909 /// Update the current schedule
2911 /// NewSchedule The new schedule (given as schedule tree).
2912 void setScheduleTree(__isl_take isl_schedule *NewSchedule);
2914 /// Intersects the domains of all statements in the SCoP.
2916 /// @return true if a change was made
2917 bool restrictDomains(__isl_take isl_union_set *Domain);
2919 /// Get the depth of a loop relative to the outermost loop in the Scop.
2921 /// This will return
2922 /// 0 if @p L is an outermost loop in the SCoP
2923 /// >0 for other loops in the SCoP
2924 /// -1 if @p L is nullptr or there is no outermost loop in the SCoP
2925 int getRelativeLoopDepth(const Loop *L) const;
2927 /// Find the ScopArrayInfo associated with an isl Id
2928 /// that has name @p Name.
2929 ScopArrayInfo *getArrayInfoByName(const std::string BaseName);
2931 /// Check whether @p Schedule contains extension nodes.
2933 /// @return true if @p Schedule contains extension nodes.
2934 static bool containsExtensionNode(__isl_keep isl_schedule *Schedule);
2936 /// Simplify the SCoP representation.
2938 /// @param AfterHoisting Whether it is called after invariant load hoisting.
2939 /// When true, also removes statements without
2940 /// side-effects.
2941 void simplifySCoP(bool AfterHoisting);
2943 /// Get the next free array index.
2945 /// This function returns a unique index which can be used to identify an
2946 /// array.
2947 long getNextArrayIdx() { return ArrayIdx++; }
2949 /// Get the next free statement index.
2951 /// This function returns a unique index which can be used to identify a
2952 /// statement.
2953 long getNextStmtIdx() { return StmtIdx++; }
2955 /// Return the MemoryAccess that writes an llvm::Value, represented by a
2956 /// ScopArrayInfo.
2958 /// There can be at most one such MemoryAccess per llvm::Value in the SCoP.
2959 /// Zero is possible for read-only values.
2960 MemoryAccess *getValueDef(const ScopArrayInfo *SAI) const;
2962 /// Return all MemoryAccesses that us an llvm::Value, represented by a
2963 /// ScopArrayInfo.
2964 ArrayRef<MemoryAccess *> getValueUses(const ScopArrayInfo *SAI) const;
2966 /// Return the MemoryAccess that represents an llvm::PHINode.
2968 /// ExitPHIs's PHINode is not within the SCoPs. This function returns nullptr
2969 /// for them.
2970 MemoryAccess *getPHIRead(const ScopArrayInfo *SAI) const;
2972 /// Return all MemoryAccesses for all incoming statements of a PHINode,
2973 /// represented by a ScopArrayInfo.
2974 ArrayRef<MemoryAccess *> getPHIIncomings(const ScopArrayInfo *SAI) const;
2977 /// Print Scop scop to raw_ostream O.
2978 raw_ostream &operator<<(raw_ostream &O, const Scop &scop);
2980 /// The legacy pass manager's analysis pass to compute scop information
2981 /// for a region.
2982 class ScopInfoRegionPass : public RegionPass {
2983 /// The Scop pointer which is used to construct a Scop.
2984 std::unique_ptr<Scop> S;
2986 public:
2987 static char ID; // Pass identification, replacement for typeid
2989 ScopInfoRegionPass() : RegionPass(ID) {}
2990 ~ScopInfoRegionPass() {}
2992 /// Build Scop object, the Polly IR of static control
2993 /// part for the current SESE-Region.
2995 /// @return If the current region is a valid for a static control part,
2996 /// return the Polly IR representing this static control part,
2997 /// return null otherwise.
2998 Scop *getScop() { return S.get(); }
2999 const Scop *getScop() const { return S.get(); }
3001 /// Calculate the polyhedral scop information for a given Region.
3002 bool runOnRegion(Region *R, RGPassManager &RGM) override;
3004 void releaseMemory() override { S.reset(); }
3006 void print(raw_ostream &O, const Module *M = nullptr) const override;
3008 void getAnalysisUsage(AnalysisUsage &AU) const override;
3011 class ScopInfo {
3012 public:
3013 using RegionToScopMapTy = DenseMap<Region *, std::unique_ptr<Scop>>;
3014 using iterator = RegionToScopMapTy::iterator;
3015 using const_iterator = RegionToScopMapTy::const_iterator;
3017 private:
3018 /// A map of Region to its Scop object containing
3019 /// Polly IR of static control part.
3020 RegionToScopMapTy RegionToScopMap;
3022 public:
3023 ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE,
3024 LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT,
3025 AssumptionCache &AC);
3027 /// Get the Scop object for the given Region.
3029 /// @return If the given region is the maximal region within a scop, return
3030 /// the scop object. If the given region is a subregion, return a
3031 /// nullptr. Top level region containing the entry block of a function
3032 /// is not considered in the scop creation.
3033 Scop *getScop(Region *R) const {
3034 auto MapIt = RegionToScopMap.find(R);
3035 if (MapIt != RegionToScopMap.end())
3036 return MapIt->second.get();
3037 return nullptr;
3040 iterator begin() { return RegionToScopMap.begin(); }
3041 iterator end() { return RegionToScopMap.end(); }
3042 const_iterator begin() const { return RegionToScopMap.begin(); }
3043 const_iterator end() const { return RegionToScopMap.end(); }
3044 bool empty() const { return RegionToScopMap.empty(); }
3047 struct ScopInfoAnalysis : public AnalysisInfoMixin<ScopInfoAnalysis> {
3048 static AnalysisKey Key;
3049 using Result = ScopInfo;
3050 Result run(Function &, FunctionAnalysisManager &);
3053 struct ScopInfoPrinterPass : public PassInfoMixin<ScopInfoPrinterPass> {
3054 ScopInfoPrinterPass(raw_ostream &O) : Stream(O) {}
3055 PreservedAnalyses run(Function &, FunctionAnalysisManager &);
3056 raw_ostream &Stream;
3059 //===----------------------------------------------------------------------===//
3060 /// The legacy pass manager's analysis pass to compute scop information
3061 /// for the whole function.
3063 /// This pass will maintain a map of the maximal region within a scop to its
3064 /// scop object for all the feasible scops present in a function.
3065 /// This pass is an alternative to the ScopInfoRegionPass in order to avoid a
3066 /// region pass manager.
3067 class ScopInfoWrapperPass : public FunctionPass {
3068 std::unique_ptr<ScopInfo> Result;
3070 public:
3071 ScopInfoWrapperPass() : FunctionPass(ID) {}
3072 ~ScopInfoWrapperPass() = default;
3074 static char ID; // Pass identification, replacement for typeid
3076 ScopInfo *getSI() { return Result.get(); }
3077 const ScopInfo *getSI() const { return Result.get(); }
3079 /// Calculate all the polyhedral scops for a given function.
3080 bool runOnFunction(Function &F) override;
3082 void releaseMemory() override { Result.reset(); }
3084 void print(raw_ostream &O, const Module *M = nullptr) const override;
3086 void getAnalysisUsage(AnalysisUsage &AU) const override;
3089 } // end namespace polly
3091 namespace llvm {
3092 class PassRegistry;
3093 void initializeScopInfoRegionPassPass(llvm::PassRegistry &);
3094 void initializeScopInfoWrapperPassPass(llvm::PassRegistry &);
3095 } // namespace llvm
3097 #endif