blob: 805a3f4366cd452eb639e54fc0d6f3f974b8d87e [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"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070027#include "type_reference.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010028#include "utils/arm/assembler_arm_vixl.h"
29
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,
Anton Kirilovfd522532017-05-10 12:46:57 +0100403 vixl::aarch32::Label* false_target,
404 bool is_far_target = true);
Scott Wakelingfe885462016-09-22 10:24:38 +0100405 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
406 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
407 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
408 void GenerateDivRemConstantIntegral(HBinaryOperation* instruction);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000409 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Scott Wakelingfe885462016-09-22 10:24:38 +0100410
411 ArmVIXLAssembler* const assembler_;
412 CodeGeneratorARMVIXL* const codegen_;
413
414 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARMVIXL);
415};
416
417class CodeGeneratorARMVIXL : public CodeGenerator {
418 public:
419 CodeGeneratorARMVIXL(HGraph* graph,
420 const ArmInstructionSetFeatures& isa_features,
421 const CompilerOptions& compiler_options,
422 OptimizingCompilerStats* stats = nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +0100423 virtual ~CodeGeneratorARMVIXL() {}
424
Scott Wakelingfe885462016-09-22 10:24:38 +0100425 void GenerateFrameEntry() OVERRIDE;
426 void GenerateFrameExit() OVERRIDE;
427 void Bind(HBasicBlock* block) OVERRIDE;
428 void MoveConstant(Location destination, int32_t value) OVERRIDE;
429 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
430 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
431
Artem Serovd4cc5b22016-11-04 11:19:09 +0000432 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
433 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
434 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
435 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
436
437 size_t GetWordSize() const OVERRIDE {
438 return static_cast<size_t>(kArmPointerSize);
439 }
440
441 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return vixl::aarch32::kRegSizeInBytes; }
442
443 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
444
445 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
446
Scott Wakelingfe885462016-09-22 10:24:38 +0100447 ArmVIXLAssembler* GetAssembler() OVERRIDE { return &assembler_; }
448
449 const ArmVIXLAssembler& GetAssembler() const OVERRIDE { return assembler_; }
450
xueliang.zhongf51bc622016-11-04 09:23:32 +0000451 ArmVIXLMacroAssembler* GetVIXLAssembler() { return GetAssembler()->GetVIXLAssembler(); }
Scott Wakelingfe885462016-09-22 10:24:38 +0100452
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100453 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
454 vixl::aarch32::Label* block_entry_label = GetLabelOf(block);
455 DCHECK(block_entry_label->IsBound());
456 return block_entry_label->GetLocation();
457 }
458
Artem Serov09a940d2016-11-11 16:15:11 +0000459 void FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +0100460 void SetupBlockedRegisters() const OVERRIDE;
461
Scott Wakelingfe885462016-09-22 10:24:38 +0100462 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
463 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
464
Artem Serovd4cc5b22016-11-04 11:19:09 +0000465 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100466 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kThumb2; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100467 // Helper method to move a 32-bit value between two locations.
468 void Move32(Location destination, Location source);
469
Scott Wakelingc34dba72016-10-03 10:14:44 +0100470 void LoadFromShiftedRegOffset(Primitive::Type type,
471 Location out_loc,
472 vixl::aarch32::Register base,
473 vixl::aarch32::Register reg_index,
474 vixl::aarch32::Condition cond = vixl::aarch32::al);
475 void StoreToShiftedRegOffset(Primitive::Type type,
476 Location out_loc,
477 vixl::aarch32::Register base,
478 vixl::aarch32::Register reg_index,
479 vixl::aarch32::Condition cond = vixl::aarch32::al);
480
Scott Wakelingfe885462016-09-22 10:24:38 +0100481 // Generate code to invoke a runtime entry point.
482 void InvokeRuntime(QuickEntrypointEnum entrypoint,
483 HInstruction* instruction,
484 uint32_t dex_pc,
485 SlowPathCode* slow_path = nullptr) OVERRIDE;
486
487 // Generate code to invoke a runtime entry point, but do not record
488 // PC-related information in a stack map.
489 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
490 HInstruction* instruction,
491 SlowPathCode* slow_path);
492
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100493 // Emit a write barrier.
494 void MarkGCCard(vixl::aarch32::Register temp,
495 vixl::aarch32::Register card,
496 vixl::aarch32::Register object,
497 vixl::aarch32::Register value,
498 bool can_be_null);
499
Artem Serovd4cc5b22016-11-04 11:19:09 +0000500 void GenerateMemoryBarrier(MemBarrierKind kind);
501
502 vixl::aarch32::Label* GetLabelOf(HBasicBlock* block) {
503 block = FirstNonEmptyBlock(block);
504 return &(block_labels_[block->GetBlockId()]);
505 }
506
Donghui Bai426b49c2016-11-08 14:55:38 +0800507 vixl32::Label* GetFinalLabel(HInstruction* instruction, vixl32::Label* final_label);
508
Artem Serovd4cc5b22016-11-04 11:19:09 +0000509 void Initialize() OVERRIDE {
510 block_labels_.resize(GetGraph()->GetBlocks().size());
511 }
512
513 void Finalize(CodeAllocator* allocator) OVERRIDE;
514
515 const ArmInstructionSetFeatures& GetInstructionSetFeatures() const { return isa_features_; }
516
517 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
518 return type == Primitive::kPrimDouble || type == Primitive::kPrimLong;
519 }
520
521 void ComputeSpillMask() OVERRIDE;
522
523 vixl::aarch32::Label* GetFrameEntryLabel() { return &frame_entry_label_; }
524
525 // Check if the desired_string_load_kind is supported. If it is, return it,
526 // otherwise return a fall-back kind that should be used instead.
527 HLoadString::LoadKind GetSupportedLoadStringKind(
528 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
529
530 // Check if the desired_class_load_kind is supported. If it is, return it,
531 // otherwise return a fall-back kind that should be used instead.
532 HLoadClass::LoadKind GetSupportedLoadClassKind(
533 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
534
535 // Check if the desired_dispatch_info is supported. If it is, return it,
536 // otherwise return a fall-back info that should be used instead.
537 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
538 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
539 HInvokeStaticOrDirect* invoke) OVERRIDE;
540
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100541 void GenerateStaticOrDirectCall(
542 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
543 void GenerateVirtualCall(
544 HInvokeVirtual* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000545
546 void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
547
548 // The PcRelativePatchInfo is used for PC-relative addressing of dex cache arrays
549 // and boot image strings/types. The only difference is the interpretation of the
550 // offset_or_index. The PC-relative address is loaded with three instructions,
551 // MOVW+MOVT to load the offset to base_reg and then ADD base_reg, PC. The offset
552 // is calculated from the ADD's effective PC, i.e. PC+4 on Thumb2. Though we
553 // currently emit these 3 instructions together, instruction scheduling could
554 // split this sequence apart, so we keep separate labels for each of them.
555 struct PcRelativePatchInfo {
556 PcRelativePatchInfo(const DexFile& dex_file, uint32_t off_or_idx)
557 : target_dex_file(dex_file), offset_or_index(off_or_idx) { }
558 PcRelativePatchInfo(PcRelativePatchInfo&& other) = default;
559
560 const DexFile& target_dex_file;
561 // Either the dex cache array element offset or the string/type index.
562 uint32_t offset_or_index;
563 vixl::aarch32::Label movw_label;
564 vixl::aarch32::Label movt_label;
565 vixl::aarch32::Label add_pc_label;
566 };
567
Vladimir Marko65979462017-05-19 17:25:12 +0100568 PcRelativePatchInfo* NewPcRelativeMethodPatch(MethodReference target_method);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100569 PcRelativePatchInfo* NewMethodBssEntryPatch(MethodReference target_method);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000570 PcRelativePatchInfo* NewPcRelativeTypePatch(const DexFile& dex_file, dex::TypeIndex type_index);
Vladimir Marko1998cd02017-01-13 13:02:58 +0000571 PcRelativePatchInfo* NewTypeBssEntryPatch(const DexFile& dex_file, dex::TypeIndex type_index);
Vladimir Marko65979462017-05-19 17:25:12 +0100572 PcRelativePatchInfo* NewPcRelativeStringPatch(const DexFile& dex_file,
573 dex::StringIndex string_index);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100574
575 // Add a new baker read barrier patch and return the label to be bound
576 // before the BNE instruction.
577 vixl::aarch32::Label* NewBakerReadBarrierPatch(uint32_t custom_data);
578
Artem Serovc5fcb442016-12-02 19:19:58 +0000579 VIXLUInt32Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
Artem Serovc5fcb442016-12-02 19:19:58 +0000580 VIXLUInt32Literal* DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000581 dex::StringIndex string_index,
582 Handle<mirror::String> handle);
Artem Serovc5fcb442016-12-02 19:19:58 +0000583 VIXLUInt32Literal* DeduplicateJitClassLiteral(const DexFile& dex_file,
584 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000585 Handle<mirror::Class> handle);
Artem Serovc5fcb442016-12-02 19:19:58 +0000586
Artem Serovd4cc5b22016-11-04 11:19:09 +0000587 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
588
Artem Serovc5fcb442016-12-02 19:19:58 +0000589 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
590
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100591 // Maybe add the reserved entrypoint register as a temporary for field load. This temp
592 // is added only for AOT compilation if link-time generated thunks for fields are enabled.
593 void MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations);
594
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100595 // Fast path implementation of ReadBarrier::Barrier for a heap
596 // reference field load when Baker's read barriers are used.
597 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
598 Location ref,
599 vixl::aarch32::Register obj,
600 uint32_t offset,
601 Location temp,
602 bool needs_null_check);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000603 // Fast path implementation of ReadBarrier::Barrier for a heap
604 // reference array load when Baker's read barriers are used.
605 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
606 Location ref,
607 vixl::aarch32::Register obj,
608 uint32_t data_offset,
609 Location index,
610 Location temp,
611 bool needs_null_check);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100612 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
613 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
614 //
615 // Load the object reference located at the address
616 // `obj + offset + (index << scale_factor)`, held by object `obj`, into
617 // `ref`, and mark it if needed.
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100618 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
619 Location ref,
620 vixl::aarch32::Register obj,
621 uint32_t offset,
622 Location index,
623 ScaleFactor scale_factor,
624 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +0000625 bool needs_null_check);
626
627 // Generate code checking whether the the reference field at the
628 // address `obj + field_offset`, held by object `obj`, needs to be
629 // marked, and if so, marking it and updating the field within `obj`
630 // with the marked value.
631 //
632 // This routine is used for the implementation of the
633 // UnsafeCASObject intrinsic with Baker read barriers.
634 //
635 // This method has a structure similar to
636 // GenerateReferenceLoadWithBakerReadBarrier, but note that argument
637 // `ref` is only as a temporary here, and thus its value should not
638 // be used afterwards.
639 void UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
640 Location ref,
641 vixl::aarch32::Register obj,
642 Location field_offset,
643 Location temp,
644 bool needs_null_check,
645 vixl::aarch32::Register temp2);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100646
Roland Levillainba650a42017-03-06 13:52:32 +0000647 // Generate a heap reference load (with no read barrier).
648 void GenerateRawReferenceLoad(HInstruction* instruction,
649 Location ref,
650 vixl::aarch32::Register obj,
651 uint32_t offset,
652 Location index,
653 ScaleFactor scale_factor,
654 bool needs_null_check);
655
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100656 // Generate a read barrier for a heap reference within `instruction`
657 // using a slow path.
658 //
659 // A read barrier for an object reference read from the heap is
660 // implemented as a call to the artReadBarrierSlow runtime entry
661 // point, which is passed the values in locations `ref`, `obj`, and
662 // `offset`:
663 //
664 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
665 // mirror::Object* obj,
666 // uint32_t offset);
667 //
668 // The `out` location contains the value returned by
669 // artReadBarrierSlow.
670 //
671 // When `index` is provided (i.e. for array accesses), the offset
672 // value passed to artReadBarrierSlow is adjusted to take `index`
673 // into account.
674 void GenerateReadBarrierSlow(HInstruction* instruction,
675 Location out,
676 Location ref,
677 Location obj,
678 uint32_t offset,
679 Location index = Location::NoLocation());
680
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100681 // If read barriers are enabled, generate a read barrier for a heap
682 // reference using a slow path. If heap poisoning is enabled, also
683 // unpoison the reference in `out`.
684 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
685 Location out,
686 Location ref,
687 Location obj,
688 uint32_t offset,
689 Location index = Location::NoLocation());
690
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000691 // Generate a read barrier for a GC root within `instruction` using
692 // a slow path.
693 //
694 // A read barrier for an object reference GC root is implemented as
695 // a call to the artReadBarrierForRootSlow runtime entry point,
696 // which is passed the value in location `root`:
697 //
698 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
699 //
700 // The `out` location contains the value returned by
701 // artReadBarrierForRootSlow.
702 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
703
Scott Wakelingfe885462016-09-22 10:24:38 +0100704 void GenerateNop() OVERRIDE;
705
Artem Serovd4cc5b22016-11-04 11:19:09 +0000706 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
707 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
708
709 JumpTableARMVIXL* CreateJumpTable(HPackedSwitch* switch_instr) {
710 jump_tables_.emplace_back(new (GetGraph()->GetArena()) JumpTableARMVIXL(switch_instr));
711 return jump_tables_.back().get();
712 }
713 void EmitJumpTables();
714
715 void EmitMovwMovtPlaceholder(CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
716 vixl::aarch32::Register out);
717
Anton Kirilov5601d4e2017-05-11 19:33:50 +0100718 // `temp` is an extra temporary register that is used for some conditions;
719 // callers may not specify it, in which case the method will use a scratch
720 // register instead.
721 void GenerateConditionWithZero(IfCondition condition,
722 vixl::aarch32::Register out,
723 vixl::aarch32::Register in,
724 vixl::aarch32::Register temp = vixl32::Register());
725
Scott Wakelingfe885462016-09-22 10:24:38 +0100726 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100727 vixl::aarch32::Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
728 vixl::aarch32::Register temp);
729
Artem Serovc5fcb442016-12-02 19:19:58 +0000730 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, VIXLUInt32Literal*>;
Artem Serovc5fcb442016-12-02 19:19:58 +0000731 using StringToLiteralMap = ArenaSafeMap<StringReference,
732 VIXLUInt32Literal*,
733 StringReferenceValueComparator>;
734 using TypeToLiteralMap = ArenaSafeMap<TypeReference,
735 VIXLUInt32Literal*,
736 TypeReferenceValueComparator>;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000737
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100738 struct BakerReadBarrierPatchInfo {
739 explicit BakerReadBarrierPatchInfo(uint32_t data) : label(), custom_data(data) { }
740
741 vixl::aarch32::Label label;
742 uint32_t custom_data;
743 };
744
Artem Serovc5fcb442016-12-02 19:19:58 +0000745 VIXLUInt32Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000746 PcRelativePatchInfo* NewPcRelativePatch(const DexFile& dex_file,
747 uint32_t offset_or_index,
748 ArenaDeque<PcRelativePatchInfo>* patches);
749 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
750 static void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
751 ArenaVector<LinkerPatch>* linker_patches);
752
Scott Wakelingfe885462016-09-22 10:24:38 +0100753 // Labels for each block that will be compiled.
754 // We use a deque so that the `vixl::aarch32::Label` objects do not move in memory.
755 ArenaDeque<vixl::aarch32::Label> block_labels_; // Indexed by block id.
756 vixl::aarch32::Label frame_entry_label_;
757
Artem Serov551b28f2016-10-18 19:11:30 +0100758 ArenaVector<std::unique_ptr<JumpTableARMVIXL>> jump_tables_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100759 LocationsBuilderARMVIXL location_builder_;
760 InstructionCodeGeneratorARMVIXL instruction_visitor_;
761 ParallelMoveResolverARMVIXL move_resolver_;
762
763 ArmVIXLAssembler assembler_;
764 const ArmInstructionSetFeatures& isa_features_;
765
Artem Serovc5fcb442016-12-02 19:19:58 +0000766 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
767 Uint32ToLiteralMap uint32_literals_;
Vladimir Marko65979462017-05-19 17:25:12 +0100768 // PC-relative method patch info for kBootImageLinkTimePcRelative.
769 ArenaDeque<PcRelativePatchInfo> pc_relative_method_patches_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100770 // PC-relative method patch info for kBssEntry.
771 ArenaDeque<PcRelativePatchInfo> method_bss_entry_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000772 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Artem Serovd4cc5b22016-11-04 11:19:09 +0000773 ArenaDeque<PcRelativePatchInfo> pc_relative_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000774 // PC-relative type patch info for kBssEntry.
775 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100776 // PC-relative String patch info; type depends on configuration (app .bss or boot image PIC).
777 ArenaDeque<PcRelativePatchInfo> pc_relative_string_patches_;
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100778 // Baker read barrier patch info.
779 ArenaDeque<BakerReadBarrierPatchInfo> baker_read_barrier_patches_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000780
781 // Patches for string literals in JIT compiled code.
782 StringToLiteralMap jit_string_patches_;
783 // Patches for class literals in JIT compiled code.
784 TypeToLiteralMap jit_class_patches_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000785
Scott Wakelingfe885462016-09-22 10:24:38 +0100786 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARMVIXL);
787};
788
Scott Wakelingfe885462016-09-22 10:24:38 +0100789} // namespace arm
790} // namespace art
791
792#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM_VIXL_H_