blob: 9565b6f8767e39a5e80e434aeaed0aaf55c3f093 [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"
21#include "nodes.h"
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000022#include "parallel_move_resolver.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010023#include "utils/x86_64/assembler_x86_64.h"
24
25namespace art {
26namespace x86_64 {
27
28static constexpr size_t kX86_64WordSize = 8;
29
30static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010031static constexpr FloatRegister kParameterFloatRegisters[] =
32 { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033
34static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010035static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010037class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010038 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010039 InvokeDexCallingConvention() : CallingConvention(
40 kParameterCoreRegisters,
41 kParameterCoreRegistersLength,
42 kParameterFloatRegisters,
43 kParameterFloatRegistersLength) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010044
45 private:
46 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
47};
48
49class InvokeDexCallingConventionVisitor {
50 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010051 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010052
53 Location GetNextLocation(Primitive::Type type);
54
55 private:
56 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057 // The current index for cpu registers.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010058 uint32_t gp_index_;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010059 // The current index for fpu registers.
60 uint32_t fp_index_;
61 // The current stack index.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010062 uint32_t stack_index_;
63
64 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
65};
66
67class CodeGeneratorX86_64;
Nicolas Geoffray424f6762014-11-03 14:51:25 +000068class SlowPathCodeX86_64;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010069
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000070class ParallelMoveResolverX86_64 : public ParallelMoveResolver {
71 public:
72 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
73 : ParallelMoveResolver(allocator), codegen_(codegen) {}
74
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000075 void EmitMove(size_t index) OVERRIDE;
76 void EmitSwap(size_t index) OVERRIDE;
77 void SpillScratch(int reg) OVERRIDE;
78 void RestoreScratch(int reg) OVERRIDE;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000079
80 X86_64Assembler* GetAssembler() const;
81
82 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010083 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010084 void Exchange32(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010085 void Exchange32(int mem1, int mem2);
86 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010087 void Exchange64(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010088 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000089
90 CodeGeneratorX86_64* const codegen_;
91
92 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
93};
94
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010095class LocationsBuilderX86_64 : public HGraphVisitor {
96 public:
97 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
98 : HGraphVisitor(graph), codegen_(codegen) {}
99
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100100#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000101 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100102
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100103 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100104
105#undef DECLARE_VISIT_INSTRUCTION
106
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100107 void HandleInvoke(HInvoke* invoke);
108
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100109 private:
110 CodeGeneratorX86_64* const codegen_;
111 InvokeDexCallingConventionVisitor parameter_visitor_;
112
113 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
114};
115
116class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
117 public:
118 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
119
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100120#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000121 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100122
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100123 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100124
125#undef DECLARE_VISIT_INSTRUCTION
126
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100127 X86_64Assembler* GetAssembler() const { return assembler_; }
128
129 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100130 // Generate code for the given suspend check. If not null, `successor`
131 // is the block to branch to if the suspend check is not needed, and after
132 // the suspend call.
133 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000134 void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100136 X86_64Assembler* const assembler_;
137 CodeGeneratorX86_64* const codegen_;
138
139 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
140};
141
142class CodeGeneratorX86_64 : public CodeGenerator {
143 public:
144 explicit CodeGeneratorX86_64(HGraph* graph);
145 virtual ~CodeGeneratorX86_64() {}
146
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000147 void GenerateFrameEntry() OVERRIDE;
148 void GenerateFrameExit() OVERRIDE;
149 void Bind(HBasicBlock* block) OVERRIDE;
150 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
151 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
152 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
153 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
154 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100155
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000156 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100157 return kX86_64WordSize;
158 }
159
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000160 size_t FrameEntrySpillSize() const OVERRIDE;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100161
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000162 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100163 return &location_builder_;
164 }
165
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000166 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100167 return &instruction_visitor_;
168 }
169
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000170 X86_64Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100171 return &assembler_;
172 }
173
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000174 ParallelMoveResolverX86_64* GetMoveResolver() {
175 return &move_resolver_;
176 }
177
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000178 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
179 return GetLabelOf(block)->Position();
180 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100181
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000182 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100183
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000184 void SetupBlockedRegisters() const OVERRIDE;
185 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
186 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
187 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
188
189 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100190 return InstructionSet::kX86_64;
191 }
192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100193 // Emit a write barrier.
194 void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value);
195
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100196 // Helper method to move a value between two locations.
197 void Move(Location destination, Location source);
198
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100199 void LoadCurrentMethod(CpuRegister reg);
200
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100201 Label* GetLabelOf(HBasicBlock* block) const {
202 return block_labels_.GetRawStorage() + block->GetBlockId();
203 }
204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000205 void Initialize() OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100206 block_labels_.SetSize(GetGraph()->GetBlocks().Size());
207 }
208
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100209 private:
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100210 // Labels for each block that will be compiled.
211 GrowableArray<Label> block_labels_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100212 LocationsBuilderX86_64 location_builder_;
213 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000214 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100215 X86_64Assembler assembler_;
216
217 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
218};
219
220} // namespace x86_64
221} // namespace art
222
223#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_