a0248f4be3889e2a7982e1ad09356404993d0b3f
[libfirm] / ir / be / beutil.h
1 /*
2  * Copyright (C) 1995-2007 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 "firm_config.h"
30
31 #include <stdio.h>
32
33 #include "pset.h"
34 #include "irgraph.h"
35 #include "irnode.h"
36 #include "irmode.h"
37
38 #include "bearch_t.h"
39
40 /* iterate over a list of ir_nodes linked by link field */
41 #define foreach_linked_irns(head, iter) for ((iter) = (head); (iter); (iter) = get_irn_link((iter)))
42
43 /**
44  * Get an empty set.
45  * This function always returns the same set.
46  */
47 pset *be_empty_set(void);
48
49 /** Undefine this to disable debugging mode. */
50 #define BE_DEBUG 1
51
52 /**
53  * Convenient block getter.
54  * Works also, if the given node is a block.
55  * @param  irn The node.
56  * @return The block of the node, or the node itself, if the node is a
57  *         block.
58  */
59 static INLINE const ir_node *get_block(const ir_node *irn)
60 {
61         return is_Block(irn) ? irn : get_nodes_block(irn);
62 }
63
64 static INLINE int is_firm_be_mode(const ir_mode *mode)
65 {
66         return mode_is_data(mode);
67 }
68
69 /**
70  * Check, if a node produces or consumes a data value.
71  * If it does, it is significant for scheduling and register allocation.
72  * A node produces/consumes a data value, if one of its operands is of
73  * mode datab, or his retuning mode is of mode datab.
74  * @param irn The node to check for.
75  * @return 1, if the node is a data node, 0 if not.
76  */
77 static INLINE int is_data_node(const ir_node *irn)
78 {
79         int i, n;
80
81         /* If the node produces a data value, return immediately. */
82         if (is_firm_be_mode(get_irn_mode(irn)))
83                 return 1;
84
85         /* else check, if it takes a data value, if that is so, return */
86         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
87                 ir_node *op = get_irn_n(irn, i);
88                 if (is_firm_be_mode(get_irn_mode(op)))
89                         return 1;
90         }
91
92         /* Else the node does not produce/consume a data value */
93         return 0;
94 }
95
96 /**
97  * Dump a vcg graph containing the controlflow graph, the schedule and
98  * allocated registers.
99  * @param irg The irg. Note that scheduling, register allocation must
100  * have been performed.
101  */
102 void dump_allocated_irg(arch_env_t *env, ir_graph *irg, char *suffix);
103
104 void be_clear_links(ir_graph *irg);
105
106 static INLINE FILE *ffopen(const char *base, const char *ext, const char *mode) {
107         FILE *out;
108         char buf[1024];
109
110         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
111         buf[sizeof(buf) - 1] = '\0';
112         if (! (out = fopen(buf, mode))) {
113                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
114                 return NULL;
115         }
116         return out;
117 }
118
119 /**
120  * Dump a graph with schedule edges.
121  * @param irg The graph.
122  * @param suffix A suffix to its file name.
123  */
124 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix);
125
126 /**
127  * Dump a extended block graph with schedule edges.
128  * @param irg The graph.
129  * @param suffix A suffix to its file name.
130  */
131 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix);
132
133 /**
134  * Dumps a graph and numbers all dumps.
135  * @param irg    The graph
136  * @param suffix A suffix to its file name.
137  * @param dumper The dump function
138  */
139 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *));
140
141 /**
142  * Returns the number of reachable nodes in an irg.
143  * @param irg The irg.
144  * @return The number of reachable nodes.
145  */
146 unsigned get_num_reachable_nodes(ir_graph *irg);
147
148 /**
149  * Sets all node inputs to BAD node.
150  * @param irn  The node to be killed.
151  */
152 void be_kill_node(ir_node *irn);
153
154 /**
155  * Search for an irn in @p accept.
156  * The search is started at @p start_point_exclusive and continues upwards the dom-tree
157  * @return The first node out of accept if found. Else NULL is returned.
158  */
159 ir_node *dom_up_search(pset *accept, ir_node *start_point_exclusive);
160
161 /**
162  * Gets the Proj with number pn from irn.
163  */
164 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn);
165
166 #endif /* FIRM_BE_BEUTIL_H */