1 #include "llvm/Analysis/CallGraph.h"
2 #include "llvm/AsmParser/Parser.h"
3 #include "llvm/Config/config.h"
4 #include "llvm/IR/Module.h"
5 #include "llvm/Passes/PassBuilder.h"
6 #include "llvm/Passes/PassPlugin.h"
7 #include "llvm/Support/CommandLine.h"
8 #include "llvm/Support/raw_ostream.h"
9 #include "llvm/Testing/Support/Error.h"
10 #include "gtest/gtest.h"
12 #include "llvm/Analysis/InlineOrder.h"
20 std::string
libPath(const std::string Name
= "InlineOrderPlugin") {
21 const auto &Argvs
= testing::internal::GetArgvs();
23 Argvs
.size() > 0 ? Argvs
[0].c_str() : "PluginInlineOrderAnalysisTest";
24 void *Ptr
= (void *)(intptr_t)anchor
;
25 std::string Path
= sys::fs::getMainExecutable(Argv0
, Ptr
);
26 llvm::SmallString
<256> Buf
{sys::path::parent_path(Path
)};
27 sys::path::append(Buf
, (Name
+ LLVM_PLUGIN_EXT
).c_str());
28 return std::string(Buf
.str());
31 struct CompilerInstance
{
33 ModulePassManager MPM
;
37 LoopAnalysisManager LAM
;
38 FunctionAnalysisManager FAM
;
39 CGSCCAnalysisManager CGAM
;
40 ModuleAnalysisManager MAM
;
44 // Connect the plugin to our compiler instance.
46 auto PluginPath
= libPath();
47 ASSERT_NE("", PluginPath
);
48 Expected
<PassPlugin
> Plugin
= PassPlugin::Load(PluginPath
);
49 ASSERT_TRUE(!!Plugin
) << "Plugin path: " << PluginPath
;
50 Plugin
->registerPassBuilderCallbacks(PB
);
54 IP
= getInlineParams(3, 0);
55 PB
.registerModuleAnalyses(MAM
);
56 PB
.registerCGSCCAnalyses(CGAM
);
57 PB
.registerFunctionAnalyses(FAM
);
58 PB
.registerLoopAnalyses(LAM
);
59 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
60 MPM
.addPass(ModuleInlinerPass(IP
, InliningAdvisorMode::Default
,
61 ThinOrFullLTOPhase::None
));
65 std::unique_ptr
<Module
> OutputM
;
67 // Run with the dynamic inline order.
68 auto run(StringRef IR
) {
69 OutputM
= parseAssemblyString(IR
, Error
, Ctx
);
70 MPM
.run(*OutputM
, MAM
);
73 raw_string_ostream OStream
{Output
};
74 OutputM
->print(OStream
, nullptr);
79 StringRef TestIRS
[] = {
80 // Simple 3 function inline case.
94 // Test that has 5 functions of which 2 are recursive.
117 // Test with 2 mutually recursive functions and 1 function with a loop.
141 // Test that has a function that computes fibonacci in a loop, one in a
142 // recursive manner, and one that calls both and compares them.
144 define i32 @fib_loop(i32 %n){
148 store i32 1, i32* %curr
149 store i32 1, i32* %last
153 %i_val = load i32, i32* %i
154 %cmp = icmp slt i32 %i_val, %n
155 br i1 %cmp, label %loop_body, label %loop_end
157 %curr_val = load i32, i32* %curr
158 %last_val = load i32, i32* %last
159 %add = add i32 %curr_val, %last_val
160 store i32 %add, i32* %last
161 store i32 %curr_val, i32* %curr
162 %i_val2 = load i32, i32* %i
163 %add2 = add i32 %i_val2, 1
164 store i32 %add2, i32* %i
167 %curr_val3 = load i32, i32* %curr
171 define i32 @foo(i32 %n){
172 %cmp = icmp eq i32 %n, 0
173 %cmp2 = icmp eq i32 %n, 1
174 %or = or i1 %cmp, %cmp2
175 br i1 %or, label %if_true, label %if_false
180 %call = call i32 @foo(i32 %sub)
181 %sub2 = sub i32 %n, 2
182 %call2 = call i32 @foo(i32 %sub2)
183 %add = add i32 %call, %call2
187 define i32 @fib_check(){
188 %correct = alloca i32
190 store i32 1, i32* %correct
194 %i_val = load i32, i32* %i
195 %cmp = icmp slt i32 %i_val, 10
196 br i1 %cmp, label %loop_body, label %loop_end
198 %i_val2 = load i32, i32* %i
199 %call = call i32 @fib_loop(i32 %i_val2)
200 %i_val3 = load i32, i32* %i
201 %call2 = call i32 @foo(i32 %i_val3)
202 %cmp2 = icmp ne i32 %call, %call2
203 br i1 %cmp2, label %if_true, label %if_false
205 store i32 0, i32* %correct
210 %i_val4 = load i32, i32* %i
211 %add = add i32 %i_val4, 1
212 store i32 %add, i32* %i
215 %correct_val = load i32, i32* %correct
222 // Check that the behaviour of a custom inline order is correct.
223 // The custom order drops any functions named "foo" so all tests
224 // should contain at least one function named foo.
225 TEST(PluginInlineOrderTest
, NoInlineFoo
) {
226 #if !defined(LLVM_ENABLE_PLUGINS)
227 // Skip the test if plugins are disabled.
230 CompilerInstance CI
{};
233 for (StringRef IR
: TestIRS
) {
234 bool FoundFoo
= false;
236 CallGraph CGraph
= CallGraph(*CI
.OutputM
);
237 for (auto &Node
: CGraph
) {
238 for (auto &Edge
: *Node
.second
) {
239 FoundFoo
|= Edge
.second
->getFunction()->getName() == "foo";
242 ASSERT_TRUE(FoundFoo
);