blob: 794b81ffbc8e898c676c38be37ce3ee3e87880c0 [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
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000028// Use a local definition to prevent copying mistakes.
29static constexpr size_t kX86_64WordSize = kX86_64PointerSize;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030
31static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010032static constexpr FloatRegister kParameterFloatRegisters[] =
33 { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010034
35static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010036static constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010037
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010038class InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010039 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010040 InvokeDexCallingConvention() : CallingConvention(
41 kParameterCoreRegisters,
42 kParameterCoreRegistersLength,
43 kParameterFloatRegisters,
44 kParameterFloatRegistersLength) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010045
46 private:
47 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
48};
49
50class InvokeDexCallingConventionVisitor {
51 public:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010053
54 Location GetNextLocation(Primitive::Type type);
55
56 private:
57 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010058 // The current index for cpu registers.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010059 uint32_t gp_index_;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010060 // The current index for fpu registers.
61 uint32_t fp_index_;
62 // The current stack index.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010063 uint32_t stack_index_;
64
65 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
66};
67
68class CodeGeneratorX86_64;
Nicolas Geoffray424f6762014-11-03 14:51:25 +000069class SlowPathCodeX86_64;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010070
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000071class ParallelMoveResolverX86_64 : public ParallelMoveResolver {
72 public:
73 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
74 : ParallelMoveResolver(allocator), codegen_(codegen) {}
75
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000076 void EmitMove(size_t index) OVERRIDE;
77 void EmitSwap(size_t index) OVERRIDE;
78 void SpillScratch(int reg) OVERRIDE;
79 void RestoreScratch(int reg) OVERRIDE;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000080
81 X86_64Assembler* GetAssembler() const;
82
83 private:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010084 void Exchange32(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010085 void Exchange32(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010086 void Exchange32(int mem1, int mem2);
87 void Exchange64(CpuRegister reg, int mem);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010088 void Exchange64(XmmRegister reg, int mem);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010089 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000090
91 CodeGeneratorX86_64* const codegen_;
92
93 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
94};
95
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010096class LocationsBuilderX86_64 : public HGraphVisitor {
97 public:
98 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
99 : HGraphVisitor(graph), codegen_(codegen) {}
100
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100101#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000102 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100103
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100104 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100105
106#undef DECLARE_VISIT_INSTRUCTION
107
108 private:
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000109 void HandleInvoke(HInvoke* invoke);
110 void HandleBitwiseOperation(HBinaryOperation* operation);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000111 void HandleShift(HBinaryOperation* operation);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000112
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100113 CodeGeneratorX86_64* const codegen_;
114 InvokeDexCallingConventionVisitor parameter_visitor_;
115
116 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
117};
118
119class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
120 public:
121 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
122
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100123#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000124 void Visit##name(H##name* instr) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100125
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100126 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100127
128#undef DECLARE_VISIT_INSTRUCTION
129
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100130 X86_64Assembler* GetAssembler() const { return assembler_; }
131
132 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 // Generate code for the given suspend check. If not null, `successor`
134 // is the block to branch to if the suspend check is not needed, and after
135 // the suspend call.
136 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000137 void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000138 void HandleBitwiseOperation(HBinaryOperation* operation);
Calin Juravlebacfec32014-11-14 15:54:36 +0000139 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000140 void HandleShift(HBinaryOperation* operation);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100141
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100142 X86_64Assembler* const assembler_;
143 CodeGeneratorX86_64* const codegen_;
144
145 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
146};
147
148class CodeGeneratorX86_64 : public CodeGenerator {
149 public:
150 explicit CodeGeneratorX86_64(HGraph* graph);
151 virtual ~CodeGeneratorX86_64() {}
152
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000153 void GenerateFrameEntry() OVERRIDE;
154 void GenerateFrameExit() OVERRIDE;
155 void Bind(HBasicBlock* block) OVERRIDE;
156 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
157 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
158 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
159 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
160 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100161
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000162 size_t GetWordSize() const OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100163 return kX86_64WordSize;
164 }
165
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000166 size_t FrameEntrySpillSize() const OVERRIDE;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100167
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000168 HGraphVisitor* GetLocationBuilder() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100169 return &location_builder_;
170 }
171
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000172 HGraphVisitor* GetInstructionVisitor() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100173 return &instruction_visitor_;
174 }
175
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000176 X86_64Assembler* GetAssembler() OVERRIDE {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100177 return &assembler_;
178 }
179
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000181 return &move_resolver_;
182 }
183
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000184 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
185 return GetLabelOf(block)->Position();
186 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100187
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000188 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100189
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000190 void SetupBlockedRegisters() const OVERRIDE;
191 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
192 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
193 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
194
195 InstructionSet GetInstructionSet() const OVERRIDE {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100196 return InstructionSet::kX86_64;
197 }
198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100199 // Emit a write barrier.
200 void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value);
201
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100202 // Helper method to move a value between two locations.
203 void Move(Location destination, Location source);
204
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205 void LoadCurrentMethod(CpuRegister reg);
206
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100207 Label* GetLabelOf(HBasicBlock* block) const {
208 return block_labels_.GetRawStorage() + block->GetBlockId();
209 }
210
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000211 void Initialize() OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100212 block_labels_.SetSize(GetGraph()->GetBlocks().Size());
213 }
214
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100215 private:
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100216 // Labels for each block that will be compiled.
217 GrowableArray<Label> block_labels_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100218 LocationsBuilderX86_64 location_builder_;
219 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000220 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100221 X86_64Assembler assembler_;
222
223 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
224};
225
226} // namespace x86_64
227} // namespace art
228
229#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_