[Alignment][NFC] Use Align with TargetLowering::setPrefLoopAlignment
[llvm-complete.git] / lib / Target / AArch64 / AArch64InstrInfo.cpp
bloba9f54a1bc9ea1e1255ad3e65f64e541ff6a176ee
1 //===- AArch64InstrInfo.cpp - AArch64 Instruction Information -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the AArch64 implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "AArch64InstrInfo.h"
14 #include "AArch64MachineFunctionInfo.h"
15 #include "AArch64Subtarget.h"
16 #include "MCTargetDesc/AArch64AddressingModes.h"
17 #include "Utils/AArch64BaseInfo.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineMemOperand.h"
27 #include "llvm/CodeGen/MachineOperand.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
30 #include "llvm/CodeGen/StackMaps.h"
31 #include "llvm/CodeGen/TargetRegisterInfo.h"
32 #include "llvm/CodeGen/TargetSubtargetInfo.h"
33 #include "llvm/IR/DebugLoc.h"
34 #include "llvm/IR/GlobalValue.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCInst.h"
37 #include "llvm/MC/MCInstrDesc.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/CodeGen.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/MathExtras.h"
44 #include "llvm/Target/TargetMachine.h"
45 #include "llvm/Target/TargetOptions.h"
46 #include <cassert>
47 #include <cstdint>
48 #include <iterator>
49 #include <utility>
51 using namespace llvm;
53 #define GET_INSTRINFO_CTOR_DTOR
54 #include "AArch64GenInstrInfo.inc"
56 static cl::opt<unsigned> TBZDisplacementBits(
57 "aarch64-tbz-offset-bits", cl::Hidden, cl::init(14),
58 cl::desc("Restrict range of TB[N]Z instructions (DEBUG)"));
60 static cl::opt<unsigned> CBZDisplacementBits(
61 "aarch64-cbz-offset-bits", cl::Hidden, cl::init(19),
62 cl::desc("Restrict range of CB[N]Z instructions (DEBUG)"));
64 static cl::opt<unsigned>
65 BCCDisplacementBits("aarch64-bcc-offset-bits", cl::Hidden, cl::init(19),
66 cl::desc("Restrict range of Bcc instructions (DEBUG)"));
68 AArch64InstrInfo::AArch64InstrInfo(const AArch64Subtarget &STI)
69 : AArch64GenInstrInfo(AArch64::ADJCALLSTACKDOWN, AArch64::ADJCALLSTACKUP,
70 AArch64::CATCHRET),
71 RI(STI.getTargetTriple()), Subtarget(STI) {}
73 /// GetInstSize - Return the number of bytes of code the specified
74 /// instruction may be. This returns the maximum number of bytes.
75 unsigned AArch64InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
76 const MachineBasicBlock &MBB = *MI.getParent();
77 const MachineFunction *MF = MBB.getParent();
78 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
81 auto Op = MI.getOpcode();
82 if (Op == AArch64::INLINEASM || Op == AArch64::INLINEASM_BR)
83 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
86 // Meta-instructions emit no code.
87 if (MI.isMetaInstruction())
88 return 0;
90 // FIXME: We currently only handle pseudoinstructions that don't get expanded
91 // before the assembly printer.
92 unsigned NumBytes = 0;
93 const MCInstrDesc &Desc = MI.getDesc();
94 switch (Desc.getOpcode()) {
95 default:
96 // Anything not explicitly designated otherwise is a normal 4-byte insn.
97 NumBytes = 4;
98 break;
99 case TargetOpcode::STACKMAP:
100 // The upper bound for a stackmap intrinsic is the full length of its shadow
101 NumBytes = StackMapOpers(&MI).getNumPatchBytes();
102 assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
103 break;
104 case TargetOpcode::PATCHPOINT:
105 // The size of the patchpoint intrinsic is the number of bytes requested
106 NumBytes = PatchPointOpers(&MI).getNumPatchBytes();
107 assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
108 break;
109 case AArch64::TLSDESC_CALLSEQ:
110 // This gets lowered to an instruction sequence which takes 16 bytes
111 NumBytes = 16;
112 break;
113 case AArch64::JumpTableDest32:
114 case AArch64::JumpTableDest16:
115 case AArch64::JumpTableDest8:
116 NumBytes = 12;
117 break;
118 case AArch64::SPACE:
119 NumBytes = MI.getOperand(1).getImm();
120 break;
123 return NumBytes;
126 static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target,
127 SmallVectorImpl<MachineOperand> &Cond) {
128 // Block ends with fall-through condbranch.
129 switch (LastInst->getOpcode()) {
130 default:
131 llvm_unreachable("Unknown branch instruction?");
132 case AArch64::Bcc:
133 Target = LastInst->getOperand(1).getMBB();
134 Cond.push_back(LastInst->getOperand(0));
135 break;
136 case AArch64::CBZW:
137 case AArch64::CBZX:
138 case AArch64::CBNZW:
139 case AArch64::CBNZX:
140 Target = LastInst->getOperand(1).getMBB();
141 Cond.push_back(MachineOperand::CreateImm(-1));
142 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
143 Cond.push_back(LastInst->getOperand(0));
144 break;
145 case AArch64::TBZW:
146 case AArch64::TBZX:
147 case AArch64::TBNZW:
148 case AArch64::TBNZX:
149 Target = LastInst->getOperand(2).getMBB();
150 Cond.push_back(MachineOperand::CreateImm(-1));
151 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
152 Cond.push_back(LastInst->getOperand(0));
153 Cond.push_back(LastInst->getOperand(1));
157 static unsigned getBranchDisplacementBits(unsigned Opc) {
158 switch (Opc) {
159 default:
160 llvm_unreachable("unexpected opcode!");
161 case AArch64::B:
162 return 64;
163 case AArch64::TBNZW:
164 case AArch64::TBZW:
165 case AArch64::TBNZX:
166 case AArch64::TBZX:
167 return TBZDisplacementBits;
168 case AArch64::CBNZW:
169 case AArch64::CBZW:
170 case AArch64::CBNZX:
171 case AArch64::CBZX:
172 return CBZDisplacementBits;
173 case AArch64::Bcc:
174 return BCCDisplacementBits;
178 bool AArch64InstrInfo::isBranchOffsetInRange(unsigned BranchOp,
179 int64_t BrOffset) const {
180 unsigned Bits = getBranchDisplacementBits(BranchOp);
181 assert(Bits >= 3 && "max branch displacement must be enough to jump"
182 "over conditional branch expansion");
183 return isIntN(Bits, BrOffset / 4);
186 MachineBasicBlock *
187 AArch64InstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
188 switch (MI.getOpcode()) {
189 default:
190 llvm_unreachable("unexpected opcode!");
191 case AArch64::B:
192 return MI.getOperand(0).getMBB();
193 case AArch64::TBZW:
194 case AArch64::TBNZW:
195 case AArch64::TBZX:
196 case AArch64::TBNZX:
197 return MI.getOperand(2).getMBB();
198 case AArch64::CBZW:
199 case AArch64::CBNZW:
200 case AArch64::CBZX:
201 case AArch64::CBNZX:
202 case AArch64::Bcc:
203 return MI.getOperand(1).getMBB();
207 // Branch analysis.
208 bool AArch64InstrInfo::analyzeBranch(MachineBasicBlock &MBB,
209 MachineBasicBlock *&TBB,
210 MachineBasicBlock *&FBB,
211 SmallVectorImpl<MachineOperand> &Cond,
212 bool AllowModify) const {
213 // If the block has no terminators, it just falls into the block after it.
214 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
215 if (I == MBB.end())
216 return false;
218 if (!isUnpredicatedTerminator(*I))
219 return false;
221 // Get the last instruction in the block.
222 MachineInstr *LastInst = &*I;
224 // If there is only one terminator instruction, process it.
225 unsigned LastOpc = LastInst->getOpcode();
226 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
227 if (isUncondBranchOpcode(LastOpc)) {
228 TBB = LastInst->getOperand(0).getMBB();
229 return false;
231 if (isCondBranchOpcode(LastOpc)) {
232 // Block ends with fall-through condbranch.
233 parseCondBranch(LastInst, TBB, Cond);
234 return false;
236 return true; // Can't handle indirect branch.
239 // Get the instruction before it if it is a terminator.
240 MachineInstr *SecondLastInst = &*I;
241 unsigned SecondLastOpc = SecondLastInst->getOpcode();
243 // If AllowModify is true and the block ends with two or more unconditional
244 // branches, delete all but the first unconditional branch.
245 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
246 while (isUncondBranchOpcode(SecondLastOpc)) {
247 LastInst->eraseFromParent();
248 LastInst = SecondLastInst;
249 LastOpc = LastInst->getOpcode();
250 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
251 // Return now the only terminator is an unconditional branch.
252 TBB = LastInst->getOperand(0).getMBB();
253 return false;
254 } else {
255 SecondLastInst = &*I;
256 SecondLastOpc = SecondLastInst->getOpcode();
261 // If there are three terminators, we don't know what sort of block this is.
262 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I))
263 return true;
265 // If the block ends with a B and a Bcc, handle it.
266 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
267 parseCondBranch(SecondLastInst, TBB, Cond);
268 FBB = LastInst->getOperand(0).getMBB();
269 return false;
272 // If the block ends with two unconditional branches, handle it. The second
273 // one is not executed, so remove it.
274 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
275 TBB = SecondLastInst->getOperand(0).getMBB();
276 I = LastInst;
277 if (AllowModify)
278 I->eraseFromParent();
279 return false;
282 // ...likewise if it ends with an indirect branch followed by an unconditional
283 // branch.
284 if (isIndirectBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
285 I = LastInst;
286 if (AllowModify)
287 I->eraseFromParent();
288 return true;
291 // Otherwise, can't handle this.
292 return true;
295 bool AArch64InstrInfo::reverseBranchCondition(
296 SmallVectorImpl<MachineOperand> &Cond) const {
297 if (Cond[0].getImm() != -1) {
298 // Regular Bcc
299 AArch64CC::CondCode CC = (AArch64CC::CondCode)(int)Cond[0].getImm();
300 Cond[0].setImm(AArch64CC::getInvertedCondCode(CC));
301 } else {
302 // Folded compare-and-branch
303 switch (Cond[1].getImm()) {
304 default:
305 llvm_unreachable("Unknown conditional branch!");
306 case AArch64::CBZW:
307 Cond[1].setImm(AArch64::CBNZW);
308 break;
309 case AArch64::CBNZW:
310 Cond[1].setImm(AArch64::CBZW);
311 break;
312 case AArch64::CBZX:
313 Cond[1].setImm(AArch64::CBNZX);
314 break;
315 case AArch64::CBNZX:
316 Cond[1].setImm(AArch64::CBZX);
317 break;
318 case AArch64::TBZW:
319 Cond[1].setImm(AArch64::TBNZW);
320 break;
321 case AArch64::TBNZW:
322 Cond[1].setImm(AArch64::TBZW);
323 break;
324 case AArch64::TBZX:
325 Cond[1].setImm(AArch64::TBNZX);
326 break;
327 case AArch64::TBNZX:
328 Cond[1].setImm(AArch64::TBZX);
329 break;
333 return false;
336 unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &MBB,
337 int *BytesRemoved) const {
338 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
339 if (I == MBB.end())
340 return 0;
342 if (!isUncondBranchOpcode(I->getOpcode()) &&
343 !isCondBranchOpcode(I->getOpcode()))
344 return 0;
346 // Remove the branch.
347 I->eraseFromParent();
349 I = MBB.end();
351 if (I == MBB.begin()) {
352 if (BytesRemoved)
353 *BytesRemoved = 4;
354 return 1;
356 --I;
357 if (!isCondBranchOpcode(I->getOpcode())) {
358 if (BytesRemoved)
359 *BytesRemoved = 4;
360 return 1;
363 // Remove the branch.
364 I->eraseFromParent();
365 if (BytesRemoved)
366 *BytesRemoved = 8;
368 return 2;
371 void AArch64InstrInfo::instantiateCondBranch(
372 MachineBasicBlock &MBB, const DebugLoc &DL, MachineBasicBlock *TBB,
373 ArrayRef<MachineOperand> Cond) const {
374 if (Cond[0].getImm() != -1) {
375 // Regular Bcc
376 BuildMI(&MBB, DL, get(AArch64::Bcc)).addImm(Cond[0].getImm()).addMBB(TBB);
377 } else {
378 // Folded compare-and-branch
379 // Note that we use addOperand instead of addReg to keep the flags.
380 const MachineInstrBuilder MIB =
381 BuildMI(&MBB, DL, get(Cond[1].getImm())).add(Cond[2]);
382 if (Cond.size() > 3)
383 MIB.addImm(Cond[3].getImm());
384 MIB.addMBB(TBB);
388 unsigned AArch64InstrInfo::insertBranch(
389 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
390 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
391 // Shouldn't be a fall through.
392 assert(TBB && "insertBranch must not be told to insert a fallthrough");
394 if (!FBB) {
395 if (Cond.empty()) // Unconditional branch?
396 BuildMI(&MBB, DL, get(AArch64::B)).addMBB(TBB);
397 else
398 instantiateCondBranch(MBB, DL, TBB, Cond);
400 if (BytesAdded)
401 *BytesAdded = 4;
403 return 1;
406 // Two-way conditional branch.
407 instantiateCondBranch(MBB, DL, TBB, Cond);
408 BuildMI(&MBB, DL, get(AArch64::B)).addMBB(FBB);
410 if (BytesAdded)
411 *BytesAdded = 8;
413 return 2;
416 // Find the original register that VReg is copied from.
417 static unsigned removeCopies(const MachineRegisterInfo &MRI, unsigned VReg) {
418 while (Register::isVirtualRegister(VReg)) {
419 const MachineInstr *DefMI = MRI.getVRegDef(VReg);
420 if (!DefMI->isFullCopy())
421 return VReg;
422 VReg = DefMI->getOperand(1).getReg();
424 return VReg;
427 // Determine if VReg is defined by an instruction that can be folded into a
428 // csel instruction. If so, return the folded opcode, and the replacement
429 // register.
430 static unsigned canFoldIntoCSel(const MachineRegisterInfo &MRI, unsigned VReg,
431 unsigned *NewVReg = nullptr) {
432 VReg = removeCopies(MRI, VReg);
433 if (!Register::isVirtualRegister(VReg))
434 return 0;
436 bool Is64Bit = AArch64::GPR64allRegClass.hasSubClassEq(MRI.getRegClass(VReg));
437 const MachineInstr *DefMI = MRI.getVRegDef(VReg);
438 unsigned Opc = 0;
439 unsigned SrcOpNum = 0;
440 switch (DefMI->getOpcode()) {
441 case AArch64::ADDSXri:
442 case AArch64::ADDSWri:
443 // if NZCV is used, do not fold.
444 if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
445 return 0;
446 // fall-through to ADDXri and ADDWri.
447 LLVM_FALLTHROUGH;
448 case AArch64::ADDXri:
449 case AArch64::ADDWri:
450 // add x, 1 -> csinc.
451 if (!DefMI->getOperand(2).isImm() || DefMI->getOperand(2).getImm() != 1 ||
452 DefMI->getOperand(3).getImm() != 0)
453 return 0;
454 SrcOpNum = 1;
455 Opc = Is64Bit ? AArch64::CSINCXr : AArch64::CSINCWr;
456 break;
458 case AArch64::ORNXrr:
459 case AArch64::ORNWrr: {
460 // not x -> csinv, represented as orn dst, xzr, src.
461 unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
462 if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
463 return 0;
464 SrcOpNum = 2;
465 Opc = Is64Bit ? AArch64::CSINVXr : AArch64::CSINVWr;
466 break;
469 case AArch64::SUBSXrr:
470 case AArch64::SUBSWrr:
471 // if NZCV is used, do not fold.
472 if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
473 return 0;
474 // fall-through to SUBXrr and SUBWrr.
475 LLVM_FALLTHROUGH;
476 case AArch64::SUBXrr:
477 case AArch64::SUBWrr: {
478 // neg x -> csneg, represented as sub dst, xzr, src.
479 unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
480 if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
481 return 0;
482 SrcOpNum = 2;
483 Opc = Is64Bit ? AArch64::CSNEGXr : AArch64::CSNEGWr;
484 break;
486 default:
487 return 0;
489 assert(Opc && SrcOpNum && "Missing parameters");
491 if (NewVReg)
492 *NewVReg = DefMI->getOperand(SrcOpNum).getReg();
493 return Opc;
496 bool AArch64InstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
497 ArrayRef<MachineOperand> Cond,
498 unsigned TrueReg, unsigned FalseReg,
499 int &CondCycles, int &TrueCycles,
500 int &FalseCycles) const {
501 // Check register classes.
502 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
503 const TargetRegisterClass *RC =
504 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
505 if (!RC)
506 return false;
508 // Expanding cbz/tbz requires an extra cycle of latency on the condition.
509 unsigned ExtraCondLat = Cond.size() != 1;
511 // GPRs are handled by csel.
512 // FIXME: Fold in x+1, -x, and ~x when applicable.
513 if (AArch64::GPR64allRegClass.hasSubClassEq(RC) ||
514 AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
515 // Single-cycle csel, csinc, csinv, and csneg.
516 CondCycles = 1 + ExtraCondLat;
517 TrueCycles = FalseCycles = 1;
518 if (canFoldIntoCSel(MRI, TrueReg))
519 TrueCycles = 0;
520 else if (canFoldIntoCSel(MRI, FalseReg))
521 FalseCycles = 0;
522 return true;
525 // Scalar floating point is handled by fcsel.
526 // FIXME: Form fabs, fmin, and fmax when applicable.
527 if (AArch64::FPR64RegClass.hasSubClassEq(RC) ||
528 AArch64::FPR32RegClass.hasSubClassEq(RC)) {
529 CondCycles = 5 + ExtraCondLat;
530 TrueCycles = FalseCycles = 2;
531 return true;
534 // Can't do vectors.
535 return false;
538 void AArch64InstrInfo::insertSelect(MachineBasicBlock &MBB,
539 MachineBasicBlock::iterator I,
540 const DebugLoc &DL, unsigned DstReg,
541 ArrayRef<MachineOperand> Cond,
542 unsigned TrueReg, unsigned FalseReg) const {
543 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
545 // Parse the condition code, see parseCondBranch() above.
546 AArch64CC::CondCode CC;
547 switch (Cond.size()) {
548 default:
549 llvm_unreachable("Unknown condition opcode in Cond");
550 case 1: // b.cc
551 CC = AArch64CC::CondCode(Cond[0].getImm());
552 break;
553 case 3: { // cbz/cbnz
554 // We must insert a compare against 0.
555 bool Is64Bit;
556 switch (Cond[1].getImm()) {
557 default:
558 llvm_unreachable("Unknown branch opcode in Cond");
559 case AArch64::CBZW:
560 Is64Bit = false;
561 CC = AArch64CC::EQ;
562 break;
563 case AArch64::CBZX:
564 Is64Bit = true;
565 CC = AArch64CC::EQ;
566 break;
567 case AArch64::CBNZW:
568 Is64Bit = false;
569 CC = AArch64CC::NE;
570 break;
571 case AArch64::CBNZX:
572 Is64Bit = true;
573 CC = AArch64CC::NE;
574 break;
576 Register SrcReg = Cond[2].getReg();
577 if (Is64Bit) {
578 // cmp reg, #0 is actually subs xzr, reg, #0.
579 MRI.constrainRegClass(SrcReg, &AArch64::GPR64spRegClass);
580 BuildMI(MBB, I, DL, get(AArch64::SUBSXri), AArch64::XZR)
581 .addReg(SrcReg)
582 .addImm(0)
583 .addImm(0);
584 } else {
585 MRI.constrainRegClass(SrcReg, &AArch64::GPR32spRegClass);
586 BuildMI(MBB, I, DL, get(AArch64::SUBSWri), AArch64::WZR)
587 .addReg(SrcReg)
588 .addImm(0)
589 .addImm(0);
591 break;
593 case 4: { // tbz/tbnz
594 // We must insert a tst instruction.
595 switch (Cond[1].getImm()) {
596 default:
597 llvm_unreachable("Unknown branch opcode in Cond");
598 case AArch64::TBZW:
599 case AArch64::TBZX:
600 CC = AArch64CC::EQ;
601 break;
602 case AArch64::TBNZW:
603 case AArch64::TBNZX:
604 CC = AArch64CC::NE;
605 break;
607 // cmp reg, #foo is actually ands xzr, reg, #1<<foo.
608 if (Cond[1].getImm() == AArch64::TBZW || Cond[1].getImm() == AArch64::TBNZW)
609 BuildMI(MBB, I, DL, get(AArch64::ANDSWri), AArch64::WZR)
610 .addReg(Cond[2].getReg())
611 .addImm(
612 AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 32));
613 else
614 BuildMI(MBB, I, DL, get(AArch64::ANDSXri), AArch64::XZR)
615 .addReg(Cond[2].getReg())
616 .addImm(
617 AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 64));
618 break;
622 unsigned Opc = 0;
623 const TargetRegisterClass *RC = nullptr;
624 bool TryFold = false;
625 if (MRI.constrainRegClass(DstReg, &AArch64::GPR64RegClass)) {
626 RC = &AArch64::GPR64RegClass;
627 Opc = AArch64::CSELXr;
628 TryFold = true;
629 } else if (MRI.constrainRegClass(DstReg, &AArch64::GPR32RegClass)) {
630 RC = &AArch64::GPR32RegClass;
631 Opc = AArch64::CSELWr;
632 TryFold = true;
633 } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR64RegClass)) {
634 RC = &AArch64::FPR64RegClass;
635 Opc = AArch64::FCSELDrrr;
636 } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR32RegClass)) {
637 RC = &AArch64::FPR32RegClass;
638 Opc = AArch64::FCSELSrrr;
640 assert(RC && "Unsupported regclass");
642 // Try folding simple instructions into the csel.
643 if (TryFold) {
644 unsigned NewVReg = 0;
645 unsigned FoldedOpc = canFoldIntoCSel(MRI, TrueReg, &NewVReg);
646 if (FoldedOpc) {
647 // The folded opcodes csinc, csinc and csneg apply the operation to
648 // FalseReg, so we need to invert the condition.
649 CC = AArch64CC::getInvertedCondCode(CC);
650 TrueReg = FalseReg;
651 } else
652 FoldedOpc = canFoldIntoCSel(MRI, FalseReg, &NewVReg);
654 // Fold the operation. Leave any dead instructions for DCE to clean up.
655 if (FoldedOpc) {
656 FalseReg = NewVReg;
657 Opc = FoldedOpc;
658 // The extends the live range of NewVReg.
659 MRI.clearKillFlags(NewVReg);
663 // Pull all virtual register into the appropriate class.
664 MRI.constrainRegClass(TrueReg, RC);
665 MRI.constrainRegClass(FalseReg, RC);
667 // Insert the csel.
668 BuildMI(MBB, I, DL, get(Opc), DstReg)
669 .addReg(TrueReg)
670 .addReg(FalseReg)
671 .addImm(CC);
674 /// Returns true if a MOVi32imm or MOVi64imm can be expanded to an ORRxx.
675 static bool canBeExpandedToORR(const MachineInstr &MI, unsigned BitSize) {
676 uint64_t Imm = MI.getOperand(1).getImm();
677 uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
678 uint64_t Encoding;
679 return AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding);
682 // FIXME: this implementation should be micro-architecture dependent, so a
683 // micro-architecture target hook should be introduced here in future.
684 bool AArch64InstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
685 if (!Subtarget.hasCustomCheapAsMoveHandling())
686 return MI.isAsCheapAsAMove();
688 const unsigned Opcode = MI.getOpcode();
690 // Firstly, check cases gated by features.
692 if (Subtarget.hasZeroCycleZeroingFP()) {
693 if (Opcode == AArch64::FMOVH0 ||
694 Opcode == AArch64::FMOVS0 ||
695 Opcode == AArch64::FMOVD0)
696 return true;
699 if (Subtarget.hasZeroCycleZeroingGP()) {
700 if (Opcode == TargetOpcode::COPY &&
701 (MI.getOperand(1).getReg() == AArch64::WZR ||
702 MI.getOperand(1).getReg() == AArch64::XZR))
703 return true;
706 // Secondly, check cases specific to sub-targets.
708 if (Subtarget.hasExynosCheapAsMoveHandling()) {
709 if (isExynosCheapAsMove(MI))
710 return true;
712 return MI.isAsCheapAsAMove();
715 // Finally, check generic cases.
717 switch (Opcode) {
718 default:
719 return false;
721 // add/sub on register without shift
722 case AArch64::ADDWri:
723 case AArch64::ADDXri:
724 case AArch64::SUBWri:
725 case AArch64::SUBXri:
726 return (MI.getOperand(3).getImm() == 0);
728 // logical ops on immediate
729 case AArch64::ANDWri:
730 case AArch64::ANDXri:
731 case AArch64::EORWri:
732 case AArch64::EORXri:
733 case AArch64::ORRWri:
734 case AArch64::ORRXri:
735 return true;
737 // logical ops on register without shift
738 case AArch64::ANDWrr:
739 case AArch64::ANDXrr:
740 case AArch64::BICWrr:
741 case AArch64::BICXrr:
742 case AArch64::EONWrr:
743 case AArch64::EONXrr:
744 case AArch64::EORWrr:
745 case AArch64::EORXrr:
746 case AArch64::ORNWrr:
747 case AArch64::ORNXrr:
748 case AArch64::ORRWrr:
749 case AArch64::ORRXrr:
750 return true;
752 // If MOVi32imm or MOVi64imm can be expanded into ORRWri or
753 // ORRXri, it is as cheap as MOV
754 case AArch64::MOVi32imm:
755 return canBeExpandedToORR(MI, 32);
756 case AArch64::MOVi64imm:
757 return canBeExpandedToORR(MI, 64);
760 llvm_unreachable("Unknown opcode to check as cheap as a move!");
763 bool AArch64InstrInfo::isFalkorShiftExtFast(const MachineInstr &MI) {
764 switch (MI.getOpcode()) {
765 default:
766 return false;
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;
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;
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);
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);
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;
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;
885 bool AArch64InstrInfo::isSEHInstruction(const MachineInstr &MI) {
886 unsigned Opc = MI.getOpcode();
887 switch (Opc) {
888 default:
889 return false;
890 case AArch64::SEH_StackAlloc:
891 case AArch64::SEH_SaveFPLR:
892 case AArch64::SEH_SaveFPLR_X:
893 case AArch64::SEH_SaveReg:
894 case AArch64::SEH_SaveReg_X:
895 case AArch64::SEH_SaveRegP:
896 case AArch64::SEH_SaveRegP_X:
897 case AArch64::SEH_SaveFReg:
898 case AArch64::SEH_SaveFReg_X:
899 case AArch64::SEH_SaveFRegP:
900 case AArch64::SEH_SaveFRegP_X:
901 case AArch64::SEH_SetFP:
902 case AArch64::SEH_AddFP:
903 case AArch64::SEH_Nop:
904 case AArch64::SEH_PrologEnd:
905 case AArch64::SEH_EpilogStart:
906 case AArch64::SEH_EpilogEnd:
907 return true;
911 bool AArch64InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
912 unsigned &SrcReg, unsigned &DstReg,
913 unsigned &SubIdx) const {
914 switch (MI.getOpcode()) {
915 default:
916 return false;
917 case AArch64::SBFMXri: // aka sxtw
918 case AArch64::UBFMXri: // aka uxtw
919 // Check for the 32 -> 64 bit extension case, these instructions can do
920 // much more.
921 if (MI.getOperand(2).getImm() != 0 || MI.getOperand(3).getImm() != 31)
922 return false;
923 // This is a signed or unsigned 32 -> 64 bit extension.
924 SrcReg = MI.getOperand(1).getReg();
925 DstReg = MI.getOperand(0).getReg();
926 SubIdx = AArch64::sub_32;
927 return true;
931 bool AArch64InstrInfo::areMemAccessesTriviallyDisjoint(
932 const MachineInstr &MIa, const MachineInstr &MIb, AliasAnalysis *AA) const {
933 const TargetRegisterInfo *TRI = &getRegisterInfo();
934 const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
935 int64_t OffsetA = 0, OffsetB = 0;
936 unsigned WidthA = 0, WidthB = 0;
938 assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
939 assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
941 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
942 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
943 return false;
945 // Retrieve the base, offset from the base and width. Width
946 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4, 8). If
947 // base are identical, and the offset of a lower memory access +
948 // the width doesn't overlap the offset of a higher memory access,
949 // then the memory accesses are different.
950 if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) &&
951 getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) {
952 if (BaseOpA->isIdenticalTo(*BaseOpB)) {
953 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
954 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
955 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
956 if (LowOffset + LowWidth <= HighOffset)
957 return true;
960 return false;
963 bool AArch64InstrInfo::isSchedulingBoundary(const MachineInstr &MI,
964 const MachineBasicBlock *MBB,
965 const MachineFunction &MF) const {
966 if (TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF))
967 return true;
968 switch (MI.getOpcode()) {
969 case AArch64::HINT:
970 // CSDB hints are scheduling barriers.
971 if (MI.getOperand(0).getImm() == 0x14)
972 return true;
973 break;
974 case AArch64::DSB:
975 case AArch64::ISB:
976 // DSB and ISB also are scheduling barriers.
977 return true;
978 default:;
980 return isSEHInstruction(MI);
983 /// analyzeCompare - For a comparison instruction, return the source registers
984 /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
985 /// Return true if the comparison instruction can be analyzed.
986 bool AArch64InstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
987 unsigned &SrcReg2, int &CmpMask,
988 int &CmpValue) const {
989 // The first operand can be a frame index where we'd normally expect a
990 // register.
991 assert(MI.getNumOperands() >= 2 && "All AArch64 cmps should have 2 operands");
992 if (!MI.getOperand(1).isReg())
993 return false;
995 switch (MI.getOpcode()) {
996 default:
997 break;
998 case AArch64::SUBSWrr:
999 case AArch64::SUBSWrs:
1000 case AArch64::SUBSWrx:
1001 case AArch64::SUBSXrr:
1002 case AArch64::SUBSXrs:
1003 case AArch64::SUBSXrx:
1004 case AArch64::ADDSWrr:
1005 case AArch64::ADDSWrs:
1006 case AArch64::ADDSWrx:
1007 case AArch64::ADDSXrr:
1008 case AArch64::ADDSXrs:
1009 case AArch64::ADDSXrx:
1010 // Replace SUBSWrr with SUBWrr if NZCV is not used.
1011 SrcReg = MI.getOperand(1).getReg();
1012 SrcReg2 = MI.getOperand(2).getReg();
1013 CmpMask = ~0;
1014 CmpValue = 0;
1015 return true;
1016 case AArch64::SUBSWri:
1017 case AArch64::ADDSWri:
1018 case AArch64::SUBSXri:
1019 case AArch64::ADDSXri:
1020 SrcReg = MI.getOperand(1).getReg();
1021 SrcReg2 = 0;
1022 CmpMask = ~0;
1023 // FIXME: In order to convert CmpValue to 0 or 1
1024 CmpValue = MI.getOperand(2).getImm() != 0;
1025 return true;
1026 case AArch64::ANDSWri:
1027 case AArch64::ANDSXri:
1028 // ANDS does not use the same encoding scheme as the others xxxS
1029 // instructions.
1030 SrcReg = MI.getOperand(1).getReg();
1031 SrcReg2 = 0;
1032 CmpMask = ~0;
1033 // FIXME:The return val type of decodeLogicalImmediate is uint64_t,
1034 // while the type of CmpValue is int. When converting uint64_t to int,
1035 // the high 32 bits of uint64_t will be lost.
1036 // In fact it causes a bug in spec2006-483.xalancbmk
1037 // CmpValue is only used to compare with zero in OptimizeCompareInstr
1038 CmpValue = AArch64_AM::decodeLogicalImmediate(
1039 MI.getOperand(2).getImm(),
1040 MI.getOpcode() == AArch64::ANDSWri ? 32 : 64) != 0;
1041 return true;
1044 return false;
1047 static bool UpdateOperandRegClass(MachineInstr &Instr) {
1048 MachineBasicBlock *MBB = Instr.getParent();
1049 assert(MBB && "Can't get MachineBasicBlock here");
1050 MachineFunction *MF = MBB->getParent();
1051 assert(MF && "Can't get MachineFunction here");
1052 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1053 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
1054 MachineRegisterInfo *MRI = &MF->getRegInfo();
1056 for (unsigned OpIdx = 0, EndIdx = Instr.getNumOperands(); OpIdx < EndIdx;
1057 ++OpIdx) {
1058 MachineOperand &MO = Instr.getOperand(OpIdx);
1059 const TargetRegisterClass *OpRegCstraints =
1060 Instr.getRegClassConstraint(OpIdx, TII, TRI);
1062 // If there's no constraint, there's nothing to do.
1063 if (!OpRegCstraints)
1064 continue;
1065 // If the operand is a frame index, there's nothing to do here.
1066 // A frame index operand will resolve correctly during PEI.
1067 if (MO.isFI())
1068 continue;
1070 assert(MO.isReg() &&
1071 "Operand has register constraints without being a register!");
1073 Register Reg = MO.getReg();
1074 if (Register::isPhysicalRegister(Reg)) {
1075 if (!OpRegCstraints->contains(Reg))
1076 return false;
1077 } else if (!OpRegCstraints->hasSubClassEq(MRI->getRegClass(Reg)) &&
1078 !MRI->constrainRegClass(Reg, OpRegCstraints))
1079 return false;
1082 return true;
1085 /// Return the opcode that does not set flags when possible - otherwise
1086 /// return the original opcode. The caller is responsible to do the actual
1087 /// substitution and legality checking.
1088 static unsigned convertToNonFlagSettingOpc(const MachineInstr &MI) {
1089 // Don't convert all compare instructions, because for some the zero register
1090 // encoding becomes the sp register.
1091 bool MIDefinesZeroReg = false;
1092 if (MI.definesRegister(AArch64::WZR) || MI.definesRegister(AArch64::XZR))
1093 MIDefinesZeroReg = true;
1095 switch (MI.getOpcode()) {
1096 default:
1097 return MI.getOpcode();
1098 case AArch64::ADDSWrr:
1099 return AArch64::ADDWrr;
1100 case AArch64::ADDSWri:
1101 return MIDefinesZeroReg ? AArch64::ADDSWri : AArch64::ADDWri;
1102 case AArch64::ADDSWrs:
1103 return MIDefinesZeroReg ? AArch64::ADDSWrs : AArch64::ADDWrs;
1104 case AArch64::ADDSWrx:
1105 return AArch64::ADDWrx;
1106 case AArch64::ADDSXrr:
1107 return AArch64::ADDXrr;
1108 case AArch64::ADDSXri:
1109 return MIDefinesZeroReg ? AArch64::ADDSXri : AArch64::ADDXri;
1110 case AArch64::ADDSXrs:
1111 return MIDefinesZeroReg ? AArch64::ADDSXrs : AArch64::ADDXrs;
1112 case AArch64::ADDSXrx:
1113 return AArch64::ADDXrx;
1114 case AArch64::SUBSWrr:
1115 return AArch64::SUBWrr;
1116 case AArch64::SUBSWri:
1117 return MIDefinesZeroReg ? AArch64::SUBSWri : AArch64::SUBWri;
1118 case AArch64::SUBSWrs:
1119 return MIDefinesZeroReg ? AArch64::SUBSWrs : AArch64::SUBWrs;
1120 case AArch64::SUBSWrx:
1121 return AArch64::SUBWrx;
1122 case AArch64::SUBSXrr:
1123 return AArch64::SUBXrr;
1124 case AArch64::SUBSXri:
1125 return MIDefinesZeroReg ? AArch64::SUBSXri : AArch64::SUBXri;
1126 case AArch64::SUBSXrs:
1127 return MIDefinesZeroReg ? AArch64::SUBSXrs : AArch64::SUBXrs;
1128 case AArch64::SUBSXrx:
1129 return AArch64::SUBXrx;
1133 enum AccessKind { AK_Write = 0x01, AK_Read = 0x10, AK_All = 0x11 };
1135 /// True when condition flags are accessed (either by writing or reading)
1136 /// on the instruction trace starting at From and ending at To.
1138 /// Note: If From and To are from different blocks it's assumed CC are accessed
1139 /// on the path.
1140 static bool areCFlagsAccessedBetweenInstrs(
1141 MachineBasicBlock::iterator From, MachineBasicBlock::iterator To,
1142 const TargetRegisterInfo *TRI, const AccessKind AccessToCheck = AK_All) {
1143 // Early exit if To is at the beginning of the BB.
1144 if (To == To->getParent()->begin())
1145 return true;
1147 // Check whether the instructions are in the same basic block
1148 // If not, assume the condition flags might get modified somewhere.
1149 if (To->getParent() != From->getParent())
1150 return true;
1152 // From must be above To.
1153 assert(std::find_if(++To.getReverse(), To->getParent()->rend(),
1154 [From](MachineInstr &MI) {
1155 return MI.getIterator() == From;
1156 }) != To->getParent()->rend());
1158 // We iterate backward starting \p To until we hit \p From.
1159 for (--To; To != From; --To) {
1160 const MachineInstr &Instr = *To;
1162 if (((AccessToCheck & AK_Write) &&
1163 Instr.modifiesRegister(AArch64::NZCV, TRI)) ||
1164 ((AccessToCheck & AK_Read) && Instr.readsRegister(AArch64::NZCV, TRI)))
1165 return true;
1167 return false;
1170 /// Try to optimize a compare instruction. A compare instruction is an
1171 /// instruction which produces AArch64::NZCV. It can be truly compare
1172 /// instruction
1173 /// when there are no uses of its destination register.
1175 /// The following steps are tried in order:
1176 /// 1. Convert CmpInstr into an unconditional version.
1177 /// 2. Remove CmpInstr if above there is an instruction producing a needed
1178 /// condition code or an instruction which can be converted into such an
1179 /// instruction.
1180 /// Only comparison with zero is supported.
1181 bool AArch64InstrInfo::optimizeCompareInstr(
1182 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
1183 int CmpValue, const MachineRegisterInfo *MRI) const {
1184 assert(CmpInstr.getParent());
1185 assert(MRI);
1187 // Replace SUBSWrr with SUBWrr if NZCV is not used.
1188 int DeadNZCVIdx = CmpInstr.findRegisterDefOperandIdx(AArch64::NZCV, true);
1189 if (DeadNZCVIdx != -1) {
1190 if (CmpInstr.definesRegister(AArch64::WZR) ||
1191 CmpInstr.definesRegister(AArch64::XZR)) {
1192 CmpInstr.eraseFromParent();
1193 return true;
1195 unsigned Opc = CmpInstr.getOpcode();
1196 unsigned NewOpc = convertToNonFlagSettingOpc(CmpInstr);
1197 if (NewOpc == Opc)
1198 return false;
1199 const MCInstrDesc &MCID = get(NewOpc);
1200 CmpInstr.setDesc(MCID);
1201 CmpInstr.RemoveOperand(DeadNZCVIdx);
1202 bool succeeded = UpdateOperandRegClass(CmpInstr);
1203 (void)succeeded;
1204 assert(succeeded && "Some operands reg class are incompatible!");
1205 return true;
1208 // Continue only if we have a "ri" where immediate is zero.
1209 // FIXME:CmpValue has already been converted to 0 or 1 in analyzeCompare
1210 // function.
1211 assert((CmpValue == 0 || CmpValue == 1) && "CmpValue must be 0 or 1!");
1212 if (CmpValue != 0 || SrcReg2 != 0)
1213 return false;
1215 // CmpInstr is a Compare instruction if destination register is not used.
1216 if (!MRI->use_nodbg_empty(CmpInstr.getOperand(0).getReg()))
1217 return false;
1219 return substituteCmpToZero(CmpInstr, SrcReg, MRI);
1222 /// Get opcode of S version of Instr.
1223 /// If Instr is S version its opcode is returned.
1224 /// AArch64::INSTRUCTION_LIST_END is returned if Instr does not have S version
1225 /// or we are not interested in it.
1226 static unsigned sForm(MachineInstr &Instr) {
1227 switch (Instr.getOpcode()) {
1228 default:
1229 return AArch64::INSTRUCTION_LIST_END;
1231 case AArch64::ADDSWrr:
1232 case AArch64::ADDSWri:
1233 case AArch64::ADDSXrr:
1234 case AArch64::ADDSXri:
1235 case AArch64::SUBSWrr:
1236 case AArch64::SUBSWri:
1237 case AArch64::SUBSXrr:
1238 case AArch64::SUBSXri:
1239 return Instr.getOpcode();
1241 case AArch64::ADDWrr:
1242 return AArch64::ADDSWrr;
1243 case AArch64::ADDWri:
1244 return AArch64::ADDSWri;
1245 case AArch64::ADDXrr:
1246 return AArch64::ADDSXrr;
1247 case AArch64::ADDXri:
1248 return AArch64::ADDSXri;
1249 case AArch64::ADCWr:
1250 return AArch64::ADCSWr;
1251 case AArch64::ADCXr:
1252 return AArch64::ADCSXr;
1253 case AArch64::SUBWrr:
1254 return AArch64::SUBSWrr;
1255 case AArch64::SUBWri:
1256 return AArch64::SUBSWri;
1257 case AArch64::SUBXrr:
1258 return AArch64::SUBSXrr;
1259 case AArch64::SUBXri:
1260 return AArch64::SUBSXri;
1261 case AArch64::SBCWr:
1262 return AArch64::SBCSWr;
1263 case AArch64::SBCXr:
1264 return AArch64::SBCSXr;
1265 case AArch64::ANDWri:
1266 return AArch64::ANDSWri;
1267 case AArch64::ANDXri:
1268 return AArch64::ANDSXri;
1272 /// Check if AArch64::NZCV should be alive in successors of MBB.
1273 static bool areCFlagsAliveInSuccessors(MachineBasicBlock *MBB) {
1274 for (auto *BB : MBB->successors())
1275 if (BB->isLiveIn(AArch64::NZCV))
1276 return true;
1277 return false;
1280 namespace {
1282 struct UsedNZCV {
1283 bool N = false;
1284 bool Z = false;
1285 bool C = false;
1286 bool V = false;
1288 UsedNZCV() = default;
1290 UsedNZCV &operator|=(const UsedNZCV &UsedFlags) {
1291 this->N |= UsedFlags.N;
1292 this->Z |= UsedFlags.Z;
1293 this->C |= UsedFlags.C;
1294 this->V |= UsedFlags.V;
1295 return *this;
1299 } // end anonymous namespace
1301 /// Find a condition code used by the instruction.
1302 /// Returns AArch64CC::Invalid if either the instruction does not use condition
1303 /// codes or we don't optimize CmpInstr in the presence of such instructions.
1304 static AArch64CC::CondCode findCondCodeUsedByInstr(const MachineInstr &Instr) {
1305 switch (Instr.getOpcode()) {
1306 default:
1307 return AArch64CC::Invalid;
1309 case AArch64::Bcc: {
1310 int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
1311 assert(Idx >= 2);
1312 return static_cast<AArch64CC::CondCode>(Instr.getOperand(Idx - 2).getImm());
1315 case AArch64::CSINVWr:
1316 case AArch64::CSINVXr:
1317 case AArch64::CSINCWr:
1318 case AArch64::CSINCXr:
1319 case AArch64::CSELWr:
1320 case AArch64::CSELXr:
1321 case AArch64::CSNEGWr:
1322 case AArch64::CSNEGXr:
1323 case AArch64::FCSELSrrr:
1324 case AArch64::FCSELDrrr: {
1325 int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
1326 assert(Idx >= 1);
1327 return static_cast<AArch64CC::CondCode>(Instr.getOperand(Idx - 1).getImm());
1332 static UsedNZCV getUsedNZCV(AArch64CC::CondCode CC) {
1333 assert(CC != AArch64CC::Invalid);
1334 UsedNZCV UsedFlags;
1335 switch (CC) {
1336 default:
1337 break;
1339 case AArch64CC::EQ: // Z set
1340 case AArch64CC::NE: // Z clear
1341 UsedFlags.Z = true;
1342 break;
1344 case AArch64CC::HI: // Z clear and C set
1345 case AArch64CC::LS: // Z set or C clear
1346 UsedFlags.Z = true;
1347 LLVM_FALLTHROUGH;
1348 case AArch64CC::HS: // C set
1349 case AArch64CC::LO: // C clear
1350 UsedFlags.C = true;
1351 break;
1353 case AArch64CC::MI: // N set
1354 case AArch64CC::PL: // N clear
1355 UsedFlags.N = true;
1356 break;
1358 case AArch64CC::VS: // V set
1359 case AArch64CC::VC: // V clear
1360 UsedFlags.V = true;
1361 break;
1363 case AArch64CC::GT: // Z clear, N and V the same
1364 case AArch64CC::LE: // Z set, N and V differ
1365 UsedFlags.Z = true;
1366 LLVM_FALLTHROUGH;
1367 case AArch64CC::GE: // N and V the same
1368 case AArch64CC::LT: // N and V differ
1369 UsedFlags.N = true;
1370 UsedFlags.V = true;
1371 break;
1373 return UsedFlags;
1376 static bool isADDSRegImm(unsigned Opcode) {
1377 return Opcode == AArch64::ADDSWri || Opcode == AArch64::ADDSXri;
1380 static bool isSUBSRegImm(unsigned Opcode) {
1381 return Opcode == AArch64::SUBSWri || Opcode == AArch64::SUBSXri;
1384 /// Check if CmpInstr can be substituted by MI.
1386 /// CmpInstr can be substituted:
1387 /// - CmpInstr is either 'ADDS %vreg, 0' or 'SUBS %vreg, 0'
1388 /// - and, MI and CmpInstr are from the same MachineBB
1389 /// - and, condition flags are not alive in successors of the CmpInstr parent
1390 /// - and, if MI opcode is the S form there must be no defs of flags between
1391 /// MI and CmpInstr
1392 /// or if MI opcode is not the S form there must be neither defs of flags
1393 /// nor uses of flags between MI and CmpInstr.
1394 /// - and C/V flags are not used after CmpInstr
1395 static bool canInstrSubstituteCmpInstr(MachineInstr *MI, MachineInstr *CmpInstr,
1396 const TargetRegisterInfo *TRI) {
1397 assert(MI);
1398 assert(sForm(*MI) != AArch64::INSTRUCTION_LIST_END);
1399 assert(CmpInstr);
1401 const unsigned CmpOpcode = CmpInstr->getOpcode();
1402 if (!isADDSRegImm(CmpOpcode) && !isSUBSRegImm(CmpOpcode))
1403 return false;
1405 if (MI->getParent() != CmpInstr->getParent())
1406 return false;
1408 if (areCFlagsAliveInSuccessors(CmpInstr->getParent()))
1409 return false;
1411 AccessKind AccessToCheck = AK_Write;
1412 if (sForm(*MI) != MI->getOpcode())
1413 AccessToCheck = AK_All;
1414 if (areCFlagsAccessedBetweenInstrs(MI, CmpInstr, TRI, AccessToCheck))
1415 return false;
1417 UsedNZCV NZCVUsedAfterCmp;
1418 for (auto I = std::next(CmpInstr->getIterator()),
1419 E = CmpInstr->getParent()->instr_end();
1420 I != E; ++I) {
1421 const MachineInstr &Instr = *I;
1422 if (Instr.readsRegister(AArch64::NZCV, TRI)) {
1423 AArch64CC::CondCode CC = findCondCodeUsedByInstr(Instr);
1424 if (CC == AArch64CC::Invalid) // Unsupported conditional instruction
1425 return false;
1426 NZCVUsedAfterCmp |= getUsedNZCV(CC);
1429 if (Instr.modifiesRegister(AArch64::NZCV, TRI))
1430 break;
1433 return !NZCVUsedAfterCmp.C && !NZCVUsedAfterCmp.V;
1436 /// Substitute an instruction comparing to zero with another instruction
1437 /// which produces needed condition flags.
1439 /// Return true on success.
1440 bool AArch64InstrInfo::substituteCmpToZero(
1441 MachineInstr &CmpInstr, unsigned SrcReg,
1442 const MachineRegisterInfo *MRI) const {
1443 assert(MRI);
1444 // Get the unique definition of SrcReg.
1445 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1446 if (!MI)
1447 return false;
1449 const TargetRegisterInfo *TRI = &getRegisterInfo();
1451 unsigned NewOpc = sForm(*MI);
1452 if (NewOpc == AArch64::INSTRUCTION_LIST_END)
1453 return false;
1455 if (!canInstrSubstituteCmpInstr(MI, &CmpInstr, TRI))
1456 return false;
1458 // Update the instruction to set NZCV.
1459 MI->setDesc(get(NewOpc));
1460 CmpInstr.eraseFromParent();
1461 bool succeeded = UpdateOperandRegClass(*MI);
1462 (void)succeeded;
1463 assert(succeeded && "Some operands reg class are incompatible!");
1464 MI->addRegisterDefined(AArch64::NZCV, TRI);
1465 return true;
1468 bool AArch64InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1469 if (MI.getOpcode() != TargetOpcode::LOAD_STACK_GUARD &&
1470 MI.getOpcode() != AArch64::CATCHRET)
1471 return false;
1473 MachineBasicBlock &MBB = *MI.getParent();
1474 DebugLoc DL = MI.getDebugLoc();
1476 if (MI.getOpcode() == AArch64::CATCHRET) {
1477 // Skip to the first instruction before the epilog.
1478 const TargetInstrInfo *TII =
1479 MBB.getParent()->getSubtarget().getInstrInfo();
1480 MachineBasicBlock *TargetMBB = MI.getOperand(0).getMBB();
1481 auto MBBI = MachineBasicBlock::iterator(MI);
1482 MachineBasicBlock::iterator FirstEpilogSEH = std::prev(MBBI);
1483 while (FirstEpilogSEH->getFlag(MachineInstr::FrameDestroy) &&
1484 FirstEpilogSEH != MBB.begin())
1485 FirstEpilogSEH = std::prev(FirstEpilogSEH);
1486 if (FirstEpilogSEH != MBB.begin())
1487 FirstEpilogSEH = std::next(FirstEpilogSEH);
1488 BuildMI(MBB, FirstEpilogSEH, DL, TII->get(AArch64::ADRP))
1489 .addReg(AArch64::X0, RegState::Define)
1490 .addMBB(TargetMBB);
1491 BuildMI(MBB, FirstEpilogSEH, DL, TII->get(AArch64::ADDXri))
1492 .addReg(AArch64::X0, RegState::Define)
1493 .addReg(AArch64::X0)
1494 .addMBB(TargetMBB)
1495 .addImm(0);
1496 return true;
1499 Register Reg = MI.getOperand(0).getReg();
1500 const GlobalValue *GV =
1501 cast<GlobalValue>((*MI.memoperands_begin())->getValue());
1502 const TargetMachine &TM = MBB.getParent()->getTarget();
1503 unsigned OpFlags = Subtarget.ClassifyGlobalReference(GV, TM);
1504 const unsigned char MO_NC = AArch64II::MO_NC;
1506 if ((OpFlags & AArch64II::MO_GOT) != 0) {
1507 BuildMI(MBB, MI, DL, get(AArch64::LOADgot), Reg)
1508 .addGlobalAddress(GV, 0, OpFlags);
1509 BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1510 .addReg(Reg, RegState::Kill)
1511 .addImm(0)
1512 .addMemOperand(*MI.memoperands_begin());
1513 } else if (TM.getCodeModel() == CodeModel::Large) {
1514 BuildMI(MBB, MI, DL, get(AArch64::MOVZXi), Reg)
1515 .addGlobalAddress(GV, 0, AArch64II::MO_G0 | MO_NC)
1516 .addImm(0);
1517 BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1518 .addReg(Reg, RegState::Kill)
1519 .addGlobalAddress(GV, 0, AArch64II::MO_G1 | MO_NC)
1520 .addImm(16);
1521 BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1522 .addReg(Reg, RegState::Kill)
1523 .addGlobalAddress(GV, 0, AArch64II::MO_G2 | MO_NC)
1524 .addImm(32);
1525 BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
1526 .addReg(Reg, RegState::Kill)
1527 .addGlobalAddress(GV, 0, AArch64II::MO_G3)
1528 .addImm(48);
1529 BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1530 .addReg(Reg, RegState::Kill)
1531 .addImm(0)
1532 .addMemOperand(*MI.memoperands_begin());
1533 } else if (TM.getCodeModel() == CodeModel::Tiny) {
1534 BuildMI(MBB, MI, DL, get(AArch64::ADR), Reg)
1535 .addGlobalAddress(GV, 0, OpFlags);
1536 } else {
1537 BuildMI(MBB, MI, DL, get(AArch64::ADRP), Reg)
1538 .addGlobalAddress(GV, 0, OpFlags | AArch64II::MO_PAGE);
1539 unsigned char LoFlags = OpFlags | AArch64II::MO_PAGEOFF | MO_NC;
1540 BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
1541 .addReg(Reg, RegState::Kill)
1542 .addGlobalAddress(GV, 0, LoFlags)
1543 .addMemOperand(*MI.memoperands_begin());
1546 MBB.erase(MI);
1548 return true;
1551 // Return true if this instruction simply sets its single destination register
1552 // to zero. This is equivalent to a register rename of the zero-register.
1553 bool AArch64InstrInfo::isGPRZero(const MachineInstr &MI) {
1554 switch (MI.getOpcode()) {
1555 default:
1556 break;
1557 case AArch64::MOVZWi:
1558 case AArch64::MOVZXi: // movz Rd, #0 (LSL #0)
1559 if (MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) {
1560 assert(MI.getDesc().getNumOperands() == 3 &&
1561 MI.getOperand(2).getImm() == 0 && "invalid MOVZi operands");
1562 return true;
1564 break;
1565 case AArch64::ANDWri: // and Rd, Rzr, #imm
1566 return MI.getOperand(1).getReg() == AArch64::WZR;
1567 case AArch64::ANDXri:
1568 return MI.getOperand(1).getReg() == AArch64::XZR;
1569 case TargetOpcode::COPY:
1570 return MI.getOperand(1).getReg() == AArch64::WZR;
1572 return false;
1575 // Return true if this instruction simply renames a general register without
1576 // modifying bits.
1577 bool AArch64InstrInfo::isGPRCopy(const MachineInstr &MI) {
1578 switch (MI.getOpcode()) {
1579 default:
1580 break;
1581 case TargetOpcode::COPY: {
1582 // GPR32 copies will by lowered to ORRXrs
1583 Register DstReg = MI.getOperand(0).getReg();
1584 return (AArch64::GPR32RegClass.contains(DstReg) ||
1585 AArch64::GPR64RegClass.contains(DstReg));
1587 case AArch64::ORRXrs: // orr Xd, Xzr, Xm (LSL #0)
1588 if (MI.getOperand(1).getReg() == AArch64::XZR) {
1589 assert(MI.getDesc().getNumOperands() == 4 &&
1590 MI.getOperand(3).getImm() == 0 && "invalid ORRrs operands");
1591 return true;
1593 break;
1594 case AArch64::ADDXri: // add Xd, Xn, #0 (LSL #0)
1595 if (MI.getOperand(2).getImm() == 0) {
1596 assert(MI.getDesc().getNumOperands() == 4 &&
1597 MI.getOperand(3).getImm() == 0 && "invalid ADDXri operands");
1598 return true;
1600 break;
1602 return false;
1605 // Return true if this instruction simply renames a general register without
1606 // modifying bits.
1607 bool AArch64InstrInfo::isFPRCopy(const MachineInstr &MI) {
1608 switch (MI.getOpcode()) {
1609 default:
1610 break;
1611 case TargetOpcode::COPY: {
1612 // FPR64 copies will by lowered to ORR.16b
1613 Register DstReg = MI.getOperand(0).getReg();
1614 return (AArch64::FPR64RegClass.contains(DstReg) ||
1615 AArch64::FPR128RegClass.contains(DstReg));
1617 case AArch64::ORRv16i8:
1618 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
1619 assert(MI.getDesc().getNumOperands() == 3 && MI.getOperand(0).isReg() &&
1620 "invalid ORRv16i8 operands");
1621 return true;
1623 break;
1625 return false;
1628 unsigned AArch64InstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1629 int &FrameIndex) const {
1630 switch (MI.getOpcode()) {
1631 default:
1632 break;
1633 case AArch64::LDRWui:
1634 case AArch64::LDRXui:
1635 case AArch64::LDRBui:
1636 case AArch64::LDRHui:
1637 case AArch64::LDRSui:
1638 case AArch64::LDRDui:
1639 case AArch64::LDRQui:
1640 if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
1641 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
1642 FrameIndex = MI.getOperand(1).getIndex();
1643 return MI.getOperand(0).getReg();
1645 break;
1648 return 0;
1651 unsigned AArch64InstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1652 int &FrameIndex) const {
1653 switch (MI.getOpcode()) {
1654 default:
1655 break;
1656 case AArch64::STRWui:
1657 case AArch64::STRXui:
1658 case AArch64::STRBui:
1659 case AArch64::STRHui:
1660 case AArch64::STRSui:
1661 case AArch64::STRDui:
1662 case AArch64::STRQui:
1663 if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
1664 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
1665 FrameIndex = MI.getOperand(1).getIndex();
1666 return MI.getOperand(0).getReg();
1668 break;
1670 return 0;
1673 /// Check all MachineMemOperands for a hint to suppress pairing.
1674 bool AArch64InstrInfo::isLdStPairSuppressed(const MachineInstr &MI) {
1675 return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
1676 return MMO->getFlags() & MOSuppressPair;
1680 /// Set a flag on the first MachineMemOperand to suppress pairing.
1681 void AArch64InstrInfo::suppressLdStPair(MachineInstr &MI) {
1682 if (MI.memoperands_empty())
1683 return;
1684 (*MI.memoperands_begin())->setFlags(MOSuppressPair);
1687 /// Check all MachineMemOperands for a hint that the load/store is strided.
1688 bool AArch64InstrInfo::isStridedAccess(const MachineInstr &MI) {
1689 return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
1690 return MMO->getFlags() & MOStridedAccess;
1694 bool AArch64InstrInfo::isUnscaledLdSt(unsigned Opc) {
1695 switch (Opc) {
1696 default:
1697 return false;
1698 case AArch64::STURSi:
1699 case AArch64::STURDi:
1700 case AArch64::STURQi:
1701 case AArch64::STURBBi:
1702 case AArch64::STURHHi:
1703 case AArch64::STURWi:
1704 case AArch64::STURXi:
1705 case AArch64::LDURSi:
1706 case AArch64::LDURDi:
1707 case AArch64::LDURQi:
1708 case AArch64::LDURWi:
1709 case AArch64::LDURXi:
1710 case AArch64::LDURSWi:
1711 case AArch64::LDURHHi:
1712 case AArch64::LDURBBi:
1713 case AArch64::LDURSBWi:
1714 case AArch64::LDURSHWi:
1715 return true;
1719 Optional<unsigned> AArch64InstrInfo::getUnscaledLdSt(unsigned Opc) {
1720 switch (Opc) {
1721 default: return {};
1722 case AArch64::PRFMui: return AArch64::PRFUMi;
1723 case AArch64::LDRXui: return AArch64::LDURXi;
1724 case AArch64::LDRWui: return AArch64::LDURWi;
1725 case AArch64::LDRBui: return AArch64::LDURBi;
1726 case AArch64::LDRHui: return AArch64::LDURHi;
1727 case AArch64::LDRSui: return AArch64::LDURSi;
1728 case AArch64::LDRDui: return AArch64::LDURDi;
1729 case AArch64::LDRQui: return AArch64::LDURQi;
1730 case AArch64::LDRBBui: return AArch64::LDURBBi;
1731 case AArch64::LDRHHui: return AArch64::LDURHHi;
1732 case AArch64::LDRSBXui: return AArch64::LDURSBXi;
1733 case AArch64::LDRSBWui: return AArch64::LDURSBWi;
1734 case AArch64::LDRSHXui: return AArch64::LDURSHXi;
1735 case AArch64::LDRSHWui: return AArch64::LDURSHWi;
1736 case AArch64::LDRSWui: return AArch64::LDURSWi;
1737 case AArch64::STRXui: return AArch64::STURXi;
1738 case AArch64::STRWui: return AArch64::STURWi;
1739 case AArch64::STRBui: return AArch64::STURBi;
1740 case AArch64::STRHui: return AArch64::STURHi;
1741 case AArch64::STRSui: return AArch64::STURSi;
1742 case AArch64::STRDui: return AArch64::STURDi;
1743 case AArch64::STRQui: return AArch64::STURQi;
1744 case AArch64::STRBBui: return AArch64::STURBBi;
1745 case AArch64::STRHHui: return AArch64::STURHHi;
1749 unsigned AArch64InstrInfo::getLoadStoreImmIdx(unsigned Opc) {
1750 switch (Opc) {
1751 default:
1752 return 2;
1753 case AArch64::LDPXi:
1754 case AArch64::LDPDi:
1755 case AArch64::STPXi:
1756 case AArch64::STPDi:
1757 case AArch64::LDNPXi:
1758 case AArch64::LDNPDi:
1759 case AArch64::STNPXi:
1760 case AArch64::STNPDi:
1761 case AArch64::LDPQi:
1762 case AArch64::STPQi:
1763 case AArch64::LDNPQi:
1764 case AArch64::STNPQi:
1765 case AArch64::LDPWi:
1766 case AArch64::LDPSi:
1767 case AArch64::STPWi:
1768 case AArch64::STPSi:
1769 case AArch64::LDNPWi:
1770 case AArch64::LDNPSi:
1771 case AArch64::STNPWi:
1772 case AArch64::STNPSi:
1773 case AArch64::LDG:
1774 case AArch64::STGPi:
1775 return 3;
1776 case AArch64::ADDG:
1777 case AArch64::STGOffset:
1778 return 2;
1782 bool AArch64InstrInfo::isPairableLdStInst(const MachineInstr &MI) {
1783 switch (MI.getOpcode()) {
1784 default:
1785 return false;
1786 // Scaled instructions.
1787 case AArch64::STRSui:
1788 case AArch64::STRDui:
1789 case AArch64::STRQui:
1790 case AArch64::STRXui:
1791 case AArch64::STRWui:
1792 case AArch64::LDRSui:
1793 case AArch64::LDRDui:
1794 case AArch64::LDRQui:
1795 case AArch64::LDRXui:
1796 case AArch64::LDRWui:
1797 case AArch64::LDRSWui:
1798 // Unscaled instructions.
1799 case AArch64::STURSi:
1800 case AArch64::STURDi:
1801 case AArch64::STURQi:
1802 case AArch64::STURWi:
1803 case AArch64::STURXi:
1804 case AArch64::LDURSi:
1805 case AArch64::LDURDi:
1806 case AArch64::LDURQi:
1807 case AArch64::LDURWi:
1808 case AArch64::LDURXi:
1809 case AArch64::LDURSWi:
1810 return true;
1814 unsigned AArch64InstrInfo::convertToFlagSettingOpc(unsigned Opc,
1815 bool &Is64Bit) {
1816 switch (Opc) {
1817 default:
1818 llvm_unreachable("Opcode has no flag setting equivalent!");
1819 // 32-bit cases:
1820 case AArch64::ADDWri:
1821 Is64Bit = false;
1822 return AArch64::ADDSWri;
1823 case AArch64::ADDWrr:
1824 Is64Bit = false;
1825 return AArch64::ADDSWrr;
1826 case AArch64::ADDWrs:
1827 Is64Bit = false;
1828 return AArch64::ADDSWrs;
1829 case AArch64::ADDWrx:
1830 Is64Bit = false;
1831 return AArch64::ADDSWrx;
1832 case AArch64::ANDWri:
1833 Is64Bit = false;
1834 return AArch64::ANDSWri;
1835 case AArch64::ANDWrr:
1836 Is64Bit = false;
1837 return AArch64::ANDSWrr;
1838 case AArch64::ANDWrs:
1839 Is64Bit = false;
1840 return AArch64::ANDSWrs;
1841 case AArch64::BICWrr:
1842 Is64Bit = false;
1843 return AArch64::BICSWrr;
1844 case AArch64::BICWrs:
1845 Is64Bit = false;
1846 return AArch64::BICSWrs;
1847 case AArch64::SUBWri:
1848 Is64Bit = false;
1849 return AArch64::SUBSWri;
1850 case AArch64::SUBWrr:
1851 Is64Bit = false;
1852 return AArch64::SUBSWrr;
1853 case AArch64::SUBWrs:
1854 Is64Bit = false;
1855 return AArch64::SUBSWrs;
1856 case AArch64::SUBWrx:
1857 Is64Bit = false;
1858 return AArch64::SUBSWrx;
1859 // 64-bit cases:
1860 case AArch64::ADDXri:
1861 Is64Bit = true;
1862 return AArch64::ADDSXri;
1863 case AArch64::ADDXrr:
1864 Is64Bit = true;
1865 return AArch64::ADDSXrr;
1866 case AArch64::ADDXrs:
1867 Is64Bit = true;
1868 return AArch64::ADDSXrs;
1869 case AArch64::ADDXrx:
1870 Is64Bit = true;
1871 return AArch64::ADDSXrx;
1872 case AArch64::ANDXri:
1873 Is64Bit = true;
1874 return AArch64::ANDSXri;
1875 case AArch64::ANDXrr:
1876 Is64Bit = true;
1877 return AArch64::ANDSXrr;
1878 case AArch64::ANDXrs:
1879 Is64Bit = true;
1880 return AArch64::ANDSXrs;
1881 case AArch64::BICXrr:
1882 Is64Bit = true;
1883 return AArch64::BICSXrr;
1884 case AArch64::BICXrs:
1885 Is64Bit = true;
1886 return AArch64::BICSXrs;
1887 case AArch64::SUBXri:
1888 Is64Bit = true;
1889 return AArch64::SUBSXri;
1890 case AArch64::SUBXrr:
1891 Is64Bit = true;
1892 return AArch64::SUBSXrr;
1893 case AArch64::SUBXrs:
1894 Is64Bit = true;
1895 return AArch64::SUBSXrs;
1896 case AArch64::SUBXrx:
1897 Is64Bit = true;
1898 return AArch64::SUBSXrx;
1902 // Is this a candidate for ld/st merging or pairing? For example, we don't
1903 // touch volatiles or load/stores that have a hint to avoid pair formation.
1904 bool AArch64InstrInfo::isCandidateToMergeOrPair(const MachineInstr &MI) const {
1905 // If this is a volatile load/store, don't mess with it.
1906 if (MI.hasOrderedMemoryRef())
1907 return false;
1909 // Make sure this is a reg/fi+imm (as opposed to an address reloc).
1910 assert((MI.getOperand(1).isReg() || MI.getOperand(1).isFI()) &&
1911 "Expected a reg or frame index operand.");
1912 if (!MI.getOperand(2).isImm())
1913 return false;
1915 // Can't merge/pair if the instruction modifies the base register.
1916 // e.g., ldr x0, [x0]
1917 // This case will never occur with an FI base.
1918 if (MI.getOperand(1).isReg()) {
1919 Register BaseReg = MI.getOperand(1).getReg();
1920 const TargetRegisterInfo *TRI = &getRegisterInfo();
1921 if (MI.modifiesRegister(BaseReg, TRI))
1922 return false;
1925 // Check if this load/store has a hint to avoid pair formation.
1926 // MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
1927 if (isLdStPairSuppressed(MI))
1928 return false;
1930 // Do not pair any callee-save store/reload instructions in the
1931 // prologue/epilogue if the CFI information encoded the operations as separate
1932 // instructions, as that will cause the size of the actual prologue to mismatch
1933 // with the prologue size recorded in the Windows CFI.
1934 const MCAsmInfo *MAI = MI.getMF()->getTarget().getMCAsmInfo();
1935 bool NeedsWinCFI = MAI->usesWindowsCFI() &&
1936 MI.getMF()->getFunction().needsUnwindTableEntry();
1937 if (NeedsWinCFI && (MI.getFlag(MachineInstr::FrameSetup) ||
1938 MI.getFlag(MachineInstr::FrameDestroy)))
1939 return false;
1941 // On some CPUs quad load/store pairs are slower than two single load/stores.
1942 if (Subtarget.isPaired128Slow()) {
1943 switch (MI.getOpcode()) {
1944 default:
1945 break;
1946 case AArch64::LDURQi:
1947 case AArch64::STURQi:
1948 case AArch64::LDRQui:
1949 case AArch64::STRQui:
1950 return false;
1954 return true;
1957 bool AArch64InstrInfo::getMemOperandWithOffset(const MachineInstr &LdSt,
1958 const MachineOperand *&BaseOp,
1959 int64_t &Offset,
1960 const TargetRegisterInfo *TRI) const {
1961 unsigned Width;
1962 return getMemOperandWithOffsetWidth(LdSt, BaseOp, Offset, Width, TRI);
1965 bool AArch64InstrInfo::getMemOperandWithOffsetWidth(
1966 const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset,
1967 unsigned &Width, const TargetRegisterInfo *TRI) const {
1968 assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
1969 // Handle only loads/stores with base register followed by immediate offset.
1970 if (LdSt.getNumExplicitOperands() == 3) {
1971 // Non-paired instruction (e.g., ldr x1, [x0, #8]).
1972 if ((!LdSt.getOperand(1).isReg() && !LdSt.getOperand(1).isFI()) ||
1973 !LdSt.getOperand(2).isImm())
1974 return false;
1975 } else if (LdSt.getNumExplicitOperands() == 4) {
1976 // Paired instruction (e.g., ldp x1, x2, [x0, #8]).
1977 if (!LdSt.getOperand(1).isReg() ||
1978 (!LdSt.getOperand(2).isReg() && !LdSt.getOperand(2).isFI()) ||
1979 !LdSt.getOperand(3).isImm())
1980 return false;
1981 } else
1982 return false;
1984 // Get the scaling factor for the instruction and set the width for the
1985 // instruction.
1986 unsigned Scale = 0;
1987 int64_t Dummy1, Dummy2;
1989 // If this returns false, then it's an instruction we don't want to handle.
1990 if (!getMemOpInfo(LdSt.getOpcode(), Scale, Width, Dummy1, Dummy2))
1991 return false;
1993 // Compute the offset. Offset is calculated as the immediate operand
1994 // multiplied by the scaling factor. Unscaled instructions have scaling factor
1995 // set to 1.
1996 if (LdSt.getNumExplicitOperands() == 3) {
1997 BaseOp = &LdSt.getOperand(1);
1998 Offset = LdSt.getOperand(2).getImm() * Scale;
1999 } else {
2000 assert(LdSt.getNumExplicitOperands() == 4 && "invalid number of operands");
2001 BaseOp = &LdSt.getOperand(2);
2002 Offset = LdSt.getOperand(3).getImm() * Scale;
2005 assert((BaseOp->isReg() || BaseOp->isFI()) &&
2006 "getMemOperandWithOffset only supports base "
2007 "operands of type register or frame index.");
2009 return true;
2012 MachineOperand &
2013 AArch64InstrInfo::getMemOpBaseRegImmOfsOffsetOperand(MachineInstr &LdSt) const {
2014 assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
2015 MachineOperand &OfsOp = LdSt.getOperand(LdSt.getNumExplicitOperands() - 1);
2016 assert(OfsOp.isImm() && "Offset operand wasn't immediate.");
2017 return OfsOp;
2020 bool AArch64InstrInfo::getMemOpInfo(unsigned Opcode, unsigned &Scale,
2021 unsigned &Width, int64_t &MinOffset,
2022 int64_t &MaxOffset) {
2023 switch (Opcode) {
2024 // Not a memory operation or something we want to handle.
2025 default:
2026 Scale = Width = 0;
2027 MinOffset = MaxOffset = 0;
2028 return false;
2029 case AArch64::STRWpost:
2030 case AArch64::LDRWpost:
2031 Width = 32;
2032 Scale = 4;
2033 MinOffset = -256;
2034 MaxOffset = 255;
2035 break;
2036 case AArch64::LDURQi:
2037 case AArch64::STURQi:
2038 Width = 16;
2039 Scale = 1;
2040 MinOffset = -256;
2041 MaxOffset = 255;
2042 break;
2043 case AArch64::PRFUMi:
2044 case AArch64::LDURXi:
2045 case AArch64::LDURDi:
2046 case AArch64::STURXi:
2047 case AArch64::STURDi:
2048 Width = 8;
2049 Scale = 1;
2050 MinOffset = -256;
2051 MaxOffset = 255;
2052 break;
2053 case AArch64::LDURWi:
2054 case AArch64::LDURSi:
2055 case AArch64::LDURSWi:
2056 case AArch64::STURWi:
2057 case AArch64::STURSi:
2058 Width = 4;
2059 Scale = 1;
2060 MinOffset = -256;
2061 MaxOffset = 255;
2062 break;
2063 case AArch64::LDURHi:
2064 case AArch64::LDURHHi:
2065 case AArch64::LDURSHXi:
2066 case AArch64::LDURSHWi:
2067 case AArch64::STURHi:
2068 case AArch64::STURHHi:
2069 Width = 2;
2070 Scale = 1;
2071 MinOffset = -256;
2072 MaxOffset = 255;
2073 break;
2074 case AArch64::LDURBi:
2075 case AArch64::LDURBBi:
2076 case AArch64::LDURSBXi:
2077 case AArch64::LDURSBWi:
2078 case AArch64::STURBi:
2079 case AArch64::STURBBi:
2080 Width = 1;
2081 Scale = 1;
2082 MinOffset = -256;
2083 MaxOffset = 255;
2084 break;
2085 case AArch64::LDPQi:
2086 case AArch64::LDNPQi:
2087 case AArch64::STPQi:
2088 case AArch64::STNPQi:
2089 Scale = 16;
2090 Width = 32;
2091 MinOffset = -64;
2092 MaxOffset = 63;
2093 break;
2094 case AArch64::LDRQui:
2095 case AArch64::STRQui:
2096 Scale = Width = 16;
2097 MinOffset = 0;
2098 MaxOffset = 4095;
2099 break;
2100 case AArch64::LDPXi:
2101 case AArch64::LDPDi:
2102 case AArch64::LDNPXi:
2103 case AArch64::LDNPDi:
2104 case AArch64::STPXi:
2105 case AArch64::STPDi:
2106 case AArch64::STNPXi:
2107 case AArch64::STNPDi:
2108 Scale = 8;
2109 Width = 16;
2110 MinOffset = -64;
2111 MaxOffset = 63;
2112 break;
2113 case AArch64::PRFMui:
2114 case AArch64::LDRXui:
2115 case AArch64::LDRDui:
2116 case AArch64::STRXui:
2117 case AArch64::STRDui:
2118 Scale = Width = 8;
2119 MinOffset = 0;
2120 MaxOffset = 4095;
2121 break;
2122 case AArch64::LDPWi:
2123 case AArch64::LDPSi:
2124 case AArch64::LDNPWi:
2125 case AArch64::LDNPSi:
2126 case AArch64::STPWi:
2127 case AArch64::STPSi:
2128 case AArch64::STNPWi:
2129 case AArch64::STNPSi:
2130 Scale = 4;
2131 Width = 8;
2132 MinOffset = -64;
2133 MaxOffset = 63;
2134 break;
2135 case AArch64::LDRWui:
2136 case AArch64::LDRSui:
2137 case AArch64::LDRSWui:
2138 case AArch64::STRWui:
2139 case AArch64::STRSui:
2140 Scale = Width = 4;
2141 MinOffset = 0;
2142 MaxOffset = 4095;
2143 break;
2144 case AArch64::LDRHui:
2145 case AArch64::LDRHHui:
2146 case AArch64::LDRSHWui:
2147 case AArch64::LDRSHXui:
2148 case AArch64::STRHui:
2149 case AArch64::STRHHui:
2150 Scale = Width = 2;
2151 MinOffset = 0;
2152 MaxOffset = 4095;
2153 break;
2154 case AArch64::LDRBui:
2155 case AArch64::LDRBBui:
2156 case AArch64::LDRSBWui:
2157 case AArch64::LDRSBXui:
2158 case AArch64::STRBui:
2159 case AArch64::STRBBui:
2160 Scale = Width = 1;
2161 MinOffset = 0;
2162 MaxOffset = 4095;
2163 break;
2164 case AArch64::ADDG:
2165 case AArch64::TAGPstack:
2166 Scale = 16;
2167 Width = 0;
2168 MinOffset = 0;
2169 MaxOffset = 63;
2170 break;
2171 case AArch64::LDG:
2172 case AArch64::STGOffset:
2173 case AArch64::STZGOffset:
2174 Scale = Width = 16;
2175 MinOffset = -256;
2176 MaxOffset = 255;
2177 break;
2178 case AArch64::ST2GOffset:
2179 case AArch64::STZ2GOffset:
2180 Scale = 16;
2181 Width = 32;
2182 MinOffset = -256;
2183 MaxOffset = 255;
2184 break;
2185 case AArch64::STGPi:
2186 Scale = Width = 16;
2187 MinOffset = -64;
2188 MaxOffset = 63;
2189 break;
2192 return true;
2195 static unsigned getOffsetStride(unsigned Opc) {
2196 switch (Opc) {
2197 default:
2198 return 0;
2199 case AArch64::LDURQi:
2200 case AArch64::STURQi:
2201 return 16;
2202 case AArch64::LDURXi:
2203 case AArch64::LDURDi:
2204 case AArch64::STURXi:
2205 case AArch64::STURDi:
2206 return 8;
2207 case AArch64::LDURWi:
2208 case AArch64::LDURSi:
2209 case AArch64::LDURSWi:
2210 case AArch64::STURWi:
2211 case AArch64::STURSi:
2212 return 4;
2216 // Scale the unscaled offsets. Returns false if the unscaled offset can't be
2217 // scaled.
2218 static bool scaleOffset(unsigned Opc, int64_t &Offset) {
2219 unsigned OffsetStride = getOffsetStride(Opc);
2220 if (OffsetStride == 0)
2221 return false;
2222 // If the byte-offset isn't a multiple of the stride, we can't scale this
2223 // offset.
2224 if (Offset % OffsetStride != 0)
2225 return false;
2227 // Convert the byte-offset used by unscaled into an "element" offset used
2228 // by the scaled pair load/store instructions.
2229 Offset /= OffsetStride;
2230 return true;
2233 // Unscale the scaled offsets. Returns false if the scaled offset can't be
2234 // unscaled.
2235 static bool unscaleOffset(unsigned Opc, int64_t &Offset) {
2236 unsigned OffsetStride = getOffsetStride(Opc);
2237 if (OffsetStride == 0)
2238 return false;
2240 // Convert the "element" offset used by scaled pair load/store instructions
2241 // into the byte-offset used by unscaled.
2242 Offset *= OffsetStride;
2243 return true;
2246 static bool canPairLdStOpc(unsigned FirstOpc, unsigned SecondOpc) {
2247 if (FirstOpc == SecondOpc)
2248 return true;
2249 // We can also pair sign-ext and zero-ext instructions.
2250 switch (FirstOpc) {
2251 default:
2252 return false;
2253 case AArch64::LDRWui:
2254 case AArch64::LDURWi:
2255 return SecondOpc == AArch64::LDRSWui || SecondOpc == AArch64::LDURSWi;
2256 case AArch64::LDRSWui:
2257 case AArch64::LDURSWi:
2258 return SecondOpc == AArch64::LDRWui || SecondOpc == AArch64::LDURWi;
2260 // These instructions can't be paired based on their opcodes.
2261 return false;
2264 static bool shouldClusterFI(const MachineFrameInfo &MFI, int FI1,
2265 int64_t Offset1, unsigned Opcode1, int FI2,
2266 int64_t Offset2, unsigned Opcode2) {
2267 // Accesses through fixed stack object frame indices may access a different
2268 // fixed stack slot. Check that the object offsets + offsets match.
2269 if (MFI.isFixedObjectIndex(FI1) && MFI.isFixedObjectIndex(FI2)) {
2270 int64_t ObjectOffset1 = MFI.getObjectOffset(FI1);
2271 int64_t ObjectOffset2 = MFI.getObjectOffset(FI2);
2272 assert(ObjectOffset1 <= ObjectOffset2 && "Object offsets are not ordered.");
2273 // Get the byte-offset from the object offset.
2274 if (!unscaleOffset(Opcode1, Offset1) || !unscaleOffset(Opcode2, Offset2))
2275 return false;
2276 ObjectOffset1 += Offset1;
2277 ObjectOffset2 += Offset2;
2278 // Get the "element" index in the object.
2279 if (!scaleOffset(Opcode1, ObjectOffset1) ||
2280 !scaleOffset(Opcode2, ObjectOffset2))
2281 return false;
2282 return ObjectOffset1 + 1 == ObjectOffset2;
2285 return FI1 == FI2;
2288 /// Detect opportunities for ldp/stp formation.
2290 /// Only called for LdSt for which getMemOperandWithOffset returns true.
2291 bool AArch64InstrInfo::shouldClusterMemOps(const MachineOperand &BaseOp1,
2292 const MachineOperand &BaseOp2,
2293 unsigned NumLoads) const {
2294 const MachineInstr &FirstLdSt = *BaseOp1.getParent();
2295 const MachineInstr &SecondLdSt = *BaseOp2.getParent();
2296 if (BaseOp1.getType() != BaseOp2.getType())
2297 return false;
2299 assert((BaseOp1.isReg() || BaseOp1.isFI()) &&
2300 "Only base registers and frame indices are supported.");
2302 // Check for both base regs and base FI.
2303 if (BaseOp1.isReg() && BaseOp1.getReg() != BaseOp2.getReg())
2304 return false;
2306 // Only cluster up to a single pair.
2307 if (NumLoads > 1)
2308 return false;
2310 if (!isPairableLdStInst(FirstLdSt) || !isPairableLdStInst(SecondLdSt))
2311 return false;
2313 // Can we pair these instructions based on their opcodes?
2314 unsigned FirstOpc = FirstLdSt.getOpcode();
2315 unsigned SecondOpc = SecondLdSt.getOpcode();
2316 if (!canPairLdStOpc(FirstOpc, SecondOpc))
2317 return false;
2319 // Can't merge volatiles or load/stores that have a hint to avoid pair
2320 // formation, for example.
2321 if (!isCandidateToMergeOrPair(FirstLdSt) ||
2322 !isCandidateToMergeOrPair(SecondLdSt))
2323 return false;
2325 // isCandidateToMergeOrPair guarantees that operand 2 is an immediate.
2326 int64_t Offset1 = FirstLdSt.getOperand(2).getImm();
2327 if (isUnscaledLdSt(FirstOpc) && !scaleOffset(FirstOpc, Offset1))
2328 return false;
2330 int64_t Offset2 = SecondLdSt.getOperand(2).getImm();
2331 if (isUnscaledLdSt(SecondOpc) && !scaleOffset(SecondOpc, Offset2))
2332 return false;
2334 // Pairwise instructions have a 7-bit signed offset field.
2335 if (Offset1 > 63 || Offset1 < -64)
2336 return false;
2338 // The caller should already have ordered First/SecondLdSt by offset.
2339 // Note: except for non-equal frame index bases
2340 if (BaseOp1.isFI()) {
2341 assert((!BaseOp1.isIdenticalTo(BaseOp2) || Offset1 >= Offset2) &&
2342 "Caller should have ordered offsets.");
2344 const MachineFrameInfo &MFI =
2345 FirstLdSt.getParent()->getParent()->getFrameInfo();
2346 return shouldClusterFI(MFI, BaseOp1.getIndex(), Offset1, FirstOpc,
2347 BaseOp2.getIndex(), Offset2, SecondOpc);
2350 assert((!BaseOp1.isIdenticalTo(BaseOp2) || Offset1 <= Offset2) &&
2351 "Caller should have ordered offsets.");
2353 return Offset1 + 1 == Offset2;
2356 static const MachineInstrBuilder &AddSubReg(const MachineInstrBuilder &MIB,
2357 unsigned Reg, unsigned SubIdx,
2358 unsigned State,
2359 const TargetRegisterInfo *TRI) {
2360 if (!SubIdx)
2361 return MIB.addReg(Reg, State);
2363 if (Register::isPhysicalRegister(Reg))
2364 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
2365 return MIB.addReg(Reg, State, SubIdx);
2368 static bool forwardCopyWillClobberTuple(unsigned DestReg, unsigned SrcReg,
2369 unsigned NumRegs) {
2370 // We really want the positive remainder mod 32 here, that happens to be
2371 // easily obtainable with a mask.
2372 return ((DestReg - SrcReg) & 0x1f) < NumRegs;
2375 void AArch64InstrInfo::copyPhysRegTuple(MachineBasicBlock &MBB,
2376 MachineBasicBlock::iterator I,
2377 const DebugLoc &DL, unsigned DestReg,
2378 unsigned SrcReg, bool KillSrc,
2379 unsigned Opcode,
2380 ArrayRef<unsigned> Indices) const {
2381 assert(Subtarget.hasNEON() && "Unexpected register copy without NEON");
2382 const TargetRegisterInfo *TRI = &getRegisterInfo();
2383 uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
2384 uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
2385 unsigned NumRegs = Indices.size();
2387 int SubReg = 0, End = NumRegs, Incr = 1;
2388 if (forwardCopyWillClobberTuple(DestEncoding, SrcEncoding, NumRegs)) {
2389 SubReg = NumRegs - 1;
2390 End = -1;
2391 Incr = -1;
2394 for (; SubReg != End; SubReg += Incr) {
2395 const MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opcode));
2396 AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
2397 AddSubReg(MIB, SrcReg, Indices[SubReg], 0, TRI);
2398 AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
2402 void AArch64InstrInfo::copyGPRRegTuple(MachineBasicBlock &MBB,
2403 MachineBasicBlock::iterator I,
2404 DebugLoc DL, unsigned DestReg,
2405 unsigned SrcReg, bool KillSrc,
2406 unsigned Opcode, unsigned ZeroReg,
2407 llvm::ArrayRef<unsigned> Indices) const {
2408 const TargetRegisterInfo *TRI = &getRegisterInfo();
2409 unsigned NumRegs = Indices.size();
2411 #ifndef NDEBUG
2412 uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
2413 uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
2414 assert(DestEncoding % NumRegs == 0 && SrcEncoding % NumRegs == 0 &&
2415 "GPR reg sequences should not be able to overlap");
2416 #endif
2418 for (unsigned SubReg = 0; SubReg != NumRegs; ++SubReg) {
2419 const MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opcode));
2420 AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
2421 MIB.addReg(ZeroReg);
2422 AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
2423 MIB.addImm(0);
2427 void AArch64InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
2428 MachineBasicBlock::iterator I,
2429 const DebugLoc &DL, unsigned DestReg,
2430 unsigned SrcReg, bool KillSrc) const {
2431 if (AArch64::GPR32spRegClass.contains(DestReg) &&
2432 (AArch64::GPR32spRegClass.contains(SrcReg) || SrcReg == AArch64::WZR)) {
2433 const TargetRegisterInfo *TRI = &getRegisterInfo();
2435 if (DestReg == AArch64::WSP || SrcReg == AArch64::WSP) {
2436 // If either operand is WSP, expand to ADD #0.
2437 if (Subtarget.hasZeroCycleRegMove()) {
2438 // Cyclone recognizes "ADD Xd, Xn, #0" as a zero-cycle register move.
2439 unsigned DestRegX = TRI->getMatchingSuperReg(DestReg, AArch64::sub_32,
2440 &AArch64::GPR64spRegClass);
2441 unsigned SrcRegX = TRI->getMatchingSuperReg(SrcReg, AArch64::sub_32,
2442 &AArch64::GPR64spRegClass);
2443 // This instruction is reading and writing X registers. This may upset
2444 // the register scavenger and machine verifier, so we need to indicate
2445 // that we are reading an undefined value from SrcRegX, but a proper
2446 // value from SrcReg.
2447 BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestRegX)
2448 .addReg(SrcRegX, RegState::Undef)
2449 .addImm(0)
2450 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0))
2451 .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
2452 } else {
2453 BuildMI(MBB, I, DL, get(AArch64::ADDWri), DestReg)
2454 .addReg(SrcReg, getKillRegState(KillSrc))
2455 .addImm(0)
2456 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2458 } else if (SrcReg == AArch64::WZR && Subtarget.hasZeroCycleZeroingGP()) {
2459 BuildMI(MBB, I, DL, get(AArch64::MOVZWi), DestReg)
2460 .addImm(0)
2461 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2462 } else {
2463 if (Subtarget.hasZeroCycleRegMove()) {
2464 // Cyclone recognizes "ORR Xd, XZR, Xm" as a zero-cycle register move.
2465 unsigned DestRegX = TRI->getMatchingSuperReg(DestReg, AArch64::sub_32,
2466 &AArch64::GPR64spRegClass);
2467 unsigned SrcRegX = TRI->getMatchingSuperReg(SrcReg, AArch64::sub_32,
2468 &AArch64::GPR64spRegClass);
2469 // This instruction is reading and writing X registers. This may upset
2470 // the register scavenger and machine verifier, so we need to indicate
2471 // that we are reading an undefined value from SrcRegX, but a proper
2472 // value from SrcReg.
2473 BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestRegX)
2474 .addReg(AArch64::XZR)
2475 .addReg(SrcRegX, RegState::Undef)
2476 .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
2477 } else {
2478 // Otherwise, expand to ORR WZR.
2479 BuildMI(MBB, I, DL, get(AArch64::ORRWrr), DestReg)
2480 .addReg(AArch64::WZR)
2481 .addReg(SrcReg, getKillRegState(KillSrc));
2484 return;
2487 // Copy a Z register by ORRing with itself.
2488 if (AArch64::ZPRRegClass.contains(DestReg) &&
2489 AArch64::ZPRRegClass.contains(SrcReg)) {
2490 assert(Subtarget.hasSVE() && "Unexpected SVE register.");
2491 BuildMI(MBB, I, DL, get(AArch64::ORR_ZZZ), DestReg)
2492 .addReg(SrcReg)
2493 .addReg(SrcReg, getKillRegState(KillSrc));
2494 return;
2497 if (AArch64::GPR64spRegClass.contains(DestReg) &&
2498 (AArch64::GPR64spRegClass.contains(SrcReg) || SrcReg == AArch64::XZR)) {
2499 if (DestReg == AArch64::SP || SrcReg == AArch64::SP) {
2500 // If either operand is SP, expand to ADD #0.
2501 BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestReg)
2502 .addReg(SrcReg, getKillRegState(KillSrc))
2503 .addImm(0)
2504 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2505 } else if (SrcReg == AArch64::XZR && Subtarget.hasZeroCycleZeroingGP()) {
2506 BuildMI(MBB, I, DL, get(AArch64::MOVZXi), DestReg)
2507 .addImm(0)
2508 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
2509 } else {
2510 // Otherwise, expand to ORR XZR.
2511 BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestReg)
2512 .addReg(AArch64::XZR)
2513 .addReg(SrcReg, getKillRegState(KillSrc));
2515 return;
2518 // Copy a DDDD register quad by copying the individual sub-registers.
2519 if (AArch64::DDDDRegClass.contains(DestReg) &&
2520 AArch64::DDDDRegClass.contains(SrcReg)) {
2521 static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
2522 AArch64::dsub2, AArch64::dsub3};
2523 copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
2524 Indices);
2525 return;
2528 // Copy a DDD register triple by copying the individual sub-registers.
2529 if (AArch64::DDDRegClass.contains(DestReg) &&
2530 AArch64::DDDRegClass.contains(SrcReg)) {
2531 static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
2532 AArch64::dsub2};
2533 copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
2534 Indices);
2535 return;
2538 // Copy a DD register pair by copying the individual sub-registers.
2539 if (AArch64::DDRegClass.contains(DestReg) &&
2540 AArch64::DDRegClass.contains(SrcReg)) {
2541 static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1};
2542 copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
2543 Indices);
2544 return;
2547 // Copy a QQQQ register quad by copying the individual sub-registers.
2548 if (AArch64::QQQQRegClass.contains(DestReg) &&
2549 AArch64::QQQQRegClass.contains(SrcReg)) {
2550 static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
2551 AArch64::qsub2, AArch64::qsub3};
2552 copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
2553 Indices);
2554 return;
2557 // Copy a QQQ register triple by copying the individual sub-registers.
2558 if (AArch64::QQQRegClass.contains(DestReg) &&
2559 AArch64::QQQRegClass.contains(SrcReg)) {
2560 static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
2561 AArch64::qsub2};
2562 copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
2563 Indices);
2564 return;
2567 // Copy a QQ register pair by copying the individual sub-registers.
2568 if (AArch64::QQRegClass.contains(DestReg) &&
2569 AArch64::QQRegClass.contains(SrcReg)) {
2570 static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1};
2571 copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
2572 Indices);
2573 return;
2576 if (AArch64::XSeqPairsClassRegClass.contains(DestReg) &&
2577 AArch64::XSeqPairsClassRegClass.contains(SrcReg)) {
2578 static const unsigned Indices[] = {AArch64::sube64, AArch64::subo64};
2579 copyGPRRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRXrs,
2580 AArch64::XZR, Indices);
2581 return;
2584 if (AArch64::WSeqPairsClassRegClass.contains(DestReg) &&
2585 AArch64::WSeqPairsClassRegClass.contains(SrcReg)) {
2586 static const unsigned Indices[] = {AArch64::sube32, AArch64::subo32};
2587 copyGPRRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRWrs,
2588 AArch64::WZR, Indices);
2589 return;
2592 if (AArch64::FPR128RegClass.contains(DestReg) &&
2593 AArch64::FPR128RegClass.contains(SrcReg)) {
2594 if (Subtarget.hasNEON()) {
2595 BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2596 .addReg(SrcReg)
2597 .addReg(SrcReg, getKillRegState(KillSrc));
2598 } else {
2599 BuildMI(MBB, I, DL, get(AArch64::STRQpre))
2600 .addReg(AArch64::SP, RegState::Define)
2601 .addReg(SrcReg, getKillRegState(KillSrc))
2602 .addReg(AArch64::SP)
2603 .addImm(-16);
2604 BuildMI(MBB, I, DL, get(AArch64::LDRQpre))
2605 .addReg(AArch64::SP, RegState::Define)
2606 .addReg(DestReg, RegState::Define)
2607 .addReg(AArch64::SP)
2608 .addImm(16);
2610 return;
2613 if (AArch64::FPR64RegClass.contains(DestReg) &&
2614 AArch64::FPR64RegClass.contains(SrcReg)) {
2615 if (Subtarget.hasNEON()) {
2616 DestReg = RI.getMatchingSuperReg(DestReg, AArch64::dsub,
2617 &AArch64::FPR128RegClass);
2618 SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::dsub,
2619 &AArch64::FPR128RegClass);
2620 BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2621 .addReg(SrcReg)
2622 .addReg(SrcReg, getKillRegState(KillSrc));
2623 } else {
2624 BuildMI(MBB, I, DL, get(AArch64::FMOVDr), DestReg)
2625 .addReg(SrcReg, getKillRegState(KillSrc));
2627 return;
2630 if (AArch64::FPR32RegClass.contains(DestReg) &&
2631 AArch64::FPR32RegClass.contains(SrcReg)) {
2632 if (Subtarget.hasNEON()) {
2633 DestReg = RI.getMatchingSuperReg(DestReg, AArch64::ssub,
2634 &AArch64::FPR128RegClass);
2635 SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::ssub,
2636 &AArch64::FPR128RegClass);
2637 BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2638 .addReg(SrcReg)
2639 .addReg(SrcReg, getKillRegState(KillSrc));
2640 } else {
2641 BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
2642 .addReg(SrcReg, getKillRegState(KillSrc));
2644 return;
2647 if (AArch64::FPR16RegClass.contains(DestReg) &&
2648 AArch64::FPR16RegClass.contains(SrcReg)) {
2649 if (Subtarget.hasNEON()) {
2650 DestReg = RI.getMatchingSuperReg(DestReg, AArch64::hsub,
2651 &AArch64::FPR128RegClass);
2652 SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::hsub,
2653 &AArch64::FPR128RegClass);
2654 BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2655 .addReg(SrcReg)
2656 .addReg(SrcReg, getKillRegState(KillSrc));
2657 } else {
2658 DestReg = RI.getMatchingSuperReg(DestReg, AArch64::hsub,
2659 &AArch64::FPR32RegClass);
2660 SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::hsub,
2661 &AArch64::FPR32RegClass);
2662 BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
2663 .addReg(SrcReg, getKillRegState(KillSrc));
2665 return;
2668 if (AArch64::FPR8RegClass.contains(DestReg) &&
2669 AArch64::FPR8RegClass.contains(SrcReg)) {
2670 if (Subtarget.hasNEON()) {
2671 DestReg = RI.getMatchingSuperReg(DestReg, AArch64::bsub,
2672 &AArch64::FPR128RegClass);
2673 SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::bsub,
2674 &AArch64::FPR128RegClass);
2675 BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
2676 .addReg(SrcReg)
2677 .addReg(SrcReg, getKillRegState(KillSrc));
2678 } else {
2679 DestReg = RI.getMatchingSuperReg(DestReg, AArch64::bsub,
2680 &AArch64::FPR32RegClass);
2681 SrcReg = RI.getMatchingSuperReg(SrcReg, AArch64::bsub,
2682 &AArch64::FPR32RegClass);
2683 BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
2684 .addReg(SrcReg, getKillRegState(KillSrc));
2686 return;
2689 // Copies between GPR64 and FPR64.
2690 if (AArch64::FPR64RegClass.contains(DestReg) &&
2691 AArch64::GPR64RegClass.contains(SrcReg)) {
2692 BuildMI(MBB, I, DL, get(AArch64::FMOVXDr), DestReg)
2693 .addReg(SrcReg, getKillRegState(KillSrc));
2694 return;
2696 if (AArch64::GPR64RegClass.contains(DestReg) &&
2697 AArch64::FPR64RegClass.contains(SrcReg)) {
2698 BuildMI(MBB, I, DL, get(AArch64::FMOVDXr), DestReg)
2699 .addReg(SrcReg, getKillRegState(KillSrc));
2700 return;
2702 // Copies between GPR32 and FPR32.
2703 if (AArch64::FPR32RegClass.contains(DestReg) &&
2704 AArch64::GPR32RegClass.contains(SrcReg)) {
2705 BuildMI(MBB, I, DL, get(AArch64::FMOVWSr), DestReg)
2706 .addReg(SrcReg, getKillRegState(KillSrc));
2707 return;
2709 if (AArch64::GPR32RegClass.contains(DestReg) &&
2710 AArch64::FPR32RegClass.contains(SrcReg)) {
2711 BuildMI(MBB, I, DL, get(AArch64::FMOVSWr), DestReg)
2712 .addReg(SrcReg, getKillRegState(KillSrc));
2713 return;
2716 if (DestReg == AArch64::NZCV) {
2717 assert(AArch64::GPR64RegClass.contains(SrcReg) && "Invalid NZCV copy");
2718 BuildMI(MBB, I, DL, get(AArch64::MSR))
2719 .addImm(AArch64SysReg::NZCV)
2720 .addReg(SrcReg, getKillRegState(KillSrc))
2721 .addReg(AArch64::NZCV, RegState::Implicit | RegState::Define);
2722 return;
2725 if (SrcReg == AArch64::NZCV) {
2726 assert(AArch64::GPR64RegClass.contains(DestReg) && "Invalid NZCV copy");
2727 BuildMI(MBB, I, DL, get(AArch64::MRS), DestReg)
2728 .addImm(AArch64SysReg::NZCV)
2729 .addReg(AArch64::NZCV, RegState::Implicit | getKillRegState(KillSrc));
2730 return;
2733 llvm_unreachable("unimplemented reg-to-reg copy");
2736 static void storeRegPairToStackSlot(const TargetRegisterInfo &TRI,
2737 MachineBasicBlock &MBB,
2738 MachineBasicBlock::iterator InsertBefore,
2739 const MCInstrDesc &MCID,
2740 unsigned SrcReg, bool IsKill,
2741 unsigned SubIdx0, unsigned SubIdx1, int FI,
2742 MachineMemOperand *MMO) {
2743 unsigned SrcReg0 = SrcReg;
2744 unsigned SrcReg1 = SrcReg;
2745 if (Register::isPhysicalRegister(SrcReg)) {
2746 SrcReg0 = TRI.getSubReg(SrcReg, SubIdx0);
2747 SubIdx0 = 0;
2748 SrcReg1 = TRI.getSubReg(SrcReg, SubIdx1);
2749 SubIdx1 = 0;
2751 BuildMI(MBB, InsertBefore, DebugLoc(), MCID)
2752 .addReg(SrcReg0, getKillRegState(IsKill), SubIdx0)
2753 .addReg(SrcReg1, getKillRegState(IsKill), SubIdx1)
2754 .addFrameIndex(FI)
2755 .addImm(0)
2756 .addMemOperand(MMO);
2759 void AArch64InstrInfo::storeRegToStackSlot(
2760 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
2761 bool isKill, int FI, const TargetRegisterClass *RC,
2762 const TargetRegisterInfo *TRI) const {
2763 MachineFunction &MF = *MBB.getParent();
2764 MachineFrameInfo &MFI = MF.getFrameInfo();
2765 unsigned Align = MFI.getObjectAlignment(FI);
2767 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
2768 MachineMemOperand *MMO = MF.getMachineMemOperand(
2769 PtrInfo, MachineMemOperand::MOStore, MFI.getObjectSize(FI), Align);
2770 unsigned Opc = 0;
2771 bool Offset = true;
2772 switch (TRI->getSpillSize(*RC)) {
2773 case 1:
2774 if (AArch64::FPR8RegClass.hasSubClassEq(RC))
2775 Opc = AArch64::STRBui;
2776 break;
2777 case 2:
2778 if (AArch64::FPR16RegClass.hasSubClassEq(RC))
2779 Opc = AArch64::STRHui;
2780 break;
2781 case 4:
2782 if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
2783 Opc = AArch64::STRWui;
2784 if (Register::isVirtualRegister(SrcReg))
2785 MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
2786 else
2787 assert(SrcReg != AArch64::WSP);
2788 } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
2789 Opc = AArch64::STRSui;
2790 break;
2791 case 8:
2792 if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
2793 Opc = AArch64::STRXui;
2794 if (Register::isVirtualRegister(SrcReg))
2795 MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
2796 else
2797 assert(SrcReg != AArch64::SP);
2798 } else if (AArch64::FPR64RegClass.hasSubClassEq(RC)) {
2799 Opc = AArch64::STRDui;
2800 } else if (AArch64::WSeqPairsClassRegClass.hasSubClassEq(RC)) {
2801 storeRegPairToStackSlot(getRegisterInfo(), MBB, MBBI,
2802 get(AArch64::STPWi), SrcReg, isKill,
2803 AArch64::sube32, AArch64::subo32, FI, MMO);
2804 return;
2806 break;
2807 case 16:
2808 if (AArch64::FPR128RegClass.hasSubClassEq(RC))
2809 Opc = AArch64::STRQui;
2810 else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
2811 assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2812 Opc = AArch64::ST1Twov1d;
2813 Offset = false;
2814 } else if (AArch64::XSeqPairsClassRegClass.hasSubClassEq(RC)) {
2815 storeRegPairToStackSlot(getRegisterInfo(), MBB, MBBI,
2816 get(AArch64::STPXi), SrcReg, isKill,
2817 AArch64::sube64, AArch64::subo64, FI, MMO);
2818 return;
2820 break;
2821 case 24:
2822 if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
2823 assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2824 Opc = AArch64::ST1Threev1d;
2825 Offset = false;
2827 break;
2828 case 32:
2829 if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
2830 assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2831 Opc = AArch64::ST1Fourv1d;
2832 Offset = false;
2833 } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
2834 assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2835 Opc = AArch64::ST1Twov2d;
2836 Offset = false;
2838 break;
2839 case 48:
2840 if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
2841 assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2842 Opc = AArch64::ST1Threev2d;
2843 Offset = false;
2845 break;
2846 case 64:
2847 if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
2848 assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
2849 Opc = AArch64::ST1Fourv2d;
2850 Offset = false;
2852 break;
2854 assert(Opc && "Unknown register class");
2856 const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DebugLoc(), get(Opc))
2857 .addReg(SrcReg, getKillRegState(isKill))
2858 .addFrameIndex(FI);
2860 if (Offset)
2861 MI.addImm(0);
2862 MI.addMemOperand(MMO);
2865 static void loadRegPairFromStackSlot(const TargetRegisterInfo &TRI,
2866 MachineBasicBlock &MBB,
2867 MachineBasicBlock::iterator InsertBefore,
2868 const MCInstrDesc &MCID,
2869 unsigned DestReg, unsigned SubIdx0,
2870 unsigned SubIdx1, int FI,
2871 MachineMemOperand *MMO) {
2872 unsigned DestReg0 = DestReg;
2873 unsigned DestReg1 = DestReg;
2874 bool IsUndef = true;
2875 if (Register::isPhysicalRegister(DestReg)) {
2876 DestReg0 = TRI.getSubReg(DestReg, SubIdx0);
2877 SubIdx0 = 0;
2878 DestReg1 = TRI.getSubReg(DestReg, SubIdx1);
2879 SubIdx1 = 0;
2880 IsUndef = false;
2882 BuildMI(MBB, InsertBefore, DebugLoc(), MCID)
2883 .addReg(DestReg0, RegState::Define | getUndefRegState(IsUndef), SubIdx0)
2884 .addReg(DestReg1, RegState::Define | getUndefRegState(IsUndef), SubIdx1)
2885 .addFrameIndex(FI)
2886 .addImm(0)
2887 .addMemOperand(MMO);
2890 void AArch64InstrInfo::loadRegFromStackSlot(
2891 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
2892 int FI, const TargetRegisterClass *RC,
2893 const TargetRegisterInfo *TRI) const {
2894 MachineFunction &MF = *MBB.getParent();
2895 MachineFrameInfo &MFI = MF.getFrameInfo();
2896 unsigned Align = MFI.getObjectAlignment(FI);
2897 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
2898 MachineMemOperand *MMO = MF.getMachineMemOperand(
2899 PtrInfo, MachineMemOperand::MOLoad, MFI.getObjectSize(FI), Align);
2901 unsigned Opc = 0;
2902 bool Offset = true;
2903 switch (TRI->getSpillSize(*RC)) {
2904 case 1:
2905 if (AArch64::FPR8RegClass.hasSubClassEq(RC))
2906 Opc = AArch64::LDRBui;
2907 break;
2908 case 2:
2909 if (AArch64::FPR16RegClass.hasSubClassEq(RC))
2910 Opc = AArch64::LDRHui;
2911 break;
2912 case 4:
2913 if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
2914 Opc = AArch64::LDRWui;
2915 if (Register::isVirtualRegister(DestReg))
2916 MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR32RegClass);
2917 else
2918 assert(DestReg != AArch64::WSP);
2919 } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
2920 Opc = AArch64::LDRSui;
2921 break;
2922 case 8:
2923 if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
2924 Opc = AArch64::LDRXui;
2925 if (Register::isVirtualRegister(DestReg))
2926 MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR64RegClass);
2927 else
2928 assert(DestReg != AArch64::SP);
2929 } else if (AArch64::FPR64RegClass.hasSubClassEq(RC)) {
2930 Opc = AArch64::LDRDui;
2931 } else if (AArch64::WSeqPairsClassRegClass.hasSubClassEq(RC)) {
2932 loadRegPairFromStackSlot(getRegisterInfo(), MBB, MBBI,
2933 get(AArch64::LDPWi), DestReg, AArch64::sube32,
2934 AArch64::subo32, FI, MMO);
2935 return;
2937 break;
2938 case 16:
2939 if (AArch64::FPR128RegClass.hasSubClassEq(RC))
2940 Opc = AArch64::LDRQui;
2941 else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
2942 assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2943 Opc = AArch64::LD1Twov1d;
2944 Offset = false;
2945 } else if (AArch64::XSeqPairsClassRegClass.hasSubClassEq(RC)) {
2946 loadRegPairFromStackSlot(getRegisterInfo(), MBB, MBBI,
2947 get(AArch64::LDPXi), DestReg, AArch64::sube64,
2948 AArch64::subo64, FI, MMO);
2949 return;
2951 break;
2952 case 24:
2953 if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
2954 assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2955 Opc = AArch64::LD1Threev1d;
2956 Offset = false;
2958 break;
2959 case 32:
2960 if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
2961 assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2962 Opc = AArch64::LD1Fourv1d;
2963 Offset = false;
2964 } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
2965 assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2966 Opc = AArch64::LD1Twov2d;
2967 Offset = false;
2969 break;
2970 case 48:
2971 if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
2972 assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2973 Opc = AArch64::LD1Threev2d;
2974 Offset = false;
2976 break;
2977 case 64:
2978 if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
2979 assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
2980 Opc = AArch64::LD1Fourv2d;
2981 Offset = false;
2983 break;
2985 assert(Opc && "Unknown register class");
2987 const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DebugLoc(), get(Opc))
2988 .addReg(DestReg, getDefRegState(true))
2989 .addFrameIndex(FI);
2990 if (Offset)
2991 MI.addImm(0);
2992 MI.addMemOperand(MMO);
2995 // Helper function to emit a frame offset adjustment from a given
2996 // pointer (SrcReg), stored into DestReg. This function is explicit
2997 // in that it requires the opcode.
2998 static void emitFrameOffsetAdj(MachineBasicBlock &MBB,
2999 MachineBasicBlock::iterator MBBI,
3000 const DebugLoc &DL, unsigned DestReg,
3001 unsigned SrcReg, int64_t Offset, unsigned Opc,
3002 const TargetInstrInfo *TII,
3003 MachineInstr::MIFlag Flag, bool NeedsWinCFI,
3004 bool *HasWinCFI) {
3005 int Sign = 1;
3006 unsigned MaxEncoding, ShiftSize;
3007 switch (Opc) {
3008 case AArch64::ADDXri:
3009 case AArch64::ADDSXri:
3010 case AArch64::SUBXri:
3011 case AArch64::SUBSXri:
3012 MaxEncoding = 0xfff;
3013 ShiftSize = 12;
3014 break;
3015 default:
3016 llvm_unreachable("Unsupported opcode");
3019 // FIXME: If the offset won't fit in 24-bits, compute the offset into a
3020 // scratch register. If DestReg is a virtual register, use it as the
3021 // scratch register; otherwise, create a new virtual register (to be
3022 // replaced by the scavenger at the end of PEI). That case can be optimized
3023 // slightly if DestReg is SP which is always 16-byte aligned, so the scratch
3024 // register can be loaded with offset%8 and the add/sub can use an extending
3025 // instruction with LSL#3.
3026 // Currently the function handles any offsets but generates a poor sequence
3027 // of code.
3028 // assert(Offset < (1 << 24) && "unimplemented reg plus immediate");
3030 const unsigned MaxEncodableValue = MaxEncoding << ShiftSize;
3031 do {
3032 unsigned ThisVal = std::min<unsigned>(Offset, MaxEncodableValue);
3033 unsigned LocalShiftSize = 0;
3034 if (ThisVal > MaxEncoding) {
3035 ThisVal = ThisVal >> ShiftSize;
3036 LocalShiftSize = ShiftSize;
3038 assert((ThisVal >> ShiftSize) <= MaxEncoding &&
3039 "Encoding cannot handle value that big");
3040 auto MBI = BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
3041 .addReg(SrcReg)
3042 .addImm(Sign * (int)ThisVal);
3043 if (ShiftSize)
3044 MBI = MBI.addImm(
3045 AArch64_AM::getShifterImm(AArch64_AM::LSL, LocalShiftSize));
3046 MBI = MBI.setMIFlag(Flag);
3048 if (NeedsWinCFI) {
3049 assert(Sign == 1 && "SEH directives should always have a positive sign");
3050 int Imm = (int)(ThisVal << LocalShiftSize);
3051 if ((DestReg == AArch64::FP && SrcReg == AArch64::SP) ||
3052 (SrcReg == AArch64::FP && DestReg == AArch64::SP)) {
3053 if (HasWinCFI)
3054 *HasWinCFI = true;
3055 if (Imm == 0)
3056 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_SetFP)).setMIFlag(Flag);
3057 else
3058 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_AddFP))
3059 .addImm(Imm)
3060 .setMIFlag(Flag);
3061 assert((Offset - Imm) == 0 && "Expected remaining offset to be zero to "
3062 "emit a single SEH directive");
3063 } else if (DestReg == AArch64::SP) {
3064 if (HasWinCFI)
3065 *HasWinCFI = true;
3066 assert(SrcReg == AArch64::SP && "Unexpected SrcReg for SEH_StackAlloc");
3067 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_StackAlloc))
3068 .addImm(Imm)
3069 .setMIFlag(Flag);
3071 if (HasWinCFI)
3072 *HasWinCFI = true;
3075 SrcReg = DestReg;
3076 Offset -= ThisVal << LocalShiftSize;
3077 } while (Offset);
3080 void llvm::emitFrameOffset(MachineBasicBlock &MBB,
3081 MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
3082 unsigned DestReg, unsigned SrcReg,
3083 StackOffset Offset, const TargetInstrInfo *TII,
3084 MachineInstr::MIFlag Flag, bool SetNZCV,
3085 bool NeedsWinCFI, bool *HasWinCFI) {
3086 int64_t Bytes;
3087 Offset.getForFrameOffset(Bytes);
3089 // First emit non-scalable frame offsets, or a simple 'mov'.
3090 if (Bytes || (!Offset && SrcReg != DestReg)) {
3091 assert((DestReg != AArch64::SP || Bytes % 16 == 0) &&
3092 "SP increment/decrement not 16-byte aligned");
3093 unsigned Opc = SetNZCV ? AArch64::ADDSXri : AArch64::ADDXri;
3094 if (Bytes < 0) {
3095 Bytes = -Bytes;
3096 Opc = SetNZCV ? AArch64::SUBSXri : AArch64::SUBXri;
3098 emitFrameOffsetAdj(MBB, MBBI, DL, DestReg, SrcReg, Bytes, Opc, TII, Flag,
3099 NeedsWinCFI, HasWinCFI);
3100 SrcReg = DestReg;
3104 MachineInstr *AArch64InstrInfo::foldMemoryOperandImpl(
3105 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
3106 MachineBasicBlock::iterator InsertPt, int FrameIndex,
3107 LiveIntervals *LIS, VirtRegMap *VRM) const {
3108 // This is a bit of a hack. Consider this instruction:
3110 // %0 = COPY %sp; GPR64all:%0
3112 // We explicitly chose GPR64all for the virtual register so such a copy might
3113 // be eliminated by RegisterCoalescer. However, that may not be possible, and
3114 // %0 may even spill. We can't spill %sp, and since it is in the GPR64all
3115 // register class, TargetInstrInfo::foldMemoryOperand() is going to try.
3117 // To prevent that, we are going to constrain the %0 register class here.
3119 // <rdar://problem/11522048>
3121 if (MI.isFullCopy()) {
3122 Register DstReg = MI.getOperand(0).getReg();
3123 Register SrcReg = MI.getOperand(1).getReg();
3124 if (SrcReg == AArch64::SP && Register::isVirtualRegister(DstReg)) {
3125 MF.getRegInfo().constrainRegClass(DstReg, &AArch64::GPR64RegClass);
3126 return nullptr;
3128 if (DstReg == AArch64::SP && Register::isVirtualRegister(SrcReg)) {
3129 MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
3130 return nullptr;
3134 // Handle the case where a copy is being spilled or filled but the source
3135 // and destination register class don't match. For example:
3137 // %0 = COPY %xzr; GPR64common:%0
3139 // In this case we can still safely fold away the COPY and generate the
3140 // following spill code:
3142 // STRXui %xzr, %stack.0
3144 // This also eliminates spilled cross register class COPYs (e.g. between x and
3145 // d regs) of the same size. For example:
3147 // %0 = COPY %1; GPR64:%0, FPR64:%1
3149 // will be filled as
3151 // LDRDui %0, fi<#0>
3153 // instead of
3155 // LDRXui %Temp, fi<#0>
3156 // %0 = FMOV %Temp
3158 if (MI.isCopy() && Ops.size() == 1 &&
3159 // Make sure we're only folding the explicit COPY defs/uses.
3160 (Ops[0] == 0 || Ops[0] == 1)) {
3161 bool IsSpill = Ops[0] == 0;
3162 bool IsFill = !IsSpill;
3163 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
3164 const MachineRegisterInfo &MRI = MF.getRegInfo();
3165 MachineBasicBlock &MBB = *MI.getParent();
3166 const MachineOperand &DstMO = MI.getOperand(0);
3167 const MachineOperand &SrcMO = MI.getOperand(1);
3168 Register DstReg = DstMO.getReg();
3169 Register SrcReg = SrcMO.getReg();
3170 // This is slightly expensive to compute for physical regs since
3171 // getMinimalPhysRegClass is slow.
3172 auto getRegClass = [&](unsigned Reg) {
3173 return Register::isVirtualRegister(Reg) ? MRI.getRegClass(Reg)
3174 : TRI.getMinimalPhysRegClass(Reg);
3177 if (DstMO.getSubReg() == 0 && SrcMO.getSubReg() == 0) {
3178 assert(TRI.getRegSizeInBits(*getRegClass(DstReg)) ==
3179 TRI.getRegSizeInBits(*getRegClass(SrcReg)) &&
3180 "Mismatched register size in non subreg COPY");
3181 if (IsSpill)
3182 storeRegToStackSlot(MBB, InsertPt, SrcReg, SrcMO.isKill(), FrameIndex,
3183 getRegClass(SrcReg), &TRI);
3184 else
3185 loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex,
3186 getRegClass(DstReg), &TRI);
3187 return &*--InsertPt;
3190 // Handle cases like spilling def of:
3192 // %0:sub_32<def,read-undef> = COPY %wzr; GPR64common:%0
3194 // where the physical register source can be widened and stored to the full
3195 // virtual reg destination stack slot, in this case producing:
3197 // STRXui %xzr, %stack.0
3199 if (IsSpill && DstMO.isUndef() && Register::isPhysicalRegister(SrcReg)) {
3200 assert(SrcMO.getSubReg() == 0 &&
3201 "Unexpected subreg on physical register");
3202 const TargetRegisterClass *SpillRC;
3203 unsigned SpillSubreg;
3204 switch (DstMO.getSubReg()) {
3205 default:
3206 SpillRC = nullptr;
3207 break;
3208 case AArch64::sub_32:
3209 case AArch64::ssub:
3210 if (AArch64::GPR32RegClass.contains(SrcReg)) {
3211 SpillRC = &AArch64::GPR64RegClass;
3212 SpillSubreg = AArch64::sub_32;
3213 } else if (AArch64::FPR32RegClass.contains(SrcReg)) {
3214 SpillRC = &AArch64::FPR64RegClass;
3215 SpillSubreg = AArch64::ssub;
3216 } else
3217 SpillRC = nullptr;
3218 break;
3219 case AArch64::dsub:
3220 if (AArch64::FPR64RegClass.contains(SrcReg)) {
3221 SpillRC = &AArch64::FPR128RegClass;
3222 SpillSubreg = AArch64::dsub;
3223 } else
3224 SpillRC = nullptr;
3225 break;
3228 if (SpillRC)
3229 if (unsigned WidenedSrcReg =
3230 TRI.getMatchingSuperReg(SrcReg, SpillSubreg, SpillRC)) {
3231 storeRegToStackSlot(MBB, InsertPt, WidenedSrcReg, SrcMO.isKill(),
3232 FrameIndex, SpillRC, &TRI);
3233 return &*--InsertPt;
3237 // Handle cases like filling use of:
3239 // %0:sub_32<def,read-undef> = COPY %1; GPR64:%0, GPR32:%1
3241 // where we can load the full virtual reg source stack slot, into the subreg
3242 // destination, in this case producing:
3244 // LDRWui %0:sub_32<def,read-undef>, %stack.0
3246 if (IsFill && SrcMO.getSubReg() == 0 && DstMO.isUndef()) {
3247 const TargetRegisterClass *FillRC;
3248 switch (DstMO.getSubReg()) {
3249 default:
3250 FillRC = nullptr;
3251 break;
3252 case AArch64::sub_32:
3253 FillRC = &AArch64::GPR32RegClass;
3254 break;
3255 case AArch64::ssub:
3256 FillRC = &AArch64::FPR32RegClass;
3257 break;
3258 case AArch64::dsub:
3259 FillRC = &AArch64::FPR64RegClass;
3260 break;
3263 if (FillRC) {
3264 assert(TRI.getRegSizeInBits(*getRegClass(SrcReg)) ==
3265 TRI.getRegSizeInBits(*FillRC) &&
3266 "Mismatched regclass size on folded subreg COPY");
3267 loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex, FillRC, &TRI);
3268 MachineInstr &LoadMI = *--InsertPt;
3269 MachineOperand &LoadDst = LoadMI.getOperand(0);
3270 assert(LoadDst.getSubReg() == 0 && "unexpected subreg on fill load");
3271 LoadDst.setSubReg(DstMO.getSubReg());
3272 LoadDst.setIsUndef();
3273 return &LoadMI;
3278 // Cannot fold.
3279 return nullptr;
3282 int llvm::isAArch64FrameOffsetLegal(const MachineInstr &MI,
3283 StackOffset &SOffset,
3284 bool *OutUseUnscaledOp,
3285 unsigned *OutUnscaledOp,
3286 int *EmittableOffset) {
3287 // Set output values in case of early exit.
3288 if (EmittableOffset)
3289 *EmittableOffset = 0;
3290 if (OutUseUnscaledOp)
3291 *OutUseUnscaledOp = false;
3292 if (OutUnscaledOp)
3293 *OutUnscaledOp = 0;
3295 // Exit early for structured vector spills/fills as they can't take an
3296 // immediate offset.
3297 switch (MI.getOpcode()) {
3298 default:
3299 break;
3300 case AArch64::LD1Twov2d:
3301 case AArch64::LD1Threev2d:
3302 case AArch64::LD1Fourv2d:
3303 case AArch64::LD1Twov1d:
3304 case AArch64::LD1Threev1d:
3305 case AArch64::LD1Fourv1d:
3306 case AArch64::ST1Twov2d:
3307 case AArch64::ST1Threev2d:
3308 case AArch64::ST1Fourv2d:
3309 case AArch64::ST1Twov1d:
3310 case AArch64::ST1Threev1d:
3311 case AArch64::ST1Fourv1d:
3312 case AArch64::IRG:
3313 case AArch64::IRGstack:
3314 return AArch64FrameOffsetCannotUpdate;
3317 // Get the min/max offset and the scale.
3318 unsigned Scale, Width;
3319 int64_t MinOff, MaxOff;
3320 if (!AArch64InstrInfo::getMemOpInfo(MI.getOpcode(), Scale, Width, MinOff,
3321 MaxOff))
3322 llvm_unreachable("unhandled opcode in isAArch64FrameOffsetLegal");
3324 // Construct the complete offset.
3325 const MachineOperand &ImmOpnd =
3326 MI.getOperand(AArch64InstrInfo::getLoadStoreImmIdx(MI.getOpcode()));
3327 int Offset = SOffset.getBytes() + ImmOpnd.getImm() * Scale;
3329 // If the offset doesn't match the scale, we rewrite the instruction to
3330 // use the unscaled instruction instead. Likewise, if we have a negative
3331 // offset and there is an unscaled op to use.
3332 Optional<unsigned> UnscaledOp =
3333 AArch64InstrInfo::getUnscaledLdSt(MI.getOpcode());
3334 bool useUnscaledOp = UnscaledOp && (Offset % Scale || Offset < 0);
3335 if (useUnscaledOp &&
3336 !AArch64InstrInfo::getMemOpInfo(*UnscaledOp, Scale, Width, MinOff, MaxOff))
3337 llvm_unreachable("unhandled opcode in isAArch64FrameOffsetLegal");
3339 int64_t Remainder = Offset % Scale;
3340 assert(!(Remainder && useUnscaledOp) &&
3341 "Cannot have remainder when using unscaled op");
3343 assert(MinOff < MaxOff && "Unexpected Min/Max offsets");
3344 int NewOffset = Offset / Scale;
3345 if (MinOff <= NewOffset && NewOffset <= MaxOff)
3346 Offset = Remainder;
3347 else {
3348 NewOffset = NewOffset < 0 ? MinOff : MaxOff;
3349 Offset = Offset - NewOffset * Scale + Remainder;
3352 if (EmittableOffset)
3353 *EmittableOffset = NewOffset;
3354 if (OutUseUnscaledOp)
3355 *OutUseUnscaledOp = useUnscaledOp;
3356 if (OutUnscaledOp && UnscaledOp)
3357 *OutUnscaledOp = *UnscaledOp;
3359 SOffset = StackOffset(Offset, MVT::i8);
3360 return AArch64FrameOffsetCanUpdate |
3361 (Offset == 0 ? AArch64FrameOffsetIsLegal : 0);
3364 bool llvm::rewriteAArch64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
3365 unsigned FrameReg, StackOffset &Offset,
3366 const AArch64InstrInfo *TII) {
3367 unsigned Opcode = MI.getOpcode();
3368 unsigned ImmIdx = FrameRegIdx + 1;
3370 if (Opcode == AArch64::ADDSXri || Opcode == AArch64::ADDXri) {
3371 Offset += StackOffset(MI.getOperand(ImmIdx).getImm(), MVT::i8);
3372 emitFrameOffset(*MI.getParent(), MI, MI.getDebugLoc(),
3373 MI.getOperand(0).getReg(), FrameReg, Offset, TII,
3374 MachineInstr::NoFlags, (Opcode == AArch64::ADDSXri));
3375 MI.eraseFromParent();
3376 Offset = StackOffset();
3377 return true;
3380 int NewOffset;
3381 unsigned UnscaledOp;
3382 bool UseUnscaledOp;
3383 int Status = isAArch64FrameOffsetLegal(MI, Offset, &UseUnscaledOp,
3384 &UnscaledOp, &NewOffset);
3385 if (Status & AArch64FrameOffsetCanUpdate) {
3386 if (Status & AArch64FrameOffsetIsLegal)
3387 // Replace the FrameIndex with FrameReg.
3388 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
3389 if (UseUnscaledOp)
3390 MI.setDesc(TII->get(UnscaledOp));
3392 MI.getOperand(ImmIdx).ChangeToImmediate(NewOffset);
3393 return !Offset;
3396 return false;
3399 void AArch64InstrInfo::getNoop(MCInst &NopInst) const {
3400 NopInst.setOpcode(AArch64::HINT);
3401 NopInst.addOperand(MCOperand::createImm(0));
3404 // AArch64 supports MachineCombiner.
3405 bool AArch64InstrInfo::useMachineCombiner() const { return true; }
3407 // True when Opc sets flag
3408 static bool isCombineInstrSettingFlag(unsigned Opc) {
3409 switch (Opc) {
3410 case AArch64::ADDSWrr:
3411 case AArch64::ADDSWri:
3412 case AArch64::ADDSXrr:
3413 case AArch64::ADDSXri:
3414 case AArch64::SUBSWrr:
3415 case AArch64::SUBSXrr:
3416 // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
3417 case AArch64::SUBSWri:
3418 case AArch64::SUBSXri:
3419 return true;
3420 default:
3421 break;
3423 return false;
3426 // 32b Opcodes that can be combined with a MUL
3427 static bool isCombineInstrCandidate32(unsigned Opc) {
3428 switch (Opc) {
3429 case AArch64::ADDWrr:
3430 case AArch64::ADDWri:
3431 case AArch64::SUBWrr:
3432 case AArch64::ADDSWrr:
3433 case AArch64::ADDSWri:
3434 case AArch64::SUBSWrr:
3435 // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
3436 case AArch64::SUBWri:
3437 case AArch64::SUBSWri:
3438 return true;
3439 default:
3440 break;
3442 return false;
3445 // 64b Opcodes that can be combined with a MUL
3446 static bool isCombineInstrCandidate64(unsigned Opc) {
3447 switch (Opc) {
3448 case AArch64::ADDXrr:
3449 case AArch64::ADDXri:
3450 case AArch64::SUBXrr:
3451 case AArch64::ADDSXrr:
3452 case AArch64::ADDSXri:
3453 case AArch64::SUBSXrr:
3454 // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
3455 case AArch64::SUBXri:
3456 case AArch64::SUBSXri:
3457 return true;
3458 default:
3459 break;
3461 return false;
3464 // FP Opcodes that can be combined with a FMUL
3465 static bool isCombineInstrCandidateFP(const MachineInstr &Inst) {
3466 switch (Inst.getOpcode()) {
3467 default:
3468 break;
3469 case AArch64::FADDSrr:
3470 case AArch64::FADDDrr:
3471 case AArch64::FADDv2f32:
3472 case AArch64::FADDv2f64:
3473 case AArch64::FADDv4f32:
3474 case AArch64::FSUBSrr:
3475 case AArch64::FSUBDrr:
3476 case AArch64::FSUBv2f32:
3477 case AArch64::FSUBv2f64:
3478 case AArch64::FSUBv4f32:
3479 TargetOptions Options = Inst.getParent()->getParent()->getTarget().Options;
3480 return (Options.UnsafeFPMath ||
3481 Options.AllowFPOpFusion == FPOpFusion::Fast);
3483 return false;
3486 // Opcodes that can be combined with a MUL
3487 static bool isCombineInstrCandidate(unsigned Opc) {
3488 return (isCombineInstrCandidate32(Opc) || isCombineInstrCandidate64(Opc));
3492 // Utility routine that checks if \param MO is defined by an
3493 // \param CombineOpc instruction in the basic block \param MBB
3494 static bool canCombine(MachineBasicBlock &MBB, MachineOperand &MO,
3495 unsigned CombineOpc, unsigned ZeroReg = 0,
3496 bool CheckZeroReg = false) {
3497 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3498 MachineInstr *MI = nullptr;
3500 if (MO.isReg() && Register::isVirtualRegister(MO.getReg()))
3501 MI = MRI.getUniqueVRegDef(MO.getReg());
3502 // And it needs to be in the trace (otherwise, it won't have a depth).
3503 if (!MI || MI->getParent() != &MBB || (unsigned)MI->getOpcode() != CombineOpc)
3504 return false;
3505 // Must only used by the user we combine with.
3506 if (!MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
3507 return false;
3509 if (CheckZeroReg) {
3510 assert(MI->getNumOperands() >= 4 && MI->getOperand(0).isReg() &&
3511 MI->getOperand(1).isReg() && MI->getOperand(2).isReg() &&
3512 MI->getOperand(3).isReg() && "MAdd/MSub must have a least 4 regs");
3513 // The third input reg must be zero.
3514 if (MI->getOperand(3).getReg() != ZeroReg)
3515 return false;
3518 return true;
3522 // Is \param MO defined by an integer multiply and can be combined?
3523 static bool canCombineWithMUL(MachineBasicBlock &MBB, MachineOperand &MO,
3524 unsigned MulOpc, unsigned ZeroReg) {
3525 return canCombine(MBB, MO, MulOpc, ZeroReg, true);
3529 // Is \param MO defined by a floating-point multiply and can be combined?
3530 static bool canCombineWithFMUL(MachineBasicBlock &MBB, MachineOperand &MO,
3531 unsigned MulOpc) {
3532 return canCombine(MBB, MO, MulOpc);
3535 // TODO: There are many more machine instruction opcodes to match:
3536 // 1. Other data types (integer, vectors)
3537 // 2. Other math / logic operations (xor, or)
3538 // 3. Other forms of the same operation (intrinsics and other variants)
3539 bool AArch64InstrInfo::isAssociativeAndCommutative(
3540 const MachineInstr &Inst) const {
3541 switch (Inst.getOpcode()) {
3542 case AArch64::FADDDrr:
3543 case AArch64::FADDSrr:
3544 case AArch64::FADDv2f32:
3545 case AArch64::FADDv2f64:
3546 case AArch64::FADDv4f32:
3547 case AArch64::FMULDrr:
3548 case AArch64::FMULSrr:
3549 case AArch64::FMULX32:
3550 case AArch64::FMULX64:
3551 case AArch64::FMULXv2f32:
3552 case AArch64::FMULXv2f64:
3553 case AArch64::FMULXv4f32:
3554 case AArch64::FMULv2f32:
3555 case AArch64::FMULv2f64:
3556 case AArch64::FMULv4f32:
3557 return Inst.getParent()->getParent()->getTarget().Options.UnsafeFPMath;
3558 default:
3559 return false;
3563 /// Find instructions that can be turned into madd.
3564 static bool getMaddPatterns(MachineInstr &Root,
3565 SmallVectorImpl<MachineCombinerPattern> &Patterns) {
3566 unsigned Opc = Root.getOpcode();
3567 MachineBasicBlock &MBB = *Root.getParent();
3568 bool Found = false;
3570 if (!isCombineInstrCandidate(Opc))
3571 return false;
3572 if (isCombineInstrSettingFlag(Opc)) {
3573 int Cmp_NZCV = Root.findRegisterDefOperandIdx(AArch64::NZCV, true);
3574 // When NZCV is live bail out.
3575 if (Cmp_NZCV == -1)
3576 return false;
3577 unsigned NewOpc = convertToNonFlagSettingOpc(Root);
3578 // When opcode can't change bail out.
3579 // CHECKME: do we miss any cases for opcode conversion?
3580 if (NewOpc == Opc)
3581 return false;
3582 Opc = NewOpc;
3585 switch (Opc) {
3586 default:
3587 break;
3588 case AArch64::ADDWrr:
3589 assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
3590 "ADDWrr does not have register operands");
3591 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3592 AArch64::WZR)) {
3593 Patterns.push_back(MachineCombinerPattern::MULADDW_OP1);
3594 Found = true;
3596 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDWrrr,
3597 AArch64::WZR)) {
3598 Patterns.push_back(MachineCombinerPattern::MULADDW_OP2);
3599 Found = true;
3601 break;
3602 case AArch64::ADDXrr:
3603 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3604 AArch64::XZR)) {
3605 Patterns.push_back(MachineCombinerPattern::MULADDX_OP1);
3606 Found = true;
3608 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDXrrr,
3609 AArch64::XZR)) {
3610 Patterns.push_back(MachineCombinerPattern::MULADDX_OP2);
3611 Found = true;
3613 break;
3614 case AArch64::SUBWrr:
3615 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3616 AArch64::WZR)) {
3617 Patterns.push_back(MachineCombinerPattern::MULSUBW_OP1);
3618 Found = true;
3620 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDWrrr,
3621 AArch64::WZR)) {
3622 Patterns.push_back(MachineCombinerPattern::MULSUBW_OP2);
3623 Found = true;
3625 break;
3626 case AArch64::SUBXrr:
3627 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3628 AArch64::XZR)) {
3629 Patterns.push_back(MachineCombinerPattern::MULSUBX_OP1);
3630 Found = true;
3632 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDXrrr,
3633 AArch64::XZR)) {
3634 Patterns.push_back(MachineCombinerPattern::MULSUBX_OP2);
3635 Found = true;
3637 break;
3638 case AArch64::ADDWri:
3639 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3640 AArch64::WZR)) {
3641 Patterns.push_back(MachineCombinerPattern::MULADDWI_OP1);
3642 Found = true;
3644 break;
3645 case AArch64::ADDXri:
3646 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3647 AArch64::XZR)) {
3648 Patterns.push_back(MachineCombinerPattern::MULADDXI_OP1);
3649 Found = true;
3651 break;
3652 case AArch64::SUBWri:
3653 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3654 AArch64::WZR)) {
3655 Patterns.push_back(MachineCombinerPattern::MULSUBWI_OP1);
3656 Found = true;
3658 break;
3659 case AArch64::SUBXri:
3660 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3661 AArch64::XZR)) {
3662 Patterns.push_back(MachineCombinerPattern::MULSUBXI_OP1);
3663 Found = true;
3665 break;
3667 return Found;
3669 /// Floating-Point Support
3671 /// Find instructions that can be turned into madd.
3672 static bool getFMAPatterns(MachineInstr &Root,
3673 SmallVectorImpl<MachineCombinerPattern> &Patterns) {
3675 if (!isCombineInstrCandidateFP(Root))
3676 return false;
3678 MachineBasicBlock &MBB = *Root.getParent();
3679 bool Found = false;
3681 switch (Root.getOpcode()) {
3682 default:
3683 assert(false && "Unsupported FP instruction in combiner\n");
3684 break;
3685 case AArch64::FADDSrr:
3686 assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
3687 "FADDWrr does not have register operands");
3688 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULSrr)) {
3689 Patterns.push_back(MachineCombinerPattern::FMULADDS_OP1);
3690 Found = true;
3691 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3692 AArch64::FMULv1i32_indexed)) {
3693 Patterns.push_back(MachineCombinerPattern::FMLAv1i32_indexed_OP1);
3694 Found = true;
3696 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULSrr)) {
3697 Patterns.push_back(MachineCombinerPattern::FMULADDS_OP2);
3698 Found = true;
3699 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3700 AArch64::FMULv1i32_indexed)) {
3701 Patterns.push_back(MachineCombinerPattern::FMLAv1i32_indexed_OP2);
3702 Found = true;
3704 break;
3705 case AArch64::FADDDrr:
3706 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULDrr)) {
3707 Patterns.push_back(MachineCombinerPattern::FMULADDD_OP1);
3708 Found = true;
3709 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3710 AArch64::FMULv1i64_indexed)) {
3711 Patterns.push_back(MachineCombinerPattern::FMLAv1i64_indexed_OP1);
3712 Found = true;
3714 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULDrr)) {
3715 Patterns.push_back(MachineCombinerPattern::FMULADDD_OP2);
3716 Found = true;
3717 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3718 AArch64::FMULv1i64_indexed)) {
3719 Patterns.push_back(MachineCombinerPattern::FMLAv1i64_indexed_OP2);
3720 Found = true;
3722 break;
3723 case AArch64::FADDv2f32:
3724 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3725 AArch64::FMULv2i32_indexed)) {
3726 Patterns.push_back(MachineCombinerPattern::FMLAv2i32_indexed_OP1);
3727 Found = true;
3728 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3729 AArch64::FMULv2f32)) {
3730 Patterns.push_back(MachineCombinerPattern::FMLAv2f32_OP1);
3731 Found = true;
3733 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3734 AArch64::FMULv2i32_indexed)) {
3735 Patterns.push_back(MachineCombinerPattern::FMLAv2i32_indexed_OP2);
3736 Found = true;
3737 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3738 AArch64::FMULv2f32)) {
3739 Patterns.push_back(MachineCombinerPattern::FMLAv2f32_OP2);
3740 Found = true;
3742 break;
3743 case AArch64::FADDv2f64:
3744 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3745 AArch64::FMULv2i64_indexed)) {
3746 Patterns.push_back(MachineCombinerPattern::FMLAv2i64_indexed_OP1);
3747 Found = true;
3748 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3749 AArch64::FMULv2f64)) {
3750 Patterns.push_back(MachineCombinerPattern::FMLAv2f64_OP1);
3751 Found = true;
3753 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3754 AArch64::FMULv2i64_indexed)) {
3755 Patterns.push_back(MachineCombinerPattern::FMLAv2i64_indexed_OP2);
3756 Found = true;
3757 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3758 AArch64::FMULv2f64)) {
3759 Patterns.push_back(MachineCombinerPattern::FMLAv2f64_OP2);
3760 Found = true;
3762 break;
3763 case AArch64::FADDv4f32:
3764 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3765 AArch64::FMULv4i32_indexed)) {
3766 Patterns.push_back(MachineCombinerPattern::FMLAv4i32_indexed_OP1);
3767 Found = true;
3768 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3769 AArch64::FMULv4f32)) {
3770 Patterns.push_back(MachineCombinerPattern::FMLAv4f32_OP1);
3771 Found = true;
3773 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3774 AArch64::FMULv4i32_indexed)) {
3775 Patterns.push_back(MachineCombinerPattern::FMLAv4i32_indexed_OP2);
3776 Found = true;
3777 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3778 AArch64::FMULv4f32)) {
3779 Patterns.push_back(MachineCombinerPattern::FMLAv4f32_OP2);
3780 Found = true;
3782 break;
3784 case AArch64::FSUBSrr:
3785 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULSrr)) {
3786 Patterns.push_back(MachineCombinerPattern::FMULSUBS_OP1);
3787 Found = true;
3789 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULSrr)) {
3790 Patterns.push_back(MachineCombinerPattern::FMULSUBS_OP2);
3791 Found = true;
3792 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3793 AArch64::FMULv1i32_indexed)) {
3794 Patterns.push_back(MachineCombinerPattern::FMLSv1i32_indexed_OP2);
3795 Found = true;
3797 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FNMULSrr)) {
3798 Patterns.push_back(MachineCombinerPattern::FNMULSUBS_OP1);
3799 Found = true;
3801 break;
3802 case AArch64::FSUBDrr:
3803 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULDrr)) {
3804 Patterns.push_back(MachineCombinerPattern::FMULSUBD_OP1);
3805 Found = true;
3807 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULDrr)) {
3808 Patterns.push_back(MachineCombinerPattern::FMULSUBD_OP2);
3809 Found = true;
3810 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3811 AArch64::FMULv1i64_indexed)) {
3812 Patterns.push_back(MachineCombinerPattern::FMLSv1i64_indexed_OP2);
3813 Found = true;
3815 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FNMULDrr)) {
3816 Patterns.push_back(MachineCombinerPattern::FNMULSUBD_OP1);
3817 Found = true;
3819 break;
3820 case AArch64::FSUBv2f32:
3821 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3822 AArch64::FMULv2i32_indexed)) {
3823 Patterns.push_back(MachineCombinerPattern::FMLSv2i32_indexed_OP2);
3824 Found = true;
3825 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3826 AArch64::FMULv2f32)) {
3827 Patterns.push_back(MachineCombinerPattern::FMLSv2f32_OP2);
3828 Found = true;
3830 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3831 AArch64::FMULv2i32_indexed)) {
3832 Patterns.push_back(MachineCombinerPattern::FMLSv2i32_indexed_OP1);
3833 Found = true;
3834 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3835 AArch64::FMULv2f32)) {
3836 Patterns.push_back(MachineCombinerPattern::FMLSv2f32_OP1);
3837 Found = true;
3839 break;
3840 case AArch64::FSUBv2f64:
3841 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3842 AArch64::FMULv2i64_indexed)) {
3843 Patterns.push_back(MachineCombinerPattern::FMLSv2i64_indexed_OP2);
3844 Found = true;
3845 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3846 AArch64::FMULv2f64)) {
3847 Patterns.push_back(MachineCombinerPattern::FMLSv2f64_OP2);
3848 Found = true;
3850 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3851 AArch64::FMULv2i64_indexed)) {
3852 Patterns.push_back(MachineCombinerPattern::FMLSv2i64_indexed_OP1);
3853 Found = true;
3854 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3855 AArch64::FMULv2f64)) {
3856 Patterns.push_back(MachineCombinerPattern::FMLSv2f64_OP1);
3857 Found = true;
3859 break;
3860 case AArch64::FSUBv4f32:
3861 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3862 AArch64::FMULv4i32_indexed)) {
3863 Patterns.push_back(MachineCombinerPattern::FMLSv4i32_indexed_OP2);
3864 Found = true;
3865 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3866 AArch64::FMULv4f32)) {
3867 Patterns.push_back(MachineCombinerPattern::FMLSv4f32_OP2);
3868 Found = true;
3870 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3871 AArch64::FMULv4i32_indexed)) {
3872 Patterns.push_back(MachineCombinerPattern::FMLSv4i32_indexed_OP1);
3873 Found = true;
3874 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3875 AArch64::FMULv4f32)) {
3876 Patterns.push_back(MachineCombinerPattern::FMLSv4f32_OP1);
3877 Found = true;
3879 break;
3881 return Found;
3884 /// Return true when a code sequence can improve throughput. It
3885 /// should be called only for instructions in loops.
3886 /// \param Pattern - combiner pattern
3887 bool AArch64InstrInfo::isThroughputPattern(
3888 MachineCombinerPattern Pattern) const {
3889 switch (Pattern) {
3890 default:
3891 break;
3892 case MachineCombinerPattern::FMULADDS_OP1:
3893 case MachineCombinerPattern::FMULADDS_OP2:
3894 case MachineCombinerPattern::FMULSUBS_OP1:
3895 case MachineCombinerPattern::FMULSUBS_OP2:
3896 case MachineCombinerPattern::FMULADDD_OP1:
3897 case MachineCombinerPattern::FMULADDD_OP2:
3898 case MachineCombinerPattern::FMULSUBD_OP1:
3899 case MachineCombinerPattern::FMULSUBD_OP2:
3900 case MachineCombinerPattern::FNMULSUBS_OP1:
3901 case MachineCombinerPattern::FNMULSUBD_OP1:
3902 case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
3903 case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
3904 case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
3905 case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
3906 case MachineCombinerPattern::FMLAv2f32_OP2:
3907 case MachineCombinerPattern::FMLAv2f32_OP1:
3908 case MachineCombinerPattern::FMLAv2f64_OP1:
3909 case MachineCombinerPattern::FMLAv2f64_OP2:
3910 case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
3911 case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
3912 case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
3913 case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
3914 case MachineCombinerPattern::FMLAv4f32_OP1:
3915 case MachineCombinerPattern::FMLAv4f32_OP2:
3916 case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
3917 case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
3918 case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
3919 case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
3920 case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
3921 case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
3922 case MachineCombinerPattern::FMLSv2f32_OP2:
3923 case MachineCombinerPattern::FMLSv2f64_OP2:
3924 case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
3925 case MachineCombinerPattern::FMLSv4f32_OP2:
3926 return true;
3927 } // end switch (Pattern)
3928 return false;
3930 /// Return true when there is potentially a faster code sequence for an
3931 /// instruction chain ending in \p Root. All potential patterns are listed in
3932 /// the \p Pattern vector. Pattern should be sorted in priority order since the
3933 /// pattern evaluator stops checking as soon as it finds a faster sequence.
3935 bool AArch64InstrInfo::getMachineCombinerPatterns(
3936 MachineInstr &Root,
3937 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
3938 // Integer patterns
3939 if (getMaddPatterns(Root, Patterns))
3940 return true;
3941 // Floating point patterns
3942 if (getFMAPatterns(Root, Patterns))
3943 return true;
3945 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
3948 enum class FMAInstKind { Default, Indexed, Accumulator };
3949 /// genFusedMultiply - Generate fused multiply instructions.
3950 /// This function supports both integer and floating point instructions.
3951 /// A typical example:
3952 /// F|MUL I=A,B,0
3953 /// F|ADD R,I,C
3954 /// ==> F|MADD R,A,B,C
3955 /// \param MF Containing MachineFunction
3956 /// \param MRI Register information
3957 /// \param TII Target information
3958 /// \param Root is the F|ADD instruction
3959 /// \param [out] InsInstrs is a vector of machine instructions and will
3960 /// contain the generated madd instruction
3961 /// \param IdxMulOpd is index of operand in Root that is the result of
3962 /// the F|MUL. In the example above IdxMulOpd is 1.
3963 /// \param MaddOpc the opcode fo the f|madd instruction
3964 /// \param RC Register class of operands
3965 /// \param kind of fma instruction (addressing mode) to be generated
3966 /// \param ReplacedAddend is the result register from the instruction
3967 /// replacing the non-combined operand, if any.
3968 static MachineInstr *
3969 genFusedMultiply(MachineFunction &MF, MachineRegisterInfo &MRI,
3970 const TargetInstrInfo *TII, MachineInstr &Root,
3971 SmallVectorImpl<MachineInstr *> &InsInstrs, unsigned IdxMulOpd,
3972 unsigned MaddOpc, const TargetRegisterClass *RC,
3973 FMAInstKind kind = FMAInstKind::Default,
3974 const Register *ReplacedAddend = nullptr) {
3975 assert(IdxMulOpd == 1 || IdxMulOpd == 2);
3977 unsigned IdxOtherOpd = IdxMulOpd == 1 ? 2 : 1;
3978 MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
3979 Register ResultReg = Root.getOperand(0).getReg();
3980 Register SrcReg0 = MUL->getOperand(1).getReg();
3981 bool Src0IsKill = MUL->getOperand(1).isKill();
3982 Register SrcReg1 = MUL->getOperand(2).getReg();
3983 bool Src1IsKill = MUL->getOperand(2).isKill();
3985 unsigned SrcReg2;
3986 bool Src2IsKill;
3987 if (ReplacedAddend) {
3988 // If we just generated a new addend, we must be it's only use.
3989 SrcReg2 = *ReplacedAddend;
3990 Src2IsKill = true;
3991 } else {
3992 SrcReg2 = Root.getOperand(IdxOtherOpd).getReg();
3993 Src2IsKill = Root.getOperand(IdxOtherOpd).isKill();
3996 if (Register::isVirtualRegister(ResultReg))
3997 MRI.constrainRegClass(ResultReg, RC);
3998 if (Register::isVirtualRegister(SrcReg0))
3999 MRI.constrainRegClass(SrcReg0, RC);
4000 if (Register::isVirtualRegister(SrcReg1))
4001 MRI.constrainRegClass(SrcReg1, RC);
4002 if (Register::isVirtualRegister(SrcReg2))
4003 MRI.constrainRegClass(SrcReg2, RC);
4005 MachineInstrBuilder MIB;
4006 if (kind == FMAInstKind::Default)
4007 MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4008 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4009 .addReg(SrcReg1, getKillRegState(Src1IsKill))
4010 .addReg(SrcReg2, getKillRegState(Src2IsKill));
4011 else if (kind == FMAInstKind::Indexed)
4012 MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4013 .addReg(SrcReg2, getKillRegState(Src2IsKill))
4014 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4015 .addReg(SrcReg1, getKillRegState(Src1IsKill))
4016 .addImm(MUL->getOperand(3).getImm());
4017 else if (kind == FMAInstKind::Accumulator)
4018 MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4019 .addReg(SrcReg2, getKillRegState(Src2IsKill))
4020 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4021 .addReg(SrcReg1, getKillRegState(Src1IsKill));
4022 else
4023 assert(false && "Invalid FMA instruction kind \n");
4024 // Insert the MADD (MADD, FMA, FMS, FMLA, FMSL)
4025 InsInstrs.push_back(MIB);
4026 return MUL;
4029 /// genMaddR - Generate madd instruction and combine mul and add using
4030 /// an extra virtual register
4031 /// Example - an ADD intermediate needs to be stored in a register:
4032 /// MUL I=A,B,0
4033 /// ADD R,I,Imm
4034 /// ==> ORR V, ZR, Imm
4035 /// ==> MADD R,A,B,V
4036 /// \param MF Containing MachineFunction
4037 /// \param MRI Register information
4038 /// \param TII Target information
4039 /// \param Root is the ADD instruction
4040 /// \param [out] InsInstrs is a vector of machine instructions and will
4041 /// contain the generated madd instruction
4042 /// \param IdxMulOpd is index of operand in Root that is the result of
4043 /// the MUL. In the example above IdxMulOpd is 1.
4044 /// \param MaddOpc the opcode fo the madd instruction
4045 /// \param VR is a virtual register that holds the value of an ADD operand
4046 /// (V in the example above).
4047 /// \param RC Register class of operands
4048 static MachineInstr *genMaddR(MachineFunction &MF, MachineRegisterInfo &MRI,
4049 const TargetInstrInfo *TII, MachineInstr &Root,
4050 SmallVectorImpl<MachineInstr *> &InsInstrs,
4051 unsigned IdxMulOpd, unsigned MaddOpc, unsigned VR,
4052 const TargetRegisterClass *RC) {
4053 assert(IdxMulOpd == 1 || IdxMulOpd == 2);
4055 MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
4056 Register ResultReg = Root.getOperand(0).getReg();
4057 Register SrcReg0 = MUL->getOperand(1).getReg();
4058 bool Src0IsKill = MUL->getOperand(1).isKill();
4059 Register SrcReg1 = MUL->getOperand(2).getReg();
4060 bool Src1IsKill = MUL->getOperand(2).isKill();
4062 if (Register::isVirtualRegister(ResultReg))
4063 MRI.constrainRegClass(ResultReg, RC);
4064 if (Register::isVirtualRegister(SrcReg0))
4065 MRI.constrainRegClass(SrcReg0, RC);
4066 if (Register::isVirtualRegister(SrcReg1))
4067 MRI.constrainRegClass(SrcReg1, RC);
4068 if (Register::isVirtualRegister(VR))
4069 MRI.constrainRegClass(VR, RC);
4071 MachineInstrBuilder MIB =
4072 BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4073 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4074 .addReg(SrcReg1, getKillRegState(Src1IsKill))
4075 .addReg(VR);
4076 // Insert the MADD
4077 InsInstrs.push_back(MIB);
4078 return MUL;
4081 /// When getMachineCombinerPatterns() finds potential patterns,
4082 /// this function generates the instructions that could replace the
4083 /// original code sequence
4084 void AArch64InstrInfo::genAlternativeCodeSequence(
4085 MachineInstr &Root, MachineCombinerPattern Pattern,
4086 SmallVectorImpl<MachineInstr *> &InsInstrs,
4087 SmallVectorImpl<MachineInstr *> &DelInstrs,
4088 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
4089 MachineBasicBlock &MBB = *Root.getParent();
4090 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4091 MachineFunction &MF = *MBB.getParent();
4092 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
4094 MachineInstr *MUL;
4095 const TargetRegisterClass *RC;
4096 unsigned Opc;
4097 switch (Pattern) {
4098 default:
4099 // Reassociate instructions.
4100 TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs,
4101 DelInstrs, InstrIdxForVirtReg);
4102 return;
4103 case MachineCombinerPattern::MULADDW_OP1:
4104 case MachineCombinerPattern::MULADDX_OP1:
4105 // MUL I=A,B,0
4106 // ADD R,I,C
4107 // ==> MADD R,A,B,C
4108 // --- Create(MADD);
4109 if (Pattern == MachineCombinerPattern::MULADDW_OP1) {
4110 Opc = AArch64::MADDWrrr;
4111 RC = &AArch64::GPR32RegClass;
4112 } else {
4113 Opc = AArch64::MADDXrrr;
4114 RC = &AArch64::GPR64RegClass;
4116 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4117 break;
4118 case MachineCombinerPattern::MULADDW_OP2:
4119 case MachineCombinerPattern::MULADDX_OP2:
4120 // MUL I=A,B,0
4121 // ADD R,C,I
4122 // ==> MADD R,A,B,C
4123 // --- Create(MADD);
4124 if (Pattern == MachineCombinerPattern::MULADDW_OP2) {
4125 Opc = AArch64::MADDWrrr;
4126 RC = &AArch64::GPR32RegClass;
4127 } else {
4128 Opc = AArch64::MADDXrrr;
4129 RC = &AArch64::GPR64RegClass;
4131 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4132 break;
4133 case MachineCombinerPattern::MULADDWI_OP1:
4134 case MachineCombinerPattern::MULADDXI_OP1: {
4135 // MUL I=A,B,0
4136 // ADD R,I,Imm
4137 // ==> ORR V, ZR, Imm
4138 // ==> MADD R,A,B,V
4139 // --- Create(MADD);
4140 const TargetRegisterClass *OrrRC;
4141 unsigned BitSize, OrrOpc, ZeroReg;
4142 if (Pattern == MachineCombinerPattern::MULADDWI_OP1) {
4143 OrrOpc = AArch64::ORRWri;
4144 OrrRC = &AArch64::GPR32spRegClass;
4145 BitSize = 32;
4146 ZeroReg = AArch64::WZR;
4147 Opc = AArch64::MADDWrrr;
4148 RC = &AArch64::GPR32RegClass;
4149 } else {
4150 OrrOpc = AArch64::ORRXri;
4151 OrrRC = &AArch64::GPR64spRegClass;
4152 BitSize = 64;
4153 ZeroReg = AArch64::XZR;
4154 Opc = AArch64::MADDXrrr;
4155 RC = &AArch64::GPR64RegClass;
4157 Register NewVR = MRI.createVirtualRegister(OrrRC);
4158 uint64_t Imm = Root.getOperand(2).getImm();
4160 if (Root.getOperand(3).isImm()) {
4161 unsigned Val = Root.getOperand(3).getImm();
4162 Imm = Imm << Val;
4164 uint64_t UImm = SignExtend64(Imm, BitSize);
4165 uint64_t Encoding;
4166 if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
4167 MachineInstrBuilder MIB1 =
4168 BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
4169 .addReg(ZeroReg)
4170 .addImm(Encoding);
4171 InsInstrs.push_back(MIB1);
4172 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4173 MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4175 break;
4177 case MachineCombinerPattern::MULSUBW_OP1:
4178 case MachineCombinerPattern::MULSUBX_OP1: {
4179 // MUL I=A,B,0
4180 // SUB R,I, C
4181 // ==> SUB V, 0, C
4182 // ==> MADD R,A,B,V // = -C + A*B
4183 // --- Create(MADD);
4184 const TargetRegisterClass *SubRC;
4185 unsigned SubOpc, ZeroReg;
4186 if (Pattern == MachineCombinerPattern::MULSUBW_OP1) {
4187 SubOpc = AArch64::SUBWrr;
4188 SubRC = &AArch64::GPR32spRegClass;
4189 ZeroReg = AArch64::WZR;
4190 Opc = AArch64::MADDWrrr;
4191 RC = &AArch64::GPR32RegClass;
4192 } else {
4193 SubOpc = AArch64::SUBXrr;
4194 SubRC = &AArch64::GPR64spRegClass;
4195 ZeroReg = AArch64::XZR;
4196 Opc = AArch64::MADDXrrr;
4197 RC = &AArch64::GPR64RegClass;
4199 Register NewVR = MRI.createVirtualRegister(SubRC);
4200 // SUB NewVR, 0, C
4201 MachineInstrBuilder MIB1 =
4202 BuildMI(MF, Root.getDebugLoc(), TII->get(SubOpc), NewVR)
4203 .addReg(ZeroReg)
4204 .add(Root.getOperand(2));
4205 InsInstrs.push_back(MIB1);
4206 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4207 MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4208 break;
4210 case MachineCombinerPattern::MULSUBW_OP2:
4211 case MachineCombinerPattern::MULSUBX_OP2:
4212 // MUL I=A,B,0
4213 // SUB R,C,I
4214 // ==> MSUB R,A,B,C (computes C - A*B)
4215 // --- Create(MSUB);
4216 if (Pattern == MachineCombinerPattern::MULSUBW_OP2) {
4217 Opc = AArch64::MSUBWrrr;
4218 RC = &AArch64::GPR32RegClass;
4219 } else {
4220 Opc = AArch64::MSUBXrrr;
4221 RC = &AArch64::GPR64RegClass;
4223 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4224 break;
4225 case MachineCombinerPattern::MULSUBWI_OP1:
4226 case MachineCombinerPattern::MULSUBXI_OP1: {
4227 // MUL I=A,B,0
4228 // SUB R,I, Imm
4229 // ==> ORR V, ZR, -Imm
4230 // ==> MADD R,A,B,V // = -Imm + A*B
4231 // --- Create(MADD);
4232 const TargetRegisterClass *OrrRC;
4233 unsigned BitSize, OrrOpc, ZeroReg;
4234 if (Pattern == MachineCombinerPattern::MULSUBWI_OP1) {
4235 OrrOpc = AArch64::ORRWri;
4236 OrrRC = &AArch64::GPR32spRegClass;
4237 BitSize = 32;
4238 ZeroReg = AArch64::WZR;
4239 Opc = AArch64::MADDWrrr;
4240 RC = &AArch64::GPR32RegClass;
4241 } else {
4242 OrrOpc = AArch64::ORRXri;
4243 OrrRC = &AArch64::GPR64spRegClass;
4244 BitSize = 64;
4245 ZeroReg = AArch64::XZR;
4246 Opc = AArch64::MADDXrrr;
4247 RC = &AArch64::GPR64RegClass;
4249 Register NewVR = MRI.createVirtualRegister(OrrRC);
4250 uint64_t Imm = Root.getOperand(2).getImm();
4251 if (Root.getOperand(3).isImm()) {
4252 unsigned Val = Root.getOperand(3).getImm();
4253 Imm = Imm << Val;
4255 uint64_t UImm = SignExtend64(-Imm, BitSize);
4256 uint64_t Encoding;
4257 if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
4258 MachineInstrBuilder MIB1 =
4259 BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
4260 .addReg(ZeroReg)
4261 .addImm(Encoding);
4262 InsInstrs.push_back(MIB1);
4263 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4264 MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4266 break;
4268 // Floating Point Support
4269 case MachineCombinerPattern::FMULADDS_OP1:
4270 case MachineCombinerPattern::FMULADDD_OP1:
4271 // MUL I=A,B,0
4272 // ADD R,I,C
4273 // ==> MADD R,A,B,C
4274 // --- Create(MADD);
4275 if (Pattern == MachineCombinerPattern::FMULADDS_OP1) {
4276 Opc = AArch64::FMADDSrrr;
4277 RC = &AArch64::FPR32RegClass;
4278 } else {
4279 Opc = AArch64::FMADDDrrr;
4280 RC = &AArch64::FPR64RegClass;
4282 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4283 break;
4284 case MachineCombinerPattern::FMULADDS_OP2:
4285 case MachineCombinerPattern::FMULADDD_OP2:
4286 // FMUL I=A,B,0
4287 // FADD R,C,I
4288 // ==> FMADD R,A,B,C
4289 // --- Create(FMADD);
4290 if (Pattern == MachineCombinerPattern::FMULADDS_OP2) {
4291 Opc = AArch64::FMADDSrrr;
4292 RC = &AArch64::FPR32RegClass;
4293 } else {
4294 Opc = AArch64::FMADDDrrr;
4295 RC = &AArch64::FPR64RegClass;
4297 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4298 break;
4300 case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
4301 Opc = AArch64::FMLAv1i32_indexed;
4302 RC = &AArch64::FPR32RegClass;
4303 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4304 FMAInstKind::Indexed);
4305 break;
4306 case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
4307 Opc = AArch64::FMLAv1i32_indexed;
4308 RC = &AArch64::FPR32RegClass;
4309 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4310 FMAInstKind::Indexed);
4311 break;
4313 case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
4314 Opc = AArch64::FMLAv1i64_indexed;
4315 RC = &AArch64::FPR64RegClass;
4316 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4317 FMAInstKind::Indexed);
4318 break;
4319 case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
4320 Opc = AArch64::FMLAv1i64_indexed;
4321 RC = &AArch64::FPR64RegClass;
4322 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4323 FMAInstKind::Indexed);
4324 break;
4326 case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
4327 case MachineCombinerPattern::FMLAv2f32_OP1:
4328 RC = &AArch64::FPR64RegClass;
4329 if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP1) {
4330 Opc = AArch64::FMLAv2i32_indexed;
4331 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4332 FMAInstKind::Indexed);
4333 } else {
4334 Opc = AArch64::FMLAv2f32;
4335 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4336 FMAInstKind::Accumulator);
4338 break;
4339 case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
4340 case MachineCombinerPattern::FMLAv2f32_OP2:
4341 RC = &AArch64::FPR64RegClass;
4342 if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP2) {
4343 Opc = AArch64::FMLAv2i32_indexed;
4344 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4345 FMAInstKind::Indexed);
4346 } else {
4347 Opc = AArch64::FMLAv2f32;
4348 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4349 FMAInstKind::Accumulator);
4351 break;
4353 case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
4354 case MachineCombinerPattern::FMLAv2f64_OP1:
4355 RC = &AArch64::FPR128RegClass;
4356 if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP1) {
4357 Opc = AArch64::FMLAv2i64_indexed;
4358 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4359 FMAInstKind::Indexed);
4360 } else {
4361 Opc = AArch64::FMLAv2f64;
4362 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4363 FMAInstKind::Accumulator);
4365 break;
4366 case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
4367 case MachineCombinerPattern::FMLAv2f64_OP2:
4368 RC = &AArch64::FPR128RegClass;
4369 if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP2) {
4370 Opc = AArch64::FMLAv2i64_indexed;
4371 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4372 FMAInstKind::Indexed);
4373 } else {
4374 Opc = AArch64::FMLAv2f64;
4375 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4376 FMAInstKind::Accumulator);
4378 break;
4380 case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
4381 case MachineCombinerPattern::FMLAv4f32_OP1:
4382 RC = &AArch64::FPR128RegClass;
4383 if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP1) {
4384 Opc = AArch64::FMLAv4i32_indexed;
4385 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4386 FMAInstKind::Indexed);
4387 } else {
4388 Opc = AArch64::FMLAv4f32;
4389 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4390 FMAInstKind::Accumulator);
4392 break;
4394 case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
4395 case MachineCombinerPattern::FMLAv4f32_OP2:
4396 RC = &AArch64::FPR128RegClass;
4397 if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP2) {
4398 Opc = AArch64::FMLAv4i32_indexed;
4399 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4400 FMAInstKind::Indexed);
4401 } else {
4402 Opc = AArch64::FMLAv4f32;
4403 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4404 FMAInstKind::Accumulator);
4406 break;
4408 case MachineCombinerPattern::FMULSUBS_OP1:
4409 case MachineCombinerPattern::FMULSUBD_OP1: {
4410 // FMUL I=A,B,0
4411 // FSUB R,I,C
4412 // ==> FNMSUB R,A,B,C // = -C + A*B
4413 // --- Create(FNMSUB);
4414 if (Pattern == MachineCombinerPattern::FMULSUBS_OP1) {
4415 Opc = AArch64::FNMSUBSrrr;
4416 RC = &AArch64::FPR32RegClass;
4417 } else {
4418 Opc = AArch64::FNMSUBDrrr;
4419 RC = &AArch64::FPR64RegClass;
4421 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4422 break;
4425 case MachineCombinerPattern::FNMULSUBS_OP1:
4426 case MachineCombinerPattern::FNMULSUBD_OP1: {
4427 // FNMUL I=A,B,0
4428 // FSUB R,I,C
4429 // ==> FNMADD R,A,B,C // = -A*B - C
4430 // --- Create(FNMADD);
4431 if (Pattern == MachineCombinerPattern::FNMULSUBS_OP1) {
4432 Opc = AArch64::FNMADDSrrr;
4433 RC = &AArch64::FPR32RegClass;
4434 } else {
4435 Opc = AArch64::FNMADDDrrr;
4436 RC = &AArch64::FPR64RegClass;
4438 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4439 break;
4442 case MachineCombinerPattern::FMULSUBS_OP2:
4443 case MachineCombinerPattern::FMULSUBD_OP2: {
4444 // FMUL I=A,B,0
4445 // FSUB R,C,I
4446 // ==> FMSUB R,A,B,C (computes C - A*B)
4447 // --- Create(FMSUB);
4448 if (Pattern == MachineCombinerPattern::FMULSUBS_OP2) {
4449 Opc = AArch64::FMSUBSrrr;
4450 RC = &AArch64::FPR32RegClass;
4451 } else {
4452 Opc = AArch64::FMSUBDrrr;
4453 RC = &AArch64::FPR64RegClass;
4455 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4456 break;
4459 case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
4460 Opc = AArch64::FMLSv1i32_indexed;
4461 RC = &AArch64::FPR32RegClass;
4462 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4463 FMAInstKind::Indexed);
4464 break;
4466 case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
4467 Opc = AArch64::FMLSv1i64_indexed;
4468 RC = &AArch64::FPR64RegClass;
4469 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4470 FMAInstKind::Indexed);
4471 break;
4473 case MachineCombinerPattern::FMLSv2f32_OP2:
4474 case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
4475 RC = &AArch64::FPR64RegClass;
4476 if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP2) {
4477 Opc = AArch64::FMLSv2i32_indexed;
4478 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4479 FMAInstKind::Indexed);
4480 } else {
4481 Opc = AArch64::FMLSv2f32;
4482 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4483 FMAInstKind::Accumulator);
4485 break;
4487 case MachineCombinerPattern::FMLSv2f64_OP2:
4488 case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
4489 RC = &AArch64::FPR128RegClass;
4490 if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP2) {
4491 Opc = AArch64::FMLSv2i64_indexed;
4492 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4493 FMAInstKind::Indexed);
4494 } else {
4495 Opc = AArch64::FMLSv2f64;
4496 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4497 FMAInstKind::Accumulator);
4499 break;
4501 case MachineCombinerPattern::FMLSv4f32_OP2:
4502 case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
4503 RC = &AArch64::FPR128RegClass;
4504 if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP2) {
4505 Opc = AArch64::FMLSv4i32_indexed;
4506 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4507 FMAInstKind::Indexed);
4508 } else {
4509 Opc = AArch64::FMLSv4f32;
4510 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4511 FMAInstKind::Accumulator);
4513 break;
4514 case MachineCombinerPattern::FMLSv2f32_OP1:
4515 case MachineCombinerPattern::FMLSv2i32_indexed_OP1: {
4516 RC = &AArch64::FPR64RegClass;
4517 Register NewVR = MRI.createVirtualRegister(RC);
4518 MachineInstrBuilder MIB1 =
4519 BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f32), NewVR)
4520 .add(Root.getOperand(2));
4521 InsInstrs.push_back(MIB1);
4522 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4523 if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP1) {
4524 Opc = AArch64::FMLAv2i32_indexed;
4525 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4526 FMAInstKind::Indexed, &NewVR);
4527 } else {
4528 Opc = AArch64::FMLAv2f32;
4529 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4530 FMAInstKind::Accumulator, &NewVR);
4532 break;
4534 case MachineCombinerPattern::FMLSv4f32_OP1:
4535 case MachineCombinerPattern::FMLSv4i32_indexed_OP1: {
4536 RC = &AArch64::FPR128RegClass;
4537 Register NewVR = MRI.createVirtualRegister(RC);
4538 MachineInstrBuilder MIB1 =
4539 BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv4f32), NewVR)
4540 .add(Root.getOperand(2));
4541 InsInstrs.push_back(MIB1);
4542 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4543 if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP1) {
4544 Opc = AArch64::FMLAv4i32_indexed;
4545 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4546 FMAInstKind::Indexed, &NewVR);
4547 } else {
4548 Opc = AArch64::FMLAv4f32;
4549 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4550 FMAInstKind::Accumulator, &NewVR);
4552 break;
4554 case MachineCombinerPattern::FMLSv2f64_OP1:
4555 case MachineCombinerPattern::FMLSv2i64_indexed_OP1: {
4556 RC = &AArch64::FPR128RegClass;
4557 Register NewVR = MRI.createVirtualRegister(RC);
4558 MachineInstrBuilder MIB1 =
4559 BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f64), NewVR)
4560 .add(Root.getOperand(2));
4561 InsInstrs.push_back(MIB1);
4562 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4563 if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP1) {
4564 Opc = AArch64::FMLAv2i64_indexed;
4565 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4566 FMAInstKind::Indexed, &NewVR);
4567 } else {
4568 Opc = AArch64::FMLAv2f64;
4569 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4570 FMAInstKind::Accumulator, &NewVR);
4572 break;
4574 } // end switch (Pattern)
4575 // Record MUL and ADD/SUB for deletion
4576 DelInstrs.push_back(MUL);
4577 DelInstrs.push_back(&Root);
4580 /// Replace csincr-branch sequence by simple conditional branch
4582 /// Examples:
4583 /// 1. \code
4584 /// csinc w9, wzr, wzr, <condition code>
4585 /// tbnz w9, #0, 0x44
4586 /// \endcode
4587 /// to
4588 /// \code
4589 /// b.<inverted condition code>
4590 /// \endcode
4592 /// 2. \code
4593 /// csinc w9, wzr, wzr, <condition code>
4594 /// tbz w9, #0, 0x44
4595 /// \endcode
4596 /// to
4597 /// \code
4598 /// b.<condition code>
4599 /// \endcode
4601 /// Replace compare and branch sequence by TBZ/TBNZ instruction when the
4602 /// compare's constant operand is power of 2.
4604 /// Examples:
4605 /// \code
4606 /// and w8, w8, #0x400
4607 /// cbnz w8, L1
4608 /// \endcode
4609 /// to
4610 /// \code
4611 /// tbnz w8, #10, L1
4612 /// \endcode
4614 /// \param MI Conditional Branch
4615 /// \return True when the simple conditional branch is generated
4617 bool AArch64InstrInfo::optimizeCondBranch(MachineInstr &MI) const {
4618 bool IsNegativeBranch = false;
4619 bool IsTestAndBranch = false;
4620 unsigned TargetBBInMI = 0;
4621 switch (MI.getOpcode()) {
4622 default:
4623 llvm_unreachable("Unknown branch instruction?");
4624 case AArch64::Bcc:
4625 return false;
4626 case AArch64::CBZW:
4627 case AArch64::CBZX:
4628 TargetBBInMI = 1;
4629 break;
4630 case AArch64::CBNZW:
4631 case AArch64::CBNZX:
4632 TargetBBInMI = 1;
4633 IsNegativeBranch = true;
4634 break;
4635 case AArch64::TBZW:
4636 case AArch64::TBZX:
4637 TargetBBInMI = 2;
4638 IsTestAndBranch = true;
4639 break;
4640 case AArch64::TBNZW:
4641 case AArch64::TBNZX:
4642 TargetBBInMI = 2;
4643 IsNegativeBranch = true;
4644 IsTestAndBranch = true;
4645 break;
4647 // So we increment a zero register and test for bits other
4648 // than bit 0? Conservatively bail out in case the verifier
4649 // missed this case.
4650 if (IsTestAndBranch && MI.getOperand(1).getImm())
4651 return false;
4653 // Find Definition.
4654 assert(MI.getParent() && "Incomplete machine instruciton\n");
4655 MachineBasicBlock *MBB = MI.getParent();
4656 MachineFunction *MF = MBB->getParent();
4657 MachineRegisterInfo *MRI = &MF->getRegInfo();
4658 Register VReg = MI.getOperand(0).getReg();
4659 if (!Register::isVirtualRegister(VReg))
4660 return false;
4662 MachineInstr *DefMI = MRI->getVRegDef(VReg);
4664 // Look through COPY instructions to find definition.
4665 while (DefMI->isCopy()) {
4666 Register CopyVReg = DefMI->getOperand(1).getReg();
4667 if (!MRI->hasOneNonDBGUse(CopyVReg))
4668 return false;
4669 if (!MRI->hasOneDef(CopyVReg))
4670 return false;
4671 DefMI = MRI->getVRegDef(CopyVReg);
4674 switch (DefMI->getOpcode()) {
4675 default:
4676 return false;
4677 // Fold AND into a TBZ/TBNZ if constant operand is power of 2.
4678 case AArch64::ANDWri:
4679 case AArch64::ANDXri: {
4680 if (IsTestAndBranch)
4681 return false;
4682 if (DefMI->getParent() != MBB)
4683 return false;
4684 if (!MRI->hasOneNonDBGUse(VReg))
4685 return false;
4687 bool Is32Bit = (DefMI->getOpcode() == AArch64::ANDWri);
4688 uint64_t Mask = AArch64_AM::decodeLogicalImmediate(
4689 DefMI->getOperand(2).getImm(), Is32Bit ? 32 : 64);
4690 if (!isPowerOf2_64(Mask))
4691 return false;
4693 MachineOperand &MO = DefMI->getOperand(1);
4694 Register NewReg = MO.getReg();
4695 if (!Register::isVirtualRegister(NewReg))
4696 return false;
4698 assert(!MRI->def_empty(NewReg) && "Register must be defined.");
4700 MachineBasicBlock &RefToMBB = *MBB;
4701 MachineBasicBlock *TBB = MI.getOperand(1).getMBB();
4702 DebugLoc DL = MI.getDebugLoc();
4703 unsigned Imm = Log2_64(Mask);
4704 unsigned Opc = (Imm < 32)
4705 ? (IsNegativeBranch ? AArch64::TBNZW : AArch64::TBZW)
4706 : (IsNegativeBranch ? AArch64::TBNZX : AArch64::TBZX);
4707 MachineInstr *NewMI = BuildMI(RefToMBB, MI, DL, get(Opc))
4708 .addReg(NewReg)
4709 .addImm(Imm)
4710 .addMBB(TBB);
4711 // Register lives on to the CBZ now.
4712 MO.setIsKill(false);
4714 // For immediate smaller than 32, we need to use the 32-bit
4715 // variant (W) in all cases. Indeed the 64-bit variant does not
4716 // allow to encode them.
4717 // Therefore, if the input register is 64-bit, we need to take the
4718 // 32-bit sub-part.
4719 if (!Is32Bit && Imm < 32)
4720 NewMI->getOperand(0).setSubReg(AArch64::sub_32);
4721 MI.eraseFromParent();
4722 return true;
4724 // Look for CSINC
4725 case AArch64::CSINCWr:
4726 case AArch64::CSINCXr: {
4727 if (!(DefMI->getOperand(1).getReg() == AArch64::WZR &&
4728 DefMI->getOperand(2).getReg() == AArch64::WZR) &&
4729 !(DefMI->getOperand(1).getReg() == AArch64::XZR &&
4730 DefMI->getOperand(2).getReg() == AArch64::XZR))
4731 return false;
4733 if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) != -1)
4734 return false;
4736 AArch64CC::CondCode CC = (AArch64CC::CondCode)DefMI->getOperand(3).getImm();
4737 // Convert only when the condition code is not modified between
4738 // the CSINC and the branch. The CC may be used by other
4739 // instructions in between.
4740 if (areCFlagsAccessedBetweenInstrs(DefMI, MI, &getRegisterInfo(), AK_Write))
4741 return false;
4742 MachineBasicBlock &RefToMBB = *MBB;
4743 MachineBasicBlock *TBB = MI.getOperand(TargetBBInMI).getMBB();
4744 DebugLoc DL = MI.getDebugLoc();
4745 if (IsNegativeBranch)
4746 CC = AArch64CC::getInvertedCondCode(CC);
4747 BuildMI(RefToMBB, MI, DL, get(AArch64::Bcc)).addImm(CC).addMBB(TBB);
4748 MI.eraseFromParent();
4749 return true;
4754 std::pair<unsigned, unsigned>
4755 AArch64InstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
4756 const unsigned Mask = AArch64II::MO_FRAGMENT;
4757 return std::make_pair(TF & Mask, TF & ~Mask);
4760 ArrayRef<std::pair<unsigned, const char *>>
4761 AArch64InstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
4762 using namespace AArch64II;
4764 static const std::pair<unsigned, const char *> TargetFlags[] = {
4765 {MO_PAGE, "aarch64-page"}, {MO_PAGEOFF, "aarch64-pageoff"},
4766 {MO_G3, "aarch64-g3"}, {MO_G2, "aarch64-g2"},
4767 {MO_G1, "aarch64-g1"}, {MO_G0, "aarch64-g0"},
4768 {MO_HI12, "aarch64-hi12"}};
4769 return makeArrayRef(TargetFlags);
4772 ArrayRef<std::pair<unsigned, const char *>>
4773 AArch64InstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
4774 using namespace AArch64II;
4776 static const std::pair<unsigned, const char *> TargetFlags[] = {
4777 {MO_COFFSTUB, "aarch64-coffstub"},
4778 {MO_GOT, "aarch64-got"},
4779 {MO_NC, "aarch64-nc"},
4780 {MO_S, "aarch64-s"},
4781 {MO_TLS, "aarch64-tls"},
4782 {MO_DLLIMPORT, "aarch64-dllimport"},
4783 {MO_PREL, "aarch64-prel"},
4784 {MO_TAGGED, "aarch64-tagged"}};
4785 return makeArrayRef(TargetFlags);
4788 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
4789 AArch64InstrInfo::getSerializableMachineMemOperandTargetFlags() const {
4790 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
4791 {{MOSuppressPair, "aarch64-suppress-pair"},
4792 {MOStridedAccess, "aarch64-strided-access"}};
4793 return makeArrayRef(TargetFlags);
4796 /// Constants defining how certain sequences should be outlined.
4797 /// This encompasses how an outlined function should be called, and what kind of
4798 /// frame should be emitted for that outlined function.
4800 /// \p MachineOutlinerDefault implies that the function should be called with
4801 /// a save and restore of LR to the stack.
4803 /// That is,
4805 /// I1 Save LR OUTLINED_FUNCTION:
4806 /// I2 --> BL OUTLINED_FUNCTION I1
4807 /// I3 Restore LR I2
4808 /// I3
4809 /// RET
4811 /// * Call construction overhead: 3 (save + BL + restore)
4812 /// * Frame construction overhead: 1 (ret)
4813 /// * Requires stack fixups? Yes
4815 /// \p MachineOutlinerTailCall implies that the function is being created from
4816 /// a sequence of instructions ending in a return.
4818 /// That is,
4820 /// I1 OUTLINED_FUNCTION:
4821 /// I2 --> B OUTLINED_FUNCTION I1
4822 /// RET I2
4823 /// RET
4825 /// * Call construction overhead: 1 (B)
4826 /// * Frame construction overhead: 0 (Return included in sequence)
4827 /// * Requires stack fixups? No
4829 /// \p MachineOutlinerNoLRSave implies that the function should be called using
4830 /// a BL instruction, but doesn't require LR to be saved and restored. This
4831 /// happens when LR is known to be dead.
4833 /// That is,
4835 /// I1 OUTLINED_FUNCTION:
4836 /// I2 --> BL OUTLINED_FUNCTION I1
4837 /// I3 I2
4838 /// I3
4839 /// RET
4841 /// * Call construction overhead: 1 (BL)
4842 /// * Frame construction overhead: 1 (RET)
4843 /// * Requires stack fixups? No
4845 /// \p MachineOutlinerThunk implies that the function is being created from
4846 /// a sequence of instructions ending in a call. The outlined function is
4847 /// called with a BL instruction, and the outlined function tail-calls the
4848 /// original call destination.
4850 /// That is,
4852 /// I1 OUTLINED_FUNCTION:
4853 /// I2 --> BL OUTLINED_FUNCTION I1
4854 /// BL f I2
4855 /// B f
4856 /// * Call construction overhead: 1 (BL)
4857 /// * Frame construction overhead: 0
4858 /// * Requires stack fixups? No
4860 /// \p MachineOutlinerRegSave implies that the function should be called with a
4861 /// save and restore of LR to an available register. This allows us to avoid
4862 /// stack fixups. Note that this outlining variant is compatible with the
4863 /// NoLRSave case.
4865 /// That is,
4867 /// I1 Save LR OUTLINED_FUNCTION:
4868 /// I2 --> BL OUTLINED_FUNCTION I1
4869 /// I3 Restore LR I2
4870 /// I3
4871 /// RET
4873 /// * Call construction overhead: 3 (save + BL + restore)
4874 /// * Frame construction overhead: 1 (ret)
4875 /// * Requires stack fixups? No
4876 enum MachineOutlinerClass {
4877 MachineOutlinerDefault, /// Emit a save, restore, call, and return.
4878 MachineOutlinerTailCall, /// Only emit a branch.
4879 MachineOutlinerNoLRSave, /// Emit a call and return.
4880 MachineOutlinerThunk, /// Emit a call and tail-call.
4881 MachineOutlinerRegSave /// Same as default, but save to a register.
4884 enum MachineOutlinerMBBFlags {
4885 LRUnavailableSomewhere = 0x2,
4886 HasCalls = 0x4,
4887 UnsafeRegsDead = 0x8
4890 unsigned
4891 AArch64InstrInfo::findRegisterToSaveLRTo(const outliner::Candidate &C) const {
4892 assert(C.LRUWasSet && "LRU wasn't set?");
4893 MachineFunction *MF = C.getMF();
4894 const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
4895 MF->getSubtarget().getRegisterInfo());
4897 // Check if there is an available register across the sequence that we can
4898 // use.
4899 for (unsigned Reg : AArch64::GPR64RegClass) {
4900 if (!ARI->isReservedReg(*MF, Reg) &&
4901 Reg != AArch64::LR && // LR is not reserved, but don't use it.
4902 Reg != AArch64::X16 && // X16 is not guaranteed to be preserved.
4903 Reg != AArch64::X17 && // Ditto for X17.
4904 C.LRU.available(Reg) && C.UsedInSequence.available(Reg))
4905 return Reg;
4908 // No suitable register. Return 0.
4909 return 0u;
4912 outliner::OutlinedFunction
4913 AArch64InstrInfo::getOutliningCandidateInfo(
4914 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
4915 outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
4916 unsigned SequenceSize =
4917 std::accumulate(FirstCand.front(), std::next(FirstCand.back()), 0,
4918 [this](unsigned Sum, const MachineInstr &MI) {
4919 return Sum + getInstSizeInBytes(MI);
4922 // Properties about candidate MBBs that hold for all of them.
4923 unsigned FlagsSetInAll = 0xF;
4925 // Compute liveness information for each candidate, and set FlagsSetInAll.
4926 const TargetRegisterInfo &TRI = getRegisterInfo();
4927 std::for_each(RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(),
4928 [&FlagsSetInAll](outliner::Candidate &C) {
4929 FlagsSetInAll &= C.Flags;
4932 // According to the AArch64 Procedure Call Standard, the following are
4933 // undefined on entry/exit from a function call:
4935 // * Registers x16, x17, (and thus w16, w17)
4936 // * Condition codes (and thus the NZCV register)
4938 // Because if this, we can't outline any sequence of instructions where
4939 // one
4940 // of these registers is live into/across it. Thus, we need to delete
4941 // those
4942 // candidates.
4943 auto CantGuaranteeValueAcrossCall = [&TRI](outliner::Candidate &C) {
4944 // If the unsafe registers in this block are all dead, then we don't need
4945 // to compute liveness here.
4946 if (C.Flags & UnsafeRegsDead)
4947 return false;
4948 C.initLRU(TRI);
4949 LiveRegUnits LRU = C.LRU;
4950 return (!LRU.available(AArch64::W16) || !LRU.available(AArch64::W17) ||
4951 !LRU.available(AArch64::NZCV));
4954 // Are there any candidates where those registers are live?
4955 if (!(FlagsSetInAll & UnsafeRegsDead)) {
4956 // Erase every candidate that violates the restrictions above. (It could be
4957 // true that we have viable candidates, so it's not worth bailing out in
4958 // the case that, say, 1 out of 20 candidates violate the restructions.)
4959 RepeatedSequenceLocs.erase(std::remove_if(RepeatedSequenceLocs.begin(),
4960 RepeatedSequenceLocs.end(),
4961 CantGuaranteeValueAcrossCall),
4962 RepeatedSequenceLocs.end());
4964 // If the sequence doesn't have enough candidates left, then we're done.
4965 if (RepeatedSequenceLocs.size() < 2)
4966 return outliner::OutlinedFunction();
4969 // At this point, we have only "safe" candidates to outline. Figure out
4970 // frame + call instruction information.
4972 unsigned LastInstrOpcode = RepeatedSequenceLocs[0].back()->getOpcode();
4974 // Helper lambda which sets call information for every candidate.
4975 auto SetCandidateCallInfo =
4976 [&RepeatedSequenceLocs](unsigned CallID, unsigned NumBytesForCall) {
4977 for (outliner::Candidate &C : RepeatedSequenceLocs)
4978 C.setCallInfo(CallID, NumBytesForCall);
4981 unsigned FrameID = MachineOutlinerDefault;
4982 unsigned NumBytesToCreateFrame = 4;
4984 bool HasBTI = any_of(RepeatedSequenceLocs, [](outliner::Candidate &C) {
4985 return C.getMF()->getFunction().hasFnAttribute("branch-target-enforcement");
4988 // Returns true if an instructions is safe to fix up, false otherwise.
4989 auto IsSafeToFixup = [this, &TRI](MachineInstr &MI) {
4990 if (MI.isCall())
4991 return true;
4993 if (!MI.modifiesRegister(AArch64::SP, &TRI) &&
4994 !MI.readsRegister(AArch64::SP, &TRI))
4995 return true;
4997 // Any modification of SP will break our code to save/restore LR.
4998 // FIXME: We could handle some instructions which add a constant
4999 // offset to SP, with a bit more work.
5000 if (MI.modifiesRegister(AArch64::SP, &TRI))
5001 return false;
5003 // At this point, we have a stack instruction that we might need to
5004 // fix up. We'll handle it if it's a load or store.
5005 if (MI.mayLoadOrStore()) {
5006 const MachineOperand *Base; // Filled with the base operand of MI.
5007 int64_t Offset; // Filled with the offset of MI.
5009 // Does it allow us to offset the base operand and is the base the
5010 // register SP?
5011 if (!getMemOperandWithOffset(MI, Base, Offset, &TRI) || !Base->isReg() ||
5012 Base->getReg() != AArch64::SP)
5013 return false;
5015 // Find the minimum/maximum offset for this instruction and check
5016 // if fixing it up would be in range.
5017 int64_t MinOffset,
5018 MaxOffset; // Unscaled offsets for the instruction.
5019 unsigned Scale; // The scale to multiply the offsets by.
5020 unsigned DummyWidth;
5021 getMemOpInfo(MI.getOpcode(), Scale, DummyWidth, MinOffset, MaxOffset);
5023 Offset += 16; // Update the offset to what it would be if we outlined.
5024 if (Offset < MinOffset * Scale || Offset > MaxOffset * Scale)
5025 return false;
5027 // It's in range, so we can outline it.
5028 return true;
5031 // FIXME: Add handling for instructions like "add x0, sp, #8".
5033 // We can't fix it up, so don't outline it.
5034 return false;
5037 // True if it's possible to fix up each stack instruction in this sequence.
5038 // Important for frames/call variants that modify the stack.
5039 bool AllStackInstrsSafe = std::all_of(
5040 FirstCand.front(), std::next(FirstCand.back()), IsSafeToFixup);
5042 // If the last instruction in any candidate is a terminator, then we should
5043 // tail call all of the candidates.
5044 if (RepeatedSequenceLocs[0].back()->isTerminator()) {
5045 FrameID = MachineOutlinerTailCall;
5046 NumBytesToCreateFrame = 0;
5047 SetCandidateCallInfo(MachineOutlinerTailCall, 4);
5050 else if (LastInstrOpcode == AArch64::BL ||
5051 (LastInstrOpcode == AArch64::BLR && !HasBTI)) {
5052 // FIXME: Do we need to check if the code after this uses the value of LR?
5053 FrameID = MachineOutlinerThunk;
5054 NumBytesToCreateFrame = 0;
5055 SetCandidateCallInfo(MachineOutlinerThunk, 4);
5058 else {
5059 // We need to decide how to emit calls + frames. We can always emit the same
5060 // frame if we don't need to save to the stack. If we have to save to the
5061 // stack, then we need a different frame.
5062 unsigned NumBytesNoStackCalls = 0;
5063 std::vector<outliner::Candidate> CandidatesWithoutStackFixups;
5065 for (outliner::Candidate &C : RepeatedSequenceLocs) {
5066 C.initLRU(TRI);
5068 // Is LR available? If so, we don't need a save.
5069 if (C.LRU.available(AArch64::LR)) {
5070 NumBytesNoStackCalls += 4;
5071 C.setCallInfo(MachineOutlinerNoLRSave, 4);
5072 CandidatesWithoutStackFixups.push_back(C);
5075 // Is an unused register available? If so, we won't modify the stack, so
5076 // we can outline with the same frame type as those that don't save LR.
5077 else if (findRegisterToSaveLRTo(C)) {
5078 NumBytesNoStackCalls += 12;
5079 C.setCallInfo(MachineOutlinerRegSave, 12);
5080 CandidatesWithoutStackFixups.push_back(C);
5083 // Is SP used in the sequence at all? If not, we don't have to modify
5084 // the stack, so we are guaranteed to get the same frame.
5085 else if (C.UsedInSequence.available(AArch64::SP)) {
5086 NumBytesNoStackCalls += 12;
5087 C.setCallInfo(MachineOutlinerDefault, 12);
5088 CandidatesWithoutStackFixups.push_back(C);
5091 // If we outline this, we need to modify the stack. Pretend we don't
5092 // outline this by saving all of its bytes.
5093 else {
5094 NumBytesNoStackCalls += SequenceSize;
5098 // If there are no places where we have to save LR, then note that we
5099 // don't have to update the stack. Otherwise, give every candidate the
5100 // default call type, as long as it's safe to do so.
5101 if (!AllStackInstrsSafe ||
5102 NumBytesNoStackCalls <= RepeatedSequenceLocs.size() * 12) {
5103 RepeatedSequenceLocs = CandidatesWithoutStackFixups;
5104 FrameID = MachineOutlinerNoLRSave;
5105 } else {
5106 SetCandidateCallInfo(MachineOutlinerDefault, 12);
5109 // If we dropped all of the candidates, bail out here.
5110 if (RepeatedSequenceLocs.size() < 2) {
5111 RepeatedSequenceLocs.clear();
5112 return outliner::OutlinedFunction();
5116 // Does every candidate's MBB contain a call? If so, then we might have a call
5117 // in the range.
5118 if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
5119 // Check if the range contains a call. These require a save + restore of the
5120 // link register.
5121 bool ModStackToSaveLR = false;
5122 if (std::any_of(FirstCand.front(), FirstCand.back(),
5123 [](const MachineInstr &MI) { return MI.isCall(); }))
5124 ModStackToSaveLR = true;
5126 // Handle the last instruction separately. If this is a tail call, then the
5127 // last instruction is a call. We don't want to save + restore in this case.
5128 // However, it could be possible that the last instruction is a call without
5129 // it being valid to tail call this sequence. We should consider this as
5130 // well.
5131 else if (FrameID != MachineOutlinerThunk &&
5132 FrameID != MachineOutlinerTailCall && FirstCand.back()->isCall())
5133 ModStackToSaveLR = true;
5135 if (ModStackToSaveLR) {
5136 // We can't fix up the stack. Bail out.
5137 if (!AllStackInstrsSafe) {
5138 RepeatedSequenceLocs.clear();
5139 return outliner::OutlinedFunction();
5142 // Save + restore LR.
5143 NumBytesToCreateFrame += 8;
5147 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
5148 NumBytesToCreateFrame, FrameID);
5151 bool AArch64InstrInfo::isFunctionSafeToOutlineFrom(
5152 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
5153 const Function &F = MF.getFunction();
5155 // Can F be deduplicated by the linker? If it can, don't outline from it.
5156 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
5157 return false;
5159 // Don't outline from functions with section markings; the program could
5160 // expect that all the code is in the named section.
5161 // FIXME: Allow outlining from multiple functions with the same section
5162 // marking.
5163 if (F.hasSection())
5164 return false;
5166 // Outlining from functions with redzones is unsafe since the outliner may
5167 // modify the stack. Check if hasRedZone is true or unknown; if yes, don't
5168 // outline from it.
5169 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
5170 if (!AFI || AFI->hasRedZone().getValueOr(true))
5171 return false;
5173 // It's safe to outline from MF.
5174 return true;
5177 bool AArch64InstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
5178 unsigned &Flags) const {
5179 // Check if LR is available through all of the MBB. If it's not, then set
5180 // a flag.
5181 assert(MBB.getParent()->getRegInfo().tracksLiveness() &&
5182 "Suitable Machine Function for outlining must track liveness");
5183 LiveRegUnits LRU(getRegisterInfo());
5185 std::for_each(MBB.rbegin(), MBB.rend(),
5186 [&LRU](MachineInstr &MI) { LRU.accumulate(MI); });
5188 // Check if each of the unsafe registers are available...
5189 bool W16AvailableInBlock = LRU.available(AArch64::W16);
5190 bool W17AvailableInBlock = LRU.available(AArch64::W17);
5191 bool NZCVAvailableInBlock = LRU.available(AArch64::NZCV);
5193 // If all of these are dead (and not live out), we know we don't have to check
5194 // them later.
5195 if (W16AvailableInBlock && W17AvailableInBlock && NZCVAvailableInBlock)
5196 Flags |= MachineOutlinerMBBFlags::UnsafeRegsDead;
5198 // Now, add the live outs to the set.
5199 LRU.addLiveOuts(MBB);
5201 // If any of these registers is available in the MBB, but also a live out of
5202 // the block, then we know outlining is unsafe.
5203 if (W16AvailableInBlock && !LRU.available(AArch64::W16))
5204 return false;
5205 if (W17AvailableInBlock && !LRU.available(AArch64::W17))
5206 return false;
5207 if (NZCVAvailableInBlock && !LRU.available(AArch64::NZCV))
5208 return false;
5210 // Check if there's a call inside this MachineBasicBlock. If there is, then
5211 // set a flag.
5212 if (any_of(MBB, [](MachineInstr &MI) { return MI.isCall(); }))
5213 Flags |= MachineOutlinerMBBFlags::HasCalls;
5215 MachineFunction *MF = MBB.getParent();
5217 // In the event that we outline, we may have to save LR. If there is an
5218 // available register in the MBB, then we'll always save LR there. Check if
5219 // this is true.
5220 bool CanSaveLR = false;
5221 const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
5222 MF->getSubtarget().getRegisterInfo());
5224 // Check if there is an available register across the sequence that we can
5225 // use.
5226 for (unsigned Reg : AArch64::GPR64RegClass) {
5227 if (!ARI->isReservedReg(*MF, Reg) && Reg != AArch64::LR &&
5228 Reg != AArch64::X16 && Reg != AArch64::X17 && LRU.available(Reg)) {
5229 CanSaveLR = true;
5230 break;
5234 // Check if we have a register we can save LR to, and if LR was used
5235 // somewhere. If both of those things are true, then we need to evaluate the
5236 // safety of outlining stack instructions later.
5237 if (!CanSaveLR && !LRU.available(AArch64::LR))
5238 Flags |= MachineOutlinerMBBFlags::LRUnavailableSomewhere;
5240 return true;
5243 outliner::InstrType
5244 AArch64InstrInfo::getOutliningType(MachineBasicBlock::iterator &MIT,
5245 unsigned Flags) const {
5246 MachineInstr &MI = *MIT;
5247 MachineBasicBlock *MBB = MI.getParent();
5248 MachineFunction *MF = MBB->getParent();
5249 AArch64FunctionInfo *FuncInfo = MF->getInfo<AArch64FunctionInfo>();
5251 // Don't outline LOHs.
5252 if (FuncInfo->getLOHRelated().count(&MI))
5253 return outliner::InstrType::Illegal;
5255 // Don't allow debug values to impact outlining type.
5256 if (MI.isDebugInstr() || MI.isIndirectDebugValue())
5257 return outliner::InstrType::Invisible;
5259 // At this point, KILL instructions don't really tell us much so we can go
5260 // ahead and skip over them.
5261 if (MI.isKill())
5262 return outliner::InstrType::Invisible;
5264 // Is this a terminator for a basic block?
5265 if (MI.isTerminator()) {
5267 // Is this the end of a function?
5268 if (MI.getParent()->succ_empty())
5269 return outliner::InstrType::Legal;
5271 // It's not, so don't outline it.
5272 return outliner::InstrType::Illegal;
5275 // Make sure none of the operands are un-outlinable.
5276 for (const MachineOperand &MOP : MI.operands()) {
5277 if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() ||
5278 MOP.isTargetIndex())
5279 return outliner::InstrType::Illegal;
5281 // If it uses LR or W30 explicitly, then don't touch it.
5282 if (MOP.isReg() && !MOP.isImplicit() &&
5283 (MOP.getReg() == AArch64::LR || MOP.getReg() == AArch64::W30))
5284 return outliner::InstrType::Illegal;
5287 // Special cases for instructions that can always be outlined, but will fail
5288 // the later tests. e.g, ADRPs, which are PC-relative use LR, but can always
5289 // be outlined because they don't require a *specific* value to be in LR.
5290 if (MI.getOpcode() == AArch64::ADRP)
5291 return outliner::InstrType::Legal;
5293 // If MI is a call we might be able to outline it. We don't want to outline
5294 // any calls that rely on the position of items on the stack. When we outline
5295 // something containing a call, we have to emit a save and restore of LR in
5296 // the outlined function. Currently, this always happens by saving LR to the
5297 // stack. Thus, if we outline, say, half the parameters for a function call
5298 // plus the call, then we'll break the callee's expectations for the layout
5299 // of the stack.
5301 // FIXME: Allow calls to functions which construct a stack frame, as long
5302 // as they don't access arguments on the stack.
5303 // FIXME: Figure out some way to analyze functions defined in other modules.
5304 // We should be able to compute the memory usage based on the IR calling
5305 // convention, even if we can't see the definition.
5306 if (MI.isCall()) {
5307 // Get the function associated with the call. Look at each operand and find
5308 // the one that represents the callee and get its name.
5309 const Function *Callee = nullptr;
5310 for (const MachineOperand &MOP : MI.operands()) {
5311 if (MOP.isGlobal()) {
5312 Callee = dyn_cast<Function>(MOP.getGlobal());
5313 break;
5317 // Never outline calls to mcount. There isn't any rule that would require
5318 // this, but the Linux kernel's "ftrace" feature depends on it.
5319 if (Callee && Callee->getName() == "\01_mcount")
5320 return outliner::InstrType::Illegal;
5322 // If we don't know anything about the callee, assume it depends on the
5323 // stack layout of the caller. In that case, it's only legal to outline
5324 // as a tail-call. Whitelist the call instructions we know about so we
5325 // don't get unexpected results with call pseudo-instructions.
5326 auto UnknownCallOutlineType = outliner::InstrType::Illegal;
5327 if (MI.getOpcode() == AArch64::BLR || MI.getOpcode() == AArch64::BL)
5328 UnknownCallOutlineType = outliner::InstrType::LegalTerminator;
5330 if (!Callee)
5331 return UnknownCallOutlineType;
5333 // We have a function we have information about. Check it if it's something
5334 // can safely outline.
5335 MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
5337 // We don't know what's going on with the callee at all. Don't touch it.
5338 if (!CalleeMF)
5339 return UnknownCallOutlineType;
5341 // Check if we know anything about the callee saves on the function. If we
5342 // don't, then don't touch it, since that implies that we haven't
5343 // computed anything about its stack frame yet.
5344 MachineFrameInfo &MFI = CalleeMF->getFrameInfo();
5345 if (!MFI.isCalleeSavedInfoValid() || MFI.getStackSize() > 0 ||
5346 MFI.getNumObjects() > 0)
5347 return UnknownCallOutlineType;
5349 // At this point, we can say that CalleeMF ought to not pass anything on the
5350 // stack. Therefore, we can outline it.
5351 return outliner::InstrType::Legal;
5354 // Don't outline positions.
5355 if (MI.isPosition())
5356 return outliner::InstrType::Illegal;
5358 // Don't touch the link register or W30.
5359 if (MI.readsRegister(AArch64::W30, &getRegisterInfo()) ||
5360 MI.modifiesRegister(AArch64::W30, &getRegisterInfo()))
5361 return outliner::InstrType::Illegal;
5363 // Don't outline BTI instructions, because that will prevent the outlining
5364 // site from being indirectly callable.
5365 if (MI.getOpcode() == AArch64::HINT) {
5366 int64_t Imm = MI.getOperand(0).getImm();
5367 if (Imm == 32 || Imm == 34 || Imm == 36 || Imm == 38)
5368 return outliner::InstrType::Illegal;
5371 return outliner::InstrType::Legal;
5374 void AArch64InstrInfo::fixupPostOutline(MachineBasicBlock &MBB) const {
5375 for (MachineInstr &MI : MBB) {
5376 const MachineOperand *Base;
5377 unsigned Width;
5378 int64_t Offset;
5380 // Is this a load or store with an immediate offset with SP as the base?
5381 if (!MI.mayLoadOrStore() ||
5382 !getMemOperandWithOffsetWidth(MI, Base, Offset, Width, &RI) ||
5383 (Base->isReg() && Base->getReg() != AArch64::SP))
5384 continue;
5386 // It is, so we have to fix it up.
5387 unsigned Scale;
5388 int64_t Dummy1, Dummy2;
5390 MachineOperand &StackOffsetOperand = getMemOpBaseRegImmOfsOffsetOperand(MI);
5391 assert(StackOffsetOperand.isImm() && "Stack offset wasn't immediate!");
5392 getMemOpInfo(MI.getOpcode(), Scale, Width, Dummy1, Dummy2);
5393 assert(Scale != 0 && "Unexpected opcode!");
5395 // We've pushed the return address to the stack, so add 16 to the offset.
5396 // This is safe, since we already checked if it would overflow when we
5397 // checked if this instruction was legal to outline.
5398 int64_t NewImm = (Offset + 16) / Scale;
5399 StackOffsetOperand.setImm(NewImm);
5403 void AArch64InstrInfo::buildOutlinedFrame(
5404 MachineBasicBlock &MBB, MachineFunction &MF,
5405 const outliner::OutlinedFunction &OF) const {
5406 // For thunk outlining, rewrite the last instruction from a call to a
5407 // tail-call.
5408 if (OF.FrameConstructionID == MachineOutlinerThunk) {
5409 MachineInstr *Call = &*--MBB.instr_end();
5410 unsigned TailOpcode;
5411 if (Call->getOpcode() == AArch64::BL) {
5412 TailOpcode = AArch64::TCRETURNdi;
5413 } else {
5414 assert(Call->getOpcode() == AArch64::BLR);
5415 TailOpcode = AArch64::TCRETURNriALL;
5417 MachineInstr *TC = BuildMI(MF, DebugLoc(), get(TailOpcode))
5418 .add(Call->getOperand(0))
5419 .addImm(0);
5420 MBB.insert(MBB.end(), TC);
5421 Call->eraseFromParent();
5424 // Is there a call in the outlined range?
5425 auto IsNonTailCall = [](MachineInstr &MI) {
5426 return MI.isCall() && !MI.isReturn();
5428 if (std::any_of(MBB.instr_begin(), MBB.instr_end(), IsNonTailCall)) {
5429 // Fix up the instructions in the range, since we're going to modify the
5430 // stack.
5431 assert(OF.FrameConstructionID != MachineOutlinerDefault &&
5432 "Can only fix up stack references once");
5433 fixupPostOutline(MBB);
5435 // LR has to be a live in so that we can save it.
5436 MBB.addLiveIn(AArch64::LR);
5438 MachineBasicBlock::iterator It = MBB.begin();
5439 MachineBasicBlock::iterator Et = MBB.end();
5441 if (OF.FrameConstructionID == MachineOutlinerTailCall ||
5442 OF.FrameConstructionID == MachineOutlinerThunk)
5443 Et = std::prev(MBB.end());
5445 // Insert a save before the outlined region
5446 MachineInstr *STRXpre = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
5447 .addReg(AArch64::SP, RegState::Define)
5448 .addReg(AArch64::LR)
5449 .addReg(AArch64::SP)
5450 .addImm(-16);
5451 It = MBB.insert(It, STRXpre);
5453 const TargetSubtargetInfo &STI = MF.getSubtarget();
5454 const MCRegisterInfo *MRI = STI.getRegisterInfo();
5455 unsigned DwarfReg = MRI->getDwarfRegNum(AArch64::LR, true);
5457 // Add a CFI saying the stack was moved 16 B down.
5458 int64_t StackPosEntry =
5459 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 16));
5460 BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
5461 .addCFIIndex(StackPosEntry)
5462 .setMIFlags(MachineInstr::FrameSetup);
5464 // Add a CFI saying that the LR that we want to find is now 16 B higher than
5465 // before.
5466 int64_t LRPosEntry =
5467 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, DwarfReg, 16));
5468 BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
5469 .addCFIIndex(LRPosEntry)
5470 .setMIFlags(MachineInstr::FrameSetup);
5472 // Insert a restore before the terminator for the function.
5473 MachineInstr *LDRXpost = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
5474 .addReg(AArch64::SP, RegState::Define)
5475 .addReg(AArch64::LR, RegState::Define)
5476 .addReg(AArch64::SP)
5477 .addImm(16);
5478 Et = MBB.insert(Et, LDRXpost);
5481 // If this is a tail call outlined function, then there's already a return.
5482 if (OF.FrameConstructionID == MachineOutlinerTailCall ||
5483 OF.FrameConstructionID == MachineOutlinerThunk)
5484 return;
5486 // It's not a tail call, so we have to insert the return ourselves.
5487 MachineInstr *ret = BuildMI(MF, DebugLoc(), get(AArch64::RET))
5488 .addReg(AArch64::LR, RegState::Undef);
5489 MBB.insert(MBB.end(), ret);
5491 // Did we have to modify the stack by saving the link register?
5492 if (OF.FrameConstructionID != MachineOutlinerDefault)
5493 return;
5495 // We modified the stack.
5496 // Walk over the basic block and fix up all the stack accesses.
5497 fixupPostOutline(MBB);
5500 MachineBasicBlock::iterator AArch64InstrInfo::insertOutlinedCall(
5501 Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
5502 MachineFunction &MF, const outliner::Candidate &C) const {
5504 // Are we tail calling?
5505 if (C.CallConstructionID == MachineOutlinerTailCall) {
5506 // If yes, then we can just branch to the label.
5507 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::TCRETURNdi))
5508 .addGlobalAddress(M.getNamedValue(MF.getName()))
5509 .addImm(0));
5510 return It;
5513 // Are we saving the link register?
5514 if (C.CallConstructionID == MachineOutlinerNoLRSave ||
5515 C.CallConstructionID == MachineOutlinerThunk) {
5516 // No, so just insert the call.
5517 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
5518 .addGlobalAddress(M.getNamedValue(MF.getName())));
5519 return It;
5522 // We want to return the spot where we inserted the call.
5523 MachineBasicBlock::iterator CallPt;
5525 // Instructions for saving and restoring LR around the call instruction we're
5526 // going to insert.
5527 MachineInstr *Save;
5528 MachineInstr *Restore;
5529 // Can we save to a register?
5530 if (C.CallConstructionID == MachineOutlinerRegSave) {
5531 // FIXME: This logic should be sunk into a target-specific interface so that
5532 // we don't have to recompute the register.
5533 unsigned Reg = findRegisterToSaveLRTo(C);
5534 assert(Reg != 0 && "No callee-saved register available?");
5536 // Save and restore LR from that register.
5537 Save = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), Reg)
5538 .addReg(AArch64::XZR)
5539 .addReg(AArch64::LR)
5540 .addImm(0);
5541 Restore = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), AArch64::LR)
5542 .addReg(AArch64::XZR)
5543 .addReg(Reg)
5544 .addImm(0);
5545 } else {
5546 // We have the default case. Save and restore from SP.
5547 Save = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
5548 .addReg(AArch64::SP, RegState::Define)
5549 .addReg(AArch64::LR)
5550 .addReg(AArch64::SP)
5551 .addImm(-16);
5552 Restore = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
5553 .addReg(AArch64::SP, RegState::Define)
5554 .addReg(AArch64::LR, RegState::Define)
5555 .addReg(AArch64::SP)
5556 .addImm(16);
5559 It = MBB.insert(It, Save);
5560 It++;
5562 // Insert the call.
5563 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
5564 .addGlobalAddress(M.getNamedValue(MF.getName())));
5565 CallPt = It;
5566 It++;
5568 It = MBB.insert(It, Restore);
5569 return CallPt;
5572 bool AArch64InstrInfo::shouldOutlineFromFunctionByDefault(
5573 MachineFunction &MF) const {
5574 return MF.getFunction().hasMinSize();
5577 bool AArch64InstrInfo::isCopyInstrImpl(
5578 const MachineInstr &MI, const MachineOperand *&Source,
5579 const MachineOperand *&Destination) const {
5581 // AArch64::ORRWrs and AArch64::ORRXrs with WZR/XZR reg
5582 // and zero immediate operands used as an alias for mov instruction.
5583 if (MI.getOpcode() == AArch64::ORRWrs &&
5584 MI.getOperand(1).getReg() == AArch64::WZR &&
5585 MI.getOperand(3).getImm() == 0x0) {
5586 Destination = &MI.getOperand(0);
5587 Source = &MI.getOperand(2);
5588 return true;
5591 if (MI.getOpcode() == AArch64::ORRXrs &&
5592 MI.getOperand(1).getReg() == AArch64::XZR &&
5593 MI.getOperand(3).getImm() == 0x0) {
5594 Destination = &MI.getOperand(0);
5595 Source = &MI.getOperand(2);
5596 return true;
5599 return false;
5602 #define GET_INSTRINFO_HELPERS
5603 #include "AArch64GenInstrInfo.inc"