beifg: Factorise code to count interference components.
[libfirm] / ir / ir / irgopt.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief    Optimizations for a whole ir graph, i.e., a procedure.
9  * @author   Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
10  *           Michael Beck
11  */
12 #include "config.h"
13
14 #include <assert.h>
15
16 #include "irnode_t.h"
17 #include "irgraph_t.h"
18
19 #include "iroptimize.h"
20 #include "iropt_t.h"
21 #include "irgopt.h"
22 #include "irgmod.h"
23 #include "irgwalk.h"
24 #include "ircons.h"
25
26 #include "adt/pdeq.h"
27
28 #include "irpass_t.h"
29 #include "irflag_t.h"
30 #include "iredges_t.h"
31 #include "irtools.h"
32
33 /**
34  * A wrapper around optimize_inplace_2() to be called from a walker.
35  */
36 static void optimize_in_place_wrapper(ir_node *n, void *env)
37 {
38         ir_node *optimized = optimize_in_place_2(n);
39         (void) env;
40
41         if (optimized != n) {
42                 exchange(n, optimized);
43         }
44 }
45
46 /**
47  * Do local optimizations for a node.
48  *
49  * @param n  the IR-node where to start. Typically the End node
50  *           of a graph
51  *
52  * @note current_ir_graph must be set
53  */
54 static inline void do_local_optimize(ir_node *n)
55 {
56         ir_graph *irg = get_irn_irg(n);
57
58         if (get_opt_global_cse())
59                 set_irg_pinned(irg, op_pin_state_floats);
60         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
61
62         /* Clean the value_table in irg for the CSE. */
63         new_identities(irg);
64
65         /* walk over the graph */
66         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
67 }
68
69 void local_optimize_node(ir_node *n)
70 {
71         ir_graph *rem = current_ir_graph;
72         current_ir_graph = get_irn_irg(n);
73
74         do_local_optimize(n);
75
76         current_ir_graph = rem;
77 }
78
79 static void enqueue_node(ir_node *node, pdeq *waitq)
80 {
81         if (get_irn_link(node) == waitq)
82                 return;
83         pdeq_putr(waitq, node);
84         set_irn_link(node, waitq);
85 }
86
87 /**
88  * Enqueue all users of a node to a wait queue.
89  * Handles mode_T nodes.
90  */
91 static void enqueue_users(ir_node *n, pdeq *waitq)
92 {
93         foreach_out_edge(n, edge) {
94                 ir_node *succ  = get_edge_src_irn(edge);
95
96                 enqueue_node(succ, waitq);
97
98                 /* Also enqueue Phis to prevent inconsistencies. */
99                 if (is_Block(succ)) {
100                         foreach_out_edge(succ, edge2) {
101                                 ir_node *succ2 = get_edge_src_irn(edge2);
102
103                                 if (is_Phi(succ2)) {
104                                         enqueue_node(succ2, waitq);
105                                 }
106                         }
107                 } else if (get_irn_mode(succ) == mode_T) {
108                 /* A mode_T node has Proj's. Because most optimizations
109                         run on the Proj's we have to enqueue them also. */
110                         enqueue_users(succ, waitq);
111                 }
112         }
113 }
114
115 /**
116  * Block-Walker: uses dominance depth to mark dead blocks.
117  */
118 static void find_unreachable_blocks(ir_node *block, void *env)
119 {
120         pdeq *waitq = (pdeq*) env;
121
122         if (get_Block_dom_depth(block) < 0) {
123                 ir_graph *irg = get_irn_irg(block);
124                 ir_node  *end = get_irg_end(irg);
125
126                 foreach_block_succ(block, edge) {
127                         ir_node *succ_block = get_edge_src_irn(edge);
128                         enqueue_node(succ_block, waitq);
129                         foreach_out_edge(succ_block, edge2) {
130                                 ir_node *succ = get_edge_src_irn(edge2);
131                                 if (is_Phi(succ))
132                                         enqueue_node(succ, waitq);
133                         }
134                 }
135                 enqueue_node(end, waitq);
136         }
137 }
138
139 void local_optimize_graph(ir_graph *irg)
140 {
141         ir_graph *rem = current_ir_graph;
142         current_ir_graph = irg;
143
144         do_local_optimize(get_irg_end(irg));
145
146         current_ir_graph = rem;
147 }
148
149 /**
150  * Data flow optimization walker.
151  * Optimizes all nodes and enqueue its users
152  * if done.
153  */
154 static void opt_walker(ir_node *n, void *env)
155 {
156         pdeq *waitq = (pdeq*)env;
157         ir_node *optimized;
158
159         optimized = optimize_in_place_2(n);
160         set_irn_link(optimized, NULL);
161
162         if (optimized != n) {
163                 enqueue_users(n, waitq);
164                 exchange(n, optimized);
165         }
166 }
167
168 int optimize_graph_df(ir_graph *irg)
169 {
170         pdeq     *waitq = new_pdeq();
171         ir_graph *rem = current_ir_graph;
172         ir_node  *end;
173
174         current_ir_graph = irg;
175
176         if (get_opt_global_cse())
177                 set_irg_pinned(irg, op_pin_state_floats);
178
179         /* enable unreachable code elimination */
180         assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE));
181         add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
182
183         new_identities(irg);
184         assure_edges(irg);
185         assure_doms(irg);
186
187
188         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
189         irg_walk_graph(irg, NULL, opt_walker, waitq);
190
191         /* any optimized nodes are stored in the wait queue,
192          * so if it's not empty, the graph has been changed */
193         while (!pdeq_empty(waitq)) {
194                 /* finish the wait queue */
195                 while (! pdeq_empty(waitq)) {
196                         ir_node *n = (ir_node*)pdeq_getl(waitq);
197                         opt_walker(n, waitq);
198                 }
199                 /* Calculate dominance so we can kill unreachable code
200                  * We want this intertwined with localopts for better optimization (phase coupling) */
201                 compute_doms(irg);
202                 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
203         }
204         del_pdeq(waitq);
205         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
206
207         /* disable unreachable code elimination */
208         clear_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
209         add_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
210
211         /* invalidate infos */
212         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
213         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
214         edges_deactivate(irg);
215
216         /* Finally kill BAD and doublets from the keep alives.
217          * Doing this AFTER edges where deactivated saves cycles */
218         end = get_irg_end(irg);
219         remove_End_Bads_and_doublets(end);
220
221         current_ir_graph = rem;
222
223         /* Note we do not have a reliable way to detect changes, since some
224          * localopt rules change the inputs of a node and do not return a new
225          * node, so we conservatively say true here */
226         return true;
227 }
228
229 void local_opts_const_code(void)
230 {
231         ir_graph *irg = get_const_code_irg();
232         /* Clean the value_table in irg for the CSE. */
233         new_identities(irg);
234
235         walk_const_code(firm_clear_link, optimize_in_place_wrapper, NULL);
236 }
237
238 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
239 {
240         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
241 }