-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg.cpp
More file actions
810 lines (680 loc) · 24.6 KB
/
cfg.cpp
File metadata and controls
810 lines (680 loc) · 24.6 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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#include <cassert>
#include <cstdio>
#include <algorithm>
#include "cpputil.h"
#include "cfg.h"
////////////////////////////////////////////////////////////////////////
// Operand implementation
////////////////////////////////////////////////////////////////////////
Operand::Operand()
: m_kind(OPERAND_NONE)
, m_basereg(0)
, m_indexreg(0)
, m_ival(0)
{
}
Operand::Operand(OperandKind kind, long ival)
: m_kind(kind)
, m_basereg(0)
, m_indexreg(0)
, m_ival(0) {
assert(kind == OPERAND_VREG || kind == OPERAND_MREG ||
kind == OPERAND_VREG_MEMREF || kind == OPERAND_MREG_MEMREF ||
kind == OPERAND_INT_LITERAL);
if (kind == OPERAND_INT_LITERAL) {
m_ival = ival;
} else {
m_basereg = int(ival);
}
}
Operand::Operand(OperandKind kind, int basereg, int offset_or_index)
: m_kind(kind)
, m_basereg(basereg)
, m_indexreg(0)
, m_ival(0) {
assert(kind == OPERAND_VREG_MEMREF_OFFSET || kind == OPERAND_VREG_MEMREF_INDEX ||
kind == OPERAND_MREG_MEMREF_OFFSET || kind == OPERAND_MREG_MEMREF_INDEX);
if (kind == OPERAND_VREG_MEMREF_OFFSET || kind == OPERAND_MREG_MEMREF_OFFSET) {
m_ival = offset_or_index;
} else {
m_indexreg = offset_or_index;
}
}
Operand::Operand(OperandKind kind, int basereg, int indexreg, int offset)
: m_kind(kind)
, m_basereg(basereg)
, m_indexreg(indexreg)
, m_ival(offset) {
// currently there is only one kind of reg+reg+offset operand
assert(m_kind == OPERAND_MREG_MEMREF_OFFSET_INDEX);
}
Operand::Operand(const std::string &target_label, bool is_immediate)
: m_kind(is_immediate ? OPERAND_LABEL_IMMEDIATE : OPERAND_LABEL)
, m_basereg(0)
, m_indexreg(0)
, m_ival(0)
, m_target_label(target_label) {
}
bool Operand::has_base_reg() const {
return (m_kind & OPROP_HAS_BASEREG) != 0;
}
bool Operand::has_index_reg() const {
return (m_kind & OPROP_HAS_INDEXREG) != 0;
}
int Operand::get_base_reg() const {
assert(has_base_reg());
return m_basereg;
}
int Operand::get_index_reg() const {
assert(has_index_reg());
return m_indexreg;
}
long Operand::get_int_value() const {
assert(m_kind == OPERAND_INT_LITERAL);
return m_ival;
}
int Operand::get_offset() const {
assert((m_kind & OPROP_HAS_INTVAL) != 0 && (m_kind & OPROP_IS_MEMREF) != 0);
return int(m_ival);
}
std::string Operand::get_target_label() const {
assert((m_kind & OPROP_HAS_LABEL) != 0);
return m_target_label;
}
////////////////////////////////////////////////////////////////////////
// Instruction implementation
////////////////////////////////////////////////////////////////////////
Instruction::Instruction(int opcode)
: m_opcode(opcode)
, m_num_operands(0) {
}
Instruction::Instruction(int opcode, Operand op1)
: m_opcode(opcode)
, m_num_operands(1) {
m_operands[0] = op1;
}
Instruction::Instruction(int opcode, Operand op1, Operand op2)
: m_opcode(opcode)
, m_num_operands(2) {
m_operands[0] = op1;
m_operands[1] = op2;
}
Instruction::Instruction(int opcode, Operand op1, Operand op2, Operand op3)
: m_opcode(opcode)
, m_num_operands(3) {
m_operands[0] = op1;
m_operands[1] = op2;
m_operands[2] = op3;
}
unsigned Instruction::get_num_operands() const {
return m_num_operands;
}
Operand Instruction::get_operand(unsigned index) const {
assert(index >= 0);
assert(index < m_num_operands);
return m_operands[index];
}
void Instruction::set_operand(unsigned index, Operand op) {
assert(index >= 0);
assert(index < m_num_operands);
m_operands[index] = op;
}
void Instruction::set_comment(const std::string &comment) {
m_comment = comment;
}
bool Instruction::has_comment() const {
return !m_comment.empty();
}
const std::string &Instruction::get_comment() const {
return m_comment;
}
Instruction *Instruction::duplicate() const {
return new Instruction(*this);
}
////////////////////////////////////////////////////////////////////////
// InstructionSequence implementation
////////////////////////////////////////////////////////////////////////
InstructionSequence::InstructionSequence() {
}
void InstructionSequence::add_instruction(Instruction *ins) {
m_labels.push_back(m_next_label);
m_instr_seq.push_back(ins);
m_next_label = "";
}
void InstructionSequence::define_label(const std::string &label) {
assert(m_next_label.empty());
m_next_label = label;
m_label_to_index[label] = unsigned(m_instr_seq.size());
}
void InstructionSequence::define_label_if_necessary(const std::string &label, Instruction *branch) {
assert(branch->get_num_operands() == 1);
assert((*branch)[0].get_target_label() == label);
if (m_next_label.empty()) {
// define the label
define_label(label);
} else {
// use the existing label
(*branch)[0] = Operand(m_next_label);
}
}
Instruction *InstructionSequence::get_labeled_instruction(const std::string &label) const {
auto i = m_label_to_index.find(label);
if (i == m_label_to_index.cend()) {
// nonexistent label
return nullptr;
}
unsigned index = i->second;
assert(index < unsigned(m_instr_seq.size()));
return m_instr_seq[index];
}
unsigned InstructionSequence::get_index_of_labeled_instruction(const std::string &label) const {
auto i = m_label_to_index.find(label);
assert(i != m_label_to_index.cend());
return i->second;
}
unsigned InstructionSequence::get_length() const {
return unsigned(m_instr_seq.size());
}
Instruction *InstructionSequence::get_instruction(unsigned index) const {
assert(index < unsigned(m_instr_seq.size()));
return m_instr_seq[index];
}
Instruction *InstructionSequence::get_last() const {
assert(!m_instr_seq.empty());
return m_instr_seq.back();
}
bool InstructionSequence::has_label(unsigned index) const {
/*
assert(index < unsigned(m_instr_seq.size()));
return !m_labels[index].empty();
*/
if (index == unsigned(m_instr_seq.size())) {
return has_label_at_end();
} else {
return !m_labels[index].empty();
}
}
std::string InstructionSequence::get_label(unsigned index) const {
assert(has_label(index));
return m_labels[index];
}
bool InstructionSequence::has_label_at_end() const {
return !m_next_label.empty();
}
std::string InstructionSequence::get_label_at_end() const {
assert(has_label_at_end());
return m_next_label;
}
////////////////////////////////////////////////////////////////////////
// PrintInstructionSequence implementation
////////////////////////////////////////////////////////////////////////
PrintInstructionSequence::PrintInstructionSequence(InstructionSequence *iseq)
: m_iseq(iseq) {
}
std::string PrintInstructionSequence::format_instruction(const Instruction *ins) {
std::string formatted_ins;
formatted_ins += get_opcode_name(ins->get_opcode());
formatted_ins += " ";
for (unsigned j = 0; j < ins->get_num_operands(); j++) {
Operand operand = ins->get_operand(j);
if (j > 0) {
formatted_ins += ", ";
}
formatted_ins += format_operand(operand);
}
unsigned len(formatted_ins.size());
if (ins->has_comment()) {
for (unsigned i = len; i < 28; i++) {
formatted_ins += ' ';
}
formatted_ins += "/* ";
formatted_ins += ins->get_comment();
formatted_ins += " */";
}
return formatted_ins;
}
void PrintInstructionSequence::print() {
for (unsigned i = 0; i < m_iseq->get_length(); i++) {
if (m_iseq->has_label(i)) {
std::string label = m_iseq->get_label(i);
printf("%s:\n", label.c_str());
}
Instruction *ins = m_iseq->get_instruction(i);
std::string formatted_ins = format_instruction(ins);
printf("\t%s\n", formatted_ins.c_str());
}
// special case: if there is a label at the end, print it
if (m_iseq->has_label_at_end()) {
printf("%s:\n", m_iseq->get_label_at_end().c_str());
}
}
std::string PrintInstructionSequence::format_operand(const Operand &operand) {
assert(operand.get_kind() != OPERAND_NONE);
switch (operand.get_kind()) {
case OPERAND_VREG:
return cpputil::format("vr%d", operand.get_base_reg());
case OPERAND_VREG_MEMREF:
return cpputil::format("(vr%d)", operand.get_base_reg());
case OPERAND_VREG_MEMREF_OFFSET:
return cpputil::format("%d(vr%d)", operand.get_offset(), operand.get_base_reg());
case OPERAND_VREG_MEMREF_INDEX:
return cpputil::format("(vr%d,vr%d)", operand.get_base_reg(), operand.get_index_reg());
case OPERAND_MREG:
return get_mreg_name(operand.get_base_reg());
case OPERAND_MREG_MEMREF:
return cpputil::format("(%s)", get_mreg_name(operand.get_base_reg()).c_str());
case OPERAND_MREG_MEMREF_OFFSET:
return cpputil::format("%d(%s)", operand.get_offset(), get_mreg_name(operand.get_base_reg()).c_str());
case OPERAND_MREG_MEMREF_INDEX:
return cpputil::format("(%s,%s)",
get_mreg_name(operand.get_base_reg()).c_str(),
get_mreg_name(operand.get_index_reg()).c_str());
case OPERAND_MREG_MEMREF_OFFSET_INDEX:
return cpputil::format("%d(%s, %s)",
operand.get_offset(),
get_mreg_name(operand.get_base_reg()).c_str(),
get_mreg_name(operand.get_index_reg()).c_str());
case OPERAND_INT_LITERAL:
return cpputil::format("$%ld", operand.get_int_value());
case OPERAND_LABEL:
return operand.get_target_label();
case OPERAND_LABEL_IMMEDIATE:
return "$" + operand.get_target_label();
default:
assert(false);
return "<invalid>";
}
}
////////////////////////////////////////////////////////////////////////
// BasicBlock implementation
////////////////////////////////////////////////////////////////////////
BasicBlock::BasicBlock(BasicBlockKind kind, unsigned id, const std::string &label)
: m_kind(kind)
, m_id(id)
, m_label(label) {
}
BasicBlock::~BasicBlock() {
}
BasicBlockKind BasicBlock::get_kind() const {
return m_kind;
}
unsigned BasicBlock::get_id() const {
return m_id;
}
bool BasicBlock::has_label() const {
return !m_label.empty();
}
std::string BasicBlock::get_label() const {
return m_label;
}
void BasicBlock::set_label(const std::string &label) {
assert(!has_label());
m_label = label;
}
////////////////////////////////////////////////////////////////////////
// Edge implementation
////////////////////////////////////////////////////////////////////////
Edge::Edge(BasicBlock *source, BasicBlock *target, EdgeKind kind)
: m_kind(kind)
, m_source(source)
, m_target(target) {
}
Edge::~Edge() {
}
////////////////////////////////////////////////////////////////////////
// ControlFlowGraph implementation
////////////////////////////////////////////////////////////////////////
ControlFlowGraph::ControlFlowGraph()
: m_entry(nullptr)
, m_exit(nullptr) {
}
ControlFlowGraph::~ControlFlowGraph() {
}
BasicBlock *ControlFlowGraph::get_entry_block() const {
return m_entry;
}
BasicBlock *ControlFlowGraph::get_exit_block() const {
return m_exit;
}
BasicBlock *ControlFlowGraph::create_basic_block(BasicBlockKind kind, const std::string &label) {
BasicBlock *bb = new BasicBlock(kind, unsigned(m_basic_blocks.size()), label);
m_basic_blocks.push_back(bb);
if (bb->get_kind() == BASICBLOCK_ENTRY) {
assert(m_entry == nullptr);
m_entry = bb;
}
if (bb->get_kind() == BASICBLOCK_EXIT) {
assert(m_exit == nullptr);
m_exit = bb;
}
return bb;
}
Edge *ControlFlowGraph::create_edge(BasicBlock *source, BasicBlock *target, EdgeKind kind) {
// make sure BasicBlocks belong to this ControlFlowGraph
assert(std::find(m_basic_blocks.begin(), m_basic_blocks.end(), source) != m_basic_blocks.end());
assert(std::find(m_basic_blocks.begin(), m_basic_blocks.end(), target) != m_basic_blocks.end());
// make sure this Edge doesn't already exist
assert(lookup_edge(source, target) == nullptr);
// create the edge, add it to outgoing/incoming edge maps
Edge *e = new Edge(source, target, kind);
m_outgoing_edges[source].push_back(e);
m_incoming_edges[target].push_back(e);
return e;
}
Edge *ControlFlowGraph::lookup_edge(BasicBlock *source, BasicBlock *target) const {
auto i = m_outgoing_edges.find(source);
if (i == m_outgoing_edges.cend()) {
return nullptr;
}
const EdgeList &outgoing = i->second;
for (auto j = outgoing.cbegin(); j != outgoing.cend(); j++) {
Edge *e = *j;
assert(e->get_source() == source);
if (e->get_target() == target) {
return e;
}
}
return nullptr;
}
const ControlFlowGraph::EdgeList &ControlFlowGraph::get_outgoing_edges(BasicBlock *bb) const {
EdgeMap::const_iterator i = m_outgoing_edges.find(bb);
return i == m_outgoing_edges.end() ? m_empty_edge_list : i->second;
}
const ControlFlowGraph::EdgeList &ControlFlowGraph::get_incoming_edges(BasicBlock *bb) const {
EdgeMap::const_iterator i = m_incoming_edges.find(bb);
return i == m_incoming_edges.end() ? m_empty_edge_list : i->second;
}
InstructionSequence *ControlFlowGraph::create_instruction_sequence() const {
assert(m_entry != nullptr);
assert(m_exit != nullptr);
assert(m_outgoing_edges.size() == m_incoming_edges.size());
// Find all Chunks (groups of basic blocks connected via fall-through)
typedef std::map<BasicBlock *, Chunk *> ChunkMap;
ChunkMap chunk_map;
for (auto i = m_outgoing_edges.cbegin(); i != m_outgoing_edges.cend(); i++) {
const EdgeList &outgoing_edges = i->second;
for (auto j = outgoing_edges.cbegin(); j != outgoing_edges.cend(); j++) {
Edge *e = *j;
if (e->get_kind() != EDGE_FALLTHROUGH) {
continue;
}
BasicBlock *pred = e->get_source();
BasicBlock *succ = e->get_target();
Chunk *pred_chunk = (chunk_map.find(pred) == chunk_map.end()) ? nullptr : chunk_map[pred];
Chunk *succ_chunk = (chunk_map.find(succ) == chunk_map.end()) ? nullptr : chunk_map[succ];
if (pred_chunk == nullptr && succ_chunk == nullptr) {
// create a new chunk
Chunk *chunk = new Chunk();
chunk->append(pred);
chunk->append(succ);
chunk_map[pred] = chunk;
chunk_map[succ] = chunk;
} else if (pred_chunk == nullptr) {
// prepend predecessor to successor's chunk (successor should be the first block)
assert(succ_chunk->is_first(succ));
succ_chunk->prepend(pred);
chunk_map[pred] = succ_chunk;
} else if (succ_chunk == nullptr) {
// append successor to predecessor's chunk (predecessor should be the last block)
assert(pred_chunk->is_last(pred));
pred_chunk->append(succ);
chunk_map[succ] = pred_chunk;
} else {
// merge the chunks
Chunk *merged = pred_chunk->merge_with(succ_chunk);
// update every basic block to point to the merged chunk
for (auto i = merged->blocks.begin(); i != merged->blocks.end(); i++) {
BasicBlock *bb = *i;
chunk_map[bb] = merged;
}
// delete old Chunks
delete pred_chunk;
delete succ_chunk;
}
}
}
InstructionSequence *result = new InstructionSequence();
std::vector<bool> finished_blocks(get_num_blocks(), false);
Chunk *exit_chunk = nullptr;
// Traverse the CFG, appending basic blocks to the generated InstructionSequence.
// If we find a block that is part of a Chunk, the entire Chunk is emitted.
// (This allows fall through to work.)
std::deque<BasicBlock *> work_list;
work_list.push_back(m_entry);
while (!work_list.empty()) {
BasicBlock *bb = work_list.front();
work_list.pop_front();
unsigned block_id = bb->get_id();
if (finished_blocks[block_id]) {
// already added this block
continue;
}
ChunkMap::iterator i = chunk_map.find(bb);
if (i != chunk_map.end()) {
// This basic block is part of a Chunk: append all of its blocks
Chunk *chunk = i->second;
// If this chunk contains the exit block, it needs to be at the end
// of the generated InstructionSequence, so defer appending any of
// its blocks. (But, *do* find its control successors.)
bool is_exit_chunk = false;
if (chunk->contains_exit_block()) {
exit_chunk = chunk;
is_exit_chunk = true;
}
for (auto j = chunk->blocks.begin(); j != chunk->blocks.end(); j++) {
BasicBlock *b = *j;
if (is_exit_chunk) {
// mark the block as finished, but don't append its instructions yet
finished_blocks[b->get_id()] = true;
} else {
append_basic_block(result, b, finished_blocks);
}
// Visit control successors
visit_successors(b, work_list);
}
} else {
// This basic block is not part of a Chunk
append_basic_block(result, bb, finished_blocks);
// Visit control successors
visit_successors(bb, work_list);
}
}
// append exit chunk
if (exit_chunk != nullptr) {
append_chunk(result, exit_chunk, finished_blocks);
}
return result;
}
void ControlFlowGraph::append_basic_block(InstructionSequence *iseq, const BasicBlock *bb, std::vector<bool> &finished_blocks) const {
if (bb->has_label()) {
iseq->define_label(bb->get_label());
}
for (auto i = bb->cbegin(); i != bb->cend(); i++) {
iseq->add_instruction((*i)->duplicate());
}
finished_blocks[bb->get_id()] = true;
}
void ControlFlowGraph::append_chunk(InstructionSequence *iseq, Chunk *chunk, std::vector<bool> &finished_blocks) const {
for (auto i = chunk->blocks.begin(); i != chunk->blocks.end(); i++) {
append_basic_block(iseq, *i, finished_blocks);
}
}
void ControlFlowGraph::visit_successors(BasicBlock *bb, std::deque<BasicBlock *> &work_list) const {
const EdgeList &outgoing_edges = get_outgoing_edges(bb);
for (auto k = outgoing_edges.cbegin(); k != outgoing_edges.cend(); k++) {
Edge *e = *k;
work_list.push_back(e->get_target());
}
}
////////////////////////////////////////////////////////////////////////
// ControlFlowGraphBuilder implementation
////////////////////////////////////////////////////////////////////////
ControlFlowGraphBuilder::ControlFlowGraphBuilder(InstructionSequence *iseq)
: m_iseq(iseq)
, m_cfg(new ControlFlowGraph()) {
}
ControlFlowGraphBuilder::~ControlFlowGraphBuilder() {
}
ControlFlowGraph *ControlFlowGraphBuilder::build() {
unsigned num_instructions = m_iseq->get_length();
BasicBlock *entry = m_cfg->create_basic_block(BASICBLOCK_ENTRY);
BasicBlock *exit = m_cfg->create_basic_block(BASICBLOCK_EXIT);
// exit block is reached by any branch that targets the end of the
// InstructionSequence
m_basic_blocks[num_instructions] = exit;
std::deque<WorkItem> work_list;
work_list.push_back({ ins_index: 0, pred: entry, edge_kind: EDGE_FALLTHROUGH });
BasicBlock *last = nullptr;
while (!work_list.empty()) {
WorkItem item = work_list.front();
work_list.pop_front();
assert(item.ins_index <= m_iseq->get_length());
// if item target is end of InstructionSequence, then it targets the exit block
if (item.ins_index == m_iseq->get_length()) {
m_cfg->create_edge(item.pred, exit, item.edge_kind);
continue;
}
BasicBlock *bb;
bool is_new_block;
std::map<unsigned, BasicBlock *>::iterator i = m_basic_blocks.find(item.ins_index);
if (i != m_basic_blocks.end()) {
// a block starting at this instruction already exists
bb = i->second;
is_new_block = false;
// Special case: if this block was originally discovered via a fall-through
// edge, but is also reachable via a branch, then it might not be labeled
// yet. Set the label if necessary.
if (item.edge_kind == EDGE_BRANCH && !bb->has_label()) {
bb->set_label(item.label);
}
} else {
// no block starting at this instruction currently exists:
// scan the basic block and add it to the map of known basic blocks
// (indexed by instruction index)
bb = scan_basic_block(item, item.label);
is_new_block = true;
m_basic_blocks[item.ins_index] = bb;
}
// if the edge is a branch, make sure the work item's label matches
// the BasicBlock's label (if it doesn't, then somehow this block
// is reachable via two different labels, which shouldn't be possible)
assert(item.edge_kind != EDGE_BRANCH || bb->get_label() == item.label);
// connect to predecessor
m_cfg->create_edge(item.pred, bb, item.edge_kind);
if (!is_new_block) {
// we've seen this block already
continue;
}
// if this basic block ends in a branch, prepare to create an edge
// to the BasicBlock for the target (creating the BasicBlock if it
// doesn't exist yet)
if (ends_in_branch(bb)) {
unsigned target_index = get_branch_target_index(bb);
// Note: we assume that branch instructions have a single Operand,
// which is a label
Instruction *branch = bb->get_last();
assert(branch->get_num_operands() == 1);
Operand operand = (*branch)[0];
assert(operand.get_kind() == OPERAND_LABEL);
std::string target_label = operand.get_target_label();
work_list.push_back({ ins_index: target_index, pred: bb, edge_kind: EDGE_BRANCH, label: target_label });
}
// if this basic block falls through, prepare to create an edge
// to the BasicBlock for successor instruction (creating it if it doesn't
// exist yet)
if (falls_through(bb)) {
unsigned target_index = item.ins_index + bb->get_length();
assert(target_index <= m_iseq->get_length());
if (target_index == num_instructions) {
// this is the basic block at the end of the instruction sequence,
// its fall-through successor should be the exit block
last = bb;
} else {
// fall through to basic block starting at successor instruction
work_list.push_back({ ins_index: target_index, pred: bb, edge_kind: EDGE_FALLTHROUGH });
}
}
}
assert(last != nullptr);
m_cfg->create_edge(last, exit, EDGE_FALLTHROUGH);
return m_cfg;
}
// Note that subclasses may override this method.
bool ControlFlowGraphBuilder::is_branch(Instruction *ins) {
// assume that any branch instruction will have a single operand which is
// a label
return (ins->get_num_operands() != 1) ? false : (*ins)[0].get_kind() == OPERAND_LABEL;
}
BasicBlock *ControlFlowGraphBuilder::scan_basic_block(const WorkItem &item, const std::string &label) {
unsigned index = item.ins_index;
BasicBlock *bb = m_cfg->create_basic_block(BASICBLOCK_INTERIOR, label);
// keep adding instructions until we
// - reach an instruction that is a branch
// - reach an instruction that is a target of a branch
// - reach the end of the overall instruction sequence
while (index < m_iseq->get_length()) {
Instruction *ins = m_iseq->get_instruction(index);
// this instruction is part of the basic block
ins = ins->duplicate();
bb->add_instruction(ins);
index++;
if (m_iseq->has_label(index)) {
// instruction at index is a control target
break;
}
if (is_branch(ins)) {
// this is a branch instruction
break;
}
}
assert(bb->get_length() > 0);
return bb;
}
bool ControlFlowGraphBuilder::ends_in_branch(BasicBlock *bb) {
return is_branch(bb->get_last());
}
unsigned ControlFlowGraphBuilder::get_branch_target_index(BasicBlock *bb) {
assert(ends_in_branch(bb));
Instruction *last = bb->get_last();
// assume that any branch instruction will have a single operand which is
// a label
assert(last->get_num_operands() == 1);
Operand label = (*last)[0];
assert(label.get_kind() == OPERAND_LABEL);
// look up the index of the instruction targeted by this label
unsigned target_index = m_iseq->get_index_of_labeled_instruction(label.get_target_label());
return target_index;
}
bool ControlFlowGraphBuilder::falls_through(BasicBlock *bb) {
return falls_through(bb->get_last());
}
////////////////////////////////////////////////////////////////////////
// ControlFlowGraphPrinter implementation
////////////////////////////////////////////////////////////////////////
ControlFlowGraphPrinter::ControlFlowGraphPrinter(ControlFlowGraph *cfg)
: m_cfg(cfg) {
}
ControlFlowGraphPrinter::~ControlFlowGraphPrinter() {
}
void ControlFlowGraphPrinter::print() {
for (auto i = m_cfg->bb_begin(); i != m_cfg->bb_end(); i++) {
BasicBlock *bb = *i;
printf("BASIC BLOCK %u", bb->get_id());
BasicBlockKind bb_kind = bb->get_kind();
if (bb_kind != BASICBLOCK_INTERIOR) {
printf(" %s", bb_kind == BASICBLOCK_ENTRY ? "[entry]" : "[exit]");
}
if (bb->has_label()) {
printf(" (label %s)", bb->get_label().c_str());
}
printf("\n");
print_basic_block(bb);
const ControlFlowGraph::EdgeList &outgoing_edges = m_cfg->get_outgoing_edges(bb);
for (auto j = outgoing_edges.cbegin(); j != outgoing_edges.cend(); j++) {
const Edge *e = *j;
assert(e->get_kind() == EDGE_BRANCH || e->get_kind() == EDGE_FALLTHROUGH);
printf(" %s EDGE to BASIC BLOCK %u\n", e->get_kind() == EDGE_FALLTHROUGH ? "fall-through" : "branch", e->get_target()->get_id());
}
printf("\n");
}
}