[CodeGen] Add flag to code-generate most memory access expressions
[polly-mirror.git] / include / polly / ScopInfo.h
blob696effd509c0bbf99b9c41e6faf46cf88be7c56e
1 //===------ polly/ScopInfo.h -----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Store the polyhedral model representation of a static control flow region,
11 // also called SCoP (Static Control Part).
13 // This representation is shared among several tools in the polyhedral
14 // community, which are e.g. CLooG, Pluto, Loopo, Graphite.
16 //===----------------------------------------------------------------------===//
18 #ifndef POLLY_SCOP_INFO_H
19 #define POLLY_SCOP_INFO_H
21 #include "polly/ScopDetection.h"
22 #include "polly/Support/SCEVAffinator.h"
24 #include "llvm/ADT/MapVector.h"
25 #include "llvm/Analysis/RegionPass.h"
26 #include "isl/aff.h"
27 #include "isl/ctx.h"
28 #include "isl/set.h"
30 #include <deque>
31 #include <forward_list>
33 using namespace llvm;
35 namespace llvm {
36 class AssumptionCache;
37 class Loop;
38 class LoopInfo;
39 class PHINode;
40 class ScalarEvolution;
41 class SCEV;
42 class SCEVAddRecExpr;
43 class Type;
44 } // namespace llvm
46 struct isl_ctx;
47 struct isl_map;
48 struct isl_basic_map;
49 struct isl_id;
50 struct isl_set;
51 struct isl_union_set;
52 struct isl_union_map;
53 struct isl_space;
54 struct isl_ast_build;
55 struct isl_constraint;
56 struct isl_pw_aff;
57 struct isl_pw_multi_aff;
58 struct isl_schedule;
60 namespace polly {
62 class MemoryAccess;
63 class Scop;
64 class ScopStmt;
65 class ScopBuilder;
67 //===---------------------------------------------------------------------===//
69 /// Enumeration of assumptions Polly can take.
70 enum AssumptionKind {
71 ALIASING,
72 INBOUNDS,
73 WRAPPING,
74 UNSIGNED,
75 PROFITABLE,
76 ERRORBLOCK,
77 COMPLEXITY,
78 INFINITELOOP,
79 INVARIANTLOAD,
80 DELINEARIZATION,
83 /// Enum to distinguish between assumptions and restrictions.
84 enum AssumptionSign { AS_ASSUMPTION, AS_RESTRICTION };
86 /// Maps from a loop to the affine function expressing its backedge taken count.
87 /// The backedge taken count already enough to express iteration domain as we
88 /// only allow loops with canonical induction variable.
89 /// A canonical induction variable is:
90 /// an integer recurrence that starts at 0 and increments by one each time
91 /// through the loop.
92 typedef std::map<const Loop *, const SCEV *> LoopBoundMapType;
94 typedef std::vector<std::unique_ptr<MemoryAccess>> AccFuncVector;
96 /// A class to store information about arrays in the SCoP.
97 ///
98 /// Objects are accessible via the ScoP, MemoryAccess or the id associated with
99 /// the MemoryAccess access function.
101 class ScopArrayInfo {
102 public:
103 /// The kind of a ScopArrayInfo memory object.
105 /// We distinguish between arrays and various scalar memory objects. We use
106 /// the term ``array'' to describe memory objects that consist of a set of
107 /// individual data elements arranged in a multi-dimensional grid. A scalar
108 /// memory object describes an individual data element and is used to model
109 /// the definition and uses of llvm::Values.
111 /// The polyhedral model does traditionally not reason about SSA values. To
112 /// reason about llvm::Values we model them "as if" they were zero-dimensional
113 /// memory objects, even though they were not actually allocated in (main)
114 /// memory. Memory for such objects is only alloca[ed] at CodeGeneration
115 /// time. To relate the memory slots used during code generation with the
116 /// llvm::Values they belong to the new names for these corresponding stack
117 /// slots are derived by appending suffixes (currently ".s2a" and ".phiops")
118 /// to the name of the original llvm::Value. To describe how def/uses are
119 /// modeled exactly we use these suffixes here as well.
121 /// There are currently four different kinds of memory objects:
122 enum MemoryKind {
123 /// MK_Array: Models a one or multi-dimensional array
125 /// A memory object that can be described by a multi-dimensional array.
126 /// Memory objects of this type are used to model actual multi-dimensional
127 /// arrays as they exist in LLVM-IR, but they are also used to describe
128 /// other objects:
129 /// - A single data element allocated on the stack using 'alloca' is
130 /// modeled as a one-dimensional, single-element array.
131 /// - A single data element allocated as a global variable is modeled as
132 /// one-dimensional, single-element array.
133 /// - Certain multi-dimensional arrays with variable size, which in
134 /// LLVM-IR are commonly expressed as a single-dimensional access with a
135 /// complicated access function, are modeled as multi-dimensional
136 /// memory objects (grep for "delinearization").
137 MK_Array,
139 /// MK_Value: Models an llvm::Value
141 /// Memory objects of type MK_Value are used to model the data flow
142 /// induced by llvm::Values. For each llvm::Value that is used across
143 /// BasicBocks one ScopArrayInfo object is created. A single memory WRITE
144 /// stores the llvm::Value at its definition into the memory object and at
145 /// each use of the llvm::Value (ignoring trivial intra-block uses) a
146 /// corresponding READ is added. For instance, the use/def chain of a
147 /// llvm::Value %V depicted below
148 /// ______________________
149 /// |DefBB: |
150 /// | %V = float op ... |
151 /// ----------------------
152 /// | |
153 /// _________________ _________________
154 /// |UseBB1: | |UseBB2: |
155 /// | use float %V | | use float %V |
156 /// ----------------- -----------------
158 /// is modeled as if the following memory accesses occured:
160 /// __________________________
161 /// |entry: |
162 /// | %V.s2a = alloca float |
163 /// --------------------------
164 /// |
165 /// ___________________________________
166 /// |DefBB: |
167 /// | store %float %V, float* %V.s2a |
168 /// -----------------------------------
169 /// | |
170 /// ____________________________________ ___________________________________
171 /// |UseBB1: | |UseBB2: |
172 /// | %V.reload1 = load float* %V.s2a | | %V.reload2 = load float* %V.s2a|
173 /// | use float %V.reload1 | | use float %V.reload2 |
174 /// ------------------------------------ -----------------------------------
176 MK_Value,
178 /// MK_PHI: Models PHI nodes within the SCoP
180 /// Besides the MK_Value memory object used to model the normal
181 /// llvm::Value dependences described above, PHI nodes require an additional
182 /// memory object of type MK_PHI to describe the forwarding of values to
183 /// the PHI node.
185 /// As an example, a PHIInst instructions
187 /// %PHI = phi float [ %Val1, %IncomingBlock1 ], [ %Val2, %IncomingBlock2 ]
189 /// is modeled as if the accesses occured this way:
191 /// _______________________________
192 /// |entry: |
193 /// | %PHI.phiops = alloca float |
194 /// -------------------------------
195 /// | |
196 /// __________________________________ __________________________________
197 /// |IncomingBlock1: | |IncomingBlock2: |
198 /// | ... | | ... |
199 /// | store float %Val1 %PHI.phiops | | store float %Val2 %PHI.phiops |
200 /// | br label % JoinBlock | | br label %JoinBlock |
201 /// ---------------------------------- ----------------------------------
202 /// \ /
203 /// \ /
204 /// _________________________________________
205 /// |JoinBlock: |
206 /// | %PHI = load float, float* PHI.phiops |
207 /// -----------------------------------------
209 /// Note that there can also be a scalar write access for %PHI if used in a
210 /// different BasicBlock, i.e. there can be a memory object %PHI.phiops as
211 /// well as a memory object %PHI.s2a.
212 MK_PHI,
214 /// MK_ExitPHI: Models PHI nodes in the SCoP's exit block
216 /// For PHI nodes in the Scop's exit block a special memory object kind is
217 /// used. The modeling used is identical to MK_PHI, with the exception
218 /// that there are no READs from these memory objects. The PHINode's
219 /// llvm::Value is treated as a value escaping the SCoP. WRITE accesses
220 /// write directly to the escaping value's ".s2a" alloca.
221 MK_ExitPHI
224 /// Construct a ScopArrayInfo object.
226 /// @param BasePtr The array base pointer.
227 /// @param ElementType The type of the elements stored in the array.
228 /// @param IslCtx The isl context used to create the base pointer id.
229 /// @param DimensionSizes A vector containing the size of each dimension.
230 /// @param Kind The kind of the array object.
231 /// @param DL The data layout of the module.
232 /// @param S The scop this array object belongs to.
233 /// @param BaseName The optional name of this memory reference.
234 ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *IslCtx,
235 ArrayRef<const SCEV *> DimensionSizes, enum MemoryKind Kind,
236 const DataLayout &DL, Scop *S, const char *BaseName = nullptr);
238 /// Update the element type of the ScopArrayInfo object.
240 /// Memory accesses referencing this ScopArrayInfo object may use
241 /// different element sizes. This function ensures the canonical element type
242 /// stored is small enough to model accesses to the current element type as
243 /// well as to @p NewElementType.
245 /// @param NewElementType An element type that is used to access this array.
246 void updateElementType(Type *NewElementType);
248 /// Update the sizes of the ScopArrayInfo object.
250 /// A ScopArrayInfo object may be created without all outer dimensions being
251 /// available. This function is called when new memory accesses are added for
252 /// this ScopArrayInfo object. It verifies that sizes are compatible and adds
253 /// additional outer array dimensions, if needed.
255 /// @param Sizes A vector of array sizes where the rightmost array
256 /// sizes need to match the innermost array sizes already
257 /// defined in SAI.
258 bool updateSizes(ArrayRef<const SCEV *> Sizes);
260 /// Destructor to free the isl id of the base pointer.
261 ~ScopArrayInfo();
263 /// Set the base pointer to @p BP.
264 void setBasePtr(Value *BP) { BasePtr = BP; }
266 /// Return the base pointer.
267 Value *getBasePtr() const { return BasePtr; }
269 /// For indirect accesses return the origin SAI of the BP, else null.
270 const ScopArrayInfo *getBasePtrOriginSAI() const { return BasePtrOriginSAI; }
272 /// The set of derived indirect SAIs for this origin SAI.
273 const SmallSetVector<ScopArrayInfo *, 2> &getDerivedSAIs() const {
274 return DerivedSAIs;
277 /// Return the number of dimensions.
278 unsigned getNumberOfDimensions() const {
279 if (Kind == MK_PHI || Kind == MK_ExitPHI || Kind == MK_Value)
280 return 0;
281 return DimensionSizes.size();
284 /// Return the size of dimension @p dim as SCEV*.
286 // Scalars do not have array dimensions and the first dimension of
287 // a (possibly multi-dimensional) array also does not carry any size
288 // information, in case the array is not newly created.
289 const SCEV *getDimensionSize(unsigned Dim) const {
290 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
291 return DimensionSizes[Dim];
294 /// Return the size of dimension @p dim as isl_pw_aff.
296 // Scalars do not have array dimensions and the first dimension of
297 // a (possibly multi-dimensional) array also does not carry any size
298 // information, in case the array is not newly created.
299 __isl_give isl_pw_aff *getDimensionSizePw(unsigned Dim) const {
300 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
301 return isl_pw_aff_copy(DimensionSizesPw[Dim]);
304 /// Get the canonical element type of this array.
306 /// @returns The canonical element type of this array.
307 Type *getElementType() const { return ElementType; }
309 /// Get element size in bytes.
310 int getElemSizeInBytes() const;
312 /// Get the name of this memory reference.
313 std::string getName() const;
315 /// Return the isl id for the base pointer.
316 __isl_give isl_id *getBasePtrId() const;
318 /// Return what kind of memory this represents.
319 enum MemoryKind getKind() const { return Kind; }
321 /// Is this array info modeling an llvm::Value?
322 bool isValueKind() const { return Kind == MK_Value; }
324 /// Is this array info modeling special PHI node memory?
326 /// During code generation of PHI nodes, there is a need for two kinds of
327 /// virtual storage. The normal one as it is used for all scalar dependences,
328 /// where the result of the PHI node is stored and later loaded from as well
329 /// as a second one where the incoming values of the PHI nodes are stored
330 /// into and reloaded when the PHI is executed. As both memories use the
331 /// original PHI node as virtual base pointer, we have this additional
332 /// attribute to distinguish the PHI node specific array modeling from the
333 /// normal scalar array modeling.
334 bool isPHIKind() const { return Kind == MK_PHI; }
336 /// Is this array info modeling an MK_ExitPHI?
337 bool isExitPHIKind() const { return Kind == MK_ExitPHI; }
339 /// Is this array info modeling an array?
340 bool isArrayKind() const { return Kind == MK_Array; }
342 /// Dump a readable representation to stderr.
343 void dump() const;
345 /// Print a readable representation to @p OS.
347 /// @param SizeAsPwAff Print the size as isl_pw_aff
348 void print(raw_ostream &OS, bool SizeAsPwAff = false) const;
350 /// Access the ScopArrayInfo associated with an access function.
351 static const ScopArrayInfo *
352 getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA);
354 /// Access the ScopArrayInfo associated with an isl Id.
355 static const ScopArrayInfo *getFromId(__isl_take isl_id *Id);
357 /// Get the space of this array access.
358 __isl_give isl_space *getSpace() const;
360 /// If the array is read only
361 bool isReadOnly();
363 private:
364 void addDerivedSAI(ScopArrayInfo *DerivedSAI) {
365 DerivedSAIs.insert(DerivedSAI);
368 /// For indirect accesses this is the SAI of the BP origin.
369 const ScopArrayInfo *BasePtrOriginSAI;
371 /// For origin SAIs the set of derived indirect SAIs.
372 SmallSetVector<ScopArrayInfo *, 2> DerivedSAIs;
374 /// The base pointer.
375 AssertingVH<Value> BasePtr;
377 /// The canonical element type of this array.
379 /// The canonical element type describes the minimal accessible element in
380 /// this array. Not all elements accessed, need to be of the very same type,
381 /// but the allocation size of the type of the elements loaded/stored from/to
382 /// this array needs to be a multiple of the allocation size of the canonical
383 /// type.
384 Type *ElementType;
386 /// The isl id for the base pointer.
387 isl_id *Id;
389 /// The sizes of each dimension as SCEV*.
390 SmallVector<const SCEV *, 4> DimensionSizes;
392 /// The sizes of each dimension as isl_pw_aff.
393 SmallVector<isl_pw_aff *, 4> DimensionSizesPw;
395 /// The type of this scop array info object.
397 /// We distinguish between SCALAR, PHI and ARRAY objects.
398 enum MemoryKind Kind;
400 /// The data layout of the module.
401 const DataLayout &DL;
403 /// The scop this SAI object belongs to.
404 Scop &S;
407 /// Represent memory accesses in statements.
408 class MemoryAccess {
409 friend class Scop;
410 friend class ScopStmt;
412 public:
413 /// The access type of a memory access
415 /// There are three kind of access types:
417 /// * A read access
419 /// A certain set of memory locations are read and may be used for internal
420 /// calculations.
422 /// * A must-write access
424 /// A certain set of memory locations is definitely written. The old value is
425 /// replaced by a newly calculated value. The old value is not read or used at
426 /// all.
428 /// * A may-write access
430 /// A certain set of memory locations may be written. The memory location may
431 /// contain a new value if there is actually a write or the old value may
432 /// remain, if no write happens.
433 enum AccessType {
434 READ = 0x1,
435 MUST_WRITE = 0x2,
436 MAY_WRITE = 0x3,
439 /// Reduction access type
441 /// Commutative and associative binary operations suitable for reductions
442 enum ReductionType {
443 RT_NONE, ///< Indicate no reduction at all
444 RT_ADD, ///< Addition
445 RT_MUL, ///< Multiplication
446 RT_BOR, ///< Bitwise Or
447 RT_BXOR, ///< Bitwise XOr
448 RT_BAND, ///< Bitwise And
451 private:
452 MemoryAccess(const MemoryAccess &) = delete;
453 const MemoryAccess &operator=(const MemoryAccess &) = delete;
455 /// A unique identifier for this memory access.
457 /// The identifier is unique between all memory accesses belonging to the same
458 /// scop statement.
459 isl_id *Id;
461 /// What is modeled by this MemoryAccess.
462 /// @see ScopArrayInfo::MemoryKind
463 ScopArrayInfo::MemoryKind Kind;
465 /// Whether it a reading or writing access, and if writing, whether it
466 /// is conditional (MAY_WRITE).
467 enum AccessType AccType;
469 /// Reduction type for reduction like accesses, RT_NONE otherwise
471 /// An access is reduction like if it is part of a load-store chain in which
472 /// both access the same memory location (use the same LLVM-IR value
473 /// as pointer reference). Furthermore, between the load and the store there
474 /// is exactly one binary operator which is known to be associative and
475 /// commutative.
477 /// TODO:
479 /// We can later lift the constraint that the same LLVM-IR value defines the
480 /// memory location to handle scops such as the following:
482 /// for i
483 /// for j
484 /// sum[i+j] = sum[i] + 3;
486 /// Here not all iterations access the same memory location, but iterations
487 /// for which j = 0 holds do. After lifting the equality check in ScopBuilder,
488 /// subsequent transformations do not only need check if a statement is
489 /// reduction like, but they also need to verify that that the reduction
490 /// property is only exploited for statement instances that load from and
491 /// store to the same data location. Doing so at dependence analysis time
492 /// could allow us to handle the above example.
493 ReductionType RedType = RT_NONE;
495 /// Parent ScopStmt of this access.
496 ScopStmt *Statement;
498 /// The domain under which this access is not modeled precisely.
500 /// The invalid domain for an access describes all parameter combinations
501 /// under which the statement looks to be executed but is in fact not because
502 /// some assumption/restriction makes the access invalid.
503 isl_set *InvalidDomain;
505 // Properties describing the accessed array.
506 // TODO: It might be possible to move them to ScopArrayInfo.
507 // @{
509 /// The base address (e.g., A for A[i+j]).
511 /// The #BaseAddr of a memory access of kind MK_Array is the base pointer
512 /// of the memory access.
513 /// The #BaseAddr of a memory access of kind MK_PHI or MK_ExitPHI is the
514 /// PHI node itself.
515 /// The #BaseAddr of a memory access of kind MK_Value is the instruction
516 /// defining the value.
517 AssertingVH<Value> BaseAddr;
519 /// An unique name of the accessed array.
520 std::string BaseName;
522 /// Type a single array element wrt. this access.
523 Type *ElementType;
525 /// Size of each dimension of the accessed array.
526 SmallVector<const SCEV *, 4> Sizes;
527 // @}
529 // Properties describing the accessed element.
530 // @{
532 /// The access instruction of this memory access.
534 /// For memory accesses of kind MK_Array the access instruction is the
535 /// Load or Store instruction performing the access.
537 /// For memory accesses of kind MK_PHI or MK_ExitPHI the access
538 /// instruction of a load access is the PHI instruction. The access
539 /// instruction of a PHI-store is the incoming's block's terminator
540 /// instruction.
542 /// For memory accesses of kind MK_Value the access instruction of a load
543 /// access is nullptr because generally there can be multiple instructions in
544 /// the statement using the same llvm::Value. The access instruction of a
545 /// write access is the instruction that defines the llvm::Value.
546 Instruction *AccessInstruction;
548 /// Incoming block and value of a PHINode.
549 SmallVector<std::pair<BasicBlock *, Value *>, 4> Incoming;
551 /// The value associated with this memory access.
553 /// - For array memory accesses (MK_Array) it is the loaded result or the
554 /// stored value. If the access instruction is a memory intrinsic it
555 /// the access value is also the memory intrinsic.
556 /// - For accesses of kind MK_Value it is the access instruction itself.
557 /// - For accesses of kind MK_PHI or MK_ExitPHI it is the PHI node itself
558 /// (for both, READ and WRITE accesses).
560 AssertingVH<Value> AccessValue;
562 /// Are all the subscripts affine expression?
563 bool IsAffine;
565 /// Subscript expression for each dimension.
566 SmallVector<const SCEV *, 4> Subscripts;
568 /// Relation from statement instances to the accessed array elements.
570 /// In the common case this relation is a function that maps a set of loop
571 /// indices to the memory address from which a value is loaded/stored:
573 /// for i
574 /// for j
575 /// S: A[i + 3 j] = ...
577 /// => { S[i,j] -> A[i + 3j] }
579 /// In case the exact access function is not known, the access relation may
580 /// also be a one to all mapping { S[i,j] -> A[o] } describing that any
581 /// element accessible through A might be accessed.
583 /// In case of an access to a larger element belonging to an array that also
584 /// contains smaller elements, the access relation models the larger access
585 /// with multiple smaller accesses of the size of the minimal array element
586 /// type:
588 /// short *A;
590 /// for i
591 /// S: A[i] = *((double*)&A[4 * i]);
593 /// => { S[i] -> A[i]; S[i] -> A[o] : 4i <= o <= 4i + 3 }
594 isl_map *AccessRelation;
596 /// Updated access relation read from JSCOP file.
597 isl_map *NewAccessRelation;
598 // @}
600 __isl_give isl_basic_map *createBasicAccessMap(ScopStmt *Statement);
602 void assumeNoOutOfBound();
604 /// Compute bounds on an over approximated access relation.
606 /// @param ElementSize The size of one element accessed.
607 void computeBoundsOnAccessRelation(unsigned ElementSize);
609 /// Get the original access function as read from IR.
610 __isl_give isl_map *getOriginalAccessRelation() const;
612 /// Return the space in which the access relation lives in.
613 __isl_give isl_space *getOriginalAccessRelationSpace() const;
615 /// Get the new access function imported or set by a pass
616 __isl_give isl_map *getNewAccessRelation() const;
618 /// Fold the memory access to consider parameteric offsets
620 /// To recover memory accesses with array size parameters in the subscript
621 /// expression we post-process the delinearization results.
623 /// We would normally recover from an access A[exp0(i) * N + exp1(i)] into an
624 /// array A[][N] the 2D access A[exp0(i)][exp1(i)]. However, another valid
625 /// delinearization is A[exp0(i) - 1][exp1(i) + N] which - depending on the
626 /// range of exp1(i) - may be preferrable. Specifically, for cases where we
627 /// know exp1(i) is negative, we want to choose the latter expression.
629 /// As we commonly do not have any information about the range of exp1(i),
630 /// we do not choose one of the two options, but instead create a piecewise
631 /// access function that adds the (-1, N) offsets as soon as exp1(i) becomes
632 /// negative. For a 2D array such an access function is created by applying
633 /// the piecewise map:
635 /// [i,j] -> [i, j] : j >= 0
636 /// [i,j] -> [i-1, j+N] : j < 0
638 /// We can generalize this mapping to arbitrary dimensions by applying this
639 /// piecewise mapping pairwise from the rightmost to the leftmost access
640 /// dimension. It would also be possible to cover a wider range by introducing
641 /// more cases and adding multiple of Ns to these cases. However, this has
642 /// not yet been necessary.
643 /// The introduction of different cases necessarily complicates the memory
644 /// access function, but cases that can be statically proven to not happen
645 /// will be eliminated later on.
646 __isl_give isl_map *foldAccess(__isl_take isl_map *AccessRelation,
647 ScopStmt *Statement);
649 /// Create the access relation for the underlying memory intrinsic.
650 void buildMemIntrinsicAccessRelation();
652 /// Assemble the access relation from all available information.
654 /// In particular, used the information passes in the constructor and the
655 /// parent ScopStmt set by setStatment().
657 /// @param SAI Info object for the accessed array.
658 void buildAccessRelation(const ScopArrayInfo *SAI);
660 /// Carry index overflows of dimensions with constant size to the next higher
661 /// dimension.
663 /// For dimensions that have constant size, modulo the index by the size and
664 /// add up the carry (floored division) to the next higher dimension. This is
665 /// how overflow is defined in row-major order.
666 /// It happens e.g. when ScalarEvolution computes the offset to the base
667 /// pointer and would algebraically sum up all lower dimensions' indices of
668 /// constant size.
670 /// Example:
671 /// float (*A)[4];
672 /// A[1][6] -> A[2][2]
673 void wrapConstantDimensions();
675 public:
676 /// Create a new MemoryAccess.
678 /// @param Stmt The parent statement.
679 /// @param AccessInst The instruction doing the access.
680 /// @param BaseAddr The accessed array's address.
681 /// @param ElemType The type of the accessed array elements.
682 /// @param AccType Whether read or write access.
683 /// @param IsAffine Whether the subscripts are affine expressions.
684 /// @param Kind The kind of memory accessed.
685 /// @param Subscripts Subscipt expressions
686 /// @param Sizes Dimension lengths of the accessed array.
687 /// @param BaseName Name of the acessed array.
688 MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, AccessType AccType,
689 Value *BaseAddress, Type *ElemType, bool Affine,
690 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
691 Value *AccessValue, ScopArrayInfo::MemoryKind Kind,
692 StringRef BaseName);
694 /// Create a new MemoryAccess that corresponds to @p AccRel.
696 /// Along with @p Stmt and @p AccType it uses information about dimension
697 /// lengths of the accessed array, the type of the accessed array elements,
698 /// the name of the accessed array that is derived from the object accessible
699 /// via @p AccRel.
701 /// @param Stmt The parent statement.
702 /// @param AccType Whether read or write access.
703 /// @param AccRel The access relation that describes the memory access.
704 MemoryAccess(ScopStmt *Stmt, AccessType AccType, __isl_take isl_map *AccRel);
706 ~MemoryAccess();
708 /// Add a new incoming block/value pairs for this PHI/ExitPHI access.
710 /// @param IncomingBlock The PHI's incoming block.
711 /// @param IncomingValue The value when reacing the PHI from the @p
712 /// IncomingBlock.
713 void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue) {
714 assert(!isRead());
715 assert(isAnyPHIKind());
716 Incoming.emplace_back(std::make_pair(IncomingBlock, IncomingValue));
719 /// Return the list of possible PHI/ExitPHI values.
721 /// After code generation moves some PHIs around during region simplification,
722 /// we cannot reliably locate the original PHI node and its incoming values
723 /// anymore. For this reason we remember these explicitly for all PHI-kind
724 /// accesses.
725 ArrayRef<std::pair<BasicBlock *, Value *>> getIncoming() const {
726 assert(isAnyPHIKind());
727 return Incoming;
730 /// Get the type of a memory access.
731 enum AccessType getType() { return AccType; }
733 /// Is this a reduction like access?
734 bool isReductionLike() const { return RedType != RT_NONE; }
736 /// Is this a read memory access?
737 bool isRead() const { return AccType == MemoryAccess::READ; }
739 /// Is this a must-write memory access?
740 bool isMustWrite() const { return AccType == MemoryAccess::MUST_WRITE; }
742 /// Is this a may-write memory access?
743 bool isMayWrite() const { return AccType == MemoryAccess::MAY_WRITE; }
745 /// Is this a write memory access?
746 bool isWrite() const { return isMustWrite() || isMayWrite(); }
748 /// Is this a memory intrinsic access (memcpy, memset, memmove)?
749 bool isMemoryIntrinsic() const {
750 return isa<MemIntrinsic>(getAccessInstruction());
753 /// Check if a new access relation was imported or set by a pass.
754 bool hasNewAccessRelation() const { return NewAccessRelation; }
756 /// Return the newest access relation of this access.
758 /// There are two possibilities:
759 /// 1) The original access relation read from the LLVM-IR.
760 /// 2) A new access relation imported from a json file or set by another
761 /// pass (e.g., for privatization).
763 /// As 2) is by construction "newer" than 1) we return the new access
764 /// relation if present.
766 __isl_give isl_map *getLatestAccessRelation() const {
767 return hasNewAccessRelation() ? getNewAccessRelation()
768 : getOriginalAccessRelation();
771 /// Old name of getLatestAccessRelation().
772 __isl_give isl_map *getAccessRelation() const {
773 return getLatestAccessRelation();
776 /// Get an isl map describing the memory address accessed.
778 /// In most cases the memory address accessed is well described by the access
779 /// relation obtained with getAccessRelation. However, in case of arrays
780 /// accessed with types of different size the access relation maps one access
781 /// to multiple smaller address locations. This method returns an isl map that
782 /// relates each dynamic statement instance to the unique memory location
783 /// that is loaded from / stored to.
785 /// For an access relation { S[i] -> A[o] : 4i <= o <= 4i + 3 } this method
786 /// will return the address function { S[i] -> A[4i] }.
788 /// @returns The address function for this memory access.
789 __isl_give isl_map *getAddressFunction() const;
791 /// Return the access relation after the schedule was applied.
792 __isl_give isl_pw_multi_aff *
793 applyScheduleToAccessRelation(__isl_take isl_union_map *Schedule) const;
795 /// Get an isl string representing the access function read from IR.
796 std::string getOriginalAccessRelationStr() const;
798 /// Get an isl string representing a new access function, if available.
799 std::string getNewAccessRelationStr() const;
801 /// Get the base address of this access (e.g. A for A[i+j]) when
802 /// detected.
803 Value *getOriginalBaseAddr() const {
804 assert(!getOriginalScopArrayInfo() /* may noy yet be initialized */ ||
805 getOriginalScopArrayInfo()->getBasePtr() == BaseAddr);
806 return BaseAddr;
809 /// Get the base address of this access (e.g. A for A[i+j]) after a
810 /// potential change by setNewAccessRelation().
811 Value *getLatestBaseAddr() const {
812 return getLatestScopArrayInfo()->getBasePtr();
815 /// Old name for getOriginalBaseAddr().
816 Value *getBaseAddr() const { return getOriginalBaseAddr(); }
818 /// Get the detection-time base array isl_id for this access.
819 __isl_give isl_id *getOriginalArrayId() const;
821 /// Get the base array isl_id for this access, modifiable through
822 /// setNewAccessRelation().
823 __isl_give isl_id *getLatestArrayId() const;
825 /// Old name of getOriginalArrayId().
826 __isl_give isl_id *getArrayId() const { return getOriginalArrayId(); }
828 /// Get the detection-time ScopArrayInfo object for the base address.
829 const ScopArrayInfo *getOriginalScopArrayInfo() const;
831 /// Get the ScopArrayInfo object for the base address, or the one set
832 /// by setNewAccessRelation().
833 const ScopArrayInfo *getLatestScopArrayInfo() const;
835 /// Legacy name of getOriginalScopArrayInfo().
836 const ScopArrayInfo *getScopArrayInfo() const {
837 return getOriginalScopArrayInfo();
840 /// Return a string representation of the access's reduction type.
841 const std::string getReductionOperatorStr() const;
843 /// Return a string representation of the reduction type @p RT.
844 static const std::string getReductionOperatorStr(ReductionType RT);
846 const std::string &getBaseName() const { return BaseName; }
848 /// Return the element type of the accessed array wrt. this access.
849 Type *getElementType() const { return ElementType; }
851 /// Return the access value of this memory access.
852 Value *getAccessValue() const { return AccessValue; }
854 /// Return the access instruction of this memory access.
855 Instruction *getAccessInstruction() const { return AccessInstruction; }
857 /// Return the number of access function subscript.
858 unsigned getNumSubscripts() const { return Subscripts.size(); }
860 /// Return the access function subscript in the dimension @p Dim.
861 const SCEV *getSubscript(unsigned Dim) const { return Subscripts[Dim]; }
863 /// Compute the isl representation for the SCEV @p E wrt. this access.
865 /// Note that this function will also adjust the invalid context accordingly.
866 __isl_give isl_pw_aff *getPwAff(const SCEV *E);
868 /// Get the invalid domain for this access.
869 __isl_give isl_set *getInvalidDomain() const {
870 return isl_set_copy(InvalidDomain);
873 /// Get the invalid context for this access.
874 __isl_give isl_set *getInvalidContext() const {
875 return isl_set_params(getInvalidDomain());
878 /// Get the stride of this memory access in the specified Schedule. Schedule
879 /// is a map from the statement to a schedule where the innermost dimension is
880 /// the dimension of the innermost loop containing the statement.
881 __isl_give isl_set *getStride(__isl_take const isl_map *Schedule) const;
883 /// Is the stride of the access equal to a certain width? Schedule is a map
884 /// from the statement to a schedule where the innermost dimension is the
885 /// dimension of the innermost loop containing the statement.
886 bool isStrideX(__isl_take const isl_map *Schedule, int StrideWidth) const;
888 /// Is consecutive memory accessed for a given statement instance set?
889 /// Schedule is a map from the statement to a schedule where the innermost
890 /// dimension is the dimension of the innermost loop containing the
891 /// statement.
892 bool isStrideOne(__isl_take const isl_map *Schedule) const;
894 /// Is always the same memory accessed for a given statement instance set?
895 /// Schedule is a map from the statement to a schedule where the innermost
896 /// dimension is the dimension of the innermost loop containing the
897 /// statement.
898 bool isStrideZero(__isl_take const isl_map *Schedule) const;
900 /// Return the kind when this access was first detected.
901 ScopArrayInfo::MemoryKind getOriginalKind() const {
902 assert(!getOriginalScopArrayInfo() /* not yet initialized */ ||
903 getOriginalScopArrayInfo()->getKind() == Kind);
904 return Kind;
907 /// Return the kind considering a potential setNewAccessRelation.
908 ScopArrayInfo::MemoryKind getLatestKind() const {
909 return getLatestScopArrayInfo()->getKind();
912 /// Whether this is an access of an explicit load or store in the IR.
913 bool isOriginalArrayKind() const {
914 return getOriginalKind() == ScopArrayInfo::MK_Array;
917 /// Whether storage memory is either an custom .s2a/.phiops alloca
918 /// (false) or an existing pointer into an array (true).
919 bool isLatestArrayKind() const {
920 return getLatestKind() == ScopArrayInfo::MK_Array;
923 /// Old name of isOriginalArrayKind.
924 bool isArrayKind() const { return isOriginalArrayKind(); }
926 /// Whether this access is an array to a scalar memory object, without
927 /// considering changes by setNewAccessRelation.
929 /// Scalar accesses are accesses to MK_Value, MK_PHI or MK_ExitPHI.
930 bool isOriginalScalarKind() const {
931 return getOriginalKind() != ScopArrayInfo::MK_Array;
934 /// Whether this access is an array to a scalar memory object, also
935 /// considering changes by setNewAccessRelation.
936 bool isLatestScalarKind() const {
937 return getLatestKind() != ScopArrayInfo::MK_Array;
940 /// Old name of isOriginalScalarKind.
941 bool isScalarKind() const { return isOriginalScalarKind(); }
943 /// Was this MemoryAccess detected as a scalar dependences?
944 bool isOriginalValueKind() const {
945 return getOriginalKind() == ScopArrayInfo::MK_Value;
948 /// Is this MemoryAccess currently modeling scalar dependences?
949 bool isLatestValueKind() const {
950 return getLatestKind() == ScopArrayInfo::MK_Value;
953 /// Old name of isOriginalValueKind().
954 bool isValueKind() const { return isOriginalValueKind(); }
956 /// Was this MemoryAccess detected as a special PHI node access?
957 bool isOriginalPHIKind() const {
958 return getOriginalKind() == ScopArrayInfo::MK_PHI;
961 /// Is this MemoryAccess modeling special PHI node accesses, also
962 /// considering a potential change by setNewAccessRelation?
963 bool isLatestPHIKind() const {
964 return getLatestKind() == ScopArrayInfo::MK_PHI;
967 /// Old name of isOriginalPHIKind.
968 bool isPHIKind() const { return isOriginalPHIKind(); }
970 /// Was this MemoryAccess detected as the accesses of a PHI node in the
971 /// SCoP's exit block?
972 bool isOriginalExitPHIKind() const {
973 return getOriginalKind() == ScopArrayInfo::MK_ExitPHI;
976 /// Is this MemoryAccess modeling the accesses of a PHI node in the
977 /// SCoP's exit block? Can be changed to an array access using
978 /// setNewAccessRelation().
979 bool isLatestExitPHIKind() const {
980 return getLatestKind() == ScopArrayInfo::MK_ExitPHI;
983 /// Old name of isOriginalExitPHIKind().
984 bool isExitPHIKind() const { return isOriginalExitPHIKind(); }
986 /// Was this access detected as one of the two PHI types?
987 bool isOriginalAnyPHIKind() const {
988 return isOriginalPHIKind() || isOriginalExitPHIKind();
991 /// Does this access orginate from one of the two PHI types? Can be
992 /// changed to an array access using setNewAccessRelation().
993 bool isLatestAnyPHIKind() const {
994 return isLatestPHIKind() || isLatestExitPHIKind();
997 /// Old name of isOriginalAnyPHIKind().
998 bool isAnyPHIKind() const { return isOriginalAnyPHIKind(); }
1000 /// Get the statement that contains this memory access.
1001 ScopStmt *getStatement() const { return Statement; }
1003 /// Get the reduction type of this access
1004 ReductionType getReductionType() const { return RedType; }
1006 /// Set the updated access relation read from JSCOP file.
1007 void setNewAccessRelation(__isl_take isl_map *NewAccessRelation);
1009 /// Mark this a reduction like access
1010 void markAsReductionLike(ReductionType RT) { RedType = RT; }
1012 /// Align the parameters in the access relation to the scop context
1013 void realignParams();
1015 /// Update the dimensionality of the memory access.
1017 /// During scop construction some memory accesses may not be constructed with
1018 /// their full dimensionality, but outer dimensions may have been omitted if
1019 /// they took the value 'zero'. By updating the dimensionality of the
1020 /// statement we add additional zero-valued dimensions to match the
1021 /// dimensionality of the ScopArrayInfo object that belongs to this memory
1022 /// access.
1023 void updateDimensionality();
1025 /// Get identifier for the memory access.
1027 /// This identifier is unique for all accesses that belong to the same scop
1028 /// statement.
1029 __isl_give isl_id *getId() const;
1031 /// Print the MemoryAccess.
1033 /// @param OS The output stream the MemoryAccess is printed to.
1034 void print(raw_ostream &OS) const;
1036 /// Print the MemoryAccess to stderr.
1037 void dump() const;
1039 /// Is the memory access affine?
1040 bool isAffine() const { return IsAffine; }
1043 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1044 MemoryAccess::ReductionType RT);
1046 /// Ordered list type to hold accesses.
1047 using MemoryAccessList = std::forward_list<MemoryAccess *>;
1049 /// Helper structure for invariant memory accesses.
1050 struct InvariantAccess {
1051 /// The memory access that is (partially) invariant.
1052 MemoryAccess *MA;
1054 /// The context under which the access is not invariant.
1055 isl_set *NonHoistableCtx;
1058 /// Ordered container type to hold invariant accesses.
1059 using InvariantAccessesTy = SmallVector<InvariantAccess, 8>;
1061 /// Type for equivalent invariant accesses and their domain context.
1062 struct InvariantEquivClassTy {
1064 /// The pointer that identifies this equivalence class
1065 const SCEV *IdentifyingPointer;
1067 /// Memory accesses now treated invariant
1069 /// These memory accesses access the pointer location that identifies
1070 /// this equivalence class. They are treated as invariant and hoisted during
1071 /// code generation.
1072 MemoryAccessList InvariantAccesses;
1074 /// The execution context under which the memory location is accessed
1076 /// It is the union of the execution domains of the memory accesses in the
1077 /// InvariantAccesses list.
1078 isl_set *ExecutionContext;
1080 /// The type of the invariant access
1082 /// It is used to differentiate between differently typed invariant loads from
1083 /// the same location.
1084 Type *AccessType;
1087 /// Type for invariant accesses equivalence classes.
1088 using InvariantEquivClassesTy = SmallVector<InvariantEquivClassTy, 8>;
1090 /// Statement of the Scop
1092 /// A Scop statement represents an instruction in the Scop.
1094 /// It is further described by its iteration domain, its schedule and its data
1095 /// accesses.
1096 /// At the moment every statement represents a single basic block of LLVM-IR.
1097 class ScopStmt {
1098 public:
1099 ScopStmt(const ScopStmt &) = delete;
1100 const ScopStmt &operator=(const ScopStmt &) = delete;
1102 /// Create the ScopStmt from a BasicBlock.
1103 ScopStmt(Scop &parent, BasicBlock &bb);
1105 /// Create an overapproximating ScopStmt for the region @p R.
1106 ScopStmt(Scop &parent, Region &R);
1108 /// Create a copy statement.
1110 /// @param Stmt The parent statement.
1111 /// @param SourceRel The source location.
1112 /// @param TargetRel The target location.
1113 /// @param Domain The original domain under which copy statement whould
1114 /// be executed.
1115 ScopStmt(Scop &parent, __isl_take isl_map *SourceRel,
1116 __isl_take isl_map *TargetRel, __isl_take isl_set *Domain);
1118 /// Initialize members after all MemoryAccesses have been added.
1119 void init(LoopInfo &LI);
1121 private:
1122 /// Polyhedral description
1123 //@{
1125 /// The Scop containing this ScopStmt
1126 Scop &Parent;
1128 /// The domain under which this statement is not modeled precisely.
1130 /// The invalid domain for a statement describes all parameter combinations
1131 /// under which the statement looks to be executed but is in fact not because
1132 /// some assumption/restriction makes the statement/scop invalid.
1133 isl_set *InvalidDomain;
1135 /// The iteration domain describes the set of iterations for which this
1136 /// statement is executed.
1138 /// Example:
1139 /// for (i = 0; i < 100 + b; ++i)
1140 /// for (j = 0; j < i; ++j)
1141 /// S(i,j);
1143 /// 'S' is executed for different values of i and j. A vector of all
1144 /// induction variables around S (i, j) is called iteration vector.
1145 /// The domain describes the set of possible iteration vectors.
1147 /// In this case it is:
1149 /// Domain: 0 <= i <= 100 + b
1150 /// 0 <= j <= i
1152 /// A pair of statement and iteration vector (S, (5,3)) is called statement
1153 /// instance.
1154 isl_set *Domain;
1156 /// The memory accesses of this statement.
1158 /// The only side effects of a statement are its memory accesses.
1159 typedef SmallVector<MemoryAccess *, 8> MemoryAccessVec;
1160 MemoryAccessVec MemAccs;
1162 /// Mapping from instructions to (scalar) memory accesses.
1163 DenseMap<const Instruction *, MemoryAccessList> InstructionToAccess;
1165 /// The set of values defined elsewhere required in this ScopStmt and
1166 /// their MK_Value READ MemoryAccesses.
1167 DenseMap<Value *, MemoryAccess *> ValueReads;
1169 /// The set of values defined in this ScopStmt that are required
1170 /// elsewhere, mapped to their MK_Value WRITE MemoryAccesses.
1171 DenseMap<Instruction *, MemoryAccess *> ValueWrites;
1173 /// Map from PHI nodes to its incoming value when coming from this
1174 /// statement.
1176 /// Non-affine subregions can have multiple exiting blocks that are incoming
1177 /// blocks of the PHI nodes. This map ensures that there is only one write
1178 /// operation for the complete subregion. A PHI selecting the relevant value
1179 /// will be inserted.
1180 DenseMap<PHINode *, MemoryAccess *> PHIWrites;
1182 //@}
1184 /// A SCoP statement represents either a basic block (affine/precise case) or
1185 /// a whole region (non-affine case).
1187 /// Only one of the following two members will therefore be set and indicate
1188 /// which kind of statement this is.
1190 ///{
1192 /// The BasicBlock represented by this statement (in the affine case).
1193 BasicBlock *BB;
1195 /// The region represented by this statement (in the non-affine case).
1196 Region *R;
1198 ///}
1200 /// The isl AST build for the new generated AST.
1201 isl_ast_build *Build;
1203 SmallVector<Loop *, 4> NestLoops;
1205 std::string BaseName;
1207 /// Build the statement.
1208 //@{
1209 void buildDomain();
1211 /// Fill NestLoops with loops surrounding this statement.
1212 void collectSurroundingLoops();
1214 /// Build the access relation of all memory accesses.
1215 void buildAccessRelations();
1217 /// Detect and mark reductions in the ScopStmt
1218 void checkForReductions();
1220 /// Collect loads which might form a reduction chain with @p StoreMA
1221 void
1222 collectCandiateReductionLoads(MemoryAccess *StoreMA,
1223 llvm::SmallVectorImpl<MemoryAccess *> &Loads);
1224 //@}
1226 public:
1227 ~ScopStmt();
1229 /// Get an isl_ctx pointer.
1230 isl_ctx *getIslCtx() const;
1232 /// Get the iteration domain of this ScopStmt.
1234 /// @return The iteration domain of this ScopStmt.
1235 __isl_give isl_set *getDomain() const;
1237 /// Get the space of the iteration domain
1239 /// @return The space of the iteration domain
1240 __isl_give isl_space *getDomainSpace() const;
1242 /// Get the id of the iteration domain space
1244 /// @return The id of the iteration domain space
1245 __isl_give isl_id *getDomainId() const;
1247 /// Get an isl string representing this domain.
1248 std::string getDomainStr() const;
1250 /// Get the schedule function of this ScopStmt.
1252 /// @return The schedule function of this ScopStmt, if it does not contain
1253 /// extension nodes, and nullptr, otherwise.
1254 __isl_give isl_map *getSchedule() const;
1256 /// Get an isl string representing this schedule.
1258 /// @return An isl string representing this schedule, if it does not contain
1259 /// extension nodes, and an empty string, otherwise.
1260 std::string getScheduleStr() const;
1262 /// Get the invalid domain for this statement.
1263 __isl_give isl_set *getInvalidDomain() const {
1264 return isl_set_copy(InvalidDomain);
1267 /// Get the invalid context for this statement.
1268 __isl_give isl_set *getInvalidContext() const {
1269 return isl_set_params(getInvalidDomain());
1272 /// Set the invalid context for this statement to @p ID.
1273 void setInvalidDomain(__isl_take isl_set *ID);
1275 /// Get the BasicBlock represented by this ScopStmt (if any).
1277 /// @return The BasicBlock represented by this ScopStmt, or null if the
1278 /// statement represents a region.
1279 BasicBlock *getBasicBlock() const { return BB; }
1281 /// Return true if this statement represents a single basic block.
1282 bool isBlockStmt() const { return BB != nullptr; }
1284 /// Return true if this is a copy statement.
1285 bool isCopyStmt() const { return BB == nullptr && R == nullptr; }
1287 /// Get the region represented by this ScopStmt (if any).
1289 /// @return The region represented by this ScopStmt, or null if the statement
1290 /// represents a basic block.
1291 Region *getRegion() const { return R; }
1293 /// Return true if this statement represents a whole region.
1294 bool isRegionStmt() const { return R != nullptr; }
1296 /// Return a BasicBlock from this statement.
1298 /// For block statements, it returns the BasicBlock itself. For subregion
1299 /// statements, return its entry block.
1300 BasicBlock *getEntryBlock() const;
1302 /// Return true if this statement does not contain any accesses.
1303 bool isEmpty() const { return MemAccs.empty(); }
1305 /// Return the only array access for @p Inst, if existing.
1307 /// @param Inst The instruction for which to look up the access.
1308 /// @returns The unique array memory access related to Inst or nullptr if
1309 /// no array access exists
1310 MemoryAccess *getArrayAccessOrNULLFor(const Instruction *Inst) const {
1311 auto It = InstructionToAccess.find(Inst);
1312 if (It == InstructionToAccess.end())
1313 return nullptr;
1315 MemoryAccess *ArrayAccess = nullptr;
1317 for (auto Access : It->getSecond()) {
1318 if (!Access->isArrayKind())
1319 continue;
1321 assert(!ArrayAccess && "More then one array access for instruction");
1323 ArrayAccess = Access;
1326 return ArrayAccess;
1329 /// Return the only array access for @p Inst.
1331 /// @param Inst The instruction for which to look up the access.
1332 /// @returns The unique array memory access related to Inst.
1333 MemoryAccess &getArrayAccessFor(const Instruction *Inst) const {
1334 MemoryAccess *ArrayAccess = getArrayAccessOrNULLFor(Inst);
1336 assert(ArrayAccess && "No array access found for instruction!");
1337 return *ArrayAccess;
1340 /// Return the MemoryAccess that writes the value of an instruction
1341 /// defined in this statement, or nullptr if not existing, respectively
1342 /// not yet added.
1343 MemoryAccess *lookupValueWriteOf(Instruction *Inst) const {
1344 assert((isRegionStmt() && R->contains(Inst)) ||
1345 (!isRegionStmt() && Inst->getParent() == BB));
1346 return ValueWrites.lookup(Inst);
1349 /// Return the MemoryAccess that reloads a value, or nullptr if not
1350 /// existing, respectively not yet added.
1351 MemoryAccess *lookupValueReadOf(Value *Inst) const {
1352 return ValueReads.lookup(Inst);
1355 /// Return the PHI write MemoryAccess for the incoming values from any
1356 /// basic block in this ScopStmt, or nullptr if not existing,
1357 /// respectively not yet added.
1358 MemoryAccess *lookupPHIWriteOf(PHINode *PHI) const {
1359 assert(isBlockStmt() || R->getExit() == PHI->getParent());
1360 return PHIWrites.lookup(PHI);
1363 /// Add @p Access to this statement's list of accesses.
1364 void addAccess(MemoryAccess *Access);
1366 /// Remove a MemoryAccess from this statement.
1368 /// Note that scalar accesses that are caused by MA will
1369 /// be eliminated too.
1370 void removeMemoryAccess(MemoryAccess *MA);
1372 typedef MemoryAccessVec::iterator iterator;
1373 typedef MemoryAccessVec::const_iterator const_iterator;
1375 iterator begin() { return MemAccs.begin(); }
1376 iterator end() { return MemAccs.end(); }
1377 const_iterator begin() const { return MemAccs.begin(); }
1378 const_iterator end() const { return MemAccs.end(); }
1379 size_t size() const { return MemAccs.size(); }
1381 unsigned getNumIterators() const;
1383 Scop *getParent() { return &Parent; }
1384 const Scop *getParent() const { return &Parent; }
1386 const char *getBaseName() const;
1388 /// Set the isl AST build.
1389 void setAstBuild(__isl_keep isl_ast_build *B) { Build = B; }
1391 /// Get the isl AST build.
1392 __isl_keep isl_ast_build *getAstBuild() const { return Build; }
1394 /// Restrict the domain of the statement.
1396 /// @param NewDomain The new statement domain.
1397 void restrictDomain(__isl_take isl_set *NewDomain);
1399 /// Compute the isl representation for the SCEV @p E in this stmt.
1401 /// @param E The SCEV that should be translated.
1402 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
1404 /// Note that this function will also adjust the invalid context accordingly.
1405 __isl_give isl_pw_aff *getPwAff(const SCEV *E, bool NonNegative = false);
1407 /// Get the loop for a dimension.
1409 /// @param Dimension The dimension of the induction variable
1410 /// @return The loop at a certain dimension.
1411 Loop *getLoopForDimension(unsigned Dimension) const;
1413 /// Align the parameters in the statement to the scop context
1414 void realignParams();
1416 /// Print the ScopStmt.
1418 /// @param OS The output stream the ScopStmt is printed to.
1419 void print(raw_ostream &OS) const;
1421 /// Print the ScopStmt to stderr.
1422 void dump() const;
1425 /// Print ScopStmt S to raw_ostream O.
1426 static inline raw_ostream &operator<<(raw_ostream &O, const ScopStmt &S) {
1427 S.print(O);
1428 return O;
1431 /// Static Control Part
1433 /// A Scop is the polyhedral representation of a control flow region detected
1434 /// by the Scop detection. It is generated by translating the LLVM-IR and
1435 /// abstracting its effects.
1437 /// A Scop consists of a set of:
1439 /// * A set of statements executed in the Scop.
1441 /// * A set of global parameters
1442 /// Those parameters are scalar integer values, which are constant during
1443 /// execution.
1445 /// * A context
1446 /// This context contains information about the values the parameters
1447 /// can take and relations between different parameters.
1448 class Scop {
1449 public:
1450 /// Type to represent a pair of minimal/maximal access to an array.
1451 using MinMaxAccessTy = std::pair<isl_pw_multi_aff *, isl_pw_multi_aff *>;
1453 /// Vector of minimal/maximal accesses to different arrays.
1454 using MinMaxVectorTy = SmallVector<MinMaxAccessTy, 4>;
1456 /// Pair of minimal/maximal access vectors representing
1457 /// read write and read only accesses
1458 using MinMaxVectorPairTy = std::pair<MinMaxVectorTy, MinMaxVectorTy>;
1460 /// Vector of pair of minimal/maximal access vectors representing
1461 /// non read only and read only accesses for each alias group.
1462 using MinMaxVectorPairVectorTy = SmallVector<MinMaxVectorPairTy, 4>;
1464 private:
1465 Scop(const Scop &) = delete;
1466 const Scop &operator=(const Scop &) = delete;
1468 ScalarEvolution *SE;
1470 /// The underlying Region.
1471 Region &R;
1473 // Access functions of the SCoP.
1475 // This owns all the MemoryAccess objects of the Scop created in this pass.
1476 AccFuncVector AccessFunctions;
1478 /// Flag to indicate that the scheduler actually optimized the SCoP.
1479 bool IsOptimized;
1481 /// True if the underlying region has a single exiting block.
1482 bool HasSingleExitEdge;
1484 /// Flag to remember if the SCoP contained an error block or not.
1485 bool HasErrorBlock;
1487 /// Max loop depth.
1488 unsigned MaxLoopDepth;
1490 /// Number of copy statements.
1491 unsigned CopyStmtsNum;
1493 typedef std::list<ScopStmt> StmtSet;
1494 /// The statements in this Scop.
1495 StmtSet Stmts;
1497 /// Parameters of this Scop
1498 ParameterSetTy Parameters;
1500 /// Mapping from parameters to their ids.
1501 DenseMap<const SCEV *, isl_id *> ParameterIds;
1503 /// The context of the SCoP created during SCoP detection.
1504 ScopDetection::DetectionContext &DC;
1506 /// Isl context.
1508 /// We need a shared_ptr with reference counter to delete the context when all
1509 /// isl objects are deleted. We will distribute the shared_ptr to all objects
1510 /// that use the context to create isl objects, and increase the reference
1511 /// counter. By doing this, we guarantee that the context is deleted when we
1512 /// delete the last object that creates isl objects with the context.
1513 std::shared_ptr<isl_ctx> IslCtx;
1515 /// A map from basic blocks to SCoP statements.
1516 DenseMap<BasicBlock *, ScopStmt *> StmtMap;
1518 /// A map from basic blocks to their domains.
1519 DenseMap<BasicBlock *, isl_set *> DomainMap;
1521 /// Constraints on parameters.
1522 isl_set *Context;
1524 /// The affinator used to translate SCEVs to isl expressions.
1525 SCEVAffinator Affinator;
1527 typedef std::map<std::pair<AssertingVH<const Value>, int>,
1528 std::unique_ptr<ScopArrayInfo>>
1529 ArrayInfoMapTy;
1531 typedef StringMap<std::unique_ptr<ScopArrayInfo>> ArrayNameMapTy;
1533 typedef SetVector<ScopArrayInfo *> ArrayInfoSetTy;
1535 /// A map to remember ScopArrayInfo objects for all base pointers.
1537 /// As PHI nodes may have two array info objects associated, we add a flag
1538 /// that distinguishes between the PHI node specific ArrayInfo object
1539 /// and the normal one.
1540 ArrayInfoMapTy ScopArrayInfoMap;
1542 /// A map to remember ScopArrayInfo objects for all names of memory
1543 /// references.
1544 ArrayNameMapTy ScopArrayNameMap;
1546 /// A set to remember ScopArrayInfo objects.
1547 /// @see Scop::ScopArrayInfoMap
1548 ArrayInfoSetTy ScopArrayInfoSet;
1550 /// The assumptions under which this scop was built.
1552 /// When constructing a scop sometimes the exact representation of a statement
1553 /// or condition would be very complex, but there is a common case which is a
1554 /// lot simpler, but which is only valid under certain assumptions. The
1555 /// assumed context records the assumptions taken during the construction of
1556 /// this scop and that need to be code generated as a run-time test.
1557 isl_set *AssumedContext;
1559 /// The restrictions under which this SCoP was built.
1561 /// The invalid context is similar to the assumed context as it contains
1562 /// constraints over the parameters. However, while we need the constraints
1563 /// in the assumed context to be "true" the constraints in the invalid context
1564 /// need to be "false". Otherwise they behave the same.
1565 isl_set *InvalidContext;
1567 /// Helper struct to remember assumptions.
1568 struct Assumption {
1570 /// The kind of the assumption (e.g., WRAPPING).
1571 AssumptionKind Kind;
1573 /// Flag to distinguish assumptions and restrictions.
1574 AssumptionSign Sign;
1576 /// The valid/invalid context if this is an assumption/restriction.
1577 isl_set *Set;
1579 /// The location that caused this assumption.
1580 DebugLoc Loc;
1582 /// An optional block whose domain can simplify the assumption.
1583 BasicBlock *BB;
1586 /// Collection to hold taken assumptions.
1588 /// There are two reasons why we want to record assumptions first before we
1589 /// add them to the assumed/invalid context:
1590 /// 1) If the SCoP is not profitable or otherwise invalid without the
1591 /// assumed/invalid context we do not have to compute it.
1592 /// 2) Information about the context are gathered rather late in the SCoP
1593 /// construction (basically after we know all parameters), thus the user
1594 /// might see overly complicated assumptions to be taken while they will
1595 /// only be simplified later on.
1596 SmallVector<Assumption, 8> RecordedAssumptions;
1598 /// The schedule of the SCoP
1600 /// The schedule of the SCoP describes the execution order of the statements
1601 /// in the scop by assigning each statement instance a possibly
1602 /// multi-dimensional execution time. The schedule is stored as a tree of
1603 /// schedule nodes.
1605 /// The most common nodes in a schedule tree are so-called band nodes. Band
1606 /// nodes map statement instances into a multi dimensional schedule space.
1607 /// This space can be seen as a multi-dimensional clock.
1609 /// Example:
1611 /// <S,(5,4)> may be mapped to (5,4) by this schedule:
1613 /// s0 = i (Year of execution)
1614 /// s1 = j (Day of execution)
1616 /// or to (9, 20) by this schedule:
1618 /// s0 = i + j (Year of execution)
1619 /// s1 = 20 (Day of execution)
1621 /// The order statement instances are executed is defined by the
1622 /// schedule vectors they are mapped to. A statement instance
1623 /// <A, (i, j, ..)> is executed before a statement instance <B, (i', ..)>, if
1624 /// the schedule vector of A is lexicographic smaller than the schedule
1625 /// vector of B.
1627 /// Besides band nodes, schedule trees contain additional nodes that specify
1628 /// a textual ordering between two subtrees or filter nodes that filter the
1629 /// set of statement instances that will be scheduled in a subtree. There
1630 /// are also several other nodes. A full description of the different nodes
1631 /// in a schedule tree is given in the isl manual.
1632 isl_schedule *Schedule;
1634 /// The set of minimal/maximal accesses for each alias group.
1636 /// When building runtime alias checks we look at all memory instructions and
1637 /// build so called alias groups. Each group contains a set of accesses to
1638 /// different base arrays which might alias with each other. However, between
1639 /// alias groups there is no aliasing possible.
1641 /// In a program with int and float pointers annotated with tbaa information
1642 /// we would probably generate two alias groups, one for the int pointers and
1643 /// one for the float pointers.
1645 /// During code generation we will create a runtime alias check for each alias
1646 /// group to ensure the SCoP is executed in an alias free environment.
1647 MinMaxVectorPairVectorTy MinMaxAliasGroups;
1649 /// Mapping from invariant loads to the representing invariant load of
1650 /// their equivalence class.
1651 ValueToValueMap InvEquivClassVMap;
1653 /// List of invariant accesses.
1654 InvariantEquivClassesTy InvariantEquivClasses;
1656 /// Scop constructor; invoked from ScopBuilder::buildScop.
1657 Scop(Region &R, ScalarEvolution &SE, LoopInfo &LI,
1658 ScopDetection::DetectionContext &DC);
1660 //@}
1662 /// Initialize this ScopBuilder.
1663 void init(AliasAnalysis &AA, AssumptionCache &AC, DominatorTree &DT,
1664 LoopInfo &LI);
1666 /// Propagate domains that are known due to graph properties.
1668 /// As a CFG is mostly structured we use the graph properties to propagate
1669 /// domains without the need to compute all path conditions. In particular, if
1670 /// a block A dominates a block B and B post-dominates A we know that the
1671 /// domain of B is a superset of the domain of A. As we do not have
1672 /// post-dominator information available here we use the less precise region
1673 /// information. Given a region R, we know that the exit is always executed if
1674 /// the entry was executed, thus the domain of the exit is a superset of the
1675 /// domain of the entry. In case the exit can only be reached from within the
1676 /// region the domains are in fact equal. This function will use this property
1677 /// to avoid the generation of condition constraints that determine when a
1678 /// branch is taken. If @p BB is a region entry block we will propagate its
1679 /// domain to the region exit block. Additionally, we put the region exit
1680 /// block in the @p FinishedExitBlocks set so we can later skip edges from
1681 /// within the region to that block.
1683 /// @param BB The block for which the domain is currently propagated.
1684 /// @param BBLoop The innermost affine loop surrounding @p BB.
1685 /// @param FinishedExitBlocks Set of region exits the domain was set for.
1686 /// @param LI The LoopInfo for the current function.
1688 void propagateDomainConstraintsToRegionExit(
1689 BasicBlock *BB, Loop *BBLoop,
1690 SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI);
1692 /// Compute the union of predecessor domains for @p BB.
1694 /// To compute the union of all domains of predecessors of @p BB this
1695 /// function applies similar reasoning on the CFG structure as described for
1696 /// @see propagateDomainConstraintsToRegionExit
1698 /// @param BB The block for which the predecessor domains are collected.
1699 /// @param Domain The domain under which BB is executed.
1700 /// @param DT The DominatorTree for the current function.
1701 /// @param LI The LoopInfo for the current function.
1703 /// @returns The domain under which @p BB is executed.
1704 __isl_give isl_set *
1705 getPredecessorDomainConstraints(BasicBlock *BB, __isl_keep isl_set *Domain,
1706 DominatorTree &DT, LoopInfo &LI);
1708 /// Add loop carried constraints to the header block of the loop @p L.
1710 /// @param L The loop to process.
1711 /// @param LI The LoopInfo for the current function.
1713 /// @returns True if there was no problem and false otherwise.
1714 bool addLoopBoundsToHeaderDomain(Loop *L, LoopInfo &LI);
1716 /// Compute the branching constraints for each basic block in @p R.
1718 /// @param R The region we currently build branching conditions for.
1719 /// @param DT The DominatorTree for the current function.
1720 /// @param LI The LoopInfo for the current function.
1722 /// @returns True if there was no problem and false otherwise.
1723 bool buildDomainsWithBranchConstraints(Region *R, DominatorTree &DT,
1724 LoopInfo &LI);
1726 /// Propagate the domain constraints through the region @p R.
1728 /// @param R The region we currently build branching conditions for.
1729 /// @param DT The DominatorTree for the current function.
1730 /// @param LI The LoopInfo for the current function.
1732 /// @returns True if there was no problem and false otherwise.
1733 bool propagateDomainConstraints(Region *R, DominatorTree &DT, LoopInfo &LI);
1735 /// Propagate invalid domains of statements through @p R.
1737 /// This method will propagate invalid statement domains through @p R and at
1738 /// the same time add error block domains to them. Additionally, the domains
1739 /// of error statements and those only reachable via error statements will be
1740 /// replaced by an empty set. Later those will be removed completely.
1742 /// @param R The currently traversed region.
1743 /// @param DT The DominatorTree for the current function.
1744 /// @param LI The LoopInfo for the current function.
1746 /// @returns True if there was no problem and false otherwise.
1747 bool propagateInvalidStmtDomains(Region *R, DominatorTree &DT, LoopInfo &LI);
1749 /// Compute the domain for each basic block in @p R.
1751 /// @param R The region we currently traverse.
1752 /// @param DT The DominatorTree for the current function.
1753 /// @param LI The LoopInfo for the current function.
1755 /// @returns True if there was no problem and false otherwise.
1756 bool buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI);
1758 /// Add parameter constraints to @p C that imply a non-empty domain.
1759 __isl_give isl_set *addNonEmptyDomainConstraints(__isl_take isl_set *C) const;
1761 /// Return the access for the base ptr of @p MA if any.
1762 MemoryAccess *lookupBasePtrAccess(MemoryAccess *MA);
1764 /// Check if the base ptr of @p MA is in the SCoP but not hoistable.
1765 bool hasNonHoistableBasePtrInScop(MemoryAccess *MA,
1766 __isl_keep isl_union_map *Writes);
1768 /// Create equivalence classes for required invariant accesses.
1770 /// These classes will consolidate multiple required invariant loads from the
1771 /// same address in order to keep the number of dimensions in the SCoP
1772 /// description small. For each such class equivalence class only one
1773 /// representing element, hence one required invariant load, will be chosen
1774 /// and modeled as parameter. The method
1775 /// Scop::getRepresentingInvariantLoadSCEV() will replace each element from an
1776 /// equivalence class with the representing element that is modeled. As a
1777 /// consequence Scop::getIdForParam() will only return an id for the
1778 /// representing element of each equivalence class, thus for each required
1779 /// invariant location.
1780 void buildInvariantEquivalenceClasses();
1782 /// Return the context under which the access cannot be hoisted.
1784 /// @param Access The access to check.
1785 /// @param Writes The set of all memory writes in the scop.
1787 /// @return Return the context under which the access cannot be hoisted or a
1788 /// nullptr if it cannot be hoisted at all.
1789 __isl_give isl_set *getNonHoistableCtx(MemoryAccess *Access,
1790 __isl_keep isl_union_map *Writes);
1792 /// Verify that all required invariant loads have been hoisted.
1794 /// Invariant load hoisting is not guaranteed to hoist all loads that were
1795 /// assumed to be scop invariant during scop detection. This function checks
1796 /// for cases where the hoisting failed, but where it would have been
1797 /// necessary for our scop modeling to be correct. In case of insufficent
1798 /// hoisting the scop is marked as invalid.
1800 /// In the example below Bound[1] is required to be invariant:
1802 /// for (int i = 1; i < Bound[0]; i++)
1803 /// for (int j = 1; j < Bound[1]; j++)
1804 /// ...
1806 void verifyInvariantLoads();
1808 /// Hoist invariant memory loads and check for required ones.
1810 /// We first identify "common" invariant loads, thus loads that are invariant
1811 /// and can be hoisted. Then we check if all required invariant loads have
1812 /// been identified as (common) invariant. A load is a required invariant load
1813 /// if it was assumed to be invariant during SCoP detection, e.g., to assume
1814 /// loop bounds to be affine or runtime alias checks to be placeable. In case
1815 /// a required invariant load was not identified as (common) invariant we will
1816 /// drop this SCoP. An example for both "common" as well as required invariant
1817 /// loads is given below:
1819 /// for (int i = 1; i < *LB[0]; i++)
1820 /// for (int j = 1; j < *LB[1]; j++)
1821 /// A[i][j] += A[0][0] + (*V);
1823 /// Common inv. loads: V, A[0][0], LB[0], LB[1]
1824 /// Required inv. loads: LB[0], LB[1], (V, if it may alias with A or LB)
1826 void hoistInvariantLoads();
1828 /// Add invariant loads listed in @p InvMAs with the domain of @p Stmt.
1829 void addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs);
1831 /// Create an id for @p Param and store it in the ParameterIds map.
1832 void createParameterId(const SCEV *Param);
1834 /// Build the Context of the Scop.
1835 void buildContext();
1837 /// Add user provided parameter constraints to context (source code).
1838 void addUserAssumptions(AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI);
1840 /// Add user provided parameter constraints to context (command line).
1841 void addUserContext();
1843 /// Add the bounds of the parameters to the context.
1844 void addParameterBounds();
1846 /// Simplify the assumed and invalid context.
1847 void simplifyContexts();
1849 /// Get the representing SCEV for @p S if applicable, otherwise @p S.
1851 /// Invariant loads of the same location are put in an equivalence class and
1852 /// only one of them is chosen as a representing element that will be
1853 /// modeled as a parameter. The others have to be normalized, i.e.,
1854 /// replaced by the representing element of their equivalence class, in order
1855 /// to get the correct parameter value, e.g., in the SCEVAffinator.
1857 /// @param S The SCEV to normalize.
1859 /// @return The representing SCEV for invariant loads or @p S if none.
1860 const SCEV *getRepresentingInvariantLoadSCEV(const SCEV *S);
1862 /// Create a new SCoP statement for @p BB.
1864 /// A new statement for @p BB will be created and added to the statement
1865 /// vector
1866 /// and map.
1868 /// @param BB The basic block we build the statement for.
1869 void addScopStmt(BasicBlock *BB);
1871 /// Create a new SCoP statement for @p R.
1873 /// A new statement for @p R will be created and added to the statement vector
1874 /// and map.
1876 /// @param R The region we build the statement for.
1877 void addScopStmt(Region *R);
1879 /// @param Update access dimensionalities.
1881 /// When detecting memory accesses different accesses to the same array may
1882 /// have built with different dimensionality, as outer zero-values dimensions
1883 /// may not have been recognized as separate dimensions. This function goes
1884 /// again over all memory accesses and updates their dimensionality to match
1885 /// the dimensionality of the underlying ScopArrayInfo object.
1886 void updateAccessDimensionality();
1888 /// Construct the schedule of this SCoP.
1890 /// @param LI The LoopInfo for the current function.
1891 void buildSchedule(LoopInfo &LI);
1893 /// A loop stack element to keep track of per-loop information during
1894 /// schedule construction.
1895 typedef struct LoopStackElement {
1896 // The loop for which we keep information.
1897 Loop *L;
1899 // The (possibly incomplete) schedule for this loop.
1900 isl_schedule *Schedule;
1902 // The number of basic blocks in the current loop, for which a schedule has
1903 // already been constructed.
1904 unsigned NumBlocksProcessed;
1906 LoopStackElement(Loop *L, __isl_give isl_schedule *S,
1907 unsigned NumBlocksProcessed)
1908 : L(L), Schedule(S), NumBlocksProcessed(NumBlocksProcessed) {}
1909 } LoopStackElementTy;
1911 /// The loop stack used for schedule construction.
1913 /// The loop stack keeps track of schedule information for a set of nested
1914 /// loops as well as an (optional) 'nullptr' loop that models the outermost
1915 /// schedule dimension. The loops in a loop stack always have a parent-child
1916 /// relation where the loop at position n is the parent of the loop at
1917 /// position n + 1.
1918 typedef SmallVector<LoopStackElementTy, 4> LoopStackTy;
1920 /// Construct schedule information for a given Region and add the
1921 /// derived information to @p LoopStack.
1923 /// Given a Region we derive schedule information for all RegionNodes
1924 /// contained in this region ensuring that the assigned execution times
1925 /// correctly model the existing control flow relations.
1927 /// @param R The region which to process.
1928 /// @param LoopStack A stack of loops that are currently under
1929 /// construction.
1930 /// @param LI The LoopInfo for the current function.
1931 void buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI);
1933 /// Build Schedule for the region node @p RN and add the derived
1934 /// information to @p LoopStack.
1936 /// In case @p RN is a BasicBlock or a non-affine Region, we construct the
1937 /// schedule for this @p RN and also finalize loop schedules in case the
1938 /// current @p RN completes the loop.
1940 /// In case @p RN is a not-non-affine Region, we delegate the construction to
1941 /// buildSchedule(Region *R, ...).
1943 /// @param RN The RegionNode region traversed.
1944 /// @param LoopStack A stack of loops that are currently under
1945 /// construction.
1946 /// @param LI The LoopInfo for the current function.
1947 void buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI);
1949 /// Collect all memory access relations of a given type.
1951 /// @param Predicate A predicate function that returns true if an access is
1952 /// of a given type.
1954 /// @returns The set of memory accesses in the scop that match the predicate.
1955 __isl_give isl_union_map *
1956 getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate);
1958 /// @name Helper functions for printing the Scop.
1960 //@{
1961 void printContext(raw_ostream &OS) const;
1962 void printArrayInfo(raw_ostream &OS) const;
1963 void printStatements(raw_ostream &OS) const;
1964 void printAliasAssumptions(raw_ostream &OS) const;
1965 //@}
1967 friend class ScopBuilder;
1969 public:
1970 ~Scop();
1972 /// Get the count of copy statements added to this Scop.
1974 /// @return The count of copy statements added to this Scop.
1975 unsigned getCopyStmtsNum() { return CopyStmtsNum; }
1977 /// Create a new copy statement.
1979 /// A new statement will be created and added to the statement vector.
1981 /// @param Stmt The parent statement.
1982 /// @param SourceRel The source location.
1983 /// @param TargetRel The target location.
1984 /// @param Domain The original domain under which copy statement whould
1985 /// be executed.
1986 ScopStmt *addScopStmt(__isl_take isl_map *SourceRel,
1987 __isl_take isl_map *TargetRel,
1988 __isl_take isl_set *Domain);
1990 /// Add the access function to all MemoryAccess objects of the Scop
1991 /// created in this pass.
1992 void addAccessFunction(MemoryAccess *Access) {
1993 AccessFunctions.emplace_back(Access);
1996 ScalarEvolution *getSE() const;
1998 /// Get the count of parameters used in this Scop.
2000 /// @return The count of parameters used in this Scop.
2001 size_t getNumParams() const { return Parameters.size(); }
2003 /// Take a list of parameters and add the new ones to the scop.
2004 void addParams(const ParameterSetTy &NewParameters);
2006 /// Return whether this scop is empty, i.e. contains no statements that
2007 /// could be executed.
2008 bool isEmpty() const { return Stmts.empty(); }
2010 typedef ArrayInfoSetTy::iterator array_iterator;
2011 typedef ArrayInfoSetTy::const_iterator const_array_iterator;
2012 typedef iterator_range<ArrayInfoSetTy::iterator> array_range;
2013 typedef iterator_range<ArrayInfoSetTy::const_iterator> const_array_range;
2015 inline array_iterator array_begin() { return ScopArrayInfoSet.begin(); }
2017 inline array_iterator array_end() { return ScopArrayInfoSet.end(); }
2019 inline const_array_iterator array_begin() const {
2020 return ScopArrayInfoSet.begin();
2023 inline const_array_iterator array_end() const {
2024 return ScopArrayInfoSet.end();
2027 inline array_range arrays() {
2028 return array_range(array_begin(), array_end());
2031 inline const_array_range arrays() const {
2032 return const_array_range(array_begin(), array_end());
2035 /// Return the isl_id that represents a certain parameter.
2037 /// @param Parameter A SCEV that was recognized as a Parameter.
2039 /// @return The corresponding isl_id or NULL otherwise.
2040 __isl_give isl_id *getIdForParam(const SCEV *Parameter);
2042 /// Get the maximum region of this static control part.
2044 /// @return The maximum region of this static control part.
2045 inline const Region &getRegion() const { return R; }
2046 inline Region &getRegion() { return R; }
2048 /// Return the function this SCoP is in.
2049 Function &getFunction() const { return *R.getEntry()->getParent(); }
2051 /// Check if @p L is contained in the SCoP.
2052 bool contains(const Loop *L) const { return R.contains(L); }
2054 /// Check if @p BB is contained in the SCoP.
2055 bool contains(const BasicBlock *BB) const { return R.contains(BB); }
2057 /// Check if @p I is contained in the SCoP.
2058 bool contains(const Instruction *I) const { return R.contains(I); }
2060 /// Return the unique exit block of the SCoP.
2061 BasicBlock *getExit() const { return R.getExit(); }
2063 /// Return the unique exiting block of the SCoP if any.
2064 BasicBlock *getExitingBlock() const { return R.getExitingBlock(); }
2066 /// Return the unique entry block of the SCoP.
2067 BasicBlock *getEntry() const { return R.getEntry(); }
2069 /// Return the unique entering block of the SCoP if any.
2070 BasicBlock *getEnteringBlock() const { return R.getEnteringBlock(); }
2072 /// Return true if @p BB is the exit block of the SCoP.
2073 bool isExit(BasicBlock *BB) const { return getExit() == BB; }
2075 /// Return a range of all basic blocks in the SCoP.
2076 Region::block_range blocks() const { return R.blocks(); }
2078 /// Return true if and only if @p BB dominates the SCoP.
2079 bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const;
2081 /// Get the maximum depth of the loop.
2083 /// @return The maximum depth of the loop.
2084 inline unsigned getMaxLoopDepth() const { return MaxLoopDepth; }
2086 /// Return the invariant equivalence class for @p Val if any.
2087 InvariantEquivClassTy *lookupInvariantEquivClass(Value *Val);
2089 /// Return the set of invariant accesses.
2090 InvariantEquivClassesTy &getInvariantAccesses() {
2091 return InvariantEquivClasses;
2094 /// Check if the scop has any invariant access.
2095 bool hasInvariantAccesses() { return !InvariantEquivClasses.empty(); }
2097 /// Mark the SCoP as optimized by the scheduler.
2098 void markAsOptimized() { IsOptimized = true; }
2100 /// Check if the SCoP has been optimized by the scheduler.
2101 bool isOptimized() const { return IsOptimized; }
2103 /// Get the name of this Scop.
2104 std::string getNameStr() const;
2106 /// Get the constraint on parameter of this Scop.
2108 /// @return The constraint on parameter of this Scop.
2109 __isl_give isl_set *getContext() const;
2110 __isl_give isl_space *getParamSpace() const;
2112 /// Get the assumed context for this Scop.
2114 /// @return The assumed context of this Scop.
2115 __isl_give isl_set *getAssumedContext() const;
2117 /// Return true if the optimized SCoP can be executed.
2119 /// In addition to the runtime check context this will also utilize the domain
2120 /// constraints to decide it the optimized version can actually be executed.
2122 /// @returns True if the optimized SCoP can be executed.
2123 bool hasFeasibleRuntimeContext() const;
2125 /// Check if the assumption in @p Set is trivial or not.
2127 /// @param Set The relations between parameters that are assumed to hold.
2128 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2129 /// (needed/assumptions) or negative (invalid/restrictions).
2131 /// @returns True if the assumption @p Set is not trivial.
2132 bool isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign);
2134 /// Track and report an assumption.
2136 /// Use 'clang -Rpass-analysis=polly-scops' or 'opt
2137 /// -pass-remarks-analysis=polly-scops' to output the assumptions.
2139 /// @param Kind The assumption kind describing the underlying cause.
2140 /// @param Set The relations between parameters that are assumed to hold.
2141 /// @param Loc The location in the source that caused this assumption.
2142 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2143 /// (needed/assumptions) or negative (invalid/restrictions).
2145 /// @returns True if the assumption is not trivial.
2146 bool trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set,
2147 DebugLoc Loc, AssumptionSign Sign);
2149 /// Add assumptions to assumed context.
2151 /// The assumptions added will be assumed to hold during the execution of the
2152 /// scop. However, as they are generally not statically provable, at code
2153 /// generation time run-time checks will be generated that ensure the
2154 /// assumptions hold.
2156 /// WARNING: We currently exploit in simplifyAssumedContext the knowledge
2157 /// that assumptions do not change the set of statement instances
2158 /// executed.
2160 /// @param Kind The assumption kind describing the underlying cause.
2161 /// @param Set The relations between parameters that are assumed to hold.
2162 /// @param Loc The location in the source that caused this assumption.
2163 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2164 /// (needed/assumptions) or negative (invalid/restrictions).
2165 void addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, DebugLoc Loc,
2166 AssumptionSign Sign);
2168 /// Record an assumption for later addition to the assumed context.
2170 /// This function will add the assumption to the RecordedAssumptions. This
2171 /// collection will be added (@see addAssumption) to the assumed context once
2172 /// all paramaters are known and the context is fully build.
2174 /// @param Kind The assumption kind describing the underlying cause.
2175 /// @param Set The relations between parameters that are assumed to hold.
2176 /// @param Loc The location in the source that caused this assumption.
2177 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2178 /// (needed/assumptions) or negative (invalid/restrictions).
2179 /// @param BB The block in which this assumption was taken. If it is
2180 /// set, the domain of that block will be used to simplify the
2181 /// actual assumption in @p Set once it is added. This is useful
2182 /// if the assumption was created prior to the domain.
2183 void recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set,
2184 DebugLoc Loc, AssumptionSign Sign,
2185 BasicBlock *BB = nullptr);
2187 /// Add all recorded assumptions to the assumed context.
2188 void addRecordedAssumptions();
2190 /// Mark the scop as invalid.
2192 /// This method adds an assumption to the scop that is always invalid. As a
2193 /// result, the scop will not be optimized later on. This function is commonly
2194 /// called when a condition makes it impossible (or too compile time
2195 /// expensive) to process this scop any further.
2197 /// @param Kind The assumption kind describing the underlying cause.
2198 /// @param Loc The location in the source that triggered .
2199 void invalidate(AssumptionKind Kind, DebugLoc Loc);
2201 /// Get the invalid context for this Scop.
2203 /// @return The invalid context of this Scop.
2204 __isl_give isl_set *getInvalidContext() const;
2206 /// Return true if and only if the InvalidContext is trivial (=empty).
2207 bool hasTrivialInvalidContext() const {
2208 return isl_set_is_empty(InvalidContext);
2211 /// Build the alias checks for this SCoP.
2212 bool buildAliasChecks(AliasAnalysis &AA);
2214 /// Build all alias groups for this SCoP.
2216 /// @returns True if __no__ error occurred, false otherwise.
2217 bool buildAliasGroups(AliasAnalysis &AA);
2219 /// Return all alias groups for this SCoP.
2220 const MinMaxVectorPairVectorTy &getAliasGroups() const {
2221 return MinMaxAliasGroups;
2224 /// Get an isl string representing the context.
2225 std::string getContextStr() const;
2227 /// Get an isl string representing the assumed context.
2228 std::string getAssumedContextStr() const;
2230 /// Get an isl string representing the invalid context.
2231 std::string getInvalidContextStr() const;
2233 /// Return the ScopStmt for the given @p BB or nullptr if there is
2234 /// none.
2235 ScopStmt *getStmtFor(BasicBlock *BB) const;
2237 /// Return the ScopStmt that represents the Region @p R, or nullptr if
2238 /// it is not represented by any statement in this Scop.
2239 ScopStmt *getStmtFor(Region *R) const;
2241 /// Return the ScopStmt that represents @p RN; can return nullptr if
2242 /// the RegionNode is not within the SCoP or has been removed due to
2243 /// simplifications.
2244 ScopStmt *getStmtFor(RegionNode *RN) const;
2246 /// Return the ScopStmt an instruction belongs to, or nullptr if it
2247 /// does not belong to any statement in this Scop.
2248 ScopStmt *getStmtFor(Instruction *Inst) const {
2249 return getStmtFor(Inst->getParent());
2252 /// Return the number of statements in the SCoP.
2253 size_t getSize() const { return Stmts.size(); }
2255 /// @name Statements Iterators
2257 /// These iterators iterate over all statements of this Scop.
2258 //@{
2259 typedef StmtSet::iterator iterator;
2260 typedef StmtSet::const_iterator const_iterator;
2262 iterator begin() { return Stmts.begin(); }
2263 iterator end() { return Stmts.end(); }
2264 const_iterator begin() const { return Stmts.begin(); }
2265 const_iterator end() const { return Stmts.end(); }
2267 typedef StmtSet::reverse_iterator reverse_iterator;
2268 typedef StmtSet::const_reverse_iterator const_reverse_iterator;
2270 reverse_iterator rbegin() { return Stmts.rbegin(); }
2271 reverse_iterator rend() { return Stmts.rend(); }
2272 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
2273 const_reverse_iterator rend() const { return Stmts.rend(); }
2274 //@}
2276 /// Return the set of required invariant loads.
2277 const InvariantLoadsSetTy &getRequiredInvariantLoads() const {
2278 return DC.RequiredILS;
2281 /// Add @p LI to the set of required invariant loads.
2282 void addRequiredInvariantLoad(LoadInst *LI) { DC.RequiredILS.insert(LI); }
2284 /// Return true if and only if @p LI is a required invariant load.
2285 bool isRequiredInvariantLoad(LoadInst *LI) const {
2286 return getRequiredInvariantLoads().count(LI);
2289 /// Return the set of boxed (thus overapproximated) loops.
2290 const BoxedLoopsSetTy &getBoxedLoops() const { return DC.BoxedLoopsSet; }
2292 /// Return true if and only if @p R is a non-affine subregion.
2293 bool isNonAffineSubRegion(const Region *R) {
2294 return DC.NonAffineSubRegionSet.count(R);
2297 const MapInsnToMemAcc &getInsnToMemAccMap() const { return DC.InsnToMemAcc; }
2299 /// Return the (possibly new) ScopArrayInfo object for @p Access.
2301 /// @param ElementType The type of the elements stored in this array.
2302 /// @param Kind The kind of the array info object.
2303 /// @param BaseName The optional name of this memory reference.
2304 const ScopArrayInfo *getOrCreateScopArrayInfo(Value *BasePtr,
2305 Type *ElementType,
2306 ArrayRef<const SCEV *> Sizes,
2307 ScopArrayInfo::MemoryKind Kind,
2308 const char *BaseName = nullptr);
2310 /// Create an array and return the corresponding ScopArrayInfo object.
2312 /// @param ElementType The type of the elements stored in this array.
2313 /// @param BaseName The name of this memory reference.
2314 /// @param Sizes The sizes of dimensions.
2315 const ScopArrayInfo *createScopArrayInfo(Type *ElementType,
2316 const std::string &BaseName,
2317 const std::vector<unsigned> &Sizes);
2319 /// Return the cached ScopArrayInfo object for @p BasePtr.
2321 /// @param BasePtr The base pointer the object has been stored for.
2322 /// @param Kind The kind of array info object.
2323 const ScopArrayInfo *getScopArrayInfo(Value *BasePtr,
2324 ScopArrayInfo::MemoryKind Kind);
2326 /// Invalidate ScopArrayInfo object for base address.
2328 /// @param BasePtr The base pointer of the ScopArrayInfo object to invalidate.
2329 /// @param Kind The Kind of the ScopArrayInfo object.
2330 void invalidateScopArrayInfo(Value *BasePtr, ScopArrayInfo::MemoryKind Kind) {
2331 auto It = ScopArrayInfoMap.find(std::make_pair(BasePtr, Kind));
2332 if (It == ScopArrayInfoMap.end())
2333 return;
2334 ScopArrayInfoSet.remove(It->second.get());
2335 ScopArrayInfoMap.erase(It);
2338 void setContext(__isl_take isl_set *NewContext);
2340 /// Align the parameters in the statement to the scop context
2341 void realignParams();
2343 /// Return true if this SCoP can be profitably optimized.
2344 bool isProfitable() const;
2346 /// Return true if the SCoP contained at least one error block.
2347 bool hasErrorBlock() const { return HasErrorBlock; }
2349 /// Return true if the underlying region has a single exiting block.
2350 bool hasSingleExitEdge() const { return HasSingleExitEdge; }
2352 /// Print the static control part.
2354 /// @param OS The output stream the static control part is printed to.
2355 void print(raw_ostream &OS) const;
2357 /// Print the ScopStmt to stderr.
2358 void dump() const;
2360 /// Get the isl context of this static control part.
2362 /// @return The isl context of this static control part.
2363 isl_ctx *getIslCtx() const;
2365 /// Directly return the shared_ptr of the context.
2366 const std::shared_ptr<isl_ctx> &getSharedIslCtx() const { return IslCtx; }
2368 /// Compute the isl representation for the SCEV @p E
2370 /// @param E The SCEV that should be translated.
2371 /// @param BB An (optional) basic block in which the isl_pw_aff is computed.
2372 /// SCEVs known to not reference any loops in the SCoP can be
2373 /// passed without a @p BB.
2374 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
2376 /// Note that this function will always return a valid isl_pw_aff. However, if
2377 /// the translation of @p E was deemed to complex the SCoP is invalidated and
2378 /// a dummy value of appropriate dimension is returned. This allows to bail
2379 /// for complex cases without "error handling code" needed on the users side.
2380 __isl_give PWACtx getPwAff(const SCEV *E, BasicBlock *BB = nullptr,
2381 bool NonNegative = false);
2383 /// Compute the isl representation for the SCEV @p E
2385 /// This function is like @see Scop::getPwAff() but strips away the invalid
2386 /// domain part associated with the piecewise affine function.
2387 __isl_give isl_pw_aff *getPwAffOnly(const SCEV *E, BasicBlock *BB = nullptr);
2389 /// Return the domain of @p Stmt.
2391 /// @param Stmt The statement for which the conditions should be returned.
2392 __isl_give isl_set *getDomainConditions(const ScopStmt *Stmt) const;
2394 /// Return the domain of @p BB.
2396 /// @param BB The block for which the conditions should be returned.
2397 __isl_give isl_set *getDomainConditions(BasicBlock *BB) const;
2399 /// Get a union set containing the iteration domains of all statements.
2400 __isl_give isl_union_set *getDomains() const;
2402 /// Get a union map of all may-writes performed in the SCoP.
2403 __isl_give isl_union_map *getMayWrites();
2405 /// Get a union map of all must-writes performed in the SCoP.
2406 __isl_give isl_union_map *getMustWrites();
2408 /// Get a union map of all writes performed in the SCoP.
2409 __isl_give isl_union_map *getWrites();
2411 /// Get a union map of all reads performed in the SCoP.
2412 __isl_give isl_union_map *getReads();
2414 /// Get a union map of all memory accesses performed in the SCoP.
2415 __isl_give isl_union_map *getAccesses();
2417 /// Get the schedule of all the statements in the SCoP.
2419 /// @return The schedule of all the statements in the SCoP, if the schedule of
2420 /// the Scop does not contain extension nodes, and nullptr, otherwise.
2421 __isl_give isl_union_map *getSchedule() const;
2423 /// Get a schedule tree describing the schedule of all statements.
2424 __isl_give isl_schedule *getScheduleTree() const;
2426 /// Update the current schedule
2428 /// NewSchedule The new schedule (given as a flat union-map).
2429 void setSchedule(__isl_take isl_union_map *NewSchedule);
2431 /// Update the current schedule
2433 /// NewSchedule The new schedule (given as schedule tree).
2434 void setScheduleTree(__isl_take isl_schedule *NewSchedule);
2436 /// Intersects the domains of all statements in the SCoP.
2438 /// @return true if a change was made
2439 bool restrictDomains(__isl_take isl_union_set *Domain);
2441 /// Get the depth of a loop relative to the outermost loop in the Scop.
2443 /// This will return
2444 /// 0 if @p L is an outermost loop in the SCoP
2445 /// >0 for other loops in the SCoP
2446 /// -1 if @p L is nullptr or there is no outermost loop in the SCoP
2447 int getRelativeLoopDepth(const Loop *L) const;
2449 /// Find the ScopArrayInfo associated with an isl Id
2450 /// that has name @p Name.
2451 ScopArrayInfo *getArrayInfoByName(const std::string BaseName);
2453 /// Check whether @p Schedule contains extension nodes.
2455 /// @return true if @p Schedule contains extension nodes.
2456 static bool containsExtensionNode(__isl_keep isl_schedule *Schedule);
2458 /// Simplify the SCoP representation.
2460 /// @param AfterHoisting Whether it is called after invariant load hoisting.
2461 /// When true, also removes statements without
2462 /// side-effects.
2463 void simplifySCoP(bool AfterHoisting);
2466 /// Print Scop scop to raw_ostream O.
2467 static inline raw_ostream &operator<<(raw_ostream &O, const Scop &scop) {
2468 scop.print(O);
2469 return O;
2472 /// The legacy pass manager's analysis pass to compute scop information
2473 /// for a region.
2474 class ScopInfoRegionPass : public RegionPass {
2475 /// The Scop pointer which is used to construct a Scop.
2476 std::unique_ptr<Scop> S;
2478 public:
2479 static char ID; // Pass identification, replacement for typeid
2481 ScopInfoRegionPass() : RegionPass(ID) {}
2482 ~ScopInfoRegionPass() {}
2484 /// Build Scop object, the Polly IR of static control
2485 /// part for the current SESE-Region.
2487 /// @return If the current region is a valid for a static control part,
2488 /// return the Polly IR representing this static control part,
2489 /// return null otherwise.
2490 Scop *getScop() { return S.get(); }
2491 const Scop *getScop() const { return S.get(); }
2493 /// Calculate the polyhedral scop information for a given Region.
2494 bool runOnRegion(Region *R, RGPassManager &RGM) override;
2496 void releaseMemory() override { S.reset(); }
2498 void print(raw_ostream &O, const Module *M = nullptr) const override;
2500 void getAnalysisUsage(AnalysisUsage &AU) const override;
2503 //===----------------------------------------------------------------------===//
2504 /// The legacy pass manager's analysis pass to compute scop information
2505 /// for the whole function.
2507 /// This pass will maintain a map of the maximal region within a scop to its
2508 /// scop object for all the feasible scops present in a function.
2509 /// This pass is an alternative to the ScopInfoRegionPass in order to avoid a
2510 /// region pass manager.
2511 class ScopInfoWrapperPass : public FunctionPass {
2513 public:
2514 using RegionToScopMapTy = DenseMap<Region *, std::unique_ptr<Scop>>;
2515 using iterator = RegionToScopMapTy::iterator;
2516 using const_iterator = RegionToScopMapTy::const_iterator;
2518 private:
2519 /// A map of Region to its Scop object containing
2520 /// Polly IR of static control part
2521 RegionToScopMapTy RegionToScopMap;
2523 public:
2524 static char ID; // Pass identification, replacement for typeid
2526 ScopInfoWrapperPass() : FunctionPass(ID) {}
2527 ~ScopInfoWrapperPass() {}
2529 /// Get the Scop object for the given Region
2531 /// @return If the given region is the maximal region within a scop, return
2532 /// the scop object. If the given region is a subregion, return a
2533 /// nullptr. Top level region containing the entry block of a function
2534 /// is not considered in the scop creation.
2535 Scop *getScop(Region *R) const {
2536 auto MapIt = RegionToScopMap.find(R);
2537 if (MapIt != RegionToScopMap.end())
2538 return MapIt->second.get();
2539 return nullptr;
2542 iterator begin() { return RegionToScopMap.begin(); }
2543 iterator end() { return RegionToScopMap.end(); }
2544 const_iterator begin() const { return RegionToScopMap.begin(); }
2545 const_iterator end() const { return RegionToScopMap.end(); }
2547 /// Calculate all the polyhedral scops for a given function.
2548 bool runOnFunction(Function &F) override;
2550 void releaseMemory() override { RegionToScopMap.clear(); }
2552 void print(raw_ostream &O, const Module *M = nullptr) const override;
2554 void getAnalysisUsage(AnalysisUsage &AU) const override;
2557 } // end namespace polly
2559 namespace llvm {
2560 class PassRegistry;
2561 void initializeScopInfoRegionPassPass(llvm::PassRegistry &);
2562 void initializeScopInfoWrapperPassPass(llvm::PassRegistry &);
2563 } // namespace llvm
2565 #endif