| 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 | |
| 17 | #ifndef _DALVIK_VM_COMPILER_IR |
| 18 | #define _DALVIK_VM_COMPILER_IR |
| 19 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 20 | #include "codegen/Optimizer.h" |
| 21 | |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 22 | typedef enum RegisterClass { |
| 23 | kCoreReg, |
| 24 | kFPReg, |
| 25 | kAnyReg, |
| 26 | } RegisterClass; |
| 27 | |
| 28 | typedef enum RegLocationType { |
| 29 | kLocDalvikFrame = 0, |
| 30 | kLocPhysReg, |
| 31 | kLocRetval, // Return region in interpState |
| 32 | kLocSpill, |
| 33 | } RegLocationType; |
| 34 | |
| 35 | typedef struct RegLocation { |
| 36 | RegLocationType location:2; |
| 37 | unsigned wide:1; |
| 38 | unsigned fp:1; // Hint for float/double |
| 39 | u1 lowReg:6; // First physical register |
| 40 | u1 highReg:6; // 2nd physical register (if wide) |
| 41 | s2 sRegLow; // SSA name for low Dalvik word |
| 42 | } RegLocation; |
| 43 | |
| 44 | #define INVALID_SREG (-1) |
| 45 | #define INVALID_REG (-1) |
| 46 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 47 | typedef enum BBType { |
| 48 | /* For coding convenience reasons chaining cell types should appear first */ |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 49 | kChainingCellNormal = 0, |
| 50 | kChainingCellHot, |
| 51 | kChainingCellInvokeSingleton, |
| 52 | kChainingCellInvokePredicted, |
| 53 | kChainingCellBackwardBranch, |
| Ben Cheng | cec26f6 | 2010-01-15 15:29:33 -0800 | [diff] [blame] | 54 | kChainingCellGap, |
| 55 | /* Don't insert new fields between Gap and Last */ |
| 56 | kChainingCellLast = kChainingCellGap + 1, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 57 | kMethodEntryBlock, |
| 58 | kTraceEntryBlock, |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 59 | kDalvikByteCode, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 60 | kTraceExitBlock, |
| 61 | kMethodExitBlock, |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 62 | kPCReconstruction, |
| 63 | kExceptionHandling, |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 64 | } BBType; |
| 65 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 66 | typedef struct ChainCellCounts { |
| 67 | union { |
| Ben Cheng | cec26f6 | 2010-01-15 15:29:33 -0800 | [diff] [blame] | 68 | u1 count[kChainingCellLast]; /* include one more space for the gap # */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 69 | u4 dummyForAlignment; |
| 70 | } u; |
| 71 | } ChainCellCounts; |
| 72 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 73 | typedef struct LIR { |
| 74 | int offset; |
| 75 | struct LIR *next; |
| 76 | struct LIR *prev; |
| 77 | struct LIR *target; |
| 78 | } LIR; |
| 79 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 80 | enum ExtendedMIROpcode { |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 81 | kMirOpFirst = 256, |
| 82 | kMirOpPhi = kMirOpFirst, |
| 83 | kMirOpNullNRangeUpCheck, |
| 84 | kMirOpNullNRangeDownCheck, |
| 85 | kMirOpLowerBound, |
| 86 | kMirOpPunt, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 87 | kMirOpCheckInlinePrediction, // Gen checks for predicted inlining |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 88 | kMirOpLast, |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct SSARepresentation; |
| 92 | |
| 93 | typedef enum { |
| 94 | kMIRIgnoreNullCheck = 0, |
| 95 | kMIRNullCheckOnly, |
| 96 | kMIRIgnoreRangeCheck, |
| 97 | kMIRRangeCheckOnly, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 98 | kMIRInlined, // Invoke is inlined (ie dead) |
| 99 | kMIRInlinedPred, // Invoke is inlined via prediction |
| 100 | kMIRCallee, // Instruction is inlined from callee |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 101 | } MIROptimizationFlagPositons; |
| 102 | |
| 103 | #define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck) |
| 104 | #define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly) |
| 105 | #define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck) |
| 106 | #define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly) |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 107 | #define MIR_INLINED (1 << kMIRInlined) |
| 108 | #define MIR_INLINED_PRED (1 << kMIRInlinedPred) |
| 109 | #define MIR_CALLEE (1 << kMIRCallee) |
| 110 | |
| 111 | typedef struct CallsiteInfo { |
| 112 | const ClassObject *clazz; |
| 113 | const Method *method; |
| 114 | LIR *misPredBranchOver; |
| 115 | } CallsiteInfo; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 116 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 117 | typedef struct MIR { |
| 118 | DecodedInstruction dalvikInsn; |
| 119 | unsigned int width; |
| 120 | unsigned int offset; |
| 121 | struct MIR *prev; |
| 122 | struct MIR *next; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 123 | struct SSARepresentation *ssaRep; |
| 124 | int OptimizationFlags; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 125 | int seqNum; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 126 | union { |
| 127 | // Used by the inlined insn from the callee to find the mother method |
| 128 | const Method *calleeMethod; |
| 129 | // Used by the inlined invoke to find the class and method pointers |
| 130 | CallsiteInfo *callsiteInfo; |
| 131 | } meta; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 132 | } MIR; |
| 133 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 134 | struct BasicBlockDataFlow; |
| 135 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 136 | typedef struct BasicBlock { |
| 137 | int id; |
| 138 | int visited; |
| 139 | unsigned int startOffset; |
| 140 | const Method *containingMethod; // For blocks from the callee |
| 141 | BBType blockType; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 142 | bool needFallThroughBranch; // For blocks ended due to length limit |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 143 | bool isFallThroughFromInvoke; // True means the block needs alignment |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 144 | MIR *firstMIRInsn; |
| 145 | MIR *lastMIRInsn; |
| 146 | struct BasicBlock *fallThrough; |
| 147 | struct BasicBlock *taken; |
| 148 | struct BasicBlock *next; // Serial link for book keeping purposes |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 149 | struct BasicBlockDataFlow *dataFlowInfo; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 150 | } BasicBlock; |
| 151 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 152 | struct LoopAnalysis; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 153 | struct RegisterPool; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 154 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 155 | typedef struct CompilationUnit { |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 156 | int numInsts; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 157 | int numBlocks; |
| 158 | BasicBlock **blockList; |
| 159 | const Method *method; |
| 160 | const JitTraceDescription *traceDesc; |
| 161 | LIR *firstLIRInsn; |
| 162 | LIR *lastLIRInsn; |
| 163 | LIR *wordList; |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 164 | LIR *chainCellOffsetLIR; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 165 | GrowableList pcReconstructionList; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 166 | int headerSize; // bytes before the first code ptr |
| 167 | int dataOffset; // starting offset of literal pool |
| 168 | int totalSize; // header + code size |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 169 | unsigned char *codeBuffer; |
| 170 | void *baseAddr; |
| 171 | bool printMe; |
| 172 | bool allSingleStep; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 173 | bool halveInstCount; |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 174 | bool executionCount; // Add code to count trace executions |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 175 | bool hasLoop; // Contains a loop |
| 176 | bool hasInvoke; // Contains an invoke instruction |
| jeffhao | 9e45c0b | 2010-02-03 10:24:05 -0800 | [diff] [blame] | 177 | bool heapMemOp; // Mark mem ops for self verification |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 178 | bool wholeMethod; |
| Ben Cheng | cec26f6 | 2010-01-15 15:29:33 -0800 | [diff] [blame] | 179 | int numChainingCells[kChainingCellGap]; |
| 180 | LIR *firstChainingLIR[kChainingCellGap]; |
| 181 | LIR *chainingCellBottom; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 182 | struct RegisterPool *regPool; |
| Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 183 | int optRound; // round number to tell an LIR's age |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 184 | jmp_buf *bailPtr; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 185 | JitInstructionSetType instructionSet; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 186 | /* Number of total regs used in the whole cUnit after SSA transformation */ |
| 187 | int numSSARegs; |
| 188 | /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */ |
| 189 | GrowableList *ssaToDalvikMap; |
| 190 | |
| 191 | /* The following are new data structures to support SSA representations */ |
| 192 | /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */ |
| 193 | int *dalvikToSSAMap; // length == method->registersSize |
| 194 | BitVector *isConstantV; // length == numSSAReg |
| 195 | int *constantValues; // length == numSSAReg |
| 196 | |
| 197 | /* Data structure for loop analysis and optimizations */ |
| 198 | struct LoopAnalysis *loopAnalysis; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 199 | |
| 200 | /* Map SSA names to location */ |
| 201 | RegLocation *regLocation; |
| 202 | int sequenceNumber; |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Set to the Dalvik PC of the switch instruction if it has more than |
| 206 | * MAX_CHAINED_SWITCH_CASES cases. |
| 207 | */ |
| 208 | const u2 *switchOverflowPad; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 209 | } CompilationUnit; |
| 210 | |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 211 | #if defined(WITH_SELF_VERIFICATION) |
| 212 | #define HEAP_ACCESS_SHADOW(_state) cUnit->heapMemOp = _state |
| 213 | #else |
| 214 | #define HEAP_ACCESS_SHADOW(_state) |
| 215 | #endif |
| 216 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 217 | BasicBlock *dvmCompilerNewBB(BBType blockType); |
| 218 | |
| 219 | void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir); |
| 220 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 221 | void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir); |
| 222 | |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame^] | 223 | void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR); |
| 224 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 225 | void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir); |
| 226 | |
| Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 227 | void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR); |
| 228 | |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 229 | void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR); |
| 230 | |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 231 | void dvmCompilerAbort(CompilationUnit *cUnit); |
| 232 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 233 | /* Debug Utilities */ |
| 234 | void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit); |
| 235 | |
| 236 | #endif /* _DALVIK_VM_COMPILER_IR */ |