GenericCycleImpl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. /// \brief Helper class for computing cycle information.
  64. template <typename ContextT> class GenericCycleInfoCompute {
  65. using BlockT = typename ContextT::BlockT;
  66. using CycleInfoT = GenericCycleInfo<ContextT>;
  67. using CycleT = typename CycleInfoT::CycleT;
  68. CycleInfoT &Info;
  69. struct DFSInfo {
  70. unsigned Start = 0; // DFS start; positive if block is found
  71. unsigned End = 0; // DFS end
  72. DFSInfo() = default;
  73. explicit DFSInfo(unsigned Start) : Start(Start) {}
  74. /// Whether this node is an ancestor (or equal to) the node \p Other
  75. /// in the DFS tree.
  76. bool isAncestorOf(const DFSInfo &Other) const {
  77. return Start <= Other.Start && Other.End <= End;
  78. }
  79. };
  80. DenseMap<BlockT *, DFSInfo> BlockDFSInfo;
  81. SmallVector<BlockT *, 8> BlockPreorder;
  82. GenericCycleInfoCompute(const GenericCycleInfoCompute &) = delete;
  83. GenericCycleInfoCompute &operator=(const GenericCycleInfoCompute &) = delete;
  84. public:
  85. GenericCycleInfoCompute(CycleInfoT &Info) : Info(Info) {}
  86. void run(BlockT *EntryBlock);
  87. static void updateDepth(CycleT *SubTree);
  88. private:
  89. void dfs(BlockT *EntryBlock);
  90. };
  91. template <typename ContextT>
  92. auto GenericCycleInfo<ContextT>::getTopLevelParentCycle(
  93. const BlockT *Block) const -> CycleT * {
  94. auto MapIt = BlockMap.find(Block);
  95. if (MapIt == BlockMap.end())
  96. return nullptr;
  97. auto *C = MapIt->second;
  98. while (C->ParentCycle)
  99. C = C->ParentCycle;
  100. return C;
  101. }
  102. template <typename ContextT>
  103. void GenericCycleInfo<ContextT>::moveToNewParent(CycleT *NewParent,
  104. CycleT *Child) {
  105. auto &CurrentContainer =
  106. Child->ParentCycle ? Child->ParentCycle->Children : TopLevelCycles;
  107. auto Pos = llvm::find_if(CurrentContainer, [=](const auto &Ptr) -> bool {
  108. return Child == Ptr.get();
  109. });
  110. assert(Pos != CurrentContainer.end());
  111. NewParent->Children.push_back(std::move(*Pos));
  112. *Pos = std::move(CurrentContainer.back());
  113. CurrentContainer.pop_back();
  114. Child->ParentCycle = NewParent;
  115. }
  116. /// \brief Main function of the cycle info computations.
  117. template <typename ContextT>
  118. void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {
  119. LLVM_DEBUG(errs() << "Entry block: " << Info.Context.print(EntryBlock)
  120. << "\n");
  121. dfs(EntryBlock);
  122. SmallVector<BlockT *, 8> Worklist;
  123. for (BlockT *HeaderCandidate : llvm::reverse(BlockPreorder)) {
  124. const DFSInfo CandidateInfo = BlockDFSInfo.lookup(HeaderCandidate);
  125. for (BlockT *Pred : predecessors(HeaderCandidate)) {
  126. const DFSInfo PredDFSInfo = BlockDFSInfo.lookup(Pred);
  127. if (CandidateInfo.isAncestorOf(PredDFSInfo))
  128. Worklist.push_back(Pred);
  129. }
  130. if (Worklist.empty()) {
  131. continue;
  132. }
  133. // Found a cycle with the candidate as its header.
  134. LLVM_DEBUG(errs() << "Found cycle for header: "
  135. << Info.Context.print(HeaderCandidate) << "\n");
  136. std::unique_ptr<CycleT> NewCycle = std::make_unique<CycleT>();
  137. NewCycle->appendEntry(HeaderCandidate);
  138. NewCycle->appendBlock(HeaderCandidate);
  139. Info.BlockMap.try_emplace(HeaderCandidate, NewCycle.get());
  140. // Helper function to process (non-back-edge) predecessors of a discovered
  141. // block and either add them to the worklist or recognize that the given
  142. // block is an additional cycle entry.
  143. auto ProcessPredecessors = [&](BlockT *Block) {
  144. LLVM_DEBUG(errs() << " block " << Info.Context.print(Block) << ": ");
  145. bool IsEntry = false;
  146. for (BlockT *Pred : predecessors(Block)) {
  147. const DFSInfo PredDFSInfo = BlockDFSInfo.lookup(Pred);
  148. if (CandidateInfo.isAncestorOf(PredDFSInfo)) {
  149. Worklist.push_back(Pred);
  150. } else {
  151. IsEntry = true;
  152. }
  153. }
  154. if (IsEntry) {
  155. assert(!NewCycle->isEntry(Block));
  156. LLVM_DEBUG(errs() << "append as entry\n");
  157. NewCycle->appendEntry(Block);
  158. } else {
  159. LLVM_DEBUG(errs() << "append as child\n");
  160. }
  161. };
  162. do {
  163. BlockT *Block = Worklist.pop_back_val();
  164. if (Block == HeaderCandidate)
  165. continue;
  166. // If the block has already been discovered by some cycle
  167. // (possibly by ourself), then the outermost cycle containing it
  168. // should become our child.
  169. if (auto *BlockParent = Info.getTopLevelParentCycle(Block)) {
  170. LLVM_DEBUG(errs() << " block " << Info.Context.print(Block) << ": ");
  171. if (BlockParent != NewCycle.get()) {
  172. LLVM_DEBUG(errs()
  173. << "discovered child cycle "
  174. << Info.Context.print(BlockParent->getHeader()) << "\n");
  175. // Make BlockParent the child of NewCycle.
  176. Info.moveToNewParent(NewCycle.get(), BlockParent);
  177. NewCycle->Blocks.insert(NewCycle->Blocks.end(),
  178. BlockParent->block_begin(),
  179. BlockParent->block_end());
  180. for (auto *ChildEntry : BlockParent->entries())
  181. ProcessPredecessors(ChildEntry);
  182. } else {
  183. LLVM_DEBUG(errs()
  184. << "known child cycle "
  185. << Info.Context.print(BlockParent->getHeader()) << "\n");
  186. }
  187. } else {
  188. Info.BlockMap.try_emplace(Block, NewCycle.get());
  189. assert(!is_contained(NewCycle->Blocks, Block));
  190. NewCycle->Blocks.push_back(Block);
  191. ProcessPredecessors(Block);
  192. }
  193. } while (!Worklist.empty());
  194. Info.TopLevelCycles.push_back(std::move(NewCycle));
  195. }
  196. // Fix top-level cycle links and compute cycle depths.
  197. for (auto *TLC : Info.toplevel_cycles()) {
  198. LLVM_DEBUG(errs() << "top-level cycle: "
  199. << Info.Context.print(TLC->getHeader()) << "\n");
  200. TLC->ParentCycle = nullptr;
  201. updateDepth(TLC);
  202. }
  203. }
  204. /// \brief Recompute depth values of \p SubTree and all descendants.
  205. template <typename ContextT>
  206. void GenericCycleInfoCompute<ContextT>::updateDepth(CycleT *SubTree) {
  207. for (CycleT *Cycle : depth_first(SubTree))
  208. Cycle->Depth = Cycle->ParentCycle ? Cycle->ParentCycle->Depth + 1 : 1;
  209. }
  210. /// \brief Compute a DFS of basic blocks starting at the function entry.
  211. ///
  212. /// Fills BlockDFSInfo with start/end counters and BlockPreorder.
  213. template <typename ContextT>
  214. void GenericCycleInfoCompute<ContextT>::dfs(BlockT *EntryBlock) {
  215. SmallVector<unsigned, 8> DFSTreeStack;
  216. SmallVector<BlockT *, 8> TraverseStack;
  217. unsigned Counter = 0;
  218. TraverseStack.emplace_back(EntryBlock);
  219. do {
  220. BlockT *Block = TraverseStack.back();
  221. LLVM_DEBUG(errs() << "DFS visiting block: " << Info.Context.print(Block)
  222. << "\n");
  223. if (!BlockDFSInfo.count(Block)) {
  224. // We're visiting the block for the first time. Open its DFSInfo, add
  225. // successors to the traversal stack, and remember the traversal stack
  226. // depth at which the block was opened, so that we can correctly record
  227. // its end time.
  228. LLVM_DEBUG(errs() << " first encountered at depth "
  229. << TraverseStack.size() << "\n");
  230. DFSTreeStack.emplace_back(TraverseStack.size());
  231. llvm::append_range(TraverseStack, successors(Block));
  232. LLVM_ATTRIBUTE_UNUSED
  233. bool Added = BlockDFSInfo.try_emplace(Block, ++Counter).second;
  234. assert(Added);
  235. BlockPreorder.push_back(Block);
  236. LLVM_DEBUG(errs() << " preorder number: " << Counter << "\n");
  237. } else {
  238. assert(!DFSTreeStack.empty());
  239. if (DFSTreeStack.back() == TraverseStack.size()) {
  240. LLVM_DEBUG(errs() << " ended at " << Counter << "\n");
  241. BlockDFSInfo.find(Block)->second.End = Counter;
  242. DFSTreeStack.pop_back();
  243. } else {
  244. LLVM_DEBUG(errs() << " already done\n");
  245. }
  246. TraverseStack.pop_back();
  247. }
  248. } while (!TraverseStack.empty());
  249. assert(DFSTreeStack.empty());
  250. LLVM_DEBUG(
  251. errs() << "Preorder:\n";
  252. for (int i = 0, e = BlockPreorder.size(); i != e; ++i) {
  253. errs() << " " << Info.Context.print(BlockPreorder[i]) << ": " << i << "\n";
  254. }
  255. );
  256. }
  257. /// \brief Reset the object to its initial state.
  258. template <typename ContextT> void GenericCycleInfo<ContextT>::clear() {
  259. TopLevelCycles.clear();
  260. BlockMap.clear();
  261. }
  262. /// \brief Compute the cycle info for a function.
  263. template <typename ContextT>
  264. void GenericCycleInfo<ContextT>::compute(FunctionT &F) {
  265. GenericCycleInfoCompute<ContextT> Compute(*this);
  266. Context.setFunction(F);
  267. LLVM_DEBUG(errs() << "Computing cycles for function: " << F.getName()
  268. << "\n");
  269. Compute.run(ContextT::getEntryBlock(F));
  270. assert(validateTree());
  271. }
  272. /// \brief Find the innermost cycle containing a given block.
  273. ///
  274. /// \returns the innermost cycle containing \p Block or nullptr if
  275. /// it is not contained in any cycle.
  276. template <typename ContextT>
  277. auto GenericCycleInfo<ContextT>::getCycle(const BlockT *Block) const
  278. -> CycleT * {
  279. auto MapIt = BlockMap.find(Block);
  280. if (MapIt != BlockMap.end())
  281. return MapIt->second;
  282. return nullptr;
  283. }
  284. /// \brief Validate the internal consistency of the cycle tree.
  285. ///
  286. /// Note that this does \em not check that cycles are really cycles in the CFG,
  287. /// or that the right set of cycles in the CFG were found.
  288. template <typename ContextT>
  289. bool GenericCycleInfo<ContextT>::validateTree() const {
  290. DenseSet<BlockT *> Blocks;
  291. DenseSet<BlockT *> Entries;
  292. auto reportError = [](const char *File, int Line, const char *Cond) {
  293. errs() << File << ':' << Line
  294. << ": GenericCycleInfo::validateTree: " << Cond << '\n';
  295. };
  296. #define check(cond) \
  297. do { \
  298. if (!(cond)) { \
  299. reportError(__FILE__, __LINE__, #cond); \
  300. return false; \
  301. } \
  302. } while (false)
  303. for (const auto *TLC : toplevel_cycles()) {
  304. for (const CycleT *Cycle : depth_first(TLC)) {
  305. if (Cycle->ParentCycle)
  306. check(is_contained(Cycle->ParentCycle->children(), Cycle));
  307. for (BlockT *Block : Cycle->Blocks) {
  308. auto MapIt = BlockMap.find(Block);
  309. check(MapIt != BlockMap.end());
  310. check(Cycle->contains(MapIt->second));
  311. check(Blocks.insert(Block).second); // duplicates in block list?
  312. }
  313. Blocks.clear();
  314. check(!Cycle->Entries.empty());
  315. for (BlockT *Entry : Cycle->Entries) {
  316. check(Entries.insert(Entry).second); // duplicate entry?
  317. check(is_contained(Cycle->Blocks, Entry));
  318. }
  319. Entries.clear();
  320. unsigned ChildDepth = 0;
  321. for (const CycleT *Child : Cycle->children()) {
  322. check(Child->Depth > Cycle->Depth);
  323. if (!ChildDepth) {
  324. ChildDepth = Child->Depth;
  325. } else {
  326. check(ChildDepth == Child->Depth);
  327. }
  328. }
  329. }
  330. }
  331. for (const auto &Entry : BlockMap) {
  332. BlockT *Block = Entry.first;
  333. for (const CycleT *Cycle = Entry.second; Cycle;
  334. Cycle = Cycle->ParentCycle) {
  335. check(is_contained(Cycle->Blocks, Block));
  336. }
  337. }
  338. #undef check
  339. return true;
  340. }
  341. /// \brief Print the cycle info.
  342. template <typename ContextT>
  343. void GenericCycleInfo<ContextT>::print(raw_ostream &Out) const {
  344. for (const auto *TLC : toplevel_cycles()) {
  345. for (const CycleT *Cycle : depth_first(TLC)) {
  346. for (unsigned I = 0; I < Cycle->Depth; ++I)
  347. Out << " ";
  348. Out << Cycle->print(Context) << '\n';
  349. }
  350. }
  351. }
  352. } // namespace llvm
  353. #undef DEBUG_TYPE
  354. #endif // LLVM_ADT_GENERICCYCLEIMPL_H
  355. #ifdef __GNUC__
  356. #pragma GCC diagnostic pop
  357. #endif