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