DominatorTreeTest.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. //===- llvm/unittests/IR/DominatorTreeTest.cpp - Constants unit tests -----===//
  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. #include <random>
  9. #include "llvm/Analysis/PostDominators.h"
  10. #include "llvm/Analysis/IteratedDominanceFrontier.h"
  11. #include "llvm/AsmParser/Parser.h"
  12. #include "llvm/IR/Constants.h"
  13. #include "llvm/IR/Dominators.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Support/SourceMgr.h"
  18. #include "CFGBuilder.h"
  19. #include "gtest/gtest.h"
  20. using namespace llvm;
  21. /// Build the dominator tree for the function and run the Test.
  22. static void runWithDomTree(
  23. Module &M, StringRef FuncName,
  24. function_ref<void(Function &F, DominatorTree *DT, PostDominatorTree *PDT)>
  25. Test) {
  26. auto *F = M.getFunction(FuncName);
  27. ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
  28. // Compute the dominator tree for the function.
  29. DominatorTree DT(*F);
  30. PostDominatorTree PDT(*F);
  31. Test(*F, &DT, &PDT);
  32. }
  33. static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
  34. StringRef ModuleStr) {
  35. SMDiagnostic Err;
  36. std::unique_ptr<Module> M = parseAssemblyString(ModuleStr, Err, Context);
  37. assert(M && "Bad assembly?");
  38. return M;
  39. }
  40. TEST(DominatorTree, Unreachable) {
  41. StringRef ModuleString =
  42. "declare i32 @g()\n"
  43. "define void @f(i32 %x) personality i32 ()* @g {\n"
  44. "bb0:\n"
  45. " %y1 = add i32 %x, 1\n"
  46. " %y2 = add i32 %x, 1\n"
  47. " %y3 = invoke i32 @g() to label %bb1 unwind label %bb2\n"
  48. "bb1:\n"
  49. " %y4 = add i32 %x, 1\n"
  50. " br label %bb4\n"
  51. "bb2:\n"
  52. " %y5 = landingpad i32\n"
  53. " cleanup\n"
  54. " br label %bb4\n"
  55. "bb3:\n"
  56. " %y6 = add i32 %x, 1\n"
  57. " %y7 = add i32 %x, 1\n"
  58. " ret void\n"
  59. "bb4:\n"
  60. " %y8 = phi i32 [0, %bb2], [%y4, %bb1]\n"
  61. " %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n"
  62. " ret void\n"
  63. "}\n";
  64. // Parse the module.
  65. LLVMContext Context;
  66. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
  67. runWithDomTree(
  68. *M, "f", [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
  69. Function::iterator FI = F.begin();
  70. BasicBlock *BB0 = &*FI++;
  71. BasicBlock::iterator BBI = BB0->begin();
  72. Instruction *Y1 = &*BBI++;
  73. Instruction *Y2 = &*BBI++;
  74. Instruction *Y3 = &*BBI++;
  75. BasicBlock *BB1 = &*FI++;
  76. BBI = BB1->begin();
  77. Instruction *Y4 = &*BBI++;
  78. BasicBlock *BB2 = &*FI++;
  79. BBI = BB2->begin();
  80. Instruction *Y5 = &*BBI++;
  81. BasicBlock *BB3 = &*FI++;
  82. BBI = BB3->begin();
  83. Instruction *Y6 = &*BBI++;
  84. Instruction *Y7 = &*BBI++;
  85. BasicBlock *BB4 = &*FI++;
  86. BBI = BB4->begin();
  87. Instruction *Y8 = &*BBI++;
  88. Instruction *Y9 = &*BBI++;
  89. // Reachability
  90. EXPECT_TRUE(DT->isReachableFromEntry(BB0));
  91. EXPECT_TRUE(DT->isReachableFromEntry(BB1));
  92. EXPECT_TRUE(DT->isReachableFromEntry(BB2));
  93. EXPECT_FALSE(DT->isReachableFromEntry(BB3));
  94. EXPECT_TRUE(DT->isReachableFromEntry(BB4));
  95. // BB dominance
  96. EXPECT_TRUE(DT->dominates(BB0, BB0));
  97. EXPECT_TRUE(DT->dominates(BB0, BB1));
  98. EXPECT_TRUE(DT->dominates(BB0, BB2));
  99. EXPECT_TRUE(DT->dominates(BB0, BB3));
  100. EXPECT_TRUE(DT->dominates(BB0, BB4));
  101. EXPECT_FALSE(DT->dominates(BB1, BB0));
  102. EXPECT_TRUE(DT->dominates(BB1, BB1));
  103. EXPECT_FALSE(DT->dominates(BB1, BB2));
  104. EXPECT_TRUE(DT->dominates(BB1, BB3));
  105. EXPECT_FALSE(DT->dominates(BB1, BB4));
  106. EXPECT_FALSE(DT->dominates(BB2, BB0));
  107. EXPECT_FALSE(DT->dominates(BB2, BB1));
  108. EXPECT_TRUE(DT->dominates(BB2, BB2));
  109. EXPECT_TRUE(DT->dominates(BB2, BB3));
  110. EXPECT_FALSE(DT->dominates(BB2, BB4));
  111. EXPECT_FALSE(DT->dominates(BB3, BB0));
  112. EXPECT_FALSE(DT->dominates(BB3, BB1));
  113. EXPECT_FALSE(DT->dominates(BB3, BB2));
  114. EXPECT_TRUE(DT->dominates(BB3, BB3));
  115. EXPECT_FALSE(DT->dominates(BB3, BB4));
  116. // BB proper dominance
  117. EXPECT_FALSE(DT->properlyDominates(BB0, BB0));
  118. EXPECT_TRUE(DT->properlyDominates(BB0, BB1));
  119. EXPECT_TRUE(DT->properlyDominates(BB0, BB2));
  120. EXPECT_TRUE(DT->properlyDominates(BB0, BB3));
  121. EXPECT_FALSE(DT->properlyDominates(BB1, BB0));
  122. EXPECT_FALSE(DT->properlyDominates(BB1, BB1));
  123. EXPECT_FALSE(DT->properlyDominates(BB1, BB2));
  124. EXPECT_TRUE(DT->properlyDominates(BB1, BB3));
  125. EXPECT_FALSE(DT->properlyDominates(BB2, BB0));
  126. EXPECT_FALSE(DT->properlyDominates(BB2, BB1));
  127. EXPECT_FALSE(DT->properlyDominates(BB2, BB2));
  128. EXPECT_TRUE(DT->properlyDominates(BB2, BB3));
  129. EXPECT_FALSE(DT->properlyDominates(BB3, BB0));
  130. EXPECT_FALSE(DT->properlyDominates(BB3, BB1));
  131. EXPECT_FALSE(DT->properlyDominates(BB3, BB2));
  132. EXPECT_FALSE(DT->properlyDominates(BB3, BB3));
  133. // Instruction dominance in the same reachable BB
  134. EXPECT_FALSE(DT->dominates(Y1, Y1));
  135. EXPECT_TRUE(DT->dominates(Y1, Y2));
  136. EXPECT_FALSE(DT->dominates(Y2, Y1));
  137. EXPECT_FALSE(DT->dominates(Y2, Y2));
  138. // Instruction dominance in the same unreachable BB
  139. EXPECT_TRUE(DT->dominates(Y6, Y6));
  140. EXPECT_TRUE(DT->dominates(Y6, Y7));
  141. EXPECT_TRUE(DT->dominates(Y7, Y6));
  142. EXPECT_TRUE(DT->dominates(Y7, Y7));
  143. // Invoke
  144. EXPECT_TRUE(DT->dominates(Y3, Y4));
  145. EXPECT_FALSE(DT->dominates(Y3, Y5));
  146. // Phi
  147. EXPECT_TRUE(DT->dominates(Y2, Y9));
  148. EXPECT_FALSE(DT->dominates(Y3, Y9));
  149. EXPECT_FALSE(DT->dominates(Y8, Y9));
  150. // Anything dominates unreachable
  151. EXPECT_TRUE(DT->dominates(Y1, Y6));
  152. EXPECT_TRUE(DT->dominates(Y3, Y6));
  153. // Unreachable doesn't dominate reachable
  154. EXPECT_FALSE(DT->dominates(Y6, Y1));
  155. // Instruction, BB dominance
  156. EXPECT_FALSE(DT->dominates(Y1, BB0));
  157. EXPECT_TRUE(DT->dominates(Y1, BB1));
  158. EXPECT_TRUE(DT->dominates(Y1, BB2));
  159. EXPECT_TRUE(DT->dominates(Y1, BB3));
  160. EXPECT_TRUE(DT->dominates(Y1, BB4));
  161. EXPECT_FALSE(DT->dominates(Y3, BB0));
  162. EXPECT_TRUE(DT->dominates(Y3, BB1));
  163. EXPECT_FALSE(DT->dominates(Y3, BB2));
  164. EXPECT_TRUE(DT->dominates(Y3, BB3));
  165. EXPECT_FALSE(DT->dominates(Y3, BB4));
  166. EXPECT_TRUE(DT->dominates(Y6, BB3));
  167. // Post dominance.
  168. EXPECT_TRUE(PDT->dominates(BB0, BB0));
  169. EXPECT_FALSE(PDT->dominates(BB1, BB0));
  170. EXPECT_FALSE(PDT->dominates(BB2, BB0));
  171. EXPECT_FALSE(PDT->dominates(BB3, BB0));
  172. EXPECT_TRUE(PDT->dominates(BB4, BB1));
  173. // Dominance descendants.
  174. SmallVector<BasicBlock *, 8> DominatedBBs, PostDominatedBBs;
  175. DT->getDescendants(BB0, DominatedBBs);
  176. PDT->getDescendants(BB0, PostDominatedBBs);
  177. EXPECT_EQ(DominatedBBs.size(), 4UL);
  178. EXPECT_EQ(PostDominatedBBs.size(), 1UL);
  179. // BB3 is unreachable. It should have no dominators nor postdominators.
  180. DominatedBBs.clear();
  181. PostDominatedBBs.clear();
  182. DT->getDescendants(BB3, DominatedBBs);
  183. DT->getDescendants(BB3, PostDominatedBBs);
  184. EXPECT_EQ(DominatedBBs.size(), 0UL);
  185. EXPECT_EQ(PostDominatedBBs.size(), 0UL);
  186. // Check DFS Numbers before
  187. DT->updateDFSNumbers();
  188. EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
  189. EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 7UL);
  190. EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
  191. EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 2UL);
  192. EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 5UL);
  193. EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 6UL);
  194. EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 3UL);
  195. EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 4UL);
  196. // Check levels before
  197. EXPECT_EQ(DT->getNode(BB0)->getLevel(), 0U);
  198. EXPECT_EQ(DT->getNode(BB1)->getLevel(), 1U);
  199. EXPECT_EQ(DT->getNode(BB2)->getLevel(), 1U);
  200. EXPECT_EQ(DT->getNode(BB4)->getLevel(), 1U);
  201. // Reattach block 3 to block 1 and recalculate
  202. BB1->getTerminator()->eraseFromParent();
  203. BranchInst::Create(BB4, BB3, ConstantInt::getTrue(F.getContext()), BB1);
  204. DT->recalculate(F);
  205. // Check DFS Numbers after
  206. DT->updateDFSNumbers();
  207. EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
  208. EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 9UL);
  209. EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
  210. EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 4UL);
  211. EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 7UL);
  212. EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 8UL);
  213. EXPECT_EQ(DT->getNode(BB3)->getDFSNumIn(), 2UL);
  214. EXPECT_EQ(DT->getNode(BB3)->getDFSNumOut(), 3UL);
  215. EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 5UL);
  216. EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 6UL);
  217. // Check levels after
  218. EXPECT_EQ(DT->getNode(BB0)->getLevel(), 0U);
  219. EXPECT_EQ(DT->getNode(BB1)->getLevel(), 1U);
  220. EXPECT_EQ(DT->getNode(BB2)->getLevel(), 1U);
  221. EXPECT_EQ(DT->getNode(BB3)->getLevel(), 2U);
  222. EXPECT_EQ(DT->getNode(BB4)->getLevel(), 1U);
  223. // Change root node
  224. EXPECT_TRUE(DT->verify());
  225. BasicBlock *NewEntry =
  226. BasicBlock::Create(F.getContext(), "new_entry", &F, BB0);
  227. BranchInst::Create(BB0, NewEntry);
  228. EXPECT_EQ(F.begin()->getName(), NewEntry->getName());
  229. EXPECT_TRUE(&F.getEntryBlock() == NewEntry);
  230. DT->setNewRoot(NewEntry);
  231. EXPECT_TRUE(DT->verify());
  232. });
  233. }
  234. TEST(DominatorTree, NonUniqueEdges) {
  235. StringRef ModuleString =
  236. "define i32 @f(i32 %i, i32 *%p) {\n"
  237. "bb0:\n"
  238. " store i32 %i, i32 *%p\n"
  239. " switch i32 %i, label %bb2 [\n"
  240. " i32 0, label %bb1\n"
  241. " i32 1, label %bb1\n"
  242. " ]\n"
  243. " bb1:\n"
  244. " ret i32 1\n"
  245. " bb2:\n"
  246. " ret i32 4\n"
  247. "}\n";
  248. // Parse the module.
  249. LLVMContext Context;
  250. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
  251. runWithDomTree(
  252. *M, "f", [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
  253. Function::iterator FI = F.begin();
  254. BasicBlock *BB0 = &*FI++;
  255. BasicBlock *BB1 = &*FI++;
  256. BasicBlock *BB2 = &*FI++;
  257. const Instruction *TI = BB0->getTerminator();
  258. assert(TI->getNumSuccessors() == 3 && "Switch has three successors");
  259. BasicBlockEdge Edge_BB0_BB2(BB0, TI->getSuccessor(0));
  260. assert(Edge_BB0_BB2.getEnd() == BB2 &&
  261. "Default label is the 1st successor");
  262. BasicBlockEdge Edge_BB0_BB1_a(BB0, TI->getSuccessor(1));
  263. assert(Edge_BB0_BB1_a.getEnd() == BB1 && "BB1 is the 2nd successor");
  264. BasicBlockEdge Edge_BB0_BB1_b(BB0, TI->getSuccessor(2));
  265. assert(Edge_BB0_BB1_b.getEnd() == BB1 && "BB1 is the 3rd successor");
  266. EXPECT_TRUE(DT->dominates(Edge_BB0_BB2, BB2));
  267. EXPECT_FALSE(DT->dominates(Edge_BB0_BB2, BB1));
  268. EXPECT_FALSE(DT->dominates(Edge_BB0_BB1_a, BB1));
  269. EXPECT_FALSE(DT->dominates(Edge_BB0_BB1_b, BB1));
  270. EXPECT_FALSE(DT->dominates(Edge_BB0_BB1_a, BB2));
  271. EXPECT_FALSE(DT->dominates(Edge_BB0_BB1_b, BB2));
  272. });
  273. }
  274. // Verify that the PDT is correctly updated in case an edge removal results
  275. // in a new unreachable CFG node. Also make sure that the updated PDT is the
  276. // same as a freshly recalculated one.
  277. //
  278. // For the following input code and initial PDT:
  279. //
  280. // CFG PDT
  281. //
  282. // A Exit
  283. // | |
  284. // _B D
  285. // / | \ |
  286. // ^ v \ B
  287. // \ / D / \
  288. // C \ C A
  289. // v
  290. // Exit
  291. //
  292. // we verify that CFG' and PDT-updated is obtained after removal of edge C -> B.
  293. //
  294. // CFG' PDT-updated
  295. //
  296. // A Exit
  297. // | / | \
  298. // B C B D
  299. // | \ |
  300. // v \ A
  301. // / D
  302. // C \
  303. // | \
  304. // unreachable Exit
  305. //
  306. // Both the blocks that end with ret and with unreachable become trivial
  307. // PostDominatorTree roots, as they have no successors.
  308. //
  309. TEST(DominatorTree, DeletingEdgesIntroducesUnreachables) {
  310. StringRef ModuleString =
  311. "define void @f() {\n"
  312. "A:\n"
  313. " br label %B\n"
  314. "B:\n"
  315. " br i1 undef, label %D, label %C\n"
  316. "C:\n"
  317. " br label %B\n"
  318. "D:\n"
  319. " ret void\n"
  320. "}\n";
  321. // Parse the module.
  322. LLVMContext Context;
  323. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
  324. runWithDomTree(
  325. *M, "f", [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
  326. Function::iterator FI = F.begin();
  327. FI++;
  328. BasicBlock *B = &*FI++;
  329. BasicBlock *C = &*FI++;
  330. BasicBlock *D = &*FI++;
  331. ASSERT_TRUE(PDT->dominates(PDT->getNode(D), PDT->getNode(B)));
  332. EXPECT_TRUE(DT->verify());
  333. EXPECT_TRUE(PDT->verify());
  334. C->getTerminator()->eraseFromParent();
  335. new UnreachableInst(C->getContext(), C);
  336. DT->deleteEdge(C, B);
  337. PDT->deleteEdge(C, B);
  338. EXPECT_TRUE(DT->verify());
  339. EXPECT_TRUE(PDT->verify());
  340. EXPECT_FALSE(PDT->dominates(PDT->getNode(D), PDT->getNode(B)));
  341. EXPECT_NE(PDT->getNode(C), nullptr);
  342. DominatorTree NDT(F);
  343. EXPECT_EQ(DT->compare(NDT), 0);
  344. PostDominatorTree NPDT(F);
  345. EXPECT_EQ(PDT->compare(NPDT), 0);
  346. });
  347. }
  348. // Verify that the PDT is correctly updated in case an edge removal results
  349. // in an infinite loop. Also make sure that the updated PDT is the
  350. // same as a freshly recalculated one.
  351. //
  352. // Test case:
  353. //
  354. // CFG PDT
  355. //
  356. // A Exit
  357. // | |
  358. // _B D
  359. // / | \ |
  360. // ^ v \ B
  361. // \ / D / \
  362. // C \ C A
  363. // / \ v
  364. // ^ v Exit
  365. // \_/
  366. //
  367. // After deleting the edge C->B, C is part of an infinite reverse-unreachable
  368. // loop:
  369. //
  370. // CFG' PDT'
  371. //
  372. // A Exit
  373. // | / | \
  374. // B C B D
  375. // | \ |
  376. // v \ A
  377. // / D
  378. // C \
  379. // / \ v
  380. // ^ v Exit
  381. // \_/
  382. //
  383. // As C now becomes reverse-unreachable, it forms a new non-trivial root and
  384. // gets connected to the virtual exit.
  385. // D does not postdominate B anymore, because there are two forward paths from
  386. // B to the virtual exit:
  387. // - B -> C -> VirtualExit
  388. // - B -> D -> VirtualExit.
  389. //
  390. TEST(DominatorTree, DeletingEdgesIntroducesInfiniteLoop) {
  391. StringRef ModuleString =
  392. "define void @f() {\n"
  393. "A:\n"
  394. " br label %B\n"
  395. "B:\n"
  396. " br i1 undef, label %D, label %C\n"
  397. "C:\n"
  398. " switch i32 undef, label %C [\n"
  399. " i32 0, label %B\n"
  400. " ]\n"
  401. "D:\n"
  402. " ret void\n"
  403. "}\n";
  404. // Parse the module.
  405. LLVMContext Context;
  406. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
  407. runWithDomTree(
  408. *M, "f", [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
  409. Function::iterator FI = F.begin();
  410. FI++;
  411. BasicBlock *B = &*FI++;
  412. BasicBlock *C = &*FI++;
  413. BasicBlock *D = &*FI++;
  414. ASSERT_TRUE(PDT->dominates(PDT->getNode(D), PDT->getNode(B)));
  415. EXPECT_TRUE(DT->verify());
  416. EXPECT_TRUE(PDT->verify());
  417. auto SwitchC = cast<SwitchInst>(C->getTerminator());
  418. SwitchC->removeCase(SwitchC->case_begin());
  419. DT->deleteEdge(C, B);
  420. EXPECT_TRUE(DT->verify());
  421. PDT->deleteEdge(C, B);
  422. EXPECT_TRUE(PDT->verify());
  423. EXPECT_FALSE(PDT->dominates(PDT->getNode(D), PDT->getNode(B)));
  424. EXPECT_NE(PDT->getNode(C), nullptr);
  425. DominatorTree NDT(F);
  426. EXPECT_EQ(DT->compare(NDT), 0);
  427. PostDominatorTree NPDT(F);
  428. EXPECT_EQ(PDT->compare(NPDT), 0);
  429. });
  430. }
  431. // Verify that the PDT is correctly updated in case an edge removal results
  432. // in an infinite loop.
  433. //
  434. // Test case:
  435. //
  436. // CFG PDT
  437. //
  438. // A Exit
  439. // | / | \
  440. // B-- C2 B D
  441. // | \ / |
  442. // v \ C A
  443. // / D
  444. // C--C2 \
  445. // / \ \ v
  446. // ^ v --Exit
  447. // \_/
  448. //
  449. // After deleting the edge C->E, C is part of an infinite reverse-unreachable
  450. // loop:
  451. //
  452. // CFG' PDT'
  453. //
  454. // A Exit
  455. // | / | \
  456. // B C B D
  457. // | \ |
  458. // v \ A
  459. // / D
  460. // C \
  461. // / \ v
  462. // ^ v Exit
  463. // \_/
  464. //
  465. // In PDT, D does not post-dominate B. After the edge C -> C2 is removed,
  466. // C becomes a new nontrivial PDT root.
  467. //
  468. TEST(DominatorTree, DeletingEdgesIntroducesInfiniteLoop2) {
  469. StringRef ModuleString =
  470. "define void @f() {\n"
  471. "A:\n"
  472. " br label %B\n"
  473. "B:\n"
  474. " br i1 undef, label %D, label %C\n"
  475. "C:\n"
  476. " switch i32 undef, label %C [\n"
  477. " i32 0, label %C2\n"
  478. " ]\n"
  479. "C2:\n"
  480. " ret void\n"
  481. "D:\n"
  482. " ret void\n"
  483. "}\n";
  484. // Parse the module.
  485. LLVMContext Context;
  486. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
  487. runWithDomTree(
  488. *M, "f", [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
  489. Function::iterator FI = F.begin();
  490. FI++;
  491. BasicBlock *B = &*FI++;
  492. BasicBlock *C = &*FI++;
  493. BasicBlock *C2 = &*FI++;
  494. BasicBlock *D = &*FI++;
  495. EXPECT_TRUE(DT->verify());
  496. EXPECT_TRUE(PDT->verify());
  497. auto SwitchC = cast<SwitchInst>(C->getTerminator());
  498. SwitchC->removeCase(SwitchC->case_begin());
  499. DT->deleteEdge(C, C2);
  500. PDT->deleteEdge(C, C2);
  501. C2->removeFromParent();
  502. EXPECT_EQ(DT->getNode(C2), nullptr);
  503. PDT->eraseNode(C2);
  504. delete C2;
  505. EXPECT_TRUE(DT->verify());
  506. EXPECT_TRUE(PDT->verify());
  507. EXPECT_FALSE(PDT->dominates(PDT->getNode(D), PDT->getNode(B)));
  508. EXPECT_NE(PDT->getNode(C), nullptr);
  509. DominatorTree NDT(F);
  510. EXPECT_EQ(DT->compare(NDT), 0);
  511. PostDominatorTree NPDT(F);
  512. EXPECT_EQ(PDT->compare(NPDT), 0);
  513. });
  514. }
  515. // Verify that the IDF returns blocks in a deterministic way.
  516. //
  517. // Test case:
  518. //
  519. // CFG
  520. //
  521. // (A)
  522. // / \
  523. // / \
  524. // (B) (C)
  525. // |\ /|
  526. // | X |
  527. // |/ \|
  528. // (D) (E)
  529. //
  530. // IDF for block B is {D, E}, and the order of blocks in this list is defined by
  531. // their 1) level in dom-tree and 2) DFSIn number if the level is the same.
  532. //
  533. TEST(DominatorTree, IDFDeterminismTest) {
  534. StringRef ModuleString =
  535. "define void @f() {\n"
  536. "A:\n"
  537. " br i1 undef, label %B, label %C\n"
  538. "B:\n"
  539. " br i1 undef, label %D, label %E\n"
  540. "C:\n"
  541. " br i1 undef, label %D, label %E\n"
  542. "D:\n"
  543. " ret void\n"
  544. "E:\n"
  545. " ret void\n"
  546. "}\n";
  547. // Parse the module.
  548. LLVMContext Context;
  549. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
  550. runWithDomTree(
  551. *M, "f", [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
  552. Function::iterator FI = F.begin();
  553. BasicBlock *A = &*FI++;
  554. BasicBlock *B = &*FI++;
  555. BasicBlock *C = &*FI++;
  556. BasicBlock *D = &*FI++;
  557. BasicBlock *E = &*FI++;
  558. (void)C;
  559. DT->updateDFSNumbers();
  560. ForwardIDFCalculator IDF(*DT);
  561. SmallPtrSet<BasicBlock *, 1> DefBlocks;
  562. DefBlocks.insert(B);
  563. IDF.setDefiningBlocks(DefBlocks);
  564. SmallVector<BasicBlock *, 32> IDFBlocks;
  565. SmallPtrSet<BasicBlock *, 32> LiveInBlocks;
  566. IDF.resetLiveInBlocks();
  567. IDF.calculate(IDFBlocks);
  568. EXPECT_EQ(IDFBlocks.size(), 2UL);
  569. EXPECT_EQ(DT->getNode(A)->getDFSNumIn(), 0UL);
  570. EXPECT_EQ(IDFBlocks[0], D);
  571. EXPECT_EQ(IDFBlocks[1], E);
  572. EXPECT_TRUE(DT->getNode(IDFBlocks[0])->getDFSNumIn() <
  573. DT->getNode(IDFBlocks[1])->getDFSNumIn());
  574. });
  575. }
  576. namespace {
  577. const auto Insert = CFGBuilder::ActionKind::Insert;
  578. const auto Delete = CFGBuilder::ActionKind::Delete;
  579. bool CompUpdates(const CFGBuilder::Update &A, const CFGBuilder::Update &B) {
  580. return std::tie(A.Action, A.Edge.From, A.Edge.To) <
  581. std::tie(B.Action, B.Edge.From, B.Edge.To);
  582. }
  583. } // namespace
  584. TEST(DominatorTree, InsertReachable) {
  585. CFGHolder Holder;
  586. std::vector<CFGBuilder::Arc> Arcs = {
  587. {"1", "2"}, {"2", "3"}, {"3", "4"}, {"4", "5"}, {"5", "6"}, {"5", "7"},
  588. {"3", "8"}, {"8", "9"}, {"9", "10"}, {"8", "11"}, {"11", "12"}};
  589. std::vector<CFGBuilder::Update> Updates = {{Insert, {"12", "10"}},
  590. {Insert, {"10", "9"}},
  591. {Insert, {"7", "6"}},
  592. {Insert, {"7", "5"}}};
  593. CFGBuilder B(Holder.F, Arcs, Updates);
  594. DominatorTree DT(*Holder.F);
  595. EXPECT_TRUE(DT.verify());
  596. PostDominatorTree PDT(*Holder.F);
  597. EXPECT_TRUE(PDT.verify());
  598. Optional<CFGBuilder::Update> LastUpdate;
  599. while ((LastUpdate = B.applyUpdate())) {
  600. EXPECT_EQ(LastUpdate->Action, Insert);
  601. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  602. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  603. DT.insertEdge(From, To);
  604. EXPECT_TRUE(DT.verify());
  605. PDT.insertEdge(From, To);
  606. EXPECT_TRUE(PDT.verify());
  607. }
  608. }
  609. TEST(DominatorTree, InsertReachable2) {
  610. CFGHolder Holder;
  611. std::vector<CFGBuilder::Arc> Arcs = {
  612. {"1", "2"}, {"2", "3"}, {"3", "4"}, {"4", "5"}, {"5", "6"}, {"5", "7"},
  613. {"7", "5"}, {"2", "8"}, {"8", "11"}, {"11", "12"}, {"12", "10"},
  614. {"10", "9"}, {"9", "10"}};
  615. std::vector<CFGBuilder::Update> Updates = {{Insert, {"10", "7"}}};
  616. CFGBuilder B(Holder.F, Arcs, Updates);
  617. DominatorTree DT(*Holder.F);
  618. EXPECT_TRUE(DT.verify());
  619. PostDominatorTree PDT(*Holder.F);
  620. EXPECT_TRUE(PDT.verify());
  621. Optional<CFGBuilder::Update> LastUpdate = B.applyUpdate();
  622. EXPECT_TRUE(LastUpdate);
  623. EXPECT_EQ(LastUpdate->Action, Insert);
  624. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  625. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  626. DT.insertEdge(From, To);
  627. EXPECT_TRUE(DT.verify());
  628. PDT.insertEdge(From, To);
  629. EXPECT_TRUE(PDT.verify());
  630. }
  631. TEST(DominatorTree, InsertUnreachable) {
  632. CFGHolder Holder;
  633. std::vector<CFGBuilder::Arc> Arcs = {{"1", "2"}, {"2", "3"}, {"3", "4"},
  634. {"5", "6"}, {"5", "7"}, {"3", "8"},
  635. {"9", "10"}, {"11", "12"}};
  636. std::vector<CFGBuilder::Update> Updates = {{Insert, {"4", "5"}},
  637. {Insert, {"8", "9"}},
  638. {Insert, {"10", "12"}},
  639. {Insert, {"10", "11"}}};
  640. CFGBuilder B(Holder.F, Arcs, Updates);
  641. DominatorTree DT(*Holder.F);
  642. EXPECT_TRUE(DT.verify());
  643. PostDominatorTree PDT(*Holder.F);
  644. EXPECT_TRUE(PDT.verify());
  645. Optional<CFGBuilder::Update> LastUpdate;
  646. while ((LastUpdate = B.applyUpdate())) {
  647. EXPECT_EQ(LastUpdate->Action, Insert);
  648. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  649. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  650. DT.insertEdge(From, To);
  651. EXPECT_TRUE(DT.verify());
  652. PDT.insertEdge(From, To);
  653. EXPECT_TRUE(PDT.verify());
  654. }
  655. }
  656. TEST(DominatorTree, InsertFromUnreachable) {
  657. CFGHolder Holder;
  658. std::vector<CFGBuilder::Arc> Arcs = {{"1", "2"}, {"2", "3"}, {"3", "4"}};
  659. std::vector<CFGBuilder::Update> Updates = {{Insert, {"3", "5"}}};
  660. CFGBuilder B(Holder.F, Arcs, Updates);
  661. PostDominatorTree PDT(*Holder.F);
  662. EXPECT_TRUE(PDT.verify());
  663. Optional<CFGBuilder::Update> LastUpdate = B.applyUpdate();
  664. EXPECT_TRUE(LastUpdate);
  665. EXPECT_EQ(LastUpdate->Action, Insert);
  666. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  667. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  668. PDT.insertEdge(From, To);
  669. EXPECT_TRUE(PDT.verify());
  670. EXPECT_TRUE(PDT.getRoots().size() == 2);
  671. // Make sure we can use a const pointer with getNode.
  672. const BasicBlock *BB5 = B.getOrAddBlock("5");
  673. EXPECT_NE(PDT.getNode(BB5), nullptr);
  674. }
  675. TEST(DominatorTree, InsertMixed) {
  676. CFGHolder Holder;
  677. std::vector<CFGBuilder::Arc> Arcs = {
  678. {"1", "2"}, {"2", "3"}, {"3", "4"}, {"5", "6"}, {"5", "7"},
  679. {"8", "9"}, {"9", "10"}, {"8", "11"}, {"11", "12"}, {"7", "3"}};
  680. std::vector<CFGBuilder::Update> Updates = {
  681. {Insert, {"4", "5"}}, {Insert, {"2", "5"}}, {Insert, {"10", "9"}},
  682. {Insert, {"12", "10"}}, {Insert, {"12", "10"}}, {Insert, {"7", "8"}},
  683. {Insert, {"7", "5"}}};
  684. CFGBuilder B(Holder.F, Arcs, Updates);
  685. DominatorTree DT(*Holder.F);
  686. EXPECT_TRUE(DT.verify());
  687. PostDominatorTree PDT(*Holder.F);
  688. EXPECT_TRUE(PDT.verify());
  689. Optional<CFGBuilder::Update> LastUpdate;
  690. while ((LastUpdate = B.applyUpdate())) {
  691. EXPECT_EQ(LastUpdate->Action, Insert);
  692. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  693. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  694. DT.insertEdge(From, To);
  695. EXPECT_TRUE(DT.verify());
  696. PDT.insertEdge(From, To);
  697. EXPECT_TRUE(PDT.verify());
  698. }
  699. }
  700. TEST(DominatorTree, InsertPermut) {
  701. std::vector<CFGBuilder::Arc> Arcs = {
  702. {"1", "2"}, {"2", "3"}, {"3", "4"}, {"5", "6"}, {"5", "7"},
  703. {"8", "9"}, {"9", "10"}, {"8", "11"}, {"11", "12"}, {"7", "3"}};
  704. std::vector<CFGBuilder::Update> Updates = {{Insert, {"4", "5"}},
  705. {Insert, {"2", "5"}},
  706. {Insert, {"10", "9"}},
  707. {Insert, {"12", "10"}}};
  708. while (std::next_permutation(Updates.begin(), Updates.end(), CompUpdates)) {
  709. CFGHolder Holder;
  710. CFGBuilder B(Holder.F, Arcs, Updates);
  711. DominatorTree DT(*Holder.F);
  712. EXPECT_TRUE(DT.verify());
  713. PostDominatorTree PDT(*Holder.F);
  714. EXPECT_TRUE(PDT.verify());
  715. Optional<CFGBuilder::Update> LastUpdate;
  716. while ((LastUpdate = B.applyUpdate())) {
  717. EXPECT_EQ(LastUpdate->Action, Insert);
  718. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  719. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  720. DT.insertEdge(From, To);
  721. EXPECT_TRUE(DT.verify());
  722. PDT.insertEdge(From, To);
  723. EXPECT_TRUE(PDT.verify());
  724. }
  725. }
  726. }
  727. TEST(DominatorTree, DeleteReachable) {
  728. CFGHolder Holder;
  729. std::vector<CFGBuilder::Arc> Arcs = {
  730. {"1", "2"}, {"2", "3"}, {"2", "4"}, {"3", "4"}, {"4", "5"}, {"5", "6"},
  731. {"5", "7"}, {"7", "8"}, {"3", "8"}, {"8", "9"}, {"9", "10"}, {"10", "2"}};
  732. std::vector<CFGBuilder::Update> Updates = {
  733. {Delete, {"2", "4"}}, {Delete, {"7", "8"}}, {Delete, {"10", "2"}}};
  734. CFGBuilder B(Holder.F, Arcs, Updates);
  735. DominatorTree DT(*Holder.F);
  736. EXPECT_TRUE(DT.verify());
  737. PostDominatorTree PDT(*Holder.F);
  738. EXPECT_TRUE(PDT.verify());
  739. Optional<CFGBuilder::Update> LastUpdate;
  740. while ((LastUpdate = B.applyUpdate())) {
  741. EXPECT_EQ(LastUpdate->Action, Delete);
  742. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  743. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  744. DT.deleteEdge(From, To);
  745. EXPECT_TRUE(DT.verify());
  746. PDT.deleteEdge(From, To);
  747. EXPECT_TRUE(PDT.verify());
  748. }
  749. }
  750. TEST(DominatorTree, DeleteUnreachable) {
  751. CFGHolder Holder;
  752. std::vector<CFGBuilder::Arc> Arcs = {
  753. {"1", "2"}, {"2", "3"}, {"3", "4"}, {"4", "5"}, {"5", "6"}, {"5", "7"},
  754. {"7", "8"}, {"3", "8"}, {"8", "9"}, {"9", "10"}, {"10", "2"}};
  755. std::vector<CFGBuilder::Update> Updates = {
  756. {Delete, {"8", "9"}}, {Delete, {"7", "8"}}, {Delete, {"3", "4"}}};
  757. CFGBuilder B(Holder.F, Arcs, Updates);
  758. DominatorTree DT(*Holder.F);
  759. EXPECT_TRUE(DT.verify());
  760. PostDominatorTree PDT(*Holder.F);
  761. EXPECT_TRUE(PDT.verify());
  762. Optional<CFGBuilder::Update> LastUpdate;
  763. while ((LastUpdate = B.applyUpdate())) {
  764. EXPECT_EQ(LastUpdate->Action, Delete);
  765. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  766. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  767. DT.deleteEdge(From, To);
  768. EXPECT_TRUE(DT.verify());
  769. PDT.deleteEdge(From, To);
  770. EXPECT_TRUE(PDT.verify());
  771. }
  772. }
  773. TEST(DominatorTree, InsertDelete) {
  774. std::vector<CFGBuilder::Arc> Arcs = {
  775. {"1", "2"}, {"2", "3"}, {"3", "4"}, {"4", "5"}, {"5", "6"}, {"5", "7"},
  776. {"3", "8"}, {"8", "9"}, {"9", "10"}, {"8", "11"}, {"11", "12"}};
  777. std::vector<CFGBuilder::Update> Updates = {
  778. {Insert, {"2", "4"}}, {Insert, {"12", "10"}}, {Insert, {"10", "9"}},
  779. {Insert, {"7", "6"}}, {Insert, {"7", "5"}}, {Delete, {"3", "8"}},
  780. {Insert, {"10", "7"}}, {Insert, {"2", "8"}}, {Delete, {"3", "4"}},
  781. {Delete, {"8", "9"}}, {Delete, {"11", "12"}}};
  782. CFGHolder Holder;
  783. CFGBuilder B(Holder.F, Arcs, Updates);
  784. DominatorTree DT(*Holder.F);
  785. EXPECT_TRUE(DT.verify());
  786. PostDominatorTree PDT(*Holder.F);
  787. EXPECT_TRUE(PDT.verify());
  788. Optional<CFGBuilder::Update> LastUpdate;
  789. while ((LastUpdate = B.applyUpdate())) {
  790. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  791. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  792. if (LastUpdate->Action == Insert) {
  793. DT.insertEdge(From, To);
  794. PDT.insertEdge(From, To);
  795. } else {
  796. DT.deleteEdge(From, To);
  797. PDT.deleteEdge(From, To);
  798. }
  799. EXPECT_TRUE(DT.verify());
  800. EXPECT_TRUE(PDT.verify());
  801. }
  802. }
  803. TEST(DominatorTree, InsertDeleteExhaustive) {
  804. std::vector<CFGBuilder::Arc> Arcs = {
  805. {"1", "2"}, {"2", "3"}, {"3", "4"}, {"4", "5"}, {"5", "6"}, {"5", "7"},
  806. {"3", "8"}, {"8", "9"}, {"9", "10"}, {"8", "11"}, {"11", "12"}};
  807. std::vector<CFGBuilder::Update> Updates = {
  808. {Insert, {"2", "4"}}, {Insert, {"12", "10"}}, {Insert, {"10", "9"}},
  809. {Insert, {"7", "6"}}, {Insert, {"7", "5"}}, {Delete, {"3", "8"}},
  810. {Insert, {"10", "7"}}, {Insert, {"2", "8"}}, {Delete, {"3", "4"}},
  811. {Delete, {"8", "9"}}, {Delete, {"11", "12"}}};
  812. std::mt19937 Generator(0);
  813. for (unsigned i = 0; i < 16; ++i) {
  814. std::shuffle(Updates.begin(), Updates.end(), Generator);
  815. CFGHolder Holder;
  816. CFGBuilder B(Holder.F, Arcs, Updates);
  817. DominatorTree DT(*Holder.F);
  818. EXPECT_TRUE(DT.verify());
  819. PostDominatorTree PDT(*Holder.F);
  820. EXPECT_TRUE(PDT.verify());
  821. Optional<CFGBuilder::Update> LastUpdate;
  822. while ((LastUpdate = B.applyUpdate())) {
  823. BasicBlock *From = B.getOrAddBlock(LastUpdate->Edge.From);
  824. BasicBlock *To = B.getOrAddBlock(LastUpdate->Edge.To);
  825. if (LastUpdate->Action == Insert) {
  826. DT.insertEdge(From, To);
  827. PDT.insertEdge(From, To);
  828. } else {
  829. DT.deleteEdge(From, To);
  830. PDT.deleteEdge(From, To);
  831. }
  832. EXPECT_TRUE(DT.verify());
  833. EXPECT_TRUE(PDT.verify());
  834. }
  835. }
  836. }
  837. TEST(DominatorTree, InsertIntoIrreducible) {
  838. std::vector<CFGBuilder::Arc> Arcs = {
  839. {"0", "1"},
  840. {"1", "27"}, {"1", "7"},
  841. {"10", "18"},
  842. {"13", "10"},
  843. {"18", "13"}, {"18", "23"},
  844. {"23", "13"}, {"23", "24"},
  845. {"24", "1"}, {"24", "18"},
  846. {"27", "24"}};
  847. CFGHolder Holder;
  848. CFGBuilder B(Holder.F, Arcs, {{Insert, {"7", "23"}}});
  849. DominatorTree DT(*Holder.F);
  850. EXPECT_TRUE(DT.verify());
  851. B.applyUpdate();
  852. BasicBlock *From = B.getOrAddBlock("7");
  853. BasicBlock *To = B.getOrAddBlock("23");
  854. DT.insertEdge(From, To);
  855. EXPECT_TRUE(DT.verify());
  856. }