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