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();
105 unsigned SrcReg = I.getOperand(1).getReg();
106 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
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?!");
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 selectFAdd(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
139 MachineRegisterInfo &MRI) {
140 assert(TII.getSubtarget().hasVFP2() && "Can't select fp add without vfp");
142 LLT Ty = MRI.getType(MIB->getOperand(0).getReg());
143 unsigned ValSize = Ty.getSizeInBits();
146 if (TII.getSubtarget().useNEONForSinglePrecisionFP())
148 MIB->setDesc(TII.get(ARM::VADDS));
150 assert(ValSize == 64 && "Unsupported size for floating point value");
151 if (TII.getSubtarget().isFPOnlySP())
153 MIB->setDesc(TII.get(ARM::VADDD));
155 MIB.add(predOps(ARMCC::AL));
160 static bool selectSequence(MachineInstrBuilder &MIB,
161 const ARMBaseInstrInfo &TII,
162 MachineRegisterInfo &MRI,
163 const TargetRegisterInfo &TRI,
164 const RegisterBankInfo &RBI) {
165 assert(TII.getSubtarget().hasVFP2() && "Can't select sequence without VFP");
167 // We only support G_SEQUENCE as a way to stick together two scalar GPRs
169 unsigned VReg0 = MIB->getOperand(0).getReg();
171 assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
172 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
173 "Unsupported operand for G_SEQUENCE");
174 unsigned VReg1 = MIB->getOperand(1).getReg();
176 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
177 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
178 "Unsupported operand for G_SEQUENCE");
179 unsigned VReg2 = MIB->getOperand(3).getReg();
181 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
182 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
183 "Unsupported operand for G_SEQUENCE");
185 // Remove the operands corresponding to the offsets.
186 MIB->RemoveOperand(4);
187 MIB->RemoveOperand(2);
189 MIB->setDesc(TII.get(ARM::VMOVDRR));
190 MIB.add(predOps(ARMCC::AL));
195 static bool selectExtract(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
196 MachineRegisterInfo &MRI,
197 const TargetRegisterInfo &TRI,
198 const RegisterBankInfo &RBI) {
199 assert(TII.getSubtarget().hasVFP2() && "Can't select extract without VFP");
201 // We only support G_EXTRACT as a way to break up one DPR into two GPRs.
202 unsigned VReg0 = MIB->getOperand(0).getReg();
204 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
205 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
206 "Unsupported operand for G_EXTRACT");
207 unsigned VReg1 = MIB->getOperand(1).getReg();
209 assert(MRI.getType(VReg1).getSizeInBits() == 64 &&
210 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::FPRRegBankID &&
211 "Unsupported operand for G_EXTRACT");
212 assert(MIB->getOperand(2).getImm() % 32 == 0 &&
213 "Unsupported operand for G_EXTRACT");
215 // Remove the operands corresponding to the offsets.
216 MIB->getOperand(2).setImm(MIB->getOperand(2).getImm() / 32);
218 MIB->setDesc(TII.get(ARM::VGETLNi32));
219 MIB.add(predOps(ARMCC::AL));
224 /// Select the opcode for simple extensions (that translate to a single SXT/UXT
225 /// instruction). Extension operations more complicated than that should not
226 /// invoke this. Returns the original opcode if it doesn't know how to select a
228 static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
229 using namespace TargetOpcode;
231 if (Size != 8 && Size != 16)
235 return Size == 8 ? ARM::SXTB : ARM::SXTH;
238 return Size == 8 ? ARM::UXTB : ARM::UXTH;
243 /// Select the opcode for simple loads and stores. For types smaller than 32
244 /// bits, the value will be zero extended. Returns the original opcode if it
245 /// doesn't know how to select a better one.
246 static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
248 bool isStore = Opc == TargetOpcode::G_STORE;
250 if (RegBank == ARM::GPRRegBankID) {
254 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
256 return isStore ? ARM::STRH : ARM::LDRH;
258 return isStore ? ARM::STRi12 : ARM::LDRi12;
264 if (RegBank == ARM::FPRRegBankID) {
267 return isStore ? ARM::VSTRS : ARM::VLDRS;
269 return isStore ? ARM::VSTRD : ARM::VLDRD;
278 bool ARMInstructionSelector::select(MachineInstr &I) const {
279 assert(I.getParent() && "Instruction should be in a basic block!");
280 assert(I.getParent()->getParent() && "Instruction should be in a function!");
282 auto &MBB = *I.getParent();
283 auto &MF = *MBB.getParent();
284 auto &MRI = MF.getRegInfo();
286 if (!isPreISelGenericOpcode(I.getOpcode())) {
288 return selectCopy(I, TII, MRI, TRI, RBI);
296 MachineInstrBuilder MIB{MF, I};
299 using namespace TargetOpcode;
300 switch (I.getOpcode()) {
305 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
306 // FIXME: Smaller destination sizes coming soon!
307 if (DstTy.getSizeInBits() != 32) {
308 DEBUG(dbgs() << "Unsupported destination size for extension");
312 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
313 unsigned SrcSize = SrcTy.getSizeInBits();
316 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
317 I.setDesc(TII.get(ARM::ANDri));
318 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
321 unsigned SExtResult = I.getOperand(0).getReg();
323 // Use a new virtual register for the result of the AND
324 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
325 I.getOperand(0).setReg(AndResult);
327 auto InsertBefore = std::next(I.getIterator());
329 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
333 .add(predOps(ARMCC::AL))
335 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
342 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
343 if (NewOpc == I.getOpcode())
345 I.setDesc(TII.get(NewOpc));
346 MIB.addImm(0).add(predOps(ARMCC::AL));
350 DEBUG(dbgs() << "Unsupported source size for extension");
356 // The high bits are undefined, so there's nothing special to do, just
357 // treat it as a copy.
358 auto SrcReg = I.getOperand(1).getReg();
359 auto DstReg = I.getOperand(0).getReg();
361 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
362 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
364 if (SrcRegBank.getID() != DstRegBank.getID()) {
365 DEBUG(dbgs() << "G_TRUNC operands on different register banks\n");
369 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
370 DEBUG(dbgs() << "G_TRUNC on non-GPR not supported yet\n");
374 I.setDesc(TII.get(COPY));
375 return selectCopy(I, TII, MRI, TRI, RBI);
379 I.setDesc(TII.get(ARM::ADDrr));
380 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
383 I.setDesc(TII.get(ARM::SUBrr));
384 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
387 if (TII.getSubtarget().hasV6Ops()) {
388 I.setDesc(TII.get(ARM::MUL));
390 assert(TII.getSubtarget().useMulOps() && "Unsupported target");
391 I.setDesc(TII.get(ARM::MULv5));
392 MIB->getOperand(0).setIsEarlyClobber(true);
394 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
397 if (!selectFAdd(MIB, TII, MRI))
401 // Add 0 to the given frame index and hope it will eventually be folded into
403 I.setDesc(TII.get(ARM::ADDri));
404 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
407 unsigned Reg = I.getOperand(0).getReg();
408 if (MRI.getType(Reg).getSizeInBits() != 32)
411 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
412 "Expected constant to live in a GPR");
413 I.setDesc(TII.get(ARM::MOVi));
414 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
416 auto &Val = I.getOperand(1);
418 if (Val.getCImm()->getBitWidth() > 32)
420 Val.ChangeToImmediate(Val.getCImm()->getZExtValue());
431 const auto &MemOp = **I.memoperands_begin();
432 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
433 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
437 unsigned Reg = I.getOperand(0).getReg();
438 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
440 LLT ValTy = MRI.getType(Reg);
441 const auto ValSize = ValTy.getSizeInBits();
443 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
444 "Don't know how to load/store 64-bit value without VFP");
446 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
447 if (NewOpc == G_LOAD || NewOpc == G_STORE)
450 I.setDesc(TII.get(NewOpc));
452 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
453 // LDRH has a funny addressing mode (there's already a FIXME for it).
455 MIB.addImm(0).add(predOps(ARMCC::AL));
459 if (!selectSequence(MIB, TII, MRI, TRI, RBI))
464 if (!selectExtract(MIB, TII, MRI, TRI, RBI))
472 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);