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