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