1 //===-- RISCVInstrInfo.cpp - RISCV Instruction Information ------*- C++ -*-===// 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 RISCV implementation of the TargetInstrInfo class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "RISCVInstrInfo.h" 14 #include "MCTargetDesc/RISCVMatInt.h" 15 #include "RISCV.h" 16 #include "RISCVSubtarget.h" 17 #include "RISCVTargetMachine.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/CodeGen/MachineFunctionPass.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/RegisterScavenging.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/Support/TargetRegistry.h" 26 27 using namespace llvm; 28 29 #define GEN_CHECK_COMPRESS_INSTR 30 #include "RISCVGenCompressInstEmitter.inc" 31 32 #define GET_INSTRINFO_CTOR_DTOR 33 #include "RISCVGenInstrInfo.inc" 34 35 namespace llvm { 36 namespace RISCVVPseudosTable { 37 38 using namespace RISCV; 39 40 #define GET_RISCVVPseudosTable_IMPL 41 #include "RISCVGenSearchableTables.inc" 42 43 } // namespace RISCVVPseudosTable 44 } // namespace llvm 45 46 RISCVInstrInfo::RISCVInstrInfo(RISCVSubtarget &STI) 47 : RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP), 48 STI(STI) {} 49 50 unsigned RISCVInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 51 int &FrameIndex) const { 52 switch (MI.getOpcode()) { 53 default: 54 return 0; 55 case RISCV::LB: 56 case RISCV::LBU: 57 case RISCV::LH: 58 case RISCV::LHU: 59 case RISCV::FLH: 60 case RISCV::LW: 61 case RISCV::FLW: 62 case RISCV::LWU: 63 case RISCV::LD: 64 case RISCV::FLD: 65 break; 66 } 67 68 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() && 69 MI.getOperand(2).getImm() == 0) { 70 FrameIndex = MI.getOperand(1).getIndex(); 71 return MI.getOperand(0).getReg(); 72 } 73 74 return 0; 75 } 76 77 unsigned RISCVInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 78 int &FrameIndex) const { 79 switch (MI.getOpcode()) { 80 default: 81 return 0; 82 case RISCV::SB: 83 case RISCV::SH: 84 case RISCV::SW: 85 case RISCV::FSH: 86 case RISCV::FSW: 87 case RISCV::SD: 88 case RISCV::FSD: 89 break; 90 } 91 92 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() && 93 MI.getOperand(2).getImm() == 0) { 94 FrameIndex = MI.getOperand(1).getIndex(); 95 return MI.getOperand(0).getReg(); 96 } 97 98 return 0; 99 } 100 101 void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 102 MachineBasicBlock::iterator MBBI, 103 const DebugLoc &DL, MCRegister DstReg, 104 MCRegister SrcReg, bool KillSrc) const { 105 if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) { 106 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg) 107 .addReg(SrcReg, getKillRegState(KillSrc)) 108 .addImm(0); 109 return; 110 } 111 112 // FPR->FPR copies and VR->VR copies. 113 unsigned Opc; 114 bool IsScalableVector = false; 115 if (RISCV::FPR16RegClass.contains(DstReg, SrcReg)) 116 Opc = RISCV::FSGNJ_H; 117 else if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) 118 Opc = RISCV::FSGNJ_S; 119 else if (RISCV::FPR64RegClass.contains(DstReg, SrcReg)) 120 Opc = RISCV::FSGNJ_D; 121 else if (RISCV::VRRegClass.contains(DstReg, SrcReg)) { 122 Opc = RISCV::PseudoVMV1R_V; 123 IsScalableVector = true; 124 } else if (RISCV::VRM2RegClass.contains(DstReg, SrcReg)) { 125 Opc = RISCV::PseudoVMV2R_V; 126 IsScalableVector = true; 127 } else if (RISCV::VRM4RegClass.contains(DstReg, SrcReg)) { 128 Opc = RISCV::PseudoVMV4R_V; 129 IsScalableVector = true; 130 } else if (RISCV::VRM8RegClass.contains(DstReg, SrcReg)) { 131 Opc = RISCV::PseudoVMV8R_V; 132 IsScalableVector = true; 133 } else 134 llvm_unreachable("Impossible reg-to-reg copy"); 135 136 if (IsScalableVector) 137 BuildMI(MBB, MBBI, DL, get(Opc), DstReg) 138 .addReg(SrcReg, getKillRegState(KillSrc)); 139 else 140 BuildMI(MBB, MBBI, DL, get(Opc), DstReg) 141 .addReg(SrcReg, getKillRegState(KillSrc)) 142 .addReg(SrcReg, getKillRegState(KillSrc)); 143 } 144 145 void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 146 MachineBasicBlock::iterator I, 147 Register SrcReg, bool IsKill, int FI, 148 const TargetRegisterClass *RC, 149 const TargetRegisterInfo *TRI) const { 150 DebugLoc DL; 151 if (I != MBB.end()) 152 DL = I->getDebugLoc(); 153 154 MachineFunction *MF = MBB.getParent(); 155 const MachineFrameInfo &MFI = MF->getFrameInfo(); 156 MachineMemOperand *MMO = MF->getMachineMemOperand( 157 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore, 158 MFI.getObjectSize(FI), MFI.getObjectAlign(FI)); 159 160 unsigned Opcode; 161 if (RISCV::GPRRegClass.hasSubClassEq(RC)) 162 Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ? 163 RISCV::SW : RISCV::SD; 164 else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) 165 Opcode = RISCV::FSH; 166 else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) 167 Opcode = RISCV::FSW; 168 else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) 169 Opcode = RISCV::FSD; 170 else 171 llvm_unreachable("Can't store this register to stack slot"); 172 173 BuildMI(MBB, I, DL, get(Opcode)) 174 .addReg(SrcReg, getKillRegState(IsKill)) 175 .addFrameIndex(FI) 176 .addImm(0) 177 .addMemOperand(MMO); 178 } 179 180 void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 181 MachineBasicBlock::iterator I, 182 Register DstReg, int FI, 183 const TargetRegisterClass *RC, 184 const TargetRegisterInfo *TRI) const { 185 DebugLoc DL; 186 if (I != MBB.end()) 187 DL = I->getDebugLoc(); 188 189 MachineFunction *MF = MBB.getParent(); 190 const MachineFrameInfo &MFI = MF->getFrameInfo(); 191 MachineMemOperand *MMO = MF->getMachineMemOperand( 192 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad, 193 MFI.getObjectSize(FI), MFI.getObjectAlign(FI)); 194 195 unsigned Opcode; 196 if (RISCV::GPRRegClass.hasSubClassEq(RC)) 197 Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ? 198 RISCV::LW : RISCV::LD; 199 else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) 200 Opcode = RISCV::FLH; 201 else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) 202 Opcode = RISCV::FLW; 203 else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) 204 Opcode = RISCV::FLD; 205 else 206 llvm_unreachable("Can't load this register from stack slot"); 207 208 BuildMI(MBB, I, DL, get(Opcode), DstReg) 209 .addFrameIndex(FI) 210 .addImm(0) 211 .addMemOperand(MMO); 212 } 213 214 void RISCVInstrInfo::movImm(MachineBasicBlock &MBB, 215 MachineBasicBlock::iterator MBBI, 216 const DebugLoc &DL, Register DstReg, uint64_t Val, 217 MachineInstr::MIFlag Flag) const { 218 MachineFunction *MF = MBB.getParent(); 219 MachineRegisterInfo &MRI = MF->getRegInfo(); 220 bool IsRV64 = MF->getSubtarget<RISCVSubtarget>().is64Bit(); 221 Register SrcReg = RISCV::X0; 222 Register Result = MRI.createVirtualRegister(&RISCV::GPRRegClass); 223 unsigned Num = 0; 224 225 if (!IsRV64 && !isInt<32>(Val)) 226 report_fatal_error("Should only materialize 32-bit constants for RV32"); 227 228 RISCVMatInt::InstSeq Seq; 229 RISCVMatInt::generateInstSeq(Val, IsRV64, Seq); 230 assert(Seq.size() > 0); 231 232 for (RISCVMatInt::Inst &Inst : Seq) { 233 // Write the final result to DstReg if it's the last instruction in the Seq. 234 // Otherwise, write the result to the temp register. 235 if (++Num == Seq.size()) 236 Result = DstReg; 237 238 if (Inst.Opc == RISCV::LUI) { 239 BuildMI(MBB, MBBI, DL, get(RISCV::LUI), Result) 240 .addImm(Inst.Imm) 241 .setMIFlag(Flag); 242 } else { 243 BuildMI(MBB, MBBI, DL, get(Inst.Opc), Result) 244 .addReg(SrcReg, RegState::Kill) 245 .addImm(Inst.Imm) 246 .setMIFlag(Flag); 247 } 248 // Only the first instruction has X0 as its source. 249 SrcReg = Result; 250 } 251 } 252 253 // The contents of values added to Cond are not examined outside of 254 // RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we 255 // push BranchOpcode, Reg1, Reg2. 256 static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target, 257 SmallVectorImpl<MachineOperand> &Cond) { 258 // Block ends with fall-through condbranch. 259 assert(LastInst.getDesc().isConditionalBranch() && 260 "Unknown conditional branch"); 261 Target = LastInst.getOperand(2).getMBB(); 262 Cond.push_back(MachineOperand::CreateImm(LastInst.getOpcode())); 263 Cond.push_back(LastInst.getOperand(0)); 264 Cond.push_back(LastInst.getOperand(1)); 265 } 266 267 static unsigned getOppositeBranchOpcode(int Opc) { 268 switch (Opc) { 269 default: 270 llvm_unreachable("Unrecognized conditional branch"); 271 case RISCV::BEQ: 272 return RISCV::BNE; 273 case RISCV::BNE: 274 return RISCV::BEQ; 275 case RISCV::BLT: 276 return RISCV::BGE; 277 case RISCV::BGE: 278 return RISCV::BLT; 279 case RISCV::BLTU: 280 return RISCV::BGEU; 281 case RISCV::BGEU: 282 return RISCV::BLTU; 283 } 284 } 285 286 bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 287 MachineBasicBlock *&TBB, 288 MachineBasicBlock *&FBB, 289 SmallVectorImpl<MachineOperand> &Cond, 290 bool AllowModify) const { 291 TBB = FBB = nullptr; 292 Cond.clear(); 293 294 // If the block has no terminators, it just falls into the block after it. 295 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 296 if (I == MBB.end() || !isUnpredicatedTerminator(*I)) 297 return false; 298 299 // Count the number of terminators and find the first unconditional or 300 // indirect branch. 301 MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end(); 302 int NumTerminators = 0; 303 for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J); 304 J++) { 305 NumTerminators++; 306 if (J->getDesc().isUnconditionalBranch() || 307 J->getDesc().isIndirectBranch()) { 308 FirstUncondOrIndirectBr = J.getReverse(); 309 } 310 } 311 312 // If AllowModify is true, we can erase any terminators after 313 // FirstUncondOrIndirectBR. 314 if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) { 315 while (std::next(FirstUncondOrIndirectBr) != MBB.end()) { 316 std::next(FirstUncondOrIndirectBr)->eraseFromParent(); 317 NumTerminators--; 318 } 319 I = FirstUncondOrIndirectBr; 320 } 321 322 // We can't handle blocks that end in an indirect branch. 323 if (I->getDesc().isIndirectBranch()) 324 return true; 325 326 // We can't handle blocks with more than 2 terminators. 327 if (NumTerminators > 2) 328 return true; 329 330 // Handle a single unconditional branch. 331 if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) { 332 TBB = getBranchDestBlock(*I); 333 return false; 334 } 335 336 // Handle a single conditional branch. 337 if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) { 338 parseCondBranch(*I, TBB, Cond); 339 return false; 340 } 341 342 // Handle a conditional branch followed by an unconditional branch. 343 if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() && 344 I->getDesc().isUnconditionalBranch()) { 345 parseCondBranch(*std::prev(I), TBB, Cond); 346 FBB = getBranchDestBlock(*I); 347 return false; 348 } 349 350 // Otherwise, we can't handle this. 351 return true; 352 } 353 354 unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB, 355 int *BytesRemoved) const { 356 if (BytesRemoved) 357 *BytesRemoved = 0; 358 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 359 if (I == MBB.end()) 360 return 0; 361 362 if (!I->getDesc().isUnconditionalBranch() && 363 !I->getDesc().isConditionalBranch()) 364 return 0; 365 366 // Remove the branch. 367 if (BytesRemoved) 368 *BytesRemoved += getInstSizeInBytes(*I); 369 I->eraseFromParent(); 370 371 I = MBB.end(); 372 373 if (I == MBB.begin()) 374 return 1; 375 --I; 376 if (!I->getDesc().isConditionalBranch()) 377 return 1; 378 379 // Remove the branch. 380 if (BytesRemoved) 381 *BytesRemoved += getInstSizeInBytes(*I); 382 I->eraseFromParent(); 383 return 2; 384 } 385 386 // Inserts a branch into the end of the specific MachineBasicBlock, returning 387 // the number of instructions inserted. 388 unsigned RISCVInstrInfo::insertBranch( 389 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, 390 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const { 391 if (BytesAdded) 392 *BytesAdded = 0; 393 394 // Shouldn't be a fall through. 395 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 396 assert((Cond.size() == 3 || Cond.size() == 0) && 397 "RISCV branch conditions have two components!"); 398 399 // Unconditional branch. 400 if (Cond.empty()) { 401 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB); 402 if (BytesAdded) 403 *BytesAdded += getInstSizeInBytes(MI); 404 return 1; 405 } 406 407 // Either a one or two-way conditional branch. 408 unsigned Opc = Cond[0].getImm(); 409 MachineInstr &CondMI = 410 *BuildMI(&MBB, DL, get(Opc)).add(Cond[1]).add(Cond[2]).addMBB(TBB); 411 if (BytesAdded) 412 *BytesAdded += getInstSizeInBytes(CondMI); 413 414 // One-way conditional branch. 415 if (!FBB) 416 return 1; 417 418 // Two-way conditional branch. 419 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB); 420 if (BytesAdded) 421 *BytesAdded += getInstSizeInBytes(MI); 422 return 2; 423 } 424 425 unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, 426 MachineBasicBlock &DestBB, 427 const DebugLoc &DL, 428 int64_t BrOffset, 429 RegScavenger *RS) const { 430 assert(RS && "RegScavenger required for long branching"); 431 assert(MBB.empty() && 432 "new block should be inserted for expanding unconditional branch"); 433 assert(MBB.pred_size() == 1); 434 435 MachineFunction *MF = MBB.getParent(); 436 MachineRegisterInfo &MRI = MF->getRegInfo(); 437 438 if (!isInt<32>(BrOffset)) 439 report_fatal_error( 440 "Branch offsets outside of the signed 32-bit range not supported"); 441 442 // FIXME: A virtual register must be used initially, as the register 443 // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch 444 // uses the same workaround). 445 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); 446 auto II = MBB.end(); 447 448 MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump)) 449 .addReg(ScratchReg, RegState::Define | RegState::Dead) 450 .addMBB(&DestBB, RISCVII::MO_CALL); 451 452 RS->enterBasicBlockEnd(MBB); 453 unsigned Scav = RS->scavengeRegisterBackwards(RISCV::GPRRegClass, 454 MI.getIterator(), false, 0); 455 MRI.replaceRegWith(ScratchReg, Scav); 456 MRI.clearVirtRegs(); 457 RS->setRegUsed(Scav); 458 return 8; 459 } 460 461 bool RISCVInstrInfo::reverseBranchCondition( 462 SmallVectorImpl<MachineOperand> &Cond) const { 463 assert((Cond.size() == 3) && "Invalid branch condition!"); 464 Cond[0].setImm(getOppositeBranchOpcode(Cond[0].getImm())); 465 return false; 466 } 467 468 MachineBasicBlock * 469 RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const { 470 assert(MI.getDesc().isBranch() && "Unexpected opcode!"); 471 // The branch target is always the last operand. 472 int NumOp = MI.getNumExplicitOperands(); 473 return MI.getOperand(NumOp - 1).getMBB(); 474 } 475 476 bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp, 477 int64_t BrOffset) const { 478 unsigned XLen = STI.getXLen(); 479 // Ideally we could determine the supported branch offset from the 480 // RISCVII::FormMask, but this can't be used for Pseudo instructions like 481 // PseudoBR. 482 switch (BranchOp) { 483 default: 484 llvm_unreachable("Unexpected opcode!"); 485 case RISCV::BEQ: 486 case RISCV::BNE: 487 case RISCV::BLT: 488 case RISCV::BGE: 489 case RISCV::BLTU: 490 case RISCV::BGEU: 491 return isIntN(13, BrOffset); 492 case RISCV::JAL: 493 case RISCV::PseudoBR: 494 return isIntN(21, BrOffset); 495 case RISCV::PseudoJump: 496 return isIntN(32, SignExtend64(BrOffset + 0x800, XLen)); 497 } 498 } 499 500 unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 501 unsigned Opcode = MI.getOpcode(); 502 503 switch (Opcode) { 504 default: { 505 if (MI.getParent() && MI.getParent()->getParent()) { 506 const auto MF = MI.getMF(); 507 const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget()); 508 const MCRegisterInfo &MRI = *TM.getMCRegisterInfo(); 509 const MCSubtargetInfo &STI = *TM.getMCSubtargetInfo(); 510 const RISCVSubtarget &ST = MF->getSubtarget<RISCVSubtarget>(); 511 if (isCompressibleInst(MI, &ST, MRI, STI)) 512 return 2; 513 } 514 return get(Opcode).getSize(); 515 } 516 case TargetOpcode::EH_LABEL: 517 case TargetOpcode::IMPLICIT_DEF: 518 case TargetOpcode::KILL: 519 case TargetOpcode::DBG_VALUE: 520 return 0; 521 // These values are determined based on RISCVExpandAtomicPseudoInsts, 522 // RISCVExpandPseudoInsts and RISCVMCCodeEmitter, depending on where the 523 // pseudos are expanded. 524 case RISCV::PseudoCALLReg: 525 case RISCV::PseudoCALL: 526 case RISCV::PseudoJump: 527 case RISCV::PseudoTAIL: 528 case RISCV::PseudoLLA: 529 case RISCV::PseudoLA: 530 case RISCV::PseudoLA_TLS_IE: 531 case RISCV::PseudoLA_TLS_GD: 532 return 8; 533 case RISCV::PseudoAtomicLoadNand32: 534 case RISCV::PseudoAtomicLoadNand64: 535 return 20; 536 case RISCV::PseudoMaskedAtomicSwap32: 537 case RISCV::PseudoMaskedAtomicLoadAdd32: 538 case RISCV::PseudoMaskedAtomicLoadSub32: 539 return 28; 540 case RISCV::PseudoMaskedAtomicLoadNand32: 541 return 32; 542 case RISCV::PseudoMaskedAtomicLoadMax32: 543 case RISCV::PseudoMaskedAtomicLoadMin32: 544 return 44; 545 case RISCV::PseudoMaskedAtomicLoadUMax32: 546 case RISCV::PseudoMaskedAtomicLoadUMin32: 547 return 36; 548 case RISCV::PseudoCmpXchg32: 549 case RISCV::PseudoCmpXchg64: 550 return 16; 551 case RISCV::PseudoMaskedCmpXchg32: 552 return 32; 553 case TargetOpcode::INLINEASM: 554 case TargetOpcode::INLINEASM_BR: { 555 const MachineFunction &MF = *MI.getParent()->getParent(); 556 const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget()); 557 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), 558 *TM.getMCAsmInfo()); 559 } 560 } 561 } 562 563 bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const { 564 const unsigned Opcode = MI.getOpcode(); 565 switch (Opcode) { 566 default: 567 break; 568 case RISCV::FSGNJ_D: 569 case RISCV::FSGNJ_S: 570 // The canonical floating-point move is fsgnj rd, rs, rs. 571 return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() && 572 MI.getOperand(1).getReg() == MI.getOperand(2).getReg(); 573 case RISCV::ADDI: 574 case RISCV::ORI: 575 case RISCV::XORI: 576 return (MI.getOperand(1).isReg() && 577 MI.getOperand(1).getReg() == RISCV::X0) || 578 (MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0); 579 } 580 return MI.isAsCheapAsAMove(); 581 } 582 583 Optional<DestSourcePair> 584 RISCVInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const { 585 if (MI.isMoveReg()) 586 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 587 switch (MI.getOpcode()) { 588 default: 589 break; 590 case RISCV::ADDI: 591 // Operand 1 can be a frameindex but callers expect registers 592 if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() && 593 MI.getOperand(2).getImm() == 0) 594 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 595 break; 596 case RISCV::FSGNJ_D: 597 case RISCV::FSGNJ_S: 598 // The canonical floating-point move is fsgnj rd, rs, rs. 599 if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() && 600 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) 601 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 602 break; 603 } 604 return None; 605 } 606 607 bool RISCVInstrInfo::verifyInstruction(const MachineInstr &MI, 608 StringRef &ErrInfo) const { 609 const MCInstrInfo *MCII = STI.getInstrInfo(); 610 MCInstrDesc const &Desc = MCII->get(MI.getOpcode()); 611 612 for (auto &OI : enumerate(Desc.operands())) { 613 unsigned OpType = OI.value().OperandType; 614 if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM && 615 OpType <= RISCVOp::OPERAND_LAST_RISCV_IMM) { 616 const MachineOperand &MO = MI.getOperand(OI.index()); 617 if (MO.isImm()) { 618 int64_t Imm = MO.getImm(); 619 bool Ok; 620 switch (OpType) { 621 default: 622 llvm_unreachable("Unexpected operand type"); 623 case RISCVOp::OPERAND_UIMM4: 624 Ok = isUInt<4>(Imm); 625 break; 626 case RISCVOp::OPERAND_UIMM5: 627 Ok = isUInt<5>(Imm); 628 break; 629 case RISCVOp::OPERAND_UIMM12: 630 Ok = isUInt<12>(Imm); 631 break; 632 case RISCVOp::OPERAND_SIMM12: 633 Ok = isInt<12>(Imm); 634 break; 635 case RISCVOp::OPERAND_UIMM20: 636 Ok = isUInt<20>(Imm); 637 break; 638 case RISCVOp::OPERAND_UIMMLOG2XLEN: 639 if (STI.getTargetTriple().isArch64Bit()) 640 Ok = isUInt<6>(Imm); 641 else 642 Ok = isUInt<5>(Imm); 643 break; 644 } 645 if (!Ok) { 646 ErrInfo = "Invalid immediate"; 647 return false; 648 } 649 } 650 } 651 } 652 653 return true; 654 } 655 656 // Return true if get the base operand, byte offset of an instruction and the 657 // memory width. Width is the size of memory that is being loaded/stored. 658 bool RISCVInstrInfo::getMemOperandWithOffsetWidth( 659 const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset, 660 unsigned &Width, const TargetRegisterInfo *TRI) const { 661 if (!LdSt.mayLoadOrStore()) 662 return false; 663 664 // Here we assume the standard RISC-V ISA, which uses a base+offset 665 // addressing mode. You'll need to relax these conditions to support custom 666 // load/stores instructions. 667 if (LdSt.getNumExplicitOperands() != 3) 668 return false; 669 if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isImm()) 670 return false; 671 672 if (!LdSt.hasOneMemOperand()) 673 return false; 674 675 Width = (*LdSt.memoperands_begin())->getSize(); 676 BaseReg = &LdSt.getOperand(1); 677 Offset = LdSt.getOperand(2).getImm(); 678 return true; 679 } 680 681 bool RISCVInstrInfo::areMemAccessesTriviallyDisjoint( 682 const MachineInstr &MIa, const MachineInstr &MIb) const { 683 assert(MIa.mayLoadOrStore() && "MIa must be a load or store."); 684 assert(MIb.mayLoadOrStore() && "MIb must be a load or store."); 685 686 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() || 687 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 688 return false; 689 690 // Retrieve the base register, offset from the base register and width. Width 691 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If 692 // base registers are identical, and the offset of a lower memory access + 693 // the width doesn't overlap the offset of a higher memory access, 694 // then the memory accesses are different. 695 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); 696 const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr; 697 int64_t OffsetA = 0, OffsetB = 0; 698 unsigned int WidthA = 0, WidthB = 0; 699 if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) && 700 getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) { 701 if (BaseOpA->isIdenticalTo(*BaseOpB)) { 702 int LowOffset = std::min(OffsetA, OffsetB); 703 int HighOffset = std::max(OffsetA, OffsetB); 704 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 705 if (LowOffset + LowWidth <= HighOffset) 706 return true; 707 } 708 } 709 return false; 710 } 711 712 std::pair<unsigned, unsigned> 713 RISCVInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 714 const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK; 715 return std::make_pair(TF & Mask, TF & ~Mask); 716 } 717 718 ArrayRef<std::pair<unsigned, const char *>> 719 RISCVInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 720 using namespace RISCVII; 721 static const std::pair<unsigned, const char *> TargetFlags[] = { 722 {MO_CALL, "riscv-call"}, 723 {MO_PLT, "riscv-plt"}, 724 {MO_LO, "riscv-lo"}, 725 {MO_HI, "riscv-hi"}, 726 {MO_PCREL_LO, "riscv-pcrel-lo"}, 727 {MO_PCREL_HI, "riscv-pcrel-hi"}, 728 {MO_GOT_HI, "riscv-got-hi"}, 729 {MO_TPREL_LO, "riscv-tprel-lo"}, 730 {MO_TPREL_HI, "riscv-tprel-hi"}, 731 {MO_TPREL_ADD, "riscv-tprel-add"}, 732 {MO_TLS_GOT_HI, "riscv-tls-got-hi"}, 733 {MO_TLS_GD_HI, "riscv-tls-gd-hi"}}; 734 return makeArrayRef(TargetFlags); 735 } 736 bool RISCVInstrInfo::isFunctionSafeToOutlineFrom( 737 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const { 738 const Function &F = MF.getFunction(); 739 740 // Can F be deduplicated by the linker? If it can, don't outline from it. 741 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage()) 742 return false; 743 744 // Don't outline from functions with section markings; the program could 745 // expect that all the code is in the named section. 746 if (F.hasSection()) 747 return false; 748 749 // It's safe to outline from MF. 750 return true; 751 } 752 753 bool RISCVInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB, 754 unsigned &Flags) const { 755 // More accurate safety checking is done in getOutliningCandidateInfo. 756 return true; 757 } 758 759 // Enum values indicating how an outlined call should be constructed. 760 enum MachineOutlinerConstructionID { 761 MachineOutlinerDefault 762 }; 763 764 outliner::OutlinedFunction RISCVInstrInfo::getOutliningCandidateInfo( 765 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const { 766 767 // First we need to filter out candidates where the X5 register (IE t0) can't 768 // be used to setup the function call. 769 auto CannotInsertCall = [](outliner::Candidate &C) { 770 const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo(); 771 772 C.initLRU(*TRI); 773 LiveRegUnits LRU = C.LRU; 774 return !LRU.available(RISCV::X5); 775 }; 776 777 llvm::erase_if(RepeatedSequenceLocs, CannotInsertCall); 778 779 // If the sequence doesn't have enough candidates left, then we're done. 780 if (RepeatedSequenceLocs.size() < 2) 781 return outliner::OutlinedFunction(); 782 783 unsigned SequenceSize = 0; 784 785 auto I = RepeatedSequenceLocs[0].front(); 786 auto E = std::next(RepeatedSequenceLocs[0].back()); 787 for (; I != E; ++I) 788 SequenceSize += getInstSizeInBytes(*I); 789 790 // call t0, function = 8 bytes. 791 unsigned CallOverhead = 8; 792 for (auto &C : RepeatedSequenceLocs) 793 C.setCallInfo(MachineOutlinerDefault, CallOverhead); 794 795 // jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled. 796 unsigned FrameOverhead = 4; 797 if (RepeatedSequenceLocs[0].getMF()->getSubtarget() 798 .getFeatureBits()[RISCV::FeatureStdExtC]) 799 FrameOverhead = 2; 800 801 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize, 802 FrameOverhead, MachineOutlinerDefault); 803 } 804 805 outliner::InstrType 806 RISCVInstrInfo::getOutliningType(MachineBasicBlock::iterator &MBBI, 807 unsigned Flags) const { 808 MachineInstr &MI = *MBBI; 809 MachineBasicBlock *MBB = MI.getParent(); 810 const TargetRegisterInfo *TRI = 811 MBB->getParent()->getSubtarget().getRegisterInfo(); 812 813 // Positions generally can't safely be outlined. 814 if (MI.isPosition()) { 815 // We can manually strip out CFI instructions later. 816 if (MI.isCFIInstruction()) 817 return outliner::InstrType::Invisible; 818 819 return outliner::InstrType::Illegal; 820 } 821 822 // Don't trust the user to write safe inline assembly. 823 if (MI.isInlineAsm()) 824 return outliner::InstrType::Illegal; 825 826 // We can't outline branches to other basic blocks. 827 if (MI.isTerminator() && !MBB->succ_empty()) 828 return outliner::InstrType::Illegal; 829 830 // We need support for tail calls to outlined functions before return 831 // statements can be allowed. 832 if (MI.isReturn()) 833 return outliner::InstrType::Illegal; 834 835 // Don't allow modifying the X5 register which we use for return addresses for 836 // these outlined functions. 837 if (MI.modifiesRegister(RISCV::X5, TRI) || 838 MI.getDesc().hasImplicitDefOfPhysReg(RISCV::X5)) 839 return outliner::InstrType::Illegal; 840 841 // Make sure the operands don't reference something unsafe. 842 for (const auto &MO : MI.operands()) 843 if (MO.isMBB() || MO.isBlockAddress() || MO.isCPI()) 844 return outliner::InstrType::Illegal; 845 846 // Don't allow instructions which won't be materialized to impact outlining 847 // analysis. 848 if (MI.isMetaInstruction()) 849 return outliner::InstrType::Invisible; 850 851 return outliner::InstrType::Legal; 852 } 853 854 void RISCVInstrInfo::buildOutlinedFrame( 855 MachineBasicBlock &MBB, MachineFunction &MF, 856 const outliner::OutlinedFunction &OF) const { 857 858 // Strip out any CFI instructions 859 bool Changed = true; 860 while (Changed) { 861 Changed = false; 862 auto I = MBB.begin(); 863 auto E = MBB.end(); 864 for (; I != E; ++I) { 865 if (I->isCFIInstruction()) { 866 I->removeFromParent(); 867 Changed = true; 868 break; 869 } 870 } 871 } 872 873 MBB.addLiveIn(RISCV::X5); 874 875 // Add in a return instruction to the end of the outlined frame. 876 MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR)) 877 .addReg(RISCV::X0, RegState::Define) 878 .addReg(RISCV::X5) 879 .addImm(0)); 880 } 881 882 MachineBasicBlock::iterator RISCVInstrInfo::insertOutlinedCall( 883 Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It, 884 MachineFunction &MF, const outliner::Candidate &C) const { 885 886 // Add in a call instruction to the outlined function at the given location. 887 It = MBB.insert(It, 888 BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5) 889 .addGlobalAddress(M.getNamedValue(MF.getName()), 0, 890 RISCVII::MO_CALL)); 891 return It; 892 } 893 894 // clang-format off 895 #define CASE_VFMA_OPCODE_COMMON(OP, TYPE, LMUL) \ 896 RISCV::PseudoV##OP##_##TYPE##_##LMUL##_COMMUTABLE 897 898 #define CASE_VFMA_OPCODE_LMULS(OP, TYPE) \ 899 CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF8): \ 900 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF4): \ 901 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF2): \ 902 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M1): \ 903 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M2): \ 904 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M4): \ 905 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M8) 906 907 #define CASE_VFMA_SPLATS(OP) \ 908 CASE_VFMA_OPCODE_LMULS(OP, VF16): \ 909 case CASE_VFMA_OPCODE_LMULS(OP, VF32): \ 910 case CASE_VFMA_OPCODE_LMULS(OP, VF64) 911 // clang-format on 912 913 bool RISCVInstrInfo::findCommutedOpIndices(const MachineInstr &MI, 914 unsigned &SrcOpIdx1, 915 unsigned &SrcOpIdx2) const { 916 const MCInstrDesc &Desc = MI.getDesc(); 917 if (!Desc.isCommutable()) 918 return false; 919 920 switch (MI.getOpcode()) { 921 case CASE_VFMA_SPLATS(FMADD): 922 case CASE_VFMA_SPLATS(FMSUB): 923 case CASE_VFMA_SPLATS(FMACC): 924 case CASE_VFMA_SPLATS(FMSAC): 925 case CASE_VFMA_SPLATS(FNMADD): 926 case CASE_VFMA_SPLATS(FNMSUB): 927 case CASE_VFMA_SPLATS(FNMACC): 928 case CASE_VFMA_SPLATS(FNMSAC): 929 case CASE_VFMA_OPCODE_LMULS(FMACC, VV): 930 case CASE_VFMA_OPCODE_LMULS(FMSAC, VV): 931 case CASE_VFMA_OPCODE_LMULS(FNMACC, VV): 932 case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV): { 933 // For these instructions we can only swap operand 1 and operand 3 by 934 // changing the opcode. 935 unsigned CommutableOpIdx1 = 1; 936 unsigned CommutableOpIdx2 = 3; 937 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1, 938 CommutableOpIdx2)) 939 return false; 940 return true; 941 } 942 case CASE_VFMA_OPCODE_LMULS(FMADD, VV): 943 case CASE_VFMA_OPCODE_LMULS(FMSUB, VV): 944 case CASE_VFMA_OPCODE_LMULS(FNMADD, VV): 945 case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV): { 946 // For these instructions we have more freedom. We can commute with the 947 // other multiplicand or with the addend/subtrahend/minuend. 948 949 // Any fixed operand must be from source 1, 2 or 3. 950 if (SrcOpIdx1 != CommuteAnyOperandIndex && SrcOpIdx1 > 3) 951 return false; 952 if (SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx2 > 3) 953 return false; 954 955 // It both ops are fixed one must be the tied source. 956 if (SrcOpIdx1 != CommuteAnyOperandIndex && 957 SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx1 != 1 && SrcOpIdx2 != 1) 958 return false; 959 960 // Look for two different register operands assumed to be commutable 961 // regardless of the FMA opcode. The FMA opcode is adjusted later if 962 // needed. 963 if (SrcOpIdx1 == CommuteAnyOperandIndex || 964 SrcOpIdx2 == CommuteAnyOperandIndex) { 965 // At least one of operands to be commuted is not specified and 966 // this method is free to choose appropriate commutable operands. 967 unsigned CommutableOpIdx1 = SrcOpIdx1; 968 if (SrcOpIdx1 == SrcOpIdx2) { 969 // Both of operands are not fixed. Set one of commutable 970 // operands to the tied source. 971 CommutableOpIdx1 = 1; 972 } else if (SrcOpIdx1 == CommutableOpIdx1) { 973 // Only one of the operands is not fixed. 974 CommutableOpIdx1 = SrcOpIdx2; 975 } 976 977 // CommutableOpIdx1 is well defined now. Let's choose another commutable 978 // operand and assign its index to CommutableOpIdx2. 979 unsigned CommutableOpIdx2; 980 if (CommutableOpIdx1 != 1) { 981 // If we haven't already used the tied source, we must use it now. 982 CommutableOpIdx2 = 1; 983 } else { 984 Register Op1Reg = MI.getOperand(CommutableOpIdx1).getReg(); 985 986 // The commuted operands should have different registers. 987 // Otherwise, the commute transformation does not change anything and 988 // is useless. We use this as a hint to make our decision. 989 if (Op1Reg != MI.getOperand(2).getReg()) 990 CommutableOpIdx2 = 2; 991 else 992 CommutableOpIdx2 = 3; 993 } 994 995 // Assign the found pair of commutable indices to SrcOpIdx1 and 996 // SrcOpIdx2 to return those values. 997 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1, 998 CommutableOpIdx2)) 999 return false; 1000 } 1001 1002 return true; 1003 } 1004 } 1005 1006 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 1007 } 1008 1009 #define CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL) \ 1010 case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL##_COMMUTABLE: \ 1011 Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL##_COMMUTABLE; \ 1012 break; 1013 1014 #define CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE) \ 1015 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF8) \ 1016 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4) \ 1017 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2) \ 1018 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1) \ 1019 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2) \ 1020 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4) \ 1021 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8) 1022 1023 #define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP) \ 1024 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF16) \ 1025 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF32) \ 1026 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF64) 1027 1028 MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI, 1029 bool NewMI, 1030 unsigned OpIdx1, 1031 unsigned OpIdx2) const { 1032 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & { 1033 if (NewMI) 1034 return *MI.getParent()->getParent()->CloneMachineInstr(&MI); 1035 return MI; 1036 }; 1037 1038 switch (MI.getOpcode()) { 1039 case CASE_VFMA_SPLATS(FMACC): 1040 case CASE_VFMA_SPLATS(FMADD): 1041 case CASE_VFMA_SPLATS(FMSAC): 1042 case CASE_VFMA_SPLATS(FMSUB): 1043 case CASE_VFMA_SPLATS(FNMACC): 1044 case CASE_VFMA_SPLATS(FNMADD): 1045 case CASE_VFMA_SPLATS(FNMSAC): 1046 case CASE_VFMA_SPLATS(FNMSUB): 1047 case CASE_VFMA_OPCODE_LMULS(FMACC, VV): 1048 case CASE_VFMA_OPCODE_LMULS(FMSAC, VV): 1049 case CASE_VFMA_OPCODE_LMULS(FNMACC, VV): 1050 case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV): { 1051 // It only make sense to toggle these between clobbering the 1052 // addend/subtrahend/minuend one of the multiplicands. 1053 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index"); 1054 assert((OpIdx1 == 3 || OpIdx2 == 3) && "Unexpected opcode index"); 1055 unsigned Opc; 1056 switch (MI.getOpcode()) { 1057 default: 1058 llvm_unreachable("Unexpected opcode"); 1059 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMACC, FMADD) 1060 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMADD, FMACC) 1061 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSAC, FMSUB) 1062 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSUB, FMSAC) 1063 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMACC, FNMADD) 1064 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMADD, FNMACC) 1065 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSAC, FNMSUB) 1066 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSUB, FNMSAC) 1067 CASE_VFMA_CHANGE_OPCODE_LMULS(FMACC, FMADD, VV) 1068 CASE_VFMA_CHANGE_OPCODE_LMULS(FMSAC, FMSUB, VV) 1069 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMACC, FNMADD, VV) 1070 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSAC, FNMSUB, VV) 1071 } 1072 1073 auto &WorkingMI = cloneIfNew(MI); 1074 WorkingMI.setDesc(get(Opc)); 1075 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 1076 OpIdx1, OpIdx2); 1077 } 1078 case CASE_VFMA_OPCODE_LMULS(FMADD, VV): 1079 case CASE_VFMA_OPCODE_LMULS(FMSUB, VV): 1080 case CASE_VFMA_OPCODE_LMULS(FNMADD, VV): 1081 case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV): { 1082 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index"); 1083 // If one of the operands, is the addend we need to change opcode. 1084 // Otherwise we're just swapping 2 of the multiplicands. 1085 if (OpIdx1 == 3 || OpIdx2 == 3) { 1086 unsigned Opc; 1087 switch (MI.getOpcode()) { 1088 default: 1089 llvm_unreachable("Unexpected opcode"); 1090 CASE_VFMA_CHANGE_OPCODE_LMULS(FMADD, FMACC, VV) 1091 CASE_VFMA_CHANGE_OPCODE_LMULS(FMSUB, FMSAC, VV) 1092 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMADD, FNMACC, VV) 1093 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSUB, FNMSAC, VV) 1094 } 1095 1096 auto &WorkingMI = cloneIfNew(MI); 1097 WorkingMI.setDesc(get(Opc)); 1098 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 1099 OpIdx1, OpIdx2); 1100 } 1101 // Let the default code handle it. 1102 break; 1103 } 1104 } 1105 1106 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 1107 } 1108 1109 #undef CASE_VFMA_CHANGE_OPCODE_SPLATS 1110 #undef CASE_VFMA_CHANGE_OPCODE_LMULS 1111 #undef CASE_VFMA_CHANGE_OPCODE_COMMON 1112 #undef CASE_VFMA_SPLATS 1113 #undef CASE_VFMA_OPCODE_LMULS 1114 #undef CASE_VFMA_OPCODE_COMMON 1115