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