1 //===- ARMInstructionSelector.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 InstructionSelector class for ARM. 10 /// \todo This should be generated by TableGen. 11 //===----------------------------------------------------------------------===// 12 13 #include "ARMRegisterBankInfo.h" 14 #include "ARMSubtarget.h" 15 #include "ARMTargetMachine.h" 16 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" 17 #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h" 18 #include "llvm/CodeGen/MachineConstantPool.h" 19 #include "llvm/CodeGen/MachineRegisterInfo.h" 20 #include "llvm/Support/Debug.h" 21 22 #define DEBUG_TYPE "arm-isel" 23 24 using namespace llvm; 25 26 namespace { 27 28 #define GET_GLOBALISEL_PREDICATE_BITSET 29 #include "ARMGenGlobalISel.inc" 30 #undef GET_GLOBALISEL_PREDICATE_BITSET 31 32 class ARMInstructionSelector : public InstructionSelector { 33 public: 34 ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI, 35 const ARMRegisterBankInfo &RBI); 36 37 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override; 38 static const char *getName() { return DEBUG_TYPE; } 39 40 private: 41 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const; 42 43 struct CmpConstants; 44 struct InsertInfo; 45 46 bool selectCmp(CmpConstants Helper, MachineInstrBuilder &MIB, 47 MachineRegisterInfo &MRI) const; 48 49 // Helper for inserting a comparison sequence that sets \p ResReg to either 1 50 // if \p LHSReg and \p RHSReg are in the relationship defined by \p Cond, or 51 // \p PrevRes otherwise. In essence, it computes PrevRes OR (LHS Cond RHS). 52 bool insertComparison(CmpConstants Helper, InsertInfo I, unsigned ResReg, 53 ARMCC::CondCodes Cond, unsigned LHSReg, unsigned RHSReg, 54 unsigned PrevRes) const; 55 56 // Set \p DestReg to \p Constant. 57 void putConstant(InsertInfo I, unsigned DestReg, unsigned Constant) const; 58 59 bool selectGlobal(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const; 60 bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const; 61 bool selectShift(unsigned ShiftOpc, MachineInstrBuilder &MIB) const; 62 63 // Check if the types match and both operands have the expected size and 64 // register bank. 65 bool validOpRegPair(MachineRegisterInfo &MRI, unsigned LHS, unsigned RHS, 66 unsigned ExpectedSize, unsigned ExpectedRegBankID) const; 67 68 // Check if the register has the expected size and register bank. 69 bool validReg(MachineRegisterInfo &MRI, unsigned Reg, unsigned ExpectedSize, 70 unsigned ExpectedRegBankID) const; 71 72 const ARMBaseInstrInfo &TII; 73 const ARMBaseRegisterInfo &TRI; 74 const ARMBaseTargetMachine &TM; 75 const ARMRegisterBankInfo &RBI; 76 const ARMSubtarget &STI; 77 78 // FIXME: This is necessary because DAGISel uses "Subtarget->" and GlobalISel 79 // uses "STI." in the code generated by TableGen. If we want to reuse some of 80 // the custom C++ predicates written for DAGISel, we need to have both around. 81 const ARMSubtarget *Subtarget = &STI; 82 83 // Store the opcodes that we might need, so we don't have to check what kind 84 // of subtarget (ARM vs Thumb) we have all the time. 85 struct OpcodeCache { 86 unsigned ZEXT16; 87 unsigned SEXT16; 88 89 unsigned ZEXT8; 90 unsigned SEXT8; 91 92 // Used for implementing ZEXT/SEXT from i1 93 unsigned AND; 94 unsigned RSB; 95 96 unsigned STORE32; 97 unsigned LOAD32; 98 99 unsigned STORE16; 100 unsigned LOAD16; 101 102 unsigned STORE8; 103 unsigned LOAD8; 104 105 unsigned ADDrr; 106 unsigned ADDri; 107 108 // Used for G_ICMP 109 unsigned CMPrr; 110 unsigned MOVi; 111 unsigned MOVCCi; 112 113 // Used for G_SELECT 114 unsigned CMPri; 115 unsigned MOVCCr; 116 117 unsigned TSTri; 118 unsigned Bcc; 119 120 // Used for G_GLOBAL_VALUE 121 unsigned MOVi32imm; 122 unsigned ConstPoolLoad; 123 unsigned MOV_ga_pcrel; 124 unsigned LDRLIT_ga_pcrel; 125 unsigned LDRLIT_ga_abs; 126 127 OpcodeCache(const ARMSubtarget &STI); 128 } const Opcodes; 129 130 // Select the opcode for simple extensions (that translate to a single SXT/UXT 131 // instruction). Extension operations more complicated than that should not 132 // invoke this. Returns the original opcode if it doesn't know how to select a 133 // better one. 134 unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) const; 135 136 // Select the opcode for simple loads and stores. Returns the original opcode 137 // if it doesn't know how to select a better one. 138 unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank, 139 unsigned Size) const; 140 141 #define GET_GLOBALISEL_PREDICATES_DECL 142 #include "ARMGenGlobalISel.inc" 143 #undef GET_GLOBALISEL_PREDICATES_DECL 144 145 // We declare the temporaries used by selectImpl() in the class to minimize the 146 // cost of constructing placeholder values. 147 #define GET_GLOBALISEL_TEMPORARIES_DECL 148 #include "ARMGenGlobalISel.inc" 149 #undef GET_GLOBALISEL_TEMPORARIES_DECL 150 }; 151 } // end anonymous namespace 152 153 namespace llvm { 154 InstructionSelector * 155 createARMInstructionSelector(const ARMBaseTargetMachine &TM, 156 const ARMSubtarget &STI, 157 const ARMRegisterBankInfo &RBI) { 158 return new ARMInstructionSelector(TM, STI, RBI); 159 } 160 } 161 162 const unsigned zero_reg = 0; 163 164 #define GET_GLOBALISEL_IMPL 165 #include "ARMGenGlobalISel.inc" 166 #undef GET_GLOBALISEL_IMPL 167 168 ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM, 169 const ARMSubtarget &STI, 170 const ARMRegisterBankInfo &RBI) 171 : InstructionSelector(), TII(*STI.getInstrInfo()), 172 TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), Opcodes(STI), 173 #define GET_GLOBALISEL_PREDICATES_INIT 174 #include "ARMGenGlobalISel.inc" 175 #undef GET_GLOBALISEL_PREDICATES_INIT 176 #define GET_GLOBALISEL_TEMPORARIES_INIT 177 #include "ARMGenGlobalISel.inc" 178 #undef GET_GLOBALISEL_TEMPORARIES_INIT 179 { 180 } 181 182 static const TargetRegisterClass *guessRegClass(unsigned Reg, 183 MachineRegisterInfo &MRI, 184 const TargetRegisterInfo &TRI, 185 const RegisterBankInfo &RBI) { 186 const RegisterBank *RegBank = RBI.getRegBank(Reg, MRI, TRI); 187 assert(RegBank && "Can't get reg bank for virtual register"); 188 189 const unsigned Size = MRI.getType(Reg).getSizeInBits(); 190 assert((RegBank->getID() == ARM::GPRRegBankID || 191 RegBank->getID() == ARM::FPRRegBankID) && 192 "Unsupported reg bank"); 193 194 if (RegBank->getID() == ARM::FPRRegBankID) { 195 if (Size == 32) 196 return &ARM::SPRRegClass; 197 else if (Size == 64) 198 return &ARM::DPRRegClass; 199 else if (Size == 128) 200 return &ARM::QPRRegClass; 201 else 202 llvm_unreachable("Unsupported destination size"); 203 } 204 205 return &ARM::GPRRegClass; 206 } 207 208 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, 209 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 210 const RegisterBankInfo &RBI) { 211 unsigned DstReg = I.getOperand(0).getReg(); 212 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) 213 return true; 214 215 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI); 216 217 // No need to constrain SrcReg. It will get constrained when 218 // we hit another of its uses or its defs. 219 // Copies do not have constraints. 220 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { 221 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) 222 << " operand\n"); 223 return false; 224 } 225 return true; 226 } 227 228 static bool selectMergeValues(MachineInstrBuilder &MIB, 229 const ARMBaseInstrInfo &TII, 230 MachineRegisterInfo &MRI, 231 const TargetRegisterInfo &TRI, 232 const RegisterBankInfo &RBI) { 233 assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP"); 234 235 // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs 236 // into one DPR. 237 unsigned VReg0 = MIB->getOperand(0).getReg(); 238 (void)VReg0; 239 assert(MRI.getType(VReg0).getSizeInBits() == 64 && 240 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID && 241 "Unsupported operand for G_MERGE_VALUES"); 242 unsigned VReg1 = MIB->getOperand(1).getReg(); 243 (void)VReg1; 244 assert(MRI.getType(VReg1).getSizeInBits() == 32 && 245 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && 246 "Unsupported operand for G_MERGE_VALUES"); 247 unsigned VReg2 = MIB->getOperand(2).getReg(); 248 (void)VReg2; 249 assert(MRI.getType(VReg2).getSizeInBits() == 32 && 250 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID && 251 "Unsupported operand for G_MERGE_VALUES"); 252 253 MIB->setDesc(TII.get(ARM::VMOVDRR)); 254 MIB.add(predOps(ARMCC::AL)); 255 256 return true; 257 } 258 259 static bool selectUnmergeValues(MachineInstrBuilder &MIB, 260 const ARMBaseInstrInfo &TII, 261 MachineRegisterInfo &MRI, 262 const TargetRegisterInfo &TRI, 263 const RegisterBankInfo &RBI) { 264 assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP"); 265 266 // We only support G_UNMERGE_VALUES as a way to break up one DPR into two 267 // GPRs. 268 unsigned VReg0 = MIB->getOperand(0).getReg(); 269 (void)VReg0; 270 assert(MRI.getType(VReg0).getSizeInBits() == 32 && 271 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID && 272 "Unsupported operand for G_UNMERGE_VALUES"); 273 unsigned VReg1 = MIB->getOperand(1).getReg(); 274 (void)VReg1; 275 assert(MRI.getType(VReg1).getSizeInBits() == 32 && 276 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && 277 "Unsupported operand for G_UNMERGE_VALUES"); 278 unsigned VReg2 = MIB->getOperand(2).getReg(); 279 (void)VReg2; 280 assert(MRI.getType(VReg2).getSizeInBits() == 64 && 281 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID && 282 "Unsupported operand for G_UNMERGE_VALUES"); 283 284 MIB->setDesc(TII.get(ARM::VMOVRRD)); 285 MIB.add(predOps(ARMCC::AL)); 286 287 return true; 288 } 289 290 ARMInstructionSelector::OpcodeCache::OpcodeCache(const ARMSubtarget &STI) { 291 bool isThumb = STI.isThumb(); 292 293 using namespace TargetOpcode; 294 295 #define STORE_OPCODE(VAR, OPC) VAR = isThumb ? ARM::t2##OPC : ARM::OPC 296 STORE_OPCODE(SEXT16, SXTH); 297 STORE_OPCODE(ZEXT16, UXTH); 298 299 STORE_OPCODE(SEXT8, SXTB); 300 STORE_OPCODE(ZEXT8, UXTB); 301 302 STORE_OPCODE(AND, ANDri); 303 STORE_OPCODE(RSB, RSBri); 304 305 STORE_OPCODE(STORE32, STRi12); 306 STORE_OPCODE(LOAD32, LDRi12); 307 308 // LDRH/STRH are special... 309 STORE16 = isThumb ? ARM::t2STRHi12 : ARM::STRH; 310 LOAD16 = isThumb ? ARM::t2LDRHi12 : ARM::LDRH; 311 312 STORE_OPCODE(STORE8, STRBi12); 313 STORE_OPCODE(LOAD8, LDRBi12); 314 315 STORE_OPCODE(ADDrr, ADDrr); 316 STORE_OPCODE(ADDri, ADDri); 317 318 STORE_OPCODE(CMPrr, CMPrr); 319 STORE_OPCODE(MOVi, MOVi); 320 STORE_OPCODE(MOVCCi, MOVCCi); 321 322 STORE_OPCODE(CMPri, CMPri); 323 STORE_OPCODE(MOVCCr, MOVCCr); 324 325 STORE_OPCODE(TSTri, TSTri); 326 STORE_OPCODE(Bcc, Bcc); 327 328 STORE_OPCODE(MOVi32imm, MOVi32imm); 329 ConstPoolLoad = isThumb ? ARM::t2LDRpci : ARM::LDRi12; 330 STORE_OPCODE(MOV_ga_pcrel, MOV_ga_pcrel); 331 LDRLIT_ga_pcrel = isThumb ? ARM::tLDRLIT_ga_pcrel : ARM::LDRLIT_ga_pcrel; 332 LDRLIT_ga_abs = isThumb ? ARM::tLDRLIT_ga_abs : ARM::LDRLIT_ga_abs; 333 #undef MAP_OPCODE 334 } 335 336 unsigned ARMInstructionSelector::selectSimpleExtOpc(unsigned Opc, 337 unsigned Size) const { 338 using namespace TargetOpcode; 339 340 if (Size != 8 && Size != 16) 341 return Opc; 342 343 if (Opc == G_SEXT) 344 return Size == 8 ? Opcodes.SEXT8 : Opcodes.SEXT16; 345 346 if (Opc == G_ZEXT) 347 return Size == 8 ? Opcodes.ZEXT8 : Opcodes.ZEXT16; 348 349 return Opc; 350 } 351 352 unsigned ARMInstructionSelector::selectLoadStoreOpCode(unsigned Opc, 353 unsigned RegBank, 354 unsigned Size) const { 355 bool isStore = Opc == TargetOpcode::G_STORE; 356 357 if (RegBank == ARM::GPRRegBankID) { 358 switch (Size) { 359 case 1: 360 case 8: 361 return isStore ? Opcodes.STORE8 : Opcodes.LOAD8; 362 case 16: 363 return isStore ? Opcodes.STORE16 : Opcodes.LOAD16; 364 case 32: 365 return isStore ? Opcodes.STORE32 : Opcodes.LOAD32; 366 default: 367 return Opc; 368 } 369 } 370 371 if (RegBank == ARM::FPRRegBankID) { 372 switch (Size) { 373 case 32: 374 return isStore ? ARM::VSTRS : ARM::VLDRS; 375 case 64: 376 return isStore ? ARM::VSTRD : ARM::VLDRD; 377 default: 378 return Opc; 379 } 380 } 381 382 return Opc; 383 } 384 385 // When lowering comparisons, we sometimes need to perform two compares instead 386 // of just one. Get the condition codes for both comparisons. If only one is 387 // needed, the second member of the pair is ARMCC::AL. 388 static std::pair<ARMCC::CondCodes, ARMCC::CondCodes> 389 getComparePreds(CmpInst::Predicate Pred) { 390 std::pair<ARMCC::CondCodes, ARMCC::CondCodes> Preds = {ARMCC::AL, ARMCC::AL}; 391 switch (Pred) { 392 case CmpInst::FCMP_ONE: 393 Preds = {ARMCC::GT, ARMCC::MI}; 394 break; 395 case CmpInst::FCMP_UEQ: 396 Preds = {ARMCC::EQ, ARMCC::VS}; 397 break; 398 case CmpInst::ICMP_EQ: 399 case CmpInst::FCMP_OEQ: 400 Preds.first = ARMCC::EQ; 401 break; 402 case CmpInst::ICMP_SGT: 403 case CmpInst::FCMP_OGT: 404 Preds.first = ARMCC::GT; 405 break; 406 case CmpInst::ICMP_SGE: 407 case CmpInst::FCMP_OGE: 408 Preds.first = ARMCC::GE; 409 break; 410 case CmpInst::ICMP_UGT: 411 case CmpInst::FCMP_UGT: 412 Preds.first = ARMCC::HI; 413 break; 414 case CmpInst::FCMP_OLT: 415 Preds.first = ARMCC::MI; 416 break; 417 case CmpInst::ICMP_ULE: 418 case CmpInst::FCMP_OLE: 419 Preds.first = ARMCC::LS; 420 break; 421 case CmpInst::FCMP_ORD: 422 Preds.first = ARMCC::VC; 423 break; 424 case CmpInst::FCMP_UNO: 425 Preds.first = ARMCC::VS; 426 break; 427 case CmpInst::FCMP_UGE: 428 Preds.first = ARMCC::PL; 429 break; 430 case CmpInst::ICMP_SLT: 431 case CmpInst::FCMP_ULT: 432 Preds.first = ARMCC::LT; 433 break; 434 case CmpInst::ICMP_SLE: 435 case CmpInst::FCMP_ULE: 436 Preds.first = ARMCC::LE; 437 break; 438 case CmpInst::FCMP_UNE: 439 case CmpInst::ICMP_NE: 440 Preds.first = ARMCC::NE; 441 break; 442 case CmpInst::ICMP_UGE: 443 Preds.first = ARMCC::HS; 444 break; 445 case CmpInst::ICMP_ULT: 446 Preds.first = ARMCC::LO; 447 break; 448 default: 449 break; 450 } 451 assert(Preds.first != ARMCC::AL && "No comparisons needed?"); 452 return Preds; 453 } 454 455 struct ARMInstructionSelector::CmpConstants { 456 CmpConstants(unsigned CmpOpcode, unsigned FlagsOpcode, unsigned SelectOpcode, 457 unsigned OpRegBank, unsigned OpSize) 458 : ComparisonOpcode(CmpOpcode), ReadFlagsOpcode(FlagsOpcode), 459 SelectResultOpcode(SelectOpcode), OperandRegBankID(OpRegBank), 460 OperandSize(OpSize) {} 461 462 // The opcode used for performing the comparison. 463 const unsigned ComparisonOpcode; 464 465 // The opcode used for reading the flags set by the comparison. May be 466 // ARM::INSTRUCTION_LIST_END if we don't need to read the flags. 467 const unsigned ReadFlagsOpcode; 468 469 // The opcode used for materializing the result of the comparison. 470 const unsigned SelectResultOpcode; 471 472 // The assumed register bank ID for the operands. 473 const unsigned OperandRegBankID; 474 475 // The assumed size in bits for the operands. 476 const unsigned OperandSize; 477 }; 478 479 struct ARMInstructionSelector::InsertInfo { 480 InsertInfo(MachineInstrBuilder &MIB) 481 : MBB(*MIB->getParent()), InsertBefore(std::next(MIB->getIterator())), 482 DbgLoc(MIB->getDebugLoc()) {} 483 484 MachineBasicBlock &MBB; 485 const MachineBasicBlock::instr_iterator InsertBefore; 486 const DebugLoc &DbgLoc; 487 }; 488 489 void ARMInstructionSelector::putConstant(InsertInfo I, unsigned DestReg, 490 unsigned Constant) const { 491 (void)BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Opcodes.MOVi)) 492 .addDef(DestReg) 493 .addImm(Constant) 494 .add(predOps(ARMCC::AL)) 495 .add(condCodeOp()); 496 } 497 498 bool ARMInstructionSelector::validOpRegPair(MachineRegisterInfo &MRI, 499 unsigned LHSReg, unsigned RHSReg, 500 unsigned ExpectedSize, 501 unsigned ExpectedRegBankID) const { 502 return MRI.getType(LHSReg) == MRI.getType(RHSReg) && 503 validReg(MRI, LHSReg, ExpectedSize, ExpectedRegBankID) && 504 validReg(MRI, RHSReg, ExpectedSize, ExpectedRegBankID); 505 } 506 507 bool ARMInstructionSelector::validReg(MachineRegisterInfo &MRI, unsigned Reg, 508 unsigned ExpectedSize, 509 unsigned ExpectedRegBankID) const { 510 if (MRI.getType(Reg).getSizeInBits() != ExpectedSize) { 511 LLVM_DEBUG(dbgs() << "Unexpected size for register"); 512 return false; 513 } 514 515 if (RBI.getRegBank(Reg, MRI, TRI)->getID() != ExpectedRegBankID) { 516 LLVM_DEBUG(dbgs() << "Unexpected register bank for register"); 517 return false; 518 } 519 520 return true; 521 } 522 523 bool ARMInstructionSelector::selectCmp(CmpConstants Helper, 524 MachineInstrBuilder &MIB, 525 MachineRegisterInfo &MRI) const { 526 const InsertInfo I(MIB); 527 528 auto ResReg = MIB->getOperand(0).getReg(); 529 if (!validReg(MRI, ResReg, 1, ARM::GPRRegBankID)) 530 return false; 531 532 auto Cond = 533 static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate()); 534 if (Cond == CmpInst::FCMP_TRUE || Cond == CmpInst::FCMP_FALSE) { 535 putConstant(I, ResReg, Cond == CmpInst::FCMP_TRUE ? 1 : 0); 536 MIB->eraseFromParent(); 537 return true; 538 } 539 540 auto LHSReg = MIB->getOperand(2).getReg(); 541 auto RHSReg = MIB->getOperand(3).getReg(); 542 if (!validOpRegPair(MRI, LHSReg, RHSReg, Helper.OperandSize, 543 Helper.OperandRegBankID)) 544 return false; 545 546 auto ARMConds = getComparePreds(Cond); 547 auto ZeroReg = MRI.createVirtualRegister(&ARM::GPRRegClass); 548 putConstant(I, ZeroReg, 0); 549 550 if (ARMConds.second == ARMCC::AL) { 551 // Simple case, we only need one comparison and we're done. 552 if (!insertComparison(Helper, I, ResReg, ARMConds.first, LHSReg, RHSReg, 553 ZeroReg)) 554 return false; 555 } else { 556 // Not so simple, we need two successive comparisons. 557 auto IntermediateRes = MRI.createVirtualRegister(&ARM::GPRRegClass); 558 if (!insertComparison(Helper, I, IntermediateRes, ARMConds.first, LHSReg, 559 RHSReg, ZeroReg)) 560 return false; 561 if (!insertComparison(Helper, I, ResReg, ARMConds.second, LHSReg, RHSReg, 562 IntermediateRes)) 563 return false; 564 } 565 566 MIB->eraseFromParent(); 567 return true; 568 } 569 570 bool ARMInstructionSelector::insertComparison(CmpConstants Helper, InsertInfo I, 571 unsigned ResReg, 572 ARMCC::CondCodes Cond, 573 unsigned LHSReg, unsigned RHSReg, 574 unsigned PrevRes) const { 575 // Perform the comparison. 576 auto CmpI = 577 BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Helper.ComparisonOpcode)) 578 .addUse(LHSReg) 579 .addUse(RHSReg) 580 .add(predOps(ARMCC::AL)); 581 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) 582 return false; 583 584 // Read the comparison flags (if necessary). 585 if (Helper.ReadFlagsOpcode != ARM::INSTRUCTION_LIST_END) { 586 auto ReadI = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, 587 TII.get(Helper.ReadFlagsOpcode)) 588 .add(predOps(ARMCC::AL)); 589 if (!constrainSelectedInstRegOperands(*ReadI, TII, TRI, RBI)) 590 return false; 591 } 592 593 // Select either 1 or the previous result based on the value of the flags. 594 auto Mov1I = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, 595 TII.get(Helper.SelectResultOpcode)) 596 .addDef(ResReg) 597 .addUse(PrevRes) 598 .addImm(1) 599 .add(predOps(Cond, ARM::CPSR)); 600 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) 601 return false; 602 603 return true; 604 } 605 606 bool ARMInstructionSelector::selectGlobal(MachineInstrBuilder &MIB, 607 MachineRegisterInfo &MRI) const { 608 if ((STI.isROPI() || STI.isRWPI()) && !STI.isTargetELF()) { 609 LLVM_DEBUG(dbgs() << "ROPI and RWPI only supported for ELF\n"); 610 return false; 611 } 612 613 auto GV = MIB->getOperand(1).getGlobal(); 614 if (GV->isThreadLocal()) { 615 LLVM_DEBUG(dbgs() << "TLS variables not supported yet\n"); 616 return false; 617 } 618 619 auto &MBB = *MIB->getParent(); 620 auto &MF = *MBB.getParent(); 621 622 bool UseMovt = STI.useMovt(); 623 624 unsigned Size = TM.getPointerSize(0); 625 unsigned Alignment = 4; 626 627 auto addOpsForConstantPoolLoad = [&MF, Alignment, 628 Size](MachineInstrBuilder &MIB, 629 const GlobalValue *GV, bool IsSBREL) { 630 assert((MIB->getOpcode() == ARM::LDRi12 || 631 MIB->getOpcode() == ARM::t2LDRpci) && 632 "Unsupported instruction"); 633 auto ConstPool = MF.getConstantPool(); 634 auto CPIndex = 635 // For SB relative entries we need a target-specific constant pool. 636 // Otherwise, just use a regular constant pool entry. 637 IsSBREL 638 ? ConstPool->getConstantPoolIndex( 639 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL), Alignment) 640 : ConstPool->getConstantPoolIndex(GV, Alignment); 641 MIB.addConstantPoolIndex(CPIndex, /*Offset*/ 0, /*TargetFlags*/ 0) 642 .addMemOperand(MF.getMachineMemOperand( 643 MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad, 644 Size, Alignment)); 645 if (MIB->getOpcode() == ARM::LDRi12) 646 MIB.addImm(0); 647 MIB.add(predOps(ARMCC::AL)); 648 }; 649 650 auto addGOTMemOperand = [this, &MF, Alignment](MachineInstrBuilder &MIB) { 651 MIB.addMemOperand(MF.getMachineMemOperand( 652 MachinePointerInfo::getGOT(MF), MachineMemOperand::MOLoad, 653 TM.getProgramPointerSize(), Alignment)); 654 }; 655 656 if (TM.isPositionIndependent()) { 657 bool Indirect = STI.isGVIndirectSymbol(GV); 658 659 // For ARM mode, we have different pseudoinstructions for direct accesses 660 // and indirect accesses, and the ones for indirect accesses include the 661 // load from GOT. For Thumb mode, we use the same pseudoinstruction for both 662 // direct and indirect accesses, and we need to manually generate the load 663 // from GOT. 664 bool UseOpcodeThatLoads = Indirect && !STI.isThumb(); 665 666 // FIXME: Taking advantage of MOVT for ELF is pretty involved, so we don't 667 // support it yet. See PR28229. 668 unsigned Opc = 669 UseMovt && !STI.isTargetELF() 670 ? (UseOpcodeThatLoads ? (unsigned)ARM::MOV_ga_pcrel_ldr 671 : Opcodes.MOV_ga_pcrel) 672 : (UseOpcodeThatLoads ? (unsigned)ARM::LDRLIT_ga_pcrel_ldr 673 : Opcodes.LDRLIT_ga_pcrel); 674 MIB->setDesc(TII.get(Opc)); 675 676 int TargetFlags = ARMII::MO_NO_FLAG; 677 if (STI.isTargetDarwin()) 678 TargetFlags |= ARMII::MO_NONLAZY; 679 if (STI.isGVInGOT(GV)) 680 TargetFlags |= ARMII::MO_GOT; 681 MIB->getOperand(1).setTargetFlags(TargetFlags); 682 683 if (Indirect) { 684 if (!UseOpcodeThatLoads) { 685 auto ResultReg = MIB->getOperand(0).getReg(); 686 auto AddressReg = MRI.createVirtualRegister(&ARM::GPRRegClass); 687 688 MIB->getOperand(0).setReg(AddressReg); 689 690 auto InsertBefore = std::next(MIB->getIterator()); 691 auto MIBLoad = BuildMI(MBB, InsertBefore, MIB->getDebugLoc(), 692 TII.get(Opcodes.LOAD32)) 693 .addDef(ResultReg) 694 .addReg(AddressReg) 695 .addImm(0) 696 .add(predOps(ARMCC::AL)); 697 addGOTMemOperand(MIBLoad); 698 699 if (!constrainSelectedInstRegOperands(*MIBLoad, TII, TRI, RBI)) 700 return false; 701 } else { 702 addGOTMemOperand(MIB); 703 } 704 } 705 706 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); 707 } 708 709 bool isReadOnly = STI.getTargetLowering()->isReadOnly(GV); 710 if (STI.isROPI() && isReadOnly) { 711 unsigned Opc = UseMovt ? Opcodes.MOV_ga_pcrel : Opcodes.LDRLIT_ga_pcrel; 712 MIB->setDesc(TII.get(Opc)); 713 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); 714 } 715 if (STI.isRWPI() && !isReadOnly) { 716 auto Offset = MRI.createVirtualRegister(&ARM::GPRRegClass); 717 MachineInstrBuilder OffsetMIB; 718 if (UseMovt) { 719 OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(), 720 TII.get(Opcodes.MOVi32imm), Offset); 721 OffsetMIB.addGlobalAddress(GV, /*Offset*/ 0, ARMII::MO_SBREL); 722 } else { 723 // Load the offset from the constant pool. 724 OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(), 725 TII.get(Opcodes.ConstPoolLoad), Offset); 726 addOpsForConstantPoolLoad(OffsetMIB, GV, /*IsSBREL*/ true); 727 } 728 if (!constrainSelectedInstRegOperands(*OffsetMIB, TII, TRI, RBI)) 729 return false; 730 731 // Add the offset to the SB register. 732 MIB->setDesc(TII.get(Opcodes.ADDrr)); 733 MIB->RemoveOperand(1); 734 MIB.addReg(ARM::R9) // FIXME: don't hardcode R9 735 .addReg(Offset) 736 .add(predOps(ARMCC::AL)) 737 .add(condCodeOp()); 738 739 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); 740 } 741 742 if (STI.isTargetELF()) { 743 if (UseMovt) { 744 MIB->setDesc(TII.get(Opcodes.MOVi32imm)); 745 } else { 746 // Load the global's address from the constant pool. 747 MIB->setDesc(TII.get(Opcodes.ConstPoolLoad)); 748 MIB->RemoveOperand(1); 749 addOpsForConstantPoolLoad(MIB, GV, /*IsSBREL*/ false); 750 } 751 } else if (STI.isTargetMachO()) { 752 if (UseMovt) 753 MIB->setDesc(TII.get(Opcodes.MOVi32imm)); 754 else 755 MIB->setDesc(TII.get(Opcodes.LDRLIT_ga_abs)); 756 } else { 757 LLVM_DEBUG(dbgs() << "Object format not supported yet\n"); 758 return false; 759 } 760 761 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); 762 } 763 764 bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB, 765 MachineRegisterInfo &MRI) const { 766 auto &MBB = *MIB->getParent(); 767 auto InsertBefore = std::next(MIB->getIterator()); 768 auto &DbgLoc = MIB->getDebugLoc(); 769 770 // Compare the condition to 0. 771 auto CondReg = MIB->getOperand(1).getReg(); 772 assert(validReg(MRI, CondReg, 1, ARM::GPRRegBankID) && 773 "Unsupported types for select operation"); 774 auto CmpI = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(Opcodes.CMPri)) 775 .addUse(CondReg) 776 .addImm(0) 777 .add(predOps(ARMCC::AL)); 778 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) 779 return false; 780 781 // Move a value into the result register based on the result of the 782 // comparison. 783 auto ResReg = MIB->getOperand(0).getReg(); 784 auto TrueReg = MIB->getOperand(2).getReg(); 785 auto FalseReg = MIB->getOperand(3).getReg(); 786 assert(validOpRegPair(MRI, ResReg, TrueReg, 32, ARM::GPRRegBankID) && 787 validOpRegPair(MRI, TrueReg, FalseReg, 32, ARM::GPRRegBankID) && 788 "Unsupported types for select operation"); 789 auto Mov1I = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(Opcodes.MOVCCr)) 790 .addDef(ResReg) 791 .addUse(TrueReg) 792 .addUse(FalseReg) 793 .add(predOps(ARMCC::EQ, ARM::CPSR)); 794 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) 795 return false; 796 797 MIB->eraseFromParent(); 798 return true; 799 } 800 801 bool ARMInstructionSelector::selectShift(unsigned ShiftOpc, 802 MachineInstrBuilder &MIB) const { 803 MIB->setDesc(TII.get(ARM::MOVsr)); 804 MIB.addImm(ShiftOpc); 805 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 806 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); 807 } 808 809 bool ARMInstructionSelector::select(MachineInstr &I, 810 CodeGenCoverage &CoverageInfo) const { 811 assert(I.getParent() && "Instruction should be in a basic block!"); 812 assert(I.getParent()->getParent() && "Instruction should be in a function!"); 813 814 auto &MBB = *I.getParent(); 815 auto &MF = *MBB.getParent(); 816 auto &MRI = MF.getRegInfo(); 817 818 if (!isPreISelGenericOpcode(I.getOpcode())) { 819 if (I.isCopy()) 820 return selectCopy(I, TII, MRI, TRI, RBI); 821 822 return true; 823 } 824 825 using namespace TargetOpcode; 826 827 if (selectImpl(I, CoverageInfo)) 828 return true; 829 830 MachineInstrBuilder MIB{MF, I}; 831 bool isSExt = false; 832 833 switch (I.getOpcode()) { 834 case G_SEXT: 835 isSExt = true; 836 LLVM_FALLTHROUGH; 837 case G_ZEXT: { 838 LLT DstTy = MRI.getType(I.getOperand(0).getReg()); 839 // FIXME: Smaller destination sizes coming soon! 840 if (DstTy.getSizeInBits() != 32) { 841 LLVM_DEBUG(dbgs() << "Unsupported destination size for extension"); 842 return false; 843 } 844 845 LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); 846 unsigned SrcSize = SrcTy.getSizeInBits(); 847 switch (SrcSize) { 848 case 1: { 849 // ZExt boils down to & 0x1; for SExt we also subtract that from 0 850 I.setDesc(TII.get(Opcodes.AND)); 851 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); 852 853 if (isSExt) { 854 unsigned SExtResult = I.getOperand(0).getReg(); 855 856 // Use a new virtual register for the result of the AND 857 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); 858 I.getOperand(0).setReg(AndResult); 859 860 auto InsertBefore = std::next(I.getIterator()); 861 auto SubI = 862 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(Opcodes.RSB)) 863 .addDef(SExtResult) 864 .addUse(AndResult) 865 .addImm(0) 866 .add(predOps(ARMCC::AL)) 867 .add(condCodeOp()); 868 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) 869 return false; 870 } 871 break; 872 } 873 case 8: 874 case 16: { 875 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); 876 if (NewOpc == I.getOpcode()) 877 return false; 878 I.setDesc(TII.get(NewOpc)); 879 MIB.addImm(0).add(predOps(ARMCC::AL)); 880 break; 881 } 882 default: 883 LLVM_DEBUG(dbgs() << "Unsupported source size for extension"); 884 return false; 885 } 886 break; 887 } 888 case G_ANYEXT: 889 case G_TRUNC: { 890 // The high bits are undefined, so there's nothing special to do, just 891 // treat it as a copy. 892 auto SrcReg = I.getOperand(1).getReg(); 893 auto DstReg = I.getOperand(0).getReg(); 894 895 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); 896 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); 897 898 if (SrcRegBank.getID() == ARM::FPRRegBankID) { 899 // This should only happen in the obscure case where we have put a 64-bit 900 // integer into a D register. Get it out of there and keep only the 901 // interesting part. 902 assert(I.getOpcode() == G_TRUNC && "Unsupported operand for G_ANYEXT"); 903 assert(DstRegBank.getID() == ARM::GPRRegBankID && 904 "Unsupported combination of register banks"); 905 assert(MRI.getType(SrcReg).getSizeInBits() == 64 && "Unsupported size"); 906 assert(MRI.getType(DstReg).getSizeInBits() <= 32 && "Unsupported size"); 907 908 unsigned IgnoredBits = MRI.createVirtualRegister(&ARM::GPRRegClass); 909 auto InsertBefore = std::next(I.getIterator()); 910 auto MovI = 911 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::VMOVRRD)) 912 .addDef(DstReg) 913 .addDef(IgnoredBits) 914 .addUse(SrcReg) 915 .add(predOps(ARMCC::AL)); 916 if (!constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI)) 917 return false; 918 919 MIB->eraseFromParent(); 920 return true; 921 } 922 923 if (SrcRegBank.getID() != DstRegBank.getID()) { 924 LLVM_DEBUG( 925 dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n"); 926 return false; 927 } 928 929 if (SrcRegBank.getID() != ARM::GPRRegBankID) { 930 LLVM_DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n"); 931 return false; 932 } 933 934 I.setDesc(TII.get(COPY)); 935 return selectCopy(I, TII, MRI, TRI, RBI); 936 } 937 case G_CONSTANT: { 938 if (!MRI.getType(I.getOperand(0).getReg()).isPointer()) { 939 // Non-pointer constants should be handled by TableGen. 940 LLVM_DEBUG(dbgs() << "Unsupported constant type\n"); 941 return false; 942 } 943 944 auto &Val = I.getOperand(1); 945 if (Val.isCImm()) { 946 if (!Val.getCImm()->isZero()) { 947 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n"); 948 return false; 949 } 950 Val.ChangeToImmediate(0); 951 } else { 952 assert(Val.isImm() && "Unexpected operand for G_CONSTANT"); 953 if (Val.getImm() != 0) { 954 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n"); 955 return false; 956 } 957 } 958 959 I.setDesc(TII.get(ARM::MOVi)); 960 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 961 break; 962 } 963 case G_INTTOPTR: 964 case G_PTRTOINT: { 965 auto SrcReg = I.getOperand(1).getReg(); 966 auto DstReg = I.getOperand(0).getReg(); 967 968 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); 969 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); 970 971 if (SrcRegBank.getID() != DstRegBank.getID()) { 972 LLVM_DEBUG( 973 dbgs() 974 << "G_INTTOPTR/G_PTRTOINT operands on different register banks\n"); 975 return false; 976 } 977 978 if (SrcRegBank.getID() != ARM::GPRRegBankID) { 979 LLVM_DEBUG( 980 dbgs() << "G_INTTOPTR/G_PTRTOINT on non-GPR not supported yet\n"); 981 return false; 982 } 983 984 I.setDesc(TII.get(COPY)); 985 return selectCopy(I, TII, MRI, TRI, RBI); 986 } 987 case G_SELECT: 988 return selectSelect(MIB, MRI); 989 case G_ICMP: { 990 CmpConstants Helper(Opcodes.CMPrr, ARM::INSTRUCTION_LIST_END, 991 Opcodes.MOVCCi, ARM::GPRRegBankID, 32); 992 return selectCmp(Helper, MIB, MRI); 993 } 994 case G_FCMP: { 995 assert(STI.hasVFP2() && "Can't select fcmp without VFP"); 996 997 unsigned OpReg = I.getOperand(2).getReg(); 998 unsigned Size = MRI.getType(OpReg).getSizeInBits(); 999 1000 if (Size == 64 && STI.isFPOnlySP()) { 1001 LLVM_DEBUG(dbgs() << "Subtarget only supports single precision"); 1002 return false; 1003 } 1004 if (Size != 32 && Size != 64) { 1005 LLVM_DEBUG(dbgs() << "Unsupported size for G_FCMP operand"); 1006 return false; 1007 } 1008 1009 CmpConstants Helper(Size == 32 ? ARM::VCMPS : ARM::VCMPD, ARM::FMSTAT, 1010 Opcodes.MOVCCi, ARM::FPRRegBankID, Size); 1011 return selectCmp(Helper, MIB, MRI); 1012 } 1013 case G_LSHR: 1014 return selectShift(ARM_AM::ShiftOpc::lsr, MIB); 1015 case G_ASHR: 1016 return selectShift(ARM_AM::ShiftOpc::asr, MIB); 1017 case G_SHL: { 1018 return selectShift(ARM_AM::ShiftOpc::lsl, MIB); 1019 } 1020 case G_GEP: 1021 I.setDesc(TII.get(Opcodes.ADDrr)); 1022 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 1023 break; 1024 case G_FRAME_INDEX: 1025 // Add 0 to the given frame index and hope it will eventually be folded into 1026 // the user(s). 1027 I.setDesc(TII.get(Opcodes.ADDri)); 1028 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); 1029 break; 1030 case G_GLOBAL_VALUE: 1031 return selectGlobal(MIB, MRI); 1032 case G_STORE: 1033 case G_LOAD: { 1034 const auto &MemOp = **I.memoperands_begin(); 1035 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { 1036 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n"); 1037 return false; 1038 } 1039 1040 unsigned Reg = I.getOperand(0).getReg(); 1041 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID(); 1042 1043 LLT ValTy = MRI.getType(Reg); 1044 const auto ValSize = ValTy.getSizeInBits(); 1045 1046 assert((ValSize != 64 || STI.hasVFP2()) && 1047 "Don't know how to load/store 64-bit value without VFP"); 1048 1049 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize); 1050 if (NewOpc == G_LOAD || NewOpc == G_STORE) 1051 return false; 1052 1053 I.setDesc(TII.get(NewOpc)); 1054 1055 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH) 1056 // LDRH has a funny addressing mode (there's already a FIXME for it). 1057 MIB.addReg(0); 1058 MIB.addImm(0).add(predOps(ARMCC::AL)); 1059 break; 1060 } 1061 case G_MERGE_VALUES: { 1062 if (!selectMergeValues(MIB, TII, MRI, TRI, RBI)) 1063 return false; 1064 break; 1065 } 1066 case G_UNMERGE_VALUES: { 1067 if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI)) 1068 return false; 1069 break; 1070 } 1071 case G_BRCOND: { 1072 if (!validReg(MRI, I.getOperand(0).getReg(), 1, ARM::GPRRegBankID)) { 1073 LLVM_DEBUG(dbgs() << "Unsupported condition register for G_BRCOND"); 1074 return false; 1075 } 1076 1077 // Set the flags. 1078 auto Test = 1079 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcodes.TSTri)) 1080 .addReg(I.getOperand(0).getReg()) 1081 .addImm(1) 1082 .add(predOps(ARMCC::AL)); 1083 if (!constrainSelectedInstRegOperands(*Test, TII, TRI, RBI)) 1084 return false; 1085 1086 // Branch conditionally. 1087 auto Branch = 1088 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcodes.Bcc)) 1089 .add(I.getOperand(1)) 1090 .add(predOps(ARMCC::NE, ARM::CPSR)); 1091 if (!constrainSelectedInstRegOperands(*Branch, TII, TRI, RBI)) 1092 return false; 1093 I.eraseFromParent(); 1094 return true; 1095 } 1096 case G_PHI: { 1097 I.setDesc(TII.get(PHI)); 1098 1099 unsigned DstReg = I.getOperand(0).getReg(); 1100 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI); 1101 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { 1102 break; 1103 } 1104 1105 return true; 1106 } 1107 default: 1108 return false; 1109 } 1110 1111 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 1112 } 1113