-
Notifications
You must be signed in to change notification settings - Fork 3
/
optimize.cpp
253 lines (213 loc) · 7.12 KB
/
optimize.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "optimize.h"
#include "stdio.h"
#include <iostream>
#include "asmjit/src/asmjit/asmjit.h"
#include "ast/all.h"
#include "value.h"
#include "runtime_error.h"
#include "visitor/ssa_builder.h"
#include "ssa/emitter.h"
#include "ssa/reg_alloc.h"
#include "ssa/print_visitor.h"
#include "ssa/constant_propagation.h"
#include "ssa/instruction_combiner.h"
#include "ssa/unused_code.h"
#include "ssa/type_analysis.h"
#include "ssa/constant_inliner.h"
#include "ssa/jmp_threader.h"
#include "ssa/inliner.h"
#include "ssa/dominator_builder.h"
#include "ssa/loop_invariant.h"
#include "ssa/common_subexpression.h"
#include <iostream>
#include <stdint.h>
using namespace asmjit;
using namespace vaiven::visitor;
using namespace vaiven::ast;
using namespace vaiven;
using namespace asmjit;
// Error handler that just prints the error and lets AsmJit ignore it.
class PrintErrorHandler2 : public asmjit::ErrorHandler {
public:
// Return `true` to set last error to `err`, return `false` to do nothing.
bool handleError(asmjit::Error err, const char* message, asmjit::CodeEmitter* origin) override {
fprintf(stderr, "Error: %s\n", message);
return false;
}
};
OverkillFunc vaiven::optimize(vaiven::Functions& funcs, ast::FuncDecl<>& funcDecl) {
if (funcs.funcs.find(funcDecl.name) == funcs.funcs.end()) {
throw "func not known";
}
#ifdef OPTIMIZATION_DIAGNOSTICS
std::cout << "optimizing " << funcDecl.name << std::endl;
#endif
performOptimize(funcDecl, funcs, *funcs.funcs[funcDecl.name]->usage);
#ifdef OPTIMIZATION_DIAGNOSTICS
std::cout << "done optimizing" << std::endl;
#endif
return funcs.funcs[funcDecl.name]->fptr;
}
void vaiven::performOptimize(ast::FuncDecl<>& decl, Functions& funcs, FunctionUsage& usageInfo) {
CodeHolder codeHolder;
codeHolder.init(funcs.runtime.getCodeInfo());
#ifdef DISASSEMBLY_DIAGNOSTICS
PrintErrorHandler2 eh;
FileLogger logger(stdout);
codeHolder.setErrorHandler(&eh);
codeHolder.setLogger(&logger);
#endif
X86Assembler assembler(&codeHolder);
X86Compiler cc(&codeHolder);
cc.addOptions(CodeEmitter::kOptionStrictValidation);
assembler.addOptions(CodeEmitter::kOptionStrictValidation);
#ifdef DISASSEMBLY_DIAGNOSTICS
#endif
uint8_t sigArgs[decl.args.size()];
for (int i = 0; i < decl.args.size(); ++i) {
sigArgs[i] = TypeIdOf<int64_t>::kTypeId;
}
FuncSignature sig;
sig.init(CallConv::kIdHost, TypeIdOf<int64_t>::kTypeId, sigArgs, decl.args.size());
CCFunc* curFunc = cc.addFunc(sig);
curFunc->getFrameInfo().addAttributes(asmjit::FuncFrameInfo::kAttrPreserveFP);
vector<X86Gp> argRegs;
Label deoptimizeLabel = cc.newLabel();
for (int i = 0; i < decl.args.size(); ++i) {
X86Gp arg = cc.newInt64();
cc.setArg(i, arg);
argRegs.push_back(arg);
}
SsaBuilder builder(usageInfo, funcs);
decl.accept(builder);
#ifdef SSA_DIAGNOSTICS
ssa::PrintVisitor printer;
builder.head.accept(printer); printer.varIds.clear();
#endif
ssa::Inliner inliner(funcs, funcs.funcs[decl.name]->worstSize);
builder.head.accept(inliner);
#ifdef SSA_DIAGNOSTICS
std::cout << "inliner" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
ssa::DominatorBuilder dbuilder;
dbuilder.firstBuild(builder.head);
#ifdef SSA_DIAGNOSTICS
std::cout << "dominator builder" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
ssa::TypeAnalysis typeAnalysis;
builder.head.accept(typeAnalysis);
#ifdef SSA_DIAGNOSTICS
std::cout << "type analysis" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
while(true) {
ssa::ConstantPropagator constantProp;
builder.head.accept(constantProp);
#ifdef SSA_DIAGNOSTICS
std::cout << "constant prop" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
ssa::InstructionCombiner instrComb;
builder.head.accept(instrComb);
#ifdef SSA_DIAGNOSTICS
std::cout << "instr comb" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
ssa::LoopInvariantCodeMover loopInvariant;
builder.head.accept(loopInvariant);
if (loopInvariant.requiresRebuildDominators) {
dbuilder.reset(builder.head);
}
#ifdef SSA_DIAGNOSTICS
std::cout << "licm" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
ssa::CommonSubexpressionEliminator commonSubElim;
builder.head.accept(commonSubElim);
#ifdef SSA_DIAGNOSTICS
std::cout << "cse" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
ssa::UnusedCodeEliminator unusedCodeElim;
builder.head.accept(unusedCodeElim);
if (unusedCodeElim.requiresRebuildDominators) {
dbuilder.reset(builder.head);
}
#ifdef SSA_DIAGNOSTICS
std::cout << "unused val elim" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
if (!unusedCodeElim.performedWork && !instrComb.performedWork && !constantProp.performedWork && !loopInvariant.performedWork && !commonSubElim.performedWork) {
break;
}
}
ssa::ConstantInliner constInliner;
builder.head.accept(constInliner);
#ifdef SSA_DIAGNOSTICS
std::cout << "const inliner" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
while(true) {
ssa::UnusedCodeEliminator unusedCodeElim;
builder.head.accept(unusedCodeElim);
if (unusedCodeElim.requiresRebuildDominators) {
dbuilder.reset(builder.head);
}
#ifdef SSA_DIAGNOSTICS
std::cout << "unused val elim" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
if (!unusedCodeElim.performedWork) {
break;
}
}
ssa::JmpThreader jmpThreader;
builder.head.accept(jmpThreader);
if (jmpThreader.requiresRebuildDominators) {
dbuilder.reset(builder.head);
}
#ifdef SSA_DIAGNOSTICS
std::cout << "jmp threading" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
while(true) {
ssa::UnusedCodeEliminator unusedCodeElim;
builder.head.accept(unusedCodeElim);
if (unusedCodeElim.requiresRebuildDominators) {
dbuilder.reset(builder.head);
}
#ifdef SSA_DIAGNOSTICS
std::cout << "unused val elim" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
if (!unusedCodeElim.performedWork) {
break;
}
}
ssa::RegAlloc allocator(cc, argRegs);
builder.head.accept(allocator);
ErrorCompiler error(cc);
ssa::Emitter emitter(cc, funcs, curFunc->getLabel(), decl.name, error, deoptimizeLabel);
builder.head.accept(emitter);
#ifdef SSA_DIAGNOSTICS
std::cout << "final code" << std::endl;
builder.head.accept(printer); printer.varIds.clear();
#endif
cc.bind(deoptimizeLabel);
CCFuncCall* deoptimizedCall = cc.call((unsigned long long) funcs.funcs[decl.name]->fptr, sig);
for (int i = 0; i < decl.args.size(); ++i) {
deoptimizedCall->setArg(i, argRegs[i]);
}
X86Gp deoptimizedRet = cc.newUInt64();
deoptimizedCall->setRet(0, deoptimizedRet);
cc.ret(deoptimizedRet);
error.generateTypeErrorEpilog();
cc.endFunc();
cc.finalize();
OverkillFunc fptr;
funcs.runtime.add(&fptr, &codeHolder);
funcs.funcs[decl.name]->slowfptr = funcs.funcs[decl.name]->fptr;
funcs.funcs[decl.name]->fptr = fptr;
}