blob: e86123ef01a2523dbc80c43b2c8015728afe2517 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
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 Mendell17077d82015-12-16 19:15:59 +000020#include "arch/x86_64/instruction_set_features_x86_64.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010021#include "code_generator.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000022#include "driver/compiler_options.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010023#include "nodes.h"
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000024#include "parallel_move_resolver.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "utils/x86_64/assembler_x86_64.h"
26
27namespace art {
28namespace x86_64 {
29
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000030// Use a local definition to prevent copying mistakes.
Andreas Gampe542451c2016-07-26 09:02:02 -070031static constexpr size_t kX86_64WordSize = static_cast<size_t>(kX86_64PointerSize);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +010033// Some x86_64 instructions require a register to be available as temp.
34static constexpr Register TMP = R11;
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010037static constexpr FloatRegister kParameterFloatRegisters[] =
38 { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010039
40static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010041static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010042
Jeff Hao848f70a2014-01-15 13:49:50 -080043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX };
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000044static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
46static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
47static constexpr size_t kRuntimeParameterFpuRegistersLength =
48 arraysize(kRuntimeParameterFpuRegisters);
49
Mark Mendella4f12202015-08-06 15:23:34 -040050// These XMM registers are non-volatile in ART ABI, but volatile in native ABI.
51// If the ART ABI changes, this list must be updated. It is used to ensure that
52// these are not clobbered by any direct call to native code (such as math intrinsics).
53static constexpr FloatRegister non_volatile_xmm_regs[] = { XMM12, XMM13, XMM14, XMM15 };
54
55
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000056class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
57 public:
58 InvokeRuntimeCallingConvention()
59 : CallingConvention(kRuntimeParameterCoreRegisters,
60 kRuntimeParameterCoreRegistersLength,
61 kRuntimeParameterFpuRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 kRuntimeParameterFpuRegistersLength,
63 kX86_64PointerSize) {}
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000064
65 private:
66 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
67};
68
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010069class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010070 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010071 InvokeDexCallingConvention() : CallingConvention(
72 kParameterCoreRegisters,
73 kParameterCoreRegistersLength,
74 kParameterFloatRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070075 kParameterFloatRegistersLength,
76 kX86_64PointerSize) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010077
78 private:
79 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
80};
81
Calin Juravlee460d1d2015-09-29 04:52:17 +010082class FieldAccessCallingConventionX86_64 : public FieldAccessCallingConvention {
83 public:
84 FieldAccessCallingConventionX86_64() {}
85
86 Location GetObjectLocation() const OVERRIDE {
87 return Location::RegisterLocation(RSI);
88 }
89 Location GetFieldIndexLocation() const OVERRIDE {
90 return Location::RegisterLocation(RDI);
91 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 Location GetReturnLocation(DataType::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravlee460d1d2015-09-29 04:52:17 +010093 return Location::RegisterLocation(RAX);
94 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 Location GetSetValueLocation(DataType::Type type ATTRIBUTE_UNUSED, bool is_instance)
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +000096 const OVERRIDE {
97 return is_instance
Calin Juravlee460d1d2015-09-29 04:52:17 +010098 ? Location::RegisterLocation(RDX)
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +000099 : Location::RegisterLocation(RSI);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100100 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100101 Location GetFpuLocation(DataType::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100102 return Location::FpuRegisterLocation(XMM0);
103 }
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionX86_64);
107};
108
109
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100110class InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100111 public:
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100112 InvokeDexCallingConventionVisitorX86_64() {}
113 virtual ~InvokeDexCallingConventionVisitorX86_64() {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100115 Location GetNextLocation(DataType::Type type) OVERRIDE;
116 Location GetReturnLocation(DataType::Type type) const OVERRIDE;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100117 Location GetMethodLocation() const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100118
119 private:
120 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100121
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100122 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100123};
124
125class CodeGeneratorX86_64;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800126
Zheng Xuad4450e2015-04-17 18:48:56 +0800127class ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000128 public:
129 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
Zheng Xuad4450e2015-04-17 18:48:56 +0800130 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000131
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000132 void EmitMove(size_t index) OVERRIDE;
133 void EmitSwap(size_t index) OVERRIDE;
134 void SpillScratch(int reg) OVERRIDE;
135 void RestoreScratch(int reg) OVERRIDE;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000136
137 X86_64Assembler* GetAssembler() const;
138
139 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100140 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100141 void Exchange32(XmmRegister reg, int mem);
Mark Mendell8a1c7282015-06-29 15:41:28 -0400142 void Exchange64(CpuRegister reg1, CpuRegister reg2);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100143 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100144 void Exchange64(XmmRegister reg, int mem);
Aart Bikcfe50bb2017-12-12 14:54:12 -0800145 void Exchange128(XmmRegister reg, int mem);
146 void ExchangeMemory32(int mem1, int mem2);
147 void ExchangeMemory64(int mem1, int mem2, int num_of_qwords);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000148
149 CodeGeneratorX86_64* const codegen_;
150
151 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
152};
153
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100154class LocationsBuilderX86_64 : public HGraphVisitor {
155 public:
156 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
157 : HGraphVisitor(graph), codegen_(codegen) {}
158
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100159#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000160 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100161
Alexandre Ramesef20f712015-06-09 10:29:30 +0100162 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
163 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100164
165#undef DECLARE_VISIT_INSTRUCTION
166
Alexandre Ramesef20f712015-06-09 10:29:30 +0100167 void VisitInstruction(HInstruction* instruction) OVERRIDE {
168 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
169 << " (id " << instruction->GetId() << ")";
170 }
171
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100172 private:
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000173 void HandleInvoke(HInvoke* invoke);
174 void HandleBitwiseOperation(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000175 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000176 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000177 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
178 void HandleFieldGet(HInstruction* instruction);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000179
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100180 CodeGeneratorX86_64* const codegen_;
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100181 InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100182
183 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
184};
185
Aart Bik42249c32016-01-07 15:33:50 -0800186class InstructionCodeGeneratorX86_64 : public InstructionCodeGenerator {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100187 public:
188 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
189
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100190#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000191 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100192
Alexandre Ramesef20f712015-06-09 10:29:30 +0100193 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
194 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100195
196#undef DECLARE_VISIT_INSTRUCTION
197
Alexandre Ramesef20f712015-06-09 10:29:30 +0100198 void VisitInstruction(HInstruction* instruction) OVERRIDE {
199 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
200 << " (id " << instruction->GetId() << ")";
201 }
202
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100203 X86_64Assembler* GetAssembler() const { return assembler_; }
204
205 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 // 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 Gampe85b62f22015-09-09 13:15:38 -0700210 void GenerateClassInitializationCheck(SlowPathCode* slow_path, CpuRegister class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000211 void HandleBitwiseOperation(HBinaryOperation* operation);
Mark Mendellc4701932015-04-10 13:18:51 -0400212 void GenerateRemFP(HRem* rem);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100213 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
Guillaume Sanchezb19930c2015-04-09 21:12:15 +0100214 void DivByPowerOfTwo(HDiv* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100215 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
Calin Juravlebacfec32014-11-14 15:54:36 +0000216 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000217 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000218 void HandleShift(HBinaryOperation* operation);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000219
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100220 void HandleFieldSet(HInstruction* instruction,
221 const FieldInfo& field_info,
222 bool value_can_be_null);
Calin Juravle52c48962014-12-16 17:02:57 +0000223 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000224
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).
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000230 //
231 // Location `maybe_temp` is used when generating a read barrier and
232 // shall be a register in that case; it may be an invalid location
233 // otherwise.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000234 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
235 Location out,
236 uint32_t offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -0800237 Location maybe_temp,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800238 ReadBarrierOption read_barrier_option);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000239 // Generate a heap reference load using two different registers
240 // `out` and `obj`:
241 //
242 // out <- *(obj + offset)
243 //
244 // while honoring heap poisoning and/or read barriers (if any).
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000245 //
246 // Location `maybe_temp` is used when generating a Baker's (fast
247 // path) read barrier and shall be a register in that case; it may
248 // be an invalid location otherwise.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000249 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
250 Location out,
251 Location obj,
Mathieu Chartiercdba73b2016-11-03 19:23:06 -0700252 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800253 ReadBarrierOption read_barrier_option);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000254 // Generate a GC root reference load:
255 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000256 // root <- *address
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000257 //
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800258 // while honoring read barriers based on read_barrier_option.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000259 void GenerateGcRootFieldLoad(HInstruction* instruction,
260 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000261 const Address& address,
Roland Levillain00468f32016-10-27 18:02:48 +0100262 Label* fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800263 ReadBarrierOption read_barrier_option);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000264
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500265 void PushOntoFPStack(Location source, uint32_t temp_offset,
266 uint32_t stack_adjustment, bool is_float);
Mark Mendell7c0b44f2016-02-01 10:08:35 -0500267 void GenerateCompareTest(HCondition* condition);
Mark Mendell152408f2015-12-31 12:28:50 -0500268 template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700269 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000270 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -0500271 LabelType* true_target,
272 LabelType* false_target);
273 template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +0000274 void GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -0500275 LabelType* true_target,
276 LabelType* false_target);
277 template<class LabelType>
278 void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
279
David Brazdilfc6a86a2015-06-26 10:33:45 +0000280 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100281
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100282 X86_64Assembler* const assembler_;
283 CodeGeneratorX86_64* const codegen_;
284
285 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
286};
287
Mark Mendell9c86b482015-09-18 13:36:07 -0400288// Class for fixups to jump tables.
289class JumpTableRIPFixup;
290
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100291class CodeGeneratorX86_64 : public CodeGenerator {
292 public:
Mark Mendellfb8d2792015-03-31 22:16:59 -0400293 CodeGeneratorX86_64(HGraph* graph,
294 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100295 const CompilerOptions& compiler_options,
296 OptimizingCompilerStats* stats = nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100297 virtual ~CodeGeneratorX86_64() {}
298
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000299 void GenerateFrameEntry() OVERRIDE;
300 void GenerateFrameExit() OVERRIDE;
301 void Bind(HBasicBlock* block) OVERRIDE;
Calin Juravle175dc732015-08-25 15:42:32 +0100302 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100303 void MoveLocation(Location dst, Location src, DataType::Type dst_type) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100304 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
305
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000306 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
307 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
308 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
309 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100310
Alexandre Rames8158f282015-08-07 10:26:17 +0100311 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100312 void InvokeRuntime(QuickEntrypointEnum entrypoint,
313 HInstruction* instruction,
314 uint32_t dex_pc,
Serban Constantinescuba45db02016-07-12 22:53:02 +0100315 SlowPathCode* slow_path = nullptr) OVERRIDE;
Alexandre Rames8158f282015-08-07 10:26:17 +0100316
Roland Levillaindec8f632016-07-22 17:10:06 +0100317 // Generate code to invoke a runtime entry point, but do not record
318 // PC-related information in a stack map.
319 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
320 HInstruction* instruction,
321 SlowPathCode* slow_path);
322
Serban Constantinescuba45db02016-07-12 22:53:02 +0100323 void GenerateInvokeRuntime(int32_t entry_point_offset);
324
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000325 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100326 return kX86_64WordSize;
327 }
328
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500329 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700330 return GetGraph()->HasSIMD()
331 ? 2 * kX86_64WordSize // 16 bytes == 2 x86_64 words for each spill
332 : 1 * kX86_64WordSize; // 8 bytes == 1 x86_64 words for each spill
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500333 }
334
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000335 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100336 return &location_builder_;
337 }
338
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000339 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100340 return &instruction_visitor_;
341 }
342
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000343 X86_64Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100344 return &assembler_;
345 }
346
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100347 const X86_64Assembler& GetAssembler() const OVERRIDE {
348 return assembler_;
349 }
350
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000351 ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000352 return &move_resolver_;
353 }
354
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100355 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000356 return GetLabelOf(block)->Position();
357 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100358
David Brazdil58282f42016-01-14 12:45:10 +0000359 void SetupBlockedRegisters() const OVERRIDE;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000360 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
361 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Mark Mendellf55c3e02015-03-26 21:07:46 -0400362 void Finalize(CodeAllocator* allocator) OVERRIDE;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000363
364 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100365 return InstructionSet::kX86_64;
366 }
367
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100368 // Emit a write barrier.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100369 void MarkGCCard(CpuRegister temp,
370 CpuRegister card,
371 CpuRegister object,
372 CpuRegister value,
373 bool value_can_be_null);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100374
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000375 void GenerateMemoryBarrier(MemBarrierKind kind);
376
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100377 // Helper method to move a value between two locations.
378 void Move(Location destination, Location source);
379
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100380 Label* GetLabelOf(HBasicBlock* block) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100381 return CommonGetLabelOf<Label>(block_labels_, block);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100382 }
383
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000384 void Initialize() OVERRIDE {
Vladimir Marko225b6462015-09-28 12:17:40 +0100385 block_labels_ = CommonInitializeLabels<Label>();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100386 }
387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100388 bool NeedsTwoRegisters(DataType::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000389 return false;
390 }
391
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000392 // Check if the desired_string_load_kind is supported. If it is, return it,
393 // otherwise return a fall-back kind that should be used instead.
394 HLoadString::LoadKind GetSupportedLoadStringKind(
395 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
396
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100397 // Check if the desired_class_load_kind is supported. If it is, return it,
398 // otherwise return a fall-back kind that should be used instead.
399 HLoadClass::LoadKind GetSupportedLoadClassKind(
400 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
401
Vladimir Markodc151b22015-10-15 18:02:30 +0100402 // Check if the desired_dispatch_info is supported. If it is, return it,
403 // otherwise return a fall-back info that should be used instead.
404 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
405 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100406 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100407
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100408 void GenerateStaticOrDirectCall(
409 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
410 void GenerateVirtualCall(
411 HInvokeVirtual* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
Andreas Gampe85b62f22015-09-09 13:15:38 -0700412
Vladimir Marko65979462017-05-19 17:25:12 +0100413 void RecordBootMethodPatch(HInvokeStaticOrDirect* invoke);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100414 Label* NewMethodBssEntryPatch(MethodReference target_method);
Vladimir Marko1998cd02017-01-13 13:02:58 +0000415 void RecordBootTypePatch(HLoadClass* load_class);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000416 Label* NewTypeBssEntryPatch(HLoadClass* load_class);
Vladimir Marko65979462017-05-19 17:25:12 +0100417 void RecordBootStringPatch(HLoadString* load_string);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000418 Label* NewStringBssEntryPatch(HLoadString* load_string);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000419 Label* NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +0100420 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000421 Handle<mirror::String> handle);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000422 Label* NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +0100423 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000424 Handle<mirror::Class> handle);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000425
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100426 void MoveFromReturnRegister(Location trg, DataType::Type type) OVERRIDE;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800427
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100428 void EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +0000429
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000430 void PatchJitRootUse(uint8_t* code,
431 const uint8_t* roots_data,
432 const PatchInfo<Label>& info,
433 uint64_t index_in_table) const;
434
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000435 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
436
Mark Mendellfb8d2792015-03-31 22:16:59 -0400437 const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
438 return isa_features_;
439 }
440
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000441 // Fast path implementation of ReadBarrier::Barrier for a heap
442 // reference field load when Baker's read barriers are used.
443 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000444 Location ref,
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000445 CpuRegister obj,
446 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000447 bool needs_null_check);
448 // Fast path implementation of ReadBarrier::Barrier for a heap
449 // reference array load when Baker's read barriers are used.
450 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000451 Location ref,
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000452 CpuRegister obj,
453 uint32_t data_offset,
454 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000455 bool needs_null_check);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100456 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
457 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
458 //
459 // Load the object reference located at address `src`, held by
460 // object `obj`, into `ref`, and mark it if needed. The base of
461 // address `src` must be `obj`.
462 //
463 // If `always_update_field` is true, the value of the reference is
464 // atomically updated in the holder (`obj`). This operation
465 // requires two temporary registers, which must be provided as
466 // non-null pointers (`temp1` and `temp2`).
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +0800467 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
468 Location ref,
469 CpuRegister obj,
470 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100471 bool needs_null_check,
472 bool always_update_field = false,
473 CpuRegister* temp1 = nullptr,
474 CpuRegister* temp2 = nullptr);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000475
476 // Generate a read barrier for a heap reference within `instruction`
477 // using a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000478 //
479 // A read barrier for an object reference read from the heap is
480 // implemented as a call to the artReadBarrierSlow runtime entry
481 // point, which is passed the values in locations `ref`, `obj`, and
482 // `offset`:
483 //
484 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
485 // mirror::Object* obj,
486 // uint32_t offset);
487 //
488 // The `out` location contains the value returned by
489 // artReadBarrierSlow.
490 //
491 // When `index` provided (i.e., when it is different from
492 // Location::NoLocation()), the offset value passed to
493 // artReadBarrierSlow is adjusted to take `index` into account.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000494 void GenerateReadBarrierSlow(HInstruction* instruction,
495 Location out,
496 Location ref,
497 Location obj,
498 uint32_t offset,
499 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000500
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000501 // If read barriers are enabled, generate a read barrier for a heap
502 // reference using a slow path. If heap poisoning is enabled, also
503 // unpoison the reference in `out`.
504 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
505 Location out,
506 Location ref,
507 Location obj,
508 uint32_t offset,
509 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000510
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000511 // Generate a read barrier for a GC root within `instruction` using
512 // a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000513 //
514 // A read barrier for an object reference GC root is implemented as
515 // a call to the artReadBarrierForRootSlow runtime entry point,
516 // which is passed the value in location `root`:
517 //
518 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
519 //
520 // The `out` location contains the value returned by
521 // artReadBarrierForRootSlow.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000522 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000523
Mark Mendellf55c3e02015-03-26 21:07:46 -0400524 int ConstantAreaStart() const {
525 return constant_area_start_;
526 }
527
528 Address LiteralDoubleAddress(double v);
529 Address LiteralFloatAddress(float v);
530 Address LiteralInt32Address(int32_t v);
531 Address LiteralInt64Address(int64_t v);
532
Aart Bika19616e2016-02-01 18:57:58 -0800533 // Load a 32/64-bit value into a register in the most efficient manner.
Aart Bikc5d47542016-01-27 17:00:35 -0800534 void Load32BitValue(CpuRegister dest, int32_t value);
Mark Mendell92e83bf2015-05-07 11:25:03 -0400535 void Load64BitValue(CpuRegister dest, int64_t value);
Mark Mendell7c0b44f2016-02-01 10:08:35 -0500536 void Load32BitValue(XmmRegister dest, int32_t value);
537 void Load64BitValue(XmmRegister dest, int64_t value);
538 void Load32BitValue(XmmRegister dest, float value);
539 void Load64BitValue(XmmRegister dest, double value);
Aart Bikc5d47542016-01-27 17:00:35 -0800540
Aart Bika19616e2016-02-01 18:57:58 -0800541 // Compare a register with a 32/64-bit value in the most efficient manner.
542 void Compare32BitValue(CpuRegister dest, int32_t value);
543 void Compare64BitValue(CpuRegister dest, int64_t value);
544
jessicahandojo4877b792016-09-08 19:49:13 -0700545 // Compare int values. Supports register locations for `lhs`.
Vladimir Marko56f4bdd2016-09-16 11:32:36 +0100546 void GenerateIntCompare(Location lhs, Location rhs);
jessicahandojo4877b792016-09-08 19:49:13 -0700547 void GenerateIntCompare(CpuRegister lhs, Location rhs);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +0100548
549 // Compare long values. Supports only register locations for `lhs`.
550 void GenerateLongCompare(Location lhs, Location rhs);
551
552 // Construct address for array access.
553 static Address ArrayAddress(CpuRegister obj,
554 Location index,
555 ScaleFactor scale,
556 uint32_t data_offset);
557
Mark Mendell9c86b482015-09-18 13:36:07 -0400558 Address LiteralCaseTable(HPackedSwitch* switch_instr);
Mark Mendell92e83bf2015-05-07 11:25:03 -0400559
Mark Mendellcfa410b2015-05-25 16:02:44 -0400560 // Store a 64 bit value into a DoubleStackSlot in the most efficient manner.
561 void Store64BitValueToStack(Location dest, int64_t value);
562
Mark Mendellea5af682015-10-22 17:35:49 -0400563 // Assign a 64 bit constant to an address.
564 void MoveInt64ToAddress(const Address& addr_low,
565 const Address& addr_high,
566 int64_t v,
567 HInstruction* instruction);
568
Mark P Mendell17077d82015-12-16 19:15:59 +0000569 // Ensure that prior stores complete to memory before subsequent loads.
570 // The locked add implementation will avoid serializing device memory, but will
Mark Mendell7aa04a12016-01-27 22:39:07 -0500571 // touch (but not change) the top of the stack.
572 // The 'non_temporal' parameter should be used to ensure ordering of non-temporal stores.
Mark P Mendell17077d82015-12-16 19:15:59 +0000573 void MemoryFence(bool force_mfence = false) {
Mark Mendell7aa04a12016-01-27 22:39:07 -0500574 if (!force_mfence) {
Mark P Mendell17077d82015-12-16 19:15:59 +0000575 assembler_.lock()->addl(Address(CpuRegister(RSP), 0), Immediate(0));
576 } else {
577 assembler_.mfence();
578 }
579 }
580
Roland Levillainf41f9562016-09-14 19:26:48 +0100581 void GenerateNop() OVERRIDE;
582 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
583 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
David Srbeckyc7098ff2016-02-09 14:30:11 +0000584
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000585 // When we don't know the proper offset for the value, we use kDummy32BitOffset.
586 // We will fix this up in the linker later to have the right value.
587 static constexpr int32_t kDummy32BitOffset = 256;
588
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100589 private:
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100590 template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +0000591 static void EmitPcRelativeLinkerPatches(const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100592 ArenaVector<linker::LinkerPatch>* linker_patches);
Vladimir Marko58155012015-08-19 12:49:41 +0000593
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100594 // Labels for each block that will be compiled.
Vladimir Marko225b6462015-09-28 12:17:40 +0100595 Label* block_labels_; // Indexed by block id.
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000596 Label frame_entry_label_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100597 LocationsBuilderX86_64 location_builder_;
598 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000599 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 X86_64Assembler assembler_;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400601 const X86_64InstructionSetFeatures& isa_features_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100602
Mark Mendell39dcf552015-04-09 20:42:42 -0400603 // Offset to the start of the constant area in the assembled code.
Mark Mendellf55c3e02015-03-26 21:07:46 -0400604 // Used for fixups to the constant area.
605 int constant_area_start_;
606
Vladimir Marko65979462017-05-19 17:25:12 +0100607 // PC-relative method patch info for kBootImageLinkTimePcRelative.
608 ArenaDeque<PatchInfo<Label>> boot_image_method_patches_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100609 // PC-relative method patch info for kBssEntry.
610 ArenaDeque<PatchInfo<Label>> method_bss_entry_patches_;
Vladimir Marko764d4542017-05-16 10:31:41 +0100611 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Vladimir Marko1998cd02017-01-13 13:02:58 +0000612 ArenaDeque<PatchInfo<Label>> boot_image_type_patches_;
613 // Type patch locations for kBssEntry.
614 ArenaDeque<PatchInfo<Label>> type_bss_entry_patches_;
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100615 // String patch locations; type depends on configuration (intern table or boot image PIC).
Vladimir Marko65979462017-05-19 17:25:12 +0100616 ArenaDeque<PatchInfo<Label>> string_patches_;
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100617 // String patch locations for kBssEntry.
618 ArenaDeque<PatchInfo<Label>> string_bss_entry_patches_;
Mark Mendell9c86b482015-09-18 13:36:07 -0400619
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000620 // Patches for string literals in JIT compiled code.
621 ArenaDeque<PatchInfo<Label>> jit_string_patches_;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000622 // Patches for class literals in JIT compiled code.
623 ArenaDeque<PatchInfo<Label>> jit_class_patches_;
624
Vladimir Marko65979462017-05-19 17:25:12 +0100625 // Fixups for jump tables need to be handled specially.
626 ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_;
627
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100628 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
629};
630
631} // namespace x86_64
632} // namespace art
633
634#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_