BasicBlockUtilsTest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //===- BasicBlockUtils.cpp - Unit tests for BasicBlockUtils ---------------===//
  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 "llvm/Transforms/Utils/BasicBlockUtils.h"
  9. #include "llvm/Analysis/PostDominators.h"
  10. #include "llvm/AsmParser/Parser.h"
  11. #include "llvm/IR/BasicBlock.h"
  12. #include "llvm/IR/Dominators.h"
  13. #include "llvm/IR/LLVMContext.h"
  14. #include "llvm/Support/SourceMgr.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
  18. SMDiagnostic Err;
  19. std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
  20. if (!Mod)
  21. Err.print("BasicBlockUtilsTests", errs());
  22. return Mod;
  23. }
  24. TEST(BasicBlockUtils, EliminateUnreachableBlocks) {
  25. LLVMContext C;
  26. std::unique_ptr<Module> M = parseIR(
  27. C,
  28. "define i32 @has_unreachable(i1 %cond) {\n"
  29. "entry:\n"
  30. " br i1 %cond, label %bb0, label %bb1\n"
  31. "bb0:\n"
  32. " br label %bb1\n"
  33. "bb1:\n"
  34. " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]"
  35. " ret i32 %phi\n"
  36. "bb2:\n"
  37. " ret i32 42\n"
  38. "}\n"
  39. "\n"
  40. );
  41. auto *F = M->getFunction("has_unreachable");
  42. DominatorTree DT(*F);
  43. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
  44. EXPECT_EQ(F->size(), (size_t)4);
  45. bool Result = EliminateUnreachableBlocks(*F, &DTU);
  46. EXPECT_TRUE(Result);
  47. EXPECT_EQ(F->size(), (size_t)3);
  48. EXPECT_TRUE(DT.verify());
  49. }
  50. TEST(BasicBlockUtils, NoUnreachableBlocksToEliminate) {
  51. LLVMContext C;
  52. std::unique_ptr<Module> M = parseIR(
  53. C,
  54. "define i32 @no_unreachable(i1 %cond) {\n"
  55. "entry:\n"
  56. " br i1 %cond, label %bb0, label %bb1\n"
  57. "bb0:\n"
  58. " br label %bb1\n"
  59. "bb1:\n"
  60. " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]"
  61. " ret i32 %phi\n"
  62. "}\n"
  63. "\n"
  64. );
  65. auto *F = M->getFunction("no_unreachable");
  66. DominatorTree DT(*F);
  67. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
  68. EXPECT_EQ(F->size(), (size_t)3);
  69. bool Result = EliminateUnreachableBlocks(*F, &DTU);
  70. EXPECT_FALSE(Result);
  71. EXPECT_EQ(F->size(), (size_t)3);
  72. EXPECT_TRUE(DT.verify());
  73. }
  74. TEST(BasicBlockUtils, SplitBlockPredecessors) {
  75. LLVMContext C;
  76. std::unique_ptr<Module> M = parseIR(
  77. C,
  78. "define i32 @basic_func(i1 %cond) {\n"
  79. "entry:\n"
  80. " br i1 %cond, label %bb0, label %bb1\n"
  81. "bb0:\n"
  82. " br label %bb1\n"
  83. "bb1:\n"
  84. " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]"
  85. " ret i32 %phi\n"
  86. "}\n"
  87. "\n"
  88. );
  89. auto *F = M->getFunction("basic_func");
  90. DominatorTree DT(*F);
  91. // Make sure the dominator tree is properly updated if calling this on the
  92. // entry block.
  93. SplitBlockPredecessors(&F->getEntryBlock(), {}, "split.entry", &DT);
  94. EXPECT_TRUE(DT.verify());
  95. }
  96. TEST(BasicBlockUtils, SplitCriticalEdge) {
  97. LLVMContext C;
  98. std::unique_ptr<Module> M = parseIR(
  99. C,
  100. "define void @crit_edge(i1 %cond0, i1 %cond1) {\n"
  101. "entry:\n"
  102. " br i1 %cond0, label %bb0, label %bb1\n"
  103. "bb0:\n"
  104. " br label %bb1\n"
  105. "bb1:\n"
  106. " br label %bb2\n"
  107. "bb2:\n"
  108. " ret void\n"
  109. "}\n"
  110. "\n"
  111. );
  112. auto *F = M->getFunction("crit_edge");
  113. DominatorTree DT(*F);
  114. PostDominatorTree PDT(*F);
  115. CriticalEdgeSplittingOptions CESO(&DT, nullptr, nullptr, &PDT);
  116. EXPECT_EQ(1u, SplitAllCriticalEdges(*F, CESO));
  117. EXPECT_TRUE(DT.verify());
  118. EXPECT_TRUE(PDT.verify());
  119. }