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 const ARMBaseInstrInfo &TII; 46 const ARMBaseRegisterInfo &TRI; 47 const ARMBaseTargetMachine &TM; 48 const ARMRegisterBankInfo &RBI; 49 const ARMSubtarget &STI; 50 51 #define GET_GLOBALISEL_PREDICATES_DECL 52 #include "ARMGenGlobalISel.inc" 53 #undef GET_GLOBALISEL_PREDICATES_DECL 54 55 // We declare the temporaries used by selectImpl() in the class to minimize the 56 // cost of constructing placeholder values. 57 #define GET_GLOBALISEL_TEMPORARIES_DECL 58 #include "ARMGenGlobalISel.inc" 59 #undef GET_GLOBALISEL_TEMPORARIES_DECL 60 }; 61 } // end anonymous namespace 62 63 namespace llvm { 64 InstructionSelector * 65 createARMInstructionSelector(const ARMBaseTargetMachine &TM, 66 const ARMSubtarget &STI, 67 const ARMRegisterBankInfo &RBI) { 68 return new ARMInstructionSelector(TM, STI, RBI); 69 } 70 } 71 72 unsigned zero_reg = 0; 73 74 #define GET_GLOBALISEL_IMPL 75 #include "ARMGenGlobalISel.inc" 76 #undef GET_GLOBALISEL_IMPL 77 78 ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM, 79 const ARMSubtarget &STI, 80 const ARMRegisterBankInfo &RBI) 81 : InstructionSelector(), TII(*STI.getInstrInfo()), 82 TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), 83 #define GET_GLOBALISEL_PREDICATES_INIT 84 #include "ARMGenGlobalISel.inc" 85 #undef GET_GLOBALISEL_PREDICATES_INIT 86 #define GET_GLOBALISEL_TEMPORARIES_INIT 87 #include "ARMGenGlobalISel.inc" 88 #undef GET_GLOBALISEL_TEMPORARIES_INIT 89 { 90 } 91 92 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, 93 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 94 const RegisterBankInfo &RBI) { 95 unsigned DstReg = I.getOperand(0).getReg(); 96 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) 97 return true; 98 99 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI); 100 (void)RegBank; 101 assert(RegBank && "Can't get reg bank for virtual register"); 102 103 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); 104 (void)DstSize; 105 unsigned SrcReg = I.getOperand(1).getReg(); 106 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI); 107 (void)SrcSize; 108 // We use copies for trunc, so it's ok for the size of the destination to be 109 // smaller (the higher bits will just be undefined). 110 assert(DstSize <= SrcSize && "Copy with different width?!"); 111 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 selectSequence(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 sequence without VFP"); 144 145 // We only support G_SEQUENCE 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_SEQUENCE"); 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_SEQUENCE"); 157 unsigned VReg2 = MIB->getOperand(3).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_SEQUENCE"); 162 163 // Remove the operands corresponding to the offsets. 164 MIB->RemoveOperand(4); 165 MIB->RemoveOperand(2); 166 167 MIB->setDesc(TII.get(ARM::VMOVDRR)); 168 MIB.add(predOps(ARMCC::AL)); 169 170 return true; 171 } 172 173 static bool selectExtract(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, 174 MachineRegisterInfo &MRI, 175 const TargetRegisterInfo &TRI, 176 const RegisterBankInfo &RBI) { 177 assert(TII.getSubtarget().hasVFP2() && "Can't select extract without VFP"); 178 179 // We only support G_EXTRACT as a way to break up one DPR into two GPRs. 180 unsigned VReg0 = MIB->getOperand(0).getReg(); 181 (void)VReg0; 182 assert(MRI.getType(VReg0).getSizeInBits() == 32 && 183 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID && 184 "Unsupported operand for G_EXTRACT"); 185 unsigned VReg1 = MIB->getOperand(1).getReg(); 186 (void)VReg1; 187 assert(MRI.getType(VReg1).getSizeInBits() == 64 && 188 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::FPRRegBankID && 189 "Unsupported operand for G_EXTRACT"); 190 assert(MIB->getOperand(2).getImm() % 32 == 0 && 191 "Unsupported operand for G_EXTRACT"); 192 193 // Remove the operands corresponding to the offsets. 194 MIB->getOperand(2).setImm(MIB->getOperand(2).getImm() / 32); 195 196 MIB->setDesc(TII.get(ARM::VGETLNi32)); 197 MIB.add(predOps(ARMCC::AL)); 198 199 return true; 200 } 201 202 /// Select the opcode for simple extensions (that translate to a single SXT/UXT 203 /// instruction). Extension operations more complicated than that should not 204 /// invoke this. Returns the original opcode if it doesn't know how to select a 205 /// better one. 206 static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) { 207 using namespace TargetOpcode; 208 209 if (Size != 8 && Size != 16) 210 return Opc; 211 212 if (Opc == G_SEXT) 213 return Size == 8 ? ARM::SXTB : ARM::SXTH; 214 215 if (Opc == G_ZEXT) 216 return Size == 8 ? ARM::UXTB : ARM::UXTH; 217 218 return Opc; 219 } 220 221 /// Select the opcode for simple loads and stores. For types smaller than 32 222 /// bits, the value will be zero extended. Returns the original opcode if it 223 /// doesn't know how to select a better one. 224 static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank, 225 unsigned Size) { 226 bool isStore = Opc == TargetOpcode::G_STORE; 227 228 if (RegBank == ARM::GPRRegBankID) { 229 switch (Size) { 230 case 1: 231 case 8: 232 return isStore ? ARM::STRBi12 : ARM::LDRBi12; 233 case 16: 234 return isStore ? ARM::STRH : ARM::LDRH; 235 case 32: 236 return isStore ? ARM::STRi12 : ARM::LDRi12; 237 default: 238 return Opc; 239 } 240 } 241 242 if (RegBank == ARM::FPRRegBankID) { 243 switch (Size) { 244 case 32: 245 return isStore ? ARM::VSTRS : ARM::VLDRS; 246 case 64: 247 return isStore ? ARM::VSTRD : ARM::VLDRD; 248 default: 249 return Opc; 250 } 251 } 252 253 return Opc; 254 } 255 256 bool ARMInstructionSelector::select(MachineInstr &I) const { 257 assert(I.getParent() && "Instruction should be in a basic block!"); 258 assert(I.getParent()->getParent() && "Instruction should be in a function!"); 259 260 auto &MBB = *I.getParent(); 261 auto &MF = *MBB.getParent(); 262 auto &MRI = MF.getRegInfo(); 263 264 if (!isPreISelGenericOpcode(I.getOpcode())) { 265 if (I.isCopy()) 266 return selectCopy(I, TII, MRI, TRI, RBI); 267 268 return true; 269 } 270 271 if (selectImpl(I)) 272 return true; 273 274 MachineInstrBuilder MIB{MF, I}; 275 bool isSExt = false; 276 277 using namespace TargetOpcode; 278 switch (I.getOpcode()) { 279 case G_SEXT: 280 isSExt = true; 281 LLVM_FALLTHROUGH; 282 case G_ZEXT: { 283 LLT DstTy = MRI.getType(I.getOperand(0).getReg()); 284 // FIXME: Smaller destination sizes coming soon! 285 if (DstTy.getSizeInBits() != 32) { 286 DEBUG(dbgs() << "Unsupported destination size for extension"); 287 return false; 288 } 289 290 LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); 291 unsigned SrcSize = SrcTy.getSizeInBits(); 292 switch (SrcSize) { 293 case 1: { 294 // ZExt boils down to & 0x1; for SExt we also subtract that from 0 295 I.setDesc(TII.get(ARM::ANDri)); 296 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); 297 298 if (isSExt) { 299 unsigned SExtResult = I.getOperand(0).getReg(); 300 301 // Use a new virtual register for the result of the AND 302 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); 303 I.getOperand(0).setReg(AndResult); 304 305 auto InsertBefore = std::next(I.getIterator()); 306 auto SubI = 307 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri)) 308 .addDef(SExtResult) 309 .addUse(AndResult) 310 .addImm(0) 311 .add(predOps(ARMCC::AL)) 312 .add(condCodeOp()); 313 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) 314 return false; 315 } 316 break; 317 } 318 case 8: 319 case 16: { 320 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); 321 if (NewOpc == I.getOpcode()) 322 return false; 323 I.setDesc(TII.get(NewOpc)); 324 MIB.addImm(0).add(predOps(ARMCC::AL)); 325 break; 326 } 327 default: 328 DEBUG(dbgs() << "Unsupported source size for extension"); 329 return false; 330 } 331 break; 332 } 333 case G_TRUNC: { 334 // The high bits are undefined, so there's nothing special to do, just 335 // treat it as a copy. 336 auto SrcReg = I.getOperand(1).getReg(); 337 auto DstReg = I.getOperand(0).getReg(); 338 339 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); 340 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); 341 342 if (SrcRegBank.getID() != DstRegBank.getID()) { 343 DEBUG(dbgs() << "G_TRUNC operands on different register banks\n"); 344 return false; 345 } 346 347 if (SrcRegBank.getID() != ARM::GPRRegBankID) { 348 DEBUG(dbgs() << "G_TRUNC on non-GPR not supported yet\n"); 349 return false; 350 } 351 352 I.setDesc(TII.get(COPY)); 353 return selectCopy(I, TII, MRI, TRI, RBI); 354 } 355 case G_ADD: 356 case G_GEP: 357 I.setDesc(TII.get(ARM::ADDrr)); 358 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 359 break; 360 case G_SUB: 361 I.setDesc(TII.get(ARM::SUBrr)); 362 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 363 break; 364 case G_MUL: 365 if (TII.getSubtarget().hasV6Ops()) { 366 I.setDesc(TII.get(ARM::MUL)); 367 } else { 368 assert(TII.getSubtarget().useMulOps() && "Unsupported target"); 369 I.setDesc(TII.get(ARM::MULv5)); 370 MIB->getOperand(0).setIsEarlyClobber(true); 371 } 372 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 373 break; 374 case G_FRAME_INDEX: 375 // Add 0 to the given frame index and hope it will eventually be folded into 376 // the user(s). 377 I.setDesc(TII.get(ARM::ADDri)); 378 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); 379 break; 380 case G_CONSTANT: { 381 unsigned Reg = I.getOperand(0).getReg(); 382 if (MRI.getType(Reg).getSizeInBits() != 32) 383 return false; 384 385 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID && 386 "Expected constant to live in a GPR"); 387 I.setDesc(TII.get(ARM::MOVi)); 388 MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); 389 390 auto &Val = I.getOperand(1); 391 if (Val.isCImm()) { 392 if (Val.getCImm()->getBitWidth() > 32) 393 return false; 394 Val.ChangeToImmediate(Val.getCImm()->getZExtValue()); 395 } 396 397 if (!Val.isImm()) { 398 return false; 399 } 400 401 break; 402 } 403 case G_STORE: 404 case G_LOAD: { 405 const auto &MemOp = **I.memoperands_begin(); 406 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { 407 DEBUG(dbgs() << "Atomic load/store not supported yet\n"); 408 return false; 409 } 410 411 unsigned Reg = I.getOperand(0).getReg(); 412 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID(); 413 414 LLT ValTy = MRI.getType(Reg); 415 const auto ValSize = ValTy.getSizeInBits(); 416 417 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) && 418 "Don't know how to load/store 64-bit value without VFP"); 419 420 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize); 421 if (NewOpc == G_LOAD || NewOpc == G_STORE) 422 return false; 423 424 I.setDesc(TII.get(NewOpc)); 425 426 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH) 427 // LDRH has a funny addressing mode (there's already a FIXME for it). 428 MIB.addReg(0); 429 MIB.addImm(0).add(predOps(ARMCC::AL)); 430 break; 431 } 432 case G_SEQUENCE: { 433 if (!selectSequence(MIB, TII, MRI, TRI, RBI)) 434 return false; 435 break; 436 } 437 case G_EXTRACT: { 438 if (!selectExtract(MIB, TII, MRI, TRI, RBI)) 439 return false; 440 break; 441 } 442 default: 443 return false; 444 } 445 446 return constrainSelectedInstRegOperands(I, TII, TRI, RBI); 447 } 448