mir_to_lir.h revision 06606b9c4a1c00154ed15f719ad8ea994e54ee8e
1/* 2 * Copyright (C) 2012 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_COMPILER_DEX_QUICK_MIR_TO_LIR_H_ 18#define ART_COMPILER_DEX_QUICK_MIR_TO_LIR_H_ 19 20#include "invoke_type.h" 21#include "compiled_method.h" 22#include "dex/compiler_enums.h" 23#include "dex/compiler_ir.h" 24#include "dex/backend.h" 25#include "dex/growable_array.h" 26#include "dex/arena_allocator.h" 27#include "driver/compiler_driver.h" 28#include "leb128_encoder.h" 29#include "safe_map.h" 30 31namespace art { 32 33/* 34 * TODO: refactoring pass to move these (and other) typdefs towards usage style of runtime to 35 * add type safety (see runtime/offsets.h). 36 */ 37typedef uint32_t DexOffset; // Dex offset in code units. 38typedef uint16_t NarrowDexOffset; // For use in structs, Dex offsets range from 0 .. 0xffff. 39typedef uint32_t CodeOffset; // Native code offset in bytes. 40 41// Set to 1 to measure cost of suspend check. 42#define NO_SUSPEND 0 43 44#define IS_BINARY_OP (1ULL << kIsBinaryOp) 45#define IS_BRANCH (1ULL << kIsBranch) 46#define IS_IT (1ULL << kIsIT) 47#define IS_LOAD (1ULL << kMemLoad) 48#define IS_QUAD_OP (1ULL << kIsQuadOp) 49#define IS_QUIN_OP (1ULL << kIsQuinOp) 50#define IS_SEXTUPLE_OP (1ULL << kIsSextupleOp) 51#define IS_STORE (1ULL << kMemStore) 52#define IS_TERTIARY_OP (1ULL << kIsTertiaryOp) 53#define IS_UNARY_OP (1ULL << kIsUnaryOp) 54#define NEEDS_FIXUP (1ULL << kPCRelFixup) 55#define NO_OPERAND (1ULL << kNoOperand) 56#define REG_DEF0 (1ULL << kRegDef0) 57#define REG_DEF1 (1ULL << kRegDef1) 58#define REG_DEFA (1ULL << kRegDefA) 59#define REG_DEFD (1ULL << kRegDefD) 60#define REG_DEF_FPCS_LIST0 (1ULL << kRegDefFPCSList0) 61#define REG_DEF_FPCS_LIST2 (1ULL << kRegDefFPCSList2) 62#define REG_DEF_LIST0 (1ULL << kRegDefList0) 63#define REG_DEF_LIST1 (1ULL << kRegDefList1) 64#define REG_DEF_LR (1ULL << kRegDefLR) 65#define REG_DEF_SP (1ULL << kRegDefSP) 66#define REG_USE0 (1ULL << kRegUse0) 67#define REG_USE1 (1ULL << kRegUse1) 68#define REG_USE2 (1ULL << kRegUse2) 69#define REG_USE3 (1ULL << kRegUse3) 70#define REG_USE4 (1ULL << kRegUse4) 71#define REG_USEA (1ULL << kRegUseA) 72#define REG_USEC (1ULL << kRegUseC) 73#define REG_USED (1ULL << kRegUseD) 74#define REG_USE_FPCS_LIST0 (1ULL << kRegUseFPCSList0) 75#define REG_USE_FPCS_LIST2 (1ULL << kRegUseFPCSList2) 76#define REG_USE_LIST0 (1ULL << kRegUseList0) 77#define REG_USE_LIST1 (1ULL << kRegUseList1) 78#define REG_USE_LR (1ULL << kRegUseLR) 79#define REG_USE_PC (1ULL << kRegUsePC) 80#define REG_USE_SP (1ULL << kRegUseSP) 81#define SETS_CCODES (1ULL << kSetsCCodes) 82#define USES_CCODES (1ULL << kUsesCCodes) 83 84// Common combo register usage patterns. 85#define REG_DEF01 (REG_DEF0 | REG_DEF1) 86#define REG_DEF01_USE2 (REG_DEF0 | REG_DEF1 | REG_USE2) 87#define REG_DEF0_USE01 (REG_DEF0 | REG_USE01) 88#define REG_DEF0_USE0 (REG_DEF0 | REG_USE0) 89#define REG_DEF0_USE12 (REG_DEF0 | REG_USE12) 90#define REG_DEF0_USE123 (REG_DEF0 | REG_USE123) 91#define REG_DEF0_USE1 (REG_DEF0 | REG_USE1) 92#define REG_DEF0_USE2 (REG_DEF0 | REG_USE2) 93#define REG_DEFAD_USEAD (REG_DEFAD_USEA | REG_USED) 94#define REG_DEFAD_USEA (REG_DEFA_USEA | REG_DEFD) 95#define REG_DEFA_USEA (REG_DEFA | REG_USEA) 96#define REG_USE012 (REG_USE01 | REG_USE2) 97#define REG_USE014 (REG_USE01 | REG_USE4) 98#define REG_USE01 (REG_USE0 | REG_USE1) 99#define REG_USE02 (REG_USE0 | REG_USE2) 100#define REG_USE12 (REG_USE1 | REG_USE2) 101#define REG_USE23 (REG_USE2 | REG_USE3) 102#define REG_USE123 (REG_USE1 | REG_USE2 | REG_USE3) 103 104struct BasicBlock; 105struct CallInfo; 106struct CompilationUnit; 107struct MIR; 108struct LIR; 109struct RegLocation; 110struct RegisterInfo; 111class DexFileMethodInliner; 112class MIRGraph; 113class Mir2Lir; 114 115typedef int (*NextCallInsn)(CompilationUnit*, CallInfo*, int, 116 const MethodReference& target_method, 117 uint32_t method_idx, uintptr_t direct_code, 118 uintptr_t direct_method, InvokeType type); 119 120typedef std::vector<uint8_t> CodeBuffer; 121 122struct UseDefMasks { 123 uint64_t use_mask; // Resource mask for use. 124 uint64_t def_mask; // Resource mask for def. 125}; 126 127struct AssemblyInfo { 128 LIR* pcrel_next; // Chain of LIR nodes needing pc relative fixups. 129 uint8_t bytes[16]; // Encoded instruction bytes. 130}; 131 132struct LIR { 133 CodeOffset offset; // Offset of this instruction. 134 NarrowDexOffset dalvik_offset; // Offset of Dalvik opcode in code units (16-bit words). 135 int16_t opcode; 136 LIR* next; 137 LIR* prev; 138 LIR* target; 139 struct { 140 unsigned int alias_info:17; // For Dalvik register disambiguation. 141 bool is_nop:1; // LIR is optimized away. 142 unsigned int size:4; // Note: size of encoded instruction is in bytes. 143 bool use_def_invalid:1; // If true, masks should not be used. 144 unsigned int generation:1; // Used to track visitation state during fixup pass. 145 unsigned int fixup:8; // Fixup kind. 146 } flags; 147 union { 148 UseDefMasks m; // Use & Def masks used during optimization. 149 AssemblyInfo a; // Instruction encoding used during assembly phase. 150 } u; 151 int32_t operands[5]; // [0..4] = [dest, src1, src2, extra, extra2]. 152}; 153 154// Target-specific initialization. 155Mir2Lir* ArmCodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph, 156 ArenaAllocator* const arena); 157Mir2Lir* MipsCodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph, 158 ArenaAllocator* const arena); 159Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph, 160 ArenaAllocator* const arena); 161 162// Utility macros to traverse the LIR list. 163#define NEXT_LIR(lir) (lir->next) 164#define PREV_LIR(lir) (lir->prev) 165 166// Defines for alias_info (tracks Dalvik register references). 167#define DECODE_ALIAS_INFO_REG(X) (X & 0xffff) 168#define DECODE_ALIAS_INFO_WIDE_FLAG (0x10000) 169#define DECODE_ALIAS_INFO_WIDE(X) ((X & DECODE_ALIAS_INFO_WIDE_FLAG) ? 1 : 0) 170#define ENCODE_ALIAS_INFO(REG, ISWIDE) (REG | (ISWIDE ? DECODE_ALIAS_INFO_WIDE_FLAG : 0)) 171 172// Common resource macros. 173#define ENCODE_CCODE (1ULL << kCCode) 174#define ENCODE_FP_STATUS (1ULL << kFPStatus) 175 176// Abstract memory locations. 177#define ENCODE_DALVIK_REG (1ULL << kDalvikReg) 178#define ENCODE_LITERAL (1ULL << kLiteral) 179#define ENCODE_HEAP_REF (1ULL << kHeapRef) 180#define ENCODE_MUST_NOT_ALIAS (1ULL << kMustNotAlias) 181 182#define ENCODE_ALL (~0ULL) 183#define ENCODE_MEM (ENCODE_DALVIK_REG | ENCODE_LITERAL | \ 184 ENCODE_HEAP_REF | ENCODE_MUST_NOT_ALIAS) 185 186// Mask to denote sreg as the start of a double. Must not interfere with low 16 bits. 187#define STARTING_DOUBLE_SREG 0x10000 188 189// TODO: replace these macros 190#define SLOW_FIELD_PATH (cu_->enable_debug & (1 << kDebugSlowFieldPath)) 191#define SLOW_INVOKE_PATH (cu_->enable_debug & (1 << kDebugSlowInvokePath)) 192#define SLOW_STRING_PATH (cu_->enable_debug & (1 << kDebugSlowStringPath)) 193#define SLOW_TYPE_PATH (cu_->enable_debug & (1 << kDebugSlowTypePath)) 194#define EXERCISE_SLOWEST_STRING_PATH (cu_->enable_debug & (1 << kDebugSlowestStringPath)) 195 196class Mir2Lir : public Backend { 197 public: 198 /* 199 * Auxiliary information describing the location of data embedded in the Dalvik 200 * byte code stream. 201 */ 202 struct EmbeddedData { 203 CodeOffset offset; // Code offset of data block. 204 const uint16_t* table; // Original dex data. 205 DexOffset vaddr; // Dalvik offset of parent opcode. 206 }; 207 208 struct FillArrayData : EmbeddedData { 209 int32_t size; 210 }; 211 212 struct SwitchTable : EmbeddedData { 213 LIR* anchor; // Reference instruction for relative offsets. 214 LIR** targets; // Array of case targets. 215 }; 216 217 /* Static register use counts */ 218 struct RefCounts { 219 int count; 220 int s_reg; 221 }; 222 223 /* 224 * Data structure tracking the mapping between a Dalvik register (pair) and a 225 * native register (pair). The idea is to reuse the previously loaded value 226 * if possible, otherwise to keep the value in a native register as long as 227 * possible. 228 */ 229 struct RegisterInfo { 230 int reg; // Reg number 231 bool in_use; // Has it been allocated? 232 bool is_temp; // Can allocate as temp? 233 bool pair; // Part of a register pair? 234 int partner; // If pair, other reg of pair. 235 bool live; // Is there an associated SSA name? 236 bool dirty; // If live, is it dirty? 237 int s_reg; // Name of live value. 238 LIR *def_start; // Starting inst in last def sequence. 239 LIR *def_end; // Ending inst in last def sequence. 240 }; 241 242 struct RegisterPool { 243 int num_core_regs; 244 RegisterInfo *core_regs; 245 int next_core_reg; 246 int num_fp_regs; 247 RegisterInfo *FPRegs; 248 int next_fp_reg; 249 }; 250 251 struct PromotionMap { 252 RegLocationType core_location:3; 253 uint8_t core_reg; 254 RegLocationType fp_location:3; 255 uint8_t FpReg; 256 bool first_in_pair; 257 }; 258 259 virtual ~Mir2Lir() {} 260 261 int32_t s4FromSwitchData(const void* switch_data) { 262 return *reinterpret_cast<const int32_t*>(switch_data); 263 } 264 265 RegisterClass oat_reg_class_by_size(OpSize size) { 266 return (size == kUnsignedHalf || size == kSignedHalf || size == kUnsignedByte || 267 size == kSignedByte) ? kCoreReg : kAnyReg; 268 } 269 270 size_t CodeBufferSizeInBytes() { 271 return code_buffer_.size() / sizeof(code_buffer_[0]); 272 } 273 274 bool IsPseudoLirOp(int opcode) { 275 return (opcode < 0); 276 } 277 278 /* 279 * LIR operands are 32-bit integers. Sometimes, (especially for managing 280 * instructions which require PC-relative fixups), we need the operands to carry 281 * pointers. To do this, we assign these pointers an index in pointer_storage_, and 282 * hold that index in the operand array. 283 * TUNING: If use of these utilities becomes more common on 32-bit builds, it 284 * may be worth conditionally-compiling a set of identity functions here. 285 */ 286 uint32_t WrapPointer(void* pointer) { 287 uint32_t res = pointer_storage_.Size(); 288 pointer_storage_.Insert(pointer); 289 return res; 290 } 291 292 void* UnwrapPointer(size_t index) { 293 return pointer_storage_.Get(index); 294 } 295 296 // strdup(), but allocates from the arena. 297 char* ArenaStrdup(const char* str) { 298 size_t len = strlen(str) + 1; 299 char* res = reinterpret_cast<char*>(arena_->Alloc(len, ArenaAllocator::kAllocMisc)); 300 if (res != NULL) { 301 strncpy(res, str, len); 302 } 303 return res; 304 } 305 306 // Shared by all targets - implemented in codegen_util.cc 307 void AppendLIR(LIR* lir); 308 void InsertLIRBefore(LIR* current_lir, LIR* new_lir); 309 void InsertLIRAfter(LIR* current_lir, LIR* new_lir); 310 311 int ComputeFrameSize(); 312 virtual void Materialize(); 313 virtual CompiledMethod* GetCompiledMethod(); 314 void MarkSafepointPC(LIR* inst); 315 bool FastInstance(uint32_t field_idx, bool is_put, int* field_offset, bool* is_volatile); 316 void SetupResourceMasks(LIR* lir); 317 void SetMemRefType(LIR* lir, bool is_load, int mem_type); 318 void AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load, bool is64bit); 319 void SetupRegMask(uint64_t* mask, int reg); 320 void DumpLIRInsn(LIR* arg, unsigned char* base_addr); 321 void DumpPromotionMap(); 322 void CodegenDump(); 323 LIR* RawLIR(DexOffset dalvik_offset, int opcode, int op0 = 0, int op1 = 0, 324 int op2 = 0, int op3 = 0, int op4 = 0, LIR* target = NULL); 325 LIR* NewLIR0(int opcode); 326 LIR* NewLIR1(int opcode, int dest); 327 LIR* NewLIR2(int opcode, int dest, int src1); 328 LIR* NewLIR3(int opcode, int dest, int src1, int src2); 329 LIR* NewLIR4(int opcode, int dest, int src1, int src2, int info); 330 LIR* NewLIR5(int opcode, int dest, int src1, int src2, int info1, int info2); 331 LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta); 332 LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi); 333 LIR* AddWordData(LIR* *constant_list_p, int value); 334 LIR* AddWideData(LIR* *constant_list_p, int val_lo, int val_hi); 335 void ProcessSwitchTables(); 336 void DumpSparseSwitchTable(const uint16_t* table); 337 void DumpPackedSwitchTable(const uint16_t* table); 338 void MarkBoundary(DexOffset offset, const char* inst_str); 339 void NopLIR(LIR* lir); 340 void UnlinkLIR(LIR* lir); 341 bool EvaluateBranch(Instruction::Code opcode, int src1, int src2); 342 bool IsInexpensiveConstant(RegLocation rl_src); 343 ConditionCode FlipComparisonOrder(ConditionCode before); 344 void InstallLiteralPools(); 345 void InstallSwitchTables(); 346 void InstallFillArrayData(); 347 bool VerifyCatchEntries(); 348 void CreateMappingTables(); 349 void CreateNativeGcMap(); 350 int AssignLiteralOffset(CodeOffset offset); 351 int AssignSwitchTablesOffset(CodeOffset offset); 352 int AssignFillArrayDataOffset(CodeOffset offset); 353 LIR* InsertCaseLabel(DexOffset vaddr, int keyVal); 354 void MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec); 355 void MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec); 356 357 // Shared by all targets - implemented in local_optimizations.cc 358 void ConvertMemOpIntoMove(LIR* orig_lir, int dest, int src); 359 void ApplyLoadStoreElimination(LIR* head_lir, LIR* tail_lir); 360 void ApplyLoadHoisting(LIR* head_lir, LIR* tail_lir); 361 void ApplyLocalOptimizations(LIR* head_lir, LIR* tail_lir); 362 363 // Shared by all targets - implemented in ralloc_util.cc 364 int GetSRegHi(int lowSreg); 365 bool oat_live_out(int s_reg); 366 int oatSSASrc(MIR* mir, int num); 367 void SimpleRegAlloc(); 368 void ResetRegPool(); 369 void CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num); 370 void DumpRegPool(RegisterInfo* p, int num_regs); 371 void DumpCoreRegPool(); 372 void DumpFpRegPool(); 373 /* Mark a temp register as dead. Does not affect allocation state. */ 374 void Clobber(int reg) { 375 ClobberBody(GetRegInfo(reg)); 376 } 377 void ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg); 378 void ClobberSReg(int s_reg); 379 int SRegToPMap(int s_reg); 380 void RecordCorePromotion(int reg, int s_reg); 381 int AllocPreservedCoreReg(int s_reg); 382 void RecordFpPromotion(int reg, int s_reg); 383 int AllocPreservedSingle(int s_reg); 384 int AllocPreservedDouble(int s_reg); 385 int AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp, bool required); 386 int AllocTempDouble(); 387 int AllocFreeTemp(); 388 int AllocTemp(); 389 int AllocTempFloat(); 390 RegisterInfo* AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg); 391 RegisterInfo* AllocLive(int s_reg, int reg_class); 392 void FreeTemp(int reg); 393 RegisterInfo* IsLive(int reg); 394 RegisterInfo* IsTemp(int reg); 395 RegisterInfo* IsPromoted(int reg); 396 bool IsDirty(int reg); 397 void LockTemp(int reg); 398 void ResetDef(int reg); 399 void NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2); 400 void MarkDef(RegLocation rl, LIR *start, LIR *finish); 401 void MarkDefWide(RegLocation rl, LIR *start, LIR *finish); 402 RegLocation WideToNarrow(RegLocation rl); 403 void ResetDefLoc(RegLocation rl); 404 void ResetDefLocWide(RegLocation rl); 405 void ResetDefTracking(); 406 void ClobberAllRegs(); 407 void FlushAllRegsBody(RegisterInfo* info, int num_regs); 408 void FlushAllRegs(); 409 bool RegClassMatches(int reg_class, int reg); 410 void MarkLive(int reg, int s_reg); 411 void MarkTemp(int reg); 412 void UnmarkTemp(int reg); 413 void MarkPair(int low_reg, int high_reg); 414 void MarkClean(RegLocation loc); 415 void MarkDirty(RegLocation loc); 416 void MarkInUse(int reg); 417 void CopyRegInfo(int new_reg, int old_reg); 418 bool CheckCorePoolSanity(); 419 RegLocation UpdateLoc(RegLocation loc); 420 RegLocation UpdateLocWide(RegLocation loc); 421 RegLocation UpdateRawLoc(RegLocation loc); 422 RegLocation EvalLocWide(RegLocation loc, int reg_class, bool update); 423 RegLocation EvalLoc(RegLocation loc, int reg_class, bool update); 424 void CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs); 425 void DumpCounts(const RefCounts* arr, int size, const char* msg); 426 void DoPromotion(); 427 int VRegOffset(int v_reg); 428 int SRegOffset(int s_reg); 429 RegLocation GetReturnWide(bool is_double); 430 RegLocation GetReturn(bool is_float); 431 RegisterInfo* GetRegInfo(int reg); 432 433 // Shared by all targets - implemented in gen_common.cc. 434 bool HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div, 435 RegLocation rl_src, RegLocation rl_dest, int lit); 436 bool HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit); 437 void HandleSuspendLaunchPads(); 438 void HandleIntrinsicLaunchPads(); 439 void HandleThrowLaunchPads(); 440 void GenBarrier(); 441 LIR* GenCheck(ConditionCode c_code, ThrowKind kind); 442 LIR* GenImmedCheck(ConditionCode c_code, int reg, int imm_val, 443 ThrowKind kind); 444 LIR* GenNullCheck(int s_reg, int m_reg, int opt_flags); 445 LIR* GenRegRegCheck(ConditionCode c_code, int reg1, int reg2, 446 ThrowKind kind); 447 void GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1, 448 RegLocation rl_src2, LIR* taken, LIR* fall_through); 449 void GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, 450 LIR* taken, LIR* fall_through); 451 void GenIntToLong(RegLocation rl_dest, RegLocation rl_src); 452 void GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest, 453 RegLocation rl_src); 454 void GenNewArray(uint32_t type_idx, RegLocation rl_dest, 455 RegLocation rl_src); 456 void GenFilledNewArray(CallInfo* info); 457 void GenSput(uint32_t field_idx, RegLocation rl_src, 458 bool is_long_or_double, bool is_object); 459 void GenSget(uint32_t field_idx, RegLocation rl_dest, 460 bool is_long_or_double, bool is_object); 461 void GenIGet(uint32_t field_idx, int opt_flags, OpSize size, 462 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double, bool is_object); 463 void GenIPut(uint32_t field_idx, int opt_flags, OpSize size, 464 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double, bool is_object); 465 void GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index, 466 RegLocation rl_src); 467 468 void GenConstClass(uint32_t type_idx, RegLocation rl_dest); 469 void GenConstString(uint32_t string_idx, RegLocation rl_dest); 470 void GenNewInstance(uint32_t type_idx, RegLocation rl_dest); 471 void GenThrow(RegLocation rl_src); 472 void GenInstanceof(uint32_t type_idx, RegLocation rl_dest, 473 RegLocation rl_src); 474 void GenCheckCast(uint32_t insn_idx, uint32_t type_idx, 475 RegLocation rl_src); 476 void GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest, 477 RegLocation rl_src1, RegLocation rl_src2); 478 void GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest, 479 RegLocation rl_src1, RegLocation rl_shift); 480 void GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest, 481 RegLocation rl_src1, RegLocation rl_src2); 482 void GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, 483 RegLocation rl_src, int lit); 484 void GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, 485 RegLocation rl_src1, RegLocation rl_src2); 486 void GenConversionCall(ThreadOffset func_offset, RegLocation rl_dest, 487 RegLocation rl_src); 488 void GenSuspendTest(int opt_flags); 489 void GenSuspendTestAndBranch(int opt_flags, LIR* target); 490 491 // Shared by all targets - implemented in gen_invoke.cc. 492 int CallHelperSetup(ThreadOffset helper_offset); 493 LIR* CallHelper(int r_tgt, ThreadOffset helper_offset, bool safepoint_pc); 494 void CallRuntimeHelperImm(ThreadOffset helper_offset, int arg0, bool safepoint_pc); 495 void CallRuntimeHelperReg(ThreadOffset helper_offset, int arg0, bool safepoint_pc); 496 void CallRuntimeHelperRegLocation(ThreadOffset helper_offset, RegLocation arg0, 497 bool safepoint_pc); 498 void CallRuntimeHelperImmImm(ThreadOffset helper_offset, int arg0, int arg1, 499 bool safepoint_pc); 500 void CallRuntimeHelperImmRegLocation(ThreadOffset helper_offset, int arg0, 501 RegLocation arg1, bool safepoint_pc); 502 void CallRuntimeHelperRegLocationImm(ThreadOffset helper_offset, RegLocation arg0, 503 int arg1, bool safepoint_pc); 504 void CallRuntimeHelperImmReg(ThreadOffset helper_offset, int arg0, int arg1, 505 bool safepoint_pc); 506 void CallRuntimeHelperRegImm(ThreadOffset helper_offset, int arg0, int arg1, 507 bool safepoint_pc); 508 void CallRuntimeHelperImmMethod(ThreadOffset helper_offset, int arg0, 509 bool safepoint_pc); 510 void CallRuntimeHelperRegLocationRegLocation(ThreadOffset helper_offset, 511 RegLocation arg0, RegLocation arg1, 512 bool safepoint_pc); 513 void CallRuntimeHelperRegReg(ThreadOffset helper_offset, int arg0, int arg1, 514 bool safepoint_pc); 515 void CallRuntimeHelperRegRegImm(ThreadOffset helper_offset, int arg0, int arg1, 516 int arg2, bool safepoint_pc); 517 void CallRuntimeHelperImmMethodRegLocation(ThreadOffset helper_offset, int arg0, 518 RegLocation arg2, bool safepoint_pc); 519 void CallRuntimeHelperImmMethodImm(ThreadOffset helper_offset, int arg0, int arg2, 520 bool safepoint_pc); 521 void CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset helper_offset, 522 int arg0, RegLocation arg1, RegLocation arg2, 523 bool safepoint_pc); 524 void CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset helper_offset, 525 RegLocation arg0, RegLocation arg1, 526 RegLocation arg2, 527 bool safepoint_pc); 528 void GenInvoke(CallInfo* info); 529 void FlushIns(RegLocation* ArgLocs, RegLocation rl_method); 530 int GenDalvikArgsNoRange(CallInfo* info, int call_state, LIR** pcrLabel, 531 NextCallInsn next_call_insn, 532 const MethodReference& target_method, 533 uint32_t vtable_idx, 534 uintptr_t direct_code, uintptr_t direct_method, InvokeType type, 535 bool skip_this); 536 int GenDalvikArgsRange(CallInfo* info, int call_state, LIR** pcrLabel, 537 NextCallInsn next_call_insn, 538 const MethodReference& target_method, 539 uint32_t vtable_idx, 540 uintptr_t direct_code, uintptr_t direct_method, InvokeType type, 541 bool skip_this); 542 RegLocation InlineTarget(CallInfo* info); 543 RegLocation InlineTargetWide(CallInfo* info); 544 545 bool GenInlinedCharAt(CallInfo* info); 546 bool GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty); 547 bool GenInlinedReverseBytes(CallInfo* info, OpSize size); 548 bool GenInlinedAbsInt(CallInfo* info); 549 bool GenInlinedAbsLong(CallInfo* info); 550 bool GenInlinedFloatCvt(CallInfo* info); 551 bool GenInlinedDoubleCvt(CallInfo* info); 552 bool GenInlinedIndexOf(CallInfo* info, bool zero_based); 553 bool GenInlinedStringCompareTo(CallInfo* info); 554 bool GenInlinedCurrentThread(CallInfo* info); 555 bool GenInlinedUnsafeGet(CallInfo* info, bool is_long, bool is_volatile); 556 bool GenInlinedUnsafePut(CallInfo* info, bool is_long, bool is_object, 557 bool is_volatile, bool is_ordered); 558 int LoadArgRegs(CallInfo* info, int call_state, 559 NextCallInsn next_call_insn, 560 const MethodReference& target_method, 561 uint32_t vtable_idx, 562 uintptr_t direct_code, uintptr_t direct_method, InvokeType type, 563 bool skip_this); 564 565 // Shared by all targets - implemented in gen_loadstore.cc. 566 RegLocation LoadCurrMethod(); 567 void LoadCurrMethodDirect(int r_tgt); 568 LIR* LoadConstant(int r_dest, int value); 569 LIR* LoadWordDisp(int rBase, int displacement, int r_dest); 570 RegLocation LoadValue(RegLocation rl_src, RegisterClass op_kind); 571 RegLocation LoadValueWide(RegLocation rl_src, RegisterClass op_kind); 572 void LoadValueDirect(RegLocation rl_src, int r_dest); 573 void LoadValueDirectFixed(RegLocation rl_src, int r_dest); 574 void LoadValueDirectWide(RegLocation rl_src, int reg_lo, int reg_hi); 575 void LoadValueDirectWideFixed(RegLocation rl_src, int reg_lo, int reg_hi); 576 LIR* StoreWordDisp(int rBase, int displacement, int r_src); 577 void StoreValue(RegLocation rl_dest, RegLocation rl_src); 578 void StoreValueWide(RegLocation rl_dest, RegLocation rl_src); 579 580 // Shared by all targets - implemented in mir_to_lir.cc. 581 void CompileDalvikInstruction(MIR* mir, BasicBlock* bb, LIR* label_list); 582 void HandleExtendedMethodMIR(BasicBlock* bb, MIR* mir); 583 bool MethodBlockCodeGen(BasicBlock* bb); 584 void SpecialMIR2LIR(SpecialCaseHandler special_case); 585 void MethodMIR2LIR(); 586 587 588 589 // Required for target - codegen helpers. 590 virtual bool SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div, 591 RegLocation rl_src, RegLocation rl_dest, int lit) = 0; 592 virtual int LoadHelper(ThreadOffset offset) = 0; 593 virtual LIR* LoadBaseDisp(int rBase, int displacement, int r_dest, OpSize size, int s_reg) = 0; 594 virtual LIR* LoadBaseDispWide(int rBase, int displacement, int r_dest_lo, int r_dest_hi, 595 int s_reg) = 0; 596 virtual LIR* LoadBaseIndexed(int rBase, int r_index, int r_dest, int scale, OpSize size) = 0; 597 virtual LIR* LoadBaseIndexedDisp(int rBase, int r_index, int scale, int displacement, 598 int r_dest, int r_dest_hi, OpSize size, int s_reg) = 0; 599 virtual LIR* LoadConstantNoClobber(int r_dest, int value) = 0; 600 virtual LIR* LoadConstantWide(int r_dest_lo, int r_dest_hi, int64_t value) = 0; 601 virtual LIR* StoreBaseDisp(int rBase, int displacement, int r_src, OpSize size) = 0; 602 virtual LIR* StoreBaseDispWide(int rBase, int displacement, int r_src_lo, int r_src_hi) = 0; 603 virtual LIR* StoreBaseIndexed(int rBase, int r_index, int r_src, int scale, OpSize size) = 0; 604 virtual LIR* StoreBaseIndexedDisp(int rBase, int r_index, int scale, int displacement, 605 int r_src, int r_src_hi, OpSize size, int s_reg) = 0; 606 virtual void MarkGCCard(int val_reg, int tgt_addr_reg) = 0; 607 608 // Required for target - register utilities. 609 virtual bool IsFpReg(int reg) = 0; 610 virtual bool SameRegType(int reg1, int reg2) = 0; 611 virtual int AllocTypedTemp(bool fp_hint, int reg_class) = 0; 612 virtual int AllocTypedTempPair(bool fp_hint, int reg_class) = 0; 613 virtual int S2d(int low_reg, int high_reg) = 0; 614 virtual int TargetReg(SpecialTargetRegister reg) = 0; 615 virtual RegLocation GetReturnAlt() = 0; 616 virtual RegLocation GetReturnWideAlt() = 0; 617 virtual RegLocation LocCReturn() = 0; 618 virtual RegLocation LocCReturnDouble() = 0; 619 virtual RegLocation LocCReturnFloat() = 0; 620 virtual RegLocation LocCReturnWide() = 0; 621 virtual uint32_t FpRegMask() = 0; 622 virtual uint64_t GetRegMaskCommon(int reg) = 0; 623 virtual void AdjustSpillMask() = 0; 624 virtual void ClobberCalleeSave() = 0; 625 virtual void FlushReg(int reg) = 0; 626 virtual void FlushRegWide(int reg1, int reg2) = 0; 627 virtual void FreeCallTemps() = 0; 628 virtual void FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) = 0; 629 virtual void LockCallTemps() = 0; 630 virtual void MarkPreservedSingle(int v_reg, int reg) = 0; 631 virtual void CompilerInitializeRegAlloc() = 0; 632 633 // Required for target - miscellaneous. 634 virtual void AssembleLIR() = 0; 635 virtual void DumpResourceMask(LIR* lir, uint64_t mask, const char* prefix) = 0; 636 virtual void SetupTargetResourceMasks(LIR* lir, uint64_t flags) = 0; 637 virtual const char* GetTargetInstFmt(int opcode) = 0; 638 virtual const char* GetTargetInstName(int opcode) = 0; 639 virtual std::string BuildInsnString(const char* fmt, LIR* lir, unsigned char* base_addr) = 0; 640 virtual uint64_t GetPCUseDefEncoding() = 0; 641 virtual uint64_t GetTargetInstFlags(int opcode) = 0; 642 virtual int GetInsnSize(LIR* lir) = 0; 643 virtual bool IsUnconditionalBranch(LIR* lir) = 0; 644 645 // Required for target - Dalvik-level generators. 646 virtual void GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest, 647 RegLocation rl_src1, RegLocation rl_src2) = 0; 648 virtual void GenMulLong(RegLocation rl_dest, RegLocation rl_src1, 649 RegLocation rl_src2) = 0; 650 virtual void GenAddLong(RegLocation rl_dest, RegLocation rl_src1, 651 RegLocation rl_src2) = 0; 652 virtual void GenAndLong(RegLocation rl_dest, RegLocation rl_src1, 653 RegLocation rl_src2) = 0; 654 virtual void GenArithOpDouble(Instruction::Code opcode, 655 RegLocation rl_dest, RegLocation rl_src1, 656 RegLocation rl_src2) = 0; 657 virtual void GenArithOpFloat(Instruction::Code opcode, RegLocation rl_dest, 658 RegLocation rl_src1, RegLocation rl_src2) = 0; 659 virtual void GenCmpFP(Instruction::Code opcode, RegLocation rl_dest, 660 RegLocation rl_src1, RegLocation rl_src2) = 0; 661 virtual void GenConversion(Instruction::Code opcode, RegLocation rl_dest, 662 RegLocation rl_src) = 0; 663 virtual bool GenInlinedCas(CallInfo* info, bool is_long, bool is_object) = 0; 664 virtual bool GenInlinedMinMaxInt(CallInfo* info, bool is_min) = 0; 665 virtual bool GenInlinedSqrt(CallInfo* info) = 0; 666 virtual bool GenInlinedPeek(CallInfo* info, OpSize size) = 0; 667 virtual bool GenInlinedPoke(CallInfo* info, OpSize size) = 0; 668 virtual void GenNegLong(RegLocation rl_dest, RegLocation rl_src) = 0; 669 virtual void GenOrLong(RegLocation rl_dest, RegLocation rl_src1, 670 RegLocation rl_src2) = 0; 671 virtual void GenSubLong(RegLocation rl_dest, RegLocation rl_src1, 672 RegLocation rl_src2) = 0; 673 virtual void GenXorLong(RegLocation rl_dest, RegLocation rl_src1, 674 RegLocation rl_src2) = 0; 675 virtual LIR* GenRegMemCheck(ConditionCode c_code, int reg1, int base, 676 int offset, ThrowKind kind) = 0; 677 virtual RegLocation GenDivRem(RegLocation rl_dest, int reg_lo, int reg_hi, 678 bool is_div) = 0; 679 virtual RegLocation GenDivRemLit(RegLocation rl_dest, int reg_lo, int lit, 680 bool is_div) = 0; 681 virtual void GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, 682 RegLocation rl_src2) = 0; 683 virtual void GenDivZeroCheck(int reg_lo, int reg_hi) = 0; 684 virtual void GenEntrySequence(RegLocation* ArgLocs, 685 RegLocation rl_method) = 0; 686 virtual void GenExitSequence() = 0; 687 virtual void GenFillArrayData(DexOffset table_offset, 688 RegLocation rl_src) = 0; 689 virtual void GenFusedFPCmpBranch(BasicBlock* bb, MIR* mir, bool gt_bias, 690 bool is_double) = 0; 691 virtual void GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) = 0; 692 virtual void GenSelect(BasicBlock* bb, MIR* mir) = 0; 693 virtual void GenMemBarrier(MemBarrierKind barrier_kind) = 0; 694 virtual void GenMoveException(RegLocation rl_dest) = 0; 695 virtual void GenMultiplyByTwoBitMultiplier(RegLocation rl_src, 696 RegLocation rl_result, int lit, int first_bit, 697 int second_bit) = 0; 698 virtual void GenNegDouble(RegLocation rl_dest, RegLocation rl_src) = 0; 699 virtual void GenNegFloat(RegLocation rl_dest, RegLocation rl_src) = 0; 700 virtual void GenPackedSwitch(MIR* mir, DexOffset table_offset, 701 RegLocation rl_src) = 0; 702 virtual void GenSparseSwitch(MIR* mir, DexOffset table_offset, 703 RegLocation rl_src) = 0; 704 virtual void GenSpecialCase(BasicBlock* bb, MIR* mir, 705 SpecialCaseHandler special_case) = 0; 706 virtual void GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array, 707 RegLocation rl_index, RegLocation rl_dest, int scale) = 0; 708 virtual void GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array, 709 RegLocation rl_index, RegLocation rl_src, int scale, 710 bool card_mark) = 0; 711 virtual void GenShiftImmOpLong(Instruction::Code opcode, 712 RegLocation rl_dest, RegLocation rl_src1, 713 RegLocation rl_shift) = 0; 714 715 // Required for target - single operation generators. 716 virtual LIR* OpUnconditionalBranch(LIR* target) = 0; 717 virtual LIR* OpCmpBranch(ConditionCode cond, int src1, int src2, LIR* target) = 0; 718 virtual LIR* OpCmpImmBranch(ConditionCode cond, int reg, int check_value, LIR* target) = 0; 719 virtual LIR* OpCondBranch(ConditionCode cc, LIR* target) = 0; 720 virtual LIR* OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) = 0; 721 virtual LIR* OpFpRegCopy(int r_dest, int r_src) = 0; 722 virtual LIR* OpIT(ConditionCode cond, const char* guide) = 0; 723 virtual LIR* OpMem(OpKind op, int rBase, int disp) = 0; 724 virtual LIR* OpPcRelLoad(int reg, LIR* target) = 0; 725 virtual LIR* OpReg(OpKind op, int r_dest_src) = 0; 726 virtual LIR* OpRegCopy(int r_dest, int r_src) = 0; 727 virtual LIR* OpRegCopyNoInsert(int r_dest, int r_src) = 0; 728 virtual LIR* OpRegImm(OpKind op, int r_dest_src1, int value) = 0; 729 virtual LIR* OpRegMem(OpKind op, int r_dest, int rBase, int offset) = 0; 730 virtual LIR* OpRegReg(OpKind op, int r_dest_src1, int r_src2) = 0; 731 virtual LIR* OpRegRegImm(OpKind op, int r_dest, int r_src1, int value) = 0; 732 virtual LIR* OpRegRegReg(OpKind op, int r_dest, int r_src1, int r_src2) = 0; 733 virtual LIR* OpTestSuspend(LIR* target) = 0; 734 virtual LIR* OpThreadMem(OpKind op, ThreadOffset thread_offset) = 0; 735 virtual LIR* OpVldm(int rBase, int count) = 0; 736 virtual LIR* OpVstm(int rBase, int count) = 0; 737 virtual void OpLea(int rBase, int reg1, int reg2, int scale, int offset) = 0; 738 virtual void OpRegCopyWide(int dest_lo, int dest_hi, int src_lo, int src_hi) = 0; 739 virtual void OpTlsCmp(ThreadOffset offset, int val) = 0; 740 virtual bool InexpensiveConstantInt(int32_t value) = 0; 741 virtual bool InexpensiveConstantFloat(int32_t value) = 0; 742 virtual bool InexpensiveConstantLong(int64_t value) = 0; 743 virtual bool InexpensiveConstantDouble(int64_t value) = 0; 744 745 // May be optimized by targets. 746 virtual void GenMonitorEnter(int opt_flags, RegLocation rl_src); 747 virtual void GenMonitorExit(int opt_flags, RegLocation rl_src); 748 749 // Temp workaround 750 void Workaround7250540(RegLocation rl_dest, int value); 751 752 protected: 753 Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena); 754 755 CompilationUnit* GetCompilationUnit() { 756 return cu_; 757 } 758 759 private: 760 void GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest, 761 RegLocation rl_src); 762 void GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final, 763 bool type_known_abstract, bool use_declaring_class, 764 bool can_assume_type_is_in_dex_cache, 765 uint32_t type_idx, RegLocation rl_dest, 766 RegLocation rl_src); 767 768 void ClobberBody(RegisterInfo* p); 769 void ResetDefBody(RegisterInfo* p) { 770 p->def_start = NULL; 771 p->def_end = NULL; 772 } 773 774 public: 775 // TODO: add accessors for these. 776 LIR* literal_list_; // Constants. 777 LIR* method_literal_list_; // Method literals requiring patching. 778 LIR* code_literal_list_; // Code literals requiring patching. 779 LIR* first_fixup_; // Doubly-linked list of LIR nodes requiring fixups. 780 781 protected: 782 CompilationUnit* const cu_; 783 MIRGraph* const mir_graph_; 784 GrowableArray<SwitchTable*> switch_tables_; 785 GrowableArray<FillArrayData*> fill_array_data_; 786 GrowableArray<LIR*> throw_launchpads_; 787 GrowableArray<LIR*> suspend_launchpads_; 788 GrowableArray<LIR*> intrinsic_launchpads_; 789 GrowableArray<RegisterInfo*> tempreg_info_; 790 GrowableArray<RegisterInfo*> reginfo_map_; 791 GrowableArray<void*> pointer_storage_; 792 CodeOffset current_code_offset_; // Working byte offset of machine instructons. 793 CodeOffset data_offset_; // starting offset of literal pool. 794 size_t total_size_; // header + code size. 795 LIR* block_label_list_; 796 PromotionMap* promotion_map_; 797 /* 798 * TODO: The code generation utilities don't have a built-in 799 * mechanism to propagate the original Dalvik opcode address to the 800 * associated generated instructions. For the trace compiler, this wasn't 801 * necessary because the interpreter handled all throws and debugging 802 * requests. For now we'll handle this by placing the Dalvik offset 803 * in the CompilationUnit struct before codegen for each instruction. 804 * The low-level LIR creation utilites will pull it from here. Rework this. 805 */ 806 DexOffset current_dalvik_offset_; 807 size_t estimated_native_code_size_; // Just an estimate; used to reserve code_buffer_ size. 808 RegisterPool* reg_pool_; 809 /* 810 * Sanity checking for the register temp tracking. The same ssa 811 * name should never be associated with one temp register per 812 * instruction compilation. 813 */ 814 int live_sreg_; 815 CodeBuffer code_buffer_; 816 // The encoding mapping table data (dex -> pc offset and pc offset -> dex) with a size prefix. 817 std::vector<uint8_t> encoded_mapping_table_; 818 std::vector<uint32_t> core_vmap_table_; 819 std::vector<uint32_t> fp_vmap_table_; 820 std::vector<uint8_t> native_gc_map_; 821 int num_core_spills_; 822 int num_fp_spills_; 823 int frame_size_; 824 unsigned int core_spill_mask_; 825 unsigned int fp_spill_mask_; 826 LIR* first_lir_insn_; 827 LIR* last_lir_insn_; 828 // Lazily retrieved method inliner for intrinsics. 829 const DexFileMethodInliner* inliner_; 830}; // Class Mir2Lir 831 832} // namespace art 833 834#endif // ART_COMPILER_DEX_QUICK_MIR_TO_LIR_H_ 835