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 const ARMBaseInstrInfo &TII;
46 const ARMBaseRegisterInfo &TRI;
47 const ARMBaseTargetMachine &TM;
48 const ARMRegisterBankInfo &RBI;
49 const ARMSubtarget &STI;
51 #define GET_GLOBALISEL_PREDICATES_DECL
52 #include "ARMGenGlobalISel.inc"
53 #undef GET_GLOBALISEL_PREDICATES_DECL
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
61 } // end anonymous namespace
65 createARMInstructionSelector(const ARMBaseTargetMachine &TM,
66 const ARMSubtarget &STI,
67 const ARMRegisterBankInfo &RBI) {
68 return new ARMInstructionSelector(TM, STI, RBI);
72 unsigned zero_reg = 0;
74 #define GET_GLOBALISEL_IMPL
75 #include "ARMGenGlobalISel.inc"
76 #undef GET_GLOBALISEL_IMPL
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
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))
99 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI);
101 assert(RegBank && "Can't get reg bank for virtual register");
103 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
104 assert((RegBank->getID() == ARM::GPRRegBankID ||
105 RegBank->getID() == ARM::FPRRegBankID) &&
106 "Unsupported reg bank");
108 const TargetRegisterClass *RC = &ARM::GPRRegClass;
110 if (RegBank->getID() == ARM::FPRRegBankID) {
112 RC = &ARM::SPRRegClass;
113 else if (DstSize == 64)
114 RC = &ARM::DPRRegClass;
116 llvm_unreachable("Unsupported destination size");
119 // No need to constrain SrcReg. It will get constrained when
120 // we hit another of its uses or its defs.
121 // Copies do not have constraints.
122 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
123 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
130 static bool selectMergeValues(MachineInstrBuilder &MIB,
131 const ARMBaseInstrInfo &TII,
132 MachineRegisterInfo &MRI,
133 const TargetRegisterInfo &TRI,
134 const RegisterBankInfo &RBI) {
135 assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP");
137 // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs
139 unsigned VReg0 = MIB->getOperand(0).getReg();
141 assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
142 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
143 "Unsupported operand for G_MERGE_VALUES");
144 unsigned VReg1 = MIB->getOperand(1).getReg();
146 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
147 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
148 "Unsupported operand for G_MERGE_VALUES");
149 unsigned VReg2 = MIB->getOperand(2).getReg();
151 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
152 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
153 "Unsupported operand for G_MERGE_VALUES");
155 MIB->setDesc(TII.get(ARM::VMOVDRR));
156 MIB.add(predOps(ARMCC::AL));
161 static bool selectUnmergeValues(MachineInstrBuilder &MIB,
162 const ARMBaseInstrInfo &TII,
163 MachineRegisterInfo &MRI,
164 const TargetRegisterInfo &TRI,
165 const RegisterBankInfo &RBI) {
166 assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP");
168 // We only support G_UNMERGE_VALUES as a way to break up one DPR into two
170 unsigned VReg0 = MIB->getOperand(0).getReg();
172 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
173 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
174 "Unsupported operand for G_UNMERGE_VALUES");
175 unsigned VReg1 = MIB->getOperand(1).getReg();
177 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
178 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
179 "Unsupported operand for G_UNMERGE_VALUES");
180 unsigned VReg2 = MIB->getOperand(2).getReg();
182 assert(MRI.getType(VReg2).getSizeInBits() == 64 &&
183 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID &&
184 "Unsupported operand for G_UNMERGE_VALUES");
186 MIB->setDesc(TII.get(ARM::VMOVRRD));
187 MIB.add(predOps(ARMCC::AL));
192 /// Select the opcode for simple extensions (that translate to a single SXT/UXT
193 /// instruction). Extension operations more complicated than that should not
194 /// invoke this. Returns the original opcode if it doesn't know how to select a
196 static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
197 using namespace TargetOpcode;
199 if (Size != 8 && Size != 16)
203 return Size == 8 ? ARM::SXTB : ARM::SXTH;
206 return Size == 8 ? ARM::UXTB : ARM::UXTH;
211 /// Select the opcode for simple loads and stores. For types smaller than 32
212 /// bits, the value will be zero extended. Returns the original opcode if it
213 /// doesn't know how to select a better one.
214 static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
216 bool isStore = Opc == TargetOpcode::G_STORE;
218 if (RegBank == ARM::GPRRegBankID) {
222 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
224 return isStore ? ARM::STRH : ARM::LDRH;
226 return isStore ? ARM::STRi12 : ARM::LDRi12;
232 if (RegBank == ARM::FPRRegBankID) {
235 return isStore ? ARM::VSTRS : ARM::VLDRS;
237 return isStore ? ARM::VSTRD : ARM::VLDRD;
246 bool ARMInstructionSelector::select(MachineInstr &I) const {
247 assert(I.getParent() && "Instruction should be in a basic block!");
248 assert(I.getParent()->getParent() && "Instruction should be in a function!");
250 auto &MBB = *I.getParent();
251 auto &MF = *MBB.getParent();
252 auto &MRI = MF.getRegInfo();
254 if (!isPreISelGenericOpcode(I.getOpcode())) {
256 return selectCopy(I, TII, MRI, TRI, RBI);
264 MachineInstrBuilder MIB{MF, I};
267 using namespace TargetOpcode;
268 switch (I.getOpcode()) {
273 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
274 // FIXME: Smaller destination sizes coming soon!
275 if (DstTy.getSizeInBits() != 32) {
276 DEBUG(dbgs() << "Unsupported destination size for extension");
280 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
281 unsigned SrcSize = SrcTy.getSizeInBits();
284 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
285 I.setDesc(TII.get(ARM::ANDri));
286 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
289 unsigned SExtResult = I.getOperand(0).getReg();
291 // Use a new virtual register for the result of the AND
292 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
293 I.getOperand(0).setReg(AndResult);
295 auto InsertBefore = std::next(I.getIterator());
297 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
301 .add(predOps(ARMCC::AL))
303 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
310 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
311 if (NewOpc == I.getOpcode())
313 I.setDesc(TII.get(NewOpc));
314 MIB.addImm(0).add(predOps(ARMCC::AL));
318 DEBUG(dbgs() << "Unsupported source size for extension");
325 // The high bits are undefined, so there's nothing special to do, just
326 // treat it as a copy.
327 auto SrcReg = I.getOperand(1).getReg();
328 auto DstReg = I.getOperand(0).getReg();
330 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
331 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
333 if (SrcRegBank.getID() != DstRegBank.getID()) {
334 DEBUG(dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n");
338 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
339 DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n");
343 I.setDesc(TII.get(COPY));
344 return selectCopy(I, TII, MRI, TRI, RBI);
347 I.setDesc(TII.get(ARM::ADDrr));
348 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
351 // Add 0 to the given frame index and hope it will eventually be folded into
353 I.setDesc(TII.get(ARM::ADDri));
354 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
357 unsigned Reg = I.getOperand(0).getReg();
358 if (MRI.getType(Reg).getSizeInBits() != 32)
361 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
362 "Expected constant to live in a GPR");
363 I.setDesc(TII.get(ARM::MOVi));
364 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
366 auto &Val = I.getOperand(1);
368 if (Val.getCImm()->getBitWidth() > 32)
370 Val.ChangeToImmediate(Val.getCImm()->getZExtValue());
381 const auto &MemOp = **I.memoperands_begin();
382 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
383 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
387 unsigned Reg = I.getOperand(0).getReg();
388 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
390 LLT ValTy = MRI.getType(Reg);
391 const auto ValSize = ValTy.getSizeInBits();
393 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
394 "Don't know how to load/store 64-bit value without VFP");
396 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
397 if (NewOpc == G_LOAD || NewOpc == G_STORE)
400 I.setDesc(TII.get(NewOpc));
402 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
403 // LDRH has a funny addressing mode (there's already a FIXME for it).
405 MIB.addImm(0).add(predOps(ARMCC::AL));
408 case G_MERGE_VALUES: {
409 if (!selectMergeValues(MIB, TII, MRI, TRI, RBI))
413 case G_UNMERGE_VALUES: {
414 if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI))
422 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);