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