LLVM 18.0.0git
AArch64ExpandPseudoInsts.cpp
Go to the documentation of this file.
1//===- AArch64ExpandPseudoInsts.cpp - Expand pseudo instructions ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a pass that expands pseudo instructions into target
10// instructions to allow proper scheduling and other late optimizations. This
11// pass should be run after register allocation but before the post-regalloc
12// scheduling pass.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AArch64ExpandImm.h"
17#include "AArch64InstrInfo.h"
19#include "AArch64Subtarget.h"
22#include "llvm/ADT/DenseMap.h"
32#include "llvm/IR/DebugLoc.h"
33#include "llvm/MC/MCInstrDesc.h"
34#include "llvm/Pass.h"
39#include <cassert>
40#include <cstdint>
41#include <iterator>
42#include <limits>
43#include <utility>
44
45using namespace llvm;
46
47#define AARCH64_EXPAND_PSEUDO_NAME "AArch64 pseudo instruction expansion pass"
48
49namespace {
50
51class AArch64ExpandPseudo : public MachineFunctionPass {
52public:
53 const AArch64InstrInfo *TII;
54
55 static char ID;
56
57 AArch64ExpandPseudo() : MachineFunctionPass(ID) {
59 }
60
61 bool runOnMachineFunction(MachineFunction &Fn) override;
62
63 StringRef getPassName() const override { return AARCH64_EXPAND_PSEUDO_NAME; }
64
65private:
66 bool expandMBB(MachineBasicBlock &MBB);
69 bool expandMultiVecPseudo(MachineBasicBlock &MBB,
71 TargetRegisterClass ContiguousClass,
72 TargetRegisterClass StridedClass,
73 unsigned ContiguousOpc, unsigned StridedOpc);
75 unsigned BitSize);
76
77 bool expand_DestructiveOp(MachineInstr &MI, MachineBasicBlock &MBB,
80 unsigned LdarOp, unsigned StlrOp, unsigned CmpOp,
81 unsigned ExtendImm, unsigned ZeroReg,
83 bool expandCMP_SWAP_128(MachineBasicBlock &MBB,
86 bool expandSetTagLoop(MachineBasicBlock &MBB,
89 bool expandSVESpillFill(MachineBasicBlock &MBB,
91 unsigned N);
92 bool expandCALL_RVMARKER(MachineBasicBlock &MBB,
95 bool expandStoreSwiftAsyncContext(MachineBasicBlock &MBB,
97 MachineBasicBlock *expandRestoreZA(MachineBasicBlock &MBB,
99 MachineBasicBlock *expandCondSMToggle(MachineBasicBlock &MBB,
101};
102
103} // end anonymous namespace
104
105char AArch64ExpandPseudo::ID = 0;
106
107INITIALIZE_PASS(AArch64ExpandPseudo, "aarch64-expand-pseudo",
108 AARCH64_EXPAND_PSEUDO_NAME, false, false)
109
110/// Transfer implicit operands on the pseudo instruction to the
111/// instructions created from the expansion.
112static void transferImpOps(MachineInstr &OldMI, MachineInstrBuilder &UseMI,
114 const MCInstrDesc &Desc = OldMI.getDesc();
115 for (const MachineOperand &MO :
116 llvm::drop_begin(OldMI.operands(), Desc.getNumOperands())) {
117 assert(MO.isReg() && MO.getReg());
118 if (MO.isUse())
119 UseMI.add(MO);
120 else
121 DefMI.add(MO);
122 }
123}
124
125/// Expand a MOVi32imm or MOVi64imm pseudo instruction to one or more
126/// real move-immediate instructions to synthesize the immediate.
127bool AArch64ExpandPseudo::expandMOVImm(MachineBasicBlock &MBB,
129 unsigned BitSize) {
130 MachineInstr &MI = *MBBI;
131 Register DstReg = MI.getOperand(0).getReg();
132 uint64_t RenamableState =
133 MI.getOperand(0).isRenamable() ? RegState::Renamable : 0;
134 uint64_t Imm = MI.getOperand(1).getImm();
135
136 if (DstReg == AArch64::XZR || DstReg == AArch64::WZR) {
137 // Useless def, and we don't want to risk creating an invalid ORR (which
138 // would really write to sp).
139 MI.eraseFromParent();
140 return true;
141 }
142
144 AArch64_IMM::expandMOVImm(Imm, BitSize, Insn);
145 assert(Insn.size() != 0);
146
148 for (auto I = Insn.begin(), E = Insn.end(); I != E; ++I) {
149 bool LastItem = std::next(I) == E;
150 switch (I->Opcode)
151 {
152 default: llvm_unreachable("unhandled!"); break;
153
154 case AArch64::ORRWri:
155 case AArch64::ORRXri:
156 if (I->Op1 == 0) {
157 MIBS.push_back(BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
158 .add(MI.getOperand(0))
159 .addReg(BitSize == 32 ? AArch64::WZR : AArch64::XZR)
160 .addImm(I->Op2));
161 } else {
162 Register DstReg = MI.getOperand(0).getReg();
163 bool DstIsDead = MI.getOperand(0).isDead();
164 MIBS.push_back(
165 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
166 .addReg(DstReg, RegState::Define |
167 getDeadRegState(DstIsDead && LastItem) |
168 RenamableState)
169 .addReg(DstReg)
170 .addImm(I->Op2));
171 }
172 break;
173 case AArch64::ANDXri:
174 if (I->Op1 == 0) {
175 MIBS.push_back(BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
176 .add(MI.getOperand(0))
177 .addReg(BitSize == 32 ? AArch64::WZR : AArch64::XZR)
178 .addImm(I->Op2));
179 } else {
180 Register DstReg = MI.getOperand(0).getReg();
181 bool DstIsDead = MI.getOperand(0).isDead();
182 MIBS.push_back(
183 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
184 .addReg(DstReg, RegState::Define |
185 getDeadRegState(DstIsDead && LastItem) |
186 RenamableState)
187 .addReg(DstReg)
188 .addImm(I->Op2));
189 }
190 break;
191 case AArch64::MOVNWi:
192 case AArch64::MOVNXi:
193 case AArch64::MOVZWi:
194 case AArch64::MOVZXi: {
195 bool DstIsDead = MI.getOperand(0).isDead();
196 MIBS.push_back(BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
197 .addReg(DstReg, RegState::Define |
198 getDeadRegState(DstIsDead && LastItem) |
199 RenamableState)
200 .addImm(I->Op1)
201 .addImm(I->Op2));
202 } break;
203 case AArch64::MOVKWi:
204 case AArch64::MOVKXi: {
205 Register DstReg = MI.getOperand(0).getReg();
206 bool DstIsDead = MI.getOperand(0).isDead();
207 MIBS.push_back(BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
208 .addReg(DstReg,
210 getDeadRegState(DstIsDead && LastItem) |
211 RenamableState)
212 .addReg(DstReg)
213 .addImm(I->Op1)
214 .addImm(I->Op2));
215 } break;
216 }
217 }
218 transferImpOps(MI, MIBS.front(), MIBS.back());
219 MI.eraseFromParent();
220 return true;
221}
222
223bool AArch64ExpandPseudo::expandCMP_SWAP(
225 unsigned StlrOp, unsigned CmpOp, unsigned ExtendImm, unsigned ZeroReg,
226 MachineBasicBlock::iterator &NextMBBI) {
227 MachineInstr &MI = *MBBI;
228 MIMetadata MIMD(MI);
229 const MachineOperand &Dest = MI.getOperand(0);
230 Register StatusReg = MI.getOperand(1).getReg();
231 bool StatusDead = MI.getOperand(1).isDead();
232 // Duplicating undef operands into 2 instructions does not guarantee the same
233 // value on both; However undef should be replaced by xzr anyway.
234 assert(!MI.getOperand(2).isUndef() && "cannot handle undef");
235 Register AddrReg = MI.getOperand(2).getReg();
236 Register DesiredReg = MI.getOperand(3).getReg();
237 Register NewReg = MI.getOperand(4).getReg();
238
240 auto LoadCmpBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
241 auto StoreBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
242 auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
243
244 MF->insert(++MBB.getIterator(), LoadCmpBB);
245 MF->insert(++LoadCmpBB->getIterator(), StoreBB);
246 MF->insert(++StoreBB->getIterator(), DoneBB);
247
248 // .Lloadcmp:
249 // mov wStatus, 0
250 // ldaxr xDest, [xAddr]
251 // cmp xDest, xDesired
252 // b.ne .Ldone
253 if (!StatusDead)
254 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::MOVZWi), StatusReg)
255 .addImm(0).addImm(0);
256 BuildMI(LoadCmpBB, MIMD, TII->get(LdarOp), Dest.getReg())
257 .addReg(AddrReg);
258 BuildMI(LoadCmpBB, MIMD, TII->get(CmpOp), ZeroReg)
259 .addReg(Dest.getReg(), getKillRegState(Dest.isDead()))
260 .addReg(DesiredReg)
261 .addImm(ExtendImm);
262 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::Bcc))
264 .addMBB(DoneBB)
265 .addReg(AArch64::NZCV, RegState::Implicit | RegState::Kill);
266 LoadCmpBB->addSuccessor(DoneBB);
267 LoadCmpBB->addSuccessor(StoreBB);
268
269 // .Lstore:
270 // stlxr wStatus, xNew, [xAddr]
271 // cbnz wStatus, .Lloadcmp
272 BuildMI(StoreBB, MIMD, TII->get(StlrOp), StatusReg)
273 .addReg(NewReg)
274 .addReg(AddrReg);
275 BuildMI(StoreBB, MIMD, TII->get(AArch64::CBNZW))
276 .addReg(StatusReg, getKillRegState(StatusDead))
277 .addMBB(LoadCmpBB);
278 StoreBB->addSuccessor(LoadCmpBB);
279 StoreBB->addSuccessor(DoneBB);
280
281 DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
282 DoneBB->transferSuccessors(&MBB);
283
284 MBB.addSuccessor(LoadCmpBB);
285
286 NextMBBI = MBB.end();
287 MI.eraseFromParent();
288
289 // Recompute livein lists.
290 LivePhysRegs LiveRegs;
291 computeAndAddLiveIns(LiveRegs, *DoneBB);
292 computeAndAddLiveIns(LiveRegs, *StoreBB);
293 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
294 // Do an extra pass around the loop to get loop carried registers right.
295 StoreBB->clearLiveIns();
296 computeAndAddLiveIns(LiveRegs, *StoreBB);
297 LoadCmpBB->clearLiveIns();
298 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
299
300 return true;
301}
302
303bool AArch64ExpandPseudo::expandCMP_SWAP_128(
305 MachineBasicBlock::iterator &NextMBBI) {
306 MachineInstr &MI = *MBBI;
307 MIMetadata MIMD(MI);
308 MachineOperand &DestLo = MI.getOperand(0);
309 MachineOperand &DestHi = MI.getOperand(1);
310 Register StatusReg = MI.getOperand(2).getReg();
311 bool StatusDead = MI.getOperand(2).isDead();
312 // Duplicating undef operands into 2 instructions does not guarantee the same
313 // value on both; However undef should be replaced by xzr anyway.
314 assert(!MI.getOperand(3).isUndef() && "cannot handle undef");
315 Register AddrReg = MI.getOperand(3).getReg();
316 Register DesiredLoReg = MI.getOperand(4).getReg();
317 Register DesiredHiReg = MI.getOperand(5).getReg();
318 Register NewLoReg = MI.getOperand(6).getReg();
319 Register NewHiReg = MI.getOperand(7).getReg();
320
321 unsigned LdxpOp, StxpOp;
322
323 switch (MI.getOpcode()) {
324 case AArch64::CMP_SWAP_128_MONOTONIC:
325 LdxpOp = AArch64::LDXPX;
326 StxpOp = AArch64::STXPX;
327 break;
328 case AArch64::CMP_SWAP_128_RELEASE:
329 LdxpOp = AArch64::LDXPX;
330 StxpOp = AArch64::STLXPX;
331 break;
332 case AArch64::CMP_SWAP_128_ACQUIRE:
333 LdxpOp = AArch64::LDAXPX;
334 StxpOp = AArch64::STXPX;
335 break;
336 case AArch64::CMP_SWAP_128:
337 LdxpOp = AArch64::LDAXPX;
338 StxpOp = AArch64::STLXPX;
339 break;
340 default:
341 llvm_unreachable("Unexpected opcode");
342 }
343
345 auto LoadCmpBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
346 auto StoreBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
347 auto FailBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
348 auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
349
350 MF->insert(++MBB.getIterator(), LoadCmpBB);
351 MF->insert(++LoadCmpBB->getIterator(), StoreBB);
352 MF->insert(++StoreBB->getIterator(), FailBB);
353 MF->insert(++FailBB->getIterator(), DoneBB);
354
355 // .Lloadcmp:
356 // ldaxp xDestLo, xDestHi, [xAddr]
357 // cmp xDestLo, xDesiredLo
358 // sbcs xDestHi, xDesiredHi
359 // b.ne .Ldone
360 BuildMI(LoadCmpBB, MIMD, TII->get(LdxpOp))
361 .addReg(DestLo.getReg(), RegState::Define)
362 .addReg(DestHi.getReg(), RegState::Define)
363 .addReg(AddrReg);
364 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::SUBSXrs), AArch64::XZR)
365 .addReg(DestLo.getReg(), getKillRegState(DestLo.isDead()))
366 .addReg(DesiredLoReg)
367 .addImm(0);
368 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::CSINCWr), StatusReg)
369 .addUse(AArch64::WZR)
370 .addUse(AArch64::WZR)
372 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::SUBSXrs), AArch64::XZR)
373 .addReg(DestHi.getReg(), getKillRegState(DestHi.isDead()))
374 .addReg(DesiredHiReg)
375 .addImm(0);
376 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::CSINCWr), StatusReg)
377 .addUse(StatusReg, RegState::Kill)
378 .addUse(StatusReg, RegState::Kill)
380 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::CBNZW))
381 .addUse(StatusReg, getKillRegState(StatusDead))
382 .addMBB(FailBB);
383 LoadCmpBB->addSuccessor(FailBB);
384 LoadCmpBB->addSuccessor(StoreBB);
385
386 // .Lstore:
387 // stlxp wStatus, xNewLo, xNewHi, [xAddr]
388 // cbnz wStatus, .Lloadcmp
389 BuildMI(StoreBB, MIMD, TII->get(StxpOp), StatusReg)
390 .addReg(NewLoReg)
391 .addReg(NewHiReg)
392 .addReg(AddrReg);
393 BuildMI(StoreBB, MIMD, TII->get(AArch64::CBNZW))
394 .addReg(StatusReg, getKillRegState(StatusDead))
395 .addMBB(LoadCmpBB);
396 BuildMI(StoreBB, MIMD, TII->get(AArch64::B)).addMBB(DoneBB);
397 StoreBB->addSuccessor(LoadCmpBB);
398 StoreBB->addSuccessor(DoneBB);
399
400 // .Lfail:
401 // stlxp wStatus, xDestLo, xDestHi, [xAddr]
402 // cbnz wStatus, .Lloadcmp
403 BuildMI(FailBB, MIMD, TII->get(StxpOp), StatusReg)
404 .addReg(DestLo.getReg())
405 .addReg(DestHi.getReg())
406 .addReg(AddrReg);
407 BuildMI(FailBB, MIMD, TII->get(AArch64::CBNZW))
408 .addReg(StatusReg, getKillRegState(StatusDead))
409 .addMBB(LoadCmpBB);
410 FailBB->addSuccessor(LoadCmpBB);
411 FailBB->addSuccessor(DoneBB);
412
413 DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
414 DoneBB->transferSuccessors(&MBB);
415
416 MBB.addSuccessor(LoadCmpBB);
417
418 NextMBBI = MBB.end();
419 MI.eraseFromParent();
420
421 // Recompute liveness bottom up.
422 LivePhysRegs LiveRegs;
423 computeAndAddLiveIns(LiveRegs, *DoneBB);
424 computeAndAddLiveIns(LiveRegs, *FailBB);
425 computeAndAddLiveIns(LiveRegs, *StoreBB);
426 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
427
428 // Do an extra pass in the loop to get the loop carried dependencies right.
429 FailBB->clearLiveIns();
430 computeAndAddLiveIns(LiveRegs, *FailBB);
431 StoreBB->clearLiveIns();
432 computeAndAddLiveIns(LiveRegs, *StoreBB);
433 LoadCmpBB->clearLiveIns();
434 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
435
436 return true;
437}
438
439/// \brief Expand Pseudos to Instructions with destructive operands.
440///
441/// This mechanism uses MOVPRFX instructions for zeroing the false lanes
442/// or for fixing relaxed register allocation conditions to comply with
443/// the instructions register constraints. The latter case may be cheaper
444/// than setting the register constraints in the register allocator,
445/// since that will insert regular MOV instructions rather than MOVPRFX.
446///
447/// Example (after register allocation):
448///
449/// FSUB_ZPZZ_ZERO_B Z0, Pg, Z1, Z0
450///
451/// * The Pseudo FSUB_ZPZZ_ZERO_B maps to FSUB_ZPmZ_B.
452/// * We cannot map directly to FSUB_ZPmZ_B because the register
453/// constraints of the instruction are not met.
454/// * Also the _ZERO specifies the false lanes need to be zeroed.
455///
456/// We first try to see if the destructive operand == result operand,
457/// if not, we try to swap the operands, e.g.
458///
459/// FSUB_ZPmZ_B Z0, Pg/m, Z0, Z1
460///
461/// But because FSUB_ZPmZ is not commutative, this is semantically
462/// different, so we need a reverse instruction:
463///
464/// FSUBR_ZPmZ_B Z0, Pg/m, Z0, Z1
465///
466/// Then we implement the zeroing of the false lanes of Z0 by adding
467/// a zeroing MOVPRFX instruction:
468///
469/// MOVPRFX_ZPzZ_B Z0, Pg/z, Z0
470/// FSUBR_ZPmZ_B Z0, Pg/m, Z0, Z1
471///
472/// Note that this can only be done for _ZERO or _UNDEF variants where
473/// we can guarantee the false lanes to be zeroed (by implementing this)
474/// or that they are undef (don't care / not used), otherwise the
475/// swapping of operands is illegal because the operation is not
476/// (or cannot be emulated to be) fully commutative.
477bool AArch64ExpandPseudo::expand_DestructiveOp(
481 unsigned Opcode = AArch64::getSVEPseudoMap(MI.getOpcode());
482 uint64_t DType = TII->get(Opcode).TSFlags & AArch64::DestructiveInstTypeMask;
483 uint64_t FalseLanes = MI.getDesc().TSFlags & AArch64::FalseLanesMask;
484 bool FalseZero = FalseLanes == AArch64::FalseLanesZero;
485 Register DstReg = MI.getOperand(0).getReg();
486 bool DstIsDead = MI.getOperand(0).isDead();
487 bool UseRev = false;
488 unsigned PredIdx, DOPIdx, SrcIdx, Src2Idx;
489
490 switch (DType) {
493 if (DstReg == MI.getOperand(3).getReg()) {
494 // FSUB Zd, Pg, Zs1, Zd ==> FSUBR Zd, Pg/m, Zd, Zs1
495 std::tie(PredIdx, DOPIdx, SrcIdx) = std::make_tuple(1, 3, 2);
496 UseRev = true;
497 break;
498 }
499 [[fallthrough]];
502 std::tie(PredIdx, DOPIdx, SrcIdx) = std::make_tuple(1, 2, 3);
503 break;
505 std::tie(PredIdx, DOPIdx, SrcIdx) = std::make_tuple(2, 3, 3);
506 break;
508 std::tie(PredIdx, DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 2, 3, 4);
509 if (DstReg == MI.getOperand(3).getReg()) {
510 // FMLA Zd, Pg, Za, Zd, Zm ==> FMAD Zdn, Pg, Zm, Za
511 std::tie(PredIdx, DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 3, 4, 2);
512 UseRev = true;
513 } else if (DstReg == MI.getOperand(4).getReg()) {
514 // FMLA Zd, Pg, Za, Zm, Zd ==> FMAD Zdn, Pg, Zm, Za
515 std::tie(PredIdx, DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 4, 3, 2);
516 UseRev = true;
517 }
518 break;
519 default:
520 llvm_unreachable("Unsupported Destructive Operand type");
521 }
522
523 // MOVPRFX can only be used if the destination operand
524 // is the destructive operand, not as any other operand,
525 // so the Destructive Operand must be unique.
526 bool DOPRegIsUnique = false;
527 switch (DType) {
529 DOPRegIsUnique = DstReg != MI.getOperand(SrcIdx).getReg();
530 break;
533 DOPRegIsUnique =
534 DstReg != MI.getOperand(DOPIdx).getReg() ||
535 MI.getOperand(DOPIdx).getReg() != MI.getOperand(SrcIdx).getReg();
536 break;
539 DOPRegIsUnique = true;
540 break;
542 DOPRegIsUnique =
543 DstReg != MI.getOperand(DOPIdx).getReg() ||
544 (MI.getOperand(DOPIdx).getReg() != MI.getOperand(SrcIdx).getReg() &&
545 MI.getOperand(DOPIdx).getReg() != MI.getOperand(Src2Idx).getReg());
546 break;
547 }
548
549 // Resolve the reverse opcode
550 if (UseRev) {
551 int NewOpcode;
552 // e.g. DIV -> DIVR
553 if ((NewOpcode = AArch64::getSVERevInstr(Opcode)) != -1)
554 Opcode = NewOpcode;
555 // e.g. DIVR -> DIV
556 else if ((NewOpcode = AArch64::getSVENonRevInstr(Opcode)) != -1)
557 Opcode = NewOpcode;
558 }
559
560 // Get the right MOVPRFX
561 uint64_t ElementSize = TII->getElementSizeForOpcode(Opcode);
562 unsigned MovPrfx, LSLZero, MovPrfxZero;
563 switch (ElementSize) {
566 MovPrfx = AArch64::MOVPRFX_ZZ;
567 LSLZero = AArch64::LSL_ZPmI_B;
568 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_B;
569 break;
571 MovPrfx = AArch64::MOVPRFX_ZZ;
572 LSLZero = AArch64::LSL_ZPmI_H;
573 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_H;
574 break;
576 MovPrfx = AArch64::MOVPRFX_ZZ;
577 LSLZero = AArch64::LSL_ZPmI_S;
578 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_S;
579 break;
581 MovPrfx = AArch64::MOVPRFX_ZZ;
582 LSLZero = AArch64::LSL_ZPmI_D;
583 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_D;
584 break;
585 default:
586 llvm_unreachable("Unsupported ElementSize");
587 }
588
589 //
590 // Create the destructive operation (if required)
591 //
592 MachineInstrBuilder PRFX, DOP;
593 if (FalseZero) {
594 // If we cannot prefix the requested instruction we'll instead emit a
595 // prefixed_zeroing_mov for DestructiveBinary.
596 assert((DOPRegIsUnique || DType == AArch64::DestructiveBinary ||
599 "The destructive operand should be unique");
600 assert(ElementSize != AArch64::ElementSizeNone &&
601 "This instruction is unpredicated");
602
603 // Merge source operand into destination register
604 PRFX = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(MovPrfxZero))
605 .addReg(DstReg, RegState::Define)
606 .addReg(MI.getOperand(PredIdx).getReg())
607 .addReg(MI.getOperand(DOPIdx).getReg());
608
609 // After the movprfx, the destructive operand is same as Dst
610 DOPIdx = 0;
611
612 // Create the additional LSL to zero the lanes when the DstReg is not
613 // unique. Zeros the lanes in z0 that aren't active in p0 with sequence
614 // movprfx z0.b, p0/z, z0.b; lsl z0.b, p0/m, z0.b, #0;
615 if ((DType == AArch64::DestructiveBinary ||
618 !DOPRegIsUnique) {
619 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(LSLZero))
620 .addReg(DstReg, RegState::Define)
621 .add(MI.getOperand(PredIdx))
622 .addReg(DstReg)
623 .addImm(0);
624 }
625 } else if (DstReg != MI.getOperand(DOPIdx).getReg()) {
626 assert(DOPRegIsUnique && "The destructive operand should be unique");
627 PRFX = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(MovPrfx))
628 .addReg(DstReg, RegState::Define)
629 .addReg(MI.getOperand(DOPIdx).getReg());
630 DOPIdx = 0;
631 }
632
633 //
634 // Create the destructive operation
635 //
636 DOP = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opcode))
637 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead));
638
639 switch (DType) {
641 DOP.addReg(MI.getOperand(DOPIdx).getReg(), RegState::Kill)
642 .add(MI.getOperand(PredIdx))
643 .add(MI.getOperand(SrcIdx));
644 break;
649 DOP.add(MI.getOperand(PredIdx))
650 .addReg(MI.getOperand(DOPIdx).getReg(), RegState::Kill)
651 .add(MI.getOperand(SrcIdx));
652 break;
654 DOP.add(MI.getOperand(PredIdx))
655 .addReg(MI.getOperand(DOPIdx).getReg(), RegState::Kill)
656 .add(MI.getOperand(SrcIdx))
657 .add(MI.getOperand(Src2Idx));
658 break;
659 }
660
661 if (PRFX) {
663 transferImpOps(MI, PRFX, DOP);
664 } else
665 transferImpOps(MI, DOP, DOP);
666
667 MI.eraseFromParent();
668 return true;
669}
670
671bool AArch64ExpandPseudo::expandSetTagLoop(
673 MachineBasicBlock::iterator &NextMBBI) {
674 MachineInstr &MI = *MBBI;
675 DebugLoc DL = MI.getDebugLoc();
676 Register SizeReg = MI.getOperand(0).getReg();
677 Register AddressReg = MI.getOperand(1).getReg();
678
680
681 bool ZeroData = MI.getOpcode() == AArch64::STZGloop_wback;
682 const unsigned OpCode1 =
683 ZeroData ? AArch64::STZGPostIndex : AArch64::STGPostIndex;
684 const unsigned OpCode2 =
685 ZeroData ? AArch64::STZ2GPostIndex : AArch64::ST2GPostIndex;
686
687 unsigned Size = MI.getOperand(2).getImm();
688 assert(Size > 0 && Size % 16 == 0);
689 if (Size % (16 * 2) != 0) {
690 BuildMI(MBB, MBBI, DL, TII->get(OpCode1), AddressReg)
691 .addReg(AddressReg)
692 .addReg(AddressReg)
693 .addImm(1);
694 Size -= 16;
695 }
697 BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVi64imm), SizeReg)
698 .addImm(Size);
699 expandMOVImm(MBB, I, 64);
700
701 auto LoopBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
702 auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
703
704 MF->insert(++MBB.getIterator(), LoopBB);
705 MF->insert(++LoopBB->getIterator(), DoneBB);
706
707 BuildMI(LoopBB, DL, TII->get(OpCode2))
708 .addDef(AddressReg)
709 .addReg(AddressReg)
710 .addReg(AddressReg)
711 .addImm(2)
713 .setMIFlags(MI.getFlags());
714 BuildMI(LoopBB, DL, TII->get(AArch64::SUBSXri))
715 .addDef(SizeReg)
716 .addReg(SizeReg)
717 .addImm(16 * 2)
718 .addImm(0);
719 BuildMI(LoopBB, DL, TII->get(AArch64::Bcc))
721 .addMBB(LoopBB)
722 .addReg(AArch64::NZCV, RegState::Implicit | RegState::Kill);
723
724 LoopBB->addSuccessor(LoopBB);
725 LoopBB->addSuccessor(DoneBB);
726
727 DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
728 DoneBB->transferSuccessors(&MBB);
729
730 MBB.addSuccessor(LoopBB);
731
732 NextMBBI = MBB.end();
733 MI.eraseFromParent();
734 // Recompute liveness bottom up.
735 LivePhysRegs LiveRegs;
736 computeAndAddLiveIns(LiveRegs, *DoneBB);
737 computeAndAddLiveIns(LiveRegs, *LoopBB);
738 // Do an extra pass in the loop to get the loop carried dependencies right.
739 // FIXME: is this necessary?
740 LoopBB->clearLiveIns();
741 computeAndAddLiveIns(LiveRegs, *LoopBB);
742 DoneBB->clearLiveIns();
743 computeAndAddLiveIns(LiveRegs, *DoneBB);
744
745 return true;
746}
747
748bool AArch64ExpandPseudo::expandSVESpillFill(MachineBasicBlock &MBB,
750 unsigned Opc, unsigned N) {
751 const TargetRegisterInfo *TRI =
753 MachineInstr &MI = *MBBI;
754 for (unsigned Offset = 0; Offset < N; ++Offset) {
755 int ImmOffset = MI.getOperand(2).getImm() + Offset;
756 bool Kill = (Offset + 1 == N) ? MI.getOperand(1).isKill() : false;
757 assert(ImmOffset >= -256 && ImmOffset < 256 &&
758 "Immediate spill offset out of range");
759 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc))
760 .addReg(
761 TRI->getSubReg(MI.getOperand(0).getReg(), AArch64::zsub0 + Offset),
762 Opc == AArch64::LDR_ZXI ? RegState::Define : 0)
763 .addReg(MI.getOperand(1).getReg(), getKillRegState(Kill))
764 .addImm(ImmOffset);
765 }
766 MI.eraseFromParent();
767 return true;
768}
769
770bool AArch64ExpandPseudo::expandCALL_RVMARKER(
772 // Expand CALL_RVMARKER pseudo to:
773 // - a branch to the call target, followed by
774 // - the special `mov x29, x29` marker, and
775 // - another branch, to the runtime function
776 // Mark the sequence as bundle, to avoid passes moving other code in between.
777 MachineInstr &MI = *MBBI;
778
779 MachineInstr *OriginalCall;
780 MachineOperand &RVTarget = MI.getOperand(0);
781 MachineOperand &CallTarget = MI.getOperand(1);
782 assert((CallTarget.isGlobal() || CallTarget.isReg()) &&
783 "invalid operand for regular call");
784 assert(RVTarget.isGlobal() && "invalid operand for attached call");
785 unsigned Opc = CallTarget.isGlobal() ? AArch64::BL : AArch64::BLR;
786 OriginalCall = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)).getInstr();
787 OriginalCall->addOperand(CallTarget);
788
789 unsigned RegMaskStartIdx = 2;
790 // Skip register arguments. Those are added during ISel, but are not
791 // needed for the concrete branch.
792 while (!MI.getOperand(RegMaskStartIdx).isRegMask()) {
793 auto MOP = MI.getOperand(RegMaskStartIdx);
794 assert(MOP.isReg() && "can only add register operands");
796 MOP.getReg(), /*Def=*/false, /*Implicit=*/true));
797 RegMaskStartIdx++;
798 }
799 for (const MachineOperand &MO :
800 llvm::drop_begin(MI.operands(), RegMaskStartIdx))
801 OriginalCall->addOperand(MO);
802
803 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ORRXrs))
804 .addReg(AArch64::FP, RegState::Define)
805 .addReg(AArch64::XZR)
806 .addReg(AArch64::FP)
807 .addImm(0);
808
809 auto *RVCall = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::BL))
810 .add(RVTarget)
811 .getInstr();
812
813 if (MI.shouldUpdateCallSiteInfo())
814 MBB.getParent()->moveCallSiteInfo(&MI, OriginalCall);
815
816 MI.eraseFromParent();
817 finalizeBundle(MBB, OriginalCall->getIterator(),
818 std::next(RVCall->getIterator()));
819 return true;
820}
821
822bool AArch64ExpandPseudo::expandCALL_BTI(MachineBasicBlock &MBB,
824 // Expand CALL_BTI pseudo to:
825 // - a branch to the call target
826 // - a BTI instruction
827 // Mark the sequence as a bundle, to avoid passes moving other code in
828 // between.
829
830 MachineInstr &MI = *MBBI;
831 MachineOperand &CallTarget = MI.getOperand(0);
832 assert((CallTarget.isGlobal() || CallTarget.isReg()) &&
833 "invalid operand for regular call");
834 unsigned Opc = CallTarget.isGlobal() ? AArch64::BL : AArch64::BLR;
836 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)).getInstr();
837 Call->addOperand(CallTarget);
838 Call->setCFIType(*MBB.getParent(), MI.getCFIType());
839
840 MachineInstr *BTI =
841 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::HINT))
842 // BTI J so that setjmp can to BR to this.
843 .addImm(36)
844 .getInstr();
845
846 if (MI.shouldUpdateCallSiteInfo())
847 MBB.getParent()->moveCallSiteInfo(&MI, Call);
848
849 MI.eraseFromParent();
850 finalizeBundle(MBB, Call->getIterator(), std::next(BTI->getIterator()));
851 return true;
852}
853
854bool AArch64ExpandPseudo::expandStoreSwiftAsyncContext(
856 Register CtxReg = MBBI->getOperand(0).getReg();
857 Register BaseReg = MBBI->getOperand(1).getReg();
858 int Offset = MBBI->getOperand(2).getImm();
859 DebugLoc DL(MBBI->getDebugLoc());
860 auto &STI = MBB.getParent()->getSubtarget<AArch64Subtarget>();
861
862 if (STI.getTargetTriple().getArchName() != "arm64e") {
863 BuildMI(MBB, MBBI, DL, TII->get(AArch64::STRXui))
864 .addUse(CtxReg)
865 .addUse(BaseReg)
866 .addImm(Offset / 8)
869 return true;
870 }
871
872 // We need to sign the context in an address-discriminated way. 0xc31a is a
873 // fixed random value, chosen as part of the ABI.
874 // add x16, xBase, #Offset
875 // movk x16, #0xc31a, lsl #48
876 // mov x17, x22/xzr
877 // pacdb x17, x16
878 // str x17, [xBase, #Offset]
879 unsigned Opc = Offset >= 0 ? AArch64::ADDXri : AArch64::SUBXri;
880 BuildMI(MBB, MBBI, DL, TII->get(Opc), AArch64::X16)
881 .addUse(BaseReg)
882 .addImm(abs(Offset))
883 .addImm(0)
885 BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVKXi), AArch64::X16)
886 .addUse(AArch64::X16)
887 .addImm(0xc31a)
888 .addImm(48)
890 // We're not allowed to clobber X22 (and couldn't clobber XZR if we tried), so
891 // move it somewhere before signing.
892 BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::X17)
893 .addUse(AArch64::XZR)
894 .addUse(CtxReg)
895 .addImm(0)
897 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACDB), AArch64::X17)
898 .addUse(AArch64::X17)
899 .addUse(AArch64::X16)
901 BuildMI(MBB, MBBI, DL, TII->get(AArch64::STRXui))
902 .addUse(AArch64::X17)
903 .addUse(BaseReg)
904 .addImm(Offset / 8)
906
908 return true;
909}
910
912AArch64ExpandPseudo::expandRestoreZA(MachineBasicBlock &MBB,
914 MachineInstr &MI = *MBBI;
915 assert((std::next(MBBI) != MBB.end() ||
916 MI.getParent()->successors().begin() !=
917 MI.getParent()->successors().end()) &&
918 "Unexpected unreachable in block that restores ZA");
919
920 // Compare TPIDR2_EL0 value against 0.
921 DebugLoc DL = MI.getDebugLoc();
922 MachineInstrBuilder Cbz = BuildMI(MBB, MBBI, DL, TII->get(AArch64::CBZX))
923 .add(MI.getOperand(0));
924
925 // Split MBB and create two new blocks:
926 // - MBB now contains all instructions before RestoreZAPseudo.
927 // - SMBB contains the RestoreZAPseudo instruction only.
928 // - EndBB contains all instructions after RestoreZAPseudo.
929 MachineInstr &PrevMI = *std::prev(MBBI);
930 MachineBasicBlock *SMBB = MBB.splitAt(PrevMI, /*UpdateLiveIns*/ true);
931 MachineBasicBlock *EndBB = std::next(MI.getIterator()) == SMBB->end()
932 ? *SMBB->successors().begin()
933 : SMBB->splitAt(MI, /*UpdateLiveIns*/ true);
934
935 // Add the SMBB label to the TB[N]Z instruction & create a branch to EndBB.
936 Cbz.addMBB(SMBB);
937 BuildMI(&MBB, DL, TII->get(AArch64::B))
938 .addMBB(EndBB);
939 MBB.addSuccessor(EndBB);
940
941 // Replace the pseudo with a call (BL).
943 BuildMI(*SMBB, SMBB->end(), DL, TII->get(AArch64::BL));
944 MIB.addReg(MI.getOperand(1).getReg(), RegState::Implicit);
945 for (unsigned I = 2; I < MI.getNumOperands(); ++I)
946 MIB.add(MI.getOperand(I));
947 BuildMI(SMBB, DL, TII->get(AArch64::B)).addMBB(EndBB);
948
949 MI.eraseFromParent();
950 return EndBB;
951}
952
954AArch64ExpandPseudo::expandCondSMToggle(MachineBasicBlock &MBB,
956 MachineInstr &MI = *MBBI;
957 // In the case of a smstart/smstop before a unreachable, just remove the pseudo.
958 // Exception handling code generated by Clang may introduce unreachables and it
959 // seems unnecessary to restore pstate.sm when that happens. Note that it is
960 // not just an optimisation, the code below expects a successor instruction/block
961 // in order to split the block at MBBI.
962 if (std::next(MBBI) == MBB.end() &&
963 MI.getParent()->successors().begin() ==
964 MI.getParent()->successors().end()) {
965 MI.eraseFromParent();
966 return &MBB;
967 }
968
969 // Expand the pseudo into smstart or smstop instruction. The pseudo has the
970 // following operands:
971 //
972 // MSRpstatePseudo <za|sm|both>, <0|1>, pstate.sm, expectedval, <regmask>
973 //
974 // The pseudo is expanded into a conditional smstart/smstop, with a
975 // check if pstate.sm (register) equals the expected value, and if not,
976 // invokes the smstart/smstop.
977 //
978 // As an example, the following block contains a normal call from a
979 // streaming-compatible function:
980 //
981 // OrigBB:
982 // MSRpstatePseudo 3, 0, %0, 0, <regmask> <- Conditional SMSTOP
983 // bl @normal_callee
984 // MSRpstatePseudo 3, 1, %0, 0, <regmask> <- Conditional SMSTART
985 //
986 // ...which will be transformed into:
987 //
988 // OrigBB:
989 // TBNZx %0:gpr64, 0, SMBB
990 // b EndBB
991 //
992 // SMBB:
993 // MSRpstatesvcrImm1 3, 0, <regmask> <- SMSTOP
994 //
995 // EndBB:
996 // bl @normal_callee
997 // MSRcond_pstatesvcrImm1 3, 1, <regmask> <- SMSTART
998 //
999 DebugLoc DL = MI.getDebugLoc();
1000
1001 // Create the conditional branch based on the third operand of the
1002 // instruction, which tells us if we are wrapping a normal or streaming
1003 // function.
1004 // We test the live value of pstate.sm and toggle pstate.sm if this is not the
1005 // expected value for the callee (0 for a normal callee and 1 for a streaming
1006 // callee).
1007 auto PStateSM = MI.getOperand(2).getReg();
1008 bool IsStreamingCallee = MI.getOperand(3).getImm();
1009 unsigned Opc = IsStreamingCallee ? AArch64::TBZX : AArch64::TBNZX;
1011 BuildMI(MBB, MBBI, DL, TII->get(Opc)).addReg(PStateSM).addImm(0);
1012
1013 // Split MBB and create two new blocks:
1014 // - MBB now contains all instructions before MSRcond_pstatesvcrImm1.
1015 // - SMBB contains the MSRcond_pstatesvcrImm1 instruction only.
1016 // - EndBB contains all instructions after MSRcond_pstatesvcrImm1.
1017 MachineInstr &PrevMI = *std::prev(MBBI);
1018 MachineBasicBlock *SMBB = MBB.splitAt(PrevMI, /*UpdateLiveIns*/ true);
1019 MachineBasicBlock *EndBB = std::next(MI.getIterator()) == SMBB->end()
1020 ? *SMBB->successors().begin()
1021 : SMBB->splitAt(MI, /*UpdateLiveIns*/ true);
1022
1023 // Add the SMBB label to the TB[N]Z instruction & create a branch to EndBB.
1024 Tbx.addMBB(SMBB);
1025 BuildMI(&MBB, DL, TII->get(AArch64::B))
1026 .addMBB(EndBB);
1027 MBB.addSuccessor(EndBB);
1028
1029 // Create the SMSTART/SMSTOP (MSRpstatesvcrImm1) instruction in SMBB.
1030 MachineInstrBuilder MIB = BuildMI(*SMBB, SMBB->begin(), MI.getDebugLoc(),
1031 TII->get(AArch64::MSRpstatesvcrImm1));
1032 // Copy all but the second and third operands of MSRcond_pstatesvcrImm1 (as
1033 // these contain the CopyFromReg for the first argument and the flag to
1034 // indicate whether the callee is streaming or normal).
1035 MIB.add(MI.getOperand(0));
1036 MIB.add(MI.getOperand(1));
1037 for (unsigned i = 4; i < MI.getNumOperands(); ++i)
1038 MIB.add(MI.getOperand(i));
1039
1040 BuildMI(SMBB, DL, TII->get(AArch64::B)).addMBB(EndBB);
1041
1042 MI.eraseFromParent();
1043 return EndBB;
1044}
1045
1046bool AArch64ExpandPseudo::expandMultiVecPseudo(
1048 TargetRegisterClass ContiguousClass, TargetRegisterClass StridedClass,
1049 unsigned ContiguousOp, unsigned StridedOpc) {
1050 MachineInstr &MI = *MBBI;
1051 Register Tuple = MI.getOperand(0).getReg();
1052
1053 auto ContiguousRange = ContiguousClass.getRegisters();
1054 auto StridedRange = StridedClass.getRegisters();
1055 unsigned Opc;
1056 if ((std::find(ContiguousRange.begin(), ContiguousRange.end(),
1057 Tuple.asMCReg()) != std::end(ContiguousRange))) {
1058 Opc = ContiguousOp;
1059 } else if ((std::find(StridedRange.begin(), StridedRange.end(),
1060 Tuple.asMCReg()) != std::end(StridedRange))) {
1061 Opc = StridedOpc;
1062 } else
1063 llvm_unreachable("Cannot expand Multi-Vector pseudo");
1064
1065 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc))
1066 .add(MI.getOperand(0))
1067 .add(MI.getOperand(1))
1068 .add(MI.getOperand(2))
1069 .add(MI.getOperand(3));
1070 transferImpOps(MI, MIB, MIB);
1071 MI.eraseFromParent();
1072 return true;
1073}
1074
1075/// If MBBI references a pseudo instruction that should be expanded here,
1076/// do the expansion and return true. Otherwise return false.
1077bool AArch64ExpandPseudo::expandMI(MachineBasicBlock &MBB,
1079 MachineBasicBlock::iterator &NextMBBI) {
1080 MachineInstr &MI = *MBBI;
1081 unsigned Opcode = MI.getOpcode();
1082
1083 // Check if we can expand the destructive op
1084 int OrigInstr = AArch64::getSVEPseudoMap(MI.getOpcode());
1085 if (OrigInstr != -1) {
1086 auto &Orig = TII->get(OrigInstr);
1087 if ((Orig.TSFlags & AArch64::DestructiveInstTypeMask) !=
1089 return expand_DestructiveOp(MI, MBB, MBBI);
1090 }
1091 }
1092
1093 switch (Opcode) {
1094 default:
1095 break;
1096
1097 case AArch64::BSPv8i8:
1098 case AArch64::BSPv16i8: {
1099 Register DstReg = MI.getOperand(0).getReg();
1100 if (DstReg == MI.getOperand(3).getReg()) {
1101 // Expand to BIT
1102 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1103 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BITv8i8
1104 : AArch64::BITv16i8))
1105 .add(MI.getOperand(0))
1106 .add(MI.getOperand(3))
1107 .add(MI.getOperand(2))
1108 .add(MI.getOperand(1));
1109 } else if (DstReg == MI.getOperand(2).getReg()) {
1110 // Expand to BIF
1111 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1112 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BIFv8i8
1113 : AArch64::BIFv16i8))
1114 .add(MI.getOperand(0))
1115 .add(MI.getOperand(2))
1116 .add(MI.getOperand(3))
1117 .add(MI.getOperand(1));
1118 } else {
1119 // Expand to BSL, use additional move if required
1120 if (DstReg == MI.getOperand(1).getReg()) {
1121 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1122 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BSLv8i8
1123 : AArch64::BSLv16i8))
1124 .add(MI.getOperand(0))
1125 .add(MI.getOperand(1))
1126 .add(MI.getOperand(2))
1127 .add(MI.getOperand(3));
1128 } else {
1129 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1130 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::ORRv8i8
1131 : AArch64::ORRv16i8))
1132 .addReg(DstReg,
1134 getRenamableRegState(MI.getOperand(0).isRenamable()))
1135 .add(MI.getOperand(1))
1136 .add(MI.getOperand(1));
1137 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1138 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BSLv8i8
1139 : AArch64::BSLv16i8))
1140 .add(MI.getOperand(0))
1141 .addReg(DstReg,
1143 getRenamableRegState(MI.getOperand(0).isRenamable()))
1144 .add(MI.getOperand(2))
1145 .add(MI.getOperand(3));
1146 }
1147 }
1148 MI.eraseFromParent();
1149 return true;
1150 }
1151
1152 case AArch64::ADDWrr:
1153 case AArch64::SUBWrr:
1154 case AArch64::ADDXrr:
1155 case AArch64::SUBXrr:
1156 case AArch64::ADDSWrr:
1157 case AArch64::SUBSWrr:
1158 case AArch64::ADDSXrr:
1159 case AArch64::SUBSXrr:
1160 case AArch64::ANDWrr:
1161 case AArch64::ANDXrr:
1162 case AArch64::BICWrr:
1163 case AArch64::BICXrr:
1164 case AArch64::ANDSWrr:
1165 case AArch64::ANDSXrr:
1166 case AArch64::BICSWrr:
1167 case AArch64::BICSXrr:
1168 case AArch64::EONWrr:
1169 case AArch64::EONXrr:
1170 case AArch64::EORWrr:
1171 case AArch64::EORXrr:
1172 case AArch64::ORNWrr:
1173 case AArch64::ORNXrr:
1174 case AArch64::ORRWrr:
1175 case AArch64::ORRXrr: {
1176 unsigned Opcode;
1177 switch (MI.getOpcode()) {
1178 default:
1179 return false;
1180 case AArch64::ADDWrr: Opcode = AArch64::ADDWrs; break;
1181 case AArch64::SUBWrr: Opcode = AArch64::SUBWrs; break;
1182 case AArch64::ADDXrr: Opcode = AArch64::ADDXrs; break;
1183 case AArch64::SUBXrr: Opcode = AArch64::SUBXrs; break;
1184 case AArch64::ADDSWrr: Opcode = AArch64::ADDSWrs; break;
1185 case AArch64::SUBSWrr: Opcode = AArch64::SUBSWrs; break;
1186 case AArch64::ADDSXrr: Opcode = AArch64::ADDSXrs; break;
1187 case AArch64::SUBSXrr: Opcode = AArch64::SUBSXrs; break;
1188 case AArch64::ANDWrr: Opcode = AArch64::ANDWrs; break;
1189 case AArch64::ANDXrr: Opcode = AArch64::ANDXrs; break;
1190 case AArch64::BICWrr: Opcode = AArch64::BICWrs; break;
1191 case AArch64::BICXrr: Opcode = AArch64::BICXrs; break;
1192 case AArch64::ANDSWrr: Opcode = AArch64::ANDSWrs; break;
1193 case AArch64::ANDSXrr: Opcode = AArch64::ANDSXrs; break;
1194 case AArch64::BICSWrr: Opcode = AArch64::BICSWrs; break;
1195 case AArch64::BICSXrr: Opcode = AArch64::BICSXrs; break;
1196 case AArch64::EONWrr: Opcode = AArch64::EONWrs; break;
1197 case AArch64::EONXrr: Opcode = AArch64::EONXrs; break;
1198 case AArch64::EORWrr: Opcode = AArch64::EORWrs; break;
1199 case AArch64::EORXrr: Opcode = AArch64::EORXrs; break;
1200 case AArch64::ORNWrr: Opcode = AArch64::ORNWrs; break;
1201 case AArch64::ORNXrr: Opcode = AArch64::ORNXrs; break;
1202 case AArch64::ORRWrr: Opcode = AArch64::ORRWrs; break;
1203 case AArch64::ORRXrr: Opcode = AArch64::ORRXrs; break;
1204 }
1205 MachineFunction &MF = *MBB.getParent();
1206 // Try to create new inst without implicit operands added.
1207 MachineInstr *NewMI = MF.CreateMachineInstr(
1208 TII->get(Opcode), MI.getDebugLoc(), /*NoImplicit=*/true);
1209 MBB.insert(MBBI, NewMI);
1210 MachineInstrBuilder MIB1(MF, NewMI);
1211 MIB1->setPCSections(MF, MI.getPCSections());
1212 MIB1.addReg(MI.getOperand(0).getReg(), RegState::Define)
1213 .add(MI.getOperand(1))
1214 .add(MI.getOperand(2))
1216 transferImpOps(MI, MIB1, MIB1);
1217 if (auto DebugNumber = MI.peekDebugInstrNum())
1218 NewMI->setDebugInstrNum(DebugNumber);
1219 MI.eraseFromParent();
1220 return true;
1221 }
1222
1223 case AArch64::LOADgot: {
1225 Register DstReg = MI.getOperand(0).getReg();
1226 const MachineOperand &MO1 = MI.getOperand(1);
1227 unsigned Flags = MO1.getTargetFlags();
1228
1229 if (MF->getTarget().getCodeModel() == CodeModel::Tiny) {
1230 // Tiny codemodel expand to LDR
1231 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(),
1232 TII->get(AArch64::LDRXl), DstReg);
1233
1234 if (MO1.isGlobal()) {
1235 MIB.addGlobalAddress(MO1.getGlobal(), 0, Flags);
1236 } else if (MO1.isSymbol()) {
1237 MIB.addExternalSymbol(MO1.getSymbolName(), Flags);
1238 } else {
1239 assert(MO1.isCPI() &&
1240 "Only expect globals, externalsymbols, or constant pools");
1241 MIB.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(), Flags);
1242 }
1243 } else {
1244 // Small codemodel expand into ADRP + LDR.
1245 MachineFunction &MF = *MI.getParent()->getParent();
1246 DebugLoc DL = MI.getDebugLoc();
1247 MachineInstrBuilder MIB1 =
1248 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg);
1249
1253 unsigned Reg32 = TRI->getSubReg(DstReg, AArch64::sub_32);
1254 unsigned DstFlags = MI.getOperand(0).getTargetFlags();
1255 MIB2 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::LDRWui))
1256 .addDef(Reg32)
1257 .addReg(DstReg, RegState::Kill)
1258 .addReg(DstReg, DstFlags | RegState::Implicit);
1259 } else {
1260 Register DstReg = MI.getOperand(0).getReg();
1261 MIB2 = BuildMI(MBB, MBBI, DL, TII->get(AArch64::LDRXui))
1262 .add(MI.getOperand(0))
1263 .addUse(DstReg, RegState::Kill);
1264 }
1265
1266 if (MO1.isGlobal()) {
1267 MIB1.addGlobalAddress(MO1.getGlobal(), 0, Flags | AArch64II::MO_PAGE);
1268 MIB2.addGlobalAddress(MO1.getGlobal(), 0,
1270 } else if (MO1.isSymbol()) {
1272 MIB2.addExternalSymbol(MO1.getSymbolName(), Flags |
1275 } else {
1276 assert(MO1.isCPI() &&
1277 "Only expect globals, externalsymbols, or constant pools");
1278 MIB1.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(),
1279 Flags | AArch64II::MO_PAGE);
1280 MIB2.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(),
1281 Flags | AArch64II::MO_PAGEOFF |
1283 }
1284
1285 transferImpOps(MI, MIB1, MIB2);
1286 }
1287 MI.eraseFromParent();
1288 return true;
1289 }
1290 case AArch64::MOVaddrBA: {
1291 MachineFunction &MF = *MI.getParent()->getParent();
1293 // blockaddress expressions have to come from a constant pool because the
1294 // largest addend (and hence offset within a function) allowed for ADRP is
1295 // only 8MB.
1296 const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
1297 assert(MI.getOperand(1).getOffset() == 0 && "unexpected offset");
1298
1300 unsigned CPIdx = MCP->getConstantPoolIndex(BA, Align(8));
1301
1302 Register DstReg = MI.getOperand(0).getReg();
1303 auto MIB1 =
1304 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg)
1306 auto MIB2 = BuildMI(MBB, MBBI, MI.getDebugLoc(),
1307 TII->get(AArch64::LDRXui), DstReg)
1308 .addUse(DstReg)
1311 transferImpOps(MI, MIB1, MIB2);
1312 MI.eraseFromParent();
1313 return true;
1314 }
1315 }
1316 [[fallthrough]];
1317 case AArch64::MOVaddr:
1318 case AArch64::MOVaddrJT:
1319 case AArch64::MOVaddrCP:
1320 case AArch64::MOVaddrTLS:
1321 case AArch64::MOVaddrEXT: {
1322 // Expand into ADRP + ADD.
1323 Register DstReg = MI.getOperand(0).getReg();
1324 assert(DstReg != AArch64::XZR);
1325 MachineInstrBuilder MIB1 =
1326 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg)
1327 .add(MI.getOperand(1));
1328
1329 if (MI.getOperand(1).getTargetFlags() & AArch64II::MO_TAGGED) {
1330 // MO_TAGGED on the page indicates a tagged address. Set the tag now.
1331 // We do so by creating a MOVK that sets bits 48-63 of the register to
1332 // (global address + 0x100000000 - PC) >> 48. This assumes that we're in
1333 // the small code model so we can assume a binary size of <= 4GB, which
1334 // makes the untagged PC relative offset positive. The binary must also be
1335 // loaded into address range [0, 2^48). Both of these properties need to
1336 // be ensured at runtime when using tagged addresses.
1337 auto Tag = MI.getOperand(1);
1338 Tag.setTargetFlags(AArch64II::MO_PREL | AArch64II::MO_G3);
1339 Tag.setOffset(0x100000000);
1340 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi), DstReg)
1341 .addReg(DstReg)
1342 .add(Tag)
1343 .addImm(48);
1344 }
1345
1346 MachineInstrBuilder MIB2 =
1347 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDXri))
1348 .add(MI.getOperand(0))
1349 .addReg(DstReg)
1350 .add(MI.getOperand(2))
1351 .addImm(0);
1352
1353 transferImpOps(MI, MIB1, MIB2);
1354 MI.eraseFromParent();
1355 return true;
1356 }
1357 case AArch64::ADDlowTLS:
1358 // Produce a plain ADD
1359 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDXri))
1360 .add(MI.getOperand(0))
1361 .add(MI.getOperand(1))
1362 .add(MI.getOperand(2))
1363 .addImm(0);
1364 MI.eraseFromParent();
1365 return true;
1366
1367 case AArch64::MOVbaseTLS: {
1368 Register DstReg = MI.getOperand(0).getReg();
1369 auto SysReg = AArch64SysReg::TPIDR_EL0;
1371 if (MF->getSubtarget<AArch64Subtarget>().useEL3ForTP())
1372 SysReg = AArch64SysReg::TPIDR_EL3;
1373 else if (MF->getSubtarget<AArch64Subtarget>().useEL2ForTP())
1374 SysReg = AArch64SysReg::TPIDR_EL2;
1375 else if (MF->getSubtarget<AArch64Subtarget>().useEL1ForTP())
1376 SysReg = AArch64SysReg::TPIDR_EL1;
1377 else if (MF->getSubtarget<AArch64Subtarget>().useROEL0ForTP())
1378 SysReg = AArch64SysReg::TPIDRRO_EL0;
1379 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MRS), DstReg)
1380 .addImm(SysReg);
1381 MI.eraseFromParent();
1382 return true;
1383 }
1384
1385 case AArch64::MOVi32imm:
1386 return expandMOVImm(MBB, MBBI, 32);
1387 case AArch64::MOVi64imm:
1388 return expandMOVImm(MBB, MBBI, 64);
1389 case AArch64::RET_ReallyLR: {
1390 // Hiding the LR use with RET_ReallyLR may lead to extra kills in the
1391 // function and missing live-ins. We are fine in practice because callee
1392 // saved register handling ensures the register value is restored before
1393 // RET, but we need the undef flag here to appease the MachineVerifier
1394 // liveness checks.
1396 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::RET))
1397 .addReg(AArch64::LR, RegState::Undef);
1398 transferImpOps(MI, MIB, MIB);
1399 MI.eraseFromParent();
1400 return true;
1401 }
1402 case AArch64::CMP_SWAP_8:
1403 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRB, AArch64::STLXRB,
1404 AArch64::SUBSWrx,
1406 AArch64::WZR, NextMBBI);
1407 case AArch64::CMP_SWAP_16:
1408 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRH, AArch64::STLXRH,
1409 AArch64::SUBSWrx,
1411 AArch64::WZR, NextMBBI);
1412 case AArch64::CMP_SWAP_32:
1413 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRW, AArch64::STLXRW,
1414 AArch64::SUBSWrs,
1416 AArch64::WZR, NextMBBI);
1417 case AArch64::CMP_SWAP_64:
1418 return expandCMP_SWAP(MBB, MBBI,
1419 AArch64::LDAXRX, AArch64::STLXRX, AArch64::SUBSXrs,
1421 AArch64::XZR, NextMBBI);
1422 case AArch64::CMP_SWAP_128:
1423 case AArch64::CMP_SWAP_128_RELEASE:
1424 case AArch64::CMP_SWAP_128_ACQUIRE:
1425 case AArch64::CMP_SWAP_128_MONOTONIC:
1426 return expandCMP_SWAP_128(MBB, MBBI, NextMBBI);
1427
1428 case AArch64::AESMCrrTied:
1429 case AArch64::AESIMCrrTied: {
1431 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1432 TII->get(Opcode == AArch64::AESMCrrTied ? AArch64::AESMCrr :
1433 AArch64::AESIMCrr))
1434 .add(MI.getOperand(0))
1435 .add(MI.getOperand(1));
1436 transferImpOps(MI, MIB, MIB);
1437 MI.eraseFromParent();
1438 return true;
1439 }
1440 case AArch64::IRGstack: {
1441 MachineFunction &MF = *MBB.getParent();
1443 const AArch64FrameLowering *TFI =
1444 MF.getSubtarget<AArch64Subtarget>().getFrameLowering();
1445
1446 // IRG does not allow immediate offset. getTaggedBasePointerOffset should
1447 // almost always point to SP-after-prologue; if not, emit a longer
1448 // instruction sequence.
1449 int BaseOffset = -AFI->getTaggedBasePointerOffset();
1450 Register FrameReg;
1451 StackOffset FrameRegOffset = TFI->resolveFrameOffsetReference(
1452 MF, BaseOffset, false /*isFixed*/, false /*isSVE*/, FrameReg,
1453 /*PreferFP=*/false,
1454 /*ForSimm=*/true);
1455 Register SrcReg = FrameReg;
1456 if (FrameRegOffset) {
1457 // Use output register as temporary.
1458 SrcReg = MI.getOperand(0).getReg();
1459 emitFrameOffset(MBB, &MI, MI.getDebugLoc(), SrcReg, FrameReg,
1460 FrameRegOffset, TII);
1461 }
1462 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::IRG))
1463 .add(MI.getOperand(0))
1464 .addUse(SrcReg)
1465 .add(MI.getOperand(2));
1466 MI.eraseFromParent();
1467 return true;
1468 }
1469 case AArch64::TAGPstack: {
1470 int64_t Offset = MI.getOperand(2).getImm();
1471 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1472 TII->get(Offset >= 0 ? AArch64::ADDG : AArch64::SUBG))
1473 .add(MI.getOperand(0))
1474 .add(MI.getOperand(1))
1475 .addImm(std::abs(Offset))
1476 .add(MI.getOperand(4));
1477 MI.eraseFromParent();
1478 return true;
1479 }
1480 case AArch64::STGloop_wback:
1481 case AArch64::STZGloop_wback:
1482 return expandSetTagLoop(MBB, MBBI, NextMBBI);
1483 case AArch64::STGloop:
1484 case AArch64::STZGloop:
1486 "Non-writeback variants of STGloop / STZGloop should not "
1487 "survive past PrologEpilogInserter.");
1488 case AArch64::STR_ZZZZXI:
1489 return expandSVESpillFill(MBB, MBBI, AArch64::STR_ZXI, 4);
1490 case AArch64::STR_ZZZXI:
1491 return expandSVESpillFill(MBB, MBBI, AArch64::STR_ZXI, 3);
1492 case AArch64::STR_ZZXI:
1493 return expandSVESpillFill(MBB, MBBI, AArch64::STR_ZXI, 2);
1494 case AArch64::LDR_ZZZZXI:
1495 return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 4);
1496 case AArch64::LDR_ZZZXI:
1497 return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 3);
1498 case AArch64::LDR_ZZXI:
1499 return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 2);
1500 case AArch64::BLR_RVMARKER:
1501 return expandCALL_RVMARKER(MBB, MBBI);
1502 case AArch64::BLR_BTI:
1503 return expandCALL_BTI(MBB, MBBI);
1504 case AArch64::StoreSwiftAsyncContext:
1505 return expandStoreSwiftAsyncContext(MBB, MBBI);
1506 case AArch64::RestoreZAPseudo: {
1507 auto *NewMBB = expandRestoreZA(MBB, MBBI);
1508 if (NewMBB != &MBB)
1509 NextMBBI = MBB.end(); // The NextMBBI iterator is invalidated.
1510 return true;
1511 }
1512 case AArch64::MSRpstatePseudo: {
1513 auto *NewMBB = expandCondSMToggle(MBB, MBBI);
1514 if (NewMBB != &MBB)
1515 NextMBBI = MBB.end(); // The NextMBBI iterator is invalidated.
1516 return true;
1517 }
1518 case AArch64::LD1B_2Z_IMM_PSEUDO:
1519 return expandMultiVecPseudo(
1520 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1521 AArch64::LD1B_2Z_IMM, AArch64::LD1B_2Z_STRIDED_IMM);
1522 case AArch64::LD1H_2Z_IMM_PSEUDO:
1523 return expandMultiVecPseudo(
1524 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1525 AArch64::LD1H_2Z_IMM, AArch64::LD1H_2Z_STRIDED_IMM);
1526 case AArch64::LD1W_2Z_IMM_PSEUDO:
1527 return expandMultiVecPseudo(
1528 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1529 AArch64::LD1W_2Z_IMM, AArch64::LD1W_2Z_STRIDED_IMM);
1530 case AArch64::LD1D_2Z_IMM_PSEUDO:
1531 return expandMultiVecPseudo(
1532 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1533 AArch64::LD1D_2Z_IMM, AArch64::LD1D_2Z_STRIDED_IMM);
1534 case AArch64::LDNT1B_2Z_IMM_PSEUDO:
1535 return expandMultiVecPseudo(
1536 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1537 AArch64::LDNT1B_2Z_IMM, AArch64::LDNT1B_2Z_STRIDED_IMM);
1538 case AArch64::LDNT1H_2Z_IMM_PSEUDO:
1539 return expandMultiVecPseudo(
1540 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1541 AArch64::LDNT1H_2Z_IMM, AArch64::LDNT1H_2Z_STRIDED_IMM);
1542 case AArch64::LDNT1W_2Z_IMM_PSEUDO:
1543 return expandMultiVecPseudo(
1544 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1545 AArch64::LDNT1W_2Z_IMM, AArch64::LDNT1W_2Z_STRIDED_IMM);
1546 case AArch64::LDNT1D_2Z_IMM_PSEUDO:
1547 return expandMultiVecPseudo(
1548 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1549 AArch64::LDNT1D_2Z_IMM, AArch64::LDNT1D_2Z_STRIDED_IMM);
1550 case AArch64::LD1B_2Z_PSEUDO:
1551 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1552 AArch64::ZPR2StridedRegClass, AArch64::LD1B_2Z,
1553 AArch64::LD1B_2Z_STRIDED);
1554 case AArch64::LD1H_2Z_PSEUDO:
1555 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1556 AArch64::ZPR2StridedRegClass, AArch64::LD1H_2Z,
1557 AArch64::LD1H_2Z_STRIDED);
1558 case AArch64::LD1W_2Z_PSEUDO:
1559 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1560 AArch64::ZPR2StridedRegClass, AArch64::LD1W_2Z,
1561 AArch64::LD1W_2Z_STRIDED);
1562 case AArch64::LD1D_2Z_PSEUDO:
1563 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1564 AArch64::ZPR2StridedRegClass, AArch64::LD1D_2Z,
1565 AArch64::LD1D_2Z_STRIDED);
1566 case AArch64::LDNT1B_2Z_PSEUDO:
1567 return expandMultiVecPseudo(
1568 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1569 AArch64::LDNT1B_2Z, AArch64::LDNT1B_2Z_STRIDED);
1570 case AArch64::LDNT1H_2Z_PSEUDO:
1571 return expandMultiVecPseudo(
1572 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1573 AArch64::LDNT1H_2Z, AArch64::LDNT1H_2Z_STRIDED);
1574 case AArch64::LDNT1W_2Z_PSEUDO:
1575 return expandMultiVecPseudo(
1576 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1577 AArch64::LDNT1W_2Z, AArch64::LDNT1W_2Z_STRIDED);
1578 case AArch64::LDNT1D_2Z_PSEUDO:
1579 return expandMultiVecPseudo(
1580 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1581 AArch64::LDNT1D_2Z, AArch64::LDNT1D_2Z_STRIDED);
1582 case AArch64::LD1B_4Z_IMM_PSEUDO:
1583 return expandMultiVecPseudo(
1584 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1585 AArch64::LD1B_4Z_IMM, AArch64::LD1B_4Z_STRIDED_IMM);
1586 case AArch64::LD1H_4Z_IMM_PSEUDO:
1587 return expandMultiVecPseudo(
1588 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1589 AArch64::LD1H_4Z_IMM, AArch64::LD1H_4Z_STRIDED_IMM);
1590 case AArch64::LD1W_4Z_IMM_PSEUDO:
1591 return expandMultiVecPseudo(
1592 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1593 AArch64::LD1W_4Z_IMM, AArch64::LD1W_4Z_STRIDED_IMM);
1594 case AArch64::LD1D_4Z_IMM_PSEUDO:
1595 return expandMultiVecPseudo(
1596 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1597 AArch64::LD1D_4Z_IMM, AArch64::LD1D_4Z_STRIDED_IMM);
1598 case AArch64::LDNT1B_4Z_IMM_PSEUDO:
1599 return expandMultiVecPseudo(
1600 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1601 AArch64::LDNT1B_4Z_IMM, AArch64::LDNT1B_4Z_STRIDED_IMM);
1602 case AArch64::LDNT1H_4Z_IMM_PSEUDO:
1603 return expandMultiVecPseudo(
1604 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1605 AArch64::LDNT1H_4Z_IMM, AArch64::LDNT1H_4Z_STRIDED_IMM);
1606 case AArch64::LDNT1W_4Z_IMM_PSEUDO:
1607 return expandMultiVecPseudo(
1608 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1609 AArch64::LDNT1W_4Z_IMM, AArch64::LDNT1W_4Z_STRIDED_IMM);
1610 case AArch64::LDNT1D_4Z_IMM_PSEUDO:
1611 return expandMultiVecPseudo(
1612 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1613 AArch64::LDNT1D_4Z_IMM, AArch64::LDNT1D_4Z_STRIDED_IMM);
1614 case AArch64::LD1B_4Z_PSEUDO:
1615 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1616 AArch64::ZPR4StridedRegClass, AArch64::LD1B_4Z,
1617 AArch64::LD1B_4Z_STRIDED);
1618 case AArch64::LD1H_4Z_PSEUDO:
1619 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1620 AArch64::ZPR4StridedRegClass, AArch64::LD1H_4Z,
1621 AArch64::LD1H_4Z_STRIDED);
1622 case AArch64::LD1W_4Z_PSEUDO:
1623 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1624 AArch64::ZPR4StridedRegClass, AArch64::LD1W_4Z,
1625 AArch64::LD1W_4Z_STRIDED);
1626 case AArch64::LD1D_4Z_PSEUDO:
1627 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1628 AArch64::ZPR4StridedRegClass, AArch64::LD1D_4Z,
1629 AArch64::LD1D_4Z_STRIDED);
1630 case AArch64::LDNT1B_4Z_PSEUDO:
1631 return expandMultiVecPseudo(
1632 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1633 AArch64::LDNT1B_4Z, AArch64::LDNT1B_4Z_STRIDED);
1634 case AArch64::LDNT1H_4Z_PSEUDO:
1635 return expandMultiVecPseudo(
1636 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1637 AArch64::LDNT1H_4Z, AArch64::LDNT1H_4Z_STRIDED);
1638 case AArch64::LDNT1W_4Z_PSEUDO:
1639 return expandMultiVecPseudo(
1640 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1641 AArch64::LDNT1W_4Z, AArch64::LDNT1W_4Z_STRIDED);
1642 case AArch64::LDNT1D_4Z_PSEUDO:
1643 return expandMultiVecPseudo(
1644 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1645 AArch64::LDNT1D_4Z, AArch64::LDNT1D_4Z_STRIDED);
1646 }
1647 return false;
1648}
1649
1650/// Iterate over the instructions in basic block MBB and expand any
1651/// pseudo instructions. Return true if anything was modified.
1652bool AArch64ExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
1653 bool Modified = false;
1654
1656 while (MBBI != E) {
1657 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
1658 Modified |= expandMI(MBB, MBBI, NMBBI);
1659 MBBI = NMBBI;
1660 }
1661
1662 return Modified;
1663}
1664
1665bool AArch64ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
1666 TII = static_cast<const AArch64InstrInfo *>(MF.getSubtarget().getInstrInfo());
1667
1668 bool Modified = false;
1669 for (auto &MBB : MF)
1670 Modified |= expandMBB(MBB);
1671 return Modified;
1672}
1673
1674/// Returns an instance of the pseudo instruction expansion pass.
1676 return new AArch64ExpandPseudo();
1677}
#define AARCH64_EXPAND_PSEUDO_NAME
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
SmallVector< AArch64_IMM::ImmInsnModel, 4 > Insn
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
The address of a basic block.
Definition: Constants.h:874
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:50
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
Set of metadata that should be preserved when using BuildMI().
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
MachineBasicBlock * splitAt(MachineInstr &SplitInst, bool UpdateLiveIns=true, LiveIntervals *LIS=nullptr)
Split a basic block into 2 pieces at SplitPoint.
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
The MachineConstantPool class keeps track of constants referenced by a function which must be spilled...
unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineInstr * CreateMachineInstr(const MCInstrDesc &MCID, DebugLoc DL, bool NoImplicit=false)
CreateMachineInstr - Allocate a new MachineInstr.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
void moveCallSiteInfo(const MachineInstr *Old, const MachineInstr *New)
Move the call site info from Old to \New call site info.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
Definition: MachineInstr.h:68
void setDebugInstrNum(unsigned Num)
Set instruction number of this MachineInstr.
Definition: MachineInstr.h:523
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
int64_t getOffset() const
Return the offset from the symbol in this operand.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition: Register.h:110
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:36
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
CodeModel::Model getCodeModel() const
Returns the code model.
iterator_range< SmallVectorImpl< MCPhysReg >::const_iterator > getRegisters() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
self_iterator getIterator()
Definition: ilist_node.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_NC
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow.
@ MO_PAGEOFF
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page.
@ MO_PREL
MO_PREL - Indicates that the bits of the symbol operand represented by MO_G0 etc are PC relative.
@ MO_PAGE
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
@ MO_TAGGED
MO_TAGGED - With MO_PAGE, indicates that the page includes a memory tag in bits 56-63.
@ MO_G3
MO_G3 - A symbol operand with this flag (granule 3) represents the high 16-bits of a 64-bit address,...
static unsigned getArithExtendImm(AArch64_AM::ShiftExtendType ET, unsigned Imm)
getArithExtendImm - Encode the extend type and shift amount for an arithmetic instruction: imm: 3-bit...
static unsigned getShifterImm(AArch64_AM::ShiftExtendType ST, unsigned Imm)
getShifterImm - Encode the shift type and amount: imm: 6-bit shift amount shifter: 000 ==> lsl 001 ==...
void expandMOVImm(uint64_t Imm, unsigned BitSize, SmallVectorImpl< ImmInsnModel > &Insn)
Expand a MOVi32imm or MOVi64imm pseudo instruction to one or more real move-immediate instructions to...
int getSVERevInstr(uint16_t Opcode)
int getSVEPseudoMap(uint16_t Opcode)
int getSVENonRevInstr(uint16_t Opcode)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Renamable
Register that may be renamed.
@ Define
Register definition.
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:330
@ Offset
Definition: DWP.cpp:440
void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1379
unsigned getDeadRegState(bool B)
void initializeAArch64ExpandPseudoPass(PassRegistry &)
FunctionPass * createAArch64ExpandPseudoPass()
Returns an instance of the pseudo instruction expansion pass.
void emitFrameOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, unsigned DestReg, unsigned SrcReg, StackOffset Offset, const TargetInstrInfo *TII, MachineInstr::MIFlag=MachineInstr::NoFlags, bool SetNZCV=false, bool NeedsWinCFI=false, bool *HasWinCFI=nullptr, bool EmitCFAOffset=false, StackOffset InitialOffset={}, unsigned FrameReg=AArch64::SP)
emitFrameOffset - Emit instructions as needed to set DestReg to SrcReg plus Offset.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
unsigned getKillRegState(bool B)
unsigned getRenamableRegState(bool B)
void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Description of the encoding of one expression Op.