beifg: Factorise code to count interference components.
[libfirm] / ir / be / beutil.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Contains some useful function for the backend.
9  * @author      Sebastian Hack
10  */
11 #include "config.h"
12
13 #include <stdio.h>
14
15 #include "pset.h"
16
17 #include "irgraph.h"
18 #include "irgwalk.h"
19 #include "irdump_t.h"
20 #include "irdom_t.h"
21 #include "ircons.h"
22 #include "iropt.h"
23 #include "irgopt.h"
24 #include "irtools.h"
25 #include "irprintf.h"
26 #include "iredges_t.h"
27
28 #include "beutil.h"
29 #include "besched.h"
30 #include "bearch.h"
31
32 void be_clear_links(ir_graph *irg)
33 {
34         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
35 }
36
37 /**
38  * Gets the Proj with number pn from irn.
39  */
40 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn)
41 {
42         ir_node *proj;
43         assert(get_irn_mode(irn) == mode_T && "need mode_T");
44
45         foreach_out_edge(irn, edge) {
46                 proj = get_edge_src_irn(edge);
47
48                 if (is_Proj(proj) && get_Proj_proj(proj) == pn)
49                         return proj;
50         }
51
52         return NULL;
53 }
54
55 /**
56  * Block-walker: adds the visited block to a flexible array.
57  */
58 static void add_to_postorder(ir_node *block, void *data)
59 {
60         ir_node ***list = (ir_node***) data;
61         ARR_APP1(ir_node*, *list, block);
62 }
63
64 ir_node **be_get_cfgpostorder(ir_graph *irg)
65 {
66         ir_node **list      = NEW_ARR_F(ir_node*, 0);
67         ir_node  *end_block = get_irg_end_block(irg);
68
69         /* end block may be unreachable in case of endless loops */
70         if (get_Block_n_cfgpreds(end_block) == 0)
71                 ARR_APP1(ir_node*, list, end_block);
72
73         /* walk blocks */
74         irg_block_edges_walk(get_irg_start_block(irg), NULL, add_to_postorder,
75                              &list);
76
77         return list;
78 }
79
80 ir_node *get_first_block_succ(const ir_node *block)
81 {
82         const ir_edge_t *edge = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
83         assert(edge != NULL);
84         return get_edge_src_irn(edge);
85 }