blob: f981ccd167982cfd4f4ed7df00742079ddb00220 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
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"
buzbeec143c552011-08-20 17:38:58 -070021#include <vector>
buzbee67bf8852011-08-17 17:51:35 -070022
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080023namespace art {
24
buzbee67bf8852011-08-17 17:51:35 -070025typedef enum RegisterClass {
26 kCoreReg,
27 kFPReg,
28 kAnyReg,
29} RegisterClass;
30
31typedef enum RegLocationType {
32 kLocDalvikFrame = 0, // Normal Dalvik register
33 kLocPhysReg,
34 kLocSpill,
35} RegLocationType;
36
buzbee67bc2362011-10-11 18:08:40 -070037typedef struct PromotionMap {
38 RegLocationType coreLocation:3;
39 u1 coreReg;
40 RegLocationType fpLocation:3;
41 u1 fpReg;
42 bool firstInPair;
43} PromotionMap;
44
buzbee67bf8852011-08-17 17:51:35 -070045typedef struct RegLocation {
buzbee67bc2362011-10-11 18:08:40 -070046 RegLocationType location:3;
buzbee67bf8852011-08-17 17:51:35 -070047 unsigned wide:1;
buzbee67bc2362011-10-11 18:08:40 -070048 unsigned defined:1; // Do we know the type?
49 unsigned fp:1; // Floating point?
50 unsigned core:1; // Non-floating point?
51 unsigned highWord:1; // High word of pair?
52 unsigned home:1; // Does this represent the home location?
53 u1 lowReg; // First physical register
54 u1 highReg; // 2nd physical register (if wide)
55 s2 sRegLow; // SSA name for low Dalvik word
buzbee67bf8852011-08-17 17:51:35 -070056} RegLocation;
57
58#define INVALID_SREG (-1)
buzbee3ddc0d12011-10-05 10:36:21 -070059#define INVALID_VREG (0xFFFFU)
buzbee67bc2362011-10-11 18:08:40 -070060#define INVALID_REG (0xFF)
buzbee67bf8852011-08-17 17:51:35 -070061#define INVALID_OFFSET (-1)
62
63typedef enum BBType {
64 kEntryBlock,
65 kDalvikByteCode,
66 kExitBlock,
67 kExceptionHandling,
68 kCatchEntry,
69} BBType;
70
71typedef struct LIR {
72 int offset; // Offset of this instruction
73 int dalvikOffset; // Offset of Dalvik opcode
74 struct LIR* next;
75 struct LIR* prev;
76 struct LIR* target;
77} LIR;
78
79enum ExtendedMIROpcode {
80 kMirOpFirst = kNumPackedOpcodes,
81 kMirOpPhi = kMirOpFirst,
82 kMirOpNullNRangeUpCheck,
83 kMirOpNullNRangeDownCheck,
84 kMirOpLowerBound,
85 kMirOpPunt,
86 kMirOpCheckInlinePrediction, // Gen checks for predicted inlining
87 kMirOpLast,
88};
89
90struct SSARepresentation;
91
92typedef enum {
93 kMIRIgnoreNullCheck = 0,
94 kMIRNullCheckOnly,
95 kMIRIgnoreRangeCheck,
96 kMIRRangeCheckOnly,
97 kMIRInlined, // Invoke is inlined (ie dead)
98 kMIRInlinedPred, // Invoke is inlined via prediction
99 kMIRCallee, // Instruction is inlined from callee
buzbeec1f45042011-09-21 16:03:19 -0700100 kMIRIgnoreSuspendCheck,
buzbee67bf8852011-08-17 17:51:35 -0700101} 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)
107#define MIR_INLINED (1 << kMIRInlined)
108#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
109#define MIR_CALLEE (1 << kMIRCallee)
buzbeec1f45042011-09-21 16:03:19 -0700110#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
buzbee67bf8852011-08-17 17:51:35 -0700111
112typedef struct CallsiteInfo {
113 const char* classDescriptor;
114 Object* classLoader;
115 const Method* method;
116 LIR* misPredBranchOver;
117} CallsiteInfo;
118
119typedef struct MIR {
120 DecodedInstruction dalvikInsn;
121 unsigned int width;
122 unsigned int offset;
123 struct MIR* prev;
124 struct MIR* next;
125 struct SSARepresentation* ssaRep;
buzbee43a36422011-09-14 14:00:13 -0700126 int optimizationFlags;
buzbee67bf8852011-08-17 17:51:35 -0700127 int seqNum;
128 union {
129 // Used by the inlined insn from the callee to find the mother method
130 const Method* calleeMethod;
131 // Used by the inlined invoke to find the class and method pointers
132 CallsiteInfo* callsiteInfo;
buzbeec0ecd652011-09-25 18:11:54 -0700133 // Used to quickly locate all Phi opcodes
134 struct MIR* phiNext;
buzbee67bf8852011-08-17 17:51:35 -0700135 } meta;
136} MIR;
137
138struct BasicBlockDataFlow;
139
140/* For successorBlockList */
141typedef enum BlockListType {
142 kNotUsed = 0,
143 kCatch,
144 kPackedSwitch,
145 kSparseSwitch,
146} BlockListType;
147
148typedef struct BasicBlock {
149 int id;
150 bool visited;
151 bool hidden;
buzbee43a36422011-09-14 14:00:13 -0700152 bool catchEntry;
buzbee67bf8852011-08-17 17:51:35 -0700153 unsigned int startOffset;
154 const Method* containingMethod; // For blocks from the callee
155 BBType blockType;
156 bool needFallThroughBranch; // For blocks ended due to length limit
157 bool isFallThroughFromInvoke; // True means the block needs alignment
158 MIR* firstMIRInsn;
159 MIR* lastMIRInsn;
160 struct BasicBlock* fallThrough;
161 struct BasicBlock* taken;
162 struct BasicBlock* iDom; // Immediate dominator
163 struct BasicBlockDataFlow* dataFlowInfo;
164 ArenaBitVector* predecessors;
165 ArenaBitVector* dominators;
166 ArenaBitVector* iDominated; // Set nodes being immediately dominated
167 ArenaBitVector* domFrontier; // Dominance frontier
168 struct { // For one-to-many successors like
169 BlockListType blockListType; // switch and exception handling
170 GrowableList blocks;
171 } successorBlockList;
172} BasicBlock;
173
174/*
175 * The "blocks" field in "successorBlockList" points to an array of
176 * elements with the type "SuccessorBlockInfo".
177 * For catch blocks, key is type index for the exception.
178 * For swtich blocks, key is the case value.
179 */
180typedef struct SuccessorBlockInfo {
181 BasicBlock* block;
182 int key;
183} SuccessorBlockInfo;
184
185struct LoopAnalysis;
186struct RegisterPool;
187
188typedef enum AssemblerStatus {
189 kSuccess,
190 kRetryAll,
191 kRetryHalve
192} AssemblerStatus;
193
buzbee67bf8852011-08-17 17:51:35 -0700194typedef struct CompilationUnit {
195 int numInsts;
196 int numBlocks;
197 GrowableList blockList;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800198 const Compiler* compiler; // Compiler driving this compiler
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800199 ClassLinker* class_linker; // Linker to resolve fields and methods
200 const DexFile* dex_file; // DexFile containing the method being compiled
201 DexCache* dex_cache; // DexFile's corresponding cache
202 const ClassLoader* class_loader; // compiling method's class loader
Ian Rogersa3760aa2011-11-14 14:32:37 -0800203 uint32_t method_idx; // compiling method's index into method_ids of DexFile
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800204 const DexFile::CodeItem* code_item; // compiling method's DexFile code_item
Ian Rogersa3760aa2011-11-14 14:32:37 -0800205 uint32_t access_flags; // compiling method's access flags
206 const char* shorty; // compiling method's shorty
buzbee67bf8852011-08-17 17:51:35 -0700207 LIR* firstLIRInsn;
208 LIR* lastLIRInsn;
209 LIR* literalList; // Constants
210 LIR* classPointerList; // Relocatable
211 int numClassPointers;
212 LIR* chainCellOffsetLIR;
buzbeece302932011-10-04 14:32:18 -0700213 uint32_t disableOpt; // optControlVector flags
214 uint32_t enableDebug; // debugControlVector flags
buzbee67bf8852011-08-17 17:51:35 -0700215 int headerSize; // bytes before the first code ptr
216 int dataOffset; // starting offset of literal pool
217 int totalSize; // header + code size
218 AssemblerStatus assemblerStatus; // Success or fix and retry
219 int assemblerRetries;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800220 std::vector<uint16_t> codeBuffer;
buzbee4ef76522011-09-08 10:00:32 -0700221 std::vector<uint32_t> mappingTable;
buzbee3ddc0d12011-10-05 10:36:21 -0700222 std::vector<uint16_t> coreVmapTable;
223 std::vector<uint16_t> fpVmapTable;
buzbee67bf8852011-08-17 17:51:35 -0700224 bool printMe;
buzbee67bf8852011-08-17 17:51:35 -0700225 bool hasClassLiterals; // Contains class ptrs used as literals
226 bool hasLoop; // Contains a loop
227 bool hasInvoke; // Contains an invoke instruction
228 bool heapMemOp; // Mark mem ops for self verification
229 bool usesLinkRegister; // For self-verification only
230 bool methodTraceSupport; // For TraceView profiling
231 struct RegisterPool* regPool;
232 int optRound; // round number to tell an LIR's age
233 OatInstructionSetType instructionSet;
234 /* Number of total regs used in the whole cUnit after SSA transformation */
235 int numSSARegs;
236 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
237 GrowableList* ssaToDalvikMap;
238
239 /* The following are new data structures to support SSA representations */
240 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
241 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700242 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700243 ArenaBitVector* isConstantV; // length == numSSAReg
244 int* constantValues; // length == numSSAReg
buzbeec0ecd652011-09-25 18:11:54 -0700245 int* phiAliasMap; // length == numSSAReg
246 MIR* phiList;
buzbee67bf8852011-08-17 17:51:35 -0700247
248 /* Map SSA names to location */
249 RegLocation* regLocation;
250 int sequenceNumber;
251
buzbee67bc2362011-10-11 18:08:40 -0700252 /* Keep track of Dalvik vReg to physical register mappings */
253 PromotionMap* promotionMap;
254
buzbee67bf8852011-08-17 17:51:35 -0700255 /*
256 * Set to the Dalvik PC of the switch instruction if it has more than
257 * MAX_CHAINED_SWITCH_CASES cases.
258 */
259 const u2* switchOverflowPad;
260
261 int numReachableBlocks;
262 int numDalvikRegisters; // method->registersSize + inlined
263 BasicBlock* entryBlock;
264 BasicBlock* exitBlock;
265 BasicBlock* curBlock;
266 BasicBlock* nextCodegenBlock; // for extended trace codegen
267 GrowableList dfsOrder;
268 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700269 GrowableList throwLaunchpads;
buzbeec1f45042011-09-21 16:03:19 -0700270 GrowableList suspendLaunchpads;
buzbee67bf8852011-08-17 17:51:35 -0700271 ArenaBitVector* tryBlockAddr;
272 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
273 ArenaBitVector* tempBlockV;
274 ArenaBitVector* tempDalvikRegisterV;
275 ArenaBitVector* tempSSARegisterV; // numSSARegs
276 bool printSSANames;
277 void* blockLabelList;
278 bool quitLoopMode; // cold path/complex bytecode
279 int preservedRegsUsed; // How many callee save regs used
280 /*
buzbee5ade1d22011-09-09 14:44:52 -0700281 * Frame layout details.
282 * NOTE: for debug support it will be necessary to add a structure
283 * to map the Dalvik virtual registers to the promoted registers.
284 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700285 */
286 int numIns;
287 int numOuts;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800288 int numRegs; // Unlike numDalvikRegisters, does not include ins
buzbeebbaf8942011-10-02 13:08:29 -0700289 int numCoreSpills;
buzbee67bf8852011-08-17 17:51:35 -0700290 int numFPSpills;
291 int numPadding; // # of 4-byte padding cells
292 int regsOffset; // sp-relative offset to beginning of Dalvik regs
293 int insOffset; // sp-relative offset to beginning of Dalvik ins
294 int frameSize;
295 unsigned int coreSpillMask;
296 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700297 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700298 /*
299 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
buzbee03fa2632011-09-20 17:10:57 -0700300 * mechanism to propagate the original Dalvik opcode address to the
buzbee67bf8852011-08-17 17:51:35 -0700301 * associated generated instructions. For the trace compiler, this wasn't
302 * necessary because the interpreter handled all throws and debugging
303 * requests. For now we'll handle this by placing the Dalvik offset
304 * in the CompilationUnit struct before codegen for each instruction.
305 * The low-level LIR creation utilites will pull it from here. Should
306 * be rewritten.
307 */
308 int currentDalvikOffset;
309 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700310 GrowableList fillArrayData;
311 const u2* insns;
312 u4 insnsSize;
313} CompilationUnit;
314
315BasicBlock* oatNewBB(BBType blockType, int blockId);
316
317void oatAppendMIR(BasicBlock* bb, MIR* mir);
318
319void oatPrependMIR(BasicBlock* bb, MIR* mir);
320
321void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
322
323void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
324
325void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
326
327void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
328
329/* Debug Utilities */
330void oatDumpCompilationUnit(CompilationUnit* cUnit);
331
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800332} // namespace art
333
buzbee67bf8852011-08-17 17:51:35 -0700334#endif // ART_SRC_COMPILER_COMPILER_IR_H_