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 
9 #include "llvm/Transforms/Utils/BasicBlockUtils.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 
17 using namespace llvm;
18 
19 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
20   SMDiagnostic Err;
21   std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
22   if (!Mod)
23     Err.print("BasicBlockUtilsTests", errs());
24   return Mod;
25 }
26 
27 TEST(BasicBlockUtils, SplitBlockPredecessors) {
28   LLVMContext C;
29 
30   std::unique_ptr<Module> M = parseIR(
31     C,
32     "define i32 @basic_func(i1 %cond) {\n"
33     "entry:\n"
34     "  br i1 %cond, label %bb0, label %bb1\n"
35     "bb0:\n"
36     "  br label %bb1\n"
37     "bb1:\n"
38     "  %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]"
39     "  ret i32 %phi\n"
40     "}\n"
41     "\n"
42     );
43 
44   auto *F = M->getFunction("basic_func");
45   DominatorTree DT(*F);
46 
47   // Make sure the dominator tree is properly updated if calling this on the
48   // entry block.
49   SplitBlockPredecessors(&F->getEntryBlock(), {}, "split.entry", &DT);
50   EXPECT_TRUE(DT.verify());
51 }
52