GenericCycleImpl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- GenericCycleImpl.h -------------------------------------*- C++ -*---===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This template implementation resides in a separate file so that it
  16. /// does not get injected into every .cpp file that includes the
  17. /// generic header.
  18. ///
  19. /// DO NOT INCLUDE THIS FILE WHEN MERELY USING CYCLEINFO.
  20. ///
  21. /// This file should only be included by files that implement a
  22. /// specialization of the relevant templates. Currently these are:
  23. /// - CycleAnalysis.cpp
  24. /// - MachineCycleAnalysis.cpp
  25. ///
  26. //===----------------------------------------------------------------------===//
  27. #ifndef LLVM_ADT_GENERICCYCLEIMPL_H
  28. #define LLVM_ADT_GENERICCYCLEIMPL_H
  29. #include "llvm/ADT/DenseSet.h"
  30. #include "llvm/ADT/DepthFirstIterator.h"
  31. #include "llvm/ADT/GenericCycleInfo.h"
  32. #define DEBUG_TYPE "generic-cycle-impl"
  33. namespace llvm {
  34. template <typename ContextT>
  35. bool GenericCycle<ContextT>::contains(const GenericCycle *C) const {
  36. if (!C)
  37. return false;
  38. if (Depth > C->Depth)
  39. return false;
  40. while (Depth < C->Depth)
  41. C = C->ParentCycle;
  42. return this == C;
  43. }
  44. template <typename ContextT>
  45. void GenericCycle<ContextT>::getExitBlocks(
  46. SmallVectorImpl<BlockT *> &TmpStorage) const {
  47. TmpStorage.clear();
  48. size_t NumExitBlocks = 0;
  49. for (BlockT *Block : blocks()) {
  50. llvm::append_range(TmpStorage, successors(Block));
  51. for (size_t Idx = NumExitBlocks, End = TmpStorage.size(); Idx < End;
  52. ++Idx) {
  53. BlockT *Succ = TmpStorage[Idx];
  54. if (!contains(Succ)) {
  55. auto ExitEndIt = TmpStorage.begin() + NumExitBlocks;
  56. if (std::find(TmpStorage.begin(), ExitEndIt, Succ) == ExitEndIt)
  57. TmpStorage[NumExitBlocks++] = Succ;
  58. }
  59. }
  60. TmpStorage.resize(NumExitBlocks);
  61. }
  62. }
  63. template <typename ContextT>
  64. auto GenericCycle<ContextT>::getCyclePreheader() const -> BlockT * {
  65. BlockT *Predecessor = getCyclePredecessor();
  66. if (!Predecessor)
  67. return nullptr;
  68. assert(isReducible() && "Cycle Predecessor must be in a reducible cycle!");
  69. if (succ_size(Predecessor) != 1)
  70. return nullptr;
  71. // Make sure we are allowed to hoist instructions into the predecessor.
  72. if (!Predecessor->isLegalToHoistInto())
  73. return nullptr;
  74. return Predecessor;
  75. }
  76. template <typename ContextT>
  77. auto GenericCycle<ContextT>::getCyclePredecessor() const -> BlockT * {
  78. if (!isReducible())
  79. return nullptr;
  80. BlockT *Out = nullptr;
  81. // Loop over the predecessors of the header node...
  82. BlockT *Header = getHeader();
  83. for (const auto Pred : predecessors(Header)) {
  84. if (!contains(Pred)) {
  85. if (Out && Out != Pred)
  86. return nullptr;
  87. Out = Pred;
  88. }
  89. }
  90. return Out;
  91. }
  92. /// \brief Helper class for computing cycle information.
  93. template <typename ContextT> class GenericCycleInfoCompute {
  94. using BlockT = typename ContextT::BlockT;
  95. using CycleInfoT = GenericCycleInfo<ContextT>;
  96. using CycleT = typename CycleInfoT::CycleT;
  97. CycleInfoT &Info;
  98. struct DFSInfo {
  99. unsigned Start = 0; // DFS start; positive if block is found
  100. unsigned End = 0; // DFS end
  101. DFSInfo() = default;
  102. explicit DFSInfo(unsigned Start) : Start(Start) {}
  103. /// Whether this node is an ancestor (or equal to) the node \p Other
  104. /// in the DFS tree.
  105. bool isAncestorOf(const DFSInfo &Other) const {
  106. return Start <= Other.Start && Other.End <= End;
  107. }
  108. };
  109. DenseMap<BlockT *, DFSInfo> BlockDFSInfo;
  110. SmallVector<BlockT *, 8> BlockPreorder;
  111. GenericCycleInfoCompute(const GenericCycleInfoCompute &) = delete;
  112. GenericCycleInfoCompute &operator=(const GenericCycleInfoCompute &) = delete;
  113. public:
  114. GenericCycleInfoCompute(CycleInfoT &Info) : Info(Info) {}
  115. void run(BlockT *EntryBlock);
  116. static void updateDepth(CycleT *SubTree);
  117. private:
  118. void dfs(BlockT *EntryBlock);
  119. };
  120. template <typename ContextT>
  121. auto GenericCycleInfo<ContextT>::getTopLevelParentCycle(BlockT *Block)
  122. -> CycleT * {
  123. auto Cycle = BlockMapTopLevel.find(Block);
  124. if (Cycle != BlockMapTopLevel.end())
  125. return Cycle->second;
  126. auto MapIt = BlockMap.find(Block);
  127. if (MapIt == BlockMap.end())
  128. return nullptr;
  129. auto *C = MapIt->second;
  130. while (C->ParentCycle)
  131. C = C->ParentCycle;
  132. BlockMapTopLevel.try_emplace(Block, C);
  133. return C;
  134. }
  135. template <typename ContextT>
  136. void GenericCycleInfo<ContextT>::moveTopLevelCycleToNewParent(CycleT *NewParent,
  137. CycleT *Child) {
  138. assert((!Child->ParentCycle && !NewParent->ParentCycle) &&
  139. "NewParent and Child must be both top level cycle!\n");
  140. auto &CurrentContainer =
  141. Child->ParentCycle ? Child->ParentCycle->Children : TopLevelCycles;
  142. auto Pos = llvm::find_if(CurrentContainer, [=](const auto &Ptr) -> bool {
  143. return Child == Ptr.get();
  144. });
  145. assert(Pos != CurrentContainer.end());
  146. NewParent->Children.push_back(std::move(*Pos));
  147. *Pos = std::move(CurrentContainer.back());
  148. CurrentContainer.pop_back();
  149. Child->ParentCycle = NewParent;
  150. NewParent->Blocks.insert(NewParent->Blocks.end(), Child->block_begin(),
  151. Child->block_end());
  152. for (auto &It : BlockMapTopLevel)
  153. if (It.second == Child)
  154. It.second = NewParent;
  155. }
  156. /// \brief Main function of the cycle info computations.
  157. template <typename ContextT>
  158. void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {
  159. LLVM_DEBUG(errs() << "Entry block: " << Info.Context.print(EntryBlock)
  160. << "\n");
  161. dfs(EntryBlock);
  162. SmallVector<BlockT *, 8> Worklist;
  163. for (BlockT *HeaderCandidate : llvm::reverse(BlockPreorder)) {
  164. const DFSInfo CandidateInfo = BlockDFSInfo.lookup(HeaderCandidate);
  165. for (BlockT *Pred : predecessors(HeaderCandidate)) {
  166. const DFSInfo PredDFSInfo = BlockDFSInfo.lookup(Pred);
  167. if (CandidateInfo.isAncestorOf(PredDFSInfo))
  168. Worklist.push_back(Pred);
  169. }
  170. if (Worklist.empty()) {
  171. continue;
  172. }
  173. // Found a cycle with the candidate as its header.
  174. LLVM_DEBUG(errs() << "Found cycle for header: "
  175. << Info.Context.print(HeaderCandidate) << "\n");
  176. std::unique_ptr<CycleT> NewCycle = std::make_unique<CycleT>();
  177. NewCycle->appendEntry(HeaderCandidate);
  178. NewCycle->appendBlock(HeaderCandidate);
  179. Info.BlockMap.try_emplace(HeaderCandidate, NewCycle.get());
  180. // Helper function to process (non-back-edge) predecessors of a discovered
  181. // block and either add them to the worklist or recognize that the given
  182. // block is an additional cycle entry.
  183. auto ProcessPredecessors = [&](BlockT *Block) {
  184. LLVM_DEBUG(errs() << " block " << Info.Context.print(Block) << ": ");
  185. bool IsEntry = false;
  186. for (BlockT *Pred : predecessors(Block)) {
  187. const DFSInfo PredDFSInfo = BlockDFSInfo.lookup(Pred);
  188. if (CandidateInfo.isAncestorOf(PredDFSInfo)) {
  189. Worklist.push_back(Pred);
  190. } else {
  191. IsEntry = true;
  192. }
  193. }
  194. if (IsEntry) {
  195. assert(!NewCycle->isEntry(Block));
  196. LLVM_DEBUG(errs() << "append as entry\n");
  197. NewCycle->appendEntry(Block);
  198. } else {
  199. LLVM_DEBUG(errs() << "append as child\n");
  200. }
  201. };
  202. do {
  203. BlockT *Block = Worklist.pop_back_val();
  204. if (Block == HeaderCandidate)
  205. continue;
  206. // If the block has already been discovered by some cycle
  207. // (possibly by ourself), then the outermost cycle containing it
  208. // should become our child.
  209. if (auto *BlockParent = Info.getTopLevelParentCycle(Block)) {
  210. LLVM_DEBUG(errs() << " block " << Info.Context.print(Block) << ": ");
  211. if (BlockParent != NewCycle.get()) {
  212. LLVM_DEBUG(errs()
  213. << "discovered child cycle "
  214. << Info.Context.print(BlockParent->getHeader()) << "\n");
  215. // Make BlockParent the child of NewCycle.
  216. Info.moveTopLevelCycleToNewParent(NewCycle.get(), BlockParent);
  217. for (auto *ChildEntry : BlockParent->entries())
  218. ProcessPredecessors(ChildEntry);
  219. } else {
  220. LLVM_DEBUG(errs()
  221. << "known child cycle "
  222. << Info.Context.print(BlockParent->getHeader()) << "\n");
  223. }
  224. } else {
  225. Info.BlockMap.try_emplace(Block, NewCycle.get());
  226. assert(!is_contained(NewCycle->Blocks, Block));
  227. NewCycle->Blocks.push_back(Block);
  228. ProcessPredecessors(Block);
  229. Info.BlockMapTopLevel.try_emplace(Block, NewCycle.get());
  230. }
  231. } while (!Worklist.empty());
  232. Info.TopLevelCycles.push_back(std::move(NewCycle));
  233. }
  234. // Fix top-level cycle links and compute cycle depths.
  235. for (auto *TLC : Info.toplevel_cycles()) {
  236. LLVM_DEBUG(errs() << "top-level cycle: "
  237. << Info.Context.print(TLC->getHeader()) << "\n");
  238. TLC->ParentCycle = nullptr;
  239. updateDepth(TLC);
  240. }
  241. }
  242. /// \brief Recompute depth values of \p SubTree and all descendants.
  243. template <typename ContextT>
  244. void GenericCycleInfoCompute<ContextT>::updateDepth(CycleT *SubTree) {
  245. for (CycleT *Cycle : depth_first(SubTree))
  246. Cycle->Depth = Cycle->ParentCycle ? Cycle->ParentCycle->Depth + 1 : 1;
  247. }
  248. /// \brief Compute a DFS of basic blocks starting at the function entry.
  249. ///
  250. /// Fills BlockDFSInfo with start/end counters and BlockPreorder.
  251. template <typename ContextT>
  252. void GenericCycleInfoCompute<ContextT>::dfs(BlockT *EntryBlock) {
  253. SmallVector<unsigned, 8> DFSTreeStack;
  254. SmallVector<BlockT *, 8> TraverseStack;
  255. unsigned Counter = 0;
  256. TraverseStack.emplace_back(EntryBlock);
  257. do {
  258. BlockT *Block = TraverseStack.back();
  259. LLVM_DEBUG(errs() << "DFS visiting block: " << Info.Context.print(Block)
  260. << "\n");
  261. if (!BlockDFSInfo.count(Block)) {
  262. // We're visiting the block for the first time. Open its DFSInfo, add
  263. // successors to the traversal stack, and remember the traversal stack
  264. // depth at which the block was opened, so that we can correctly record
  265. // its end time.
  266. LLVM_DEBUG(errs() << " first encountered at depth "
  267. << TraverseStack.size() << "\n");
  268. DFSTreeStack.emplace_back(TraverseStack.size());
  269. llvm::append_range(TraverseStack, successors(Block));
  270. bool Added = BlockDFSInfo.try_emplace(Block, ++Counter).second;
  271. (void)Added;
  272. assert(Added);
  273. BlockPreorder.push_back(Block);
  274. LLVM_DEBUG(errs() << " preorder number: " << Counter << "\n");
  275. } else {
  276. assert(!DFSTreeStack.empty());
  277. if (DFSTreeStack.back() == TraverseStack.size()) {
  278. LLVM_DEBUG(errs() << " ended at " << Counter << "\n");
  279. BlockDFSInfo.find(Block)->second.End = Counter;
  280. DFSTreeStack.pop_back();
  281. } else {
  282. LLVM_DEBUG(errs() << " already done\n");
  283. }
  284. TraverseStack.pop_back();
  285. }
  286. } while (!TraverseStack.empty());
  287. assert(DFSTreeStack.empty());
  288. LLVM_DEBUG(
  289. errs() << "Preorder:\n";
  290. for (int i = 0, e = BlockPreorder.size(); i != e; ++i) {
  291. errs() << " " << Info.Context.print(BlockPreorder[i]) << ": " << i << "\n";
  292. }
  293. );
  294. }
  295. /// \brief Reset the object to its initial state.
  296. template <typename ContextT> void GenericCycleInfo<ContextT>::clear() {
  297. TopLevelCycles.clear();
  298. BlockMap.clear();
  299. BlockMapTopLevel.clear();
  300. }
  301. /// \brief Compute the cycle info for a function.
  302. template <typename ContextT>
  303. void GenericCycleInfo<ContextT>::compute(FunctionT &F) {
  304. GenericCycleInfoCompute<ContextT> Compute(*this);
  305. Context.setFunction(F);
  306. LLVM_DEBUG(errs() << "Computing cycles for function: " << F.getName()
  307. << "\n");
  308. Compute.run(ContextT::getEntryBlock(F));
  309. assert(validateTree());
  310. }
  311. /// \brief Find the innermost cycle containing a given block.
  312. ///
  313. /// \returns the innermost cycle containing \p Block or nullptr if
  314. /// it is not contained in any cycle.
  315. template <typename ContextT>
  316. auto GenericCycleInfo<ContextT>::getCycle(const BlockT *Block) const
  317. -> CycleT * {
  318. auto MapIt = BlockMap.find(Block);
  319. if (MapIt != BlockMap.end())
  320. return MapIt->second;
  321. return nullptr;
  322. }
  323. /// \brief get the depth for the cycle which containing a given block.
  324. ///
  325. /// \returns the depth for the innermost cycle containing \p Block or 0 if it is
  326. /// not contained in any cycle.
  327. template <typename ContextT>
  328. unsigned GenericCycleInfo<ContextT>::getCycleDepth(const BlockT *Block) const {
  329. CycleT *Cycle = getCycle(Block);
  330. if (!Cycle)
  331. return 0;
  332. return Cycle->getDepth();
  333. }
  334. #ifndef NDEBUG
  335. /// \brief Validate the internal consistency of the cycle tree.
  336. ///
  337. /// Note that this does \em not check that cycles are really cycles in the CFG,
  338. /// or that the right set of cycles in the CFG were found.
  339. template <typename ContextT>
  340. bool GenericCycleInfo<ContextT>::validateTree() const {
  341. DenseSet<BlockT *> Blocks;
  342. DenseSet<BlockT *> Entries;
  343. auto reportError = [](const char *File, int Line, const char *Cond) {
  344. errs() << File << ':' << Line
  345. << ": GenericCycleInfo::validateTree: " << Cond << '\n';
  346. };
  347. #define check(cond) \
  348. do { \
  349. if (!(cond)) { \
  350. reportError(__FILE__, __LINE__, #cond); \
  351. return false; \
  352. } \
  353. } while (false)
  354. for (const auto *TLC : toplevel_cycles()) {
  355. for (const CycleT *Cycle : depth_first(TLC)) {
  356. if (Cycle->ParentCycle)
  357. check(is_contained(Cycle->ParentCycle->children(), Cycle));
  358. for (BlockT *Block : Cycle->Blocks) {
  359. auto MapIt = BlockMap.find(Block);
  360. check(MapIt != BlockMap.end());
  361. check(Cycle->contains(MapIt->second));
  362. check(Blocks.insert(Block).second); // duplicates in block list?
  363. }
  364. Blocks.clear();
  365. check(!Cycle->Entries.empty());
  366. for (BlockT *Entry : Cycle->Entries) {
  367. check(Entries.insert(Entry).second); // duplicate entry?
  368. check(is_contained(Cycle->Blocks, Entry));
  369. }
  370. Entries.clear();
  371. unsigned ChildDepth = 0;
  372. for (const CycleT *Child : Cycle->children()) {
  373. check(Child->Depth > Cycle->Depth);
  374. if (!ChildDepth) {
  375. ChildDepth = Child->Depth;
  376. } else {
  377. check(ChildDepth == Child->Depth);
  378. }
  379. }
  380. }
  381. }
  382. for (const auto &Entry : BlockMap) {
  383. BlockT *Block = Entry.first;
  384. for (const CycleT *Cycle = Entry.second; Cycle;
  385. Cycle = Cycle->ParentCycle) {
  386. check(is_contained(Cycle->Blocks, Block));
  387. }
  388. }
  389. #undef check
  390. return true;
  391. }
  392. #endif
  393. /// \brief Print the cycle info.
  394. template <typename ContextT>
  395. void GenericCycleInfo<ContextT>::print(raw_ostream &Out) const {
  396. for (const auto *TLC : toplevel_cycles()) {
  397. for (const CycleT *Cycle : depth_first(TLC)) {
  398. for (unsigned I = 0; I < Cycle->Depth; ++I)
  399. Out << " ";
  400. Out << Cycle->print(Context) << '\n';
  401. }
  402. }
  403. }
  404. } // namespace llvm
  405. #undef DEBUG_TYPE
  406. #endif // LLVM_ADT_GENERICCYCLEIMPL_H
  407. #ifdef __GNUC__
  408. #pragma GCC diagnostic pop
  409. #endif