blob: 453ccdf556c850145d152cfe63f07ff567eb91ed [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
Elliott Hughesa0e18062012-04-13 15:59:59 -070020#include <vector>
21
buzbee67bf8852011-08-17 17:51:35 -070022#include "codegen/Optimizer.h"
Ian Rogers1bddec32012-02-04 12:27:34 -080023#include "CompilerUtility.h"
buzbee31a4a6f2012-02-28 15:36:15 -080024#include "oat_compilation_unit.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025#include "safe_map.h"
buzbee2cfc6392012-05-07 14:51:40 -070026#if defined(ART_USE_QUICK_COMPILER)
27#include "greenland/ir_builder.h"
28#include "llvm/Module.h"
29#endif
buzbee67bf8852011-08-17 17:51:35 -070030
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080031namespace art {
32
buzbee31a4a6f2012-02-28 15:36:15 -080033#define SLOW_FIELD_PATH (cUnit->enableDebug & (1 << kDebugSlowFieldPath))
34#define SLOW_INVOKE_PATH (cUnit->enableDebug & (1 << kDebugSlowInvokePath))
35#define SLOW_STRING_PATH (cUnit->enableDebug & (1 << kDebugSlowStringPath))
36#define SLOW_TYPE_PATH (cUnit->enableDebug & (1 << kDebugSlowTypePath))
37#define EXERCISE_SLOWEST_FIELD_PATH (cUnit->enableDebug & \
Bill Buzbeea114add2012-05-03 15:00:40 -070038 (1 << kDebugSlowestFieldPath))
buzbee31a4a6f2012-02-28 15:36:15 -080039#define EXERCISE_SLOWEST_STRING_PATH (cUnit->enableDebug & \
Bill Buzbeea114add2012-05-03 15:00:40 -070040 (1 << kDebugSlowestStringPath))
buzbee31a4a6f2012-02-28 15:36:15 -080041#define EXERCISE_RESOLVE_METHOD (cUnit->enableDebug & \
Bill Buzbeea114add2012-05-03 15:00:40 -070042 (1 << kDebugExerciseResolveMethod))
buzbee31a4a6f2012-02-28 15:36:15 -080043
Elliott Hughes719ace42012-03-09 18:06:03 -080044enum RegisterClass {
Bill Buzbeea114add2012-05-03 15:00:40 -070045 kCoreReg,
46 kFPReg,
47 kAnyReg,
Elliott Hughes719ace42012-03-09 18:06:03 -080048};
buzbee67bf8852011-08-17 17:51:35 -070049
Elliott Hughes719ace42012-03-09 18:06:03 -080050enum RegLocationType {
Bill Buzbeea114add2012-05-03 15:00:40 -070051 kLocDalvikFrame = 0, // Normal Dalvik register
52 kLocPhysReg,
53 kLocCompilerTemp,
54 kLocInvalid
Elliott Hughes719ace42012-03-09 18:06:03 -080055};
buzbee67bf8852011-08-17 17:51:35 -070056
Elliott Hughes719ace42012-03-09 18:06:03 -080057struct PromotionMap {
Bill Buzbeea114add2012-05-03 15:00:40 -070058 RegLocationType coreLocation:3;
59 u1 coreReg;
60 RegLocationType fpLocation:3;
61 u1 fpReg;
62 bool firstInPair;
Elliott Hughes719ace42012-03-09 18:06:03 -080063};
buzbee67bc2362011-10-11 18:08:40 -070064
Elliott Hughes719ace42012-03-09 18:06:03 -080065struct RegLocation {
Bill Buzbeea114add2012-05-03 15:00:40 -070066 RegLocationType location:3;
67 unsigned wide:1;
68 unsigned defined:1; // Do we know the type?
buzbee2cfc6392012-05-07 14:51:40 -070069 unsigned isConst:1; // Constant, value in cUnit->constantValues[]
Bill Buzbeea114add2012-05-03 15:00:40 -070070 unsigned fp:1; // Floating point?
71 unsigned core:1; // Non-floating point?
buzbeebff24652012-05-06 16:22:05 -070072 unsigned ref:1; // Something GC cares about
Bill Buzbeea114add2012-05-03 15:00:40 -070073 unsigned highWord:1; // High word of pair?
74 unsigned home:1; // Does this represent the home location?
75 u1 lowReg; // First physical register
76 u1 highReg; // 2nd physical register (if wide)
77 int32_t sRegLow; // SSA name for low Dalvik word
buzbee2cfc6392012-05-07 14:51:40 -070078 int32_t origSReg; // TODO: remove after Bitcode gen complete
79 // and consolodate usage w/ sRegLow
buzbeee1965672012-03-11 18:39:19 -070080};
81
82struct CompilerTemp {
Bill Buzbeea114add2012-05-03 15:00:40 -070083 int sReg;
84 ArenaBitVector* bv;
Elliott Hughes719ace42012-03-09 18:06:03 -080085};
buzbee67bf8852011-08-17 17:51:35 -070086
buzbee3b3dbdd2012-06-13 13:39:34 -070087struct CallInfo {
buzbee15bf9802012-06-12 17:49:27 -070088 int numArgWords; // Note: word count, not arg count
89 RegLocation* args; // One for each word of arguments
90 RegLocation result; // Eventual target of MOVE_RESULT
91 int optFlags;
92 InvokeType type;
93 uint32_t dexIdx;
buzbee3b3dbdd2012-06-13 13:39:34 -070094 uint32_t index; // Method idx for invokes, type idx for FilledNewArray
buzbee15bf9802012-06-12 17:49:27 -070095 uintptr_t directCode;
96 uintptr_t directMethod;
97 RegLocation target; // Target of following move_result
98 bool skipThis;
99 bool isRange;
100 int offset; // Dalvik offset
101};
102
buzbeee3acd072012-02-25 17:03:10 -0800103 /*
104 * Data structure tracking the mapping between a Dalvik register (pair) and a
105 * native register (pair). The idea is to reuse the previously loaded value
106 * if possible, otherwise to keep the value in a native register as long as
107 * possible.
108 */
Elliott Hughes719ace42012-03-09 18:06:03 -0800109struct RegisterInfo {
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 int reg; // Reg number
111 bool inUse; // Has it been allocated?
112 bool isTemp; // Can allocate as temp?
113 bool pair; // Part of a register pair?
114 int partner; // If pair, other reg of pair
115 bool live; // Is there an associated SSA name?
116 bool dirty; // If live, is it dirty?
117 int sReg; // Name of live value
118 LIR *defStart; // Starting inst in last def sequence
119 LIR *defEnd; // Ending inst in last def sequence
Elliott Hughes719ace42012-03-09 18:06:03 -0800120};
buzbeee3acd072012-02-25 17:03:10 -0800121
Elliott Hughes719ace42012-03-09 18:06:03 -0800122struct RegisterPool {
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 int numCoreRegs;
124 RegisterInfo *coreRegs;
125 int nextCoreReg;
126 int numFPRegs;
127 RegisterInfo *FPRegs;
128 int nextFPReg;
Elliott Hughes719ace42012-03-09 18:06:03 -0800129};
buzbeee3acd072012-02-25 17:03:10 -0800130
buzbee67bf8852011-08-17 17:51:35 -0700131#define INVALID_SREG (-1)
buzbee3ddc0d12011-10-05 10:36:21 -0700132#define INVALID_VREG (0xFFFFU)
buzbee67bc2362011-10-11 18:08:40 -0700133#define INVALID_REG (0xFF)
buzbee67bf8852011-08-17 17:51:35 -0700134#define INVALID_OFFSET (-1)
135
buzbeee1965672012-03-11 18:39:19 -0700136/* SSA encodings for special registers */
buzbee9c044ce2012-03-18 13:24:07 -0700137#define SSA_METHOD_BASEREG (-2)
buzbeee1965672012-03-11 18:39:19 -0700138/* First compiler temp basereg, grows smaller */
buzbee9c044ce2012-03-18 13:24:07 -0700139#define SSA_CTEMP_BASEREG (SSA_METHOD_BASEREG - 1)
buzbee2cfc6392012-05-07 14:51:40 -0700140/* Max SSA name length */
141#define SSA_NAME_MAX 16
buzbeee1965672012-03-11 18:39:19 -0700142
buzbee99ba9642012-01-25 14:23:14 -0800143/*
144 * Some code patterns cause the generation of excessively large
145 * methods - in particular initialization sequences. There isn't much
146 * benefit in optimizing these methods, and the cost can be very high.
147 * We attempt to identify these cases, and avoid performing most dataflow
148 * analysis. Two thresholds are used - one for known initializers and one
buzbee5abfa3e2012-01-31 17:01:43 -0800149 * for everything else.
buzbee99ba9642012-01-25 14:23:14 -0800150 */
buzbee5abfa3e2012-01-31 17:01:43 -0800151#define MANY_BLOCKS_INITIALIZER 1000 /* Threshold for switching dataflow off */
152#define MANY_BLOCKS 4000 /* Non-initializer threshold */
buzbee99ba9642012-01-25 14:23:14 -0800153
Elliott Hughes719ace42012-03-09 18:06:03 -0800154enum BBType {
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 kEntryBlock,
156 kDalvikByteCode,
157 kExitBlock,
158 kExceptionHandling,
159 kCatchEntry,
Elliott Hughes719ace42012-03-09 18:06:03 -0800160};
buzbee67bf8852011-08-17 17:51:35 -0700161
buzbee31a4a6f2012-02-28 15:36:15 -0800162/* Utility macros to traverse the LIR list */
163#define NEXT_LIR(lir) (lir->next)
164#define PREV_LIR(lir) (lir->prev)
165
166#define NEXT_LIR_LVALUE(lir) (lir)->next
167#define PREV_LIR_LVALUE(lir) (lir)->prev
168
Elliott Hughes719ace42012-03-09 18:06:03 -0800169struct LIR {
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 int offset; // Offset of this instruction
171 int dalvikOffset; // Offset of Dalvik opcode
172 LIR* next;
173 LIR* prev;
174 LIR* target;
175 int opcode;
176 int operands[5]; // [0..4] = [dest, src1, src2, extra, extra2]
177 struct {
178 bool isNop:1; // LIR is optimized away
179 bool pcRelFixup:1; // May need pc-relative fixup
180 unsigned int age:4; // default is 0, set lazily by the optimizer
181 unsigned int size:5; // in bytes
182 unsigned int unused:21;
183 } flags;
184 int aliasInfo; // For Dalvik register & litpool disambiguation
185 u8 useMask; // Resource mask for use
186 u8 defMask; // Resource mask for def
Elliott Hughes719ace42012-03-09 18:06:03 -0800187};
buzbee67bf8852011-08-17 17:51:35 -0700188
189enum ExtendedMIROpcode {
Bill Buzbeea114add2012-05-03 15:00:40 -0700190 kMirOpFirst = kNumPackedOpcodes,
191 kMirOpPhi = kMirOpFirst,
192 kMirOpCopy,
193 kMirOpFusedCmplFloat,
194 kMirOpFusedCmpgFloat,
195 kMirOpFusedCmplDouble,
196 kMirOpFusedCmpgDouble,
197 kMirOpFusedCmpLong,
198 kMirOpNop,
Brian Carlstrom937b73e2012-08-01 16:09:16 -0700199 kMirOpNullNRangeUpCheck,
200 kMirOpNullNRangeDownCheck,
201 kMirOpLowerBound,
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 kMirOpLast,
buzbee67bf8852011-08-17 17:51:35 -0700203};
204
205struct SSARepresentation;
206
Elliott Hughes719ace42012-03-09 18:06:03 -0800207enum MIROptimizationFlagPositons {
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 kMIRIgnoreNullCheck = 0,
209 kMIRNullCheckOnly,
210 kMIRIgnoreRangeCheck,
211 kMIRRangeCheckOnly,
212 kMIRInlined, // Invoke is inlined (ie dead)
213 kMIRInlinedPred, // Invoke is inlined via prediction
214 kMIRCallee, // Instruction is inlined from callee
215 kMIRIgnoreSuspendCheck,
216 kMIRDup,
217 kMIRMark, // Temporary node mark
Elliott Hughes719ace42012-03-09 18:06:03 -0800218};
buzbee67bf8852011-08-17 17:51:35 -0700219
220#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
221#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
222#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
223#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
224#define MIR_INLINED (1 << kMIRInlined)
225#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
226#define MIR_CALLEE (1 << kMIRCallee)
buzbeec1f45042011-09-21 16:03:19 -0700227#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
buzbeee1965672012-03-11 18:39:19 -0700228#define MIR_DUP (1 << kMIRDup)
buzbee239c4e72012-03-16 08:42:29 -0700229#define MIR_MARK (1 << kMIRMark)
buzbee67bf8852011-08-17 17:51:35 -0700230
Elliott Hughes719ace42012-03-09 18:06:03 -0800231struct CallsiteInfo {
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 const char* classDescriptor;
233 Object* classLoader;
234 const Method* method;
235 LIR* misPredBranchOver;
Elliott Hughes719ace42012-03-09 18:06:03 -0800236};
buzbee67bf8852011-08-17 17:51:35 -0700237
Elliott Hughes719ace42012-03-09 18:06:03 -0800238struct MIR {
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 DecodedInstruction dalvikInsn;
240 unsigned int width;
241 unsigned int offset;
242 MIR* prev;
243 MIR* next;
244 SSARepresentation* ssaRep;
245 int optimizationFlags;
246 int seqNum;
247 union {
Brian Carlstrom937b73e2012-08-01 16:09:16 -0700248 // Used by the inlined insn from the callee to find the mother method
249 const Method* calleeMethod;
250 // Used by the inlined invoke to find the class and method pointers
251 CallsiteInfo* callsiteInfo;
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 // Used to quickly locate all Phi opcodes
253 MIR* phiNext;
254 } meta;
Elliott Hughes719ace42012-03-09 18:06:03 -0800255};
buzbee67bf8852011-08-17 17:51:35 -0700256
257struct BasicBlockDataFlow;
258
259/* For successorBlockList */
Elliott Hughes719ace42012-03-09 18:06:03 -0800260enum BlockListType {
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 kNotUsed = 0,
262 kCatch,
263 kPackedSwitch,
264 kSparseSwitch,
Elliott Hughes719ace42012-03-09 18:06:03 -0800265};
buzbee67bf8852011-08-17 17:51:35 -0700266
Elliott Hughes719ace42012-03-09 18:06:03 -0800267struct BasicBlock {
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 int id;
269 int dfsId;
270 bool visited;
271 bool hidden;
272 bool catchEntry;
273 bool fallThroughTarget; // Reached via fallthrough
buzbee2cfc6392012-05-07 14:51:40 -0700274#if defined(ART_USE_QUICK_COMPILER)
275 bool hasReturn;
276#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 uint16_t startOffset;
278 uint16_t nestingDepth;
279 const Method* containingMethod; // For blocks from the callee
280 BBType blockType;
Brian Carlstrom937b73e2012-08-01 16:09:16 -0700281 bool needFallThroughBranch; // For blocks ended due to length limit
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 bool isFallThroughFromInvoke; // True means the block needs alignment
283 MIR* firstMIRInsn;
284 MIR* lastMIRInsn;
285 BasicBlock* fallThrough;
286 BasicBlock* taken;
287 BasicBlock* iDom; // Immediate dominator
288 BasicBlockDataFlow* dataFlowInfo;
289 GrowableList* predecessors;
290 ArenaBitVector* dominators;
291 ArenaBitVector* iDominated; // Set nodes being immediately dominated
292 ArenaBitVector* domFrontier; // Dominance frontier
293 struct { // For one-to-many successors like
294 BlockListType blockListType; // switch and exception handling
295 GrowableList blocks;
296 } successorBlockList;
Elliott Hughes719ace42012-03-09 18:06:03 -0800297};
buzbee67bf8852011-08-17 17:51:35 -0700298
299/*
300 * The "blocks" field in "successorBlockList" points to an array of
301 * elements with the type "SuccessorBlockInfo".
302 * For catch blocks, key is type index for the exception.
303 * For swtich blocks, key is the case value.
304 */
Elliott Hughes719ace42012-03-09 18:06:03 -0800305struct SuccessorBlockInfo {
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 BasicBlock* block;
307 int key;
Elliott Hughes719ace42012-03-09 18:06:03 -0800308};
buzbee67bf8852011-08-17 17:51:35 -0700309
310struct LoopAnalysis;
311struct RegisterPool;
buzbeeba938cb2012-02-03 14:47:55 -0800312struct ArenaMemBlock;
313struct Memstats;
buzbee67bf8852011-08-17 17:51:35 -0700314
Elliott Hughes719ace42012-03-09 18:06:03 -0800315enum AssemblerStatus {
Bill Buzbeea114add2012-05-03 15:00:40 -0700316 kSuccess,
317 kRetryAll,
318 kRetryHalve
Elliott Hughes719ace42012-03-09 18:06:03 -0800319};
buzbee67bf8852011-08-17 17:51:35 -0700320
buzbee5b537102012-01-17 17:33:47 -0800321#define NOTVISITED (-1)
322
Elliott Hughes719ace42012-03-09 18:06:03 -0800323struct CompilationUnit {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700324 CompilationUnit()
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 : numBlocks(0),
326 compiler(NULL),
327 class_linker(NULL),
328 dex_file(NULL),
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 class_loader(NULL),
330 method_idx(0),
331 code_item(NULL),
332 access_flags(0),
333 shorty(NULL),
334 firstLIRInsn(NULL),
335 lastLIRInsn(NULL),
336 literalList(NULL),
337 methodLiteralList(NULL),
338 codeLiteralList(NULL),
339 classPointerList(NULL),
340 numClassPointers(0),
341 chainCellOffsetLIR(NULL),
342 disableOpt(0),
343 enableDebug(0),
344 headerSize(0),
345 dataOffset(0),
346 totalSize(0),
347 assemblerStatus(kSuccess),
348 assemblerRetries(0),
349 genDebugger(false),
350 printMe(false),
351 hasClassLiterals(false),
352 hasLoop(false),
353 hasInvoke(false),
354 heapMemOp(false),
355 qdMode(false),
356 usesLinkRegister(false),
357 methodTraceSupport(false),
358 regPool(NULL),
359 optRound(0),
360 instructionSet(kNone),
361 numSSARegs(0),
362 ssaBaseVRegs(NULL),
363 ssaSubscripts(NULL),
buzbee2cfc6392012-05-07 14:51:40 -0700364 ssaStrings(NULL),
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 vRegToSSAMap(NULL),
366 SSALastDefs(NULL),
367 isConstantV(NULL),
368 constantValues(NULL),
369 phiAliasMap(NULL),
370 phiList(NULL),
371 regLocation(NULL),
372 sequenceNumber(0),
373 promotionMap(NULL),
374 methodSReg(0),
375 switchOverflowPad(NULL),
376 numReachableBlocks(0),
377 numDalvikRegisters(0),
378 entryBlock(NULL),
379 exitBlock(NULL),
380 curBlock(NULL),
381 nextCodegenBlock(NULL),
382 iDomList(NULL),
383 tryBlockAddr(NULL),
384 defBlockMatrix(NULL),
385 tempBlockV(NULL),
386 tempDalvikRegisterV(NULL),
387 tempSSARegisterV(NULL),
buzbee2cfc6392012-05-07 14:51:40 -0700388 tempSSABlockIdV(NULL),
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 printSSANames(false),
390 blockLabelList(NULL),
391 quitLoopMode(false),
392 preservedRegsUsed(0),
393 numIns(0),
394 numOuts(0),
395 numRegs(0),
396 numCoreSpills(0),
397 numFPSpills(0),
398 numCompilerTemps(0),
399 frameSize(0),
400 coreSpillMask(0U),
401 fpSpillMask(0U),
402 attrs(0U),
403 currentDalvikOffset(0),
404 insns(NULL),
405 insnsSize(0U),
406 disableDataflow(false),
407 defCount(0),
408 compilerFlipMatch(false),
409 arenaHead(NULL),
410 currentArena(NULL),
411 numArenaBlocks(0),
412 mstats(NULL),
buzbee2cfc6392012-05-07 14:51:40 -0700413#if defined(ART_USE_QUICK_COMPILER)
414 genBitcode(false),
415 context(NULL),
416 module(NULL),
417 func(NULL),
418 intrinsic_helper(NULL),
419 irb(NULL),
420 placeholderBB(NULL),
421 entryBB(NULL),
buzbee4be777b2012-07-12 14:38:18 -0700422 entryTargetBB(NULL),
buzbee2cfc6392012-05-07 14:51:40 -0700423 tempName(0),
buzbeeb03f4872012-06-11 15:22:11 -0700424 requireShadowFrame(false),
425 numShadowFrameEntries(0),
426 shadowMap(NULL),
Elliott Hughese52e49b2012-04-02 16:05:44 -0700427#endif
buzbee2cfc6392012-05-07 14:51:40 -0700428#ifndef NDEBUG
429 liveSReg(0),
430#endif
431 opcodeCount(NULL) {}
Elliott Hughese52e49b2012-04-02 16:05:44 -0700432
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 int numBlocks;
434 GrowableList blockList;
435 Compiler* compiler; // Compiler driving this compiler
436 ClassLinker* class_linker; // Linker to resolve fields and methods
437 const DexFile* dex_file; // DexFile containing the method being compiled
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 jobject class_loader; // compiling method's class loader
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 uint32_t method_idx; // compiling method's index into method_ids of DexFile
440 const DexFile::CodeItem* code_item; // compiling method's DexFile code_item
441 uint32_t access_flags; // compiling method's access flags
442 const char* shorty; // compiling method's shorty
443 LIR* firstLIRInsn;
444 LIR* lastLIRInsn;
445 LIR* literalList; // Constants
446 LIR* methodLiteralList; // Method literals requiring patching
447 LIR* codeLiteralList; // Code literals requiring patching
448 LIR* classPointerList; // Relocatable
449 int numClassPointers;
450 LIR* chainCellOffsetLIR;
451 uint32_t disableOpt; // optControlVector flags
452 uint32_t enableDebug; // debugControlVector flags
453 int headerSize; // bytes before the first code ptr
454 int dataOffset; // starting offset of literal pool
455 int totalSize; // header + code size
456 AssemblerStatus assemblerStatus; // Success or fix and retry
457 int assemblerRetries;
458 std::vector<uint8_t> codeBuffer;
459 std::vector<uint32_t> mappingTable;
460 std::vector<uint16_t> coreVmapTable;
461 std::vector<uint16_t> fpVmapTable;
462 bool genDebugger; // Generate code for debugger
463 bool printMe;
464 bool hasClassLiterals; // Contains class ptrs used as literals
465 bool hasLoop; // Contains a loop
466 bool hasInvoke; // Contains an invoke instruction
467 bool heapMemOp; // Mark mem ops for self verification
468 bool qdMode; // Compile for code size/compile time
469 bool usesLinkRegister; // For self-verification only
470 bool methodTraceSupport; // For TraceView profiling
471 RegisterPool* regPool;
472 int optRound; // round number to tell an LIR's age
473 InstructionSet instructionSet;
474 /* Number of total regs used in the whole cUnit after SSA transformation */
475 int numSSARegs;
476 /* Map SSA reg i to the base virtual register/subscript */
477 GrowableList* ssaBaseVRegs;
478 GrowableList* ssaSubscripts;
buzbee2cfc6392012-05-07 14:51:40 -0700479 GrowableList* ssaStrings;
buzbee67bf8852011-08-17 17:51:35 -0700480
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 /* The following are new data structures to support SSA representations */
482 /* Map original Dalvik virtual reg i to the current SSA name */
483 int* vRegToSSAMap; // length == method->registersSize
484 int* SSALastDefs; // length == method->registersSize
485 ArenaBitVector* isConstantV; // length == numSSAReg
486 int* constantValues; // length == numSSAReg
487 int* phiAliasMap; // length == numSSAReg
488 MIR* phiList;
buzbee67bf8852011-08-17 17:51:35 -0700489
Bill Buzbeea114add2012-05-03 15:00:40 -0700490 /* Use counts of ssa names */
491 GrowableList useCounts; // Weighted by nesting depth
492 GrowableList rawUseCounts; // Not weighted
buzbee239c4e72012-03-16 08:42:29 -0700493
Bill Buzbeea114add2012-05-03 15:00:40 -0700494 /* Optimization support */
495 GrowableList loopHeaders;
buzbee239c4e72012-03-16 08:42:29 -0700496
Bill Buzbeea114add2012-05-03 15:00:40 -0700497 /* Map SSA names to location */
498 RegLocation* regLocation;
499 int sequenceNumber;
buzbee67bf8852011-08-17 17:51:35 -0700500
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 /* Keep track of Dalvik vReg to physical register mappings */
502 PromotionMap* promotionMap;
buzbee67bc2362011-10-11 18:08:40 -0700503
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 /* SSA name for Method* */
505 int methodSReg;
buzbeead8f15e2012-06-18 14:49:45 -0700506 RegLocation methodLoc; // Describes location of method*
buzbeee1965672012-03-11 18:39:19 -0700507
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 /*
509 * Set to the Dalvik PC of the switch instruction if it has more than
510 * MAX_CHAINED_SWITCH_CASES cases.
511 */
512 const u2* switchOverflowPad;
buzbee67bf8852011-08-17 17:51:35 -0700513
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 int numReachableBlocks;
515 int numDalvikRegisters; // method->registersSize
516 BasicBlock* entryBlock;
517 BasicBlock* exitBlock;
518 BasicBlock* curBlock;
519 BasicBlock* nextCodegenBlock; // for extended trace codegen
520 GrowableList dfsOrder;
521 GrowableList dfsPostOrder;
522 GrowableList domPostOrderTraversal;
523 GrowableList throwLaunchpads;
524 GrowableList suspendLaunchpads;
525 GrowableList intrinsicLaunchpads;
526 GrowableList compilerTemps;
527 int* iDomList;
528 ArenaBitVector* tryBlockAddr;
529 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
530 ArenaBitVector* tempBlockV;
531 ArenaBitVector* tempDalvikRegisterV;
532 ArenaBitVector* tempSSARegisterV; // numSSARegs
buzbee2cfc6392012-05-07 14:51:40 -0700533 int* tempSSABlockIdV; // working storage for Phi labels
Bill Buzbeea114add2012-05-03 15:00:40 -0700534 bool printSSANames;
buzbeea1da8a52012-07-09 14:00:21 -0700535 LIR* blockLabelList;
Bill Buzbeea114add2012-05-03 15:00:40 -0700536 bool quitLoopMode; // cold path/complex bytecode
537 int preservedRegsUsed; // How many callee save regs used
538 /*
539 * Frame layout details.
540 * NOTE: for debug support it will be necessary to add a structure
541 * to map the Dalvik virtual registers to the promoted registers.
542 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
543 */
544 int numIns;
545 int numOuts;
546 int numRegs; // Unlike numDalvikRegisters, does not include ins
547 int numCoreSpills;
548 int numFPSpills;
549 int numCompilerTemps;
550 int frameSize;
551 unsigned int coreSpillMask;
552 unsigned int fpSpillMask;
553 unsigned int attrs;
554 /*
555 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
556 * mechanism to propagate the original Dalvik opcode address to the
557 * associated generated instructions. For the trace compiler, this wasn't
558 * necessary because the interpreter handled all throws and debugging
559 * requests. For now we'll handle this by placing the Dalvik offset
560 * in the CompilationUnit struct before codegen for each instruction.
561 * The low-level LIR creation utilites will pull it from here. Should
562 * be rewritten.
563 */
564 int currentDalvikOffset;
565 GrowableList switchTables;
566 GrowableList fillArrayData;
567 const u2* insns;
568 u4 insnsSize;
569 bool disableDataflow; // Skip dataflow analysis if possible
570 SafeMap<unsigned int, BasicBlock*> blockMap; // findBlock lookup cache
571 SafeMap<unsigned int, LIR*> boundaryMap; // boundary lookup cache
572 int defCount; // Used to estimate number of SSA names
Elliott Hughese52e49b2012-04-02 16:05:44 -0700573
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 // If non-empty, apply optimizer/debug flags only to matching methods.
575 std::string compilerMethodMatch;
576 // Flips sense of compilerMethodMatch - apply flags if doesn't match.
577 bool compilerFlipMatch;
578 ArenaMemBlock* arenaHead;
579 ArenaMemBlock* currentArena;
580 int numArenaBlocks;
581 Memstats* mstats;
buzbee2cfc6392012-05-07 14:51:40 -0700582#if defined(ART_USE_QUICK_COMPILER)
583 bool genBitcode;
584 llvm::LLVMContext* context;
585 llvm::Module* module;
586 llvm::Function* func;
587 greenland::IntrinsicHelper* intrinsic_helper;
588 greenland::IRBuilder* irb;
589 llvm::BasicBlock* placeholderBB;
590 llvm::BasicBlock* entryBB;
buzbee4be777b2012-07-12 14:38:18 -0700591 llvm::BasicBlock* entryTargetBB;
buzbee2cfc6392012-05-07 14:51:40 -0700592 std::string bitcode_filename;
593 GrowableList llvmValues;
594 int32_t tempName;
595 SafeMap<llvm::BasicBlock*, LIR*> blockToLabelMap; // llvm bb -> LIR label
596 SafeMap<int32_t, llvm::BasicBlock*> idToBlockMap; // block id -> llvm bb
597 SafeMap<llvm::Value*, RegLocation> locMap; // llvm Value to loc rec
buzbeeb03f4872012-06-11 15:22:11 -0700598 bool requireShadowFrame;
599 int numShadowFrameEntries;
600 int* shadowMap;
buzbee2cfc6392012-05-07 14:51:40 -0700601#endif
buzbee3d661942012-03-14 17:37:27 -0700602#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700603 /*
604 * Sanity checking for the register temp tracking. The same ssa
605 * name should never be associated with one temp register per
606 * instruction compilation.
607 */
608 int liveSReg;
buzbee3d661942012-03-14 17:37:27 -0700609#endif
buzbee2cfc6392012-05-07 14:51:40 -0700610 int* opcodeCount; // Count Dalvik opcodes for tuning
Elliott Hughes719ace42012-03-09 18:06:03 -0800611};
buzbee67bf8852011-08-17 17:51:35 -0700612
Elliott Hughes719ace42012-03-09 18:06:03 -0800613enum OpSize {
Bill Buzbeea114add2012-05-03 15:00:40 -0700614 kWord,
615 kLong,
616 kSingle,
617 kDouble,
618 kUnsignedHalf,
619 kSignedHalf,
620 kUnsignedByte,
621 kSignedByte,
Elliott Hughes719ace42012-03-09 18:06:03 -0800622};
buzbeee3acd072012-02-25 17:03:10 -0800623
Elliott Hughes719ace42012-03-09 18:06:03 -0800624enum OpKind {
Bill Buzbeea114add2012-05-03 15:00:40 -0700625 kOpMov,
626 kOpMvn,
627 kOpCmp,
628 kOpLsl,
629 kOpLsr,
630 kOpAsr,
631 kOpRor,
632 kOpNot,
633 kOpAnd,
634 kOpOr,
635 kOpXor,
636 kOpNeg,
637 kOpAdd,
638 kOpAdc,
639 kOpSub,
640 kOpSbc,
641 kOpRsub,
642 kOpMul,
643 kOpDiv,
644 kOpRem,
645 kOpBic,
646 kOpCmn,
647 kOpTst,
648 kOpBkpt,
649 kOpBlx,
650 kOpPush,
651 kOpPop,
652 kOp2Char,
653 kOp2Short,
654 kOp2Byte,
655 kOpCondBr,
656 kOpUncondBr,
657 kOpBx,
658 kOpInvalid,
Elliott Hughes719ace42012-03-09 18:06:03 -0800659};
buzbee31a4a6f2012-02-28 15:36:15 -0800660
Ian Rogers680b1bd2012-03-07 20:18:49 -0800661std::ostream& operator<<(std::ostream& os, const OpKind& kind);
662
Elliott Hughes719ace42012-03-09 18:06:03 -0800663enum ConditionCode {
Bill Buzbeea114add2012-05-03 15:00:40 -0700664 kCondEq, // equal
665 kCondNe, // not equal
666 kCondCs, // carry set (unsigned less than)
667 kCondUlt = kCondCs,
668 kCondCc, // carry clear (unsigned greater than or same)
669 kCondUge = kCondCc,
670 kCondMi, // minus
671 kCondPl, // plus, positive or zero
672 kCondVs, // overflow
673 kCondVc, // no overflow
674 kCondHi, // unsigned greater than
675 kCondLs, // unsigned lower or same
676 kCondGe, // signed greater than or equal
677 kCondLt, // signed less than
678 kCondGt, // signed greater than
679 kCondLe, // signed less than or equal
680 kCondAl, // always
681 kCondNv, // never
Elliott Hughes719ace42012-03-09 18:06:03 -0800682};
buzbee31a4a6f2012-02-28 15:36:15 -0800683
Elliott Hughes719ace42012-03-09 18:06:03 -0800684enum ThrowKind {
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 kThrowNullPointer,
686 kThrowDivZero,
687 kThrowArrayBounds,
688 kThrowVerificationError,
689 kThrowNoSuchMethod,
690 kThrowStackOverflow,
Elliott Hughes719ace42012-03-09 18:06:03 -0800691};
buzbee31a4a6f2012-02-28 15:36:15 -0800692
Elliott Hughes719ace42012-03-09 18:06:03 -0800693struct SwitchTable {
Bill Buzbeea114add2012-05-03 15:00:40 -0700694 int offset;
695 const u2* table; // Original dex table
696 int vaddr; // Dalvik offset of switch opcode
697 LIR* anchor; // Reference instruction for relative offsets
698 LIR** targets; // Array of case targets
Elliott Hughes719ace42012-03-09 18:06:03 -0800699};
buzbee5de34942012-03-01 14:51:57 -0800700
Elliott Hughes719ace42012-03-09 18:06:03 -0800701struct FillArrayData {
Bill Buzbeea114add2012-05-03 15:00:40 -0700702 int offset;
703 const u2* table; // Original dex table
704 int size;
705 int vaddr; // Dalvik offset of FILL_ARRAY_DATA opcode
Elliott Hughes719ace42012-03-09 18:06:03 -0800706};
buzbee5de34942012-03-01 14:51:57 -0800707
buzbee16da88c2012-03-20 10:38:17 -0700708#define MAX_PATTERN_LEN 5
709
710enum SpecialCaseHandler {
Bill Buzbeea114add2012-05-03 15:00:40 -0700711 kNoHandler,
712 kNullMethod,
713 kConstFunction,
714 kIGet,
715 kIGetBoolean,
716 kIGetObject,
717 kIGetByte,
718 kIGetChar,
719 kIGetShort,
720 kIGetWide,
721 kIPut,
722 kIPutBoolean,
723 kIPutObject,
724 kIPutByte,
725 kIPutChar,
726 kIPutShort,
727 kIPutWide,
728 kIdentity,
buzbee16da88c2012-03-20 10:38:17 -0700729};
730
731struct CodePattern {
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 const Instruction::Code opcodes[MAX_PATTERN_LEN];
733 const SpecialCaseHandler handlerCode;
buzbee16da88c2012-03-20 10:38:17 -0700734};
735
736static const CodePattern specialPatterns[] = {
Bill Buzbeea114add2012-05-03 15:00:40 -0700737 {{Instruction::RETURN_VOID}, kNullMethod},
738 {{Instruction::CONST, Instruction::RETURN}, kConstFunction},
739 {{Instruction::CONST_4, Instruction::RETURN}, kConstFunction},
740 {{Instruction::CONST_4, Instruction::RETURN_OBJECT}, kConstFunction},
741 {{Instruction::CONST_16, Instruction::RETURN}, kConstFunction},
742 {{Instruction::IGET, Instruction:: RETURN}, kIGet},
743 {{Instruction::IGET_BOOLEAN, Instruction::RETURN}, kIGetBoolean},
744 {{Instruction::IGET_OBJECT, Instruction::RETURN_OBJECT}, kIGetObject},
745 {{Instruction::IGET_BYTE, Instruction::RETURN}, kIGetByte},
746 {{Instruction::IGET_CHAR, Instruction::RETURN}, kIGetChar},
747 {{Instruction::IGET_SHORT, Instruction::RETURN}, kIGetShort},
748 {{Instruction::IGET_WIDE, Instruction::RETURN_WIDE}, kIGetWide},
749 {{Instruction::IPUT, Instruction::RETURN_VOID}, kIPut},
750 {{Instruction::IPUT_BOOLEAN, Instruction::RETURN_VOID}, kIPutBoolean},
751 {{Instruction::IPUT_OBJECT, Instruction::RETURN_VOID}, kIPutObject},
752 {{Instruction::IPUT_BYTE, Instruction::RETURN_VOID}, kIPutByte},
753 {{Instruction::IPUT_CHAR, Instruction::RETURN_VOID}, kIPutChar},
754 {{Instruction::IPUT_SHORT, Instruction::RETURN_VOID}, kIPutShort},
755 {{Instruction::IPUT_WIDE, Instruction::RETURN_VOID}, kIPutWide},
756 {{Instruction::RETURN}, kIdentity},
757 {{Instruction::RETURN_OBJECT}, kIdentity},
758 {{Instruction::RETURN_WIDE}, kIdentity},
buzbee16da88c2012-03-20 10:38:17 -0700759};
buzbee5de34942012-03-01 14:51:57 -0800760
buzbee5abfa3e2012-01-31 17:01:43 -0800761BasicBlock* oatNewBB(CompilationUnit* cUnit, BBType blockType, int blockId);
buzbee67bf8852011-08-17 17:51:35 -0700762
763void oatAppendMIR(BasicBlock* bb, MIR* mir);
764
765void oatPrependMIR(BasicBlock* bb, MIR* mir);
766
767void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
768
769void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
770
771void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
772
773void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
774
buzbee15bf9802012-06-12 17:49:27 -0700775MIR* oatFindMoveResult(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir);
buzbee67bf8852011-08-17 17:51:35 -0700776/* Debug Utilities */
777void oatDumpCompilationUnit(CompilationUnit* cUnit);
778
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800779} // namespace art
780
buzbee67bf8852011-08-17 17:51:35 -0700781#endif // ART_SRC_COMPILER_COMPILER_IR_H_