| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40: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_X86_64_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_ |
| 19 | |
| 20 | #include "code_generator.h" |
| 21 | #include "nodes.h" |
| Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 22 | #include "parallel_move_resolver.h" |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 23 | #include "utils/x86_64/assembler_x86_64.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace x86_64 { |
| 27 | |
| 28 | static constexpr size_t kX86_64WordSize = 8; |
| 29 | |
| 30 | static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 }; |
| Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 31 | static constexpr FloatRegister kParameterFloatRegisters[] = |
| 32 | { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 }; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 33 | |
| 34 | static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters); |
| Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 35 | static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters); |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 36 | |
| Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 37 | class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> { |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 38 | public: |
| Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 39 | InvokeDexCallingConvention() : CallingConvention( |
| 40 | kParameterCoreRegisters, |
| 41 | kParameterCoreRegistersLength, |
| 42 | kParameterFloatRegisters, |
| 43 | kParameterFloatRegistersLength) {} |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention); |
| 47 | }; |
| 48 | |
| 49 | class InvokeDexCallingConventionVisitor { |
| 50 | public: |
| Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 51 | InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {} |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 52 | |
| 53 | Location GetNextLocation(Primitive::Type type); |
| 54 | |
| 55 | private: |
| 56 | InvokeDexCallingConvention calling_convention; |
| Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 57 | // The current index for cpu registers. |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 58 | uint32_t gp_index_; |
| Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 59 | // The current index for fpu registers. |
| 60 | uint32_t fp_index_; |
| 61 | // The current stack index. |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 62 | uint32_t stack_index_; |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor); |
| 65 | }; |
| 66 | |
| 67 | class CodeGeneratorX86_64; |
| Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 68 | class SlowPathCodeX86_64; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 69 | |
| Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 70 | class ParallelMoveResolverX86_64 : public ParallelMoveResolver { |
| 71 | public: |
| 72 | ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen) |
| 73 | : ParallelMoveResolver(allocator), codegen_(codegen) {} |
| 74 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 75 | void EmitMove(size_t index) OVERRIDE; |
| 76 | void EmitSwap(size_t index) OVERRIDE; |
| 77 | void SpillScratch(int reg) OVERRIDE; |
| 78 | void RestoreScratch(int reg) OVERRIDE; |
| Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 79 | |
| 80 | X86_64Assembler* GetAssembler() const; |
| 81 | |
| 82 | private: |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 83 | void Exchange32(CpuRegister reg, int mem); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 84 | void Exchange32(XmmRegister reg, int mem); |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 85 | void Exchange32(int mem1, int mem2); |
| 86 | void Exchange64(CpuRegister reg, int mem); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 87 | void Exchange64(XmmRegister reg, int mem); |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 88 | void Exchange64(int mem1, int mem2); |
| Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 89 | |
| 90 | CodeGeneratorX86_64* const codegen_; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64); |
| 93 | }; |
| 94 | |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 95 | class LocationsBuilderX86_64 : public HGraphVisitor { |
| 96 | public: |
| 97 | LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen) |
| 98 | : HGraphVisitor(graph), codegen_(codegen) {} |
| 99 | |
| Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 100 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 101 | void Visit##name(H##name* instr) OVERRIDE; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 102 | |
| Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 103 | FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 104 | |
| 105 | #undef DECLARE_VISIT_INSTRUCTION |
| 106 | |
| 107 | private: |
| Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame^] | 108 | void HandleInvoke(HInvoke* invoke); |
| 109 | void HandleBitwiseOperation(HBinaryOperation* operation); |
| 110 | |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 111 | CodeGeneratorX86_64* const codegen_; |
| 112 | InvokeDexCallingConventionVisitor parameter_visitor_; |
| 113 | |
| 114 | DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64); |
| 115 | }; |
| 116 | |
| 117 | class InstructionCodeGeneratorX86_64 : public HGraphVisitor { |
| 118 | public: |
| 119 | InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen); |
| 120 | |
| Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 121 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 122 | void Visit##name(H##name* instr) OVERRIDE; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 123 | |
| Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 124 | FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 125 | |
| 126 | #undef DECLARE_VISIT_INSTRUCTION |
| 127 | |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 128 | X86_64Assembler* GetAssembler() const { return assembler_; } |
| 129 | |
| 130 | private: |
| Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 131 | // Generate code for the given suspend check. If not null, `successor` |
| 132 | // is the block to branch to if the suspend check is not needed, and after |
| 133 | // the suspend call. |
| 134 | void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor); |
| Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 135 | void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg); |
| Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame^] | 136 | void HandleBitwiseOperation(HBinaryOperation* operation); |
| Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 137 | |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 138 | X86_64Assembler* const assembler_; |
| 139 | CodeGeneratorX86_64* const codegen_; |
| 140 | |
| 141 | DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64); |
| 142 | }; |
| 143 | |
| 144 | class CodeGeneratorX86_64 : public CodeGenerator { |
| 145 | public: |
| 146 | explicit CodeGeneratorX86_64(HGraph* graph); |
| 147 | virtual ~CodeGeneratorX86_64() {} |
| 148 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 149 | void GenerateFrameEntry() OVERRIDE; |
| 150 | void GenerateFrameExit() OVERRIDE; |
| 151 | void Bind(HBasicBlock* block) OVERRIDE; |
| 152 | void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE; |
| 153 | size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
| 154 | size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
| 155 | size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
| 156 | size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 157 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 158 | size_t GetWordSize() const OVERRIDE { |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 159 | return kX86_64WordSize; |
| 160 | } |
| 161 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 162 | size_t FrameEntrySpillSize() const OVERRIDE; |
| Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 163 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 164 | HGraphVisitor* GetLocationBuilder() OVERRIDE { |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 165 | return &location_builder_; |
| 166 | } |
| 167 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 168 | HGraphVisitor* GetInstructionVisitor() OVERRIDE { |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 169 | return &instruction_visitor_; |
| 170 | } |
| 171 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 172 | X86_64Assembler* GetAssembler() OVERRIDE { |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 173 | return &assembler_; |
| 174 | } |
| 175 | |
| Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 176 | ParallelMoveResolverX86_64* GetMoveResolver() { |
| 177 | return &move_resolver_; |
| 178 | } |
| 179 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 180 | uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE { |
| 181 | return GetLabelOf(block)->Position(); |
| 182 | } |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 183 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 184 | Location GetStackLocation(HLoadLocal* load) const OVERRIDE; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 185 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 186 | void SetupBlockedRegisters() const OVERRIDE; |
| 187 | Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE; |
| 188 | void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE; |
| 189 | void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE; |
| 190 | |
| 191 | InstructionSet GetInstructionSet() const OVERRIDE { |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 192 | return InstructionSet::kX86_64; |
| 193 | } |
| 194 | |
| Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 195 | // Emit a write barrier. |
| 196 | void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value); |
| 197 | |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 198 | // Helper method to move a value between two locations. |
| 199 | void Move(Location destination, Location source); |
| 200 | |
| Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 201 | void LoadCurrentMethod(CpuRegister reg); |
| 202 | |
| Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 203 | Label* GetLabelOf(HBasicBlock* block) const { |
| 204 | return block_labels_.GetRawStorage() + block->GetBlockId(); |
| 205 | } |
| 206 | |
| Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 207 | void Initialize() OVERRIDE { |
| Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 208 | block_labels_.SetSize(GetGraph()->GetBlocks().Size()); |
| 209 | } |
| 210 | |
| Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 211 | private: |
| Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 212 | // Labels for each block that will be compiled. |
| 213 | GrowableArray<Label> block_labels_; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 214 | LocationsBuilderX86_64 location_builder_; |
| 215 | InstructionCodeGeneratorX86_64 instruction_visitor_; |
| Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 216 | ParallelMoveResolverX86_64 move_resolver_; |
| Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 217 | X86_64Assembler assembler_; |
| 218 | |
| 219 | DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64); |
| 220 | }; |
| 221 | |
| 222 | } // namespace x86_64 |
| 223 | } // namespace art |
| 224 | |
| 225 | #endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_ |