irgopt.c 6.6 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Matthias Braun's avatar
Matthias Braun committed
20
21
22
23
24
25
/**
 * @file
 * @brief    Optimizations for a whole ir graph, i.e., a procedure.
 * @author   Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
 *           Michael Beck
 * @version  $Id$
Götz Lindenmaier's avatar
Götz Lindenmaier committed
26
 */
Matthias Braun's avatar
Matthias Braun committed
27
#include "config.h"
Boris Boesler's avatar
added    
Boris Boesler committed
28

29
30
31
32
33
#include <assert.h>

#include "irnode_t.h"
#include "irgraph_t.h"

34
#include "iroptimize.h"
35
36
37
38
39
#include "iropt_t.h"
#include "irgopt.h"
#include "irgmod.h"
#include "irgwalk.h"

Michael Beck's avatar
Michael Beck committed
40
#include "adt/pdeq.h"
41

42
#include "irpass_t.h"
43
#include "irflag_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
44
#include "iredges_t.h"
45
#include "irtools.h"
46

Michael Beck's avatar
Michael Beck committed
47
/*------------------------------------------------------------------*/
Götz Lindenmaier's avatar
Götz Lindenmaier committed
48
/* apply optimizations of iropt to all nodes.                       */
Michael Beck's avatar
Michael Beck committed
49
/*------------------------------------------------------------------*/
Götz Lindenmaier's avatar
Götz Lindenmaier committed
50

51
52
53
/**
 * A wrapper around optimize_inplace_2() to be called from a walker.
 */
54
55
static void optimize_in_place_wrapper(ir_node *n, void *env)
{
Michael Beck's avatar
Michael Beck committed
56
	ir_node *optimized = optimize_in_place_2(n);
57
58
59
	(void) env;

	if (optimized != n) {
60
		exchange(n, optimized);
61
	}
62
63
}

Michael Beck's avatar
Michael Beck committed
64
65
66
67
68
69
70
71
/**
 * Do local optimizations for a node.
 *
 * @param n  the IR-node where to start. Typically the End node
 *           of a graph
 *
 * @note current_ir_graph must be set
 */
72
73
static inline void do_local_optimize(ir_node *n)
{
74
75
	ir_graph *irg = get_irn_irg(n);

Michael Beck's avatar
Michael Beck committed
76
	/* Handle graph state */
77
	assert(get_irg_phase_state(irg) != phase_building);
78

Michael Beck's avatar
Michael Beck committed
79
	if (get_opt_global_cse())
80
		set_irg_pinned(irg, op_pin_state_floats);
81
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
82

Michael Beck's avatar
Michael Beck committed
83
	/* Clean the value_table in irg for the CSE. */
84
	new_identities(irg);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
85

Michael Beck's avatar
Michael Beck committed
86
87
	/* walk over the graph */
	irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
88
89
}

Michael Beck's avatar
Michael Beck committed
90
/* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
91
92
void local_optimize_node(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
93
94
	ir_graph *rem = current_ir_graph;
	current_ir_graph = get_irn_irg(n);
95

Michael Beck's avatar
Michael Beck committed
96
	do_local_optimize(n);
97

Michael Beck's avatar
Michael Beck committed
98
	current_ir_graph = rem;
99
100
}

101
102
103
104
105
106
107
108
static void enqueue_node(ir_node *node, pdeq *waitq)
{
	if (get_irn_link(node) == waitq)
		return;
	pdeq_putr(waitq, node);
	set_irn_link(node, waitq);
}

109
110
111
112
113
114
115
116
117
/**
 * Enqueue all users of a node to a wait queue.
 * Handles mode_T nodes.
 */
static void enqueue_users(ir_node *n, pdeq *waitq)
{
	const ir_edge_t *edge;

	foreach_out_edge(n, edge) {
yb9976's avatar
yb9976 committed
118
119
		ir_node         *succ  = get_edge_src_irn(edge);
		const ir_edge_t *edge2;
120

121
		enqueue_node(succ, waitq);
yb9976's avatar
yb9976 committed
122
123
124
125
126
127
128
129
130
131
132

		/* Also enqueue Phis to prevent inconsistencies. */
		if (is_Block(succ)) {
			foreach_out_edge(succ, edge2) {
				ir_node *succ2 = get_edge_src_irn(edge2);

				if (is_Phi(succ2)) {
					enqueue_node(succ2, waitq);
				}
			}
		} else if (get_irn_mode(succ) == mode_T) {
133
134
135
136
137
138
139
		/* A mode_T node has Proj's. Because most optimizations
			run on the Proj's we have to enqueue them also. */
			enqueue_users(succ, waitq);
		}
	}
}

140
141
142
/**
 * Block-Walker: uses dominance depth to mark dead blocks.
 */
143
static void find_unreachable_blocks(ir_node *block, void *env)
144
{
145
	pdeq *waitq = (pdeq*) env;
146

Michael Beck's avatar
Michael Beck committed
147
	if (get_Block_dom_depth(block) < 0) {
148
		ir_graph *irg = get_irn_irg(block);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
		ir_node  *end = get_irg_end(irg);

		const ir_edge_t *edge;
		foreach_block_succ(block, edge) {
			const ir_edge_t *edge2;
			ir_node *succ_block = get_edge_src_irn(edge);
			enqueue_node(succ_block, waitq);
			foreach_out_edge(succ_block, edge2) {
				ir_node *succ = get_edge_src_irn(edge2);
				if (is_Phi(succ))
					enqueue_node(succ, waitq);
			}
		}
		enqueue_node(end, waitq);
Michael Beck's avatar
Michael Beck committed
163
	}
164
165
}

Michael Beck's avatar
Michael Beck committed
166
/* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
167
168
void local_optimize_graph(ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
169
170
	ir_graph *rem = current_ir_graph;
	current_ir_graph = irg;
171

Michael Beck's avatar
Michael Beck committed
172
	do_local_optimize(get_irg_end(irg));
Christian Schäfer's avatar
Christian Schäfer committed
173

Michael Beck's avatar
Michael Beck committed
174
	current_ir_graph = rem;
Christian Schäfer's avatar
Christian Schäfer committed
175
176
}

177
178
/**
 * Data flow optimization walker.
179
 * Optimizes all nodes and enqueue its users
Michael Beck's avatar
Michael Beck committed
180
 * if done.
181
 */
182
183
static void opt_walker(ir_node *n, void *env)
{
184
	pdeq *waitq = (pdeq*)env;
Michael Beck's avatar
Michael Beck committed
185
	ir_node *optimized;
186

187
188
189
190
191
192
	optimized = optimize_in_place_2(n);
	set_irn_link(optimized, NULL);

	if (optimized != n) {
		enqueue_users(n, waitq);
		exchange(n, optimized);
Michael Beck's avatar
Michael Beck committed
193
	}
194
195
}

196
197
int optimize_graph_df(ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
198
199
	pdeq     *waitq = new_pdeq();
	ir_graph *rem = current_ir_graph;
200
	ir_node  *end;
Michael Beck's avatar
Michael Beck committed
201

Michael Beck's avatar
Michael Beck committed
202
	current_ir_graph = irg;
203

204
	if (get_opt_global_cse())
205
		set_irg_pinned(irg, op_pin_state_floats);
206

207
208
209
	/* enable unreachable code elimination */
	assert(!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE));
	set_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
210

211
212
	new_identities(irg);
	edges_assure(irg);
213
214
	assure_doms(irg);

215
216

	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
217
	irg_walk_graph(irg, NULL, opt_walker, waitq);
218

219
220
	/* any optimized nodes are stored in the wait queue,
	 * so if it's not empty, the graph has been changed */
221
	while (!pdeq_empty(waitq)) {
222
223
224
225
226
		/* finish the wait queue */
		while (! pdeq_empty(waitq)) {
			ir_node *n = (ir_node*)pdeq_getl(waitq);
			opt_walker(n, waitq);
		}
227
228
		/* Calculate dominance so we can kill unreachable code
		 * We want this intertwined with localopts for better optimization (phase coupling) */
229
		compute_doms(irg);
230
231
		irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
	}
Michael Beck's avatar
Michael Beck committed
232
	del_pdeq(waitq);
233
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
234

235
236
237
	/* disable unreachable code elimination */
	clear_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
	set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
Michael Beck's avatar
Michael Beck committed
238

239
240
241
242
243
	/* invalidate infos */
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
	clear_irg_state(irg, IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
	edges_deactivate(irg);
244

245
	/* Finally kill BAD and doublets from the keep alives.
246
	 * Doing this AFTER edges where deactivated saves cycles */
247
	end = get_irg_end(irg);
248
249
	remove_End_Bads_and_doublets(end);

Michael Beck's avatar
Michael Beck committed
250
	current_ir_graph = rem;
251
252
253
254
255

	/* Note we do not have a reliable way to detect changes, since some
	 * localopt rules change the inputs of a node and do not return a new
	 * node, so we conservatively say true here */
	return true;
256
}
257

258
ir_graph_pass_t *optimize_graph_df_pass(const char *name)
259
{
260
	return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
261
}