/
hir.h
302 lines (248 loc) · 8.65 KB
/
hir.h
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* Copyright (c) 2012, Fedor Indutny.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _SRC_HIR_H_
#define _SRC_HIR_H_
#include <stdint.h> // int32_t
#include "hir-instructions.h"
#include "hir-instructions-inl.h"
#include "heap.h" // Heap
#include "heap-inl.h" // Heap
#include "root.h" // Root
#include "ast.h" // AstNode
#include "scope.h" // ScopeSlot
#include "visitor.h" // Visitor
#include "zone.h" // ZoneObject
#include "utils.h" // PrintBuffer
namespace candor {
namespace internal {
// Forward declaration
class HIRGen;
class HIRBlock;
class LBlock;
typedef ZoneList<HIRBlock*> HIRBlockList;
class HIREnvironment : public ZoneObject {
public:
explicit HIREnvironment(int stack_slots);
void Copy(HIREnvironment* from);
inline HIRInstruction* At(int i);
inline HIRPhi* PhiAt(int i);
inline void Set(int i, HIRInstruction* value);
inline void SetPhi(int i, HIRPhi* value);
inline HIRInstruction* At(ScopeSlot* slot);
inline HIRPhi* PhiAt(ScopeSlot* slot);
inline void Set(ScopeSlot* slot, HIRInstruction* value);
inline void SetPhi(ScopeSlot* slot, HIRPhi* value);
inline ScopeSlot* logic_slot();
inline int stack_slots();
protected:
int stack_slots_;
ScopeSlot* logic_slot_;
HIRInstruction** instructions_;
HIRPhi** phis_;
};
class HIRBlock : public ZoneObject {
public:
explicit HIRBlock(HIRGen* g);
int id;
int dfs_id;
int loop_depth;
HIRInstruction* Assign(ScopeSlot* slot, HIRInstruction* value);
void Remove(HIRInstruction* instr);
inline HIRBlock* root();
inline void root(HIRBlock* root);
inline HIRBlock* AddSuccessor(HIRBlock* b);
inline HIRInstruction* Add(HIRInstruction::Type type);
inline HIRInstruction* Add(HIRInstruction::Type type, ScopeSlot* slot);
inline HIRInstruction* Add(HIRInstruction* instr);
inline HIRInstruction* Goto(HIRBlock* target);
inline HIRInstruction* Branch(HIRInstruction* instr,
HIRBlock* t,
HIRBlock* f);
inline HIRInstruction* Return(HIRInstruction* instr);
inline bool IsEnded();
inline bool IsEmpty();
void MarkPreLoop();
void MarkLoop();
inline bool IsLoop();
inline HIRPhi* CreatePhi(ScopeSlot* slot);
inline BitField<EmptyClass>* reachable_from();
inline HIREnvironment* env();
inline void env(HIREnvironment* env);
inline HIRInstructionList* instructions();
inline HIRPhiList* phis();
inline HIRBlock* SuccAt(int i);
inline HIRBlock* PredAt(int i);
inline int pred_count();
inline int succ_count();
inline HIRBlock* parent();
inline void parent(HIRBlock* parent);
inline HIRBlock* ancestor();
inline void ancestor(HIRBlock* ancestor);
inline HIRBlock* label();
inline void label(HIRBlock* label);
inline HIRBlock* semi();
inline void semi(HIRBlock* semi);
inline HIRBlock* dominator();
inline void dominator(HIRBlock* dom);
inline int dominator_depth();
inline HIRBlockList* dominates();
inline LBlock* lir();
inline void lir(LBlock* lir);
inline void Print(PrintBuffer* p);
protected:
void AddPredecessor(HIRBlock* b);
inline void Compress();
inline HIRBlock* Evaluate();
HIRGen* g_;
BitField<EmptyClass> reachable_from_;
bool loop_;
bool ended_;
HIREnvironment* env_;
HIRInstructionList instructions_;
HIRPhiList phis_;
int pred_count_;
int succ_count_;
HIRBlock* root_;
HIRBlock* pred_[2];
HIRBlock* succ_[2];
// Dominators algorithm augmentation
HIRBlock* parent_;
HIRBlock* ancestor_;
HIRBlock* label_;
HIRBlock* semi_;
HIRBlock* dominator_;
int dominator_depth_;
HIRBlockList dominates_;
// Allocator augmentation
LBlock* lir_;
int start_id_;
int end_id_;
// Only HIRGen should be able to call Evaluate()
friend class HIRGen;
};
class BreakContinueInfo : public ZoneObject {
public:
BreakContinueInfo(HIRGen* g, HIRBlock* end);
HIRBlock* GetContinue();
HIRBlock* GetBreak();
inline HIRBlockList* continue_blocks();
private:
HIRGen* g_;
HIRBlockList continue_blocks_;
HIRBlock* brk_;
};
class HIRGen : public Visitor<HIRInstruction> {
public:
HIRGen(Heap* heap, Root* root, const char* filename);
~HIRGen();
void Build(AstNode* root);
void PrunePhis();
void FindReachableBlocks();
void DeriveDominators();
void EnumerateDFS(HIRBlock* b, HIRBlockList* blocks);
void EliminateDeadCode();
void EliminateDeadCode(HIRInstruction* instr);
void FindEffects();
void FindOutEffects(HIRInstruction* instr);
void FindInEffects(HIRInstruction* instr);
void GlobalValueNumbering();
void GlobalValueNumbering(HIRInstruction* instr, HIRGVNMap* gvn);
void GlobalCodeMotion();
void ScheduleEarly(HIRInstruction* instr, HIRBlock* root);
void ScheduleLate(HIRInstruction* instr);
HIRBlock* FindLCA(HIRBlock* a, HIRBlock* b);
void Replace(HIRInstruction* o, HIRInstruction* n);
HIRInstruction* Visit(AstNode* stmt);
HIRInstruction* VisitFunction(AstNode* stmt);
HIRInstruction* VisitAssign(AstNode* stmt);
HIRInstruction* VisitReturn(AstNode* stmt);
HIRInstruction* VisitValue(AstNode* stmt);
HIRInstruction* VisitIf(AstNode* stmt);
HIRInstruction* VisitWhile(AstNode* stmt);
HIRInstruction* VisitBreak(AstNode* stmt);
HIRInstruction* VisitContinue(AstNode* stmt);
HIRInstruction* VisitUnOp(AstNode* stmt);
HIRInstruction* VisitBinOp(AstNode* stmt);
HIRInstruction* VisitObjectLiteral(AstNode* stmt);
HIRInstruction* VisitArrayLiteral(AstNode* stmt);
HIRInstruction* VisitMember(AstNode* stmt);
HIRInstruction* VisitDelete(AstNode* stmt);
HIRInstruction* VisitCall(AstNode* stmt);
HIRInstruction* VisitTypeof(AstNode* stmt);
HIRInstruction* VisitSizeof(AstNode* stmt);
HIRInstruction* VisitKeysof(AstNode* stmt);
HIRInstruction* VisitClone(AstNode* stmt);
// Literals
HIRInstruction* VisitLiteral(AstNode* stmt);
HIRInstruction* VisitNumber(AstNode* stmt);
HIRInstruction* VisitNil(AstNode* stmt);
HIRInstruction* VisitTrue(AstNode* stmt);
HIRInstruction* VisitFalse(AstNode* stmt);
HIRInstruction* VisitString(AstNode* stmt);
HIRInstruction* VisitProperty(AstNode* stmt);
inline void set_current_block(HIRBlock* b);
inline void set_current_root(HIRBlock* b);
inline HIRBlock* current_block();
inline HIRBlock* current_root();
inline HIRBlockList* blocks();
inline HIRBlockList* roots();
inline HIRInstruction* Add(HIRInstruction::Type type);
inline HIRInstruction* Add(HIRInstruction::Type type, ScopeSlot* slot);
inline HIRInstruction* Add(HIRInstruction* instr);
inline HIRInstruction* Goto(HIRBlock* target);
inline HIRInstruction* Branch(HIRInstruction* instr,
HIRBlock* t,
HIRBlock* f);
inline HIRInstruction* Return(HIRInstruction* instr);
inline HIRBlock* Join(HIRBlock* b1, HIRBlock* b2);
inline HIRInstruction* Assign(ScopeSlot* slot, HIRInstruction* value);
inline HIRInstruction* GetNumber(uint64_t i);
inline HIRBlock* CreateBlock(int stack_slots);
inline HIRBlock* CreateBlock();
inline HIRInstruction* CreateInstruction(HIRInstruction::Type type);
inline HIRPhi* CreatePhi(ScopeSlot* slot);
static void EnableLogging();
static void DisableLogging();
inline void Print(PrintBuffer* p);
inline void Print(char* out, int32_t size);
inline Root* root();
inline int block_id();
inline int instr_id();
inline int dfs_id();
static const int kMaxOptimizableSize = 25000;
private:
HIRBlock* current_block_;
HIRBlock* current_root_;
BreakContinueInfo* break_continue_info_;
HIRBlockList roots_;
HIRBlockList blocks_;
Root* root_;
const char* filename_;
int loop_depth_;
int block_id_;
int instr_id_;
int dfs_id_;
static bool log_;
};
} // namespace internal
} // namespace candor
#endif // _SRC_HIR_H_