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 case RISCV::FSGNJ_H: 1064 // The canonical floating-point move is fsgnj rd, rs, rs. 1065 return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() && 1066 MI.getOperand(1).getReg() == MI.getOperand(2).getReg(); 1067 case RISCV::ADDI: 1068 case RISCV::ORI: 1069 case RISCV::XORI: 1070 return (MI.getOperand(1).isReg() && 1071 MI.getOperand(1).getReg() == RISCV::X0) || 1072 (MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0); 1073 } 1074 return MI.isAsCheapAsAMove(); 1075 } 1076 1077 Optional<DestSourcePair> 1078 RISCVInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const { 1079 if (MI.isMoveReg()) 1080 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 1081 switch (MI.getOpcode()) { 1082 default: 1083 break; 1084 case RISCV::ADDI: 1085 // Operand 1 can be a frameindex but callers expect registers 1086 if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() && 1087 MI.getOperand(2).getImm() == 0) 1088 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 1089 break; 1090 case RISCV::FSGNJ_D: 1091 case RISCV::FSGNJ_S: 1092 case RISCV::FSGNJ_H: 1093 // The canonical floating-point move is fsgnj rd, rs, rs. 1094 if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() && 1095 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) 1096 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 1097 break; 1098 } 1099 return None; 1100 } 1101 1102 bool RISCVInstrInfo::verifyInstruction(const MachineInstr &MI, 1103 StringRef &ErrInfo) const { 1104 const MCInstrInfo *MCII = STI.getInstrInfo(); 1105 MCInstrDesc const &Desc = MCII->get(MI.getOpcode()); 1106 1107 for (auto &OI : enumerate(Desc.operands())) { 1108 unsigned OpType = OI.value().OperandType; 1109 if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM && 1110 OpType <= RISCVOp::OPERAND_LAST_RISCV_IMM) { 1111 const MachineOperand &MO = MI.getOperand(OI.index()); 1112 if (MO.isImm()) { 1113 int64_t Imm = MO.getImm(); 1114 bool Ok; 1115 switch (OpType) { 1116 default: 1117 llvm_unreachable("Unexpected operand type"); 1118 case RISCVOp::OPERAND_UIMM2: 1119 Ok = isUInt<2>(Imm); 1120 break; 1121 case RISCVOp::OPERAND_UIMM3: 1122 Ok = isUInt<3>(Imm); 1123 break; 1124 case RISCVOp::OPERAND_UIMM4: 1125 Ok = isUInt<4>(Imm); 1126 break; 1127 case RISCVOp::OPERAND_UIMM5: 1128 Ok = isUInt<5>(Imm); 1129 break; 1130 case RISCVOp::OPERAND_UIMM7: 1131 Ok = isUInt<7>(Imm); 1132 break; 1133 case RISCVOp::OPERAND_UIMM12: 1134 Ok = isUInt<12>(Imm); 1135 break; 1136 case RISCVOp::OPERAND_SIMM12: 1137 Ok = isInt<12>(Imm); 1138 break; 1139 case RISCVOp::OPERAND_UIMM20: 1140 Ok = isUInt<20>(Imm); 1141 break; 1142 case RISCVOp::OPERAND_UIMMLOG2XLEN: 1143 if (STI.getTargetTriple().isArch64Bit()) 1144 Ok = isUInt<6>(Imm); 1145 else 1146 Ok = isUInt<5>(Imm); 1147 break; 1148 } 1149 if (!Ok) { 1150 ErrInfo = "Invalid immediate"; 1151 return false; 1152 } 1153 } 1154 } 1155 } 1156 1157 return true; 1158 } 1159 1160 // Return true if get the base operand, byte offset of an instruction and the 1161 // memory width. Width is the size of memory that is being loaded/stored. 1162 bool RISCVInstrInfo::getMemOperandWithOffsetWidth( 1163 const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset, 1164 unsigned &Width, const TargetRegisterInfo *TRI) const { 1165 if (!LdSt.mayLoadOrStore()) 1166 return false; 1167 1168 // Here we assume the standard RISC-V ISA, which uses a base+offset 1169 // addressing mode. You'll need to relax these conditions to support custom 1170 // load/stores instructions. 1171 if (LdSt.getNumExplicitOperands() != 3) 1172 return false; 1173 if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isImm()) 1174 return false; 1175 1176 if (!LdSt.hasOneMemOperand()) 1177 return false; 1178 1179 Width = (*LdSt.memoperands_begin())->getSize(); 1180 BaseReg = &LdSt.getOperand(1); 1181 Offset = LdSt.getOperand(2).getImm(); 1182 return true; 1183 } 1184 1185 bool RISCVInstrInfo::areMemAccessesTriviallyDisjoint( 1186 const MachineInstr &MIa, const MachineInstr &MIb) const { 1187 assert(MIa.mayLoadOrStore() && "MIa must be a load or store."); 1188 assert(MIb.mayLoadOrStore() && "MIb must be a load or store."); 1189 1190 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() || 1191 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 1192 return false; 1193 1194 // Retrieve the base register, offset from the base register and width. Width 1195 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If 1196 // base registers are identical, and the offset of a lower memory access + 1197 // the width doesn't overlap the offset of a higher memory access, 1198 // then the memory accesses are different. 1199 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); 1200 const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr; 1201 int64_t OffsetA = 0, OffsetB = 0; 1202 unsigned int WidthA = 0, WidthB = 0; 1203 if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) && 1204 getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) { 1205 if (BaseOpA->isIdenticalTo(*BaseOpB)) { 1206 int LowOffset = std::min(OffsetA, OffsetB); 1207 int HighOffset = std::max(OffsetA, OffsetB); 1208 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 1209 if (LowOffset + LowWidth <= HighOffset) 1210 return true; 1211 } 1212 } 1213 return false; 1214 } 1215 1216 std::pair<unsigned, unsigned> 1217 RISCVInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 1218 const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK; 1219 return std::make_pair(TF & Mask, TF & ~Mask); 1220 } 1221 1222 ArrayRef<std::pair<unsigned, const char *>> 1223 RISCVInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 1224 using namespace RISCVII; 1225 static const std::pair<unsigned, const char *> TargetFlags[] = { 1226 {MO_CALL, "riscv-call"}, 1227 {MO_PLT, "riscv-plt"}, 1228 {MO_LO, "riscv-lo"}, 1229 {MO_HI, "riscv-hi"}, 1230 {MO_PCREL_LO, "riscv-pcrel-lo"}, 1231 {MO_PCREL_HI, "riscv-pcrel-hi"}, 1232 {MO_GOT_HI, "riscv-got-hi"}, 1233 {MO_TPREL_LO, "riscv-tprel-lo"}, 1234 {MO_TPREL_HI, "riscv-tprel-hi"}, 1235 {MO_TPREL_ADD, "riscv-tprel-add"}, 1236 {MO_TLS_GOT_HI, "riscv-tls-got-hi"}, 1237 {MO_TLS_GD_HI, "riscv-tls-gd-hi"}}; 1238 return makeArrayRef(TargetFlags); 1239 } 1240 bool RISCVInstrInfo::isFunctionSafeToOutlineFrom( 1241 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const { 1242 const Function &F = MF.getFunction(); 1243 1244 // Can F be deduplicated by the linker? If it can, don't outline from it. 1245 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage()) 1246 return false; 1247 1248 // Don't outline from functions with section markings; the program could 1249 // expect that all the code is in the named section. 1250 if (F.hasSection()) 1251 return false; 1252 1253 // It's safe to outline from MF. 1254 return true; 1255 } 1256 1257 bool RISCVInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB, 1258 unsigned &Flags) const { 1259 // More accurate safety checking is done in getOutliningCandidateInfo. 1260 return TargetInstrInfo::isMBBSafeToOutlineFrom(MBB, Flags); 1261 } 1262 1263 // Enum values indicating how an outlined call should be constructed. 1264 enum MachineOutlinerConstructionID { 1265 MachineOutlinerDefault 1266 }; 1267 1268 outliner::OutlinedFunction RISCVInstrInfo::getOutliningCandidateInfo( 1269 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const { 1270 1271 // First we need to filter out candidates where the X5 register (IE t0) can't 1272 // be used to setup the function call. 1273 auto CannotInsertCall = [](outliner::Candidate &C) { 1274 const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo(); 1275 1276 C.initLRU(*TRI); 1277 LiveRegUnits LRU = C.LRU; 1278 return !LRU.available(RISCV::X5); 1279 }; 1280 1281 llvm::erase_if(RepeatedSequenceLocs, CannotInsertCall); 1282 1283 // If the sequence doesn't have enough candidates left, then we're done. 1284 if (RepeatedSequenceLocs.size() < 2) 1285 return outliner::OutlinedFunction(); 1286 1287 unsigned SequenceSize = 0; 1288 1289 auto I = RepeatedSequenceLocs[0].front(); 1290 auto E = std::next(RepeatedSequenceLocs[0].back()); 1291 for (; I != E; ++I) 1292 SequenceSize += getInstSizeInBytes(*I); 1293 1294 // call t0, function = 8 bytes. 1295 unsigned CallOverhead = 8; 1296 for (auto &C : RepeatedSequenceLocs) 1297 C.setCallInfo(MachineOutlinerDefault, CallOverhead); 1298 1299 // jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled. 1300 unsigned FrameOverhead = 4; 1301 if (RepeatedSequenceLocs[0].getMF()->getSubtarget() 1302 .getFeatureBits()[RISCV::FeatureStdExtC]) 1303 FrameOverhead = 2; 1304 1305 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize, 1306 FrameOverhead, MachineOutlinerDefault); 1307 } 1308 1309 outliner::InstrType 1310 RISCVInstrInfo::getOutliningType(MachineBasicBlock::iterator &MBBI, 1311 unsigned Flags) const { 1312 MachineInstr &MI = *MBBI; 1313 MachineBasicBlock *MBB = MI.getParent(); 1314 const TargetRegisterInfo *TRI = 1315 MBB->getParent()->getSubtarget().getRegisterInfo(); 1316 1317 // Positions generally can't safely be outlined. 1318 if (MI.isPosition()) { 1319 // We can manually strip out CFI instructions later. 1320 if (MI.isCFIInstruction()) 1321 return outliner::InstrType::Invisible; 1322 1323 return outliner::InstrType::Illegal; 1324 } 1325 1326 // Don't trust the user to write safe inline assembly. 1327 if (MI.isInlineAsm()) 1328 return outliner::InstrType::Illegal; 1329 1330 // We can't outline branches to other basic blocks. 1331 if (MI.isTerminator() && !MBB->succ_empty()) 1332 return outliner::InstrType::Illegal; 1333 1334 // We need support for tail calls to outlined functions before return 1335 // statements can be allowed. 1336 if (MI.isReturn()) 1337 return outliner::InstrType::Illegal; 1338 1339 // Don't allow modifying the X5 register which we use for return addresses for 1340 // these outlined functions. 1341 if (MI.modifiesRegister(RISCV::X5, TRI) || 1342 MI.getDesc().hasImplicitDefOfPhysReg(RISCV::X5)) 1343 return outliner::InstrType::Illegal; 1344 1345 // Make sure the operands don't reference something unsafe. 1346 for (const auto &MO : MI.operands()) 1347 if (MO.isMBB() || MO.isBlockAddress() || MO.isCPI() || MO.isJTI()) 1348 return outliner::InstrType::Illegal; 1349 1350 // Don't allow instructions which won't be materialized to impact outlining 1351 // analysis. 1352 if (MI.isMetaInstruction()) 1353 return outliner::InstrType::Invisible; 1354 1355 return outliner::InstrType::Legal; 1356 } 1357 1358 void RISCVInstrInfo::buildOutlinedFrame( 1359 MachineBasicBlock &MBB, MachineFunction &MF, 1360 const outliner::OutlinedFunction &OF) const { 1361 1362 // Strip out any CFI instructions 1363 bool Changed = true; 1364 while (Changed) { 1365 Changed = false; 1366 auto I = MBB.begin(); 1367 auto E = MBB.end(); 1368 for (; I != E; ++I) { 1369 if (I->isCFIInstruction()) { 1370 I->removeFromParent(); 1371 Changed = true; 1372 break; 1373 } 1374 } 1375 } 1376 1377 MBB.addLiveIn(RISCV::X5); 1378 1379 // Add in a return instruction to the end of the outlined frame. 1380 MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR)) 1381 .addReg(RISCV::X0, RegState::Define) 1382 .addReg(RISCV::X5) 1383 .addImm(0)); 1384 } 1385 1386 MachineBasicBlock::iterator RISCVInstrInfo::insertOutlinedCall( 1387 Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It, 1388 MachineFunction &MF, const outliner::Candidate &C) const { 1389 1390 // Add in a call instruction to the outlined function at the given location. 1391 It = MBB.insert(It, 1392 BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5) 1393 .addGlobalAddress(M.getNamedValue(MF.getName()), 0, 1394 RISCVII::MO_CALL)); 1395 return It; 1396 } 1397 1398 // clang-format off 1399 #define CASE_VFMA_OPCODE_COMMON(OP, TYPE, LMUL) \ 1400 RISCV::PseudoV##OP##_##TYPE##_##LMUL 1401 1402 #define CASE_VFMA_OPCODE_LMULS(OP, TYPE) \ 1403 CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF8): \ 1404 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF4): \ 1405 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF2): \ 1406 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M1): \ 1407 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M2): \ 1408 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M4): \ 1409 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M8) 1410 1411 #define CASE_VFMA_SPLATS(OP) \ 1412 CASE_VFMA_OPCODE_LMULS(OP, VF16): \ 1413 case CASE_VFMA_OPCODE_LMULS(OP, VF32): \ 1414 case CASE_VFMA_OPCODE_LMULS(OP, VF64) 1415 // clang-format on 1416 1417 bool RISCVInstrInfo::findCommutedOpIndices(const MachineInstr &MI, 1418 unsigned &SrcOpIdx1, 1419 unsigned &SrcOpIdx2) const { 1420 const MCInstrDesc &Desc = MI.getDesc(); 1421 if (!Desc.isCommutable()) 1422 return false; 1423 1424 switch (MI.getOpcode()) { 1425 case CASE_VFMA_SPLATS(FMADD): 1426 case CASE_VFMA_SPLATS(FMSUB): 1427 case CASE_VFMA_SPLATS(FMACC): 1428 case CASE_VFMA_SPLATS(FMSAC): 1429 case CASE_VFMA_SPLATS(FNMADD): 1430 case CASE_VFMA_SPLATS(FNMSUB): 1431 case CASE_VFMA_SPLATS(FNMACC): 1432 case CASE_VFMA_SPLATS(FNMSAC): 1433 case CASE_VFMA_OPCODE_LMULS(FMACC, VV): 1434 case CASE_VFMA_OPCODE_LMULS(FMSAC, VV): 1435 case CASE_VFMA_OPCODE_LMULS(FNMACC, VV): 1436 case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV): 1437 case CASE_VFMA_OPCODE_LMULS(MADD, VX): 1438 case CASE_VFMA_OPCODE_LMULS(NMSUB, VX): 1439 case CASE_VFMA_OPCODE_LMULS(MACC, VX): 1440 case CASE_VFMA_OPCODE_LMULS(NMSAC, VX): 1441 case CASE_VFMA_OPCODE_LMULS(MACC, VV): 1442 case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): { 1443 // If the tail policy is undisturbed we can't commute. 1444 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags)); 1445 if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0) 1446 return false; 1447 1448 // For these instructions we can only swap operand 1 and operand 3 by 1449 // changing the opcode. 1450 unsigned CommutableOpIdx1 = 1; 1451 unsigned CommutableOpIdx2 = 3; 1452 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1, 1453 CommutableOpIdx2)) 1454 return false; 1455 return true; 1456 } 1457 case CASE_VFMA_OPCODE_LMULS(FMADD, VV): 1458 case CASE_VFMA_OPCODE_LMULS(FMSUB, VV): 1459 case CASE_VFMA_OPCODE_LMULS(FNMADD, VV): 1460 case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV): 1461 case CASE_VFMA_OPCODE_LMULS(MADD, VV): 1462 case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): { 1463 // If the tail policy is undisturbed we can't commute. 1464 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags)); 1465 if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0) 1466 return false; 1467 1468 // For these instructions we have more freedom. We can commute with the 1469 // other multiplicand or with the addend/subtrahend/minuend. 1470 1471 // Any fixed operand must be from source 1, 2 or 3. 1472 if (SrcOpIdx1 != CommuteAnyOperandIndex && SrcOpIdx1 > 3) 1473 return false; 1474 if (SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx2 > 3) 1475 return false; 1476 1477 // It both ops are fixed one must be the tied source. 1478 if (SrcOpIdx1 != CommuteAnyOperandIndex && 1479 SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx1 != 1 && SrcOpIdx2 != 1) 1480 return false; 1481 1482 // Look for two different register operands assumed to be commutable 1483 // regardless of the FMA opcode. The FMA opcode is adjusted later if 1484 // needed. 1485 if (SrcOpIdx1 == CommuteAnyOperandIndex || 1486 SrcOpIdx2 == CommuteAnyOperandIndex) { 1487 // At least one of operands to be commuted is not specified and 1488 // this method is free to choose appropriate commutable operands. 1489 unsigned CommutableOpIdx1 = SrcOpIdx1; 1490 if (SrcOpIdx1 == SrcOpIdx2) { 1491 // Both of operands are not fixed. Set one of commutable 1492 // operands to the tied source. 1493 CommutableOpIdx1 = 1; 1494 } else if (SrcOpIdx1 == CommuteAnyOperandIndex) { 1495 // Only one of the operands is not fixed. 1496 CommutableOpIdx1 = SrcOpIdx2; 1497 } 1498 1499 // CommutableOpIdx1 is well defined now. Let's choose another commutable 1500 // operand and assign its index to CommutableOpIdx2. 1501 unsigned CommutableOpIdx2; 1502 if (CommutableOpIdx1 != 1) { 1503 // If we haven't already used the tied source, we must use it now. 1504 CommutableOpIdx2 = 1; 1505 } else { 1506 Register Op1Reg = MI.getOperand(CommutableOpIdx1).getReg(); 1507 1508 // The commuted operands should have different registers. 1509 // Otherwise, the commute transformation does not change anything and 1510 // is useless. We use this as a hint to make our decision. 1511 if (Op1Reg != MI.getOperand(2).getReg()) 1512 CommutableOpIdx2 = 2; 1513 else 1514 CommutableOpIdx2 = 3; 1515 } 1516 1517 // Assign the found pair of commutable indices to SrcOpIdx1 and 1518 // SrcOpIdx2 to return those values. 1519 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1, 1520 CommutableOpIdx2)) 1521 return false; 1522 } 1523 1524 return true; 1525 } 1526 } 1527 1528 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 1529 } 1530 1531 #define CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL) \ 1532 case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL: \ 1533 Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL; \ 1534 break; 1535 1536 #define CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE) \ 1537 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF8) \ 1538 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4) \ 1539 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2) \ 1540 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1) \ 1541 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2) \ 1542 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4) \ 1543 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8) 1544 1545 #define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP) \ 1546 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF16) \ 1547 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF32) \ 1548 CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF64) 1549 1550 MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI, 1551 bool NewMI, 1552 unsigned OpIdx1, 1553 unsigned OpIdx2) const { 1554 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & { 1555 if (NewMI) 1556 return *MI.getParent()->getParent()->CloneMachineInstr(&MI); 1557 return MI; 1558 }; 1559 1560 switch (MI.getOpcode()) { 1561 case CASE_VFMA_SPLATS(FMACC): 1562 case CASE_VFMA_SPLATS(FMADD): 1563 case CASE_VFMA_SPLATS(FMSAC): 1564 case CASE_VFMA_SPLATS(FMSUB): 1565 case CASE_VFMA_SPLATS(FNMACC): 1566 case CASE_VFMA_SPLATS(FNMADD): 1567 case CASE_VFMA_SPLATS(FNMSAC): 1568 case CASE_VFMA_SPLATS(FNMSUB): 1569 case CASE_VFMA_OPCODE_LMULS(FMACC, VV): 1570 case CASE_VFMA_OPCODE_LMULS(FMSAC, VV): 1571 case CASE_VFMA_OPCODE_LMULS(FNMACC, VV): 1572 case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV): 1573 case CASE_VFMA_OPCODE_LMULS(MADD, VX): 1574 case CASE_VFMA_OPCODE_LMULS(NMSUB, VX): 1575 case CASE_VFMA_OPCODE_LMULS(MACC, VX): 1576 case CASE_VFMA_OPCODE_LMULS(NMSAC, VX): 1577 case CASE_VFMA_OPCODE_LMULS(MACC, VV): 1578 case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): { 1579 // It only make sense to toggle these between clobbering the 1580 // addend/subtrahend/minuend one of the multiplicands. 1581 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index"); 1582 assert((OpIdx1 == 3 || OpIdx2 == 3) && "Unexpected opcode index"); 1583 unsigned Opc; 1584 switch (MI.getOpcode()) { 1585 default: 1586 llvm_unreachable("Unexpected opcode"); 1587 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMACC, FMADD) 1588 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMADD, FMACC) 1589 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSAC, FMSUB) 1590 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSUB, FMSAC) 1591 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMACC, FNMADD) 1592 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMADD, FNMACC) 1593 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSAC, FNMSUB) 1594 CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSUB, FNMSAC) 1595 CASE_VFMA_CHANGE_OPCODE_LMULS(FMACC, FMADD, VV) 1596 CASE_VFMA_CHANGE_OPCODE_LMULS(FMSAC, FMSUB, VV) 1597 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMACC, FNMADD, VV) 1598 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSAC, FNMSUB, VV) 1599 CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VX) 1600 CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VX) 1601 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VX) 1602 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VX) 1603 CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VV) 1604 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VV) 1605 } 1606 1607 auto &WorkingMI = cloneIfNew(MI); 1608 WorkingMI.setDesc(get(Opc)); 1609 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 1610 OpIdx1, OpIdx2); 1611 } 1612 case CASE_VFMA_OPCODE_LMULS(FMADD, VV): 1613 case CASE_VFMA_OPCODE_LMULS(FMSUB, VV): 1614 case CASE_VFMA_OPCODE_LMULS(FNMADD, VV): 1615 case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV): 1616 case CASE_VFMA_OPCODE_LMULS(MADD, VV): 1617 case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): { 1618 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index"); 1619 // If one of the operands, is the addend we need to change opcode. 1620 // Otherwise we're just swapping 2 of the multiplicands. 1621 if (OpIdx1 == 3 || OpIdx2 == 3) { 1622 unsigned Opc; 1623 switch (MI.getOpcode()) { 1624 default: 1625 llvm_unreachable("Unexpected opcode"); 1626 CASE_VFMA_CHANGE_OPCODE_LMULS(FMADD, FMACC, VV) 1627 CASE_VFMA_CHANGE_OPCODE_LMULS(FMSUB, FMSAC, VV) 1628 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMADD, FNMACC, VV) 1629 CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSUB, FNMSAC, VV) 1630 CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VV) 1631 CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VV) 1632 } 1633 1634 auto &WorkingMI = cloneIfNew(MI); 1635 WorkingMI.setDesc(get(Opc)); 1636 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 1637 OpIdx1, OpIdx2); 1638 } 1639 // Let the default code handle it. 1640 break; 1641 } 1642 } 1643 1644 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 1645 } 1646 1647 #undef CASE_VFMA_CHANGE_OPCODE_SPLATS 1648 #undef CASE_VFMA_CHANGE_OPCODE_LMULS 1649 #undef CASE_VFMA_CHANGE_OPCODE_COMMON 1650 #undef CASE_VFMA_SPLATS 1651 #undef CASE_VFMA_OPCODE_LMULS 1652 #undef CASE_VFMA_OPCODE_COMMON 1653 1654 // clang-format off 1655 #define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL) \ 1656 RISCV::PseudoV##OP##_##LMUL##_TIED 1657 1658 #define CASE_WIDEOP_OPCODE_LMULS(OP) \ 1659 CASE_WIDEOP_OPCODE_COMMON(OP, MF8): \ 1660 case CASE_WIDEOP_OPCODE_COMMON(OP, MF4): \ 1661 case CASE_WIDEOP_OPCODE_COMMON(OP, MF2): \ 1662 case CASE_WIDEOP_OPCODE_COMMON(OP, M1): \ 1663 case CASE_WIDEOP_OPCODE_COMMON(OP, M2): \ 1664 case CASE_WIDEOP_OPCODE_COMMON(OP, M4) 1665 // clang-format on 1666 1667 #define CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL) \ 1668 case RISCV::PseudoV##OP##_##LMUL##_TIED: \ 1669 NewOpc = RISCV::PseudoV##OP##_##LMUL; \ 1670 break; 1671 1672 #define CASE_WIDEOP_CHANGE_OPCODE_LMULS(OP) \ 1673 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF8) \ 1674 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4) \ 1675 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2) \ 1676 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1) \ 1677 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2) \ 1678 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4) 1679 1680 MachineInstr *RISCVInstrInfo::convertToThreeAddress(MachineInstr &MI, 1681 LiveVariables *LV, 1682 LiveIntervals *LIS) const { 1683 switch (MI.getOpcode()) { 1684 default: 1685 break; 1686 case CASE_WIDEOP_OPCODE_LMULS(FWADD_WV): 1687 case CASE_WIDEOP_OPCODE_LMULS(FWSUB_WV): 1688 case CASE_WIDEOP_OPCODE_LMULS(WADD_WV): 1689 case CASE_WIDEOP_OPCODE_LMULS(WADDU_WV): 1690 case CASE_WIDEOP_OPCODE_LMULS(WSUB_WV): 1691 case CASE_WIDEOP_OPCODE_LMULS(WSUBU_WV): { 1692 // clang-format off 1693 unsigned NewOpc; 1694 switch (MI.getOpcode()) { 1695 default: 1696 llvm_unreachable("Unexpected opcode"); 1697 CASE_WIDEOP_CHANGE_OPCODE_LMULS(FWADD_WV) 1698 CASE_WIDEOP_CHANGE_OPCODE_LMULS(FWSUB_WV) 1699 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADD_WV) 1700 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADDU_WV) 1701 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUB_WV) 1702 CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUBU_WV) 1703 } 1704 //clang-format on 1705 1706 MachineBasicBlock &MBB = *MI.getParent(); 1707 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc)) 1708 .add(MI.getOperand(0)) 1709 .add(MI.getOperand(1)) 1710 .add(MI.getOperand(2)) 1711 .add(MI.getOperand(3)) 1712 .add(MI.getOperand(4)); 1713 MIB.copyImplicitOps(MI); 1714 1715 if (LV) { 1716 unsigned NumOps = MI.getNumOperands(); 1717 for (unsigned I = 1; I < NumOps; ++I) { 1718 MachineOperand &Op = MI.getOperand(I); 1719 if (Op.isReg() && Op.isKill()) 1720 LV->replaceKillInstruction(Op.getReg(), MI, *MIB); 1721 } 1722 } 1723 1724 if (LIS) { 1725 SlotIndex Idx = LIS->ReplaceMachineInstrInMaps(MI, *MIB); 1726 1727 if (MI.getOperand(0).isEarlyClobber()) { 1728 // Use operand 1 was tied to early-clobber def operand 0, so its live 1729 // interval could have ended at an early-clobber slot. Now they are not 1730 // tied we need to update it to the normal register slot. 1731 LiveInterval &LI = LIS->getInterval(MI.getOperand(1).getReg()); 1732 LiveRange::Segment *S = LI.getSegmentContaining(Idx); 1733 if (S->end == Idx.getRegSlot(true)) 1734 S->end = Idx.getRegSlot(); 1735 } 1736 } 1737 1738 return MIB; 1739 } 1740 } 1741 1742 return nullptr; 1743 } 1744 1745 #undef CASE_WIDEOP_CHANGE_OPCODE_LMULS 1746 #undef CASE_WIDEOP_CHANGE_OPCODE_COMMON 1747 #undef CASE_WIDEOP_OPCODE_LMULS 1748 #undef CASE_WIDEOP_OPCODE_COMMON 1749 1750 Register RISCVInstrInfo::getVLENFactoredAmount(MachineFunction &MF, 1751 MachineBasicBlock &MBB, 1752 MachineBasicBlock::iterator II, 1753 const DebugLoc &DL, 1754 int64_t Amount, 1755 MachineInstr::MIFlag Flag) const { 1756 assert(Amount > 0 && "There is no need to get VLEN scaled value."); 1757 assert(Amount % 8 == 0 && 1758 "Reserve the stack by the multiple of one vector size."); 1759 1760 MachineRegisterInfo &MRI = MF.getRegInfo(); 1761 const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo(); 1762 int64_t NumOfVReg = Amount / 8; 1763 1764 Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1765 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VL) 1766 .setMIFlag(Flag); 1767 assert(isInt<32>(NumOfVReg) && 1768 "Expect the number of vector registers within 32-bits."); 1769 if (isPowerOf2_32(NumOfVReg)) { 1770 uint32_t ShiftAmount = Log2_32(NumOfVReg); 1771 if (ShiftAmount == 0) 1772 return VL; 1773 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VL) 1774 .addReg(VL, RegState::Kill) 1775 .addImm(ShiftAmount) 1776 .setMIFlag(Flag); 1777 } else if (isPowerOf2_32(NumOfVReg - 1)) { 1778 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1779 uint32_t ShiftAmount = Log2_32(NumOfVReg - 1); 1780 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), ScaledRegister) 1781 .addReg(VL) 1782 .addImm(ShiftAmount) 1783 .setMIFlag(Flag); 1784 BuildMI(MBB, II, DL, TII->get(RISCV::ADD), VL) 1785 .addReg(ScaledRegister, RegState::Kill) 1786 .addReg(VL, RegState::Kill) 1787 .setMIFlag(Flag); 1788 } else if (isPowerOf2_32(NumOfVReg + 1)) { 1789 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1790 uint32_t ShiftAmount = Log2_32(NumOfVReg + 1); 1791 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), ScaledRegister) 1792 .addReg(VL) 1793 .addImm(ShiftAmount) 1794 .setMIFlag(Flag); 1795 BuildMI(MBB, II, DL, TII->get(RISCV::SUB), VL) 1796 .addReg(ScaledRegister, RegState::Kill) 1797 .addReg(VL, RegState::Kill) 1798 .setMIFlag(Flag); 1799 } else { 1800 Register N = MRI.createVirtualRegister(&RISCV::GPRRegClass); 1801 if (!isInt<12>(NumOfVReg)) 1802 movImm(MBB, II, DL, N, NumOfVReg); 1803 else { 1804 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), N) 1805 .addReg(RISCV::X0) 1806 .addImm(NumOfVReg) 1807 .setMIFlag(Flag); 1808 } 1809 if (!MF.getSubtarget<RISCVSubtarget>().hasStdExtM()) 1810 MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{ 1811 MF.getFunction(), 1812 "M-extension must be enabled to calculate the vscaled size/offset."}); 1813 BuildMI(MBB, II, DL, TII->get(RISCV::MUL), VL) 1814 .addReg(VL, RegState::Kill) 1815 .addReg(N, RegState::Kill) 1816 .setMIFlag(Flag); 1817 } 1818 1819 return VL; 1820 } 1821 1822 static bool isRVVWholeLoadStore(unsigned Opcode) { 1823 switch (Opcode) { 1824 default: 1825 return false; 1826 case RISCV::VS1R_V: 1827 case RISCV::VS2R_V: 1828 case RISCV::VS4R_V: 1829 case RISCV::VS8R_V: 1830 case RISCV::VL1RE8_V: 1831 case RISCV::VL2RE8_V: 1832 case RISCV::VL4RE8_V: 1833 case RISCV::VL8RE8_V: 1834 case RISCV::VL1RE16_V: 1835 case RISCV::VL2RE16_V: 1836 case RISCV::VL4RE16_V: 1837 case RISCV::VL8RE16_V: 1838 case RISCV::VL1RE32_V: 1839 case RISCV::VL2RE32_V: 1840 case RISCV::VL4RE32_V: 1841 case RISCV::VL8RE32_V: 1842 case RISCV::VL1RE64_V: 1843 case RISCV::VL2RE64_V: 1844 case RISCV::VL4RE64_V: 1845 case RISCV::VL8RE64_V: 1846 return true; 1847 } 1848 } 1849 1850 bool RISCVInstrInfo::isRVVSpill(const MachineInstr &MI, bool CheckFIs) const { 1851 // RVV lacks any support for immediate addressing for stack addresses, so be 1852 // conservative. 1853 unsigned Opcode = MI.getOpcode(); 1854 if (!RISCVVPseudosTable::getPseudoInfo(Opcode) && 1855 !isRVVWholeLoadStore(Opcode) && !isRVVSpillForZvlsseg(Opcode)) 1856 return false; 1857 return !CheckFIs || any_of(MI.operands(), [](const MachineOperand &MO) { 1858 return MO.isFI(); 1859 }); 1860 } 1861 1862 Optional<std::pair<unsigned, unsigned>> 1863 RISCVInstrInfo::isRVVSpillForZvlsseg(unsigned Opcode) const { 1864 switch (Opcode) { 1865 default: 1866 return None; 1867 case RISCV::PseudoVSPILL2_M1: 1868 case RISCV::PseudoVRELOAD2_M1: 1869 return std::make_pair(2u, 1u); 1870 case RISCV::PseudoVSPILL2_M2: 1871 case RISCV::PseudoVRELOAD2_M2: 1872 return std::make_pair(2u, 2u); 1873 case RISCV::PseudoVSPILL2_M4: 1874 case RISCV::PseudoVRELOAD2_M4: 1875 return std::make_pair(2u, 4u); 1876 case RISCV::PseudoVSPILL3_M1: 1877 case RISCV::PseudoVRELOAD3_M1: 1878 return std::make_pair(3u, 1u); 1879 case RISCV::PseudoVSPILL3_M2: 1880 case RISCV::PseudoVRELOAD3_M2: 1881 return std::make_pair(3u, 2u); 1882 case RISCV::PseudoVSPILL4_M1: 1883 case RISCV::PseudoVRELOAD4_M1: 1884 return std::make_pair(4u, 1u); 1885 case RISCV::PseudoVSPILL4_M2: 1886 case RISCV::PseudoVRELOAD4_M2: 1887 return std::make_pair(4u, 2u); 1888 case RISCV::PseudoVSPILL5_M1: 1889 case RISCV::PseudoVRELOAD5_M1: 1890 return std::make_pair(5u, 1u); 1891 case RISCV::PseudoVSPILL6_M1: 1892 case RISCV::PseudoVRELOAD6_M1: 1893 return std::make_pair(6u, 1u); 1894 case RISCV::PseudoVSPILL7_M1: 1895 case RISCV::PseudoVRELOAD7_M1: 1896 return std::make_pair(7u, 1u); 1897 case RISCV::PseudoVSPILL8_M1: 1898 case RISCV::PseudoVRELOAD8_M1: 1899 return std::make_pair(8u, 1u); 1900 } 1901 } 1902