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 // VC2 < 32 318 // VC1 == maskTrailingOnes<uint64_t>(VC2) 319 320 bool RISCVDAGToDAGISel::SelectSLOIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 321 assert(Subtarget->is64Bit() && "SLOIW should only be matched on RV64"); 322 if (N.getOpcode() != ISD::SIGN_EXTEND_INREG || 323 cast<VTSDNode>(N.getOperand(1))->getVT() != MVT::i32) 324 return false; 325 326 SDValue Or = N.getOperand(0); 327 328 if (Or.getOpcode() != ISD::OR || !isa<ConstantSDNode>(Or.getOperand(1))) 329 return false; 330 331 SDValue Shl = Or.getOperand(0); 332 if (Shl.getOpcode() != ISD::SHL || !isa<ConstantSDNode>(Shl.getOperand(1))) 333 return false; 334 335 uint64_t VC1 = Or.getConstantOperandVal(1); 336 uint64_t VC2 = Shl.getConstantOperandVal(1); 337 338 if (VC2 >= 32 || VC1 != maskTrailingOnes<uint64_t>(VC2)) 339 return false; 340 341 RS1 = Shl.getOperand(0); 342 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 343 Shl.getOperand(1).getValueType()); 344 return true; 345 } 346 347 // Check that it is a SROIW (Shift Right Ones Immediate i32 on RV64). 348 // We first check that it is the right node tree: 349 // 350 // (OR (SRL RS1, VC2), VC1) 351 // 352 // and then we check that VC1, the mask used to fill with ones, is compatible 353 // with VC2, the shamt: 354 // 355 // VC2 < 32 356 // VC1 == maskTrailingZeros<uint64_t>(32 - VC2) 357 // 358 bool RISCVDAGToDAGISel::SelectSROIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 359 assert(Subtarget->is64Bit() && "SROIW should only be matched on RV64"); 360 if (N.getOpcode() != ISD::OR || !isa<ConstantSDNode>(N.getOperand(1))) 361 return false; 362 363 SDValue Srl = N.getOperand(0); 364 if (Srl.getOpcode() != ISD::SRL || !isa<ConstantSDNode>(Srl.getOperand(1))) 365 return false; 366 367 uint64_t VC1 = N.getConstantOperandVal(1); 368 uint64_t VC2 = Srl.getConstantOperandVal(1); 369 370 if (VC2 >= 32 || VC1 != maskTrailingZeros<uint64_t>(32 - VC2)) 371 return false; 372 373 RS1 = Srl.getOperand(0); 374 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 375 Srl.getOperand(1).getValueType()); 376 return true; 377 } 378 379 // Merge an ADDI into the offset of a load/store instruction where possible. 380 // (load (addi base, off1), off2) -> (load base, off1+off2) 381 // (store val, (addi base, off1), off2) -> (store val, base, off1+off2) 382 // This is possible when off1+off2 fits a 12-bit immediate. 383 void RISCVDAGToDAGISel::doPeepholeLoadStoreADDI() { 384 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); 385 ++Position; 386 387 while (Position != CurDAG->allnodes_begin()) { 388 SDNode *N = &*--Position; 389 // Skip dead nodes and any non-machine opcodes. 390 if (N->use_empty() || !N->isMachineOpcode()) 391 continue; 392 393 int OffsetOpIdx; 394 int BaseOpIdx; 395 396 // Only attempt this optimisation for I-type loads and S-type stores. 397 switch (N->getMachineOpcode()) { 398 default: 399 continue; 400 case RISCV::LB: 401 case RISCV::LH: 402 case RISCV::LW: 403 case RISCV::LBU: 404 case RISCV::LHU: 405 case RISCV::LWU: 406 case RISCV::LD: 407 case RISCV::FLW: 408 case RISCV::FLD: 409 BaseOpIdx = 0; 410 OffsetOpIdx = 1; 411 break; 412 case RISCV::SB: 413 case RISCV::SH: 414 case RISCV::SW: 415 case RISCV::SD: 416 case RISCV::FSW: 417 case RISCV::FSD: 418 BaseOpIdx = 1; 419 OffsetOpIdx = 2; 420 break; 421 } 422 423 if (!isa<ConstantSDNode>(N->getOperand(OffsetOpIdx))) 424 continue; 425 426 SDValue Base = N->getOperand(BaseOpIdx); 427 428 // If the base is an ADDI, we can merge it in to the load/store. 429 if (!Base.isMachineOpcode() || Base.getMachineOpcode() != RISCV::ADDI) 430 continue; 431 432 SDValue ImmOperand = Base.getOperand(1); 433 uint64_t Offset2 = N->getConstantOperandVal(OffsetOpIdx); 434 435 if (auto Const = dyn_cast<ConstantSDNode>(ImmOperand)) { 436 int64_t Offset1 = Const->getSExtValue(); 437 int64_t CombinedOffset = Offset1 + Offset2; 438 if (!isInt<12>(CombinedOffset)) 439 continue; 440 ImmOperand = CurDAG->getTargetConstant(CombinedOffset, SDLoc(ImmOperand), 441 ImmOperand.getValueType()); 442 } else if (auto GA = dyn_cast<GlobalAddressSDNode>(ImmOperand)) { 443 // If the off1 in (addi base, off1) is a global variable's address (its 444 // low part, really), then we can rely on the alignment of that variable 445 // to provide a margin of safety before off1 can overflow the 12 bits. 446 // Check if off2 falls within that margin; if so off1+off2 can't overflow. 447 const DataLayout &DL = CurDAG->getDataLayout(); 448 Align Alignment = GA->getGlobal()->getPointerAlignment(DL); 449 if (Offset2 != 0 && Alignment <= Offset2) 450 continue; 451 int64_t Offset1 = GA->getOffset(); 452 int64_t CombinedOffset = Offset1 + Offset2; 453 ImmOperand = CurDAG->getTargetGlobalAddress( 454 GA->getGlobal(), SDLoc(ImmOperand), ImmOperand.getValueType(), 455 CombinedOffset, GA->getTargetFlags()); 456 } else if (auto CP = dyn_cast<ConstantPoolSDNode>(ImmOperand)) { 457 // Ditto. 458 Align Alignment = CP->getAlign(); 459 if (Offset2 != 0 && Alignment <= Offset2) 460 continue; 461 int64_t Offset1 = CP->getOffset(); 462 int64_t CombinedOffset = Offset1 + Offset2; 463 ImmOperand = CurDAG->getTargetConstantPool( 464 CP->getConstVal(), ImmOperand.getValueType(), CP->getAlign(), 465 CombinedOffset, CP->getTargetFlags()); 466 } else { 467 continue; 468 } 469 470 LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); 471 LLVM_DEBUG(Base->dump(CurDAG)); 472 LLVM_DEBUG(dbgs() << "\nN: "); 473 LLVM_DEBUG(N->dump(CurDAG)); 474 LLVM_DEBUG(dbgs() << "\n"); 475 476 // Modify the offset operand of the load/store. 477 if (BaseOpIdx == 0) // Load 478 CurDAG->UpdateNodeOperands(N, Base.getOperand(0), ImmOperand, 479 N->getOperand(2)); 480 else // Store 481 CurDAG->UpdateNodeOperands(N, N->getOperand(0), Base.getOperand(0), 482 ImmOperand, N->getOperand(3)); 483 484 // The add-immediate may now be dead, in which case remove it. 485 if (Base.getNode()->use_empty()) 486 CurDAG->RemoveDeadNode(Base.getNode()); 487 } 488 } 489 490 // This pass converts a legalized DAG into a RISCV-specific DAG, ready 491 // for instruction scheduling. 492 FunctionPass *llvm::createRISCVISelDag(RISCVTargetMachine &TM) { 493 return new RISCVDAGToDAGISel(TM); 494 } 495