1 //===- AArch64InstrInfo.cpp - AArch64 Instruction Information -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the AArch64 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AArch64InstrInfo.h"
15 #include "AArch64MachineFunctionInfo.h"
16 #include "AArch64Subtarget.h"
17 #include "MCTargetDesc/AArch64AddressingModes.h"
18 #include "Utils/AArch64BaseInfo.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineMemOperand.h"
28 #include "llvm/CodeGen/MachineOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/StackMaps.h"
31 #include "llvm/IR/DebugLoc.h"
32 #include "llvm/IR/GlobalValue.h"
33 #include "llvm/MC/MCInst.h"
34 #include "llvm/MC/MCInstrDesc.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/CodeGen.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Compiler.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
43 #include "llvm/Target/TargetRegisterInfo.h"
44 #include "llvm/Target/TargetSubtargetInfo.h"
45 #include <cassert>
46 #include <cstdint>
47 #include <iterator>
48 #include <utility>
49 
50 using namespace llvm;
51 
52 #define GET_INSTRINFO_CTOR_DTOR
53 #include "AArch64GenInstrInfo.inc"
54 
55 static cl::opt<unsigned> TBZDisplacementBits(
56     "aarch64-tbz-offset-bits", cl::Hidden, cl::init(14),
57     cl::desc("Restrict range of TB[N]Z instructions (DEBUG)"));
58 
59 static cl::opt<unsigned> CBZDisplacementBits(
60     "aarch64-cbz-offset-bits", cl::Hidden, cl::init(19),
61     cl::desc("Restrict range of CB[N]Z instructions (DEBUG)"));
62 
63 static cl::opt<unsigned>
64     BCCDisplacementBits("aarch64-bcc-offset-bits", cl::Hidden, cl::init(19),
65                         cl::desc("Restrict range of Bcc instructions (DEBUG)"));
66 
67 AArch64InstrInfo::AArch64InstrInfo(const AArch64Subtarget &STI)
68     : AArch64GenInstrInfo(AArch64::ADJCALLSTACKDOWN, AArch64::ADJCALLSTACKUP),
69       RI(STI.getTargetTriple()), Subtarget(STI) {}
70 
71 /// GetInstSize - Return the number of bytes of code the specified
72 /// instruction may be.  This returns the maximum number of bytes.
73 unsigned AArch64InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
74   const MachineBasicBlock &MBB = *MI.getParent();
75   const MachineFunction *MF = MBB.getParent();
76   const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
77 
78   if (MI.getOpcode() == AArch64::INLINEASM)
79     return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
80 
81   // FIXME: We currently only handle pseudoinstructions that don't get expanded
82   //        before the assembly printer.
83   unsigned NumBytes = 0;
84   const MCInstrDesc &Desc = MI.getDesc();
85   switch (Desc.getOpcode()) {
86   default:
87     // Anything not explicitly designated otherwise is a normal 4-byte insn.
88     NumBytes = 4;
89     break;
90   case TargetOpcode::DBG_VALUE:
91   case TargetOpcode::EH_LABEL:
92   case TargetOpcode::IMPLICIT_DEF:
93   case TargetOpcode::KILL:
94     NumBytes = 0;
95     break;
96   case TargetOpcode::STACKMAP:
97     // The upper bound for a stackmap intrinsic is the full length of its shadow
98     NumBytes = StackMapOpers(&MI).getNumPatchBytes();
99     assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
100     break;
101   case TargetOpcode::PATCHPOINT:
102     // The size of the patchpoint intrinsic is the number of bytes requested
103     NumBytes = PatchPointOpers(&MI).getNumPatchBytes();
104     assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
105     break;
106   case AArch64::TLSDESC_CALLSEQ:
107     // This gets lowered to an instruction sequence which takes 16 bytes
108     NumBytes = 16;
109     break;
110   }
111 
112   return NumBytes;
113 }
114 
115 static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target,
116                             SmallVectorImpl<MachineOperand> &Cond) {
117   // Block ends with fall-through condbranch.
118   switch (LastInst->getOpcode()) {
119   default:
120     llvm_unreachable("Unknown branch instruction?");
121   case AArch64::Bcc:
122     Target = LastInst->getOperand(1).getMBB();
123     Cond.push_back(LastInst->getOperand(0));
124     break;
125   case AArch64::CBZW:
126   case AArch64::CBZX:
127   case AArch64::CBNZW:
128   case AArch64::CBNZX:
129     Target = LastInst->getOperand(1).getMBB();
130     Cond.push_back(MachineOperand::CreateImm(-1));
131     Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
132     Cond.push_back(LastInst->getOperand(0));
133     break;
134   case AArch64::TBZW:
135   case AArch64::TBZX:
136   case AArch64::TBNZW:
137   case AArch64::TBNZX:
138     Target = LastInst->getOperand(2).getMBB();
139     Cond.push_back(MachineOperand::CreateImm(-1));
140     Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
141     Cond.push_back(LastInst->getOperand(0));
142     Cond.push_back(LastInst->getOperand(1));
143   }
144 }
145 
146 static unsigned getBranchDisplacementBits(unsigned Opc) {
147   switch (Opc) {
148   default:
149     llvm_unreachable("unexpected opcode!");
150   case AArch64::B:
151     return 64;
152   case AArch64::TBNZW:
153   case AArch64::TBZW:
154   case AArch64::TBNZX:
155   case AArch64::TBZX:
156     return TBZDisplacementBits;
157   case AArch64::CBNZW:
158   case AArch64::CBZW:
159   case AArch64::CBNZX:
160   case AArch64::CBZX:
161     return CBZDisplacementBits;
162   case AArch64::Bcc:
163     return BCCDisplacementBits;
164   }
165 }
166 
167 bool AArch64InstrInfo::isBranchOffsetInRange(unsigned BranchOp,
168                                              int64_t BrOffset) const {
169   unsigned Bits = getBranchDisplacementBits(BranchOp);
170   assert(Bits >= 3 && "max branch displacement must be enough to jump"
171                       "over conditional branch expansion");
172   return isIntN(Bits, BrOffset / 4);
173 }
174 
175 MachineBasicBlock *
176 AArch64InstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
177   switch (MI.getOpcode()) {
178   default:
179     llvm_unreachable("unexpected opcode!");
180   case AArch64::B:
181     return MI.getOperand(0).getMBB();
182   case AArch64::TBZW:
183   case AArch64::TBNZW:
184   case AArch64::TBZX:
185   case AArch64::TBNZX:
186     return MI.getOperand(2).getMBB();
187   case AArch64::CBZW:
188   case AArch64::CBNZW:
189   case AArch64::CBZX:
190   case AArch64::CBNZX:
191   case AArch64::Bcc:
192     return MI.getOperand(1).getMBB();
193   }
194 }
195 
196 // Branch analysis.
197 bool AArch64InstrInfo::analyzeBranch(MachineBasicBlock &MBB,
198                                      MachineBasicBlock *&TBB,
199                                      MachineBasicBlock *&FBB,
200                                      SmallVectorImpl<MachineOperand> &Cond,
201                                      bool AllowModify) const {
202   // If the block has no terminators, it just falls into the block after it.
203   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
204   if (I == MBB.end())
205     return false;
206 
207   if (!isUnpredicatedTerminator(*I))
208     return false;
209 
210   // Get the last instruction in the block.
211   MachineInstr *LastInst = &*I;
212 
213   // If there is only one terminator instruction, process it.
214   unsigned LastOpc = LastInst->getOpcode();
215   if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
216     if (isUncondBranchOpcode(LastOpc)) {
217       TBB = LastInst->getOperand(0).getMBB();
218       return false;
219     }
220     if (isCondBranchOpcode(LastOpc)) {
221       // Block ends with fall-through condbranch.
222       parseCondBranch(LastInst, TBB, Cond);
223       return false;
224     }
225     return true; // Can't handle indirect branch.
226   }
227 
228   // Get the instruction before it if it is a terminator.
229   MachineInstr *SecondLastInst = &*I;
230   unsigned SecondLastOpc = SecondLastInst->getOpcode();
231 
232   // If AllowModify is true and the block ends with two or more unconditional
233   // branches, delete all but the first unconditional branch.
234   if (AllowModify && isUncondBranchOpcode(LastOpc)) {
235     while (isUncondBranchOpcode(SecondLastOpc)) {
236       LastInst->eraseFromParent();
237       LastInst = SecondLastInst;
238       LastOpc = LastInst->getOpcode();
239       if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
240         // Return now the only terminator is an unconditional branch.
241         TBB = LastInst->getOperand(0).getMBB();
242         return false;
243       } else {
244         SecondLastInst = &*I;
245         SecondLastOpc = SecondLastInst->getOpcode();
246       }
247     }
248   }
249 
250   // If there are three terminators, we don't know what sort of block this is.
251   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I))
252     return true;
253 
254   // If the block ends with a B and a Bcc, handle it.
255   if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
256     parseCondBranch(SecondLastInst, TBB, Cond);
257     FBB = LastInst->getOperand(0).getMBB();
258     return false;
259   }
260 
261   // If the block ends with two unconditional branches, handle it.  The second
262   // one is not executed, so remove it.
263   if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
264     TBB = SecondLastInst->getOperand(0).getMBB();
265     I = LastInst;
266     if (AllowModify)
267       I->eraseFromParent();
268     return false;
269   }
270 
271   // ...likewise if it ends with an indirect branch followed by an unconditional
272   // branch.
273   if (isIndirectBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
274     I = LastInst;
275     if (AllowModify)
276       I->eraseFromParent();
277     return true;
278   }
279 
280   // Otherwise, can't handle this.
281   return true;
282 }
283 
284 bool AArch64InstrInfo::reverseBranchCondition(
285     SmallVectorImpl<MachineOperand> &Cond) const {
286   if (Cond[0].getImm() != -1) {
287     // Regular Bcc
288     AArch64CC::CondCode CC = (AArch64CC::CondCode)(int)Cond[0].getImm();
289     Cond[0].setImm(AArch64CC::getInvertedCondCode(CC));
290   } else {
291     // Folded compare-and-branch
292     switch (Cond[1].getImm()) {
293     default:
294       llvm_unreachable("Unknown conditional branch!");
295     case AArch64::CBZW:
296       Cond[1].setImm(AArch64::CBNZW);
297       break;
298     case AArch64::CBNZW:
299       Cond[1].setImm(AArch64::CBZW);
300       break;
301     case AArch64::CBZX:
302       Cond[1].setImm(AArch64::CBNZX);
303       break;
304     case AArch64::CBNZX:
305       Cond[1].setImm(AArch64::CBZX);
306       break;
307     case AArch64::TBZW:
308       Cond[1].setImm(AArch64::TBNZW);
309       break;
310     case AArch64::TBNZW:
311       Cond[1].setImm(AArch64::TBZW);
312       break;
313     case AArch64::TBZX:
314       Cond[1].setImm(AArch64::TBNZX);
315       break;
316     case AArch64::TBNZX:
317       Cond[1].setImm(AArch64::TBZX);
318       break;
319     }
320   }
321 
322   return false;
323 }
324 
325 unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &MBB,
326                                         int *BytesRemoved) const {
327   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
328   if (I == MBB.end())
329     return 0;
330 
331   if (!isUncondBranchOpcode(I->getOpcode()) &&
332       !isCondBranchOpcode(I->getOpcode()))
333     return 0;
334 
335   // Remove the branch.
336   I->eraseFromParent();
337 
338   I = MBB.end();
339 
340   if (I == MBB.begin()) {
341     if (BytesRemoved)
342       *BytesRemoved = 4;
343     return 1;
344   }
345   --I;
346   if (!isCondBranchOpcode(I->getOpcode())) {
347     if (BytesRemoved)
348       *BytesRemoved = 4;
349     return 1;
350   }
351 
352   // Remove the branch.
353   I->eraseFromParent();
354   if (BytesRemoved)
355     *BytesRemoved = 8;
356 
357   return 2;
358 }
359 
360 void AArch64InstrInfo::instantiateCondBranch(
361     MachineBasicBlock &MBB, const DebugLoc &DL, MachineBasicBlock *TBB,
362     ArrayRef<MachineOperand> Cond) const {
363   if (Cond[0].getImm() != -1) {
364     // Regular Bcc
365     BuildMI(&MBB, DL, get(AArch64::Bcc)).addImm(Cond[0].getImm()).addMBB(TBB);
366   } else {
367     // Folded compare-and-branch
368     // Note that we use addOperand instead of addReg to keep the flags.
369     const MachineInstrBuilder MIB =
370         BuildMI(&MBB, DL, get(Cond[1].getImm())).add(Cond[2]);
371     if (Cond.size() > 3)
372       MIB.addImm(Cond[3].getImm());
373     MIB.addMBB(TBB);
374   }
375 }
376 
377 unsigned AArch64InstrInfo::insertBranch(
378     MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
379     ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
380   // Shouldn't be a fall through.
381   assert(TBB && "insertBranch must not be told to insert a fallthrough");
382 
383   if (!FBB) {
384     if (Cond.empty()) // Unconditional branch?
385       BuildMI(&MBB, DL, get(AArch64::B)).addMBB(TBB);
386     else
387       instantiateCondBranch(MBB, DL, TBB, Cond);
388 
389     if (BytesAdded)
390       *BytesAdded = 4;
391 
392     return 1;
393   }
394 
395   // Two-way conditional branch.
396   instantiateCondBranch(MBB, DL, TBB, Cond);
397   BuildMI(&MBB, DL, get(AArch64::B)).addMBB(FBB);
398 
399   if (BytesAdded)
400     *BytesAdded = 8;
401 
402   return 2;
403 }
404 
405 // Find the original register that VReg is copied from.
406 static unsigned removeCopies(const MachineRegisterInfo &MRI, unsigned VReg) {
407   while (TargetRegisterInfo::isVirtualRegister(VReg)) {
408     const MachineInstr *DefMI = MRI.getVRegDef(VReg);
409     if (!DefMI->isFullCopy())
410       return VReg;
411     VReg = DefMI->getOperand(1).getReg();
412   }
413   return VReg;
414 }
415 
416 // Determine if VReg is defined by an instruction that can be folded into a
417 // csel instruction. If so, return the folded opcode, and the replacement
418 // register.
419 static unsigned canFoldIntoCSel(const MachineRegisterInfo &MRI, unsigned VReg,
420                                 unsigned *NewVReg = nullptr) {
421   VReg = removeCopies(MRI, VReg);
422   if (!TargetRegisterInfo::isVirtualRegister(VReg))
423     return 0;
424 
425   bool Is64Bit = AArch64::GPR64allRegClass.hasSubClassEq(MRI.getRegClass(VReg));
426   const MachineInstr *DefMI = MRI.getVRegDef(VReg);
427   unsigned Opc = 0;
428   unsigned SrcOpNum = 0;
429   switch (DefMI->getOpcode()) {
430   case AArch64::ADDSXri:
431   case AArch64::ADDSWri:
432     // if NZCV is used, do not fold.
433     if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
434       return 0;
435     // fall-through to ADDXri and ADDWri.
436     LLVM_FALLTHROUGH;
437   case AArch64::ADDXri:
438   case AArch64::ADDWri:
439     // add x, 1 -> csinc.
440     if (!DefMI->getOperand(2).isImm() || DefMI->getOperand(2).getImm() != 1 ||
441         DefMI->getOperand(3).getImm() != 0)
442       return 0;
443     SrcOpNum = 1;
444     Opc = Is64Bit ? AArch64::CSINCXr : AArch64::CSINCWr;
445     break;
446 
447   case AArch64::ORNXrr:
448   case AArch64::ORNWrr: {
449     // not x -> csinv, represented as orn dst, xzr, src.
450     unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
451     if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
452       return 0;
453     SrcOpNum = 2;
454     Opc = Is64Bit ? AArch64::CSINVXr : AArch64::CSINVWr;
455     break;
456   }
457 
458   case AArch64::SUBSXrr:
459   case AArch64::SUBSWrr:
460     // if NZCV is used, do not fold.
461     if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
462       return 0;
463     // fall-through to SUBXrr and SUBWrr.
464     LLVM_FALLTHROUGH;
465   case AArch64::SUBXrr:
466   case AArch64::SUBWrr: {
467     // neg x -> csneg, represented as sub dst, xzr, src.
468     unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
469     if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
470       return 0;
471     SrcOpNum = 2;
472     Opc = Is64Bit ? AArch64::CSNEGXr : AArch64::CSNEGWr;
473     break;
474   }
475   default:
476     return 0;
477   }
478   assert(Opc && SrcOpNum && "Missing parameters");
479 
480   if (NewVReg)
481     *NewVReg = DefMI->getOperand(SrcOpNum).getReg();
482   return Opc;
483 }
484 
485 bool AArch64InstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
486                                        ArrayRef<MachineOperand> Cond,
487                                        unsigned TrueReg, unsigned FalseReg,
488                                        int &CondCycles, int &TrueCycles,
489                                        int &FalseCycles) const {
490   // Check register classes.
491   const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
492   const TargetRegisterClass *RC =
493       RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
494   if (!RC)
495     return false;
496 
497   // Expanding cbz/tbz requires an extra cycle of latency on the condition.
498   unsigned ExtraCondLat = Cond.size() != 1;
499 
500   // GPRs are handled by csel.
501   // FIXME: Fold in x+1, -x, and ~x when applicable.
502   if (AArch64::GPR64allRegClass.hasSubClassEq(RC) ||
503       AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
504     // Single-cycle csel, csinc, csinv, and csneg.
505     CondCycles = 1 + ExtraCondLat;
506     TrueCycles = FalseCycles = 1;
507     if (canFoldIntoCSel(MRI, TrueReg))
508       TrueCycles = 0;
509     else if (canFoldIntoCSel(MRI, FalseReg))
510       FalseCycles = 0;
511     return true;
512   }
513 
514   // Scalar floating point is handled by fcsel.
515   // FIXME: Form fabs, fmin, and fmax when applicable.
516   if (AArch64::FPR64RegClass.hasSubClassEq(RC) ||
517       AArch64::FPR32RegClass.hasSubClassEq(RC)) {
518     CondCycles = 5 + ExtraCondLat;
519     TrueCycles = FalseCycles = 2;
520     return true;
521   }
522 
523   // Can't do vectors.
524   return false;
525 }
526 
527 void AArch64InstrInfo::insertSelect(MachineBasicBlock &MBB,
528                                     MachineBasicBlock::iterator I,
529                                     const DebugLoc &DL, unsigned DstReg,
530                                     ArrayRef<MachineOperand> Cond,
531                                     unsigned TrueReg, unsigned FalseReg) const {
532   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
533 
534   // Parse the condition code, see parseCondBranch() above.
535   AArch64CC::CondCode CC;
536   switch (Cond.size()) {
537   default:
538     llvm_unreachable("Unknown condition opcode in Cond");
539   case 1: // b.cc
540     CC = AArch64CC::CondCode(Cond[0].getImm());
541     break;
542   case 3: { // cbz/cbnz
543     // We must insert a compare against 0.
544     bool Is64Bit;
545     switch (Cond[1].getImm()) {
546     default:
547       llvm_unreachable("Unknown branch opcode in Cond");
548     case AArch64::CBZW:
549       Is64Bit = false;
550       CC = AArch64CC::EQ;
551       break;
552     case AArch64::CBZX:
553       Is64Bit = true;
554       CC = AArch64CC::EQ;
555       break;
556     case AArch64::CBNZW:
557       Is64Bit = false;
558       CC = AArch64CC::NE;
559       break;
560     case AArch64::CBNZX:
561       Is64Bit = true;
562       CC = AArch64CC::NE;
563       break;
564     }
565     unsigned SrcReg = Cond[2].getReg();
566     if (Is64Bit) {
567       // cmp reg, #0 is actually subs xzr, reg, #0.
568       MRI.constrainRegClass(SrcReg, &AArch64::GPR64spRegClass);
569       BuildMI(MBB, I, DL, get(AArch64::SUBSXri), AArch64::XZR)
570           .addReg(SrcReg)
571           .addImm(0)
572           .addImm(0);
573     } else {
574       MRI.constrainRegClass(SrcReg, &AArch64::GPR32spRegClass);
575       BuildMI(MBB, I, DL, get(AArch64::SUBSWri), AArch64::WZR)
576           .addReg(SrcReg)
577           .addImm(0)
578           .addImm(0);
579     }
580     break;
581   }
582   case 4: { // tbz/tbnz
583     // We must insert a tst instruction.
584     switch (Cond[1].getImm()) {
585     default:
586       llvm_unreachable("Unknown branch opcode in Cond");
587     case AArch64::TBZW:
588     case AArch64::TBZX:
589       CC = AArch64CC::EQ;
590       break;
591     case AArch64::TBNZW:
592     case AArch64::TBNZX:
593       CC = AArch64CC::NE;
594       break;
595     }
596     // cmp reg, #foo is actually ands xzr, reg, #1<<foo.
597     if (Cond[1].getImm() == AArch64::TBZW || Cond[1].getImm() == AArch64::TBNZW)
598       BuildMI(MBB, I, DL, get(AArch64::ANDSWri), AArch64::WZR)
599           .addReg(Cond[2].getReg())
600           .addImm(
601               AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 32));
602     else
603       BuildMI(MBB, I, DL, get(AArch64::ANDSXri), AArch64::XZR)
604           .addReg(Cond[2].getReg())
605           .addImm(
606               AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 64));
607     break;
608   }
609   }
610 
611   unsigned Opc = 0;
612   const TargetRegisterClass *RC = nullptr;
613   bool TryFold = false;
614   if (MRI.constrainRegClass(DstReg, &AArch64::GPR64RegClass)) {
615     RC = &AArch64::GPR64RegClass;
616     Opc = AArch64::CSELXr;
617     TryFold = true;
618   } else if (MRI.constrainRegClass(DstReg, &AArch64::GPR32RegClass)) {
619     RC = &AArch64::GPR32RegClass;
620     Opc = AArch64::CSELWr;
621     TryFold = true;
622   } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR64RegClass)) {
623     RC = &AArch64::FPR64RegClass;
624     Opc = AArch64::FCSELDrrr;
625   } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR32RegClass)) {
626     RC = &AArch64::FPR32RegClass;
627     Opc = AArch64::FCSELSrrr;
628   }
629   assert(RC && "Unsupported regclass");
630 
631   // Try folding simple instructions into the csel.
632   if (TryFold) {
633     unsigned NewVReg = 0;
634     unsigned FoldedOpc = canFoldIntoCSel(MRI, TrueReg, &NewVReg);
635     if (FoldedOpc) {
636       // The folded opcodes csinc, csinc and csneg apply the operation to
637       // FalseReg, so we need to invert the condition.
638       CC = AArch64CC::getInvertedCondCode(CC);
639       TrueReg = FalseReg;
640     } else
641       FoldedOpc = canFoldIntoCSel(MRI, FalseReg, &NewVReg);
642 
643     // Fold the operation. Leave any dead instructions for DCE to clean up.
644     if (FoldedOpc) {
645       FalseReg = NewVReg;
646       Opc = FoldedOpc;
647       // The extends the live range of NewVReg.
648       MRI.clearKillFlags(NewVReg);
649     }
650   }
651 
652   // Pull all virtual register into the appropriate class.
653   MRI.constrainRegClass(TrueReg, RC);
654   MRI.constrainRegClass(FalseReg, RC);
655 
656   // Insert the csel.
657   BuildMI(MBB, I, DL, get(Opc), DstReg)
658       .addReg(TrueReg)
659       .addReg(FalseReg)
660       .addImm(CC);
661 }
662 
663 /// Returns true if a MOVi32imm or MOVi64imm can be expanded to an  ORRxx.
664 static bool canBeExpandedToORR(const MachineInstr &MI, unsigned BitSize) {
665   uint64_t Imm = MI.getOperand(1).getImm();
666   uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
667   uint64_t Encoding;
668   return AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding);
669 }
670 
671 // FIXME: this implementation should be micro-architecture dependent, so a
672 // micro-architecture target hook should be introduced here in future.
673 bool AArch64InstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
674   if (!Subtarget.hasCustomCheapAsMoveHandling())
675     return MI.isAsCheapAsAMove();
676 
677   unsigned Imm;
678 
679   switch (MI.getOpcode()) {
680   default:
681     return false;
682 
683   // add/sub on register without shift
684   case AArch64::ADDWri:
685   case AArch64::ADDXri:
686   case AArch64::SUBWri:
687   case AArch64::SUBXri:
688     return (Subtarget.getProcFamily() == AArch64Subtarget::ExynosM1 ||
689             MI.getOperand(3).getImm() == 0);
690 
691   // add/sub on register with shift
692   case AArch64::ADDWrs:
693   case AArch64::ADDXrs:
694   case AArch64::SUBWrs:
695   case AArch64::SUBXrs:
696     Imm = MI.getOperand(3).getImm();
697     return (Subtarget.getProcFamily() == AArch64Subtarget::ExynosM1 &&
698             AArch64_AM::getArithShiftValue(Imm) < 4);
699 
700   // logical ops on immediate
701   case AArch64::ANDWri:
702   case AArch64::ANDXri:
703   case AArch64::EORWri:
704   case AArch64::EORXri:
705   case AArch64::ORRWri:
706   case AArch64::ORRXri:
707     return true;
708 
709   // logical ops on register without shift
710   case AArch64::ANDWrr:
711   case AArch64::ANDXrr:
712   case AArch64::BICWrr:
713   case AArch64::BICXrr:
714   case AArch64::EONWrr:
715   case AArch64::EONXrr:
716   case AArch64::EORWrr:
717   case AArch64::EORXrr:
718   case AArch64::ORNWrr:
719   case AArch64::ORNXrr:
720   case AArch64::ORRWrr:
721   case AArch64::ORRXrr:
722     return true;
723 
724   // logical ops on register with shift
725   case AArch64::ANDWrs:
726   case AArch64::ANDXrs:
727   case AArch64::BICWrs:
728   case AArch64::BICXrs:
729   case AArch64::EONWrs:
730   case AArch64::EONXrs:
731   case AArch64::EORWrs:
732   case AArch64::EORXrs:
733   case AArch64::ORNWrs:
734   case AArch64::ORNXrs:
735   case AArch64::ORRWrs:
736   case AArch64::ORRXrs:
737     Imm = MI.getOperand(3).getImm();
738     return (Subtarget.getProcFamily() == AArch64Subtarget::ExynosM1 &&
739             AArch64_AM::getShiftValue(Imm) < 4 &&
740             AArch64_AM::getShiftType(Imm) == AArch64_AM::LSL);
741 
742   // If MOVi32imm or MOVi64imm can be expanded into ORRWri or
743   // ORRXri, it is as cheap as MOV
744   case AArch64::MOVi32imm:
745     return canBeExpandedToORR(MI, 32);
746   case AArch64::MOVi64imm:
747     return canBeExpandedToORR(MI, 64);
748 
749   // It is cheap to zero out registers if the subtarget has ZeroCycleZeroing
750   // feature.
751   case AArch64::FMOVS0:
752   case AArch64::FMOVD0:
753     return Subtarget.hasZeroCycleZeroing();
754   case TargetOpcode::COPY:
755     return (Subtarget.hasZeroCycleZeroing() &&
756             (MI.getOperand(1).getReg() == AArch64::WZR ||
757              MI.getOperand(1).getReg() == AArch64::XZR));
758   }
759 
760   llvm_unreachable("Unknown opcode to check as cheap as a move!");
761 }
762 
763 bool AArch64InstrInfo::isFalkorShiftExtFast(const MachineInstr &MI) const {
764   switch (MI.getOpcode()) {
765   default:
766     return false;
767 
768   case AArch64::ADDWrs:
769   case AArch64::ADDXrs:
770   case AArch64::ADDSWrs:
771   case AArch64::ADDSXrs: {
772     unsigned Imm = MI.getOperand(3).getImm();
773     unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
774     if (ShiftVal == 0)
775       return true;
776     return AArch64_AM::getShiftType(Imm) == AArch64_AM::LSL && ShiftVal <= 5;
777   }
778 
779   case AArch64::ADDWrx:
780   case AArch64::ADDXrx:
781   case AArch64::ADDXrx64:
782   case AArch64::ADDSWrx:
783   case AArch64::ADDSXrx:
784   case AArch64::ADDSXrx64: {
785     unsigned Imm = MI.getOperand(3).getImm();
786     switch (AArch64_AM::getArithExtendType(Imm)) {
787     default:
788       return false;
789     case AArch64_AM::UXTB:
790     case AArch64_AM::UXTH:
791     case AArch64_AM::UXTW:
792     case AArch64_AM::UXTX:
793       return AArch64_AM::getArithShiftValue(Imm) <= 4;
794     }
795   }
796 
797   case AArch64::SUBWrs:
798   case AArch64::SUBSWrs: {
799     unsigned Imm = MI.getOperand(3).getImm();
800     unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
801     return ShiftVal == 0 ||
802            (AArch64_AM::getShiftType(Imm) == AArch64_AM::ASR && ShiftVal == 31);
803   }
804 
805   case AArch64::SUBXrs:
806   case AArch64::SUBSXrs: {
807     unsigned Imm = MI.getOperand(3).getImm();
808     unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
809     return ShiftVal == 0 ||
810            (AArch64_AM::getShiftType(Imm) == AArch64_AM::ASR && ShiftVal == 63);
811   }
812 
813   case AArch64::SUBWrx:
814   case AArch64::SUBXrx:
815   case AArch64::SUBXrx64:
816   case AArch64::SUBSWrx:
817   case AArch64::SUBSXrx:
818   case AArch64::SUBSXrx64: {
819     unsigned Imm = MI.getOperand(3).getImm();
820     switch (AArch64_AM::getArithExtendType(Imm)) {
821     default:
822       return false;
823     case AArch64_AM::UXTB:
824     case AArch64_AM::UXTH:
825     case AArch64_AM::UXTW:
826     case AArch64_AM::UXTX:
827       return AArch64_AM::getArithShiftValue(Imm) == 0;
828     }
829   }
830 
831   case AArch64::LDRBBroW:
832   case AArch64::LDRBBroX:
833   case AArch64::LDRBroW:
834   case AArch64::LDRBroX:
835   case AArch64::LDRDroW:
836   case AArch64::LDRDroX:
837   case AArch64::LDRHHroW:
838   case AArch64::LDRHHroX:
839   case AArch64::LDRHroW:
840   case AArch64::LDRHroX:
841   case AArch64::LDRQroW:
842   case AArch64::LDRQroX:
843   case AArch64::LDRSBWroW:
844   case AArch64::LDRSBWroX:
845   case AArch64::LDRSBXroW:
846   case AArch64::LDRSBXroX:
847   case AArch64::LDRSHWroW:
848   case AArch64::LDRSHWroX:
849   case AArch64::LDRSHXroW:
850   case AArch64::LDRSHXroX:
851   case AArch64::LDRSWroW:
852   case AArch64::LDRSWroX:
853   case AArch64::LDRSroW:
854   case AArch64::LDRSroX:
855   case AArch64::LDRWroW:
856   case AArch64::LDRWroX:
857   case AArch64::LDRXroW:
858   case AArch64::LDRXroX:
859   case AArch64::PRFMroW:
860   case AArch64::PRFMroX:
861   case AArch64::STRBBroW:
862   case AArch64::STRBBroX:
863   case AArch64::STRBroW:
864   case AArch64::STRBroX:
865   case AArch64::STRDroW:
866   case AArch64::STRDroX:
867   case AArch64::STRHHroW:
868   case AArch64::STRHHroX:
869   case AArch64::STRHroW:
870   case AArch64::STRHroX:
871   case AArch64::STRQroW:
872   case AArch64::STRQroX:
873   case AArch64::STRSroW:
874   case AArch64::STRSroX:
875   case AArch64::STRWroW:
876   case AArch64::STRWroX:
877   case AArch64::STRXroW:
878   case AArch64::STRXroX: {
879     unsigned IsSigned = MI.getOperand(3).getImm();
880     return !IsSigned;
881   }
882   }
883 }
884 
885 bool AArch64InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
886                                              unsigned &SrcReg, unsigned &DstReg,
887                                              unsigned &SubIdx) const {
888   switch (MI.getOpcode()) {
889   default:
890     return false;
891   case AArch64::SBFMXri: // aka sxtw
892   case AArch64::UBFMXri: // aka uxtw
893     // Check for the 32 -> 64 bit extension case, these instructions can do
894     // much more.
895     if (MI.getOperand(2).getImm() != 0 || MI.getOperand(3).getImm() != 31)
896       return false;
897     // This is a signed or unsigned 32 -> 64 bit extension.
898     SrcReg = MI.getOperand(1).getReg();
899     DstReg = MI.getOperand(0).getReg();
900     SubIdx = AArch64::sub_32;
901     return true;
902   }
903 }
904 
905 bool AArch64InstrInfo::areMemAccessesTriviallyDisjoint(
906     MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
907   const TargetRegisterInfo *TRI = &getRegisterInfo();
908   unsigned BaseRegA = 0, BaseRegB = 0;
909   int64_t OffsetA = 0, OffsetB = 0;
910   unsigned WidthA = 0, WidthB = 0;
911 
912   assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
913   assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
914 
915   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
916       MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
917     return false;
918 
919   // Retrieve the base register, offset from the base register and width. Width
920   // is the size of memory that is being loaded/stored (e.g. 1, 2, 4, 8).  If
921   // base registers are identical, and the offset of a lower memory access +
922   // the width doesn't overlap the offset of a higher memory access,
923   // then the memory accesses are different.
924   if (getMemOpBaseRegImmOfsWidth(MIa, BaseRegA, OffsetA, WidthA, TRI) &&
925       getMemOpBaseRegImmOfsWidth(MIb, BaseRegB, OffsetB, WidthB, TRI)) {
926     if (BaseRegA == BaseRegB) {
927       int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
928       int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
929       int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
930       if (LowOffset + LowWidth <= HighOffset)
931         return true;
932     }
933   }
934   return false;
935 }
936 
937 /// analyzeCompare - For a comparison instruction, return the source registers
938 /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
939 /// Return true if the comparison instruction can be analyzed.
940 bool AArch64InstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
941                                       unsigned &SrcReg2, int &CmpMask,
942                                       int &CmpValue) const {
943   switch (MI.getOpcode()) {
944   default:
945     break;
946   case AArch64::SUBSWrr:
947   case AArch64::SUBSWrs:
948   case AArch64::SUBSWrx:
949   case AArch64::SUBSXrr:
950   case AArch64::SUBSXrs:
951   case AArch64::SUBSXrx:
952   case AArch64::ADDSWrr:
953   case AArch64::ADDSWrs:
954   case AArch64::ADDSWrx:
955   case AArch64::ADDSXrr:
956   case AArch64::ADDSXrs:
957   case AArch64::ADDSXrx:
958     // Replace SUBSWrr with SUBWrr if NZCV is not used.
959     SrcReg = MI.getOperand(1).getReg();
960     SrcReg2 = MI.getOperand(2).getReg();
961     CmpMask = ~0;
962     CmpValue = 0;
963     return true;
964   case AArch64::SUBSWri:
965   case AArch64::ADDSWri:
966   case AArch64::SUBSXri:
967   case AArch64::ADDSXri:
968     SrcReg = MI.getOperand(1).getReg();
969     SrcReg2 = 0;
970     CmpMask = ~0;
971     // FIXME: In order to convert CmpValue to 0 or 1
972     CmpValue = MI.getOperand(2).getImm() != 0;
973     return true;
974   case AArch64::ANDSWri:
975   case AArch64::ANDSXri:
976     // ANDS does not use the same encoding scheme as the others xxxS
977     // instructions.
978     SrcReg = MI.getOperand(1).getReg();
979     SrcReg2 = 0;
980     CmpMask = ~0;
981     // FIXME:The return val type of decodeLogicalImmediate is uint64_t,
982     // while the type of CmpValue is int. When converting uint64_t to int,
983     // the high 32 bits of uint64_t will be lost.
984     // In fact it causes a bug in spec2006-483.xalancbmk
985     // CmpValue is only used to compare with zero in OptimizeCompareInstr
986     CmpValue = AArch64_AM::decodeLogicalImmediate(
987                    MI.getOperand(2).getImm(),
988                    MI.getOpcode() == AArch64::ANDSWri ? 32 : 64) != 0;
989     return true;
990   }
991 
992   return false;
993 }
994 
995 static bool UpdateOperandRegClass(MachineInstr &Instr) {
996   MachineBasicBlock *MBB = Instr.getParent();
997   assert(MBB && "Can't get MachineBasicBlock here");
998   MachineFunction *MF = MBB->getParent();
999   assert(MF && "Can't get MachineFunction here");
1000   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1001   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
1002   MachineRegisterInfo *MRI = &MF->getRegInfo();
1003 
1004   for (unsigned OpIdx = 0, EndIdx = Instr.getNumOperands(); OpIdx < EndIdx;
1005        ++OpIdx) {
1006     MachineOperand &MO = Instr.getOperand(OpIdx);
1007     const TargetRegisterClass *OpRegCstraints =
1008         Instr.getRegClassConstraint(OpIdx, TII, TRI);
1009 
1010     // If there's no constraint, there's nothing to do.
1011     if (!OpRegCstraints)
1012       continue;
1013     // If the operand is a frame index, there's nothing to do here.
1014     // A frame index operand will resolve correctly during PEI.
1015     if (MO.isFI())
1016       continue;
1017 
1018     assert(MO.isReg() &&
1019            "Operand has register constraints without being a register!");
1020 
1021     unsigned Reg = MO.getReg();
1022     if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1023       if (!OpRegCstraints->contains(Reg))
1024         return false;
1025     } else if (!OpRegCstraints->hasSubClassEq(MRI->getRegClass(Reg)) &&
1026                !MRI->constrainRegClass(Reg, OpRegCstraints))
1027       return false;
1028   }
1029 
1030   return true;
1031 }
1032 
1033 /// \brief Return the opcode that does not set flags when possible - otherwise
1034 /// return the original opcode. The caller is responsible to do the actual
1035 /// substitution and legality checking.
1036 static unsigned convertToNonFlagSettingOpc(const MachineInstr &MI) {
1037   // Don't convert all compare instructions, because for some the zero register
1038   // encoding becomes the sp register.
1039   bool MIDefinesZeroReg = false;
1040   if (MI.definesRegister(AArch64::WZR) || MI.definesRegister(AArch64::XZR))
1041     MIDefinesZeroReg = true;
1042 
1043   switch (MI.getOpcode()) {
1044   default:
1045     return MI.getOpcode();
1046   case AArch64::ADDSWrr:
1047     return AArch64::ADDWrr;
1048   case AArch64::ADDSWri:
1049     return MIDefinesZeroReg ? AArch64::ADDSWri : AArch64::ADDWri;
1050   case AArch64::ADDSWrs:
1051     return MIDefinesZeroReg ? AArch64::ADDSWrs : AArch64::ADDWrs;
1052   case AArch64::ADDSWrx:
1053     return AArch64::ADDWrx;
1054   case AArch64::ADDSXrr:
1055     return AArch64::ADDXrr;
1056   case AArch64::ADDSXri:
1057     return MIDefinesZeroReg ? AArch64::ADDSXri : AArch64::ADDXri;
1058   case AArch64::ADDSXrs:
1059     return MIDefinesZeroReg ? AArch64::ADDSXrs : AArch64::ADDXrs;
1060   case AArch64::ADDSXrx:
1061     return AArch64::ADDXrx;
1062   case AArch64::SUBSWrr:
1063     return AArch64::SUBWrr;
1064   case AArch64::SUBSWri:
1065     return MIDefinesZeroReg ? AArch64::SUBSWri : AArch64::SUBWri;
1066   case AArch64::SUBSWrs:
1067     return MIDefinesZeroReg ? AArch64::SUBSWrs : AArch64::SUBWrs;
1068   case AArch64::SUBSWrx:
1069     return AArch64::SUBWrx;
1070   case AArch64::SUBSXrr:
1071     return AArch64::SUBXrr;
1072   case AArch64::SUBSXri:
1073     return MIDefinesZeroReg ? AArch64::SUBSXri : AArch64::SUBXri;
1074   case AArch64::SUBSXrs:
1075     return MIDefinesZeroReg ? AArch64::SUBSXrs : AArch64::SUBXrs;
1076   case AArch64::SUBSXrx:
1077     return AArch64::SUBXrx;
1078   }
1079 }
1080 
1081 enum AccessKind { AK_Write = 0x01, AK_Read = 0x10, AK_All = 0x11 };
1082 
1083 /// True when condition flags are accessed (either by writing or reading)
1084 /// on the instruction trace starting at From and ending at To.
1085 ///
1086 /// Note: If From and To are from different blocks it's assumed CC are accessed
1087 ///       on the path.
1088 static bool areCFlagsAccessedBetweenInstrs(
1089     MachineBasicBlock::iterator From, MachineBasicBlock::iterator To,
1090     const TargetRegisterInfo *TRI, const AccessKind AccessToCheck = AK_All) {
1091   // Early exit if To is at the beginning of the BB.
1092   if (To == To->getParent()->begin())
1093     return true;
1094 
1095   // Check whether the instructions are in the same basic block
1096   // If not, assume the condition flags might get modified somewhere.
1097   if (To->getParent() != From->getParent())
1098     return true;
1099 
1100   // From must be above To.
1101   assert(std::find_if(++To.getReverse(), To->getParent()->rend(),
1102                       [From](MachineInstr &MI) {
1103                         return MI.getIterator() == From;
1104                       }) != To->getParent()->rend());
1105 
1106   // We iterate backward starting \p To until we hit \p From.
1107   for (--To; To != From; --To) {
1108     const MachineInstr &Instr = *To;
1109 
1110     if (((AccessToCheck & AK_Write) &&
1111          Instr.modifiesRegister(AArch64::NZCV, TRI)) ||
1112         ((AccessToCheck & AK_Read) && Instr.readsRegister(AArch64::NZCV, TRI)))
1113       return true;
1114   }
1115   return false;
1116 }
1117 
1118 /// Try to optimize a compare instruction. A compare instruction is an
1119 /// instruction which produces AArch64::NZCV. It can be truly compare
1120 /// instruction
1121 /// when there are no uses of its destination register.
1122 ///
1123 /// The following steps are tried in order:
1124 /// 1. Convert CmpInstr into an unconditional version.
1125 /// 2. Remove CmpInstr if above there is an instruction producing a needed
1126 ///    condition code or an instruction which can be converted into such an
1127 ///    instruction.
1128 ///    Only comparison with zero is supported.
1129 bool AArch64InstrInfo::optimizeCompareInstr(
1130     MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
1131     int CmpValue, const MachineRegisterInfo *MRI) const {
1132   assert(CmpInstr.getParent());
1133   assert(MRI);
1134 
1135   // Replace SUBSWrr with SUBWrr if NZCV is not used.
1136   int DeadNZCVIdx = CmpInstr.findRegisterDefOperandIdx(AArch64::NZCV, true);
1137   if (DeadNZCVIdx != -1) {
1138     if (CmpInstr.definesRegister(AArch64::WZR) ||
1139         CmpInstr.definesRegister(AArch64::XZR)) {
1140       CmpInstr.eraseFromParent();
1141       return true;
1142     }
1143     unsigned Opc = CmpInstr.getOpcode();
1144     unsigned NewOpc = convertToNonFlagSettingOpc(CmpInstr);
1145     if (NewOpc == Opc)
1146       return false;
1147     const MCInstrDesc &MCID = get(NewOpc);
1148     CmpInstr.setDesc(MCID);
1149     CmpInstr.RemoveOperand(DeadNZCVIdx);
1150     bool succeeded = UpdateOperandRegClass(CmpInstr);
1151     (void)succeeded;
1152     assert(succeeded && "Some operands reg class are incompatible!");
1153     return true;
1154   }
1155 
1156   // Continue only if we have a "ri" where immediate is zero.
1157   // FIXME:CmpValue has already been converted to 0 or 1 in analyzeCompare
1158   // function.
1159   assert((CmpValue == 0 || CmpValue == 1) && "CmpValue must be 0 or 1!");
1160   if (CmpValue != 0 || SrcReg2 != 0)
1161     return false;
1162 
1163   // CmpInstr is a Compare instruction if destination register is not used.
1164   if (!MRI->use_nodbg_empty(CmpInstr.getOperand(0).getReg()))
1165     return false;
1166 
1167   return substituteCmpToZero(CmpInstr, SrcReg, MRI);
1168 }
1169 
1170 /// Get opcode of S version of Instr.
1171 /// If Instr is S version its opcode is returned.
1172 /// AArch64::INSTRUCTION_LIST_END is returned if Instr does not have S version
1173 /// or we are not interested in it.
1174 static unsigned sForm(MachineInstr &Instr) {
1175   switch (Instr.getOpcode()) {
1176   default:
1177     return AArch64::INSTRUCTION_LIST_END;
1178 
1179   case AArch64::ADDSWrr:
1180   case AArch64::ADDSWri:
1181   case AArch64::ADDSXrr:
1182   case AArch64::ADDSXri:
1183   case AArch64::SUBSWrr:
1184   case AArch64::SUBSWri:
1185   case AArch64::SUBSXrr:
1186   case AArch64::SUBSXri:
1187     return Instr.getOpcode();
1188 
1189   case AArch64::ADDWrr:
1190     return AArch64::ADDSWrr;
1191   case AArch64::ADDWri:
1192     return AArch64::ADDSWri;
1193   case AArch64::ADDXrr:
1194     return AArch64::ADDSXrr;
1195   case AArch64::ADDXri:
1196     return AArch64::ADDSXri;
1197   case AArch64::ADCWr:
1198     return AArch64::ADCSWr;
1199   case AArch64::ADCXr:
1200     return AArch64::ADCSXr;
1201   case AArch64::SUBWrr:
1202     return AArch64::SUBSWrr;
1203   case AArch64::SUBWri:
1204     return AArch64::SUBSWri;
1205   case AArch64::SUBXrr:
1206     return AArch64::SUBSXrr;
1207   case AArch64::SUBXri:
1208     return AArch64::SUBSXri;
1209   case AArch64::SBCWr:
1210     return AArch64::SBCSWr;
1211   case AArch64::SBCXr:
1212     return AArch64::SBCSXr;
1213   case AArch64::ANDWri:
1214     return AArch64::ANDSWri;
1215   case AArch64::ANDXri:
1216     return AArch64::ANDSXri;
1217   }
1218 }
1219 
1220 /// Check if AArch64::NZCV should be alive in successors of MBB.
1221 static bool areCFlagsAliveInSuccessors(MachineBasicBlock *MBB) {
1222   for (auto *BB : MBB->successors())
1223     if (BB->isLiveIn(AArch64::NZCV))
1224       return true;
1225   return false;
1226 }
1227 
1228 namespace {
1229 
1230 struct UsedNZCV {
1231   bool N = false;
1232   bool Z = false;
1233   bool C = false;
1234   bool V = false;
1235 
1236   UsedNZCV() = default;
1237 
1238   UsedNZCV &operator|=(const UsedNZCV &UsedFlags) {
1239     this->N |= UsedFlags.N;
1240     this->Z |= UsedFlags.Z;
1241     this->C |= UsedFlags.C;
1242     this->V |= UsedFlags.V;
1243     return *this;
1244   }
1245 };
1246 
1247 } // end anonymous namespace
1248 
1249 /// Find a condition code used by the instruction.
1250 /// Returns AArch64CC::Invalid if either the instruction does not use condition
1251 /// codes or we don't optimize CmpInstr in the presence of such instructions.
1252 static AArch64CC::CondCode findCondCodeUsedByInstr(const MachineInstr &Instr) {
1253   switch (Instr.getOpcode()) {
1254   default:
1255     return AArch64CC::Invalid;
1256 
1257   case AArch64::Bcc: {
1258     int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
1259     assert(Idx >= 2);
1260     return static_cast<AArch64CC::CondCode>(Instr.getOperand(Idx - 2).getImm());
1261   }
1262 
1263   case AArch64::CSINVWr:
1264   case AArch64::CSINVXr:
1265   case AArch64::CSINCWr:
1266   case AArch64::CSINCXr:
1267   case AArch64::CSELWr:
1268   case AArch64::CSELXr:
1269   case AArch64::CSNEGWr:
1270   case AArch64::CSNEGXr:
1271   case AArch64::FCSELSrrr:
1272   case AArch64::FCSELDrrr: {
1273     int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
1274     assert(Idx >= 1);
1275     return static_cast<AArch64CC::CondCode>(Instr.getOperand(Idx - 1).getImm());
1276   }
1277   }
1278 }
1279 
1280 static UsedNZCV getUsedNZCV(AArch64CC::CondCode CC) {
1281   assert(CC != AArch64CC::Invalid);
1282   UsedNZCV UsedFlags;
1283   switch (CC) {
1284   default:
1285     break;
1286 
1287   case AArch64CC::EQ: // Z set
1288   case AArch64CC::NE: // Z clear
1289     UsedFlags.Z = true;
1290     break;
1291 
1292   case AArch64CC::HI: // Z clear and C set
1293   case AArch64CC::LS: // Z set   or  C clear
1294     UsedFlags.Z = true;
1295     LLVM_FALLTHROUGH;
1296   case AArch64CC::HS: // C set
1297   case AArch64CC::LO: // C clear
1298     UsedFlags.C = true;
1299     break;
1300 
1301   case AArch64CC::MI: // N set
1302   case AArch64CC::PL: // N clear
1303     UsedFlags.N = true;
1304     break;
1305 
1306   case AArch64CC::VS: // V set
1307   case AArch64CC::VC: // V clear
1308     UsedFlags.V = true;
1309     break;
1310 
1311   case AArch64CC::GT: // Z clear, N and V the same
1312   case AArch64CC::LE: // Z set,   N and V differ
1313     UsedFlags.Z = true;
1314     LLVM_FALLTHROUGH;
1315   case AArch64CC::GE: // N and V the same
1316   case AArch64CC::LT: // N and V differ
1317     UsedFlags.N = true;
1318     UsedFlags.V = true;
1319     break;
1320   }
1321   return UsedFlags;
1322 }
1323 
1324 static bool isADDSRegImm(unsigned Opcode) {
1325   return Opcode == AArch64::ADDSWri || Opcode == AArch64::ADDSXri;
1326 }
1327 
1328 static bool isSUBSRegImm(unsigned Opcode) {
1329   return Opcode == AArch64::SUBSWri || Opcode == AArch64::SUBSXri;
1330 }
1331 
1332 /// Check if CmpInstr can be substituted by MI.
1333 ///
1334 /// CmpInstr can be substituted:
1335 /// - CmpInstr is either 'ADDS %vreg, 0' or 'SUBS %vreg, 0'
1336 /// - and, MI and CmpInstr are from the same MachineBB
1337 /// - and, condition flags are not alive in successors of the CmpInstr parent
1338 /// - and, if MI opcode is the S form there must be no defs of flags between
1339 ///        MI and CmpInstr
1340 ///        or if MI opcode is not the S form there must be neither defs of flags
1341 ///        nor uses of flags between MI and CmpInstr.
1342 /// - and  C/V flags are not used after CmpInstr
1343 static bool canInstrSubstituteCmpInstr(MachineInstr *MI, MachineInstr *CmpInstr,
1344                                        const TargetRegisterInfo *TRI) {
1345   assert(MI);
1346   assert(sForm(*MI) != AArch64::INSTRUCTION_LIST_END);
1347   assert(CmpInstr);
1348 
1349   const unsigned CmpOpcode = CmpInstr->getOpcode();
1350   if (!isADDSRegImm(CmpOpcode) && !isSUBSRegImm(CmpOpcode))
1351     return false;
1352 
1353   if (MI->getParent() != CmpInstr->getParent())
1354     return false;
1355 
1356   if (areCFlagsAliveInSuccessors(CmpInstr->getParent()))
1357     return false;
1358 
1359   AccessKind AccessToCheck = AK_Write;
1360   if (sForm(*MI) != MI->getOpcode())
1361     AccessToCheck = AK_All;
1362   if (areCFlagsAccessedBetweenInstrs(MI, CmpInstr, TRI, AccessToCheck))
1363     return false;
1364 
1365   UsedNZCV NZCVUsedAfterCmp;
1366   for (auto I = std::next(CmpInstr->getIterator()),
1367             E = CmpInstr->getParent()->instr_end();
1368        I != E; ++I) {
1369     const MachineInstr &Instr = *I;
1370     if (Instr.readsRegister(AArch64::NZCV, TRI)) {
1371       AArch64CC::CondCode CC = findCondCodeUsedByInstr(Instr);
1372       if (CC == AArch64CC::Invalid) // Unsupported conditional instruction
1373         return false;
1374       NZCVUsedAfterCmp |= getUsedNZCV(CC);
1375     }
1376 
1377     if (Instr.modifiesRegister(AArch64::NZCV, TRI))
1378       break;
1379   }
1380 
1381   return !NZCVUsedAfterCmp.C && !NZCVUsedAfterCmp.V;
1382 }
1383 
1384 /// Substitute an instruction comparing to zero with another instruction
1385 /// which produces needed condition flags.
1386 ///
1387 /// Return true on success.
1388 bool AArch64InstrInfo::substituteCmpToZero(
1389     MachineInstr &CmpInstr, unsigned SrcReg,
1390     const MachineRegisterInfo *MRI) const {
1391   assert(MRI);
1392   // Get the unique definition of SrcReg.
1393   MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1394   if (!MI)
1395     return false;
1396 
1397   const TargetRegisterInfo *TRI = &getRegisterInfo();
1398 
1399   unsigned NewOpc = sForm(*MI);
1400   if (NewOpc == AArch64::INSTRUCTION_LIST_END)
1401     return false;
1402 
1403   if (!canInstrSubstituteCmpInstr(MI, &CmpInstr, TRI))
1404     return false;
1405 
1406   // Update the instruction to set NZCV.
1407   MI->setDesc(get(NewOpc));
1408   CmpInstr.eraseFromParent();
1409   bool succeeded = UpdateOperandRegClass(*MI);
1410   (void)succeeded;
1411   assert(succeeded && "Some operands reg class are incompatible!");
1412   MI->addRegisterDefined(AArch64::NZCV, TRI);
1413   return true;
1414 }
1415 
1416 bool AArch64InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1417   if (MI.getOpcode() != TargetOpcode::LOAD_STACK_GUARD)
1418     return false;
1419 
1420   MachineBasicBlock &MBB = *MI.getParent();
1421   DebugLoc DL = MI.getDebugLoc();
1422   unsigned Reg = MI.getOperand(0).getReg();
1423   const GlobalValue *GV =
1424       cast<GlobalValue>((*MI.memoperands_begin())->getValue());
1425   const TargetMachine &TM = MBB.getParent()->getTarget();
1426   unsigned char OpFlags = Subtarget.ClassifyGlobalReference(GV, TM);
1427   const unsigned char MO_NC = AArch64II::MO_NC;
1428 
1429   if ((OpFlags & AArch64II::MO_GOT) != 0) {
1430     BuildMI(MBB, MI, DL, get(AArch64::LOADgot), Reg)
1431         .addGlobalAddress(GV, 0, AArch64II::MO_GOT);
1432     BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1433         .addReg(Reg, RegState::Kill)
1434         .addImm(0)
1435         .addMemOperand(*MI.memoperands_begin());
1436   } else if (TM.getCodeModel() == CodeModel::Large) {
1437     BuildMI(MBB, MI, DL, get(AArch64::MOVZXi), Reg)
1438         .addGlobalAddress(GV, 0, AArch64II::MO_G0 | MO_NC)
1439         .addImm(0);
1440     BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1441         .addReg(Reg, RegState::Kill)
1442         .addGlobalAddress(GV, 0, AArch64II::MO_G1 | MO_NC)
1443         .addImm(16);
1444     BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1445         .addReg(Reg, RegState::Kill)
1446         .addGlobalAddress(GV, 0, AArch64II::MO_G2 | MO_NC)
1447         .addImm(32);
1448     BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1449         .addReg(Reg, RegState::Kill)
1450         .addGlobalAddress(GV, 0, AArch64II::MO_G3)
1451         .addImm(48);
1452     BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1453         .addReg(Reg, RegState::Kill)
1454         .addImm(0)
1455         .addMemOperand(*MI.memoperands_begin());
1456   } else {
1457     BuildMI(MBB, MI, DL, get(AArch64::ADRP), Reg)
1458         .addGlobalAddress(GV, 0, OpFlags | AArch64II::MO_PAGE);
1459     unsigned char LoFlags = OpFlags | AArch64II::MO_PAGEOFF | MO_NC;
1460     BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1461         .addReg(Reg, RegState::Kill)
1462         .addGlobalAddress(GV, 0, LoFlags)
1463         .addMemOperand(*MI.memoperands_begin());
1464   }
1465 
1466   MBB.erase(MI);
1467 
1468   return true;
1469 }
1470 
1471 /// Return true if this is this instruction has a non-zero immediate
1472 bool AArch64InstrInfo::hasShiftedReg(const MachineInstr &MI) const {
1473   switch (MI.getOpcode()) {
1474   default:
1475     break;
1476   case AArch64::ADDSWrs:
1477   case AArch64::ADDSXrs:
1478   case AArch64::ADDWrs:
1479   case AArch64::ADDXrs:
1480   case AArch64::ANDSWrs:
1481   case AArch64::ANDSXrs:
1482   case AArch64::ANDWrs:
1483   case AArch64::ANDXrs:
1484   case AArch64::BICSWrs:
1485   case AArch64::BICSXrs:
1486   case AArch64::BICWrs:
1487   case AArch64::BICXrs:
1488   case AArch64::EONWrs:
1489   case AArch64::EONXrs:
1490   case AArch64::EORWrs:
1491   case AArch64::EORXrs:
1492   case AArch64::ORNWrs:
1493   case AArch64::ORNXrs:
1494   case AArch64::ORRWrs:
1495   case AArch64::ORRXrs:
1496   case AArch64::SUBSWrs:
1497   case AArch64::SUBSXrs:
1498   case AArch64::SUBWrs:
1499   case AArch64::SUBXrs:
1500     if (MI.getOperand(3).isImm()) {
1501       unsigned val = MI.getOperand(3).getImm();
1502       return (val != 0);
1503     }
1504     break;
1505   }
1506   return false;
1507 }
1508 
1509 /// Return true if this is this instruction has a non-zero immediate
1510 bool AArch64InstrInfo::hasExtendedReg(const MachineInstr &MI) const {
1511   switch (MI.getOpcode()) {
1512   default:
1513     break;
1514   case AArch64::ADDSWrx:
1515   case AArch64::ADDSXrx:
1516   case AArch64::ADDSXrx64:
1517   case AArch64::ADDWrx:
1518   case AArch64::ADDXrx:
1519   case AArch64::ADDXrx64:
1520   case AArch64::SUBSWrx:
1521   case AArch64::SUBSXrx:
1522   case AArch64::SUBSXrx64:
1523   case AArch64::SUBWrx:
1524   case AArch64::SUBXrx:
1525   case AArch64::SUBXrx64:
1526     if (MI.getOperand(3).isImm()) {
1527       unsigned val = MI.getOperand(3).getImm();
1528       return (val != 0);
1529     }
1530     break;
1531   }
1532 
1533   return false;
1534 }
1535 
1536 // Return true if this instruction simply sets its single destination register
1537 // to zero. This is equivalent to a register rename of the zero-register.
1538 bool AArch64InstrInfo::isGPRZero(const MachineInstr &MI) const {
1539   switch (MI.getOpcode()) {
1540   default:
1541     break;
1542   case AArch64::MOVZWi:
1543   case AArch64::MOVZXi: // movz Rd, #0 (LSL #0)
1544     if (MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) {
1545       assert(MI.getDesc().getNumOperands() == 3 &&
1546              MI.getOperand(2).getImm() == 0 && "invalid MOVZi operands");
1547       return true;
1548     }
1549     break;
1550   case AArch64::ANDWri: // and Rd, Rzr, #imm
1551     return MI.getOperand(1).getReg() == AArch64::WZR;
1552   case AArch64::ANDXri:
1553     return MI.getOperand(1).getReg() == AArch64::XZR;
1554   case TargetOpcode::COPY:
1555     return MI.getOperand(1).getReg() == AArch64::WZR;
1556   }
1557   return false;
1558 }
1559 
1560 // Return true if this instruction simply renames a general register without
1561 // modifying bits.
1562 bool AArch64InstrInfo::isGPRCopy(const MachineInstr &MI) const {
1563   switch (MI.getOpcode()) {
1564   default:
1565     break;
1566   case TargetOpcode::COPY: {
1567     // GPR32 copies will by lowered to ORRXrs
1568     unsigned DstReg = MI.getOperand(0).getReg();
1569     return (AArch64::GPR32RegClass.contains(DstReg) ||
1570             AArch64::GPR64RegClass.contains(DstReg));
1571   }
1572   case AArch64::ORRXrs: // orr Xd, Xzr, Xm (LSL #0)
1573     if (MI.getOperand(1).getReg() == AArch64::XZR) {
1574       assert(MI.getDesc().getNumOperands() == 4 &&
1575              MI.getOperand(3).getImm() == 0 && "invalid ORRrs operands");
1576       return true;
1577     }
1578     break;
1579   case AArch64::ADDXri: // add Xd, Xn, #0 (LSL #0)
1580     if (MI.getOperand(2).getImm() == 0) {
1581       assert(MI.getDesc().getNumOperands() == 4 &&
1582              MI.getOperand(3).getImm() == 0 && "invalid ADDXri operands");
1583       return true;
1584     }
1585     break;
1586   }
1587   return false;
1588 }
1589 
1590 // Return true if this instruction simply renames a general register without
1591 // modifying bits.
1592 bool AArch64InstrInfo::isFPRCopy(const MachineInstr &MI) const {
1593   switch (MI.getOpcode()) {
1594   default:
1595     break;
1596   case TargetOpcode::COPY: {
1597     // FPR64 copies will by lowered to ORR.16b
1598     unsigned DstReg = MI.getOperand(0).getReg();
1599     return (AArch64::FPR64RegClass.contains(DstReg) ||
1600             AArch64::FPR128RegClass.contains(DstReg));
1601   }
1602   case AArch64::ORRv16i8:
1603     if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
1604       assert(MI.getDesc().getNumOperands() == 3 && MI.getOperand(0).isReg() &&
1605              "invalid ORRv16i8 operands");
1606       return true;
1607     }
1608     break;
1609   }
1610   return false;
1611 }
1612 
1613 unsigned AArch64InstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1614                                                int &FrameIndex) const {
1615   switch (MI.getOpcode()) {
1616   default:
1617     break;
1618   case AArch64::LDRWui:
1619   case AArch64::LDRXui:
1620   case AArch64::LDRBui:
1621   case AArch64::LDRHui:
1622   case AArch64::LDRSui:
1623   case AArch64::LDRDui:
1624   case AArch64::LDRQui:
1625     if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
1626         MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
1627       FrameIndex = MI.getOperand(1).getIndex();
1628       return MI.getOperand(0).getReg();
1629     }
1630     break;
1631   }
1632 
1633   return 0;
1634 }
1635 
1636 unsigned AArch64InstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1637                                               int &FrameIndex) const {
1638   switch (MI.getOpcode()) {
1639   default:
1640     break;
1641   case AArch64::STRWui:
1642   case AArch64::STRXui:
1643   case AArch64::STRBui:
1644   case AArch64::STRHui:
1645   case AArch64::STRSui:
1646   case AArch64::STRDui:
1647   case AArch64::STRQui:
1648     if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
1649         MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
1650       FrameIndex = MI.getOperand(1).getIndex();
1651       return MI.getOperand(0).getReg();
1652     }
1653     break;
1654   }
1655   return 0;
1656 }
1657 
1658 /// Return true if this is load/store scales or extends its register offset.
1659 /// This refers to scaling a dynamic index as opposed to scaled immediates.
1660 /// MI should be a memory op that allows scaled addressing.
1661 bool AArch64InstrInfo::isScaledAddr(const MachineInstr &MI) const {
1662   switch (MI.getOpcode()) {
1663   default:
1664     break;
1665   case AArch64::LDRBBroW:
1666   case AArch64::LDRBroW:
1667   case AArch64::LDRDroW:
1668   case AArch64::LDRHHroW:
1669   case AArch64::LDRHroW:
1670   case AArch64::LDRQroW:
1671   case AArch64::LDRSBWroW:
1672   case AArch64::LDRSBXroW:
1673   case AArch64::LDRSHWroW:
1674   case AArch64::LDRSHXroW:
1675   case AArch64::LDRSWroW:
1676   case AArch64::LDRSroW:
1677   case AArch64::LDRWroW:
1678   case AArch64::LDRXroW:
1679   case AArch64::STRBBroW:
1680   case AArch64::STRBroW:
1681   case AArch64::STRDroW:
1682   case AArch64::STRHHroW:
1683   case AArch64::STRHroW:
1684   case AArch64::STRQroW:
1685   case AArch64::STRSroW:
1686   case AArch64::STRWroW:
1687   case AArch64::STRXroW:
1688   case AArch64::LDRBBroX:
1689   case AArch64::LDRBroX:
1690   case AArch64::LDRDroX:
1691   case AArch64::LDRHHroX:
1692   case AArch64::LDRHroX:
1693   case AArch64::LDRQroX:
1694   case AArch64::LDRSBWroX:
1695   case AArch64::LDRSBXroX:
1696   case AArch64::LDRSHWroX:
1697   case AArch64::LDRSHXroX:
1698   case AArch64::LDRSWroX:
1699   case AArch64::LDRSroX:
1700   case AArch64::LDRWroX:
1701   case AArch64::LDRXroX:
1702   case AArch64::STRBBroX:
1703   case AArch64::STRBroX:
1704   case AArch64::STRDroX:
1705   case AArch64::STRHHroX:
1706   case AArch64::STRHroX:
1707   case AArch64::STRQroX:
1708   case AArch64::STRSroX:
1709   case AArch64::STRWroX:
1710   case AArch64::STRXroX:
1711 
1712     unsigned Val = MI.getOperand(3).getImm();
1713     AArch64_AM::ShiftExtendType ExtType = AArch64_AM::getMemExtendType(Val);
1714     return (ExtType != AArch64_AM::UXTX) || AArch64_AM::getMemDoShift(Val);
1715   }
1716   return false;
1717 }
1718 
1719 /// Check all MachineMemOperands for a hint to suppress pairing.
1720 bool AArch64InstrInfo::isLdStPairSuppressed(const MachineInstr &MI) const {
1721   return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
1722     return MMO->getFlags() & MOSuppressPair;
1723   });
1724 }
1725 
1726 /// Set a flag on the first MachineMemOperand to suppress pairing.
1727 void AArch64InstrInfo::suppressLdStPair(MachineInstr &MI) const {
1728   if (MI.memoperands_empty())
1729     return;
1730   (*MI.memoperands_begin())->setFlags(MOSuppressPair);
1731 }
1732 
1733 /// Check all MachineMemOperands for a hint that the load/store is strided.
1734 bool AArch64InstrInfo::isStridedAccess(const MachineInstr &MI) const {
1735   return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
1736     return MMO->getFlags() & MOStridedAccess;
1737   });
1738 }
1739 
1740 bool AArch64InstrInfo::isUnscaledLdSt(unsigned Opc) const {
1741   switch (Opc) {
1742   default:
1743     return false;
1744   case AArch64::STURSi:
1745   case AArch64::STURDi:
1746   case AArch64::STURQi:
1747   case AArch64::STURBBi:
1748   case AArch64::STURHHi:
1749   case AArch64::STURWi:
1750   case AArch64::STURXi:
1751   case AArch64::LDURSi:
1752   case AArch64::LDURDi:
1753   case AArch64::LDURQi:
1754   case AArch64::LDURWi:
1755   case AArch64::LDURXi:
1756   case AArch64::LDURSWi:
1757   case AArch64::LDURHHi:
1758   case AArch64::LDURBBi:
1759   case AArch64::LDURSBWi:
1760   case AArch64::LDURSHWi:
1761     return true;
1762   }
1763 }
1764 
1765 bool AArch64InstrInfo::isUnscaledLdSt(MachineInstr &MI) const {
1766   return isUnscaledLdSt(MI.getOpcode());
1767 }
1768 
1769 // Is this a candidate for ld/st merging or pairing?  For example, we don't
1770 // touch volatiles or load/stores that have a hint to avoid pair formation.
1771 bool AArch64InstrInfo::isCandidateToMergeOrPair(MachineInstr &MI) const {
1772   // If this is a volatile load/store, don't mess with it.
1773   if (MI.hasOrderedMemoryRef())
1774     return false;
1775 
1776   // Make sure this is a reg+imm (as opposed to an address reloc).
1777   assert(MI.getOperand(1).isReg() && "Expected a reg operand.");
1778   if (!MI.getOperand(2).isImm())
1779     return false;
1780 
1781   // Can't merge/pair if the instruction modifies the base register.
1782   // e.g., ldr x0, [x0]
1783   unsigned BaseReg = MI.getOperand(1).getReg();
1784   const TargetRegisterInfo *TRI = &getRegisterInfo();
1785   if (MI.modifiesRegister(BaseReg, TRI))
1786     return false;
1787 
1788   // Check if this load/store has a hint to avoid pair formation.
1789   // MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
1790   if (isLdStPairSuppressed(MI))
1791     return false;
1792 
1793   // On some CPUs quad load/store pairs are slower than two single load/stores.
1794   if (Subtarget.isPaired128Slow()) {
1795     switch (MI.getOpcode()) {
1796     default:
1797       break;
1798     case AArch64::LDURQi:
1799     case AArch64::STURQi:
1800     case AArch64::LDRQui:
1801     case AArch64::STRQui:
1802       return false;
1803     }
1804   }
1805 
1806   return true;
1807 }
1808 
1809 bool AArch64InstrInfo::getMemOpBaseRegImmOfs(
1810     MachineInstr &LdSt, unsigned &BaseReg, int64_t &Offset,
1811     const TargetRegisterInfo *TRI) const {
1812   unsigned Width;
1813   return getMemOpBaseRegImmOfsWidth(LdSt, BaseReg, Offset, Width, TRI);
1814 }
1815 
1816 bool AArch64InstrInfo::getMemOpBaseRegImmOfsWidth(
1817     MachineInstr &LdSt, unsigned &BaseReg, int64_t &Offset, unsigned &Width,
1818     const TargetRegisterInfo *TRI) const {
1819   assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
1820   // Handle only loads/stores with base register followed by immediate offset.
1821   if (LdSt.getNumExplicitOperands() == 3) {
1822     // Non-paired instruction (e.g., ldr x1, [x0, #8]).
1823     if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isImm())
1824       return false;
1825   } else if (LdSt.getNumExplicitOperands() == 4) {
1826     // Paired instruction (e.g., ldp x1, x2, [x0, #8]).
1827     if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isReg() ||
1828         !LdSt.getOperand(3).isImm())
1829       return false;
1830   } else
1831     return false;
1832 
1833   // Get the scaling factor for the instruction and set the width for the
1834   // instruction.
1835   unsigned Scale = 0;
1836   int64_t Dummy1, Dummy2;
1837 
1838   // If this returns false, then it's an instruction we don't want to handle.
1839   if (!getMemOpInfo(LdSt.getOpcode(), Scale, Width, Dummy1, Dummy2))
1840     return false;
1841 
1842   // Compute the offset. Offset is calculated as the immediate operand
1843   // multiplied by the scaling factor. Unscaled instructions have scaling factor
1844   // set to 1.
1845   if (LdSt.getNumExplicitOperands() == 3) {
1846     BaseReg = LdSt.getOperand(1).getReg();
1847     Offset = LdSt.getOperand(2).getImm() * Scale;
1848   } else {
1849     assert(LdSt.getNumExplicitOperands() == 4 && "invalid number of operands");
1850     BaseReg = LdSt.getOperand(2).getReg();
1851     Offset = LdSt.getOperand(3).getImm() * Scale;
1852   }
1853   return true;
1854 }
1855 
1856 MachineOperand &
1857 AArch64InstrInfo::getMemOpBaseRegImmOfsOffsetOperand(MachineInstr &LdSt) const {
1858   assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
1859   MachineOperand &OfsOp = LdSt.getOperand(LdSt.getNumExplicitOperands() - 1);
1860   assert(OfsOp.isImm() && "Offset operand wasn't immediate.");
1861   return OfsOp;
1862 }
1863 
1864 bool AArch64InstrInfo::getMemOpInfo(unsigned Opcode, unsigned &Scale,
1865                                     unsigned &Width, int64_t &MinOffset,
1866                                     int64_t &MaxOffset) const {
1867   switch (Opcode) {
1868   // Not a memory operation or something we want to handle.
1869   default:
1870     Scale = Width = 0;
1871     MinOffset = MaxOffset = 0;
1872     return false;
1873   case AArch64::STRWpost:
1874   case AArch64::LDRWpost:
1875     Width = 32;
1876     Scale = 4;
1877     MinOffset = -256;
1878     MaxOffset = 255;
1879     break;
1880   case AArch64::LDURQi:
1881   case AArch64::STURQi:
1882     Width = 16;
1883     Scale = 1;
1884     MinOffset = -256;
1885     MaxOffset = 255;
1886     break;
1887   case AArch64::LDURXi:
1888   case AArch64::LDURDi:
1889   case AArch64::STURXi:
1890   case AArch64::STURDi:
1891     Width = 8;
1892     Scale = 1;
1893     MinOffset = -256;
1894     MaxOffset = 255;
1895     break;
1896   case AArch64::LDURWi:
1897   case AArch64::LDURSi:
1898   case AArch64::LDURSWi:
1899   case AArch64::STURWi:
1900   case AArch64::STURSi:
1901     Width = 4;
1902     Scale = 1;
1903     MinOffset = -256;
1904     MaxOffset = 255;
1905     break;
1906   case AArch64::LDURHi:
1907   case AArch64::LDURHHi:
1908   case AArch64::LDURSHXi:
1909   case AArch64::LDURSHWi:
1910   case AArch64::STURHi:
1911   case AArch64::STURHHi:
1912     Width = 2;
1913     Scale = 1;
1914     MinOffset = -256;
1915     MaxOffset = 255;
1916     break;
1917   case AArch64::LDURBi:
1918   case AArch64::LDURBBi:
1919   case AArch64::LDURSBXi:
1920   case AArch64::LDURSBWi:
1921   case AArch64::STURBi:
1922   case AArch64::STURBBi:
1923     Width = 1;
1924     Scale = 1;
1925     MinOffset = -256;
1926     MaxOffset = 255;
1927     break;
1928   case AArch64::LDPQi:
1929   case AArch64::LDNPQi:
1930   case AArch64::STPQi:
1931   case AArch64::STNPQi:
1932     Scale = 16;
1933     Width = 32;
1934     MinOffset = -64;
1935     MaxOffset = 63;
1936     break;
1937   case AArch64::LDRQui:
1938   case AArch64::STRQui:
1939     Scale = Width = 16;
1940     MinOffset = 0;
1941     MaxOffset = 4095;
1942     break;
1943   case AArch64::LDPXi:
1944   case AArch64::LDPDi:
1945   case AArch64::LDNPXi:
1946   case AArch64::LDNPDi:
1947   case AArch64::STPXi:
1948   case AArch64::STPDi:
1949   case AArch64::STNPXi:
1950   case AArch64::STNPDi:
1951     Scale = 8;
1952     Width = 16;
1953     MinOffset = -64;
1954     MaxOffset = 63;
1955     break;
1956   case AArch64::LDRXui:
1957   case AArch64::LDRDui:
1958   case AArch64::STRXui:
1959   case AArch64::STRDui:
1960     Scale = Width = 8;
1961     MinOffset = 0;
1962     MaxOffset = 4095;
1963     break;
1964   case AArch64::LDPWi:
1965   case AArch64::LDPSi:
1966   case AArch64::LDNPWi:
1967   case AArch64::LDNPSi:
1968   case AArch64::STPWi:
1969   case AArch64::STPSi:
1970   case AArch64::STNPWi:
1971   case AArch64::STNPSi:
1972     Scale = 4;
1973     Width = 8;
1974     MinOffset = -64;
1975     MaxOffset = 63;
1976     break;
1977   case AArch64::LDRWui:
1978   case AArch64::LDRSui:
1979   case AArch64::LDRSWui:
1980   case AArch64::STRWui:
1981   case AArch64::STRSui:
1982     Scale = Width = 4;
1983     MinOffset = 0;
1984     MaxOffset = 4095;
1985     break;
1986   case AArch64::LDRHui:
1987   case AArch64::LDRHHui:
1988   case AArch64::STRHui:
1989   case AArch64::STRHHui:
1990     Scale = Width = 2;
1991     MinOffset = 0;
1992     MaxOffset = 4095;
1993     break;
1994   case AArch64::LDRBui:
1995   case AArch64::LDRBBui:
1996   case AArch64::STRBui:
1997   case AArch64::STRBBui:
1998     Scale = Width = 1;
1999     MinOffset = 0;
2000     MaxOffset = 4095;
2001     break;
2002   }
2003 
2004   return true;
2005 }
2006 
2007 // Scale the unscaled offsets.  Returns false if the unscaled offset can't be
2008 // scaled.
2009 static bool scaleOffset(unsigned Opc, int64_t &Offset) {
2010   unsigned OffsetStride = 1;
2011   switch (Opc) {
2012   default:
2013     return false;
2014   case AArch64::LDURQi:
2015   case AArch64::STURQi:
2016     OffsetStride = 16;
2017     break;
2018   case AArch64::LDURXi:
2019   case AArch64::LDURDi:
2020   case AArch64::STURXi:
2021   case AArch64::STURDi:
2022     OffsetStride = 8;
2023     break;
2024   case AArch64::LDURWi:
2025   case AArch64::LDURSi:
2026   case AArch64::LDURSWi:
2027   case AArch64::STURWi:
2028   case AArch64::STURSi:
2029     OffsetStride = 4;
2030     break;
2031   }
2032   // If the byte-offset isn't a multiple of the stride, we can't scale this
2033   // offset.
2034   if (Offset % OffsetStride != 0)
2035     return false;
2036 
2037   // Convert the byte-offset used by unscaled into an "element" offset used
2038   // by the scaled pair load/store instructions.
2039   Offset /= OffsetStride;
2040   return true;
2041 }
2042 
2043 static bool canPairLdStOpc(unsigned FirstOpc, unsigned SecondOpc) {
2044   if (FirstOpc == SecondOpc)
2045     return true;
2046   // We can also pair sign-ext and zero-ext instructions.
2047   switch (FirstOpc) {
2048   default:
2049     return false;
2050   case AArch64::LDRWui:
2051   case AArch64::LDURWi:
2052     return SecondOpc == AArch64::LDRSWui || SecondOpc == AArch64::LDURSWi;
2053   case AArch64::LDRSWui:
2054   case AArch64::LDURSWi:
2055     return SecondOpc == AArch64::LDRWui || SecondOpc == AArch64::LDURWi;
2056   }
2057   // These instructions can't be paired based on their opcodes.
2058   return false;
2059 }
2060 
2061 /// Detect opportunities for ldp/stp formation.
2062 ///
2063 /// Only called for LdSt for which getMemOpBaseRegImmOfs returns true.
2064 bool AArch64InstrInfo::shouldClusterMemOps(MachineInstr &FirstLdSt,
2065                                            MachineInstr &SecondLdSt,
2066                                            unsigned NumLoads) const {
2067   // Only cluster up to a single pair.
2068   if (NumLoads > 1)
2069     return false;
2070 
2071   if (!isPairableLdStInst(FirstLdSt) || !isPairableLdStInst(SecondLdSt))
2072     return false;
2073 
2074   // Can we pair these instructions based on their opcodes?
2075   unsigned FirstOpc = FirstLdSt.getOpcode();
2076   unsigned SecondOpc = SecondLdSt.getOpcode();
2077   if (!canPairLdStOpc(FirstOpc, SecondOpc))
2078     return false;
2079 
2080   // Can't merge volatiles or load/stores that have a hint to avoid pair
2081   // formation, for example.
2082   if (!isCandidateToMergeOrPair(FirstLdSt) ||
2083       !isCandidateToMergeOrPair(SecondLdSt))
2084     return false;
2085 
2086   // isCandidateToMergeOrPair guarantees that operand 2 is an immediate.
2087   int64_t Offset1 = FirstLdSt.getOperand(2).getImm();
2088   if (isUnscaledLdSt(FirstOpc) && !scaleOffset(FirstOpc, Offset1))
2089     return false;
2090 
2091   int64_t Offset2 = SecondLdSt.getOperand(2).getImm();
2092   if (isUnscaledLdSt(SecondOpc) && !scaleOffset(SecondOpc, Offset2))
2093     return false;
2094 
2095   // Pairwise instructions have a 7-bit signed offset field.
2096   if (Offset1 > 63 || Offset1 < -64)
2097     return false;
2098 
2099   // The caller should already have ordered First/SecondLdSt by offset.
2100   assert(Offset1 <= Offset2 && "Caller should have ordered offsets.");
2101   return Offset1 + 1 == Offset2;
2102 }
2103 
2104 static const MachineInstrBuilder &AddSubReg(const MachineInstrBuilder &MIB,
2105                                             unsigned Reg, unsigned SubIdx,
2106                                             unsigned State,
2107                                             const TargetRegisterInfo *TRI) {
2108   if (!SubIdx)
2109     return MIB.addReg(Reg, State);
2110 
2111   if (TargetRegisterInfo::isPhysicalRegister(Reg))
2112     return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
2113   return MIB.addReg(Reg, State, SubIdx);
2114 }
2115 
2116 static bool forwardCopyWillClobberTuple(unsigned DestReg, unsigned SrcReg,
2117                                         unsigned NumRegs) {
2118   // We really want the positive remainder mod 32 here, that happens to be
2119   // easily obtainable with a mask.
2120   return ((DestReg - SrcReg) & 0x1f) < NumRegs;
2121 }
2122 
2123 void AArch64InstrInfo::copyPhysRegTuple(MachineBasicBlock &MBB,
2124                                         MachineBasicBlock::iterator I,
2125                                         const DebugLoc &DL, unsigned DestReg,
2126                                         unsigned SrcReg, bool KillSrc,
2127                                         unsigned Opcode,
2128                                         ArrayRef<unsigned> Indices) const {
2129   assert(Subtarget.hasNEON() && "Unexpected register copy without NEON");
2130   const TargetRegisterInfo *TRI = &getRegisterInfo();
2131   uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
2132   uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
2133   unsigned NumRegs = Indices.size();
2134 
2135   int SubReg = 0, End = NumRegs, Incr = 1;
2136   if (forwardCopyWillClobberTuple(DestEncoding, SrcEncoding, NumRegs)) {
2137     SubReg = NumRegs - 1;
2138     End = -1;
2139     Incr = -1;
2140   }
2141 
2142   for (; SubReg != End; SubReg += Incr) {
2143     const MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opcode));
2144     AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
2145     AddSubReg(MIB, SrcReg, Indices[SubReg], 0, TRI);
2146     AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
2147   }
2148 }
2149 
2150 void AArch64InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
2151                                    MachineBasicBlock::iterator I,
2152                                    const DebugLoc &DL, unsigned DestReg,
2153                                    unsigned SrcReg, bool KillSrc) const {
2154   if (AArch64::GPR32spRegClass.contains(DestReg) &&
2155       (AArch64::GPR32spRegClass.contains(SrcReg) || SrcReg == AArch64::WZR)) {
2156     const TargetRegisterInfo *TRI = &getRegisterInfo();
2157 
2158     if (DestReg == AArch64::WSP || SrcReg == AArch64::WSP) {
2159       // If either operand is WSP, expand to ADD #0.
2160       if (Subtarget.hasZeroCycleRegMove()) {
2161         // Cyclone recognizes "ADD Xd, Xn, #0" as a zero-cycle register move.
2162         unsigned DestRegX = TRI->getMatchingSuperReg(DestReg, AArch64::sub_32,
2163                                                      &AArch64::GPR64spRegClass);
2164         unsigned SrcRegX = TRI->getMatchingSuperReg(SrcReg, AArch64::sub_32,
2165                                                     &AArch64::GPR64spRegClass);
2166         // This instruction is reading and writing X registers.  This may upset
2167         // the register scavenger and machine verifier, so we need to indicate
2168         // that we are reading an undefined value from SrcRegX, but a proper
2169         // value from SrcReg.
2170         BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestRegX)
2171             .addReg(SrcRegX, RegState::Undef)
2172             .addImm(0)
2173             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0))
2174             .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
2175       } else {
2176         BuildMI(MBB, I, DL, get(AArch64::ADDWri), DestReg)
2177             .addReg(SrcReg, getKillRegState(KillSrc))
2178             .addImm(0)
2179             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2180       }
2181     } else if (SrcReg == AArch64::WZR && Subtarget.hasZeroCycleZeroing()) {
2182       BuildMI(MBB, I, DL, get(AArch64::MOVZWi), DestReg)
2183           .addImm(0)
2184           .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2185     } else {
2186       if (Subtarget.hasZeroCycleRegMove()) {
2187         // Cyclone recognizes "ORR Xd, XZR, Xm" as a zero-cycle register move.
2188         unsigned DestRegX = TRI->getMatchingSuperReg(DestReg, AArch64::sub_32,
2189                                                      &AArch64::GPR64spRegClass);
2190         unsigned SrcRegX = TRI->getMatchingSuperReg(SrcReg, AArch64::sub_32,
2191                                                     &AArch64::GPR64spRegClass);
2192         // This instruction is reading and writing X registers.  This may upset
2193         // the register scavenger and machine verifier, so we need to indicate
2194         // that we are reading an undefined value from SrcRegX, but a proper
2195         // value from SrcReg.
2196         BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestRegX)
2197             .addReg(AArch64::XZR)
2198             .addReg(SrcRegX, RegState::Undef)
2199             .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
2200       } else {
2201         // Otherwise, expand to ORR WZR.
2202         BuildMI(MBB, I, DL, get(AArch64::ORRWrr), DestReg)
2203             .addReg(AArch64::WZR)
2204             .addReg(SrcReg, getKillRegState(KillSrc));
2205       }
2206     }
2207     return;
2208   }
2209 
2210   if (AArch64::GPR64spRegClass.contains(DestReg) &&
2211       (AArch64::GPR64spRegClass.contains(SrcReg) || SrcReg == AArch64::XZR)) {
2212     if (DestReg == AArch64::SP || SrcReg == AArch64::SP) {
2213       // If either operand is SP, expand to ADD #0.
2214       BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestReg)
2215           .addReg(SrcReg, getKillRegState(KillSrc))
2216           .addImm(0)
2217           .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2218     } else if (SrcReg == AArch64::XZR && Subtarget.hasZeroCycleZeroing()) {
2219       BuildMI(MBB, I, DL, get(AArch64::MOVZXi), DestReg)
2220           .addImm(0)
2221           .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2222     } else {
2223       // Otherwise, expand to ORR XZR.
2224       BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestReg)
2225           .addReg(AArch64::XZR)
2226           .addReg(SrcReg, getKillRegState(KillSrc));
2227     }
2228     return;
2229   }
2230 
2231   // Copy a DDDD register quad by copying the individual sub-registers.
2232   if (AArch64::DDDDRegClass.contains(DestReg) &&
2233       AArch64::DDDDRegClass.contains(SrcReg)) {
2234     static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
2235                                        AArch64::dsub2, AArch64::dsub3};
2236     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
2237                      Indices);
2238     return;
2239   }
2240 
2241   // Copy a DDD register triple by copying the individual sub-registers.
2242   if (AArch64::DDDRegClass.contains(DestReg) &&
2243       AArch64::DDDRegClass.contains(SrcReg)) {
2244     static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
2245                                        AArch64::dsub2};
2246     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
2247                      Indices);
2248     return;
2249   }
2250 
2251   // Copy a DD register pair by copying the individual sub-registers.
2252   if (AArch64::DDRegClass.contains(DestReg) &&
2253       AArch64::DDRegClass.contains(SrcReg)) {
2254     static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1};
2255     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
2256                      Indices);
2257     return;
2258   }
2259 
2260   // Copy a QQQQ register quad by copying the individual sub-registers.
2261   if (AArch64::QQQQRegClass.contains(DestReg) &&
2262       AArch64::QQQQRegClass.contains(SrcReg)) {
2263     static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
2264                                        AArch64::qsub2, AArch64::qsub3};
2265     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
2266                      Indices);
2267     return;
2268   }
2269 
2270   // Copy a QQQ register triple by copying the individual sub-registers.
2271   if (AArch64::QQQRegClass.contains(DestReg) &&
2272       AArch64::QQQRegClass.contains(SrcReg)) {
2273     static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
2274                                        AArch64::qsub2};
2275     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
2276                      Indices);
2277     return;
2278   }
2279 
2280   // Copy a QQ register pair by copying the individual sub-registers.
2281   if (AArch64::QQRegClass.contains(DestReg) &&
2282       AArch64::QQRegClass.contains(SrcReg)) {
2283     static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1};
2284     copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
2285                      Indices);
2286     return;
2287   }
2288 
2289   if (AArch64::FPR128RegClass.contains(DestReg) &&
2290       AArch64::FPR128RegClass.contains(SrcReg)) {
2291     if (Subtarget.hasNEON()) {
2292       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2293           .addReg(SrcReg)
2294           .addReg(SrcReg, getKillRegState(KillSrc));
2295     } else {
2296       BuildMI(MBB, I, DL, get(AArch64::STRQpre))
2297           .addReg(AArch64::SP, RegState::Define)
2298           .addReg(SrcReg, getKillRegState(KillSrc))
2299           .addReg(AArch64::SP)
2300           .addImm(-16);
2301       BuildMI(MBB, I, DL, get(AArch64::LDRQpre))
2302           .addReg(AArch64::SP, RegState::Define)
2303           .addReg(DestReg, RegState::Define)
2304           .addReg(AArch64::SP)
2305           .addImm(16);
2306     }
2307     return;
2308   }
2309 
2310   if (AArch64::FPR64RegClass.contains(DestReg) &&
2311       AArch64::FPR64RegClass.contains(SrcReg)) {
2312     if (Subtarget.hasNEON()) {
2313       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::dsub,
2314                                        &AArch64::FPR128RegClass);
2315       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::dsub,
2316                                       &AArch64::FPR128RegClass);
2317       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2318           .addReg(SrcReg)
2319           .addReg(SrcReg, getKillRegState(KillSrc));
2320     } else {
2321       BuildMI(MBB, I, DL, get(AArch64::FMOVDr), DestReg)
2322           .addReg(SrcReg, getKillRegState(KillSrc));
2323     }
2324     return;
2325   }
2326 
2327   if (AArch64::FPR32RegClass.contains(DestReg) &&
2328       AArch64::FPR32RegClass.contains(SrcReg)) {
2329     if (Subtarget.hasNEON()) {
2330       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::ssub,
2331                                        &AArch64::FPR128RegClass);
2332       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::ssub,
2333                                       &AArch64::FPR128RegClass);
2334       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2335           .addReg(SrcReg)
2336           .addReg(SrcReg, getKillRegState(KillSrc));
2337     } else {
2338       BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
2339           .addReg(SrcReg, getKillRegState(KillSrc));
2340     }
2341     return;
2342   }
2343 
2344   if (AArch64::FPR16RegClass.contains(DestReg) &&
2345       AArch64::FPR16RegClass.contains(SrcReg)) {
2346     if (Subtarget.hasNEON()) {
2347       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::hsub,
2348                                        &AArch64::FPR128RegClass);
2349       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::hsub,
2350                                       &AArch64::FPR128RegClass);
2351       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2352           .addReg(SrcReg)
2353           .addReg(SrcReg, getKillRegState(KillSrc));
2354     } else {
2355       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::hsub,
2356                                        &AArch64::FPR32RegClass);
2357       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::hsub,
2358                                       &AArch64::FPR32RegClass);
2359       BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
2360           .addReg(SrcReg, getKillRegState(KillSrc));
2361     }
2362     return;
2363   }
2364 
2365   if (AArch64::FPR8RegClass.contains(DestReg) &&
2366       AArch64::FPR8RegClass.contains(SrcReg)) {
2367     if (Subtarget.hasNEON()) {
2368       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::bsub,
2369                                        &AArch64::FPR128RegClass);
2370       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::bsub,
2371                                       &AArch64::FPR128RegClass);
2372       BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2373           .addReg(SrcReg)
2374           .addReg(SrcReg, getKillRegState(KillSrc));
2375     } else {
2376       DestReg = RI.getMatchingSuperReg(DestReg, AArch64::bsub,
2377                                        &AArch64::FPR32RegClass);
2378       SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::bsub,
2379                                       &AArch64::FPR32RegClass);
2380       BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
2381           .addReg(SrcReg, getKillRegState(KillSrc));
2382     }
2383     return;
2384   }
2385 
2386   // Copies between GPR64 and FPR64.
2387   if (AArch64::FPR64RegClass.contains(DestReg) &&
2388       AArch64::GPR64RegClass.contains(SrcReg)) {
2389     BuildMI(MBB, I, DL, get(AArch64::FMOVXDr), DestReg)
2390         .addReg(SrcReg, getKillRegState(KillSrc));
2391     return;
2392   }
2393   if (AArch64::GPR64RegClass.contains(DestReg) &&
2394       AArch64::FPR64RegClass.contains(SrcReg)) {
2395     BuildMI(MBB, I, DL, get(AArch64::FMOVDXr), DestReg)
2396         .addReg(SrcReg, getKillRegState(KillSrc));
2397     return;
2398   }
2399   // Copies between GPR32 and FPR32.
2400   if (AArch64::FPR32RegClass.contains(DestReg) &&
2401       AArch64::GPR32RegClass.contains(SrcReg)) {
2402     BuildMI(MBB, I, DL, get(AArch64::FMOVWSr), DestReg)
2403         .addReg(SrcReg, getKillRegState(KillSrc));
2404     return;
2405   }
2406   if (AArch64::GPR32RegClass.contains(DestReg) &&
2407       AArch64::FPR32RegClass.contains(SrcReg)) {
2408     BuildMI(MBB, I, DL, get(AArch64::FMOVSWr), DestReg)
2409         .addReg(SrcReg, getKillRegState(KillSrc));
2410     return;
2411   }
2412 
2413   if (DestReg == AArch64::NZCV) {
2414     assert(AArch64::GPR64RegClass.contains(SrcReg) && "Invalid NZCV copy");
2415     BuildMI(MBB, I, DL, get(AArch64::MSR))
2416         .addImm(AArch64SysReg::NZCV)
2417         .addReg(SrcReg, getKillRegState(KillSrc))
2418         .addReg(AArch64::NZCV, RegState::Implicit | RegState::Define);
2419     return;
2420   }
2421 
2422   if (SrcReg == AArch64::NZCV) {
2423     assert(AArch64::GPR64RegClass.contains(DestReg) && "Invalid NZCV copy");
2424     BuildMI(MBB, I, DL, get(AArch64::MRS), DestReg)
2425         .addImm(AArch64SysReg::NZCV)
2426         .addReg(AArch64::NZCV, RegState::Implicit | getKillRegState(KillSrc));
2427     return;
2428   }
2429 
2430   llvm_unreachable("unimplemented reg-to-reg copy");
2431 }
2432 
2433 void AArch64InstrInfo::storeRegToStackSlot(
2434     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
2435     bool isKill, int FI, const TargetRegisterClass *RC,
2436     const TargetRegisterInfo *TRI) const {
2437   DebugLoc DL;
2438   if (MBBI != MBB.end())
2439     DL = MBBI->getDebugLoc();
2440   MachineFunction &MF = *MBB.getParent();
2441   MachineFrameInfo &MFI = MF.getFrameInfo();
2442   unsigned Align = MFI.getObjectAlignment(FI);
2443 
2444   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
2445   MachineMemOperand *MMO = MF.getMachineMemOperand(
2446       PtrInfo, MachineMemOperand::MOStore, MFI.getObjectSize(FI), Align);
2447   unsigned Opc = 0;
2448   bool Offset = true;
2449   switch (TRI->getSpillSize(*RC)) {
2450   case 1:
2451     if (AArch64::FPR8RegClass.hasSubClassEq(RC))
2452       Opc = AArch64::STRBui;
2453     break;
2454   case 2:
2455     if (AArch64::FPR16RegClass.hasSubClassEq(RC))
2456       Opc = AArch64::STRHui;
2457     break;
2458   case 4:
2459     if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
2460       Opc = AArch64::STRWui;
2461       if (TargetRegisterInfo::isVirtualRegister(SrcReg))
2462         MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
2463       else
2464         assert(SrcReg != AArch64::WSP);
2465     } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
2466       Opc = AArch64::STRSui;
2467     break;
2468   case 8:
2469     if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
2470       Opc = AArch64::STRXui;
2471       if (TargetRegisterInfo::isVirtualRegister(SrcReg))
2472         MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
2473       else
2474         assert(SrcReg != AArch64::SP);
2475     } else if (AArch64::FPR64RegClass.hasSubClassEq(RC))
2476       Opc = AArch64::STRDui;
2477     break;
2478   case 16:
2479     if (AArch64::FPR128RegClass.hasSubClassEq(RC))
2480       Opc = AArch64::STRQui;
2481     else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
2482       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2483       Opc = AArch64::ST1Twov1d;
2484       Offset = false;
2485     }
2486     break;
2487   case 24:
2488     if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
2489       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2490       Opc = AArch64::ST1Threev1d;
2491       Offset = false;
2492     }
2493     break;
2494   case 32:
2495     if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
2496       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2497       Opc = AArch64::ST1Fourv1d;
2498       Offset = false;
2499     } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
2500       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2501       Opc = AArch64::ST1Twov2d;
2502       Offset = false;
2503     }
2504     break;
2505   case 48:
2506     if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
2507       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2508       Opc = AArch64::ST1Threev2d;
2509       Offset = false;
2510     }
2511     break;
2512   case 64:
2513     if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
2514       assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2515       Opc = AArch64::ST1Fourv2d;
2516       Offset = false;
2517     }
2518     break;
2519   }
2520   assert(Opc && "Unknown register class");
2521 
2522   const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DL, get(Opc))
2523                                      .addReg(SrcReg, getKillRegState(isKill))
2524                                      .addFrameIndex(FI);
2525 
2526   if (Offset)
2527     MI.addImm(0);
2528   MI.addMemOperand(MMO);
2529 }
2530 
2531 void AArch64InstrInfo::loadRegFromStackSlot(
2532     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
2533     int FI, const TargetRegisterClass *RC,
2534     const TargetRegisterInfo *TRI) const {
2535   DebugLoc DL;
2536   if (MBBI != MBB.end())
2537     DL = MBBI->getDebugLoc();
2538   MachineFunction &MF = *MBB.getParent();
2539   MachineFrameInfo &MFI = MF.getFrameInfo();
2540   unsigned Align = MFI.getObjectAlignment(FI);
2541   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
2542   MachineMemOperand *MMO = MF.getMachineMemOperand(
2543       PtrInfo, MachineMemOperand::MOLoad, MFI.getObjectSize(FI), Align);
2544 
2545   unsigned Opc = 0;
2546   bool Offset = true;
2547   switch (TRI->getSpillSize(*RC)) {
2548   case 1:
2549     if (AArch64::FPR8RegClass.hasSubClassEq(RC))
2550       Opc = AArch64::LDRBui;
2551     break;
2552   case 2:
2553     if (AArch64::FPR16RegClass.hasSubClassEq(RC))
2554       Opc = AArch64::LDRHui;
2555     break;
2556   case 4:
2557     if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
2558       Opc = AArch64::LDRWui;
2559       if (TargetRegisterInfo::isVirtualRegister(DestReg))
2560         MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR32RegClass);
2561       else
2562         assert(DestReg != AArch64::WSP);
2563     } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
2564       Opc = AArch64::LDRSui;
2565     break;
2566   case 8:
2567     if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
2568       Opc = AArch64::LDRXui;
2569       if (TargetRegisterInfo::isVirtualRegister(DestReg))
2570         MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR64RegClass);
2571       else
2572         assert(DestReg != AArch64::SP);
2573     } else if (AArch64::FPR64RegClass.hasSubClassEq(RC))
2574       Opc = AArch64::LDRDui;
2575     break;
2576   case 16:
2577     if (AArch64::FPR128RegClass.hasSubClassEq(RC))
2578       Opc = AArch64::LDRQui;
2579     else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
2580       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2581       Opc = AArch64::LD1Twov1d;
2582       Offset = false;
2583     }
2584     break;
2585   case 24:
2586     if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
2587       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2588       Opc = AArch64::LD1Threev1d;
2589       Offset = false;
2590     }
2591     break;
2592   case 32:
2593     if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
2594       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2595       Opc = AArch64::LD1Fourv1d;
2596       Offset = false;
2597     } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
2598       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2599       Opc = AArch64::LD1Twov2d;
2600       Offset = false;
2601     }
2602     break;
2603   case 48:
2604     if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
2605       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2606       Opc = AArch64::LD1Threev2d;
2607       Offset = false;
2608     }
2609     break;
2610   case 64:
2611     if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
2612       assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2613       Opc = AArch64::LD1Fourv2d;
2614       Offset = false;
2615     }
2616     break;
2617   }
2618   assert(Opc && "Unknown register class");
2619 
2620   const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DL, get(Opc))
2621                                      .addReg(DestReg, getDefRegState(true))
2622                                      .addFrameIndex(FI);
2623   if (Offset)
2624     MI.addImm(0);
2625   MI.addMemOperand(MMO);
2626 }
2627 
2628 void llvm::emitFrameOffset(MachineBasicBlock &MBB,
2629                            MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
2630                            unsigned DestReg, unsigned SrcReg, int Offset,
2631                            const TargetInstrInfo *TII,
2632                            MachineInstr::MIFlag Flag, bool SetNZCV) {
2633   if (DestReg == SrcReg && Offset == 0)
2634     return;
2635 
2636   assert((DestReg != AArch64::SP || Offset % 16 == 0) &&
2637          "SP increment/decrement not 16-byte aligned");
2638 
2639   bool isSub = Offset < 0;
2640   if (isSub)
2641     Offset = -Offset;
2642 
2643   // FIXME: If the offset won't fit in 24-bits, compute the offset into a
2644   // scratch register.  If DestReg is a virtual register, use it as the
2645   // scratch register; otherwise, create a new virtual register (to be
2646   // replaced by the scavenger at the end of PEI).  That case can be optimized
2647   // slightly if DestReg is SP which is always 16-byte aligned, so the scratch
2648   // register can be loaded with offset%8 and the add/sub can use an extending
2649   // instruction with LSL#3.
2650   // Currently the function handles any offsets but generates a poor sequence
2651   // of code.
2652   //  assert(Offset < (1 << 24) && "unimplemented reg plus immediate");
2653 
2654   unsigned Opc;
2655   if (SetNZCV)
2656     Opc = isSub ? AArch64::SUBSXri : AArch64::ADDSXri;
2657   else
2658     Opc = isSub ? AArch64::SUBXri : AArch64::ADDXri;
2659   const unsigned MaxEncoding = 0xfff;
2660   const unsigned ShiftSize = 12;
2661   const unsigned MaxEncodableValue = MaxEncoding << ShiftSize;
2662   while (((unsigned)Offset) >= (1 << ShiftSize)) {
2663     unsigned ThisVal;
2664     if (((unsigned)Offset) > MaxEncodableValue) {
2665       ThisVal = MaxEncodableValue;
2666     } else {
2667       ThisVal = Offset & MaxEncodableValue;
2668     }
2669     assert((ThisVal >> ShiftSize) <= MaxEncoding &&
2670            "Encoding cannot handle value that big");
2671     BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
2672         .addReg(SrcReg)
2673         .addImm(ThisVal >> ShiftSize)
2674         .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftSize))
2675         .setMIFlag(Flag);
2676 
2677     SrcReg = DestReg;
2678     Offset -= ThisVal;
2679     if (Offset == 0)
2680       return;
2681   }
2682   BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
2683       .addReg(SrcReg)
2684       .addImm(Offset)
2685       .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0))
2686       .setMIFlag(Flag);
2687 }
2688 
2689 MachineInstr *AArch64InstrInfo::foldMemoryOperandImpl(
2690     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
2691     MachineBasicBlock::iterator InsertPt, int FrameIndex,
2692     LiveIntervals *LIS) const {
2693   // This is a bit of a hack. Consider this instruction:
2694   //
2695   //   %vreg0<def> = COPY %SP; GPR64all:%vreg0
2696   //
2697   // We explicitly chose GPR64all for the virtual register so such a copy might
2698   // be eliminated by RegisterCoalescer. However, that may not be possible, and
2699   // %vreg0 may even spill. We can't spill %SP, and since it is in the GPR64all
2700   // register class, TargetInstrInfo::foldMemoryOperand() is going to try.
2701   //
2702   // To prevent that, we are going to constrain the %vreg0 register class here.
2703   //
2704   // <rdar://problem/11522048>
2705   //
2706   if (MI.isFullCopy()) {
2707     unsigned DstReg = MI.getOperand(0).getReg();
2708     unsigned SrcReg = MI.getOperand(1).getReg();
2709     if (SrcReg == AArch64::SP &&
2710         TargetRegisterInfo::isVirtualRegister(DstReg)) {
2711       MF.getRegInfo().constrainRegClass(DstReg, &AArch64::GPR64RegClass);
2712       return nullptr;
2713     }
2714     if (DstReg == AArch64::SP &&
2715         TargetRegisterInfo::isVirtualRegister(SrcReg)) {
2716       MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
2717       return nullptr;
2718     }
2719   }
2720 
2721   // Handle the case where a copy is being spilled or filled but the source
2722   // and destination register class don't match.  For example:
2723   //
2724   //   %vreg0<def> = COPY %XZR; GPR64common:%vreg0
2725   //
2726   // In this case we can still safely fold away the COPY and generate the
2727   // following spill code:
2728   //
2729   //   STRXui %XZR, <fi#0>
2730   //
2731   // This also eliminates spilled cross register class COPYs (e.g. between x and
2732   // d regs) of the same size.  For example:
2733   //
2734   //   %vreg0<def> = COPY %vreg1; GPR64:%vreg0, FPR64:%vreg1
2735   //
2736   // will be filled as
2737   //
2738   //   LDRDui %vreg0, fi<#0>
2739   //
2740   // instead of
2741   //
2742   //   LDRXui %vregTemp, fi<#0>
2743   //   %vreg0 = FMOV %vregTemp
2744   //
2745   if (MI.isCopy() && Ops.size() == 1 &&
2746       // Make sure we're only folding the explicit COPY defs/uses.
2747       (Ops[0] == 0 || Ops[0] == 1)) {
2748     bool IsSpill = Ops[0] == 0;
2749     bool IsFill = !IsSpill;
2750     const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
2751     const MachineRegisterInfo &MRI = MF.getRegInfo();
2752     MachineBasicBlock &MBB = *MI.getParent();
2753     const MachineOperand &DstMO = MI.getOperand(0);
2754     const MachineOperand &SrcMO = MI.getOperand(1);
2755     unsigned DstReg = DstMO.getReg();
2756     unsigned SrcReg = SrcMO.getReg();
2757     // This is slightly expensive to compute for physical regs since
2758     // getMinimalPhysRegClass is slow.
2759     auto getRegClass = [&](unsigned Reg) {
2760       return TargetRegisterInfo::isVirtualRegister(Reg)
2761                  ? MRI.getRegClass(Reg)
2762                  : TRI.getMinimalPhysRegClass(Reg);
2763     };
2764 
2765     if (DstMO.getSubReg() == 0 && SrcMO.getSubReg() == 0) {
2766       assert(TRI.getRegSizeInBits(*getRegClass(DstReg)) ==
2767                  TRI.getRegSizeInBits(*getRegClass(SrcReg)) &&
2768              "Mismatched register size in non subreg COPY");
2769       if (IsSpill)
2770         storeRegToStackSlot(MBB, InsertPt, SrcReg, SrcMO.isKill(), FrameIndex,
2771                             getRegClass(SrcReg), &TRI);
2772       else
2773         loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex,
2774                              getRegClass(DstReg), &TRI);
2775       return &*--InsertPt;
2776     }
2777 
2778     // Handle cases like spilling def of:
2779     //
2780     //   %vreg0:sub_32<def,read-undef> = COPY %WZR; GPR64common:%vreg0
2781     //
2782     // where the physical register source can be widened and stored to the full
2783     // virtual reg destination stack slot, in this case producing:
2784     //
2785     //   STRXui %XZR, <fi#0>
2786     //
2787     if (IsSpill && DstMO.isUndef() &&
2788         TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
2789       assert(SrcMO.getSubReg() == 0 &&
2790              "Unexpected subreg on physical register");
2791       const TargetRegisterClass *SpillRC;
2792       unsigned SpillSubreg;
2793       switch (DstMO.getSubReg()) {
2794       default:
2795         SpillRC = nullptr;
2796         break;
2797       case AArch64::sub_32:
2798       case AArch64::ssub:
2799         if (AArch64::GPR32RegClass.contains(SrcReg)) {
2800           SpillRC = &AArch64::GPR64RegClass;
2801           SpillSubreg = AArch64::sub_32;
2802         } else if (AArch64::FPR32RegClass.contains(SrcReg)) {
2803           SpillRC = &AArch64::FPR64RegClass;
2804           SpillSubreg = AArch64::ssub;
2805         } else
2806           SpillRC = nullptr;
2807         break;
2808       case AArch64::dsub:
2809         if (AArch64::FPR64RegClass.contains(SrcReg)) {
2810           SpillRC = &AArch64::FPR128RegClass;
2811           SpillSubreg = AArch64::dsub;
2812         } else
2813           SpillRC = nullptr;
2814         break;
2815       }
2816 
2817       if (SpillRC)
2818         if (unsigned WidenedSrcReg =
2819                 TRI.getMatchingSuperReg(SrcReg, SpillSubreg, SpillRC)) {
2820           storeRegToStackSlot(MBB, InsertPt, WidenedSrcReg, SrcMO.isKill(),
2821                               FrameIndex, SpillRC, &TRI);
2822           return &*--InsertPt;
2823         }
2824     }
2825 
2826     // Handle cases like filling use of:
2827     //
2828     //   %vreg0:sub_32<def,read-undef> = COPY %vreg1; GPR64:%vreg0, GPR32:%vreg1
2829     //
2830     // where we can load the full virtual reg source stack slot, into the subreg
2831     // destination, in this case producing:
2832     //
2833     //   LDRWui %vreg0:sub_32<def,read-undef>, <fi#0>
2834     //
2835     if (IsFill && SrcMO.getSubReg() == 0 && DstMO.isUndef()) {
2836       const TargetRegisterClass *FillRC;
2837       switch (DstMO.getSubReg()) {
2838       default:
2839         FillRC = nullptr;
2840         break;
2841       case AArch64::sub_32:
2842         FillRC = &AArch64::GPR32RegClass;
2843         break;
2844       case AArch64::ssub:
2845         FillRC = &AArch64::FPR32RegClass;
2846         break;
2847       case AArch64::dsub:
2848         FillRC = &AArch64::FPR64RegClass;
2849         break;
2850       }
2851 
2852       if (FillRC) {
2853         assert(TRI.getRegSizeInBits(*getRegClass(SrcReg)) ==
2854                    TRI.getRegSizeInBits(*FillRC) &&
2855                "Mismatched regclass size on folded subreg COPY");
2856         loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex, FillRC, &TRI);
2857         MachineInstr &LoadMI = *--InsertPt;
2858         MachineOperand &LoadDst = LoadMI.getOperand(0);
2859         assert(LoadDst.getSubReg() == 0 && "unexpected subreg on fill load");
2860         LoadDst.setSubReg(DstMO.getSubReg());
2861         LoadDst.setIsUndef();
2862         return &LoadMI;
2863       }
2864     }
2865   }
2866 
2867   // Cannot fold.
2868   return nullptr;
2869 }
2870 
2871 int llvm::isAArch64FrameOffsetLegal(const MachineInstr &MI, int &Offset,
2872                                     bool *OutUseUnscaledOp,
2873                                     unsigned *OutUnscaledOp,
2874                                     int *EmittableOffset) {
2875   int Scale = 1;
2876   bool IsSigned = false;
2877   // The ImmIdx should be changed case by case if it is not 2.
2878   unsigned ImmIdx = 2;
2879   unsigned UnscaledOp = 0;
2880   // Set output values in case of early exit.
2881   if (EmittableOffset)
2882     *EmittableOffset = 0;
2883   if (OutUseUnscaledOp)
2884     *OutUseUnscaledOp = false;
2885   if (OutUnscaledOp)
2886     *OutUnscaledOp = 0;
2887   switch (MI.getOpcode()) {
2888   default:
2889     llvm_unreachable("unhandled opcode in rewriteAArch64FrameIndex");
2890   // Vector spills/fills can't take an immediate offset.
2891   case AArch64::LD1Twov2d:
2892   case AArch64::LD1Threev2d:
2893   case AArch64::LD1Fourv2d:
2894   case AArch64::LD1Twov1d:
2895   case AArch64::LD1Threev1d:
2896   case AArch64::LD1Fourv1d:
2897   case AArch64::ST1Twov2d:
2898   case AArch64::ST1Threev2d:
2899   case AArch64::ST1Fourv2d:
2900   case AArch64::ST1Twov1d:
2901   case AArch64::ST1Threev1d:
2902   case AArch64::ST1Fourv1d:
2903     return AArch64FrameOffsetCannotUpdate;
2904   case AArch64::PRFMui:
2905     Scale = 8;
2906     UnscaledOp = AArch64::PRFUMi;
2907     break;
2908   case AArch64::LDRXui:
2909     Scale = 8;
2910     UnscaledOp = AArch64::LDURXi;
2911     break;
2912   case AArch64::LDRWui:
2913     Scale = 4;
2914     UnscaledOp = AArch64::LDURWi;
2915     break;
2916   case AArch64::LDRBui:
2917     Scale = 1;
2918     UnscaledOp = AArch64::LDURBi;
2919     break;
2920   case AArch64::LDRHui:
2921     Scale = 2;
2922     UnscaledOp = AArch64::LDURHi;
2923     break;
2924   case AArch64::LDRSui:
2925     Scale = 4;
2926     UnscaledOp = AArch64::LDURSi;
2927     break;
2928   case AArch64::LDRDui:
2929     Scale = 8;
2930     UnscaledOp = AArch64::LDURDi;
2931     break;
2932   case AArch64::LDRQui:
2933     Scale = 16;
2934     UnscaledOp = AArch64::LDURQi;
2935     break;
2936   case AArch64::LDRBBui:
2937     Scale = 1;
2938     UnscaledOp = AArch64::LDURBBi;
2939     break;
2940   case AArch64::LDRHHui:
2941     Scale = 2;
2942     UnscaledOp = AArch64::LDURHHi;
2943     break;
2944   case AArch64::LDRSBXui:
2945     Scale = 1;
2946     UnscaledOp = AArch64::LDURSBXi;
2947     break;
2948   case AArch64::LDRSBWui:
2949     Scale = 1;
2950     UnscaledOp = AArch64::LDURSBWi;
2951     break;
2952   case AArch64::LDRSHXui:
2953     Scale = 2;
2954     UnscaledOp = AArch64::LDURSHXi;
2955     break;
2956   case AArch64::LDRSHWui:
2957     Scale = 2;
2958     UnscaledOp = AArch64::LDURSHWi;
2959     break;
2960   case AArch64::LDRSWui:
2961     Scale = 4;
2962     UnscaledOp = AArch64::LDURSWi;
2963     break;
2964 
2965   case AArch64::STRXui:
2966     Scale = 8;
2967     UnscaledOp = AArch64::STURXi;
2968     break;
2969   case AArch64::STRWui:
2970     Scale = 4;
2971     UnscaledOp = AArch64::STURWi;
2972     break;
2973   case AArch64::STRBui:
2974     Scale = 1;
2975     UnscaledOp = AArch64::STURBi;
2976     break;
2977   case AArch64::STRHui:
2978     Scale = 2;
2979     UnscaledOp = AArch64::STURHi;
2980     break;
2981   case AArch64::STRSui:
2982     Scale = 4;
2983     UnscaledOp = AArch64::STURSi;
2984     break;
2985   case AArch64::STRDui:
2986     Scale = 8;
2987     UnscaledOp = AArch64::STURDi;
2988     break;
2989   case AArch64::STRQui:
2990     Scale = 16;
2991     UnscaledOp = AArch64::STURQi;
2992     break;
2993   case AArch64::STRBBui:
2994     Scale = 1;
2995     UnscaledOp = AArch64::STURBBi;
2996     break;
2997   case AArch64::STRHHui:
2998     Scale = 2;
2999     UnscaledOp = AArch64::STURHHi;
3000     break;
3001 
3002   case AArch64::LDPXi:
3003   case AArch64::LDPDi:
3004   case AArch64::STPXi:
3005   case AArch64::STPDi:
3006   case AArch64::LDNPXi:
3007   case AArch64::LDNPDi:
3008   case AArch64::STNPXi:
3009   case AArch64::STNPDi:
3010     ImmIdx = 3;
3011     IsSigned = true;
3012     Scale = 8;
3013     break;
3014   case AArch64::LDPQi:
3015   case AArch64::STPQi:
3016   case AArch64::LDNPQi:
3017   case AArch64::STNPQi:
3018     ImmIdx = 3;
3019     IsSigned = true;
3020     Scale = 16;
3021     break;
3022   case AArch64::LDPWi:
3023   case AArch64::LDPSi:
3024   case AArch64::STPWi:
3025   case AArch64::STPSi:
3026   case AArch64::LDNPWi:
3027   case AArch64::LDNPSi:
3028   case AArch64::STNPWi:
3029   case AArch64::STNPSi:
3030     ImmIdx = 3;
3031     IsSigned = true;
3032     Scale = 4;
3033     break;
3034 
3035   case AArch64::LDURXi:
3036   case AArch64::LDURWi:
3037   case AArch64::LDURBi:
3038   case AArch64::LDURHi:
3039   case AArch64::LDURSi:
3040   case AArch64::LDURDi:
3041   case AArch64::LDURQi:
3042   case AArch64::LDURHHi:
3043   case AArch64::LDURBBi:
3044   case AArch64::LDURSBXi:
3045   case AArch64::LDURSBWi:
3046   case AArch64::LDURSHXi:
3047   case AArch64::LDURSHWi:
3048   case AArch64::LDURSWi:
3049   case AArch64::STURXi:
3050   case AArch64::STURWi:
3051   case AArch64::STURBi:
3052   case AArch64::STURHi:
3053   case AArch64::STURSi:
3054   case AArch64::STURDi:
3055   case AArch64::STURQi:
3056   case AArch64::STURBBi:
3057   case AArch64::STURHHi:
3058     Scale = 1;
3059     break;
3060   }
3061 
3062   Offset += MI.getOperand(ImmIdx).getImm() * Scale;
3063 
3064   bool useUnscaledOp = false;
3065   // If the offset doesn't match the scale, we rewrite the instruction to
3066   // use the unscaled instruction instead. Likewise, if we have a negative
3067   // offset (and have an unscaled op to use).
3068   if ((Offset & (Scale - 1)) != 0 || (Offset < 0 && UnscaledOp != 0))
3069     useUnscaledOp = true;
3070 
3071   // Use an unscaled addressing mode if the instruction has a negative offset
3072   // (or if the instruction is already using an unscaled addressing mode).
3073   unsigned MaskBits;
3074   if (IsSigned) {
3075     // ldp/stp instructions.
3076     MaskBits = 7;
3077     Offset /= Scale;
3078   } else if (UnscaledOp == 0 || useUnscaledOp) {
3079     MaskBits = 9;
3080     IsSigned = true;
3081     Scale = 1;
3082   } else {
3083     MaskBits = 12;
3084     IsSigned = false;
3085     Offset /= Scale;
3086   }
3087 
3088   // Attempt to fold address computation.
3089   int MaxOff = (1 << (MaskBits - IsSigned)) - 1;
3090   int MinOff = (IsSigned ? (-MaxOff - 1) : 0);
3091   if (Offset >= MinOff && Offset <= MaxOff) {
3092     if (EmittableOffset)
3093       *EmittableOffset = Offset;
3094     Offset = 0;
3095   } else {
3096     int NewOff = Offset < 0 ? MinOff : MaxOff;
3097     if (EmittableOffset)
3098       *EmittableOffset = NewOff;
3099     Offset = (Offset - NewOff) * Scale;
3100   }
3101   if (OutUseUnscaledOp)
3102     *OutUseUnscaledOp = useUnscaledOp;
3103   if (OutUnscaledOp)
3104     *OutUnscaledOp = UnscaledOp;
3105   return AArch64FrameOffsetCanUpdate |
3106          (Offset == 0 ? AArch64FrameOffsetIsLegal : 0);
3107 }
3108 
3109 bool llvm::rewriteAArch64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
3110                                     unsigned FrameReg, int &Offset,
3111                                     const AArch64InstrInfo *TII) {
3112   unsigned Opcode = MI.getOpcode();
3113   unsigned ImmIdx = FrameRegIdx + 1;
3114 
3115   if (Opcode == AArch64::ADDSXri || Opcode == AArch64::ADDXri) {
3116     Offset += MI.getOperand(ImmIdx).getImm();
3117     emitFrameOffset(*MI.getParent(), MI, MI.getDebugLoc(),
3118                     MI.getOperand(0).getReg(), FrameReg, Offset, TII,
3119                     MachineInstr::NoFlags, (Opcode == AArch64::ADDSXri));
3120     MI.eraseFromParent();
3121     Offset = 0;
3122     return true;
3123   }
3124 
3125   int NewOffset;
3126   unsigned UnscaledOp;
3127   bool UseUnscaledOp;
3128   int Status = isAArch64FrameOffsetLegal(MI, Offset, &UseUnscaledOp,
3129                                          &UnscaledOp, &NewOffset);
3130   if (Status & AArch64FrameOffsetCanUpdate) {
3131     if (Status & AArch64FrameOffsetIsLegal)
3132       // Replace the FrameIndex with FrameReg.
3133       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
3134     if (UseUnscaledOp)
3135       MI.setDesc(TII->get(UnscaledOp));
3136 
3137     MI.getOperand(ImmIdx).ChangeToImmediate(NewOffset);
3138     return Offset == 0;
3139   }
3140 
3141   return false;
3142 }
3143 
3144 void AArch64InstrInfo::getNoop(MCInst &NopInst) const {
3145   NopInst.setOpcode(AArch64::HINT);
3146   NopInst.addOperand(MCOperand::createImm(0));
3147 }
3148 
3149 // AArch64 supports MachineCombiner.
3150 bool AArch64InstrInfo::useMachineCombiner() const { return true; }
3151 
3152 // True when Opc sets flag
3153 static bool isCombineInstrSettingFlag(unsigned Opc) {
3154   switch (Opc) {
3155   case AArch64::ADDSWrr:
3156   case AArch64::ADDSWri:
3157   case AArch64::ADDSXrr:
3158   case AArch64::ADDSXri:
3159   case AArch64::SUBSWrr:
3160   case AArch64::SUBSXrr:
3161   // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
3162   case AArch64::SUBSWri:
3163   case AArch64::SUBSXri:
3164     return true;
3165   default:
3166     break;
3167   }
3168   return false;
3169 }
3170 
3171 // 32b Opcodes that can be combined with a MUL
3172 static bool isCombineInstrCandidate32(unsigned Opc) {
3173   switch (Opc) {
3174   case AArch64::ADDWrr:
3175   case AArch64::ADDWri:
3176   case AArch64::SUBWrr:
3177   case AArch64::ADDSWrr:
3178   case AArch64::ADDSWri:
3179   case AArch64::SUBSWrr:
3180   // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
3181   case AArch64::SUBWri:
3182   case AArch64::SUBSWri:
3183     return true;
3184   default:
3185     break;
3186   }
3187   return false;
3188 }
3189 
3190 // 64b Opcodes that can be combined with a MUL
3191 static bool isCombineInstrCandidate64(unsigned Opc) {
3192   switch (Opc) {
3193   case AArch64::ADDXrr:
3194   case AArch64::ADDXri:
3195   case AArch64::SUBXrr:
3196   case AArch64::ADDSXrr:
3197   case AArch64::ADDSXri:
3198   case AArch64::SUBSXrr:
3199   // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
3200   case AArch64::SUBXri:
3201   case AArch64::SUBSXri:
3202     return true;
3203   default:
3204     break;
3205   }
3206   return false;
3207 }
3208 
3209 // FP Opcodes that can be combined with a FMUL
3210 static bool isCombineInstrCandidateFP(const MachineInstr &Inst) {
3211   switch (Inst.getOpcode()) {
3212   default:
3213     break;
3214   case AArch64::FADDSrr:
3215   case AArch64::FADDDrr:
3216   case AArch64::FADDv2f32:
3217   case AArch64::FADDv2f64:
3218   case AArch64::FADDv4f32:
3219   case AArch64::FSUBSrr:
3220   case AArch64::FSUBDrr:
3221   case AArch64::FSUBv2f32:
3222   case AArch64::FSUBv2f64:
3223   case AArch64::FSUBv4f32:
3224     TargetOptions Options = Inst.getParent()->getParent()->getTarget().Options;
3225     return (Options.UnsafeFPMath ||
3226             Options.AllowFPOpFusion == FPOpFusion::Fast);
3227   }
3228   return false;
3229 }
3230 
3231 // Opcodes that can be combined with a MUL
3232 static bool isCombineInstrCandidate(unsigned Opc) {
3233   return (isCombineInstrCandidate32(Opc) || isCombineInstrCandidate64(Opc));
3234 }
3235 
3236 //
3237 // Utility routine that checks if \param MO is defined by an
3238 // \param CombineOpc instruction in the basic block \param MBB
3239 static bool canCombine(MachineBasicBlock &MBB, MachineOperand &MO,
3240                        unsigned CombineOpc, unsigned ZeroReg = 0,
3241                        bool CheckZeroReg = false) {
3242   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3243   MachineInstr *MI = nullptr;
3244 
3245   if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
3246     MI = MRI.getUniqueVRegDef(MO.getReg());
3247   // And it needs to be in the trace (otherwise, it won't have a depth).
3248   if (!MI || MI->getParent() != &MBB || (unsigned)MI->getOpcode() != CombineOpc)
3249     return false;
3250   // Must only used by the user we combine with.
3251   if (!MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
3252     return false;
3253 
3254   if (CheckZeroReg) {
3255     assert(MI->getNumOperands() >= 4 && MI->getOperand(0).isReg() &&
3256            MI->getOperand(1).isReg() && MI->getOperand(2).isReg() &&
3257            MI->getOperand(3).isReg() && "MAdd/MSub must have a least 4 regs");
3258     // The third input reg must be zero.
3259     if (MI->getOperand(3).getReg() != ZeroReg)
3260       return false;
3261   }
3262 
3263   return true;
3264 }
3265 
3266 //
3267 // Is \param MO defined by an integer multiply and can be combined?
3268 static bool canCombineWithMUL(MachineBasicBlock &MBB, MachineOperand &MO,
3269                               unsigned MulOpc, unsigned ZeroReg) {
3270   return canCombine(MBB, MO, MulOpc, ZeroReg, true);
3271 }
3272 
3273 //
3274 // Is \param MO defined by a floating-point multiply and can be combined?
3275 static bool canCombineWithFMUL(MachineBasicBlock &MBB, MachineOperand &MO,
3276                                unsigned MulOpc) {
3277   return canCombine(MBB, MO, MulOpc);
3278 }
3279 
3280 // TODO: There are many more machine instruction opcodes to match:
3281 //       1. Other data types (integer, vectors)
3282 //       2. Other math / logic operations (xor, or)
3283 //       3. Other forms of the same operation (intrinsics and other variants)
3284 bool AArch64InstrInfo::isAssociativeAndCommutative(
3285     const MachineInstr &Inst) const {
3286   switch (Inst.getOpcode()) {
3287   case AArch64::FADDDrr:
3288   case AArch64::FADDSrr:
3289   case AArch64::FADDv2f32:
3290   case AArch64::FADDv2f64:
3291   case AArch64::FADDv4f32:
3292   case AArch64::FMULDrr:
3293   case AArch64::FMULSrr:
3294   case AArch64::FMULX32:
3295   case AArch64::FMULX64:
3296   case AArch64::FMULXv2f32:
3297   case AArch64::FMULXv2f64:
3298   case AArch64::FMULXv4f32:
3299   case AArch64::FMULv2f32:
3300   case AArch64::FMULv2f64:
3301   case AArch64::FMULv4f32:
3302     return Inst.getParent()->getParent()->getTarget().Options.UnsafeFPMath;
3303   default:
3304     return false;
3305   }
3306 }
3307 
3308 /// Find instructions that can be turned into madd.
3309 static bool getMaddPatterns(MachineInstr &Root,
3310                             SmallVectorImpl<MachineCombinerPattern> &Patterns) {
3311   unsigned Opc = Root.getOpcode();
3312   MachineBasicBlock &MBB = *Root.getParent();
3313   bool Found = false;
3314 
3315   if (!isCombineInstrCandidate(Opc))
3316     return false;
3317   if (isCombineInstrSettingFlag(Opc)) {
3318     int Cmp_NZCV = Root.findRegisterDefOperandIdx(AArch64::NZCV, true);
3319     // When NZCV is live bail out.
3320     if (Cmp_NZCV == -1)
3321       return false;
3322     unsigned NewOpc = convertToNonFlagSettingOpc(Root);
3323     // When opcode can't change bail out.
3324     // CHECKME: do we miss any cases for opcode conversion?
3325     if (NewOpc == Opc)
3326       return false;
3327     Opc = NewOpc;
3328   }
3329 
3330   switch (Opc) {
3331   default:
3332     break;
3333   case AArch64::ADDWrr:
3334     assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
3335            "ADDWrr does not have register operands");
3336     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3337                           AArch64::WZR)) {
3338       Patterns.push_back(MachineCombinerPattern::MULADDW_OP1);
3339       Found = true;
3340     }
3341     if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDWrrr,
3342                           AArch64::WZR)) {
3343       Patterns.push_back(MachineCombinerPattern::MULADDW_OP2);
3344       Found = true;
3345     }
3346     break;
3347   case AArch64::ADDXrr:
3348     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3349                           AArch64::XZR)) {
3350       Patterns.push_back(MachineCombinerPattern::MULADDX_OP1);
3351       Found = true;
3352     }
3353     if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDXrrr,
3354                           AArch64::XZR)) {
3355       Patterns.push_back(MachineCombinerPattern::MULADDX_OP2);
3356       Found = true;
3357     }
3358     break;
3359   case AArch64::SUBWrr:
3360     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3361                           AArch64::WZR)) {
3362       Patterns.push_back(MachineCombinerPattern::MULSUBW_OP1);
3363       Found = true;
3364     }
3365     if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDWrrr,
3366                           AArch64::WZR)) {
3367       Patterns.push_back(MachineCombinerPattern::MULSUBW_OP2);
3368       Found = true;
3369     }
3370     break;
3371   case AArch64::SUBXrr:
3372     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3373                           AArch64::XZR)) {
3374       Patterns.push_back(MachineCombinerPattern::MULSUBX_OP1);
3375       Found = true;
3376     }
3377     if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDXrrr,
3378                           AArch64::XZR)) {
3379       Patterns.push_back(MachineCombinerPattern::MULSUBX_OP2);
3380       Found = true;
3381     }
3382     break;
3383   case AArch64::ADDWri:
3384     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3385                           AArch64::WZR)) {
3386       Patterns.push_back(MachineCombinerPattern::MULADDWI_OP1);
3387       Found = true;
3388     }
3389     break;
3390   case AArch64::ADDXri:
3391     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3392                           AArch64::XZR)) {
3393       Patterns.push_back(MachineCombinerPattern::MULADDXI_OP1);
3394       Found = true;
3395     }
3396     break;
3397   case AArch64::SUBWri:
3398     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3399                           AArch64::WZR)) {
3400       Patterns.push_back(MachineCombinerPattern::MULSUBWI_OP1);
3401       Found = true;
3402     }
3403     break;
3404   case AArch64::SUBXri:
3405     if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3406                           AArch64::XZR)) {
3407       Patterns.push_back(MachineCombinerPattern::MULSUBXI_OP1);
3408       Found = true;
3409     }
3410     break;
3411   }
3412   return Found;
3413 }
3414 /// Floating-Point Support
3415 
3416 /// Find instructions that can be turned into madd.
3417 static bool getFMAPatterns(MachineInstr &Root,
3418                            SmallVectorImpl<MachineCombinerPattern> &Patterns) {
3419 
3420   if (!isCombineInstrCandidateFP(Root))
3421     return false;
3422 
3423   MachineBasicBlock &MBB = *Root.getParent();
3424   bool Found = false;
3425 
3426   switch (Root.getOpcode()) {
3427   default:
3428     assert(false && "Unsupported FP instruction in combiner\n");
3429     break;
3430   case AArch64::FADDSrr:
3431     assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
3432            "FADDWrr does not have register operands");
3433     if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULSrr)) {
3434       Patterns.push_back(MachineCombinerPattern::FMULADDS_OP1);
3435       Found = true;
3436     } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3437                                   AArch64::FMULv1i32_indexed)) {
3438       Patterns.push_back(MachineCombinerPattern::FMLAv1i32_indexed_OP1);
3439       Found = true;
3440     }
3441     if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULSrr)) {
3442       Patterns.push_back(MachineCombinerPattern::FMULADDS_OP2);
3443       Found = true;
3444     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3445                                   AArch64::FMULv1i32_indexed)) {
3446       Patterns.push_back(MachineCombinerPattern::FMLAv1i32_indexed_OP2);
3447       Found = true;
3448     }
3449     break;
3450   case AArch64::FADDDrr:
3451     if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULDrr)) {
3452       Patterns.push_back(MachineCombinerPattern::FMULADDD_OP1);
3453       Found = true;
3454     } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3455                                   AArch64::FMULv1i64_indexed)) {
3456       Patterns.push_back(MachineCombinerPattern::FMLAv1i64_indexed_OP1);
3457       Found = true;
3458     }
3459     if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULDrr)) {
3460       Patterns.push_back(MachineCombinerPattern::FMULADDD_OP2);
3461       Found = true;
3462     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3463                                   AArch64::FMULv1i64_indexed)) {
3464       Patterns.push_back(MachineCombinerPattern::FMLAv1i64_indexed_OP2);
3465       Found = true;
3466     }
3467     break;
3468   case AArch64::FADDv2f32:
3469     if (canCombineWithFMUL(MBB, Root.getOperand(1),
3470                            AArch64::FMULv2i32_indexed)) {
3471       Patterns.push_back(MachineCombinerPattern::FMLAv2i32_indexed_OP1);
3472       Found = true;
3473     } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3474                                   AArch64::FMULv2f32)) {
3475       Patterns.push_back(MachineCombinerPattern::FMLAv2f32_OP1);
3476       Found = true;
3477     }
3478     if (canCombineWithFMUL(MBB, Root.getOperand(2),
3479                            AArch64::FMULv2i32_indexed)) {
3480       Patterns.push_back(MachineCombinerPattern::FMLAv2i32_indexed_OP2);
3481       Found = true;
3482     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3483                                   AArch64::FMULv2f32)) {
3484       Patterns.push_back(MachineCombinerPattern::FMLAv2f32_OP2);
3485       Found = true;
3486     }
3487     break;
3488   case AArch64::FADDv2f64:
3489     if (canCombineWithFMUL(MBB, Root.getOperand(1),
3490                            AArch64::FMULv2i64_indexed)) {
3491       Patterns.push_back(MachineCombinerPattern::FMLAv2i64_indexed_OP1);
3492       Found = true;
3493     } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3494                                   AArch64::FMULv2f64)) {
3495       Patterns.push_back(MachineCombinerPattern::FMLAv2f64_OP1);
3496       Found = true;
3497     }
3498     if (canCombineWithFMUL(MBB, Root.getOperand(2),
3499                            AArch64::FMULv2i64_indexed)) {
3500       Patterns.push_back(MachineCombinerPattern::FMLAv2i64_indexed_OP2);
3501       Found = true;
3502     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3503                                   AArch64::FMULv2f64)) {
3504       Patterns.push_back(MachineCombinerPattern::FMLAv2f64_OP2);
3505       Found = true;
3506     }
3507     break;
3508   case AArch64::FADDv4f32:
3509     if (canCombineWithFMUL(MBB, Root.getOperand(1),
3510                            AArch64::FMULv4i32_indexed)) {
3511       Patterns.push_back(MachineCombinerPattern::FMLAv4i32_indexed_OP1);
3512       Found = true;
3513     } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3514                                   AArch64::FMULv4f32)) {
3515       Patterns.push_back(MachineCombinerPattern::FMLAv4f32_OP1);
3516       Found = true;
3517     }
3518     if (canCombineWithFMUL(MBB, Root.getOperand(2),
3519                            AArch64::FMULv4i32_indexed)) {
3520       Patterns.push_back(MachineCombinerPattern::FMLAv4i32_indexed_OP2);
3521       Found = true;
3522     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3523                                   AArch64::FMULv4f32)) {
3524       Patterns.push_back(MachineCombinerPattern::FMLAv4f32_OP2);
3525       Found = true;
3526     }
3527     break;
3528 
3529   case AArch64::FSUBSrr:
3530     if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULSrr)) {
3531       Patterns.push_back(MachineCombinerPattern::FMULSUBS_OP1);
3532       Found = true;
3533     }
3534     if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULSrr)) {
3535       Patterns.push_back(MachineCombinerPattern::FMULSUBS_OP2);
3536       Found = true;
3537     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3538                                   AArch64::FMULv1i32_indexed)) {
3539       Patterns.push_back(MachineCombinerPattern::FMLSv1i32_indexed_OP2);
3540       Found = true;
3541     }
3542     if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FNMULSrr)) {
3543       Patterns.push_back(MachineCombinerPattern::FNMULSUBS_OP1);
3544       Found = true;
3545     }
3546     break;
3547   case AArch64::FSUBDrr:
3548     if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULDrr)) {
3549       Patterns.push_back(MachineCombinerPattern::FMULSUBD_OP1);
3550       Found = true;
3551     }
3552     if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULDrr)) {
3553       Patterns.push_back(MachineCombinerPattern::FMULSUBD_OP2);
3554       Found = true;
3555     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3556                                   AArch64::FMULv1i64_indexed)) {
3557       Patterns.push_back(MachineCombinerPattern::FMLSv1i64_indexed_OP2);
3558       Found = true;
3559     }
3560     if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FNMULDrr)) {
3561       Patterns.push_back(MachineCombinerPattern::FNMULSUBD_OP1);
3562       Found = true;
3563     }
3564     break;
3565   case AArch64::FSUBv2f32:
3566     if (canCombineWithFMUL(MBB, Root.getOperand(2),
3567                            AArch64::FMULv2i32_indexed)) {
3568       Patterns.push_back(MachineCombinerPattern::FMLSv2i32_indexed_OP2);
3569       Found = true;
3570     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3571                                   AArch64::FMULv2f32)) {
3572       Patterns.push_back(MachineCombinerPattern::FMLSv2f32_OP2);
3573       Found = true;
3574     }
3575     break;
3576   case AArch64::FSUBv2f64:
3577     if (canCombineWithFMUL(MBB, Root.getOperand(2),
3578                            AArch64::FMULv2i64_indexed)) {
3579       Patterns.push_back(MachineCombinerPattern::FMLSv2i64_indexed_OP2);
3580       Found = true;
3581     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3582                                   AArch64::FMULv2f64)) {
3583       Patterns.push_back(MachineCombinerPattern::FMLSv2f64_OP2);
3584       Found = true;
3585     }
3586     break;
3587   case AArch64::FSUBv4f32:
3588     if (canCombineWithFMUL(MBB, Root.getOperand(2),
3589                            AArch64::FMULv4i32_indexed)) {
3590       Patterns.push_back(MachineCombinerPattern::FMLSv4i32_indexed_OP2);
3591       Found = true;
3592     } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3593                                   AArch64::FMULv4f32)) {
3594       Patterns.push_back(MachineCombinerPattern::FMLSv4f32_OP2);
3595       Found = true;
3596     }
3597     break;
3598   }
3599   return Found;
3600 }
3601 
3602 /// Return true when a code sequence can improve throughput. It
3603 /// should be called only for instructions in loops.
3604 /// \param Pattern - combiner pattern
3605 bool AArch64InstrInfo::isThroughputPattern(
3606     MachineCombinerPattern Pattern) const {
3607   switch (Pattern) {
3608   default:
3609     break;
3610   case MachineCombinerPattern::FMULADDS_OP1:
3611   case MachineCombinerPattern::FMULADDS_OP2:
3612   case MachineCombinerPattern::FMULSUBS_OP1:
3613   case MachineCombinerPattern::FMULSUBS_OP2:
3614   case MachineCombinerPattern::FMULADDD_OP1:
3615   case MachineCombinerPattern::FMULADDD_OP2:
3616   case MachineCombinerPattern::FMULSUBD_OP1:
3617   case MachineCombinerPattern::FMULSUBD_OP2:
3618   case MachineCombinerPattern::FNMULSUBS_OP1:
3619   case MachineCombinerPattern::FNMULSUBD_OP1:
3620   case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
3621   case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
3622   case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
3623   case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
3624   case MachineCombinerPattern::FMLAv2f32_OP2:
3625   case MachineCombinerPattern::FMLAv2f32_OP1:
3626   case MachineCombinerPattern::FMLAv2f64_OP1:
3627   case MachineCombinerPattern::FMLAv2f64_OP2:
3628   case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
3629   case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
3630   case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
3631   case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
3632   case MachineCombinerPattern::FMLAv4f32_OP1:
3633   case MachineCombinerPattern::FMLAv4f32_OP2:
3634   case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
3635   case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
3636   case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
3637   case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
3638   case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
3639   case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
3640   case MachineCombinerPattern::FMLSv2f32_OP2:
3641   case MachineCombinerPattern::FMLSv2f64_OP2:
3642   case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
3643   case MachineCombinerPattern::FMLSv4f32_OP2:
3644     return true;
3645   } // end switch (Pattern)
3646   return false;
3647 }
3648 /// Return true when there is potentially a faster code sequence for an
3649 /// instruction chain ending in \p Root. All potential patterns are listed in
3650 /// the \p Pattern vector. Pattern should be sorted in priority order since the
3651 /// pattern evaluator stops checking as soon as it finds a faster sequence.
3652 
3653 bool AArch64InstrInfo::getMachineCombinerPatterns(
3654     MachineInstr &Root,
3655     SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
3656   // Integer patterns
3657   if (getMaddPatterns(Root, Patterns))
3658     return true;
3659   // Floating point patterns
3660   if (getFMAPatterns(Root, Patterns))
3661     return true;
3662 
3663   return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
3664 }
3665 
3666 enum class FMAInstKind { Default, Indexed, Accumulator };
3667 /// genFusedMultiply - Generate fused multiply instructions.
3668 /// This function supports both integer and floating point instructions.
3669 /// A typical example:
3670 ///  F|MUL I=A,B,0
3671 ///  F|ADD R,I,C
3672 ///  ==> F|MADD R,A,B,C
3673 /// \param MF Containing MachineFunction
3674 /// \param MRI Register information
3675 /// \param TII Target information
3676 /// \param Root is the F|ADD instruction
3677 /// \param [out] InsInstrs is a vector of machine instructions and will
3678 /// contain the generated madd instruction
3679 /// \param IdxMulOpd is index of operand in Root that is the result of
3680 /// the F|MUL. In the example above IdxMulOpd is 1.
3681 /// \param MaddOpc the opcode fo the f|madd instruction
3682 /// \param RC Register class of operands
3683 /// \param kind of fma instruction (addressing mode) to be generated
3684 static MachineInstr *
3685 genFusedMultiply(MachineFunction &MF, MachineRegisterInfo &MRI,
3686                  const TargetInstrInfo *TII, MachineInstr &Root,
3687                  SmallVectorImpl<MachineInstr *> &InsInstrs, unsigned IdxMulOpd,
3688                  unsigned MaddOpc, const TargetRegisterClass *RC,
3689                  FMAInstKind kind = FMAInstKind::Default) {
3690   assert(IdxMulOpd == 1 || IdxMulOpd == 2);
3691 
3692   unsigned IdxOtherOpd = IdxMulOpd == 1 ? 2 : 1;
3693   MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
3694   unsigned ResultReg = Root.getOperand(0).getReg();
3695   unsigned SrcReg0 = MUL->getOperand(1).getReg();
3696   bool Src0IsKill = MUL->getOperand(1).isKill();
3697   unsigned SrcReg1 = MUL->getOperand(2).getReg();
3698   bool Src1IsKill = MUL->getOperand(2).isKill();
3699   unsigned SrcReg2 = Root.getOperand(IdxOtherOpd).getReg();
3700   bool Src2IsKill = Root.getOperand(IdxOtherOpd).isKill();
3701 
3702   if (TargetRegisterInfo::isVirtualRegister(ResultReg))
3703     MRI.constrainRegClass(ResultReg, RC);
3704   if (TargetRegisterInfo::isVirtualRegister(SrcReg0))
3705     MRI.constrainRegClass(SrcReg0, RC);
3706   if (TargetRegisterInfo::isVirtualRegister(SrcReg1))
3707     MRI.constrainRegClass(SrcReg1, RC);
3708   if (TargetRegisterInfo::isVirtualRegister(SrcReg2))
3709     MRI.constrainRegClass(SrcReg2, RC);
3710 
3711   MachineInstrBuilder MIB;
3712   if (kind == FMAInstKind::Default)
3713     MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
3714               .addReg(SrcReg0, getKillRegState(Src0IsKill))
3715               .addReg(SrcReg1, getKillRegState(Src1IsKill))
3716               .addReg(SrcReg2, getKillRegState(Src2IsKill));
3717   else if (kind == FMAInstKind::Indexed)
3718     MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
3719               .addReg(SrcReg2, getKillRegState(Src2IsKill))
3720               .addReg(SrcReg0, getKillRegState(Src0IsKill))
3721               .addReg(SrcReg1, getKillRegState(Src1IsKill))
3722               .addImm(MUL->getOperand(3).getImm());
3723   else if (kind == FMAInstKind::Accumulator)
3724     MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
3725               .addReg(SrcReg2, getKillRegState(Src2IsKill))
3726               .addReg(SrcReg0, getKillRegState(Src0IsKill))
3727               .addReg(SrcReg1, getKillRegState(Src1IsKill));
3728   else
3729     assert(false && "Invalid FMA instruction kind \n");
3730   // Insert the MADD (MADD, FMA, FMS, FMLA, FMSL)
3731   InsInstrs.push_back(MIB);
3732   return MUL;
3733 }
3734 
3735 /// genMaddR - Generate madd instruction and combine mul and add using
3736 /// an extra virtual register
3737 /// Example - an ADD intermediate needs to be stored in a register:
3738 ///   MUL I=A,B,0
3739 ///   ADD R,I,Imm
3740 ///   ==> ORR  V, ZR, Imm
3741 ///   ==> MADD R,A,B,V
3742 /// \param MF Containing MachineFunction
3743 /// \param MRI Register information
3744 /// \param TII Target information
3745 /// \param Root is the ADD instruction
3746 /// \param [out] InsInstrs is a vector of machine instructions and will
3747 /// contain the generated madd instruction
3748 /// \param IdxMulOpd is index of operand in Root that is the result of
3749 /// the MUL. In the example above IdxMulOpd is 1.
3750 /// \param MaddOpc the opcode fo the madd instruction
3751 /// \param VR is a virtual register that holds the value of an ADD operand
3752 /// (V in the example above).
3753 /// \param RC Register class of operands
3754 static MachineInstr *genMaddR(MachineFunction &MF, MachineRegisterInfo &MRI,
3755                               const TargetInstrInfo *TII, MachineInstr &Root,
3756                               SmallVectorImpl<MachineInstr *> &InsInstrs,
3757                               unsigned IdxMulOpd, unsigned MaddOpc, unsigned VR,
3758                               const TargetRegisterClass *RC) {
3759   assert(IdxMulOpd == 1 || IdxMulOpd == 2);
3760 
3761   MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
3762   unsigned ResultReg = Root.getOperand(0).getReg();
3763   unsigned SrcReg0 = MUL->getOperand(1).getReg();
3764   bool Src0IsKill = MUL->getOperand(1).isKill();
3765   unsigned SrcReg1 = MUL->getOperand(2).getReg();
3766   bool Src1IsKill = MUL->getOperand(2).isKill();
3767 
3768   if (TargetRegisterInfo::isVirtualRegister(ResultReg))
3769     MRI.constrainRegClass(ResultReg, RC);
3770   if (TargetRegisterInfo::isVirtualRegister(SrcReg0))
3771     MRI.constrainRegClass(SrcReg0, RC);
3772   if (TargetRegisterInfo::isVirtualRegister(SrcReg1))
3773     MRI.constrainRegClass(SrcReg1, RC);
3774   if (TargetRegisterInfo::isVirtualRegister(VR))
3775     MRI.constrainRegClass(VR, RC);
3776 
3777   MachineInstrBuilder MIB =
3778       BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
3779           .addReg(SrcReg0, getKillRegState(Src0IsKill))
3780           .addReg(SrcReg1, getKillRegState(Src1IsKill))
3781           .addReg(VR);
3782   // Insert the MADD
3783   InsInstrs.push_back(MIB);
3784   return MUL;
3785 }
3786 
3787 /// When getMachineCombinerPatterns() finds potential patterns,
3788 /// this function generates the instructions that could replace the
3789 /// original code sequence
3790 void AArch64InstrInfo::genAlternativeCodeSequence(
3791     MachineInstr &Root, MachineCombinerPattern Pattern,
3792     SmallVectorImpl<MachineInstr *> &InsInstrs,
3793     SmallVectorImpl<MachineInstr *> &DelInstrs,
3794     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
3795   MachineBasicBlock &MBB = *Root.getParent();
3796   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3797   MachineFunction &MF = *MBB.getParent();
3798   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
3799 
3800   MachineInstr *MUL;
3801   const TargetRegisterClass *RC;
3802   unsigned Opc;
3803   switch (Pattern) {
3804   default:
3805     // Reassociate instructions.
3806     TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs,
3807                                                 DelInstrs, InstrIdxForVirtReg);
3808     return;
3809   case MachineCombinerPattern::MULADDW_OP1:
3810   case MachineCombinerPattern::MULADDX_OP1:
3811     // MUL I=A,B,0
3812     // ADD R,I,C
3813     // ==> MADD R,A,B,C
3814     // --- Create(MADD);
3815     if (Pattern == MachineCombinerPattern::MULADDW_OP1) {
3816       Opc = AArch64::MADDWrrr;
3817       RC = &AArch64::GPR32RegClass;
3818     } else {
3819       Opc = AArch64::MADDXrrr;
3820       RC = &AArch64::GPR64RegClass;
3821     }
3822     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
3823     break;
3824   case MachineCombinerPattern::MULADDW_OP2:
3825   case MachineCombinerPattern::MULADDX_OP2:
3826     // MUL I=A,B,0
3827     // ADD R,C,I
3828     // ==> MADD R,A,B,C
3829     // --- Create(MADD);
3830     if (Pattern == MachineCombinerPattern::MULADDW_OP2) {
3831       Opc = AArch64::MADDWrrr;
3832       RC = &AArch64::GPR32RegClass;
3833     } else {
3834       Opc = AArch64::MADDXrrr;
3835       RC = &AArch64::GPR64RegClass;
3836     }
3837     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
3838     break;
3839   case MachineCombinerPattern::MULADDWI_OP1:
3840   case MachineCombinerPattern::MULADDXI_OP1: {
3841     // MUL I=A,B,0
3842     // ADD R,I,Imm
3843     // ==> ORR  V, ZR, Imm
3844     // ==> MADD R,A,B,V
3845     // --- Create(MADD);
3846     const TargetRegisterClass *OrrRC;
3847     unsigned BitSize, OrrOpc, ZeroReg;
3848     if (Pattern == MachineCombinerPattern::MULADDWI_OP1) {
3849       OrrOpc = AArch64::ORRWri;
3850       OrrRC = &AArch64::GPR32spRegClass;
3851       BitSize = 32;
3852       ZeroReg = AArch64::WZR;
3853       Opc = AArch64::MADDWrrr;
3854       RC = &AArch64::GPR32RegClass;
3855     } else {
3856       OrrOpc = AArch64::ORRXri;
3857       OrrRC = &AArch64::GPR64spRegClass;
3858       BitSize = 64;
3859       ZeroReg = AArch64::XZR;
3860       Opc = AArch64::MADDXrrr;
3861       RC = &AArch64::GPR64RegClass;
3862     }
3863     unsigned NewVR = MRI.createVirtualRegister(OrrRC);
3864     uint64_t Imm = Root.getOperand(2).getImm();
3865 
3866     if (Root.getOperand(3).isImm()) {
3867       unsigned Val = Root.getOperand(3).getImm();
3868       Imm = Imm << Val;
3869     }
3870     uint64_t UImm = SignExtend64(Imm, BitSize);
3871     uint64_t Encoding;
3872     if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
3873       MachineInstrBuilder MIB1 =
3874           BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
3875               .addReg(ZeroReg)
3876               .addImm(Encoding);
3877       InsInstrs.push_back(MIB1);
3878       InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
3879       MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
3880     }
3881     break;
3882   }
3883   case MachineCombinerPattern::MULSUBW_OP1:
3884   case MachineCombinerPattern::MULSUBX_OP1: {
3885     // MUL I=A,B,0
3886     // SUB R,I, C
3887     // ==> SUB  V, 0, C
3888     // ==> MADD R,A,B,V // = -C + A*B
3889     // --- Create(MADD);
3890     const TargetRegisterClass *SubRC;
3891     unsigned SubOpc, ZeroReg;
3892     if (Pattern == MachineCombinerPattern::MULSUBW_OP1) {
3893       SubOpc = AArch64::SUBWrr;
3894       SubRC = &AArch64::GPR32spRegClass;
3895       ZeroReg = AArch64::WZR;
3896       Opc = AArch64::MADDWrrr;
3897       RC = &AArch64::GPR32RegClass;
3898     } else {
3899       SubOpc = AArch64::SUBXrr;
3900       SubRC = &AArch64::GPR64spRegClass;
3901       ZeroReg = AArch64::XZR;
3902       Opc = AArch64::MADDXrrr;
3903       RC = &AArch64::GPR64RegClass;
3904     }
3905     unsigned NewVR = MRI.createVirtualRegister(SubRC);
3906     // SUB NewVR, 0, C
3907     MachineInstrBuilder MIB1 =
3908         BuildMI(MF, Root.getDebugLoc(), TII->get(SubOpc), NewVR)
3909             .addReg(ZeroReg)
3910             .add(Root.getOperand(2));
3911     InsInstrs.push_back(MIB1);
3912     InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
3913     MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
3914     break;
3915   }
3916   case MachineCombinerPattern::MULSUBW_OP2:
3917   case MachineCombinerPattern::MULSUBX_OP2:
3918     // MUL I=A,B,0
3919     // SUB R,C,I
3920     // ==> MSUB R,A,B,C (computes C - A*B)
3921     // --- Create(MSUB);
3922     if (Pattern == MachineCombinerPattern::MULSUBW_OP2) {
3923       Opc = AArch64::MSUBWrrr;
3924       RC = &AArch64::GPR32RegClass;
3925     } else {
3926       Opc = AArch64::MSUBXrrr;
3927       RC = &AArch64::GPR64RegClass;
3928     }
3929     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
3930     break;
3931   case MachineCombinerPattern::MULSUBWI_OP1:
3932   case MachineCombinerPattern::MULSUBXI_OP1: {
3933     // MUL I=A,B,0
3934     // SUB R,I, Imm
3935     // ==> ORR  V, ZR, -Imm
3936     // ==> MADD R,A,B,V // = -Imm + A*B
3937     // --- Create(MADD);
3938     const TargetRegisterClass *OrrRC;
3939     unsigned BitSize, OrrOpc, ZeroReg;
3940     if (Pattern == MachineCombinerPattern::MULSUBWI_OP1) {
3941       OrrOpc = AArch64::ORRWri;
3942       OrrRC = &AArch64::GPR32spRegClass;
3943       BitSize = 32;
3944       ZeroReg = AArch64::WZR;
3945       Opc = AArch64::MADDWrrr;
3946       RC = &AArch64::GPR32RegClass;
3947     } else {
3948       OrrOpc = AArch64::ORRXri;
3949       OrrRC = &AArch64::GPR64spRegClass;
3950       BitSize = 64;
3951       ZeroReg = AArch64::XZR;
3952       Opc = AArch64::MADDXrrr;
3953       RC = &AArch64::GPR64RegClass;
3954     }
3955     unsigned NewVR = MRI.createVirtualRegister(OrrRC);
3956     uint64_t Imm = Root.getOperand(2).getImm();
3957     if (Root.getOperand(3).isImm()) {
3958       unsigned Val = Root.getOperand(3).getImm();
3959       Imm = Imm << Val;
3960     }
3961     uint64_t UImm = SignExtend64(-Imm, BitSize);
3962     uint64_t Encoding;
3963     if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
3964       MachineInstrBuilder MIB1 =
3965           BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
3966               .addReg(ZeroReg)
3967               .addImm(Encoding);
3968       InsInstrs.push_back(MIB1);
3969       InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
3970       MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
3971     }
3972     break;
3973   }
3974   // Floating Point Support
3975   case MachineCombinerPattern::FMULADDS_OP1:
3976   case MachineCombinerPattern::FMULADDD_OP1:
3977     // MUL I=A,B,0
3978     // ADD R,I,C
3979     // ==> MADD R,A,B,C
3980     // --- Create(MADD);
3981     if (Pattern == MachineCombinerPattern::FMULADDS_OP1) {
3982       Opc = AArch64::FMADDSrrr;
3983       RC = &AArch64::FPR32RegClass;
3984     } else {
3985       Opc = AArch64::FMADDDrrr;
3986       RC = &AArch64::FPR64RegClass;
3987     }
3988     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
3989     break;
3990   case MachineCombinerPattern::FMULADDS_OP2:
3991   case MachineCombinerPattern::FMULADDD_OP2:
3992     // FMUL I=A,B,0
3993     // FADD R,C,I
3994     // ==> FMADD R,A,B,C
3995     // --- Create(FMADD);
3996     if (Pattern == MachineCombinerPattern::FMULADDS_OP2) {
3997       Opc = AArch64::FMADDSrrr;
3998       RC = &AArch64::FPR32RegClass;
3999     } else {
4000       Opc = AArch64::FMADDDrrr;
4001       RC = &AArch64::FPR64RegClass;
4002     }
4003     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4004     break;
4005 
4006   case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
4007     Opc = AArch64::FMLAv1i32_indexed;
4008     RC = &AArch64::FPR32RegClass;
4009     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4010                            FMAInstKind::Indexed);
4011     break;
4012   case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
4013     Opc = AArch64::FMLAv1i32_indexed;
4014     RC = &AArch64::FPR32RegClass;
4015     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4016                            FMAInstKind::Indexed);
4017     break;
4018 
4019   case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
4020     Opc = AArch64::FMLAv1i64_indexed;
4021     RC = &AArch64::FPR64RegClass;
4022     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4023                            FMAInstKind::Indexed);
4024     break;
4025   case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
4026     Opc = AArch64::FMLAv1i64_indexed;
4027     RC = &AArch64::FPR64RegClass;
4028     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4029                            FMAInstKind::Indexed);
4030     break;
4031 
4032   case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
4033   case MachineCombinerPattern::FMLAv2f32_OP1:
4034     RC = &AArch64::FPR64RegClass;
4035     if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP1) {
4036       Opc = AArch64::FMLAv2i32_indexed;
4037       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4038                              FMAInstKind::Indexed);
4039     } else {
4040       Opc = AArch64::FMLAv2f32;
4041       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4042                              FMAInstKind::Accumulator);
4043     }
4044     break;
4045   case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
4046   case MachineCombinerPattern::FMLAv2f32_OP2:
4047     RC = &AArch64::FPR64RegClass;
4048     if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP2) {
4049       Opc = AArch64::FMLAv2i32_indexed;
4050       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4051                              FMAInstKind::Indexed);
4052     } else {
4053       Opc = AArch64::FMLAv2f32;
4054       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4055                              FMAInstKind::Accumulator);
4056     }
4057     break;
4058 
4059   case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
4060   case MachineCombinerPattern::FMLAv2f64_OP1:
4061     RC = &AArch64::FPR128RegClass;
4062     if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP1) {
4063       Opc = AArch64::FMLAv2i64_indexed;
4064       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4065                              FMAInstKind::Indexed);
4066     } else {
4067       Opc = AArch64::FMLAv2f64;
4068       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4069                              FMAInstKind::Accumulator);
4070     }
4071     break;
4072   case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
4073   case MachineCombinerPattern::FMLAv2f64_OP2:
4074     RC = &AArch64::FPR128RegClass;
4075     if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP2) {
4076       Opc = AArch64::FMLAv2i64_indexed;
4077       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4078                              FMAInstKind::Indexed);
4079     } else {
4080       Opc = AArch64::FMLAv2f64;
4081       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4082                              FMAInstKind::Accumulator);
4083     }
4084     break;
4085 
4086   case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
4087   case MachineCombinerPattern::FMLAv4f32_OP1:
4088     RC = &AArch64::FPR128RegClass;
4089     if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP1) {
4090       Opc = AArch64::FMLAv4i32_indexed;
4091       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4092                              FMAInstKind::Indexed);
4093     } else {
4094       Opc = AArch64::FMLAv4f32;
4095       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4096                              FMAInstKind::Accumulator);
4097     }
4098     break;
4099 
4100   case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
4101   case MachineCombinerPattern::FMLAv4f32_OP2:
4102     RC = &AArch64::FPR128RegClass;
4103     if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP2) {
4104       Opc = AArch64::FMLAv4i32_indexed;
4105       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4106                              FMAInstKind::Indexed);
4107     } else {
4108       Opc = AArch64::FMLAv4f32;
4109       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4110                              FMAInstKind::Accumulator);
4111     }
4112     break;
4113 
4114   case MachineCombinerPattern::FMULSUBS_OP1:
4115   case MachineCombinerPattern::FMULSUBD_OP1: {
4116     // FMUL I=A,B,0
4117     // FSUB R,I,C
4118     // ==> FNMSUB R,A,B,C // = -C + A*B
4119     // --- Create(FNMSUB);
4120     if (Pattern == MachineCombinerPattern::FMULSUBS_OP1) {
4121       Opc = AArch64::FNMSUBSrrr;
4122       RC = &AArch64::FPR32RegClass;
4123     } else {
4124       Opc = AArch64::FNMSUBDrrr;
4125       RC = &AArch64::FPR64RegClass;
4126     }
4127     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4128     break;
4129   }
4130 
4131   case MachineCombinerPattern::FNMULSUBS_OP1:
4132   case MachineCombinerPattern::FNMULSUBD_OP1: {
4133     // FNMUL I=A,B,0
4134     // FSUB R,I,C
4135     // ==> FNMADD R,A,B,C // = -A*B - C
4136     // --- Create(FNMADD);
4137     if (Pattern == MachineCombinerPattern::FNMULSUBS_OP1) {
4138       Opc = AArch64::FNMADDSrrr;
4139       RC = &AArch64::FPR32RegClass;
4140     } else {
4141       Opc = AArch64::FNMADDDrrr;
4142       RC = &AArch64::FPR64RegClass;
4143     }
4144     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4145     break;
4146   }
4147 
4148   case MachineCombinerPattern::FMULSUBS_OP2:
4149   case MachineCombinerPattern::FMULSUBD_OP2: {
4150     // FMUL I=A,B,0
4151     // FSUB R,C,I
4152     // ==> FMSUB R,A,B,C (computes C - A*B)
4153     // --- Create(FMSUB);
4154     if (Pattern == MachineCombinerPattern::FMULSUBS_OP2) {
4155       Opc = AArch64::FMSUBSrrr;
4156       RC = &AArch64::FPR32RegClass;
4157     } else {
4158       Opc = AArch64::FMSUBDrrr;
4159       RC = &AArch64::FPR64RegClass;
4160     }
4161     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4162     break;
4163   }
4164 
4165   case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
4166     Opc = AArch64::FMLSv1i32_indexed;
4167     RC = &AArch64::FPR32RegClass;
4168     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4169                            FMAInstKind::Indexed);
4170     break;
4171 
4172   case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
4173     Opc = AArch64::FMLSv1i64_indexed;
4174     RC = &AArch64::FPR64RegClass;
4175     MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4176                            FMAInstKind::Indexed);
4177     break;
4178 
4179   case MachineCombinerPattern::FMLSv2f32_OP2:
4180   case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
4181     RC = &AArch64::FPR64RegClass;
4182     if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP2) {
4183       Opc = AArch64::FMLSv2i32_indexed;
4184       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4185                              FMAInstKind::Indexed);
4186     } else {
4187       Opc = AArch64::FMLSv2f32;
4188       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4189                              FMAInstKind::Accumulator);
4190     }
4191     break;
4192 
4193   case MachineCombinerPattern::FMLSv2f64_OP2:
4194   case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
4195     RC = &AArch64::FPR128RegClass;
4196     if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP2) {
4197       Opc = AArch64::FMLSv2i64_indexed;
4198       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4199                              FMAInstKind::Indexed);
4200     } else {
4201       Opc = AArch64::FMLSv2f64;
4202       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4203                              FMAInstKind::Accumulator);
4204     }
4205     break;
4206 
4207   case MachineCombinerPattern::FMLSv4f32_OP2:
4208   case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
4209     RC = &AArch64::FPR128RegClass;
4210     if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP2) {
4211       Opc = AArch64::FMLSv4i32_indexed;
4212       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4213                              FMAInstKind::Indexed);
4214     } else {
4215       Opc = AArch64::FMLSv4f32;
4216       MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4217                              FMAInstKind::Accumulator);
4218     }
4219     break;
4220   } // end switch (Pattern)
4221   // Record MUL and ADD/SUB for deletion
4222   DelInstrs.push_back(MUL);
4223   DelInstrs.push_back(&Root);
4224 }
4225 
4226 /// \brief Replace csincr-branch sequence by simple conditional branch
4227 ///
4228 /// Examples:
4229 /// 1. \code
4230 ///   csinc  w9, wzr, wzr, <condition code>
4231 ///   tbnz   w9, #0, 0x44
4232 ///    \endcode
4233 /// to
4234 ///    \code
4235 ///   b.<inverted condition code>
4236 ///    \endcode
4237 ///
4238 /// 2. \code
4239 ///   csinc w9, wzr, wzr, <condition code>
4240 ///   tbz   w9, #0, 0x44
4241 ///    \endcode
4242 /// to
4243 ///    \code
4244 ///   b.<condition code>
4245 ///    \endcode
4246 ///
4247 /// Replace compare and branch sequence by TBZ/TBNZ instruction when the
4248 /// compare's constant operand is power of 2.
4249 ///
4250 /// Examples:
4251 ///    \code
4252 ///   and  w8, w8, #0x400
4253 ///   cbnz w8, L1
4254 ///    \endcode
4255 /// to
4256 ///    \code
4257 ///   tbnz w8, #10, L1
4258 ///    \endcode
4259 ///
4260 /// \param  MI Conditional Branch
4261 /// \return True when the simple conditional branch is generated
4262 ///
4263 bool AArch64InstrInfo::optimizeCondBranch(MachineInstr &MI) const {
4264   bool IsNegativeBranch = false;
4265   bool IsTestAndBranch = false;
4266   unsigned TargetBBInMI = 0;
4267   switch (MI.getOpcode()) {
4268   default:
4269     llvm_unreachable("Unknown branch instruction?");
4270   case AArch64::Bcc:
4271     return false;
4272   case AArch64::CBZW:
4273   case AArch64::CBZX:
4274     TargetBBInMI = 1;
4275     break;
4276   case AArch64::CBNZW:
4277   case AArch64::CBNZX:
4278     TargetBBInMI = 1;
4279     IsNegativeBranch = true;
4280     break;
4281   case AArch64::TBZW:
4282   case AArch64::TBZX:
4283     TargetBBInMI = 2;
4284     IsTestAndBranch = true;
4285     break;
4286   case AArch64::TBNZW:
4287   case AArch64::TBNZX:
4288     TargetBBInMI = 2;
4289     IsNegativeBranch = true;
4290     IsTestAndBranch = true;
4291     break;
4292   }
4293   // So we increment a zero register and test for bits other
4294   // than bit 0? Conservatively bail out in case the verifier
4295   // missed this case.
4296   if (IsTestAndBranch && MI.getOperand(1).getImm())
4297     return false;
4298 
4299   // Find Definition.
4300   assert(MI.getParent() && "Incomplete machine instruciton\n");
4301   MachineBasicBlock *MBB = MI.getParent();
4302   MachineFunction *MF = MBB->getParent();
4303   MachineRegisterInfo *MRI = &MF->getRegInfo();
4304   unsigned VReg = MI.getOperand(0).getReg();
4305   if (!TargetRegisterInfo::isVirtualRegister(VReg))
4306     return false;
4307 
4308   MachineInstr *DefMI = MRI->getVRegDef(VReg);
4309 
4310   // Look through COPY instructions to find definition.
4311   while (DefMI->isCopy()) {
4312     unsigned CopyVReg = DefMI->getOperand(1).getReg();
4313     if (!MRI->hasOneNonDBGUse(CopyVReg))
4314       return false;
4315     if (!MRI->hasOneDef(CopyVReg))
4316       return false;
4317     DefMI = MRI->getVRegDef(CopyVReg);
4318   }
4319 
4320   switch (DefMI->getOpcode()) {
4321   default:
4322     return false;
4323   // Fold AND into a TBZ/TBNZ if constant operand is power of 2.
4324   case AArch64::ANDWri:
4325   case AArch64::ANDXri: {
4326     if (IsTestAndBranch)
4327       return false;
4328     if (DefMI->getParent() != MBB)
4329       return false;
4330     if (!MRI->hasOneNonDBGUse(VReg))
4331       return false;
4332 
4333     bool Is32Bit = (DefMI->getOpcode() == AArch64::ANDWri);
4334     uint64_t Mask = AArch64_AM::decodeLogicalImmediate(
4335         DefMI->getOperand(2).getImm(), Is32Bit ? 32 : 64);
4336     if (!isPowerOf2_64(Mask))
4337       return false;
4338 
4339     MachineOperand &MO = DefMI->getOperand(1);
4340     unsigned NewReg = MO.getReg();
4341     if (!TargetRegisterInfo::isVirtualRegister(NewReg))
4342       return false;
4343 
4344     assert(!MRI->def_empty(NewReg) && "Register must be defined.");
4345 
4346     MachineBasicBlock &RefToMBB = *MBB;
4347     MachineBasicBlock *TBB = MI.getOperand(1).getMBB();
4348     DebugLoc DL = MI.getDebugLoc();
4349     unsigned Imm = Log2_64(Mask);
4350     unsigned Opc = (Imm < 32)
4351                        ? (IsNegativeBranch ? AArch64::TBNZW : AArch64::TBZW)
4352                        : (IsNegativeBranch ? AArch64::TBNZX : AArch64::TBZX);
4353     MachineInstr *NewMI = BuildMI(RefToMBB, MI, DL, get(Opc))
4354                               .addReg(NewReg)
4355                               .addImm(Imm)
4356                               .addMBB(TBB);
4357     // Register lives on to the CBZ now.
4358     MO.setIsKill(false);
4359 
4360     // For immediate smaller than 32, we need to use the 32-bit
4361     // variant (W) in all cases. Indeed the 64-bit variant does not
4362     // allow to encode them.
4363     // Therefore, if the input register is 64-bit, we need to take the
4364     // 32-bit sub-part.
4365     if (!Is32Bit && Imm < 32)
4366       NewMI->getOperand(0).setSubReg(AArch64::sub_32);
4367     MI.eraseFromParent();
4368     return true;
4369   }
4370   // Look for CSINC
4371   case AArch64::CSINCWr:
4372   case AArch64::CSINCXr: {
4373     if (!(DefMI->getOperand(1).getReg() == AArch64::WZR &&
4374           DefMI->getOperand(2).getReg() == AArch64::WZR) &&
4375         !(DefMI->getOperand(1).getReg() == AArch64::XZR &&
4376           DefMI->getOperand(2).getReg() == AArch64::XZR))
4377       return false;
4378 
4379     if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) != -1)
4380       return false;
4381 
4382     AArch64CC::CondCode CC = (AArch64CC::CondCode)DefMI->getOperand(3).getImm();
4383     // Convert only when the condition code is not modified between
4384     // the CSINC and the branch. The CC may be used by other
4385     // instructions in between.
4386     if (areCFlagsAccessedBetweenInstrs(DefMI, MI, &getRegisterInfo(), AK_Write))
4387       return false;
4388     MachineBasicBlock &RefToMBB = *MBB;
4389     MachineBasicBlock *TBB = MI.getOperand(TargetBBInMI).getMBB();
4390     DebugLoc DL = MI.getDebugLoc();
4391     if (IsNegativeBranch)
4392       CC = AArch64CC::getInvertedCondCode(CC);
4393     BuildMI(RefToMBB, MI, DL, get(AArch64::Bcc)).addImm(CC).addMBB(TBB);
4394     MI.eraseFromParent();
4395     return true;
4396   }
4397   }
4398 }
4399 
4400 std::pair<unsigned, unsigned>
4401 AArch64InstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
4402   const unsigned Mask = AArch64II::MO_FRAGMENT;
4403   return std::make_pair(TF & Mask, TF & ~Mask);
4404 }
4405 
4406 ArrayRef<std::pair<unsigned, const char *>>
4407 AArch64InstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
4408   using namespace AArch64II;
4409 
4410   static const std::pair<unsigned, const char *> TargetFlags[] = {
4411       {MO_PAGE, "aarch64-page"}, {MO_PAGEOFF, "aarch64-pageoff"},
4412       {MO_G3, "aarch64-g3"},     {MO_G2, "aarch64-g2"},
4413       {MO_G1, "aarch64-g1"},     {MO_G0, "aarch64-g0"},
4414       {MO_HI12, "aarch64-hi12"}};
4415   return makeArrayRef(TargetFlags);
4416 }
4417 
4418 ArrayRef<std::pair<unsigned, const char *>>
4419 AArch64InstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
4420   using namespace AArch64II;
4421 
4422   static const std::pair<unsigned, const char *> TargetFlags[] = {
4423       {MO_GOT, "aarch64-got"}, {MO_NC, "aarch64-nc"}, {MO_TLS, "aarch64-tls"}};
4424   return makeArrayRef(TargetFlags);
4425 }
4426 
4427 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
4428 AArch64InstrInfo::getSerializableMachineMemOperandTargetFlags() const {
4429   static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
4430       {{MOSuppressPair, "aarch64-suppress-pair"},
4431        {MOStridedAccess, "aarch64-strided-access"}};
4432   return makeArrayRef(TargetFlags);
4433 }
4434 
4435 // Constants defining how certain sequences should be outlined.
4436 const unsigned MachineOutlinerDefaultFn = 0;
4437 const unsigned MachineOutlinerTailCallFn = 1;
4438 
4439 std::pair<size_t, unsigned> AArch64InstrInfo::getOutliningCallOverhead(
4440     MachineBasicBlock::iterator &StartIt,
4441     MachineBasicBlock::iterator &EndIt) const {
4442   // Is this a tail-call?
4443   if (EndIt->isTerminator()) {
4444     // Yes, so we only have to emit a call. Return a cost of 1 + signify that
4445     // this candidate should be tail-called.
4446     return std::make_pair(1, MachineOutlinerTailCallFn);
4447   }
4448 
4449   // No, so save + restore LR.
4450   return std::make_pair(3, MachineOutlinerDefaultFn);
4451 }
4452 
4453 std::pair<size_t, unsigned> AArch64InstrInfo::getOutliningFrameOverhead(
4454     std::vector<std::pair<MachineBasicBlock::iterator,
4455                           MachineBasicBlock::iterator>> &CandidateClass) const {
4456 
4457   // Is the last instruction in this class a terminator?
4458   if (CandidateClass[0].second->isTerminator())
4459     return std::make_pair(0, MachineOutlinerTailCallFn);
4460 
4461   // No, so we have to add a return to the end.
4462   return std::make_pair(1, MachineOutlinerDefaultFn);
4463 }
4464 
4465 bool AArch64InstrInfo::isFunctionSafeToOutlineFrom(MachineFunction &MF) const {
4466   return MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
4467 }
4468 
4469 AArch64GenInstrInfo::MachineOutlinerInstrType
4470 AArch64InstrInfo::getOutliningType(MachineInstr &MI) const {
4471 
4472   MachineFunction *MF = MI.getParent()->getParent();
4473   AArch64FunctionInfo *FuncInfo = MF->getInfo<AArch64FunctionInfo>();
4474 
4475   // Don't outline LOHs.
4476   if (FuncInfo->getLOHRelated().count(&MI))
4477     return MachineOutlinerInstrType::Illegal;
4478 
4479   // Don't allow debug values to impact outlining type.
4480   if (MI.isDebugValue() || MI.isIndirectDebugValue())
4481     return MachineOutlinerInstrType::Invisible;
4482 
4483   // Is this a terminator for a basic block?
4484   if (MI.isTerminator()) {
4485 
4486     // Is this the end of a function?
4487     if (MI.getParent()->succ_empty())
4488       return MachineOutlinerInstrType::Legal;
4489 
4490     // It's not, so don't outline it.
4491     return MachineOutlinerInstrType::Illegal;
4492   }
4493 
4494   // Don't outline positions.
4495   if (MI.isPosition())
4496     return MachineOutlinerInstrType::Illegal;
4497 
4498   // Don't touch the link register or W30.
4499   if (MI.readsRegister(AArch64::W30, &getRegisterInfo()) ||
4500       MI.modifiesRegister(AArch64::W30, &getRegisterInfo()))
4501     return MachineOutlinerInstrType::Illegal;
4502 
4503   // Make sure none of the operands are un-outlinable.
4504   for (const MachineOperand &MOP : MI.operands()) {
4505     if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() ||
4506         MOP.isTargetIndex())
4507       return MachineOutlinerInstrType::Illegal;
4508   }
4509 
4510   // Does this use the stack?
4511   if (MI.modifiesRegister(AArch64::SP, &RI) ||
4512       MI.readsRegister(AArch64::SP, &RI)) {
4513 
4514     // Is it a memory operation?
4515     if (MI.mayLoadOrStore()) {
4516       unsigned Base;  // Filled with the base regiser of MI.
4517       int64_t Offset; // Filled with the offset of MI.
4518       unsigned DummyWidth;
4519 
4520       // Does it allow us to offset the base register and is the base SP?
4521       if (!getMemOpBaseRegImmOfsWidth(MI, Base, Offset, DummyWidth, &RI) ||
4522           Base != AArch64::SP)
4523         return MachineOutlinerInstrType::Illegal;
4524 
4525       // Find the minimum/maximum offset for this instruction and check if
4526       // fixing it up would be in range.
4527       int64_t MinOffset, MaxOffset;
4528       unsigned DummyScale;
4529       getMemOpInfo(MI.getOpcode(), DummyScale, DummyWidth, MinOffset,
4530                    MaxOffset);
4531 
4532       // TODO: We should really test what happens if an instruction overflows.
4533       // This is tricky to test with IR tests, but when the outliner is moved
4534       // to a MIR test, it really ought to be checked.
4535       if (Offset + 16 < MinOffset || Offset + 16 > MaxOffset)
4536         return MachineOutlinerInstrType::Illegal;
4537 
4538       // It's in range, so we can outline it.
4539       return MachineOutlinerInstrType::Legal;
4540     }
4541 
4542     // We can't fix it up, so don't outline it.
4543     return MachineOutlinerInstrType::Illegal;
4544   }
4545 
4546   return MachineOutlinerInstrType::Legal;
4547 }
4548 
4549 void AArch64InstrInfo::fixupPostOutline(MachineBasicBlock &MBB) const {
4550   for (MachineInstr &MI : MBB) {
4551     unsigned Base, Width;
4552     int64_t Offset;
4553 
4554     // Is this a load or store with an immediate offset with SP as the base?
4555     if (!MI.mayLoadOrStore() ||
4556         !getMemOpBaseRegImmOfsWidth(MI, Base, Offset, Width, &RI) ||
4557         Base != AArch64::SP)
4558       continue;
4559 
4560     // It is, so we have to fix it up.
4561     unsigned Scale;
4562     int64_t Dummy1, Dummy2;
4563 
4564     MachineOperand &StackOffsetOperand = getMemOpBaseRegImmOfsOffsetOperand(MI);
4565     assert(StackOffsetOperand.isImm() && "Stack offset wasn't immediate!");
4566     getMemOpInfo(MI.getOpcode(), Scale, Width, Dummy1, Dummy2);
4567     assert(Scale != 0 && "Unexpected opcode!");
4568 
4569     // We've pushed the return address to the stack, so add 16 to the offset.
4570     // This is safe, since we already checked if it would overflow when we
4571     // checked if this instruction was legal to outline.
4572     int64_t NewImm = (Offset + 16) / Scale;
4573     StackOffsetOperand.setImm(NewImm);
4574   }
4575 }
4576 
4577 void AArch64InstrInfo::insertOutlinerEpilogue(MachineBasicBlock &MBB,
4578                                               MachineFunction &MF,
4579                                               unsigned FrameClass) const {
4580 
4581   // If this is a tail call outlined function, then there's already a return.
4582   if (FrameClass == MachineOutlinerTailCallFn)
4583     return;
4584 
4585   // It's not a tail call, so we have to insert the return ourselves.
4586   MachineInstr *ret = BuildMI(MF, DebugLoc(), get(AArch64::RET))
4587                           .addReg(AArch64::LR, RegState::Undef);
4588   MBB.insert(MBB.end(), ret);
4589 
4590   // Walk over the basic block and fix up all the stack accesses.
4591   fixupPostOutline(MBB);
4592 }
4593 
4594 void AArch64InstrInfo::insertOutlinerPrologue(MachineBasicBlock &MBB,
4595                                               MachineFunction &MF,
4596                                               unsigned FrameClass) const {}
4597 
4598 MachineBasicBlock::iterator AArch64InstrInfo::insertOutlinedCall(
4599     Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
4600     MachineFunction &MF, unsigned CallClass) const {
4601 
4602   // Are we tail calling?
4603   if (CallClass == MachineOutlinerTailCallFn) {
4604     // If yes, then we can just branch to the label.
4605     It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::B))
4606                             .addGlobalAddress(M.getNamedValue(MF.getName())));
4607     return It;
4608   }
4609 
4610   // We're not tail calling, so we have to save LR before the call and restore
4611   // it after.
4612   MachineInstr *STRXpre = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
4613                               .addReg(AArch64::SP, RegState::Define)
4614                               .addReg(AArch64::LR)
4615                               .addReg(AArch64::SP)
4616                               .addImm(-16);
4617   It = MBB.insert(It, STRXpre);
4618   It++;
4619 
4620   // Insert the call.
4621   It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
4622                           .addGlobalAddress(M.getNamedValue(MF.getName())));
4623 
4624   It++;
4625 
4626   // Restore the link register.
4627   MachineInstr *LDRXpost = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
4628                                .addReg(AArch64::SP, RegState::Define)
4629                                .addReg(AArch64::LR, RegState::Define)
4630                                .addReg(AArch64::SP)
4631                                .addImm(16);
4632   It = MBB.insert(It, LDRXpost);
4633 
4634   return It;
4635 }
4636