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