blob: 8dcf56bdd43361a9c6589c0b817fd1c39f73ea2e [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)
buzbee3ddc0d12011-10-05 10:36:21 -070050#define INVALID_VREG (0xFFFFU)
buzbee67bf8852011-08-17 17:51:35 -070051#define INVALID_REG (0x3F)
52#define INVALID_OFFSET (-1)
53
54typedef enum BBType {
55 kEntryBlock,
56 kDalvikByteCode,
57 kExitBlock,
58 kExceptionHandling,
59 kCatchEntry,
60} BBType;
61
62typedef struct LIR {
63 int offset; // Offset of this instruction
64 int dalvikOffset; // Offset of Dalvik opcode
65 struct LIR* next;
66 struct LIR* prev;
67 struct LIR* target;
68} LIR;
69
70enum ExtendedMIROpcode {
71 kMirOpFirst = kNumPackedOpcodes,
72 kMirOpPhi = kMirOpFirst,
73 kMirOpNullNRangeUpCheck,
74 kMirOpNullNRangeDownCheck,
75 kMirOpLowerBound,
76 kMirOpPunt,
77 kMirOpCheckInlinePrediction, // Gen checks for predicted inlining
78 kMirOpLast,
79};
80
81struct SSARepresentation;
82
83typedef enum {
84 kMIRIgnoreNullCheck = 0,
85 kMIRNullCheckOnly,
86 kMIRIgnoreRangeCheck,
87 kMIRRangeCheckOnly,
88 kMIRInlined, // Invoke is inlined (ie dead)
89 kMIRInlinedPred, // Invoke is inlined via prediction
90 kMIRCallee, // Instruction is inlined from callee
buzbeec1f45042011-09-21 16:03:19 -070091 kMIRIgnoreSuspendCheck,
buzbee67bf8852011-08-17 17:51:35 -070092} MIROptimizationFlagPositons;
93
94#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
95#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
96#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
97#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
98#define MIR_INLINED (1 << kMIRInlined)
99#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
100#define MIR_CALLEE (1 << kMIRCallee)
buzbeec1f45042011-09-21 16:03:19 -0700101#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
buzbee67bf8852011-08-17 17:51:35 -0700102
103typedef struct CallsiteInfo {
104 const char* classDescriptor;
105 Object* classLoader;
106 const Method* method;
107 LIR* misPredBranchOver;
108} CallsiteInfo;
109
110typedef struct MIR {
111 DecodedInstruction dalvikInsn;
112 unsigned int width;
113 unsigned int offset;
114 struct MIR* prev;
115 struct MIR* next;
116 struct SSARepresentation* ssaRep;
buzbee43a36422011-09-14 14:00:13 -0700117 int optimizationFlags;
buzbee67bf8852011-08-17 17:51:35 -0700118 int seqNum;
119 union {
120 // Used by the inlined insn from the callee to find the mother method
121 const Method* calleeMethod;
122 // Used by the inlined invoke to find the class and method pointers
123 CallsiteInfo* callsiteInfo;
buzbeec0ecd652011-09-25 18:11:54 -0700124 // Used to quickly locate all Phi opcodes
125 struct MIR* phiNext;
buzbee67bf8852011-08-17 17:51:35 -0700126 } meta;
127} MIR;
128
129struct BasicBlockDataFlow;
130
131/* For successorBlockList */
132typedef enum BlockListType {
133 kNotUsed = 0,
134 kCatch,
135 kPackedSwitch,
136 kSparseSwitch,
137} BlockListType;
138
139typedef struct BasicBlock {
140 int id;
141 bool visited;
142 bool hidden;
buzbee43a36422011-09-14 14:00:13 -0700143 bool catchEntry;
buzbee67bf8852011-08-17 17:51:35 -0700144 unsigned int startOffset;
145 const Method* containingMethod; // For blocks from the callee
146 BBType blockType;
147 bool needFallThroughBranch; // For blocks ended due to length limit
148 bool isFallThroughFromInvoke; // True means the block needs alignment
149 MIR* firstMIRInsn;
150 MIR* lastMIRInsn;
151 struct BasicBlock* fallThrough;
152 struct BasicBlock* taken;
153 struct BasicBlock* iDom; // Immediate dominator
154 struct BasicBlockDataFlow* dataFlowInfo;
155 ArenaBitVector* predecessors;
156 ArenaBitVector* dominators;
157 ArenaBitVector* iDominated; // Set nodes being immediately dominated
158 ArenaBitVector* domFrontier; // Dominance frontier
159 struct { // For one-to-many successors like
160 BlockListType blockListType; // switch and exception handling
161 GrowableList blocks;
162 } successorBlockList;
163} BasicBlock;
164
165/*
166 * The "blocks" field in "successorBlockList" points to an array of
167 * elements with the type "SuccessorBlockInfo".
168 * For catch blocks, key is type index for the exception.
169 * For swtich blocks, key is the case value.
170 */
171typedef struct SuccessorBlockInfo {
172 BasicBlock* block;
173 int key;
174} SuccessorBlockInfo;
175
176struct LoopAnalysis;
177struct RegisterPool;
178
179typedef enum AssemblerStatus {
180 kSuccess,
181 kRetryAll,
182 kRetryHalve
183} AssemblerStatus;
184
buzbee67bf8852011-08-17 17:51:35 -0700185typedef struct CompilationUnit {
186 int numInsts;
187 int numBlocks;
188 GrowableList blockList;
189 const Method *method;
190 LIR* firstLIRInsn;
191 LIR* lastLIRInsn;
192 LIR* literalList; // Constants
193 LIR* classPointerList; // Relocatable
194 int numClassPointers;
195 LIR* chainCellOffsetLIR;
buzbeece302932011-10-04 14:32:18 -0700196 uint32_t disableOpt; // optControlVector flags
197 uint32_t enableDebug; // debugControlVector flags
buzbee67bf8852011-08-17 17:51:35 -0700198 int headerSize; // bytes before the first code ptr
199 int dataOffset; // starting offset of literal pool
200 int totalSize; // header + code size
201 AssemblerStatus assemblerStatus; // Success or fix and retry
202 int assemblerRetries;
buzbee4ef76522011-09-08 10:00:32 -0700203 std::vector<short> codeBuffer;
204 std::vector<uint32_t> mappingTable;
buzbee3ddc0d12011-10-05 10:36:21 -0700205 std::vector<uint16_t> coreVmapTable;
206 std::vector<uint16_t> fpVmapTable;
buzbee67bf8852011-08-17 17:51:35 -0700207 bool printMe;
buzbee67bf8852011-08-17 17:51:35 -0700208 bool hasClassLiterals; // Contains class ptrs used as literals
209 bool hasLoop; // Contains a loop
210 bool hasInvoke; // Contains an invoke instruction
211 bool heapMemOp; // Mark mem ops for self verification
212 bool usesLinkRegister; // For self-verification only
213 bool methodTraceSupport; // For TraceView profiling
214 struct RegisterPool* regPool;
215 int optRound; // round number to tell an LIR's age
216 OatInstructionSetType instructionSet;
217 /* Number of total regs used in the whole cUnit after SSA transformation */
218 int numSSARegs;
219 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
220 GrowableList* ssaToDalvikMap;
221
222 /* The following are new data structures to support SSA representations */
223 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
224 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700225 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700226 ArenaBitVector* isConstantV; // length == numSSAReg
227 int* constantValues; // length == numSSAReg
buzbeec0ecd652011-09-25 18:11:54 -0700228 int* phiAliasMap; // length == numSSAReg
229 MIR* phiList;
buzbee67bf8852011-08-17 17:51:35 -0700230
231 /* Map SSA names to location */
232 RegLocation* regLocation;
233 int sequenceNumber;
234
235 /*
236 * Set to the Dalvik PC of the switch instruction if it has more than
237 * MAX_CHAINED_SWITCH_CASES cases.
238 */
239 const u2* switchOverflowPad;
240
241 int numReachableBlocks;
242 int numDalvikRegisters; // method->registersSize + inlined
243 BasicBlock* entryBlock;
244 BasicBlock* exitBlock;
245 BasicBlock* curBlock;
246 BasicBlock* nextCodegenBlock; // for extended trace codegen
247 GrowableList dfsOrder;
248 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700249 GrowableList throwLaunchpads;
buzbeec1f45042011-09-21 16:03:19 -0700250 GrowableList suspendLaunchpads;
buzbee67bf8852011-08-17 17:51:35 -0700251 ArenaBitVector* tryBlockAddr;
252 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
253 ArenaBitVector* tempBlockV;
254 ArenaBitVector* tempDalvikRegisterV;
255 ArenaBitVector* tempSSARegisterV; // numSSARegs
256 bool printSSANames;
257 void* blockLabelList;
258 bool quitLoopMode; // cold path/complex bytecode
259 int preservedRegsUsed; // How many callee save regs used
260 /*
buzbee5ade1d22011-09-09 14:44:52 -0700261 * Frame layout details.
262 * NOTE: for debug support it will be necessary to add a structure
263 * to map the Dalvik virtual registers to the promoted registers.
264 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700265 */
266 int numIns;
267 int numOuts;
268 int numRegs; // Unlike struct Method, does not include ins
buzbeebbaf8942011-10-02 13:08:29 -0700269 int numCoreSpills;
buzbee67bf8852011-08-17 17:51:35 -0700270 int numFPSpills;
271 int numPadding; // # of 4-byte padding cells
272 int regsOffset; // sp-relative offset to beginning of Dalvik regs
273 int insOffset; // sp-relative offset to beginning of Dalvik ins
274 int frameSize;
275 unsigned int coreSpillMask;
276 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700277 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700278 /*
279 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
buzbee03fa2632011-09-20 17:10:57 -0700280 * mechanism to propagate the original Dalvik opcode address to the
buzbee67bf8852011-08-17 17:51:35 -0700281 * associated generated instructions. For the trace compiler, this wasn't
282 * necessary because the interpreter handled all throws and debugging
283 * requests. For now we'll handle this by placing the Dalvik offset
284 * in the CompilationUnit struct before codegen for each instruction.
285 * The low-level LIR creation utilites will pull it from here. Should
286 * be rewritten.
287 */
288 int currentDalvikOffset;
289 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700290 GrowableList fillArrayData;
291 const u2* insns;
292 u4 insnsSize;
293} CompilationUnit;
294
295BasicBlock* oatNewBB(BBType blockType, int blockId);
296
297void oatAppendMIR(BasicBlock* bb, MIR* mir);
298
299void oatPrependMIR(BasicBlock* bb, MIR* mir);
300
301void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
302
303void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
304
305void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
306
307void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
308
309/* Debug Utilities */
310void oatDumpCompilationUnit(CompilationUnit* cUnit);
311
312#endif // ART_SRC_COMPILER_COMPILER_IR_H_