ScopHelper.cpp 26 KB

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