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