blob: f8cdd34b604d25494347eeef44cf16c72d516edc [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
buzbee395116c2013-02-27 14:30:25 -080017#ifndef ART_SRC_COMPILER_DEX_COMPILER_IR_H_
18#define ART_SRC_COMPILER_DEX_COMPILER_IR_H_
buzbee67bf8852011-08-17 17:51:35 -070019
Elliott Hughesa0e18062012-04-13 15:59:59 -070020#include <vector>
Brian Carlstrom265091e2013-01-30 14:08:26 -080021
22#include <llvm/Module.h>
23
24#include "compiler/dex/quick/codegen.h"
Ian Rogers1212a022013-03-04 10:48:41 -080025#include "compiler/driver/compiler_driver.h"
Ian Rogers89756f22013-03-04 16:40:02 -080026#include "compiler/driver/dex_compilation_unit.h"
Ian Rogers4c1c2832013-03-04 18:30:13 -080027#include "compiler/llvm/intrinsic_helper.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080028#include "compiler/llvm/ir_builder.h"
buzbeecbd6d442012-11-17 14:11:25 -080029#include "compiler_enums.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080030#include "compiler_utility.h"
31#include "dex_instruction.h"
32#include "safe_map.h"
buzbee67bf8852011-08-17 17:51:35 -070033
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080034namespace art {
35
buzbeefa57c472012-11-21 12:06:18 -080036#define SLOW_FIELD_PATH (cu->enable_debug & (1 << kDebugSlowFieldPath))
37#define SLOW_INVOKE_PATH (cu->enable_debug & (1 << kDebugSlowInvokePath))
38#define SLOW_STRING_PATH (cu->enable_debug & (1 << kDebugSlowStringPath))
39#define SLOW_TYPE_PATH (cu->enable_debug & (1 << kDebugSlowTypePath))
40#define EXERCISE_SLOWEST_STRING_PATH (cu->enable_debug & \
Bill Buzbeea114add2012-05-03 15:00:40 -070041 (1 << kDebugSlowestStringPath))
buzbee31a4a6f2012-02-28 15:36:15 -080042
buzbee02031b12012-11-23 09:41:35 -080043// Minimum field size to contain Dalvik v_reg number.
buzbeeca7a5e42012-08-20 11:12:18 -070044#define VREG_NUM_WIDTH 16
45
buzbeeeaf09bc2012-11-15 14:51:41 -080046struct ArenaBitVector;
47struct LIR;
48class LLVMInfo;
Brian Carlstrom265091e2013-01-30 14:08:26 -080049namespace llvm {
50class LlvmCompilationUnit;
51} // namespace llvm
buzbeeeaf09bc2012-11-15 14:51:41 -080052
Elliott Hughes719ace42012-03-09 18:06:03 -080053struct PromotionMap {
buzbeefa57c472012-11-21 12:06:18 -080054 RegLocationType core_location:3;
55 uint8_t core_reg;
56 RegLocationType fp_location:3;
buzbee52a77fc2012-11-20 19:50:46 -080057 uint8_t FpReg;
buzbeefa57c472012-11-21 12:06:18 -080058 bool first_in_pair;
Elliott Hughes719ace42012-03-09 18:06:03 -080059};
buzbee67bc2362011-10-11 18:08:40 -070060
Elliott Hughes719ace42012-03-09 18:06:03 -080061struct RegLocation {
Bill Buzbeea114add2012-05-03 15:00:40 -070062 RegLocationType location:3;
63 unsigned wide:1;
64 unsigned defined:1; // Do we know the type?
buzbee02031b12012-11-23 09:41:35 -080065 unsigned is_const:1; // Constant, value in cu->constant_values[].
Bill Buzbeea114add2012-05-03 15:00:40 -070066 unsigned fp:1; // Floating point?
67 unsigned core:1; // Non-floating point?
buzbee02031b12012-11-23 09:41:35 -080068 unsigned ref:1; // Something GC cares about.
69 unsigned high_word:1; // High word of pair?
Bill Buzbeea114add2012-05-03 15:00:40 -070070 unsigned home:1; // Does this represent the home location?
buzbee02031b12012-11-23 09:41:35 -080071 uint8_t low_reg; // First physical register.
72 uint8_t high_reg; // 2nd physical register (if wide).
73 int32_t s_reg_low; // SSA name for low Dalvik word.
74 int32_t orig_sreg; // TODO: remove after Bitcode gen complete
75 // and consolodate usage w/ s_reg_low.
buzbeee1965672012-03-11 18:39:19 -070076};
77
78struct CompilerTemp {
buzbeefa57c472012-11-21 12:06:18 -080079 int s_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -070080 ArenaBitVector* bv;
Elliott Hughes719ace42012-03-09 18:06:03 -080081};
buzbee67bf8852011-08-17 17:51:35 -070082
buzbee3b3dbdd2012-06-13 13:39:34 -070083struct CallInfo {
buzbee02031b12012-11-23 09:41:35 -080084 int num_arg_words; // Note: word count, not arg count.
85 RegLocation* args; // One for each word of arguments.
86 RegLocation result; // Eventual target of MOVE_RESULT.
buzbeefa57c472012-11-21 12:06:18 -080087 int opt_flags;
buzbee15bf9802012-06-12 17:49:27 -070088 InvokeType type;
buzbeefa57c472012-11-21 12:06:18 -080089 uint32_t dex_idx;
buzbee02031b12012-11-23 09:41:35 -080090 uint32_t index; // Method idx for invokes, type idx for FilledNewArray.
buzbeefa57c472012-11-21 12:06:18 -080091 uintptr_t direct_code;
92 uintptr_t direct_method;
buzbee02031b12012-11-23 09:41:35 -080093 RegLocation target; // Target of following move_result.
buzbeefa57c472012-11-21 12:06:18 -080094 bool skip_this;
95 bool is_range;
buzbee02031b12012-11-23 09:41:35 -080096 int offset; // Dalvik offset.
buzbee15bf9802012-06-12 17:49:27 -070097};
98
buzbeee3acd072012-02-25 17:03:10 -080099 /*
100 * Data structure tracking the mapping between a Dalvik register (pair) and a
101 * native register (pair). The idea is to reuse the previously loaded value
102 * if possible, otherwise to keep the value in a native register as long as
103 * possible.
104 */
Elliott Hughes719ace42012-03-09 18:06:03 -0800105struct RegisterInfo {
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 int reg; // Reg number
buzbee02031b12012-11-23 09:41:35 -0800107 bool in_use; // Has it been allocated?
108 bool is_temp; // Can allocate as temp?
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 bool pair; // Part of a register pair?
buzbee02031b12012-11-23 09:41:35 -0800110 int partner; // If pair, other reg of pair.
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 bool live; // Is there an associated SSA name?
112 bool dirty; // If live, is it dirty?
buzbee02031b12012-11-23 09:41:35 -0800113 int s_reg; // Name of live value.
114 LIR *def_start; // Starting inst in last def sequence.
115 LIR *def_end; // Ending inst in last def sequence.
Elliott Hughes719ace42012-03-09 18:06:03 -0800116};
buzbeee3acd072012-02-25 17:03:10 -0800117
Elliott Hughes719ace42012-03-09 18:06:03 -0800118struct RegisterPool {
buzbeefa57c472012-11-21 12:06:18 -0800119 int num_core_regs;
120 RegisterInfo *core_regs;
121 int next_core_reg;
122 int num_fp_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 RegisterInfo *FPRegs;
buzbeefa57c472012-11-21 12:06:18 -0800124 int next_fp_reg;
Elliott Hughes719ace42012-03-09 18:06:03 -0800125};
buzbeee3acd072012-02-25 17:03:10 -0800126
buzbee67bf8852011-08-17 17:51:35 -0700127#define INVALID_SREG (-1)
buzbee3ddc0d12011-10-05 10:36:21 -0700128#define INVALID_VREG (0xFFFFU)
buzbee67bc2362011-10-11 18:08:40 -0700129#define INVALID_REG (0xFF)
buzbeeb046e162012-10-30 15:48:42 -0700130#define INVALID_OFFSET (0xDEADF00FU)
buzbee67bf8852011-08-17 17:51:35 -0700131
buzbeee1965672012-03-11 18:39:19 -0700132/* SSA encodings for special registers */
buzbee9c044ce2012-03-18 13:24:07 -0700133#define SSA_METHOD_BASEREG (-2)
buzbeee1965672012-03-11 18:39:19 -0700134/* First compiler temp basereg, grows smaller */
buzbee9c044ce2012-03-18 13:24:07 -0700135#define SSA_CTEMP_BASEREG (SSA_METHOD_BASEREG - 1)
buzbeee1965672012-03-11 18:39:19 -0700136
buzbee99ba9642012-01-25 14:23:14 -0800137/*
138 * Some code patterns cause the generation of excessively large
139 * methods - in particular initialization sequences. There isn't much
140 * benefit in optimizing these methods, and the cost can be very high.
141 * We attempt to identify these cases, and avoid performing most dataflow
142 * analysis. Two thresholds are used - one for known initializers and one
buzbee5abfa3e2012-01-31 17:01:43 -0800143 * for everything else.
buzbee99ba9642012-01-25 14:23:14 -0800144 */
buzbee5abfa3e2012-01-31 17:01:43 -0800145#define MANY_BLOCKS_INITIALIZER 1000 /* Threshold for switching dataflow off */
146#define MANY_BLOCKS 4000 /* Non-initializer threshold */
buzbee99ba9642012-01-25 14:23:14 -0800147
buzbee02031b12012-11-23 09:41:35 -0800148// Utility macros to traverse the LIR list.
buzbee31a4a6f2012-02-28 15:36:15 -0800149#define NEXT_LIR(lir) (lir->next)
150#define PREV_LIR(lir) (lir->prev)
151
buzbee02031b12012-11-23 09:41:35 -0800152// Defines for alias_info (tracks Dalvik register references).
buzbeeec137432012-11-13 12:13:16 -0800153#define DECODE_ALIAS_INFO_REG(X) (X & 0xffff)
154#define DECODE_ALIAS_INFO_WIDE_FLAG (0x80000000)
155#define DECODE_ALIAS_INFO_WIDE(X) ((X & DECODE_ALIAS_INFO_WIDE_FLAG) ? 1 : 0)
156#define ENCODE_ALIAS_INFO(REG, ISWIDE) (REG | (ISWIDE ? DECODE_ALIAS_INFO_WIDE_FLAG : 0))
157
buzbee02031b12012-11-23 09:41:35 -0800158// Common resource macros.
buzbeeec137432012-11-13 12:13:16 -0800159#define ENCODE_CCODE (1ULL << kCCode)
160#define ENCODE_FP_STATUS (1ULL << kFPStatus)
161
buzbee02031b12012-11-23 09:41:35 -0800162// Abstract memory locations.
buzbeeec137432012-11-13 12:13:16 -0800163#define ENCODE_DALVIK_REG (1ULL << kDalvikReg)
164#define ENCODE_LITERAL (1ULL << kLiteral)
165#define ENCODE_HEAP_REF (1ULL << kHeapRef)
166#define ENCODE_MUST_NOT_ALIAS (1ULL << kMustNotAlias)
167
168#define ENCODE_ALL (~0ULL)
169#define ENCODE_MEM (ENCODE_DALVIK_REG | ENCODE_LITERAL | \
170 ENCODE_HEAP_REF | ENCODE_MUST_NOT_ALIAS)
171
buzbeefa57c472012-11-21 12:06:18 -0800172#define is_pseudo_opcode(opcode) (static_cast<int>(opcode) < 0)
buzbee1bc37c62012-11-20 13:35:41 -0800173
Elliott Hughes719ace42012-03-09 18:06:03 -0800174struct LIR {
buzbee02031b12012-11-23 09:41:35 -0800175 int offset; // Offset of this instruction.
176 int dalvik_offset; // Offset of Dalvik opcode.
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 LIR* next;
178 LIR* prev;
179 LIR* target;
180 int opcode;
buzbee02031b12012-11-23 09:41:35 -0800181 int operands[5]; // [0..4] = [dest, src1, src2, extra, extra2].
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 struct {
buzbee02031b12012-11-23 09:41:35 -0800183 bool is_nop:1; // LIR is optimized away.
184 bool pcRelFixup:1; // May need pc-relative fixup.
185 unsigned int size:5; // Note: size is in bytes.
buzbeef5f5a122012-09-21 13:57:36 -0700186 unsigned int unused:25;
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 } flags;
buzbee02031b12012-11-23 09:41:35 -0800188 int alias_info; // For Dalvik register & litpool disambiguation.
189 uint64_t use_mask; // Resource mask for use.
190 uint64_t def_mask; // Resource mask for def.
Elliott Hughes719ace42012-03-09 18:06:03 -0800191};
buzbee67bf8852011-08-17 17:51:35 -0700192
buzbeefa57c472012-11-21 12:06:18 -0800193extern const char* extended_mir_op_names[kMirOpLast - kMirOpFirst];
buzbee67bf8852011-08-17 17:51:35 -0700194
195struct SSARepresentation;
196
buzbee67bf8852011-08-17 17:51:35 -0700197#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
198#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
199#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
200#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
201#define MIR_INLINED (1 << kMIRInlined)
202#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
203#define MIR_CALLEE (1 << kMIRCallee)
buzbeec1f45042011-09-21 16:03:19 -0700204#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
buzbeee1965672012-03-11 18:39:19 -0700205#define MIR_DUP (1 << kMIRDup)
buzbee67bf8852011-08-17 17:51:35 -0700206
buzbeed1643e42012-09-05 14:06:51 -0700207struct Checkstats {
buzbeefa57c472012-11-21 12:06:18 -0800208 int null_checks;
209 int null_checks_eliminated;
210 int range_checks;
211 int range_checks_eliminated;
buzbeed1643e42012-09-05 14:06:51 -0700212};
213
Elliott Hughes719ace42012-03-09 18:06:03 -0800214struct MIR {
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 DecodedInstruction dalvikInsn;
216 unsigned int width;
217 unsigned int offset;
218 MIR* prev;
219 MIR* next;
buzbeefa57c472012-11-21 12:06:18 -0800220 SSARepresentation* ssa_rep;
221 int optimization_flags;
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 union {
buzbee02031b12012-11-23 09:41:35 -0800223 // Establish link between two halves of throwing instructions.
buzbeefa57c472012-11-21 12:06:18 -0800224 MIR* throw_insn;
buzbeea169e1d2012-12-05 14:26:44 -0800225 // Saved opcode for NOP'd MIRs
226 Instruction::Code original_opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 } meta;
Elliott Hughes719ace42012-03-09 18:06:03 -0800228};
buzbee67bf8852011-08-17 17:51:35 -0700229
230struct BasicBlockDataFlow;
231
Elliott Hughes719ace42012-03-09 18:06:03 -0800232struct BasicBlock {
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 int id;
buzbeefa57c472012-11-21 12:06:18 -0800234 int dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 bool visited;
236 bool hidden;
buzbeefa57c472012-11-21 12:06:18 -0800237 bool catch_entry;
238 bool explicit_throw;
239 bool conditional_branch;
buzbeebbdd0532013-02-07 09:33:02 -0800240 bool terminated_by_return; // Block ends with a Dalvik return opcode.
241 bool dominates_return; // Is a member of return extended basic block.
buzbeefa57c472012-11-21 12:06:18 -0800242 uint16_t start_offset;
243 uint16_t nesting_depth;
244 BBType block_type;
245 MIR* first_mir_insn;
246 MIR* last_mir_insn;
247 BasicBlock* fall_through;
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 BasicBlock* taken;
buzbee02031b12012-11-23 09:41:35 -0800249 BasicBlock* i_dom; // Immediate dominator.
buzbeefa57c472012-11-21 12:06:18 -0800250 BasicBlockDataFlow* data_flow_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 GrowableList* predecessors;
252 ArenaBitVector* dominators;
buzbee02031b12012-11-23 09:41:35 -0800253 ArenaBitVector* i_dominated; // Set nodes being immediately dominated.
254 ArenaBitVector* dom_frontier; // Dominance frontier.
255 struct { // For one-to-many successors like.
256 BlockListType block_list_type; // switch and exception handling.
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 GrowableList blocks;
buzbeefa57c472012-11-21 12:06:18 -0800258 } successor_block_list;
Elliott Hughes719ace42012-03-09 18:06:03 -0800259};
buzbee67bf8852011-08-17 17:51:35 -0700260
261/*
buzbeefa57c472012-11-21 12:06:18 -0800262 * The "blocks" field in "successor_block_list" points to an array of
buzbee67bf8852011-08-17 17:51:35 -0700263 * elements with the type "SuccessorBlockInfo".
264 * For catch blocks, key is type index for the exception.
265 * For swtich blocks, key is the case value.
266 */
Elliott Hughes719ace42012-03-09 18:06:03 -0800267struct SuccessorBlockInfo {
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 BasicBlock* block;
269 int key;
Elliott Hughes719ace42012-03-09 18:06:03 -0800270};
buzbee67bf8852011-08-17 17:51:35 -0700271
272struct LoopAnalysis;
273struct RegisterPool;
buzbeeba938cb2012-02-03 14:47:55 -0800274struct ArenaMemBlock;
275struct Memstats;
buzbee02031b12012-11-23 09:41:35 -0800276class Codegen;
buzbee67bf8852011-08-17 17:51:35 -0700277
buzbee5b537102012-01-17 17:33:47 -0800278#define NOTVISITED (-1)
279
Elliott Hughes719ace42012-03-09 18:06:03 -0800280struct CompilationUnit {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700281 CompilationUnit()
buzbeefa57c472012-11-21 12:06:18 -0800282 : num_blocks(0),
Ian Rogers1212a022013-03-04 10:48:41 -0800283 compiler_driver(NULL),
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 class_linker(NULL),
285 dex_file(NULL),
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 class_loader(NULL),
Ian Rogersfffdb022013-01-04 15:14:08 -0800287 class_def_idx(0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 method_idx(0),
289 code_item(NULL),
290 access_flags(0),
Ian Rogers08f753d2012-08-24 14:35:25 -0700291 invoke_type(kDirect),
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 shorty(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800293 first_lir_insn(NULL),
294 last_lir_insn(NULL),
295 literal_list(NULL),
296 method_literal_list(NULL),
297 code_literal_list(NULL),
298 disable_opt(0),
299 enable_debug(0),
300 data_offset(0),
301 total_size(0),
302 assembler_status(kSuccess),
303 assembler_retries(0),
304 verbose(false),
305 has_loop(false),
306 has_invoke(false),
307 qd_mode(false),
308 reg_pool(NULL),
309 instruction_set(kNone),
310 num_ssa_regs(0),
311 ssa_base_vregs(NULL),
312 ssa_subscripts(NULL),
313 ssa_strings(NULL),
314 vreg_to_ssa_map(NULL),
315 ssa_last_defs(NULL),
316 is_constant_v(NULL),
buzbee4ef3e452012-12-14 13:35:28 -0800317 must_flush_constant_v(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800318 constant_values(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800319 reg_location(NULL),
320 promotion_map(NULL),
321 method_sreg(0),
322 num_reachable_blocks(0),
323 num_dalvik_registers(0),
324 entry_block(NULL),
325 exit_block(NULL),
326 cur_block(NULL),
327 i_dom_list(NULL),
328 try_block_addr(NULL),
329 def_block_matrix(NULL),
330 temp_block_v(NULL),
331 temp_dalvik_register_v(NULL),
332 temp_ssa_register_v(NULL),
333 temp_ssa_block_id_v(NULL),
334 block_label_list(NULL),
335 num_ins(0),
336 num_outs(0),
337 num_regs(0),
338 num_core_spills(0),
339 num_fp_spills(0),
340 num_compiler_temps(0),
341 frame_size(0),
342 core_spill_mask(0U),
343 fp_spill_mask(0U),
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 attrs(0U),
buzbeefa57c472012-11-21 12:06:18 -0800345 current_dalvik_offset(0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 insns(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800347 insns_size(0U),
348 disable_dataflow(false),
349 def_count(0),
350 compiler_flip_match(false),
351 arena_head(NULL),
352 current_arena(NULL),
353 num_arena_blocks(0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 mstats(NULL),
buzbeed1643e42012-09-05 14:06:51 -0700355 checkstats(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800356 gen_bitcode(false),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800357 llvm_compilation_unit(NULL),
Ian Rogersfffdb022013-01-04 15:14:08 -0800358 llvm_info(NULL),
buzbee2cfc6392012-05-07 14:51:40 -0700359 context(NULL),
360 module(NULL),
361 func(NULL),
362 intrinsic_helper(NULL),
363 irb(NULL),
buzbeefa57c472012-11-21 12:06:18 -0800364 placeholder_bb(NULL),
365 entry_bb(NULL),
366 entryTarget_bb(NULL),
367 temp_name(0),
buzbee2cfc6392012-05-07 14:51:40 -0700368#ifndef NDEBUG
buzbeefa57c472012-11-21 12:06:18 -0800369 live_sreg(0),
buzbee2cfc6392012-05-07 14:51:40 -0700370#endif
buzbee02031b12012-11-23 09:41:35 -0800371 opcode_count(NULL),
372 cg(NULL) {}
Elliott Hughese52e49b2012-04-02 16:05:44 -0700373
buzbeefa57c472012-11-21 12:06:18 -0800374 int num_blocks;
375 GrowableList block_list;
Ian Rogers1212a022013-03-04 10:48:41 -0800376 CompilerDriver* compiler_driver;
buzbee02031b12012-11-23 09:41:35 -0800377 ClassLinker* class_linker; // Linker to resolve fields and methods.
378 const DexFile* dex_file; // DexFile containing the method being compiled.
379 jobject class_loader; // compiling method's class loader.
Ian Rogersfffdb022013-01-04 15:14:08 -0800380 uint32_t class_def_idx; // compiling method's defining class definition index.
buzbee02031b12012-11-23 09:41:35 -0800381 uint32_t method_idx; // compiling method's index into method_ids of DexFile.
382 const DexFile::CodeItem* code_item; // compiling method's DexFile code_item.
383 uint32_t access_flags; // compiling method's access flags.
384 InvokeType invoke_type; // compiling method's invocation type.
385 const char* shorty; // compiling method's shorty.
buzbeefa57c472012-11-21 12:06:18 -0800386 LIR* first_lir_insn;
387 LIR* last_lir_insn;
buzbee02031b12012-11-23 09:41:35 -0800388 LIR* literal_list; // Constants.
389 LIR* method_literal_list; // Method literals requiring patching.
390 LIR* code_literal_list; // Code literals requiring patching.
391 uint32_t disable_opt; // opt_control_vector flags.
392 uint32_t enable_debug; // debugControlVector flags.
393 int data_offset; // starting offset of literal pool.
394 int total_size; // header + code size.
395 AssemblerStatus assembler_status; // Success or fix and retry.
buzbeefa57c472012-11-21 12:06:18 -0800396 int assembler_retries;
397 std::vector<uint8_t> code_buffer;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700398 /*
399 * Holds mapping from native PC to dex PC for safepoints where we may deoptimize.
400 * Native PC is on the return address of the safepointed operation. Dex PC is for
401 * the instruction being executed at the safepoint.
402 */
403 std::vector<uint32_t> pc2dexMappingTable;
404 /*
405 * Holds mapping from Dex PC to native PC for catch entry points. Native PC and Dex PC
406 * immediately preceed the instruction.
407 */
408 std::vector<uint32_t> dex2pcMappingTable;
buzbeefa57c472012-11-21 12:06:18 -0800409 std::vector<uint32_t> combined_mapping_table;
410 std::vector<uint32_t> core_vmap_table;
411 std::vector<uint32_t> fp_vmap_table;
412 std::vector<uint8_t> native_gc_map;
buzbee2502e002012-12-31 16:05:53 -0800413 std::vector<BasicBlock*> extended_basic_blocks;
buzbeefa57c472012-11-21 12:06:18 -0800414 bool verbose;
buzbee02031b12012-11-23 09:41:35 -0800415 bool has_loop; // Contains a loop.
416 bool has_invoke; // Contains an invoke instruction.
417 bool qd_mode; // Compile for code size/compile time.
buzbeefa57c472012-11-21 12:06:18 -0800418 RegisterPool* reg_pool;
419 InstructionSet instruction_set;
buzbee02031b12012-11-23 09:41:35 -0800420 // Number of total regs used in the whole cu after SSA transformation .
buzbeefa57c472012-11-21 12:06:18 -0800421 int num_ssa_regs;
buzbee02031b12012-11-23 09:41:35 -0800422 // Map SSA reg i to the base virtual register/subscript.
buzbeefa57c472012-11-21 12:06:18 -0800423 GrowableList* ssa_base_vregs;
424 GrowableList* ssa_subscripts;
425 GrowableList* ssa_strings;
buzbee67bf8852011-08-17 17:51:35 -0700426
buzbee02031b12012-11-23 09:41:35 -0800427 // Map original Dalvik virtual reg i to the current SSA name.
428 int* vreg_to_ssa_map; // length == method->registers_size
429 int* ssa_last_defs; // length == method->registers_size
430 ArenaBitVector* is_constant_v; // length == num_ssa_reg
buzbee4ef3e452012-12-14 13:35:28 -0800431 ArenaBitVector* must_flush_constant_v; // length == num_ssa_reg
buzbee02031b12012-11-23 09:41:35 -0800432 int* constant_values; // length == num_ssa_reg
buzbee67bf8852011-08-17 17:51:35 -0700433
buzbee02031b12012-11-23 09:41:35 -0800434 // Use counts of ssa names.
435 GrowableList use_counts; // Weighted by nesting depth
436 GrowableList raw_use_counts; // Not weighted
buzbee239c4e72012-03-16 08:42:29 -0700437
buzbee02031b12012-11-23 09:41:35 -0800438 // Optimization support.
buzbeefa57c472012-11-21 12:06:18 -0800439 GrowableList loop_headers;
buzbee239c4e72012-03-16 08:42:29 -0700440
buzbee02031b12012-11-23 09:41:35 -0800441 // Map SSA names to location.
buzbeefa57c472012-11-21 12:06:18 -0800442 RegLocation* reg_location;
buzbee67bf8852011-08-17 17:51:35 -0700443
buzbee02031b12012-11-23 09:41:35 -0800444 // Keep track of Dalvik v_reg to physical register mappings.
buzbeefa57c472012-11-21 12:06:18 -0800445 PromotionMap* promotion_map;
buzbee67bc2362011-10-11 18:08:40 -0700446
buzbee02031b12012-11-23 09:41:35 -0800447 // SSA name for Method*.
buzbeefa57c472012-11-21 12:06:18 -0800448 int method_sreg;
buzbee02031b12012-11-23 09:41:35 -0800449 RegLocation method_loc; // Describes location of method*.
buzbeee1965672012-03-11 18:39:19 -0700450
buzbeefa57c472012-11-21 12:06:18 -0800451 int num_reachable_blocks;
buzbee02031b12012-11-23 09:41:35 -0800452 int num_dalvik_registers; // method->registers_size.
buzbeefa57c472012-11-21 12:06:18 -0800453 BasicBlock* entry_block;
454 BasicBlock* exit_block;
455 BasicBlock* cur_block;
456 GrowableList dfs_order;
457 GrowableList dfs_post_order;
458 GrowableList dom_post_order_traversal;
459 GrowableList throw_launchpads;
460 GrowableList suspend_launchpads;
461 GrowableList intrinsic_launchpads;
462 GrowableList compiler_temps;
463 int* i_dom_list;
464 ArenaBitVector* try_block_addr;
buzbee02031b12012-11-23 09:41:35 -0800465 ArenaBitVector** def_block_matrix; // num_dalvik_register x num_blocks.
buzbeefa57c472012-11-21 12:06:18 -0800466 ArenaBitVector* temp_block_v;
467 ArenaBitVector* temp_dalvik_register_v;
buzbee02031b12012-11-23 09:41:35 -0800468 ArenaBitVector* temp_ssa_register_v; // num_ssa_regs.
469 int* temp_ssa_block_id_v; // working storage for Phi labels.
buzbeefa57c472012-11-21 12:06:18 -0800470 LIR* block_label_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 /*
472 * Frame layout details.
473 * NOTE: for debug support it will be necessary to add a structure
474 * to map the Dalvik virtual registers to the promoted registers.
475 * NOTE: "num" fields are in 4-byte words, "Size" and "Offset" in bytes.
476 */
buzbeefa57c472012-11-21 12:06:18 -0800477 int num_ins;
478 int num_outs;
buzbee02031b12012-11-23 09:41:35 -0800479 int num_regs; // Unlike num_dalvik_registers, does not include ins.
buzbeefa57c472012-11-21 12:06:18 -0800480 int num_core_spills;
481 int num_fp_spills;
482 int num_compiler_temps;
483 int frame_size;
484 unsigned int core_spill_mask;
485 unsigned int fp_spill_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 unsigned int attrs;
487 /*
buzbee02031b12012-11-23 09:41:35 -0800488 * TODO: The code generation utilities don't have a built-in
Bill Buzbeea114add2012-05-03 15:00:40 -0700489 * mechanism to propagate the original Dalvik opcode address to the
490 * associated generated instructions. For the trace compiler, this wasn't
491 * necessary because the interpreter handled all throws and debugging
492 * requests. For now we'll handle this by placing the Dalvik offset
493 * in the CompilationUnit struct before codegen for each instruction.
buzbee02031b12012-11-23 09:41:35 -0800494 * The low-level LIR creation utilites will pull it from here. Rework this.
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 */
buzbeefa57c472012-11-21 12:06:18 -0800496 int current_dalvik_offset;
497 GrowableList switch_tables;
498 GrowableList fill_array_data;
buzbeeeaf09bc2012-11-15 14:51:41 -0800499 const uint16_t* insns;
buzbeefa57c472012-11-21 12:06:18 -0800500 uint32_t insns_size;
501 bool disable_dataflow; // Skip dataflow analysis if possible
buzbee02031b12012-11-23 09:41:35 -0800502 SafeMap<unsigned int, BasicBlock*> block_map; // FindBlock lookup cache.
503 SafeMap<unsigned int, unsigned int> block_id_map; // Block collapse lookup cache.
504 SafeMap<unsigned int, LIR*> boundary_map; // boundary lookup cache.
505 int def_count; // Used to estimate number of SSA names.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700506
Bill Buzbeea114add2012-05-03 15:00:40 -0700507 // If non-empty, apply optimizer/debug flags only to matching methods.
buzbeefa57c472012-11-21 12:06:18 -0800508 std::string compiler_method_match;
509 // Flips sense of compiler_method_match - apply flags if doesn't match.
510 bool compiler_flip_match;
511 ArenaMemBlock* arena_head;
512 ArenaMemBlock* current_arena;
513 int num_arena_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 Memstats* mstats;
buzbeed1643e42012-09-05 14:06:51 -0700515 Checkstats* checkstats;
buzbeefa57c472012-11-21 12:06:18 -0800516 bool gen_bitcode;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800517
518 // Fields for Portable
519 llvm::LlvmCompilationUnit* llvm_compilation_unit;
buzbee4df2bbd2012-10-11 14:46:06 -0700520 LLVMInfo* llvm_info;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800521 std::string symbol;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800522 ::llvm::LLVMContext* context;
523 ::llvm::Module* module;
524 ::llvm::Function* func;
525 art::llvm::IntrinsicHelper* intrinsic_helper;
526 art::llvm::IRBuilder* irb;
527 ::llvm::BasicBlock* placeholder_bb;
528 ::llvm::BasicBlock* entry_bb;
529 ::llvm::BasicBlock* entryTarget_bb;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800530
buzbee2cfc6392012-05-07 14:51:40 -0700531 std::string bitcode_filename;
buzbeefa57c472012-11-21 12:06:18 -0800532 GrowableList llvm_values;
533 int32_t temp_name;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800534 SafeMap< ::llvm::BasicBlock*, LIR*> block_to_label_map; // llvm bb -> LIR label.
535 SafeMap<int32_t, ::llvm::BasicBlock*> id_to_block_map; // block id -> llvm bb.
536 SafeMap< ::llvm::Value*, RegLocation> loc_map; // llvm Value to loc rec.
537 std::set< ::llvm::BasicBlock*> llvm_blocks;
buzbee3d661942012-03-14 17:37:27 -0700538#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700539 /*
540 * Sanity checking for the register temp tracking. The same ssa
541 * name should never be associated with one temp register per
542 * instruction compilation.
543 */
buzbeefa57c472012-11-21 12:06:18 -0800544 int live_sreg;
buzbee3d661942012-03-14 17:37:27 -0700545#endif
buzbee6459e7c2012-10-02 14:42:41 -0700546 std::set<uint32_t> catches;
buzbee02031b12012-11-23 09:41:35 -0800547 int* opcode_count; // Count Dalvik opcodes for tuning.
548 UniquePtr<Codegen> cg;
Elliott Hughes719ace42012-03-09 18:06:03 -0800549};
buzbee67bf8852011-08-17 17:51:35 -0700550
Elliott Hughes719ace42012-03-09 18:06:03 -0800551struct SwitchTable {
Bill Buzbeea114add2012-05-03 15:00:40 -0700552 int offset;
buzbee02031b12012-11-23 09:41:35 -0800553 const uint16_t* table; // Original dex table.
554 int vaddr; // Dalvik offset of switch opcode.
555 LIR* anchor; // Reference instruction for relative offsets.
556 LIR** targets; // Array of case targets.
Elliott Hughes719ace42012-03-09 18:06:03 -0800557};
buzbee5de34942012-03-01 14:51:57 -0800558
Elliott Hughes719ace42012-03-09 18:06:03 -0800559struct FillArrayData {
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 int offset;
buzbee02031b12012-11-23 09:41:35 -0800561 const uint16_t* table; // Original dex table.
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 int size;
buzbee02031b12012-11-23 09:41:35 -0800563 int vaddr; // Dalvik offset of FILL_ARRAY_DATA opcode.
Elliott Hughes719ace42012-03-09 18:06:03 -0800564};
buzbee5de34942012-03-01 14:51:57 -0800565
buzbee16da88c2012-03-20 10:38:17 -0700566#define MAX_PATTERN_LEN 5
567
buzbee16da88c2012-03-20 10:38:17 -0700568struct CodePattern {
Bill Buzbeea114add2012-05-03 15:00:40 -0700569 const Instruction::Code opcodes[MAX_PATTERN_LEN];
buzbeefa57c472012-11-21 12:06:18 -0800570 const SpecialCaseHandler handler_code;
buzbee16da88c2012-03-20 10:38:17 -0700571};
572
buzbeefa57c472012-11-21 12:06:18 -0800573static const CodePattern special_patterns[] = {
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 {{Instruction::RETURN_VOID}, kNullMethod},
575 {{Instruction::CONST, Instruction::RETURN}, kConstFunction},
576 {{Instruction::CONST_4, Instruction::RETURN}, kConstFunction},
577 {{Instruction::CONST_4, Instruction::RETURN_OBJECT}, kConstFunction},
578 {{Instruction::CONST_16, Instruction::RETURN}, kConstFunction},
579 {{Instruction::IGET, Instruction:: RETURN}, kIGet},
580 {{Instruction::IGET_BOOLEAN, Instruction::RETURN}, kIGetBoolean},
581 {{Instruction::IGET_OBJECT, Instruction::RETURN_OBJECT}, kIGetObject},
582 {{Instruction::IGET_BYTE, Instruction::RETURN}, kIGetByte},
583 {{Instruction::IGET_CHAR, Instruction::RETURN}, kIGetChar},
584 {{Instruction::IGET_SHORT, Instruction::RETURN}, kIGetShort},
585 {{Instruction::IGET_WIDE, Instruction::RETURN_WIDE}, kIGetWide},
586 {{Instruction::IPUT, Instruction::RETURN_VOID}, kIPut},
587 {{Instruction::IPUT_BOOLEAN, Instruction::RETURN_VOID}, kIPutBoolean},
588 {{Instruction::IPUT_OBJECT, Instruction::RETURN_VOID}, kIPutObject},
589 {{Instruction::IPUT_BYTE, Instruction::RETURN_VOID}, kIPutByte},
590 {{Instruction::IPUT_CHAR, Instruction::RETURN_VOID}, kIPutChar},
591 {{Instruction::IPUT_SHORT, Instruction::RETURN_VOID}, kIPutShort},
592 {{Instruction::IPUT_WIDE, Instruction::RETURN_VOID}, kIPutWide},
593 {{Instruction::RETURN}, kIdentity},
594 {{Instruction::RETURN_OBJECT}, kIdentity},
595 {{Instruction::RETURN_WIDE}, kIdentity},
buzbee16da88c2012-03-20 10:38:17 -0700596};
buzbee5de34942012-03-01 14:51:57 -0800597
buzbee4ef3e452012-12-14 13:35:28 -0800598static inline bool IsConst(const CompilationUnit* cu, int32_t s_reg)
599{
600 return (IsBitSet(cu->is_constant_v, s_reg));
601}
602
603static inline bool IsConst(const CompilationUnit* cu, RegLocation loc)
604{
605 return (IsConst(cu, loc.orig_sreg));
606}
607
608static inline int32_t ConstantValue(const CompilationUnit* cu, RegLocation loc)
609{
610 DCHECK(IsConst(cu, loc));
611 return cu->constant_values[loc.orig_sreg];
612}
613
buzbeef662a7c2013-02-12 16:19:43 -0800614static inline int32_t ConstantValue(const CompilationUnit* cu, int32_t s_reg)
615{
616 DCHECK(IsConst(cu, s_reg));
617 return cu->constant_values[s_reg];
618}
619
buzbee4ef3e452012-12-14 13:35:28 -0800620static inline int64_t ConstantValueWide(const CompilationUnit* cu, RegLocation loc)
621{
622 DCHECK(IsConst(cu, loc));
623 return (static_cast<int64_t>(cu->constant_values[loc.orig_sreg + 1]) << 32) |
624 Low32Bits(static_cast<int64_t>(cu->constant_values[loc.orig_sreg]));
625}
626
buzbee6a791b22013-02-07 05:35:08 -0800627static inline bool IsConstantNullRef(const CompilationUnit* cu, RegLocation loc)
628{
629 return loc.ref && loc.is_const && (ConstantValue(cu, loc) == 0);
630}
631
buzbee4ef3e452012-12-14 13:35:28 -0800632static inline bool MustFlushConstant(const CompilationUnit* cu, RegLocation loc)
633{
634 DCHECK(IsConst(cu, loc));
635 return IsBitSet(cu->must_flush_constant_v, loc.orig_sreg);
636}
637
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800638} // namespace art
639
buzbee395116c2013-02-27 14:30:25 -0800640#endif // ART_SRC_COMPILER_DEX_COMPILER_IR_H_