blob: 21aadec3abea688fc8720663178d83af49a5c8bb [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 {
Bill Buzbee1465db52009-09-23 17:17:35 -070081 kMirOpFirst = 256,
82 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
Ben Chengba4fc8b2009-06-01 13:00:29 -0700155typedef struct CompilationUnit {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700156 int numInsts;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700157 int numBlocks;
158 BasicBlock **blockList;
159 const Method *method;
160 const JitTraceDescription *traceDesc;
161 LIR *firstLIRInsn;
162 LIR *lastLIRInsn;
163 LIR *wordList;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700164 LIR *chainCellOffsetLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700165 GrowableList pcReconstructionList;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700166 int headerSize; // bytes before the first code ptr
167 int dataOffset; // starting offset of literal pool
168 int totalSize; // header + code size
Ben Chengba4fc8b2009-06-01 13:00:29 -0700169 unsigned char *codeBuffer;
170 void *baseAddr;
171 bool printMe;
172 bool allSingleStep;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700173 bool halveInstCount;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700174 bool executionCount; // Add code to count trace executions
Ben Cheng7a2697d2010-06-07 13:44:23 -0700175 bool hasLoop; // Contains a loop
176 bool hasInvoke; // Contains an invoke instruction
jeffhao9e45c0b2010-02-03 10:24:05 -0800177 bool heapMemOp; // Mark mem ops for self verification
Ben Cheng7a2697d2010-06-07 13:44:23 -0700178 bool wholeMethod;
Ben Chengcec26f62010-01-15 15:29:33 -0800179 int numChainingCells[kChainingCellGap];
180 LIR *firstChainingLIR[kChainingCellGap];
181 LIR *chainingCellBottom;
Bill Buzbee1465db52009-09-23 17:17:35 -0700182 struct RegisterPool *regPool;
Ben Chenge9695e52009-06-16 16:11:47 -0700183 int optRound; // round number to tell an LIR's age
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800184 jmp_buf *bailPtr;
Bill Buzbee716f1202009-07-23 13:22:09 -0700185 JitInstructionSetType instructionSet;
Ben Cheng4238ec22009-08-24 16:32:22 -0700186 /* Number of total regs used in the whole cUnit after SSA transformation */
187 int numSSARegs;
188 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
189 GrowableList *ssaToDalvikMap;
190
191 /* The following are new data structures to support SSA representations */
192 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
193 int *dalvikToSSAMap; // length == method->registersSize
194 BitVector *isConstantV; // length == numSSAReg
195 int *constantValues; // length == numSSAReg
196
197 /* Data structure for loop analysis and optimizations */
198 struct LoopAnalysis *loopAnalysis;
Bill Buzbee1465db52009-09-23 17:17:35 -0700199
200 /* Map SSA names to location */
201 RegLocation *regLocation;
202 int sequenceNumber;
Ben Cheng6c10a972009-10-29 14:39:18 -0700203
204 /*
205 * Set to the Dalvik PC of the switch instruction if it has more than
206 * MAX_CHAINED_SWITCH_CASES cases.
207 */
208 const u2 *switchOverflowPad;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700209} CompilationUnit;
210
Ben Cheng11d8f142010-03-24 15:24:19 -0700211#if defined(WITH_SELF_VERIFICATION)
212#define HEAP_ACCESS_SHADOW(_state) cUnit->heapMemOp = _state
213#else
214#define HEAP_ACCESS_SHADOW(_state)
215#endif
216
Ben Chengba4fc8b2009-06-01 13:00:29 -0700217BasicBlock *dvmCompilerNewBB(BBType blockType);
218
219void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir);
220
Ben Cheng4238ec22009-08-24 16:32:22 -0700221void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir);
222
Ben Cheng7a2697d2010-06-07 13:44:23 -0700223void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR);
224
Ben Chengba4fc8b2009-06-01 13:00:29 -0700225void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir);
226
Ben Chenge9695e52009-06-16 16:11:47 -0700227void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR);
228
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700229void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR);
230
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800231void dvmCompilerAbort(CompilationUnit *cUnit);
232
Ben Chengba4fc8b2009-06-01 13:00:29 -0700233/* Debug Utilities */
234void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit);
235
236#endif /* _DALVIK_VM_COMPILER_IR */