Polly 20.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 RT_BOTTOM, ///< Pseudo type for the data flow analysis
475 };
476
477 using SubscriptsTy = SmallVector<const SCEV *, 4>;
478
479private:
480 /// A unique identifier for this memory access.
481 ///
482 /// The identifier is unique between all memory accesses belonging to the same
483 /// scop statement.
485
486 /// What is modeled by this MemoryAccess.
487 /// @see MemoryKind
489
490 /// Whether it a reading or writing access, and if writing, whether it
491 /// is conditional (MAY_WRITE).
493
494 /// Reduction type for reduction like accesses, RT_NONE otherwise
495 ///
496 /// An access is reduction like if it is part of a load-store chain in which
497 /// both access the same memory location (use the same LLVM-IR value
498 /// as pointer reference). Furthermore, between the load and the store there
499 /// is exactly one binary operator which is known to be associative and
500 /// commutative.
501 ///
502 /// TODO:
503 ///
504 /// We can later lift the constraint that the same LLVM-IR value defines the
505 /// memory location to handle scops such as the following:
506 ///
507 /// for i
508 /// for j
509 /// sum[i+j] = sum[i] + 3;
510 ///
511 /// Here not all iterations access the same memory location, but iterations
512 /// for which j = 0 holds do. After lifting the equality check in ScopBuilder,
513 /// subsequent transformations do not only need check if a statement is
514 /// reduction like, but they also need to verify that the reduction
515 /// property is only exploited for statement instances that load from and
516 /// store to the same data location. Doing so at dependence analysis time
517 /// could allow us to handle the above example.
519
520 /// Parent ScopStmt of this access.
522
523 /// The domain under which this access is not modeled precisely.
524 ///
525 /// The invalid domain for an access describes all parameter combinations
526 /// under which the statement looks to be executed but is in fact not because
527 /// some assumption/restriction makes the access invalid.
529
530 // Properties describing the accessed array.
531 // TODO: It might be possible to move them to ScopArrayInfo.
532 // @{
533
534 /// The base address (e.g., A for A[i+j]).
535 ///
536 /// The #BaseAddr of a memory access of kind MemoryKind::Array is the base
537 /// pointer of the memory access.
538 /// The #BaseAddr of a memory access of kind MemoryKind::PHI or
539 /// MemoryKind::ExitPHI is the PHI node itself.
540 /// The #BaseAddr of a memory access of kind MemoryKind::Value is the
541 /// instruction defining the value.
542 AssertingVH<Value> BaseAddr;
543
544 /// Type a single array element wrt. this access.
546
547 /// Size of each dimension of the accessed array.
548 SmallVector<const SCEV *, 4> Sizes;
549 // @}
550
551 // Properties describing the accessed element.
552 // @{
553
554 /// The access instruction of this memory access.
555 ///
556 /// For memory accesses of kind MemoryKind::Array the access instruction is
557 /// the Load or Store instruction performing the access.
558 ///
559 /// For memory accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI the
560 /// access instruction of a load access is the PHI instruction. The access
561 /// instruction of a PHI-store is the incoming's block's terminator
562 /// instruction.
563 ///
564 /// For memory accesses of kind MemoryKind::Value the access instruction of a
565 /// load access is nullptr because generally there can be multiple
566 /// instructions in the statement using the same llvm::Value. The access
567 /// instruction of a write access is the instruction that defines the
568 /// llvm::Value.
569 Instruction *AccessInstruction = nullptr;
570
571 /// Incoming block and value of a PHINode.
572 SmallVector<std::pair<BasicBlock *, Value *>, 4> Incoming;
573
574 /// The value associated with this memory access.
575 ///
576 /// - For array memory accesses (MemoryKind::Array) it is the loaded result
577 /// or the stored value. If the access instruction is a memory intrinsic it
578 /// the access value is also the memory intrinsic.
579 /// - For accesses of kind MemoryKind::Value it is the access instruction
580 /// itself.
581 /// - For accesses of kind MemoryKind::PHI or MemoryKind::ExitPHI it is the
582 /// PHI node itself (for both, READ and WRITE accesses).
583 ///
584 AssertingVH<Value> AccessValue;
585
586 /// Are all the subscripts affine expression?
587 bool IsAffine = true;
588
589 /// Subscript expression for each dimension.
591
592 /// Relation from statement instances to the accessed array elements.
593 ///
594 /// In the common case this relation is a function that maps a set of loop
595 /// indices to the memory address from which a value is loaded/stored:
596 ///
597 /// for i
598 /// for j
599 /// S: A[i + 3 j] = ...
600 ///
601 /// => { S[i,j] -> A[i + 3j] }
602 ///
603 /// In case the exact access function is not known, the access relation may
604 /// also be a one to all mapping { S[i,j] -> A[o] } describing that any
605 /// element accessible through A might be accessed.
606 ///
607 /// In case of an access to a larger element belonging to an array that also
608 /// contains smaller elements, the access relation models the larger access
609 /// with multiple smaller accesses of the size of the minimal array element
610 /// type:
611 ///
612 /// short *A;
613 ///
614 /// for i
615 /// S: A[i] = *((double*)&A[4 * i]);
616 ///
617 /// => { S[i] -> A[i]; S[i] -> A[o] : 4i <= o <= 4i + 3 }
619
620 /// Updated access relation read from JSCOP file.
622 // @}
623
625
627
628 /// Compute bounds on an over approximated access relation.
629 ///
630 /// @param ElementSize The size of one element accessed.
631 void computeBoundsOnAccessRelation(unsigned ElementSize);
632
633 /// Get the original access function as read from IR.
635
636 /// Return the space in which the access relation lives in.
638
639 /// Get the new access function imported or set by a pass
641
642 /// Fold the memory access to consider parametric offsets
643 ///
644 /// To recover memory accesses with array size parameters in the subscript
645 /// expression we post-process the delinearization results.
646 ///
647 /// We would normally recover from an access A[exp0(i) * N + exp1(i)] into an
648 /// array A[][N] the 2D access A[exp0(i)][exp1(i)]. However, another valid
649 /// delinearization is A[exp0(i) - 1][exp1(i) + N] which - depending on the
650 /// range of exp1(i) - may be preferable. Specifically, for cases where we
651 /// know exp1(i) is negative, we want to choose the latter expression.
652 ///
653 /// As we commonly do not have any information about the range of exp1(i),
654 /// we do not choose one of the two options, but instead create a piecewise
655 /// access function that adds the (-1, N) offsets as soon as exp1(i) becomes
656 /// negative. For a 2D array such an access function is created by applying
657 /// the piecewise map:
658 ///
659 /// [i,j] -> [i, j] : j >= 0
660 /// [i,j] -> [i-1, j+N] : j < 0
661 ///
662 /// We can generalize this mapping to arbitrary dimensions by applying this
663 /// piecewise mapping pairwise from the rightmost to the leftmost access
664 /// dimension. It would also be possible to cover a wider range by introducing
665 /// more cases and adding multiple of Ns to these cases. However, this has
666 /// not yet been necessary.
667 /// The introduction of different cases necessarily complicates the memory
668 /// access function, but cases that can be statically proven to not happen
669 /// will be eliminated later on.
670 void foldAccessRelation();
671
672 /// Create the access relation for the underlying memory intrinsic.
674
675 /// Assemble the access relation from all available information.
676 ///
677 /// In particular, used the information passes in the constructor and the
678 /// parent ScopStmt set by setStatment().
679 ///
680 /// @param SAI Info object for the accessed array.
681 void buildAccessRelation(const ScopArrayInfo *SAI);
682
683 /// Carry index overflows of dimensions with constant size to the next higher
684 /// dimension.
685 ///
686 /// For dimensions that have constant size, modulo the index by the size and
687 /// add up the carry (floored division) to the next higher dimension. This is
688 /// how overflow is defined in row-major order.
689 /// It happens e.g. when ScalarEvolution computes the offset to the base
690 /// pointer and would algebraically sum up all lower dimensions' indices of
691 /// constant size.
692 ///
693 /// Example:
694 /// float (*A)[4];
695 /// A[1][6] -> A[2][2]
697
698public:
699 /// Create a new MemoryAccess.
700 ///
701 /// @param Stmt The parent statement.
702 /// @param AccessInst The instruction doing the access.
703 /// @param BaseAddr The accessed array's address.
704 /// @param ElemType The type of the accessed array elements.
705 /// @param AccType Whether read or write access.
706 /// @param IsAffine Whether the subscripts are affine expressions.
707 /// @param Kind The kind of memory accessed.
708 /// @param Subscripts Subscript expressions
709 /// @param Sizes Dimension lengths of the accessed array.
710 MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, AccessType AccType,
711 Value *BaseAddress, Type *ElemType, bool Affine,
712 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
713 Value *AccessValue, MemoryKind Kind);
714
715 /// Create a new MemoryAccess that corresponds to @p AccRel.
716 ///
717 /// Along with @p Stmt and @p AccType it uses information about dimension
718 /// lengths of the accessed array, the type of the accessed array elements,
719 /// the name of the accessed array that is derived from the object accessible
720 /// via @p AccRel.
721 ///
722 /// @param Stmt The parent statement.
723 /// @param AccType Whether read or write access.
724 /// @param AccRel The access relation that describes the memory access.
726
727 MemoryAccess(const MemoryAccess &) = delete;
730
731 /// Add a new incoming block/value pairs for this PHI/ExitPHI access.
732 ///
733 /// @param IncomingBlock The PHI's incoming block.
734 /// @param IncomingValue The value when reaching the PHI from the @p
735 /// IncomingBlock.
736 void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue) {
737 assert(!isRead());
739 Incoming.emplace_back(std::make_pair(IncomingBlock, IncomingValue));
740 }
741
742 /// Return the list of possible PHI/ExitPHI values.
743 ///
744 /// After code generation moves some PHIs around during region simplification,
745 /// we cannot reliably locate the original PHI node and its incoming values
746 /// anymore. For this reason we remember these explicitly for all PHI-kind
747 /// accesses.
748 ArrayRef<std::pair<BasicBlock *, Value *>> getIncoming() const {
750 return Incoming;
751 }
752
753 /// Get the type of a memory access.
754 enum AccessType getType() { return AccType; }
755
756 /// Is this a reduction like access?
757 bool isReductionLike() const { return RedType != RT_NONE; }
758
759 /// Is this a read memory access?
760 bool isRead() const { return AccType == MemoryAccess::READ; }
761
762 /// Is this a must-write memory access?
763 bool isMustWrite() const { return AccType == MemoryAccess::MUST_WRITE; }
764
765 /// Is this a may-write memory access?
766 bool isMayWrite() const { return AccType == MemoryAccess::MAY_WRITE; }
767
768 /// Is this a write memory access?
769 bool isWrite() const { return isMustWrite() || isMayWrite(); }
770
771 /// Is this a memory intrinsic access (memcpy, memset, memmove)?
772 bool isMemoryIntrinsic() const {
773 return isa<MemIntrinsic>(getAccessInstruction());
774 }
775
776 /// Check if a new access relation was imported or set by a pass.
778
779 /// Return the newest access relation of this access.
780 ///
781 /// There are two possibilities:
782 /// 1) The original access relation read from the LLVM-IR.
783 /// 2) A new access relation imported from a json file or set by another
784 /// pass (e.g., for privatization).
785 ///
786 /// As 2) is by construction "newer" than 1) we return the new access
787 /// relation if present.
788 ///
792 }
793
794 /// Old name of getLatestAccessRelation().
796
797 /// Get an isl map describing the memory address accessed.
798 ///
799 /// In most cases the memory address accessed is well described by the access
800 /// relation obtained with getAccessRelation. However, in case of arrays
801 /// accessed with types of different size the access relation maps one access
802 /// to multiple smaller address locations. This method returns an isl map that
803 /// relates each dynamic statement instance to the unique memory location
804 /// that is loaded from / stored to.
805 ///
806 /// For an access relation { S[i] -> A[o] : 4i <= o <= 4i + 3 } this method
807 /// will return the address function { S[i] -> A[4i] }.
808 ///
809 /// @returns The address function for this memory access.
811
812 /// Return the access relation after the schedule was applied.
815
816 /// Get an isl string representing the access function read from IR.
817 std::string getOriginalAccessRelationStr() const;
818
819 /// Get an isl string representing a new access function, if available.
820 std::string getNewAccessRelationStr() const;
821
822 /// Get an isl string representing the latest access relation.
823 std::string getAccessRelationStr() const;
824
825 /// Get the original base address of this access (e.g. A for A[i+j]) when
826 /// detected.
827 ///
828 /// This address may differ from the base address referenced by the original
829 /// ScopArrayInfo to which this array belongs, as this memory access may
830 /// have been canonicalized to a ScopArrayInfo which has a different but
831 /// identically-valued base pointer in case invariant load hoisting is
832 /// enabled.
833 Value *getOriginalBaseAddr() const { return BaseAddr; }
834
835 /// Get the detection-time base array isl::id for this access.
837
838 /// Get the base array isl::id for this access, modifiable through
839 /// setNewAccessRelation().
841
842 /// Old name of getOriginalArrayId().
844
845 /// Get the detection-time ScopArrayInfo object for the base address.
847
848 /// Get the ScopArrayInfo object for the base address, or the one set
849 /// by setNewAccessRelation().
851
852 /// Legacy name of getOriginalScopArrayInfo().
855 }
856
857 /// Return a string representation of the access's reduction type.
858 const std::string getReductionOperatorStr() const;
859
860 /// Return a string representation of the reduction type @p RT.
861 static const std::string getReductionOperatorStr(ReductionType RT);
862
863 /// Return the element type of the accessed array wrt. this access.
864 Type *getElementType() const { return ElementType; }
865
866 /// Return the access value of this memory access.
867 Value *getAccessValue() const { return AccessValue; }
868
869 /// Return llvm::Value that is stored by this access, if available.
870 ///
871 /// PHI nodes may not have a unique value available that is stored, as in
872 /// case of region statements one out of possibly several llvm::Values
873 /// might be stored. In this case nullptr is returned.
875 assert(isWrite() && "Only write statement store values");
876 if (isAnyPHIKind()) {
877 if (Incoming.size() == 1)
878 return Incoming[0].second;
879 return nullptr;
880 }
881 return AccessValue;
882 }
883
884 /// Return the access instruction of this memory access.
885 Instruction *getAccessInstruction() const { return AccessInstruction; }
886
887 /// Return an iterator range containing the subscripts.
888 iterator_range<SubscriptsTy::const_iterator> subscripts() const {
889 return make_range(Subscripts.begin(), Subscripts.end());
890 }
891
892 /// Return the number of access function subscript.
893 unsigned getNumSubscripts() const { return Subscripts.size(); }
894
895 /// Return the access function subscript in the dimension @p Dim.
896 const SCEV *getSubscript(unsigned Dim) const { return Subscripts[Dim]; }
897
898 /// Compute the isl representation for the SCEV @p E wrt. this access.
899 ///
900 /// Note that this function will also adjust the invalid context accordingly.
901 isl::pw_aff getPwAff(const SCEV *E);
902
903 /// Get the invalid domain for this access.
905
906 /// Get the invalid context for this access.
908
909 /// Get the stride of this memory access in the specified Schedule. Schedule
910 /// is a map from the statement to a schedule where the innermost dimension is
911 /// the dimension of the innermost loop containing the statement.
912 isl::set getStride(isl::map Schedule) const;
913
914 /// Is the stride of the access equal to a certain width? Schedule is a map
915 /// from the statement to a schedule where the innermost dimension is the
916 /// dimension of the innermost loop containing the statement.
917 bool isStrideX(isl::map Schedule, int StrideWidth) const;
918
919 /// Is consecutive memory accessed for a given statement instance set?
920 /// Schedule is a map from the statement to a schedule where the innermost
921 /// dimension is the dimension of the innermost loop containing the
922 /// statement.
923 bool isStrideOne(isl::map Schedule) const;
924
925 /// Is always the same memory accessed for a given statement instance set?
926 /// Schedule is a map from the statement to a schedule where the innermost
927 /// dimension is the dimension of the innermost loop containing the
928 /// statement.
929 bool isStrideZero(isl::map Schedule) const;
930
931 /// Return the kind when this access was first detected.
933 assert(!getOriginalScopArrayInfo() /* not yet initialized */ ||
934 getOriginalScopArrayInfo()->getKind() == Kind);
935 return Kind;
936 }
937
938 /// Return the kind considering a potential setNewAccessRelation.
941 }
942
943 /// Whether this is an access of an explicit load or store in the IR.
944 bool isOriginalArrayKind() const {
946 }
947
948 /// Whether storage memory is either an custom .s2a/.phiops alloca
949 /// (false) or an existing pointer into an array (true).
950 bool isLatestArrayKind() const {
952 }
953
954 /// Old name of isOriginalArrayKind.
955 bool isArrayKind() const { return isOriginalArrayKind(); }
956
957 /// Whether this access is an array to a scalar memory object, without
958 /// considering changes by setNewAccessRelation.
959 ///
960 /// Scalar accesses are accesses to MemoryKind::Value, MemoryKind::PHI or
961 /// MemoryKind::ExitPHI.
962 bool isOriginalScalarKind() const {
964 }
965
966 /// Whether this access is an array to a scalar memory object, also
967 /// considering changes by setNewAccessRelation.
968 bool isLatestScalarKind() const {
970 }
971
972 /// Old name of isOriginalScalarKind.
973 bool isScalarKind() const { return isOriginalScalarKind(); }
974
975 /// Was this MemoryAccess detected as a scalar dependences?
976 bool isOriginalValueKind() const {
978 }
979
980 /// Is this MemoryAccess currently modeling scalar dependences?
981 bool isLatestValueKind() const {
983 }
984
985 /// Old name of isOriginalValueKind().
986 bool isValueKind() const { return isOriginalValueKind(); }
987
988 /// Was this MemoryAccess detected as a special PHI node access?
989 bool isOriginalPHIKind() const {
991 }
992
993 /// Is this MemoryAccess modeling special PHI node accesses, also
994 /// considering a potential change by setNewAccessRelation?
995 bool isLatestPHIKind() const { return getLatestKind() == MemoryKind::PHI; }
996
997 /// Old name of isOriginalPHIKind.
998 bool isPHIKind() const { return isOriginalPHIKind(); }
999
1000 /// Was this MemoryAccess detected as the accesses of a PHI node in the
1001 /// SCoP's exit block?
1004 }
1005
1006 /// Is this MemoryAccess modeling the accesses of a PHI node in the
1007 /// SCoP's exit block? Can be changed to an array access using
1008 /// setNewAccessRelation().
1009 bool isLatestExitPHIKind() const {
1011 }
1012
1013 /// Old name of isOriginalExitPHIKind().
1014 bool isExitPHIKind() const { return isOriginalExitPHIKind(); }
1015
1016 /// Was this access detected as one of the two PHI types?
1019 }
1020
1021 /// Does this access originate from one of the two PHI types? Can be
1022 /// changed to an array access using setNewAccessRelation().
1023 bool isLatestAnyPHIKind() const {
1025 }
1026
1027 /// Old name of isOriginalAnyPHIKind().
1028 bool isAnyPHIKind() const { return isOriginalAnyPHIKind(); }
1029
1030 /// Get the statement that contains this memory access.
1031 ScopStmt *getStatement() const { return Statement; }
1032
1033 /// Get the reduction type of this access
1035
1036 /// Update the original access relation.
1037 ///
1038 /// We need to update the original access relation during scop construction,
1039 /// when unifying the memory accesses that access the same scop array info
1040 /// object. After the scop has been constructed, the original access relation
1041 /// should not be changed any more. Instead setNewAccessRelation should
1042 /// be called.
1044
1045 /// Set the updated access relation read from JSCOP file.
1047
1048 /// Return whether the MemoryyAccess is a partial access. That is, the access
1049 /// is not executed in some instances of the parent statement's domain.
1050 bool isLatestPartialAccess() const;
1051
1052 /// Mark this a reduction like access
1054
1055 /// Align the parameters in the access relation to the scop context
1056 void realignParams();
1057
1058 /// Update the dimensionality of the memory access.
1059 ///
1060 /// During scop construction some memory accesses may not be constructed with
1061 /// their full dimensionality, but outer dimensions may have been omitted if
1062 /// they took the value 'zero'. By updating the dimensionality of the
1063 /// statement we add additional zero-valued dimensions to match the
1064 /// dimensionality of the ScopArrayInfo object that belongs to this memory
1065 /// access.
1066 void updateDimensionality();
1067
1068 /// Get identifier for the memory access.
1069 ///
1070 /// This identifier is unique for all accesses that belong to the same scop
1071 /// statement.
1072 isl::id getId() const;
1073
1074 /// Print the MemoryAccess.
1075 ///
1076 /// @param OS The output stream the MemoryAccess is printed to.
1077 void print(raw_ostream &OS) const;
1078
1079#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1080 /// Print the MemoryAccess to stderr.
1081 void dump() const;
1082#endif
1083
1084 /// Is the memory access affine?
1085 bool isAffine() const { return IsAffine; }
1086};
1087
1088raw_ostream &operator<<(raw_ostream &OS, MemoryAccess::ReductionType RT);
1089
1090/// Ordered list type to hold accesses.
1091using MemoryAccessList = std::forward_list<MemoryAccess *>;
1092
1093/// Helper structure for invariant memory accesses.
1095 /// The memory access that is (partially) invariant.
1097
1098 /// The context under which the access is not invariant.
1100};
1101
1102/// Ordered container type to hold invariant accesses.
1103using InvariantAccessesTy = SmallVector<InvariantAccess, 8>;
1104
1105/// Type for equivalent invariant accesses and their domain context.
1107 /// The pointer that identifies this equivalence class
1109
1110 /// Memory accesses now treated invariant
1111 ///
1112 /// These memory accesses access the pointer location that identifies
1113 /// this equivalence class. They are treated as invariant and hoisted during
1114 /// code generation.
1116
1117 /// The execution context under which the memory location is accessed
1118 ///
1119 /// It is the union of the execution domains of the memory accesses in the
1120 /// InvariantAccesses list.
1122
1123 /// The type of the invariant access
1124 ///
1125 /// It is used to differentiate between differently typed invariant loads from
1126 /// the same location.
1128};
1129
1130/// Type for invariant accesses equivalence classes.
1131using InvariantEquivClassesTy = SmallVector<InvariantEquivClassTy, 8>;
1132
1133/// Statement of the Scop
1134///
1135/// A Scop statement represents an instruction in the Scop.
1136///
1137/// It is further described by its iteration domain, its schedule and its data
1138/// accesses.
1139/// At the moment every statement represents a single basic block of LLVM-IR.
1140class ScopStmt final {
1141 friend class ScopBuilder;
1142
1143public:
1144 using MemoryAccessVec = llvm::SmallVector<MemoryAccess *, 8>;
1145 /// Create the ScopStmt from a BasicBlock.
1146 ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, Loop *SurroundingLoop,
1147 std::vector<Instruction *> Instructions);
1148
1149 /// Create an overapproximating ScopStmt for the region @p R.
1150 ///
1151 /// @param EntryBlockInstructions The list of instructions that belong to the
1152 /// entry block of the region statement.
1153 /// Instructions are only tracked for entry
1154 /// blocks for now. We currently do not allow
1155 /// to modify the instructions of blocks later
1156 /// in the region statement.
1157 ScopStmt(Scop &parent, Region &R, StringRef Name, Loop *SurroundingLoop,
1158 std::vector<Instruction *> EntryBlockInstructions);
1159
1160 /// Create a copy statement.
1161 ///
1162 /// @param Stmt The parent statement.
1163 /// @param SourceRel The source location.
1164 /// @param TargetRel The target location.
1165 /// @param Domain The original domain under which the copy statement would
1166 /// be executed.
1167 ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel,
1169
1170 ScopStmt(const ScopStmt &) = delete;
1171 const ScopStmt &operator=(const ScopStmt &) = delete;
1173
1174private:
1175 /// Polyhedral description
1176 //@{
1177
1178 /// The Scop containing this ScopStmt.
1180
1181 /// The domain under which this statement is not modeled precisely.
1182 ///
1183 /// The invalid domain for a statement describes all parameter combinations
1184 /// under which the statement looks to be executed but is in fact not because
1185 /// some assumption/restriction makes the statement/scop invalid.
1187
1188 /// The iteration domain describes the set of iterations for which this
1189 /// statement is executed.
1190 ///
1191 /// Example:
1192 /// for (i = 0; i < 100 + b; ++i)
1193 /// for (j = 0; j < i; ++j)
1194 /// S(i,j);
1195 ///
1196 /// 'S' is executed for different values of i and j. A vector of all
1197 /// induction variables around S (i, j) is called iteration vector.
1198 /// The domain describes the set of possible iteration vectors.
1199 ///
1200 /// In this case it is:
1201 ///
1202 /// Domain: 0 <= i <= 100 + b
1203 /// 0 <= j <= i
1204 ///
1205 /// A pair of statement and iteration vector (S, (5,3)) is called statement
1206 /// instance.
1208
1209 /// The memory accesses of this statement.
1210 ///
1211 /// The only side effects of a statement are its memory accesses.
1213
1214 /// Mapping from instructions to (scalar) memory accesses.
1215 DenseMap<const Instruction *, MemoryAccessList> InstructionToAccess;
1216
1217 /// The set of values defined elsewhere required in this ScopStmt and
1218 /// their MemoryKind::Value READ MemoryAccesses.
1219 DenseMap<Value *, MemoryAccess *> ValueReads;
1220
1221 /// The set of values defined in this ScopStmt that are required
1222 /// elsewhere, mapped to their MemoryKind::Value WRITE MemoryAccesses.
1223 DenseMap<Instruction *, MemoryAccess *> ValueWrites;
1224
1225 /// Map from PHI nodes to its incoming value when coming from this
1226 /// statement.
1227 ///
1228 /// Non-affine subregions can have multiple exiting blocks that are incoming
1229 /// blocks of the PHI nodes. This map ensures that there is only one write
1230 /// operation for the complete subregion. A PHI selecting the relevant value
1231 /// will be inserted.
1232 DenseMap<PHINode *, MemoryAccess *> PHIWrites;
1233
1234 /// Map from PHI nodes to its read access in this statement.
1235 DenseMap<PHINode *, MemoryAccess *> PHIReads;
1236
1237 //@}
1238
1239 /// A SCoP statement represents either a basic block (affine/precise case) or
1240 /// a whole region (non-affine case).
1241 ///
1242 /// Only one of the following two members will therefore be set and indicate
1243 /// which kind of statement this is.
1244 ///
1245 ///{
1246
1247 /// The BasicBlock represented by this statement (in the affine case).
1248 BasicBlock *BB = nullptr;
1249
1250 /// The region represented by this statement (in the non-affine case).
1251 Region *R = nullptr;
1252
1253 ///}
1254
1255 /// The isl AST build for the new generated AST.
1257
1258 SmallVector<Loop *, 4> NestLoops;
1259
1260 std::string BaseName;
1261
1262 /// The closest loop that contains this statement.
1264
1265 /// Vector for Instructions in this statement.
1266 std::vector<Instruction *> Instructions;
1267
1268 /// Remove @p MA from dictionaries pointing to them.
1270
1271public:
1272 /// Get an isl_ctx pointer.
1273 isl::ctx getIslCtx() const;
1274
1275 /// Get the iteration domain of this ScopStmt.
1276 ///
1277 /// @return The iteration domain of this ScopStmt.
1278 isl::set getDomain() const;
1279
1280 /// Get the space of the iteration domain
1281 ///
1282 /// @return The space of the iteration domain
1283 isl::space getDomainSpace() const;
1284
1285 /// Get the id of the iteration domain space
1286 ///
1287 /// @return The id of the iteration domain space
1288 isl::id getDomainId() const;
1289
1290 /// Get an isl string representing this domain.
1291 std::string getDomainStr() const;
1292
1293 /// Get the schedule function of this ScopStmt.
1294 ///
1295 /// @return The schedule function of this ScopStmt, if it does not contain
1296 /// extension nodes, and nullptr, otherwise.
1297 isl::map getSchedule() const;
1298
1299 /// Get an isl string representing this schedule.
1300 ///
1301 /// @return An isl string representing this schedule, if it does not contain
1302 /// extension nodes, and an empty string, otherwise.
1303 std::string getScheduleStr() const;
1304
1305 /// Get the invalid domain for this statement.
1307
1308 /// Get the invalid context for this statement.
1310
1311 /// Set the invalid context for this statement to @p ID.
1312 void setInvalidDomain(isl::set ID);
1313
1314 /// Get the BasicBlock represented by this ScopStmt (if any).
1315 ///
1316 /// @return The BasicBlock represented by this ScopStmt, or null if the
1317 /// statement represents a region.
1318 BasicBlock *getBasicBlock() const { return BB; }
1319
1320 /// Return true if this statement represents a single basic block.
1321 bool isBlockStmt() const { return BB != nullptr; }
1322
1323 /// Return true if this is a copy statement.
1324 bool isCopyStmt() const { return BB == nullptr && R == nullptr; }
1325
1326 /// Get the region represented by this ScopStmt (if any).
1327 ///
1328 /// @return The region represented by this ScopStmt, or null if the statement
1329 /// represents a basic block.
1330 Region *getRegion() const { return R; }
1331
1332 /// Return true if this statement represents a whole region.
1333 bool isRegionStmt() const { return R != nullptr; }
1334
1335 /// Return a BasicBlock from this statement.
1336 ///
1337 /// For block statements, it returns the BasicBlock itself. For subregion
1338 /// statements, return its entry block.
1339 BasicBlock *getEntryBlock() const;
1340
1341 /// Return whether @p L is boxed within this statement.
1342 bool contains(const Loop *L) const {
1343 // Block statements never contain loops.
1344 if (isBlockStmt())
1345 return false;
1346
1347 return getRegion()->contains(L);
1348 }
1349
1350 /// Return whether this statement represents @p BB.
1351 bool represents(BasicBlock *BB) const {
1352 if (isCopyStmt())
1353 return false;
1354 if (isBlockStmt())
1355 return BB == getBasicBlock();
1356 return getRegion()->contains(BB);
1357 }
1358
1359 /// Return whether this statement contains @p Inst.
1360 bool contains(Instruction *Inst) const {
1361 if (!Inst)
1362 return false;
1363 if (isBlockStmt())
1364 return llvm::is_contained(Instructions, Inst);
1365 return represents(Inst->getParent());
1366 }
1367
1368 /// Return the closest innermost loop that contains this statement, but is not
1369 /// contained in it.
1370 ///
1371 /// For block statement, this is just the loop that contains the block. Region
1372 /// statements can contain boxed loops, so getting the loop of one of the
1373 /// region's BBs might return such an inner loop. For instance, the region's
1374 /// entry could be a header of a loop, but the region might extend to BBs
1375 /// after the loop exit. Similarly, the region might only contain parts of the
1376 /// loop body and still include the loop header.
1377 ///
1378 /// Most of the time the surrounding loop is the top element of #NestLoops,
1379 /// except when it is empty. In that case it return the loop that the whole
1380 /// SCoP is contained in. That can be nullptr if there is no such loop.
1381 Loop *getSurroundingLoop() const {
1382 assert(!isCopyStmt() &&
1383 "No surrounding loop for artificially created statements");
1384 return SurroundingLoop;
1385 }
1386
1387 /// Return true if this statement does not contain any accesses.
1388 bool isEmpty() const { return MemAccs.empty(); }
1389
1390 /// Find all array accesses for @p Inst.
1391 ///
1392 /// @param Inst The instruction accessing an array.
1393 ///
1394 /// @return A list of array accesses (MemoryKind::Array) accessed by @p Inst.
1395 /// If there is no such access, it returns nullptr.
1396 const MemoryAccessList *
1397 lookupArrayAccessesFor(const Instruction *Inst) const {
1398 auto It = InstructionToAccess.find(Inst);
1399 if (It == InstructionToAccess.end())
1400 return nullptr;
1401 if (It->second.empty())
1402 return nullptr;
1403 return &It->second;
1404 }
1405
1406 /// Return the only array access for @p Inst, if existing.
1407 ///
1408 /// @param Inst The instruction for which to look up the access.
1409 /// @returns The unique array memory access related to Inst or nullptr if
1410 /// no array access exists
1411 MemoryAccess *getArrayAccessOrNULLFor(const Instruction *Inst) const {
1412 auto It = InstructionToAccess.find(Inst);
1413 if (It == InstructionToAccess.end())
1414 return nullptr;
1415
1416 MemoryAccess *ArrayAccess = nullptr;
1417
1418 for (auto Access : It->getSecond()) {
1419 if (!Access->isArrayKind())
1420 continue;
1421
1422 assert(!ArrayAccess && "More then one array access for instruction");
1423
1424 ArrayAccess = Access;
1425 }
1426
1427 return ArrayAccess;
1428 }
1429
1430 /// Return the only array access for @p Inst.
1431 ///
1432 /// @param Inst The instruction for which to look up the access.
1433 /// @returns The unique array memory access related to Inst.
1434 MemoryAccess &getArrayAccessFor(const Instruction *Inst) const {
1435 MemoryAccess *ArrayAccess = getArrayAccessOrNULLFor(Inst);
1436
1437 assert(ArrayAccess && "No array access found for instruction!");
1438 return *ArrayAccess;
1439 }
1440
1441 /// Return the MemoryAccess that writes the value of an instruction
1442 /// defined in this statement, or nullptr if not existing, respectively
1443 /// not yet added.
1444 MemoryAccess *lookupValueWriteOf(Instruction *Inst) const {
1445 assert((isRegionStmt() && R->contains(Inst)) ||
1446 (!isRegionStmt() && Inst->getParent() == BB));
1447 return ValueWrites.lookup(Inst);
1448 }
1449
1450 /// Return the MemoryAccess that reloads a value, or nullptr if not
1451 /// existing, respectively not yet added.
1452 MemoryAccess *lookupValueReadOf(Value *Inst) const {
1453 return ValueReads.lookup(Inst);
1454 }
1455
1456 /// Return the MemoryAccess that loads a PHINode value, or nullptr if not
1457 /// existing, respectively not yet added.
1459 return PHIReads.lookup(PHI);
1460 }
1461
1462 /// Return the PHI write MemoryAccess for the incoming values from any
1463 /// basic block in this ScopStmt, or nullptr if not existing,
1464 /// respectively not yet added.
1466 assert(isBlockStmt() || R->getExit() == PHI->getParent());
1467 return PHIWrites.lookup(PHI);
1468 }
1469
1470 /// Return the input access of the value, or null if no such MemoryAccess
1471 /// exists.
1472 ///
1473 /// The input access is the MemoryAccess that makes an inter-statement value
1474 /// available in this statement by reading it at the start of this statement.
1475 /// This can be a MemoryKind::Value if defined in another statement or a
1476 /// MemoryKind::PHI if the value is a PHINode in this statement.
1478 if (isa<PHINode>(Val))
1479 if (auto InputMA = lookupPHIReadOf(cast<PHINode>(Val))) {
1480 assert(!lookupValueReadOf(Val) && "input accesses must be unique; a "
1481 "statement cannot read a .s2a and "
1482 ".phiops simultaneously");
1483 return InputMA;
1484 }
1485
1486 if (auto *InputMA = lookupValueReadOf(Val))
1487 return InputMA;
1488
1489 return nullptr;
1490 }
1491
1492 /// Add @p Access to this statement's list of accesses.
1493 ///
1494 /// @param Access The access to add.
1495 /// @param Prepend If true, will add @p Access before all other instructions
1496 /// (instead of appending it).
1497 void addAccess(MemoryAccess *Access, bool Preprend = false);
1498
1499 /// Remove a MemoryAccess from this statement.
1500 ///
1501 /// Note that scalar accesses that are caused by MA will
1502 /// be eliminated too.
1504
1505 /// Remove @p MA from this statement.
1506 ///
1507 /// In contrast to removeMemoryAccess(), no other access will be eliminated.
1508 ///
1509 /// @param MA The MemoryAccess to be removed.
1510 /// @param AfterHoisting If true, also remove from data access lists.
1511 /// These lists are filled during
1512 /// ScopBuilder::buildAccessRelations. Therefore, if this
1513 /// method is called before buildAccessRelations, false
1514 /// must be passed.
1515 void removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting = true);
1516
1517 using iterator = MemoryAccessVec::iterator;
1518 using const_iterator = MemoryAccessVec::const_iterator;
1519
1520 iterator begin() { return MemAccs.begin(); }
1521 iterator end() { return MemAccs.end(); }
1522 const_iterator begin() const { return MemAccs.begin(); }
1523 const_iterator end() const { return MemAccs.end(); }
1524 size_t size() const { return MemAccs.size(); }
1525
1526 unsigned getNumIterators() const;
1527
1528 Scop *getParent() { return &Parent; }
1529 const Scop *getParent() const { return &Parent; }
1530
1531 const std::vector<Instruction *> &getInstructions() const {
1532 return Instructions;
1533 }
1534
1535 /// Set the list of instructions for this statement. It replaces the current
1536 /// list.
1537 void setInstructions(ArrayRef<Instruction *> Range) {
1538 Instructions.assign(Range.begin(), Range.end());
1539 }
1540
1541 std::vector<Instruction *>::const_iterator insts_begin() const {
1542 return Instructions.begin();
1543 }
1544
1545 std::vector<Instruction *>::const_iterator insts_end() const {
1546 return Instructions.end();
1547 }
1548
1549 /// The range of instructions in this statement.
1550 iterator_range<std::vector<Instruction *>::const_iterator> insts() const {
1551 return {insts_begin(), insts_end()};
1552 }
1553
1554 /// Insert an instruction before all other instructions in this statement.
1555 void prependInstruction(Instruction *Inst) {
1556 Instructions.insert(Instructions.begin(), Inst);
1557 }
1558
1559 const char *getBaseName() const;
1560
1561 /// Set the isl AST build.
1563
1564 /// Get the isl AST build.
1566
1567 /// Restrict the domain of the statement.
1568 ///
1569 /// @param NewDomain The new statement domain.
1570 void restrictDomain(isl::set NewDomain);
1571
1572 /// Get the loop for a dimension.
1573 ///
1574 /// @param Dimension The dimension of the induction variable
1575 /// @return The loop at a certain dimension.
1576 Loop *getLoopForDimension(unsigned Dimension) const;
1577
1578 /// Align the parameters in the statement to the scop context
1579 void realignParams();
1580
1581 /// Print the ScopStmt.
1582 ///
1583 /// @param OS The output stream the ScopStmt is printed to.
1584 /// @param PrintInstructions Whether to print the statement's instructions as
1585 /// well.
1586 void print(raw_ostream &OS, bool PrintInstructions) const;
1587
1588 /// Print the instructions in ScopStmt.
1589 ///
1590 void printInstructions(raw_ostream &OS) const;
1591
1592 /// Check whether there is a value read access for @p V in this statement, and
1593 /// if not, create one.
1594 ///
1595 /// This allows to add MemoryAccesses after the initial creation of the Scop
1596 /// by ScopBuilder.
1597 ///
1598 /// @return The already existing or newly created MemoryKind::Value READ
1599 /// MemoryAccess.
1600 ///
1601 /// @see ScopBuilder::ensureValueRead(Value*,ScopStmt*)
1602 MemoryAccess *ensureValueRead(Value *V);
1603
1604#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1605 /// Print the ScopStmt to stderr.
1606 void dump() const;
1607#endif
1608};
1609
1610/// Print ScopStmt S to raw_ostream OS.
1611raw_ostream &operator<<(raw_ostream &OS, const ScopStmt &S);
1612
1613/// Static Control Part
1614///
1615/// A Scop is the polyhedral representation of a control flow region detected
1616/// by the Scop detection. It is generated by translating the LLVM-IR and
1617/// abstracting its effects.
1618///
1619/// A Scop consists of a set of:
1620///
1621/// * A set of statements executed in the Scop.
1622///
1623/// * A set of global parameters
1624/// Those parameters are scalar integer values, which are constant during
1625/// execution.
1626///
1627/// * A context
1628/// This context contains information about the values the parameters
1629/// can take and relations between different parameters.
1630class Scop final {
1631public:
1632 /// Type to represent a pair of minimal/maximal access to an array.
1633 using MinMaxAccessTy = std::pair<isl::pw_multi_aff, isl::pw_multi_aff>;
1634
1635 /// Vector of minimal/maximal accesses to different arrays.
1636 using MinMaxVectorTy = SmallVector<MinMaxAccessTy, 4>;
1637
1638 /// Pair of minimal/maximal access vectors representing
1639 /// read write and read only accesses
1640 using MinMaxVectorPairTy = std::pair<MinMaxVectorTy, MinMaxVectorTy>;
1641
1642 /// Vector of pair of minimal/maximal access vectors representing
1643 /// non read only and read only accesses for each alias group.
1644 using MinMaxVectorPairVectorTy = SmallVector<MinMaxVectorPairTy, 4>;
1645
1646private:
1647 friend class ScopBuilder;
1648
1649 /// Isl context.
1650 ///
1651 /// We need a shared_ptr with reference counter to delete the context when all
1652 /// isl objects are deleted. We will distribute the shared_ptr to all objects
1653 /// that use the context to create isl objects, and increase the reference
1654 /// counter. By doing this, we guarantee that the context is deleted when we
1655 /// delete the last object that creates isl objects with the context. This
1656 /// declaration needs to be the first in class to gracefully destroy all isl
1657 /// objects before the context.
1658 std::shared_ptr<isl_ctx> IslCtx;
1659
1660 ScalarEvolution *SE;
1661 DominatorTree *DT;
1662
1663 /// The underlying Region.
1664 Region &R;
1665
1666 /// The name of the SCoP (identical to the regions name)
1667 std::optional<std::string> name;
1668
1669 // Access functions of the SCoP.
1670 //
1671 // This owns all the MemoryAccess objects of the Scop created in this pass.
1673
1674 /// Flag to indicate that the scheduler actually optimized the SCoP.
1675 bool IsOptimized = false;
1676
1677 /// True if the underlying region has a single exiting block.
1679
1680 /// Flag to remember if the SCoP contained an error block or not.
1681 bool HasErrorBlock = false;
1682
1683 /// Max loop depth.
1684 unsigned MaxLoopDepth = 0;
1685
1686 /// Number of copy statements.
1687 unsigned CopyStmtsNum = 0;
1688
1689 using StmtSet = std::list<ScopStmt>;
1690
1691 /// The statements in this Scop.
1693
1694 /// Parameters of this Scop
1696
1697 /// Mapping from parameters to their ids.
1698 DenseMap<const SCEV *, isl::id> ParameterIds;
1699
1700 /// The context of the SCoP created during SCoP detection.
1702
1703 /// OptimizationRemarkEmitter object for displaying diagnostic remarks
1704 OptimizationRemarkEmitter &ORE;
1705
1706 /// A map from basic blocks to vector of SCoP statements. Currently this
1707 /// vector comprises only of a single statement.
1708 DenseMap<BasicBlock *, std::vector<ScopStmt *>> StmtMap;
1709
1710 /// A map from instructions to SCoP statements.
1711 DenseMap<Instruction *, ScopStmt *> InstStmtMap;
1712
1713 /// A map from basic blocks to their domains.
1714 DenseMap<BasicBlock *, isl::set> DomainMap;
1715
1716 /// Constraints on parameters.
1718
1719 /// The affinator used to translate SCEVs to isl expressions.
1721
1723 std::map<std::pair<AssertingVH<const Value>, MemoryKind>,
1724 std::unique_ptr<ScopArrayInfo>>;
1725
1726 using ArrayNameMapTy = StringMap<std::unique_ptr<ScopArrayInfo>>;
1727
1728 using ArrayInfoSetTy = SetVector<ScopArrayInfo *>;
1729
1730 /// A map to remember ScopArrayInfo objects for all base pointers.
1731 ///
1732 /// As PHI nodes may have two array info objects associated, we add a flag
1733 /// that distinguishes between the PHI node specific ArrayInfo object
1734 /// and the normal one.
1736
1737 /// A map to remember ScopArrayInfo objects for all names of memory
1738 /// references.
1740
1741 /// A set to remember ScopArrayInfo objects.
1742 /// @see Scop::ScopArrayInfoMap
1744
1745 /// The assumptions under which this scop was built.
1746 ///
1747 /// When constructing a scop sometimes the exact representation of a statement
1748 /// or condition would be very complex, but there is a common case which is a
1749 /// lot simpler, but which is only valid under certain assumptions. The
1750 /// assumed context records the assumptions taken during the construction of
1751 /// this scop and that need to be code generated as a run-time test.
1753
1754 /// The restrictions under which this SCoP was built.
1755 ///
1756 /// The invalid context is similar to the assumed context as it contains
1757 /// constraints over the parameters. However, while we need the constraints
1758 /// in the assumed context to be "true" the constraints in the invalid context
1759 /// need to be "false". Otherwise they behave the same.
1761
1762 /// The context under which the SCoP must have defined behavior. Optimizer and
1763 /// code generator can assume that the SCoP will only be executed with
1764 /// parameter values within this context. This might be either because we can
1765 /// prove that other values are impossible or explicitly have undefined
1766 /// behavior, such as due to no-wrap flags. If this becomes too complex, can
1767 /// also be nullptr.
1768 ///
1769 /// In contrast to Scop::AssumedContext and Scop::InvalidContext, these do not
1770 /// need to be checked at runtime.
1771 ///
1772 /// Scop::Context on the other side is an overapproximation and does not
1773 /// include all requirements, but is always defined. However, there is still
1774 /// no guarantee that there is no undefined behavior in
1775 /// DefinedBehaviorContext.
1777
1778 /// The schedule of the SCoP
1779 ///
1780 /// The schedule of the SCoP describes the execution order of the statements
1781 /// in the scop by assigning each statement instance a possibly
1782 /// multi-dimensional execution time. The schedule is stored as a tree of
1783 /// schedule nodes.
1784 ///
1785 /// The most common nodes in a schedule tree are so-called band nodes. Band
1786 /// nodes map statement instances into a multi dimensional schedule space.
1787 /// This space can be seen as a multi-dimensional clock.
1788 ///
1789 /// Example:
1790 ///
1791 /// <S,(5,4)> may be mapped to (5,4) by this schedule:
1792 ///
1793 /// s0 = i (Year of execution)
1794 /// s1 = j (Day of execution)
1795 ///
1796 /// or to (9, 20) by this schedule:
1797 ///
1798 /// s0 = i + j (Year of execution)
1799 /// s1 = 20 (Day of execution)
1800 ///
1801 /// The order statement instances are executed is defined by the
1802 /// schedule vectors they are mapped to. A statement instance
1803 /// <A, (i, j, ..)> is executed before a statement instance <B, (i', ..)>, if
1804 /// the schedule vector of A is lexicographic smaller than the schedule
1805 /// vector of B.
1806 ///
1807 /// Besides band nodes, schedule trees contain additional nodes that specify
1808 /// a textual ordering between two subtrees or filter nodes that filter the
1809 /// set of statement instances that will be scheduled in a subtree. There
1810 /// are also several other nodes. A full description of the different nodes
1811 /// in a schedule tree is given in the isl manual.
1813
1814 /// Is this Scop marked as not to be transformed by an optimization heuristic?
1816
1817 /// Whether the schedule has been modified after derived from the CFG by
1818 /// ScopBuilder.
1819 bool ScheduleModified = false;
1820
1821 /// The set of minimal/maximal accesses for each alias group.
1822 ///
1823 /// When building runtime alias checks we look at all memory instructions and
1824 /// build so called alias groups. Each group contains a set of accesses to
1825 /// different base arrays which might alias with each other. However, between
1826 /// alias groups there is no aliasing possible.
1827 ///
1828 /// In a program with int and float pointers annotated with tbaa information
1829 /// we would probably generate two alias groups, one for the int pointers and
1830 /// one for the float pointers.
1831 ///
1832 /// During code generation we will create a runtime alias check for each alias
1833 /// group to ensure the SCoP is executed in an alias free environment.
1835
1836 /// Mapping from invariant loads to the representing invariant load of
1837 /// their equivalence class.
1838 ValueToValueMap InvEquivClassVMap;
1839
1840 /// List of invariant accesses.
1842
1843 /// The smallest array index not yet assigned.
1844 long ArrayIdx = 0;
1845
1846 /// The smallest statement index not yet assigned.
1847 long StmtIdx = 0;
1848
1849 /// A number that uniquely represents a Scop within its function
1850 const int ID;
1851
1852 /// Map of values to the MemoryAccess that writes its definition.
1853 ///
1854 /// There must be at most one definition per llvm::Instruction in a SCoP.
1855 DenseMap<Value *, MemoryAccess *> ValueDefAccs;
1856
1857 /// Map of values to the MemoryAccess that reads a PHI.
1858 DenseMap<PHINode *, MemoryAccess *> PHIReadAccs;
1859
1860 /// List of all uses (i.e. read MemoryAccesses) for a MemoryKind::Value
1861 /// scalar.
1862 DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>> ValueUseAccs;
1863
1864 /// List of all incoming values (write MemoryAccess) of a MemoryKind::PHI or
1865 /// MemoryKind::ExitPHI scalar.
1866 DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>>
1868
1869 /// Scop constructor; invoked from ScopBuilder::buildScop.
1870 Scop(Region &R, ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT,
1871 ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE,
1872 int ID);
1873
1874 //@}
1875
1876 /// Return the access for the base ptr of @p MA if any.
1878
1879 /// Create an id for @p Param and store it in the ParameterIds map.
1880 void createParameterId(const SCEV *Param);
1881
1882 /// Build the Context of the Scop.
1883 void buildContext();
1884
1885 /// Add the bounds of the parameters to the context.
1886 void addParameterBounds();
1887
1888 /// Simplify the assumed and invalid context.
1889 void simplifyContexts();
1890
1891 /// Create a new SCoP statement for @p BB.
1892 ///
1893 /// A new statement for @p BB will be created and added to the statement
1894 /// vector
1895 /// and map.
1896 ///
1897 /// @param BB The basic block we build the statement for.
1898 /// @param Name The name of the new statement.
1899 /// @param SurroundingLoop The loop the created statement is contained in.
1900 /// @param Instructions The instructions in the statement.
1901 void addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop,
1902 std::vector<Instruction *> Instructions);
1903
1904 /// Create a new SCoP statement for @p R.
1905 ///
1906 /// A new statement for @p R will be created and added to the statement vector
1907 /// and map.
1908 ///
1909 /// @param R The region we build the statement for.
1910 /// @param Name The name of the new statement.
1911 /// @param SurroundingLoop The loop the created statement is contained
1912 /// in.
1913 /// @param EntryBlockInstructions The (interesting) instructions in the
1914 /// entry block of the region statement.
1915 void addScopStmt(Region *R, StringRef Name, Loop *SurroundingLoop,
1916 std::vector<Instruction *> EntryBlockInstructions);
1917
1918 /// Removes @p Stmt from the StmtMap.
1919 void removeFromStmtMap(ScopStmt &Stmt);
1920
1921 /// Removes all statements where the entry block of the statement does not
1922 /// have a corresponding domain in the domain map (or it is empty).
1924
1925 /// Collect all memory access relations of a given type.
1926 ///
1927 /// @param Predicate A predicate function that returns true if an access is
1928 /// of a given type.
1929 ///
1930 /// @returns The set of memory accesses in the scop that match the predicate.
1932 getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate);
1933
1934 /// @name Helper functions for printing the Scop.
1935 ///
1936 //@{
1937 void printContext(raw_ostream &OS) const;
1938 void printArrayInfo(raw_ostream &OS) const;
1939 void printStatements(raw_ostream &OS, bool PrintInstructions) const;
1940 void printAliasAssumptions(raw_ostream &OS) const;
1941 //@}
1942
1943public:
1944 Scop(const Scop &) = delete;
1945 Scop &operator=(const Scop &) = delete;
1947
1948 /// Increment actual number of aliasing assumptions taken
1949 ///
1950 /// @param Step Number of new aliasing assumptions which should be added to
1951 /// the number of already taken assumptions.
1952 static void incrementNumberOfAliasingAssumptions(unsigned Step);
1953
1954 /// Get the count of copy statements added to this Scop.
1955 ///
1956 /// @return The count of copy statements added to this Scop.
1957 unsigned getCopyStmtsNum() { return CopyStmtsNum; }
1958
1959 /// Create a new copy statement.
1960 ///
1961 /// A new statement will be created and added to the statement vector.
1962 ///
1963 /// @param SourceRel The source location.
1964 /// @param TargetRel The target location.
1965 /// @param Domain The original domain under which the copy statement would
1966 /// be executed.
1967 ScopStmt *addScopStmt(isl::map SourceRel, isl::map TargetRel,
1969
1970 /// Add the access function to all MemoryAccess objects of the Scop
1971 /// created in this pass.
1973 AccessFunctions.emplace_back(Access);
1974
1975 // Register value definitions.
1976 if (Access->isWrite() && Access->isOriginalValueKind()) {
1977 assert(!ValueDefAccs.count(Access->getAccessValue()) &&
1978 "there can be just one definition per value");
1979 ValueDefAccs[Access->getAccessValue()] = Access;
1980 } else if (Access->isRead() && Access->isOriginalPHIKind()) {
1981 PHINode *PHI = cast<PHINode>(Access->getAccessInstruction());
1982 assert(!PHIReadAccs.count(PHI) &&
1983 "there can be just one PHI read per PHINode");
1984 PHIReadAccs[PHI] = Access;
1985 }
1986 }
1987
1988 /// Add metadata for @p Access.
1989 void addAccessData(MemoryAccess *Access);
1990
1991 /// Add new invariant access equivalence class
1992 void
1993 addInvariantEquivClass(const InvariantEquivClassTy &InvariantEquivClass) {
1994 InvariantEquivClasses.emplace_back(InvariantEquivClass);
1995 }
1996
1997 /// Add mapping from invariant loads to the representing invariant load of
1998 /// their equivalence class.
1999 void addInvariantLoadMapping(const Value *LoadInst, Value *ClassRep) {
2000 InvEquivClassVMap[LoadInst] = ClassRep;
2001 }
2002
2003 /// Remove the metadata stored for @p Access.
2004 void removeAccessData(MemoryAccess *Access);
2005
2006 /// Return the scalar evolution.
2007 ScalarEvolution *getSE() const;
2008
2009 /// Return the dominator tree.
2010 DominatorTree *getDT() const { return DT; }
2011
2012 /// Return the LoopInfo used for this Scop.
2013 LoopInfo *getLI() const { return Affinator.getLI(); }
2014
2015 /// Get the count of parameters used in this Scop.
2016 ///
2017 /// @return The count of parameters used in this Scop.
2018 size_t getNumParams() const { return Parameters.size(); }
2019
2020 /// Return whether given SCEV is used as the parameter in this Scop.
2021 bool isParam(const SCEV *Param) const { return Parameters.count(Param); }
2022
2023 /// Take a list of parameters and add the new ones to the scop.
2024 void addParams(const ParameterSetTy &NewParameters);
2025
2026 /// Return an iterator range containing the scop parameters.
2027 iterator_range<ParameterSetTy::iterator> parameters() const {
2028 return make_range(Parameters.begin(), Parameters.end());
2029 }
2030
2031 /// Return an iterator range containing invariant accesses.
2032 iterator_range<InvariantEquivClassesTy::iterator> invariantEquivClasses() {
2033 return make_range(InvariantEquivClasses.begin(),
2034 InvariantEquivClasses.end());
2035 }
2036
2037 /// Return an iterator range containing all the MemoryAccess objects of the
2038 /// Scop.
2039 iterator_range<AccFuncVector::iterator> access_functions() {
2040 return make_range(AccessFunctions.begin(), AccessFunctions.end());
2041 }
2042
2043 /// Return whether this scop is empty, i.e. contains no statements that
2044 /// could be executed.
2045 bool isEmpty() const { return Stmts.empty(); }
2046
2047 StringRef getName() {
2048 if (!name)
2049 name = R.getNameStr();
2050 return *name;
2051 }
2052
2053 using array_iterator = ArrayInfoSetTy::iterator;
2054 using const_array_iterator = ArrayInfoSetTy::const_iterator;
2055 using array_range = iterator_range<ArrayInfoSetTy::iterator>;
2056 using const_array_range = iterator_range<ArrayInfoSetTy::const_iterator>;
2057
2058 inline array_iterator array_begin() { return ScopArrayInfoSet.begin(); }
2059
2060 inline array_iterator array_end() { return ScopArrayInfoSet.end(); }
2061
2063 return ScopArrayInfoSet.begin();
2064 }
2065
2067 return ScopArrayInfoSet.end();
2068 }
2069
2071 return array_range(array_begin(), array_end());
2072 }
2073
2074 inline const_array_range arrays() const {
2076 }
2077
2078 /// Return the isl_id that represents a certain parameter.
2079 ///
2080 /// @param Parameter A SCEV that was recognized as a Parameter.
2081 ///
2082 /// @return The corresponding isl_id or NULL otherwise.
2083 isl::id getIdForParam(const SCEV *Parameter) const;
2084
2085 /// Get the maximum region of this static control part.
2086 ///
2087 /// @return The maximum region of this static control part.
2088 inline const Region &getRegion() const { return R; }
2089 inline Region &getRegion() { return R; }
2090
2091 /// Return the function this SCoP is in.
2092 Function &getFunction() const { return *R.getEntry()->getParent(); }
2093
2094 /// Check if @p L is contained in the SCoP.
2095 bool contains(const Loop *L) const { return R.contains(L); }
2096
2097 /// Check if @p BB is contained in the SCoP.
2098 bool contains(const BasicBlock *BB) const { return R.contains(BB); }
2099
2100 /// Check if @p I is contained in the SCoP.
2101 bool contains(const Instruction *I) const { return R.contains(I); }
2102
2103 /// Return the unique exit block of the SCoP.
2104 BasicBlock *getExit() const { return R.getExit(); }
2105
2106 /// Return the unique exiting block of the SCoP if any.
2107 BasicBlock *getExitingBlock() const { return R.getExitingBlock(); }
2108
2109 /// Return the unique entry block of the SCoP.
2110 BasicBlock *getEntry() const { return R.getEntry(); }
2111
2112 /// Return the unique entering block of the SCoP if any.
2113 BasicBlock *getEnteringBlock() const { return R.getEnteringBlock(); }
2114
2115 /// Return true if @p BB is the exit block of the SCoP.
2116 bool isExit(BasicBlock *BB) const { return getExit() == BB; }
2117
2118 /// Return a range of all basic blocks in the SCoP.
2119 Region::block_range blocks() const { return R.blocks(); }
2120
2121 /// Return true if and only if @p BB dominates the SCoP.
2122 bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const;
2123
2124 /// Get the maximum depth of the loop.
2125 ///
2126 /// @return The maximum depth of the loop.
2127 inline unsigned getMaxLoopDepth() const { return MaxLoopDepth; }
2128
2129 /// Return the invariant equivalence class for @p Val if any.
2131
2132 /// Return the set of invariant accesses.
2134 return InvariantEquivClasses;
2135 }
2136
2137 /// Check if the scop has any invariant access.
2139
2140 /// Mark the SCoP as optimized by the scheduler.
2141 void markAsOptimized() { IsOptimized = true; }
2142
2143 /// Check if the SCoP has been optimized by the scheduler.
2144 bool isOptimized() const { return IsOptimized; }
2145
2146 /// Return the ID of the Scop
2147 int getID() const { return ID; }
2148
2149 /// Get the name of the entry and exit blocks of this Scop.
2150 ///
2151 /// These along with the function name can uniquely identify a Scop.
2152 ///
2153 /// @return std::pair whose first element is the entry name & second element
2154 /// is the exit name.
2155 std::pair<std::string, std::string> getEntryExitStr() const;
2156
2157 /// Get the name of this Scop.
2158 std::string getNameStr() const;
2159
2160 /// Get the constraint on parameter of this Scop.
2161 ///
2162 /// @return The constraint on parameter of this Scop.
2163 isl::set getContext() const;
2164
2165 /// Return the context where execution behavior is defined. Might return
2166 /// nullptr.
2168
2169 /// Return the define behavior context, or if not available, its approximation
2170 /// from all other contexts.
2174
2176 }
2177
2178 /// Return space of isl context parameters.
2179 ///
2180 /// Returns the set of context parameters that are currently constrained. In
2181 /// case the full set of parameters is needed, see @getFullParamSpace.
2182 isl::space getParamSpace() const;
2183
2184 /// Return the full space of parameters.
2185 ///
2186 /// getParamSpace will only return the parameters of the context that are
2187 /// actually constrained, whereas getFullParamSpace will return all
2188 // parameters. This is useful in cases, where we need to ensure all
2189 // parameters are available, as certain isl functions will abort if this is
2190 // not the case.
2192
2193 /// Get the assumed context for this Scop.
2194 ///
2195 /// @return The assumed context of this Scop.
2197
2198 /// Return true if the optimized SCoP can be executed.
2199 ///
2200 /// In addition to the runtime check context this will also utilize the domain
2201 /// constraints to decide it the optimized version can actually be executed.
2202 ///
2203 /// @returns True if the optimized SCoP can be executed.
2204 bool hasFeasibleRuntimeContext() const;
2205
2206 /// Check if the assumption in @p Set is trivial or not.
2207 ///
2208 /// @param Set The relations between parameters that are assumed to hold.
2209 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2210 /// (needed/assumptions) or negative (invalid/restrictions).
2211 ///
2212 /// @returns True if the assumption @p Set is not trivial.
2214
2215 /// Track and report an assumption.
2216 ///
2217 /// Use 'clang -Rpass-analysis=polly-scops' or 'opt
2218 /// -pass-remarks-analysis=polly-scops' to output the assumptions.
2219 ///
2220 /// @param Kind The assumption kind describing the underlying cause.
2221 /// @param Set The relations between parameters that are assumed to hold.
2222 /// @param Loc The location in the source that caused this assumption.
2223 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2224 /// (needed/assumptions) or negative (invalid/restrictions).
2225 /// @param BB The block in which this assumption was taken. Used to
2226 /// calculate hotness when emitting remark.
2227 ///
2228 /// @returns True if the assumption is not trivial.
2229 bool trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc,
2230 AssumptionSign Sign, BasicBlock *BB);
2231
2232 /// Add the conditions from @p Set (or subtract them if @p Sign is
2233 /// AS_RESTRICTION) to the defined behaviour context.
2235
2236 /// Add assumptions to assumed context.
2237 ///
2238 /// The assumptions added will be assumed to hold during the execution of the
2239 /// scop. However, as they are generally not statically provable, at code
2240 /// generation time run-time checks will be generated that ensure the
2241 /// assumptions hold.
2242 ///
2243 /// WARNING: We currently exploit in simplifyAssumedContext the knowledge
2244 /// that assumptions do not change the set of statement instances
2245 /// executed.
2246 ///
2247 /// @param Kind The assumption kind describing the underlying cause.
2248 /// @param Set The relations between parameters that are assumed to hold.
2249 /// @param Loc The location in the source that caused this assumption.
2250 /// @param Sign Enum to indicate if the assumptions in @p Set are positive
2251 /// (needed/assumptions) or negative (invalid/restrictions).
2252 /// @param BB The block in which this assumption was taken. Used to
2253 /// calculate hotness when emitting remark.
2254 /// @param RTC Does the assumption require a runtime check?
2255 void addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc,
2256 AssumptionSign Sign, BasicBlock *BB, bool RTC = true);
2257
2258 /// Mark the scop as invalid.
2259 ///
2260 /// This method adds an assumption to the scop that is always invalid. As a
2261 /// result, the scop will not be optimized later on. This function is commonly
2262 /// called when a condition makes it impossible (or too compile time
2263 /// expensive) to process this scop any further.
2264 ///
2265 /// @param Kind The assumption kind describing the underlying cause.
2266 /// @param Loc The location in the source that triggered .
2267 /// @param BB The BasicBlock where it was triggered.
2268 void invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB = nullptr);
2269
2270 /// Get the invalid context for this Scop.
2271 ///
2272 /// @return The invalid context of this Scop.
2274
2275 /// Return true if and only if the InvalidContext is trivial (=empty).
2277
2278 /// Return all alias groups for this SCoP.
2280 return MinMaxAliasGroups;
2281 }
2282
2283 void addAliasGroup(MinMaxVectorTy &MinMaxAccessesReadWrite,
2284 MinMaxVectorTy &MinMaxAccessesReadOnly) {
2285 MinMaxAliasGroups.emplace_back();
2286 MinMaxAliasGroups.back().first = MinMaxAccessesReadWrite;
2287 MinMaxAliasGroups.back().second = MinMaxAccessesReadOnly;
2288 }
2289
2290 /// Remove statements from the list of scop statements.
2291 ///
2292 /// @param ShouldDelete A function that returns true if the statement passed
2293 /// to it should be deleted.
2294 /// @param AfterHoisting If true, also remove from data access lists.
2295 /// These lists are filled during
2296 /// ScopBuilder::buildAccessRelations. Therefore, if this
2297 /// method is called before buildAccessRelations, false
2298 /// must be passed.
2299 void removeStmts(function_ref<bool(ScopStmt &)> ShouldDelete,
2300 bool AfterHoisting = true);
2301
2302 /// Get an isl string representing the context.
2303 std::string getContextStr() const;
2304
2305 /// Get an isl string representing the assumed context.
2306 std::string getAssumedContextStr() const;
2307
2308 /// Get an isl string representing the invalid context.
2309 std::string getInvalidContextStr() const;
2310
2311 /// Return the list of ScopStmts that represent the given @p BB.
2312 ArrayRef<ScopStmt *> getStmtListFor(BasicBlock *BB) const;
2313
2314 /// Get the statement to put a PHI WRITE into.
2315 ///
2316 /// @param U The operand of a PHINode.
2317 ScopStmt *getIncomingStmtFor(const Use &U) const;
2318
2319 /// Return the last statement representing @p BB.
2320 ///
2321 /// Of the sequence of statements that represent a @p BB, this is the last one
2322 /// to be executed. It is typically used to determine which instruction to add
2323 /// a MemoryKind::PHI WRITE to. For this purpose, it is not strictly required
2324 /// to be executed last, only that the incoming value is available in it.
2325 ScopStmt *getLastStmtFor(BasicBlock *BB) const;
2326
2327 /// Return the ScopStmts that represents the Region @p R, or nullptr if
2328 /// it is not represented by any statement in this Scop.
2329 ArrayRef<ScopStmt *> getStmtListFor(Region *R) const;
2330
2331 /// Return the ScopStmts that represents @p RN; can return nullptr if
2332 /// the RegionNode is not within the SCoP or has been removed due to
2333 /// simplifications.
2334 ArrayRef<ScopStmt *> getStmtListFor(RegionNode *RN) const;
2335
2336 /// Return the ScopStmt an instruction belongs to, or nullptr if it
2337 /// does not belong to any statement in this Scop.
2338 ScopStmt *getStmtFor(Instruction *Inst) const {
2339 return InstStmtMap.lookup(Inst);
2340 }
2341
2342 /// Return the number of statements in the SCoP.
2343 size_t getSize() const { return Stmts.size(); }
2344
2345 /// @name Statements Iterators
2346 ///
2347 /// These iterators iterate over all statements of this Scop.
2348 //@{
2349 using iterator = StmtSet::iterator;
2350 using const_iterator = StmtSet::const_iterator;
2351
2352 iterator begin() { return Stmts.begin(); }
2353 iterator end() { return Stmts.end(); }
2354 const_iterator begin() const { return Stmts.begin(); }
2355 const_iterator end() const { return Stmts.end(); }
2356
2357 using reverse_iterator = StmtSet::reverse_iterator;
2358 using const_reverse_iterator = StmtSet::const_reverse_iterator;
2359
2360 reverse_iterator rbegin() { return Stmts.rbegin(); }
2361 reverse_iterator rend() { return Stmts.rend(); }
2362 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
2363 const_reverse_iterator rend() const { return Stmts.rend(); }
2364 //@}
2365
2366 /// Return the set of required invariant loads.
2368 return DC.RequiredILS;
2369 }
2370
2371 /// Add @p LI to the set of required invariant loads.
2372 void addRequiredInvariantLoad(LoadInst *LI) { DC.RequiredILS.insert(LI); }
2373
2374 /// Return the set of boxed (thus overapproximated) loops.
2376
2377 /// Return true if and only if @p R is a non-affine subregion.
2378 bool isNonAffineSubRegion(const Region *R) {
2379 return DC.NonAffineSubRegionSet.count(R);
2380 }
2381
2383
2384 /// Return the (possibly new) ScopArrayInfo object for @p Access.
2385 ///
2386 /// @param ElementType The type of the elements stored in this array.
2387 /// @param Kind The kind of the array info object.
2388 /// @param BaseName The optional name of this memory reference.
2389 ScopArrayInfo *getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType,
2390 ArrayRef<const SCEV *> Sizes,
2392 const char *BaseName = nullptr);
2393
2394 /// Create an array and return the corresponding ScopArrayInfo object.
2395 ///
2396 /// @param ElementType The type of the elements stored in this array.
2397 /// @param BaseName The name of this memory reference.
2398 /// @param Sizes The sizes of dimensions.
2399 ScopArrayInfo *createScopArrayInfo(Type *ElementType,
2400 const std::string &BaseName,
2401 const std::vector<unsigned> &Sizes);
2402
2403 /// Return the cached ScopArrayInfo object for @p BasePtr.
2404 ///
2405 /// @param BasePtr The base pointer the object has been stored for.
2406 /// @param Kind The kind of array info object.
2407 ///
2408 /// @returns The ScopArrayInfo pointer or NULL if no such pointer is
2409 /// available.
2411
2412 /// Return the cached ScopArrayInfo object for @p BasePtr.
2413 ///
2414 /// @param BasePtr The base pointer the object has been stored for.
2415 /// @param Kind The kind of array info object.
2416 ///
2417 /// @returns The ScopArrayInfo pointer (may assert if no such pointer is
2418 /// available).
2420
2421 /// Invalidate ScopArrayInfo object for base address.
2422 ///
2423 /// @param BasePtr The base pointer of the ScopArrayInfo object to invalidate.
2424 /// @param Kind The Kind of the ScopArrayInfo object.
2426 auto It = ScopArrayInfoMap.find(std::make_pair(BasePtr, Kind));
2427 if (It == ScopArrayInfoMap.end())
2428 return;
2429 ScopArrayInfoSet.remove(It->second.get());
2430 ScopArrayInfoMap.erase(It);
2431 }
2432
2433 /// Set new isl context.
2434 void setContext(isl::set NewContext);
2435
2436 /// Update maximal loop depth. If @p Depth is smaller than current value,
2437 /// then maximal loop depth is not updated.
2438 void updateMaxLoopDepth(unsigned Depth) {
2439 MaxLoopDepth = std::max(MaxLoopDepth, Depth);
2440 }
2441
2442 /// Align the parameters in the statement to the scop context
2443 void realignParams();
2444
2445 /// Return true if this SCoP can be profitably optimized.
2446 ///
2447 /// @param ScalarsAreUnprofitable Never consider statements with scalar writes
2448 /// as profitably optimizable.
2449 ///
2450 /// @return Whether this SCoP can be profitably optimized.
2451 bool isProfitable(bool ScalarsAreUnprofitable) const;
2452
2453 /// Return true if the SCoP contained at least one error block.
2454 bool hasErrorBlock() const { return HasErrorBlock; }
2455
2456 /// Notify SCoP that it contains an error block
2458
2459 /// Return true if the underlying region has a single exiting block.
2460 bool hasSingleExitEdge() const { return HasSingleExitEdge; }
2461
2462 /// Print the static control part.
2463 ///
2464 /// @param OS The output stream the static control part is printed to.
2465 /// @param PrintInstructions Whether to print the statement's instructions as
2466 /// well.
2467 void print(raw_ostream &OS, bool PrintInstructions) const;
2468
2469#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2470 /// Print the ScopStmt to stderr.
2471 void dump() const;
2472#endif
2473
2474 /// Get the isl context of this static control part.
2475 ///
2476 /// @return The isl context of this static control part.
2477 isl::ctx getIslCtx() const;
2478
2479 /// Directly return the shared_ptr of the context.
2480 const std::shared_ptr<isl_ctx> &getSharedIslCtx() const { return IslCtx; }
2481
2482 /// Compute the isl representation for the SCEV @p E
2483 ///
2484 /// @param E The SCEV that should be translated.
2485 /// @param BB An (optional) basic block in which the isl_pw_aff is computed.
2486 /// SCEVs known to not reference any loops in the SCoP can be
2487 /// passed without a @p BB.
2488 /// @param NonNegative Flag to indicate the @p E has to be non-negative.
2489 ///
2490 /// Note that this function will always return a valid isl_pw_aff. However, if
2491 /// the translation of @p E was deemed to complex the SCoP is invalidated and
2492 /// a dummy value of appropriate dimension is returned. This allows to bail
2493 /// for complex cases without "error handling code" needed on the users side.
2494 PWACtx getPwAff(const SCEV *E, BasicBlock *BB = nullptr,
2495 bool NonNegative = false,
2496 RecordedAssumptionsTy *RecordedAssumptions = nullptr);
2497
2498 /// Compute the isl representation for the SCEV @p E
2499 ///
2500 /// This function is like @see Scop::getPwAff() but strips away the invalid
2501 /// domain part associated with the piecewise affine function.
2503 getPwAffOnly(const SCEV *E, BasicBlock *BB = nullptr,
2504 RecordedAssumptionsTy *RecordedAssumptions = nullptr);
2505
2506 /// Check if an <nsw> AddRec for the loop L is cached.
2508
2509 /// Return the domain of @p Stmt.
2510 ///
2511 /// @param Stmt The statement for which the conditions should be returned.
2512 isl::set getDomainConditions(const ScopStmt *Stmt) const;
2513
2514 /// Return the domain of @p BB.
2515 ///
2516 /// @param BB The block for which the conditions should be returned.
2517 isl::set getDomainConditions(BasicBlock *BB) const;
2518
2519 /// Return the domain of @p BB. If it does not exist, create an empty one.
2520 isl::set &getOrInitEmptyDomain(BasicBlock *BB) { return DomainMap[BB]; }
2521
2522 /// Check if domain is determined for @p BB.
2523 bool isDomainDefined(BasicBlock *BB) const { return DomainMap.count(BB) > 0; }
2524
2525 /// Set domain for @p BB.
2526 void setDomain(BasicBlock *BB, isl::set &Domain) { DomainMap[BB] = Domain; }
2527
2528 /// Get a union set containing the iteration domains of all statements.
2529 isl::union_set getDomains() const;
2530
2531 /// Get a union map of all may-writes performed in the SCoP.
2533
2534 /// Get a union map of all must-writes performed in the SCoP.
2536
2537 /// Get a union map of all writes performed in the SCoP.
2539
2540 /// Get a union map of all reads performed in the SCoP.
2542
2543 /// Get a union map of all memory accesses performed in the SCoP.
2545
2546 /// Get a union map of all memory accesses performed in the SCoP.
2547 ///
2548 /// @param Array The array to which the accesses should belong.
2550
2551 /// Get the schedule of all the statements in the SCoP.
2552 ///
2553 /// @return The schedule of all the statements in the SCoP, if the schedule of
2554 /// the Scop does not contain extension nodes, and nullptr, otherwise.
2556
2557 /// Get a schedule tree describing the schedule of all statements.
2559
2560 /// Update the current schedule
2561 ///
2562 /// NewSchedule The new schedule (given as a flat union-map).
2563 void setSchedule(isl::union_map NewSchedule);
2564
2565 /// Update the current schedule
2566 ///
2567 /// NewSchedule The new schedule (given as schedule tree).
2568 void setScheduleTree(isl::schedule NewSchedule);
2569
2570 /// Whether the schedule is the original schedule as derived from the CFG by
2571 /// ScopBuilder.
2572 bool isOriginalSchedule() const { return !ScheduleModified; }
2573
2574 /// Intersects the domains of all statements in the SCoP.
2575 ///
2576 /// @return true if a change was made
2578
2579 /// Get the depth of a loop relative to the outermost loop in the Scop.
2580 ///
2581 /// This will return
2582 /// 0 if @p L is an outermost loop in the SCoP
2583 /// >0 for other loops in the SCoP
2584 /// -1 if @p L is nullptr or there is no outermost loop in the SCoP
2585 int getRelativeLoopDepth(const Loop *L) const;
2586
2587 /// Find the ScopArrayInfo associated with an isl Id
2588 /// that has name @p Name.
2589 ScopArrayInfo *getArrayInfoByName(const std::string BaseName);
2590
2591 /// Simplify the SCoP representation.
2592 ///
2593 /// @param AfterHoisting Whether it is called after invariant load hoisting.
2594 /// When true, also removes statements without
2595 /// side-effects.
2596 void simplifySCoP(bool AfterHoisting);
2597
2598 /// Get the next free array index.
2599 ///
2600 /// This function returns a unique index which can be used to identify an
2601 /// array.
2602 long getNextArrayIdx() { return ArrayIdx++; }
2603
2604 /// Get the next free statement index.
2605 ///
2606 /// This function returns a unique index which can be used to identify a
2607 /// statement.
2608 long getNextStmtIdx() { return StmtIdx++; }
2609
2610 /// Get the representing SCEV for @p S if applicable, otherwise @p S.
2611 ///
2612 /// Invariant loads of the same location are put in an equivalence class and
2613 /// only one of them is chosen as a representing element that will be
2614 /// modeled as a parameter. The others have to be normalized, i.e.,
2615 /// replaced by the representing element of their equivalence class, in order
2616 /// to get the correct parameter value, e.g., in the SCEVAffinator.
2617 ///
2618 /// @param S The SCEV to normalize.
2619 ///
2620 /// @return The representing SCEV for invariant loads or @p S if none.
2621 const SCEV *getRepresentingInvariantLoadSCEV(const SCEV *S) const;
2622
2623 /// Return the MemoryAccess that writes an llvm::Value, represented by a
2624 /// ScopArrayInfo.
2625 ///
2626 /// There can be at most one such MemoryAccess per llvm::Value in the SCoP.
2627 /// Zero is possible for read-only values.
2628 MemoryAccess *getValueDef(const ScopArrayInfo *SAI) const;
2629
2630 /// Return all MemoryAccesses that us an llvm::Value, represented by a
2631 /// ScopArrayInfo.
2632 ArrayRef<MemoryAccess *> getValueUses(const ScopArrayInfo *SAI) const;
2633
2634 /// Return the MemoryAccess that represents an llvm::PHINode.
2635 ///
2636 /// ExitPHIs's PHINode is not within the SCoPs. This function returns nullptr
2637 /// for them.
2638 MemoryAccess *getPHIRead(const ScopArrayInfo *SAI) const;
2639
2640 /// Return all MemoryAccesses for all incoming statements of a PHINode,
2641 /// represented by a ScopArrayInfo.
2642 ArrayRef<MemoryAccess *> getPHIIncomings(const ScopArrayInfo *SAI) const;
2643
2644 /// Return whether @p Inst has a use outside of this SCoP.
2645 bool isEscaping(Instruction *Inst);
2646
2650
2657 };
2658
2659 /// Collect statistic about this SCoP.
2660 ///
2661 /// These are most commonly used for LLVM's static counters (Statistic.h) in
2662 /// various places. If statistics are disabled, only zeros are returned to
2663 /// avoid the overhead.
2665
2666 /// Is this Scop marked as not to be transformed by an optimization heuristic?
2667 /// In this case, only user-directed transformations are allowed.
2669
2670 /// Mark this Scop to not apply an optimization heuristic.
2672};
2673
2674/// Print Scop scop to raw_ostream OS.
2675raw_ostream &operator<<(raw_ostream &OS, const Scop &scop);
2676
2677/// The legacy pass manager's analysis pass to compute scop information
2678/// for a region.
2679class ScopInfoRegionPass final : public RegionPass {
2680 /// The Scop pointer which is used to construct a Scop.
2681 std::unique_ptr<Scop> S;
2682
2683public:
2684 static char ID; // Pass identification, replacement for typeid
2685
2686 ScopInfoRegionPass() : RegionPass(ID) {}
2687 ~ScopInfoRegionPass() override = default;
2688
2689 /// Build Scop object, the Polly IR of static control
2690 /// part for the current SESE-Region.
2691 ///
2692 /// @return If the current region is a valid for a static control part,
2693 /// return the Polly IR representing this static control part,
2694 /// return null otherwise.
2695 Scop *getScop() { return S.get(); }
2696 const Scop *getScop() const { return S.get(); }
2697
2698 /// Calculate the polyhedral scop information for a given Region.
2699 bool runOnRegion(Region *R, RGPassManager &RGM) override;
2700
2701 void releaseMemory() override { S.reset(); }
2702
2703 void print(raw_ostream &O, const Module *M = nullptr) const override;
2704
2705 void getAnalysisUsage(AnalysisUsage &AU) const override;
2706};
2707
2708llvm::Pass *createScopInfoPrinterLegacyRegionPass(raw_ostream &OS);
2709
2711public:
2712 using RegionToScopMapTy = MapVector<Region *, std::unique_ptr<Scop>>;
2713 using reverse_iterator = RegionToScopMapTy::reverse_iterator;
2714 using const_reverse_iterator = RegionToScopMapTy::const_reverse_iterator;
2715 using iterator = RegionToScopMapTy::iterator;
2716 using const_iterator = RegionToScopMapTy::const_iterator;
2717
2718private:
2719 /// A map of Region to its Scop object containing
2720 /// Polly IR of static control part.
2722 const DataLayout &DL;
2724 ScalarEvolution &SE;
2725 LoopInfo &LI;
2726 AAResults &AA;
2727 DominatorTree &DT;
2728 AssumptionCache &AC;
2729 OptimizationRemarkEmitter &ORE;
2730
2731public:
2732 ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE,
2733 LoopInfo &LI, AAResults &AA, DominatorTree &DT, AssumptionCache &AC,
2734 OptimizationRemarkEmitter &ORE);
2735
2736 /// Get the Scop object for the given Region.
2737 ///
2738 /// @return If the given region is the maximal region within a scop, return
2739 /// the scop object. If the given region is a subregion, return a
2740 /// nullptr. Top level region containing the entry block of a function
2741 /// is not considered in the scop creation.
2742 Scop *getScop(Region *R) const {
2743 auto MapIt = RegionToScopMap.find(R);
2744 if (MapIt != RegionToScopMap.end())
2745 return MapIt->second.get();
2746 return nullptr;
2747 }
2748
2749 /// Recompute the Scop-Information for a function.
2750 ///
2751 /// This invalidates any iterators.
2752 void recompute();
2753
2754 /// Handle invalidation explicitly
2755 bool invalidate(Function &F, const PreservedAnalyses &PA,
2756 FunctionAnalysisManager::Invalidator &Inv);
2757
2758 iterator begin() { return RegionToScopMap.begin(); }
2759 iterator end() { return RegionToScopMap.end(); }
2760 const_iterator begin() const { return RegionToScopMap.begin(); }
2761 const_iterator end() const { return RegionToScopMap.end(); }
2764 const_reverse_iterator rbegin() const { return RegionToScopMap.rbegin(); }
2766 bool empty() const { return RegionToScopMap.empty(); }
2767};
2768
2769struct ScopInfoAnalysis : AnalysisInfoMixin<ScopInfoAnalysis> {
2770 static AnalysisKey Key;
2771
2773
2774 Result run(Function &, FunctionAnalysisManager &);
2775};
2776
2777struct ScopInfoPrinterPass final : PassInfoMixin<ScopInfoPrinterPass> {
2778 ScopInfoPrinterPass(raw_ostream &OS) : Stream(OS) {}
2779
2780 PreservedAnalyses run(Function &, FunctionAnalysisManager &);
2781
2782 raw_ostream &Stream;
2783};
2784
2785//===----------------------------------------------------------------------===//
2786/// The legacy pass manager's analysis pass to compute scop information
2787/// for the whole function.
2788///
2789/// This pass will maintain a map of the maximal region within a scop to its
2790/// scop object for all the feasible scops present in a function.
2791/// This pass is an alternative to the ScopInfoRegionPass in order to avoid a
2792/// region pass manager.
2793class ScopInfoWrapperPass final : public FunctionPass {
2794 std::unique_ptr<ScopInfo> Result;
2795
2796public:
2797 ScopInfoWrapperPass() : FunctionPass(ID) {}
2798 ~ScopInfoWrapperPass() override = default;
2799
2800 static char ID; // Pass identification, replacement for typeid
2801
2802 ScopInfo *getSI() { return Result.get(); }
2803 const ScopInfo *getSI() const { return Result.get(); }
2804
2805 /// Calculate all the polyhedral scops for a given function.
2806 bool runOnFunction(Function &F) override;
2807
2808 void releaseMemory() override { Result.reset(); }
2809
2810 void print(raw_ostream &O, const Module *M = nullptr) const override;
2811
2812 void getAnalysisUsage(AnalysisUsage &AU) const override;
2813};
2814
2815llvm::Pass *createScopInfoPrinterLegacyFunctionPass(llvm::raw_ostream &OS);
2816} // end namespace polly
2817
2818namespace llvm {
2819void initializeScopInfoRegionPassPass(PassRegistry &);
2821void initializeScopInfoWrapperPassPass(PassRegistry &);
2823} // end namespace llvm
2824
2825#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:560
std::string getAccessRelationStr() const
Get an isl string representing the latest access relation.
Definition: ScopInfo.cpp:613
void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue)
Add a new incoming block/value pairs for this PHI/ExitPHI access.
Definition: ScopInfo.h:736
isl::map getNewAccessRelation() const
Get the new access function imported or set by a pass.
Definition: ScopInfo.cpp:605
void dump() const
Print the MemoryAccess to stderr.
Definition: ScopInfo.cpp:955
isl::set assumeNoOutOfBound()
Definition: ScopInfo.cpp:647
isl::id getArrayId() const
Old name of getOriginalArrayId().
Definition: ScopInfo.h:843
SmallVector< const SCEV *, 4 > Sizes
Size of each dimension of the accessed array.
Definition: ScopInfo.h:548
bool isOriginalArrayKind() const
Whether this is an access of an explicit load or store in the IR.
Definition: ScopInfo.h:944
void foldAccessRelation()
Fold the memory access to consider parametric offsets.
Definition: ScopInfo.cpp:750
AssertingVH< Value > AccessValue
The value associated with this memory access.
Definition: ScopInfo.h:584
bool isOriginalValueKind() const
Was this MemoryAccess detected as a scalar dependences?
Definition: ScopInfo.h:976
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:995
isl::space getOriginalAccessRelationSpace() const
Return the space in which the access relation lives in.
Definition: ScopInfo.cpp:601
bool isAnyPHIKind() const
Old name of isOriginalAnyPHIKind().
Definition: ScopInfo.h:1028
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:1053
ReductionType
Reduction access type.
Definition: ScopInfo.h:466
@ RT_BOTTOM
Pseudo type for the data flow analysis.
Definition: ScopInfo.h:474
@ 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:617
SubscriptsTy Subscripts
Subscript expression for each dimension.
Definition: ScopInfo.h:590
isl::map getLatestAccessRelation() const
Return the newest access relation of this access.
Definition: ScopInfo.h:789
isl::id getOriginalArrayId() const
Get the detection-time base array isl::id for this access.
Definition: ScopInfo.cpp:567
MemoryAccess(const MemoryAccess &)=delete
isl::pw_aff getPwAff(const SCEV *E)
Compute the isl representation for the SCEV E wrt.
Definition: ScopInfo.cpp:958
Instruction * AccessInstruction
The access instruction of this memory access.
Definition: ScopInfo.h:569
void computeBoundsOnAccessRelation(unsigned ElementSize)
Compute bounds on an over approximated access relation.
Definition: ScopInfo.cpp:705
ReductionType RedType
Reduction type for reduction like accesses, RT_NONE otherwise.
Definition: ScopInfo.h:518
bool isValueKind() const
Old name of isOriginalValueKind().
Definition: ScopInfo.h:986
bool hasNewAccessRelation() const
Check if a new access relation was imported or set by a pass.
Definition: ScopInfo.h:777
isl::id Id
A unique identifier for this memory access.
Definition: ScopInfo.h:484
bool isOriginalExitPHIKind() const
Was this MemoryAccess detected as the accesses of a PHI node in the SCoP's exit block?
Definition: ScopInfo.h:1002
bool isLatestArrayKind() const
Whether storage memory is either an custom .s2a/.phiops alloca (false) or an existing pointer into an...
Definition: ScopInfo.h:950
bool isPHIKind() const
Old name of isOriginalPHIKind.
Definition: ScopInfo.h:998
bool isWrite() const
Is this a write memory access?
Definition: ScopInfo.h:769
bool IsAffine
Are all the subscripts affine expression?
Definition: ScopInfo.h:587
ReductionType getReductionType() const
Get the reduction type of this access.
Definition: ScopInfo.h:1034
MemoryKind getLatestKind() const
Return the kind considering a potential setNewAccessRelation.
Definition: ScopInfo.h:939
const ScopArrayInfo * getOriginalScopArrayInfo() const
Get the detection-time ScopArrayInfo object for the base address.
Definition: ScopInfo.cpp:553
AssertingVH< Value > BaseAddr
The base address (e.g., A for A[i+j]).
Definition: ScopInfo.h:542
bool isOriginalAnyPHIKind() const
Was this access detected as one of the two PHI types?
Definition: ScopInfo.h:1017
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:885
iterator_range< SubscriptsTy::const_iterator > subscripts() const
Return an iterator range containing the subscripts.
Definition: ScopInfo.h:888
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:1032
bool isLatestPartialAccess() const
Return whether the MemoryyAccess is a partial access.
Definition: ScopInfo.cpp:1095
std::string getOriginalAccessRelationStr() const
Get an isl string representing the access function read from IR.
Definition: ScopInfo.cpp:597
bool isExitPHIKind() const
Old name of isOriginalExitPHIKind().
Definition: ScopInfo.h:1014
Value * tryGetValueStored()
Return llvm::Value that is stored by this access, if available.
Definition: ScopInfo.h:874
isl::set InvalidDomain
The domain under which this access is not modeled precisely.
Definition: ScopInfo.h:528
enum AccessType getType()
Get the type of a memory access.
Definition: ScopInfo.h:754
bool isLatestScalarKind() const
Whether this access is an array to a scalar memory object, also considering changes by setNewAccessRe...
Definition: ScopInfo.h:968
bool isRead() const
Is this a read memory access?
Definition: ScopInfo.h:760
void buildAccessRelation(const ScopArrayInfo *SAI)
Assemble the access relation from all available information.
Definition: ScopInfo.cpp:817
isl::id getId() const
Get identifier for the memory access.
Definition: ScopInfo.cpp:917
isl::map NewAccessRelation
Updated access relation read from JSCOP file.
Definition: ScopInfo.h:621
SmallVector< const SCEV *, 4 > SubscriptsTy
Definition: ScopInfo.h:477
unsigned getNumSubscripts() const
Return the number of access function subscript.
Definition: ScopInfo.h:893
isl::map getAddressFunction() const
Get an isl map describing the memory address accessed.
Definition: ScopInfo.cpp:577
void setAccessRelation(isl::map AccessRelation)
Update the original access relation.
Definition: ScopInfo.cpp:1040
bool isMustWrite() const
Is this a must-write memory access?
Definition: ScopInfo.h:763
bool isScalarKind() const
Old name of isOriginalScalarKind.
Definition: ScopInfo.h:973
isl::map AccessRelation
Relation from statement instances to the accessed array elements.
Definition: ScopInfo.h:618
void realignParams()
Align the parameters in the access relation to the scop context.
Definition: ScopInfo.cpp:901
Type * getElementType() const
Return the element type of the accessed array wrt. this access.
Definition: ScopInfo.h:864
const SCEV * getSubscript(unsigned Dim) const
Return the access function subscript in the dimension Dim.
Definition: ScopInfo.h:896
bool isReductionLike() const
Is this a reduction like access?
Definition: ScopInfo.h:757
bool isOriginalPHIKind() const
Was this MemoryAccess detected as a special PHI node access?
Definition: ScopInfo.h:989
isl::set getInvalidContext() const
Get the invalid context for this access.
Definition: ScopInfo.h:907
void print(raw_ostream &OS) const
Print the MemoryAccess.
Definition: ScopInfo.cpp:933
const ScopArrayInfo * getScopArrayInfo() const
Legacy name of getOriginalScopArrayInfo().
Definition: ScopInfo.h:853
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:962
ScopStmt * Statement
Parent ScopStmt of this access.
Definition: ScopInfo.h:521
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:1017
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:1036
Type * ElementType
Type a single array element wrt. this access.
Definition: ScopInfo.h:545
enum AccessType AccType
Whether it a reading or writing access, and if writing, whether it is conditional (MAY_WRITE).
Definition: ScopInfo.h:492
std::string getNewAccessRelationStr() const
Get an isl string representing a new access function, if available.
Definition: ScopInfo.cpp:609
void buildMemIntrinsicAccessRelation()
Create the access relation for the underlying memory intrinsic.
Definition: ScopInfo.cpp:681
isl::set getInvalidDomain() const
Get the invalid domain for this access.
Definition: ScopInfo.h:904
ArrayRef< std::pair< BasicBlock *, Value * > > getIncoming() const
Return the list of possible PHI/ExitPHI values.
Definition: ScopInfo.h:748
Value * getOriginalBaseAddr() const
Get the original base address of this access (e.g.
Definition: ScopInfo.h:833
ScopStmt * getStatement() const
Get the statement that contains this memory access.
Definition: ScopInfo.h:1031
bool isLatestExitPHIKind() const
Is this MemoryAccess modeling the accesses of a PHI node in the SCoP's exit block?...
Definition: ScopInfo.h:1009
bool isAffine() const
Is the memory access affine?
Definition: ScopInfo.h:1085
isl::set getStride(isl::map Schedule) const
Get the stride of this memory access in the specified Schedule.
Definition: ScopInfo.cpp:1000
bool isMayWrite() const
Is this a may-write memory access?
Definition: ScopInfo.h:766
isl::id getLatestArrayId() const
Get the base array isl::id for this access, modifiable through setNewAccessRelation().
Definition: ScopInfo.cpp:571
MemoryKind Kind
What is modeled by this MemoryAccess.
Definition: ScopInfo.h:488
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:1023
bool isLatestValueKind() const
Is this MemoryAccess currently modeling scalar dependences?
Definition: ScopInfo.h:981
isl::pw_multi_aff applyScheduleToAccessRelation(isl::union_map Schedule) const
Return the access relation after the schedule was applied.
Definition: ScopInfo.cpp:582
SmallVector< std::pair< BasicBlock *, Value * >, 4 > Incoming
Incoming block and value of a PHINode.
Definition: ScopInfo.h:572
isl::map getAccessRelation() const
Old name of getLatestAccessRelation().
Definition: ScopInfo.h:795
Value * getAccessValue() const
Return the access value of this memory access.
Definition: ScopInfo.h:867
isl::map getOriginalAccessRelation() const
Get the original access function as read from IR.
Definition: ScopInfo.cpp:593
void setNewAccessRelation(isl::map NewAccessRelation)
Set the updated access relation read from JSCOP file.
Definition: ScopInfo.cpp:1044
bool isArrayKind() const
Old name of isOriginalArrayKind.
Definition: ScopInfo.h:955
bool isMemoryIntrinsic() const
Is this a memory intrinsic access (memcpy, memset, memmove)?
Definition: ScopInfo.h:772
MemoryKind getOriginalKind() const
Return the kind when this access was first detected.
Definition: ScopInfo.h:932
const std::string getReductionOperatorStr() const
Return a string representation of the access's reduction type.
Definition: ScopInfo.cpp:913
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:2679
std::unique_ptr< Scop > S
The Scop pointer which is used to construct a Scop.
Definition: ScopInfo.h:2681
void print(raw_ostream &O, const Module *M=nullptr) const override
Definition: ScopInfo.cpp:2626
Scop * getScop()
Build Scop object, the Polly IR of static control part for the current SESE-Region.
Definition: ScopInfo.h:2695
~ScopInfoRegionPass() override=default
void getAnalysisUsage(AnalysisUsage &AU) const override
Definition: ScopInfo.cpp:2550
bool runOnRegion(Region *R, RGPassManager &RGM) override
Calculate the polyhedral scop information for a given Region.
Definition: ScopInfo.cpp:2597
const Scop * getScop() const
Definition: ScopInfo.h:2696
void releaseMemory() override
Definition: ScopInfo.h:2701
The legacy pass manager's analysis pass to compute scop information for the whole function.
Definition: ScopInfo.h:2793
void getAnalysisUsage(AnalysisUsage &AU) const override
Definition: ScopInfo.cpp:2777
void print(raw_ostream &O, const Module *M=nullptr) const override
Definition: ScopInfo.cpp:2803
bool runOnFunction(Function &F) override
Calculate all the polyhedral scops for a given function.
Definition: ScopInfo.cpp:2789
const ScopInfo * getSI() const
Definition: ScopInfo.h:2803
~ScopInfoWrapperPass() override=default
std::unique_ptr< ScopInfo > Result
Definition: ScopInfo.h:2794
void releaseMemory() override
Definition: ScopInfo.h:2808
iterator end()
Definition: ScopInfo.h:2759
RegionToScopMapTy::const_reverse_iterator const_reverse_iterator
Definition: ScopInfo.h:2714
const_iterator end() const
Definition: ScopInfo.h:2761
RegionToScopMapTy::const_iterator const_iterator
Definition: ScopInfo.h:2716
AAResults & AA
Definition: ScopInfo.h:2726
RegionToScopMapTy::iterator iterator
Definition: ScopInfo.h:2715
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation explicitly.
Definition: ScopInfo.cpp:2734
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:2712
AssumptionCache & AC
Definition: ScopInfo.h:2728
DominatorTree & DT
Definition: ScopInfo.h:2727
LoopInfo & LI
Definition: ScopInfo.h:2725
ScopDetection & SD
Definition: ScopInfo.h:2723
reverse_iterator rend()
Definition: ScopInfo.h:2763
const DataLayout & DL
Definition: ScopInfo.h:2722
const_reverse_iterator rend() const
Definition: ScopInfo.h:2765
Scop * getScop(Region *R) const
Get the Scop object for the given Region.
Definition: ScopInfo.h:2742
const_iterator begin() const
Definition: ScopInfo.h:2760
ScalarEvolution & SE
Definition: ScopInfo.h:2724
const_reverse_iterator rbegin() const
Definition: ScopInfo.h:2764
reverse_iterator rbegin()
Definition: ScopInfo.h:2762
bool empty() const
Definition: ScopInfo.h:2766
void recompute()
Recompute the Scop-Information for a function.
Definition: ScopInfo.cpp:2710
OptimizationRemarkEmitter & ORE
Definition: ScopInfo.h:2729
iterator begin()
Definition: ScopInfo.h:2758
RegionToScopMapTy::reverse_iterator reverse_iterator
Definition: ScopInfo.h:2713
RegionToScopMapTy RegionToScopMap
A map of Region to its Scop object containing Polly IR of static control part.
Definition: ScopInfo.h:2721
Statement of the Scop.
Definition: ScopInfo.h:1140
iterator end()
Definition: ScopInfo.h:1521
void addAccess(MemoryAccess *Access, bool Preprend=false)
Add Access to this statement's list of accesses.
Definition: ScopInfo.cpp:1127
llvm::SmallVector< MemoryAccess *, 8 > MemoryAccessVec
Definition: ScopInfo.h:1144
MemoryAccess & getArrayAccessFor(const Instruction *Inst) const
Return the only array access for Inst.
Definition: ScopInfo.h:1434
Scop * getParent()
Definition: ScopInfo.h:1528
BasicBlock * getEntryBlock() const
Return a BasicBlock from this statement.
Definition: ScopInfo.cpp:1221
void dump() const
Print the ScopStmt to stderr.
Definition: ScopInfo.cpp:1276
bool isEmpty() const
Return true if this statement does not contain any accesses.
Definition: ScopInfo.h:1388
std::vector< Instruction * > Instructions
Vector for Instructions in this statement.
Definition: ScopInfo.h:1266
void print(raw_ostream &OS, bool PrintInstructions) const
Print the ScopStmt.
Definition: ScopInfo.cpp:1252
Region * R
The region represented by this statement (in the non-affine case).
Definition: ScopInfo.h:1251
DenseMap< PHINode *, MemoryAccess * > PHIWrites
Map from PHI nodes to its incoming value when coming from this statement.
Definition: ScopInfo.h:1232
const Scop * getParent() const
Definition: ScopInfo.h:1529
std::vector< Instruction * >::const_iterator insts_end() const
Definition: ScopInfo.h:1545
isl::set Domain
The iteration domain describes the set of iterations for which this statement is executed.
Definition: ScopInfo.h:1207
const std::vector< Instruction * > & getInstructions() const
Definition: ScopInfo.h:1531
bool isBlockStmt() const
Return true if this statement represents a single basic block.
Definition: ScopInfo.h:1321
void removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting=true)
Remove MA from this statement.
Definition: ScopInfo.cpp:1322
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:1340
void setInstructions(ArrayRef< Instruction * > Range)
Set the list of instructions for this statement.
Definition: ScopInfo.h:1537
Loop * SurroundingLoop
The closest loop that contains this statement.
Definition: ScopInfo.h:1263
MemoryAccess * lookupInputAccessOf(Value *Val) const
Return the input access of the value, or null if no such MemoryAccess exists.
Definition: ScopInfo.h:1477
MemoryAccessVec::const_iterator const_iterator
Definition: ScopInfo.h:1518
std::string getScheduleStr() const
Get an isl string representing this schedule.
Definition: ScopInfo.cpp:1215
const_iterator end() const
Definition: ScopInfo.h:1523
void prependInstruction(Instruction *Inst)
Insert an instruction before all other instructions in this statement.
Definition: ScopInfo.h:1555
const ScopStmt & operator=(const ScopStmt &)=delete
std::string getDomainStr() const
Get an isl string representing this domain.
Definition: ScopInfo.cpp:1213
const MemoryAccessList * lookupArrayAccessesFor(const Instruction *Inst) const
Find all array accesses for Inst.
Definition: ScopInfo.h:1397
std::vector< Instruction * >::const_iterator insts_begin() const
Definition: ScopInfo.h:1541
size_t size() const
Definition: ScopInfo.h:1524
isl::set getInvalidContext() const
Get the invalid context for this statement.
Definition: ScopInfo.h:1309
void realignParams()
Align the parameters in the statement to the scop context.
Definition: ScopInfo.cpp:1162
void removeAccessData(MemoryAccess *MA)
Remove MA from dictionaries pointing to them.
Definition: ScopInfo.cpp:1279
ScopStmt(const ScopStmt &)=delete
isl::map getSchedule() const
Get the schedule function of this ScopStmt.
Definition: ScopInfo.cpp:1104
isl::set getInvalidDomain() const
Get the invalid domain for this statement.
Definition: ScopInfo.h:1306
SmallVector< Loop *, 4 > NestLoops
Definition: ScopInfo.h:1258
MemoryAccessVec::iterator iterator
Definition: ScopInfo.h:1517
DenseMap< const Instruction *, MemoryAccessList > InstructionToAccess
Mapping from instructions to (scalar) memory accesses.
Definition: ScopInfo.h:1215
Scop & Parent
Polyhedral description.
Definition: ScopInfo.h:1179
void restrictDomain(isl::set NewDomain)
Restrict the domain of the statement.
Definition: ScopInfo.cpp:1121
std::string BaseName
Definition: ScopInfo.h:1260
isl::ctx getIslCtx() const
Get an isl_ctx pointer.
Definition: ScopInfo.cpp:1235
Region * getRegion() const
Get the region represented by this ScopStmt (if any).
Definition: ScopInfo.h:1330
bool represents(BasicBlock *BB) const
Return whether this statement represents BB.
Definition: ScopInfo.h:1351
DenseMap< Instruction *, MemoryAccess * > ValueWrites
The set of values defined in this ScopStmt that are required elsewhere, mapped to their MemoryKind::V...
Definition: ScopInfo.h:1223
iterator_range< std::vector< Instruction * >::const_iterator > insts() const
The range of instructions in this statement.
Definition: ScopInfo.h:1550
MemoryAccess * lookupPHIReadOf(PHINode *PHI) const
Return the MemoryAccess that loads a PHINode value, or nullptr if not existing, respectively not yet ...
Definition: ScopInfo.h:1458
BasicBlock * getBasicBlock() const
Get the BasicBlock represented by this ScopStmt (if any).
Definition: ScopInfo.h:1318
void removeMemoryAccess(MemoryAccess *MA)
Remove a MemoryAccess from this statement.
Definition: ScopInfo.cpp:1302
MemoryAccessVec MemAccs
The memory accesses of this statement.
Definition: ScopInfo.h:1212
const char * getBaseName() const
Definition: ScopInfo.cpp:1229
bool contains(const Loop *L) const
Return whether L is boxed within this statement.
Definition: ScopInfo.h:1342
isl::ast_build Build
}
Definition: ScopInfo.h:1256
bool isCopyStmt() const
Return true if this is a copy statement.
Definition: ScopInfo.h:1324
DenseMap< Value *, MemoryAccess * > ValueReads
The set of values defined elsewhere required in this ScopStmt and their MemoryKind::Value READ Memory...
Definition: ScopInfo.h:1219
isl::ast_build getAstBuild() const
Get the isl AST build.
Definition: ScopInfo.h:1565
isl::set InvalidDomain
The domain under which this statement is not modeled precisely.
Definition: ScopInfo.h:1186
DenseMap< PHINode *, MemoryAccess * > PHIReads
Map from PHI nodes to its read access in this statement.
Definition: ScopInfo.h:1235
MemoryAccess * getArrayAccessOrNULLFor(const Instruction *Inst) const
Return the only array access for Inst, if existing.
Definition: ScopInfo.h:1411
isl::id getDomainId() const
Get the id of the iteration domain space.
Definition: ScopInfo.cpp:1241
bool isRegionStmt() const
Return true if this statement represents a whole region.
Definition: ScopInfo.h:1333
void setInvalidDomain(isl::set ID)
Set the invalid context for this statement to ID.
Definition: ScopInfo.cpp:1219
unsigned getNumIterators() const
Definition: ScopInfo.cpp:1227
Loop * getLoopForDimension(unsigned Dimension) const
Get the loop for a dimension.
Definition: ScopInfo.cpp:1231
isl::set getDomain() const
Get the iteration domain of this ScopStmt.
Definition: ScopInfo.cpp:1237
const_iterator begin() const
Definition: ScopInfo.h:1522
void setAstBuild(isl::ast_build B)
Set the isl AST build.
Definition: ScopInfo.h:1562
MemoryAccess * lookupValueWriteOf(Instruction *Inst) const
Return the MemoryAccess that writes the value of an instruction defined in this statement,...
Definition: ScopInfo.h:1444
Loop * getSurroundingLoop() const
Return the closest innermost loop that contains this statement, but is not contained in it.
Definition: ScopInfo.h:1381
BasicBlock * BB
A SCoP statement represents either a basic block (affine/precise case) or a whole region (non-affine ...
Definition: ScopInfo.h:1248
isl::space getDomainSpace() const
Get the space of the iteration domain.
Definition: ScopInfo.cpp:1239
MemoryAccess * lookupPHIWriteOf(PHINode *PHI) const
Return the PHI write MemoryAccess for the incoming values from any basic block in this ScopStmt,...
Definition: ScopInfo.h:1465
void printInstructions(raw_ostream &OS) const
Print the instructions in ScopStmt.
Definition: ScopInfo.cpp:1243
MemoryAccess * lookupValueReadOf(Value *Inst) const
Return the MemoryAccess that reloads a value, or nullptr if not existing, respectively not yet added.
Definition: ScopInfo.h:1452
bool contains(Instruction *Inst) const
Return whether this statement contains Inst.
Definition: ScopInfo.h:1360
iterator begin()
Definition: ScopInfo.h:1520
Static Control Part.
Definition: ScopInfo.h:1630
InvariantEquivClassTy * lookupInvariantEquivClass(Value *Val)
Return the invariant equivalence class for Val if any.
Definition: ScopInfo.cpp:1721
isl::schedule getScheduleTree() const
Get a schedule tree describing the schedule of all statements.
Definition: ScopInfo.cpp:2265
isl::set InvalidContext
The restrictions under which this SCoP was built.
Definition: ScopInfo.h:1760
bool IsOptimized
Flag to indicate that the scheduler actually optimized the SCoP.
Definition: ScopInfo.h:1675
bool HasErrorBlock
Flag to remember if the SCoP contained an error block or not.
Definition: ScopInfo.h:1681
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:2033
isl::space getFullParamSpace() const
Return the full space of parameters.
Definition: ScopInfo.cpp:1836
ArrayRef< MemoryAccess * > getValueUses(const ScopArrayInfo *SAI) const
Return all MemoryAccesses that us an llvm::Value, represented by a ScopArrayInfo.
Definition: ScopInfo.cpp:2454
bool isParam(const SCEV *Param) const
Return whether given SCEV is used as the parameter in this Scop.
Definition: ScopInfo.h:2021
DenseMap< const ScopArrayInfo *, SmallVector< MemoryAccess *, 4 > > ValueUseAccs
List of all uses (i.e.
Definition: ScopInfo.h:1862
isl::union_map getMayWrites()
Get a union map of all may-writes performed in the SCoP.
Definition: ScopInfo.cpp:2239
void printContext(raw_ostream &OS) const
Definition: ScopInfo.cpp:2060
const MinMaxVectorPairVectorTy & getAliasGroups() const
Return all alias groups for this SCoP.
Definition: ScopInfo.h:2279
isl::set getInvalidContext() const
Get the invalid context for this Scop.
Definition: ScopInfo.cpp:2058
void invalidateScopArrayInfo(Value *BasePtr, MemoryKind Kind)
Invalidate ScopArrayInfo object for base address.
Definition: ScopInfo.h:2425
void dump() const
Print the ScopStmt to stderr.
Definition: ScopInfo.cpp:2170
isl::union_map getSchedule() const
Get the schedule of all the statements in the SCoP.
Definition: ScopInfo.cpp:2260
void invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB=nullptr)
Mark the scop as invalid.
Definition: ScopInfo.cpp:2053
MemoryAccess * getValueDef(const ScopArrayInfo *SAI) const
Return the MemoryAccess that writes an llvm::Value, represented by a ScopArrayInfo.
Definition: ScopInfo.cpp:2444
ScalarEvolution * getSE() const
Return the scalar evolution.
Definition: ScopInfo.cpp:2302
ScalarEvolution * SE
Definition: ScopInfo.h:1660
DominatorTree * getDT() const
Return the dominator tree.
Definition: ScopInfo.h:2010
unsigned getMaxLoopDepth() const
Get the maximum depth of the loop.
Definition: ScopInfo.h:2127
void printAliasAssumptions(raw_ostream &OS) const
Definition: ScopInfo.cpp:2081
ArrayRef< MemoryAccess * > getPHIIncomings(const ScopArrayInfo *SAI) const
Return all MemoryAccesses for all incoming statements of a PHINode, represented by a ScopArrayInfo.
Definition: ScopInfo.cpp:2472
bool hasInvariantAccesses()
Check if the scop has any invariant access.
Definition: ScopInfo.h:2138
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:2338
void setDomain(BasicBlock *BB, isl::set &Domain)
Set domain for BB.
Definition: ScopInfo.h:2526
ScopArrayInfo * getScopArrayInfo(Value *BasePtr, MemoryKind Kind)
Return the cached ScopArrayInfo object for BasePtr.
Definition: ScopInfo.cpp:1790
ParameterSetTy Parameters
Parameters of this Scop.
Definition: ScopInfo.h:1695
bool hasDisableHeuristicsHint() const
Is this Scop marked as not to be transformed by an optimization heuristic? In this case,...
Definition: ScopInfo.h:2668
bool hasTrivialInvalidContext() const
Return true if and only if the InvalidContext is trivial (=empty).
Definition: ScopInfo.h:2276
ArrayInfoSetTy ScopArrayInfoSet
A set to remember ScopArrayInfo objects.
Definition: ScopInfo.h:1743
ValueToValueMap InvEquivClassVMap
Mapping from invariant loads to the representing invariant load of their equivalence class.
Definition: ScopInfo.h:1838
iterator_range< AccFuncVector::iterator > access_functions()
Return an iterator range containing all the MemoryAccess objects of the Scop.
Definition: ScopInfo.h:2039
isl::union_map getReads()
Get a union map of all reads performed in the SCoP.
Definition: ScopInfo.cpp:2247
unsigned getCopyStmtsNum()
Get the count of copy statements added to this Scop.
Definition: ScopInfo.h:1957
bool isDomainDefined(BasicBlock *BB) const
Check if domain is determined for BB.
Definition: ScopInfo.h:2523
unsigned CopyStmtsNum
Number of copy statements.
Definition: ScopInfo.h:1687
bool HasDisableHeuristicsHint
Is this Scop marked as not to be transformed by an optimization heuristic?
Definition: ScopInfo.h:1815
std::map< std::pair< AssertingVH< const Value >, MemoryKind >, std::unique_ptr< ScopArrayInfo > > ArrayInfoMapTy
Definition: ScopInfo.h:1724
DenseMap< BasicBlock *, std::vector< ScopStmt * > > StmtMap
A map from basic blocks to vector of SCoP statements.
Definition: ScopInfo.h:1708
const MapInsnToMemAcc & getInsnToMemAccMap() const
Definition: ScopInfo.h:2382
void addParams(const ParameterSetTy &NewParameters)
Take a list of parameters and add the new ones to the scop.
Definition: ScopInfo.cpp:1477
iterator end()
Definition: ScopInfo.h:2353
isl::set getAssumedContext() const
Get the assumed context for this Scop.
Definition: ScopInfo.cpp:1849
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:2304
SCEVAffinator Affinator
The affinator used to translate SCEVs to isl expressions.
Definition: ScopInfo.h:1720
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:1744
DominatorTree * DT
Definition: ScopInfo.h:1661
const InvariantLoadsSetTy & getRequiredInvariantLoads() const
Return the set of required invariant loads.
Definition: ScopInfo.h:2367
void addAccessFunction(MemoryAccess *Access)
Add the access function to all MemoryAccess objects of the Scop created in this pass.
Definition: ScopInfo.h:1972
isl::schedule Schedule
The schedule of the SCoP.
Definition: ScopInfo.h:1812
iterator begin()
Definition: ScopInfo.h:2352
isl::set getBestKnownDefinedBehaviorContext() const
Return the define behavior context, or if not available, its approximation from all other contexts.
Definition: ScopInfo.h:2171
bool contains(const Instruction *I) const
Check if I is contained in the SCoP.
Definition: ScopInfo.h:2101
SmallVector< MinMaxVectorPairTy, 4 > MinMaxVectorPairVectorTy
Vector of pair of minimal/maximal access vectors representing non read only and read only accesses fo...
Definition: ScopInfo.h:1644
isl::set Context
Constraints on parameters.
Definition: ScopInfo.h:1717
unsigned MaxLoopDepth
Max loop depth.
Definition: ScopInfo.h:1684
isl::union_set getDomains() const
Get a union set containing the iteration domains of all statements.
Definition: ScopInfo.cpp:2200
const BoxedLoopsSetTy & getBoxedLoops() const
Return the set of boxed (thus overapproximated) loops.
Definition: ScopInfo.h:2375
std::shared_ptr< isl_ctx > IslCtx
Isl context.
Definition: ScopInfo.h:1658
void addAliasGroup(MinMaxVectorTy &MinMaxAccessesReadWrite, MinMaxVectorTy &MinMaxAccessesReadOnly)
Definition: ScopInfo.h:2283
int getID() const
Return the ID of the Scop.
Definition: ScopInfo.h:2147
void markAsOptimized()
Mark the SCoP as optimized by the scheduler.
Definition: ScopInfo.h:2141
bool isOriginalSchedule() const
Whether the schedule is the original schedule as derived from the CFG by ScopBuilder.
Definition: ScopInfo.h:2572
ArrayInfoSetTy::iterator array_iterator
Definition: ScopInfo.h:2053
std::string getAssumedContextStr() const
Get an isl string representing the assumed context.
Definition: ScopInfo.cpp:1800
bool isProfitable(bool ScalarsAreUnprofitable) const
Return true if this SCoP can be profitably optimized.
Definition: ScopInfo.cpp:1854
array_iterator array_begin()
Definition: ScopInfo.h:2058
bool isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const
Return true if and only if BB dominates the SCoP.
Definition: ScopInfo.cpp:1494
ScopArrayInfo * getArrayInfoByName(const std::string BaseName)
Find the ScopArrayInfo associated with an isl Id that has name Name.
Definition: ScopInfo.cpp:2411
array_range arrays()
Definition: ScopInfo.h:2070
void addAccessData(MemoryAccess *Access)
Add metadata for Access.
Definition: ScopInfo.cpp:2419
isl::set getDomainConditions(const ScopStmt *Stmt) const
Return the domain of Stmt.
Definition: ScopInfo.cpp:1592
void addInvariantEquivClass(const InvariantEquivClassTy &InvariantEquivClass)
Add new invariant access equivalence class.
Definition: ScopInfo.h:1993
AccFuncVector AccessFunctions
Definition: ScopInfo.h:1672
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:2175
DenseMap< Value *, MemoryAccess * > ValueDefAccs
Map of values to the MemoryAccess that writes its definition.
Definition: ScopInfo.h:1855
isl::union_map getMustWrites()
Get a union map of all must-writes performed in the SCoP.
Definition: ScopInfo.cpp:2235
reverse_iterator rbegin()
Definition: ScopInfo.h:2360
std::pair< std::string, std::string > getEntryExitStr() const
Get the name of the entry and exit blocks of this Scop.
Definition: ScopInfo.cpp:1815
isl::pw_aff getPwAffOnly(const SCEV *E, BasicBlock *BB=nullptr, RecordedAssumptionsTy *RecordedAssumptions=nullptr)
Compute the isl representation for the SCEV E.
Definition: ScopInfo.cpp:2210
ScopStatistics getStatistics() const
Collect statistic about this SCoP.
Definition: ScopInfo.cpp:2503
std::string getContextStr() const
Get an isl string representing the context.
Definition: ScopInfo.cpp:1796
std::pair< MinMaxVectorTy, MinMaxVectorTy > MinMaxVectorPairTy
Pair of minimal/maximal access vectors representing read write and read only accesses.
Definition: ScopInfo.h:1640
DenseMap< BasicBlock *, isl::set > DomainMap
A map from basic blocks to their domains.
Definition: ScopInfo.h:1714
isl::union_map getAccessesOfType(std::function< bool(MemoryAccess &)> Predicate)
Collect all memory access relations of a given type.
Definition: ScopInfo.cpp:2217
void removeStmts(function_ref< bool(ScopStmt &)> ShouldDelete, bool AfterHoisting=true)
Remove statements from the list of scop statements.
Definition: ScopInfo.cpp:1665
void buildContext()
Build the Context of the Scop.
Definition: ScopInfo.cpp:1498
void addInvariantLoadMapping(const Value *LoadInst, Value *ClassRep)
Add mapping from invariant loads to the representing invariant load of their equivalence class.
Definition: ScopInfo.h:1999
int getRelativeLoopDepth(const Loop *L) const
Get the depth of a loop relative to the outermost loop in the Scop.
Definition: ScopInfo.cpp:2397
isl::ctx getIslCtx() const
Get the isl context of this static control part.
Definition: ScopInfo.cpp:2173
LoopInfo * getLI() const
Return the LoopInfo used for this Scop.
Definition: ScopInfo.h:2013
std::string getInvalidContextStr() const
Get an isl string representing the invalid context.
Definition: ScopInfo.cpp:1805
StringRef getName()
Definition: ScopInfo.h:2047
iterator_range< ArrayInfoSetTy::iterator > array_range
Definition: ScopInfo.h:2055
bool HasSingleExitEdge
True if the underlying region has a single exiting block.
Definition: ScopInfo.h:1678
DenseMap< Instruction *, ScopStmt * > InstStmtMap
A map from instructions to SCoP statements.
Definition: ScopInfo.h:1711
bool isEscaping(Instruction *Inst)
Return whether Inst has a use outside of this SCoP.
Definition: ScopInfo.cpp:2480
void removeStmtNotInDomainMap()
Removes all statements where the entry block of the statement does not have a corresponding domain in...
Definition: ScopInfo.cpp:1685
bool hasNSWAddRecForLoop(Loop *L)
Check if an <nsw> AddRec for the loop L is cached.
Definition: ScopInfo.h:2507
void updateMaxLoopDepth(unsigned Depth)
Update maximal loop depth.
Definition: ScopInfo.h:2438
ScopDetection::DetectionContext & DC
The context of the SCoP created during SCoP detection.
Definition: ScopInfo.h:1701
void print(raw_ostream &OS, bool PrintInstructions) const
Print the static control part.
Definition: ScopInfo.cpp:2147
const_iterator end() const
Definition: ScopInfo.h:2355
void printStatements(raw_ostream &OS, bool PrintInstructions) const
Definition: ScopInfo.cpp:2120
isl::union_map getWrites()
Get a union map of all writes performed in the SCoP.
Definition: ScopInfo.cpp:2243
reverse_iterator rend()
Definition: ScopInfo.h:2361
void setSchedule(isl::union_map NewSchedule)
Update the current schedule.
Definition: ScopInfo.cpp:2269
bool hasErrorBlock() const
Return true if the SCoP contained at least one error block.
Definition: ScopInfo.h:2454
void setContext(isl::set NewContext)
Set new isl context.
Definition: ScopInfo.cpp:1364
bool hasFeasibleRuntimeContext() const
Return true if the optimized SCoP can be executed.
Definition: ScopInfo.cpp:1882
DenseMap< const SCEV *, isl::id > ParameterIds
Mapping from parameters to their ids.
Definition: ScopInfo.h:1698
isl::space getParamSpace() const
Return space of isl context parameters.
Definition: ScopInfo.cpp:1834
bool isExit(BasicBlock *BB) const
Return true if BB is the exit block of the SCoP.
Definition: ScopInfo.h:2116
void addRequiredInvariantLoad(LoadInst *LI)
Add LI to the set of required invariant loads.
Definition: ScopInfo.h:2372
const std::shared_ptr< isl_ctx > & getSharedIslCtx() const
Directly return the shared_ptr of the context.
Definition: ScopInfo.h:2480
SmallVector< MinMaxAccessTy, 4 > MinMaxVectorTy
Vector of minimal/maximal accesses to different arrays.
Definition: ScopInfo.h:1636
isl::set getDefinedBehaviorContext() const
Return the context where execution behavior is defined.
Definition: ScopInfo.h:2167
Region::block_range blocks() const
Return a range of all basic blocks in the SCoP.
Definition: ScopInfo.h:2119
const_iterator begin() const
Definition: ScopInfo.h:2354
isl::set & getOrInitEmptyDomain(BasicBlock *BB)
Return the domain of BB. If it does not exist, create an empty one.
Definition: ScopInfo.h:2520
StmtSet::const_reverse_iterator const_reverse_iterator
Definition: ScopInfo.h:2358
std::string getNameStr() const
Get the name of this Scop.
Definition: ScopInfo.cpp:1809
DenseMap< PHINode *, MemoryAccess * > PHIReadAccs
Map of values to the MemoryAccess that reads a PHI.
Definition: ScopInfo.h:1858
static void incrementNumberOfAliasingAssumptions(unsigned Step)
Increment actual number of aliasing assumptions taken.
Definition: ScopInfo.cpp:2499
long StmtIdx
The smallest statement index not yet assigned.
Definition: ScopInfo.h:1847
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:1633
iterator_range< ArrayInfoSetTy::const_iterator > const_array_range
Definition: ScopInfo.h:2056
std::optional< std::string > name
The name of the SCoP (identical to the regions name)
Definition: ScopInfo.h:1667
size_t getSize() const
Return the number of statements in the SCoP.
Definition: ScopInfo.h:2343
void createParameterId(const SCEV *Param)
Create an id for Param and store it in the ParameterIds map.
Definition: ScopInfo.cpp:1444
const_reverse_iterator rbegin() const
Definition: ScopInfo.h:2362
BasicBlock * getEnteringBlock() const
Return the unique entering block of the SCoP if any.
Definition: ScopInfo.h:2113
ArrayNameMapTy ScopArrayNameMap
A map to remember ScopArrayInfo objects for all names of memory references.
Definition: ScopInfo.h:1739
isl::set DefinedBehaviorContext
The context under which the SCoP must have defined behavior.
Definition: ScopInfo.h:1776
iterator_range< ParameterSetTy::iterator > parameters() const
Return an iterator range containing the scop parameters.
Definition: ScopInfo.h:2027
bool isEmpty() const
Return whether this scop is empty, i.e.
Definition: ScopInfo.h:2045
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:1867
void markDisableHeuristics()
Mark this Scop to not apply an optimization heuristic.
Definition: ScopInfo.h:2671
isl::id getIdForParam(const SCEV *Parameter) const
Return the isl_id that represents a certain parameter.
Definition: ScopInfo.cpp:1488
iterator_range< InvariantEquivClassesTy::iterator > invariantEquivClasses()
Return an iterator range containing invariant accesses.
Definition: ScopInfo.h:2032
bool isOptimized() const
Check if the SCoP has been optimized by the scheduler.
Definition: ScopInfo.h:2144
InvariantEquivClassesTy InvariantEquivClasses
List of invariant accesses.
Definition: ScopInfo.h:1841
StmtSet::iterator iterator
Definition: ScopInfo.h:2349
BasicBlock * getExitingBlock() const
Return the unique exiting block of the SCoP if any.
Definition: ScopInfo.h:2107
Region & R
The underlying Region.
Definition: ScopInfo.h:1664
OptimizationRemarkEmitter & ORE
OptimizationRemarkEmitter object for displaying diagnostic remarks.
Definition: ScopInfo.h:1704
ArrayInfoSetTy::const_iterator const_array_iterator
Definition: ScopInfo.h:2054
bool ScheduleModified
Whether the schedule has been modified after derived from the CFG by ScopBuilder.
Definition: ScopInfo.h:1819
size_t getNumParams() const
Get the count of parameters used in this Scop.
Definition: ScopInfo.h:2018
void addParameterBounds()
Add the bounds of the parameters to the context.
Definition: ScopInfo.cpp:1506
bool restrictDomains(isl::union_set Domain)
Intersects the domains of all statements in the SCoP.
Definition: ScopInfo.cpp:2281
const_array_iterator array_begin() const
Definition: ScopInfo.h:2062
isl::union_map getAccesses()
Get a union map of all memory accesses performed in the SCoP.
Definition: ScopInfo.cpp:2251
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:1768
BasicBlock * getExit() const
Return the unique exit block of the SCoP.
Definition: ScopInfo.h:2104
long getNextStmtIdx()
Get the next free statement index.
Definition: ScopInfo.h:2608
void notifyErrorBlock()
Notify SCoP that it contains an error block.
Definition: ScopInfo.h:2457
StmtSet Stmts
The statements in this Scop.
Definition: ScopInfo.h:1692
SetVector< ScopArrayInfo * > ArrayInfoSetTy
Definition: ScopInfo.h:1728
StmtSet::reverse_iterator reverse_iterator
Definition: ScopInfo.h:2357
void removeAccessData(MemoryAccess *Access)
Remove the metadata stored for Access.
Definition: ScopInfo.cpp:2429
ArrayRef< ScopStmt * > getStmtListFor(BasicBlock *BB) const
Return the list of ScopStmts that represent the given BB.
Definition: ScopInfo.cpp:2356
MemoryAccess * getPHIRead(const ScopArrayInfo *SAI) const
Return the MemoryAccess that represents an llvm::PHINode.
Definition: ScopInfo.cpp:2462
bool contains(const Loop *L) const
Check if L is contained in the SCoP.
Definition: ScopInfo.h:2095
void realignParams()
Align the parameters in the statement to the scop context.
Definition: ScopInfo.cpp:1515
array_iterator array_end()
Definition: ScopInfo.h:2060
Function & getFunction() const
Return the function this SCoP is in.
Definition: ScopInfo.h:2092
ArrayInfoMapTy ScopArrayInfoMap
A map to remember ScopArrayInfo objects for all base pointers.
Definition: ScopInfo.h:1735
StmtSet::const_iterator const_iterator
Definition: ScopInfo.h:2350
void printArrayInfo(raw_ostream &OS) const
Definition: ScopInfo.cpp:2131
bool isNonAffineSubRegion(const Region *R)
Return true if and only if R is a non-affine subregion.
Definition: ScopInfo.h:2378
StringMap< std::unique_ptr< ScopArrayInfo > > ArrayNameMapTy
Definition: ScopInfo.h:1726
const SCEV * getRepresentingInvariantLoadSCEV(const SCEV *S) const
Get the representing SCEV for S if applicable, otherwise S.
Definition: ScopInfo.cpp:1433
long getNextArrayIdx()
Get the next free array index.
Definition: ScopInfo.h:2602
void simplifyContexts()
Simplify the assumed and invalid context.
Definition: ScopInfo.cpp:1557
bool hasSingleExitEdge() const
Return true if the underlying region has a single exiting block.
Definition: ScopInfo.h:2460
const_array_iterator array_end() const
Definition: ScopInfo.h:2066
ScopArrayInfo * getScopArrayInfoOrNull(Value *BasePtr, MemoryKind Kind)
Return the cached ScopArrayInfo object for BasePtr.
Definition: ScopInfo.cpp:1785
const_reverse_iterator rend() const
Definition: ScopInfo.h:2363
MemoryAccess * lookupBasePtrAccess(MemoryAccess *MA)
Return the access for the base ptr of MA if any.
Definition: ScopInfo.cpp:1894
InvariantEquivClassesTy & getInvariantAccesses()
Return the set of invariant accesses.
Definition: ScopInfo.h:2133
isl::set AssumedContext
The assumptions under which this scop was built.
Definition: ScopInfo.h:1752
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:2014
MinMaxVectorPairVectorTy MinMaxAliasGroups
The set of minimal/maximal accesses for each alias group.
Definition: ScopInfo.h:1834
std::list< ScopStmt > StmtSet
Definition: ScopInfo.h:1689
const Region & getRegion() const
Get the maximum region of this static control part.
Definition: ScopInfo.h:2088
Region & getRegion()
Definition: ScopInfo.h:2089
bool isEffectiveAssumption(isl::set Set, AssumptionSign Sign)
Check if the assumption in Set is trivial or not.
Definition: ScopInfo.cpp:1934
void simplifySCoP(bool AfterHoisting)
Simplify the SCoP representation.
Definition: ScopInfo.cpp:1694
ScopStmt * getIncomingStmtFor(const Use &U) const
Get the statement to put a PHI WRITE into.
Definition: ScopInfo.cpp:2363
bool trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB)
Track and report an assumption.
Definition: ScopInfo.cpp:1951
isl::set getContext() const
Get the constraint on parameter of this Scop.
Definition: ScopInfo.cpp:1832
long ArrayIdx
The smallest array index not yet assigned.
Definition: ScopInfo.h:1844
void setScheduleTree(isl::schedule NewSchedule)
Update the current schedule.
Definition: ScopInfo.cpp:2276
BasicBlock * getEntry() const
Return the unique entry block of the SCoP.
Definition: ScopInfo.h:2110
const_array_range arrays() const
Definition: ScopInfo.h:2074
const int ID
A number that uniquely represents a Scop within its function.
Definition: ScopInfo.h:1850
ScopStmt * getLastStmtFor(BasicBlock *BB) const
Return the last statement representing BB.
Definition: ScopInfo.cpp:2380
void removeFromStmtMap(ScopStmt &Stmt)
Removes Stmt from the StmtMap.
Definition: ScopInfo.cpp:1642
bool contains(const BasicBlock *BB) const
Check if BB is contained in the SCoP.
Definition: ScopInfo.h:2098
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:57
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:919
llvm::SmallVector< Assumption, 8 > RecordedAssumptionsTy
Definition: ScopHelper.h:80
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:109
llvm::SetVector< const llvm::Loop * > BoxedLoopsSetTy
Set of loops (used to remember loops in non-affine subregions).
Definition: ScopHelper.h:115
llvm::SetVector< const llvm::SCEV * > ParameterSetTy
Set type for parameters.
Definition: ScopHelper.h:112
std::map< const Instruction *, MemAcc > MapInsnToMemAcc
SmallVector< InvariantEquivClassTy, 8 > InvariantEquivClassesTy
Type for invariant accesses equivalence classes.
Definition: ScopInfo.h:1131
llvm::Pass * createScopInfoPrinterLegacyFunctionPass(llvm::raw_ostream &OS)
AssumptionKind
Enumeration of assumptions Polly can take.
Definition: ScopHelper.h:43
SmallVector< InvariantAccess, 8 > InvariantAccessesTy
Ordered container type to hold invariant accesses.
Definition: ScopInfo.h:1103
std::forward_list< MemoryAccess * > MemoryAccessList
Ordered list type to hold accesses.
Definition: ScopInfo.h:1091
std::vector< std::unique_ptr< MemoryAccess > > AccFuncVector
Definition: ScopInfo.h:212
Helper structure for invariant memory accesses.
Definition: ScopInfo.h:1094
MemoryAccess * MA
The memory access that is (partially) invariant.
Definition: ScopInfo.h:1096
isl::set NonHoistableCtx
The context under which the access is not invariant.
Definition: ScopInfo.h:1099
Type for equivalent invariant accesses and their domain context.
Definition: ScopInfo.h:1106
MemoryAccessList InvariantAccesses
Memory accesses now treated invariant.
Definition: ScopInfo.h:1115
Type * AccessType
The type of the invariant access.
Definition: ScopInfo.h:1127
isl::set ExecutionContext
The execution context under which the memory location is accessed.
Definition: ScopInfo.h:1121
const SCEV * IdentifyingPointer
The pointer that identifies this equivalence class.
Definition: ScopInfo.h:1108
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:2750
static AnalysisKey Key
Definition: ScopInfo.h:2770
PreservedAnalyses run(Function &, FunctionAnalysisManager &)
Definition: ScopInfo.cpp:2763
ScopInfoPrinterPass(raw_ostream &OS)
Definition: ScopInfo.h:2778
static TupleKindPtr Domain("Domain")
static TupleKindPtr Range("Range")