code_generator_x86_64.h revision e401d146407d61eeb99f8d6176b2ac13c4df1e33
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
33static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
34static constexpr FloatRegister kParameterFloatRegisters[] =
35    { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
36
37static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
38static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
39
40static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX };
41static constexpr size_t kRuntimeParameterCoreRegistersLength =
42    arraysize(kRuntimeParameterCoreRegisters);
43static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
44static constexpr size_t kRuntimeParameterFpuRegistersLength =
45    arraysize(kRuntimeParameterFpuRegisters);
46
47class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
48 public:
49  InvokeRuntimeCallingConvention()
50      : CallingConvention(kRuntimeParameterCoreRegisters,
51                          kRuntimeParameterCoreRegistersLength,
52                          kRuntimeParameterFpuRegisters,
53                          kRuntimeParameterFpuRegistersLength,
54                          kX86_64PointerSize) {}
55
56 private:
57  DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
60class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
61 public:
62  InvokeDexCallingConvention() : CallingConvention(
63      kParameterCoreRegisters,
64      kParameterCoreRegistersLength,
65      kParameterFloatRegisters,
66      kParameterFloatRegistersLength,
67      kX86_64PointerSize) {}
68
69 private:
70  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
71};
72
73class InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor {
74 public:
75  InvokeDexCallingConventionVisitorX86_64() {}
76  virtual ~InvokeDexCallingConventionVisitorX86_64() {}
77
78  Location GetNextLocation(Primitive::Type type) OVERRIDE;
79
80 private:
81  InvokeDexCallingConvention calling_convention;
82
83  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64);
84};
85
86class CodeGeneratorX86_64;
87
88class SlowPathCodeX86_64 : public SlowPathCode {
89 public:
90  SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
91
92  Label* GetEntryLabel() { return &entry_label_; }
93  Label* GetExitLabel() { return &exit_label_; }
94
95 private:
96  Label entry_label_;
97  Label exit_label_;
98
99  DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
100};
101
102class ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
103 public:
104  ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
105      : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
106
107  void EmitMove(size_t index) OVERRIDE;
108  void EmitSwap(size_t index) OVERRIDE;
109  void SpillScratch(int reg) OVERRIDE;
110  void RestoreScratch(int reg) OVERRIDE;
111
112  X86_64Assembler* GetAssembler() const;
113
114 private:
115  void Exchange32(CpuRegister reg, int mem);
116  void Exchange32(XmmRegister reg, int mem);
117  void Exchange32(int mem1, int mem2);
118  void Exchange64(CpuRegister reg, int mem);
119  void Exchange64(XmmRegister reg, int mem);
120  void Exchange64(int mem1, int mem2);
121
122  CodeGeneratorX86_64* const codegen_;
123
124  DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
125};
126
127class LocationsBuilderX86_64 : public HGraphVisitor {
128 public:
129  LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
130      : HGraphVisitor(graph), codegen_(codegen) {}
131
132#define DECLARE_VISIT_INSTRUCTION(name, super)     \
133  void Visit##name(H##name* instr) OVERRIDE;
134
135  FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
136
137#undef DECLARE_VISIT_INSTRUCTION
138
139 private:
140  void HandleInvoke(HInvoke* invoke);
141  void HandleBitwiseOperation(HBinaryOperation* operation);
142  void HandleShift(HBinaryOperation* operation);
143  void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
144  void HandleFieldGet(HInstruction* instruction);
145
146  CodeGeneratorX86_64* const codegen_;
147  InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
148
149  DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
150};
151
152class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
153 public:
154  InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
155
156#define DECLARE_VISIT_INSTRUCTION(name, super)     \
157  void Visit##name(H##name* instr) OVERRIDE;
158
159  FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
160
161#undef DECLARE_VISIT_INSTRUCTION
162
163  X86_64Assembler* GetAssembler() const { return assembler_; }
164
165 private:
166  // Generate code for the given suspend check. If not null, `successor`
167  // is the block to branch to if the suspend check is not needed, and after
168  // the suspend call.
169  void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
170  void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
171  void HandleBitwiseOperation(HBinaryOperation* operation);
172  void GenerateRemFP(HRem *rem);
173  void DivRemOneOrMinusOne(HBinaryOperation* instruction);
174  void DivByPowerOfTwo(HDiv* instruction);
175  void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
176  void GenerateDivRemIntegral(HBinaryOperation* instruction);
177  void HandleShift(HBinaryOperation* operation);
178  void GenerateMemoryBarrier(MemBarrierKind kind);
179  void HandleFieldSet(HInstruction* instruction,
180                      const FieldInfo& field_info,
181                      bool value_can_be_null);
182  void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
183  void GenerateImplicitNullCheck(HNullCheck* instruction);
184  void GenerateExplicitNullCheck(HNullCheck* instruction);
185  void PushOntoFPStack(Location source, uint32_t temp_offset,
186                       uint32_t stack_adjustment, bool is_float);
187  void GenerateTestAndBranch(HInstruction* instruction,
188                             Label* true_target,
189                             Label* false_target,
190                             Label* always_true_target);
191
192  X86_64Assembler* const assembler_;
193  CodeGeneratorX86_64* const codegen_;
194
195  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
196};
197
198class CodeGeneratorX86_64 : public CodeGenerator {
199 public:
200  CodeGeneratorX86_64(HGraph* graph,
201                  const X86_64InstructionSetFeatures& isa_features,
202                  const CompilerOptions& compiler_options);
203  virtual ~CodeGeneratorX86_64() {}
204
205  void GenerateFrameEntry() OVERRIDE;
206  void GenerateFrameExit() OVERRIDE;
207  void Bind(HBasicBlock* block) OVERRIDE;
208  void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
209  size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
210  size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
211  size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
212  size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
213
214  size_t GetWordSize() const OVERRIDE {
215    return kX86_64WordSize;
216  }
217
218  size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
219    return kX86_64WordSize;
220  }
221
222  HGraphVisitor* GetLocationBuilder() OVERRIDE {
223    return &location_builder_;
224  }
225
226  HGraphVisitor* GetInstructionVisitor() OVERRIDE {
227    return &instruction_visitor_;
228  }
229
230  X86_64Assembler* GetAssembler() OVERRIDE {
231    return &assembler_;
232  }
233
234  ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
235    return &move_resolver_;
236  }
237
238  uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
239    return GetLabelOf(block)->Position();
240  }
241
242  Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
243
244  void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
245  Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
246  void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
247  void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
248  void Finalize(CodeAllocator* allocator) OVERRIDE;
249
250  InstructionSet GetInstructionSet() const OVERRIDE {
251    return InstructionSet::kX86_64;
252  }
253
254  // Emit a write barrier.
255  void MarkGCCard(CpuRegister temp,
256                  CpuRegister card,
257                  CpuRegister object,
258                  CpuRegister value,
259                  bool value_can_be_null);
260
261  // Helper method to move a value between two locations.
262  void Move(Location destination, Location source);
263
264  void LoadCurrentMethod(CpuRegister reg);
265
266  Label* GetLabelOf(HBasicBlock* block) const {
267    return CommonGetLabelOf<Label>(block_labels_.GetRawStorage(), block);
268  }
269
270  void Initialize() OVERRIDE {
271    block_labels_.SetSize(GetGraph()->GetBlocks().Size());
272  }
273
274  bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
275    return false;
276  }
277
278  void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, CpuRegister temp);
279
280  const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
281    return isa_features_;
282  }
283
284  int ConstantAreaStart() const {
285    return constant_area_start_;
286  }
287
288  Address LiteralDoubleAddress(double v);
289  Address LiteralFloatAddress(float v);
290  Address LiteralInt32Address(int32_t v);
291  Address LiteralInt64Address(int64_t v);
292
293  // Load a 64 bit value into a register in the most efficient manner.
294  void Load64BitValue(CpuRegister dest, int64_t value);
295
296 private:
297  // Labels for each block that will be compiled.
298  GrowableArray<Label> block_labels_;
299  Label frame_entry_label_;
300  LocationsBuilderX86_64 location_builder_;
301  InstructionCodeGeneratorX86_64 instruction_visitor_;
302  ParallelMoveResolverX86_64 move_resolver_;
303  X86_64Assembler assembler_;
304  const X86_64InstructionSetFeatures& isa_features_;
305
306  // Offset to the start of the constant area in the assembled code.
307  // Used for fixups to the constant area.
308  int constant_area_start_;
309
310  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
311};
312
313}  // namespace x86_64
314}  // namespace art
315
316#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
317