blob: 32d2702d72d8ab87dcf16c48a61ec709082a383b [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;
68
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000069class ParallelMoveResolverX86_64 : public ParallelMoveResolver {
70 public:
71 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
72 : ParallelMoveResolver(allocator), codegen_(codegen) {}
73
74 virtual void EmitMove(size_t index) OVERRIDE;
75 virtual void EmitSwap(size_t index) OVERRIDE;
76 virtual void SpillScratch(int reg) OVERRIDE;
77 virtual void RestoreScratch(int reg) OVERRIDE;
78
79 X86_64Assembler* GetAssembler() const;
80
81 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010082 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010083 void Exchange32(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010084 void Exchange32(int mem1, int mem2);
85 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010086 void Exchange64(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010087 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000088
89 CodeGeneratorX86_64* const codegen_;
90
91 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
92};
93
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010094class LocationsBuilderX86_64 : public HGraphVisitor {
95 public:
96 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
97 : HGraphVisitor(graph), codegen_(codegen) {}
98
Nicolas Geoffray360231a2014-10-08 21:07:48 +010099#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100100 virtual void Visit##name(H##name* instr);
101
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100102 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100103
104#undef DECLARE_VISIT_INSTRUCTION
105
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100106 void HandleInvoke(HInvoke* invoke);
107
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100108 private:
109 CodeGeneratorX86_64* const codegen_;
110 InvokeDexCallingConventionVisitor parameter_visitor_;
111
112 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
113};
114
115class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
116 public:
117 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
118
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100119#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100120 virtual void Visit##name(H##name* instr);
121
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100122 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100123
124#undef DECLARE_VISIT_INSTRUCTION
125
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100126 X86_64Assembler* GetAssembler() const { return assembler_; }
127
128 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 // Generate code for the given suspend check. If not null, `successor`
130 // is the block to branch to if the suspend check is not needed, and after
131 // the suspend call.
132 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
133
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100134 X86_64Assembler* const assembler_;
135 CodeGeneratorX86_64* const codegen_;
136
137 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
138};
139
140class CodeGeneratorX86_64 : public CodeGenerator {
141 public:
142 explicit CodeGeneratorX86_64(HGraph* graph);
143 virtual ~CodeGeneratorX86_64() {}
144
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100145 virtual void GenerateFrameEntry() OVERRIDE;
146 virtual void GenerateFrameExit() OVERRIDE;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 virtual void Bind(HBasicBlock* block) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100148 virtual void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100149 virtual size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
150 virtual size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
151 virtual size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
152 virtual size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100153
154 virtual size_t GetWordSize() const OVERRIDE {
155 return kX86_64WordSize;
156 }
157
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100158 virtual size_t FrameEntrySpillSize() const OVERRIDE;
159
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100160 virtual HGraphVisitor* GetLocationBuilder() OVERRIDE {
161 return &location_builder_;
162 }
163
164 virtual HGraphVisitor* GetInstructionVisitor() OVERRIDE {
165 return &instruction_visitor_;
166 }
167
168 virtual X86_64Assembler* GetAssembler() OVERRIDE {
169 return &assembler_;
170 }
171
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000172 ParallelMoveResolverX86_64* GetMoveResolver() {
173 return &move_resolver_;
174 }
175
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100176 virtual Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
177
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100178 virtual void SetupBlockedRegisters() const OVERRIDE;
179 virtual Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100180 virtual void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
181 virtual void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
182
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100183 virtual InstructionSet GetInstructionSet() const OVERRIDE {
184 return InstructionSet::kX86_64;
185 }
186
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 // Emit a write barrier.
188 void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value);
189
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100190 // Helper method to move a value between two locations.
191 void Move(Location destination, Location source);
192
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193 void LoadCurrentMethod(CpuRegister reg);
194
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100195 Label* GetLabelOf(HBasicBlock* block) const {
196 return block_labels_.GetRawStorage() + block->GetBlockId();
197 }
198
199 virtual void Initialize() OVERRIDE {
200 block_labels_.SetSize(GetGraph()->GetBlocks().Size());
201 }
202
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100203 private:
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100204 // Labels for each block that will be compiled.
205 GrowableArray<Label> block_labels_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100206 LocationsBuilderX86_64 location_builder_;
207 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000208 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100209 X86_64Assembler assembler_;
210
211 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
212};
213
214} // namespace x86_64
215} // namespace art
216
217#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_