ForwardOpTree.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. //===- ForwardOpTree.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. // Move instructions between statements.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/ForwardOpTree.h"
  13. #include "polly/Options.h"
  14. #include "polly/ScopBuilder.h"
  15. #include "polly/ScopInfo.h"
  16. #include "polly/ScopPass.h"
  17. #include "polly/Support/GICHelper.h"
  18. #include "polly/Support/ISLOStream.h"
  19. #include "polly/Support/ISLTools.h"
  20. #include "polly/Support/VirtualInstruction.h"
  21. #include "polly/ZoneAlgo.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/Statistic.h"
  25. #include "llvm/Analysis/LoopInfo.h"
  26. #include "llvm/Analysis/ValueTracking.h"
  27. #include "llvm/IR/Instruction.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/Value.h"
  30. #include "llvm/InitializePasses.h"
  31. #include "llvm/Support/Casting.h"
  32. #include "llvm/Support/CommandLine.h"
  33. #include "llvm/Support/Compiler.h"
  34. #include "llvm/Support/Debug.h"
  35. #include "llvm/Support/ErrorHandling.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include "isl/ctx.h"
  38. #include "isl/isl-noexceptions.h"
  39. #include <cassert>
  40. #include <memory>
  41. #define DEBUG_TYPE "polly-optree"
  42. using namespace llvm;
  43. using namespace polly;
  44. static cl::opt<bool>
  45. AnalyzeKnown("polly-optree-analyze-known",
  46. cl::desc("Analyze array contents for load forwarding"),
  47. cl::cat(PollyCategory), cl::init(true), cl::Hidden);
  48. static cl::opt<bool>
  49. NormalizePHIs("polly-optree-normalize-phi",
  50. cl::desc("Replace PHIs by their incoming values"),
  51. cl::cat(PollyCategory), cl::init(false), cl::Hidden);
  52. static cl::opt<unsigned>
  53. MaxOps("polly-optree-max-ops",
  54. cl::desc("Maximum number of ISL operations to invest for known "
  55. "analysis; 0=no limit"),
  56. cl::init(1000000), cl::cat(PollyCategory), cl::Hidden);
  57. STATISTIC(KnownAnalyzed, "Number of successfully analyzed SCoPs");
  58. STATISTIC(KnownOutOfQuota,
  59. "Analyses aborted because max_operations was reached");
  60. STATISTIC(TotalInstructionsCopied, "Number of copied instructions");
  61. STATISTIC(TotalKnownLoadsForwarded,
  62. "Number of forwarded loads because their value was known");
  63. STATISTIC(TotalReloads, "Number of reloaded values");
  64. STATISTIC(TotalReadOnlyCopied, "Number of copied read-only accesses");
  65. STATISTIC(TotalForwardedTrees, "Number of forwarded operand trees");
  66. STATISTIC(TotalModifiedStmts,
  67. "Number of statements with at least one forwarded tree");
  68. STATISTIC(ScopsModified, "Number of SCoPs with at least one forwarded tree");
  69. STATISTIC(NumValueWrites, "Number of scalar value writes after OpTree");
  70. STATISTIC(NumValueWritesInLoops,
  71. "Number of scalar value writes nested in affine loops after OpTree");
  72. STATISTIC(NumPHIWrites, "Number of scalar phi writes after OpTree");
  73. STATISTIC(NumPHIWritesInLoops,
  74. "Number of scalar phi writes nested in affine loops after OpTree");
  75. STATISTIC(NumSingletonWrites, "Number of singleton writes after OpTree");
  76. STATISTIC(NumSingletonWritesInLoops,
  77. "Number of singleton writes nested in affine loops after OpTree");
  78. namespace {
  79. /// The state of whether an operand tree was/can be forwarded.
  80. ///
  81. /// The items apply to an instructions and its operand tree with the instruction
  82. /// as the root element. If the value in question is not an instruction in the
  83. /// SCoP, it can be a leaf of an instruction's operand tree.
  84. enum ForwardingDecision {
  85. /// An uninitialized value.
  86. FD_Unknown,
  87. /// The root instruction or value cannot be forwarded at all.
  88. FD_CannotForward,
  89. /// The root instruction or value can be forwarded as a leaf of a larger
  90. /// operand tree.
  91. /// It does not make sense to move the value itself, it would just replace it
  92. /// by a use of itself. For instance, a constant "5" used in a statement can
  93. /// be forwarded, but it would just replace it by the same constant "5".
  94. /// However, it makes sense to move as an operand of
  95. ///
  96. /// %add = add 5, 5
  97. ///
  98. /// where "5" is moved as part of a larger operand tree. "5" would be placed
  99. /// (disregarding for a moment that literal constants don't have a location
  100. /// and can be used anywhere) into the same statement as %add would.
  101. FD_CanForwardLeaf,
  102. /// The root instruction can be forwarded and doing so avoids a scalar
  103. /// dependency.
  104. ///
  105. /// This can be either because the operand tree can be moved to the target
  106. /// statement, or a memory access is redirected to read from a different
  107. /// location.
  108. FD_CanForwardProfitably,
  109. /// A forwarding method cannot be applied to the operand tree.
  110. /// The difference to FD_CannotForward is that there might be other methods
  111. /// that can handle it.
  112. FD_NotApplicable
  113. };
  114. /// Represents the evaluation of and action to taken when forwarding a value
  115. /// from an operand tree.
  116. struct ForwardingAction {
  117. using KeyTy = std::pair<Value *, ScopStmt *>;
  118. /// Evaluation of forwarding a value.
  119. ForwardingDecision Decision = FD_Unknown;
  120. /// Callback to execute the forwarding.
  121. /// Returning true allows deleting the polly::MemoryAccess if the value is the
  122. /// root of the operand tree (and its elimination the reason why the
  123. /// forwarding is done). Return false if the MemoryAccess is reused or there
  124. /// might be other users of the read accesses. In the letter case the
  125. /// polly::SimplifyPass can remove dead MemoryAccesses.
  126. std::function<bool()> Execute = []() -> bool {
  127. llvm_unreachable("unspecified how to forward");
  128. };
  129. /// Other values that need to be forwarded if this action is executed. Their
  130. /// actions are executed after this one.
  131. SmallVector<KeyTy, 4> Depends;
  132. /// Named ctor: The method creating this object does not apply to the kind of
  133. /// value, but other methods may.
  134. static ForwardingAction notApplicable() {
  135. ForwardingAction Result;
  136. Result.Decision = FD_NotApplicable;
  137. return Result;
  138. }
  139. /// Named ctor: The value cannot be forwarded.
  140. static ForwardingAction cannotForward() {
  141. ForwardingAction Result;
  142. Result.Decision = FD_CannotForward;
  143. return Result;
  144. }
  145. /// Named ctor: The value can just be used without any preparation.
  146. static ForwardingAction triviallyForwardable(bool IsProfitable, Value *Val) {
  147. ForwardingAction Result;
  148. Result.Decision =
  149. IsProfitable ? FD_CanForwardProfitably : FD_CanForwardLeaf;
  150. Result.Execute = [=]() {
  151. LLVM_DEBUG(dbgs() << " trivially forwarded: " << *Val << "\n");
  152. return true;
  153. };
  154. return Result;
  155. }
  156. /// Name ctor: The value can be forwarded by executing an action.
  157. static ForwardingAction canForward(std::function<bool()> Execute,
  158. ArrayRef<KeyTy> Depends,
  159. bool IsProfitable) {
  160. ForwardingAction Result;
  161. Result.Decision =
  162. IsProfitable ? FD_CanForwardProfitably : FD_CanForwardLeaf;
  163. Result.Execute = std::move(Execute);
  164. Result.Depends.append(Depends.begin(), Depends.end());
  165. return Result;
  166. }
  167. };
  168. /// Implementation of operand tree forwarding for a specific SCoP.
  169. ///
  170. /// For a statement that requires a scalar value (through a value read
  171. /// MemoryAccess), see if its operand can be moved into the statement. If so,
  172. /// the MemoryAccess is removed and the all the operand tree instructions are
  173. /// moved into the statement. All original instructions are left in the source
  174. /// statements. The simplification pass can clean these up.
  175. class ForwardOpTreeImpl : ZoneAlgorithm {
  176. private:
  177. using MemoizationTy = DenseMap<ForwardingAction::KeyTy, ForwardingAction>;
  178. /// Scope guard to limit the number of isl operations for this pass.
  179. IslMaxOperationsGuard &MaxOpGuard;
  180. /// How many instructions have been copied to other statements.
  181. int NumInstructionsCopied = 0;
  182. /// Number of loads forwarded because their value was known.
  183. int NumKnownLoadsForwarded = 0;
  184. /// Number of values reloaded from known array elements.
  185. int NumReloads = 0;
  186. /// How many read-only accesses have been copied.
  187. int NumReadOnlyCopied = 0;
  188. /// How many operand trees have been forwarded.
  189. int NumForwardedTrees = 0;
  190. /// Number of statements with at least one forwarded operand tree.
  191. int NumModifiedStmts = 0;
  192. /// Whether we carried out at least one change to the SCoP.
  193. bool Modified = false;
  194. /// Cache of how to forward values.
  195. /// The key of this map is the llvm::Value to be forwarded and the
  196. /// polly::ScopStmt it is forwarded from. This is because the same llvm::Value
  197. /// can evaluate differently depending on where it is evaluate. For instance,
  198. /// a synthesizable Scev represents a recurrence with an loop but the loop's
  199. /// exit value if evaluated after the loop.
  200. /// The cached results are only valid for the current TargetStmt.
  201. /// CHECKME: ScalarEvolution::getScevAtScope should take care for getting the
  202. /// exit value when instantiated outside of the loop. The primary concern is
  203. /// ambiguity when crossing PHI nodes, which currently is not supported.
  204. MemoizationTy ForwardingActions;
  205. /// Contains the zones where array elements are known to contain a specific
  206. /// value.
  207. /// { [Element[] -> Zone[]] -> ValInst[] }
  208. /// @see computeKnown()
  209. isl::union_map Known;
  210. /// Translator for newly introduced ValInsts to already existing ValInsts such
  211. /// that new introduced load instructions can reuse the Known analysis of its
  212. /// original load. { ValInst[] -> ValInst[] }
  213. isl::union_map Translator;
  214. /// Get list of array elements that do contain the same ValInst[] at Domain[].
  215. ///
  216. /// @param ValInst { Domain[] -> ValInst[] }
  217. /// The values for which we search for alternative locations,
  218. /// per statement instance.
  219. ///
  220. /// @return { Domain[] -> Element[] }
  221. /// For each statement instance, the array elements that contain the
  222. /// same ValInst.
  223. isl::union_map findSameContentElements(isl::union_map ValInst) {
  224. assert(!ValInst.is_single_valued().is_false());
  225. // { Domain[] }
  226. isl::union_set Domain = ValInst.domain();
  227. // { Domain[] -> Scatter[] }
  228. isl::union_map Schedule = getScatterFor(Domain);
  229. // { Element[] -> [Scatter[] -> ValInst[]] }
  230. isl::union_map MustKnownCurried =
  231. convertZoneToTimepoints(Known, isl::dim::in, false, true).curry();
  232. // { [Domain[] -> ValInst[]] -> Scatter[] }
  233. isl::union_map DomValSched = ValInst.domain_map().apply_range(Schedule);
  234. // { [Scatter[] -> ValInst[]] -> [Domain[] -> ValInst[]] }
  235. isl::union_map SchedValDomVal =
  236. DomValSched.range_product(ValInst.range_map()).reverse();
  237. // { Element[] -> [Domain[] -> ValInst[]] }
  238. isl::union_map MustKnownInst = MustKnownCurried.apply_range(SchedValDomVal);
  239. // { Domain[] -> Element[] }
  240. isl::union_map MustKnownMap =
  241. MustKnownInst.uncurry().domain().unwrap().reverse();
  242. simplify(MustKnownMap);
  243. return MustKnownMap;
  244. }
  245. /// Find a single array element for each statement instance, within a single
  246. /// array.
  247. ///
  248. /// @param MustKnown { Domain[] -> Element[] }
  249. /// Set of candidate array elements.
  250. /// @param Domain { Domain[] }
  251. /// The statement instance for which we need elements for.
  252. ///
  253. /// @return { Domain[] -> Element[] }
  254. /// For each statement instance, an array element out of @p MustKnown.
  255. /// All array elements must be in the same array (Polly does not yet
  256. /// support reading from different accesses using the same
  257. /// MemoryAccess). If no mapping for all of @p Domain exists, returns
  258. /// null.
  259. isl::map singleLocation(isl::union_map MustKnown, isl::set Domain) {
  260. // { Domain[] -> Element[] }
  261. isl::map Result;
  262. // Make irrelevant elements not interfere.
  263. Domain = Domain.intersect_params(S->getContext());
  264. // MemoryAccesses can read only elements from a single array
  265. // (i.e. not: { Dom[0] -> A[0]; Dom[1] -> B[1] }).
  266. // Look through all spaces until we find one that contains at least the
  267. // wanted statement instance.s
  268. for (isl::map Map : MustKnown.get_map_list()) {
  269. // Get the array this is accessing.
  270. isl::id ArrayId = Map.get_tuple_id(isl::dim::out);
  271. ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(ArrayId.get_user());
  272. // No support for generation of indirect array accesses.
  273. if (SAI->getBasePtrOriginSAI())
  274. continue;
  275. // Determine whether this map contains all wanted values.
  276. isl::set MapDom = Map.domain();
  277. if (!Domain.is_subset(MapDom).is_true())
  278. continue;
  279. // There might be multiple array elements that contain the same value, but
  280. // choose only one of them. lexmin is used because it returns a one-value
  281. // mapping, we do not care about which one.
  282. // TODO: Get the simplest access function.
  283. Result = Map.lexmin();
  284. break;
  285. }
  286. return Result;
  287. }
  288. public:
  289. ForwardOpTreeImpl(Scop *S, LoopInfo *LI, IslMaxOperationsGuard &MaxOpGuard)
  290. : ZoneAlgorithm("polly-optree", S, LI), MaxOpGuard(MaxOpGuard) {}
  291. /// Compute the zones of known array element contents.
  292. ///
  293. /// @return True if the computed #Known is usable.
  294. bool computeKnownValues() {
  295. isl::union_map MustKnown, KnownFromLoad, KnownFromInit;
  296. // Check that nothing strange occurs.
  297. collectCompatibleElts();
  298. {
  299. IslQuotaScope QuotaScope = MaxOpGuard.enter();
  300. computeCommon();
  301. if (NormalizePHIs)
  302. computeNormalizedPHIs();
  303. Known = computeKnown(true, true);
  304. // Preexisting ValInsts use the known content analysis of themselves.
  305. Translator = makeIdentityMap(Known.range(), false);
  306. }
  307. if (Known.is_null() || Translator.is_null() || NormalizeMap.is_null()) {
  308. assert(isl_ctx_last_error(IslCtx.get()) == isl_error_quota);
  309. Known = {};
  310. Translator = {};
  311. NormalizeMap = {};
  312. LLVM_DEBUG(dbgs() << "Known analysis exceeded max_operations\n");
  313. return false;
  314. }
  315. KnownAnalyzed++;
  316. LLVM_DEBUG(dbgs() << "All known: " << Known << "\n");
  317. return true;
  318. }
  319. void printStatistics(raw_ostream &OS, int Indent = 0) {
  320. OS.indent(Indent) << "Statistics {\n";
  321. OS.indent(Indent + 4) << "Instructions copied: " << NumInstructionsCopied
  322. << '\n';
  323. OS.indent(Indent + 4) << "Known loads forwarded: " << NumKnownLoadsForwarded
  324. << '\n';
  325. OS.indent(Indent + 4) << "Reloads: " << NumReloads << '\n';
  326. OS.indent(Indent + 4) << "Read-only accesses copied: " << NumReadOnlyCopied
  327. << '\n';
  328. OS.indent(Indent + 4) << "Operand trees forwarded: " << NumForwardedTrees
  329. << '\n';
  330. OS.indent(Indent + 4) << "Statements with forwarded operand trees: "
  331. << NumModifiedStmts << '\n';
  332. OS.indent(Indent) << "}\n";
  333. }
  334. void printStatements(raw_ostream &OS, int Indent = 0) const {
  335. OS.indent(Indent) << "After statements {\n";
  336. for (auto &Stmt : *S) {
  337. OS.indent(Indent + 4) << Stmt.getBaseName() << "\n";
  338. for (auto *MA : Stmt)
  339. MA->print(OS);
  340. OS.indent(Indent + 12);
  341. Stmt.printInstructions(OS);
  342. }
  343. OS.indent(Indent) << "}\n";
  344. }
  345. /// Create a new MemoryAccess of type read and MemoryKind::Array.
  346. ///
  347. /// @param Stmt The statement in which the access occurs.
  348. /// @param LI The instruction that does the access.
  349. /// @param AccessRelation The array element that each statement instance
  350. /// accesses.
  351. ///
  352. /// @param The newly created access.
  353. MemoryAccess *makeReadArrayAccess(ScopStmt *Stmt, LoadInst *LI,
  354. isl::map AccessRelation) {
  355. isl::id ArrayId = AccessRelation.get_tuple_id(isl::dim::out);
  356. ScopArrayInfo *SAI = reinterpret_cast<ScopArrayInfo *>(ArrayId.get_user());
  357. // Create a dummy SCEV access, to be replaced anyway.
  358. SmallVector<const SCEV *, 4> Sizes;
  359. Sizes.reserve(SAI->getNumberOfDimensions());
  360. SmallVector<const SCEV *, 4> Subscripts;
  361. Subscripts.reserve(SAI->getNumberOfDimensions());
  362. for (unsigned i = 0; i < SAI->getNumberOfDimensions(); i += 1) {
  363. Sizes.push_back(SAI->getDimensionSize(i));
  364. Subscripts.push_back(nullptr);
  365. }
  366. MemoryAccess *Access =
  367. new MemoryAccess(Stmt, LI, MemoryAccess::READ, SAI->getBasePtr(),
  368. LI->getType(), true, {}, Sizes, LI, MemoryKind::Array);
  369. S->addAccessFunction(Access);
  370. Stmt->addAccess(Access, true);
  371. Access->setNewAccessRelation(AccessRelation);
  372. return Access;
  373. }
  374. /// Forward a load by reading from an array element that contains the same
  375. /// value. Typically the location it was loaded from.
  376. ///
  377. /// @param TargetStmt The statement the operand tree will be copied to.
  378. /// @param Inst The (possibly speculatable) instruction to forward.
  379. /// @param UseStmt The statement that uses @p Inst.
  380. /// @param UseLoop The loop @p Inst is used in.
  381. /// @param DefStmt The statement @p Inst is defined in.
  382. /// @param DefLoop The loop which contains @p Inst.
  383. ///
  384. /// @return A ForwardingAction object describing the feasibility and
  385. /// profitability evaluation and the callback carrying-out the value
  386. /// forwarding.
  387. ForwardingAction forwardKnownLoad(ScopStmt *TargetStmt, Instruction *Inst,
  388. ScopStmt *UseStmt, Loop *UseLoop,
  389. ScopStmt *DefStmt, Loop *DefLoop) {
  390. // Cannot do anything without successful known analysis.
  391. if (Known.is_null() || Translator.is_null() ||
  392. MaxOpGuard.hasQuotaExceeded())
  393. return ForwardingAction::notApplicable();
  394. LoadInst *LI = dyn_cast<LoadInst>(Inst);
  395. if (!LI)
  396. return ForwardingAction::notApplicable();
  397. ForwardingDecision OpDecision =
  398. forwardTree(TargetStmt, LI->getPointerOperand(), DefStmt, DefLoop);
  399. switch (OpDecision) {
  400. case FD_CanForwardProfitably:
  401. case FD_CanForwardLeaf:
  402. break;
  403. case FD_CannotForward:
  404. return ForwardingAction::cannotForward();
  405. default:
  406. llvm_unreachable("Shouldn't return this");
  407. }
  408. MemoryAccess *Access = TargetStmt->getArrayAccessOrNULLFor(LI);
  409. if (Access) {
  410. // If the load is already in the statement, no forwarding is necessary.
  411. // However, it might happen that the LoadInst is already present in the
  412. // statement's instruction list. In that case we do as follows:
  413. // - For the evaluation, we can trivially forward it as it is
  414. // benefit of forwarding an already present instruction.
  415. // - For the execution, prepend the instruction (to make it
  416. // available to all instructions following in the instruction list), but
  417. // do not add another MemoryAccess.
  418. auto ExecAction = [this, TargetStmt, LI, Access]() -> bool {
  419. TargetStmt->prependInstruction(LI);
  420. LLVM_DEBUG(
  421. dbgs() << " forwarded known load with preexisting MemoryAccess"
  422. << Access << "\n");
  423. (void)Access;
  424. NumKnownLoadsForwarded++;
  425. TotalKnownLoadsForwarded++;
  426. return true;
  427. };
  428. return ForwardingAction::canForward(
  429. ExecAction, {{LI->getPointerOperand(), DefStmt}}, true);
  430. }
  431. // Allow the following Isl calculations (until we return the
  432. // ForwardingAction, excluding the code inside the lambda that will be
  433. // executed later) to fail.
  434. IslQuotaScope QuotaScope = MaxOpGuard.enter();
  435. // { DomainDef[] -> ValInst[] }
  436. isl::map ExpectedVal = makeValInst(Inst, UseStmt, UseLoop);
  437. assert(!isNormalized(ExpectedVal).is_false() &&
  438. "LoadInsts are always normalized");
  439. // { DomainUse[] -> DomainTarget[] }
  440. isl::map UseToTarget = getDefToTarget(UseStmt, TargetStmt);
  441. // { DomainTarget[] -> ValInst[] }
  442. isl::map TargetExpectedVal = ExpectedVal.apply_domain(UseToTarget);
  443. isl::union_map TranslatedExpectedVal =
  444. isl::union_map(TargetExpectedVal).apply_range(Translator);
  445. // { DomainTarget[] -> Element[] }
  446. isl::union_map Candidates = findSameContentElements(TranslatedExpectedVal);
  447. isl::map SameVal = singleLocation(Candidates, getDomainFor(TargetStmt));
  448. if (SameVal.is_null())
  449. return ForwardingAction::notApplicable();
  450. LLVM_DEBUG(dbgs() << " expected values where " << TargetExpectedVal
  451. << "\n");
  452. LLVM_DEBUG(dbgs() << " candidate elements where " << Candidates
  453. << "\n");
  454. // { ValInst[] }
  455. isl::space ValInstSpace = ExpectedVal.get_space().range();
  456. // After adding a new load to the SCoP, also update the Known content
  457. // about it. The new load will have a known ValInst of
  458. // { [DomainTarget[] -> Value[]] }
  459. // but which -- because it is a copy of it -- has same value as the
  460. // { [DomainDef[] -> Value[]] }
  461. // that it replicates. Instead of cloning the known content of
  462. // [DomainDef[] -> Value[]]
  463. // for DomainTarget[], we add a 'translator' that maps
  464. // [DomainTarget[] -> Value[]] to [DomainDef[] -> Value[]]
  465. // before comparing to the known content.
  466. // TODO: 'Translator' could also be used to map PHINodes to their incoming
  467. // ValInsts.
  468. isl::map LocalTranslator;
  469. if (!ValInstSpace.is_wrapping().is_false()) {
  470. // { DefDomain[] -> Value[] }
  471. isl::map ValInsts = ExpectedVal.range().unwrap();
  472. // { DefDomain[] }
  473. isl::set DefDomain = ValInsts.domain();
  474. // { Value[] }
  475. isl::space ValSpace = ValInstSpace.unwrap().range();
  476. // { Value[] -> Value[] }
  477. isl::map ValToVal =
  478. isl::map::identity(ValSpace.map_from_domain_and_range(ValSpace));
  479. // { DomainDef[] -> DomainTarget[] }
  480. isl::map DefToTarget = getDefToTarget(DefStmt, TargetStmt);
  481. // { [TargetDomain[] -> Value[]] -> [DefDomain[] -> Value] }
  482. LocalTranslator = DefToTarget.reverse().product(ValToVal);
  483. LLVM_DEBUG(dbgs() << " local translator is " << LocalTranslator
  484. << "\n");
  485. if (LocalTranslator.is_null())
  486. return ForwardingAction::notApplicable();
  487. }
  488. auto ExecAction = [this, TargetStmt, LI, SameVal,
  489. LocalTranslator]() -> bool {
  490. TargetStmt->prependInstruction(LI);
  491. MemoryAccess *Access = makeReadArrayAccess(TargetStmt, LI, SameVal);
  492. LLVM_DEBUG(dbgs() << " forwarded known load with new MemoryAccess"
  493. << Access << "\n");
  494. (void)Access;
  495. if (!LocalTranslator.is_null())
  496. Translator = Translator.unite(LocalTranslator);
  497. NumKnownLoadsForwarded++;
  498. TotalKnownLoadsForwarded++;
  499. return true;
  500. };
  501. return ForwardingAction::canForward(
  502. ExecAction, {{LI->getPointerOperand(), DefStmt}}, true);
  503. }
  504. /// Forward a scalar by redirecting the access to an array element that stores
  505. /// the same value.
  506. ///
  507. /// @param TargetStmt The statement the operand tree will be copied to.
  508. /// @param Inst The scalar to forward.
  509. /// @param UseStmt The statement that uses @p Inst.
  510. /// @param UseLoop The loop @p Inst is used in.
  511. /// @param DefStmt The statement @p Inst is defined in.
  512. /// @param DefLoop The loop which contains @p Inst.
  513. ///
  514. /// @return A ForwardingAction object describing the feasibility and
  515. /// profitability evaluation and the callback carrying-out the value
  516. /// forwarding.
  517. ForwardingAction reloadKnownContent(ScopStmt *TargetStmt, Instruction *Inst,
  518. ScopStmt *UseStmt, Loop *UseLoop,
  519. ScopStmt *DefStmt, Loop *DefLoop) {
  520. // Cannot do anything without successful known analysis.
  521. if (Known.is_null() || Translator.is_null() ||
  522. MaxOpGuard.hasQuotaExceeded())
  523. return ForwardingAction::notApplicable();
  524. // Don't spend too much time analyzing whether it can be reloaded.
  525. IslQuotaScope QuotaScope = MaxOpGuard.enter();
  526. // { DomainDef[] -> ValInst[] }
  527. isl::union_map ExpectedVal = makeNormalizedValInst(Inst, UseStmt, UseLoop);
  528. // { DomainUse[] -> DomainTarget[] }
  529. isl::map UseToTarget = getDefToTarget(UseStmt, TargetStmt);
  530. // { DomainTarget[] -> ValInst[] }
  531. isl::union_map TargetExpectedVal = ExpectedVal.apply_domain(UseToTarget);
  532. isl::union_map TranslatedExpectedVal =
  533. TargetExpectedVal.apply_range(Translator);
  534. // { DomainTarget[] -> Element[] }
  535. isl::union_map Candidates = findSameContentElements(TranslatedExpectedVal);
  536. isl::map SameVal = singleLocation(Candidates, getDomainFor(TargetStmt));
  537. simplify(SameVal);
  538. if (SameVal.is_null())
  539. return ForwardingAction::notApplicable();
  540. auto ExecAction = [this, TargetStmt, Inst, SameVal]() {
  541. MemoryAccess *Access = TargetStmt->lookupInputAccessOf(Inst);
  542. if (!Access)
  543. Access = TargetStmt->ensureValueRead(Inst);
  544. Access->setNewAccessRelation(SameVal);
  545. LLVM_DEBUG(dbgs() << " forwarded known content of " << *Inst
  546. << " which is " << SameVal << "\n");
  547. TotalReloads++;
  548. NumReloads++;
  549. return false;
  550. };
  551. return ForwardingAction::canForward(ExecAction, {}, true);
  552. }
  553. /// Forwards a speculatively executable instruction.
  554. ///
  555. /// @param TargetStmt The statement the operand tree will be copied to.
  556. /// @param UseInst The (possibly speculatable) instruction to forward.
  557. /// @param DefStmt The statement @p UseInst is defined in.
  558. /// @param DefLoop The loop which contains @p UseInst.
  559. ///
  560. /// @return A ForwardingAction object describing the feasibility and
  561. /// profitability evaluation and the callback carrying-out the value
  562. /// forwarding.
  563. ForwardingAction forwardSpeculatable(ScopStmt *TargetStmt,
  564. Instruction *UseInst, ScopStmt *DefStmt,
  565. Loop *DefLoop) {
  566. // PHIs, unless synthesizable, are not yet supported.
  567. if (isa<PHINode>(UseInst))
  568. return ForwardingAction::notApplicable();
  569. // Compatible instructions must satisfy the following conditions:
  570. // 1. Idempotent (instruction will be copied, not moved; although its
  571. // original instance might be removed by simplification)
  572. // 2. Not access memory (There might be memory writes between)
  573. // 3. Not cause undefined behaviour (we might copy to a location when the
  574. // original instruction was no executed; this is currently not possible
  575. // because we do not forward PHINodes)
  576. // 4. Not leak memory if executed multiple times (i.e. malloc)
  577. //
  578. // Instruction::mayHaveSideEffects is not sufficient because it considers
  579. // malloc to not have side-effects. llvm::isSafeToSpeculativelyExecute is
  580. // not sufficient because it allows memory accesses.
  581. if (mayBeMemoryDependent(*UseInst))
  582. return ForwardingAction::notApplicable();
  583. SmallVector<ForwardingAction::KeyTy, 4> Depends;
  584. Depends.reserve(UseInst->getNumOperands());
  585. for (Value *OpVal : UseInst->operand_values()) {
  586. ForwardingDecision OpDecision =
  587. forwardTree(TargetStmt, OpVal, DefStmt, DefLoop);
  588. switch (OpDecision) {
  589. case FD_CannotForward:
  590. return ForwardingAction::cannotForward();
  591. case FD_CanForwardLeaf:
  592. case FD_CanForwardProfitably:
  593. Depends.emplace_back(OpVal, DefStmt);
  594. break;
  595. case FD_NotApplicable:
  596. case FD_Unknown:
  597. llvm_unreachable(
  598. "forwardTree should never return FD_NotApplicable/FD_Unknown");
  599. }
  600. }
  601. auto ExecAction = [this, TargetStmt, UseInst]() {
  602. // To ensure the right order, prepend this instruction before its
  603. // operands. This ensures that its operands are inserted before the
  604. // instruction using them.
  605. TargetStmt->prependInstruction(UseInst);
  606. LLVM_DEBUG(dbgs() << " forwarded speculable instruction: " << *UseInst
  607. << "\n");
  608. NumInstructionsCopied++;
  609. TotalInstructionsCopied++;
  610. return true;
  611. };
  612. return ForwardingAction::canForward(ExecAction, Depends, true);
  613. }
  614. /// Determines whether an operand tree can be forwarded and returns
  615. /// instructions how to do so in the form of a ForwardingAction object.
  616. ///
  617. /// @param TargetStmt The statement the operand tree will be copied to.
  618. /// @param UseVal The value (usually an instruction) which is root of an
  619. /// operand tree.
  620. /// @param UseStmt The statement that uses @p UseVal.
  621. /// @param UseLoop The loop @p UseVal is used in.
  622. ///
  623. /// @return A ForwardingAction object describing the feasibility and
  624. /// profitability evaluation and the callback carrying-out the value
  625. /// forwarding.
  626. ForwardingAction forwardTreeImpl(ScopStmt *TargetStmt, Value *UseVal,
  627. ScopStmt *UseStmt, Loop *UseLoop) {
  628. ScopStmt *DefStmt = nullptr;
  629. Loop *DefLoop = nullptr;
  630. // { DefDomain[] -> TargetDomain[] }
  631. isl::map DefToTarget;
  632. VirtualUse VUse = VirtualUse::create(UseStmt, UseLoop, UseVal, true);
  633. switch (VUse.getKind()) {
  634. case VirtualUse::Constant:
  635. case VirtualUse::Block:
  636. case VirtualUse::Hoisted:
  637. // These can be used anywhere without special considerations.
  638. return ForwardingAction::triviallyForwardable(false, UseVal);
  639. case VirtualUse::Synthesizable: {
  640. // Check if the value is synthesizable at the new location as well. This
  641. // might be possible when leaving a loop for which ScalarEvolution is
  642. // unable to derive the exit value for.
  643. // TODO: If there is a LCSSA PHI at the loop exit, use that one.
  644. // If the SCEV contains a SCEVAddRecExpr, we currently depend on that we
  645. // do not forward past its loop header. This would require us to use a
  646. // previous loop induction variable instead the current one. We currently
  647. // do not allow forwarding PHI nodes, thus this should never occur (the
  648. // only exception where no phi is necessary being an unreachable loop
  649. // without edge from the outside).
  650. VirtualUse TargetUse = VirtualUse::create(
  651. S, TargetStmt, TargetStmt->getSurroundingLoop(), UseVal, true);
  652. if (TargetUse.getKind() == VirtualUse::Synthesizable)
  653. return ForwardingAction::triviallyForwardable(false, UseVal);
  654. LLVM_DEBUG(
  655. dbgs() << " Synthesizable would not be synthesizable anymore: "
  656. << *UseVal << "\n");
  657. return ForwardingAction::cannotForward();
  658. }
  659. case VirtualUse::ReadOnly: {
  660. if (!ModelReadOnlyScalars)
  661. return ForwardingAction::triviallyForwardable(false, UseVal);
  662. // If we model read-only scalars, we need to create a MemoryAccess for it.
  663. auto ExecAction = [this, TargetStmt, UseVal]() {
  664. TargetStmt->ensureValueRead(UseVal);
  665. LLVM_DEBUG(dbgs() << " forwarded read-only value " << *UseVal
  666. << "\n");
  667. NumReadOnlyCopied++;
  668. TotalReadOnlyCopied++;
  669. // Note that we cannot return true here. With a operand tree
  670. // depth of 0, UseVal is the use in TargetStmt that we try to replace.
  671. // With -polly-analyze-read-only-scalars=true we would ensure the
  672. // existence of a MemoryAccess (which already exists for a leaf) and be
  673. // removed again by tryForwardTree because it's goal is to remove this
  674. // scalar MemoryAccess. It interprets FD_CanForwardTree as the
  675. // permission to do so.
  676. return false;
  677. };
  678. return ForwardingAction::canForward(ExecAction, {}, false);
  679. }
  680. case VirtualUse::Intra:
  681. // Knowing that UseStmt and DefStmt are the same statement instance, just
  682. // reuse the information about UseStmt for DefStmt
  683. DefStmt = UseStmt;
  684. LLVM_FALLTHROUGH;
  685. case VirtualUse::Inter:
  686. Instruction *Inst = cast<Instruction>(UseVal);
  687. if (!DefStmt) {
  688. DefStmt = S->getStmtFor(Inst);
  689. if (!DefStmt)
  690. return ForwardingAction::cannotForward();
  691. }
  692. DefLoop = LI->getLoopFor(Inst->getParent());
  693. ForwardingAction SpeculativeResult =
  694. forwardSpeculatable(TargetStmt, Inst, DefStmt, DefLoop);
  695. if (SpeculativeResult.Decision != FD_NotApplicable)
  696. return SpeculativeResult;
  697. ForwardingAction KnownResult = forwardKnownLoad(
  698. TargetStmt, Inst, UseStmt, UseLoop, DefStmt, DefLoop);
  699. if (KnownResult.Decision != FD_NotApplicable)
  700. return KnownResult;
  701. ForwardingAction ReloadResult = reloadKnownContent(
  702. TargetStmt, Inst, UseStmt, UseLoop, DefStmt, DefLoop);
  703. if (ReloadResult.Decision != FD_NotApplicable)
  704. return ReloadResult;
  705. // When no method is found to forward the operand tree, we effectively
  706. // cannot handle it.
  707. LLVM_DEBUG(dbgs() << " Cannot forward instruction: " << *Inst << "\n");
  708. return ForwardingAction::cannotForward();
  709. }
  710. llvm_unreachable("Case unhandled");
  711. }
  712. /// Determines whether an operand tree can be forwarded. Previous evaluations
  713. /// are cached.
  714. ///
  715. /// @param TargetStmt The statement the operand tree will be copied to.
  716. /// @param UseVal The value (usually an instruction) which is root of an
  717. /// operand tree.
  718. /// @param UseStmt The statement that uses @p UseVal.
  719. /// @param UseLoop The loop @p UseVal is used in.
  720. ///
  721. /// @return FD_CannotForward if @p UseVal cannot be forwarded.
  722. /// FD_CanForwardLeaf if @p UseVal is forwardable, but not
  723. /// profitable.
  724. /// FD_CanForwardProfitably if @p UseVal is forwardable and useful to
  725. /// do.
  726. ForwardingDecision forwardTree(ScopStmt *TargetStmt, Value *UseVal,
  727. ScopStmt *UseStmt, Loop *UseLoop) {
  728. // Lookup any cached evaluation.
  729. auto It = ForwardingActions.find({UseVal, UseStmt});
  730. if (It != ForwardingActions.end())
  731. return It->second.Decision;
  732. // Make a new evaluation.
  733. ForwardingAction Action =
  734. forwardTreeImpl(TargetStmt, UseVal, UseStmt, UseLoop);
  735. ForwardingDecision Result = Action.Decision;
  736. // Remember for the next time.
  737. assert(!ForwardingActions.count({UseVal, UseStmt}) &&
  738. "circular dependency?");
  739. ForwardingActions.insert({{UseVal, UseStmt}, std::move(Action)});
  740. return Result;
  741. }
  742. /// Forward an operand tree using cached actions.
  743. ///
  744. /// @param Stmt Statement the operand tree is moved into.
  745. /// @param UseVal Root of the operand tree within @p Stmt.
  746. /// @param RA The MemoryAccess for @p UseVal that the forwarding intends
  747. /// to remove.
  748. void applyForwardingActions(ScopStmt *Stmt, Value *UseVal, MemoryAccess *RA) {
  749. using ChildItTy =
  750. decltype(std::declval<ForwardingAction>().Depends.begin());
  751. using EdgeTy = std::pair<ForwardingAction *, ChildItTy>;
  752. DenseSet<ForwardingAction::KeyTy> Visited;
  753. SmallVector<EdgeTy, 32> Stack;
  754. SmallVector<ForwardingAction *, 32> Ordered;
  755. // Seed the tree search using the root value.
  756. assert(ForwardingActions.count({UseVal, Stmt}));
  757. ForwardingAction *RootAction = &ForwardingActions[{UseVal, Stmt}];
  758. Stack.emplace_back(RootAction, RootAction->Depends.begin());
  759. // Compute the postorder of the operand tree: all operands of an instruction
  760. // must be visited before the instruction itself. As an additional
  761. // requirement, the topological ordering must be 'compact': Any subtree node
  762. // must not be interleaved with nodes from a non-shared subtree. This is
  763. // because the same llvm::Instruction can be materialized multiple times as
  764. // used at different ScopStmts which might be different values. Intersecting
  765. // these lifetimes may result in miscompilations.
  766. // FIXME: Intersecting lifetimes might still be possible for the roots
  767. // themselves, since instructions are just prepended to a ScopStmt's
  768. // instruction list.
  769. while (!Stack.empty()) {
  770. EdgeTy &Top = Stack.back();
  771. ForwardingAction *TopAction = Top.first;
  772. ChildItTy &TopEdge = Top.second;
  773. if (TopEdge == TopAction->Depends.end()) {
  774. // Postorder sorting
  775. Ordered.push_back(TopAction);
  776. Stack.pop_back();
  777. continue;
  778. }
  779. ForwardingAction::KeyTy Key = *TopEdge;
  780. // Next edge for this level
  781. ++TopEdge;
  782. auto VisitIt = Visited.insert(Key);
  783. if (!VisitIt.second)
  784. continue;
  785. assert(ForwardingActions.count(Key) &&
  786. "Must not insert new actions during execution phase");
  787. ForwardingAction *ChildAction = &ForwardingActions[Key];
  788. Stack.emplace_back(ChildAction, ChildAction->Depends.begin());
  789. }
  790. // Actually, we need the reverse postorder because actions prepend new
  791. // instructions. Therefore, the first one will always be the action for the
  792. // operand tree's root.
  793. assert(Ordered.back() == RootAction);
  794. if (RootAction->Execute())
  795. Stmt->removeSingleMemoryAccess(RA);
  796. Ordered.pop_back();
  797. for (auto DepAction : reverse(Ordered)) {
  798. assert(DepAction->Decision != FD_Unknown &&
  799. DepAction->Decision != FD_CannotForward);
  800. assert(DepAction != RootAction);
  801. DepAction->Execute();
  802. }
  803. }
  804. /// Try to forward an operand tree rooted in @p RA.
  805. bool tryForwardTree(MemoryAccess *RA) {
  806. assert(RA->isLatestScalarKind());
  807. LLVM_DEBUG(dbgs() << "Trying to forward operand tree " << RA << "...\n");
  808. ScopStmt *Stmt = RA->getStatement();
  809. Loop *InLoop = Stmt->getSurroundingLoop();
  810. isl::map TargetToUse;
  811. if (!Known.is_null()) {
  812. isl::space DomSpace = Stmt->getDomainSpace();
  813. TargetToUse =
  814. isl::map::identity(DomSpace.map_from_domain_and_range(DomSpace));
  815. }
  816. ForwardingDecision Assessment =
  817. forwardTree(Stmt, RA->getAccessValue(), Stmt, InLoop);
  818. // If considered feasible and profitable, forward it.
  819. bool Changed = false;
  820. if (Assessment == FD_CanForwardProfitably) {
  821. applyForwardingActions(Stmt, RA->getAccessValue(), RA);
  822. Changed = true;
  823. }
  824. ForwardingActions.clear();
  825. return Changed;
  826. }
  827. /// Return which SCoP this instance is processing.
  828. Scop *getScop() const { return S; }
  829. /// Run the algorithm: Use value read accesses as operand tree roots and try
  830. /// to forward them into the statement.
  831. bool forwardOperandTrees() {
  832. for (ScopStmt &Stmt : *S) {
  833. bool StmtModified = false;
  834. // Because we are modifying the MemoryAccess list, collect them first to
  835. // avoid iterator invalidation.
  836. SmallVector<MemoryAccess *, 16> Accs(Stmt.begin(), Stmt.end());
  837. for (MemoryAccess *RA : Accs) {
  838. if (!RA->isRead())
  839. continue;
  840. if (!RA->isLatestScalarKind())
  841. continue;
  842. if (tryForwardTree(RA)) {
  843. Modified = true;
  844. StmtModified = true;
  845. NumForwardedTrees++;
  846. TotalForwardedTrees++;
  847. }
  848. }
  849. if (StmtModified) {
  850. NumModifiedStmts++;
  851. TotalModifiedStmts++;
  852. }
  853. }
  854. if (Modified) {
  855. ScopsModified++;
  856. S->realignParams();
  857. }
  858. return Modified;
  859. }
  860. /// Print the pass result, performed transformations and the SCoP after the
  861. /// transformation.
  862. void print(raw_ostream &OS, int Indent = 0) {
  863. printStatistics(OS, Indent);
  864. if (!Modified) {
  865. // This line can easily be checked in regression tests.
  866. OS << "ForwardOpTree executed, but did not modify anything\n";
  867. return;
  868. }
  869. printStatements(OS, Indent);
  870. }
  871. bool isModified() const { return Modified; }
  872. };
  873. static std::unique_ptr<ForwardOpTreeImpl> runForwardOpTree(Scop &S,
  874. LoopInfo &LI) {
  875. std::unique_ptr<ForwardOpTreeImpl> Impl;
  876. {
  877. IslMaxOperationsGuard MaxOpGuard(S.getIslCtx().get(), MaxOps, false);
  878. Impl = std::make_unique<ForwardOpTreeImpl>(&S, &LI, MaxOpGuard);
  879. if (AnalyzeKnown) {
  880. LLVM_DEBUG(dbgs() << "Prepare forwarders...\n");
  881. Impl->computeKnownValues();
  882. }
  883. LLVM_DEBUG(dbgs() << "Forwarding operand trees...\n");
  884. Impl->forwardOperandTrees();
  885. if (MaxOpGuard.hasQuotaExceeded()) {
  886. LLVM_DEBUG(dbgs() << "Not all operations completed because of "
  887. "max_operations exceeded\n");
  888. KnownOutOfQuota++;
  889. }
  890. }
  891. LLVM_DEBUG(dbgs() << "\nFinal Scop:\n");
  892. LLVM_DEBUG(dbgs() << S);
  893. // Update statistics
  894. Scop::ScopStatistics ScopStats = S.getStatistics();
  895. NumValueWrites += ScopStats.NumValueWrites;
  896. NumValueWritesInLoops += ScopStats.NumValueWritesInLoops;
  897. NumPHIWrites += ScopStats.NumPHIWrites;
  898. NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops;
  899. NumSingletonWrites += ScopStats.NumSingletonWrites;
  900. NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops;
  901. return Impl;
  902. }
  903. static PreservedAnalyses
  904. runForwardOpTreeUsingNPM(Scop &S, ScopAnalysisManager &SAM,
  905. ScopStandardAnalysisResults &SAR, SPMUpdater &U,
  906. raw_ostream *OS) {
  907. LoopInfo &LI = SAR.LI;
  908. std::unique_ptr<ForwardOpTreeImpl> Impl = runForwardOpTree(S, LI);
  909. if (OS) {
  910. *OS << "Printing analysis 'Polly - Forward operand tree' for region: '"
  911. << S.getName() << "' in function '" << S.getFunction().getName()
  912. << "':\n";
  913. if (Impl) {
  914. assert(Impl->getScop() == &S);
  915. Impl->print(*OS);
  916. }
  917. }
  918. if (!Impl->isModified())
  919. return PreservedAnalyses::all();
  920. PreservedAnalyses PA;
  921. PA.preserveSet<AllAnalysesOn<Module>>();
  922. PA.preserveSet<AllAnalysesOn<Function>>();
  923. PA.preserveSet<AllAnalysesOn<Loop>>();
  924. return PA;
  925. }
  926. /// Pass that redirects scalar reads to array elements that are known to contain
  927. /// the same value.
  928. ///
  929. /// This reduces the number of scalar accesses and therefore potentially
  930. /// increases the freedom of the scheduler. In the ideal case, all reads of a
  931. /// scalar definition are redirected (We currently do not care about removing
  932. /// the write in this case). This is also useful for the main DeLICM pass as
  933. /// there are less scalars to be mapped.
  934. class ForwardOpTreeWrapperPass : public ScopPass {
  935. private:
  936. /// The pass implementation, also holding per-scop data.
  937. std::unique_ptr<ForwardOpTreeImpl> Impl;
  938. public:
  939. static char ID;
  940. explicit ForwardOpTreeWrapperPass() : ScopPass(ID) {}
  941. ForwardOpTreeWrapperPass(const ForwardOpTreeWrapperPass &) = delete;
  942. ForwardOpTreeWrapperPass &
  943. operator=(const ForwardOpTreeWrapperPass &) = delete;
  944. void getAnalysisUsage(AnalysisUsage &AU) const override {
  945. AU.addRequiredTransitive<ScopInfoRegionPass>();
  946. AU.addRequired<LoopInfoWrapperPass>();
  947. AU.setPreservesAll();
  948. }
  949. bool runOnScop(Scop &S) override {
  950. // Free resources for previous SCoP's computation, if not yet done.
  951. releaseMemory();
  952. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  953. Impl = runForwardOpTree(S, LI);
  954. return false;
  955. }
  956. void printScop(raw_ostream &OS, Scop &S) const override {
  957. if (!Impl)
  958. return;
  959. assert(Impl->getScop() == &S);
  960. Impl->print(OS);
  961. }
  962. void releaseMemory() override { Impl.reset(); }
  963. }; // class ForwardOpTree
  964. char ForwardOpTreeWrapperPass::ID;
  965. } // namespace
  966. Pass *polly::createForwardOpTreeWrapperPass() {
  967. return new ForwardOpTreeWrapperPass();
  968. }
  969. INITIALIZE_PASS_BEGIN(ForwardOpTreeWrapperPass, "polly-optree",
  970. "Polly - Forward operand tree", false, false)
  971. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  972. INITIALIZE_PASS_END(ForwardOpTreeWrapperPass, "polly-optree",
  973. "Polly - Forward operand tree", false, false)
  974. llvm::PreservedAnalyses ForwardOpTreePass::run(Scop &S,
  975. ScopAnalysisManager &SAM,
  976. ScopStandardAnalysisResults &SAR,
  977. SPMUpdater &U) {
  978. return runForwardOpTreeUsingNPM(S, SAM, SAR, U, nullptr);
  979. }
  980. llvm::PreservedAnalyses
  981. ForwardOpTreePrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
  982. ScopStandardAnalysisResults &SAR, SPMUpdater &U) {
  983. return runForwardOpTreeUsingNPM(S, SAM, SAR, U, &OS);
  984. }