Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_OPTIMIZING_CODE_GENERATOR_ARM64_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_ |
| 19 | |
| 20 | #include "code_generator.h" |
| 21 | #include "nodes.h" |
| 22 | #include "parallel_move_resolver.h" |
| 23 | #include "utils/arm64/assembler_arm64.h" |
| 24 | #include "a64/disasm-a64.h" |
| 25 | #include "a64/macro-assembler-a64.h" |
| 26 | #include "arch/arm64/quick_method_frame_info_arm64.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace arm64 { |
| 30 | |
| 31 | class CodeGeneratorARM64; |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 32 | class SlowPathCodeARM64; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 33 | |
| 34 | static constexpr size_t kArm64WordSize = 8; |
| 35 | static const vixl::Register kParameterCoreRegisters[] = { |
| 36 | vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7 |
| 37 | }; |
| 38 | static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters); |
| 39 | static const vixl::FPRegister kParameterFPRegisters[] = { |
| 40 | vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7 |
| 41 | }; |
| 42 | static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters); |
| 43 | |
| 44 | const vixl::Register tr = vixl::x18; // Thread Register |
| 45 | const vixl::Register wSuspend = vixl::w19; // Suspend Register |
| 46 | const vixl::Register xSuspend = vixl::x19; |
| 47 | |
| 48 | const vixl::CPURegList vixl_reserved_core_registers(vixl::ip0, vixl::ip1); |
Alexandre Rames | a89086e | 2014-11-07 17:13:25 +0000 | [diff] [blame] | 49 | const vixl::CPURegList vixl_reserved_fp_registers(vixl::d31); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 50 | const vixl::CPURegList runtime_reserved_core_registers(tr, xSuspend, vixl::lr); |
| 51 | const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister, |
| 52 | vixl::kXRegSize, |
| 53 | kArm64CalleeSaveRefSpills); |
| 54 | |
Alexandre Rames | a89086e | 2014-11-07 17:13:25 +0000 | [diff] [blame] | 55 | Location ARM64ReturnLocation(Primitive::Type return_type); |
| 56 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 57 | class InvokeDexCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> { |
| 58 | public: |
| 59 | InvokeDexCallingConvention() |
| 60 | : CallingConvention(kParameterCoreRegisters, |
| 61 | kParameterCoreRegistersLength, |
| 62 | kParameterFPRegisters, |
| 63 | kParameterFPRegistersLength) {} |
| 64 | |
| 65 | Location GetReturnLocation(Primitive::Type return_type) { |
Alexandre Rames | a89086e | 2014-11-07 17:13:25 +0000 | [diff] [blame] | 66 | return ARM64ReturnLocation(return_type); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | private: |
| 71 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention); |
| 72 | }; |
| 73 | |
| 74 | class InvokeDexCallingConventionVisitor { |
| 75 | public: |
Alexandre Rames | a89086e | 2014-11-07 17:13:25 +0000 | [diff] [blame] | 76 | InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {} |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 77 | |
| 78 | Location GetNextLocation(Primitive::Type type); |
| 79 | Location GetReturnLocation(Primitive::Type return_type) { |
| 80 | return calling_convention.GetReturnLocation(return_type); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | InvokeDexCallingConvention calling_convention; |
| 85 | // The current index for core registers. |
| 86 | uint32_t gp_index_; |
Alexandre Rames | a89086e | 2014-11-07 17:13:25 +0000 | [diff] [blame] | 87 | // The current index for floating-point registers. |
| 88 | uint32_t fp_index_; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 89 | // The current stack index. |
| 90 | uint32_t stack_index_; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor); |
| 93 | }; |
| 94 | |
| 95 | class InstructionCodeGeneratorARM64 : public HGraphVisitor { |
| 96 | public: |
| 97 | InstructionCodeGeneratorARM64(HGraph* graph, CodeGeneratorARM64* codegen); |
| 98 | |
| 99 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 100 | void Visit##name(H##name* instr) OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 101 | FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 102 | #undef DECLARE_VISIT_INSTRUCTION |
| 103 | |
| 104 | void LoadCurrentMethod(XRegister reg); |
| 105 | |
| 106 | Arm64Assembler* GetAssembler() const { return assembler_; } |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 107 | vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 108 | |
| 109 | private: |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 110 | void GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path, vixl::Register class_reg); |
| 111 | void HandleBinaryOp(HBinaryOperation* instr); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 112 | |
| 113 | Arm64Assembler* const assembler_; |
| 114 | CodeGeneratorARM64* const codegen_; |
| 115 | |
| 116 | DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64); |
| 117 | }; |
| 118 | |
| 119 | class LocationsBuilderARM64 : public HGraphVisitor { |
| 120 | public: |
| 121 | explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen) |
| 122 | : HGraphVisitor(graph), codegen_(codegen) {} |
| 123 | |
| 124 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 125 | void Visit##name(H##name* instr) OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 126 | FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 127 | #undef DECLARE_VISIT_INSTRUCTION |
| 128 | |
| 129 | private: |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 130 | void HandleBinaryOp(HBinaryOperation* instr); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 131 | void HandleInvoke(HInvoke* instr); |
| 132 | |
| 133 | CodeGeneratorARM64* const codegen_; |
| 134 | InvokeDexCallingConventionVisitor parameter_visitor_; |
| 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64); |
| 137 | }; |
| 138 | |
| 139 | class CodeGeneratorARM64 : public CodeGenerator { |
| 140 | public: |
| 141 | explicit CodeGeneratorARM64(HGraph* graph); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 142 | virtual ~CodeGeneratorARM64() {} |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 143 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 144 | void GenerateFrameEntry() OVERRIDE; |
| 145 | void GenerateFrameExit() OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 146 | |
| 147 | static const vixl::CPURegList& GetFramePreservedRegisters() { |
| 148 | static const vixl::CPURegList frame_preserved_regs = |
| 149 | vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit()); |
| 150 | return frame_preserved_regs; |
| 151 | } |
| 152 | static int GetFramePreservedRegistersSize() { |
| 153 | return GetFramePreservedRegisters().TotalSizeInBytes(); |
| 154 | } |
| 155 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 156 | void Bind(HBasicBlock* block) OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 157 | |
| 158 | vixl::Label* GetLabelOf(HBasicBlock* block) const { |
| 159 | return block_labels_ + block->GetBlockId(); |
| 160 | } |
| 161 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 162 | void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 163 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 164 | size_t GetWordSize() const OVERRIDE { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 165 | return kArm64WordSize; |
| 166 | } |
| 167 | |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 168 | uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE { |
| 169 | vixl::Label* block_entry_label = GetLabelOf(block); |
| 170 | DCHECK(block_entry_label->IsBound()); |
| 171 | return block_entry_label->location(); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 172 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 173 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 174 | size_t FrameEntrySpillSize() const OVERRIDE; |
| 175 | |
| 176 | HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; } |
| 177 | HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; } |
| 178 | Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; } |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 179 | vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 180 | |
| 181 | // Emit a write barrier. |
| 182 | void MarkGCCard(vixl::Register object, vixl::Register value); |
| 183 | |
| 184 | // Register allocation. |
| 185 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 186 | void SetupBlockedRegisters() const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 187 | // AllocateFreeRegister() is only used when allocating registers locally |
| 188 | // during CompileBaseline(). |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 189 | Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 190 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 191 | Location GetStackLocation(HLoadLocal* load) const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 192 | |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 193 | size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 194 | UNUSED(stack_index); |
| 195 | UNUSED(reg_id); |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 196 | LOG(INFO) << "CodeGeneratorARM64::SaveCoreRegister()"; |
| 197 | return kArm64WordSize; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 200 | size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 201 | UNUSED(stack_index); |
| 202 | UNUSED(reg_id); |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 203 | LOG(INFO) << "CodeGeneratorARM64::RestoreCoreRegister()"; |
| 204 | return kArm64WordSize; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // The number of registers that can be allocated. The register allocator may |
| 208 | // decide to reserve and not use a few of them. |
| 209 | // We do not consider registers sp, xzr, wzr. They are either not allocatable |
| 210 | // (xzr, wzr), or make for poor allocatable registers (sp alignment |
| 211 | // requirements, etc.). This also facilitates our task as all other registers |
| 212 | // can easily be mapped via to or from their type and index or code. |
Alexandre Rames | a89086e | 2014-11-07 17:13:25 +0000 | [diff] [blame] | 213 | static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1; |
| 214 | static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 215 | static constexpr int kNumberOfAllocatableRegisterPairs = 0; |
| 216 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 217 | void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE; |
| 218 | void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 219 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 220 | InstructionSet GetInstructionSet() const OVERRIDE { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 221 | return InstructionSet::kArm64; |
| 222 | } |
| 223 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 224 | void Initialize() OVERRIDE { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 225 | HGraph* graph = GetGraph(); |
| 226 | int length = graph->GetBlocks().Size(); |
| 227 | block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length); |
| 228 | for (int i = 0; i < length; ++i) { |
| 229 | new(block_labels_ + i) vixl::Label(); |
| 230 | } |
| 231 | } |
| 232 | |
Alexandre Rames | fc19de8 | 2014-11-07 17:13:31 +0000 | [diff] [blame] | 233 | // Code generation helpers. |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 234 | void MoveConstant(vixl::CPURegister destination, HConstant* constant); |
Alexandre Rames | fc19de8 | 2014-11-07 17:13:31 +0000 | [diff] [blame] | 235 | void MoveHelper(Location destination, Location source, Primitive::Type type); |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame^] | 236 | void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src); |
| 237 | void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst); |
| 238 | void LoadCurrentMethod(vixl::Register current_method); |
| 239 | |
| 240 | // Generate code to invoke a runtime entry point. |
| 241 | void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc); |
Alexandre Rames | fc19de8 | 2014-11-07 17:13:31 +0000 | [diff] [blame] | 242 | |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 243 | ParallelMoveResolver* GetMoveResolver() OVERRIDE { |
| 244 | UNIMPLEMENTED(INFO) << "TODO: MoveResolver"; |
| 245 | return nullptr; |
| 246 | } |
| 247 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 248 | private: |
| 249 | // Labels for each block that will be compiled. |
| 250 | vixl::Label* block_labels_; |
| 251 | |
| 252 | LocationsBuilderARM64 location_builder_; |
| 253 | InstructionCodeGeneratorARM64 instruction_visitor_; |
| 254 | Arm64Assembler assembler_; |
| 255 | |
| 256 | DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64); |
| 257 | }; |
| 258 | |
| 259 | } // namespace arm64 |
| 260 | } // namespace art |
| 261 | |
| 262 | #endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_ |