irgopt.c 5.18 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
55
	/* walk over the graph */
	irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
56
57
}

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

66
67
68
69
70
71
72
/**
 * 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) {
73
		ir_node *succ  = get_edge_src_irn(edge);
74

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

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

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

Michael Beck's avatar
Michael Beck committed
101
	if (get_Block_dom_depth(block) < 0) {
102
		ir_graph *irg = get_irn_irg(block);
103
104
105
106
107
108
109
110
111
112
113
114
		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
115
	}
116
117
}

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

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

	if (optimized != n) {
		enqueue_users(n, waitq);
		exchange(n, optimized);
Michael Beck's avatar
Michael Beck committed
137
	}
138
139
}

140
void optimize_graph_df(ir_graph *irg)
141
{
142
	pdeq *waitq = new_pdeq();
143

144
	if (get_opt_global_cse())
145
		set_irg_pinned(irg, op_pin_state_floats);
146

147
148
149
150
151
152
153
154
	/* 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);
	}
155

156
	new_identities(irg);
157
158
	assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
	                         | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
159

yb9976's avatar
yb9976 committed
160
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
yb9976's avatar
yb9976 committed
161

yb9976's avatar
yb9976 committed
162
	constbits_analyze(irg);
yb9976's avatar
yb9976 committed
163

164
	irg_walk_graph(irg, NULL, opt_walker, waitq);
165

166
167
	/* any optimized nodes are stored in the wait queue,
	 * so if it's not empty, the graph has been changed */
168
	while (!pdeq_empty(waitq)) {
169
170
171
172
173
		/* finish the wait queue */
		while (! pdeq_empty(waitq)) {
			ir_node *n = (ir_node*)pdeq_getl(waitq);
			opt_walker(n, waitq);
		}
174
		/* Calculate dominance so we can kill unreachable code
175
176
		 * We want this intertwined with localopts for better optimization
		 * (phase coupling) */
177
		compute_doms(irg);
178
179
		irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
	}
Michael Beck's avatar
Michael Beck committed
180
	del_pdeq(waitq);
yb9976's avatar
yb9976 committed
181
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
yb9976's avatar
yb9976 committed
182
183

	constbits_clear(irg);
184

185
186
187
	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
188

189
	if (get_opt_algebraic_simplification()) {
yb9976's avatar
yb9976 committed
190
		/* Unreachable code elimination was enabled. */
191
		clear_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
192
		add_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
193
	}
194

195
	/* Finally kill BAD and doublets from the keep alives.
196
	 * Doing this AFTER edges where deactivated saves cycles */
yb9976's avatar
yb9976 committed
197
	ir_node *end = get_irg_end(irg);
198
	remove_End_Bads_and_doublets(end);
199
}
200

201
202
203
204
205
206
207
208
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);

	walk_const_code(firm_clear_link, optimize_in_place_wrapper, NULL);
}