blob: 317924b31ebc003df3e8e60d87acba7aec63cce7 [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;
buzbee5b537102012-01-17 17:33:47 -0800150 int dfsId;
buzbee67bf8852011-08-17 17:51:35 -0700151 bool visited;
152 bool hidden;
buzbee43a36422011-09-14 14:00:13 -0700153 bool catchEntry;
buzbee67bf8852011-08-17 17:51:35 -0700154 unsigned int startOffset;
155 const Method* containingMethod; // For blocks from the callee
156 BBType blockType;
157 bool needFallThroughBranch; // For blocks ended due to length limit
158 bool isFallThroughFromInvoke; // True means the block needs alignment
159 MIR* firstMIRInsn;
160 MIR* lastMIRInsn;
161 struct BasicBlock* fallThrough;
162 struct BasicBlock* taken;
163 struct BasicBlock* iDom; // Immediate dominator
164 struct BasicBlockDataFlow* dataFlowInfo;
165 ArenaBitVector* predecessors;
166 ArenaBitVector* dominators;
167 ArenaBitVector* iDominated; // Set nodes being immediately dominated
168 ArenaBitVector* domFrontier; // Dominance frontier
169 struct { // For one-to-many successors like
170 BlockListType blockListType; // switch and exception handling
171 GrowableList blocks;
172 } successorBlockList;
173} BasicBlock;
174
175/*
176 * The "blocks" field in "successorBlockList" points to an array of
177 * elements with the type "SuccessorBlockInfo".
178 * For catch blocks, key is type index for the exception.
179 * For swtich blocks, key is the case value.
180 */
181typedef struct SuccessorBlockInfo {
182 BasicBlock* block;
183 int key;
184} SuccessorBlockInfo;
185
186struct LoopAnalysis;
187struct RegisterPool;
188
189typedef enum AssemblerStatus {
190 kSuccess,
191 kRetryAll,
192 kRetryHalve
193} AssemblerStatus;
194
buzbee5b537102012-01-17 17:33:47 -0800195#define NOTVISITED (-1)
196
buzbee67bf8852011-08-17 17:51:35 -0700197typedef struct CompilationUnit {
198 int numInsts;
199 int numBlocks;
200 GrowableList blockList;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800201 const Compiler* compiler; // Compiler driving this compiler
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800202 ClassLinker* class_linker; // Linker to resolve fields and methods
203 const DexFile* dex_file; // DexFile containing the method being compiled
204 DexCache* dex_cache; // DexFile's corresponding cache
205 const ClassLoader* class_loader; // compiling method's class loader
Ian Rogersa3760aa2011-11-14 14:32:37 -0800206 uint32_t method_idx; // compiling method's index into method_ids of DexFile
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800207 const DexFile::CodeItem* code_item; // compiling method's DexFile code_item
Ian Rogersa3760aa2011-11-14 14:32:37 -0800208 uint32_t access_flags; // compiling method's access flags
209 const char* shorty; // compiling method's shorty
buzbee67bf8852011-08-17 17:51:35 -0700210 LIR* firstLIRInsn;
211 LIR* lastLIRInsn;
212 LIR* literalList; // Constants
213 LIR* classPointerList; // Relocatable
214 int numClassPointers;
215 LIR* chainCellOffsetLIR;
buzbeece302932011-10-04 14:32:18 -0700216 uint32_t disableOpt; // optControlVector flags
217 uint32_t enableDebug; // debugControlVector flags
buzbee67bf8852011-08-17 17:51:35 -0700218 int headerSize; // bytes before the first code ptr
219 int dataOffset; // starting offset of literal pool
220 int totalSize; // header + code size
221 AssemblerStatus assemblerStatus; // Success or fix and retry
222 int assemblerRetries;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800223 std::vector<uint16_t> codeBuffer;
buzbee4ef76522011-09-08 10:00:32 -0700224 std::vector<uint32_t> mappingTable;
buzbee3ddc0d12011-10-05 10:36:21 -0700225 std::vector<uint16_t> coreVmapTable;
226 std::vector<uint16_t> fpVmapTable;
buzbee67bf8852011-08-17 17:51:35 -0700227 bool printMe;
buzbee67bf8852011-08-17 17:51:35 -0700228 bool hasClassLiterals; // Contains class ptrs used as literals
229 bool hasLoop; // Contains a loop
230 bool hasInvoke; // Contains an invoke instruction
231 bool heapMemOp; // Mark mem ops for self verification
232 bool usesLinkRegister; // For self-verification only
233 bool methodTraceSupport; // For TraceView profiling
234 struct RegisterPool* regPool;
235 int optRound; // round number to tell an LIR's age
236 OatInstructionSetType instructionSet;
237 /* Number of total regs used in the whole cUnit after SSA transformation */
238 int numSSARegs;
239 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
240 GrowableList* ssaToDalvikMap;
241
242 /* The following are new data structures to support SSA representations */
243 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
244 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700245 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700246 ArenaBitVector* isConstantV; // length == numSSAReg
247 int* constantValues; // length == numSSAReg
buzbeec0ecd652011-09-25 18:11:54 -0700248 int* phiAliasMap; // length == numSSAReg
249 MIR* phiList;
buzbee67bf8852011-08-17 17:51:35 -0700250
251 /* Map SSA names to location */
252 RegLocation* regLocation;
253 int sequenceNumber;
254
buzbee67bc2362011-10-11 18:08:40 -0700255 /* Keep track of Dalvik vReg to physical register mappings */
256 PromotionMap* promotionMap;
257
buzbee67bf8852011-08-17 17:51:35 -0700258 /*
259 * Set to the Dalvik PC of the switch instruction if it has more than
260 * MAX_CHAINED_SWITCH_CASES cases.
261 */
262 const u2* switchOverflowPad;
263
264 int numReachableBlocks;
265 int numDalvikRegisters; // method->registersSize + inlined
266 BasicBlock* entryBlock;
267 BasicBlock* exitBlock;
268 BasicBlock* curBlock;
269 BasicBlock* nextCodegenBlock; // for extended trace codegen
270 GrowableList dfsOrder;
buzbee5b537102012-01-17 17:33:47 -0800271 GrowableList dfsPostOrder;
buzbee67bf8852011-08-17 17:51:35 -0700272 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700273 GrowableList throwLaunchpads;
buzbeec1f45042011-09-21 16:03:19 -0700274 GrowableList suspendLaunchpads;
buzbee5b537102012-01-17 17:33:47 -0800275 int* iDomList;
buzbee67bf8852011-08-17 17:51:35 -0700276 ArenaBitVector* tryBlockAddr;
277 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
278 ArenaBitVector* tempBlockV;
279 ArenaBitVector* tempDalvikRegisterV;
280 ArenaBitVector* tempSSARegisterV; // numSSARegs
281 bool printSSANames;
282 void* blockLabelList;
283 bool quitLoopMode; // cold path/complex bytecode
284 int preservedRegsUsed; // How many callee save regs used
285 /*
buzbee5ade1d22011-09-09 14:44:52 -0700286 * Frame layout details.
287 * NOTE: for debug support it will be necessary to add a structure
288 * to map the Dalvik virtual registers to the promoted registers.
289 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700290 */
291 int numIns;
292 int numOuts;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800293 int numRegs; // Unlike numDalvikRegisters, does not include ins
buzbeebbaf8942011-10-02 13:08:29 -0700294 int numCoreSpills;
buzbee67bf8852011-08-17 17:51:35 -0700295 int numFPSpills;
296 int numPadding; // # of 4-byte padding cells
297 int regsOffset; // sp-relative offset to beginning of Dalvik regs
298 int insOffset; // sp-relative offset to beginning of Dalvik ins
299 int frameSize;
300 unsigned int coreSpillMask;
301 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700302 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700303 /*
304 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
buzbee03fa2632011-09-20 17:10:57 -0700305 * mechanism to propagate the original Dalvik opcode address to the
buzbee67bf8852011-08-17 17:51:35 -0700306 * associated generated instructions. For the trace compiler, this wasn't
307 * necessary because the interpreter handled all throws and debugging
308 * requests. For now we'll handle this by placing the Dalvik offset
309 * in the CompilationUnit struct before codegen for each instruction.
310 * The low-level LIR creation utilites will pull it from here. Should
311 * be rewritten.
312 */
313 int currentDalvikOffset;
314 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700315 GrowableList fillArrayData;
316 const u2* insns;
317 u4 insnsSize;
buzbee5b537102012-01-17 17:33:47 -0800318 std::map<unsigned int, BasicBlock*> blockMap; // findBlock lookup cache
buzbee67bf8852011-08-17 17:51:35 -0700319} CompilationUnit;
320
321BasicBlock* oatNewBB(BBType blockType, int blockId);
322
323void oatAppendMIR(BasicBlock* bb, MIR* mir);
324
325void oatPrependMIR(BasicBlock* bb, MIR* mir);
326
327void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
328
329void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
330
331void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
332
333void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
334
335/* Debug Utilities */
336void oatDumpCompilationUnit(CompilationUnit* cUnit);
337
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800338} // namespace art
339
buzbee67bf8852011-08-17 17:51:35 -0700340#endif // ART_SRC_COMPILER_COMPILER_IR_H_