blob: 14af69d3d6dfd7a65fbf4df2091b353b5599885d [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
buzbeec1f45042011-09-21 16:03:19 -070090 kMIRIgnoreSuspendCheck,
buzbee67bf8852011-08-17 17:51:35 -070091} MIROptimizationFlagPositons;
92
93#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
94#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
95#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
96#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
97#define MIR_INLINED (1 << kMIRInlined)
98#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
99#define MIR_CALLEE (1 << kMIRCallee)
buzbeec1f45042011-09-21 16:03:19 -0700100#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
buzbee67bf8852011-08-17 17:51:35 -0700101
102typedef struct CallsiteInfo {
103 const char* classDescriptor;
104 Object* classLoader;
105 const Method* method;
106 LIR* misPredBranchOver;
107} CallsiteInfo;
108
109typedef struct MIR {
110 DecodedInstruction dalvikInsn;
111 unsigned int width;
112 unsigned int offset;
113 struct MIR* prev;
114 struct MIR* next;
115 struct SSARepresentation* ssaRep;
buzbee43a36422011-09-14 14:00:13 -0700116 int optimizationFlags;
buzbee67bf8852011-08-17 17:51:35 -0700117 int seqNum;
118 union {
119 // Used by the inlined insn from the callee to find the mother method
120 const Method* calleeMethod;
121 // Used by the inlined invoke to find the class and method pointers
122 CallsiteInfo* callsiteInfo;
buzbeec0ecd652011-09-25 18:11:54 -0700123 // Used to quickly locate all Phi opcodes
124 struct MIR* phiNext;
buzbee67bf8852011-08-17 17:51:35 -0700125 } meta;
126} MIR;
127
128struct BasicBlockDataFlow;
129
130/* For successorBlockList */
131typedef enum BlockListType {
132 kNotUsed = 0,
133 kCatch,
134 kPackedSwitch,
135 kSparseSwitch,
136} BlockListType;
137
138typedef struct BasicBlock {
139 int id;
140 bool visited;
141 bool hidden;
buzbee43a36422011-09-14 14:00:13 -0700142 bool catchEntry;
buzbee67bf8852011-08-17 17:51:35 -0700143 unsigned int startOffset;
144 const Method* containingMethod; // For blocks from the callee
145 BBType blockType;
146 bool needFallThroughBranch; // For blocks ended due to length limit
147 bool isFallThroughFromInvoke; // True means the block needs alignment
148 MIR* firstMIRInsn;
149 MIR* lastMIRInsn;
150 struct BasicBlock* fallThrough;
151 struct BasicBlock* taken;
152 struct BasicBlock* iDom; // Immediate dominator
153 struct BasicBlockDataFlow* dataFlowInfo;
154 ArenaBitVector* predecessors;
155 ArenaBitVector* dominators;
156 ArenaBitVector* iDominated; // Set nodes being immediately dominated
157 ArenaBitVector* domFrontier; // Dominance frontier
158 struct { // For one-to-many successors like
159 BlockListType blockListType; // switch and exception handling
160 GrowableList blocks;
161 } successorBlockList;
162} BasicBlock;
163
164/*
165 * The "blocks" field in "successorBlockList" points to an array of
166 * elements with the type "SuccessorBlockInfo".
167 * For catch blocks, key is type index for the exception.
168 * For swtich blocks, key is the case value.
169 */
170typedef struct SuccessorBlockInfo {
171 BasicBlock* block;
172 int key;
173} SuccessorBlockInfo;
174
175struct LoopAnalysis;
176struct RegisterPool;
177
178typedef enum AssemblerStatus {
179 kSuccess,
180 kRetryAll,
181 kRetryHalve
182} AssemblerStatus;
183
buzbee67bf8852011-08-17 17:51:35 -0700184typedef 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;
buzbeece302932011-10-04 14:32:18 -0700195 uint32_t disableOpt; // optControlVector flags
196 uint32_t enableDebug; // debugControlVector flags
buzbee67bf8852011-08-17 17:51:35 -0700197 int headerSize; // bytes before the first code ptr
198 int dataOffset; // starting offset of literal pool
199 int totalSize; // header + code size
200 AssemblerStatus assemblerStatus; // Success or fix and retry
201 int assemblerRetries;
buzbee4ef76522011-09-08 10:00:32 -0700202 std::vector<short> codeBuffer;
203 std::vector<uint32_t> mappingTable;
buzbeec41e5b52011-09-23 12:46:19 -0700204 std::vector<uint32_t> coreVmapTable;
205 std::vector<short> fpVmapTable;
buzbee67bf8852011-08-17 17:51:35 -0700206 bool printMe;
buzbee67bf8852011-08-17 17:51:35 -0700207 bool hasClassLiterals; // Contains class ptrs used as literals
208 bool hasLoop; // Contains a loop
209 bool hasInvoke; // Contains an invoke instruction
210 bool heapMemOp; // Mark mem ops for self verification
211 bool usesLinkRegister; // For self-verification only
212 bool methodTraceSupport; // For TraceView profiling
213 struct RegisterPool* regPool;
214 int optRound; // round number to tell an LIR's age
215 OatInstructionSetType instructionSet;
216 /* Number of total regs used in the whole cUnit after SSA transformation */
217 int numSSARegs;
218 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
219 GrowableList* ssaToDalvikMap;
220
221 /* The following are new data structures to support SSA representations */
222 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
223 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700224 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700225 ArenaBitVector* isConstantV; // length == numSSAReg
226 int* constantValues; // length == numSSAReg
buzbeec0ecd652011-09-25 18:11:54 -0700227 int* phiAliasMap; // length == numSSAReg
228 MIR* phiList;
buzbee67bf8852011-08-17 17:51:35 -0700229
230 /* Map SSA names to location */
231 RegLocation* regLocation;
232 int sequenceNumber;
233
234 /*
235 * Set to the Dalvik PC of the switch instruction if it has more than
236 * MAX_CHAINED_SWITCH_CASES cases.
237 */
238 const u2* switchOverflowPad;
239
240 int numReachableBlocks;
241 int numDalvikRegisters; // method->registersSize + inlined
242 BasicBlock* entryBlock;
243 BasicBlock* exitBlock;
244 BasicBlock* curBlock;
245 BasicBlock* nextCodegenBlock; // for extended trace codegen
246 GrowableList dfsOrder;
247 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700248 GrowableList throwLaunchpads;
buzbeec1f45042011-09-21 16:03:19 -0700249 GrowableList suspendLaunchpads;
buzbee67bf8852011-08-17 17:51:35 -0700250 ArenaBitVector* tryBlockAddr;
251 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
252 ArenaBitVector* tempBlockV;
253 ArenaBitVector* tempDalvikRegisterV;
254 ArenaBitVector* tempSSARegisterV; // numSSARegs
255 bool printSSANames;
256 void* blockLabelList;
257 bool quitLoopMode; // cold path/complex bytecode
258 int preservedRegsUsed; // How many callee save regs used
259 /*
buzbee5ade1d22011-09-09 14:44:52 -0700260 * Frame layout details.
261 * NOTE: for debug support it will be necessary to add a structure
262 * to map the Dalvik virtual registers to the promoted registers.
263 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700264 */
265 int numIns;
266 int numOuts;
267 int numRegs; // Unlike struct Method, does not include ins
buzbeebbaf8942011-10-02 13:08:29 -0700268 int numCoreSpills;
buzbee67bf8852011-08-17 17:51:35 -0700269 int numFPSpills;
270 int numPadding; // # of 4-byte padding cells
271 int regsOffset; // sp-relative offset to beginning of Dalvik regs
272 int insOffset; // sp-relative offset to beginning of Dalvik ins
273 int frameSize;
274 unsigned int coreSpillMask;
275 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700276 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700277 /*
278 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
buzbee03fa2632011-09-20 17:10:57 -0700279 * mechanism to propagate the original Dalvik opcode address to the
buzbee67bf8852011-08-17 17:51:35 -0700280 * associated generated instructions. For the trace compiler, this wasn't
281 * necessary because the interpreter handled all throws and debugging
282 * requests. For now we'll handle this by placing the Dalvik offset
283 * in the CompilationUnit struct before codegen for each instruction.
284 * The low-level LIR creation utilites will pull it from here. Should
285 * be rewritten.
286 */
287 int currentDalvikOffset;
288 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700289 GrowableList fillArrayData;
290 const u2* insns;
291 u4 insnsSize;
292} CompilationUnit;
293
294BasicBlock* oatNewBB(BBType blockType, int blockId);
295
296void oatAppendMIR(BasicBlock* bb, MIR* mir);
297
298void oatPrependMIR(BasicBlock* bb, MIR* mir);
299
300void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
301
302void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
303
304void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
305
306void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
307
308/* Debug Utilities */
309void oatDumpCompilationUnit(CompilationUnit* cUnit);
310
311#endif // ART_SRC_COMPILER_COMPILER_IR_H_