blob: ade478f89fc08d63079fd0e714ef3376115c122a [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"
Ian Rogers1bddec32012-02-04 12:27:34 -080021#include "CompilerUtility.h"
buzbeec143c552011-08-20 17:38:58 -070022#include <vector>
buzbee67bf8852011-08-17 17:51:35 -070023
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
buzbee67bf8852011-08-17 17:51:35 -070026typedef enum RegisterClass {
27 kCoreReg,
28 kFPReg,
29 kAnyReg,
30} RegisterClass;
31
32typedef enum RegLocationType {
33 kLocDalvikFrame = 0, // Normal Dalvik register
34 kLocPhysReg,
35 kLocSpill,
36} RegLocationType;
37
buzbee67bc2362011-10-11 18:08:40 -070038typedef struct PromotionMap {
39 RegLocationType coreLocation:3;
40 u1 coreReg;
41 RegLocationType fpLocation:3;
42 u1 fpReg;
43 bool firstInPair;
44} PromotionMap;
45
buzbee67bf8852011-08-17 17:51:35 -070046typedef struct RegLocation {
buzbee67bc2362011-10-11 18:08:40 -070047 RegLocationType location:3;
buzbee67bf8852011-08-17 17:51:35 -070048 unsigned wide:1;
buzbee67bc2362011-10-11 18:08:40 -070049 unsigned defined:1; // Do we know the type?
50 unsigned fp:1; // Floating point?
51 unsigned core:1; // Non-floating point?
52 unsigned highWord:1; // High word of pair?
53 unsigned home:1; // Does this represent the home location?
54 u1 lowReg; // First physical register
55 u1 highReg; // 2nd physical register (if wide)
56 s2 sRegLow; // SSA name for low Dalvik word
buzbee67bf8852011-08-17 17:51:35 -070057} RegLocation;
58
59#define INVALID_SREG (-1)
buzbee3ddc0d12011-10-05 10:36:21 -070060#define INVALID_VREG (0xFFFFU)
buzbee67bc2362011-10-11 18:08:40 -070061#define INVALID_REG (0xFF)
buzbee67bf8852011-08-17 17:51:35 -070062#define INVALID_OFFSET (-1)
63
buzbee99ba9642012-01-25 14:23:14 -080064/*
65 * Some code patterns cause the generation of excessively large
66 * methods - in particular initialization sequences. There isn't much
67 * benefit in optimizing these methods, and the cost can be very high.
68 * We attempt to identify these cases, and avoid performing most dataflow
69 * analysis. Two thresholds are used - one for known initializers and one
buzbee5abfa3e2012-01-31 17:01:43 -080070 * for everything else.
buzbee99ba9642012-01-25 14:23:14 -080071 */
buzbee5abfa3e2012-01-31 17:01:43 -080072#define MANY_BLOCKS_INITIALIZER 1000 /* Threshold for switching dataflow off */
73#define MANY_BLOCKS 4000 /* Non-initializer threshold */
buzbee99ba9642012-01-25 14:23:14 -080074
buzbee67bf8852011-08-17 17:51:35 -070075typedef enum BBType {
76 kEntryBlock,
77 kDalvikByteCode,
78 kExitBlock,
79 kExceptionHandling,
80 kCatchEntry,
81} BBType;
82
83typedef struct LIR {
84 int offset; // Offset of this instruction
85 int dalvikOffset; // Offset of Dalvik opcode
86 struct LIR* next;
87 struct LIR* prev;
88 struct LIR* target;
89} LIR;
90
91enum ExtendedMIROpcode {
92 kMirOpFirst = kNumPackedOpcodes,
93 kMirOpPhi = kMirOpFirst,
94 kMirOpNullNRangeUpCheck,
95 kMirOpNullNRangeDownCheck,
96 kMirOpLowerBound,
97 kMirOpPunt,
98 kMirOpCheckInlinePrediction, // Gen checks for predicted inlining
99 kMirOpLast,
100};
101
102struct SSARepresentation;
103
104typedef enum {
105 kMIRIgnoreNullCheck = 0,
106 kMIRNullCheckOnly,
107 kMIRIgnoreRangeCheck,
108 kMIRRangeCheckOnly,
109 kMIRInlined, // Invoke is inlined (ie dead)
110 kMIRInlinedPred, // Invoke is inlined via prediction
111 kMIRCallee, // Instruction is inlined from callee
buzbeec1f45042011-09-21 16:03:19 -0700112 kMIRIgnoreSuspendCheck,
buzbee67bf8852011-08-17 17:51:35 -0700113} MIROptimizationFlagPositons;
114
115#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
116#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
117#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
118#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
119#define MIR_INLINED (1 << kMIRInlined)
120#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
121#define MIR_CALLEE (1 << kMIRCallee)
buzbeec1f45042011-09-21 16:03:19 -0700122#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
buzbee67bf8852011-08-17 17:51:35 -0700123
124typedef struct CallsiteInfo {
125 const char* classDescriptor;
126 Object* classLoader;
127 const Method* method;
128 LIR* misPredBranchOver;
129} CallsiteInfo;
130
131typedef struct MIR {
132 DecodedInstruction dalvikInsn;
133 unsigned int width;
134 unsigned int offset;
135 struct MIR* prev;
136 struct MIR* next;
137 struct SSARepresentation* ssaRep;
buzbee43a36422011-09-14 14:00:13 -0700138 int optimizationFlags;
buzbee67bf8852011-08-17 17:51:35 -0700139 int seqNum;
140 union {
141 // Used by the inlined insn from the callee to find the mother method
142 const Method* calleeMethod;
143 // Used by the inlined invoke to find the class and method pointers
144 CallsiteInfo* callsiteInfo;
buzbeec0ecd652011-09-25 18:11:54 -0700145 // Used to quickly locate all Phi opcodes
146 struct MIR* phiNext;
buzbee67bf8852011-08-17 17:51:35 -0700147 } meta;
148} MIR;
149
150struct BasicBlockDataFlow;
151
152/* For successorBlockList */
153typedef enum BlockListType {
154 kNotUsed = 0,
155 kCatch,
156 kPackedSwitch,
157 kSparseSwitch,
158} BlockListType;
159
160typedef struct BasicBlock {
161 int id;
buzbee5b537102012-01-17 17:33:47 -0800162 int dfsId;
buzbee67bf8852011-08-17 17:51:35 -0700163 bool visited;
164 bool hidden;
buzbee43a36422011-09-14 14:00:13 -0700165 bool catchEntry;
buzbee67bf8852011-08-17 17:51:35 -0700166 unsigned int startOffset;
167 const Method* containingMethod; // For blocks from the callee
168 BBType blockType;
169 bool needFallThroughBranch; // For blocks ended due to length limit
170 bool isFallThroughFromInvoke; // True means the block needs alignment
171 MIR* firstMIRInsn;
172 MIR* lastMIRInsn;
173 struct BasicBlock* fallThrough;
174 struct BasicBlock* taken;
175 struct BasicBlock* iDom; // Immediate dominator
176 struct BasicBlockDataFlow* dataFlowInfo;
buzbee5abfa3e2012-01-31 17:01:43 -0800177 GrowableList* predecessors;
buzbee67bf8852011-08-17 17:51:35 -0700178 ArenaBitVector* dominators;
179 ArenaBitVector* iDominated; // Set nodes being immediately dominated
180 ArenaBitVector* domFrontier; // Dominance frontier
181 struct { // For one-to-many successors like
182 BlockListType blockListType; // switch and exception handling
183 GrowableList blocks;
184 } successorBlockList;
185} BasicBlock;
186
187/*
188 * The "blocks" field in "successorBlockList" points to an array of
189 * elements with the type "SuccessorBlockInfo".
190 * For catch blocks, key is type index for the exception.
191 * For swtich blocks, key is the case value.
192 */
193typedef struct SuccessorBlockInfo {
194 BasicBlock* block;
195 int key;
196} SuccessorBlockInfo;
197
198struct LoopAnalysis;
199struct RegisterPool;
buzbeeba938cb2012-02-03 14:47:55 -0800200struct ArenaMemBlock;
201struct Memstats;
buzbee67bf8852011-08-17 17:51:35 -0700202
203typedef enum AssemblerStatus {
204 kSuccess,
205 kRetryAll,
206 kRetryHalve
207} AssemblerStatus;
208
buzbee5b537102012-01-17 17:33:47 -0800209#define NOTVISITED (-1)
210
buzbee67bf8852011-08-17 17:51:35 -0700211typedef struct CompilationUnit {
212 int numInsts;
213 int numBlocks;
214 GrowableList blockList;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800215 const Compiler* compiler; // Compiler driving this compiler
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800216 ClassLinker* class_linker; // Linker to resolve fields and methods
217 const DexFile* dex_file; // DexFile containing the method being compiled
218 DexCache* dex_cache; // DexFile's corresponding cache
219 const ClassLoader* class_loader; // compiling method's class loader
Ian Rogersa3760aa2011-11-14 14:32:37 -0800220 uint32_t method_idx; // compiling method's index into method_ids of DexFile
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800221 const DexFile::CodeItem* code_item; // compiling method's DexFile code_item
Ian Rogersa3760aa2011-11-14 14:32:37 -0800222 uint32_t access_flags; // compiling method's access flags
223 const char* shorty; // compiling method's shorty
buzbee67bf8852011-08-17 17:51:35 -0700224 LIR* firstLIRInsn;
225 LIR* lastLIRInsn;
226 LIR* literalList; // Constants
227 LIR* classPointerList; // Relocatable
228 int numClassPointers;
229 LIR* chainCellOffsetLIR;
buzbeece302932011-10-04 14:32:18 -0700230 uint32_t disableOpt; // optControlVector flags
231 uint32_t enableDebug; // debugControlVector flags
buzbee67bf8852011-08-17 17:51:35 -0700232 int headerSize; // bytes before the first code ptr
233 int dataOffset; // starting offset of literal pool
234 int totalSize; // header + code size
235 AssemblerStatus assemblerStatus; // Success or fix and retry
236 int assemblerRetries;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800237 std::vector<uint16_t> codeBuffer;
buzbee4ef76522011-09-08 10:00:32 -0700238 std::vector<uint32_t> mappingTable;
buzbee3ddc0d12011-10-05 10:36:21 -0700239 std::vector<uint16_t> coreVmapTable;
240 std::vector<uint16_t> fpVmapTable;
buzbee44b412b2012-02-04 08:50:53 -0800241 bool genDebugger; // Generate code for debugger
buzbee67bf8852011-08-17 17:51:35 -0700242 bool printMe;
buzbee67bf8852011-08-17 17:51:35 -0700243 bool hasClassLiterals; // Contains class ptrs used as literals
244 bool hasLoop; // Contains a loop
245 bool hasInvoke; // Contains an invoke instruction
246 bool heapMemOp; // Mark mem ops for self verification
247 bool usesLinkRegister; // For self-verification only
248 bool methodTraceSupport; // For TraceView profiling
249 struct RegisterPool* regPool;
250 int optRound; // round number to tell an LIR's age
251 OatInstructionSetType instructionSet;
252 /* Number of total regs used in the whole cUnit after SSA transformation */
253 int numSSARegs;
254 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
255 GrowableList* ssaToDalvikMap;
256
257 /* The following are new data structures to support SSA representations */
258 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
259 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700260 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700261 ArenaBitVector* isConstantV; // length == numSSAReg
262 int* constantValues; // length == numSSAReg
buzbeec0ecd652011-09-25 18:11:54 -0700263 int* phiAliasMap; // length == numSSAReg
264 MIR* phiList;
buzbee67bf8852011-08-17 17:51:35 -0700265
266 /* Map SSA names to location */
267 RegLocation* regLocation;
268 int sequenceNumber;
269
buzbee67bc2362011-10-11 18:08:40 -0700270 /* Keep track of Dalvik vReg to physical register mappings */
271 PromotionMap* promotionMap;
272
buzbee67bf8852011-08-17 17:51:35 -0700273 /*
274 * Set to the Dalvik PC of the switch instruction if it has more than
275 * MAX_CHAINED_SWITCH_CASES cases.
276 */
277 const u2* switchOverflowPad;
278
279 int numReachableBlocks;
280 int numDalvikRegisters; // method->registersSize + inlined
281 BasicBlock* entryBlock;
282 BasicBlock* exitBlock;
283 BasicBlock* curBlock;
284 BasicBlock* nextCodegenBlock; // for extended trace codegen
285 GrowableList dfsOrder;
buzbee5b537102012-01-17 17:33:47 -0800286 GrowableList dfsPostOrder;
buzbee67bf8852011-08-17 17:51:35 -0700287 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700288 GrowableList throwLaunchpads;
buzbeec1f45042011-09-21 16:03:19 -0700289 GrowableList suspendLaunchpads;
buzbee5b537102012-01-17 17:33:47 -0800290 int* iDomList;
buzbee67bf8852011-08-17 17:51:35 -0700291 ArenaBitVector* tryBlockAddr;
292 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
293 ArenaBitVector* tempBlockV;
294 ArenaBitVector* tempDalvikRegisterV;
295 ArenaBitVector* tempSSARegisterV; // numSSARegs
296 bool printSSANames;
297 void* blockLabelList;
298 bool quitLoopMode; // cold path/complex bytecode
299 int preservedRegsUsed; // How many callee save regs used
300 /*
buzbee5ade1d22011-09-09 14:44:52 -0700301 * Frame layout details.
302 * NOTE: for debug support it will be necessary to add a structure
303 * to map the Dalvik virtual registers to the promoted registers.
304 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700305 */
306 int numIns;
307 int numOuts;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800308 int numRegs; // Unlike numDalvikRegisters, does not include ins
buzbeebbaf8942011-10-02 13:08:29 -0700309 int numCoreSpills;
buzbee67bf8852011-08-17 17:51:35 -0700310 int numFPSpills;
311 int numPadding; // # of 4-byte padding cells
312 int regsOffset; // sp-relative offset to beginning of Dalvik regs
313 int insOffset; // sp-relative offset to beginning of Dalvik ins
314 int frameSize;
315 unsigned int coreSpillMask;
316 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700317 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700318 /*
319 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
buzbee03fa2632011-09-20 17:10:57 -0700320 * mechanism to propagate the original Dalvik opcode address to the
buzbee67bf8852011-08-17 17:51:35 -0700321 * associated generated instructions. For the trace compiler, this wasn't
322 * necessary because the interpreter handled all throws and debugging
323 * requests. For now we'll handle this by placing the Dalvik offset
324 * in the CompilationUnit struct before codegen for each instruction.
325 * The low-level LIR creation utilites will pull it from here. Should
326 * be rewritten.
327 */
328 int currentDalvikOffset;
329 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700330 GrowableList fillArrayData;
331 const u2* insns;
332 u4 insnsSize;
buzbee99ba9642012-01-25 14:23:14 -0800333 bool disableDataflow; // Skip dataflow analysis if possible
buzbee5b537102012-01-17 17:33:47 -0800334 std::map<unsigned int, BasicBlock*> blockMap; // findBlock lookup cache
buzbee85d8c1e2012-01-27 15:52:35 -0800335 std::map<unsigned int, LIR*> boundaryMap; // boundary lookup cache
buzbee5abfa3e2012-01-31 17:01:43 -0800336 int defCount; // Used to estimate number of SSA names
buzbeeba938cb2012-02-03 14:47:55 -0800337 std::string* compilerMethodMatch;
338 bool compilerFlipMatch;
339 struct ArenaMemBlock* arenaHead;
340 struct ArenaMemBlock* currentArena;
341 int numArenaBlocks;
342 struct Memstats* mstats;
buzbee67bf8852011-08-17 17:51:35 -0700343} CompilationUnit;
344
buzbee5abfa3e2012-01-31 17:01:43 -0800345BasicBlock* oatNewBB(CompilationUnit* cUnit, BBType blockType, int blockId);
buzbee67bf8852011-08-17 17:51:35 -0700346
347void oatAppendMIR(BasicBlock* bb, MIR* mir);
348
349void oatPrependMIR(BasicBlock* bb, MIR* mir);
350
351void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
352
353void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
354
355void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
356
357void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
358
359/* Debug Utilities */
360void oatDumpCompilationUnit(CompilationUnit* cUnit);
361
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800362} // namespace art
363
buzbee67bf8852011-08-17 17:51:35 -0700364#endif // ART_SRC_COMPILER_COMPILER_IR_H_