blob: 7b9987b0b16d3930138cfe7741c5e1cc54219653 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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 _DALVIK_VM_COMPILER_IR
18#define _DALVIK_VM_COMPILER_IR
19
Ben Cheng4238ec22009-08-24 16:32:22 -070020#include "codegen/Optimizer.h"
21
Bill Buzbee1465db52009-09-23 17:17:35 -070022typedef enum RegisterClass {
23 kCoreReg,
24 kFPReg,
25 kAnyReg,
26} RegisterClass;
27
28typedef enum RegLocationType {
29 kLocDalvikFrame = 0,
30 kLocPhysReg,
31 kLocRetval, // Return region in interpState
32 kLocSpill,
33} RegLocationType;
34
35typedef struct RegLocation {
36 RegLocationType location:2;
37 unsigned wide:1;
38 unsigned fp:1; // Hint for float/double
39 u1 lowReg:6; // First physical register
40 u1 highReg:6; // 2nd physical register (if wide)
41 s2 sRegLow; // SSA name for low Dalvik word
42} RegLocation;
43
44#define INVALID_SREG (-1)
45#define INVALID_REG (-1)
46
Ben Chengba4fc8b2009-06-01 13:00:29 -070047typedef enum BBType {
48 /* For coding convenience reasons chaining cell types should appear first */
Bill Buzbee1465db52009-09-23 17:17:35 -070049 kChainingCellNormal = 0,
50 kChainingCellHot,
51 kChainingCellInvokeSingleton,
52 kChainingCellInvokePredicted,
53 kChainingCellBackwardBranch,
Ben Chengcec26f62010-01-15 15:29:33 -080054 kChainingCellGap,
55 /* Don't insert new fields between Gap and Last */
56 kChainingCellLast = kChainingCellGap + 1,
Ben Cheng7a2697d2010-06-07 13:44:23 -070057 kMethodEntryBlock,
58 kTraceEntryBlock,
Bill Buzbee1465db52009-09-23 17:17:35 -070059 kDalvikByteCode,
Ben Cheng7a2697d2010-06-07 13:44:23 -070060 kTraceExitBlock,
61 kMethodExitBlock,
Bill Buzbee1465db52009-09-23 17:17:35 -070062 kPCReconstruction,
63 kExceptionHandling,
Ben Cheng00603072010-10-28 11:13:58 -070064 kCatchEntry,
Ben Chengba4fc8b2009-06-01 13:00:29 -070065} BBType;
66
Ben Cheng46cd4fb2011-03-16 17:19:06 -070067typedef enum JitMode {
68 kJitTrace = 0, // Acyclic - all instructions come from the trace descriptor
69 kJitLoop, // Cycle - trace descriptor is used as a hint
70 kJitMethod, // Whole method
71} JitMode;
72
Bill Buzbee46cd5b62009-06-05 15:36:06 -070073typedef struct ChainCellCounts {
74 union {
Ben Chengcec26f62010-01-15 15:29:33 -080075 u1 count[kChainingCellLast]; /* include one more space for the gap # */
Bill Buzbee46cd5b62009-06-05 15:36:06 -070076 u4 dummyForAlignment;
77 } u;
78} ChainCellCounts;
79
Ben Chengba4fc8b2009-06-01 13:00:29 -070080typedef struct LIR {
81 int offset;
82 struct LIR *next;
83 struct LIR *prev;
84 struct LIR *target;
85} LIR;
86
Ben Cheng4238ec22009-08-24 16:32:22 -070087enum ExtendedMIROpcode {
Dan Bornsteinccaab182010-12-03 15:32:40 -080088 kMirOpFirst = kNumPackedOpcodes,
Bill Buzbee1465db52009-09-23 17:17:35 -070089 kMirOpPhi = kMirOpFirst,
90 kMirOpNullNRangeUpCheck,
91 kMirOpNullNRangeDownCheck,
92 kMirOpLowerBound,
93 kMirOpPunt,
Ben Cheng7a2697d2010-06-07 13:44:23 -070094 kMirOpCheckInlinePrediction, // Gen checks for predicted inlining
Bill Buzbee1465db52009-09-23 17:17:35 -070095 kMirOpLast,
Ben Cheng4238ec22009-08-24 16:32:22 -070096};
97
98struct SSARepresentation;
99
100typedef enum {
101 kMIRIgnoreNullCheck = 0,
102 kMIRNullCheckOnly,
103 kMIRIgnoreRangeCheck,
104 kMIRRangeCheckOnly,
Ben Cheng7a2697d2010-06-07 13:44:23 -0700105 kMIRInlined, // Invoke is inlined (ie dead)
106 kMIRInlinedPred, // Invoke is inlined via prediction
107 kMIRCallee, // Instruction is inlined from callee
Ben Chengcfdeca32011-01-14 11:36:46 -0800108 kMIRInvokeMethodJIT, // Callee is JIT'ed as a whole method
Ben Cheng4238ec22009-08-24 16:32:22 -0700109} MIROptimizationFlagPositons;
110
111#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
112#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
113#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
114#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
Ben Cheng7a2697d2010-06-07 13:44:23 -0700115#define MIR_INLINED (1 << kMIRInlined)
116#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
117#define MIR_CALLEE (1 << kMIRCallee)
Ben Chengcfdeca32011-01-14 11:36:46 -0800118#define MIR_INVOKE_METHOD_JIT (1 << kMIRInvokeMethodJIT)
Ben Cheng7a2697d2010-06-07 13:44:23 -0700119
120typedef struct CallsiteInfo {
Ben Cheng385828e2011-03-04 16:48:33 -0800121 const char *classDescriptor;
122 Object *classLoader;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700123 const Method *method;
124 LIR *misPredBranchOver;
125} CallsiteInfo;
Ben Cheng4238ec22009-08-24 16:32:22 -0700126
Ben Chengba4fc8b2009-06-01 13:00:29 -0700127typedef struct MIR {
128 DecodedInstruction dalvikInsn;
129 unsigned int width;
130 unsigned int offset;
131 struct MIR *prev;
132 struct MIR *next;
Ben Cheng4238ec22009-08-24 16:32:22 -0700133 struct SSARepresentation *ssaRep;
134 int OptimizationFlags;
Bill Buzbee1465db52009-09-23 17:17:35 -0700135 int seqNum;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700136 union {
137 // Used by the inlined insn from the callee to find the mother method
138 const Method *calleeMethod;
139 // Used by the inlined invoke to find the class and method pointers
140 CallsiteInfo *callsiteInfo;
141 } meta;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700142} MIR;
143
Ben Cheng4238ec22009-08-24 16:32:22 -0700144struct BasicBlockDataFlow;
145
Ben Cheng00603072010-10-28 11:13:58 -0700146/* For successorBlockList */
147typedef enum BlockListType {
148 kNotUsed = 0,
149 kCatch,
150 kPackedSwitch,
151 kSparseSwitch,
152} BlockListType;
153
Ben Chengba4fc8b2009-06-01 13:00:29 -0700154typedef struct BasicBlock {
155 int id;
Ben Cheng00603072010-10-28 11:13:58 -0700156 bool visited;
Ben Cheng46cd4fb2011-03-16 17:19:06 -0700157 bool hidden;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700158 unsigned int startOffset;
159 const Method *containingMethod; // For blocks from the callee
160 BBType blockType;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700161 bool needFallThroughBranch; // For blocks ended due to length limit
Ben Cheng7a2697d2010-06-07 13:44:23 -0700162 bool isFallThroughFromInvoke; // True means the block needs alignment
Ben Chengba4fc8b2009-06-01 13:00:29 -0700163 MIR *firstMIRInsn;
164 MIR *lastMIRInsn;
165 struct BasicBlock *fallThrough;
166 struct BasicBlock *taken;
Ben Cheng00603072010-10-28 11:13:58 -0700167 struct BasicBlock *iDom; // Immediate dominator
Ben Cheng4238ec22009-08-24 16:32:22 -0700168 struct BasicBlockDataFlow *dataFlowInfo;
Ben Cheng00603072010-10-28 11:13:58 -0700169 BitVector *predecessors;
170 BitVector *dominators;
171 BitVector *iDominated; // Set nodes being immediately dominated
172 BitVector *domFrontier; // Dominance frontier
173 struct { // For one-to-many successors like
174 BlockListType blockListType; // switch and exception handling
175 GrowableList blocks;
176 } successorBlockList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700177} BasicBlock;
178
Ben Cheng7a02cb12010-12-15 14:18:31 -0800179/*
180 * The "blocks" field in "successorBlockList" points to an array of
181 * elements with the type "SuccessorBlockInfo".
182 * For catch blocks, key is type index for the exception.
183 * For swtich blocks, key is the case value.
184 */
185typedef struct SuccessorBlockInfo {
186 BasicBlock *block;
187 int key;
188} SuccessorBlockInfo;
189
Ben Cheng4238ec22009-08-24 16:32:22 -0700190struct LoopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700191struct RegisterPool;
Ben Cheng4238ec22009-08-24 16:32:22 -0700192
buzbeebff121a2010-08-04 15:25:06 -0700193typedef enum AssemblerStatus {
194 kSuccess,
195 kRetryAll,
196 kRetryHalve
197} AssemblerStatus;
198
Ben Chengba4fc8b2009-06-01 13:00:29 -0700199typedef struct CompilationUnit {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700200 int numInsts;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700201 int numBlocks;
Ben Cheng00603072010-10-28 11:13:58 -0700202 GrowableList blockList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700203 const Method *method;
204 const JitTraceDescription *traceDesc;
205 LIR *firstLIRInsn;
206 LIR *lastLIRInsn;
Ben Cheng385828e2011-03-04 16:48:33 -0800207 LIR *literalList; // Constants
208 LIR *classPointerList; // Relocatable
209 int numClassPointers;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700210 LIR *chainCellOffsetLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700211 GrowableList pcReconstructionList;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700212 int headerSize; // bytes before the first code ptr
213 int dataOffset; // starting offset of literal pool
214 int totalSize; // header + code size
buzbeebff121a2010-08-04 15:25:06 -0700215 AssemblerStatus assemblerStatus; // Success or fix and retry
216 int assemblerRetries; // How many times tried to fix assembly
Ben Chengba4fc8b2009-06-01 13:00:29 -0700217 unsigned char *codeBuffer;
218 void *baseAddr;
219 bool printMe;
220 bool allSingleStep;
Ben Cheng385828e2011-03-04 16:48:33 -0800221 bool hasClassLiterals; // Contains class ptrs used as literals
Ben Cheng7a2697d2010-06-07 13:44:23 -0700222 bool hasLoop; // Contains a loop
223 bool hasInvoke; // Contains an invoke instruction
jeffhao9e45c0b2010-02-03 10:24:05 -0800224 bool heapMemOp; // Mark mem ops for self verification
Ben Chengd72564c2011-02-08 17:09:25 -0800225 bool usesLinkRegister; // For self-verification only
buzbee2e152ba2010-12-15 16:32:35 -0800226 int profileCodeSize; // Size of the profile prefix in bytes
Ben Chengcec26f62010-01-15 15:29:33 -0800227 int numChainingCells[kChainingCellGap];
228 LIR *firstChainingLIR[kChainingCellGap];
229 LIR *chainingCellBottom;
Bill Buzbee1465db52009-09-23 17:17:35 -0700230 struct RegisterPool *regPool;
Ben Chenge9695e52009-06-16 16:11:47 -0700231 int optRound; // round number to tell an LIR's age
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800232 jmp_buf *bailPtr;
Bill Buzbee716f1202009-07-23 13:22:09 -0700233 JitInstructionSetType instructionSet;
Ben Cheng4238ec22009-08-24 16:32:22 -0700234 /* 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
242 BitVector *isConstantV; // length == numSSAReg
243 int *constantValues; // length == numSSAReg
244
245 /* Data structure for loop analysis and optimizations */
246 struct LoopAnalysis *loopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700247
248 /* Map SSA names to location */
249 RegLocation *regLocation;
250 int sequenceNumber;
Ben Cheng6c10a972009-10-29 14:39:18 -0700251
252 /*
253 * Set to the Dalvik PC of the switch instruction if it has more than
254 * MAX_CHAINED_SWITCH_CASES cases.
255 */
256 const u2 *switchOverflowPad;
Ben Cheng00603072010-10-28 11:13:58 -0700257
258 /* New fields only for method-based compilation */
Ben Cheng46cd4fb2011-03-16 17:19:06 -0700259 JitMode jitMode;
Ben Cheng00603072010-10-28 11:13:58 -0700260 int numReachableBlocks;
261 int numDalvikRegisters; // method->registersSize + inlined
262 BasicBlock *entryBlock;
263 BasicBlock *exitBlock;
Ben Chengcfdeca32011-01-14 11:36:46 -0800264 BasicBlock *curBlock;
Ben Cheng7ab74e12011-02-03 14:02:06 -0800265 BasicBlock *nextCodegenBlock; // for extended trace codegen
Ben Cheng00603072010-10-28 11:13:58 -0700266 GrowableList dfsOrder;
267 GrowableList domPostOrderTraversal;
268 BitVector *tryBlockAddr;
269 BitVector **defBlockMatrix; // numDalvikRegister x numBlocks
270 BitVector *tempBlockV;
271 BitVector *tempDalvikRegisterV;
272 BitVector *tempSSARegisterV; // numSSARegs
273 bool printSSANames;
Ben Chengcfdeca32011-01-14 11:36:46 -0800274 void *blockLabelList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700275} CompilationUnit;
276
Ben Cheng11d8f142010-03-24 15:24:19 -0700277#if defined(WITH_SELF_VERIFICATION)
278#define HEAP_ACCESS_SHADOW(_state) cUnit->heapMemOp = _state
279#else
280#define HEAP_ACCESS_SHADOW(_state)
281#endif
282
Ben Cheng00603072010-10-28 11:13:58 -0700283BasicBlock *dvmCompilerNewBB(BBType blockType, int blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700284
285void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir);
286
Ben Cheng4238ec22009-08-24 16:32:22 -0700287void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir);
288
Ben Cheng7a2697d2010-06-07 13:44:23 -0700289void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR);
290
Ben Chengba4fc8b2009-06-01 13:00:29 -0700291void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir);
292
Ben Chenge9695e52009-06-16 16:11:47 -0700293void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR);
294
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700295void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR);
296
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800297void dvmCompilerAbort(CompilationUnit *cUnit);
298
Ben Chengba4fc8b2009-06-01 13:00:29 -0700299/* Debug Utilities */
300void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit);
301
302#endif /* _DALVIK_VM_COMPILER_IR */