code_generator_x86_64.h revision ee3cf0731d0ef0787bc2947c8e3ca432b513956b
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
19
20#include "code_generator.h"
21#include "dex/compiler_enums.h"
22#include "driver/compiler_options.h"
23#include "nodes.h"
24#include "parallel_move_resolver.h"
25#include "utils/x86_64/assembler_x86_64.h"
26
27namespace art {
28namespace x86_64 {
29
30// Use a local definition to prevent copying mistakes.
31static constexpr size_t kX86_64WordSize = kX86_64PointerSize;
32
33// Some x86_64 instructions require a register to be available as temp.
34static constexpr Register TMP = R11;
35
36static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
37static constexpr FloatRegister kParameterFloatRegisters[] =
38    { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
39
40static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
41static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
42
43static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45    arraysize(kRuntimeParameterCoreRegisters);
46static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
47static constexpr size_t kRuntimeParameterFpuRegistersLength =
48    arraysize(kRuntimeParameterFpuRegisters);
49
50class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
51 public:
52  InvokeRuntimeCallingConvention()
53      : CallingConvention(kRuntimeParameterCoreRegisters,
54                          kRuntimeParameterCoreRegistersLength,
55                          kRuntimeParameterFpuRegisters,
56                          kRuntimeParameterFpuRegistersLength,
57                          kX86_64PointerSize) {}
58
59 private:
60  DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
61};
62
63class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
64 public:
65  InvokeDexCallingConvention() : CallingConvention(
66      kParameterCoreRegisters,
67      kParameterCoreRegistersLength,
68      kParameterFloatRegisters,
69      kParameterFloatRegistersLength,
70      kX86_64PointerSize) {}
71
72 private:
73  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
74};
75
76class FieldAccessCallingConventionX86_64 : public FieldAccessCallingConvention {
77 public:
78  FieldAccessCallingConventionX86_64() {}
79
80  Location GetObjectLocation() const OVERRIDE {
81    return Location::RegisterLocation(RSI);
82  }
83  Location GetFieldIndexLocation() const OVERRIDE {
84    return Location::RegisterLocation(RDI);
85  }
86  Location GetReturnLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
87    return Location::RegisterLocation(RAX);
88  }
89  Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
90    return Primitive::Is64BitType(type)
91        ? Location::RegisterLocation(RDX)
92        : (is_instance
93            ? Location::RegisterLocation(RDX)
94            : Location::RegisterLocation(RSI));
95  }
96  Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
97    return Location::FpuRegisterLocation(XMM0);
98  }
99
100 private:
101  DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionX86_64);
102};
103
104
105class InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor {
106 public:
107  InvokeDexCallingConventionVisitorX86_64() {}
108  virtual ~InvokeDexCallingConventionVisitorX86_64() {}
109
110  Location GetNextLocation(Primitive::Type type) OVERRIDE;
111  Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
112  Location GetMethodLocation() const OVERRIDE;
113
114 private:
115  InvokeDexCallingConvention calling_convention;
116
117  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64);
118};
119
120class CodeGeneratorX86_64;
121
122class ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
123 public:
124  ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
125      : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
126
127  void EmitMove(size_t index) OVERRIDE;
128  void EmitSwap(size_t index) OVERRIDE;
129  void SpillScratch(int reg) OVERRIDE;
130  void RestoreScratch(int reg) OVERRIDE;
131
132  X86_64Assembler* GetAssembler() const;
133
134 private:
135  void Exchange32(CpuRegister reg, int mem);
136  void Exchange32(XmmRegister reg, int mem);
137  void Exchange32(int mem1, int mem2);
138  void Exchange64(CpuRegister reg, int mem);
139  void Exchange64(XmmRegister reg, int mem);
140  void Exchange64(int mem1, int mem2);
141
142  CodeGeneratorX86_64* const codegen_;
143
144  DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
145};
146
147class LocationsBuilderX86_64 : public HGraphVisitor {
148 public:
149  LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
150      : HGraphVisitor(graph), codegen_(codegen) {}
151
152#define DECLARE_VISIT_INSTRUCTION(name, super)     \
153  void Visit##name(H##name* instr) OVERRIDE;
154
155  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
156  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
157
158#undef DECLARE_VISIT_INSTRUCTION
159
160  void VisitInstruction(HInstruction* instruction) OVERRIDE {
161    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
162               << " (id " << instruction->GetId() << ")";
163  }
164
165 private:
166  void HandleInvoke(HInvoke* invoke);
167  void HandleBitwiseOperation(HBinaryOperation* operation);
168  void HandleShift(HBinaryOperation* operation);
169  void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
170  void HandleFieldGet(HInstruction* instruction);
171
172  CodeGeneratorX86_64* const codegen_;
173  InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
174
175  DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
176};
177
178class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
179 public:
180  InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
181
182#define DECLARE_VISIT_INSTRUCTION(name, super)     \
183  void Visit##name(H##name* instr) OVERRIDE;
184
185  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
186  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
187
188#undef DECLARE_VISIT_INSTRUCTION
189
190  void VisitInstruction(HInstruction* instruction) OVERRIDE {
191    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
192               << " (id " << instruction->GetId() << ")";
193  }
194
195  X86_64Assembler* GetAssembler() const { return assembler_; }
196
197 private:
198  // Generate code for the given suspend check. If not null, `successor`
199  // is the block to branch to if the suspend check is not needed, and after
200  // the suspend call.
201  void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
202  void GenerateClassInitializationCheck(SlowPathCode* slow_path, CpuRegister class_reg);
203  void HandleBitwiseOperation(HBinaryOperation* operation);
204  void GenerateRemFP(HRem* rem);
205  void DivRemOneOrMinusOne(HBinaryOperation* instruction);
206  void DivByPowerOfTwo(HDiv* instruction);
207  void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
208  void GenerateDivRemIntegral(HBinaryOperation* instruction);
209  void HandleShift(HBinaryOperation* operation);
210  void GenerateMemoryBarrier(MemBarrierKind kind);
211  void HandleFieldSet(HInstruction* instruction,
212                      const FieldInfo& field_info,
213                      bool value_can_be_null);
214  void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
215  void GenerateImplicitNullCheck(HNullCheck* instruction);
216  void GenerateExplicitNullCheck(HNullCheck* instruction);
217  void PushOntoFPStack(Location source, uint32_t temp_offset,
218                       uint32_t stack_adjustment, bool is_float);
219  void GenerateTestAndBranch(HInstruction* instruction,
220                             Label* true_target,
221                             Label* false_target,
222                             Label* always_true_target);
223  void GenerateCompareTestAndBranch(HIf* if_inst,
224                                    HCondition* condition,
225                                    Label* true_target,
226                                    Label* false_target,
227                                    Label* always_true_target);
228  void GenerateFPJumps(HCondition* cond, Label* true_label, Label* false_label);
229  void HandleGoto(HInstruction* got, HBasicBlock* successor);
230
231  X86_64Assembler* const assembler_;
232  CodeGeneratorX86_64* const codegen_;
233
234  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
235};
236
237class CodeGeneratorX86_64 : public CodeGenerator {
238 public:
239  CodeGeneratorX86_64(HGraph* graph,
240                  const X86_64InstructionSetFeatures& isa_features,
241                  const CompilerOptions& compiler_options,
242                  OptimizingCompilerStats* stats = nullptr);
243  virtual ~CodeGeneratorX86_64() {}
244
245  void GenerateFrameEntry() OVERRIDE;
246  void GenerateFrameExit() OVERRIDE;
247  void Bind(HBasicBlock* block) OVERRIDE;
248  void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
249  void MoveConstant(Location destination, int32_t value) OVERRIDE;
250  void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
251  void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
252
253  size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
254  size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
255  size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
256  size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
257
258  // Generate code to invoke a runtime entry point.
259  void InvokeRuntime(QuickEntrypointEnum entrypoint,
260                     HInstruction* instruction,
261                     uint32_t dex_pc,
262                     SlowPathCode* slow_path) OVERRIDE;
263
264  void InvokeRuntime(int32_t entry_point_offset,
265                     HInstruction* instruction,
266                     uint32_t dex_pc,
267                     SlowPathCode* slow_path);
268
269  size_t GetWordSize() const OVERRIDE {
270    return kX86_64WordSize;
271  }
272
273  size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
274    return kX86_64WordSize;
275  }
276
277  HGraphVisitor* GetLocationBuilder() OVERRIDE {
278    return &location_builder_;
279  }
280
281  HGraphVisitor* GetInstructionVisitor() OVERRIDE {
282    return &instruction_visitor_;
283  }
284
285  X86_64Assembler* GetAssembler() OVERRIDE {
286    return &assembler_;
287  }
288
289  const X86_64Assembler& GetAssembler() const OVERRIDE {
290    return assembler_;
291  }
292
293  ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
294    return &move_resolver_;
295  }
296
297  uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
298    return GetLabelOf(block)->Position();
299  }
300
301  Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
302
303  void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
304  Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
305  void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
306  void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
307  void Finalize(CodeAllocator* allocator) OVERRIDE;
308
309  InstructionSet GetInstructionSet() const OVERRIDE {
310    return InstructionSet::kX86_64;
311  }
312
313  // Emit a write barrier.
314  void MarkGCCard(CpuRegister temp,
315                  CpuRegister card,
316                  CpuRegister object,
317                  CpuRegister value,
318                  bool value_can_be_null);
319
320  // Helper method to move a value between two locations.
321  void Move(Location destination, Location source);
322
323  Label* GetLabelOf(HBasicBlock* block) const {
324    return CommonGetLabelOf<Label>(block_labels_, block);
325  }
326
327  void Initialize() OVERRIDE {
328    block_labels_ = CommonInitializeLabels<Label>();
329  }
330
331  bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
332    return false;
333  }
334
335  void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
336  void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
337
338  void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
339
340  void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
341
342  const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
343    return isa_features_;
344  }
345
346  int ConstantAreaStart() const {
347    return constant_area_start_;
348  }
349
350  Address LiteralDoubleAddress(double v);
351  Address LiteralFloatAddress(float v);
352  Address LiteralInt32Address(int32_t v);
353  Address LiteralInt64Address(int64_t v);
354
355  // Load a 64 bit value into a register in the most efficient manner.
356  void Load64BitValue(CpuRegister dest, int64_t value);
357
358  // Store a 64 bit value into a DoubleStackSlot in the most efficient manner.
359  void Store64BitValueToStack(Location dest, int64_t value);
360
361 private:
362  struct PcRelativeDexCacheAccessInfo {
363    PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off)
364        : target_dex_file(dex_file), element_offset(element_off), label() { }
365
366    const DexFile& target_dex_file;
367    uint32_t element_offset;
368    Label label;
369  };
370
371  // Labels for each block that will be compiled.
372  Label* block_labels_;  // Indexed by block id.
373  Label frame_entry_label_;
374  LocationsBuilderX86_64 location_builder_;
375  InstructionCodeGeneratorX86_64 instruction_visitor_;
376  ParallelMoveResolverX86_64 move_resolver_;
377  X86_64Assembler assembler_;
378  const X86_64InstructionSetFeatures& isa_features_;
379
380  // Offset to the start of the constant area in the assembled code.
381  // Used for fixups to the constant area.
382  int constant_area_start_;
383
384  // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
385  ArenaDeque<MethodPatchInfo<Label>> method_patches_;
386  ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_;
387  // PC-relative DexCache access info.
388  ArenaDeque<PcRelativeDexCacheAccessInfo> pc_rel_dex_cache_patches_;
389
390  // When we don't know the proper offset for the value, we use kDummy32BitOffset.
391  // We will fix this up in the linker later to have the right value.
392  static constexpr int32_t kDummy32BitOffset = 256;
393
394  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
395};
396
397}  // namespace x86_64
398}  // namespace art
399
400#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
401