ScopBuilder.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. //===- polly/ScopBuilder.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. // Create a polyhedral description for a static control flow region.
  10. //
  11. // The pass creates a polyhedral description of the Scops detected by the SCoP
  12. // detection derived from their LLVM-IR code.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef POLLY_SCOPBUILDER_H
  16. #define POLLY_SCOPBUILDER_H
  17. #include "polly/ScopInfo.h"
  18. #include "polly/Support/ScopHelper.h"
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/SetVector.h"
  21. namespace polly {
  22. using llvm::SmallSetVector;
  23. class ScopDetection;
  24. /// Command line switch whether to model read-only accesses.
  25. extern bool ModelReadOnlyScalars;
  26. /// Build the Polly IR (Scop and ScopStmt) on a Region.
  27. class ScopBuilder final {
  28. /// The AAResults to build AliasSetTracker.
  29. AAResults &AA;
  30. /// Target data for element size computing.
  31. const DataLayout &DL;
  32. /// DominatorTree to reason about guaranteed execution.
  33. DominatorTree &DT;
  34. /// LoopInfo for information about loops.
  35. LoopInfo &LI;
  36. /// Valid Regions for Scop
  37. ScopDetection &SD;
  38. /// The ScalarEvolution to help building Scop.
  39. ScalarEvolution &SE;
  40. /// An optimization diagnostic interface to add optimization remarks.
  41. OptimizationRemarkEmitter &ORE;
  42. /// Set of instructions that might read any memory location.
  43. SmallVector<std::pair<ScopStmt *, Instruction *>, 16> GlobalReads;
  44. /// Set of all accessed array base pointers.
  45. SmallSetVector<Value *, 16> ArrayBasePointers;
  46. // The Scop
  47. std::unique_ptr<Scop> scop;
  48. /// Collection to hold taken assumptions.
  49. ///
  50. /// There are two reasons why we want to record assumptions first before we
  51. /// add them to the assumed/invalid context:
  52. /// 1) If the SCoP is not profitable or otherwise invalid without the
  53. /// assumed/invalid context we do not have to compute it.
  54. /// 2) Information about the context are gathered rather late in the SCoP
  55. /// construction (basically after we know all parameters), thus the user
  56. /// might see overly complicated assumptions to be taken while they will
  57. /// only be simplified later on.
  58. RecordedAssumptionsTy RecordedAssumptions;
  59. // Build the SCoP for Region @p R.
  60. void buildScop(Region &R, AssumptionCache &AC);
  61. /// Adjust the dimensions of @p Dom that was constructed for @p OldL
  62. /// to be compatible to domains constructed for loop @p NewL.
  63. ///
  64. /// This function assumes @p NewL and @p OldL are equal or there is a CFG
  65. /// edge from @p OldL to @p NewL.
  66. isl::set adjustDomainDimensions(isl::set Dom, Loop *OldL, Loop *NewL);
  67. /// Compute the domain for each basic block in @p R.
  68. ///
  69. /// @param R The region we currently traverse.
  70. /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
  71. /// region.
  72. ///
  73. /// @returns True if there was no problem and false otherwise.
  74. bool buildDomains(Region *R,
  75. DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
  76. /// Compute the branching constraints for each basic block in @p R.
  77. ///
  78. /// @param R The region we currently build branching conditions
  79. /// for.
  80. /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
  81. /// region.
  82. ///
  83. /// @returns True if there was no problem and false otherwise.
  84. bool buildDomainsWithBranchConstraints(
  85. Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
  86. /// Build the conditions sets for the terminator @p TI in the @p Domain.
  87. ///
  88. /// This will fill @p ConditionSets with the conditions under which control
  89. /// will be moved from @p TI to its successors. Hence, @p ConditionSets will
  90. /// have as many elements as @p TI has successors.
  91. bool buildConditionSets(BasicBlock *BB, Instruction *TI, Loop *L,
  92. __isl_keep isl_set *Domain,
  93. DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
  94. SmallVectorImpl<__isl_give isl_set *> &ConditionSets);
  95. /// Build the conditions sets for the branch condition @p Condition in
  96. /// the @p Domain.
  97. ///
  98. /// This will fill @p ConditionSets with the conditions under which control
  99. /// will be moved from @p TI to its successors. Hence, @p ConditionSets will
  100. /// have as many elements as @p TI has successors. If @p TI is nullptr the
  101. /// context under which @p Condition is true/false will be returned as the
  102. /// new elements of @p ConditionSets.
  103. bool buildConditionSets(BasicBlock *BB, Value *Condition, Instruction *TI,
  104. Loop *L, __isl_keep isl_set *Domain,
  105. DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
  106. SmallVectorImpl<__isl_give isl_set *> &ConditionSets);
  107. /// Build the conditions sets for the switch @p SI in the @p Domain.
  108. ///
  109. /// This will fill @p ConditionSets with the conditions under which control
  110. /// will be moved from @p SI to its successors. Hence, @p ConditionSets will
  111. /// have as many elements as @p SI has successors.
  112. bool buildConditionSets(BasicBlock *BB, SwitchInst *SI, Loop *L,
  113. __isl_keep isl_set *Domain,
  114. DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
  115. SmallVectorImpl<__isl_give isl_set *> &ConditionSets);
  116. /// Build condition sets for unsigned ICmpInst(s).
  117. /// Special handling is required for unsigned operands to ensure that if
  118. /// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst
  119. /// it should wrap around.
  120. ///
  121. /// @param IsStrictUpperBound holds information on the predicate relation
  122. /// between TestVal and UpperBound, i.e,
  123. /// TestVal < UpperBound OR TestVal <= UpperBound
  124. __isl_give isl_set *buildUnsignedConditionSets(
  125. BasicBlock *BB, Value *Condition, __isl_keep isl_set *Domain,
  126. const SCEV *SCEV_TestVal, const SCEV *SCEV_UpperBound,
  127. DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
  128. bool IsStrictUpperBound);
  129. /// Propagate the domain constraints through the region @p R.
  130. ///
  131. /// @param R The region we currently build branching
  132. /// conditions for.
  133. /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
  134. /// region.
  135. ///
  136. /// @returns True if there was no problem and false otherwise.
  137. bool propagateDomainConstraints(
  138. Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
  139. /// Propagate domains that are known due to graph properties.
  140. ///
  141. /// As a CFG is mostly structured we use the graph properties to propagate
  142. /// domains without the need to compute all path conditions. In particular,
  143. /// if a block A dominates a block B and B post-dominates A we know that the
  144. /// domain of B is a superset of the domain of A. As we do not have
  145. /// post-dominator information available here we use the less precise region
  146. /// information. Given a region R, we know that the exit is always executed
  147. /// if the entry was executed, thus the domain of the exit is a superset of
  148. /// the domain of the entry. In case the exit can only be reached from
  149. /// within the region the domains are in fact equal. This function will use
  150. /// this property to avoid the generation of condition constraints that
  151. /// determine when a branch is taken. If @p BB is a region entry block we
  152. /// will propagate its domain to the region exit block. Additionally, we put
  153. /// the region exit block in the @p FinishedExitBlocks set so we can later
  154. /// skip edges from within the region to that block.
  155. ///
  156. /// @param BB The block for which the domain is currently
  157. /// propagated.
  158. /// @param BBLoop The innermost affine loop surrounding @p BB.
  159. /// @param FinishedExitBlocks Set of region exits the domain was set for.
  160. /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
  161. /// region.
  162. void propagateDomainConstraintsToRegionExit(
  163. BasicBlock *BB, Loop *BBLoop,
  164. SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks,
  165. DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
  166. /// Propagate invalid domains of statements through @p R.
  167. ///
  168. /// This method will propagate invalid statement domains through @p R and at
  169. /// the same time add error block domains to them. Additionally, the domains
  170. /// of error statements and those only reachable via error statements will
  171. /// be replaced by an empty set. Later those will be removed completely.
  172. ///
  173. /// @param R The currently traversed region.
  174. /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
  175. /// region.
  176. //
  177. /// @returns True if there was no problem and false otherwise.
  178. bool propagateInvalidStmtDomains(
  179. Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
  180. /// Compute the union of predecessor domains for @p BB.
  181. ///
  182. /// To compute the union of all domains of predecessors of @p BB this
  183. /// function applies similar reasoning on the CFG structure as described for
  184. /// @see propagateDomainConstraintsToRegionExit
  185. ///
  186. /// @param BB The block for which the predecessor domains are collected.
  187. /// @param Domain The domain under which BB is executed.
  188. ///
  189. /// @returns The domain under which @p BB is executed.
  190. isl::set getPredecessorDomainConstraints(BasicBlock *BB, isl::set Domain);
  191. /// Add loop carried constraints to the header block of the loop @p L.
  192. ///
  193. /// @param L The loop to process.
  194. /// @param InvalidDomainMap BB to InvalidDomain map for the BB of current
  195. /// region.
  196. ///
  197. /// @returns True if there was no problem and false otherwise.
  198. bool addLoopBoundsToHeaderDomain(
  199. Loop *L, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
  200. /// Compute the isl representation for the SCEV @p E in this BB.
  201. ///
  202. /// @param BB The BB for which isl representation is to be
  203. /// computed.
  204. /// @param InvalidDomainMap A map of BB to their invalid domains.
  205. /// @param E The SCEV that should be translated.
  206. /// @param NonNegative Flag to indicate the @p E has to be
  207. /// non-negative.
  208. ///
  209. /// Note that this function will also adjust the invalid context
  210. /// accordingly.
  211. __isl_give isl_pw_aff *
  212. getPwAff(BasicBlock *BB, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
  213. const SCEV *E, bool NonNegative = false);
  214. /// Create equivalence classes for required invariant accesses.
  215. ///
  216. /// These classes will consolidate multiple required invariant loads from the
  217. /// same address in order to keep the number of dimensions in the SCoP
  218. /// description small. For each such class equivalence class only one
  219. /// representing element, hence one required invariant load, will be chosen
  220. /// and modeled as parameter. The method
  221. /// Scop::getRepresentingInvariantLoadSCEV() will replace each element from an
  222. /// equivalence class with the representing element that is modeled. As a
  223. /// consequence Scop::getIdForParam() will only return an id for the
  224. /// representing element of each equivalence class, thus for each required
  225. /// invariant location.
  226. void buildInvariantEquivalenceClasses();
  227. /// Try to build a multi-dimensional fixed sized MemoryAccess from the
  228. /// Load/Store instruction.
  229. ///
  230. /// @param Inst The Load/Store instruction that access the memory
  231. /// @param Stmt The parent statement of the instruction
  232. ///
  233. /// @returns True if the access could be built, False otherwise.
  234. bool buildAccessMultiDimFixed(MemAccInst Inst, ScopStmt *Stmt);
  235. /// Try to build a multi-dimensional parametric sized MemoryAccess.
  236. /// from the Load/Store instruction.
  237. ///
  238. /// @param Inst The Load/Store instruction that access the memory
  239. /// @param Stmt The parent statement of the instruction
  240. ///
  241. /// @returns True if the access could be built, False otherwise.
  242. bool buildAccessMultiDimParam(MemAccInst Inst, ScopStmt *Stmt);
  243. /// Try to build a MemoryAccess for a memory intrinsic.
  244. ///
  245. /// @param Inst The instruction that access the memory
  246. /// @param Stmt The parent statement of the instruction
  247. ///
  248. /// @returns True if the access could be built, False otherwise.
  249. bool buildAccessMemIntrinsic(MemAccInst Inst, ScopStmt *Stmt);
  250. /// Try to build a MemoryAccess for a call instruction.
  251. ///
  252. /// @param Inst The call instruction that access the memory
  253. /// @param Stmt The parent statement of the instruction
  254. ///
  255. /// @returns True if the access could be built, False otherwise.
  256. bool buildAccessCallInst(MemAccInst Inst, ScopStmt *Stmt);
  257. /// Build a single-dimensional parametric sized MemoryAccess
  258. /// from the Load/Store instruction.
  259. ///
  260. /// @param Inst The Load/Store instruction that access the memory
  261. /// @param Stmt The parent statement of the instruction
  262. ///
  263. /// @returns True if the access could be built, False otherwise.
  264. bool buildAccessSingleDim(MemAccInst Inst, ScopStmt *Stmt);
  265. /// Finalize all access relations.
  266. ///
  267. /// When building up access relations, temporary access relations that
  268. /// correctly represent each individual access are constructed. However, these
  269. /// access relations can be inconsistent or non-optimal when looking at the
  270. /// set of accesses as a whole. This function finalizes the memory accesses
  271. /// and constructs a globally consistent state.
  272. void finalizeAccesses();
  273. /// Update access dimensionalities.
  274. ///
  275. /// When detecting memory accesses different accesses to the same array may
  276. /// have built with different dimensionality, as outer zero-values dimensions
  277. /// may not have been recognized as separate dimensions. This function goes
  278. /// again over all memory accesses and updates their dimensionality to match
  279. /// the dimensionality of the underlying ScopArrayInfo object.
  280. void updateAccessDimensionality();
  281. /// Fold size constants to the right.
  282. ///
  283. /// In case all memory accesses in a given dimension are multiplied with a
  284. /// common constant, we can remove this constant from the individual access
  285. /// functions and move it to the size of the memory access. We do this as this
  286. /// increases the size of the innermost dimension, consequently widens the
  287. /// valid range the array subscript in this dimension can evaluate to, and
  288. /// as a result increases the likelihood that our delinearization is
  289. /// correct.
  290. ///
  291. /// Example:
  292. ///
  293. /// A[][n]
  294. /// S[i,j] -> A[2i][2j+1]
  295. /// S[i,j] -> A[2i][2j]
  296. ///
  297. /// =>
  298. ///
  299. /// A[][2n]
  300. /// S[i,j] -> A[i][2j+1]
  301. /// S[i,j] -> A[i][2j]
  302. ///
  303. /// Constants in outer dimensions can arise when the elements of a parametric
  304. /// multi-dimensional array are not elementary data types, but e.g.,
  305. /// structures.
  306. void foldSizeConstantsToRight();
  307. /// Fold memory accesses to handle parametric offset.
  308. ///
  309. /// As a post-processing step, we 'fold' memory accesses to parametric
  310. /// offsets in the access functions. @see MemoryAccess::foldAccess for
  311. /// details.
  312. void foldAccessRelations();
  313. /// Assume that all memory accesses are within bounds.
  314. ///
  315. /// After we have built a model of all memory accesses, we need to assume
  316. /// that the model we built matches reality -- aka. all modeled memory
  317. /// accesses always remain within bounds. We do this as last step, after
  318. /// all memory accesses have been modeled and canonicalized.
  319. void assumeNoOutOfBounds();
  320. /// Build the alias checks for this SCoP.
  321. bool buildAliasChecks();
  322. /// A vector of memory accesses that belong to an alias group.
  323. using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
  324. /// A vector of alias groups.
  325. using AliasGroupVectorTy = SmallVector<AliasGroupTy, 4>;
  326. /// Build a given alias group and its access data.
  327. ///
  328. /// @param AliasGroup The alias group to build.
  329. /// @param HasWriteAccess A set of arrays through which memory is not only
  330. /// read, but also written.
  331. //
  332. /// @returns True if __no__ error occurred, false otherwise.
  333. bool buildAliasGroup(AliasGroupTy &AliasGroup,
  334. DenseSet<const ScopArrayInfo *> HasWriteAccess);
  335. /// Build all alias groups for this SCoP.
  336. ///
  337. /// @returns True if __no__ error occurred, false otherwise.
  338. bool buildAliasGroups();
  339. /// Build alias groups for all memory accesses in the Scop.
  340. ///
  341. /// Using the alias analysis and an alias set tracker we build alias sets
  342. /// for all memory accesses inside the Scop. For each alias set we then map
  343. /// the aliasing pointers back to the memory accesses we know, thus obtain
  344. /// groups of memory accesses which might alias. We also collect the set of
  345. /// arrays through which memory is written.
  346. ///
  347. /// @returns A pair consistent of a vector of alias groups and a set of arrays
  348. /// through which memory is written.
  349. std::tuple<AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>>
  350. buildAliasGroupsForAccesses();
  351. /// Split alias groups by iteration domains.
  352. ///
  353. /// We split each group based on the domains of the minimal/maximal accesses.
  354. /// That means two minimal/maximal accesses are only in a group if their
  355. /// access domains intersect. Otherwise, they are in different groups.
  356. ///
  357. /// @param AliasGroups The alias groups to split
  358. void splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups);
  359. /// Build an instance of MemoryAccess from the Load/Store instruction.
  360. ///
  361. /// @param Inst The Load/Store instruction that access the memory
  362. /// @param Stmt The parent statement of the instruction
  363. void buildMemoryAccess(MemAccInst Inst, ScopStmt *Stmt);
  364. /// Analyze and extract the cross-BB scalar dependences (or, dataflow
  365. /// dependencies) of an instruction.
  366. ///
  367. /// @param UserStmt The statement @p Inst resides in.
  368. /// @param Inst The instruction to be analyzed.
  369. void buildScalarDependences(ScopStmt *UserStmt, Instruction *Inst);
  370. /// Build the escaping dependences for @p Inst.
  371. ///
  372. /// Search for uses of the llvm::Value defined by @p Inst that are not
  373. /// within the SCoP. If there is such use, add a SCALAR WRITE such that
  374. /// it is available after the SCoP as escaping value.
  375. ///
  376. /// @param Inst The instruction to be analyzed.
  377. void buildEscapingDependences(Instruction *Inst);
  378. /// Create MemoryAccesses for the given PHI node in the given region.
  379. ///
  380. /// @param PHIStmt The statement @p PHI resides in.
  381. /// @param PHI The PHI node to be handled
  382. /// @param NonAffineSubRegion The non affine sub-region @p PHI is in.
  383. /// @param IsExitBlock Flag to indicate that @p PHI is in the exit BB.
  384. void buildPHIAccesses(ScopStmt *PHIStmt, PHINode *PHI,
  385. Region *NonAffineSubRegion, bool IsExitBlock = false);
  386. /// Build the access functions for the subregion @p SR.
  387. void buildAccessFunctions();
  388. /// Should an instruction be modeled in a ScopStmt.
  389. ///
  390. /// @param Inst The instruction to check.
  391. /// @param L The loop in which context the instruction is looked at.
  392. ///
  393. /// @returns True if the instruction should be modeled.
  394. bool shouldModelInst(Instruction *Inst, Loop *L);
  395. /// Create one or more ScopStmts for @p BB.
  396. ///
  397. /// Consecutive instructions are associated to the same statement until a
  398. /// separator is found.
  399. void buildSequentialBlockStmts(BasicBlock *BB, bool SplitOnStore = false);
  400. /// Create one or more ScopStmts for @p BB using equivalence classes.
  401. ///
  402. /// Instructions of a basic block that belong to the same equivalence class
  403. /// are added to the same statement.
  404. void buildEqivClassBlockStmts(BasicBlock *BB);
  405. /// Create ScopStmt for all BBs and non-affine subregions of @p SR.
  406. ///
  407. /// @param SR A subregion of @p R.
  408. ///
  409. /// Some of the statements might be optimized away later when they do not
  410. /// access any memory and thus have no effect.
  411. void buildStmts(Region &SR);
  412. /// Build the access functions for the statement @p Stmt in or represented by
  413. /// @p BB.
  414. ///
  415. /// @param Stmt Statement to add MemoryAccesses to.
  416. /// @param BB A basic block in @p R.
  417. /// @param NonAffineSubRegion The non affine sub-region @p BB is in.
  418. void buildAccessFunctions(ScopStmt *Stmt, BasicBlock &BB,
  419. Region *NonAffineSubRegion = nullptr);
  420. /// Create a new MemoryAccess object and add it to #AccFuncMap.
  421. ///
  422. /// @param Stmt The statement where the access takes place.
  423. /// @param Inst The instruction doing the access. It is not necessarily
  424. /// inside @p BB.
  425. /// @param AccType The kind of access.
  426. /// @param BaseAddress The accessed array's base address.
  427. /// @param ElemType The type of the accessed array elements.
  428. /// @param Affine Whether all subscripts are affine expressions.
  429. /// @param AccessValue Value read or written.
  430. /// @param Subscripts Access subscripts per dimension.
  431. /// @param Sizes The array dimension's sizes.
  432. /// @param Kind The kind of memory accessed.
  433. ///
  434. /// @return The created MemoryAccess, or nullptr if the access is not within
  435. /// the SCoP.
  436. MemoryAccess *addMemoryAccess(ScopStmt *Stmt, Instruction *Inst,
  437. MemoryAccess::AccessType AccType,
  438. Value *BaseAddress, Type *ElemType, bool Affine,
  439. Value *AccessValue,
  440. ArrayRef<const SCEV *> Subscripts,
  441. ArrayRef<const SCEV *> Sizes, MemoryKind Kind);
  442. /// Create a MemoryAccess that represents either a LoadInst or
  443. /// StoreInst.
  444. ///
  445. /// @param Stmt The statement to add the MemoryAccess to.
  446. /// @param MemAccInst The LoadInst or StoreInst.
  447. /// @param AccType The kind of access.
  448. /// @param BaseAddress The accessed array's base address.
  449. /// @param ElemType The type of the accessed array elements.
  450. /// @param IsAffine Whether all subscripts are affine expressions.
  451. /// @param Subscripts Access subscripts per dimension.
  452. /// @param Sizes The array dimension's sizes.
  453. /// @param AccessValue Value read or written.
  454. ///
  455. /// @see MemoryKind
  456. void addArrayAccess(ScopStmt *Stmt, MemAccInst MemAccInst,
  457. MemoryAccess::AccessType AccType, Value *BaseAddress,
  458. Type *ElemType, bool IsAffine,
  459. ArrayRef<const SCEV *> Subscripts,
  460. ArrayRef<const SCEV *> Sizes, Value *AccessValue);
  461. /// Create a MemoryAccess for writing an llvm::Instruction.
  462. ///
  463. /// The access will be created at the position of @p Inst.
  464. ///
  465. /// @param Inst The instruction to be written.
  466. ///
  467. /// @see ensureValueRead()
  468. /// @see MemoryKind
  469. void ensureValueWrite(Instruction *Inst);
  470. /// Ensure an llvm::Value is available in the BB's statement, creating a
  471. /// MemoryAccess for reloading it if necessary.
  472. ///
  473. /// @param V The value expected to be loaded.
  474. /// @param UserStmt Where to reload the value.
  475. ///
  476. /// @see ensureValueStore()
  477. /// @see MemoryKind
  478. void ensureValueRead(Value *V, ScopStmt *UserStmt);
  479. /// Create a write MemoryAccess for the incoming block of a phi node.
  480. ///
  481. /// Each of the incoming blocks write their incoming value to be picked in the
  482. /// phi's block.
  483. ///
  484. /// @param PHI PHINode under consideration.
  485. /// @param IncomingStmt The statement to add the MemoryAccess to.
  486. /// @param IncomingBlock Some predecessor block.
  487. /// @param IncomingValue @p PHI's value when coming from @p IncomingBlock.
  488. /// @param IsExitBlock When true, uses the .s2a alloca instead of the
  489. /// .phiops one. Required for values escaping through a
  490. /// PHINode in the SCoP region's exit block.
  491. /// @see addPHIReadAccess()
  492. /// @see MemoryKind
  493. void ensurePHIWrite(PHINode *PHI, ScopStmt *IncomintStmt,
  494. BasicBlock *IncomingBlock, Value *IncomingValue,
  495. bool IsExitBlock);
  496. /// Add user provided parameter constraints to context (command line).
  497. void addUserContext();
  498. /// Add user provided parameter constraints to context (source code).
  499. void addUserAssumptions(AssumptionCache &AC,
  500. DenseMap<BasicBlock *, isl::set> &InvalidDomainMap);
  501. /// Add all recorded assumptions to the assumed context.
  502. void addRecordedAssumptions();
  503. /// Create a MemoryAccess for reading the value of a phi.
  504. ///
  505. /// The modeling assumes that all incoming blocks write their incoming value
  506. /// to the same location. Thus, this access will read the incoming block's
  507. /// value as instructed by this @p PHI.
  508. ///
  509. /// @param PHIStmt Statement @p PHI resides in.
  510. /// @param PHI PHINode under consideration; the READ access will be added
  511. /// here.
  512. ///
  513. /// @see ensurePHIWrite()
  514. /// @see MemoryKind
  515. void addPHIReadAccess(ScopStmt *PHIStmt, PHINode *PHI);
  516. /// Wrapper function to calculate minimal/maximal accesses to each array.
  517. bool calculateMinMaxAccess(AliasGroupTy AliasGroup,
  518. Scop::MinMaxVectorTy &MinMaxAccesses);
  519. /// Build the domain of @p Stmt.
  520. void buildDomain(ScopStmt &Stmt);
  521. /// Fill NestLoops with loops surrounding @p Stmt.
  522. void collectSurroundingLoops(ScopStmt &Stmt);
  523. /// Check for reductions in @p Stmt.
  524. ///
  525. /// Iterate over all store memory accesses and check for valid binary
  526. /// reduction like chains. For all candidates we check if they have the same
  527. /// base address and there are no other accesses which overlap with them. The
  528. /// base address check rules out impossible reductions candidates early. The
  529. /// overlap check, together with the "only one user" check in
  530. /// collectCandidateReductionLoads, guarantees that none of the intermediate
  531. /// results will escape during execution of the loop nest. We basically check
  532. /// here that no other memory access can access the same memory as the
  533. /// potential reduction.
  534. void checkForReductions(ScopStmt &Stmt);
  535. /// Verify that all required invariant loads have been hoisted.
  536. ///
  537. /// Invariant load hoisting is not guaranteed to hoist all loads that were
  538. /// assumed to be scop invariant during scop detection. This function checks
  539. /// for cases where the hoisting failed, but where it would have been
  540. /// necessary for our scop modeling to be correct. In case of insufficient
  541. /// hoisting the scop is marked as invalid.
  542. ///
  543. /// In the example below Bound[1] is required to be invariant:
  544. ///
  545. /// for (int i = 1; i < Bound[0]; i++)
  546. /// for (int j = 1; j < Bound[1]; j++)
  547. /// ...
  548. void verifyInvariantLoads();
  549. /// Hoist invariant memory loads and check for required ones.
  550. ///
  551. /// We first identify "common" invariant loads, thus loads that are invariant
  552. /// and can be hoisted. Then we check if all required invariant loads have
  553. /// been identified as (common) invariant. A load is a required invariant load
  554. /// if it was assumed to be invariant during SCoP detection, e.g., to assume
  555. /// loop bounds to be affine or runtime alias checks to be placeable. In case
  556. /// a required invariant load was not identified as (common) invariant we will
  557. /// drop this SCoP. An example for both "common" as well as required invariant
  558. /// loads is given below:
  559. ///
  560. /// for (int i = 1; i < *LB[0]; i++)
  561. /// for (int j = 1; j < *LB[1]; j++)
  562. /// A[i][j] += A[0][0] + (*V);
  563. ///
  564. /// Common inv. loads: V, A[0][0], LB[0], LB[1]
  565. /// Required inv. loads: LB[0], LB[1], (V, if it may alias with A or LB)
  566. void hoistInvariantLoads();
  567. /// Add invariant loads listed in @p InvMAs with the domain of @p Stmt.
  568. void addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs);
  569. /// Check if @p MA can always be hoisted without execution context.
  570. bool canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty,
  571. bool MAInvalidCtxIsEmpty,
  572. bool NonHoistableCtxIsEmpty);
  573. /// Return true if and only if @p LI is a required invariant load.
  574. bool isRequiredInvariantLoad(LoadInst *LI) const {
  575. return scop->getRequiredInvariantLoads().count(LI);
  576. }
  577. /// Check if the base ptr of @p MA is in the SCoP but not hoistable.
  578. bool hasNonHoistableBasePtrInScop(MemoryAccess *MA, isl::union_map Writes);
  579. /// Return the context under which the access cannot be hoisted.
  580. ///
  581. /// @param Access The access to check.
  582. /// @param Writes The set of all memory writes in the scop.
  583. ///
  584. /// @return Return the context under which the access cannot be hoisted or a
  585. /// nullptr if it cannot be hoisted at all.
  586. isl::set getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes);
  587. /// Collect loads which might form a reduction chain with @p StoreMA.
  588. ///
  589. /// Check if the stored value for @p StoreMA is a binary operator with one or
  590. /// two loads as operands. If the binary operand is commutative & associative,
  591. /// used only once (by @p StoreMA) and its load operands are also used only
  592. /// once, we have found a possible reduction chain. It starts at an operand
  593. /// load and includes the binary operator and @p StoreMA.
  594. ///
  595. /// Note: We allow only one use to ensure the load and binary operator cannot
  596. /// escape this block or into any other store except @p StoreMA.
  597. void collectCandidateReductionLoads(MemoryAccess *StoreMA,
  598. SmallVectorImpl<MemoryAccess *> &Loads);
  599. /// Build the access relation of all memory accesses of @p Stmt.
  600. void buildAccessRelations(ScopStmt &Stmt);
  601. /// Canonicalize arrays with base pointers from the same equivalence class.
  602. ///
  603. /// Some context: in our normal model we assume that each base pointer is
  604. /// related to a single specific memory region, where memory regions
  605. /// associated with different base pointers are disjoint. Consequently we do
  606. /// not need to compute additional data dependences that model possible
  607. /// overlaps of these memory regions. To verify our assumption we compute
  608. /// alias checks that verify that modeled arrays indeed do not overlap. In
  609. /// case an overlap is detected the runtime check fails and we fall back to
  610. /// the original code.
  611. ///
  612. /// In case of arrays where the base pointers are know to be identical,
  613. /// because they are dynamically loaded by accesses that are in the same
  614. /// invariant load equivalence class, such run-time alias check would always
  615. /// be false.
  616. ///
  617. /// This function makes sure that we do not generate consistently failing
  618. /// run-time checks for code that contains distinct arrays with known
  619. /// equivalent base pointers. It identifies for each invariant load
  620. /// equivalence class a single canonical array and canonicalizes all memory
  621. /// accesses that reference arrays that have base pointers that are known to
  622. /// be equal to the base pointer of such a canonical array to this canonical
  623. /// array.
  624. ///
  625. /// We currently do not canonicalize arrays for which certain memory accesses
  626. /// have been hoisted as loop invariant.
  627. void canonicalizeDynamicBasePtrs();
  628. /// Construct the schedule of this SCoP.
  629. void buildSchedule();
  630. /// A loop stack element to keep track of per-loop information during
  631. /// schedule construction.
  632. using LoopStackElementTy = struct LoopStackElement {
  633. // The loop for which we keep information.
  634. Loop *L;
  635. // The (possibly incomplete) schedule for this loop.
  636. isl::schedule Schedule;
  637. // The number of basic blocks in the current loop, for which a schedule has
  638. // already been constructed.
  639. unsigned NumBlocksProcessed;
  640. LoopStackElement(Loop *L, isl::schedule S, unsigned NumBlocksProcessed)
  641. : L(L), Schedule(S), NumBlocksProcessed(NumBlocksProcessed) {}
  642. };
  643. /// The loop stack used for schedule construction.
  644. ///
  645. /// The loop stack keeps track of schedule information for a set of nested
  646. /// loops as well as an (optional) 'nullptr' loop that models the outermost
  647. /// schedule dimension. The loops in a loop stack always have a parent-child
  648. /// relation where the loop at position n is the parent of the loop at
  649. /// position n + 1.
  650. using LoopStackTy = SmallVector<LoopStackElementTy, 4>;
  651. /// Construct schedule information for a given Region and add the
  652. /// derived information to @p LoopStack.
  653. ///
  654. /// Given a Region we derive schedule information for all RegionNodes
  655. /// contained in this region ensuring that the assigned execution times
  656. /// correctly model the existing control flow relations.
  657. ///
  658. /// @param R The region which to process.
  659. /// @param LoopStack A stack of loops that are currently under
  660. /// construction.
  661. void buildSchedule(Region *R, LoopStackTy &LoopStack);
  662. /// Build Schedule for the region node @p RN and add the derived
  663. /// information to @p LoopStack.
  664. ///
  665. /// In case @p RN is a BasicBlock or a non-affine Region, we construct the
  666. /// schedule for this @p RN and also finalize loop schedules in case the
  667. /// current @p RN completes the loop.
  668. ///
  669. /// In case @p RN is a not-non-affine Region, we delegate the construction to
  670. /// buildSchedule(Region *R, ...).
  671. ///
  672. /// @param RN The RegionNode region traversed.
  673. /// @param LoopStack A stack of loops that are currently under
  674. /// construction.
  675. void buildSchedule(RegionNode *RN, LoopStackTy &LoopStack);
  676. public:
  677. explicit ScopBuilder(Region *R, AssumptionCache &AC, AAResults &AA,
  678. const DataLayout &DL, DominatorTree &DT, LoopInfo &LI,
  679. ScopDetection &SD, ScalarEvolution &SE,
  680. OptimizationRemarkEmitter &ORE);
  681. ScopBuilder(const ScopBuilder &) = delete;
  682. ScopBuilder &operator=(const ScopBuilder &) = delete;
  683. ~ScopBuilder() = default;
  684. /// Try to build the Polly IR of static control part on the current
  685. /// SESE-Region.
  686. ///
  687. /// @return Give up the ownership of the scop object or static control part
  688. /// for the region
  689. std::unique_ptr<Scop> getScop() { return std::move(scop); }
  690. };
  691. } // end namespace polly
  692. #endif // POLLY_SCOPBUILDER_H