1 //===- AArch64InstrInfo.cpp - AArch64 Instruction Information -------------===//
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 the AArch64 implementation of the TargetInstrInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "AArch64InstrInfo.h"
14 #include "AArch64MachineFunctionInfo.h"
15 #include "AArch64Subtarget.h"
16 #include "MCTargetDesc/AArch64AddressingModes.h"
17 #include "Utils/AArch64BaseInfo.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineMemOperand.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/StackMaps.h"
31 #include "llvm/CodeGen/TargetRegisterInfo.h"
32 #include "llvm/CodeGen/TargetSubtargetInfo.h"
33 #include "llvm/IR/DebugInfoMetadata.h"
34 #include "llvm/IR/DebugLoc.h"
35 #include "llvm/IR/GlobalValue.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/MCInst.h"
38 #include "llvm/MC/MCInstBuilder.h"
39 #include "llvm/MC/MCInstrDesc.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/CodeGen.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Compiler.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/MathExtras.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
48 #include <cassert>
49 #include <cstdint>
50 #include <iterator>
51 #include <utility>
52 
53 using namespace llvm;
54 
55 #define GET_INSTRINFO_CTOR_DTOR
56 #include "AArch64GenInstrInfo.inc"
57 
58 static cl::opt<unsigned> TBZDisplacementBits(
59     "aarch64-tbz-offset-bits", cl::Hidden, cl::init(14),
60     cl::desc("Restrict range of TB[N]Z instructions (DEBUG)"));
61 
62 static cl::opt<unsigned> CBZDisplacementBits(
63     "aarch64-cbz-offset-bits", cl::Hidden, cl::init(19),
64     cl::desc("Restrict range of CB[N]Z instructions (DEBUG)"));
65 
66 static cl::opt<unsigned>
67     BCCDisplacementBits("aarch64-bcc-offset-bits", cl::Hidden, cl::init(19),
68                         cl::desc("Restrict range of Bcc instructions (DEBUG)"));
69 
70 AArch64InstrInfo::AArch64InstrInfo(const AArch64Subtarget &STI)
71     : AArch64GenInstrInfo(AArch64::ADJCALLSTACKDOWN, AArch64::ADJCALLSTACKUP,
72                           AArch64::CATCHRET),
73       RI(STI.getTargetTriple()), Subtarget(STI) {}
74 
75 /// GetInstSize - Return the number of bytes of code the specified
76 /// instruction may be.  This returns the maximum number of bytes.
77 unsigned AArch64InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
78   const MachineBasicBlock &MBB = *MI.getParent();
79   const MachineFunction *MF = MBB.getParent();
80   const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
81 
82   {
83     auto Op = MI.getOpcode();
84     if (Op == AArch64::INLINEASM || Op == AArch64::INLINEASM_BR)
85       return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
86   }
87 
88   // Meta-instructions emit no code.
89   if (MI.isMetaInstruction())
90     return 0;
91 
92   // FIXME: We currently only handle pseudoinstructions that don't get expanded
93   //        before the assembly printer.
94   unsigned NumBytes = 0;
95   const MCInstrDesc &Desc = MI.getDesc();
96   switch (Desc.getOpcode()) {
97   default:
98     // Anything not explicitly designated otherwise is a normal 4-byte insn.
99     NumBytes = 4;
100     break;
101   case TargetOpcode::STACKMAP:
102     // The upper bound for a stackmap intrinsic is the full length of its shadow
103     NumBytes = StackMapOpers(&MI).getNumPatchBytes();
104     assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
105     break;
106   case TargetOpcode::PATCHPOINT:
107     // The size of the patchpoint intrinsic is the number of bytes requested
108     NumBytes = PatchPointOpers(&MI).getNumPatchBytes();
109     assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
110     break;
111   case TargetOpcode::STATEPOINT:
112     NumBytes = StatepointOpers(&MI).getNumPatchBytes();
113     assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
114     // No patch bytes means a normal call inst is emitted
115     if (NumBytes == 0)
116       NumBytes = 4;
117     break;
118   case AArch64::TLSDESC_CALLSEQ:
119     // This gets lowered to an instruction sequence which takes 16 bytes
120     NumBytes = 16;
121     break;
122   case AArch64::SpeculationBarrierISBDSBEndBB:
123     // This gets lowered to 2 4-byte instructions.
124     NumBytes = 8;
125     break;
126   case AArch64::SpeculationBarrierSBEndBB:
127     // This gets lowered to 1 4-byte instructions.
128     NumBytes = 4;
129     break;
130   case AArch64::JumpTableDest32:
131   case AArch64::JumpTableDest16:
132   case AArch64::JumpTableDest8:
133     NumBytes = 12;
134     break;
135   case AArch64::SPACE:
136     NumBytes = MI.getOperand(1).getImm();
137     break;
138   case TargetOpcode::BUNDLE:
139     NumBytes = getInstBundleLength(MI);
140     break;
141   }
142 
143   return NumBytes;
144 }
145 
146 unsigned AArch64InstrInfo::getInstBundleLength(const MachineInstr &MI) const {
147   unsigned Size = 0;
148   MachineBasicBlock::const_instr_iterator I = MI.getIterator();
149   MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
150   while (++I != E && I->isInsideBundle()) {
151     assert(!I->isBundle() && "No nested bundle!");
152     Size += getInstSizeInBytes(*I);
153   }
154   return Size;
155 }
156 
157 static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target,
158                             SmallVectorImpl<MachineOperand> &Cond) {
159   // Block ends with fall-through condbranch.
160   switch (LastInst->getOpcode()) {
161   default:
162     llvm_unreachable("Unknown branch instruction?");
163   case AArch64::Bcc:
164     Target = LastInst->getOperand(1).getMBB();
165     Cond.push_back(LastInst->getOperand(0));
166     break;
167   case AArch64::CBZW:
168   case AArch64::CBZX:
169   case AArch64::CBNZW:
170   case AArch64::CBNZX:
171     Target = LastInst->getOperand(1).getMBB();
172     Cond.push_back(MachineOperand::CreateImm(-1));
173     Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
174     Cond.push_back(LastInst->getOperand(0));
175     break;
176   case AArch64::TBZW:
177   case AArch64::TBZX:
178   case AArch64::TBNZW:
179   case AArch64::TBNZX:
180     Target = LastInst->getOperand(2).getMBB();
181     Cond.push_back(MachineOperand::CreateImm(-1));
182     Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
183     Cond.push_back(LastInst->getOperand(0));
184     Cond.push_back(LastInst->getOperand(1));
185   }
186 }
187 
188 static unsigned getBranchDisplacementBits(unsigned Opc) {
189   switch (Opc) {
190   default:
191     llvm_unreachable("unexpected opcode!");
192   case AArch64::B:
193     return 64;
194   case AArch64::TBNZW:
195   case AArch64::TBZW:
196   case AArch64::TBNZX:
197   case AArch64::TBZX:
198     return TBZDisplacementBits;
199   case AArch64::CBNZW:
200   case AArch64::CBZW:
201   case AArch64::CBNZX:
202   case AArch64::CBZX:
203     return CBZDisplacementBits;
204   case AArch64::Bcc:
205     return BCCDisplacementBits;
206   }
207 }
208 
209 bool AArch64InstrInfo::isBranchOffsetInRange(unsigned BranchOp,
210                                              int64_t BrOffset) const {
211   unsigned Bits = getBranchDisplacementBits(BranchOp);
212   assert(Bits >= 3 && "max branch displacement must be enough to jump"
213                       "over conditional branch expansion");
214   return isIntN(Bits, BrOffset / 4);
215 }
216 
217 MachineBasicBlock *
218 AArch64InstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
219   switch (MI.getOpcode()) {
220   default:
221     llvm_unreachable("unexpected opcode!");
222   case AArch64::B:
223     return MI.getOperand(0).getMBB();
224   case AArch64::TBZW:
225   case AArch64::TBNZW:
226   case AArch64::TBZX:
227   case AArch64::TBNZX:
228     return MI.getOperand(2).getMBB();
229   case AArch64::CBZW:
230   case AArch64::CBNZW:
231   case AArch64::CBZX:
232   case AArch64::CBNZX:
233   case AArch64::Bcc:
234     return MI.getOperand(1).getMBB();
235   }
236 }
237 
238 // Branch analysis.
239 bool AArch64InstrInfo::analyzeBranch(MachineBasicBlock &MBB,
240                                      MachineBasicBlock *&TBB,
241                                      MachineBasicBlock *&FBB,
242                                      SmallVectorImpl<MachineOperand> &Cond,
243                                      bool AllowModify) const {
244   // If the block has no terminators, it just falls into the block after it.
245   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
246   if (I == MBB.end())
247     return false;
248 
249   // Skip over SpeculationBarrierEndBB terminators
250   if (I->getOpcode() == AArch64::SpeculationBarrierISBDSBEndBB ||
251       I->getOpcode() == AArch64::SpeculationBarrierSBEndBB) {
252     --I;
253   }
254 
255   if (!isUnpredicatedTerminator(*I))
256     return false;
257 
258   // Get the last instruction in the block.
259   MachineInstr *LastInst = &*I;
260 
261   // If there is only one terminator instruction, process it.
262   unsigned LastOpc = LastInst->getOpcode();
263   if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
264     if (isUncondBranchOpcode(LastOpc)) {
265       TBB = LastInst->getOperand(0).getMBB();
266       return false;
267     }
268     if (isCondBranchOpcode(LastOpc)) {
269       // Block ends with fall-through condbranch.
270       parseCondBranch(LastInst, TBB, Cond);
271       return false;
272     }
273     return true; // Can't handle indirect branch.
274   }
275 
276   // Get the instruction before it if it is a terminator.
277   MachineInstr *SecondLastInst = &*I;
278   unsigned SecondLastOpc = SecondLastInst->getOpcode();
279 
280   // If AllowModify is true and the block ends with two or more unconditional
281   // branches, delete all but the first unconditional branch.
282   if (AllowModify && isUncondBranchOpcode(LastOpc)) {
283     while (isUncondBranchOpcode(SecondLastOpc)) {
284       LastInst->eraseFromParent();
285       LastInst = SecondLastInst;
286       LastOpc = LastInst->getOpcode();
287       if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
288         // Return now the only terminator is an unconditional branch.
289         TBB = LastInst->getOperand(0).getMBB();
290         return false;
291       } else {
292         SecondLastInst = &*I;
293         SecondLastOpc = SecondLastInst->getOpcode();
294       }
295     }
296   }
297 
298   // If we're allowed to modify and the block ends in a unconditional branch
299   // which could simply fallthrough, remove the branch.  (Note: This case only
300   // matters when we can't understand the whole sequence, otherwise it's also
301   // handled by BranchFolding.cpp.)
302   if (AllowModify && isUncondBranchOpcode(LastOpc) &&
303       MBB.isLayoutSuccessor(getBranchDestBlock(*LastInst))) {
304     LastInst->eraseFromParent();
305     LastInst = SecondLastInst;
306     LastOpc = LastInst->getOpcode();
307     if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
308       assert(!isUncondBranchOpcode(LastOpc) &&
309              "unreachable unconditional branches removed above");
310 
311       if (isCondBranchOpcode(LastOpc)) {
312         // Block ends with fall-through condbranch.
313         parseCondBranch(LastInst, TBB, Cond);
314         return false;
315       }
316       return true; // Can't handle indirect branch.
317     } else {
318       SecondLastInst = &*I;
319       SecondLastOpc = SecondLastInst->getOpcode();
320     }
321   }
322 
323   // If there are three terminators, we don't know what sort of block this is.
324   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I))
325     return true;
326 
327   // If the block ends with a B and a Bcc, handle it.
328   if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
329     parseCondBranch(SecondLastInst, TBB, Cond);
330     FBB = LastInst->getOperand(0).getMBB();
331     return false;
332   }
333 
334   // If the block ends with two unconditional branches, handle it.  The second
335   // one is not executed, so remove it.
336   if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
337     TBB = SecondLastInst->getOperand(0).getMBB();
338     I = LastInst;
339     if (AllowModify)
340       I->eraseFromParent();
341     return false;
342   }
343 
344   // ...likewise if it ends with an indirect branch followed by an unconditional
345   // branch.
346   if (isIndirectBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
347     I = LastInst;
348     if (AllowModify)
349       I->eraseFromParent();
350     return true;
351   }
352 
353   // Otherwise, can't handle this.
354   return true;
355 }
356 
357 bool AArch64InstrInfo::analyzeBranchPredicate(MachineBasicBlock &MBB,
358                                               MachineBranchPredicate &MBP,
359                                               bool AllowModify) const {
360   // For the moment, handle only a block which ends with a cb(n)zx followed by
361   // a fallthrough.  Why this?  Because it is a common form.
362   // TODO: Should we handle b.cc?
363 
364   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
365   if (I == MBB.end())
366     return true;
367 
368   // Skip over SpeculationBarrierEndBB terminators
369   if (I->getOpcode() == AArch64::SpeculationBarrierISBDSBEndBB ||
370       I->getOpcode() == AArch64::SpeculationBarrierSBEndBB) {
371     --I;
372   }
373 
374   if (!isUnpredicatedTerminator(*I))
375     return true;
376 
377   // Get the last instruction in the block.
378   MachineInstr *LastInst = &*I;
379   unsigned LastOpc = LastInst->getOpcode();
380   if (!isCondBranchOpcode(LastOpc))
381     return true;
382 
383   switch (LastOpc) {
384   default:
385     return true;
386   case AArch64::CBZW:
387   case AArch64::CBZX:
388   case AArch64::CBNZW:
389   case AArch64::CBNZX:
390     break;
391   };
392 
393   MBP.TrueDest = LastInst->getOperand(1).getMBB();
394   assert(MBP.TrueDest && "expected!");
395   MBP.FalseDest = MBB.getNextNode();
396 
397   MBP.ConditionDef = nullptr;
398   MBP.SingleUseCondition = false;
399 
400   MBP.LHS = LastInst->getOperand(0);
401   MBP.RHS = MachineOperand::CreateImm(0);
402   MBP.Predicate = LastOpc == AArch64::CBNZX ? MachineBranchPredicate::PRED_NE
403                                             : MachineBranchPredicate::PRED_EQ;
404   return false;
405 }
406 
407 bool AArch64InstrInfo::reverseBranchCondition(
408     SmallVectorImpl<MachineOperand> &Cond) const {
409   if (Cond[0].getImm() != -1) {
410     // Regular Bcc
411     AArch64CC::CondCode CC = (AArch64CC::CondCode)(int)Cond[0].getImm();
412     Cond[0].setImm(AArch64CC::getInvertedCondCode(CC));
413   } else {
414     // Folded compare-and-branch
415     switch (Cond[1].getImm()) {
416     default:
417       llvm_unreachable("Unknown conditional branch!");
418     case AArch64::CBZW:
419       Cond[1].setImm(AArch64::CBNZW);
420       break;
421     case AArch64::CBNZW:
422       Cond[1].setImm(AArch64::CBZW);
423       break;
424     case AArch64::CBZX:
425       Cond[1].setImm(AArch64::CBNZX);
426       break;
427     case AArch64::CBNZX:
428       Cond[1].setImm(AArch64::CBZX);
429       break;
430     case AArch64::TBZW:
431       Cond[1].setImm(AArch64::TBNZW);
432       break;
433     case AArch64::TBNZW:
434       Cond[1].setImm(AArch64::TBZW);
435       break;
436     case AArch64::TBZX:
437       Cond[1].setImm(AArch64::TBNZX);
438       break;
439     case AArch64::TBNZX:
440       Cond[1].setImm(AArch64::TBZX);
441       break;
442     }
443   }
444 
445   return false;
446 }
447 
448 unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &MBB,
449                                         int *BytesRemoved) const {
450   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
451   if (I == MBB.end())
452     return 0;
453 
454   if (!isUncondBranchOpcode(I->getOpcode()) &&
455       !isCondBranchOpcode(I->getOpcode()))
456     return 0;
457 
458   // Remove the branch.
459   I->eraseFromParent();
460 
461   I = MBB.end();
462 
463   if (I == MBB.begin()) {
464     if (BytesRemoved)
465       *BytesRemoved = 4;
466     return 1;
467   }
468   --I;
469   if (!isCondBranchOpcode(I->getOpcode())) {
470     if (BytesRemoved)
471       *BytesRemoved = 4;
472     return 1;
473   }
474 
475   // Remove the branch.
476   I->eraseFromParent();
477   if (BytesRemoved)
478     *BytesRemoved = 8;
479 
480   return 2;
481 }
482 
483 void AArch64InstrInfo::instantiateCondBranch(
484     MachineBasicBlock &MBB, const DebugLoc &DL, MachineBasicBlock *TBB,
485     ArrayRef<MachineOperand> Cond) const {
486   if (Cond[0].getImm() != -1) {
487     // Regular Bcc
488     BuildMI(&MBB, DL, get(AArch64::Bcc)).addImm(Cond[0].getImm()).addMBB(TBB);
489   } else {
490     // Folded compare-and-branch
491     // Note that we use addOperand instead of addReg to keep the flags.
492     const MachineInstrBuilder MIB =
493         BuildMI(&MBB, DL, get(Cond[1].getImm())).add(Cond[2]);
494     if (Cond.size() > 3)
495       MIB.addImm(Cond[3].getImm());
496     MIB.addMBB(TBB);
497   }
498 }
499 
500 unsigned AArch64InstrInfo::insertBranch(
501     MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
502     ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
503   // Shouldn't be a fall through.
504   assert(TBB && "insertBranch must not be told to insert a fallthrough");
505 
506   if (!FBB) {
507     if (Cond.empty()) // Unconditional branch?
508       BuildMI(&MBB, DL, get(AArch64::B)).addMBB(TBB);
509     else
510       instantiateCondBranch(MBB, DL, TBB, Cond);
511 
512     if (BytesAdded)
513       *BytesAdded = 4;
514 
515     return 1;
516   }
517 
518   // Two-way conditional branch.
519   instantiateCondBranch(MBB, DL, TBB, Cond);
520   BuildMI(&MBB, DL, get(AArch64::B)).addMBB(FBB);
521 
522   if (BytesAdded)
523     *BytesAdded = 8;
524 
525   return 2;
526 }
527 
528 // Find the original register that VReg is copied from.
529 static unsigned removeCopies(const MachineRegisterInfo &MRI, unsigned VReg) {
530   while (Register::isVirtualRegister(VReg)) {
531     const MachineInstr *DefMI = MRI.getVRegDef(VReg);
532     if (!DefMI->isFullCopy())
533       return VReg;
534     VReg = DefMI->getOperand(1).getReg();
535   }
536   return VReg;
537 }
538 
539 // Determine if VReg is defined by an instruction that can be folded into a
540 // csel instruction. If so, return the folded opcode, and the replacement
541 // register.
542 static unsigned canFoldIntoCSel(const MachineRegisterInfo &MRI, unsigned VReg,
543                                 unsigned *NewVReg = nullptr) {
544   VReg = removeCopies(MRI, VReg);
545   if (!Register::isVirtualRegister(VReg))
546     return 0;
547 
548   bool Is64Bit = AArch64::GPR64allRegClass.hasSubClassEq(MRI.getRegClass(VReg));
549   const MachineInstr *DefMI = MRI.getVRegDef(VReg);
550   unsigned Opc = 0;
551   unsigned SrcOpNum = 0;
552   switch (DefMI->getOpcode()) {
553   case AArch64::ADDSXri:
554   case AArch64::ADDSWri:
555     // if NZCV is used, do not fold.
556     if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
557       return 0;
558     // fall-through to ADDXri and ADDWri.
559     LLVM_FALLTHROUGH;
560   case AArch64::ADDXri:
561   case AArch64::ADDWri:
562     // add x, 1 -> csinc.
563     if (!DefMI->getOperand(2).isImm() || DefMI->getOperand(2).getImm() != 1 ||
564         DefMI->getOperand(3).getImm() != 0)
565       return 0;
566     SrcOpNum = 1;
567     Opc = Is64Bit ? AArch64::CSINCXr : AArch64::CSINCWr;
568     break;
569 
570   case AArch64::ORNXrr:
571   case AArch64::ORNWrr: {
572     // not x -> csinv, represented as orn dst, xzr, src.
573     unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
574     if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
575       return 0;
576     SrcOpNum = 2;
577     Opc = Is64Bit ? AArch64::CSINVXr : AArch64::CSINVWr;
578     break;
579   }
580 
581   case AArch64::SUBSXrr:
582   case AArch64::SUBSWrr:
583     // if NZCV is used, do not fold.
584     if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
585       return 0;
586     // fall-through to SUBXrr and SUBWrr.
587     LLVM_FALLTHROUGH;
588   case AArch64::SUBXrr:
589   case AArch64::SUBWrr: {
590     // neg x -> csneg, represented as sub dst, xzr, src.
591     unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
592     if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
593       return 0;
594     SrcOpNum = 2;
595     Opc = Is64Bit ? AArch64::CSNEGXr : AArch64::CSNEGWr;
596     break;
597   }
598   default:
599     return 0;
600   }
601   assert(Opc && SrcOpNum && "Missing parameters");
602 
603   if (NewVReg)
604     *NewVReg = DefMI->getOperand(SrcOpNum).getReg();
605   return Opc;
606 }
607 
608 bool AArch64InstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
609                                        ArrayRef<MachineOperand> Cond,
610                                        Register DstReg, Register TrueReg,
611                                        Register FalseReg, int &CondCycles,
612                                        int &TrueCycles,
613                                        int &FalseCycles) const {
614   // Check register classes.
615   const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
616   const TargetRegisterClass *RC =
617       RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
618   if (!RC)
619     return false;
620 
621   // Also need to check the dest regclass, in case we're trying to optimize
622   // something like:
623   // %1(gpr) = PHI %2(fpr), bb1, %(fpr), bb2
624   if (!RI.getCommonSubClass(RC, MRI.getRegClass(DstReg)))
625     return false;
626 
627   // Expanding cbz/tbz requires an extra cycle of latency on the condition.
628   unsigned ExtraCondLat = Cond.size() != 1;
629 
630   // GPRs are handled by csel.
631   // FIXME: Fold in x+1, -x, and ~x when applicable.
632   if (AArch64::GPR64allRegClass.hasSubClassEq(RC) ||
633       AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
634     // Single-cycle csel, csinc, csinv, and csneg.
635     CondCycles = 1 + ExtraCondLat;
636     TrueCycles = FalseCycles = 1;
637     if (canFoldIntoCSel(MRI, TrueReg))
638       TrueCycles = 0;
639     else if (canFoldIntoCSel(MRI, FalseReg))
640       FalseCycles = 0;
641     return true;
642   }
643 
644   // Scalar floating point is handled by fcsel.
645   // FIXME: Form fabs, fmin, and fmax when applicable.
646   if (AArch64::FPR64RegClass.hasSubClassEq(RC) ||
647       AArch64::FPR32RegClass.hasSubClassEq(RC)) {
648     CondCycles = 5 + ExtraCondLat;
649     TrueCycles = FalseCycles = 2;
650     return true;
651   }
652 
653   // Can't do vectors.
654   return false;
655 }
656 
657 void AArch64InstrInfo::insertSelect(MachineBasicBlock &MBB,
658                                     MachineBasicBlock::iterator I,
659                                     const DebugLoc &DL, Register DstReg,
660                                     ArrayRef<MachineOperand> Cond,
661                                     Register TrueReg, Register FalseReg) const {
662   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
663 
664   // Parse the condition code, see parseCondBranch() above.
665   AArch64CC::CondCode CC;
666   switch (Cond.size()) {
667   default:
668     llvm_unreachable("Unknown condition opcode in Cond");
669   case 1: // b.cc
670     CC = AArch64CC::CondCode(Cond[0].getImm());
671     break;
672   case 3: { // cbz/cbnz
673     // We must insert a compare against 0.
674     bool Is64Bit;
675     switch (Cond[1].getImm()) {
676     default:
677       llvm_unreachable("Unknown branch opcode in Cond");
678     case AArch64::CBZW:
679       Is64Bit = false;
680       CC = AArch64CC::EQ;
681       break;
682     case AArch64::CBZX:
683       Is64Bit = true;
684       CC = AArch64CC::EQ;
685       break;
686     case AArch64::CBNZW:
687       Is64Bit = false;
688       CC = AArch64CC::NE;
689       break;
690     case AArch64::CBNZX:
691       Is64Bit = true;
692       CC = AArch64CC::NE;
693       break;
694     }
695     Register SrcReg = Cond[2].getReg();
696     if (Is64Bit) {
697       // cmp reg, #0 is actually subs xzr, reg, #0.
698       MRI.constrainRegClass(SrcReg, &AArch64::GPR64spRegClass);
699       BuildMI(MBB, I, DL, get(AArch64::SUBSXri), AArch64::XZR)
700           .addReg(SrcReg)
701           .addImm(0)
702           .addImm(0);
703     } else {
704       MRI.constrainRegClass(SrcReg, &AArch64::GPR32spRegClass);
705       BuildMI(MBB, I, DL, get(AArch64::SUBSWri), AArch64::WZR)
706           .addReg(SrcReg)
707           .addImm(0)
708           .addImm(0);
709     }
710     break;
711   }
712   case 4: { // tbz/tbnz
713     // We must insert a tst instruction.
714     switch (Cond[1].getImm()) {
715     default:
716       llvm_unreachable("Unknown branch opcode in Cond");
717     case AArch64::TBZW:
718     case AArch64::TBZX:
719       CC = AArch64CC::EQ;
720       break;
721     case AArch64::TBNZW:
722     case AArch64::TBNZX:
723       CC = AArch64CC::NE;
724       break;
725     }
726     // cmp reg, #foo is actually ands xzr, reg, #1<<foo.
727     if (Cond[1].getImm() == AArch64::TBZW || Cond[1].getImm() == AArch64::TBNZW)
728       BuildMI(MBB, I, DL, get(AArch64::ANDSWri), AArch64::WZR)
729           .addReg(Cond[2].getReg())
730           .addImm(
731               AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 32));
732     else
733       BuildMI(MBB, I, DL, get(AArch64::ANDSXri), AArch64::XZR)
734           .addReg(Cond[2].getReg())
735           .addImm(
736               AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 64));
737     break;
738   }
739   }
740 
741   unsigned Opc = 0;
742   const TargetRegisterClass *RC = nullptr;
743   bool TryFold = false;
744   if (MRI.constrainRegClass(DstReg, &AArch64::GPR64RegClass)) {
745     RC = &AArch64::GPR64RegClass;
746     Opc = AArch64::CSELXr;
747     TryFold = true;
748   } else if (MRI.constrainRegClass(DstReg, &AArch64::GPR32RegClass)) {
749     RC = &AArch64::GPR32RegClass;
750     Opc = AArch64::CSELWr;
751     TryFold = true;
752   } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR64RegClass)) {
753     RC = &AArch64::FPR64RegClass;
754     Opc = AArch64::FCSELDrrr;
755   } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR32RegClass)) {
756     RC = &AArch64::FPR32RegClass;
757     Opc = AArch64::FCSELSrrr;
758   }
759   assert(RC && "Unsupported regclass");
760 
761   // Try folding simple instructions into the csel.
762   if (TryFold) {
763     unsigned NewVReg = 0;
764     unsigned FoldedOpc = canFoldIntoCSel(MRI, TrueReg, &NewVReg);
765     if (FoldedOpc) {
766       // The folded opcodes csinc, csinc and csneg apply the operation to
767       // FalseReg, so we need to invert the condition.
768       CC = AArch64CC::getInvertedCondCode(CC);
769       TrueReg = FalseReg;
770     } else
771       FoldedOpc = canFoldIntoCSel(MRI, FalseReg, &NewVReg);
772 
773     // Fold the operation. Leave any dead instructions for DCE to clean up.
774     if (FoldedOpc) {
775       FalseReg = NewVReg;
776       Opc = FoldedOpc;
777       // The extends the live range of NewVReg.
778       MRI.clearKillFlags(NewVReg);
779     }
780   }
781 
782   // Pull all virtual register into the appropriate class.
783   MRI.constrainRegClass(TrueReg, RC);
784   MRI.constrainRegClass(FalseReg, RC);
785 
786   // Insert the csel.
787   BuildMI(MBB, I, DL, get(Opc), DstReg)
788       .addReg(TrueReg)
789       .addReg(FalseReg)
790       .addImm(CC);
791 }
792 
793 /// Returns true if a MOVi32imm or MOVi64imm can be expanded to an  ORRxx.
794 static bool canBeExpandedToORR(const MachineInstr &MI, unsigned BitSize) {
795   uint64_t Imm = MI.getOperand(1).getImm();
796   uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
797   uint64_t Encoding;
798   return AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding);
799 }
800 
801 // FIXME: this implementation should be micro-architecture dependent, so a
802 // micro-architecture target hook should be introduced here in future.
803 bool AArch64InstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
804   if (!Subtarget.hasCustomCheapAsMoveHandling())
805     return MI.isAsCheapAsAMove();
806 
807   const unsigned Opcode = MI.getOpcode();
808 
809   // Firstly, check cases gated by features.
810 
811   if (Subtarget.hasZeroCycleZeroingFP()) {
812     if (Opcode == AArch64::FMOVH0 ||
813         Opcode == AArch64::FMOVS0 ||
814         Opcode == AArch64::FMOVD0)
815       return true;
816   }
817 
818   if (Subtarget.hasZeroCycleZeroingGP()) {
819     if (Opcode == TargetOpcode::COPY &&
820         (MI.getOperand(1).getReg() == AArch64::WZR ||
821          MI.getOperand(1).getReg() == AArch64::XZR))
822       return true;
823   }
824 
825   // Secondly, check cases specific to sub-targets.
826 
827   if (Subtarget.hasExynosCheapAsMoveHandling()) {
828     if (isExynosCheapAsMove(MI))
829       return true;
830 
831     return MI.isAsCheapAsAMove();
832   }
833 
834   // Finally, check generic cases.
835 
836   switch (Opcode) {
837   default:
838     return false;
839 
840   // add/sub on register without shift
841   case AArch64::ADDWri:
842   case AArch64::ADDXri:
843   case AArch64::SUBWri:
844   case AArch64::SUBXri:
845     return (MI.getOperand(3).getImm() == 0);
846 
847   // logical ops on immediate
848   case AArch64::ANDWri:
849   case AArch64::ANDXri:
850   case AArch64::EORWri:
851   case AArch64::EORXri:
852   case AArch64::ORRWri:
853   case AArch64::ORRXri:
854     return true;
855 
856   // logical ops on register without shift
857   case AArch64::ANDWrr:
858   case AArch64::ANDXrr:
859   case AArch64::BICWrr:
860   case AArch64::BICXrr:
861   case AArch64::EONWrr:
862   case AArch64::EONXrr:
863   case AArch64::EORWrr:
864   case AArch64::EORXrr:
865   case AArch64::ORNWrr:
866   case AArch64::ORNXrr:
867   case AArch64::ORRWrr:
868   case AArch64::ORRXrr:
869     return true;
870 
871   // If MOVi32imm or MOVi64imm can be expanded into ORRWri or
872   // ORRXri, it is as cheap as MOV
873   case AArch64::MOVi32imm:
874     return canBeExpandedToORR(MI, 32);
875   case AArch64::MOVi64imm:
876     return canBeExpandedToORR(MI, 64);
877   }
878 
879   llvm_unreachable("Unknown opcode to check as cheap as a move!");
880 }
881 
882 bool AArch64InstrInfo::isFalkorShiftExtFast(const MachineInstr &MI) {
883   switch (MI.getOpcode()) {
884   default:
885     return false;
886 
887   case AArch64::ADDWrs:
888   case AArch64::ADDXrs:
889   case AArch64::ADDSWrs:
890   case AArch64::ADDSXrs: {
891     unsigned Imm = MI.getOperand(3).getImm();
892     unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
893     if (ShiftVal == 0)
894       return true;
895     return AArch64_AM::getShiftType(Imm) == AArch64_AM::LSL && ShiftVal <= 5;
896   }
897 
898   case AArch64::ADDWrx:
899   case AArch64::ADDXrx:
900   case AArch64::ADDXrx64:
901   case AArch64::ADDSWrx:
902   case AArch64::ADDSXrx:
903   case AArch64::ADDSXrx64: {
904     unsigned Imm = MI.getOperand(3).getImm();
905     switch (AArch64_AM::getArithExtendType(Imm)) {
906     default:
907       return false;
908     case AArch64_AM::UXTB:
909     case AArch64_AM::UXTH:
910     case AArch64_AM::UXTW:
911     case AArch64_AM::UXTX:
912       return AArch64_AM::getArithShiftValue(Imm) <= 4;
913     }
914   }
915 
916   case AArch64::SUBWrs:
917   case AArch64::SUBSWrs: {
918     unsigned Imm = MI.getOperand(3).getImm();
919     unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
920     return ShiftVal == 0 ||
921            (AArch64_AM::getShiftType(Imm) == AArch64_AM::ASR && ShiftVal == 31);
922   }
923 
924   case AArch64::SUBXrs:
925   case AArch64::SUBSXrs: {
926     unsigned Imm = MI.getOperand(3).getImm();
927     unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
928     return ShiftVal == 0 ||
929            (AArch64_AM::getShiftType(Imm) == AArch64_AM::ASR && ShiftVal == 63);
930   }
931 
932   case AArch64::SUBWrx:
933   case AArch64::SUBXrx:
934   case AArch64::SUBXrx64:
935   case AArch64::SUBSWrx:
936   case AArch64::SUBSXrx:
937   case AArch64::SUBSXrx64: {
938     unsigned Imm = MI.getOperand(3).getImm();
939     switch (AArch64_AM::getArithExtendType(Imm)) {
940     default:
941       return false;
942     case AArch64_AM::UXTB:
943     case AArch64_AM::UXTH:
944     case AArch64_AM::UXTW:
945     case AArch64_AM::UXTX:
946       return AArch64_AM::getArithShiftValue(Imm) == 0;
947     }
948   }
949 
950   case AArch64::LDRBBroW:
951   case AArch64::LDRBBroX:
952   case AArch64::LDRBroW:
953   case AArch64::LDRBroX:
954   case AArch64::LDRDroW:
955   case AArch64::LDRDroX:
956   case AArch64::LDRHHroW:
957   case AArch64::LDRHHroX:
958   case AArch64::LDRHroW:
959   case AArch64::LDRHroX:
960   case AArch64::LDRQroW:
961   case AArch64::LDRQroX:
962   case AArch64::LDRSBWroW:
963   case AArch64::LDRSBWroX:
964   case AArch64::LDRSBXroW:
965   case AArch64::LDRSBXroX:
966   case AArch64::LDRSHWroW:
967   case AArch64::LDRSHWroX:
968   case AArch64::LDRSHXroW:
969   case AArch64::LDRSHXroX:
970   case AArch64::LDRSWroW:
971   case AArch64::LDRSWroX:
972   case AArch64::LDRSroW:
973   case AArch64::LDRSroX:
974   case AArch64::LDRWroW:
975   case AArch64::LDRWroX:
976   case AArch64::LDRXroW:
977   case AArch64::LDRXroX:
978   case AArch64::PRFMroW:
979   case AArch64::PRFMroX:
980   case AArch64::STRBBroW:
981   case AArch64::STRBBroX:
982   case AArch64::STRBroW:
983   case AArch64::STRBroX:
984   case AArch64::STRDroW:
985   case AArch64::STRDroX:
986   case AArch64::STRHHroW:
987   case AArch64::STRHHroX:
988   case AArch64::STRHroW:
989   case AArch64::STRHroX:
990   case AArch64::STRQroW:
991   case AArch64::STRQroX:
992   case AArch64::STRSroW:
993   case AArch64::STRSroX:
994   case AArch64::STRWroW:
995   case AArch64::STRWroX:
996   case AArch64::STRXroW:
997   case AArch64::STRXroX: {
998     unsigned IsSigned = MI.getOperand(3).getImm();
999     return !IsSigned;
1000   }
1001   }
1002 }
1003 
1004 bool AArch64InstrInfo::isSEHInstruction(const MachineInstr &MI) {
1005   unsigned Opc = MI.getOpcode();
1006   switch (Opc) {
1007     default:
1008       return false;
1009     case AArch64::SEH_StackAlloc:
1010     case AArch64::SEH_SaveFPLR:
1011     case AArch64::SEH_SaveFPLR_X:
1012     case AArch64::SEH_SaveReg:
1013     case AArch64::SEH_SaveReg_X:
1014     case AArch64::SEH_SaveRegP:
1015     case AArch64::SEH_SaveRegP_X:
1016     case AArch64::SEH_SaveFReg:
1017     case AArch64::SEH_SaveFReg_X:
1018     case AArch64::SEH_SaveFRegP:
1019     case AArch64::SEH_SaveFRegP_X:
1020     case AArch64::SEH_SetFP:
1021     case AArch64::SEH_AddFP:
1022     case AArch64::SEH_Nop:
1023     case AArch64::SEH_PrologEnd:
1024     case AArch64::SEH_EpilogStart:
1025     case AArch64::SEH_EpilogEnd:
1026       return true;
1027   }
1028 }
1029 
1030 bool AArch64InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
1031                                              Register &SrcReg, Register &DstReg,
1032                                              unsigned &SubIdx) const {
1033   switch (MI.getOpcode()) {
1034   default:
1035     return false;
1036   case AArch64::SBFMXri: // aka sxtw
1037   case AArch64::UBFMXri: // aka uxtw
1038     // Check for the 32 -> 64 bit extension case, these instructions can do
1039     // much more.
1040     if (MI.getOperand(2).getImm() != 0 || MI.getOperand(3).getImm() != 31)
1041       return false;
1042     // This is a signed or unsigned 32 -> 64 bit extension.
1043     SrcReg = MI.getOperand(1).getReg();
1044     DstReg = MI.getOperand(0).getReg();
1045     SubIdx = AArch64::sub_32;
1046     return true;
1047   }
1048 }
1049 
1050 bool AArch64InstrInfo::areMemAccessesTriviallyDisjoint(
1051     const MachineInstr &MIa, const MachineInstr &MIb) const {
1052   const TargetRegisterInfo *TRI = &getRegisterInfo();
1053   const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
1054   int64_t OffsetA = 0, OffsetB = 0;
1055   unsigned WidthA = 0, WidthB = 0;
1056   bool OffsetAIsScalable = false, OffsetBIsScalable = false;
1057 
1058   assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
1059   assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
1060 
1061   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1062       MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
1063     return false;
1064 
1065   // Retrieve the base, offset from the base and width. Width
1066   // is the size of memory that is being loaded/stored (e.g. 1, 2, 4, 8).  If
1067   // base are identical, and the offset of a lower memory access +
1068   // the width doesn't overlap the offset of a higher memory access,
1069   // then the memory accesses are different.
1070   // If OffsetAIsScalable and OffsetBIsScalable are both true, they
1071   // are assumed to have the same scale (vscale).
1072   if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, OffsetAIsScalable,
1073                                    WidthA, TRI) &&
1074       getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, OffsetBIsScalable,
1075                                    WidthB, TRI)) {
1076     if (BaseOpA->isIdenticalTo(*BaseOpB) &&
1077         OffsetAIsScalable == OffsetBIsScalable) {
1078       int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
1079       int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
1080       int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1081       if (LowOffset + LowWidth <= HighOffset)
1082         return true;
1083     }
1084   }
1085   return false;
1086 }
1087 
1088 bool AArch64InstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1089                                             const MachineBasicBlock *MBB,
1090                                             const MachineFunction &MF) const {
1091   if (TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF))
1092     return true;
1093   switch (MI.getOpcode()) {
1094   case AArch64::HINT:
1095     // CSDB hints are scheduling barriers.
1096     if (MI.getOperand(0).getImm() == 0x14)
1097       return true;
1098     break;
1099   case AArch64::DSB:
1100   case AArch64::ISB:
1101     // DSB and ISB also are scheduling barriers.
1102     return true;
1103   default:;
1104   }
1105   return isSEHInstruction(MI);
1106 }
1107 
1108 /// analyzeCompare - For a comparison instruction, return the source registers
1109 /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
1110 /// Return true if the comparison instruction can be analyzed.
1111 bool AArch64InstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
1112                                       Register &SrcReg2, int &CmpMask,
1113                                       int &CmpValue) const {
1114   // The first operand can be a frame index where we'd normally expect a
1115   // register.
1116   assert(MI.getNumOperands() >= 2 && "All AArch64 cmps should have 2 operands");
1117   if (!MI.getOperand(1).isReg())
1118     return false;
1119 
1120   switch (MI.getOpcode()) {
1121   default:
1122     break;
1123   case AArch64::PTEST_PP:
1124     SrcReg = MI.getOperand(0).getReg();
1125     SrcReg2 = MI.getOperand(1).getReg();
1126     // Not sure about the mask and value for now...
1127     CmpMask = ~0;
1128     CmpValue = 0;
1129     return true;
1130   case AArch64::SUBSWrr:
1131   case AArch64::SUBSWrs:
1132   case AArch64::SUBSWrx:
1133   case AArch64::SUBSXrr:
1134   case AArch64::SUBSXrs:
1135   case AArch64::SUBSXrx:
1136   case AArch64::ADDSWrr:
1137   case AArch64::ADDSWrs:
1138   case AArch64::ADDSWrx:
1139   case AArch64::ADDSXrr:
1140   case AArch64::ADDSXrs:
1141   case AArch64::ADDSXrx:
1142     // Replace SUBSWrr with SUBWrr if NZCV is not used.
1143     SrcReg = MI.getOperand(1).getReg();
1144     SrcReg2 = MI.getOperand(2).getReg();
1145     CmpMask = ~0;
1146     CmpValue = 0;
1147     return true;
1148   case AArch64::SUBSWri:
1149   case AArch64::ADDSWri:
1150   case AArch64::SUBSXri:
1151   case AArch64::ADDSXri:
1152     SrcReg = MI.getOperand(1).getReg();
1153     SrcReg2 = 0;
1154     CmpMask = ~0;
1155     // FIXME: In order to convert CmpValue to 0 or 1
1156     CmpValue = MI.getOperand(2).getImm() != 0;
1157     return true;
1158   case AArch64::ANDSWri:
1159   case AArch64::ANDSXri:
1160     // ANDS does not use the same encoding scheme as the others xxxS
1161     // instructions.
1162     SrcReg = MI.getOperand(1).getReg();
1163     SrcReg2 = 0;
1164     CmpMask = ~0;
1165     // FIXME:The return val type of decodeLogicalImmediate is uint64_t,
1166     // while the type of CmpValue is int. When converting uint64_t to int,
1167     // the high 32 bits of uint64_t will be lost.
1168     // In fact it causes a bug in spec2006-483.xalancbmk
1169     // CmpValue is only used to compare with zero in OptimizeCompareInstr
1170     CmpValue = AArch64_AM::decodeLogicalImmediate(
1171                    MI.getOperand(2).getImm(),
1172                    MI.getOpcode() == AArch64::ANDSWri ? 32 : 64) != 0;
1173     return true;
1174   }
1175 
1176   return false;
1177 }
1178 
1179 static bool UpdateOperandRegClass(MachineInstr &Instr) {
1180   MachineBasicBlock *MBB = Instr.getParent();
1181   assert(MBB && "Can't get MachineBasicBlock here");
1182   MachineFunction *MF = MBB->getParent();
1183   assert(MF && "Can't get MachineFunction here");
1184   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1185   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
1186   MachineRegisterInfo *MRI = &MF->getRegInfo();
1187 
1188   for (unsigned OpIdx = 0, EndIdx = Instr.getNumOperands(); OpIdx < EndIdx;
1189        ++OpIdx) {
1190     MachineOperand &MO = Instr.getOperand(OpIdx);
1191     const TargetRegisterClass *OpRegCstraints =
1192         Instr.getRegClassConstraint(OpIdx, TII, TRI);
1193 
1194     // If there's no constraint, there's nothing to do.
1195     if (!OpRegCstraints)
1196       continue;
1197     // If the operand is a frame index, there's nothing to do here.
1198     // A frame index operand will resolve correctly during PEI.
1199     if (MO.isFI())
1200       continue;
1201 
1202     assert(MO.isReg() &&
1203            "Operand has register constraints without being a register!");
1204 
1205     Register Reg = MO.getReg();
1206     if (Register::isPhysicalRegister(Reg)) {
1207       if (!OpRegCstraints->contains(Reg))
1208         return false;
1209     } else if (!OpRegCstraints->hasSubClassEq(MRI->getRegClass(Reg)) &&
1210                !MRI->constrainRegClass(Reg, OpRegCstraints))
1211       return false;
1212   }
1213 
1214   return true;
1215 }
1216 
1217 /// Return the opcode that does not set flags when possible - otherwise
1218 /// return the original opcode. The caller is responsible to do the actual
1219 /// substitution and legality checking.
1220 static unsigned convertToNonFlagSettingOpc(const MachineInstr &MI) {
1221   // Don't convert all compare instructions, because for some the zero register
1222   // encoding becomes the sp register.
1223   bool MIDefinesZeroReg = false;
1224   if (MI.definesRegister(AArch64::WZR) || MI.definesRegister(AArch64::XZR))
1225     MIDefinesZeroReg = true;
1226 
1227   switch (MI.getOpcode()) {
1228   default:
1229     return MI.getOpcode();
1230   case AArch64::ADDSWrr:
1231     return AArch64::ADDWrr;
1232   case AArch64::ADDSWri:
1233     return MIDefinesZeroReg ? AArch64::ADDSWri : AArch64::ADDWri;
1234   case AArch64::ADDSWrs:
1235     return MIDefinesZeroReg ? AArch64::ADDSWrs : AArch64::ADDWrs;
1236   case AArch64::ADDSWrx:
1237     return AArch64::ADDWrx;
1238   case AArch64::ADDSXrr:
1239     return AArch64::ADDXrr;
1240   case AArch64::ADDSXri:
1241     return MIDefinesZeroReg ? AArch64::ADDSXri : AArch64::ADDXri;
1242   case AArch64::ADDSXrs:
1243     return MIDefinesZeroReg ? AArch64::ADDSXrs : AArch64::ADDXrs;
1244   case AArch64::ADDSXrx:
1245     return AArch64::ADDXrx;
1246   case AArch64::SUBSWrr:
1247     return AArch64::SUBWrr;
1248   case AArch64::SUBSWri:
1249     return MIDefinesZeroReg ? AArch64::SUBSWri : AArch64::SUBWri;
1250   case AArch64::SUBSWrs:
1251     return MIDefinesZeroReg ? AArch64::SUBSWrs : AArch64::SUBWrs;
1252   case AArch64::SUBSWrx:
1253     return AArch64::SUBWrx;
1254   case AArch64::SUBSXrr:
1255     return AArch64::SUBXrr;
1256   case AArch64::SUBSXri:
1257     return MIDefinesZeroReg ? AArch64::SUBSXri : AArch64::SUBXri;
1258   case AArch64::SUBSXrs:
1259     return MIDefinesZeroReg ? AArch64::SUBSXrs : AArch64::SUBXrs;
1260   case AArch64::SUBSXrx:
1261     return AArch64::SUBXrx;
1262   }
1263 }
1264 
1265 enum AccessKind { AK_Write = 0x01, AK_Read = 0x10, AK_All = 0x11 };
1266 
1267 /// True when condition flags are accessed (either by writing or reading)
1268 /// on the instruction trace starting at From and ending at To.
1269 ///
1270 /// Note: If From and To are from different blocks it's assumed CC are accessed
1271 ///       on the path.
1272 static bool areCFlagsAccessedBetweenInstrs(
1273     MachineBasicBlock::iterator From, MachineBasicBlock::iterator To,
1274     const TargetRegisterInfo *TRI, const AccessKind AccessToCheck = AK_All) {
1275   // Early exit if To is at the beginning of the BB.
1276   if (To == To->getParent()->begin())
1277     return true;
1278 
1279   // Check whether the instructions are in the same basic block
1280   // If not, assume the condition flags might get modified somewhere.
1281   if (To->getParent() != From->getParent())
1282     return true;
1283 
1284   // From must be above To.
1285   assert(std::any_of(
1286       ++To.getReverse(), To->getParent()->rend(),
1287       [From](MachineInstr &MI) { return MI.getIterator() == From; }));
1288 
1289   // We iterate backward starting at \p To until we hit \p From.
1290   for (const MachineInstr &Instr :
1291        instructionsWithoutDebug(++To.getReverse(), From.getReverse())) {
1292     if (((AccessToCheck & AK_Write) &&
1293          Instr.modifiesRegister(AArch64::NZCV, TRI)) ||
1294         ((AccessToCheck & AK_Read) && Instr.readsRegister(AArch64::NZCV, TRI)))
1295       return true;
1296   }
1297   return false;
1298 }
1299 
1300 /// optimizePTestInstr - Attempt to remove a ptest of a predicate-generating
1301 /// operation which could set the flags in an identical manner
1302 bool AArch64InstrInfo::optimizePTestInstr(
1303     MachineInstr *PTest, unsigned MaskReg, unsigned PredReg,
1304     const MachineRegisterInfo *MRI) const {
1305   auto *Mask = MRI->getUniqueVRegDef(MaskReg);
1306   auto *Pred = MRI->getUniqueVRegDef(PredReg);
1307   auto NewOp = Pred->getOpcode();
1308   bool OpChanged = false;
1309 
1310   unsigned MaskOpcode = Mask->getOpcode();
1311   unsigned PredOpcode = Pred->getOpcode();
1312   bool PredIsPTestLike = isPTestLikeOpcode(PredOpcode);
1313   bool PredIsWhileLike = isWhileOpcode(PredOpcode);
1314 
1315   if (isPTrueOpcode(MaskOpcode) && (PredIsPTestLike || PredIsWhileLike)) {
1316     // For PTEST(PTRUE, OTHER_INST), PTEST is redundant when PTRUE doesn't
1317     // deactivate any lanes OTHER_INST might set.
1318     uint64_t MaskElementSize = getElementSizeForOpcode(MaskOpcode);
1319     uint64_t PredElementSize = getElementSizeForOpcode(PredOpcode);
1320 
1321     // Must be an all active predicate of matching element size.
1322     if ((PredElementSize != MaskElementSize) ||
1323         (Mask->getOperand(1).getImm() != 31))
1324       return false;
1325 
1326     // Fallthough to simply remove the PTEST.
1327   } else if ((Mask == Pred) && (PredIsPTestLike || PredIsWhileLike)) {
1328     // For PTEST(PG, PG), PTEST is redundant when PG is the result of an
1329     // instruction that sets the flags as PTEST would.
1330 
1331     // Fallthough to simply remove the PTEST.
1332   } else if (PredIsPTestLike) {
1333     // For PTEST(PG_1, PTEST_LIKE(PG2, ...)), PTEST is redundant when both
1334     // instructions use the same predicate.
1335     auto PTestLikeMask = MRI->getUniqueVRegDef(Pred->getOperand(1).getReg());
1336     if (Mask != PTestLikeMask)
1337       return false;
1338 
1339     // Fallthough to simply remove the PTEST.
1340   } else {
1341     switch (Pred->getOpcode()) {
1342     case AArch64::BRKB_PPzP:
1343     case AArch64::BRKPB_PPzPP: {
1344       // Op 0 is chain, 1 is the mask, 2 the previous predicate to
1345       // propagate, 3 the new predicate.
1346 
1347       // Check to see if our mask is the same as the brkpb's. If
1348       // not the resulting flag bits may be different and we
1349       // can't remove the ptest.
1350       auto *PredMask = MRI->getUniqueVRegDef(Pred->getOperand(1).getReg());
1351       if (Mask != PredMask)
1352         return false;
1353 
1354       // Switch to the new opcode
1355       NewOp = Pred->getOpcode() == AArch64::BRKB_PPzP ? AArch64::BRKBS_PPzP
1356                                                       : AArch64::BRKPBS_PPzPP;
1357       OpChanged = true;
1358       break;
1359     }
1360     case AArch64::BRKN_PPzP: {
1361       auto *PredMask = MRI->getUniqueVRegDef(Pred->getOperand(1).getReg());
1362       if (Mask != PredMask)
1363         return false;
1364 
1365       NewOp = AArch64::BRKNS_PPzP;
1366       OpChanged = true;
1367       break;
1368     }
1369     default:
1370       // Bail out if we don't recognize the input
1371       return false;
1372     }
1373   }
1374 
1375   const TargetRegisterInfo *TRI = &getRegisterInfo();
1376 
1377   // If the predicate is in a different block (possibly because its been
1378   // hoisted out), then assume the flags are set in between statements.
1379   if (Pred->getParent() != PTest->getParent())
1380     return false;
1381 
1382   // If another instruction between the propagation and test sets the
1383   // flags, don't remove the ptest.
1384   MachineBasicBlock::iterator I = Pred, E = PTest;
1385   ++I; // Skip past the predicate op itself.
1386   for (; I != E; ++I) {
1387     const MachineInstr &Inst = *I;
1388 
1389     // TODO: If the ptest flags are unused, we could still remove it.
1390     if (Inst.modifiesRegister(AArch64::NZCV, TRI))
1391       return false;
1392   }
1393 
1394   // If we pass all the checks, it's safe to remove the PTEST and use the flags
1395   // as they are prior to PTEST. Sometimes this requires the tested PTEST
1396   // operand to be replaced with an equivalent instruction that also sets the
1397   // flags.
1398   Pred->setDesc(get(NewOp));
1399   PTest->eraseFromParent();
1400   if (OpChanged) {
1401     bool succeeded = UpdateOperandRegClass(*Pred);
1402     (void)succeeded;
1403     assert(succeeded && "Operands have incompatible register classes!");
1404     Pred->addRegisterDefined(AArch64::NZCV, TRI);
1405   }
1406 
1407   // Ensure that the flags def is live.
1408   if (Pred->registerDefIsDead(AArch64::NZCV, TRI)) {
1409     unsigned i = 0, e = Pred->getNumOperands();
1410     for (; i != e; ++i) {
1411       MachineOperand &MO = Pred->getOperand(i);
1412       if (MO.isReg() && MO.isDef() && MO.getReg() == AArch64::NZCV) {
1413         MO.setIsDead(false);
1414         break;
1415       }
1416     }
1417   }
1418   return true;
1419 }
1420 
1421 /// Try to optimize a compare instruction. A compare instruction is an
1422 /// instruction which produces AArch64::NZCV. It can be truly compare
1423 /// instruction
1424 /// when there are no uses of its destination register.
1425 ///
1426 /// The following steps are tried in order:
1427 /// 1. Convert CmpInstr into an unconditional version.
1428 /// 2. Remove CmpInstr if above there is an instruction producing a needed
1429 ///    condition code or an instruction which can be converted into such an
1430 ///    instruction.
1431 ///    Only comparison with zero is supported.
1432 bool AArch64InstrInfo::optimizeCompareInstr(
1433     MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int CmpMask,
1434     int CmpValue, const MachineRegisterInfo *MRI) const {
1435   assert(CmpInstr.getParent());
1436   assert(MRI);
1437 
1438   // Replace SUBSWrr with SUBWrr if NZCV is not used.
1439   int DeadNZCVIdx = CmpInstr.findRegisterDefOperandIdx(AArch64::NZCV, true);
1440   if (DeadNZCVIdx != -1) {
1441     if (CmpInstr.definesRegister(AArch64::WZR) ||
1442         CmpInstr.definesRegister(AArch64::XZR)) {
1443       CmpInstr.eraseFromParent();
1444       return true;
1445     }
1446     unsigned Opc = CmpInstr.getOpcode();
1447     unsigned NewOpc = convertToNonFlagSettingOpc(CmpInstr);
1448     if (NewOpc == Opc)
1449       return false;
1450     const MCInstrDesc &MCID = get(NewOpc);
1451     CmpInstr.setDesc(MCID);
1452     CmpInstr.RemoveOperand(DeadNZCVIdx);
1453     bool succeeded = UpdateOperandRegClass(CmpInstr);
1454     (void)succeeded;
1455     assert(succeeded && "Some operands reg class are incompatible!");
1456     return true;
1457   }
1458 
1459   if (CmpInstr.getOpcode() == AArch64::PTEST_PP)
1460     return optimizePTestInstr(&CmpInstr, SrcReg, SrcReg2, MRI);
1461 
1462   // Continue only if we have a "ri" where immediate is zero.
1463   // FIXME:CmpValue has already been converted to 0 or 1 in analyzeCompare
1464   // function.
1465   assert((CmpValue == 0 || CmpValue == 1) && "CmpValue must be 0 or 1!");
1466   if (CmpValue != 0 || SrcReg2 != 0)
1467     return false;
1468 
1469   // CmpInstr is a Compare instruction if destination register is not used.
1470   if (!MRI->use_nodbg_empty(CmpInstr.getOperand(0).getReg()))
1471     return false;
1472 
1473   return substituteCmpToZero(CmpInstr, SrcReg, MRI);
1474 }
1475 
1476 /// Get opcode of S version of Instr.
1477 /// If Instr is S version its opcode is returned.
1478 /// AArch64::INSTRUCTION_LIST_END is returned if Instr does not have S version
1479 /// or we are not interested in it.
1480 static unsigned sForm(MachineInstr &Instr) {
1481   switch (Instr.getOpcode()) {
1482   default:
1483     return AArch64::INSTRUCTION_LIST_END;
1484 
1485   case AArch64::ADDSWrr:
1486   case AArch64::ADDSWri:
1487   case AArch64::ADDSXrr:
1488   case AArch64::ADDSXri:
1489   case AArch64::SUBSWrr:
1490   case AArch64::SUBSWri:
1491   case AArch64::SUBSXrr:
1492   case AArch64::SUBSXri:
1493     return Instr.getOpcode();
1494 
1495   case AArch64::ADDWrr:
1496     return AArch64::ADDSWrr;
1497   case AArch64::ADDWri:
1498     return AArch64::ADDSWri;
1499   case AArch64::ADDXrr:
1500     return AArch64::ADDSXrr;
1501   case AArch64::ADDXri:
1502     return AArch64::ADDSXri;
1503   case AArch64::ADCWr:
1504     return AArch64::ADCSWr;
1505   case AArch64::ADCXr:
1506     return AArch64::ADCSXr;
1507   case AArch64::SUBWrr:
1508     return AArch64::SUBSWrr;
1509   case AArch64::SUBWri:
1510     return AArch64::SUBSWri;
1511   case AArch64::SUBXrr:
1512     return AArch64::SUBSXrr;
1513   case AArch64::SUBXri:
1514     return AArch64::SUBSXri;
1515   case AArch64::SBCWr:
1516     return AArch64::SBCSWr;
1517   case AArch64::SBCXr:
1518     return AArch64::SBCSXr;
1519   case AArch64::ANDWri:
1520     return AArch64::ANDSWri;
1521   case AArch64::ANDXri:
1522     return AArch64::ANDSXri;
1523   }
1524 }
1525 
1526 /// Check if AArch64::NZCV should be alive in successors of MBB.
1527 static bool areCFlagsAliveInSuccessors(MachineBasicBlock *MBB) {
1528   for (auto *BB : MBB->successors())
1529     if (BB->isLiveIn(AArch64::NZCV))
1530       return true;
1531   return false;
1532 }
1533 
1534 namespace {
1535 
1536 struct UsedNZCV {
1537   bool N = false;
1538   bool Z = false;
1539   bool C = false;
1540   bool V = false;
1541 
1542   UsedNZCV() = default;
1543 
1544   UsedNZCV &operator|=(const UsedNZCV &UsedFlags) {
1545     this->N |= UsedFlags.N;
1546     this->Z |= UsedFlags.Z;
1547     this->C |= UsedFlags.C;
1548     this->V |= UsedFlags.V;
1549     return *this;
1550   }
1551 };
1552 
1553 } // end anonymous namespace
1554 
1555 /// Find a condition code used by the instruction.
1556 /// Returns AArch64CC::Invalid if either the instruction does not use condition
1557 /// codes or we don't optimize CmpInstr in the presence of such instructions.
1558 static AArch64CC::CondCode findCondCodeUsedByInstr(const MachineInstr &Instr) {
1559   switch (Instr.getOpcode()) {
1560   default:
1561     return AArch64CC::Invalid;
1562 
1563   case AArch64::Bcc: {
1564     int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
1565     assert(Idx >= 2);
1566     return static_cast<AArch64CC::CondCode>(Instr.getOperand(Idx - 2).getImm());
1567   }
1568 
1569   case AArch64::CSINVWr:
1570   case AArch64::CSINVXr:
1571   case AArch64::CSINCWr:
1572   case AArch64::CSINCXr:
1573   case AArch64::CSELWr:
1574   case AArch64::CSELXr:
1575   case AArch64::CSNEGWr:
1576   case AArch64::CSNEGXr:
1577   case AArch64::FCSELSrrr:
1578   case AArch64::FCSELDrrr: {
1579     int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
1580     assert(Idx >= 1);
1581     return static_cast<AArch64CC::CondCode>(Instr.getOperand(Idx - 1).getImm());
1582   }
1583   }
1584 }
1585 
1586 static UsedNZCV getUsedNZCV(AArch64CC::CondCode CC) {
1587   assert(CC != AArch64CC::Invalid);
1588   UsedNZCV UsedFlags;
1589   switch (CC) {
1590   default:
1591     break;
1592 
1593   case AArch64CC::EQ: // Z set
1594   case AArch64CC::NE: // Z clear
1595     UsedFlags.Z = true;
1596     break;
1597 
1598   case AArch64CC::HI: // Z clear and C set
1599   case AArch64CC::LS: // Z set   or  C clear
1600     UsedFlags.Z = true;
1601     LLVM_FALLTHROUGH;
1602   case AArch64CC::HS: // C set
1603   case AArch64CC::LO: // C clear
1604     UsedFlags.C = true;
1605     break;
1606 
1607   case AArch64CC::MI: // N set
1608   case AArch64CC::PL: // N clear
1609     UsedFlags.N = true;
1610     break;
1611 
1612   case AArch64CC::VS: // V set
1613   case AArch64CC::VC: // V clear
1614     UsedFlags.V = true;
1615     break;
1616 
1617   case AArch64CC::GT: // Z clear, N and V the same
1618   case AArch64CC::LE: // Z set,   N and V differ
1619     UsedFlags.Z = true;
1620     LLVM_FALLTHROUGH;
1621   case AArch64CC::GE: // N and V the same
1622   case AArch64CC::LT: // N and V differ
1623     UsedFlags.N = true;
1624     UsedFlags.V = true;
1625     break;
1626   }
1627   return UsedFlags;
1628 }
1629 
1630 static bool isADDSRegImm(unsigned Opcode) {
1631   return Opcode == AArch64::ADDSWri || Opcode == AArch64::ADDSXri;
1632 }
1633 
1634 static bool isSUBSRegImm(unsigned Opcode) {
1635   return Opcode == AArch64::SUBSWri || Opcode == AArch64::SUBSXri;
1636 }
1637 
1638 /// Check if CmpInstr can be substituted by MI.
1639 ///
1640 /// CmpInstr can be substituted:
1641 /// - CmpInstr is either 'ADDS %vreg, 0' or 'SUBS %vreg, 0'
1642 /// - and, MI and CmpInstr are from the same MachineBB
1643 /// - and, condition flags are not alive in successors of the CmpInstr parent
1644 /// - and, if MI opcode is the S form there must be no defs of flags between
1645 ///        MI and CmpInstr
1646 ///        or if MI opcode is not the S form there must be neither defs of flags
1647 ///        nor uses of flags between MI and CmpInstr.
1648 /// - and  C/V flags are not used after CmpInstr
1649 static bool canInstrSubstituteCmpInstr(MachineInstr *MI, MachineInstr *CmpInstr,
1650                                        const TargetRegisterInfo *TRI) {
1651   assert(MI);
1652   assert(sForm(*MI) != AArch64::INSTRUCTION_LIST_END);
1653   assert(CmpInstr);
1654 
1655   const unsigned CmpOpcode = CmpInstr->getOpcode();
1656   if (!isADDSRegImm(CmpOpcode) && !isSUBSRegImm(CmpOpcode))
1657     return false;
1658 
1659   if (MI->getParent() != CmpInstr->getParent())
1660     return false;
1661 
1662   if (areCFlagsAliveInSuccessors(CmpInstr->getParent()))
1663     return false;
1664 
1665   AccessKind AccessToCheck = AK_Write;
1666   if (sForm(*MI) != MI->getOpcode())
1667     AccessToCheck = AK_All;
1668   if (areCFlagsAccessedBetweenInstrs(MI, CmpInstr, TRI, AccessToCheck))
1669     return false;
1670 
1671   UsedNZCV NZCVUsedAfterCmp;
1672   for (const MachineInstr &Instr :
1673        instructionsWithoutDebug(std::next(CmpInstr->getIterator()),
1674                                 CmpInstr->getParent()->instr_end())) {
1675     if (Instr.readsRegister(AArch64::NZCV, TRI)) {
1676       AArch64CC::CondCode CC = findCondCodeUsedByInstr(Instr);
1677       if (CC == AArch64CC::Invalid) // Unsupported conditional instruction
1678         return false;
1679       NZCVUsedAfterCmp |= getUsedNZCV(CC);
1680     }
1681 
1682     if (Instr.modifiesRegister(AArch64::NZCV, TRI))
1683       break;
1684   }
1685 
1686   return !NZCVUsedAfterCmp.C && !NZCVUsedAfterCmp.V;
1687 }
1688 
1689 /// Substitute an instruction comparing to zero with another instruction
1690 /// which produces needed condition flags.
1691 ///
1692 /// Return true on success.
1693 bool AArch64InstrInfo::substituteCmpToZero(
1694     MachineInstr &CmpInstr, unsigned SrcReg,
1695     const MachineRegisterInfo *MRI) const {
1696   assert(MRI);
1697   // Get the unique definition of SrcReg.
1698   MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1699   if (!MI)
1700     return false;
1701 
1702   const TargetRegisterInfo *TRI = &getRegisterInfo();
1703 
1704   unsigned NewOpc = sForm(*MI);
1705   if (NewOpc == AArch64::INSTRUCTION_LIST_END)
1706     return false;
1707 
1708   if (!canInstrSubstituteCmpInstr(MI, &CmpInstr, TRI))
1709     return false;
1710 
1711   // Update the instruction to set NZCV.
1712   MI->setDesc(get(NewOpc));
1713   CmpInstr.eraseFromParent();
1714   bool succeeded = UpdateOperandRegClass(*MI);
1715   (void)succeeded;
1716   assert(succeeded && "Some operands reg class are incompatible!");
1717   MI->addRegisterDefined(AArch64::NZCV, TRI);
1718   return true;
1719 }
1720 
1721 bool AArch64InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1722   if (MI.getOpcode() != TargetOpcode::LOAD_STACK_GUARD &&
1723       MI.getOpcode() != AArch64::CATCHRET)
1724     return false;
1725 
1726   MachineBasicBlock &MBB = *MI.getParent();
1727   auto &Subtarget = MBB.getParent()->getSubtarget<AArch64Subtarget>();
1728   auto TRI = Subtarget.getRegisterInfo();
1729   DebugLoc DL = MI.getDebugLoc();
1730 
1731   if (MI.getOpcode() == AArch64::CATCHRET) {
1732     // Skip to the first instruction before the epilog.
1733     const TargetInstrInfo *TII =
1734       MBB.getParent()->getSubtarget().getInstrInfo();
1735     MachineBasicBlock *TargetMBB = MI.getOperand(0).getMBB();
1736     auto MBBI = MachineBasicBlock::iterator(MI);
1737     MachineBasicBlock::iterator FirstEpilogSEH = std::prev(MBBI);
1738     while (FirstEpilogSEH->getFlag(MachineInstr::FrameDestroy) &&
1739            FirstEpilogSEH != MBB.begin())
1740       FirstEpilogSEH = std::prev(FirstEpilogSEH);
1741     if (FirstEpilogSEH != MBB.begin())
1742       FirstEpilogSEH = std::next(FirstEpilogSEH);
1743     BuildMI(MBB, FirstEpilogSEH, DL, TII->get(AArch64::ADRP))
1744         .addReg(AArch64::X0, RegState::Define)
1745         .addMBB(TargetMBB);
1746     BuildMI(MBB, FirstEpilogSEH, DL, TII->get(AArch64::ADDXri))
1747         .addReg(AArch64::X0, RegState::Define)
1748         .addReg(AArch64::X0)
1749         .addMBB(TargetMBB)
1750         .addImm(0);
1751     return true;
1752   }
1753 
1754   Register Reg = MI.getOperand(0).getReg();
1755   const GlobalValue *GV =
1756       cast<GlobalValue>((*MI.memoperands_begin())->getValue());
1757   const TargetMachine &TM = MBB.getParent()->getTarget();
1758   unsigned OpFlags = Subtarget.ClassifyGlobalReference(GV, TM);
1759   const unsigned char MO_NC = AArch64II::MO_NC;
1760 
1761   if ((OpFlags & AArch64II::MO_GOT) != 0) {
1762     BuildMI(MBB, MI, DL, get(AArch64::LOADgot), Reg)
1763         .addGlobalAddress(GV, 0, OpFlags);
1764     if (Subtarget.isTargetILP32()) {
1765       unsigned Reg32 = TRI->getSubReg(Reg, AArch64::sub_32);
1766       BuildMI(MBB, MI, DL, get(AArch64::LDRWui))
1767           .addDef(Reg32, RegState::Dead)
1768           .addUse(Reg, RegState::Kill)
1769           .addImm(0)
1770           .addMemOperand(*MI.memoperands_begin())
1771           .addDef(Reg, RegState::Implicit);
1772     } else {
1773       BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1774           .addReg(Reg, RegState::Kill)
1775           .addImm(0)
1776           .addMemOperand(*MI.memoperands_begin());
1777     }
1778   } else if (TM.getCodeModel() == CodeModel::Large) {
1779     assert(!Subtarget.isTargetILP32() && "how can large exist in ILP32?");
1780     BuildMI(MBB, MI, DL, get(AArch64::MOVZXi), Reg)
1781         .addGlobalAddress(GV, 0, AArch64II::MO_G0 | MO_NC)
1782         .addImm(0);
1783     BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1784         .addReg(Reg, RegState::Kill)
1785         .addGlobalAddress(GV, 0, AArch64II::MO_G1 | MO_NC)
1786         .addImm(16);
1787     BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1788         .addReg(Reg, RegState::Kill)
1789         .addGlobalAddress(GV, 0, AArch64II::MO_G2 | MO_NC)
1790         .addImm(32);
1791     BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1792         .addReg(Reg, RegState::Kill)
1793         .addGlobalAddress(GV, 0, AArch64II::MO_G3)
1794         .addImm(48);
1795     BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1796         .addReg(Reg, RegState::Kill)
1797         .addImm(0)
1798         .addMemOperand(*MI.memoperands_begin());
1799   } else if (TM.getCodeModel() == CodeModel::Tiny) {
1800     BuildMI(MBB, MI, DL, get(AArch64::ADR), Reg)
1801         .addGlobalAddress(GV, 0, OpFlags);
1802   } else {
1803     BuildMI(MBB, MI, DL, get(AArch64::ADRP), Reg)
1804         .addGlobalAddress(GV, 0, OpFlags | AArch64II::MO_PAGE);
1805     unsigned char LoFlags = OpFlags | AArch64II::MO_PAGEOFF | MO_NC;
1806     if (Subtarget.isTargetILP32()) {
1807       unsigned Reg32 = TRI->getSubReg(Reg, AArch64::sub_32);
1808       BuildMI(MBB, MI, DL, get(AArch64::LDRWui))
1809           .addDef(Reg32, RegState::Dead)
1810           .addUse(Reg, RegState::Kill)
1811           .addGlobalAddress(GV, 0, LoFlags)
1812           .addMemOperand(*MI.memoperands_begin())
1813           .addDef(Reg, RegState::Implicit);
1814     } else {
1815       BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1816           .addReg(Reg, RegState::Kill)
1817           .addGlobalAddress(GV, 0, LoFlags)
1818           .addMemOperand(*MI.memoperands_begin());
1819     }
1820   }
1821 
1822   MBB.erase(MI);
1823 
1824   return true;
1825 }
1826 
1827 // Return true if this instruction simply sets its single destination register
1828 // to zero. This is equivalent to a register rename of the zero-register.
1829 bool AArch64InstrInfo::isGPRZero(const MachineInstr &MI) {
1830   switch (MI.getOpcode()) {
1831   default:
1832     break;
1833   case AArch64::MOVZWi:
1834   case AArch64::MOVZXi: // movz Rd, #0 (LSL #0)
1835     if (MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) {
1836       assert(MI.getDesc().getNumOperands() == 3 &&
1837              MI.getOperand(2).getImm() == 0 && "invalid MOVZi operands");
1838       return true;
1839     }
1840     break;
1841   case AArch64::ANDWri: // and Rd, Rzr, #imm
1842     return MI.getOperand(1).getReg() == AArch64::WZR;
1843   case AArch64::ANDXri:
1844     return MI.getOperand(1).getReg() == AArch64::XZR;
1845   case TargetOpcode::COPY:
1846     return MI.getOperand(1).getReg() == AArch64::WZR;
1847   }
1848   return false;
1849 }
1850 
1851 // Return true if this instruction simply renames a general register without
1852 // modifying bits.
1853 bool AArch64InstrInfo::isGPRCopy(const MachineInstr &MI) {
1854   switch (MI.getOpcode()) {
1855   default:
1856     break;
1857   case TargetOpcode::COPY: {
1858     // GPR32 copies will by lowered to ORRXrs
1859     Register DstReg = MI.getOperand(0).getReg();
1860     return (AArch64::GPR32RegClass.contains(DstReg) ||
1861             AArch64::GPR64RegClass.contains(DstReg));
1862   }
1863   case AArch64::ORRXrs: // orr Xd, Xzr, Xm (LSL #0)
1864     if (MI.getOperand(1).getReg() == AArch64::XZR) {
1865       assert(MI.getDesc().getNumOperands() == 4 &&
1866              MI.getOperand(3).getImm() == 0 && "invalid ORRrs operands");
1867       return true;
1868     }
1869     break;
1870   case AArch64::ADDXri: // add Xd, Xn, #0 (LSL #0)
1871     if (MI.getOperand(2).getImm() == 0) {
1872       assert(MI.getDesc().getNumOperands() == 4 &&
1873              MI.getOperand(3).getImm() == 0 && "invalid ADDXri operands");
1874       return true;
1875     }
1876     break;
1877   }
1878   return false;
1879 }
1880 
1881 // Return true if this instruction simply renames a general register without
1882 // modifying bits.
1883 bool AArch64InstrInfo::isFPRCopy(const MachineInstr &MI) {
1884   switch (MI.getOpcode()) {
1885   default:
1886     break;
1887   case TargetOpcode::COPY: {
1888     // FPR64 copies will by lowered to ORR.16b
1889     Register DstReg = MI.getOperand(0).getReg();
1890     return (AArch64::FPR64RegClass.contains(DstReg) ||
1891             AArch64::FPR128RegClass.contains(DstReg));
1892   }
1893   case AArch64::ORRv16i8:
1894     if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
1895       assert(MI.getDesc().getNumOperands() == 3 && MI.getOperand(0).isReg() &&
1896              "invalid ORRv16i8 operands");
1897       return true;
1898     }
1899     break;
1900   }
1901   return false;
1902 }
1903 
1904 unsigned AArch64InstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1905                                                int &FrameIndex) const {
1906   switch (MI.getOpcode()) {
1907   default:
1908     break;
1909   case AArch64::LDRWui:
1910   case AArch64::LDRXui:
1911   case AArch64::LDRBui:
1912   case AArch64::LDRHui:
1913   case AArch64::LDRSui:
1914   case AArch64::LDRDui:
1915   case AArch64::LDRQui:
1916     if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
1917         MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
1918       FrameIndex = MI.getOperand(1).getIndex();
1919       return MI.getOperand(0).getReg();
1920     }
1921     break;
1922   }
1923 
1924   return 0;
1925 }
1926 
1927 unsigned AArch64InstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1928                                               int &FrameIndex) const {
1929   switch (MI.getOpcode()) {
1930   default:
1931     break;
1932   case AArch64::STRWui:
1933   case AArch64::STRXui:
1934   case AArch64::STRBui:
1935   case AArch64::STRHui:
1936   case AArch64::STRSui:
1937   case AArch64::STRDui:
1938   case AArch64::STRQui:
1939   case AArch64::LDR_PXI:
1940   case AArch64::STR_PXI:
1941     if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
1942         MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
1943       FrameIndex = MI.getOperand(1).getIndex();
1944       return MI.getOperand(0).getReg();
1945     }
1946     break;
1947   }
1948   return 0;
1949 }
1950 
1951 /// Check all MachineMemOperands for a hint to suppress pairing.
1952 bool AArch64InstrInfo::isLdStPairSuppressed(const MachineInstr &MI) {
1953   return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
1954     return MMO->getFlags() & MOSuppressPair;
1955   });
1956 }
1957 
1958 /// Set a flag on the first MachineMemOperand to suppress pairing.
1959 void AArch64InstrInfo::suppressLdStPair(MachineInstr &MI) {
1960   if (MI.memoperands_empty())
1961     return;
1962   (*MI.memoperands_begin())->setFlags(MOSuppressPair);
1963 }
1964 
1965 /// Check all MachineMemOperands for a hint that the load/store is strided.
1966 bool AArch64InstrInfo::isStridedAccess(const MachineInstr &MI) {
1967   return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
1968     return MMO->getFlags() & MOStridedAccess;
1969   });
1970 }
1971 
1972 bool AArch64InstrInfo::isUnscaledLdSt(unsigned Opc) {
1973   switch (Opc) {
1974   default:
1975     return false;
1976   case AArch64::STURSi:
1977   case AArch64::STURDi:
1978   case AArch64::STURQi:
1979   case AArch64::STURBBi:
1980   case AArch64::STURHHi:
1981   case AArch64::STURWi:
1982   case AArch64::STURXi:
1983   case AArch64::LDURSi:
1984   case AArch64::LDURDi:
1985   case AArch64::LDURQi:
1986   case AArch64::LDURWi:
1987   case AArch64::LDURXi:
1988   case AArch64::LDURSWi:
1989   case AArch64::LDURHHi:
1990   case AArch64::LDURBBi:
1991   case AArch64::LDURSBWi:
1992   case AArch64::LDURSHWi:
1993     return true;
1994   }
1995 }
1996 
1997 Optional<unsigned> AArch64InstrInfo::getUnscaledLdSt(unsigned Opc) {
1998   switch (Opc) {
1999   default: return {};
2000   case AArch64::PRFMui: return AArch64::PRFUMi;
2001   case AArch64::LDRXui: return AArch64::LDURXi;
2002   case AArch64::LDRWui: return AArch64::LDURWi;
2003   case AArch64::LDRBui: return AArch64::LDURBi;
2004   case AArch64::LDRHui: return AArch64::LDURHi;
2005   case AArch64::LDRSui: return AArch64::LDURSi;
2006   case AArch64::LDRDui: return AArch64::LDURDi;
2007   case AArch64::LDRQui: return AArch64::LDURQi;
2008   case AArch64::LDRBBui: return AArch64::LDURBBi;
2009   case AArch64::LDRHHui: return AArch64::LDURHHi;
2010   case AArch64::LDRSBXui: return AArch64::LDURSBXi;
2011   case AArch64::LDRSBWui: return AArch64::LDURSBWi;
2012   case AArch64::LDRSHXui: return AArch64::LDURSHXi;
2013   case AArch64::LDRSHWui: return AArch64::LDURSHWi;
2014   case AArch64::LDRSWui: return AArch64::LDURSWi;
2015   case AArch64::STRXui: return AArch64::STURXi;
2016   case AArch64::STRWui: return AArch64::STURWi;
2017   case AArch64::STRBui: return AArch64::STURBi;
2018   case AArch64::STRHui: return AArch64::STURHi;
2019   case AArch64::STRSui: return AArch64::STURSi;
2020   case AArch64::STRDui: return AArch64::STURDi;
2021   case AArch64::STRQui: return AArch64::STURQi;
2022   case AArch64::STRBBui: return AArch64::STURBBi;
2023   case AArch64::STRHHui: return AArch64::STURHHi;
2024   }
2025 }
2026 
2027 unsigned AArch64InstrInfo::getLoadStoreImmIdx(unsigned Opc) {
2028   switch (Opc) {
2029   default:
2030     return 2;
2031   case AArch64::LDPXi:
2032   case AArch64::LDPDi:
2033   case AArch64::STPXi:
2034   case AArch64::STPDi:
2035   case AArch64::LDNPXi:
2036   case AArch64::LDNPDi:
2037   case AArch64::STNPXi:
2038   case AArch64::STNPDi:
2039   case AArch64::LDPQi:
2040   case AArch64::STPQi:
2041   case AArch64::LDNPQi:
2042   case AArch64::STNPQi:
2043   case AArch64::LDPWi:
2044   case AArch64::LDPSi:
2045   case AArch64::STPWi:
2046   case AArch64::STPSi:
2047   case AArch64::LDNPWi:
2048   case AArch64::LDNPSi:
2049   case AArch64::STNPWi:
2050   case AArch64::STNPSi:
2051   case AArch64::LDG:
2052   case AArch64::STGPi:
2053   case AArch64::LD1B_IMM:
2054   case AArch64::LD1H_IMM:
2055   case AArch64::LD1W_IMM:
2056   case AArch64::LD1D_IMM:
2057   case AArch64::ST1B_IMM:
2058   case AArch64::ST1H_IMM:
2059   case AArch64::ST1W_IMM:
2060   case AArch64::ST1D_IMM:
2061   case AArch64::LD1B_H_IMM:
2062   case AArch64::LD1SB_H_IMM:
2063   case AArch64::LD1H_S_IMM:
2064   case AArch64::LD1SH_S_IMM:
2065   case AArch64::LD1W_D_IMM:
2066   case AArch64::LD1SW_D_IMM:
2067   case AArch64::ST1B_H_IMM:
2068   case AArch64::ST1H_S_IMM:
2069   case AArch64::ST1W_D_IMM:
2070   case AArch64::LD1B_S_IMM:
2071   case AArch64::LD1SB_S_IMM:
2072   case AArch64::LD1H_D_IMM:
2073   case AArch64::LD1SH_D_IMM:
2074   case AArch64::ST1B_S_IMM:
2075   case AArch64::ST1H_D_IMM:
2076   case AArch64::LD1B_D_IMM:
2077   case AArch64::LD1SB_D_IMM:
2078   case AArch64::ST1B_D_IMM:
2079     return 3;
2080   case AArch64::ADDG:
2081   case AArch64::STGOffset:
2082   case AArch64::LDR_PXI:
2083   case AArch64::STR_PXI:
2084     return 2;
2085   }
2086 }
2087 
2088 bool AArch64InstrInfo::isPairableLdStInst(const MachineInstr &MI) {
2089   switch (MI.getOpcode()) {
2090   default:
2091     return false;
2092   // Scaled instructions.
2093   case AArch64::STRSui:
2094   case AArch64::STRDui:
2095   case AArch64::STRQui:
2096   case AArch64::STRXui:
2097   case AArch64::STRWui:
2098   case AArch64::LDRSui:
2099   case AArch64::LDRDui:
2100   case AArch64::LDRQui:
2101   case AArch64::LDRXui:
2102   case AArch64::LDRWui:
2103   case AArch64::LDRSWui:
2104   // Unscaled instructions.
2105   case AArch64::STURSi:
2106   case AArch64::STURDi:
2107   case AArch64::STURQi:
2108   case AArch64::STURWi:
2109   case AArch64::STURXi:
2110   case AArch64::LDURSi:
2111   case AArch64::LDURDi:
2112   case AArch64::LDURQi:
2113   case AArch64::LDURWi:
2114   case AArch64::LDURXi:
2115   case AArch64::LDURSWi:
2116     return true;
2117   }
2118 }
2119 
2120 unsigned AArch64InstrInfo::convertToFlagSettingOpc(unsigned Opc,
2121                                                    bool &Is64Bit) {
2122   switch (Opc) {
2123   default:
2124     llvm_unreachable("Opcode has no flag setting equivalent!");
2125   // 32-bit cases:
2126   case AArch64::ADDWri:
2127     Is64Bit = false;
2128     return AArch64::ADDSWri;
2129   case AArch64::ADDWrr:
2130     Is64Bit = false;
2131     return AArch64::ADDSWrr;
2132   case AArch64::ADDWrs:
2133     Is64Bit = false;
2134     return AArch64::ADDSWrs;
2135   case AArch64::ADDWrx:
2136     Is64Bit = false;
2137     return AArch64::ADDSWrx;
2138   case AArch64::ANDWri:
2139     Is64Bit = false;
2140     return AArch64::ANDSWri;
2141   case AArch64::ANDWrr:
2142     Is64Bit = false;
2143     return AArch64::ANDSWrr;
2144   case AArch64::ANDWrs:
2145     Is64Bit = false;
2146     return AArch64::ANDSWrs;
2147   case AArch64::BICWrr:
2148     Is64Bit = false;
2149     return AArch64::BICSWrr;
2150   case AArch64::BICWrs:
2151     Is64Bit = false;
2152     return AArch64::BICSWrs;
2153   case AArch64::SUBWri:
2154     Is64Bit = false;
2155     return AArch64::SUBSWri;
2156   case AArch64::SUBWrr:
2157     Is64Bit = false;
2158     return AArch64::SUBSWrr;
2159   case AArch64::SUBWrs:
2160     Is64Bit = false;
2161     return AArch64::SUBSWrs;
2162   case AArch64::SUBWrx:
2163     Is64Bit = false;
2164     return AArch64::SUBSWrx;
2165   // 64-bit cases:
2166   case AArch64::ADDXri:
2167     Is64Bit = true;
2168     return AArch64::ADDSXri;
2169   case AArch64::ADDXrr:
2170     Is64Bit = true;
2171     return AArch64::ADDSXrr;
2172   case AArch64::ADDXrs:
2173     Is64Bit = true;
2174     return AArch64::ADDSXrs;
2175   case AArch64::ADDXrx:
2176     Is64Bit = true;
2177     return AArch64::ADDSXrx;
2178   case AArch64::ANDXri:
2179     Is64Bit = true;
2180     return AArch64::ANDSXri;
2181   case AArch64::ANDXrr:
2182     Is64Bit = true;
2183     return AArch64::ANDSXrr;
2184   case AArch64::ANDXrs:
2185     Is64Bit = true;
2186     return AArch64::ANDSXrs;
2187   case AArch64::BICXrr:
2188     Is64Bit = true;
2189     return AArch64::BICSXrr;
2190   case AArch64::BICXrs:
2191     Is64Bit = true;
2192     return AArch64::BICSXrs;
2193   case AArch64::SUBXri:
2194     Is64Bit = true;
2195     return AArch64::SUBSXri;
2196   case AArch64::SUBXrr:
2197     Is64Bit = true;
2198     return AArch64::SUBSXrr;
2199   case AArch64::SUBXrs:
2200     Is64Bit = true;
2201     return AArch64::SUBSXrs;
2202   case AArch64::SUBXrx:
2203     Is64Bit = true;
2204     return AArch64::SUBSXrx;
2205   }
2206 }
2207 
2208 // Is this a candidate for ld/st merging or pairing?  For example, we don't
2209 // touch volatiles or load/stores that have a hint to avoid pair formation.
2210 bool AArch64InstrInfo::isCandidateToMergeOrPair(const MachineInstr &MI) const {
2211   // If this is a volatile load/store, don't mess with it.
2212   if (MI.hasOrderedMemoryRef())
2213     return false;
2214 
2215   // Make sure this is a reg/fi+imm (as opposed to an address reloc).
2216   assert((MI.getOperand(1).isReg() || MI.getOperand(1).isFI()) &&
2217          "Expected a reg or frame index operand.");
2218   if (!MI.getOperand(2).isImm())
2219     return false;
2220 
2221   // Can't merge/pair if the instruction modifies the base register.
2222   // e.g., ldr x0, [x0]
2223   // This case will never occur with an FI base.
2224   if (MI.getOperand(1).isReg()) {
2225     Register BaseReg = MI.getOperand(1).getReg();
2226     const TargetRegisterInfo *TRI = &getRegisterInfo();
2227     if (MI.modifiesRegister(BaseReg, TRI))
2228       return false;
2229   }
2230 
2231   // Check if this load/store has a hint to avoid pair formation.
2232   // MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
2233   if (isLdStPairSuppressed(MI))
2234     return false;
2235 
2236   // Do not pair any callee-save store/reload instructions in the
2237   // prologue/epilogue if the CFI information encoded the operations as separate
2238   // instructions, as that will cause the size of the actual prologue to mismatch
2239   // with the prologue size recorded in the Windows CFI.
2240   const MCAsmInfo *MAI = MI.getMF()->getTarget().getMCAsmInfo();
2241   bool NeedsWinCFI = MAI->usesWindowsCFI() &&
2242                      MI.getMF()->getFunction().needsUnwindTableEntry();
2243   if (NeedsWinCFI && (MI.getFlag(MachineInstr::FrameSetup) ||
2244                       MI.getFlag(MachineInstr::FrameDestroy)))
2245     return false;
2246 
2247   // On some CPUs quad load/store pairs are slower than two single load/stores.
2248   if (Subtarget.isPaired128Slow()) {
2249     switch (MI.getOpcode()) {
2250     default:
2251       break;
2252     case AArch64::LDURQi:
2253     case AArch64::STURQi:
2254     case AArch64::LDRQui:
2255     case AArch64::STRQui:
2256       return false;
2257     }
2258   }
2259 
2260   return true;
2261 }
2262 
2263 bool AArch64InstrInfo::getMemOperandsWithOffsetWidth(
2264     const MachineInstr &LdSt, SmallVectorImpl<const MachineOperand *> &BaseOps,
2265     int64_t &Offset, bool &OffsetIsScalable, unsigned &Width,
2266     const TargetRegisterInfo *TRI) const {
2267   if (!LdSt.mayLoadOrStore())
2268     return false;
2269 
2270   const MachineOperand *BaseOp;
2271   if (!getMemOperandWithOffsetWidth(LdSt, BaseOp, Offset, OffsetIsScalable,
2272                                     Width, TRI))
2273     return false;
2274   BaseOps.push_back(BaseOp);
2275   return true;
2276 }
2277 
2278 Optional<ExtAddrMode>
2279 AArch64InstrInfo::getAddrModeFromMemoryOp(const MachineInstr &MemI,
2280                                           const TargetRegisterInfo *TRI) const {
2281   const MachineOperand *Base; // Filled with the base operand of MI.
2282   int64_t Offset;             // Filled with the offset of MI.
2283   bool OffsetIsScalable;
2284   if (!getMemOperandWithOffset(MemI, Base, Offset, OffsetIsScalable, TRI))
2285     return None;
2286 
2287   if (!Base->isReg())
2288     return None;
2289   ExtAddrMode AM;
2290   AM.BaseReg = Base->getReg();
2291   AM.Displacement = Offset;
2292   AM.ScaledReg = 0;
2293   return AM;
2294 }
2295 
2296 bool AArch64InstrInfo::getMemOperandWithOffsetWidth(
2297     const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset,
2298     bool &OffsetIsScalable, unsigned &Width,
2299     const TargetRegisterInfo *TRI) const {
2300   assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
2301   // Handle only loads/stores with base register followed by immediate offset.
2302   if (LdSt.getNumExplicitOperands() == 3) {
2303     // Non-paired instruction (e.g., ldr x1, [x0, #8]).
2304     if ((!LdSt.getOperand(1).isReg() && !LdSt.getOperand(1).isFI()) ||
2305         !LdSt.getOperand(2).isImm())
2306       return false;
2307   } else if (LdSt.getNumExplicitOperands() == 4) {
2308     // Paired instruction (e.g., ldp x1, x2, [x0, #8]).
2309     if (!LdSt.getOperand(1).isReg() ||
2310         (!LdSt.getOperand(2).isReg() && !LdSt.getOperand(2).isFI()) ||
2311         !LdSt.getOperand(3).isImm())
2312       return false;
2313   } else
2314     return false;
2315 
2316   // Get the scaling factor for the instruction and set the width for the
2317   // instruction.
2318   TypeSize Scale(0U, false);
2319   int64_t Dummy1, Dummy2;
2320 
2321   // If this returns false, then it's an instruction we don't want to handle.
2322   if (!getMemOpInfo(LdSt.getOpcode(), Scale, Width, Dummy1, Dummy2))
2323     return false;
2324 
2325   // Compute the offset. Offset is calculated as the immediate operand
2326   // multiplied by the scaling factor. Unscaled instructions have scaling factor
2327   // set to 1.
2328   if (LdSt.getNumExplicitOperands() == 3) {
2329     BaseOp = &LdSt.getOperand(1);
2330     Offset = LdSt.getOperand(2).getImm() * Scale.getKnownMinSize();
2331   } else {
2332     assert(LdSt.getNumExplicitOperands() == 4 && "invalid number of operands");
2333     BaseOp = &LdSt.getOperand(2);
2334     Offset = LdSt.getOperand(3).getImm() * Scale.getKnownMinSize();
2335   }
2336   OffsetIsScalable = Scale.isScalable();
2337 
2338   if (!BaseOp->isReg() && !BaseOp->isFI())
2339     return false;
2340 
2341   return true;
2342 }
2343 
2344 MachineOperand &
2345 AArch64InstrInfo::getMemOpBaseRegImmOfsOffsetOperand(MachineInstr &LdSt) const {
2346   assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
2347   MachineOperand &OfsOp = LdSt.getOperand(LdSt.getNumExplicitOperands() - 1);
2348   assert(OfsOp.isImm() && "Offset operand wasn't immediate.");
2349   return OfsOp;
2350 }
2351 
2352 bool AArch64InstrInfo::getMemOpInfo(unsigned Opcode, TypeSize &Scale,
2353                                     unsigned &Width, int64_t &MinOffset,
2354                                     int64_t &MaxOffset) {
2355   const unsigned SVEMaxBytesPerVector = AArch64::SVEMaxBitsPerVector / 8;
2356   switch (Opcode) {
2357   // Not a memory operation or something we want to handle.
2358   default:
2359     Scale = TypeSize::Fixed(0);
2360     Width = 0;
2361     MinOffset = MaxOffset = 0;
2362     return false;
2363   case AArch64::STRWpost:
2364   case AArch64::LDRWpost:
2365     Width = 32;
2366     Scale = TypeSize::Fixed(4);
2367     MinOffset = -256;
2368     MaxOffset = 255;
2369     break;
2370   case AArch64::LDURQi:
2371   case AArch64::STURQi:
2372     Width = 16;
2373     Scale = TypeSize::Fixed(1);
2374     MinOffset = -256;
2375     MaxOffset = 255;
2376     break;
2377   case AArch64::PRFUMi:
2378   case AArch64::LDURXi:
2379   case AArch64::LDURDi:
2380   case AArch64::STURXi:
2381   case AArch64::STURDi:
2382     Width = 8;
2383     Scale = TypeSize::Fixed(1);
2384     MinOffset = -256;
2385     MaxOffset = 255;
2386     break;
2387   case AArch64::LDURWi:
2388   case AArch64::LDURSi:
2389   case AArch64::LDURSWi:
2390   case AArch64::STURWi:
2391   case AArch64::STURSi:
2392     Width = 4;
2393     Scale = TypeSize::Fixed(1);
2394     MinOffset = -256;
2395     MaxOffset = 255;
2396     break;
2397   case AArch64::LDURHi:
2398   case AArch64::LDURHHi:
2399   case AArch64::LDURSHXi:
2400   case AArch64::LDURSHWi:
2401   case AArch64::STURHi:
2402   case AArch64::STURHHi:
2403     Width = 2;
2404     Scale = TypeSize::Fixed(1);
2405     MinOffset = -256;
2406     MaxOffset = 255;
2407     break;
2408   case AArch64::LDURBi:
2409   case AArch64::LDURBBi:
2410   case AArch64::LDURSBXi:
2411   case AArch64::LDURSBWi:
2412   case AArch64::STURBi:
2413   case AArch64::STURBBi:
2414     Width = 1;
2415     Scale = TypeSize::Fixed(1);
2416     MinOffset = -256;
2417     MaxOffset = 255;
2418     break;
2419   case AArch64::LDPQi:
2420   case AArch64::LDNPQi:
2421   case AArch64::STPQi:
2422   case AArch64::STNPQi:
2423     Scale = TypeSize::Fixed(16);
2424     Width = 32;
2425     MinOffset = -64;
2426     MaxOffset = 63;
2427     break;
2428   case AArch64::LDRQui:
2429   case AArch64::STRQui:
2430     Scale = TypeSize::Fixed(16);
2431     Width = 16;
2432     MinOffset = 0;
2433     MaxOffset = 4095;
2434     break;
2435   case AArch64::LDPXi:
2436   case AArch64::LDPDi:
2437   case AArch64::LDNPXi:
2438   case AArch64::LDNPDi:
2439   case AArch64::STPXi:
2440   case AArch64::STPDi:
2441   case AArch64::STNPXi:
2442   case AArch64::STNPDi:
2443     Scale = TypeSize::Fixed(8);
2444     Width = 16;
2445     MinOffset = -64;
2446     MaxOffset = 63;
2447     break;
2448   case AArch64::PRFMui:
2449   case AArch64::LDRXui:
2450   case AArch64::LDRDui:
2451   case AArch64::STRXui:
2452   case AArch64::STRDui:
2453     Scale = TypeSize::Fixed(8);
2454     Width = 8;
2455     MinOffset = 0;
2456     MaxOffset = 4095;
2457     break;
2458   case AArch64::LDPWi:
2459   case AArch64::LDPSi:
2460   case AArch64::LDNPWi:
2461   case AArch64::LDNPSi:
2462   case AArch64::STPWi:
2463   case AArch64::STPSi:
2464   case AArch64::STNPWi:
2465   case AArch64::STNPSi:
2466     Scale = TypeSize::Fixed(4);
2467     Width = 8;
2468     MinOffset = -64;
2469     MaxOffset = 63;
2470     break;
2471   case AArch64::LDRWui:
2472   case AArch64::LDRSui:
2473   case AArch64::LDRSWui:
2474   case AArch64::STRWui:
2475   case AArch64::STRSui:
2476     Scale = TypeSize::Fixed(4);
2477     Width = 4;
2478     MinOffset = 0;
2479     MaxOffset = 4095;
2480     break;
2481   case AArch64::LDRHui:
2482   case AArch64::LDRHHui:
2483   case AArch64::LDRSHWui:
2484   case AArch64::LDRSHXui:
2485   case AArch64::STRHui:
2486   case AArch64::STRHHui:
2487     Scale = TypeSize::Fixed(2);
2488     Width = 2;
2489     MinOffset = 0;
2490     MaxOffset = 4095;
2491     break;
2492   case AArch64::LDRBui:
2493   case AArch64::LDRBBui:
2494   case AArch64::LDRSBWui:
2495   case AArch64::LDRSBXui:
2496   case AArch64::STRBui:
2497   case AArch64::STRBBui:
2498     Scale = TypeSize::Fixed(1);
2499     Width = 1;
2500     MinOffset = 0;
2501     MaxOffset = 4095;
2502     break;
2503   case AArch64::ADDG:
2504     Scale = TypeSize::Fixed(16);
2505     Width = 0;
2506     MinOffset = 0;
2507     MaxOffset = 63;
2508     break;
2509   case AArch64::TAGPstack:
2510     Scale = TypeSize::Fixed(16);
2511     Width = 0;
2512     // TAGP with a negative offset turns into SUBP, which has a maximum offset
2513     // of 63 (not 64!).
2514     MinOffset = -63;
2515     MaxOffset = 63;
2516     break;
2517   case AArch64::LDG:
2518   case AArch64::STGOffset:
2519   case AArch64::STZGOffset:
2520     Scale = TypeSize::Fixed(16);
2521     Width = 16;
2522     MinOffset = -256;
2523     MaxOffset = 255;
2524     break;
2525   case AArch64::STR_ZZZZXI:
2526   case AArch64::LDR_ZZZZXI:
2527     Scale = TypeSize::Scalable(16);
2528     Width = SVEMaxBytesPerVector * 4;
2529     MinOffset = -256;
2530     MaxOffset = 252;
2531     break;
2532   case AArch64::STR_ZZZXI:
2533   case AArch64::LDR_ZZZXI:
2534     Scale = TypeSize::Scalable(16);
2535     Width = SVEMaxBytesPerVector * 3;
2536     MinOffset = -256;
2537     MaxOffset = 253;
2538     break;
2539   case AArch64::STR_ZZXI:
2540   case AArch64::LDR_ZZXI:
2541     Scale = TypeSize::Scalable(16);
2542     Width = SVEMaxBytesPerVector * 2;
2543     MinOffset = -256;
2544     MaxOffset = 254;
2545     break;
2546   case AArch64::LDR_PXI:
2547   case AArch64::STR_PXI:
2548     Scale = TypeSize::Scalable(2);
2549     Width = SVEMaxBytesPerVector / 8;
2550     MinOffset = -256;
2551     MaxOffset = 255;
2552     break;
2553   case AArch64::LDR_ZXI:
2554   case AArch64::STR_ZXI:
2555     Scale = TypeSize::Scalable(16);
2556     Width = SVEMaxBytesPerVector;
2557     MinOffset = -256;
2558     MaxOffset = 255;
2559     break;
2560   case AArch64::LD1B_IMM:
2561   case AArch64::LD1H_IMM:
2562   case AArch64::LD1W_IMM:
2563   case AArch64::LD1D_IMM:
2564   case AArch64::ST1B_IMM:
2565   case AArch64::ST1H_IMM:
2566   case AArch64::ST1W_IMM:
2567   case AArch64::ST1D_IMM:
2568     // A full vectors worth of data
2569     // Width = mbytes * elements
2570     Scale = TypeSize::Scalable(16);
2571     Width = SVEMaxBytesPerVector;
2572     MinOffset = -8;
2573     MaxOffset = 7;
2574     break;
2575   case AArch64::LD1B_H_IMM:
2576   case AArch64::LD1SB_H_IMM:
2577   case AArch64::LD1H_S_IMM:
2578   case AArch64::LD1SH_S_IMM:
2579   case AArch64::LD1W_D_IMM:
2580   case AArch64::LD1SW_D_IMM:
2581   case AArch64::ST1B_H_IMM:
2582   case AArch64::ST1H_S_IMM:
2583   case AArch64::ST1W_D_IMM:
2584     // A half vector worth of data
2585     // Width = mbytes * elements
2586     Scale = TypeSize::Scalable(8);
2587     Width = SVEMaxBytesPerVector / 2;
2588     MinOffset = -8;
2589     MaxOffset = 7;
2590     break;
2591   case AArch64::LD1B_S_IMM:
2592   case AArch64::LD1SB_S_IMM:
2593   case AArch64::LD1H_D_IMM:
2594   case AArch64::LD1SH_D_IMM:
2595   case AArch64::ST1B_S_IMM:
2596   case AArch64::ST1H_D_IMM:
2597     // A quarter vector worth of data
2598     // Width = mbytes * elements
2599     Scale = TypeSize::Scalable(4);
2600     Width = SVEMaxBytesPerVector / 4;
2601     MinOffset = -8;
2602     MaxOffset = 7;
2603     break;
2604   case AArch64::LD1B_D_IMM:
2605   case AArch64::LD1SB_D_IMM:
2606   case AArch64::ST1B_D_IMM:
2607     // A eighth vector worth of data
2608     // Width = mbytes * elements
2609     Scale = TypeSize::Scalable(2);
2610     Width = SVEMaxBytesPerVector / 8;
2611     MinOffset = -8;
2612     MaxOffset = 7;
2613     break;
2614   case AArch64::ST2GOffset:
2615   case AArch64::STZ2GOffset:
2616     Scale = TypeSize::Fixed(16);
2617     Width = 32;
2618     MinOffset = -256;
2619     MaxOffset = 255;
2620     break;
2621   case AArch64::STGPi:
2622     Scale = TypeSize::Fixed(16);
2623     Width = 16;
2624     MinOffset = -64;
2625     MaxOffset = 63;
2626     break;
2627   }
2628 
2629   return true;
2630 }
2631 
2632 // Scaling factor for unscaled load or store.
2633 int AArch64InstrInfo::getMemScale(unsigned Opc) {
2634   switch (Opc) {
2635   default:
2636     llvm_unreachable("Opcode has unknown scale!");
2637   case AArch64::LDRBBui:
2638   case AArch64::LDURBBi:
2639   case AArch64::LDRSBWui:
2640   case AArch64::LDURSBWi:
2641   case AArch64::STRBBui:
2642   case AArch64::STURBBi:
2643     return 1;
2644   case AArch64::LDRHHui:
2645   case AArch64::LDURHHi:
2646   case AArch64::LDRSHWui:
2647   case AArch64::LDURSHWi:
2648   case AArch64::STRHHui:
2649   case AArch64::STURHHi:
2650     return 2;
2651   case AArch64::LDRSui:
2652   case AArch64::LDURSi:
2653   case AArch64::LDRSWui:
2654   case AArch64::LDURSWi:
2655   case AArch64::LDRWui:
2656   case AArch64::LDURWi:
2657   case AArch64::STRSui:
2658   case AArch64::STURSi:
2659   case AArch64::STRWui:
2660   case AArch64::STURWi:
2661   case AArch64::LDPSi:
2662   case AArch64::LDPSWi:
2663   case AArch64::LDPWi:
2664   case AArch64::STPSi:
2665   case AArch64::STPWi:
2666     return 4;
2667   case AArch64::LDRDui:
2668   case AArch64::LDURDi:
2669   case AArch64::LDRXui:
2670   case AArch64::LDURXi:
2671   case AArch64::STRDui:
2672   case AArch64::STURDi:
2673   case AArch64::STRXui:
2674   case AArch64::STURXi:
2675   case AArch64::LDPDi:
2676   case AArch64::LDPXi:
2677   case AArch64::STPDi:
2678   case AArch64::STPXi:
2679     return 8;
2680   case AArch64::LDRQui:
2681   case AArch64::LDURQi:
2682   case AArch64::STRQui:
2683   case AArch64::STURQi:
2684   case AArch64::LDPQi:
2685   case AArch64::STPQi:
2686   case AArch64::STGOffset:
2687   case AArch64::STZGOffset:
2688   case AArch64::ST2GOffset:
2689   case AArch64::STZ2GOffset:
2690   case AArch64::STGPi:
2691     return 16;
2692   }
2693 }
2694 
2695 // Scale the unscaled offsets.  Returns false if the unscaled offset can't be
2696 // scaled.
2697 static bool scaleOffset(unsigned Opc, int64_t &Offset) {
2698   int Scale = AArch64InstrInfo::getMemScale(Opc);
2699 
2700   // If the byte-offset isn't a multiple of the stride, we can't scale this
2701   // offset.
2702   if (Offset % Scale != 0)
2703     return false;
2704 
2705   // Convert the byte-offset used by unscaled into an "element" offset used
2706   // by the scaled pair load/store instructions.
2707   Offset /= Scale;
2708   return true;
2709 }
2710 
2711 static bool canPairLdStOpc(unsigned FirstOpc, unsigned SecondOpc) {
2712   if (FirstOpc == SecondOpc)
2713     return true;
2714   // We can also pair sign-ext and zero-ext instructions.
2715   switch (FirstOpc) {
2716   default:
2717     return false;
2718   case AArch64::LDRWui:
2719   case AArch64::LDURWi:
2720     return SecondOpc == AArch64::LDRSWui || SecondOpc == AArch64::LDURSWi;
2721   case AArch64::LDRSWui:
2722   case AArch64::LDURSWi:
2723     return SecondOpc == AArch64::LDRWui || SecondOpc == AArch64::LDURWi;
2724   }
2725   // These instructions can't be paired based on their opcodes.
2726   return false;
2727 }
2728 
2729 static bool shouldClusterFI(const MachineFrameInfo &MFI, int FI1,
2730                             int64_t Offset1, unsigned Opcode1, int FI2,
2731                             int64_t Offset2, unsigned Opcode2) {
2732   // Accesses through fixed stack object frame indices may access a different
2733   // fixed stack slot. Check that the object offsets + offsets match.
2734   if (MFI.isFixedObjectIndex(FI1) && MFI.isFixedObjectIndex(FI2)) {
2735     int64_t ObjectOffset1 = MFI.getObjectOffset(FI1);
2736     int64_t ObjectOffset2 = MFI.getObjectOffset(FI2);
2737     assert(ObjectOffset1 <= ObjectOffset2 && "Object offsets are not ordered.");
2738     // Convert to scaled object offsets.
2739     int Scale1 = AArch64InstrInfo::getMemScale(Opcode1);
2740     if (ObjectOffset1 % Scale1 != 0)
2741       return false;
2742     ObjectOffset1 /= Scale1;
2743     int Scale2 = AArch64InstrInfo::getMemScale(Opcode2);
2744     if (ObjectOffset2 % Scale2 != 0)
2745       return false;
2746     ObjectOffset2 /= Scale2;
2747     ObjectOffset1 += Offset1;
2748     ObjectOffset2 += Offset2;
2749     return ObjectOffset1 + 1 == ObjectOffset2;
2750   }
2751 
2752   return FI1 == FI2;
2753 }
2754 
2755 /// Detect opportunities for ldp/stp formation.
2756 ///
2757 /// Only called for LdSt for which getMemOperandWithOffset returns true.
2758 bool AArch64InstrInfo::shouldClusterMemOps(
2759     ArrayRef<const MachineOperand *> BaseOps1,
2760     ArrayRef<const MachineOperand *> BaseOps2, unsigned NumLoads,
2761     unsigned NumBytes) const {
2762   assert(BaseOps1.size() == 1 && BaseOps2.size() == 1);
2763   const MachineOperand &BaseOp1 = *BaseOps1.front();
2764   const MachineOperand &BaseOp2 = *BaseOps2.front();
2765   const MachineInstr &FirstLdSt = *BaseOp1.getParent();
2766   const MachineInstr &SecondLdSt = *BaseOp2.getParent();
2767   if (BaseOp1.getType() != BaseOp2.getType())
2768     return false;
2769 
2770   assert((BaseOp1.isReg() || BaseOp1.isFI()) &&
2771          "Only base registers and frame indices are supported.");
2772 
2773   // Check for both base regs and base FI.
2774   if (BaseOp1.isReg() && BaseOp1.getReg() != BaseOp2.getReg())
2775     return false;
2776 
2777   // Only cluster up to a single pair.
2778   if (NumLoads > 2)
2779     return false;
2780 
2781   if (!isPairableLdStInst(FirstLdSt) || !isPairableLdStInst(SecondLdSt))
2782     return false;
2783 
2784   // Can we pair these instructions based on their opcodes?
2785   unsigned FirstOpc = FirstLdSt.getOpcode();
2786   unsigned SecondOpc = SecondLdSt.getOpcode();
2787   if (!canPairLdStOpc(FirstOpc, SecondOpc))
2788     return false;
2789 
2790   // Can't merge volatiles or load/stores that have a hint to avoid pair
2791   // formation, for example.
2792   if (!isCandidateToMergeOrPair(FirstLdSt) ||
2793       !isCandidateToMergeOrPair(SecondLdSt))
2794     return false;
2795 
2796   // isCandidateToMergeOrPair guarantees that operand 2 is an immediate.
2797   int64_t Offset1 = FirstLdSt.getOperand(2).getImm();
2798   if (isUnscaledLdSt(FirstOpc) && !scaleOffset(FirstOpc, Offset1))
2799     return false;
2800 
2801   int64_t Offset2 = SecondLdSt.getOperand(2).getImm();
2802   if (isUnscaledLdSt(SecondOpc) && !scaleOffset(SecondOpc, Offset2))
2803     return false;
2804 
2805   // Pairwise instructions have a 7-bit signed offset field.
2806   if (Offset1 > 63 || Offset1 < -64)
2807     return false;
2808 
2809   // The caller should already have ordered First/SecondLdSt by offset.
2810   // Note: except for non-equal frame index bases
2811   if (BaseOp1.isFI()) {
2812     assert((!BaseOp1.isIdenticalTo(BaseOp2) || Offset1 <= Offset2) &&
2813            "Caller should have ordered offsets.");
2814 
2815     const MachineFrameInfo &MFI =
2816         FirstLdSt.getParent()->getParent()->getFrameInfo();
2817     return shouldClusterFI(MFI, BaseOp1.getIndex(), Offset1, FirstOpc,
2818                            BaseOp2.getIndex(), Offset2, SecondOpc);
2819   }
2820 
2821   assert(Offset1 <= Offset2 && "Caller should have ordered offsets.");
2822 
2823   return Offset1 + 1 == Offset2;
2824 }
2825 
2826 static const MachineInstrBuilder &AddSubReg(const MachineInstrBuilder &MIB,
2827                                             unsigned Reg, unsigned SubIdx,
2828                                             unsigned State,
2829                                             const TargetRegisterInfo *TRI) {
2830   if (!SubIdx)
2831     return MIB.addReg(Reg, State);
2832 
2833   if (Register::isPhysicalRegister(Reg))
2834     return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
2835   return MIB.addReg(Reg, State, SubIdx);
2836 }
2837 
2838 static bool forwardCopyWillClobberTuple(unsigned DestReg, unsigned SrcReg,
2839                                         unsigned NumRegs) {
2840   // We really want the positive remainder mod 32 here, that happens to be
2841   // easily obtainable with a mask.
2842   return ((DestReg - SrcReg) & 0x1f) < NumRegs;
2843 }
2844 
2845 void AArch64InstrInfo::copyPhysRegTuple(MachineBasicBlock &MBB,
2846                                         MachineBasicBlock::iterator I,
2847                                         const DebugLoc &DL, MCRegister DestReg,
2848                                         MCRegister SrcReg, bool KillSrc,
2849                                         unsigned Opcode,
2850                                         ArrayRef<unsigned> Indices) const {
2851   assert(Subtarget.hasNEON() && "Unexpected register copy without NEON");
2852   const TargetRegisterInfo *TRI = &getRegisterInfo();
2853   uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
2854   uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
2855   unsigned NumRegs = Indices.size();
2856 
2857   int SubReg = 0, End = NumRegs, Incr = 1;
2858   if (forwardCopyWillClobberTuple(DestEncoding, SrcEncoding, NumRegs)) {
2859     SubReg = NumRegs - 1;
2860     End = -1;
2861     Incr = -1;
2862   }
2863 
2864   for (; SubReg != End; SubReg += Incr) {
2865     const MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opcode));
2866     AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
2867     AddSubReg(MIB, SrcReg, Indices[SubReg], 0, TRI);
2868     AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
2869   }
2870 }
2871 
2872 void AArch64InstrInfo::copyGPRRegTuple(MachineBasicBlock &MBB,
2873                                        MachineBasicBlock::iterator I,
2874                                        DebugLoc DL, unsigned DestReg,
2875                                        unsigned SrcReg, bool KillSrc,
2876                                        unsigned Opcode, unsigned ZeroReg,
2877                                        llvm::ArrayRef<unsigned> Indices) const {
2878   const TargetRegisterInfo *TRI = &getRegisterInfo();
2879   unsigned NumRegs = Indices.size();
2880 
2881 #ifndef NDEBUG
2882   uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
2883   uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
2884   assert(DestEncoding % NumRegs == 0 && SrcEncoding % NumRegs == 0 &&
2885          "GPR reg sequences should not be able to overlap");
2886 #endif
2887 
2888   for (unsigned SubReg = 0; SubReg != NumRegs; ++SubReg) {
2889     const MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opcode));
2890     AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
2891     MIB.addReg(ZeroReg);
2892     AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
2893     MIB.addImm(0);
2894   }
2895 }
2896 
2897 void AArch64InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
2898                                    MachineBasicBlock::iterator I,
2899                                    const DebugLoc &DL, MCRegister DestReg,
2900                                    MCRegister SrcReg, bool KillSrc) const {
2901   if (AArch64::GPR32spRegClass.contains(DestReg) &&
2902       (AArch64::GPR32spRegClass.contains(SrcReg) || SrcReg == AArch64::WZR)) {
2903     const TargetRegisterInfo *TRI = &getRegisterInfo();
2904 
2905     if (DestReg == AArch64::WSP || SrcReg == AArch64::WSP) {
2906       // If either operand is WSP, expand to ADD #0.
2907       if (Subtarget.hasZeroCycleRegMove()) {
2908         // Cyclone recognizes "ADD Xd, Xn, #0" as a zero-cycle register move.
2909         MCRegister DestRegX = TRI->getMatchingSuperReg(
2910             DestReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
2911         MCRegister SrcRegX = TRI->getMatchingSuperReg(
2912             SrcReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
2913         // This instruction is reading and writing X registers.  This may upset
2914         // the register scavenger and machine verifier, so we need to indicate
2915         // that we are reading an undefined value from SrcRegX, but a proper
2916         // value from SrcReg.
2917         BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestRegX)
2918             .addReg(SrcRegX, RegState::Undef)
2919             .addImm(0)
2920             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0))
2921             .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
2922       } else {
2923         BuildMI(MBB, I, DL, get(AArch64::ADDWri), DestReg)
2924             .addReg(SrcReg, getKillRegState(KillSrc))
2925             .addImm(0)
2926             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2927       }
2928     } else if (SrcReg == AArch64::WZR && Subtarget.hasZeroCycleZeroingGP()) {
2929       BuildMI(MBB, I, DL, get(AArch64::MOVZWi), DestReg)
2930           .addImm(0)
2931           .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2932     } else {
2933       if (Subtarget.hasZeroCycleRegMove()) {
2934         // Cyclone recognizes "ORR Xd, XZR, Xm" as a zero-cycle register move.
2935         MCRegister DestRegX = TRI->getMatchingSuperReg(
2936             DestReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
2937         MCRegister SrcRegX = TRI->getMatchingSuperReg(
2938             SrcReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
2939         // This instruction is reading and writing X registers.  This may upset
2940         // the register scavenger and machine verifier, so we need to indicate
2941         // that we are reading an undefined value from SrcRegX, but a proper
2942         // value from SrcReg.
2943         BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestRegX)
2944             .addReg(AArch64::XZR)
2945             .addReg(SrcRegX, RegState::Undef)
2946             .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
2947       } else {
2948         // Otherwise, expand to ORR WZR.
2949         BuildMI(MBB, I, DL, get(AArch64::ORRWrr), DestReg)
2950             .addReg(AArch64::WZR)
2951             .addReg(SrcReg, getKillRegState(KillSrc));
2952       }
2953     }
2954     return;
2955   }
2956 
2957   // Copy a Predicate register by ORRing with itself.
2958   if (AArch64::PPRRegClass.contains(DestReg) &&
2959       AArch64::PPRRegClass.contains(SrcReg)) {
2960     assert(Subtarget.hasSVE() && "Unexpected SVE register.");
2961     BuildMI(MBB, I, DL, get(AArch64::ORR_PPzPP), DestReg)
2962       .addReg(SrcReg) // Pg
2963       .addReg(SrcReg)
2964       .addReg(SrcReg, getKillRegState(KillSrc));
2965     return;
2966   }
2967 
2968   // Copy a Z register by ORRing with itself.
2969   if (AArch64::ZPRRegClass.contains(DestReg) &&
2970       AArch64::ZPRRegClass.contains(SrcReg)) {
2971     assert(Subtarget.hasSVE() && "Unexpected SVE register.");
2972     BuildMI(MBB, I, DL, get(AArch64::ORR_ZZZ), DestReg)
2973       .addReg(SrcReg)
2974       .addReg(SrcReg, getKillRegState(KillSrc));
2975     return;
2976   }
2977 
2978   // Copy a Z register pair by copying the individual sub-registers.
2979   if (AArch64::ZPR2RegClass.contains(DestReg) &&
2980       AArch64::ZPR2RegClass.contains(SrcReg)) {
2981     static const unsigned Indices[] = {AArch64::zsub0, AArch64::zsub1};
2982     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORR_ZZZ,
2983                      Indices);
2984     return;
2985   }
2986 
2987   // Copy a Z register triple by copying the individual sub-registers.
2988   if (AArch64::ZPR3RegClass.contains(DestReg) &&
2989       AArch64::ZPR3RegClass.contains(SrcReg)) {
2990     static const unsigned Indices[] = {AArch64::zsub0, AArch64::zsub1,
2991                                        AArch64::zsub2};
2992     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORR_ZZZ,
2993                      Indices);
2994     return;
2995   }
2996 
2997   // Copy a Z register quad by copying the individual sub-registers.
2998   if (AArch64::ZPR4RegClass.contains(DestReg) &&
2999       AArch64::ZPR4RegClass.contains(SrcReg)) {
3000     static const unsigned Indices[] = {AArch64::zsub0, AArch64::zsub1,
3001                                        AArch64::zsub2, AArch64::zsub3};
3002     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORR_ZZZ,
3003                      Indices);
3004     return;
3005   }
3006 
3007   if (AArch64::GPR64spRegClass.contains(DestReg) &&
3008       (AArch64::GPR64spRegClass.contains(SrcReg) || SrcReg == AArch64::XZR)) {
3009     if (DestReg == AArch64::SP || SrcReg == AArch64::SP) {
3010       // If either operand is SP, expand to ADD #0.
3011       BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestReg)
3012           .addReg(SrcReg, getKillRegState(KillSrc))
3013           .addImm(0)
3014           .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
3015     } else if (SrcReg == AArch64::XZR && Subtarget.hasZeroCycleZeroingGP()) {
3016       BuildMI(MBB, I, DL, get(AArch64::MOVZXi), DestReg)
3017           .addImm(0)
3018           .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
3019     } else {
3020       // Otherwise, expand to ORR XZR.
3021       BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestReg)
3022           .addReg(AArch64::XZR)
3023           .addReg(SrcReg, getKillRegState(KillSrc));
3024     }
3025     return;
3026   }
3027 
3028   // Copy a DDDD register quad by copying the individual sub-registers.
3029   if (AArch64::DDDDRegClass.contains(DestReg) &&
3030       AArch64::DDDDRegClass.contains(SrcReg)) {
3031     static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
3032                                        AArch64::dsub2, AArch64::dsub3};
3033     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
3034                      Indices);
3035     return;
3036   }
3037 
3038   // Copy a DDD register triple by copying the individual sub-registers.
3039   if (AArch64::DDDRegClass.contains(DestReg) &&
3040       AArch64::DDDRegClass.contains(SrcReg)) {
3041     static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
3042                                        AArch64::dsub2};
3043     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
3044                      Indices);
3045     return;
3046   }
3047 
3048   // Copy a DD register pair by copying the individual sub-registers.
3049   if (AArch64::DDRegClass.contains(DestReg) &&
3050       AArch64::DDRegClass.contains(SrcReg)) {
3051     static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1};
3052     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
3053                      Indices);
3054     return;
3055   }
3056 
3057   // Copy a QQQQ register quad by copying the individual sub-registers.
3058   if (AArch64::QQQQRegClass.contains(DestReg) &&
3059       AArch64::QQQQRegClass.contains(SrcReg)) {
3060     static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
3061                                        AArch64::qsub2, AArch64::qsub3};
3062     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
3063                      Indices);
3064     return;
3065   }
3066 
3067   // Copy a QQQ register triple by copying the individual sub-registers.
3068   if (AArch64::QQQRegClass.contains(DestReg) &&
3069       AArch64::QQQRegClass.contains(SrcReg)) {
3070     static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
3071                                        AArch64::qsub2};
3072     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
3073                      Indices);
3074     return;
3075   }
3076 
3077   // Copy a QQ register pair by copying the individual sub-registers.
3078   if (AArch64::QQRegClass.contains(DestReg) &&
3079       AArch64::QQRegClass.contains(SrcReg)) {
3080     static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1};
3081     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
3082                      Indices);
3083     return;
3084   }
3085 
3086   if (AArch64::XSeqPairsClassRegClass.contains(DestReg) &&
3087       AArch64::XSeqPairsClassRegClass.contains(SrcReg)) {
3088     static const unsigned Indices[] = {AArch64::sube64, AArch64::subo64};
3089     copyGPRRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRXrs,
3090                     AArch64::XZR, Indices);
3091     return;
3092   }
3093 
3094   if (AArch64::WSeqPairsClassRegClass.contains(DestReg) &&
3095       AArch64::WSeqPairsClassRegClass.contains(SrcReg)) {
3096     static const unsigned Indices[] = {AArch64::sube32, AArch64::subo32};
3097     copyGPRRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRWrs,
3098                     AArch64::WZR, Indices);
3099     return;
3100   }
3101 
3102   if (AArch64::FPR128RegClass.contains(DestReg) &&
3103       AArch64::FPR128RegClass.contains(SrcReg)) {
3104     if (Subtarget.hasNEON()) {
3105       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
3106           .addReg(SrcReg)
3107           .addReg(SrcReg, getKillRegState(KillSrc));
3108     } else {
3109       BuildMI(MBB, I, DL, get(AArch64::STRQpre))
3110           .addReg(AArch64::SP, RegState::Define)
3111           .addReg(SrcReg, getKillRegState(KillSrc))
3112           .addReg(AArch64::SP)
3113           .addImm(-16);
3114       BuildMI(MBB, I, DL, get(AArch64::LDRQpre))
3115           .addReg(AArch64::SP, RegState::Define)
3116           .addReg(DestReg, RegState::Define)
3117           .addReg(AArch64::SP)
3118           .addImm(16);
3119     }
3120     return;
3121   }
3122 
3123   if (AArch64::FPR64RegClass.contains(DestReg) &&
3124       AArch64::FPR64RegClass.contains(SrcReg)) {
3125     if (Subtarget.hasNEON()) {
3126       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::dsub,
3127                                        &AArch64::FPR128RegClass);
3128       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::dsub,
3129                                       &AArch64::FPR128RegClass);
3130       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
3131           .addReg(SrcReg)
3132           .addReg(SrcReg, getKillRegState(KillSrc));
3133     } else {
3134       BuildMI(MBB, I, DL, get(AArch64::FMOVDr), DestReg)
3135           .addReg(SrcReg, getKillRegState(KillSrc));
3136     }
3137     return;
3138   }
3139 
3140   if (AArch64::FPR32RegClass.contains(DestReg) &&
3141       AArch64::FPR32RegClass.contains(SrcReg)) {
3142     if (Subtarget.hasNEON()) {
3143       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::ssub,
3144                                        &AArch64::FPR128RegClass);
3145       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::ssub,
3146                                       &AArch64::FPR128RegClass);
3147       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
3148           .addReg(SrcReg)
3149           .addReg(SrcReg, getKillRegState(KillSrc));
3150     } else {
3151       BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
3152           .addReg(SrcReg, getKillRegState(KillSrc));
3153     }
3154     return;
3155   }
3156 
3157   if (AArch64::FPR16RegClass.contains(DestReg) &&
3158       AArch64::FPR16RegClass.contains(SrcReg)) {
3159     if (Subtarget.hasNEON()) {
3160       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::hsub,
3161                                        &AArch64::FPR128RegClass);
3162       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::hsub,
3163                                       &AArch64::FPR128RegClass);
3164       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
3165           .addReg(SrcReg)
3166           .addReg(SrcReg, getKillRegState(KillSrc));
3167     } else {
3168       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::hsub,
3169                                        &AArch64::FPR32RegClass);
3170       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::hsub,
3171                                       &AArch64::FPR32RegClass);
3172       BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
3173           .addReg(SrcReg, getKillRegState(KillSrc));
3174     }
3175     return;
3176   }
3177 
3178   if (AArch64::FPR8RegClass.contains(DestReg) &&
3179       AArch64::FPR8RegClass.contains(SrcReg)) {
3180     if (Subtarget.hasNEON()) {
3181       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::bsub,
3182                                        &AArch64::FPR128RegClass);
3183       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::bsub,
3184                                       &AArch64::FPR128RegClass);
3185       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
3186           .addReg(SrcReg)
3187           .addReg(SrcReg, getKillRegState(KillSrc));
3188     } else {
3189       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::bsub,
3190                                        &AArch64::FPR32RegClass);
3191       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::bsub,
3192                                       &AArch64::FPR32RegClass);
3193       BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
3194           .addReg(SrcReg, getKillRegState(KillSrc));
3195     }
3196     return;
3197   }
3198 
3199   // Copies between GPR64 and FPR64.
3200   if (AArch64::FPR64RegClass.contains(DestReg) &&
3201       AArch64::GPR64RegClass.contains(SrcReg)) {
3202     BuildMI(MBB, I, DL, get(AArch64::FMOVXDr), DestReg)
3203         .addReg(SrcReg, getKillRegState(KillSrc));
3204     return;
3205   }
3206   if (AArch64::GPR64RegClass.contains(DestReg) &&
3207       AArch64::FPR64RegClass.contains(SrcReg)) {
3208     BuildMI(MBB, I, DL, get(AArch64::FMOVDXr), DestReg)
3209         .addReg(SrcReg, getKillRegState(KillSrc));
3210     return;
3211   }
3212   // Copies between GPR32 and FPR32.
3213   if (AArch64::FPR32RegClass.contains(DestReg) &&
3214       AArch64::GPR32RegClass.contains(SrcReg)) {
3215     BuildMI(MBB, I, DL, get(AArch64::FMOVWSr), DestReg)
3216         .addReg(SrcReg, getKillRegState(KillSrc));
3217     return;
3218   }
3219   if (AArch64::GPR32RegClass.contains(DestReg) &&
3220       AArch64::FPR32RegClass.contains(SrcReg)) {
3221     BuildMI(MBB, I, DL, get(AArch64::FMOVSWr), DestReg)
3222         .addReg(SrcReg, getKillRegState(KillSrc));
3223     return;
3224   }
3225 
3226   if (DestReg == AArch64::NZCV) {
3227     assert(AArch64::GPR64RegClass.contains(SrcReg) && "Invalid NZCV copy");
3228     BuildMI(MBB, I, DL, get(AArch64::MSR))
3229         .addImm(AArch64SysReg::NZCV)
3230         .addReg(SrcReg, getKillRegState(KillSrc))
3231         .addReg(AArch64::NZCV, RegState::Implicit | RegState::Define);
3232     return;
3233   }
3234 
3235   if (SrcReg == AArch64::NZCV) {
3236     assert(AArch64::GPR64RegClass.contains(DestReg) && "Invalid NZCV copy");
3237     BuildMI(MBB, I, DL, get(AArch64::MRS), DestReg)
3238         .addImm(AArch64SysReg::NZCV)
3239         .addReg(AArch64::NZCV, RegState::Implicit | getKillRegState(KillSrc));
3240     return;
3241   }
3242 
3243   llvm_unreachable("unimplemented reg-to-reg copy");
3244 }
3245 
3246 static void storeRegPairToStackSlot(const TargetRegisterInfo &TRI,
3247                                     MachineBasicBlock &MBB,
3248                                     MachineBasicBlock::iterator InsertBefore,
3249                                     const MCInstrDesc &MCID,
3250                                     Register SrcReg, bool IsKill,
3251                                     unsigned SubIdx0, unsigned SubIdx1, int FI,
3252                                     MachineMemOperand *MMO) {
3253   Register SrcReg0 = SrcReg;
3254   Register SrcReg1 = SrcReg;
3255   if (Register::isPhysicalRegister(SrcReg)) {
3256     SrcReg0 = TRI.getSubReg(SrcReg, SubIdx0);
3257     SubIdx0 = 0;
3258     SrcReg1 = TRI.getSubReg(SrcReg, SubIdx1);
3259     SubIdx1 = 0;
3260   }
3261   BuildMI(MBB, InsertBefore, DebugLoc(), MCID)
3262       .addReg(SrcReg0, getKillRegState(IsKill), SubIdx0)
3263       .addReg(SrcReg1, getKillRegState(IsKill), SubIdx1)
3264       .addFrameIndex(FI)
3265       .addImm(0)
3266       .addMemOperand(MMO);
3267 }
3268 
3269 void AArch64InstrInfo::storeRegToStackSlot(
3270     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg,
3271     bool isKill, int FI, const TargetRegisterClass *RC,
3272     const TargetRegisterInfo *TRI) const {
3273   MachineFunction &MF = *MBB.getParent();
3274   MachineFrameInfo &MFI = MF.getFrameInfo();
3275 
3276   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
3277   MachineMemOperand *MMO =
3278       MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore,
3279                               MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
3280   unsigned Opc = 0;
3281   bool Offset = true;
3282   unsigned StackID = TargetStackID::Default;
3283   switch (TRI->getSpillSize(*RC)) {
3284   case 1:
3285     if (AArch64::FPR8RegClass.hasSubClassEq(RC))
3286       Opc = AArch64::STRBui;
3287     break;
3288   case 2:
3289     if (AArch64::FPR16RegClass.hasSubClassEq(RC))
3290       Opc = AArch64::STRHui;
3291     else if (AArch64::PPRRegClass.hasSubClassEq(RC)) {
3292       assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
3293       Opc = AArch64::STR_PXI;
3294       StackID = TargetStackID::ScalableVector;
3295     }
3296     break;
3297   case 4:
3298     if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
3299       Opc = AArch64::STRWui;
3300       if (Register::isVirtualRegister(SrcReg))
3301         MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
3302       else
3303         assert(SrcReg != AArch64::WSP);
3304     } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
3305       Opc = AArch64::STRSui;
3306     break;
3307   case 8:
3308     if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
3309       Opc = AArch64::STRXui;
3310       if (Register::isVirtualRegister(SrcReg))
3311         MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
3312       else
3313         assert(SrcReg != AArch64::SP);
3314     } else if (AArch64::FPR64RegClass.hasSubClassEq(RC)) {
3315       Opc = AArch64::STRDui;
3316     } else if (AArch64::WSeqPairsClassRegClass.hasSubClassEq(RC)) {
3317       storeRegPairToStackSlot(getRegisterInfo(), MBB, MBBI,
3318                               get(AArch64::STPWi), SrcReg, isKill,
3319                               AArch64::sube32, AArch64::subo32, FI, MMO);
3320       return;
3321     }
3322     break;
3323   case 16:
3324     if (AArch64::FPR128RegClass.hasSubClassEq(RC))
3325       Opc = AArch64::STRQui;
3326     else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
3327       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
3328       Opc = AArch64::ST1Twov1d;
3329       Offset = false;
3330     } else if (AArch64::XSeqPairsClassRegClass.hasSubClassEq(RC)) {
3331       storeRegPairToStackSlot(getRegisterInfo(), MBB, MBBI,
3332                               get(AArch64::STPXi), SrcReg, isKill,
3333                               AArch64::sube64, AArch64::subo64, FI, MMO);
3334       return;
3335     } else if (AArch64::ZPRRegClass.hasSubClassEq(RC)) {
3336       assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
3337       Opc = AArch64::STR_ZXI;
3338       StackID = TargetStackID::ScalableVector;
3339     }
3340     break;
3341   case 24:
3342     if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
3343       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
3344       Opc = AArch64::ST1Threev1d;
3345       Offset = false;
3346     }
3347     break;
3348   case 32:
3349     if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
3350       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
3351       Opc = AArch64::ST1Fourv1d;
3352       Offset = false;
3353     } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
3354       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
3355       Opc = AArch64::ST1Twov2d;
3356       Offset = false;
3357     } else if (AArch64::ZPR2RegClass.hasSubClassEq(RC)) {
3358       assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
3359       Opc = AArch64::STR_ZZXI;
3360       StackID = TargetStackID::ScalableVector;
3361     }
3362     break;
3363   case 48:
3364     if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
3365       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
3366       Opc = AArch64::ST1Threev2d;
3367       Offset = false;
3368     } else if (AArch64::ZPR3RegClass.hasSubClassEq(RC)) {
3369       assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
3370       Opc = AArch64::STR_ZZZXI;
3371       StackID = TargetStackID::ScalableVector;
3372     }
3373     break;
3374   case 64:
3375     if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
3376       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
3377       Opc = AArch64::ST1Fourv2d;
3378       Offset = false;
3379     } else if (AArch64::ZPR4RegClass.hasSubClassEq(RC)) {
3380       assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
3381       Opc = AArch64::STR_ZZZZXI;
3382       StackID = TargetStackID::ScalableVector;
3383     }
3384     break;
3385   }
3386   assert(Opc && "Unknown register class");
3387   MFI.setStackID(FI, StackID);
3388 
3389   const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DebugLoc(), get(Opc))
3390                                      .addReg(SrcReg, getKillRegState(isKill))
3391                                      .addFrameIndex(FI);
3392 
3393   if (Offset)
3394     MI.addImm(0);
3395   MI.addMemOperand(MMO);
3396 }
3397 
3398 static void loadRegPairFromStackSlot(const TargetRegisterInfo &TRI,
3399                                      MachineBasicBlock &MBB,
3400                                      MachineBasicBlock::iterator InsertBefore,
3401                                      const MCInstrDesc &MCID,
3402                                      Register DestReg, unsigned SubIdx0,
3403                                      unsigned SubIdx1, int FI,
3404                                      MachineMemOperand *MMO) {
3405   Register DestReg0 = DestReg;
3406   Register DestReg1 = DestReg;
3407   bool IsUndef = true;
3408   if (Register::isPhysicalRegister(DestReg)) {
3409     DestReg0 = TRI.getSubReg(DestReg, SubIdx0);
3410     SubIdx0 = 0;
3411     DestReg1 = TRI.getSubReg(DestReg, SubIdx1);
3412     SubIdx1 = 0;
3413     IsUndef = false;
3414   }
3415   BuildMI(MBB, InsertBefore, DebugLoc(), MCID)
3416       .addReg(DestReg0, RegState::Define | getUndefRegState(IsUndef), SubIdx0)
3417       .addReg(DestReg1, RegState::Define | getUndefRegState(IsUndef), SubIdx1)
3418       .addFrameIndex(FI)
3419       .addImm(0)
3420       .addMemOperand(MMO);
3421 }
3422 
3423 void AArch64InstrInfo::loadRegFromStackSlot(
3424     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg,
3425     int FI, const TargetRegisterClass *RC,
3426     const TargetRegisterInfo *TRI) const {
3427   MachineFunction &MF = *MBB.getParent();
3428   MachineFrameInfo &MFI = MF.getFrameInfo();
3429   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
3430   MachineMemOperand *MMO =
3431       MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOLoad,
3432                               MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
3433 
3434   unsigned Opc = 0;
3435   bool Offset = true;
3436   unsigned StackID = TargetStackID::Default;
3437   switch (TRI->getSpillSize(*RC)) {
3438   case 1:
3439     if (AArch64::FPR8RegClass.hasSubClassEq(RC))
3440       Opc = AArch64::LDRBui;
3441     break;
3442   case 2:
3443     if (AArch64::FPR16RegClass.hasSubClassEq(RC))
3444       Opc = AArch64::LDRHui;
3445     else if (AArch64::PPRRegClass.hasSubClassEq(RC)) {
3446       assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
3447       Opc = AArch64::LDR_PXI;
3448       StackID = TargetStackID::ScalableVector;
3449     }
3450     break;
3451   case 4:
3452     if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
3453       Opc = AArch64::LDRWui;
3454       if (Register::isVirtualRegister(DestReg))
3455         MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR32RegClass);
3456       else
3457         assert(DestReg != AArch64::WSP);
3458     } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
3459       Opc = AArch64::LDRSui;
3460     break;
3461   case 8:
3462     if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
3463       Opc = AArch64::LDRXui;
3464       if (Register::isVirtualRegister(DestReg))
3465         MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR64RegClass);
3466       else
3467         assert(DestReg != AArch64::SP);
3468     } else if (AArch64::FPR64RegClass.hasSubClassEq(RC)) {
3469       Opc = AArch64::LDRDui;
3470     } else if (AArch64::WSeqPairsClassRegClass.hasSubClassEq(RC)) {
3471       loadRegPairFromStackSlot(getRegisterInfo(), MBB, MBBI,
3472                                get(AArch64::LDPWi), DestReg, AArch64::sube32,
3473                                AArch64::subo32, FI, MMO);
3474       return;
3475     }
3476     break;
3477   case 16:
3478     if (AArch64::FPR128RegClass.hasSubClassEq(RC))
3479       Opc = AArch64::LDRQui;
3480     else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
3481       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
3482       Opc = AArch64::LD1Twov1d;
3483       Offset = false;
3484     } else if (AArch64::XSeqPairsClassRegClass.hasSubClassEq(RC)) {
3485       loadRegPairFromStackSlot(getRegisterInfo(), MBB, MBBI,
3486                                get(AArch64::LDPXi), DestReg, AArch64::sube64,
3487                                AArch64::subo64, FI, MMO);
3488       return;
3489     } else if (AArch64::ZPRRegClass.hasSubClassEq(RC)) {
3490       assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
3491       Opc = AArch64::LDR_ZXI;
3492       StackID = TargetStackID::ScalableVector;
3493     }
3494     break;
3495   case 24:
3496     if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
3497       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
3498       Opc = AArch64::LD1Threev1d;
3499       Offset = false;
3500     }
3501     break;
3502   case 32:
3503     if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
3504       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
3505       Opc = AArch64::LD1Fourv1d;
3506       Offset = false;
3507     } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
3508       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
3509       Opc = AArch64::LD1Twov2d;
3510       Offset = false;
3511     } else if (AArch64::ZPR2RegClass.hasSubClassEq(RC)) {
3512       assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
3513       Opc = AArch64::LDR_ZZXI;
3514       StackID = TargetStackID::ScalableVector;
3515     }
3516     break;
3517   case 48:
3518     if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
3519       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
3520       Opc = AArch64::LD1Threev2d;
3521       Offset = false;
3522     } else if (AArch64::ZPR3RegClass.hasSubClassEq(RC)) {
3523       assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
3524       Opc = AArch64::LDR_ZZZXI;
3525       StackID = TargetStackID::ScalableVector;
3526     }
3527     break;
3528   case 64:
3529     if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
3530       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
3531       Opc = AArch64::LD1Fourv2d;
3532       Offset = false;
3533     } else if (AArch64::ZPR4RegClass.hasSubClassEq(RC)) {
3534       assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
3535       Opc = AArch64::LDR_ZZZZXI;
3536       StackID = TargetStackID::ScalableVector;
3537     }
3538     break;
3539   }
3540 
3541   assert(Opc && "Unknown register class");
3542   MFI.setStackID(FI, StackID);
3543 
3544   const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DebugLoc(), get(Opc))
3545                                      .addReg(DestReg, getDefRegState(true))
3546                                      .addFrameIndex(FI);
3547   if (Offset)
3548     MI.addImm(0);
3549   MI.addMemOperand(MMO);
3550 }
3551 
3552 bool llvm::isNZCVTouchedInInstructionRange(const MachineInstr &DefMI,
3553                                            const MachineInstr &UseMI,
3554                                            const TargetRegisterInfo *TRI) {
3555   return any_of(instructionsWithoutDebug(std::next(DefMI.getIterator()),
3556                                          UseMI.getIterator()),
3557                 [TRI](const MachineInstr &I) {
3558                   return I.modifiesRegister(AArch64::NZCV, TRI) ||
3559                          I.readsRegister(AArch64::NZCV, TRI);
3560                 });
3561 }
3562 
3563 void AArch64InstrInfo::decomposeStackOffsetForDwarfOffsets(
3564     const StackOffset &Offset, int64_t &ByteSized, int64_t &VGSized) {
3565   // The smallest scalable element supported by scaled SVE addressing
3566   // modes are predicates, which are 2 scalable bytes in size. So the scalable
3567   // byte offset must always be a multiple of 2.
3568   assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset");
3569 
3570   // VGSized offsets are divided by '2', because the VG register is the
3571   // the number of 64bit granules as opposed to 128bit vector chunks,
3572   // which is how the 'n' in e.g. MVT::nxv1i8 is modelled.
3573   // So, for a stack offset of 16 MVT::nxv1i8's, the size is n x 16 bytes.
3574   // VG = n * 2 and the dwarf offset must be VG * 8 bytes.
3575   ByteSized = Offset.getFixed();
3576   VGSized = Offset.getScalable() / 2;
3577 }
3578 
3579 /// Returns the offset in parts to which this frame offset can be
3580 /// decomposed for the purpose of describing a frame offset.
3581 /// For non-scalable offsets this is simply its byte size.
3582 void AArch64InstrInfo::decomposeStackOffsetForFrameOffsets(
3583     const StackOffset &Offset, int64_t &NumBytes, int64_t &NumPredicateVectors,
3584     int64_t &NumDataVectors) {
3585   // The smallest scalable element supported by scaled SVE addressing
3586   // modes are predicates, which are 2 scalable bytes in size. So the scalable
3587   // byte offset must always be a multiple of 2.
3588   assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset");
3589 
3590   NumBytes = Offset.getFixed();
3591   NumDataVectors = 0;
3592   NumPredicateVectors = Offset.getScalable() / 2;
3593   // This method is used to get the offsets to adjust the frame offset.
3594   // If the function requires ADDPL to be used and needs more than two ADDPL
3595   // instructions, part of the offset is folded into NumDataVectors so that it
3596   // uses ADDVL for part of it, reducing the number of ADDPL instructions.
3597   if (NumPredicateVectors % 8 == 0 || NumPredicateVectors < -64 ||
3598       NumPredicateVectors > 62) {
3599     NumDataVectors = NumPredicateVectors / 8;
3600     NumPredicateVectors -= NumDataVectors * 8;
3601   }
3602 }
3603 
3604 // Helper function to emit a frame offset adjustment from a given
3605 // pointer (SrcReg), stored into DestReg. This function is explicit
3606 // in that it requires the opcode.
3607 static void emitFrameOffsetAdj(MachineBasicBlock &MBB,
3608                                MachineBasicBlock::iterator MBBI,
3609                                const DebugLoc &DL, unsigned DestReg,
3610                                unsigned SrcReg, int64_t Offset, unsigned Opc,
3611                                const TargetInstrInfo *TII,
3612                                MachineInstr::MIFlag Flag, bool NeedsWinCFI,
3613                                bool *HasWinCFI) {
3614   int Sign = 1;
3615   unsigned MaxEncoding, ShiftSize;
3616   switch (Opc) {
3617   case AArch64::ADDXri:
3618   case AArch64::ADDSXri:
3619   case AArch64::SUBXri:
3620   case AArch64::SUBSXri:
3621     MaxEncoding = 0xfff;
3622     ShiftSize = 12;
3623     break;
3624   case AArch64::ADDVL_XXI:
3625   case AArch64::ADDPL_XXI:
3626     MaxEncoding = 31;
3627     ShiftSize = 0;
3628     if (Offset < 0) {
3629       MaxEncoding = 32;
3630       Sign = -1;
3631       Offset = -Offset;
3632     }
3633     break;
3634   default:
3635     llvm_unreachable("Unsupported opcode");
3636   }
3637 
3638   // FIXME: If the offset won't fit in 24-bits, compute the offset into a
3639   // scratch register.  If DestReg is a virtual register, use it as the
3640   // scratch register; otherwise, create a new virtual register (to be
3641   // replaced by the scavenger at the end of PEI).  That case can be optimized
3642   // slightly if DestReg is SP which is always 16-byte aligned, so the scratch
3643   // register can be loaded with offset%8 and the add/sub can use an extending
3644   // instruction with LSL#3.
3645   // Currently the function handles any offsets but generates a poor sequence
3646   // of code.
3647   //  assert(Offset < (1 << 24) && "unimplemented reg plus immediate");
3648 
3649   const unsigned MaxEncodableValue = MaxEncoding << ShiftSize;
3650   Register TmpReg = DestReg;
3651   if (TmpReg == AArch64::XZR)
3652     TmpReg = MBB.getParent()->getRegInfo().createVirtualRegister(
3653         &AArch64::GPR64RegClass);
3654   do {
3655     uint64_t ThisVal = std::min<uint64_t>(Offset, MaxEncodableValue);
3656     unsigned LocalShiftSize = 0;
3657     if (ThisVal > MaxEncoding) {
3658       ThisVal = ThisVal >> ShiftSize;
3659       LocalShiftSize = ShiftSize;
3660     }
3661     assert((ThisVal >> ShiftSize) <= MaxEncoding &&
3662            "Encoding cannot handle value that big");
3663 
3664     Offset -= ThisVal << LocalShiftSize;
3665     if (Offset == 0)
3666       TmpReg = DestReg;
3667     auto MBI = BuildMI(MBB, MBBI, DL, TII->get(Opc), TmpReg)
3668                    .addReg(SrcReg)
3669                    .addImm(Sign * (int)ThisVal);
3670     if (ShiftSize)
3671       MBI = MBI.addImm(
3672           AArch64_AM::getShifterImm(AArch64_AM::LSL, LocalShiftSize));
3673     MBI = MBI.setMIFlag(Flag);
3674 
3675     if (NeedsWinCFI) {
3676       assert(Sign == 1 && "SEH directives should always have a positive sign");
3677       int Imm = (int)(ThisVal << LocalShiftSize);
3678       if ((DestReg == AArch64::FP && SrcReg == AArch64::SP) ||
3679           (SrcReg == AArch64::FP && DestReg == AArch64::SP)) {
3680         if (HasWinCFI)
3681           *HasWinCFI = true;
3682         if (Imm == 0)
3683           BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_SetFP)).setMIFlag(Flag);
3684         else
3685           BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_AddFP))
3686               .addImm(Imm)
3687               .setMIFlag(Flag);
3688         assert(Offset == 0 && "Expected remaining offset to be zero to "
3689                               "emit a single SEH directive");
3690       } else if (DestReg == AArch64::SP) {
3691         if (HasWinCFI)
3692           *HasWinCFI = true;
3693         assert(SrcReg == AArch64::SP && "Unexpected SrcReg for SEH_StackAlloc");
3694         BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_StackAlloc))
3695             .addImm(Imm)
3696             .setMIFlag(Flag);
3697       }
3698       if (HasWinCFI)
3699         *HasWinCFI = true;
3700     }
3701 
3702     SrcReg = TmpReg;
3703   } while (Offset);
3704 }
3705 
3706 void llvm::emitFrameOffset(MachineBasicBlock &MBB,
3707                            MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
3708                            unsigned DestReg, unsigned SrcReg,
3709                            StackOffset Offset, const TargetInstrInfo *TII,
3710                            MachineInstr::MIFlag Flag, bool SetNZCV,
3711                            bool NeedsWinCFI, bool *HasWinCFI) {
3712   int64_t Bytes, NumPredicateVectors, NumDataVectors;
3713   AArch64InstrInfo::decomposeStackOffsetForFrameOffsets(
3714       Offset, Bytes, NumPredicateVectors, NumDataVectors);
3715 
3716   // First emit non-scalable frame offsets, or a simple 'mov'.
3717   if (Bytes || (!Offset && SrcReg != DestReg)) {
3718     assert((DestReg != AArch64::SP || Bytes % 8 == 0) &&
3719            "SP increment/decrement not 8-byte aligned");
3720     unsigned Opc = SetNZCV ? AArch64::ADDSXri : AArch64::ADDXri;
3721     if (Bytes < 0) {
3722       Bytes = -Bytes;
3723       Opc = SetNZCV ? AArch64::SUBSXri : AArch64::SUBXri;
3724     }
3725     emitFrameOffsetAdj(MBB, MBBI, DL, DestReg, SrcReg, Bytes, Opc, TII, Flag,
3726                        NeedsWinCFI, HasWinCFI);
3727     SrcReg = DestReg;
3728   }
3729 
3730   assert(!(SetNZCV && (NumPredicateVectors || NumDataVectors)) &&
3731          "SetNZCV not supported with SVE vectors");
3732   assert(!(NeedsWinCFI && (NumPredicateVectors || NumDataVectors)) &&
3733          "WinCFI not supported with SVE vectors");
3734 
3735   if (NumDataVectors) {
3736     emitFrameOffsetAdj(MBB, MBBI, DL, DestReg, SrcReg, NumDataVectors,
3737                        AArch64::ADDVL_XXI, TII, Flag, NeedsWinCFI, nullptr);
3738     SrcReg = DestReg;
3739   }
3740 
3741   if (NumPredicateVectors) {
3742     assert(DestReg != AArch64::SP && "Unaligned access to SP");
3743     emitFrameOffsetAdj(MBB, MBBI, DL, DestReg, SrcReg, NumPredicateVectors,
3744                        AArch64::ADDPL_XXI, TII, Flag, NeedsWinCFI, nullptr);
3745   }
3746 }
3747 
3748 MachineInstr *AArch64InstrInfo::foldMemoryOperandImpl(
3749     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
3750     MachineBasicBlock::iterator InsertPt, int FrameIndex,
3751     LiveIntervals *LIS, VirtRegMap *VRM) const {
3752   // This is a bit of a hack. Consider this instruction:
3753   //
3754   //   %0 = COPY %sp; GPR64all:%0
3755   //
3756   // We explicitly chose GPR64all for the virtual register so such a copy might
3757   // be eliminated by RegisterCoalescer. However, that may not be possible, and
3758   // %0 may even spill. We can't spill %sp, and since it is in the GPR64all
3759   // register class, TargetInstrInfo::foldMemoryOperand() is going to try.
3760   //
3761   // To prevent that, we are going to constrain the %0 register class here.
3762   //
3763   // <rdar://problem/11522048>
3764   //
3765   if (MI.isFullCopy()) {
3766     Register DstReg = MI.getOperand(0).getReg();
3767     Register SrcReg = MI.getOperand(1).getReg();
3768     if (SrcReg == AArch64::SP && Register::isVirtualRegister(DstReg)) {
3769       MF.getRegInfo().constrainRegClass(DstReg, &AArch64::GPR64RegClass);
3770       return nullptr;
3771     }
3772     if (DstReg == AArch64::SP && Register::isVirtualRegister(SrcReg)) {
3773       MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
3774       return nullptr;
3775     }
3776   }
3777 
3778   // Handle the case where a copy is being spilled or filled but the source
3779   // and destination register class don't match.  For example:
3780   //
3781   //   %0 = COPY %xzr; GPR64common:%0
3782   //
3783   // In this case we can still safely fold away the COPY and generate the
3784   // following spill code:
3785   //
3786   //   STRXui %xzr, %stack.0
3787   //
3788   // This also eliminates spilled cross register class COPYs (e.g. between x and
3789   // d regs) of the same size.  For example:
3790   //
3791   //   %0 = COPY %1; GPR64:%0, FPR64:%1
3792   //
3793   // will be filled as
3794   //
3795   //   LDRDui %0, fi<#0>
3796   //
3797   // instead of
3798   //
3799   //   LDRXui %Temp, fi<#0>
3800   //   %0 = FMOV %Temp
3801   //
3802   if (MI.isCopy() && Ops.size() == 1 &&
3803       // Make sure we're only folding the explicit COPY defs/uses.
3804       (Ops[0] == 0 || Ops[0] == 1)) {
3805     bool IsSpill = Ops[0] == 0;
3806     bool IsFill = !IsSpill;
3807     const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
3808     const MachineRegisterInfo &MRI = MF.getRegInfo();
3809     MachineBasicBlock &MBB = *MI.getParent();
3810     const MachineOperand &DstMO = MI.getOperand(0);
3811     const MachineOperand &SrcMO = MI.getOperand(1);
3812     Register DstReg = DstMO.getReg();
3813     Register SrcReg = SrcMO.getReg();
3814     // This is slightly expensive to compute for physical regs since
3815     // getMinimalPhysRegClass is slow.
3816     auto getRegClass = [&](unsigned Reg) {
3817       return Register::isVirtualRegister(Reg) ? MRI.getRegClass(Reg)
3818                                               : TRI.getMinimalPhysRegClass(Reg);
3819     };
3820 
3821     if (DstMO.getSubReg() == 0 && SrcMO.getSubReg() == 0) {
3822       assert(TRI.getRegSizeInBits(*getRegClass(DstReg)) ==
3823                  TRI.getRegSizeInBits(*getRegClass(SrcReg)) &&
3824              "Mismatched register size in non subreg COPY");
3825       if (IsSpill)
3826         storeRegToStackSlot(MBB, InsertPt, SrcReg, SrcMO.isKill(), FrameIndex,
3827                             getRegClass(SrcReg), &TRI);
3828       else
3829         loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex,
3830                              getRegClass(DstReg), &TRI);
3831       return &*--InsertPt;
3832     }
3833 
3834     // Handle cases like spilling def of:
3835     //
3836     //   %0:sub_32<def,read-undef> = COPY %wzr; GPR64common:%0
3837     //
3838     // where the physical register source can be widened and stored to the full
3839     // virtual reg destination stack slot, in this case producing:
3840     //
3841     //   STRXui %xzr, %stack.0
3842     //
3843     if (IsSpill && DstMO.isUndef() && Register::isPhysicalRegister(SrcReg)) {
3844       assert(SrcMO.getSubReg() == 0 &&
3845              "Unexpected subreg on physical register");
3846       const TargetRegisterClass *SpillRC;
3847       unsigned SpillSubreg;
3848       switch (DstMO.getSubReg()) {
3849       default:
3850         SpillRC = nullptr;
3851         break;
3852       case AArch64::sub_32:
3853       case AArch64::ssub:
3854         if (AArch64::GPR32RegClass.contains(SrcReg)) {
3855           SpillRC = &AArch64::GPR64RegClass;
3856           SpillSubreg = AArch64::sub_32;
3857         } else if (AArch64::FPR32RegClass.contains(SrcReg)) {
3858           SpillRC = &AArch64::FPR64RegClass;
3859           SpillSubreg = AArch64::ssub;
3860         } else
3861           SpillRC = nullptr;
3862         break;
3863       case AArch64::dsub:
3864         if (AArch64::FPR64RegClass.contains(SrcReg)) {
3865           SpillRC = &AArch64::FPR128RegClass;
3866           SpillSubreg = AArch64::dsub;
3867         } else
3868           SpillRC = nullptr;
3869         break;
3870       }
3871 
3872       if (SpillRC)
3873         if (unsigned WidenedSrcReg =
3874                 TRI.getMatchingSuperReg(SrcReg, SpillSubreg, SpillRC)) {
3875           storeRegToStackSlot(MBB, InsertPt, WidenedSrcReg, SrcMO.isKill(),
3876                               FrameIndex, SpillRC, &TRI);
3877           return &*--InsertPt;
3878         }
3879     }
3880 
3881     // Handle cases like filling use of:
3882     //
3883     //   %0:sub_32<def,read-undef> = COPY %1; GPR64:%0, GPR32:%1
3884     //
3885     // where we can load the full virtual reg source stack slot, into the subreg
3886     // destination, in this case producing:
3887     //
3888     //   LDRWui %0:sub_32<def,read-undef>, %stack.0
3889     //
3890     if (IsFill && SrcMO.getSubReg() == 0 && DstMO.isUndef()) {
3891       const TargetRegisterClass *FillRC;
3892       switch (DstMO.getSubReg()) {
3893       default:
3894         FillRC = nullptr;
3895         break;
3896       case AArch64::sub_32:
3897         FillRC = &AArch64::GPR32RegClass;
3898         break;
3899       case AArch64::ssub:
3900         FillRC = &AArch64::FPR32RegClass;
3901         break;
3902       case AArch64::dsub:
3903         FillRC = &AArch64::FPR64RegClass;
3904         break;
3905       }
3906 
3907       if (FillRC) {
3908         assert(TRI.getRegSizeInBits(*getRegClass(SrcReg)) ==
3909                    TRI.getRegSizeInBits(*FillRC) &&
3910                "Mismatched regclass size on folded subreg COPY");
3911         loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex, FillRC, &TRI);
3912         MachineInstr &LoadMI = *--InsertPt;
3913         MachineOperand &LoadDst = LoadMI.getOperand(0);
3914         assert(LoadDst.getSubReg() == 0 && "unexpected subreg on fill load");
3915         LoadDst.setSubReg(DstMO.getSubReg());
3916         LoadDst.setIsUndef();
3917         return &LoadMI;
3918       }
3919     }
3920   }
3921 
3922   // Cannot fold.
3923   return nullptr;
3924 }
3925 
3926 int llvm::isAArch64FrameOffsetLegal(const MachineInstr &MI,
3927                                     StackOffset &SOffset,
3928                                     bool *OutUseUnscaledOp,
3929                                     unsigned *OutUnscaledOp,
3930                                     int64_t *EmittableOffset) {
3931   // Set output values in case of early exit.
3932   if (EmittableOffset)
3933     *EmittableOffset = 0;
3934   if (OutUseUnscaledOp)
3935     *OutUseUnscaledOp = false;
3936   if (OutUnscaledOp)
3937     *OutUnscaledOp = 0;
3938 
3939   // Exit early for structured vector spills/fills as they can't take an
3940   // immediate offset.
3941   switch (MI.getOpcode()) {
3942   default:
3943     break;
3944   case AArch64::LD1Twov2d:
3945   case AArch64::LD1Threev2d:
3946   case AArch64::LD1Fourv2d:
3947   case AArch64::LD1Twov1d:
3948   case AArch64::LD1Threev1d:
3949   case AArch64::LD1Fourv1d:
3950   case AArch64::ST1Twov2d:
3951   case AArch64::ST1Threev2d:
3952   case AArch64::ST1Fourv2d:
3953   case AArch64::ST1Twov1d:
3954   case AArch64::ST1Threev1d:
3955   case AArch64::ST1Fourv1d:
3956   case AArch64::IRG:
3957   case AArch64::IRGstack:
3958   case AArch64::STGloop:
3959   case AArch64::STZGloop:
3960     return AArch64FrameOffsetCannotUpdate;
3961   }
3962 
3963   // Get the min/max offset and the scale.
3964   TypeSize ScaleValue(0U, false);
3965   unsigned Width;
3966   int64_t MinOff, MaxOff;
3967   if (!AArch64InstrInfo::getMemOpInfo(MI.getOpcode(), ScaleValue, Width, MinOff,
3968                                       MaxOff))
3969     llvm_unreachable("unhandled opcode in isAArch64FrameOffsetLegal");
3970 
3971   // Construct the complete offset.
3972   bool IsMulVL = ScaleValue.isScalable();
3973   unsigned Scale = ScaleValue.getKnownMinSize();
3974   int64_t Offset = IsMulVL ? SOffset.getScalable() : SOffset.getFixed();
3975 
3976   const MachineOperand &ImmOpnd =
3977       MI.getOperand(AArch64InstrInfo::getLoadStoreImmIdx(MI.getOpcode()));
3978   Offset += ImmOpnd.getImm() * Scale;
3979 
3980   // If the offset doesn't match the scale, we rewrite the instruction to
3981   // use the unscaled instruction instead. Likewise, if we have a negative
3982   // offset and there is an unscaled op to use.
3983   Optional<unsigned> UnscaledOp =
3984       AArch64InstrInfo::getUnscaledLdSt(MI.getOpcode());
3985   bool useUnscaledOp = UnscaledOp && (Offset % Scale || Offset < 0);
3986   if (useUnscaledOp &&
3987       !AArch64InstrInfo::getMemOpInfo(*UnscaledOp, ScaleValue, Width, MinOff,
3988                                       MaxOff))
3989     llvm_unreachable("unhandled opcode in isAArch64FrameOffsetLegal");
3990 
3991   Scale = ScaleValue.getKnownMinSize();
3992   assert(IsMulVL == ScaleValue.isScalable() &&
3993          "Unscaled opcode has different value for scalable");
3994 
3995   int64_t Remainder = Offset % Scale;
3996   assert(!(Remainder && useUnscaledOp) &&
3997          "Cannot have remainder when using unscaled op");
3998 
3999   assert(MinOff < MaxOff && "Unexpected Min/Max offsets");
4000   int64_t NewOffset = Offset / Scale;
4001   if (MinOff <= NewOffset && NewOffset <= MaxOff)
4002     Offset = Remainder;
4003   else {
4004     NewOffset = NewOffset < 0 ? MinOff : MaxOff;
4005     Offset = Offset - NewOffset * Scale + Remainder;
4006   }
4007 
4008   if (EmittableOffset)
4009     *EmittableOffset = NewOffset;
4010   if (OutUseUnscaledOp)
4011     *OutUseUnscaledOp = useUnscaledOp;
4012   if (OutUnscaledOp && UnscaledOp)
4013     *OutUnscaledOp = *UnscaledOp;
4014 
4015   if (IsMulVL)
4016     SOffset = StackOffset::get(SOffset.getFixed(), Offset);
4017   else
4018     SOffset = StackOffset::get(Offset, SOffset.getScalable());
4019   return AArch64FrameOffsetCanUpdate |
4020          (SOffset ? 0 : AArch64FrameOffsetIsLegal);
4021 }
4022 
4023 bool llvm::rewriteAArch64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
4024                                     unsigned FrameReg, StackOffset &Offset,
4025                                     const AArch64InstrInfo *TII) {
4026   unsigned Opcode = MI.getOpcode();
4027   unsigned ImmIdx = FrameRegIdx + 1;
4028 
4029   if (Opcode == AArch64::ADDSXri || Opcode == AArch64::ADDXri) {
4030     Offset += StackOffset::getFixed(MI.getOperand(ImmIdx).getImm());
4031     emitFrameOffset(*MI.getParent(), MI, MI.getDebugLoc(),
4032                     MI.getOperand(0).getReg(), FrameReg, Offset, TII,
4033                     MachineInstr::NoFlags, (Opcode == AArch64::ADDSXri));
4034     MI.eraseFromParent();
4035     Offset = StackOffset();
4036     return true;
4037   }
4038 
4039   int64_t NewOffset;
4040   unsigned UnscaledOp;
4041   bool UseUnscaledOp;
4042   int Status = isAArch64FrameOffsetLegal(MI, Offset, &UseUnscaledOp,
4043                                          &UnscaledOp, &NewOffset);
4044   if (Status & AArch64FrameOffsetCanUpdate) {
4045     if (Status & AArch64FrameOffsetIsLegal)
4046       // Replace the FrameIndex with FrameReg.
4047       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
4048     if (UseUnscaledOp)
4049       MI.setDesc(TII->get(UnscaledOp));
4050 
4051     MI.getOperand(ImmIdx).ChangeToImmediate(NewOffset);
4052     return !Offset;
4053   }
4054 
4055   return false;
4056 }
4057 
4058 MCInst AArch64InstrInfo::getNop() const {
4059   return MCInstBuilder(AArch64::HINT).addImm(0);
4060 }
4061 
4062 // AArch64 supports MachineCombiner.
4063 bool AArch64InstrInfo::useMachineCombiner() const { return true; }
4064 
4065 // True when Opc sets flag
4066 static bool isCombineInstrSettingFlag(unsigned Opc) {
4067   switch (Opc) {
4068   case AArch64::ADDSWrr:
4069   case AArch64::ADDSWri:
4070   case AArch64::ADDSXrr:
4071   case AArch64::ADDSXri:
4072   case AArch64::SUBSWrr:
4073   case AArch64::SUBSXrr:
4074   // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
4075   case AArch64::SUBSWri:
4076   case AArch64::SUBSXri:
4077     return true;
4078   default:
4079     break;
4080   }
4081   return false;
4082 }
4083 
4084 // 32b Opcodes that can be combined with a MUL
4085 static bool isCombineInstrCandidate32(unsigned Opc) {
4086   switch (Opc) {
4087   case AArch64::ADDWrr:
4088   case AArch64::ADDWri:
4089   case AArch64::SUBWrr:
4090   case AArch64::ADDSWrr:
4091   case AArch64::ADDSWri:
4092   case AArch64::SUBSWrr:
4093   // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
4094   case AArch64::SUBWri:
4095   case AArch64::SUBSWri:
4096     return true;
4097   default:
4098     break;
4099   }
4100   return false;
4101 }
4102 
4103 // 64b Opcodes that can be combined with a MUL
4104 static bool isCombineInstrCandidate64(unsigned Opc) {
4105   switch (Opc) {
4106   case AArch64::ADDXrr:
4107   case AArch64::ADDXri:
4108   case AArch64::SUBXrr:
4109   case AArch64::ADDSXrr:
4110   case AArch64::ADDSXri:
4111   case AArch64::SUBSXrr:
4112   // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
4113   case AArch64::SUBXri:
4114   case AArch64::SUBSXri:
4115   case AArch64::ADDv8i8:
4116   case AArch64::ADDv16i8:
4117   case AArch64::ADDv4i16:
4118   case AArch64::ADDv8i16:
4119   case AArch64::ADDv2i32:
4120   case AArch64::ADDv4i32:
4121   case AArch64::SUBv8i8:
4122   case AArch64::SUBv16i8:
4123   case AArch64::SUBv4i16:
4124   case AArch64::SUBv8i16:
4125   case AArch64::SUBv2i32:
4126   case AArch64::SUBv4i32:
4127     return true;
4128   default:
4129     break;
4130   }
4131   return false;
4132 }
4133 
4134 // FP Opcodes that can be combined with a FMUL.
4135 static bool isCombineInstrCandidateFP(const MachineInstr &Inst) {
4136   switch (Inst.getOpcode()) {
4137   default:
4138     break;
4139   case AArch64::FADDHrr:
4140   case AArch64::FADDSrr:
4141   case AArch64::FADDDrr:
4142   case AArch64::FADDv4f16:
4143   case AArch64::FADDv8f16:
4144   case AArch64::FADDv2f32:
4145   case AArch64::FADDv2f64:
4146   case AArch64::FADDv4f32:
4147   case AArch64::FSUBHrr:
4148   case AArch64::FSUBSrr:
4149   case AArch64::FSUBDrr:
4150   case AArch64::FSUBv4f16:
4151   case AArch64::FSUBv8f16:
4152   case AArch64::FSUBv2f32:
4153   case AArch64::FSUBv2f64:
4154   case AArch64::FSUBv4f32:
4155     TargetOptions Options = Inst.getParent()->getParent()->getTarget().Options;
4156     // We can fuse FADD/FSUB with FMUL, if fusion is either allowed globally by
4157     // the target options or if FADD/FSUB has the contract fast-math flag.
4158     return Options.UnsafeFPMath ||
4159            Options.AllowFPOpFusion == FPOpFusion::Fast ||
4160            Inst.getFlag(MachineInstr::FmContract);
4161     return true;
4162   }
4163   return false;
4164 }
4165 
4166 // Opcodes that can be combined with a MUL
4167 static bool isCombineInstrCandidate(unsigned Opc) {
4168   return (isCombineInstrCandidate32(Opc) || isCombineInstrCandidate64(Opc));
4169 }
4170 
4171 //
4172 // Utility routine that checks if \param MO is defined by an
4173 // \param CombineOpc instruction in the basic block \param MBB
4174 static bool canCombine(MachineBasicBlock &MBB, MachineOperand &MO,
4175                        unsigned CombineOpc, unsigned ZeroReg = 0,
4176                        bool CheckZeroReg = false) {
4177   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4178   MachineInstr *MI = nullptr;
4179 
4180   if (MO.isReg() && Register::isVirtualRegister(MO.getReg()))
4181     MI = MRI.getUniqueVRegDef(MO.getReg());
4182   // And it needs to be in the trace (otherwise, it won't have a depth).
4183   if (!MI || MI->getParent() != &MBB || (unsigned)MI->getOpcode() != CombineOpc)
4184     return false;
4185   // Must only used by the user we combine with.
4186   if (!MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
4187     return false;
4188 
4189   if (CheckZeroReg) {
4190     assert(MI->getNumOperands() >= 4 && MI->getOperand(0).isReg() &&
4191            MI->getOperand(1).isReg() && MI->getOperand(2).isReg() &&
4192            MI->getOperand(3).isReg() && "MAdd/MSub must have a least 4 regs");
4193     // The third input reg must be zero.
4194     if (MI->getOperand(3).getReg() != ZeroReg)
4195       return false;
4196   }
4197 
4198   return true;
4199 }
4200 
4201 //
4202 // Is \param MO defined by an integer multiply and can be combined?
4203 static bool canCombineWithMUL(MachineBasicBlock &MBB, MachineOperand &MO,
4204                               unsigned MulOpc, unsigned ZeroReg) {
4205   return canCombine(MBB, MO, MulOpc, ZeroReg, true);
4206 }
4207 
4208 //
4209 // Is \param MO defined by a floating-point multiply and can be combined?
4210 static bool canCombineWithFMUL(MachineBasicBlock &MBB, MachineOperand &MO,
4211                                unsigned MulOpc) {
4212   return canCombine(MBB, MO, MulOpc);
4213 }
4214 
4215 // TODO: There are many more machine instruction opcodes to match:
4216 //       1. Other data types (integer, vectors)
4217 //       2. Other math / logic operations (xor, or)
4218 //       3. Other forms of the same operation (intrinsics and other variants)
4219 bool AArch64InstrInfo::isAssociativeAndCommutative(
4220     const MachineInstr &Inst) const {
4221   switch (Inst.getOpcode()) {
4222   case AArch64::FADDDrr:
4223   case AArch64::FADDSrr:
4224   case AArch64::FADDv2f32:
4225   case AArch64::FADDv2f64:
4226   case AArch64::FADDv4f32:
4227   case AArch64::FMULDrr:
4228   case AArch64::FMULSrr:
4229   case AArch64::FMULX32:
4230   case AArch64::FMULX64:
4231   case AArch64::FMULXv2f32:
4232   case AArch64::FMULXv2f64:
4233   case AArch64::FMULXv4f32:
4234   case AArch64::FMULv2f32:
4235   case AArch64::FMULv2f64:
4236   case AArch64::FMULv4f32:
4237     return Inst.getParent()->getParent()->getTarget().Options.UnsafeFPMath;
4238   default:
4239     return false;
4240   }
4241 }
4242 
4243 /// Find instructions that can be turned into madd.
4244 static bool getMaddPatterns(MachineInstr &Root,
4245                             SmallVectorImpl<MachineCombinerPattern> &Patterns) {
4246   unsigned Opc = Root.getOpcode();
4247   MachineBasicBlock &MBB = *Root.getParent();
4248   bool Found = false;
4249 
4250   if (!isCombineInstrCandidate(Opc))
4251     return false;
4252   if (isCombineInstrSettingFlag(Opc)) {
4253     int Cmp_NZCV = Root.findRegisterDefOperandIdx(AArch64::NZCV, true);
4254     // When NZCV is live bail out.
4255     if (Cmp_NZCV == -1)
4256       return false;
4257     unsigned NewOpc = convertToNonFlagSettingOpc(Root);
4258     // When opcode can't change bail out.
4259     // CHECKME: do we miss any cases for opcode conversion?
4260     if (NewOpc == Opc)
4261       return false;
4262     Opc = NewOpc;
4263   }
4264 
4265   auto setFound = [&](int Opcode, int Operand, unsigned ZeroReg,
4266                       MachineCombinerPattern Pattern) {
4267     if (canCombineWithMUL(MBB, Root.getOperand(Operand), Opcode, ZeroReg)) {
4268       Patterns.push_back(Pattern);
4269       Found = true;
4270     }
4271   };
4272 
4273   auto setVFound = [&](int Opcode, int Operand, MachineCombinerPattern Pattern) {
4274     if (canCombine(MBB, Root.getOperand(Operand), Opcode)) {
4275       Patterns.push_back(Pattern);
4276       Found = true;
4277     }
4278   };
4279 
4280   typedef MachineCombinerPattern MCP;
4281 
4282   switch (Opc) {
4283   default:
4284     break;
4285   case AArch64::ADDWrr:
4286     assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
4287            "ADDWrr does not have register operands");
4288     setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULADDW_OP1);
4289     setFound(AArch64::MADDWrrr, 2, AArch64::WZR, MCP::MULADDW_OP2);
4290     break;
4291   case AArch64::ADDXrr:
4292     setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULADDX_OP1);
4293     setFound(AArch64::MADDXrrr, 2, AArch64::XZR, MCP::MULADDX_OP2);
4294     break;
4295   case AArch64::SUBWrr:
4296     setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULSUBW_OP1);
4297     setFound(AArch64::MADDWrrr, 2, AArch64::WZR, MCP::MULSUBW_OP2);
4298     break;
4299   case AArch64::SUBXrr:
4300     setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULSUBX_OP1);
4301     setFound(AArch64::MADDXrrr, 2, AArch64::XZR, MCP::MULSUBX_OP2);
4302     break;
4303   case AArch64::ADDWri:
4304     setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULADDWI_OP1);
4305     break;
4306   case AArch64::ADDXri:
4307     setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULADDXI_OP1);
4308     break;
4309   case AArch64::SUBWri:
4310     setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULSUBWI_OP1);
4311     break;
4312   case AArch64::SUBXri:
4313     setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULSUBXI_OP1);
4314     break;
4315   case AArch64::ADDv8i8:
4316     setVFound(AArch64::MULv8i8, 1, MCP::MULADDv8i8_OP1);
4317     setVFound(AArch64::MULv8i8, 2, MCP::MULADDv8i8_OP2);
4318     break;
4319   case AArch64::ADDv16i8:
4320     setVFound(AArch64::MULv16i8, 1, MCP::MULADDv16i8_OP1);
4321     setVFound(AArch64::MULv16i8, 2, MCP::MULADDv16i8_OP2);
4322     break;
4323   case AArch64::ADDv4i16:
4324     setVFound(AArch64::MULv4i16, 1, MCP::MULADDv4i16_OP1);
4325     setVFound(AArch64::MULv4i16, 2, MCP::MULADDv4i16_OP2);
4326     setVFound(AArch64::MULv4i16_indexed, 1, MCP::MULADDv4i16_indexed_OP1);
4327     setVFound(AArch64::MULv4i16_indexed, 2, MCP::MULADDv4i16_indexed_OP2);
4328     break;
4329   case AArch64::ADDv8i16:
4330     setVFound(AArch64::MULv8i16, 1, MCP::MULADDv8i16_OP1);
4331     setVFound(AArch64::MULv8i16, 2, MCP::MULADDv8i16_OP2);
4332     setVFound(AArch64::MULv8i16_indexed, 1, MCP::MULADDv8i16_indexed_OP1);
4333     setVFound(AArch64::MULv8i16_indexed, 2, MCP::MULADDv8i16_indexed_OP2);
4334     break;
4335   case AArch64::ADDv2i32:
4336     setVFound(AArch64::MULv2i32, 1, MCP::MULADDv2i32_OP1);
4337     setVFound(AArch64::MULv2i32, 2, MCP::MULADDv2i32_OP2);
4338     setVFound(AArch64::MULv2i32_indexed, 1, MCP::MULADDv2i32_indexed_OP1);
4339     setVFound(AArch64::MULv2i32_indexed, 2, MCP::MULADDv2i32_indexed_OP2);
4340     break;
4341   case AArch64::ADDv4i32:
4342     setVFound(AArch64::MULv4i32, 1, MCP::MULADDv4i32_OP1);
4343     setVFound(AArch64::MULv4i32, 2, MCP::MULADDv4i32_OP2);
4344     setVFound(AArch64::MULv4i32_indexed, 1, MCP::MULADDv4i32_indexed_OP1);
4345     setVFound(AArch64::MULv4i32_indexed, 2, MCP::MULADDv4i32_indexed_OP2);
4346     break;
4347   case AArch64::SUBv8i8:
4348     setVFound(AArch64::MULv8i8, 1, MCP::MULSUBv8i8_OP1);
4349     setVFound(AArch64::MULv8i8, 2, MCP::MULSUBv8i8_OP2);
4350     break;
4351   case AArch64::SUBv16i8:
4352     setVFound(AArch64::MULv16i8, 1, MCP::MULSUBv16i8_OP1);
4353     setVFound(AArch64::MULv16i8, 2, MCP::MULSUBv16i8_OP2);
4354     break;
4355   case AArch64::SUBv4i16:
4356     setVFound(AArch64::MULv4i16, 1, MCP::MULSUBv4i16_OP1);
4357     setVFound(AArch64::MULv4i16, 2, MCP::MULSUBv4i16_OP2);
4358     setVFound(AArch64::MULv4i16_indexed, 1, MCP::MULSUBv4i16_indexed_OP1);
4359     setVFound(AArch64::MULv4i16_indexed, 2, MCP::MULSUBv4i16_indexed_OP2);
4360     break;
4361   case AArch64::SUBv8i16:
4362     setVFound(AArch64::MULv8i16, 1, MCP::MULSUBv8i16_OP1);
4363     setVFound(AArch64::MULv8i16, 2, MCP::MULSUBv8i16_OP2);
4364     setVFound(AArch64::MULv8i16_indexed, 1, MCP::MULSUBv8i16_indexed_OP1);
4365     setVFound(AArch64::MULv8i16_indexed, 2, MCP::MULSUBv8i16_indexed_OP2);
4366     break;
4367   case AArch64::SUBv2i32:
4368     setVFound(AArch64::MULv2i32, 1, MCP::MULSUBv2i32_OP1);
4369     setVFound(AArch64::MULv2i32, 2, MCP::MULSUBv2i32_OP2);
4370     setVFound(AArch64::MULv2i32_indexed, 1, MCP::MULSUBv2i32_indexed_OP1);
4371     setVFound(AArch64::MULv2i32_indexed, 2, MCP::MULSUBv2i32_indexed_OP2);
4372     break;
4373   case AArch64::SUBv4i32:
4374     setVFound(AArch64::MULv4i32, 1, MCP::MULSUBv4i32_OP1);
4375     setVFound(AArch64::MULv4i32, 2, MCP::MULSUBv4i32_OP2);
4376     setVFound(AArch64::MULv4i32_indexed, 1, MCP::MULSUBv4i32_indexed_OP1);
4377     setVFound(AArch64::MULv4i32_indexed, 2, MCP::MULSUBv4i32_indexed_OP2);
4378     break;
4379   }
4380   return Found;
4381 }
4382 /// Floating-Point Support
4383 
4384 /// Find instructions that can be turned into madd.
4385 static bool getFMAPatterns(MachineInstr &Root,
4386                            SmallVectorImpl<MachineCombinerPattern> &Patterns) {
4387 
4388   if (!isCombineInstrCandidateFP(Root))
4389     return false;
4390 
4391   MachineBasicBlock &MBB = *Root.getParent();
4392   bool Found = false;
4393 
4394   auto Match = [&](int Opcode, int Operand,
4395                    MachineCombinerPattern Pattern) -> bool {
4396     if (canCombineWithFMUL(MBB, Root.getOperand(Operand), Opcode)) {
4397       Patterns.push_back(Pattern);
4398       return true;
4399     }
4400     return false;
4401   };
4402 
4403   typedef MachineCombinerPattern MCP;
4404 
4405   switch (Root.getOpcode()) {
4406   default:
4407     assert(false && "Unsupported FP instruction in combiner\n");
4408     break;
4409   case AArch64::FADDHrr:
4410     assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
4411            "FADDHrr does not have register operands");
4412 
4413     Found  = Match(AArch64::FMULHrr, 1, MCP::FMULADDH_OP1);
4414     Found |= Match(AArch64::FMULHrr, 2, MCP::FMULADDH_OP2);
4415     break;
4416   case AArch64::FADDSrr:
4417     assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
4418            "FADDSrr does not have register operands");
4419 
4420     Found |= Match(AArch64::FMULSrr, 1, MCP::FMULADDS_OP1) ||
4421              Match(AArch64::FMULv1i32_indexed, 1, MCP::FMLAv1i32_indexed_OP1);
4422 
4423     Found |= Match(AArch64::FMULSrr, 2, MCP::FMULADDS_OP2) ||
4424              Match(AArch64::FMULv1i32_indexed, 2, MCP::FMLAv1i32_indexed_OP2);
4425     break;
4426   case AArch64::FADDDrr:
4427     Found |= Match(AArch64::FMULDrr, 1, MCP::FMULADDD_OP1) ||
4428              Match(AArch64::FMULv1i64_indexed, 1, MCP::FMLAv1i64_indexed_OP1);
4429 
4430     Found |= Match(AArch64::FMULDrr, 2, MCP::FMULADDD_OP2) ||
4431              Match(AArch64::FMULv1i64_indexed, 2, MCP::FMLAv1i64_indexed_OP2);
4432     break;
4433   case AArch64::FADDv4f16:
4434     Found |= Match(AArch64::FMULv4i16_indexed, 1, MCP::FMLAv4i16_indexed_OP1) ||
4435              Match(AArch64::FMULv4f16, 1, MCP::FMLAv4f16_OP1);
4436 
4437     Found |= Match(AArch64::FMULv4i16_indexed, 2, MCP::FMLAv4i16_indexed_OP2) ||
4438              Match(AArch64::FMULv4f16, 2, MCP::FMLAv4f16_OP2);
4439     break;
4440   case AArch64::FADDv8f16:
4441     Found |= Match(AArch64::FMULv8i16_indexed, 1, MCP::FMLAv8i16_indexed_OP1) ||
4442              Match(AArch64::FMULv8f16, 1, MCP::FMLAv8f16_OP1);
4443 
4444     Found |= Match(AArch64::FMULv8i16_indexed, 2, MCP::FMLAv8i16_indexed_OP2) ||
4445              Match(AArch64::FMULv8f16, 2, MCP::FMLAv8f16_OP2);
4446     break;
4447   case AArch64::FADDv2f32:
4448     Found |= Match(AArch64::FMULv2i32_indexed, 1, MCP::FMLAv2i32_indexed_OP1) ||
4449              Match(AArch64::FMULv2f32, 1, MCP::FMLAv2f32_OP1);
4450 
4451     Found |= Match(AArch64::FMULv2i32_indexed, 2, MCP::FMLAv2i32_indexed_OP2) ||
4452              Match(AArch64::FMULv2f32, 2, MCP::FMLAv2f32_OP2);
4453     break;
4454   case AArch64::FADDv2f64:
4455     Found |= Match(AArch64::FMULv2i64_indexed, 1, MCP::FMLAv2i64_indexed_OP1) ||
4456              Match(AArch64::FMULv2f64, 1, MCP::FMLAv2f64_OP1);
4457 
4458     Found |= Match(AArch64::FMULv2i64_indexed, 2, MCP::FMLAv2i64_indexed_OP2) ||
4459              Match(AArch64::FMULv2f64, 2, MCP::FMLAv2f64_OP2);
4460     break;
4461   case AArch64::FADDv4f32:
4462     Found |= Match(AArch64::FMULv4i32_indexed, 1, MCP::FMLAv4i32_indexed_OP1) ||
4463              Match(AArch64::FMULv4f32, 1, MCP::FMLAv4f32_OP1);
4464 
4465     Found |= Match(AArch64::FMULv4i32_indexed, 2, MCP::FMLAv4i32_indexed_OP2) ||
4466              Match(AArch64::FMULv4f32, 2, MCP::FMLAv4f32_OP2);
4467     break;
4468   case AArch64::FSUBHrr:
4469     Found  = Match(AArch64::FMULHrr, 1, MCP::FMULSUBH_OP1);
4470     Found |= Match(AArch64::FMULHrr, 2, MCP::FMULSUBH_OP2);
4471     Found |= Match(AArch64::FNMULHrr, 1, MCP::FNMULSUBH_OP1);
4472     break;
4473   case AArch64::FSUBSrr:
4474     Found = Match(AArch64::FMULSrr, 1, MCP::FMULSUBS_OP1);
4475 
4476     Found |= Match(AArch64::FMULSrr, 2, MCP::FMULSUBS_OP2) ||
4477              Match(AArch64::FMULv1i32_indexed, 2, MCP::FMLSv1i32_indexed_OP2);
4478 
4479     Found |= Match(AArch64::FNMULSrr, 1, MCP::FNMULSUBS_OP1);
4480     break;
4481   case AArch64::FSUBDrr:
4482     Found = Match(AArch64::FMULDrr, 1, MCP::FMULSUBD_OP1);
4483 
4484     Found |= Match(AArch64::FMULDrr, 2, MCP::FMULSUBD_OP2) ||
4485              Match(AArch64::FMULv1i64_indexed, 2, MCP::FMLSv1i64_indexed_OP2);
4486 
4487     Found |= Match(AArch64::FNMULDrr, 1, MCP::FNMULSUBD_OP1);
4488     break;
4489   case AArch64::FSUBv4f16:
4490     Found |= Match(AArch64::FMULv4i16_indexed, 2, MCP::FMLSv4i16_indexed_OP2) ||
4491              Match(AArch64::FMULv4f16, 2, MCP::FMLSv4f16_OP2);
4492 
4493     Found |= Match(AArch64::FMULv4i16_indexed, 1, MCP::FMLSv4i16_indexed_OP1) ||
4494              Match(AArch64::FMULv4f16, 1, MCP::FMLSv4f16_OP1);
4495     break;
4496   case AArch64::FSUBv8f16:
4497     Found |= Match(AArch64::FMULv8i16_indexed, 2, MCP::FMLSv8i16_indexed_OP2) ||
4498              Match(AArch64::FMULv8f16, 2, MCP::FMLSv8f16_OP2);
4499 
4500     Found |= Match(AArch64::FMULv8i16_indexed, 1, MCP::FMLSv8i16_indexed_OP1) ||
4501              Match(AArch64::FMULv8f16, 1, MCP::FMLSv8f16_OP1);
4502     break;
4503   case AArch64::FSUBv2f32:
4504     Found |= Match(AArch64::FMULv2i32_indexed, 2, MCP::FMLSv2i32_indexed_OP2) ||
4505              Match(AArch64::FMULv2f32, 2, MCP::FMLSv2f32_OP2);
4506 
4507     Found |= Match(AArch64::FMULv2i32_indexed, 1, MCP::FMLSv2i32_indexed_OP1) ||
4508              Match(AArch64::FMULv2f32, 1, MCP::FMLSv2f32_OP1);
4509     break;
4510   case AArch64::FSUBv2f64:
4511     Found |= Match(AArch64::FMULv2i64_indexed, 2, MCP::FMLSv2i64_indexed_OP2) ||
4512              Match(AArch64::FMULv2f64, 2, MCP::FMLSv2f64_OP2);
4513 
4514     Found |= Match(AArch64::FMULv2i64_indexed, 1, MCP::FMLSv2i64_indexed_OP1) ||
4515              Match(AArch64::FMULv2f64, 1, MCP::FMLSv2f64_OP1);
4516     break;
4517   case AArch64::FSUBv4f32:
4518     Found |= Match(AArch64::FMULv4i32_indexed, 2, MCP::FMLSv4i32_indexed_OP2) ||
4519              Match(AArch64::FMULv4f32, 2, MCP::FMLSv4f32_OP2);
4520 
4521     Found |= Match(AArch64::FMULv4i32_indexed, 1, MCP::FMLSv4i32_indexed_OP1) ||
4522              Match(AArch64::FMULv4f32, 1, MCP::FMLSv4f32_OP1);
4523     break;
4524   }
4525   return Found;
4526 }
4527 
4528 /// Return true when a code sequence can improve throughput. It
4529 /// should be called only for instructions in loops.
4530 /// \param Pattern - combiner pattern
4531 bool AArch64InstrInfo::isThroughputPattern(
4532     MachineCombinerPattern Pattern) const {
4533   switch (Pattern) {
4534   default:
4535     break;
4536   case MachineCombinerPattern::FMULADDH_OP1:
4537   case MachineCombinerPattern::FMULADDH_OP2:
4538   case MachineCombinerPattern::FMULSUBH_OP1:
4539   case MachineCombinerPattern::FMULSUBH_OP2:
4540   case MachineCombinerPattern::FMULADDS_OP1:
4541   case MachineCombinerPattern::FMULADDS_OP2:
4542   case MachineCombinerPattern::FMULSUBS_OP1:
4543   case MachineCombinerPattern::FMULSUBS_OP2:
4544   case MachineCombinerPattern::FMULADDD_OP1:
4545   case MachineCombinerPattern::FMULADDD_OP2:
4546   case MachineCombinerPattern::FMULSUBD_OP1:
4547   case MachineCombinerPattern::FMULSUBD_OP2:
4548   case MachineCombinerPattern::FNMULSUBH_OP1:
4549   case MachineCombinerPattern::FNMULSUBS_OP1:
4550   case MachineCombinerPattern::FNMULSUBD_OP1:
4551   case MachineCombinerPattern::FMLAv4i16_indexed_OP1:
4552   case MachineCombinerPattern::FMLAv4i16_indexed_OP2:
4553   case MachineCombinerPattern::FMLAv8i16_indexed_OP1:
4554   case MachineCombinerPattern::FMLAv8i16_indexed_OP2:
4555   case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
4556   case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
4557   case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
4558   case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
4559   case MachineCombinerPattern::FMLAv4f16_OP2:
4560   case MachineCombinerPattern::FMLAv4f16_OP1:
4561   case MachineCombinerPattern::FMLAv8f16_OP1:
4562   case MachineCombinerPattern::FMLAv8f16_OP2:
4563   case MachineCombinerPattern::FMLAv2f32_OP2:
4564   case MachineCombinerPattern::FMLAv2f32_OP1:
4565   case MachineCombinerPattern::FMLAv2f64_OP1:
4566   case MachineCombinerPattern::FMLAv2f64_OP2:
4567   case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
4568   case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
4569   case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
4570   case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
4571   case MachineCombinerPattern::FMLAv4f32_OP1:
4572   case MachineCombinerPattern::FMLAv4f32_OP2:
4573   case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
4574   case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
4575   case MachineCombinerPattern::FMLSv4i16_indexed_OP1:
4576   case MachineCombinerPattern::FMLSv4i16_indexed_OP2:
4577   case MachineCombinerPattern::FMLSv8i16_indexed_OP1:
4578   case MachineCombinerPattern::FMLSv8i16_indexed_OP2:
4579   case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
4580   case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
4581   case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
4582   case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
4583   case MachineCombinerPattern::FMLSv4f16_OP1:
4584   case MachineCombinerPattern::FMLSv4f16_OP2:
4585   case MachineCombinerPattern::FMLSv8f16_OP1:
4586   case MachineCombinerPattern::FMLSv8f16_OP2:
4587   case MachineCombinerPattern::FMLSv2f32_OP2:
4588   case MachineCombinerPattern::FMLSv2f64_OP2:
4589   case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
4590   case MachineCombinerPattern::FMLSv4f32_OP2:
4591   case MachineCombinerPattern::MULADDv8i8_OP1:
4592   case MachineCombinerPattern::MULADDv8i8_OP2:
4593   case MachineCombinerPattern::MULADDv16i8_OP1:
4594   case MachineCombinerPattern::MULADDv16i8_OP2:
4595   case MachineCombinerPattern::MULADDv4i16_OP1:
4596   case MachineCombinerPattern::MULADDv4i16_OP2:
4597   case MachineCombinerPattern::MULADDv8i16_OP1:
4598   case MachineCombinerPattern::MULADDv8i16_OP2:
4599   case MachineCombinerPattern::MULADDv2i32_OP1:
4600   case MachineCombinerPattern::MULADDv2i32_OP2:
4601   case MachineCombinerPattern::MULADDv4i32_OP1:
4602   case MachineCombinerPattern::MULADDv4i32_OP2:
4603   case MachineCombinerPattern::MULSUBv8i8_OP1:
4604   case MachineCombinerPattern::MULSUBv8i8_OP2:
4605   case MachineCombinerPattern::MULSUBv16i8_OP1:
4606   case MachineCombinerPattern::MULSUBv16i8_OP2:
4607   case MachineCombinerPattern::MULSUBv4i16_OP1:
4608   case MachineCombinerPattern::MULSUBv4i16_OP2:
4609   case MachineCombinerPattern::MULSUBv8i16_OP1:
4610   case MachineCombinerPattern::MULSUBv8i16_OP2:
4611   case MachineCombinerPattern::MULSUBv2i32_OP1:
4612   case MachineCombinerPattern::MULSUBv2i32_OP2:
4613   case MachineCombinerPattern::MULSUBv4i32_OP1:
4614   case MachineCombinerPattern::MULSUBv4i32_OP2:
4615   case MachineCombinerPattern::MULADDv4i16_indexed_OP1:
4616   case MachineCombinerPattern::MULADDv4i16_indexed_OP2:
4617   case MachineCombinerPattern::MULADDv8i16_indexed_OP1:
4618   case MachineCombinerPattern::MULADDv8i16_indexed_OP2:
4619   case MachineCombinerPattern::MULADDv2i32_indexed_OP1:
4620   case MachineCombinerPattern::MULADDv2i32_indexed_OP2:
4621   case MachineCombinerPattern::MULADDv4i32_indexed_OP1:
4622   case MachineCombinerPattern::MULADDv4i32_indexed_OP2:
4623   case MachineCombinerPattern::MULSUBv4i16_indexed_OP1:
4624   case MachineCombinerPattern::MULSUBv4i16_indexed_OP2:
4625   case MachineCombinerPattern::MULSUBv8i16_indexed_OP1:
4626   case MachineCombinerPattern::MULSUBv8i16_indexed_OP2:
4627   case MachineCombinerPattern::MULSUBv2i32_indexed_OP1:
4628   case MachineCombinerPattern::MULSUBv2i32_indexed_OP2:
4629   case MachineCombinerPattern::MULSUBv4i32_indexed_OP1:
4630   case MachineCombinerPattern::MULSUBv4i32_indexed_OP2:
4631     return true;
4632   } // end switch (Pattern)
4633   return false;
4634 }
4635 /// Return true when there is potentially a faster code sequence for an
4636 /// instruction chain ending in \p Root. All potential patterns are listed in
4637 /// the \p Pattern vector. Pattern should be sorted in priority order since the
4638 /// pattern evaluator stops checking as soon as it finds a faster sequence.
4639 
4640 bool AArch64InstrInfo::getMachineCombinerPatterns(
4641     MachineInstr &Root, SmallVectorImpl<MachineCombinerPattern> &Patterns,
4642     bool DoRegPressureReduce) const {
4643   // Integer patterns
4644   if (getMaddPatterns(Root, Patterns))
4645     return true;
4646   // Floating point patterns
4647   if (getFMAPatterns(Root, Patterns))
4648     return true;
4649 
4650   return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns,
4651                                                      DoRegPressureReduce);
4652 }
4653 
4654 enum class FMAInstKind { Default, Indexed, Accumulator };
4655 /// genFusedMultiply - Generate fused multiply instructions.
4656 /// This function supports both integer and floating point instructions.
4657 /// A typical example:
4658 ///  F|MUL I=A,B,0
4659 ///  F|ADD R,I,C
4660 ///  ==> F|MADD R,A,B,C
4661 /// \param MF Containing MachineFunction
4662 /// \param MRI Register information
4663 /// \param TII Target information
4664 /// \param Root is the F|ADD instruction
4665 /// \param [out] InsInstrs is a vector of machine instructions and will
4666 /// contain the generated madd instruction
4667 /// \param IdxMulOpd is index of operand in Root that is the result of
4668 /// the F|MUL. In the example above IdxMulOpd is 1.
4669 /// \param MaddOpc the opcode fo the f|madd instruction
4670 /// \param RC Register class of operands
4671 /// \param kind of fma instruction (addressing mode) to be generated
4672 /// \param ReplacedAddend is the result register from the instruction
4673 /// replacing the non-combined operand, if any.
4674 static MachineInstr *
4675 genFusedMultiply(MachineFunction &MF, MachineRegisterInfo &MRI,
4676                  const TargetInstrInfo *TII, MachineInstr &Root,
4677                  SmallVectorImpl<MachineInstr *> &InsInstrs, unsigned IdxMulOpd,
4678                  unsigned MaddOpc, const TargetRegisterClass *RC,
4679                  FMAInstKind kind = FMAInstKind::Default,
4680                  const Register *ReplacedAddend = nullptr) {
4681   assert(IdxMulOpd == 1 || IdxMulOpd == 2);
4682 
4683   unsigned IdxOtherOpd = IdxMulOpd == 1 ? 2 : 1;
4684   MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
4685   Register ResultReg = Root.getOperand(0).getReg();
4686   Register SrcReg0 = MUL->getOperand(1).getReg();
4687   bool Src0IsKill = MUL->getOperand(1).isKill();
4688   Register SrcReg1 = MUL->getOperand(2).getReg();
4689   bool Src1IsKill = MUL->getOperand(2).isKill();
4690 
4691   unsigned SrcReg2;
4692   bool Src2IsKill;
4693   if (ReplacedAddend) {
4694     // If we just generated a new addend, we must be it's only use.
4695     SrcReg2 = *ReplacedAddend;
4696     Src2IsKill = true;
4697   } else {
4698     SrcReg2 = Root.getOperand(IdxOtherOpd).getReg();
4699     Src2IsKill = Root.getOperand(IdxOtherOpd).isKill();
4700   }
4701 
4702   if (Register::isVirtualRegister(ResultReg))
4703     MRI.constrainRegClass(ResultReg, RC);
4704   if (Register::isVirtualRegister(SrcReg0))
4705     MRI.constrainRegClass(SrcReg0, RC);
4706   if (Register::isVirtualRegister(SrcReg1))
4707     MRI.constrainRegClass(SrcReg1, RC);
4708   if (Register::isVirtualRegister(SrcReg2))
4709     MRI.constrainRegClass(SrcReg2, RC);
4710 
4711   MachineInstrBuilder MIB;
4712   if (kind == FMAInstKind::Default)
4713     MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4714               .addReg(SrcReg0, getKillRegState(Src0IsKill))
4715               .addReg(SrcReg1, getKillRegState(Src1IsKill))
4716               .addReg(SrcReg2, getKillRegState(Src2IsKill));
4717   else if (kind == FMAInstKind::Indexed)
4718     MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4719               .addReg(SrcReg2, getKillRegState(Src2IsKill))
4720               .addReg(SrcReg0, getKillRegState(Src0IsKill))
4721               .addReg(SrcReg1, getKillRegState(Src1IsKill))
4722               .addImm(MUL->getOperand(3).getImm());
4723   else if (kind == FMAInstKind::Accumulator)
4724     MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4725               .addReg(SrcReg2, getKillRegState(Src2IsKill))
4726               .addReg(SrcReg0, getKillRegState(Src0IsKill))
4727               .addReg(SrcReg1, getKillRegState(Src1IsKill));
4728   else
4729     assert(false && "Invalid FMA instruction kind \n");
4730   // Insert the MADD (MADD, FMA, FMS, FMLA, FMSL)
4731   InsInstrs.push_back(MIB);
4732   return MUL;
4733 }
4734 
4735 /// genFusedMultiplyAcc - Helper to generate fused multiply accumulate
4736 /// instructions.
4737 ///
4738 /// \see genFusedMultiply
4739 static MachineInstr *genFusedMultiplyAcc(
4740     MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
4741     MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
4742     unsigned IdxMulOpd, unsigned MaddOpc, const TargetRegisterClass *RC) {
4743   return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
4744                           FMAInstKind::Accumulator);
4745 }
4746 
4747 /// genNeg - Helper to generate an intermediate negation of the second operand
4748 /// of Root
4749 static Register genNeg(MachineFunction &MF, MachineRegisterInfo &MRI,
4750                        const TargetInstrInfo *TII, MachineInstr &Root,
4751                        SmallVectorImpl<MachineInstr *> &InsInstrs,
4752                        DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
4753                        unsigned MnegOpc, const TargetRegisterClass *RC) {
4754   Register NewVR = MRI.createVirtualRegister(RC);
4755   MachineInstrBuilder MIB =
4756       BuildMI(MF, Root.getDebugLoc(), TII->get(MnegOpc), NewVR)
4757           .add(Root.getOperand(2));
4758   InsInstrs.push_back(MIB);
4759 
4760   assert(InstrIdxForVirtReg.empty());
4761   InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4762 
4763   return NewVR;
4764 }
4765 
4766 /// genFusedMultiplyAccNeg - Helper to generate fused multiply accumulate
4767 /// instructions with an additional negation of the accumulator
4768 static MachineInstr *genFusedMultiplyAccNeg(
4769     MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
4770     MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
4771     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg, unsigned IdxMulOpd,
4772     unsigned MaddOpc, unsigned MnegOpc, const TargetRegisterClass *RC) {
4773   assert(IdxMulOpd == 1);
4774 
4775   Register NewVR =
4776       genNeg(MF, MRI, TII, Root, InsInstrs, InstrIdxForVirtReg, MnegOpc, RC);
4777   return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
4778                           FMAInstKind::Accumulator, &NewVR);
4779 }
4780 
4781 /// genFusedMultiplyIdx - Helper to generate fused multiply accumulate
4782 /// instructions.
4783 ///
4784 /// \see genFusedMultiply
4785 static MachineInstr *genFusedMultiplyIdx(
4786     MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
4787     MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
4788     unsigned IdxMulOpd, unsigned MaddOpc, const TargetRegisterClass *RC) {
4789   return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
4790                           FMAInstKind::Indexed);
4791 }
4792 
4793 /// genFusedMultiplyAccNeg - Helper to generate fused multiply accumulate
4794 /// instructions with an additional negation of the accumulator
4795 static MachineInstr *genFusedMultiplyIdxNeg(
4796     MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
4797     MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
4798     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg, unsigned IdxMulOpd,
4799     unsigned MaddOpc, unsigned MnegOpc, const TargetRegisterClass *RC) {
4800   assert(IdxMulOpd == 1);
4801 
4802   Register NewVR =
4803       genNeg(MF, MRI, TII, Root, InsInstrs, InstrIdxForVirtReg, MnegOpc, RC);
4804 
4805   return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
4806                           FMAInstKind::Indexed, &NewVR);
4807 }
4808 
4809 /// genMaddR - Generate madd instruction and combine mul and add using
4810 /// an extra virtual register
4811 /// Example - an ADD intermediate needs to be stored in a register:
4812 ///   MUL I=A,B,0
4813 ///   ADD R,I,Imm
4814 ///   ==> ORR  V, ZR, Imm
4815 ///   ==> MADD R,A,B,V
4816 /// \param MF Containing MachineFunction
4817 /// \param MRI Register information
4818 /// \param TII Target information
4819 /// \param Root is the ADD instruction
4820 /// \param [out] InsInstrs is a vector of machine instructions and will
4821 /// contain the generated madd instruction
4822 /// \param IdxMulOpd is index of operand in Root that is the result of
4823 /// the MUL. In the example above IdxMulOpd is 1.
4824 /// \param MaddOpc the opcode fo the madd instruction
4825 /// \param VR is a virtual register that holds the value of an ADD operand
4826 /// (V in the example above).
4827 /// \param RC Register class of operands
4828 static MachineInstr *genMaddR(MachineFunction &MF, MachineRegisterInfo &MRI,
4829                               const TargetInstrInfo *TII, MachineInstr &Root,
4830                               SmallVectorImpl<MachineInstr *> &InsInstrs,
4831                               unsigned IdxMulOpd, unsigned MaddOpc, unsigned VR,
4832                               const TargetRegisterClass *RC) {
4833   assert(IdxMulOpd == 1 || IdxMulOpd == 2);
4834 
4835   MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
4836   Register ResultReg = Root.getOperand(0).getReg();
4837   Register SrcReg0 = MUL->getOperand(1).getReg();
4838   bool Src0IsKill = MUL->getOperand(1).isKill();
4839   Register SrcReg1 = MUL->getOperand(2).getReg();
4840   bool Src1IsKill = MUL->getOperand(2).isKill();
4841 
4842   if (Register::isVirtualRegister(ResultReg))
4843     MRI.constrainRegClass(ResultReg, RC);
4844   if (Register::isVirtualRegister(SrcReg0))
4845     MRI.constrainRegClass(SrcReg0, RC);
4846   if (Register::isVirtualRegister(SrcReg1))
4847     MRI.constrainRegClass(SrcReg1, RC);
4848   if (Register::isVirtualRegister(VR))
4849     MRI.constrainRegClass(VR, RC);
4850 
4851   MachineInstrBuilder MIB =
4852       BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4853           .addReg(SrcReg0, getKillRegState(Src0IsKill))
4854           .addReg(SrcReg1, getKillRegState(Src1IsKill))
4855           .addReg(VR);
4856   // Insert the MADD
4857   InsInstrs.push_back(MIB);
4858   return MUL;
4859 }
4860 
4861 /// When getMachineCombinerPatterns() finds potential patterns,
4862 /// this function generates the instructions that could replace the
4863 /// original code sequence
4864 void AArch64InstrInfo::genAlternativeCodeSequence(
4865     MachineInstr &Root, MachineCombinerPattern Pattern,
4866     SmallVectorImpl<MachineInstr *> &InsInstrs,
4867     SmallVectorImpl<MachineInstr *> &DelInstrs,
4868     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
4869   MachineBasicBlock &MBB = *Root.getParent();
4870   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4871   MachineFunction &MF = *MBB.getParent();
4872   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
4873 
4874   MachineInstr *MUL = nullptr;
4875   const TargetRegisterClass *RC;
4876   unsigned Opc;
4877   switch (Pattern) {
4878   default:
4879     // Reassociate instructions.
4880     TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs,
4881                                                 DelInstrs, InstrIdxForVirtReg);
4882     return;
4883   case MachineCombinerPattern::MULADDW_OP1:
4884   case MachineCombinerPattern::MULADDX_OP1:
4885     // MUL I=A,B,0
4886     // ADD R,I,C
4887     // ==> MADD R,A,B,C
4888     // --- Create(MADD);
4889     if (Pattern == MachineCombinerPattern::MULADDW_OP1) {
4890       Opc = AArch64::MADDWrrr;
4891       RC = &AArch64::GPR32RegClass;
4892     } else {
4893       Opc = AArch64::MADDXrrr;
4894       RC = &AArch64::GPR64RegClass;
4895     }
4896     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4897     break;
4898   case MachineCombinerPattern::MULADDW_OP2:
4899   case MachineCombinerPattern::MULADDX_OP2:
4900     // MUL I=A,B,0
4901     // ADD R,C,I
4902     // ==> MADD R,A,B,C
4903     // --- Create(MADD);
4904     if (Pattern == MachineCombinerPattern::MULADDW_OP2) {
4905       Opc = AArch64::MADDWrrr;
4906       RC = &AArch64::GPR32RegClass;
4907     } else {
4908       Opc = AArch64::MADDXrrr;
4909       RC = &AArch64::GPR64RegClass;
4910     }
4911     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4912     break;
4913   case MachineCombinerPattern::MULADDWI_OP1:
4914   case MachineCombinerPattern::MULADDXI_OP1: {
4915     // MUL I=A,B,0
4916     // ADD R,I,Imm
4917     // ==> ORR  V, ZR, Imm
4918     // ==> MADD R,A,B,V
4919     // --- Create(MADD);
4920     const TargetRegisterClass *OrrRC;
4921     unsigned BitSize, OrrOpc, ZeroReg;
4922     if (Pattern == MachineCombinerPattern::MULADDWI_OP1) {
4923       OrrOpc = AArch64::ORRWri;
4924       OrrRC = &AArch64::GPR32spRegClass;
4925       BitSize = 32;
4926       ZeroReg = AArch64::WZR;
4927       Opc = AArch64::MADDWrrr;
4928       RC = &AArch64::GPR32RegClass;
4929     } else {
4930       OrrOpc = AArch64::ORRXri;
4931       OrrRC = &AArch64::GPR64spRegClass;
4932       BitSize = 64;
4933       ZeroReg = AArch64::XZR;
4934       Opc = AArch64::MADDXrrr;
4935       RC = &AArch64::GPR64RegClass;
4936     }
4937     Register NewVR = MRI.createVirtualRegister(OrrRC);
4938     uint64_t Imm = Root.getOperand(2).getImm();
4939 
4940     if (Root.getOperand(3).isImm()) {
4941       unsigned Val = Root.getOperand(3).getImm();
4942       Imm = Imm << Val;
4943     }
4944     uint64_t UImm = SignExtend64(Imm, BitSize);
4945     uint64_t Encoding;
4946     if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
4947       MachineInstrBuilder MIB1 =
4948           BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
4949               .addReg(ZeroReg)
4950               .addImm(Encoding);
4951       InsInstrs.push_back(MIB1);
4952       InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4953       MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4954     }
4955     break;
4956   }
4957   case MachineCombinerPattern::MULSUBW_OP1:
4958   case MachineCombinerPattern::MULSUBX_OP1: {
4959     // MUL I=A,B,0
4960     // SUB R,I, C
4961     // ==> SUB  V, 0, C
4962     // ==> MADD R,A,B,V // = -C + A*B
4963     // --- Create(MADD);
4964     const TargetRegisterClass *SubRC;
4965     unsigned SubOpc, ZeroReg;
4966     if (Pattern == MachineCombinerPattern::MULSUBW_OP1) {
4967       SubOpc = AArch64::SUBWrr;
4968       SubRC = &AArch64::GPR32spRegClass;
4969       ZeroReg = AArch64::WZR;
4970       Opc = AArch64::MADDWrrr;
4971       RC = &AArch64::GPR32RegClass;
4972     } else {
4973       SubOpc = AArch64::SUBXrr;
4974       SubRC = &AArch64::GPR64spRegClass;
4975       ZeroReg = AArch64::XZR;
4976       Opc = AArch64::MADDXrrr;
4977       RC = &AArch64::GPR64RegClass;
4978     }
4979     Register NewVR = MRI.createVirtualRegister(SubRC);
4980     // SUB NewVR, 0, C
4981     MachineInstrBuilder MIB1 =
4982         BuildMI(MF, Root.getDebugLoc(), TII->get(SubOpc), NewVR)
4983             .addReg(ZeroReg)
4984             .add(Root.getOperand(2));
4985     InsInstrs.push_back(MIB1);
4986     InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4987     MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4988     break;
4989   }
4990   case MachineCombinerPattern::MULSUBW_OP2:
4991   case MachineCombinerPattern::MULSUBX_OP2:
4992     // MUL I=A,B,0
4993     // SUB R,C,I
4994     // ==> MSUB R,A,B,C (computes C - A*B)
4995     // --- Create(MSUB);
4996     if (Pattern == MachineCombinerPattern::MULSUBW_OP2) {
4997       Opc = AArch64::MSUBWrrr;
4998       RC = &AArch64::GPR32RegClass;
4999     } else {
5000       Opc = AArch64::MSUBXrrr;
5001       RC = &AArch64::GPR64RegClass;
5002     }
5003     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5004     break;
5005   case MachineCombinerPattern::MULSUBWI_OP1:
5006   case MachineCombinerPattern::MULSUBXI_OP1: {
5007     // MUL I=A,B,0
5008     // SUB R,I, Imm
5009     // ==> ORR  V, ZR, -Imm
5010     // ==> MADD R,A,B,V // = -Imm + A*B
5011     // --- Create(MADD);
5012     const TargetRegisterClass *OrrRC;
5013     unsigned BitSize, OrrOpc, ZeroReg;
5014     if (Pattern == MachineCombinerPattern::MULSUBWI_OP1) {
5015       OrrOpc = AArch64::ORRWri;
5016       OrrRC = &AArch64::GPR32spRegClass;
5017       BitSize = 32;
5018       ZeroReg = AArch64::WZR;
5019       Opc = AArch64::MADDWrrr;
5020       RC = &AArch64::GPR32RegClass;
5021     } else {
5022       OrrOpc = AArch64::ORRXri;
5023       OrrRC = &AArch64::GPR64spRegClass;
5024       BitSize = 64;
5025       ZeroReg = AArch64::XZR;
5026       Opc = AArch64::MADDXrrr;
5027       RC = &AArch64::GPR64RegClass;
5028     }
5029     Register NewVR = MRI.createVirtualRegister(OrrRC);
5030     uint64_t Imm = Root.getOperand(2).getImm();
5031     if (Root.getOperand(3).isImm()) {
5032       unsigned Val = Root.getOperand(3).getImm();
5033       Imm = Imm << Val;
5034     }
5035     uint64_t UImm = SignExtend64(-Imm, BitSize);
5036     uint64_t Encoding;
5037     if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
5038       MachineInstrBuilder MIB1 =
5039           BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
5040               .addReg(ZeroReg)
5041               .addImm(Encoding);
5042       InsInstrs.push_back(MIB1);
5043       InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
5044       MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
5045     }
5046     break;
5047   }
5048 
5049   case MachineCombinerPattern::MULADDv8i8_OP1:
5050     Opc = AArch64::MLAv8i8;
5051     RC = &AArch64::FPR64RegClass;
5052     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5053     break;
5054   case MachineCombinerPattern::MULADDv8i8_OP2:
5055     Opc = AArch64::MLAv8i8;
5056     RC = &AArch64::FPR64RegClass;
5057     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5058     break;
5059   case MachineCombinerPattern::MULADDv16i8_OP1:
5060     Opc = AArch64::MLAv16i8;
5061     RC = &AArch64::FPR128RegClass;
5062     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5063     break;
5064   case MachineCombinerPattern::MULADDv16i8_OP2:
5065     Opc = AArch64::MLAv16i8;
5066     RC = &AArch64::FPR128RegClass;
5067     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5068     break;
5069   case MachineCombinerPattern::MULADDv4i16_OP1:
5070     Opc = AArch64::MLAv4i16;
5071     RC = &AArch64::FPR64RegClass;
5072     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5073     break;
5074   case MachineCombinerPattern::MULADDv4i16_OP2:
5075     Opc = AArch64::MLAv4i16;
5076     RC = &AArch64::FPR64RegClass;
5077     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5078     break;
5079   case MachineCombinerPattern::MULADDv8i16_OP1:
5080     Opc = AArch64::MLAv8i16;
5081     RC = &AArch64::FPR128RegClass;
5082     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5083     break;
5084   case MachineCombinerPattern::MULADDv8i16_OP2:
5085     Opc = AArch64::MLAv8i16;
5086     RC = &AArch64::FPR128RegClass;
5087     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5088     break;
5089   case MachineCombinerPattern::MULADDv2i32_OP1:
5090     Opc = AArch64::MLAv2i32;
5091     RC = &AArch64::FPR64RegClass;
5092     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5093     break;
5094   case MachineCombinerPattern::MULADDv2i32_OP2:
5095     Opc = AArch64::MLAv2i32;
5096     RC = &AArch64::FPR64RegClass;
5097     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5098     break;
5099   case MachineCombinerPattern::MULADDv4i32_OP1:
5100     Opc = AArch64::MLAv4i32;
5101     RC = &AArch64::FPR128RegClass;
5102     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5103     break;
5104   case MachineCombinerPattern::MULADDv4i32_OP2:
5105     Opc = AArch64::MLAv4i32;
5106     RC = &AArch64::FPR128RegClass;
5107     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5108     break;
5109 
5110   case MachineCombinerPattern::MULSUBv8i8_OP1:
5111     Opc = AArch64::MLAv8i8;
5112     RC = &AArch64::FPR64RegClass;
5113     MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
5114                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv8i8,
5115                                  RC);
5116     break;
5117   case MachineCombinerPattern::MULSUBv8i8_OP2:
5118     Opc = AArch64::MLSv8i8;
5119     RC = &AArch64::FPR64RegClass;
5120     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5121     break;
5122   case MachineCombinerPattern::MULSUBv16i8_OP1:
5123     Opc = AArch64::MLAv16i8;
5124     RC = &AArch64::FPR128RegClass;
5125     MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
5126                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv16i8,
5127                                  RC);
5128     break;
5129   case MachineCombinerPattern::MULSUBv16i8_OP2:
5130     Opc = AArch64::MLSv16i8;
5131     RC = &AArch64::FPR128RegClass;
5132     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5133     break;
5134   case MachineCombinerPattern::MULSUBv4i16_OP1:
5135     Opc = AArch64::MLAv4i16;
5136     RC = &AArch64::FPR64RegClass;
5137     MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
5138                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i16,
5139                                  RC);
5140     break;
5141   case MachineCombinerPattern::MULSUBv4i16_OP2:
5142     Opc = AArch64::MLSv4i16;
5143     RC = &AArch64::FPR64RegClass;
5144     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5145     break;
5146   case MachineCombinerPattern::MULSUBv8i16_OP1:
5147     Opc = AArch64::MLAv8i16;
5148     RC = &AArch64::FPR128RegClass;
5149     MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
5150                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv8i16,
5151                                  RC);
5152     break;
5153   case MachineCombinerPattern::MULSUBv8i16_OP2:
5154     Opc = AArch64::MLSv8i16;
5155     RC = &AArch64::FPR128RegClass;
5156     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5157     break;
5158   case MachineCombinerPattern::MULSUBv2i32_OP1:
5159     Opc = AArch64::MLAv2i32;
5160     RC = &AArch64::FPR64RegClass;
5161     MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
5162                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv2i32,
5163                                  RC);
5164     break;
5165   case MachineCombinerPattern::MULSUBv2i32_OP2:
5166     Opc = AArch64::MLSv2i32;
5167     RC = &AArch64::FPR64RegClass;
5168     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5169     break;
5170   case MachineCombinerPattern::MULSUBv4i32_OP1:
5171     Opc = AArch64::MLAv4i32;
5172     RC = &AArch64::FPR128RegClass;
5173     MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
5174                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i32,
5175                                  RC);
5176     break;
5177   case MachineCombinerPattern::MULSUBv4i32_OP2:
5178     Opc = AArch64::MLSv4i32;
5179     RC = &AArch64::FPR128RegClass;
5180     MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5181     break;
5182 
5183   case MachineCombinerPattern::MULADDv4i16_indexed_OP1:
5184     Opc = AArch64::MLAv4i16_indexed;
5185     RC = &AArch64::FPR64RegClass;
5186     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5187     break;
5188   case MachineCombinerPattern::MULADDv4i16_indexed_OP2:
5189     Opc = AArch64::MLAv4i16_indexed;
5190     RC = &AArch64::FPR64RegClass;
5191     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5192     break;
5193   case MachineCombinerPattern::MULADDv8i16_indexed_OP1:
5194     Opc = AArch64::MLAv8i16_indexed;
5195     RC = &AArch64::FPR128RegClass;
5196     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5197     break;
5198   case MachineCombinerPattern::MULADDv8i16_indexed_OP2:
5199     Opc = AArch64::MLAv8i16_indexed;
5200     RC = &AArch64::FPR128RegClass;
5201     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5202     break;
5203   case MachineCombinerPattern::MULADDv2i32_indexed_OP1:
5204     Opc = AArch64::MLAv2i32_indexed;
5205     RC = &AArch64::FPR64RegClass;
5206     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5207     break;
5208   case MachineCombinerPattern::MULADDv2i32_indexed_OP2:
5209     Opc = AArch64::MLAv2i32_indexed;
5210     RC = &AArch64::FPR64RegClass;
5211     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5212     break;
5213   case MachineCombinerPattern::MULADDv4i32_indexed_OP1:
5214     Opc = AArch64::MLAv4i32_indexed;
5215     RC = &AArch64::FPR128RegClass;
5216     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5217     break;
5218   case MachineCombinerPattern::MULADDv4i32_indexed_OP2:
5219     Opc = AArch64::MLAv4i32_indexed;
5220     RC = &AArch64::FPR128RegClass;
5221     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5222     break;
5223 
5224   case MachineCombinerPattern::MULSUBv4i16_indexed_OP1:
5225     Opc = AArch64::MLAv4i16_indexed;
5226     RC = &AArch64::FPR64RegClass;
5227     MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
5228                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i16,
5229                                  RC);
5230     break;
5231   case MachineCombinerPattern::MULSUBv4i16_indexed_OP2:
5232     Opc = AArch64::MLSv4i16_indexed;
5233     RC = &AArch64::FPR64RegClass;
5234     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5235     break;
5236   case MachineCombinerPattern::MULSUBv8i16_indexed_OP1:
5237     Opc = AArch64::MLAv8i16_indexed;
5238     RC = &AArch64::FPR128RegClass;
5239     MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
5240                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv8i16,
5241                                  RC);
5242     break;
5243   case MachineCombinerPattern::MULSUBv8i16_indexed_OP2:
5244     Opc = AArch64::MLSv8i16_indexed;
5245     RC = &AArch64::FPR128RegClass;
5246     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5247     break;
5248   case MachineCombinerPattern::MULSUBv2i32_indexed_OP1:
5249     Opc = AArch64::MLAv2i32_indexed;
5250     RC = &AArch64::FPR64RegClass;
5251     MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
5252                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv2i32,
5253                                  RC);
5254     break;
5255   case MachineCombinerPattern::MULSUBv2i32_indexed_OP2:
5256     Opc = AArch64::MLSv2i32_indexed;
5257     RC = &AArch64::FPR64RegClass;
5258     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5259     break;
5260   case MachineCombinerPattern::MULSUBv4i32_indexed_OP1:
5261     Opc = AArch64::MLAv4i32_indexed;
5262     RC = &AArch64::FPR128RegClass;
5263     MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
5264                                  InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i32,
5265                                  RC);
5266     break;
5267   case MachineCombinerPattern::MULSUBv4i32_indexed_OP2:
5268     Opc = AArch64::MLSv4i32_indexed;
5269     RC = &AArch64::FPR128RegClass;
5270     MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5271     break;
5272 
5273   // Floating Point Support
5274   case MachineCombinerPattern::FMULADDH_OP1:
5275     Opc = AArch64::FMADDHrrr;
5276     RC = &AArch64::FPR16RegClass;
5277     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5278     break;
5279   case MachineCombinerPattern::FMULADDS_OP1:
5280     Opc = AArch64::FMADDSrrr;
5281     RC = &AArch64::FPR32RegClass;
5282     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5283     break;
5284   case MachineCombinerPattern::FMULADDD_OP1:
5285     Opc = AArch64::FMADDDrrr;
5286     RC = &AArch64::FPR64RegClass;
5287     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5288     break;
5289 
5290   case MachineCombinerPattern::FMULADDH_OP2:
5291     Opc = AArch64::FMADDHrrr;
5292     RC = &AArch64::FPR16RegClass;
5293     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5294     break;
5295   case MachineCombinerPattern::FMULADDS_OP2:
5296     Opc = AArch64::FMADDSrrr;
5297     RC = &AArch64::FPR32RegClass;
5298     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5299     break;
5300   case MachineCombinerPattern::FMULADDD_OP2:
5301     Opc = AArch64::FMADDDrrr;
5302     RC = &AArch64::FPR64RegClass;
5303     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5304     break;
5305 
5306   case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
5307     Opc = AArch64::FMLAv1i32_indexed;
5308     RC = &AArch64::FPR32RegClass;
5309     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5310                            FMAInstKind::Indexed);
5311     break;
5312   case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
5313     Opc = AArch64::FMLAv1i32_indexed;
5314     RC = &AArch64::FPR32RegClass;
5315     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5316                            FMAInstKind::Indexed);
5317     break;
5318 
5319   case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
5320     Opc = AArch64::FMLAv1i64_indexed;
5321     RC = &AArch64::FPR64RegClass;
5322     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5323                            FMAInstKind::Indexed);
5324     break;
5325   case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
5326     Opc = AArch64::FMLAv1i64_indexed;
5327     RC = &AArch64::FPR64RegClass;
5328     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5329                            FMAInstKind::Indexed);
5330     break;
5331 
5332   case MachineCombinerPattern::FMLAv4i16_indexed_OP1:
5333     RC = &AArch64::FPR64RegClass;
5334     Opc = AArch64::FMLAv4i16_indexed;
5335     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5336                            FMAInstKind::Indexed);
5337     break;
5338   case MachineCombinerPattern::FMLAv4f16_OP1:
5339     RC = &AArch64::FPR64RegClass;
5340     Opc = AArch64::FMLAv4f16;
5341     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5342                            FMAInstKind::Accumulator);
5343     break;
5344   case MachineCombinerPattern::FMLAv4i16_indexed_OP2:
5345     RC = &AArch64::FPR64RegClass;
5346     Opc = AArch64::FMLAv4i16_indexed;
5347     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5348                            FMAInstKind::Indexed);
5349     break;
5350   case MachineCombinerPattern::FMLAv4f16_OP2:
5351     RC = &AArch64::FPR64RegClass;
5352     Opc = AArch64::FMLAv4f16;
5353     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5354                            FMAInstKind::Accumulator);
5355     break;
5356 
5357   case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
5358   case MachineCombinerPattern::FMLAv2f32_OP1:
5359     RC = &AArch64::FPR64RegClass;
5360     if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP1) {
5361       Opc = AArch64::FMLAv2i32_indexed;
5362       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5363                              FMAInstKind::Indexed);
5364     } else {
5365       Opc = AArch64::FMLAv2f32;
5366       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5367                              FMAInstKind::Accumulator);
5368     }
5369     break;
5370   case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
5371   case MachineCombinerPattern::FMLAv2f32_OP2:
5372     RC = &AArch64::FPR64RegClass;
5373     if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP2) {
5374       Opc = AArch64::FMLAv2i32_indexed;
5375       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5376                              FMAInstKind::Indexed);
5377     } else {
5378       Opc = AArch64::FMLAv2f32;
5379       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5380                              FMAInstKind::Accumulator);
5381     }
5382     break;
5383 
5384   case MachineCombinerPattern::FMLAv8i16_indexed_OP1:
5385     RC = &AArch64::FPR128RegClass;
5386     Opc = AArch64::FMLAv8i16_indexed;
5387     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5388                            FMAInstKind::Indexed);
5389     break;
5390   case MachineCombinerPattern::FMLAv8f16_OP1:
5391     RC = &AArch64::FPR128RegClass;
5392     Opc = AArch64::FMLAv8f16;
5393     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5394                            FMAInstKind::Accumulator);
5395     break;
5396   case MachineCombinerPattern::FMLAv8i16_indexed_OP2:
5397     RC = &AArch64::FPR128RegClass;
5398     Opc = AArch64::FMLAv8i16_indexed;
5399     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5400                            FMAInstKind::Indexed);
5401     break;
5402   case MachineCombinerPattern::FMLAv8f16_OP2:
5403     RC = &AArch64::FPR128RegClass;
5404     Opc = AArch64::FMLAv8f16;
5405     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5406                            FMAInstKind::Accumulator);
5407     break;
5408 
5409   case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
5410   case MachineCombinerPattern::FMLAv2f64_OP1:
5411     RC = &AArch64::FPR128RegClass;
5412     if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP1) {
5413       Opc = AArch64::FMLAv2i64_indexed;
5414       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5415                              FMAInstKind::Indexed);
5416     } else {
5417       Opc = AArch64::FMLAv2f64;
5418       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5419                              FMAInstKind::Accumulator);
5420     }
5421     break;
5422   case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
5423   case MachineCombinerPattern::FMLAv2f64_OP2:
5424     RC = &AArch64::FPR128RegClass;
5425     if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP2) {
5426       Opc = AArch64::FMLAv2i64_indexed;
5427       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5428                              FMAInstKind::Indexed);
5429     } else {
5430       Opc = AArch64::FMLAv2f64;
5431       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5432                              FMAInstKind::Accumulator);
5433     }
5434     break;
5435 
5436   case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
5437   case MachineCombinerPattern::FMLAv4f32_OP1:
5438     RC = &AArch64::FPR128RegClass;
5439     if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP1) {
5440       Opc = AArch64::FMLAv4i32_indexed;
5441       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5442                              FMAInstKind::Indexed);
5443     } else {
5444       Opc = AArch64::FMLAv4f32;
5445       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5446                              FMAInstKind::Accumulator);
5447     }
5448     break;
5449 
5450   case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
5451   case MachineCombinerPattern::FMLAv4f32_OP2:
5452     RC = &AArch64::FPR128RegClass;
5453     if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP2) {
5454       Opc = AArch64::FMLAv4i32_indexed;
5455       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5456                              FMAInstKind::Indexed);
5457     } else {
5458       Opc = AArch64::FMLAv4f32;
5459       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5460                              FMAInstKind::Accumulator);
5461     }
5462     break;
5463 
5464   case MachineCombinerPattern::FMULSUBH_OP1:
5465     Opc = AArch64::FNMSUBHrrr;
5466     RC = &AArch64::FPR16RegClass;
5467     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5468     break;
5469   case MachineCombinerPattern::FMULSUBS_OP1:
5470     Opc = AArch64::FNMSUBSrrr;
5471     RC = &AArch64::FPR32RegClass;
5472     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5473     break;
5474   case MachineCombinerPattern::FMULSUBD_OP1:
5475     Opc = AArch64::FNMSUBDrrr;
5476     RC = &AArch64::FPR64RegClass;
5477     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5478     break;
5479 
5480   case MachineCombinerPattern::FNMULSUBH_OP1:
5481     Opc = AArch64::FNMADDHrrr;
5482     RC = &AArch64::FPR16RegClass;
5483     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5484     break;
5485   case MachineCombinerPattern::FNMULSUBS_OP1:
5486     Opc = AArch64::FNMADDSrrr;
5487     RC = &AArch64::FPR32RegClass;
5488     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5489     break;
5490   case MachineCombinerPattern::FNMULSUBD_OP1:
5491     Opc = AArch64::FNMADDDrrr;
5492     RC = &AArch64::FPR64RegClass;
5493     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
5494     break;
5495 
5496   case MachineCombinerPattern::FMULSUBH_OP2:
5497     Opc = AArch64::FMSUBHrrr;
5498     RC = &AArch64::FPR16RegClass;
5499     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5500     break;
5501   case MachineCombinerPattern::FMULSUBS_OP2:
5502     Opc = AArch64::FMSUBSrrr;
5503     RC = &AArch64::FPR32RegClass;
5504     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5505     break;
5506   case MachineCombinerPattern::FMULSUBD_OP2:
5507     Opc = AArch64::FMSUBDrrr;
5508     RC = &AArch64::FPR64RegClass;
5509     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
5510     break;
5511 
5512   case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
5513     Opc = AArch64::FMLSv1i32_indexed;
5514     RC = &AArch64::FPR32RegClass;
5515     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5516                            FMAInstKind::Indexed);
5517     break;
5518 
5519   case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
5520     Opc = AArch64::FMLSv1i64_indexed;
5521     RC = &AArch64::FPR64RegClass;
5522     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5523                            FMAInstKind::Indexed);
5524     break;
5525 
5526   case MachineCombinerPattern::FMLSv4f16_OP1:
5527   case MachineCombinerPattern::FMLSv4i16_indexed_OP1: {
5528     RC = &AArch64::FPR64RegClass;
5529     Register NewVR = MRI.createVirtualRegister(RC);
5530     MachineInstrBuilder MIB1 =
5531         BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv4f16), NewVR)
5532             .add(Root.getOperand(2));
5533     InsInstrs.push_back(MIB1);
5534     InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
5535     if (Pattern == MachineCombinerPattern::FMLSv4f16_OP1) {
5536       Opc = AArch64::FMLAv4f16;
5537       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5538                              FMAInstKind::Accumulator, &NewVR);
5539     } else {
5540       Opc = AArch64::FMLAv4i16_indexed;
5541       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5542                              FMAInstKind::Indexed, &NewVR);
5543     }
5544     break;
5545   }
5546   case MachineCombinerPattern::FMLSv4f16_OP2:
5547     RC = &AArch64::FPR64RegClass;
5548     Opc = AArch64::FMLSv4f16;
5549     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5550                            FMAInstKind::Accumulator);
5551     break;
5552   case MachineCombinerPattern::FMLSv4i16_indexed_OP2:
5553     RC = &AArch64::FPR64RegClass;
5554     Opc = AArch64::FMLSv4i16_indexed;
5555     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5556                            FMAInstKind::Indexed);
5557     break;
5558 
5559   case MachineCombinerPattern::FMLSv2f32_OP2:
5560   case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
5561     RC = &AArch64::FPR64RegClass;
5562     if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP2) {
5563       Opc = AArch64::FMLSv2i32_indexed;
5564       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5565                              FMAInstKind::Indexed);
5566     } else {
5567       Opc = AArch64::FMLSv2f32;
5568       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5569                              FMAInstKind::Accumulator);
5570     }
5571     break;
5572 
5573   case MachineCombinerPattern::FMLSv8f16_OP1:
5574   case MachineCombinerPattern::FMLSv8i16_indexed_OP1: {
5575     RC = &AArch64::FPR128RegClass;
5576     Register NewVR = MRI.createVirtualRegister(RC);
5577     MachineInstrBuilder MIB1 =
5578         BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv8f16), NewVR)
5579             .add(Root.getOperand(2));
5580     InsInstrs.push_back(MIB1);
5581     InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
5582     if (Pattern == MachineCombinerPattern::FMLSv8f16_OP1) {
5583       Opc = AArch64::FMLAv8f16;
5584       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5585                              FMAInstKind::Accumulator, &NewVR);
5586     } else {
5587       Opc = AArch64::FMLAv8i16_indexed;
5588       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5589                              FMAInstKind::Indexed, &NewVR);
5590     }
5591     break;
5592   }
5593   case MachineCombinerPattern::FMLSv8f16_OP2:
5594     RC = &AArch64::FPR128RegClass;
5595     Opc = AArch64::FMLSv8f16;
5596     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5597                            FMAInstKind::Accumulator);
5598     break;
5599   case MachineCombinerPattern::FMLSv8i16_indexed_OP2:
5600     RC = &AArch64::FPR128RegClass;
5601     Opc = AArch64::FMLSv8i16_indexed;
5602     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5603                            FMAInstKind::Indexed);
5604     break;
5605 
5606   case MachineCombinerPattern::FMLSv2f64_OP2:
5607   case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
5608     RC = &AArch64::FPR128RegClass;
5609     if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP2) {
5610       Opc = AArch64::FMLSv2i64_indexed;
5611       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5612                              FMAInstKind::Indexed);
5613     } else {
5614       Opc = AArch64::FMLSv2f64;
5615       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5616                              FMAInstKind::Accumulator);
5617     }
5618     break;
5619 
5620   case MachineCombinerPattern::FMLSv4f32_OP2:
5621   case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
5622     RC = &AArch64::FPR128RegClass;
5623     if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP2) {
5624       Opc = AArch64::FMLSv4i32_indexed;
5625       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5626                              FMAInstKind::Indexed);
5627     } else {
5628       Opc = AArch64::FMLSv4f32;
5629       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
5630                              FMAInstKind::Accumulator);
5631     }
5632     break;
5633   case MachineCombinerPattern::FMLSv2f32_OP1:
5634   case MachineCombinerPattern::FMLSv2i32_indexed_OP1: {
5635     RC = &AArch64::FPR64RegClass;
5636     Register NewVR = MRI.createVirtualRegister(RC);
5637     MachineInstrBuilder MIB1 =
5638         BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f32), NewVR)
5639             .add(Root.getOperand(2));
5640     InsInstrs.push_back(MIB1);
5641     InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
5642     if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP1) {
5643       Opc = AArch64::FMLAv2i32_indexed;
5644       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5645                              FMAInstKind::Indexed, &NewVR);
5646     } else {
5647       Opc = AArch64::FMLAv2f32;
5648       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5649                              FMAInstKind::Accumulator, &NewVR);
5650     }
5651     break;
5652   }
5653   case MachineCombinerPattern::FMLSv4f32_OP1:
5654   case MachineCombinerPattern::FMLSv4i32_indexed_OP1: {
5655     RC = &AArch64::FPR128RegClass;
5656     Register NewVR = MRI.createVirtualRegister(RC);
5657     MachineInstrBuilder MIB1 =
5658         BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv4f32), NewVR)
5659             .add(Root.getOperand(2));
5660     InsInstrs.push_back(MIB1);
5661     InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
5662     if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP1) {
5663       Opc = AArch64::FMLAv4i32_indexed;
5664       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5665                              FMAInstKind::Indexed, &NewVR);
5666     } else {
5667       Opc = AArch64::FMLAv4f32;
5668       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5669                              FMAInstKind::Accumulator, &NewVR);
5670     }
5671     break;
5672   }
5673   case MachineCombinerPattern::FMLSv2f64_OP1:
5674   case MachineCombinerPattern::FMLSv2i64_indexed_OP1: {
5675     RC = &AArch64::FPR128RegClass;
5676     Register NewVR = MRI.createVirtualRegister(RC);
5677     MachineInstrBuilder MIB1 =
5678         BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f64), NewVR)
5679             .add(Root.getOperand(2));
5680     InsInstrs.push_back(MIB1);
5681     InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
5682     if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP1) {
5683       Opc = AArch64::FMLAv2i64_indexed;
5684       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5685                              FMAInstKind::Indexed, &NewVR);
5686     } else {
5687       Opc = AArch64::FMLAv2f64;
5688       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
5689                              FMAInstKind::Accumulator, &NewVR);
5690     }
5691     break;
5692   }
5693   } // end switch (Pattern)
5694   // Record MUL and ADD/SUB for deletion
5695   // FIXME: This assertion fails in CodeGen/AArch64/tailmerging_in_mbp.ll and
5696   // CodeGen/AArch64/urem-seteq-nonzero.ll.
5697   // assert(MUL && "MUL was never set");
5698   DelInstrs.push_back(MUL);
5699   DelInstrs.push_back(&Root);
5700 }
5701 
5702 /// Replace csincr-branch sequence by simple conditional branch
5703 ///
5704 /// Examples:
5705 /// 1. \code
5706 ///   csinc  w9, wzr, wzr, <condition code>
5707 ///   tbnz   w9, #0, 0x44
5708 ///    \endcode
5709 /// to
5710 ///    \code
5711 ///   b.<inverted condition code>
5712 ///    \endcode
5713 ///
5714 /// 2. \code
5715 ///   csinc w9, wzr, wzr, <condition code>
5716 ///   tbz   w9, #0, 0x44
5717 ///    \endcode
5718 /// to
5719 ///    \code
5720 ///   b.<condition code>
5721 ///    \endcode
5722 ///
5723 /// Replace compare and branch sequence by TBZ/TBNZ instruction when the
5724 /// compare's constant operand is power of 2.
5725 ///
5726 /// Examples:
5727 ///    \code
5728 ///   and  w8, w8, #0x400
5729 ///   cbnz w8, L1
5730 ///    \endcode
5731 /// to
5732 ///    \code
5733 ///   tbnz w8, #10, L1
5734 ///    \endcode
5735 ///
5736 /// \param  MI Conditional Branch
5737 /// \return True when the simple conditional branch is generated
5738 ///
5739 bool AArch64InstrInfo::optimizeCondBranch(MachineInstr &MI) const {
5740   bool IsNegativeBranch = false;
5741   bool IsTestAndBranch = false;
5742   unsigned TargetBBInMI = 0;
5743   switch (MI.getOpcode()) {
5744   default:
5745     llvm_unreachable("Unknown branch instruction?");
5746   case AArch64::Bcc:
5747     return false;
5748   case AArch64::CBZW:
5749   case AArch64::CBZX:
5750     TargetBBInMI = 1;
5751     break;
5752   case AArch64::CBNZW:
5753   case AArch64::CBNZX:
5754     TargetBBInMI = 1;
5755     IsNegativeBranch = true;
5756     break;
5757   case AArch64::TBZW:
5758   case AArch64::TBZX:
5759     TargetBBInMI = 2;
5760     IsTestAndBranch = true;
5761     break;
5762   case AArch64::TBNZW:
5763   case AArch64::TBNZX:
5764     TargetBBInMI = 2;
5765     IsNegativeBranch = true;
5766     IsTestAndBranch = true;
5767     break;
5768   }
5769   // So we increment a zero register and test for bits other
5770   // than bit 0? Conservatively bail out in case the verifier
5771   // missed this case.
5772   if (IsTestAndBranch && MI.getOperand(1).getImm())
5773     return false;
5774 
5775   // Find Definition.
5776   assert(MI.getParent() && "Incomplete machine instruciton\n");
5777   MachineBasicBlock *MBB = MI.getParent();
5778   MachineFunction *MF = MBB->getParent();
5779   MachineRegisterInfo *MRI = &MF->getRegInfo();
5780   Register VReg = MI.getOperand(0).getReg();
5781   if (!Register::isVirtualRegister(VReg))
5782     return false;
5783 
5784   MachineInstr *DefMI = MRI->getVRegDef(VReg);
5785 
5786   // Look through COPY instructions to find definition.
5787   while (DefMI->isCopy()) {
5788     Register CopyVReg = DefMI->getOperand(1).getReg();
5789     if (!MRI->hasOneNonDBGUse(CopyVReg))
5790       return false;
5791     if (!MRI->hasOneDef(CopyVReg))
5792       return false;
5793     DefMI = MRI->getVRegDef(CopyVReg);
5794   }
5795 
5796   switch (DefMI->getOpcode()) {
5797   default:
5798     return false;
5799   // Fold AND into a TBZ/TBNZ if constant operand is power of 2.
5800   case AArch64::ANDWri:
5801   case AArch64::ANDXri: {
5802     if (IsTestAndBranch)
5803       return false;
5804     if (DefMI->getParent() != MBB)
5805       return false;
5806     if (!MRI->hasOneNonDBGUse(VReg))
5807       return false;
5808 
5809     bool Is32Bit = (DefMI->getOpcode() == AArch64::ANDWri);
5810     uint64_t Mask = AArch64_AM::decodeLogicalImmediate(
5811         DefMI->getOperand(2).getImm(), Is32Bit ? 32 : 64);
5812     if (!isPowerOf2_64(Mask))
5813       return false;
5814 
5815     MachineOperand &MO = DefMI->getOperand(1);
5816     Register NewReg = MO.getReg();
5817     if (!Register::isVirtualRegister(NewReg))
5818       return false;
5819 
5820     assert(!MRI->def_empty(NewReg) && "Register must be defined.");
5821 
5822     MachineBasicBlock &RefToMBB = *MBB;
5823     MachineBasicBlock *TBB = MI.getOperand(1).getMBB();
5824     DebugLoc DL = MI.getDebugLoc();
5825     unsigned Imm = Log2_64(Mask);
5826     unsigned Opc = (Imm < 32)
5827                        ? (IsNegativeBranch ? AArch64::TBNZW : AArch64::TBZW)
5828                        : (IsNegativeBranch ? AArch64::TBNZX : AArch64::TBZX);
5829     MachineInstr *NewMI = BuildMI(RefToMBB, MI, DL, get(Opc))
5830                               .addReg(NewReg)
5831                               .addImm(Imm)
5832                               .addMBB(TBB);
5833     // Register lives on to the CBZ now.
5834     MO.setIsKill(false);
5835 
5836     // For immediate smaller than 32, we need to use the 32-bit
5837     // variant (W) in all cases. Indeed the 64-bit variant does not
5838     // allow to encode them.
5839     // Therefore, if the input register is 64-bit, we need to take the
5840     // 32-bit sub-part.
5841     if (!Is32Bit && Imm < 32)
5842       NewMI->getOperand(0).setSubReg(AArch64::sub_32);
5843     MI.eraseFromParent();
5844     return true;
5845   }
5846   // Look for CSINC
5847   case AArch64::CSINCWr:
5848   case AArch64::CSINCXr: {
5849     if (!(DefMI->getOperand(1).getReg() == AArch64::WZR &&
5850           DefMI->getOperand(2).getReg() == AArch64::WZR) &&
5851         !(DefMI->getOperand(1).getReg() == AArch64::XZR &&
5852           DefMI->getOperand(2).getReg() == AArch64::XZR))
5853       return false;
5854 
5855     if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) != -1)
5856       return false;
5857 
5858     AArch64CC::CondCode CC = (AArch64CC::CondCode)DefMI->getOperand(3).getImm();
5859     // Convert only when the condition code is not modified between
5860     // the CSINC and the branch. The CC may be used by other
5861     // instructions in between.
5862     if (areCFlagsAccessedBetweenInstrs(DefMI, MI, &getRegisterInfo(), AK_Write))
5863       return false;
5864     MachineBasicBlock &RefToMBB = *MBB;
5865     MachineBasicBlock *TBB = MI.getOperand(TargetBBInMI).getMBB();
5866     DebugLoc DL = MI.getDebugLoc();
5867     if (IsNegativeBranch)
5868       CC = AArch64CC::getInvertedCondCode(CC);
5869     BuildMI(RefToMBB, MI, DL, get(AArch64::Bcc)).addImm(CC).addMBB(TBB);
5870     MI.eraseFromParent();
5871     return true;
5872   }
5873   }
5874 }
5875 
5876 std::pair<unsigned, unsigned>
5877 AArch64InstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
5878   const unsigned Mask = AArch64II::MO_FRAGMENT;
5879   return std::make_pair(TF & Mask, TF & ~Mask);
5880 }
5881 
5882 ArrayRef<std::pair<unsigned, const char *>>
5883 AArch64InstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
5884   using namespace AArch64II;
5885 
5886   static const std::pair<unsigned, const char *> TargetFlags[] = {
5887       {MO_PAGE, "aarch64-page"}, {MO_PAGEOFF, "aarch64-pageoff"},
5888       {MO_G3, "aarch64-g3"},     {MO_G2, "aarch64-g2"},
5889       {MO_G1, "aarch64-g1"},     {MO_G0, "aarch64-g0"},
5890       {MO_HI12, "aarch64-hi12"}};
5891   return makeArrayRef(TargetFlags);
5892 }
5893 
5894 ArrayRef<std::pair<unsigned, const char *>>
5895 AArch64InstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
5896   using namespace AArch64II;
5897 
5898   static const std::pair<unsigned, const char *> TargetFlags[] = {
5899       {MO_COFFSTUB, "aarch64-coffstub"},
5900       {MO_GOT, "aarch64-got"},
5901       {MO_NC, "aarch64-nc"},
5902       {MO_S, "aarch64-s"},
5903       {MO_TLS, "aarch64-tls"},
5904       {MO_DLLIMPORT, "aarch64-dllimport"},
5905       {MO_PREL, "aarch64-prel"},
5906       {MO_TAGGED, "aarch64-tagged"}};
5907   return makeArrayRef(TargetFlags);
5908 }
5909 
5910 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
5911 AArch64InstrInfo::getSerializableMachineMemOperandTargetFlags() const {
5912   static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
5913       {{MOSuppressPair, "aarch64-suppress-pair"},
5914        {MOStridedAccess, "aarch64-strided-access"}};
5915   return makeArrayRef(TargetFlags);
5916 }
5917 
5918 /// Constants defining how certain sequences should be outlined.
5919 /// This encompasses how an outlined function should be called, and what kind of
5920 /// frame should be emitted for that outlined function.
5921 ///
5922 /// \p MachineOutlinerDefault implies that the function should be called with
5923 /// a save and restore of LR to the stack.
5924 ///
5925 /// That is,
5926 ///
5927 /// I1     Save LR                    OUTLINED_FUNCTION:
5928 /// I2 --> BL OUTLINED_FUNCTION       I1
5929 /// I3     Restore LR                 I2
5930 ///                                   I3
5931 ///                                   RET
5932 ///
5933 /// * Call construction overhead: 3 (save + BL + restore)
5934 /// * Frame construction overhead: 1 (ret)
5935 /// * Requires stack fixups? Yes
5936 ///
5937 /// \p MachineOutlinerTailCall implies that the function is being created from
5938 /// a sequence of instructions ending in a return.
5939 ///
5940 /// That is,
5941 ///
5942 /// I1                             OUTLINED_FUNCTION:
5943 /// I2 --> B OUTLINED_FUNCTION     I1
5944 /// RET                            I2
5945 ///                                RET
5946 ///
5947 /// * Call construction overhead: 1 (B)
5948 /// * Frame construction overhead: 0 (Return included in sequence)
5949 /// * Requires stack fixups? No
5950 ///
5951 /// \p MachineOutlinerNoLRSave implies that the function should be called using
5952 /// a BL instruction, but doesn't require LR to be saved and restored. This
5953 /// happens when LR is known to be dead.
5954 ///
5955 /// That is,
5956 ///
5957 /// I1                                OUTLINED_FUNCTION:
5958 /// I2 --> BL OUTLINED_FUNCTION       I1
5959 /// I3                                I2
5960 ///                                   I3
5961 ///                                   RET
5962 ///
5963 /// * Call construction overhead: 1 (BL)
5964 /// * Frame construction overhead: 1 (RET)
5965 /// * Requires stack fixups? No
5966 ///
5967 /// \p MachineOutlinerThunk implies that the function is being created from
5968 /// a sequence of instructions ending in a call. The outlined function is
5969 /// called with a BL instruction, and the outlined function tail-calls the
5970 /// original call destination.
5971 ///
5972 /// That is,
5973 ///
5974 /// I1                                OUTLINED_FUNCTION:
5975 /// I2 --> BL OUTLINED_FUNCTION       I1
5976 /// BL f                              I2
5977 ///                                   B f
5978 /// * Call construction overhead: 1 (BL)
5979 /// * Frame construction overhead: 0
5980 /// * Requires stack fixups? No
5981 ///
5982 /// \p MachineOutlinerRegSave implies that the function should be called with a
5983 /// save and restore of LR to an available register. This allows us to avoid
5984 /// stack fixups. Note that this outlining variant is compatible with the
5985 /// NoLRSave case.
5986 ///
5987 /// That is,
5988 ///
5989 /// I1     Save LR                    OUTLINED_FUNCTION:
5990 /// I2 --> BL OUTLINED_FUNCTION       I1
5991 /// I3     Restore LR                 I2
5992 ///                                   I3
5993 ///                                   RET
5994 ///
5995 /// * Call construction overhead: 3 (save + BL + restore)
5996 /// * Frame construction overhead: 1 (ret)
5997 /// * Requires stack fixups? No
5998 enum MachineOutlinerClass {
5999   MachineOutlinerDefault,  /// Emit a save, restore, call, and return.
6000   MachineOutlinerTailCall, /// Only emit a branch.
6001   MachineOutlinerNoLRSave, /// Emit a call and return.
6002   MachineOutlinerThunk,    /// Emit a call and tail-call.
6003   MachineOutlinerRegSave   /// Same as default, but save to a register.
6004 };
6005 
6006 enum MachineOutlinerMBBFlags {
6007   LRUnavailableSomewhere = 0x2,
6008   HasCalls = 0x4,
6009   UnsafeRegsDead = 0x8
6010 };
6011 
6012 unsigned
6013 AArch64InstrInfo::findRegisterToSaveLRTo(const outliner::Candidate &C) const {
6014   assert(C.LRUWasSet && "LRU wasn't set?");
6015   MachineFunction *MF = C.getMF();
6016   const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
6017       MF->getSubtarget().getRegisterInfo());
6018 
6019   // Check if there is an available register across the sequence that we can
6020   // use.
6021   for (unsigned Reg : AArch64::GPR64RegClass) {
6022     if (!ARI->isReservedReg(*MF, Reg) &&
6023         Reg != AArch64::LR &&  // LR is not reserved, but don't use it.
6024         Reg != AArch64::X16 && // X16 is not guaranteed to be preserved.
6025         Reg != AArch64::X17 && // Ditto for X17.
6026         C.LRU.available(Reg) && C.UsedInSequence.available(Reg))
6027       return Reg;
6028   }
6029 
6030   // No suitable register. Return 0.
6031   return 0u;
6032 }
6033 
6034 static bool
6035 outliningCandidatesSigningScopeConsensus(const outliner::Candidate &a,
6036                                          const outliner::Candidate &b) {
6037   const auto &MFIa = a.getMF()->getInfo<AArch64FunctionInfo>();
6038   const auto &MFIb = b.getMF()->getInfo<AArch64FunctionInfo>();
6039 
6040   return MFIa->shouldSignReturnAddress(false) == MFIb->shouldSignReturnAddress(false) &&
6041          MFIa->shouldSignReturnAddress(true) == MFIb->shouldSignReturnAddress(true);
6042 }
6043 
6044 static bool
6045 outliningCandidatesSigningKeyConsensus(const outliner::Candidate &a,
6046                                        const outliner::Candidate &b) {
6047   const auto &MFIa = a.getMF()->getInfo<AArch64FunctionInfo>();
6048   const auto &MFIb = b.getMF()->getInfo<AArch64FunctionInfo>();
6049 
6050   return MFIa->shouldSignWithBKey() == MFIb->shouldSignWithBKey();
6051 }
6052 
6053 static bool outliningCandidatesV8_3OpsConsensus(const outliner::Candidate &a,
6054                                                 const outliner::Candidate &b) {
6055   const AArch64Subtarget &SubtargetA =
6056       a.getMF()->getSubtarget<AArch64Subtarget>();
6057   const AArch64Subtarget &SubtargetB =
6058       b.getMF()->getSubtarget<AArch64Subtarget>();
6059   return SubtargetA.hasV8_3aOps() == SubtargetB.hasV8_3aOps();
6060 }
6061 
6062 outliner::OutlinedFunction AArch64InstrInfo::getOutliningCandidateInfo(
6063     std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
6064   outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
6065   unsigned SequenceSize =
6066       std::accumulate(FirstCand.front(), std::next(FirstCand.back()), 0,
6067                       [this](unsigned Sum, const MachineInstr &MI) {
6068                         return Sum + getInstSizeInBytes(MI);
6069                       });
6070   unsigned NumBytesToCreateFrame = 0;
6071 
6072   // We only allow outlining for functions having exactly matching return
6073   // address signing attributes, i.e., all share the same value for the
6074   // attribute "sign-return-address" and all share the same type of key they
6075   // are signed with.
6076   // Additionally we require all functions to simultaniously either support
6077   // v8.3a features or not. Otherwise an outlined function could get signed
6078   // using dedicated v8.3 instructions and a call from a function that doesn't
6079   // support v8.3 instructions would therefore be invalid.
6080   if (std::adjacent_find(
6081           RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(),
6082           [](const outliner::Candidate &a, const outliner::Candidate &b) {
6083             // Return true if a and b are non-equal w.r.t. return address
6084             // signing or support of v8.3a features
6085             if (outliningCandidatesSigningScopeConsensus(a, b) &&
6086                 outliningCandidatesSigningKeyConsensus(a, b) &&
6087                 outliningCandidatesV8_3OpsConsensus(a, b)) {
6088               return false;
6089             }
6090             return true;
6091           }) != RepeatedSequenceLocs.end()) {
6092     return outliner::OutlinedFunction();
6093   }
6094 
6095   // Since at this point all candidates agree on their return address signing
6096   // picking just one is fine. If the candidate functions potentially sign their
6097   // return addresses, the outlined function should do the same. Note that in
6098   // the case of "sign-return-address"="non-leaf" this is an assumption: It is
6099   // not certainly true that the outlined function will have to sign its return
6100   // address but this decision is made later, when the decision to outline
6101   // has already been made.
6102   // The same holds for the number of additional instructions we need: On
6103   // v8.3a RET can be replaced by RETAA/RETAB and no AUT instruction is
6104   // necessary. However, at this point we don't know if the outlined function
6105   // will have a RET instruction so we assume the worst.
6106   const TargetRegisterInfo &TRI = getRegisterInfo();
6107   if (FirstCand.getMF()
6108           ->getInfo<AArch64FunctionInfo>()
6109           ->shouldSignReturnAddress(true)) {
6110     // One PAC and one AUT instructions
6111     NumBytesToCreateFrame += 8;
6112 
6113     // We have to check if sp modifying instructions would get outlined.
6114     // If so we only allow outlining if sp is unchanged overall, so matching
6115     // sub and add instructions are okay to outline, all other sp modifications
6116     // are not
6117     auto hasIllegalSPModification = [&TRI](outliner::Candidate &C) {
6118       int SPValue = 0;
6119       MachineBasicBlock::iterator MBBI = C.front();
6120       for (;;) {
6121         if (MBBI->modifiesRegister(AArch64::SP, &TRI)) {
6122           switch (MBBI->getOpcode()) {
6123           case AArch64::ADDXri:
6124           case AArch64::ADDWri:
6125             assert(MBBI->getNumOperands() == 4 && "Wrong number of operands");
6126             assert(MBBI->getOperand(2).isImm() &&
6127                    "Expected operand to be immediate");
6128             assert(MBBI->getOperand(1).isReg() &&
6129                    "Expected operand to be a register");
6130             // Check if the add just increments sp. If so, we search for
6131             // matching sub instructions that decrement sp. If not, the
6132             // modification is illegal
6133             if (MBBI->getOperand(1).getReg() == AArch64::SP)
6134               SPValue += MBBI->getOperand(2).getImm();
6135             else
6136               return true;
6137             break;
6138           case AArch64::SUBXri:
6139           case AArch64::SUBWri:
6140             assert(MBBI->getNumOperands() == 4 && "Wrong number of operands");
6141             assert(MBBI->getOperand(2).isImm() &&
6142                    "Expected operand to be immediate");
6143             assert(MBBI->getOperand(1).isReg() &&
6144                    "Expected operand to be a register");
6145             // Check if the sub just decrements sp. If so, we search for
6146             // matching add instructions that increment sp. If not, the
6147             // modification is illegal
6148             if (MBBI->getOperand(1).getReg() == AArch64::SP)
6149               SPValue -= MBBI->getOperand(2).getImm();
6150             else
6151               return true;
6152             break;
6153           default:
6154             return true;
6155           }
6156         }
6157         if (MBBI == C.back())
6158           break;
6159         ++MBBI;
6160       }
6161       if (SPValue)
6162         return true;
6163       return false;
6164     };
6165     // Remove candidates with illegal stack modifying instructions
6166     llvm::erase_if(RepeatedSequenceLocs, hasIllegalSPModification);
6167 
6168     // If the sequence doesn't have enough candidates left, then we're done.
6169     if (RepeatedSequenceLocs.size() < 2)
6170       return outliner::OutlinedFunction();
6171   }
6172 
6173   // Properties about candidate MBBs that hold for all of them.
6174   unsigned FlagsSetInAll = 0xF;
6175 
6176   // Compute liveness information for each candidate, and set FlagsSetInAll.
6177   std::for_each(RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(),
6178                 [&FlagsSetInAll](outliner::Candidate &C) {
6179                   FlagsSetInAll &= C.Flags;
6180                 });
6181 
6182   // According to the AArch64 Procedure Call Standard, the following are
6183   // undefined on entry/exit from a function call:
6184   //
6185   // * Registers x16, x17, (and thus w16, w17)
6186   // * Condition codes (and thus the NZCV register)
6187   //
6188   // Because if this, we can't outline any sequence of instructions where
6189   // one
6190   // of these registers is live into/across it. Thus, we need to delete
6191   // those
6192   // candidates.
6193   auto CantGuaranteeValueAcrossCall = [&TRI](outliner::Candidate &C) {
6194     // If the unsafe registers in this block are all dead, then we don't need
6195     // to compute liveness here.
6196     if (C.Flags & UnsafeRegsDead)
6197       return false;
6198     C.initLRU(TRI);
6199     LiveRegUnits LRU = C.LRU;
6200     return (!LRU.available(AArch64::W16) || !LRU.available(AArch64::W17) ||
6201             !LRU.available(AArch64::NZCV));
6202   };
6203 
6204   // Are there any candidates where those registers are live?
6205   if (!(FlagsSetInAll & UnsafeRegsDead)) {
6206     // Erase every candidate that violates the restrictions above. (It could be
6207     // true that we have viable candidates, so it's not worth bailing out in
6208     // the case that, say, 1 out of 20 candidates violate the restructions.)
6209     llvm::erase_if(RepeatedSequenceLocs, CantGuaranteeValueAcrossCall);
6210 
6211     // If the sequence doesn't have enough candidates left, then we're done.
6212     if (RepeatedSequenceLocs.size() < 2)
6213       return outliner::OutlinedFunction();
6214   }
6215 
6216   // At this point, we have only "safe" candidates to outline. Figure out
6217   // frame + call instruction information.
6218 
6219   unsigned LastInstrOpcode = RepeatedSequenceLocs[0].back()->getOpcode();
6220 
6221   // Helper lambda which sets call information for every candidate.
6222   auto SetCandidateCallInfo =
6223       [&RepeatedSequenceLocs](unsigned CallID, unsigned NumBytesForCall) {
6224         for (outliner::Candidate &C : RepeatedSequenceLocs)
6225           C.setCallInfo(CallID, NumBytesForCall);
6226       };
6227 
6228   unsigned FrameID = MachineOutlinerDefault;
6229   NumBytesToCreateFrame += 4;
6230 
6231   bool HasBTI = any_of(RepeatedSequenceLocs, [](outliner::Candidate &C) {
6232     return C.getMF()->getInfo<AArch64FunctionInfo>()->branchTargetEnforcement();
6233   });
6234 
6235   // We check to see if CFI Instructions are present, and if they are
6236   // we find the number of CFI Instructions in the candidates.
6237   unsigned CFICount = 0;
6238   MachineBasicBlock::iterator MBBI = RepeatedSequenceLocs[0].front();
6239   for (unsigned Loc = RepeatedSequenceLocs[0].getStartIdx();
6240        Loc < RepeatedSequenceLocs[0].getEndIdx() + 1; Loc++) {
6241     const std::vector<MCCFIInstruction> &CFIInstructions =
6242         RepeatedSequenceLocs[0].getMF()->getFrameInstructions();
6243     if (MBBI->isCFIInstruction()) {
6244       unsigned CFIIndex = MBBI->getOperand(0).getCFIIndex();
6245       MCCFIInstruction CFI = CFIInstructions[CFIIndex];
6246       CFICount++;
6247     }
6248     MBBI++;
6249   }
6250 
6251   // We compare the number of found CFI Instructions to  the number of CFI
6252   // instructions in the parent function for each candidate.  We must check this
6253   // since if we outline one of the CFI instructions in a function, we have to
6254   // outline them all for correctness. If we do not, the address offsets will be
6255   // incorrect between the two sections of the program.
6256   for (outliner::Candidate &C : RepeatedSequenceLocs) {
6257     std::vector<MCCFIInstruction> CFIInstructions =
6258         C.getMF()->getFrameInstructions();
6259 
6260     if (CFICount > 0 && CFICount != CFIInstructions.size())
6261       return outliner::OutlinedFunction();
6262   }
6263 
6264   // Returns true if an instructions is safe to fix up, false otherwise.
6265   auto IsSafeToFixup = [this, &TRI](MachineInstr &MI) {
6266     if (MI.isCall())
6267       return true;
6268 
6269     if (!MI.modifiesRegister(AArch64::SP, &TRI) &&
6270         !MI.readsRegister(AArch64::SP, &TRI))
6271       return true;
6272 
6273     // Any modification of SP will break our code to save/restore LR.
6274     // FIXME: We could handle some instructions which add a constant
6275     // offset to SP, with a bit more work.
6276     if (MI.modifiesRegister(AArch64::SP, &TRI))
6277       return false;
6278 
6279     // At this point, we have a stack instruction that we might need to
6280     // fix up. We'll handle it if it's a load or store.
6281     if (MI.mayLoadOrStore()) {
6282       const MachineOperand *Base; // Filled with the base operand of MI.
6283       int64_t Offset;             // Filled with the offset of MI.
6284       bool OffsetIsScalable;
6285 
6286       // Does it allow us to offset the base operand and is the base the
6287       // register SP?
6288       if (!getMemOperandWithOffset(MI, Base, Offset, OffsetIsScalable, &TRI) ||
6289           !Base->isReg() || Base->getReg() != AArch64::SP)
6290         return false;
6291 
6292       // Fixe-up code below assumes bytes.
6293       if (OffsetIsScalable)
6294         return false;
6295 
6296       // Find the minimum/maximum offset for this instruction and check
6297       // if fixing it up would be in range.
6298       int64_t MinOffset,
6299           MaxOffset;  // Unscaled offsets for the instruction.
6300       TypeSize Scale(0U, false); // The scale to multiply the offsets by.
6301       unsigned DummyWidth;
6302       getMemOpInfo(MI.getOpcode(), Scale, DummyWidth, MinOffset, MaxOffset);
6303 
6304       Offset += 16; // Update the offset to what it would be if we outlined.
6305       if (Offset < MinOffset * (int64_t)Scale.getFixedSize() ||
6306           Offset > MaxOffset * (int64_t)Scale.getFixedSize())
6307         return false;
6308 
6309       // It's in range, so we can outline it.
6310       return true;
6311     }
6312 
6313     // FIXME: Add handling for instructions like "add x0, sp, #8".
6314 
6315     // We can't fix it up, so don't outline it.
6316     return false;
6317   };
6318 
6319   // True if it's possible to fix up each stack instruction in this sequence.
6320   // Important for frames/call variants that modify the stack.
6321   bool AllStackInstrsSafe = std::all_of(
6322       FirstCand.front(), std::next(FirstCand.back()), IsSafeToFixup);
6323 
6324   // If the last instruction in any candidate is a terminator, then we should
6325   // tail call all of the candidates.
6326   if (RepeatedSequenceLocs[0].back()->isTerminator()) {
6327     FrameID = MachineOutlinerTailCall;
6328     NumBytesToCreateFrame = 0;
6329     SetCandidateCallInfo(MachineOutlinerTailCall, 4);
6330   }
6331 
6332   else if (LastInstrOpcode == AArch64::BL ||
6333            ((LastInstrOpcode == AArch64::BLR ||
6334              LastInstrOpcode == AArch64::BLRNoIP) &&
6335             !HasBTI)) {
6336     // FIXME: Do we need to check if the code after this uses the value of LR?
6337     FrameID = MachineOutlinerThunk;
6338     NumBytesToCreateFrame = 0;
6339     SetCandidateCallInfo(MachineOutlinerThunk, 4);
6340   }
6341 
6342   else {
6343     // We need to decide how to emit calls + frames. We can always emit the same
6344     // frame if we don't need to save to the stack. If we have to save to the
6345     // stack, then we need a different frame.
6346     unsigned NumBytesNoStackCalls = 0;
6347     std::vector<outliner::Candidate> CandidatesWithoutStackFixups;
6348 
6349     // Check if we have to save LR.
6350     for (outliner::Candidate &C : RepeatedSequenceLocs) {
6351       C.initLRU(TRI);
6352 
6353       // If we have a noreturn caller, then we're going to be conservative and
6354       // say that we have to save LR. If we don't have a ret at the end of the
6355       // block, then we can't reason about liveness accurately.
6356       //
6357       // FIXME: We can probably do better than always disabling this in
6358       // noreturn functions by fixing up the liveness info.
6359       bool IsNoReturn =
6360           C.getMF()->getFunction().hasFnAttribute(Attribute::NoReturn);
6361 
6362       // Is LR available? If so, we don't need a save.
6363       if (C.LRU.available(AArch64::LR) && !IsNoReturn) {
6364         NumBytesNoStackCalls += 4;
6365         C.setCallInfo(MachineOutlinerNoLRSave, 4);
6366         CandidatesWithoutStackFixups.push_back(C);
6367       }
6368 
6369       // Is an unused register available? If so, we won't modify the stack, so
6370       // we can outline with the same frame type as those that don't save LR.
6371       else if (findRegisterToSaveLRTo(C)) {
6372         NumBytesNoStackCalls += 12;
6373         C.setCallInfo(MachineOutlinerRegSave, 12);
6374         CandidatesWithoutStackFixups.push_back(C);
6375       }
6376 
6377       // Is SP used in the sequence at all? If not, we don't have to modify
6378       // the stack, so we are guaranteed to get the same frame.
6379       else if (C.UsedInSequence.available(AArch64::SP)) {
6380         NumBytesNoStackCalls += 12;
6381         C.setCallInfo(MachineOutlinerDefault, 12);
6382         CandidatesWithoutStackFixups.push_back(C);
6383       }
6384 
6385       // If we outline this, we need to modify the stack. Pretend we don't
6386       // outline this by saving all of its bytes.
6387       else {
6388         NumBytesNoStackCalls += SequenceSize;
6389       }
6390     }
6391 
6392     // If there are no places where we have to save LR, then note that we
6393     // don't have to update the stack. Otherwise, give every candidate the
6394     // default call type, as long as it's safe to do so.
6395     if (!AllStackInstrsSafe ||
6396         NumBytesNoStackCalls <= RepeatedSequenceLocs.size() * 12) {
6397       RepeatedSequenceLocs = CandidatesWithoutStackFixups;
6398       FrameID = MachineOutlinerNoLRSave;
6399     } else {
6400       SetCandidateCallInfo(MachineOutlinerDefault, 12);
6401 
6402       // Bugzilla ID: 46767
6403       // TODO: Check if fixing up the stack more than once is safe so we can
6404       // outline these.
6405       //
6406       // An outline resulting in a caller that requires stack fixups at the
6407       // callsite to a callee that also requires stack fixups can happen when
6408       // there are no available registers at the candidate callsite for a
6409       // candidate that itself also has calls.
6410       //
6411       // In other words if function_containing_sequence in the following pseudo
6412       // assembly requires that we save LR at the point of the call, but there
6413       // are no available registers: in this case we save using SP and as a
6414       // result the SP offsets requires stack fixups by multiples of 16.
6415       //
6416       // function_containing_sequence:
6417       //   ...
6418       //   save LR to SP <- Requires stack instr fixups in OUTLINED_FUNCTION_N
6419       //   call OUTLINED_FUNCTION_N
6420       //   restore LR from SP
6421       //   ...
6422       //
6423       // OUTLINED_FUNCTION_N:
6424       //   save LR to SP <- Requires stack instr fixups in OUTLINED_FUNCTION_N
6425       //   ...
6426       //   bl foo
6427       //   restore LR from SP
6428       //   ret
6429       //
6430       // Because the code to handle more than one stack fixup does not
6431       // currently have the proper checks for legality, these cases will assert
6432       // in the AArch64 MachineOutliner. This is because the code to do this
6433       // needs more hardening, testing, better checks that generated code is
6434       // legal, etc and because it is only verified to handle a single pass of
6435       // stack fixup.
6436       //
6437       // The assert happens in AArch64InstrInfo::buildOutlinedFrame to catch
6438       // these cases until they are known to be handled. Bugzilla 46767 is
6439       // referenced in comments at the assert site.
6440       //
6441       // To avoid asserting (or generating non-legal code on noassert builds)
6442       // we remove all candidates which would need more than one stack fixup by
6443       // pruning the cases where the candidate has calls while also having no
6444       // available LR and having no available general purpose registers to copy
6445       // LR to (ie one extra stack save/restore).
6446       //
6447       if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
6448         erase_if(RepeatedSequenceLocs, [this](outliner::Candidate &C) {
6449           return (std::any_of(
6450                      C.front(), std::next(C.back()),
6451                      [](const MachineInstr &MI) { return MI.isCall(); })) &&
6452                  (!C.LRU.available(AArch64::LR) || !findRegisterToSaveLRTo(C));
6453         });
6454       }
6455     }
6456 
6457     // If we dropped all of the candidates, bail out here.
6458     if (RepeatedSequenceLocs.size() < 2) {
6459       RepeatedSequenceLocs.clear();
6460       return outliner::OutlinedFunction();
6461     }
6462   }
6463 
6464   // Does every candidate's MBB contain a call? If so, then we might have a call
6465   // in the range.
6466   if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
6467     // Check if the range contains a call. These require a save + restore of the
6468     // link register.
6469     bool ModStackToSaveLR = false;
6470     if (std::any_of(FirstCand.front(), FirstCand.back(),
6471                     [](const MachineInstr &MI) { return MI.isCall(); }))
6472       ModStackToSaveLR = true;
6473 
6474     // Handle the last instruction separately. If this is a tail call, then the
6475     // last instruction is a call. We don't want to save + restore in this case.
6476     // However, it could be possible that the last instruction is a call without
6477     // it being valid to tail call this sequence. We should consider this as
6478     // well.
6479     else if (FrameID != MachineOutlinerThunk &&
6480              FrameID != MachineOutlinerTailCall && FirstCand.back()->isCall())
6481       ModStackToSaveLR = true;
6482 
6483     if (ModStackToSaveLR) {
6484       // We can't fix up the stack. Bail out.
6485       if (!AllStackInstrsSafe) {
6486         RepeatedSequenceLocs.clear();
6487         return outliner::OutlinedFunction();
6488       }
6489 
6490       // Save + restore LR.
6491       NumBytesToCreateFrame += 8;
6492     }
6493   }
6494 
6495   // If we have CFI instructions, we can only outline if the outlined section
6496   // can be a tail call
6497   if (FrameID != MachineOutlinerTailCall && CFICount > 0)
6498     return outliner::OutlinedFunction();
6499 
6500   return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
6501                                     NumBytesToCreateFrame, FrameID);
6502 }
6503 
6504 bool AArch64InstrInfo::isFunctionSafeToOutlineFrom(
6505     MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
6506   const Function &F = MF.getFunction();
6507 
6508   // Can F be deduplicated by the linker? If it can, don't outline from it.
6509   if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
6510     return false;
6511 
6512   // Don't outline from functions with section markings; the program could
6513   // expect that all the code is in the named section.
6514   // FIXME: Allow outlining from multiple functions with the same section
6515   // marking.
6516   if (F.hasSection())
6517     return false;
6518 
6519   // Outlining from functions with redzones is unsafe since the outliner may
6520   // modify the stack. Check if hasRedZone is true or unknown; if yes, don't
6521   // outline from it.
6522   AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
6523   if (!AFI || AFI->hasRedZone().getValueOr(true))
6524     return false;
6525 
6526   // FIXME: Teach the outliner to generate/handle Windows unwind info.
6527   if (MF.getTarget().getMCAsmInfo()->usesWindowsCFI())
6528     return false;
6529 
6530   // It's safe to outline from MF.
6531   return true;
6532 }
6533 
6534 bool AArch64InstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
6535                                               unsigned &Flags) const {
6536   // Check if LR is available through all of the MBB. If it's not, then set
6537   // a flag.
6538   assert(MBB.getParent()->getRegInfo().tracksLiveness() &&
6539          "Suitable Machine Function for outlining must track liveness");
6540   LiveRegUnits LRU(getRegisterInfo());
6541 
6542   std::for_each(MBB.rbegin(), MBB.rend(),
6543                 [&LRU](MachineInstr &MI) { LRU.accumulate(MI); });
6544 
6545   // Check if each of the unsafe registers are available...
6546   bool W16AvailableInBlock = LRU.available(AArch64::W16);
6547   bool W17AvailableInBlock = LRU.available(AArch64::W17);
6548   bool NZCVAvailableInBlock = LRU.available(AArch64::NZCV);
6549 
6550   // If all of these are dead (and not live out), we know we don't have to check
6551   // them later.
6552   if (W16AvailableInBlock && W17AvailableInBlock && NZCVAvailableInBlock)
6553     Flags |= MachineOutlinerMBBFlags::UnsafeRegsDead;
6554 
6555   // Now, add the live outs to the set.
6556   LRU.addLiveOuts(MBB);
6557 
6558   // If any of these registers is available in the MBB, but also a live out of
6559   // the block, then we know outlining is unsafe.
6560   if (W16AvailableInBlock && !LRU.available(AArch64::W16))
6561     return false;
6562   if (W17AvailableInBlock && !LRU.available(AArch64::W17))
6563     return false;
6564   if (NZCVAvailableInBlock && !LRU.available(AArch64::NZCV))
6565     return false;
6566 
6567   // Check if there's a call inside this MachineBasicBlock. If there is, then
6568   // set a flag.
6569   if (any_of(MBB, [](MachineInstr &MI) { return MI.isCall(); }))
6570     Flags |= MachineOutlinerMBBFlags::HasCalls;
6571 
6572   MachineFunction *MF = MBB.getParent();
6573 
6574   // In the event that we outline, we may have to save LR. If there is an
6575   // available register in the MBB, then we'll always save LR there. Check if
6576   // this is true.
6577   bool CanSaveLR = false;
6578   const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
6579       MF->getSubtarget().getRegisterInfo());
6580 
6581   // Check if there is an available register across the sequence that we can
6582   // use.
6583   for (unsigned Reg : AArch64::GPR64RegClass) {
6584     if (!ARI->isReservedReg(*MF, Reg) && Reg != AArch64::LR &&
6585         Reg != AArch64::X16 && Reg != AArch64::X17 && LRU.available(Reg)) {
6586       CanSaveLR = true;
6587       break;
6588     }
6589   }
6590 
6591   // Check if we have a register we can save LR to, and if LR was used
6592   // somewhere. If both of those things are true, then we need to evaluate the
6593   // safety of outlining stack instructions later.
6594   if (!CanSaveLR && !LRU.available(AArch64::LR))
6595     Flags |= MachineOutlinerMBBFlags::LRUnavailableSomewhere;
6596 
6597   return true;
6598 }
6599 
6600 outliner::InstrType
6601 AArch64InstrInfo::getOutliningType(MachineBasicBlock::iterator &MIT,
6602                                    unsigned Flags) const {
6603   MachineInstr &MI = *MIT;
6604   MachineBasicBlock *MBB = MI.getParent();
6605   MachineFunction *MF = MBB->getParent();
6606   AArch64FunctionInfo *FuncInfo = MF->getInfo<AArch64FunctionInfo>();
6607 
6608   // Don't outline anything used for return address signing. The outlined
6609   // function will get signed later if needed
6610   switch (MI.getOpcode()) {
6611   case AArch64::PACIASP:
6612   case AArch64::PACIBSP:
6613   case AArch64::AUTIASP:
6614   case AArch64::AUTIBSP:
6615   case AArch64::RETAA:
6616   case AArch64::RETAB:
6617   case AArch64::EMITBKEY:
6618     return outliner::InstrType::Illegal;
6619   }
6620 
6621   // Don't outline LOHs.
6622   if (FuncInfo->getLOHRelated().count(&MI))
6623     return outliner::InstrType::Illegal;
6624 
6625   // We can only outline these if we will tail call the outlined function, or
6626   // fix up the CFI offsets. Currently, CFI instructions are outlined only if
6627   // in a tail call.
6628   //
6629   // FIXME: If the proper fixups for the offset are implemented, this should be
6630   // possible.
6631   if (MI.isCFIInstruction())
6632     return outliner::InstrType::Legal;
6633 
6634   // Don't allow debug values to impact outlining type.
6635   if (MI.isDebugInstr() || MI.isIndirectDebugValue())
6636     return outliner::InstrType::Invisible;
6637 
6638   // At this point, KILL instructions don't really tell us much so we can go
6639   // ahead and skip over them.
6640   if (MI.isKill())
6641     return outliner::InstrType::Invisible;
6642 
6643   // Is this a terminator for a basic block?
6644   if (MI.isTerminator()) {
6645 
6646     // Is this the end of a function?
6647     if (MI.getParent()->succ_empty())
6648       return outliner::InstrType::Legal;
6649 
6650     // It's not, so don't outline it.
6651     return outliner::InstrType::Illegal;
6652   }
6653 
6654   // Make sure none of the operands are un-outlinable.
6655   for (const MachineOperand &MOP : MI.operands()) {
6656     if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() ||
6657         MOP.isTargetIndex())
6658       return outliner::InstrType::Illegal;
6659 
6660     // If it uses LR or W30 explicitly, then don't touch it.
6661     if (MOP.isReg() && !MOP.isImplicit() &&
6662         (MOP.getReg() == AArch64::LR || MOP.getReg() == AArch64::W30))
6663       return outliner::InstrType::Illegal;
6664   }
6665 
6666   // Special cases for instructions that can always be outlined, but will fail
6667   // the later tests. e.g, ADRPs, which are PC-relative use LR, but can always
6668   // be outlined because they don't require a *specific* value to be in LR.
6669   if (MI.getOpcode() == AArch64::ADRP)
6670     return outliner::InstrType::Legal;
6671 
6672   // If MI is a call we might be able to outline it. We don't want to outline
6673   // any calls that rely on the position of items on the stack. When we outline
6674   // something containing a call, we have to emit a save and restore of LR in
6675   // the outlined function. Currently, this always happens by saving LR to the
6676   // stack. Thus, if we outline, say, half the parameters for a function call
6677   // plus the call, then we'll break the callee's expectations for the layout
6678   // of the stack.
6679   //
6680   // FIXME: Allow calls to functions which construct a stack frame, as long
6681   // as they don't access arguments on the stack.
6682   // FIXME: Figure out some way to analyze functions defined in other modules.
6683   // We should be able to compute the memory usage based on the IR calling
6684   // convention, even if we can't see the definition.
6685   if (MI.isCall()) {
6686     // Get the function associated with the call. Look at each operand and find
6687     // the one that represents the callee and get its name.
6688     const Function *Callee = nullptr;
6689     for (const MachineOperand &MOP : MI.operands()) {
6690       if (MOP.isGlobal()) {
6691         Callee = dyn_cast<Function>(MOP.getGlobal());
6692         break;
6693       }
6694     }
6695 
6696     // Never outline calls to mcount.  There isn't any rule that would require
6697     // this, but the Linux kernel's "ftrace" feature depends on it.
6698     if (Callee && Callee->getName() == "\01_mcount")
6699       return outliner::InstrType::Illegal;
6700 
6701     // If we don't know anything about the callee, assume it depends on the
6702     // stack layout of the caller. In that case, it's only legal to outline
6703     // as a tail-call. Explicitly list the call instructions we know about so we
6704     // don't get unexpected results with call pseudo-instructions.
6705     auto UnknownCallOutlineType = outliner::InstrType::Illegal;
6706     if (MI.getOpcode() == AArch64::BLR ||
6707         MI.getOpcode() == AArch64::BLRNoIP || MI.getOpcode() == AArch64::BL)
6708       UnknownCallOutlineType = outliner::InstrType::LegalTerminator;
6709 
6710     if (!Callee)
6711       return UnknownCallOutlineType;
6712 
6713     // We have a function we have information about. Check it if it's something
6714     // can safely outline.
6715     MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
6716 
6717     // We don't know what's going on with the callee at all. Don't touch it.
6718     if (!CalleeMF)
6719       return UnknownCallOutlineType;
6720 
6721     // Check if we know anything about the callee saves on the function. If we
6722     // don't, then don't touch it, since that implies that we haven't
6723     // computed anything about its stack frame yet.
6724     MachineFrameInfo &MFI = CalleeMF->getFrameInfo();
6725     if (!MFI.isCalleeSavedInfoValid() || MFI.getStackSize() > 0 ||
6726         MFI.getNumObjects() > 0)
6727       return UnknownCallOutlineType;
6728 
6729     // At this point, we can say that CalleeMF ought to not pass anything on the
6730     // stack. Therefore, we can outline it.
6731     return outliner::InstrType::Legal;
6732   }
6733 
6734   // Don't outline positions.
6735   if (MI.isPosition())
6736     return outliner::InstrType::Illegal;
6737 
6738   // Don't touch the link register or W30.
6739   if (MI.readsRegister(AArch64::W30, &getRegisterInfo()) ||
6740       MI.modifiesRegister(AArch64::W30, &getRegisterInfo()))
6741     return outliner::InstrType::Illegal;
6742 
6743   // Don't outline BTI instructions, because that will prevent the outlining
6744   // site from being indirectly callable.
6745   if (MI.getOpcode() == AArch64::HINT) {
6746     int64_t Imm = MI.getOperand(0).getImm();
6747     if (Imm == 32 || Imm == 34 || Imm == 36 || Imm == 38)
6748       return outliner::InstrType::Illegal;
6749   }
6750 
6751   return outliner::InstrType::Legal;
6752 }
6753 
6754 void AArch64InstrInfo::fixupPostOutline(MachineBasicBlock &MBB) const {
6755   for (MachineInstr &MI : MBB) {
6756     const MachineOperand *Base;
6757     unsigned Width;
6758     int64_t Offset;
6759     bool OffsetIsScalable;
6760 
6761     // Is this a load or store with an immediate offset with SP as the base?
6762     if (!MI.mayLoadOrStore() ||
6763         !getMemOperandWithOffsetWidth(MI, Base, Offset, OffsetIsScalable, Width,
6764                                       &RI) ||
6765         (Base->isReg() && Base->getReg() != AArch64::SP))
6766       continue;
6767 
6768     // It is, so we have to fix it up.
6769     TypeSize Scale(0U, false);
6770     int64_t Dummy1, Dummy2;
6771 
6772     MachineOperand &StackOffsetOperand = getMemOpBaseRegImmOfsOffsetOperand(MI);
6773     assert(StackOffsetOperand.isImm() && "Stack offset wasn't immediate!");
6774     getMemOpInfo(MI.getOpcode(), Scale, Width, Dummy1, Dummy2);
6775     assert(Scale != 0 && "Unexpected opcode!");
6776     assert(!OffsetIsScalable && "Expected offset to be a byte offset");
6777 
6778     // We've pushed the return address to the stack, so add 16 to the offset.
6779     // This is safe, since we already checked if it would overflow when we
6780     // checked if this instruction was legal to outline.
6781     int64_t NewImm = (Offset + 16) / (int64_t)Scale.getFixedSize();
6782     StackOffsetOperand.setImm(NewImm);
6783   }
6784 }
6785 
6786 static void signOutlinedFunction(MachineFunction &MF, MachineBasicBlock &MBB,
6787                                  bool ShouldSignReturnAddr,
6788                                  bool ShouldSignReturnAddrWithAKey) {
6789   if (ShouldSignReturnAddr) {
6790     MachineBasicBlock::iterator MBBPAC = MBB.begin();
6791     MachineBasicBlock::iterator MBBAUT = MBB.getFirstTerminator();
6792     const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
6793     const TargetInstrInfo *TII = Subtarget.getInstrInfo();
6794     DebugLoc DL;
6795 
6796     if (MBBAUT != MBB.end())
6797       DL = MBBAUT->getDebugLoc();
6798 
6799     // At the very beginning of the basic block we insert the following
6800     // depending on the key type
6801     //
6802     // a_key:                   b_key:
6803     //    PACIASP                   EMITBKEY
6804     //    CFI_INSTRUCTION           PACIBSP
6805     //                              CFI_INSTRUCTION
6806     if (ShouldSignReturnAddrWithAKey) {
6807       BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::PACIASP))
6808           .setMIFlag(MachineInstr::FrameSetup);
6809     } else {
6810       BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::EMITBKEY))
6811           .setMIFlag(MachineInstr::FrameSetup);
6812       BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::PACIBSP))
6813           .setMIFlag(MachineInstr::FrameSetup);
6814     }
6815     unsigned CFIIndex =
6816         MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
6817     BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::CFI_INSTRUCTION))
6818         .addCFIIndex(CFIIndex)
6819         .setMIFlags(MachineInstr::FrameSetup);
6820 
6821     // If v8.3a features are available we can replace a RET instruction by
6822     // RETAA or RETAB and omit the AUT instructions
6823     if (Subtarget.hasPAuth() && MBBAUT != MBB.end() &&
6824         MBBAUT->getOpcode() == AArch64::RET) {
6825       BuildMI(MBB, MBBAUT, DL,
6826               TII->get(ShouldSignReturnAddrWithAKey ? AArch64::RETAA
6827                                                     : AArch64::RETAB))
6828           .copyImplicitOps(*MBBAUT);
6829       MBB.erase(MBBAUT);
6830     } else {
6831       BuildMI(MBB, MBBAUT, DL,
6832               TII->get(ShouldSignReturnAddrWithAKey ? AArch64::AUTIASP
6833                                                     : AArch64::AUTIBSP))
6834           .setMIFlag(MachineInstr::FrameDestroy);
6835     }
6836   }
6837 }
6838 
6839 void AArch64InstrInfo::buildOutlinedFrame(
6840     MachineBasicBlock &MBB, MachineFunction &MF,
6841     const outliner::OutlinedFunction &OF) const {
6842 
6843   AArch64FunctionInfo *FI = MF.getInfo<AArch64FunctionInfo>();
6844 
6845   if (OF.FrameConstructionID == MachineOutlinerTailCall)
6846     FI->setOutliningStyle("Tail Call");
6847   else if (OF.FrameConstructionID == MachineOutlinerThunk) {
6848     // For thunk outlining, rewrite the last instruction from a call to a
6849     // tail-call.
6850     MachineInstr *Call = &*--MBB.instr_end();
6851     unsigned TailOpcode;
6852     if (Call->getOpcode() == AArch64::BL) {
6853       TailOpcode = AArch64::TCRETURNdi;
6854     } else {
6855       assert(Call->getOpcode() == AArch64::BLR ||
6856              Call->getOpcode() == AArch64::BLRNoIP);
6857       TailOpcode = AArch64::TCRETURNriALL;
6858     }
6859     MachineInstr *TC = BuildMI(MF, DebugLoc(), get(TailOpcode))
6860                            .add(Call->getOperand(0))
6861                            .addImm(0);
6862     MBB.insert(MBB.end(), TC);
6863     Call->eraseFromParent();
6864 
6865     FI->setOutliningStyle("Thunk");
6866   }
6867 
6868   bool IsLeafFunction = true;
6869 
6870   // Is there a call in the outlined range?
6871   auto IsNonTailCall = [](const MachineInstr &MI) {
6872     return MI.isCall() && !MI.isReturn();
6873   };
6874 
6875   if (llvm::any_of(MBB.instrs(), IsNonTailCall)) {
6876     // Fix up the instructions in the range, since we're going to modify the
6877     // stack.
6878 
6879     // Bugzilla ID: 46767
6880     // TODO: Check if fixing up twice is safe so we can outline these.
6881     assert(OF.FrameConstructionID != MachineOutlinerDefault &&
6882            "Can only fix up stack references once");
6883     fixupPostOutline(MBB);
6884 
6885     IsLeafFunction = false;
6886 
6887     // LR has to be a live in so that we can save it.
6888     if (!MBB.isLiveIn(AArch64::LR))
6889       MBB.addLiveIn(AArch64::LR);
6890 
6891     MachineBasicBlock::iterator It = MBB.begin();
6892     MachineBasicBlock::iterator Et = MBB.end();
6893 
6894     if (OF.FrameConstructionID == MachineOutlinerTailCall ||
6895         OF.FrameConstructionID == MachineOutlinerThunk)
6896       Et = std::prev(MBB.end());
6897 
6898     // Insert a save before the outlined region
6899     MachineInstr *STRXpre = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
6900                                 .addReg(AArch64::SP, RegState::Define)
6901                                 .addReg(AArch64::LR)
6902                                 .addReg(AArch64::SP)
6903                                 .addImm(-16);
6904     It = MBB.insert(It, STRXpre);
6905 
6906     const TargetSubtargetInfo &STI = MF.getSubtarget();
6907     const MCRegisterInfo *MRI = STI.getRegisterInfo();
6908     unsigned DwarfReg = MRI->getDwarfRegNum(AArch64::LR, true);
6909 
6910     // Add a CFI saying the stack was moved 16 B down.
6911     int64_t StackPosEntry =
6912         MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 16));
6913     BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
6914         .addCFIIndex(StackPosEntry)
6915         .setMIFlags(MachineInstr::FrameSetup);
6916 
6917     // Add a CFI saying that the LR that we want to find is now 16 B higher than
6918     // before.
6919     int64_t LRPosEntry =
6920         MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, DwarfReg, -16));
6921     BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
6922         .addCFIIndex(LRPosEntry)
6923         .setMIFlags(MachineInstr::FrameSetup);
6924 
6925     // Insert a restore before the terminator for the function.
6926     MachineInstr *LDRXpost = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
6927                                  .addReg(AArch64::SP, RegState::Define)
6928                                  .addReg(AArch64::LR, RegState::Define)
6929                                  .addReg(AArch64::SP)
6930                                  .addImm(16);
6931     Et = MBB.insert(Et, LDRXpost);
6932   }
6933 
6934   // If a bunch of candidates reach this point they must agree on their return
6935   // address signing. It is therefore enough to just consider the signing
6936   // behaviour of one of them
6937   const auto &MFI = *OF.Candidates.front().getMF()->getInfo<AArch64FunctionInfo>();
6938   bool ShouldSignReturnAddr = MFI.shouldSignReturnAddress(!IsLeafFunction);
6939 
6940   // a_key is the default
6941   bool ShouldSignReturnAddrWithAKey = !MFI.shouldSignWithBKey();
6942 
6943   // If this is a tail call outlined function, then there's already a return.
6944   if (OF.FrameConstructionID == MachineOutlinerTailCall ||
6945       OF.FrameConstructionID == MachineOutlinerThunk) {
6946     signOutlinedFunction(MF, MBB, ShouldSignReturnAddr,
6947                          ShouldSignReturnAddrWithAKey);
6948     return;
6949   }
6950 
6951   // It's not a tail call, so we have to insert the return ourselves.
6952 
6953   // LR has to be a live in so that we can return to it.
6954   if (!MBB.isLiveIn(AArch64::LR))
6955     MBB.addLiveIn(AArch64::LR);
6956 
6957   MachineInstr *ret = BuildMI(MF, DebugLoc(), get(AArch64::RET))
6958                           .addReg(AArch64::LR);
6959   MBB.insert(MBB.end(), ret);
6960 
6961   signOutlinedFunction(MF, MBB, ShouldSignReturnAddr,
6962                        ShouldSignReturnAddrWithAKey);
6963 
6964   FI->setOutliningStyle("Function");
6965 
6966   // Did we have to modify the stack by saving the link register?
6967   if (OF.FrameConstructionID != MachineOutlinerDefault)
6968     return;
6969 
6970   // We modified the stack.
6971   // Walk over the basic block and fix up all the stack accesses.
6972   fixupPostOutline(MBB);
6973 }
6974 
6975 MachineBasicBlock::iterator AArch64InstrInfo::insertOutlinedCall(
6976     Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
6977     MachineFunction &MF, const outliner::Candidate &C) const {
6978 
6979   // Are we tail calling?
6980   if (C.CallConstructionID == MachineOutlinerTailCall) {
6981     // If yes, then we can just branch to the label.
6982     It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::TCRETURNdi))
6983                             .addGlobalAddress(M.getNamedValue(MF.getName()))
6984                             .addImm(0));
6985     return It;
6986   }
6987 
6988   // Are we saving the link register?
6989   if (C.CallConstructionID == MachineOutlinerNoLRSave ||
6990       C.CallConstructionID == MachineOutlinerThunk) {
6991     // No, so just insert the call.
6992     It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
6993                             .addGlobalAddress(M.getNamedValue(MF.getName())));
6994     return It;
6995   }
6996 
6997   // We want to return the spot where we inserted the call.
6998   MachineBasicBlock::iterator CallPt;
6999 
7000   // Instructions for saving and restoring LR around the call instruction we're
7001   // going to insert.
7002   MachineInstr *Save;
7003   MachineInstr *Restore;
7004   // Can we save to a register?
7005   if (C.CallConstructionID == MachineOutlinerRegSave) {
7006     // FIXME: This logic should be sunk into a target-specific interface so that
7007     // we don't have to recompute the register.
7008     unsigned Reg = findRegisterToSaveLRTo(C);
7009     assert(Reg != 0 && "No callee-saved register available?");
7010 
7011     // Save and restore LR from that register.
7012     Save = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), Reg)
7013                .addReg(AArch64::XZR)
7014                .addReg(AArch64::LR)
7015                .addImm(0);
7016     Restore = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), AArch64::LR)
7017                 .addReg(AArch64::XZR)
7018                 .addReg(Reg)
7019                 .addImm(0);
7020   } else {
7021     // We have the default case. Save and restore from SP.
7022     Save = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
7023                .addReg(AArch64::SP, RegState::Define)
7024                .addReg(AArch64::LR)
7025                .addReg(AArch64::SP)
7026                .addImm(-16);
7027     Restore = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
7028                   .addReg(AArch64::SP, RegState::Define)
7029                   .addReg(AArch64::LR, RegState::Define)
7030                   .addReg(AArch64::SP)
7031                   .addImm(16);
7032   }
7033 
7034   It = MBB.insert(It, Save);
7035   It++;
7036 
7037   // Insert the call.
7038   It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
7039                           .addGlobalAddress(M.getNamedValue(MF.getName())));
7040   CallPt = It;
7041   It++;
7042 
7043   It = MBB.insert(It, Restore);
7044   return CallPt;
7045 }
7046 
7047 bool AArch64InstrInfo::shouldOutlineFromFunctionByDefault(
7048   MachineFunction &MF) const {
7049   return MF.getFunction().hasMinSize();
7050 }
7051 
7052 Optional<DestSourcePair>
7053 AArch64InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
7054 
7055   // AArch64::ORRWrs and AArch64::ORRXrs with WZR/XZR reg
7056   // and zero immediate operands used as an alias for mov instruction.
7057   if (MI.getOpcode() == AArch64::ORRWrs &&
7058       MI.getOperand(1).getReg() == AArch64::WZR &&
7059       MI.getOperand(3).getImm() == 0x0) {
7060     return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
7061   }
7062 
7063   if (MI.getOpcode() == AArch64::ORRXrs &&
7064       MI.getOperand(1).getReg() == AArch64::XZR &&
7065       MI.getOperand(3).getImm() == 0x0) {
7066     return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
7067   }
7068 
7069   return None;
7070 }
7071 
7072 Optional<RegImmPair> AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
7073                                                       Register Reg) const {
7074   int Sign = 1;
7075   int64_t Offset = 0;
7076 
7077   // TODO: Handle cases where Reg is a super- or sub-register of the
7078   // destination register.
7079   const MachineOperand &Op0 = MI.getOperand(0);
7080   if (!Op0.isReg() || Reg != Op0.getReg())
7081     return None;
7082 
7083   switch (MI.getOpcode()) {
7084   default:
7085     return None;
7086   case AArch64::SUBWri:
7087   case AArch64::SUBXri:
7088   case AArch64::SUBSWri:
7089   case AArch64::SUBSXri:
7090     Sign *= -1;
7091     LLVM_FALLTHROUGH;
7092   case AArch64::ADDSWri:
7093   case AArch64::ADDSXri:
7094   case AArch64::ADDWri:
7095   case AArch64::ADDXri: {
7096     // TODO: Third operand can be global address (usually some string).
7097     if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg() ||
7098         !MI.getOperand(2).isImm())
7099       return None;
7100     int Shift = MI.getOperand(3).getImm();
7101     assert((Shift == 0 || Shift == 12) && "Shift can be either 0 or 12");
7102     Offset = Sign * (MI.getOperand(2).getImm() << Shift);
7103   }
7104   }
7105   return RegImmPair{MI.getOperand(1).getReg(), Offset};
7106 }
7107 
7108 /// If the given ORR instruction is a copy, and \p DescribedReg overlaps with
7109 /// the destination register then, if possible, describe the value in terms of
7110 /// the source register.
7111 static Optional<ParamLoadedValue>
7112 describeORRLoadedValue(const MachineInstr &MI, Register DescribedReg,
7113                        const TargetInstrInfo *TII,
7114                        const TargetRegisterInfo *TRI) {
7115   auto DestSrc = TII->isCopyInstr(MI);
7116   if (!DestSrc)
7117     return None;
7118 
7119   Register DestReg = DestSrc->Destination->getReg();
7120   Register SrcReg = DestSrc->Source->getReg();
7121 
7122   auto Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {});
7123 
7124   // If the described register is the destination, just return the source.
7125   if (DestReg == DescribedReg)
7126     return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
7127 
7128   // ORRWrs zero-extends to 64-bits, so we need to consider such cases.
7129   if (MI.getOpcode() == AArch64::ORRWrs &&
7130       TRI->isSuperRegister(DestReg, DescribedReg))
7131     return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
7132 
7133   // We may need to describe the lower part of a ORRXrs move.
7134   if (MI.getOpcode() == AArch64::ORRXrs &&
7135       TRI->isSubRegister(DestReg, DescribedReg)) {
7136     Register SrcSubReg = TRI->getSubReg(SrcReg, AArch64::sub_32);
7137     return ParamLoadedValue(MachineOperand::CreateReg(SrcSubReg, false), Expr);
7138   }
7139 
7140   assert(!TRI->isSuperOrSubRegisterEq(DestReg, DescribedReg) &&
7141          "Unhandled ORR[XW]rs copy case");
7142 
7143   return None;
7144 }
7145 
7146 Optional<ParamLoadedValue>
7147 AArch64InstrInfo::describeLoadedValue(const MachineInstr &MI,
7148                                       Register Reg) const {
7149   const MachineFunction *MF = MI.getMF();
7150   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
7151   switch (MI.getOpcode()) {
7152   case AArch64::MOVZWi:
7153   case AArch64::MOVZXi: {
7154     // MOVZWi may be used for producing zero-extended 32-bit immediates in
7155     // 64-bit parameters, so we need to consider super-registers.
7156     if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
7157       return None;
7158 
7159     if (!MI.getOperand(1).isImm())
7160       return None;
7161     int64_t Immediate = MI.getOperand(1).getImm();
7162     int Shift = MI.getOperand(2).getImm();
7163     return ParamLoadedValue(MachineOperand::CreateImm(Immediate << Shift),
7164                             nullptr);
7165   }
7166   case AArch64::ORRWrs:
7167   case AArch64::ORRXrs:
7168     return describeORRLoadedValue(MI, Reg, this, TRI);
7169   }
7170 
7171   return TargetInstrInfo::describeLoadedValue(MI, Reg);
7172 }
7173 
7174 bool AArch64InstrInfo::isExtendLikelyToBeFolded(
7175     MachineInstr &ExtMI, MachineRegisterInfo &MRI) const {
7176   assert(ExtMI.getOpcode() == TargetOpcode::G_SEXT ||
7177          ExtMI.getOpcode() == TargetOpcode::G_ZEXT ||
7178          ExtMI.getOpcode() == TargetOpcode::G_ANYEXT);
7179 
7180   // Anyexts are nops.
7181   if (ExtMI.getOpcode() == TargetOpcode::G_ANYEXT)
7182     return true;
7183 
7184   Register DefReg = ExtMI.getOperand(0).getReg();
7185   if (!MRI.hasOneNonDBGUse(DefReg))
7186     return false;
7187 
7188   // It's likely that a sext/zext as a G_PTR_ADD offset will be folded into an
7189   // addressing mode.
7190   auto *UserMI = &*MRI.use_instr_nodbg_begin(DefReg);
7191   return UserMI->getOpcode() == TargetOpcode::G_PTR_ADD;
7192 }
7193 
7194 uint64_t AArch64InstrInfo::getElementSizeForOpcode(unsigned Opc) const {
7195   return get(Opc).TSFlags & AArch64::ElementSizeMask;
7196 }
7197 
7198 bool AArch64InstrInfo::isPTestLikeOpcode(unsigned Opc) const {
7199   return get(Opc).TSFlags & AArch64::InstrFlagIsPTestLike;
7200 }
7201 
7202 bool AArch64InstrInfo::isWhileOpcode(unsigned Opc) const {
7203   return get(Opc).TSFlags & AArch64::InstrFlagIsWhile;
7204 }
7205 
7206 unsigned int
7207 AArch64InstrInfo::getTailDuplicateSize(CodeGenOpt::Level OptLevel) const {
7208   return OptLevel >= CodeGenOpt::Aggressive ? 6 : 2;
7209 }
7210 
7211 unsigned llvm::getBLRCallOpcode(const MachineFunction &MF) {
7212   if (MF.getSubtarget<AArch64Subtarget>().hardenSlsBlr())
7213     return AArch64::BLRNoIP;
7214   else
7215     return AArch64::BLR;
7216 }
7217 
7218 #define GET_INSTRINFO_HELPERS
7219 #define GET_INSTRMAP_INFO
7220 #include "AArch64GenInstrInfo.inc"
7221