2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef _DALVIK_VM_COMPILER
20 #define _DALVIK_VM_COMPILER
22 #define MAX_JIT_RUN_LEN 64
23 #define COMPILER_WORK_QUEUE_SIZE 100
24 #define COMPILER_IC_PATCH_QUEUE_SIZE 64
26 #define COMPILER_TRACED(X)
27 #define COMPILER_TRACEE(X)
28 #define COMPILER_TRACE_CHAINING(X)
30 typedef enum JitInstructionSetType {
37 } JitInstructionSetType;
39 /* Description of a compiled trace. */
40 typedef struct JitTranslationInfo {
42 JitInstructionSetType instructionSet;
43 bool discardResult; // Used for debugging divergence and IC patching
44 Thread *requestingThread; // For debugging purpose
47 typedef 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)
51 kWorkOrderTraceDebug = 3, // Work is to compile/debug code fragment(s)
54 typedef struct CompilerWorkOrder {
58 JitTranslationInfo result;
61 /* Chain cell for predicted method invocation */
62 typedef struct PredictedChainingCell {
63 u4 branch; /* Branch to chained destination */
64 const ClassObject *clazz; /* key #1 for prediction */
65 const Method *method; /* key #2 to lookup native PC from dalvik PC */
66 u4 counter; /* counter to patch the chaining cell */
67 } PredictedChainingCell;
69 /* Work order for inline cache patching */
70 typedef struct ICPatchWorkOrder {
71 PredictedChainingCell *cellAddr; /* Address to be patched */
72 PredictedChainingCell cellContent; /* content of the new cell */
75 typedef enum JitState {
77 kJitNormal = 1, // Profiling in mterp or running native
78 kJitTSelectRequest = 2, // Transition state - start trace selection
79 kJitTSelect = 3, // Actively selecting trace in dbg interp
80 kJitTSelectAbort = 4, // Something threw during selection - abort
81 kJitTSelectEnd = 5, // Done with the trace - wrap it up
82 kJitSingleStep = 6, // Single step interpretation
83 kJitSingleStepEnd = 7, // Done with single step, return to mterp
84 kJitSelfVerification = 8, // Self Verification Mode
87 #if defined(WITH_SELF_VERIFICATION)
88 typedef enum SelfVerificationState {
90 kSVSStart = 1, // Shadow space set up, running compiled code
91 kSVSPunt = 2, // Exiting compiled code by punting
92 kSVSSingleStep = 3, // Exiting compiled code by single stepping
93 kSVSTraceSelect = 4, // Exiting compiled code by trace select
94 kSVSNormal = 5, // Exiting compiled code normally
95 kSVSNoChain = 6, // Exiting compiled code by no chain
96 kSVSBackwardBranch = 7, // Exiting compiled code with backward branch trace
97 kSVSDebugInterp = 8, // Normal state restored, running debug interpreter
98 } SelfVerificationState;
101 typedef enum JitHint {
103 kJitHintTaken = 1, // Last inst in run was taken branch
104 kJitHintNotTaken = 2, // Last inst in run was not taken branch
105 kJitHintNoBias = 3, // Last inst in run was unbiased branch
109 * Element of a Jit trace description. Describes a contiguous
110 * sequence of Dalvik byte codes, the last of which can be
111 * associated with a hint.
115 u2 startOffset; // Starting offset for trace run
116 unsigned numInsts:8; // Number of Byte codes in run
117 unsigned runEnd:1; // Run ends with last byte code
118 jitHint hint:7; // Hint to apply to final code of run
127 * Trace description as will appear in the translation cache. Note
128 * flexible array at end, as these will be of variable size. To
129 * conserve space in the translation cache, total length of JitTraceRun
130 * array must be recomputed via seqential scan if needed.
133 const Method* method;
135 } JitTraceDescription;
137 typedef struct CompilerMethodStats {
138 const Method *method; // Used as hash entry signature
139 int dalvikSize; // # of bytes for dalvik bytecodes
140 int compiledDalvikSize; // # of compiled dalvik bytecodes
141 int nativeSize; // # of bytes for produced native code
142 } CompilerMethodStats;
144 bool dvmCompilerSetupCodeCache(void);
145 bool dvmCompilerArchInit(void);
146 void dvmCompilerArchDump(void);
147 bool dvmCompilerStartup(void);
148 void dvmCompilerShutdown(void);
149 bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info);
150 void *dvmCheckCodeCache(void *method);
151 bool dvmCompileMethod(const Method *method, JitTranslationInfo *info);
152 bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts,
153 JitTranslationInfo *info);
154 void dvmCompilerDumpStats(void);
155 void dvmCompilerDrainQueue(void);
156 void dvmJitUnchainAll(void);
157 void dvmCompilerSortAndPrintTraceProfiles(void);
158 void dvmCompilerPerformSafePointChecks(void);
160 struct CompilationUnit;
162 struct SSARepresentation;
166 void dvmInitializeSSAConversion(struct CompilationUnit *cUnit);
167 int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg);
168 void dvmCompilerLoopOpt(struct CompilationUnit *cUnit);
169 void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit);
170 void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit,
171 struct BasicBlock *bb);
172 void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit,
173 struct BasicBlock *bb);
174 void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit,
175 struct BasicBlock *bb);
176 void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit,
177 struct BasicBlock *bb);
178 char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn);
179 char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit,
180 struct SSARepresentation *ssaRep);
181 void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit,
182 void (*func)(struct CompilationUnit *, struct BasicBlock *));
183 void dvmCompilerStateRefresh(void);
184 JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc,
185 const struct JitEntry *desc);
187 #endif /* _DALVIK_VM_COMPILER */