code_generator.h revision eb7b7399dbdb5e471b8ae00a567bf4f19edd3907
1d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray/* 2d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project 3d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * 4d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License"); 5d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * you may not use this file except in compliance with the License. 6d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * You may obtain a copy of the License at 7d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * 8d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * http://www.apache.org/licenses/LICENSE-2.0 9d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * 10d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software 11d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS, 12d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * See the License for the specific language governing permissions and 14d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * limitations under the License. 15d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray */ 16d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 17d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#ifndef ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ 18d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ 19d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 20d582fa4ea62083a7598dded5b82dc2198b3daac7Ian Rogers#include "arch/instruction_set.h" 213416601a9e9be81bb7494864287fd3602d18ef13Calin Juravle#include "arch/instruction_set_features.h" 2201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray#include "base/bit_field.h" 23cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle#include "driver/compiler_options.h" 24bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray#include "globals.h" 25eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames#include "graph_visualizer.h" 264e3d23aa1523718ea1fdf3a32516d2f9d81e84feNicolas Geoffray#include "locations.h" 27d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "memory_region.h" 28d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "nodes.h" 293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray#include "stack_map_stream.h" 30d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 31d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffraynamespace art { 32d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 336d0e483dd2e0b63e952de060738c10e2abd12ff7Roland Levillain// Binary encoding of 2^32 for type double. 346d0e483dd2e0b63e952de060738c10e2abd12ff7Roland Levillainstatic int64_t constexpr k2Pow32EncodingForDouble = INT64_C(0x41F0000000000000); 356d0e483dd2e0b63e952de060738c10e2abd12ff7Roland Levillain// Binary encoding of 2^31 for type double. 366d0e483dd2e0b63e952de060738c10e2abd12ff7Roland Levillainstatic int64_t constexpr k2Pow31EncodingForDouble = INT64_C(0x41E0000000000000); 376d0e483dd2e0b63e952de060738c10e2abd12ff7Roland Levillain 38e82549b14c7def0a45461183964f7e6a34cbb70cMark Mendell// Minimum value for a primitive integer. 39e82549b14c7def0a45461183964f7e6a34cbb70cMark Mendellstatic int32_t constexpr kPrimIntMin = 0x80000000; 40e82549b14c7def0a45461183964f7e6a34cbb70cMark Mendell// Minimum value for a primitive long. 41e82549b14c7def0a45461183964f7e6a34cbb70cMark Mendellstatic int64_t constexpr kPrimLongMin = INT64_C(0x8000000000000000); 42e82549b14c7def0a45461183964f7e6a34cbb70cMark Mendell 433f8f936aff35f29d86183d31c20597ea17e9789dRoland Levillain// Maximum value for a primitive integer. 443f8f936aff35f29d86183d31c20597ea17e9789dRoland Levillainstatic int32_t constexpr kPrimIntMax = 0x7fffffff; 45624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain// Maximum value for a primitive long. 46e82549b14c7def0a45461183964f7e6a34cbb70cMark Mendellstatic int64_t constexpr kPrimLongMax = INT64_C(0x7fffffffffffffff); 473f8f936aff35f29d86183d31c20597ea17e9789dRoland Levillain 4892a73aef279be78e3c2b04db1713076183933436Nicolas Geoffrayclass Assembler; 49e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffrayclass CodeGenerator; 5092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffrayclass DexCompilationUnit; 51f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffrayclass ParallelMoveResolver; 52e21dc3db191df04c100620965bee4617b3b24397Andreas Gampeclass SrcMapElem; 53e21dc3db191df04c100620965bee4617b3b24397Andreas Gampetemplate <class Alloc> 54e3ea83811d47152c00abea24a9b420651a33b496Yevgeny Roubanclass SrcMap; 55e21dc3db191df04c100620965bee4617b3b24397Andreas Gampeusing DefaultSrcMap = SrcMap<std::allocator<SrcMapElem>>; 5692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 57d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffrayclass CodeAllocator { 58d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray public: 59e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray CodeAllocator() {} 60e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray virtual ~CodeAllocator() {} 61d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 62d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray virtual uint8_t* Allocate(size_t size) = 0; 63d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 64d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray private: 65d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeAllocator); 66d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray}; 67d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 686a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogersclass SlowPathCode : public ArenaObject<kArenaAllocSlowPaths> { 69e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray public: 70eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray SlowPathCode() { 71eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray for (size_t i = 0; i < kMaximumNumberOfExpectedRegisters; ++i) { 72eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray saved_core_stack_offsets_[i] = kRegisterNotSaved; 73eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray saved_fpu_stack_offsets_[i] = kRegisterNotSaved; 74eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray } 75eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray } 76eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray 77e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray virtual ~SlowPathCode() {} 78e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray 79e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray virtual void EmitNativeCode(CodeGenerator* codegen) = 0; 80e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray 81da40309f61f98c16d7d58e4c34cc0f5eef626f93Zheng Xu virtual void SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations); 82da40309f61f98c16d7d58e4c34cc0f5eef626f93Zheng Xu virtual void RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations); 83a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray void RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc); 84a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray 85eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray bool IsCoreRegisterSaved(int reg) const { 86eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray return saved_core_stack_offsets_[reg] != kRegisterNotSaved; 87eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray } 88eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray 89eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray bool IsFpuRegisterSaved(int reg) const { 90eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray return saved_fpu_stack_offsets_[reg] != kRegisterNotSaved; 91eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray } 92eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray 93eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray uint32_t GetStackOffsetOfCoreRegister(int reg) const { 94eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray return saved_core_stack_offsets_[reg]; 95eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray } 96eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray 97eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray uint32_t GetStackOffsetOfFpuRegister(int reg) const { 98eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray return saved_fpu_stack_offsets_[reg]; 99eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray } 100eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray 1019931f319cf86c56c2855d800339a3410697633a6Alexandre Rames virtual const char* GetDescription() const = 0; 1029931f319cf86c56c2855d800339a3410697633a6Alexandre Rames 103da40309f61f98c16d7d58e4c34cc0f5eef626f93Zheng Xu protected: 104eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray static constexpr size_t kMaximumNumberOfExpectedRegisters = 32; 105eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray static constexpr uint32_t kRegisterNotSaved = -1; 106eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray uint32_t saved_core_stack_offsets_[kMaximumNumberOfExpectedRegisters]; 107eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray uint32_t saved_fpu_stack_offsets_[kMaximumNumberOfExpectedRegisters]; 108da40309f61f98c16d7d58e4c34cc0f5eef626f93Zheng Xu 109da40309f61f98c16d7d58e4c34cc0f5eef626f93Zheng Xu private: 110e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray DISALLOW_COPY_AND_ASSIGN(SlowPathCode); 111e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray}; 112e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray 1132d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillainclass InvokeDexCallingConventionVisitor { 1142d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain public: 1152d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain virtual Location GetNextLocation(Primitive::Type type) = 0; 116fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray virtual Location GetReturnLocation(Primitive::Type type) const = 0; 117fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray virtual Location GetMethodLocation() const = 0; 1182d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain 1192d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain protected: 1202d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain InvokeDexCallingConventionVisitor() {} 1212d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain virtual ~InvokeDexCallingConventionVisitor() {} 1222d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain 1232d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain // The current index for core registers. 1242d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain uint32_t gp_index_ = 0u; 1252d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain // The current index for floating-point registers. 1262d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain uint32_t float_index_ = 0u; 1272d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain // The current stack index. 1282d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain uint32_t stack_index_ = 0u; 1292d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain 1302d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain private: 1312d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor); 1322d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain}; 1332d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain 13412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffrayclass CodeGenerator { 135d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray public: 136d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray // Compiles the graph to executable instructions. Returns whether the compilation 137d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray // succeeded. 13873e80c3ae76fafdb53afe3a85306dcb491fb5b00Nicolas Geoffray void CompileBaseline(CodeAllocator* allocator, bool is_leaf = false); 13986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray void CompileOptimized(CodeAllocator* allocator); 14012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray static CodeGenerator* Create(HGraph* graph, 1413416601a9e9be81bb7494864287fd3602d18ef13Calin Juravle InstructionSet instruction_set, 142cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle const InstructionSetFeatures& isa_features, 143cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle const CompilerOptions& compiler_options); 14412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray virtual ~CodeGenerator() {} 145d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 146787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray HGraph* GetGraph() const { return graph_; } 147d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 148dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray HBasicBlock* GetNextBlockToEmit() const; 149dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray HBasicBlock* FirstNonEmptyBlock(HBasicBlock* block) const; 150bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray bool GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const; 151bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray 15286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t GetStackSlotOfParameter(HParameterValue* parameter) const { 15386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Note that this follows the current calling convention. 15486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return GetFrameSize() 155e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier + InstructionSetPointerSize(GetInstructionSet()) // Art method 156e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray + parameter->GetIndex() * kVRegSize; 15786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 15886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 15992a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray virtual void Initialize() = 0; 16032f5b4d2c8c9b52e9522941c159577b21752d0faSerban Constantinescu virtual void Finalize(CodeAllocator* allocator); 161d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray virtual void GenerateFrameEntry() = 0; 162d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray virtual void GenerateFrameExit() = 0; 16392a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray virtual void Bind(HBasicBlock* block) = 0; 1644a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray virtual void Move(HInstruction* instruction, Location location, HInstruction* move_for) = 0; 165787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual Assembler* GetAssembler() = 0; 166eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames virtual const Assembler& GetAssembler() const = 0; 167707c809f661554713edfacf338365adca8dfd3a3Nicolas Geoffray virtual size_t GetWordSize() const = 0; 168f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell virtual size_t GetFloatingPointSpillSlotSize() const = 0; 169de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray virtual uintptr_t GetAddressOf(HBasicBlock* block) const = 0; 1704c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray void InitializeCodeGeneration(size_t number_of_spill_slots, 1714c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray size_t maximum_number_of_live_core_registers, 1724c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray size_t maximum_number_of_live_fp_registers, 1734c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray size_t number_of_out_slots, 1744c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray const GrowableArray<HBasicBlock*>& block_order); 175ab032bc1ff57831106fdac6a91a136293609401fNicolas Geoffray int32_t GetStackSlot(HLocal* local) const; 176ab032bc1ff57831106fdac6a91a136293609401fNicolas Geoffray Location GetTemporaryLocation(HTemporary* temp) const; 177787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 178787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray uint32_t GetFrameSize() const { return frame_size_; } 179787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray void SetFrameSize(uint32_t size) { frame_size_ = size; } 1808ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray uint32_t GetCoreSpillMask() const { return core_spill_mask_; } 181d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray uint32_t GetFpuSpillMask() const { return fpu_spill_mask_; } 1828ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray 18371175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray size_t GetNumberOfCoreRegisters() const { return number_of_core_registers_; } 18471175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray size_t GetNumberOfFloatingPointRegisters() const { return number_of_fpu_registers_; } 185988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray virtual void SetupBlockedRegisters(bool is_baseline) const = 0; 18671175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray 1874dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray virtual void ComputeSpillMask() { 1884dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_; 1894dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved"; 1904dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_; 1914dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray } 1924dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray 1934dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray static uint32_t ComputeRegisterMask(const int* registers, size_t length) { 1944dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray uint32_t mask = 0; 1954dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray for (size_t i = 0, e = length; i < e; ++i) { 1964dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray mask |= (1 << registers[i]); 1974dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray } 1984dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray return mask; 1994dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray } 2004dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray 201a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray virtual void DumpCoreRegister(std::ostream& stream, int reg) const = 0; 202a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray virtual void DumpFloatingPointRegister(std::ostream& stream, int reg) const = 0; 203412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray virtual InstructionSet GetInstructionSet() const = 0; 204cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle 205cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle const CompilerOptions& GetCompilerOptions() const { return compiler_options_; } 206cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle 207102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray // Saves the register in the stack. Returns the size taken on stack. 208102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray virtual size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) = 0; 209102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray // Restores the register from the stack. Returns the size taken on stack. 210102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray virtual size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) = 0; 211234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray 212234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray virtual size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) = 0; 213234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray virtual size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) = 0; 214234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray 215840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray virtual bool NeedsTwoRegisters(Primitive::Type type) const = 0; 216234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // Returns whether we should split long moves in parallel moves. 217234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray virtual bool ShouldSplitLongMoves() const { return false; } 218a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 219988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray bool IsCoreCalleeSaveRegister(int reg) const { 220988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray return (core_callee_save_mask_ & (1 << reg)) != 0; 221988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray } 222988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray 223988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray bool IsFloatingPointCalleeSaveRegister(int reg) const { 224988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray return (fpu_callee_save_mask_ & (1 << reg)) != 0; 225988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray } 226988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray 227eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray void RecordPcInfo(HInstruction* instruction, uint32_t dex_pc, SlowPathCode* slow_path = nullptr); 22877520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle bool CanMoveNullCheckToUser(HNullCheck* null_check); 22977520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle void MaybeRecordImplicitNullCheck(HInstruction* instruction); 230787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 231e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray void AddSlowPath(SlowPathCode* slow_path) { 232e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray slow_paths_.Add(slow_path); 233e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray } 234e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray 235c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky void BuildSourceMap(DefaultSrcMap* src_map) const; 236c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky void BuildMappingTable(std::vector<uint8_t>* vector) const; 2378ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray void BuildVMapTable(std::vector<uint8_t>* vector) const; 23892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray void BuildNativeGCMap( 23992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray std::vector<uint8_t>* vector, const DexCompilationUnit& dex_compilation_unit) const; 2403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray void BuildStackMaps(std::vector<uint8_t>* vector); 241787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 2423e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain bool IsBaseline() const { 2433e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain return is_baseline_; 2443e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain } 2453e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain 246f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray bool IsLeafMethod() const { 247f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray return is_leaf_; 248f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray } 249f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray 250f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray void MarkNotLeaf() { 251f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray is_leaf_ = false; 252c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray requires_current_method_ = true; 253c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray } 254c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray 255c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray void SetRequiresCurrentMethod() { 256c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray requires_current_method_ = true; 257c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray } 258c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray 259c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray bool RequiresCurrentMethod() const { 260c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray return requires_current_method_; 261f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray } 262f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray 2633c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // Clears the spill slots taken by loop phis in the `LocationSummary` of the 2643c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // suspend check. This is called when the code generator generates code 2653c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // for the suspend check at the back edge (instead of where the suspend check 2663c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // is, which is the loop entry). At this point, the spill slots for the phis 2673c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // have not been written to. 2683c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray void ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const; 2693c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray 27071175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray bool* GetBlockedCoreRegisters() const { return blocked_core_registers_; } 271102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray bool* GetBlockedFloatingPointRegisters() const { return blocked_fpu_registers_; } 27271175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray 27319a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray // Helper that returns the pointer offset of an index in an object array. 27419a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray // Note: this method assumes we always have the same pointer size, regardless 27519a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray // of the architecture. 27619a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray static size_t GetCacheOffset(uint32_t index); 277e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier // Pointer variant for ArtMethod and ArtField arrays. 278e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier size_t GetCachePointerOffset(uint32_t index); 27919a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray 2809021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray void EmitParallelMoves(Location from1, 2819021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray Location to1, 2829021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray Primitive::Type type1, 2839021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray Location from2, 2849021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray Location to2, 2859021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray Primitive::Type type2); 286f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray 287af07bc121121d7bd7e8329c55dfe24782207b561Nicolas Geoffray static bool StoreNeedsWriteBarrier(Primitive::Type type, HInstruction* value) { 288522e2241f5b5f331d0aa2f8508f4c97973f7f012David Brazdil // Check that null value is not represented as an integer constant. 289522e2241f5b5f331d0aa2f8508f4c97973f7f012David Brazdil DCHECK(type != Primitive::kPrimNot || !value->IsIntConstant()); 290522e2241f5b5f331d0aa2f8508f4c97973f7f012David Brazdil return type == Primitive::kPrimNot && !value->IsNullConstant(); 291af07bc121121d7bd7e8329c55dfe24782207b561Nicolas Geoffray } 292af07bc121121d7bd7e8329c55dfe24782207b561Nicolas Geoffray 293988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray void AddAllocatedRegister(Location location) { 294988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray allocated_registers_.Add(location); 295988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray } 296988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray 297c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray void AllocateLocations(HInstruction* instruction); 298c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray 299aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Tells whether the stack frame of the compiled method is 300aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty", that is either actually having a size of zero, 301aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // or just containing the saved return address register. 302aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain bool HasEmptyFrame() const { 303aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain return GetFrameSize() == (CallPushesPC() ? GetWordSize() : 0); 304aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain } 305aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain 306d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray static int32_t GetInt32ValueOf(HConstant* constant) { 307d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray if (constant->IsIntConstant()) { 308d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray return constant->AsIntConstant()->GetValue(); 309d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } else if (constant->IsNullConstant()) { 310d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray return 0; 311d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } else { 312d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray DCHECK(constant->IsFloatConstant()); 313da4d79bc9a4aeb9da7c6259ce4c9c1c3bf545eb8Roland Levillain return bit_cast<int32_t, float>(constant->AsFloatConstant()->GetValue()); 314d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } 315d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } 316d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray 317d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray static int64_t GetInt64ValueOf(HConstant* constant) { 318d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray if (constant->IsIntConstant()) { 319d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray return constant->AsIntConstant()->GetValue(); 320d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } else if (constant->IsNullConstant()) { 321d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray return 0; 322d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } else if (constant->IsFloatConstant()) { 323da4d79bc9a4aeb9da7c6259ce4c9c1c3bf545eb8Roland Levillain return bit_cast<int32_t, float>(constant->AsFloatConstant()->GetValue()); 324d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } else if (constant->IsLongConstant()) { 325d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray return constant->AsLongConstant()->GetValue(); 326d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } else { 327d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray DCHECK(constant->IsDoubleConstant()); 328da4d79bc9a4aeb9da7c6259ce4c9c1c3bf545eb8Roland Levillain return bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue()); 329d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } 330d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray } 331d6138ef1ea13d07ae555542f8898b30d89e9ac9aNicolas Geoffray 332a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray size_t GetFirstRegisterSlotInSlowPath() const { 333a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray return first_register_slot_in_slow_path_; 334a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray } 335a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray 336a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray uint32_t FrameEntrySpillSize() const { 337a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray return GetFpuSpillSize() + GetCoreSpillSize(); 338a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray } 339a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray 340ec525fc30848189051b888da53ba051bc0878b78Roland Levillain virtual ParallelMoveResolver* GetMoveResolver() = 0; 341a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray 342fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray static void CreateCommonInvokeLocationSummary( 343fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor); 344fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray 345eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames void SetDisassemblyInformation(DisassemblyInformation* info) { disasm_info_ = info; } 346eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames DisassemblyInformation* GetDisassemblyInformation() const { return disasm_info_; } 347eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 348787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray protected: 34971175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray CodeGenerator(HGraph* graph, 35071175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray size_t number_of_core_registers, 35171175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray size_t number_of_fpu_registers, 352cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle size_t number_of_register_pairs, 353988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray uint32_t core_callee_save_mask, 354988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray uint32_t fpu_callee_save_mask, 355cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle const CompilerOptions& compiler_options) 356c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray : frame_size_(0), 3574361beff5bc540c43ab7c072c99994adc4ed78f9Nicolas Geoffray core_spill_mask_(0), 358d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray fpu_spill_mask_(0), 3593bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray first_register_slot_in_slow_path_(0), 36071175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray blocked_core_registers_(graph->GetArena()->AllocArray<bool>(number_of_core_registers)), 36171175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray blocked_fpu_registers_(graph->GetArena()->AllocArray<bool>(number_of_fpu_registers)), 36271175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray blocked_register_pairs_(graph->GetArena()->AllocArray<bool>(number_of_register_pairs)), 36371175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray number_of_core_registers_(number_of_core_registers), 36471175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray number_of_fpu_registers_(number_of_fpu_registers), 36571175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray number_of_register_pairs_(number_of_register_pairs), 366988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray core_callee_save_mask_(core_callee_save_mask), 367988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray fpu_callee_save_mask_(fpu_callee_save_mask), 368cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko stack_map_stream_(graph->GetArena()), 369cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko block_order_(nullptr), 3703e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain is_baseline_(false), 371eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames disasm_info_(nullptr), 372787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray graph_(graph), 373cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle compiler_options_(compiler_options), 374e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray slow_paths_(graph->GetArena(), 8), 3754c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray current_block_index_(0), 3763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray is_leaf_(true), 377cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko requires_current_method_(false) {} 378d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 379a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray // Register allocation logic. 380a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray void AllocateRegistersLocally(HInstruction* instruction) const; 381a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray 382a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray // Backend specific implementation for allocating a register. 38371175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray virtual Location AllocateFreeRegister(Primitive::Type type) const = 0; 384a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray 38571175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray static size_t FindFreeEntry(bool* array, size_t length); 3863c03503d66df3b4440f851ae7d0c4fae5e7872dfNicolas Geoffray static size_t FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length); 387a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray 388a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray virtual Location GetStackLocation(HLoadLocal* load) const = 0; 389a7aca370a7d62ca04a1e24423d90e8020d6f1a58Nicolas Geoffray 390c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray virtual HGraphVisitor* GetLocationBuilder() = 0; 391c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray virtual HGraphVisitor* GetInstructionVisitor() = 0; 392f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray 393d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray // Returns the location of the first spilled entry for floating point registers, 394d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray // relative to the stack pointer. 395d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray uint32_t GetFpuSpillStart() const { 396d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray return GetFrameSize() - FrameEntrySpillSize(); 397d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray } 398d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray 399d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray uint32_t GetFpuSpillSize() const { 400d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray return POPCOUNT(fpu_spill_mask_) * GetFloatingPointSpillSlotSize(); 401d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray } 402d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray 403d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray uint32_t GetCoreSpillSize() const { 404d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray return POPCOUNT(core_spill_mask_) * GetWordSize(); 405d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray } 406d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray 407c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray bool HasAllocatedCalleeSaveRegisters() const { 408c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray // We check the core registers against 1 because it always comprises the return PC. 409c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray return (POPCOUNT(allocated_registers_.GetCoreRegisters() & core_callee_save_mask_) != 1) 410c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray || (POPCOUNT(allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_) != 0); 411c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray } 412c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray 413c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray bool CallPushesPC() const { 414c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray InstructionSet instruction_set = GetInstructionSet(); 415c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray return instruction_set == kX86 || instruction_set == kX86_64; 416c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray } 417c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray 418dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray // Arm64 has its own type for a label, so we need to templatize this method 419dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray // to share the logic. 420dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray template <typename T> 421dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray T* CommonGetLabelOf(T* raw_pointer_to_labels_array, HBasicBlock* block) const { 422dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray block = FirstNonEmptyBlock(block); 423dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray return raw_pointer_to_labels_array + block->GetBlockId(); 424dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray } 425dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray 4268ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // Frame size required for this method. 4278ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray uint32_t frame_size_; 4288ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray uint32_t core_spill_mask_; 429d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray uint32_t fpu_spill_mask_; 4303bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray uint32_t first_register_slot_in_slow_path_; 4318ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray 432988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray // Registers that were allocated during linear scan. 433988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray RegisterSet allocated_registers_; 434988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray 43571175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray // Arrays used when doing register allocation to know which 43671175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray // registers we can allocate. `SetupBlockedRegisters` updates the 43771175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray // arrays. 43871175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray bool* const blocked_core_registers_; 43971175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray bool* const blocked_fpu_registers_; 44071175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray bool* const blocked_register_pairs_; 44171175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray size_t number_of_core_registers_; 44271175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray size_t number_of_fpu_registers_; 44371175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray size_t number_of_register_pairs_; 444988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray const uint32_t core_callee_save_mask_; 445988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray const uint32_t fpu_callee_save_mask_; 44671175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray 447cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko StackMapStream stack_map_stream_; 448cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko 449cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko // The order to use for code generation. 450cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko const GrowableArray<HBasicBlock*>* block_order_; 451cf93a5cd9c978f59113d42f9f642fab5e2cc8877Vladimir Marko 4523e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain // Whether we are using baseline. 4533e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain bool is_baseline_; 4543e3d73349a2de81d14e2279f60ffbd9ab3f3ac28Roland Levillain 455eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames DisassemblyInformation* disasm_info_; 456eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 457bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray private: 458c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray void InitLocationsBaseline(HInstruction* instruction); 4593bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray size_t GetStackOffsetOfSavedRegister(size_t index); 460eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames void GenerateSlowPaths(); 4614c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray void CompileInternal(CodeAllocator* allocator, bool is_baseline); 4625f8741860d465410bfed495dbb5f794590d338daMark Mendell void BlockIfInRegister(Location location, bool is_out = false) const; 463b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray void EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path); 464d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 465787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray HGraph* const graph_; 466cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle const CompilerOptions& compiler_options_; 467d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 468e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray GrowableArray<SlowPathCode*> slow_paths_; 469d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 4704c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray // The current block index in `block_order_` of the block 4714c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray // we are generating code for. 4724c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray size_t current_block_index_; 4734c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray 474c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray // Whether the method is a leaf method. 475f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray bool is_leaf_; 476f12feb8e0e857f2832545b3f28d31bad5a9d3903Nicolas Geoffray 477c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray // Whether an instruction in the graph accesses the current method. 478c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray bool requires_current_method_; 479c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray 480c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky friend class OptimizingCFITest; 481c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky 482d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeGenerator); 483d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray}; 484d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 4857fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffraytemplate <typename C, typename F> 4864a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffrayclass CallingConvention { 4874a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray public: 4887fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray CallingConvention(const C* registers, 4897fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray size_t number_of_registers, 4907fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray const F* fpu_registers, 491e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier size_t number_of_fpu_registers, 492e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier size_t pointer_size) 4937fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray : registers_(registers), 4947fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray number_of_registers_(number_of_registers), 4957fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray fpu_registers_(fpu_registers), 496e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier number_of_fpu_registers_(number_of_fpu_registers), 497e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier pointer_size_(pointer_size) {} 4984a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray 4994a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray size_t GetNumberOfRegisters() const { return number_of_registers_; } 5007fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray size_t GetNumberOfFpuRegisters() const { return number_of_fpu_registers_; } 5014a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray 5027fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray C GetRegisterAt(size_t index) const { 5034a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray DCHECK_LT(index, number_of_registers_); 5044a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray return registers_[index]; 5054a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray } 5064a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray 5077fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray F GetFpuRegisterAt(size_t index) const { 5087fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray DCHECK_LT(index, number_of_fpu_registers_); 5097fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray return fpu_registers_[index]; 5107fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray } 5117fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray 5127fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray size_t GetStackOffsetOf(size_t index) const { 5134a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray // We still reserve the space for parameters passed by registers. 514e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier // Add space for the method pointer. 515e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier return pointer_size_ + index * kVRegSize; 5164a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray } 5174a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray 5184a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray private: 5197fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray const C* registers_; 5204a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray const size_t number_of_registers_; 5217fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray const F* fpu_registers_; 5227fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray const size_t number_of_fpu_registers_; 523e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier const size_t pointer_size_; 5244a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray 5254a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CallingConvention); 5264a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray}; 5274a34a428c6a2588e0857ef6baf88f1b73ce65958Nicolas Geoffray 528d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} // namespace art 529d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 530d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ 531