1 //===- MipsRegisterBankInfo.cpp ---------------------------------*- C++ -*-===// 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 /// \file 9 /// This file implements the targeting of the RegisterBankInfo class for Mips. 10 /// \todo This should be generated by TableGen. 11 //===----------------------------------------------------------------------===// 12 13 #include "MipsRegisterBankInfo.h" 14 #include "MipsInstrInfo.h" 15 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" 16 #include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h" 17 #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h" 18 #include "llvm/CodeGen/MachineRegisterInfo.h" 19 20 #define GET_TARGET_REGBANK_IMPL 21 22 #include "MipsGenRegisterBank.inc" 23 24 namespace llvm { 25 namespace Mips { 26 enum PartialMappingIdx { 27 PMI_GPR, 28 PMI_SPR, 29 PMI_DPR, 30 PMI_Min = PMI_GPR, 31 }; 32 33 RegisterBankInfo::PartialMapping PartMappings[]{ 34 {0, 32, GPRBRegBank}, 35 {0, 32, FPRBRegBank}, 36 {0, 64, FPRBRegBank} 37 }; 38 39 enum ValueMappingIdx { 40 InvalidIdx = 0, 41 GPRIdx = 1, 42 SPRIdx = 4, 43 DPRIdx = 7 44 }; 45 46 RegisterBankInfo::ValueMapping ValueMappings[] = { 47 // invalid 48 {nullptr, 0}, 49 // up to 3 operands in GPRs 50 {&PartMappings[PMI_GPR - PMI_Min], 1}, 51 {&PartMappings[PMI_GPR - PMI_Min], 1}, 52 {&PartMappings[PMI_GPR - PMI_Min], 1}, 53 // up to 3 operands in FPRs - single precission 54 {&PartMappings[PMI_SPR - PMI_Min], 1}, 55 {&PartMappings[PMI_SPR - PMI_Min], 1}, 56 {&PartMappings[PMI_SPR - PMI_Min], 1}, 57 // up to 3 operands in FPRs - double precission 58 {&PartMappings[PMI_DPR - PMI_Min], 1}, 59 {&PartMappings[PMI_DPR - PMI_Min], 1}, 60 {&PartMappings[PMI_DPR - PMI_Min], 1} 61 }; 62 63 } // end namespace Mips 64 } // end namespace llvm 65 66 using namespace llvm; 67 68 MipsRegisterBankInfo::MipsRegisterBankInfo(const TargetRegisterInfo &TRI) 69 : MipsGenRegisterBankInfo() {} 70 71 const RegisterBank &MipsRegisterBankInfo::getRegBankFromRegClass( 72 const TargetRegisterClass &RC) const { 73 using namespace Mips; 74 75 switch (RC.getID()) { 76 case Mips::GPR32RegClassID: 77 case Mips::CPU16Regs_and_GPRMM16ZeroRegClassID: 78 case Mips::GPRMM16MovePPairFirstRegClassID: 79 case Mips::CPU16Regs_and_GPRMM16MovePPairSecondRegClassID: 80 case Mips::GPRMM16MoveP_and_CPU16Regs_and_GPRMM16ZeroRegClassID: 81 case Mips::GPRMM16MovePPairFirst_and_GPRMM16MovePPairSecondRegClassID: 82 case Mips::SP32RegClassID: 83 case Mips::GP32RegClassID: 84 return getRegBank(Mips::GPRBRegBankID); 85 case Mips::FGRCCRegClassID: 86 case Mips::FGR32RegClassID: 87 case Mips::FGR64RegClassID: 88 case Mips::AFGR64RegClassID: 89 return getRegBank(Mips::FPRBRegBankID); 90 default: 91 llvm_unreachable("Register class not supported"); 92 } 93 } 94 95 // Instructions where all register operands are floating point. 96 static bool isFloatingPointOpcode(unsigned Opc) { 97 switch (Opc) { 98 case TargetOpcode::G_FCONSTANT: 99 case TargetOpcode::G_FADD: 100 case TargetOpcode::G_FSUB: 101 case TargetOpcode::G_FMUL: 102 case TargetOpcode::G_FDIV: 103 case TargetOpcode::G_FABS: 104 case TargetOpcode::G_FSQRT: 105 case TargetOpcode::G_FCEIL: 106 case TargetOpcode::G_FFLOOR: 107 case TargetOpcode::G_FPEXT: 108 case TargetOpcode::G_FPTRUNC: 109 return true; 110 default: 111 return false; 112 } 113 } 114 115 // Instructions where use operands are floating point registers. 116 // Def operands are general purpose. 117 static bool isFloatingPointOpcodeUse(unsigned Opc) { 118 switch (Opc) { 119 case TargetOpcode::G_FPTOSI: 120 case TargetOpcode::G_FPTOUI: 121 case TargetOpcode::G_FCMP: 122 case Mips::MFC1: 123 case Mips::ExtractElementF64: 124 case Mips::ExtractElementF64_64: 125 return true; 126 default: 127 return isFloatingPointOpcode(Opc); 128 } 129 } 130 131 // Instructions where def operands are floating point registers. 132 // Use operands are general purpose. 133 static bool isFloatingPointOpcodeDef(unsigned Opc) { 134 switch (Opc) { 135 case TargetOpcode::G_SITOFP: 136 case TargetOpcode::G_UITOFP: 137 case Mips::MTC1: 138 case Mips::BuildPairF64: 139 case Mips::BuildPairF64_64: 140 return true; 141 default: 142 return isFloatingPointOpcode(Opc); 143 } 144 } 145 146 static bool isAmbiguous(unsigned Opc) { 147 switch (Opc) { 148 case TargetOpcode::G_LOAD: 149 case TargetOpcode::G_STORE: 150 case TargetOpcode::G_PHI: 151 case TargetOpcode::G_SELECT: 152 case TargetOpcode::G_IMPLICIT_DEF: 153 return true; 154 default: 155 return false; 156 } 157 } 158 159 void MipsRegisterBankInfo::AmbiguousRegDefUseContainer::addDefUses( 160 Register Reg, const MachineRegisterInfo &MRI) { 161 assert(!MRI.getType(Reg).isPointer() && 162 "Pointers are gprb, they should not be considered as ambiguous.\n"); 163 for (MachineInstr &UseMI : MRI.use_instructions(Reg)) { 164 MachineInstr *NonCopyInstr = skipCopiesOutgoing(&UseMI); 165 // Copy with many uses. 166 if (NonCopyInstr->getOpcode() == TargetOpcode::COPY && 167 !Register::isPhysicalRegister(NonCopyInstr->getOperand(0).getReg())) 168 addDefUses(NonCopyInstr->getOperand(0).getReg(), MRI); 169 else 170 DefUses.push_back(skipCopiesOutgoing(&UseMI)); 171 } 172 } 173 174 void MipsRegisterBankInfo::AmbiguousRegDefUseContainer::addUseDef( 175 Register Reg, const MachineRegisterInfo &MRI) { 176 assert(!MRI.getType(Reg).isPointer() && 177 "Pointers are gprb, they should not be considered as ambiguous.\n"); 178 MachineInstr *DefMI = MRI.getVRegDef(Reg); 179 UseDefs.push_back(skipCopiesIncoming(DefMI)); 180 } 181 182 MachineInstr * 183 MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesOutgoing( 184 MachineInstr *MI) const { 185 const MachineFunction &MF = *MI->getParent()->getParent(); 186 const MachineRegisterInfo &MRI = MF.getRegInfo(); 187 MachineInstr *Ret = MI; 188 while (Ret->getOpcode() == TargetOpcode::COPY && 189 !Register::isPhysicalRegister(Ret->getOperand(0).getReg()) && 190 MRI.hasOneUse(Ret->getOperand(0).getReg())) { 191 Ret = &(*MRI.use_instr_begin(Ret->getOperand(0).getReg())); 192 } 193 return Ret; 194 } 195 196 MachineInstr * 197 MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesIncoming( 198 MachineInstr *MI) const { 199 const MachineFunction &MF = *MI->getParent()->getParent(); 200 const MachineRegisterInfo &MRI = MF.getRegInfo(); 201 MachineInstr *Ret = MI; 202 while (Ret->getOpcode() == TargetOpcode::COPY && 203 !Register::isPhysicalRegister(Ret->getOperand(1).getReg())) 204 Ret = MRI.getVRegDef(Ret->getOperand(1).getReg()); 205 return Ret; 206 } 207 208 MipsRegisterBankInfo::AmbiguousRegDefUseContainer::AmbiguousRegDefUseContainer( 209 const MachineInstr *MI) { 210 assert(isAmbiguous(MI->getOpcode()) && 211 "Not implemented for non Ambiguous opcode.\n"); 212 213 const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo(); 214 215 if (MI->getOpcode() == TargetOpcode::G_LOAD) 216 addDefUses(MI->getOperand(0).getReg(), MRI); 217 218 if (MI->getOpcode() == TargetOpcode::G_STORE) 219 addUseDef(MI->getOperand(0).getReg(), MRI); 220 221 if (MI->getOpcode() == TargetOpcode::G_PHI) { 222 addDefUses(MI->getOperand(0).getReg(), MRI); 223 224 for (unsigned i = 1; i < MI->getNumOperands(); i += 2) 225 addUseDef(MI->getOperand(i).getReg(), MRI); 226 } 227 228 if (MI->getOpcode() == TargetOpcode::G_SELECT) { 229 addDefUses(MI->getOperand(0).getReg(), MRI); 230 231 addUseDef(MI->getOperand(2).getReg(), MRI); 232 addUseDef(MI->getOperand(3).getReg(), MRI); 233 } 234 235 if (MI->getOpcode() == TargetOpcode::G_IMPLICIT_DEF) 236 addDefUses(MI->getOperand(0).getReg(), MRI); 237 } 238 239 bool MipsRegisterBankInfo::TypeInfoForMF::visit( 240 const MachineInstr *MI, const MachineInstr *WaitingForTypeOfMI) { 241 assert(isAmbiguous(MI->getOpcode()) && "Visiting non-Ambiguous opcode.\n"); 242 if (wasVisited(MI)) 243 return true; // InstType has already been determined for MI. 244 245 startVisit(MI); 246 AmbiguousRegDefUseContainer DefUseContainer(MI); 247 248 // Visit instructions where MI's DEF operands are USED. 249 if (visitAdjacentInstrs(MI, DefUseContainer.getDefUses(), true)) 250 return true; 251 252 // Visit instructions that DEFINE MI's USE operands. 253 if (visitAdjacentInstrs(MI, DefUseContainer.getUseDefs(), false)) 254 return true; 255 256 // All MI's adjacent instructions, are ambiguous. 257 if (!WaitingForTypeOfMI) { 258 // This is chain of ambiguous instructions. 259 setTypes(MI, InstType::Ambiguous); 260 return true; 261 } 262 // Excluding WaitingForTypeOfMI, MI is either connected to chains of ambiguous 263 // instructions or has no other adjacent instructions. Anyway InstType could 264 // not be determined. There could be unexplored path from some of 265 // WaitingForTypeOfMI's adjacent instructions to an instruction with only one 266 // mapping available. 267 // We are done with this branch, add MI to WaitingForTypeOfMI's WaitingQueue, 268 // this way when WaitingForTypeOfMI figures out its InstType same InstType 269 // will be assigned to all instructions in this branch. 270 addToWaitingQueue(WaitingForTypeOfMI, MI); 271 return false; 272 } 273 274 bool MipsRegisterBankInfo::TypeInfoForMF::visitAdjacentInstrs( 275 const MachineInstr *MI, SmallVectorImpl<MachineInstr *> &AdjacentInstrs, 276 bool isDefUse) { 277 while (!AdjacentInstrs.empty()) { 278 MachineInstr *AdjMI = AdjacentInstrs.pop_back_val(); 279 280 if (isDefUse ? isFloatingPointOpcodeUse(AdjMI->getOpcode()) 281 : isFloatingPointOpcodeDef(AdjMI->getOpcode())) { 282 setTypes(MI, InstType::FloatingPoint); 283 return true; 284 } 285 286 // Determine InstType from register bank of phys register that is 287 // 'isDefUse ? def : use' of this copy. 288 if (AdjMI->getOpcode() == TargetOpcode::COPY) { 289 setTypesAccordingToPhysicalRegister(MI, AdjMI, isDefUse ? 0 : 1); 290 return true; 291 } 292 293 // Defaults to integer instruction. Includes G_MERGE_VALUES and 294 // G_UNMERGE_VALUES. 295 if (!isAmbiguous(AdjMI->getOpcode())) { 296 setTypes(MI, InstType::Integer); 297 return true; 298 } 299 300 // When AdjMI was visited first, MI has to continue to explore remaining 301 // adjacent instructions and determine InstType without visiting AdjMI. 302 if (!wasVisited(AdjMI) || 303 getRecordedTypeForInstr(AdjMI) != InstType::NotDetermined) { 304 if (visit(AdjMI, MI)) { 305 // InstType is successfully determined and is same as for AdjMI. 306 setTypes(MI, getRecordedTypeForInstr(AdjMI)); 307 return true; 308 } 309 } 310 } 311 return false; 312 } 313 314 void MipsRegisterBankInfo::TypeInfoForMF::setTypes(const MachineInstr *MI, 315 InstType InstTy) { 316 changeRecordedTypeForInstr(MI, InstTy); 317 for (const MachineInstr *WaitingInstr : getWaitingQueueFor(MI)) { 318 setTypes(WaitingInstr, InstTy); 319 } 320 } 321 322 void MipsRegisterBankInfo::TypeInfoForMF::setTypesAccordingToPhysicalRegister( 323 const MachineInstr *MI, const MachineInstr *CopyInst, unsigned Op) { 324 assert((Register::isPhysicalRegister(CopyInst->getOperand(Op).getReg())) && 325 "Copies of non physical registers should not be considered here.\n"); 326 327 const MachineFunction &MF = *CopyInst->getMF(); 328 const MachineRegisterInfo &MRI = MF.getRegInfo(); 329 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 330 const RegisterBankInfo &RBI = 331 *CopyInst->getMF()->getSubtarget().getRegBankInfo(); 332 const RegisterBank *Bank = 333 RBI.getRegBank(CopyInst->getOperand(Op).getReg(), MRI, TRI); 334 335 if (Bank == &Mips::FPRBRegBank) 336 setTypes(MI, InstType::FloatingPoint); 337 else if (Bank == &Mips::GPRBRegBank) 338 setTypes(MI, InstType::Integer); 339 else 340 llvm_unreachable("Unsupported register bank.\n"); 341 } 342 343 MipsRegisterBankInfo::InstType 344 MipsRegisterBankInfo::TypeInfoForMF::determineInstType(const MachineInstr *MI) { 345 visit(MI, nullptr); 346 return getRecordedTypeForInstr(MI); 347 } 348 349 void MipsRegisterBankInfo::TypeInfoForMF::cleanupIfNewFunction( 350 llvm::StringRef FunctionName) { 351 if (MFName != FunctionName) { 352 MFName = FunctionName; 353 WaitingQueues.clear(); 354 Types.clear(); 355 } 356 } 357 358 static const MipsRegisterBankInfo::ValueMapping *getFprbMapping(unsigned Size) { 359 return Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx] 360 : &Mips::ValueMappings[Mips::DPRIdx]; 361 } 362 363 static const unsigned CustomMappingID = 1; 364 365 // Only 64 bit mapping is available in fprb and will be marked as custom, i.e. 366 // will be split into two 32 bit registers in gprb. 367 static const MipsRegisterBankInfo::ValueMapping * 368 getGprbOrCustomMapping(unsigned Size, unsigned &MappingID) { 369 if (Size == 32) 370 return &Mips::ValueMappings[Mips::GPRIdx]; 371 372 MappingID = CustomMappingID; 373 return &Mips::ValueMappings[Mips::DPRIdx]; 374 } 375 376 const RegisterBankInfo::InstructionMapping & 377 MipsRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const { 378 379 static TypeInfoForMF TI; 380 381 // Reset TI internal data when MF changes. 382 TI.cleanupIfNewFunction(MI.getMF()->getName()); 383 384 unsigned Opc = MI.getOpcode(); 385 const MachineFunction &MF = *MI.getParent()->getParent(); 386 const MachineRegisterInfo &MRI = MF.getRegInfo(); 387 388 if (MI.getOpcode() != TargetOpcode::G_PHI) { 389 const RegisterBankInfo::InstructionMapping &Mapping = 390 getInstrMappingImpl(MI); 391 if (Mapping.isValid()) 392 return Mapping; 393 } 394 395 using namespace TargetOpcode; 396 397 unsigned NumOperands = MI.getNumOperands(); 398 const ValueMapping *OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx]; 399 unsigned MappingID = DefaultMappingID; 400 401 // Check if LLT sizes match sizes of available register banks. 402 for (const MachineOperand &Op : MI.operands()) { 403 if (Op.isReg()) { 404 LLT RegTy = MRI.getType(Op.getReg()); 405 406 if (RegTy.isScalar() && 407 (RegTy.getSizeInBits() != 32 && RegTy.getSizeInBits() != 64)) 408 return getInvalidInstructionMapping(); 409 } 410 } 411 412 const LLT Op0Ty = MRI.getType(MI.getOperand(0).getReg()); 413 unsigned Op0Size = Op0Ty.getSizeInBits(); 414 InstType InstTy = InstType::Integer; 415 416 switch (Opc) { 417 case G_TRUNC: 418 case G_ADD: 419 case G_SUB: 420 case G_MUL: 421 case G_UMULH: 422 case G_ZEXTLOAD: 423 case G_SEXTLOAD: 424 case G_GEP: 425 case G_INTTOPTR: 426 case G_PTRTOINT: 427 case G_AND: 428 case G_OR: 429 case G_XOR: 430 case G_SHL: 431 case G_ASHR: 432 case G_LSHR: 433 case G_SDIV: 434 case G_UDIV: 435 case G_SREM: 436 case G_UREM: 437 case G_BRINDIRECT: 438 case G_VASTART: 439 OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx]; 440 break; 441 case G_STORE: 442 case G_LOAD: 443 if (!Op0Ty.isPointer()) 444 InstTy = TI.determineInstType(&MI); 445 446 if (InstTy == InstType::FloatingPoint || 447 (Op0Size == 64 && InstTy == InstType::Ambiguous)) 448 OperandsMapping = getOperandsMapping( 449 {getFprbMapping(Op0Size), &Mips::ValueMappings[Mips::GPRIdx]}); 450 else 451 OperandsMapping = 452 getOperandsMapping({getGprbOrCustomMapping(Op0Size, MappingID), 453 &Mips::ValueMappings[Mips::GPRIdx]}); 454 455 break; 456 case G_PHI: 457 if (!Op0Ty.isPointer()) 458 InstTy = TI.determineInstType(&MI); 459 460 // PHI is copylike and should have one regbank in mapping for def register. 461 if (InstTy == InstType::Integer && Op0Size == 64) { 462 OperandsMapping = 463 getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx]}); 464 return getInstructionMapping(CustomMappingID, /*Cost=*/1, OperandsMapping, 465 /*NumOperands=*/1); 466 } 467 // Use default handling for PHI, i.e. set reg bank of def operand to match 468 // register banks of use operands. 469 return getInstrMappingImpl(MI); 470 case G_SELECT: { 471 if (!Op0Ty.isPointer()) 472 InstTy = TI.determineInstType(&MI); 473 474 if (InstTy == InstType::FloatingPoint || 475 (Op0Size == 64 && InstTy == InstType::Ambiguous)) { 476 const RegisterBankInfo::ValueMapping *Bank = getFprbMapping(Op0Size); 477 OperandsMapping = getOperandsMapping( 478 {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank}); 479 break; 480 } else { 481 const RegisterBankInfo::ValueMapping *Bank = 482 getGprbOrCustomMapping(Op0Size, MappingID); 483 OperandsMapping = getOperandsMapping( 484 {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank}); 485 } 486 break; 487 } 488 case G_IMPLICIT_DEF: 489 if (!Op0Ty.isPointer()) 490 InstTy = TI.determineInstType(&MI); 491 492 if (InstTy == InstType::FloatingPoint) 493 OperandsMapping = getFprbMapping(Op0Size); 494 else 495 OperandsMapping = getGprbOrCustomMapping(Op0Size, MappingID); 496 497 break; 498 case G_UNMERGE_VALUES: 499 OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], 500 &Mips::ValueMappings[Mips::GPRIdx], 501 &Mips::ValueMappings[Mips::DPRIdx]}); 502 MappingID = CustomMappingID; 503 break; 504 case G_MERGE_VALUES: 505 OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx], 506 &Mips::ValueMappings[Mips::GPRIdx], 507 &Mips::ValueMappings[Mips::GPRIdx]}); 508 MappingID = CustomMappingID; 509 break; 510 case G_FADD: 511 case G_FSUB: 512 case G_FMUL: 513 case G_FDIV: 514 case G_FABS: 515 case G_FSQRT: 516 OperandsMapping = getFprbMapping(Op0Size); 517 break; 518 case G_FCONSTANT: 519 OperandsMapping = getOperandsMapping({getFprbMapping(Op0Size), nullptr}); 520 break; 521 case G_FCMP: { 522 unsigned Op2Size = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits(); 523 OperandsMapping = 524 getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr, 525 getFprbMapping(Op2Size), getFprbMapping(Op2Size)}); 526 break; 527 } 528 case G_FPEXT: 529 OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx], 530 &Mips::ValueMappings[Mips::SPRIdx]}); 531 break; 532 case G_FPTRUNC: 533 OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::SPRIdx], 534 &Mips::ValueMappings[Mips::DPRIdx]}); 535 break; 536 case G_FPTOSI: { 537 assert((Op0Size == 32) && "Unsupported integer size"); 538 unsigned SizeFP = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 539 OperandsMapping = getOperandsMapping( 540 {&Mips::ValueMappings[Mips::GPRIdx], getFprbMapping(SizeFP)}); 541 break; 542 } 543 case G_SITOFP: 544 assert((MRI.getType(MI.getOperand(1).getReg()).getSizeInBits() == 32) && 545 "Unsupported integer size"); 546 OperandsMapping = getOperandsMapping( 547 {getFprbMapping(Op0Size), &Mips::ValueMappings[Mips::GPRIdx]}); 548 break; 549 case G_CONSTANT: 550 case G_FRAME_INDEX: 551 case G_GLOBAL_VALUE: 552 case G_JUMP_TABLE: 553 case G_BRCOND: 554 OperandsMapping = 555 getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr}); 556 break; 557 case G_BRJT: 558 OperandsMapping = 559 getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr, 560 &Mips::ValueMappings[Mips::GPRIdx]}); 561 break; 562 case G_ICMP: 563 OperandsMapping = 564 getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr, 565 &Mips::ValueMappings[Mips::GPRIdx], 566 &Mips::ValueMappings[Mips::GPRIdx]}); 567 break; 568 default: 569 return getInvalidInstructionMapping(); 570 } 571 572 return getInstructionMapping(MappingID, /*Cost=*/1, OperandsMapping, 573 NumOperands); 574 } 575 576 using InstListTy = GISelWorkList<4>; 577 namespace { 578 class InstManager : public GISelChangeObserver { 579 InstListTy &InstList; 580 581 public: 582 InstManager(InstListTy &Insts) : InstList(Insts) {} 583 584 void createdInstr(MachineInstr &MI) override { InstList.insert(&MI); } 585 void erasingInstr(MachineInstr &MI) override {} 586 void changingInstr(MachineInstr &MI) override {} 587 void changedInstr(MachineInstr &MI) override {} 588 }; 589 } // end anonymous namespace 590 591 void MipsRegisterBankInfo::setRegBank(MachineInstr &MI, 592 MachineRegisterInfo &MRI) const { 593 Register Dest = MI.getOperand(0).getReg(); 594 switch (MI.getOpcode()) { 595 case TargetOpcode::G_STORE: 596 // No def operands, skip this instruction. 597 break; 598 case TargetOpcode::G_CONSTANT: 599 case TargetOpcode::G_LOAD: 600 case TargetOpcode::G_SELECT: 601 case TargetOpcode::G_PHI: 602 case TargetOpcode::G_IMPLICIT_DEF: { 603 assert(MRI.getType(Dest) == LLT::scalar(32) && "Unexpected operand type."); 604 MRI.setRegBank(Dest, getRegBank(Mips::GPRBRegBankID)); 605 break; 606 } 607 case TargetOpcode::G_GEP: { 608 assert(MRI.getType(Dest).isPointer() && "Unexpected operand type."); 609 MRI.setRegBank(Dest, getRegBank(Mips::GPRBRegBankID)); 610 break; 611 } 612 default: 613 llvm_unreachable("Unexpected opcode."); 614 } 615 } 616 617 static void 618 combineAwayG_UNMERGE_VALUES(LegalizationArtifactCombiner &ArtCombiner, 619 MachineInstr &MI) { 620 SmallVector<MachineInstr *, 2> DeadInstrs; 621 ArtCombiner.tryCombineMerges(MI, DeadInstrs); 622 for (MachineInstr *DeadMI : DeadInstrs) 623 DeadMI->eraseFromParent(); 624 } 625 626 void MipsRegisterBankInfo::applyMappingImpl( 627 const OperandsMapper &OpdMapper) const { 628 MachineInstr &MI = OpdMapper.getMI(); 629 InstListTy NewInstrs; 630 MachineIRBuilder B(MI); 631 MachineFunction *MF = MI.getMF(); 632 MachineRegisterInfo &MRI = OpdMapper.getMRI(); 633 const LegalizerInfo &LegInfo = *MF->getSubtarget().getLegalizerInfo(); 634 635 InstManager NewInstrObserver(NewInstrs); 636 GISelObserverWrapper WrapperObserver(&NewInstrObserver); 637 LegalizerHelper Helper(*MF, WrapperObserver, B); 638 LegalizationArtifactCombiner ArtCombiner(B, MF->getRegInfo(), LegInfo); 639 640 switch (MI.getOpcode()) { 641 case TargetOpcode::G_LOAD: 642 case TargetOpcode::G_STORE: 643 case TargetOpcode::G_PHI: 644 case TargetOpcode::G_SELECT: 645 case TargetOpcode::G_IMPLICIT_DEF: { 646 Helper.narrowScalar(MI, 0, LLT::scalar(32)); 647 // Handle new instructions. 648 while (!NewInstrs.empty()) { 649 MachineInstr *NewMI = NewInstrs.pop_back_val(); 650 // This is new G_UNMERGE that was created during narrowScalar and will 651 // not be considered for regbank selection. RegBankSelect for mips 652 // visits/makes corresponding G_MERGE first. Combine them here. 653 if (NewMI->getOpcode() == TargetOpcode::G_UNMERGE_VALUES) 654 combineAwayG_UNMERGE_VALUES(ArtCombiner, *NewMI); 655 // This G_MERGE will be combined away when its corresponding G_UNMERGE 656 // gets regBankSelected. 657 else if (NewMI->getOpcode() == TargetOpcode::G_MERGE_VALUES) 658 continue; 659 else 660 // Manually set register banks for def operands to 32 bit gprb. 661 setRegBank(*NewMI, MRI); 662 } 663 return; 664 } 665 case TargetOpcode::G_UNMERGE_VALUES: 666 combineAwayG_UNMERGE_VALUES(ArtCombiner, MI); 667 return; 668 default: 669 break; 670 } 671 672 return applyDefaultMapping(OpdMapper); 673 } 674