blob: 0f0a9540baa3ef25046ebc6d0452802b7aecd297 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 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_ARM_VIXL_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM_VIXL_H_
19
Artem Serovd4cc5b22016-11-04 11:19:09 +000020#include "base/enums.h"
21#include "code_generator.h"
Artem Serovcfbe9132016-10-14 15:58:56 +010022#include "common_arm.h"
Artem Serovd4cc5b22016-11-04 11:19:09 +000023#include "driver/compiler_options.h"
24#include "nodes.h"
25#include "string_reference.h"
26#include "parallel_move_resolver.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010027#include "utils/arm/assembler_arm_vixl.h"
Artem Serovd4cc5b22016-11-04 11:19:09 +000028#include "utils/type_reference.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010029
30// TODO(VIXL): make vixl clean wrt -Wshadow.
31#pragma GCC diagnostic push
32#pragma GCC diagnostic ignored "-Wshadow"
33#include "aarch32/constants-aarch32.h"
34#include "aarch32/instructions-aarch32.h"
35#include "aarch32/macro-assembler-aarch32.h"
36#pragma GCC diagnostic pop
37
38// True if VIXL32 should be used for codegen on ARM.
Scott Wakelinga7812ae2016-10-17 10:03:36 +010039#ifdef ART_USE_VIXL_ARM_BACKEND
Scott Wakelingfe885462016-09-22 10:24:38 +010040static constexpr bool kArmUseVIXL32 = true;
41#else
42static constexpr bool kArmUseVIXL32 = false;
43#endif
44
45namespace art {
46namespace arm {
47
Scott Wakelinga7812ae2016-10-17 10:03:36 +010048static const vixl::aarch32::Register kParameterCoreRegistersVIXL[] = {
49 vixl::aarch32::r1,
50 vixl::aarch32::r2,
51 vixl::aarch32::r3
52};
Artem Serovd4cc5b22016-11-04 11:19:09 +000053static const size_t kParameterCoreRegistersLengthVIXL = arraysize(kParameterCoreRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +010054static const vixl::aarch32::SRegister kParameterFpuRegistersVIXL[] = {
55 vixl::aarch32::s0,
56 vixl::aarch32::s1,
57 vixl::aarch32::s2,
58 vixl::aarch32::s3,
59 vixl::aarch32::s4,
60 vixl::aarch32::s5,
61 vixl::aarch32::s6,
62 vixl::aarch32::s7,
63 vixl::aarch32::s8,
64 vixl::aarch32::s9,
65 vixl::aarch32::s10,
66 vixl::aarch32::s11,
67 vixl::aarch32::s12,
68 vixl::aarch32::s13,
69 vixl::aarch32::s14,
70 vixl::aarch32::s15
71};
Artem Serovd4cc5b22016-11-04 11:19:09 +000072static const size_t kParameterFpuRegistersLengthVIXL = arraysize(kParameterFpuRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +010073
Scott Wakelingfe885462016-09-22 10:24:38 +010074static const vixl::aarch32::Register kMethodRegister = vixl::aarch32::r0;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010075
Scott Wakelingfe885462016-09-22 10:24:38 +010076static const vixl::aarch32::Register kCoreAlwaysSpillRegister = vixl::aarch32::r5;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010077
78// Callee saves core registers r5, r6, r7, r8, r10, r11, and lr.
79static const vixl::aarch32::RegisterList kCoreCalleeSaves = vixl::aarch32::RegisterList::Union(
80 vixl::aarch32::RegisterList(vixl::aarch32::r5,
81 vixl::aarch32::r6,
82 vixl::aarch32::r7,
83 vixl::aarch32::r8),
84 vixl::aarch32::RegisterList(vixl::aarch32::r10,
85 vixl::aarch32::r11,
86 vixl::aarch32::lr));
87
88// Callee saves FP registers s16 to s31 inclusive.
Scott Wakelingfe885462016-09-22 10:24:38 +010089static const vixl::aarch32::SRegisterList kFpuCalleeSaves =
90 vixl::aarch32::SRegisterList(vixl::aarch32::s16, 16);
91
Scott Wakelinga7812ae2016-10-17 10:03:36 +010092static const vixl::aarch32::Register kRuntimeParameterCoreRegistersVIXL[] = {
93 vixl::aarch32::r0,
94 vixl::aarch32::r1,
95 vixl::aarch32::r2,
96 vixl::aarch32::r3
97};
98static const size_t kRuntimeParameterCoreRegistersLengthVIXL =
Artem Serovd4cc5b22016-11-04 11:19:09 +000099 arraysize(kRuntimeParameterCoreRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100100static const vixl::aarch32::SRegister kRuntimeParameterFpuRegistersVIXL[] = {
101 vixl::aarch32::s0,
102 vixl::aarch32::s1,
103 vixl::aarch32::s2,
104 vixl::aarch32::s3
105};
106static const size_t kRuntimeParameterFpuRegistersLengthVIXL =
Artem Serovd4cc5b22016-11-04 11:19:09 +0000107 arraysize(kRuntimeParameterFpuRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100108
109class LoadClassSlowPathARMVIXL;
Scott Wakelingfe885462016-09-22 10:24:38 +0100110class CodeGeneratorARMVIXL;
111
Artem Serovc5fcb442016-12-02 19:19:58 +0000112using VIXLInt32Literal = vixl::aarch32::Literal<int32_t>;
113using VIXLUInt32Literal = vixl::aarch32::Literal<uint32_t>;
114
Artem Serov551b28f2016-10-18 19:11:30 +0100115class JumpTableARMVIXL : public DeletableArenaObject<kArenaAllocSwitchTable> {
116 public:
117 explicit JumpTableARMVIXL(HPackedSwitch* switch_instr)
Artem Serov09a940d2016-11-11 16:15:11 +0000118 : switch_instr_(switch_instr),
119 table_start_(),
120 bb_addresses_(switch_instr->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
121 uint32_t num_entries = switch_instr_->GetNumEntries();
122 for (uint32_t i = 0; i < num_entries; i++) {
Artem Serovc5fcb442016-12-02 19:19:58 +0000123 VIXLInt32Literal *lit = new VIXLInt32Literal(0, vixl32::RawLiteral::kManuallyPlaced);
Artem Serov09a940d2016-11-11 16:15:11 +0000124 bb_addresses_.emplace_back(lit);
125 }
126 }
Artem Serov551b28f2016-10-18 19:11:30 +0100127
128 vixl::aarch32::Label* GetTableStartLabel() { return &table_start_; }
129
130 void EmitTable(CodeGeneratorARMVIXL* codegen);
Artem Serov09a940d2016-11-11 16:15:11 +0000131 void FixTable(CodeGeneratorARMVIXL* codegen);
Artem Serov551b28f2016-10-18 19:11:30 +0100132
133 private:
134 HPackedSwitch* const switch_instr_;
135 vixl::aarch32::Label table_start_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000136 ArenaVector<std::unique_ptr<VIXLInt32Literal>> bb_addresses_;
Artem Serov551b28f2016-10-18 19:11:30 +0100137
138 DISALLOW_COPY_AND_ASSIGN(JumpTableARMVIXL);
139};
140
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100141class InvokeRuntimeCallingConventionARMVIXL
142 : public CallingConvention<vixl::aarch32::Register, vixl::aarch32::SRegister> {
143 public:
144 InvokeRuntimeCallingConventionARMVIXL()
145 : CallingConvention(kRuntimeParameterCoreRegistersVIXL,
146 kRuntimeParameterCoreRegistersLengthVIXL,
147 kRuntimeParameterFpuRegistersVIXL,
148 kRuntimeParameterFpuRegistersLengthVIXL,
149 kArmPointerSize) {}
150
151 private:
152 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConventionARMVIXL);
153};
154
155class InvokeDexCallingConventionARMVIXL
156 : public CallingConvention<vixl::aarch32::Register, vixl::aarch32::SRegister> {
157 public:
158 InvokeDexCallingConventionARMVIXL()
159 : CallingConvention(kParameterCoreRegistersVIXL,
160 kParameterCoreRegistersLengthVIXL,
161 kParameterFpuRegistersVIXL,
162 kParameterFpuRegistersLengthVIXL,
163 kArmPointerSize) {}
164
165 private:
166 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionARMVIXL);
167};
168
Artem Serovd4cc5b22016-11-04 11:19:09 +0000169class InvokeDexCallingConventionVisitorARMVIXL : public InvokeDexCallingConventionVisitor {
170 public:
171 InvokeDexCallingConventionVisitorARMVIXL() {}
172 virtual ~InvokeDexCallingConventionVisitorARMVIXL() {}
173
174 Location GetNextLocation(Primitive::Type type) OVERRIDE;
175 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
176 Location GetMethodLocation() const OVERRIDE;
177
178 private:
179 InvokeDexCallingConventionARMVIXL calling_convention;
180 uint32_t double_index_ = 0;
181
182 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorARMVIXL);
183};
184
Artem Serovcfbe9132016-10-14 15:58:56 +0100185class FieldAccessCallingConventionARMVIXL : public FieldAccessCallingConvention {
186 public:
187 FieldAccessCallingConventionARMVIXL() {}
188
189 Location GetObjectLocation() const OVERRIDE {
190 return helpers::LocationFrom(vixl::aarch32::r1);
191 }
192 Location GetFieldIndexLocation() const OVERRIDE {
193 return helpers::LocationFrom(vixl::aarch32::r0);
194 }
195 Location GetReturnLocation(Primitive::Type type) const OVERRIDE {
196 return Primitive::Is64BitType(type)
197 ? helpers::LocationFrom(vixl::aarch32::r0, vixl::aarch32::r1)
198 : helpers::LocationFrom(vixl::aarch32::r0);
199 }
200 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
201 return Primitive::Is64BitType(type)
202 ? helpers::LocationFrom(vixl::aarch32::r2, vixl::aarch32::r3)
203 : (is_instance
204 ? helpers::LocationFrom(vixl::aarch32::r2)
205 : helpers::LocationFrom(vixl::aarch32::r1));
206 }
207 Location GetFpuLocation(Primitive::Type type) const OVERRIDE {
208 return Primitive::Is64BitType(type)
209 ? helpers::LocationFrom(vixl::aarch32::s0, vixl::aarch32::s1)
210 : helpers::LocationFrom(vixl::aarch32::s0);
211 }
212
213 private:
214 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionARMVIXL);
215};
216
Scott Wakelingfe885462016-09-22 10:24:38 +0100217class SlowPathCodeARMVIXL : public SlowPathCode {
218 public:
219 explicit SlowPathCodeARMVIXL(HInstruction* instruction)
220 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
221
222 vixl::aarch32::Label* GetEntryLabel() { return &entry_label_; }
223 vixl::aarch32::Label* GetExitLabel() { return &exit_label_; }
224
225 void SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) OVERRIDE;
226 void RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) OVERRIDE;
227
228 private:
229 vixl::aarch32::Label entry_label_;
230 vixl::aarch32::Label exit_label_;
231
232 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARMVIXL);
233};
234
235class ParallelMoveResolverARMVIXL : public ParallelMoveResolverWithSwap {
236 public:
237 ParallelMoveResolverARMVIXL(ArenaAllocator* allocator, CodeGeneratorARMVIXL* codegen)
238 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
239
240 void EmitMove(size_t index) OVERRIDE;
241 void EmitSwap(size_t index) OVERRIDE;
242 void SpillScratch(int reg) OVERRIDE;
243 void RestoreScratch(int reg) OVERRIDE;
244
245 ArmVIXLAssembler* GetAssembler() const;
246
247 private:
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100248 void Exchange(vixl32::Register reg, int mem);
Scott Wakelingfe885462016-09-22 10:24:38 +0100249 void Exchange(int mem1, int mem2);
250
251 CodeGeneratorARMVIXL* const codegen_;
252
253 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARMVIXL);
254};
255
Scott Wakelingfe885462016-09-22 10:24:38 +0100256class LocationsBuilderARMVIXL : public HGraphVisitor {
257 public:
258 LocationsBuilderARMVIXL(HGraph* graph, CodeGeneratorARMVIXL* codegen)
259 : HGraphVisitor(graph), codegen_(codegen) {}
260
Artem Serovd4cc5b22016-11-04 11:19:09 +0000261#define DECLARE_VISIT_INSTRUCTION(name, super) \
262 void Visit##name(H##name* instr) OVERRIDE;
Scott Wakelingfe885462016-09-22 10:24:38 +0100263
Artem Serovd4cc5b22016-11-04 11:19:09 +0000264 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
265 FOR_EACH_CONCRETE_INSTRUCTION_ARM(DECLARE_VISIT_INSTRUCTION)
266 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
Scott Wakelingfe885462016-09-22 10:24:38 +0100267
Artem Serovd4cc5b22016-11-04 11:19:09 +0000268#undef DECLARE_VISIT_INSTRUCTION
269
270 void VisitInstruction(HInstruction* instruction) OVERRIDE {
271 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
272 << " (id " << instruction->GetId() << ")";
Scott Wakelingfe885462016-09-22 10:24:38 +0100273 }
274
Artem Serovd4cc5b22016-11-04 11:19:09 +0000275 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100276 void HandleInvoke(HInvoke* invoke);
Artem Serov02109dd2016-09-23 17:17:54 +0100277 void HandleBitwiseOperation(HBinaryOperation* operation, Opcode opcode);
Scott Wakelingfe885462016-09-22 10:24:38 +0100278 void HandleCondition(HCondition* condition);
Artem Serov02109dd2016-09-23 17:17:54 +0100279 void HandleIntegerRotate(LocationSummary* locations);
280 void HandleLongRotate(LocationSummary* locations);
281 void HandleShift(HBinaryOperation* operation);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100282 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
283 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Scott Wakelingfe885462016-09-22 10:24:38 +0100284
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100285 Location ArithmeticZeroOrFpuRegister(HInstruction* input);
Artem Serov02109dd2016-09-23 17:17:54 +0100286 Location ArmEncodableConstantOrRegister(HInstruction* constant, Opcode opcode);
287 bool CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode);
288 bool CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode, SetCc set_cc = kCcDontCare);
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100289
Scott Wakelingfe885462016-09-22 10:24:38 +0100290 CodeGeneratorARMVIXL* const codegen_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000291 InvokeDexCallingConventionVisitorARMVIXL parameter_visitor_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100292
293 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARMVIXL);
294};
295
296class InstructionCodeGeneratorARMVIXL : public InstructionCodeGenerator {
297 public:
298 InstructionCodeGeneratorARMVIXL(HGraph* graph, CodeGeneratorARMVIXL* codegen);
299
Artem Serovd4cc5b22016-11-04 11:19:09 +0000300#define DECLARE_VISIT_INSTRUCTION(name, super) \
301 void Visit##name(H##name* instr) OVERRIDE;
Scott Wakelingfe885462016-09-22 10:24:38 +0100302
Artem Serovd4cc5b22016-11-04 11:19:09 +0000303 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
304 FOR_EACH_CONCRETE_INSTRUCTION_ARM(DECLARE_VISIT_INSTRUCTION)
305 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
306
307#undef DECLARE_VISIT_INSTRUCTION
308
309 void VisitInstruction(HInstruction* instruction) OVERRIDE {
310 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
311 << " (id " << instruction->GetId() << ")";
312 }
Scott Wakelingfe885462016-09-22 10:24:38 +0100313
314 ArmVIXLAssembler* GetAssembler() const { return assembler_; }
xueliang.zhongf51bc622016-11-04 09:23:32 +0000315 ArmVIXLMacroAssembler* GetVIXLAssembler() { return GetAssembler()->GetVIXLAssembler(); }
Scott Wakelingfe885462016-09-22 10:24:38 +0100316
317 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100318 // Generate code for the given suspend check. If not null, `successor`
319 // is the block to branch to if the suspend check is not needed, and after
320 // the suspend call.
Scott Wakelingfe885462016-09-22 10:24:38 +0100321 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100322 void GenerateClassInitializationCheck(LoadClassSlowPathARMVIXL* slow_path,
323 vixl32::Register class_reg);
Artem Serov02109dd2016-09-23 17:17:54 +0100324 void GenerateAndConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
325 void GenerateOrrConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
326 void GenerateEorConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
Anton Kirilovdda43962016-11-21 19:55:20 +0000327 void GenerateAddLongConst(Location out, Location first, uint64_t value);
Artem Serov02109dd2016-09-23 17:17:54 +0100328 void HandleBitwiseOperation(HBinaryOperation* operation);
Scott Wakelingfe885462016-09-22 10:24:38 +0100329 void HandleCondition(HCondition* condition);
Artem Serov02109dd2016-09-23 17:17:54 +0100330 void HandleIntegerRotate(HRor* ror);
331 void HandleLongRotate(HRor* ror);
332 void HandleShift(HBinaryOperation* operation);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100333
334 void GenerateWideAtomicStore(vixl::aarch32::Register addr,
335 uint32_t offset,
336 vixl::aarch32::Register value_lo,
337 vixl::aarch32::Register value_hi,
338 vixl::aarch32::Register temp1,
339 vixl::aarch32::Register temp2,
340 HInstruction* instruction);
341 void GenerateWideAtomicLoad(vixl::aarch32::Register addr,
342 uint32_t offset,
343 vixl::aarch32::Register out_lo,
344 vixl::aarch32::Register out_hi);
345
346 void HandleFieldSet(HInstruction* instruction,
347 const FieldInfo& field_info,
348 bool value_can_be_null);
349 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
350
Artem Serovcfbe9132016-10-14 15:58:56 +0100351 // Generate a heap reference load using one register `out`:
352 //
353 // out <- *(out + offset)
354 //
355 // while honoring heap poisoning and/or read barriers (if any).
356 //
357 // Location `maybe_temp` is used when generating a read barrier and
358 // shall be a register in that case; it may be an invalid location
359 // otherwise.
360 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
361 Location out,
362 uint32_t offset,
Artem Serov657022c2016-11-23 14:19:38 +0000363 Location maybe_temp,
364 ReadBarrierOption read_barrier_option);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100365 // Generate a heap reference load using two different registers
366 // `out` and `obj`:
367 //
368 // out <- *(obj + offset)
369 //
370 // while honoring heap poisoning and/or read barriers (if any).
371 //
372 // Location `maybe_temp` is used when generating a Baker's (fast
373 // path) read barrier and shall be a register in that case; it may
374 // be an invalid location otherwise.
375 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
376 Location out,
377 Location obj,
378 uint32_t offset,
Artem Serov657022c2016-11-23 14:19:38 +0000379 Location maybe_temp,
380 ReadBarrierOption read_barrier_option);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100381 // Generate a GC root reference load:
382 //
383 // root <- *(obj + offset)
384 //
Artem Serovd4cc5b22016-11-04 11:19:09 +0000385 // while honoring read barriers based on read_barrier_option.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100386 void GenerateGcRootFieldLoad(HInstruction* instruction,
387 Location root,
388 vixl::aarch32::Register obj,
389 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +0000390 ReadBarrierOption read_barrier_option);
Scott Wakelingfe885462016-09-22 10:24:38 +0100391 void GenerateTestAndBranch(HInstruction* instruction,
392 size_t condition_input_index,
393 vixl::aarch32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +0000394 vixl::aarch32::Label* false_target,
395 bool far_target = true);
Scott Wakelingfe885462016-09-22 10:24:38 +0100396 void GenerateCompareTestAndBranch(HCondition* condition,
397 vixl::aarch32::Label* true_target,
398 vixl::aarch32::Label* false_target);
399 void GenerateVcmp(HInstruction* instruction);
400 void GenerateFPJumps(HCondition* cond,
401 vixl::aarch32::Label* true_label,
402 vixl::aarch32::Label* false_label);
403 void GenerateLongComparesAndJumps(HCondition* cond,
404 vixl::aarch32::Label* true_label,
405 vixl::aarch32::Label* false_label);
406 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
407 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
408 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
409 void GenerateDivRemConstantIntegral(HBinaryOperation* instruction);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000410 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Scott Wakelingfe885462016-09-22 10:24:38 +0100411
412 ArmVIXLAssembler* const assembler_;
413 CodeGeneratorARMVIXL* const codegen_;
414
415 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARMVIXL);
416};
417
418class CodeGeneratorARMVIXL : public CodeGenerator {
419 public:
420 CodeGeneratorARMVIXL(HGraph* graph,
421 const ArmInstructionSetFeatures& isa_features,
422 const CompilerOptions& compiler_options,
423 OptimizingCompilerStats* stats = nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +0100424 virtual ~CodeGeneratorARMVIXL() {}
425
Scott Wakelingfe885462016-09-22 10:24:38 +0100426 void GenerateFrameEntry() OVERRIDE;
427 void GenerateFrameExit() OVERRIDE;
428 void Bind(HBasicBlock* block) OVERRIDE;
429 void MoveConstant(Location destination, int32_t value) OVERRIDE;
430 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
431 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
432
Artem Serovd4cc5b22016-11-04 11:19:09 +0000433 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
434 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
435 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
436 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
437
438 size_t GetWordSize() const OVERRIDE {
439 return static_cast<size_t>(kArmPointerSize);
440 }
441
442 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return vixl::aarch32::kRegSizeInBytes; }
443
444 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
445
446 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
447
Scott Wakelingfe885462016-09-22 10:24:38 +0100448 ArmVIXLAssembler* GetAssembler() OVERRIDE { return &assembler_; }
449
450 const ArmVIXLAssembler& GetAssembler() const OVERRIDE { return assembler_; }
451
xueliang.zhongf51bc622016-11-04 09:23:32 +0000452 ArmVIXLMacroAssembler* GetVIXLAssembler() { return GetAssembler()->GetVIXLAssembler(); }
Scott Wakelingfe885462016-09-22 10:24:38 +0100453
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100454 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
455 vixl::aarch32::Label* block_entry_label = GetLabelOf(block);
456 DCHECK(block_entry_label->IsBound());
457 return block_entry_label->GetLocation();
458 }
459
Artem Serov09a940d2016-11-11 16:15:11 +0000460 void FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +0100461 void SetupBlockedRegisters() const OVERRIDE;
462
Scott Wakelingfe885462016-09-22 10:24:38 +0100463 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
464 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
465
Artem Serovd4cc5b22016-11-04 11:19:09 +0000466 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100467 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kThumb2; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100468 // Helper method to move a 32-bit value between two locations.
469 void Move32(Location destination, Location source);
470
Scott Wakelingc34dba72016-10-03 10:14:44 +0100471 void LoadFromShiftedRegOffset(Primitive::Type type,
472 Location out_loc,
473 vixl::aarch32::Register base,
474 vixl::aarch32::Register reg_index,
475 vixl::aarch32::Condition cond = vixl::aarch32::al);
476 void StoreToShiftedRegOffset(Primitive::Type type,
477 Location out_loc,
478 vixl::aarch32::Register base,
479 vixl::aarch32::Register reg_index,
480 vixl::aarch32::Condition cond = vixl::aarch32::al);
481
Scott Wakelingfe885462016-09-22 10:24:38 +0100482 // Generate code to invoke a runtime entry point.
483 void InvokeRuntime(QuickEntrypointEnum entrypoint,
484 HInstruction* instruction,
485 uint32_t dex_pc,
486 SlowPathCode* slow_path = nullptr) OVERRIDE;
487
488 // Generate code to invoke a runtime entry point, but do not record
489 // PC-related information in a stack map.
490 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
491 HInstruction* instruction,
492 SlowPathCode* slow_path);
493
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100494 // Emit a write barrier.
495 void MarkGCCard(vixl::aarch32::Register temp,
496 vixl::aarch32::Register card,
497 vixl::aarch32::Register object,
498 vixl::aarch32::Register value,
499 bool can_be_null);
500
Artem Serovd4cc5b22016-11-04 11:19:09 +0000501 void GenerateMemoryBarrier(MemBarrierKind kind);
502
503 vixl::aarch32::Label* GetLabelOf(HBasicBlock* block) {
504 block = FirstNonEmptyBlock(block);
505 return &(block_labels_[block->GetBlockId()]);
506 }
507
508 void Initialize() OVERRIDE {
509 block_labels_.resize(GetGraph()->GetBlocks().size());
510 }
511
512 void Finalize(CodeAllocator* allocator) OVERRIDE;
513
514 const ArmInstructionSetFeatures& GetInstructionSetFeatures() const { return isa_features_; }
515
516 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
517 return type == Primitive::kPrimDouble || type == Primitive::kPrimLong;
518 }
519
520 void ComputeSpillMask() OVERRIDE;
521
522 vixl::aarch32::Label* GetFrameEntryLabel() { return &frame_entry_label_; }
523
524 // Check if the desired_string_load_kind is supported. If it is, return it,
525 // otherwise return a fall-back kind that should be used instead.
526 HLoadString::LoadKind GetSupportedLoadStringKind(
527 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
528
529 // Check if the desired_class_load_kind is supported. If it is, return it,
530 // otherwise return a fall-back kind that should be used instead.
531 HLoadClass::LoadKind GetSupportedLoadClassKind(
532 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
533
534 // Check if the desired_dispatch_info is supported. If it is, return it,
535 // otherwise return a fall-back info that should be used instead.
536 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
537 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
538 HInvokeStaticOrDirect* invoke) OVERRIDE;
539
540 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
541 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
542
543 void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
544
545 // The PcRelativePatchInfo is used for PC-relative addressing of dex cache arrays
546 // and boot image strings/types. The only difference is the interpretation of the
547 // offset_or_index. The PC-relative address is loaded with three instructions,
548 // MOVW+MOVT to load the offset to base_reg and then ADD base_reg, PC. The offset
549 // is calculated from the ADD's effective PC, i.e. PC+4 on Thumb2. Though we
550 // currently emit these 3 instructions together, instruction scheduling could
551 // split this sequence apart, so we keep separate labels for each of them.
552 struct PcRelativePatchInfo {
553 PcRelativePatchInfo(const DexFile& dex_file, uint32_t off_or_idx)
554 : target_dex_file(dex_file), offset_or_index(off_or_idx) { }
555 PcRelativePatchInfo(PcRelativePatchInfo&& other) = default;
556
557 const DexFile& target_dex_file;
558 // Either the dex cache array element offset or the string/type index.
559 uint32_t offset_or_index;
560 vixl::aarch32::Label movw_label;
561 vixl::aarch32::Label movt_label;
562 vixl::aarch32::Label add_pc_label;
563 };
564
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000565 PcRelativePatchInfo* NewPcRelativeStringPatch(const DexFile& dex_file,
566 dex::StringIndex string_index);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000567 PcRelativePatchInfo* NewPcRelativeTypePatch(const DexFile& dex_file, dex::TypeIndex type_index);
Vladimir Marko1998cd02017-01-13 13:02:58 +0000568 PcRelativePatchInfo* NewTypeBssEntryPatch(const DexFile& dex_file, dex::TypeIndex type_index);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000569 PcRelativePatchInfo* NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
570 uint32_t element_offset);
Artem Serovc5fcb442016-12-02 19:19:58 +0000571 VIXLUInt32Literal* DeduplicateBootImageStringLiteral(const DexFile& dex_file,
572 dex::StringIndex string_index);
573 VIXLUInt32Literal* DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
574 dex::TypeIndex type_index);
575 VIXLUInt32Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
576 VIXLUInt32Literal* DeduplicateDexCacheAddressLiteral(uint32_t address);
577 VIXLUInt32Literal* DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000578 dex::StringIndex string_index,
579 Handle<mirror::String> handle);
Artem Serovc5fcb442016-12-02 19:19:58 +0000580 VIXLUInt32Literal* DeduplicateJitClassLiteral(const DexFile& dex_file,
581 dex::TypeIndex type_index,
582 uint64_t address);
583
Artem Serovd4cc5b22016-11-04 11:19:09 +0000584 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
585
Artem Serovc5fcb442016-12-02 19:19:58 +0000586 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
587
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100588 // Fast path implementation of ReadBarrier::Barrier for a heap
589 // reference field load when Baker's read barriers are used.
590 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
591 Location ref,
592 vixl::aarch32::Register obj,
593 uint32_t offset,
594 Location temp,
595 bool needs_null_check);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000596 // Fast path implementation of ReadBarrier::Barrier for a heap
597 // reference array load when Baker's read barriers are used.
598 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
599 Location ref,
600 vixl::aarch32::Register obj,
601 uint32_t data_offset,
602 Location index,
603 Location temp,
604 bool needs_null_check);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100605 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
606 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
607 //
608 // Load the object reference located at the address
609 // `obj + offset + (index << scale_factor)`, held by object `obj`, into
610 // `ref`, and mark it if needed.
611 //
612 // If `always_update_field` is true, the value of the reference is
613 // atomically updated in the holder (`obj`). This operation
614 // requires an extra temporary register, which must be provided as a
615 // non-null pointer (`temp2`).
616 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
617 Location ref,
618 vixl::aarch32::Register obj,
619 uint32_t offset,
620 Location index,
621 ScaleFactor scale_factor,
622 Location temp,
623 bool needs_null_check,
624 bool always_update_field = false,
625 vixl::aarch32::Register* temp2 = nullptr);
626
627 // Generate a read barrier for a heap reference within `instruction`
628 // using a slow path.
629 //
630 // A read barrier for an object reference read from the heap is
631 // implemented as a call to the artReadBarrierSlow runtime entry
632 // point, which is passed the values in locations `ref`, `obj`, and
633 // `offset`:
634 //
635 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
636 // mirror::Object* obj,
637 // uint32_t offset);
638 //
639 // The `out` location contains the value returned by
640 // artReadBarrierSlow.
641 //
642 // When `index` is provided (i.e. for array accesses), the offset
643 // value passed to artReadBarrierSlow is adjusted to take `index`
644 // into account.
645 void GenerateReadBarrierSlow(HInstruction* instruction,
646 Location out,
647 Location ref,
648 Location obj,
649 uint32_t offset,
650 Location index = Location::NoLocation());
651
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100652 // If read barriers are enabled, generate a read barrier for a heap
653 // reference using a slow path. If heap poisoning is enabled, also
654 // unpoison the reference in `out`.
655 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
656 Location out,
657 Location ref,
658 Location obj,
659 uint32_t offset,
660 Location index = Location::NoLocation());
661
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000662 // Generate a read barrier for a GC root within `instruction` using
663 // a slow path.
664 //
665 // A read barrier for an object reference GC root is implemented as
666 // a call to the artReadBarrierForRootSlow runtime entry point,
667 // which is passed the value in location `root`:
668 //
669 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
670 //
671 // The `out` location contains the value returned by
672 // artReadBarrierForRootSlow.
673 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
674
Scott Wakelingfe885462016-09-22 10:24:38 +0100675 void GenerateNop() OVERRIDE;
676
Artem Serovd4cc5b22016-11-04 11:19:09 +0000677 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
678 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
679
680 JumpTableARMVIXL* CreateJumpTable(HPackedSwitch* switch_instr) {
681 jump_tables_.emplace_back(new (GetGraph()->GetArena()) JumpTableARMVIXL(switch_instr));
682 return jump_tables_.back().get();
683 }
684 void EmitJumpTables();
685
686 void EmitMovwMovtPlaceholder(CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
687 vixl::aarch32::Register out);
688
Scott Wakelingfe885462016-09-22 10:24:38 +0100689 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100690 vixl::aarch32::Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
691 vixl::aarch32::Register temp);
692
Artem Serovc5fcb442016-12-02 19:19:58 +0000693 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, VIXLUInt32Literal*>;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000694 using MethodToLiteralMap =
Artem Serovc5fcb442016-12-02 19:19:58 +0000695 ArenaSafeMap<MethodReference, VIXLUInt32Literal*, MethodReferenceComparator>;
696 using StringToLiteralMap = ArenaSafeMap<StringReference,
697 VIXLUInt32Literal*,
698 StringReferenceValueComparator>;
699 using TypeToLiteralMap = ArenaSafeMap<TypeReference,
700 VIXLUInt32Literal*,
701 TypeReferenceValueComparator>;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000702
Artem Serovc5fcb442016-12-02 19:19:58 +0000703 VIXLUInt32Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
704 VIXLUInt32Literal* DeduplicateMethodLiteral(MethodReference target_method,
705 MethodToLiteralMap* map);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000706 PcRelativePatchInfo* NewPcRelativePatch(const DexFile& dex_file,
707 uint32_t offset_or_index,
708 ArenaDeque<PcRelativePatchInfo>* patches);
709 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
710 static void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
711 ArenaVector<LinkerPatch>* linker_patches);
712
Scott Wakelingfe885462016-09-22 10:24:38 +0100713 // Labels for each block that will be compiled.
714 // We use a deque so that the `vixl::aarch32::Label` objects do not move in memory.
715 ArenaDeque<vixl::aarch32::Label> block_labels_; // Indexed by block id.
716 vixl::aarch32::Label frame_entry_label_;
717
Artem Serov551b28f2016-10-18 19:11:30 +0100718 ArenaVector<std::unique_ptr<JumpTableARMVIXL>> jump_tables_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100719 LocationsBuilderARMVIXL location_builder_;
720 InstructionCodeGeneratorARMVIXL instruction_visitor_;
721 ParallelMoveResolverARMVIXL move_resolver_;
722
723 ArmVIXLAssembler assembler_;
724 const ArmInstructionSetFeatures& isa_features_;
725
Artem Serovc5fcb442016-12-02 19:19:58 +0000726 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
727 Uint32ToLiteralMap uint32_literals_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000728 // PC-relative patch info for each HArmDexCacheArraysBase.
729 ArenaDeque<PcRelativePatchInfo> pc_relative_dex_cache_patches_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000730 // Deduplication map for boot string literals for kBootImageLinkTimeAddress.
731 StringToLiteralMap boot_image_string_patches_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000732 // PC-relative String patch info; type depends on configuration (app .bss or boot image PIC).
733 ArenaDeque<PcRelativePatchInfo> pc_relative_string_patches_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000734 // Deduplication map for boot type literals for kBootImageLinkTimeAddress.
735 TypeToLiteralMap boot_image_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000736 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Artem Serovd4cc5b22016-11-04 11:19:09 +0000737 ArenaDeque<PcRelativePatchInfo> pc_relative_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000738 // PC-relative type patch info for kBssEntry.
739 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000740 // Deduplication map for patchable boot image addresses.
741 Uint32ToLiteralMap boot_image_address_patches_;
742
743 // Patches for string literals in JIT compiled code.
744 StringToLiteralMap jit_string_patches_;
745 // Patches for class literals in JIT compiled code.
746 TypeToLiteralMap jit_class_patches_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000747
Scott Wakelingfe885462016-09-22 10:24:38 +0100748 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARMVIXL);
749};
750
Scott Wakelingfe885462016-09-22 10:24:38 +0100751} // namespace arm
752} // namespace art
753
754#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM_VIXL_H_