RegionInfoImpl.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. //===- RegionInfoImpl.h - SESE region detection analysis --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Detects single entry single exit regions in the control flow graph.
  10. //===----------------------------------------------------------------------===//
  11. #ifndef LLVM_ANALYSIS_REGIONINFOIMPL_H
  12. #define LLVM_ANALYSIS_REGIONINFOIMPL_H
  13. #include "llvm/ADT/PostOrderIterator.h"
  14. #include "llvm/Analysis/DominanceFrontier.h"
  15. #include "llvm/Analysis/LoopInfo.h"
  16. #include "llvm/Analysis/PostDominators.h"
  17. #include "llvm/Analysis/RegionInfo.h"
  18. #include "llvm/Analysis/RegionIterator.h"
  19. #include "llvm/Support/CommandLine.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include <algorithm>
  23. #include <iterator>
  24. #include <set>
  25. namespace llvm {
  26. #define DEBUG_TYPE "region"
  27. //===----------------------------------------------------------------------===//
  28. /// RegionBase Implementation
  29. template <class Tr>
  30. RegionBase<Tr>::RegionBase(BlockT *Entry, BlockT *Exit,
  31. typename Tr::RegionInfoT *RInfo, DomTreeT *dt,
  32. RegionT *Parent)
  33. : RegionNodeBase<Tr>(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
  34. template <class Tr>
  35. RegionBase<Tr>::~RegionBase() {
  36. // Free the cached nodes.
  37. for (typename BBNodeMapT::iterator it = BBNodeMap.begin(),
  38. ie = BBNodeMap.end();
  39. it != ie; ++it)
  40. delete it->second;
  41. // Only clean the cache for this Region. Caches of child Regions will be
  42. // cleaned when the child Regions are deleted.
  43. BBNodeMap.clear();
  44. }
  45. template <class Tr>
  46. void RegionBase<Tr>::replaceEntry(BlockT *BB) {
  47. this->entry.setPointer(BB);
  48. }
  49. template <class Tr>
  50. void RegionBase<Tr>::replaceExit(BlockT *BB) {
  51. assert(exit && "No exit to replace!");
  52. exit = BB;
  53. }
  54. template <class Tr>
  55. void RegionBase<Tr>::replaceEntryRecursive(BlockT *NewEntry) {
  56. std::vector<RegionT *> RegionQueue;
  57. BlockT *OldEntry = getEntry();
  58. RegionQueue.push_back(static_cast<RegionT *>(this));
  59. while (!RegionQueue.empty()) {
  60. RegionT *R = RegionQueue.back();
  61. RegionQueue.pop_back();
  62. R->replaceEntry(NewEntry);
  63. for (typename RegionT::const_iterator RI = R->begin(), RE = R->end();
  64. RI != RE; ++RI) {
  65. if ((*RI)->getEntry() == OldEntry)
  66. RegionQueue.push_back(RI->get());
  67. }
  68. }
  69. }
  70. template <class Tr>
  71. void RegionBase<Tr>::replaceExitRecursive(BlockT *NewExit) {
  72. std::vector<RegionT *> RegionQueue;
  73. BlockT *OldExit = getExit();
  74. RegionQueue.push_back(static_cast<RegionT *>(this));
  75. while (!RegionQueue.empty()) {
  76. RegionT *R = RegionQueue.back();
  77. RegionQueue.pop_back();
  78. R->replaceExit(NewExit);
  79. for (typename RegionT::const_iterator RI = R->begin(), RE = R->end();
  80. RI != RE; ++RI) {
  81. if ((*RI)->getExit() == OldExit)
  82. RegionQueue.push_back(RI->get());
  83. }
  84. }
  85. }
  86. template <class Tr>
  87. bool RegionBase<Tr>::contains(const BlockT *B) const {
  88. BlockT *BB = const_cast<BlockT *>(B);
  89. if (!DT->getNode(BB))
  90. return false;
  91. BlockT *entry = getEntry(), *exit = getExit();
  92. // Toplevel region.
  93. if (!exit)
  94. return true;
  95. return (DT->dominates(entry, BB) &&
  96. !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
  97. }
  98. template <class Tr>
  99. bool RegionBase<Tr>::contains(const LoopT *L) const {
  100. // BBs that are not part of any loop are element of the Loop
  101. // described by the NULL pointer. This loop is not part of any region,
  102. // except if the region describes the whole function.
  103. if (!L)
  104. return getExit() == nullptr;
  105. if (!contains(L->getHeader()))
  106. return false;
  107. SmallVector<BlockT *, 8> ExitingBlocks;
  108. L->getExitingBlocks(ExitingBlocks);
  109. for (BlockT *BB : ExitingBlocks) {
  110. if (!contains(BB))
  111. return false;
  112. }
  113. return true;
  114. }
  115. template <class Tr>
  116. typename Tr::LoopT *RegionBase<Tr>::outermostLoopInRegion(LoopT *L) const {
  117. if (!contains(L))
  118. return nullptr;
  119. while (L && contains(L->getParentLoop())) {
  120. L = L->getParentLoop();
  121. }
  122. return L;
  123. }
  124. template <class Tr>
  125. typename Tr::LoopT *RegionBase<Tr>::outermostLoopInRegion(LoopInfoT *LI,
  126. BlockT *BB) const {
  127. assert(LI && BB && "LI and BB cannot be null!");
  128. LoopT *L = LI->getLoopFor(BB);
  129. return outermostLoopInRegion(L);
  130. }
  131. template <class Tr>
  132. typename RegionBase<Tr>::BlockT *RegionBase<Tr>::getEnteringBlock() const {
  133. BlockT *entry = getEntry();
  134. BlockT *Pred;
  135. BlockT *enteringBlock = nullptr;
  136. for (PredIterTy PI = InvBlockTraits::child_begin(entry),
  137. PE = InvBlockTraits::child_end(entry);
  138. PI != PE; ++PI) {
  139. Pred = *PI;
  140. if (DT->getNode(Pred) && !contains(Pred)) {
  141. if (enteringBlock)
  142. return nullptr;
  143. enteringBlock = Pred;
  144. }
  145. }
  146. return enteringBlock;
  147. }
  148. template <class Tr>
  149. typename RegionBase<Tr>::BlockT *RegionBase<Tr>::getExitingBlock() const {
  150. BlockT *exit = getExit();
  151. BlockT *Pred;
  152. BlockT *exitingBlock = nullptr;
  153. if (!exit)
  154. return nullptr;
  155. for (PredIterTy PI = InvBlockTraits::child_begin(exit),
  156. PE = InvBlockTraits::child_end(exit);
  157. PI != PE; ++PI) {
  158. Pred = *PI;
  159. if (contains(Pred)) {
  160. if (exitingBlock)
  161. return nullptr;
  162. exitingBlock = Pred;
  163. }
  164. }
  165. return exitingBlock;
  166. }
  167. template <class Tr>
  168. bool RegionBase<Tr>::isSimple() const {
  169. return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
  170. }
  171. template <class Tr>
  172. std::string RegionBase<Tr>::getNameStr() const {
  173. std::string exitName;
  174. std::string entryName;
  175. if (getEntry()->getName().empty()) {
  176. raw_string_ostream OS(entryName);
  177. getEntry()->printAsOperand(OS, false);
  178. } else
  179. entryName = getEntry()->getName();
  180. if (getExit()) {
  181. if (getExit()->getName().empty()) {
  182. raw_string_ostream OS(exitName);
  183. getExit()->printAsOperand(OS, false);
  184. } else
  185. exitName = getExit()->getName();
  186. } else
  187. exitName = "<Function Return>";
  188. return entryName + " => " + exitName;
  189. }
  190. template <class Tr>
  191. void RegionBase<Tr>::verifyBBInRegion(BlockT *BB) const {
  192. if (!contains(BB))
  193. llvm_unreachable("Broken region found!");
  194. BlockT *entry = getEntry(), *exit = getExit();
  195. for (SuccIterTy SI = BlockTraits::child_begin(BB),
  196. SE = BlockTraits::child_end(BB);
  197. SI != SE; ++SI) {
  198. if (!contains(*SI) && exit != *SI)
  199. llvm_unreachable("Broken region found!");
  200. }
  201. if (entry != BB) {
  202. for (PredIterTy SI = InvBlockTraits::child_begin(BB),
  203. SE = InvBlockTraits::child_end(BB);
  204. SI != SE; ++SI) {
  205. if (!contains(*SI))
  206. llvm_unreachable("Broken region found!");
  207. }
  208. }
  209. }
  210. template <class Tr>
  211. void RegionBase<Tr>::verifyWalk(BlockT *BB, std::set<BlockT *> *visited) const {
  212. BlockT *exit = getExit();
  213. visited->insert(BB);
  214. verifyBBInRegion(BB);
  215. for (SuccIterTy SI = BlockTraits::child_begin(BB),
  216. SE = BlockTraits::child_end(BB);
  217. SI != SE; ++SI) {
  218. if (*SI != exit && visited->find(*SI) == visited->end())
  219. verifyWalk(*SI, visited);
  220. }
  221. }
  222. template <class Tr>
  223. void RegionBase<Tr>::verifyRegion() const {
  224. // Only do verification when user wants to, otherwise this expensive check
  225. // will be invoked by PMDataManager::verifyPreservedAnalysis when
  226. // a regionpass (marked PreservedAll) finish.
  227. if (!RegionInfoBase<Tr>::VerifyRegionInfo)
  228. return;
  229. std::set<BlockT *> visited;
  230. verifyWalk(getEntry(), &visited);
  231. }
  232. template <class Tr>
  233. void RegionBase<Tr>::verifyRegionNest() const {
  234. for (typename RegionT::const_iterator RI = begin(), RE = end(); RI != RE;
  235. ++RI)
  236. (*RI)->verifyRegionNest();
  237. verifyRegion();
  238. }
  239. template <class Tr>
  240. typename RegionBase<Tr>::element_iterator RegionBase<Tr>::element_begin() {
  241. return GraphTraits<RegionT *>::nodes_begin(static_cast<RegionT *>(this));
  242. }
  243. template <class Tr>
  244. typename RegionBase<Tr>::element_iterator RegionBase<Tr>::element_end() {
  245. return GraphTraits<RegionT *>::nodes_end(static_cast<RegionT *>(this));
  246. }
  247. template <class Tr>
  248. typename RegionBase<Tr>::const_element_iterator
  249. RegionBase<Tr>::element_begin() const {
  250. return GraphTraits<const RegionT *>::nodes_begin(
  251. static_cast<const RegionT *>(this));
  252. }
  253. template <class Tr>
  254. typename RegionBase<Tr>::const_element_iterator
  255. RegionBase<Tr>::element_end() const {
  256. return GraphTraits<const RegionT *>::nodes_end(
  257. static_cast<const RegionT *>(this));
  258. }
  259. template <class Tr>
  260. typename Tr::RegionT *RegionBase<Tr>::getSubRegionNode(BlockT *BB) const {
  261. typedef typename Tr::RegionT RegionT;
  262. RegionT *R = RI->getRegionFor(BB);
  263. if (!R || R == this)
  264. return nullptr;
  265. // If we pass the BB out of this region, that means our code is broken.
  266. assert(contains(R) && "BB not in current region!");
  267. while (contains(R->getParent()) && R->getParent() != this)
  268. R = R->getParent();
  269. if (R->getEntry() != BB)
  270. return nullptr;
  271. return R;
  272. }
  273. template <class Tr>
  274. typename Tr::RegionNodeT *RegionBase<Tr>::getBBNode(BlockT *BB) const {
  275. assert(contains(BB) && "Can get BB node out of this region!");
  276. typename BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
  277. if (at != BBNodeMap.end())
  278. return at->second;
  279. auto Deconst = const_cast<RegionBase<Tr> *>(this);
  280. RegionNodeT *NewNode = new RegionNodeT(static_cast<RegionT *>(Deconst), BB);
  281. BBNodeMap.insert(std::make_pair(BB, NewNode));
  282. return NewNode;
  283. }
  284. template <class Tr>
  285. typename Tr::RegionNodeT *RegionBase<Tr>::getNode(BlockT *BB) const {
  286. assert(contains(BB) && "Can get BB node out of this region!");
  287. if (RegionT *Child = getSubRegionNode(BB))
  288. return Child->getNode();
  289. return getBBNode(BB);
  290. }
  291. template <class Tr>
  292. void RegionBase<Tr>::transferChildrenTo(RegionT *To) {
  293. for (iterator I = begin(), E = end(); I != E; ++I) {
  294. (*I)->parent = To;
  295. To->children.push_back(std::move(*I));
  296. }
  297. children.clear();
  298. }
  299. template <class Tr>
  300. void RegionBase<Tr>::addSubRegion(RegionT *SubRegion, bool moveChildren) {
  301. assert(!SubRegion->parent && "SubRegion already has a parent!");
  302. assert(std::find_if(begin(), end(), [&](const std::unique_ptr<RegionT> &R) {
  303. return R.get() == SubRegion;
  304. }) == children.end() &&
  305. "Subregion already exists!");
  306. SubRegion->parent = static_cast<RegionT *>(this);
  307. children.push_back(std::unique_ptr<RegionT>(SubRegion));
  308. if (!moveChildren)
  309. return;
  310. assert(SubRegion->children.empty() &&
  311. "SubRegions that contain children are not supported");
  312. for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) {
  313. if (!(*I)->isSubRegion()) {
  314. BlockT *BB = (*I)->template getNodeAs<BlockT>();
  315. if (SubRegion->contains(BB))
  316. RI->setRegionFor(BB, SubRegion);
  317. }
  318. }
  319. std::vector<std::unique_ptr<RegionT>> Keep;
  320. for (iterator I = begin(), E = end(); I != E; ++I) {
  321. if (SubRegion->contains(I->get()) && I->get() != SubRegion) {
  322. (*I)->parent = SubRegion;
  323. SubRegion->children.push_back(std::move(*I));
  324. } else
  325. Keep.push_back(std::move(*I));
  326. }
  327. children.clear();
  328. children.insert(
  329. children.begin(),
  330. std::move_iterator<typename RegionSet::iterator>(Keep.begin()),
  331. std::move_iterator<typename RegionSet::iterator>(Keep.end()));
  332. }
  333. template <class Tr>
  334. typename Tr::RegionT *RegionBase<Tr>::removeSubRegion(RegionT *Child) {
  335. assert(Child->parent == this && "Child is not a child of this region!");
  336. Child->parent = nullptr;
  337. typename RegionSet::iterator I = std::find_if(
  338. children.begin(), children.end(),
  339. [&](const std::unique_ptr<RegionT> &R) { return R.get() == Child; });
  340. assert(I != children.end() && "Region does not exit. Unable to remove.");
  341. children.erase(children.begin() + (I - begin()));
  342. return Child;
  343. }
  344. template <class Tr>
  345. unsigned RegionBase<Tr>::getDepth() const {
  346. unsigned Depth = 0;
  347. for (RegionT *R = getParent(); R != nullptr; R = R->getParent())
  348. ++Depth;
  349. return Depth;
  350. }
  351. template <class Tr>
  352. typename Tr::RegionT *RegionBase<Tr>::getExpandedRegion() const {
  353. unsigned NumSuccessors = Tr::getNumSuccessors(exit);
  354. if (NumSuccessors == 0)
  355. return nullptr;
  356. for (PredIterTy PI = InvBlockTraits::child_begin(getExit()),
  357. PE = InvBlockTraits::child_end(getExit());
  358. PI != PE; ++PI) {
  359. if (!DT->dominates(getEntry(), *PI))
  360. return nullptr;
  361. }
  362. RegionT *R = RI->getRegionFor(exit);
  363. if (R->getEntry() != exit) {
  364. if (Tr::getNumSuccessors(exit) == 1)
  365. return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT);
  366. return nullptr;
  367. }
  368. while (R->getParent() && R->getParent()->getEntry() == exit)
  369. R = R->getParent();
  370. if (!DT->dominates(getEntry(), R->getExit())) {
  371. for (PredIterTy PI = InvBlockTraits::child_begin(getExit()),
  372. PE = InvBlockTraits::child_end(getExit());
  373. PI != PE; ++PI) {
  374. if (!DT->dominates(R->getExit(), *PI))
  375. return nullptr;
  376. }
  377. }
  378. return new RegionT(getEntry(), R->getExit(), RI, DT);
  379. }
  380. template <class Tr>
  381. void RegionBase<Tr>::print(raw_ostream &OS, bool print_tree, unsigned level,
  382. PrintStyle Style) const {
  383. if (print_tree)
  384. OS.indent(level * 2) << '[' << level << "] " << getNameStr();
  385. else
  386. OS.indent(level * 2) << getNameStr();
  387. OS << '\n';
  388. if (Style != PrintNone) {
  389. OS.indent(level * 2) << "{\n";
  390. OS.indent(level * 2 + 2);
  391. if (Style == PrintBB) {
  392. for (const auto *BB : blocks())
  393. OS << BB->getName() << ", "; // TODO: remove the last ","
  394. } else if (Style == PrintRN) {
  395. for (const_element_iterator I = element_begin(), E = element_end();
  396. I != E; ++I) {
  397. OS << **I << ", "; // TODO: remove the last ",
  398. }
  399. }
  400. OS << '\n';
  401. }
  402. if (print_tree) {
  403. for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
  404. (*RI)->print(OS, print_tree, level + 1, Style);
  405. }
  406. if (Style != PrintNone)
  407. OS.indent(level * 2) << "} \n";
  408. }
  409. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  410. template <class Tr>
  411. void RegionBase<Tr>::dump() const {
  412. print(dbgs(), true, getDepth(), RegionInfoBase<Tr>::printStyle);
  413. }
  414. #endif
  415. template <class Tr>
  416. void RegionBase<Tr>::clearNodeCache() {
  417. // Free the cached nodes.
  418. for (typename BBNodeMapT::iterator I = BBNodeMap.begin(),
  419. IE = BBNodeMap.end();
  420. I != IE; ++I)
  421. delete I->second;
  422. BBNodeMap.clear();
  423. for (typename RegionT::iterator RI = begin(), RE = end(); RI != RE; ++RI)
  424. (*RI)->clearNodeCache();
  425. }
  426. // //
  427. ///////////////////////////////////////////////////////////////////////////////
  428. // RegionInfoBase implementation
  429. //
  430. template <class Tr>
  431. RegionInfoBase<Tr>::RegionInfoBase()
  432. : TopLevelRegion(nullptr) {}
  433. template <class Tr>
  434. RegionInfoBase<Tr>::~RegionInfoBase() {
  435. releaseMemory();
  436. }
  437. template <class Tr>
  438. bool RegionInfoBase<Tr>::isCommonDomFrontier(BlockT *BB, BlockT *entry,
  439. BlockT *exit) const {
  440. for (PredIterTy PI = InvBlockTraits::child_begin(BB),
  441. PE = InvBlockTraits::child_end(BB);
  442. PI != PE; ++PI) {
  443. BlockT *P = *PI;
  444. if (DT->dominates(entry, P) && !DT->dominates(exit, P))
  445. return false;
  446. }
  447. return true;
  448. }
  449. template <class Tr>
  450. bool RegionInfoBase<Tr>::isRegion(BlockT *entry, BlockT *exit) const {
  451. assert(entry && exit && "entry and exit must not be null!");
  452. typedef typename DomFrontierT::DomSetType DST;
  453. DST *entrySuccs = &DF->find(entry)->second;
  454. // Exit is the header of a loop that contains the entry. In this case,
  455. // the dominance frontier must only contain the exit.
  456. if (!DT->dominates(entry, exit)) {
  457. for (typename DST::iterator SI = entrySuccs->begin(),
  458. SE = entrySuccs->end();
  459. SI != SE; ++SI) {
  460. if (*SI != exit && *SI != entry)
  461. return false;
  462. }
  463. return true;
  464. }
  465. DST *exitSuccs = &DF->find(exit)->second;
  466. // Do not allow edges leaving the region.
  467. for (typename DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
  468. SI != SE; ++SI) {
  469. if (*SI == exit || *SI == entry)
  470. continue;
  471. if (exitSuccs->find(*SI) == exitSuccs->end())
  472. return false;
  473. if (!isCommonDomFrontier(*SI, entry, exit))
  474. return false;
  475. }
  476. // Do not allow edges pointing into the region.
  477. for (typename DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
  478. SI != SE; ++SI) {
  479. if (DT->properlyDominates(entry, *SI) && *SI != exit)
  480. return false;
  481. }
  482. return true;
  483. }
  484. template <class Tr>
  485. void RegionInfoBase<Tr>::insertShortCut(BlockT *entry, BlockT *exit,
  486. BBtoBBMap *ShortCut) const {
  487. assert(entry && exit && "entry and exit must not be null!");
  488. typename BBtoBBMap::iterator e = ShortCut->find(exit);
  489. if (e == ShortCut->end())
  490. // No further region at exit available.
  491. (*ShortCut)[entry] = exit;
  492. else {
  493. // We found a region e that starts at exit. Therefore (entry, e->second)
  494. // is also a region, that is larger than (entry, exit). Insert the
  495. // larger one.
  496. BlockT *BB = e->second;
  497. (*ShortCut)[entry] = BB;
  498. }
  499. }
  500. template <class Tr>
  501. typename Tr::DomTreeNodeT *
  502. RegionInfoBase<Tr>::getNextPostDom(DomTreeNodeT *N, BBtoBBMap *ShortCut) const {
  503. typename BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
  504. if (e == ShortCut->end())
  505. return N->getIDom();
  506. return PDT->getNode(e->second)->getIDom();
  507. }
  508. template <class Tr>
  509. bool RegionInfoBase<Tr>::isTrivialRegion(BlockT *entry, BlockT *exit) const {
  510. assert(entry && exit && "entry and exit must not be null!");
  511. unsigned num_successors =
  512. BlockTraits::child_end(entry) - BlockTraits::child_begin(entry);
  513. if (num_successors <= 1 && exit == *(BlockTraits::child_begin(entry)))
  514. return true;
  515. return false;
  516. }
  517. template <class Tr>
  518. typename Tr::RegionT *RegionInfoBase<Tr>::createRegion(BlockT *entry,
  519. BlockT *exit) {
  520. assert(entry && exit && "entry and exit must not be null!");
  521. if (isTrivialRegion(entry, exit))
  522. return nullptr;
  523. RegionT *region =
  524. new RegionT(entry, exit, static_cast<RegionInfoT *>(this), DT);
  525. BBtoRegion.insert(std::make_pair(entry, region));
  526. #ifdef XDEBUG
  527. region->verifyRegion();
  528. #else
  529. DEBUG(region->verifyRegion());
  530. #endif
  531. updateStatistics(region);
  532. return region;
  533. }
  534. template <class Tr>
  535. void RegionInfoBase<Tr>::findRegionsWithEntry(BlockT *entry,
  536. BBtoBBMap *ShortCut) {
  537. assert(entry);
  538. DomTreeNodeT *N = PDT->getNode(entry);
  539. if (!N)
  540. return;
  541. RegionT *lastRegion = nullptr;
  542. BlockT *lastExit = entry;
  543. // As only a BasicBlock that postdominates entry can finish a region, walk the
  544. // post dominance tree upwards.
  545. while ((N = getNextPostDom(N, ShortCut))) {
  546. BlockT *exit = N->getBlock();
  547. if (!exit)
  548. break;
  549. if (isRegion(entry, exit)) {
  550. RegionT *newRegion = createRegion(entry, exit);
  551. if (lastRegion)
  552. newRegion->addSubRegion(lastRegion);
  553. lastRegion = newRegion;
  554. lastExit = exit;
  555. }
  556. // This can never be a region, so stop the search.
  557. if (!DT->dominates(entry, exit))
  558. break;
  559. }
  560. // Tried to create regions from entry to lastExit. Next time take a
  561. // shortcut from entry to lastExit.
  562. if (lastExit != entry)
  563. insertShortCut(entry, lastExit, ShortCut);
  564. }
  565. template <class Tr>
  566. void RegionInfoBase<Tr>::scanForRegions(FuncT &F, BBtoBBMap *ShortCut) {
  567. typedef typename std::add_pointer<FuncT>::type FuncPtrT;
  568. BlockT *entry = GraphTraits<FuncPtrT>::getEntryNode(&F);
  569. DomTreeNodeT *N = DT->getNode(entry);
  570. // Iterate over the dominance tree in post order to start with the small
  571. // regions from the bottom of the dominance tree. If the small regions are
  572. // detected first, detection of bigger regions is faster, as we can jump
  573. // over the small regions.
  574. for (auto DomNode : post_order(N))
  575. findRegionsWithEntry(DomNode->getBlock(), ShortCut);
  576. }
  577. template <class Tr>
  578. typename Tr::RegionT *RegionInfoBase<Tr>::getTopMostParent(RegionT *region) {
  579. while (region->getParent())
  580. region = region->getParent();
  581. return region;
  582. }
  583. template <class Tr>
  584. void RegionInfoBase<Tr>::buildRegionsTree(DomTreeNodeT *N, RegionT *region) {
  585. BlockT *BB = N->getBlock();
  586. // Passed region exit
  587. while (BB == region->getExit())
  588. region = region->getParent();
  589. typename BBtoRegionMap::iterator it = BBtoRegion.find(BB);
  590. // This basic block is a start block of a region. It is already in the
  591. // BBtoRegion relation. Only the child basic blocks have to be updated.
  592. if (it != BBtoRegion.end()) {
  593. RegionT *newRegion = it->second;
  594. region->addSubRegion(getTopMostParent(newRegion));
  595. region = newRegion;
  596. } else {
  597. BBtoRegion[BB] = region;
  598. }
  599. for (typename DomTreeNodeT::iterator CI = N->begin(), CE = N->end(); CI != CE;
  600. ++CI) {
  601. buildRegionsTree(*CI, region);
  602. }
  603. }
  604. #ifdef XDEBUG
  605. template <class Tr>
  606. bool RegionInfoBase<Tr>::VerifyRegionInfo = true;
  607. #else
  608. template <class Tr>
  609. bool RegionInfoBase<Tr>::VerifyRegionInfo = false;
  610. #endif
  611. template <class Tr>
  612. typename Tr::RegionT::PrintStyle RegionInfoBase<Tr>::printStyle =
  613. RegionBase<Tr>::PrintNone;
  614. template <class Tr>
  615. void RegionInfoBase<Tr>::print(raw_ostream &OS) const {
  616. OS << "Region tree:\n";
  617. TopLevelRegion->print(OS, true, 0, printStyle);
  618. OS << "End region tree\n";
  619. }
  620. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  621. template <class Tr>
  622. void RegionInfoBase<Tr>::dump() const { print(dbgs()); }
  623. #endif
  624. template <class Tr>
  625. void RegionInfoBase<Tr>::releaseMemory() {
  626. BBtoRegion.clear();
  627. if (TopLevelRegion)
  628. delete TopLevelRegion;
  629. TopLevelRegion = nullptr;
  630. }
  631. template <class Tr>
  632. void RegionInfoBase<Tr>::verifyAnalysis() const {
  633. TopLevelRegion->verifyRegionNest();
  634. }
  635. // Region pass manager support.
  636. template <class Tr>
  637. typename Tr::RegionT *RegionInfoBase<Tr>::getRegionFor(BlockT *BB) const {
  638. typename BBtoRegionMap::const_iterator I = BBtoRegion.find(BB);
  639. return I != BBtoRegion.end() ? I->second : nullptr;
  640. }
  641. template <class Tr>
  642. void RegionInfoBase<Tr>::setRegionFor(BlockT *BB, RegionT *R) {
  643. BBtoRegion[BB] = R;
  644. }
  645. template <class Tr>
  646. typename Tr::RegionT *RegionInfoBase<Tr>::operator[](BlockT *BB) const {
  647. return getRegionFor(BB);
  648. }
  649. template <class Tr>
  650. typename RegionInfoBase<Tr>::BlockT *
  651. RegionInfoBase<Tr>::getMaxRegionExit(BlockT *BB) const {
  652. BlockT *Exit = nullptr;
  653. while (true) {
  654. // Get largest region that starts at BB.
  655. RegionT *R = getRegionFor(BB);
  656. while (R && R->getParent() && R->getParent()->getEntry() == BB)
  657. R = R->getParent();
  658. // Get the single exit of BB.
  659. if (R && R->getEntry() == BB)
  660. Exit = R->getExit();
  661. else if (++BlockTraits::child_begin(BB) == BlockTraits::child_end(BB))
  662. Exit = *BlockTraits::child_begin(BB);
  663. else // No single exit exists.
  664. return Exit;
  665. // Get largest region that starts at Exit.
  666. RegionT *ExitR = getRegionFor(Exit);
  667. while (ExitR && ExitR->getParent() &&
  668. ExitR->getParent()->getEntry() == Exit)
  669. ExitR = ExitR->getParent();
  670. for (PredIterTy PI = InvBlockTraits::child_begin(Exit),
  671. PE = InvBlockTraits::child_end(Exit);
  672. PI != PE; ++PI) {
  673. if (!R->contains(*PI) && !ExitR->contains(*PI))
  674. break;
  675. }
  676. // This stops infinite cycles.
  677. if (DT->dominates(Exit, BB))
  678. break;
  679. BB = Exit;
  680. }
  681. return Exit;
  682. }
  683. template <class Tr>
  684. typename Tr::RegionT *RegionInfoBase<Tr>::getCommonRegion(RegionT *A,
  685. RegionT *B) const {
  686. assert(A && B && "One of the Regions is NULL");
  687. if (A->contains(B))
  688. return A;
  689. while (!B->contains(A))
  690. B = B->getParent();
  691. return B;
  692. }
  693. template <class Tr>
  694. typename Tr::RegionT *
  695. RegionInfoBase<Tr>::getCommonRegion(SmallVectorImpl<RegionT *> &Regions) const {
  696. RegionT *ret = Regions.back();
  697. Regions.pop_back();
  698. for (RegionT *R : Regions)
  699. ret = getCommonRegion(ret, R);
  700. return ret;
  701. }
  702. template <class Tr>
  703. typename Tr::RegionT *
  704. RegionInfoBase<Tr>::getCommonRegion(SmallVectorImpl<BlockT *> &BBs) const {
  705. RegionT *ret = getRegionFor(BBs.back());
  706. BBs.pop_back();
  707. for (BlockT *BB : BBs)
  708. ret = getCommonRegion(ret, getRegionFor(BB));
  709. return ret;
  710. }
  711. template <class Tr>
  712. void RegionInfoBase<Tr>::splitBlock(BlockT *NewBB, BlockT *OldBB) {
  713. RegionT *R = getRegionFor(OldBB);
  714. setRegionFor(NewBB, R);
  715. while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
  716. R->replaceEntry(NewBB);
  717. R = R->getParent();
  718. }
  719. setRegionFor(OldBB, R);
  720. }
  721. template <class Tr>
  722. void RegionInfoBase<Tr>::calculate(FuncT &F) {
  723. typedef typename std::add_pointer<FuncT>::type FuncPtrT;
  724. // ShortCut a function where for every BB the exit of the largest region
  725. // starting with BB is stored. These regions can be threated as single BBS.
  726. // This improves performance on linear CFGs.
  727. BBtoBBMap ShortCut;
  728. scanForRegions(F, &ShortCut);
  729. BlockT *BB = GraphTraits<FuncPtrT>::getEntryNode(&F);
  730. buildRegionsTree(DT->getNode(BB), TopLevelRegion);
  731. }
  732. #undef DEBUG_TYPE
  733. } // end namespace llvm
  734. #endif