-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrematerialize.cc
More file actions
458 lines (414 loc) · 12.5 KB
/
Copy pathrematerialize.cc
File metadata and controls
458 lines (414 loc) · 12.5 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* contains functions and data for implementing rematerialization in
* the chow allocator.
*
* The core ideas for the implemenation come from the paper
* "Rematerialization" by Briggs, Cooper, Torczan. They have been
* modified to fit into the framework of a chow allocator.
*
* the code and algorithm for finding the tag values borrows heavily
* from the ra implementation:
* /home/compiler/installed/ra/rematerialize.inc
*/
/*--------------------------INCLUDES---------------------------*/
#include <SSA.h>
#include <list>
#include <utility>
#include <map>
#include "rematerialize.h"
#include "cfg_tools.h"
#include "union_find.h"
#include "chow.h"
#include "mapping.h"
#include "live_range.h"
#include "live_unit.h"
/*------------------MODULE LOCAL DECLARATIONS------------------*/
namespace {
using Remat::LatticeVal;
using Remat::LatticeElem;
using std::vector;
using std::list;
//variables
vector<std::pair<Variable,Variable> > splits;
//functions
void InitializeTagsAndWorklist(vector<LatticeElem>&,list<Variable>&);
LatticeElem MeetOverPhiNodeOps(const Phi_Node*,
LatticeElem,
const vector<LatticeElem>&);
bool OpersAllEqual(Operation* oper1, Operation* oper2);
}
/*--------------------BEGIN IMPLEMENTATION---------------------*/
namespace Remat{
using std::vector;
using std::pair;
vector<LatticeElem> tags;
UFSet** remat_sets;
/*
*=============================
* ComputeTags()
*=============================
* Find the initial tag values for all SSA Names
*/
void ComputeTags()
{
//save space for all the tags and initailize to TOP
tags.resize(SSA_def_count);
for(unsigned int i = 0; i < tags.size(); i++) tags[i].val = TOP;
//find constant values and initalize worklist
std::list<Variable> worklist;
InitializeTagsAndWorklist(tags, worklist);
//run the sparse constant propagation algorithm on the lattice to
//find which values are rematerializable
while(worklist.size() > 0)
{
Variable def = worklist.front(); worklist.pop_front();
//follow the def to its uses and update those lattice elements
Chains_List* use;
Chain_ForAllUses(use,def)
{
Chain chain = use->chain;
if(chain.is_phi_node)
//meet over all phi-node params
{
Variable phi_name = chain.op_pointer.phi_node->new_name;
LatticeElem orig_lattice_elem = tags[phi_name];
LatticeElem new_elem =
MeetOverPhiNodeOps(chain.op_pointer.phi_node,
orig_lattice_elem,
tags);
//and update lattice if the value changes
if(new_elem.val != orig_lattice_elem.val)
{
tags[phi_name].val = new_elem.val;
tags[phi_name].op = new_elem.op;
worklist.push_back(phi_name);
}
}
else
//lower the defs of the operation, handling copies special
{
Operation* op = chain.op_pointer.operation;
if(opcode_specs[op->opcode].details & COPY)
{
Register src = op->arguments[0];
Register dest = op->arguments[1];
//update lattice if the source is lower than copy
if(tags[src].val < tags[dest].val)
{
tags[dest].val = tags[src].val;
tags[dest].op = tags[src].op;
worklist.push_back(dest);
}
}
else //not a copy
{
//lower all defined registers to bottom
Register* reg;
Operation_ForAllDefs(reg, op)
{
if(tags[*reg].val != BOTTOM)
{
tags[*reg].val = BOTTOM;
worklist.push_back(*reg);
}
}
}
}
}
}
DumpTags();
}
/*
*=============================
* DumpTags()
*=============================
*/
void DumpTags()
{
#ifdef __DEBUG
for(unsigned int i = 0; i < tags.size(); i++)
{
LatticeElem lv = tags[i];
const char* val = lv.val == CONST ? "CONST" :
(lv.val == TOP ? "TOP" : "BOTTOM");
const char* op = lv.val == CONST ? Debug::StringOfOp(lv.op) : "";
debug("r%d - %s %s", i, val, op);
}
#endif
}
/*
*=============================
* GetSplits()
*=============================
*/
const vector<std::pair<Variable,Variable> >& GetSplits(void)
{
return splits;
}
/*
*=============================
* AddSplit()
*=============================
*/
void AddSplit(Variable parent_ssa_name, Variable child_ssa_name)
{
splits.push_back(std::make_pair(parent_ssa_name, child_ssa_name));
}
/*
*=============================
* TagsAreEqual()
*=============================
*/
bool TagsAllEqual(Variable v1, Variable v2)
{
return ((tags[v1].val == tags[v2].val)
&&
( tags[v1].val != Remat::CONST
|| OpersAllEqual(tags[v1].op, tags[v2].op)));
}
/*
*===================================
* SplitRematerializableLiveRanges()
*===================================
*/
void SplitRematerializableLiveRanges()
{
using Chow::live_ranges;
using Mapping::SSAName2OrigLRID;
typedef std::map<unsigned int, std::vector<LiveUnit*> > LUMap;
//find which live ranges are to be split
std::set<LiveRange*> splitset;
for(SplitList::const_iterator i = splits.begin();
i != splits.end();
i++)
{
Variable ssa_orig = (*i).first;
splitset.insert(live_ranges[SSAName2OrigLRID(ssa_orig)]);
debug("phi split: %d", ssa_orig);
}
debug("found %d live ranges that have remat parts",
(int)splitset.size());
//process each live range that is to be split
for(std::set<LiveRange*>::iterator i = splitset.begin();
i != splitset.end();
i++)
{
LiveRange* lr = *i;
LUMap lu_map;
debug("splitting: %d (lrid)", lr->id);
Debug::LiveRange_DDump(lr);
//build the mapping from setid --> list of live units based on the
//earlier findings in the remat_sets. these mappings represent the
//new split live ranges
for(LiveRange::iterator i = lr->begin(); i != lr->end(); i++)
{
LiveUnit* lu = *i;
unsigned int setid = (Find_Set(lu->orig_name, remat_sets))->id;
debug("LiveUnit: %s(%d) is setid %d", bname(lu->block),
bid(lu->block), setid);
lu_map[setid].push_back(lu);
}
assert(lu_map.size() > 1);
//now do the actual splitting. an arbitrary element is chosen to
//retain the original live range id. this element is removed from
//the live unit map since all remaning elements in the live unit
//map will have their units transferred away.
lu_map.erase(lu_map.begin());
std::vector<LiveRange*> new_lrs;
for(LUMap::iterator i = lu_map.begin(); i != lu_map.end(); i++)
{
//create a new live range
LiveRange* lr_new = lr->Mitosis();
live_ranges.push_back(lr_new);
new_lrs.push_back(lr_new);
//check if it is rematerializable
Variable ssa_name = (*i).second.back()->orig_name;
if(tags[ssa_name].val == CONST)
{
lr_new->rematerializable = true;
lr_new->remat_op = tags[ssa_name].op;
}
else
{
lr_new->rematerializable = false;
}
//transfer the live units
for(std::vector<LiveUnit*>::iterator luIT = (*i).second.begin();
luIT != (*i).second.end();
luIT++)
{
lr->TransferLiveUnitTo(lr_new, *luIT);
}
lr->splits->push_back(lr_new);
}
//rebuild interferences
for(LazySet::iterator fearIT = lr->fear_list->begin();
fearIT != lr->fear_list->end(); fearIT++)
{
LiveRange* fearlr = *fearIT;
//check each of the new lrs we split from us for interference
for(std::vector<LiveRange*>::iterator lrIT = new_lrs.begin();
lrIT != new_lrs.end();
lrIT++)
{
LiveRange* newlr = *lrIT;
//update newlr interference
if(newlr->InterferesWith(fearlr))
{
newlr->AddInterference(fearlr);
}
}
//update origlr interference
//can delete during iteration from a LazySet
//LRSet::iterator del = fearIT++;
if(!lr->InterferesWith(fearlr))
{
//increment iterator before delete
fearlr->fear_list->erase(lr);
lr->fear_list->erase(fearlr);
}
}
//check to see whether our origlr is now rematerializable
Variable ssa_name = (*lr->begin())->orig_name;
if(tags[ssa_name].val == CONST)
{
lr->rematerializable = true;
lr->remat_op = tags[ssa_name].op;
}
else
{
lr->rematerializable = false;
}
//----DEBUG STUFF BELOW----
//dump splits for dot
if(Debug::dot_dump_lr && Debug::dot_dump_lr == lr->orig_lrid)
{
//check each of the new lrs we split from us for interference
for(std::vector<LiveRange*>::iterator lrIT = new_lrs.begin();
lrIT != new_lrs.end();
lrIT++)
{
LiveRange* newlr = *lrIT;
Debug::DotDumpLR(newlr, "split");
Debug::dot_dumped_lrs.push_back(newlr);
}
Debug::DotDumpLR(lr, "split");
Debug::dot_dumped_lrs.push_back(lr);
}
debug("done splitting lr: %d", lr->id);
Debug::LiveRange_DDump(lr);
for(std::vector<LiveRange*>::iterator lrIT = new_lrs.begin();
lrIT != new_lrs.end();
lrIT++)
{
Debug::LiveRange_DDump(*lrIT);
}
//----DEBUG STUFF ABOVE----
}
}
}//end Rematerialize namespace
/*-------------------BEGIN LOCAL DEFINITIONS-------------------*/
namespace {
LatticeElem
MeetOverPhiNodeOps(const Phi_Node* phi_node,
LatticeElem phi_elem,
const vector<LatticeElem>& tags)
{
LatticeElem new_elem;
new_elem.val = phi_elem.val;
new_elem.op = phi_elem.op;
const Variable *parm_ptr;
Phi_Node_ForAllParms(parm_ptr, phi_node)
{
Variable parm = *parm_ptr;
LatticeElem parm_elem = tags[parm];
if (parm_elem.val < new_elem.val)
{
new_elem.val = parm_elem.val;
new_elem.op = parm_elem.op;
}
else if (parm_elem.val == new_elem.val && new_elem.val == Remat::CONST)
{
if(!OpersAllEqual(new_elem.op, parm_elem.op))
new_elem.val = Remat::BOTTOM;
}
}
return new_elem;
}
void
InitializeTagsAndWorklist(vector<LatticeElem>& tags,
list<Variable>& worklist)
{
using Remat::CONST;
using Remat::BOTTOM;
Block *block; Inst *inst; Operation **oper_ptr;
//need the frame to note that the static_pointer_reg and
//static_data_reg are known values. we can rematerialize operations
//that use these registers
Operation *frame = GetFrameOperation();
Register stack_pointer_reg = frame->arguments[1];
Register static_data_reg = frame->arguments[2];
ForAllBlocks(block)
{
Block_ForAllInsts(inst, block)
{
Inst_ForAllOperations(oper_ptr, inst)
{
Operation *oper = *oper_ptr;
Unsigned_Int details = opcode_specs[oper->opcode].details;
//see whether or not we can rematerialize the expression
if ((details & EXPR))
{
Unsigned_Int2 *arg_ptr;
Boolean bad_reg_found = FALSE;
Unsigned_Int result = oper->arguments[oper->defined-1];
Operation_ForAllUses(arg_ptr, oper)
if (*arg_ptr != stack_pointer_reg)
{
bad_reg_found = TRUE;
break;
}
LatticeVal lv = BOTTOM;
Operation* lop = NULL;
if (!bad_reg_found)
{
lv = CONST;
lop = oper;
}
tags[result].val = lv;
tags[result].op = lop;
worklist.push_back(result);
}
//else the operation can not be rematerialized, send it to bottom
//unless it is a copy, in which case we will wait for more
//information about the source before we lower the copy dest
else if (!(details & COPY))
{
//Mark all the defined registers as BOTTOM
Unsigned_Int *arg_ptr;
Operation_ForAllDefs(arg_ptr, oper)
{
Unsigned_Int result = *arg_ptr;
if (result != stack_pointer_reg &&
result != static_data_reg)
{
worklist.push_back(result);
tags[result].val = BOTTOM;
}
}
}
}
}
}
}
bool OpersAllEqual(Operation* oper1, Operation* oper2)
{
Unsigned_Int i;
if (oper1->opcode != oper2->opcode)
return false;
for (i = 0; i < oper1->referenced; i++)
if (oper1->arguments[i] != oper2->arguments[i])
return false;
/* If we haven't already returned FALSE, they must be equal. */
return true;
}
}//end anonymous namespace