blob: 5b667355da5b28a329028dc48717b62cd05b074c [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>
Bill Buzbeefc519dc2010-03-06 23:30:57 -080018#include <setjmp.h>
Ben Cheng33672452010-01-12 14:59:30 -080019
Ben Chengba4fc8b2009-06-01 13:00:29 -070020#ifndef _DALVIK_VM_COMPILER
21#define _DALVIK_VM_COMPILER
22
Ben Chengdca71432010-03-16 16:04:11 -070023/*
24 * Uncomment the following to enable JIT signature breakpoint
25 * #define SIGNATURE_BREAKPOINT
26 */
27
Ben Chengba4fc8b2009-06-01 13:00:29 -070028#define MAX_JIT_RUN_LEN 64
29#define COMPILER_WORK_QUEUE_SIZE 100
Ben Cheng6999d842010-01-26 16:46:15 -080030#define COMPILER_IC_PATCH_QUEUE_SIZE 64
Ben Chengba4fc8b2009-06-01 13:00:29 -070031
32#define COMPILER_TRACED(X)
33#define COMPILER_TRACEE(X)
34#define COMPILER_TRACE_CHAINING(X)
35
Bill Buzbee716f1202009-07-23 13:22:09 -070036typedef enum JitInstructionSetType {
37 DALVIK_JIT_NONE = 0,
38 DALVIK_JIT_ARM,
39 DALVIK_JIT_THUMB,
40 DALVIK_JIT_THUMB2,
Bill Buzbee9bc3df32009-07-30 10:52:29 -070041 DALVIK_JIT_THUMB2EE,
Bill Buzbee716f1202009-07-23 13:22:09 -070042 DALVIK_JIT_X86
43} JitInstructionSetType;
44
45/* Description of a compiled trace. */
46typedef struct JitTranslationInfo {
Ben Chengccd6c012009-10-15 14:52:45 -070047 void *codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -070048 JitInstructionSetType instructionSet;
Ben Cheng60c24f42010-01-04 12:29:56 -080049 bool discardResult; // Used for debugging divergence and IC patching
Ben Cheng33672452010-01-12 14:59:30 -080050 Thread *requestingThread; // For debugging purpose
Bill Buzbee716f1202009-07-23 13:22:09 -070051} JitTranslationInfo;
52
Ben Chengba4fc8b2009-06-01 13:00:29 -070053typedef enum WorkOrderKind {
54 kWorkOrderInvalid = 0, // Should never see by the backend
55 kWorkOrderMethod = 1, // Work is to compile a whole method
56 kWorkOrderTrace = 2, // Work is to compile code fragment(s)
Ben Chengccd6c012009-10-15 14:52:45 -070057 kWorkOrderTraceDebug = 3, // Work is to compile/debug code fragment(s)
Ben Chengba4fc8b2009-06-01 13:00:29 -070058} WorkOrderKind;
59
60typedef struct CompilerWorkOrder {
61 const u2* pc;
62 WorkOrderKind kind;
63 void* info;
Bill Buzbee716f1202009-07-23 13:22:09 -070064 JitTranslationInfo result;
Bill Buzbeefc519dc2010-03-06 23:30:57 -080065 jmp_buf *bailPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -070066} CompilerWorkOrder;
67
Ben Cheng6999d842010-01-26 16:46:15 -080068/* Chain cell for predicted method invocation */
69typedef struct PredictedChainingCell {
70 u4 branch; /* Branch to chained destination */
71 const ClassObject *clazz; /* key #1 for prediction */
72 const Method *method; /* key #2 to lookup native PC from dalvik PC */
73 u4 counter; /* counter to patch the chaining cell */
74} PredictedChainingCell;
75
76/* Work order for inline cache patching */
77typedef struct ICPatchWorkOrder {
78 PredictedChainingCell *cellAddr; /* Address to be patched */
79 PredictedChainingCell cellContent; /* content of the new cell */
80} ICPatchWorkOrder;
81
Ben Chengba4fc8b2009-06-01 13:00:29 -070082typedef enum JitState {
83 kJitOff = 0,
84 kJitNormal = 1, // Profiling in mterp or running native
85 kJitTSelectRequest = 2, // Transition state - start trace selection
Ben Cheng40094c12010-02-24 20:58:44 -080086 kJitTSelectRequestHot = 3, // Transition state - start hot trace selection
87 kJitTSelect = 4, // Actively selecting trace in dbg interp
88 kJitTSelectAbort = 5, // Something threw during selection - abort
89 kJitTSelectEnd = 6, // Done with the trace - wrap it up
90 kJitSingleStep = 7, // Single step interpretation
91 kJitSingleStepEnd = 8, // Done with single step, return to mterp
92 kJitSelfVerification = 9, // Self Verification Mode
Ben Chengba4fc8b2009-06-01 13:00:29 -070093} JitState;
94
Jeff Hao97319a82009-08-12 16:57:15 -070095#if defined(WITH_SELF_VERIFICATION)
96typedef enum SelfVerificationState {
97 kSVSIdle = 0, // Idle
98 kSVSStart = 1, // Shadow space set up, running compiled code
99 kSVSPunt = 2, // Exiting compiled code by punting
100 kSVSSingleStep = 3, // Exiting compiled code by single stepping
Ben Cheng40094c12010-02-24 20:58:44 -0800101 kSVSTraceSelectNoChain = 4,// Exiting compiled code by trace select no chain
102 kSVSTraceSelect = 5, // Exiting compiled code by trace select
103 kSVSNormal = 6, // Exiting compiled code normally
104 kSVSNoChain = 7, // Exiting compiled code by no chain
105 kSVSBackwardBranch = 8, // Exiting compiled code with backward branch trace
106 kSVSDebugInterp = 9, // Normal state restored, running debug interpreter
Jeff Hao97319a82009-08-12 16:57:15 -0700107} SelfVerificationState;
108#endif
109
Ben Chengba4fc8b2009-06-01 13:00:29 -0700110typedef enum JitHint {
111 kJitHintNone = 0,
112 kJitHintTaken = 1, // Last inst in run was taken branch
113 kJitHintNotTaken = 2, // Last inst in run was not taken branch
114 kJitHintNoBias = 3, // Last inst in run was unbiased branch
115} jitHint;
116
117/*
118 * Element of a Jit trace description. Describes a contiguous
119 * sequence of Dalvik byte codes, the last of which can be
120 * associated with a hint.
121 * Dalvik byte code
122 */
123typedef struct {
124 u2 startOffset; // Starting offset for trace run
125 unsigned numInsts:8; // Number of Byte codes in run
126 unsigned runEnd:1; // Run ends with last byte code
127 jitHint hint:7; // Hint to apply to final code of run
128} JitCodeDesc;
129
130typedef union {
131 JitCodeDesc frag;
132 void* hint;
133} JitTraceRun;
134
135/*
136 * Trace description as will appear in the translation cache. Note
137 * flexible array at end, as these will be of variable size. To
138 * conserve space in the translation cache, total length of JitTraceRun
139 * array must be recomputed via seqential scan if needed.
140 */
141typedef struct {
142 const Method* method;
143 JitTraceRun trace[];
144} JitTraceDescription;
145
Ben Cheng8b258bf2009-06-24 17:27:07 -0700146typedef struct CompilerMethodStats {
147 const Method *method; // Used as hash entry signature
148 int dalvikSize; // # of bytes for dalvik bytecodes
149 int compiledDalvikSize; // # of compiled dalvik bytecodes
150 int nativeSize; // # of bytes for produced native code
151} CompilerMethodStats;
152
Ben Chengba4fc8b2009-06-01 13:00:29 -0700153bool dvmCompilerSetupCodeCache(void);
154bool dvmCompilerArchInit(void);
155void dvmCompilerArchDump(void);
156bool dvmCompilerStartup(void);
157void dvmCompilerShutdown(void);
158bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info);
159void *dvmCheckCodeCache(void *method);
Bill Buzbee716f1202009-07-23 13:22:09 -0700160bool dvmCompileMethod(const Method *method, JitTranslationInfo *info);
161bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts,
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800162 JitTranslationInfo *info, jmp_buf *bailPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700163void dvmCompilerDumpStats(void);
164void dvmCompilerDrainQueue(void);
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700165void dvmJitUnchainAll(void);
Bill Buzbee716f1202009-07-23 13:22:09 -0700166void dvmCompilerSortAndPrintTraceProfiles(void);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700167
Ben Cheng4238ec22009-08-24 16:32:22 -0700168struct CompilationUnit;
169struct BasicBlock;
170struct SSARepresentation;
171struct GrowableList;
Ben Cheng88a0f972010-02-24 15:00:40 -0800172struct JitEntry;
Ben Cheng4238ec22009-08-24 16:32:22 -0700173
174void dvmInitializeSSAConversion(struct CompilationUnit *cUnit);
175int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg);
176void dvmCompilerLoopOpt(struct CompilationUnit *cUnit);
177void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit);
178void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit,
179 struct BasicBlock *bb);
180void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit,
181 struct BasicBlock *bb);
182void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit,
183 struct BasicBlock *bb);
184void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit,
185 struct BasicBlock *bb);
Ben Chengccd6c012009-10-15 14:52:45 -0700186char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn);
Ben Cheng4238ec22009-08-24 16:32:22 -0700187char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit,
188 struct SSARepresentation *ssaRep);
189void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit,
190 void (*func)(struct CompilationUnit *, struct BasicBlock *));
Bill Buzbee06bb8392010-01-31 18:53:15 -0800191void dvmCompilerStateRefresh(void);
Ben Cheng88a0f972010-02-24 15:00:40 -0800192JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc,
193 const struct JitEntry *desc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700194#endif /* _DALVIK_VM_COMPILER */