LoopVersioningLICM.cpp 24 KB

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