cleanup iredges API a bit
[libfirm] / ir / ir / instrument.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Instrumentation of graphs.
23  * @date    14.4.2008
24  * @author  Michael Beck
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "irgraph_t.h"
30 #include "iredges.h"
31 #include "error.h"
32 #include "ircons.h"
33 #include "instrument.h"
34
35 /**
36  * Adds a Call at the beginning of the given irg.
37  */
38 void instrument_initcall(ir_graph *irg, ir_entity *ent)
39 {
40         const ir_edge_t *edge;
41         ir_node         *initial_exec;
42         ir_node         *initial_mem;
43         ir_node         *start_block;
44         ir_node         *adr, *call, *new_mem;
45         ir_node         *first_block = NULL;
46         int             i, idx, need_new_block;
47         symconst_symbol sym;
48
49         edges_assure(irg);
50
51         /* find the first block */
52         initial_exec = get_irg_initial_exec(irg);
53         start_block  = get_irg_start_block(irg);
54
55         foreach_out_edge(initial_exec, edge) {
56                 ir_node *succ = get_edge_src_irn(edge);
57
58                 if (succ != start_block && is_Block(succ)) {
59                         /* found the first block */
60                         first_block = succ;
61                         break;
62                 }
63         }
64         if (first_block == NULL) {
65                 panic("Cannot find first block of irg %+F", irg);
66         }
67
68         /* check if this block has only one predecessor */
69         idx = -1;
70         need_new_block = 0;
71         for (i = get_Block_n_cfgpreds(first_block) - 1; i >= 0; --i) {
72                 ir_node *p = get_Block_cfgpred(first_block, i);
73
74                 if (is_Bad(p))
75                         continue;
76                 if (p == initial_exec)
77                         idx = i;
78                 else
79                         need_new_block = 1;
80         }
81
82         if (need_new_block) {
83                 ir_node *blk = new_r_Block(irg, 1, &initial_exec);
84                 set_Block_cfgpred(first_block, idx, new_r_Jmp(blk));
85                 first_block = blk;
86         }
87
88         /* place the call */
89         sym.entity_p = ent;
90         adr = new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
91
92         call    = new_r_Call(first_block, get_irg_no_mem(irg), adr, 0, NULL, get_entity_type(ent));
93         new_mem = new_r_Proj(call, mode_M, pn_Call_M);
94
95         initial_mem = get_irg_initial_mem(irg);
96         edges_reroute(initial_mem, new_mem);
97         /* beware: reroute routes anchor edges also, revert this */
98         set_irg_initial_mem(irg, initial_mem);
99         set_Call_mem(call, initial_mem);
100 }