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/MachineRegisterInfo.h" 19 #include "llvm/Support/Debug.h" 20 21 #define DEBUG_TYPE "arm-isel" 22 23 using namespace llvm; 24 25 #ifndef LLVM_BUILD_GLOBAL_ISEL 26 #error "You shouldn't build this" 27 #endif 28 29 namespace { 30 31 #define GET_GLOBALISEL_PREDICATE_BITSET 32 #include "ARMGenGlobalISel.inc" 33 #undef GET_GLOBALISEL_PREDICATE_BITSET 34 35 class ARMInstructionSelector : public InstructionSelector { 36 public: 37 ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI, 38 const ARMRegisterBankInfo &RBI); 39 40 bool select(MachineInstr &I) const override; 41 42 private: 43 bool selectImpl(MachineInstr &I) const; 44 45 bool selectICmp(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, 46 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 47 const RegisterBankInfo &RBI) const; 48 49 bool selectSelect(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, 50 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 51 const RegisterBankInfo &RBI) const; 52 53 const ARMBaseInstrInfo &TII; 54 const ARMBaseRegisterInfo &TRI; 55 const ARMBaseTargetMachine &TM; 56 const ARMRegisterBankInfo &RBI; 57 const ARMSubtarget &STI; 58 59 #define GET_GLOBALISEL_PREDICATES_DECL 60 #include "ARMGenGlobalISel.inc" 61 #undef GET_GLOBALISEL_PREDICATES_DECL 62 63 // We declare the temporaries used by selectImpl() in the class to minimize the 64 // cost of constructing placeholder values. 65 #define GET_GLOBALISEL_TEMPORARIES_DECL 66 #include "ARMGenGlobalISel.inc" 67 #undef GET_GLOBALISEL_TEMPORARIES_DECL 68 }; 69 } // end anonymous namespace 70 71 namespace llvm { 72 InstructionSelector * 73 createARMInstructionSelector(const ARMBaseTargetMachine &TM, 74 const ARMSubtarget &STI, 75 const ARMRegisterBankInfo &RBI) { 76 return new ARMInstructionSelector(TM, STI, RBI); 77 } 78 } 79 80 unsigned zero_reg = 0; 81 82 #define GET_GLOBALISEL_IMPL 83 #include "ARMGenGlobalISel.inc" 84 #undef GET_GLOBALISEL_IMPL 85 86 ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM, 87 const ARMSubtarget &STI, 88 const ARMRegisterBankInfo &RBI) 89 : InstructionSelector(), TII(*STI.getInstrInfo()), 90 TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), 91 #define GET_GLOBALISEL_PREDICATES_INIT 92 #include "ARMGenGlobalISel.inc" 93 #undef GET_GLOBALISEL_PREDICATES_INIT 94 #define GET_GLOBALISEL_TEMPORARIES_INIT 95 #include "ARMGenGlobalISel.inc" 96 #undef GET_GLOBALISEL_TEMPORARIES_INIT 97 { 98 } 99 100 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, 101 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 102 const RegisterBankInfo &RBI) { 103 unsigned DstReg = I.getOperand(0).getReg(); 104 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) 105 return true; 106 107 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI); 108 (void)RegBank; 109 assert(RegBank && "Can't get reg bank for virtual register"); 110 111 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); 112 assert((RegBank->getID() == ARM::GPRRegBankID || 113 RegBank->getID() == ARM::FPRRegBankID) && 114 "Unsupported reg bank"); 115 116 const TargetRegisterClass *RC = &ARM::GPRRegClass; 117 118 if (RegBank->getID() == ARM::FPRRegBankID) { 119 if (DstSize == 32) 120 RC = &ARM::SPRRegClass; 121 else if (DstSize == 64) 122 RC = &ARM::DPRRegClass; 123 else 124 llvm_unreachable("Unsupported destination size"); 125 } 126 127 // No need to constrain SrcReg. It will get constrained when 128 // we hit another of its uses or its defs. 129 // Copies do not have constraints. 130 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { 131 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) 132 << " operand\n"); 133 return false; 134 } 135 return true; 136 } 137 138 static bool selectMergeValues(MachineInstrBuilder &MIB, 139 const ARMBaseInstrInfo &TII, 140 MachineRegisterInfo &MRI, 141 const TargetRegisterInfo &TRI, 142 const RegisterBankInfo &RBI) { 143 assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP"); 144 145 // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs 146 // into one DPR. 147 unsigned VReg0 = MIB->getOperand(0).getReg(); 148 (void)VReg0; 149 assert(MRI.getType(VReg0).getSizeInBits() == 64 && 150 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID && 151 "Unsupported operand for G_MERGE_VALUES"); 152 unsigned VReg1 = MIB->getOperand(1).getReg(); 153 (void)VReg1; 154 assert(MRI.getType(VReg1).getSizeInBits() == 32 && 155 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && 156 "Unsupported operand for G_MERGE_VALUES"); 157 unsigned VReg2 = MIB->getOperand(2).getReg(); 158 (void)VReg2; 159 assert(MRI.getType(VReg2).getSizeInBits() == 32 && 160 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID && 161 "Unsupported operand for G_MERGE_VALUES"); 162 163 MIB->setDesc(TII.get(ARM::VMOVDRR)); 164 MIB.add(predOps(ARMCC::AL)); 165 166 return true; 167 } 168 169 static bool selectUnmergeValues(MachineInstrBuilder &MIB, 170 const ARMBaseInstrInfo &TII, 171 MachineRegisterInfo &MRI, 172 const TargetRegisterInfo &TRI, 173 const RegisterBankInfo &RBI) { 174 assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP"); 175 176 // We only support G_UNMERGE_VALUES as a way to break up one DPR into two 177 // GPRs. 178 unsigned VReg0 = MIB->getOperand(0).getReg(); 179 (void)VReg0; 180 assert(MRI.getType(VReg0).getSizeInBits() == 32 && 181 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID && 182 "Unsupported operand for G_UNMERGE_VALUES"); 183 unsigned VReg1 = MIB->getOperand(1).getReg(); 184 (void)VReg1; 185 assert(MRI.getType(VReg1).getSizeInBits() == 32 && 186 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && 187 "Unsupported operand for G_UNMERGE_VALUES"); 188 unsigned VReg2 = MIB->getOperand(2).getReg(); 189 (void)VReg2; 190 assert(MRI.getType(VReg2).getSizeInBits() == 64 && 191 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID && 192 "Unsupported operand for G_UNMERGE_VALUES"); 193 194 MIB->setDesc(TII.get(ARM::VMOVRRD)); 195 MIB.add(predOps(ARMCC::AL)); 196 197 return true; 198 } 199 200 /// Select the opcode for simple extensions (that translate to a single SXT/UXT 201 /// instruction). Extension operations more complicated than that should not 202 /// invoke this. Returns the original opcode if it doesn't know how to select a 203 /// better one. 204 static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) { 205 using namespace TargetOpcode; 206 207 if (Size != 8 && Size != 16) 208 return Opc; 209 210 if (Opc == G_SEXT) 211 return Size == 8 ? ARM::SXTB : ARM::SXTH; 212 213 if (Opc == G_ZEXT) 214 return Size == 8 ? ARM::UXTB : ARM::UXTH; 215 216 return Opc; 217 } 218 219 /// Select the opcode for simple loads and stores. For types smaller than 32 220 /// bits, the value will be zero extended. Returns the original opcode if it 221 /// doesn't know how to select a better one. 222 static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank, 223 unsigned Size) { 224 bool isStore = Opc == TargetOpcode::G_STORE; 225 226 if (RegBank == ARM::GPRRegBankID) { 227 switch (Size) { 228 case 1: 229 case 8: 230 return isStore ? ARM::STRBi12 : ARM::LDRBi12; 231 case 16: 232 return isStore ? ARM::STRH : ARM::LDRH; 233 case 32: 234 return isStore ? ARM::STRi12 : ARM::LDRi12; 235 default: 236 return Opc; 237 } 238 } 239 240 if (RegBank == ARM::FPRRegBankID) { 241 switch (Size) { 242 case 32: 243 return isStore ? ARM::VSTRS : ARM::VLDRS; 244 case 64: 245 return isStore ? ARM::VSTRD : ARM::VLDRD; 246 default: 247 return Opc; 248 } 249 } 250 251 return Opc; 252 } 253 254 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { 255 switch (Pred) { 256 // Needs two compares... 257 case CmpInst::FCMP_ONE: 258 case CmpInst::FCMP_UEQ: 259 default: 260 // AL is our "false" for now. The other two need more compares. 261 return ARMCC::AL; 262 case CmpInst::ICMP_EQ: 263 case CmpInst::FCMP_OEQ: 264 return ARMCC::EQ; 265 case CmpInst::ICMP_SGT: 266 case CmpInst::FCMP_OGT: 267 return ARMCC::GT; 268 case CmpInst::ICMP_SGE: 269 case CmpInst::FCMP_OGE: 270 return ARMCC::GE; 271 case CmpInst::ICMP_UGT: 272 case CmpInst::FCMP_UGT: 273 return ARMCC::HI; 274 case CmpInst::FCMP_OLT: 275 return ARMCC::MI; 276 case CmpInst::ICMP_ULE: 277 case CmpInst::FCMP_OLE: 278 return ARMCC::LS; 279 case CmpInst::FCMP_ORD: 280 return ARMCC::VC; 281 case CmpInst::FCMP_UNO: 282 return ARMCC::VS; 283 case CmpInst::FCMP_UGE: 284 return ARMCC::PL; 285 case CmpInst::ICMP_SLT: 286 case CmpInst::FCMP_ULT: 287 return ARMCC::LT; 288 case CmpInst::ICMP_SLE: 289 case CmpInst::FCMP_ULE: 290 return ARMCC::LE; 291 case CmpInst::FCMP_UNE: 292 case CmpInst::ICMP_NE: 293 return ARMCC::NE; 294 case CmpInst::ICMP_UGE: 295 return ARMCC::HS; 296 case CmpInst::ICMP_ULT: 297 return ARMCC::LO; 298 } 299 } 300 301 bool ARMInstructionSelector::selectICmp(MachineInstrBuilder &MIB, 302 const ARMBaseInstrInfo &TII, 303 MachineRegisterInfo &MRI, 304 const TargetRegisterInfo &TRI, 305 const RegisterBankInfo &RBI) const { 306 auto &MBB = *MIB->getParent(); 307 auto InsertBefore = std::next(MIB->getIterator()); 308 auto &DebugLoc = MIB->getDebugLoc(); 309 310 // Move 0 into the result register. 311 auto Mov0I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVi)) 312 .addDef(MRI.createVirtualRegister(&ARM::GPRRegClass)) 313 .addImm(0) 314 .add(predOps(ARMCC::AL)) 315 .add(condCodeOp()); 316 if (!constrainSelectedInstRegOperands(*Mov0I, TII, TRI, RBI)) 317 return false; 318 319 // Perform the comparison. 320 auto LHSReg = MIB->getOperand(2).getReg(); 321 auto RHSReg = MIB->getOperand(3).getReg(); 322 assert(MRI.getType(LHSReg) == MRI.getType(RHSReg) && 323 MRI.getType(LHSReg).getSizeInBits() == 32 && 324 MRI.getType(RHSReg).getSizeInBits() == 32 && 325 "Unsupported types for comparison operation"); 326 auto CmpI = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::CMPrr)) 327 .addUse(LHSReg) 328 .addUse(RHSReg) 329 .add(predOps(ARMCC::AL)); 330 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) 331 return false; 332 333 // Move 1 into the result register if the flags say so. 334 auto ResReg = MIB->getOperand(0).getReg(); 335 auto Cond = 336 static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate()); 337 auto ARMCond = getComparePred(Cond); 338 if (ARMCond == ARMCC::AL) 339 return false; 340 341 auto Mov1I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVCCi)) 342 .addDef(ResReg) 343 .addUse(Mov0I->getOperand(0).getReg()) 344 .addImm(1) 345 .add(predOps(ARMCond, ARM::CPSR)); 346 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) 347 return false; 348 349 MIB->eraseFromParent(); 350 return true; 351 } 352 353 bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB, 354 const ARMBaseInstrInfo &TII, 355 MachineRegisterInfo &MRI, 356 const TargetRegisterInfo &TRI, 357 const RegisterBankInfo &RBI) const { 358 auto &MBB = *MIB->getParent(); 359 auto InsertBefore = std::next(MIB->getIterator()); 360 auto &DebugLoc = MIB->getDebugLoc(); 361 362 // Compare the condition to 0. 363 auto CondReg = MIB->getOperand(1).getReg(); 364 assert(MRI.getType(CondReg).getSizeInBits() == 1 && 365 RBI.getRegBank(CondReg, MRI, TRI)->getID() == ARM::GPRRegBankID && 366 "Unsupported types for select operation"); 367 auto CmpI = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::CMPri)) 368 .addUse(CondReg) 369 .addImm(0) 370 .add(predOps(ARMCC::AL)); 371 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) 372 return false; 373 374 // Move a value into the result register based on the result of the 375 // comparison. 376 auto ResReg = MIB->getOperand(0).getReg(); 377 auto TrueReg = MIB->getOperand(2).getReg(); 378 auto FalseReg = MIB->getOperand(3).getReg(); 379 assert(MRI.getType(ResReg) == MRI.getType(TrueReg) && 380 MRI.getType(TrueReg) == MRI.getType(FalseReg) && 381 MRI.getType(FalseReg).getSizeInBits() == 32 && 382 RBI.getRegBank(TrueReg, MRI, TRI)->getID() == ARM::GPRRegBankID && 383 RBI.getRegBank(FalseReg, MRI, TRI)->getID() == ARM::GPRRegBankID && 384 "Unsupported types for select operation"); 385 auto Mov1I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVCCr)) 386 .addDef(ResReg) 387 .addUse(TrueReg) 388 .addUse(FalseReg) 389 .add(predOps(ARMCC::EQ, ARM::CPSR)); 390 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) 391 return false; 392 393 MIB->eraseFromParent(); 394 return true; 395 } 396 397 bool ARMInstructionSelector::select(MachineInstr &I) const { 398 assert(I.getParent() && "Instruction should be in a basic block!"); 399 assert(I.getParent()->getParent() && "Instruction should be in a function!"); 400 401 auto &MBB = *I.getParent(); 402 auto &MF = *MBB.getParent(); 403 auto &MRI = MF.getRegInfo(); 404 405 if (!isPreISelGenericOpcode(I.getOpcode())) { 406 if (I.isCopy()) 407 return selectCopy(I, TII, MRI, TRI, RBI); 408 409 return true; 410 } 411 412 if (selectImpl(I)) 413 return true; 414 415 MachineInstrBuilder MIB{MF, I}; 416 bool isSExt = false; 417 418 using namespace TargetOpcode; 419 switch (I.getOpcode()) { 420 case G_SEXT: 421 isSExt = true; 422 LLVM_FALLTHROUGH; 423 case G_ZEXT: { 424 LLT DstTy = MRI.getType(I.getOperand(0).getReg()); 425 // FIXME: Smaller destination sizes coming soon! 426 if (DstTy.getSizeInBits() != 32) { 427 DEBUG(dbgs() << "Unsupported destination size for extension"); 428 return false; 429 } 430 431 LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); 432 unsigned SrcSize = SrcTy.getSizeInBits(); 433 switch (SrcSize) { 434 case 1: { 435 // ZExt boils down to & 0x1; for SExt we also subtract that from 0 436 I.setDesc(TII.get(ARM::ANDri)); 437 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); 438 439 if (isSExt) { 440 unsigned SExtResult = I.getOperand(0).getReg(); 441 442 // Use a new virtual register for the result of the AND 443 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); 444 I.getOperand(0).setReg(AndResult); 445 446 auto InsertBefore = std::next(I.getIterator()); 447 auto SubI = 448 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri)) 449 .addDef(SExtResult) 450 .addUse(AndResult) 451 .addImm(0) 452 .add(predOps(ARMCC::AL)) 453 .add(condCodeOp()); 454 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) 455 return false; 456 } 457 break; 458 } 459 case 8: 460 case 16: { 461 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); 462 if (NewOpc == I.getOpcode()) 463 return false; 464 I.setDesc(TII.get(NewOpc)); 465 MIB.addImm(0).add(predOps(ARMCC::AL)); 466 break; 467 } 468 default: 469 DEBUG(dbgs() << "Unsupported source size for extension"); 470 return false; 471 } 472 break; 473 } 474 case G_ANYEXT: 475 case G_TRUNC: { 476 // The high bits are undefined, so there's nothing special to do, just 477 // treat it as a copy. 478 auto SrcReg = I.getOperand(1).getReg(); 479 auto DstReg = I.getOperand(0).getReg(); 480 481 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); 482 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); 483 484 if (SrcRegBank.getID() != DstRegBank.getID()) { 485 DEBUG(dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n"); 486 return false; 487 } 488 489 if (SrcRegBank.getID() != ARM::GPRRegBankID) { 490 DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n"); 491 return false; 492 } 493 494 I.setDesc(TII.get(COPY)); 495 return selectCopy(I, TII, MRI, TRI, RBI); 496 } 497 case G_ICMP: 498 return selectICmp(MIB, TII, MRI, TRI, RBI); 499 case G_SELECT: 500 return selectSelect(MIB, TII, MRI, TRI, RBI); 501 case G_GEP: 502 I.setDesc(TII.get(ARM::ADDrr)); 503 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 504 break; 505 case G_FRAME_INDEX: 506 // Add 0 to the given frame index and hope it will eventually be folded into 507 // the user(s). 508 I.setDesc(TII.get(ARM::ADDri)); 509 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); 510 break; 511 case G_CONSTANT: { 512 unsigned Reg = I.getOperand(0).getReg(); 513 if (MRI.getType(Reg).getSizeInBits() != 32) 514 return false; 515 516 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID && 517 "Expected constant to live in a GPR"); 518 I.setDesc(TII.get(ARM::MOVi)); 519 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 520 521 auto &Val = I.getOperand(1); 522 if (Val.isCImm()) { 523 if (Val.getCImm()->getBitWidth() > 32) 524 return false; 525 Val.ChangeToImmediate(Val.getCImm()->getZExtValue()); 526 } 527 528 if (!Val.isImm()) { 529 return false; 530 } 531 532 break; 533 } 534 case G_STORE: 535 case G_LOAD: { 536 const auto &MemOp = **I.memoperands_begin(); 537 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { 538 DEBUG(dbgs() << "Atomic load/store not supported yet\n"); 539 return false; 540 } 541 542 unsigned Reg = I.getOperand(0).getReg(); 543 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID(); 544 545 LLT ValTy = MRI.getType(Reg); 546 const auto ValSize = ValTy.getSizeInBits(); 547 548 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) && 549 "Don't know how to load/store 64-bit value without VFP"); 550 551 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize); 552 if (NewOpc == G_LOAD || NewOpc == G_STORE) 553 return false; 554 555 I.setDesc(TII.get(NewOpc)); 556 557 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH) 558 // LDRH has a funny addressing mode (there's already a FIXME for it). 559 MIB.addReg(0); 560 MIB.addImm(0).add(predOps(ARMCC::AL)); 561 break; 562 } 563 case G_MERGE_VALUES: { 564 if (!selectMergeValues(MIB, TII, MRI, TRI, RBI)) 565 return false; 566 break; 567 } 568 case G_UNMERGE_VALUES: { 569 if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI)) 570 return false; 571 break; 572 } 573 default: 574 return false; 575 } 576 577 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 578 } 579