blob: e9933ffee5993a47d5d7e1048f39de8de89ca39f [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;
200 bool hasClassLiterals; // Contains class ptrs used as literals
201 bool hasLoop; // Contains a loop
202 bool hasInvoke; // Contains an invoke instruction
203 bool heapMemOp; // Mark mem ops for self verification
204 bool usesLinkRegister; // For self-verification only
205 bool methodTraceSupport; // For TraceView profiling
206 struct RegisterPool* regPool;
207 int optRound; // round number to tell an LIR's age
208 OatInstructionSetType instructionSet;
209 /* Number of total regs used in the whole cUnit after SSA transformation */
210 int numSSARegs;
211 /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
212 GrowableList* ssaToDalvikMap;
213
214 /* The following are new data structures to support SSA representations */
215 /* Map original Dalvik reg i to the SSA[15..0]/Sub[31..16] pair */
216 int* dalvikToSSAMap; // length == method->registersSize
217 ArenaBitVector* isConstantV; // length == numSSAReg
218 int* constantValues; // length == numSSAReg
219
220 /* Map SSA names to location */
221 RegLocation* regLocation;
222 int sequenceNumber;
223
224 /*
225 * Set to the Dalvik PC of the switch instruction if it has more than
226 * MAX_CHAINED_SWITCH_CASES cases.
227 */
228 const u2* switchOverflowPad;
229
230 int numReachableBlocks;
231 int numDalvikRegisters; // method->registersSize + inlined
232 BasicBlock* entryBlock;
233 BasicBlock* exitBlock;
234 BasicBlock* curBlock;
235 BasicBlock* nextCodegenBlock; // for extended trace codegen
236 GrowableList dfsOrder;
237 GrowableList domPostOrderTraversal;
buzbee5ade1d22011-09-09 14:44:52 -0700238 GrowableList throwLaunchpads;
buzbee67bf8852011-08-17 17:51:35 -0700239 ArenaBitVector* tryBlockAddr;
240 ArenaBitVector** defBlockMatrix; // numDalvikRegister x numBlocks
241 ArenaBitVector* tempBlockV;
242 ArenaBitVector* tempDalvikRegisterV;
243 ArenaBitVector* tempSSARegisterV; // numSSARegs
244 bool printSSANames;
245 void* blockLabelList;
246 bool quitLoopMode; // cold path/complex bytecode
247 int preservedRegsUsed; // How many callee save regs used
248 /*
buzbee5ade1d22011-09-09 14:44:52 -0700249 * Frame layout details.
250 * NOTE: for debug support it will be necessary to add a structure
251 * to map the Dalvik virtual registers to the promoted registers.
252 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
buzbee67bf8852011-08-17 17:51:35 -0700253 */
254 int numIns;
255 int numOuts;
256 int numRegs; // Unlike struct Method, does not include ins
257 int numSpills; // NOTE: includes numFPSpills
258 int numFPSpills;
259 int numPadding; // # of 4-byte padding cells
260 int regsOffset; // sp-relative offset to beginning of Dalvik regs
261 int insOffset; // sp-relative offset to beginning of Dalvik ins
262 int frameSize;
263 unsigned int coreSpillMask;
264 unsigned int fpSpillMask;
buzbeecefd1872011-09-09 09:59:52 -0700265 unsigned int attrs;
buzbee67bf8852011-08-17 17:51:35 -0700266 /*
267 * CLEANUP/RESTRUCTURE: The code generation utilities don't have a built-in
268 * mechanism to propogate the original Dalvik opcode address to the
269 * associated generated instructions. For the trace compiler, this wasn't
270 * necessary because the interpreter handled all throws and debugging
271 * requests. For now we'll handle this by placing the Dalvik offset
272 * in the CompilationUnit struct before codegen for each instruction.
273 * The low-level LIR creation utilites will pull it from here. Should
274 * be rewritten.
275 */
276 int currentDalvikOffset;
277 GrowableList switchTables;
buzbee67bf8852011-08-17 17:51:35 -0700278 GrowableList fillArrayData;
279 const u2* insns;
280 u4 insnsSize;
281} CompilationUnit;
282
283BasicBlock* oatNewBB(BBType blockType, int blockId);
284
285void oatAppendMIR(BasicBlock* bb, MIR* mir);
286
287void oatPrependMIR(BasicBlock* bb, MIR* mir);
288
289void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR);
290
291void oatAppendLIR(CompilationUnit* cUnit, LIR* lir);
292
293void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR);
294
295void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR);
296
297/* Debug Utilities */
298void oatDumpCompilationUnit(CompilationUnit* cUnit);
299
300#endif // ART_SRC_COMPILER_COMPILER_IR_H_