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