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 "RISCVMachineFunctionInfo.h" 17 #include "RISCVSubtarget.h" 18 #include "RISCVTargetMachine.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/Analysis/MemoryLocation.h" 22 #include "llvm/CodeGen/LiveVariables.h" 23 #include "llvm/CodeGen/MachineFunctionPass.h" 24 #include "llvm/CodeGen/MachineInstrBuilder.h" 25 #include "llvm/CodeGen/MachineRegisterInfo.h" 26 #include "llvm/CodeGen/RegisterScavenging.h" 27 #include "llvm/MC/MCInstBuilder.h" 28 #include "llvm/MC/TargetRegistry.h" 29 #include "llvm/Support/ErrorHandling.h" 30 31 using namespace llvm; 32 33 #define GEN_CHECK_COMPRESS_INSTR 34 #include "RISCVGenCompressInstEmitter.inc" 35 36 #define GET_INSTRINFO_CTOR_DTOR 37 #include "RISCVGenInstrInfo.inc" 38 39 namespace llvm { 40 namespace RISCVVPseudosTable { 41 42 using namespace RISCV; 43 44 #define GET_RISCVVPseudosTable_IMPL 45 #include "RISCVGenSearchableTables.inc" 46 47 } // namespace RISCVVPseudosTable 48 } // namespace llvm 49 50 RISCVInstrInfo::RISCVInstrInfo(RISCVSubtarget &STI) 51 : RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP), 52 STI(STI) {} 53 54 MCInst RISCVInstrInfo::getNop() const { 55 if (STI.getFeatureBits()[RISCV::FeatureStdExtC]) 56 return MCInstBuilder(RISCV::C_NOP); 57 return MCInstBuilder(RISCV::ADDI) 58 .addReg(RISCV::X0) 59 .addReg(RISCV::X0) 60 .addImm(0); 61 } 62 63 unsigned RISCVInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 64 int &FrameIndex) const { 65 switch (MI.getOpcode()) { 66 default: 67 return 0; 68 case RISCV::LB: 69 case RISCV::LBU: 70 case RISCV::LH: 71 case RISCV::LHU: 72 case RISCV::FLH: 73 case RISCV::LW: 74 case RISCV::FLW: 75 case RISCV::LWU: 76 case RISCV::LD: 77 case RISCV::FLD: 78 break; 79 } 80 81 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() && 82 MI.getOperand(2).getImm() == 0) { 83 FrameIndex = MI.getOperand(1).getIndex(); 84 return MI.getOperand(0).getReg(); 85 } 86 87 return 0; 88 } 89 90 unsigned RISCVInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 91 int &FrameIndex) const { 92 switch (MI.getOpcode()) { 93 default: 94 return 0; 95 case RISCV::SB: 96 case RISCV::SH: 97 case RISCV::SW: 98 case RISCV::FSH: 99 case RISCV::FSW: 100 case RISCV::SD: 101 case RISCV::FSD: 102 break; 103 } 104 105 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() && 106 MI.getOperand(2).getImm() == 0) { 107 FrameIndex = MI.getOperand(1).getIndex(); 108 return MI.getOperand(0).getReg(); 109 } 110 111 return 0; 112 } 113 114 static bool forwardCopyWillClobberTuple(unsigned DstReg, unsigned SrcReg, 115 unsigned NumRegs) { 116 return DstReg > SrcReg && (DstReg - SrcReg) < NumRegs; 117 } 118 119 void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 120 MachineBasicBlock::iterator MBBI, 121 const DebugLoc &DL, MCRegister DstReg, 122 MCRegister SrcReg, bool KillSrc) const { 123 if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) { 124 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg) 125 .addReg(SrcReg, getKillRegState(KillSrc)) 126 .addImm(0); 127 return; 128 } 129 130 // FPR->FPR copies and VR->VR copies. 131 unsigned Opc; 132 bool IsScalableVector = true; 133 unsigned NF = 1; 134 unsigned LMul = 1; 135 unsigned SubRegIdx = RISCV::sub_vrm1_0; 136 if (RISCV::FPR16RegClass.contains(DstReg, SrcReg)) { 137 Opc = RISCV::FSGNJ_H; 138 IsScalableVector = false; 139 } else if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) { 140 Opc = RISCV::FSGNJ_S; 141 IsScalableVector = false; 142 } else if (RISCV::FPR64RegClass.contains(DstReg, SrcReg)) { 143 Opc = RISCV::FSGNJ_D; 144 IsScalableVector = false; 145 } else if (RISCV::VRRegClass.contains(DstReg, SrcReg)) { 146 Opc = RISCV::PseudoVMV1R_V; 147 } else if (RISCV::VRM2RegClass.contains(DstReg, SrcReg)) { 148 Opc = RISCV::PseudoVMV2R_V; 149 } else if (RISCV::VRM4RegClass.contains(DstReg, SrcReg)) { 150 Opc = RISCV::PseudoVMV4R_V; 151 } else if (RISCV::VRM8RegClass.contains(DstReg, SrcReg)) { 152 Opc = RISCV::PseudoVMV8R_V; 153 } else if (RISCV::VRN2M1RegClass.contains(DstReg, SrcReg)) { 154 Opc = RISCV::PseudoVMV1R_V; 155 SubRegIdx = RISCV::sub_vrm1_0; 156 NF = 2; 157 LMul = 1; 158 } else if (RISCV::VRN2M2RegClass.contains(DstReg, SrcReg)) { 159 Opc = RISCV::PseudoVMV2R_V; 160 SubRegIdx = RISCV::sub_vrm2_0; 161 NF = 2; 162 LMul = 2; 163 } else if (RISCV::VRN2M4RegClass.contains(DstReg, SrcReg)) { 164 Opc = RISCV::PseudoVMV4R_V; 165 SubRegIdx = RISCV::sub_vrm4_0; 166 NF = 2; 167 LMul = 4; 168 } else if (RISCV::VRN3M1RegClass.contains(DstReg, SrcReg)) { 169 Opc = RISCV::PseudoVMV1R_V; 170 SubRegIdx = RISCV::sub_vrm1_0; 171 NF = 3; 172 LMul = 1; 173 } else if (RISCV::VRN3M2RegClass.contains(DstReg, SrcReg)) { 174 Opc = RISCV::PseudoVMV2R_V; 175 SubRegIdx = RISCV::sub_vrm2_0; 176 NF = 3; 177 LMul = 2; 178 } else if (RISCV::VRN4M1RegClass.contains(DstReg, SrcReg)) { 179 Opc = RISCV::PseudoVMV1R_V; 180 SubRegIdx = RISCV::sub_vrm1_0; 181 NF = 4; 182 LMul = 1; 183 } else if (RISCV::VRN4M2RegClass.contains(DstReg, SrcReg)) { 184 Opc = RISCV::PseudoVMV2R_V; 185 SubRegIdx = RISCV::sub_vrm2_0; 186 NF = 4; 187 LMul = 2; 188 } else if (RISCV::VRN5M1RegClass.contains(DstReg, SrcReg)) { 189 Opc = RISCV::PseudoVMV1R_V; 190 SubRegIdx = RISCV::sub_vrm1_0; 191 NF = 5; 192 LMul = 1; 193 } else if (RISCV::VRN6M1RegClass.contains(DstReg, SrcReg)) { 194 Opc = RISCV::PseudoVMV1R_V; 195 SubRegIdx = RISCV::sub_vrm1_0; 196 NF = 6; 197 LMul = 1; 198 } else if (RISCV::VRN7M1RegClass.contains(DstReg, SrcReg)) { 199 Opc = RISCV::PseudoVMV1R_V; 200 SubRegIdx = RISCV::sub_vrm1_0; 201 NF = 7; 202 LMul = 1; 203 } else if (RISCV::VRN8M1RegClass.contains(DstReg, SrcReg)) { 204 Opc = RISCV::PseudoVMV1R_V; 205 SubRegIdx = RISCV::sub_vrm1_0; 206 NF = 8; 207 LMul = 1; 208 } else { 209 llvm_unreachable("Impossible reg-to-reg copy"); 210 } 211 212 if (IsScalableVector) { 213 if (NF == 1) { 214 BuildMI(MBB, MBBI, DL, get(Opc), DstReg) 215 .addReg(SrcReg, getKillRegState(KillSrc)); 216 } else { 217 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); 218 219 int I = 0, End = NF, Incr = 1; 220 unsigned SrcEncoding = TRI->getEncodingValue(SrcReg); 221 unsigned DstEncoding = TRI->getEncodingValue(DstReg); 222 if (forwardCopyWillClobberTuple(DstEncoding, SrcEncoding, NF * LMul)) { 223 I = NF - 1; 224 End = -1; 225 Incr = -1; 226 } 227 228 for (; I != End; I += Incr) { 229 BuildMI(MBB, MBBI, DL, get(Opc), TRI->getSubReg(DstReg, SubRegIdx + I)) 230 .addReg(TRI->getSubReg(SrcReg, SubRegIdx + I), 231 getKillRegState(KillSrc)); 232 } 233 } 234 } else { 235 BuildMI(MBB, MBBI, DL, get(Opc), DstReg) 236 .addReg(SrcReg, getKillRegState(KillSrc)) 237 .addReg(SrcReg, getKillRegState(KillSrc)); 238 } 239 } 240 241 void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 242 MachineBasicBlock::iterator I, 243 Register SrcReg, bool IsKill, int FI, 244 const TargetRegisterClass *RC, 245 const TargetRegisterInfo *TRI) const { 246 DebugLoc DL; 247 if (I != MBB.end()) 248 DL = I->getDebugLoc(); 249 250 MachineFunction *MF = MBB.getParent(); 251 MachineFrameInfo &MFI = MF->getFrameInfo(); 252 253 unsigned Opcode; 254 bool IsScalableVector = true; 255 bool IsZvlsseg = true; 256 if (RISCV::GPRRegClass.hasSubClassEq(RC)) { 257 Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ? 258 RISCV::SW : RISCV::SD; 259 IsScalableVector = false; 260 } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) { 261 Opcode = RISCV::FSH; 262 IsScalableVector = false; 263 } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) { 264 Opcode = RISCV::FSW; 265 IsScalableVector = false; 266 } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) { 267 Opcode = RISCV::FSD; 268 IsScalableVector = false; 269 } else if (RISCV::VRRegClass.hasSubClassEq(RC)) { 270 Opcode = RISCV::PseudoVSPILL_M1; 271 IsZvlsseg = false; 272 } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) { 273 Opcode = RISCV::PseudoVSPILL_M2; 274 IsZvlsseg = false; 275 } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) { 276 Opcode = RISCV::PseudoVSPILL_M4; 277 IsZvlsseg = false; 278 } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) { 279 Opcode = RISCV::PseudoVSPILL_M8; 280 IsZvlsseg = false; 281 } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC)) 282 Opcode = RISCV::PseudoVSPILL2_M1; 283 else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC)) 284 Opcode = RISCV::PseudoVSPILL2_M2; 285 else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC)) 286 Opcode = RISCV::PseudoVSPILL2_M4; 287 else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC)) 288 Opcode = RISCV::PseudoVSPILL3_M1; 289 else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC)) 290 Opcode = RISCV::PseudoVSPILL3_M2; 291 else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC)) 292 Opcode = RISCV::PseudoVSPILL4_M1; 293 else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC)) 294 Opcode = RISCV::PseudoVSPILL4_M2; 295 else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC)) 296 Opcode = RISCV::PseudoVSPILL5_M1; 297 else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC)) 298 Opcode = RISCV::PseudoVSPILL6_M1; 299 else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC)) 300 Opcode = RISCV::PseudoVSPILL7_M1; 301 else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC)) 302 Opcode = RISCV::PseudoVSPILL8_M1; 303 else 304 llvm_unreachable("Can't store this register to stack slot"); 305 306 if (IsScalableVector) { 307 MachineMemOperand *MMO = MF->getMachineMemOperand( 308 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore, 309 MemoryLocation::UnknownSize, MFI.getObjectAlign(FI)); 310 311 MFI.setStackID(FI, TargetStackID::ScalableVector); 312 auto MIB = BuildMI(MBB, I, DL, get(Opcode)) 313 .addReg(SrcReg, getKillRegState(IsKill)) 314 .addFrameIndex(FI) 315 .addMemOperand(MMO); 316 if (IsZvlsseg) { 317 // For spilling/reloading Zvlsseg registers, append the dummy field for 318 // the scaled vector length. The argument will be used when expanding 319 // these pseudo instructions. 320 MIB.addReg(RISCV::X0); 321 } 322 } else { 323 MachineMemOperand *MMO = MF->getMachineMemOperand( 324 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore, 325 MFI.getObjectSize(FI), MFI.getObjectAlign(FI)); 326 327 BuildMI(MBB, I, DL, get(Opcode)) 328 .addReg(SrcReg, getKillRegState(IsKill)) 329 .addFrameIndex(FI) 330 .addImm(0) 331 .addMemOperand(MMO); 332 } 333 } 334 335 void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 336 MachineBasicBlock::iterator I, 337 Register DstReg, int FI, 338 const TargetRegisterClass *RC, 339 const TargetRegisterInfo *TRI) const { 340 DebugLoc DL; 341 if (I != MBB.end()) 342 DL = I->getDebugLoc(); 343 344 MachineFunction *MF = MBB.getParent(); 345 MachineFrameInfo &MFI = MF->getFrameInfo(); 346 347 unsigned Opcode; 348 bool IsScalableVector = true; 349 bool IsZvlsseg = true; 350 if (RISCV::GPRRegClass.hasSubClassEq(RC)) { 351 Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ? 352 RISCV::LW : RISCV::LD; 353 IsScalableVector = false; 354 } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) { 355 Opcode = RISCV::FLH; 356 IsScalableVector = false; 357 } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) { 358 Opcode = RISCV::FLW; 359 IsScalableVector = false; 360 } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) { 361 Opcode = RISCV::FLD; 362 IsScalableVector = false; 363 } else if (RISCV::VRRegClass.hasSubClassEq(RC)) { 364 Opcode = RISCV::PseudoVRELOAD_M1; 365 IsZvlsseg = false; 366 } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) { 367 Opcode = RISCV::PseudoVRELOAD_M2; 368 IsZvlsseg = false; 369 } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) { 370 Opcode = RISCV::PseudoVRELOAD_M4; 371 IsZvlsseg = false; 372 } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) { 373 Opcode = RISCV::PseudoVRELOAD_M8; 374 IsZvlsseg = false; 375 } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC)) 376 Opcode = RISCV::PseudoVRELOAD2_M1; 377 else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC)) 378 Opcode = RISCV::PseudoVRELOAD2_M2; 379 else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC)) 380 Opcode = RISCV::PseudoVRELOAD2_M4; 381 else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC)) 382 Opcode = RISCV::PseudoVRELOAD3_M1; 383 else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC)) 384 Opcode = RISCV::PseudoVRELOAD3_M2; 385 else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC)) 386 Opcode = RISCV::PseudoVRELOAD4_M1; 387 else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC)) 388 Opcode = RISCV::PseudoVRELOAD4_M2; 389 else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC)) 390 Opcode = RISCV::PseudoVRELOAD5_M1; 391 else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC)) 392 Opcode = RISCV::PseudoVRELOAD6_M1; 393 else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC)) 394 Opcode = RISCV::PseudoVRELOAD7_M1; 395 else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC)) 396 Opcode = RISCV::PseudoVRELOAD8_M1; 397 else 398 llvm_unreachable("Can't load this register from stack slot"); 399 400 if (IsScalableVector) { 401 MachineMemOperand *MMO = MF->getMachineMemOperand( 402 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad, 403 MemoryLocation::UnknownSize, MFI.getObjectAlign(FI)); 404 405 MFI.setStackID(FI, TargetStackID::ScalableVector); 406 auto MIB = BuildMI(MBB, I, DL, get(Opcode), DstReg) 407 .addFrameIndex(FI) 408 .addMemOperand(MMO); 409 if (IsZvlsseg) { 410 // For spilling/reloading Zvlsseg registers, append the dummy field for 411 // the scaled vector length. The argument will be used when expanding 412 // these pseudo instructions. 413 MIB.addReg(RISCV::X0); 414 } 415 } else { 416 MachineMemOperand *MMO = MF->getMachineMemOperand( 417 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad, 418 MFI.getObjectSize(FI), MFI.getObjectAlign(FI)); 419 420 BuildMI(MBB, I, DL, get(Opcode), DstReg) 421 .addFrameIndex(FI) 422 .addImm(0) 423 .addMemOperand(MMO); 424 } 425 } 426 427 void RISCVInstrInfo::movImm(MachineBasicBlock &MBB, 428 MachineBasicBlock::iterator MBBI, 429 const DebugLoc &DL, Register DstReg, uint64_t Val, 430 MachineInstr::MIFlag Flag) const { 431 MachineFunction *MF = MBB.getParent(); 432 MachineRegisterInfo &MRI = MF->getRegInfo(); 433 Register SrcReg = RISCV::X0; 434 Register Result = MRI.createVirtualRegister(&RISCV::GPRRegClass); 435 unsigned Num = 0; 436 437 if (!STI.is64Bit() && !isInt<32>(Val)) 438 report_fatal_error("Should only materialize 32-bit constants for RV32"); 439 440 RISCVMatInt::InstSeq Seq = 441 RISCVMatInt::generateInstSeq(Val, STI.getFeatureBits()); 442 assert(!Seq.empty()); 443 444 for (RISCVMatInt::Inst &Inst : Seq) { 445 // Write the final result to DstReg if it's the last instruction in the Seq. 446 // Otherwise, write the result to the temp register. 447 if (++Num == Seq.size()) 448 Result = DstReg; 449 450 if (Inst.Opc == RISCV::LUI) { 451 BuildMI(MBB, MBBI, DL, get(RISCV::LUI), Result) 452 .addImm(Inst.Imm) 453 .setMIFlag(Flag); 454 } else if (Inst.Opc == RISCV::ADDUW) { 455 BuildMI(MBB, MBBI, DL, get(RISCV::ADDUW), Result) 456 .addReg(SrcReg, RegState::Kill) 457 .addReg(RISCV::X0) 458 .setMIFlag(Flag); 459 } else if (Inst.Opc == RISCV::SH1ADD || Inst.Opc == RISCV::SH2ADD || 460 Inst.Opc == RISCV::SH3ADD) { 461 BuildMI(MBB, MBBI, DL, get(Inst.Opc), Result) 462 .addReg(SrcReg, RegState::Kill) 463 .addReg(SrcReg, RegState::Kill) 464 .setMIFlag(Flag); 465 } else { 466 BuildMI(MBB, MBBI, DL, get(Inst.Opc), Result) 467 .addReg(SrcReg, RegState::Kill) 468 .addImm(Inst.Imm) 469 .setMIFlag(Flag); 470 } 471 // Only the first instruction has X0 as its source. 472 SrcReg = Result; 473 } 474 } 475 476 static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) { 477 switch (Opc) { 478 default: 479 return RISCVCC::COND_INVALID; 480 case RISCV::BEQ: 481 return RISCVCC::COND_EQ; 482 case RISCV::BNE: 483 return RISCVCC::COND_NE; 484 case RISCV::BLT: 485 return RISCVCC::COND_LT; 486 case RISCV::BGE: 487 return RISCVCC::COND_GE; 488 case RISCV::BLTU: 489 return RISCVCC::COND_LTU; 490 case RISCV::BGEU: 491 return RISCVCC::COND_GEU; 492 } 493 } 494 495 // The contents of values added to Cond are not examined outside of 496 // RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we 497 // push BranchOpcode, Reg1, Reg2. 498 static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target, 499 SmallVectorImpl<MachineOperand> &Cond) { 500 // Block ends with fall-through condbranch. 501 assert(LastInst.getDesc().isConditionalBranch() && 502 "Unknown conditional branch"); 503 Target = LastInst.getOperand(2).getMBB(); 504 unsigned CC = getCondFromBranchOpc(LastInst.getOpcode()); 505 Cond.push_back(MachineOperand::CreateImm(CC)); 506 Cond.push_back(LastInst.getOperand(0)); 507 Cond.push_back(LastInst.getOperand(1)); 508 } 509 510 const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const { 511 switch (CC) { 512 default: 513 llvm_unreachable("Unknown condition code!"); 514 case RISCVCC::COND_EQ: 515 return get(RISCV::BEQ); 516 case RISCVCC::COND_NE: 517 return get(RISCV::BNE); 518 case RISCVCC::COND_LT: 519 return get(RISCV::BLT); 520 case RISCVCC::COND_GE: 521 return get(RISCV::BGE); 522 case RISCVCC::COND_LTU: 523 return get(RISCV::BLTU); 524 case RISCVCC::COND_GEU: 525 return get(RISCV::BGEU); 526 } 527 } 528 529 RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) { 530 switch (CC) { 531 default: 532 llvm_unreachable("Unrecognized conditional branch"); 533 case RISCVCC::COND_EQ: 534 return RISCVCC::COND_NE; 535 case RISCVCC::COND_NE: 536 return RISCVCC::COND_EQ; 537 case RISCVCC::COND_LT: 538 return RISCVCC::COND_GE; 539 case RISCVCC::COND_GE: 540 return RISCVCC::COND_LT; 541 case RISCVCC::COND_LTU: 542 return RISCVCC::COND_GEU; 543 case RISCVCC::COND_GEU: 544 return RISCVCC::COND_LTU; 545 } 546 } 547 548 bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 549 MachineBasicBlock *&TBB, 550 MachineBasicBlock *&FBB, 551 SmallVectorImpl<MachineOperand> &Cond, 552 bool AllowModify) const { 553 TBB = FBB = nullptr; 554 Cond.clear(); 555 556 // If the block has no terminators, it just falls into the block after it. 557 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 558 if (I == MBB.end() || !isUnpredicatedTerminator(*I)) 559 return false; 560 561 // Count the number of terminators and find the first unconditional or 562 // indirect branch. 563 MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end(); 564 int NumTerminators = 0; 565 for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J); 566 J++) { 567 NumTerminators++; 568 if (J->getDesc().isUnconditionalBranch() || 569 J->getDesc().isIndirectBranch()) { 570 FirstUncondOrIndirectBr = J.getReverse(); 571 } 572 } 573 574 // If AllowModify is true, we can erase any terminators after 575 // FirstUncondOrIndirectBR. 576 if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) { 577 while (std::next(FirstUncondOrIndirectBr) != MBB.end()) { 578 std::next(FirstUncondOrIndirectBr)->eraseFromParent(); 579 NumTerminators--; 580 } 581 I = FirstUncondOrIndirectBr; 582 } 583 584 // We can't handle blocks that end in an indirect branch. 585 if (I->getDesc().isIndirectBranch()) 586 return true; 587 588 // We can't handle blocks with more than 2 terminators. 589 if (NumTerminators > 2) 590 return true; 591 592 // Handle a single unconditional branch. 593 if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) { 594 TBB = getBranchDestBlock(*I); 595 return false; 596 } 597 598 // Handle a single conditional branch. 599 if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) { 600 parseCondBranch(*I, TBB, Cond); 601 return false; 602 } 603 604 // Handle a conditional branch followed by an unconditional branch. 605 if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() && 606 I->getDesc().isUnconditionalBranch()) { 607 parseCondBranch(*std::prev(I), TBB, Cond); 608 FBB = getBranchDestBlock(*I); 609 return false; 610 } 611 612 // Otherwise, we can't handle this. 613 return true; 614 } 615 616 unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB, 617 int *BytesRemoved) const { 618 if (BytesRemoved) 619 *BytesRemoved = 0; 620 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 621 if (I == MBB.end()) 622 return 0; 623 624 if (!I->getDesc().isUnconditionalBranch() && 625 !I->getDesc().isConditionalBranch()) 626 return 0; 627 628 // Remove the branch. 629 if (BytesRemoved) 630 *BytesRemoved += getInstSizeInBytes(*I); 631 I->eraseFromParent(); 632 633 I = MBB.end(); 634 635 if (I == MBB.begin()) 636 return 1; 637 --I; 638 if (!I->getDesc().isConditionalBranch()) 639 return 1; 640 641 // Remove the branch. 642 if (BytesRemoved) 643 *BytesRemoved += getInstSizeInBytes(*I); 644 I->eraseFromParent(); 645 return 2; 646 } 647 648 // Inserts a branch into the end of the specific MachineBasicBlock, returning 649 // the number of instructions inserted. 650 unsigned RISCVInstrInfo::insertBranch( 651 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, 652 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const { 653 if (BytesAdded) 654 *BytesAdded = 0; 655 656 // Shouldn't be a fall through. 657 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 658 assert((Cond.size() == 3 || Cond.size() == 0) && 659 "RISCV branch conditions have two components!"); 660 661 // Unconditional branch. 662 if (Cond.empty()) { 663 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB); 664 if (BytesAdded) 665 *BytesAdded += getInstSizeInBytes(MI); 666 return 1; 667 } 668 669 // Either a one or two-way conditional branch. 670 auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm()); 671 MachineInstr &CondMI = 672 *BuildMI(&MBB, DL, getBrCond(CC)).add(Cond[1]).add(Cond[2]).addMBB(TBB); 673 if (BytesAdded) 674 *BytesAdded += getInstSizeInBytes(CondMI); 675 676 // One-way conditional branch. 677 if (!FBB) 678 return 1; 679 680 // Two-way conditional branch. 681 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB); 682 if (BytesAdded) 683 *BytesAdded += getInstSizeInBytes(MI); 684 return 2; 685 } 686 687 unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, 688 MachineBasicBlock &DestBB, 689 const DebugLoc &DL, 690 int64_t BrOffset, 691 RegScavenger *RS) const { 692 assert(RS && "RegScavenger required for long branching"); 693 assert(MBB.empty() && 694 "new block should be inserted for expanding unconditional branch"); 695 assert(MBB.pred_size() == 1); 696 697 MachineFunction *MF = MBB.getParent(); 698 MachineRegisterInfo &MRI = MF->getRegInfo(); 699 700 if (!isInt<32>(BrOffset)) 701 report_fatal_error( 702 "Branch offsets outside of the signed 32-bit range not supported"); 703 704 // FIXME: A virtual register must be used initially, as the register 705 // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch 706 // uses the same workaround). 707 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); 708 auto II = MBB.end(); 709 710 MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump)) 711 .addReg(ScratchReg, RegState::Define | RegState::Dead) 712 .addMBB(&DestBB, RISCVII::MO_CALL); 713 714 RS->enterBasicBlockEnd(MBB); 715 unsigned Scav = RS->scavengeRegisterBackwards(RISCV::GPRRegClass, 716 MI.getIterator(), false, 0); 717 MRI.replaceRegWith(ScratchReg, Scav); 718 MRI.clearVirtRegs(); 719 RS->setRegUsed(Scav); 720 return 8; 721 } 722 723 bool RISCVInstrInfo::reverseBranchCondition( 724 SmallVectorImpl<MachineOperand> &Cond) const { 725 assert((Cond.size() == 3) && "Invalid branch condition!"); 726 auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm()); 727 Cond[0].setImm(getOppositeBranchCondition(CC)); 728 return false; 729 } 730 731 MachineBasicBlock * 732 RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const { 733 assert(MI.getDesc().isBranch() && "Unexpected opcode!"); 734 // The branch target is always the last operand. 735 int NumOp = MI.getNumExplicitOperands(); 736 return MI.getOperand(NumOp - 1).getMBB(); 737 } 738 739 bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp, 740 int64_t BrOffset) const { 741 unsigned XLen = STI.getXLen(); 742 // Ideally we could determine the supported branch offset from the 743 // RISCVII::FormMask, but this can't be used for Pseudo instructions like 744 // PseudoBR. 745 switch (BranchOp) { 746 default: 747 llvm_unreachable("Unexpected opcode!"); 748 case RISCV::BEQ: 749 case RISCV::BNE: 750 case RISCV::BLT: 751 case RISCV::BGE: 752 case RISCV::BLTU: 753 case RISCV::BGEU: 754 return isIntN(13, BrOffset); 755 case RISCV::JAL: 756 case RISCV::PseudoBR: 757 return isIntN(21, BrOffset); 758 case RISCV::PseudoJump: 759 return isIntN(32, SignExtend64(BrOffset + 0x800, XLen)); 760 } 761 } 762 763 unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 764 unsigned Opcode = MI.getOpcode(); 765 766 switch (Opcode) { 767 default: { 768 if (MI.getParent() && MI.getParent()->getParent()) { 769 const auto MF = MI.getMF(); 770 const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget()); 771 const MCRegisterInfo &MRI = *TM.getMCRegisterInfo(); 772 const MCSubtargetInfo &STI = *TM.getMCSubtargetInfo(); 773 const RISCVSubtarget &ST = MF->getSubtarget<RISCVSubtarget>(); 774 if (isCompressibleInst(MI, &ST, MRI, STI)) 775 return 2; 776 } 777 return get(Opcode).getSize(); 778 } 779 case TargetOpcode::EH_LABEL: 780 case TargetOpcode::IMPLICIT_DEF: 781 case TargetOpcode::KILL: 782 case TargetOpcode::DBG_VALUE: 783 return 0; 784 // These values are determined based on RISCVExpandAtomicPseudoInsts, 785 // RISCVExpandPseudoInsts and RISCVMCCodeEmitter, depending on where the 786 // pseudos are expanded. 787 case RISCV::PseudoCALLReg: 788 case RISCV::PseudoCALL: 789 case RISCV::PseudoJump: 790 case RISCV::PseudoTAIL: 791 case RISCV::PseudoLLA: 792 case RISCV::PseudoLA: 793 case RISCV::PseudoLA_TLS_IE: 794 case RISCV::PseudoLA_TLS_GD: 795 return 8; 796 case RISCV::PseudoAtomicLoadNand32: 797 case RISCV::PseudoAtomicLoadNand64: 798 return 20; 799 case RISCV::PseudoMaskedAtomicSwap32: 800 case RISCV::PseudoMaskedAtomicLoadAdd32: 801 case RISCV::PseudoMaskedAtomicLoadSub32: 802 return 28; 803 case RISCV::PseudoMaskedAtomicLoadNand32: 804 return 32; 805 case RISCV::PseudoMaskedAtomicLoadMax32: 806 case RISCV::PseudoMaskedAtomicLoadMin32: 807 return 44; 808 case RISCV::PseudoMaskedAtomicLoadUMax32: 809 case RISCV::PseudoMaskedAtomicLoadUMin32: 810 return 36; 811 case RISCV::PseudoCmpXchg32: 812 case RISCV::PseudoCmpXchg64: 813 return 16; 814 case RISCV::PseudoMaskedCmpXchg32: 815 return 32; 816 case TargetOpcode::INLINEASM: 817 case TargetOpcode::INLINEASM_BR: { 818 const MachineFunction &MF = *MI.getParent()->getParent(); 819 const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget()); 820 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), 821 *TM.getMCAsmInfo()); 822 } 823 case RISCV::PseudoVSPILL2_M1: 824 case RISCV::PseudoVSPILL2_M2: 825 case RISCV::PseudoVSPILL2_M4: 826 case RISCV::PseudoVSPILL3_M1: 827 case RISCV::PseudoVSPILL3_M2: 828 case RISCV::PseudoVSPILL4_M1: 829 case RISCV::PseudoVSPILL4_M2: 830 case RISCV::PseudoVSPILL5_M1: 831 case RISCV::PseudoVSPILL6_M1: 832 case RISCV::PseudoVSPILL7_M1: 833 case RISCV::PseudoVSPILL8_M1: 834 case RISCV::PseudoVRELOAD2_M1: 835 case RISCV::PseudoVRELOAD2_M2: 836 case RISCV::PseudoVRELOAD2_M4: 837 case RISCV::PseudoVRELOAD3_M1: 838 case RISCV::PseudoVRELOAD3_M2: 839 case RISCV::PseudoVRELOAD4_M1: 840 case RISCV::PseudoVRELOAD4_M2: 841 case RISCV::PseudoVRELOAD5_M1: 842 case RISCV::PseudoVRELOAD6_M1: 843 case RISCV::PseudoVRELOAD7_M1: 844 case RISCV::PseudoVRELOAD8_M1: { 845 // The values are determined based on expandVSPILL and expandVRELOAD that 846 // expand the pseudos depending on NF. 847 unsigned NF = isRVVSpillForZvlsseg(Opcode)->first; 848 return 4 * (2 * NF - 1); 849 } 850 } 851 } 852 853 bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const { 854 const unsigned Opcode = MI.getOpcode(); 855 switch (Opcode) { 856 default: 857 break; 858 case RISCV::FSGNJ_D: 859 case RISCV::FSGNJ_S: 860 // The canonical floating-point move is fsgnj rd, rs, rs. 861 return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() && 862 MI.getOperand(1).getReg() == MI.getOperand(2).getReg(); 863 case RISCV::ADDI: 864 case RISCV::ORI: 865 case RISCV::XORI: 866 return (MI.getOperand(1).isReg() && 867 MI.getOperand(1).getReg() == RISCV::X0) || 868 (MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0); 869 } 870 return MI.isAsCheapAsAMove(); 871 } 872 873 Optional<DestSourcePair> 874 RISCVInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const { 875 if (MI.isMoveReg()) 876 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 877 switch (MI.getOpcode()) { 878 default: 879 break; 880 case RISCV::ADDI: 881 // Operand 1 can be a frameindex but callers expect registers 882 if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() && 883 MI.getOperand(2).getImm() == 0) 884 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 885 break; 886 case RISCV::FSGNJ_D: 887 case RISCV::FSGNJ_S: 888 // The canonical floating-point move is fsgnj rd, rs, rs. 889 if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() && 890 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) 891 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 892 break; 893 } 894 return None; 895 } 896 897 bool RISCVInstrInfo::verifyInstruction(const MachineInstr &MI, 898 StringRef &ErrInfo) const { 899 const MCInstrInfo *MCII = STI.getInstrInfo(); 900 MCInstrDesc const &Desc = MCII->get(MI.getOpcode()); 901 902 for (auto &OI : enumerate(Desc.operands())) { 903 unsigned OpType = OI.value().OperandType; 904 if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM && 905 OpType <= RISCVOp::OPERAND_LAST_RISCV_IMM) { 906 const MachineOperand &MO = MI.getOperand(OI.index()); 907 if (MO.isImm()) { 908 int64_t Imm = MO.getImm(); 909 bool Ok; 910 switch (OpType) { 911 default: 912 llvm_unreachable("Unexpected operand type"); 913 case RISCVOp::OPERAND_UIMM2: 914 Ok = isUInt<2>(Imm); 915 break; 916 case RISCVOp::OPERAND_UIMM3: 917 Ok = isUInt<3>(Imm); 918 break; 919 case RISCVOp::OPERAND_UIMM4: 920 Ok = isUInt<4>(Imm); 921 break; 922 case RISCVOp::OPERAND_UIMM5: 923 Ok = isUInt<5>(Imm); 924 break; 925 case RISCVOp::OPERAND_UIMM7: 926 Ok = isUInt<7>(Imm); 927 break; 928 case RISCVOp::OPERAND_UIMM12: 929 Ok = isUInt<12>(Imm); 930 break; 931 case RISCVOp::OPERAND_SIMM12: 932 Ok = isInt<12>(Imm); 933 break; 934 case RISCVOp::OPERAND_UIMM20: 935 Ok = isUInt<20>(Imm); 936 break; 937 case RISCVOp::OPERAND_UIMMLOG2XLEN: 938 if (STI.getTargetTriple().isArch64Bit()) 939 Ok = isUInt<6>(Imm); 940 else 941 Ok = isUInt<5>(Imm); 942 break; 943 } 944 if (!Ok) { 945 ErrInfo = "Invalid immediate"; 946 return false; 947 } 948 } 949 } 950 } 951 952 return true; 953 } 954 955 // Return true if get the base operand, byte offset of an instruction and the 956 // memory width. Width is the size of memory that is being loaded/stored. 957 bool RISCVInstrInfo::getMemOperandWithOffsetWidth( 958 const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset, 959 unsigned &Width, const TargetRegisterInfo *TRI) const { 960 if (!LdSt.mayLoadOrStore()) 961 return false; 962 963 // Here we assume the standard RISC-V ISA, which uses a base+offset 964 // addressing mode. You'll need to relax these conditions to support custom 965 // load/stores instructions. 966 if (LdSt.getNumExplicitOperands() != 3) 967 return false; 968 if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isImm()) 969 return false; 970 971 if (!LdSt.hasOneMemOperand()) 972 return false; 973 974 Width = (*LdSt.memoperands_begin())->getSize(); 975 BaseReg = &LdSt.getOperand(1); 976 Offset = LdSt.getOperand(2).getImm(); 977 return true; 978 } 979 980 bool RISCVInstrInfo::areMemAccessesTriviallyDisjoint( 981 const MachineInstr &MIa, const MachineInstr &MIb) const { 982 assert(MIa.mayLoadOrStore() && "MIa must be a load or store."); 983 assert(MIb.mayLoadOrStore() && "MIb must be a load or store."); 984 985 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() || 986 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 987 return false; 988 989 // Retrieve the base register, offset from the base register and width. Width 990 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If 991 // base registers are identical, and the offset of a lower memory access + 992 // the width doesn't overlap the offset of a higher memory access, 993 // then the memory accesses are different. 994 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); 995 const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr; 996 int64_t OffsetA = 0, OffsetB = 0; 997 unsigned int WidthA = 0, WidthB = 0; 998 if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) && 999 getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) { 1000 if (BaseOpA->isIdenticalTo(*BaseOpB)) { 1001 int LowOffset = std::min(OffsetA, OffsetB); 1002 int HighOffset = std::max(OffsetA, OffsetB); 1003 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 1004 if (LowOffset + LowWidth <= HighOffset) 1005 return true; 1006 } 1007 } 1008 return false; 1009 } 1010 1011 std::pair<unsigned, unsigned> 1012 RISCVInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 1013 const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK; 1014 return std::make_pair(TF & Mask, TF & ~Mask); 1015 } 1016 1017 ArrayRef<std::pair<unsigned, const char *>> 1018 RISCVInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 1019 using namespace RISCVII; 1020 static const std::pair<unsigned, const char *> TargetFlags[] = { 1021 {MO_CALL, "riscv-call"}, 1022 {MO_PLT, "riscv-plt"}, 1023 {MO_LO, "riscv-lo"}, 1024 {MO_HI, "riscv-hi"}, 1025 {MO_PCREL_LO, "riscv-pcrel-lo"}, 1026 {MO_PCREL_HI, "riscv-pcrel-hi"}, 1027 {MO_GOT_HI, "riscv-got-hi"}, 1028 {MO_TPREL_LO, "riscv-tprel-lo"}, 1029 {MO_TPREL_HI, "riscv-tprel-hi"}, 1030 {MO_TPREL_ADD, "riscv-tprel-add"}, 1031 {MO_TLS_GOT_HI, "riscv-tls-got-hi"}, 1032 {MO_TLS_GD_HI, "riscv-tls-gd-hi"}}; 1033 return makeArrayRef(TargetFlags); 1034 } 1035 bool RISCVInstrInfo::isFunctionSafeToOutlineFrom( 1036 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const { 1037 const Function &F = MF.getFunction(); 1038 1039 // Can F be deduplicated by the linker? If it can, don't outline from it. 1040 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage()) 1041 return false; 1042 1043 // Don't outline from functions with section markings; the program could 1044 // expect that all the code is in the named section. 1045 if (F.hasSection()) 1046 return false; 1047 1048 // It's safe to outline from MF. 1049 return true; 1050 } 1051 1052 bool RISCVInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB, 1053 unsigned &Flags) const { 1054 // More accurate safety checking is done in getOutliningCandidateInfo. 1055 return true; 1056 } 1057 1058 // Enum values indicating how an outlined call should be constructed. 1059 enum MachineOutlinerConstructionID { 1060 MachineOutlinerDefault 1061 }; 1062 1063 outliner::OutlinedFunction RISCVInstrInfo::getOutliningCandidateInfo( 1064 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const { 1065 1066 // First we need to filter out candidates where the X5 register (IE t0) can't 1067 // be used to setup the function call. 1068 auto CannotInsertCall = [](outliner::Candidate &C) { 1069 const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo(); 1070 1071 C.initLRU(*TRI); 1072 LiveRegUnits LRU = C.LRU; 1073 return !LRU.available(RISCV::X5); 1074 }; 1075 1076 llvm::erase_if(RepeatedSequenceLocs, CannotInsertCall); 1077 1078 // If the sequence doesn't have enough candidates left, then we're done. 1079 if (RepeatedSequenceLocs.size() < 2) 1080 return outliner::OutlinedFunction(); 1081 1082 unsigned SequenceSize = 0; 1083 1084 auto I = RepeatedSequenceLocs[0].front(); 1085 auto E = std::next(RepeatedSequenceLocs[0].back()); 1086 for (; I != E; ++I) 1087 SequenceSize += getInstSizeInBytes(*I); 1088 1089 // call t0, function = 8 bytes. 1090 unsigned CallOverhead = 8; 1091 for (auto &C : RepeatedSequenceLocs) 1092 C.setCallInfo(MachineOutlinerDefault, CallOverhead); 1093 1094 // jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled. 1095 unsigned FrameOverhead = 4; 1096 if (RepeatedSequenceLocs[0].getMF()->getSubtarget() 1097 .getFeatureBits()[RISCV::FeatureStdExtC]) 1098 FrameOverhead = 2; 1099 1100 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize, 1101 FrameOverhead, MachineOutlinerDefault); 1102 } 1103 1104 outliner::InstrType 1105 RISCVInstrInfo::getOutliningType(MachineBasicBlock::iterator &MBBI, 1106 unsigned Flags) const { 1107 MachineInstr &MI = *MBBI; 1108 MachineBasicBlock *MBB = MI.getParent(); 1109 const TargetRegisterInfo *TRI = 1110 MBB->getParent()->getSubtarget().getRegisterInfo(); 1111 1112 // Positions generally can't safely be outlined. 1113 if (MI.isPosition()) { 1114 // We can manually strip out CFI instructions later. 1115 if (MI.isCFIInstruction()) 1116 return outliner::InstrType::Invisible; 1117 1118 return outliner::InstrType::Illegal; 1119 } 1120 1121 // Don't trust the user to write safe inline assembly. 1122 if (MI.isInlineAsm()) 1123 return outliner::InstrType::Illegal; 1124 1125 // We can't outline branches to other basic blocks. 1126 if (MI.isTerminator() && !MBB->succ_empty()) 1127 return outliner::InstrType::Illegal; 1128 1129 // We need support for tail calls to outlined functions before return 1130 // statements can be allowed. 1131 if (MI.isReturn()) 1132 return outliner::InstrType::Illegal; 1133 1134 // Don't allow modifying the X5 register which we use for return addresses for 1135 // these outlined functions. 1136 if (MI.modifiesRegister(RISCV::X5, TRI) || 1137 MI.getDesc().hasImplicitDefOfPhysReg(RISCV::X5)) 1138 return outliner::InstrType::Illegal; 1139 1140 // Make sure the operands don't reference something unsafe. 1141 for (const auto &MO : MI.operands()) 1142 if (MO.isMBB() || MO.isBlockAddress() || MO.isCPI() || MO.isJTI()) 1143 return outliner::InstrType::Illegal; 1144 1145 // Don't allow instructions which won't be materialized to impact outlining 1146 // analysis. 1147 if (MI.isMetaInstruction()) 1148 return outliner::InstrType::Invisible; 1149 1150 return outliner::InstrType::Legal; 1151 } 1152 1153 void RISCVInstrInfo::buildOutlinedFrame( 1154 MachineBasicBlock &MBB, MachineFunction &MF, 1155 const outliner::OutlinedFunction &OF) const { 1156 1157 // Strip out any CFI instructions 1158 bool Changed = true; 1159 while (Changed) { 1160 Changed = false; 1161 auto I = MBB.begin(); 1162 auto E = MBB.end(); 1163 for (; I != E; ++I) { 1164 if (I->isCFIInstruction()) { 1165 I->removeFromParent(); 1166 Changed = true; 1167 break; 1168 } 1169 } 1170 } 1171 1172 MBB.addLiveIn(RISCV::X5); 1173 1174 // Add in a return instruction to the end of the outlined frame. 1175 MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR)) 1176 .addReg(RISCV::X0, RegState::Define) 1177 .addReg(RISCV::X5) 1178 .addImm(0)); 1179 } 1180 1181 MachineBasicBlock::iterator RISCVInstrInfo::insertOutlinedCall( 1182 Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It, 1183 MachineFunction &MF, const outliner::Candidate &C) const { 1184 1185 // Add in a call instruction to the outlined function at the given location. 1186 It = MBB.insert(It, 1187 BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5) 1188 .addGlobalAddress(M.getNamedValue(MF.getName()), 0, 1189 RISCVII::MO_CALL)); 1190 return It; 1191 } 1192 1193 // clang-format off 1194 #define CASE_VFMA_OPCODE_COMMON(OP, TYPE, LMUL) \ 1195 RISCV::PseudoV##OP##_##TYPE##_##LMUL 1196 1197 #define CASE_VFMA_OPCODE_LMULS(OP, TYPE) \ 1198 CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF8): \ 1199 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF4): \ 1200 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF2): \ 1201 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M1): \ 1202 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M2): \ 1203 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M4): \ 1204 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M8) 1205 1206 #define CASE_VFMA_SPLATS(OP) \ 1207 CASE_VFMA_OPCODE_LMULS(OP, VF16): \ 1208 case CASE_VFMA_OPCODE_LMULS(OP, VF32): \ 1209 case CASE_VFMA_OPCODE_LMULS(OP, VF64) 1210 // clang-format on 1211 1212 bool RISCVInstrInfo::findCommutedOpIndices(const MachineInstr &MI, 1213 unsigned &SrcOpIdx1, 1214 unsigned &SrcOpIdx2) const { 1215 const MCInstrDesc &Desc = MI.getDesc(); 1216 if (!Desc.isCommutable()) 1217 return false; 1218 1219 switch (MI.getOpcode()) { 1220 case CASE_VFMA_SPLATS(FMADD): 1221 case CASE_VFMA_SPLATS(FMSUB): 1222 case CASE_VFMA_SPLATS(FMACC): 1223 case CASE_VFMA_SPLATS(FMSAC): 1224 case CASE_VFMA_SPLATS(FNMADD): 1225 case CASE_VFMA_SPLATS(FNMSUB): 1226 case CASE_VFMA_SPLATS(FNMACC): 1227 case CASE_VFMA_SPLATS(FNMSAC): 1228 case CASE_VFMA_OPCODE_LMULS(FMACC, VV): 1229 case CASE_VFMA_OPCODE_LMULS(FMSAC, VV): 1230 case CASE_VFMA_OPCODE_LMULS(FNMACC, VV): 1231 case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV): 1232 case CASE_VFMA_OPCODE_LMULS(MADD, VX): 1233 case CASE_VFMA_OPCODE_LMULS(NMSUB, VX): 1234 case CASE_VFMA_OPCODE_LMULS(MACC, VX): 1235 case CASE_VFMA_OPCODE_LMULS(NMSAC, VX): 1236 case CASE_VFMA_OPCODE_LMULS(MACC, VV): 1237 case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): { 1238 // If the tail policy is undisturbed we can't commute. 1239 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags)); 1240 if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0) 1241 return false; 1242 1243 // For these instructions we can only swap operand 1 and operand 3 by 1244 // changing the opcode. 1245 unsigned CommutableOpIdx1 = 1; 1246 unsigned CommutableOpIdx2 = 3; 1247 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1, 1248 CommutableOpIdx2)) 1249 return false; 1250 return true; 1251 } 1252 case CASE_VFMA_OPCODE_LMULS(FMADD, VV): 1253 case CASE_VFMA_OPCODE_LMULS(FMSUB, VV): 1254 case CASE_VFMA_OPCODE_LMULS(FNMADD, VV): 1255 case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV): 1256 case CASE_VFMA_OPCODE_LMULS(MADD, VV): 1257 case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): { 1258 // If the tail policy is undisturbed we can't commute. 1259 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags)); 1260 if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0) 1261 return false; 1262 1263 // For these instructions we have more freedom. We can commute with the 1264 // other multiplicand or with the addend/subtrahend/minuend. 1265 1266 // Any fixed operand must be from source 1, 2 or 3. 1267 if (SrcOpIdx1 != CommuteAnyOperandIndex && SrcOpIdx1 > 3) 1268 return false; 1269 if (SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx2 > 3) 1270 return false; 1271 1272 // It both ops are fixed one must be the tied source. 1273 if (SrcOpIdx1 != CommuteAnyOperandIndex && 1274 SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx1 != 1 && SrcOpIdx2 != 1) 1275 return false; 1276 1277 // Look for two different register operands assumed to be commutable 1278 // regardless of the FMA opcode. The FMA opcode is adjusted later if 1279 // needed. 1280 if (SrcOpIdx1 == CommuteAnyOperandIndex || 1281 SrcOpIdx2 == CommuteAnyOperandIndex) { 1282 // At least one of operands to be commuted is not specified and 1283 // this method is free to choose appropriate commutable operands. 1284 unsigned CommutableOpIdx1 = SrcOpIdx1; 1285 if (SrcOpIdx1 == SrcOpIdx2) { 1286 // Both of operands are not fixed. Set one of commutable 1287 // operands to the tied source. 1288 CommutableOpIdx1 = 1; 1289 } else if (SrcOpIdx1 == CommuteAnyOperandIndex) { 1290 // Only one of the operands is not fixed. 1291 CommutableOpIdx1 = SrcOpIdx2; 1292 } 1293 1294 // CommutableOpIdx1 is well defined now. Let's choose another commutable 1295 // operand and assign its index to CommutableOpIdx2. 1296 unsigned CommutableOpIdx2; 1297 if (CommutableOpIdx1 != 1) { 1298 // If we haven't already used the tied source, we must use it now. 1299 CommutableOpIdx2 = 1; 1300 } else { 1301 Register Op1Reg = MI.getOperand(CommutableOpIdx1).getReg(); 1302 1303 // The commuted operands should have different registers. 1304 // Otherwise, the commute transformation does not change anything and 1305 // is useless. We use this as a hint to make our decision. 1306 if (Op1Reg != MI.getOperand(2).getReg()) 1307 CommutableOpIdx2 = 2; 1308 else 1309 CommutableOpIdx2 = 3; 1310 } 1311 1312 // Assign the found pair of commutable indices to SrcOpIdx1 and 1313 // SrcOpIdx2 to return those values. 1314 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1, 1315 CommutableOpIdx2)) 1316 return false; 1317 } 1318 1319 return true; 1320 } 1321 } 1322 1323 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 1324 } 1325 1326 #define CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL) \ 1327 case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL: \ 1328 Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL; \ 1329 break; 1330 1331 #define CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE) \ 1332 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF8) \ 1333 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4) \ 1334 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2) \ 1335 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1) \ 1336 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2) \ 1337 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4) \ 1338 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8) 1339 1340 #define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP) \ 1341 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF16) \ 1342 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF32) \ 1343 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF64) 1344 1345 MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI, 1346 bool NewMI, 1347 unsigned OpIdx1, 1348 unsigned OpIdx2) const { 1349 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & { 1350 if (NewMI) 1351 return *MI.getParent()->getParent()->CloneMachineInstr(&MI); 1352 return MI; 1353 }; 1354 1355 switch (MI.getOpcode()) { 1356 case CASE_VFMA_SPLATS(FMACC): 1357 case CASE_VFMA_SPLATS(FMADD): 1358 case CASE_VFMA_SPLATS(FMSAC): 1359 case CASE_VFMA_SPLATS(FMSUB): 1360 case CASE_VFMA_SPLATS(FNMACC): 1361 case CASE_VFMA_SPLATS(FNMADD): 1362 case CASE_VFMA_SPLATS(FNMSAC): 1363 case CASE_VFMA_SPLATS(FNMSUB): 1364 case CASE_VFMA_OPCODE_LMULS(FMACC, VV): 1365 case CASE_VFMA_OPCODE_LMULS(FMSAC, VV): 1366 case CASE_VFMA_OPCODE_LMULS(FNMACC, VV): 1367 case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV): 1368 case CASE_VFMA_OPCODE_LMULS(MADD, VX): 1369 case CASE_VFMA_OPCODE_LMULS(NMSUB, VX): 1370 case CASE_VFMA_OPCODE_LMULS(MACC, VX): 1371 case CASE_VFMA_OPCODE_LMULS(NMSAC, VX): 1372 case CASE_VFMA_OPCODE_LMULS(MACC, VV): 1373 case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): { 1374 // It only make sense to toggle these between clobbering the 1375 // addend/subtrahend/minuend one of the multiplicands. 1376 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index"); 1377 assert((OpIdx1 == 3 || OpIdx2 == 3) && "Unexpected opcode index"); 1378 unsigned Opc; 1379 switch (MI.getOpcode()) { 1380 default: 1381 llvm_unreachable("Unexpected opcode"); 1382 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMACC, FMADD) 1383 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMADD, FMACC) 1384 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSAC, FMSUB) 1385 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSUB, FMSAC) 1386 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMACC, FNMADD) 1387 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMADD, FNMACC) 1388 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSAC, FNMSUB) 1389 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSUB, FNMSAC) 1390 CASE_VFMA_CHANGE_OPCODE_LMULS(FMACC, FMADD, VV) 1391 CASE_VFMA_CHANGE_OPCODE_LMULS(FMSAC, FMSUB, VV) 1392 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMACC, FNMADD, VV) 1393 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSAC, FNMSUB, VV) 1394 CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VX) 1395 CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VX) 1396 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VX) 1397 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VX) 1398 CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VV) 1399 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VV) 1400 } 1401 1402 auto &WorkingMI = cloneIfNew(MI); 1403 WorkingMI.setDesc(get(Opc)); 1404 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 1405 OpIdx1, OpIdx2); 1406 } 1407 case CASE_VFMA_OPCODE_LMULS(FMADD, VV): 1408 case CASE_VFMA_OPCODE_LMULS(FMSUB, VV): 1409 case CASE_VFMA_OPCODE_LMULS(FNMADD, VV): 1410 case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV): 1411 case CASE_VFMA_OPCODE_LMULS(MADD, VV): 1412 case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): { 1413 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index"); 1414 // If one of the operands, is the addend we need to change opcode. 1415 // Otherwise we're just swapping 2 of the multiplicands. 1416 if (OpIdx1 == 3 || OpIdx2 == 3) { 1417 unsigned Opc; 1418 switch (MI.getOpcode()) { 1419 default: 1420 llvm_unreachable("Unexpected opcode"); 1421 CASE_VFMA_CHANGE_OPCODE_LMULS(FMADD, FMACC, VV) 1422 CASE_VFMA_CHANGE_OPCODE_LMULS(FMSUB, FMSAC, VV) 1423 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMADD, FNMACC, VV) 1424 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSUB, FNMSAC, VV) 1425 CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VV) 1426 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VV) 1427 } 1428 1429 auto &WorkingMI = cloneIfNew(MI); 1430 WorkingMI.setDesc(get(Opc)); 1431 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 1432 OpIdx1, OpIdx2); 1433 } 1434 // Let the default code handle it. 1435 break; 1436 } 1437 } 1438 1439 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 1440 } 1441 1442 #undef CASE_VFMA_CHANGE_OPCODE_SPLATS 1443 #undef CASE_VFMA_CHANGE_OPCODE_LMULS 1444 #undef CASE_VFMA_CHANGE_OPCODE_COMMON 1445 #undef CASE_VFMA_SPLATS 1446 #undef CASE_VFMA_OPCODE_LMULS 1447 #undef CASE_VFMA_OPCODE_COMMON 1448 1449 // clang-format off 1450 #define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL) \ 1451 RISCV::PseudoV##OP##_##LMUL##_TIED 1452 1453 #define CASE_WIDEOP_OPCODE_LMULS(OP) \ 1454 CASE_WIDEOP_OPCODE_COMMON(OP, MF8): \ 1455 case CASE_WIDEOP_OPCODE_COMMON(OP, MF4): \ 1456 case CASE_WIDEOP_OPCODE_COMMON(OP, MF2): \ 1457 case CASE_WIDEOP_OPCODE_COMMON(OP, M1): \ 1458 case CASE_WIDEOP_OPCODE_COMMON(OP, M2): \ 1459 case CASE_WIDEOP_OPCODE_COMMON(OP, M4) 1460 // clang-format on 1461 1462 #define CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL) \ 1463 case RISCV::PseudoV##OP##_##LMUL##_TIED: \ 1464 NewOpc = RISCV::PseudoV##OP##_##LMUL; \ 1465 break; 1466 1467 #define CASE_WIDEOP_CHANGE_OPCODE_LMULS(OP) \ 1468 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF8) \ 1469 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4) \ 1470 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2) \ 1471 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1) \ 1472 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2) \ 1473 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4) 1474 1475 MachineInstr *RISCVInstrInfo::convertToThreeAddress(MachineInstr &MI, 1476 LiveVariables *LV) const { 1477 switch (MI.getOpcode()) { 1478 default: 1479 break; 1480 case CASE_WIDEOP_OPCODE_LMULS(FWADD_WV): 1481 case CASE_WIDEOP_OPCODE_LMULS(FWSUB_WV): 1482 case CASE_WIDEOP_OPCODE_LMULS(WADD_WV): 1483 case CASE_WIDEOP_OPCODE_LMULS(WADDU_WV): 1484 case CASE_WIDEOP_OPCODE_LMULS(WSUB_WV): 1485 case CASE_WIDEOP_OPCODE_LMULS(WSUBU_WV): { 1486 // clang-format off 1487 unsigned NewOpc; 1488 switch (MI.getOpcode()) { 1489 default: 1490 llvm_unreachable("Unexpected opcode"); 1491 CASE_WIDEOP_CHANGE_OPCODE_LMULS(FWADD_WV) 1492 CASE_WIDEOP_CHANGE_OPCODE_LMULS(FWSUB_WV) 1493 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADD_WV) 1494 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADDU_WV) 1495 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUB_WV) 1496 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUBU_WV) 1497 } 1498 //clang-format on 1499 1500 MachineBasicBlock &MBB = *MI.getParent(); 1501 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc)) 1502 .add(MI.getOperand(0)) 1503 .add(MI.getOperand(1)) 1504 .add(MI.getOperand(2)) 1505 .add(MI.getOperand(3)) 1506 .add(MI.getOperand(4)); 1507 MIB.copyImplicitOps(MI); 1508 1509 if (LV) { 1510 unsigned NumOps = MI.getNumOperands(); 1511 for (unsigned I = 1; I < NumOps; ++I) { 1512 MachineOperand &Op = MI.getOperand(I); 1513 if (Op.isReg() && Op.isKill()) 1514 LV->replaceKillInstruction(Op.getReg(), MI, *MIB); 1515 } 1516 } 1517 1518 return MIB; 1519 } 1520 } 1521 1522 return nullptr; 1523 } 1524 1525 #undef CASE_WIDEOP_CHANGE_OPCODE_LMULS 1526 #undef CASE_WIDEOP_CHANGE_OPCODE_COMMON 1527 #undef CASE_WIDEOP_OPCODE_LMULS 1528 #undef CASE_WIDEOP_OPCODE_COMMON 1529 1530 Register RISCVInstrInfo::getVLENFactoredAmount(MachineFunction &MF, 1531 MachineBasicBlock &MBB, 1532 MachineBasicBlock::iterator II, 1533 const DebugLoc &DL, 1534 int64_t Amount, 1535 MachineInstr::MIFlag Flag) const { 1536 assert(Amount > 0 && "There is no need to get VLEN scaled value."); 1537 assert(Amount % 8 == 0 && 1538 "Reserve the stack by the multiple of one vector size."); 1539 1540 MachineRegisterInfo &MRI = MF.getRegInfo(); 1541 const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo(); 1542 int64_t NumOfVReg = Amount / 8; 1543 1544 Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1545 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VL) 1546 .setMIFlag(Flag); 1547 assert(isInt<32>(NumOfVReg) && 1548 "Expect the number of vector registers within 32-bits."); 1549 if (isPowerOf2_32(NumOfVReg)) { 1550 uint32_t ShiftAmount = Log2_32(NumOfVReg); 1551 if (ShiftAmount == 0) 1552 return VL; 1553 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VL) 1554 .addReg(VL, RegState::Kill) 1555 .addImm(ShiftAmount) 1556 .setMIFlag(Flag); 1557 } else if (isPowerOf2_32(NumOfVReg - 1)) { 1558 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1559 uint32_t ShiftAmount = Log2_32(NumOfVReg - 1); 1560 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), ScaledRegister) 1561 .addReg(VL) 1562 .addImm(ShiftAmount) 1563 .setMIFlag(Flag); 1564 BuildMI(MBB, II, DL, TII->get(RISCV::ADD), VL) 1565 .addReg(ScaledRegister, RegState::Kill) 1566 .addReg(VL, RegState::Kill) 1567 .setMIFlag(Flag); 1568 } else if (isPowerOf2_32(NumOfVReg + 1)) { 1569 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1570 uint32_t ShiftAmount = Log2_32(NumOfVReg + 1); 1571 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), ScaledRegister) 1572 .addReg(VL) 1573 .addImm(ShiftAmount) 1574 .setMIFlag(Flag); 1575 BuildMI(MBB, II, DL, TII->get(RISCV::SUB), VL) 1576 .addReg(ScaledRegister, RegState::Kill) 1577 .addReg(VL, RegState::Kill) 1578 .setMIFlag(Flag); 1579 } else { 1580 Register N = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1581 if (!isInt<12>(NumOfVReg)) 1582 movImm(MBB, II, DL, N, NumOfVReg); 1583 else { 1584 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), N) 1585 .addReg(RISCV::X0) 1586 .addImm(NumOfVReg) 1587 .setMIFlag(Flag); 1588 } 1589 if (!MF.getSubtarget<RISCVSubtarget>().hasStdExtM()) 1590 MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{ 1591 MF.getFunction(), 1592 "M-extension must be enabled to calculate the vscaled size/offset."}); 1593 BuildMI(MBB, II, DL, TII->get(RISCV::MUL), VL) 1594 .addReg(VL, RegState::Kill) 1595 .addReg(N, RegState::Kill) 1596 .setMIFlag(Flag); 1597 } 1598 1599 return VL; 1600 } 1601 1602 static bool isRVVWholeLoadStore(unsigned Opcode) { 1603 switch (Opcode) { 1604 default: 1605 return false; 1606 case RISCV::VS1R_V: 1607 case RISCV::VS2R_V: 1608 case RISCV::VS4R_V: 1609 case RISCV::VS8R_V: 1610 case RISCV::VL1RE8_V: 1611 case RISCV::VL2RE8_V: 1612 case RISCV::VL4RE8_V: 1613 case RISCV::VL8RE8_V: 1614 case RISCV::VL1RE16_V: 1615 case RISCV::VL2RE16_V: 1616 case RISCV::VL4RE16_V: 1617 case RISCV::VL8RE16_V: 1618 case RISCV::VL1RE32_V: 1619 case RISCV::VL2RE32_V: 1620 case RISCV::VL4RE32_V: 1621 case RISCV::VL8RE32_V: 1622 case RISCV::VL1RE64_V: 1623 case RISCV::VL2RE64_V: 1624 case RISCV::VL4RE64_V: 1625 case RISCV::VL8RE64_V: 1626 return true; 1627 } 1628 } 1629 1630 bool RISCVInstrInfo::isRVVSpill(const MachineInstr &MI, bool CheckFIs) const { 1631 // RVV lacks any support for immediate addressing for stack addresses, so be 1632 // conservative. 1633 unsigned Opcode = MI.getOpcode(); 1634 if (!RISCVVPseudosTable::getPseudoInfo(Opcode) && 1635 !isRVVWholeLoadStore(Opcode) && !isRVVSpillForZvlsseg(Opcode)) 1636 return false; 1637 return !CheckFIs || any_of(MI.operands(), [](const MachineOperand &MO) { 1638 return MO.isFI(); 1639 }); 1640 } 1641 1642 Optional<std::pair<unsigned, unsigned>> 1643 RISCVInstrInfo::isRVVSpillForZvlsseg(unsigned Opcode) const { 1644 switch (Opcode) { 1645 default: 1646 return None; 1647 case RISCV::PseudoVSPILL2_M1: 1648 case RISCV::PseudoVRELOAD2_M1: 1649 return std::make_pair(2u, 1u); 1650 case RISCV::PseudoVSPILL2_M2: 1651 case RISCV::PseudoVRELOAD2_M2: 1652 return std::make_pair(2u, 2u); 1653 case RISCV::PseudoVSPILL2_M4: 1654 case RISCV::PseudoVRELOAD2_M4: 1655 return std::make_pair(2u, 4u); 1656 case RISCV::PseudoVSPILL3_M1: 1657 case RISCV::PseudoVRELOAD3_M1: 1658 return std::make_pair(3u, 1u); 1659 case RISCV::PseudoVSPILL3_M2: 1660 case RISCV::PseudoVRELOAD3_M2: 1661 return std::make_pair(3u, 2u); 1662 case RISCV::PseudoVSPILL4_M1: 1663 case RISCV::PseudoVRELOAD4_M1: 1664 return std::make_pair(4u, 1u); 1665 case RISCV::PseudoVSPILL4_M2: 1666 case RISCV::PseudoVRELOAD4_M2: 1667 return std::make_pair(4u, 2u); 1668 case RISCV::PseudoVSPILL5_M1: 1669 case RISCV::PseudoVRELOAD5_M1: 1670 return std::make_pair(5u, 1u); 1671 case RISCV::PseudoVSPILL6_M1: 1672 case RISCV::PseudoVRELOAD6_M1: 1673 return std::make_pair(6u, 1u); 1674 case RISCV::PseudoVSPILL7_M1: 1675 case RISCV::PseudoVRELOAD7_M1: 1676 return std::make_pair(7u, 1u); 1677 case RISCV::PseudoVSPILL8_M1: 1678 case RISCV::PseudoVRELOAD8_M1: 1679 return std::make_pair(8u, 1u); 1680 } 1681 } 1682