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