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
40 #include "mm/memory.h"
41 #include "vm/jit/jit.h"
44 /* reorder_place_next_unplaced_block *******************************************
46 Find next unplaced basic block in the array and place it.
48 *******************************************************************************/
50 static basicblock *reorder_place_next_unplaced_block(jitdata *jd, u1 *blocks,
56 for (i = 0; i < jd->basicblockcount; i++) {
57 if (blocks[i] == false) {
58 tbptr = &jd->basicblocks[i];
62 blocks[tbptr->nr] = true;
64 /* change the basic block order */
76 /* reorder *********************************************************************
78 Reorders basic blocks in respect to their execution frequency.
80 *******************************************************************************/
82 bool reorder(jitdata *jd)
87 basicblock *tbptr; /* taken basic block pointer */
88 basicblock *ntbptr; /* not-taken basic block pointer */
89 basicblock *pbptr; /* predecessor basic block pointer */
95 u1 *blocks; /* flag array */
96 s4 placed; /* number of placed basic blocks */
99 /* get required compiler data */
107 /* get the codeinfo of the previous method */
112 if (jd->basicblockcount > 8)
115 /* allocate flag array for blocks which are placed */
117 blocks = DMNEW(u1, jd->basicblockcount);
119 MZERO(blocks, u1, jd->basicblockcount);
121 /* Get the entry block and iterate over all basic blocks until we
122 have placed all blocks. */
124 bptr = jd->basicblocks;
127 /* the first block is always placed as the first one */
132 while (placed <= jd->basicblockcount + 1) {
133 /* get last instruction of basic block */
135 iptr = bptr->iinstr + bptr->icount - 1;
137 printf("L%03d, ", bptr->nr);
139 switch (bptr->type) {
151 printf(", predecessors: %d, successors: %d, frequency: %d -> ",
152 bptr->predecessorcount, bptr->successorcount,
153 pcode->bbfrequency[bptr->nr]);
165 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
169 /* all blocks placed? return. */
174 /* set last placed block */
212 tbptr = iptr->dst.block;
215 printf("cond. L%03d\n", tbptr->nr);
217 tfreq = pcode->bbfrequency[tbptr->nr];
218 ntfreq = pcode->bbfrequency[ntbptr->nr];
220 /* check which branch is more frequently */
222 if ((blocks[tbptr->nr] == false) && (tfreq > ntfreq)) {
223 /* If we place the taken block, we need to change the
224 conditional instruction (opcode and target). */
226 iptr->opc = jit_complement_condition(iptr->opc);
227 iptr->dst.block = ntbptr;
229 /* change the basic block order */
233 /* place taken block */
235 blocks[tbptr->nr] = true;
238 /* set last placed block */
242 else if (blocks[ntbptr->nr] == false) {
243 /* place not-taken block */
245 blocks[ntbptr->nr] = true;
248 /* set last placed block */
253 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
257 /* all blocks placed? return. */
262 /* set last placed block */
269 tbptr = iptr->dst.block;
271 printf("L%03d\n", tbptr->nr);
273 /* If next block is already placed, search for another
276 if (blocks[tbptr->nr] == true) {
277 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
281 /* all block placed? return. */
286 else if (tbptr->predecessorcount > 1) {
287 /* Check if the target block has other predecessors.
288 And if the other predecessors have a higher
289 frequency, don't place it. */
291 freq = pcode->bbfrequency[bptr->nr];
293 for (i = 0; i < tbptr->predecessorcount; i++) {
294 pbptr = tbptr->predecessors[i];
296 /* skip the current basic block */
298 if (pbptr->nr != bptr->nr) {
299 pfreq = pcode->bbfrequency[pbptr->nr];
301 /* Other predecessor has a higher frequency?
302 Search for another block to place. */
305 tbptr = reorder_place_next_unplaced_block(jd,
311 /* all block placed? return. */
324 blocks[tbptr->nr] = true;
328 /* set last placed block */
334 /* No control flow instruction at the end of the basic
335 block (fall-through). Just place the block. */
341 /* If next block is already placed, search for another
344 if (blocks[tbptr->nr] == true) {
345 tbptr = reorder_place_next_unplaced_block(jd, blocks, bptr);
349 /* all block placed? return. */
357 blocks[tbptr->nr] = true;
361 /* set last placed block */
368 /* close the basic block chain with the last dummy basic block */
370 bptr->next = &jd->basicblocks[jd->basicblockcount];
374 for (bptr = jd->basicblocks; bptr != NULL; bptr = bptr->next) {
375 printf("L%03d\n", bptr->nr);
378 /* everything's ok */
385 * These are local overrides for various environment variables in Emacs.
386 * Please do not remove this and leave it at the end of the file, where
387 * Emacs will automagically detect them.
388 * ---------------------------------------------------------------------
391 * indent-tabs-mode: t
395 * vim:noexpandtab:sw=4:ts=4: