blob: c652f0c00dc8acae839275e4b19c907741af96ed [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
Elliott Hughesa0e18062012-04-13 15:59:59 -070020#include <vector>
buzbeeeaf09bc2012-11-15 14:51:41 -080021#include "dex_instruction.h"
22#include "compiler.h"
buzbeeefc63692012-11-14 16:31:52 -080023#include "compiler_utility.h"
buzbee31a4a6f2012-02-28 15:36:15 -080024#include "oat_compilation_unit.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025#include "safe_map.h"
buzbee2cfc6392012-05-07 14:51:40 -070026#include "greenland/ir_builder.h"
27#include "llvm/Module.h"
buzbeecbd6d442012-11-17 14:11:25 -080028#include "compiler_enums.h"
buzbee67bf8852011-08-17 17:51:35 -070029
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080030namespace art {
31
buzbeefa57c472012-11-21 12:06:18 -080032#define SLOW_FIELD_PATH (cu->enable_debug & (1 << kDebugSlowFieldPath))
33#define SLOW_INVOKE_PATH (cu->enable_debug & (1 << kDebugSlowInvokePath))
34#define SLOW_STRING_PATH (cu->enable_debug & (1 << kDebugSlowStringPath))
35#define SLOW_TYPE_PATH (cu->enable_debug & (1 << kDebugSlowTypePath))
36#define EXERCISE_SLOWEST_STRING_PATH (cu->enable_debug & \
Bill Buzbeea114add2012-05-03 15:00:40 -070037 (1 << kDebugSlowestStringPath))
buzbee31a4a6f2012-02-28 15:36:15 -080038
buzbee02031b12012-11-23 09:41:35 -080039// Minimum field size to contain Dalvik v_reg number.
buzbeeca7a5e42012-08-20 11:12:18 -070040#define VREG_NUM_WIDTH 16
41
buzbeeeaf09bc2012-11-15 14:51:41 -080042struct ArenaBitVector;
43struct LIR;
44class LLVMInfo;
45
Elliott Hughes719ace42012-03-09 18:06:03 -080046struct PromotionMap {
buzbeefa57c472012-11-21 12:06:18 -080047 RegLocationType core_location:3;
48 uint8_t core_reg;
49 RegLocationType fp_location:3;
buzbee52a77fc2012-11-20 19:50:46 -080050 uint8_t FpReg;
buzbeefa57c472012-11-21 12:06:18 -080051 bool first_in_pair;
Elliott Hughes719ace42012-03-09 18:06:03 -080052};
buzbee67bc2362011-10-11 18:08:40 -070053
Elliott Hughes719ace42012-03-09 18:06:03 -080054struct RegLocation {
Bill Buzbeea114add2012-05-03 15:00:40 -070055 RegLocationType location:3;
56 unsigned wide:1;
57 unsigned defined:1; // Do we know the type?
buzbee02031b12012-11-23 09:41:35 -080058 unsigned is_const:1; // Constant, value in cu->constant_values[].
Bill Buzbeea114add2012-05-03 15:00:40 -070059 unsigned fp:1; // Floating point?
60 unsigned core:1; // Non-floating point?
buzbee02031b12012-11-23 09:41:35 -080061 unsigned ref:1; // Something GC cares about.
62 unsigned high_word:1; // High word of pair?
Bill Buzbeea114add2012-05-03 15:00:40 -070063 unsigned home:1; // Does this represent the home location?
buzbee02031b12012-11-23 09:41:35 -080064 uint8_t low_reg; // First physical register.
65 uint8_t high_reg; // 2nd physical register (if wide).
66 int32_t s_reg_low; // SSA name for low Dalvik word.
67 int32_t orig_sreg; // TODO: remove after Bitcode gen complete
68 // and consolodate usage w/ s_reg_low.
buzbeee1965672012-03-11 18:39:19 -070069};
70
71struct CompilerTemp {
buzbeefa57c472012-11-21 12:06:18 -080072 int s_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -070073 ArenaBitVector* bv;
Elliott Hughes719ace42012-03-09 18:06:03 -080074};
buzbee67bf8852011-08-17 17:51:35 -070075
buzbee3b3dbdd2012-06-13 13:39:34 -070076struct CallInfo {
buzbee02031b12012-11-23 09:41:35 -080077 int num_arg_words; // Note: word count, not arg count.
78 RegLocation* args; // One for each word of arguments.
79 RegLocation result; // Eventual target of MOVE_RESULT.
buzbeefa57c472012-11-21 12:06:18 -080080 int opt_flags;
buzbee15bf9802012-06-12 17:49:27 -070081 InvokeType type;
buzbeefa57c472012-11-21 12:06:18 -080082 uint32_t dex_idx;
buzbee02031b12012-11-23 09:41:35 -080083 uint32_t index; // Method idx for invokes, type idx for FilledNewArray.
buzbeefa57c472012-11-21 12:06:18 -080084 uintptr_t direct_code;
85 uintptr_t direct_method;
buzbee02031b12012-11-23 09:41:35 -080086 RegLocation target; // Target of following move_result.
buzbeefa57c472012-11-21 12:06:18 -080087 bool skip_this;
88 bool is_range;
buzbee02031b12012-11-23 09:41:35 -080089 int offset; // Dalvik offset.
buzbee15bf9802012-06-12 17:49:27 -070090};
91
buzbeee3acd072012-02-25 17:03:10 -080092 /*
93 * Data structure tracking the mapping between a Dalvik register (pair) and a
94 * native register (pair). The idea is to reuse the previously loaded value
95 * if possible, otherwise to keep the value in a native register as long as
96 * possible.
97 */
Elliott Hughes719ace42012-03-09 18:06:03 -080098struct RegisterInfo {
Bill Buzbeea114add2012-05-03 15:00:40 -070099 int reg; // Reg number
buzbee02031b12012-11-23 09:41:35 -0800100 bool in_use; // Has it been allocated?
101 bool is_temp; // Can allocate as temp?
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 bool pair; // Part of a register pair?
buzbee02031b12012-11-23 09:41:35 -0800103 int partner; // If pair, other reg of pair.
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 bool live; // Is there an associated SSA name?
105 bool dirty; // If live, is it dirty?
buzbee02031b12012-11-23 09:41:35 -0800106 int s_reg; // Name of live value.
107 LIR *def_start; // Starting inst in last def sequence.
108 LIR *def_end; // Ending inst in last def sequence.
Elliott Hughes719ace42012-03-09 18:06:03 -0800109};
buzbeee3acd072012-02-25 17:03:10 -0800110
Elliott Hughes719ace42012-03-09 18:06:03 -0800111struct RegisterPool {
buzbeefa57c472012-11-21 12:06:18 -0800112 int num_core_regs;
113 RegisterInfo *core_regs;
114 int next_core_reg;
115 int num_fp_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700116 RegisterInfo *FPRegs;
buzbeefa57c472012-11-21 12:06:18 -0800117 int next_fp_reg;
Elliott Hughes719ace42012-03-09 18:06:03 -0800118};
buzbeee3acd072012-02-25 17:03:10 -0800119
buzbee67bf8852011-08-17 17:51:35 -0700120#define INVALID_SREG (-1)
buzbee3ddc0d12011-10-05 10:36:21 -0700121#define INVALID_VREG (0xFFFFU)
buzbee67bc2362011-10-11 18:08:40 -0700122#define INVALID_REG (0xFF)
buzbeeb046e162012-10-30 15:48:42 -0700123#define INVALID_OFFSET (0xDEADF00FU)
buzbee67bf8852011-08-17 17:51:35 -0700124
buzbeee1965672012-03-11 18:39:19 -0700125/* SSA encodings for special registers */
buzbee9c044ce2012-03-18 13:24:07 -0700126#define SSA_METHOD_BASEREG (-2)
buzbeee1965672012-03-11 18:39:19 -0700127/* First compiler temp basereg, grows smaller */
buzbee9c044ce2012-03-18 13:24:07 -0700128#define SSA_CTEMP_BASEREG (SSA_METHOD_BASEREG - 1)
buzbeee1965672012-03-11 18:39:19 -0700129
buzbee99ba9642012-01-25 14:23:14 -0800130/*
131 * Some code patterns cause the generation of excessively large
132 * methods - in particular initialization sequences. There isn't much
133 * benefit in optimizing these methods, and the cost can be very high.
134 * We attempt to identify these cases, and avoid performing most dataflow
135 * analysis. Two thresholds are used - one for known initializers and one
buzbee5abfa3e2012-01-31 17:01:43 -0800136 * for everything else.
buzbee99ba9642012-01-25 14:23:14 -0800137 */
buzbee5abfa3e2012-01-31 17:01:43 -0800138#define MANY_BLOCKS_INITIALIZER 1000 /* Threshold for switching dataflow off */
139#define MANY_BLOCKS 4000 /* Non-initializer threshold */
buzbee99ba9642012-01-25 14:23:14 -0800140
buzbee02031b12012-11-23 09:41:35 -0800141// Utility macros to traverse the LIR list.
buzbee31a4a6f2012-02-28 15:36:15 -0800142#define NEXT_LIR(lir) (lir->next)
143#define PREV_LIR(lir) (lir->prev)
144
buzbee02031b12012-11-23 09:41:35 -0800145// Defines for alias_info (tracks Dalvik register references).
buzbeeec137432012-11-13 12:13:16 -0800146#define DECODE_ALIAS_INFO_REG(X) (X & 0xffff)
147#define DECODE_ALIAS_INFO_WIDE_FLAG (0x80000000)
148#define DECODE_ALIAS_INFO_WIDE(X) ((X & DECODE_ALIAS_INFO_WIDE_FLAG) ? 1 : 0)
149#define ENCODE_ALIAS_INFO(REG, ISWIDE) (REG | (ISWIDE ? DECODE_ALIAS_INFO_WIDE_FLAG : 0))
150
buzbee02031b12012-11-23 09:41:35 -0800151// Common resource macros.
buzbeeec137432012-11-13 12:13:16 -0800152#define ENCODE_CCODE (1ULL << kCCode)
153#define ENCODE_FP_STATUS (1ULL << kFPStatus)
154
buzbee02031b12012-11-23 09:41:35 -0800155// Abstract memory locations.
buzbeeec137432012-11-13 12:13:16 -0800156#define ENCODE_DALVIK_REG (1ULL << kDalvikReg)
157#define ENCODE_LITERAL (1ULL << kLiteral)
158#define ENCODE_HEAP_REF (1ULL << kHeapRef)
159#define ENCODE_MUST_NOT_ALIAS (1ULL << kMustNotAlias)
160
161#define ENCODE_ALL (~0ULL)
162#define ENCODE_MEM (ENCODE_DALVIK_REG | ENCODE_LITERAL | \
163 ENCODE_HEAP_REF | ENCODE_MUST_NOT_ALIAS)
164
buzbeefa57c472012-11-21 12:06:18 -0800165#define is_pseudo_opcode(opcode) (static_cast<int>(opcode) < 0)
buzbee1bc37c62012-11-20 13:35:41 -0800166
Elliott Hughes719ace42012-03-09 18:06:03 -0800167struct LIR {
buzbee02031b12012-11-23 09:41:35 -0800168 int offset; // Offset of this instruction.
169 int dalvik_offset; // Offset of Dalvik opcode.
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 LIR* next;
171 LIR* prev;
172 LIR* target;
173 int opcode;
buzbee02031b12012-11-23 09:41:35 -0800174 int operands[5]; // [0..4] = [dest, src1, src2, extra, extra2].
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 struct {
buzbee02031b12012-11-23 09:41:35 -0800176 bool is_nop:1; // LIR is optimized away.
177 bool pcRelFixup:1; // May need pc-relative fixup.
178 unsigned int size:5; // Note: size is in bytes.
buzbeef5f5a122012-09-21 13:57:36 -0700179 unsigned int unused:25;
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 } flags;
buzbee02031b12012-11-23 09:41:35 -0800181 int alias_info; // For Dalvik register & litpool disambiguation.
182 uint64_t use_mask; // Resource mask for use.
183 uint64_t def_mask; // Resource mask for def.
Elliott Hughes719ace42012-03-09 18:06:03 -0800184};
buzbee67bf8852011-08-17 17:51:35 -0700185
buzbeefa57c472012-11-21 12:06:18 -0800186extern const char* extended_mir_op_names[kMirOpLast - kMirOpFirst];
buzbee67bf8852011-08-17 17:51:35 -0700187
188struct SSARepresentation;
189
buzbee67bf8852011-08-17 17:51:35 -0700190#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
191#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
192#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
193#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
194#define MIR_INLINED (1 << kMIRInlined)
195#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
196#define MIR_CALLEE (1 << kMIRCallee)
buzbeec1f45042011-09-21 16:03:19 -0700197#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
buzbeee1965672012-03-11 18:39:19 -0700198#define MIR_DUP (1 << kMIRDup)
buzbee67bf8852011-08-17 17:51:35 -0700199
buzbeed1643e42012-09-05 14:06:51 -0700200struct Checkstats {
buzbeefa57c472012-11-21 12:06:18 -0800201 int null_checks;
202 int null_checks_eliminated;
203 int range_checks;
204 int range_checks_eliminated;
buzbeed1643e42012-09-05 14:06:51 -0700205};
206
Elliott Hughes719ace42012-03-09 18:06:03 -0800207struct MIR {
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 DecodedInstruction dalvikInsn;
209 unsigned int width;
210 unsigned int offset;
211 MIR* prev;
212 MIR* next;
buzbeefa57c472012-11-21 12:06:18 -0800213 SSARepresentation* ssa_rep;
214 int optimization_flags;
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 union {
buzbee02031b12012-11-23 09:41:35 -0800216 // Establish link between two halves of throwing instructions.
buzbeefa57c472012-11-21 12:06:18 -0800217 MIR* throw_insn;
buzbeea169e1d2012-12-05 14:26:44 -0800218 // Saved opcode for NOP'd MIRs
219 Instruction::Code original_opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 } meta;
Elliott Hughes719ace42012-03-09 18:06:03 -0800221};
buzbee67bf8852011-08-17 17:51:35 -0700222
223struct BasicBlockDataFlow;
224
Elliott Hughes719ace42012-03-09 18:06:03 -0800225struct BasicBlock {
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 int id;
buzbeefa57c472012-11-21 12:06:18 -0800227 int dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 bool visited;
229 bool hidden;
buzbeefa57c472012-11-21 12:06:18 -0800230 bool catch_entry;
231 bool explicit_throw;
232 bool conditional_branch;
buzbeebbdd0532013-02-07 09:33:02 -0800233 bool terminated_by_return; // Block ends with a Dalvik return opcode.
234 bool dominates_return; // Is a member of return extended basic block.
buzbeefa57c472012-11-21 12:06:18 -0800235 uint16_t start_offset;
236 uint16_t nesting_depth;
237 BBType block_type;
238 MIR* first_mir_insn;
239 MIR* last_mir_insn;
240 BasicBlock* fall_through;
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 BasicBlock* taken;
buzbee02031b12012-11-23 09:41:35 -0800242 BasicBlock* i_dom; // Immediate dominator.
buzbeefa57c472012-11-21 12:06:18 -0800243 BasicBlockDataFlow* data_flow_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 GrowableList* predecessors;
245 ArenaBitVector* dominators;
buzbee02031b12012-11-23 09:41:35 -0800246 ArenaBitVector* i_dominated; // Set nodes being immediately dominated.
247 ArenaBitVector* dom_frontier; // Dominance frontier.
248 struct { // For one-to-many successors like.
249 BlockListType block_list_type; // switch and exception handling.
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 GrowableList blocks;
buzbeefa57c472012-11-21 12:06:18 -0800251 } successor_block_list;
Elliott Hughes719ace42012-03-09 18:06:03 -0800252};
buzbee67bf8852011-08-17 17:51:35 -0700253
254/*
buzbeefa57c472012-11-21 12:06:18 -0800255 * The "blocks" field in "successor_block_list" points to an array of
buzbee67bf8852011-08-17 17:51:35 -0700256 * elements with the type "SuccessorBlockInfo".
257 * For catch blocks, key is type index for the exception.
258 * For swtich blocks, key is the case value.
259 */
Elliott Hughes719ace42012-03-09 18:06:03 -0800260struct SuccessorBlockInfo {
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 BasicBlock* block;
262 int key;
Elliott Hughes719ace42012-03-09 18:06:03 -0800263};
buzbee67bf8852011-08-17 17:51:35 -0700264
265struct LoopAnalysis;
266struct RegisterPool;
buzbeeba938cb2012-02-03 14:47:55 -0800267struct ArenaMemBlock;
268struct Memstats;
buzbee02031b12012-11-23 09:41:35 -0800269class Codegen;
buzbee67bf8852011-08-17 17:51:35 -0700270
buzbee5b537102012-01-17 17:33:47 -0800271#define NOTVISITED (-1)
272
Elliott Hughes719ace42012-03-09 18:06:03 -0800273struct CompilationUnit {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700274 CompilationUnit()
buzbeefa57c472012-11-21 12:06:18 -0800275 : num_blocks(0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 compiler(NULL),
277 class_linker(NULL),
278 dex_file(NULL),
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 class_loader(NULL),
Ian Rogersfffdb022013-01-04 15:14:08 -0800280 class_def_idx(0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 method_idx(0),
282 code_item(NULL),
283 access_flags(0),
Ian Rogers08f753d2012-08-24 14:35:25 -0700284 invoke_type(kDirect),
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 shorty(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800286 first_lir_insn(NULL),
287 last_lir_insn(NULL),
288 literal_list(NULL),
289 method_literal_list(NULL),
290 code_literal_list(NULL),
291 disable_opt(0),
292 enable_debug(0),
293 data_offset(0),
294 total_size(0),
295 assembler_status(kSuccess),
296 assembler_retries(0),
297 verbose(false),
298 has_loop(false),
299 has_invoke(false),
300 qd_mode(false),
301 reg_pool(NULL),
302 instruction_set(kNone),
303 num_ssa_regs(0),
304 ssa_base_vregs(NULL),
305 ssa_subscripts(NULL),
306 ssa_strings(NULL),
307 vreg_to_ssa_map(NULL),
308 ssa_last_defs(NULL),
309 is_constant_v(NULL),
buzbee4ef3e452012-12-14 13:35:28 -0800310 must_flush_constant_v(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800311 constant_values(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800312 reg_location(NULL),
313 promotion_map(NULL),
314 method_sreg(0),
315 num_reachable_blocks(0),
316 num_dalvik_registers(0),
317 entry_block(NULL),
318 exit_block(NULL),
319 cur_block(NULL),
320 i_dom_list(NULL),
321 try_block_addr(NULL),
322 def_block_matrix(NULL),
323 temp_block_v(NULL),
324 temp_dalvik_register_v(NULL),
325 temp_ssa_register_v(NULL),
326 temp_ssa_block_id_v(NULL),
327 block_label_list(NULL),
328 num_ins(0),
329 num_outs(0),
330 num_regs(0),
331 num_core_spills(0),
332 num_fp_spills(0),
333 num_compiler_temps(0),
334 frame_size(0),
335 core_spill_mask(0U),
336 fp_spill_mask(0U),
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 attrs(0U),
buzbeefa57c472012-11-21 12:06:18 -0800338 current_dalvik_offset(0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 insns(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800340 insns_size(0U),
341 disable_dataflow(false),
342 def_count(0),
343 compiler_flip_match(false),
344 arena_head(NULL),
345 current_arena(NULL),
346 num_arena_blocks(0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700347 mstats(NULL),
buzbeed1643e42012-09-05 14:06:51 -0700348 checkstats(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800349 gen_bitcode(false),
Ian Rogersfffdb022013-01-04 15:14:08 -0800350 llvm_info(NULL),
buzbee2cfc6392012-05-07 14:51:40 -0700351 context(NULL),
352 module(NULL),
353 func(NULL),
354 intrinsic_helper(NULL),
355 irb(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800356 placeholder_bb(NULL),
357 entry_bb(NULL),
358 entryTarget_bb(NULL),
359 temp_name(0),
buzbee2cfc6392012-05-07 14:51:40 -0700360#ifndef NDEBUG
buzbeefa57c472012-11-21 12:06:18 -0800361 live_sreg(0),
buzbee2cfc6392012-05-07 14:51:40 -0700362#endif
buzbee02031b12012-11-23 09:41:35 -0800363 opcode_count(NULL),
364 cg(NULL) {}
Elliott Hughese52e49b2012-04-02 16:05:44 -0700365
buzbeefa57c472012-11-21 12:06:18 -0800366 int num_blocks;
367 GrowableList block_list;
buzbee02031b12012-11-23 09:41:35 -0800368 Compiler* compiler; // Compiler driving this compiler.
369 ClassLinker* class_linker; // Linker to resolve fields and methods.
370 const DexFile* dex_file; // DexFile containing the method being compiled.
371 jobject class_loader; // compiling method's class loader.
Ian Rogersfffdb022013-01-04 15:14:08 -0800372 uint32_t class_def_idx; // compiling method's defining class definition index.
buzbee02031b12012-11-23 09:41:35 -0800373 uint32_t method_idx; // compiling method's index into method_ids of DexFile.
374 const DexFile::CodeItem* code_item; // compiling method's DexFile code_item.
375 uint32_t access_flags; // compiling method's access flags.
376 InvokeType invoke_type; // compiling method's invocation type.
377 const char* shorty; // compiling method's shorty.
buzbeefa57c472012-11-21 12:06:18 -0800378 LIR* first_lir_insn;
379 LIR* last_lir_insn;
buzbee02031b12012-11-23 09:41:35 -0800380 LIR* literal_list; // Constants.
381 LIR* method_literal_list; // Method literals requiring patching.
382 LIR* code_literal_list; // Code literals requiring patching.
383 uint32_t disable_opt; // opt_control_vector flags.
384 uint32_t enable_debug; // debugControlVector flags.
385 int data_offset; // starting offset of literal pool.
386 int total_size; // header + code size.
387 AssemblerStatus assembler_status; // Success or fix and retry.
buzbeefa57c472012-11-21 12:06:18 -0800388 int assembler_retries;
389 std::vector<uint8_t> code_buffer;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700390 /*
391 * Holds mapping from native PC to dex PC for safepoints where we may deoptimize.
392 * Native PC is on the return address of the safepointed operation. Dex PC is for
393 * the instruction being executed at the safepoint.
394 */
395 std::vector<uint32_t> pc2dexMappingTable;
396 /*
397 * Holds mapping from Dex PC to native PC for catch entry points. Native PC and Dex PC
398 * immediately preceed the instruction.
399 */
400 std::vector<uint32_t> dex2pcMappingTable;
buzbeefa57c472012-11-21 12:06:18 -0800401 std::vector<uint32_t> combined_mapping_table;
402 std::vector<uint32_t> core_vmap_table;
403 std::vector<uint32_t> fp_vmap_table;
404 std::vector<uint8_t> native_gc_map;
buzbee2502e002012-12-31 16:05:53 -0800405 std::vector<BasicBlock*> extended_basic_blocks;
buzbeefa57c472012-11-21 12:06:18 -0800406 bool verbose;
buzbee02031b12012-11-23 09:41:35 -0800407 bool has_loop; // Contains a loop.
408 bool has_invoke; // Contains an invoke instruction.
409 bool qd_mode; // Compile for code size/compile time.
buzbeefa57c472012-11-21 12:06:18 -0800410 RegisterPool* reg_pool;
411 InstructionSet instruction_set;
buzbee02031b12012-11-23 09:41:35 -0800412 // Number of total regs used in the whole cu after SSA transformation .
buzbeefa57c472012-11-21 12:06:18 -0800413 int num_ssa_regs;
buzbee02031b12012-11-23 09:41:35 -0800414 // Map SSA reg i to the base virtual register/subscript.
buzbeefa57c472012-11-21 12:06:18 -0800415 GrowableList* ssa_base_vregs;
416 GrowableList* ssa_subscripts;
417 GrowableList* ssa_strings;
buzbee67bf8852011-08-17 17:51:35 -0700418
buzbee02031b12012-11-23 09:41:35 -0800419 // Map original Dalvik virtual reg i to the current SSA name.
420 int* vreg_to_ssa_map; // length == method->registers_size
421 int* ssa_last_defs; // length == method->registers_size
422 ArenaBitVector* is_constant_v; // length == num_ssa_reg
buzbee4ef3e452012-12-14 13:35:28 -0800423 ArenaBitVector* must_flush_constant_v; // length == num_ssa_reg
buzbee02031b12012-11-23 09:41:35 -0800424 int* constant_values; // length == num_ssa_reg
buzbee67bf8852011-08-17 17:51:35 -0700425
buzbee02031b12012-11-23 09:41:35 -0800426 // Use counts of ssa names.
427 GrowableList use_counts; // Weighted by nesting depth
428 GrowableList raw_use_counts; // Not weighted
buzbee239c4e72012-03-16 08:42:29 -0700429
buzbee02031b12012-11-23 09:41:35 -0800430 // Optimization support.
buzbeefa57c472012-11-21 12:06:18 -0800431 GrowableList loop_headers;
buzbee239c4e72012-03-16 08:42:29 -0700432
buzbee02031b12012-11-23 09:41:35 -0800433 // Map SSA names to location.
buzbeefa57c472012-11-21 12:06:18 -0800434 RegLocation* reg_location;
buzbee67bf8852011-08-17 17:51:35 -0700435
buzbee02031b12012-11-23 09:41:35 -0800436 // Keep track of Dalvik v_reg to physical register mappings.
buzbeefa57c472012-11-21 12:06:18 -0800437 PromotionMap* promotion_map;
buzbee67bc2362011-10-11 18:08:40 -0700438
buzbee02031b12012-11-23 09:41:35 -0800439 // SSA name for Method*.
buzbeefa57c472012-11-21 12:06:18 -0800440 int method_sreg;
buzbee02031b12012-11-23 09:41:35 -0800441 RegLocation method_loc; // Describes location of method*.
buzbeee1965672012-03-11 18:39:19 -0700442
buzbeefa57c472012-11-21 12:06:18 -0800443 int num_reachable_blocks;
buzbee02031b12012-11-23 09:41:35 -0800444 int num_dalvik_registers; // method->registers_size.
buzbeefa57c472012-11-21 12:06:18 -0800445 BasicBlock* entry_block;
446 BasicBlock* exit_block;
447 BasicBlock* cur_block;
448 GrowableList dfs_order;
449 GrowableList dfs_post_order;
450 GrowableList dom_post_order_traversal;
451 GrowableList throw_launchpads;
452 GrowableList suspend_launchpads;
453 GrowableList intrinsic_launchpads;
454 GrowableList compiler_temps;
455 int* i_dom_list;
456 ArenaBitVector* try_block_addr;
buzbee02031b12012-11-23 09:41:35 -0800457 ArenaBitVector** def_block_matrix; // num_dalvik_register x num_blocks.
buzbeefa57c472012-11-21 12:06:18 -0800458 ArenaBitVector* temp_block_v;
459 ArenaBitVector* temp_dalvik_register_v;
buzbee02031b12012-11-23 09:41:35 -0800460 ArenaBitVector* temp_ssa_register_v; // num_ssa_regs.
461 int* temp_ssa_block_id_v; // working storage for Phi labels.
buzbeefa57c472012-11-21 12:06:18 -0800462 LIR* block_label_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 /*
464 * Frame layout details.
465 * NOTE: for debug support it will be necessary to add a structure
466 * to map the Dalvik virtual registers to the promoted registers.
467 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
468 */
buzbeefa57c472012-11-21 12:06:18 -0800469 int num_ins;
470 int num_outs;
buzbee02031b12012-11-23 09:41:35 -0800471 int num_regs; // Unlike num_dalvik_registers, does not include ins.
buzbeefa57c472012-11-21 12:06:18 -0800472 int num_core_spills;
473 int num_fp_spills;
474 int num_compiler_temps;
475 int frame_size;
476 unsigned int core_spill_mask;
477 unsigned int fp_spill_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 unsigned int attrs;
479 /*
buzbee02031b12012-11-23 09:41:35 -0800480 * TODO: The code generation utilities don't have a built-in
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 * mechanism to propagate the original Dalvik opcode address to the
482 * associated generated instructions. For the trace compiler, this wasn't
483 * necessary because the interpreter handled all throws and debugging
484 * requests. For now we'll handle this by placing the Dalvik offset
485 * in the CompilationUnit struct before codegen for each instruction.
buzbee02031b12012-11-23 09:41:35 -0800486 * The low-level LIR creation utilites will pull it from here. Rework this.
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 */
buzbeefa57c472012-11-21 12:06:18 -0800488 int current_dalvik_offset;
489 GrowableList switch_tables;
490 GrowableList fill_array_data;
buzbeeeaf09bc2012-11-15 14:51:41 -0800491 const uint16_t* insns;
buzbeefa57c472012-11-21 12:06:18 -0800492 uint32_t insns_size;
493 bool disable_dataflow; // Skip dataflow analysis if possible
buzbee02031b12012-11-23 09:41:35 -0800494 SafeMap<unsigned int, BasicBlock*> block_map; // FindBlock lookup cache.
495 SafeMap<unsigned int, unsigned int> block_id_map; // Block collapse lookup cache.
496 SafeMap<unsigned int, LIR*> boundary_map; // boundary lookup cache.
497 int def_count; // Used to estimate number of SSA names.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700498
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 // If non-empty, apply optimizer/debug flags only to matching methods.
buzbeefa57c472012-11-21 12:06:18 -0800500 std::string compiler_method_match;
501 // Flips sense of compiler_method_match - apply flags if doesn't match.
502 bool compiler_flip_match;
503 ArenaMemBlock* arena_head;
504 ArenaMemBlock* current_arena;
505 int num_arena_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 Memstats* mstats;
buzbeed1643e42012-09-05 14:06:51 -0700507 Checkstats* checkstats;
buzbeefa57c472012-11-21 12:06:18 -0800508 bool gen_bitcode;
buzbee4df2bbd2012-10-11 14:46:06 -0700509 LLVMInfo* llvm_info;
buzbee2cfc6392012-05-07 14:51:40 -0700510 llvm::LLVMContext* context;
511 llvm::Module* module;
512 llvm::Function* func;
513 greenland::IntrinsicHelper* intrinsic_helper;
514 greenland::IRBuilder* irb;
buzbeefa57c472012-11-21 12:06:18 -0800515 llvm::BasicBlock* placeholder_bb;
516 llvm::BasicBlock* entry_bb;
517 llvm::BasicBlock* entryTarget_bb;
buzbee2cfc6392012-05-07 14:51:40 -0700518 std::string bitcode_filename;
buzbeefa57c472012-11-21 12:06:18 -0800519 GrowableList llvm_values;
520 int32_t temp_name;
buzbee02031b12012-11-23 09:41:35 -0800521 SafeMap<llvm::BasicBlock*, LIR*> block_to_label_map; // llvm bb -> LIR label.
522 SafeMap<int32_t, llvm::BasicBlock*> id_to_block_map; // block id -> llvm bb.
523 SafeMap<llvm::Value*, RegLocation> loc_map; // llvm Value to loc rec.
buzbeefa57c472012-11-21 12:06:18 -0800524 std::set<llvm::BasicBlock*> llvm_blocks;
buzbee3d661942012-03-14 17:37:27 -0700525#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 /*
527 * Sanity checking for the register temp tracking. The same ssa
528 * name should never be associated with one temp register per
529 * instruction compilation.
530 */
buzbeefa57c472012-11-21 12:06:18 -0800531 int live_sreg;
buzbee3d661942012-03-14 17:37:27 -0700532#endif
buzbee6459e7c2012-10-02 14:42:41 -0700533 std::set<uint32_t> catches;
buzbee02031b12012-11-23 09:41:35 -0800534 int* opcode_count; // Count Dalvik opcodes for tuning.
535 UniquePtr<Codegen> cg;
Elliott Hughes719ace42012-03-09 18:06:03 -0800536};
buzbee67bf8852011-08-17 17:51:35 -0700537
Elliott Hughes719ace42012-03-09 18:06:03 -0800538struct SwitchTable {
Bill Buzbeea114add2012-05-03 15:00:40 -0700539 int offset;
buzbee02031b12012-11-23 09:41:35 -0800540 const uint16_t* table; // Original dex table.
541 int vaddr; // Dalvik offset of switch opcode.
542 LIR* anchor; // Reference instruction for relative offsets.
543 LIR** targets; // Array of case targets.
Elliott Hughes719ace42012-03-09 18:06:03 -0800544};
buzbee5de34942012-03-01 14:51:57 -0800545
Elliott Hughes719ace42012-03-09 18:06:03 -0800546struct FillArrayData {
Bill Buzbeea114add2012-05-03 15:00:40 -0700547 int offset;
buzbee02031b12012-11-23 09:41:35 -0800548 const uint16_t* table; // Original dex table.
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 int size;
buzbee02031b12012-11-23 09:41:35 -0800550 int vaddr; // Dalvik offset of FILL_ARRAY_DATA opcode.
Elliott Hughes719ace42012-03-09 18:06:03 -0800551};
buzbee5de34942012-03-01 14:51:57 -0800552
buzbee16da88c2012-03-20 10:38:17 -0700553#define MAX_PATTERN_LEN 5
554
buzbee16da88c2012-03-20 10:38:17 -0700555struct CodePattern {
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 const Instruction::Code opcodes[MAX_PATTERN_LEN];
buzbeefa57c472012-11-21 12:06:18 -0800557 const SpecialCaseHandler handler_code;
buzbee16da88c2012-03-20 10:38:17 -0700558};
559
buzbeefa57c472012-11-21 12:06:18 -0800560static const CodePattern special_patterns[] = {
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 {{Instruction::RETURN_VOID}, kNullMethod},
562 {{Instruction::CONST, Instruction::RETURN}, kConstFunction},
563 {{Instruction::CONST_4, Instruction::RETURN}, kConstFunction},
564 {{Instruction::CONST_4, Instruction::RETURN_OBJECT}, kConstFunction},
565 {{Instruction::CONST_16, Instruction::RETURN}, kConstFunction},
566 {{Instruction::IGET, Instruction:: RETURN}, kIGet},
567 {{Instruction::IGET_BOOLEAN, Instruction::RETURN}, kIGetBoolean},
568 {{Instruction::IGET_OBJECT, Instruction::RETURN_OBJECT}, kIGetObject},
569 {{Instruction::IGET_BYTE, Instruction::RETURN}, kIGetByte},
570 {{Instruction::IGET_CHAR, Instruction::RETURN}, kIGetChar},
571 {{Instruction::IGET_SHORT, Instruction::RETURN}, kIGetShort},
572 {{Instruction::IGET_WIDE, Instruction::RETURN_WIDE}, kIGetWide},
573 {{Instruction::IPUT, Instruction::RETURN_VOID}, kIPut},
574 {{Instruction::IPUT_BOOLEAN, Instruction::RETURN_VOID}, kIPutBoolean},
575 {{Instruction::IPUT_OBJECT, Instruction::RETURN_VOID}, kIPutObject},
576 {{Instruction::IPUT_BYTE, Instruction::RETURN_VOID}, kIPutByte},
577 {{Instruction::IPUT_CHAR, Instruction::RETURN_VOID}, kIPutChar},
578 {{Instruction::IPUT_SHORT, Instruction::RETURN_VOID}, kIPutShort},
579 {{Instruction::IPUT_WIDE, Instruction::RETURN_VOID}, kIPutWide},
580 {{Instruction::RETURN}, kIdentity},
581 {{Instruction::RETURN_OBJECT}, kIdentity},
582 {{Instruction::RETURN_WIDE}, kIdentity},
buzbee16da88c2012-03-20 10:38:17 -0700583};
buzbee5de34942012-03-01 14:51:57 -0800584
buzbee4ef3e452012-12-14 13:35:28 -0800585static inline bool IsConst(const CompilationUnit* cu, int32_t s_reg)
586{
587 return (IsBitSet(cu->is_constant_v, s_reg));
588}
589
590static inline bool IsConst(const CompilationUnit* cu, RegLocation loc)
591{
592 return (IsConst(cu, loc.orig_sreg));
593}
594
595static inline int32_t ConstantValue(const CompilationUnit* cu, RegLocation loc)
596{
597 DCHECK(IsConst(cu, loc));
598 return cu->constant_values[loc.orig_sreg];
599}
600
601static inline int64_t ConstantValueWide(const CompilationUnit* cu, RegLocation loc)
602{
603 DCHECK(IsConst(cu, loc));
604 return (static_cast<int64_t>(cu->constant_values[loc.orig_sreg + 1]) << 32) |
605 Low32Bits(static_cast<int64_t>(cu->constant_values[loc.orig_sreg]));
606}
607
608static inline bool MustFlushConstant(const CompilationUnit* cu, RegLocation loc)
609{
610 DCHECK(IsConst(cu, loc));
611 return IsBitSet(cu->must_flush_constant_v, loc.orig_sreg);
612}
613
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800614} // namespace art
615
buzbee67bf8852011-08-17 17:51:35 -0700616#endif // ART_SRC_COMPILER_COMPILER_IR_H_