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