1 //===- CodeExtractor.cpp - Unit tests for CodeExtractor -------------------===// 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/CodeExtractor.h" 10 #include "llvm/AsmParser/Parser.h" 11 #include "llvm/IR/BasicBlock.h" 12 #include "llvm/IR/Dominators.h" 13 #include "llvm/IR/Instructions.h" 14 #include "llvm/IR/LLVMContext.h" 15 #include "llvm/IR/Module.h" 16 #include "llvm/IR/Verifier.h" 17 #include "llvm/IRReader/IRReader.h" 18 #include "llvm/Support/SourceMgr.h" 19 #include "gtest/gtest.h" 20 21 using namespace llvm; 22 23 namespace { 24 BasicBlock *getBlockByName(Function *F, StringRef name) { 25 for (auto &BB : *F) 26 if (BB.getName() == name) 27 return &BB; 28 return nullptr; 29 } 30 31 TEST(CodeExtractor, ExitStub) { 32 LLVMContext Ctx; 33 SMDiagnostic Err; 34 std::unique_ptr<Module> M(parseAssemblyString(R"invalid( 35 define i32 @foo(i32 %x, i32 %y, i32 %z) { 36 header: 37 %0 = icmp ugt i32 %x, %y 38 br i1 %0, label %body1, label %body2 39 40 body1: 41 %1 = add i32 %z, 2 42 br label %notExtracted 43 44 body2: 45 %2 = mul i32 %z, 7 46 br label %notExtracted 47 48 notExtracted: 49 %3 = phi i32 [ %1, %body1 ], [ %2, %body2 ] 50 %4 = add i32 %3, %x 51 ret i32 %4 52 } 53 )invalid", 54 Err, Ctx)); 55 56 Function *Func = M->getFunction("foo"); 57 SmallVector<BasicBlock *, 3> Candidates{ getBlockByName(Func, "header"), 58 getBlockByName(Func, "body1"), 59 getBlockByName(Func, "body2") }; 60 61 DominatorTree DT(*Func); 62 CodeExtractor CE(Candidates, &DT); 63 EXPECT_TRUE(CE.isEligible()); 64 65 Function *Outlined = CE.extractCodeRegion(); 66 EXPECT_TRUE(Outlined); 67 BasicBlock *Exit = getBlockByName(Func, "notExtracted"); 68 BasicBlock *ExitSplit = getBlockByName(Outlined, "notExtracted.split"); 69 // Ensure that PHI in exit block has only one incoming value (from code 70 // replacer block). 71 EXPECT_TRUE(Exit && cast<PHINode>(Exit->front()).getNumIncomingValues() == 1); 72 // Ensure that there is a PHI in outlined function with 2 incoming values. 73 EXPECT_TRUE(ExitSplit && 74 cast<PHINode>(ExitSplit->front()).getNumIncomingValues() == 2); 75 EXPECT_FALSE(verifyFunction(*Outlined)); 76 EXPECT_FALSE(verifyFunction(*Func)); 77 } 78 79 TEST(CodeExtractor, ExitPHIOnePredFromRegion) { 80 LLVMContext Ctx; 81 SMDiagnostic Err; 82 std::unique_ptr<Module> M(parseAssemblyString(R"invalid( 83 define i32 @foo() { 84 header: 85 br i1 undef, label %extracted1, label %pred 86 87 pred: 88 br i1 undef, label %exit1, label %exit2 89 90 extracted1: 91 br i1 undef, label %extracted2, label %exit1 92 93 extracted2: 94 br label %exit2 95 96 exit1: 97 %0 = phi i32 [ 1, %extracted1 ], [ 2, %pred ] 98 ret i32 %0 99 100 exit2: 101 %1 = phi i32 [ 3, %extracted2 ], [ 4, %pred ] 102 ret i32 %1 103 } 104 )invalid", Err, Ctx)); 105 106 Function *Func = M->getFunction("foo"); 107 SmallVector<BasicBlock *, 2> ExtractedBlocks{ 108 getBlockByName(Func, "extracted1"), 109 getBlockByName(Func, "extracted2") 110 }; 111 112 DominatorTree DT(*Func); 113 CodeExtractor CE(ExtractedBlocks, &DT); 114 EXPECT_TRUE(CE.isEligible()); 115 116 Function *Outlined = CE.extractCodeRegion(); 117 EXPECT_TRUE(Outlined); 118 BasicBlock *Exit1 = getBlockByName(Func, "exit1"); 119 BasicBlock *Exit2 = getBlockByName(Func, "exit2"); 120 // Ensure that PHIs in exits are not splitted (since that they have only one 121 // incoming value from extracted region). 122 EXPECT_TRUE(Exit1 && 123 cast<PHINode>(Exit1->front()).getNumIncomingValues() == 2); 124 EXPECT_TRUE(Exit2 && 125 cast<PHINode>(Exit2->front()).getNumIncomingValues() == 2); 126 EXPECT_FALSE(verifyFunction(*Outlined)); 127 EXPECT_FALSE(verifyFunction(*Func)); 128 } 129 130 TEST(CodeExtractor, StoreOutputInvokeResultAfterEHPad) { 131 LLVMContext Ctx; 132 SMDiagnostic Err; 133 std::unique_ptr<Module> M(parseAssemblyString(R"invalid( 134 declare i8 @hoge() 135 136 define i32 @foo() personality i8* null { 137 entry: 138 %call = invoke i8 @hoge() 139 to label %invoke.cont unwind label %lpad 140 141 invoke.cont: ; preds = %entry 142 unreachable 143 144 lpad: ; preds = %entry 145 %0 = landingpad { i8*, i32 } 146 catch i8* null 147 br i1 undef, label %catch, label %finally.catchall 148 149 catch: ; preds = %lpad 150 %call2 = invoke i8 @hoge() 151 to label %invoke.cont2 unwind label %lpad2 152 153 invoke.cont2: ; preds = %catch 154 %call3 = invoke i8 @hoge() 155 to label %invoke.cont3 unwind label %lpad2 156 157 invoke.cont3: ; preds = %invoke.cont2 158 unreachable 159 160 lpad2: ; preds = %invoke.cont2, %catch 161 %ex.1 = phi i8* [ undef, %invoke.cont2 ], [ null, %catch ] 162 %1 = landingpad { i8*, i32 } 163 catch i8* null 164 br label %finally.catchall 165 166 finally.catchall: ; preds = %lpad33, %lpad 167 %ex.2 = phi i8* [ %ex.1, %lpad2 ], [ null, %lpad ] 168 unreachable 169 } 170 )invalid", Err, Ctx)); 171 172 if (!M) { 173 Err.print("unit", errs()); 174 exit(1); 175 } 176 177 Function *Func = M->getFunction("foo"); 178 EXPECT_FALSE(verifyFunction(*Func, &errs())); 179 180 SmallVector<BasicBlock *, 2> ExtractedBlocks{ 181 getBlockByName(Func, "catch"), 182 getBlockByName(Func, "invoke.cont2"), 183 getBlockByName(Func, "invoke.cont3"), 184 getBlockByName(Func, "lpad2") 185 }; 186 187 DominatorTree DT(*Func); 188 CodeExtractor CE(ExtractedBlocks, &DT); 189 EXPECT_TRUE(CE.isEligible()); 190 191 Function *Outlined = CE.extractCodeRegion(); 192 EXPECT_TRUE(Outlined); 193 EXPECT_FALSE(verifyFunction(*Outlined, &errs())); 194 EXPECT_FALSE(verifyFunction(*Func, &errs())); 195 } 196 197 } // end anonymous namespace 198