1 #include "llvm/Analysis/CallGraph.h"
2 #include "llvm/AsmParser/Parser.h"
3 #include "llvm/Config/config.h"
4 #include "llvm/Passes/PassBuilder.h"
5 #include "llvm/Passes/PassPlugin.h"
6 #include "llvm/Support/CommandLine.h"
7 #include "llvm/Support/raw_ostream.h"
8 #include "llvm/Testing/Support/Error.h"
9 #include "gtest/gtest.h"
11 #include "llvm/Analysis/InlineOrder.h"
19 std::string
libPath(const std::string Name
= "InlineOrderPlugin") {
20 const auto &Argvs
= testing::internal::GetArgvs();
22 Argvs
.size() > 0 ? Argvs
[0].c_str() : "PluginInlineOrderAnalysisTest";
23 void *Ptr
= (void *)(intptr_t)anchor
;
24 std::string Path
= sys::fs::getMainExecutable(Argv0
, Ptr
);
25 llvm::SmallString
<256> Buf
{sys::path::parent_path(Path
)};
26 sys::path::append(Buf
, (Name
+ LLVM_PLUGIN_EXT
).c_str());
27 return std::string(Buf
.str());
30 struct CompilerInstance
{
32 ModulePassManager MPM
;
36 LoopAnalysisManager LAM
;
37 FunctionAnalysisManager FAM
;
38 CGSCCAnalysisManager CGAM
;
39 ModuleAnalysisManager MAM
;
43 // Connect the plugin to our compiler instance.
45 auto PluginPath
= libPath();
46 ASSERT_NE("", PluginPath
);
47 Expected
<PassPlugin
> Plugin
= PassPlugin::Load(PluginPath
);
48 ASSERT_TRUE(!!Plugin
) << "Plugin path: " << PluginPath
;
49 Plugin
->registerPassBuilderCallbacks(PB
);
53 IP
= getInlineParams(3, 0);
54 PB
.registerModuleAnalyses(MAM
);
55 PB
.registerCGSCCAnalyses(CGAM
);
56 PB
.registerFunctionAnalyses(FAM
);
57 PB
.registerLoopAnalyses(LAM
);
58 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
59 MPM
.addPass(ModuleInlinerPass(IP
, InliningAdvisorMode::Default
,
60 ThinOrFullLTOPhase::None
));
64 // Reset the static variable that tracks if the plugin has been registered.
65 // This is needed to allow the test to run multiple times.
66 PluginInlineOrderAnalysis::unregister();
70 std::unique_ptr
<Module
> OutputM
;
72 // Run with the dynamic inline order.
73 auto run(StringRef IR
) {
74 OutputM
= parseAssemblyString(IR
, Error
, Ctx
);
75 MPM
.run(*OutputM
, MAM
);
78 raw_string_ostream OStream
{Output
};
79 OutputM
->print(OStream
, nullptr);
84 StringRef TestIRS
[] = {
85 // Simple 3 function inline case.
99 // Test that has 5 functions of which 2 are recursive.
122 // Test with 2 mutually recursive functions and 1 function with a loop.
146 // Test that has a function that computes fibonacci in a loop, one in a
147 // recursive manner, and one that calls both and compares them.
149 define i32 @fib_loop(i32 %n){
153 store i32 1, i32* %curr
154 store i32 1, i32* %last
158 %i_val = load i32, i32* %i
159 %cmp = icmp slt i32 %i_val, %n
160 br i1 %cmp, label %loop_body, label %loop_end
162 %curr_val = load i32, i32* %curr
163 %last_val = load i32, i32* %last
164 %add = add i32 %curr_val, %last_val
165 store i32 %add, i32* %last
166 store i32 %curr_val, i32* %curr
167 %i_val2 = load i32, i32* %i
168 %add2 = add i32 %i_val2, 1
169 store i32 %add2, i32* %i
172 %curr_val3 = load i32, i32* %curr
176 define i32 @foo(i32 %n){
177 %cmp = icmp eq i32 %n, 0
178 %cmp2 = icmp eq i32 %n, 1
179 %or = or i1 %cmp, %cmp2
180 br i1 %or, label %if_true, label %if_false
185 %call = call i32 @foo(i32 %sub)
186 %sub2 = sub i32 %n, 2
187 %call2 = call i32 @foo(i32 %sub2)
188 %add = add i32 %call, %call2
192 define i32 @fib_check(){
193 %correct = alloca i32
195 store i32 1, i32* %correct
199 %i_val = load i32, i32* %i
200 %cmp = icmp slt i32 %i_val, 10
201 br i1 %cmp, label %loop_body, label %loop_end
203 %i_val2 = load i32, i32* %i
204 %call = call i32 @fib_loop(i32 %i_val2)
205 %i_val3 = load i32, i32* %i
206 %call2 = call i32 @foo(i32 %i_val3)
207 %cmp2 = icmp ne i32 %call, %call2
208 br i1 %cmp2, label %if_true, label %if_false
210 store i32 0, i32* %correct
215 %i_val4 = load i32, i32* %i
216 %add = add i32 %i_val4, 1
217 store i32 %add, i32* %i
220 %correct_val = load i32, i32* %correct
227 // Check that the behaviour of a custom inline order is correct.
228 // The custom order drops any functions named "foo" so all tests
229 // should contain at least one function named foo.
230 TEST(PluginInlineOrderTest
, NoInlineFoo
) {
231 #if !defined(LLVM_ENABLE_PLUGINS)
232 // Skip the test if plugins are disabled.
235 CompilerInstance CI
{};
238 for (StringRef IR
: TestIRS
) {
239 bool FoundFoo
= false;
241 CallGraph CGraph
= CallGraph(*CI
.OutputM
);
242 for (auto &Node
: CGraph
) {
243 for (auto &Edge
: *Node
.second
) {
244 FoundFoo
|= Edge
.second
->getFunction()->getName() == "foo";
247 ASSERT_TRUE(FoundFoo
);