blob: 712ca4ce89ccc8352b545f60763160ec598d1fc1 [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 Chengba4fc8b2009-06-01 13:00:29 -070064} BBType;
65
Bill Buzbee46cd5b62009-06-05 15:36:06 -070066typedef struct ChainCellCounts {
67 union {
Ben Chengcec26f62010-01-15 15:29:33 -080068 u1 count[kChainingCellLast]; /* include one more space for the gap # */
Bill Buzbee46cd5b62009-06-05 15:36:06 -070069 u4 dummyForAlignment;
70 } u;
71} ChainCellCounts;
72
Ben Chengba4fc8b2009-06-01 13:00:29 -070073typedef struct LIR {
74 int offset;
75 struct LIR *next;
76 struct LIR *prev;
77 struct LIR *target;
78} LIR;
79
Ben Cheng4238ec22009-08-24 16:32:22 -070080enum ExtendedMIROpcode {
Dan Bornsteinccaab182010-12-03 15:32:40 -080081 kMirOpFirst = kNumPackedOpcodes,
Bill Buzbee1465db52009-09-23 17:17:35 -070082 kMirOpPhi = kMirOpFirst,
83 kMirOpNullNRangeUpCheck,
84 kMirOpNullNRangeDownCheck,
85 kMirOpLowerBound,
86 kMirOpPunt,
Ben Cheng7a2697d2010-06-07 13:44:23 -070087 kMirOpCheckInlinePrediction, // Gen checks for predicted inlining
Bill Buzbee1465db52009-09-23 17:17:35 -070088 kMirOpLast,
Ben Cheng4238ec22009-08-24 16:32:22 -070089};
90
91struct SSARepresentation;
92
93typedef enum {
94 kMIRIgnoreNullCheck = 0,
95 kMIRNullCheckOnly,
96 kMIRIgnoreRangeCheck,
97 kMIRRangeCheckOnly,
Ben Cheng7a2697d2010-06-07 13:44:23 -070098 kMIRInlined, // Invoke is inlined (ie dead)
99 kMIRInlinedPred, // Invoke is inlined via prediction
100 kMIRCallee, // Instruction is inlined from callee
Ben Cheng4238ec22009-08-24 16:32:22 -0700101} MIROptimizationFlagPositons;
102
103#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
104#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
105#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
106#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
Ben Cheng7a2697d2010-06-07 13:44:23 -0700107#define MIR_INLINED (1 << kMIRInlined)
108#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
109#define MIR_CALLEE (1 << kMIRCallee)
110
111typedef struct CallsiteInfo {
112 const ClassObject *clazz;
113 const Method *method;
114 LIR *misPredBranchOver;
115} CallsiteInfo;
Ben Cheng4238ec22009-08-24 16:32:22 -0700116
Ben Chengba4fc8b2009-06-01 13:00:29 -0700117typedef struct MIR {
118 DecodedInstruction dalvikInsn;
119 unsigned int width;
120 unsigned int offset;
121 struct MIR *prev;
122 struct MIR *next;
Ben Cheng4238ec22009-08-24 16:32:22 -0700123 struct SSARepresentation *ssaRep;
124 int OptimizationFlags;
Bill Buzbee1465db52009-09-23 17:17:35 -0700125 int seqNum;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700126 union {
127 // Used by the inlined insn from the callee to find the mother method
128 const Method *calleeMethod;
129 // Used by the inlined invoke to find the class and method pointers
130 CallsiteInfo *callsiteInfo;
131 } meta;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700132} MIR;
133
Ben Cheng4238ec22009-08-24 16:32:22 -0700134struct BasicBlockDataFlow;
135
Ben Chengba4fc8b2009-06-01 13:00:29 -0700136typedef struct BasicBlock {
137 int id;
138 int visited;
139 unsigned int startOffset;
140 const Method *containingMethod; // For blocks from the callee
141 BBType blockType;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700142 bool needFallThroughBranch; // For blocks ended due to length limit
Ben Cheng7a2697d2010-06-07 13:44:23 -0700143 bool isFallThroughFromInvoke; // True means the block needs alignment
Ben Chengba4fc8b2009-06-01 13:00:29 -0700144 MIR *firstMIRInsn;
145 MIR *lastMIRInsn;
146 struct BasicBlock *fallThrough;
147 struct BasicBlock *taken;
148 struct BasicBlock *next; // Serial link for book keeping purposes
Ben Cheng4238ec22009-08-24 16:32:22 -0700149 struct BasicBlockDataFlow *dataFlowInfo;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700150} BasicBlock;
151
Ben Cheng4238ec22009-08-24 16:32:22 -0700152struct LoopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700153struct RegisterPool;
Ben Cheng4238ec22009-08-24 16:32:22 -0700154
buzbeebff121a2010-08-04 15:25:06 -0700155typedef enum AssemblerStatus {
156 kSuccess,
157 kRetryAll,
158 kRetryHalve
159} AssemblerStatus;
160
Ben Chengba4fc8b2009-06-01 13:00:29 -0700161typedef struct CompilationUnit {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700162 int numInsts;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700163 int numBlocks;
164 BasicBlock **blockList;
165 const Method *method;
166 const JitTraceDescription *traceDesc;
167 LIR *firstLIRInsn;
168 LIR *lastLIRInsn;
169 LIR *wordList;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700170 LIR *chainCellOffsetLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700171 GrowableList pcReconstructionList;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700172 int headerSize; // bytes before the first code ptr
173 int dataOffset; // starting offset of literal pool
174 int totalSize; // header + code size
buzbeebff121a2010-08-04 15:25:06 -0700175 AssemblerStatus assemblerStatus; // Success or fix and retry
176 int assemblerRetries; // How many times tried to fix assembly
Ben Chengba4fc8b2009-06-01 13:00:29 -0700177 unsigned char *codeBuffer;
178 void *baseAddr;
179 bool printMe;
180 bool allSingleStep;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700181 bool executionCount; // Add code to count trace executions
Ben Cheng7a2697d2010-06-07 13:44:23 -0700182 bool hasLoop; // Contains a loop
183 bool hasInvoke; // Contains an invoke instruction
jeffhao9e45c0b2010-02-03 10:24:05 -0800184 bool heapMemOp; // Mark mem ops for self verification
Ben Cheng7a2697d2010-06-07 13:44:23 -0700185 bool wholeMethod;
Ben Chengcec26f62010-01-15 15:29:33 -0800186 int numChainingCells[kChainingCellGap];
187 LIR *firstChainingLIR[kChainingCellGap];
188 LIR *chainingCellBottom;
Bill Buzbee1465db52009-09-23 17:17:35 -0700189 struct RegisterPool *regPool;
Ben Chenge9695e52009-06-16 16:11:47 -0700190 int optRound; // round number to tell an LIR's age
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800191 jmp_buf *bailPtr;
Bill Buzbee716f1202009-07-23 13:22:09 -0700192 JitInstructionSetType instructionSet;
Ben Cheng4238ec22009-08-24 16:32:22 -0700193 /* Number of total regs used in the whole cUnit after SSA transformation */
194 int numSSARegs;
195 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
196 GrowableList *ssaToDalvikMap;
197
198 /* The following are new data structures to support SSA representations */
199 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
200 int *dalvikToSSAMap; // length == method->registersSize
201 BitVector *isConstantV; // length == numSSAReg
202 int *constantValues; // length == numSSAReg
203
204 /* Data structure for loop analysis and optimizations */
205 struct LoopAnalysis *loopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700206
207 /* Map SSA names to location */
208 RegLocation *regLocation;
209 int sequenceNumber;
Ben Cheng6c10a972009-10-29 14:39:18 -0700210
211 /*
212 * Set to the Dalvik PC of the switch instruction if it has more than
213 * MAX_CHAINED_SWITCH_CASES cases.
214 */
215 const u2 *switchOverflowPad;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700216} CompilationUnit;
217
Ben Cheng11d8f142010-03-24 15:24:19 -0700218#if defined(WITH_SELF_VERIFICATION)
219#define HEAP_ACCESS_SHADOW(_state) cUnit->heapMemOp = _state
220#else
221#define HEAP_ACCESS_SHADOW(_state)
222#endif
223
Ben Chengba4fc8b2009-06-01 13:00:29 -0700224BasicBlock *dvmCompilerNewBB(BBType blockType);
225
226void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir);
227
Ben Cheng4238ec22009-08-24 16:32:22 -0700228void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir);
229
Ben Cheng7a2697d2010-06-07 13:44:23 -0700230void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR);
231
Ben Chengba4fc8b2009-06-01 13:00:29 -0700232void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir);
233
Ben Chenge9695e52009-06-16 16:11:47 -0700234void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR);
235
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700236void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR);
237
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800238void dvmCompilerAbort(CompilationUnit *cUnit);
239
Ben Chengba4fc8b2009-06-01 13:00:29 -0700240/* Debug Utilities */
241void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit);
242
243#endif /* _DALVIK_VM_COMPILER_IR */