1 //===-- RISCVISelDAGToDAG.cpp - A dag to dag inst selector for RISCV ------===// 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 defines an instruction selector for the RISCV target. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "RISCVISelDAGToDAG.h" 14 #include "MCTargetDesc/RISCVMCTargetDesc.h" 15 #include "MCTargetDesc/RISCVMatInt.h" 16 #include "RISCVISelLowering.h" 17 #include "RISCVMachineFunctionInfo.h" 18 #include "llvm/CodeGen/MachineFrameInfo.h" 19 #include "llvm/IR/IntrinsicsRISCV.h" 20 #include "llvm/Support/Alignment.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Support/KnownBits.h" 23 #include "llvm/Support/MathExtras.h" 24 #include "llvm/Support/raw_ostream.h" 25 26 using namespace llvm; 27 28 #define DEBUG_TYPE "riscv-isel" 29 30 namespace llvm { 31 namespace RISCV { 32 #define GET_RISCVVSSEGTable_IMPL 33 #define GET_RISCVVLSEGTable_IMPL 34 #define GET_RISCVVLXSEGTable_IMPL 35 #define GET_RISCVVSXSEGTable_IMPL 36 #define GET_RISCVVLETable_IMPL 37 #define GET_RISCVVSETable_IMPL 38 #define GET_RISCVVLXTable_IMPL 39 #define GET_RISCVVSXTable_IMPL 40 #include "RISCVGenSearchableTables.inc" 41 } // namespace RISCV 42 } // namespace llvm 43 44 void RISCVDAGToDAGISel::PreprocessISelDAG() { 45 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), 46 E = CurDAG->allnodes_end(); 47 I != E;) { 48 SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues. 49 50 // Lower SPLAT_VECTOR_SPLIT_I64 to two scalar stores and a stride 0 vector 51 // load. Done after lowering and combining so that we have a chance to 52 // optimize this to VMV_V_X_VL when the upper bits aren't needed. 53 if (N->getOpcode() != RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL) 54 continue; 55 56 assert(N->getNumOperands() == 3 && "Unexpected number of operands"); 57 MVT VT = N->getSimpleValueType(0); 58 SDValue Lo = N->getOperand(0); 59 SDValue Hi = N->getOperand(1); 60 SDValue VL = N->getOperand(2); 61 assert(VT.getVectorElementType() == MVT::i64 && VT.isScalableVector() && 62 Lo.getValueType() == MVT::i32 && Hi.getValueType() == MVT::i32 && 63 "Unexpected VTs!"); 64 MachineFunction &MF = CurDAG->getMachineFunction(); 65 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>(); 66 SDLoc DL(N); 67 68 // We use the same frame index we use for moving two i32s into 64-bit FPR. 69 // This is an analogous operation. 70 int FI = FuncInfo->getMoveF64FrameIndex(MF); 71 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, FI); 72 const TargetLowering &TLI = CurDAG->getTargetLoweringInfo(); 73 SDValue StackSlot = 74 CurDAG->getFrameIndex(FI, TLI.getPointerTy(CurDAG->getDataLayout())); 75 76 SDValue Chain = CurDAG->getEntryNode(); 77 Lo = CurDAG->getStore(Chain, DL, Lo, StackSlot, MPI, Align(8)); 78 79 SDValue OffsetSlot = 80 CurDAG->getMemBasePlusOffset(StackSlot, TypeSize::Fixed(4), DL); 81 Hi = CurDAG->getStore(Chain, DL, Hi, OffsetSlot, MPI.getWithOffset(4), 82 Align(8)); 83 84 Chain = CurDAG->getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi); 85 86 SDVTList VTs = CurDAG->getVTList({VT, MVT::Other}); 87 SDValue IntID = 88 CurDAG->getTargetConstant(Intrinsic::riscv_vlse, DL, MVT::i64); 89 SDValue Ops[] = {Chain, IntID, StackSlot, 90 CurDAG->getRegister(RISCV::X0, MVT::i64), VL}; 91 92 SDValue Result = CurDAG->getMemIntrinsicNode( 93 ISD::INTRINSIC_W_CHAIN, DL, VTs, Ops, MVT::i64, MPI, Align(8), 94 MachineMemOperand::MOLoad); 95 96 // We're about to replace all uses of the SPLAT_VECTOR_SPLIT_I64 with the 97 // vlse we created. This will cause general havok on the dag because 98 // anything below the conversion could be folded into other existing nodes. 99 // To avoid invalidating 'I', back it up to the convert node. 100 --I; 101 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); 102 103 // Now that we did that, the node is dead. Increment the iterator to the 104 // next node to process, then delete N. 105 ++I; 106 CurDAG->DeleteNode(N); 107 } 108 } 109 110 void RISCVDAGToDAGISel::PostprocessISelDAG() { 111 doPeepholeLoadStoreADDI(); 112 } 113 114 static SDNode *selectImm(SelectionDAG *CurDAG, const SDLoc &DL, int64_t Imm, 115 const RISCVSubtarget &Subtarget) { 116 MVT XLenVT = Subtarget.getXLenVT(); 117 RISCVMatInt::InstSeq Seq = 118 RISCVMatInt::generateInstSeq(Imm, Subtarget.getFeatureBits()); 119 120 SDNode *Result = nullptr; 121 SDValue SrcReg = CurDAG->getRegister(RISCV::X0, XLenVT); 122 for (RISCVMatInt::Inst &Inst : Seq) { 123 SDValue SDImm = CurDAG->getTargetConstant(Inst.Imm, DL, XLenVT); 124 if (Inst.Opc == RISCV::LUI) 125 Result = CurDAG->getMachineNode(RISCV::LUI, DL, XLenVT, SDImm); 126 else if (Inst.Opc == RISCV::ADDUW) 127 Result = CurDAG->getMachineNode(RISCV::ADDUW, DL, XLenVT, SrcReg, 128 CurDAG->getRegister(RISCV::X0, XLenVT)); 129 else 130 Result = CurDAG->getMachineNode(Inst.Opc, DL, XLenVT, SrcReg, SDImm); 131 132 // Only the first instruction has X0 as its source. 133 SrcReg = SDValue(Result, 0); 134 } 135 136 return Result; 137 } 138 139 static SDValue createTupleImpl(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs, 140 unsigned RegClassID, unsigned SubReg0) { 141 assert(Regs.size() >= 2 && Regs.size() <= 8); 142 143 SDLoc DL(Regs[0]); 144 SmallVector<SDValue, 8> Ops; 145 146 Ops.push_back(CurDAG.getTargetConstant(RegClassID, DL, MVT::i32)); 147 148 for (unsigned I = 0; I < Regs.size(); ++I) { 149 Ops.push_back(Regs[I]); 150 Ops.push_back(CurDAG.getTargetConstant(SubReg0 + I, DL, MVT::i32)); 151 } 152 SDNode *N = 153 CurDAG.getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops); 154 return SDValue(N, 0); 155 } 156 157 static SDValue createM1Tuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs, 158 unsigned NF) { 159 static const unsigned RegClassIDs[] = { 160 RISCV::VRN2M1RegClassID, RISCV::VRN3M1RegClassID, RISCV::VRN4M1RegClassID, 161 RISCV::VRN5M1RegClassID, RISCV::VRN6M1RegClassID, RISCV::VRN7M1RegClassID, 162 RISCV::VRN8M1RegClassID}; 163 164 return createTupleImpl(CurDAG, Regs, RegClassIDs[NF - 2], RISCV::sub_vrm1_0); 165 } 166 167 static SDValue createM2Tuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs, 168 unsigned NF) { 169 static const unsigned RegClassIDs[] = {RISCV::VRN2M2RegClassID, 170 RISCV::VRN3M2RegClassID, 171 RISCV::VRN4M2RegClassID}; 172 173 return createTupleImpl(CurDAG, Regs, RegClassIDs[NF - 2], RISCV::sub_vrm2_0); 174 } 175 176 static SDValue createM4Tuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs, 177 unsigned NF) { 178 return createTupleImpl(CurDAG, Regs, RISCV::VRN2M4RegClassID, 179 RISCV::sub_vrm4_0); 180 } 181 182 static SDValue createTuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs, 183 unsigned NF, RISCVII::VLMUL LMUL) { 184 switch (LMUL) { 185 default: 186 llvm_unreachable("Invalid LMUL."); 187 case RISCVII::VLMUL::LMUL_F8: 188 case RISCVII::VLMUL::LMUL_F4: 189 case RISCVII::VLMUL::LMUL_F2: 190 case RISCVII::VLMUL::LMUL_1: 191 return createM1Tuple(CurDAG, Regs, NF); 192 case RISCVII::VLMUL::LMUL_2: 193 return createM2Tuple(CurDAG, Regs, NF); 194 case RISCVII::VLMUL::LMUL_4: 195 return createM4Tuple(CurDAG, Regs, NF); 196 } 197 } 198 199 void RISCVDAGToDAGISel::addVectorLoadStoreOperands( 200 SDNode *Node, unsigned Log2SEW, const SDLoc &DL, unsigned CurOp, 201 bool IsMasked, bool IsStridedOrIndexed, SmallVectorImpl<SDValue> &Operands, 202 MVT *IndexVT) { 203 SDValue Chain = Node->getOperand(0); 204 SDValue Glue; 205 206 SDValue Base; 207 SelectBaseAddr(Node->getOperand(CurOp++), Base); 208 Operands.push_back(Base); // Base pointer. 209 210 if (IsStridedOrIndexed) { 211 Operands.push_back(Node->getOperand(CurOp++)); // Index. 212 if (IndexVT) 213 *IndexVT = Operands.back()->getSimpleValueType(0); 214 } 215 216 if (IsMasked) { 217 // Mask needs to be copied to V0. 218 SDValue Mask = Node->getOperand(CurOp++); 219 Chain = CurDAG->getCopyToReg(Chain, DL, RISCV::V0, Mask, SDValue()); 220 Glue = Chain.getValue(1); 221 Operands.push_back(CurDAG->getRegister(RISCV::V0, Mask.getValueType())); 222 } 223 SDValue VL; 224 selectVLOp(Node->getOperand(CurOp++), VL); 225 Operands.push_back(VL); 226 227 MVT XLenVT = Subtarget->getXLenVT(); 228 SDValue SEWOp = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT); 229 Operands.push_back(SEWOp); 230 231 Operands.push_back(Chain); // Chain. 232 if (Glue) 233 Operands.push_back(Glue); 234 } 235 236 void RISCVDAGToDAGISel::selectVLSEG(SDNode *Node, bool IsMasked, 237 bool IsStrided) { 238 SDLoc DL(Node); 239 unsigned NF = Node->getNumValues() - 1; 240 MVT VT = Node->getSimpleValueType(0); 241 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 242 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 243 244 unsigned CurOp = 2; 245 SmallVector<SDValue, 8> Operands; 246 if (IsMasked) { 247 SmallVector<SDValue, 8> Regs(Node->op_begin() + CurOp, 248 Node->op_begin() + CurOp + NF); 249 SDValue MaskedOff = createTuple(*CurDAG, Regs, NF, LMUL); 250 Operands.push_back(MaskedOff); 251 CurOp += NF; 252 } 253 254 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided, 255 Operands); 256 257 const RISCV::VLSEGPseudo *P = 258 RISCV::getVLSEGPseudo(NF, IsMasked, IsStrided, /*FF*/ false, Log2SEW, 259 static_cast<unsigned>(LMUL)); 260 MachineSDNode *Load = 261 CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped, MVT::Other, Operands); 262 263 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 264 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()}); 265 266 SDValue SuperReg = SDValue(Load, 0); 267 for (unsigned I = 0; I < NF; ++I) { 268 unsigned SubRegIdx = RISCVTargetLowering::getSubregIndexByMVT(VT, I); 269 ReplaceUses(SDValue(Node, I), 270 CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, SuperReg)); 271 } 272 273 ReplaceUses(SDValue(Node, NF), SDValue(Load, 1)); 274 CurDAG->RemoveDeadNode(Node); 275 } 276 277 void RISCVDAGToDAGISel::selectVLSEGFF(SDNode *Node, bool IsMasked) { 278 SDLoc DL(Node); 279 unsigned NF = Node->getNumValues() - 2; // Do not count VL and Chain. 280 MVT VT = Node->getSimpleValueType(0); 281 MVT XLenVT = Subtarget->getXLenVT(); 282 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 283 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 284 285 unsigned CurOp = 2; 286 SmallVector<SDValue, 7> Operands; 287 if (IsMasked) { 288 SmallVector<SDValue, 8> Regs(Node->op_begin() + CurOp, 289 Node->op_begin() + CurOp + NF); 290 SDValue MaskedOff = createTuple(*CurDAG, Regs, NF, LMUL); 291 Operands.push_back(MaskedOff); 292 CurOp += NF; 293 } 294 295 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, 296 /*IsStridedOrIndexed*/ false, Operands); 297 298 const RISCV::VLSEGPseudo *P = 299 RISCV::getVLSEGPseudo(NF, IsMasked, /*Strided*/ false, /*FF*/ true, 300 Log2SEW, static_cast<unsigned>(LMUL)); 301 MachineSDNode *Load = CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped, 302 MVT::Other, MVT::Glue, Operands); 303 SDNode *ReadVL = CurDAG->getMachineNode(RISCV::PseudoReadVL, DL, XLenVT, 304 /*Glue*/ SDValue(Load, 2)); 305 306 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 307 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()}); 308 309 SDValue SuperReg = SDValue(Load, 0); 310 for (unsigned I = 0; I < NF; ++I) { 311 unsigned SubRegIdx = RISCVTargetLowering::getSubregIndexByMVT(VT, I); 312 ReplaceUses(SDValue(Node, I), 313 CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, SuperReg)); 314 } 315 316 ReplaceUses(SDValue(Node, NF), SDValue(ReadVL, 0)); // VL 317 ReplaceUses(SDValue(Node, NF + 1), SDValue(Load, 1)); // Chain 318 CurDAG->RemoveDeadNode(Node); 319 } 320 321 void RISCVDAGToDAGISel::selectVLXSEG(SDNode *Node, bool IsMasked, 322 bool IsOrdered) { 323 SDLoc DL(Node); 324 unsigned NF = Node->getNumValues() - 1; 325 MVT VT = Node->getSimpleValueType(0); 326 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 327 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 328 329 unsigned CurOp = 2; 330 SmallVector<SDValue, 8> Operands; 331 if (IsMasked) { 332 SmallVector<SDValue, 8> Regs(Node->op_begin() + CurOp, 333 Node->op_begin() + CurOp + NF); 334 SDValue MaskedOff = createTuple(*CurDAG, Regs, NF, LMUL); 335 Operands.push_back(MaskedOff); 336 CurOp += NF; 337 } 338 339 MVT IndexVT; 340 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, 341 /*IsStridedOrIndexed*/ true, Operands, &IndexVT); 342 343 assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() && 344 "Element count mismatch"); 345 346 RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT); 347 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits()); 348 const RISCV::VLXSEGPseudo *P = RISCV::getVLXSEGPseudo( 349 NF, IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL), 350 static_cast<unsigned>(IndexLMUL)); 351 MachineSDNode *Load = 352 CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped, MVT::Other, Operands); 353 354 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 355 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()}); 356 357 SDValue SuperReg = SDValue(Load, 0); 358 for (unsigned I = 0; I < NF; ++I) { 359 unsigned SubRegIdx = RISCVTargetLowering::getSubregIndexByMVT(VT, I); 360 ReplaceUses(SDValue(Node, I), 361 CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, SuperReg)); 362 } 363 364 ReplaceUses(SDValue(Node, NF), SDValue(Load, 1)); 365 CurDAG->RemoveDeadNode(Node); 366 } 367 368 void RISCVDAGToDAGISel::selectVSSEG(SDNode *Node, bool IsMasked, 369 bool IsStrided) { 370 SDLoc DL(Node); 371 unsigned NF = Node->getNumOperands() - 4; 372 if (IsStrided) 373 NF--; 374 if (IsMasked) 375 NF--; 376 MVT VT = Node->getOperand(2)->getSimpleValueType(0); 377 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 378 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 379 SmallVector<SDValue, 8> Regs(Node->op_begin() + 2, Node->op_begin() + 2 + NF); 380 SDValue StoreVal = createTuple(*CurDAG, Regs, NF, LMUL); 381 382 SmallVector<SDValue, 8> Operands; 383 Operands.push_back(StoreVal); 384 unsigned CurOp = 2 + NF; 385 386 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided, 387 Operands); 388 389 const RISCV::VSSEGPseudo *P = RISCV::getVSSEGPseudo( 390 NF, IsMasked, IsStrided, Log2SEW, static_cast<unsigned>(LMUL)); 391 MachineSDNode *Store = 392 CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0), Operands); 393 394 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 395 CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()}); 396 397 ReplaceNode(Node, Store); 398 } 399 400 void RISCVDAGToDAGISel::selectVSXSEG(SDNode *Node, bool IsMasked, 401 bool IsOrdered) { 402 SDLoc DL(Node); 403 unsigned NF = Node->getNumOperands() - 5; 404 if (IsMasked) 405 --NF; 406 MVT VT = Node->getOperand(2)->getSimpleValueType(0); 407 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 408 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 409 SmallVector<SDValue, 8> Regs(Node->op_begin() + 2, Node->op_begin() + 2 + NF); 410 SDValue StoreVal = createTuple(*CurDAG, Regs, NF, LMUL); 411 412 SmallVector<SDValue, 8> Operands; 413 Operands.push_back(StoreVal); 414 unsigned CurOp = 2 + NF; 415 416 MVT IndexVT; 417 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, 418 /*IsStridedOrIndexed*/ true, Operands, &IndexVT); 419 420 assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() && 421 "Element count mismatch"); 422 423 RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT); 424 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits()); 425 const RISCV::VSXSEGPseudo *P = RISCV::getVSXSEGPseudo( 426 NF, IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL), 427 static_cast<unsigned>(IndexLMUL)); 428 MachineSDNode *Store = 429 CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0), Operands); 430 431 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 432 CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()}); 433 434 ReplaceNode(Node, Store); 435 } 436 437 438 void RISCVDAGToDAGISel::Select(SDNode *Node) { 439 // If we have a custom node, we have already selected. 440 if (Node->isMachineOpcode()) { 441 LLVM_DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n"); 442 Node->setNodeId(-1); 443 return; 444 } 445 446 // Instruction Selection not handled by the auto-generated tablegen selection 447 // should be handled here. 448 unsigned Opcode = Node->getOpcode(); 449 MVT XLenVT = Subtarget->getXLenVT(); 450 SDLoc DL(Node); 451 MVT VT = Node->getSimpleValueType(0); 452 453 switch (Opcode) { 454 case ISD::Constant: { 455 auto *ConstNode = cast<ConstantSDNode>(Node); 456 if (VT == XLenVT && ConstNode->isNullValue()) { 457 SDValue New = 458 CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, RISCV::X0, XLenVT); 459 ReplaceNode(Node, New.getNode()); 460 return; 461 } 462 ReplaceNode(Node, 463 selectImm(CurDAG, DL, ConstNode->getSExtValue(), *Subtarget)); 464 return; 465 } 466 case ISD::FrameIndex: { 467 SDValue Imm = CurDAG->getTargetConstant(0, DL, XLenVT); 468 int FI = cast<FrameIndexSDNode>(Node)->getIndex(); 469 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT); 470 ReplaceNode(Node, CurDAG->getMachineNode(RISCV::ADDI, DL, VT, TFI, Imm)); 471 return; 472 } 473 case ISD::SRL: { 474 // We don't need this transform if zext.h is supported. 475 if (Subtarget->hasStdExtZbb() || Subtarget->hasStdExtZbp()) 476 break; 477 // Optimize (srl (and X, 0xffff), C) -> 478 // (srli (slli X, (XLen-16), (XLen-16) + C) 479 // Taking into account that the 0xffff may have had lower bits unset by 480 // SimplifyDemandedBits. This avoids materializing the 0xffff immediate. 481 // This pattern occurs when type legalizing i16 right shifts. 482 // FIXME: This could be extended to other AND masks. 483 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1)); 484 if (N1C) { 485 uint64_t ShAmt = N1C->getZExtValue(); 486 SDValue N0 = Node->getOperand(0); 487 if (ShAmt < 16 && N0.getOpcode() == ISD::AND && N0.hasOneUse() && 488 isa<ConstantSDNode>(N0.getOperand(1))) { 489 uint64_t Mask = N0.getConstantOperandVal(1); 490 Mask |= maskTrailingOnes<uint64_t>(ShAmt); 491 if (Mask == 0xffff) { 492 unsigned LShAmt = Subtarget->getXLen() - 16; 493 SDNode *SLLI = 494 CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0->getOperand(0), 495 CurDAG->getTargetConstant(LShAmt, DL, VT)); 496 SDNode *SRLI = CurDAG->getMachineNode( 497 RISCV::SRLI, DL, VT, SDValue(SLLI, 0), 498 CurDAG->getTargetConstant(LShAmt + ShAmt, DL, VT)); 499 ReplaceNode(Node, SRLI); 500 return; 501 } 502 } 503 } 504 505 break; 506 } 507 case ISD::INTRINSIC_WO_CHAIN: { 508 unsigned IntNo = Node->getConstantOperandVal(0); 509 switch (IntNo) { 510 // By default we do not custom select any intrinsic. 511 default: 512 break; 513 case Intrinsic::riscv_vmsgeu: 514 case Intrinsic::riscv_vmsge: { 515 SDValue Src1 = Node->getOperand(1); 516 SDValue Src2 = Node->getOperand(2); 517 // Only custom select scalar second operand. 518 if (Src2.getValueType() != XLenVT) 519 break; 520 // Small constants are handled with patterns. 521 if (auto *C = dyn_cast<ConstantSDNode>(Src2)) { 522 int64_t CVal = C->getSExtValue(); 523 if (CVal >= -15 && CVal <= 16) 524 break; 525 } 526 bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu; 527 MVT Src1VT = Src1.getSimpleValueType(); 528 unsigned VMSLTOpcode, VMNANDOpcode; 529 switch (RISCVTargetLowering::getLMUL(Src1VT)) { 530 default: 531 llvm_unreachable("Unexpected LMUL!"); 532 case RISCVII::VLMUL::LMUL_F8: 533 VMSLTOpcode = 534 IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF8 : RISCV::PseudoVMSLT_VX_MF8; 535 VMNANDOpcode = RISCV::PseudoVMNAND_MM_MF8; 536 break; 537 case RISCVII::VLMUL::LMUL_F4: 538 VMSLTOpcode = 539 IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF4 : RISCV::PseudoVMSLT_VX_MF4; 540 VMNANDOpcode = RISCV::PseudoVMNAND_MM_MF4; 541 break; 542 case RISCVII::VLMUL::LMUL_F2: 543 VMSLTOpcode = 544 IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF2 : RISCV::PseudoVMSLT_VX_MF2; 545 VMNANDOpcode = RISCV::PseudoVMNAND_MM_MF2; 546 break; 547 case RISCVII::VLMUL::LMUL_1: 548 VMSLTOpcode = 549 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M1 : RISCV::PseudoVMSLT_VX_M1; 550 VMNANDOpcode = RISCV::PseudoVMNAND_MM_M1; 551 break; 552 case RISCVII::VLMUL::LMUL_2: 553 VMSLTOpcode = 554 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M2 : RISCV::PseudoVMSLT_VX_M2; 555 VMNANDOpcode = RISCV::PseudoVMNAND_MM_M2; 556 break; 557 case RISCVII::VLMUL::LMUL_4: 558 VMSLTOpcode = 559 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M4 : RISCV::PseudoVMSLT_VX_M4; 560 VMNANDOpcode = RISCV::PseudoVMNAND_MM_M4; 561 break; 562 case RISCVII::VLMUL::LMUL_8: 563 VMSLTOpcode = 564 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M8 : RISCV::PseudoVMSLT_VX_M8; 565 VMNANDOpcode = RISCV::PseudoVMNAND_MM_M8; 566 break; 567 } 568 SDValue SEW = CurDAG->getTargetConstant( 569 Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT); 570 SDValue VL; 571 selectVLOp(Node->getOperand(3), VL); 572 573 // Expand to 574 // vmslt{u}.vx vd, va, x; vmnand.mm vd, vd, vd 575 SDValue Cmp = SDValue( 576 CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}), 577 0); 578 ReplaceNode(Node, CurDAG->getMachineNode(VMNANDOpcode, DL, VT, 579 {Cmp, Cmp, VL, SEW})); 580 return; 581 } 582 case Intrinsic::riscv_vmsgeu_mask: 583 case Intrinsic::riscv_vmsge_mask: { 584 SDValue Src1 = Node->getOperand(2); 585 SDValue Src2 = Node->getOperand(3); 586 // Only custom select scalar second operand. 587 if (Src2.getValueType() != XLenVT) 588 break; 589 // Small constants are handled with patterns. 590 if (auto *C = dyn_cast<ConstantSDNode>(Src2)) { 591 int64_t CVal = C->getSExtValue(); 592 if (CVal >= -15 && CVal <= 16) 593 break; 594 } 595 bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu_mask; 596 MVT Src1VT = Src1.getSimpleValueType(); 597 unsigned VMSLTOpcode, VMSLTMaskOpcode, VMXOROpcode, VMANDNOTOpcode; 598 switch (RISCVTargetLowering::getLMUL(Src1VT)) { 599 default: 600 llvm_unreachable("Unexpected LMUL!"); 601 case RISCVII::VLMUL::LMUL_F8: 602 VMSLTOpcode = 603 IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF8 : RISCV::PseudoVMSLT_VX_MF8; 604 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF8_MASK 605 : RISCV::PseudoVMSLT_VX_MF8_MASK; 606 break; 607 case RISCVII::VLMUL::LMUL_F4: 608 VMSLTOpcode = 609 IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF4 : RISCV::PseudoVMSLT_VX_MF4; 610 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF4_MASK 611 : RISCV::PseudoVMSLT_VX_MF4_MASK; 612 break; 613 case RISCVII::VLMUL::LMUL_F2: 614 VMSLTOpcode = 615 IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF2 : RISCV::PseudoVMSLT_VX_MF2; 616 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_MF2_MASK 617 : RISCV::PseudoVMSLT_VX_MF2_MASK; 618 break; 619 case RISCVII::VLMUL::LMUL_1: 620 VMSLTOpcode = 621 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M1 : RISCV::PseudoVMSLT_VX_M1; 622 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_M1_MASK 623 : RISCV::PseudoVMSLT_VX_M1_MASK; 624 break; 625 case RISCVII::VLMUL::LMUL_2: 626 VMSLTOpcode = 627 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M2 : RISCV::PseudoVMSLT_VX_M2; 628 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_M2_MASK 629 : RISCV::PseudoVMSLT_VX_M2_MASK; 630 break; 631 case RISCVII::VLMUL::LMUL_4: 632 VMSLTOpcode = 633 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M4 : RISCV::PseudoVMSLT_VX_M4; 634 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_M4_MASK 635 : RISCV::PseudoVMSLT_VX_M4_MASK; 636 break; 637 case RISCVII::VLMUL::LMUL_8: 638 VMSLTOpcode = 639 IsUnsigned ? RISCV::PseudoVMSLTU_VX_M8 : RISCV::PseudoVMSLT_VX_M8; 640 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_M8_MASK 641 : RISCV::PseudoVMSLT_VX_M8_MASK; 642 break; 643 } 644 // Mask operations use the LMUL from the mask type. 645 switch (RISCVTargetLowering::getLMUL(VT)) { 646 default: 647 llvm_unreachable("Unexpected LMUL!"); 648 case RISCVII::VLMUL::LMUL_F8: 649 VMXOROpcode = RISCV::PseudoVMXOR_MM_MF8; 650 VMANDNOTOpcode = RISCV::PseudoVMANDNOT_MM_MF8; 651 break; 652 case RISCVII::VLMUL::LMUL_F4: 653 VMXOROpcode = RISCV::PseudoVMXOR_MM_MF4; 654 VMANDNOTOpcode = RISCV::PseudoVMANDNOT_MM_MF4; 655 break; 656 case RISCVII::VLMUL::LMUL_F2: 657 VMXOROpcode = RISCV::PseudoVMXOR_MM_MF2; 658 VMANDNOTOpcode = RISCV::PseudoVMANDNOT_MM_MF2; 659 break; 660 case RISCVII::VLMUL::LMUL_1: 661 VMXOROpcode = RISCV::PseudoVMXOR_MM_M1; 662 VMANDNOTOpcode = RISCV::PseudoVMANDNOT_MM_M1; 663 break; 664 case RISCVII::VLMUL::LMUL_2: 665 VMXOROpcode = RISCV::PseudoVMXOR_MM_M2; 666 VMANDNOTOpcode = RISCV::PseudoVMANDNOT_MM_M2; 667 break; 668 case RISCVII::VLMUL::LMUL_4: 669 VMXOROpcode = RISCV::PseudoVMXOR_MM_M4; 670 VMANDNOTOpcode = RISCV::PseudoVMANDNOT_MM_M4; 671 break; 672 case RISCVII::VLMUL::LMUL_8: 673 VMXOROpcode = RISCV::PseudoVMXOR_MM_M8; 674 VMANDNOTOpcode = RISCV::PseudoVMANDNOT_MM_M8; 675 break; 676 } 677 SDValue SEW = CurDAG->getTargetConstant( 678 Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT); 679 SDValue MaskSEW = CurDAG->getTargetConstant(0, DL, XLenVT); 680 SDValue VL; 681 selectVLOp(Node->getOperand(5), VL); 682 SDValue MaskedOff = Node->getOperand(1); 683 SDValue Mask = Node->getOperand(4); 684 // If the MaskedOff value and the Mask are the same value use 685 // vmslt{u}.vx vt, va, x; vmandnot.mm vd, vd, vt 686 // This avoids needing to copy v0 to vd before starting the next sequence. 687 if (Mask == MaskedOff) { 688 SDValue Cmp = SDValue( 689 CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}), 690 0); 691 ReplaceNode(Node, CurDAG->getMachineNode(VMANDNOTOpcode, DL, VT, 692 {Mask, Cmp, VL, MaskSEW})); 693 return; 694 } 695 696 // Mask needs to be copied to V0. 697 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, 698 RISCV::V0, Mask, SDValue()); 699 SDValue Glue = Chain.getValue(1); 700 SDValue V0 = CurDAG->getRegister(RISCV::V0, VT); 701 702 // Otherwise use 703 // vmslt{u}.vx vd, va, x, v0.t; vmxor.mm vd, vd, v0 704 SDValue Cmp = SDValue( 705 CurDAG->getMachineNode(VMSLTMaskOpcode, DL, VT, 706 {MaskedOff, Src1, Src2, V0, VL, SEW, Glue}), 707 0); 708 ReplaceNode(Node, CurDAG->getMachineNode(VMXOROpcode, DL, VT, 709 {Cmp, Mask, VL, MaskSEW})); 710 return; 711 } 712 } 713 break; 714 } 715 case ISD::INTRINSIC_W_CHAIN: { 716 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 717 switch (IntNo) { 718 // By default we do not custom select any intrinsic. 719 default: 720 break; 721 722 case Intrinsic::riscv_vsetvli: 723 case Intrinsic::riscv_vsetvlimax: { 724 if (!Subtarget->hasStdExtV()) 725 break; 726 727 bool VLMax = IntNo == Intrinsic::riscv_vsetvlimax; 728 unsigned Offset = VLMax ? 2 : 3; 729 730 assert(Node->getNumOperands() == Offset + 2 && 731 "Unexpected number of operands"); 732 733 unsigned SEW = 734 RISCVVType::decodeVSEW(Node->getConstantOperandVal(Offset) & 0x7); 735 RISCVII::VLMUL VLMul = static_cast<RISCVII::VLMUL>( 736 Node->getConstantOperandVal(Offset + 1) & 0x7); 737 738 unsigned VTypeI = RISCVVType::encodeVTYPE( 739 VLMul, SEW, /*TailAgnostic*/ true, /*MaskAgnostic*/ false); 740 SDValue VTypeIOp = CurDAG->getTargetConstant(VTypeI, DL, XLenVT); 741 742 SDValue VLOperand; 743 if (VLMax) { 744 VLOperand = CurDAG->getRegister(RISCV::X0, XLenVT); 745 } else { 746 VLOperand = Node->getOperand(2); 747 748 if (auto *C = dyn_cast<ConstantSDNode>(VLOperand)) { 749 uint64_t AVL = C->getZExtValue(); 750 if (isUInt<5>(AVL)) { 751 SDValue VLImm = CurDAG->getTargetConstant(AVL, DL, XLenVT); 752 ReplaceNode( 753 Node, CurDAG->getMachineNode(RISCV::PseudoVSETIVLI, DL, XLenVT, 754 MVT::Other, VLImm, VTypeIOp, 755 /* Chain */ Node->getOperand(0))); 756 return; 757 } 758 } 759 } 760 761 ReplaceNode(Node, 762 CurDAG->getMachineNode(RISCV::PseudoVSETVLI, DL, XLenVT, 763 MVT::Other, VLOperand, VTypeIOp, 764 /* Chain */ Node->getOperand(0))); 765 return; 766 } 767 case Intrinsic::riscv_vlseg2: 768 case Intrinsic::riscv_vlseg3: 769 case Intrinsic::riscv_vlseg4: 770 case Intrinsic::riscv_vlseg5: 771 case Intrinsic::riscv_vlseg6: 772 case Intrinsic::riscv_vlseg7: 773 case Intrinsic::riscv_vlseg8: { 774 selectVLSEG(Node, /*IsMasked*/ false, /*IsStrided*/ false); 775 return; 776 } 777 case Intrinsic::riscv_vlseg2_mask: 778 case Intrinsic::riscv_vlseg3_mask: 779 case Intrinsic::riscv_vlseg4_mask: 780 case Intrinsic::riscv_vlseg5_mask: 781 case Intrinsic::riscv_vlseg6_mask: 782 case Intrinsic::riscv_vlseg7_mask: 783 case Intrinsic::riscv_vlseg8_mask: { 784 selectVLSEG(Node, /*IsMasked*/ true, /*IsStrided*/ false); 785 return; 786 } 787 case Intrinsic::riscv_vlsseg2: 788 case Intrinsic::riscv_vlsseg3: 789 case Intrinsic::riscv_vlsseg4: 790 case Intrinsic::riscv_vlsseg5: 791 case Intrinsic::riscv_vlsseg6: 792 case Intrinsic::riscv_vlsseg7: 793 case Intrinsic::riscv_vlsseg8: { 794 selectVLSEG(Node, /*IsMasked*/ false, /*IsStrided*/ true); 795 return; 796 } 797 case Intrinsic::riscv_vlsseg2_mask: 798 case Intrinsic::riscv_vlsseg3_mask: 799 case Intrinsic::riscv_vlsseg4_mask: 800 case Intrinsic::riscv_vlsseg5_mask: 801 case Intrinsic::riscv_vlsseg6_mask: 802 case Intrinsic::riscv_vlsseg7_mask: 803 case Intrinsic::riscv_vlsseg8_mask: { 804 selectVLSEG(Node, /*IsMasked*/ true, /*IsStrided*/ true); 805 return; 806 } 807 case Intrinsic::riscv_vloxseg2: 808 case Intrinsic::riscv_vloxseg3: 809 case Intrinsic::riscv_vloxseg4: 810 case Intrinsic::riscv_vloxseg5: 811 case Intrinsic::riscv_vloxseg6: 812 case Intrinsic::riscv_vloxseg7: 813 case Intrinsic::riscv_vloxseg8: 814 selectVLXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ true); 815 return; 816 case Intrinsic::riscv_vluxseg2: 817 case Intrinsic::riscv_vluxseg3: 818 case Intrinsic::riscv_vluxseg4: 819 case Intrinsic::riscv_vluxseg5: 820 case Intrinsic::riscv_vluxseg6: 821 case Intrinsic::riscv_vluxseg7: 822 case Intrinsic::riscv_vluxseg8: 823 selectVLXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ false); 824 return; 825 case Intrinsic::riscv_vloxseg2_mask: 826 case Intrinsic::riscv_vloxseg3_mask: 827 case Intrinsic::riscv_vloxseg4_mask: 828 case Intrinsic::riscv_vloxseg5_mask: 829 case Intrinsic::riscv_vloxseg6_mask: 830 case Intrinsic::riscv_vloxseg7_mask: 831 case Intrinsic::riscv_vloxseg8_mask: 832 selectVLXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ true); 833 return; 834 case Intrinsic::riscv_vluxseg2_mask: 835 case Intrinsic::riscv_vluxseg3_mask: 836 case Intrinsic::riscv_vluxseg4_mask: 837 case Intrinsic::riscv_vluxseg5_mask: 838 case Intrinsic::riscv_vluxseg6_mask: 839 case Intrinsic::riscv_vluxseg7_mask: 840 case Intrinsic::riscv_vluxseg8_mask: 841 selectVLXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ false); 842 return; 843 case Intrinsic::riscv_vlseg8ff: 844 case Intrinsic::riscv_vlseg7ff: 845 case Intrinsic::riscv_vlseg6ff: 846 case Intrinsic::riscv_vlseg5ff: 847 case Intrinsic::riscv_vlseg4ff: 848 case Intrinsic::riscv_vlseg3ff: 849 case Intrinsic::riscv_vlseg2ff: { 850 selectVLSEGFF(Node, /*IsMasked*/ false); 851 return; 852 } 853 case Intrinsic::riscv_vlseg8ff_mask: 854 case Intrinsic::riscv_vlseg7ff_mask: 855 case Intrinsic::riscv_vlseg6ff_mask: 856 case Intrinsic::riscv_vlseg5ff_mask: 857 case Intrinsic::riscv_vlseg4ff_mask: 858 case Intrinsic::riscv_vlseg3ff_mask: 859 case Intrinsic::riscv_vlseg2ff_mask: { 860 selectVLSEGFF(Node, /*IsMasked*/ true); 861 return; 862 } 863 case Intrinsic::riscv_vloxei: 864 case Intrinsic::riscv_vloxei_mask: 865 case Intrinsic::riscv_vluxei: 866 case Intrinsic::riscv_vluxei_mask: { 867 bool IsMasked = IntNo == Intrinsic::riscv_vloxei_mask || 868 IntNo == Intrinsic::riscv_vluxei_mask; 869 bool IsOrdered = IntNo == Intrinsic::riscv_vloxei || 870 IntNo == Intrinsic::riscv_vloxei_mask; 871 872 MVT VT = Node->getSimpleValueType(0); 873 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 874 875 unsigned CurOp = 2; 876 SmallVector<SDValue, 8> Operands; 877 if (IsMasked) 878 Operands.push_back(Node->getOperand(CurOp++)); 879 880 MVT IndexVT; 881 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, 882 /*IsStridedOrIndexed*/ true, Operands, 883 &IndexVT); 884 885 assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() && 886 "Element count mismatch"); 887 888 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 889 RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT); 890 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits()); 891 const RISCV::VLX_VSXPseudo *P = RISCV::getVLXPseudo( 892 IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL), 893 static_cast<unsigned>(IndexLMUL)); 894 MachineSDNode *Load = 895 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands); 896 897 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 898 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()}); 899 900 ReplaceNode(Node, Load); 901 return; 902 } 903 case Intrinsic::riscv_vle1: 904 case Intrinsic::riscv_vle: 905 case Intrinsic::riscv_vle_mask: 906 case Intrinsic::riscv_vlse: 907 case Intrinsic::riscv_vlse_mask: { 908 bool IsMasked = IntNo == Intrinsic::riscv_vle_mask || 909 IntNo == Intrinsic::riscv_vlse_mask; 910 bool IsStrided = 911 IntNo == Intrinsic::riscv_vlse || IntNo == Intrinsic::riscv_vlse_mask; 912 913 MVT VT = Node->getSimpleValueType(0); 914 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 915 916 unsigned CurOp = 2; 917 SmallVector<SDValue, 8> Operands; 918 if (IsMasked) 919 Operands.push_back(Node->getOperand(CurOp++)); 920 921 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided, 922 Operands); 923 924 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 925 const RISCV::VLEPseudo *P = 926 RISCV::getVLEPseudo(IsMasked, IsStrided, /*FF*/ false, Log2SEW, 927 static_cast<unsigned>(LMUL)); 928 MachineSDNode *Load = 929 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands); 930 931 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 932 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()}); 933 934 ReplaceNode(Node, Load); 935 return; 936 } 937 case Intrinsic::riscv_vleff: 938 case Intrinsic::riscv_vleff_mask: { 939 bool IsMasked = IntNo == Intrinsic::riscv_vleff_mask; 940 941 MVT VT = Node->getSimpleValueType(0); 942 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 943 944 unsigned CurOp = 2; 945 SmallVector<SDValue, 7> Operands; 946 if (IsMasked) 947 Operands.push_back(Node->getOperand(CurOp++)); 948 949 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, 950 /*IsStridedOrIndexed*/ false, Operands); 951 952 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 953 const RISCV::VLEPseudo *P = 954 RISCV::getVLEPseudo(IsMasked, /*Strided*/ false, /*FF*/ true, Log2SEW, 955 static_cast<unsigned>(LMUL)); 956 MachineSDNode *Load = 957 CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0), 958 MVT::Other, MVT::Glue, Operands); 959 SDNode *ReadVL = CurDAG->getMachineNode(RISCV::PseudoReadVL, DL, XLenVT, 960 /*Glue*/ SDValue(Load, 2)); 961 962 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 963 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()}); 964 965 ReplaceUses(SDValue(Node, 0), SDValue(Load, 0)); 966 ReplaceUses(SDValue(Node, 1), SDValue(ReadVL, 0)); // VL 967 ReplaceUses(SDValue(Node, 2), SDValue(Load, 1)); // Chain 968 CurDAG->RemoveDeadNode(Node); 969 return; 970 } 971 } 972 break; 973 } 974 case ISD::INTRINSIC_VOID: { 975 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 976 switch (IntNo) { 977 case Intrinsic::riscv_vsseg2: 978 case Intrinsic::riscv_vsseg3: 979 case Intrinsic::riscv_vsseg4: 980 case Intrinsic::riscv_vsseg5: 981 case Intrinsic::riscv_vsseg6: 982 case Intrinsic::riscv_vsseg7: 983 case Intrinsic::riscv_vsseg8: { 984 selectVSSEG(Node, /*IsMasked*/ false, /*IsStrided*/ false); 985 return; 986 } 987 case Intrinsic::riscv_vsseg2_mask: 988 case Intrinsic::riscv_vsseg3_mask: 989 case Intrinsic::riscv_vsseg4_mask: 990 case Intrinsic::riscv_vsseg5_mask: 991 case Intrinsic::riscv_vsseg6_mask: 992 case Intrinsic::riscv_vsseg7_mask: 993 case Intrinsic::riscv_vsseg8_mask: { 994 selectVSSEG(Node, /*IsMasked*/ true, /*IsStrided*/ false); 995 return; 996 } 997 case Intrinsic::riscv_vssseg2: 998 case Intrinsic::riscv_vssseg3: 999 case Intrinsic::riscv_vssseg4: 1000 case Intrinsic::riscv_vssseg5: 1001 case Intrinsic::riscv_vssseg6: 1002 case Intrinsic::riscv_vssseg7: 1003 case Intrinsic::riscv_vssseg8: { 1004 selectVSSEG(Node, /*IsMasked*/ false, /*IsStrided*/ true); 1005 return; 1006 } 1007 case Intrinsic::riscv_vssseg2_mask: 1008 case Intrinsic::riscv_vssseg3_mask: 1009 case Intrinsic::riscv_vssseg4_mask: 1010 case Intrinsic::riscv_vssseg5_mask: 1011 case Intrinsic::riscv_vssseg6_mask: 1012 case Intrinsic::riscv_vssseg7_mask: 1013 case Intrinsic::riscv_vssseg8_mask: { 1014 selectVSSEG(Node, /*IsMasked*/ true, /*IsStrided*/ true); 1015 return; 1016 } 1017 case Intrinsic::riscv_vsoxseg2: 1018 case Intrinsic::riscv_vsoxseg3: 1019 case Intrinsic::riscv_vsoxseg4: 1020 case Intrinsic::riscv_vsoxseg5: 1021 case Intrinsic::riscv_vsoxseg6: 1022 case Intrinsic::riscv_vsoxseg7: 1023 case Intrinsic::riscv_vsoxseg8: 1024 selectVSXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ true); 1025 return; 1026 case Intrinsic::riscv_vsuxseg2: 1027 case Intrinsic::riscv_vsuxseg3: 1028 case Intrinsic::riscv_vsuxseg4: 1029 case Intrinsic::riscv_vsuxseg5: 1030 case Intrinsic::riscv_vsuxseg6: 1031 case Intrinsic::riscv_vsuxseg7: 1032 case Intrinsic::riscv_vsuxseg8: 1033 selectVSXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ false); 1034 return; 1035 case Intrinsic::riscv_vsoxseg2_mask: 1036 case Intrinsic::riscv_vsoxseg3_mask: 1037 case Intrinsic::riscv_vsoxseg4_mask: 1038 case Intrinsic::riscv_vsoxseg5_mask: 1039 case Intrinsic::riscv_vsoxseg6_mask: 1040 case Intrinsic::riscv_vsoxseg7_mask: 1041 case Intrinsic::riscv_vsoxseg8_mask: 1042 selectVSXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ true); 1043 return; 1044 case Intrinsic::riscv_vsuxseg2_mask: 1045 case Intrinsic::riscv_vsuxseg3_mask: 1046 case Intrinsic::riscv_vsuxseg4_mask: 1047 case Intrinsic::riscv_vsuxseg5_mask: 1048 case Intrinsic::riscv_vsuxseg6_mask: 1049 case Intrinsic::riscv_vsuxseg7_mask: 1050 case Intrinsic::riscv_vsuxseg8_mask: 1051 selectVSXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ false); 1052 return; 1053 case Intrinsic::riscv_vsoxei: 1054 case Intrinsic::riscv_vsoxei_mask: 1055 case Intrinsic::riscv_vsuxei: 1056 case Intrinsic::riscv_vsuxei_mask: { 1057 bool IsMasked = IntNo == Intrinsic::riscv_vsoxei_mask || 1058 IntNo == Intrinsic::riscv_vsuxei_mask; 1059 bool IsOrdered = IntNo == Intrinsic::riscv_vsoxei || 1060 IntNo == Intrinsic::riscv_vsoxei_mask; 1061 1062 MVT VT = Node->getOperand(2)->getSimpleValueType(0); 1063 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 1064 1065 unsigned CurOp = 2; 1066 SmallVector<SDValue, 8> Operands; 1067 Operands.push_back(Node->getOperand(CurOp++)); // Store value. 1068 1069 MVT IndexVT; 1070 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, 1071 /*IsStridedOrIndexed*/ true, Operands, 1072 &IndexVT); 1073 1074 assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() && 1075 "Element count mismatch"); 1076 1077 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 1078 RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT); 1079 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits()); 1080 const RISCV::VLX_VSXPseudo *P = RISCV::getVSXPseudo( 1081 IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL), 1082 static_cast<unsigned>(IndexLMUL)); 1083 MachineSDNode *Store = 1084 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands); 1085 1086 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 1087 CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()}); 1088 1089 ReplaceNode(Node, Store); 1090 return; 1091 } 1092 case Intrinsic::riscv_vse1: 1093 case Intrinsic::riscv_vse: 1094 case Intrinsic::riscv_vse_mask: 1095 case Intrinsic::riscv_vsse: 1096 case Intrinsic::riscv_vsse_mask: { 1097 bool IsMasked = IntNo == Intrinsic::riscv_vse_mask || 1098 IntNo == Intrinsic::riscv_vsse_mask; 1099 bool IsStrided = 1100 IntNo == Intrinsic::riscv_vsse || IntNo == Intrinsic::riscv_vsse_mask; 1101 1102 MVT VT = Node->getOperand(2)->getSimpleValueType(0); 1103 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 1104 1105 unsigned CurOp = 2; 1106 SmallVector<SDValue, 8> Operands; 1107 Operands.push_back(Node->getOperand(CurOp++)); // Store value. 1108 1109 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided, 1110 Operands); 1111 1112 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 1113 const RISCV::VSEPseudo *P = RISCV::getVSEPseudo( 1114 IsMasked, IsStrided, Log2SEW, static_cast<unsigned>(LMUL)); 1115 MachineSDNode *Store = 1116 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands); 1117 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 1118 CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()}); 1119 1120 ReplaceNode(Node, Store); 1121 return; 1122 } 1123 } 1124 break; 1125 } 1126 case ISD::BITCAST: { 1127 MVT SrcVT = Node->getOperand(0).getSimpleValueType(); 1128 // Just drop bitcasts between vectors if both are fixed or both are 1129 // scalable. 1130 if ((VT.isScalableVector() && SrcVT.isScalableVector()) || 1131 (VT.isFixedLengthVector() && SrcVT.isFixedLengthVector())) { 1132 ReplaceUses(SDValue(Node, 0), Node->getOperand(0)); 1133 CurDAG->RemoveDeadNode(Node); 1134 return; 1135 } 1136 break; 1137 } 1138 case ISD::INSERT_SUBVECTOR: { 1139 SDValue V = Node->getOperand(0); 1140 SDValue SubV = Node->getOperand(1); 1141 SDLoc DL(SubV); 1142 auto Idx = Node->getConstantOperandVal(2); 1143 MVT SubVecVT = SubV.getSimpleValueType(); 1144 1145 const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering(); 1146 MVT SubVecContainerVT = SubVecVT; 1147 // Establish the correct scalable-vector types for any fixed-length type. 1148 if (SubVecVT.isFixedLengthVector()) 1149 SubVecContainerVT = TLI.getContainerForFixedLengthVector(SubVecVT); 1150 if (VT.isFixedLengthVector()) 1151 VT = TLI.getContainerForFixedLengthVector(VT); 1152 1153 const auto *TRI = Subtarget->getRegisterInfo(); 1154 unsigned SubRegIdx; 1155 std::tie(SubRegIdx, Idx) = 1156 RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs( 1157 VT, SubVecContainerVT, Idx, TRI); 1158 1159 // If the Idx hasn't been completely eliminated then this is a subvector 1160 // insert which doesn't naturally align to a vector register. These must 1161 // be handled using instructions to manipulate the vector registers. 1162 if (Idx != 0) 1163 break; 1164 1165 RISCVII::VLMUL SubVecLMUL = RISCVTargetLowering::getLMUL(SubVecContainerVT); 1166 bool IsSubVecPartReg = SubVecLMUL == RISCVII::VLMUL::LMUL_F2 || 1167 SubVecLMUL == RISCVII::VLMUL::LMUL_F4 || 1168 SubVecLMUL == RISCVII::VLMUL::LMUL_F8; 1169 (void)IsSubVecPartReg; // Silence unused variable warning without asserts. 1170 assert((!IsSubVecPartReg || V.isUndef()) && 1171 "Expecting lowering to have created legal INSERT_SUBVECTORs when " 1172 "the subvector is smaller than a full-sized register"); 1173 1174 // If we haven't set a SubRegIdx, then we must be going between 1175 // equally-sized LMUL groups (e.g. VR -> VR). This can be done as a copy. 1176 if (SubRegIdx == RISCV::NoSubRegister) { 1177 unsigned InRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(VT); 1178 assert(RISCVTargetLowering::getRegClassIDForVecVT(SubVecContainerVT) == 1179 InRegClassID && 1180 "Unexpected subvector extraction"); 1181 SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT); 1182 SDNode *NewNode = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 1183 DL, VT, SubV, RC); 1184 ReplaceNode(Node, NewNode); 1185 return; 1186 } 1187 1188 SDValue Insert = CurDAG->getTargetInsertSubreg(SubRegIdx, DL, VT, V, SubV); 1189 ReplaceNode(Node, Insert.getNode()); 1190 return; 1191 } 1192 case ISD::EXTRACT_SUBVECTOR: { 1193 SDValue V = Node->getOperand(0); 1194 auto Idx = Node->getConstantOperandVal(1); 1195 MVT InVT = V.getSimpleValueType(); 1196 SDLoc DL(V); 1197 1198 const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering(); 1199 MVT SubVecContainerVT = VT; 1200 // Establish the correct scalable-vector types for any fixed-length type. 1201 if (VT.isFixedLengthVector()) 1202 SubVecContainerVT = TLI.getContainerForFixedLengthVector(VT); 1203 if (InVT.isFixedLengthVector()) 1204 InVT = TLI.getContainerForFixedLengthVector(InVT); 1205 1206 const auto *TRI = Subtarget->getRegisterInfo(); 1207 unsigned SubRegIdx; 1208 std::tie(SubRegIdx, Idx) = 1209 RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs( 1210 InVT, SubVecContainerVT, Idx, TRI); 1211 1212 // If the Idx hasn't been completely eliminated then this is a subvector 1213 // extract which doesn't naturally align to a vector register. These must 1214 // be handled using instructions to manipulate the vector registers. 1215 if (Idx != 0) 1216 break; 1217 1218 // If we haven't set a SubRegIdx, then we must be going between 1219 // equally-sized LMUL types (e.g. VR -> VR). This can be done as a copy. 1220 if (SubRegIdx == RISCV::NoSubRegister) { 1221 unsigned InRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(InVT); 1222 assert(RISCVTargetLowering::getRegClassIDForVecVT(SubVecContainerVT) == 1223 InRegClassID && 1224 "Unexpected subvector extraction"); 1225 SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT); 1226 SDNode *NewNode = 1227 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DL, VT, V, RC); 1228 ReplaceNode(Node, NewNode); 1229 return; 1230 } 1231 1232 SDValue Extract = CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, V); 1233 ReplaceNode(Node, Extract.getNode()); 1234 return; 1235 } 1236 case RISCVISD::VMV_V_X_VL: 1237 case RISCVISD::VFMV_V_F_VL: { 1238 // Try to match splat of a scalar load to a strided load with stride of x0. 1239 SDValue Src = Node->getOperand(0); 1240 auto *Ld = dyn_cast<LoadSDNode>(Src); 1241 if (!Ld) 1242 break; 1243 EVT MemVT = Ld->getMemoryVT(); 1244 // The memory VT should be the same size as the element type. 1245 if (MemVT.getStoreSize() != VT.getVectorElementType().getStoreSize()) 1246 break; 1247 if (!IsProfitableToFold(Src, Node, Node) || 1248 !IsLegalToFold(Src, Node, Node, TM.getOptLevel())) 1249 break; 1250 1251 SDValue VL; 1252 selectVLOp(Node->getOperand(1), VL); 1253 1254 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits()); 1255 SDValue SEW = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT); 1256 1257 SDValue Operands[] = {Ld->getBasePtr(), 1258 CurDAG->getRegister(RISCV::X0, XLenVT), VL, SEW, 1259 Ld->getChain()}; 1260 1261 RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT); 1262 const RISCV::VLEPseudo *P = RISCV::getVLEPseudo( 1263 /*IsMasked*/ false, /*IsStrided*/ true, /*FF*/ false, Log2SEW, 1264 static_cast<unsigned>(LMUL)); 1265 MachineSDNode *Load = 1266 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands); 1267 1268 if (auto *MemOp = dyn_cast<MemSDNode>(Node)) 1269 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()}); 1270 1271 ReplaceNode(Node, Load); 1272 return; 1273 } 1274 } 1275 1276 // Select the default instruction. 1277 SelectCode(Node); 1278 } 1279 1280 bool RISCVDAGToDAGISel::SelectInlineAsmMemoryOperand( 1281 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) { 1282 switch (ConstraintID) { 1283 case InlineAsm::Constraint_m: 1284 // We just support simple memory operands that have a single address 1285 // operand and need no special handling. 1286 OutOps.push_back(Op); 1287 return false; 1288 case InlineAsm::Constraint_A: 1289 OutOps.push_back(Op); 1290 return false; 1291 default: 1292 break; 1293 } 1294 1295 return true; 1296 } 1297 1298 bool RISCVDAGToDAGISel::SelectAddrFI(SDValue Addr, SDValue &Base) { 1299 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { 1300 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT()); 1301 return true; 1302 } 1303 return false; 1304 } 1305 1306 bool RISCVDAGToDAGISel::SelectBaseAddr(SDValue Addr, SDValue &Base) { 1307 // If this is FrameIndex, select it directly. Otherwise just let it get 1308 // selected to a register independently. 1309 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr)) 1310 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT()); 1311 else 1312 Base = Addr; 1313 return true; 1314 } 1315 1316 bool RISCVDAGToDAGISel::selectShiftMask(SDValue N, unsigned ShiftWidth, 1317 SDValue &ShAmt) { 1318 // Shift instructions on RISCV only read the lower 5 or 6 bits of the shift 1319 // amount. If there is an AND on the shift amount, we can bypass it if it 1320 // doesn't affect any of those bits. 1321 if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(N.getOperand(1))) { 1322 const APInt &AndMask = N->getConstantOperandAPInt(1); 1323 1324 // Since the max shift amount is a power of 2 we can subtract 1 to make a 1325 // mask that covers the bits needed to represent all shift amounts. 1326 assert(isPowerOf2_32(ShiftWidth) && "Unexpected max shift amount!"); 1327 APInt ShMask(AndMask.getBitWidth(), ShiftWidth - 1); 1328 1329 if (ShMask.isSubsetOf(AndMask)) { 1330 ShAmt = N.getOperand(0); 1331 return true; 1332 } 1333 1334 // SimplifyDemandedBits may have optimized the mask so try restoring any 1335 // bits that are known zero. 1336 KnownBits Known = CurDAG->computeKnownBits(N->getOperand(0)); 1337 if (ShMask.isSubsetOf(AndMask | Known.Zero)) { 1338 ShAmt = N.getOperand(0); 1339 return true; 1340 } 1341 } 1342 1343 ShAmt = N; 1344 return true; 1345 } 1346 1347 bool RISCVDAGToDAGISel::selectSExti32(SDValue N, SDValue &Val) { 1348 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG && 1349 cast<VTSDNode>(N.getOperand(1))->getVT() == MVT::i32) { 1350 Val = N.getOperand(0); 1351 return true; 1352 } 1353 MVT VT = N.getSimpleValueType(); 1354 if (CurDAG->ComputeNumSignBits(N) > (VT.getSizeInBits() - 32)) { 1355 Val = N; 1356 return true; 1357 } 1358 1359 return false; 1360 } 1361 1362 bool RISCVDAGToDAGISel::selectZExti32(SDValue N, SDValue &Val) { 1363 if (N.getOpcode() == ISD::AND) { 1364 auto *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); 1365 if (C && C->getZExtValue() == UINT64_C(0xFFFFFFFF)) { 1366 Val = N.getOperand(0); 1367 return true; 1368 } 1369 } 1370 MVT VT = N.getSimpleValueType(); 1371 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), 32); 1372 if (CurDAG->MaskedValueIsZero(N, Mask)) { 1373 Val = N; 1374 return true; 1375 } 1376 1377 return false; 1378 } 1379 1380 // Check that it is a SLLIUW (Shift Logical Left Immediate Unsigned i32 1381 // on RV64). 1382 // SLLIUW is the same as SLLI except for the fact that it clears the bits 1383 // XLEN-1:32 of the input RS1 before shifting. 1384 // A PatFrag has already checked that it has the right structure: 1385 // 1386 // (AND (SHL RS1, VC2), VC1) 1387 // 1388 // We check that VC2, the shamt is less than 32, otherwise the pattern is 1389 // exactly the same as SLLI and we give priority to that. 1390 // Eventually we check that VC1, the mask used to clear the upper 32 bits 1391 // of RS1, is correct: 1392 // 1393 // VC1 == (0xFFFFFFFF << VC2) 1394 // 1395 bool RISCVDAGToDAGISel::MatchSLLIUW(SDNode *N) const { 1396 assert(N->getOpcode() == ISD::AND); 1397 assert(N->getOperand(0).getOpcode() == ISD::SHL); 1398 assert(isa<ConstantSDNode>(N->getOperand(1))); 1399 assert(isa<ConstantSDNode>(N->getOperand(0).getOperand(1))); 1400 1401 // The IsRV64 predicate is checked after PatFrag predicates so we can get 1402 // here even on RV32. 1403 if (!Subtarget->is64Bit()) 1404 return false; 1405 1406 SDValue Shl = N->getOperand(0); 1407 uint64_t VC1 = N->getConstantOperandVal(1); 1408 uint64_t VC2 = Shl.getConstantOperandVal(1); 1409 1410 // Immediate range should be enforced by uimm5 predicate. 1411 assert(VC2 < 32 && "Unexpected immediate"); 1412 return (VC1 >> VC2) == UINT64_C(0xFFFFFFFF); 1413 } 1414 1415 // Select VL as a 5 bit immediate or a value that will become a register. This 1416 // allows us to choose betwen VSETIVLI or VSETVLI later. 1417 bool RISCVDAGToDAGISel::selectVLOp(SDValue N, SDValue &VL) { 1418 auto *C = dyn_cast<ConstantSDNode>(N); 1419 if (C && isUInt<5>(C->getZExtValue())) 1420 VL = CurDAG->getTargetConstant(C->getZExtValue(), SDLoc(N), 1421 N->getValueType(0)); 1422 else 1423 VL = N; 1424 1425 return true; 1426 } 1427 1428 bool RISCVDAGToDAGISel::selectVSplat(SDValue N, SDValue &SplatVal) { 1429 if (N.getOpcode() != ISD::SPLAT_VECTOR && 1430 N.getOpcode() != RISCVISD::SPLAT_VECTOR_I64 && 1431 N.getOpcode() != RISCVISD::VMV_V_X_VL) 1432 return false; 1433 SplatVal = N.getOperand(0); 1434 return true; 1435 } 1436 1437 using ValidateFn = bool (*)(int64_t); 1438 1439 static bool selectVSplatSimmHelper(SDValue N, SDValue &SplatVal, 1440 SelectionDAG &DAG, 1441 const RISCVSubtarget &Subtarget, 1442 ValidateFn ValidateImm) { 1443 if ((N.getOpcode() != ISD::SPLAT_VECTOR && 1444 N.getOpcode() != RISCVISD::SPLAT_VECTOR_I64 && 1445 N.getOpcode() != RISCVISD::VMV_V_X_VL) || 1446 !isa<ConstantSDNode>(N.getOperand(0))) 1447 return false; 1448 1449 int64_t SplatImm = cast<ConstantSDNode>(N.getOperand(0))->getSExtValue(); 1450 1451 // ISD::SPLAT_VECTOR, RISCVISD::SPLAT_VECTOR_I64 and RISCVISD::VMV_V_X_VL 1452 // share semantics when the operand type is wider than the resulting vector 1453 // element type: an implicit truncation first takes place. Therefore, perform 1454 // a manual truncation/sign-extension in order to ignore any truncated bits 1455 // and catch any zero-extended immediate. 1456 // For example, we wish to match (i8 -1) -> (XLenVT 255) as a simm5 by first 1457 // sign-extending to (XLenVT -1). 1458 MVT XLenVT = Subtarget.getXLenVT(); 1459 assert(XLenVT == N.getOperand(0).getSimpleValueType() && 1460 "Unexpected splat operand type"); 1461 MVT EltVT = N.getSimpleValueType().getVectorElementType(); 1462 if (EltVT.bitsLT(XLenVT)) 1463 SplatImm = SignExtend64(SplatImm, EltVT.getSizeInBits()); 1464 1465 if (!ValidateImm(SplatImm)) 1466 return false; 1467 1468 SplatVal = DAG.getTargetConstant(SplatImm, SDLoc(N), XLenVT); 1469 return true; 1470 } 1471 1472 bool RISCVDAGToDAGISel::selectVSplatSimm5(SDValue N, SDValue &SplatVal) { 1473 return selectVSplatSimmHelper(N, SplatVal, *CurDAG, *Subtarget, 1474 [](int64_t Imm) { return isInt<5>(Imm); }); 1475 } 1476 1477 bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1(SDValue N, SDValue &SplatVal) { 1478 return selectVSplatSimmHelper( 1479 N, SplatVal, *CurDAG, *Subtarget, 1480 [](int64_t Imm) { return (isInt<5>(Imm) && Imm != -16) || Imm == 16; }); 1481 } 1482 1483 bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1NonZero(SDValue N, 1484 SDValue &SplatVal) { 1485 return selectVSplatSimmHelper( 1486 N, SplatVal, *CurDAG, *Subtarget, [](int64_t Imm) { 1487 return Imm != 0 && ((isInt<5>(Imm) && Imm != -16) || Imm == 16); 1488 }); 1489 } 1490 1491 bool RISCVDAGToDAGISel::selectVSplatUimm5(SDValue N, SDValue &SplatVal) { 1492 if ((N.getOpcode() != ISD::SPLAT_VECTOR && 1493 N.getOpcode() != RISCVISD::SPLAT_VECTOR_I64 && 1494 N.getOpcode() != RISCVISD::VMV_V_X_VL) || 1495 !isa<ConstantSDNode>(N.getOperand(0))) 1496 return false; 1497 1498 int64_t SplatImm = cast<ConstantSDNode>(N.getOperand(0))->getSExtValue(); 1499 1500 if (!isUInt<5>(SplatImm)) 1501 return false; 1502 1503 SplatVal = 1504 CurDAG->getTargetConstant(SplatImm, SDLoc(N), Subtarget->getXLenVT()); 1505 1506 return true; 1507 } 1508 1509 bool RISCVDAGToDAGISel::selectRVVSimm5(SDValue N, unsigned Width, 1510 SDValue &Imm) { 1511 if (auto *C = dyn_cast<ConstantSDNode>(N)) { 1512 int64_t ImmVal = SignExtend64(C->getSExtValue(), Width); 1513 1514 if (!isInt<5>(ImmVal)) 1515 return false; 1516 1517 Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), Subtarget->getXLenVT()); 1518 return true; 1519 } 1520 1521 return false; 1522 } 1523 1524 // Merge an ADDI into the offset of a load/store instruction where possible. 1525 // (load (addi base, off1), off2) -> (load base, off1+off2) 1526 // (store val, (addi base, off1), off2) -> (store val, base, off1+off2) 1527 // This is possible when off1+off2 fits a 12-bit immediate. 1528 void RISCVDAGToDAGISel::doPeepholeLoadStoreADDI() { 1529 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); 1530 ++Position; 1531 1532 while (Position != CurDAG->allnodes_begin()) { 1533 SDNode *N = &*--Position; 1534 // Skip dead nodes and any non-machine opcodes. 1535 if (N->use_empty() || !N->isMachineOpcode()) 1536 continue; 1537 1538 int OffsetOpIdx; 1539 int BaseOpIdx; 1540 1541 // Only attempt this optimisation for I-type loads and S-type stores. 1542 switch (N->getMachineOpcode()) { 1543 default: 1544 continue; 1545 case RISCV::LB: 1546 case RISCV::LH: 1547 case RISCV::LW: 1548 case RISCV::LBU: 1549 case RISCV::LHU: 1550 case RISCV::LWU: 1551 case RISCV::LD: 1552 case RISCV::FLH: 1553 case RISCV::FLW: 1554 case RISCV::FLD: 1555 BaseOpIdx = 0; 1556 OffsetOpIdx = 1; 1557 break; 1558 case RISCV::SB: 1559 case RISCV::SH: 1560 case RISCV::SW: 1561 case RISCV::SD: 1562 case RISCV::FSH: 1563 case RISCV::FSW: 1564 case RISCV::FSD: 1565 BaseOpIdx = 1; 1566 OffsetOpIdx = 2; 1567 break; 1568 } 1569 1570 if (!isa<ConstantSDNode>(N->getOperand(OffsetOpIdx))) 1571 continue; 1572 1573 SDValue Base = N->getOperand(BaseOpIdx); 1574 1575 // If the base is an ADDI, we can merge it in to the load/store. 1576 if (!Base.isMachineOpcode() || Base.getMachineOpcode() != RISCV::ADDI) 1577 continue; 1578 1579 SDValue ImmOperand = Base.getOperand(1); 1580 uint64_t Offset2 = N->getConstantOperandVal(OffsetOpIdx); 1581 1582 if (auto *Const = dyn_cast<ConstantSDNode>(ImmOperand)) { 1583 int64_t Offset1 = Const->getSExtValue(); 1584 int64_t CombinedOffset = Offset1 + Offset2; 1585 if (!isInt<12>(CombinedOffset)) 1586 continue; 1587 ImmOperand = CurDAG->getTargetConstant(CombinedOffset, SDLoc(ImmOperand), 1588 ImmOperand.getValueType()); 1589 } else if (auto *GA = dyn_cast<GlobalAddressSDNode>(ImmOperand)) { 1590 // If the off1 in (addi base, off1) is a global variable's address (its 1591 // low part, really), then we can rely on the alignment of that variable 1592 // to provide a margin of safety before off1 can overflow the 12 bits. 1593 // Check if off2 falls within that margin; if so off1+off2 can't overflow. 1594 const DataLayout &DL = CurDAG->getDataLayout(); 1595 Align Alignment = GA->getGlobal()->getPointerAlignment(DL); 1596 if (Offset2 != 0 && Alignment <= Offset2) 1597 continue; 1598 int64_t Offset1 = GA->getOffset(); 1599 int64_t CombinedOffset = Offset1 + Offset2; 1600 ImmOperand = CurDAG->getTargetGlobalAddress( 1601 GA->getGlobal(), SDLoc(ImmOperand), ImmOperand.getValueType(), 1602 CombinedOffset, GA->getTargetFlags()); 1603 } else if (auto *CP = dyn_cast<ConstantPoolSDNode>(ImmOperand)) { 1604 // Ditto. 1605 Align Alignment = CP->getAlign(); 1606 if (Offset2 != 0 && Alignment <= Offset2) 1607 continue; 1608 int64_t Offset1 = CP->getOffset(); 1609 int64_t CombinedOffset = Offset1 + Offset2; 1610 ImmOperand = CurDAG->getTargetConstantPool( 1611 CP->getConstVal(), ImmOperand.getValueType(), CP->getAlign(), 1612 CombinedOffset, CP->getTargetFlags()); 1613 } else { 1614 continue; 1615 } 1616 1617 LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); 1618 LLVM_DEBUG(Base->dump(CurDAG)); 1619 LLVM_DEBUG(dbgs() << "\nN: "); 1620 LLVM_DEBUG(N->dump(CurDAG)); 1621 LLVM_DEBUG(dbgs() << "\n"); 1622 1623 // Modify the offset operand of the load/store. 1624 if (BaseOpIdx == 0) // Load 1625 CurDAG->UpdateNodeOperands(N, Base.getOperand(0), ImmOperand, 1626 N->getOperand(2)); 1627 else // Store 1628 CurDAG->UpdateNodeOperands(N, N->getOperand(0), Base.getOperand(0), 1629 ImmOperand, N->getOperand(3)); 1630 1631 // The add-immediate may now be dead, in which case remove it. 1632 if (Base.getNode()->use_empty()) 1633 CurDAG->RemoveDeadNode(Base.getNode()); 1634 } 1635 } 1636 1637 // This pass converts a legalized DAG into a RISCV-specific DAG, ready 1638 // for instruction scheduling. 1639 FunctionPass *llvm::createRISCVISelDag(RISCVTargetMachine &TM) { 1640 return new RISCVDAGToDAGISel(TM); 1641 } 1642