[ARM] Remove declaration of unimplemented function. NFC.
[llvm-complete.git] / lib / Target / AArch64 / AArch64InstrInfo.cpp
blob3e1e798e43beb06a5e6678a19a43eeb2d66a5fbe
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::FADDHrr:
3470 case AArch64::FADDSrr:
3471 case AArch64::FADDDrr:
3472 case AArch64::FADDv4f16:
3473 case AArch64::FADDv8f16:
3474 case AArch64::FADDv2f32:
3475 case AArch64::FADDv2f64:
3476 case AArch64::FADDv4f32:
3477 case AArch64::FSUBHrr:
3478 case AArch64::FSUBSrr:
3479 case AArch64::FSUBDrr:
3480 case AArch64::FSUBv4f16:
3481 case AArch64::FSUBv8f16:
3482 case AArch64::FSUBv2f32:
3483 case AArch64::FSUBv2f64:
3484 case AArch64::FSUBv4f32:
3485 TargetOptions Options = Inst.getParent()->getParent()->getTarget().Options;
3486 return (Options.UnsafeFPMath ||
3487 Options.AllowFPOpFusion == FPOpFusion::Fast);
3489 return false;
3492 // Opcodes that can be combined with a MUL
3493 static bool isCombineInstrCandidate(unsigned Opc) {
3494 return (isCombineInstrCandidate32(Opc) || isCombineInstrCandidate64(Opc));
3498 // Utility routine that checks if \param MO is defined by an
3499 // \param CombineOpc instruction in the basic block \param MBB
3500 static bool canCombine(MachineBasicBlock &MBB, MachineOperand &MO,
3501 unsigned CombineOpc, unsigned ZeroReg = 0,
3502 bool CheckZeroReg = false) {
3503 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3504 MachineInstr *MI = nullptr;
3506 if (MO.isReg() && Register::isVirtualRegister(MO.getReg()))
3507 MI = MRI.getUniqueVRegDef(MO.getReg());
3508 // And it needs to be in the trace (otherwise, it won't have a depth).
3509 if (!MI || MI->getParent() != &MBB || (unsigned)MI->getOpcode() != CombineOpc)
3510 return false;
3511 // Must only used by the user we combine with.
3512 if (!MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
3513 return false;
3515 if (CheckZeroReg) {
3516 assert(MI->getNumOperands() >= 4 && MI->getOperand(0).isReg() &&
3517 MI->getOperand(1).isReg() && MI->getOperand(2).isReg() &&
3518 MI->getOperand(3).isReg() && "MAdd/MSub must have a least 4 regs");
3519 // The third input reg must be zero.
3520 if (MI->getOperand(3).getReg() != ZeroReg)
3521 return false;
3524 return true;
3528 // Is \param MO defined by an integer multiply and can be combined?
3529 static bool canCombineWithMUL(MachineBasicBlock &MBB, MachineOperand &MO,
3530 unsigned MulOpc, unsigned ZeroReg) {
3531 return canCombine(MBB, MO, MulOpc, ZeroReg, true);
3535 // Is \param MO defined by a floating-point multiply and can be combined?
3536 static bool canCombineWithFMUL(MachineBasicBlock &MBB, MachineOperand &MO,
3537 unsigned MulOpc) {
3538 return canCombine(MBB, MO, MulOpc);
3541 // TODO: There are many more machine instruction opcodes to match:
3542 // 1. Other data types (integer, vectors)
3543 // 2. Other math / logic operations (xor, or)
3544 // 3. Other forms of the same operation (intrinsics and other variants)
3545 bool AArch64InstrInfo::isAssociativeAndCommutative(
3546 const MachineInstr &Inst) const {
3547 switch (Inst.getOpcode()) {
3548 case AArch64::FADDDrr:
3549 case AArch64::FADDSrr:
3550 case AArch64::FADDv2f32:
3551 case AArch64::FADDv2f64:
3552 case AArch64::FADDv4f32:
3553 case AArch64::FMULDrr:
3554 case AArch64::FMULSrr:
3555 case AArch64::FMULX32:
3556 case AArch64::FMULX64:
3557 case AArch64::FMULXv2f32:
3558 case AArch64::FMULXv2f64:
3559 case AArch64::FMULXv4f32:
3560 case AArch64::FMULv2f32:
3561 case AArch64::FMULv2f64:
3562 case AArch64::FMULv4f32:
3563 return Inst.getParent()->getParent()->getTarget().Options.UnsafeFPMath;
3564 default:
3565 return false;
3569 /// Find instructions that can be turned into madd.
3570 static bool getMaddPatterns(MachineInstr &Root,
3571 SmallVectorImpl<MachineCombinerPattern> &Patterns) {
3572 unsigned Opc = Root.getOpcode();
3573 MachineBasicBlock &MBB = *Root.getParent();
3574 bool Found = false;
3576 if (!isCombineInstrCandidate(Opc))
3577 return false;
3578 if (isCombineInstrSettingFlag(Opc)) {
3579 int Cmp_NZCV = Root.findRegisterDefOperandIdx(AArch64::NZCV, true);
3580 // When NZCV is live bail out.
3581 if (Cmp_NZCV == -1)
3582 return false;
3583 unsigned NewOpc = convertToNonFlagSettingOpc(Root);
3584 // When opcode can't change bail out.
3585 // CHECKME: do we miss any cases for opcode conversion?
3586 if (NewOpc == Opc)
3587 return false;
3588 Opc = NewOpc;
3591 switch (Opc) {
3592 default:
3593 break;
3594 case AArch64::ADDWrr:
3595 assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
3596 "ADDWrr does not have register operands");
3597 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3598 AArch64::WZR)) {
3599 Patterns.push_back(MachineCombinerPattern::MULADDW_OP1);
3600 Found = true;
3602 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDWrrr,
3603 AArch64::WZR)) {
3604 Patterns.push_back(MachineCombinerPattern::MULADDW_OP2);
3605 Found = true;
3607 break;
3608 case AArch64::ADDXrr:
3609 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3610 AArch64::XZR)) {
3611 Patterns.push_back(MachineCombinerPattern::MULADDX_OP1);
3612 Found = true;
3614 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDXrrr,
3615 AArch64::XZR)) {
3616 Patterns.push_back(MachineCombinerPattern::MULADDX_OP2);
3617 Found = true;
3619 break;
3620 case AArch64::SUBWrr:
3621 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3622 AArch64::WZR)) {
3623 Patterns.push_back(MachineCombinerPattern::MULSUBW_OP1);
3624 Found = true;
3626 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDWrrr,
3627 AArch64::WZR)) {
3628 Patterns.push_back(MachineCombinerPattern::MULSUBW_OP2);
3629 Found = true;
3631 break;
3632 case AArch64::SUBXrr:
3633 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3634 AArch64::XZR)) {
3635 Patterns.push_back(MachineCombinerPattern::MULSUBX_OP1);
3636 Found = true;
3638 if (canCombineWithMUL(MBB, Root.getOperand(2), AArch64::MADDXrrr,
3639 AArch64::XZR)) {
3640 Patterns.push_back(MachineCombinerPattern::MULSUBX_OP2);
3641 Found = true;
3643 break;
3644 case AArch64::ADDWri:
3645 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3646 AArch64::WZR)) {
3647 Patterns.push_back(MachineCombinerPattern::MULADDWI_OP1);
3648 Found = true;
3650 break;
3651 case AArch64::ADDXri:
3652 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3653 AArch64::XZR)) {
3654 Patterns.push_back(MachineCombinerPattern::MULADDXI_OP1);
3655 Found = true;
3657 break;
3658 case AArch64::SUBWri:
3659 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDWrrr,
3660 AArch64::WZR)) {
3661 Patterns.push_back(MachineCombinerPattern::MULSUBWI_OP1);
3662 Found = true;
3664 break;
3665 case AArch64::SUBXri:
3666 if (canCombineWithMUL(MBB, Root.getOperand(1), AArch64::MADDXrrr,
3667 AArch64::XZR)) {
3668 Patterns.push_back(MachineCombinerPattern::MULSUBXI_OP1);
3669 Found = true;
3671 break;
3673 return Found;
3675 /// Floating-Point Support
3677 /// Find instructions that can be turned into madd.
3678 static bool getFMAPatterns(MachineInstr &Root,
3679 SmallVectorImpl<MachineCombinerPattern> &Patterns) {
3681 if (!isCombineInstrCandidateFP(Root))
3682 return false;
3684 MachineBasicBlock &MBB = *Root.getParent();
3685 bool Found = false;
3687 switch (Root.getOpcode()) {
3688 default:
3689 assert(false && "Unsupported FP instruction in combiner\n");
3690 break;
3691 case AArch64::FADDHrr:
3692 assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
3693 "FADDHrr does not have register operands");
3694 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULHrr)) {
3695 Patterns.push_back(MachineCombinerPattern::FMULADDH_OP1);
3696 Found = true;
3698 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULHrr)) {
3699 Patterns.push_back(MachineCombinerPattern::FMULADDH_OP2);
3700 Found = true;
3702 break;
3703 case AArch64::FADDSrr:
3704 assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
3705 "FADDSrr does not have register operands");
3706 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULSrr)) {
3707 Patterns.push_back(MachineCombinerPattern::FMULADDS_OP1);
3708 Found = true;
3709 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3710 AArch64::FMULv1i32_indexed)) {
3711 Patterns.push_back(MachineCombinerPattern::FMLAv1i32_indexed_OP1);
3712 Found = true;
3714 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULSrr)) {
3715 Patterns.push_back(MachineCombinerPattern::FMULADDS_OP2);
3716 Found = true;
3717 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3718 AArch64::FMULv1i32_indexed)) {
3719 Patterns.push_back(MachineCombinerPattern::FMLAv1i32_indexed_OP2);
3720 Found = true;
3722 break;
3723 case AArch64::FADDDrr:
3724 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULDrr)) {
3725 Patterns.push_back(MachineCombinerPattern::FMULADDD_OP1);
3726 Found = true;
3727 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3728 AArch64::FMULv1i64_indexed)) {
3729 Patterns.push_back(MachineCombinerPattern::FMLAv1i64_indexed_OP1);
3730 Found = true;
3732 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULDrr)) {
3733 Patterns.push_back(MachineCombinerPattern::FMULADDD_OP2);
3734 Found = true;
3735 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3736 AArch64::FMULv1i64_indexed)) {
3737 Patterns.push_back(MachineCombinerPattern::FMLAv1i64_indexed_OP2);
3738 Found = true;
3740 break;
3741 case AArch64::FADDv4f16:
3742 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3743 AArch64::FMULv4i16_indexed)) {
3744 Patterns.push_back(MachineCombinerPattern::FMLAv4i16_indexed_OP1);
3745 Found = true;
3746 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3747 AArch64::FMULv4f16)) {
3748 Patterns.push_back(MachineCombinerPattern::FMLAv4f16_OP1);
3749 Found = true;
3751 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3752 AArch64::FMULv4i16_indexed)) {
3753 Patterns.push_back(MachineCombinerPattern::FMLAv4i16_indexed_OP2);
3754 Found = true;
3755 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3756 AArch64::FMULv4f16)) {
3757 Patterns.push_back(MachineCombinerPattern::FMLAv4f16_OP2);
3758 Found = true;
3760 break;
3761 case AArch64::FADDv8f16:
3762 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3763 AArch64::FMULv8i16_indexed)) {
3764 Patterns.push_back(MachineCombinerPattern::FMLAv8i16_indexed_OP1);
3765 Found = true;
3766 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3767 AArch64::FMULv8f16)) {
3768 Patterns.push_back(MachineCombinerPattern::FMLAv8f16_OP1);
3769 Found = true;
3771 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3772 AArch64::FMULv8i16_indexed)) {
3773 Patterns.push_back(MachineCombinerPattern::FMLAv8i16_indexed_OP2);
3774 Found = true;
3775 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3776 AArch64::FMULv8f16)) {
3777 Patterns.push_back(MachineCombinerPattern::FMLAv8f16_OP2);
3778 Found = true;
3780 break;
3781 case AArch64::FADDv2f32:
3782 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3783 AArch64::FMULv2i32_indexed)) {
3784 Patterns.push_back(MachineCombinerPattern::FMLAv2i32_indexed_OP1);
3785 Found = true;
3786 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3787 AArch64::FMULv2f32)) {
3788 Patterns.push_back(MachineCombinerPattern::FMLAv2f32_OP1);
3789 Found = true;
3791 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3792 AArch64::FMULv2i32_indexed)) {
3793 Patterns.push_back(MachineCombinerPattern::FMLAv2i32_indexed_OP2);
3794 Found = true;
3795 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3796 AArch64::FMULv2f32)) {
3797 Patterns.push_back(MachineCombinerPattern::FMLAv2f32_OP2);
3798 Found = true;
3800 break;
3801 case AArch64::FADDv2f64:
3802 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3803 AArch64::FMULv2i64_indexed)) {
3804 Patterns.push_back(MachineCombinerPattern::FMLAv2i64_indexed_OP1);
3805 Found = true;
3806 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3807 AArch64::FMULv2f64)) {
3808 Patterns.push_back(MachineCombinerPattern::FMLAv2f64_OP1);
3809 Found = true;
3811 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3812 AArch64::FMULv2i64_indexed)) {
3813 Patterns.push_back(MachineCombinerPattern::FMLAv2i64_indexed_OP2);
3814 Found = true;
3815 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3816 AArch64::FMULv2f64)) {
3817 Patterns.push_back(MachineCombinerPattern::FMLAv2f64_OP2);
3818 Found = true;
3820 break;
3821 case AArch64::FADDv4f32:
3822 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3823 AArch64::FMULv4i32_indexed)) {
3824 Patterns.push_back(MachineCombinerPattern::FMLAv4i32_indexed_OP1);
3825 Found = true;
3826 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3827 AArch64::FMULv4f32)) {
3828 Patterns.push_back(MachineCombinerPattern::FMLAv4f32_OP1);
3829 Found = true;
3831 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3832 AArch64::FMULv4i32_indexed)) {
3833 Patterns.push_back(MachineCombinerPattern::FMLAv4i32_indexed_OP2);
3834 Found = true;
3835 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3836 AArch64::FMULv4f32)) {
3837 Patterns.push_back(MachineCombinerPattern::FMLAv4f32_OP2);
3838 Found = true;
3840 break;
3842 case AArch64::FSUBHrr:
3843 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULHrr)) {
3844 Patterns.push_back(MachineCombinerPattern::FMULSUBH_OP1);
3845 Found = true;
3847 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULHrr)) {
3848 Patterns.push_back(MachineCombinerPattern::FMULSUBH_OP2);
3849 Found = true;
3851 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FNMULHrr)) {
3852 Patterns.push_back(MachineCombinerPattern::FNMULSUBH_OP1);
3853 Found = true;
3855 break;
3856 case AArch64::FSUBSrr:
3857 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULSrr)) {
3858 Patterns.push_back(MachineCombinerPattern::FMULSUBS_OP1);
3859 Found = true;
3861 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULSrr)) {
3862 Patterns.push_back(MachineCombinerPattern::FMULSUBS_OP2);
3863 Found = true;
3864 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3865 AArch64::FMULv1i32_indexed)) {
3866 Patterns.push_back(MachineCombinerPattern::FMLSv1i32_indexed_OP2);
3867 Found = true;
3869 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FNMULSrr)) {
3870 Patterns.push_back(MachineCombinerPattern::FNMULSUBS_OP1);
3871 Found = true;
3873 break;
3874 case AArch64::FSUBDrr:
3875 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FMULDrr)) {
3876 Patterns.push_back(MachineCombinerPattern::FMULSUBD_OP1);
3877 Found = true;
3879 if (canCombineWithFMUL(MBB, Root.getOperand(2), AArch64::FMULDrr)) {
3880 Patterns.push_back(MachineCombinerPattern::FMULSUBD_OP2);
3881 Found = true;
3882 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3883 AArch64::FMULv1i64_indexed)) {
3884 Patterns.push_back(MachineCombinerPattern::FMLSv1i64_indexed_OP2);
3885 Found = true;
3887 if (canCombineWithFMUL(MBB, Root.getOperand(1), AArch64::FNMULDrr)) {
3888 Patterns.push_back(MachineCombinerPattern::FNMULSUBD_OP1);
3889 Found = true;
3891 break;
3892 case AArch64::FSUBv4f16:
3893 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3894 AArch64::FMULv4i16_indexed)) {
3895 Patterns.push_back(MachineCombinerPattern::FMLSv4i16_indexed_OP2);
3896 Found = true;
3897 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3898 AArch64::FMULv4f16)) {
3899 Patterns.push_back(MachineCombinerPattern::FMLSv4f16_OP2);
3900 Found = true;
3902 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3903 AArch64::FMULv4i16_indexed)) {
3904 Patterns.push_back(MachineCombinerPattern::FMLSv2i32_indexed_OP1);
3905 Found = true;
3906 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3907 AArch64::FMULv4f16)) {
3908 Patterns.push_back(MachineCombinerPattern::FMLSv2f32_OP1);
3909 Found = true;
3911 break;
3912 case AArch64::FSUBv8f16:
3913 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3914 AArch64::FMULv8i16_indexed)) {
3915 Patterns.push_back(MachineCombinerPattern::FMLSv8i16_indexed_OP2);
3916 Found = true;
3917 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3918 AArch64::FMULv8f16)) {
3919 Patterns.push_back(MachineCombinerPattern::FMLSv8f16_OP2);
3920 Found = true;
3922 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3923 AArch64::FMULv8i16_indexed)) {
3924 Patterns.push_back(MachineCombinerPattern::FMLSv8i16_indexed_OP1);
3925 Found = true;
3926 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3927 AArch64::FMULv8f16)) {
3928 Patterns.push_back(MachineCombinerPattern::FMLSv8f16_OP1);
3929 Found = true;
3931 break;
3932 case AArch64::FSUBv2f32:
3933 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3934 AArch64::FMULv2i32_indexed)) {
3935 Patterns.push_back(MachineCombinerPattern::FMLSv2i32_indexed_OP2);
3936 Found = true;
3937 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3938 AArch64::FMULv2f32)) {
3939 Patterns.push_back(MachineCombinerPattern::FMLSv2f32_OP2);
3940 Found = true;
3942 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3943 AArch64::FMULv2i32_indexed)) {
3944 Patterns.push_back(MachineCombinerPattern::FMLSv2i32_indexed_OP1);
3945 Found = true;
3946 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3947 AArch64::FMULv2f32)) {
3948 Patterns.push_back(MachineCombinerPattern::FMLSv2f32_OP1);
3949 Found = true;
3951 break;
3952 case AArch64::FSUBv2f64:
3953 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3954 AArch64::FMULv2i64_indexed)) {
3955 Patterns.push_back(MachineCombinerPattern::FMLSv2i64_indexed_OP2);
3956 Found = true;
3957 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3958 AArch64::FMULv2f64)) {
3959 Patterns.push_back(MachineCombinerPattern::FMLSv2f64_OP2);
3960 Found = true;
3962 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3963 AArch64::FMULv2i64_indexed)) {
3964 Patterns.push_back(MachineCombinerPattern::FMLSv2i64_indexed_OP1);
3965 Found = true;
3966 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3967 AArch64::FMULv2f64)) {
3968 Patterns.push_back(MachineCombinerPattern::FMLSv2f64_OP1);
3969 Found = true;
3971 break;
3972 case AArch64::FSUBv4f32:
3973 if (canCombineWithFMUL(MBB, Root.getOperand(2),
3974 AArch64::FMULv4i32_indexed)) {
3975 Patterns.push_back(MachineCombinerPattern::FMLSv4i32_indexed_OP2);
3976 Found = true;
3977 } else if (canCombineWithFMUL(MBB, Root.getOperand(2),
3978 AArch64::FMULv4f32)) {
3979 Patterns.push_back(MachineCombinerPattern::FMLSv4f32_OP2);
3980 Found = true;
3982 if (canCombineWithFMUL(MBB, Root.getOperand(1),
3983 AArch64::FMULv4i32_indexed)) {
3984 Patterns.push_back(MachineCombinerPattern::FMLSv4i32_indexed_OP1);
3985 Found = true;
3986 } else if (canCombineWithFMUL(MBB, Root.getOperand(1),
3987 AArch64::FMULv4f32)) {
3988 Patterns.push_back(MachineCombinerPattern::FMLSv4f32_OP1);
3989 Found = true;
3991 break;
3993 return Found;
3996 /// Return true when a code sequence can improve throughput. It
3997 /// should be called only for instructions in loops.
3998 /// \param Pattern - combiner pattern
3999 bool AArch64InstrInfo::isThroughputPattern(
4000 MachineCombinerPattern Pattern) const {
4001 switch (Pattern) {
4002 default:
4003 break;
4004 case MachineCombinerPattern::FMULADDH_OP1:
4005 case MachineCombinerPattern::FMULADDH_OP2:
4006 case MachineCombinerPattern::FMULSUBH_OP1:
4007 case MachineCombinerPattern::FMULSUBH_OP2:
4008 case MachineCombinerPattern::FMULADDS_OP1:
4009 case MachineCombinerPattern::FMULADDS_OP2:
4010 case MachineCombinerPattern::FMULSUBS_OP1:
4011 case MachineCombinerPattern::FMULSUBS_OP2:
4012 case MachineCombinerPattern::FMULADDD_OP1:
4013 case MachineCombinerPattern::FMULADDD_OP2:
4014 case MachineCombinerPattern::FMULSUBD_OP1:
4015 case MachineCombinerPattern::FMULSUBD_OP2:
4016 case MachineCombinerPattern::FNMULSUBH_OP1:
4017 case MachineCombinerPattern::FNMULSUBS_OP1:
4018 case MachineCombinerPattern::FNMULSUBD_OP1:
4019 case MachineCombinerPattern::FMLAv4i16_indexed_OP1:
4020 case MachineCombinerPattern::FMLAv4i16_indexed_OP2:
4021 case MachineCombinerPattern::FMLAv8i16_indexed_OP1:
4022 case MachineCombinerPattern::FMLAv8i16_indexed_OP2:
4023 case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
4024 case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
4025 case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
4026 case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
4027 case MachineCombinerPattern::FMLAv4f16_OP2:
4028 case MachineCombinerPattern::FMLAv4f16_OP1:
4029 case MachineCombinerPattern::FMLAv8f16_OP1:
4030 case MachineCombinerPattern::FMLAv8f16_OP2:
4031 case MachineCombinerPattern::FMLAv2f32_OP2:
4032 case MachineCombinerPattern::FMLAv2f32_OP1:
4033 case MachineCombinerPattern::FMLAv2f64_OP1:
4034 case MachineCombinerPattern::FMLAv2f64_OP2:
4035 case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
4036 case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
4037 case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
4038 case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
4039 case MachineCombinerPattern::FMLAv4f32_OP1:
4040 case MachineCombinerPattern::FMLAv4f32_OP2:
4041 case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
4042 case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
4043 case MachineCombinerPattern::FMLSv4i16_indexed_OP2:
4044 case MachineCombinerPattern::FMLSv8i16_indexed_OP1:
4045 case MachineCombinerPattern::FMLSv8i16_indexed_OP2:
4046 case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
4047 case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
4048 case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
4049 case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
4050 case MachineCombinerPattern::FMLSv4f16_OP2:
4051 case MachineCombinerPattern::FMLSv8f16_OP1:
4052 case MachineCombinerPattern::FMLSv8f16_OP2:
4053 case MachineCombinerPattern::FMLSv2f32_OP2:
4054 case MachineCombinerPattern::FMLSv2f64_OP2:
4055 case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
4056 case MachineCombinerPattern::FMLSv4f32_OP2:
4057 return true;
4058 } // end switch (Pattern)
4059 return false;
4061 /// Return true when there is potentially a faster code sequence for an
4062 /// instruction chain ending in \p Root. All potential patterns are listed in
4063 /// the \p Pattern vector. Pattern should be sorted in priority order since the
4064 /// pattern evaluator stops checking as soon as it finds a faster sequence.
4066 bool AArch64InstrInfo::getMachineCombinerPatterns(
4067 MachineInstr &Root,
4068 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
4069 // Integer patterns
4070 if (getMaddPatterns(Root, Patterns))
4071 return true;
4072 // Floating point patterns
4073 if (getFMAPatterns(Root, Patterns))
4074 return true;
4076 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
4079 enum class FMAInstKind { Default, Indexed, Accumulator };
4080 /// genFusedMultiply - Generate fused multiply instructions.
4081 /// This function supports both integer and floating point instructions.
4082 /// A typical example:
4083 /// F|MUL I=A,B,0
4084 /// F|ADD R,I,C
4085 /// ==> F|MADD R,A,B,C
4086 /// \param MF Containing MachineFunction
4087 /// \param MRI Register information
4088 /// \param TII Target information
4089 /// \param Root is the F|ADD instruction
4090 /// \param [out] InsInstrs is a vector of machine instructions and will
4091 /// contain the generated madd instruction
4092 /// \param IdxMulOpd is index of operand in Root that is the result of
4093 /// the F|MUL. In the example above IdxMulOpd is 1.
4094 /// \param MaddOpc the opcode fo the f|madd instruction
4095 /// \param RC Register class of operands
4096 /// \param kind of fma instruction (addressing mode) to be generated
4097 /// \param ReplacedAddend is the result register from the instruction
4098 /// replacing the non-combined operand, if any.
4099 static MachineInstr *
4100 genFusedMultiply(MachineFunction &MF, MachineRegisterInfo &MRI,
4101 const TargetInstrInfo *TII, MachineInstr &Root,
4102 SmallVectorImpl<MachineInstr *> &InsInstrs, unsigned IdxMulOpd,
4103 unsigned MaddOpc, const TargetRegisterClass *RC,
4104 FMAInstKind kind = FMAInstKind::Default,
4105 const Register *ReplacedAddend = nullptr) {
4106 assert(IdxMulOpd == 1 || IdxMulOpd == 2);
4108 unsigned IdxOtherOpd = IdxMulOpd == 1 ? 2 : 1;
4109 MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
4110 Register ResultReg = Root.getOperand(0).getReg();
4111 Register SrcReg0 = MUL->getOperand(1).getReg();
4112 bool Src0IsKill = MUL->getOperand(1).isKill();
4113 Register SrcReg1 = MUL->getOperand(2).getReg();
4114 bool Src1IsKill = MUL->getOperand(2).isKill();
4116 unsigned SrcReg2;
4117 bool Src2IsKill;
4118 if (ReplacedAddend) {
4119 // If we just generated a new addend, we must be it's only use.
4120 SrcReg2 = *ReplacedAddend;
4121 Src2IsKill = true;
4122 } else {
4123 SrcReg2 = Root.getOperand(IdxOtherOpd).getReg();
4124 Src2IsKill = Root.getOperand(IdxOtherOpd).isKill();
4127 if (Register::isVirtualRegister(ResultReg))
4128 MRI.constrainRegClass(ResultReg, RC);
4129 if (Register::isVirtualRegister(SrcReg0))
4130 MRI.constrainRegClass(SrcReg0, RC);
4131 if (Register::isVirtualRegister(SrcReg1))
4132 MRI.constrainRegClass(SrcReg1, RC);
4133 if (Register::isVirtualRegister(SrcReg2))
4134 MRI.constrainRegClass(SrcReg2, RC);
4136 MachineInstrBuilder MIB;
4137 if (kind == FMAInstKind::Default)
4138 MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4139 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4140 .addReg(SrcReg1, getKillRegState(Src1IsKill))
4141 .addReg(SrcReg2, getKillRegState(Src2IsKill));
4142 else if (kind == FMAInstKind::Indexed)
4143 MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4144 .addReg(SrcReg2, getKillRegState(Src2IsKill))
4145 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4146 .addReg(SrcReg1, getKillRegState(Src1IsKill))
4147 .addImm(MUL->getOperand(3).getImm());
4148 else if (kind == FMAInstKind::Accumulator)
4149 MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4150 .addReg(SrcReg2, getKillRegState(Src2IsKill))
4151 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4152 .addReg(SrcReg1, getKillRegState(Src1IsKill));
4153 else
4154 assert(false && "Invalid FMA instruction kind \n");
4155 // Insert the MADD (MADD, FMA, FMS, FMLA, FMSL)
4156 InsInstrs.push_back(MIB);
4157 return MUL;
4160 /// genMaddR - Generate madd instruction and combine mul and add using
4161 /// an extra virtual register
4162 /// Example - an ADD intermediate needs to be stored in a register:
4163 /// MUL I=A,B,0
4164 /// ADD R,I,Imm
4165 /// ==> ORR V, ZR, Imm
4166 /// ==> MADD R,A,B,V
4167 /// \param MF Containing MachineFunction
4168 /// \param MRI Register information
4169 /// \param TII Target information
4170 /// \param Root is the ADD instruction
4171 /// \param [out] InsInstrs is a vector of machine instructions and will
4172 /// contain the generated madd instruction
4173 /// \param IdxMulOpd is index of operand in Root that is the result of
4174 /// the MUL. In the example above IdxMulOpd is 1.
4175 /// \param MaddOpc the opcode fo the madd instruction
4176 /// \param VR is a virtual register that holds the value of an ADD operand
4177 /// (V in the example above).
4178 /// \param RC Register class of operands
4179 static MachineInstr *genMaddR(MachineFunction &MF, MachineRegisterInfo &MRI,
4180 const TargetInstrInfo *TII, MachineInstr &Root,
4181 SmallVectorImpl<MachineInstr *> &InsInstrs,
4182 unsigned IdxMulOpd, unsigned MaddOpc, unsigned VR,
4183 const TargetRegisterClass *RC) {
4184 assert(IdxMulOpd == 1 || IdxMulOpd == 2);
4186 MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
4187 Register ResultReg = Root.getOperand(0).getReg();
4188 Register SrcReg0 = MUL->getOperand(1).getReg();
4189 bool Src0IsKill = MUL->getOperand(1).isKill();
4190 Register SrcReg1 = MUL->getOperand(2).getReg();
4191 bool Src1IsKill = MUL->getOperand(2).isKill();
4193 if (Register::isVirtualRegister(ResultReg))
4194 MRI.constrainRegClass(ResultReg, RC);
4195 if (Register::isVirtualRegister(SrcReg0))
4196 MRI.constrainRegClass(SrcReg0, RC);
4197 if (Register::isVirtualRegister(SrcReg1))
4198 MRI.constrainRegClass(SrcReg1, RC);
4199 if (Register::isVirtualRegister(VR))
4200 MRI.constrainRegClass(VR, RC);
4202 MachineInstrBuilder MIB =
4203 BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
4204 .addReg(SrcReg0, getKillRegState(Src0IsKill))
4205 .addReg(SrcReg1, getKillRegState(Src1IsKill))
4206 .addReg(VR);
4207 // Insert the MADD
4208 InsInstrs.push_back(MIB);
4209 return MUL;
4212 /// When getMachineCombinerPatterns() finds potential patterns,
4213 /// this function generates the instructions that could replace the
4214 /// original code sequence
4215 void AArch64InstrInfo::genAlternativeCodeSequence(
4216 MachineInstr &Root, MachineCombinerPattern Pattern,
4217 SmallVectorImpl<MachineInstr *> &InsInstrs,
4218 SmallVectorImpl<MachineInstr *> &DelInstrs,
4219 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
4220 MachineBasicBlock &MBB = *Root.getParent();
4221 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4222 MachineFunction &MF = *MBB.getParent();
4223 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
4225 MachineInstr *MUL;
4226 const TargetRegisterClass *RC;
4227 unsigned Opc;
4228 switch (Pattern) {
4229 default:
4230 // Reassociate instructions.
4231 TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs,
4232 DelInstrs, InstrIdxForVirtReg);
4233 return;
4234 case MachineCombinerPattern::MULADDW_OP1:
4235 case MachineCombinerPattern::MULADDX_OP1:
4236 // MUL I=A,B,0
4237 // ADD R,I,C
4238 // ==> MADD R,A,B,C
4239 // --- Create(MADD);
4240 if (Pattern == MachineCombinerPattern::MULADDW_OP1) {
4241 Opc = AArch64::MADDWrrr;
4242 RC = &AArch64::GPR32RegClass;
4243 } else {
4244 Opc = AArch64::MADDXrrr;
4245 RC = &AArch64::GPR64RegClass;
4247 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4248 break;
4249 case MachineCombinerPattern::MULADDW_OP2:
4250 case MachineCombinerPattern::MULADDX_OP2:
4251 // MUL I=A,B,0
4252 // ADD R,C,I
4253 // ==> MADD R,A,B,C
4254 // --- Create(MADD);
4255 if (Pattern == MachineCombinerPattern::MULADDW_OP2) {
4256 Opc = AArch64::MADDWrrr;
4257 RC = &AArch64::GPR32RegClass;
4258 } else {
4259 Opc = AArch64::MADDXrrr;
4260 RC = &AArch64::GPR64RegClass;
4262 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4263 break;
4264 case MachineCombinerPattern::MULADDWI_OP1:
4265 case MachineCombinerPattern::MULADDXI_OP1: {
4266 // MUL I=A,B,0
4267 // ADD R,I,Imm
4268 // ==> ORR V, ZR, Imm
4269 // ==> MADD R,A,B,V
4270 // --- Create(MADD);
4271 const TargetRegisterClass *OrrRC;
4272 unsigned BitSize, OrrOpc, ZeroReg;
4273 if (Pattern == MachineCombinerPattern::MULADDWI_OP1) {
4274 OrrOpc = AArch64::ORRWri;
4275 OrrRC = &AArch64::GPR32spRegClass;
4276 BitSize = 32;
4277 ZeroReg = AArch64::WZR;
4278 Opc = AArch64::MADDWrrr;
4279 RC = &AArch64::GPR32RegClass;
4280 } else {
4281 OrrOpc = AArch64::ORRXri;
4282 OrrRC = &AArch64::GPR64spRegClass;
4283 BitSize = 64;
4284 ZeroReg = AArch64::XZR;
4285 Opc = AArch64::MADDXrrr;
4286 RC = &AArch64::GPR64RegClass;
4288 Register NewVR = MRI.createVirtualRegister(OrrRC);
4289 uint64_t Imm = Root.getOperand(2).getImm();
4291 if (Root.getOperand(3).isImm()) {
4292 unsigned Val = Root.getOperand(3).getImm();
4293 Imm = Imm << Val;
4295 uint64_t UImm = SignExtend64(Imm, BitSize);
4296 uint64_t Encoding;
4297 if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
4298 MachineInstrBuilder MIB1 =
4299 BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
4300 .addReg(ZeroReg)
4301 .addImm(Encoding);
4302 InsInstrs.push_back(MIB1);
4303 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4304 MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4306 break;
4308 case MachineCombinerPattern::MULSUBW_OP1:
4309 case MachineCombinerPattern::MULSUBX_OP1: {
4310 // MUL I=A,B,0
4311 // SUB R,I, C
4312 // ==> SUB V, 0, C
4313 // ==> MADD R,A,B,V // = -C + A*B
4314 // --- Create(MADD);
4315 const TargetRegisterClass *SubRC;
4316 unsigned SubOpc, ZeroReg;
4317 if (Pattern == MachineCombinerPattern::MULSUBW_OP1) {
4318 SubOpc = AArch64::SUBWrr;
4319 SubRC = &AArch64::GPR32spRegClass;
4320 ZeroReg = AArch64::WZR;
4321 Opc = AArch64::MADDWrrr;
4322 RC = &AArch64::GPR32RegClass;
4323 } else {
4324 SubOpc = AArch64::SUBXrr;
4325 SubRC = &AArch64::GPR64spRegClass;
4326 ZeroReg = AArch64::XZR;
4327 Opc = AArch64::MADDXrrr;
4328 RC = &AArch64::GPR64RegClass;
4330 Register NewVR = MRI.createVirtualRegister(SubRC);
4331 // SUB NewVR, 0, C
4332 MachineInstrBuilder MIB1 =
4333 BuildMI(MF, Root.getDebugLoc(), TII->get(SubOpc), NewVR)
4334 .addReg(ZeroReg)
4335 .add(Root.getOperand(2));
4336 InsInstrs.push_back(MIB1);
4337 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4338 MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4339 break;
4341 case MachineCombinerPattern::MULSUBW_OP2:
4342 case MachineCombinerPattern::MULSUBX_OP2:
4343 // MUL I=A,B,0
4344 // SUB R,C,I
4345 // ==> MSUB R,A,B,C (computes C - A*B)
4346 // --- Create(MSUB);
4347 if (Pattern == MachineCombinerPattern::MULSUBW_OP2) {
4348 Opc = AArch64::MSUBWrrr;
4349 RC = &AArch64::GPR32RegClass;
4350 } else {
4351 Opc = AArch64::MSUBXrrr;
4352 RC = &AArch64::GPR64RegClass;
4354 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4355 break;
4356 case MachineCombinerPattern::MULSUBWI_OP1:
4357 case MachineCombinerPattern::MULSUBXI_OP1: {
4358 // MUL I=A,B,0
4359 // SUB R,I, Imm
4360 // ==> ORR V, ZR, -Imm
4361 // ==> MADD R,A,B,V // = -Imm + A*B
4362 // --- Create(MADD);
4363 const TargetRegisterClass *OrrRC;
4364 unsigned BitSize, OrrOpc, ZeroReg;
4365 if (Pattern == MachineCombinerPattern::MULSUBWI_OP1) {
4366 OrrOpc = AArch64::ORRWri;
4367 OrrRC = &AArch64::GPR32spRegClass;
4368 BitSize = 32;
4369 ZeroReg = AArch64::WZR;
4370 Opc = AArch64::MADDWrrr;
4371 RC = &AArch64::GPR32RegClass;
4372 } else {
4373 OrrOpc = AArch64::ORRXri;
4374 OrrRC = &AArch64::GPR64spRegClass;
4375 BitSize = 64;
4376 ZeroReg = AArch64::XZR;
4377 Opc = AArch64::MADDXrrr;
4378 RC = &AArch64::GPR64RegClass;
4380 Register NewVR = MRI.createVirtualRegister(OrrRC);
4381 uint64_t Imm = Root.getOperand(2).getImm();
4382 if (Root.getOperand(3).isImm()) {
4383 unsigned Val = Root.getOperand(3).getImm();
4384 Imm = Imm << Val;
4386 uint64_t UImm = SignExtend64(-Imm, BitSize);
4387 uint64_t Encoding;
4388 if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
4389 MachineInstrBuilder MIB1 =
4390 BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
4391 .addReg(ZeroReg)
4392 .addImm(Encoding);
4393 InsInstrs.push_back(MIB1);
4394 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4395 MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
4397 break;
4399 // Floating Point Support
4400 case MachineCombinerPattern::FMULADDH_OP1:
4401 Opc = AArch64::FMADDHrrr;
4402 RC = &AArch64::FPR16RegClass;
4403 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4404 break;
4405 case MachineCombinerPattern::FMULADDS_OP1:
4406 Opc = AArch64::FMADDSrrr;
4407 RC = &AArch64::FPR32RegClass;
4408 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4409 break;
4410 case MachineCombinerPattern::FMULADDD_OP1:
4411 Opc = AArch64::FMADDDrrr;
4412 RC = &AArch64::FPR64RegClass;
4413 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4414 break;
4416 case MachineCombinerPattern::FMULADDH_OP2:
4417 Opc = AArch64::FMADDHrrr;
4418 RC = &AArch64::FPR16RegClass;
4419 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4420 break;
4421 case MachineCombinerPattern::FMULADDS_OP2:
4422 Opc = AArch64::FMADDSrrr;
4423 RC = &AArch64::FPR32RegClass;
4424 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4425 break;
4426 case MachineCombinerPattern::FMULADDD_OP2:
4427 Opc = AArch64::FMADDDrrr;
4428 RC = &AArch64::FPR64RegClass;
4429 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4430 break;
4432 case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
4433 Opc = AArch64::FMLAv1i32_indexed;
4434 RC = &AArch64::FPR32RegClass;
4435 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4436 FMAInstKind::Indexed);
4437 break;
4438 case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
4439 Opc = AArch64::FMLAv1i32_indexed;
4440 RC = &AArch64::FPR32RegClass;
4441 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4442 FMAInstKind::Indexed);
4443 break;
4445 case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
4446 Opc = AArch64::FMLAv1i64_indexed;
4447 RC = &AArch64::FPR64RegClass;
4448 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4449 FMAInstKind::Indexed);
4450 break;
4451 case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
4452 Opc = AArch64::FMLAv1i64_indexed;
4453 RC = &AArch64::FPR64RegClass;
4454 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4455 FMAInstKind::Indexed);
4456 break;
4458 case MachineCombinerPattern::FMLAv4i16_indexed_OP1:
4459 RC = &AArch64::FPR64RegClass;
4460 Opc = AArch64::FMLAv4i16_indexed;
4461 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4462 FMAInstKind::Indexed);
4463 break;
4464 case MachineCombinerPattern::FMLAv4f16_OP1:
4465 RC = &AArch64::FPR64RegClass;
4466 Opc = AArch64::FMLAv4f16;
4467 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4468 FMAInstKind::Accumulator);
4469 break;
4470 case MachineCombinerPattern::FMLAv4i16_indexed_OP2:
4471 RC = &AArch64::FPR64RegClass;
4472 Opc = AArch64::FMLAv4i16_indexed;
4473 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4474 FMAInstKind::Indexed);
4475 break;
4476 case MachineCombinerPattern::FMLAv4f16_OP2:
4477 RC = &AArch64::FPR64RegClass;
4478 Opc = AArch64::FMLAv4f16;
4479 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4480 FMAInstKind::Accumulator);
4481 break;
4483 case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
4484 case MachineCombinerPattern::FMLAv2f32_OP1:
4485 RC = &AArch64::FPR64RegClass;
4486 if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP1) {
4487 Opc = AArch64::FMLAv2i32_indexed;
4488 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4489 FMAInstKind::Indexed);
4490 } else {
4491 Opc = AArch64::FMLAv2f32;
4492 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4493 FMAInstKind::Accumulator);
4495 break;
4496 case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
4497 case MachineCombinerPattern::FMLAv2f32_OP2:
4498 RC = &AArch64::FPR64RegClass;
4499 if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP2) {
4500 Opc = AArch64::FMLAv2i32_indexed;
4501 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4502 FMAInstKind::Indexed);
4503 } else {
4504 Opc = AArch64::FMLAv2f32;
4505 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4506 FMAInstKind::Accumulator);
4508 break;
4510 case MachineCombinerPattern::FMLAv8i16_indexed_OP1:
4511 RC = &AArch64::FPR128RegClass;
4512 Opc = AArch64::FMLAv8i16_indexed;
4513 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4514 FMAInstKind::Indexed);
4515 break;
4516 case MachineCombinerPattern::FMLAv8f16_OP1:
4517 RC = &AArch64::FPR128RegClass;
4518 Opc = AArch64::FMLAv8f16;
4519 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4520 FMAInstKind::Accumulator);
4521 break;
4522 case MachineCombinerPattern::FMLAv8i16_indexed_OP2:
4523 RC = &AArch64::FPR128RegClass;
4524 Opc = AArch64::FMLAv8i16_indexed;
4525 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4526 FMAInstKind::Indexed);
4527 break;
4528 case MachineCombinerPattern::FMLAv8f16_OP2:
4529 RC = &AArch64::FPR128RegClass;
4530 Opc = AArch64::FMLAv8f16;
4531 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4532 FMAInstKind::Accumulator);
4533 break;
4535 case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
4536 case MachineCombinerPattern::FMLAv2f64_OP1:
4537 RC = &AArch64::FPR128RegClass;
4538 if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP1) {
4539 Opc = AArch64::FMLAv2i64_indexed;
4540 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4541 FMAInstKind::Indexed);
4542 } else {
4543 Opc = AArch64::FMLAv2f64;
4544 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4545 FMAInstKind::Accumulator);
4547 break;
4548 case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
4549 case MachineCombinerPattern::FMLAv2f64_OP2:
4550 RC = &AArch64::FPR128RegClass;
4551 if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP2) {
4552 Opc = AArch64::FMLAv2i64_indexed;
4553 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4554 FMAInstKind::Indexed);
4555 } else {
4556 Opc = AArch64::FMLAv2f64;
4557 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4558 FMAInstKind::Accumulator);
4560 break;
4562 case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
4563 case MachineCombinerPattern::FMLAv4f32_OP1:
4564 RC = &AArch64::FPR128RegClass;
4565 if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP1) {
4566 Opc = AArch64::FMLAv4i32_indexed;
4567 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4568 FMAInstKind::Indexed);
4569 } else {
4570 Opc = AArch64::FMLAv4f32;
4571 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4572 FMAInstKind::Accumulator);
4574 break;
4576 case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
4577 case MachineCombinerPattern::FMLAv4f32_OP2:
4578 RC = &AArch64::FPR128RegClass;
4579 if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP2) {
4580 Opc = AArch64::FMLAv4i32_indexed;
4581 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4582 FMAInstKind::Indexed);
4583 } else {
4584 Opc = AArch64::FMLAv4f32;
4585 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4586 FMAInstKind::Accumulator);
4588 break;
4590 case MachineCombinerPattern::FMULSUBH_OP1:
4591 Opc = AArch64::FNMSUBHrrr;
4592 RC = &AArch64::FPR16RegClass;
4593 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4594 break;
4595 case MachineCombinerPattern::FMULSUBS_OP1:
4596 Opc = AArch64::FNMSUBSrrr;
4597 RC = &AArch64::FPR32RegClass;
4598 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4599 break;
4600 case MachineCombinerPattern::FMULSUBD_OP1:
4601 Opc = AArch64::FNMSUBDrrr;
4602 RC = &AArch64::FPR64RegClass;
4603 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4604 break;
4606 case MachineCombinerPattern::FNMULSUBH_OP1:
4607 Opc = AArch64::FNMADDHrrr;
4608 RC = &AArch64::FPR16RegClass;
4609 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4610 break;
4611 case MachineCombinerPattern::FNMULSUBS_OP1:
4612 Opc = AArch64::FNMADDSrrr;
4613 RC = &AArch64::FPR32RegClass;
4614 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4615 break;
4616 case MachineCombinerPattern::FNMULSUBD_OP1:
4617 Opc = AArch64::FNMADDDrrr;
4618 RC = &AArch64::FPR64RegClass;
4619 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
4620 break;
4622 case MachineCombinerPattern::FMULSUBH_OP2:
4623 Opc = AArch64::FMSUBHrrr;
4624 RC = &AArch64::FPR16RegClass;
4625 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4626 break;
4627 case MachineCombinerPattern::FMULSUBS_OP2:
4628 Opc = AArch64::FMSUBSrrr;
4629 RC = &AArch64::FPR32RegClass;
4630 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4631 break;
4632 case MachineCombinerPattern::FMULSUBD_OP2:
4633 Opc = AArch64::FMSUBDrrr;
4634 RC = &AArch64::FPR64RegClass;
4635 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
4636 break;
4638 case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
4639 Opc = AArch64::FMLSv1i32_indexed;
4640 RC = &AArch64::FPR32RegClass;
4641 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4642 FMAInstKind::Indexed);
4643 break;
4645 case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
4646 Opc = AArch64::FMLSv1i64_indexed;
4647 RC = &AArch64::FPR64RegClass;
4648 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4649 FMAInstKind::Indexed);
4650 break;
4652 case MachineCombinerPattern::FMLSv4f16_OP2:
4653 RC = &AArch64::FPR64RegClass;
4654 Opc = AArch64::FMLSv4f16;
4655 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4656 FMAInstKind::Accumulator);
4657 break;
4658 case MachineCombinerPattern::FMLSv4i16_indexed_OP2:
4659 RC = &AArch64::FPR64RegClass;
4660 Opc = AArch64::FMLSv4i16_indexed;
4661 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4662 FMAInstKind::Indexed);
4663 break;
4665 case MachineCombinerPattern::FMLSv2f32_OP2:
4666 case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
4667 RC = &AArch64::FPR64RegClass;
4668 if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP2) {
4669 Opc = AArch64::FMLSv2i32_indexed;
4670 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4671 FMAInstKind::Indexed);
4672 } else {
4673 Opc = AArch64::FMLSv2f32;
4674 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4675 FMAInstKind::Accumulator);
4677 break;
4679 case MachineCombinerPattern::FMLSv8f16_OP1:
4680 RC = &AArch64::FPR128RegClass;
4681 Opc = AArch64::FMLSv8f16;
4682 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4683 FMAInstKind::Accumulator);
4684 break;
4685 case MachineCombinerPattern::FMLSv8i16_indexed_OP1:
4686 RC = &AArch64::FPR128RegClass;
4687 Opc = AArch64::FMLSv8i16_indexed;
4688 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4689 FMAInstKind::Indexed);
4690 break;
4692 case MachineCombinerPattern::FMLSv8f16_OP2:
4693 RC = &AArch64::FPR128RegClass;
4694 Opc = AArch64::FMLSv8f16;
4695 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4696 FMAInstKind::Accumulator);
4697 break;
4698 case MachineCombinerPattern::FMLSv8i16_indexed_OP2:
4699 RC = &AArch64::FPR128RegClass;
4700 Opc = AArch64::FMLSv8i16_indexed;
4701 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4702 FMAInstKind::Indexed);
4703 break;
4705 case MachineCombinerPattern::FMLSv2f64_OP2:
4706 case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
4707 RC = &AArch64::FPR128RegClass;
4708 if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP2) {
4709 Opc = AArch64::FMLSv2i64_indexed;
4710 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4711 FMAInstKind::Indexed);
4712 } else {
4713 Opc = AArch64::FMLSv2f64;
4714 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4715 FMAInstKind::Accumulator);
4717 break;
4719 case MachineCombinerPattern::FMLSv4f32_OP2:
4720 case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
4721 RC = &AArch64::FPR128RegClass;
4722 if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP2) {
4723 Opc = AArch64::FMLSv4i32_indexed;
4724 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4725 FMAInstKind::Indexed);
4726 } else {
4727 Opc = AArch64::FMLSv4f32;
4728 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
4729 FMAInstKind::Accumulator);
4731 break;
4732 case MachineCombinerPattern::FMLSv2f32_OP1:
4733 case MachineCombinerPattern::FMLSv2i32_indexed_OP1: {
4734 RC = &AArch64::FPR64RegClass;
4735 Register NewVR = MRI.createVirtualRegister(RC);
4736 MachineInstrBuilder MIB1 =
4737 BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f32), NewVR)
4738 .add(Root.getOperand(2));
4739 InsInstrs.push_back(MIB1);
4740 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4741 if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP1) {
4742 Opc = AArch64::FMLAv2i32_indexed;
4743 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4744 FMAInstKind::Indexed, &NewVR);
4745 } else {
4746 Opc = AArch64::FMLAv2f32;
4747 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4748 FMAInstKind::Accumulator, &NewVR);
4750 break;
4752 case MachineCombinerPattern::FMLSv4f32_OP1:
4753 case MachineCombinerPattern::FMLSv4i32_indexed_OP1: {
4754 RC = &AArch64::FPR128RegClass;
4755 Register NewVR = MRI.createVirtualRegister(RC);
4756 MachineInstrBuilder MIB1 =
4757 BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv4f32), NewVR)
4758 .add(Root.getOperand(2));
4759 InsInstrs.push_back(MIB1);
4760 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4761 if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP1) {
4762 Opc = AArch64::FMLAv4i32_indexed;
4763 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4764 FMAInstKind::Indexed, &NewVR);
4765 } else {
4766 Opc = AArch64::FMLAv4f32;
4767 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4768 FMAInstKind::Accumulator, &NewVR);
4770 break;
4772 case MachineCombinerPattern::FMLSv2f64_OP1:
4773 case MachineCombinerPattern::FMLSv2i64_indexed_OP1: {
4774 RC = &AArch64::FPR128RegClass;
4775 Register NewVR = MRI.createVirtualRegister(RC);
4776 MachineInstrBuilder MIB1 =
4777 BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f64), NewVR)
4778 .add(Root.getOperand(2));
4779 InsInstrs.push_back(MIB1);
4780 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
4781 if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP1) {
4782 Opc = AArch64::FMLAv2i64_indexed;
4783 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4784 FMAInstKind::Indexed, &NewVR);
4785 } else {
4786 Opc = AArch64::FMLAv2f64;
4787 MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
4788 FMAInstKind::Accumulator, &NewVR);
4790 break;
4792 } // end switch (Pattern)
4793 // Record MUL and ADD/SUB for deletion
4794 DelInstrs.push_back(MUL);
4795 DelInstrs.push_back(&Root);
4798 /// Replace csincr-branch sequence by simple conditional branch
4800 /// Examples:
4801 /// 1. \code
4802 /// csinc w9, wzr, wzr, <condition code>
4803 /// tbnz w9, #0, 0x44
4804 /// \endcode
4805 /// to
4806 /// \code
4807 /// b.<inverted condition code>
4808 /// \endcode
4810 /// 2. \code
4811 /// csinc w9, wzr, wzr, <condition code>
4812 /// tbz w9, #0, 0x44
4813 /// \endcode
4814 /// to
4815 /// \code
4816 /// b.<condition code>
4817 /// \endcode
4819 /// Replace compare and branch sequence by TBZ/TBNZ instruction when the
4820 /// compare's constant operand is power of 2.
4822 /// Examples:
4823 /// \code
4824 /// and w8, w8, #0x400
4825 /// cbnz w8, L1
4826 /// \endcode
4827 /// to
4828 /// \code
4829 /// tbnz w8, #10, L1
4830 /// \endcode
4832 /// \param MI Conditional Branch
4833 /// \return True when the simple conditional branch is generated
4835 bool AArch64InstrInfo::optimizeCondBranch(MachineInstr &MI) const {
4836 bool IsNegativeBranch = false;
4837 bool IsTestAndBranch = false;
4838 unsigned TargetBBInMI = 0;
4839 switch (MI.getOpcode()) {
4840 default:
4841 llvm_unreachable("Unknown branch instruction?");
4842 case AArch64::Bcc:
4843 return false;
4844 case AArch64::CBZW:
4845 case AArch64::CBZX:
4846 TargetBBInMI = 1;
4847 break;
4848 case AArch64::CBNZW:
4849 case AArch64::CBNZX:
4850 TargetBBInMI = 1;
4851 IsNegativeBranch = true;
4852 break;
4853 case AArch64::TBZW:
4854 case AArch64::TBZX:
4855 TargetBBInMI = 2;
4856 IsTestAndBranch = true;
4857 break;
4858 case AArch64::TBNZW:
4859 case AArch64::TBNZX:
4860 TargetBBInMI = 2;
4861 IsNegativeBranch = true;
4862 IsTestAndBranch = true;
4863 break;
4865 // So we increment a zero register and test for bits other
4866 // than bit 0? Conservatively bail out in case the verifier
4867 // missed this case.
4868 if (IsTestAndBranch && MI.getOperand(1).getImm())
4869 return false;
4871 // Find Definition.
4872 assert(MI.getParent() && "Incomplete machine instruciton\n");
4873 MachineBasicBlock *MBB = MI.getParent();
4874 MachineFunction *MF = MBB->getParent();
4875 MachineRegisterInfo *MRI = &MF->getRegInfo();
4876 Register VReg = MI.getOperand(0).getReg();
4877 if (!Register::isVirtualRegister(VReg))
4878 return false;
4880 MachineInstr *DefMI = MRI->getVRegDef(VReg);
4882 // Look through COPY instructions to find definition.
4883 while (DefMI->isCopy()) {
4884 Register CopyVReg = DefMI->getOperand(1).getReg();
4885 if (!MRI->hasOneNonDBGUse(CopyVReg))
4886 return false;
4887 if (!MRI->hasOneDef(CopyVReg))
4888 return false;
4889 DefMI = MRI->getVRegDef(CopyVReg);
4892 switch (DefMI->getOpcode()) {
4893 default:
4894 return false;
4895 // Fold AND into a TBZ/TBNZ if constant operand is power of 2.
4896 case AArch64::ANDWri:
4897 case AArch64::ANDXri: {
4898 if (IsTestAndBranch)
4899 return false;
4900 if (DefMI->getParent() != MBB)
4901 return false;
4902 if (!MRI->hasOneNonDBGUse(VReg))
4903 return false;
4905 bool Is32Bit = (DefMI->getOpcode() == AArch64::ANDWri);
4906 uint64_t Mask = AArch64_AM::decodeLogicalImmediate(
4907 DefMI->getOperand(2).getImm(), Is32Bit ? 32 : 64);
4908 if (!isPowerOf2_64(Mask))
4909 return false;
4911 MachineOperand &MO = DefMI->getOperand(1);
4912 Register NewReg = MO.getReg();
4913 if (!Register::isVirtualRegister(NewReg))
4914 return false;
4916 assert(!MRI->def_empty(NewReg) && "Register must be defined.");
4918 MachineBasicBlock &RefToMBB = *MBB;
4919 MachineBasicBlock *TBB = MI.getOperand(1).getMBB();
4920 DebugLoc DL = MI.getDebugLoc();
4921 unsigned Imm = Log2_64(Mask);
4922 unsigned Opc = (Imm < 32)
4923 ? (IsNegativeBranch ? AArch64::TBNZW : AArch64::TBZW)
4924 : (IsNegativeBranch ? AArch64::TBNZX : AArch64::TBZX);
4925 MachineInstr *NewMI = BuildMI(RefToMBB, MI, DL, get(Opc))
4926 .addReg(NewReg)
4927 .addImm(Imm)
4928 .addMBB(TBB);
4929 // Register lives on to the CBZ now.
4930 MO.setIsKill(false);
4932 // For immediate smaller than 32, we need to use the 32-bit
4933 // variant (W) in all cases. Indeed the 64-bit variant does not
4934 // allow to encode them.
4935 // Therefore, if the input register is 64-bit, we need to take the
4936 // 32-bit sub-part.
4937 if (!Is32Bit && Imm < 32)
4938 NewMI->getOperand(0).setSubReg(AArch64::sub_32);
4939 MI.eraseFromParent();
4940 return true;
4942 // Look for CSINC
4943 case AArch64::CSINCWr:
4944 case AArch64::CSINCXr: {
4945 if (!(DefMI->getOperand(1).getReg() == AArch64::WZR &&
4946 DefMI->getOperand(2).getReg() == AArch64::WZR) &&
4947 !(DefMI->getOperand(1).getReg() == AArch64::XZR &&
4948 DefMI->getOperand(2).getReg() == AArch64::XZR))
4949 return false;
4951 if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) != -1)
4952 return false;
4954 AArch64CC::CondCode CC = (AArch64CC::CondCode)DefMI->getOperand(3).getImm();
4955 // Convert only when the condition code is not modified between
4956 // the CSINC and the branch. The CC may be used by other
4957 // instructions in between.
4958 if (areCFlagsAccessedBetweenInstrs(DefMI, MI, &getRegisterInfo(), AK_Write))
4959 return false;
4960 MachineBasicBlock &RefToMBB = *MBB;
4961 MachineBasicBlock *TBB = MI.getOperand(TargetBBInMI).getMBB();
4962 DebugLoc DL = MI.getDebugLoc();
4963 if (IsNegativeBranch)
4964 CC = AArch64CC::getInvertedCondCode(CC);
4965 BuildMI(RefToMBB, MI, DL, get(AArch64::Bcc)).addImm(CC).addMBB(TBB);
4966 MI.eraseFromParent();
4967 return true;
4972 std::pair<unsigned, unsigned>
4973 AArch64InstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
4974 const unsigned Mask = AArch64II::MO_FRAGMENT;
4975 return std::make_pair(TF & Mask, TF & ~Mask);
4978 ArrayRef<std::pair<unsigned, const char *>>
4979 AArch64InstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
4980 using namespace AArch64II;
4982 static const std::pair<unsigned, const char *> TargetFlags[] = {
4983 {MO_PAGE, "aarch64-page"}, {MO_PAGEOFF, "aarch64-pageoff"},
4984 {MO_G3, "aarch64-g3"}, {MO_G2, "aarch64-g2"},
4985 {MO_G1, "aarch64-g1"}, {MO_G0, "aarch64-g0"},
4986 {MO_HI12, "aarch64-hi12"}};
4987 return makeArrayRef(TargetFlags);
4990 ArrayRef<std::pair<unsigned, const char *>>
4991 AArch64InstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
4992 using namespace AArch64II;
4994 static const std::pair<unsigned, const char *> TargetFlags[] = {
4995 {MO_COFFSTUB, "aarch64-coffstub"},
4996 {MO_GOT, "aarch64-got"},
4997 {MO_NC, "aarch64-nc"},
4998 {MO_S, "aarch64-s"},
4999 {MO_TLS, "aarch64-tls"},
5000 {MO_DLLIMPORT, "aarch64-dllimport"},
5001 {MO_PREL, "aarch64-prel"},
5002 {MO_TAGGED, "aarch64-tagged"}};
5003 return makeArrayRef(TargetFlags);
5006 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
5007 AArch64InstrInfo::getSerializableMachineMemOperandTargetFlags() const {
5008 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
5009 {{MOSuppressPair, "aarch64-suppress-pair"},
5010 {MOStridedAccess, "aarch64-strided-access"}};
5011 return makeArrayRef(TargetFlags);
5014 /// Constants defining how certain sequences should be outlined.
5015 /// This encompasses how an outlined function should be called, and what kind of
5016 /// frame should be emitted for that outlined function.
5018 /// \p MachineOutlinerDefault implies that the function should be called with
5019 /// a save and restore of LR to the stack.
5021 /// That is,
5023 /// I1 Save LR OUTLINED_FUNCTION:
5024 /// I2 --> BL OUTLINED_FUNCTION I1
5025 /// I3 Restore LR I2
5026 /// I3
5027 /// RET
5029 /// * Call construction overhead: 3 (save + BL + restore)
5030 /// * Frame construction overhead: 1 (ret)
5031 /// * Requires stack fixups? Yes
5033 /// \p MachineOutlinerTailCall implies that the function is being created from
5034 /// a sequence of instructions ending in a return.
5036 /// That is,
5038 /// I1 OUTLINED_FUNCTION:
5039 /// I2 --> B OUTLINED_FUNCTION I1
5040 /// RET I2
5041 /// RET
5043 /// * Call construction overhead: 1 (B)
5044 /// * Frame construction overhead: 0 (Return included in sequence)
5045 /// * Requires stack fixups? No
5047 /// \p MachineOutlinerNoLRSave implies that the function should be called using
5048 /// a BL instruction, but doesn't require LR to be saved and restored. This
5049 /// happens when LR is known to be dead.
5051 /// That is,
5053 /// I1 OUTLINED_FUNCTION:
5054 /// I2 --> BL OUTLINED_FUNCTION I1
5055 /// I3 I2
5056 /// I3
5057 /// RET
5059 /// * Call construction overhead: 1 (BL)
5060 /// * Frame construction overhead: 1 (RET)
5061 /// * Requires stack fixups? No
5063 /// \p MachineOutlinerThunk implies that the function is being created from
5064 /// a sequence of instructions ending in a call. The outlined function is
5065 /// called with a BL instruction, and the outlined function tail-calls the
5066 /// original call destination.
5068 /// That is,
5070 /// I1 OUTLINED_FUNCTION:
5071 /// I2 --> BL OUTLINED_FUNCTION I1
5072 /// BL f I2
5073 /// B f
5074 /// * Call construction overhead: 1 (BL)
5075 /// * Frame construction overhead: 0
5076 /// * Requires stack fixups? No
5078 /// \p MachineOutlinerRegSave implies that the function should be called with a
5079 /// save and restore of LR to an available register. This allows us to avoid
5080 /// stack fixups. Note that this outlining variant is compatible with the
5081 /// NoLRSave case.
5083 /// That is,
5085 /// I1 Save LR OUTLINED_FUNCTION:
5086 /// I2 --> BL OUTLINED_FUNCTION I1
5087 /// I3 Restore LR I2
5088 /// I3
5089 /// RET
5091 /// * Call construction overhead: 3 (save + BL + restore)
5092 /// * Frame construction overhead: 1 (ret)
5093 /// * Requires stack fixups? No
5094 enum MachineOutlinerClass {
5095 MachineOutlinerDefault, /// Emit a save, restore, call, and return.
5096 MachineOutlinerTailCall, /// Only emit a branch.
5097 MachineOutlinerNoLRSave, /// Emit a call and return.
5098 MachineOutlinerThunk, /// Emit a call and tail-call.
5099 MachineOutlinerRegSave /// Same as default, but save to a register.
5102 enum MachineOutlinerMBBFlags {
5103 LRUnavailableSomewhere = 0x2,
5104 HasCalls = 0x4,
5105 UnsafeRegsDead = 0x8
5108 unsigned
5109 AArch64InstrInfo::findRegisterToSaveLRTo(const outliner::Candidate &C) const {
5110 assert(C.LRUWasSet && "LRU wasn't set?");
5111 MachineFunction *MF = C.getMF();
5112 const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
5113 MF->getSubtarget().getRegisterInfo());
5115 // Check if there is an available register across the sequence that we can
5116 // use.
5117 for (unsigned Reg : AArch64::GPR64RegClass) {
5118 if (!ARI->isReservedReg(*MF, Reg) &&
5119 Reg != AArch64::LR && // LR is not reserved, but don't use it.
5120 Reg != AArch64::X16 && // X16 is not guaranteed to be preserved.
5121 Reg != AArch64::X17 && // Ditto for X17.
5122 C.LRU.available(Reg) && C.UsedInSequence.available(Reg))
5123 return Reg;
5126 // No suitable register. Return 0.
5127 return 0u;
5130 outliner::OutlinedFunction
5131 AArch64InstrInfo::getOutliningCandidateInfo(
5132 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
5133 outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
5134 unsigned SequenceSize =
5135 std::accumulate(FirstCand.front(), std::next(FirstCand.back()), 0,
5136 [this](unsigned Sum, const MachineInstr &MI) {
5137 return Sum + getInstSizeInBytes(MI);
5140 // Properties about candidate MBBs that hold for all of them.
5141 unsigned FlagsSetInAll = 0xF;
5143 // Compute liveness information for each candidate, and set FlagsSetInAll.
5144 const TargetRegisterInfo &TRI = getRegisterInfo();
5145 std::for_each(RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(),
5146 [&FlagsSetInAll](outliner::Candidate &C) {
5147 FlagsSetInAll &= C.Flags;
5150 // According to the AArch64 Procedure Call Standard, the following are
5151 // undefined on entry/exit from a function call:
5153 // * Registers x16, x17, (and thus w16, w17)
5154 // * Condition codes (and thus the NZCV register)
5156 // Because if this, we can't outline any sequence of instructions where
5157 // one
5158 // of these registers is live into/across it. Thus, we need to delete
5159 // those
5160 // candidates.
5161 auto CantGuaranteeValueAcrossCall = [&TRI](outliner::Candidate &C) {
5162 // If the unsafe registers in this block are all dead, then we don't need
5163 // to compute liveness here.
5164 if (C.Flags & UnsafeRegsDead)
5165 return false;
5166 C.initLRU(TRI);
5167 LiveRegUnits LRU = C.LRU;
5168 return (!LRU.available(AArch64::W16) || !LRU.available(AArch64::W17) ||
5169 !LRU.available(AArch64::NZCV));
5172 // Are there any candidates where those registers are live?
5173 if (!(FlagsSetInAll & UnsafeRegsDead)) {
5174 // Erase every candidate that violates the restrictions above. (It could be
5175 // true that we have viable candidates, so it's not worth bailing out in
5176 // the case that, say, 1 out of 20 candidates violate the restructions.)
5177 RepeatedSequenceLocs.erase(std::remove_if(RepeatedSequenceLocs.begin(),
5178 RepeatedSequenceLocs.end(),
5179 CantGuaranteeValueAcrossCall),
5180 RepeatedSequenceLocs.end());
5182 // If the sequence doesn't have enough candidates left, then we're done.
5183 if (RepeatedSequenceLocs.size() < 2)
5184 return outliner::OutlinedFunction();
5187 // At this point, we have only "safe" candidates to outline. Figure out
5188 // frame + call instruction information.
5190 unsigned LastInstrOpcode = RepeatedSequenceLocs[0].back()->getOpcode();
5192 // Helper lambda which sets call information for every candidate.
5193 auto SetCandidateCallInfo =
5194 [&RepeatedSequenceLocs](unsigned CallID, unsigned NumBytesForCall) {
5195 for (outliner::Candidate &C : RepeatedSequenceLocs)
5196 C.setCallInfo(CallID, NumBytesForCall);
5199 unsigned FrameID = MachineOutlinerDefault;
5200 unsigned NumBytesToCreateFrame = 4;
5202 bool HasBTI = any_of(RepeatedSequenceLocs, [](outliner::Candidate &C) {
5203 return C.getMF()->getFunction().hasFnAttribute("branch-target-enforcement");
5206 // Returns true if an instructions is safe to fix up, false otherwise.
5207 auto IsSafeToFixup = [this, &TRI](MachineInstr &MI) {
5208 if (MI.isCall())
5209 return true;
5211 if (!MI.modifiesRegister(AArch64::SP, &TRI) &&
5212 !MI.readsRegister(AArch64::SP, &TRI))
5213 return true;
5215 // Any modification of SP will break our code to save/restore LR.
5216 // FIXME: We could handle some instructions which add a constant
5217 // offset to SP, with a bit more work.
5218 if (MI.modifiesRegister(AArch64::SP, &TRI))
5219 return false;
5221 // At this point, we have a stack instruction that we might need to
5222 // fix up. We'll handle it if it's a load or store.
5223 if (MI.mayLoadOrStore()) {
5224 const MachineOperand *Base; // Filled with the base operand of MI.
5225 int64_t Offset; // Filled with the offset of MI.
5227 // Does it allow us to offset the base operand and is the base the
5228 // register SP?
5229 if (!getMemOperandWithOffset(MI, Base, Offset, &TRI) || !Base->isReg() ||
5230 Base->getReg() != AArch64::SP)
5231 return false;
5233 // Find the minimum/maximum offset for this instruction and check
5234 // if fixing it up would be in range.
5235 int64_t MinOffset,
5236 MaxOffset; // Unscaled offsets for the instruction.
5237 unsigned Scale; // The scale to multiply the offsets by.
5238 unsigned DummyWidth;
5239 getMemOpInfo(MI.getOpcode(), Scale, DummyWidth, MinOffset, MaxOffset);
5241 Offset += 16; // Update the offset to what it would be if we outlined.
5242 if (Offset < MinOffset * Scale || Offset > MaxOffset * Scale)
5243 return false;
5245 // It's in range, so we can outline it.
5246 return true;
5249 // FIXME: Add handling for instructions like "add x0, sp, #8".
5251 // We can't fix it up, so don't outline it.
5252 return false;
5255 // True if it's possible to fix up each stack instruction in this sequence.
5256 // Important for frames/call variants that modify the stack.
5257 bool AllStackInstrsSafe = std::all_of(
5258 FirstCand.front(), std::next(FirstCand.back()), IsSafeToFixup);
5260 // If the last instruction in any candidate is a terminator, then we should
5261 // tail call all of the candidates.
5262 if (RepeatedSequenceLocs[0].back()->isTerminator()) {
5263 FrameID = MachineOutlinerTailCall;
5264 NumBytesToCreateFrame = 0;
5265 SetCandidateCallInfo(MachineOutlinerTailCall, 4);
5268 else if (LastInstrOpcode == AArch64::BL ||
5269 (LastInstrOpcode == AArch64::BLR && !HasBTI)) {
5270 // FIXME: Do we need to check if the code after this uses the value of LR?
5271 FrameID = MachineOutlinerThunk;
5272 NumBytesToCreateFrame = 0;
5273 SetCandidateCallInfo(MachineOutlinerThunk, 4);
5276 else {
5277 // We need to decide how to emit calls + frames. We can always emit the same
5278 // frame if we don't need to save to the stack. If we have to save to the
5279 // stack, then we need a different frame.
5280 unsigned NumBytesNoStackCalls = 0;
5281 std::vector<outliner::Candidate> CandidatesWithoutStackFixups;
5283 for (outliner::Candidate &C : RepeatedSequenceLocs) {
5284 C.initLRU(TRI);
5286 // Is LR available? If so, we don't need a save.
5287 if (C.LRU.available(AArch64::LR)) {
5288 NumBytesNoStackCalls += 4;
5289 C.setCallInfo(MachineOutlinerNoLRSave, 4);
5290 CandidatesWithoutStackFixups.push_back(C);
5293 // Is an unused register available? If so, we won't modify the stack, so
5294 // we can outline with the same frame type as those that don't save LR.
5295 else if (findRegisterToSaveLRTo(C)) {
5296 NumBytesNoStackCalls += 12;
5297 C.setCallInfo(MachineOutlinerRegSave, 12);
5298 CandidatesWithoutStackFixups.push_back(C);
5301 // Is SP used in the sequence at all? If not, we don't have to modify
5302 // the stack, so we are guaranteed to get the same frame.
5303 else if (C.UsedInSequence.available(AArch64::SP)) {
5304 NumBytesNoStackCalls += 12;
5305 C.setCallInfo(MachineOutlinerDefault, 12);
5306 CandidatesWithoutStackFixups.push_back(C);
5309 // If we outline this, we need to modify the stack. Pretend we don't
5310 // outline this by saving all of its bytes.
5311 else {
5312 NumBytesNoStackCalls += SequenceSize;
5316 // If there are no places where we have to save LR, then note that we
5317 // don't have to update the stack. Otherwise, give every candidate the
5318 // default call type, as long as it's safe to do so.
5319 if (!AllStackInstrsSafe ||
5320 NumBytesNoStackCalls <= RepeatedSequenceLocs.size() * 12) {
5321 RepeatedSequenceLocs = CandidatesWithoutStackFixups;
5322 FrameID = MachineOutlinerNoLRSave;
5323 } else {
5324 SetCandidateCallInfo(MachineOutlinerDefault, 12);
5327 // If we dropped all of the candidates, bail out here.
5328 if (RepeatedSequenceLocs.size() < 2) {
5329 RepeatedSequenceLocs.clear();
5330 return outliner::OutlinedFunction();
5334 // Does every candidate's MBB contain a call? If so, then we might have a call
5335 // in the range.
5336 if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
5337 // Check if the range contains a call. These require a save + restore of the
5338 // link register.
5339 bool ModStackToSaveLR = false;
5340 if (std::any_of(FirstCand.front(), FirstCand.back(),
5341 [](const MachineInstr &MI) { return MI.isCall(); }))
5342 ModStackToSaveLR = true;
5344 // Handle the last instruction separately. If this is a tail call, then the
5345 // last instruction is a call. We don't want to save + restore in this case.
5346 // However, it could be possible that the last instruction is a call without
5347 // it being valid to tail call this sequence. We should consider this as
5348 // well.
5349 else if (FrameID != MachineOutlinerThunk &&
5350 FrameID != MachineOutlinerTailCall && FirstCand.back()->isCall())
5351 ModStackToSaveLR = true;
5353 if (ModStackToSaveLR) {
5354 // We can't fix up the stack. Bail out.
5355 if (!AllStackInstrsSafe) {
5356 RepeatedSequenceLocs.clear();
5357 return outliner::OutlinedFunction();
5360 // Save + restore LR.
5361 NumBytesToCreateFrame += 8;
5365 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
5366 NumBytesToCreateFrame, FrameID);
5369 bool AArch64InstrInfo::isFunctionSafeToOutlineFrom(
5370 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
5371 const Function &F = MF.getFunction();
5373 // Can F be deduplicated by the linker? If it can, don't outline from it.
5374 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
5375 return false;
5377 // Don't outline from functions with section markings; the program could
5378 // expect that all the code is in the named section.
5379 // FIXME: Allow outlining from multiple functions with the same section
5380 // marking.
5381 if (F.hasSection())
5382 return false;
5384 // Outlining from functions with redzones is unsafe since the outliner may
5385 // modify the stack. Check if hasRedZone is true or unknown; if yes, don't
5386 // outline from it.
5387 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
5388 if (!AFI || AFI->hasRedZone().getValueOr(true))
5389 return false;
5391 // It's safe to outline from MF.
5392 return true;
5395 bool AArch64InstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
5396 unsigned &Flags) const {
5397 // Check if LR is available through all of the MBB. If it's not, then set
5398 // a flag.
5399 assert(MBB.getParent()->getRegInfo().tracksLiveness() &&
5400 "Suitable Machine Function for outlining must track liveness");
5401 LiveRegUnits LRU(getRegisterInfo());
5403 std::for_each(MBB.rbegin(), MBB.rend(),
5404 [&LRU](MachineInstr &MI) { LRU.accumulate(MI); });
5406 // Check if each of the unsafe registers are available...
5407 bool W16AvailableInBlock = LRU.available(AArch64::W16);
5408 bool W17AvailableInBlock = LRU.available(AArch64::W17);
5409 bool NZCVAvailableInBlock = LRU.available(AArch64::NZCV);
5411 // If all of these are dead (and not live out), we know we don't have to check
5412 // them later.
5413 if (W16AvailableInBlock && W17AvailableInBlock && NZCVAvailableInBlock)
5414 Flags |= MachineOutlinerMBBFlags::UnsafeRegsDead;
5416 // Now, add the live outs to the set.
5417 LRU.addLiveOuts(MBB);
5419 // If any of these registers is available in the MBB, but also a live out of
5420 // the block, then we know outlining is unsafe.
5421 if (W16AvailableInBlock && !LRU.available(AArch64::W16))
5422 return false;
5423 if (W17AvailableInBlock && !LRU.available(AArch64::W17))
5424 return false;
5425 if (NZCVAvailableInBlock && !LRU.available(AArch64::NZCV))
5426 return false;
5428 // Check if there's a call inside this MachineBasicBlock. If there is, then
5429 // set a flag.
5430 if (any_of(MBB, [](MachineInstr &MI) { return MI.isCall(); }))
5431 Flags |= MachineOutlinerMBBFlags::HasCalls;
5433 MachineFunction *MF = MBB.getParent();
5435 // In the event that we outline, we may have to save LR. If there is an
5436 // available register in the MBB, then we'll always save LR there. Check if
5437 // this is true.
5438 bool CanSaveLR = false;
5439 const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
5440 MF->getSubtarget().getRegisterInfo());
5442 // Check if there is an available register across the sequence that we can
5443 // use.
5444 for (unsigned Reg : AArch64::GPR64RegClass) {
5445 if (!ARI->isReservedReg(*MF, Reg) && Reg != AArch64::LR &&
5446 Reg != AArch64::X16 && Reg != AArch64::X17 && LRU.available(Reg)) {
5447 CanSaveLR = true;
5448 break;
5452 // Check if we have a register we can save LR to, and if LR was used
5453 // somewhere. If both of those things are true, then we need to evaluate the
5454 // safety of outlining stack instructions later.
5455 if (!CanSaveLR && !LRU.available(AArch64::LR))
5456 Flags |= MachineOutlinerMBBFlags::LRUnavailableSomewhere;
5458 return true;
5461 outliner::InstrType
5462 AArch64InstrInfo::getOutliningType(MachineBasicBlock::iterator &MIT,
5463 unsigned Flags) const {
5464 MachineInstr &MI = *MIT;
5465 MachineBasicBlock *MBB = MI.getParent();
5466 MachineFunction *MF = MBB->getParent();
5467 AArch64FunctionInfo *FuncInfo = MF->getInfo<AArch64FunctionInfo>();
5469 // Don't outline LOHs.
5470 if (FuncInfo->getLOHRelated().count(&MI))
5471 return outliner::InstrType::Illegal;
5473 // Don't allow debug values to impact outlining type.
5474 if (MI.isDebugInstr() || MI.isIndirectDebugValue())
5475 return outliner::InstrType::Invisible;
5477 // At this point, KILL instructions don't really tell us much so we can go
5478 // ahead and skip over them.
5479 if (MI.isKill())
5480 return outliner::InstrType::Invisible;
5482 // Is this a terminator for a basic block?
5483 if (MI.isTerminator()) {
5485 // Is this the end of a function?
5486 if (MI.getParent()->succ_empty())
5487 return outliner::InstrType::Legal;
5489 // It's not, so don't outline it.
5490 return outliner::InstrType::Illegal;
5493 // Make sure none of the operands are un-outlinable.
5494 for (const MachineOperand &MOP : MI.operands()) {
5495 if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() ||
5496 MOP.isTargetIndex())
5497 return outliner::InstrType::Illegal;
5499 // If it uses LR or W30 explicitly, then don't touch it.
5500 if (MOP.isReg() && !MOP.isImplicit() &&
5501 (MOP.getReg() == AArch64::LR || MOP.getReg() == AArch64::W30))
5502 return outliner::InstrType::Illegal;
5505 // Special cases for instructions that can always be outlined, but will fail
5506 // the later tests. e.g, ADRPs, which are PC-relative use LR, but can always
5507 // be outlined because they don't require a *specific* value to be in LR.
5508 if (MI.getOpcode() == AArch64::ADRP)
5509 return outliner::InstrType::Legal;
5511 // If MI is a call we might be able to outline it. We don't want to outline
5512 // any calls that rely on the position of items on the stack. When we outline
5513 // something containing a call, we have to emit a save and restore of LR in
5514 // the outlined function. Currently, this always happens by saving LR to the
5515 // stack. Thus, if we outline, say, half the parameters for a function call
5516 // plus the call, then we'll break the callee's expectations for the layout
5517 // of the stack.
5519 // FIXME: Allow calls to functions which construct a stack frame, as long
5520 // as they don't access arguments on the stack.
5521 // FIXME: Figure out some way to analyze functions defined in other modules.
5522 // We should be able to compute the memory usage based on the IR calling
5523 // convention, even if we can't see the definition.
5524 if (MI.isCall()) {
5525 // Get the function associated with the call. Look at each operand and find
5526 // the one that represents the callee and get its name.
5527 const Function *Callee = nullptr;
5528 for (const MachineOperand &MOP : MI.operands()) {
5529 if (MOP.isGlobal()) {
5530 Callee = dyn_cast<Function>(MOP.getGlobal());
5531 break;
5535 // Never outline calls to mcount. There isn't any rule that would require
5536 // this, but the Linux kernel's "ftrace" feature depends on it.
5537 if (Callee && Callee->getName() == "\01_mcount")
5538 return outliner::InstrType::Illegal;
5540 // If we don't know anything about the callee, assume it depends on the
5541 // stack layout of the caller. In that case, it's only legal to outline
5542 // as a tail-call. Whitelist the call instructions we know about so we
5543 // don't get unexpected results with call pseudo-instructions.
5544 auto UnknownCallOutlineType = outliner::InstrType::Illegal;
5545 if (MI.getOpcode() == AArch64::BLR || MI.getOpcode() == AArch64::BL)
5546 UnknownCallOutlineType = outliner::InstrType::LegalTerminator;
5548 if (!Callee)
5549 return UnknownCallOutlineType;
5551 // We have a function we have information about. Check it if it's something
5552 // can safely outline.
5553 MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
5555 // We don't know what's going on with the callee at all. Don't touch it.
5556 if (!CalleeMF)
5557 return UnknownCallOutlineType;
5559 // Check if we know anything about the callee saves on the function. If we
5560 // don't, then don't touch it, since that implies that we haven't
5561 // computed anything about its stack frame yet.
5562 MachineFrameInfo &MFI = CalleeMF->getFrameInfo();
5563 if (!MFI.isCalleeSavedInfoValid() || MFI.getStackSize() > 0 ||
5564 MFI.getNumObjects() > 0)
5565 return UnknownCallOutlineType;
5567 // At this point, we can say that CalleeMF ought to not pass anything on the
5568 // stack. Therefore, we can outline it.
5569 return outliner::InstrType::Legal;
5572 // Don't outline positions.
5573 if (MI.isPosition())
5574 return outliner::InstrType::Illegal;
5576 // Don't touch the link register or W30.
5577 if (MI.readsRegister(AArch64::W30, &getRegisterInfo()) ||
5578 MI.modifiesRegister(AArch64::W30, &getRegisterInfo()))
5579 return outliner::InstrType::Illegal;
5581 // Don't outline BTI instructions, because that will prevent the outlining
5582 // site from being indirectly callable.
5583 if (MI.getOpcode() == AArch64::HINT) {
5584 int64_t Imm = MI.getOperand(0).getImm();
5585 if (Imm == 32 || Imm == 34 || Imm == 36 || Imm == 38)
5586 return outliner::InstrType::Illegal;
5589 return outliner::InstrType::Legal;
5592 void AArch64InstrInfo::fixupPostOutline(MachineBasicBlock &MBB) const {
5593 for (MachineInstr &MI : MBB) {
5594 const MachineOperand *Base;
5595 unsigned Width;
5596 int64_t Offset;
5598 // Is this a load or store with an immediate offset with SP as the base?
5599 if (!MI.mayLoadOrStore() ||
5600 !getMemOperandWithOffsetWidth(MI, Base, Offset, Width, &RI) ||
5601 (Base->isReg() && Base->getReg() != AArch64::SP))
5602 continue;
5604 // It is, so we have to fix it up.
5605 unsigned Scale;
5606 int64_t Dummy1, Dummy2;
5608 MachineOperand &StackOffsetOperand = getMemOpBaseRegImmOfsOffsetOperand(MI);
5609 assert(StackOffsetOperand.isImm() && "Stack offset wasn't immediate!");
5610 getMemOpInfo(MI.getOpcode(), Scale, Width, Dummy1, Dummy2);
5611 assert(Scale != 0 && "Unexpected opcode!");
5613 // We've pushed the return address to the stack, so add 16 to the offset.
5614 // This is safe, since we already checked if it would overflow when we
5615 // checked if this instruction was legal to outline.
5616 int64_t NewImm = (Offset + 16) / Scale;
5617 StackOffsetOperand.setImm(NewImm);
5621 void AArch64InstrInfo::buildOutlinedFrame(
5622 MachineBasicBlock &MBB, MachineFunction &MF,
5623 const outliner::OutlinedFunction &OF) const {
5624 // For thunk outlining, rewrite the last instruction from a call to a
5625 // tail-call.
5626 if (OF.FrameConstructionID == MachineOutlinerThunk) {
5627 MachineInstr *Call = &*--MBB.instr_end();
5628 unsigned TailOpcode;
5629 if (Call->getOpcode() == AArch64::BL) {
5630 TailOpcode = AArch64::TCRETURNdi;
5631 } else {
5632 assert(Call->getOpcode() == AArch64::BLR);
5633 TailOpcode = AArch64::TCRETURNriALL;
5635 MachineInstr *TC = BuildMI(MF, DebugLoc(), get(TailOpcode))
5636 .add(Call->getOperand(0))
5637 .addImm(0);
5638 MBB.insert(MBB.end(), TC);
5639 Call->eraseFromParent();
5642 // Is there a call in the outlined range?
5643 auto IsNonTailCall = [](MachineInstr &MI) {
5644 return MI.isCall() && !MI.isReturn();
5646 if (std::any_of(MBB.instr_begin(), MBB.instr_end(), IsNonTailCall)) {
5647 // Fix up the instructions in the range, since we're going to modify the
5648 // stack.
5649 assert(OF.FrameConstructionID != MachineOutlinerDefault &&
5650 "Can only fix up stack references once");
5651 fixupPostOutline(MBB);
5653 // LR has to be a live in so that we can save it.
5654 MBB.addLiveIn(AArch64::LR);
5656 MachineBasicBlock::iterator It = MBB.begin();
5657 MachineBasicBlock::iterator Et = MBB.end();
5659 if (OF.FrameConstructionID == MachineOutlinerTailCall ||
5660 OF.FrameConstructionID == MachineOutlinerThunk)
5661 Et = std::prev(MBB.end());
5663 // Insert a save before the outlined region
5664 MachineInstr *STRXpre = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
5665 .addReg(AArch64::SP, RegState::Define)
5666 .addReg(AArch64::LR)
5667 .addReg(AArch64::SP)
5668 .addImm(-16);
5669 It = MBB.insert(It, STRXpre);
5671 const TargetSubtargetInfo &STI = MF.getSubtarget();
5672 const MCRegisterInfo *MRI = STI.getRegisterInfo();
5673 unsigned DwarfReg = MRI->getDwarfRegNum(AArch64::LR, true);
5675 // Add a CFI saying the stack was moved 16 B down.
5676 int64_t StackPosEntry =
5677 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 16));
5678 BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
5679 .addCFIIndex(StackPosEntry)
5680 .setMIFlags(MachineInstr::FrameSetup);
5682 // Add a CFI saying that the LR that we want to find is now 16 B higher than
5683 // before.
5684 int64_t LRPosEntry =
5685 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, DwarfReg, 16));
5686 BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
5687 .addCFIIndex(LRPosEntry)
5688 .setMIFlags(MachineInstr::FrameSetup);
5690 // Insert a restore before the terminator for the function.
5691 MachineInstr *LDRXpost = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
5692 .addReg(AArch64::SP, RegState::Define)
5693 .addReg(AArch64::LR, RegState::Define)
5694 .addReg(AArch64::SP)
5695 .addImm(16);
5696 Et = MBB.insert(Et, LDRXpost);
5699 // If this is a tail call outlined function, then there's already a return.
5700 if (OF.FrameConstructionID == MachineOutlinerTailCall ||
5701 OF.FrameConstructionID == MachineOutlinerThunk)
5702 return;
5704 // It's not a tail call, so we have to insert the return ourselves.
5705 MachineInstr *ret = BuildMI(MF, DebugLoc(), get(AArch64::RET))
5706 .addReg(AArch64::LR, RegState::Undef);
5707 MBB.insert(MBB.end(), ret);
5709 // Did we have to modify the stack by saving the link register?
5710 if (OF.FrameConstructionID != MachineOutlinerDefault)
5711 return;
5713 // We modified the stack.
5714 // Walk over the basic block and fix up all the stack accesses.
5715 fixupPostOutline(MBB);
5718 MachineBasicBlock::iterator AArch64InstrInfo::insertOutlinedCall(
5719 Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
5720 MachineFunction &MF, const outliner::Candidate &C) const {
5722 // Are we tail calling?
5723 if (C.CallConstructionID == MachineOutlinerTailCall) {
5724 // If yes, then we can just branch to the label.
5725 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::TCRETURNdi))
5726 .addGlobalAddress(M.getNamedValue(MF.getName()))
5727 .addImm(0));
5728 return It;
5731 // Are we saving the link register?
5732 if (C.CallConstructionID == MachineOutlinerNoLRSave ||
5733 C.CallConstructionID == MachineOutlinerThunk) {
5734 // No, so just insert the call.
5735 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
5736 .addGlobalAddress(M.getNamedValue(MF.getName())));
5737 return It;
5740 // We want to return the spot where we inserted the call.
5741 MachineBasicBlock::iterator CallPt;
5743 // Instructions for saving and restoring LR around the call instruction we're
5744 // going to insert.
5745 MachineInstr *Save;
5746 MachineInstr *Restore;
5747 // Can we save to a register?
5748 if (C.CallConstructionID == MachineOutlinerRegSave) {
5749 // FIXME: This logic should be sunk into a target-specific interface so that
5750 // we don't have to recompute the register.
5751 unsigned Reg = findRegisterToSaveLRTo(C);
5752 assert(Reg != 0 && "No callee-saved register available?");
5754 // Save and restore LR from that register.
5755 Save = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), Reg)
5756 .addReg(AArch64::XZR)
5757 .addReg(AArch64::LR)
5758 .addImm(0);
5759 Restore = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), AArch64::LR)
5760 .addReg(AArch64::XZR)
5761 .addReg(Reg)
5762 .addImm(0);
5763 } else {
5764 // We have the default case. Save and restore from SP.
5765 Save = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
5766 .addReg(AArch64::SP, RegState::Define)
5767 .addReg(AArch64::LR)
5768 .addReg(AArch64::SP)
5769 .addImm(-16);
5770 Restore = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
5771 .addReg(AArch64::SP, RegState::Define)
5772 .addReg(AArch64::LR, RegState::Define)
5773 .addReg(AArch64::SP)
5774 .addImm(16);
5777 It = MBB.insert(It, Save);
5778 It++;
5780 // Insert the call.
5781 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
5782 .addGlobalAddress(M.getNamedValue(MF.getName())));
5783 CallPt = It;
5784 It++;
5786 It = MBB.insert(It, Restore);
5787 return CallPt;
5790 bool AArch64InstrInfo::shouldOutlineFromFunctionByDefault(
5791 MachineFunction &MF) const {
5792 return MF.getFunction().hasMinSize();
5795 bool AArch64InstrInfo::isCopyInstrImpl(
5796 const MachineInstr &MI, const MachineOperand *&Source,
5797 const MachineOperand *&Destination) const {
5799 // AArch64::ORRWrs and AArch64::ORRXrs with WZR/XZR reg
5800 // and zero immediate operands used as an alias for mov instruction.
5801 if (MI.getOpcode() == AArch64::ORRWrs &&
5802 MI.getOperand(1).getReg() == AArch64::WZR &&
5803 MI.getOperand(3).getImm() == 0x0) {
5804 Destination = &MI.getOperand(0);
5805 Source = &MI.getOperand(2);
5806 return true;
5809 if (MI.getOpcode() == AArch64::ORRXrs &&
5810 MI.getOperand(1).getReg() == AArch64::XZR &&
5811 MI.getOperand(3).getImm() == 0x0) {
5812 Destination = &MI.getOperand(0);
5813 Source = &MI.getOperand(2);
5814 return true;
5817 return false;
5820 #define GET_INSTRINFO_HELPERS
5821 #include "AArch64GenInstrInfo.inc"