-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInlinePass.cpp
62 lines (50 loc) · 1.8 KB
/
InlinePass.cpp
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
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "Inline.h"
#include "CallFunctionGraph.h"
#include "llvm/IR/LegacyPassManager.h"
using namespace llvm;
namespace {
struct InlinePass : public ModulePass {
static char ID;
InlinePass() : ModulePass(ID) {}
bool runOnModule(Module &M) override {
Function *MainFunction = M.getFunction("main");
CallFunctionGraph *CallGraph = new CallFunctionGraph(MainFunction);
CallGraph->createCallGraph();
CallGraph->findRecursiveCalls();
bool IRChanged = false;
std::vector<CallInst *> CallInstructions;
do {
for (CallInst *CallInstr : CallInstructions) {
if (Inline::inlineFunction(CallInstr)) {
IRChanged = true;
}
}
CallInstructions.clear();
for (BasicBlock &BB : *MainFunction) {
for (Instruction &I : BB) {
if (CallInst *CallInstr = dyn_cast<CallInst>(&I)) {
if (Inline::shouldInline(CallInstr->getCalledFunction(), CallGraph)) {
CallInstructions.push_back(CallInstr);
}
}
}
}
} while (!CallInstructions.empty());
CallGraph->createCallGraph();
if (CallGraph->removeUnusedFunctions()) {
IRChanged = true;
}
delete CallGraph;
return IRChanged;
}
};
} // end of anonymous namespace
char InlinePass::ID = 0;
static RegisterPass<InlinePass> X("inline-pass", "Inline Pass",
false /* Only looks at CFG */,
false /* Analysis Pass */);