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 | |
Mark P Mendell | 17077d8 | 2015-12-16 19:15:59 +0000 | [diff] [blame] | 20 | #include "arch/x86_64/instruction_set_features_x86_64.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 21 | #include "code_generator.h" |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 22 | #include "dex/compiler_enums.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 23 | #include "driver/compiler_options.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 24 | #include "nodes.h" |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 25 | #include "parallel_move_resolver.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 26 | #include "utils/x86_64/assembler_x86_64.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace x86_64 { |
| 30 | |
Nicolas Geoffray | 86a8d7a | 2014-11-19 08:47:18 +0000 | [diff] [blame] | 31 | // Use a local definition to prevent copying mistakes. |
| 32 | static constexpr size_t kX86_64WordSize = kX86_64PointerSize; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 33 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 34 | // Some x86_64 instructions require a register to be available as temp. |
| 35 | static constexpr Register TMP = R11; |
| 36 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 37 | static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 }; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 38 | static constexpr FloatRegister kParameterFloatRegisters[] = |
| 39 | { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 }; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 40 | |
| 41 | static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 42 | static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 43 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 44 | static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX }; |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 45 | static constexpr size_t kRuntimeParameterCoreRegistersLength = |
| 46 | arraysize(kRuntimeParameterCoreRegisters); |
| 47 | static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 }; |
| 48 | static constexpr size_t kRuntimeParameterFpuRegistersLength = |
| 49 | arraysize(kRuntimeParameterFpuRegisters); |
| 50 | |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 51 | // These XMM registers are non-volatile in ART ABI, but volatile in native ABI. |
| 52 | // If the ART ABI changes, this list must be updated. It is used to ensure that |
| 53 | // these are not clobbered by any direct call to native code (such as math intrinsics). |
| 54 | static constexpr FloatRegister non_volatile_xmm_regs[] = { XMM12, XMM13, XMM14, XMM15 }; |
| 55 | |
| 56 | |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 57 | class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> { |
| 58 | public: |
| 59 | InvokeRuntimeCallingConvention() |
| 60 | : CallingConvention(kRuntimeParameterCoreRegisters, |
| 61 | kRuntimeParameterCoreRegistersLength, |
| 62 | kRuntimeParameterFpuRegisters, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 63 | kRuntimeParameterFpuRegistersLength, |
| 64 | kX86_64PointerSize) {} |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention); |
| 68 | }; |
| 69 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 70 | class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 71 | public: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 72 | InvokeDexCallingConvention() : CallingConvention( |
| 73 | kParameterCoreRegisters, |
| 74 | kParameterCoreRegistersLength, |
| 75 | kParameterFloatRegisters, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 76 | kParameterFloatRegistersLength, |
| 77 | kX86_64PointerSize) {} |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention); |
| 81 | }; |
| 82 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 83 | class FieldAccessCallingConventionX86_64 : public FieldAccessCallingConvention { |
| 84 | public: |
| 85 | FieldAccessCallingConventionX86_64() {} |
| 86 | |
| 87 | Location GetObjectLocation() const OVERRIDE { |
| 88 | return Location::RegisterLocation(RSI); |
| 89 | } |
| 90 | Location GetFieldIndexLocation() const OVERRIDE { |
| 91 | return Location::RegisterLocation(RDI); |
| 92 | } |
| 93 | Location GetReturnLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE { |
| 94 | return Location::RegisterLocation(RAX); |
| 95 | } |
| 96 | Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE { |
| 97 | return Primitive::Is64BitType(type) |
| 98 | ? Location::RegisterLocation(RDX) |
| 99 | : (is_instance |
| 100 | ? Location::RegisterLocation(RDX) |
| 101 | : Location::RegisterLocation(RSI)); |
| 102 | } |
| 103 | Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE { |
| 104 | return Location::FpuRegisterLocation(XMM0); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionX86_64); |
| 109 | }; |
| 110 | |
| 111 | |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 112 | class InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 113 | public: |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 114 | InvokeDexCallingConventionVisitorX86_64() {} |
| 115 | virtual ~InvokeDexCallingConventionVisitorX86_64() {} |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 116 | |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 117 | Location GetNextLocation(Primitive::Type type) OVERRIDE; |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 118 | Location GetReturnLocation(Primitive::Type type) const OVERRIDE; |
| 119 | Location GetMethodLocation() const OVERRIDE; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | InvokeDexCallingConvention calling_convention; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 123 | |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 124 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | class CodeGeneratorX86_64; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 128 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 129 | class ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 130 | public: |
| 131 | ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen) |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 132 | : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {} |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 133 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 134 | void EmitMove(size_t index) OVERRIDE; |
| 135 | void EmitSwap(size_t index) OVERRIDE; |
| 136 | void SpillScratch(int reg) OVERRIDE; |
| 137 | void RestoreScratch(int reg) OVERRIDE; |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 138 | |
| 139 | X86_64Assembler* GetAssembler() const; |
| 140 | |
| 141 | private: |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 142 | void Exchange32(CpuRegister reg, int mem); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 143 | void Exchange32(XmmRegister reg, int mem); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 144 | void Exchange32(int mem1, int mem2); |
| 145 | void Exchange64(CpuRegister reg, int mem); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 146 | void Exchange64(XmmRegister reg, int mem); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 147 | void Exchange64(int mem1, int mem2); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 148 | |
| 149 | CodeGeneratorX86_64* const codegen_; |
| 150 | |
| 151 | DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64); |
| 152 | }; |
| 153 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 154 | class LocationsBuilderX86_64 : public HGraphVisitor { |
| 155 | public: |
| 156 | LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen) |
| 157 | : HGraphVisitor(graph), codegen_(codegen) {} |
| 158 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 159 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 160 | void Visit##name(H##name* instr) OVERRIDE; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 161 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 162 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION) |
| 163 | FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION) |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 164 | |
| 165 | #undef DECLARE_VISIT_INSTRUCTION |
| 166 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 167 | void VisitInstruction(HInstruction* instruction) OVERRIDE { |
| 168 | LOG(FATAL) << "Unreachable instruction " << instruction->DebugName() |
| 169 | << " (id " << instruction->GetId() << ")"; |
| 170 | } |
| 171 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 172 | private: |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 173 | void HandleInvoke(HInvoke* invoke); |
| 174 | void HandleBitwiseOperation(HBinaryOperation* operation); |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 175 | void HandleCondition(HCondition* condition); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 176 | void HandleShift(HBinaryOperation* operation); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 177 | void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info); |
| 178 | void HandleFieldGet(HInstruction* instruction); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 179 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 180 | CodeGeneratorX86_64* const codegen_; |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 181 | InvokeDexCallingConventionVisitorX86_64 parameter_visitor_; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 182 | |
| 183 | DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64); |
| 184 | }; |
| 185 | |
| 186 | class InstructionCodeGeneratorX86_64 : public HGraphVisitor { |
| 187 | public: |
| 188 | InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen); |
| 189 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 190 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 191 | void Visit##name(H##name* instr) OVERRIDE; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 192 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 193 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION) |
| 194 | FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION) |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 195 | |
| 196 | #undef DECLARE_VISIT_INSTRUCTION |
| 197 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 198 | void VisitInstruction(HInstruction* instruction) OVERRIDE { |
| 199 | LOG(FATAL) << "Unreachable instruction " << instruction->DebugName() |
| 200 | << " (id " << instruction->GetId() << ")"; |
| 201 | } |
| 202 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 203 | X86_64Assembler* GetAssembler() const { return assembler_; } |
| 204 | |
| 205 | private: |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 206 | // Generate code for the given suspend check. If not null, `successor` |
| 207 | // is the block to branch to if the suspend check is not needed, and after |
| 208 | // the suspend call. |
| 209 | void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 210 | void GenerateClassInitializationCheck(SlowPathCode* slow_path, CpuRegister class_reg); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 211 | void HandleBitwiseOperation(HBinaryOperation* operation); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 212 | void GenerateRemFP(HRem* rem); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 213 | void DivRemOneOrMinusOne(HBinaryOperation* instruction); |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 214 | void DivByPowerOfTwo(HDiv* instruction); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 215 | void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 216 | void GenerateDivRemIntegral(HBinaryOperation* instruction); |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 217 | void HandleCondition(HCondition* condition); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 218 | void HandleShift(HBinaryOperation* operation); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 219 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 220 | void HandleFieldSet(HInstruction* instruction, |
| 221 | const FieldInfo& field_info, |
| 222 | bool value_can_be_null); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 223 | void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 224 | |
| 225 | // Generate a heap reference load using one register `out`: |
| 226 | // |
| 227 | // out <- *(out + offset) |
| 228 | // |
| 229 | // while honoring heap poisoning and/or read barriers (if any). |
| 230 | // Register `temp` is used when generating a read barrier. |
| 231 | void GenerateReferenceLoadOneRegister(HInstruction* instruction, |
| 232 | Location out, |
| 233 | uint32_t offset, |
| 234 | Location temp); |
| 235 | // Generate a heap reference load using two different registers |
| 236 | // `out` and `obj`: |
| 237 | // |
| 238 | // out <- *(obj + offset) |
| 239 | // |
| 240 | // while honoring heap poisoning and/or read barriers (if any). |
| 241 | // Register `temp` is used when generating a Baker's read barrier. |
| 242 | void GenerateReferenceLoadTwoRegisters(HInstruction* instruction, |
| 243 | Location out, |
| 244 | Location obj, |
| 245 | uint32_t offset, |
| 246 | Location temp); |
| 247 | // Generate a GC root reference load: |
| 248 | // |
| 249 | // root <- *(obj + offset) |
| 250 | // |
| 251 | // while honoring read barriers (if any). |
| 252 | void GenerateGcRootFieldLoad(HInstruction* instruction, |
| 253 | Location root, |
| 254 | CpuRegister obj, |
| 255 | uint32_t offset); |
| 256 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 257 | void GenerateImplicitNullCheck(HNullCheck* instruction); |
| 258 | void GenerateExplicitNullCheck(HNullCheck* instruction); |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 259 | void PushOntoFPStack(Location source, uint32_t temp_offset, |
| 260 | uint32_t stack_adjustment, bool is_float); |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame^] | 261 | template<class LabelType> |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 262 | void GenerateTestAndBranch(HInstruction* instruction, |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 263 | size_t condition_input_index, |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame^] | 264 | LabelType* true_target, |
| 265 | LabelType* false_target); |
| 266 | template<class LabelType> |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 267 | void GenerateCompareTestAndBranch(HCondition* condition, |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame^] | 268 | LabelType* true_target, |
| 269 | LabelType* false_target); |
| 270 | template<class LabelType> |
| 271 | void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label); |
| 272 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 273 | void HandleGoto(HInstruction* got, HBasicBlock* successor); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 274 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 275 | X86_64Assembler* const assembler_; |
| 276 | CodeGeneratorX86_64* const codegen_; |
| 277 | |
| 278 | DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64); |
| 279 | }; |
| 280 | |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 281 | // Class for fixups to jump tables. |
| 282 | class JumpTableRIPFixup; |
| 283 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 284 | class CodeGeneratorX86_64 : public CodeGenerator { |
| 285 | public: |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 286 | CodeGeneratorX86_64(HGraph* graph, |
| 287 | const X86_64InstructionSetFeatures& isa_features, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 288 | const CompilerOptions& compiler_options, |
| 289 | OptimizingCompilerStats* stats = nullptr); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 290 | virtual ~CodeGeneratorX86_64() {} |
| 291 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 292 | void GenerateFrameEntry() OVERRIDE; |
| 293 | void GenerateFrameExit() OVERRIDE; |
| 294 | void Bind(HBasicBlock* block) OVERRIDE; |
| 295 | void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE; |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 296 | void MoveConstant(Location destination, int32_t value) OVERRIDE; |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 297 | void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE; |
| 298 | void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE; |
| 299 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 300 | size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
| 301 | size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
| 302 | size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
| 303 | size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 304 | |
Alexandre Rames | 8158f28 | 2015-08-07 10:26:17 +0100 | [diff] [blame] | 305 | // Generate code to invoke a runtime entry point. |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 306 | void InvokeRuntime(QuickEntrypointEnum entrypoint, |
| 307 | HInstruction* instruction, |
| 308 | uint32_t dex_pc, |
| 309 | SlowPathCode* slow_path) OVERRIDE; |
| 310 | |
| 311 | void InvokeRuntime(int32_t entry_point_offset, |
Alexandre Rames | 8158f28 | 2015-08-07 10:26:17 +0100 | [diff] [blame] | 312 | HInstruction* instruction, |
| 313 | uint32_t dex_pc, |
| 314 | SlowPathCode* slow_path); |
| 315 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 316 | size_t GetWordSize() const OVERRIDE { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 317 | return kX86_64WordSize; |
| 318 | } |
| 319 | |
Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 320 | size_t GetFloatingPointSpillSlotSize() const OVERRIDE { |
| 321 | return kX86_64WordSize; |
| 322 | } |
| 323 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 324 | HGraphVisitor* GetLocationBuilder() OVERRIDE { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 325 | return &location_builder_; |
| 326 | } |
| 327 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 328 | HGraphVisitor* GetInstructionVisitor() OVERRIDE { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 329 | return &instruction_visitor_; |
| 330 | } |
| 331 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 332 | X86_64Assembler* GetAssembler() OVERRIDE { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 333 | return &assembler_; |
| 334 | } |
| 335 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 336 | const X86_64Assembler& GetAssembler() const OVERRIDE { |
| 337 | return assembler_; |
| 338 | } |
| 339 | |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 340 | ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 341 | return &move_resolver_; |
| 342 | } |
| 343 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 344 | uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE { |
| 345 | return GetLabelOf(block)->Position(); |
| 346 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 347 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 348 | Location GetStackLocation(HLoadLocal* load) const OVERRIDE; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 349 | |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 350 | void SetupBlockedRegisters(bool is_baseline) const OVERRIDE; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 351 | Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE; |
| 352 | void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE; |
| 353 | void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE; |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 354 | void Finalize(CodeAllocator* allocator) OVERRIDE; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 355 | |
| 356 | InstructionSet GetInstructionSet() const OVERRIDE { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 357 | return InstructionSet::kX86_64; |
| 358 | } |
| 359 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 360 | // Emit a write barrier. |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 361 | void MarkGCCard(CpuRegister temp, |
| 362 | CpuRegister card, |
| 363 | CpuRegister object, |
| 364 | CpuRegister value, |
| 365 | bool value_can_be_null); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 366 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 367 | void GenerateMemoryBarrier(MemBarrierKind kind); |
| 368 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 369 | // Helper method to move a value between two locations. |
| 370 | void Move(Location destination, Location source); |
| 371 | |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 372 | Label* GetLabelOf(HBasicBlock* block) const { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 373 | return CommonGetLabelOf<Label>(block_labels_, block); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 374 | } |
| 375 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 376 | void Initialize() OVERRIDE { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 377 | block_labels_ = CommonInitializeLabels<Label>(); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 380 | bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE { |
| 381 | return false; |
| 382 | } |
| 383 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 384 | // Check if the desired_dispatch_info is supported. If it is, return it, |
| 385 | // otherwise return a fall-back info that should be used instead. |
| 386 | HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch( |
| 387 | const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info, |
| 388 | MethodReference target_method) OVERRIDE; |
| 389 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 390 | void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE; |
| 391 | void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE; |
| 392 | |
| 393 | void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 394 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 395 | void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE; |
| 396 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 397 | const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const { |
| 398 | return isa_features_; |
| 399 | } |
| 400 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 401 | // Fast path implementation of ReadBarrier::Barrier for a heap |
| 402 | // reference field load when Baker's read barriers are used. |
| 403 | void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction, |
| 404 | Location out, |
| 405 | CpuRegister obj, |
| 406 | uint32_t offset, |
| 407 | Location temp, |
| 408 | bool needs_null_check); |
| 409 | // Fast path implementation of ReadBarrier::Barrier for a heap |
| 410 | // reference array load when Baker's read barriers are used. |
| 411 | void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction, |
| 412 | Location out, |
| 413 | CpuRegister obj, |
| 414 | uint32_t data_offset, |
| 415 | Location index, |
| 416 | Location temp, |
| 417 | bool needs_null_check); |
| 418 | |
| 419 | // Generate a read barrier for a heap reference within `instruction` |
| 420 | // using a slow path. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 421 | // |
| 422 | // A read barrier for an object reference read from the heap is |
| 423 | // implemented as a call to the artReadBarrierSlow runtime entry |
| 424 | // point, which is passed the values in locations `ref`, `obj`, and |
| 425 | // `offset`: |
| 426 | // |
| 427 | // mirror::Object* artReadBarrierSlow(mirror::Object* ref, |
| 428 | // mirror::Object* obj, |
| 429 | // uint32_t offset); |
| 430 | // |
| 431 | // The `out` location contains the value returned by |
| 432 | // artReadBarrierSlow. |
| 433 | // |
| 434 | // When `index` provided (i.e., when it is different from |
| 435 | // Location::NoLocation()), the offset value passed to |
| 436 | // artReadBarrierSlow is adjusted to take `index` into account. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 437 | void GenerateReadBarrierSlow(HInstruction* instruction, |
| 438 | Location out, |
| 439 | Location ref, |
| 440 | Location obj, |
| 441 | uint32_t offset, |
| 442 | Location index = Location::NoLocation()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 443 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 444 | // If read barriers are enabled, generate a read barrier for a heap |
| 445 | // reference using a slow path. If heap poisoning is enabled, also |
| 446 | // unpoison the reference in `out`. |
| 447 | void MaybeGenerateReadBarrierSlow(HInstruction* instruction, |
| 448 | Location out, |
| 449 | Location ref, |
| 450 | Location obj, |
| 451 | uint32_t offset, |
| 452 | Location index = Location::NoLocation()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 453 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 454 | // Generate a read barrier for a GC root within `instruction` using |
| 455 | // a slow path. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 456 | // |
| 457 | // A read barrier for an object reference GC root is implemented as |
| 458 | // a call to the artReadBarrierForRootSlow runtime entry point, |
| 459 | // which is passed the value in location `root`: |
| 460 | // |
| 461 | // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root); |
| 462 | // |
| 463 | // The `out` location contains the value returned by |
| 464 | // artReadBarrierForRootSlow. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 465 | void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 466 | |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 467 | int ConstantAreaStart() const { |
| 468 | return constant_area_start_; |
| 469 | } |
| 470 | |
| 471 | Address LiteralDoubleAddress(double v); |
| 472 | Address LiteralFloatAddress(float v); |
| 473 | Address LiteralInt32Address(int32_t v); |
| 474 | Address LiteralInt64Address(int64_t v); |
| 475 | |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 476 | // Load a 64 bit value into a register in the most efficient manner. |
| 477 | void Load64BitValue(CpuRegister dest, int64_t value); |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 478 | Address LiteralCaseTable(HPackedSwitch* switch_instr); |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 479 | |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 480 | // Store a 64 bit value into a DoubleStackSlot in the most efficient manner. |
| 481 | void Store64BitValueToStack(Location dest, int64_t value); |
| 482 | |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 483 | // Assign a 64 bit constant to an address. |
| 484 | void MoveInt64ToAddress(const Address& addr_low, |
| 485 | const Address& addr_high, |
| 486 | int64_t v, |
| 487 | HInstruction* instruction); |
| 488 | |
Mark P Mendell | 17077d8 | 2015-12-16 19:15:59 +0000 | [diff] [blame] | 489 | // Ensure that prior stores complete to memory before subsequent loads. |
| 490 | // The locked add implementation will avoid serializing device memory, but will |
| 491 | // touch (but not change) the top of the stack. The locked add should not be used for |
| 492 | // ordering non-temporal stores. |
| 493 | void MemoryFence(bool force_mfence = false) { |
| 494 | if (!force_mfence && isa_features_.PrefersLockedAddSynchronization()) { |
| 495 | assembler_.lock()->addl(Address(CpuRegister(RSP), 0), Immediate(0)); |
| 496 | } else { |
| 497 | assembler_.mfence(); |
| 498 | } |
| 499 | } |
| 500 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 501 | private: |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 502 | // Factored implementation of GenerateFieldLoadWithBakerReadBarrier |
| 503 | // and GenerateArrayLoadWithBakerReadBarrier. |
| 504 | void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction, |
| 505 | Location ref, |
| 506 | CpuRegister obj, |
| 507 | const Address& src, |
| 508 | Location temp, |
| 509 | bool needs_null_check); |
| 510 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 511 | struct PcRelativeDexCacheAccessInfo { |
| 512 | PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off) |
| 513 | : target_dex_file(dex_file), element_offset(element_off), label() { } |
| 514 | |
| 515 | const DexFile& target_dex_file; |
| 516 | uint32_t element_offset; |
| 517 | Label label; |
| 518 | }; |
| 519 | |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 520 | // Labels for each block that will be compiled. |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 521 | Label* block_labels_; // Indexed by block id. |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 522 | Label frame_entry_label_; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 523 | LocationsBuilderX86_64 location_builder_; |
| 524 | InstructionCodeGeneratorX86_64 instruction_visitor_; |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 525 | ParallelMoveResolverX86_64 move_resolver_; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 526 | X86_64Assembler assembler_; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 527 | const X86_64InstructionSetFeatures& isa_features_; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 528 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 529 | // Offset to the start of the constant area in the assembled code. |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 530 | // Used for fixups to the constant area. |
| 531 | int constant_area_start_; |
| 532 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 533 | // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back(). |
| 534 | ArenaDeque<MethodPatchInfo<Label>> method_patches_; |
| 535 | ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_; |
| 536 | // PC-relative DexCache access info. |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 537 | ArenaDeque<PcRelativeDexCacheAccessInfo> pc_relative_dex_cache_patches_; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 538 | |
| 539 | // When we don't know the proper offset for the value, we use kDummy32BitOffset. |
| 540 | // We will fix this up in the linker later to have the right value. |
| 541 | static constexpr int32_t kDummy32BitOffset = 256; |
| 542 | |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 543 | // Fixups for jump tables need to be handled specially. |
| 544 | ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_; |
| 545 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 546 | DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64); |
| 547 | }; |
| 548 | |
| 549 | } // namespace x86_64 |
| 550 | } // namespace art |
| 551 | |
| 552 | #endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_ |