blob: 71eed5da166cb1f062ac5fabd044df30d630bb9f [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ben Cheng33672452010-01-12 14:59:30 -080017#include <Thread.h>
18
Ben Chengba4fc8b2009-06-01 13:00:29 -070019#ifndef _DALVIK_VM_COMPILER
20#define _DALVIK_VM_COMPILER
21
22#define CODE_CACHE_SIZE 1024*1024
23#define MAX_JIT_RUN_LEN 64
24#define COMPILER_WORK_QUEUE_SIZE 100
25
26#define COMPILER_TRACED(X)
27#define COMPILER_TRACEE(X)
28#define COMPILER_TRACE_CHAINING(X)
29
Bill Buzbee716f1202009-07-23 13:22:09 -070030typedef enum JitInstructionSetType {
31 DALVIK_JIT_NONE = 0,
32 DALVIK_JIT_ARM,
33 DALVIK_JIT_THUMB,
34 DALVIK_JIT_THUMB2,
Bill Buzbee9bc3df32009-07-30 10:52:29 -070035 DALVIK_JIT_THUMB2EE,
Bill Buzbee716f1202009-07-23 13:22:09 -070036 DALVIK_JIT_X86
37} JitInstructionSetType;
38
39/* Description of a compiled trace. */
40typedef struct JitTranslationInfo {
Ben Chengccd6c012009-10-15 14:52:45 -070041 void *codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -070042 JitInstructionSetType instructionSet;
Ben Cheng60c24f42010-01-04 12:29:56 -080043 bool discardResult; // Used for debugging divergence and IC patching
Ben Cheng33672452010-01-12 14:59:30 -080044 Thread *requestingThread; // For debugging purpose
Bill Buzbee716f1202009-07-23 13:22:09 -070045} JitTranslationInfo;
46
Ben Chengba4fc8b2009-06-01 13:00:29 -070047typedef enum WorkOrderKind {
48 kWorkOrderInvalid = 0, // Should never see by the backend
49 kWorkOrderMethod = 1, // Work is to compile a whole method
50 kWorkOrderTrace = 2, // Work is to compile code fragment(s)
Ben Chengccd6c012009-10-15 14:52:45 -070051 kWorkOrderTraceDebug = 3, // Work is to compile/debug code fragment(s)
Ben Cheng60c24f42010-01-04 12:29:56 -080052 kWorkOrderICPatch = 4, // Work is to patch a polymorphic callsite
Ben Chengba4fc8b2009-06-01 13:00:29 -070053} WorkOrderKind;
54
55typedef struct CompilerWorkOrder {
56 const u2* pc;
57 WorkOrderKind kind;
58 void* info;
Bill Buzbee716f1202009-07-23 13:22:09 -070059 JitTranslationInfo result;
Ben Chengba4fc8b2009-06-01 13:00:29 -070060} CompilerWorkOrder;
61
62typedef enum JitState {
63 kJitOff = 0,
64 kJitNormal = 1, // Profiling in mterp or running native
65 kJitTSelectRequest = 2, // Transition state - start trace selection
66 kJitTSelect = 3, // Actively selecting trace in dbg interp
67 kJitTSelectAbort = 4, // Something threw during selection - abort
68 kJitTSelectEnd = 5, // Done with the trace - wrap it up
69 kJitSingleStep = 6, // Single step interpretation
70 kJitSingleStepEnd = 7, // Done with single step, return to mterp
Jeff Hao97319a82009-08-12 16:57:15 -070071 kJitSelfVerification = 8, // Self Verification Mode
Ben Chengba4fc8b2009-06-01 13:00:29 -070072} JitState;
73
Jeff Hao97319a82009-08-12 16:57:15 -070074#if defined(WITH_SELF_VERIFICATION)
75typedef enum SelfVerificationState {
76 kSVSIdle = 0, // Idle
77 kSVSStart = 1, // Shadow space set up, running compiled code
78 kSVSPunt = 2, // Exiting compiled code by punting
79 kSVSSingleStep = 3, // Exiting compiled code by single stepping
80 kSVSTraceSelect = 4, // Exiting compiled code by trace select
81 kSVSNormal = 5, // Exiting compiled code normally
82 kSVSNoChain = 6, // Exiting compiled code by no chain
83 kSVSBackwardBranch = 7, // Exiting compiled code with backward branch trace
84 kSVSDebugInterp = 8, // Normal state restored, running debug interpreter
85} SelfVerificationState;
86#endif
87
Ben Chengba4fc8b2009-06-01 13:00:29 -070088typedef enum JitHint {
89 kJitHintNone = 0,
90 kJitHintTaken = 1, // Last inst in run was taken branch
91 kJitHintNotTaken = 2, // Last inst in run was not taken branch
92 kJitHintNoBias = 3, // Last inst in run was unbiased branch
93} jitHint;
94
95/*
96 * Element of a Jit trace description. Describes a contiguous
97 * sequence of Dalvik byte codes, the last of which can be
98 * associated with a hint.
99 * Dalvik byte code
100 */
101typedef struct {
102 u2 startOffset; // Starting offset for trace run
103 unsigned numInsts:8; // Number of Byte codes in run
104 unsigned runEnd:1; // Run ends with last byte code
105 jitHint hint:7; // Hint to apply to final code of run
106} JitCodeDesc;
107
108typedef union {
109 JitCodeDesc frag;
110 void* hint;
111} JitTraceRun;
112
113/*
114 * Trace description as will appear in the translation cache. Note
115 * flexible array at end, as these will be of variable size. To
116 * conserve space in the translation cache, total length of JitTraceRun
117 * array must be recomputed via seqential scan if needed.
118 */
119typedef struct {
120 const Method* method;
121 JitTraceRun trace[];
122} JitTraceDescription;
123
Ben Cheng8b258bf2009-06-24 17:27:07 -0700124typedef struct CompilerMethodStats {
125 const Method *method; // Used as hash entry signature
126 int dalvikSize; // # of bytes for dalvik bytecodes
127 int compiledDalvikSize; // # of compiled dalvik bytecodes
128 int nativeSize; // # of bytes for produced native code
129} CompilerMethodStats;
130
Ben Chengba4fc8b2009-06-01 13:00:29 -0700131bool dvmCompilerSetupCodeCache(void);
132bool dvmCompilerArchInit(void);
133void dvmCompilerArchDump(void);
134bool dvmCompilerStartup(void);
135void dvmCompilerShutdown(void);
136bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info);
137void *dvmCheckCodeCache(void *method);
Bill Buzbee716f1202009-07-23 13:22:09 -0700138bool dvmCompileMethod(const Method *method, JitTranslationInfo *info);
139bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts,
140 JitTranslationInfo *info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700141void dvmCompilerDumpStats(void);
142void dvmCompilerDrainQueue(void);
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700143void dvmJitUnchainAll(void);
Bill Buzbee716f1202009-07-23 13:22:09 -0700144void dvmCompilerSortAndPrintTraceProfiles(void);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700145
Ben Cheng4238ec22009-08-24 16:32:22 -0700146struct CompilationUnit;
147struct BasicBlock;
148struct SSARepresentation;
149struct GrowableList;
150
151void dvmInitializeSSAConversion(struct CompilationUnit *cUnit);
152int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg);
153void dvmCompilerLoopOpt(struct CompilationUnit *cUnit);
154void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit);
155void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit,
156 struct BasicBlock *bb);
157void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit,
158 struct BasicBlock *bb);
159void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit,
160 struct BasicBlock *bb);
161void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit,
162 struct BasicBlock *bb);
Ben Chengccd6c012009-10-15 14:52:45 -0700163char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn);
Ben Cheng4238ec22009-08-24 16:32:22 -0700164char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit,
165 struct SSARepresentation *ssaRep);
166void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit,
167 void (*func)(struct CompilationUnit *, struct BasicBlock *));
Ben Chengccd6c012009-10-15 14:52:45 -0700168JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc);
Ben Cheng4238ec22009-08-24 16:32:22 -0700169
Ben Chengba4fc8b2009-06-01 13:00:29 -0700170#endif /* _DALVIK_VM_COMPILER */