d322170e918103e3ce832fad396c510bcc3629c1
[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  */
26 #include "config.h"
27
28 #include "irgraph_t.h"
29 #include "iredges.h"
30 #include "error.h"
31 #include "ircons.h"
32 #include "instrument.h"
33
34 void instrument_initcall(ir_graph *irg, ir_entity *ent)
35 {
36         ir_node        *initial_exec;
37         ir_node        *start_block;
38         ir_node        *first_block = NULL;
39         int             i, idx, need_new_block;
40         symconst_symbol sym;
41
42         assure_edges(irg);
43
44         /* find the first block */
45         initial_exec = get_irg_initial_exec(irg);
46         start_block  = get_irg_start_block(irg);
47
48         foreach_out_edge(initial_exec, edge) {
49                 ir_node *succ = get_edge_src_irn(edge);
50
51                 if (succ != start_block && is_Block(succ)) {
52                         /* found the first block */
53                         first_block = succ;
54                         break;
55                 }
56         }
57         if (first_block == NULL) {
58                 panic("Cannot find first block of irg %+F", irg);
59         }
60
61         /* check if this block has only one predecessor */
62         idx = -1;
63         need_new_block = 0;
64         for (i = get_Block_n_cfgpreds(first_block) - 1; i >= 0; --i) {
65                 ir_node *p = get_Block_cfgpred(first_block, i);
66
67                 if (is_Bad(p))
68                         continue;
69                 if (p == initial_exec)
70                         idx = i;
71                 else
72                         need_new_block = 1;
73         }
74
75         if (need_new_block) {
76                 ir_node *blk = new_r_Block(irg, 1, &initial_exec);
77                 set_Block_cfgpred(first_block, idx, new_r_Jmp(blk));
78                 first_block = blk;
79         }
80
81         /* place the call */
82         sym.entity_p = ent;
83         ir_node *const adr         = new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
84         ir_node *const initial_mem = get_irg_initial_mem(irg);
85         ir_node *const call        = new_r_Call(first_block, initial_mem, adr, 0, NULL, get_entity_type(ent));
86         ir_node *const new_mem     = new_r_Proj(call, mode_M, pn_Call_M);
87
88         edges_reroute_except(initial_mem, new_mem, call);
89         /* beware: reroute routes anchor edges also, revert this */
90         set_irg_initial_mem(irg, initial_mem);
91 }