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