blob: e25acd9fb9f92dcc6a971d7aa44188419f37eac3 [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;
buzbee43a36422011-09-14 14:00:13 -0700114 int optimizationFlags;
buzbee67bf8852011-08-17 17:51:35 -0700115 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;
buzbee43a36422011-09-14 14:00:13 -0700138 bool catchEntry;
buzbee67bf8852011-08-17 17:51:35 -0700139 unsigned int startOffset;
140 const Method* containingMethod; // For blocks from the callee
141 BBType blockType;
142 bool needFallThroughBranch; // For blocks ended due to length limit
143 bool isFallThroughFromInvoke; // True means the block needs alignment
144 MIR* firstMIRInsn;
145 MIR* lastMIRInsn;
146 struct BasicBlock* fallThrough;
147 struct BasicBlock* taken;
148 struct BasicBlock* iDom; // Immediate dominator
149 struct BasicBlockDataFlow* dataFlowInfo;
150 ArenaBitVector* predecessors;
151 ArenaBitVector* dominators;
152 ArenaBitVector* iDominated; // Set nodes being immediately dominated
153 ArenaBitVector* domFrontier; // Dominance frontier
154 struct { // For one-to-many successors like
155 BlockListType blockListType; // switch and exception handling
156 GrowableList blocks;
157 } successorBlockList;
158} BasicBlock;
159
160/*
161 * The "blocks" field in "successorBlockList" points to an array of
162 * elements with the type "SuccessorBlockInfo".
163 * For catch blocks, key is type index for the exception.
164 * For swtich blocks, key is the case value.
165 */
166typedef struct SuccessorBlockInfo {
167 BasicBlock* block;
168 int key;
169} SuccessorBlockInfo;
170
171struct LoopAnalysis;
172struct RegisterPool;
173
174typedef enum AssemblerStatus {
175 kSuccess,
176 kRetryAll,
177 kRetryHalve
178} AssemblerStatus;
179
buzbee67bf8852011-08-17 17:51:35 -0700180typedef struct CompilationUnit {
181 int numInsts;
182 int numBlocks;
183 GrowableList blockList;
184 const Method *method;
185 LIR* firstLIRInsn;
186 LIR* lastLIRInsn;
187 LIR* literalList; // Constants
188 LIR* classPointerList; // Relocatable
189 int numClassPointers;
190 LIR* chainCellOffsetLIR;
191 int disableOpt;
192 int headerSize; // bytes before the first code ptr
193 int dataOffset; // starting offset of literal pool
194 int totalSize; // header + code size
195 AssemblerStatus assemblerStatus; // Success or fix and retry
196 int assemblerRetries;
buzbee4ef76522011-09-08 10:00:32 -0700197 std::vector<short> codeBuffer;
198 std::vector<uint32_t> mappingTable;
buzbee67bf8852011-08-17 17:51:35 -0700199 bool printMe;
200 bool printMeVerbose;
buzbeeec5adf32011-09-11 15:25:43 -0700201 bool dumpCFG;
buzbee67bf8852011-08-17 17:51:35 -0700202 bool hasClassLiterals; // Contains class ptrs used as literals
203 bool hasLoop; // Contains a loop
204 bool hasInvoke; // Contains an invoke instruction
205 bool heapMemOp; // Mark mem ops for self verification
206 bool usesLinkRegister; // For self-verification only
207 bool methodTraceSupport; // For TraceView profiling
208 struct RegisterPool* regPool;
209 int optRound; // round number to tell an LIR's age
210 OatInstructionSetType instructionSet;
211 /* Number of total regs used in the whole cUnit after SSA transformation */
212 int numSSARegs;
213 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
214 GrowableList* ssaToDalvikMap;
215
216 /* The following are new data structures to support SSA representations */
217 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
218 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700219 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700220 ArenaBitVector* isConstantV; // length == numSSAReg
221 int* constantValues; // length == numSSAReg
222
223 /* Map SSA names to location */
224 RegLocation* regLocation;
225 int sequenceNumber;
226
227 /*
228 * Set to the Dalvik PC of the switch instruction if it has more than
229 * MAX_CHAINED_SWITCH_CASES cases.
230 */
231 const u2* switchOverflowPad;
232
233 int numReachableBlocks;
234 int numDalvikRegisters; // method->registersSize + inlined
235 BasicBlock* entryBlock;
236 BasicBlock* exitBlock;
237 BasicBlock* curBlock;
238 BasicBlock* nextCodegenBlock; // for extended trace codegen
239 GrowableList dfsOrder;
240 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700241 GrowableList throwLaunchpads;
buzbee67bf8852011-08-17 17:51:35 -0700242 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 /*
buzbee5ade1d22011-09-09 14:44:52 -0700252 * Frame layout details.
253 * NOTE: for debug support it will be necessary to add a structure
254 * to map the Dalvik virtual registers to the promoted registers.
255 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700256 */
257 int numIns;
258 int numOuts;
259 int numRegs; // Unlike struct Method, does not include ins
260 int numSpills; // NOTE: includes numFPSpills
261 int numFPSpills;
262 int numPadding; // # of 4-byte padding cells
263 int regsOffset; // sp-relative offset to beginning of Dalvik regs
264 int insOffset; // sp-relative offset to beginning of Dalvik ins
265 int frameSize;
266 unsigned int coreSpillMask;
267 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700268 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700269 /*
270 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
271 * mechanism to propogate the original Dalvik opcode address to the
272 * associated generated instructions. For the trace compiler, this wasn't
273 * necessary because the interpreter handled all throws and debugging
274 * requests. For now we'll handle this by placing the Dalvik offset
275 * in the CompilationUnit struct before codegen for each instruction.
276 * The low-level LIR creation utilites will pull it from here. Should
277 * be rewritten.
278 */
279 int currentDalvikOffset;
280 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700281 GrowableList fillArrayData;
282 const u2* insns;
283 u4 insnsSize;
284} CompilationUnit;
285
286BasicBlock* oatNewBB(BBType blockType, int blockId);
287
288void oatAppendMIR(BasicBlock* bb, MIR* mir);
289
290void oatPrependMIR(BasicBlock* bb, MIR* mir);
291
292void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
293
294void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
295
296void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
297
298void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
299
300/* Debug Utilities */
301void oatDumpCompilationUnit(CompilationUnit* cUnit);
302
303#endif // ART_SRC_COMPILER_COMPILER_IR_H_