LLVM 24.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"
31#include "llvm/IR/DebugLoc.h"
32#include "llvm/MC/MCInstrDesc.h"
33#include "llvm/Pass.h"
37#include <cassert>
38#include <cstdint>
39#include <iterator>
40
41using namespace llvm;
42
43#define AARCH64_EXPAND_PSEUDO_NAME "AArch64 pseudo instruction expansion pass"
44
45namespace {
46
47class AArch64ExpandPseudoImpl {
48public:
49 const AArch64InstrInfo *TII;
50
51 bool run(MachineFunction &MF);
52
53private:
54 bool expandMBB(MachineBasicBlock &MBB);
57 bool expandMultiVecPseudo(MachineBasicBlock &MBB,
59 const TargetRegisterClass &ContiguousClass,
60 const TargetRegisterClass &StridedClass,
61 unsigned ContiguousOpc, unsigned StridedOpc);
62 bool expandCopyIntoTuplePseudo(MachineInstr &MI, MachineBasicBlock &MBB,
65 unsigned BitSize);
66
67 bool expand_DestructiveOp(MachineInstr &MI, MachineBasicBlock &MBB,
69 bool expandSVEBitwisePseudo(MachineInstr &MI, MachineBasicBlock &MBB,
72 unsigned LdarOp, unsigned StlrOp, unsigned CmpOp,
73 unsigned ExtendImm, unsigned ZeroReg,
75 bool expandCMP_SWAP_128(MachineBasicBlock &MBB,
78 bool expandSetTagLoop(MachineBasicBlock &MBB,
81 bool expandSVESpillFill(MachineBasicBlock &MBB,
83 unsigned N);
84 bool expandCALL_RVMARKER(MachineBasicBlock &MBB,
87 bool expandStoreSwiftAsyncContext(MachineBasicBlock &MBB,
89 struct ConditionalBlocks {
90 MachineBasicBlock &CondBB;
91 MachineBasicBlock &EndBB;
92 };
93 ConditionalBlocks expandConditionalPseudo(MachineBasicBlock &MBB,
96 MachineInstrBuilder &Branch);
97 MachineBasicBlock *expandRestoreZASave(MachineBasicBlock &MBB,
99 MachineBasicBlock *expandCommitZASave(MachineBasicBlock &MBB,
101 MachineBasicBlock *expandCondSMToggle(MachineBasicBlock &MBB,
103};
104
105class AArch64ExpandPseudoLegacy : public MachineFunctionPass {
106public:
107 static char ID;
108
109 AArch64ExpandPseudoLegacy() : MachineFunctionPass(ID) {}
110
111 bool runOnMachineFunction(MachineFunction &MF) override;
112
113 StringRef getPassName() const override { return AARCH64_EXPAND_PSEUDO_NAME; }
114};
115
116} // end anonymous namespace
117
118char AArch64ExpandPseudoLegacy::ID = 0;
119
120INITIALIZE_PASS(AArch64ExpandPseudoLegacy, "aarch64-expand-pseudo",
121 AARCH64_EXPAND_PSEUDO_NAME, false, false)
122
123/// Transfer implicit operands on the pseudo instruction to the
124/// instructions created from the expansion.
127 const MCInstrDesc &Desc = OldMI.getDesc();
128 for (const MachineOperand &MO :
129 llvm::drop_begin(OldMI.operands(), Desc.getNumOperands())) {
130 assert(MO.isReg() && MO.getReg());
131 if (MO.isUse())
132 UseMI.add(MO);
133 else
134 DefMI.add(MO);
135 }
136}
137
138/// Expand a MOVi32imm or MOVi64imm pseudo instruction to one or more
139/// real move-immediate instructions to synthesize the immediate.
140bool AArch64ExpandPseudoImpl::expandMOVImm(MachineBasicBlock &MBB,
142 unsigned BitSize) {
143 MachineInstr &MI = *MBBI;
144 Register DstReg = MI.getOperand(0).getReg();
145 RegState RenamableState =
146 getRenamableRegState(MI.getOperand(0).isRenamable());
147 uint64_t Imm = MI.getOperand(1).getImm();
148
149 if (DstReg == AArch64::XZR || DstReg == AArch64::WZR) {
150 // Useless def, and we don't want to risk creating an invalid ORR (which
151 // would really write to sp).
152 MI.eraseFromParent();
153 return true;
154 }
155
157 AArch64_IMM::expandMOVImm(Imm, BitSize, Insn);
158 assert(Insn.size() != 0);
159
160 SmallVector<MachineInstrBuilder, 4> MIBS;
161 for (auto I = Insn.begin(), E = Insn.end(); I != E; ++I) {
162 bool LastItem = std::next(I) == E;
163 switch (I->Opcode)
164 {
165 default: llvm_unreachable("unhandled!"); break;
166
167 case AArch64::ORRWri:
168 case AArch64::ORRXri:
169 case AArch64::ANDXri:
170 case AArch64::EORXri:
171 if (I->Op1 == 0) {
172 MIBS.push_back(BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
173 .add(MI.getOperand(0))
174 .addReg(BitSize == 32 ? AArch64::WZR : AArch64::XZR)
175 .addImm(*I->Op2));
176 } else {
177 Register DstReg = MI.getOperand(0).getReg();
178 bool DstIsDead = MI.getOperand(0).isDead();
179 MIBS.push_back(
180 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
181 .addReg(DstReg, RegState::Define |
182 getDeadRegState(DstIsDead && LastItem) |
183 RenamableState)
184 .addReg(DstReg)
185 .addImm(*I->Op2));
186 }
187 break;
188 case AArch64::EONXrs:
189 case AArch64::EORXrs:
190 case AArch64::ORRWrs:
191 case AArch64::ORRXrs: {
192 Register DstReg = MI.getOperand(0).getReg();
193 bool DstIsDead = MI.getOperand(0).isDead();
194 MIBS.push_back(
195 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
196 .addReg(DstReg, RegState::Define |
197 getDeadRegState(DstIsDead && LastItem) |
198 RenamableState)
199 .addReg(DstReg)
200 .addReg(DstReg)
201 .addImm(*I->Op2));
202 } break;
203 case AArch64::MOVNWi:
204 case AArch64::MOVNXi:
205 case AArch64::MOVZWi:
206 case AArch64::MOVZXi: {
207 bool DstIsDead = MI.getOperand(0).isDead();
208 MIBS.push_back(
209 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
210 .addReg(DstReg, RegState::Define |
211 getDeadRegState(DstIsDead && LastItem) |
212 RenamableState)
213 .addImm(*I->Op1)
214 .addImm(*I->Op2));
215 } break;
216 case AArch64::MOVKWi:
217 case AArch64::MOVKXi: {
218 Register DstReg = MI.getOperand(0).getReg();
219 bool DstIsDead = MI.getOperand(0).isDead();
220 MIBS.push_back(
221 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(I->Opcode))
222 .addReg(DstReg, RegState::Define |
223 getDeadRegState(DstIsDead && LastItem) |
224 RenamableState)
225 .addReg(DstReg)
226 .addImm(*I->Op1)
227 .addImm(*I->Op2));
228 } break;
229 }
230 }
231 transferImpOps(MI, MIBS.front(), MIBS.back());
232 MI.eraseFromParent();
233 return true;
234}
235
236bool AArch64ExpandPseudoImpl::expandCMP_SWAP(
237 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned LdarOp,
238 unsigned StlrOp, unsigned CmpOp, unsigned ExtendImm, unsigned ZeroReg,
239 MachineBasicBlock::iterator &NextMBBI) {
240 MachineInstr &MI = *MBBI;
241 MIMetadata MIMD(MI);
242 const MachineOperand &Dest = MI.getOperand(0);
243 Register StatusReg = MI.getOperand(1).getReg();
244 bool StatusDead = MI.getOperand(1).isDead();
245 // Duplicating undef operands into 2 instructions does not guarantee the same
246 // value on both; However undef should be replaced by xzr anyway.
247 assert(!MI.getOperand(2).isUndef() && "cannot handle undef");
248 Register AddrReg = MI.getOperand(2).getReg();
249 Register DesiredReg = MI.getOperand(3).getReg();
250 Register NewReg = MI.getOperand(4).getReg();
251
253 auto LoadCmpBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
254 auto StoreBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
255 auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
256
257 MF->insert(++MBB.getIterator(), LoadCmpBB);
258 MF->insert(++LoadCmpBB->getIterator(), StoreBB);
259 MF->insert(++StoreBB->getIterator(), DoneBB);
260
261 // .Lloadcmp:
262 // mov wStatus, 0
263 // ldaxr xDest, [xAddr]
264 // cmp xDest, xDesired
265 // b.ne .Ldone
266 if (!StatusDead)
267 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::MOVZWi), StatusReg)
268 .addImm(0).addImm(0);
269 BuildMI(LoadCmpBB, MIMD, TII->get(LdarOp), Dest.getReg())
270 .addReg(AddrReg);
271 BuildMI(LoadCmpBB, MIMD, TII->get(CmpOp), ZeroReg)
272 .addReg(Dest.getReg(), getKillRegState(Dest.isDead()))
273 .addReg(DesiredReg)
274 .addImm(ExtendImm);
275 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::Bcc))
277 .addMBB(DoneBB)
278 .addReg(AArch64::NZCV, RegState::Implicit | RegState::Kill);
279 LoadCmpBB->addSuccessor(DoneBB);
280 LoadCmpBB->addSuccessor(StoreBB);
281
282 // .Lstore:
283 // stlxr wStatus, xNew, [xAddr]
284 // cbnz wStatus, .Lloadcmp
285 BuildMI(StoreBB, MIMD, TII->get(StlrOp), StatusReg)
286 .addReg(NewReg)
287 .addReg(AddrReg);
288 BuildMI(StoreBB, MIMD, TII->get(AArch64::CBNZW))
289 .addReg(StatusReg, getKillRegState(StatusDead))
290 .addMBB(LoadCmpBB);
291 StoreBB->addSuccessor(LoadCmpBB);
292 StoreBB->addSuccessor(DoneBB);
293
294 DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
295 DoneBB->transferSuccessors(&MBB);
296
297 MBB.addSuccessor(LoadCmpBB);
298
299 NextMBBI = MBB.end();
300 MI.eraseFromParent();
301
302 // Recompute livein lists.
303 LivePhysRegs LiveRegs;
304 computeAndAddLiveIns(LiveRegs, *DoneBB);
305 computeAndAddLiveIns(LiveRegs, *StoreBB);
306 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
307 // Do an extra pass around the loop to get loop carried registers right.
308 StoreBB->clearLiveIns();
309 computeAndAddLiveIns(LiveRegs, *StoreBB);
310 LoadCmpBB->clearLiveIns();
311 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
312
313 return true;
314}
315
316bool AArch64ExpandPseudoImpl::expandCMP_SWAP_128(
317 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
318 MachineBasicBlock::iterator &NextMBBI) {
319 MachineInstr &MI = *MBBI;
320 MIMetadata MIMD(MI);
321 MachineOperand &DestLo = MI.getOperand(0);
322 MachineOperand &DestHi = MI.getOperand(1);
323 Register StatusReg = MI.getOperand(2).getReg();
324 bool StatusDead = MI.getOperand(2).isDead();
325 // Duplicating undef operands into 2 instructions does not guarantee the same
326 // value on both; However undef should be replaced by xzr anyway.
327 assert(!MI.getOperand(3).isUndef() && "cannot handle undef");
328 Register AddrReg = MI.getOperand(3).getReg();
329 Register DesiredLoReg = MI.getOperand(4).getReg();
330 Register DesiredHiReg = MI.getOperand(5).getReg();
331 Register NewLoReg = MI.getOperand(6).getReg();
332 Register NewHiReg = MI.getOperand(7).getReg();
333
334 auto &STI = MBB.getParent()->getSubtarget<AArch64Subtarget>();
335 bool LittleEndian = STI.isLittleEndian();
336 MachineOperand &Dest0 = LittleEndian ? DestLo : DestHi;
337 MachineOperand &Dest1 = LittleEndian ? DestHi : DestLo;
338 Register New0Reg = LittleEndian ? NewLoReg : NewHiReg;
339 Register New1Reg = LittleEndian ? NewHiReg : NewLoReg;
340
341 unsigned LdxpOp, StxpOp;
342
343 switch (MI.getOpcode()) {
344 case AArch64::CMP_SWAP_128_MONOTONIC:
345 LdxpOp = AArch64::LDXPX;
346 StxpOp = AArch64::STXPX;
347 break;
348 case AArch64::CMP_SWAP_128_RELEASE:
349 LdxpOp = AArch64::LDXPX;
350 StxpOp = AArch64::STLXPX;
351 break;
352 case AArch64::CMP_SWAP_128_ACQUIRE:
353 LdxpOp = AArch64::LDAXPX;
354 StxpOp = AArch64::STXPX;
355 break;
356 case AArch64::CMP_SWAP_128:
357 LdxpOp = AArch64::LDAXPX;
358 StxpOp = AArch64::STLXPX;
359 break;
360 default:
361 llvm_unreachable("Unexpected opcode");
362 }
363
365 auto LoadCmpBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
366 auto StoreBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
367 auto FailBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
368 auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
369
370 MF->insert(++MBB.getIterator(), LoadCmpBB);
371 MF->insert(++LoadCmpBB->getIterator(), StoreBB);
372 MF->insert(++StoreBB->getIterator(), FailBB);
373 MF->insert(++FailBB->getIterator(), DoneBB);
374
375 // .Lloadcmp:
376 // ldaxp xDestLo, xDestHi, [xAddr]
377 // cmp xDestLo, xDesiredLo
378 // sbcs xDestHi, xDesiredHi
379 // b.ne .Ldone
380 BuildMI(LoadCmpBB, MIMD, TII->get(LdxpOp))
381 .addReg(Dest0.getReg(), RegState::Define)
382 .addReg(Dest1.getReg(), RegState::Define)
383 .addReg(AddrReg);
384 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::SUBSXrs), AArch64::XZR)
385 .addReg(DestLo.getReg(), getKillRegState(DestLo.isDead()))
386 .addReg(DesiredLoReg)
387 .addImm(0);
388 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::CSINCWr), StatusReg)
389 .addUse(AArch64::WZR)
390 .addUse(AArch64::WZR)
392 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::SUBSXrs), AArch64::XZR)
393 .addReg(DestHi.getReg(), getKillRegState(DestHi.isDead()))
394 .addReg(DesiredHiReg)
395 .addImm(0);
396 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::CSINCWr), StatusReg)
397 .addUse(StatusReg, RegState::Kill)
398 .addUse(StatusReg, RegState::Kill)
400 BuildMI(LoadCmpBB, MIMD, TII->get(AArch64::CBNZW))
401 .addUse(StatusReg, getKillRegState(StatusDead))
402 .addMBB(FailBB);
403 LoadCmpBB->addSuccessor(FailBB);
404 LoadCmpBB->addSuccessor(StoreBB);
405
406 // .Lstore:
407 // stlxp wStatus, xNewLo, xNewHi, [xAddr]
408 // cbnz wStatus, .Lloadcmp
409 BuildMI(StoreBB, MIMD, TII->get(StxpOp), StatusReg)
410 .addReg(New0Reg)
411 .addReg(New1Reg)
412 .addReg(AddrReg);
413 BuildMI(StoreBB, MIMD, TII->get(AArch64::CBNZW))
414 .addReg(StatusReg, getKillRegState(StatusDead))
415 .addMBB(LoadCmpBB);
416 BuildMI(StoreBB, MIMD, TII->get(AArch64::B)).addMBB(DoneBB);
417 StoreBB->addSuccessor(LoadCmpBB);
418 StoreBB->addSuccessor(DoneBB);
419
420 // .Lfail:
421 // stlxp wStatus, xDestLo, xDestHi, [xAddr]
422 // cbnz wStatus, .Lloadcmp
423 BuildMI(FailBB, MIMD, TII->get(StxpOp), StatusReg)
424 .addReg(Dest0.getReg())
425 .addReg(Dest1.getReg())
426 .addReg(AddrReg);
427 BuildMI(FailBB, MIMD, TII->get(AArch64::CBNZW))
428 .addReg(StatusReg, getKillRegState(StatusDead))
429 .addMBB(LoadCmpBB);
430 FailBB->addSuccessor(LoadCmpBB);
431 FailBB->addSuccessor(DoneBB);
432
433 DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
434 DoneBB->transferSuccessors(&MBB);
435
436 MBB.addSuccessor(LoadCmpBB);
437
438 NextMBBI = MBB.end();
439 MI.eraseFromParent();
440
441 // Recompute liveness bottom up.
442 LivePhysRegs LiveRegs;
443 computeAndAddLiveIns(LiveRegs, *DoneBB);
444 computeAndAddLiveIns(LiveRegs, *FailBB);
445 computeAndAddLiveIns(LiveRegs, *StoreBB);
446 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
447
448 // Do an extra pass in the loop to get the loop carried dependencies right.
449 FailBB->clearLiveIns();
450 computeAndAddLiveIns(LiveRegs, *FailBB);
451 StoreBB->clearLiveIns();
452 computeAndAddLiveIns(LiveRegs, *StoreBB);
453 LoadCmpBB->clearLiveIns();
454 computeAndAddLiveIns(LiveRegs, *LoadCmpBB);
455
456 return true;
457}
458
459/// \brief Expand Pseudos to Instructions with destructive operands.
460///
461/// This mechanism uses MOVPRFX instructions for zeroing the false lanes
462/// or for fixing relaxed register allocation conditions to comply with
463/// the instructions register constraints. The latter case may be cheaper
464/// than setting the register constraints in the register allocator,
465/// since that will insert regular MOV instructions rather than MOVPRFX.
466///
467/// Example (after register allocation):
468///
469/// FSUB_ZPZZ_ZERO_B Z0, Pg, Z1, Z0
470///
471/// * The Pseudo FSUB_ZPZZ_ZERO_B maps to FSUB_ZPmZ_B.
472/// * We cannot map directly to FSUB_ZPmZ_B because the register
473/// constraints of the instruction are not met.
474/// * Also the _ZERO specifies the false lanes need to be zeroed.
475///
476/// We first try to see if the destructive operand == result operand,
477/// if not, we try to swap the operands, e.g.
478///
479/// FSUB_ZPmZ_B Z0, Pg/m, Z0, Z1
480///
481/// But because FSUB_ZPmZ is not commutative, this is semantically
482/// different, so we need a reverse instruction:
483///
484/// FSUBR_ZPmZ_B Z0, Pg/m, Z0, Z1
485///
486/// Then we implement the zeroing of the false lanes of Z0 by adding
487/// a zeroing MOVPRFX instruction:
488///
489/// MOVPRFX_ZPzZ_B Z0, Pg/z, Z0
490/// FSUBR_ZPmZ_B Z0, Pg/m, Z0, Z1
491///
492/// Note that this can only be done for _ZERO or _UNDEF variants where
493/// we can guarantee the false lanes to be zeroed (by implementing this)
494/// or that they are undef (don't care / not used), otherwise the
495/// swapping of operands is illegal because the operation is not
496/// (or cannot be emulated to be) fully commutative.
497bool AArch64ExpandPseudoImpl::expand_DestructiveOp(
498 MachineInstr &MI, MachineBasicBlock &MBB,
500 unsigned Opcode = AArch64::getSVEPseudoMap(MI.getOpcode());
501 uint64_t DType = TII->get(Opcode).TSFlags & AArch64::DestructiveInstTypeMask;
502 uint64_t FalseLanes = MI.getDesc().TSFlags & AArch64::FalseLanesMask;
503 bool FalseZero = FalseLanes == AArch64::FalseLanesZero;
504 Register DstReg = MI.getOperand(0).getReg();
505 bool DstIsDead = MI.getOperand(0).isDead();
506 bool UseRev = false;
507 unsigned PredIdx, DOPIdx, SrcIdx, Src2Idx;
508
509 switch (DType) {
512 if (DstReg == MI.getOperand(3).getReg()) {
513 // FSUB Zd, Pg, Zs1, Zd ==> FSUBR Zd, Pg/m, Zd, Zs1
514 std::tie(PredIdx, DOPIdx, SrcIdx) = std::make_tuple(1, 3, 2);
515 UseRev = true;
516 break;
517 }
518 [[fallthrough]];
521 std::tie(PredIdx, DOPIdx, SrcIdx) = std::make_tuple(1, 2, 3);
522 break;
524 std::tie(PredIdx, DOPIdx, SrcIdx) = std::make_tuple(2, 3, 3);
525 break;
527 std::tie(PredIdx, DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 2, 3, 4);
528 if (DstReg == MI.getOperand(3).getReg()) {
529 // FMLA Zd, Pg, Za, Zd, Zm ==> FMAD Zdn, Pg, Zm, Za
530 std::tie(PredIdx, DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 3, 4, 2);
531 UseRev = true;
532 } else if (DstReg == MI.getOperand(4).getReg()) {
533 // FMLA Zd, Pg, Za, Zm, Zd ==> FMAD Zdn, Pg, Zm, Za
534 std::tie(PredIdx, DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 4, 3, 2);
535 UseRev = true;
536 }
537 break;
539 // EXT_ZZI_CONSTRUCTIVE Zd, Zs, Imm
540 // ==> MOVPRFX Zd Zs; EXT_ZZI Zd, Zd, Zs, Imm
541 std::tie(DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 1, 2);
542 break;
544 std::tie(DOPIdx, SrcIdx) = std::make_tuple(1, 2);
545 break;
547 std::tie(DOPIdx, SrcIdx, Src2Idx) = std::make_tuple(1, 2, 3);
548 break;
549 default:
550 llvm_unreachable("Unsupported Destructive Operand type");
551 }
552
553 // MOVPRFX can only be used if the destination operand
554 // is the destructive operand, not as any other operand,
555 // so the Destructive Operand must be unique.
556 bool DOPRegIsUnique = false;
557 switch (DType) {
559 DOPRegIsUnique = DstReg != MI.getOperand(SrcIdx).getReg();
560 break;
563 DOPRegIsUnique =
564 DstReg != MI.getOperand(DOPIdx).getReg() ||
565 MI.getOperand(DOPIdx).getReg() != MI.getOperand(SrcIdx).getReg();
566 break;
572 DOPRegIsUnique = true;
573 break;
575 DOPRegIsUnique =
576 DstReg != MI.getOperand(DOPIdx).getReg() ||
577 (MI.getOperand(DOPIdx).getReg() != MI.getOperand(SrcIdx).getReg() &&
578 MI.getOperand(DOPIdx).getReg() != MI.getOperand(Src2Idx).getReg());
579 break;
580 }
581
582 // Resolve the reverse opcode
583 if (UseRev) {
584 int NewOpcode;
585 // e.g. DIV -> DIVR
586 if ((NewOpcode = AArch64::getSVERevInstr(Opcode)) != -1)
587 Opcode = NewOpcode;
588 // e.g. DIVR -> DIV
589 else if ((NewOpcode = AArch64::getSVENonRevInstr(Opcode)) != -1)
590 Opcode = NewOpcode;
591 }
592
593 // Get the right MOVPRFX
594 uint64_t ElementSize = TII->getElementSizeForOpcode(Opcode);
595 unsigned MovPrfx, LSLZero, MovPrfxZero;
596 switch (ElementSize) {
599 MovPrfx = AArch64::MOVPRFX_ZZ;
600 LSLZero = AArch64::LSL_ZPmI_B;
601 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_B;
602 break;
604 MovPrfx = AArch64::MOVPRFX_ZZ;
605 LSLZero = AArch64::LSL_ZPmI_H;
606 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_H;
607 break;
609 MovPrfx = AArch64::MOVPRFX_ZZ;
610 LSLZero = AArch64::LSL_ZPmI_S;
611 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_S;
612 break;
614 MovPrfx = AArch64::MOVPRFX_ZZ;
615 LSLZero = AArch64::LSL_ZPmI_D;
616 MovPrfxZero = AArch64::MOVPRFX_ZPzZ_D;
617 break;
618 default:
619 llvm_unreachable("Unsupported ElementSize");
620 }
621
622 // Preserve undef state until DOP's reg is defined.
623 RegState DOPRegState = getUndefRegState(MI.getOperand(DOPIdx).isUndef());
624
625 //
626 // Create the destructive operation (if required)
627 //
628 MachineInstrBuilder PRFX, DOP;
629 if (FalseZero) {
630 // If we cannot prefix the requested instruction we'll instead emit a
631 // prefixed_zeroing_mov for DestructiveBinary.
632 assert((DOPRegIsUnique || DType == AArch64::DestructiveBinary ||
635 "The destructive operand should be unique");
636 assert(ElementSize != AArch64::ElementSizeNone &&
637 "This instruction is unpredicated");
638
639 // Merge source operand into destination register
640 PRFX = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(MovPrfxZero))
641 .addReg(DstReg, RegState::Define)
642 .addReg(MI.getOperand(PredIdx).getReg())
643 .addReg(MI.getOperand(DOPIdx).getReg(), DOPRegState);
644
645 // After the movprfx, the destructive operand is same as Dst
646 DOPIdx = 0;
647 DOPRegState = {};
648
649 // Create the additional LSL to zero the lanes when the DstReg is not
650 // unique. Zeros the lanes in z0 that aren't active in p0 with sequence
651 // movprfx z0.b, p0/z, z0.b; lsl z0.b, p0/m, z0.b, #0;
652 if ((DType == AArch64::DestructiveBinary ||
655 !DOPRegIsUnique) {
656 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(LSLZero))
657 .addReg(DstReg, RegState::Define)
658 .add(MI.getOperand(PredIdx))
659 .addReg(DstReg)
660 .addImm(0);
661 }
662 } else if (DstReg != MI.getOperand(DOPIdx).getReg()) {
663 assert(DOPRegIsUnique && "The destructive operand should be unique");
664 PRFX = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(MovPrfx))
665 .addReg(DstReg, RegState::Define)
666 .addReg(MI.getOperand(DOPIdx).getReg(), DOPRegState);
667 DOPIdx = 0;
668 DOPRegState = {};
669 }
670
671 //
672 // Create the destructive operation
673 //
674 DOP = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opcode))
675 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead));
676 DOPRegState = DOPRegState | RegState::Kill;
677
678 switch (DType) {
680 DOP.addReg(MI.getOperand(DOPIdx).getReg(), DOPRegState)
681 .add(MI.getOperand(PredIdx))
682 .add(MI.getOperand(SrcIdx));
683 break;
688 DOP.add(MI.getOperand(PredIdx))
689 .addReg(MI.getOperand(DOPIdx).getReg(), DOPRegState)
690 .add(MI.getOperand(SrcIdx));
691 break;
693 DOP.add(MI.getOperand(PredIdx))
694 .addReg(MI.getOperand(DOPIdx).getReg(), DOPRegState)
695 .add(MI.getOperand(SrcIdx))
696 .add(MI.getOperand(Src2Idx));
697 break;
699 DOP.addReg(MI.getOperand(DOPIdx).getReg(), DOPRegState)
700 .add(MI.getOperand(SrcIdx));
701 break;
704 DOP.addReg(MI.getOperand(DOPIdx).getReg(), DOPRegState)
705 .add(MI.getOperand(SrcIdx))
706 .add(MI.getOperand(Src2Idx));
707 break;
708 }
709
710 if (PRFX) {
711 transferImpOps(MI, PRFX, DOP);
713 } else
714 transferImpOps(MI, DOP, DOP);
715
716 MI.eraseFromParent();
717 return true;
718}
719
720bool AArch64ExpandPseudoImpl::expandSVEBitwisePseudo(
721 MachineInstr &MI, MachineBasicBlock &MBB,
723 MachineInstrBuilder PRFX, DOP;
724 const unsigned Opcode = MI.getOpcode();
725 const MachineOperand &Op0 = MI.getOperand(0);
726 const MachineOperand *Op1 = &MI.getOperand(1);
727 const MachineOperand *Op2 = &MI.getOperand(2);
728 const Register DOPReg = Op0.getReg();
729
730 if (DOPReg == Op2->getReg()) {
731 // Commute the operands to allow destroying the second source.
732 std::swap(Op1, Op2);
733 } else if (DOPReg != Op1->getReg()) {
734 // If not in destructive form, emit a MOVPRFX. The input should only be
735 // killed if unused by the subsequent instruction.
736 PRFX = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVPRFX_ZZ))
738 .addReg(Op1->getReg(),
740 getUndefRegState(Op1->isUndef()) |
741 getKillRegState(Op1->isKill() &&
742 Opcode == AArch64::NAND_ZZZ));
743 }
744
745 assert((DOPReg == Op1->getReg() || PRFX) && "invalid expansion");
746
747 const RegState DOPRegState = getRenamableRegState(Op0.isRenamable()) |
748 getUndefRegState(!PRFX && Op1->isUndef()) |
749 RegState::Kill;
750
751 switch (Opcode) {
752 default:
753 llvm_unreachable("unhandled opcode");
754 case AArch64::EON_ZZZ:
755 DOP = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::BSL2N_ZZZZ))
756 .add(Op0)
757 .addReg(DOPReg, DOPRegState)
758 .add(*Op1)
759 .add(*Op2);
760 break;
761 case AArch64::NAND_ZZZ:
762 DOP = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::NBSL_ZZZZ))
763 .add(Op0)
764 .addReg(DOPReg, DOPRegState)
765 .add(*Op2)
766 .add(*Op2);
767 break;
768 case AArch64::NOR_ZZZ:
769 DOP = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::NBSL_ZZZZ))
770 .add(Op0)
771 .addReg(DOPReg, DOPRegState)
772 .add(*Op2)
773 .add(*Op1);
774 break;
775 }
776
777 if (PRFX) {
778 transferImpOps(MI, PRFX, DOP);
780 } else {
781 transferImpOps(MI, DOP, DOP);
782 }
783
784 MI.eraseFromParent();
785 return true;
786}
787
788bool AArch64ExpandPseudoImpl::expandSetTagLoop(
789 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
790 MachineBasicBlock::iterator &NextMBBI) {
791 MachineInstr &MI = *MBBI;
792 DebugLoc DL = MI.getDebugLoc();
793 Register SizeReg = MI.getOperand(0).getReg();
794 Register AddressReg = MI.getOperand(1).getReg();
795
797
798 bool ZeroData = MI.getOpcode() == AArch64::STZGloop_wback;
799 const unsigned OpCode1 =
800 ZeroData ? AArch64::STZGPostIndex : AArch64::STGPostIndex;
801 const unsigned OpCode2 =
802 ZeroData ? AArch64::STZ2GPostIndex : AArch64::ST2GPostIndex;
803
804 unsigned Size = MI.getOperand(2).getImm();
805 assert(Size > 0 && Size % 16 == 0);
806 if (Size % (16 * 2) != 0) {
807 BuildMI(MBB, MBBI, DL, TII->get(OpCode1), AddressReg)
808 .addReg(AddressReg)
809 .addReg(AddressReg)
810 .addImm(1);
811 Size -= 16;
812 }
814 BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVi64imm), SizeReg)
815 .addImm(Size);
816 expandMOVImm(MBB, I, 64);
817
818 auto LoopBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
819 auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
820
821 MF->insert(++MBB.getIterator(), LoopBB);
822 MF->insert(++LoopBB->getIterator(), DoneBB);
823
824 BuildMI(LoopBB, DL, TII->get(OpCode2))
825 .addDef(AddressReg)
826 .addReg(AddressReg)
827 .addReg(AddressReg)
828 .addImm(2)
830 .setMIFlags(MI.getFlags());
831 BuildMI(LoopBB, DL, TII->get(AArch64::SUBSXri))
832 .addDef(SizeReg)
833 .addReg(SizeReg)
834 .addImm(16 * 2)
835 .addImm(0);
836 BuildMI(LoopBB, DL, TII->get(AArch64::Bcc))
838 .addMBB(LoopBB)
839 .addReg(AArch64::NZCV, RegState::Implicit | RegState::Kill);
840
841 LoopBB->addSuccessor(LoopBB);
842 LoopBB->addSuccessor(DoneBB);
843
844 DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
845 DoneBB->transferSuccessors(&MBB);
846
847 MBB.addSuccessor(LoopBB);
848
849 NextMBBI = MBB.end();
850 MI.eraseFromParent();
851 // Recompute liveness bottom up.
852 LivePhysRegs LiveRegs;
853 computeAndAddLiveIns(LiveRegs, *DoneBB);
854 computeAndAddLiveIns(LiveRegs, *LoopBB);
855 // Do an extra pass in the loop to get the loop carried dependencies right.
856 // FIXME: is this necessary?
857 LoopBB->clearLiveIns();
858 computeAndAddLiveIns(LiveRegs, *LoopBB);
859 DoneBB->clearLiveIns();
860 computeAndAddLiveIns(LiveRegs, *DoneBB);
861
862 return true;
863}
864
865bool AArch64ExpandPseudoImpl::expandSVESpillFill(
866 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned Opc,
867 unsigned N) {
868 assert((Opc == AArch64::LDR_ZXI || Opc == AArch64::STR_ZXI ||
869 Opc == AArch64::LDR_PXI || Opc == AArch64::STR_PXI) &&
870 "Unexpected opcode");
871 RegState RState =
872 getDefRegState(Opc == AArch64::LDR_ZXI || Opc == AArch64::LDR_PXI);
873 unsigned sub0 = (Opc == AArch64::LDR_ZXI || Opc == AArch64::STR_ZXI)
874 ? AArch64::zsub0
875 : AArch64::psub0;
876 const TargetRegisterInfo *TRI =
878 MachineInstr &MI = *MBBI;
879 for (unsigned Offset = 0; Offset < N; ++Offset) {
880 int ImmOffset = MI.getOperand(2).getImm() + Offset;
881 bool Kill = (Offset + 1 == N) ? MI.getOperand(1).isKill() : false;
882 assert(ImmOffset >= -256 && ImmOffset < 256 &&
883 "Immediate spill offset out of range");
884 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc))
885 .addReg(TRI->getSubReg(MI.getOperand(0).getReg(), sub0 + Offset),
886 RState)
887 .addReg(MI.getOperand(1).getReg(), getKillRegState(Kill))
888 .addImm(ImmOffset);
889 }
891 return true;
892}
893
894// Create a call with the passed opcode and explicit operands, copying over all
895// the implicit operands from *MBBI, starting at the regmask.
898 const AArch64InstrInfo *TII,
899 unsigned Opcode,
900 ArrayRef<MachineOperand> ExplicitOps,
901 unsigned RegMaskStartIdx) {
902 // Be careful not to duplicate the LR def which the original instruction
903 // already carries.
904 MachineFunction &MF = *MBB.getParent();
906 MF.CreateMachineInstr(TII->get(Opcode), MBBI->getDebugLoc(),
907 /*NoImplicit=*/true);
908 MBB.insert(MBBI, Call);
909 MachineInstrBuilder(MF, Call).add(ExplicitOps);
910
911 // Register arguments are added during ISel, but cannot be added as explicit
912 // operands of the branch as it expects to be B <target> which is only one
913 // operand. Instead they are implicit operands used by the branch.
914 while (!MBBI->getOperand(RegMaskStartIdx).isRegMask()) {
915 const MachineOperand &MOP = MBBI->getOperand(RegMaskStartIdx);
916 assert(MOP.isReg() && "can only add register operands");
918 MOP.getReg(), /*Def=*/false, /*Implicit=*/true, /*isKill=*/false,
919 /*isDead=*/false, /*isUndef=*/MOP.isUndef()));
920 RegMaskStartIdx++;
921 }
922 for (const MachineOperand &MO :
923 llvm::drop_begin(MBBI->operands(), RegMaskStartIdx))
924 Call->addOperand(MO);
925
926 return Call;
927}
928
929// Create a call to CallTarget, copying over all the operands from *MBBI,
930// starting at the regmask.
933 const AArch64InstrInfo *TII,
934 MachineOperand &CallTarget,
935 unsigned RegMaskStartIdx) {
936 unsigned Opc = CallTarget.isGlobal() ? AArch64::BL : AArch64::BLR;
937
938 assert((CallTarget.isGlobal() || CallTarget.isReg()) &&
939 "invalid operand for regular call");
940 return createCallWithOps(MBB, MBBI, TII, Opc, CallTarget, RegMaskStartIdx);
941}
942
943bool AArch64ExpandPseudoImpl::expandCALL_RVMARKER(
944 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) {
945 // Expand CALL_RVMARKER pseudo to:
946 // - a branch to the call target, followed by
947 // - the special `mov x29, x29` marker, if necessary, and
948 // - another branch, to the runtime function
949 // Mark the sequence as bundle, to avoid passes moving other code in between.
950 MachineInstr &MI = *MBBI;
951 MachineOperand &RVTarget = MI.getOperand(0);
952 bool DoEmitMarker = MI.getOperand(1).getImm();
953 assert(RVTarget.isGlobal() && "invalid operand for attached call");
954
955 MachineInstr *OriginalCall = nullptr;
956
957 if (MI.getOpcode() == AArch64::BLRA_RVMARKER) {
958 // ptrauth call.
959 const MachineOperand &CallTarget = MI.getOperand(2);
960 const MachineOperand &Key = MI.getOperand(3);
961 const MachineOperand &IntDisc = MI.getOperand(4);
962 const MachineOperand &AddrDisc = MI.getOperand(5);
963
964 assert((Key.getImm() == AArch64PACKey::IA ||
965 Key.getImm() == AArch64PACKey::IB) &&
966 "Invalid auth call key");
967
968 MachineOperand Ops[] = {CallTarget, Key, IntDisc, AddrDisc};
969
970 OriginalCall = createCallWithOps(MBB, MBBI, TII, AArch64::BLRA, Ops,
971 /*RegMaskStartIdx=*/6);
972 } else {
973 assert(MI.getOpcode() == AArch64::BLR_RVMARKER && "unknown rvmarker MI");
974 OriginalCall = createCall(MBB, MBBI, TII, MI.getOperand(2),
975 // Regmask starts after the RV and call targets.
976 /*RegMaskStartIdx=*/3);
977 }
978
979 if (DoEmitMarker)
980 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ORRXrs))
981 .addReg(AArch64::FP, RegState::Define)
982 .addReg(AArch64::XZR)
983 .addReg(AArch64::FP)
984 .addImm(0);
985
986 auto *RVCall = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::BL))
987 .add(RVTarget)
988 .getInstr();
989
990 if (MI.shouldUpdateAdditionalCallInfo())
991 MBB.getParent()->moveAdditionalCallInfo(&MI, OriginalCall);
992
993 MI.eraseFromParent();
994 finalizeBundle(MBB, OriginalCall->getIterator(),
995 std::next(RVCall->getIterator()));
996 return true;
997}
998
999bool AArch64ExpandPseudoImpl::expandCALL_BTI(MachineBasicBlock &MBB,
1001 // Expand CALL_BTI pseudo to:
1002 // - a branch to the call target
1003 // - a BTI instruction
1004 // Mark the sequence as a bundle, to avoid passes moving other code in
1005 // between.
1006 MachineInstr &MI = *MBBI;
1007 MachineInstr *Call = createCall(MBB, MBBI, TII, MI.getOperand(0),
1008 // Regmask starts after the call target.
1009 /*RegMaskStartIdx=*/1);
1010
1011 Call->setCFIType(*MBB.getParent(), MI.getCFIType());
1012
1013 MachineInstr *BTI =
1014 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::HINT))
1015 // BTI J so that setjmp can to BR to this.
1016 .addImm(36)
1017 .getInstr();
1018
1019 if (MI.shouldUpdateAdditionalCallInfo())
1021
1022 MI.eraseFromParent();
1023 finalizeBundle(MBB, Call->getIterator(), std::next(BTI->getIterator()));
1024 return true;
1025}
1026
1027bool AArch64ExpandPseudoImpl::expandStoreSwiftAsyncContext(
1028 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) {
1029 Register CtxReg = MBBI->getOperand(0).getReg();
1030 Register BaseReg = MBBI->getOperand(1).getReg();
1031 int Offset = MBBI->getOperand(2).getImm();
1032 DebugLoc DL(MBBI->getDebugLoc());
1033 auto &STI = MBB.getParent()->getSubtarget<AArch64Subtarget>();
1034
1035 if (STI.getTargetTriple().getArchName() != "arm64e") {
1036 BuildMI(MBB, MBBI, DL, TII->get(AArch64::STRXui))
1037 .addUse(CtxReg)
1038 .addUse(BaseReg)
1039 .addImm(Offset / 8)
1042 return true;
1043 }
1044
1045 // We need to sign the context in an address-discriminated way. 0xc31a is a
1046 // fixed random value, chosen as part of the ABI.
1047 // add x16, xBase, #Offset
1048 // movk x16, #0xc31a, lsl #48
1049 // mov x17, x22/xzr
1050 // pacdb x17, x16
1051 // str x17, [xBase, #Offset]
1052 unsigned Opc = Offset >= 0 ? AArch64::ADDXri : AArch64::SUBXri;
1053 BuildMI(MBB, MBBI, DL, TII->get(Opc), AArch64::X16)
1054 .addUse(BaseReg)
1055 .addImm(abs(Offset))
1056 .addImm(0)
1058 BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVKXi), AArch64::X16)
1059 .addUse(AArch64::X16)
1060 .addImm(0xc31a)
1061 .addImm(48)
1063 // We're not allowed to clobber X22 (and couldn't clobber XZR if we tried), so
1064 // move it somewhere before signing.
1065 BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::X17)
1066 .addUse(AArch64::XZR)
1067 .addUse(CtxReg)
1068 .addImm(0)
1070 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACDB), AArch64::X17)
1071 .addUse(AArch64::X17)
1072 .addUse(AArch64::X16)
1074 BuildMI(MBB, MBBI, DL, TII->get(AArch64::STRXui))
1075 .addUse(AArch64::X17)
1076 .addUse(BaseReg)
1077 .addImm(Offset / 8)
1079
1081 return true;
1082}
1083
1084AArch64ExpandPseudoImpl::ConditionalBlocks
1085AArch64ExpandPseudoImpl::expandConditionalPseudo(
1086 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, DebugLoc DL,
1087 MachineInstrBuilder &Branch) {
1088 assert((std::next(MBBI) != MBB.end() ||
1089 MBB.successors().begin() != MBB.successors().end()) &&
1090 "Unexpected unreachable in block");
1091
1092 // Split MBB and create two new blocks:
1093 // - MBB now contains all instructions before the conditional pseudo.
1094 // - CondBB contains the conditional pseudo instruction only.
1095 // - EndBB contains all instructions after the conditional pseudo.
1096 MachineInstr &PrevMI = *std::prev(MBBI);
1097 MachineBasicBlock *CondBB = MBB.splitAt(PrevMI, /*UpdateLiveIns*/ true);
1098 MachineBasicBlock *EndBB =
1099 std::next(MBBI) == CondBB->end()
1100 ? *CondBB->successors().begin()
1101 : CondBB->splitAt(*MBBI, /*UpdateLiveIns*/ true);
1102
1103 // Add the SMBB label to the branch instruction & create a branch to EndBB.
1104 Branch.addMBB(CondBB);
1105 BuildMI(&MBB, DL, TII->get(AArch64::B))
1106 .addMBB(EndBB);
1107 MBB.addSuccessor(EndBB);
1108
1109 // Create branch from CondBB to EndBB. Users of this helper should insert new
1110 // instructions at CondBB.back() -- i.e. before the branch.
1111 BuildMI(CondBB, DL, TII->get(AArch64::B)).addMBB(EndBB);
1112 return {*CondBB, *EndBB};
1113}
1114
1115MachineBasicBlock *
1116AArch64ExpandPseudoImpl::expandRestoreZASave(MachineBasicBlock &MBB,
1118 MachineInstr &MI = *MBBI;
1119 DebugLoc DL = MI.getDebugLoc();
1120
1121 // Compare TPIDR2_EL0 against 0. Restore ZA if TPIDR2_EL0 is zero.
1122 MachineInstrBuilder Branch =
1123 BuildMI(MBB, MBBI, DL, TII->get(AArch64::CBZX)).add(MI.getOperand(0));
1124
1125 auto [CondBB, EndBB] = expandConditionalPseudo(MBB, MBBI, DL, Branch);
1126 // Replace the pseudo with a call (BL).
1127 MachineInstrBuilder MIB =
1128 BuildMI(CondBB, CondBB.back(), DL, TII->get(AArch64::BL));
1129 // Copy operands (mainly the regmask) from the pseudo.
1130 for (unsigned I = 2; I < MI.getNumOperands(); ++I)
1131 MIB.add(MI.getOperand(I));
1132 // Mark the TPIDR2 block pointer (X0) as an implicit use.
1133 MIB.addReg(MI.getOperand(1).getReg(), RegState::Implicit);
1134
1135 MI.eraseFromParent();
1136 return &EndBB;
1137}
1138
1139static constexpr unsigned ZERO_ALL_ZA_MASK = 0b11111111;
1140
1142AArch64ExpandPseudoImpl::expandCommitZASave(MachineBasicBlock &MBB,
1144 MachineInstr &MI = *MBBI;
1145 DebugLoc DL = MI.getDebugLoc();
1146 [[maybe_unused]] auto *RI = MBB.getParent()->getSubtarget().getRegisterInfo();
1147
1148 // Compare TPIDR2_EL0 against 0. Commit ZA if TPIDR2_EL0 is non-zero.
1149 MachineInstrBuilder Branch =
1150 BuildMI(MBB, MBBI, DL, TII->get(AArch64::CBNZX)).add(MI.getOperand(0));
1151
1152 auto [CondBB, EndBB] = expandConditionalPseudo(MBB, MBBI, DL, Branch);
1153 // Replace the pseudo with a call (BL).
1155 BuildMI(CondBB, CondBB.back(), DL, TII->get(AArch64::BL));
1156 // Copy operands (mainly the regmask) from the pseudo.
1157 for (unsigned I = 3; I < MI.getNumOperands(); ++I)
1158 MIB.add(MI.getOperand(I));
1159 // Clear TPIDR2_EL0.
1160 BuildMI(CondBB, CondBB.back(), DL, TII->get(AArch64::MSR))
1161 .addImm(AArch64SysReg::TPIDR2_EL0)
1162 .addReg(AArch64::XZR);
1163 bool ZeroZA = MI.getOperand(1).getImm() != 0;
1164 bool ZeroZT0 = MI.getOperand(2).getImm() != 0;
1165 if (ZeroZA) {
1166 assert(MI.definesRegister(AArch64::ZAB0, RI) && "should define ZA!");
1167 BuildMI(CondBB, CondBB.back(), DL, TII->get(AArch64::ZERO_M))
1169 .addDef(AArch64::ZAB0, RegState::ImplicitDefine);
1170 }
1171 if (ZeroZT0) {
1172 assert(MI.definesRegister(AArch64::ZT0, RI) && "should define ZT0!");
1173 BuildMI(CondBB, CondBB.back(), DL, TII->get(AArch64::ZERO_T))
1174 .addDef(AArch64::ZT0);
1175 }
1176
1177 MI.eraseFromParent();
1178 return &EndBB;
1179}
1180
1181MachineBasicBlock *
1182AArch64ExpandPseudoImpl::expandCondSMToggle(MachineBasicBlock &MBB,
1184 MachineInstr &MI = *MBBI;
1185 // In the case of a smstart/smstop before a unreachable, just remove the pseudo.
1186 // Exception handling code generated by Clang may introduce unreachables and it
1187 // seems unnecessary to restore pstate.sm when that happens. Note that it is
1188 // not just an optimisation, the code below expects a successor instruction/block
1189 // in order to split the block at MBBI.
1190 if (std::next(MBBI) == MBB.end() &&
1191 MI.getParent()->successors().begin() ==
1192 MI.getParent()->successors().end()) {
1193 MI.eraseFromParent();
1194 return &MBB;
1195 }
1196
1197 // Expand the pseudo into smstart or smstop instruction. The pseudo has the
1198 // following operands:
1199 //
1200 // MSRpstatePseudo <za|sm|both>, <0|1>, condition[, pstate.sm], <regmask>
1201 //
1202 // The pseudo is expanded into a conditional smstart/smstop, with a
1203 // check if pstate.sm (register) equals the expected value, and if not,
1204 // invokes the smstart/smstop.
1205 //
1206 // As an example, the following block contains a normal call from a
1207 // streaming-compatible function:
1208 //
1209 // OrigBB:
1210 // MSRpstatePseudo 3, 0, IfCallerIsStreaming, %0, <regmask> <- Cond SMSTOP
1211 // bl @normal_callee
1212 // MSRpstatePseudo 3, 1, IfCallerIsStreaming, %0, <regmask> <- Cond SMSTART
1213 //
1214 // ...which will be transformed into:
1215 //
1216 // OrigBB:
1217 // TBNZx %0:gpr64, 0, SMBB
1218 // b EndBB
1219 //
1220 // SMBB:
1221 // MSRpstatesvcrImm1 3, 0, <regmask> <- SMSTOP
1222 //
1223 // EndBB:
1224 // bl @normal_callee
1225 // MSRcond_pstatesvcrImm1 3, 1, <regmask> <- SMSTART
1226 //
1227 DebugLoc DL = MI.getDebugLoc();
1228
1229 // Create the conditional branch based on the third operand of the
1230 // instruction, which tells us if we are wrapping a normal or streaming
1231 // function.
1232 // We test the live value of pstate.sm and toggle pstate.sm if this is not the
1233 // expected value for the callee (0 for a normal callee and 1 for a streaming
1234 // callee).
1235 unsigned Opc;
1236 switch (MI.getOperand(2).getImm()) {
1237 case AArch64SME::Always:
1238 llvm_unreachable("Should have matched to instruction directly");
1240 Opc = AArch64::TBNZW;
1241 break;
1243 Opc = AArch64::TBZW;
1244 break;
1245 }
1246 auto PStateSM = MI.getOperand(3).getReg();
1248 unsigned SMReg32 = TRI->getSubReg(PStateSM, AArch64::sub_32);
1249 MachineInstrBuilder Tbx =
1250 BuildMI(MBB, MBBI, DL, TII->get(Opc)).addReg(SMReg32).addImm(0);
1251
1252 auto [CondBB, EndBB] = expandConditionalPseudo(MBB, MBBI, DL, Tbx);
1253 // Create the SMSTART/SMSTOP (MSRpstatesvcrImm1) instruction in SMBB.
1254 MachineInstrBuilder MIB = BuildMI(CondBB, CondBB.back(), MI.getDebugLoc(),
1255 TII->get(AArch64::MSRpstatesvcrImm1));
1256 // Copy all but the second and third operands of MSRcond_pstatesvcrImm1 (as
1257 // these contain the CopyFromReg for the first argument and the flag to
1258 // indicate whether the callee is streaming or normal).
1259 MIB.add(MI.getOperand(0));
1260 MIB.add(MI.getOperand(1));
1261 for (unsigned i = 4; i < MI.getNumOperands(); ++i)
1262 MIB.add(MI.getOperand(i));
1263
1264 MI.eraseFromParent();
1265 return &EndBB;
1266}
1267
1268bool AArch64ExpandPseudoImpl::expandMultiVecPseudo(
1269 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
1270 const TargetRegisterClass &ContiguousClass,
1271 const TargetRegisterClass &StridedClass, unsigned ContiguousOp,
1272 unsigned StridedOpc) {
1273 MachineInstr &MI = *MBBI;
1274 Register Tuple = MI.getOperand(0).getReg();
1275
1276 auto ContiguousRange = ContiguousClass.getRegisters();
1277 auto StridedRange = StridedClass.getRegisters();
1278 unsigned Opc;
1279 if (llvm::is_contained(ContiguousRange, Tuple.asMCReg())) {
1280 Opc = ContiguousOp;
1281 } else if (llvm::is_contained(StridedRange, Tuple.asMCReg())) {
1282 Opc = StridedOpc;
1283 } else
1284 llvm_unreachable("Cannot expand Multi-Vector pseudo");
1285
1286 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc))
1287 .add(MI.getOperand(0))
1288 .add(MI.getOperand(1))
1289 .add(MI.getOperand(2))
1290 .add(MI.getOperand(3));
1291 transferImpOps(MI, MIB, MIB);
1292 MI.eraseFromParent();
1293 return true;
1294}
1295
1296bool AArch64ExpandPseudoImpl::expandCopyIntoTuplePseudo(
1297 MachineInstr &MI, MachineBasicBlock &MBB,
1299 Register Src = MI.getOperand(1).getReg();
1300 Register Dest = MI.getOperand(0).getReg();
1301
1302 if (Src != Dest)
1303 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ORR_ZZZ))
1304 .addReg(Dest, RegState::Define)
1305 .addReg(Src)
1306 .addReg(Src);
1307
1308 MI.eraseFromParent();
1309 return true;
1310}
1311
1312/// If MBBI references a pseudo instruction that should be expanded here,
1313/// do the expansion and return true. Otherwise return false.
1314bool AArch64ExpandPseudoImpl::expandMI(MachineBasicBlock &MBB,
1316 MachineBasicBlock::iterator &NextMBBI) {
1317 MachineInstr &MI = *MBBI;
1318 unsigned Opcode = MI.getOpcode();
1319
1320 // Check if we can expand the destructive op
1321 int OrigInstr = AArch64::getSVEPseudoMap(MI.getOpcode());
1322 if (OrigInstr != -1) {
1323 auto &Orig = TII->get(OrigInstr);
1324 if ((Orig.TSFlags & AArch64::DestructiveInstTypeMask) !=
1326 return expand_DestructiveOp(MI, MBB, MBBI);
1327 }
1328 }
1329
1330 switch (Opcode) {
1331 default:
1332 break;
1333
1334 case AArch64::BSPv8i8:
1335 case AArch64::BSPv16i8: {
1336 Register DstReg = MI.getOperand(0).getReg();
1337 if (DstReg == MI.getOperand(3).getReg()) {
1338 // Expand to BIT
1339 auto I = BuildMI(MBB, MBBI, MI.getDebugLoc(),
1340 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BITv8i8
1341 : AArch64::BITv16i8))
1342 .add(MI.getOperand(0))
1343 .add(MI.getOperand(3))
1344 .add(MI.getOperand(2))
1345 .add(MI.getOperand(1));
1346 transferImpOps(MI, I, I);
1347 } else if (DstReg == MI.getOperand(2).getReg()) {
1348 // Expand to BIF
1349 auto I = BuildMI(MBB, MBBI, MI.getDebugLoc(),
1350 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BIFv8i8
1351 : AArch64::BIFv16i8))
1352 .add(MI.getOperand(0))
1353 .add(MI.getOperand(2))
1354 .add(MI.getOperand(3))
1355 .add(MI.getOperand(1));
1356 transferImpOps(MI, I, I);
1357 } else {
1358 // Expand to BSL, use additional move if required
1359 if (DstReg == MI.getOperand(1).getReg()) {
1360 auto I =
1361 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1362 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BSLv8i8
1363 : AArch64::BSLv16i8))
1364 .add(MI.getOperand(0))
1365 .add(MI.getOperand(1))
1366 .add(MI.getOperand(2))
1367 .add(MI.getOperand(3));
1368 transferImpOps(MI, I, I);
1369 } else {
1371 getRenamableRegState(MI.getOperand(1).isRenamable()) |
1373 MI.getOperand(1).isKill() &&
1374 MI.getOperand(1).getReg() != MI.getOperand(2).getReg() &&
1375 MI.getOperand(1).getReg() != MI.getOperand(3).getReg());
1376 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1377 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::ORRv8i8
1378 : AArch64::ORRv16i8))
1379 .addReg(DstReg,
1380 RegState::Define |
1381 getRenamableRegState(MI.getOperand(0).isRenamable()))
1382 .addReg(MI.getOperand(1).getReg(), RegState)
1383 .addReg(MI.getOperand(1).getReg(), RegState);
1384 auto I2 =
1385 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1386 TII->get(Opcode == AArch64::BSPv8i8 ? AArch64::BSLv8i8
1387 : AArch64::BSLv16i8))
1388 .add(MI.getOperand(0))
1389 .addReg(DstReg,
1390 RegState::Kill | getRenamableRegState(
1391 MI.getOperand(0).isRenamable()))
1392 .add(MI.getOperand(2))
1393 .add(MI.getOperand(3));
1394 transferImpOps(MI, I2, I2);
1395 }
1396 }
1397 MI.eraseFromParent();
1398 return true;
1399 }
1400
1401 case AArch64::ADDWrr:
1402 case AArch64::SUBWrr:
1403 case AArch64::ADDXrr:
1404 case AArch64::SUBXrr:
1405 case AArch64::ADDSWrr:
1406 case AArch64::SUBSWrr:
1407 case AArch64::ADDSXrr:
1408 case AArch64::SUBSXrr:
1409 case AArch64::ANDWrr:
1410 case AArch64::ANDXrr:
1411 case AArch64::BICWrr:
1412 case AArch64::BICXrr:
1413 case AArch64::ANDSWrr:
1414 case AArch64::ANDSXrr:
1415 case AArch64::BICSWrr:
1416 case AArch64::BICSXrr:
1417 case AArch64::EONWrr:
1418 case AArch64::EONXrr:
1419 case AArch64::EORWrr:
1420 case AArch64::EORXrr:
1421 case AArch64::ORNWrr:
1422 case AArch64::ORNXrr:
1423 case AArch64::ORRWrr:
1424 case AArch64::ORRXrr: {
1425 unsigned Opcode;
1426 switch (MI.getOpcode()) {
1427 default:
1428 return false;
1429 case AArch64::ADDWrr: Opcode = AArch64::ADDWrs; break;
1430 case AArch64::SUBWrr: Opcode = AArch64::SUBWrs; break;
1431 case AArch64::ADDXrr: Opcode = AArch64::ADDXrs; break;
1432 case AArch64::SUBXrr: Opcode = AArch64::SUBXrs; break;
1433 case AArch64::ADDSWrr: Opcode = AArch64::ADDSWrs; break;
1434 case AArch64::SUBSWrr: Opcode = AArch64::SUBSWrs; break;
1435 case AArch64::ADDSXrr: Opcode = AArch64::ADDSXrs; break;
1436 case AArch64::SUBSXrr: Opcode = AArch64::SUBSXrs; break;
1437 case AArch64::ANDWrr: Opcode = AArch64::ANDWrs; break;
1438 case AArch64::ANDXrr: Opcode = AArch64::ANDXrs; break;
1439 case AArch64::BICWrr: Opcode = AArch64::BICWrs; break;
1440 case AArch64::BICXrr: Opcode = AArch64::BICXrs; break;
1441 case AArch64::ANDSWrr: Opcode = AArch64::ANDSWrs; break;
1442 case AArch64::ANDSXrr: Opcode = AArch64::ANDSXrs; break;
1443 case AArch64::BICSWrr: Opcode = AArch64::BICSWrs; break;
1444 case AArch64::BICSXrr: Opcode = AArch64::BICSXrs; break;
1445 case AArch64::EONWrr: Opcode = AArch64::EONWrs; break;
1446 case AArch64::EONXrr: Opcode = AArch64::EONXrs; break;
1447 case AArch64::EORWrr: Opcode = AArch64::EORWrs; break;
1448 case AArch64::EORXrr: Opcode = AArch64::EORXrs; break;
1449 case AArch64::ORNWrr: Opcode = AArch64::ORNWrs; break;
1450 case AArch64::ORNXrr: Opcode = AArch64::ORNXrs; break;
1451 case AArch64::ORRWrr: Opcode = AArch64::ORRWrs; break;
1452 case AArch64::ORRXrr: Opcode = AArch64::ORRXrs; break;
1453 }
1454 MachineFunction &MF = *MBB.getParent();
1455 // Try to create new inst without implicit operands added.
1456 MachineInstr *NewMI = MF.CreateMachineInstr(
1457 TII->get(Opcode), MI.getDebugLoc(), /*NoImplicit=*/true);
1458 MBB.insert(MBBI, NewMI);
1459 MachineInstrBuilder MIB1(MF, NewMI);
1460 MIB1->setPCSections(MF, MI.getPCSections());
1461 MIB1.addReg(MI.getOperand(0).getReg(), RegState::Define)
1462 .add(MI.getOperand(1))
1463 .add(MI.getOperand(2))
1465 transferImpOps(MI, MIB1, MIB1);
1466 if (auto DebugNumber = MI.peekDebugInstrNum())
1467 NewMI->setDebugInstrNum(DebugNumber);
1468 MI.eraseFromParent();
1469 return true;
1470 }
1471
1472 case AArch64::LOADgot: {
1474 Register DstReg = MI.getOperand(0).getReg();
1475 const MachineOperand &MO1 = MI.getOperand(1);
1476 unsigned Flags = MO1.getTargetFlags();
1477
1478 if (MF->getTarget().getCodeModel() == CodeModel::Tiny) {
1479 // Tiny codemodel expand to LDR
1480 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(),
1481 TII->get(AArch64::LDRXl), DstReg);
1482
1483 if (MO1.isGlobal()) {
1484 MIB.addGlobalAddress(MO1.getGlobal(), 0, Flags);
1485 } else if (MO1.isSymbol()) {
1486 MIB.addExternalSymbol(MO1.getSymbolName(), Flags);
1487 } else {
1488 assert(MO1.isCPI() &&
1489 "Only expect globals, externalsymbols, or constant pools");
1490 MIB.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(), Flags);
1491 }
1492 } else {
1493 // Small codemodel expand into ADRP + LDR.
1494 MachineFunction &MF = *MI.getParent()->getParent();
1495 DebugLoc DL = MI.getDebugLoc();
1496 MachineInstrBuilder MIB1 =
1497 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg);
1498
1499 MachineInstrBuilder MIB2;
1500 if (MF.getSubtarget<AArch64Subtarget>().isTargetILP32()) {
1502 unsigned Reg32 = TRI->getSubReg(DstReg, AArch64::sub_32);
1503 MIB2 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::LDRWui))
1504 .addDef(Reg32)
1505 .addReg(DstReg, RegState::Kill)
1506 .addReg(DstReg, RegState::Implicit);
1507 } else {
1508 Register DstReg = MI.getOperand(0).getReg();
1509 MIB2 = BuildMI(MBB, MBBI, DL, TII->get(AArch64::LDRXui))
1510 .add(MI.getOperand(0))
1511 .addUse(DstReg, RegState::Kill);
1512 }
1513
1514 if (MO1.isGlobal()) {
1515 MIB1.addGlobalAddress(MO1.getGlobal(), 0, Flags | AArch64II::MO_PAGE);
1516 MIB2.addGlobalAddress(MO1.getGlobal(), 0,
1518 } else if (MO1.isSymbol()) {
1520 MIB2.addExternalSymbol(MO1.getSymbolName(), Flags |
1523 } else {
1524 assert(MO1.isCPI() &&
1525 "Only expect globals, externalsymbols, or constant pools");
1526 MIB1.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(),
1527 Flags | AArch64II::MO_PAGE);
1528 MIB2.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(),
1529 Flags | AArch64II::MO_PAGEOFF |
1531 }
1532
1533 // If the LOADgot instruction has a debug-instr-number, annotate the
1534 // LDRWui instruction that it is expanded to with the same
1535 // debug-instr-number to preserve debug information.
1536 if (MI.peekDebugInstrNum() != 0)
1537 MIB2->setDebugInstrNum(MI.peekDebugInstrNum());
1538 transferImpOps(MI, MIB1, MIB2);
1539 }
1540 MI.eraseFromParent();
1541 return true;
1542 }
1543 case AArch64::MOVaddrBA:
1544 case AArch64::MOVaddr:
1545 case AArch64::MOVaddrJT:
1546 case AArch64::MOVaddrCP:
1547 case AArch64::MOVaddrTLS:
1548 case AArch64::MOVaddrEXT: {
1549 MachineFunction &MF = *MI.getParent()->getParent();
1550 Register DstReg = MI.getOperand(0).getReg();
1551 assert(DstReg != AArch64::XZR);
1552
1553 bool IsTargetMachO = MF.getSubtarget<AArch64Subtarget>().isTargetMachO();
1556 MI.getOpcode(), MI.getOperand(1).getTargetFlags(), IsTargetMachO, Insn);
1557
1558 // Compute the constant pool index, if any.
1559 std::optional<unsigned> CPIdx;
1560 if (Opcode == AArch64::MOVaddrBA && IsTargetMachO) {
1561 // blockaddress expressions have to come from a constant pool because the
1562 // largest addend (and hence offset within a function) allowed for ADRP is
1563 // only 8MB.
1564 const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
1565 assert(MI.getOperand(1).getOffset() == 0 && "unexpected offset");
1566 MachineConstantPool *MCP = MF.getConstantPool();
1567 CPIdx = MCP->getConstantPoolIndex(BA, Align(8));
1568 }
1569
1570 MachineInstrBuilder FirstMIB;
1571 MachineInstrBuilder LastMIB;
1572 for (const auto &I : Insn) {
1573 MachineInstrBuilder MIB;
1574 switch (I.Opcode) {
1575 case AArch64::ADRP:
1576 MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP),
1577 DstReg);
1578 if (CPIdx)
1580 else
1581 MIB.add(MI.getOperand(1));
1582 break;
1583 case AArch64::LDRXui:
1584 MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::LDRXui),
1585 DstReg)
1586 .addUse(DstReg)
1589 break;
1590 case AArch64::MOVKXi: {
1591 // MO_TAGGED on the page indicates a tagged address. Set the tag now.
1592 // We do so by creating a MOVK that sets bits 48-63 of the register to
1593 // (global address + 0x100000000 - PC) >> 48. This assumes that we're in
1594 // the small code model so we can assume a binary size of <= 4GB, which
1595 // makes the untagged PC relative offset positive. The binary must also
1596 // be loaded into address range [0, 2^48). Both of these properties need
1597 // to be ensured at runtime when using tagged addresses.
1598 auto Tag = MI.getOperand(1);
1599 Tag.setTargetFlags(AArch64II::MO_PREL | AArch64II::MO_G3);
1600 Tag.setOffset(0x100000000);
1601 MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi),
1602 DstReg)
1603 .addReg(DstReg)
1604 .add(Tag)
1605 .addImm(48);
1606 break;
1607 }
1608 case AArch64::ADDXri:
1609 MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDXri))
1610 .add(MI.getOperand(0))
1611 .addReg(DstReg)
1612 .add(MI.getOperand(2))
1613 .addImm(0);
1614 break;
1615 default:
1616 llvm_unreachable("unexpected opcode in MOVaddr expansion");
1617 }
1618
1619 if (!FirstMIB.getInstr())
1620 FirstMIB = MIB;
1621 LastMIB = MIB;
1622 }
1623
1624 transferImpOps(MI, FirstMIB, LastMIB);
1625 MI.eraseFromParent();
1626 return true;
1627 }
1628 case AArch64::ADDlowTLS:
1629 // Produce a plain ADD
1630 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDXri))
1631 .add(MI.getOperand(0))
1632 .add(MI.getOperand(1))
1633 .add(MI.getOperand(2))
1634 .addImm(0);
1635 MI.eraseFromParent();
1636 return true;
1637
1638 case AArch64::MOVbaseTLS: {
1639 Register DstReg = MI.getOperand(0).getReg();
1640 auto SysReg = AArch64SysReg::TPIDR_EL0;
1642 if (MF->getSubtarget<AArch64Subtarget>().useEL3ForTP())
1643 SysReg = AArch64SysReg::TPIDR_EL3;
1644 else if (MF->getSubtarget<AArch64Subtarget>().useEL2ForTP())
1645 SysReg = AArch64SysReg::TPIDR_EL2;
1646 else if (MF->getSubtarget<AArch64Subtarget>().useEL1ForTP())
1647 SysReg = AArch64SysReg::TPIDR_EL1;
1648 else if (MF->getSubtarget<AArch64Subtarget>().useROEL0ForTP())
1649 SysReg = AArch64SysReg::TPIDRRO_EL0;
1650 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MRS), DstReg)
1651 .addImm(SysReg);
1652 MI.eraseFromParent();
1653 return true;
1654 }
1655
1656 case AArch64::MOVi32imm:
1657 return expandMOVImm(MBB, MBBI, 32);
1658 case AArch64::MOVi64imm:
1659 return expandMOVImm(MBB, MBBI, 64);
1660 case AArch64::RET_ReallyLR: {
1661 // Hiding the LR use with RET_ReallyLR may lead to extra kills in the
1662 // function and missing live-ins. We are fine in practice because callee
1663 // saved register handling ensures the register value is restored before
1664 // RET, but we need the undef flag here to appease the MachineVerifier
1665 // liveness checks.
1666 MachineInstrBuilder MIB =
1667 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::RET))
1668 .addReg(AArch64::LR, RegState::Undef);
1669 transferImpOps(MI, MIB, MIB);
1670 MI.eraseFromParent();
1671 return true;
1672 }
1673 case AArch64::CMP_SWAP_8:
1674 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRB, AArch64::STLXRB,
1675 AArch64::SUBSWrx,
1677 AArch64::WZR, NextMBBI);
1678 case AArch64::CMP_SWAP_16:
1679 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRH, AArch64::STLXRH,
1680 AArch64::SUBSWrx,
1682 AArch64::WZR, NextMBBI);
1683 case AArch64::CMP_SWAP_32:
1684 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRW, AArch64::STLXRW,
1685 AArch64::SUBSWrs,
1687 AArch64::WZR, NextMBBI);
1688 case AArch64::CMP_SWAP_64:
1689 return expandCMP_SWAP(MBB, MBBI,
1690 AArch64::LDAXRX, AArch64::STLXRX, AArch64::SUBSXrs,
1692 AArch64::XZR, NextMBBI);
1693 case AArch64::CMP_SWAP_128:
1694 case AArch64::CMP_SWAP_128_RELEASE:
1695 case AArch64::CMP_SWAP_128_ACQUIRE:
1696 case AArch64::CMP_SWAP_128_MONOTONIC:
1697 return expandCMP_SWAP_128(MBB, MBBI, NextMBBI);
1698
1699 case AArch64::AESMCrrTied:
1700 case AArch64::AESIMCrrTied: {
1701 MachineInstrBuilder MIB =
1702 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1703 TII->get(Opcode == AArch64::AESMCrrTied ? AArch64::AESMCrr :
1704 AArch64::AESIMCrr))
1705 .add(MI.getOperand(0))
1706 .add(MI.getOperand(1));
1707 transferImpOps(MI, MIB, MIB);
1708 MI.eraseFromParent();
1709 return true;
1710 }
1711 case AArch64::IRGstack: {
1712 MachineFunction &MF = *MBB.getParent();
1713 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
1714 const AArch64FrameLowering *TFI =
1715 MF.getSubtarget<AArch64Subtarget>().getFrameLowering();
1716
1717 // IRG does not allow immediate offset. getTaggedBasePointerOffset should
1718 // almost always point to SP-after-prologue; if not, emit a longer
1719 // instruction sequence.
1720 int BaseOffset = -AFI->getTaggedBasePointerOffset();
1721 Register FrameReg;
1722 StackOffset FrameRegOffset = TFI->resolveFrameOffsetReference(
1723 MF, BaseOffset, false /*isFixed*/, TargetStackID::Default /*StackID*/,
1724 FrameReg,
1725 /*PreferFP=*/false,
1726 /*ForSimm=*/true);
1727 Register SrcReg = FrameReg;
1728 if (FrameRegOffset) {
1729 // Use output register as temporary.
1730 SrcReg = MI.getOperand(0).getReg();
1731 emitFrameOffset(MBB, &MI, MI.getDebugLoc(), SrcReg, FrameReg,
1732 FrameRegOffset, TII);
1733 }
1734 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::IRG))
1735 .add(MI.getOperand(0))
1736 .addUse(SrcReg)
1737 .add(MI.getOperand(2));
1738 MI.eraseFromParent();
1739 return true;
1740 }
1741 case AArch64::TAGPstack: {
1742 int64_t Offset = MI.getOperand(2).getImm();
1743 BuildMI(MBB, MBBI, MI.getDebugLoc(),
1744 TII->get(Offset >= 0 ? AArch64::ADDG : AArch64::SUBG))
1745 .add(MI.getOperand(0))
1746 .add(MI.getOperand(1))
1747 .addImm(std::abs(Offset))
1748 .add(MI.getOperand(4));
1749 MI.eraseFromParent();
1750 return true;
1751 }
1752 case AArch64::STGloop_wback:
1753 case AArch64::STZGloop_wback:
1754 return expandSetTagLoop(MBB, MBBI, NextMBBI);
1755 case AArch64::STGloop:
1756 case AArch64::STZGloop:
1758 "Non-writeback variants of STGloop / STZGloop should not "
1759 "survive past PrologEpilogInserter.");
1760 case AArch64::STR_ZZZZXI:
1761 case AArch64::STR_ZZZZXI_STRIDED_CONTIGUOUS:
1762 return expandSVESpillFill(MBB, MBBI, AArch64::STR_ZXI, 4);
1763 case AArch64::STR_ZZZXI:
1764 return expandSVESpillFill(MBB, MBBI, AArch64::STR_ZXI, 3);
1765 case AArch64::STR_ZZXI:
1766 case AArch64::STR_ZZXI_STRIDED_CONTIGUOUS:
1767 return expandSVESpillFill(MBB, MBBI, AArch64::STR_ZXI, 2);
1768 case AArch64::STR_PPXI:
1769 return expandSVESpillFill(MBB, MBBI, AArch64::STR_PXI, 2);
1770 case AArch64::LDR_ZZZZXI:
1771 case AArch64::LDR_ZZZZXI_STRIDED_CONTIGUOUS:
1772 return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 4);
1773 case AArch64::LDR_ZZZXI:
1774 return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 3);
1775 case AArch64::LDR_ZZXI:
1776 case AArch64::LDR_ZZXI_STRIDED_CONTIGUOUS:
1777 return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 2);
1778 case AArch64::LDR_PPXI:
1779 return expandSVESpillFill(MBB, MBBI, AArch64::LDR_PXI, 2);
1780 case AArch64::BLR_RVMARKER:
1781 case AArch64::BLRA_RVMARKER:
1782 return expandCALL_RVMARKER(MBB, MBBI);
1783 case AArch64::BLR_BTI:
1784 return expandCALL_BTI(MBB, MBBI);
1785 case AArch64::StoreSwiftAsyncContext:
1786 return expandStoreSwiftAsyncContext(MBB, MBBI);
1787 case AArch64::RestoreZAPseudo:
1788 case AArch64::CommitZASavePseudo:
1789 case AArch64::MSRpstatePseudo: {
1790 auto *NewMBB = [&] {
1791 switch (Opcode) {
1792 case AArch64::RestoreZAPseudo:
1793 return expandRestoreZASave(MBB, MBBI);
1794 case AArch64::CommitZASavePseudo:
1795 return expandCommitZASave(MBB, MBBI);
1796 case AArch64::MSRpstatePseudo:
1797 return expandCondSMToggle(MBB, MBBI);
1798 default:
1799 llvm_unreachable("Unexpected conditional pseudo!");
1800 }
1801 }();
1802 if (NewMBB != &MBB)
1803 NextMBBI = MBB.end(); // The NextMBBI iterator is invalidated.
1804 return true;
1805 }
1806 case AArch64::InOutZAUsePseudo:
1807 case AArch64::RequiresZASavePseudo:
1808 case AArch64::RequiresZT0SavePseudo:
1809 case AArch64::SMEStateAllocPseudo:
1810 case AArch64::COALESCER_BARRIER_FPR16:
1811 case AArch64::COALESCER_BARRIER_FPR32:
1812 case AArch64::COALESCER_BARRIER_FPR64:
1813 case AArch64::COALESCER_BARRIER_FPR128:
1814 MI.eraseFromParent();
1815 return true;
1816 case AArch64::LD1B_2Z_IMM_PSEUDO:
1817 return expandMultiVecPseudo(
1818 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1819 AArch64::LD1B_2Z_IMM, AArch64::LD1B_2Z_STRIDED_IMM);
1820 case AArch64::LD1H_2Z_IMM_PSEUDO:
1821 return expandMultiVecPseudo(
1822 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1823 AArch64::LD1H_2Z_IMM, AArch64::LD1H_2Z_STRIDED_IMM);
1824 case AArch64::LD1W_2Z_IMM_PSEUDO:
1825 return expandMultiVecPseudo(
1826 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1827 AArch64::LD1W_2Z_IMM, AArch64::LD1W_2Z_STRIDED_IMM);
1828 case AArch64::LD1D_2Z_IMM_PSEUDO:
1829 return expandMultiVecPseudo(
1830 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1831 AArch64::LD1D_2Z_IMM, AArch64::LD1D_2Z_STRIDED_IMM);
1832 case AArch64::LDNT1B_2Z_IMM_PSEUDO:
1833 return expandMultiVecPseudo(
1834 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1835 AArch64::LDNT1B_2Z_IMM, AArch64::LDNT1B_2Z_STRIDED_IMM);
1836 case AArch64::LDNT1H_2Z_IMM_PSEUDO:
1837 return expandMultiVecPseudo(
1838 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1839 AArch64::LDNT1H_2Z_IMM, AArch64::LDNT1H_2Z_STRIDED_IMM);
1840 case AArch64::LDNT1W_2Z_IMM_PSEUDO:
1841 return expandMultiVecPseudo(
1842 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1843 AArch64::LDNT1W_2Z_IMM, AArch64::LDNT1W_2Z_STRIDED_IMM);
1844 case AArch64::LDNT1D_2Z_IMM_PSEUDO:
1845 return expandMultiVecPseudo(
1846 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1847 AArch64::LDNT1D_2Z_IMM, AArch64::LDNT1D_2Z_STRIDED_IMM);
1848 case AArch64::ST1B_2Z_IMM_PSEUDO:
1849 return expandMultiVecPseudo(
1850 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1851 AArch64::ST1B_2Z_IMM, AArch64::ST1B_2Z_STRIDED_IMM);
1852 case AArch64::ST1H_2Z_IMM_PSEUDO:
1853 return expandMultiVecPseudo(
1854 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1855 AArch64::ST1H_2Z_IMM, AArch64::ST1H_2Z_STRIDED_IMM);
1856 case AArch64::ST1W_2Z_IMM_PSEUDO:
1857 return expandMultiVecPseudo(
1858 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1859 AArch64::ST1W_2Z_IMM, AArch64::ST1W_2Z_STRIDED_IMM);
1860 case AArch64::ST1D_2Z_IMM_PSEUDO:
1861 return expandMultiVecPseudo(
1862 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1863 AArch64::ST1D_2Z_IMM, AArch64::ST1D_2Z_STRIDED_IMM);
1864 case AArch64::STNT1B_2Z_IMM_PSEUDO:
1865 return expandMultiVecPseudo(
1866 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1867 AArch64::STNT1B_2Z_IMM, AArch64::STNT1B_2Z_STRIDED_IMM);
1868 case AArch64::STNT1H_2Z_IMM_PSEUDO:
1869 return expandMultiVecPseudo(
1870 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1871 AArch64::STNT1H_2Z_IMM, AArch64::STNT1H_2Z_STRIDED_IMM);
1872 case AArch64::STNT1W_2Z_IMM_PSEUDO:
1873 return expandMultiVecPseudo(
1874 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1875 AArch64::STNT1W_2Z_IMM, AArch64::STNT1W_2Z_STRIDED_IMM);
1876 case AArch64::STNT1D_2Z_IMM_PSEUDO:
1877 return expandMultiVecPseudo(
1878 MBB, MBBI, AArch64::ZPR2RegClass, AArch64::ZPR2StridedRegClass,
1879 AArch64::STNT1D_2Z_IMM, AArch64::STNT1D_2Z_STRIDED_IMM);
1880 case AArch64::LD1B_2Z_PSEUDO:
1881 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1882 AArch64::ZPR2StridedRegClass, AArch64::LD1B_2Z,
1883 AArch64::LD1B_2Z_STRIDED);
1884 case AArch64::LD1H_2Z_PSEUDO:
1885 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1886 AArch64::ZPR2StridedRegClass, AArch64::LD1H_2Z,
1887 AArch64::LD1H_2Z_STRIDED);
1888 case AArch64::LD1W_2Z_PSEUDO:
1889 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1890 AArch64::ZPR2StridedRegClass, AArch64::LD1W_2Z,
1891 AArch64::LD1W_2Z_STRIDED);
1892 case AArch64::LD1D_2Z_PSEUDO:
1893 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1894 AArch64::ZPR2StridedRegClass, AArch64::LD1D_2Z,
1895 AArch64::LD1D_2Z_STRIDED);
1896 case AArch64::LDNT1B_2Z_PSEUDO:
1897 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1898 AArch64::ZPR2StridedRegClass,
1899 AArch64::LDNT1B_2Z, AArch64::LDNT1B_2Z_STRIDED);
1900 case AArch64::LDNT1H_2Z_PSEUDO:
1901 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1902 AArch64::ZPR2StridedRegClass,
1903 AArch64::LDNT1H_2Z, AArch64::LDNT1H_2Z_STRIDED);
1904 case AArch64::LDNT1W_2Z_PSEUDO:
1905 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1906 AArch64::ZPR2StridedRegClass,
1907 AArch64::LDNT1W_2Z, AArch64::LDNT1W_2Z_STRIDED);
1908 case AArch64::LDNT1D_2Z_PSEUDO:
1909 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR2RegClass,
1910 AArch64::ZPR2StridedRegClass,
1911 AArch64::LDNT1D_2Z, AArch64::LDNT1D_2Z_STRIDED);
1912 case AArch64::LD1B_4Z_IMM_PSEUDO:
1913 return expandMultiVecPseudo(
1914 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1915 AArch64::LD1B_4Z_IMM, AArch64::LD1B_4Z_STRIDED_IMM);
1916 case AArch64::LD1H_4Z_IMM_PSEUDO:
1917 return expandMultiVecPseudo(
1918 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1919 AArch64::LD1H_4Z_IMM, AArch64::LD1H_4Z_STRIDED_IMM);
1920 case AArch64::LD1W_4Z_IMM_PSEUDO:
1921 return expandMultiVecPseudo(
1922 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1923 AArch64::LD1W_4Z_IMM, AArch64::LD1W_4Z_STRIDED_IMM);
1924 case AArch64::LD1D_4Z_IMM_PSEUDO:
1925 return expandMultiVecPseudo(
1926 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1927 AArch64::LD1D_4Z_IMM, AArch64::LD1D_4Z_STRIDED_IMM);
1928 case AArch64::LDNT1B_4Z_IMM_PSEUDO:
1929 return expandMultiVecPseudo(
1930 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1931 AArch64::LDNT1B_4Z_IMM, AArch64::LDNT1B_4Z_STRIDED_IMM);
1932 case AArch64::LDNT1H_4Z_IMM_PSEUDO:
1933 return expandMultiVecPseudo(
1934 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1935 AArch64::LDNT1H_4Z_IMM, AArch64::LDNT1H_4Z_STRIDED_IMM);
1936 case AArch64::LDNT1W_4Z_IMM_PSEUDO:
1937 return expandMultiVecPseudo(
1938 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1939 AArch64::LDNT1W_4Z_IMM, AArch64::LDNT1W_4Z_STRIDED_IMM);
1940 case AArch64::LDNT1D_4Z_IMM_PSEUDO:
1941 return expandMultiVecPseudo(
1942 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1943 AArch64::LDNT1D_4Z_IMM, AArch64::LDNT1D_4Z_STRIDED_IMM);
1944 case AArch64::ST1B_4Z_IMM_PSEUDO:
1945 return expandMultiVecPseudo(
1946 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1947 AArch64::ST1B_4Z_IMM, AArch64::ST1B_4Z_STRIDED_IMM);
1948 case AArch64::ST1H_4Z_IMM_PSEUDO:
1949 return expandMultiVecPseudo(
1950 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1951 AArch64::ST1H_4Z_IMM, AArch64::ST1H_4Z_STRIDED_IMM);
1952 case AArch64::ST1W_4Z_IMM_PSEUDO:
1953 return expandMultiVecPseudo(
1954 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1955 AArch64::ST1W_4Z_IMM, AArch64::ST1W_4Z_STRIDED_IMM);
1956 case AArch64::ST1D_4Z_IMM_PSEUDO:
1957 return expandMultiVecPseudo(
1958 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1959 AArch64::ST1D_4Z_IMM, AArch64::ST1D_4Z_STRIDED_IMM);
1960 case AArch64::STNT1B_4Z_IMM_PSEUDO:
1961 return expandMultiVecPseudo(
1962 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1963 AArch64::STNT1B_4Z_IMM, AArch64::STNT1B_4Z_STRIDED_IMM);
1964 case AArch64::STNT1H_4Z_IMM_PSEUDO:
1965 return expandMultiVecPseudo(
1966 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1967 AArch64::STNT1H_4Z_IMM, AArch64::STNT1H_4Z_STRIDED_IMM);
1968 case AArch64::STNT1W_4Z_IMM_PSEUDO:
1969 return expandMultiVecPseudo(
1970 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1971 AArch64::STNT1W_4Z_IMM, AArch64::STNT1W_4Z_STRIDED_IMM);
1972 case AArch64::STNT1D_4Z_IMM_PSEUDO:
1973 return expandMultiVecPseudo(
1974 MBB, MBBI, AArch64::ZPR4RegClass, AArch64::ZPR4StridedRegClass,
1975 AArch64::STNT1D_4Z_IMM, AArch64::STNT1D_4Z_STRIDED_IMM);
1976 case AArch64::LD1B_4Z_PSEUDO:
1977 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1978 AArch64::ZPR4StridedRegClass, AArch64::LD1B_4Z,
1979 AArch64::LD1B_4Z_STRIDED);
1980 case AArch64::LD1H_4Z_PSEUDO:
1981 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1982 AArch64::ZPR4StridedRegClass, AArch64::LD1H_4Z,
1983 AArch64::LD1H_4Z_STRIDED);
1984 case AArch64::LD1W_4Z_PSEUDO:
1985 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1986 AArch64::ZPR4StridedRegClass, AArch64::LD1W_4Z,
1987 AArch64::LD1W_4Z_STRIDED);
1988 case AArch64::LD1D_4Z_PSEUDO:
1989 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1990 AArch64::ZPR4StridedRegClass, AArch64::LD1D_4Z,
1991 AArch64::LD1D_4Z_STRIDED);
1992 case AArch64::LDNT1B_4Z_PSEUDO:
1993 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1994 AArch64::ZPR4StridedRegClass,
1995 AArch64::LDNT1B_4Z, AArch64::LDNT1B_4Z_STRIDED);
1996 case AArch64::LDNT1H_4Z_PSEUDO:
1997 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
1998 AArch64::ZPR4StridedRegClass,
1999 AArch64::LDNT1H_4Z, AArch64::LDNT1H_4Z_STRIDED);
2000 case AArch64::LDNT1W_4Z_PSEUDO:
2001 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
2002 AArch64::ZPR4StridedRegClass,
2003 AArch64::LDNT1W_4Z, AArch64::LDNT1W_4Z_STRIDED);
2004 case AArch64::LDNT1D_4Z_PSEUDO:
2005 return expandMultiVecPseudo(MBB, MBBI, AArch64::ZPR4RegClass,
2006 AArch64::ZPR4StridedRegClass,
2007 AArch64::LDNT1D_4Z, AArch64::LDNT1D_4Z_STRIDED);
2008 case AArch64::COPY_INTO_TRANSPOSED_TUPLE:
2009 return expandCopyIntoTuplePseudo(MI, MBB, MBBI);
2010 case AArch64::EON_ZZZ:
2011 case AArch64::NAND_ZZZ:
2012 case AArch64::NOR_ZZZ:
2013 return expandSVEBitwisePseudo(MI, MBB, MBBI);
2014 }
2015 return false;
2016}
2017
2018/// Iterate over the instructions in basic block MBB and expand any
2019/// pseudo instructions. Return true if anything was modified.
2020bool AArch64ExpandPseudoImpl::expandMBB(MachineBasicBlock &MBB) {
2021 bool Modified = false;
2022
2024 while (MBBI != E) {
2025 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
2026 if (MBBI->isPseudo())
2027 Modified |= expandMI(MBB, MBBI, NMBBI);
2028 MBBI = NMBBI;
2029 }
2030
2031 return Modified;
2032}
2033
2034bool AArch64ExpandPseudoImpl::run(MachineFunction &MF) {
2035 TII = MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
2036
2037 bool Modified = false;
2038 for (auto &MBB : MF)
2039 Modified |= expandMBB(MBB);
2040 return Modified;
2041}
2042
2043bool AArch64ExpandPseudoLegacy::runOnMachineFunction(MachineFunction &MF) {
2044 return AArch64ExpandPseudoImpl().run(MF);
2045}
2046
2047/// Returns an instance of the pseudo instruction expansion pass.
2049 return new AArch64ExpandPseudoLegacy();
2050}
2051
2055 const bool Changed = AArch64ExpandPseudoImpl().run(MF);
2056 if (!Changed)
2057 return PreservedAnalyses::all();
2060 return PA;
2061}
#define AARCH64_EXPAND_PSEUDO_NAME
MachineInstrBuilder & UseMI
static MachineInstr * createCallWithOps(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const AArch64InstrInfo *TII, unsigned Opcode, ArrayRef< MachineOperand > ExplicitOps, unsigned RegMaskStartIdx)
static constexpr unsigned ZERO_ALL_ZA_MASK
static MachineInstr * createCall(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const AArch64InstrInfo *TII, MachineOperand &CallTarget, unsigned RegMaskStartIdx)
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Imm
unsigned uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static void transferImpOps(const MachineInstr &OldMI, MachineInstrBuilder &MI)
Transfer implicit operands on the pseudo instruction to the instructions created from the expansion.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Describe properties that are true of each instruction in the target description file.
ArrayRef< MCPhysReg > getRegisters() const
LLVM_ABI 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.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI MachineBasicBlock * splitAt(MachineInstr &SplitInst, bool UpdateLiveIns=true, LiveIntervals *LIS=nullptr)
Split a basic block into 2 pieces at SplitPoint.
LLVM_ABI 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()
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI 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...
void moveAdditionalCallInfo(const MachineInstr *Old, const MachineInstr *New)
Move the call site info from Old to \New call site info.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
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 & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
void setDebugInstrNum(unsigned Num)
Set instruction number of this MachineInstr.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
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.
LLVM_ABI bool isRenamable() const
isRenamable - Returns true if this register may be renamed, i.e.
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.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
CodeModel::Model getCodeModel() const
Returns the code model.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
self_iterator getIterator()
Definition ilist_node.h:123
IteratorT end() const
IteratorT begin() const
CallInst * Call
Changed
#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_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 expandMOVAddr(unsigned Opcode, unsigned TargetFlags, bool IsTargetMachO, SmallVectorImpl< AddrInsnModel > &Insn)
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...
int32_t getSVERevInstr(uint32_t Opcode)
int32_t getSVENonRevInstr(uint32_t Opcode)
int32_t getSVEPseudoMap(uint32_t Opcode)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
LLVM_ABI 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.
RegState
Flags to represent properties of register accesses.
@ Kill
The last use of a register.
constexpr RegState getKillRegState(bool B)
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition APFloat.h:1721
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
constexpr RegState getDeadRegState(bool B)
Op::Description Desc
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createAArch64ExpandPseudoLegacyPass()
Returns an instance of the pseudo instruction expansion pass.
constexpr RegState getRenamableRegState(bool B)
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.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr RegState getDefRegState(bool B)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
constexpr RegState getUndefRegState(bool B)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N