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(jitdata *jd, u1 *blocks,
58 for (i = 0; i < jd->new_basicblockcount; i++) {
59 if (blocks[i] == false) {
60 tbptr = &jd->new_basicblocks[i];
64 blocks[tbptr->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 */
109 /* get the codeinfo of the previous method */
114 if (jd->new_basicblockcount > 8)
117 /* allocate flag array for blocks which are placed */
119 blocks = DMNEW(u1, jd->new_basicblockcount);
121 MZERO(blocks, u1, jd->new_basicblockcount);
123 /* Get the entry block and iterate over all basic blocks until we
124 have placed all blocks. */
126 bptr = jd->new_basicblocks;
129 /* the first block is always placed as the first one */
134 while (placed <= jd->new_basicblockcount + 1) {
135 /* get last instruction of basic block */
137 iptr = bptr->iinstr + bptr->icount - 1;
139 printf("L%03d, ", bptr->nr);
141 switch (bptr->type) {
153 printf(", predecessors: %d, successors: %d, frequency: %d -> ",
154 bptr->predecessorcount, bptr->successorcount,
155 pcode->bbfrequency[bptr->nr]);
167 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
171 /* all blocks placed? return. */
176 /* set last placed block */
214 tbptr = iptr->dst.block;
217 printf("cond. L%03d\n", tbptr->nr);
219 tfreq = pcode->bbfrequency[tbptr->nr];
220 ntfreq = pcode->bbfrequency[ntbptr->nr];
222 /* check which branch is more frequently */
224 if ((blocks[tbptr->nr] == false) && (tfreq > ntfreq)) {
225 /* If we place the taken block, we need to change the
226 conditional instruction (opcode and target). */
228 iptr->opc = jit_complement_condition(iptr->opc);
229 iptr->dst.block = ntbptr;
231 /* change the basic block order */
235 /* place taken block */
237 blocks[tbptr->nr] = true;
240 /* set last placed block */
244 else if (blocks[ntbptr->nr] == false) {
245 /* place not-taken block */
247 blocks[ntbptr->nr] = true;
250 /* set last placed block */
255 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
259 /* all blocks placed? return. */
264 /* set last placed block */
271 tbptr = iptr->dst.block;
273 printf("L%03d\n", tbptr->nr);
275 /* If next block is already placed, search for another
278 if (blocks[tbptr->nr] == true) {
279 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
283 /* all block placed? return. */
288 else if (tbptr->predecessorcount > 1) {
289 /* Check if the target block has other predecessors.
290 And if the other predecessors have a higher
291 frequency, don't place it. */
293 freq = pcode->bbfrequency[bptr->nr];
295 for (i = 0; i < tbptr->predecessorcount; i++) {
296 pbptr = tbptr->predecessors[i];
298 /* skip the current basic block */
300 if (pbptr->nr != bptr->nr) {
301 pfreq = pcode->bbfrequency[pbptr->nr];
303 /* Other predecessor has a higher frequency?
304 Search for another block to place. */
307 tbptr = reorder_place_next_unplaced_block(jd,
313 /* all block placed? return. */
326 blocks[tbptr->nr] = true;
330 /* set last placed block */
336 /* No control flow instruction at the end of the basic
337 block (fall-through). Just place the block. */
343 /* If next block is already placed, search for another
346 if (blocks[tbptr->nr] == true) {
347 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
351 /* all block placed? return. */
359 blocks[tbptr->nr] = true;
363 /* set last placed block */
370 /* close the basic block chain with the last dummy basic block */
372 bptr->next = &jd->new_basicblocks[jd->new_basicblockcount];
376 for (bptr = jd->new_basicblocks; bptr != NULL; bptr = bptr->next) {
377 printf("L%03d\n", bptr->nr);
380 /* everything's ok */
387 * These are local overrides for various environment variables in Emacs.
388 * Please do not remove this and leave it at the end of the file, where
389 * Emacs will automagically detect them.
390 * ---------------------------------------------------------------------
393 * indent-tabs-mode: t
397 * vim:noexpandtab:sw=4:ts=4: