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/Analysis/PostDominators.h" 11 #include "llvm/AsmParser/Parser.h" 12 #include "llvm/IR/BasicBlock.h" 13 #include "llvm/IR/Dominators.h" 14 #include "llvm/IR/LLVMContext.h" 15 #include "llvm/Support/SourceMgr.h" 16 #include "gtest/gtest.h" 17 18 using namespace llvm; 19 20 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { 21 SMDiagnostic Err; 22 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); 23 if (!Mod) 24 Err.print("BasicBlockUtilsTests", errs()); 25 return Mod; 26 } 27 28 TEST(BasicBlockUtils, SplitBlockPredecessors) { 29 LLVMContext C; 30 31 std::unique_ptr<Module> M = parseIR( 32 C, 33 "define i32 @basic_func(i1 %cond) {\n" 34 "entry:\n" 35 " br i1 %cond, label %bb0, label %bb1\n" 36 "bb0:\n" 37 " br label %bb1\n" 38 "bb1:\n" 39 " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]" 40 " ret i32 %phi\n" 41 "}\n" 42 "\n" 43 ); 44 45 auto *F = M->getFunction("basic_func"); 46 DominatorTree DT(*F); 47 48 // Make sure the dominator tree is properly updated if calling this on the 49 // entry block. 50 SplitBlockPredecessors(&F->getEntryBlock(), {}, "split.entry", &DT); 51 EXPECT_TRUE(DT.verify()); 52 } 53 54 TEST(BasicBlockUtils, SplitCriticalEdge) { 55 LLVMContext C; 56 57 std::unique_ptr<Module> M = parseIR( 58 C, 59 "define void @crit_edge(i1 %cond0, i1 %cond1) {\n" 60 "entry:\n" 61 " br i1 %cond0, label %bb0, label %bb1\n" 62 "bb0:\n" 63 " br label %bb1\n" 64 "bb1:\n" 65 " br label %bb2\n" 66 "bb2:\n" 67 " ret void\n" 68 "}\n" 69 "\n" 70 ); 71 72 auto *F = M->getFunction("crit_edge"); 73 DominatorTree DT(*F); 74 PostDominatorTree PDT(*F); 75 76 CriticalEdgeSplittingOptions CESO(&DT, nullptr, nullptr, &PDT); 77 EXPECT_EQ(1u, SplitAllCriticalEdges(*F, CESO)); 78 EXPECT_TRUE(DT.verify()); 79 EXPECT_TRUE(PDT.verify()); 80 } 81