Initial commit of morgans spilling algorithm (spill unused values that live through
[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 /**
14  * Get an empty set.
15  * This function always returns the same set.
16  */
17 pset *be_empty_set(void);
18
19
20 /** Undefine this to disable debugging mode. */
21 #define BE_DEBUG 1
22
23 /**
24  * Convenient block getter.
25  * Works also, if the given node is a block.
26  * @param  irn The node.
27  * @return The block of the node, or the node itself, if the node is a
28  *         block.
29  */
30 static INLINE const ir_node *get_block(const ir_node *irn)
31 {
32         return is_Block(irn) ? irn : get_nodes_block(irn);
33 }
34
35 static INLINE int is_firm_be_mode(const ir_mode *mode)
36 {
37         return mode_is_data(mode);
38 }
39
40 /**
41  * Check, if a node produces or consumes a data value.
42  * If it does, it is significant for scheduling and register allocation.
43  * A node produces/consumes a data value, if one of its operands is of
44  * mode datab, or his retuning mode is of mode datab.
45  * @param irn The node to check for.
46  * @return 1, if the node is a data node, 0 if not.
47  */
48 static INLINE int is_data_node(const ir_node *irn)
49 {
50         int i, n;
51
52         /* If the node produces a data value, return immediately. */
53         if(is_firm_be_mode(get_irn_mode(irn)))
54                 return 1;
55
56         /* else check, if it takes a data value, if that is so, return */
57         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
58                 ir_node *op = get_irn_n(irn, i);
59                 if(is_firm_be_mode(get_irn_mode(op)))
60                         return 1;
61         }
62
63         /* Else the node does not produce/consume a data value */
64         return 0;
65 }
66
67 /**
68  * Make each constant local to its use.
69  * This duplicates all constants in order to simulate a realistic
70  * register pressure.
71  * @param irg The graph.
72  */
73 void localize_consts(ir_graph *irg);
74
75 /**
76  * Dump a vcg graph containing the controlflow graph, the schedule and
77  * allocated registers.
78  * @param irg The irg. Note that scheduling, register allocation must
79  * have been performed.
80  */
81 void dump_allocated_irg(arch_env_t *env, ir_graph *irg, char *suffix);
82
83 void be_clear_links(ir_graph *irg);
84
85 static INLINE FILE *ffopen(const char *base, const char *ext, const char *mode) {
86         FILE *out;
87         char buf[1024];
88
89         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
90         if (! (out = fopen(buf, mode))) {
91                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
92                 return NULL;
93         }
94         return out;
95 }
96
97 /**
98  * Dump a graph with schedule edges.
99  * @param irg The graph.
100  * @param suffix A suffix to its file name.
101  */
102 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix);
103
104 /**
105  * Dump a extended block graph with schedule edges.
106  * @param irg The graph.
107  * @param suffix A suffix to its file name.
108  */
109 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix);
110
111 /**
112  * Dumps a graph and numbers all dumps.
113  * @param irg    The graph
114  * @param suffix A suffix to its file name.
115  * @param dumper The dump function
116  */
117 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *));
118
119 /**
120  * Search for an irn in @p accept.
121  * The search is started at @p start_point_exclusive and continues upwards the dom-tree
122  * @return The first node out of accept if found. Else NULL is returned.
123  */
124 ir_node *dom_up_search(pset *accept, ir_node *start_point_exclusive);
125
126 #endif /* _BEUTIL_H */