backend: created a (not so nice) macro to iterate over all values defined by an instr...
[libfirm] / ir / be / beutil.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       Contains some useful function for the backend.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #include "config.h"
27
28 #include <stdio.h>
29
30 #include "pset.h"
31
32 #include "irgraph.h"
33 #include "irgwalk.h"
34 #include "irdump_t.h"
35 #include "irdom_t.h"
36 #include "ircons.h"
37 #include "iropt.h"
38 #include "irgopt.h"
39 #include "irtools.h"
40 #include "irprintf.h"
41 #include "iredges_t.h"
42
43 #include "beutil.h"
44 #include "besched.h"
45 #include "bearch.h"
46
47 void be_clear_links(ir_graph *irg)
48 {
49         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
50 }
51
52 static void count_num_reachable_nodes(ir_node *irn, void *env)
53 {
54         int *num = env;
55         (*num)++;
56         (void) irn;
57 }
58
59 unsigned get_num_reachable_nodes(ir_graph *irg)
60 {
61         int num = 0;
62         irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
63         return num;
64 }
65
66 /**
67  * Gets the Proj with number pn from irn.
68  */
69 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn)
70 {
71         const ir_edge_t *edge;
72         ir_node         *proj;
73         assert(get_irn_mode(irn) == mode_T && "need mode_T");
74
75         foreach_out_edge(irn, edge) {
76                 proj = get_edge_src_irn(edge);
77
78                 if (is_Proj(proj) && get_Proj_proj(proj) == pn)
79                         return proj;
80         }
81
82         return NULL;
83 }
84
85 static void add_to_postorder(ir_node *block, void *data)
86 {
87         ir_node ***list = (ir_node***) data;
88         ARR_APP1(ir_node*, *list, block);
89 }
90
91 ir_node **be_get_cfgpostorder(ir_graph *irg)
92 {
93         ir_node **list      = NEW_ARR_F(ir_node*, 0);
94         ir_node  *end_block = get_irg_end_block(irg);
95
96         /* end block may be unreachable in case of endless loops */
97         if (get_Block_n_cfgpreds(end_block) == 0)
98                 ARR_APP1(ir_node*, list, end_block);
99
100         /* walk blocks */
101         irg_block_edges_walk(get_irg_start_block(irg), NULL, add_to_postorder,
102                              &list);
103
104         return list;
105 }
106
107 ir_node *get_first_block_succ(const ir_node *block)
108 {
109         const ir_edge_t *edge = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
110         assert(edge != NULL);
111         return get_edge_src_irn(edge);
112 }