| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 17 | #include <Thread.h> |
| 18 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 19 | #ifndef _DALVIK_VM_COMPILER |
| 20 | #define _DALVIK_VM_COMPILER |
| 21 | |
| 22 | #define CODE_CACHE_SIZE 1024*1024 |
| 23 | #define MAX_JIT_RUN_LEN 64 |
| 24 | #define COMPILER_WORK_QUEUE_SIZE 100 |
| Ben Cheng | c3b92b2 | 2010-01-26 16:46:15 -0800 | [diff] [blame^] | 25 | #define COMPILER_IC_PATCH_QUEUE_SIZE 64 |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 26 | |
| 27 | #define COMPILER_TRACED(X) |
| 28 | #define COMPILER_TRACEE(X) |
| 29 | #define COMPILER_TRACE_CHAINING(X) |
| 30 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 31 | typedef enum JitInstructionSetType { |
| 32 | DALVIK_JIT_NONE = 0, |
| 33 | DALVIK_JIT_ARM, |
| 34 | DALVIK_JIT_THUMB, |
| 35 | DALVIK_JIT_THUMB2, |
| Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 36 | DALVIK_JIT_THUMB2EE, |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 37 | DALVIK_JIT_X86 |
| 38 | } JitInstructionSetType; |
| 39 | |
| 40 | /* Description of a compiled trace. */ |
| 41 | typedef struct JitTranslationInfo { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 42 | void *codeAddress; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 43 | JitInstructionSetType instructionSet; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 44 | bool discardResult; // Used for debugging divergence and IC patching |
| Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 45 | Thread *requestingThread; // For debugging purpose |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 46 | } JitTranslationInfo; |
| 47 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 48 | typedef 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 Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 52 | kWorkOrderTraceDebug = 3, // Work is to compile/debug code fragment(s) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 53 | } WorkOrderKind; |
| 54 | |
| 55 | typedef struct CompilerWorkOrder { |
| 56 | const u2* pc; |
| 57 | WorkOrderKind kind; |
| 58 | void* info; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 59 | JitTranslationInfo result; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 60 | } CompilerWorkOrder; |
| 61 | |
| Ben Cheng | c3b92b2 | 2010-01-26 16:46:15 -0800 | [diff] [blame^] | 62 | /* Chain cell for predicted method invocation */ |
| 63 | typedef struct PredictedChainingCell { |
| 64 | u4 branch; /* Branch to chained destination */ |
| 65 | const ClassObject *clazz; /* key #1 for prediction */ |
| 66 | const Method *method; /* key #2 to lookup native PC from dalvik PC */ |
| 67 | u4 counter; /* counter to patch the chaining cell */ |
| 68 | } PredictedChainingCell; |
| 69 | |
| 70 | /* Work order for inline cache patching */ |
| 71 | typedef struct ICPatchWorkOrder { |
| 72 | PredictedChainingCell *cellAddr; /* Address to be patched */ |
| 73 | PredictedChainingCell cellContent; /* content of the new cell */ |
| 74 | } ICPatchWorkOrder; |
| 75 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 76 | typedef enum JitState { |
| 77 | kJitOff = 0, |
| 78 | kJitNormal = 1, // Profiling in mterp or running native |
| 79 | kJitTSelectRequest = 2, // Transition state - start trace selection |
| 80 | kJitTSelect = 3, // Actively selecting trace in dbg interp |
| 81 | kJitTSelectAbort = 4, // Something threw during selection - abort |
| 82 | kJitTSelectEnd = 5, // Done with the trace - wrap it up |
| 83 | kJitSingleStep = 6, // Single step interpretation |
| 84 | kJitSingleStepEnd = 7, // Done with single step, return to mterp |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 85 | kJitSelfVerification = 8, // Self Verification Mode |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 86 | } JitState; |
| 87 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 88 | #if defined(WITH_SELF_VERIFICATION) |
| 89 | typedef enum SelfVerificationState { |
| 90 | kSVSIdle = 0, // Idle |
| 91 | kSVSStart = 1, // Shadow space set up, running compiled code |
| 92 | kSVSPunt = 2, // Exiting compiled code by punting |
| 93 | kSVSSingleStep = 3, // Exiting compiled code by single stepping |
| 94 | kSVSTraceSelect = 4, // Exiting compiled code by trace select |
| 95 | kSVSNormal = 5, // Exiting compiled code normally |
| 96 | kSVSNoChain = 6, // Exiting compiled code by no chain |
| 97 | kSVSBackwardBranch = 7, // Exiting compiled code with backward branch trace |
| 98 | kSVSDebugInterp = 8, // Normal state restored, running debug interpreter |
| 99 | } SelfVerificationState; |
| 100 | #endif |
| 101 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 102 | typedef enum JitHint { |
| 103 | kJitHintNone = 0, |
| 104 | kJitHintTaken = 1, // Last inst in run was taken branch |
| 105 | kJitHintNotTaken = 2, // Last inst in run was not taken branch |
| 106 | kJitHintNoBias = 3, // Last inst in run was unbiased branch |
| 107 | } jitHint; |
| 108 | |
| 109 | /* |
| 110 | * Element of a Jit trace description. Describes a contiguous |
| 111 | * sequence of Dalvik byte codes, the last of which can be |
| 112 | * associated with a hint. |
| 113 | * Dalvik byte code |
| 114 | */ |
| 115 | typedef struct { |
| 116 | u2 startOffset; // Starting offset for trace run |
| 117 | unsigned numInsts:8; // Number of Byte codes in run |
| 118 | unsigned runEnd:1; // Run ends with last byte code |
| 119 | jitHint hint:7; // Hint to apply to final code of run |
| 120 | } JitCodeDesc; |
| 121 | |
| 122 | typedef union { |
| 123 | JitCodeDesc frag; |
| 124 | void* hint; |
| 125 | } JitTraceRun; |
| 126 | |
| 127 | /* |
| 128 | * Trace description as will appear in the translation cache. Note |
| 129 | * flexible array at end, as these will be of variable size. To |
| 130 | * conserve space in the translation cache, total length of JitTraceRun |
| 131 | * array must be recomputed via seqential scan if needed. |
| 132 | */ |
| 133 | typedef struct { |
| 134 | const Method* method; |
| 135 | JitTraceRun trace[]; |
| 136 | } JitTraceDescription; |
| 137 | |
| Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 138 | typedef struct CompilerMethodStats { |
| 139 | const Method *method; // Used as hash entry signature |
| 140 | int dalvikSize; // # of bytes for dalvik bytecodes |
| 141 | int compiledDalvikSize; // # of compiled dalvik bytecodes |
| 142 | int nativeSize; // # of bytes for produced native code |
| 143 | } CompilerMethodStats; |
| 144 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 145 | bool dvmCompilerSetupCodeCache(void); |
| 146 | bool dvmCompilerArchInit(void); |
| 147 | void dvmCompilerArchDump(void); |
| 148 | bool dvmCompilerStartup(void); |
| 149 | void dvmCompilerShutdown(void); |
| 150 | bool dvmCompilerWorkEnqueue(const u2* pc, WorkOrderKind kind, void* info); |
| 151 | void *dvmCheckCodeCache(void *method); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 152 | bool dvmCompileMethod(const Method *method, JitTranslationInfo *info); |
| 153 | bool dvmCompileTrace(JitTraceDescription *trace, int numMaxInsts, |
| 154 | JitTranslationInfo *info); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 155 | void dvmCompilerDumpStats(void); |
| 156 | void dvmCompilerDrainQueue(void); |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 157 | void dvmJitUnchainAll(void); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 158 | void dvmCompilerSortAndPrintTraceProfiles(void); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 159 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 160 | struct CompilationUnit; |
| 161 | struct BasicBlock; |
| 162 | struct SSARepresentation; |
| 163 | struct GrowableList; |
| 164 | |
| 165 | void dvmInitializeSSAConversion(struct CompilationUnit *cUnit); |
| 166 | int dvmConvertSSARegToDalvik(struct CompilationUnit *cUnit, int ssaReg); |
| 167 | void dvmCompilerLoopOpt(struct CompilationUnit *cUnit); |
| 168 | void dvmCompilerNonLoopAnalysis(struct CompilationUnit *cUnit); |
| 169 | void dvmCompilerFindLiveIn(struct CompilationUnit *cUnit, |
| 170 | struct BasicBlock *bb); |
| 171 | void dvmCompilerDoSSAConversion(struct CompilationUnit *cUnit, |
| 172 | struct BasicBlock *bb); |
| 173 | void dvmCompilerDoConstantPropagation(struct CompilationUnit *cUnit, |
| 174 | struct BasicBlock *bb); |
| 175 | void dvmCompilerFindInductionVariables(struct CompilationUnit *cUnit, |
| 176 | struct BasicBlock *bb); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 177 | char *dvmCompilerGetDalvikDisassembly(DecodedInstruction *insn); |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 178 | char *dvmCompilerGetSSAString(struct CompilationUnit *cUnit, |
| 179 | struct SSARepresentation *ssaRep); |
| 180 | void dvmCompilerDataFlowAnalysisDispatcher(struct CompilationUnit *cUnit, |
| 181 | void (*func)(struct CompilationUnit *, struct BasicBlock *)); |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 182 | void dvmCompilerStateRefresh(void); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 183 | JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc); |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 184 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 185 | #endif /* _DALVIK_VM_COMPILER */ |