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