ScopHelper.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. //===- ScopHelper.cpp - Some Helper Functions for Scop. ------------------===//
  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. // Small functions that help with Scop and LLVM-IR.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/Support/ScopHelper.h"
  13. #include "polly/Options.h"
  14. #include "polly/ScopInfo.h"
  15. #include "polly/Support/SCEVValidator.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/Analysis/RegionInfo.h"
  18. #include "llvm/Analysis/ScalarEvolution.h"
  19. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  20. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  21. #include "llvm/Transforms/Utils/LoopUtils.h"
  22. #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
  23. #include <optional>
  24. using namespace llvm;
  25. using namespace polly;
  26. #define DEBUG_TYPE "polly-scop-helper"
  27. static cl::list<std::string> DebugFunctions(
  28. "polly-debug-func",
  29. cl::desc("Allow calls to the specified functions in SCoPs even if their "
  30. "side-effects are unknown. This can be used to do debug output in "
  31. "Polly-transformed code."),
  32. cl::Hidden, cl::CommaSeparated, cl::cat(PollyCategory));
  33. // Ensures that there is just one predecessor to the entry node from outside the
  34. // region.
  35. // The identity of the region entry node is preserved.
  36. static void simplifyRegionEntry(Region *R, DominatorTree *DT, LoopInfo *LI,
  37. RegionInfo *RI) {
  38. BasicBlock *EnteringBB = R->getEnteringBlock();
  39. BasicBlock *Entry = R->getEntry();
  40. // Before (one of):
  41. //
  42. // \ / //
  43. // EnteringBB //
  44. // | \------> //
  45. // \ / | //
  46. // Entry <--\ Entry <--\ //
  47. // / \ / / \ / //
  48. // .... .... //
  49. // Create single entry edge if the region has multiple entry edges.
  50. if (!EnteringBB) {
  51. SmallVector<BasicBlock *, 4> Preds;
  52. for (BasicBlock *P : predecessors(Entry))
  53. if (!R->contains(P))
  54. Preds.push_back(P);
  55. BasicBlock *NewEntering =
  56. SplitBlockPredecessors(Entry, Preds, ".region_entering", DT, LI);
  57. if (RI) {
  58. // The exit block of predecessing regions must be changed to NewEntering
  59. for (BasicBlock *ExitPred : predecessors(NewEntering)) {
  60. Region *RegionOfPred = RI->getRegionFor(ExitPred);
  61. if (RegionOfPred->getExit() != Entry)
  62. continue;
  63. while (!RegionOfPred->isTopLevelRegion() &&
  64. RegionOfPred->getExit() == Entry) {
  65. RegionOfPred->replaceExit(NewEntering);
  66. RegionOfPred = RegionOfPred->getParent();
  67. }
  68. }
  69. // Make all ancestors use EnteringBB as entry; there might be edges to it
  70. Region *AncestorR = R->getParent();
  71. RI->setRegionFor(NewEntering, AncestorR);
  72. while (!AncestorR->isTopLevelRegion() && AncestorR->getEntry() == Entry) {
  73. AncestorR->replaceEntry(NewEntering);
  74. AncestorR = AncestorR->getParent();
  75. }
  76. }
  77. EnteringBB = NewEntering;
  78. }
  79. assert(R->getEnteringBlock() == EnteringBB);
  80. // After:
  81. //
  82. // \ / //
  83. // EnteringBB //
  84. // | //
  85. // | //
  86. // Entry <--\ //
  87. // / \ / //
  88. // .... //
  89. }
  90. // Ensure that the region has a single block that branches to the exit node.
  91. static void simplifyRegionExit(Region *R, DominatorTree *DT, LoopInfo *LI,
  92. RegionInfo *RI) {
  93. BasicBlock *ExitBB = R->getExit();
  94. BasicBlock *ExitingBB = R->getExitingBlock();
  95. // Before:
  96. //
  97. // (Region) ______/ //
  98. // \ | / //
  99. // ExitBB //
  100. // / \ //
  101. if (!ExitingBB) {
  102. SmallVector<BasicBlock *, 4> Preds;
  103. for (BasicBlock *P : predecessors(ExitBB))
  104. if (R->contains(P))
  105. Preds.push_back(P);
  106. // Preds[0] Preds[1] otherBB //
  107. // \ | ________/ //
  108. // \ | / //
  109. // BB //
  110. ExitingBB =
  111. SplitBlockPredecessors(ExitBB, Preds, ".region_exiting", DT, LI);
  112. // Preds[0] Preds[1] otherBB //
  113. // \ / / //
  114. // BB.region_exiting / //
  115. // \ / //
  116. // BB //
  117. if (RI)
  118. RI->setRegionFor(ExitingBB, R);
  119. // Change the exit of nested regions, but not the region itself,
  120. R->replaceExitRecursive(ExitingBB);
  121. R->replaceExit(ExitBB);
  122. }
  123. assert(ExitingBB == R->getExitingBlock());
  124. // After:
  125. //
  126. // \ / //
  127. // ExitingBB _____/ //
  128. // \ / //
  129. // ExitBB //
  130. // / \ //
  131. }
  132. void polly::simplifyRegion(Region *R, DominatorTree *DT, LoopInfo *LI,
  133. RegionInfo *RI) {
  134. assert(R && !R->isTopLevelRegion());
  135. assert(!RI || RI == R->getRegionInfo());
  136. assert((!RI || DT) &&
  137. "RegionInfo requires DominatorTree to be updated as well");
  138. simplifyRegionEntry(R, DT, LI, RI);
  139. simplifyRegionExit(R, DT, LI, RI);
  140. assert(R->isSimple());
  141. }
  142. // Split the block into two successive blocks.
  143. //
  144. // Like llvm::SplitBlock, but also preserves RegionInfo
  145. static BasicBlock *splitBlock(BasicBlock *Old, Instruction *SplitPt,
  146. DominatorTree *DT, llvm::LoopInfo *LI,
  147. RegionInfo *RI) {
  148. assert(Old && SplitPt);
  149. // Before:
  150. //
  151. // \ / //
  152. // Old //
  153. // / \ //
  154. BasicBlock *NewBlock = llvm::SplitBlock(Old, SplitPt, DT, LI);
  155. if (RI) {
  156. Region *R = RI->getRegionFor(Old);
  157. RI->setRegionFor(NewBlock, R);
  158. }
  159. // After:
  160. //
  161. // \ / //
  162. // Old //
  163. // | //
  164. // NewBlock //
  165. // / \ //
  166. return NewBlock;
  167. }
  168. void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, DominatorTree *DT,
  169. LoopInfo *LI, RegionInfo *RI) {
  170. // Find first non-alloca instruction. Every basic block has a non-alloca
  171. // instruction, as every well formed basic block has a terminator.
  172. BasicBlock::iterator I = EntryBlock->begin();
  173. while (isa<AllocaInst>(I))
  174. ++I;
  175. // splitBlock updates DT, LI and RI.
  176. splitBlock(EntryBlock, &*I, DT, LI, RI);
  177. }
  178. void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
  179. auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  180. auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
  181. auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
  182. auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
  183. RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>();
  184. RegionInfo *RI = RIP ? &RIP->getRegionInfo() : nullptr;
  185. // splitBlock updates DT, LI and RI.
  186. polly::splitEntryBlockForAlloca(EntryBlock, DT, LI, RI);
  187. }
  188. void polly::recordAssumption(polly::RecordedAssumptionsTy *RecordedAssumptions,
  189. polly::AssumptionKind Kind, isl::set Set,
  190. DebugLoc Loc, polly::AssumptionSign Sign,
  191. BasicBlock *BB, bool RTC) {
  192. assert((Set.is_params() || BB) &&
  193. "Assumptions without a basic block must be parameter sets");
  194. if (RecordedAssumptions)
  195. RecordedAssumptions->push_back({Kind, Sign, Set, Loc, BB, RTC});
  196. }
  197. /// The SCEVExpander will __not__ generate any code for an existing SDiv/SRem
  198. /// instruction but just use it, if it is referenced as a SCEVUnknown. We want
  199. /// however to generate new code if the instruction is in the analyzed region
  200. /// and we generate code outside/in front of that region. Hence, we generate the
  201. /// code for the SDiv/SRem operands in front of the analyzed region and then
  202. /// create a new SDiv/SRem operation there too.
  203. struct ScopExpander final : SCEVVisitor<ScopExpander, const SCEV *> {
  204. friend struct SCEVVisitor<ScopExpander, const SCEV *>;
  205. explicit ScopExpander(const Region &R, ScalarEvolution &SE,
  206. const DataLayout &DL, const char *Name, ValueMapT *VMap,
  207. BasicBlock *RTCBB)
  208. : Expander(SE, DL, Name, /*PreserveLCSSA=*/false), SE(SE), Name(Name),
  209. R(R), VMap(VMap), RTCBB(RTCBB) {}
  210. Value *expandCodeFor(const SCEV *E, Type *Ty, Instruction *I) {
  211. // If we generate code in the region we will immediately fall back to the
  212. // SCEVExpander, otherwise we will stop at all unknowns in the SCEV and if
  213. // needed replace them by copies computed in the entering block.
  214. if (!R.contains(I))
  215. E = visit(E);
  216. return Expander.expandCodeFor(E, Ty, I);
  217. }
  218. const SCEV *visit(const SCEV *E) {
  219. // Cache the expansion results for intermediate SCEV expressions. A SCEV
  220. // expression can refer to an operand multiple times (e.g. "x*x), so
  221. // a naive visitor takes exponential time.
  222. if (SCEVCache.count(E))
  223. return SCEVCache[E];
  224. const SCEV *Result = SCEVVisitor::visit(E);
  225. SCEVCache[E] = Result;
  226. return Result;
  227. }
  228. private:
  229. SCEVExpander Expander;
  230. ScalarEvolution &SE;
  231. const char *Name;
  232. const Region &R;
  233. ValueMapT *VMap;
  234. BasicBlock *RTCBB;
  235. DenseMap<const SCEV *, const SCEV *> SCEVCache;
  236. const SCEV *visitGenericInst(const SCEVUnknown *E, Instruction *Inst,
  237. Instruction *IP) {
  238. if (!Inst || !R.contains(Inst))
  239. return E;
  240. assert(!Inst->mayThrow() && !Inst->mayReadOrWriteMemory() &&
  241. !isa<PHINode>(Inst));
  242. auto *InstClone = Inst->clone();
  243. for (auto &Op : Inst->operands()) {
  244. assert(SE.isSCEVable(Op->getType()));
  245. auto *OpSCEV = SE.getSCEV(Op);
  246. auto *OpClone = expandCodeFor(OpSCEV, Op->getType(), IP);
  247. InstClone->replaceUsesOfWith(Op, OpClone);
  248. }
  249. InstClone->setName(Name + Inst->getName());
  250. InstClone->insertBefore(IP);
  251. return SE.getSCEV(InstClone);
  252. }
  253. const SCEV *visitUnknown(const SCEVUnknown *E) {
  254. // If a value mapping was given try if the underlying value is remapped.
  255. Value *NewVal = VMap ? VMap->lookup(E->getValue()) : nullptr;
  256. if (NewVal) {
  257. auto *NewE = SE.getSCEV(NewVal);
  258. // While the mapped value might be different the SCEV representation might
  259. // not be. To this end we will check before we go into recursion here.
  260. if (E != NewE)
  261. return visit(NewE);
  262. }
  263. Instruction *Inst = dyn_cast<Instruction>(E->getValue());
  264. Instruction *IP;
  265. if (Inst && !R.contains(Inst))
  266. IP = Inst;
  267. else if (Inst && RTCBB->getParent() == Inst->getFunction())
  268. IP = RTCBB->getTerminator();
  269. else
  270. IP = RTCBB->getParent()->getEntryBlock().getTerminator();
  271. if (!Inst || (Inst->getOpcode() != Instruction::SRem &&
  272. Inst->getOpcode() != Instruction::SDiv))
  273. return visitGenericInst(E, Inst, IP);
  274. const SCEV *LHSScev = SE.getSCEV(Inst->getOperand(0));
  275. const SCEV *RHSScev = SE.getSCEV(Inst->getOperand(1));
  276. if (!SE.isKnownNonZero(RHSScev))
  277. RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
  278. Value *LHS = expandCodeFor(LHSScev, E->getType(), IP);
  279. Value *RHS = expandCodeFor(RHSScev, E->getType(), IP);
  280. Inst = BinaryOperator::Create((Instruction::BinaryOps)Inst->getOpcode(),
  281. LHS, RHS, Inst->getName() + Name, IP);
  282. return SE.getSCEV(Inst);
  283. }
  284. /// The following functions will just traverse the SCEV and rebuild it with
  285. /// the new operands returned by the traversal.
  286. ///
  287. ///{
  288. const SCEV *visitConstant(const SCEVConstant *E) { return E; }
  289. const SCEV *visitPtrToIntExpr(const SCEVPtrToIntExpr *E) {
  290. return SE.getPtrToIntExpr(visit(E->getOperand()), E->getType());
  291. }
  292. const SCEV *visitTruncateExpr(const SCEVTruncateExpr *E) {
  293. return SE.getTruncateExpr(visit(E->getOperand()), E->getType());
  294. }
  295. const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *E) {
  296. return SE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
  297. }
  298. const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *E) {
  299. return SE.getSignExtendExpr(visit(E->getOperand()), E->getType());
  300. }
  301. const SCEV *visitUDivExpr(const SCEVUDivExpr *E) {
  302. auto *RHSScev = visit(E->getRHS());
  303. if (!SE.isKnownNonZero(RHSScev))
  304. RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
  305. return SE.getUDivExpr(visit(E->getLHS()), RHSScev);
  306. }
  307. const SCEV *visitAddExpr(const SCEVAddExpr *E) {
  308. SmallVector<const SCEV *, 4> NewOps;
  309. for (const SCEV *Op : E->operands())
  310. NewOps.push_back(visit(Op));
  311. return SE.getAddExpr(NewOps);
  312. }
  313. const SCEV *visitMulExpr(const SCEVMulExpr *E) {
  314. SmallVector<const SCEV *, 4> NewOps;
  315. for (const SCEV *Op : E->operands())
  316. NewOps.push_back(visit(Op));
  317. return SE.getMulExpr(NewOps);
  318. }
  319. const SCEV *visitUMaxExpr(const SCEVUMaxExpr *E) {
  320. SmallVector<const SCEV *, 4> NewOps;
  321. for (const SCEV *Op : E->operands())
  322. NewOps.push_back(visit(Op));
  323. return SE.getUMaxExpr(NewOps);
  324. }
  325. const SCEV *visitSMaxExpr(const SCEVSMaxExpr *E) {
  326. SmallVector<const SCEV *, 4> NewOps;
  327. for (const SCEV *Op : E->operands())
  328. NewOps.push_back(visit(Op));
  329. return SE.getSMaxExpr(NewOps);
  330. }
  331. const SCEV *visitUMinExpr(const SCEVUMinExpr *E) {
  332. SmallVector<const SCEV *, 4> NewOps;
  333. for (const SCEV *Op : E->operands())
  334. NewOps.push_back(visit(Op));
  335. return SE.getUMinExpr(NewOps);
  336. }
  337. const SCEV *visitSMinExpr(const SCEVSMinExpr *E) {
  338. SmallVector<const SCEV *, 4> NewOps;
  339. for (const SCEV *Op : E->operands())
  340. NewOps.push_back(visit(Op));
  341. return SE.getSMinExpr(NewOps);
  342. }
  343. const SCEV *visitSequentialUMinExpr(const SCEVSequentialUMinExpr *E) {
  344. SmallVector<const SCEV *, 4> NewOps;
  345. for (const SCEV *Op : E->operands())
  346. NewOps.push_back(visit(Op));
  347. return SE.getUMinExpr(NewOps, /*Sequential=*/true);
  348. }
  349. const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) {
  350. SmallVector<const SCEV *, 4> NewOps;
  351. for (const SCEV *Op : E->operands())
  352. NewOps.push_back(visit(Op));
  353. return SE.getAddRecExpr(NewOps, E->getLoop(), E->getNoWrapFlags());
  354. }
  355. ///}
  356. };
  357. Value *polly::expandCodeFor(Scop &S, ScalarEvolution &SE, const DataLayout &DL,
  358. const char *Name, const SCEV *E, Type *Ty,
  359. Instruction *IP, ValueMapT *VMap,
  360. BasicBlock *RTCBB) {
  361. ScopExpander Expander(S.getRegion(), SE, DL, Name, VMap, RTCBB);
  362. return Expander.expandCodeFor(E, Ty, IP);
  363. }
  364. Value *polly::getConditionFromTerminator(Instruction *TI) {
  365. if (BranchInst *BR = dyn_cast<BranchInst>(TI)) {
  366. if (BR->isUnconditional())
  367. return ConstantInt::getTrue(Type::getInt1Ty(TI->getContext()));
  368. return BR->getCondition();
  369. }
  370. if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
  371. return SI->getCondition();
  372. return nullptr;
  373. }
  374. Loop *polly::getLoopSurroundingScop(Scop &S, LoopInfo &LI) {
  375. // Start with the smallest loop containing the entry and expand that
  376. // loop until it contains all blocks in the region. If there is a loop
  377. // containing all blocks in the region check if it is itself contained
  378. // and if so take the parent loop as it will be the smallest containing
  379. // the region but not contained by it.
  380. Loop *L = LI.getLoopFor(S.getEntry());
  381. while (L) {
  382. bool AllContained = true;
  383. for (auto *BB : S.blocks())
  384. AllContained &= L->contains(BB);
  385. if (AllContained)
  386. break;
  387. L = L->getParentLoop();
  388. }
  389. return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr;
  390. }
  391. unsigned polly::getNumBlocksInLoop(Loop *L) {
  392. unsigned NumBlocks = L->getNumBlocks();
  393. SmallVector<BasicBlock *, 4> ExitBlocks;
  394. L->getExitBlocks(ExitBlocks);
  395. for (auto ExitBlock : ExitBlocks) {
  396. if (isa<UnreachableInst>(ExitBlock->getTerminator()))
  397. NumBlocks++;
  398. }
  399. return NumBlocks;
  400. }
  401. unsigned polly::getNumBlocksInRegionNode(RegionNode *RN) {
  402. if (!RN->isSubRegion())
  403. return 1;
  404. Region *R = RN->getNodeAs<Region>();
  405. return std::distance(R->block_begin(), R->block_end());
  406. }
  407. Loop *polly::getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) {
  408. if (!RN->isSubRegion()) {
  409. BasicBlock *BB = RN->getNodeAs<BasicBlock>();
  410. Loop *L = LI.getLoopFor(BB);
  411. // Unreachable statements are not considered to belong to a LLVM loop, as
  412. // they are not part of an actual loop in the control flow graph.
  413. // Nevertheless, we handle certain unreachable statements that are common
  414. // when modeling run-time bounds checks as being part of the loop to be
  415. // able to model them and to later eliminate the run-time bounds checks.
  416. //
  417. // Specifically, for basic blocks that terminate in an unreachable and
  418. // where the immediate predecessor is part of a loop, we assume these
  419. // basic blocks belong to the loop the predecessor belongs to. This
  420. // allows us to model the following code.
  421. //
  422. // for (i = 0; i < N; i++) {
  423. // if (i > 1024)
  424. // abort(); <- this abort might be translated to an
  425. // unreachable
  426. //
  427. // A[i] = ...
  428. // }
  429. if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode())
  430. L = LI.getLoopFor(BB->getPrevNode());
  431. return L;
  432. }
  433. Region *NonAffineSubRegion = RN->getNodeAs<Region>();
  434. Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry());
  435. while (L && NonAffineSubRegion->contains(L))
  436. L = L->getParentLoop();
  437. return L;
  438. }
  439. static bool hasVariantIndex(GetElementPtrInst *Gep, Loop *L, Region &R,
  440. ScalarEvolution &SE) {
  441. for (const Use &Val : llvm::drop_begin(Gep->operands(), 1)) {
  442. const SCEV *PtrSCEV = SE.getSCEVAtScope(Val, L);
  443. Loop *OuterLoop = R.outermostLoopInRegion(L);
  444. if (!SE.isLoopInvariant(PtrSCEV, OuterLoop))
  445. return true;
  446. }
  447. return false;
  448. }
  449. bool polly::isHoistableLoad(LoadInst *LInst, Region &R, LoopInfo &LI,
  450. ScalarEvolution &SE, const DominatorTree &DT,
  451. const InvariantLoadsSetTy &KnownInvariantLoads) {
  452. Loop *L = LI.getLoopFor(LInst->getParent());
  453. auto *Ptr = LInst->getPointerOperand();
  454. // A LoadInst is hoistable if the address it is loading from is also
  455. // invariant; in this case: another invariant load (whether that address
  456. // is also not written to has to be checked separately)
  457. // TODO: This only checks for a LoadInst->GetElementPtrInst->LoadInst
  458. // pattern generated by the Chapel frontend, but generally this applies
  459. // for any chain of instruction that does not also depend on any
  460. // induction variable
  461. if (auto *GepInst = dyn_cast<GetElementPtrInst>(Ptr)) {
  462. if (!hasVariantIndex(GepInst, L, R, SE)) {
  463. if (auto *DecidingLoad =
  464. dyn_cast<LoadInst>(GepInst->getPointerOperand())) {
  465. if (KnownInvariantLoads.count(DecidingLoad))
  466. return true;
  467. }
  468. }
  469. }
  470. const SCEV *PtrSCEV = SE.getSCEVAtScope(Ptr, L);
  471. while (L && R.contains(L)) {
  472. if (!SE.isLoopInvariant(PtrSCEV, L))
  473. return false;
  474. L = L->getParentLoop();
  475. }
  476. for (auto *User : Ptr->users()) {
  477. auto *UserI = dyn_cast<Instruction>(User);
  478. if (!UserI || !R.contains(UserI))
  479. continue;
  480. if (!UserI->mayWriteToMemory())
  481. continue;
  482. auto &BB = *UserI->getParent();
  483. if (DT.dominates(&BB, LInst->getParent()))
  484. return false;
  485. bool DominatesAllPredecessors = true;
  486. if (R.isTopLevelRegion()) {
  487. for (BasicBlock &I : *R.getEntry()->getParent())
  488. if (isa<ReturnInst>(I.getTerminator()) && !DT.dominates(&BB, &I))
  489. DominatesAllPredecessors = false;
  490. } else {
  491. for (auto Pred : predecessors(R.getExit()))
  492. if (R.contains(Pred) && !DT.dominates(&BB, Pred))
  493. DominatesAllPredecessors = false;
  494. }
  495. if (!DominatesAllPredecessors)
  496. continue;
  497. return false;
  498. }
  499. return true;
  500. }
  501. bool polly::isIgnoredIntrinsic(const Value *V) {
  502. if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
  503. switch (IT->getIntrinsicID()) {
  504. // Lifetime markers are supported/ignored.
  505. case llvm::Intrinsic::lifetime_start:
  506. case llvm::Intrinsic::lifetime_end:
  507. // Invariant markers are supported/ignored.
  508. case llvm::Intrinsic::invariant_start:
  509. case llvm::Intrinsic::invariant_end:
  510. // Some misc annotations are supported/ignored.
  511. case llvm::Intrinsic::var_annotation:
  512. case llvm::Intrinsic::ptr_annotation:
  513. case llvm::Intrinsic::annotation:
  514. case llvm::Intrinsic::donothing:
  515. case llvm::Intrinsic::assume:
  516. // Some debug info intrinsics are supported/ignored.
  517. case llvm::Intrinsic::dbg_value:
  518. case llvm::Intrinsic::dbg_declare:
  519. return true;
  520. default:
  521. break;
  522. }
  523. }
  524. return false;
  525. }
  526. bool polly::canSynthesize(const Value *V, const Scop &S, ScalarEvolution *SE,
  527. Loop *Scope) {
  528. if (!V || !SE->isSCEVable(V->getType()))
  529. return false;
  530. const InvariantLoadsSetTy &ILS = S.getRequiredInvariantLoads();
  531. if (const SCEV *Scev = SE->getSCEVAtScope(const_cast<Value *>(V), Scope))
  532. if (!isa<SCEVCouldNotCompute>(Scev))
  533. if (!hasScalarDepsInsideRegion(Scev, &S.getRegion(), Scope, false, ILS))
  534. return true;
  535. return false;
  536. }
  537. llvm::BasicBlock *polly::getUseBlock(const llvm::Use &U) {
  538. Instruction *UI = dyn_cast<Instruction>(U.getUser());
  539. if (!UI)
  540. return nullptr;
  541. if (PHINode *PHI = dyn_cast<PHINode>(UI))
  542. return PHI->getIncomingBlock(U);
  543. return UI->getParent();
  544. }
  545. llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::Loop *L, llvm::LoopInfo &LI,
  546. const BoxedLoopsSetTy &BoxedLoops) {
  547. while (BoxedLoops.count(L))
  548. L = L->getParentLoop();
  549. return L;
  550. }
  551. llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::BasicBlock *BB,
  552. llvm::LoopInfo &LI,
  553. const BoxedLoopsSetTy &BoxedLoops) {
  554. Loop *L = LI.getLoopFor(BB);
  555. return getFirstNonBoxedLoopFor(L, LI, BoxedLoops);
  556. }
  557. bool polly::isDebugCall(Instruction *Inst) {
  558. auto *CI = dyn_cast<CallInst>(Inst);
  559. if (!CI)
  560. return false;
  561. Function *CF = CI->getCalledFunction();
  562. if (!CF)
  563. return false;
  564. return std::find(DebugFunctions.begin(), DebugFunctions.end(),
  565. CF->getName()) != DebugFunctions.end();
  566. }
  567. static bool hasDebugCall(BasicBlock *BB) {
  568. for (Instruction &Inst : *BB) {
  569. if (isDebugCall(&Inst))
  570. return true;
  571. }
  572. return false;
  573. }
  574. bool polly::hasDebugCall(ScopStmt *Stmt) {
  575. // Quick skip if no debug functions have been defined.
  576. if (DebugFunctions.empty())
  577. return false;
  578. if (!Stmt)
  579. return false;
  580. for (Instruction *Inst : Stmt->getInstructions())
  581. if (isDebugCall(Inst))
  582. return true;
  583. if (Stmt->isRegionStmt()) {
  584. for (BasicBlock *RBB : Stmt->getRegion()->blocks())
  585. if (RBB != Stmt->getEntryBlock() && ::hasDebugCall(RBB))
  586. return true;
  587. }
  588. return false;
  589. }
  590. /// Find a property in a LoopID.
  591. static MDNode *findNamedMetadataNode(MDNode *LoopMD, StringRef Name) {
  592. if (!LoopMD)
  593. return nullptr;
  594. for (const MDOperand &X : drop_begin(LoopMD->operands(), 1)) {
  595. auto *OpNode = dyn_cast<MDNode>(X.get());
  596. if (!OpNode)
  597. continue;
  598. auto *OpName = dyn_cast<MDString>(OpNode->getOperand(0));
  599. if (!OpName)
  600. continue;
  601. if (OpName->getString() == Name)
  602. return OpNode;
  603. }
  604. return nullptr;
  605. }
  606. static std::optional<const MDOperand *> findNamedMetadataArg(MDNode *LoopID,
  607. StringRef Name) {
  608. MDNode *MD = findNamedMetadataNode(LoopID, Name);
  609. if (!MD)
  610. return std::nullopt;
  611. switch (MD->getNumOperands()) {
  612. case 1:
  613. return nullptr;
  614. case 2:
  615. return &MD->getOperand(1);
  616. default:
  617. llvm_unreachable("loop metadata has 0 or 1 operand");
  618. }
  619. }
  620. std::optional<Metadata *> polly::findMetadataOperand(MDNode *LoopMD,
  621. StringRef Name) {
  622. MDNode *MD = findNamedMetadataNode(LoopMD, Name);
  623. if (!MD)
  624. return std::nullopt;
  625. switch (MD->getNumOperands()) {
  626. case 1:
  627. return nullptr;
  628. case 2:
  629. return MD->getOperand(1).get();
  630. default:
  631. llvm_unreachable("loop metadata must have 0 or 1 operands");
  632. }
  633. }
  634. static std::optional<bool> getOptionalBoolLoopAttribute(MDNode *LoopID,
  635. StringRef Name) {
  636. MDNode *MD = findNamedMetadataNode(LoopID, Name);
  637. if (!MD)
  638. return std::nullopt;
  639. switch (MD->getNumOperands()) {
  640. case 1:
  641. return true;
  642. case 2:
  643. if (ConstantInt *IntMD =
  644. mdconst::extract_or_null<ConstantInt>(MD->getOperand(1).get()))
  645. return IntMD->getZExtValue();
  646. return true;
  647. }
  648. llvm_unreachable("unexpected number of options");
  649. }
  650. bool polly::getBooleanLoopAttribute(MDNode *LoopID, StringRef Name) {
  651. return getOptionalBoolLoopAttribute(LoopID, Name).value_or(false);
  652. }
  653. std::optional<int> polly::getOptionalIntLoopAttribute(MDNode *LoopID,
  654. StringRef Name) {
  655. const MDOperand *AttrMD =
  656. findNamedMetadataArg(LoopID, Name).value_or(nullptr);
  657. if (!AttrMD)
  658. return std::nullopt;
  659. ConstantInt *IntMD = mdconst::extract_or_null<ConstantInt>(AttrMD->get());
  660. if (!IntMD)
  661. return std::nullopt;
  662. return IntMD->getSExtValue();
  663. }
  664. bool polly::hasDisableAllTransformsHint(Loop *L) {
  665. return llvm::hasDisableAllTransformsHint(L);
  666. }
  667. bool polly::hasDisableAllTransformsHint(llvm::MDNode *LoopID) {
  668. return getBooleanLoopAttribute(LoopID, "llvm.loop.disable_nonforced");
  669. }
  670. isl::id polly::getIslLoopAttr(isl::ctx Ctx, BandAttr *Attr) {
  671. assert(Attr && "Must be a valid BandAttr");
  672. // The name "Loop" signals that this id contains a pointer to a BandAttr.
  673. // The ScheduleOptimizer also uses the string "Inter iteration alias-free" in
  674. // markers, but it's user pointer is an llvm::Value.
  675. isl::id Result = isl::id::alloc(Ctx, "Loop with Metadata", Attr);
  676. Result = isl::manage(isl_id_set_free_user(Result.release(), [](void *Ptr) {
  677. BandAttr *Attr = reinterpret_cast<BandAttr *>(Ptr);
  678. delete Attr;
  679. }));
  680. return Result;
  681. }
  682. isl::id polly::createIslLoopAttr(isl::ctx Ctx, Loop *L) {
  683. if (!L)
  684. return {};
  685. // A loop without metadata does not need to be annotated.
  686. MDNode *LoopID = L->getLoopID();
  687. if (!LoopID)
  688. return {};
  689. BandAttr *Attr = new BandAttr();
  690. Attr->OriginalLoop = L;
  691. Attr->Metadata = L->getLoopID();
  692. return getIslLoopAttr(Ctx, Attr);
  693. }
  694. bool polly::isLoopAttr(const isl::id &Id) {
  695. if (Id.is_null())
  696. return false;
  697. return Id.get_name() == "Loop with Metadata";
  698. }
  699. BandAttr *polly::getLoopAttr(const isl::id &Id) {
  700. if (!isLoopAttr(Id))
  701. return nullptr;
  702. return reinterpret_cast<BandAttr *>(Id.get_user());
  703. }