blob: 7cf3bae79bcde3b81a3b5718218bd721ce03d6ca [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"
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.
Andreas Gampe542451c2016-07-26 09:02:02 -070032static constexpr size_t kX86WordSize = static_cast<size_t>(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);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000170 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000171 void HandleShift(HBinaryOperation* instruction);
Calin Juravle52c48962014-12-16 17:02:57 +0000172 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
173 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100174
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100175 CodeGeneratorX86* const codegen_;
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100176 InvokeDexCallingConventionVisitorX86 parameter_visitor_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000178 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86);
179};
180
Aart Bik42249c32016-01-07 15:33:50 -0800181class InstructionCodeGeneratorX86 : public InstructionCodeGenerator {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000182 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100183 InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000184
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100185#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000186 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000187
Alexandre Ramesef20f712015-06-09 10:29:30 +0100188 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
189 FOR_EACH_CONCRETE_INSTRUCTION_X86(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000190
191#undef DECLARE_VISIT_INSTRUCTION
192
Alexandre Ramesef20f712015-06-09 10:29:30 +0100193 void VisitInstruction(HInstruction* instruction) OVERRIDE {
194 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
195 << " (id " << instruction->GetId() << ")";
196 }
197
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100198 X86Assembler* GetAssembler() const { return assembler_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000199
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000200 // The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
201 // table version generates 7 instructions and num_entries literals. Compare/jump sequence will
202 // generates less code/data with a small num_entries.
203 static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
204
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000205 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 // Generate code for the given suspend check. If not null, `successor`
207 // is the block to branch to if the suspend check is not needed, and after
208 // the suspend call.
209 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700210 void GenerateClassInitializationCheck(SlowPathCode* slow_path, Register class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000211 void HandleBitwiseOperation(HBinaryOperation* instruction);
Calin Juravlebacfec32014-11-14 15:54:36 +0000212 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100213 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
Guillaume Sanchezb19930c2015-04-09 21:12:15 +0100214 void DivByPowerOfTwo(HDiv* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100215 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
Mark Mendellc4701932015-04-10 13:18:51 -0400216 void GenerateRemFP(HRem* rem);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000217 void HandleCondition(HCondition* condition);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000218 void HandleShift(HBinaryOperation* instruction);
219 void GenerateShlLong(const Location& loc, Register shifter);
220 void GenerateShrLong(const Location& loc, Register shifter);
221 void GenerateUShrLong(const Location& loc, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000222 void GenerateShlLong(const Location& loc, int shift);
223 void GenerateShrLong(const Location& loc, int shift);
224 void GenerateUShrLong(const Location& loc, int shift);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000225
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100226 void HandleFieldSet(HInstruction* instruction,
227 const FieldInfo& field_info,
228 bool value_can_be_null);
Calin Juravle52c48962014-12-16 17:02:57 +0000229 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000230
231 // Generate a heap reference load using one register `out`:
232 //
233 // out <- *(out + offset)
234 //
235 // while honoring heap poisoning and/or read barriers (if any).
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000236 //
237 // Location `maybe_temp` is used when generating a read barrier and
238 // shall be a register in that case; it may be an invalid location
239 // otherwise.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000240 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
241 Location out,
242 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000243 Location maybe_temp);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000244 // Generate a heap reference load using two different registers
245 // `out` and `obj`:
246 //
247 // out <- *(obj + offset)
248 //
249 // while honoring heap poisoning and/or read barriers (if any).
Roland Levillain95e7ffc2016-01-22 11:57:25 +0000250 //
251 // Location `maybe_temp` is used when generating a Baker's (fast
252 // path) read barrier and shall be a register in that case; it may
253 // be an invalid location otherwise.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000254 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
255 Location out,
256 Location obj,
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -0700257 uint32_t offset,
258 bool emit_read_barrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000259 // Generate a GC root reference load:
260 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000261 // root <- *address
Roland Levillain7c1559a2015-12-15 10:55:36 +0000262 //
Roland Levillain00468f32016-10-27 18:02:48 +0100263 // while honoring read barriers if `requires_read_barrier` is true.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000264 void GenerateGcRootFieldLoad(HInstruction* instruction,
265 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000266 const Address& address,
Roland Levillain00468f32016-10-27 18:02:48 +0100267 Label* fixup_label,
268 bool requires_read_barrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000269
Roland Levillain232ade02015-04-20 15:14:36 +0100270 // Push value to FPU stack. `is_fp` specifies whether the value is floating point or not.
271 // `is_wide` specifies whether it is long/double or not.
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500272 void PushOntoFPStack(Location source, uint32_t temp_offset,
Roland Levillain232ade02015-04-20 15:14:36 +0100273 uint32_t stack_adjustment, bool is_fp, bool is_wide);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100274
Mark Mendell152408f2015-12-31 12:28:50 -0500275 template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700276 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000277 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -0500278 LabelType* true_target,
279 LabelType* false_target);
280 template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +0000281 void GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -0500282 LabelType* true_target,
283 LabelType* false_target);
284 template<class LabelType>
285 void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
286 template<class LabelType>
287 void GenerateLongComparesAndJumps(HCondition* cond,
288 LabelType* true_label,
289 LabelType* false_label);
290
David Brazdilfc6a86a2015-06-26 10:33:45 +0000291 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000292 void GenPackedSwitchWithCompares(Register value_reg,
293 int32_t lower_bound,
294 uint32_t num_entries,
295 HBasicBlock* switch_block,
296 HBasicBlock* default_block);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000297
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000298 void GenerateFPCompare(Location lhs, Location rhs, HInstruction* insn, bool is_double);
299
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100300 X86Assembler* const assembler_;
301 CodeGeneratorX86* const codegen_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000302
303 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86);
304};
305
Mark Mendell805b3b52015-09-18 14:10:29 -0400306class JumpTableRIPFixup;
307
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000308class CodeGeneratorX86 : public CodeGenerator {
309 public:
Mark Mendellfb8d2792015-03-31 22:16:59 -0400310 CodeGeneratorX86(HGraph* graph,
311 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100312 const CompilerOptions& compiler_options,
313 OptimizingCompilerStats* stats = nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100314 virtual ~CodeGeneratorX86() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000315
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000316 void GenerateFrameEntry() OVERRIDE;
317 void GenerateFrameExit() OVERRIDE;
318 void Bind(HBasicBlock* block) OVERRIDE;
Calin Juravle175dc732015-08-25 15:42:32 +0100319 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100320 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
321 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
322
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000323 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
324 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Mark Mendell7c8d0092015-01-26 11:21:33 -0500325 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
326 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000327
Alexandre Rames8158f282015-08-07 10:26:17 +0100328 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100329 void InvokeRuntime(QuickEntrypointEnum entrypoint,
330 HInstruction* instruction,
331 uint32_t dex_pc,
Serban Constantinescuba45db02016-07-12 22:53:02 +0100332 SlowPathCode* slow_path = nullptr) OVERRIDE;
Alexandre Rames8158f282015-08-07 10:26:17 +0100333
Roland Levillaindec8f632016-07-22 17:10:06 +0100334 // Generate code to invoke a runtime entry point, but do not record
335 // PC-related information in a stack map.
336 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
337 HInstruction* instruction,
338 SlowPathCode* slow_path);
339
Serban Constantinescuba45db02016-07-12 22:53:02 +0100340 void GenerateInvokeRuntime(int32_t entry_point_offset);
341
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000342 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100343 return kX86WordSize;
344 }
345
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500346 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
347 // 8 bytes == 2 words for each spill.
348 return 2 * kX86WordSize;
349 }
350
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000351 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000352 return &location_builder_;
353 }
354
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000355 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000356 return &instruction_visitor_;
357 }
358
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000359 X86Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000360 return &assembler_;
361 }
362
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100363 const X86Assembler& GetAssembler() const OVERRIDE {
364 return assembler_;
365 }
366
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100367 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000368 return GetLabelOf(block)->Position();
369 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100370
David Brazdil58282f42016-01-14 12:45:10 +0000371 void SetupBlockedRegisters() const OVERRIDE;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100372
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000373 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
374 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000376 ParallelMoveResolverX86* GetMoveResolver() OVERRIDE {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100377 return &move_resolver_;
378 }
379
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000380 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100381 return InstructionSet::kX86;
382 }
383
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100384 // Helper method to move a 32bits value between two locations.
385 void Move32(Location destination, Location source);
386 // Helper method to move a 64bits value between two locations.
387 void Move64(Location destination, Location source);
388
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000389 // Check if the desired_string_load_kind is supported. If it is, return it,
390 // otherwise return a fall-back kind that should be used instead.
391 HLoadString::LoadKind GetSupportedLoadStringKind(
392 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
393
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100394 // Check if the desired_class_load_kind is supported. If it is, return it,
395 // otherwise return a fall-back kind that should be used instead.
396 HLoadClass::LoadKind GetSupportedLoadClassKind(
397 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
398
Vladimir Markodc151b22015-10-15 18:02:30 +0100399 // Check if the desired_dispatch_info is supported. If it is, return it,
400 // otherwise return a fall-back info that should be used instead.
401 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
402 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100403 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100404
Mark Mendell09ed1a32015-03-25 08:30:06 -0400405 // Generate a call to a static or direct method.
Serguei Katkov288c7a82016-05-16 11:53:15 +0600406 Location GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700407 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000408 // Generate a call to a virtual method.
Andreas Gampe85b62f22015-09-09 13:15:38 -0700409 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
410
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000411 void RecordSimplePatch();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000412 void RecordBootStringPatch(HLoadString* load_string);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100413 void RecordTypePatch(HLoadClass* load_class);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000414 Label* NewStringBssEntryPatch(HLoadString* load_string);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000415 Label* NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file, uint32_t element_offset);
Nicolas Geoffray997d1212016-11-09 10:36:29 +0000416 Label* NewJitRootStringPatch(const DexFile& dex_file, uint32_t dex_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000417
Andreas Gampe85b62f22015-09-09 13:15:38 -0700418 void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
Mark Mendell09ed1a32015-03-25 08:30:06 -0400419
Vladimir Marko58155012015-08-19 12:49:41 +0000420 // Emit linker patches.
421 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
422
Nicolas Geoffray997d1212016-11-09 10:36:29 +0000423 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
424
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100425 // Emit a write barrier.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100426 void MarkGCCard(Register temp,
427 Register card,
428 Register object,
429 Register value,
430 bool value_can_be_null);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100431
Roland Levillain7c1559a2015-12-15 10:55:36 +0000432 void GenerateMemoryBarrier(MemBarrierKind kind);
433
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100434 Label* GetLabelOf(HBasicBlock* block) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100435 return CommonGetLabelOf<Label>(block_labels_, block);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100436 }
437
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000438 void Initialize() OVERRIDE {
Vladimir Marko225b6462015-09-28 12:17:40 +0100439 block_labels_ = CommonInitializeLabels<Label>();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100440 }
441
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000442 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
443 return type == Primitive::kPrimLong;
444 }
445
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000446 bool ShouldSplitLongMoves() const OVERRIDE { return true; }
447
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000448 Label* GetFrameEntryLabel() { return &frame_entry_label_; }
449
Mark Mendellfb8d2792015-03-31 22:16:59 -0400450 const X86InstructionSetFeatures& GetInstructionSetFeatures() const {
451 return isa_features_;
452 }
453
Mark Mendell0616ae02015-04-17 12:49:27 -0400454 void SetMethodAddressOffset(int32_t offset) {
455 method_address_offset_ = offset;
456 }
457
458 int32_t GetMethodAddressOffset() const {
459 return method_address_offset_;
460 }
461
462 int32_t ConstantAreaStart() const {
463 return constant_area_start_;
464 }
465
466 Address LiteralDoubleAddress(double v, Register reg);
467 Address LiteralFloatAddress(float v, Register reg);
468 Address LiteralInt32Address(int32_t v, Register reg);
469 Address LiteralInt64Address(int64_t v, Register reg);
470
Aart Bika19616e2016-02-01 18:57:58 -0800471 // Load a 32-bit value into a register in the most efficient manner.
472 void Load32BitValue(Register dest, int32_t value);
473
474 // Compare a register with a 32-bit value in the most efficient manner.
475 void Compare32BitValue(Register dest, int32_t value);
476
Vladimir Marko56f4bdd2016-09-16 11:32:36 +0100477 // Compare int values. Supports only register locations for `lhs`.
478 void GenerateIntCompare(Location lhs, Location rhs);
jessicahandojo4877b792016-09-08 19:49:13 -0700479 void GenerateIntCompare(Register lhs, Location rhs);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +0100480
481 // Construct address for array access.
482 static Address ArrayAddress(Register obj,
483 Location index,
484 ScaleFactor scale,
485 uint32_t data_offset);
486
Mark Mendell805b3b52015-09-18 14:10:29 -0400487 Address LiteralCaseTable(HX86PackedSwitch* switch_instr, Register reg, Register value);
488
Mark Mendell0616ae02015-04-17 12:49:27 -0400489 void Finalize(CodeAllocator* allocator) OVERRIDE;
490
Roland Levillain7c1559a2015-12-15 10:55:36 +0000491 // Fast path implementation of ReadBarrier::Barrier for a heap
492 // reference field load when Baker's read barriers are used.
493 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000494 Location ref,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000495 Register obj,
496 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000497 bool needs_null_check);
498 // Fast path implementation of ReadBarrier::Barrier for a heap
499 // reference array load when Baker's read barriers are used.
500 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +0000501 Location ref,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 Register obj,
503 uint32_t data_offset,
504 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505 bool needs_null_check);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
507 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
508 //
509 // Load the object reference located at address `src`, held by
510 // object `obj`, into `ref`, and mark it if needed. The base of
511 // address `src` must be `obj`.
512 //
513 // If `always_update_field` is true, the value of the reference is
514 // atomically updated in the holder (`obj`). This operation
515 // requires a temporary register, which must be provided as a
516 // non-null pointer (`temp`).
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +0800517 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
518 Location ref,
519 Register obj,
520 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100521 bool needs_null_check,
522 bool always_update_field = false,
523 Register* temp = nullptr);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524
525 // Generate a read barrier for a heap reference within `instruction`
526 // using a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000527 //
528 // A read barrier for an object reference read from the heap is
529 // implemented as a call to the artReadBarrierSlow runtime entry
530 // point, which is passed the values in locations `ref`, `obj`, and
531 // `offset`:
532 //
533 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
534 // mirror::Object* obj,
535 // uint32_t offset);
536 //
537 // The `out` location contains the value returned by
538 // artReadBarrierSlow.
539 //
540 // When `index` is provided (i.e. for array accesses), the offset
541 // value passed to artReadBarrierSlow is adjusted to take `index`
542 // into account.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000543 void GenerateReadBarrierSlow(HInstruction* instruction,
544 Location out,
545 Location ref,
546 Location obj,
547 uint32_t offset,
548 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000549
Roland Levillain7c1559a2015-12-15 10:55:36 +0000550 // If read barriers are enabled, generate a read barrier for a heap
551 // reference using a slow path. If heap poisoning is enabled, also
552 // unpoison the reference in `out`.
553 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
554 Location out,
555 Location ref,
556 Location obj,
557 uint32_t offset,
558 Location index = Location::NoLocation());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000559
Roland Levillain7c1559a2015-12-15 10:55:36 +0000560 // Generate a read barrier for a GC root within `instruction` using
561 // a slow path.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000562 //
563 // A read barrier for an object reference GC root is implemented as
564 // a call to the artReadBarrierForRootSlow runtime entry point,
565 // which is passed the value in location `root`:
566 //
567 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
568 //
569 // The `out` location contains the value returned by
570 // artReadBarrierForRootSlow.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000571 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000572
Mark P Mendell17077d82015-12-16 19:15:59 +0000573 // Ensure that prior stores complete to memory before subsequent loads.
574 // The locked add implementation will avoid serializing device memory, but will
575 // touch (but not change) the top of the stack.
576 // The 'non_temporal' parameter should be used to ensure ordering of non-temporal stores.
577 void MemoryFence(bool non_temporal = false) {
Mark Mendell7aa04a12016-01-27 22:39:07 -0500578 if (!non_temporal) {
Mark P Mendell17077d82015-12-16 19:15:59 +0000579 assembler_.lock()->addl(Address(ESP, 0), Immediate(0));
580 } else {
581 assembler_.mfence();
582 }
583 }
584
Roland Levillainf41f9562016-09-14 19:26:48 +0100585 void GenerateNop() OVERRIDE;
586 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
587 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
Mark P Mendell17077d82015-12-16 19:15:59 +0000588
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000589 // When we don't know the proper offset for the value, we use kDummy32BitOffset.
590 // The correct value will be inserted when processing Assembler fixups.
591 static constexpr int32_t kDummy32BitOffset = 256;
592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100593 private:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000594 Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke, Register temp);
595
Vladimir Markoaad75c62016-10-03 08:46:48 +0000596 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
597 void EmitPcRelativeLinkerPatches(const ArenaDeque<PatchInfo<Label>>& infos,
598 ArenaVector<LinkerPatch>* linker_patches);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000599
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100600 // Labels for each block that will be compiled.
Vladimir Marko225b6462015-09-28 12:17:40 +0100601 Label* block_labels_; // Indexed by block id.
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000602 Label frame_entry_label_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000603 LocationsBuilderX86 location_builder_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000604 InstructionCodeGeneratorX86 instruction_visitor_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100605 ParallelMoveResolverX86 move_resolver_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000606 X86Assembler assembler_;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400607 const X86InstructionSetFeatures& isa_features_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000608
Vladimir Marko58155012015-08-19 12:49:41 +0000609 // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
Vladimir Markoaad75c62016-10-03 08:46:48 +0000610 ArenaDeque<PatchInfo<Label>> method_patches_;
611 ArenaDeque<PatchInfo<Label>> relative_call_patches_;
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000612 // PC-relative DexCache access info.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000613 ArenaDeque<PatchInfo<Label>> pc_relative_dex_cache_patches_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000614 // Patch locations for patchoat where the linker doesn't do any other work.
615 ArenaDeque<Label> simple_patches_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000616 // String patch locations; type depends on configuration (app .bss or boot image PIC/non-PIC).
617 ArenaDeque<PatchInfo<Label>> string_patches_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100618 // Type patch locations.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000619 ArenaDeque<PatchInfo<Label>> type_patches_;
Vladimir Marko58155012015-08-19 12:49:41 +0000620
Nicolas Geoffray997d1212016-11-09 10:36:29 +0000621 // Patches for string root accesses in JIT compiled code.
622 ArenaDeque<PatchInfo<Label>> jit_string_patches_;
623
Mark Mendell0616ae02015-04-17 12:49:27 -0400624 // Offset to the start of the constant area in the assembled code.
625 // Used for fixups to the constant area.
626 int32_t constant_area_start_;
627
Mark Mendell805b3b52015-09-18 14:10:29 -0400628 // Fixups for jump tables that need to be patched after the constant table is generated.
629 ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_;
630
Mark Mendell0616ae02015-04-17 12:49:27 -0400631 // If there is a HX86ComputeBaseMethodAddress instruction in the graph
632 // (which shall be the sole instruction of this kind), subtracting this offset
633 // from the value contained in the out register of this HX86ComputeBaseMethodAddress
634 // instruction gives the address of the start of this method.
635 int32_t method_address_offset_;
636
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000637 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86);
638};
639
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000640} // namespace x86
641} // namespace art
642
643#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_H_