blob: ebbe486cb54f01f91d9af8b637dd11b85a9e6a39 [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"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#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 Geoffrayd4dd2552014-02-28 10:23:58 +000024#include "nodes.h"
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010025#include "parallel_move_resolver.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026#include "utils/x86/assembler_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027
28namespace art {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029namespace x86 {
30
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000031// Use a local definition to prevent copying mistakes.
32static constexpr size_t kX86WordSize = kX86PointerSize;
Nicolas Geoffray707c8092014-04-04 10:50:14 +010033
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034class CodeGeneratorX86;
35
Nicolas Geoffraya747a392014-04-17 14:56:23 +010036static constexpr Register kParameterCoreRegisters[] = { ECX, EDX, EBX };
37static constexpr RegisterPair kParameterCorePairRegisters[] = { ECX_EDX, EDX_EBX };
38static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000039static constexpr XmmRegister kParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
40static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
Nicolas Geoffraya747a392014-04-17 14:56:23 +010041
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000042static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
43static constexpr size_t kRuntimeParameterCoreRegistersLength =
44 arraysize(kRuntimeParameterCoreRegisters);
45static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
46static constexpr size_t kRuntimeParameterFpuRegistersLength =
47 arraysize(kRuntimeParameterFpuRegisters);
48
49class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
50 public:
51 InvokeRuntimeCallingConvention()
52 : CallingConvention(kRuntimeParameterCoreRegisters,
53 kRuntimeParameterCoreRegistersLength,
54 kRuntimeParameterFpuRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070055 kRuntimeParameterFpuRegistersLength,
56 kX86PointerSize) {}
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000057
58 private:
59 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
60};
61
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062class InvokeDexCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffraya747a392014-04-17 14:56:23 +010063 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010064 InvokeDexCallingConvention() : CallingConvention(
65 kParameterCoreRegisters,
66 kParameterCoreRegistersLength,
67 kParameterFpuRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070068 kParameterFpuRegistersLength,
69 kX86PointerSize) {}
Nicolas Geoffraya747a392014-04-17 14:56:23 +010070
71 RegisterPair GetRegisterPairAt(size_t argument_index) {
72 DCHECK_LT(argument_index + 1, GetNumberOfRegisters());
73 return kParameterCorePairRegisters[argument_index];
74 }
75
76 private:
77 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
78};
79
Roland Levillain2d27c8e2015-04-28 15:48:45 +010080class InvokeDexCallingConventionVisitorX86 : public InvokeDexCallingConventionVisitor {
Nicolas Geoffraya747a392014-04-17 14:56:23 +010081 public:
Roland Levillain2d27c8e2015-04-28 15:48:45 +010082 InvokeDexCallingConventionVisitorX86() {}
83 virtual ~InvokeDexCallingConventionVisitorX86() {}
Nicolas Geoffraya747a392014-04-17 14:56:23 +010084
Roland Levillain2d27c8e2015-04-28 15:48:45 +010085 Location GetNextLocation(Primitive::Type type) OVERRIDE;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +010086 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
87 Location GetMethodLocation() const OVERRIDE;
Nicolas Geoffraya747a392014-04-17 14:56:23 +010088
89 private:
90 InvokeDexCallingConvention calling_convention;
Nicolas Geoffraya747a392014-04-17 14:56:23 +010091
Roland Levillain2d27c8e2015-04-28 15:48:45 +010092 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86);
Nicolas Geoffraya747a392014-04-17 14:56:23 +010093};
94
Calin Juravlee460d1d2015-09-29 04:52:17 +010095class FieldAccessCallingConventionX86 : public FieldAccessCallingConvention {
96 public:
97 FieldAccessCallingConventionX86() {}
98
99 Location GetObjectLocation() const OVERRIDE {
100 return Location::RegisterLocation(ECX);
101 }
102 Location GetFieldIndexLocation() const OVERRIDE {
103 return Location::RegisterLocation(EAX);
104 }
105 Location GetReturnLocation(Primitive::Type type) const OVERRIDE {
106 return Primitive::Is64BitType(type)
107 ? Location::RegisterPairLocation(EAX, EDX)
108 : Location::RegisterLocation(EAX);
109 }
110 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
111 return Primitive::Is64BitType(type)
112 ? Location::RegisterPairLocation(EDX, EBX)
113 : (is_instance
114 ? Location::RegisterLocation(EDX)
115 : Location::RegisterLocation(ECX));
116 }
117 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
118 return Location::FpuRegisterLocation(XMM0);
119 }
120
121 private:
122 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionX86);
123};
124
Zheng Xuad4450e2015-04-17 18:48:56 +0800125class ParallelMoveResolverX86 : public ParallelMoveResolverWithSwap {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100126 public:
127 ParallelMoveResolverX86(ArenaAllocator* allocator, CodeGeneratorX86* codegen)
Zheng Xuad4450e2015-04-17 18:48:56 +0800128 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100129
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000130 void EmitMove(size_t index) OVERRIDE;
131 void EmitSwap(size_t index) OVERRIDE;
132 void SpillScratch(int reg) OVERRIDE;
133 void RestoreScratch(int reg) OVERRIDE;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100134
135 X86Assembler* GetAssembler() const;
136
137 private:
138 void Exchange(Register reg, int mem);
139 void Exchange(int mem1, int mem2);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500140 void Exchange32(XmmRegister reg, int mem);
141 void MoveMemoryToMemory32(int dst, int src);
142 void MoveMemoryToMemory64(int dst, int src);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100143
144 CodeGeneratorX86* const codegen_;
145
146 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86);
147};
148
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000149class LocationsBuilderX86 : public HGraphVisitor {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000150 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100151 LocationsBuilderX86(HGraph* graph, CodeGeneratorX86* codegen)
152 : HGraphVisitor(graph), codegen_(codegen) {}
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000153
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100154#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000155 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000156
Alexandre Ramesef20f712015-06-09 10:29:30 +0100157 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
158 FOR_EACH_CONCRETE_INSTRUCTION_X86(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000159
160#undef DECLARE_VISIT_INSTRUCTION
161
Alexandre Ramesef20f712015-06-09 10:29:30 +0100162 void VisitInstruction(HInstruction* instruction) OVERRIDE {
163 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
164 << " (id " << instruction->GetId() << ")";
165 }
166
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000167 private:
168 void HandleBitwiseOperation(HBinaryOperation* instruction);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100169 void HandleInvoke(HInvoke* invoke);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000170 void HandleShift(HBinaryOperation* instruction);
Calin Juravle52c48962014-12-16 17:02:57 +0000171 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
172 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100173
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100174 CodeGeneratorX86* const codegen_;
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100175 InvokeDexCallingConventionVisitorX86 parameter_visitor_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100176
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000177 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86);
178};
179
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000180class InstructionCodeGeneratorX86 : public HGraphVisitor {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000181 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100182 InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000183
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100184#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000185 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000186
Alexandre Ramesef20f712015-06-09 10:29:30 +0100187 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
188 FOR_EACH_CONCRETE_INSTRUCTION_X86(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000189
190#undef DECLARE_VISIT_INSTRUCTION
191
Alexandre Ramesef20f712015-06-09 10:29:30 +0100192 void VisitInstruction(HInstruction* instruction) OVERRIDE {
193 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
194 << " (id " << instruction->GetId() << ")";
195 }
196
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100197 X86Assembler* GetAssembler() const { return assembler_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000198
199 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 // Generate code for the given suspend check. If not null, `successor`
201 // is the block to branch to if the suspend check is not needed, and after
202 // the suspend call.
203 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700204 void GenerateClassInitializationCheck(SlowPathCode* slow_path, Register class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000205 void HandleBitwiseOperation(HBinaryOperation* instruction);
Calin Juravlebacfec32014-11-14 15:54:36 +0000206 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100207 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
Guillaume Sanchezb19930c2015-04-09 21:12:15 +0100208 void DivByPowerOfTwo(HDiv* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100209 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
Mark Mendellc4701932015-04-10 13:18:51 -0400210 void GenerateRemFP(HRem* rem);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000211 void HandleShift(HBinaryOperation* instruction);
212 void GenerateShlLong(const Location& loc, Register shifter);
213 void GenerateShrLong(const Location& loc, Register shifter);
214 void GenerateUShrLong(const Location& loc, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000215 void GenerateShlLong(const Location& loc, int shift);
216 void GenerateShrLong(const Location& loc, int shift);
217 void GenerateUShrLong(const Location& loc, int shift);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000218
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100219 void HandleFieldSet(HInstruction* instruction,
220 const FieldInfo& field_info,
221 bool value_can_be_null);
Calin Juravle52c48962014-12-16 17:02:57 +0000222 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000223
224 // Generate a heap reference load using one register `out`:
225 //
226 // out <- *(out + offset)
227 //
228 // while honoring heap poisoning and/or read barriers (if any).
229 // Register `temp` is used when generating a read barrier.
230 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
231 Location out,
232 uint32_t offset,
233 Location temp);
234 // Generate a heap reference load using two different registers
235 // `out` and `obj`:
236 //
237 // out <- *(obj + offset)
238 //
239 // while honoring heap poisoning and/or read barriers (if any).
240 // Register `temp` is used when generating a Baker's read barrier.
241 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
242 Location out,
243 Location obj,
244 uint32_t offset,
245 Location temp);
246 // Generate a GC root reference load:
247 //
248 // root <- *(obj + offset)
249 //
250 // while honoring read barriers (if any).
251 void GenerateGcRootFieldLoad(HInstruction* instruction,
252 Location root,
253 Register obj,
254 uint32_t offset);
255
Roland Levillain232ade02015-04-20 15:14:36 +0100256 // Push value to FPU stack. `is_fp` specifies whether the value is floating point or not.
257 // `is_wide` specifies whether it is long/double or not.
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500258 void PushOntoFPStack(Location source, uint32_t temp_offset,
Roland Levillain232ade02015-04-20 15:14:36 +0100259 uint32_t stack_adjustment, bool is_fp, bool is_wide);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100260
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000261 void GenerateImplicitNullCheck(HNullCheck* instruction);
262 void GenerateExplicitNullCheck(HNullCheck* instruction);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700263 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000264 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700265 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000266 Label* false_target);
267 void GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendellc4701932015-04-10 13:18:51 -0400268 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000269 Label* false_target);
Mark Mendellc4701932015-04-10 13:18:51 -0400270 void GenerateFPJumps(HCondition* cond, Label* true_label, Label* false_label);
271 void GenerateLongComparesAndJumps(HCondition* cond, Label* true_label, Label* false_label);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000272 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000273
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100274 X86Assembler* const assembler_;
275 CodeGeneratorX86* const codegen_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000276
277 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86);
278};
279
Mark Mendell805b3b52015-09-18 14:10:29 -0400280class JumpTableRIPFixup;
281
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000282class CodeGeneratorX86 : public CodeGenerator {
283 public:
Mark Mendellfb8d2792015-03-31 22:16:59 -0400284 CodeGeneratorX86(HGraph* graph,
285 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100286 const CompilerOptions& compiler_options,
287 OptimizingCompilerStats* stats = nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100288 virtual ~CodeGeneratorX86() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000289
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000290 void GenerateFrameEntry() OVERRIDE;
291 void GenerateFrameExit() OVERRIDE;
292 void Bind(HBasicBlock* block) OVERRIDE;
293 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Calin Juravle175dc732015-08-25 15:42:32 +0100294 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100295 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
296 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
297
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000298 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
299 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Mark Mendell7c8d0092015-01-26 11:21:33 -0500300 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
301 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000302
Alexandre Rames8158f282015-08-07 10:26:17 +0100303 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100304 void InvokeRuntime(QuickEntrypointEnum entrypoint,
305 HInstruction* instruction,
306 uint32_t dex_pc,
307 SlowPathCode* slow_path) OVERRIDE;
308
309 void InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100310 HInstruction* instruction,
311 uint32_t dex_pc,
312 SlowPathCode* slow_path);
313
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000314 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100315 return kX86WordSize;
316 }
317
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500318 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
319 // 8 bytes == 2 words for each spill.
320 return 2 * kX86WordSize;
321 }
322
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000323 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000324 return &location_builder_;
325 }
326
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000327 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 return &instruction_visitor_;
329 }
330
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000331 X86Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000332 return &assembler_;
333 }
334
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100335 const X86Assembler& GetAssembler() const OVERRIDE {
336 return assembler_;
337 }
338
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000339 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
340 return GetLabelOf(block)->Position();
341 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100342
Nicolas Geoffray98893962015-01-21 12:32:32 +0000343 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100344
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000345 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100346
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000347 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
348
349 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
350 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100351
Calin Juravle34bacdf2014-10-07 20:23:36 +0100352 // Blocks all register pairs made out of blocked core registers.
353 void UpdateBlockedPairRegisters() const;
354
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000355 ParallelMoveResolverX86* GetMoveResolver() OVERRIDE {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100356 return &move_resolver_;
357 }
358
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000359 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100360 return InstructionSet::kX86;
361 }
362
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100363 // Helper method to move a 32bits value between two locations.
364 void Move32(Location destination, Location source);
365 // Helper method to move a 64bits value between two locations.
366 void Move64(Location destination, Location source);
367
Vladimir Markodc151b22015-10-15 18:02:30 +0100368 // Check if the desired_dispatch_info is supported. If it is, return it,
369 // otherwise return a fall-back info that should be used instead.
370 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
371 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
372 MethodReference target_method) OVERRIDE;
373
Mark Mendell09ed1a32015-03-25 08:30:06 -0400374 // Generate a call to a static or direct method.
Andreas Gampe85b62f22015-09-09 13:15:38 -0700375 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000376 // Generate a call to a virtual method.
Andreas Gampe85b62f22015-09-09 13:15:38 -0700377 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
378
379 void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
Mark Mendell09ed1a32015-03-25 08:30:06 -0400380
Vladimir Marko58155012015-08-19 12:49:41 +0000381 // Emit linker patches.
382 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
383
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100384 // Emit a write barrier.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100385 void MarkGCCard(Register temp,
386 Register card,
387 Register object,
388 Register value,
389 bool value_can_be_null);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100390
Roland Levillain7c1559a2015-12-15 10:55:36 +0000391 void GenerateMemoryBarrier(MemBarrierKind kind);
392
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100393 Label* GetLabelOf(HBasicBlock* block) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100394 return CommonGetLabelOf<Label>(block_labels_, block);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100395 }
396
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000397 void Initialize() OVERRIDE {
Vladimir Marko225b6462015-09-28 12:17:40 +0100398 block_labels_ = CommonInitializeLabels<Label>();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100399 }
400
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000401 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
402 return type == Primitive::kPrimLong;
403 }
404
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000405 bool ShouldSplitLongMoves() const OVERRIDE { return true; }
406
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000407 Label* GetFrameEntryLabel() { return &frame_entry_label_; }
408
Mark Mendellfb8d2792015-03-31 22:16:59 -0400409 const X86InstructionSetFeatures& GetInstructionSetFeatures() const {
410 return isa_features_;
411 }
412
Mark Mendell0616ae02015-04-17 12:49:27 -0400413 void SetMethodAddressOffset(int32_t offset) {
414 method_address_offset_ = offset;
415 }
416
417 int32_t GetMethodAddressOffset() const {
418 return method_address_offset_;
419 }
420
421 int32_t ConstantAreaStart() const {
422 return constant_area_start_;
423 }
424
425 Address LiteralDoubleAddress(double v, Register reg);
426 Address LiteralFloatAddress(float v, Register reg);
427 Address LiteralInt32Address(int32_t v, Register reg);
428 Address LiteralInt64Address(int64_t v, Register reg);
429
Mark Mendell805b3b52015-09-18 14:10:29 -0400430 Address LiteralCaseTable(HX86PackedSwitch* switch_instr, Register reg, Register value);
431
Mark Mendell0616ae02015-04-17 12:49:27 -0400432 void Finalize(CodeAllocator* allocator) OVERRIDE;
433
Roland Levillain7c1559a2015-12-15 10:55:36 +0000434 // Fast path implementation of ReadBarrier::Barrier for a heap
435 // reference field load when Baker's read barriers are used.
436 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
437 Location out,
438 Register obj,
439 uint32_t offset,
440 Location temp,
441 bool needs_null_check);
442 // Fast path implementation of ReadBarrier::Barrier for a heap
443 // reference array load when Baker's read barriers are used.
444 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
445 Location out,
446 Register obj,
447 uint32_t data_offset,
448 Location index,
449 Location temp,
450 bool needs_null_check);
451
452 // Generate a read barrier for a heap reference within `instruction`
453 // using a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000454 //
455 // A read barrier for an object reference read from the heap is
456 // implemented as a call to the artReadBarrierSlow runtime entry
457 // point, which is passed the values in locations `ref`, `obj`, and
458 // `offset`:
459 //
460 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
461 // mirror::Object* obj,
462 // uint32_t offset);
463 //
464 // The `out` location contains the value returned by
465 // artReadBarrierSlow.
466 //
467 // When `index` is provided (i.e. for array accesses), the offset
468 // value passed to artReadBarrierSlow is adjusted to take `index`
469 // into account.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000470 void GenerateReadBarrierSlow(HInstruction* instruction,
471 Location out,
472 Location ref,
473 Location obj,
474 uint32_t offset,
475 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000476
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 // If read barriers are enabled, generate a read barrier for a heap
478 // reference using a slow path. If heap poisoning is enabled, also
479 // unpoison the reference in `out`.
480 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
481 Location out,
482 Location ref,
483 Location obj,
484 uint32_t offset,
485 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000486
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487 // Generate a read barrier for a GC root within `instruction` using
488 // a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000489 //
490 // A read barrier for an object reference GC root is implemented as
491 // a call to the artReadBarrierForRootSlow runtime entry point,
492 // which is passed the value in location `root`:
493 //
494 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
495 //
496 // The `out` location contains the value returned by
497 // artReadBarrierForRootSlow.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000498 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000499
Mark P Mendell17077d82015-12-16 19:15:59 +0000500 // Ensure that prior stores complete to memory before subsequent loads.
501 // The locked add implementation will avoid serializing device memory, but will
502 // touch (but not change) the top of the stack.
503 // The 'non_temporal' parameter should be used to ensure ordering of non-temporal stores.
504 void MemoryFence(bool non_temporal = false) {
505 if (!non_temporal && isa_features_.PrefersLockedAddSynchronization()) {
506 assembler_.lock()->addl(Address(ESP, 0), Immediate(0));
507 } else {
508 assembler_.mfence();
509 }
510 }
511
512
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100513 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000514 // Factored implementation of GenerateFieldLoadWithBakerReadBarrier
515 // and GenerateArrayLoadWithBakerReadBarrier.
516 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
517 Location ref,
518 Register obj,
519 const Address& src,
520 Location temp,
521 bool needs_null_check);
522
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000523 Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke, Register temp);
524
525 struct PcRelativeDexCacheAccessInfo {
526 PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off)
527 : target_dex_file(dex_file), element_offset(element_off), label() { }
528
529 const DexFile& target_dex_file;
530 uint32_t element_offset;
531 // NOTE: Label is bound to the end of the instruction that has an embedded 32-bit offset.
532 Label label;
533 };
534
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100535 // Labels for each block that will be compiled.
Vladimir Marko225b6462015-09-28 12:17:40 +0100536 Label* block_labels_; // Indexed by block id.
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000537 Label frame_entry_label_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000538 LocationsBuilderX86 location_builder_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000539 InstructionCodeGeneratorX86 instruction_visitor_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100540 ParallelMoveResolverX86 move_resolver_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000541 X86Assembler assembler_;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400542 const X86InstructionSetFeatures& isa_features_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000543
Vladimir Marko58155012015-08-19 12:49:41 +0000544 // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
545 ArenaDeque<MethodPatchInfo<Label>> method_patches_;
546 ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_;
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000547 // PC-relative DexCache access info.
548 ArenaDeque<PcRelativeDexCacheAccessInfo> pc_relative_dex_cache_patches_;
Vladimir Marko58155012015-08-19 12:49:41 +0000549
Mark Mendell0616ae02015-04-17 12:49:27 -0400550 // Offset to the start of the constant area in the assembled code.
551 // Used for fixups to the constant area.
552 int32_t constant_area_start_;
553
Mark Mendell805b3b52015-09-18 14:10:29 -0400554 // Fixups for jump tables that need to be patched after the constant table is generated.
555 ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_;
556
Mark Mendell0616ae02015-04-17 12:49:27 -0400557 // If there is a HX86ComputeBaseMethodAddress instruction in the graph
558 // (which shall be the sole instruction of this kind), subtracting this offset
559 // from the value contained in the out register of this HX86ComputeBaseMethodAddress
560 // instruction gives the address of the start of this method.
561 int32_t method_address_offset_;
562
563 // When we don't know the proper offset for the value, we use kDummy32BitOffset.
564 // The correct value will be inserted when processing Assembler fixups.
565 static constexpr int32_t kDummy32BitOffset = 256;
566
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000567 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86);
568};
569
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000570} // namespace x86
571} // namespace art
572
573#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_H_