1 /* src/vm/jit/jit.hpp - Just-In-Time compiler
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 /* forward typedefs ***********************************************************/
31 typedef struct jitdata jitdata;
32 typedef struct basicblock basicblock;
33 typedef struct exception_entry exception_entry;
39 #include "vm/global.h"
40 #include "vm/method.h"
41 #include "vm/references.h"
42 #include "vm/resolve.h"
44 #if defined(ENABLE_STATISTICS)
45 # include "vm/statistics.h"
48 #include "vm/jit/codegen-common.hpp"
49 #include "vm/jit/reg.h"
50 #include "vm/jit/replace.hpp"
51 #include "vm/jit/stack.h"
52 #include "vm/jit/stacktrace.hpp"
54 #if defined(ENABLE_INLINING)
55 # include "vm/jit/inline/inline.h"
58 #include "vm/jit/ir/bytecode.h"
59 #include "vm/jit/ir/instruction.hpp"
61 #if defined(ENABLE_LOOP)
62 # include "vm/jit/loop/loop.h"
64 #if defined(ENABLE_SSA)
65 # include "vm/jit/optimizing/lsra.h"
67 #if defined(ENABLE_LSRA)
68 # include "vm/jit/allocator/lsra.h"
71 #include "vm/jit/verify/typeinfo.h"
74 /* common jit/codegen macros **************************************************/
76 #if defined(ENABLE_STATISTICS)
77 # define COUNT(x) (x)++
78 # define COUNT_SPILLS /* use COUNT_(READ|WRITE)_SPILLS instead */
79 # define COUNT_READ_SPILLS(var) \
81 case TYPE_FLT: count_spills_read_flt++; break; \
82 case TYPE_DBL: count_spills_read_dbl++; break; \
83 default: count_spills_read_ila++; break; \
86 # define COUNT_WRITE_SPILLS(var) \
88 case TYPE_FLT: count_spills_write_flt++; break; \
89 case TYPE_DBL: count_spills_write_dbl++; break; \
90 default: count_spills_write_ila++; break; \
94 # define COUNT(x) /* nothing */
95 # define COUNT_SPILLS /* nothing */
96 # define COUNT_READ_SPILLS(x) /* nothing */
97 # define COUNT_WRITE_SPILLS(x) /* nothing */
100 typedef struct interface_info interface_info;
102 struct interface_info {
108 /* jitdata ********************************************************************/
111 methodinfo *m; /* methodinfo of the method compiled */
115 #if defined(ENABLE_LOOP)
118 #if defined(ENABLE_SSA) || defined(ENABLE_LSRA)
122 u4 flags; /* contains JIT compiler flags */
124 instruction *instructions; /* ICMDs, valid between parse and stack */
125 basicblock *basicblocks; /* start of basic block list */
126 stackelement_t *stack; /* XXX should become stack.c internal */
127 s4 instructioncount;/* XXX remove this? */
128 s4 basicblockcount; /* number of basic blocks */
129 s4 stackcount; /* number of stackelements to allocate */
130 /* (passed from parse to stack) */
132 varinfo *var; /* array of variables */
133 s4 vartop; /* next free index in var array */
135 s4 varcount; /* number of variables in var array */
136 s4 localcount; /* number of locals at start of var ar. */
137 s4 *local_map; /* map for renaming (de-coallescing) */
138 /* locals and keeping the coalescing info for simplereg. */
139 /* local_map[javaindex * 5 + type] = */
140 /* >= 0......index into jd->var, or */
141 /* UNUSED....this (javaindex,type) pair is not used */
143 s4 *reverselocalmap; /* map from CACAO varindex to javaindex */
144 /* (varindex must be < localcount) */
146 s4 maxlocals; /* max. number of javalocals */
148 interface_info *interface_map; /* interface variables (for simplereg) */
149 s4 maxinterfaces; /* max. number of interface variables */
151 s4 exceptiontablelength; /* exceptiontable length */
152 exception_entry *exceptiontable; /* the exceptiontable */
154 basicblock *returnblock; /* block containing the *RETURN */
155 /* (only use if returncount==1) */
156 s4 returncount; /* number of return instructions */
157 bool branchtoentry; /* true if first block is a target */
158 bool branchtoend; /* true if end dummy is a target */
161 #define FOR_EACH_BASICBLOCK(jd, it) \
162 for ((it) = (jd)->basicblocks; (it) != NULL; (it) = (it)->next)
166 #define JITDATA_FLAG_PARSE 0x00000001
167 #define JITDATA_FLAG_VERIFY 0x00000002
169 #define JITDATA_FLAG_INSTRUMENT 0x00000004
171 #define JITDATA_FLAG_IFCONV 0x00000008
172 #define JITDATA_FLAG_REORDER 0x00000010
173 #define JITDATA_FLAG_INLINE 0x00000020
175 #define JITDATA_FLAG_COUNTDOWN 0x00000100
177 #define JITDATA_FLAG_SHOWINTERMEDIATE 0x20000000
178 #define JITDATA_FLAG_SHOWDISASSEMBLE 0x40000000
179 #define JITDATA_FLAG_VERBOSECALL 0x80000000
182 #define JITDATA_HAS_FLAG_PARSE(jd) \
183 ((jd)->flags & JITDATA_FLAG_PARSE)
185 #define JITDATA_HAS_FLAG_VERIFY(jd) \
186 ((jd)->flags & JITDATA_FLAG_VERIFY)
188 #define JITDATA_HAS_FLAG_INSTRUMENT(jd) \
189 ((jd)->flags & JITDATA_FLAG_INSTRUMENT)
191 #define JITDATA_HAS_FLAG_IFCONV(jd) \
192 ((jd)->flags & JITDATA_FLAG_IFCONV)
194 #define JITDATA_HAS_FLAG_REORDER(jd) \
195 ((jd)->flags & JITDATA_FLAG_REORDER)
197 #define JITDATA_HAS_FLAG_INLINE(jd) \
198 ((jd)->flags & JITDATA_FLAG_INLINE)
200 #define JITDATA_HAS_FLAG_COUNTDOWN(jd) \
201 ((jd)->flags & JITDATA_FLAG_COUNTDOWN)
203 #define JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd) \
204 ((jd)->flags & JITDATA_FLAG_SHOWINTERMEDIATE)
206 #define JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd) \
207 ((jd)->flags & JITDATA_FLAG_SHOWDISASSEMBLE)
209 #define JITDATA_HAS_FLAG_VERBOSECALL(jd) \
210 ((jd)->flags & JITDATA_FLAG_VERBOSECALL)
213 /* exception_entry ************************************************************/
215 struct exception_entry {
219 classref_or_classinfo catchtype; /* catchtype of exc. (NULL == catchall) */
220 exception_entry *next; /* next in list of exceptions when */
221 /* loops are copied */
222 exception_entry *down; /* next exception_entry */
226 /* macros for accessing variables *********************************************
228 Use VAROP for s1, s2, s3 and dst operands (eg. VAROP(iptr->s1)),
229 use VAR if you have the variable index (eg. VAR(iptr->sx.s23.s2.args[0])).
231 ******************************************************************************/
233 #define VAROP(v) (jd->var + (v).varindex)
234 #define VAR(i) (jd->var + (i))
236 static inline bool var_is_local(const jitdata *jd, s4 i) {
237 return (i < jd->localcount);
240 static inline bool var_is_prealloc(const jitdata *jd, s4 i) {
241 return ((i >= jd->localcount) && (jd->var[i].flags & PREALLOC));
244 static inline bool var_is_inout(const jitdata *jd, s4 i) {
245 const varinfo *v = jd->var + i;
247 (i >= jd->localcount) && (
248 (!(v->flags & PREALLOC) && (v->flags & INOUT)) ||
249 /* special case of TYPE_RET, used with JSR */
250 ((v->flags & PREALLOC) && (v->flags & INOUT) && (v->type == TYPE_RET))
255 static inline bool var_is_temp(const jitdata *jd, s4 i) {
256 const varinfo *v = jd->var + i;
257 return ((i >= jd->localcount) && !(v->flags & PREALLOC) && !(v->flags & INOUT));
260 static inline bool var_is_saved(const jitdata *jd, s4 i) {
261 return (jd->var[i].flags & SAVEDVAR);
265 /* basicblock *****************************************************************/
274 #define BBTYPECHECK_UNDEF 2
275 #define BBTYPECHECK_REACHED 3
277 #define BBTYPE_STD 0 /* standard basic block type */
278 #define BBTYPE_EXH 1 /* exception handler basic block type */
279 #define BBTYPE_SBR 2 /* subroutine basic block type */
281 #define BBFLAG_REPLACEMENT 0x01 /* put a replacement point at the start */
283 /* XXX basicblock wastes quite a lot of memory by having four flag fields */
284 /* (flags, bitflags, type and lflags). Probably the last three could be */
285 /* combined without loss of efficiency. The first one could be combined with */
286 /* the others by using bitfields. */
288 /* XXX "flags" should probably be called "state", as it is an integer state */
291 s4 nr; /* basic block number */
292 s4 flags; /* used during stack analysis, init with -1 */
293 s4 bitflags; /* OR of BBFLAG_... constants, init with 0 */
294 s4 type; /* basic block type (std, xhandler, subroutine*/
295 s4 lflags; /* used during loop copying, init with 0 */
297 s4 icount; /* number of intermediate code instructions */
298 instruction *iinstr; /* pointer to intermediate code instructions */
300 varinfo *inlocals; /* copy of locals on block entry */
301 s4 *javalocals; /* map from java locals to cacao variables[+] */
302 s4 *invars; /* array of in-variables at begin of block */
303 s4 *outvars; /* array of out-variables at end of block */
304 s4 indepth; /* stack depth at begin of basic block */
305 s4 outdepth; /* stack depth end of basic block */
306 s4 varstart; /* index of first non-invar block variable */
307 s4 varcount; /* number of non-invar block variables */
311 basicblock **predecessors; /* array of predecessor basic blocks */
312 basicblock **successors; /* array of successor basic blocks */
314 branchref *branchrefs; /* list of branches to be patched */
316 basicblock *next; /* used to build a BB list (instead of array) */
317 basicblock *copied_to; /* points to the copy of this basic block */
318 /* when loop nodes are copied */
319 basicblock *original; /* block of which this block is a clone */
320 /* NULL for the original block itself */
321 methodinfo *method; /* method this block belongs to */
322 insinfo_inline *inlineinfo; /* inlineinfo for the start of this block */
324 s4 mpc; /* machine code pc at start of block */
326 /* TODO: those fields are probably usefull for other passes as well. */
328 #if defined(ENABLE_SSA)
329 basicblock *idom; /* Immediate dominator, parent in dominator tree */
330 basicblock **domsuccessors;/* Children in dominator tree */
331 s4 domsuccessorcount;
333 basicblock **domfrontier; /* Dominance frontier */
336 basicblock **exhandlers; /* Exception handlers for this block */
338 basicblock **expredecessors; /* Blocks this block is exception handler for */
339 s4 expredecessorcount;
340 s4 exouts; /* Number of exceptional exits */
342 instruction *phis; /* Phi functions */
343 s4 phicount; /* Number of phi functions */
345 void *vp; /* Freely used by different passes */
349 #define FOR_EACH_SUCCESSOR(bptr, it) \
350 for ((it) = (bptr)->successors; (it) != (bptr)->successors + (bptr)->successorcount; ++(it))
352 #define FOR_EACH_PREDECESSOR(bptr, it) \
354 (it) = (bptr)->predecessors; \
355 (it) != (bptr)->predecessors + ((bptr)->predecessorcount < 0 ? 0 : (bptr)->predecessorcount); \
359 #define FOR_EACH_INSTRUCTION(bptr, it) \
360 for ((it) = (bptr)->iinstr; (it) != (bptr)->iinstr + (bptr)->icount; ++(it))
362 #define FOR_EACH_INSTRUCTION_REV(bptr, it) \
363 for ((it) = (bptr)->iinstr + (bptr)->icount - 1; (it) != (bptr)->iinstr - 1; --(it))
365 #if defined(ENABLE_SSA)
367 #define FOR_EACH_EXHANDLER(bptr, it) \
368 for ((it) = (bptr)->exhandlers; (it) != (bptr)->exhandlers + (bptr)->exhandlercount; ++(it))
370 #define FOR_EACH_EXPREDECESSOR(bptr, it) \
371 for ((it) = (bptr)->expredecessors; (it) != (bptr)->expredecessors + (bptr)->expredecessorcount; ++(it))
375 /* [+]...the javalocals array: This array is indexed by the javaindex (the */
376 /* local variable index ocurring in the original bytecode). An element */
377 /* javalocals[javaindex] encodes where to find the contents of the */
378 /* original variable at this point in the program. */
379 /* There are three cases for javalocals[javaindex]: */
380 /* >= 0.......it's an index into the jd->var array, where the */
381 /* CACAO variable corresponding to the original local */
383 /* UNUSED.....the original variable is not live at this point */
384 /* < UNUSED...the original variable contains a returnAddress at */
385 /* this point. The number of the block to return to can */
386 /* be calculated using RETADDR_FROM_JAVALOCAL: */
388 /* javalocals[javaindex] == JAVALOCAL_FROM_RETADDR(nr) */
389 /* RETADDR_FROM_JAVALOCAL(javalocals[javaindex]) == nr */
391 #define JAVALOCAL_FROM_RETADDR(nr) (UNUSED - (1 + (nr)))
392 #define RETADDR_FROM_JAVALOCAL(jl) (UNUSED - (1 + (jl)))
395 /* Macro for initializing newly allocated basic block's. It does not
396 need to zero fields, as we zero out the whole basic block array. */
398 #define BASICBLOCK_INIT(bptr,m) \
402 bptr->type = BBTYPE_STD; \
403 bptr->method = (m); \
406 static inline bool basicblock_reached(const basicblock *bptr) {
407 return (bptr->flags >= BBREACHED);
412 /***************************** register types *********************************/
414 #define REG_RES 0 /* reserved register for OS or code generator */
415 #define REG_RET 1 /* return value register */
416 #define REG_EXC 2 /* exception value register */
417 #define REG_SAV 3 /* (callee) saved register */
418 #define REG_TMP 4 /* scratch temporary register (caller saved) */
419 #define REG_ARG 5 /* argument register (caller saved) */
421 #define REG_END -1 /* last entry in tables */
423 #define PARAMMODE_NUMBERED 0
424 #define PARAMMODE_STUFFED 1
427 /* function prototypes ********************************************************/
433 /* compiler initialisation */
436 /* compiler finalisation */
437 void jit_close(void);
439 /* create a new jitdata */
440 jitdata *jit_jitdata_new(methodinfo *m);
442 /* compile a method with jit compiler */
443 u1 *jit_compile(methodinfo *m);
444 u1 *jit_recompile(methodinfo *m);
446 void jit_invalidate_code(methodinfo *m);
447 codeinfo *jit_get_current_code(methodinfo *m);
448 void jit_request_optimization(methodinfo *m);
450 /* patch the method entrypoint */
451 #if !defined(JIT_COMPILER_VIA_SIGNAL)
452 void *jit_asm_compile(methodinfo *m, void* mptr, void* sp, void* ra);
454 void *jit_compile_handle(methodinfo *m, void *pv, void *ra, void *mptr);
456 s4 jit_complement_condition(s4 opcode);
458 void jit_renumber_basicblocks(jitdata *jd);
460 void jit_check_basicblock_numbers(jitdata *jd);
464 /* machine dependent functions ************************************************/
466 #if defined(ENABLE_JIT)
470 #if defined(ENABLE_INTRP)
471 void intrp_md_init(void);
474 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr);
484 * These are local overrides for various environment variables in Emacs.
485 * Please do not remove this and leave it at the end of the file, where
486 * Emacs will automagically detect them.
487 * ---------------------------------------------------------------------
490 * indent-tabs-mode: t
494 * vim:noexpandtab:sw=4:ts=4: