buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 ART_SRC_COMPILER_COMPILER_IR_H_ |
| 18 | #define ART_SRC_COMPILER_COMPILER_IR_H_ |
| 19 | |
| 20 | #include "codegen/Optimizer.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 21 | #include <vector> |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 22 | |
| 23 | typedef enum RegisterClass { |
| 24 | kCoreReg, |
| 25 | kFPReg, |
| 26 | kAnyReg, |
| 27 | } RegisterClass; |
| 28 | |
| 29 | typedef enum RegLocationType { |
| 30 | kLocDalvikFrame = 0, // Normal Dalvik register |
| 31 | kLocPhysReg, |
| 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 | unsigned home:1; // Does this represent the home location? |
| 43 | RegLocationType fpLocation:2; // Used only for non-SSA loc records |
| 44 | u1 fpLowReg:6; // Used only for non-SSA loc records |
| 45 | u1 fpHighReg:6; // Used only for non-SSA loc records |
| 46 | int spOffset:17; |
| 47 | } RegLocation; |
| 48 | |
| 49 | #define INVALID_SREG (-1) |
| 50 | #define INVALID_REG (0x3F) |
| 51 | #define INVALID_OFFSET (-1) |
| 52 | |
| 53 | typedef enum BBType { |
| 54 | kEntryBlock, |
| 55 | kDalvikByteCode, |
| 56 | kExitBlock, |
| 57 | kExceptionHandling, |
| 58 | kCatchEntry, |
| 59 | } BBType; |
| 60 | |
| 61 | typedef struct LIR { |
| 62 | int offset; // Offset of this instruction |
| 63 | int dalvikOffset; // Offset of Dalvik opcode |
| 64 | struct LIR* next; |
| 65 | struct LIR* prev; |
| 66 | struct LIR* target; |
| 67 | } LIR; |
| 68 | |
| 69 | enum ExtendedMIROpcode { |
| 70 | kMirOpFirst = kNumPackedOpcodes, |
| 71 | kMirOpPhi = kMirOpFirst, |
| 72 | kMirOpNullNRangeUpCheck, |
| 73 | kMirOpNullNRangeDownCheck, |
| 74 | kMirOpLowerBound, |
| 75 | kMirOpPunt, |
| 76 | kMirOpCheckInlinePrediction, // Gen checks for predicted inlining |
| 77 | kMirOpLast, |
| 78 | }; |
| 79 | |
| 80 | struct SSARepresentation; |
| 81 | |
| 82 | typedef enum { |
| 83 | kMIRIgnoreNullCheck = 0, |
| 84 | kMIRNullCheckOnly, |
| 85 | kMIRIgnoreRangeCheck, |
| 86 | kMIRRangeCheckOnly, |
| 87 | kMIRInlined, // Invoke is inlined (ie dead) |
| 88 | kMIRInlinedPred, // Invoke is inlined via prediction |
| 89 | kMIRCallee, // Instruction is inlined from callee |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 90 | kMIRIgnoreSuspendCheck, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 91 | } MIROptimizationFlagPositons; |
| 92 | |
| 93 | #define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck) |
| 94 | #define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly) |
| 95 | #define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck) |
| 96 | #define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly) |
| 97 | #define MIR_INLINED (1 << kMIRInlined) |
| 98 | #define MIR_INLINED_PRED (1 << kMIRInlinedPred) |
| 99 | #define MIR_CALLEE (1 << kMIRCallee) |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 100 | #define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 101 | |
| 102 | typedef struct CallsiteInfo { |
| 103 | const char* classDescriptor; |
| 104 | Object* classLoader; |
| 105 | const Method* method; |
| 106 | LIR* misPredBranchOver; |
| 107 | } CallsiteInfo; |
| 108 | |
| 109 | typedef struct MIR { |
| 110 | DecodedInstruction dalvikInsn; |
| 111 | unsigned int width; |
| 112 | unsigned int offset; |
| 113 | struct MIR* prev; |
| 114 | struct MIR* next; |
| 115 | struct SSARepresentation* ssaRep; |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 116 | int optimizationFlags; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 117 | int seqNum; |
| 118 | union { |
| 119 | // Used by the inlined insn from the callee to find the mother method |
| 120 | const Method* calleeMethod; |
| 121 | // Used by the inlined invoke to find the class and method pointers |
| 122 | CallsiteInfo* callsiteInfo; |
buzbee | c0ecd65 | 2011-09-25 18:11:54 -0700 | [diff] [blame] | 123 | // Used to quickly locate all Phi opcodes |
| 124 | struct MIR* phiNext; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 125 | } meta; |
| 126 | } MIR; |
| 127 | |
| 128 | struct BasicBlockDataFlow; |
| 129 | |
| 130 | /* For successorBlockList */ |
| 131 | typedef enum BlockListType { |
| 132 | kNotUsed = 0, |
| 133 | kCatch, |
| 134 | kPackedSwitch, |
| 135 | kSparseSwitch, |
| 136 | } BlockListType; |
| 137 | |
| 138 | typedef struct BasicBlock { |
| 139 | int id; |
| 140 | bool visited; |
| 141 | bool hidden; |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 142 | bool catchEntry; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 143 | unsigned int startOffset; |
| 144 | const Method* containingMethod; // For blocks from the callee |
| 145 | BBType blockType; |
| 146 | bool needFallThroughBranch; // For blocks ended due to length limit |
| 147 | bool isFallThroughFromInvoke; // True means the block needs alignment |
| 148 | MIR* firstMIRInsn; |
| 149 | MIR* lastMIRInsn; |
| 150 | struct BasicBlock* fallThrough; |
| 151 | struct BasicBlock* taken; |
| 152 | struct BasicBlock* iDom; // Immediate dominator |
| 153 | struct BasicBlockDataFlow* dataFlowInfo; |
| 154 | ArenaBitVector* predecessors; |
| 155 | ArenaBitVector* dominators; |
| 156 | ArenaBitVector* iDominated; // Set nodes being immediately dominated |
| 157 | ArenaBitVector* domFrontier; // Dominance frontier |
| 158 | struct { // For one-to-many successors like |
| 159 | BlockListType blockListType; // switch and exception handling |
| 160 | GrowableList blocks; |
| 161 | } successorBlockList; |
| 162 | } BasicBlock; |
| 163 | |
| 164 | /* |
| 165 | * The "blocks" field in "successorBlockList" points to an array of |
| 166 | * elements with the type "SuccessorBlockInfo". |
| 167 | * For catch blocks, key is type index for the exception. |
| 168 | * For swtich blocks, key is the case value. |
| 169 | */ |
| 170 | typedef struct SuccessorBlockInfo { |
| 171 | BasicBlock* block; |
| 172 | int key; |
| 173 | } SuccessorBlockInfo; |
| 174 | |
| 175 | struct LoopAnalysis; |
| 176 | struct RegisterPool; |
| 177 | |
| 178 | typedef enum AssemblerStatus { |
| 179 | kSuccess, |
| 180 | kRetryAll, |
| 181 | kRetryHalve |
| 182 | } AssemblerStatus; |
| 183 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 184 | typedef struct CompilationUnit { |
| 185 | int numInsts; |
| 186 | int numBlocks; |
| 187 | GrowableList blockList; |
| 188 | const Method *method; |
| 189 | LIR* firstLIRInsn; |
| 190 | LIR* lastLIRInsn; |
| 191 | LIR* literalList; // Constants |
| 192 | LIR* classPointerList; // Relocatable |
| 193 | int numClassPointers; |
| 194 | LIR* chainCellOffsetLIR; |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame^] | 195 | uint32_t disableOpt; // optControlVector flags |
| 196 | uint32_t enableDebug; // debugControlVector flags |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 197 | int headerSize; // bytes before the first code ptr |
| 198 | int dataOffset; // starting offset of literal pool |
| 199 | int totalSize; // header + code size |
| 200 | AssemblerStatus assemblerStatus; // Success or fix and retry |
| 201 | int assemblerRetries; |
buzbee | 4ef7652 | 2011-09-08 10:00:32 -0700 | [diff] [blame] | 202 | std::vector<short> codeBuffer; |
| 203 | std::vector<uint32_t> mappingTable; |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 204 | std::vector<uint32_t> coreVmapTable; |
| 205 | std::vector<short> fpVmapTable; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 206 | bool printMe; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 207 | bool hasClassLiterals; // Contains class ptrs used as literals |
| 208 | bool hasLoop; // Contains a loop |
| 209 | bool hasInvoke; // Contains an invoke instruction |
| 210 | bool heapMemOp; // Mark mem ops for self verification |
| 211 | bool usesLinkRegister; // For self-verification only |
| 212 | bool methodTraceSupport; // For TraceView profiling |
| 213 | struct RegisterPool* regPool; |
| 214 | int optRound; // round number to tell an LIR's age |
| 215 | OatInstructionSetType instructionSet; |
| 216 | /* Number of total regs used in the whole cUnit after SSA transformation */ |
| 217 | int numSSARegs; |
| 218 | /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */ |
| 219 | GrowableList* ssaToDalvikMap; |
| 220 | |
| 221 | /* The following are new data structures to support SSA representations */ |
| 222 | /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */ |
| 223 | int* dalvikToSSAMap; // length == method->registersSize |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 224 | int* SSALastDefs; // length == method->registersSize |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 225 | ArenaBitVector* isConstantV; // length == numSSAReg |
| 226 | int* constantValues; // length == numSSAReg |
buzbee | c0ecd65 | 2011-09-25 18:11:54 -0700 | [diff] [blame] | 227 | int* phiAliasMap; // length == numSSAReg |
| 228 | MIR* phiList; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 229 | |
| 230 | /* Map SSA names to location */ |
| 231 | RegLocation* regLocation; |
| 232 | int sequenceNumber; |
| 233 | |
| 234 | /* |
| 235 | * Set to the Dalvik PC of the switch instruction if it has more than |
| 236 | * MAX_CHAINED_SWITCH_CASES cases. |
| 237 | */ |
| 238 | const u2* switchOverflowPad; |
| 239 | |
| 240 | int numReachableBlocks; |
| 241 | int numDalvikRegisters; // method->registersSize + inlined |
| 242 | BasicBlock* entryBlock; |
| 243 | BasicBlock* exitBlock; |
| 244 | BasicBlock* curBlock; |
| 245 | BasicBlock* nextCodegenBlock; // for extended trace codegen |
| 246 | GrowableList dfsOrder; |
| 247 | GrowableList domPostOrderTraversal; |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 248 | GrowableList throwLaunchpads; |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 249 | GrowableList suspendLaunchpads; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 250 | ArenaBitVector* tryBlockAddr; |
| 251 | ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks |
| 252 | ArenaBitVector* tempBlockV; |
| 253 | ArenaBitVector* tempDalvikRegisterV; |
| 254 | ArenaBitVector* tempSSARegisterV; // numSSARegs |
| 255 | bool printSSANames; |
| 256 | void* blockLabelList; |
| 257 | bool quitLoopMode; // cold path/complex bytecode |
| 258 | int preservedRegsUsed; // How many callee save regs used |
| 259 | /* |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 260 | * Frame layout details. |
| 261 | * NOTE: for debug support it will be necessary to add a structure |
| 262 | * to map the Dalvik virtual registers to the promoted registers. |
| 263 | * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 264 | */ |
| 265 | int numIns; |
| 266 | int numOuts; |
| 267 | int numRegs; // Unlike struct Method, does not include ins |
buzbee | bbaf894 | 2011-10-02 13:08:29 -0700 | [diff] [blame] | 268 | int numCoreSpills; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 269 | int numFPSpills; |
| 270 | int numPadding; // # of 4-byte padding cells |
| 271 | int regsOffset; // sp-relative offset to beginning of Dalvik regs |
| 272 | int insOffset; // sp-relative offset to beginning of Dalvik ins |
| 273 | int frameSize; |
| 274 | unsigned int coreSpillMask; |
| 275 | unsigned int fpSpillMask; |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 276 | unsigned int attrs; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 277 | /* |
| 278 | * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in |
buzbee | 03fa263 | 2011-09-20 17:10:57 -0700 | [diff] [blame] | 279 | * mechanism to propagate the original Dalvik opcode address to the |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 280 | * associated generated instructions. For the trace compiler, this wasn't |
| 281 | * necessary because the interpreter handled all throws and debugging |
| 282 | * requests. For now we'll handle this by placing the Dalvik offset |
| 283 | * in the CompilationUnit struct before codegen for each instruction. |
| 284 | * The low-level LIR creation utilites will pull it from here. Should |
| 285 | * be rewritten. |
| 286 | */ |
| 287 | int currentDalvikOffset; |
| 288 | GrowableList switchTables; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 289 | GrowableList fillArrayData; |
| 290 | const u2* insns; |
| 291 | u4 insnsSize; |
| 292 | } CompilationUnit; |
| 293 | |
| 294 | BasicBlock* oatNewBB(BBType blockType, int blockId); |
| 295 | |
| 296 | void oatAppendMIR(BasicBlock* bb, MIR* mir); |
| 297 | |
| 298 | void oatPrependMIR(BasicBlock* bb, MIR* mir); |
| 299 | |
| 300 | void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR); |
| 301 | |
| 302 | void oatAppendLIR(CompilationUnit* cUnit, LIR* lir); |
| 303 | |
| 304 | void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR); |
| 305 | |
| 306 | void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR); |
| 307 | |
| 308 | /* Debug Utilities */ |
| 309 | void oatDumpCompilationUnit(CompilationUnit* cUnit); |
| 310 | |
| 311 | #endif // ART_SRC_COMPILER_COMPILER_IR_H_ |