Polly 22.0.0git
ScopInfo.h
Go to the documentation of this file.
1//===- polly/ScopInfo.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Store the polyhedral model representation of a static control flow region,
10// also called SCoP (Static Control Part).
11//
12// This representation is shared among several tools in the polyhedral
13// community, which are e.g. CLooG, Pluto, Loopo, Graphite.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef POLLY_SCOPINFO_H
18#define POLLY_SCOPINFO_H
19
20#include "polly/ScopDetection.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/MapVector.h"
25#include "llvm/ADT/SetVector.h"
26#include "llvm/IR/DebugLoc.h"
27#include "llvm/IR/Instruction.h"
28#include "llvm/IR/Instructions.h"
29#include "llvm/IR/ValueHandle.h"
31#include <cassert>
32#include <cstddef>
33#include <forward_list>
34#include <optional>
35
36namespace polly {
37using llvm::AnalysisInfoMixin;
38using llvm::ArrayRef;
39using llvm::AssertingVH;
40using llvm::AssumptionCache;
41using llvm::cast;
42using llvm::DataLayout;
43using llvm::DenseMap;
44using llvm::DenseSet;
45using llvm::function_ref;
46using llvm::isa;
47using llvm::iterator_range;
48using llvm::LoadInst;
49using llvm::make_range;
50using llvm::MapVector;
51using llvm::MemIntrinsic;
52using llvm::PassInfoMixin;
53using llvm::PHINode;
54using llvm::RegionNode;
55using llvm::SetVector;
56using llvm::SmallPtrSetImpl;
57using llvm::SmallVector;
58using llvm::SmallVectorImpl;
59using llvm::StringMap;
60using llvm::Type;
61using llvm::Use;
62using llvm::Value;
63using llvm::ValueToValueMap;
64
65class MemoryAccess;
66
67//===---------------------------------------------------------------------===//
68
69extern bool UseInstructionNames;
70
71// The maximal number of basic sets we allow during domain construction to
72// be created. More complex scops will result in very high compile time and
73// are also unlikely to result in good code.
74extern unsigned const MaxDisjunctsInDomain;
75
76/// The different memory kinds used in Polly.
77///
78/// We distinguish between arrays and various scalar memory objects. We use
79/// the term ``array'' to describe memory objects that consist of a set of
80/// individual data elements arranged in a multi-dimensional grid. A scalar
81/// memory object describes an individual data element and is used to model
82/// the definition and uses of llvm::Values.
83///
84/// The polyhedral model does traditionally not reason about SSA values. To
85/// reason about llvm::Values we model them "as if" they were zero-dimensional
86/// memory objects, even though they were not actually allocated in (main)
87/// memory. Memory for such objects is only alloca[ed] at CodeGeneration
88/// time. To relate the memory slots used during code generation with the
89/// llvm::Values they belong to the new names for these corresponding stack
90/// slots are derived by appending suffixes (currently ".s2a" and ".phiops")
91/// to the name of the original llvm::Value. To describe how def/uses are
92/// modeled exactly we use these suffixes here as well.
93///
94/// There are currently four different kinds of memory objects:
95enum class MemoryKind {
96 /// MemoryKind::Array: Models a one or multi-dimensional array
97 ///
98 /// A memory object that can be described by a multi-dimensional array.
99 /// Memory objects of this type are used to model actual multi-dimensional
100 /// arrays as they exist in LLVM-IR, but they are also used to describe
101 /// other objects:
102 /// - A single data element allocated on the stack using 'alloca' is
103 /// modeled as a one-dimensional, single-element array.
104 /// - A single data element allocated as a global variable is modeled as
105 /// one-dimensional, single-element array.
106 /// - Certain multi-dimensional arrays with variable size, which in
107 /// LLVM-IR are commonly expressed as a single-dimensional access with a
108 /// complicated access function, are modeled as multi-dimensional
109 /// memory objects (grep for "delinearization").
111
112 /// MemoryKind::Value: Models an llvm::Value
113 ///
114 /// Memory objects of type MemoryKind::Value are used to model the data flow
115 /// induced by llvm::Values. For each llvm::Value that is used across
116 /// BasicBlocks, one ScopArrayInfo object is created. A single memory WRITE
117 /// stores the llvm::Value at its definition into the memory object and at
118 /// each use of the llvm::Value (ignoring trivial intra-block uses) a
119 /// corresponding READ is added. For instance, the use/def chain of a
120 /// llvm::Value %V depicted below
121 /// ______________________
122 /// |DefBB: |
123 /// | %V = float op ... |
124 /// ----------------------
125 /// | |
126 /// _________________ _________________
127 /// |UseBB1: | |UseBB2: |
128 /// | use float %V | | use float %V |
129 /// ----------------- -----------------
130 ///
131 /// is modeled as if the following memory accesses occurred:
132 ///
133 /// __________________________
134 /// |entry: |
135 /// | %V.s2a = alloca float |
136 /// --------------------------
137 /// |
138 /// ___________________________________
139 /// |DefBB: |
140 /// | store %float %V, float* %V.s2a |
141 /// -----------------------------------
142 /// | |
143 /// ____________________________________ ___________________________________
144 /// |UseBB1: | |UseBB2: |
145 /// | %V.reload1 = load float* %V.s2a | | %V.reload2 = load float* %V.s2a|
146 /// | use float %V.reload1 | | use float %V.reload2 |
147 /// ------------------------------------ -----------------------------------
148 ///
150
151 /// MemoryKind::PHI: Models PHI nodes within the SCoP
152 ///
153 /// Besides the MemoryKind::Value memory object used to model the normal
154 /// llvm::Value dependences described above, PHI nodes require an additional
155 /// memory object of type MemoryKind::PHI to describe the forwarding of values
156 /// to
157 /// the PHI node.
158 ///
159 /// As an example, a PHIInst instructions
160 ///
161 /// %PHI = phi float [ %Val1, %IncomingBlock1 ], [ %Val2, %IncomingBlock2 ]
162 ///
163 /// is modeled as if the accesses occurred this way:
164 ///
165 /// _______________________________
166 /// |entry: |
167 /// | %PHI.phiops = alloca float |
168 /// -------------------------------
169 /// | |
170 /// __________________________________ __________________________________
171 /// |IncomingBlock1: | |IncomingBlock2: |
172 /// | ... | | ... |
173 /// | store float %Val1 %PHI.phiops | | store float %Val2 %PHI.phiops |
174 /// | br label % JoinBlock | | br label %JoinBlock |
175 /// ---------------------------------- ----------------------------------
176 /// \ /
177 /// \ /
178 /// _________________________________________
179 /// |JoinBlock: |
180 /// | %PHI = load float, float* PHI.phiops |
181 /// -----------------------------------------
182 ///
183 /// Note that there can also be a scalar write access for %PHI if used in a
184 /// different BasicBlock, i.e. there can be a memory object %PHI.phiops as
185 /// well as a memory object %PHI.s2a.
187
188 /// MemoryKind::ExitPHI: Models PHI nodes in the SCoP's exit block
189 ///
190 /// For PHI nodes in the Scop's exit block a special memory object kind is
191 /// used. The modeling used is identical to MemoryKind::PHI, with the
192 /// exception
193 /// that there are no READs from these memory objects. The PHINode's
194 /// llvm::Value is treated as a value escaping the SCoP. WRITE accesses
195 /// write directly to the escaping value's ".s2a" alloca.
197};
198
199/// Maps from a loop to the affine function expressing its backedge taken count.
200/// The backedge taken count already enough to express iteration domain as we
201/// only allow loops with canonical induction variable.
202/// A canonical induction variable is:
203/// an integer recurrence that starts at 0 and increments by one each time
204/// through the loop.
205using LoopBoundMapType = std::map<const Loop *, const SCEV *>;
206
207using AccFuncVector = std::vector<std::unique_ptr<MemoryAccess>>;
208
209/// A class to store information about arrays in the SCoP.
210///
211/// Objects are accessible via the ScoP, MemoryAccess or the id associated with
212/// the MemoryAccess access function.
213///
214class ScopArrayInfo final {
215public:
216 /// Construct a ScopArrayInfo object.
217 ///
218 /// @param BasePtr The array base pointer.
219 /// @param ElementType The type of the elements stored in the array.
220 /// @param IslCtx The isl context used to create the base pointer id.
221 /// @param DimensionSizes A vector containing the size of each dimension.
222 /// @param Kind The kind of the array object.
223 /// @param DL The data layout of the module.
224 /// @param S The scop this array object belongs to.
225 /// @param BaseName The optional name of this memory reference.
227 ArrayRef<const SCEV *> DimensionSizes, MemoryKind Kind,
228 const DataLayout &DL, Scop *S, const char *BaseName = nullptr);
229
230 /// Destructor to free the isl id of the base pointer.
232
233 /// Update the element type of the ScopArrayInfo object.
234 ///
235 /// Memory accesses referencing this ScopArrayInfo object may use
236 /// different element sizes. This function ensures the canonical element type
237 /// stored is small enough to model accesses to the current element type as
238 /// well as to @p NewElementType.
239 ///
240 /// @param NewElementType An element type that is used to access this array.
241 void updateElementType(Type *NewElementType);
242
243 /// Update the sizes of the ScopArrayInfo object.
244 ///
245 /// A ScopArrayInfo object may be created without all outer dimensions being
246 /// available. This function is called when new memory accesses are added for
247 /// this ScopArrayInfo object. It verifies that sizes are compatible and adds
248 /// additional outer array dimensions, if needed.
249 ///
250 /// @param Sizes A vector of array sizes where the rightmost array
251 /// sizes need to match the innermost array sizes already
252 /// defined in SAI.
253 /// @param CheckConsistency Update sizes, even if new sizes are inconsistent
254 /// with old sizes
255 bool updateSizes(ArrayRef<const SCEV *> Sizes, bool CheckConsistency = true);
256
257 /// Set the base pointer to @p BP.
258 void setBasePtr(Value *BP) { BasePtr = BP; }
259
260 /// Return the base pointer.
261 Value *getBasePtr() const { return BasePtr; }
262
263 // Set IsOnHeap to the value in parameter.
264 void setIsOnHeap(bool value) { IsOnHeap = value; }
265
266 /// For indirect accesses return the origin SAI of the BP, else null.
268
269 /// The set of derived indirect SAIs for this origin SAI.
270 const SmallSetVector<ScopArrayInfo *, 2> &getDerivedSAIs() const {
271 return DerivedSAIs;
272 }
273
274 /// Return the number of dimensions.
275 unsigned getNumberOfDimensions() const {
278 return 0;
279 return DimensionSizes.size();
280 }
281
282 /// Return the size of dimension @p dim as SCEV*.
283 //
284 // Scalars do not have array dimensions and the first dimension of
285 // a (possibly multi-dimensional) array also does not carry any size
286 // information, in case the array is not newly created.
287 const SCEV *getDimensionSize(unsigned Dim) const {
288 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
289 return DimensionSizes[Dim];
290 }
291
292 /// Return the size of dimension @p dim as isl::pw_aff.
293 //
294 // Scalars do not have array dimensions and the first dimension of
295 // a (possibly multi-dimensional) array also does not carry any size
296 // information, in case the array is not newly created.
297 isl::pw_aff getDimensionSizePw(unsigned Dim) const {
298 assert(Dim < getNumberOfDimensions() && "Invalid dimension");
299 return DimensionSizesPw[Dim];
300 }
301
302 /// Get the canonical element type of this array.
303 ///
304 /// @returns The canonical element type of this array.
305 Type *getElementType() const { return ElementType; }
306
307 /// Get element size in bytes.
308 int getElemSizeInBytes() const;
309
310 /// Get the name of this memory reference.
311 std::string getName() const;
312
313 /// Return the isl id for the base pointer.
314 isl::id getBasePtrId() const;
315
316 /// Return what kind of memory this represents.
317 MemoryKind getKind() const { return Kind; }
318
319 /// Is this array info modeling an llvm::Value?
320 bool isValueKind() const { return Kind == MemoryKind::Value; }
321
322 /// Is this array info modeling special PHI node memory?
323 ///
324 /// During code generation of PHI nodes, there is a need for two kinds of
325 /// virtual storage. The normal one as it is used for all scalar dependences,
326 /// where the result of the PHI node is stored and later loaded from as well
327 /// as a second one where the incoming values of the PHI nodes are stored
328 /// into and reloaded when the PHI is executed. As both memories use the
329 /// original PHI node as virtual base pointer, we have this additional
330 /// attribute to distinguish the PHI node specific array modeling from the
331 /// normal scalar array modeling.
332 bool isPHIKind() const { return Kind == MemoryKind::PHI; }
333
334 /// Is this array info modeling an MemoryKind::ExitPHI?
335 bool isExitPHIKind() const { return Kind == MemoryKind::ExitPHI; }
336
337 /// Is this array info modeling an array?
338 bool isArrayKind() const { return Kind == MemoryKind::Array; }
339
340 /// Is this array allocated on heap
341 ///
342 /// This property is only relevant if the array is allocated by Polly instead
343 /// of pre-existing. If false, it is allocated using alloca instead malloca.
344 bool isOnHeap() const { return IsOnHeap; }
345
346#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
347 /// Dump a readable representation to stderr.
348 void dump() const;
349#endif
350
351 /// Print a readable representation to @p OS.
352 ///
353 /// @param SizeAsPwAff Print the size as isl::pw_aff
354 void print(raw_ostream &OS, bool SizeAsPwAff = false) const;
355
356 /// Access the ScopArrayInfo associated with an access function.
358
359 /// Access the ScopArrayInfo associated with an isl Id.
360 static const ScopArrayInfo *getFromId(isl::id Id);
361
362 /// Get the space of this array access.
363 isl::space getSpace() const;
364
365 /// If the array is read only
366 bool isReadOnly();
367
368 /// Verify that @p Array is compatible to this ScopArrayInfo.
369 ///
370 /// Two arrays are compatible if their dimensionality, the sizes of their
371 /// dimensions, and their element sizes match.
372 ///
373 /// @param Array The array to compare against.
374 ///
375 /// @returns True, if the arrays are compatible, False otherwise.
376 bool isCompatibleWith(const ScopArrayInfo *Array) const;
377
378private:
379 void addDerivedSAI(ScopArrayInfo *DerivedSAI) {
380 DerivedSAIs.insert(DerivedSAI);
381 }
382
383 /// For indirect accesses this is the SAI of the BP origin.
385
386 /// For origin SAIs the set of derived indirect SAIs.
387 SmallSetVector<ScopArrayInfo *, 2> DerivedSAIs;
388
389 /// The base pointer.
390 AssertingVH<Value> BasePtr;
391
392 /// The canonical element type of this array.
393 ///
394 /// The canonical element type describes the minimal accessible element in
395 /// this array. Not all elements accessed, need to be of the very same type,
396 /// but the allocation size of the type of the elements loaded/stored from/to
397 /// this array needs to be a multiple of the allocation size of the canonical
398 /// type.
400
401 /// The isl id for the base pointer.
403
404 /// True if the newly allocated array is on heap.
405 bool IsOnHeap = false;
406
407 /// The sizes of each dimension as SCEV*.
408 SmallVector<const SCEV *, 4> DimensionSizes;
409
410 /// The sizes of each dimension as isl::pw_aff.
411 SmallVector<isl::pw_aff, 4> DimensionSizesPw;
412
413 /// The type of this scop array info object.
414 ///
415 /// We distinguish between SCALAR, PHI and ARRAY objects.
417
418 /// The data layout of the module.
419 const DataLayout &DL;
420
421 /// The scop this SAI object belongs to.
423};
424
425/// Represent memory accesses in statements.
426class MemoryAccess final {
427 friend class Scop;
428 friend class ScopStmt;
429 friend class ScopBuilder;
430
431public:
432 /// The access type of a memory access
433 ///
434 /// There are three kind of access types:
435 ///
436 /// * A read access
437 ///
438 /// A certain set of memory locations are read and may be used for internal
439 /// calculations.
440 ///
441 /// * A must-write access
442 ///
443 /// A certain set of memory locations is definitely written. The old value is
444 /// replaced by a newly calculated value. The old value is not read or used at
445 /// all.
446 ///
447 /// * A may-write access
448 ///
449 /// A certain set of memory locations may be written. The memory location may
450 /// contain a new value if there is actually a write or the old value may
451 /// remain, if no write happens.
453 READ = 0x1,
456 };
457
458 /// Reduction access type
459 ///
460 /// Commutative and associative binary operations suitable for reductions
462 RT_NONE, ///< Indicate no reduction at all
463 RT_ADD, ///< Addition
464 RT_MUL, ///< Multiplication
465 RT_BOR, ///< Bitwise Or
466 RT_BXOR, ///< Bitwise XOr
467 RT_BAND, ///< Bitwise And
468
469 RT_BOTTOM, ///< Pseudo type for the data flow analysis
470 };
471
472 using SubscriptsTy = SmallVector<const SCEV *, 4>;
473
474private:
475 /// A unique identifier for this memory access.
476 ///
477 /// The identifier is unique between all memory accesses belonging to the same
478 /// scop statement.
480
481 /// What is modeled by this MemoryAccess.
482 /// @see MemoryKind
484
485 /// Whether it a reading or writing access, and if writing, whether it
486 /// is conditional (MAY_WRITE).
488
489 /// Reduction type for reduction like accesses, RT_NONE otherwise
490 ///
491 /// An access is reduction like if it is part of a load-store chain in which
492 /// both access the same memory location (use the same LLVM-IR value
493 /// as pointer reference). Furthermore, between the load and the store there
494 /// is exactly one binary operator which is known to be associative and
495 /// commutative.
496 ///
497 /// TODO:
498 ///
499 /// We can later lift the constraint that the same LLVM-IR value defines the
500 /// memory location to handle scops such as the following:
501 ///
502 /// for i
503 /// for j
504 /// sum[i+j] = sum[i] + 3;
505 ///
506 /// Here not all iterations access the same memory location, but iterations
507 /// for which j = 0 holds do. After lifting the equality check in ScopBuilder,
508 /// subsequent transformations do not only need check if a statement is
509 /// reduction like, but they also need to verify that the reduction
510 /// property is only exploited for statement instances that load from and
511 /// store to the same data location. Doing so at dependence analysis time
512 /// could allow us to handle the above example.
514
515 /// Parent ScopStmt of this access.
517
518 /// The domain under which this access is not modeled precisely.
519 ///
520 /// The invalid domain for an access describes all parameter combinations
521 /// under which the statement looks to be executed but is in fact not because
522 /// some assumption/restriction makes the access invalid.
524
525 // Properties describing the accessed array.
526 // TODO: It might be possible to move them to ScopArrayInfo.
527 // @{
528
529 /// The base address (e.g., A for A[i+j]).
530 ///
531 /// The #BaseAddr of a memory access of kind MemoryKind::Array is the base
532 /// pointer of the memory access.
533 /// The #BaseAddr of a memory access of kind MemoryKind::PHI or
534 /// MemoryKind::ExitPHI is the PHI node itself.
535 /// The #BaseAddr of a memory access of kind MemoryKind::Value is the
536 /// instruction defining the value.
537 AssertingVH<Value> BaseAddr;
538
539 /// Type a single array element wrt. this access.
541
542 /// Size of each dimension of the accessed array.
543 SmallVector<const SCEV *, 4> Sizes;
544 // @}
545
546 // Properties describing the accessed element.
547 // @{
548
549 /// The access instruction of this memory access.
550 ///
551 /// For memory accesses of kind MemoryKind::Array the access instruction is
552 /// the Load or Store instruction performing the access.
553 ///
554 /// For memory accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI the
555 /// access instruction of a load access is the PHI instruction. The access
556 /// instruction of a PHI-store is the incoming's block's terminator
557 /// instruction.
558 ///
559 /// For memory accesses of kind MemoryKind::Value the access instruction of a
560 /// load access is nullptr because generally there can be multiple
561 /// instructions in the statement using the same llvm::Value. The access
562 /// instruction of a write access is the instruction that defines the
563 /// llvm::Value.
564 Instruction *AccessInstruction = nullptr;
565
566 /// Incoming block and value of a PHINode.
567 SmallVector<std::pair<BasicBlock *, Value *>, 4> Incoming;
568
569 /// The value associated with this memory access.
570 ///
571 /// - For array memory accesses (MemoryKind::Array) it is the loaded result
572 /// or the stored value. If the access instruction is a memory intrinsic it
573 /// the access value is also the memory intrinsic.
574 /// - For accesses of kind MemoryKind::Value it is the access instruction
575 /// itself.
576 /// - For accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI it is the
577 /// PHI node itself (for both, READ and WRITE accesses).
578 ///
579 AssertingVH<Value> AccessValue;
580
581 /// Are all the subscripts affine expression?
582 bool IsAffine = true;
583
584 /// Subscript expression for each dimension.
586
587 /// Relation from statement instances to the accessed array elements.
588 ///
589 /// In the common case this relation is a function that maps a set of loop
590 /// indices to the memory address from which a value is loaded/stored:
591 ///
592 /// for i
593 /// for j
594 /// S: A[i + 3 j] = ...
595 ///
596 /// => { S[i,j] -> A[i + 3j] }
597 ///
598 /// In case the exact access function is not known, the access relation may
599 /// also be a one to all mapping { S[i,j] -> A[o] } describing that any
600 /// element accessible through A might be accessed.
601 ///
602 /// In case of an access to a larger element belonging to an array that also
603 /// contains smaller elements, the access relation models the larger access
604 /// with multiple smaller accesses of the size of the minimal array element
605 /// type:
606 ///
607 /// short *A;
608 ///
609 /// for i
610 /// S: A[i] = *((double*)&A[4 * i]);
611 ///
612 /// => { S[i] -> A[i]; S[i] -> A[o] : 4i <= o <= 4i + 3 }
614
615 /// Updated access relation read from JSCOP file.
617 // @}
618
620
622
623 /// Compute bounds on an over approximated access relation.
624 ///
625 /// @param ElementSize The size of one element accessed.
626 void computeBoundsOnAccessRelation(unsigned ElementSize);
627
628 /// Get the original access function as read from IR.
630
631 /// Return the space in which the access relation lives in.
633
634 /// Get the new access function imported or set by a pass
636
637 /// Fold the memory access to consider parametric offsets
638 ///
639 /// To recover memory accesses with array size parameters in the subscript
640 /// expression we post-process the delinearization results.
641 ///
642 /// We would normally recover from an access A[exp0(i) * N + exp1(i)] into an
643 /// array A[][N] the 2D access A[exp0(i)][exp1(i)]. However, another valid
644 /// delinearization is A[exp0(i) - 1][exp1(i) + N] which - depending on the
645 /// range of exp1(i) - may be preferable. Specifically, for cases where we
646 /// know exp1(i) is negative, we want to choose the latter expression.
647 ///
648 /// As we commonly do not have any information about the range of exp1(i),
649 /// we do not choose one of the two options, but instead create a piecewise
650 /// access function that adds the (-1, N) offsets as soon as exp1(i) becomes
651 /// negative. For a 2D array such an access function is created by applying
652 /// the piecewise map:
653 ///
654 /// [i,j] -> [i, j] : j >= 0
655 /// [i,j] -> [i-1, j+N] : j < 0
656 ///
657 /// We can generalize this mapping to arbitrary dimensions by applying this
658 /// piecewise mapping pairwise from the rightmost to the leftmost access
659 /// dimension. It would also be possible to cover a wider range by introducing
660 /// more cases and adding multiple of Ns to these cases. However, this has
661 /// not yet been necessary.
662 /// The introduction of different cases necessarily complicates the memory
663 /// access function, but cases that can be statically proven to not happen
664 /// will be eliminated later on.
665 void foldAccessRelation();
666
667 /// Create the access relation for the underlying memory intrinsic.
669
670 /// Assemble the access relation from all available information.
671 ///
672 /// In particular, used the information passes in the constructor and the
673 /// parent ScopStmt set by setStatment().
674 ///
675 /// @param SAI Info object for the accessed array.
676 void buildAccessRelation(const ScopArrayInfo *SAI);
677
678 /// Carry index overflows of dimensions with constant size to the next higher
679 /// dimension.
680 ///
681 /// For dimensions that have constant size, modulo the index by the size and
682 /// add up the carry (floored division) to the next higher dimension. This is
683 /// how overflow is defined in row-major order.
684 /// It happens e.g. when ScalarEvolution computes the offset to the base
685 /// pointer and would algebraically sum up all lower dimensions' indices of
686 /// constant size.
687 ///
688 /// Example:
689 /// float (*A)[4];
690 /// A[1][6] -> A[2][2]
692
693public:
694 /// Create a new MemoryAccess.
695 ///
696 /// @param Stmt The parent statement.
697 /// @param AccessInst The instruction doing the access.
698 /// @param BaseAddr The accessed array's address.
699 /// @param ElemType The type of the accessed array elements.
700 /// @param AccType Whether read or write access.
701 /// @param IsAffine Whether the subscripts are affine expressions.
702 /// @param Kind The kind of memory accessed.
703 /// @param Subscripts Subscript expressions
704 /// @param Sizes Dimension lengths of the accessed array.
705 MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, AccessType AccType,
706 Value *BaseAddress, Type *ElemType, bool Affine,
707 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
709
710 /// Create a new MemoryAccess that corresponds to @p AccRel.
711 ///
712 /// Along with @p Stmt and @p AccType it uses information about dimension
713 /// lengths of the accessed array, the type of the accessed array elements,
714 /// the name of the accessed array that is derived from the object accessible
715 /// via @p AccRel.
716 ///
717 /// @param Stmt The parent statement.
718 /// @param AccType Whether read or write access.
719 /// @param AccRel The access relation that describes the memory access.
721
722 MemoryAccess(const MemoryAccess &) = delete;
725
726 /// Add a new incoming block/value pairs for this PHI/ExitPHI access.
727 ///
728 /// @param IncomingBlock The PHI's incoming block.
729 /// @param IncomingValue The value when reaching the PHI from the @p
730 /// IncomingBlock.
731 void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue) {
732 assert(!isRead());
734 Incoming.emplace_back(std::make_pair(IncomingBlock, IncomingValue));
735 }
736
737 /// Return the list of possible PHI/ExitPHI values.
738 ///
739 /// After code generation moves some PHIs around during region simplification,
740 /// we cannot reliably locate the original PHI node and its incoming values
741 /// anymore. For this reason we remember these explicitly for all PHI-kind
742 /// accesses.
743 ArrayRef<std::pair<BasicBlock *, Value *>> getIncoming() const {
745 return Incoming;
746 }
747
748 /// Get the type of a memory access.
749 enum AccessType getType() { return AccType; }
750
751 /// Is this a reduction like access?
752 bool isReductionLike() const { return RedType != RT_NONE; }
753
754 /// Is this a read memory access?
755 bool isRead() const { return AccType == MemoryAccess::READ; }
756
757 /// Is this a must-write memory access?
758 bool isMustWrite() const { return AccType == MemoryAccess::MUST_WRITE; }
759
760 /// Is this a may-write memory access?
761 bool isMayWrite() const { return AccType == MemoryAccess::MAY_WRITE; }
762
763 /// Is this a write memory access?
764 bool isWrite() const { return isMustWrite() || isMayWrite(); }
765
766 /// Is this a memory intrinsic access (memcpy, memset, memmove)?
767 bool isMemoryIntrinsic() const {
768 return isa<MemIntrinsic>(getAccessInstruction());
769 }
770
771 /// Check if a new access relation was imported or set by a pass.
772 bool hasNewAccessRelation() const { return !NewAccessRelation.is_null(); }
773
774 /// Return the newest access relation of this access.
775 ///
776 /// There are two possibilities:
777 /// 1) The original access relation read from the LLVM-IR.
778 /// 2) A new access relation imported from a json file or set by another
779 /// pass (e.g., for privatization).
780 ///
781 /// As 2) is by construction "newer" than 1) we return the new access
782 /// relation if present.
783 ///
788
789 /// Old name of getLatestAccessRelation().
791
792 /// Get an isl map describing the memory address accessed.
793 ///
794 /// In most cases the memory address accessed is well described by the access
795 /// relation obtained with getAccessRelation. However, in case of arrays
796 /// accessed with types of different size the access relation maps one access
797 /// to multiple smaller address locations. This method returns an isl map that
798 /// relates each dynamic statement instance to the unique memory location
799 /// that is loaded from / stored to.
800 ///
801 /// For an access relation { S[i] -> A[o] : 4i <= o <= 4i + 3 } this method
802 /// will return the address function { S[i] -> A[4i] }.
803 ///
804 /// @returns The address function for this memory access.
806
807 /// Return the access relation after the schedule was applied.
810
811 /// Get an isl string representing the access function read from IR.
812 std::string getOriginalAccessRelationStr() const;
813
814 /// Get an isl string representing a new access function, if available.
815 std::string getNewAccessRelationStr() const;
816
817 /// Get an isl string representing the latest access relation.
818 std::string getAccessRelationStr() const;
819
820 /// Get the original base address of this access (e.g. A for A[i+j]) when
821 /// detected.
822 ///
823 /// This address may differ from the base address referenced by the original
824 /// ScopArrayInfo to which this array belongs, as this memory access may
825 /// have been canonicalized to a ScopArrayInfo which has a different but
826 /// identically-valued base pointer in case invariant load hoisting is
827 /// enabled.
828 Value *getOriginalBaseAddr() const { return BaseAddr; }
829
830 /// Get the detection-time base array isl::id for this access.
832
833 /// Get the base array isl::id for this access, modifiable through
834 /// setNewAccessRelation().
836
837 /// Old name of getOriginalArrayId().
839
840 /// Get the detection-time ScopArrayInfo object for the base address.
842
843 /// Get the ScopArrayInfo object for the base address, or the one set
844 /// by setNewAccessRelation().
846
847 /// Legacy name of getOriginalScopArrayInfo().
850 }
851
852 /// Return a string representation of the access's reduction type.
853 std::string getReductionOperatorStr() const;
854
855 /// Return a string representation of the reduction type @p RT.
856 static std::string getReductionOperatorStr(ReductionType RT);
857
858 /// Return the element type of the accessed array wrt. this access.
859 Type *getElementType() const { return ElementType; }
860
861 /// Return the access value of this memory access.
862 Value *getAccessValue() const { return AccessValue; }
863
864 /// Return llvm::Value that is stored by this access, if available.
865 ///
866 /// PHI nodes may not have a unique value available that is stored, as in
867 /// case of region statements one out of possibly several llvm::Values
868 /// might be stored. In this case nullptr is returned.
870 assert(isWrite() && "Only write statement store values");
871 if (isAnyPHIKind()) {
872 if (Incoming.size() == 1)
873 return Incoming[0].second;
874 return nullptr;
875 }
876 return AccessValue;
877 }
878
879 /// Return the access instruction of this memory access.
880 Instruction *getAccessInstruction() const { return AccessInstruction; }
881
882 /// Return an iterator range containing the subscripts.
883 iterator_range<SubscriptsTy::const_iterator> subscripts() const {
884 return make_range(Subscripts.begin(), Subscripts.end());
885 }
886
887 /// Return the number of access function subscript.
888 unsigned getNumSubscripts() const { return Subscripts.size(); }
889
890 /// Return the access function subscript in the dimension @p Dim.
891 const SCEV *getSubscript(unsigned Dim) const { return Subscripts[Dim]; }
892
893 /// Compute the isl representation for the SCEV @p E wrt. this access.
894 ///
895 /// Note that this function will also adjust the invalid context accordingly.
896 isl::pw_aff getPwAff(const SCEV *E);
897
898 /// Get the invalid domain for this access.
900
901 /// Get the invalid context for this access.
903
904 /// Get the stride of this memory access in the specified Schedule. Schedule
905 /// is a map from the statement to a schedule where the innermost dimension is
906 /// the dimension of the innermost loop containing the statement.
907 isl::set getStride(isl::map Schedule) const;
908
909 /// Is the stride of the access equal to a certain width? Schedule is a map
910 /// from the statement to a schedule where the innermost dimension is the
911 /// dimension of the innermost loop containing the statement.
912 bool isStrideX(isl::map Schedule, int StrideWidth) const;
913
914 /// Is consecutive memory accessed for a given statement instance set?
915 /// Schedule is a map from the statement to a schedule where the innermost
916 /// dimension is the dimension of the innermost loop containing the
917 /// statement.
918 bool isStrideOne(isl::map Schedule) const;
919
920 /// Is always the same memory accessed for a given statement instance set?
921 /// Schedule is a map from the statement to a schedule where the innermost
922 /// dimension is the dimension of the innermost loop containing the
923 /// statement.
924 bool isStrideZero(isl::map Schedule) const;
925
926 /// Return the kind when this access was first detected.
928 assert(!getOriginalScopArrayInfo() /* not yet initialized */ ||
929 getOriginalScopArrayInfo()->getKind() == Kind);
930 return Kind;
931 }
932
933 /// Return the kind considering a potential setNewAccessRelation.
936 }
937
938 /// Whether this is an access of an explicit load or store in the IR.
939 bool isOriginalArrayKind() const {
941 }
942
943 /// Whether storage memory is either an custom .s2a/.phiops alloca
944 /// (false) or an existing pointer into an array (true).
945 bool isLatestArrayKind() const {
947 }
948
949 /// Old name of isOriginalArrayKind.
950 bool isArrayKind() const { return isOriginalArrayKind(); }
951
952 /// Whether this access is an array to a scalar memory object, without
953 /// considering changes by setNewAccessRelation.
954 ///
955 /// Scalar accesses are accesses to MemoryKind::Value, MemoryKind::PHI or
956 /// MemoryKind::ExitPHI.
957 bool isOriginalScalarKind() const {
959 }
960
961 /// Whether this access is an array to a scalar memory object, also
962 /// considering changes by setNewAccessRelation.
963 bool isLatestScalarKind() const {
965 }
966
967 /// Old name of isOriginalScalarKind.
968 bool isScalarKind() const { return isOriginalScalarKind(); }
969
970 /// Was this MemoryAccess detected as a scalar dependences?
971 bool isOriginalValueKind() const {
973 }
974
975 /// Is this MemoryAccess currently modeling scalar dependences?
976 bool isLatestValueKind() const {
978 }
979
980 /// Old name of isOriginalValueKind().
981 bool isValueKind() const { return isOriginalValueKind(); }
982
983 /// Was this MemoryAccess detected as a special PHI node access?
984 bool isOriginalPHIKind() const {
986 }
987
988 /// Is this MemoryAccess modeling special PHI node accesses, also
989 /// considering a potential change by setNewAccessRelation?
990 bool isLatestPHIKind() const { return getLatestKind() == MemoryKind::PHI; }
991
992 /// Old name of isOriginalPHIKind.
993 bool isPHIKind() const { return isOriginalPHIKind(); }
994
995 /// Was this MemoryAccess detected as the accesses of a PHI node in the
996 /// SCoP's exit block?
999 }
1000
1001 /// Is this MemoryAccess modeling the accesses of a PHI node in the
1002 /// SCoP's exit block? Can be changed to an array access using
1003 /// setNewAccessRelation().
1004 bool isLatestExitPHIKind() const {
1006 }
1007
1008 /// Old name of isOriginalExitPHIKind().
1009 bool isExitPHIKind() const { return isOriginalExitPHIKind(); }
1010
1011 /// Was this access detected as one of the two PHI types?
1014 }
1015
1016 /// Does this access originate from one of the two PHI types? Can be
1017 /// changed to an array access using setNewAccessRelation().
1018 bool isLatestAnyPHIKind() const {
1020 }
1021
1022 /// Old name of isOriginalAnyPHIKind().
1023 bool isAnyPHIKind() const { return isOriginalAnyPHIKind(); }
1024
1025 /// Get the statement that contains this memory access.
1026 ScopStmt *getStatement() const { return Statement; }
1027
1028 /// Get the reduction type of this access
1030
1031 /// Update the original access relation.
1032 ///
1033 /// We need to update the original access relation during scop construction,
1034 /// when unifying the memory accesses that access the same scop array info
1035 /// object. After the scop has been constructed, the original access relation
1036 /// should not be changed any more. Instead setNewAccessRelation should
1037 /// be called.
1039
1040 /// Set the updated access relation read from JSCOP file.
1042
1043 /// Return whether the MemoryyAccess is a partial access. That is, the access
1044 /// is not executed in some instances of the parent statement's domain.
1045 bool isLatestPartialAccess() const;
1046
1047 /// Mark this a reduction like access
1049
1050 /// Align the parameters in the access relation to the scop context
1051 void realignParams();
1052
1053 /// Update the dimensionality of the memory access.
1054 ///
1055 /// During scop construction some memory accesses may not be constructed with
1056 /// their full dimensionality, but outer dimensions may have been omitted if
1057 /// they took the value 'zero'. By updating the dimensionality of the
1058 /// statement we add additional zero-valued dimensions to match the
1059 /// dimensionality of the ScopArrayInfo object that belongs to this memory
1060 /// access.
1061 void updateDimensionality();
1062
1063 /// Get identifier for the memory access.
1064 ///
1065 /// This identifier is unique for all accesses that belong to the same scop
1066 /// statement.
1067 isl::id getId() const;
1068
1069 /// Print the MemoryAccess.
1070 ///
1071 /// @param OS The output stream the MemoryAccess is printed to.
1072 void print(raw_ostream &OS) const;
1073
1074#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1075 /// Print the MemoryAccess to stderr.
1076 void dump() const;
1077#endif
1078
1079 /// Is the memory access affine?
1080 bool isAffine() const { return IsAffine; }
1081};
1082
1083raw_ostream &operator<<(raw_ostream &OS, MemoryAccess::ReductionType RT);
1084
1085/// Ordered list type to hold accesses.
1086using MemoryAccessList = std::forward_list<MemoryAccess *>;
1087
1088/// Helper structure for invariant memory accesses.
1090 /// The memory access that is (partially) invariant.
1092
1093 /// The context under which the access is not invariant.
1095};
1096
1097/// Ordered container type to hold invariant accesses.
1098using InvariantAccessesTy = SmallVector<InvariantAccess, 8>;
1099
1100/// Type for equivalent invariant accesses and their domain context.
1102 /// The pointer that identifies this equivalence class
1104
1105 /// Memory accesses now treated invariant
1106 ///
1107 /// These memory accesses access the pointer location that identifies
1108 /// this equivalence class. They are treated as invariant and hoisted during
1109 /// code generation.
1111
1112 /// The execution context under which the memory location is accessed
1113 ///
1114 /// It is the union of the execution domains of the memory accesses in the
1115 /// InvariantAccesses list.
1117
1118 /// The type of the invariant access
1119 ///
1120 /// It is used to differentiate between differently typed invariant loads from
1121 /// the same location.
1123};
1124
1125/// Type for invariant accesses equivalence classes.
1126using InvariantEquivClassesTy = SmallVector<InvariantEquivClassTy, 8>;
1127
1128/// Statement of the Scop
1129///
1130/// A Scop statement represents an instruction in the Scop.
1131///
1132/// It is further described by its iteration domain, its schedule and its data
1133/// accesses.
1134/// At the moment every statement represents a single basic block of LLVM-IR.
1135class ScopStmt final {
1136 friend class ScopBuilder;
1137
1138public:
1139 using MemoryAccessVec = llvm::SmallVector<MemoryAccess *, 8>;
1140 /// Create the ScopStmt from a BasicBlock.
1141 ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, Loop *SurroundingLoop,
1142 std::vector<Instruction *> Instructions);
1143
1144 /// Create an overapproximating ScopStmt for the region @p R.
1145 ///
1146 /// @param EntryBlockInstructions The list of instructions that belong to the
1147 /// entry block of the region statement.
1148 /// Instructions are only tracked for entry
1149 /// blocks for now. We currently do not allow
1150 /// to modify the instructions of blocks later
1151 /// in the region statement.
1152 ScopStmt(Scop &parent, Region &R, StringRef Name, Loop *SurroundingLoop,
1153 std::vector<Instruction *> EntryBlockInstructions);
1154
1155 /// Create a copy statement.
1156 ///
1157 /// @param Stmt The parent statement.
1158 /// @param SourceRel The source location.
1159 /// @param TargetRel The target location.
1160 /// @param Domain The original domain under which the copy statement would
1161 /// be executed.
1162 ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel,
1164
1165 ScopStmt(const ScopStmt &) = delete;
1166 const ScopStmt &operator=(const ScopStmt &) = delete;
1168
1169private:
1170 /// Polyhedral description
1171 //@{
1172
1173 /// The Scop containing this ScopStmt.
1175
1176 /// The domain under which this statement is not modeled precisely.
1177 ///
1178 /// The invalid domain for a statement describes all parameter combinations
1179 /// under which the statement looks to be executed but is in fact not because
1180 /// some assumption/restriction makes the statement/scop invalid.
1182
1183 /// The iteration domain describes the set of iterations for which this
1184 /// statement is executed.
1185 ///
1186 /// Example:
1187 /// for (i = 0; i < 100 + b; ++i)
1188 /// for (j = 0; j < i; ++j)
1189 /// S(i,j);
1190 ///
1191 /// 'S' is executed for different values of i and j. A vector of all
1192 /// induction variables around S (i, j) is called iteration vector.
1193 /// The domain describes the set of possible iteration vectors.
1194 ///
1195 /// In this case it is:
1196 ///
1197 /// Domain: 0 <= i <= 100 + b
1198 /// 0 <= j <= i
1199 ///
1200 /// A pair of statement and iteration vector (S, (5,3)) is called statement
1201 /// instance.
1203
1204 /// The memory accesses of this statement.
1205 ///
1206 /// The only side effects of a statement are its memory accesses.
1208
1209 /// Mapping from instructions to (scalar) memory accesses.
1210 DenseMap<const Instruction *, MemoryAccessList> InstructionToAccess;
1211
1212 /// The set of values defined elsewhere required in this ScopStmt and
1213 /// their MemoryKind::Value READ MemoryAccesses.
1214 DenseMap<Value *, MemoryAccess *> ValueReads;
1215
1216 /// The set of values defined in this ScopStmt that are required
1217 /// elsewhere, mapped to their MemoryKind::Value WRITE MemoryAccesses.
1218 DenseMap<Instruction *, MemoryAccess *> ValueWrites;
1219
1220 /// Map from PHI nodes to its incoming value when coming from this
1221 /// statement.
1222 ///
1223 /// Non-affine subregions can have multiple exiting blocks that are incoming
1224 /// blocks of the PHI nodes. This map ensures that there is only one write
1225 /// operation for the complete subregion. A PHI selecting the relevant value
1226 /// will be inserted.
1227 DenseMap<PHINode *, MemoryAccess *> PHIWrites;
1228
1229 /// Map from PHI nodes to its read access in this statement.
1230 DenseMap<PHINode *, MemoryAccess *> PHIReads;
1231
1232 //@}
1233
1234 /// A SCoP statement represents either a basic block (affine/precise case) or
1235 /// a whole region (non-affine case).
1236 ///
1237 /// Only one of the following two members will therefore be set and indicate
1238 /// which kind of statement this is.
1239 ///
1240 ///{
1241
1242 /// The BasicBlock represented by this statement (in the affine case).
1243 BasicBlock *BB = nullptr;
1244
1245 /// The region represented by this statement (in the non-affine case).
1246 Region *R = nullptr;
1247
1248 ///}
1249
1250 /// The isl AST build for the new generated AST.
1252
1253 SmallVector<Loop *, 4> NestLoops;
1254
1255 std::string BaseName;
1256
1257 /// The closest loop that contains this statement.
1259
1260 /// Vector for Instructions in this statement.
1261 std::vector<Instruction *> Instructions;
1262
1263 /// Remove @p MA from dictionaries pointing to them.
1265
1266public:
1267 /// Get an isl_ctx pointer.
1268 isl::ctx getIslCtx() const;
1269
1270 /// Get the iteration domain of this ScopStmt.
1271 ///
1272 /// @return The iteration domain of this ScopStmt.
1273 isl::set getDomain() const;
1274
1275 /// Get the space of the iteration domain
1276 ///
1277 /// @return The space of the iteration domain
1278 isl::space getDomainSpace() const;
1279
1280 /// Get the id of the iteration domain space
1281 ///
1282 /// @return The id of the iteration domain space
1283 isl::id getDomainId() const;
1284
1285 /// Get an isl string representing this domain.
1286 std::string getDomainStr() const;
1287
1288 /// Get the schedule function of this ScopStmt.
1289 ///
1290 /// @return The schedule function of this ScopStmt, if it does not contain
1291 /// extension nodes, and nullptr, otherwise.
1292 isl::map getSchedule() const;
1293
1294 /// Get an isl string representing this schedule.
1295 ///
1296 /// @return An isl string representing this schedule, if it does not contain
1297 /// extension nodes, and an empty string, otherwise.
1298 std::string getScheduleStr() const;
1299
1300 /// Get the invalid domain for this statement.
1302
1303 /// Get the invalid context for this statement.
1305
1306 /// Set the invalid context for this statement to @p ID.
1307 void setInvalidDomain(isl::set ID);
1308
1309 /// Get the BasicBlock represented by this ScopStmt (if any).
1310 ///
1311 /// @return The BasicBlock represented by this ScopStmt, or null if the
1312 /// statement represents a region.
1313 BasicBlock *getBasicBlock() const { return BB; }
1314
1315 /// Return true if this statement represents a single basic block.
1316 bool isBlockStmt() const { return BB != nullptr; }
1317
1318 /// Return true if this is a copy statement.
1319 bool isCopyStmt() const { return BB == nullptr && R == nullptr; }
1320
1321 /// Get the region represented by this ScopStmt (if any).
1322 ///
1323 /// @return The region represented by this ScopStmt, or null if the statement
1324 /// represents a basic block.
1325 Region *getRegion() const { return R; }
1326
1327 /// Return true if this statement represents a whole region.
1328 bool isRegionStmt() const { return R != nullptr; }
1329
1330 /// Return a BasicBlock from this statement.
1331 ///
1332 /// For block statements, it returns the BasicBlock itself. For subregion
1333 /// statements, return its entry block.
1334 BasicBlock *getEntryBlock() const;
1335
1336 /// Return whether @p L is boxed within this statement.
1337 bool contains(const Loop *L) const {
1338 // Block statements never contain loops.
1339 if (isBlockStmt())
1340 return false;
1341
1342 return getRegion()->contains(L);
1343 }
1344
1345 /// Return whether this statement represents @p BB.
1346 bool represents(BasicBlock *BB) const {
1347 if (isCopyStmt())
1348 return false;
1349 if (isBlockStmt())
1350 return BB == getBasicBlock();
1351 return getRegion()->contains(BB);
1352 }
1353
1354 /// Return whether this statement contains @p Inst.
1355 bool contains(Instruction *Inst) const {
1356 if (!Inst)
1357 return false;
1358 if (isBlockStmt())
1359 return llvm::is_contained(Instructions, Inst);
1360 return represents(Inst->getParent());
1361 }
1362
1363 /// Return the closest innermost loop that contains this statement, but is not
1364 /// contained in it.
1365 ///
1366 /// For block statement, this is just the loop that contains the block. Region
1367 /// statements can contain boxed loops, so getting the loop of one of the
1368 /// region's BBs might return such an inner loop. For instance, the region's
1369 /// entry could be a header of a loop, but the region might extend to BBs
1370 /// after the loop exit. Similarly, the region might only contain parts of the
1371 /// loop body and still include the loop header.
1372 ///
1373 /// Most of the time the surrounding loop is the top element of #NestLoops,
1374 /// except when it is empty. In that case it return the loop that the whole
1375 /// SCoP is contained in. That can be nullptr if there is no such loop.
1376 Loop *getSurroundingLoop() const {
1377 assert(!isCopyStmt() &&
1378 "No surrounding loop for artificially created statements");
1379 return SurroundingLoop;
1380 }
1381
1382 /// Return true if this statement does not contain any accesses.
1383 bool isEmpty() const { return MemAccs.empty(); }
1384
1385 /// Find all array accesses for @p Inst.
1386 ///
1387 /// @param Inst The instruction accessing an array.
1388 ///
1389 /// @return A list of array accesses (MemoryKind::Array) accessed by @p Inst.
1390 /// If there is no such access, it returns nullptr.
1391 const MemoryAccessList *
1392 lookupArrayAccessesFor(const Instruction *Inst) const {
1393 auto It = InstructionToAccess.find(Inst);
1394 if (It == InstructionToAccess.end())
1395 return nullptr;
1396 if (It->second.empty())
1397 return nullptr;
1398 return &It->second;
1399 }
1400
1401 /// Return the only array access for @p Inst, if existing.
1402 ///
1403 /// @param Inst The instruction for which to look up the access.
1404 /// @returns The unique array memory access related to Inst or nullptr if
1405 /// no array access exists
1406 MemoryAccess *getArrayAccessOrNULLFor(const Instruction *Inst) const {
1407 auto It = InstructionToAccess.find(Inst);
1408 if (It == InstructionToAccess.end())
1409 return nullptr;
1410
1411 MemoryAccess *ArrayAccess = nullptr;
1412
1413 for (auto Access : It->getSecond()) {
1414 if (!Access->isArrayKind())
1415 continue;
1416
1417 assert(!ArrayAccess && "More then one array access for instruction");
1418
1419 ArrayAccess = Access;
1420 }
1421
1422 return ArrayAccess;
1423 }
1424
1425 /// Return the only array access for @p Inst.
1426 ///
1427 /// @param Inst The instruction for which to look up the access.
1428 /// @returns The unique array memory access related to Inst.
1429 MemoryAccess &getArrayAccessFor(const Instruction *Inst) const {
1430 MemoryAccess *ArrayAccess = getArrayAccessOrNULLFor(Inst);
1431
1432 assert(ArrayAccess && "No array access found for instruction!");
1433 return *ArrayAccess;
1434 }
1435
1436 /// Return the MemoryAccess that writes the value of an instruction
1437 /// defined in this statement, or nullptr if not existing, respectively
1438 /// not yet added.
1439 MemoryAccess *lookupValueWriteOf(Instruction *Inst) const {
1440 assert((isRegionStmt() && R->contains(Inst)) ||
1441 (!isRegionStmt() && Inst->getParent() == BB));
1442 return ValueWrites.lookup(Inst);
1443 }
1444
1445 /// Return the MemoryAccess that reloads a value, or nullptr if not
1446 /// existing, respectively not yet added.
1448 return ValueReads.lookup(Inst);
1449 }
1450
1451 /// Return the MemoryAccess that loads a PHINode value, or nullptr if not
1452 /// existing, respectively not yet added.
1454 return PHIReads.lookup(PHI);
1455 }
1456
1457 /// Return the PHI write MemoryAccess for the incoming values from any
1458 /// basic block in this ScopStmt, or nullptr if not existing,
1459 /// respectively not yet added.
1461 assert(isBlockStmt() || R->getExit() == PHI->getParent());
1462 return PHIWrites.lookup(PHI);
1463 }
1464
1465 /// Return the input access of the value, or null if no such MemoryAccess
1466 /// exists.
1467 ///
1468 /// The input access is the MemoryAccess that makes an inter-statement value
1469 /// available in this statement by reading it at the start of this statement.
1470 /// This can be a MemoryKind::Value if defined in another statement or a
1471 /// MemoryKind::PHI if the value is a PHINode in this statement.
1473 if (isa<PHINode>(Val))
1474 if (auto InputMA = lookupPHIReadOf(cast<PHINode>(Val))) {
1475 assert(!lookupValueReadOf(Val) && "input accesses must be unique; a "
1476 "statement cannot read a .s2a and "
1477 ".phiops simultaneously");
1478 return InputMA;
1479 }
1480
1481 if (auto *InputMA = lookupValueReadOf(Val))
1482 return InputMA;
1483
1484 return nullptr;
1485 }
1486
1487 /// Add @p Access to this statement's list of accesses.
1488 ///
1489 /// @param Access The access to add.
1490 /// @param Prepend If true, will add @p Access before all other instructions
1491 /// (instead of appending it).
1492 void addAccess(MemoryAccess *Access, bool Prepend = false);
1493
1494 /// Remove a MemoryAccess from this statement.
1495 ///
1496 /// Note that scalar accesses that are caused by MA will
1497 /// be eliminated too.
1499
1500 /// Remove @p MA from this statement.
1501 ///
1502 /// In contrast to removeMemoryAccess(), no other access will be eliminated.
1503 ///
1504 /// @param MA The MemoryAccess to be removed.
1505 /// @param AfterHoisting If true, also remove from data access lists.
1506 /// These lists are filled during
1507 /// ScopBuilder::buildAccessRelations. Therefore, if this
1508 /// method is called before buildAccessRelations, false
1509 /// must be passed.
1510 void removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting = true);
1511
1512 using iterator = MemoryAccessVec::iterator;
1513 using const_iterator = MemoryAccessVec::const_iterator;
1514
1515 iterator begin() { return MemAccs.begin(); }
1516 iterator end() { return MemAccs.end(); }
1517 const_iterator begin() const { return MemAccs.begin(); }
1518 const_iterator end() const { return MemAccs.end(); }
1519 size_t size() const { return MemAccs.size(); }
1520
1521 unsigned getNumIterators() const;
1522
1523 Scop *getParent() { return &Parent; }
1524 const Scop *getParent() const { return &Parent; }
1525
1526 const std::vector<Instruction *> &getInstructions() const {
1527 return Instructions;
1528 }
1529
1530 /// Set the list of instructions for this statement. It replaces the current
1531 /// list.
1532 void setInstructions(ArrayRef<Instruction *> Range) {
1533 Instructions.assign(Range.begin(), Range.end());
1534 }
1535
1536 std::vector<Instruction *>::const_iterator insts_begin() const {
1537 return Instructions.begin();
1538 }
1539
1540 std::vector<Instruction *>::const_iterator insts_end() const {
1541 return Instructions.end();
1542 }
1543
1544 /// The range of instructions in this statement.
1545 iterator_range<std::vector<Instruction *>::const_iterator> insts() const {
1546 return {insts_begin(), insts_end()};
1547 }
1548
1549 /// Insert an instruction before all other instructions in this statement.
1550 void prependInstruction(Instruction *Inst) {
1551 Instructions.insert(Instructions.begin(), Inst);
1552 }
1553
1554 const char *getBaseName() const;
1555
1556 /// Set the isl AST build.
1558
1559 /// Get the isl AST build.
1561
1562 /// Restrict the domain of the statement.
1563 ///
1564 /// @param NewDomain The new statement domain.
1565 void restrictDomain(isl::set NewDomain);
1566
1567 /// Get the loop for a dimension.
1568 ///
1569 /// @param Dimension The dimension of the induction variable
1570 /// @return The loop at a certain dimension.
1571 Loop *getLoopForDimension(unsigned Dimension) const;
1572
1573 /// Align the parameters in the statement to the scop context
1574 void realignParams();
1575
1576 /// Print the ScopStmt.
1577 ///
1578 /// @param OS The output stream the ScopStmt is printed to.
1579 /// @param PrintInstructions Whether to print the statement's instructions as
1580 /// well.
1581 void print(raw_ostream &OS, bool PrintInstructions) const;
1582
1583 /// Print the instructions in ScopStmt.
1584 ///
1585 void printInstructions(raw_ostream &OS) const;
1586
1587 /// Check whether there is a value read access for @p V in this statement, and
1588 /// if not, create one.
1589 ///
1590 /// This allows to add MemoryAccesses after the initial creation of the Scop
1591 /// by ScopBuilder.
1592 ///
1593 /// @return The already existing or newly created MemoryKind::Value READ
1594 /// MemoryAccess.
1595 ///
1596 /// @see ScopBuilder::ensureValueRead(Value*,ScopStmt*)
1598
1599#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1600 /// Print the ScopStmt to stderr.
1601 void dump() const;
1602#endif
1603};
1604
1605/// Print ScopStmt S to raw_ostream OS.
1606raw_ostream &operator<<(raw_ostream &OS, const ScopStmt &S);
1607
1608/// Static Control Part
1609///
1610/// A Scop is the polyhedral representation of a control flow region detected
1611/// by the Scop detection. It is generated by translating the LLVM-IR and
1612/// abstracting its effects.
1613///
1614/// A Scop consists of a set of:
1615///
1616/// * A set of statements executed in the Scop.
1617///
1618/// * A set of global parameters
1619/// Those parameters are scalar integer values, which are constant during
1620/// execution.
1621///
1622/// * A context
1623/// This context contains information about the values the parameters
1624/// can take and relations between different parameters.
1625class Scop final {
1626public:
1627 /// Type to represent a pair of minimal/maximal access to an array.
1628 using MinMaxAccessTy = std::pair<isl::pw_multi_aff, isl::pw_multi_aff>;
1629
1630 /// Vector of minimal/maximal accesses to different arrays.
1631 using MinMaxVectorTy = SmallVector<MinMaxAccessTy, 4>;
1632
1633 /// Pair of minimal/maximal access vectors representing
1634 /// read write and read only accesses
1635 using MinMaxVectorPairTy = std::pair<MinMaxVectorTy, MinMaxVectorTy>;
1636
1637 /// Vector of pair of minimal/maximal access vectors representing
1638 /// non read only and read only accesses for each alias group.
1639 using MinMaxVectorPairVectorTy = SmallVector<MinMaxVectorPairTy, 4>;
1640
1641private:
1642 friend class ScopBuilder;
1643
1644 /// Isl context.
1645 ///
1646 /// We need a shared_ptr with reference counter to delete the context when all
1647 /// isl objects are deleted. We will distribute the shared_ptr to all objects
1648 /// that use the context to create isl objects, and increase the reference
1649 /// counter. By doing this, we guarantee that the context is deleted when we
1650 /// delete the last object that creates isl objects with the context. This
1651 /// declaration needs to be the first in class to gracefully destroy all isl
1652 /// objects before the context.
1653 std::shared_ptr<isl_ctx> IslCtx;
1654
1655 ScalarEvolution *SE;
1656 DominatorTree *DT;
1657
1658 /// The underlying Region.
1659 Region &R;
1660
1661 /// The name of the SCoP (identical to the regions name)
1662 std::optional<std::string> name;
1663
1664 // Access functions of the SCoP.
1665 //
1666 // This owns all the MemoryAccess objects of the Scop created in this pass.
1668
1669 /// Flag to indicate that the scheduler actually optimized the SCoP.
1670 bool IsOptimized = false;
1671
1672 /// True if the underlying region has a single exiting block.
1674
1675 /// Flag to remember if the SCoP contained an error block or not.
1676 bool HasErrorBlock = false;
1677
1678 /// Max loop depth.
1679 unsigned MaxLoopDepth = 0;
1680
1681 /// Number of copy statements.
1682 unsigned CopyStmtsNum = 0;
1683
1684 using StmtSet = std::list<ScopStmt>;
1685
1686 /// The statements in this Scop.
1688
1689 /// Parameters of this Scop
1691
1692 /// Mapping from parameters to their ids.
1693 DenseMap<const SCEV *, isl::id> ParameterIds;
1694
1695 /// The context of the SCoP created during SCoP detection.
1697
1698 /// OptimizationRemarkEmitter object for displaying diagnostic remarks
1699 OptimizationRemarkEmitter &ORE;
1700
1701 /// A map from basic blocks to vector of SCoP statements. Currently this
1702 /// vector comprises only of a single statement.
1703 DenseMap<BasicBlock *, std::vector<ScopStmt *>> StmtMap;
1704
1705 /// A map from instructions to SCoP statements.
1706 DenseMap<Instruction *, ScopStmt *> InstStmtMap;
1707
1708 /// A map from basic blocks to their domains.
1709 DenseMap<BasicBlock *, isl::set> DomainMap;
1710
1711 /// Constraints on parameters.
1713
1714 /// The affinator used to translate SCEVs to isl expressions.
1716
1718 std::map<std::pair<AssertingVH<const Value>, MemoryKind>,
1719 std::unique_ptr<ScopArrayInfo>>;
1720
1721 using ArrayNameMapTy = StringMap<std::unique_ptr<ScopArrayInfo>>;
1722
1723 using ArrayInfoSetTy = SetVector<ScopArrayInfo *>;
1724
1725 /// A map to remember ScopArrayInfo objects for all base pointers.
1726 ///
1727 /// As PHI nodes may have two array info objects associated, we add a flag
1728 /// that distinguishes between the PHI node specific ArrayInfo object
1729 /// and the normal one.
1731
1732 /// A map to remember ScopArrayInfo objects for all names of memory
1733 /// references.
1735
1736 /// A set to remember ScopArrayInfo objects.
1737 /// @see Scop::ScopArrayInfoMap
1739
1740 /// The assumptions under which this scop was built.
1741 ///
1742 /// When constructing a scop sometimes the exact representation of a statement
1743 /// or condition would be very complex, but there is a common case which is a
1744 /// lot simpler, but which is only valid under certain assumptions. The
1745 /// assumed context records the assumptions taken during the construction of
1746 /// this scop and that need to be code generated as a run-time test.
1748
1749 /// The restrictions under which this SCoP was built.
1750 ///
1751 /// The invalid context is similar to the assumed context as it contains
1752 /// constraints over the parameters. However, while we need the constraints
1753 /// in the assumed context to be "true" the constraints in the invalid context
1754 /// need to be "false". Otherwise they behave the same.
1756
1757 /// The context under which the SCoP must have defined behavior. Optimizer and
1758 /// code generator can assume that the SCoP will only be executed with
1759 /// parameter values within this context. This might be either because we can
1760 /// prove that other values are impossible or explicitly have undefined
1761 /// behavior, such as due to no-wrap flags. If this becomes too complex, can
1762 /// also be nullptr.
1763 ///
1764 /// In contrast to Scop::AssumedContext and Scop::InvalidContext, these do not
1765 /// need to be checked at runtime.
1766 ///
1767 /// Scop::Context on the other side is an overapproximation and does not
1768 /// include all requirements, but is always defined. However, there is still
1769 /// no guarantee that there is no undefined behavior in
1770 /// DefinedBehaviorContext.
1772
1773 /// The schedule of the SCoP
1774 ///
1775 /// The schedule of the SCoP describes the execution order of the statements
1776 /// in the scop by assigning each statement instance a possibly
1777 /// multi-dimensional execution time. The schedule is stored as a tree of
1778 /// schedule nodes.
1779 ///
1780 /// The most common nodes in a schedule tree are so-called band nodes. Band
1781 /// nodes map statement instances into a multi dimensional schedule space.
1782 /// This space can be seen as a multi-dimensional clock.
1783 ///
1784 /// Example:
1785 ///
1786 /// <S,(5,4)> may be mapped to (5,4) by this schedule:
1787 ///
1788 /// s0 = i (Year of execution)
1789 /// s1 = j (Day of execution)
1790 ///
1791 /// or to (9, 20) by this schedule:
1792 ///
1793 /// s0 = i + j (Year of execution)
1794 /// s1 = 20 (Day of execution)
1795 ///
1796 /// The order statement instances are executed is defined by the
1797 /// schedule vectors they are mapped to. A statement instance
1798 /// <A, (i, j, ..)> is executed before a statement instance <B, (i', ..)>, if
1799 /// the schedule vector of A is lexicographic smaller than the schedule
1800 /// vector of B.
1801 ///
1802 /// Besides band nodes, schedule trees contain additional nodes that specify
1803 /// a textual ordering between two subtrees or filter nodes that filter the
1804 /// set of statement instances that will be scheduled in a subtree. There
1805 /// are also several other nodes. A full description of the different nodes
1806 /// in a schedule tree is given in the isl manual.
1808
1809 /// Is this Scop marked as not to be transformed by an optimization heuristic?
1811
1812 /// Whether the schedule has been modified after derived from the CFG by
1813 /// ScopBuilder.
1814 bool ScheduleModified = false;
1815
1816 /// The set of minimal/maximal accesses for each alias group.
1817 ///
1818 /// When building runtime alias checks we look at all memory instructions and
1819 /// build so called alias groups. Each group contains a set of accesses to
1820 /// different base arrays which might alias with each other. However, between
1821 /// alias groups there is no aliasing possible.
1822 ///
1823 /// In a program with int and float pointers annotated with tbaa information
1824 /// we would probably generate two alias groups, one for the int pointers and
1825 /// one for the float pointers.
1826 ///
1827 /// During code generation we will create a runtime alias check for each alias
1828 /// group to ensure the SCoP is executed in an alias free environment.
1830
1831 /// Mapping from invariant loads to the representing invariant load of
1832 /// their equivalence class.
1833 ValueToValueMap InvEquivClassVMap;
1834
1835 /// List of invariant accesses.
1837
1838 /// The smallest array index not yet assigned.
1839 long ArrayIdx = 0;
1840
1841 /// The smallest statement index not yet assigned.
1842 long StmtIdx = 0;
1843
1844 /// A number that uniquely represents a Scop within its function
1845 const int ID;
1846
1847 /// Map of values to the MemoryAccess that writes its definition.
1848 ///
1849 /// There must be at most one definition per llvm::Instruction in a SCoP.
1850 DenseMap<Value *, MemoryAccess *> ValueDefAccs;
1851
1852 /// Map of values to the MemoryAccess that reads a PHI.
1853 DenseMap<PHINode *, MemoryAccess *> PHIReadAccs;
1854
1855 /// List of all uses (i.e. read MemoryAccesses) for a MemoryKind::Value
1856 /// scalar.
1857 DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>> ValueUseAccs;
1858
1859 /// List of all incoming values (write MemoryAccess) of a MemoryKind::PHI or
1860 /// MemoryKind::ExitPHI scalar.
1861 DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>>
1863
1864 /// Scop constructor; invoked from ScopBuilder::buildScop.
1865 Scop(Region &R, ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT,
1866 ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE,
1867 int ID);
1868
1869 //@}
1870
1871 /// Return the access for the base ptr of @p MA if any.
1873
1874 /// Create an id for @p Param and store it in the ParameterIds map.
1875 void createParameterId(const SCEV *Param);
1876
1877 /// Build the Context of the Scop.
1878 void buildContext();
1879
1880 /// Add the bounds of the parameters to the context.
1881 void addParameterBounds();
1882
1883 /// Simplify the assumed and invalid context.
1884 void simplifyContexts();
1885
1886 /// Create a new SCoP statement for @p BB.
1887 ///
1888 /// A new statement for @p BB will be created and added to the statement
1889 /// vector
1890 /// and map.
1891 ///
1892 /// @param BB The basic block we build the statement for.
1893 /// @param Name The name of the new statement.
1894 /// @param SurroundingLoop The loop the created statement is contained in.
1895 /// @param Instructions The instructions in the statement.
1896 void addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop,
1897 std::vector<Instruction *> Instructions);
1898
1899 /// Create a new SCoP statement for @p R.
1900 ///
1901 /// A new statement for @p R will be created and added to the statement vector
1902 /// and map.
1903 ///
1904 /// @param R The region we build the statement for.
1905 /// @param Name The name of the new statement.
1906 /// @param SurroundingLoop The loop the created statement is contained
1907 /// in.
1908 /// @param EntryBlockInstructions The (interesting) instructions in the
1909 /// entry block of the region statement.
1910 void addScopStmt(Region *R, StringRef Name, Loop *SurroundingLoop,
1911 std::vector<Instruction *> EntryBlockInstructions);
1912
1913 /// Removes @p Stmt from the StmtMap.
1914 void removeFromStmtMap(ScopStmt &Stmt);
1915
1916 /// Removes all statements where the entry block of the statement does not
1917 /// have a corresponding domain in the domain map (or it is empty).
1919
1920 /// Collect all memory access relations of a given type.
1921 ///
1922 /// @param Predicate A predicate function that returns true if an access is
1923 /// of a given type.
1924 ///
1925 /// @returns The set of memory accesses in the scop that match the predicate.
1927 getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate);
1928
1929 /// @name Helper functions for printing the Scop.
1930 ///
1931 //@{
1932 void printContext(raw_ostream &OS) const;
1933 void printArrayInfo(raw_ostream &OS) const;
1934 void printStatements(raw_ostream &OS, bool PrintInstructions) const;
1935 void printAliasAssumptions(raw_ostream &OS) const;
1936 //@}
1937
1938public:
1939 Scop(const Scop &) = delete;
1940 Scop &operator=(const Scop &) = delete;
1942
1943 /// Increment actual number of aliasing assumptions taken
1944 ///
1945 /// @param Step Number of new aliasing assumptions which should be added to
1946 /// the number of already taken assumptions.
1947 static void incrementNumberOfAliasingAssumptions(unsigned Step);
1948
1949 /// Get the count of copy statements added to this Scop.
1950 ///
1951 /// @return The count of copy statements added to this Scop.
1952 unsigned getCopyStmtsNum() { return CopyStmtsNum; }
1953
1954 /// Create a new copy statement.
1955 ///
1956 /// A new statement will be created and added to the statement vector.
1957 ///
1958 /// @param SourceRel The source location.
1959 /// @param TargetRel The target location.
1960 /// @param Domain The original domain under which the copy statement would
1961 /// be executed.
1962 ScopStmt *addScopStmt(isl::map SourceRel, isl::map TargetRel,
1964
1965 /// Add the access function to all MemoryAccess objects of the Scop
1966 /// created in this pass.
1968 AccessFunctions.emplace_back(Access);
1969
1970 // Register value definitions.
1971 if (Access->isWrite() && Access->isOriginalValueKind()) {
1972 assert(!ValueDefAccs.count(Access->getAccessValue()) &&
1973 "there can be just one definition per value");
1974 ValueDefAccs[Access->getAccessValue()] = Access;
1975 } else if (Access->isRead() && Access->isOriginalPHIKind()) {
1976 PHINode *PHI = cast<PHINode>(Access->getAccessInstruction());
1977 assert(!PHIReadAccs.count(PHI) &&
1978 "there can be just one PHI read per PHINode");
1979 PHIReadAccs[PHI] = Access;
1980 }
1981 }
1982
1983 /// Add metadata for @p Access.
1984 void addAccessData(MemoryAccess *Access);
1985
1986 /// Add new invariant access equivalence class
1987 void
1988 addInvariantEquivClass(const InvariantEquivClassTy &InvariantEquivClass) {
1989 InvariantEquivClasses.emplace_back(InvariantEquivClass);
1990 }
1991
1992 /// Add mapping from invariant loads to the representing invariant load of
1993 /// their equivalence class.
1994 void addInvariantLoadMapping(const Value *LoadInst, Value *ClassRep) {
1995 InvEquivClassVMap[LoadInst] = ClassRep;
1996 }
1997
1998 /// Remove the metadata stored for @p Access.
1999 void removeAccessData(MemoryAccess *Access);
2000
2001 /// Return the scalar evolution.
2002 ScalarEvolution *getSE() const;
2003
2004 /// Return the dominator tree.
2005 DominatorTree *getDT() const { return DT; }
2006
2007 /// Return the LoopInfo used for this Scop.
2008 LoopInfo *getLI() const { return Affinator.getLI(); }
2009
2010 /// Get the count of parameters used in this Scop.
2011 ///
2012 /// @return The count of parameters used in this Scop.
2013 size_t getNumParams() const { return Parameters.size(); }
2014
2015 /// Return whether given SCEV is used as the parameter in this Scop.
2016 bool isParam(const SCEV *Param) const { return Parameters.count(Param); }
2017
2018 /// Take a list of parameters and add the new ones to the scop.
2019 void addParams(const ParameterSetTy &NewParameters);
2020
2021 /// Return an iterator range containing the scop parameters.
2022 iterator_range<ParameterSetTy::iterator> parameters() const {
2023 return make_range(Parameters.begin(), Parameters.end());
2024 }
2025
2026 /// Return an iterator range containing invariant accesses.
2027 iterator_range<InvariantEquivClassesTy::iterator> invariantEquivClasses() {
2028 return make_range(InvariantEquivClasses.begin(),
2029 InvariantEquivClasses.end());
2030 }
2031
2032 /// Return an iterator range containing all the MemoryAccess objects of the
2033 /// Scop.
2034 iterator_range<AccFuncVector::iterator> access_functions() {
2035 return make_range(AccessFunctions.begin(), AccessFunctions.end());
2036 }
2037
2038 /// Return whether this scop is empty, i.e. contains no statements that
2039 /// could be executed.
2040 bool isEmpty() const { return Stmts.empty(); }
2041
2042 StringRef getName() {
2043 if (!name)
2044 name = R.getNameStr();
2045 return *name;
2046 }
2047
2048 using array_iterator = ArrayInfoSetTy::iterator;
2049 using const_array_iterator = ArrayInfoSetTy::const_iterator;
2050 using array_range = iterator_range<ArrayInfoSetTy::iterator>;
2051 using const_array_range = iterator_range<ArrayInfoSetTy::const_iterator>;
2052
2053 inline array_iterator array_begin() { return ScopArrayInfoSet.begin(); }
2054
2055 inline array_iterator array_end() { return ScopArrayInfoSet.end(); }
2056
2058 return ScopArrayInfoSet.begin();
2059 }
2060
2062 return ScopArrayInfoSet.end();
2063 }
2064
2066 return array_range(array_begin(), array_end());
2067 }
2068
2069 inline const_array_range arrays() const {
2071 }
2072
2073 /// Return the isl_id that represents a certain parameter.
2074 ///
2075 /// @param Parameter A SCEV that was recognized as a Parameter.
2076 ///
2077 /// @return The corresponding isl_id or NULL otherwise.
2078 isl::id getIdForParam(const SCEV *Parameter) const;
2079
2080 /// Get the maximum region of this static control part.
2081 ///
2082 /// @return The maximum region of this static control part.
2083 inline const Region &getRegion() const { return R; }
2084 inline Region &getRegion() { return R; }
2085
2086 /// Return the function this SCoP is in.
2087 Function &getFunction() const { return *R.getEntry()->getParent(); }
2088
2089 /// Check if @p L is contained in the SCoP.
2090 bool contains(const Loop *L) const { return R.contains(L); }
2091
2092 /// Check if @p BB is contained in the SCoP.
2093 bool contains(const BasicBlock *BB) const { return R.contains(BB); }
2094
2095 /// Check if @p I is contained in the SCoP.
2096 bool contains(const Instruction *I) const { return R.contains(I); }
2097
2098 /// Return the unique exit block of the SCoP.
2099 BasicBlock *getExit() const { return R.getExit(); }
2100
2101 /// Return the unique exiting block of the SCoP if any.
2102 BasicBlock *getExitingBlock() const { return R.getExitingBlock(); }
2103
2104 /// Return the unique entry block of the SCoP.
2105 BasicBlock *getEntry() const { return R.getEntry(); }
2106
2107 /// Return the unique entering block of the SCoP if any.
2108 BasicBlock *getEnteringBlock() const { return R.getEnteringBlock(); }
2109
2110 /// Return true if @p BB is the exit block of the SCoP.
2111 bool isExit(BasicBlock *BB) const { return getExit() == BB; }
2112
2113 /// Return a range of all basic blocks in the SCoP.
2114 Region::block_range blocks() const { return R.blocks(); }
2115
2116 /// Return true if and only if @p BB dominates the SCoP.
2117 bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const;
2118
2119 /// Get the maximum depth of the loop.
2120 ///
2121 /// @return The maximum depth of the loop.
2122 inline unsigned getMaxLoopDepth() const { return MaxLoopDepth; }
2123
2124 /// Return the invariant equivalence class for @p Val if any.
2126
2127 /// Return the set of invariant accesses.
2131
2132 /// Check if the scop has any invariant access.
2134
2135 /// Mark the SCoP as optimized by the scheduler.
2136 void markAsOptimized() { IsOptimized = true; }
2137
2138 /// Check if the SCoP has been optimized by the scheduler.
2139 bool isOptimized() const { return IsOptimized; }
2140
2141 /// Return the ID of the Scop
2142 int getID() const { return ID; }
2143
2144 /// Get the name of the entry and exit blocks of this Scop.
2145 ///
2146 /// These along with the function name can uniquely identify a Scop.
2147 ///
2148 /// @return std::pair whose first element is the entry name & second element
2149 /// is the exit name.
2150 std::pair<std::string, std::string> getEntryExitStr() const;
2151
2152 /// Get the name of this Scop.
2153 std::string getNameStr() const;
2154
2155 /// Get the constraint on parameter of this Scop.
2156 ///
2157 /// @return The constraint on parameter of this Scop.
2158 isl::set getContext() const;
2159
2160 /// Return the context where execution behavior is defined. Might return
2161 /// nullptr.
2163
2164 /// Return the define behavior context, or if not available, its approximation
2165 /// from all other contexts.
2167 if (!DefinedBehaviorContext.is_null())
2169
2170 return Context.intersect_params(AssumedContext).subtract(InvalidContext);
2171 }
2172
2173 /// Return space of isl context parameters.
2174 ///
2175 /// Returns the set of context parameters that are currently constrained. In
2176 /// case the full set of parameters is needed, see @getFullParamSpace.
2177 isl::space getParamSpace() const;
2178
2179 /// Return the full space of parameters.
2180 ///
2181 /// getParamSpace will only return the parameters of the context that are
2182 /// actually constrained, whereas getFullParamSpace will return all
2183 // parameters. This is useful in cases, where we need to ensure all
2184 // parameters are available, as certain isl functions will abort if this is
2185 // not the case.
2187
2188 /// Get the assumed context for this Scop.
2189 ///
2190 /// @return The assumed context of this Scop.
2192
2193 /// Return true if the optimized SCoP can be executed.
2194 ///
2195 /// In addition to the runtime check context this will also utilize the domain
2196 /// constraints to decide it the optimized version can actually be executed.
2197 ///
2198 /// @returns True if the optimized SCoP can be executed.
2199 bool hasFeasibleRuntimeContext() const;
2200
2201 /// Check if the assumption in @p Set is trivial or not.
2202 ///
2203 /// @param Set The relations between parameters that are assumed to hold.
2204 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2205 /// (needed/assumptions) or negative (invalid/restrictions).
2206 ///
2207 /// @returns True if the assumption @p Set is not trivial.
2209
2210 /// Track and report an assumption.
2211 ///
2212 /// Use 'clang -Rpass-analysis=polly-scops' or 'opt
2213 /// -pass-remarks-analysis=polly-scops' to output the assumptions.
2214 ///
2215 /// @param Kind The assumption kind describing the underlying cause.
2216 /// @param Set The relations between parameters that are assumed to hold.
2217 /// @param Loc The location in the source that caused this assumption.
2218 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2219 /// (needed/assumptions) or negative (invalid/restrictions).
2220 /// @param BB The block in which this assumption was taken. Used to
2221 /// calculate hotness when emitting remark.
2222 ///
2223 /// @returns True if the assumption is not trivial.
2224 bool trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc,
2225 AssumptionSign Sign, BasicBlock *BB);
2226
2227 /// Add the conditions from @p Set (or subtract them if @p Sign is
2228 /// AS_RESTRICTION) to the defined behaviour context.
2230
2231 /// Add assumptions to assumed context.
2232 ///
2233 /// The assumptions added will be assumed to hold during the execution of the
2234 /// scop. However, as they are generally not statically provable, at code
2235 /// generation time run-time checks will be generated that ensure the
2236 /// assumptions hold.
2237 ///
2238 /// WARNING: We currently exploit in simplifyAssumedContext the knowledge
2239 /// that assumptions do not change the set of statement instances
2240 /// executed.
2241 ///
2242 /// @param Kind The assumption kind describing the underlying cause.
2243 /// @param Set The relations between parameters that are assumed to hold.
2244 /// @param Loc The location in the source that caused this assumption.
2245 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2246 /// (needed/assumptions) or negative (invalid/restrictions).
2247 /// @param BB The block in which this assumption was taken. Used to
2248 /// calculate hotness when emitting remark.
2249 /// @param RTC Does the assumption require a runtime check?
2250 void addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc,
2251 AssumptionSign Sign, BasicBlock *BB, bool RTC = true);
2252
2253 /// Mark the scop as invalid.
2254 ///
2255 /// This method adds an assumption to the scop that is always invalid. As a
2256 /// result, the scop will not be optimized later on. This function is commonly
2257 /// called when a condition makes it impossible (or too compile time
2258 /// expensive) to process this scop any further.
2259 ///
2260 /// @param Kind The assumption kind describing the underlying cause.
2261 /// @param Loc The location in the source that triggered .
2262 /// @param BB The BasicBlock where it was triggered.
2263 void invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB = nullptr);
2264
2265 /// Get the invalid context for this Scop.
2266 ///
2267 /// @return The invalid context of this Scop.
2269
2270 /// Return true if and only if the InvalidContext is trivial (=empty).
2271 bool hasTrivialInvalidContext() const { return InvalidContext.is_empty(); }
2272
2273 /// Return all alias groups for this SCoP.
2275 return MinMaxAliasGroups;
2276 }
2277
2278 void addAliasGroup(MinMaxVectorTy &MinMaxAccessesReadWrite,
2279 MinMaxVectorTy &MinMaxAccessesReadOnly) {
2280 MinMaxAliasGroups.emplace_back();
2281 MinMaxAliasGroups.back().first = MinMaxAccessesReadWrite;
2282 MinMaxAliasGroups.back().second = MinMaxAccessesReadOnly;
2283 }
2284
2285 /// Remove statements from the list of scop statements.
2286 ///
2287 /// @param ShouldDelete A function that returns true if the statement passed
2288 /// to it should be deleted.
2289 /// @param AfterHoisting If true, also remove from data access lists.
2290 /// These lists are filled during
2291 /// ScopBuilder::buildAccessRelations. Therefore, if this
2292 /// method is called before buildAccessRelations, false
2293 /// must be passed.
2294 void removeStmts(function_ref<bool(ScopStmt &)> ShouldDelete,
2295 bool AfterHoisting = true);
2296
2297 /// Get an isl string representing the context.
2298 std::string getContextStr() const;
2299
2300 /// Get an isl string representing the assumed context.
2301 std::string getAssumedContextStr() const;
2302
2303 /// Get an isl string representing the invalid context.
2304 std::string getInvalidContextStr() const;
2305
2306 /// Return the list of ScopStmts that represent the given @p BB.
2307 ArrayRef<ScopStmt *> getStmtListFor(BasicBlock *BB) const;
2308
2309 /// Get the statement to put a PHI WRITE into.
2310 ///
2311 /// @param U The operand of a PHINode.
2312 ScopStmt *getIncomingStmtFor(const Use &U) const;
2313
2314 /// Return the last statement representing @p BB.
2315 ///
2316 /// Of the sequence of statements that represent a @p BB, this is the last one
2317 /// to be executed. It is typically used to determine which instruction to add
2318 /// a MemoryKind::PHI WRITE to. For this purpose, it is not strictly required
2319 /// to be executed last, only that the incoming value is available in it.
2320 ScopStmt *getLastStmtFor(BasicBlock *BB) const;
2321
2322 /// Return the ScopStmts that represents the Region @p R, or nullptr if
2323 /// it is not represented by any statement in this Scop.
2324 ArrayRef<ScopStmt *> getStmtListFor(Region *R) const;
2325
2326 /// Return the ScopStmts that represents @p RN; can return nullptr if
2327 /// the RegionNode is not within the SCoP or has been removed due to
2328 /// simplifications.
2329 ArrayRef<ScopStmt *> getStmtListFor(RegionNode *RN) const;
2330
2331 /// Return the ScopStmt an instruction belongs to, or nullptr if it
2332 /// does not belong to any statement in this Scop.
2333 ScopStmt *getStmtFor(Instruction *Inst) const {
2334 return InstStmtMap.lookup(Inst);
2335 }
2336
2337 /// Return the number of statements in the SCoP.
2338 size_t getSize() const { return Stmts.size(); }
2339
2340 /// @name Statements Iterators
2341 ///
2342 /// These iterators iterate over all statements of this Scop.
2343 //@{
2344 using iterator = StmtSet::iterator;
2345 using const_iterator = StmtSet::const_iterator;
2346
2347 iterator begin() { return Stmts.begin(); }
2348 iterator end() { return Stmts.end(); }
2349 const_iterator begin() const { return Stmts.begin(); }
2350 const_iterator end() const { return Stmts.end(); }
2351
2352 using reverse_iterator = StmtSet::reverse_iterator;
2353 using const_reverse_iterator = StmtSet::const_reverse_iterator;
2354
2355 reverse_iterator rbegin() { return Stmts.rbegin(); }
2356 reverse_iterator rend() { return Stmts.rend(); }
2357 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
2358 const_reverse_iterator rend() const { return Stmts.rend(); }
2359 //@}
2360
2361 /// Return the set of required invariant loads.
2363 return DC.RequiredILS;
2364 }
2365
2366 /// Add @p LI to the set of required invariant loads.
2367 void addRequiredInvariantLoad(LoadInst *LI) { DC.RequiredILS.insert(LI); }
2368
2369 /// Return the set of boxed (thus overapproximated) loops.
2370 const BoxedLoopsSetTy &getBoxedLoops() const { return DC.BoxedLoopsSet; }
2371
2372 /// Return true if and only if @p R is a non-affine subregion.
2373 bool isNonAffineSubRegion(const Region *R) {
2374 return DC.NonAffineSubRegionSet.count(R);
2375 }
2376
2377 const MapInsnToMemAcc &getInsnToMemAccMap() const { return DC.InsnToMemAcc; }
2378
2379 /// Return the (possibly new) ScopArrayInfo object for @p Access.
2380 ///
2381 /// @param ElementType The type of the elements stored in this array.
2382 /// @param Kind The kind of the array info object.
2383 /// @param BaseName The optional name of this memory reference.
2384 ScopArrayInfo *getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType,
2385 ArrayRef<const SCEV *> Sizes,
2387 const char *BaseName = nullptr);
2388
2389 /// Create an array and return the corresponding ScopArrayInfo object.
2390 ///
2391 /// @param ElementType The type of the elements stored in this array.
2392 /// @param BaseName The name of this memory reference.
2393 /// @param Sizes The sizes of dimensions.
2394 ScopArrayInfo *createScopArrayInfo(Type *ElementType,
2395 const std::string &BaseName,
2396 const std::vector<unsigned> &Sizes);
2397
2398 /// Return the cached ScopArrayInfo object for @p BasePtr.
2399 ///
2400 /// @param BasePtr The base pointer the object has been stored for.
2401 /// @param Kind The kind of array info object.
2402 ///
2403 /// @returns The ScopArrayInfo pointer or NULL if no such pointer is
2404 /// available.
2406
2407 /// Return the cached ScopArrayInfo object for @p BasePtr.
2408 ///
2409 /// @param BasePtr The base pointer the object has been stored for.
2410 /// @param Kind The kind of array info object.
2411 ///
2412 /// @returns The ScopArrayInfo pointer (may assert if no such pointer is
2413 /// available).
2415
2416 /// Invalidate ScopArrayInfo object for base address.
2417 ///
2418 /// @param BasePtr The base pointer of the ScopArrayInfo object to invalidate.
2419 /// @param Kind The Kind of the ScopArrayInfo object.
2421 auto It = ScopArrayInfoMap.find(std::make_pair(BasePtr, Kind));
2422 if (It == ScopArrayInfoMap.end())
2423 return;
2424 ScopArrayInfoSet.remove(It->second.get());
2425 ScopArrayInfoMap.erase(It);
2426 }
2427
2428 /// Set new isl context.
2429 void setContext(isl::set NewContext);
2430
2431 /// Update maximal loop depth. If @p Depth is smaller than current value,
2432 /// then maximal loop depth is not updated.
2433 void updateMaxLoopDepth(unsigned Depth) {
2434 MaxLoopDepth = std::max(MaxLoopDepth, Depth);
2435 }
2436
2437 /// Align the parameters in the statement to the scop context
2438 void realignParams();
2439
2440 /// Return true if this SCoP can be profitably optimized.
2441 ///
2442 /// @param ScalarsAreUnprofitable Never consider statements with scalar writes
2443 /// as profitably optimizable.
2444 ///
2445 /// @return Whether this SCoP can be profitably optimized.
2446 bool isProfitable(bool ScalarsAreUnprofitable) const;
2447
2448 /// Return true if the SCoP contained at least one error block.
2449 bool hasErrorBlock() const { return HasErrorBlock; }
2450
2451 /// Notify SCoP that it contains an error block
2453
2454 /// Return true if the underlying region has a single exiting block.
2455 bool hasSingleExitEdge() const { return HasSingleExitEdge; }
2456
2457 /// Print the static control part.
2458 ///
2459 /// @param OS The output stream the static control part is printed to.
2460 /// @param PrintInstructions Whether to print the statement's instructions as
2461 /// well.
2462 void print(raw_ostream &OS, bool PrintInstructions) const;
2463
2464#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2465 /// Print the ScopStmt to stderr.
2466 void dump() const;
2467#endif
2468
2469 /// Get the isl context of this static control part.
2470 ///
2471 /// @return The isl context of this static control part.
2472 isl::ctx getIslCtx() const;
2473
2474 /// Directly return the shared_ptr of the context.
2475 const std::shared_ptr<isl_ctx> &getSharedIslCtx() const { return IslCtx; }
2476
2477 /// Compute the isl representation for the SCEV @p E
2478 ///
2479 /// @param E The SCEV that should be translated.
2480 /// @param BB An (optional) basic block in which the isl_pw_aff is computed.
2481 /// SCEVs known to not reference any loops in the SCoP can be
2482 /// passed without a @p BB.
2483 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
2484 ///
2485 /// Note that this function will always return a valid isl_pw_aff. However, if
2486 /// the translation of @p E was deemed to complex the SCoP is invalidated and
2487 /// a dummy value of appropriate dimension is returned. This allows to bail
2488 /// for complex cases without "error handling code" needed on the users side.
2489 PWACtx getPwAff(const SCEV *E, BasicBlock *BB = nullptr,
2490 bool NonNegative = false,
2491 RecordedAssumptionsTy *RecordedAssumptions = nullptr);
2492
2493 /// Compute the isl representation for the SCEV @p E
2494 ///
2495 /// This function is like @see Scop::getPwAff() but strips away the invalid
2496 /// domain part associated with the piecewise affine function.
2498 getPwAffOnly(const SCEV *E, BasicBlock *BB = nullptr,
2499 RecordedAssumptionsTy *RecordedAssumptions = nullptr);
2500
2501 /// Check if an <nsw> AddRec for the loop L is cached.
2502 bool hasNSWAddRecForLoop(Loop *L) { return Affinator.hasNSWAddRecForLoop(L); }
2503
2504 /// Return the domain of @p Stmt.
2505 ///
2506 /// @param Stmt The statement for which the conditions should be returned.
2507 isl::set getDomainConditions(const ScopStmt *Stmt) const;
2508
2509 /// Return the domain of @p BB.
2510 ///
2511 /// @param BB The block for which the conditions should be returned.
2512 isl::set getDomainConditions(BasicBlock *BB) const;
2513
2514 /// Return the domain of @p BB. If it does not exist, create an empty one.
2515 isl::set &getOrInitEmptyDomain(BasicBlock *BB) { return DomainMap[BB]; }
2516
2517 /// Check if domain is determined for @p BB.
2518 bool isDomainDefined(BasicBlock *BB) const { return DomainMap.count(BB) > 0; }
2519
2520 /// Set domain for @p BB.
2521 void setDomain(BasicBlock *BB, isl::set &Domain) { DomainMap[BB] = Domain; }
2522
2523 /// Get a union set containing the iteration domains of all statements.
2524 isl::union_set getDomains() const;
2525
2526 /// Get a union map of all may-writes performed in the SCoP.
2528
2529 /// Get a union map of all must-writes performed in the SCoP.
2531
2532 /// Get a union map of all writes performed in the SCoP.
2534
2535 /// Get a union map of all reads performed in the SCoP.
2537
2538 /// Get a union map of all memory accesses performed in the SCoP.
2540
2541 /// Get a union map of all memory accesses performed in the SCoP.
2542 ///
2543 /// @param Array The array to which the accesses should belong.
2545
2546 /// Get the schedule of all the statements in the SCoP.
2547 ///
2548 /// @return The schedule of all the statements in the SCoP, if the schedule of
2549 /// the Scop does not contain extension nodes, and nullptr, otherwise.
2551
2552 /// Get a schedule tree describing the schedule of all statements.
2554
2555 /// Update the current schedule
2556 ///
2557 /// NewSchedule The new schedule (given as a flat union-map).
2558 void setSchedule(isl::union_map NewSchedule);
2559
2560 /// Update the current schedule
2561 ///
2562 /// NewSchedule The new schedule (given as schedule tree).
2563 void setScheduleTree(isl::schedule NewSchedule);
2564
2565 /// Whether the schedule is the original schedule as derived from the CFG by
2566 /// ScopBuilder.
2567 bool isOriginalSchedule() const { return !ScheduleModified; }
2568
2569 /// Intersects the domains of all statements in the SCoP.
2570 ///
2571 /// @return true if a change was made
2573
2574 /// Get the depth of a loop relative to the outermost loop in the Scop.
2575 ///
2576 /// This will return
2577 /// 0 if @p L is an outermost loop in the SCoP
2578 /// >0 for other loops in the SCoP
2579 /// -1 if @p L is nullptr or there is no outermost loop in the SCoP
2580 int getRelativeLoopDepth(const Loop *L) const;
2581
2582 /// Find the ScopArrayInfo associated with an isl Id
2583 /// that has name @p Name.
2584 ScopArrayInfo *getArrayInfoByName(const std::string BaseName);
2585
2586 /// Simplify the SCoP representation.
2587 ///
2588 /// @param AfterHoisting Whether it is called after invariant load hoisting.
2589 /// When true, also removes statements without
2590 /// side-effects.
2591 void simplifySCoP(bool AfterHoisting);
2592
2593 /// Get the next free array index.
2594 ///
2595 /// This function returns a unique index which can be used to identify an
2596 /// array.
2597 long getNextArrayIdx() { return ArrayIdx++; }
2598
2599 /// Get the next free statement index.
2600 ///
2601 /// This function returns a unique index which can be used to identify a
2602 /// statement.
2603 long getNextStmtIdx() { return StmtIdx++; }
2604
2605 /// Get the representing SCEV for @p S if applicable, otherwise @p S.
2606 ///
2607 /// Invariant loads of the same location are put in an equivalence class and
2608 /// only one of them is chosen as a representing element that will be
2609 /// modeled as a parameter. The others have to be normalized, i.e.,
2610 /// replaced by the representing element of their equivalence class, in order
2611 /// to get the correct parameter value, e.g., in the SCEVAffinator.
2612 ///
2613 /// @param S The SCEV to normalize.
2614 ///
2615 /// @return The representing SCEV for invariant loads or @p S if none.
2616 const SCEV *getRepresentingInvariantLoadSCEV(const SCEV *S) const;
2617
2618 /// Return the MemoryAccess that writes an llvm::Value, represented by a
2619 /// ScopArrayInfo.
2620 ///
2621 /// There can be at most one such MemoryAccess per llvm::Value in the SCoP.
2622 /// Zero is possible for read-only values.
2623 MemoryAccess *getValueDef(const ScopArrayInfo *SAI) const;
2624
2625 /// Return all MemoryAccesses that us an llvm::Value, represented by a
2626 /// ScopArrayInfo.
2627 ArrayRef<MemoryAccess *> getValueUses(const ScopArrayInfo *SAI) const;
2628
2629 /// Return the MemoryAccess that represents an llvm::PHINode.
2630 ///
2631 /// ExitPHIs's PHINode is not within the SCoPs. This function returns nullptr
2632 /// for them.
2633 MemoryAccess *getPHIRead(const ScopArrayInfo *SAI) const;
2634
2635 /// Return all MemoryAccesses for all incoming statements of a PHINode,
2636 /// represented by a ScopArrayInfo.
2637 ArrayRef<MemoryAccess *> getPHIIncomings(const ScopArrayInfo *SAI) const;
2638
2639 /// Return whether @p Inst has a use outside of this SCoP.
2640 bool isEscaping(Instruction *Inst);
2641
2653
2654 /// Collect statistic about this SCoP.
2655 ///
2656 /// These are most commonly used for LLVM's static counters (Statistic.h) in
2657 /// various places. If statistics are disabled, only zeros are returned to
2658 /// avoid the overhead.
2660
2661 /// Is this Scop marked as not to be transformed by an optimization heuristic?
2662 /// In this case, only user-directed transformations are allowed.
2664
2665 /// Mark this Scop to not apply an optimization heuristic.
2667};
2668
2669/// Print Scop scop to raw_ostream OS.
2670raw_ostream &operator<<(raw_ostream &OS, const Scop &scop);
2671
2673public:
2674 using RegionToScopMapTy = MapVector<Region *, std::unique_ptr<Scop>>;
2675 using reverse_iterator = RegionToScopMapTy::reverse_iterator;
2676 using const_reverse_iterator = RegionToScopMapTy::const_reverse_iterator;
2677 using iterator = RegionToScopMapTy::iterator;
2678 using const_iterator = RegionToScopMapTy::const_iterator;
2679
2680private:
2681 /// A map of Region to its Scop object containing
2682 /// Polly IR of static control part.
2684 const DataLayout &DL;
2686 ScalarEvolution &SE;
2687 LoopInfo &LI;
2688 AAResults &AA;
2689 DominatorTree &DT;
2690 AssumptionCache &AC;
2691 OptimizationRemarkEmitter &ORE;
2692
2693public:
2694 ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE,
2695 LoopInfo &LI, AAResults &AA, DominatorTree &DT, AssumptionCache &AC,
2696 OptimizationRemarkEmitter &ORE);
2697
2698 /// Get the Scop object for the given Region.
2699 ///
2700 /// @return If the given region is the maximal region within a scop, return
2701 /// the scop object. If the given region is a subregion, return a
2702 /// nullptr. Top level region containing the entry block of a function
2703 /// is not considered in the scop creation.
2704 Scop *getScop(Region *R) const {
2705 auto MapIt = RegionToScopMap.find(R);
2706 if (MapIt != RegionToScopMap.end())
2707 return MapIt->second.get();
2708 return nullptr;
2709 }
2710
2711 /// Recompute the Scop-Information for a function.
2712 ///
2713 /// This invalidates any iterators.
2714 void recompute();
2715
2716 /// Handle invalidation explicitly
2717 bool invalidate(Function &F, const PreservedAnalyses &PA,
2718 FunctionAnalysisManager::Invalidator &Inv);
2719
2720 iterator begin() { return RegionToScopMap.begin(); }
2721 iterator end() { return RegionToScopMap.end(); }
2722 const_iterator begin() const { return RegionToScopMap.begin(); }
2723 const_iterator end() const { return RegionToScopMap.end(); }
2726 const_reverse_iterator rbegin() const { return RegionToScopMap.rbegin(); }
2728 bool empty() const { return RegionToScopMap.empty(); }
2729};
2730
2731struct ScopInfoAnalysis : AnalysisInfoMixin<ScopInfoAnalysis> {
2732 static AnalysisKey Key;
2733
2735
2736 Result run(Function &, FunctionAnalysisManager &);
2737};
2738
2739struct ScopInfoPrinterPass final : PassInfoMixin<ScopInfoPrinterPass> {
2740 ScopInfoPrinterPass(raw_ostream &OS) : Stream(OS) {}
2741
2742 PreservedAnalyses run(Function &, FunctionAnalysisManager &);
2743
2744 raw_ostream &Stream;
2745};
2746} // end namespace polly
2747
2748#endif // POLLY_SCOPINFO_H
isl::set params() const
Represent memory accesses in statements.
Definition ScopInfo.h:426
const ScopArrayInfo * getLatestScopArrayInfo() const
Get the ScopArrayInfo object for the base address, or the one set by setNewAccessRelation().
Definition ScopInfo.cpp:557
std::string getAccessRelationStr() const
Get an isl string representing the latest access relation.
Definition ScopInfo.cpp:610
void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue)
Add a new incoming block/value pairs for this PHI/ExitPHI access.
Definition ScopInfo.h:731
isl::map getNewAccessRelation() const
Get the new access function imported or set by a pass.
Definition ScopInfo.cpp:602
void dump() const
Print the MemoryAccess to stderr.
Definition ScopInfo.cpp:952
isl::set assumeNoOutOfBound()
Definition ScopInfo.cpp:644
isl::id getArrayId() const
Old name of getOriginalArrayId().
Definition ScopInfo.h:838
SmallVector< const SCEV *, 4 > Sizes
Size of each dimension of the accessed array.
Definition ScopInfo.h:543
bool isOriginalArrayKind() const
Whether this is an access of an explicit load or store in the IR.
Definition ScopInfo.h:939
void foldAccessRelation()
Fold the memory access to consider parametric offsets.
Definition ScopInfo.cpp:747
AssertingVH< Value > AccessValue
The value associated with this memory access.
Definition ScopInfo.h:579
bool isOriginalValueKind() const
Was this MemoryAccess detected as a scalar dependences?
Definition ScopInfo.h:971
MemoryAccess & operator=(const MemoryAccess &)=delete
bool isLatestPHIKind() const
Is this MemoryAccess modeling special PHI node accesses, also considering a potential change by setNe...
Definition ScopInfo.h:990
isl::space getOriginalAccessRelationSpace() const
Return the space in which the access relation lives in.
Definition ScopInfo.cpp:598
bool isAnyPHIKind() const
Old name of isOriginalAnyPHIKind().
Definition ScopInfo.h:1023
friend class Scop
Definition ScopInfo.h:427
AccessType
The access type of a memory access.
Definition ScopInfo.h:452
void markAsReductionLike(ReductionType RT)
Mark this a reduction like access.
Definition ScopInfo.h:1048
ReductionType
Reduction access type.
Definition ScopInfo.h:461
@ RT_BOTTOM
Pseudo type for the data flow analysis.
Definition ScopInfo.h:469
@ RT_BOR
Bitwise Or.
Definition ScopInfo.h:465
@ RT_BAND
Bitwise And.
Definition ScopInfo.h:467
@ RT_ADD
Addition.
Definition ScopInfo.h:463
@ RT_BXOR
Bitwise XOr.
Definition ScopInfo.h:466
@ RT_NONE
Indicate no reduction at all.
Definition ScopInfo.h:462
@ RT_MUL
Multiplication.
Definition ScopInfo.h:464
isl::basic_map createBasicAccessMap(ScopStmt *Statement)
Definition ScopInfo.cpp:614
SubscriptsTy Subscripts
Subscript expression for each dimension.
Definition ScopInfo.h:585
isl::map getLatestAccessRelation() const
Return the newest access relation of this access.
Definition ScopInfo.h:784
isl::id getOriginalArrayId() const
Get the detection-time base array isl::id for this access.
Definition ScopInfo.cpp:564
MemoryAccess(const MemoryAccess &)=delete
isl::pw_aff getPwAff(const SCEV *E)
Compute the isl representation for the SCEV E wrt.
Definition ScopInfo.cpp:955
Instruction * AccessInstruction
The access instruction of this memory access.
Definition ScopInfo.h:564
void computeBoundsOnAccessRelation(unsigned ElementSize)
Compute bounds on an over approximated access relation.
Definition ScopInfo.cpp:702
ReductionType RedType
Reduction type for reduction like accesses, RT_NONE otherwise.
Definition ScopInfo.h:513
bool isValueKind() const
Old name of isOriginalValueKind().
Definition ScopInfo.h:981
bool hasNewAccessRelation() const
Check if a new access relation was imported or set by a pass.
Definition ScopInfo.h:772
isl::id Id
A unique identifier for this memory access.
Definition ScopInfo.h:479
bool isOriginalExitPHIKind() const
Was this MemoryAccess detected as the accesses of a PHI node in the SCoP's exit block?
Definition ScopInfo.h:997
bool isLatestArrayKind() const
Whether storage memory is either an custom .s2a/.phiops alloca (false) or an existing pointer into an...
Definition ScopInfo.h:945
bool isPHIKind() const
Old name of isOriginalPHIKind.
Definition ScopInfo.h:993
bool isWrite() const
Is this a write memory access?
Definition ScopInfo.h:764
bool IsAffine
Are all the subscripts affine expression?
Definition ScopInfo.h:582
ReductionType getReductionType() const
Get the reduction type of this access.
Definition ScopInfo.h:1029
MemoryKind getLatestKind() const
Return the kind considering a potential setNewAccessRelation.
Definition ScopInfo.h:934
const ScopArrayInfo * getOriginalScopArrayInfo() const
Get the detection-time ScopArrayInfo object for the base address.
Definition ScopInfo.cpp:550
AssertingVH< Value > BaseAddr
The base address (e.g., A for A[i+j]).
Definition ScopInfo.h:537
bool isOriginalAnyPHIKind() const
Was this access detected as one of the two PHI types?
Definition ScopInfo.h:1012
void updateDimensionality()
Update the dimensionality of the memory access.
Definition ScopInfo.cpp:439
Instruction * getAccessInstruction() const
Return the access instruction of this memory access.
Definition ScopInfo.h:880
iterator_range< SubscriptsTy::const_iterator > subscripts() const
Return an iterator range containing the subscripts.
Definition ScopInfo.h:883
bool isStrideZero(isl::map Schedule) const
Is always the same memory accessed for a given statement instance set?
bool isLatestPartialAccess() const
Return whether the MemoryyAccess is a partial access.
std::string getOriginalAccessRelationStr() const
Get an isl string representing the access function read from IR.
Definition ScopInfo.cpp:594
bool isExitPHIKind() const
Old name of isOriginalExitPHIKind().
Definition ScopInfo.h:1009
Value * tryGetValueStored()
Return llvm::Value that is stored by this access, if available.
Definition ScopInfo.h:869
friend class ScopStmt
Definition ScopInfo.h:428
isl::set InvalidDomain
The domain under which this access is not modeled precisely.
Definition ScopInfo.h:523
enum AccessType getType()
Get the type of a memory access.
Definition ScopInfo.h:749
bool isLatestScalarKind() const
Whether this access is an array to a scalar memory object, also considering changes by setNewAccessRe...
Definition ScopInfo.h:963
bool isRead() const
Is this a read memory access?
Definition ScopInfo.h:755
void buildAccessRelation(const ScopArrayInfo *SAI)
Assemble the access relation from all available information.
Definition ScopInfo.cpp:814
isl::id getId() const
Get identifier for the memory access.
Definition ScopInfo.cpp:914
isl::map NewAccessRelation
Updated access relation read from JSCOP file.
Definition ScopInfo.h:616
SmallVector< const SCEV *, 4 > SubscriptsTy
Definition ScopInfo.h:472
unsigned getNumSubscripts() const
Return the number of access function subscript.
Definition ScopInfo.h:888
isl::map getAddressFunction() const
Get an isl map describing the memory address accessed.
Definition ScopInfo.cpp:574
void setAccessRelation(isl::map AccessRelation)
Update the original access relation.
bool isMustWrite() const
Is this a must-write memory access?
Definition ScopInfo.h:758
bool isScalarKind() const
Old name of isOriginalScalarKind.
Definition ScopInfo.h:968
isl::map AccessRelation
Relation from statement instances to the accessed array elements.
Definition ScopInfo.h:613
friend class ScopBuilder
Definition ScopInfo.h:429
void realignParams()
Align the parameters in the access relation to the scop context.
Definition ScopInfo.cpp:898
Type * getElementType() const
Return the element type of the accessed array wrt. this access.
Definition ScopInfo.h:859
const SCEV * getSubscript(unsigned Dim) const
Return the access function subscript in the dimension Dim.
Definition ScopInfo.h:891
bool isReductionLike() const
Is this a reduction like access?
Definition ScopInfo.h:752
bool isOriginalPHIKind() const
Was this MemoryAccess detected as a special PHI node access?
Definition ScopInfo.h:984
isl::set getInvalidContext() const
Get the invalid context for this access.
Definition ScopInfo.h:902
void print(raw_ostream &OS) const
Print the MemoryAccess.
Definition ScopInfo.cpp:930
const ScopArrayInfo * getScopArrayInfo() const
Legacy name of getOriginalScopArrayInfo().
Definition ScopInfo.h:848
void wrapConstantDimensions()
Carry index overflows of dimensions with constant size to the next higher dimension.
Definition ScopInfo.cpp:387
bool isOriginalScalarKind() const
Whether this access is an array to a scalar memory object, without considering changes by setNewAcces...
Definition ScopInfo.h:957
ScopStmt * Statement
Parent ScopStmt of this access.
Definition ScopInfo.h:516
bool isStrideX(isl::map Schedule, int StrideWidth) const
Is the stride of the access equal to a certain width?
bool isStrideOne(isl::map Schedule) const
Is consecutive memory accessed for a given statement instance set?
Type * ElementType
Type a single array element wrt. this access.
Definition ScopInfo.h:540
enum AccessType AccType
Whether it a reading or writing access, and if writing, whether it is conditional (MAY_WRITE).
Definition ScopInfo.h:487
std::string getNewAccessRelationStr() const
Get an isl string representing a new access function, if available.
Definition ScopInfo.cpp:606
void buildMemIntrinsicAccessRelation()
Create the access relation for the underlying memory intrinsic.
Definition ScopInfo.cpp:678
isl::set getInvalidDomain() const
Get the invalid domain for this access.
Definition ScopInfo.h:899
ArrayRef< std::pair< BasicBlock *, Value * > > getIncoming() const
Return the list of possible PHI/ExitPHI values.
Definition ScopInfo.h:743
Value * getOriginalBaseAddr() const
Get the original base address of this access (e.g.
Definition ScopInfo.h:828
ScopStmt * getStatement() const
Get the statement that contains this memory access.
Definition ScopInfo.h:1026
bool isLatestExitPHIKind() const
Is this MemoryAccess modeling the accesses of a PHI node in the SCoP's exit block?
Definition ScopInfo.h:1004
bool isAffine() const
Is the memory access affine?
Definition ScopInfo.h:1080
isl::set getStride(isl::map Schedule) const
Get the stride of this memory access in the specified Schedule.
Definition ScopInfo.cpp:997
bool isMayWrite() const
Is this a may-write memory access?
Definition ScopInfo.h:761
isl::id getLatestArrayId() const
Get the base array isl::id for this access, modifiable through setNewAccessRelation().
Definition ScopInfo.cpp:568
MemoryKind Kind
What is modeled by this MemoryAccess.
Definition ScopInfo.h:483
bool isLatestAnyPHIKind() const
Does this access originate from one of the two PHI types?
Definition ScopInfo.h:1018
bool isLatestValueKind() const
Is this MemoryAccess currently modeling scalar dependences?
Definition ScopInfo.h:976
isl::pw_multi_aff applyScheduleToAccessRelation(isl::union_map Schedule) const
Return the access relation after the schedule was applied.
Definition ScopInfo.cpp:579
MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, AccessType AccType, Value *BaseAddress, Type *ElemType, bool Affine, ArrayRef< const SCEV * > Subscripts, ArrayRef< const SCEV * > Sizes, Value *AccessValue, MemoryKind Kind)
Create a new MemoryAccess.
Definition ScopInfo.cpp:860
std::string getReductionOperatorStr() const
Return a string representation of the access's reduction type.
Definition ScopInfo.cpp:910
SmallVector< std::pair< BasicBlock *, Value * >, 4 > Incoming
Incoming block and value of a PHINode.
Definition ScopInfo.h:567
isl::map getAccessRelation() const
Old name of getLatestAccessRelation().
Definition ScopInfo.h:790
Value * getAccessValue() const
Return the access value of this memory access.
Definition ScopInfo.h:862
isl::map getOriginalAccessRelation() const
Get the original access function as read from IR.
Definition ScopInfo.cpp:590
void setNewAccessRelation(isl::map NewAccessRelation)
Set the updated access relation read from JSCOP file.
bool isArrayKind() const
Old name of isOriginalArrayKind.
Definition ScopInfo.h:950
bool isMemoryIntrinsic() const
Is this a memory intrinsic access (memcpy, memset, memmove)?
Definition ScopInfo.h:767
MemoryKind getOriginalKind() const
Return the kind when this access was first detected.
Definition ScopInfo.h:927
Translate a SCEV to an isl::pw_aff and the domain on which it is invalid.
A class to store information about arrays in the SCoP.
Definition ScopInfo.h:214
const SCEV * getDimensionSize(unsigned Dim) const
Return the size of dimension dim as SCEV*.
Definition ScopInfo.h:287
Type * ElementType
The canonical element type of this array.
Definition ScopInfo.h:399
isl::space getSpace() const
Get the space of this array access.
Definition ScopInfo.cpp:254
const SmallSetVector< ScopArrayInfo *, 2 > & getDerivedSAIs() const
The set of derived indirect SAIs for this origin SAI.
Definition ScopInfo.h:270
SmallSetVector< ScopArrayInfo *, 2 > DerivedSAIs
For origin SAIs the set of derived indirect SAIs.
Definition ScopInfo.h:387
isl::id Id
The isl id for the base pointer.
Definition ScopInfo.h:402
SmallVector< isl::pw_aff, 4 > DimensionSizesPw
The sizes of each dimension as isl::pw_aff.
Definition ScopInfo.h:411
bool isExitPHIKind() const
Is this array info modeling an MemoryKind::ExitPHI?
Definition ScopInfo.h:335
bool isReadOnly()
If the array is read only.
Definition ScopInfo.cpp:260
bool updateSizes(ArrayRef< const SCEV * > Sizes, bool CheckConsistency=true)
Update the sizes of the ScopArrayInfo object.
Definition ScopInfo.cpp:300
~ScopArrayInfo()
Destructor to free the isl id of the base pointer.
bool isArrayKind() const
Is this array info modeling an array?
Definition ScopInfo.h:338
MemoryKind getKind() const
Return what kind of memory this represents.
Definition ScopInfo.h:317
bool isValueKind() const
Is this array info modeling an llvm::Value?
Definition ScopInfo.h:320
bool IsOnHeap
True if the newly allocated array is on heap.
Definition ScopInfo.h:405
ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx IslCtx, ArrayRef< const SCEV * > DimensionSizes, MemoryKind Kind, const DataLayout &DL, Scop *S, const char *BaseName=nullptr)
Construct a ScopArrayInfo object.
Definition ScopInfo.cpp:228
static const ScopArrayInfo * getFromId(isl::id Id)
Access the ScopArrayInfo associated with an isl Id.
Definition ScopInfo.cpp:381
void setIsOnHeap(bool value)
Definition ScopInfo.h:264
std::string getName() const
Get the name of this memory reference.
Definition ScopInfo.cpp:333
bool isPHIKind() const
Is this array info modeling special PHI node memory?
Definition ScopInfo.h:332
Value * getBasePtr() const
Return the base pointer.
Definition ScopInfo.h:261
int getElemSizeInBytes() const
Get element size in bytes.
Definition ScopInfo.cpp:335
isl::pw_aff getDimensionSizePw(unsigned Dim) const
Return the size of dimension dim as isl::pw_aff.
Definition ScopInfo.h:297
AssertingVH< Value > BasePtr
The base pointer.
Definition ScopInfo.h:390
bool isCompatibleWith(const ScopArrayInfo *Array) const
Verify that Array is compatible to this ScopArrayInfo.
Definition ScopInfo.cpp:268
void addDerivedSAI(ScopArrayInfo *DerivedSAI)
Definition ScopInfo.h:379
bool isOnHeap() const
Is this array allocated on heap.
Definition ScopInfo.h:344
void updateElementType(Type *NewElementType)
Update the element type of the ScopArrayInfo object.
Definition ScopInfo.cpp:282
const ScopArrayInfo * BasePtrOriginSAI
For indirect accesses this is the SAI of the BP origin.
Definition ScopInfo.h:384
const DataLayout & DL
The data layout of the module.
Definition ScopInfo.h:419
void setBasePtr(Value *BP)
Set the base pointer to BP.
Definition ScopInfo.h:258
isl::id getBasePtrId() const
Return the isl id for the base pointer.
Definition ScopInfo.cpp:339
Scop & S
The scop this SAI object belongs to.
Definition ScopInfo.h:422
static const ScopArrayInfo * getFromAccessFunction(isl::pw_multi_aff PMA)
Access the ScopArrayInfo associated with an access function.
Definition ScopInfo.cpp:375
unsigned getNumberOfDimensions() const
Return the number of dimensions.
Definition ScopInfo.h:275
void print(raw_ostream &OS, bool SizeAsPwAff=false) const
Print a readable representation to OS.
Definition ScopInfo.cpp:345
Type * getElementType() const
Get the canonical element type of this array.
Definition ScopInfo.h:305
SmallVector< const SCEV *, 4 > DimensionSizes
The sizes of each dimension as SCEV*.
Definition ScopInfo.h:408
MemoryKind Kind
The type of this scop array info object.
Definition ScopInfo.h:416
void dump() const
Dump a readable representation to stderr.
Definition ScopInfo.cpp:342
const ScopArrayInfo * getBasePtrOriginSAI() const
For indirect accesses return the origin SAI of the BP, else null.
Definition ScopInfo.h:267
Pass to detect the maximal static control parts (Scops) of a function.
iterator end()
Definition ScopInfo.h:2721
RegionToScopMapTy::const_reverse_iterator const_reverse_iterator
Definition ScopInfo.h:2676
const_iterator end() const
Definition ScopInfo.h:2723
RegionToScopMapTy::const_iterator const_iterator
Definition ScopInfo.h:2678
AAResults & AA
Definition ScopInfo.h:2688
RegionToScopMapTy::iterator iterator
Definition ScopInfo.h:2677
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation explicitly.
AssumptionCache & AC
Definition ScopInfo.h:2690
MapVector< Region *, std::unique_ptr< Scop > > RegionToScopMapTy
Definition ScopInfo.h:2674
DominatorTree & DT
Definition ScopInfo.h:2689
LoopInfo & LI
Definition ScopInfo.h:2687
ScopDetection & SD
Definition ScopInfo.h:2685
reverse_iterator rend()
Definition ScopInfo.h:2725
const DataLayout & DL
Definition ScopInfo.h:2684
const_reverse_iterator rend() const
Definition ScopInfo.h:2727
Scop * getScop(Region *R) const
Get the Scop object for the given Region.
Definition ScopInfo.h:2704
const_iterator begin() const
Definition ScopInfo.h:2722
ScalarEvolution & SE
Definition ScopInfo.h:2686
const_reverse_iterator rbegin() const
Definition ScopInfo.h:2726
reverse_iterator rbegin()
Definition ScopInfo.h:2724
bool empty() const
Definition ScopInfo.h:2728
void recompute()
Recompute the Scop-Information for a function.
OptimizationRemarkEmitter & ORE
Definition ScopInfo.h:2691
iterator begin()
Definition ScopInfo.h:2720
ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, LoopInfo &LI, AAResults &AA, DominatorTree &DT, AssumptionCache &AC, OptimizationRemarkEmitter &ORE)
RegionToScopMapTy::reverse_iterator reverse_iterator
Definition ScopInfo.h:2675
RegionToScopMapTy RegionToScopMap
A map of Region to its Scop object containing Polly IR of static control part.
Definition ScopInfo.h:2683
Statement of the Scop.
Definition ScopInfo.h:1135
iterator end()
Definition ScopInfo.h:1516
llvm::SmallVector< MemoryAccess *, 8 > MemoryAccessVec
Definition ScopInfo.h:1139
MemoryAccess & getArrayAccessFor(const Instruction *Inst) const
Return the only array access for Inst.
Definition ScopInfo.h:1429
Scop * getParent()
Definition ScopInfo.h:1523
BasicBlock * getEntryBlock() const
Return a BasicBlock from this statement.
void dump() const
Print the ScopStmt to stderr.
bool isEmpty() const
Return true if this statement does not contain any accesses.
Definition ScopInfo.h:1383
std::vector< Instruction * > Instructions
Vector for Instructions in this statement.
Definition ScopInfo.h:1261
void print(raw_ostream &OS, bool PrintInstructions) const
Print the ScopStmt.
Region * R
The region represented by this statement (in the non-affine case).
Definition ScopInfo.h:1246
DenseMap< PHINode *, MemoryAccess * > PHIWrites
Map from PHI nodes to its incoming value when coming from this statement.
Definition ScopInfo.h:1227
const Scop * getParent() const
Definition ScopInfo.h:1524
std::vector< Instruction * >::const_iterator insts_end() const
Definition ScopInfo.h:1540
isl::set Domain
The iteration domain describes the set of iterations for which this statement is executed.
Definition ScopInfo.h:1202
const std::vector< Instruction * > & getInstructions() const
Definition ScopInfo.h:1526
bool isBlockStmt() const
Return true if this statement represents a single basic block.
Definition ScopInfo.h:1316
void removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting=true)
Remove MA from this statement.
MemoryAccess * ensureValueRead(Value *V)
Check whether there is a value read access for V in this statement, and if not, create one.
void setInstructions(ArrayRef< Instruction * > Range)
Set the list of instructions for this statement.
Definition ScopInfo.h:1532
Loop * SurroundingLoop
The closest loop that contains this statement.
Definition ScopInfo.h:1258
ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, Loop *SurroundingLoop, std::vector< Instruction * > Instructions)
Create the ScopStmt from a BasicBlock.
MemoryAccess * lookupInputAccessOf(Value *Val) const
Return the input access of the value, or null if no such MemoryAccess exists.
Definition ScopInfo.h:1472
MemoryAccessVec::const_iterator const_iterator
Definition ScopInfo.h:1513
std::string getScheduleStr() const
Get an isl string representing this schedule.
const_iterator end() const
Definition ScopInfo.h:1518
void prependInstruction(Instruction *Inst)
Insert an instruction before all other instructions in this statement.
Definition ScopInfo.h:1550
const ScopStmt & operator=(const ScopStmt &)=delete
std::string getDomainStr() const
Get an isl string representing this domain.
const MemoryAccessList * lookupArrayAccessesFor(const Instruction *Inst) const
Find all array accesses for Inst.
Definition ScopInfo.h:1392
std::vector< Instruction * >::const_iterator insts_begin() const
Definition ScopInfo.h:1536
size_t size() const
Definition ScopInfo.h:1519
isl::set getInvalidContext() const
Get the invalid context for this statement.
Definition ScopInfo.h:1304
void realignParams()
Align the parameters in the statement to the scop context.
void removeAccessData(MemoryAccess *MA)
Remove MA from dictionaries pointing to them.
ScopStmt(const ScopStmt &)=delete
isl::map getSchedule() const
Get the schedule function of this ScopStmt.
isl::set getInvalidDomain() const
Get the invalid domain for this statement.
Definition ScopInfo.h:1301
SmallVector< Loop *, 4 > NestLoops
Definition ScopInfo.h:1253
MemoryAccessVec::iterator iterator
Definition ScopInfo.h:1512
DenseMap< const Instruction *, MemoryAccessList > InstructionToAccess
Mapping from instructions to (scalar) memory accesses.
Definition ScopInfo.h:1210
Scop & Parent
Polyhedral description.
Definition ScopInfo.h:1174
void restrictDomain(isl::set NewDomain)
Restrict the domain of the statement.
std::string BaseName
Definition ScopInfo.h:1255
isl::ctx getIslCtx() const
Get an isl_ctx pointer.
Region * getRegion() const
Get the region represented by this ScopStmt (if any).
Definition ScopInfo.h:1325
bool represents(BasicBlock *BB) const
Return whether this statement represents BB.
Definition ScopInfo.h:1346
friend class ScopBuilder
Definition ScopInfo.h:1136
DenseMap< Instruction *, MemoryAccess * > ValueWrites
The set of values defined in this ScopStmt that are required elsewhere, mapped to their MemoryKind::V...
Definition ScopInfo.h:1218
iterator_range< std::vector< Instruction * >::const_iterator > insts() const
The range of instructions in this statement.
Definition ScopInfo.h:1545
MemoryAccess * lookupPHIReadOf(PHINode *PHI) const
Return the MemoryAccess that loads a PHINode value, or nullptr if not existing, respectively not yet ...
Definition ScopInfo.h:1453
BasicBlock * getBasicBlock() const
Get the BasicBlock represented by this ScopStmt (if any).
Definition ScopInfo.h:1313
void removeMemoryAccess(MemoryAccess *MA)
Remove a MemoryAccess from this statement.
MemoryAccessVec MemAccs
The memory accesses of this statement.
Definition ScopInfo.h:1207
const char * getBaseName() const
bool contains(const Loop *L) const
Return whether L is boxed within this statement.
Definition ScopInfo.h:1337
isl::ast_build Build
}
Definition ScopInfo.h:1251
bool isCopyStmt() const
Return true if this is a copy statement.
Definition ScopInfo.h:1319
DenseMap< Value *, MemoryAccess * > ValueReads
The set of values defined elsewhere required in this ScopStmt and their MemoryKind::Value READ Memory...
Definition ScopInfo.h:1214
isl::ast_build getAstBuild() const
Get the isl AST build.
Definition ScopInfo.h:1560
isl::set InvalidDomain
The domain under which this statement is not modeled precisely.
Definition ScopInfo.h:1181
DenseMap< PHINode *, MemoryAccess * > PHIReads
Map from PHI nodes to its read access in this statement.
Definition ScopInfo.h:1230
MemoryAccess * getArrayAccessOrNULLFor(const Instruction *Inst) const
Return the only array access for Inst, if existing.
Definition ScopInfo.h:1406
isl::id getDomainId() const
Get the id of the iteration domain space.
void addAccess(MemoryAccess *Access, bool Prepend=false)
Add Access to this statement's list of accesses.
bool isRegionStmt() const
Return true if this statement represents a whole region.
Definition ScopInfo.h:1328
void setInvalidDomain(isl::set ID)
Set the invalid context for this statement to ID.
unsigned getNumIterators() const
Loop * getLoopForDimension(unsigned Dimension) const
Get the loop for a dimension.
isl::set getDomain() const
Get the iteration domain of this ScopStmt.
const_iterator begin() const
Definition ScopInfo.h:1517
void setAstBuild(isl::ast_build B)
Set the isl AST build.
Definition ScopInfo.h:1557
MemoryAccess * lookupValueWriteOf(Instruction *Inst) const
Return the MemoryAccess that writes the value of an instruction defined in this statement,...
Definition ScopInfo.h:1439
Loop * getSurroundingLoop() const
Return the closest innermost loop that contains this statement, but is not contained in it.
Definition ScopInfo.h:1376
BasicBlock * BB
A SCoP statement represents either a basic block (affine/precise case) or a whole region (non-affine ...
Definition ScopInfo.h:1243
isl::space getDomainSpace() const
Get the space of the iteration domain.
MemoryAccess * lookupPHIWriteOf(PHINode *PHI) const
Return the PHI write MemoryAccess for the incoming values from any basic block in this ScopStmt,...
Definition ScopInfo.h:1460
void printInstructions(raw_ostream &OS) const
Print the instructions in ScopStmt.
MemoryAccess * lookupValueReadOf(Value *Inst) const
Return the MemoryAccess that reloads a value, or nullptr if not existing, respectively not yet added.
Definition ScopInfo.h:1447
bool contains(Instruction *Inst) const
Return whether this statement contains Inst.
Definition ScopInfo.h:1355
iterator begin()
Definition ScopInfo.h:1515
Static Control Part.
Definition ScopInfo.h:1625
InvariantEquivClassTy * lookupInvariantEquivClass(Value *Val)
Return the invariant equivalence class for Val if any.
isl::schedule getScheduleTree() const
Get a schedule tree describing the schedule of all statements.
isl::set InvalidContext
The restrictions under which this SCoP was built.
Definition ScopInfo.h:1755
bool IsOptimized
Flag to indicate that the scheduler actually optimized the SCoP.
Definition ScopInfo.h:1670
bool HasErrorBlock
Flag to remember if the SCoP contained an error block or not.
Definition ScopInfo.h:1676
void intersectDefinedBehavior(isl::set Set, AssumptionSign Sign)
Add the conditions from Set (or subtract them if Sign is AS_RESTRICTION) to the defined behaviour con...
isl::space getFullParamSpace() const
Return the full space of parameters.
ArrayRef< MemoryAccess * > getValueUses(const ScopArrayInfo *SAI) const
Return all MemoryAccesses that us an llvm::Value, represented by a ScopArrayInfo.
bool isParam(const SCEV *Param) const
Return whether given SCEV is used as the parameter in this Scop.
Definition ScopInfo.h:2016
DenseMap< const ScopArrayInfo *, SmallVector< MemoryAccess *, 4 > > ValueUseAccs
List of all uses (i.e.
Definition ScopInfo.h:1857
isl::union_map getMayWrites()
Get a union map of all may-writes performed in the SCoP.
void printContext(raw_ostream &OS) const
const MinMaxVectorPairVectorTy & getAliasGroups() const
Return all alias groups for this SCoP.
Definition ScopInfo.h:2274
isl::set getInvalidContext() const
Get the invalid context for this Scop.
void invalidateScopArrayInfo(Value *BasePtr, MemoryKind Kind)
Invalidate ScopArrayInfo object for base address.
Definition ScopInfo.h:2420
void dump() const
Print the ScopStmt to stderr.
isl::union_map getSchedule() const
Get the schedule of all the statements in the SCoP.
void invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB=nullptr)
Mark the scop as invalid.
MemoryAccess * getValueDef(const ScopArrayInfo *SAI) const
Return the MemoryAccess that writes an llvm::Value, represented by a ScopArrayInfo.
ScalarEvolution * getSE() const
Return the scalar evolution.
ScalarEvolution * SE
Definition ScopInfo.h:1655
DominatorTree * getDT() const
Return the dominator tree.
Definition ScopInfo.h:2005
unsigned getMaxLoopDepth() const
Get the maximum depth of the loop.
Definition ScopInfo.h:2122
void printAliasAssumptions(raw_ostream &OS) const
ArrayRef< MemoryAccess * > getPHIIncomings(const ScopArrayInfo *SAI) const
Return all MemoryAccesses for all incoming statements of a PHINode, represented by a ScopArrayInfo.
bool hasInvariantAccesses()
Check if the scop has any invariant access.
Definition ScopInfo.h:2133
ScopStmt * getStmtFor(Instruction *Inst) const
Return the ScopStmt an instruction belongs to, or nullptr if it does not belong to any statement in t...
Definition ScopInfo.h:2333
void setDomain(BasicBlock *BB, isl::set &Domain)
Set domain for BB.
Definition ScopInfo.h:2521
ScopArrayInfo * getScopArrayInfo(Value *BasePtr, MemoryKind Kind)
Return the cached ScopArrayInfo object for BasePtr.
ParameterSetTy Parameters
Parameters of this Scop.
Definition ScopInfo.h:1690
bool hasDisableHeuristicsHint() const
Is this Scop marked as not to be transformed by an optimization heuristic?
Definition ScopInfo.h:2663
bool hasTrivialInvalidContext() const
Return true if and only if the InvalidContext is trivial (=empty).
Definition ScopInfo.h:2271
ArrayInfoSetTy ScopArrayInfoSet
A set to remember ScopArrayInfo objects.
Definition ScopInfo.h:1738
ValueToValueMap InvEquivClassVMap
Mapping from invariant loads to the representing invariant load of their equivalence class.
Definition ScopInfo.h:1833
iterator_range< AccFuncVector::iterator > access_functions()
Return an iterator range containing all the MemoryAccess objects of the Scop.
Definition ScopInfo.h:2034
isl::union_map getReads()
Get a union map of all reads performed in the SCoP.
unsigned getCopyStmtsNum()
Get the count of copy statements added to this Scop.
Definition ScopInfo.h:1952
bool isDomainDefined(BasicBlock *BB) const
Check if domain is determined for BB.
Definition ScopInfo.h:2518
unsigned CopyStmtsNum
Number of copy statements.
Definition ScopInfo.h:1682
bool HasDisableHeuristicsHint
Is this Scop marked as not to be transformed by an optimization heuristic?
Definition ScopInfo.h:1810
DenseMap< BasicBlock *, std::vector< ScopStmt * > > StmtMap
A map from basic blocks to vector of SCoP statements.
Definition ScopInfo.h:1703
const MapInsnToMemAcc & getInsnToMemAccMap() const
Definition ScopInfo.h:2377
void addParams(const ParameterSetTy &NewParameters)
Take a list of parameters and add the new ones to the scop.
iterator end()
Definition ScopInfo.h:2348
isl::set getAssumedContext() const
Get the assumed context for this Scop.
Scop & operator=(const Scop &)=delete
void addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop, std::vector< Instruction * > Instructions)
Create a new SCoP statement for BB.
SCEVAffinator Affinator
The affinator used to translate SCEVs to isl expressions.
Definition ScopInfo.h:1715
ScopArrayInfo * getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, ArrayRef< const SCEV * > Sizes, MemoryKind Kind, const char *BaseName=nullptr)
Return the (possibly new) ScopArrayInfo object for Access.
DominatorTree * DT
Definition ScopInfo.h:1656
const InvariantLoadsSetTy & getRequiredInvariantLoads() const
Return the set of required invariant loads.
Definition ScopInfo.h:2362
void addAccessFunction(MemoryAccess *Access)
Add the access function to all MemoryAccess objects of the Scop created in this pass.
Definition ScopInfo.h:1967
isl::schedule Schedule
The schedule of the SCoP.
Definition ScopInfo.h:1807
iterator begin()
Definition ScopInfo.h:2347
isl::set getBestKnownDefinedBehaviorContext() const
Return the define behavior context, or if not available, its approximation from all other contexts.
Definition ScopInfo.h:2166
bool contains(const Instruction *I) const
Check if I is contained in the SCoP.
Definition ScopInfo.h:2096
SmallVector< MinMaxVectorPairTy, 4 > MinMaxVectorPairVectorTy
Vector of pair of minimal/maximal access vectors representing non read only and read only accesses fo...
Definition ScopInfo.h:1639
isl::set Context
Constraints on parameters.
Definition ScopInfo.h:1712
unsigned MaxLoopDepth
Max loop depth.
Definition ScopInfo.h:1679
isl::union_set getDomains() const
Get a union set containing the iteration domains of all statements.
const BoxedLoopsSetTy & getBoxedLoops() const
Return the set of boxed (thus overapproximated) loops.
Definition ScopInfo.h:2370
std::shared_ptr< isl_ctx > IslCtx
Isl context.
Definition ScopInfo.h:1653
void addAliasGroup(MinMaxVectorTy &MinMaxAccessesReadWrite, MinMaxVectorTy &MinMaxAccessesReadOnly)
Definition ScopInfo.h:2278
int getID() const
Return the ID of the Scop.
Definition ScopInfo.h:2142
void markAsOptimized()
Mark the SCoP as optimized by the scheduler.
Definition ScopInfo.h:2136
bool isOriginalSchedule() const
Whether the schedule is the original schedule as derived from the CFG by ScopBuilder.
Definition ScopInfo.h:2567
ArrayInfoSetTy::iterator array_iterator
Definition ScopInfo.h:2048
std::string getAssumedContextStr() const
Get an isl string representing the assumed context.
bool isProfitable(bool ScalarsAreUnprofitable) const
Return true if this SCoP can be profitably optimized.
array_iterator array_begin()
Definition ScopInfo.h:2053
bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const
Return true if and only if BB dominates the SCoP.
ScopArrayInfo * getArrayInfoByName(const std::string BaseName)
Find the ScopArrayInfo associated with an isl Id that has name Name.
array_range arrays()
Definition ScopInfo.h:2065
void addAccessData(MemoryAccess *Access)
Add metadata for Access.
isl::set getDomainConditions(const ScopStmt *Stmt) const
Return the domain of Stmt.
void addInvariantEquivClass(const InvariantEquivClassTy &InvariantEquivClass)
Add new invariant access equivalence class.
Definition ScopInfo.h:1988
AccFuncVector AccessFunctions
Definition ScopInfo.h:1667
PWACtx getPwAff(const SCEV *E, BasicBlock *BB=nullptr, bool NonNegative=false, RecordedAssumptionsTy *RecordedAssumptions=nullptr)
Compute the isl representation for the SCEV E.
DenseMap< Value *, MemoryAccess * > ValueDefAccs
Map of values to the MemoryAccess that writes its definition.
Definition ScopInfo.h:1850
isl::union_map getMustWrites()
Get a union map of all must-writes performed in the SCoP.
reverse_iterator rbegin()
Definition ScopInfo.h:2355
std::pair< std::string, std::string > getEntryExitStr() const
Get the name of the entry and exit blocks of this Scop.
isl::pw_aff getPwAffOnly(const SCEV *E, BasicBlock *BB=nullptr, RecordedAssumptionsTy *RecordedAssumptions=nullptr)
Compute the isl representation for the SCEV E.
ScopStatistics getStatistics() const
Collect statistic about this SCoP.
std::string getContextStr() const
Get an isl string representing the context.
std::pair< MinMaxVectorTy, MinMaxVectorTy > MinMaxVectorPairTy
Pair of minimal/maximal access vectors representing read write and read only accesses.
Definition ScopInfo.h:1635
DenseMap< BasicBlock *, isl::set > DomainMap
A map from basic blocks to their domains.
Definition ScopInfo.h:1709
isl::union_map getAccessesOfType(std::function< bool(MemoryAccess &)> Predicate)
Collect all memory access relations of a given type.
void removeStmts(function_ref< bool(ScopStmt &)> ShouldDelete, bool AfterHoisting=true)
Remove statements from the list of scop statements.
void buildContext()
Build the Context of the Scop.
void addInvariantLoadMapping(const Value *LoadInst, Value *ClassRep)
Add mapping from invariant loads to the representing invariant load of their equivalence class.
Definition ScopInfo.h:1994
int getRelativeLoopDepth(const Loop *L) const
Get the depth of a loop relative to the outermost loop in the Scop.
isl::ctx getIslCtx() const
Get the isl context of this static control part.
LoopInfo * getLI() const
Return the LoopInfo used for this Scop.
Definition ScopInfo.h:2008
std::string getInvalidContextStr() const
Get an isl string representing the invalid context.
StringRef getName()
Definition ScopInfo.h:2042
iterator_range< ArrayInfoSetTy::iterator > array_range
Definition ScopInfo.h:2050
bool HasSingleExitEdge
True if the underlying region has a single exiting block.
Definition ScopInfo.h:1673
DenseMap< Instruction *, ScopStmt * > InstStmtMap
A map from instructions to SCoP statements.
Definition ScopInfo.h:1706
bool isEscaping(Instruction *Inst)
Return whether Inst has a use outside of this SCoP.
void removeStmtNotInDomainMap()
Removes all statements where the entry block of the statement does not have a corresponding domain in...
bool hasNSWAddRecForLoop(Loop *L)
Check if an <nsw> AddRec for the loop L is cached.
Definition ScopInfo.h:2502
void updateMaxLoopDepth(unsigned Depth)
Update maximal loop depth.
Definition ScopInfo.h:2433
ScopDetection::DetectionContext & DC
The context of the SCoP created during SCoP detection.
Definition ScopInfo.h:1696
void print(raw_ostream &OS, bool PrintInstructions) const
Print the static control part.
const_iterator end() const
Definition ScopInfo.h:2350
void printStatements(raw_ostream &OS, bool PrintInstructions) const
isl::union_map getWrites()
Get a union map of all writes performed in the SCoP.
reverse_iterator rend()
Definition ScopInfo.h:2356
void setSchedule(isl::union_map NewSchedule)
Update the current schedule.
bool hasErrorBlock() const
Return true if the SCoP contained at least one error block.
Definition ScopInfo.h:2449
void setContext(isl::set NewContext)
Set new isl context.
bool hasFeasibleRuntimeContext() const
Return true if the optimized SCoP can be executed.
DenseMap< const SCEV *, isl::id > ParameterIds
Mapping from parameters to their ids.
Definition ScopInfo.h:1693
isl::space getParamSpace() const
Return space of isl context parameters.
bool isExit(BasicBlock *BB) const
Return true if BB is the exit block of the SCoP.
Definition ScopInfo.h:2111
void addRequiredInvariantLoad(LoadInst *LI)
Add LI to the set of required invariant loads.
Definition ScopInfo.h:2367
const std::shared_ptr< isl_ctx > & getSharedIslCtx() const
Directly return the shared_ptr of the context.
Definition ScopInfo.h:2475
SmallVector< MinMaxAccessTy, 4 > MinMaxVectorTy
Vector of minimal/maximal accesses to different arrays.
Definition ScopInfo.h:1631
isl::set getDefinedBehaviorContext() const
Return the context where execution behavior is defined.
Definition ScopInfo.h:2162
friend class ScopBuilder
Definition ScopInfo.h:1642
Region::block_range blocks() const
Return a range of all basic blocks in the SCoP.
Definition ScopInfo.h:2114
const_iterator begin() const
Definition ScopInfo.h:2349
isl::set & getOrInitEmptyDomain(BasicBlock *BB)
Return the domain of BB. If it does not exist, create an empty one.
Definition ScopInfo.h:2515
StmtSet::const_reverse_iterator const_reverse_iterator
Definition ScopInfo.h:2353
std::string getNameStr() const
Get the name of this Scop.
DenseMap< PHINode *, MemoryAccess * > PHIReadAccs
Map of values to the MemoryAccess that reads a PHI.
Definition ScopInfo.h:1853
static void incrementNumberOfAliasingAssumptions(unsigned Step)
Increment actual number of aliasing assumptions taken.
long StmtIdx
The smallest statement index not yet assigned.
Definition ScopInfo.h:1842
std::pair< isl::pw_multi_aff, isl::pw_multi_aff > MinMaxAccessTy
Type to represent a pair of minimal/maximal access to an array.
Definition ScopInfo.h:1628
iterator_range< ArrayInfoSetTy::const_iterator > const_array_range
Definition ScopInfo.h:2051
std::optional< std::string > name
The name of the SCoP (identical to the regions name)
Definition ScopInfo.h:1662
size_t getSize() const
Return the number of statements in the SCoP.
Definition ScopInfo.h:2338
void createParameterId(const SCEV *Param)
Create an id for Param and store it in the ParameterIds map.
const_reverse_iterator rbegin() const
Definition ScopInfo.h:2357
BasicBlock * getEnteringBlock() const
Return the unique entering block of the SCoP if any.
Definition ScopInfo.h:2108
ArrayNameMapTy ScopArrayNameMap
A map to remember ScopArrayInfo objects for all names of memory references.
Definition ScopInfo.h:1734
isl::set DefinedBehaviorContext
The context under which the SCoP must have defined behavior.
Definition ScopInfo.h:1771
iterator_range< ParameterSetTy::iterator > parameters() const
Return an iterator range containing the scop parameters.
Definition ScopInfo.h:2022
bool isEmpty() const
Return whether this scop is empty, i.e.
Definition ScopInfo.h:2040
DenseMap< const ScopArrayInfo *, SmallVector< MemoryAccess *, 4 > > PHIIncomingAccs
List of all incoming values (write MemoryAccess) of a MemoryKind::PHI or MemoryKind::ExitPHI scalar.
Definition ScopInfo.h:1862
void markDisableHeuristics()
Mark this Scop to not apply an optimization heuristic.
Definition ScopInfo.h:2666
isl::id getIdForParam(const SCEV *Parameter) const
Return the isl_id that represents a certain parameter.
iterator_range< InvariantEquivClassesTy::iterator > invariantEquivClasses()
Return an iterator range containing invariant accesses.
Definition ScopInfo.h:2027
bool isOptimized() const
Check if the SCoP has been optimized by the scheduler.
Definition ScopInfo.h:2139
InvariantEquivClassesTy InvariantEquivClasses
List of invariant accesses.
Definition ScopInfo.h:1836
StmtSet::iterator iterator
Definition ScopInfo.h:2344
BasicBlock * getExitingBlock() const
Return the unique exiting block of the SCoP if any.
Definition ScopInfo.h:2102
Region & R
The underlying Region.
Definition ScopInfo.h:1659
OptimizationRemarkEmitter & ORE
OptimizationRemarkEmitter object for displaying diagnostic remarks.
Definition ScopInfo.h:1699
ArrayInfoSetTy::const_iterator const_array_iterator
Definition ScopInfo.h:2049
bool ScheduleModified
Whether the schedule has been modified after derived from the CFG by ScopBuilder.
Definition ScopInfo.h:1814
size_t getNumParams() const
Get the count of parameters used in this Scop.
Definition ScopInfo.h:2013
void addParameterBounds()
Add the bounds of the parameters to the context.
bool restrictDomains(isl::union_set Domain)
Intersects the domains of all statements in the SCoP.
const_array_iterator array_begin() const
Definition ScopInfo.h:2057
isl::union_map getAccesses()
Get a union map of all memory accesses performed in the SCoP.
ScopArrayInfo * createScopArrayInfo(Type *ElementType, const std::string &BaseName, const std::vector< unsigned > &Sizes)
Create an array and return the corresponding ScopArrayInfo object.
BasicBlock * getExit() const
Return the unique exit block of the SCoP.
Definition ScopInfo.h:2099
long getNextStmtIdx()
Get the next free statement index.
Definition ScopInfo.h:2603
void notifyErrorBlock()
Notify SCoP that it contains an error block.
Definition ScopInfo.h:2452
StmtSet Stmts
The statements in this Scop.
Definition ScopInfo.h:1687
SetVector< ScopArrayInfo * > ArrayInfoSetTy
Definition ScopInfo.h:1723
StmtSet::reverse_iterator reverse_iterator
Definition ScopInfo.h:2352
void removeAccessData(MemoryAccess *Access)
Remove the metadata stored for Access.
ArrayRef< ScopStmt * > getStmtListFor(BasicBlock *BB) const
Return the list of ScopStmts that represent the given BB.
MemoryAccess * getPHIRead(const ScopArrayInfo *SAI) const
Return the MemoryAccess that represents an llvm::PHINode.
std::map< std::pair< AssertingVH< const Value >, MemoryKind >, std::unique_ptr< ScopArrayInfo > > ArrayInfoMapTy
Definition ScopInfo.h:1717
bool contains(const Loop *L) const
Check if L is contained in the SCoP.
Definition ScopInfo.h:2090
void realignParams()
Align the parameters in the statement to the scop context.
array_iterator array_end()
Definition ScopInfo.h:2055
Function & getFunction() const
Return the function this SCoP is in.
Definition ScopInfo.h:2087
ArrayInfoMapTy ScopArrayInfoMap
A map to remember ScopArrayInfo objects for all base pointers.
Definition ScopInfo.h:1730
StmtSet::const_iterator const_iterator
Definition ScopInfo.h:2345
void printArrayInfo(raw_ostream &OS) const
bool isNonAffineSubRegion(const Region *R)
Return true if and only if R is a non-affine subregion.
Definition ScopInfo.h:2373
Scop(Region &R, ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT, ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE, int ID)
Scop constructor; invoked from ScopBuilder::buildScop.
const SCEV * getRepresentingInvariantLoadSCEV(const SCEV *S) const
Get the representing SCEV for S if applicable, otherwise S.
long getNextArrayIdx()
Get the next free array index.
Definition ScopInfo.h:2597
void simplifyContexts()
Simplify the assumed and invalid context.
bool hasSingleExitEdge() const
Return true if the underlying region has a single exiting block.
Definition ScopInfo.h:2455
const_array_iterator array_end() const
Definition ScopInfo.h:2061
ScopArrayInfo * getScopArrayInfoOrNull(Value *BasePtr, MemoryKind Kind)
Return the cached ScopArrayInfo object for BasePtr.
const_reverse_iterator rend() const
Definition ScopInfo.h:2358
MemoryAccess * lookupBasePtrAccess(MemoryAccess *MA)
Return the access for the base ptr of MA if any.
InvariantEquivClassesTy & getInvariantAccesses()
Return the set of invariant accesses.
Definition ScopInfo.h:2128
isl::set AssumedContext
The assumptions under which this scop was built.
Definition ScopInfo.h:1747
Scop(const Scop &)=delete
void addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB, bool RTC=true)
Add assumptions to assumed context.
MinMaxVectorPairVectorTy MinMaxAliasGroups
The set of minimal/maximal accesses for each alias group.
Definition ScopInfo.h:1829
std::list< ScopStmt > StmtSet
Definition ScopInfo.h:1684
const Region & getRegion() const
Get the maximum region of this static control part.
Definition ScopInfo.h:2083
Region & getRegion()
Definition ScopInfo.h:2084
bool isEffectiveAssumption(isl::set Set, AssumptionSign Sign)
Check if the assumption in Set is trivial or not.
void simplifySCoP(bool AfterHoisting)
Simplify the SCoP representation.
ScopStmt * getIncomingStmtFor(const Use &U) const
Get the statement to put a PHI WRITE into.
bool trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB)
Track and report an assumption.
isl::set getContext() const
Get the constraint on parameter of this Scop.
long ArrayIdx
The smallest array index not yet assigned.
Definition ScopInfo.h:1839
void setScheduleTree(isl::schedule NewSchedule)
Update the current schedule.
BasicBlock * getEntry() const
Return the unique entry block of the SCoP.
Definition ScopInfo.h:2105
const_array_range arrays() const
Definition ScopInfo.h:2069
const int ID
A number that uniquely represents a Scop within its function.
Definition ScopInfo.h:1845
ScopStmt * getLastStmtFor(BasicBlock *BB) const
Return the last statement representing BB.
void removeFromStmtMap(ScopStmt &Stmt)
Removes Stmt from the StmtMap.
bool contains(const BasicBlock *BB) const
Check if BB is contained in the SCoP.
Definition ScopInfo.h:2093
StringMap< std::unique_ptr< ScopArrayInfo > > ArrayNameMapTy
Definition ScopInfo.h:1721
B()
#define S(TYPE, NAME)
#define assert(exp)
std::forward_list< MemoryAccess * > MemoryAccessList
Ordered list type to hold accesses.
Definition ScopInfo.h:1086
std::pair< isl::pw_aff, isl::set > PWACtx
The result type of the SCEVAffinator.
unsigned const MaxDisjunctsInDomain
Definition ScopInfo.cpp:115
SmallVector< InvariantAccess, 8 > InvariantAccessesTy
Ordered container type to hold invariant accesses.
Definition ScopInfo.h:1098
llvm::SetVector< llvm::AssertingVH< llvm::LoadInst > > InvariantLoadsSetTy
Type for a set of invariant loads.
Definition ScopHelper.h:109
llvm::SetVector< const llvm::SCEV * > ParameterSetTy
Set type for parameters.
Definition ScopHelper.h:112
AssumptionSign
Enum to distinguish between assumptions and restrictions.
Definition ScopHelper.h:57
MemoryKind
The different memory kinds used in Polly.
Definition ScopInfo.h:95
@ Array
MemoryKind::Array: Models a one or multi-dimensional array.
Definition ScopInfo.h:110
@ Value
MemoryKind::Value: Models an llvm::Value.
Definition ScopInfo.h:149
@ PHI
MemoryKind::PHI: Models PHI nodes within the SCoP.
Definition ScopInfo.h:186
@ ExitPHI
MemoryKind::ExitPHI: Models PHI nodes in the SCoP's exit block.
Definition ScopInfo.h:196
std::map< const Instruction *, MemAcc > MapInsnToMemAcc
raw_ostream & operator<<(raw_ostream &OS, MemoryAccess::ReductionType RT)
Definition ScopInfo.cpp:916
std::vector< std::unique_ptr< MemoryAccess > > AccFuncVector
Definition ScopInfo.h:207
std::map< const Loop *, const SCEV * > LoopBoundMapType
Maps from a loop to the affine function expressing its backedge taken count.
Definition ScopInfo.h:205
bool UseInstructionNames
Definition ScopInfo.cpp:153
llvm::SetVector< const llvm::Loop * > BoxedLoopsSetTy
Set of loops (used to remember loops in non-affine subregions).
Definition ScopHelper.h:115
llvm::SmallVector< Assumption, 8 > RecordedAssumptionsTy
Definition ScopHelper.h:80
AssumptionKind
Enumeration of assumptions Polly can take.
Definition ScopHelper.h:43
SmallVector< InvariantEquivClassTy, 8 > InvariantEquivClassesTy
Type for invariant accesses equivalence classes.
Definition ScopInfo.h:1126
Helper structure for invariant memory accesses.
Definition ScopInfo.h:1089
MemoryAccess * MA
The memory access that is (partially) invariant.
Definition ScopInfo.h:1091
isl::set NonHoistableCtx
The context under which the access is not invariant.
Definition ScopInfo.h:1094
Type for equivalent invariant accesses and their domain context.
Definition ScopInfo.h:1101
MemoryAccessList InvariantAccesses
Memory accesses now treated invariant.
Definition ScopInfo.h:1110
Type * AccessType
The type of the invariant access.
Definition ScopInfo.h:1122
isl::set ExecutionContext
The execution context under which the memory location is accessed.
Definition ScopInfo.h:1116
const SCEV * IdentifyingPointer
The pointer that identifies this equivalence class.
Definition ScopInfo.h:1103
Context variables for SCoP detection.
Result run(Function &, FunctionAnalysisManager &)
static AnalysisKey Key
Definition ScopInfo.h:2732
PreservedAnalyses run(Function &, FunctionAnalysisManager &)
ScopInfoPrinterPass(raw_ostream &OS)
Definition ScopInfo.h:2740
static TupleKindPtr Domain("Domain")
static TupleKindPtr Range("Range")