blob: d04b97c93d30d12b73b59b27acb880e3c31a648f [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;
buzbee67bf8852011-08-17 17:51:35 -0700241 bool printMe;
buzbee67bf8852011-08-17 17:51:35 -0700242 bool hasClassLiterals; // Contains class ptrs used as literals
243 bool hasLoop; // Contains a loop
244 bool hasInvoke; // Contains an invoke instruction
245 bool heapMemOp; // Mark mem ops for self verification
246 bool usesLinkRegister; // For self-verification only
247 bool methodTraceSupport; // For TraceView profiling
248 struct RegisterPool* regPool;
249 int optRound; // round number to tell an LIR's age
250 OatInstructionSetType instructionSet;
251 /* Number of total regs used in the whole cUnit after SSA transformation */
252 int numSSARegs;
253 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
254 GrowableList* ssaToDalvikMap;
255
256 /* The following are new data structures to support SSA representations */
257 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
258 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700259 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700260 ArenaBitVector* isConstantV; // length == numSSAReg
261 int* constantValues; // length == numSSAReg
buzbeec0ecd652011-09-25 18:11:54 -0700262 int* phiAliasMap; // length == numSSAReg
263 MIR* phiList;
buzbee67bf8852011-08-17 17:51:35 -0700264
265 /* Map SSA names to location */
266 RegLocation* regLocation;
267 int sequenceNumber;
268
buzbee67bc2362011-10-11 18:08:40 -0700269 /* Keep track of Dalvik vReg to physical register mappings */
270 PromotionMap* promotionMap;
271
buzbee67bf8852011-08-17 17:51:35 -0700272 /*
273 * Set to the Dalvik PC of the switch instruction if it has more than
274 * MAX_CHAINED_SWITCH_CASES cases.
275 */
276 const u2* switchOverflowPad;
277
278 int numReachableBlocks;
279 int numDalvikRegisters; // method->registersSize + inlined
280 BasicBlock* entryBlock;
281 BasicBlock* exitBlock;
282 BasicBlock* curBlock;
283 BasicBlock* nextCodegenBlock; // for extended trace codegen
284 GrowableList dfsOrder;
buzbee5b537102012-01-17 17:33:47 -0800285 GrowableList dfsPostOrder;
buzbee67bf8852011-08-17 17:51:35 -0700286 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700287 GrowableList throwLaunchpads;
buzbeec1f45042011-09-21 16:03:19 -0700288 GrowableList suspendLaunchpads;
buzbee5b537102012-01-17 17:33:47 -0800289 int* iDomList;
buzbee67bf8852011-08-17 17:51:35 -0700290 ArenaBitVector* tryBlockAddr;
291 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
292 ArenaBitVector* tempBlockV;
293 ArenaBitVector* tempDalvikRegisterV;
294 ArenaBitVector* tempSSARegisterV; // numSSARegs
295 bool printSSANames;
296 void* blockLabelList;
297 bool quitLoopMode; // cold path/complex bytecode
298 int preservedRegsUsed; // How many callee save regs used
299 /*
buzbee5ade1d22011-09-09 14:44:52 -0700300 * Frame layout details.
301 * NOTE: for debug support it will be necessary to add a structure
302 * to map the Dalvik virtual registers to the promoted registers.
303 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700304 */
305 int numIns;
306 int numOuts;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800307 int numRegs; // Unlike numDalvikRegisters, does not include ins
buzbeebbaf8942011-10-02 13:08:29 -0700308 int numCoreSpills;
buzbee67bf8852011-08-17 17:51:35 -0700309 int numFPSpills;
310 int numPadding; // # of 4-byte padding cells
311 int regsOffset; // sp-relative offset to beginning of Dalvik regs
312 int insOffset; // sp-relative offset to beginning of Dalvik ins
313 int frameSize;
314 unsigned int coreSpillMask;
315 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700316 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700317 /*
318 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
buzbee03fa2632011-09-20 17:10:57 -0700319 * mechanism to propagate the original Dalvik opcode address to the
buzbee67bf8852011-08-17 17:51:35 -0700320 * associated generated instructions. For the trace compiler, this wasn't
321 * necessary because the interpreter handled all throws and debugging
322 * requests. For now we'll handle this by placing the Dalvik offset
323 * in the CompilationUnit struct before codegen for each instruction.
324 * The low-level LIR creation utilites will pull it from here. Should
325 * be rewritten.
326 */
327 int currentDalvikOffset;
328 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700329 GrowableList fillArrayData;
330 const u2* insns;
331 u4 insnsSize;
buzbee99ba9642012-01-25 14:23:14 -0800332 bool disableDataflow; // Skip dataflow analysis if possible
buzbee5b537102012-01-17 17:33:47 -0800333 std::map<unsigned int, BasicBlock*> blockMap; // findBlock lookup cache
buzbee85d8c1e2012-01-27 15:52:35 -0800334 std::map<unsigned int, LIR*> boundaryMap; // boundary lookup cache
buzbee5abfa3e2012-01-31 17:01:43 -0800335 int defCount; // Used to estimate number of SSA names
buzbeeba938cb2012-02-03 14:47:55 -0800336 std::string* compilerMethodMatch;
337 bool compilerFlipMatch;
338 struct ArenaMemBlock* arenaHead;
339 struct ArenaMemBlock* currentArena;
340 int numArenaBlocks;
341 struct Memstats* mstats;
buzbee67bf8852011-08-17 17:51:35 -0700342} CompilationUnit;
343
buzbee5abfa3e2012-01-31 17:01:43 -0800344BasicBlock* oatNewBB(CompilationUnit* cUnit, BBType blockType, int blockId);
buzbee67bf8852011-08-17 17:51:35 -0700345
346void oatAppendMIR(BasicBlock* bb, MIR* mir);
347
348void oatPrependMIR(BasicBlock* bb, MIR* mir);
349
350void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
351
352void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
353
354void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
355
356void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
357
358/* Debug Utilities */
359void oatDumpCompilationUnit(CompilationUnit* cUnit);
360
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800361} // namespace art
362
buzbee67bf8852011-08-17 17:51:35 -0700363#endif // ART_SRC_COMPILER_COMPILER_IR_H_