| 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 | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 64 | kCatchEntry, |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 65 | } BBType; |
| 66 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 67 | typedef struct ChainCellCounts { |
| 68 | union { |
| Ben Cheng | cec26f6 | 2010-01-15 15:29:33 -0800 | [diff] [blame] | 69 | u1 count[kChainingCellLast]; /* include one more space for the gap # */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 70 | u4 dummyForAlignment; |
| 71 | } u; |
| 72 | } ChainCellCounts; |
| 73 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 74 | typedef struct LIR { |
| 75 | int offset; |
| 76 | struct LIR *next; |
| 77 | struct LIR *prev; |
| 78 | struct LIR *target; |
| 79 | } LIR; |
| 80 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 81 | enum ExtendedMIROpcode { |
| Dan Bornstein | ccaab18 | 2010-12-03 15:32:40 -0800 | [diff] [blame] | 82 | kMirOpFirst = kNumPackedOpcodes, |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 83 | kMirOpPhi = kMirOpFirst, |
| 84 | kMirOpNullNRangeUpCheck, |
| 85 | kMirOpNullNRangeDownCheck, |
| 86 | kMirOpLowerBound, |
| 87 | kMirOpPunt, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 88 | kMirOpCheckInlinePrediction, // Gen checks for predicted inlining |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 89 | kMirOpLast, |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | struct SSARepresentation; |
| 93 | |
| 94 | typedef enum { |
| 95 | kMIRIgnoreNullCheck = 0, |
| 96 | kMIRNullCheckOnly, |
| 97 | kMIRIgnoreRangeCheck, |
| 98 | kMIRRangeCheckOnly, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 99 | kMIRInlined, // Invoke is inlined (ie dead) |
| 100 | kMIRInlinedPred, // Invoke is inlined via prediction |
| 101 | kMIRCallee, // Instruction is inlined from callee |
| Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 102 | kMIRInvokeMethodJIT, // Callee is JIT'ed as a whole method |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 103 | } MIROptimizationFlagPositons; |
| 104 | |
| 105 | #define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck) |
| 106 | #define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly) |
| 107 | #define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck) |
| 108 | #define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly) |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 109 | #define MIR_INLINED (1 << kMIRInlined) |
| 110 | #define MIR_INLINED_PRED (1 << kMIRInlinedPred) |
| 111 | #define MIR_CALLEE (1 << kMIRCallee) |
| Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 112 | #define MIR_INVOKE_METHOD_JIT (1 << kMIRInvokeMethodJIT) |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 113 | |
| 114 | typedef struct CallsiteInfo { |
| Ben Cheng | 385828e | 2011-03-04 16:48:33 -0800 | [diff] [blame] | 115 | const char *classDescriptor; |
| 116 | Object *classLoader; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 117 | const Method *method; |
| 118 | LIR *misPredBranchOver; |
| 119 | } CallsiteInfo; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 120 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 121 | typedef struct MIR { |
| 122 | DecodedInstruction dalvikInsn; |
| 123 | unsigned int width; |
| 124 | unsigned int offset; |
| 125 | struct MIR *prev; |
| 126 | struct MIR *next; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 127 | struct SSARepresentation *ssaRep; |
| 128 | int OptimizationFlags; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 129 | int seqNum; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 130 | union { |
| 131 | // Used by the inlined insn from the callee to find the mother method |
| 132 | const Method *calleeMethod; |
| 133 | // Used by the inlined invoke to find the class and method pointers |
| 134 | CallsiteInfo *callsiteInfo; |
| 135 | } meta; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 136 | } MIR; |
| 137 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 138 | struct BasicBlockDataFlow; |
| 139 | |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 140 | /* For successorBlockList */ |
| 141 | typedef enum BlockListType { |
| 142 | kNotUsed = 0, |
| 143 | kCatch, |
| 144 | kPackedSwitch, |
| 145 | kSparseSwitch, |
| 146 | } BlockListType; |
| 147 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 148 | typedef struct BasicBlock { |
| 149 | int id; |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 150 | bool visited; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 151 | unsigned int startOffset; |
| 152 | const Method *containingMethod; // For blocks from the callee |
| 153 | BBType blockType; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 154 | bool needFallThroughBranch; // For blocks ended due to length limit |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 155 | bool isFallThroughFromInvoke; // True means the block needs alignment |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 156 | MIR *firstMIRInsn; |
| 157 | MIR *lastMIRInsn; |
| 158 | struct BasicBlock *fallThrough; |
| 159 | struct BasicBlock *taken; |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 160 | struct BasicBlock *iDom; // Immediate dominator |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 161 | struct BasicBlockDataFlow *dataFlowInfo; |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 162 | BitVector *predecessors; |
| 163 | BitVector *dominators; |
| 164 | BitVector *iDominated; // Set nodes being immediately dominated |
| 165 | BitVector *domFrontier; // Dominance frontier |
| 166 | struct { // For one-to-many successors like |
| 167 | BlockListType blockListType; // switch and exception handling |
| 168 | GrowableList blocks; |
| 169 | } successorBlockList; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 170 | } BasicBlock; |
| 171 | |
| Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 172 | /* |
| 173 | * The "blocks" field in "successorBlockList" points to an array of |
| 174 | * elements with the type "SuccessorBlockInfo". |
| 175 | * For catch blocks, key is type index for the exception. |
| 176 | * For swtich blocks, key is the case value. |
| 177 | */ |
| 178 | typedef struct SuccessorBlockInfo { |
| 179 | BasicBlock *block; |
| 180 | int key; |
| 181 | } SuccessorBlockInfo; |
| 182 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 183 | struct LoopAnalysis; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 184 | struct RegisterPool; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 185 | |
| buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 186 | typedef enum AssemblerStatus { |
| 187 | kSuccess, |
| 188 | kRetryAll, |
| 189 | kRetryHalve |
| 190 | } AssemblerStatus; |
| 191 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 192 | typedef struct CompilationUnit { |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 193 | int numInsts; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 194 | int numBlocks; |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 195 | GrowableList blockList; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 196 | const Method *method; |
| 197 | const JitTraceDescription *traceDesc; |
| 198 | LIR *firstLIRInsn; |
| 199 | LIR *lastLIRInsn; |
| Ben Cheng | 385828e | 2011-03-04 16:48:33 -0800 | [diff] [blame] | 200 | LIR *literalList; // Constants |
| 201 | LIR *classPointerList; // Relocatable |
| 202 | int numClassPointers; |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 203 | LIR *chainCellOffsetLIR; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 204 | GrowableList pcReconstructionList; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 205 | int headerSize; // bytes before the first code ptr |
| 206 | int dataOffset; // starting offset of literal pool |
| 207 | int totalSize; // header + code size |
| buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 208 | AssemblerStatus assemblerStatus; // Success or fix and retry |
| 209 | int assemblerRetries; // How many times tried to fix assembly |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 210 | unsigned char *codeBuffer; |
| 211 | void *baseAddr; |
| 212 | bool printMe; |
| 213 | bool allSingleStep; |
| Ben Cheng | 385828e | 2011-03-04 16:48:33 -0800 | [diff] [blame] | 214 | bool hasClassLiterals; // Contains class ptrs used as literals |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 215 | bool hasLoop; // Contains a loop |
| 216 | bool hasInvoke; // Contains an invoke instruction |
| jeffhao | 9e45c0b | 2010-02-03 10:24:05 -0800 | [diff] [blame] | 217 | bool heapMemOp; // Mark mem ops for self verification |
| Ben Cheng | d72564c | 2011-02-08 17:09:25 -0800 | [diff] [blame] | 218 | bool usesLinkRegister; // For self-verification only |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 219 | int profileCodeSize; // Size of the profile prefix in bytes |
| Ben Cheng | cec26f6 | 2010-01-15 15:29:33 -0800 | [diff] [blame] | 220 | int numChainingCells[kChainingCellGap]; |
| 221 | LIR *firstChainingLIR[kChainingCellGap]; |
| 222 | LIR *chainingCellBottom; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 223 | struct RegisterPool *regPool; |
| Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 224 | int optRound; // round number to tell an LIR's age |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 225 | jmp_buf *bailPtr; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 226 | JitInstructionSetType instructionSet; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 227 | /* Number of total regs used in the whole cUnit after SSA transformation */ |
| 228 | int numSSARegs; |
| 229 | /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */ |
| 230 | GrowableList *ssaToDalvikMap; |
| 231 | |
| 232 | /* The following are new data structures to support SSA representations */ |
| 233 | /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */ |
| 234 | int *dalvikToSSAMap; // length == method->registersSize |
| 235 | BitVector *isConstantV; // length == numSSAReg |
| 236 | int *constantValues; // length == numSSAReg |
| 237 | |
| 238 | /* Data structure for loop analysis and optimizations */ |
| 239 | struct LoopAnalysis *loopAnalysis; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 240 | |
| 241 | /* Map SSA names to location */ |
| 242 | RegLocation *regLocation; |
| 243 | int sequenceNumber; |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * Set to the Dalvik PC of the switch instruction if it has more than |
| 247 | * MAX_CHAINED_SWITCH_CASES cases. |
| 248 | */ |
| 249 | const u2 *switchOverflowPad; |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 250 | |
| 251 | /* New fields only for method-based compilation */ |
| Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 252 | bool methodJitMode; |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 253 | int numReachableBlocks; |
| 254 | int numDalvikRegisters; // method->registersSize + inlined |
| 255 | BasicBlock *entryBlock; |
| 256 | BasicBlock *exitBlock; |
| Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 257 | BasicBlock *curBlock; |
| Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame] | 258 | BasicBlock *nextCodegenBlock; // for extended trace codegen |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 259 | GrowableList dfsOrder; |
| 260 | GrowableList domPostOrderTraversal; |
| 261 | BitVector *tryBlockAddr; |
| 262 | BitVector **defBlockMatrix; // numDalvikRegister x numBlocks |
| 263 | BitVector *tempBlockV; |
| 264 | BitVector *tempDalvikRegisterV; |
| 265 | BitVector *tempSSARegisterV; // numSSARegs |
| 266 | bool printSSANames; |
| Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 267 | void *blockLabelList; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 268 | } CompilationUnit; |
| 269 | |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 270 | #if defined(WITH_SELF_VERIFICATION) |
| 271 | #define HEAP_ACCESS_SHADOW(_state) cUnit->heapMemOp = _state |
| 272 | #else |
| 273 | #define HEAP_ACCESS_SHADOW(_state) |
| 274 | #endif |
| 275 | |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 276 | BasicBlock *dvmCompilerNewBB(BBType blockType, int blockId); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 277 | |
| 278 | void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir); |
| 279 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 280 | void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir); |
| 281 | |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 282 | void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR); |
| 283 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 284 | void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir); |
| 285 | |
| Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 286 | void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR); |
| 287 | |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 288 | void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR); |
| 289 | |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 290 | void dvmCompilerAbort(CompilationUnit *cUnit); |
| 291 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 292 | /* Debug Utilities */ |
| 293 | void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit); |
| 294 | |
| 295 | #endif /* _DALVIK_VM_COMPILER_IR */ |