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