code_generator_x86_64.h revision cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3f
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 bool kCoalescedImplicitNullCheck = false;
41
42class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
43 public:
44  InvokeDexCallingConvention() : CallingConvention(
45      kParameterCoreRegisters,
46      kParameterCoreRegistersLength,
47      kParameterFloatRegisters,
48      kParameterFloatRegistersLength) {}
49
50 private:
51  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
52};
53
54class InvokeDexCallingConventionVisitor {
55 public:
56  InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
57
58  Location GetNextLocation(Primitive::Type type);
59
60 private:
61  InvokeDexCallingConvention calling_convention;
62  // The current index for cpu registers.
63  uint32_t gp_index_;
64  // The current index for fpu registers.
65  uint32_t fp_index_;
66  // The current stack index.
67  uint32_t stack_index_;
68
69  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
70};
71
72class CodeGeneratorX86_64;
73
74class SlowPathCodeX86_64 : public SlowPathCode {
75 public:
76  SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
77
78  Label* GetEntryLabel() { return &entry_label_; }
79  Label* GetExitLabel() { return &exit_label_; }
80
81 private:
82  Label entry_label_;
83  Label exit_label_;
84
85  DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
86};
87
88class ParallelMoveResolverX86_64 : public ParallelMoveResolver {
89 public:
90  ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
91      : ParallelMoveResolver(allocator), codegen_(codegen) {}
92
93  void EmitMove(size_t index) OVERRIDE;
94  void EmitSwap(size_t index) OVERRIDE;
95  void SpillScratch(int reg) OVERRIDE;
96  void RestoreScratch(int reg) OVERRIDE;
97
98  X86_64Assembler* GetAssembler() const;
99
100 private:
101  void Exchange32(CpuRegister reg, int mem);
102  void Exchange32(XmmRegister reg, int mem);
103  void Exchange32(int mem1, int mem2);
104  void Exchange64(CpuRegister reg, int mem);
105  void Exchange64(XmmRegister reg, int mem);
106  void Exchange64(int mem1, int mem2);
107
108  CodeGeneratorX86_64* const codegen_;
109
110  DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
111};
112
113class LocationsBuilderX86_64 : public HGraphVisitor {
114 public:
115  LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
116      : HGraphVisitor(graph), codegen_(codegen) {}
117
118#define DECLARE_VISIT_INSTRUCTION(name, super)     \
119  void Visit##name(H##name* instr) OVERRIDE;
120
121  FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
122
123#undef DECLARE_VISIT_INSTRUCTION
124
125 private:
126  void HandleInvoke(HInvoke* invoke);
127  void HandleBitwiseOperation(HBinaryOperation* operation);
128  void HandleShift(HBinaryOperation* operation);
129  void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
130  void HandleFieldGet(HInstruction* instruction);
131
132  CodeGeneratorX86_64* const codegen_;
133  InvokeDexCallingConventionVisitor parameter_visitor_;
134
135  DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
136};
137
138class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
139 public:
140  InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
141
142#define DECLARE_VISIT_INSTRUCTION(name, super)     \
143  void Visit##name(H##name* instr) OVERRIDE;
144
145  FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
146
147#undef DECLARE_VISIT_INSTRUCTION
148
149  X86_64Assembler* GetAssembler() const { return assembler_; }
150
151 private:
152  // Generate code for the given suspend check. If not null, `successor`
153  // is the block to branch to if the suspend check is not needed, and after
154  // the suspend call.
155  void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
156  void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
157  void HandleBitwiseOperation(HBinaryOperation* operation);
158  void GenerateDivRemIntegral(HBinaryOperation* instruction);
159  void HandleShift(HBinaryOperation* operation);
160  void GenerateMemoryBarrier(MemBarrierKind kind);
161  void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
162  void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
163  void GenerateImplicitNullCheck(HNullCheck* instruction);
164  void GenerateExplicitNullCheck(HNullCheck* instruction);
165
166  X86_64Assembler* const assembler_;
167  CodeGeneratorX86_64* const codegen_;
168
169  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
170};
171
172class CodeGeneratorX86_64 : public CodeGenerator {
173 public:
174  CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options);
175  virtual ~CodeGeneratorX86_64() {}
176
177  void GenerateFrameEntry() OVERRIDE;
178  void GenerateFrameExit() OVERRIDE;
179  void Bind(HBasicBlock* block) OVERRIDE;
180  void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
181  size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
182  size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
183  size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
184  size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
185
186  size_t GetWordSize() const OVERRIDE {
187    return kX86_64WordSize;
188  }
189
190  size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
191    return kX86_64WordSize;
192  }
193
194  size_t FrameEntrySpillSize() const OVERRIDE;
195
196  HGraphVisitor* GetLocationBuilder() OVERRIDE {
197    return &location_builder_;
198  }
199
200  HGraphVisitor* GetInstructionVisitor() OVERRIDE {
201    return &instruction_visitor_;
202  }
203
204  X86_64Assembler* GetAssembler() OVERRIDE {
205    return &assembler_;
206  }
207
208  ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
209    return &move_resolver_;
210  }
211
212  uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
213    return GetLabelOf(block)->Position();
214  }
215
216  Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
217
218  void SetupBlockedRegisters() const OVERRIDE;
219  Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
220  void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
221  void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
222
223  InstructionSet GetInstructionSet() const OVERRIDE {
224    return InstructionSet::kX86_64;
225  }
226
227  // Emit a write barrier.
228  void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value);
229
230  // Helper method to move a value between two locations.
231  void Move(Location destination, Location source);
232
233  void LoadCurrentMethod(CpuRegister reg);
234
235  Label* GetLabelOf(HBasicBlock* block) const {
236    return block_labels_.GetRawStorage() + block->GetBlockId();
237  }
238
239  void Initialize() OVERRIDE {
240    block_labels_.SetSize(GetGraph()->GetBlocks().Size());
241  }
242
243  bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
244    return false;
245  }
246
247  void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, CpuRegister temp);
248
249 private:
250  // Labels for each block that will be compiled.
251  GrowableArray<Label> block_labels_;
252  LocationsBuilderX86_64 location_builder_;
253  InstructionCodeGeneratorX86_64 instruction_visitor_;
254  ParallelMoveResolverX86_64 move_resolver_;
255  X86_64Assembler assembler_;
256
257  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
258};
259
260}  // namespace x86_64
261}  // namespace art
262
263#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
264