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