1 /* src/vm/reorder.c - basic block reordering
3 Copyright (C) 2006 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
31 $Id: cacao.c 4357 2006-01-22 23:33:38Z twisti $
42 #include "mm/memory.h"
43 #include "vm/jit/jit.h"
46 /* reorder_place_next_unplaced_block *******************************************
48 Find next unplaced basic block in the array and place it.
50 *******************************************************************************/
52 static basicblock *reorder_place_next_unplaced_block(methodinfo *m, u1 *blocks,
58 for (i = 0; i < m->basicblockcount; i++) {
59 if (blocks[i] == false) {
60 tbptr = &m->basicblocks[i];
64 blocks[tbptr->debug_nr] = true;
66 /* change the basic block order */
78 /* reorder *********************************************************************
80 Reorders basic blocks in respect to their execution frequency.
82 *******************************************************************************/
84 bool reorder(jitdata *jd)
89 basicblock *tbptr; /* taken basic block pointer */
90 basicblock *ntbptr; /* not-taken basic block pointer */
91 basicblock *pbptr; /* predecessor basic block pointer */
97 u1 *blocks; /* flag array */
98 s4 placed; /* number of placed basic blocks */
101 /* get required compiler data */
106 /* get the codeinfo of the previous method */
111 if (m->basicblockcount > 8)
114 /* allocate flag array for blocks which are placed */
116 blocks = DMNEW(u1, m->basicblockcount);
118 MZERO(blocks, u1, m->basicblockcount);
120 /* Get the entry block and iterate over all basic blocks until we
121 have placed all blocks. */
123 bptr = m->basicblocks;
126 /* the first block is always placed as the first one */
131 while (placed <= m->basicblockcount) {
132 /* get last instruction of basic block */
134 iptr = bptr->iinstr + bptr->icount - 1;
136 printf("L%03d, ", bptr->debug_nr);
138 switch (bptr->type) {
150 printf(", predecessors: %d, successors: %d, frequency: %d -> ",
151 bptr->predecessorcount, bptr->successorcount,
152 pcode->bbfrequency[bptr->debug_nr]);
164 tbptr = reorder_place_next_unplaced_block(m, blocks, bptr);
168 /* all blocks placed? return. */
173 /* set last placed block */
211 tbptr = (basicblock *) iptr->target;
214 printf("cond. L%03d\n", tbptr->debug_nr);
216 tfreq = pcode->bbfrequency[tbptr->debug_nr];
217 ntfreq = pcode->bbfrequency[ntbptr->debug_nr];
219 /* check which branch is more frequently */
221 if ((blocks[tbptr->debug_nr] == false) && (tfreq > ntfreq)) {
222 /* If we place the taken block, we need to change the
223 conditional instruction (opcode and target). */
225 iptr->opc = jit_complement_condition(iptr->opc);
226 iptr->target = ntbptr;
228 /* change the basic block order */
232 /* place taken block */
234 blocks[tbptr->debug_nr] = true;
237 /* set last placed block */
241 else if (blocks[ntbptr->debug_nr] == false) {
242 /* place not-taken block */
244 blocks[ntbptr->debug_nr] = true;
247 /* set last placed block */
252 tbptr = reorder_place_next_unplaced_block(m, blocks, bptr);
256 /* all blocks placed? return. */
261 /* set last placed block */
268 tbptr = (basicblock *) iptr->target;
270 printf("L%03d\n", tbptr->debug_nr);
272 /* If next block is already placed, search for another
275 if (blocks[tbptr->debug_nr] == true) {
276 tbptr = reorder_place_next_unplaced_block(m, blocks, bptr);
280 /* all block placed? return. */
285 else if (tbptr->predecessorcount > 1) {
286 /* Check if the target block has other predecessors.
287 And if the other predecessors have a higher
288 frequency, don't place it. */
290 freq = pcode->bbfrequency[bptr->debug_nr];
292 for (i = 0; i < tbptr->predecessorcount; i++) {
293 pbptr = tbptr->predecessors[i];
295 /* skip the current basic block */
297 if (pbptr->debug_nr != bptr->debug_nr) {
298 pfreq = pcode->bbfrequency[pbptr->debug_nr];
300 /* Other predecessor has a higher frequency?
301 Search for another block to place. */
304 tbptr = reorder_place_next_unplaced_block(m, blocks,
309 /* all block placed? return. */
322 blocks[tbptr->debug_nr] = true;
326 /* set last placed block */
332 /* No control flow instruction at the end of the basic
333 block (fall-through). Just place the block. */
339 /* If next block is already placed, search for another
342 if (blocks[tbptr->debug_nr] == true) {
343 tbptr = reorder_place_next_unplaced_block(m, blocks, bptr);
347 /* all block placed? return. */
355 blocks[tbptr->debug_nr] = true;
359 /* set last placed block */
366 /* close the basic block chain with the last dummy basic block */
368 bptr->next = &m->basicblocks[m->basicblockcount];
372 for (bptr = m->basicblocks; bptr != NULL; bptr = bptr->next) {
373 printf("L%03d\n", bptr->debug_nr);
376 /* everything's ok */
383 * These are local overrides for various environment variables in Emacs.
384 * Please do not remove this and leave it at the end of the file, where
385 * Emacs will automagically detect them.
386 * ---------------------------------------------------------------------
389 * indent-tabs-mode: t
393 * vim:noexpandtab:sw=4:ts=4: