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 | |
Nicolas Geoffray | 86a8d7a | 2014-11-19 08:47:18 +0000 | [diff] [blame] | 34 | // Use a local definition to prevent copying mistakes. |
| 35 | static constexpr size_t kArm64WordSize = kArm64PointerSize; |
| 36 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 37 | static const vixl::Register kParameterCoreRegisters[] = { |
| 38 | vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7 |
| 39 | }; |
| 40 | static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters); |
| 41 | static const vixl::FPRegister kParameterFPRegisters[] = { |
| 42 | vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7 |
| 43 | }; |
| 44 | static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters); |
| 45 | |
| 46 | const vixl::Register tr = vixl::x18; // Thread Register |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 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); |
Serban Constantinescu | 02164b3 | 2014-11-13 14:05:07 +0000 | [diff] [blame] | 50 | const vixl::CPURegList runtime_reserved_core_registers(tr, vixl::lr); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 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); |
Serban Constantinescu | 02164b3 | 2014-11-13 14:05:07 +0000 | [diff] [blame] | 111 | void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor); |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 112 | void HandleBinaryOp(HBinaryOperation* instr); |
Serban Constantinescu | 02164b3 | 2014-11-13 14:05:07 +0000 | [diff] [blame] | 113 | void HandleShift(HBinaryOperation* instr); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 114 | |
| 115 | Arm64Assembler* const assembler_; |
| 116 | CodeGeneratorARM64* const codegen_; |
| 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64); |
| 119 | }; |
| 120 | |
| 121 | class LocationsBuilderARM64 : public HGraphVisitor { |
| 122 | public: |
| 123 | explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen) |
| 124 | : HGraphVisitor(graph), codegen_(codegen) {} |
| 125 | |
| 126 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 127 | void Visit##name(H##name* instr) OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 128 | FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 129 | #undef DECLARE_VISIT_INSTRUCTION |
| 130 | |
| 131 | private: |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 132 | void HandleBinaryOp(HBinaryOperation* instr); |
Serban Constantinescu | 02164b3 | 2014-11-13 14:05:07 +0000 | [diff] [blame] | 133 | void HandleShift(HBinaryOperation* instr); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 134 | void HandleInvoke(HInvoke* instr); |
| 135 | |
| 136 | CodeGeneratorARM64* const codegen_; |
| 137 | InvokeDexCallingConventionVisitor parameter_visitor_; |
| 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64); |
| 140 | }; |
| 141 | |
| 142 | class CodeGeneratorARM64 : public CodeGenerator { |
| 143 | public: |
| 144 | explicit CodeGeneratorARM64(HGraph* graph); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 145 | virtual ~CodeGeneratorARM64() {} |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 146 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 147 | void GenerateFrameEntry() OVERRIDE; |
| 148 | void GenerateFrameExit() OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 149 | |
| 150 | static const vixl::CPURegList& GetFramePreservedRegisters() { |
| 151 | static const vixl::CPURegList frame_preserved_regs = |
| 152 | vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit()); |
| 153 | return frame_preserved_regs; |
| 154 | } |
| 155 | static int GetFramePreservedRegistersSize() { |
| 156 | return GetFramePreservedRegisters().TotalSizeInBytes(); |
| 157 | } |
| 158 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 159 | void Bind(HBasicBlock* block) OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 160 | |
| 161 | vixl::Label* GetLabelOf(HBasicBlock* block) const { |
| 162 | return block_labels_ + block->GetBlockId(); |
| 163 | } |
| 164 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 165 | void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 166 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 167 | size_t GetWordSize() const OVERRIDE { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 168 | return kArm64WordSize; |
| 169 | } |
| 170 | |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 171 | uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE { |
| 172 | vixl::Label* block_entry_label = GetLabelOf(block); |
| 173 | DCHECK(block_entry_label->IsBound()); |
| 174 | return block_entry_label->location(); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 175 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 176 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 177 | size_t FrameEntrySpillSize() const OVERRIDE; |
| 178 | |
| 179 | HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; } |
| 180 | HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; } |
| 181 | Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; } |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 182 | vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 183 | |
| 184 | // Emit a write barrier. |
| 185 | void MarkGCCard(vixl::Register object, vixl::Register value); |
| 186 | |
| 187 | // Register allocation. |
| 188 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 189 | void SetupBlockedRegisters() const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 190 | // AllocateFreeRegister() is only used when allocating registers locally |
| 191 | // during CompileBaseline(). |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 192 | Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 193 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 194 | Location GetStackLocation(HLoadLocal* load) const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 195 | |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 196 | size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 197 | UNUSED(stack_index); |
| 198 | UNUSED(reg_id); |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 199 | LOG(INFO) << "CodeGeneratorARM64::SaveCoreRegister()"; |
| 200 | return kArm64WordSize; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 203 | size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 204 | UNUSED(stack_index); |
| 205 | UNUSED(reg_id); |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 206 | LOG(INFO) << "CodeGeneratorARM64::RestoreCoreRegister()"; |
| 207 | return kArm64WordSize; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // The number of registers that can be allocated. The register allocator may |
| 211 | // decide to reserve and not use a few of them. |
| 212 | // We do not consider registers sp, xzr, wzr. They are either not allocatable |
| 213 | // (xzr, wzr), or make for poor allocatable registers (sp alignment |
| 214 | // requirements, etc.). This also facilitates our task as all other registers |
| 215 | // 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] | 216 | static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1; |
| 217 | static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 218 | static constexpr int kNumberOfAllocatableRegisterPairs = 0; |
| 219 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 220 | void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE; |
| 221 | void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 222 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 223 | InstructionSet GetInstructionSet() const OVERRIDE { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 224 | return InstructionSet::kArm64; |
| 225 | } |
| 226 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 227 | void Initialize() OVERRIDE { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 228 | HGraph* graph = GetGraph(); |
| 229 | int length = graph->GetBlocks().Size(); |
| 230 | block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length); |
| 231 | for (int i = 0; i < length; ++i) { |
| 232 | new(block_labels_ + i) vixl::Label(); |
| 233 | } |
| 234 | } |
| 235 | |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 236 | void Finalize(CodeAllocator* allocator) OVERRIDE; |
| 237 | |
Alexandre Rames | fc19de8 | 2014-11-07 17:13:31 +0000 | [diff] [blame] | 238 | // Code generation helpers. |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 239 | void MoveConstant(vixl::CPURegister destination, HConstant* constant); |
Alexandre Rames | fc19de8 | 2014-11-07 17:13:31 +0000 | [diff] [blame] | 240 | void MoveHelper(Location destination, Location source, Primitive::Type type); |
Alexandre Rames | 67555f7 | 2014-11-18 10:55:16 +0000 | [diff] [blame] | 241 | void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src); |
| 242 | void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst); |
| 243 | void LoadCurrentMethod(vixl::Register current_method); |
| 244 | |
| 245 | // Generate code to invoke a runtime entry point. |
| 246 | void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc); |
Alexandre Rames | fc19de8 | 2014-11-07 17:13:31 +0000 | [diff] [blame] | 247 | |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 248 | ParallelMoveResolver* GetMoveResolver() OVERRIDE { |
| 249 | UNIMPLEMENTED(INFO) << "TODO: MoveResolver"; |
| 250 | return nullptr; |
| 251 | } |
| 252 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 253 | private: |
| 254 | // Labels for each block that will be compiled. |
| 255 | vixl::Label* block_labels_; |
| 256 | |
| 257 | LocationsBuilderARM64 location_builder_; |
| 258 | InstructionCodeGeneratorARM64 instruction_visitor_; |
| 259 | Arm64Assembler assembler_; |
| 260 | |
| 261 | DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64); |
| 262 | }; |
| 263 | |
| 264 | } // namespace arm64 |
| 265 | } // namespace art |
| 266 | |
| 267 | #endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_ |