irgopt.c 5.69 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Matthias Braun's avatar
Matthias Braun committed
6
7
8
9
10
/**
 * @file
 * @brief    Optimizations for a whole ir graph, i.e., a procedure.
 * @author   Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
 *           Michael Beck
Götz Lindenmaier's avatar
Götz Lindenmaier committed
11
 */
12
13
14
15
16
#include <assert.h>

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

yb9976's avatar
yb9976 committed
17
#include "constbits.h"
18
#include "iroptimize.h"
19
20
21
22
#include "iropt_t.h"
#include "irgopt.h"
#include "irgmod.h"
#include "irgwalk.h"
23
#include "ircons.h"
24

Michael Beck's avatar
Michael Beck committed
25
#include "adt/pdeq.h"
26

27
#include "irflag_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
28
#include "iredges_t.h"
29
#include "irtools.h"
30

31
32
33
/**
 * A wrapper around optimize_inplace_2() to be called from a walker.
 */
34
35
static void optimize_in_place_wrapper(ir_node *n, void *env)
{
Matthias Braun's avatar
cleanup    
Matthias Braun committed
36
	(void)env;
Michael Beck's avatar
Michael Beck committed
37
	ir_node *optimized = optimize_in_place_2(n);
38

Matthias Braun's avatar
cleanup    
Matthias Braun committed
39
	if (optimized != n)
40
		exchange(n, optimized);
41
42
}

43
void local_optimize_node(ir_node *n)
44
{
45
46
	ir_graph *irg = get_irn_irg(n);

Michael Beck's avatar
Michael Beck committed
47
	if (get_opt_global_cse())
48
		set_irg_pinned(irg, op_pin_state_floats);
49
	clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
50

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

Michael Beck's avatar
Michael Beck committed
54
	/* walk over the graph */
55
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
56
	irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
57
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
58
59
}

60
61
62
63
64
65
66
67
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);
}

68
69
70
71
72
73
74
/**
 * Enqueue all users of a node to a wait queue.
 * Handles mode_T nodes.
 */
static void enqueue_users(ir_node *n, pdeq *waitq)
{
	foreach_out_edge(n, edge) {
75
		ir_node *succ  = get_edge_src_irn(edge);
76

77
		enqueue_node(succ, waitq);
yb9976's avatar
yb9976 committed
78
79
80
81
82
83
84
85
86
87
88

		/* 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) {
89
90
91
92
93
94
95
		/* 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);
		}
	}
}

96
97
98
/**
 * Block-Walker: uses dominance depth to mark dead blocks.
 */
99
static void find_unreachable_blocks(ir_node *block, void *env)
100
{
101
	pdeq *waitq = (pdeq*) env;
102

Michael Beck's avatar
Michael Beck committed
103
	if (get_Block_dom_depth(block) < 0) {
104
		ir_graph *irg = get_irn_irg(block);
105
106
107
108
109
110
111
112
113
114
115
116
		ir_node  *end = get_irg_end(irg);

		foreach_block_succ(block, edge) {
			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
117
	}
118
119
}

120
121
void local_optimize_graph(ir_graph *irg)
{
122
	local_optimize_node(get_irg_end(irg));
Christian Schäfer's avatar
Christian Schäfer committed
123
124
}

125
126
/**
 * Data flow optimization walker.
127
 * Optimizes all nodes and enqueue its users
Michael Beck's avatar
Michael Beck committed
128
 * if done.
129
 */
130
131
static void opt_walker(ir_node *n, void *env)
{
132
133
	pdeq *waitq = (pdeq *)env;
	set_irn_link(n, NULL);
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	/* If CSE occurs during the optimization,
	 * our operands have fewer users than before.
	 * Thus, we may be able to apply a rule that
	 * requires an operand with only one user.
	 * Hence, we need a loop to reach the fixpoint. */
	ir_node *optimized = n;
	ir_node *last;
	do {
		last      = optimized;
		optimized = optimize_in_place_2(last);

		if (optimized != last) {
			enqueue_users(last, waitq);
			exchange(last, optimized);
		}
	} while (optimized != last);
151
152
}

153
void optimize_graph_df(ir_graph *irg)
154
{
155
	pdeq *waitq = new_pdeq();
156

157
	if (get_opt_global_cse())
158
		set_irg_pinned(irg, op_pin_state_floats);
159

160
161
162
163
164
165
166
167
	/* enable unreachable code elimination,
	 * not that currently disabling algebraic simplifications disables all
	 * transform_node_XXX() functions and therefore unreachable code
	 * elimination. */
	if (get_opt_algebraic_simplification()) {
		assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE));
		add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
	}
168

169
	new_identities(irg);
170
171
	assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
	                         | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
172

yb9976's avatar
yb9976 committed
173
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
yb9976's avatar
yb9976 committed
174

yb9976's avatar
yb9976 committed
175
	constbits_analyze(irg);
yb9976's avatar
yb9976 committed
176

177
	irg_walk_graph(irg, NULL, opt_walker, waitq);
178

179
180
	/* any optimized nodes are stored in the wait queue,
	 * so if it's not empty, the graph has been changed */
181
	while (!pdeq_empty(waitq)) {
182
183
184
185
186
		/* finish the wait queue */
		while (! pdeq_empty(waitq)) {
			ir_node *n = (ir_node*)pdeq_getl(waitq);
			opt_walker(n, waitq);
		}
187
		/* Calculate dominance so we can kill unreachable code
188
189
		 * We want this intertwined with localopts for better optimization
		 * (phase coupling) */
190
		compute_doms(irg);
191
192
		irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
	}
Michael Beck's avatar
Michael Beck committed
193
	del_pdeq(waitq);
yb9976's avatar
yb9976 committed
194
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
yb9976's avatar
yb9976 committed
195
196

	constbits_clear(irg);
197

198
199
200
	confirm_irg_properties(irg, IR_GRAPH_PROPERTY_ONE_RETURN
	                            | IR_GRAPH_PROPERTY_MANY_RETURNS
	                            | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES);
yb9976's avatar
yb9976 committed
201

202
	if (get_opt_algebraic_simplification()) {
yb9976's avatar
yb9976 committed
203
		/* Unreachable code elimination was enabled. */
204
		clear_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
205
		add_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
206
	}
207

208
	/* Finally kill BAD and doublets from the keep alives.
209
	 * Doing this AFTER edges where deactivated saves cycles */
yb9976's avatar
yb9976 committed
210
	ir_node *end = get_irg_end(irg);
211
	remove_End_Bads_and_doublets(end);
212
}
213

214
215
216
217
218
219
void local_opts_const_code(void)
{
	ir_graph *irg = get_const_code_irg();
	/* Clean the value_table in irg for the CSE. */
	new_identities(irg);

220
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
221
	walk_const_code(firm_clear_link, optimize_in_place_wrapper, NULL);
222
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
223
}