| 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, |
| 54 | kChainingCellLast, |
| 55 | kEntryBlock, |
| 56 | kDalvikByteCode, |
| 57 | kExitBlock, |
| 58 | kPCReconstruction, |
| 59 | kExceptionHandling, |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 60 | } BBType; |
| 61 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 62 | typedef struct ChainCellCounts { |
| 63 | union { |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 64 | u1 count[kChainingCellLast]; |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 65 | u4 dummyForAlignment; |
| 66 | } u; |
| 67 | } ChainCellCounts; |
| 68 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 69 | typedef struct LIR { |
| 70 | int offset; |
| 71 | struct LIR *next; |
| 72 | struct LIR *prev; |
| 73 | struct LIR *target; |
| 74 | } LIR; |
| 75 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 76 | enum ExtendedMIROpcode { |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 77 | kMirOpFirst = 256, |
| 78 | kMirOpPhi = kMirOpFirst, |
| 79 | kMirOpNullNRangeUpCheck, |
| 80 | kMirOpNullNRangeDownCheck, |
| 81 | kMirOpLowerBound, |
| 82 | kMirOpPunt, |
| 83 | kMirOpLast, |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct SSARepresentation; |
| 87 | |
| 88 | typedef enum { |
| 89 | kMIRIgnoreNullCheck = 0, |
| 90 | kMIRNullCheckOnly, |
| 91 | kMIRIgnoreRangeCheck, |
| 92 | kMIRRangeCheckOnly, |
| 93 | } MIROptimizationFlagPositons; |
| 94 | |
| 95 | #define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck) |
| 96 | #define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly) |
| 97 | #define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck) |
| 98 | #define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly) |
| 99 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 100 | typedef struct MIR { |
| 101 | DecodedInstruction dalvikInsn; |
| 102 | unsigned int width; |
| 103 | unsigned int offset; |
| 104 | struct MIR *prev; |
| 105 | struct MIR *next; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 106 | struct SSARepresentation *ssaRep; |
| 107 | int OptimizationFlags; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 108 | int seqNum; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 109 | } MIR; |
| 110 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 111 | struct BasicBlockDataFlow; |
| 112 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 113 | typedef struct BasicBlock { |
| 114 | int id; |
| 115 | int visited; |
| 116 | unsigned int startOffset; |
| 117 | const Method *containingMethod; // For blocks from the callee |
| 118 | BBType blockType; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 119 | bool needFallThroughBranch; // For blocks ended due to length limit |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 120 | MIR *firstMIRInsn; |
| 121 | MIR *lastMIRInsn; |
| 122 | struct BasicBlock *fallThrough; |
| 123 | struct BasicBlock *taken; |
| 124 | struct BasicBlock *next; // Serial link for book keeping purposes |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 125 | struct BasicBlockDataFlow *dataFlowInfo; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 126 | } BasicBlock; |
| 127 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 128 | struct LoopAnalysis; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 129 | struct RegisterPool; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 130 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 131 | typedef struct CompilationUnit { |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 132 | int numInsts; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 133 | int numBlocks; |
| 134 | BasicBlock **blockList; |
| 135 | const Method *method; |
| 136 | const JitTraceDescription *traceDesc; |
| 137 | LIR *firstLIRInsn; |
| 138 | LIR *lastLIRInsn; |
| 139 | LIR *wordList; |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 140 | LIR *chainCellOffsetLIR; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 141 | GrowableList pcReconstructionList; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 142 | int headerSize; // bytes before the first code ptr |
| 143 | int dataOffset; // starting offset of literal pool |
| 144 | int totalSize; // header + code size |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 145 | unsigned char *codeBuffer; |
| 146 | void *baseAddr; |
| 147 | bool printMe; |
| 148 | bool allSingleStep; |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 149 | bool halveInstCount; |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 150 | bool executionCount; // Add code to count trace executions |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 151 | bool hasLoop; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 152 | int numChainingCells[kChainingCellLast]; |
| 153 | LIR *firstChainingLIR[kChainingCellLast]; |
| 154 | struct RegisterPool *regPool; |
| Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 155 | int optRound; // round number to tell an LIR's age |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 156 | JitInstructionSetType instructionSet; |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 157 | /* Number of total regs used in the whole cUnit after SSA transformation */ |
| 158 | int numSSARegs; |
| 159 | /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */ |
| 160 | GrowableList *ssaToDalvikMap; |
| 161 | |
| 162 | /* The following are new data structures to support SSA representations */ |
| 163 | /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */ |
| 164 | int *dalvikToSSAMap; // length == method->registersSize |
| 165 | BitVector *isConstantV; // length == numSSAReg |
| 166 | int *constantValues; // length == numSSAReg |
| 167 | |
| 168 | /* Data structure for loop analysis and optimizations */ |
| 169 | struct LoopAnalysis *loopAnalysis; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 170 | |
| 171 | /* Map SSA names to location */ |
| 172 | RegLocation *regLocation; |
| 173 | int sequenceNumber; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 174 | } CompilationUnit; |
| 175 | |
| 176 | BasicBlock *dvmCompilerNewBB(BBType blockType); |
| 177 | |
| 178 | void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir); |
| 179 | |
| Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 180 | void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir); |
| 181 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 182 | void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir); |
| 183 | |
| Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 184 | void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR); |
| 185 | |
| Ben Cheng | dcf3e5d | 2009-09-11 13:42:05 -0700 | [diff] [blame] | 186 | void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR); |
| 187 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 188 | /* Debug Utilities */ |
| 189 | void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit); |
| 190 | |
| 191 | #endif /* _DALVIK_VM_COMPILER_IR */ |