blob: 91f7524c8e97e0e0cb1a58dd574c602818e37b93 [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
Nicolas Geoffray467d94a2017-03-16 10:24:17 +000038// Default to use the VIXL-based backend on ARM.
39#ifdef ART_USE_OLD_ARM_BACKEND
Nicolas Geoffray25275be2017-03-14 08:57:02 +000040static constexpr bool kArmUseVIXL32 = false;
Nicolas Geoffray467d94a2017-03-16 10:24:17 +000041#else
42static constexpr bool kArmUseVIXL32 = true;
Scott Wakelingfe885462016-09-22 10:24:38 +010043#endif
44
45namespace art {
46namespace arm {
47
Roland Levillainba650a42017-03-06 13:52:32 +000048// This constant is used as an approximate margin when emission of veneer and literal pools
49// must be blocked.
50static constexpr int kMaxMacroInstructionSizeInBytes =
51 15 * vixl::aarch32::kMaxInstructionSizeInBytes;
52
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053static const vixl::aarch32::Register kParameterCoreRegistersVIXL[] = {
54 vixl::aarch32::r1,
55 vixl::aarch32::r2,
56 vixl::aarch32::r3
57};
Artem Serovd4cc5b22016-11-04 11:19:09 +000058static const size_t kParameterCoreRegistersLengthVIXL = arraysize(kParameterCoreRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +010059static const vixl::aarch32::SRegister kParameterFpuRegistersVIXL[] = {
60 vixl::aarch32::s0,
61 vixl::aarch32::s1,
62 vixl::aarch32::s2,
63 vixl::aarch32::s3,
64 vixl::aarch32::s4,
65 vixl::aarch32::s5,
66 vixl::aarch32::s6,
67 vixl::aarch32::s7,
68 vixl::aarch32::s8,
69 vixl::aarch32::s9,
70 vixl::aarch32::s10,
71 vixl::aarch32::s11,
72 vixl::aarch32::s12,
73 vixl::aarch32::s13,
74 vixl::aarch32::s14,
75 vixl::aarch32::s15
76};
Artem Serovd4cc5b22016-11-04 11:19:09 +000077static const size_t kParameterFpuRegistersLengthVIXL = arraysize(kParameterFpuRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +010078
Scott Wakelingfe885462016-09-22 10:24:38 +010079static const vixl::aarch32::Register kMethodRegister = vixl::aarch32::r0;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010080
Scott Wakelingfe885462016-09-22 10:24:38 +010081static const vixl::aarch32::Register kCoreAlwaysSpillRegister = vixl::aarch32::r5;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010082
83// Callee saves core registers r5, r6, r7, r8, r10, r11, and lr.
84static const vixl::aarch32::RegisterList kCoreCalleeSaves = vixl::aarch32::RegisterList::Union(
85 vixl::aarch32::RegisterList(vixl::aarch32::r5,
86 vixl::aarch32::r6,
87 vixl::aarch32::r7,
88 vixl::aarch32::r8),
89 vixl::aarch32::RegisterList(vixl::aarch32::r10,
90 vixl::aarch32::r11,
91 vixl::aarch32::lr));
92
93// Callee saves FP registers s16 to s31 inclusive.
Scott Wakelingfe885462016-09-22 10:24:38 +010094static const vixl::aarch32::SRegisterList kFpuCalleeSaves =
95 vixl::aarch32::SRegisterList(vixl::aarch32::s16, 16);
96
Scott Wakelinga7812ae2016-10-17 10:03:36 +010097static const vixl::aarch32::Register kRuntimeParameterCoreRegistersVIXL[] = {
98 vixl::aarch32::r0,
99 vixl::aarch32::r1,
100 vixl::aarch32::r2,
101 vixl::aarch32::r3
102};
103static const size_t kRuntimeParameterCoreRegistersLengthVIXL =
Artem Serovd4cc5b22016-11-04 11:19:09 +0000104 arraysize(kRuntimeParameterCoreRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100105static const vixl::aarch32::SRegister kRuntimeParameterFpuRegistersVIXL[] = {
106 vixl::aarch32::s0,
107 vixl::aarch32::s1,
108 vixl::aarch32::s2,
109 vixl::aarch32::s3
110};
111static const size_t kRuntimeParameterFpuRegistersLengthVIXL =
Artem Serovd4cc5b22016-11-04 11:19:09 +0000112 arraysize(kRuntimeParameterFpuRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100113
114class LoadClassSlowPathARMVIXL;
Scott Wakelingfe885462016-09-22 10:24:38 +0100115class CodeGeneratorARMVIXL;
116
Artem Serovc5fcb442016-12-02 19:19:58 +0000117using VIXLInt32Literal = vixl::aarch32::Literal<int32_t>;
118using VIXLUInt32Literal = vixl::aarch32::Literal<uint32_t>;
119
Artem Serov551b28f2016-10-18 19:11:30 +0100120class JumpTableARMVIXL : public DeletableArenaObject<kArenaAllocSwitchTable> {
121 public:
122 explicit JumpTableARMVIXL(HPackedSwitch* switch_instr)
Artem Serov09a940d2016-11-11 16:15:11 +0000123 : switch_instr_(switch_instr),
124 table_start_(),
125 bb_addresses_(switch_instr->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
126 uint32_t num_entries = switch_instr_->GetNumEntries();
127 for (uint32_t i = 0; i < num_entries; i++) {
Artem Serovc5fcb442016-12-02 19:19:58 +0000128 VIXLInt32Literal *lit = new VIXLInt32Literal(0, vixl32::RawLiteral::kManuallyPlaced);
Artem Serov09a940d2016-11-11 16:15:11 +0000129 bb_addresses_.emplace_back(lit);
130 }
131 }
Artem Serov551b28f2016-10-18 19:11:30 +0100132
133 vixl::aarch32::Label* GetTableStartLabel() { return &table_start_; }
134
135 void EmitTable(CodeGeneratorARMVIXL* codegen);
Artem Serov09a940d2016-11-11 16:15:11 +0000136 void FixTable(CodeGeneratorARMVIXL* codegen);
Artem Serov551b28f2016-10-18 19:11:30 +0100137
138 private:
139 HPackedSwitch* const switch_instr_;
140 vixl::aarch32::Label table_start_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000141 ArenaVector<std::unique_ptr<VIXLInt32Literal>> bb_addresses_;
Artem Serov551b28f2016-10-18 19:11:30 +0100142
143 DISALLOW_COPY_AND_ASSIGN(JumpTableARMVIXL);
144};
145
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100146class InvokeRuntimeCallingConventionARMVIXL
147 : public CallingConvention<vixl::aarch32::Register, vixl::aarch32::SRegister> {
148 public:
149 InvokeRuntimeCallingConventionARMVIXL()
150 : CallingConvention(kRuntimeParameterCoreRegistersVIXL,
151 kRuntimeParameterCoreRegistersLengthVIXL,
152 kRuntimeParameterFpuRegistersVIXL,
153 kRuntimeParameterFpuRegistersLengthVIXL,
154 kArmPointerSize) {}
155
156 private:
157 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConventionARMVIXL);
158};
159
160class InvokeDexCallingConventionARMVIXL
161 : public CallingConvention<vixl::aarch32::Register, vixl::aarch32::SRegister> {
162 public:
163 InvokeDexCallingConventionARMVIXL()
164 : CallingConvention(kParameterCoreRegistersVIXL,
165 kParameterCoreRegistersLengthVIXL,
166 kParameterFpuRegistersVIXL,
167 kParameterFpuRegistersLengthVIXL,
168 kArmPointerSize) {}
169
170 private:
171 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionARMVIXL);
172};
173
Artem Serovd4cc5b22016-11-04 11:19:09 +0000174class InvokeDexCallingConventionVisitorARMVIXL : public InvokeDexCallingConventionVisitor {
175 public:
176 InvokeDexCallingConventionVisitorARMVIXL() {}
177 virtual ~InvokeDexCallingConventionVisitorARMVIXL() {}
178
179 Location GetNextLocation(Primitive::Type type) OVERRIDE;
180 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
181 Location GetMethodLocation() const OVERRIDE;
182
183 private:
184 InvokeDexCallingConventionARMVIXL calling_convention;
185 uint32_t double_index_ = 0;
186
187 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorARMVIXL);
188};
189
Artem Serovcfbe9132016-10-14 15:58:56 +0100190class FieldAccessCallingConventionARMVIXL : public FieldAccessCallingConvention {
191 public:
192 FieldAccessCallingConventionARMVIXL() {}
193
194 Location GetObjectLocation() const OVERRIDE {
195 return helpers::LocationFrom(vixl::aarch32::r1);
196 }
197 Location GetFieldIndexLocation() const OVERRIDE {
198 return helpers::LocationFrom(vixl::aarch32::r0);
199 }
200 Location GetReturnLocation(Primitive::Type type) const OVERRIDE {
201 return Primitive::Is64BitType(type)
202 ? helpers::LocationFrom(vixl::aarch32::r0, vixl::aarch32::r1)
203 : helpers::LocationFrom(vixl::aarch32::r0);
204 }
205 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
206 return Primitive::Is64BitType(type)
Nicolas Geoffraya72859d2017-01-26 22:47:27 +0000207 ? helpers::LocationFrom(vixl::aarch32::r2, vixl::aarch32::r3)
Artem Serovcfbe9132016-10-14 15:58:56 +0100208 : (is_instance
209 ? helpers::LocationFrom(vixl::aarch32::r2)
210 : helpers::LocationFrom(vixl::aarch32::r1));
211 }
212 Location GetFpuLocation(Primitive::Type type) const OVERRIDE {
213 return Primitive::Is64BitType(type)
214 ? helpers::LocationFrom(vixl::aarch32::s0, vixl::aarch32::s1)
215 : helpers::LocationFrom(vixl::aarch32::s0);
216 }
217
218 private:
219 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionARMVIXL);
220};
221
Scott Wakelingfe885462016-09-22 10:24:38 +0100222class SlowPathCodeARMVIXL : public SlowPathCode {
223 public:
224 explicit SlowPathCodeARMVIXL(HInstruction* instruction)
225 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
226
227 vixl::aarch32::Label* GetEntryLabel() { return &entry_label_; }
228 vixl::aarch32::Label* GetExitLabel() { return &exit_label_; }
229
230 void SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) OVERRIDE;
231 void RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) OVERRIDE;
232
233 private:
234 vixl::aarch32::Label entry_label_;
235 vixl::aarch32::Label exit_label_;
236
237 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARMVIXL);
238};
239
240class ParallelMoveResolverARMVIXL : public ParallelMoveResolverWithSwap {
241 public:
242 ParallelMoveResolverARMVIXL(ArenaAllocator* allocator, CodeGeneratorARMVIXL* codegen)
243 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
244
245 void EmitMove(size_t index) OVERRIDE;
246 void EmitSwap(size_t index) OVERRIDE;
247 void SpillScratch(int reg) OVERRIDE;
248 void RestoreScratch(int reg) OVERRIDE;
249
250 ArmVIXLAssembler* GetAssembler() const;
251
252 private:
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100253 void Exchange(vixl32::Register reg, int mem);
Scott Wakelingfe885462016-09-22 10:24:38 +0100254 void Exchange(int mem1, int mem2);
255
256 CodeGeneratorARMVIXL* const codegen_;
257
258 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARMVIXL);
259};
260
Scott Wakelingfe885462016-09-22 10:24:38 +0100261class LocationsBuilderARMVIXL : public HGraphVisitor {
262 public:
263 LocationsBuilderARMVIXL(HGraph* graph, CodeGeneratorARMVIXL* codegen)
264 : HGraphVisitor(graph), codegen_(codegen) {}
265
Artem Serovd4cc5b22016-11-04 11:19:09 +0000266#define DECLARE_VISIT_INSTRUCTION(name, super) \
267 void Visit##name(H##name* instr) OVERRIDE;
Scott Wakelingfe885462016-09-22 10:24:38 +0100268
Artem Serovd4cc5b22016-11-04 11:19:09 +0000269 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
270 FOR_EACH_CONCRETE_INSTRUCTION_ARM(DECLARE_VISIT_INSTRUCTION)
271 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
Scott Wakelingfe885462016-09-22 10:24:38 +0100272
Artem Serovd4cc5b22016-11-04 11:19:09 +0000273#undef DECLARE_VISIT_INSTRUCTION
274
275 void VisitInstruction(HInstruction* instruction) OVERRIDE {
276 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
277 << " (id " << instruction->GetId() << ")";
Scott Wakelingfe885462016-09-22 10:24:38 +0100278 }
279
Artem Serovd4cc5b22016-11-04 11:19:09 +0000280 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100281 void HandleInvoke(HInvoke* invoke);
Artem Serov02109dd2016-09-23 17:17:54 +0100282 void HandleBitwiseOperation(HBinaryOperation* operation, Opcode opcode);
Scott Wakelingfe885462016-09-22 10:24:38 +0100283 void HandleCondition(HCondition* condition);
Artem Serov02109dd2016-09-23 17:17:54 +0100284 void HandleIntegerRotate(LocationSummary* locations);
285 void HandleLongRotate(LocationSummary* locations);
286 void HandleShift(HBinaryOperation* operation);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100287 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
288 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Scott Wakelingfe885462016-09-22 10:24:38 +0100289
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100290 Location ArithmeticZeroOrFpuRegister(HInstruction* input);
Artem Serov02109dd2016-09-23 17:17:54 +0100291 Location ArmEncodableConstantOrRegister(HInstruction* constant, Opcode opcode);
292 bool CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode);
293 bool CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode, SetCc set_cc = kCcDontCare);
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100294
Scott Wakelingfe885462016-09-22 10:24:38 +0100295 CodeGeneratorARMVIXL* const codegen_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000296 InvokeDexCallingConventionVisitorARMVIXL parameter_visitor_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100297
298 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARMVIXL);
299};
300
301class InstructionCodeGeneratorARMVIXL : public InstructionCodeGenerator {
302 public:
303 InstructionCodeGeneratorARMVIXL(HGraph* graph, CodeGeneratorARMVIXL* codegen);
304
Artem Serovd4cc5b22016-11-04 11:19:09 +0000305#define DECLARE_VISIT_INSTRUCTION(name, super) \
306 void Visit##name(H##name* instr) OVERRIDE;
Scott Wakelingfe885462016-09-22 10:24:38 +0100307
Artem Serovd4cc5b22016-11-04 11:19:09 +0000308 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
309 FOR_EACH_CONCRETE_INSTRUCTION_ARM(DECLARE_VISIT_INSTRUCTION)
310 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
311
312#undef DECLARE_VISIT_INSTRUCTION
313
314 void VisitInstruction(HInstruction* instruction) OVERRIDE {
315 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
316 << " (id " << instruction->GetId() << ")";
317 }
Scott Wakelingfe885462016-09-22 10:24:38 +0100318
319 ArmVIXLAssembler* GetAssembler() const { return assembler_; }
xueliang.zhongf51bc622016-11-04 09:23:32 +0000320 ArmVIXLMacroAssembler* GetVIXLAssembler() { return GetAssembler()->GetVIXLAssembler(); }
Scott Wakelingfe885462016-09-22 10:24:38 +0100321
322 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100323 // Generate code for the given suspend check. If not null, `successor`
324 // is the block to branch to if the suspend check is not needed, and after
325 // the suspend call.
Scott Wakelingfe885462016-09-22 10:24:38 +0100326 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100327 void GenerateClassInitializationCheck(LoadClassSlowPathARMVIXL* slow_path,
328 vixl32::Register class_reg);
Artem Serov02109dd2016-09-23 17:17:54 +0100329 void GenerateAndConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
330 void GenerateOrrConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
331 void GenerateEorConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
Anton Kirilovdda43962016-11-21 19:55:20 +0000332 void GenerateAddLongConst(Location out, Location first, uint64_t value);
Artem Serov02109dd2016-09-23 17:17:54 +0100333 void HandleBitwiseOperation(HBinaryOperation* operation);
Scott Wakelingfe885462016-09-22 10:24:38 +0100334 void HandleCondition(HCondition* condition);
Artem Serov02109dd2016-09-23 17:17:54 +0100335 void HandleIntegerRotate(HRor* ror);
336 void HandleLongRotate(HRor* ror);
337 void HandleShift(HBinaryOperation* operation);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100338
339 void GenerateWideAtomicStore(vixl::aarch32::Register addr,
340 uint32_t offset,
341 vixl::aarch32::Register value_lo,
342 vixl::aarch32::Register value_hi,
343 vixl::aarch32::Register temp1,
344 vixl::aarch32::Register temp2,
345 HInstruction* instruction);
346 void GenerateWideAtomicLoad(vixl::aarch32::Register addr,
347 uint32_t offset,
348 vixl::aarch32::Register out_lo,
349 vixl::aarch32::Register out_hi);
350
351 void HandleFieldSet(HInstruction* instruction,
352 const FieldInfo& field_info,
353 bool value_can_be_null);
354 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
355
Artem Serovcfbe9132016-10-14 15:58:56 +0100356 // Generate a heap reference load using one register `out`:
357 //
358 // out <- *(out + offset)
359 //
360 // while honoring heap poisoning and/or read barriers (if any).
361 //
362 // Location `maybe_temp` is used when generating a read barrier and
363 // shall be a register in that case; it may be an invalid location
364 // otherwise.
365 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
366 Location out,
367 uint32_t offset,
Artem Serov657022c2016-11-23 14:19:38 +0000368 Location maybe_temp,
369 ReadBarrierOption read_barrier_option);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100370 // Generate a heap reference load using two different registers
371 // `out` and `obj`:
372 //
373 // out <- *(obj + offset)
374 //
375 // while honoring heap poisoning and/or read barriers (if any).
376 //
377 // Location `maybe_temp` is used when generating a Baker's (fast
378 // path) read barrier and shall be a register in that case; it may
379 // be an invalid location otherwise.
380 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
381 Location out,
382 Location obj,
383 uint32_t offset,
Artem Serov657022c2016-11-23 14:19:38 +0000384 Location maybe_temp,
385 ReadBarrierOption read_barrier_option);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100386 // Generate a GC root reference load:
387 //
388 // root <- *(obj + offset)
389 //
Artem Serovd4cc5b22016-11-04 11:19:09 +0000390 // while honoring read barriers based on read_barrier_option.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100391 void GenerateGcRootFieldLoad(HInstruction* instruction,
392 Location root,
393 vixl::aarch32::Register obj,
394 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +0000395 ReadBarrierOption read_barrier_option);
Scott Wakelingfe885462016-09-22 10:24:38 +0100396 void GenerateTestAndBranch(HInstruction* instruction,
397 size_t condition_input_index,
398 vixl::aarch32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +0000399 vixl::aarch32::Label* false_target,
400 bool far_target = true);
Scott Wakelingfe885462016-09-22 10:24:38 +0100401 void GenerateCompareTestAndBranch(HCondition* condition,
402 vixl::aarch32::Label* true_target,
403 vixl::aarch32::Label* false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +0100404 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
405 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
406 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
407 void GenerateDivRemConstantIntegral(HBinaryOperation* instruction);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000408 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Scott Wakelingfe885462016-09-22 10:24:38 +0100409
410 ArmVIXLAssembler* const assembler_;
411 CodeGeneratorARMVIXL* const codegen_;
412
413 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARMVIXL);
414};
415
416class CodeGeneratorARMVIXL : public CodeGenerator {
417 public:
418 CodeGeneratorARMVIXL(HGraph* graph,
419 const ArmInstructionSetFeatures& isa_features,
420 const CompilerOptions& compiler_options,
421 OptimizingCompilerStats* stats = nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +0100422 virtual ~CodeGeneratorARMVIXL() {}
423
Scott Wakelingfe885462016-09-22 10:24:38 +0100424 void GenerateFrameEntry() OVERRIDE;
425 void GenerateFrameExit() OVERRIDE;
426 void Bind(HBasicBlock* block) OVERRIDE;
427 void MoveConstant(Location destination, int32_t value) OVERRIDE;
428 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
429 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
430
Artem Serovd4cc5b22016-11-04 11:19:09 +0000431 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
432 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
433 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
434 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
435
436 size_t GetWordSize() const OVERRIDE {
437 return static_cast<size_t>(kArmPointerSize);
438 }
439
440 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return vixl::aarch32::kRegSizeInBytes; }
441
442 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
443
444 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
445
Scott Wakelingfe885462016-09-22 10:24:38 +0100446 ArmVIXLAssembler* GetAssembler() OVERRIDE { return &assembler_; }
447
448 const ArmVIXLAssembler& GetAssembler() const OVERRIDE { return assembler_; }
449
xueliang.zhongf51bc622016-11-04 09:23:32 +0000450 ArmVIXLMacroAssembler* GetVIXLAssembler() { return GetAssembler()->GetVIXLAssembler(); }
Scott Wakelingfe885462016-09-22 10:24:38 +0100451
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100452 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
453 vixl::aarch32::Label* block_entry_label = GetLabelOf(block);
454 DCHECK(block_entry_label->IsBound());
455 return block_entry_label->GetLocation();
456 }
457
Artem Serov09a940d2016-11-11 16:15:11 +0000458 void FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +0100459 void SetupBlockedRegisters() const OVERRIDE;
460
Scott Wakelingfe885462016-09-22 10:24:38 +0100461 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
462 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
463
Artem Serovd4cc5b22016-11-04 11:19:09 +0000464 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100465 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kThumb2; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100466 // Helper method to move a 32-bit value between two locations.
467 void Move32(Location destination, Location source);
468
Scott Wakelingc34dba72016-10-03 10:14:44 +0100469 void LoadFromShiftedRegOffset(Primitive::Type type,
470 Location out_loc,
471 vixl::aarch32::Register base,
472 vixl::aarch32::Register reg_index,
473 vixl::aarch32::Condition cond = vixl::aarch32::al);
474 void StoreToShiftedRegOffset(Primitive::Type type,
475 Location out_loc,
476 vixl::aarch32::Register base,
477 vixl::aarch32::Register reg_index,
478 vixl::aarch32::Condition cond = vixl::aarch32::al);
479
Scott Wakelingfe885462016-09-22 10:24:38 +0100480 // Generate code to invoke a runtime entry point.
481 void InvokeRuntime(QuickEntrypointEnum entrypoint,
482 HInstruction* instruction,
483 uint32_t dex_pc,
484 SlowPathCode* slow_path = nullptr) OVERRIDE;
485
486 // Generate code to invoke a runtime entry point, but do not record
487 // PC-related information in a stack map.
488 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
489 HInstruction* instruction,
490 SlowPathCode* slow_path);
491
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100492 // Emit a write barrier.
493 void MarkGCCard(vixl::aarch32::Register temp,
494 vixl::aarch32::Register card,
495 vixl::aarch32::Register object,
496 vixl::aarch32::Register value,
497 bool can_be_null);
498
Artem Serovd4cc5b22016-11-04 11:19:09 +0000499 void GenerateMemoryBarrier(MemBarrierKind kind);
500
501 vixl::aarch32::Label* GetLabelOf(HBasicBlock* block) {
502 block = FirstNonEmptyBlock(block);
503 return &(block_labels_[block->GetBlockId()]);
504 }
505
Donghui Bai426b49c2016-11-08 14:55:38 +0800506 vixl32::Label* GetFinalLabel(HInstruction* instruction, vixl32::Label* final_label);
507
Artem Serovd4cc5b22016-11-04 11:19:09 +0000508 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
TatWai Chongd8c052a2016-11-02 16:12:48 +0800540 Location GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000541 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
542 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
543
544 void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
545
546 // The PcRelativePatchInfo is used for PC-relative addressing of dex cache arrays
547 // and boot image strings/types. The only difference is the interpretation of the
548 // offset_or_index. The PC-relative address is loaded with three instructions,
549 // MOVW+MOVT to load the offset to base_reg and then ADD base_reg, PC. The offset
550 // is calculated from the ADD's effective PC, i.e. PC+4 on Thumb2. Though we
551 // currently emit these 3 instructions together, instruction scheduling could
552 // split this sequence apart, so we keep separate labels for each of them.
553 struct PcRelativePatchInfo {
554 PcRelativePatchInfo(const DexFile& dex_file, uint32_t off_or_idx)
555 : target_dex_file(dex_file), offset_or_index(off_or_idx) { }
556 PcRelativePatchInfo(PcRelativePatchInfo&& other) = default;
557
558 const DexFile& target_dex_file;
559 // Either the dex cache array element offset or the string/type index.
560 uint32_t offset_or_index;
561 vixl::aarch32::Label movw_label;
562 vixl::aarch32::Label movt_label;
563 vixl::aarch32::Label add_pc_label;
564 };
565
Vladimir Marko65979462017-05-19 17:25:12 +0100566 PcRelativePatchInfo* NewPcRelativeMethodPatch(MethodReference target_method);
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);
Vladimir Marko65979462017-05-19 17:25:12 +0100569 PcRelativePatchInfo* NewPcRelativeStringPatch(const DexFile& dex_file,
570 dex::StringIndex string_index);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000571 PcRelativePatchInfo* NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
572 uint32_t element_offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100573
574 // Add a new baker read barrier patch and return the label to be bound
575 // before the BNE instruction.
576 vixl::aarch32::Label* NewBakerReadBarrierPatch(uint32_t custom_data);
577
Artem Serovc5fcb442016-12-02 19:19:58 +0000578 VIXLUInt32Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
Artem Serovc5fcb442016-12-02 19:19:58 +0000579 VIXLUInt32Literal* DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000580 dex::StringIndex string_index,
581 Handle<mirror::String> handle);
Artem Serovc5fcb442016-12-02 19:19:58 +0000582 VIXLUInt32Literal* DeduplicateJitClassLiteral(const DexFile& dex_file,
583 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000584 Handle<mirror::Class> handle);
Artem Serovc5fcb442016-12-02 19:19:58 +0000585
Artem Serovd4cc5b22016-11-04 11:19:09 +0000586 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
587
Artem Serovc5fcb442016-12-02 19:19:58 +0000588 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
589
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100590 // Maybe add the reserved entrypoint register as a temporary for field load. This temp
591 // is added only for AOT compilation if link-time generated thunks for fields are enabled.
592 void MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations);
593
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100594 // Fast path implementation of ReadBarrier::Barrier for a heap
595 // reference field load when Baker's read barriers are used.
596 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
597 Location ref,
598 vixl::aarch32::Register obj,
599 uint32_t offset,
600 Location temp,
601 bool needs_null_check);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000602 // Fast path implementation of ReadBarrier::Barrier for a heap
603 // reference array load when Baker's read barriers are used.
604 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
605 Location ref,
606 vixl::aarch32::Register obj,
607 uint32_t data_offset,
608 Location index,
609 Location temp,
610 bool needs_null_check);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100611 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
612 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
613 //
614 // Load the object reference located at the address
615 // `obj + offset + (index << scale_factor)`, held by object `obj`, into
616 // `ref`, and mark it if needed.
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100617 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
618 Location ref,
619 vixl::aarch32::Register obj,
620 uint32_t offset,
621 Location index,
622 ScaleFactor scale_factor,
623 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +0000624 bool needs_null_check);
625
626 // Generate code checking whether the the reference field at the
627 // address `obj + field_offset`, held by object `obj`, needs to be
628 // marked, and if so, marking it and updating the field within `obj`
629 // with the marked value.
630 //
631 // This routine is used for the implementation of the
632 // UnsafeCASObject intrinsic with Baker read barriers.
633 //
634 // This method has a structure similar to
635 // GenerateReferenceLoadWithBakerReadBarrier, but note that argument
636 // `ref` is only as a temporary here, and thus its value should not
637 // be used afterwards.
638 void UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
639 Location ref,
640 vixl::aarch32::Register obj,
641 Location field_offset,
642 Location temp,
643 bool needs_null_check,
644 vixl::aarch32::Register temp2);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100645
Roland Levillainba650a42017-03-06 13:52:32 +0000646 // Generate a heap reference load (with no read barrier).
647 void GenerateRawReferenceLoad(HInstruction* instruction,
648 Location ref,
649 vixl::aarch32::Register obj,
650 uint32_t offset,
651 Location index,
652 ScaleFactor scale_factor,
653 bool needs_null_check);
654
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100655 // Generate a read barrier for a heap reference within `instruction`
656 // using a slow path.
657 //
658 // A read barrier for an object reference read from the heap is
659 // implemented as a call to the artReadBarrierSlow runtime entry
660 // point, which is passed the values in locations `ref`, `obj`, and
661 // `offset`:
662 //
663 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
664 // mirror::Object* obj,
665 // uint32_t offset);
666 //
667 // The `out` location contains the value returned by
668 // artReadBarrierSlow.
669 //
670 // When `index` is provided (i.e. for array accesses), the offset
671 // value passed to artReadBarrierSlow is adjusted to take `index`
672 // into account.
673 void GenerateReadBarrierSlow(HInstruction* instruction,
674 Location out,
675 Location ref,
676 Location obj,
677 uint32_t offset,
678 Location index = Location::NoLocation());
679
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100680 // If read barriers are enabled, generate a read barrier for a heap
681 // reference using a slow path. If heap poisoning is enabled, also
682 // unpoison the reference in `out`.
683 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
684 Location out,
685 Location ref,
686 Location obj,
687 uint32_t offset,
688 Location index = Location::NoLocation());
689
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000690 // Generate a read barrier for a GC root within `instruction` using
691 // a slow path.
692 //
693 // A read barrier for an object reference GC root is implemented as
694 // a call to the artReadBarrierForRootSlow runtime entry point,
695 // which is passed the value in location `root`:
696 //
697 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
698 //
699 // The `out` location contains the value returned by
700 // artReadBarrierForRootSlow.
701 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
702
Scott Wakelingfe885462016-09-22 10:24:38 +0100703 void GenerateNop() OVERRIDE;
704
Artem Serovd4cc5b22016-11-04 11:19:09 +0000705 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
706 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
707
708 JumpTableARMVIXL* CreateJumpTable(HPackedSwitch* switch_instr) {
709 jump_tables_.emplace_back(new (GetGraph()->GetArena()) JumpTableARMVIXL(switch_instr));
710 return jump_tables_.back().get();
711 }
712 void EmitJumpTables();
713
714 void EmitMovwMovtPlaceholder(CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
715 vixl::aarch32::Register out);
716
Anton Kirilov5601d4e2017-05-11 19:33:50 +0100717 // `temp` is an extra temporary register that is used for some conditions;
718 // callers may not specify it, in which case the method will use a scratch
719 // register instead.
720 void GenerateConditionWithZero(IfCondition condition,
721 vixl::aarch32::Register out,
722 vixl::aarch32::Register in,
723 vixl::aarch32::Register temp = vixl32::Register());
724
Scott Wakelingfe885462016-09-22 10:24:38 +0100725 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100726 vixl::aarch32::Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
727 vixl::aarch32::Register temp);
728
Artem Serovc5fcb442016-12-02 19:19:58 +0000729 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, VIXLUInt32Literal*>;
Artem Serovc5fcb442016-12-02 19:19:58 +0000730 using StringToLiteralMap = ArenaSafeMap<StringReference,
731 VIXLUInt32Literal*,
732 StringReferenceValueComparator>;
733 using TypeToLiteralMap = ArenaSafeMap<TypeReference,
734 VIXLUInt32Literal*,
735 TypeReferenceValueComparator>;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000736
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100737 struct BakerReadBarrierPatchInfo {
738 explicit BakerReadBarrierPatchInfo(uint32_t data) : label(), custom_data(data) { }
739
740 vixl::aarch32::Label label;
741 uint32_t custom_data;
742 };
743
Artem Serovc5fcb442016-12-02 19:19:58 +0000744 VIXLUInt32Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000745 PcRelativePatchInfo* NewPcRelativePatch(const DexFile& dex_file,
746 uint32_t offset_or_index,
747 ArenaDeque<PcRelativePatchInfo>* patches);
748 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
749 static void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
750 ArenaVector<LinkerPatch>* linker_patches);
751
Scott Wakelingfe885462016-09-22 10:24:38 +0100752 // Labels for each block that will be compiled.
753 // We use a deque so that the `vixl::aarch32::Label` objects do not move in memory.
754 ArenaDeque<vixl::aarch32::Label> block_labels_; // Indexed by block id.
755 vixl::aarch32::Label frame_entry_label_;
756
Artem Serov551b28f2016-10-18 19:11:30 +0100757 ArenaVector<std::unique_ptr<JumpTableARMVIXL>> jump_tables_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100758 LocationsBuilderARMVIXL location_builder_;
759 InstructionCodeGeneratorARMVIXL instruction_visitor_;
760 ParallelMoveResolverARMVIXL move_resolver_;
761
762 ArmVIXLAssembler assembler_;
763 const ArmInstructionSetFeatures& isa_features_;
764
Artem Serovc5fcb442016-12-02 19:19:58 +0000765 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
766 Uint32ToLiteralMap uint32_literals_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000767 // PC-relative patch info for each HArmDexCacheArraysBase.
768 ArenaDeque<PcRelativePatchInfo> pc_relative_dex_cache_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100769 // PC-relative method patch info for kBootImageLinkTimePcRelative.
770 ArenaDeque<PcRelativePatchInfo> pc_relative_method_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000771 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Artem Serovd4cc5b22016-11-04 11:19:09 +0000772 ArenaDeque<PcRelativePatchInfo> pc_relative_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000773 // PC-relative type patch info for kBssEntry.
774 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100775 // PC-relative String patch info; type depends on configuration (app .bss or boot image PIC).
776 ArenaDeque<PcRelativePatchInfo> pc_relative_string_patches_;
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100777 // Baker read barrier patch info.
778 ArenaDeque<BakerReadBarrierPatchInfo> baker_read_barrier_patches_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000779
780 // Patches for string literals in JIT compiled code.
781 StringToLiteralMap jit_string_patches_;
782 // Patches for class literals in JIT compiled code.
783 TypeToLiteralMap jit_class_patches_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000784
Scott Wakelingfe885462016-09-22 10:24:38 +0100785 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARMVIXL);
786};
787
Scott Wakelingfe885462016-09-22 10:24:38 +0100788} // namespace arm
789} // namespace art
790
791#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM_VIXL_H_