blob: 06367a6ecdc4de3c23ef1c7074f92a1c65d1df1e [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 Chengccd6c012009-10-15 14:52:45 -070041 bool discardResult; // Used for debugging divergence
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 Chengba4fc8b2009-06-01 13:00:29 -070049} WorkOrderKind;
50
51typedef struct CompilerWorkOrder {
52 const u2* pc;
53 WorkOrderKind kind;
54 void* info;
Bill Buzbee716f1202009-07-23 13:22:09 -070055 JitTranslationInfo result;
Ben Chengba4fc8b2009-06-01 13:00:29 -070056} CompilerWorkOrder;
57
58typedef enum JitState {
59 kJitOff = 0,
60 kJitNormal = 1, // Profiling in mterp or running native
61 kJitTSelectRequest = 2, // Transition state - start trace selection
62 kJitTSelect = 3, // Actively selecting trace in dbg interp
63 kJitTSelectAbort = 4, // Something threw during selection - abort
64 kJitTSelectEnd = 5, // Done with the trace - wrap it up
65 kJitSingleStep = 6, // Single step interpretation
66 kJitSingleStepEnd = 7, // Done with single step, return to mterp
Jeff Hao97319a82009-08-12 16:57:15 -070067 kJitSelfVerification = 8, // Self Verification Mode
Ben Chengba4fc8b2009-06-01 13:00:29 -070068} JitState;
69
Jeff Hao97319a82009-08-12 16:57:15 -070070#if defined(WITH_SELF_VERIFICATION)
71typedef enum SelfVerificationState {
72 kSVSIdle = 0, // Idle
73 kSVSStart = 1, // Shadow space set up, running compiled code
74 kSVSPunt = 2, // Exiting compiled code by punting
75 kSVSSingleStep = 3, // Exiting compiled code by single stepping
76 kSVSTraceSelect = 4, // Exiting compiled code by trace select
77 kSVSNormal = 5, // Exiting compiled code normally
78 kSVSNoChain = 6, // Exiting compiled code by no chain
79 kSVSBackwardBranch = 7, // Exiting compiled code with backward branch trace
80 kSVSDebugInterp = 8, // Normal state restored, running debug interpreter
81} SelfVerificationState;
82#endif
83
Ben Chengba4fc8b2009-06-01 13:00:29 -070084typedef enum JitHint {
85 kJitHintNone = 0,
86 kJitHintTaken = 1, // Last inst in run was taken branch
87 kJitHintNotTaken = 2, // Last inst in run was not taken branch
88 kJitHintNoBias = 3, // Last inst in run was unbiased branch
89} jitHint;
90
91/*
92 * Element of a Jit trace description. Describes a contiguous
93 * sequence of Dalvik byte codes, the last of which can be
94 * associated with a hint.
95 * Dalvik byte code
96 */
97typedef struct {
98 u2 startOffset; // Starting offset for trace run
99 unsigned numInsts:8; // Number of Byte codes in run
100 unsigned runEnd:1; // Run ends with last byte code
101 jitHint hint:7; // Hint to apply to final code of run
102} JitCodeDesc;
103
104typedef union {
105 JitCodeDesc frag;
106 void* hint;
107} JitTraceRun;
108
109/*
110 * Trace description as will appear in the translation cache. Note
111 * flexible array at end, as these will be of variable size. To
112 * conserve space in the translation cache, total length of JitTraceRun
113 * array must be recomputed via seqential scan if needed.
114 */
115typedef struct {
116 const Method* method;
117 JitTraceRun trace[];
118} JitTraceDescription;
119
Ben Cheng8b258bf2009-06-24 17:27:07 -0700120typedef struct CompilerMethodStats {
121 const Method *method; // Used as hash entry signature
122 int dalvikSize; // # of bytes for dalvik bytecodes
123 int compiledDalvikSize; // # of compiled dalvik bytecodes
124 int nativeSize; // # of bytes for produced native code
125} CompilerMethodStats;
126
Ben Chengba4fc8b2009-06-01 13:00:29 -0700127bool dvmCompilerSetupCodeCache(void);
128bool dvmCompilerArchInit(void);
129void dvmCompilerArchDump(void);
130bool dvmCompilerStartup(void);
131void dvmCompilerShutdown(void);
132bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info);
133void *dvmCheckCodeCache(void *method);
Bill Buzbee716f1202009-07-23 13:22:09 -0700134bool dvmCompileMethod(const Method *method, JitTranslationInfo *info);
135bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts,
136 JitTranslationInfo *info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700137void dvmCompilerDumpStats(void);
138void dvmCompilerDrainQueue(void);
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700139void dvmJitUnchainAll(void);
Bill Buzbee716f1202009-07-23 13:22:09 -0700140void dvmCompilerSortAndPrintTraceProfiles(void);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700141
Ben Cheng4238ec22009-08-24 16:32:22 -0700142struct CompilationUnit;
143struct BasicBlock;
144struct SSARepresentation;
145struct GrowableList;
146
147void dvmInitializeSSAConversion(struct CompilationUnit *cUnit);
148int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg);
149void dvmCompilerLoopOpt(struct CompilationUnit *cUnit);
150void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit);
151void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit,
152 struct BasicBlock *bb);
153void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit,
154 struct BasicBlock *bb);
155void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit,
156 struct BasicBlock *bb);
157void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit,
158 struct BasicBlock *bb);
Ben Chengccd6c012009-10-15 14:52:45 -0700159char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn);
Ben Cheng4238ec22009-08-24 16:32:22 -0700160char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit,
161 struct SSARepresentation *ssaRep);
162void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit,
163 void (*func)(struct CompilationUnit *, struct BasicBlock *));
Ben Chengccd6c012009-10-15 14:52:45 -0700164JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc);
Ben Cheng4238ec22009-08-24 16:32:22 -0700165
Ben Chengba4fc8b2009-06-01 13:00:29 -0700166#endif /* _DALVIK_VM_COMPILER */