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