blob: 643e7953c8cadcfb2f3bd5421b4e822a38ba2056 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 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 ART_SRC_COMPILER_COMPILER_IR_H_
18#define ART_SRC_COMPILER_COMPILER_IR_H_
19
20#include "codegen/Optimizer.h"
buzbeec143c552011-08-20 17:38:58 -070021#include <vector>
buzbee67bf8852011-08-17 17:51:35 -070022
23typedef enum RegisterClass {
24 kCoreReg,
25 kFPReg,
26 kAnyReg,
27} RegisterClass;
28
29typedef enum RegLocationType {
30 kLocDalvikFrame = 0, // Normal Dalvik register
31 kLocPhysReg,
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 unsigned home:1; // Does this represent the home location?
43 RegLocationType fpLocation:2; // Used only for non-SSA loc records
44 u1 fpLowReg:6; // Used only for non-SSA loc records
45 u1 fpHighReg:6; // Used only for non-SSA loc records
46 int spOffset:17;
47} RegLocation;
48
49#define INVALID_SREG (-1)
50#define INVALID_REG (0x3F)
51#define INVALID_OFFSET (-1)
52
53typedef enum BBType {
54 kEntryBlock,
55 kDalvikByteCode,
56 kExitBlock,
57 kExceptionHandling,
58 kCatchEntry,
59} BBType;
60
61typedef struct LIR {
62 int offset; // Offset of this instruction
63 int dalvikOffset; // Offset of Dalvik opcode
64 struct LIR* next;
65 struct LIR* prev;
66 struct LIR* target;
67} LIR;
68
69enum ExtendedMIROpcode {
70 kMirOpFirst = kNumPackedOpcodes,
71 kMirOpPhi = kMirOpFirst,
72 kMirOpNullNRangeUpCheck,
73 kMirOpNullNRangeDownCheck,
74 kMirOpLowerBound,
75 kMirOpPunt,
76 kMirOpCheckInlinePrediction, // Gen checks for predicted inlining
77 kMirOpLast,
78};
79
80struct SSARepresentation;
81
82typedef enum {
83 kMIRIgnoreNullCheck = 0,
84 kMIRNullCheckOnly,
85 kMIRIgnoreRangeCheck,
86 kMIRRangeCheckOnly,
87 kMIRInlined, // Invoke is inlined (ie dead)
88 kMIRInlinedPred, // Invoke is inlined via prediction
89 kMIRCallee, // Instruction is inlined from callee
90} MIROptimizationFlagPositons;
91
92#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
93#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
94#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
95#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
96#define MIR_INLINED (1 << kMIRInlined)
97#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
98#define MIR_CALLEE (1 << kMIRCallee)
99
100typedef struct CallsiteInfo {
101 const char* classDescriptor;
102 Object* classLoader;
103 const Method* method;
104 LIR* misPredBranchOver;
105} CallsiteInfo;
106
107typedef struct MIR {
108 DecodedInstruction dalvikInsn;
109 unsigned int width;
110 unsigned int offset;
111 struct MIR* prev;
112 struct MIR* next;
113 struct SSARepresentation* ssaRep;
114 int OptimizationFlags;
115 int seqNum;
116 union {
117 // Used by the inlined insn from the callee to find the mother method
118 const Method* calleeMethod;
119 // Used by the inlined invoke to find the class and method pointers
120 CallsiteInfo* callsiteInfo;
121 } meta;
122} MIR;
123
124struct BasicBlockDataFlow;
125
126/* For successorBlockList */
127typedef enum BlockListType {
128 kNotUsed = 0,
129 kCatch,
130 kPackedSwitch,
131 kSparseSwitch,
132} BlockListType;
133
134typedef struct BasicBlock {
135 int id;
136 bool visited;
137 bool hidden;
138 unsigned int startOffset;
139 const Method* containingMethod; // For blocks from the callee
140 BBType blockType;
141 bool needFallThroughBranch; // For blocks ended due to length limit
142 bool isFallThroughFromInvoke; // True means the block needs alignment
143 MIR* firstMIRInsn;
144 MIR* lastMIRInsn;
145 struct BasicBlock* fallThrough;
146 struct BasicBlock* taken;
147 struct BasicBlock* iDom; // Immediate dominator
148 struct BasicBlockDataFlow* dataFlowInfo;
149 ArenaBitVector* predecessors;
150 ArenaBitVector* dominators;
151 ArenaBitVector* iDominated; // Set nodes being immediately dominated
152 ArenaBitVector* domFrontier; // Dominance frontier
153 struct { // For one-to-many successors like
154 BlockListType blockListType; // switch and exception handling
155 GrowableList blocks;
156 } successorBlockList;
157} BasicBlock;
158
159/*
160 * The "blocks" field in "successorBlockList" points to an array of
161 * elements with the type "SuccessorBlockInfo".
162 * For catch blocks, key is type index for the exception.
163 * For swtich blocks, key is the case value.
164 */
165typedef struct SuccessorBlockInfo {
166 BasicBlock* block;
167 int key;
168} SuccessorBlockInfo;
169
170struct LoopAnalysis;
171struct RegisterPool;
172
173typedef enum AssemblerStatus {
174 kSuccess,
175 kRetryAll,
176 kRetryHalve
177} AssemblerStatus;
178
179typedef struct MappingTable {
180 int targetOffset;
181 int dalvikOffset;
182} MappingTable;
183
184typedef struct CompilationUnit {
185 int numInsts;
186 int numBlocks;
187 GrowableList blockList;
188 const Method *method;
189 LIR* firstLIRInsn;
190 LIR* lastLIRInsn;
191 LIR* literalList; // Constants
192 LIR* classPointerList; // Relocatable
193 int numClassPointers;
194 LIR* chainCellOffsetLIR;
195 int disableOpt;
196 int headerSize; // bytes before the first code ptr
197 int dataOffset; // starting offset of literal pool
198 int totalSize; // header + code size
199 AssemblerStatus assemblerStatus; // Success or fix and retry
200 int assemblerRetries;
buzbeec143c552011-08-20 17:38:58 -0700201 std::vector<short>codeBuffer;
buzbee67bf8852011-08-17 17:51:35 -0700202 bool printMe;
203 bool printMeVerbose;
204 bool hasClassLiterals; // Contains class ptrs used as literals
205 bool hasLoop; // Contains a loop
206 bool hasInvoke; // Contains an invoke instruction
207 bool heapMemOp; // Mark mem ops for self verification
208 bool usesLinkRegister; // For self-verification only
209 bool methodTraceSupport; // For TraceView profiling
210 struct RegisterPool* regPool;
211 int optRound; // round number to tell an LIR's age
212 OatInstructionSetType instructionSet;
213 /* Number of total regs used in the whole cUnit after SSA transformation */
214 int numSSARegs;
215 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
216 GrowableList* ssaToDalvikMap;
217
218 /* The following are new data structures to support SSA representations */
219 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
220 int* dalvikToSSAMap; // length == method->registersSize
221 ArenaBitVector* isConstantV; // length == numSSAReg
222 int* constantValues; // length == numSSAReg
223
224 /* Map SSA names to location */
225 RegLocation* regLocation;
226 int sequenceNumber;
227
228 /*
229 * Set to the Dalvik PC of the switch instruction if it has more than
230 * MAX_CHAINED_SWITCH_CASES cases.
231 */
232 const u2* switchOverflowPad;
233
234 int numReachableBlocks;
235 int numDalvikRegisters; // method->registersSize + inlined
236 BasicBlock* entryBlock;
237 BasicBlock* exitBlock;
238 BasicBlock* curBlock;
239 BasicBlock* nextCodegenBlock; // for extended trace codegen
240 GrowableList dfsOrder;
241 GrowableList domPostOrderTraversal;
242 ArenaBitVector* tryBlockAddr;
243 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
244 ArenaBitVector* tempBlockV;
245 ArenaBitVector* tempDalvikRegisterV;
246 ArenaBitVector* tempSSARegisterV; // numSSARegs
247 bool printSSANames;
248 void* blockLabelList;
249 bool quitLoopMode; // cold path/complex bytecode
250 int preservedRegsUsed; // How many callee save regs used
251 /*
252 * Frame layout details. TODO: Reorganize, remove reduncancy
253 * and move elsewhere. Some of this is already in struct Method,
254 * at least frameSize should eventually move there. Delay regorg
255 * until we get a feel for how this will be used by the low-level
256 * codegen utilities. "num" fields are in 4-byte words, "Size" and
257 * "Offset" in bytes.
258 */
259 int numIns;
260 int numOuts;
261 int numRegs; // Unlike struct Method, does not include ins
262 int numSpills; // NOTE: includes numFPSpills
263 int numFPSpills;
264 int numPadding; // # of 4-byte padding cells
265 int regsOffset; // sp-relative offset to beginning of Dalvik regs
266 int insOffset; // sp-relative offset to beginning of Dalvik ins
267 int frameSize;
268 unsigned int coreSpillMask;
269 unsigned int fpSpillMask;
270 /*
271 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
272 * mechanism to propogate the original Dalvik opcode address to the
273 * associated generated instructions. For the trace compiler, this wasn't
274 * necessary because the interpreter handled all throws and debugging
275 * requests. For now we'll handle this by placing the Dalvik offset
276 * in the CompilationUnit struct before codegen for each instruction.
277 * The low-level LIR creation utilites will pull it from here. Should
278 * be rewritten.
279 */
280 int currentDalvikOffset;
281 GrowableList switchTables;
282 int mappingTableSize;
283 MappingTable* mappingTable;
284 GrowableList fillArrayData;
285 const u2* insns;
286 u4 insnsSize;
287} CompilationUnit;
288
289BasicBlock* oatNewBB(BBType blockType, int blockId);
290
291void oatAppendMIR(BasicBlock* bb, MIR* mir);
292
293void oatPrependMIR(BasicBlock* bb, MIR* mir);
294
295void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
296
297void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
298
299void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
300
301void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
302
303/* Debug Utilities */
304void oatDumpCompilationUnit(CompilationUnit* cUnit);
305
306#endif // ART_SRC_COMPILER_COMPILER_IR_H_