sparc: preliminary SwitchJmp implementation
[libfirm] / ir / be / beutil.h
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 #ifndef FIRM_BE_BEUTIL_H
27 #define FIRM_BE_BEUTIL_H
28
29 #include <stdio.h>
30
31 #include "firm_types.h"
32 #include "pset.h"
33
34 #include "bearch.h"
35
36 /* iterate over a list of ir_nodes linked by link field */
37 #define foreach_linked_irns(head, iter) for ((iter) = (head); (iter); (iter) = get_irn_link((iter)))
38
39 /**
40  * Convenient block getter.
41  * Works also, if the given node is a block.
42  * @param  irn The node.
43  * @return The block of the node, or the node itself, if the node is a
44  *         block.
45  */
46 static inline ir_node *get_block(ir_node *irn)
47 {
48         return is_Block(irn) ? irn : get_nodes_block(irn);
49 }
50
51 static inline const ir_node *get_block_const(const ir_node *irn)
52 {
53         return is_Block(irn) ? irn : get_nodes_block(irn);
54 }
55
56 /**
57  * Check, if a node produces or consumes a data value.
58  * If it does, it is significant for scheduling and register allocation.
59  * A node produces/consumes a data value, if one of its operands is of
60  * mode datab, or his retuning mode is of mode datab.
61  * @param irn The node to check for.
62  * @return 1, if the node is a data node, 0 if not.
63  */
64 static inline int is_data_node(const ir_node *irn)
65 {
66         int i, n;
67
68         /* If the node produces a data value, return immediately. */
69         if (mode_is_data(get_irn_mode(irn)))
70                 return 1;
71
72         /* else check, if it takes a data value, if that is so, return */
73         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
74                 ir_node *op = get_irn_n(irn, i);
75                 if (mode_is_data(get_irn_mode(op)))
76                         return 1;
77         }
78
79         /* Else the node does not produce/consume a data value */
80         return 0;
81 }
82
83 /**
84  * Clears the link fields of all nodes of the given graph.
85  * @param irg The graph.
86  */
87 void be_clear_links(ir_graph *irg);
88
89 /**
90  * Returns the number of reachable nodes in an irg.
91  * @param irg The irg.
92  * @return The number of reachable nodes.
93  */
94 unsigned get_num_reachable_nodes(ir_graph *irg);
95
96 /**
97  * Gets the Proj with number pn from irn.
98  */
99 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn);
100
101 /**
102  * Returns an array (an ARR_F) of the programs blocks in reverse postorder
103  * (note: caller has to free the memory with DEL_ARR_F after use;
104  *  of course you can use ARR_LEN on the array too.)
105  */
106 ir_node **be_get_cfgpostorder(ir_graph *irg);
107
108 /**
109  * convenience function to return the first successor block
110  * (it is often known that there is exactly 1 successor anyway)
111  */
112 ir_node *get_first_block_succ(const ir_node *block);
113
114 #endif