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