LoopVersioningLICM.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. //===- LoopVersioningLICM.cpp - LICM Loop Versioning ----------------------===//
  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. // When alias analysis is uncertain about the aliasing between any two accesses,
  10. // it will return MayAlias. This uncertainty from alias analysis restricts LICM
  11. // from proceeding further. In cases where alias analysis is uncertain we might
  12. // use loop versioning as an alternative.
  13. //
  14. // Loop Versioning will create a version of the loop with aggressive aliasing
  15. // assumptions in addition to the original with conservative (default) aliasing
  16. // assumptions. The version of the loop making aggressive aliasing assumptions
  17. // will have all the memory accesses marked as no-alias. These two versions of
  18. // loop will be preceded by a memory runtime check. This runtime check consists
  19. // of bound checks for all unique memory accessed in loop, and it ensures the
  20. // lack of memory aliasing. The result of the runtime check determines which of
  21. // the loop versions is executed: If the runtime check detects any memory
  22. // aliasing, then the original loop is executed. Otherwise, the version with
  23. // aggressive aliasing assumptions is used.
  24. //
  25. // Following are the top level steps:
  26. //
  27. // a) Perform LoopVersioningLICM's feasibility check.
  28. // b) If loop is a candidate for versioning then create a memory bound check,
  29. // by considering all the memory accesses in loop body.
  30. // c) Clone original loop and set all memory accesses as no-alias in new loop.
  31. // d) Set original loop & versioned loop as a branch target of the runtime check
  32. // result.
  33. //
  34. // It transforms loop as shown below:
  35. //
  36. // +----------------+
  37. // |Runtime Memcheck|
  38. // +----------------+
  39. // |
  40. // +----------+----------------+----------+
  41. // | |
  42. // +---------+----------+ +-----------+----------+
  43. // |Orig Loop Preheader | |Cloned Loop Preheader |
  44. // +--------------------+ +----------------------+
  45. // | |
  46. // +--------------------+ +----------------------+
  47. // |Orig Loop Body | |Cloned Loop Body |
  48. // +--------------------+ +----------------------+
  49. // | |
  50. // +--------------------+ +----------------------+
  51. // |Orig Loop Exit Block| |Cloned Loop Exit Block|
  52. // +--------------------+ +-----------+----------+
  53. // | |
  54. // +----------+--------------+-----------+
  55. // |
  56. // +-----+----+
  57. // |Join Block|
  58. // +----------+
  59. //
  60. //===----------------------------------------------------------------------===//
  61. #include "llvm/Transforms/Scalar/LoopVersioningLICM.h"
  62. #include "llvm/ADT/SmallVector.h"
  63. #include "llvm/ADT/StringRef.h"
  64. #include "llvm/Analysis/AliasAnalysis.h"
  65. #include "llvm/Analysis/AliasSetTracker.h"
  66. #include "llvm/Analysis/GlobalsModRef.h"
  67. #include "llvm/Analysis/LoopAccessAnalysis.h"
  68. #include "llvm/Analysis/LoopInfo.h"
  69. #include "llvm/Analysis/LoopPass.h"
  70. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  71. #include "llvm/Analysis/ScalarEvolution.h"
  72. #include "llvm/IR/Constants.h"
  73. #include "llvm/IR/Dominators.h"
  74. #include "llvm/IR/Instruction.h"
  75. #include "llvm/IR/Instructions.h"
  76. #include "llvm/IR/LLVMContext.h"
  77. #include "llvm/IR/MDBuilder.h"
  78. #include "llvm/IR/Metadata.h"
  79. #include "llvm/IR/Type.h"
  80. #include "llvm/IR/Value.h"
  81. #include "llvm/InitializePasses.h"
  82. #include "llvm/Pass.h"
  83. #include "llvm/Support/Casting.h"
  84. #include "llvm/Support/CommandLine.h"
  85. #include "llvm/Support/Debug.h"
  86. #include "llvm/Support/raw_ostream.h"
  87. #include "llvm/Transforms/Scalar.h"
  88. #include "llvm/Transforms/Utils.h"
  89. #include "llvm/Transforms/Utils/LoopUtils.h"
  90. #include "llvm/Transforms/Utils/LoopVersioning.h"
  91. #include <cassert>
  92. #include <memory>
  93. using namespace llvm;
  94. #define DEBUG_TYPE "loop-versioning-licm"
  95. static const char *LICMVersioningMetaData = "llvm.loop.licm_versioning.disable";
  96. /// Threshold minimum allowed percentage for possible
  97. /// invariant instructions in a loop.
  98. static cl::opt<float>
  99. LVInvarThreshold("licm-versioning-invariant-threshold",
  100. cl::desc("LoopVersioningLICM's minimum allowed percentage"
  101. "of possible invariant instructions per loop"),
  102. cl::init(25), cl::Hidden);
  103. /// Threshold for maximum allowed loop nest/depth
  104. static cl::opt<unsigned> LVLoopDepthThreshold(
  105. "licm-versioning-max-depth-threshold",
  106. cl::desc(
  107. "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"),
  108. cl::init(2), cl::Hidden);
  109. namespace {
  110. struct LoopVersioningLICMLegacyPass : public LoopPass {
  111. static char ID;
  112. LoopVersioningLICMLegacyPass() : LoopPass(ID) {
  113. initializeLoopVersioningLICMLegacyPassPass(
  114. *PassRegistry::getPassRegistry());
  115. }
  116. bool runOnLoop(Loop *L, LPPassManager &LPM) override;
  117. StringRef getPassName() const override { return "Loop Versioning for LICM"; }
  118. void getAnalysisUsage(AnalysisUsage &AU) const override {
  119. AU.setPreservesCFG();
  120. AU.addRequired<AAResultsWrapperPass>();
  121. AU.addRequired<DominatorTreeWrapperPass>();
  122. AU.addRequiredID(LCSSAID);
  123. AU.addRequired<LoopAccessLegacyAnalysis>();
  124. AU.addRequired<LoopInfoWrapperPass>();
  125. AU.addRequiredID(LoopSimplifyID);
  126. AU.addRequired<ScalarEvolutionWrapperPass>();
  127. AU.addPreserved<AAResultsWrapperPass>();
  128. AU.addPreserved<GlobalsAAWrapperPass>();
  129. AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
  130. }
  131. };
  132. struct LoopVersioningLICM {
  133. // We don't explicitly pass in LoopAccessInfo to the constructor since the
  134. // loop versioning might return early due to instructions that are not safe
  135. // for versioning. By passing the proxy instead the construction of
  136. // LoopAccessInfo will take place only when it's necessary.
  137. LoopVersioningLICM(AliasAnalysis *AA, ScalarEvolution *SE,
  138. OptimizationRemarkEmitter *ORE,
  139. function_ref<const LoopAccessInfo &(Loop *)> GetLAI)
  140. : AA(AA), SE(SE), GetLAI(GetLAI),
  141. LoopDepthThreshold(LVLoopDepthThreshold),
  142. InvariantThreshold(LVInvarThreshold), ORE(ORE) {}
  143. bool runOnLoop(Loop *L, LoopInfo *LI, DominatorTree *DT);
  144. void reset() {
  145. AA = nullptr;
  146. SE = nullptr;
  147. CurLoop = nullptr;
  148. LoadAndStoreCounter = 0;
  149. InvariantCounter = 0;
  150. IsReadOnlyLoop = true;
  151. ORE = nullptr;
  152. CurAST.reset();
  153. }
  154. class AutoResetter {
  155. public:
  156. AutoResetter(LoopVersioningLICM &LVLICM) : LVLICM(LVLICM) {}
  157. ~AutoResetter() { LVLICM.reset(); }
  158. private:
  159. LoopVersioningLICM &LVLICM;
  160. };
  161. private:
  162. // Current AliasAnalysis information
  163. AliasAnalysis *AA = nullptr;
  164. // Current ScalarEvolution
  165. ScalarEvolution *SE = nullptr;
  166. // Current Loop's LoopAccessInfo
  167. const LoopAccessInfo *LAI = nullptr;
  168. // Proxy for retrieving LoopAccessInfo.
  169. function_ref<const LoopAccessInfo &(Loop *)> GetLAI;
  170. // The current loop we are working on.
  171. Loop *CurLoop = nullptr;
  172. // AliasSet information for the current loop.
  173. std::unique_ptr<AliasSetTracker> CurAST;
  174. // Maximum loop nest threshold
  175. unsigned LoopDepthThreshold;
  176. // Minimum invariant threshold
  177. float InvariantThreshold;
  178. // Counter to track num of load & store
  179. unsigned LoadAndStoreCounter = 0;
  180. // Counter to track num of invariant
  181. unsigned InvariantCounter = 0;
  182. // Read only loop marker.
  183. bool IsReadOnlyLoop = true;
  184. // OptimizationRemarkEmitter
  185. OptimizationRemarkEmitter *ORE;
  186. bool isLegalForVersioning();
  187. bool legalLoopStructure();
  188. bool legalLoopInstructions();
  189. bool legalLoopMemoryAccesses();
  190. bool isLoopAlreadyVisited();
  191. void setNoAliasToLoop(Loop *VerLoop);
  192. bool instructionSafeForVersioning(Instruction *I);
  193. };
  194. } // end anonymous namespace
  195. /// Check loop structure and confirms it's good for LoopVersioningLICM.
  196. bool LoopVersioningLICM::legalLoopStructure() {
  197. // Loop must be in loop simplify form.
  198. if (!CurLoop->isLoopSimplifyForm()) {
  199. LLVM_DEBUG(dbgs() << " loop is not in loop-simplify form.\n");
  200. return false;
  201. }
  202. // Loop should be innermost loop, if not return false.
  203. if (!CurLoop->getSubLoops().empty()) {
  204. LLVM_DEBUG(dbgs() << " loop is not innermost\n");
  205. return false;
  206. }
  207. // Loop should have a single backedge, if not return false.
  208. if (CurLoop->getNumBackEdges() != 1) {
  209. LLVM_DEBUG(dbgs() << " loop has multiple backedges\n");
  210. return false;
  211. }
  212. // Loop must have a single exiting block, if not return false.
  213. if (!CurLoop->getExitingBlock()) {
  214. LLVM_DEBUG(dbgs() << " loop has multiple exiting block\n");
  215. return false;
  216. }
  217. // We only handle bottom-tested loop, i.e. loop in which the condition is
  218. // checked at the end of each iteration. With that we can assume that all
  219. // instructions in the loop are executed the same number of times.
  220. if (CurLoop->getExitingBlock() != CurLoop->getLoopLatch()) {
  221. LLVM_DEBUG(dbgs() << " loop is not bottom tested\n");
  222. return false;
  223. }
  224. // Parallel loops must not have aliasing loop-invariant memory accesses.
  225. // Hence we don't need to version anything in this case.
  226. if (CurLoop->isAnnotatedParallel()) {
  227. LLVM_DEBUG(dbgs() << " Parallel loop is not worth versioning\n");
  228. return false;
  229. }
  230. // Loop depth more then LoopDepthThreshold are not allowed
  231. if (CurLoop->getLoopDepth() > LoopDepthThreshold) {
  232. LLVM_DEBUG(dbgs() << " loop depth is more then threshold\n");
  233. return false;
  234. }
  235. // We need to be able to compute the loop trip count in order
  236. // to generate the bound checks.
  237. const SCEV *ExitCount = SE->getBackedgeTakenCount(CurLoop);
  238. if (isa<SCEVCouldNotCompute>(ExitCount)) {
  239. LLVM_DEBUG(dbgs() << " loop does not has trip count\n");
  240. return false;
  241. }
  242. return true;
  243. }
  244. /// Check memory accesses in loop and confirms it's good for
  245. /// LoopVersioningLICM.
  246. bool LoopVersioningLICM::legalLoopMemoryAccesses() {
  247. bool HasMayAlias = false;
  248. bool TypeSafety = false;
  249. bool HasMod = false;
  250. // Memory check:
  251. // Transform phase will generate a versioned loop and also a runtime check to
  252. // ensure the pointers are independent and they don’t alias.
  253. // In version variant of loop, alias meta data asserts that all access are
  254. // mutually independent.
  255. //
  256. // Pointers aliasing in alias domain are avoided because with multiple
  257. // aliasing domains we may not be able to hoist potential loop invariant
  258. // access out of the loop.
  259. //
  260. // Iterate over alias tracker sets, and confirm AliasSets doesn't have any
  261. // must alias set.
  262. for (const auto &I : *CurAST) {
  263. const AliasSet &AS = I;
  264. // Skip Forward Alias Sets, as this should be ignored as part of
  265. // the AliasSetTracker object.
  266. if (AS.isForwardingAliasSet())
  267. continue;
  268. // With MustAlias its not worth adding runtime bound check.
  269. if (AS.isMustAlias())
  270. return false;
  271. Value *SomePtr = AS.begin()->getValue();
  272. bool TypeCheck = true;
  273. // Check for Mod & MayAlias
  274. HasMayAlias |= AS.isMayAlias();
  275. HasMod |= AS.isMod();
  276. for (const auto &A : AS) {
  277. Value *Ptr = A.getValue();
  278. // Alias tracker should have pointers of same data type.
  279. TypeCheck = (TypeCheck && (SomePtr->getType() == Ptr->getType()));
  280. }
  281. // At least one alias tracker should have pointers of same data type.
  282. TypeSafety |= TypeCheck;
  283. }
  284. // Ensure types should be of same type.
  285. if (!TypeSafety) {
  286. LLVM_DEBUG(dbgs() << " Alias tracker type safety failed!\n");
  287. return false;
  288. }
  289. // Ensure loop body shouldn't be read only.
  290. if (!HasMod) {
  291. LLVM_DEBUG(dbgs() << " No memory modified in loop body\n");
  292. return false;
  293. }
  294. // Make sure alias set has may alias case.
  295. // If there no alias memory ambiguity, return false.
  296. if (!HasMayAlias) {
  297. LLVM_DEBUG(dbgs() << " No ambiguity in memory access.\n");
  298. return false;
  299. }
  300. return true;
  301. }
  302. /// Check loop instructions safe for Loop versioning.
  303. /// It returns true if it's safe else returns false.
  304. /// Consider following:
  305. /// 1) Check all load store in loop body are non atomic & non volatile.
  306. /// 2) Check function call safety, by ensuring its not accessing memory.
  307. /// 3) Loop body shouldn't have any may throw instruction.
  308. /// 4) Loop body shouldn't have any convergent or noduplicate instructions.
  309. bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) {
  310. assert(I != nullptr && "Null instruction found!");
  311. // Check function call safety
  312. if (auto *Call = dyn_cast<CallBase>(I)) {
  313. if (Call->isConvergent() || Call->cannotDuplicate()) {
  314. LLVM_DEBUG(dbgs() << " Convergent call site found.\n");
  315. return false;
  316. }
  317. if (!AA->doesNotAccessMemory(Call)) {
  318. LLVM_DEBUG(dbgs() << " Unsafe call site found.\n");
  319. return false;
  320. }
  321. }
  322. // Avoid loops with possiblity of throw
  323. if (I->mayThrow()) {
  324. LLVM_DEBUG(dbgs() << " May throw instruction found in loop body\n");
  325. return false;
  326. }
  327. // If current instruction is load instructions
  328. // make sure it's a simple load (non atomic & non volatile)
  329. if (I->mayReadFromMemory()) {
  330. LoadInst *Ld = dyn_cast<LoadInst>(I);
  331. if (!Ld || !Ld->isSimple()) {
  332. LLVM_DEBUG(dbgs() << " Found a non-simple load.\n");
  333. return false;
  334. }
  335. LoadAndStoreCounter++;
  336. Value *Ptr = Ld->getPointerOperand();
  337. // Check loop invariant.
  338. if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop))
  339. InvariantCounter++;
  340. }
  341. // If current instruction is store instruction
  342. // make sure it's a simple store (non atomic & non volatile)
  343. else if (I->mayWriteToMemory()) {
  344. StoreInst *St = dyn_cast<StoreInst>(I);
  345. if (!St || !St->isSimple()) {
  346. LLVM_DEBUG(dbgs() << " Found a non-simple store.\n");
  347. return false;
  348. }
  349. LoadAndStoreCounter++;
  350. Value *Ptr = St->getPointerOperand();
  351. // Check loop invariant.
  352. if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop))
  353. InvariantCounter++;
  354. IsReadOnlyLoop = false;
  355. }
  356. return true;
  357. }
  358. /// Check loop instructions and confirms it's good for
  359. /// LoopVersioningLICM.
  360. bool LoopVersioningLICM::legalLoopInstructions() {
  361. // Resetting counters.
  362. LoadAndStoreCounter = 0;
  363. InvariantCounter = 0;
  364. IsReadOnlyLoop = true;
  365. using namespace ore;
  366. // Iterate over loop blocks and instructions of each block and check
  367. // instruction safety.
  368. for (auto *Block : CurLoop->getBlocks())
  369. for (auto &Inst : *Block) {
  370. // If instruction is unsafe just return false.
  371. if (!instructionSafeForVersioning(&Inst)) {
  372. ORE->emit([&]() {
  373. return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopInst", &Inst)
  374. << " Unsafe Loop Instruction";
  375. });
  376. return false;
  377. }
  378. }
  379. // Get LoopAccessInfo from current loop via the proxy.
  380. LAI = &GetLAI(CurLoop);
  381. // Check LoopAccessInfo for need of runtime check.
  382. if (LAI->getRuntimePointerChecking()->getChecks().empty()) {
  383. LLVM_DEBUG(dbgs() << " LAA: Runtime check not found !!\n");
  384. return false;
  385. }
  386. // Number of runtime-checks should be less then RuntimeMemoryCheckThreshold
  387. if (LAI->getNumRuntimePointerChecks() >
  388. VectorizerParams::RuntimeMemoryCheckThreshold) {
  389. LLVM_DEBUG(
  390. dbgs() << " LAA: Runtime checks are more than threshold !!\n");
  391. ORE->emit([&]() {
  392. return OptimizationRemarkMissed(DEBUG_TYPE, "RuntimeCheck",
  393. CurLoop->getStartLoc(),
  394. CurLoop->getHeader())
  395. << "Number of runtime checks "
  396. << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks())
  397. << " exceeds threshold "
  398. << NV("Threshold", VectorizerParams::RuntimeMemoryCheckThreshold);
  399. });
  400. return false;
  401. }
  402. // Loop should have at least one invariant load or store instruction.
  403. if (!InvariantCounter) {
  404. LLVM_DEBUG(dbgs() << " Invariant not found !!\n");
  405. return false;
  406. }
  407. // Read only loop not allowed.
  408. if (IsReadOnlyLoop) {
  409. LLVM_DEBUG(dbgs() << " Found a read-only loop!\n");
  410. return false;
  411. }
  412. // Profitablity check:
  413. // Check invariant threshold, should be in limit.
  414. if (InvariantCounter * 100 < InvariantThreshold * LoadAndStoreCounter) {
  415. LLVM_DEBUG(
  416. dbgs()
  417. << " Invariant load & store are less then defined threshold\n");
  418. LLVM_DEBUG(dbgs() << " Invariant loads & stores: "
  419. << ((InvariantCounter * 100) / LoadAndStoreCounter)
  420. << "%\n");
  421. LLVM_DEBUG(dbgs() << " Invariant loads & store threshold: "
  422. << InvariantThreshold << "%\n");
  423. ORE->emit([&]() {
  424. return OptimizationRemarkMissed(DEBUG_TYPE, "InvariantThreshold",
  425. CurLoop->getStartLoc(),
  426. CurLoop->getHeader())
  427. << "Invariant load & store "
  428. << NV("LoadAndStoreCounter",
  429. ((InvariantCounter * 100) / LoadAndStoreCounter))
  430. << " are less then defined threshold "
  431. << NV("Threshold", InvariantThreshold);
  432. });
  433. return false;
  434. }
  435. return true;
  436. }
  437. /// It checks loop is already visited or not.
  438. /// check loop meta data, if loop revisited return true
  439. /// else false.
  440. bool LoopVersioningLICM::isLoopAlreadyVisited() {
  441. // Check LoopVersioningLICM metadata into loop
  442. if (findStringMetadataForLoop(CurLoop, LICMVersioningMetaData)) {
  443. return true;
  444. }
  445. return false;
  446. }
  447. /// Checks legality for LoopVersioningLICM by considering following:
  448. /// a) loop structure legality b) loop instruction legality
  449. /// c) loop memory access legality.
  450. /// Return true if legal else returns false.
  451. bool LoopVersioningLICM::isLegalForVersioning() {
  452. using namespace ore;
  453. LLVM_DEBUG(dbgs() << "Loop: " << *CurLoop);
  454. // Make sure not re-visiting same loop again.
  455. if (isLoopAlreadyVisited()) {
  456. LLVM_DEBUG(
  457. dbgs() << " Revisiting loop in LoopVersioningLICM not allowed.\n\n");
  458. return false;
  459. }
  460. // Check loop structure leagality.
  461. if (!legalLoopStructure()) {
  462. LLVM_DEBUG(
  463. dbgs() << " Loop structure not suitable for LoopVersioningLICM\n\n");
  464. ORE->emit([&]() {
  465. return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopStruct",
  466. CurLoop->getStartLoc(),
  467. CurLoop->getHeader())
  468. << " Unsafe Loop structure";
  469. });
  470. return false;
  471. }
  472. // Check loop instruction leagality.
  473. if (!legalLoopInstructions()) {
  474. LLVM_DEBUG(
  475. dbgs()
  476. << " Loop instructions not suitable for LoopVersioningLICM\n\n");
  477. return false;
  478. }
  479. // Check loop memory access leagality.
  480. if (!legalLoopMemoryAccesses()) {
  481. LLVM_DEBUG(
  482. dbgs()
  483. << " Loop memory access not suitable for LoopVersioningLICM\n\n");
  484. ORE->emit([&]() {
  485. return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopMemoryAccess",
  486. CurLoop->getStartLoc(),
  487. CurLoop->getHeader())
  488. << " Unsafe Loop memory access";
  489. });
  490. return false;
  491. }
  492. // Loop versioning is feasible, return true.
  493. LLVM_DEBUG(dbgs() << " Loop Versioning found to be beneficial\n\n");
  494. ORE->emit([&]() {
  495. return OptimizationRemark(DEBUG_TYPE, "IsLegalForVersioning",
  496. CurLoop->getStartLoc(), CurLoop->getHeader())
  497. << " Versioned loop for LICM."
  498. << " Number of runtime checks we had to insert "
  499. << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks());
  500. });
  501. return true;
  502. }
  503. /// Update loop with aggressive aliasing assumptions.
  504. /// It marks no-alias to any pairs of memory operations by assuming
  505. /// loop should not have any must-alias memory accesses pairs.
  506. /// During LoopVersioningLICM legality we ignore loops having must
  507. /// aliasing memory accesses.
  508. void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) {
  509. // Get latch terminator instruction.
  510. Instruction *I = VerLoop->getLoopLatch()->getTerminator();
  511. // Create alias scope domain.
  512. MDBuilder MDB(I->getContext());
  513. MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain");
  514. StringRef Name = "LVAliasScope";
  515. MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name);
  516. SmallVector<Metadata *, 4> Scopes{NewScope}, NoAliases{NewScope};
  517. // Iterate over each instruction of loop.
  518. // set no-alias for all load & store instructions.
  519. for (auto *Block : CurLoop->getBlocks()) {
  520. for (auto &Inst : *Block) {
  521. // Only interested in instruction that may modify or read memory.
  522. if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory())
  523. continue;
  524. // Set no-alias for current instruction.
  525. Inst.setMetadata(
  526. LLVMContext::MD_noalias,
  527. MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias),
  528. MDNode::get(Inst.getContext(), NoAliases)));
  529. // set alias-scope for current instruction.
  530. Inst.setMetadata(
  531. LLVMContext::MD_alias_scope,
  532. MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope),
  533. MDNode::get(Inst.getContext(), Scopes)));
  534. }
  535. }
  536. }
  537. bool LoopVersioningLICMLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
  538. if (skipLoop(L))
  539. return false;
  540. AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
  541. ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  542. OptimizationRemarkEmitter *ORE =
  543. &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
  544. LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  545. DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  546. auto GetLAI = [&](Loop *L) -> const LoopAccessInfo & {
  547. return getAnalysis<LoopAccessLegacyAnalysis>().getInfo(L);
  548. };
  549. return LoopVersioningLICM(AA, SE, ORE, GetLAI).runOnLoop(L, LI, DT);
  550. }
  551. bool LoopVersioningLICM::runOnLoop(Loop *L, LoopInfo *LI, DominatorTree *DT) {
  552. // This will automatically release all resources hold by the current
  553. // LoopVersioningLICM object.
  554. AutoResetter Resetter(*this);
  555. // Do not do the transformation if disabled by metadata.
  556. if (hasLICMVersioningTransformation(L) & TM_Disable)
  557. return false;
  558. // Set Current Loop
  559. CurLoop = L;
  560. CurAST.reset(new AliasSetTracker(*AA));
  561. // Loop over the body of this loop, construct AST.
  562. for (auto *Block : L->getBlocks()) {
  563. if (LI->getLoopFor(Block) == L) // Ignore blocks in subloop.
  564. CurAST->add(*Block); // Incorporate the specified basic block
  565. }
  566. bool Changed = false;
  567. // Check feasiblity of LoopVersioningLICM.
  568. // If versioning found to be feasible and beneficial then proceed
  569. // else simply return, by cleaning up memory.
  570. if (isLegalForVersioning()) {
  571. // Do loop versioning.
  572. // Create memcheck for memory accessed inside loop.
  573. // Clone original loop, and set blocks properly.
  574. LoopVersioning LVer(*LAI, LAI->getRuntimePointerChecking()->getChecks(),
  575. CurLoop, LI, DT, SE);
  576. LVer.versionLoop();
  577. // Set Loop Versioning metaData for original loop.
  578. addStringMetadataToLoop(LVer.getNonVersionedLoop(), LICMVersioningMetaData);
  579. // Set Loop Versioning metaData for version loop.
  580. addStringMetadataToLoop(LVer.getVersionedLoop(), LICMVersioningMetaData);
  581. // Set "llvm.mem.parallel_loop_access" metaData to versioned loop.
  582. // FIXME: "llvm.mem.parallel_loop_access" annotates memory access
  583. // instructions, not loops.
  584. addStringMetadataToLoop(LVer.getVersionedLoop(),
  585. "llvm.mem.parallel_loop_access");
  586. // Update version loop with aggressive aliasing assumption.
  587. setNoAliasToLoop(LVer.getVersionedLoop());
  588. Changed = true;
  589. }
  590. return Changed;
  591. }
  592. char LoopVersioningLICMLegacyPass::ID = 0;
  593. INITIALIZE_PASS_BEGIN(LoopVersioningLICMLegacyPass, "loop-versioning-licm",
  594. "Loop Versioning For LICM", false, false)
  595. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  596. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  597. INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
  598. INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
  599. INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis)
  600. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  601. INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
  602. INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
  603. INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
  604. INITIALIZE_PASS_END(LoopVersioningLICMLegacyPass, "loop-versioning-licm",
  605. "Loop Versioning For LICM", false, false)
  606. Pass *llvm::createLoopVersioningLICMPass() {
  607. return new LoopVersioningLICMLegacyPass();
  608. }
  609. namespace llvm {
  610. PreservedAnalyses LoopVersioningLICMPass::run(Loop &L, LoopAnalysisManager &AM,
  611. LoopStandardAnalysisResults &LAR,
  612. LPMUpdater &U) {
  613. AliasAnalysis *AA = &LAR.AA;
  614. ScalarEvolution *SE = &LAR.SE;
  615. DominatorTree *DT = &LAR.DT;
  616. LoopInfo *LI = &LAR.LI;
  617. const Function *F = L.getHeader()->getParent();
  618. OptimizationRemarkEmitter ORE(F);
  619. auto GetLAI = [&](Loop *L) -> const LoopAccessInfo & {
  620. return AM.getResult<LoopAccessAnalysis>(*L, LAR);
  621. };
  622. if (!LoopVersioningLICM(AA, SE, &ORE, GetLAI).runOnLoop(&L, LI, DT))
  623. return PreservedAnalyses::all();
  624. return getLoopPassPreservedAnalyses();
  625. }
  626. } // namespace llvm