1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
//===- CodeExtractor.cpp - Unit tests for CodeExtractor -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/CodeExtractor.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
BasicBlock *getBlockByName(Function *F, StringRef name) {
for (auto &BB : *F)
if (BB.getName() == name)
return &BB;
return nullptr;
}
TEST(CodeExtractor, ExitStub) {
LLVMContext Ctx;
SMDiagnostic Err;
std::unique_ptr<Module> M(parseAssemblyString(R"invalid(
define i32 @foo(i32 %x, i32 %y, i32 %z) {
header:
%0 = icmp ugt i32 %x, %y
br i1 %0, label %body1, label %body2
body1:
%1 = add i32 %z, 2
br label %notExtracted
body2:
%2 = mul i32 %z, 7
br label %notExtracted
notExtracted:
%3 = phi i32 [ %1, %body1 ], [ %2, %body2 ]
%4 = add i32 %3, %x
ret i32 %4
}
)invalid",
Err, Ctx));
Function *Func = M->getFunction("foo");
SmallVector<BasicBlock *, 3> Candidates{ getBlockByName(Func, "header"),
getBlockByName(Func, "body1"),
getBlockByName(Func, "body2") };
DominatorTree DT(*Func);
CodeExtractor CE(Candidates, &DT);
EXPECT_TRUE(CE.isEligible());
Function *Outlined = CE.extractCodeRegion();
EXPECT_TRUE(Outlined);
BasicBlock *Exit = getBlockByName(Func, "notExtracted");
BasicBlock *ExitSplit = getBlockByName(Outlined, "notExtracted.split");
// Ensure that PHI in exit block has only one incoming value (from code
// replacer block).
EXPECT_TRUE(Exit && cast<PHINode>(Exit->front()).getNumIncomingValues() == 1);
// Ensure that there is a PHI in outlined function with 2 incoming values.
EXPECT_TRUE(ExitSplit &&
cast<PHINode>(ExitSplit->front()).getNumIncomingValues() == 2);
EXPECT_FALSE(verifyFunction(*Outlined));
EXPECT_FALSE(verifyFunction(*Func));
}
TEST(CodeExtractor, ExitPHIOnePredFromRegion) {
LLVMContext Ctx;
SMDiagnostic Err;
std::unique_ptr<Module> M(parseAssemblyString(R"invalid(
define i32 @foo() {
header:
br i1 undef, label %extracted1, label %pred
pred:
br i1 undef, label %exit1, label %exit2
extracted1:
br i1 undef, label %extracted2, label %exit1
extracted2:
br label %exit2
exit1:
%0 = phi i32 [ 1, %extracted1 ], [ 2, %pred ]
ret i32 %0
exit2:
%1 = phi i32 [ 3, %extracted2 ], [ 4, %pred ]
ret i32 %1
}
)invalid", Err, Ctx));
Function *Func = M->getFunction("foo");
SmallVector<BasicBlock *, 2> ExtractedBlocks{
getBlockByName(Func, "extracted1"),
getBlockByName(Func, "extracted2")
};
DominatorTree DT(*Func);
CodeExtractor CE(ExtractedBlocks, &DT);
EXPECT_TRUE(CE.isEligible());
Function *Outlined = CE.extractCodeRegion();
EXPECT_TRUE(Outlined);
BasicBlock *Exit1 = getBlockByName(Func, "exit1");
BasicBlock *Exit2 = getBlockByName(Func, "exit2");
// Ensure that PHIs in exits are not splitted (since that they have only one
// incoming value from extracted region).
EXPECT_TRUE(Exit1 &&
cast<PHINode>(Exit1->front()).getNumIncomingValues() == 2);
EXPECT_TRUE(Exit2 &&
cast<PHINode>(Exit2->front()).getNumIncomingValues() == 2);
EXPECT_FALSE(verifyFunction(*Outlined));
EXPECT_FALSE(verifyFunction(*Func));
}
} // end anonymous namespace
|