blob: 689f93e31a8da7084a5d3c9724618b9af4fa6c47 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_H_
19
Mark P Mendell17077d82015-12-16 19:15:59 +000020#include "arch/x86/instruction_set_features_x86.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000022#include "code_generator.h"
Andreas Gampe8a0128a2016-11-28 07:38:35 -080023#include "dex_file_types.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000024#include "driver/compiler_options.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "nodes.h"
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010026#include "parallel_move_resolver.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028
29namespace art {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace x86 {
31
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000032// Use a local definition to prevent copying mistakes.
Andreas Gampe542451c2016-07-26 09:02:02 -070033static constexpr size_t kX86WordSize = static_cast<size_t>(kX86PointerSize);
Nicolas Geoffray707c8092014-04-04 10:50:14 +010034
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035class CodeGeneratorX86;
36
Nicolas Geoffraya747a392014-04-17 14:56:23 +010037static constexpr Register kParameterCoreRegisters[] = { ECX, EDX, EBX };
38static constexpr RegisterPair kParameterCorePairRegisters[] = { ECX_EDX, EDX_EBX };
39static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000040static constexpr XmmRegister kParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
41static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
Nicolas Geoffraya747a392014-04-17 14:56:23 +010042
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000043static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
46static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
47static constexpr size_t kRuntimeParameterFpuRegistersLength =
48 arraysize(kRuntimeParameterFpuRegisters);
49
50class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
51 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
54 kRuntimeParameterCoreRegistersLength,
55 kRuntimeParameterFpuRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070056 kRuntimeParameterFpuRegistersLength,
57 kX86PointerSize) {}
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000058
59 private:
60 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
61};
62
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010063class InvokeDexCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffraya747a392014-04-17 14:56:23 +010064 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010065 InvokeDexCallingConvention() : CallingConvention(
66 kParameterCoreRegisters,
67 kParameterCoreRegistersLength,
68 kParameterFpuRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 kParameterFpuRegistersLength,
70 kX86PointerSize) {}
Nicolas Geoffraya747a392014-04-17 14:56:23 +010071
72 RegisterPair GetRegisterPairAt(size_t argument_index) {
73 DCHECK_LT(argument_index + 1, GetNumberOfRegisters());
74 return kParameterCorePairRegisters[argument_index];
75 }
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
79};
80
Roland Levillain2d27c8e2015-04-28 15:48:45 +010081class InvokeDexCallingConventionVisitorX86 : public InvokeDexCallingConventionVisitor {
Nicolas Geoffraya747a392014-04-17 14:56:23 +010082 public:
Roland Levillain2d27c8e2015-04-28 15:48:45 +010083 InvokeDexCallingConventionVisitorX86() {}
84 virtual ~InvokeDexCallingConventionVisitorX86() {}
Nicolas Geoffraya747a392014-04-17 14:56:23 +010085
Roland Levillain2d27c8e2015-04-28 15:48:45 +010086 Location GetNextLocation(Primitive::Type type) OVERRIDE;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +010087 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
88 Location GetMethodLocation() const OVERRIDE;
Nicolas Geoffraya747a392014-04-17 14:56:23 +010089
90 private:
91 InvokeDexCallingConvention calling_convention;
Nicolas Geoffraya747a392014-04-17 14:56:23 +010092
Roland Levillain2d27c8e2015-04-28 15:48:45 +010093 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86);
Nicolas Geoffraya747a392014-04-17 14:56:23 +010094};
95
Calin Juravlee460d1d2015-09-29 04:52:17 +010096class FieldAccessCallingConventionX86 : public FieldAccessCallingConvention {
97 public:
98 FieldAccessCallingConventionX86() {}
99
100 Location GetObjectLocation() const OVERRIDE {
101 return Location::RegisterLocation(ECX);
102 }
103 Location GetFieldIndexLocation() const OVERRIDE {
104 return Location::RegisterLocation(EAX);
105 }
106 Location GetReturnLocation(Primitive::Type type) const OVERRIDE {
107 return Primitive::Is64BitType(type)
108 ? Location::RegisterPairLocation(EAX, EDX)
109 : Location::RegisterLocation(EAX);
110 }
111 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
112 return Primitive::Is64BitType(type)
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000113 ? (is_instance
114 ? Location::RegisterPairLocation(EDX, EBX)
115 : Location::RegisterPairLocation(ECX, EDX))
Calin Juravlee460d1d2015-09-29 04:52:17 +0100116 : (is_instance
117 ? Location::RegisterLocation(EDX)
118 : Location::RegisterLocation(ECX));
119 }
120 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
121 return Location::FpuRegisterLocation(XMM0);
122 }
123
124 private:
125 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionX86);
126};
127
Zheng Xuad4450e2015-04-17 18:48:56 +0800128class ParallelMoveResolverX86 : public ParallelMoveResolverWithSwap {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100129 public:
130 ParallelMoveResolverX86(ArenaAllocator* allocator, CodeGeneratorX86* codegen)
Zheng Xuad4450e2015-04-17 18:48:56 +0800131 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100132
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000133 void EmitMove(size_t index) OVERRIDE;
134 void EmitSwap(size_t index) OVERRIDE;
135 void SpillScratch(int reg) OVERRIDE;
136 void RestoreScratch(int reg) OVERRIDE;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100137
138 X86Assembler* GetAssembler() const;
139
140 private:
141 void Exchange(Register reg, int mem);
142 void Exchange(int mem1, int mem2);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500143 void Exchange32(XmmRegister reg, int mem);
144 void MoveMemoryToMemory32(int dst, int src);
145 void MoveMemoryToMemory64(int dst, int src);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100146
147 CodeGeneratorX86* const codegen_;
148
149 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86);
150};
151
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000152class LocationsBuilderX86 : public HGraphVisitor {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000153 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100154 LocationsBuilderX86(HGraph* graph, CodeGeneratorX86* codegen)
155 : HGraphVisitor(graph), codegen_(codegen) {}
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000156
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100157#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000158 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000159
Alexandre Ramesef20f712015-06-09 10:29:30 +0100160 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
161 FOR_EACH_CONCRETE_INSTRUCTION_X86(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000162
163#undef DECLARE_VISIT_INSTRUCTION
164
Alexandre Ramesef20f712015-06-09 10:29:30 +0100165 void VisitInstruction(HInstruction* instruction) OVERRIDE {
166 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
167 << " (id " << instruction->GetId() << ")";
168 }
169
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000170 private:
171 void HandleBitwiseOperation(HBinaryOperation* instruction);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100172 void HandleInvoke(HInvoke* invoke);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000173 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000174 void HandleShift(HBinaryOperation* instruction);
Calin Juravle52c48962014-12-16 17:02:57 +0000175 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
176 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100177
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100178 CodeGeneratorX86* const codegen_;
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100179 InvokeDexCallingConventionVisitorX86 parameter_visitor_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100180
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000181 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86);
182};
183
Aart Bik42249c32016-01-07 15:33:50 -0800184class InstructionCodeGeneratorX86 : public InstructionCodeGenerator {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000185 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100186 InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000187
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100188#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000189 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000190
Alexandre Ramesef20f712015-06-09 10:29:30 +0100191 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
192 FOR_EACH_CONCRETE_INSTRUCTION_X86(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000193
194#undef DECLARE_VISIT_INSTRUCTION
195
Alexandre Ramesef20f712015-06-09 10:29:30 +0100196 void VisitInstruction(HInstruction* instruction) OVERRIDE {
197 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
198 << " (id " << instruction->GetId() << ")";
199 }
200
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100201 X86Assembler* GetAssembler() const { return assembler_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000202
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000203 // The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
204 // table version generates 7 instructions and num_entries literals. Compare/jump sequence will
205 // generates less code/data with a small num_entries.
206 static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
207
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000208 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 // Generate code for the given suspend check. If not null, `successor`
210 // is the block to branch to if the suspend check is not needed, and after
211 // the suspend call.
212 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700213 void GenerateClassInitializationCheck(SlowPathCode* slow_path, Register class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000214 void HandleBitwiseOperation(HBinaryOperation* instruction);
Calin Juravlebacfec32014-11-14 15:54:36 +0000215 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100216 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
Guillaume Sanchezb19930c2015-04-09 21:12:15 +0100217 void DivByPowerOfTwo(HDiv* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100218 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
Mark Mendellc4701932015-04-10 13:18:51 -0400219 void GenerateRemFP(HRem* rem);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000220 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000221 void HandleShift(HBinaryOperation* instruction);
222 void GenerateShlLong(const Location& loc, Register shifter);
223 void GenerateShrLong(const Location& loc, Register shifter);
224 void GenerateUShrLong(const Location& loc, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000225 void GenerateShlLong(const Location& loc, int shift);
226 void GenerateShrLong(const Location& loc, int shift);
227 void GenerateUShrLong(const Location& loc, int shift);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000228
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100229 void HandleFieldSet(HInstruction* instruction,
230 const FieldInfo& field_info,
231 bool value_can_be_null);
Calin Juravle52c48962014-12-16 17:02:57 +0000232 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000233
234 // Generate a heap reference load using one register `out`:
235 //
236 // out <- *(out + offset)
237 //
238 // while honoring heap poisoning and/or read barriers (if any).
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000239 //
240 // Location `maybe_temp` is used when generating a read barrier and
241 // shall be a register in that case; it may be an invalid location
242 // otherwise.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000243 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
244 Location out,
245 uint32_t offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -0800246 Location maybe_temp,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800247 ReadBarrierOption read_barrier_option);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000248 // Generate a heap reference load using two different registers
249 // `out` and `obj`:
250 //
251 // out <- *(obj + offset)
252 //
253 // while honoring heap poisoning and/or read barriers (if any).
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000254 //
255 // Location `maybe_temp` is used when generating a Baker's (fast
256 // path) read barrier and shall be a register in that case; it may
257 // be an invalid location otherwise.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000258 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
259 Location out,
260 Location obj,
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -0700261 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800262 ReadBarrierOption read_barrier_option);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000263 // Generate a GC root reference load:
264 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000265 // root <- *address
Roland Levillain7c1559a2015-12-15 10:55:36 +0000266 //
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800267 // while honoring read barriers based on read_barrier_option.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000268 void GenerateGcRootFieldLoad(HInstruction* instruction,
269 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000270 const Address& address,
Roland Levillain00468f32016-10-27 18:02:48 +0100271 Label* fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -0800272 ReadBarrierOption read_barrier_option);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000273
Roland Levillain232ade02015-04-20 15:14:36 +0100274 // Push value to FPU stack. `is_fp` specifies whether the value is floating point or not.
275 // `is_wide` specifies whether it is long/double or not.
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500276 void PushOntoFPStack(Location source, uint32_t temp_offset,
Roland Levillain232ade02015-04-20 15:14:36 +0100277 uint32_t stack_adjustment, bool is_fp, bool is_wide);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100278
Mark Mendell152408f2015-12-31 12:28:50 -0500279 template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700280 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000281 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -0500282 LabelType* true_target,
283 LabelType* false_target);
284 template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +0000285 void GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -0500286 LabelType* true_target,
287 LabelType* false_target);
288 template<class LabelType>
289 void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
290 template<class LabelType>
291 void GenerateLongComparesAndJumps(HCondition* cond,
292 LabelType* true_label,
293 LabelType* false_label);
294
David Brazdilfc6a86a2015-06-26 10:33:45 +0000295 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000296 void GenPackedSwitchWithCompares(Register value_reg,
297 int32_t lower_bound,
298 uint32_t num_entries,
299 HBasicBlock* switch_block,
300 HBasicBlock* default_block);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000301
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000302 void GenerateFPCompare(Location lhs, Location rhs, HInstruction* insn, bool is_double);
303
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100304 X86Assembler* const assembler_;
305 CodeGeneratorX86* const codegen_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000306
307 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86);
308};
309
Mark Mendell805b3b52015-09-18 14:10:29 -0400310class JumpTableRIPFixup;
311
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000312class CodeGeneratorX86 : public CodeGenerator {
313 public:
Mark Mendellfb8d2792015-03-31 22:16:59 -0400314 CodeGeneratorX86(HGraph* graph,
315 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100316 const CompilerOptions& compiler_options,
317 OptimizingCompilerStats* stats = nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100318 virtual ~CodeGeneratorX86() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000320 void GenerateFrameEntry() OVERRIDE;
321 void GenerateFrameExit() OVERRIDE;
322 void Bind(HBasicBlock* block) OVERRIDE;
Calin Juravle175dc732015-08-25 15:42:32 +0100323 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100324 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
325 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
326
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000327 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
328 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Mark Mendell7c8d0092015-01-26 11:21:33 -0500329 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
330 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000331
Alexandre Rames8158f282015-08-07 10:26:17 +0100332 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100333 void InvokeRuntime(QuickEntrypointEnum entrypoint,
334 HInstruction* instruction,
335 uint32_t dex_pc,
Serban Constantinescuba45db02016-07-12 22:53:02 +0100336 SlowPathCode* slow_path = nullptr) OVERRIDE;
Alexandre Rames8158f282015-08-07 10:26:17 +0100337
Roland Levillaindec8f632016-07-22 17:10:06 +0100338 // Generate code to invoke a runtime entry point, but do not record
339 // PC-related information in a stack map.
340 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
341 HInstruction* instruction,
342 SlowPathCode* slow_path);
343
Serban Constantinescuba45db02016-07-12 22:53:02 +0100344 void GenerateInvokeRuntime(int32_t entry_point_offset);
345
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000346 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100347 return kX86WordSize;
348 }
349
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500350 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700351 return GetGraph()->HasSIMD()
352 ? 4 * kX86WordSize // 16 bytes == 4 words for each spill
353 : 2 * kX86WordSize; // 8 bytes == 2 words for each spill
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500354 }
355
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000356 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000357 return &location_builder_;
358 }
359
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000360 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 return &instruction_visitor_;
362 }
363
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000364 X86Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 return &assembler_;
366 }
367
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100368 const X86Assembler& GetAssembler() const OVERRIDE {
369 return assembler_;
370 }
371
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100372 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000373 return GetLabelOf(block)->Position();
374 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100375
David Brazdil58282f42016-01-14 12:45:10 +0000376 void SetupBlockedRegisters() const OVERRIDE;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100377
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000378 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
379 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100380
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000381 ParallelMoveResolverX86* GetMoveResolver() OVERRIDE {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382 return &move_resolver_;
383 }
384
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000385 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100386 return InstructionSet::kX86;
387 }
388
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100389 // Helper method to move a 32bits value between two locations.
390 void Move32(Location destination, Location source);
391 // Helper method to move a 64bits value between two locations.
392 void Move64(Location destination, Location source);
393
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000394 // Check if the desired_string_load_kind is supported. If it is, return it,
395 // otherwise return a fall-back kind that should be used instead.
396 HLoadString::LoadKind GetSupportedLoadStringKind(
397 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
398
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100399 // Check if the desired_class_load_kind is supported. If it is, return it,
400 // otherwise return a fall-back kind that should be used instead.
401 HLoadClass::LoadKind GetSupportedLoadClassKind(
402 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
403
Vladimir Markodc151b22015-10-15 18:02:30 +0100404 // Check if the desired_dispatch_info is supported. If it is, return it,
405 // otherwise return a fall-back info that should be used instead.
406 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
407 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100408 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100409
Mark Mendell09ed1a32015-03-25 08:30:06 -0400410 // Generate a call to a static or direct method.
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100411 void GenerateStaticOrDirectCall(
412 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000413 // Generate a call to a virtual method.
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100414 void GenerateVirtualCall(
415 HInvokeVirtual* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
Andreas Gampe85b62f22015-09-09 13:15:38 -0700416
Vladimir Marko65979462017-05-19 17:25:12 +0100417 void RecordBootMethodPatch(HInvokeStaticOrDirect* invoke);
Vladimir Marko1998cd02017-01-13 13:02:58 +0000418 void RecordBootTypePatch(HLoadClass* load_class);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000419 Label* NewTypeBssEntryPatch(HLoadClass* load_class);
Vladimir Marko65979462017-05-19 17:25:12 +0100420 void RecordBootStringPatch(HLoadString* load_string);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000421 Label* NewStringBssEntryPatch(HLoadString* load_string);
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000422 Label* NewPcRelativeDexCacheArrayPatch(HX86ComputeBaseMethodAddress* method_address,
423 const DexFile& dex_file,
424 uint32_t element_offset);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000425 Label* NewJitRootStringPatch(const DexFile& dex_file,
426 dex::StringIndex dex_index,
427 Handle<mirror::String> handle);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000428 Label* NewJitRootClassPatch(const DexFile& dex_file,
429 dex::TypeIndex dex_index,
430 Handle<mirror::Class> handle);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000431
Andreas Gampe85b62f22015-09-09 13:15:38 -0700432 void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
Mark Mendell09ed1a32015-03-25 08:30:06 -0400433
Vladimir Marko58155012015-08-19 12:49:41 +0000434 // Emit linker patches.
435 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
436
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000437 void PatchJitRootUse(uint8_t* code,
438 const uint8_t* roots_data,
439 const PatchInfo<Label>& info,
440 uint64_t index_in_table) const;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000441 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
442
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100443 // Emit a write barrier.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100444 void MarkGCCard(Register temp,
445 Register card,
446 Register object,
447 Register value,
448 bool value_can_be_null);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100449
Roland Levillain7c1559a2015-12-15 10:55:36 +0000450 void GenerateMemoryBarrier(MemBarrierKind kind);
451
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100452 Label* GetLabelOf(HBasicBlock* block) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100453 return CommonGetLabelOf<Label>(block_labels_, block);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100454 }
455
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000456 void Initialize() OVERRIDE {
Vladimir Marko225b6462015-09-28 12:17:40 +0100457 block_labels_ = CommonInitializeLabels<Label>();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100458 }
459
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000460 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
461 return type == Primitive::kPrimLong;
462 }
463
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000464 bool ShouldSplitLongMoves() const OVERRIDE { return true; }
465
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000466 Label* GetFrameEntryLabel() { return &frame_entry_label_; }
467
Mark Mendellfb8d2792015-03-31 22:16:59 -0400468 const X86InstructionSetFeatures& GetInstructionSetFeatures() const {
469 return isa_features_;
470 }
471
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000472 void AddMethodAddressOffset(HX86ComputeBaseMethodAddress* method_base, int32_t offset) {
473 method_address_offset_.Put(method_base->GetId(), offset);
Mark Mendell0616ae02015-04-17 12:49:27 -0400474 }
475
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000476 int32_t GetMethodAddressOffset(HX86ComputeBaseMethodAddress* method_base) const {
477 return method_address_offset_.Get(method_base->GetId());
Mark Mendell0616ae02015-04-17 12:49:27 -0400478 }
479
480 int32_t ConstantAreaStart() const {
481 return constant_area_start_;
482 }
483
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000484 Address LiteralDoubleAddress(double v, HX86ComputeBaseMethodAddress* method_base, Register reg);
485 Address LiteralFloatAddress(float v, HX86ComputeBaseMethodAddress* method_base, Register reg);
486 Address LiteralInt32Address(int32_t v, HX86ComputeBaseMethodAddress* method_base, Register reg);
487 Address LiteralInt64Address(int64_t v, HX86ComputeBaseMethodAddress* method_base, Register reg);
Mark Mendell0616ae02015-04-17 12:49:27 -0400488
Aart Bika19616e2016-02-01 18:57:58 -0800489 // Load a 32-bit value into a register in the most efficient manner.
490 void Load32BitValue(Register dest, int32_t value);
491
492 // Compare a register with a 32-bit value in the most efficient manner.
493 void Compare32BitValue(Register dest, int32_t value);
494
Vladimir Marko56f4bdd2016-09-16 11:32:36 +0100495 // Compare int values. Supports only register locations for `lhs`.
496 void GenerateIntCompare(Location lhs, Location rhs);
jessicahandojo4877b792016-09-08 19:49:13 -0700497 void GenerateIntCompare(Register lhs, Location rhs);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +0100498
499 // Construct address for array access.
500 static Address ArrayAddress(Register obj,
501 Location index,
502 ScaleFactor scale,
503 uint32_t data_offset);
504
Mark Mendell805b3b52015-09-18 14:10:29 -0400505 Address LiteralCaseTable(HX86PackedSwitch* switch_instr, Register reg, Register value);
506
Mark Mendell0616ae02015-04-17 12:49:27 -0400507 void Finalize(CodeAllocator* allocator) OVERRIDE;
508
Roland Levillain7c1559a2015-12-15 10:55:36 +0000509 // Fast path implementation of ReadBarrier::Barrier for a heap
510 // reference field load when Baker's read barriers are used.
511 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000512 Location ref,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000513 Register obj,
514 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000515 bool needs_null_check);
516 // Fast path implementation of ReadBarrier::Barrier for a heap
517 // reference array load when Baker's read barriers are used.
518 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000519 Location ref,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000520 Register obj,
521 uint32_t data_offset,
522 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000523 bool needs_null_check);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100524 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
525 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
526 //
527 // Load the object reference located at address `src`, held by
528 // object `obj`, into `ref`, and mark it if needed. The base of
529 // address `src` must be `obj`.
530 //
531 // If `always_update_field` is true, the value of the reference is
532 // atomically updated in the holder (`obj`). This operation
533 // requires a temporary register, which must be provided as a
534 // non-null pointer (`temp`).
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +0800535 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
536 Location ref,
537 Register obj,
538 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100539 bool needs_null_check,
540 bool always_update_field = false,
541 Register* temp = nullptr);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000542
543 // Generate a read barrier for a heap reference within `instruction`
544 // using a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000545 //
546 // A read barrier for an object reference read from the heap is
547 // implemented as a call to the artReadBarrierSlow runtime entry
548 // point, which is passed the values in locations `ref`, `obj`, and
549 // `offset`:
550 //
551 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
552 // mirror::Object* obj,
553 // uint32_t offset);
554 //
555 // The `out` location contains the value returned by
556 // artReadBarrierSlow.
557 //
558 // When `index` is provided (i.e. for array accesses), the offset
559 // value passed to artReadBarrierSlow is adjusted to take `index`
560 // into account.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000561 void GenerateReadBarrierSlow(HInstruction* instruction,
562 Location out,
563 Location ref,
564 Location obj,
565 uint32_t offset,
566 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000567
Roland Levillain7c1559a2015-12-15 10:55:36 +0000568 // If read barriers are enabled, generate a read barrier for a heap
569 // reference using a slow path. If heap poisoning is enabled, also
570 // unpoison the reference in `out`.
571 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
572 Location out,
573 Location ref,
574 Location obj,
575 uint32_t offset,
576 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000577
Roland Levillain7c1559a2015-12-15 10:55:36 +0000578 // Generate a read barrier for a GC root within `instruction` using
579 // a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000580 //
581 // A read barrier for an object reference GC root is implemented as
582 // a call to the artReadBarrierForRootSlow runtime entry point,
583 // which is passed the value in location `root`:
584 //
585 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
586 //
587 // The `out` location contains the value returned by
588 // artReadBarrierForRootSlow.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000589 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000590
Mark P Mendell17077d82015-12-16 19:15:59 +0000591 // Ensure that prior stores complete to memory before subsequent loads.
592 // The locked add implementation will avoid serializing device memory, but will
593 // touch (but not change) the top of the stack.
594 // The 'non_temporal' parameter should be used to ensure ordering of non-temporal stores.
595 void MemoryFence(bool non_temporal = false) {
Mark Mendell7aa04a12016-01-27 22:39:07 -0500596 if (!non_temporal) {
Mark P Mendell17077d82015-12-16 19:15:59 +0000597 assembler_.lock()->addl(Address(ESP, 0), Immediate(0));
598 } else {
599 assembler_.mfence();
600 }
601 }
602
Roland Levillainf41f9562016-09-14 19:26:48 +0100603 void GenerateNop() OVERRIDE;
604 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
605 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
Mark P Mendell17077d82015-12-16 19:15:59 +0000606
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000607 // When we don't know the proper offset for the value, we use kDummy32BitOffset.
608 // The correct value will be inserted when processing Assembler fixups.
609 static constexpr int32_t kDummy32BitOffset = 256;
610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100611 private:
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000612 struct X86PcRelativePatchInfo : PatchInfo<Label> {
613 X86PcRelativePatchInfo(HX86ComputeBaseMethodAddress* address,
614 const DexFile& target_dex_file,
615 uint32_t target_index)
616 : PatchInfo(target_dex_file, target_index),
617 method_address(address) {}
618 HX86ComputeBaseMethodAddress* method_address;
619 };
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000620
Vladimir Markoaad75c62016-10-03 08:46:48 +0000621 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000622 void EmitPcRelativeLinkerPatches(const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000623 ArenaVector<LinkerPatch>* linker_patches);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000624
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000625 Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke, Register temp);
626
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100627 // Labels for each block that will be compiled.
Vladimir Marko225b6462015-09-28 12:17:40 +0100628 Label* block_labels_; // Indexed by block id.
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000629 Label frame_entry_label_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000630 LocationsBuilderX86 location_builder_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000631 InstructionCodeGeneratorX86 instruction_visitor_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100632 ParallelMoveResolverX86 move_resolver_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000633 X86Assembler assembler_;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400634 const X86InstructionSetFeatures& isa_features_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000635
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000636 // PC-relative DexCache access info.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000637 ArenaDeque<X86PcRelativePatchInfo> pc_relative_dex_cache_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100638 // PC-relative method patch info for kBootImageLinkTimePcRelative.
639 ArenaDeque<X86PcRelativePatchInfo> boot_image_method_patches_;
Vladimir Marko764d4542017-05-16 10:31:41 +0100640 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000641 ArenaDeque<X86PcRelativePatchInfo> boot_image_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000642 // Type patch locations for kBssEntry.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000643 ArenaDeque<X86PcRelativePatchInfo> type_bss_entry_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100644 // String patch locations; type depends on configuration (app .bss or boot image).
645 ArenaDeque<X86PcRelativePatchInfo> string_patches_;
Vladimir Marko58155012015-08-19 12:49:41 +0000646
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000647 // Patches for string root accesses in JIT compiled code.
648 ArenaDeque<PatchInfo<Label>> jit_string_patches_;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000649 // Patches for class root accesses in JIT compiled code.
650 ArenaDeque<PatchInfo<Label>> jit_class_patches_;
651
Mark Mendell0616ae02015-04-17 12:49:27 -0400652 // Offset to the start of the constant area in the assembled code.
653 // Used for fixups to the constant area.
654 int32_t constant_area_start_;
655
Mark Mendell805b3b52015-09-18 14:10:29 -0400656 // Fixups for jump tables that need to be patched after the constant table is generated.
657 ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_;
658
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000659 // Maps a HX86ComputeBaseMethodAddress instruction id, to its offset in the
660 // compiled code.
661 ArenaSafeMap<uint32_t, int32_t> method_address_offset_;
Mark Mendell0616ae02015-04-17 12:49:27 -0400662
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000663 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86);
664};
665
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000666} // namespace x86
667} // namespace art
668
669#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_H_