blob: 54d41a5c5be4b0f00dbe737ef55364a3238b1b0b [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
Bill Buzbee46cd5b62009-06-05 15:36:06 -070067typedef struct ChainCellCounts {
68 union {
Ben Chengcec26f62010-01-15 15:29:33 -080069 u1 count[kChainingCellLast]; /* include one more space for the gap # */
Bill Buzbee46cd5b62009-06-05 15:36:06 -070070 u4 dummyForAlignment;
71 } u;
72} ChainCellCounts;
73
Ben Chengba4fc8b2009-06-01 13:00:29 -070074typedef struct LIR {
75 int offset;
76 struct LIR *next;
77 struct LIR *prev;
78 struct LIR *target;
79} LIR;
80
Ben Cheng4238ec22009-08-24 16:32:22 -070081enum ExtendedMIROpcode {
Dan Bornsteinccaab182010-12-03 15:32:40 -080082 kMirOpFirst = kNumPackedOpcodes,
Bill Buzbee1465db52009-09-23 17:17:35 -070083 kMirOpPhi = kMirOpFirst,
84 kMirOpNullNRangeUpCheck,
85 kMirOpNullNRangeDownCheck,
86 kMirOpLowerBound,
87 kMirOpPunt,
Ben Cheng7a2697d2010-06-07 13:44:23 -070088 kMirOpCheckInlinePrediction, // Gen checks for predicted inlining
Bill Buzbee1465db52009-09-23 17:17:35 -070089 kMirOpLast,
Ben Cheng4238ec22009-08-24 16:32:22 -070090};
91
92struct SSARepresentation;
93
94typedef enum {
95 kMIRIgnoreNullCheck = 0,
96 kMIRNullCheckOnly,
97 kMIRIgnoreRangeCheck,
98 kMIRRangeCheckOnly,
Ben Cheng7a2697d2010-06-07 13:44:23 -070099 kMIRInlined, // Invoke is inlined (ie dead)
100 kMIRInlinedPred, // Invoke is inlined via prediction
101 kMIRCallee, // Instruction is inlined from callee
Ben Cheng4238ec22009-08-24 16:32:22 -0700102} MIROptimizationFlagPositons;
103
104#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
105#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
106#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
107#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
Ben Cheng7a2697d2010-06-07 13:44:23 -0700108#define MIR_INLINED (1 << kMIRInlined)
109#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
110#define MIR_CALLEE (1 << kMIRCallee)
111
112typedef struct CallsiteInfo {
113 const ClassObject *clazz;
114 const Method *method;
115 LIR *misPredBranchOver;
116} CallsiteInfo;
Ben Cheng4238ec22009-08-24 16:32:22 -0700117
Ben Chengba4fc8b2009-06-01 13:00:29 -0700118typedef struct MIR {
119 DecodedInstruction dalvikInsn;
120 unsigned int width;
121 unsigned int offset;
122 struct MIR *prev;
123 struct MIR *next;
Ben Cheng4238ec22009-08-24 16:32:22 -0700124 struct SSARepresentation *ssaRep;
125 int OptimizationFlags;
Bill Buzbee1465db52009-09-23 17:17:35 -0700126 int seqNum;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700127 union {
128 // Used by the inlined insn from the callee to find the mother method
129 const Method *calleeMethod;
130 // Used by the inlined invoke to find the class and method pointers
131 CallsiteInfo *callsiteInfo;
132 } meta;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700133} MIR;
134
Ben Cheng4238ec22009-08-24 16:32:22 -0700135struct BasicBlockDataFlow;
136
Ben Cheng00603072010-10-28 11:13:58 -0700137/* For successorBlockList */
138typedef enum BlockListType {
139 kNotUsed = 0,
140 kCatch,
141 kPackedSwitch,
142 kSparseSwitch,
143} BlockListType;
144
Ben Chengba4fc8b2009-06-01 13:00:29 -0700145typedef struct BasicBlock {
146 int id;
Ben Cheng00603072010-10-28 11:13:58 -0700147 bool visited;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700148 unsigned int startOffset;
149 const Method *containingMethod; // For blocks from the callee
150 BBType blockType;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700151 bool needFallThroughBranch; // For blocks ended due to length limit
Ben Cheng7a2697d2010-06-07 13:44:23 -0700152 bool isFallThroughFromInvoke; // True means the block needs alignment
Ben Chengba4fc8b2009-06-01 13:00:29 -0700153 MIR *firstMIRInsn;
154 MIR *lastMIRInsn;
155 struct BasicBlock *fallThrough;
156 struct BasicBlock *taken;
Ben Cheng00603072010-10-28 11:13:58 -0700157 struct BasicBlock *iDom; // Immediate dominator
Ben Cheng4238ec22009-08-24 16:32:22 -0700158 struct BasicBlockDataFlow *dataFlowInfo;
Ben Cheng00603072010-10-28 11:13:58 -0700159 BitVector *predecessors;
160 BitVector *dominators;
161 BitVector *iDominated; // Set nodes being immediately dominated
162 BitVector *domFrontier; // Dominance frontier
163 struct { // For one-to-many successors like
164 BlockListType blockListType; // switch and exception handling
165 GrowableList blocks;
166 } successorBlockList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700167} BasicBlock;
168
Ben Cheng7a02cb12010-12-15 14:18:31 -0800169/*
170 * The "blocks" field in "successorBlockList" points to an array of
171 * elements with the type "SuccessorBlockInfo".
172 * For catch blocks, key is type index for the exception.
173 * For swtich blocks, key is the case value.
174 */
175typedef struct SuccessorBlockInfo {
176 BasicBlock *block;
177 int key;
178} SuccessorBlockInfo;
179
Ben Cheng4238ec22009-08-24 16:32:22 -0700180struct LoopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700181struct RegisterPool;
Ben Cheng4238ec22009-08-24 16:32:22 -0700182
buzbeebff121a2010-08-04 15:25:06 -0700183typedef enum AssemblerStatus {
184 kSuccess,
185 kRetryAll,
186 kRetryHalve
187} AssemblerStatus;
188
Ben Chengba4fc8b2009-06-01 13:00:29 -0700189typedef struct CompilationUnit {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700190 int numInsts;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700191 int numBlocks;
Ben Cheng00603072010-10-28 11:13:58 -0700192 GrowableList blockList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700193 const Method *method;
194 const JitTraceDescription *traceDesc;
195 LIR *firstLIRInsn;
196 LIR *lastLIRInsn;
197 LIR *wordList;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700198 LIR *chainCellOffsetLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700199 GrowableList pcReconstructionList;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700200 int headerSize; // bytes before the first code ptr
201 int dataOffset; // starting offset of literal pool
202 int totalSize; // header + code size
buzbeebff121a2010-08-04 15:25:06 -0700203 AssemblerStatus assemblerStatus; // Success or fix and retry
204 int assemblerRetries; // How many times tried to fix assembly
Ben Chengba4fc8b2009-06-01 13:00:29 -0700205 unsigned char *codeBuffer;
206 void *baseAddr;
207 bool printMe;
208 bool allSingleStep;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700209 bool hasLoop; // Contains a loop
210 bool hasInvoke; // Contains an invoke instruction
jeffhao9e45c0b2010-02-03 10:24:05 -0800211 bool heapMemOp; // Mark mem ops for self verification
Ben Cheng7a2697d2010-06-07 13:44:23 -0700212 bool wholeMethod;
buzbee2e152ba2010-12-15 16:32:35 -0800213 int profileCodeSize; // Size of the profile prefix in bytes
Ben Chengcec26f62010-01-15 15:29:33 -0800214 int numChainingCells[kChainingCellGap];
215 LIR *firstChainingLIR[kChainingCellGap];
216 LIR *chainingCellBottom;
Bill Buzbee1465db52009-09-23 17:17:35 -0700217 struct RegisterPool *regPool;
Ben Chenge9695e52009-06-16 16:11:47 -0700218 int optRound; // round number to tell an LIR's age
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800219 jmp_buf *bailPtr;
Bill Buzbee716f1202009-07-23 13:22:09 -0700220 JitInstructionSetType instructionSet;
Ben Cheng4238ec22009-08-24 16:32:22 -0700221 /* Number of total regs used in the whole cUnit after SSA transformation */
222 int numSSARegs;
223 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
224 GrowableList *ssaToDalvikMap;
225
226 /* The following are new data structures to support SSA representations */
227 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
228 int *dalvikToSSAMap; // length == method->registersSize
229 BitVector *isConstantV; // length == numSSAReg
230 int *constantValues; // length == numSSAReg
231
232 /* Data structure for loop analysis and optimizations */
233 struct LoopAnalysis *loopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700234
235 /* Map SSA names to location */
236 RegLocation *regLocation;
237 int sequenceNumber;
Ben Cheng6c10a972009-10-29 14:39:18 -0700238
239 /*
240 * Set to the Dalvik PC of the switch instruction if it has more than
241 * MAX_CHAINED_SWITCH_CASES cases.
242 */
243 const u2 *switchOverflowPad;
Ben Cheng00603072010-10-28 11:13:58 -0700244
245 /* New fields only for method-based compilation */
246 int numReachableBlocks;
247 int numDalvikRegisters; // method->registersSize + inlined
248 BasicBlock *entryBlock;
249 BasicBlock *exitBlock;
250 GrowableList dfsOrder;
251 GrowableList domPostOrderTraversal;
252 BitVector *tryBlockAddr;
253 BitVector **defBlockMatrix; // numDalvikRegister x numBlocks
254 BitVector *tempBlockV;
255 BitVector *tempDalvikRegisterV;
256 BitVector *tempSSARegisterV; // numSSARegs
257 bool printSSANames;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700258} CompilationUnit;
259
Ben Cheng11d8f142010-03-24 15:24:19 -0700260#if defined(WITH_SELF_VERIFICATION)
261#define HEAP_ACCESS_SHADOW(_state) cUnit->heapMemOp = _state
262#else
263#define HEAP_ACCESS_SHADOW(_state)
264#endif
265
Ben Cheng00603072010-10-28 11:13:58 -0700266BasicBlock *dvmCompilerNewBB(BBType blockType, int blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700267
268void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir);
269
Ben Cheng4238ec22009-08-24 16:32:22 -0700270void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir);
271
Ben Cheng7a2697d2010-06-07 13:44:23 -0700272void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR);
273
Ben Chengba4fc8b2009-06-01 13:00:29 -0700274void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir);
275
Ben Chenge9695e52009-06-16 16:11:47 -0700276void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR);
277
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700278void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR);
279
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800280void dvmCompilerAbort(CompilationUnit *cUnit);
281
Ben Chengba4fc8b2009-06-01 13:00:29 -0700282/* Debug Utilities */
283void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit);
284
285#endif /* _DALVIK_VM_COMPILER_IR */