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 "Utils/RISCVMatInt.h" 16 #include "llvm/CodeGen/MachineFrameInfo.h" 17 #include "llvm/Support/Alignment.h" 18 #include "llvm/Support/Debug.h" 19 #include "llvm/Support/MathExtras.h" 20 #include "llvm/Support/raw_ostream.h" 21 22 using namespace llvm; 23 24 #define DEBUG_TYPE "riscv-isel" 25 26 void RISCVDAGToDAGISel::PostprocessISelDAG() { 27 doPeepholeLoadStoreADDI(); 28 } 29 30 static SDNode *selectImm(SelectionDAG *CurDAG, const SDLoc &DL, int64_t Imm, 31 MVT XLenVT) { 32 RISCVMatInt::InstSeq Seq; 33 RISCVMatInt::generateInstSeq(Imm, XLenVT == MVT::i64, Seq); 34 35 SDNode *Result = nullptr; 36 SDValue SrcReg = CurDAG->getRegister(RISCV::X0, XLenVT); 37 for (RISCVMatInt::Inst &Inst : Seq) { 38 SDValue SDImm = CurDAG->getTargetConstant(Inst.Imm, DL, XLenVT); 39 if (Inst.Opc == RISCV::LUI) 40 Result = CurDAG->getMachineNode(RISCV::LUI, DL, XLenVT, SDImm); 41 else 42 Result = CurDAG->getMachineNode(Inst.Opc, DL, XLenVT, SrcReg, SDImm); 43 44 // Only the first instruction has X0 as its source. 45 SrcReg = SDValue(Result, 0); 46 } 47 48 return Result; 49 } 50 51 // Returns true if the Node is an ISD::AND with a constant argument. If so, 52 // set Mask to that constant value. 53 static bool isConstantMask(SDNode *Node, uint64_t &Mask) { 54 if (Node->getOpcode() == ISD::AND && 55 Node->getOperand(1).getOpcode() == ISD::Constant) { 56 Mask = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 57 return true; 58 } 59 return false; 60 } 61 62 void RISCVDAGToDAGISel::Select(SDNode *Node) { 63 // If we have a custom node, we have already selected. 64 if (Node->isMachineOpcode()) { 65 LLVM_DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n"); 66 Node->setNodeId(-1); 67 return; 68 } 69 70 // Instruction Selection not handled by the auto-generated tablegen selection 71 // should be handled here. 72 unsigned Opcode = Node->getOpcode(); 73 MVT XLenVT = Subtarget->getXLenVT(); 74 SDLoc DL(Node); 75 EVT VT = Node->getValueType(0); 76 77 switch (Opcode) { 78 case ISD::ADD: { 79 // Optimize (add r, imm) to (addi (addi r, imm0) imm1) if applicable. The 80 // immediate must be in specific ranges and have a single use. 81 if (auto *ConstOp = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { 82 if (!(ConstOp->hasOneUse())) 83 break; 84 // The imm must be in range [-4096,-2049] or [2048,4094]. 85 int64_t Imm = ConstOp->getSExtValue(); 86 if (!(-4096 <= Imm && Imm <= -2049) && !(2048 <= Imm && Imm <= 4094)) 87 break; 88 // Break the imm to imm0+imm1. 89 SDLoc DL(Node); 90 EVT VT = Node->getValueType(0); 91 const SDValue ImmOp0 = CurDAG->getTargetConstant(Imm - Imm / 2, DL, VT); 92 const SDValue ImmOp1 = CurDAG->getTargetConstant(Imm / 2, DL, VT); 93 auto *NodeAddi0 = CurDAG->getMachineNode(RISCV::ADDI, DL, VT, 94 Node->getOperand(0), ImmOp0); 95 auto *NodeAddi1 = CurDAG->getMachineNode(RISCV::ADDI, DL, VT, 96 SDValue(NodeAddi0, 0), ImmOp1); 97 ReplaceNode(Node, NodeAddi1); 98 return; 99 } 100 break; 101 } 102 case ISD::Constant: { 103 auto ConstNode = cast<ConstantSDNode>(Node); 104 if (VT == XLenVT && ConstNode->isNullValue()) { 105 SDValue New = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), SDLoc(Node), 106 RISCV::X0, XLenVT); 107 ReplaceNode(Node, New.getNode()); 108 return; 109 } 110 int64_t Imm = ConstNode->getSExtValue(); 111 if (XLenVT == MVT::i64) { 112 ReplaceNode(Node, selectImm(CurDAG, SDLoc(Node), Imm, XLenVT)); 113 return; 114 } 115 break; 116 } 117 case ISD::FrameIndex: { 118 SDValue Imm = CurDAG->getTargetConstant(0, DL, XLenVT); 119 int FI = cast<FrameIndexSDNode>(Node)->getIndex(); 120 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT); 121 ReplaceNode(Node, CurDAG->getMachineNode(RISCV::ADDI, DL, VT, TFI, Imm)); 122 return; 123 } 124 case ISD::SRL: { 125 if (!Subtarget->is64Bit()) 126 break; 127 SDNode *Op0 = Node->getOperand(0).getNode(); 128 uint64_t Mask; 129 // Match (srl (and val, mask), imm) where the result would be a 130 // zero-extended 32-bit integer. i.e. the mask is 0xffffffff or the result 131 // is equivalent to this (SimplifyDemandedBits may have removed lower bits 132 // from the mask that aren't necessary due to the right-shifting). 133 if (isa<ConstantSDNode>(Node->getOperand(1)) && isConstantMask(Op0, Mask)) { 134 uint64_t ShAmt = Node->getConstantOperandVal(1); 135 136 if ((Mask | maskTrailingOnes<uint64_t>(ShAmt)) == 0xffffffff) { 137 SDValue ShAmtVal = 138 CurDAG->getTargetConstant(ShAmt, SDLoc(Node), XLenVT); 139 CurDAG->SelectNodeTo(Node, RISCV::SRLIW, XLenVT, Op0->getOperand(0), 140 ShAmtVal); 141 return; 142 } 143 } 144 break; 145 } 146 case RISCVISD::READ_CYCLE_WIDE: 147 assert(!Subtarget->is64Bit() && "READ_CYCLE_WIDE is only used on riscv32"); 148 149 ReplaceNode(Node, CurDAG->getMachineNode(RISCV::ReadCycleWide, DL, MVT::i32, 150 MVT::i32, MVT::Other, 151 Node->getOperand(0))); 152 return; 153 } 154 155 // Select the default instruction. 156 SelectCode(Node); 157 } 158 159 bool RISCVDAGToDAGISel::SelectInlineAsmMemoryOperand( 160 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) { 161 switch (ConstraintID) { 162 case InlineAsm::Constraint_m: 163 // We just support simple memory operands that have a single address 164 // operand and need no special handling. 165 OutOps.push_back(Op); 166 return false; 167 case InlineAsm::Constraint_A: 168 OutOps.push_back(Op); 169 return false; 170 default: 171 break; 172 } 173 174 return true; 175 } 176 177 bool RISCVDAGToDAGISel::SelectAddrFI(SDValue Addr, SDValue &Base) { 178 if (auto FIN = dyn_cast<FrameIndexSDNode>(Addr)) { 179 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT()); 180 return true; 181 } 182 return false; 183 } 184 185 // Check that it is a SLOI (Shift Left Ones Immediate). We first check that 186 // it is the right node tree: 187 // 188 // (OR (SHL RS1, VC2), VC1) 189 // 190 // and then we check that VC1, the mask used to fill with ones, is compatible 191 // with VC2, the shamt: 192 // 193 // VC1 == maskTrailingOnes<uint64_t>(VC2) 194 195 bool RISCVDAGToDAGISel::SelectSLOI(SDValue N, SDValue &RS1, SDValue &Shamt) { 196 MVT XLenVT = Subtarget->getXLenVT(); 197 if (N.getOpcode() == ISD::OR) { 198 SDValue Or = N; 199 if (Or.getOperand(0).getOpcode() == ISD::SHL) { 200 SDValue Shl = Or.getOperand(0); 201 if (isa<ConstantSDNode>(Shl.getOperand(1)) && 202 isa<ConstantSDNode>(Or.getOperand(1))) { 203 if (XLenVT == MVT::i64) { 204 uint64_t VC1 = Or.getConstantOperandVal(1); 205 uint64_t VC2 = Shl.getConstantOperandVal(1); 206 if (VC1 == maskTrailingOnes<uint64_t>(VC2)) { 207 RS1 = Shl.getOperand(0); 208 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 209 Shl.getOperand(1).getValueType()); 210 return true; 211 } 212 } 213 if (XLenVT == MVT::i32) { 214 uint32_t VC1 = Or.getConstantOperandVal(1); 215 uint32_t VC2 = Shl.getConstantOperandVal(1); 216 if (VC1 == maskTrailingOnes<uint32_t>(VC2)) { 217 RS1 = Shl.getOperand(0); 218 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 219 Shl.getOperand(1).getValueType()); 220 return true; 221 } 222 } 223 } 224 } 225 } 226 return false; 227 } 228 229 // Check that it is a SROI (Shift Right Ones Immediate). We first check that 230 // it is the right node tree: 231 // 232 // (OR (SRL RS1, VC2), VC1) 233 // 234 // and then we check that VC1, the mask used to fill with ones, is compatible 235 // with VC2, the shamt: 236 // 237 // VC1 == maskLeadingOnes<uint64_t>(VC2) 238 239 bool RISCVDAGToDAGISel::SelectSROI(SDValue N, SDValue &RS1, SDValue &Shamt) { 240 MVT XLenVT = Subtarget->getXLenVT(); 241 if (N.getOpcode() == ISD::OR) { 242 SDValue Or = N; 243 if (Or.getOperand(0).getOpcode() == ISD::SRL) { 244 SDValue Srl = Or.getOperand(0); 245 if (isa<ConstantSDNode>(Srl.getOperand(1)) && 246 isa<ConstantSDNode>(Or.getOperand(1))) { 247 if (XLenVT == MVT::i64) { 248 uint64_t VC1 = Or.getConstantOperandVal(1); 249 uint64_t VC2 = Srl.getConstantOperandVal(1); 250 if (VC1 == maskLeadingOnes<uint64_t>(VC2)) { 251 RS1 = Srl.getOperand(0); 252 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 253 Srl.getOperand(1).getValueType()); 254 return true; 255 } 256 } 257 if (XLenVT == MVT::i32) { 258 uint32_t VC1 = Or.getConstantOperandVal(1); 259 uint32_t VC2 = Srl.getConstantOperandVal(1); 260 if (VC1 == maskLeadingOnes<uint32_t>(VC2)) { 261 RS1 = Srl.getOperand(0); 262 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 263 Srl.getOperand(1).getValueType()); 264 return true; 265 } 266 } 267 } 268 } 269 } 270 return false; 271 } 272 273 // Check that it is a SLLIUW (Shift Logical Left Immediate Unsigned i32 274 // on RV64). 275 // SLLIUW is the same as SLLI except for the fact that it clears the bits 276 // XLEN-1:32 of the input RS1 before shifting. 277 // We first check that it is the right node tree: 278 // 279 // (AND (SHL RS1, VC2), VC1) 280 // 281 // We check that VC2, the shamt is less than 32, otherwise the pattern is 282 // exactly the same as SLLI and we give priority to that. 283 // Eventually we check that that VC1, the mask used to clear the upper 32 bits 284 // of RS1, is correct: 285 // 286 // VC1 == (0xFFFFFFFF << VC2) 287 288 bool RISCVDAGToDAGISel::SelectSLLIUW(SDValue N, SDValue &RS1, SDValue &Shamt) { 289 if (N.getOpcode() == ISD::AND && Subtarget->getXLenVT() == MVT::i64) { 290 SDValue And = N; 291 if (And.getOperand(0).getOpcode() == ISD::SHL) { 292 SDValue Shl = And.getOperand(0); 293 if (isa<ConstantSDNode>(Shl.getOperand(1)) && 294 isa<ConstantSDNode>(And.getOperand(1))) { 295 uint64_t VC1 = And.getConstantOperandVal(1); 296 uint64_t VC2 = Shl.getConstantOperandVal(1); 297 if (VC2 < 32 && VC1 == ((uint64_t)0xFFFFFFFF << VC2)) { 298 RS1 = Shl.getOperand(0); 299 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 300 Shl.getOperand(1).getValueType()); 301 return true; 302 } 303 } 304 } 305 } 306 return false; 307 } 308 309 // Check that it is a SLOIW (Shift Left Ones Immediate i32 on RV64). 310 // We first check that it is the right node tree: 311 // 312 // (SIGN_EXTEND_INREG (OR (SHL RS1, VC2), VC1)) 313 // 314 // and then we check that VC1, the mask used to fill with ones, is compatible 315 // with VC2, the shamt: 316 // 317 // VC1 == maskTrailingOnes<uint32_t>(VC2) 318 319 bool RISCVDAGToDAGISel::SelectSLOIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 320 if (Subtarget->getXLenVT() == MVT::i64 && 321 N.getOpcode() == ISD::SIGN_EXTEND_INREG && 322 cast<VTSDNode>(N.getOperand(1))->getVT() == MVT::i32) { 323 if (N.getOperand(0).getOpcode() == ISD::OR) { 324 SDValue Or = N.getOperand(0); 325 if (Or.getOperand(0).getOpcode() == ISD::SHL) { 326 SDValue Shl = Or.getOperand(0); 327 if (isa<ConstantSDNode>(Shl.getOperand(1)) && 328 isa<ConstantSDNode>(Or.getOperand(1))) { 329 uint32_t VC1 = Or.getConstantOperandVal(1); 330 uint32_t VC2 = Shl.getConstantOperandVal(1); 331 if (VC1 == maskTrailingOnes<uint32_t>(VC2)) { 332 RS1 = Shl.getOperand(0); 333 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 334 Shl.getOperand(1).getValueType()); 335 return true; 336 } 337 } 338 } 339 } 340 } 341 return false; 342 } 343 344 // Check that it is a SROIW (Shift Right Ones Immediate i32 on RV64). 345 // We first check that it is the right node tree: 346 // 347 // (OR (SHL RS1, VC2), VC1) 348 // 349 // and then we check that VC1, the mask used to fill with ones, is compatible 350 // with VC2, the shamt: 351 // 352 // VC1 == maskLeadingOnes<uint32_t>(VC2) 353 354 bool RISCVDAGToDAGISel::SelectSROIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 355 if (N.getOpcode() == ISD::OR && Subtarget->getXLenVT() == MVT::i64) { 356 SDValue Or = N; 357 if (Or.getOperand(0).getOpcode() == ISD::SRL) { 358 SDValue Srl = Or.getOperand(0); 359 if (isa<ConstantSDNode>(Srl.getOperand(1)) && 360 isa<ConstantSDNode>(Or.getOperand(1))) { 361 uint32_t VC1 = Or.getConstantOperandVal(1); 362 uint32_t VC2 = Srl.getConstantOperandVal(1); 363 if (VC1 == maskLeadingOnes<uint32_t>(VC2)) { 364 RS1 = Srl.getOperand(0); 365 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 366 Srl.getOperand(1).getValueType()); 367 return true; 368 } 369 } 370 } 371 } 372 return false; 373 } 374 375 // Check that it is a RORIW (i32 Right Rotate Immediate on RV64). 376 // We first check that it is the right node tree: 377 // 378 // (SIGN_EXTEND_INREG (OR (SHL RS1, VC2), 379 // (SRL (AND RS1, VC3), VC1))) 380 // 381 // Then we check that the constant operands respect these constraints: 382 // 383 // VC2 == 32 - VC1 384 // VC3 | maskTrailingOnes<uint64_t>(VC1) == 0xffffffff 385 // 386 // being VC1 the Shamt we need, VC2 the complementary of Shamt over 32 387 // and VC3 being 0xffffffff after accounting for SimplifyDemandedBits removing 388 // some bits due to the right shift. 389 390 bool RISCVDAGToDAGISel::SelectRORIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 391 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG && 392 Subtarget->getXLenVT() == MVT::i64 && 393 cast<VTSDNode>(N.getOperand(1))->getVT() == MVT::i32) { 394 if (N.getOperand(0).getOpcode() == ISD::OR) { 395 SDValue Or = N.getOperand(0); 396 SDValue Shl = Or.getOperand(0); 397 SDValue Srl = Or.getOperand(1); 398 399 // OR is commutable so canonicalize SHL to LHS. 400 if (Srl.getOpcode() == ISD::SHL) 401 std::swap(Shl, Srl); 402 403 if (Shl.getOpcode() == ISD::SHL && Srl.getOpcode() == ISD::SRL) { 404 if (Srl.getOperand(0).getOpcode() == ISD::AND) { 405 SDValue And = Srl.getOperand(0); 406 if (And.getOperand(0) == Shl.getOperand(0) && 407 isa<ConstantSDNode>(Srl.getOperand(1)) && 408 isa<ConstantSDNode>(Shl.getOperand(1)) && 409 isa<ConstantSDNode>(And.getOperand(1))) { 410 uint64_t VC1 = Srl.getConstantOperandVal(1); 411 uint64_t VC2 = Shl.getConstantOperandVal(1); 412 uint64_t VC3 = And.getConstantOperandVal(1); 413 // The mask needs to be 0xffffffff, but SimplifyDemandedBits may 414 // have removed lower bits that aren't necessary due to the right 415 // shift. 416 if (VC2 == (32 - VC1) && 417 (VC3 | maskTrailingOnes<uint64_t>(VC1)) == 0xffffffff) { 418 RS1 = Shl.getOperand(0); 419 Shamt = CurDAG->getTargetConstant(VC1, SDLoc(N), 420 Srl.getOperand(1).getValueType()); 421 return true; 422 } 423 } 424 } 425 } 426 } 427 } 428 return false; 429 } 430 431 // Merge an ADDI into the offset of a load/store instruction where possible. 432 // (load (addi base, off1), off2) -> (load base, off1+off2) 433 // (store val, (addi base, off1), off2) -> (store val, base, off1+off2) 434 // This is possible when off1+off2 fits a 12-bit immediate. 435 void RISCVDAGToDAGISel::doPeepholeLoadStoreADDI() { 436 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); 437 ++Position; 438 439 while (Position != CurDAG->allnodes_begin()) { 440 SDNode *N = &*--Position; 441 // Skip dead nodes and any non-machine opcodes. 442 if (N->use_empty() || !N->isMachineOpcode()) 443 continue; 444 445 int OffsetOpIdx; 446 int BaseOpIdx; 447 448 // Only attempt this optimisation for I-type loads and S-type stores. 449 switch (N->getMachineOpcode()) { 450 default: 451 continue; 452 case RISCV::LB: 453 case RISCV::LH: 454 case RISCV::LW: 455 case RISCV::LBU: 456 case RISCV::LHU: 457 case RISCV::LWU: 458 case RISCV::LD: 459 case RISCV::FLW: 460 case RISCV::FLD: 461 BaseOpIdx = 0; 462 OffsetOpIdx = 1; 463 break; 464 case RISCV::SB: 465 case RISCV::SH: 466 case RISCV::SW: 467 case RISCV::SD: 468 case RISCV::FSW: 469 case RISCV::FSD: 470 BaseOpIdx = 1; 471 OffsetOpIdx = 2; 472 break; 473 } 474 475 if (!isa<ConstantSDNode>(N->getOperand(OffsetOpIdx))) 476 continue; 477 478 SDValue Base = N->getOperand(BaseOpIdx); 479 480 // If the base is an ADDI, we can merge it in to the load/store. 481 if (!Base.isMachineOpcode() || Base.getMachineOpcode() != RISCV::ADDI) 482 continue; 483 484 SDValue ImmOperand = Base.getOperand(1); 485 uint64_t Offset2 = N->getConstantOperandVal(OffsetOpIdx); 486 487 if (auto Const = dyn_cast<ConstantSDNode>(ImmOperand)) { 488 int64_t Offset1 = Const->getSExtValue(); 489 int64_t CombinedOffset = Offset1 + Offset2; 490 if (!isInt<12>(CombinedOffset)) 491 continue; 492 ImmOperand = CurDAG->getTargetConstant(CombinedOffset, SDLoc(ImmOperand), 493 ImmOperand.getValueType()); 494 } else if (auto GA = dyn_cast<GlobalAddressSDNode>(ImmOperand)) { 495 // If the off1 in (addi base, off1) is a global variable's address (its 496 // low part, really), then we can rely on the alignment of that variable 497 // to provide a margin of safety before off1 can overflow the 12 bits. 498 // Check if off2 falls within that margin; if so off1+off2 can't overflow. 499 const DataLayout &DL = CurDAG->getDataLayout(); 500 Align Alignment = GA->getGlobal()->getPointerAlignment(DL); 501 if (Offset2 != 0 && Alignment <= Offset2) 502 continue; 503 int64_t Offset1 = GA->getOffset(); 504 int64_t CombinedOffset = Offset1 + Offset2; 505 ImmOperand = CurDAG->getTargetGlobalAddress( 506 GA->getGlobal(), SDLoc(ImmOperand), ImmOperand.getValueType(), 507 CombinedOffset, GA->getTargetFlags()); 508 } else if (auto CP = dyn_cast<ConstantPoolSDNode>(ImmOperand)) { 509 // Ditto. 510 Align Alignment = CP->getAlign(); 511 if (Offset2 != 0 && Alignment <= Offset2) 512 continue; 513 int64_t Offset1 = CP->getOffset(); 514 int64_t CombinedOffset = Offset1 + Offset2; 515 ImmOperand = CurDAG->getTargetConstantPool( 516 CP->getConstVal(), ImmOperand.getValueType(), CP->getAlign(), 517 CombinedOffset, CP->getTargetFlags()); 518 } else { 519 continue; 520 } 521 522 LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); 523 LLVM_DEBUG(Base->dump(CurDAG)); 524 LLVM_DEBUG(dbgs() << "\nN: "); 525 LLVM_DEBUG(N->dump(CurDAG)); 526 LLVM_DEBUG(dbgs() << "\n"); 527 528 // Modify the offset operand of the load/store. 529 if (BaseOpIdx == 0) // Load 530 CurDAG->UpdateNodeOperands(N, Base.getOperand(0), ImmOperand, 531 N->getOperand(2)); 532 else // Store 533 CurDAG->UpdateNodeOperands(N, N->getOperand(0), Base.getOperand(0), 534 ImmOperand, N->getOperand(3)); 535 536 // The add-immediate may now be dead, in which case remove it. 537 if (Base.getNode()->use_empty()) 538 CurDAG->RemoveDeadNode(Base.getNode()); 539 } 540 } 541 542 // This pass converts a legalized DAG into a RISCV-specific DAG, ready 543 // for instruction scheduling. 544 FunctionPass *llvm::createRISCVISelDag(RISCVTargetMachine &TM) { 545 return new RISCVDAGToDAGISel(TM); 546 } 547