irdom: move some functions to private API
[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  */
25 #include "config.h"
26
27 #include <stdio.h>
28
29 #include "pset.h"
30
31 #include "irgraph.h"
32 #include "irgwalk.h"
33 #include "irdump_t.h"
34 #include "irdom_t.h"
35 #include "ircons.h"
36 #include "iropt.h"
37 #include "irgopt.h"
38 #include "irtools.h"
39 #include "irprintf.h"
40 #include "iredges_t.h"
41
42 #include "beutil.h"
43 #include "besched.h"
44 #include "bearch.h"
45
46 void be_clear_links(ir_graph *irg)
47 {
48         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
49 }
50
51 /**
52  * Gets the Proj with number pn from irn.
53  */
54 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn)
55 {
56         const ir_edge_t *edge;
57         ir_node         *proj;
58         assert(get_irn_mode(irn) == mode_T && "need mode_T");
59
60         foreach_out_edge(irn, edge) {
61                 proj = get_edge_src_irn(edge);
62
63                 if (is_Proj(proj) && get_Proj_proj(proj) == pn)
64                         return proj;
65         }
66
67         return NULL;
68 }
69
70 /**
71  * Block-walker: adds the visited block to a flexible array.
72  */
73 static void add_to_postorder(ir_node *block, void *data)
74 {
75         ir_node ***list = (ir_node***) data;
76         ARR_APP1(ir_node*, *list, block);
77 }
78
79 ir_node **be_get_cfgpostorder(ir_graph *irg)
80 {
81         ir_node **list      = NEW_ARR_F(ir_node*, 0);
82         ir_node  *end_block = get_irg_end_block(irg);
83
84         /* end block may be unreachable in case of endless loops */
85         if (get_Block_n_cfgpreds(end_block) == 0)
86                 ARR_APP1(ir_node*, list, end_block);
87
88         /* walk blocks */
89         irg_block_edges_walk(get_irg_start_block(irg), NULL, add_to_postorder,
90                              &list);
91
92         return list;
93 }
94
95 ir_node *get_first_block_succ(const ir_node *block)
96 {
97         const ir_edge_t *edge = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
98         assert(edge != NULL);
99         return get_edge_src_irn(edge);
100 }