blob: c66d5621a7bdd2f3f98381cb97c08592bf3e4d59 [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
17#ifndef _DALVIK_VM_COMPILER
18#define _DALVIK_VM_COMPILER
19
20#define CODE_CACHE_SIZE 1024*1024
21#define MAX_JIT_RUN_LEN 64
22#define COMPILER_WORK_QUEUE_SIZE 100
23
24#define COMPILER_TRACED(X)
25#define COMPILER_TRACEE(X)
26#define COMPILER_TRACE_CHAINING(X)
27
Bill Buzbee716f1202009-07-23 13:22:09 -070028typedef enum JitInstructionSetType {
29 DALVIK_JIT_NONE = 0,
30 DALVIK_JIT_ARM,
31 DALVIK_JIT_THUMB,
32 DALVIK_JIT_THUMB2,
Bill Buzbee9bc3df32009-07-30 10:52:29 -070033 DALVIK_JIT_THUMB2EE,
Bill Buzbee716f1202009-07-23 13:22:09 -070034 DALVIK_JIT_X86
35} JitInstructionSetType;
36
37/* Description of a compiled trace. */
38typedef struct JitTranslationInfo {
Ben Chengccd6c012009-10-15 14:52:45 -070039 void *codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -070040 JitInstructionSetType instructionSet;
Ben Cheng60c24f42010-01-04 12:29:56 -080041 bool discardResult; // Used for debugging divergence and IC patching
Bill Buzbee716f1202009-07-23 13:22:09 -070042} JitTranslationInfo;
43
Ben Chengba4fc8b2009-06-01 13:00:29 -070044typedef enum WorkOrderKind {
45 kWorkOrderInvalid = 0, // Should never see by the backend
46 kWorkOrderMethod = 1, // Work is to compile a whole method
47 kWorkOrderTrace = 2, // Work is to compile code fragment(s)
Ben Chengccd6c012009-10-15 14:52:45 -070048 kWorkOrderTraceDebug = 3, // Work is to compile/debug code fragment(s)
Ben Cheng60c24f42010-01-04 12:29:56 -080049 kWorkOrderICPatch = 4, // Work is to patch a polymorphic callsite
Ben Chengba4fc8b2009-06-01 13:00:29 -070050} WorkOrderKind;
51
52typedef struct CompilerWorkOrder {
53 const u2* pc;
54 WorkOrderKind kind;
55 void* info;
Bill Buzbee716f1202009-07-23 13:22:09 -070056 JitTranslationInfo result;
Ben Chengba4fc8b2009-06-01 13:00:29 -070057} CompilerWorkOrder;
58
59typedef enum JitState {
60 kJitOff = 0,
61 kJitNormal = 1, // Profiling in mterp or running native
62 kJitTSelectRequest = 2, // Transition state - start trace selection
63 kJitTSelect = 3, // Actively selecting trace in dbg interp
64 kJitTSelectAbort = 4, // Something threw during selection - abort
65 kJitTSelectEnd = 5, // Done with the trace - wrap it up
66 kJitSingleStep = 6, // Single step interpretation
67 kJitSingleStepEnd = 7, // Done with single step, return to mterp
Jeff Hao97319a82009-08-12 16:57:15 -070068 kJitSelfVerification = 8, // Self Verification Mode
Ben Chengba4fc8b2009-06-01 13:00:29 -070069} JitState;
70
Jeff Hao97319a82009-08-12 16:57:15 -070071#if defined(WITH_SELF_VERIFICATION)
72typedef enum SelfVerificationState {
73 kSVSIdle = 0, // Idle
74 kSVSStart = 1, // Shadow space set up, running compiled code
75 kSVSPunt = 2, // Exiting compiled code by punting
76 kSVSSingleStep = 3, // Exiting compiled code by single stepping
77 kSVSTraceSelect = 4, // Exiting compiled code by trace select
78 kSVSNormal = 5, // Exiting compiled code normally
79 kSVSNoChain = 6, // Exiting compiled code by no chain
80 kSVSBackwardBranch = 7, // Exiting compiled code with backward branch trace
81 kSVSDebugInterp = 8, // Normal state restored, running debug interpreter
82} SelfVerificationState;
83#endif
84
Ben Chengba4fc8b2009-06-01 13:00:29 -070085typedef enum JitHint {
86 kJitHintNone = 0,
87 kJitHintTaken = 1, // Last inst in run was taken branch
88 kJitHintNotTaken = 2, // Last inst in run was not taken branch
89 kJitHintNoBias = 3, // Last inst in run was unbiased branch
90} jitHint;
91
92/*
93 * Element of a Jit trace description. Describes a contiguous
94 * sequence of Dalvik byte codes, the last of which can be
95 * associated with a hint.
96 * Dalvik byte code
97 */
98typedef struct {
99 u2 startOffset; // Starting offset for trace run
100 unsigned numInsts:8; // Number of Byte codes in run
101 unsigned runEnd:1; // Run ends with last byte code
102 jitHint hint:7; // Hint to apply to final code of run
103} JitCodeDesc;
104
105typedef union {
106 JitCodeDesc frag;
107 void* hint;
108} JitTraceRun;
109
110/*
111 * Trace description as will appear in the translation cache. Note
112 * flexible array at end, as these will be of variable size. To
113 * conserve space in the translation cache, total length of JitTraceRun
114 * array must be recomputed via seqential scan if needed.
115 */
116typedef struct {
117 const Method* method;
118 JitTraceRun trace[];
119} JitTraceDescription;
120
Ben Cheng8b258bf2009-06-24 17:27:07 -0700121typedef struct CompilerMethodStats {
122 const Method *method; // Used as hash entry signature
123 int dalvikSize; // # of bytes for dalvik bytecodes
124 int compiledDalvikSize; // # of compiled dalvik bytecodes
125 int nativeSize; // # of bytes for produced native code
126} CompilerMethodStats;
127
Ben Chengba4fc8b2009-06-01 13:00:29 -0700128bool dvmCompilerSetupCodeCache(void);
129bool dvmCompilerArchInit(void);
130void dvmCompilerArchDump(void);
131bool dvmCompilerStartup(void);
132void dvmCompilerShutdown(void);
133bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info);
134void *dvmCheckCodeCache(void *method);
Bill Buzbee716f1202009-07-23 13:22:09 -0700135bool dvmCompileMethod(const Method *method, JitTranslationInfo *info);
136bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts,
137 JitTranslationInfo *info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700138void dvmCompilerDumpStats(void);
139void dvmCompilerDrainQueue(void);
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700140void dvmJitUnchainAll(void);
Bill Buzbee716f1202009-07-23 13:22:09 -0700141void dvmCompilerSortAndPrintTraceProfiles(void);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700142
Ben Cheng4238ec22009-08-24 16:32:22 -0700143struct CompilationUnit;
144struct BasicBlock;
145struct SSARepresentation;
146struct GrowableList;
147
148void dvmInitializeSSAConversion(struct CompilationUnit *cUnit);
149int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg);
150void dvmCompilerLoopOpt(struct CompilationUnit *cUnit);
151void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit);
152void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit,
153 struct BasicBlock *bb);
154void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit,
155 struct BasicBlock *bb);
156void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit,
157 struct BasicBlock *bb);
158void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit,
159 struct BasicBlock *bb);
Ben Chengccd6c012009-10-15 14:52:45 -0700160char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn);
Ben Cheng4238ec22009-08-24 16:32:22 -0700161char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit,
162 struct SSARepresentation *ssaRep);
163void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit,
164 void (*func)(struct CompilationUnit *, struct BasicBlock *));
Ben Chengccd6c012009-10-15 14:52:45 -0700165JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc);
Ben Cheng4238ec22009-08-24 16:32:22 -0700166
Ben Chengba4fc8b2009-06-01 13:00:29 -0700167#endif /* _DALVIK_VM_COMPILER */