-
Notifications
You must be signed in to change notification settings - Fork 9
/
InterProcCFG.cc
853 lines (752 loc) · 24.9 KB
/
InterProcCFG.cc
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#include "func.h"
#include "cfg.h"
#include "callgraph.h"
#include "warning.h"
#include "prog.h"
#include "InterProcCFG.h"
#include "serialize.h"
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <stdio.h>
#include <vector>
#include <string>
#include <map>
using namespace std;
using namespace boost;
static const char *func_unique_name(Function *func) {
static char buf[256];
const char *name = func->getName();
if (!strcmp(name, "unknown")) {
snprintf(buf, sizeof(buf), "unknown_0x%08x", func->getAddress());
return buf;
} else {
return name;
}
}
#define DISTANCE_INFINITY 1000000L
#define G2_ADDR(x) ((x) | 0x80000000)
#define G1_ADDR(x) ((x) & ~0x80000000)
#define IS_G1(x) (!((x)->getAddress() & 0x80000000))
#define IS_G2(x) ((x)->getAddress() & 0x80000000)
void
InterProcCFG::to_vcg(ostream &out) {
char tmp[1024];
bool flat = false;
property_map<IPCFGraph, vertex_index_t>::type
index = get(vertex_index_t(), ipcfg);
property_map<IPCFGraph, bb_ptr_t>::type bb_ptr = get(bb_ptr_t(), ipcfg);
property_map<IPCFGraph, edge_type_t>::type edge_type
= get(edge_type_t(), ipcfg);
property_map<IPCFGraph, vertex_distance_t>::type
distance = get(vertex_distance_t(), ipcfg);
property_map<IPCFGraph, paths_to_header_t>::type
pth = get(paths_to_header_t(), ipcfg);
out << "graph: {" << endl;
if (flat) {
graph_traits<IPCFGraph>::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(ipcfg); vi != vi_end; ++vi) {
int bb_num = index[*vi];
BasicBlock *bb = bb_ptr[*vi];
sprintf(tmp, "node: { title: \"bb%d\" "
"label: \"%.8x-%.8x [%lld]\"}",
bb_num, bb->getAddress(),
bb->getAddress() + bb->getSize() - 1,
distance[*vi]);
out << tmp << endl;
}
} else {
for (subgraph_map::iterator fi = subgraphs.begin();
fi != subgraphs.end(); ++fi) {
string func_name = (*fi).first;
vector<IPCFGNode> &nodes = (*fi).second;
out << "graph: { title: \"" << func_name << "\"" << endl;
out << " status: black" << endl;
for (vector<IPCFGNode>::iterator ni = nodes.begin();
ni != nodes.end(); ++ni) {
int bb_num = index[*ni];
BasicBlock *bb = bb_ptr[*ni];
const char *options;
if (interesting_loops.find(bb->getAddress()) !=
interesting_loops.end()) {
options = " color: khaki";
} else if (warning && warning->slice_find(bb->getAddress())) {
options = " color: orchid";
} else if (is_loop_exit_cond(bb)) {
options = " color: lightgreen";
} else {
options = "";
}
sprintf(tmp, " node: { title: \"bb%d\" "
"label: \"%.8x-%.8x [%lld] (%ld)\"%s}",
bb_num, bb->getAddress(),
bb->getAddress() + bb->getSize() - 1,
distance[*ni], pth[*ni], options);
out << tmp << endl;
}
out << "}" << endl;
}
}
graph_traits<IPCFGraph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(ipcfg); ei != ei_end; ++ei) {
int bb_num1 = index[target(*ei, ipcfg)];
int bb_num2 = index[source(*ei, ipcfg)];
const char *options;
switch (edge_type[*ei]) {
case INTRA_EDGE:
options = "color: black priority: 10"; break;
case CALL_EDGE:
options = "color: blue priority: 10"; break;
case RETURN_EDGE:
options = "color: red priority: 1"; break;
case CALL_TO_RET_EDGE:
options = "color: lightgray priority: 10"; break;
case BACK_EDGE:
options = "color: darkgreen priority: 1"; break;
default:
assert(0);
options = "color: black priority: 5"; break;
}
sprintf(tmp, "edge: { sourcename: \"bb%d\" targetname: \"bb%d\" %s}",
bb_num1, bb_num2, options);
out << tmp << endl;
}
if (0 /* show post-dominator edges */) {
graph_traits<IPCFGraph>::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(ipcfg); vi != vi_end; ++vi) {
int bb_num1 = index[*vi];
BasicBlock *bb = bb_ptr[*vi];
BasicBlock *ipd = bb->getCfg()->getPostDominator(bb);
if (ipd) {
int bb_num2 = index[bb_to_node[ipd]];
sprintf(tmp, "edge: { sourcename: \"bb%d\" "
"targetname: \"bb%d\""
" color: purple}", bb_num1, bb_num2);
out << tmp << endl;
}
}
}
if (0 /* show component edges */) {
graph_traits<IPCFGraph>::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(ipcfg); vi != vi_end; ++vi) {
int bb_num1 = index[*vi];
BasicBlock *bb = bb_ptr[*vi];
BasicBlock *leader = bb->getCfg()->getComponent(bb);
if (bb != leader) {
/* Pink edge from node to component leader */
int bb_num2 = index[bb_to_node[leader]];
sprintf(tmp, "edge: { sourcename: \"bb%d\" "
"targetname: \"bb%d\""
" color: pink}", bb_num1, bb_num2);
out << tmp << endl;
} else {
/* Orange edge from leader to enclosing leader */
BasicBlock *leader2 =
bb->getCfg()->getEnclosingComponent(leader);
bb_num1 = index[bb_to_node[leader]];
int bb_num2 = index[bb_to_node[leader2]];
sprintf(tmp, "edge: { sourcename: \"bb%d\" "
"targetname: \"bb%d\""
" color: orange}", bb_num1, bb_num2);
out << tmp << endl;
}
}
}
out << "}" << endl;
}
void
InterProcCFG::copy_nodes_to_ipcfg(Function *func) {
property_map<IPCFGraph, bb_ptr_t>::type bb_ptr = get(bb_ptr_t(), ipcfg);
property_map<IPCFGraph, paths_to_header_t>::type
pth = get(paths_to_header_t(), ipcfg);
string func_name = func_unique_name(func);
Cfg *cfg = func->getCfg();
cfg->decode();
basicblocks_t exits(cfg->exits_begin(), cfg->exits_end());
if (0)
cfg->computePostDominators(&exits);
if (0 && exits.size() != 1) {
fprintf(stderr, "Function %s (%08x) has %d exits\n",
func_name.c_str(), func->getAddress(), exits.size());
}
for (Cfg::const_bb_iterator bbit = cfg->bb_begin();
bbit != cfg->bb_end(); bbit++) {
BasicBlock *bb = *bbit;
IPCFGNode n = bb_to_node[bb] = add_vertex(ipcfg);
bb_ptr[n] = bb;
pth[n] = -1;
subgraphs[func_name].push_back(n);
}
}
void
InterProcCFG::make_call_ret_edges(Instruction *i, bool include_returns) {
property_map<IPCFGraph, edge_type_t>::type edge_type
= get(edge_type_t(), ipcfg);
property_map<IPCFGraph, edge_weight_t>::type edge_weight
= get(edge_weight_t(), ipcfg);
BasicBlock *bb = i->getBasicBlock();
Cfg *cfg = bb->getCfg();
// The call should be the last insn in the block
assert(i->getAddress() + i->getSize() ==
bb->getAddress() + bb->getSize());
// The block should fall through to a single successor
BasicBlock *bb_next = 0;
for (Cfg::const_succ_iterator si = cfg->succ_begin(bb);
si != cfg->succ_end(bb); ++si) {
assert_msg(bb_next == 0, "%.8x\n", bb_next->getAddress());
// Loop should not repeat
bb_next = *si;
}
int seen_callees = 0;
IPCFGEdge e; bool okay;
bool multiple_callees = false;
{
functions_t::const_iterator fit = cfg->call_targets_begin(G1_ADDR(i->getAddress()));
if (fit != cfg->call_targets_end(G1_ADDR(i->getAddress()))) {
++fit;
if (fit != cfg->call_targets_end(G1_ADDR(i->getAddress())))
multiple_callees = true;
}
}
for (functions_t::const_iterator fit = cfg->call_targets_begin(G1_ADDR(i->getAddress()));
fit != cfg->call_targets_end(G1_ADDR(i->getAddress())); fit++) {
Function *callee = *fit;
Cfg *callee_cfg = callee->getCfg();
BasicBlock *callee_entry = callee_cfg->getEntry();
if (!callee_entry)
continue;
seen_callees++;
tie(e, okay) =
add_edge(bb_to_node[callee_entry], bb_to_node[bb], ipcfg);
assert(okay);
edge_type[e] = CALL_EDGE;
edge_weight[e] = (multiple_callees ? 1 /* 100 */ : 1);
if (include_returns) {
for (Cfg::const_exit_iterator xit = callee_cfg->exits_begin();
xit != callee_cfg->exits_end(); ++xit) {
BasicBlock *callee_exit = *xit;
tie(e, okay) = add_edge(bb_to_node[bb_next],
bb_to_node[callee_exit], ipcfg);
assert(okay);
edge_type[e] = RETURN_EDGE;
edge_weight[e] = 1;
}
}
}
if (!seen_callees) {
// None of the callees exist in our representation, so add
// a call-to-ret edge unconditionally.
debug2("Adding call-to-ret %.8x --> %.8x\n", bb->getAddress(), bb_next->getAddress());
tie(e, okay) = add_edge(bb_to_node[bb_next], bb_to_node[bb], ipcfg);
assert(okay);
edge_type[e] = CALL_TO_RET_EDGE;
edge_weight[e] = 1;
}
}
void
InterProcCFG::copy_edges_to_ipcfg(Function *func,
bool include_returns,
bool include_call_to_rets,
bool include_calls) {
property_map<IPCFGraph, edge_type_t>::type edge_type
= get(edge_type_t(), ipcfg);
property_map<IPCFGraph, edge_weight_t>::type edge_weight
= get(edge_weight_t(), ipcfg);
(void)include_calls;
const char *func_name = func_unique_name(func);
Cfg *cfg = func->getCfg();
for (Cfg::const_edge_iterator eit = cfg->edge_begin();
eit != cfg->edge_end(); eit++) {
BasicBlockEdge *bbe = *eit;
BasicBlock *bb1 = bbe->getSource();
BasicBlock *bb2 = bbe->getTarget();
bool is_call = bb1->isCall();
IPCFGEdge e;
bool okay;
if (is_call && include_call_to_rets) {
tie(e, okay) = add_edge(bb_to_node[bb2], bb_to_node[bb1], ipcfg);
assert(okay);
edge_type[e] = CALL_TO_RET_EDGE;
edge_weight[e] = DISTANCE_INFINITY;
} else if (!is_call) {
tie(e, okay) = add_edge(bb_to_node[bb2], bb_to_node[bb1], ipcfg);
assert(okay);
int weight;
if (cfg->getComponent(bb1) == bb2) {
edge_type[e] = BACK_EDGE;
weight = DISTANCE_INFINITY;
} else {
edge_type[e] = INTRA_EDGE;
if (cfg->getNumSuccessors(bb1) > 1) {
// branch
weight = 1 /* 100 */;
} else {
weight = 1;
}
}
edge_weight[e] = weight;
}
}
for (Cfg::const_bb_iterator bbit = cfg->bb_begin();
bbit != cfg->bb_end(); bbit++) {
BasicBlock *bb = *bbit;
for (instructions_t::const_iterator iit = bb->inst_begin();
iit != bb->inst_end(); iit++) {
Instruction *i = *iit;
if (i->isCall()) {
make_call_ret_edges(i, include_returns);
}
}
}
}
long InterProcCFG::compute_pth(bool first_call, BasicBlock *bb) {
property_map<IPCFGraph, paths_to_header_t>::type
pth = get(paths_to_header_t(), ipcfg);
Cfg *cfg = bb->getCfg();
BasicBlock *leader = cfg->getComponent(bb);
IPCFGNode node = bb_to_node[bb];
if (bb == leader && !first_call)
return 1;
else if (cfg->isExit(bb)) {
pth[node] = 1;
return 1;
}
if (pth[node] != -1)
return pth[node];
long total = 0;
for (Cfg::const_succ_iterator si = cfg->succ_begin(bb);
si != cfg->succ_end(bb); ++si) {
BasicBlock *succ_bb = *si;
if (cfg->getComponent(succ_bb) == leader) {
IPCFGNode succ_node = bb_to_node[succ_bb];
total += compute_pth(false, succ_bb);
} else {
total++;
}
}
pth[node] = total;
return total;
}
void InterProcCFG::set_target_addr(addr_t target_addr,
map<addr_t, Function *> &functions) {
BasicBlock *target_bb = lookup_bb(G2_ADDR(target_addr), functions);
assert(target_bb);
target_node = bb_to_node[target_bb];
return;
}
void InterProcCFG::set_target_warning(Warning *w,
map<addr_t, Function *> &functions) {
set_target_addr(w->getAddress(), functions);
warning = w;
for (Warning::slice_iterator sit = w->slice_begin();
sit != w->slice_end(); ++sit) {
BasicBlock *slice_bb = lookup_bb(*sit, functions);
assert(slice_bb);
Cfg *cfg = slice_bb->getCfg();
BasicBlock *leader = cfg->getComponent(slice_bb);
if (leader == cfg->getEntry()) {
//fprintf(stderr, "BB 0x%08x is not in an (intra-proc) loop\n",
// slice_bb->getAddress());
} else {
//fprintf(stderr, "BB 0x%08x is in a loop with head 0x%08x\n",
// slice_bb->getAddress(), leader->getAddress());
interesting_loops.insert(leader->getAddress());
}
}
return;
}
void
InterProcCFG::compute_shortest_paths() {
property_map<IPCFGraph, vertex_distance_t>::type
distance = get(vertex_distance_t(), ipcfg);
dijkstra_shortest_paths(ipcfg, target_node,
distance_map(distance));
}
BasicBlock*
InterProcCFG::lookup_bb(addr_t addr,
map<addr_t, Function *> &functions) {
BasicBlock *the_bb = 0;
tr1::unordered_map<addr_t, BasicBlock*>::const_iterator cache_it =
bb_lookup_cache.find(addr);
if (cache_it != bb_lookup_cache.end())
return (*cache_it).second;
for (map<addr_t, Function *>::iterator it = functions.begin();
it != functions.end(); it++) {
Function *func = (*it).second;
for (Cfg::const_bb_iterator bbit = func->getCfg()->bb_begin();
bbit != func->getCfg()->bb_end(); bbit++) {
BasicBlock *bb = *bbit;
if (addr >= bb->getAddress() &&
addr < bb->getAddress() + bb->getSize()) {
// XXX check and re-enable this assertion.
// assert(the_bb == 0); // should only appear once
the_bb = bb;
}
}
}
bb_lookup_cache[addr] = the_bb;
return the_bb;
}
long long
InterProcCFG::lookup_distance(addr_t source_addr,
map<addr_t, Function *> &functions) {
property_map<IPCFGraph, vertex_distance_t>::type
distance = get(vertex_distance_t(), ipcfg);
BasicBlock *source_bb = lookup_bb(source_addr, functions);
if (source_bb)
return distance[bb_to_node[source_bb]];
else
return -1;
}
int
InterProcCFG::lookup_influence(addr_t addr,
map<addr_t, Function *> &functions,
influence_map_t *influence) {
BasicBlock *bb = lookup_bb(addr, functions);
if (bb)
return (*influence)[bb].size();
else
return 0;
}
long
InterProcCFG::lookup_pth(addr_t addr,
map<addr_t, Function *> &functions) {
property_map<IPCFGraph, paths_to_header_t>::type
pth = get(paths_to_header_t(), ipcfg);
BasicBlock *bb = lookup_bb(addr, functions);
if (bb)
return pth[bb_to_node[bb]];
else
return -1;
}
bool
InterProcCFG::is_loop_exit_cond(addr_t branch_addr,
map<addr_t, Function *> &functions) {
BasicBlock *bb = lookup_bb(branch_addr, functions);
if (!bb)
return false; // Hmm, should we signal this separately?
else
return is_loop_exit_cond(bb);
}
/* A basic block is a loop exit condition if it has at least one
successor that remains within the loop (its target is in the same
SCC, perhaps a sub-SCC for a nested loop) and at least one that
isn't (an exit edge). */
bool
InterProcCFG::is_loop_exit_cond(BasicBlock *bb) {
Cfg *cfg = bb->getCfg();
BasicBlock *leader = cfg->getComponent(bb);
bool seen_inside = false;
bool seen_outside = false;
for (Cfg::const_succ_iterator si = cfg->succ_begin(bb);
si != cfg->succ_end(bb); ++si) {
BasicBlock *succ_bb = *si;
if (cfg->withinComponent(leader, succ_bb))
seen_inside = true;
else
seen_outside = true;
if (seen_inside && seen_outside)
break;
}
return seen_inside && seen_outside;
}
bool
InterProcCFG::edge_is_loop_exit(addr_t from_addr, addr_t to_addr,
map<addr_t, Function *> &functions) {
BasicBlock *from_bb = lookup_bb(from_addr, functions);
BasicBlock *to_bb = lookup_bb(to_addr, functions);
if (!from_bb || !to_bb)
return false;
else
return edge_is_loop_exit(from_bb, to_bb);
}
bool
InterProcCFG::edge_is_loop_exit(BasicBlock *from_bb, BasicBlock *to_bb) {
assert(is_loop_exit_cond(from_bb));
Cfg *cfg = from_bb->getCfg();
assert(to_bb->getCfg() == cfg);
bool seen = false;
for (Cfg::const_succ_iterator si = cfg->succ_begin(from_bb);
si != cfg->succ_end(from_bb); ++si) {
BasicBlock *succ_bb = *si;
if (succ_bb->getAddress() == to_bb->getAddress()) {
seen = true;
break;
}
}
assert(seen);
BasicBlock *leader = cfg->getComponent(from_bb);
return !cfg->withinComponent(leader, to_bb);
}
bool
InterProcCFG::is_interesting_loop(addr_t addr,
map<addr_t, Function *> &functions) {
BasicBlock *bb = lookup_bb(addr, functions);
Cfg *cfg = bb->getCfg();
BasicBlock *leader = cfg->getComponent(bb);
bool is_interesting =
(interesting_loops.find(leader->getAddress()) !=
interesting_loops.end());
return is_interesting;
}
addr_t
InterProcCFG::get_component(addr_t addr,
map<addr_t, Function *> &functions) {
BasicBlock *bb = lookup_bb(addr, functions);
if (!bb)
return 0;
Cfg *cfg = bb->getCfg();
BasicBlock *leader = cfg->getComponent(bb);
return leader->getAddress();
}
bool
InterProcCFG::same_bb(addr_t addr1, addr_t addr2,
map<addr_t, Function *> &functions) {
BasicBlock *bb1 = lookup_bb(addr1, functions);
BasicBlock *bb2 = lookup_bb(addr2, functions);
if (!bb1 || !bb2)
return false;
return bb1 == bb2;
}
InterProcCFG::InterProcCFG(map<addr_t, Function *> &functions)
{
warning = 0;
for (map<addr_t, Function *>::iterator it = functions.begin();
it != functions.end(); it++) {
Function *func = it->second;
const char *func_name = func_unique_name(func);
// Decode the function
debug("[FUNC] %.8x %s@%s\n", func->getAddress(),
func_name, func->getModule());
copy_nodes_to_ipcfg(func);
func->getCfg()->computeWeakTopologicalOrdering();
}
for (map<addr_t, Function *>::iterator it = functions.begin();
it != functions.end(); it++) {
Function *func = it->second;
copy_edges_to_ipcfg(func, true, true, true);
Cfg *cfg = func->getCfg();
for (Cfg::const_bb_iterator bbit = cfg->bb_begin();
bbit != cfg->bb_end(); bbit++) {
BasicBlock *bb = *bbit;
compute_pth(true, bb);
}
}
}
void InterProcCFG::remove_call_edges_from_g1() {
property_map<IPCFGraph, bb_ptr_t>::type bb_ptr = get(bb_ptr_t(), ipcfg);
property_map<IPCFGraph, edge_type_t>::type edge_type
= get(edge_type_t(), ipcfg);
property_map<IPCFGraph, edge_weight_t>::type edge_weight
= get(edge_weight_t(), ipcfg);
list<IPCFGEdge> toremove;
graph_traits<IPCFGraph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(ipcfg); ei != ei_end; ++ei) {
BasicBlock *caller = bb_ptr[target(*ei, ipcfg)];
BasicBlock *callee = bb_ptr[source(*ei, ipcfg)];
if (edge_type[*ei] == CALL_EDGE && IS_G1(caller) && IS_G1(callee)) {
debug2("Removing call edge %.8x --> %.8x\n", caller->getAddress(),
callee->getAddress());
toremove.push_back(*ei);
}
}
while (!toremove.empty()) {
IPCFGEdge e = toremove.front();
toremove.pop_front();
remove_edge(e, ipcfg);
}
}
void InterProcCFG::remove_ret_edges_from_g2() {
property_map<IPCFGraph, bb_ptr_t>::type bb_ptr = get(bb_ptr_t(), ipcfg);
property_map<IPCFGraph, edge_type_t>::type edge_type
= get(edge_type_t(), ipcfg);
property_map<IPCFGraph, edge_weight_t>::type edge_weight
= get(edge_weight_t(), ipcfg);
list<IPCFGEdge> toremove;
graph_traits<IPCFGraph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(ipcfg); ei != ei_end; ++ei) {
BasicBlock *src = bb_ptr[target(*ei, ipcfg)];
BasicBlock *dst = bb_ptr[source(*ei, ipcfg)];
if (edge_type[*ei] == RETURN_EDGE && IS_G2(src) && IS_G2(dst)) {
debug2("Removing return edge %.8x --> %.8x\n", src->getAddress(),
dst->getAddress());
toremove.push_back(*ei);
}
}
while (!toremove.empty()) {
IPCFGEdge e = toremove.front();
toremove.pop_front();
remove_edge(e, ipcfg);
}
}
void InterProcCFG::add_call_ret_edges_to_g1_and_g2(costmap &costs,
map<addr_t, Function *>
&functions) {
property_map<IPCFGraph, bb_ptr_t>::type bb_ptr = get(bb_ptr_t(), ipcfg);
property_map<IPCFGraph, edge_type_t>::type edge_type
= get(edge_type_t(), ipcfg);
property_map<IPCFGraph, edge_weight_t>::type edge_weight
= get(edge_weight_t(), ipcfg);
list<BasicBlock *> toadd;
graph_traits<IPCFGraph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(ipcfg); ei != ei_end; ++ei) {
BasicBlock *src = bb_ptr[target(*ei, ipcfg)];
BasicBlock *dst = bb_ptr[source(*ei, ipcfg)];
// Add an edge between the callsite in g1 and the callsite in g2
if (edge_type[*ei] == CALL_TO_RET_EDGE) {
assert(src->isCall());
debug2("Found call-to-ret edge %.8x --> %.8x\n", src->getAddress(),
dst->getAddress());
functions_t::const_iterator cit;
Function *f = NULL;
for (cit = src->getCfg()->call_targets_begin(*src);
cit != src->getCfg()->call_targets_end(*src); ++cit) {
debug("%.8x\n", *cit ? (*cit)->getAddress() : 0);
assert(!f);
f = *cit;
}
#if 1
IPCFGEdge e = *ei;
bool okay;
if (f) {
// Connect the callsite with the return site
debug2("Creating call-to-ret edge %.8x --> %.8x (%s %lld)\n",
src->getAddress(), dst->getAddress(), f->getName(),
costs[f]);
edge_weight[e] = costs[f];
if (IS_G1(src) && IS_G1(dst)) {
dst = lookup_bb(G2_ADDR(src->getAddress()), functions);
assert(dst);
toadd.push_back(src);
}
} else {
debug("Call without targets @ %.8x!!!!\n", src->getAddress());
}
#endif
}
}
while (!toadd.empty()) {
BasicBlock *src, *dst;
src = toadd.front();
toadd.pop_front();
IPCFGEdge e;
dst = lookup_bb(G2_ADDR(src->getAddress()), functions);
bool okay;
debug2("Creating call-to-call edge %.8x --> %.8x (0)\n",
src->getAddress(), dst->getAddress());
tie(e, okay) = add_edge(bb_to_node[dst], bb_to_node[src],
ipcfg);
assert(okay);
edge_weight[e] = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
IntraProcCFG::IntraProcCFG(Function *f) {
func = f;
cfg = f->getCfg();
exitnode = cfg->addBasicBlock(0);
for (Cfg::const_exit_iterator eit = cfg->exits_begin();
eit != cfg->exits_end(); ++eit) {
cfg->linkBasicBlocks(*eit, exitnode);
}
copy_nodes_to_ipcfg(func);
func->getCfg()->computeWeakTopologicalOrdering();
copy_edges_to_ipcfg(func, false, true, false);
}
IntraProcCFG::~IntraProcCFG() {
for (Cfg::const_exit_iterator eit = cfg->exits_begin();
eit != cfg->exits_end(); ++eit) {
cfg->unlinkBasicBlocks(*eit, exitnode);
}
}
void IntraProcCFG::add_calls_costs_to_edges(costmap &costs) {
property_map<IPCFGraph, bb_ptr_t>::type bb_ptr = get(bb_ptr_t(), ipcfg);
property_map<IPCFGraph, edge_type_t>::type edge_type
= get(edge_type_t(), ipcfg);
property_map<IPCFGraph, edge_weight_t>::type edge_weight
= get(edge_weight_t(), ipcfg);
graph_traits<IPCFGraph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(ipcfg); ei != ei_end; ++ei) {
if (edge_type[*ei] == CALL_TO_RET_EDGE) {
long long mincost = DISTANCE_INFINITY;
BasicBlock *caller = bb_ptr[target(*ei, ipcfg)];
BasicBlock *retaddr = bb_ptr[source(*ei, ipcfg)];
functions_t::const_iterator it;
for (it = cfg->call_targets_begin(*caller);
it != cfg->call_targets_end(*caller); ++it) {
mincost = mincost < costs[*it] ? mincost : costs[*it];
}
debug2("Assigning cost %lld to edge %.8x --> %.8x\n", mincost,
caller->getAddress(), retaddr->getAddress());
edge_weight[*ei] = mincost;
}
}
}
long long IntraProcCFG::compute_shortest_path_to_exit() {
property_map<IPCFGraph, vertex_distance_t>::type
distance = get(vertex_distance_t(), ipcfg);
dijkstra_shortest_paths(ipcfg, bb_to_node[exitnode],
distance_map(distance));
return distance[bb_to_node[cfg->getEntry()]];
}
void compute_functions_costs(CallGraph *cg, costmap &costs) {
vector<Function *> ordering;
computeDFNumbering(cg, ordering);
for (vector<Function *>::const_iterator it = ordering.begin();
it != ordering.end(); ++it) {
debug2("Processing function %.8x %s\n", (*it)->getAddress(),
(*it)->getName());
long long cost;
if (((*it)->getCfg()->exits_begin() != (*it)->getCfg()->exits_end()) &&
strcmp((*it)->getName(), "abort") != 0) {
IntraProcCFG ipcfg(*it);
ipcfg.add_calls_costs_to_edges(costs);
cost = ipcfg.compute_shortest_path_to_exit();
} else {
cost = DISTANCE_INFINITY;
}
debug2("Cost is %lld\n\n", cost);
costs[*it] = cost;
}
}
Prog prog, prog_bis;
map <addr_t, Function *> functions, functions_bis;
InterProcCFG *build_ipcfg(const char *fname, map<addr_t, Function *> &F) {
CallGraph *callgraph, *callgraph_bis;
InterProcCFG *ipcfg = NULL;
costmap costs;
unserialize(fname, prog, callgraph, functions);
compute_functions_costs(callgraph, costs);
unserialize(fname, prog_bis, callgraph_bis, functions_bis);
// Add a copy of the functions
for (map<addr_t, Function *>::const_iterator fit = functions_bis.begin();
fit != functions_bis.end(); ++fit) {
Function *func = fit->second;
string funcname = func->getName() + string("_bis");
func->setAddress(G2_ADDR(func->getAddress()));
func->setName(funcname.c_str());
for (Cfg::const_bb_iterator bbit = func->getCfg()->bb_begin();
bbit != func->getCfg()->bb_end(); ++bbit) {
(*bbit)->setAddress(G2_ADDR((*bbit)->getAddress()));
for (instructions_t::const_iterator iit = (*bbit)->inst_begin();
iit != (*bbit)->inst_end(); ++iit) {
(*iit)->setAddress(G2_ADDR((*iit)->getAddress()));
}
}
functions[func->getAddress()] = func;
}
for (map<addr_t, Function *>::const_iterator fit = functions.begin();
fit != functions.end(); ++fit) {
F[fit->first] = fit->second;
}
ipcfg = new InterProcCFG(functions);
ipcfg->remove_call_edges_from_g1();
ipcfg->remove_ret_edges_from_g2();
ipcfg->add_call_ret_edges_to_g1_and_g2(costs, functions);
return ipcfg;
}
// Local Variables:
// mode: c++
// c-basic-offset: 4
// compile-command: "dchroot -c typeinfer -d make"
// End: