blob: 1854df54645d96369082ae14c410782cef2fa364 [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>
18
Ben Chengba4fc8b2009-06-01 13:00:29 -070019#ifndef _DALVIK_VM_COMPILER
20#define _DALVIK_VM_COMPILER
21
Ben Chengba4fc8b2009-06-01 13:00:29 -070022#define MAX_JIT_RUN_LEN 64
23#define COMPILER_WORK_QUEUE_SIZE 100
Ben Chengc3b92b22010-01-26 16:46:15 -080024#define COMPILER_IC_PATCH_QUEUE_SIZE 64
Ben Chengba4fc8b2009-06-01 13:00:29 -070025
26#define COMPILER_TRACED(X)
27#define COMPILER_TRACEE(X)
28#define COMPILER_TRACE_CHAINING(X)
29
Bill Buzbee716f1202009-07-23 13:22:09 -070030typedef enum JitInstructionSetType {
31 DALVIK_JIT_NONE = 0,
32 DALVIK_JIT_ARM,
33 DALVIK_JIT_THUMB,
34 DALVIK_JIT_THUMB2,
Bill Buzbee9bc3df32009-07-30 10:52:29 -070035 DALVIK_JIT_THUMB2EE,
Bill Buzbee716f1202009-07-23 13:22:09 -070036 DALVIK_JIT_X86
37} JitInstructionSetType;
38
39/* Description of a compiled trace. */
40typedef struct JitTranslationInfo {
Ben Chengccd6c012009-10-15 14:52:45 -070041 void *codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -070042 JitInstructionSetType instructionSet;
Ben Cheng60c24f42010-01-04 12:29:56 -080043 bool discardResult; // Used for debugging divergence and IC patching
Ben Cheng33672452010-01-12 14:59:30 -080044 Thread *requestingThread; // For debugging purpose
Bill Buzbee716f1202009-07-23 13:22:09 -070045} JitTranslationInfo;
46
Ben Chengba4fc8b2009-06-01 13:00:29 -070047typedef 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)
Ben Chengccd6c012009-10-15 14:52:45 -070051 kWorkOrderTraceDebug = 3, // Work is to compile/debug code fragment(s)
Ben Chengba4fc8b2009-06-01 13:00:29 -070052} WorkOrderKind;
53
54typedef struct CompilerWorkOrder {
55 const u2* pc;
56 WorkOrderKind kind;
57 void* info;
Bill Buzbee716f1202009-07-23 13:22:09 -070058 JitTranslationInfo result;
Ben Chengba4fc8b2009-06-01 13:00:29 -070059} CompilerWorkOrder;
60
Ben Chengc3b92b22010-01-26 16:46:15 -080061/* Chain cell for predicted method invocation */
62typedef 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;
68
69/* Work order for inline cache patching */
70typedef struct ICPatchWorkOrder {
71 PredictedChainingCell *cellAddr; /* Address to be patched */
72 PredictedChainingCell cellContent; /* content of the new cell */
73} ICPatchWorkOrder;
74
Ben Chengba4fc8b2009-06-01 13:00:29 -070075typedef enum JitState {
76 kJitOff = 0,
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
Jeff Hao97319a82009-08-12 16:57:15 -070084 kJitSelfVerification = 8, // Self Verification Mode
Ben Chengba4fc8b2009-06-01 13:00:29 -070085} JitState;
86
Jeff Hao97319a82009-08-12 16:57:15 -070087#if defined(WITH_SELF_VERIFICATION)
88typedef enum SelfVerificationState {
89 kSVSIdle = 0, // Idle
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;
99#endif
100
Ben Chengba4fc8b2009-06-01 13:00:29 -0700101typedef enum JitHint {
102 kJitHintNone = 0,
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
106} jitHint;
107
108/*
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.
112 * Dalvik byte code
113 */
114typedef struct {
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
119} JitCodeDesc;
120
121typedef union {
122 JitCodeDesc frag;
123 void* hint;
124} JitTraceRun;
125
126/*
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.
131 */
132typedef struct {
133 const Method* method;
134 JitTraceRun trace[];
135} JitTraceDescription;
136
Ben Cheng8b258bf2009-06-24 17:27:07 -0700137typedef 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;
143
Ben Chengba4fc8b2009-06-01 13:00:29 -0700144bool dvmCompilerSetupCodeCache(void);
145bool dvmCompilerArchInit(void);
146void dvmCompilerArchDump(void);
147bool dvmCompilerStartup(void);
148void dvmCompilerShutdown(void);
149bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info);
150void *dvmCheckCodeCache(void *method);
Bill Buzbee716f1202009-07-23 13:22:09 -0700151bool dvmCompileMethod(const Method *method, JitTranslationInfo *info);
152bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts,
153 JitTranslationInfo *info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700154void dvmCompilerDumpStats(void);
155void dvmCompilerDrainQueue(void);
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700156void dvmJitUnchainAll(void);
Bill Buzbee716f1202009-07-23 13:22:09 -0700157void dvmCompilerSortAndPrintTraceProfiles(void);
Carl Shapiro3f349af2010-02-25 15:47:08 -0800158void dvmCompilerPerformSafePointChecks(void);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700159
Ben Cheng4238ec22009-08-24 16:32:22 -0700160struct CompilationUnit;
161struct BasicBlock;
162struct SSARepresentation;
163struct GrowableList;
164
165void dvmInitializeSSAConversion(struct CompilationUnit *cUnit);
166int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg);
167void dvmCompilerLoopOpt(struct CompilationUnit *cUnit);
168void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit);
169void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit,
170 struct BasicBlock *bb);
171void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit,
172 struct BasicBlock *bb);
173void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit,
174 struct BasicBlock *bb);
175void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit,
176 struct BasicBlock *bb);
Ben Chengccd6c012009-10-15 14:52:45 -0700177char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn);
Ben Cheng4238ec22009-08-24 16:32:22 -0700178char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit,
179 struct SSARepresentation *ssaRep);
180void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit,
181 void (*func)(struct CompilationUnit *, struct BasicBlock *));
Bill Buzbee06bb8392010-01-31 18:53:15 -0800182void dvmCompilerStateRefresh(void);
Ben Chengccd6c012009-10-15 14:52:45 -0700183JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc);
Ben Cheng4238ec22009-08-24 16:32:22 -0700184
Ben Chengba4fc8b2009-06-01 13:00:29 -0700185#endif /* _DALVIK_VM_COMPILER */