1 //===-- MipsSEISelDAGToDAG.cpp - A Dag to Dag Inst Selector for MipsSE ----===// 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 // Subclass of MipsDAGToDAGISel specialized for mips32/64. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "MipsSEISelDAGToDAG.h" 14 #include "MCTargetDesc/MipsBaseInfo.h" 15 #include "Mips.h" 16 #include "MipsAnalyzeImmediate.h" 17 #include "MipsMachineFunction.h" 18 #include "MipsRegisterInfo.h" 19 #include "llvm/CodeGen/MachineConstantPool.h" 20 #include "llvm/CodeGen/MachineFrameInfo.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineInstrBuilder.h" 23 #include "llvm/CodeGen/MachineRegisterInfo.h" 24 #include "llvm/CodeGen/SelectionDAGNodes.h" 25 #include "llvm/IR/CFG.h" 26 #include "llvm/IR/Dominators.h" 27 #include "llvm/IR/GlobalValue.h" 28 #include "llvm/IR/Instructions.h" 29 #include "llvm/IR/Intrinsics.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/raw_ostream.h" 34 #include "llvm/Target/TargetMachine.h" 35 using namespace llvm; 36 37 #define DEBUG_TYPE "mips-isel" 38 39 bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { 40 Subtarget = &static_cast<const MipsSubtarget &>(MF.getSubtarget()); 41 if (Subtarget->inMips16Mode()) 42 return false; 43 return MipsDAGToDAGISel::runOnMachineFunction(MF); 44 } 45 46 void MipsSEDAGToDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { 47 AU.addRequired<DominatorTreeWrapperPass>(); 48 SelectionDAGISel::getAnalysisUsage(AU); 49 } 50 51 void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef, MachineInstr &MI, 52 MachineFunction &MF) { 53 MachineInstrBuilder MIB(MF, &MI); 54 unsigned Mask = MI.getOperand(1).getImm(); 55 unsigned Flag = 56 IsDef ? RegState::ImplicitDefine : RegState::Implicit | RegState::Undef; 57 58 if (Mask & 1) 59 MIB.addReg(Mips::DSPPos, Flag); 60 61 if (Mask & 2) 62 MIB.addReg(Mips::DSPSCount, Flag); 63 64 if (Mask & 4) 65 MIB.addReg(Mips::DSPCarry, Flag); 66 67 if (Mask & 8) 68 MIB.addReg(Mips::DSPOutFlag, Flag); 69 70 if (Mask & 16) 71 MIB.addReg(Mips::DSPCCond, Flag); 72 73 if (Mask & 32) 74 MIB.addReg(Mips::DSPEFI, Flag); 75 } 76 77 unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const { 78 uint64_t RegNum = cast<ConstantSDNode>(RegIdx)->getZExtValue(); 79 return Mips::MSACtrlRegClass.getRegister(RegNum); 80 } 81 82 bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI, 83 const MachineInstr& MI) { 84 unsigned DstReg = 0, ZeroReg = 0; 85 86 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0". 87 if ((MI.getOpcode() == Mips::ADDiu) && 88 (MI.getOperand(1).getReg() == Mips::ZERO) && 89 (MI.getOperand(2).isImm()) && 90 (MI.getOperand(2).getImm() == 0)) { 91 DstReg = MI.getOperand(0).getReg(); 92 ZeroReg = Mips::ZERO; 93 } else if ((MI.getOpcode() == Mips::DADDiu) && 94 (MI.getOperand(1).getReg() == Mips::ZERO_64) && 95 (MI.getOperand(2).isImm()) && 96 (MI.getOperand(2).getImm() == 0)) { 97 DstReg = MI.getOperand(0).getReg(); 98 ZeroReg = Mips::ZERO_64; 99 } 100 101 if (!DstReg) 102 return false; 103 104 // Replace uses with ZeroReg. 105 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg), 106 E = MRI->use_end(); U != E;) { 107 MachineOperand &MO = *U; 108 unsigned OpNo = U.getOperandNo(); 109 MachineInstr *MI = MO.getParent(); 110 ++U; 111 112 // Do not replace if it is a phi's operand or is tied to def operand. 113 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo()) 114 continue; 115 116 // Also, we have to check that the register class of the operand 117 // contains the zero register. 118 if (!MRI->getRegClass(MO.getReg())->contains(ZeroReg)) 119 continue; 120 121 MO.setReg(ZeroReg); 122 } 123 124 return true; 125 } 126 127 void MipsSEDAGToDAGISel::emitMCountABI(MachineInstr &MI, MachineBasicBlock &MBB, 128 MachineFunction &MF) { 129 MachineInstrBuilder MIB(MF, &MI); 130 if (!Subtarget->isABI_O32()) { // N32, N64 131 // Save current return address. 132 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::OR64)) 133 .addDef(Mips::AT_64) 134 .addUse(Mips::RA_64, RegState::Undef) 135 .addUse(Mips::ZERO_64); 136 // Stops instruction above from being removed later on. 137 MIB.addUse(Mips::AT_64, RegState::Implicit); 138 } else { // O32 139 // Save current return address. 140 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::OR)) 141 .addDef(Mips::AT) 142 .addUse(Mips::RA, RegState::Undef) 143 .addUse(Mips::ZERO); 144 // _mcount pops 2 words from stack. 145 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::ADDiu)) 146 .addDef(Mips::SP) 147 .addUse(Mips::SP) 148 .addImm(-8); 149 // Stops first instruction above from being removed later on. 150 MIB.addUse(Mips::AT, RegState::Implicit); 151 } 152 } 153 154 void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) { 155 MF.getInfo<MipsFunctionInfo>()->initGlobalBaseReg(); 156 157 MachineRegisterInfo *MRI = &MF.getRegInfo(); 158 159 for (auto &MBB: MF) { 160 for (auto &MI: MBB) { 161 switch (MI.getOpcode()) { 162 case Mips::RDDSP: 163 addDSPCtrlRegOperands(false, MI, MF); 164 break; 165 case Mips::WRDSP: 166 addDSPCtrlRegOperands(true, MI, MF); 167 break; 168 case Mips::BuildPairF64_64: 169 case Mips::ExtractElementF64_64: 170 if (!Subtarget->useOddSPReg()) { 171 MI.addOperand(MachineOperand::CreateReg(Mips::SP, false, true)); 172 break; 173 } 174 LLVM_FALLTHROUGH; 175 case Mips::BuildPairF64: 176 case Mips::ExtractElementF64: 177 if (Subtarget->isABI_FPXX() && !Subtarget->hasMTHC1()) 178 MI.addOperand(MachineOperand::CreateReg(Mips::SP, false, true)); 179 break; 180 case Mips::JAL: 181 case Mips::JAL_MM: 182 if (MI.getOperand(0).isGlobal() && 183 MI.getOperand(0).getGlobal()->getGlobalIdentifier() == "_mcount") 184 emitMCountABI(MI, MBB, MF); 185 break; 186 case Mips::JALRPseudo: 187 case Mips::JALR64Pseudo: 188 case Mips::JALR16_MM: 189 if (MI.getOperand(2).isMCSymbol() && 190 MI.getOperand(2).getMCSymbol()->getName() == "_mcount") 191 emitMCountABI(MI, MBB, MF); 192 break; 193 case Mips::JALR: 194 if (MI.getOperand(3).isMCSymbol() && 195 MI.getOperand(3).getMCSymbol()->getName() == "_mcount") 196 emitMCountABI(MI, MBB, MF); 197 break; 198 default: 199 replaceUsesWithZeroReg(MRI, MI); 200 } 201 } 202 } 203 } 204 205 void MipsSEDAGToDAGISel::selectAddE(SDNode *Node, const SDLoc &DL) const { 206 SDValue InFlag = Node->getOperand(2); 207 unsigned Opc = InFlag.getOpcode(); 208 SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1); 209 EVT VT = LHS.getValueType(); 210 211 // In the base case, we can rely on the carry bit from the addsc 212 // instruction. 213 if (Opc == ISD::ADDC) { 214 SDValue Ops[3] = {LHS, RHS, InFlag}; 215 CurDAG->SelectNodeTo(Node, Mips::ADDWC, VT, MVT::Glue, Ops); 216 return; 217 } 218 219 assert(Opc == ISD::ADDE && "ISD::ADDE not in a chain of ADDE nodes!"); 220 221 // The more complex case is when there is a chain of ISD::ADDE nodes like: 222 // (adde (adde (adde (addc a b) c) d) e). 223 // 224 // The addwc instruction does not write to the carry bit, instead it writes 225 // to bit 20 of the dsp control register. To match this series of nodes, each 226 // intermediate adde node must be expanded to write the carry bit before the 227 // addition. 228 229 // Start by reading the overflow field for addsc and moving the value to the 230 // carry field. The usage of 1 here with MipsISD::RDDSP / Mips::WRDSP 231 // corresponds to reading/writing the entire control register to/from a GPR. 232 233 SDValue CstOne = CurDAG->getTargetConstant(1, DL, MVT::i32); 234 235 SDValue OuFlag = CurDAG->getTargetConstant(20, DL, MVT::i32); 236 237 SDNode *DSPCtrlField = CurDAG->getMachineNode(Mips::RDDSP, DL, MVT::i32, 238 MVT::Glue, CstOne, InFlag); 239 240 SDNode *Carry = CurDAG->getMachineNode( 241 Mips::EXT, DL, MVT::i32, SDValue(DSPCtrlField, 0), OuFlag, CstOne); 242 243 SDValue Ops[4] = {SDValue(DSPCtrlField, 0), 244 CurDAG->getTargetConstant(6, DL, MVT::i32), CstOne, 245 SDValue(Carry, 0)}; 246 SDNode *DSPCFWithCarry = CurDAG->getMachineNode(Mips::INS, DL, MVT::i32, Ops); 247 248 // My reading of the MIPS DSP 3.01 specification isn't as clear as I 249 // would like about whether bit 20 always gets overwritten by addwc. 250 // Hence take an extremely conservative view and presume it's sticky. We 251 // therefore need to clear it. 252 253 SDValue Zero = CurDAG->getRegister(Mips::ZERO, MVT::i32); 254 255 SDValue InsOps[4] = {Zero, OuFlag, CstOne, SDValue(DSPCFWithCarry, 0)}; 256 SDNode *DSPCtrlFinal = 257 CurDAG->getMachineNode(Mips::INS, DL, MVT::i32, InsOps); 258 259 SDNode *WrDSP = CurDAG->getMachineNode(Mips::WRDSP, DL, MVT::Glue, 260 SDValue(DSPCtrlFinal, 0), CstOne); 261 262 SDValue Operands[3] = {LHS, RHS, SDValue(WrDSP, 0)}; 263 CurDAG->SelectNodeTo(Node, Mips::ADDWC, VT, MVT::Glue, Operands); 264 } 265 266 /// Match frameindex 267 bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base, 268 SDValue &Offset) const { 269 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { 270 EVT ValTy = Addr.getValueType(); 271 272 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); 273 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), ValTy); 274 return true; 275 } 276 return false; 277 } 278 279 /// Match frameindex+offset and frameindex|offset 280 bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset( 281 SDValue Addr, SDValue &Base, SDValue &Offset, unsigned OffsetBits, 282 unsigned ShiftAmount = 0) const { 283 if (CurDAG->isBaseWithConstantOffset(Addr)) { 284 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)); 285 if (isIntN(OffsetBits + ShiftAmount, CN->getSExtValue())) { 286 EVT ValTy = Addr.getValueType(); 287 288 // If the first operand is a FI, get the TargetFI Node 289 if (FrameIndexSDNode *FIN = 290 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) 291 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); 292 else { 293 Base = Addr.getOperand(0); 294 // If base is a FI, additional offset calculation is done in 295 // eliminateFrameIndex, otherwise we need to check the alignment 296 const Align Alignment(1ULL << ShiftAmount); 297 if (!isAligned(Alignment, CN->getZExtValue())) 298 return false; 299 } 300 301 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr), 302 ValTy); 303 return true; 304 } 305 } 306 return false; 307 } 308 309 /// ComplexPattern used on MipsInstrInfo 310 /// Used on Mips Load/Store instructions 311 bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base, 312 SDValue &Offset) const { 313 // if Address is FI, get the TargetFrameIndex. 314 if (selectAddrFrameIndex(Addr, Base, Offset)) 315 return true; 316 317 // on PIC code Load GA 318 if (Addr.getOpcode() == MipsISD::Wrapper) { 319 Base = Addr.getOperand(0); 320 Offset = Addr.getOperand(1); 321 return true; 322 } 323 324 if (!TM.isPositionIndependent()) { 325 if ((Addr.getOpcode() == ISD::TargetExternalSymbol || 326 Addr.getOpcode() == ISD::TargetGlobalAddress)) 327 return false; 328 } 329 330 // Addresses of the form FI+const or FI|const 331 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16)) 332 return true; 333 334 // Operand is a result from an ADD. 335 if (Addr.getOpcode() == ISD::ADD) { 336 // When loading from constant pools, load the lower address part in 337 // the instruction itself. Example, instead of: 338 // lui $2, %hi($CPI1_0) 339 // addiu $2, $2, %lo($CPI1_0) 340 // lwc1 $f0, 0($2) 341 // Generate: 342 // lui $2, %hi($CPI1_0) 343 // lwc1 $f0, %lo($CPI1_0)($2) 344 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo || 345 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) { 346 SDValue Opnd0 = Addr.getOperand(1).getOperand(0); 347 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) || 348 isa<JumpTableSDNode>(Opnd0)) { 349 Base = Addr.getOperand(0); 350 Offset = Opnd0; 351 return true; 352 } 353 } 354 } 355 356 return false; 357 } 358 359 /// ComplexPattern used on MipsInstrInfo 360 /// Used on Mips Load/Store instructions 361 bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base, 362 SDValue &Offset) const { 363 Base = Addr; 364 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType()); 365 return true; 366 } 367 368 bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base, 369 SDValue &Offset) const { 370 return selectAddrRegImm(Addr, Base, Offset) || 371 selectAddrDefault(Addr, Base, Offset); 372 } 373 374 bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base, 375 SDValue &Offset) const { 376 if (selectAddrFrameIndex(Addr, Base, Offset)) 377 return true; 378 379 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9)) 380 return true; 381 382 return false; 383 } 384 385 /// Used on microMIPS LWC2, LDC2, SWC2 and SDC2 instructions (11-bit offset) 386 bool MipsSEDAGToDAGISel::selectAddrRegImm11(SDValue Addr, SDValue &Base, 387 SDValue &Offset) const { 388 if (selectAddrFrameIndex(Addr, Base, Offset)) 389 return true; 390 391 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 11)) 392 return true; 393 394 return false; 395 } 396 397 /// Used on microMIPS Load/Store unaligned instructions (12-bit offset) 398 bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base, 399 SDValue &Offset) const { 400 if (selectAddrFrameIndex(Addr, Base, Offset)) 401 return true; 402 403 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12)) 404 return true; 405 406 return false; 407 } 408 409 bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base, 410 SDValue &Offset) const { 411 if (selectAddrFrameIndex(Addr, Base, Offset)) 412 return true; 413 414 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16)) 415 return true; 416 417 return false; 418 } 419 420 bool MipsSEDAGToDAGISel::selectIntAddr11MM(SDValue Addr, SDValue &Base, 421 SDValue &Offset) const { 422 return selectAddrRegImm11(Addr, Base, Offset) || 423 selectAddrDefault(Addr, Base, Offset); 424 } 425 426 bool MipsSEDAGToDAGISel::selectIntAddr12MM(SDValue Addr, SDValue &Base, 427 SDValue &Offset) const { 428 return selectAddrRegImm12(Addr, Base, Offset) || 429 selectAddrDefault(Addr, Base, Offset); 430 } 431 432 bool MipsSEDAGToDAGISel::selectIntAddr16MM(SDValue Addr, SDValue &Base, 433 SDValue &Offset) const { 434 return selectAddrRegImm16(Addr, Base, Offset) || 435 selectAddrDefault(Addr, Base, Offset); 436 } 437 438 bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base, 439 SDValue &Offset) const { 440 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) { 441 if (isa<FrameIndexSDNode>(Base)) 442 return false; 443 444 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) { 445 unsigned CnstOff = CN->getZExtValue(); 446 return (CnstOff == (CnstOff & 0x3c)); 447 } 448 449 return false; 450 } 451 452 // For all other cases where "lw" would be selected, don't select "lw16" 453 // because it would result in additional instructions to prepare operands. 454 if (selectAddrRegImm(Addr, Base, Offset)) 455 return false; 456 457 return selectAddrDefault(Addr, Base, Offset); 458 } 459 460 bool MipsSEDAGToDAGISel::selectIntAddrSImm10(SDValue Addr, SDValue &Base, 461 SDValue &Offset) const { 462 463 if (selectAddrFrameIndex(Addr, Base, Offset)) 464 return true; 465 466 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10)) 467 return true; 468 469 return selectAddrDefault(Addr, Base, Offset); 470 } 471 472 bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl1(SDValue Addr, SDValue &Base, 473 SDValue &Offset) const { 474 if (selectAddrFrameIndex(Addr, Base, Offset)) 475 return true; 476 477 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 1)) 478 return true; 479 480 return selectAddrDefault(Addr, Base, Offset); 481 } 482 483 bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl2(SDValue Addr, SDValue &Base, 484 SDValue &Offset) const { 485 if (selectAddrFrameIndex(Addr, Base, Offset)) 486 return true; 487 488 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 2)) 489 return true; 490 491 return selectAddrDefault(Addr, Base, Offset); 492 } 493 494 bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl3(SDValue Addr, SDValue &Base, 495 SDValue &Offset) const { 496 if (selectAddrFrameIndex(Addr, Base, Offset)) 497 return true; 498 499 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 3)) 500 return true; 501 502 return selectAddrDefault(Addr, Base, Offset); 503 } 504 505 // Select constant vector splats. 506 // 507 // Returns true and sets Imm if: 508 // * MSA is enabled 509 // * N is a ISD::BUILD_VECTOR representing a constant splat 510 bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm, 511 unsigned MinSizeInBits) const { 512 if (!Subtarget->hasMSA()) 513 return false; 514 515 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N); 516 517 if (!Node) 518 return false; 519 520 APInt SplatValue, SplatUndef; 521 unsigned SplatBitSize; 522 bool HasAnyUndefs; 523 524 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs, 525 MinSizeInBits, !Subtarget->isLittle())) 526 return false; 527 528 Imm = SplatValue; 529 530 return true; 531 } 532 533 // Select constant vector splats. 534 // 535 // In addition to the requirements of selectVSplat(), this function returns 536 // true and sets Imm if: 537 // * The splat value is the same width as the elements of the vector 538 // * The splat value fits in an integer with the specified signed-ness and 539 // width. 540 // 541 // This function looks through ISD::BITCAST nodes. 542 // TODO: This might not be appropriate for big-endian MSA since BITCAST is 543 // sometimes a shuffle in big-endian mode. 544 // 545 // It's worth noting that this function is not used as part of the selection 546 // of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd] 547 // instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in 548 // MipsSEDAGToDAGISel::selectNode. 549 bool MipsSEDAGToDAGISel:: 550 selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed, 551 unsigned ImmBitSize) const { 552 APInt ImmValue; 553 EVT EltTy = N->getValueType(0).getVectorElementType(); 554 555 if (N->getOpcode() == ISD::BITCAST) 556 N = N->getOperand(0); 557 558 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && 559 ImmValue.getBitWidth() == EltTy.getSizeInBits()) { 560 561 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) || 562 (!Signed && ImmValue.isIntN(ImmBitSize))) { 563 Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy); 564 return true; 565 } 566 } 567 568 return false; 569 } 570 571 // Select constant vector splats. 572 bool MipsSEDAGToDAGISel:: 573 selectVSplatUimm1(SDValue N, SDValue &Imm) const { 574 return selectVSplatCommon(N, Imm, false, 1); 575 } 576 577 bool MipsSEDAGToDAGISel:: 578 selectVSplatUimm2(SDValue N, SDValue &Imm) const { 579 return selectVSplatCommon(N, Imm, false, 2); 580 } 581 582 bool MipsSEDAGToDAGISel:: 583 selectVSplatUimm3(SDValue N, SDValue &Imm) const { 584 return selectVSplatCommon(N, Imm, false, 3); 585 } 586 587 // Select constant vector splats. 588 bool MipsSEDAGToDAGISel:: 589 selectVSplatUimm4(SDValue N, SDValue &Imm) const { 590 return selectVSplatCommon(N, Imm, false, 4); 591 } 592 593 // Select constant vector splats. 594 bool MipsSEDAGToDAGISel:: 595 selectVSplatUimm5(SDValue N, SDValue &Imm) const { 596 return selectVSplatCommon(N, Imm, false, 5); 597 } 598 599 // Select constant vector splats. 600 bool MipsSEDAGToDAGISel:: 601 selectVSplatUimm6(SDValue N, SDValue &Imm) const { 602 return selectVSplatCommon(N, Imm, false, 6); 603 } 604 605 // Select constant vector splats. 606 bool MipsSEDAGToDAGISel:: 607 selectVSplatUimm8(SDValue N, SDValue &Imm) const { 608 return selectVSplatCommon(N, Imm, false, 8); 609 } 610 611 // Select constant vector splats. 612 bool MipsSEDAGToDAGISel:: 613 selectVSplatSimm5(SDValue N, SDValue &Imm) const { 614 return selectVSplatCommon(N, Imm, true, 5); 615 } 616 617 // Select constant vector splats whose value is a power of 2. 618 // 619 // In addition to the requirements of selectVSplat(), this function returns 620 // true and sets Imm if: 621 // * The splat value is the same width as the elements of the vector 622 // * The splat value is a power of two. 623 // 624 // This function looks through ISD::BITCAST nodes. 625 // TODO: This might not be appropriate for big-endian MSA since BITCAST is 626 // sometimes a shuffle in big-endian mode. 627 bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const { 628 APInt ImmValue; 629 EVT EltTy = N->getValueType(0).getVectorElementType(); 630 631 if (N->getOpcode() == ISD::BITCAST) 632 N = N->getOperand(0); 633 634 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && 635 ImmValue.getBitWidth() == EltTy.getSizeInBits()) { 636 int32_t Log2 = ImmValue.exactLogBase2(); 637 638 if (Log2 != -1) { 639 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy); 640 return true; 641 } 642 } 643 644 return false; 645 } 646 647 // Select constant vector splats whose value only has a consecutive sequence 648 // of left-most bits set (e.g. 0b11...1100...00). 649 // 650 // In addition to the requirements of selectVSplat(), this function returns 651 // true and sets Imm if: 652 // * The splat value is the same width as the elements of the vector 653 // * The splat value is a consecutive sequence of left-most bits. 654 // 655 // This function looks through ISD::BITCAST nodes. 656 // TODO: This might not be appropriate for big-endian MSA since BITCAST is 657 // sometimes a shuffle in big-endian mode. 658 bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const { 659 APInt ImmValue; 660 EVT EltTy = N->getValueType(0).getVectorElementType(); 661 662 if (N->getOpcode() == ISD::BITCAST) 663 N = N->getOperand(0); 664 665 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && 666 ImmValue.getBitWidth() == EltTy.getSizeInBits()) { 667 // Extract the run of set bits starting with bit zero from the bitwise 668 // inverse of ImmValue, and test that the inverse of this is the same 669 // as the original value. 670 if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) { 671 672 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation() - 1, SDLoc(N), 673 EltTy); 674 return true; 675 } 676 } 677 678 return false; 679 } 680 681 // Select constant vector splats whose value only has a consecutive sequence 682 // of right-most bits set (e.g. 0b00...0011...11). 683 // 684 // In addition to the requirements of selectVSplat(), this function returns 685 // true and sets Imm if: 686 // * The splat value is the same width as the elements of the vector 687 // * The splat value is a consecutive sequence of right-most bits. 688 // 689 // This function looks through ISD::BITCAST nodes. 690 // TODO: This might not be appropriate for big-endian MSA since BITCAST is 691 // sometimes a shuffle in big-endian mode. 692 bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const { 693 APInt ImmValue; 694 EVT EltTy = N->getValueType(0).getVectorElementType(); 695 696 if (N->getOpcode() == ISD::BITCAST) 697 N = N->getOperand(0); 698 699 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && 700 ImmValue.getBitWidth() == EltTy.getSizeInBits()) { 701 // Extract the run of set bits starting with bit zero, and test that the 702 // result is the same as the original value 703 if (ImmValue == (ImmValue & ~(ImmValue + 1))) { 704 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation() - 1, SDLoc(N), 705 EltTy); 706 return true; 707 } 708 } 709 710 return false; 711 } 712 713 bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N, 714 SDValue &Imm) const { 715 APInt ImmValue; 716 EVT EltTy = N->getValueType(0).getVectorElementType(); 717 718 if (N->getOpcode() == ISD::BITCAST) 719 N = N->getOperand(0); 720 721 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && 722 ImmValue.getBitWidth() == EltTy.getSizeInBits()) { 723 int32_t Log2 = (~ImmValue).exactLogBase2(); 724 725 if (Log2 != -1) { 726 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy); 727 return true; 728 } 729 } 730 731 return false; 732 } 733 734 bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) { 735 unsigned Opcode = Node->getOpcode(); 736 SDLoc DL(Node); 737 738 /// 739 // Instruction Selection not handled by the auto-generated 740 // tablegen selection should be handled here. 741 /// 742 switch(Opcode) { 743 default: break; 744 745 case Mips::PseudoD_SELECT_I: 746 case Mips::PseudoD_SELECT_I64: { 747 MVT VT = Subtarget->isGP64bit() ? MVT::i64 : MVT::i32; 748 SDValue cond = Node->getOperand(0); 749 SDValue Hi1 = Node->getOperand(1); 750 SDValue Lo1 = Node->getOperand(2); 751 SDValue Hi2 = Node->getOperand(3); 752 SDValue Lo2 = Node->getOperand(4); 753 754 SDValue ops[] = {cond, Hi1, Lo1, Hi2, Lo2}; 755 EVT NodeTys[] = {VT, VT}; 756 ReplaceNode(Node, CurDAG->getMachineNode(Subtarget->isGP64bit() 757 ? Mips::PseudoD_SELECT_I64 758 : Mips::PseudoD_SELECT_I, 759 DL, NodeTys, ops)); 760 return true; 761 } 762 763 case ISD::ADDE: { 764 selectAddE(Node, DL); 765 return true; 766 } 767 768 case ISD::ConstantFP: { 769 auto *CN = cast<ConstantFPSDNode>(Node); 770 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) { 771 if (Subtarget->isGP64bit()) { 772 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, 773 Mips::ZERO_64, MVT::i64); 774 ReplaceNode(Node, 775 CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero)); 776 } else if (Subtarget->isFP64bit()) { 777 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, 778 Mips::ZERO, MVT::i32); 779 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL, 780 MVT::f64, Zero, Zero)); 781 } else { 782 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, 783 Mips::ZERO, MVT::i32); 784 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL, 785 MVT::f64, Zero, Zero)); 786 } 787 return true; 788 } 789 break; 790 } 791 792 case ISD::Constant: { 793 auto *CN = cast<ConstantSDNode>(Node); 794 int64_t Imm = CN->getSExtValue(); 795 unsigned Size = CN->getValueSizeInBits(0); 796 797 if (isInt<32>(Imm)) 798 break; 799 800 MipsAnalyzeImmediate AnalyzeImm; 801 802 const MipsAnalyzeImmediate::InstSeq &Seq = 803 AnalyzeImm.Analyze(Imm, Size, false); 804 805 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin(); 806 SDLoc DL(CN); 807 SDNode *RegOpnd; 808 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), 809 DL, MVT::i64); 810 811 // The first instruction can be a LUi which is different from other 812 // instructions (ADDiu, ORI and SLL) in that it does not have a register 813 // operand. 814 if (Inst->Opc == Mips::LUi64) 815 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd); 816 else 817 RegOpnd = 818 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, 819 CurDAG->getRegister(Mips::ZERO_64, MVT::i64), 820 ImmOpnd); 821 822 // The remaining instructions in the sequence are handled here. 823 for (++Inst; Inst != Seq.end(); ++Inst) { 824 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL, 825 MVT::i64); 826 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, 827 SDValue(RegOpnd, 0), ImmOpnd); 828 } 829 830 ReplaceNode(Node, RegOpnd); 831 return true; 832 } 833 834 case ISD::INTRINSIC_W_CHAIN: { 835 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { 836 default: 837 break; 838 839 case Intrinsic::mips_cfcmsa: { 840 SDValue ChainIn = Node->getOperand(0); 841 SDValue RegIdx = Node->getOperand(2); 842 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL, 843 getMSACtrlReg(RegIdx), MVT::i32); 844 ReplaceNode(Node, Reg.getNode()); 845 return true; 846 } 847 } 848 break; 849 } 850 851 case ISD::INTRINSIC_WO_CHAIN: { 852 switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) { 853 default: 854 break; 855 856 case Intrinsic::mips_move_v: 857 // Like an assignment but will always produce a move.v even if 858 // unnecessary. 859 ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL, 860 Node->getValueType(0), 861 Node->getOperand(1))); 862 return true; 863 } 864 break; 865 } 866 867 case ISD::INTRINSIC_VOID: { 868 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { 869 default: 870 break; 871 872 case Intrinsic::mips_ctcmsa: { 873 SDValue ChainIn = Node->getOperand(0); 874 SDValue RegIdx = Node->getOperand(2); 875 SDValue Value = Node->getOperand(3); 876 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL, 877 getMSACtrlReg(RegIdx), Value); 878 ReplaceNode(Node, ChainOut.getNode()); 879 return true; 880 } 881 } 882 break; 883 } 884 885 // Manually match MipsISD::Ins nodes to get the correct instruction. It has 886 // to be done in this fashion so that we respect the differences between 887 // dins and dinsm, as the difference is that the size operand has the range 888 // 0 < size <= 32 for dins while dinsm has the range 2 <= size <= 64 which 889 // means SelectionDAGISel would have to test all the operands at once to 890 // match the instruction. 891 case MipsISD::Ins: { 892 893 // Sanity checking for the node operands. 894 if (Node->getValueType(0) != MVT::i32 && Node->getValueType(0) != MVT::i64) 895 return false; 896 897 if (Node->getNumOperands() != 4) 898 return false; 899 900 if (Node->getOperand(1)->getOpcode() != ISD::Constant || 901 Node->getOperand(2)->getOpcode() != ISD::Constant) 902 return false; 903 904 MVT ResTy = Node->getSimpleValueType(0); 905 uint64_t Pos = Node->getConstantOperandVal(1); 906 uint64_t Size = Node->getConstantOperandVal(2); 907 908 // Size has to be >0 for 'ins', 'dins' and 'dinsu'. 909 if (!Size) 910 return false; 911 912 if (Pos + Size > 64) 913 return false; 914 915 if (ResTy != MVT::i32 && ResTy != MVT::i64) 916 return false; 917 918 unsigned Opcode = 0; 919 if (ResTy == MVT::i32) { 920 if (Pos + Size <= 32) 921 Opcode = Mips::INS; 922 } else { 923 if (Pos + Size <= 32) 924 Opcode = Mips::DINS; 925 else if (Pos < 32 && 1 < Size) 926 Opcode = Mips::DINSM; 927 else 928 Opcode = Mips::DINSU; 929 } 930 931 if (Opcode) { 932 SDValue Ops[4] = { 933 Node->getOperand(0), CurDAG->getTargetConstant(Pos, DL, MVT::i32), 934 CurDAG->getTargetConstant(Size, DL, MVT::i32), Node->getOperand(3)}; 935 936 ReplaceNode(Node, CurDAG->getMachineNode(Opcode, DL, ResTy, Ops)); 937 return true; 938 } 939 940 return false; 941 } 942 943 case MipsISD::ThreadPointer: { 944 EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout()); 945 unsigned RdhwrOpc, DestReg; 946 947 if (PtrVT == MVT::i32) { 948 RdhwrOpc = Mips::RDHWR; 949 DestReg = Mips::V1; 950 } else { 951 RdhwrOpc = Mips::RDHWR64; 952 DestReg = Mips::V1_64; 953 } 954 955 SDNode *Rdhwr = 956 CurDAG->getMachineNode(RdhwrOpc, DL, Node->getValueType(0), 957 CurDAG->getRegister(Mips::HWR29, MVT::i32), 958 CurDAG->getTargetConstant(0, DL, MVT::i32)); 959 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg, 960 SDValue(Rdhwr, 0)); 961 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT); 962 ReplaceNode(Node, ResNode.getNode()); 963 return true; 964 } 965 966 case ISD::BUILD_VECTOR: { 967 // Select appropriate ldi.[bhwd] instructions for constant splats of 968 // 128-bit when MSA is enabled. Fixup any register class mismatches that 969 // occur as a result. 970 // 971 // This allows the compiler to use a wider range of immediates than would 972 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then 973 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101, 974 // 0x01010101 } without using a constant pool. This would be sub-optimal 975 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the 976 // same set/ of registers. Similarly, ldi.h isn't capable of producing { 977 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can. 978 979 const MipsABIInfo &ABI = 980 static_cast<const MipsTargetMachine &>(TM).getABI(); 981 982 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node); 983 APInt SplatValue, SplatUndef; 984 unsigned SplatBitSize; 985 bool HasAnyUndefs; 986 unsigned LdiOp; 987 EVT ResVecTy = BVN->getValueType(0); 988 EVT ViaVecTy; 989 990 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector()) 991 return false; 992 993 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, 994 HasAnyUndefs, 8, 995 !Subtarget->isLittle())) 996 return false; 997 998 switch (SplatBitSize) { 999 default: 1000 return false; 1001 case 8: 1002 LdiOp = Mips::LDI_B; 1003 ViaVecTy = MVT::v16i8; 1004 break; 1005 case 16: 1006 LdiOp = Mips::LDI_H; 1007 ViaVecTy = MVT::v8i16; 1008 break; 1009 case 32: 1010 LdiOp = Mips::LDI_W; 1011 ViaVecTy = MVT::v4i32; 1012 break; 1013 case 64: 1014 LdiOp = Mips::LDI_D; 1015 ViaVecTy = MVT::v2i64; 1016 break; 1017 } 1018 1019 SDNode *Res = nullptr; 1020 1021 // If we have a signed 10 bit integer, we can splat it directly. 1022 // 1023 // If we have something bigger we can synthesize the value into a GPR and 1024 // splat from there. 1025 if (SplatValue.isSignedIntN(10)) { 1026 SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL, 1027 ViaVecTy.getVectorElementType()); 1028 1029 Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm); 1030 } else if (SplatValue.isSignedIntN(16) && 1031 ((ABI.IsO32() && SplatBitSize < 64) || 1032 (ABI.IsN32() || ABI.IsN64()))) { 1033 // Only handle signed 16 bit values when the element size is GPR width. 1034 // MIPS64 can handle all the cases but MIPS32 would need to handle 1035 // negative cases specifically here. Instead, handle those cases as 1036 // 64bit values. 1037 1038 bool Is32BitSplat = ABI.IsO32() || SplatBitSize < 64; 1039 const unsigned ADDiuOp = Is32BitSplat ? Mips::ADDiu : Mips::DADDiu; 1040 const MVT SplatMVT = Is32BitSplat ? MVT::i32 : MVT::i64; 1041 SDValue ZeroVal = CurDAG->getRegister( 1042 Is32BitSplat ? Mips::ZERO : Mips::ZERO_64, SplatMVT); 1043 1044 const unsigned FILLOp = 1045 SplatBitSize == 16 1046 ? Mips::FILL_H 1047 : (SplatBitSize == 32 ? Mips::FILL_W 1048 : (SplatBitSize == 64 ? Mips::FILL_D : 0)); 1049 1050 assert(FILLOp != 0 && "Unknown FILL Op for splat synthesis!"); 1051 assert((!ABI.IsO32() || (FILLOp != Mips::FILL_D)) && 1052 "Attempting to use fill.d on MIPS32!"); 1053 1054 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); 1055 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, SplatMVT); 1056 1057 Res = CurDAG->getMachineNode(ADDiuOp, DL, SplatMVT, ZeroVal, LoVal); 1058 Res = CurDAG->getMachineNode(FILLOp, DL, ViaVecTy, SDValue(Res, 0)); 1059 1060 } else if (SplatValue.isSignedIntN(32) && SplatBitSize == 32) { 1061 // Only handle the cases where the splat size agrees with the size 1062 // of the SplatValue here. 1063 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); 1064 const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue(); 1065 SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32); 1066 1067 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32); 1068 SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32); 1069 1070 if (Hi) 1071 Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal); 1072 1073 if (Lo) 1074 Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, 1075 Hi ? SDValue(Res, 0) : ZeroVal, LoVal); 1076 1077 assert((Hi || Lo) && "Zero case reached 32 bit case splat synthesis!"); 1078 Res = 1079 CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32, SDValue(Res, 0)); 1080 1081 } else if (SplatValue.isSignedIntN(32) && SplatBitSize == 64 && 1082 (ABI.IsN32() || ABI.IsN64())) { 1083 // N32 and N64 can perform some tricks that O32 can't for signed 32 bit 1084 // integers due to having 64bit registers. lui will cause the necessary 1085 // zero/sign extension. 1086 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); 1087 const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue(); 1088 SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32); 1089 1090 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32); 1091 SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32); 1092 1093 if (Hi) 1094 Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal); 1095 1096 if (Lo) 1097 Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, 1098 Hi ? SDValue(Res, 0) : ZeroVal, LoVal); 1099 1100 Res = CurDAG->getMachineNode( 1101 Mips::SUBREG_TO_REG, DL, MVT::i64, 1102 CurDAG->getTargetConstant(((Hi >> 15) & 0x1), DL, MVT::i64), 1103 SDValue(Res, 0), 1104 CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64)); 1105 1106 Res = 1107 CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64, SDValue(Res, 0)); 1108 1109 } else if (SplatValue.isSignedIntN(64)) { 1110 // If we have a 64 bit Splat value, we perform a similar sequence to the 1111 // above: 1112 // 1113 // MIPS32: MIPS64: 1114 // lui $res, %highest(val) lui $res, %highest(val) 1115 // ori $res, $res, %higher(val) ori $res, $res, %higher(val) 1116 // lui $res2, %hi(val) lui $res2, %hi(val) 1117 // ori $res2, %res2, %lo(val) ori $res2, %res2, %lo(val) 1118 // $res3 = fill $res2 dinsu $res, $res2, 0, 32 1119 // $res4 = insert.w $res3[1], $res fill.d $res 1120 // splat.d $res4, 0 1121 // 1122 // The ability to use dinsu is guaranteed as MSA requires MIPSR5. 1123 // This saves having to materialize the value by shifts and ors. 1124 // 1125 // FIXME: Implement the preferred sequence for MIPS64R6: 1126 // 1127 // MIPS64R6: 1128 // ori $res, $zero, %lo(val) 1129 // daui $res, $res, %hi(val) 1130 // dahi $res, $res, %higher(val) 1131 // dati $res, $res, %highest(cal) 1132 // fill.d $res 1133 // 1134 1135 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); 1136 const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue(); 1137 const unsigned Higher = SplatValue.lshr(32).getLoBits(16).getZExtValue(); 1138 const unsigned Highest = SplatValue.lshr(48).getLoBits(16).getZExtValue(); 1139 1140 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32); 1141 SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32); 1142 SDValue HigherVal = CurDAG->getTargetConstant(Higher, DL, MVT::i32); 1143 SDValue HighestVal = CurDAG->getTargetConstant(Highest, DL, MVT::i32); 1144 SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32); 1145 1146 // Independent of whether we're targeting MIPS64 or not, the basic 1147 // operations are the same. Also, directly use the $zero register if 1148 // the 16 bit chunk is zero. 1149 // 1150 // For optimization purposes we always synthesize the splat value as 1151 // an i32 value, then if we're targetting MIPS64, use SUBREG_TO_REG 1152 // just before combining the values with dinsu to produce an i64. This 1153 // enables SelectionDAG to aggressively share components of splat values 1154 // where possible. 1155 // 1156 // FIXME: This is the general constant synthesis problem. This code 1157 // should be factored out into a class shared between all the 1158 // classes that need it. Specifically, for a splat size of 64 1159 // bits that's a negative number we can do better than LUi/ORi 1160 // for the upper 32bits. 1161 1162 if (Hi) 1163 Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal); 1164 1165 if (Lo) 1166 Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, 1167 Hi ? SDValue(Res, 0) : ZeroVal, LoVal); 1168 1169 SDNode *HiRes; 1170 if (Highest) 1171 HiRes = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HighestVal); 1172 1173 if (Higher) 1174 HiRes = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, 1175 Highest ? SDValue(HiRes, 0) : ZeroVal, 1176 HigherVal); 1177 1178 1179 if (ABI.IsO32()) { 1180 Res = CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32, 1181 (Hi || Lo) ? SDValue(Res, 0) : ZeroVal); 1182 1183 Res = CurDAG->getMachineNode( 1184 Mips::INSERT_W, DL, MVT::v4i32, SDValue(Res, 0), 1185 (Highest || Higher) ? SDValue(HiRes, 0) : ZeroVal, 1186 CurDAG->getTargetConstant(1, DL, MVT::i32)); 1187 1188 const TargetLowering *TLI = getTargetLowering(); 1189 const TargetRegisterClass *RC = 1190 TLI->getRegClassFor(ViaVecTy.getSimpleVT()); 1191 1192 Res = CurDAG->getMachineNode( 1193 Mips::COPY_TO_REGCLASS, DL, ViaVecTy, SDValue(Res, 0), 1194 CurDAG->getTargetConstant(RC->getID(), DL, MVT::i32)); 1195 1196 Res = CurDAG->getMachineNode( 1197 Mips::SPLATI_D, DL, MVT::v2i64, SDValue(Res, 0), 1198 CurDAG->getTargetConstant(0, DL, MVT::i32)); 1199 } else if (ABI.IsN64() || ABI.IsN32()) { 1200 1201 SDValue Zero64Val = CurDAG->getRegister(Mips::ZERO_64, MVT::i64); 1202 const bool HiResNonZero = Highest || Higher; 1203 const bool ResNonZero = Hi || Lo; 1204 1205 if (HiResNonZero) 1206 HiRes = CurDAG->getMachineNode( 1207 Mips::SUBREG_TO_REG, DL, MVT::i64, 1208 CurDAG->getTargetConstant(((Highest >> 15) & 0x1), DL, MVT::i64), 1209 SDValue(HiRes, 0), 1210 CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64)); 1211 1212 if (ResNonZero) 1213 Res = CurDAG->getMachineNode( 1214 Mips::SUBREG_TO_REG, DL, MVT::i64, 1215 CurDAG->getTargetConstant(((Hi >> 15) & 0x1), DL, MVT::i64), 1216 SDValue(Res, 0), 1217 CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64)); 1218 1219 // We have 3 cases: 1220 // The HiRes is nonzero but Res is $zero => dsll32 HiRes, 0 1221 // The Res is nonzero but HiRes is $zero => dinsu Res, $zero, 32, 32 1222 // Both are non zero => dinsu Res, HiRes, 32, 32 1223 // 1224 // The obvious "missing" case is when both are zero, but that case is 1225 // handled by the ldi case. 1226 if (ResNonZero) { 1227 IntegerType *Int32Ty = 1228 IntegerType::get(MF->getFunction().getContext(), 32); 1229 const ConstantInt *Const32 = ConstantInt::get(Int32Ty, 32); 1230 SDValue Ops[4] = {HiResNonZero ? SDValue(HiRes, 0) : Zero64Val, 1231 CurDAG->getConstant(*Const32, DL, MVT::i32), 1232 CurDAG->getConstant(*Const32, DL, MVT::i32), 1233 SDValue(Res, 0)}; 1234 1235 Res = CurDAG->getMachineNode(Mips::DINSU, DL, MVT::i64, Ops); 1236 } else if (HiResNonZero) { 1237 Res = CurDAG->getMachineNode( 1238 Mips::DSLL32, DL, MVT::i64, SDValue(HiRes, 0), 1239 CurDAG->getTargetConstant(0, DL, MVT::i32)); 1240 } else 1241 llvm_unreachable( 1242 "Zero splat value handled by non-zero 64bit splat synthesis!"); 1243 1244 Res = CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64, 1245 SDValue(Res, 0)); 1246 } else 1247 llvm_unreachable("Unknown ABI in MipsISelDAGToDAG!"); 1248 1249 } else 1250 return false; 1251 1252 if (ResVecTy != ViaVecTy) { 1253 // If LdiOp is writing to a different register class to ResVecTy, then 1254 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v 1255 // since the source and destination register sets contain the same 1256 // registers. 1257 const TargetLowering *TLI = getTargetLowering(); 1258 MVT ResVecTySimple = ResVecTy.getSimpleVT(); 1259 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple); 1260 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL, 1261 ResVecTy, SDValue(Res, 0), 1262 CurDAG->getTargetConstant(RC->getID(), DL, 1263 MVT::i32)); 1264 } 1265 1266 ReplaceNode(Node, Res); 1267 return true; 1268 } 1269 1270 } 1271 1272 return false; 1273 } 1274 1275 bool MipsSEDAGToDAGISel:: 1276 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, 1277 std::vector<SDValue> &OutOps) { 1278 SDValue Base, Offset; 1279 1280 switch(ConstraintID) { 1281 default: 1282 llvm_unreachable("Unexpected asm memory constraint"); 1283 // All memory constraints can at least accept raw pointers. 1284 case InlineAsm::Constraint_i: 1285 OutOps.push_back(Op); 1286 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); 1287 return false; 1288 case InlineAsm::Constraint_m: 1289 case InlineAsm::Constraint_o: 1290 if (selectAddrRegImm16(Op, Base, Offset)) { 1291 OutOps.push_back(Base); 1292 OutOps.push_back(Offset); 1293 return false; 1294 } 1295 OutOps.push_back(Op); 1296 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); 1297 return false; 1298 case InlineAsm::Constraint_R: 1299 // The 'R' constraint is supposed to be much more complicated than this. 1300 // However, it's becoming less useful due to architectural changes and 1301 // ought to be replaced by other constraints such as 'ZC'. 1302 // For now, support 9-bit signed offsets which is supportable by all 1303 // subtargets for all instructions. 1304 if (selectAddrRegImm9(Op, Base, Offset)) { 1305 OutOps.push_back(Base); 1306 OutOps.push_back(Offset); 1307 return false; 1308 } 1309 OutOps.push_back(Op); 1310 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); 1311 return false; 1312 case InlineAsm::Constraint_ZC: 1313 // ZC matches whatever the pref, ll, and sc instructions can handle for the 1314 // given subtarget. 1315 if (Subtarget->inMicroMipsMode()) { 1316 // On microMIPS, they can handle 12-bit offsets. 1317 if (selectAddrRegImm12(Op, Base, Offset)) { 1318 OutOps.push_back(Base); 1319 OutOps.push_back(Offset); 1320 return false; 1321 } 1322 } else if (Subtarget->hasMips32r6()) { 1323 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets. 1324 if (selectAddrRegImm9(Op, Base, Offset)) { 1325 OutOps.push_back(Base); 1326 OutOps.push_back(Offset); 1327 return false; 1328 } 1329 } else if (selectAddrRegImm16(Op, Base, Offset)) { 1330 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets. 1331 OutOps.push_back(Base); 1332 OutOps.push_back(Offset); 1333 return false; 1334 } 1335 // In all cases, 0-bit offsets are acceptable. 1336 OutOps.push_back(Op); 1337 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); 1338 return false; 1339 } 1340 return true; 1341 } 1342 1343 FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM, 1344 CodeGenOpt::Level OptLevel) { 1345 return new MipsSEDAGToDAGISel(TM, OptLevel); 1346 } 1347