-
Notifications
You must be signed in to change notification settings - Fork 0
/
super_block.cpp
executable file
·640 lines (569 loc) · 14.3 KB
/
super_block.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include <suif_copyright.h>
#include "super_block.h"
#include "loop_block.h"
#include "super_block_cfg.h"
#include "da_cfg.h"
extern char *k_call_overhead;
/*
*super_block::super_block() --- According to block kind, construct super block.
*/
super_block::super_block(super_block_kind block_kind) :
kind(block_kind) {
super_block_num = 0;
cnl = new cfg_node_list();
succs[0] = 0;
succs[1] = 0;
taken_probability = 0.00001;
immed_dominator = 0;
immed_pdominator = 0;
}
/*
*super_block::~super_block() --- destructor of super block
*/
super_block::~super_block() {
delete cnl;
}
/*
*super_block::set_block_num() --- set each super block's number.
*/
void super_block::set_block_num(int num) {
this->super_block_num = num;
}
/*
*super_block::nodes() --- return super block's cfg node list.
*/
cfg_node_list *super_block::nodes() const {
return cnl;
}
/*
*super_block::size() --- compute super block's size(dynamic instruction number)
*/
int super_block::dynamic_size() {
int size = 0;
cfg_node_list_iter cnl_iter(cnl);
while (!cnl_iter.is_empty()) {
cfg_node *curr_node = cnl_iter.step();
if (curr_node->is_block()) {
size += block_dynamic_size((cfg_block *) curr_node);
}
}
return size;
}
/*
*super_block::instr_size() --- compute super block's instruction size, don't contain lab, loc, and null
*return count of instr , region [1, count]
*/
int super_block::static_size() {
int counter = 0;
cfg_node_list_iter cnl_iter(cnl);
while (!cnl_iter.is_empty()) {
cfg_node *cn = cnl_iter.step();
if (cn->is_block()) {
cfg_node_instr_iter cnii(cn);
while (!cnii.is_empty()) {
tree_instr *test = cnii.step();
machine_instr *mi = (machine_instr *) test->instr();
if ((int) mi->opcode() == mo_lab || (int) mi->opcode() == mo_loc
|| (int) mi->opcode() == mo_null)
continue;
else
counter++;
}
}
}
return counter;
}
/*
*super_block::add_block() --- add cfg node to cfg node list(cnl).
*/
void super_block::add_block(cfg_node * the_block) {
if (the_block == NULL)
fprintf(stderr, "add a NULL block\n");
//If the block doesn't belong to nodes()
if (!occurs(the_block, nodes())) {
cnl->append(the_block);
}
}
/*
*super_block::add_blocks() --- add cfg node list to current super block
*/
void super_block::add_blocks(cfg_node_list * the_list) {
cfg_node_list_iter the_list_iter(the_list);
while (!the_list_iter.is_empty()) {
cfg_node *cn = the_list_iter.step();
if (!this->contains(cn)) {
cnl->append(cn);
}
}
}
/*
*super_block::first_block() --- find the first cfg block of the super block.
*the_full_cnl -- is gloable variable in main.cpp
*/
cfg_node *super_block::first_block() {
cfg_node *first = NULL;
cfg_node_list_iter full_cnl_iter(the_full_cnl);
while (!full_cnl_iter.is_empty()) {
cfg_node *test = full_cnl_iter.step();
if (occurs(test, nodes())) {
first = test;
break;
}
}
return first;
}
/*
*super_block::last_block() --- return the last block of the super block
*/
cfg_node *super_block::last_block() {
cfg_node *last = NULL;
cfg_node_list_iter full_cnl_iter(the_full_cnl);
while (!full_cnl_iter.is_empty()) {
cfg_node *test = full_cnl_iter.step();
if (occurs(test, nodes())) {
last = test;
}
}
return last;
}
/*
*super_block::instr_access() --- return the nth instruction of the super block.
*start at 0.
*/
machine_instr *super_block::instr_access(int n) {
tree_instr *test;
int counter = 0;
cfg_node_list_iter cnl_iter(cnl);
while (!cnl_iter.is_empty()) {
cfg_node *cn = cnl_iter.step();
if (cn->is_block()) {
cfg_node_instr_iter cnii(cn);
while (!cnii.is_empty()) {
test = cnii.step();
machine_instr *mi = (machine_instr *) test->instr();
if ((int) mi->opcode() == mo_null
|| (int) mi->opcode() == mo_lab
|| (int) mi->opcode() == mo_loc) {
continue;
} else if (counter == n) {
return mi;
} else {
counter++;
}
}
}
}
return 0;
}
/*
tnle* super_block::instr_access(int n)
{
tree_instr *test;
int counter = 0;
cfg_node_list_iter cnli(cnl);
while(!cnli.is_empty())
{
cfg_node *cn = cnli.step();
if(cn->is_block())
{
cfg_node_instr_iter cnii(cn);
while(!cnii.is_empty())
{
test = cnii.step();
if(counter == n)
return test->list_e();
else
counter++
}
}
}
return NULL;
}
*/
/*
*super_block::instr_access() --- return the n'th instruction of cfg node(node)
*start at 0.
*/
machine_instr *super_block::instr_access(cfg_node * node, int n) {
tree_instr *test;
int counter = 0;
cfg_node_list_iter cnl_iter(cnl);
while (!cnl_iter.is_empty()) {
cfg_node *cn = cnl_iter.step();
if (cn == node) {
cfg_node_instr_iter cnii(cn);
while (!cnii.is_empty()) {
test = cnii.step();
machine_instr *mi = (machine_instr *) test->instr();
if ((int) mi->opcode() == mo_null
|| (int) mi->opcode() == mo_lab
|| (int) mi->opcode() == mo_loc) {
continue;
} else if (counter == n) {
return mi;
} else {
counter++;
}
}
}
}
return 0;
}
/*
*super_block::instr_count() --- return the machine instruction(mi)'s number ID
*return index[0, (end-1)]
*/
int super_block::instr_count(machine_instr * mi) {
int index = 0;
cfg_node_list_iter cnl_iter(cnl);
while (!cnl_iter.is_empty()) {
cfg_node *cn = cnl_iter.step();
if (cn->is_block()) {
cfg_node_instr_iter cnii(cn);
while (!cnii.is_empty()) {
tree_instr *test = cnii.step();
if ((machine_instr *) test->instr() == mi) {
return index;
} else if ((int) ((machine_instr *) test->instr())->opcode()
== mo_null
|| (int) ((machine_instr *) test->instr())->opcode()
== mo_lab
|| (int) ((machine_instr *) test->instr())->opcode()
== mo_loc) {
continue;
} else {
index++;
}
}
}
}
return 0;
}
/*
*super_block::contains() --- determine the super block whether contains the cfg node(node).
*/
bool super_block::contains(cfg_node * node) {
cfg_node_list_iter iter(cnl);
while (!iter.is_empty()) {
cfg_node *mynode = iter.step();
if (mynode == node)
return true;
}
return false;
}
/*
*super_block::contains() --- determine the super block whether contains machine instruction(mi).
*/
bool super_block::contains(machine_instr * mi) {
cfg_node_list_iter cnli(cnl);
while (!cnli.is_empty()) {
cfg_node *cn = cnli.step();
if (cn->is_block()) {
cfg_block *block = (cfg_block *) cn;
cfg_node_instr_iter cnii(block);
while (!cnii.is_empty()) {
machine_instr *mi_in_block =
(machine_instr *) (cnii.step())->instr();
if (mi == mi_in_block)
return true;
}
}
}
return false;
}
/*
*super_block::in_which_block() --- find tnle position in which block, and return the cfg_block
*/
cfg_block *super_block::in_which_block(tnle * pos) {
cfg_node_list_iter iter(cnl);
while (!iter.is_empty()) {
cfg_node *node = iter.step();
if (node->is_block()) {
cfg_block *block = (cfg_block *) node;
if (block->contains(pos))
return block;
}
}
return 0;
}
/*
*super_block::set_immed_dom() --- set current super block's immed dominator.
*/
void super_block::set_immed_dom(super_block * immed_dom) {
this->immed_dominator = immed_dom;
}
/*
*super_block::immed_dom() --- return current super block's immed dominator.
*/
super_block *super_block::immed_dom() {
return this->immed_dominator;
}
/*
*super_block::set_immed_pdom() --- set current block's immed postdominator.
*/
void super_block::set_immed_pdom(super_block * immed_pdom) {
this->immed_pdominator = immed_pdom;
}
/*
*super_block::immed_pdom() --- return current super block's immed postdominator.
*/
super_block *super_block::immed_pdom() {
return this->immed_pdominator;
}
/*
*super_block::set_fall_succ() --- set current super block's fall successor.
*/
void super_block::set_fall_succ(super_block * fall_succ) {
succs[0] = fall_succ;
}
/*
*super_block::fall_succ() --- return fall successor of the super block.
*/
super_block *super_block::fall_succ() {
return succs[0];
}
/*
*super_block::set_take_succ() --- set current super block's take successor.
*/
void super_block::set_take_succ(super_block * take_succ, double taken_prob) {
succs[1] = take_succ;
this->taken_probability = taken_prob;
}
/*
*super_block::take_succ() --- return current super block's take successor.
*/
super_block *super_block::take_succ() {
return succs[1];
}
/*
*super_block::likely_succ() --- return current super block's likely successor.
*/
super_block *super_block::likely_succ() {
if (taken_prob() > 0.5)
return take_succ();
else
return fall_succ();
}
super_block *super_block::less_likely_succ(){
if(taken_prob() < 0.5)
return take_succ();
else
return fall_succ();
}
/*
*super_block::taken_prob() --- return current super block's taken probability.
*/
double super_block::taken_prob() {
return taken_probability;
}
/*
*super_block::flush() --- flush current cfg node list.
*/
void super_block::flush() {
delete cnl;
cnl = new cfg_node_list();
}
/*
*super_block::print() --- print super block
*/
void super_block::print(FILE * fp) {
cfg_node_list_iter cnli(cnl);
while (!cnli.is_empty()) {
cfg_node *node = cnli.step();
if (node == NULL) {
fprintf(stderr, "access NULL\n");
exit(1);
}
node->print(fp);
}
}
/*
*super_block::print_relationship() --- print super block relationship, such as: cfg nodes contained in each super block, and
* each super block's fall super block, take super block, dominator, and postdominator
* super block.
*/
void super_block::print_relationship(FILE * fp) {
fprintf(fp, "super block %d : ", super_block_num);
fprintf(fp, "contain: <");
cfg_node_list_iter iter(cnl);
while (!iter.is_empty()) {
cfg_node *node = iter.step();
fprintf(fp, "%d ", node->number());
}
fprintf(fp, ">");
fprintf(fp, "fall:");
if (succs[0] == 0)
fprintf(fp, "nil ");
else
fprintf(fp, "%d ", succs[0]->block_num());
fprintf(fp, "take:");
if (succs[1] == 0)
fprintf(fp, "nil ");
else
fprintf(fp, "%d ", succs[1]->block_num());
fprintf(fp, "dom:");
if (immed_dominator == 0)
fprintf(fp, " nil ");
else
fprintf(fp, "%d ", immed_dominator->block_num());
fprintf(fp, "pdom:");
if (immed_pdominator == 0)
fprintf(fp, " nil ");
else
fprintf(fp, "%d ", immed_pdominator->block_num());
fprintf(fp, "\n");
}
super_block *super_block::get_nloop_spawn_super_block_by_vec(
const std::vector<int> & spawn_vec, da_cfg * cfg) {
assert(!spawn_vec.empty());
super_block *nodes = new super_block(super_block::BLOCK_LIST);
for (int i = 0; i < spawn_vec.size(); ++i) {
cfg_node *cn = cfg->node(spawn_vec[i]);
nodes->add_block(cn);
assert(! cfg->is_loop_begin(cn));//此处不必加限制if(i > 0)
}
return nodes;
}
//have some problem
/*
*super_block::operator=() --- super block's copy constructor
*/
super_block & super_block::operator=(const super_block & other) {
if (this == &other)
return *this;
delete cnl;
cnl = new cfg_node_list();
cnl->copy(other.cnl);
return *this;
}
//-------------------------------------------------------------------
int cfg_node_static_instr_size(cfg_node *node) {
assert(node->is_block());
int counter = 0;
cfg_node_instr_iter cnii(node);
while (!cnii.is_empty()) {
tree_instr *test = cnii.step();
machine_instr *mi = (machine_instr *) test->instr();
if ((int) mi->opcode() == mo_lab || (int) mi->opcode() == mo_loc
|| (int) mi->opcode() == mo_null){
continue;
}
else{
counter++;
}
}
return counter;
}
machine_instr *instr_access(cfg_node *node, int index) {
assert(NULL != node);
assert(index >= 0);
assert(node->is_block());
int count = cfg_node_static_instr_size(node);
assert(index < count);
machine_instr *target = NULL;
cfg_node_instr_iter instrs(node);
while (!instrs.is_empty()) {
tree_instr *ti = instrs.step();
if ((int) ((machine_instr *) ti->instr())->opcode() == mo_null
|| (int) ((machine_instr *) ti->instr())->opcode() == mo_lab
|| (int) ((machine_instr *) ti->instr())->opcode() == mo_loc) {
continue;
} else if (index == 0) {
target = (machine_instr*)ti->instr();
break;
} else {
--index;
}
}
assert(NULL != target);
return target;
}
/**
* 动态指令数,包含函数调用代价,体现过程间关系
*/
int block_dynamic_size(cfg_block *cb){
assert(cb->is_block());
int size = 0;
cfg_node_instr_iter instr_iter(cb);
while (!instr_iter.is_empty()) {
tree_instr *ti = instr_iter.step();
machine_instr *mi = (machine_instr *) ti->instr();
if ((int) mi->opcode() == mo_null || (int) mi->opcode() == mo_lab
|| (int) mi->opcode() == mo_loc) {
continue;
} else if ((int) mi->opcode() == mo_jalr) {
immed_list *iml = (immed_list *) (mi->peek_annote(k_call_overhead));
if (iml) {
int overhead = (*iml)[0].integer();
if (overhead == 0)
size++;
else
size += overhead;
} else
size++;
} else {
size++;
}
}
return size;
}
/*
*block_dynamic_size_from_instr() --- compute block's size from the instruction.
计算从这条指令开始的块的大小,不包括mo_null、mo_lab和mo_loc
包括 函数调用代价-它体现了过程之间的关系
当mi_instr本身是mo_jalr型指令
*/
int block_dynamic_size_from_instr(cfg_block * cb, machine_instr * mi_instr) {
int size = 0;
bool is_begin = false;
cfg_node_instr_iter cb_instr_iter(cb);
while (!cb_instr_iter.is_empty()) {
tree_instr *ti = cb_instr_iter.step();
machine_instr *mi = (machine_instr *) ti->instr();
//从mi_instr指令算起,它是块中的第一条指令
if (mi == mi_instr) {
is_begin = true;
}
if (is_begin) {
if ((int) mi->opcode() == mo_null || (int) mi->opcode() == mo_lab
|| (int) mi->opcode() == mo_loc) {
continue;
} else if ((int) mi->opcode() == mo_jalr) {
immed_list *iml = (immed_list *) (mi->peek_annote(
k_call_overhead));
if (iml) {
int overhead = (*iml)[0].integer(); //指的是被调用的指令条数
if (overhead == 0) {
size++;
} else {
size += overhead;
}
} else {
size++;
}
} else {
size++;
}
}
}
assert(size != 0);
return size;
}
super_block* super_block::multi_likely_succ() {
if (taken_prob() < 0.15)
return fall_succ();
else
return take_succ();
}
bool super_block::taken_multi_suit() {
if(taken_prob() >= 0.001){
return true;
}
return false;
}
bool super_block::fall_multi_suit() {
if(taken_prob() <= 0.999){
return true;
}
return false;
}