blob: 4b90381f002aad33f4ac75eafa335efb11fc13fa [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
Jeff Hao848f70a2014-01-15 13:49:50 -080040static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX };
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000041static 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -070053 kRuntimeParameterFpuRegistersLength,
54 kX86_64PointerSize) {}
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010060class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010061 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062 InvokeDexCallingConvention() : CallingConvention(
63 kParameterCoreRegisters,
64 kParameterCoreRegistersLength,
65 kParameterFloatRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 kParameterFloatRegistersLength,
67 kX86_64PointerSize) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010068
69 private:
70 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
71};
72
Roland Levillain2d27c8e2015-04-28 15:48:45 +010073class InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010074 public:
Roland Levillain2d27c8e2015-04-28 15:48:45 +010075 InvokeDexCallingConventionVisitorX86_64() {}
76 virtual ~InvokeDexCallingConventionVisitorX86_64() {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010077
Roland Levillain2d27c8e2015-04-28 15:48:45 +010078 Location GetNextLocation(Primitive::Type type) OVERRIDE;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +010079 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
80 Location GetMethodLocation() const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010081
82 private:
83 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010084
Roland Levillain2d27c8e2015-04-28 15:48:45 +010085 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010086};
87
88class CodeGeneratorX86_64;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080089
90class SlowPathCodeX86_64 : public SlowPathCode {
91 public:
92 SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
93
94 Label* GetEntryLabel() { return &entry_label_; }
95 Label* GetExitLabel() { return &exit_label_; }
96
97 private:
98 Label entry_label_;
99 Label exit_label_;
100
101 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
102};
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100103
Zheng Xuad4450e2015-04-17 18:48:56 +0800104class ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000105 public:
106 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
Zheng Xuad4450e2015-04-17 18:48:56 +0800107 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000108
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000109 void EmitMove(size_t index) OVERRIDE;
110 void EmitSwap(size_t index) OVERRIDE;
111 void SpillScratch(int reg) OVERRIDE;
112 void RestoreScratch(int reg) OVERRIDE;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000113
114 X86_64Assembler* GetAssembler() const;
115
116 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100117 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100118 void Exchange32(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100119 void Exchange32(int mem1, int mem2);
120 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100121 void Exchange64(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100122 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000123
124 CodeGeneratorX86_64* const codegen_;
125
126 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
127};
128
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100129class LocationsBuilderX86_64 : public HGraphVisitor {
130 public:
131 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
132 : HGraphVisitor(graph), codegen_(codegen) {}
133
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100134#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000135 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100136
Alexandre Ramesef20f712015-06-09 10:29:30 +0100137 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
138 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100139
140#undef DECLARE_VISIT_INSTRUCTION
141
Alexandre Ramesef20f712015-06-09 10:29:30 +0100142 void VisitInstruction(HInstruction* instruction) OVERRIDE {
143 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
144 << " (id " << instruction->GetId() << ")";
145 }
146
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100147 private:
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000148 void HandleInvoke(HInvoke* invoke);
149 void HandleBitwiseOperation(HBinaryOperation* operation);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000150 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000151 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
152 void HandleFieldGet(HInstruction* instruction);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000153
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100154 CodeGeneratorX86_64* const codegen_;
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100155 InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100156
157 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
158};
159
160class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
161 public:
162 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
163
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100164#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000165 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100166
Alexandre Ramesef20f712015-06-09 10:29:30 +0100167 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
168 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100169
170#undef DECLARE_VISIT_INSTRUCTION
171
Alexandre Ramesef20f712015-06-09 10:29:30 +0100172 void VisitInstruction(HInstruction* instruction) OVERRIDE {
173 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
174 << " (id " << instruction->GetId() << ")";
175 }
176
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100177 X86_64Assembler* GetAssembler() const { return assembler_; }
178
179 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 // Generate code for the given suspend check. If not null, `successor`
181 // is the block to branch to if the suspend check is not needed, and after
182 // the suspend call.
183 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000184 void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000185 void HandleBitwiseOperation(HBinaryOperation* operation);
Mark Mendellc4701932015-04-10 13:18:51 -0400186 void GenerateRemFP(HRem* rem);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100187 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
Guillaume Sanchezb19930c2015-04-09 21:12:15 +0100188 void DivByPowerOfTwo(HDiv* instruction);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +0100189 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
Calin Juravlebacfec32014-11-14 15:54:36 +0000190 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000191 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000192 void GenerateMemoryBarrier(MemBarrierKind kind);
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100193 void HandleFieldSet(HInstruction* instruction,
194 const FieldInfo& field_info,
195 bool value_can_be_null);
Calin Juravle52c48962014-12-16 17:02:57 +0000196 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000197 void GenerateImplicitNullCheck(HNullCheck* instruction);
198 void GenerateExplicitNullCheck(HNullCheck* instruction);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500199 void PushOntoFPStack(Location source, uint32_t temp_offset,
200 uint32_t stack_adjustment, bool is_float);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700201 void GenerateTestAndBranch(HInstruction* instruction,
202 Label* true_target,
203 Label* false_target,
204 Label* always_true_target);
Mark Mendellc4701932015-04-10 13:18:51 -0400205 void GenerateCompareTestAndBranch(HIf* if_inst,
206 HCondition* condition,
207 Label* true_target,
208 Label* false_target,
209 Label* always_true_target);
210 void GenerateFPJumps(HCondition* cond, Label* true_label, Label* false_label);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000211 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100212
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100213 X86_64Assembler* const assembler_;
214 CodeGeneratorX86_64* const codegen_;
215
216 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
217};
218
219class CodeGeneratorX86_64 : public CodeGenerator {
220 public:
Mark Mendellfb8d2792015-03-31 22:16:59 -0400221 CodeGeneratorX86_64(HGraph* graph,
222 const X86_64InstructionSetFeatures& isa_features,
223 const CompilerOptions& compiler_options);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100224 virtual ~CodeGeneratorX86_64() {}
225
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000226 void GenerateFrameEntry() OVERRIDE;
227 void GenerateFrameExit() OVERRIDE;
228 void Bind(HBasicBlock* block) OVERRIDE;
229 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
230 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
231 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
232 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
233 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100234
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000235 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100236 return kX86_64WordSize;
237 }
238
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500239 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
240 return kX86_64WordSize;
241 }
242
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000243 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100244 return &location_builder_;
245 }
246
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000247 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100248 return &instruction_visitor_;
249 }
250
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000251 X86_64Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100252 return &assembler_;
253 }
254
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100255 const X86_64Assembler& GetAssembler() const OVERRIDE {
256 return assembler_;
257 }
258
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000259 ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000260 return &move_resolver_;
261 }
262
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000263 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
264 return GetLabelOf(block)->Position();
265 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100266
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000267 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100268
Nicolas Geoffray98893962015-01-21 12:32:32 +0000269 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000270 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
271 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
272 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Mark Mendellf55c3e02015-03-26 21:07:46 -0400273 void Finalize(CodeAllocator* allocator) OVERRIDE;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000274
275 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100276 return InstructionSet::kX86_64;
277 }
278
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100279 // Emit a write barrier.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100280 void MarkGCCard(CpuRegister temp,
281 CpuRegister card,
282 CpuRegister object,
283 CpuRegister value,
284 bool value_can_be_null);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100285
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100286 // Helper method to move a value between two locations.
287 void Move(Location destination, Location source);
288
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100289 Label* GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000290 return CommonGetLabelOf<Label>(block_labels_.GetRawStorage(), block);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100291 }
292
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000293 void Initialize() OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100294 block_labels_.SetSize(GetGraph()->GetBlocks().Size());
295 }
296
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000297 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
298 return false;
299 }
300
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100301 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800302
Mark Mendellfb8d2792015-03-31 22:16:59 -0400303 const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
304 return isa_features_;
305 }
306
Mark Mendellf55c3e02015-03-26 21:07:46 -0400307 int ConstantAreaStart() const {
308 return constant_area_start_;
309 }
310
311 Address LiteralDoubleAddress(double v);
312 Address LiteralFloatAddress(float v);
313 Address LiteralInt32Address(int32_t v);
314 Address LiteralInt64Address(int64_t v);
315
Mark Mendell92e83bf2015-05-07 11:25:03 -0400316 // Load a 64 bit value into a register in the most efficient manner.
317 void Load64BitValue(CpuRegister dest, int64_t value);
318
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100319 private:
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100320 // Labels for each block that will be compiled.
321 GrowableArray<Label> block_labels_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000322 Label frame_entry_label_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100323 LocationsBuilderX86_64 location_builder_;
324 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000325 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100326 X86_64Assembler assembler_;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400327 const X86_64InstructionSetFeatures& isa_features_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100328
Mark Mendell39dcf552015-04-09 20:42:42 -0400329 // Offset to the start of the constant area in the assembled code.
Mark Mendellf55c3e02015-03-26 21:07:46 -0400330 // Used for fixups to the constant area.
331 int constant_area_start_;
332
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100333 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
334};
335
336} // namespace x86_64
337} // namespace art
338
339#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_