RegionInfoImpl.h 25 KB

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