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 // Match (srl (shl val, 32), imm). 145 if (Op0->getOpcode() == ISD::SHL && 146 isa<ConstantSDNode>(Op0->getOperand(1)) && 147 isa<ConstantSDNode>(Node->getOperand(1))) { 148 uint64_t ShlAmt = Op0->getConstantOperandVal(1); 149 uint64_t SrlAmt = Node->getConstantOperandVal(1); 150 if (ShlAmt == 32 && SrlAmt > 32) { 151 SDValue SrlAmtSub32Val = 152 CurDAG->getTargetConstant(SrlAmt - 32, SDLoc(Node), XLenVT); 153 CurDAG->SelectNodeTo(Node, RISCV::SRLIW, XLenVT, Op0->getOperand(0), 154 SrlAmtSub32Val); 155 return; 156 } 157 } 158 break; 159 } 160 case RISCVISD::READ_CYCLE_WIDE: 161 assert(!Subtarget->is64Bit() && "READ_CYCLE_WIDE is only used on riscv32"); 162 163 ReplaceNode(Node, CurDAG->getMachineNode(RISCV::ReadCycleWide, DL, MVT::i32, 164 MVT::i32, MVT::Other, 165 Node->getOperand(0))); 166 return; 167 } 168 169 // Select the default instruction. 170 SelectCode(Node); 171 } 172 173 bool RISCVDAGToDAGISel::SelectInlineAsmMemoryOperand( 174 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) { 175 switch (ConstraintID) { 176 case InlineAsm::Constraint_m: 177 // We just support simple memory operands that have a single address 178 // operand and need no special handling. 179 OutOps.push_back(Op); 180 return false; 181 case InlineAsm::Constraint_A: 182 OutOps.push_back(Op); 183 return false; 184 default: 185 break; 186 } 187 188 return true; 189 } 190 191 bool RISCVDAGToDAGISel::SelectAddrFI(SDValue Addr, SDValue &Base) { 192 if (auto FIN = dyn_cast<FrameIndexSDNode>(Addr)) { 193 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT()); 194 return true; 195 } 196 return false; 197 } 198 199 // Check that it is a SLOI (Shift Left Ones Immediate). We first check that 200 // it is the right node tree: 201 // 202 // (OR (SHL RS1, VC2), VC1) 203 // 204 // and then we check that VC1, the mask used to fill with ones, is compatible 205 // with VC2, the shamt: 206 // 207 // VC1 == maskTrailingOnes<uint64_t>(VC2) 208 209 bool RISCVDAGToDAGISel::SelectSLOI(SDValue N, SDValue &RS1, SDValue &Shamt) { 210 MVT XLenVT = Subtarget->getXLenVT(); 211 if (N.getOpcode() == ISD::OR) { 212 SDValue Or = N; 213 if (Or.getOperand(0).getOpcode() == ISD::SHL) { 214 SDValue Shl = Or.getOperand(0); 215 if (isa<ConstantSDNode>(Shl.getOperand(1)) && 216 isa<ConstantSDNode>(Or.getOperand(1))) { 217 if (XLenVT == MVT::i64) { 218 uint64_t VC1 = Or.getConstantOperandVal(1); 219 uint64_t VC2 = Shl.getConstantOperandVal(1); 220 if (VC1 == maskTrailingOnes<uint64_t>(VC2)) { 221 RS1 = Shl.getOperand(0); 222 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 223 Shl.getOperand(1).getValueType()); 224 return true; 225 } 226 } 227 if (XLenVT == MVT::i32) { 228 uint32_t VC1 = Or.getConstantOperandVal(1); 229 uint32_t VC2 = Shl.getConstantOperandVal(1); 230 if (VC1 == maskTrailingOnes<uint32_t>(VC2)) { 231 RS1 = Shl.getOperand(0); 232 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 233 Shl.getOperand(1).getValueType()); 234 return true; 235 } 236 } 237 } 238 } 239 } 240 return false; 241 } 242 243 // Check that it is a SROI (Shift Right Ones Immediate). We first check that 244 // it is the right node tree: 245 // 246 // (OR (SRL RS1, VC2), VC1) 247 // 248 // and then we check that VC1, the mask used to fill with ones, is compatible 249 // with VC2, the shamt: 250 // 251 // VC1 == maskLeadingOnes<uint64_t>(VC2) 252 253 bool RISCVDAGToDAGISel::SelectSROI(SDValue N, SDValue &RS1, SDValue &Shamt) { 254 MVT XLenVT = Subtarget->getXLenVT(); 255 if (N.getOpcode() == ISD::OR) { 256 SDValue Or = N; 257 if (Or.getOperand(0).getOpcode() == ISD::SRL) { 258 SDValue Srl = Or.getOperand(0); 259 if (isa<ConstantSDNode>(Srl.getOperand(1)) && 260 isa<ConstantSDNode>(Or.getOperand(1))) { 261 if (XLenVT == MVT::i64) { 262 uint64_t VC1 = Or.getConstantOperandVal(1); 263 uint64_t VC2 = Srl.getConstantOperandVal(1); 264 if (VC1 == maskLeadingOnes<uint64_t>(VC2)) { 265 RS1 = Srl.getOperand(0); 266 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 267 Srl.getOperand(1).getValueType()); 268 return true; 269 } 270 } 271 if (XLenVT == MVT::i32) { 272 uint32_t VC1 = Or.getConstantOperandVal(1); 273 uint32_t VC2 = Srl.getConstantOperandVal(1); 274 if (VC1 == maskLeadingOnes<uint32_t>(VC2)) { 275 RS1 = Srl.getOperand(0); 276 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 277 Srl.getOperand(1).getValueType()); 278 return true; 279 } 280 } 281 } 282 } 283 } 284 return false; 285 } 286 287 // Check that it is a RORI (Rotate Right Immediate). We first check that 288 // it is the right node tree: 289 // 290 // (ROTL RS1, VC) 291 // 292 // The compiler translates immediate rotations to the right given by the call 293 // to the rotateright32/rotateright64 intrinsics as rotations to the left. 294 // Since the rotation to the left can be easily emulated as a rotation to the 295 // right by negating the constant, there is no encoding for ROLI. 296 // We then select the immediate left rotations as RORI by the complementary 297 // constant: 298 // 299 // Shamt == XLen - VC 300 301 bool RISCVDAGToDAGISel::SelectRORI(SDValue N, SDValue &RS1, SDValue &Shamt) { 302 MVT XLenVT = Subtarget->getXLenVT(); 303 if (N.getOpcode() == ISD::ROTL) { 304 if (isa<ConstantSDNode>(N.getOperand(1))) { 305 if (XLenVT == MVT::i64) { 306 uint64_t VC = N.getConstantOperandVal(1); 307 Shamt = CurDAG->getTargetConstant((64 - VC), SDLoc(N), 308 N.getOperand(1).getValueType()); 309 RS1 = N.getOperand(0); 310 return true; 311 } 312 if (XLenVT == MVT::i32) { 313 uint32_t VC = N.getConstantOperandVal(1); 314 Shamt = CurDAG->getTargetConstant((32 - VC), SDLoc(N), 315 N.getOperand(1).getValueType()); 316 RS1 = N.getOperand(0); 317 return true; 318 } 319 } 320 } 321 return false; 322 } 323 324 325 // Check that it is a SLLIUW (Shift Logical Left Immediate Unsigned i32 326 // on RV64). 327 // SLLIUW is the same as SLLI except for the fact that it clears the bits 328 // XLEN-1:32 of the input RS1 before shifting. 329 // We first check that it is the right node tree: 330 // 331 // (AND (SHL RS1, VC2), VC1) 332 // 333 // We check that VC2, the shamt is less than 32, otherwise the pattern is 334 // exactly the same as SLLI and we give priority to that. 335 // Eventually we check that that VC1, the mask used to clear the upper 32 bits 336 // of RS1, is correct: 337 // 338 // VC1 == (0xFFFFFFFF << VC2) 339 340 bool RISCVDAGToDAGISel::SelectSLLIUW(SDValue N, SDValue &RS1, SDValue &Shamt) { 341 if (N.getOpcode() == ISD::AND && Subtarget->getXLenVT() == MVT::i64) { 342 SDValue And = N; 343 if (And.getOperand(0).getOpcode() == ISD::SHL) { 344 SDValue Shl = And.getOperand(0); 345 if (isa<ConstantSDNode>(Shl.getOperand(1)) && 346 isa<ConstantSDNode>(And.getOperand(1))) { 347 uint64_t VC1 = And.getConstantOperandVal(1); 348 uint64_t VC2 = Shl.getConstantOperandVal(1); 349 if (VC2 < 32 && VC1 == ((uint64_t)0xFFFFFFFF << VC2)) { 350 RS1 = Shl.getOperand(0); 351 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 352 Shl.getOperand(1).getValueType()); 353 return true; 354 } 355 } 356 } 357 } 358 return false; 359 } 360 361 // Check that it is a SLOIW (Shift Left Ones Immediate i32 on RV64). 362 // We first check that it is the right node tree: 363 // 364 // (SIGN_EXTEND_INREG (OR (SHL RS1, VC2), VC1)) 365 // 366 // and then we check that VC1, the mask used to fill with ones, is compatible 367 // with VC2, the shamt: 368 // 369 // VC1 == maskTrailingOnes<uint32_t>(VC2) 370 371 bool RISCVDAGToDAGISel::SelectSLOIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 372 if (Subtarget->getXLenVT() == MVT::i64 && 373 N.getOpcode() == ISD::SIGN_EXTEND_INREG && 374 cast<VTSDNode>(N.getOperand(1))->getVT() == MVT::i32) { 375 if (N.getOperand(0).getOpcode() == ISD::OR) { 376 SDValue Or = N.getOperand(0); 377 if (Or.getOperand(0).getOpcode() == ISD::SHL) { 378 SDValue Shl = Or.getOperand(0); 379 if (isa<ConstantSDNode>(Shl.getOperand(1)) && 380 isa<ConstantSDNode>(Or.getOperand(1))) { 381 uint32_t VC1 = Or.getConstantOperandVal(1); 382 uint32_t VC2 = Shl.getConstantOperandVal(1); 383 if (VC1 == maskTrailingOnes<uint32_t>(VC2)) { 384 RS1 = Shl.getOperand(0); 385 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 386 Shl.getOperand(1).getValueType()); 387 return true; 388 } 389 } 390 } 391 } 392 } 393 return false; 394 } 395 396 // Check that it is a SROIW (Shift Right Ones Immediate i32 on RV64). 397 // We first check that it is the right node tree: 398 // 399 // (OR (SHL RS1, VC2), VC1) 400 // 401 // and then we check that VC1, the mask used to fill with ones, is compatible 402 // with VC2, the shamt: 403 // 404 // VC1 == maskLeadingOnes<uint32_t>(VC2) 405 406 bool RISCVDAGToDAGISel::SelectSROIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 407 if (N.getOpcode() == ISD::OR && Subtarget->getXLenVT() == MVT::i64) { 408 SDValue Or = N; 409 if (Or.getOperand(0).getOpcode() == ISD::SRL) { 410 SDValue Srl = Or.getOperand(0); 411 if (isa<ConstantSDNode>(Srl.getOperand(1)) && 412 isa<ConstantSDNode>(Or.getOperand(1))) { 413 uint32_t VC1 = Or.getConstantOperandVal(1); 414 uint32_t VC2 = Srl.getConstantOperandVal(1); 415 if (VC1 == maskLeadingOnes<uint32_t>(VC2)) { 416 RS1 = Srl.getOperand(0); 417 Shamt = CurDAG->getTargetConstant(VC2, SDLoc(N), 418 Srl.getOperand(1).getValueType()); 419 return true; 420 } 421 } 422 } 423 } 424 return false; 425 } 426 427 // Check that it is a RORIW (i32 Right Rotate Immediate on RV64). 428 // We first check that it is the right node tree: 429 // 430 // (SIGN_EXTEND_INREG (OR (SHL (AsserSext RS1, i32), VC2), 431 // (SRL (AND (AssertSext RS2, i32), VC3), VC1))) 432 // 433 // Then we check that the constant operands respect these constraints: 434 // 435 // VC2 == 32 - VC1 436 // VC3 == maskLeadingOnes<uint32_t>(VC2) 437 // 438 // being VC1 the Shamt we need, VC2 the complementary of Shamt over 32 439 // and VC3 a 32 bit mask of (32 - VC1) leading ones. 440 441 bool RISCVDAGToDAGISel::SelectRORIW(SDValue N, SDValue &RS1, SDValue &Shamt) { 442 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG && 443 Subtarget->getXLenVT() == MVT::i64 && 444 cast<VTSDNode>(N.getOperand(1))->getVT() == MVT::i32) { 445 if (N.getOperand(0).getOpcode() == ISD::OR) { 446 SDValue Or = N.getOperand(0); 447 if (Or.getOperand(0).getOpcode() == ISD::SHL && 448 Or.getOperand(1).getOpcode() == ISD::SRL) { 449 SDValue Shl = Or.getOperand(0); 450 SDValue Srl = Or.getOperand(1); 451 if (Srl.getOperand(0).getOpcode() == ISD::AND) { 452 SDValue And = Srl.getOperand(0); 453 if (isa<ConstantSDNode>(Srl.getOperand(1)) && 454 isa<ConstantSDNode>(Shl.getOperand(1)) && 455 isa<ConstantSDNode>(And.getOperand(1))) { 456 uint32_t VC1 = Srl.getConstantOperandVal(1); 457 uint32_t VC2 = Shl.getConstantOperandVal(1); 458 uint32_t VC3 = And.getConstantOperandVal(1); 459 if (VC2 == (32 - VC1) && 460 VC3 == maskLeadingOnes<uint32_t>(VC2)) { 461 RS1 = Shl.getOperand(0); 462 Shamt = CurDAG->getTargetConstant(VC1, SDLoc(N), 463 Srl.getOperand(1).getValueType()); 464 return true; 465 } 466 } 467 } 468 } 469 } 470 } 471 return false; 472 } 473 474 // Merge an ADDI into the offset of a load/store instruction where possible. 475 // (load (addi base, off1), off2) -> (load base, off1+off2) 476 // (store val, (addi base, off1), off2) -> (store val, base, off1+off2) 477 // This is possible when off1+off2 fits a 12-bit immediate. 478 void RISCVDAGToDAGISel::doPeepholeLoadStoreADDI() { 479 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); 480 ++Position; 481 482 while (Position != CurDAG->allnodes_begin()) { 483 SDNode *N = &*--Position; 484 // Skip dead nodes and any non-machine opcodes. 485 if (N->use_empty() || !N->isMachineOpcode()) 486 continue; 487 488 int OffsetOpIdx; 489 int BaseOpIdx; 490 491 // Only attempt this optimisation for I-type loads and S-type stores. 492 switch (N->getMachineOpcode()) { 493 default: 494 continue; 495 case RISCV::LB: 496 case RISCV::LH: 497 case RISCV::LW: 498 case RISCV::LBU: 499 case RISCV::LHU: 500 case RISCV::LWU: 501 case RISCV::LD: 502 case RISCV::FLW: 503 case RISCV::FLD: 504 BaseOpIdx = 0; 505 OffsetOpIdx = 1; 506 break; 507 case RISCV::SB: 508 case RISCV::SH: 509 case RISCV::SW: 510 case RISCV::SD: 511 case RISCV::FSW: 512 case RISCV::FSD: 513 BaseOpIdx = 1; 514 OffsetOpIdx = 2; 515 break; 516 } 517 518 if (!isa<ConstantSDNode>(N->getOperand(OffsetOpIdx))) 519 continue; 520 521 SDValue Base = N->getOperand(BaseOpIdx); 522 523 // If the base is an ADDI, we can merge it in to the load/store. 524 if (!Base.isMachineOpcode() || Base.getMachineOpcode() != RISCV::ADDI) 525 continue; 526 527 SDValue ImmOperand = Base.getOperand(1); 528 uint64_t Offset2 = N->getConstantOperandVal(OffsetOpIdx); 529 530 if (auto Const = dyn_cast<ConstantSDNode>(ImmOperand)) { 531 int64_t Offset1 = Const->getSExtValue(); 532 int64_t CombinedOffset = Offset1 + Offset2; 533 if (!isInt<12>(CombinedOffset)) 534 continue; 535 ImmOperand = CurDAG->getTargetConstant(CombinedOffset, SDLoc(ImmOperand), 536 ImmOperand.getValueType()); 537 } else if (auto GA = dyn_cast<GlobalAddressSDNode>(ImmOperand)) { 538 // If the off1 in (addi base, off1) is a global variable's address (its 539 // low part, really), then we can rely on the alignment of that variable 540 // to provide a margin of safety before off1 can overflow the 12 bits. 541 // Check if off2 falls within that margin; if so off1+off2 can't overflow. 542 const DataLayout &DL = CurDAG->getDataLayout(); 543 Align Alignment = GA->getGlobal()->getPointerAlignment(DL); 544 if (Offset2 != 0 && Alignment <= Offset2) 545 continue; 546 int64_t Offset1 = GA->getOffset(); 547 int64_t CombinedOffset = Offset1 + Offset2; 548 ImmOperand = CurDAG->getTargetGlobalAddress( 549 GA->getGlobal(), SDLoc(ImmOperand), ImmOperand.getValueType(), 550 CombinedOffset, GA->getTargetFlags()); 551 } else if (auto CP = dyn_cast<ConstantPoolSDNode>(ImmOperand)) { 552 // Ditto. 553 Align Alignment = CP->getAlign(); 554 if (Offset2 != 0 && Alignment <= Offset2) 555 continue; 556 int64_t Offset1 = CP->getOffset(); 557 int64_t CombinedOffset = Offset1 + Offset2; 558 ImmOperand = CurDAG->getTargetConstantPool( 559 CP->getConstVal(), ImmOperand.getValueType(), CP->getAlign(), 560 CombinedOffset, CP->getTargetFlags()); 561 } else { 562 continue; 563 } 564 565 LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); 566 LLVM_DEBUG(Base->dump(CurDAG)); 567 LLVM_DEBUG(dbgs() << "\nN: "); 568 LLVM_DEBUG(N->dump(CurDAG)); 569 LLVM_DEBUG(dbgs() << "\n"); 570 571 // Modify the offset operand of the load/store. 572 if (BaseOpIdx == 0) // Load 573 CurDAG->UpdateNodeOperands(N, Base.getOperand(0), ImmOperand, 574 N->getOperand(2)); 575 else // Store 576 CurDAG->UpdateNodeOperands(N, N->getOperand(0), Base.getOperand(0), 577 ImmOperand, N->getOperand(3)); 578 579 // The add-immediate may now be dead, in which case remove it. 580 if (Base.getNode()->use_empty()) 581 CurDAG->RemoveDeadNode(Base.getNode()); 582 } 583 } 584 585 // This pass converts a legalized DAG into a RISCV-specific DAG, ready 586 // for instruction scheduling. 587 FunctionPass *llvm::createRISCVISelDag(RISCVTargetMachine &TM) { 588 return new RISCVDAGToDAGISel(TM); 589 } 590