1 //===- ARMInstructionSelector.cpp ----------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements the targeting of the InstructionSelector class for ARM.
11 /// \todo This should be generated by TableGen.
12 //===----------------------------------------------------------------------===//
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"
21 #define DEBUG_TYPE "arm-isel"
25 #ifndef LLVM_BUILD_GLOBAL_ISEL
26 #error "You shouldn't build this"
31 #define GET_GLOBALISEL_PREDICATE_BITSET
32 #include "ARMGenGlobalISel.inc"
33 #undef GET_GLOBALISEL_PREDICATE_BITSET
35 class ARMInstructionSelector : public InstructionSelector {
37 ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI,
38 const ARMRegisterBankInfo &RBI);
40 bool select(MachineInstr &I) const override;
43 bool selectImpl(MachineInstr &I) const;
45 bool selectICmp(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
46 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
47 const RegisterBankInfo &RBI) const;
49 bool selectSelect(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
50 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
51 const RegisterBankInfo &RBI) const;
53 const ARMBaseInstrInfo &TII;
54 const ARMBaseRegisterInfo &TRI;
55 const ARMBaseTargetMachine &TM;
56 const ARMRegisterBankInfo &RBI;
57 const ARMSubtarget &STI;
59 #define GET_GLOBALISEL_PREDICATES_DECL
60 #include "ARMGenGlobalISel.inc"
61 #undef GET_GLOBALISEL_PREDICATES_DECL
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
69 } // end anonymous namespace
73 createARMInstructionSelector(const ARMBaseTargetMachine &TM,
74 const ARMSubtarget &STI,
75 const ARMRegisterBankInfo &RBI) {
76 return new ARMInstructionSelector(TM, STI, RBI);
80 unsigned zero_reg = 0;
82 #define GET_GLOBALISEL_IMPL
83 #include "ARMGenGlobalISel.inc"
84 #undef GET_GLOBALISEL_IMPL
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
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))
107 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI);
109 assert(RegBank && "Can't get reg bank for virtual register");
111 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
112 assert((RegBank->getID() == ARM::GPRRegBankID ||
113 RegBank->getID() == ARM::FPRRegBankID) &&
114 "Unsupported reg bank");
116 const TargetRegisterClass *RC = &ARM::GPRRegClass;
118 if (RegBank->getID() == ARM::FPRRegBankID) {
120 RC = &ARM::SPRRegClass;
121 else if (DstSize == 64)
122 RC = &ARM::DPRRegClass;
124 llvm_unreachable("Unsupported destination size");
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())
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");
145 // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs
147 unsigned VReg0 = MIB->getOperand(0).getReg();
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();
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();
159 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
160 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
161 "Unsupported operand for G_MERGE_VALUES");
163 MIB->setDesc(TII.get(ARM::VMOVDRR));
164 MIB.add(predOps(ARMCC::AL));
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");
176 // We only support G_UNMERGE_VALUES as a way to break up one DPR into two
178 unsigned VReg0 = MIB->getOperand(0).getReg();
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();
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();
190 assert(MRI.getType(VReg2).getSizeInBits() == 64 &&
191 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID &&
192 "Unsupported operand for G_UNMERGE_VALUES");
194 MIB->setDesc(TII.get(ARM::VMOVRRD));
195 MIB.add(predOps(ARMCC::AL));
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
204 static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
205 using namespace TargetOpcode;
207 if (Size != 8 && Size != 16)
211 return Size == 8 ? ARM::SXTB : ARM::SXTH;
214 return Size == 8 ? ARM::UXTB : ARM::UXTH;
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,
224 bool isStore = Opc == TargetOpcode::G_STORE;
226 if (RegBank == ARM::GPRRegBankID) {
230 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
232 return isStore ? ARM::STRH : ARM::LDRH;
234 return isStore ? ARM::STRi12 : ARM::LDRi12;
240 if (RegBank == ARM::FPRRegBankID) {
243 return isStore ? ARM::VSTRS : ARM::VLDRS;
245 return isStore ? ARM::VSTRD : ARM::VLDRD;
254 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
256 // Needs two compares...
257 case CmpInst::FCMP_ONE:
258 case CmpInst::FCMP_UEQ:
260 // AL is our "false" for now. The other two need more compares.
262 case CmpInst::ICMP_EQ:
263 case CmpInst::FCMP_OEQ:
265 case CmpInst::ICMP_SGT:
266 case CmpInst::FCMP_OGT:
268 case CmpInst::ICMP_SGE:
269 case CmpInst::FCMP_OGE:
271 case CmpInst::ICMP_UGT:
272 case CmpInst::FCMP_UGT:
274 case CmpInst::FCMP_OLT:
276 case CmpInst::ICMP_ULE:
277 case CmpInst::FCMP_OLE:
279 case CmpInst::FCMP_ORD:
281 case CmpInst::FCMP_UNO:
283 case CmpInst::FCMP_UGE:
285 case CmpInst::ICMP_SLT:
286 case CmpInst::FCMP_ULT:
288 case CmpInst::ICMP_SLE:
289 case CmpInst::FCMP_ULE:
291 case CmpInst::FCMP_UNE:
292 case CmpInst::ICMP_NE:
294 case CmpInst::ICMP_UGE:
296 case CmpInst::ICMP_ULT:
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();
310 // Move 0 into the result register.
311 auto Mov0I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVi))
312 .addDef(MRI.createVirtualRegister(&ARM::GPRRegClass))
314 .add(predOps(ARMCC::AL))
316 if (!constrainSelectedInstRegOperands(*Mov0I, TII, TRI, RBI))
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))
329 .add(predOps(ARMCC::AL));
330 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
333 // Move 1 into the result register if the flags say so.
334 auto ResReg = MIB->getOperand(0).getReg();
336 static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate());
337 auto ARMCond = getComparePred(Cond);
338 if (ARMCond == ARMCC::AL)
341 auto Mov1I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVCCi))
343 .addUse(Mov0I->getOperand(0).getReg())
345 .add(predOps(ARMCond, ARM::CPSR));
346 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
349 MIB->eraseFromParent();
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();
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))
370 .add(predOps(ARMCC::AL));
371 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
374 // Move a value into the result register based on the result of the
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))
389 .add(predOps(ARMCC::EQ, ARM::CPSR));
390 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
393 MIB->eraseFromParent();
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!");
401 auto &MBB = *I.getParent();
402 auto &MF = *MBB.getParent();
403 auto &MRI = MF.getRegInfo();
405 if (!isPreISelGenericOpcode(I.getOpcode())) {
407 return selectCopy(I, TII, MRI, TRI, RBI);
415 MachineInstrBuilder MIB{MF, I};
418 using namespace TargetOpcode;
419 switch (I.getOpcode()) {
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");
431 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
432 unsigned SrcSize = SrcTy.getSizeInBits();
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());
440 unsigned SExtResult = I.getOperand(0).getReg();
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);
446 auto InsertBefore = std::next(I.getIterator());
448 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
452 .add(predOps(ARMCC::AL))
454 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
461 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
462 if (NewOpc == I.getOpcode())
464 I.setDesc(TII.get(NewOpc));
465 MIB.addImm(0).add(predOps(ARMCC::AL));
469 DEBUG(dbgs() << "Unsupported source size for extension");
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();
481 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
482 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
484 if (SrcRegBank.getID() != DstRegBank.getID()) {
485 DEBUG(dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n");
489 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
490 DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n");
494 I.setDesc(TII.get(COPY));
495 return selectCopy(I, TII, MRI, TRI, RBI);
498 return selectICmp(MIB, TII, MRI, TRI, RBI);
500 return selectSelect(MIB, TII, MRI, TRI, RBI);
502 I.setDesc(TII.get(ARM::ADDrr));
503 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
506 // Add 0 to the given frame index and hope it will eventually be folded into
508 I.setDesc(TII.get(ARM::ADDri));
509 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
512 unsigned Reg = I.getOperand(0).getReg();
513 if (MRI.getType(Reg).getSizeInBits() != 32)
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());
521 auto &Val = I.getOperand(1);
523 if (Val.getCImm()->getBitWidth() > 32)
525 Val.ChangeToImmediate(Val.getCImm()->getZExtValue());
536 const auto &MemOp = **I.memoperands_begin();
537 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
538 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
542 unsigned Reg = I.getOperand(0).getReg();
543 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
545 LLT ValTy = MRI.getType(Reg);
546 const auto ValSize = ValTy.getSizeInBits();
548 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
549 "Don't know how to load/store 64-bit value without VFP");
551 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
552 if (NewOpc == G_LOAD || NewOpc == G_STORE)
555 I.setDesc(TII.get(NewOpc));
557 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
558 // LDRH has a funny addressing mode (there's already a FIXME for it).
560 MIB.addImm(0).add(predOps(ARMCC::AL));
563 case G_MERGE_VALUES: {
564 if (!selectMergeValues(MIB, TII, MRI, TRI, RBI))
568 case G_UNMERGE_VALUES: {
569 if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI))
577 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);