irextbb2.c 5.74 KB
Newer Older
1
/*
2
 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
Matthias Braun's avatar
Matthias Braun committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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.
18
19
20
 */

/**
Matthias Braun's avatar
Matthias Braun committed
21
22
23
24
25
26
 * @file
 * @brief     Alternative extended basic block computation
 * @author    Matthias Braun
 * @date      5.2005
 * @version   $Id$
 * @summary
27
28
29
 *  Alternative algorithm for computing extended basic blocks (using out edges
 *  and execution frequencies)
 */
30
31
32
33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
35
36
37
38
39
40
41
42
43
44
45
46
#include "irextbb_t.h"
#include "irgwalk.h"
#include "irnode_t.h"
#include "irgraph_t.h"
#include "iredges_t.h"
#include "irouts.h"
#include "xmalloc.h"
#include "irprintf.h"
#include "execfreq.h"

typedef struct _env {
  	struct obstack *obst;   /**< the obstack where allocations took place */
	ir_extblk *head;        /**< head of the list of all extended blocks */
47
	ir_exec_freq *execfreqs;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
} env_t;

/**
 * allocate a new extended block header.
 */
static ir_extblk *allocate_extblk(ir_node *block, env_t *env)
{
	ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));

	extblk->kind    = k_ir_extblk;
	extblk->visited = 1;
	extblk->blks    = (ir_node **)env->head;
	extblk->link    = block;
	env->head       = extblk;

	set_Block_extbb(block, extblk);
	set_irn_link(block, NULL);

	return extblk;
}

/**
 * add a block to an extended block
 */
static void addto_extblk(ir_extblk *extblk, ir_node *block)
{
	/* link all blocks belonging to this extended block */
	set_irn_link(block, extblk->link);

	extblk->link = block;
	extblk->visited++;

	set_Block_extbb(block, extblk);
}

/**
 * Returns the number of block successors.
 * we are interested only in 1, 2 and >2.
 */
static int get_block_n_succs(ir_node *block) {
	if (edges_activated(current_ir_graph)) {
		const ir_edge_t *edge;

		edge = get_block_succ_first(block);
		if (! edge)
			return 0;
Christian Würdig's avatar
Christian Würdig committed
94

95
96
97
		edge = get_block_succ_next(block, edge);
		if (! edge)
			return 1;
Christian Würdig's avatar
Christian Würdig committed
98

99
100
101
102
103
104
105
106
107
108
109
		edge = get_block_succ_next(block, edge);
		return edge ? 3 : 2;
	}

	return get_Block_n_cfg_outs(block);
}

static void pick_successor(ir_node *block, ir_extblk *extblk, env_t *env);

static void create_extblk(ir_node *block, env_t *env)
{
Christian Würdig's avatar
Christian Würdig committed
110
111
112
	ir_extblk *extblk;

	if (irn_visited(block))
113
114
		return;

Christian Würdig's avatar
Christian Würdig committed
115
	extblk = allocate_extblk(block, env);
116
117
118
119
120
121
122
123
	mark_irn_visited(block);

	pick_successor(block, extblk, env);
}

static void pick_successor(ir_node *block, ir_extblk *extblk, env_t *env)
{
	const ir_edge_t *edge;
Christian Würdig's avatar
Christian Würdig committed
124
125
126
127
128
129
130
131
132
	ir_node         *best_succ    = NULL;
	double          best_execfreq = -1;

 	/*
		More than two successors means we have a jump table.
		we cannot include a jump target into the current extended
		basic block, so create a new one here.
	*/
	if (get_block_n_succs(block) > 2) {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
		const ir_edge_t *edge;

		foreach_block_succ(block, edge) {
			ir_node *succ = get_edge_src_irn(edge);
			create_extblk(succ, env);
		}

		return;
	}

	foreach_block_succ(block, edge) {
		ir_node *succ = get_edge_src_irn(edge);
		double execfreq;

147
148
149
		if(irn_visited(succ))
			continue;

150
151
152
153
154
155
156
		if(get_Block_n_cfgpreds(succ) > 1) {
			create_extblk(succ, env);
			continue;
		}

		execfreq = get_block_execfreq(env->execfreqs, succ);

Christian Würdig's avatar
Christian Würdig committed
157
158
159
160
161
162
		/*
			Remember best successor and make non best successor with only 1
			pred block to new extbb leaders.
		*/
		if (execfreq > best_execfreq) {
			if (best_succ != NULL) {
163
164
165
166
167
				create_extblk(best_succ, env);
			}

			best_execfreq = execfreq;
			best_succ = succ;
Christian Würdig's avatar
Christian Würdig committed
168
169
		}
		else {
170
171
172
173
			create_extblk(succ, env);
		}
	}

Christian Würdig's avatar
Christian Würdig committed
174
	/* add best successor and recursively try to pick more */
175
176
177
178
179
180
181
182
183
184
	if(best_succ != NULL) {
		addto_extblk(extblk, best_succ);
		mark_irn_visited(best_succ);
		pick_successor(best_succ, extblk, env);
	}
}

/*
 * Compute the extended basic blocks for a graph
 */
185
void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs) {
Christian Würdig's avatar
Christian Würdig committed
186
  	env_t     env;
187
	ir_extblk *extbb, *next;
Christian Würdig's avatar
Christian Würdig committed
188
	ir_node   *endblock;
189
190
191

	if (irg->extbb_obst) {
		obstack_free(irg->extbb_obst, NULL);
Christian Würdig's avatar
Christian Würdig committed
192
193
	}
	else {
194
195
196
197
		irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
	}
	obstack_init(irg->extbb_obst);

Christian Würdig's avatar
Christian Würdig committed
198
199
	env.obst      = irg->extbb_obst;
	env.head      = NULL;
200
201
202
203
204
205
206
207
	env.execfreqs = execfreqs;

	assure_irg_outs(irg);

	/* we must mark nodes, so increase the visited flag */
	inc_irg_visited(irg);
	create_extblk(get_irg_start_block(irg), &env);

Christian Würdig's avatar
Christian Würdig committed
208
	/* the end block needs a extbb assigned (even for endless loops) */
209
	endblock = get_irg_end_block(irg);
Christian Würdig's avatar
Christian Würdig committed
210
	if (! irn_visited(endblock)) {
211
212
213
214
		create_extblk(endblock, &env);
	}

	/*
Christian Würdig's avatar
Christian Würdig committed
215
216
217
218
219
220
		Ok, we have now the list of all extended blocks starting with env.head
		every extended block "knowns" the number of blocks in visited and
		the blocks are linked in link.
		Now we can create arrays that hold the blocks, some kind of "out" edges
		for the extended block
	*/
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
	for (extbb = env.head; extbb; extbb = next) {
		int i, len = (int)extbb->visited;
		ir_node *block;

		next = (ir_extblk *)extbb->blks;

		extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);

		for (block = extbb->link, i = 0; i < len; ++i) {
			ir_node *nblock = get_irn_link(block);

			/* ensure that the leader is the first one */
			extbb->blks[len - 1 - i] = block;
			set_irn_link(block, NULL);
			block = nblock;
		}

Adam Szalkowski's avatar
Adam Szalkowski committed
238
#if 0
239
240
241
242
243
244
		for(i = 0; i < len; ++i) {
			if(i > 0)
				printf(", ");
			ir_printf("%+F", extbb->blks[i]);
		}
		printf("\n");
Adam Szalkowski's avatar
Adam Szalkowski committed
245
#endif
246
247
248
249
250
251
252

		extbb->link    = NULL;
		extbb->visited = 0;
	}

	irg->extblk_state = extblk_valid;
}