blob: d7cfd37c33ecea70de7334eea19e93aeb9cd5dfc [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 Juravle52c48962014-12-16 17:02:57 +000022#include "dex/compiler_enums.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000023#include "driver/compiler_options.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "nodes.h"
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000025#include "parallel_move_resolver.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "utils/x86_64/assembler_x86_64.h"
27
28namespace art {
29namespace x86_64 {
30
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000031// Use a local definition to prevent copying mistakes.
32static constexpr size_t kX86_64WordSize = kX86_64PointerSize;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +010034// Some x86_64 instructions require a register to be available as temp.
35static constexpr Register TMP = R11;
36
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010037static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010038static constexpr FloatRegister kParameterFloatRegisters[] =
39 { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040
41static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010043
Jeff Hao848f70a2014-01-15 13:49:50 -080044static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX };
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000045static constexpr size_t kRuntimeParameterCoreRegistersLength =
46 arraysize(kRuntimeParameterCoreRegisters);
47static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
48static constexpr size_t kRuntimeParameterFpuRegistersLength =
49 arraysize(kRuntimeParameterFpuRegisters);
50
Mark Mendella4f12202015-08-06 15:23:34 -040051// 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).
54static constexpr FloatRegister non_volatile_xmm_regs[] = { XMM12, XMM13, XMM14, XMM15 };
55
56
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000057class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
58 public:
59 InvokeRuntimeCallingConvention()
60 : CallingConvention(kRuntimeParameterCoreRegisters,
61 kRuntimeParameterCoreRegistersLength,
62 kRuntimeParameterFpuRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 kRuntimeParameterFpuRegistersLength,
64 kX86_64PointerSize) {}
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000065
66 private:
67 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
68};
69
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010070class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010071 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010072 InvokeDexCallingConvention() : CallingConvention(
73 kParameterCoreRegisters,
74 kParameterCoreRegistersLength,
75 kParameterFloatRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070076 kParameterFloatRegistersLength,
77 kX86_64PointerSize) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010078
79 private:
80 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
81};
82
Calin Juravlee460d1d2015-09-29 04:52:17 +010083class 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 Levillain2d27c8e2015-04-28 15:48:45 +0100112class InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100113 public:
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100114 InvokeDexCallingConventionVisitorX86_64() {}
115 virtual ~InvokeDexCallingConventionVisitorX86_64() {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100116
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100117 Location GetNextLocation(Primitive::Type type) OVERRIDE;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100118 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
119 Location GetMethodLocation() const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100120
121 private:
122 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100123
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100124 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100125};
126
127class CodeGeneratorX86_64;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800128
Zheng Xuad4450e2015-04-17 18:48:56 +0800129class ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000130 public:
131 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
Zheng Xuad4450e2015-04-17 18:48:56 +0800132 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000133
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000134 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 Geoffrayecb2f9b2014-06-13 08:59:59 +0000138
139 X86_64Assembler* GetAssembler() const;
140
141 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100142 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100143 void Exchange32(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100144 void Exchange32(int mem1, int mem2);
Mark Mendell8a1c7282015-06-29 15:41:28 -0400145 void Exchange64(CpuRegister reg1, CpuRegister reg2);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100146 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100147 void Exchange64(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100148 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000149
150 CodeGeneratorX86_64* const codegen_;
151
152 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
153};
154
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100155class LocationsBuilderX86_64 : public HGraphVisitor {
156 public:
157 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
158 : HGraphVisitor(graph), codegen_(codegen) {}
159
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100160#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000161 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100162
Alexandre Ramesef20f712015-06-09 10:29:30 +0100163 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
164 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100165
166#undef DECLARE_VISIT_INSTRUCTION
167
Alexandre Ramesef20f712015-06-09 10:29:30 +0100168 void VisitInstruction(HInstruction* instruction) OVERRIDE {
169 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
170 << " (id " << instruction->GetId() << ")";
171 }
172
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100173 private:
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000174 void HandleInvoke(HInvoke* invoke);
175 void HandleBitwiseOperation(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000176 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000177 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000178 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
179 void HandleFieldGet(HInstruction* instruction);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000180
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100181 CodeGeneratorX86_64* const codegen_;
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100182 InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100183
184 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
185};
186
Aart Bik42249c32016-01-07 15:33:50 -0800187class InstructionCodeGeneratorX86_64 : public InstructionCodeGenerator {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100188 public:
189 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
190
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100191#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000192 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100193
Alexandre Ramesef20f712015-06-09 10:29:30 +0100194 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
195 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100196
197#undef DECLARE_VISIT_INSTRUCTION
198
Alexandre Ramesef20f712015-06-09 10:29:30 +0100199 void VisitInstruction(HInstruction* instruction) OVERRIDE {
200 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
201 << " (id " << instruction->GetId() << ")";
202 }
203
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100204 X86_64Assembler* GetAssembler() const { return assembler_; }
205
206 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100207 // Generate code for the given suspend check. If not null, `successor`
208 // is the block to branch to if the suspend check is not needed, and after
209 // the suspend call.
210 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700211 void GenerateClassInitializationCheck(SlowPathCode* slow_path, CpuRegister class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000212 void HandleBitwiseOperation(HBinaryOperation* operation);
Mark Mendellc4701932015-04-10 13:18:51 -0400213 void GenerateRemFP(HRem* rem);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100214 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
Guillaume Sanchezb19930c2015-04-09 21:12:15 +0100215 void DivByPowerOfTwo(HDiv* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100216 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
Calin Juravlebacfec32014-11-14 15:54:36 +0000217 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000218 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000219 void HandleShift(HBinaryOperation* operation);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000220
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100221 void HandleFieldSet(HInstruction* instruction,
222 const FieldInfo& field_info,
223 bool value_can_be_null);
Calin Juravle52c48962014-12-16 17:02:57 +0000224 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000225
226 // Generate a heap reference load using one register `out`:
227 //
228 // out <- *(out + offset)
229 //
230 // while honoring heap poisoning and/or read barriers (if any).
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000231 //
232 // Location `maybe_temp` is used when generating a read barrier and
233 // shall be a register in that case; it may be an invalid location
234 // otherwise.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000235 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
236 Location out,
237 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000238 Location maybe_temp);
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,
252 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000253 Location maybe_temp);
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 //
258 // while honoring read barriers (if any).
259 void GenerateGcRootFieldLoad(HInstruction* instruction,
260 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000261 const Address& address,
262 Label* fixup_label = nullptr);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000263
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500264 void PushOntoFPStack(Location source, uint32_t temp_offset,
265 uint32_t stack_adjustment, bool is_float);
Mark Mendell7c0b44f2016-02-01 10:08:35 -0500266 void GenerateCompareTest(HCondition* condition);
Mark Mendell152408f2015-12-31 12:28:50 -0500267 template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700268 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000269 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -0500270 LabelType* true_target,
271 LabelType* false_target);
272 template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +0000273 void GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -0500274 LabelType* true_target,
275 LabelType* false_target);
276 template<class LabelType>
277 void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
278
David Brazdilfc6a86a2015-06-26 10:33:45 +0000279 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100281 X86_64Assembler* const assembler_;
282 CodeGeneratorX86_64* const codegen_;
283
284 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
285};
286
Mark Mendell9c86b482015-09-18 13:36:07 -0400287// Class for fixups to jump tables.
288class JumpTableRIPFixup;
289
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100290class CodeGeneratorX86_64 : public CodeGenerator {
291 public:
Mark Mendellfb8d2792015-03-31 22:16:59 -0400292 CodeGeneratorX86_64(HGraph* graph,
293 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100294 const CompilerOptions& compiler_options,
295 OptimizingCompilerStats* stats = nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100296 virtual ~CodeGeneratorX86_64() {}
297
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000298 void GenerateFrameEntry() OVERRIDE;
299 void GenerateFrameExit() OVERRIDE;
300 void Bind(HBasicBlock* block) OVERRIDE;
Calin Juravle175dc732015-08-25 15:42:32 +0100301 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100302 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
303 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
304
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000305 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
306 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
307 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
308 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100309
Alexandre Rames8158f282015-08-07 10:26:17 +0100310 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100311 void InvokeRuntime(QuickEntrypointEnum entrypoint,
312 HInstruction* instruction,
313 uint32_t dex_pc,
314 SlowPathCode* slow_path) OVERRIDE;
315
316 void InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100317 HInstruction* instruction,
318 uint32_t dex_pc,
319 SlowPathCode* slow_path);
320
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000321 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100322 return kX86_64WordSize;
323 }
324
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500325 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
326 return kX86_64WordSize;
327 }
328
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000329 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100330 return &location_builder_;
331 }
332
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000333 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100334 return &instruction_visitor_;
335 }
336
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000337 X86_64Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100338 return &assembler_;
339 }
340
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100341 const X86_64Assembler& GetAssembler() const OVERRIDE {
342 return assembler_;
343 }
344
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000346 return &move_resolver_;
347 }
348
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100349 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000350 return GetLabelOf(block)->Position();
351 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100352
David Brazdil58282f42016-01-14 12:45:10 +0000353 void SetupBlockedRegisters() const OVERRIDE;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000354 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
355 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Mark Mendellf55c3e02015-03-26 21:07:46 -0400356 void Finalize(CodeAllocator* allocator) OVERRIDE;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000357
358 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100359 return InstructionSet::kX86_64;
360 }
361
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100362 // Emit a write barrier.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100363 void MarkGCCard(CpuRegister temp,
364 CpuRegister card,
365 CpuRegister object,
366 CpuRegister value,
367 bool value_can_be_null);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100368
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000369 void GenerateMemoryBarrier(MemBarrierKind kind);
370
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100371 // Helper method to move a value between two locations.
372 void Move(Location destination, Location source);
373
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100374 Label* GetLabelOf(HBasicBlock* block) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100375 return CommonGetLabelOf<Label>(block_labels_, block);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 }
377
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000378 void Initialize() OVERRIDE {
Vladimir Marko225b6462015-09-28 12:17:40 +0100379 block_labels_ = CommonInitializeLabels<Label>();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100380 }
381
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000382 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
383 return false;
384 }
385
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000386 // Check if the desired_string_load_kind is supported. If it is, return it,
387 // otherwise return a fall-back kind that should be used instead.
388 HLoadString::LoadKind GetSupportedLoadStringKind(
389 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
390
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100391 // Check if the desired_class_load_kind is supported. If it is, return it,
392 // otherwise return a fall-back kind that should be used instead.
393 HLoadClass::LoadKind GetSupportedLoadClassKind(
394 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
395
Vladimir Markodc151b22015-10-15 18:02:30 +0100396 // Check if the desired_dispatch_info is supported. If it is, return it,
397 // otherwise return a fall-back info that should be used instead.
398 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
399 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
400 MethodReference target_method) OVERRIDE;
401
Serguei Katkov288c7a82016-05-16 11:53:15 +0600402 Location GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700403 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
404 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
405
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000406 void RecordSimplePatch();
407 void RecordStringPatch(HLoadString* load_string);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100408 void RecordTypePatch(HLoadClass* load_class);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000409 Label* NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file, uint32_t element_offset);
410
Andreas Gampe85b62f22015-09-09 13:15:38 -0700411 void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800412
Vladimir Marko58155012015-08-19 12:49:41 +0000413 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
414
Mark Mendellfb8d2792015-03-31 22:16:59 -0400415 const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
416 return isa_features_;
417 }
418
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000419 // Fast path implementation of ReadBarrier::Barrier for a heap
420 // reference field load when Baker's read barriers are used.
421 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000422 Location ref,
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000423 CpuRegister obj,
424 uint32_t offset,
425 Location temp,
426 bool needs_null_check);
427 // Fast path implementation of ReadBarrier::Barrier for a heap
428 // reference array load when Baker's read barriers are used.
429 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000430 Location ref,
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000431 CpuRegister obj,
432 uint32_t data_offset,
433 Location index,
434 Location temp,
435 bool needs_null_check);
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +0800436 // Factored implementation used by GenerateFieldLoadWithBakerReadBarrier
437 // and GenerateArrayLoadWithBakerReadBarrier.
438 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
439 Location ref,
440 CpuRegister obj,
441 const Address& src,
442 Location temp,
443 bool needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000444
445 // Generate a read barrier for a heap reference within `instruction`
446 // using a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000447 //
448 // A read barrier for an object reference read from the heap is
449 // implemented as a call to the artReadBarrierSlow runtime entry
450 // point, which is passed the values in locations `ref`, `obj`, and
451 // `offset`:
452 //
453 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
454 // mirror::Object* obj,
455 // uint32_t offset);
456 //
457 // The `out` location contains the value returned by
458 // artReadBarrierSlow.
459 //
460 // When `index` provided (i.e., when it is different from
461 // Location::NoLocation()), the offset value passed to
462 // artReadBarrierSlow is adjusted to take `index` into account.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000463 void GenerateReadBarrierSlow(HInstruction* instruction,
464 Location out,
465 Location ref,
466 Location obj,
467 uint32_t offset,
468 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000469
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000470 // If read barriers are enabled, generate a read barrier for a heap
471 // reference using a slow path. If heap poisoning is enabled, also
472 // unpoison the reference in `out`.
473 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
474 Location out,
475 Location ref,
476 Location obj,
477 uint32_t offset,
478 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000479
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000480 // Generate a read barrier for a GC root within `instruction` using
481 // a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000482 //
483 // A read barrier for an object reference GC root is implemented as
484 // a call to the artReadBarrierForRootSlow runtime entry point,
485 // which is passed the value in location `root`:
486 //
487 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
488 //
489 // The `out` location contains the value returned by
490 // artReadBarrierForRootSlow.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000491 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000492
Mark Mendellf55c3e02015-03-26 21:07:46 -0400493 int ConstantAreaStart() const {
494 return constant_area_start_;
495 }
496
497 Address LiteralDoubleAddress(double v);
498 Address LiteralFloatAddress(float v);
499 Address LiteralInt32Address(int32_t v);
500 Address LiteralInt64Address(int64_t v);
501
Aart Bika19616e2016-02-01 18:57:58 -0800502 // Load a 32/64-bit value into a register in the most efficient manner.
Aart Bikc5d47542016-01-27 17:00:35 -0800503 void Load32BitValue(CpuRegister dest, int32_t value);
Mark Mendell92e83bf2015-05-07 11:25:03 -0400504 void Load64BitValue(CpuRegister dest, int64_t value);
Mark Mendell7c0b44f2016-02-01 10:08:35 -0500505 void Load32BitValue(XmmRegister dest, int32_t value);
506 void Load64BitValue(XmmRegister dest, int64_t value);
507 void Load32BitValue(XmmRegister dest, float value);
508 void Load64BitValue(XmmRegister dest, double value);
Aart Bikc5d47542016-01-27 17:00:35 -0800509
Aart Bika19616e2016-02-01 18:57:58 -0800510 // Compare a register with a 32/64-bit value in the most efficient manner.
511 void Compare32BitValue(CpuRegister dest, int32_t value);
512 void Compare64BitValue(CpuRegister dest, int64_t value);
513
Mark Mendell9c86b482015-09-18 13:36:07 -0400514 Address LiteralCaseTable(HPackedSwitch* switch_instr);
Mark Mendell92e83bf2015-05-07 11:25:03 -0400515
Mark Mendellcfa410b2015-05-25 16:02:44 -0400516 // Store a 64 bit value into a DoubleStackSlot in the most efficient manner.
517 void Store64BitValueToStack(Location dest, int64_t value);
518
Mark Mendellea5af682015-10-22 17:35:49 -0400519 // Assign a 64 bit constant to an address.
520 void MoveInt64ToAddress(const Address& addr_low,
521 const Address& addr_high,
522 int64_t v,
523 HInstruction* instruction);
524
Mark P Mendell17077d82015-12-16 19:15:59 +0000525 // Ensure that prior stores complete to memory before subsequent loads.
526 // The locked add implementation will avoid serializing device memory, but will
Mark Mendell7aa04a12016-01-27 22:39:07 -0500527 // touch (but not change) the top of the stack.
528 // The 'non_temporal' parameter should be used to ensure ordering of non-temporal stores.
Mark P Mendell17077d82015-12-16 19:15:59 +0000529 void MemoryFence(bool force_mfence = false) {
Mark Mendell7aa04a12016-01-27 22:39:07 -0500530 if (!force_mfence) {
Mark P Mendell17077d82015-12-16 19:15:59 +0000531 assembler_.lock()->addl(Address(CpuRegister(RSP), 0), Immediate(0));
532 } else {
533 assembler_.mfence();
534 }
535 }
536
David Srbeckyc7098ff2016-02-09 14:30:11 +0000537 void GenerateNop();
Calin Juravle2ae48182016-03-16 14:05:09 +0000538 void GenerateImplicitNullCheck(HNullCheck* instruction);
539 void GenerateExplicitNullCheck(HNullCheck* instruction);
David Srbeckyc7098ff2016-02-09 14:30:11 +0000540
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000541 // When we don't know the proper offset for the value, we use kDummy32BitOffset.
542 // We will fix this up in the linker later to have the right value.
543 static constexpr int32_t kDummy32BitOffset = 256;
544
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100545 private:
Vladimir Marko58155012015-08-19 12:49:41 +0000546 struct PcRelativeDexCacheAccessInfo {
547 PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off)
548 : target_dex_file(dex_file), element_offset(element_off), label() { }
549
550 const DexFile& target_dex_file;
551 uint32_t element_offset;
552 Label label;
553 };
554
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100555 // Labels for each block that will be compiled.
Vladimir Marko225b6462015-09-28 12:17:40 +0100556 Label* block_labels_; // Indexed by block id.
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000557 Label frame_entry_label_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558 LocationsBuilderX86_64 location_builder_;
559 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000560 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100561 X86_64Assembler assembler_;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400562 const X86_64InstructionSetFeatures& isa_features_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563
Mark Mendell39dcf552015-04-09 20:42:42 -0400564 // Offset to the start of the constant area in the assembled code.
Mark Mendellf55c3e02015-03-26 21:07:46 -0400565 // Used for fixups to the constant area.
566 int constant_area_start_;
567
Vladimir Marko58155012015-08-19 12:49:41 +0000568 // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
569 ArenaDeque<MethodPatchInfo<Label>> method_patches_;
570 ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_;
571 // PC-relative DexCache access info.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000572 ArenaDeque<PcRelativeDexCacheAccessInfo> pc_relative_dex_cache_patches_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000573 // Patch locations for patchoat where the linker doesn't do any other work.
574 ArenaDeque<Label> simple_patches_;
575 // String patch locations.
576 ArenaDeque<StringPatchInfo<Label>> string_patches_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100577 // Type patch locations.
578 ArenaDeque<TypePatchInfo<Label>> type_patches_;
Vladimir Marko58155012015-08-19 12:49:41 +0000579
Mark Mendell9c86b482015-09-18 13:36:07 -0400580 // Fixups for jump tables need to be handled specially.
581 ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_;
582
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
584};
585
586} // namespace x86_64
587} // namespace art
588
589#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_