blob: 21c86abb8a45eedc9c8ef5fdccfa7c37b6ec2d10 [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 Chengba4fc8b2009-06-01 13:00:29 -070023#define MAX_JIT_RUN_LEN 64
24#define COMPILER_WORK_QUEUE_SIZE 100
Ben Cheng6999d842010-01-26 16:46:15 -080025#define COMPILER_IC_PATCH_QUEUE_SIZE 64
Ben Chengba4fc8b2009-06-01 13:00:29 -070026
27#define COMPILER_TRACED(X)
28#define COMPILER_TRACEE(X)
29#define COMPILER_TRACE_CHAINING(X)
30
Bill Buzbee716f1202009-07-23 13:22:09 -070031typedef enum JitInstructionSetType {
32 DALVIK_JIT_NONE = 0,
33 DALVIK_JIT_ARM,
34 DALVIK_JIT_THUMB,
35 DALVIK_JIT_THUMB2,
Bill Buzbee9bc3df32009-07-30 10:52:29 -070036 DALVIK_JIT_THUMB2EE,
Bill Buzbee716f1202009-07-23 13:22:09 -070037 DALVIK_JIT_X86
38} JitInstructionSetType;
39
40/* Description of a compiled trace. */
41typedef struct JitTranslationInfo {
Ben Chengccd6c012009-10-15 14:52:45 -070042 void *codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -070043 JitInstructionSetType instructionSet;
Ben Cheng60c24f42010-01-04 12:29:56 -080044 bool discardResult; // Used for debugging divergence and IC patching
Ben Cheng33672452010-01-12 14:59:30 -080045 Thread *requestingThread; // For debugging purpose
Bill Buzbee716f1202009-07-23 13:22:09 -070046} JitTranslationInfo;
47
Ben Chengba4fc8b2009-06-01 13:00:29 -070048typedef enum WorkOrderKind {
49 kWorkOrderInvalid = 0, // Should never see by the backend
50 kWorkOrderMethod = 1, // Work is to compile a whole method
51 kWorkOrderTrace = 2, // Work is to compile code fragment(s)
Ben Chengccd6c012009-10-15 14:52:45 -070052 kWorkOrderTraceDebug = 3, // Work is to compile/debug code fragment(s)
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;
Bill Buzbeefc519dc2010-03-06 23:30:57 -080060 jmp_buf *bailPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -070061} CompilerWorkOrder;
62
Ben Cheng6999d842010-01-26 16:46:15 -080063/* Chain cell for predicted method invocation */
64typedef struct PredictedChainingCell {
65 u4 branch; /* Branch to chained destination */
66 const ClassObject *clazz; /* key #1 for prediction */
67 const Method *method; /* key #2 to lookup native PC from dalvik PC */
68 u4 counter; /* counter to patch the chaining cell */
69} PredictedChainingCell;
70
71/* Work order for inline cache patching */
72typedef struct ICPatchWorkOrder {
73 PredictedChainingCell *cellAddr; /* Address to be patched */
74 PredictedChainingCell cellContent; /* content of the new cell */
75} ICPatchWorkOrder;
76
Ben Chengba4fc8b2009-06-01 13:00:29 -070077typedef enum JitState {
78 kJitOff = 0,
79 kJitNormal = 1, // Profiling in mterp or running native
80 kJitTSelectRequest = 2, // Transition state - start trace selection
Ben Cheng40094c12010-02-24 20:58:44 -080081 kJitTSelectRequestHot = 3, // Transition state - start hot trace selection
82 kJitTSelect = 4, // Actively selecting trace in dbg interp
83 kJitTSelectAbort = 5, // Something threw during selection - abort
84 kJitTSelectEnd = 6, // Done with the trace - wrap it up
85 kJitSingleStep = 7, // Single step interpretation
86 kJitSingleStepEnd = 8, // Done with single step, return to mterp
87 kJitSelfVerification = 9, // Self Verification Mode
Ben Chengba4fc8b2009-06-01 13:00:29 -070088} JitState;
89
Jeff Hao97319a82009-08-12 16:57:15 -070090#if defined(WITH_SELF_VERIFICATION)
91typedef enum SelfVerificationState {
92 kSVSIdle = 0, // Idle
93 kSVSStart = 1, // Shadow space set up, running compiled code
94 kSVSPunt = 2, // Exiting compiled code by punting
95 kSVSSingleStep = 3, // Exiting compiled code by single stepping
Ben Cheng40094c12010-02-24 20:58:44 -080096 kSVSTraceSelectNoChain = 4,// Exiting compiled code by trace select no chain
97 kSVSTraceSelect = 5, // Exiting compiled code by trace select
98 kSVSNormal = 6, // Exiting compiled code normally
99 kSVSNoChain = 7, // Exiting compiled code by no chain
100 kSVSBackwardBranch = 8, // Exiting compiled code with backward branch trace
101 kSVSDebugInterp = 9, // Normal state restored, running debug interpreter
Jeff Hao97319a82009-08-12 16:57:15 -0700102} SelfVerificationState;
103#endif
104
Ben Chengba4fc8b2009-06-01 13:00:29 -0700105typedef enum JitHint {
106 kJitHintNone = 0,
107 kJitHintTaken = 1, // Last inst in run was taken branch
108 kJitHintNotTaken = 2, // Last inst in run was not taken branch
109 kJitHintNoBias = 3, // Last inst in run was unbiased branch
110} jitHint;
111
112/*
113 * Element of a Jit trace description. Describes a contiguous
114 * sequence of Dalvik byte codes, the last of which can be
115 * associated with a hint.
116 * Dalvik byte code
117 */
118typedef struct {
119 u2 startOffset; // Starting offset for trace run
120 unsigned numInsts:8; // Number of Byte codes in run
121 unsigned runEnd:1; // Run ends with last byte code
122 jitHint hint:7; // Hint to apply to final code of run
123} JitCodeDesc;
124
125typedef union {
126 JitCodeDesc frag;
127 void* hint;
128} JitTraceRun;
129
130/*
131 * Trace description as will appear in the translation cache. Note
132 * flexible array at end, as these will be of variable size. To
133 * conserve space in the translation cache, total length of JitTraceRun
134 * array must be recomputed via seqential scan if needed.
135 */
136typedef struct {
137 const Method* method;
138 JitTraceRun trace[];
139} JitTraceDescription;
140
Ben Cheng8b258bf2009-06-24 17:27:07 -0700141typedef struct CompilerMethodStats {
142 const Method *method; // Used as hash entry signature
143 int dalvikSize; // # of bytes for dalvik bytecodes
144 int compiledDalvikSize; // # of compiled dalvik bytecodes
145 int nativeSize; // # of bytes for produced native code
146} CompilerMethodStats;
147
Ben Chengba4fc8b2009-06-01 13:00:29 -0700148bool dvmCompilerSetupCodeCache(void);
149bool dvmCompilerArchInit(void);
150void dvmCompilerArchDump(void);
151bool dvmCompilerStartup(void);
152void dvmCompilerShutdown(void);
153bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info);
154void *dvmCheckCodeCache(void *method);
Bill Buzbee716f1202009-07-23 13:22:09 -0700155bool dvmCompileMethod(const Method *method, JitTranslationInfo *info);
156bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts,
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800157 JitTranslationInfo *info, jmp_buf *bailPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700158void dvmCompilerDumpStats(void);
159void dvmCompilerDrainQueue(void);
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700160void dvmJitUnchainAll(void);
Bill Buzbee716f1202009-07-23 13:22:09 -0700161void dvmCompilerSortAndPrintTraceProfiles(void);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700162
Ben Cheng4238ec22009-08-24 16:32:22 -0700163struct CompilationUnit;
164struct BasicBlock;
165struct SSARepresentation;
166struct GrowableList;
Ben Cheng88a0f972010-02-24 15:00:40 -0800167struct JitEntry;
Ben Cheng4238ec22009-08-24 16:32:22 -0700168
169void dvmInitializeSSAConversion(struct CompilationUnit *cUnit);
170int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg);
171void dvmCompilerLoopOpt(struct CompilationUnit *cUnit);
172void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit);
173void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit,
174 struct BasicBlock *bb);
175void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit,
176 struct BasicBlock *bb);
177void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit,
178 struct BasicBlock *bb);
179void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit,
180 struct BasicBlock *bb);
Ben Chengccd6c012009-10-15 14:52:45 -0700181char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn);
Ben Cheng4238ec22009-08-24 16:32:22 -0700182char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit,
183 struct SSARepresentation *ssaRep);
184void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit,
185 void (*func)(struct CompilationUnit *, struct BasicBlock *));
Bill Buzbee06bb8392010-01-31 18:53:15 -0800186void dvmCompilerStateRefresh(void);
Ben Cheng88a0f972010-02-24 15:00:40 -0800187JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc,
188 const struct JitEntry *desc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700189#endif /* _DALVIK_VM_COMPILER */