blob: 89cd1414dceb807425fe1015fa81ccb8c65e3391 [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 Chengcfdeca32011-01-14 11:36:46 -0800102 kMIRInvokeMethodJIT, // Callee is JIT'ed as a whole method
Ben Cheng4238ec22009-08-24 16:32:22 -0700103} MIROptimizationFlagPositons;
104
105#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
106#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
107#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
108#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
Ben Cheng7a2697d2010-06-07 13:44:23 -0700109#define MIR_INLINED (1 << kMIRInlined)
110#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
111#define MIR_CALLEE (1 << kMIRCallee)
Ben Chengcfdeca32011-01-14 11:36:46 -0800112#define MIR_INVOKE_METHOD_JIT (1 << kMIRInvokeMethodJIT)
Ben Cheng7a2697d2010-06-07 13:44:23 -0700113
114typedef struct CallsiteInfo {
115 const ClassObject *clazz;
116 const Method *method;
117 LIR *misPredBranchOver;
118} CallsiteInfo;
Ben Cheng4238ec22009-08-24 16:32:22 -0700119
Ben Chengba4fc8b2009-06-01 13:00:29 -0700120typedef struct MIR {
121 DecodedInstruction dalvikInsn;
122 unsigned int width;
123 unsigned int offset;
124 struct MIR *prev;
125 struct MIR *next;
Ben Cheng4238ec22009-08-24 16:32:22 -0700126 struct SSARepresentation *ssaRep;
127 int OptimizationFlags;
Bill Buzbee1465db52009-09-23 17:17:35 -0700128 int seqNum;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700129 union {
130 // Used by the inlined insn from the callee to find the mother method
131 const Method *calleeMethod;
132 // Used by the inlined invoke to find the class and method pointers
133 CallsiteInfo *callsiteInfo;
134 } meta;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700135} MIR;
136
Ben Cheng4238ec22009-08-24 16:32:22 -0700137struct BasicBlockDataFlow;
138
Ben Cheng00603072010-10-28 11:13:58 -0700139/* For successorBlockList */
140typedef enum BlockListType {
141 kNotUsed = 0,
142 kCatch,
143 kPackedSwitch,
144 kSparseSwitch,
145} BlockListType;
146
Ben Chengba4fc8b2009-06-01 13:00:29 -0700147typedef struct BasicBlock {
148 int id;
Ben Cheng00603072010-10-28 11:13:58 -0700149 bool visited;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700150 unsigned int startOffset;
151 const Method *containingMethod; // For blocks from the callee
152 BBType blockType;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700153 bool needFallThroughBranch; // For blocks ended due to length limit
Ben Cheng7a2697d2010-06-07 13:44:23 -0700154 bool isFallThroughFromInvoke; // True means the block needs alignment
Ben Chengba4fc8b2009-06-01 13:00:29 -0700155 MIR *firstMIRInsn;
156 MIR *lastMIRInsn;
157 struct BasicBlock *fallThrough;
158 struct BasicBlock *taken;
Ben Cheng00603072010-10-28 11:13:58 -0700159 struct BasicBlock *iDom; // Immediate dominator
Ben Cheng4238ec22009-08-24 16:32:22 -0700160 struct BasicBlockDataFlow *dataFlowInfo;
Ben Cheng00603072010-10-28 11:13:58 -0700161 BitVector *predecessors;
162 BitVector *dominators;
163 BitVector *iDominated; // Set nodes being immediately dominated
164 BitVector *domFrontier; // Dominance frontier
165 struct { // For one-to-many successors like
166 BlockListType blockListType; // switch and exception handling
167 GrowableList blocks;
168 } successorBlockList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700169} BasicBlock;
170
Ben Cheng7a02cb12010-12-15 14:18:31 -0800171/*
172 * The "blocks" field in "successorBlockList" points to an array of
173 * elements with the type "SuccessorBlockInfo".
174 * For catch blocks, key is type index for the exception.
175 * For swtich blocks, key is the case value.
176 */
177typedef struct SuccessorBlockInfo {
178 BasicBlock *block;
179 int key;
180} SuccessorBlockInfo;
181
Ben Cheng4238ec22009-08-24 16:32:22 -0700182struct LoopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700183struct RegisterPool;
Ben Cheng4238ec22009-08-24 16:32:22 -0700184
buzbeebff121a2010-08-04 15:25:06 -0700185typedef enum AssemblerStatus {
186 kSuccess,
187 kRetryAll,
188 kRetryHalve
189} AssemblerStatus;
190
Ben Chengba4fc8b2009-06-01 13:00:29 -0700191typedef struct CompilationUnit {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700192 int numInsts;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700193 int numBlocks;
Ben Cheng00603072010-10-28 11:13:58 -0700194 GrowableList blockList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700195 const Method *method;
196 const JitTraceDescription *traceDesc;
197 LIR *firstLIRInsn;
198 LIR *lastLIRInsn;
199 LIR *wordList;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700200 LIR *chainCellOffsetLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700201 GrowableList pcReconstructionList;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700202 int headerSize; // bytes before the first code ptr
203 int dataOffset; // starting offset of literal pool
204 int totalSize; // header + code size
buzbeebff121a2010-08-04 15:25:06 -0700205 AssemblerStatus assemblerStatus; // Success or fix and retry
206 int assemblerRetries; // How many times tried to fix assembly
Ben Chengba4fc8b2009-06-01 13:00:29 -0700207 unsigned char *codeBuffer;
208 void *baseAddr;
209 bool printMe;
210 bool allSingleStep;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700211 bool hasLoop; // Contains a loop
212 bool hasInvoke; // Contains an invoke instruction
jeffhao9e45c0b2010-02-03 10:24:05 -0800213 bool heapMemOp; // Mark mem ops for self verification
Ben Chengd72564c2011-02-08 17:09:25 -0800214 bool usesLinkRegister; // For self-verification only
buzbee2e152ba2010-12-15 16:32:35 -0800215 int profileCodeSize; // Size of the profile prefix in bytes
Ben Chengcec26f62010-01-15 15:29:33 -0800216 int numChainingCells[kChainingCellGap];
217 LIR *firstChainingLIR[kChainingCellGap];
218 LIR *chainingCellBottom;
Bill Buzbee1465db52009-09-23 17:17:35 -0700219 struct RegisterPool *regPool;
Ben Chenge9695e52009-06-16 16:11:47 -0700220 int optRound; // round number to tell an LIR's age
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800221 jmp_buf *bailPtr;
Bill Buzbee716f1202009-07-23 13:22:09 -0700222 JitInstructionSetType instructionSet;
Ben Cheng4238ec22009-08-24 16:32:22 -0700223 /* Number of total regs used in the whole cUnit after SSA transformation */
224 int numSSARegs;
225 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
226 GrowableList *ssaToDalvikMap;
227
228 /* The following are new data structures to support SSA representations */
229 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
230 int *dalvikToSSAMap; // length == method->registersSize
231 BitVector *isConstantV; // length == numSSAReg
232 int *constantValues; // length == numSSAReg
233
234 /* Data structure for loop analysis and optimizations */
235 struct LoopAnalysis *loopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700236
237 /* Map SSA names to location */
238 RegLocation *regLocation;
239 int sequenceNumber;
Ben Cheng6c10a972009-10-29 14:39:18 -0700240
241 /*
242 * Set to the Dalvik PC of the switch instruction if it has more than
243 * MAX_CHAINED_SWITCH_CASES cases.
244 */
245 const u2 *switchOverflowPad;
Ben Cheng00603072010-10-28 11:13:58 -0700246
247 /* New fields only for method-based compilation */
Ben Chengcfdeca32011-01-14 11:36:46 -0800248 bool methodJitMode;
Ben Cheng00603072010-10-28 11:13:58 -0700249 int numReachableBlocks;
250 int numDalvikRegisters; // method->registersSize + inlined
251 BasicBlock *entryBlock;
252 BasicBlock *exitBlock;
Ben Chengcfdeca32011-01-14 11:36:46 -0800253 BasicBlock *curBlock;
Ben Cheng00603072010-10-28 11:13:58 -0700254 GrowableList dfsOrder;
255 GrowableList domPostOrderTraversal;
256 BitVector *tryBlockAddr;
257 BitVector **defBlockMatrix; // numDalvikRegister x numBlocks
258 BitVector *tempBlockV;
259 BitVector *tempDalvikRegisterV;
260 BitVector *tempSSARegisterV; // numSSARegs
261 bool printSSANames;
Ben Chengcfdeca32011-01-14 11:36:46 -0800262 void *blockLabelList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700263} CompilationUnit;
264
Ben Cheng11d8f142010-03-24 15:24:19 -0700265#if defined(WITH_SELF_VERIFICATION)
266#define HEAP_ACCESS_SHADOW(_state) cUnit->heapMemOp = _state
267#else
268#define HEAP_ACCESS_SHADOW(_state)
269#endif
270
Ben Cheng00603072010-10-28 11:13:58 -0700271BasicBlock *dvmCompilerNewBB(BBType blockType, int blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700272
273void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir);
274
Ben Cheng4238ec22009-08-24 16:32:22 -0700275void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir);
276
Ben Cheng7a2697d2010-06-07 13:44:23 -0700277void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR);
278
Ben Chengba4fc8b2009-06-01 13:00:29 -0700279void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir);
280
Ben Chenge9695e52009-06-16 16:11:47 -0700281void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR);
282
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700283void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR);
284
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800285void dvmCompilerAbort(CompilationUnit *cUnit);
286
Ben Chengba4fc8b2009-06-01 13:00:29 -0700287/* Debug Utilities */
288void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit);
289
290#endif /* _DALVIK_VM_COMPILER_IR */