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