blob: e2418b8e658f3faa976c14d0da7c20939591fde4 [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
buzbee67bf8852011-08-17 17:51:35 -0700179typedef struct CompilationUnit {
180 int numInsts;
181 int numBlocks;
182 GrowableList blockList;
183 const Method *method;
184 LIR* firstLIRInsn;
185 LIR* lastLIRInsn;
186 LIR* literalList; // Constants
187 LIR* classPointerList; // Relocatable
188 int numClassPointers;
189 LIR* chainCellOffsetLIR;
190 int disableOpt;
191 int headerSize; // bytes before the first code ptr
192 int dataOffset; // starting offset of literal pool
193 int totalSize; // header + code size
194 AssemblerStatus assemblerStatus; // Success or fix and retry
195 int assemblerRetries;
buzbee4ef76522011-09-08 10:00:32 -0700196 std::vector<short> codeBuffer;
197 std::vector<uint32_t> mappingTable;
buzbee67bf8852011-08-17 17:51:35 -0700198 bool printMe;
199 bool printMeVerbose;
buzbeeec5adf32011-09-11 15:25:43 -0700200 bool dumpCFG;
buzbee67bf8852011-08-17 17:51:35 -0700201 bool hasClassLiterals; // Contains class ptrs used as literals
202 bool hasLoop; // Contains a loop
203 bool hasInvoke; // Contains an invoke instruction
204 bool heapMemOp; // Mark mem ops for self verification
205 bool usesLinkRegister; // For self-verification only
206 bool methodTraceSupport; // For TraceView profiling
207 struct RegisterPool* regPool;
208 int optRound; // round number to tell an LIR's age
209 OatInstructionSetType instructionSet;
210 /* Number of total regs used in the whole cUnit after SSA transformation */
211 int numSSARegs;
212 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
213 GrowableList* ssaToDalvikMap;
214
215 /* The following are new data structures to support SSA representations */
216 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
217 int* dalvikToSSAMap; // length == method->registersSize
buzbeef0cde542011-09-13 14:55:02 -0700218 int* SSALastDefs; // length == method->registersSize
buzbee67bf8852011-08-17 17:51:35 -0700219 ArenaBitVector* isConstantV; // length == numSSAReg
220 int* constantValues; // length == numSSAReg
221
222 /* Map SSA names to location */
223 RegLocation* regLocation;
224 int sequenceNumber;
225
226 /*
227 * Set to the Dalvik PC of the switch instruction if it has more than
228 * MAX_CHAINED_SWITCH_CASES cases.
229 */
230 const u2* switchOverflowPad;
231
232 int numReachableBlocks;
233 int numDalvikRegisters; // method->registersSize + inlined
234 BasicBlock* entryBlock;
235 BasicBlock* exitBlock;
236 BasicBlock* curBlock;
237 BasicBlock* nextCodegenBlock; // for extended trace codegen
238 GrowableList dfsOrder;
239 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700240 GrowableList throwLaunchpads;
buzbee67bf8852011-08-17 17:51:35 -0700241 ArenaBitVector* tryBlockAddr;
242 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
243 ArenaBitVector* tempBlockV;
244 ArenaBitVector* tempDalvikRegisterV;
245 ArenaBitVector* tempSSARegisterV; // numSSARegs
246 bool printSSANames;
247 void* blockLabelList;
248 bool quitLoopMode; // cold path/complex bytecode
249 int preservedRegsUsed; // How many callee save regs used
250 /*
buzbee5ade1d22011-09-09 14:44:52 -0700251 * Frame layout details.
252 * NOTE: for debug support it will be necessary to add a structure
253 * to map the Dalvik virtual registers to the promoted registers.
254 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700255 */
256 int numIns;
257 int numOuts;
258 int numRegs; // Unlike struct Method, does not include ins
259 int numSpills; // NOTE: includes numFPSpills
260 int numFPSpills;
261 int numPadding; // # of 4-byte padding cells
262 int regsOffset; // sp-relative offset to beginning of Dalvik regs
263 int insOffset; // sp-relative offset to beginning of Dalvik ins
264 int frameSize;
265 unsigned int coreSpillMask;
266 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700267 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700268 /*
269 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
270 * mechanism to propogate the original Dalvik opcode address to the
271 * associated generated instructions. For the trace compiler, this wasn't
272 * necessary because the interpreter handled all throws and debugging
273 * requests. For now we'll handle this by placing the Dalvik offset
274 * in the CompilationUnit struct before codegen for each instruction.
275 * The low-level LIR creation utilites will pull it from here. Should
276 * be rewritten.
277 */
278 int currentDalvikOffset;
279 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700280 GrowableList fillArrayData;
281 const u2* insns;
282 u4 insnsSize;
283} CompilationUnit;
284
285BasicBlock* oatNewBB(BBType blockType, int blockId);
286
287void oatAppendMIR(BasicBlock* bb, MIR* mir);
288
289void oatPrependMIR(BasicBlock* bb, MIR* mir);
290
291void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
292
293void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
294
295void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
296
297void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
298
299/* Debug Utilities */
300void oatDumpCompilationUnit(CompilationUnit* cUnit);
301
302#endif // ART_SRC_COMPILER_COMPILER_IR_H_