blob: befe994efbb2f1e72ffc0643eed3c0357dfb23ef [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
Andreas Gampe71fb52f2014-12-29 17:43:08 -080040static constexpr bool kCoalescedImplicitNullCheck = false;
41
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010043 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010044 InvokeDexCallingConvention() : CallingConvention(
45 kParameterCoreRegisters,
46 kParameterCoreRegistersLength,
47 kParameterFloatRegisters,
48 kParameterFloatRegistersLength) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010049
50 private:
51 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
52};
53
54class InvokeDexCallingConventionVisitor {
55 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010056 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010057
58 Location GetNextLocation(Primitive::Type type);
59
60 private:
61 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062 // The current index for cpu registers.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010063 uint32_t gp_index_;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010064 // The current index for fpu registers.
65 uint32_t fp_index_;
66 // The current stack index.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010067 uint32_t stack_index_;
68
69 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
70};
71
72class CodeGeneratorX86_64;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080073
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};
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010087
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000088class ParallelMoveResolverX86_64 : public ParallelMoveResolver {
89 public:
90 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
91 : ParallelMoveResolver(allocator), codegen_(codegen) {}
92
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000093 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;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000097
98 X86_64Assembler* GetAssembler() const;
99
100 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100101 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100102 void Exchange32(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100103 void Exchange32(int mem1, int mem2);
104 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100105 void Exchange64(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100106 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000107
108 CodeGeneratorX86_64* const codegen_;
109
110 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
111};
112
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100113class LocationsBuilderX86_64 : public HGraphVisitor {
114 public:
115 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
116 : HGraphVisitor(graph), codegen_(codegen) {}
117
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100118#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000119 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100120
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100121 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100122
123#undef DECLARE_VISIT_INSTRUCTION
124
125 private:
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000126 void HandleInvoke(HInvoke* invoke);
127 void HandleBitwiseOperation(HBinaryOperation* operation);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000128 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000129 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
130 void HandleFieldGet(HInstruction* instruction);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000131
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100132 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
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100142#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000143 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100144
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100145 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100146
147#undef DECLARE_VISIT_INSTRUCTION
148
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100149 X86_64Assembler* GetAssembler() const { return assembler_; }
150
151 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 // 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);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000156 void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000157 void HandleBitwiseOperation(HBinaryOperation* operation);
Calin Juravlebacfec32014-11-14 15:54:36 +0000158 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000159 void HandleShift(HBinaryOperation* operation);
Calin Juravle52c48962014-12-16 17:02:57 +0000160 void GenerateMemoryBarrier(MemBarrierKind kind);
161 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
162 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000163 void GenerateImplicitNullCheck(HNullCheck* instruction);
164 void GenerateExplicitNullCheck(HNullCheck* instruction);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100166 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:
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000174 CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100175 virtual ~CodeGeneratorX86_64() {}
176
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000177 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;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100185
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000186 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100187 return kX86_64WordSize;
188 }
189
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500190 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
191 return kX86_64WordSize;
192 }
193
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000194 size_t FrameEntrySpillSize() const OVERRIDE;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100195
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000196 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100197 return &location_builder_;
198 }
199
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000200 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100201 return &instruction_visitor_;
202 }
203
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000204 X86_64Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100205 return &assembler_;
206 }
207
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000208 ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000209 return &move_resolver_;
210 }
211
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000212 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
213 return GetLabelOf(block)->Position();
214 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100215
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000216 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100217
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000218 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 {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100224 return InstructionSet::kX86_64;
225 }
226
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100227 // Emit a write barrier.
228 void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value);
229
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100230 // Helper method to move a value between two locations.
231 void Move(Location destination, Location source);
232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233 void LoadCurrentMethod(CpuRegister reg);
234
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100235 Label* GetLabelOf(HBasicBlock* block) const {
236 return block_labels_.GetRawStorage() + block->GetBlockId();
237 }
238
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000239 void Initialize() OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100240 block_labels_.SetSize(GetGraph()->GetBlocks().Size());
241 }
242
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000243 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
244 return false;
245 }
246
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800247 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, CpuRegister temp);
248
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100249 private:
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100250 // Labels for each block that will be compiled.
251 GrowableArray<Label> block_labels_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100252 LocationsBuilderX86_64 location_builder_;
253 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000254 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100255 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_