blob: e5b31cf9d82ac701dab950ef5939e686ec684aa5 [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"
Vladimir Marko7968cae2021-01-19 12:02:35 +000021#include "class_root.h"
Artem Serovd4cc5b22016-11-04 11:19:09 +000022#include "code_generator.h"
Artem Serovcfbe9132016-10-14 15:58:56 +010023#include "common_arm.h"
David Sehr312f3b22018-03-19 08:39:26 -070024#include "dex/string_reference.h"
25#include "dex/type_reference.h"
Artem Serovd4cc5b22016-11-04 11:19:09 +000026#include "driver/compiler_options.h"
27#include "nodes.h"
Artem Serovd4cc5b22016-11-04 11:19:09 +000028#include "parallel_move_resolver.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010029#include "utils/arm/assembler_arm_vixl.h"
30
31// TODO(VIXL): make vixl clean wrt -Wshadow.
32#pragma GCC diagnostic push
33#pragma GCC diagnostic ignored "-Wshadow"
34#include "aarch32/constants-aarch32.h"
35#include "aarch32/instructions-aarch32.h"
36#include "aarch32/macro-assembler-aarch32.h"
37#pragma GCC diagnostic pop
38
Vladimir Marko0a516052019-10-14 13:00:44 +000039namespace art {
Vladimir Markoca1e0382018-04-11 09:58:41 +000040
41namespace linker {
42class Thumb2RelativePatcherTest;
43} // namespace linker
44
Scott Wakelingfe885462016-09-22 10:24:38 +010045namespace arm {
46
Roland Levillainba650a42017-03-06 13:52:32 +000047// This constant is used as an approximate margin when emission of veneer and literal pools
48// must be blocked.
49static constexpr int kMaxMacroInstructionSizeInBytes =
50 15 * vixl::aarch32::kMaxInstructionSizeInBytes;
51
Scott Wakelinga7812ae2016-10-17 10:03:36 +010052static const vixl::aarch32::Register kParameterCoreRegistersVIXL[] = {
53 vixl::aarch32::r1,
54 vixl::aarch32::r2,
55 vixl::aarch32::r3
56};
Artem Serovd4cc5b22016-11-04 11:19:09 +000057static const size_t kParameterCoreRegistersLengthVIXL = arraysize(kParameterCoreRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058static const vixl::aarch32::SRegister kParameterFpuRegistersVIXL[] = {
59 vixl::aarch32::s0,
60 vixl::aarch32::s1,
61 vixl::aarch32::s2,
62 vixl::aarch32::s3,
63 vixl::aarch32::s4,
64 vixl::aarch32::s5,
65 vixl::aarch32::s6,
66 vixl::aarch32::s7,
67 vixl::aarch32::s8,
68 vixl::aarch32::s9,
69 vixl::aarch32::s10,
70 vixl::aarch32::s11,
71 vixl::aarch32::s12,
72 vixl::aarch32::s13,
73 vixl::aarch32::s14,
74 vixl::aarch32::s15
75};
Artem Serovd4cc5b22016-11-04 11:19:09 +000076static const size_t kParameterFpuRegistersLengthVIXL = arraysize(kParameterFpuRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +010077
Scott Wakelingfe885462016-09-22 10:24:38 +010078static const vixl::aarch32::Register kMethodRegister = vixl::aarch32::r0;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010079
Roland Levillain6d729a72017-06-30 18:34:01 +010080// Callee saves core registers r5, r6, r7, r8 (except when emitting Baker
81// read barriers, where it is used as Marking Register), r10, r11, and lr.
Scott Wakelinga7812ae2016-10-17 10:03:36 +010082static const vixl::aarch32::RegisterList kCoreCalleeSaves = vixl::aarch32::RegisterList::Union(
83 vixl::aarch32::RegisterList(vixl::aarch32::r5,
84 vixl::aarch32::r6,
Roland Levillain6d729a72017-06-30 18:34:01 +010085 vixl::aarch32::r7),
86 // Do not consider r8 as a callee-save register with Baker read barriers.
Lokesh Gidra88ccd922022-09-28 23:23:43 -070087 (kReserveMarkingRegister
Roland Levillain6d729a72017-06-30 18:34:01 +010088 ? vixl::aarch32::RegisterList()
89 : vixl::aarch32::RegisterList(vixl::aarch32::r8)),
Scott Wakelinga7812ae2016-10-17 10:03:36 +010090 vixl::aarch32::RegisterList(vixl::aarch32::r10,
91 vixl::aarch32::r11,
92 vixl::aarch32::lr));
93
94// Callee saves FP registers s16 to s31 inclusive.
Scott Wakelingfe885462016-09-22 10:24:38 +010095static const vixl::aarch32::SRegisterList kFpuCalleeSaves =
96 vixl::aarch32::SRegisterList(vixl::aarch32::s16, 16);
97
Scott Wakelinga7812ae2016-10-17 10:03:36 +010098static const vixl::aarch32::Register kRuntimeParameterCoreRegistersVIXL[] = {
99 vixl::aarch32::r0,
100 vixl::aarch32::r1,
101 vixl::aarch32::r2,
102 vixl::aarch32::r3
103};
104static const size_t kRuntimeParameterCoreRegistersLengthVIXL =
Artem Serovd4cc5b22016-11-04 11:19:09 +0000105 arraysize(kRuntimeParameterCoreRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100106static const vixl::aarch32::SRegister kRuntimeParameterFpuRegistersVIXL[] = {
107 vixl::aarch32::s0,
108 vixl::aarch32::s1,
109 vixl::aarch32::s2,
110 vixl::aarch32::s3
111};
112static const size_t kRuntimeParameterFpuRegistersLengthVIXL =
Artem Serovd4cc5b22016-11-04 11:19:09 +0000113 arraysize(kRuntimeParameterFpuRegistersVIXL);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100114
115class LoadClassSlowPathARMVIXL;
Scott Wakelingfe885462016-09-22 10:24:38 +0100116class CodeGeneratorARMVIXL;
117
Artem Serovc5fcb442016-12-02 19:19:58 +0000118using VIXLInt32Literal = vixl::aarch32::Literal<int32_t>;
119using VIXLUInt32Literal = vixl::aarch32::Literal<uint32_t>;
120
Artem Serov551b28f2016-10-18 19:11:30 +0100121class JumpTableARMVIXL : public DeletableArenaObject<kArenaAllocSwitchTable> {
122 public:
123 explicit JumpTableARMVIXL(HPackedSwitch* switch_instr)
Artem Serov09a940d2016-11-11 16:15:11 +0000124 : switch_instr_(switch_instr),
125 table_start_(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100126 bb_addresses_(switch_instr->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Artem Serov09a940d2016-11-11 16:15:11 +0000127 uint32_t num_entries = switch_instr_->GetNumEntries();
128 for (uint32_t i = 0; i < num_entries; i++) {
Artem Serovc5fcb442016-12-02 19:19:58 +0000129 VIXLInt32Literal *lit = new VIXLInt32Literal(0, vixl32::RawLiteral::kManuallyPlaced);
Artem Serov09a940d2016-11-11 16:15:11 +0000130 bb_addresses_.emplace_back(lit);
131 }
132 }
Artem Serov551b28f2016-10-18 19:11:30 +0100133
134 vixl::aarch32::Label* GetTableStartLabel() { return &table_start_; }
135
136 void EmitTable(CodeGeneratorARMVIXL* codegen);
Artem Serov09a940d2016-11-11 16:15:11 +0000137 void FixTable(CodeGeneratorARMVIXL* codegen);
Artem Serov551b28f2016-10-18 19:11:30 +0100138
139 private:
140 HPackedSwitch* const switch_instr_;
141 vixl::aarch32::Label table_start_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000142 ArenaVector<std::unique_ptr<VIXLInt32Literal>> bb_addresses_;
Artem Serov551b28f2016-10-18 19:11:30 +0100143
144 DISALLOW_COPY_AND_ASSIGN(JumpTableARMVIXL);
145};
146
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100147class InvokeRuntimeCallingConventionARMVIXL
148 : public CallingConvention<vixl::aarch32::Register, vixl::aarch32::SRegister> {
149 public:
150 InvokeRuntimeCallingConventionARMVIXL()
151 : CallingConvention(kRuntimeParameterCoreRegistersVIXL,
152 kRuntimeParameterCoreRegistersLengthVIXL,
153 kRuntimeParameterFpuRegistersVIXL,
154 kRuntimeParameterFpuRegistersLengthVIXL,
155 kArmPointerSize) {}
156
157 private:
158 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConventionARMVIXL);
159};
160
161class InvokeDexCallingConventionARMVIXL
162 : public CallingConvention<vixl::aarch32::Register, vixl::aarch32::SRegister> {
163 public:
164 InvokeDexCallingConventionARMVIXL()
165 : CallingConvention(kParameterCoreRegistersVIXL,
166 kParameterCoreRegistersLengthVIXL,
167 kParameterFpuRegistersVIXL,
168 kParameterFpuRegistersLengthVIXL,
169 kArmPointerSize) {}
170
171 private:
172 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionARMVIXL);
173};
174
Artem Serovd4cc5b22016-11-04 11:19:09 +0000175class InvokeDexCallingConventionVisitorARMVIXL : public InvokeDexCallingConventionVisitor {
176 public:
177 InvokeDexCallingConventionVisitorARMVIXL() {}
178 virtual ~InvokeDexCallingConventionVisitorARMVIXL() {}
179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100180 Location GetNextLocation(DataType::Type type) override;
181 Location GetReturnLocation(DataType::Type type) const override;
182 Location GetMethodLocation() const override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000183
184 private:
185 InvokeDexCallingConventionARMVIXL calling_convention;
186 uint32_t double_index_ = 0;
187
188 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorARMVIXL);
189};
190
Vladimir Marko86c87522020-05-11 16:55:55 +0100191class CriticalNativeCallingConventionVisitorARMVIXL : public InvokeDexCallingConventionVisitor {
192 public:
193 explicit CriticalNativeCallingConventionVisitorARMVIXL(bool for_register_allocation)
194 : for_register_allocation_(for_register_allocation) {}
195
196 virtual ~CriticalNativeCallingConventionVisitorARMVIXL() {}
197
198 Location GetNextLocation(DataType::Type type) override;
199 Location GetReturnLocation(DataType::Type type) const override;
200 Location GetMethodLocation() const override;
201
202 size_t GetStackOffset() const { return stack_offset_; }
203
204 private:
205 // Register allocator does not support adjusting frame size, so we cannot provide final locations
206 // of stack arguments for register allocation. We ask the register allocator for any location and
207 // move these arguments to the right place after adjusting the SP when generating the call.
208 const bool for_register_allocation_;
209 size_t gpr_index_ = 0u;
210 size_t stack_offset_ = 0u;
211
212 DISALLOW_COPY_AND_ASSIGN(CriticalNativeCallingConventionVisitorARMVIXL);
213};
214
Artem Serovcfbe9132016-10-14 15:58:56 +0100215class FieldAccessCallingConventionARMVIXL : public FieldAccessCallingConvention {
216 public:
217 FieldAccessCallingConventionARMVIXL() {}
218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100219 Location GetObjectLocation() const override {
Artem Serovcfbe9132016-10-14 15:58:56 +0100220 return helpers::LocationFrom(vixl::aarch32::r1);
221 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100222 Location GetFieldIndexLocation() const override {
Artem Serovcfbe9132016-10-14 15:58:56 +0100223 return helpers::LocationFrom(vixl::aarch32::r0);
224 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100225 Location GetReturnLocation(DataType::Type type) const override {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100226 return DataType::Is64BitType(type)
Artem Serovcfbe9132016-10-14 15:58:56 +0100227 ? helpers::LocationFrom(vixl::aarch32::r0, vixl::aarch32::r1)
228 : helpers::LocationFrom(vixl::aarch32::r0);
229 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100230 Location GetSetValueLocation(DataType::Type type, bool is_instance) const override {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100231 return DataType::Is64BitType(type)
Nicolas Geoffraya72859d2017-01-26 22:47:27 +0000232 ? helpers::LocationFrom(vixl::aarch32::r2, vixl::aarch32::r3)
Artem Serovcfbe9132016-10-14 15:58:56 +0100233 : (is_instance
234 ? helpers::LocationFrom(vixl::aarch32::r2)
235 : helpers::LocationFrom(vixl::aarch32::r1));
236 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100237 Location GetFpuLocation(DataType::Type type) const override {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100238 return DataType::Is64BitType(type)
Artem Serovcfbe9132016-10-14 15:58:56 +0100239 ? helpers::LocationFrom(vixl::aarch32::s0, vixl::aarch32::s1)
240 : helpers::LocationFrom(vixl::aarch32::s0);
241 }
242
243 private:
244 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionARMVIXL);
245};
246
Scott Wakelingfe885462016-09-22 10:24:38 +0100247class SlowPathCodeARMVIXL : public SlowPathCode {
248 public:
249 explicit SlowPathCodeARMVIXL(HInstruction* instruction)
250 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
251
252 vixl::aarch32::Label* GetEntryLabel() { return &entry_label_; }
253 vixl::aarch32::Label* GetExitLabel() { return &exit_label_; }
254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100255 void SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) override;
256 void RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100257
258 private:
259 vixl::aarch32::Label entry_label_;
260 vixl::aarch32::Label exit_label_;
261
262 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARMVIXL);
263};
264
265class ParallelMoveResolverARMVIXL : public ParallelMoveResolverWithSwap {
266 public:
267 ParallelMoveResolverARMVIXL(ArenaAllocator* allocator, CodeGeneratorARMVIXL* codegen)
268 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
269
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100270 void EmitMove(size_t index) override;
271 void EmitSwap(size_t index) override;
272 void SpillScratch(int reg) override;
273 void RestoreScratch(int reg) override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100274
275 ArmVIXLAssembler* GetAssembler() const;
276
277 private:
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100278 void Exchange(vixl32::Register reg, int mem);
Scott Wakelingfe885462016-09-22 10:24:38 +0100279 void Exchange(int mem1, int mem2);
280
281 CodeGeneratorARMVIXL* const codegen_;
282
283 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARMVIXL);
284};
285
Scott Wakelingfe885462016-09-22 10:24:38 +0100286class LocationsBuilderARMVIXL : public HGraphVisitor {
287 public:
288 LocationsBuilderARMVIXL(HGraph* graph, CodeGeneratorARMVIXL* codegen)
289 : HGraphVisitor(graph), codegen_(codegen) {}
290
Artem Serovd4cc5b22016-11-04 11:19:09 +0000291#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100292 void Visit##name(H##name* instr) override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100293
Artem Serovd4cc5b22016-11-04 11:19:09 +0000294 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
295 FOR_EACH_CONCRETE_INSTRUCTION_ARM(DECLARE_VISIT_INSTRUCTION)
296 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
Scott Wakelingfe885462016-09-22 10:24:38 +0100297
Artem Serovd4cc5b22016-11-04 11:19:09 +0000298#undef DECLARE_VISIT_INSTRUCTION
299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100300 void VisitInstruction(HInstruction* instruction) override {
Artem Serovd4cc5b22016-11-04 11:19:09 +0000301 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
302 << " (id " << instruction->GetId() << ")";
Scott Wakelingfe885462016-09-22 10:24:38 +0100303 }
304
Artem Serovd4cc5b22016-11-04 11:19:09 +0000305 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100306 void HandleInvoke(HInvoke* invoke);
Artem Serov02109dd2016-09-23 17:17:54 +0100307 void HandleBitwiseOperation(HBinaryOperation* operation, Opcode opcode);
Scott Wakelingfe885462016-09-22 10:24:38 +0100308 void HandleCondition(HCondition* condition);
Artem Serov02109dd2016-09-23 17:17:54 +0100309 void HandleIntegerRotate(LocationSummary* locations);
310 void HandleLongRotate(LocationSummary* locations);
311 void HandleShift(HBinaryOperation* operation);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100312 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
313 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Scott Wakelingfe885462016-09-22 10:24:38 +0100314
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100315 Location ArithmeticZeroOrFpuRegister(HInstruction* input);
Artem Serov02109dd2016-09-23 17:17:54 +0100316 Location ArmEncodableConstantOrRegister(HInstruction* constant, Opcode opcode);
317 bool CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode);
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100318
Scott Wakelingfe885462016-09-22 10:24:38 +0100319 CodeGeneratorARMVIXL* const codegen_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000320 InvokeDexCallingConventionVisitorARMVIXL parameter_visitor_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100321
322 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARMVIXL);
323};
324
325class InstructionCodeGeneratorARMVIXL : public InstructionCodeGenerator {
326 public:
327 InstructionCodeGeneratorARMVIXL(HGraph* graph, CodeGeneratorARMVIXL* codegen);
328
Artem Serovd4cc5b22016-11-04 11:19:09 +0000329#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100330 void Visit##name(H##name* instr) override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100331
Artem Serovd4cc5b22016-11-04 11:19:09 +0000332 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
333 FOR_EACH_CONCRETE_INSTRUCTION_ARM(DECLARE_VISIT_INSTRUCTION)
334 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
335
336#undef DECLARE_VISIT_INSTRUCTION
337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100338 void VisitInstruction(HInstruction* instruction) override {
Artem Serovd4cc5b22016-11-04 11:19:09 +0000339 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
340 << " (id " << instruction->GetId() << ")";
341 }
Scott Wakelingfe885462016-09-22 10:24:38 +0100342
343 ArmVIXLAssembler* GetAssembler() const { return assembler_; }
xueliang.zhongf51bc622016-11-04 09:23:32 +0000344 ArmVIXLMacroAssembler* GetVIXLAssembler() { return GetAssembler()->GetVIXLAssembler(); }
Scott Wakelingfe885462016-09-22 10:24:38 +0100345
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +0100346 void GenerateAndConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
347
Scott Wakelingfe885462016-09-22 10:24:38 +0100348 private:
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100349 // Generate code for the given suspend check. If not null, `successor`
350 // is the block to branch to if the suspend check is not needed, and after
351 // the suspend call.
Scott Wakelingfe885462016-09-22 10:24:38 +0100352 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100353 void GenerateClassInitializationCheck(LoadClassSlowPathARMVIXL* slow_path,
354 vixl32::Register class_reg);
Vladimir Marko175e7862018-03-27 09:03:13 +0000355 void GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
356 vixl::aarch32::Register temp,
357 vixl::aarch32::FlagsUpdate flags_update);
Artem Serov02109dd2016-09-23 17:17:54 +0100358 void GenerateOrrConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
359 void GenerateEorConst(vixl::aarch32::Register out, vixl::aarch32::Register first, uint32_t value);
Anton Kirilovdda43962016-11-21 19:55:20 +0000360 void GenerateAddLongConst(Location out, Location first, uint64_t value);
Artem Serov02109dd2016-09-23 17:17:54 +0100361 void HandleBitwiseOperation(HBinaryOperation* operation);
Scott Wakelingfe885462016-09-22 10:24:38 +0100362 void HandleCondition(HCondition* condition);
Artem Serov02109dd2016-09-23 17:17:54 +0100363 void HandleIntegerRotate(HRor* ror);
364 void HandleLongRotate(HRor* ror);
365 void HandleShift(HBinaryOperation* operation);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100366
367 void GenerateWideAtomicStore(vixl::aarch32::Register addr,
368 uint32_t offset,
369 vixl::aarch32::Register value_lo,
370 vixl::aarch32::Register value_hi,
371 vixl::aarch32::Register temp1,
372 vixl::aarch32::Register temp2,
373 HInstruction* instruction);
374 void GenerateWideAtomicLoad(vixl::aarch32::Register addr,
375 uint32_t offset,
376 vixl::aarch32::Register out_lo,
377 vixl::aarch32::Register out_hi);
378
379 void HandleFieldSet(HInstruction* instruction,
380 const FieldInfo& field_info,
381 bool value_can_be_null);
382 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
383
Aart Bik351df3e2018-03-07 11:54:57 -0800384 void GenerateMinMaxInt(LocationSummary* locations, bool is_min);
Aart Bik1f8d51b2018-02-15 10:42:37 -0800385 void GenerateMinMaxLong(LocationSummary* locations, bool is_min);
Aart Bik351df3e2018-03-07 11:54:57 -0800386 void GenerateMinMaxFloat(HInstruction* minmax, bool is_min);
387 void GenerateMinMaxDouble(HInstruction* minmax, bool is_min);
388 void GenerateMinMax(HBinaryOperation* minmax, bool is_min);
Aart Bik1f8d51b2018-02-15 10:42:37 -0800389
Artem Serovcfbe9132016-10-14 15:58:56 +0100390 // Generate a heap reference load using one register `out`:
391 //
392 // out <- *(out + offset)
393 //
394 // while honoring heap poisoning and/or read barriers (if any).
395 //
396 // Location `maybe_temp` is used when generating a read barrier and
397 // shall be a register in that case; it may be an invalid location
398 // otherwise.
399 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
400 Location out,
401 uint32_t offset,
Artem Serov657022c2016-11-23 14:19:38 +0000402 Location maybe_temp,
403 ReadBarrierOption read_barrier_option);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100404 // Generate a heap reference load using two different registers
405 // `out` and `obj`:
406 //
407 // out <- *(obj + offset)
408 //
409 // while honoring heap poisoning and/or read barriers (if any).
410 //
411 // Location `maybe_temp` is used when generating a Baker's (fast
412 // path) read barrier and shall be a register in that case; it may
413 // be an invalid location otherwise.
414 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
415 Location out,
416 Location obj,
417 uint32_t offset,
Artem Serov657022c2016-11-23 14:19:38 +0000418 Location maybe_temp,
419 ReadBarrierOption read_barrier_option);
Scott Wakelingfe885462016-09-22 10:24:38 +0100420 void GenerateTestAndBranch(HInstruction* instruction,
421 size_t condition_input_index,
422 vixl::aarch32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +0000423 vixl::aarch32::Label* false_target,
424 bool far_target = true);
Scott Wakelingfe885462016-09-22 10:24:38 +0100425 void GenerateCompareTestAndBranch(HCondition* condition,
426 vixl::aarch32::Label* true_target,
Anton Kirilovfd522532017-05-10 12:46:57 +0100427 vixl::aarch32::Label* false_target,
428 bool is_far_target = true);
Scott Wakelingfe885462016-09-22 10:24:38 +0100429 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
430 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
431 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
432 void GenerateDivRemConstantIntegral(HBinaryOperation* instruction);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000433 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Mythri Alle5097f832021-11-02 14:52:30 +0000434 void GenerateMethodEntryExitHook(HInstruction* instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +0100435
Artem Serov8f7c4102017-06-21 11:21:37 +0100436 vixl::aarch32::MemOperand VecAddress(
437 HVecMemoryOperation* instruction,
438 // This function may acquire a scratch register.
439 vixl::aarch32::UseScratchRegisterScope* temps_scope,
440 /*out*/ vixl32::Register* scratch);
441 vixl::aarch32::AlignedMemOperand VecAddressUnaligned(
442 HVecMemoryOperation* instruction,
443 // This function may acquire a scratch register.
444 vixl::aarch32::UseScratchRegisterScope* temps_scope,
445 /*out*/ vixl32::Register* scratch);
446
Scott Wakelingfe885462016-09-22 10:24:38 +0100447 ArmVIXLAssembler* const assembler_;
448 CodeGeneratorARMVIXL* const codegen_;
449
450 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARMVIXL);
451};
452
453class CodeGeneratorARMVIXL : public CodeGenerator {
454 public:
455 CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +0100456 const CompilerOptions& compiler_options,
457 OptimizingCompilerStats* stats = nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +0100458 virtual ~CodeGeneratorARMVIXL() {}
459
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100460 void GenerateFrameEntry() override;
461 void GenerateFrameExit() override;
462 void Bind(HBasicBlock* block) override;
463 void MoveConstant(Location destination, int32_t value) override;
464 void MoveLocation(Location dst, Location src, DataType::Type dst_type) override;
465 void AddLocationAsTemp(Location location, LocationSummary* locations) override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100467 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) override;
468 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) override;
469 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) override;
470 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100472 size_t GetWordSize() const override {
Artem Serovd4cc5b22016-11-04 11:19:09 +0000473 return static_cast<size_t>(kArmPointerSize);
474 }
475
Artem Serov6a0b6572019-07-26 20:38:37 +0100476 size_t GetCalleePreservedFPWidth() const override {
477 return vixl::aarch32::kSRegSizeInBytes;
478 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000479
Artem Serovc8150b52019-07-31 18:28:00 +0100480 size_t GetSIMDRegisterWidth() const override {
481 // ARM 32-bit backend doesn't support Q registers in vectorizer, only D
482 // registers (due to register allocator restrictions: overlapping s/d/q
483 // registers).
484 return vixl::aarch32::kDRegSizeInBytes;
485 }
486
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100487 HGraphVisitor* GetLocationBuilder() override { return &location_builder_; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000488
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100489 HGraphVisitor* GetInstructionVisitor() override { return &instruction_visitor_; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100491 ArmVIXLAssembler* GetAssembler() override { return &assembler_; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100492
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100493 const ArmVIXLAssembler& GetAssembler() const override { return assembler_; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100494
xueliang.zhongf51bc622016-11-04 09:23:32 +0000495 ArmVIXLMacroAssembler* GetVIXLAssembler() { return GetAssembler()->GetVIXLAssembler(); }
Scott Wakelingfe885462016-09-22 10:24:38 +0100496
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100497 uintptr_t GetAddressOf(HBasicBlock* block) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100498 vixl::aarch32::Label* block_entry_label = GetLabelOf(block);
499 DCHECK(block_entry_label->IsBound());
500 return block_entry_label->GetLocation();
501 }
502
Artem Serov09a940d2016-11-11 16:15:11 +0000503 void FixJumpTables();
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100504 void SetupBlockedRegisters() const override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100506 void DumpCoreRegister(std::ostream& stream, int reg) const override;
507 void DumpFloatingPointRegister(std::ostream& stream, int reg) const override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100509 ParallelMoveResolver* GetMoveResolver() override { return &move_resolver_; }
510 InstructionSet GetInstructionSet() const override { return InstructionSet::kThumb2; }
Vladimir Markoa0431112018-06-25 09:32:54 +0100511
512 const ArmInstructionSetFeatures& GetInstructionSetFeatures() const;
513
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100514 // Helper method to move a 32-bit value between two locations.
515 void Move32(Location destination, Location source);
516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100517 void LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100518 Location out_loc,
519 vixl::aarch32::Register base,
520 vixl::aarch32::Register reg_index,
521 vixl::aarch32::Condition cond = vixl::aarch32::al);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100522 void StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100523 Location out_loc,
524 vixl::aarch32::Register base,
525 vixl::aarch32::Register reg_index,
526 vixl::aarch32::Condition cond = vixl::aarch32::al);
527
Scott Wakelingfe885462016-09-22 10:24:38 +0100528 // Generate code to invoke a runtime entry point.
529 void InvokeRuntime(QuickEntrypointEnum entrypoint,
530 HInstruction* instruction,
531 uint32_t dex_pc,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100532 SlowPathCode* slow_path = nullptr) override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100533
534 // Generate code to invoke a runtime entry point, but do not record
535 // PC-related information in a stack map.
536 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
537 HInstruction* instruction,
538 SlowPathCode* slow_path);
539
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100540 // Emit a write barrier.
541 void MarkGCCard(vixl::aarch32::Register temp,
542 vixl::aarch32::Register card,
543 vixl::aarch32::Register object,
544 vixl::aarch32::Register value,
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000545 bool value_can_be_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100546
Artem Serovd4cc5b22016-11-04 11:19:09 +0000547 void GenerateMemoryBarrier(MemBarrierKind kind);
548
549 vixl::aarch32::Label* GetLabelOf(HBasicBlock* block) {
550 block = FirstNonEmptyBlock(block);
551 return &(block_labels_[block->GetBlockId()]);
552 }
553
Donghui Bai426b49c2016-11-08 14:55:38 +0800554 vixl32::Label* GetFinalLabel(HInstruction* instruction, vixl32::Label* final_label);
555
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100556 void Initialize() override {
Artem Serovd4cc5b22016-11-04 11:19:09 +0000557 block_labels_.resize(GetGraph()->GetBlocks().size());
558 }
559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100560 void Finalize(CodeAllocator* allocator) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000561
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100562 bool NeedsTwoRegisters(DataType::Type type) const override {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100563 return type == DataType::Type::kFloat64 || type == DataType::Type::kInt64;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000564 }
565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100566 void ComputeSpillMask() override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000567
568 vixl::aarch32::Label* GetFrameEntryLabel() { return &frame_entry_label_; }
569
570 // Check if the desired_string_load_kind is supported. If it is, return it,
571 // otherwise return a fall-back kind that should be used instead.
572 HLoadString::LoadKind GetSupportedLoadStringKind(
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100573 HLoadString::LoadKind desired_string_load_kind) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000574
575 // Check if the desired_class_load_kind is supported. If it is, return it,
576 // otherwise return a fall-back kind that should be used instead.
577 HLoadClass::LoadKind GetSupportedLoadClassKind(
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100578 HLoadClass::LoadKind desired_class_load_kind) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000579
580 // Check if the desired_dispatch_info is supported. If it is, return it,
581 // otherwise return a fall-back info that should be used instead.
582 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
583 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100584 ArtMethod* method) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000585
Nicolas Geoffray8d34a182020-09-16 09:46:58 +0100586 void LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke);
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100587 void GenerateStaticOrDirectCall(
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100588 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path = nullptr) override;
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100589 void GenerateVirtualCall(
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100590 HInvokeVirtual* invoke, Location temp, SlowPathCode* slow_path = nullptr) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000591
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100592 void MoveFromReturnRegister(Location trg, DataType::Type type) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000593
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000594 // The PcRelativePatchInfo is used for PC-relative addressing of methods/strings/types,
595 // whether through .data.bimg.rel.ro, .bss, or directly in the boot image.
596 //
597 // The PC-relative address is loaded with three instructions,
Artem Serovd4cc5b22016-11-04 11:19:09 +0000598 // MOVW+MOVT to load the offset to base_reg and then ADD base_reg, PC. The offset
599 // is calculated from the ADD's effective PC, i.e. PC+4 on Thumb2. Though we
600 // currently emit these 3 instructions together, instruction scheduling could
601 // split this sequence apart, so we keep separate labels for each of them.
602 struct PcRelativePatchInfo {
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000603 PcRelativePatchInfo(const DexFile* dex_file, uint32_t off_or_idx)
Artem Serovd4cc5b22016-11-04 11:19:09 +0000604 : target_dex_file(dex_file), offset_or_index(off_or_idx) { }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000605
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000606 // Target dex file or null for .data.bmig.rel.ro patches.
607 const DexFile* target_dex_file;
608 // Either the boot image offset (to write to .data.bmig.rel.ro) or string/type/method index.
Artem Serovd4cc5b22016-11-04 11:19:09 +0000609 uint32_t offset_or_index;
610 vixl::aarch32::Label movw_label;
611 vixl::aarch32::Label movt_label;
612 vixl::aarch32::Label add_pc_label;
613 };
614
Vladimir Marko6fd16062018-06-26 11:02:04 +0100615 PcRelativePatchInfo* NewBootImageIntrinsicPatch(uint32_t intrinsic_data);
Vladimir Markob066d432018-01-03 13:14:37 +0000616 PcRelativePatchInfo* NewBootImageRelRoPatch(uint32_t boot_image_offset);
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000617 PcRelativePatchInfo* NewBootImageMethodPatch(MethodReference target_method);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100618 PcRelativePatchInfo* NewMethodBssEntryPatch(MethodReference target_method);
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000619 PcRelativePatchInfo* NewBootImageTypePatch(const DexFile& dex_file, dex::TypeIndex type_index);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100620 PcRelativePatchInfo* NewTypeBssEntryPatch(HLoadClass* load_class);
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000621 PcRelativePatchInfo* NewBootImageStringPatch(const DexFile& dex_file,
622 dex::StringIndex string_index);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100623 PcRelativePatchInfo* NewStringBssEntryPatch(const DexFile& dex_file,
624 dex::StringIndex string_index);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100625
Vladimir Markof6675082019-05-17 12:05:28 +0100626 // Emit the BL instruction for entrypoint thunk call and record the associated patch for AOT.
627 void EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset);
628
Vladimir Marko966b46f2018-08-03 10:20:19 +0000629 // Emit the BNE instruction for baker read barrier and record
630 // the associated patch for AOT or slow path for JIT.
631 void EmitBakerReadBarrierBne(uint32_t custom_data);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100632
Artem Serovc5fcb442016-12-02 19:19:58 +0000633 VIXLUInt32Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
Artem Serovc5fcb442016-12-02 19:19:58 +0000634 VIXLUInt32Literal* DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000635 dex::StringIndex string_index,
636 Handle<mirror::String> handle);
Artem Serovc5fcb442016-12-02 19:19:58 +0000637 VIXLUInt32Literal* DeduplicateJitClassLiteral(const DexFile& dex_file,
638 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000639 Handle<mirror::Class> handle);
Artem Serovc5fcb442016-12-02 19:19:58 +0000640
Vladimir Marko9d31daa2022-04-14 10:48:44 +0100641 void LoadBootImageRelRoEntry(vixl::aarch32::Register reg, uint32_t boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +0100642 void LoadBootImageAddress(vixl::aarch32::Register reg, uint32_t boot_image_reference);
Vladimir Marko7968cae2021-01-19 12:02:35 +0000643 void LoadTypeForBootImageIntrinsic(vixl::aarch32::Register reg, TypeReference type_reference);
Vladimir Markode91ca92020-10-27 13:41:40 +0000644 void LoadIntrinsicDeclaringClass(vixl::aarch32::Register reg, HInvoke* invoke);
Vladimir Marko7968cae2021-01-19 12:02:35 +0000645 void LoadClassRootForIntrinsic(vixl::aarch32::Register reg, ClassRoot class_root);
Vladimir Markoeebb8212018-06-05 14:57:24 +0100646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100647 void EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) override;
648 bool NeedsThunkCode(const linker::LinkerPatch& patch) const override;
Vladimir Markoca1e0382018-04-11 09:58:41 +0000649 void EmitThunkCode(const linker::LinkerPatch& patch,
650 /*out*/ ArenaVector<uint8_t>* code,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100651 /*out*/ std::string* debug_name) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000652
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100653 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) override;
Artem Serovc5fcb442016-12-02 19:19:58 +0000654
Vladimir Markoca1e0382018-04-11 09:58:41 +0000655 // Generate a GC root reference load:
656 //
657 // root <- *(obj + offset)
658 //
659 // while honoring read barriers based on read_barrier_option.
660 void GenerateGcRootFieldLoad(HInstruction* instruction,
661 Location root,
662 vixl::aarch32::Register obj,
663 uint32_t offset,
664 ReadBarrierOption read_barrier_option);
Vladimir Marko3d350a82020-11-18 14:14:27 +0000665 // Generate MOV for an intrinsic CAS to mark the old value with Baker read barrier.
666 void GenerateIntrinsicCasMoveWithBakerReadBarrier(vixl::aarch32::Register marked_old_value,
667 vixl::aarch32::Register old_value);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100668 // Fast path implementation of ReadBarrier::Barrier for a heap
669 // reference field load when Baker's read barriers are used.
Vladimir Marko248141f2018-08-10 10:40:07 +0100670 // Overload suitable for Unsafe.getObject/-Volatile() intrinsic.
671 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
672 Location ref,
673 vixl::aarch32::Register obj,
674 const vixl::aarch32::MemOperand& src,
675 bool needs_null_check);
676 // Fast path implementation of ReadBarrier::Barrier for a heap
677 // reference field load when Baker's read barriers are used.
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100678 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
679 Location ref,
680 vixl::aarch32::Register obj,
681 uint32_t offset,
Vladimir Marko01b65522020-10-28 15:43:54 +0000682 Location maybe_temp,
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100683 bool needs_null_check);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000684 // Fast path implementation of ReadBarrier::Barrier for a heap
685 // reference array load when Baker's read barriers are used.
Vladimir Marko008e09f32018-08-06 15:42:43 +0100686 void GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000687 vixl::aarch32::Register obj,
688 uint32_t data_offset,
689 Location index,
690 Location temp,
691 bool needs_null_check);
Roland Levillainff487002017-03-07 16:50:01 +0000692
Roland Levillain5daa4952017-07-03 17:23:56 +0100693 // Emit code checking the status of the Marking Register, and
694 // aborting the program if MR does not match the value stored in the
695 // art::Thread object. Code is only emitted in debug mode and if
696 // CompilerOptions::EmitRunTimeChecksInDebugMode returns true.
697 //
698 // Argument `code` is used to identify the different occurrences of
699 // MaybeGenerateMarkingRegisterCheck in the code generator, and is
700 // used together with kMarkingRegisterCheckBreakCodeBaseCode to
701 // create the value passed to the BKPT instruction. Note that unlike
702 // in the ARM64 code generator, where `__LINE__` is passed as `code`
703 // argument to
704 // CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck, we cannot
705 // realistically do that here, as Encoding T1 for the BKPT
706 // instruction only accepts 8-bit immediate values.
707 //
708 // If `temp_loc` is a valid location, it is expected to be a
709 // register and will be used as a temporary to generate code;
710 // otherwise, a temporary will be fetched from the core register
711 // scratch pool.
712 virtual void MaybeGenerateMarkingRegisterCheck(int code,
713 Location temp_loc = Location::NoLocation());
714
Vladimir Marko3d350a82020-11-18 14:14:27 +0000715 // Create slow path for a read barrier for a heap reference within `instruction`.
716 //
717 // This is a helper function for GenerateReadBarrierSlow() that has the same
718 // arguments. The creation and adding of the slow path is exposed for intrinsics
719 // that cannot use GenerateReadBarrierSlow() from their own slow paths.
720 SlowPathCodeARMVIXL* AddReadBarrierSlowPath(HInstruction* instruction,
721 Location out,
722 Location ref,
723 Location obj,
724 uint32_t offset,
725 Location index);
726
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100727 // Generate a read barrier for a heap reference within `instruction`
728 // using a slow path.
729 //
730 // A read barrier for an object reference read from the heap is
731 // implemented as a call to the artReadBarrierSlow runtime entry
732 // point, which is passed the values in locations `ref`, `obj`, and
733 // `offset`:
734 //
735 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
736 // mirror::Object* obj,
737 // uint32_t offset);
738 //
739 // The `out` location contains the value returned by
740 // artReadBarrierSlow.
741 //
742 // When `index` is provided (i.e. for array accesses), the offset
743 // value passed to artReadBarrierSlow is adjusted to take `index`
744 // into account.
745 void GenerateReadBarrierSlow(HInstruction* instruction,
746 Location out,
747 Location ref,
748 Location obj,
749 uint32_t offset,
750 Location index = Location::NoLocation());
751
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100752 // If read barriers are enabled, generate a read barrier for a heap
753 // reference using a slow path. If heap poisoning is enabled, also
754 // unpoison the reference in `out`.
755 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
756 Location out,
757 Location ref,
758 Location obj,
759 uint32_t offset,
760 Location index = Location::NoLocation());
761
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000762 // Generate a read barrier for a GC root within `instruction` using
763 // a slow path.
764 //
765 // A read barrier for an object reference GC root is implemented as
766 // a call to the artReadBarrierForRootSlow runtime entry point,
767 // which is passed the value in location `root`:
768 //
769 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
770 //
771 // The `out` location contains the value returned by
772 // artReadBarrierForRootSlow.
773 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
774
Vladimir Markodec78172020-06-19 15:31:23 +0100775 void IncreaseFrame(size_t adjustment) override;
776 void DecreaseFrame(size_t adjustment) override;
777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100778 void GenerateNop() override;
Scott Wakelingfe885462016-09-22 10:24:38 +0100779
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100780 void GenerateImplicitNullCheck(HNullCheck* instruction) override;
781 void GenerateExplicitNullCheck(HNullCheck* instruction) override;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000782
783 JumpTableARMVIXL* CreateJumpTable(HPackedSwitch* switch_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100784 jump_tables_.emplace_back(new (GetGraph()->GetAllocator()) JumpTableARMVIXL(switch_instr));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000785 return jump_tables_.back().get();
786 }
787 void EmitJumpTables();
788
789 void EmitMovwMovtPlaceholder(CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
790 vixl::aarch32::Register out);
791
Anton Kirilov5601d4e2017-05-11 19:33:50 +0100792 // `temp` is an extra temporary register that is used for some conditions;
793 // callers may not specify it, in which case the method will use a scratch
794 // register instead.
795 void GenerateConditionWithZero(IfCondition condition,
796 vixl::aarch32::Register out,
797 vixl::aarch32::Register in,
798 vixl::aarch32::Register temp = vixl32::Register());
799
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +0100800 void MaybeRecordImplicitNullCheck(HInstruction* instr) final {
801 // The function must be only be called within special scopes
802 // (EmissionCheckScope, ExactAssemblyScope) which prevent generation of
803 // veneer/literal pools by VIXL assembler.
804 CHECK_EQ(GetVIXLAssembler()->ArePoolsBlocked(), true)
805 << "The function must only be called within EmissionCheckScope or ExactAssemblyScope";
806 CodeGenerator::MaybeRecordImplicitNullCheck(instr);
807 }
808
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +0000809 void MaybeGenerateInlineCacheCheck(HInstruction* instruction, vixl32::Register klass);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000810 void MaybeIncrementHotness(bool is_frame_entry);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +0000811
Scott Wakelingfe885462016-09-22 10:24:38 +0100812 private:
Vladimir Markoca1e0382018-04-11 09:58:41 +0000813 // Encoding of thunk type and data for link-time generated thunks for Baker read barriers.
814
815 enum class BakerReadBarrierKind : uint8_t {
Vladimir Marko3d350a82020-11-18 14:14:27 +0000816 kField, // Field get or array get with constant offset (i.e. constant index).
817 kArray, // Array get with index in register.
818 kGcRoot, // GC root load.
819 kIntrinsicCas, // Unsafe/VarHandle CAS intrinsic.
820 kLast = kIntrinsicCas
Vladimir Markoca1e0382018-04-11 09:58:41 +0000821 };
822
823 enum class BakerReadBarrierWidth : uint8_t {
824 kWide, // 32-bit LDR (and 32-bit NEG if heap poisoning is enabled).
825 kNarrow, // 16-bit LDR (and 16-bit NEG if heap poisoning is enabled).
826 kLast = kNarrow
827 };
828
829 static constexpr uint32_t kBakerReadBarrierInvalidEncodedReg = /* pc is invalid */ 15u;
830
831 static constexpr size_t kBitsForBakerReadBarrierKind =
832 MinimumBitsToStore(static_cast<size_t>(BakerReadBarrierKind::kLast));
833 static constexpr size_t kBakerReadBarrierBitsForRegister =
834 MinimumBitsToStore(kBakerReadBarrierInvalidEncodedReg);
835 using BakerReadBarrierKindField =
836 BitField<BakerReadBarrierKind, 0, kBitsForBakerReadBarrierKind>;
837 using BakerReadBarrierFirstRegField =
838 BitField<uint32_t, kBitsForBakerReadBarrierKind, kBakerReadBarrierBitsForRegister>;
839 using BakerReadBarrierSecondRegField =
840 BitField<uint32_t,
841 kBitsForBakerReadBarrierKind + kBakerReadBarrierBitsForRegister,
842 kBakerReadBarrierBitsForRegister>;
843 static constexpr size_t kBitsForBakerReadBarrierWidth =
844 MinimumBitsToStore(static_cast<size_t>(BakerReadBarrierWidth::kLast));
845 using BakerReadBarrierWidthField =
846 BitField<BakerReadBarrierWidth,
847 kBitsForBakerReadBarrierKind + 2 * kBakerReadBarrierBitsForRegister,
848 kBitsForBakerReadBarrierWidth>;
849
850 static void CheckValidReg(uint32_t reg) {
Vladimir Markodcd117e2018-04-19 11:54:00 +0100851 DCHECK(reg < vixl::aarch32::ip.GetCode() && reg != mr.GetCode()) << reg;
Vladimir Markoca1e0382018-04-11 09:58:41 +0000852 }
853
854 static uint32_t EncodeBakerReadBarrierFieldData(uint32_t base_reg,
855 uint32_t holder_reg,
856 bool narrow) {
857 CheckValidReg(base_reg);
858 CheckValidReg(holder_reg);
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +0000859 DCHECK_IMPLIES(narrow, base_reg < 8u) << base_reg;
Vladimir Markoca1e0382018-04-11 09:58:41 +0000860 BakerReadBarrierWidth width =
861 narrow ? BakerReadBarrierWidth::kNarrow : BakerReadBarrierWidth::kWide;
862 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kField) |
863 BakerReadBarrierFirstRegField::Encode(base_reg) |
864 BakerReadBarrierSecondRegField::Encode(holder_reg) |
865 BakerReadBarrierWidthField::Encode(width);
866 }
867
868 static uint32_t EncodeBakerReadBarrierArrayData(uint32_t base_reg) {
869 CheckValidReg(base_reg);
870 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kArray) |
871 BakerReadBarrierFirstRegField::Encode(base_reg) |
872 BakerReadBarrierSecondRegField::Encode(kBakerReadBarrierInvalidEncodedReg) |
873 BakerReadBarrierWidthField::Encode(BakerReadBarrierWidth::kWide);
874 }
875
876 static uint32_t EncodeBakerReadBarrierGcRootData(uint32_t root_reg, bool narrow) {
877 CheckValidReg(root_reg);
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +0000878 DCHECK_IMPLIES(narrow, root_reg < 8u) << root_reg;
Vladimir Markoca1e0382018-04-11 09:58:41 +0000879 BakerReadBarrierWidth width =
880 narrow ? BakerReadBarrierWidth::kNarrow : BakerReadBarrierWidth::kWide;
881 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kGcRoot) |
882 BakerReadBarrierFirstRegField::Encode(root_reg) |
883 BakerReadBarrierSecondRegField::Encode(kBakerReadBarrierInvalidEncodedReg) |
884 BakerReadBarrierWidthField::Encode(width);
885 }
886
Vladimir Marko3d350a82020-11-18 14:14:27 +0000887 static uint32_t EncodeBakerReadBarrierIntrinsicCasData(uint32_t root_reg) {
Vladimir Markod887ed82018-08-14 13:52:12 +0000888 CheckValidReg(root_reg);
Vladimir Marko3d350a82020-11-18 14:14:27 +0000889 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kIntrinsicCas) |
Vladimir Markod887ed82018-08-14 13:52:12 +0000890 BakerReadBarrierFirstRegField::Encode(root_reg) |
891 BakerReadBarrierSecondRegField::Encode(kBakerReadBarrierInvalidEncodedReg) |
892 BakerReadBarrierWidthField::Encode(BakerReadBarrierWidth::kWide);
893 }
894
Vladimir Markoca1e0382018-04-11 09:58:41 +0000895 void CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
896 uint32_t encoded_data,
897 /*out*/ std::string* debug_name);
898
Artem Serovc5fcb442016-12-02 19:19:58 +0000899 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, VIXLUInt32Literal*>;
Artem Serovc5fcb442016-12-02 19:19:58 +0000900 using StringToLiteralMap = ArenaSafeMap<StringReference,
901 VIXLUInt32Literal*,
902 StringReferenceValueComparator>;
903 using TypeToLiteralMap = ArenaSafeMap<TypeReference,
904 VIXLUInt32Literal*,
905 TypeReferenceValueComparator>;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000906
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100907 struct BakerReadBarrierPatchInfo {
908 explicit BakerReadBarrierPatchInfo(uint32_t data) : label(), custom_data(data) { }
909
910 vixl::aarch32::Label label;
911 uint32_t custom_data;
912 };
913
Artem Serovc5fcb442016-12-02 19:19:58 +0000914 VIXLUInt32Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000915 PcRelativePatchInfo* NewPcRelativePatch(const DexFile* dex_file,
Artem Serovd4cc5b22016-11-04 11:19:09 +0000916 uint32_t offset_or_index,
917 ArenaDeque<PcRelativePatchInfo>* patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100918 template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +0000919 static void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100920 ArenaVector<linker::LinkerPatch>* linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000921
Scott Wakelingfe885462016-09-22 10:24:38 +0100922 // Labels for each block that will be compiled.
923 // We use a deque so that the `vixl::aarch32::Label` objects do not move in memory.
924 ArenaDeque<vixl::aarch32::Label> block_labels_; // Indexed by block id.
925 vixl::aarch32::Label frame_entry_label_;
926
Artem Serov551b28f2016-10-18 19:11:30 +0100927 ArenaVector<std::unique_ptr<JumpTableARMVIXL>> jump_tables_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100928 LocationsBuilderARMVIXL location_builder_;
929 InstructionCodeGeneratorARMVIXL instruction_visitor_;
930 ParallelMoveResolverARMVIXL move_resolver_;
931
932 ArmVIXLAssembler assembler_;
Scott Wakelingfe885462016-09-22 10:24:38 +0100933
Vladimir Marko2d06e022019-07-08 15:45:19 +0100934 // PC-relative method patch info for kBootImageLinkTimePcRelative.
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000935 ArenaDeque<PcRelativePatchInfo> boot_image_method_patches_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100936 // PC-relative method patch info for kBssEntry.
937 ArenaDeque<PcRelativePatchInfo> method_bss_entry_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000938 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000939 ArenaDeque<PcRelativePatchInfo> boot_image_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000940 // PC-relative type patch info for kBssEntry.
941 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Vladimir Marko8f63f102020-09-28 12:10:28 +0100942 // PC-relative public type patch info for kBssEntryPublic.
943 ArenaDeque<PcRelativePatchInfo> public_type_bss_entry_patches_;
944 // PC-relative package type patch info for kBssEntryPackage.
945 ArenaDeque<PcRelativePatchInfo> package_type_bss_entry_patches_;
Vladimir Markoe47f60c2018-02-21 13:43:28 +0000946 // PC-relative String patch info for kBootImageLinkTimePcRelative.
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000947 ArenaDeque<PcRelativePatchInfo> boot_image_string_patches_;
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100948 // PC-relative String patch info for kBssEntry.
949 ArenaDeque<PcRelativePatchInfo> string_bss_entry_patches_;
Vladimir Marko2d06e022019-07-08 15:45:19 +0100950 // PC-relative patch info for IntrinsicObjects for the boot image,
951 // and for method/type/string patches for kBootImageRelRo otherwise.
952 ArenaDeque<PcRelativePatchInfo> boot_image_other_patches_;
Vladimir Markof6675082019-05-17 12:05:28 +0100953 // Patch info for calls to entrypoint dispatch thunks. Used for slow paths.
954 ArenaDeque<PatchInfo<vixl::aarch32::Label>> call_entrypoint_patches_;
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100955 // Baker read barrier patch info.
956 ArenaDeque<BakerReadBarrierPatchInfo> baker_read_barrier_patches_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000957
Vladimir Markof6675082019-05-17 12:05:28 +0100958 // Deduplication map for 32-bit literals, used for JIT for boot image addresses.
959 Uint32ToLiteralMap uint32_literals_;
Artem Serovc5fcb442016-12-02 19:19:58 +0000960 // Patches for string literals in JIT compiled code.
961 StringToLiteralMap jit_string_patches_;
962 // Patches for class literals in JIT compiled code.
963 TypeToLiteralMap jit_class_patches_;
Artem Serovd4cc5b22016-11-04 11:19:09 +0000964
Vladimir Marko966b46f2018-08-03 10:20:19 +0000965 // Baker read barrier slow paths, mapping custom data (uint32_t) to label.
966 // Wrap the label to work around vixl::aarch32::Label being non-copyable
967 // and non-moveable and as such unusable in ArenaSafeMap<>.
968 struct LabelWrapper {
969 LabelWrapper(const LabelWrapper& src)
970 : label() {
971 DCHECK(!src.label.IsReferenced() && !src.label.IsBound());
972 }
973 LabelWrapper() = default;
974 vixl::aarch32::Label label;
975 };
976 ArenaSafeMap<uint32_t, LabelWrapper> jit_baker_read_barrier_slow_paths_;
977
Vladimir Markoca1e0382018-04-11 09:58:41 +0000978 friend class linker::Thumb2RelativePatcherTest;
Scott Wakelingfe885462016-09-22 10:24:38 +0100979 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARMVIXL);
980};
981
Scott Wakelingfe885462016-09-22 10:24:38 +0100982} // namespace arm
983} // namespace art
984
985#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM_VIXL_H_