blob: 375c0b03b90d53ed032ec99ff936db32b1bc41d1 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
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"
Calin Juravle52c48962014-12-16 17:02:57 +000021#include "dex/compiler_enums.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000022#include "driver/compiler_options.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010023#include "nodes.h"
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000024#include "parallel_move_resolver.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "utils/x86_64/assembler_x86_64.h"
26
27namespace art {
28namespace x86_64 {
29
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000030// Use a local definition to prevent copying mistakes.
31static constexpr size_t kX86_64WordSize = kX86_64PointerSize;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032
33static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010034static constexpr FloatRegister kParameterFloatRegisters[] =
35 { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036
37static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010038static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010039
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000040static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
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
55 private:
56 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
57};
58
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010059class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010060 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010061 InvokeDexCallingConvention() : CallingConvention(
62 kParameterCoreRegisters,
63 kParameterCoreRegistersLength,
64 kParameterFloatRegisters,
65 kParameterFloatRegistersLength) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010066
67 private:
68 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
69};
70
71class InvokeDexCallingConventionVisitor {
72 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010073 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010074
75 Location GetNextLocation(Primitive::Type type);
76
77 private:
78 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010079 // The current index for cpu registers.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010080 uint32_t gp_index_;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010081 // The current index for fpu registers.
82 uint32_t fp_index_;
83 // The current stack index.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010084 uint32_t stack_index_;
85
86 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
87};
88
89class CodeGeneratorX86_64;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080090
91class SlowPathCodeX86_64 : public SlowPathCode {
92 public:
93 SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
94
95 Label* GetEntryLabel() { return &entry_label_; }
96 Label* GetExitLabel() { return &exit_label_; }
97
98 private:
99 Label entry_label_;
100 Label exit_label_;
101
102 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
103};
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100104
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000105class ParallelMoveResolverX86_64 : public ParallelMoveResolver {
106 public:
107 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
108 : ParallelMoveResolver(allocator), codegen_(codegen) {}
109
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000110 void EmitMove(size_t index) OVERRIDE;
111 void EmitSwap(size_t index) OVERRIDE;
112 void SpillScratch(int reg) OVERRIDE;
113 void RestoreScratch(int reg) OVERRIDE;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000114
115 X86_64Assembler* GetAssembler() const;
116
117 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100118 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100119 void Exchange32(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100120 void Exchange32(int mem1, int mem2);
121 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100122 void Exchange64(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100123 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000124
125 CodeGeneratorX86_64* const codegen_;
126
127 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
128};
129
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100130class LocationsBuilderX86_64 : public HGraphVisitor {
131 public:
132 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
133 : HGraphVisitor(graph), codegen_(codegen) {}
134
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100135#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000136 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100137
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100138 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100139
140#undef DECLARE_VISIT_INSTRUCTION
141
142 private:
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000143 void HandleInvoke(HInvoke* invoke);
144 void HandleBitwiseOperation(HBinaryOperation* operation);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000145 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000146 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
147 void HandleFieldGet(HInstruction* instruction);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000148
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100149 CodeGeneratorX86_64* const codegen_;
150 InvokeDexCallingConventionVisitor parameter_visitor_;
151
152 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
153};
154
155class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
156 public:
157 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
158
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100159#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000160 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100161
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100162 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100163
164#undef DECLARE_VISIT_INSTRUCTION
165
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100166 X86_64Assembler* GetAssembler() const { return assembler_; }
167
168 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100169 // Generate code for the given suspend check. If not null, `successor`
170 // is the block to branch to if the suspend check is not needed, and after
171 // the suspend call.
172 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000173 void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000174 void HandleBitwiseOperation(HBinaryOperation* operation);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500175 void GenerateRemFP(HRem *rem);
Calin Juravlebacfec32014-11-14 15:54:36 +0000176 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000177 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000178 void GenerateMemoryBarrier(MemBarrierKind kind);
179 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
180 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000181 void GenerateImplicitNullCheck(HNullCheck* instruction);
182 void GenerateExplicitNullCheck(HNullCheck* instruction);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500183 void PushOntoFPStack(Location source, uint32_t temp_offset,
184 uint32_t stack_adjustment, bool is_float);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700185 void GenerateTestAndBranch(HInstruction* instruction,
186 Label* true_target,
187 Label* false_target,
188 Label* always_true_target);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100190 X86_64Assembler* const assembler_;
191 CodeGeneratorX86_64* const codegen_;
192
193 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
194};
195
196class CodeGeneratorX86_64 : public CodeGenerator {
197 public:
Mark Mendellfb8d2792015-03-31 22:16:59 -0400198 CodeGeneratorX86_64(HGraph* graph,
199 const X86_64InstructionSetFeatures& isa_features,
200 const CompilerOptions& compiler_options);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100201 virtual ~CodeGeneratorX86_64() {}
202
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000203 void GenerateFrameEntry() OVERRIDE;
204 void GenerateFrameExit() OVERRIDE;
205 void Bind(HBasicBlock* block) OVERRIDE;
206 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
207 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
208 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
209 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
210 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100211
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000212 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100213 return kX86_64WordSize;
214 }
215
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500216 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
217 return kX86_64WordSize;
218 }
219
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000220 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100221 return &location_builder_;
222 }
223
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000224 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100225 return &instruction_visitor_;
226 }
227
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000228 X86_64Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100229 return &assembler_;
230 }
231
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000232 ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000233 return &move_resolver_;
234 }
235
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000236 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
237 return GetLabelOf(block)->Position();
238 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100239
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000240 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100241
Nicolas Geoffray98893962015-01-21 12:32:32 +0000242 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000243 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
244 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
245 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
246
247 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100248 return InstructionSet::kX86_64;
249 }
250
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100251 // Emit a write barrier.
252 void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value);
253
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100254 // Helper method to move a value between two locations.
255 void Move(Location destination, Location source);
256
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100257 void LoadCurrentMethod(CpuRegister reg);
258
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100259 Label* GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000260 return CommonGetLabelOf<Label>(block_labels_.GetRawStorage(), block);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100261 }
262
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000263 void Initialize() OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100264 block_labels_.SetSize(GetGraph()->GetBlocks().Size());
265 }
266
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000267 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
268 return false;
269 }
270
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800271 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, CpuRegister temp);
272
Mark Mendellfb8d2792015-03-31 22:16:59 -0400273 const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
274 return isa_features_;
275 }
276
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100277 private:
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100278 // Labels for each block that will be compiled.
279 GrowableArray<Label> block_labels_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000280 Label frame_entry_label_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100281 LocationsBuilderX86_64 location_builder_;
282 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000283 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100284 X86_64Assembler assembler_;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400285 const X86_64InstructionSetFeatures& isa_features_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100286
287 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
288};
289
290} // namespace x86_64
291} // namespace art
292
293#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_