blob: 2d4f83e93de224c94e40d677c75ff11dd830f732 [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;
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;
buzbee4ef76522011-09-08 10:00:32 -0700201 std::vector<short> codeBuffer;
202 std::vector<uint32_t> mappingTable;
buzbeec41e5b52011-09-23 12:46:19 -0700203 std::vector<uint32_t> coreVmapTable;
204 std::vector<short> fpVmapTable;
buzbee67bf8852011-08-17 17:51:35 -0700205 bool printMe;
206 bool printMeVerbose;
buzbeeec5adf32011-09-11 15:25:43 -0700207 bool dumpCFG;
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_