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" 21 #include "polly/Support/SCEVAffinator.h" 22 #include "polly/Support/ScopHelper.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" 33 #include "isl/isl-noexceptions.h" 34 #include <cassert> 35 #include <cstddef> 36 #include <forward_list> 37 #include <optional> 38 39 namespace polly { 40 using llvm::AnalysisInfoMixin; 41 using llvm::ArrayRef; 42 using llvm::AssertingVH; 43 using llvm::AssumptionCache; 44 using llvm::cast; 45 using llvm::DataLayout; 46 using llvm::DenseMap; 47 using llvm::DenseSet; 48 using llvm::function_ref; 49 using llvm::isa; 50 using llvm::iterator_range; 51 using llvm::LoadInst; 52 using llvm::make_range; 53 using llvm::MapVector; 54 using llvm::MemIntrinsic; 55 using llvm::PassInfoMixin; 56 using llvm::PHINode; 57 using llvm::RegionNode; 58 using llvm::RegionPass; 59 using llvm::RGPassManager; 60 using llvm::SetVector; 61 using llvm::SmallPtrSetImpl; 62 using llvm::SmallVector; 63 using llvm::SmallVectorImpl; 64 using llvm::StringMap; 65 using llvm::Type; 66 using llvm::Use; 67 using llvm::Value; 68 using llvm::ValueToValueMap; 69 70 class MemoryAccess; 71 72 //===---------------------------------------------------------------------===// 73 74 extern 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. 79 extern 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: 100 enum 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. 210 using LoopBoundMapType = std::map<const Loop *, const SCEV *>; 211 212 using 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 /// 219 class ScopArrayInfo final { 220 public: 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. 231 ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx IslCtx, 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. 236 ~ScopArrayInfo(); 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. 272 const ScopArrayInfo *getBasePtrOriginSAI() const { return BasePtrOriginSAI; } 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 { 281 if (Kind == MemoryKind::PHI || Kind == MemoryKind::ExitPHI || 282 Kind == MemoryKind::Value) 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. 362 static const ScopArrayInfo *getFromAccessFunction(isl::pw_multi_aff PMA); 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 383 private: 384 void addDerivedSAI(ScopArrayInfo *DerivedSAI) { 385 DerivedSAIs.insert(DerivedSAI); 386 } 387 388 /// For indirect accesses this is the SAI of the BP origin. 389 const ScopArrayInfo *BasePtrOriginSAI; 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. 404 Type *ElementType; 405 406 /// The isl id for the base pointer. 407 isl::id Id; 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. 421 MemoryKind Kind; 422 423 /// The data layout of the module. 424 const DataLayout &DL; 425 426 /// The scop this SAI object belongs to. 427 Scop &S; 428 }; 429 430 /// Represent memory accesses in statements. 431 class MemoryAccess final { 432 friend class Scop; 433 friend class ScopStmt; 434 friend class ScopBuilder; 435 436 public: 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. 457 enum AccessType { 458 READ = 0x1, 459 MUST_WRITE = 0x2, 460 MAY_WRITE = 0x3, 461 }; 462 463 /// Reduction access type 464 /// 465 /// Commutative and associative binary operations suitable for reductions 466 enum ReductionType { 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 479 private: 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. 484 isl::id Id; 485 486 /// What is modeled by this MemoryAccess. 487 /// @see MemoryKind 488 MemoryKind Kind; 489 490 /// Whether it a reading or writing access, and if writing, whether it 491 /// is conditional (MAY_WRITE). 492 enum AccessType AccType; 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. 518 ReductionType RedType = RT_NONE; 519 520 /// Parent ScopStmt of this access. 521 ScopStmt *Statement; 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. 528 isl::set InvalidDomain; 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. 545 Type *ElementType; 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. 590 SubscriptsTy Subscripts; 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 } 618 isl::map AccessRelation; 619 620 /// Updated access relation read from JSCOP file. 621 isl::map NewAccessRelation; 622 // @} 623 624 isl::basic_map createBasicAccessMap(ScopStmt *Statement); 625 626 isl::set assumeNoOutOfBound(); 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. 634 isl::map getOriginalAccessRelation() const; 635 636 /// Return the space in which the access relation lives in. 637 isl::space getOriginalAccessRelationSpace() const; 638 639 /// Get the new access function imported or set by a pass 640 isl::map getNewAccessRelation() const; 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. 673 void buildMemIntrinsicAccessRelation(); 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] 696 void wrapConstantDimensions(); 697 698 public: 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. 725 MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel); 726 727 MemoryAccess(const MemoryAccess &) = delete; 728 MemoryAccess &operator=(const MemoryAccess &) = delete; 729 ~MemoryAccess(); 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()); 738 assert(isAnyPHIKind()); 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 { 749 assert(isAnyPHIKind()); 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. 777 bool hasNewAccessRelation() const { return !NewAccessRelation.is_null(); } 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 /// 789 isl::map getLatestAccessRelation() const { 790 return hasNewAccessRelation() ? getNewAccessRelation() 791 : getOriginalAccessRelation(); 792 } 793 794 /// Old name of getLatestAccessRelation(). 795 isl::map getAccessRelation() const { return 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. 810 isl::map getAddressFunction() const; 811 812 /// Return the access relation after the schedule was applied. 813 isl::pw_multi_aff 814 applyScheduleToAccessRelation(isl::union_map Schedule) const; 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. 836 isl::id getOriginalArrayId() const; 837 838 /// Get the base array isl::id for this access, modifiable through 839 /// setNewAccessRelation(). 840 isl::id getLatestArrayId() const; 841 842 /// Old name of getOriginalArrayId(). 843 isl::id getArrayId() const { return getOriginalArrayId(); } 844 845 /// Get the detection-time ScopArrayInfo object for the base address. 846 const ScopArrayInfo *getOriginalScopArrayInfo() const; 847 848 /// Get the ScopArrayInfo object for the base address, or the one set 849 /// by setNewAccessRelation(). 850 const ScopArrayInfo *getLatestScopArrayInfo() const; 851 852 /// Legacy name of getOriginalScopArrayInfo(). 853 const ScopArrayInfo *getScopArrayInfo() const { 854 return 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. 874 Value *tryGetValueStored() { 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. 904 isl::set getInvalidDomain() const { return InvalidDomain; } 905 906 /// Get the invalid context for this access. 907 isl::set getInvalidContext() const { return getInvalidDomain().params(); } 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. 932 MemoryKind getOriginalKind() const { 933 assert(!getOriginalScopArrayInfo() /* not yet initialized */ || 934 getOriginalScopArrayInfo()->getKind() == Kind); 935 return Kind; 936 } 937 938 /// Return the kind considering a potential setNewAccessRelation. 939 MemoryKind getLatestKind() const { 940 return getLatestScopArrayInfo()->getKind(); 941 } 942 943 /// Whether this is an access of an explicit load or store in the IR. 944 bool isOriginalArrayKind() const { 945 return getOriginalKind() == MemoryKind::Array; 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 { 951 return getLatestKind() == MemoryKind::Array; 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 { 963 return getOriginalKind() != MemoryKind::Array; 964 } 965 966 /// Whether this access is an array to a scalar memory object, also 967 /// considering changes by setNewAccessRelation. 968 bool isLatestScalarKind() const { 969 return getLatestKind() != MemoryKind::Array; 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 { 977 return getOriginalKind() == MemoryKind::Value; 978 } 979 980 /// Is this MemoryAccess currently modeling scalar dependences? 981 bool isLatestValueKind() const { 982 return getLatestKind() == MemoryKind::Value; 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 { 990 return getOriginalKind() == MemoryKind::PHI; 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? 1002 bool isOriginalExitPHIKind() const { 1003 return getOriginalKind() == MemoryKind::ExitPHI; 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 { 1010 return getLatestKind() == MemoryKind::ExitPHI; 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? 1017 bool isOriginalAnyPHIKind() const { 1018 return isOriginalPHIKind() || isOriginalExitPHIKind(); 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 { 1024 return isLatestPHIKind() || isLatestExitPHIKind(); 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 1034 ReductionType getReductionType() const { return RedType; } 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. 1043 void setAccessRelation(isl::map AccessRelation); 1044 1045 /// Set the updated access relation read from JSCOP file. 1046 void setNewAccessRelation(isl::map NewAccessRelation); 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 1053 void markAsReductionLike(ReductionType RT) { RedType = RT; } 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 1088 raw_ostream &operator<<(raw_ostream &OS, MemoryAccess::ReductionType RT); 1089 1090 /// Ordered list type to hold accesses. 1091 using MemoryAccessList = std::forward_list<MemoryAccess *>; 1092 1093 /// Helper structure for invariant memory accesses. 1094 struct InvariantAccess { 1095 /// The memory access that is (partially) invariant. 1096 MemoryAccess *MA; 1097 1098 /// The context under which the access is not invariant. 1099 isl::set NonHoistableCtx; 1100 }; 1101 1102 /// Ordered container type to hold invariant accesses. 1103 using InvariantAccessesTy = SmallVector<InvariantAccess, 8>; 1104 1105 /// Type for equivalent invariant accesses and their domain context. 1106 struct InvariantEquivClassTy { 1107 /// The pointer that identifies this equivalence class 1108 const SCEV *IdentifyingPointer; 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. 1115 MemoryAccessList InvariantAccesses; 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. 1121 isl::set ExecutionContext; 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. 1127 Type *AccessType; 1128 }; 1129 1130 /// Type for invariant accesses equivalence classes. 1131 using 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. 1140 class ScopStmt final { 1141 friend class ScopBuilder; 1142 1143 public: 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, 1168 isl::set Domain); 1169 1170 ScopStmt(const ScopStmt &) = delete; 1171 const ScopStmt &operator=(const ScopStmt &) = delete; 1172 ~ScopStmt(); 1173 1174 private: 1175 /// Polyhedral description 1176 //@{ 1177 1178 /// The Scop containing this ScopStmt. 1179 Scop &Parent; 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. 1186 isl::set InvalidDomain; 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. 1207 isl::set Domain; 1208 1209 /// The memory accesses of this statement. 1210 /// 1211 /// The only side effects of a statement are its memory accesses. 1212 MemoryAccessVec MemAccs; 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. 1256 isl::ast_build Build; 1257 1258 SmallVector<Loop *, 4> NestLoops; 1259 1260 std::string BaseName; 1261 1262 /// The closest loop that contains this statement. 1263 Loop *SurroundingLoop; 1264 1265 /// Vector for Instructions in this statement. 1266 std::vector<Instruction *> Instructions; 1267 1268 /// Remove @p MA from dictionaries pointing to them. 1269 void removeAccessData(MemoryAccess *MA); 1270 1271 public: 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. 1306 isl::set getInvalidDomain() const { return InvalidDomain; } 1307 1308 /// Get the invalid context for this statement. 1309 isl::set getInvalidContext() const { return getInvalidDomain().params(); } 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. 1458 MemoryAccess *lookupPHIReadOf(PHINode *PHI) const { 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. 1465 MemoryAccess *lookupPHIWriteOf(PHINode *PHI) const { 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. 1477 MemoryAccess *lookupInputAccessOf(Value *Val) const { 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 Prepend = 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. 1503 void removeMemoryAccess(MemoryAccess *MA); 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. 1562 void setAstBuild(isl::ast_build B) { Build = B; } 1563 1564 /// Get the isl AST build. 1565 isl::ast_build getAstBuild() const { return 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. 1611 raw_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. 1630 class Scop final { 1631 public: 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 1646 private: 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. 1672 AccFuncVector AccessFunctions; 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. 1678 bool HasSingleExitEdge; 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. 1692 StmtSet Stmts; 1693 1694 /// Parameters of this Scop 1695 ParameterSetTy Parameters; 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. 1701 ScopDetection::DetectionContext &DC; 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. 1717 isl::set Context; 1718 1719 /// The affinator used to translate SCEVs to isl expressions. 1720 SCEVAffinator Affinator; 1721 1722 using ArrayInfoMapTy = 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. 1735 ArrayInfoMapTy ScopArrayInfoMap; 1736 1737 /// A map to remember ScopArrayInfo objects for all names of memory 1738 /// references. 1739 ArrayNameMapTy ScopArrayNameMap; 1740 1741 /// A set to remember ScopArrayInfo objects. 1742 /// @see Scop::ScopArrayInfoMap 1743 ArrayInfoSetTy ScopArrayInfoSet; 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. 1752 isl::set AssumedContext; 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. 1760 isl::set InvalidContext; 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. 1776 isl::set 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. 1812 isl::schedule Schedule; 1813 1814 /// Is this Scop marked as not to be transformed by an optimization heuristic? 1815 bool HasDisableHeuristicsHint = false; 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. 1834 MinMaxVectorPairVectorTy MinMaxAliasGroups; 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. 1841 InvariantEquivClassesTy InvariantEquivClasses; 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>> 1867 PHIIncomingAccs; 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. 1877 MemoryAccess *lookupBasePtrAccess(MemoryAccess *MA); 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). 1923 void removeStmtNotInDomainMap(); 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. 1931 isl::union_map 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 1943 public: 1944 Scop(const Scop &) = delete; 1945 Scop &operator=(const Scop &) = delete; 1946 ~Scop(); 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, 1968 isl::set Domain); 1969 1970 /// Add the access function to all MemoryAccess objects of the Scop 1971 /// created in this pass. 1972 void addAccessFunction(MemoryAccess *Access) { 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 2062 inline const_array_iterator array_begin() const { 2063 return ScopArrayInfoSet.begin(); 2064 } 2065 2066 inline const_array_iterator array_end() const { 2067 return ScopArrayInfoSet.end(); 2068 } 2069 2070 inline array_range arrays() { 2071 return array_range(array_begin(), array_end()); 2072 } 2073 2074 inline const_array_range arrays() const { 2075 return const_array_range(array_begin(), array_end()); 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. 2130 InvariantEquivClassTy *lookupInvariantEquivClass(Value *Val); 2131 2132 /// Return the set of invariant accesses. 2133 InvariantEquivClassesTy &getInvariantAccesses() { 2134 return InvariantEquivClasses; 2135 } 2136 2137 /// Check if the scop has any invariant access. 2138 bool hasInvariantAccesses() { return !InvariantEquivClasses.empty(); } 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. 2167 isl::set getDefinedBehaviorContext() const { return DefinedBehaviorContext; } 2168 2169 /// Return the define behavior context, or if not available, its approximation 2170 /// from all other contexts. 2171 isl::set getBestKnownDefinedBehaviorContext() const { 2172 if (!DefinedBehaviorContext.is_null()) 2173 return DefinedBehaviorContext; 2174 2175 return Context.intersect_params(AssumedContext).subtract(InvalidContext); 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. 2191 isl::space getFullParamSpace() const; 2192 2193 /// Get the assumed context for this Scop. 2194 /// 2195 /// @return The assumed context of this Scop. 2196 isl::set getAssumedContext() const; 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. 2213 bool isEffectiveAssumption(isl::set Set, AssumptionSign Sign); 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. 2234 void intersectDefinedBehavior(isl::set Set, AssumptionSign Sign); 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. 2273 isl::set getInvalidContext() const; 2274 2275 /// Return true if and only if the InvalidContext is trivial (=empty). 2276 bool hasTrivialInvalidContext() const { return InvalidContext.is_empty(); } 2277 2278 /// Return all alias groups for this SCoP. 2279 const MinMaxVectorPairVectorTy &getAliasGroups() const { 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. 2367 const InvariantLoadsSetTy &getRequiredInvariantLoads() const { 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. 2375 const BoxedLoopsSetTy &getBoxedLoops() const { return DC.BoxedLoopsSet; } 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 2382 const MapInsnToMemAcc &getInsnToMemAccMap() const { return DC.InsnToMemAcc; } 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, 2391 MemoryKind Kind, 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. 2410 ScopArrayInfo *getScopArrayInfoOrNull(Value *BasePtr, MemoryKind Kind); 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). 2419 ScopArrayInfo *getScopArrayInfo(Value *BasePtr, MemoryKind Kind); 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. 2425 void invalidateScopArrayInfo(Value *BasePtr, MemoryKind Kind) { 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 2457 void notifyErrorBlock() { HasErrorBlock = true; } 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. 2502 isl::pw_aff 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. 2507 bool hasNSWAddRecForLoop(Loop *L) { return Affinator.hasNSWAddRecForLoop(L); } 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. 2532 isl::union_map getMayWrites(); 2533 2534 /// Get a union map of all must-writes performed in the SCoP. 2535 isl::union_map getMustWrites(); 2536 2537 /// Get a union map of all writes performed in the SCoP. 2538 isl::union_map getWrites(); 2539 2540 /// Get a union map of all reads performed in the SCoP. 2541 isl::union_map getReads(); 2542 2543 /// Get a union map of all memory accesses performed in the SCoP. 2544 isl::union_map getAccesses(); 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. 2549 isl::union_map getAccesses(ScopArrayInfo *Array); 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. 2555 isl::union_map getSchedule() const; 2556 2557 /// Get a schedule tree describing the schedule of all statements. 2558 isl::schedule getScheduleTree() const; 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 2577 bool restrictDomains(isl::union_set Domain); 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 2647 struct ScopStatistics { 2648 int NumAffineLoops = 0; 2649 int NumBoxedLoops = 0; 2650 2651 int NumValueWrites = 0; 2652 int NumValueWritesInLoops = 0; 2653 int NumPHIWrites = 0; 2654 int NumPHIWritesInLoops = 0; 2655 int NumSingletonWrites = 0; 2656 int NumSingletonWritesInLoops = 0; 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. 2664 ScopStatistics getStatistics() const; 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. 2668 bool hasDisableHeuristicsHint() const { return HasDisableHeuristicsHint; } 2669 2670 /// Mark this Scop to not apply an optimization heuristic. 2671 void markDisableHeuristics() { HasDisableHeuristicsHint = true; } 2672 }; 2673 2674 /// Print Scop scop to raw_ostream OS. 2675 raw_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. 2679 class ScopInfoRegionPass final : public RegionPass { 2680 /// The Scop pointer which is used to construct a Scop. 2681 std::unique_ptr<Scop> S; 2682 2683 public: 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 2708 llvm::Pass *createScopInfoPrinterLegacyRegionPass(raw_ostream &OS); 2709 2710 class ScopInfo { 2711 public: 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 2718 private: 2719 /// A map of Region to its Scop object containing 2720 /// Polly IR of static control part. 2721 RegionToScopMapTy RegionToScopMap; 2722 const DataLayout &DL; 2723 ScopDetection &SD; 2724 ScalarEvolution &SE; 2725 LoopInfo &LI; 2726 AAResults &AA; 2727 DominatorTree &DT; 2728 AssumptionCache &AC; 2729 OptimizationRemarkEmitter &ORE; 2730 2731 public: 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(); } 2762 reverse_iterator rbegin() { return RegionToScopMap.rbegin(); } 2763 reverse_iterator rend() { return RegionToScopMap.rend(); } 2764 const_reverse_iterator rbegin() const { return RegionToScopMap.rbegin(); } 2765 const_reverse_iterator rend() const { return RegionToScopMap.rend(); } 2766 bool empty() const { return RegionToScopMap.empty(); } 2767 }; 2768 2769 struct ScopInfoAnalysis : AnalysisInfoMixin<ScopInfoAnalysis> { 2770 static AnalysisKey Key; 2771 2772 using Result = ScopInfo; 2773 2774 Result run(Function &, FunctionAnalysisManager &); 2775 }; 2776 2777 struct 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. 2793 class ScopInfoWrapperPass final : public FunctionPass { 2794 std::unique_ptr<ScopInfo> Result; 2795 2796 public: 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 2815 llvm::Pass *createScopInfoPrinterLegacyFunctionPass(llvm::raw_ostream &OS); 2816 } // end namespace polly 2817 2818 namespace llvm { 2819 void initializeScopInfoRegionPassPass(PassRegistry &); 2820 void initializeScopInfoPrinterLegacyRegionPassPass(PassRegistry &); 2821 void initializeScopInfoWrapperPassPass(PassRegistry &); 2822 void initializeScopInfoPrinterLegacyFunctionPassPass(PassRegistry &); 2823 } // end namespace llvm 2824 2825 #endif // POLLY_SCOPINFO_H 2826