1 //===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This file implements the targeting of the InstructionSelector class for 11 /// AArch64. 12 /// \todo This should be generated by TableGen. 13 //===----------------------------------------------------------------------===// 14 15 #include "AArch64InstrInfo.h" 16 #include "AArch64MachineFunctionInfo.h" 17 #include "AArch64RegisterBankInfo.h" 18 #include "AArch64RegisterInfo.h" 19 #include "AArch64Subtarget.h" 20 #include "AArch64TargetMachine.h" 21 #include "MCTargetDesc/AArch64AddressingModes.h" 22 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" 23 #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h" 24 #include "llvm/CodeGen/GlobalISel/Utils.h" 25 #include "llvm/CodeGen/MachineBasicBlock.h" 26 #include "llvm/CodeGen/MachineFunction.h" 27 #include "llvm/CodeGen/MachineInstr.h" 28 #include "llvm/CodeGen/MachineInstrBuilder.h" 29 #include "llvm/CodeGen/MachineOperand.h" 30 #include "llvm/CodeGen/MachineRegisterInfo.h" 31 #include "llvm/IR/Type.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/raw_ostream.h" 34 35 #define DEBUG_TYPE "aarch64-isel" 36 37 using namespace llvm; 38 39 namespace { 40 41 #define GET_GLOBALISEL_PREDICATE_BITSET 42 #include "AArch64GenGlobalISel.inc" 43 #undef GET_GLOBALISEL_PREDICATE_BITSET 44 45 class AArch64InstructionSelector : public InstructionSelector { 46 public: 47 AArch64InstructionSelector(const AArch64TargetMachine &TM, 48 const AArch64Subtarget &STI, 49 const AArch64RegisterBankInfo &RBI); 50 51 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override; 52 static const char *getName() { return DEBUG_TYPE; } 53 54 private: 55 /// tblgen-erated 'select' implementation, used as the initial selector for 56 /// the patterns that don't require complex C++. 57 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const; 58 59 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF, 60 MachineRegisterInfo &MRI) const; 61 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF, 62 MachineRegisterInfo &MRI) const; 63 64 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF, 65 MachineRegisterInfo &MRI) const; 66 67 ComplexRendererFns selectArithImmed(MachineOperand &Root) const; 68 69 ComplexRendererFns selectAddrModeUnscaled(MachineOperand &Root, 70 unsigned Size) const; 71 72 ComplexRendererFns selectAddrModeUnscaled8(MachineOperand &Root) const { 73 return selectAddrModeUnscaled(Root, 1); 74 } 75 ComplexRendererFns selectAddrModeUnscaled16(MachineOperand &Root) const { 76 return selectAddrModeUnscaled(Root, 2); 77 } 78 ComplexRendererFns selectAddrModeUnscaled32(MachineOperand &Root) const { 79 return selectAddrModeUnscaled(Root, 4); 80 } 81 ComplexRendererFns selectAddrModeUnscaled64(MachineOperand &Root) const { 82 return selectAddrModeUnscaled(Root, 8); 83 } 84 ComplexRendererFns selectAddrModeUnscaled128(MachineOperand &Root) const { 85 return selectAddrModeUnscaled(Root, 16); 86 } 87 88 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root, 89 unsigned Size) const; 90 template <int Width> 91 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root) const { 92 return selectAddrModeIndexed(Root, Width / 8); 93 } 94 95 const AArch64TargetMachine &TM; 96 const AArch64Subtarget &STI; 97 const AArch64InstrInfo &TII; 98 const AArch64RegisterInfo &TRI; 99 const AArch64RegisterBankInfo &RBI; 100 101 #define GET_GLOBALISEL_PREDICATES_DECL 102 #include "AArch64GenGlobalISel.inc" 103 #undef GET_GLOBALISEL_PREDICATES_DECL 104 105 // We declare the temporaries used by selectImpl() in the class to minimize the 106 // cost of constructing placeholder values. 107 #define GET_GLOBALISEL_TEMPORARIES_DECL 108 #include "AArch64GenGlobalISel.inc" 109 #undef GET_GLOBALISEL_TEMPORARIES_DECL 110 }; 111 112 } // end anonymous namespace 113 114 #define GET_GLOBALISEL_IMPL 115 #include "AArch64GenGlobalISel.inc" 116 #undef GET_GLOBALISEL_IMPL 117 118 AArch64InstructionSelector::AArch64InstructionSelector( 119 const AArch64TargetMachine &TM, const AArch64Subtarget &STI, 120 const AArch64RegisterBankInfo &RBI) 121 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()), 122 TRI(*STI.getRegisterInfo()), RBI(RBI), 123 #define GET_GLOBALISEL_PREDICATES_INIT 124 #include "AArch64GenGlobalISel.inc" 125 #undef GET_GLOBALISEL_PREDICATES_INIT 126 #define GET_GLOBALISEL_TEMPORARIES_INIT 127 #include "AArch64GenGlobalISel.inc" 128 #undef GET_GLOBALISEL_TEMPORARIES_INIT 129 { 130 } 131 132 // FIXME: This should be target-independent, inferred from the types declared 133 // for each class in the bank. 134 static const TargetRegisterClass * 135 getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB, 136 const RegisterBankInfo &RBI) { 137 if (RB.getID() == AArch64::GPRRegBankID) { 138 if (Ty.getSizeInBits() <= 32) 139 return &AArch64::GPR32RegClass; 140 if (Ty.getSizeInBits() == 64) 141 return &AArch64::GPR64RegClass; 142 return nullptr; 143 } 144 145 if (RB.getID() == AArch64::FPRRegBankID) { 146 if (Ty.getSizeInBits() == 32) 147 return &AArch64::FPR32RegClass; 148 if (Ty.getSizeInBits() == 64) 149 return &AArch64::FPR64RegClass; 150 if (Ty.getSizeInBits() == 128) 151 return &AArch64::FPR128RegClass; 152 return nullptr; 153 } 154 155 return nullptr; 156 } 157 158 /// Check whether \p I is a currently unsupported binary operation: 159 /// - it has an unsized type 160 /// - an operand is not a vreg 161 /// - all operands are not in the same bank 162 /// These are checks that should someday live in the verifier, but right now, 163 /// these are mostly limitations of the aarch64 selector. 164 static bool unsupportedBinOp(const MachineInstr &I, 165 const AArch64RegisterBankInfo &RBI, 166 const MachineRegisterInfo &MRI, 167 const AArch64RegisterInfo &TRI) { 168 LLT Ty = MRI.getType(I.getOperand(0).getReg()); 169 if (!Ty.isValid()) { 170 DEBUG(dbgs() << "Generic binop register should be typed\n"); 171 return true; 172 } 173 174 const RegisterBank *PrevOpBank = nullptr; 175 for (auto &MO : I.operands()) { 176 // FIXME: Support non-register operands. 177 if (!MO.isReg()) { 178 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n"); 179 return true; 180 } 181 182 // FIXME: Can generic operations have physical registers operands? If 183 // so, this will need to be taught about that, and we'll need to get the 184 // bank out of the minimal class for the register. 185 // Either way, this needs to be documented (and possibly verified). 186 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) { 187 DEBUG(dbgs() << "Generic inst has physical register operand\n"); 188 return true; 189 } 190 191 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI); 192 if (!OpBank) { 193 DEBUG(dbgs() << "Generic register has no bank or class\n"); 194 return true; 195 } 196 197 if (PrevOpBank && OpBank != PrevOpBank) { 198 DEBUG(dbgs() << "Generic inst operands have different banks\n"); 199 return true; 200 } 201 PrevOpBank = OpBank; 202 } 203 return false; 204 } 205 206 /// Select the AArch64 opcode for the basic binary operation \p GenericOpc 207 /// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID 208 /// and of size \p OpSize. 209 /// \returns \p GenericOpc if the combination is unsupported. 210 static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID, 211 unsigned OpSize) { 212 switch (RegBankID) { 213 case AArch64::GPRRegBankID: 214 if (OpSize == 32) { 215 switch (GenericOpc) { 216 case TargetOpcode::G_SHL: 217 return AArch64::LSLVWr; 218 case TargetOpcode::G_LSHR: 219 return AArch64::LSRVWr; 220 case TargetOpcode::G_ASHR: 221 return AArch64::ASRVWr; 222 default: 223 return GenericOpc; 224 } 225 } else if (OpSize == 64) { 226 switch (GenericOpc) { 227 case TargetOpcode::G_GEP: 228 return AArch64::ADDXrr; 229 case TargetOpcode::G_SHL: 230 return AArch64::LSLVXr; 231 case TargetOpcode::G_LSHR: 232 return AArch64::LSRVXr; 233 case TargetOpcode::G_ASHR: 234 return AArch64::ASRVXr; 235 default: 236 return GenericOpc; 237 } 238 } 239 break; 240 case AArch64::FPRRegBankID: 241 switch (OpSize) { 242 case 32: 243 switch (GenericOpc) { 244 case TargetOpcode::G_FADD: 245 return AArch64::FADDSrr; 246 case TargetOpcode::G_FSUB: 247 return AArch64::FSUBSrr; 248 case TargetOpcode::G_FMUL: 249 return AArch64::FMULSrr; 250 case TargetOpcode::G_FDIV: 251 return AArch64::FDIVSrr; 252 default: 253 return GenericOpc; 254 } 255 case 64: 256 switch (GenericOpc) { 257 case TargetOpcode::G_FADD: 258 return AArch64::FADDDrr; 259 case TargetOpcode::G_FSUB: 260 return AArch64::FSUBDrr; 261 case TargetOpcode::G_FMUL: 262 return AArch64::FMULDrr; 263 case TargetOpcode::G_FDIV: 264 return AArch64::FDIVDrr; 265 case TargetOpcode::G_OR: 266 return AArch64::ORRv8i8; 267 default: 268 return GenericOpc; 269 } 270 } 271 break; 272 } 273 return GenericOpc; 274 } 275 276 /// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc, 277 /// appropriate for the (value) register bank \p RegBankID and of memory access 278 /// size \p OpSize. This returns the variant with the base+unsigned-immediate 279 /// addressing mode (e.g., LDRXui). 280 /// \returns \p GenericOpc if the combination is unsupported. 281 static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID, 282 unsigned OpSize) { 283 const bool isStore = GenericOpc == TargetOpcode::G_STORE; 284 switch (RegBankID) { 285 case AArch64::GPRRegBankID: 286 switch (OpSize) { 287 case 8: 288 return isStore ? AArch64::STRBBui : AArch64::LDRBBui; 289 case 16: 290 return isStore ? AArch64::STRHHui : AArch64::LDRHHui; 291 case 32: 292 return isStore ? AArch64::STRWui : AArch64::LDRWui; 293 case 64: 294 return isStore ? AArch64::STRXui : AArch64::LDRXui; 295 } 296 break; 297 case AArch64::FPRRegBankID: 298 switch (OpSize) { 299 case 8: 300 return isStore ? AArch64::STRBui : AArch64::LDRBui; 301 case 16: 302 return isStore ? AArch64::STRHui : AArch64::LDRHui; 303 case 32: 304 return isStore ? AArch64::STRSui : AArch64::LDRSui; 305 case 64: 306 return isStore ? AArch64::STRDui : AArch64::LDRDui; 307 } 308 break; 309 } 310 return GenericOpc; 311 } 312 313 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, 314 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 315 const RegisterBankInfo &RBI) { 316 317 unsigned DstReg = I.getOperand(0).getReg(); 318 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { 319 assert(I.isCopy() && "Generic operators do not allow physical registers"); 320 return true; 321 } 322 323 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI); 324 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); 325 unsigned SrcReg = I.getOperand(1).getReg(); 326 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI); 327 (void)SrcSize; 328 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) && 329 "No phys reg on generic operators"); 330 assert( 331 (DstSize == SrcSize || 332 // Copies are a mean to setup initial types, the number of 333 // bits may not exactly match. 334 (TargetRegisterInfo::isPhysicalRegister(SrcReg) && 335 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) || 336 // Copies are a mean to copy bits around, as long as we are 337 // on the same register class, that's fine. Otherwise, that 338 // means we need some SUBREG_TO_REG or AND & co. 339 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) && 340 "Copy with different width?!"); 341 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) && 342 "GPRs cannot get more than 64-bit width values"); 343 const TargetRegisterClass *RC = nullptr; 344 345 if (RegBank.getID() == AArch64::FPRRegBankID) { 346 if (DstSize <= 16) 347 RC = &AArch64::FPR16RegClass; 348 else if (DstSize <= 32) 349 RC = &AArch64::FPR32RegClass; 350 else if (DstSize <= 64) 351 RC = &AArch64::FPR64RegClass; 352 else if (DstSize <= 128) 353 RC = &AArch64::FPR128RegClass; 354 else { 355 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n'); 356 return false; 357 } 358 } else { 359 assert(RegBank.getID() == AArch64::GPRRegBankID && 360 "Bitcast for the flags?"); 361 RC = 362 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass; 363 } 364 365 // No need to constrain SrcReg. It will get constrained when 366 // we hit another of its use or its defs. 367 // Copies do not have constraints. 368 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { 369 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) 370 << " operand\n"); 371 return false; 372 } 373 I.setDesc(TII.get(AArch64::COPY)); 374 return true; 375 } 376 377 static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) { 378 if (!DstTy.isScalar() || !SrcTy.isScalar()) 379 return GenericOpc; 380 381 const unsigned DstSize = DstTy.getSizeInBits(); 382 const unsigned SrcSize = SrcTy.getSizeInBits(); 383 384 switch (DstSize) { 385 case 32: 386 switch (SrcSize) { 387 case 32: 388 switch (GenericOpc) { 389 case TargetOpcode::G_SITOFP: 390 return AArch64::SCVTFUWSri; 391 case TargetOpcode::G_UITOFP: 392 return AArch64::UCVTFUWSri; 393 case TargetOpcode::G_FPTOSI: 394 return AArch64::FCVTZSUWSr; 395 case TargetOpcode::G_FPTOUI: 396 return AArch64::FCVTZUUWSr; 397 default: 398 return GenericOpc; 399 } 400 case 64: 401 switch (GenericOpc) { 402 case TargetOpcode::G_SITOFP: 403 return AArch64::SCVTFUXSri; 404 case TargetOpcode::G_UITOFP: 405 return AArch64::UCVTFUXSri; 406 case TargetOpcode::G_FPTOSI: 407 return AArch64::FCVTZSUWDr; 408 case TargetOpcode::G_FPTOUI: 409 return AArch64::FCVTZUUWDr; 410 default: 411 return GenericOpc; 412 } 413 default: 414 return GenericOpc; 415 } 416 case 64: 417 switch (SrcSize) { 418 case 32: 419 switch (GenericOpc) { 420 case TargetOpcode::G_SITOFP: 421 return AArch64::SCVTFUWDri; 422 case TargetOpcode::G_UITOFP: 423 return AArch64::UCVTFUWDri; 424 case TargetOpcode::G_FPTOSI: 425 return AArch64::FCVTZSUXSr; 426 case TargetOpcode::G_FPTOUI: 427 return AArch64::FCVTZUUXSr; 428 default: 429 return GenericOpc; 430 } 431 case 64: 432 switch (GenericOpc) { 433 case TargetOpcode::G_SITOFP: 434 return AArch64::SCVTFUXDri; 435 case TargetOpcode::G_UITOFP: 436 return AArch64::UCVTFUXDri; 437 case TargetOpcode::G_FPTOSI: 438 return AArch64::FCVTZSUXDr; 439 case TargetOpcode::G_FPTOUI: 440 return AArch64::FCVTZUUXDr; 441 default: 442 return GenericOpc; 443 } 444 default: 445 return GenericOpc; 446 } 447 default: 448 return GenericOpc; 449 }; 450 return GenericOpc; 451 } 452 453 static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) { 454 switch (P) { 455 default: 456 llvm_unreachable("Unknown condition code!"); 457 case CmpInst::ICMP_NE: 458 return AArch64CC::NE; 459 case CmpInst::ICMP_EQ: 460 return AArch64CC::EQ; 461 case CmpInst::ICMP_SGT: 462 return AArch64CC::GT; 463 case CmpInst::ICMP_SGE: 464 return AArch64CC::GE; 465 case CmpInst::ICMP_SLT: 466 return AArch64CC::LT; 467 case CmpInst::ICMP_SLE: 468 return AArch64CC::LE; 469 case CmpInst::ICMP_UGT: 470 return AArch64CC::HI; 471 case CmpInst::ICMP_UGE: 472 return AArch64CC::HS; 473 case CmpInst::ICMP_ULT: 474 return AArch64CC::LO; 475 case CmpInst::ICMP_ULE: 476 return AArch64CC::LS; 477 } 478 } 479 480 static void changeFCMPPredToAArch64CC(CmpInst::Predicate P, 481 AArch64CC::CondCode &CondCode, 482 AArch64CC::CondCode &CondCode2) { 483 CondCode2 = AArch64CC::AL; 484 switch (P) { 485 default: 486 llvm_unreachable("Unknown FP condition!"); 487 case CmpInst::FCMP_OEQ: 488 CondCode = AArch64CC::EQ; 489 break; 490 case CmpInst::FCMP_OGT: 491 CondCode = AArch64CC::GT; 492 break; 493 case CmpInst::FCMP_OGE: 494 CondCode = AArch64CC::GE; 495 break; 496 case CmpInst::FCMP_OLT: 497 CondCode = AArch64CC::MI; 498 break; 499 case CmpInst::FCMP_OLE: 500 CondCode = AArch64CC::LS; 501 break; 502 case CmpInst::FCMP_ONE: 503 CondCode = AArch64CC::MI; 504 CondCode2 = AArch64CC::GT; 505 break; 506 case CmpInst::FCMP_ORD: 507 CondCode = AArch64CC::VC; 508 break; 509 case CmpInst::FCMP_UNO: 510 CondCode = AArch64CC::VS; 511 break; 512 case CmpInst::FCMP_UEQ: 513 CondCode = AArch64CC::EQ; 514 CondCode2 = AArch64CC::VS; 515 break; 516 case CmpInst::FCMP_UGT: 517 CondCode = AArch64CC::HI; 518 break; 519 case CmpInst::FCMP_UGE: 520 CondCode = AArch64CC::PL; 521 break; 522 case CmpInst::FCMP_ULT: 523 CondCode = AArch64CC::LT; 524 break; 525 case CmpInst::FCMP_ULE: 526 CondCode = AArch64CC::LE; 527 break; 528 case CmpInst::FCMP_UNE: 529 CondCode = AArch64CC::NE; 530 break; 531 } 532 } 533 534 bool AArch64InstructionSelector::selectCompareBranch( 535 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const { 536 537 const unsigned CondReg = I.getOperand(0).getReg(); 538 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB(); 539 MachineInstr *CCMI = MRI.getVRegDef(CondReg); 540 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC) 541 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg()); 542 if (CCMI->getOpcode() != TargetOpcode::G_ICMP) 543 return false; 544 545 unsigned LHS = CCMI->getOperand(2).getReg(); 546 unsigned RHS = CCMI->getOperand(3).getReg(); 547 if (!getConstantVRegVal(RHS, MRI)) 548 std::swap(RHS, LHS); 549 550 const auto RHSImm = getConstantVRegVal(RHS, MRI); 551 if (!RHSImm || *RHSImm != 0) 552 return false; 553 554 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI); 555 if (RB.getID() != AArch64::GPRRegBankID) 556 return false; 557 558 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate(); 559 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ) 560 return false; 561 562 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits(); 563 unsigned CBOpc = 0; 564 if (CmpWidth <= 32) 565 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW); 566 else if (CmpWidth == 64) 567 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX); 568 else 569 return false; 570 571 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc)) 572 .addUse(LHS) 573 .addMBB(DestMBB); 574 575 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI); 576 I.eraseFromParent(); 577 return true; 578 } 579 580 bool AArch64InstructionSelector::selectVaStartAAPCS( 581 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const { 582 return false; 583 } 584 585 bool AArch64InstructionSelector::selectVaStartDarwin( 586 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const { 587 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 588 unsigned ListReg = I.getOperand(0).getReg(); 589 590 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass); 591 592 auto MIB = 593 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri)) 594 .addDef(ArgsAddrReg) 595 .addFrameIndex(FuncInfo->getVarArgsStackIndex()) 596 .addImm(0) 597 .addImm(0); 598 599 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); 600 601 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui)) 602 .addUse(ArgsAddrReg) 603 .addUse(ListReg) 604 .addImm(0) 605 .addMemOperand(*I.memoperands_begin()); 606 607 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); 608 I.eraseFromParent(); 609 return true; 610 } 611 612 bool AArch64InstructionSelector::select(MachineInstr &I, 613 CodeGenCoverage &CoverageInfo) const { 614 assert(I.getParent() && "Instruction should be in a basic block!"); 615 assert(I.getParent()->getParent() && "Instruction should be in a function!"); 616 617 MachineBasicBlock &MBB = *I.getParent(); 618 MachineFunction &MF = *MBB.getParent(); 619 MachineRegisterInfo &MRI = MF.getRegInfo(); 620 621 unsigned Opcode = I.getOpcode(); 622 // G_PHI requires same handling as PHI 623 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) { 624 // Certain non-generic instructions also need some special handling. 625 626 if (Opcode == TargetOpcode::LOAD_STACK_GUARD) 627 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 628 629 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) { 630 const unsigned DefReg = I.getOperand(0).getReg(); 631 const LLT DefTy = MRI.getType(DefReg); 632 633 const TargetRegisterClass *DefRC = nullptr; 634 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) { 635 DefRC = TRI.getRegClass(DefReg); 636 } else { 637 const RegClassOrRegBank &RegClassOrBank = 638 MRI.getRegClassOrRegBank(DefReg); 639 640 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>(); 641 if (!DefRC) { 642 if (!DefTy.isValid()) { 643 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n"); 644 return false; 645 } 646 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>(); 647 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI); 648 if (!DefRC) { 649 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n"); 650 return false; 651 } 652 } 653 } 654 I.setDesc(TII.get(TargetOpcode::PHI)); 655 656 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI); 657 } 658 659 if (I.isCopy()) 660 return selectCopy(I, TII, MRI, TRI, RBI); 661 662 return true; 663 } 664 665 666 if (I.getNumOperands() != I.getNumExplicitOperands()) { 667 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n"); 668 return false; 669 } 670 671 if (selectImpl(I, CoverageInfo)) 672 return true; 673 674 LLT Ty = 675 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{}; 676 677 switch (Opcode) { 678 case TargetOpcode::G_BRCOND: { 679 if (Ty.getSizeInBits() > 32) { 680 // We shouldn't need this on AArch64, but it would be implemented as an 681 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the 682 // bit being tested is < 32. 683 DEBUG(dbgs() << "G_BRCOND has type: " << Ty 684 << ", expected at most 32-bits"); 685 return false; 686 } 687 688 const unsigned CondReg = I.getOperand(0).getReg(); 689 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB(); 690 691 if (selectCompareBranch(I, MF, MRI)) 692 return true; 693 694 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW)) 695 .addUse(CondReg) 696 .addImm(/*bit offset=*/0) 697 .addMBB(DestMBB); 698 699 I.eraseFromParent(); 700 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI); 701 } 702 703 case TargetOpcode::G_BRINDIRECT: { 704 I.setDesc(TII.get(AArch64::BR)); 705 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 706 } 707 708 case TargetOpcode::G_FCONSTANT: 709 case TargetOpcode::G_CONSTANT: { 710 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT; 711 712 const LLT s32 = LLT::scalar(32); 713 const LLT s64 = LLT::scalar(64); 714 const LLT p0 = LLT::pointer(0, 64); 715 716 const unsigned DefReg = I.getOperand(0).getReg(); 717 const LLT DefTy = MRI.getType(DefReg); 718 const unsigned DefSize = DefTy.getSizeInBits(); 719 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); 720 721 // FIXME: Redundant check, but even less readable when factored out. 722 if (isFP) { 723 if (Ty != s32 && Ty != s64) { 724 DEBUG(dbgs() << "Unable to materialize FP " << Ty 725 << " constant, expected: " << s32 << " or " << s64 726 << '\n'); 727 return false; 728 } 729 730 if (RB.getID() != AArch64::FPRRegBankID) { 731 DEBUG(dbgs() << "Unable to materialize FP " << Ty 732 << " constant on bank: " << RB << ", expected: FPR\n"); 733 return false; 734 } 735 736 // The case when we have 0.0 is covered by tablegen. Reject it here so we 737 // can be sure tablegen works correctly and isn't rescued by this code. 738 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0)) 739 return false; 740 } else { 741 // s32 and s64 are covered by tablegen. 742 if (Ty != p0) { 743 DEBUG(dbgs() << "Unable to materialize integer " << Ty 744 << " constant, expected: " << s32 << ", " << s64 << ", or " 745 << p0 << '\n'); 746 return false; 747 } 748 749 if (RB.getID() != AArch64::GPRRegBankID) { 750 DEBUG(dbgs() << "Unable to materialize integer " << Ty 751 << " constant on bank: " << RB << ", expected: GPR\n"); 752 return false; 753 } 754 } 755 756 const unsigned MovOpc = 757 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm; 758 759 I.setDesc(TII.get(MovOpc)); 760 761 if (isFP) { 762 const TargetRegisterClass &GPRRC = 763 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass; 764 const TargetRegisterClass &FPRRC = 765 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass; 766 767 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC); 768 MachineOperand &RegOp = I.getOperand(0); 769 RegOp.setReg(DefGPRReg); 770 771 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(), 772 TII.get(AArch64::COPY)) 773 .addDef(DefReg) 774 .addUse(DefGPRReg); 775 776 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) { 777 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n"); 778 return false; 779 } 780 781 MachineOperand &ImmOp = I.getOperand(1); 782 // FIXME: Is going through int64_t always correct? 783 ImmOp.ChangeToImmediate( 784 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue()); 785 } else if (I.getOperand(1).isCImm()) { 786 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue(); 787 I.getOperand(1).ChangeToImmediate(Val); 788 } else if (I.getOperand(1).isImm()) { 789 uint64_t Val = I.getOperand(1).getImm(); 790 I.getOperand(1).ChangeToImmediate(Val); 791 } 792 793 constrainSelectedInstRegOperands(I, TII, TRI, RBI); 794 return true; 795 } 796 case TargetOpcode::G_EXTRACT: { 797 LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); 798 // Larger extracts are vectors, same-size extracts should be something else 799 // by now (either split up or simplified to a COPY). 800 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32) 801 return false; 802 803 I.setDesc(TII.get(AArch64::UBFMXri)); 804 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() + 805 Ty.getSizeInBits() - 1); 806 807 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64)); 808 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(), 809 TII.get(AArch64::COPY)) 810 .addDef(I.getOperand(0).getReg()) 811 .addUse(DstReg, 0, AArch64::sub_32); 812 RBI.constrainGenericRegister(I.getOperand(0).getReg(), 813 AArch64::GPR32RegClass, MRI); 814 I.getOperand(0).setReg(DstReg); 815 816 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 817 } 818 819 case TargetOpcode::G_INSERT: { 820 LLT SrcTy = MRI.getType(I.getOperand(2).getReg()); 821 // Larger inserts are vectors, same-size ones should be something else by 822 // now (split up or turned into COPYs). 823 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32) 824 return false; 825 826 I.setDesc(TII.get(AArch64::BFMXri)); 827 unsigned LSB = I.getOperand(3).getImm(); 828 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits(); 829 I.getOperand(3).setImm((64 - LSB) % 64); 830 MachineInstrBuilder(MF, I).addImm(Width - 1); 831 832 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64)); 833 BuildMI(MBB, I.getIterator(), I.getDebugLoc(), 834 TII.get(AArch64::SUBREG_TO_REG)) 835 .addDef(SrcReg) 836 .addImm(0) 837 .addUse(I.getOperand(2).getReg()) 838 .addImm(AArch64::sub_32); 839 RBI.constrainGenericRegister(I.getOperand(2).getReg(), 840 AArch64::GPR32RegClass, MRI); 841 I.getOperand(2).setReg(SrcReg); 842 843 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 844 } 845 case TargetOpcode::G_FRAME_INDEX: { 846 // allocas and G_FRAME_INDEX are only supported in addrspace(0). 847 if (Ty != LLT::pointer(0, 64)) { 848 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty 849 << ", expected: " << LLT::pointer(0, 64) << '\n'); 850 return false; 851 } 852 I.setDesc(TII.get(AArch64::ADDXri)); 853 854 // MOs for a #0 shifted immediate. 855 I.addOperand(MachineOperand::CreateImm(0)); 856 I.addOperand(MachineOperand::CreateImm(0)); 857 858 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 859 } 860 861 case TargetOpcode::G_GLOBAL_VALUE: { 862 auto GV = I.getOperand(1).getGlobal(); 863 if (GV->isThreadLocal()) { 864 // FIXME: we don't support TLS yet. 865 return false; 866 } 867 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM); 868 if (OpFlags & AArch64II::MO_GOT) { 869 I.setDesc(TII.get(AArch64::LOADgot)); 870 I.getOperand(1).setTargetFlags(OpFlags); 871 } else if (TM.getCodeModel() == CodeModel::Large) { 872 // Materialize the global using movz/movk instructions. 873 unsigned MovZDstReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass); 874 auto InsertPt = std::next(I.getIterator()); 875 auto MovZ = 876 BuildMI(MBB, InsertPt, I.getDebugLoc(), TII.get(AArch64::MOVZXi)) 877 .addDef(MovZDstReg); 878 MovZ->addOperand(MF, I.getOperand(1)); 879 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 | 880 AArch64II::MO_NC); 881 MovZ->addOperand(MF, MachineOperand::CreateImm(0)); 882 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI); 883 884 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags, 885 unsigned Offset, unsigned ForceDstReg) { 886 unsigned DstReg = 887 ForceDstReg ? ForceDstReg 888 : MRI.createVirtualRegister(&AArch64::GPR64RegClass); 889 auto MovI = BuildMI(MBB, InsertPt, MovZ->getDebugLoc(), 890 TII.get(AArch64::MOVKXi)) 891 .addDef(DstReg) 892 .addReg(SrcReg); 893 MovI->addOperand(MF, MachineOperand::CreateGA( 894 GV, MovZ->getOperand(1).getOffset(), Flags)); 895 MovI->addOperand(MF, MachineOperand::CreateImm(Offset)); 896 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI); 897 return DstReg; 898 }; 899 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(), 900 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0); 901 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0); 902 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg()); 903 I.eraseFromParent(); 904 return true; 905 } else { 906 I.setDesc(TII.get(AArch64::MOVaddr)); 907 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE); 908 MachineInstrBuilder MIB(MF, I); 909 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(), 910 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC); 911 } 912 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 913 } 914 915 case TargetOpcode::G_LOAD: 916 case TargetOpcode::G_STORE: { 917 LLT MemTy = Ty; 918 LLT PtrTy = MRI.getType(I.getOperand(1).getReg()); 919 920 if (PtrTy != LLT::pointer(0, 64)) { 921 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy 922 << ", expected: " << LLT::pointer(0, 64) << '\n'); 923 return false; 924 } 925 926 auto &MemOp = **I.memoperands_begin(); 927 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { 928 DEBUG(dbgs() << "Atomic load/store not supported yet\n"); 929 return false; 930 } 931 932 // FIXME: PR36018: Volatile loads in some cases are incorrectly selected by 933 // folding with an extend. Until we have a G_SEXTLOAD solution bail out if 934 // we hit one. 935 if (Opcode == TargetOpcode::G_LOAD && MemOp.isVolatile()) 936 return false; 937 938 const unsigned PtrReg = I.getOperand(1).getReg(); 939 #ifndef NDEBUG 940 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI); 941 // Sanity-check the pointer register. 942 assert(PtrRB.getID() == AArch64::GPRRegBankID && 943 "Load/Store pointer operand isn't a GPR"); 944 assert(MRI.getType(PtrReg).isPointer() && 945 "Load/Store pointer operand isn't a pointer"); 946 #endif 947 948 const unsigned ValReg = I.getOperand(0).getReg(); 949 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI); 950 951 const unsigned NewOpc = 952 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits()); 953 if (NewOpc == I.getOpcode()) 954 return false; 955 956 I.setDesc(TII.get(NewOpc)); 957 958 uint64_t Offset = 0; 959 auto *PtrMI = MRI.getVRegDef(PtrReg); 960 961 // Try to fold a GEP into our unsigned immediate addressing mode. 962 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) { 963 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) { 964 int64_t Imm = *COff; 965 const unsigned Size = MemTy.getSizeInBits() / 8; 966 const unsigned Scale = Log2_32(Size); 967 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) { 968 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg(); 969 I.getOperand(1).setReg(Ptr2Reg); 970 PtrMI = MRI.getVRegDef(Ptr2Reg); 971 Offset = Imm / Size; 972 } 973 } 974 } 975 976 // If we haven't folded anything into our addressing mode yet, try to fold 977 // a frame index into the base+offset. 978 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX) 979 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex()); 980 981 I.addOperand(MachineOperand::CreateImm(Offset)); 982 983 // If we're storing a 0, use WZR/XZR. 984 if (auto CVal = getConstantVRegVal(ValReg, MRI)) { 985 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) { 986 if (I.getOpcode() == AArch64::STRWui) 987 I.getOperand(0).setReg(AArch64::WZR); 988 else if (I.getOpcode() == AArch64::STRXui) 989 I.getOperand(0).setReg(AArch64::XZR); 990 } 991 } 992 993 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 994 } 995 996 case TargetOpcode::G_SMULH: 997 case TargetOpcode::G_UMULH: { 998 // Reject the various things we don't support yet. 999 if (unsupportedBinOp(I, RBI, MRI, TRI)) 1000 return false; 1001 1002 const unsigned DefReg = I.getOperand(0).getReg(); 1003 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); 1004 1005 if (RB.getID() != AArch64::GPRRegBankID) { 1006 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n"); 1007 return false; 1008 } 1009 1010 if (Ty != LLT::scalar(64)) { 1011 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty 1012 << ", expected: " << LLT::scalar(64) << '\n'); 1013 return false; 1014 } 1015 1016 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr 1017 : AArch64::UMULHrr; 1018 I.setDesc(TII.get(NewOpc)); 1019 1020 // Now that we selected an opcode, we need to constrain the register 1021 // operands to use appropriate classes. 1022 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 1023 } 1024 case TargetOpcode::G_FADD: 1025 case TargetOpcode::G_FSUB: 1026 case TargetOpcode::G_FMUL: 1027 case TargetOpcode::G_FDIV: 1028 1029 case TargetOpcode::G_OR: 1030 case TargetOpcode::G_SHL: 1031 case TargetOpcode::G_LSHR: 1032 case TargetOpcode::G_ASHR: 1033 case TargetOpcode::G_GEP: { 1034 // Reject the various things we don't support yet. 1035 if (unsupportedBinOp(I, RBI, MRI, TRI)) 1036 return false; 1037 1038 const unsigned OpSize = Ty.getSizeInBits(); 1039 1040 const unsigned DefReg = I.getOperand(0).getReg(); 1041 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); 1042 1043 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize); 1044 if (NewOpc == I.getOpcode()) 1045 return false; 1046 1047 I.setDesc(TII.get(NewOpc)); 1048 // FIXME: Should the type be always reset in setDesc? 1049 1050 // Now that we selected an opcode, we need to constrain the register 1051 // operands to use appropriate classes. 1052 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 1053 } 1054 1055 case TargetOpcode::G_PTR_MASK: { 1056 uint64_t Align = I.getOperand(2).getImm(); 1057 if (Align >= 64 || Align == 0) 1058 return false; 1059 1060 uint64_t Mask = ~((1ULL << Align) - 1); 1061 I.setDesc(TII.get(AArch64::ANDXri)); 1062 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64)); 1063 1064 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 1065 } 1066 case TargetOpcode::G_PTRTOINT: 1067 case TargetOpcode::G_TRUNC: { 1068 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()); 1069 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); 1070 1071 const unsigned DstReg = I.getOperand(0).getReg(); 1072 const unsigned SrcReg = I.getOperand(1).getReg(); 1073 1074 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI); 1075 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI); 1076 1077 if (DstRB.getID() != SrcRB.getID()) { 1078 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n"); 1079 return false; 1080 } 1081 1082 if (DstRB.getID() == AArch64::GPRRegBankID) { 1083 const TargetRegisterClass *DstRC = 1084 getRegClassForTypeOnBank(DstTy, DstRB, RBI); 1085 if (!DstRC) 1086 return false; 1087 1088 const TargetRegisterClass *SrcRC = 1089 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI); 1090 if (!SrcRC) 1091 return false; 1092 1093 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) || 1094 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) { 1095 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n"); 1096 return false; 1097 } 1098 1099 if (DstRC == SrcRC) { 1100 // Nothing to be done 1101 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) && 1102 SrcTy == LLT::scalar(64)) { 1103 llvm_unreachable("TableGen can import this case"); 1104 return false; 1105 } else if (DstRC == &AArch64::GPR32RegClass && 1106 SrcRC == &AArch64::GPR64RegClass) { 1107 I.getOperand(1).setSubReg(AArch64::sub_32); 1108 } else { 1109 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n"); 1110 return false; 1111 } 1112 1113 I.setDesc(TII.get(TargetOpcode::COPY)); 1114 return true; 1115 } else if (DstRB.getID() == AArch64::FPRRegBankID) { 1116 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) { 1117 I.setDesc(TII.get(AArch64::XTNv4i16)); 1118 constrainSelectedInstRegOperands(I, TII, TRI, RBI); 1119 return true; 1120 } 1121 } 1122 1123 return false; 1124 } 1125 1126 case TargetOpcode::G_ANYEXT: { 1127 const unsigned DstReg = I.getOperand(0).getReg(); 1128 const unsigned SrcReg = I.getOperand(1).getReg(); 1129 1130 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI); 1131 if (RBDst.getID() != AArch64::GPRRegBankID) { 1132 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n"); 1133 return false; 1134 } 1135 1136 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI); 1137 if (RBSrc.getID() != AArch64::GPRRegBankID) { 1138 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n"); 1139 return false; 1140 } 1141 1142 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); 1143 1144 if (DstSize == 0) { 1145 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n"); 1146 return false; 1147 } 1148 1149 if (DstSize != 64 && DstSize > 32) { 1150 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize 1151 << ", expected: 32 or 64\n"); 1152 return false; 1153 } 1154 // At this point G_ANYEXT is just like a plain COPY, but we need 1155 // to explicitly form the 64-bit value if any. 1156 if (DstSize > 32) { 1157 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass); 1158 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG)) 1159 .addDef(ExtSrc) 1160 .addImm(0) 1161 .addUse(SrcReg) 1162 .addImm(AArch64::sub_32); 1163 I.getOperand(1).setReg(ExtSrc); 1164 } 1165 return selectCopy(I, TII, MRI, TRI, RBI); 1166 } 1167 1168 case TargetOpcode::G_ZEXT: 1169 case TargetOpcode::G_SEXT: { 1170 unsigned Opcode = I.getOpcode(); 1171 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()), 1172 SrcTy = MRI.getType(I.getOperand(1).getReg()); 1173 const bool isSigned = Opcode == TargetOpcode::G_SEXT; 1174 const unsigned DefReg = I.getOperand(0).getReg(); 1175 const unsigned SrcReg = I.getOperand(1).getReg(); 1176 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); 1177 1178 if (RB.getID() != AArch64::GPRRegBankID) { 1179 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB 1180 << ", expected: GPR\n"); 1181 return false; 1182 } 1183 1184 MachineInstr *ExtI; 1185 if (DstTy == LLT::scalar(64)) { 1186 // FIXME: Can we avoid manually doing this? 1187 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) { 1188 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode) 1189 << " operand\n"); 1190 return false; 1191 } 1192 1193 const unsigned SrcXReg = 1194 MRI.createVirtualRegister(&AArch64::GPR64RegClass); 1195 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG)) 1196 .addDef(SrcXReg) 1197 .addImm(0) 1198 .addUse(SrcReg) 1199 .addImm(AArch64::sub_32); 1200 1201 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri; 1202 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc)) 1203 .addDef(DefReg) 1204 .addUse(SrcXReg) 1205 .addImm(0) 1206 .addImm(SrcTy.getSizeInBits() - 1); 1207 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) { 1208 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri; 1209 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc)) 1210 .addDef(DefReg) 1211 .addUse(SrcReg) 1212 .addImm(0) 1213 .addImm(SrcTy.getSizeInBits() - 1); 1214 } else { 1215 return false; 1216 } 1217 1218 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI); 1219 1220 I.eraseFromParent(); 1221 return true; 1222 } 1223 1224 case TargetOpcode::G_SITOFP: 1225 case TargetOpcode::G_UITOFP: 1226 case TargetOpcode::G_FPTOSI: 1227 case TargetOpcode::G_FPTOUI: { 1228 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()), 1229 SrcTy = MRI.getType(I.getOperand(1).getReg()); 1230 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy); 1231 if (NewOpc == Opcode) 1232 return false; 1233 1234 I.setDesc(TII.get(NewOpc)); 1235 constrainSelectedInstRegOperands(I, TII, TRI, RBI); 1236 1237 return true; 1238 } 1239 1240 1241 case TargetOpcode::G_INTTOPTR: 1242 // The importer is currently unable to import pointer types since they 1243 // didn't exist in SelectionDAG. 1244 return selectCopy(I, TII, MRI, TRI, RBI); 1245 1246 case TargetOpcode::G_BITCAST: 1247 // Imported SelectionDAG rules can handle every bitcast except those that 1248 // bitcast from a type to the same type. Ideally, these shouldn't occur 1249 // but we might not run an optimizer that deletes them. 1250 if (MRI.getType(I.getOperand(0).getReg()) == 1251 MRI.getType(I.getOperand(1).getReg())) 1252 return selectCopy(I, TII, MRI, TRI, RBI); 1253 return false; 1254 1255 case TargetOpcode::G_SELECT: { 1256 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) { 1257 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty 1258 << ", expected: " << LLT::scalar(1) << '\n'); 1259 return false; 1260 } 1261 1262 const unsigned CondReg = I.getOperand(1).getReg(); 1263 const unsigned TReg = I.getOperand(2).getReg(); 1264 const unsigned FReg = I.getOperand(3).getReg(); 1265 1266 unsigned CSelOpc = 0; 1267 1268 if (Ty == LLT::scalar(32)) { 1269 CSelOpc = AArch64::CSELWr; 1270 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) { 1271 CSelOpc = AArch64::CSELXr; 1272 } else { 1273 return false; 1274 } 1275 1276 MachineInstr &TstMI = 1277 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri)) 1278 .addDef(AArch64::WZR) 1279 .addUse(CondReg) 1280 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32)); 1281 1282 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc)) 1283 .addDef(I.getOperand(0).getReg()) 1284 .addUse(TReg) 1285 .addUse(FReg) 1286 .addImm(AArch64CC::NE); 1287 1288 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI); 1289 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI); 1290 1291 I.eraseFromParent(); 1292 return true; 1293 } 1294 case TargetOpcode::G_ICMP: { 1295 if (Ty != LLT::scalar(32)) { 1296 DEBUG(dbgs() << "G_ICMP result has type: " << Ty 1297 << ", expected: " << LLT::scalar(32) << '\n'); 1298 return false; 1299 } 1300 1301 unsigned CmpOpc = 0; 1302 unsigned ZReg = 0; 1303 1304 LLT CmpTy = MRI.getType(I.getOperand(2).getReg()); 1305 if (CmpTy == LLT::scalar(32)) { 1306 CmpOpc = AArch64::SUBSWrr; 1307 ZReg = AArch64::WZR; 1308 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) { 1309 CmpOpc = AArch64::SUBSXrr; 1310 ZReg = AArch64::XZR; 1311 } else { 1312 return false; 1313 } 1314 1315 // CSINC increments the result by one when the condition code is false. 1316 // Therefore, we have to invert the predicate to get an increment by 1 when 1317 // the predicate is true. 1318 const AArch64CC::CondCode invCC = 1319 changeICMPPredToAArch64CC(CmpInst::getInversePredicate( 1320 (CmpInst::Predicate)I.getOperand(1).getPredicate())); 1321 1322 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc)) 1323 .addDef(ZReg) 1324 .addUse(I.getOperand(2).getReg()) 1325 .addUse(I.getOperand(3).getReg()); 1326 1327 MachineInstr &CSetMI = 1328 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr)) 1329 .addDef(I.getOperand(0).getReg()) 1330 .addUse(AArch64::WZR) 1331 .addUse(AArch64::WZR) 1332 .addImm(invCC); 1333 1334 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI); 1335 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI); 1336 1337 I.eraseFromParent(); 1338 return true; 1339 } 1340 1341 case TargetOpcode::G_FCMP: { 1342 if (Ty != LLT::scalar(32)) { 1343 DEBUG(dbgs() << "G_FCMP result has type: " << Ty 1344 << ", expected: " << LLT::scalar(32) << '\n'); 1345 return false; 1346 } 1347 1348 unsigned CmpOpc = 0; 1349 LLT CmpTy = MRI.getType(I.getOperand(2).getReg()); 1350 if (CmpTy == LLT::scalar(32)) { 1351 CmpOpc = AArch64::FCMPSrr; 1352 } else if (CmpTy == LLT::scalar(64)) { 1353 CmpOpc = AArch64::FCMPDrr; 1354 } else { 1355 return false; 1356 } 1357 1358 // FIXME: regbank 1359 1360 AArch64CC::CondCode CC1, CC2; 1361 changeFCMPPredToAArch64CC( 1362 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2); 1363 1364 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc)) 1365 .addUse(I.getOperand(2).getReg()) 1366 .addUse(I.getOperand(3).getReg()); 1367 1368 const unsigned DefReg = I.getOperand(0).getReg(); 1369 unsigned Def1Reg = DefReg; 1370 if (CC2 != AArch64CC::AL) 1371 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass); 1372 1373 MachineInstr &CSetMI = 1374 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr)) 1375 .addDef(Def1Reg) 1376 .addUse(AArch64::WZR) 1377 .addUse(AArch64::WZR) 1378 .addImm(getInvertedCondCode(CC1)); 1379 1380 if (CC2 != AArch64CC::AL) { 1381 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass); 1382 MachineInstr &CSet2MI = 1383 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr)) 1384 .addDef(Def2Reg) 1385 .addUse(AArch64::WZR) 1386 .addUse(AArch64::WZR) 1387 .addImm(getInvertedCondCode(CC2)); 1388 MachineInstr &OrMI = 1389 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr)) 1390 .addDef(DefReg) 1391 .addUse(Def1Reg) 1392 .addUse(Def2Reg); 1393 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI); 1394 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI); 1395 } 1396 1397 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI); 1398 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI); 1399 1400 I.eraseFromParent(); 1401 return true; 1402 } 1403 case TargetOpcode::G_VASTART: 1404 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI) 1405 : selectVaStartAAPCS(I, MF, MRI); 1406 case TargetOpcode::G_IMPLICIT_DEF: 1407 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF)); 1408 return true; 1409 } 1410 1411 return false; 1412 } 1413 1414 /// SelectArithImmed - Select an immediate value that can be represented as 1415 /// a 12-bit value shifted left by either 0 or 12. If so, return true with 1416 /// Val set to the 12-bit value and Shift set to the shifter operand. 1417 InstructionSelector::ComplexRendererFns 1418 AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const { 1419 MachineInstr &MI = *Root.getParent(); 1420 MachineBasicBlock &MBB = *MI.getParent(); 1421 MachineFunction &MF = *MBB.getParent(); 1422 MachineRegisterInfo &MRI = MF.getRegInfo(); 1423 1424 // This function is called from the addsub_shifted_imm ComplexPattern, 1425 // which lists [imm] as the list of opcode it's interested in, however 1426 // we still need to check whether the operand is actually an immediate 1427 // here because the ComplexPattern opcode list is only used in 1428 // root-level opcode matching. 1429 uint64_t Immed; 1430 if (Root.isImm()) 1431 Immed = Root.getImm(); 1432 else if (Root.isCImm()) 1433 Immed = Root.getCImm()->getZExtValue(); 1434 else if (Root.isReg()) { 1435 MachineInstr *Def = MRI.getVRegDef(Root.getReg()); 1436 if (Def->getOpcode() != TargetOpcode::G_CONSTANT) 1437 return None; 1438 MachineOperand &Op1 = Def->getOperand(1); 1439 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64) 1440 return None; 1441 Immed = Op1.getCImm()->getZExtValue(); 1442 } else 1443 return None; 1444 1445 unsigned ShiftAmt; 1446 1447 if (Immed >> 12 == 0) { 1448 ShiftAmt = 0; 1449 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) { 1450 ShiftAmt = 12; 1451 Immed = Immed >> 12; 1452 } else 1453 return None; 1454 1455 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt); 1456 return {{ 1457 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); }, 1458 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); }, 1459 }}; 1460 } 1461 1462 /// Select a "register plus unscaled signed 9-bit immediate" address. This 1463 /// should only match when there is an offset that is not valid for a scaled 1464 /// immediate addressing mode. The "Size" argument is the size in bytes of the 1465 /// memory reference, which is needed here to know what is valid for a scaled 1466 /// immediate. 1467 InstructionSelector::ComplexRendererFns 1468 AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root, 1469 unsigned Size) const { 1470 MachineRegisterInfo &MRI = 1471 Root.getParent()->getParent()->getParent()->getRegInfo(); 1472 1473 if (!Root.isReg()) 1474 return None; 1475 1476 if (!isBaseWithConstantOffset(Root, MRI)) 1477 return None; 1478 1479 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg()); 1480 if (!RootDef) 1481 return None; 1482 1483 MachineOperand &OffImm = RootDef->getOperand(2); 1484 if (!OffImm.isReg()) 1485 return None; 1486 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg()); 1487 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT) 1488 return None; 1489 int64_t RHSC; 1490 MachineOperand &RHSOp1 = RHS->getOperand(1); 1491 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64) 1492 return None; 1493 RHSC = RHSOp1.getCImm()->getSExtValue(); 1494 1495 // If the offset is valid as a scaled immediate, don't match here. 1496 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size))) 1497 return None; 1498 if (RHSC >= -256 && RHSC < 256) { 1499 MachineOperand &Base = RootDef->getOperand(1); 1500 return {{ 1501 [=](MachineInstrBuilder &MIB) { MIB.add(Base); }, 1502 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); }, 1503 }}; 1504 } 1505 return None; 1506 } 1507 1508 /// Select a "register plus scaled unsigned 12-bit immediate" address. The 1509 /// "Size" argument is the size in bytes of the memory reference, which 1510 /// determines the scale. 1511 InstructionSelector::ComplexRendererFns 1512 AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root, 1513 unsigned Size) const { 1514 MachineRegisterInfo &MRI = 1515 Root.getParent()->getParent()->getParent()->getRegInfo(); 1516 1517 if (!Root.isReg()) 1518 return None; 1519 1520 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg()); 1521 if (!RootDef) 1522 return None; 1523 1524 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) { 1525 return {{ 1526 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); }, 1527 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); }, 1528 }}; 1529 } 1530 1531 if (isBaseWithConstantOffset(Root, MRI)) { 1532 MachineOperand &LHS = RootDef->getOperand(1); 1533 MachineOperand &RHS = RootDef->getOperand(2); 1534 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg()); 1535 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg()); 1536 if (LHSDef && RHSDef) { 1537 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue(); 1538 unsigned Scale = Log2_32(Size); 1539 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) { 1540 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) 1541 return {{ 1542 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); }, 1543 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); }, 1544 }}; 1545 1546 return {{ 1547 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); }, 1548 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); }, 1549 }}; 1550 } 1551 } 1552 } 1553 1554 // Before falling back to our general case, check if the unscaled 1555 // instructions can handle this. If so, that's preferable. 1556 if (selectAddrModeUnscaled(Root, Size).hasValue()) 1557 return None; 1558 1559 return {{ 1560 [=](MachineInstrBuilder &MIB) { MIB.add(Root); }, 1561 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); }, 1562 }}; 1563 } 1564 1565 namespace llvm { 1566 InstructionSelector * 1567 createAArch64InstructionSelector(const AArch64TargetMachine &TM, 1568 AArch64Subtarget &Subtarget, 1569 AArch64RegisterBankInfo &RBI) { 1570 return new AArch64InstructionSelector(TM, Subtarget, RBI); 1571 } 1572 } 1573