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 "ARMInstructionSelector.h"
15 #include "ARMRegisterBankInfo.h"
16 #include "ARMSubtarget.h"
17 #include "ARMTargetMachine.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"
29 ARMInstructionSelector::ARMInstructionSelector(const ARMSubtarget &STI,
30 const ARMRegisterBankInfo &RBI)
31 : InstructionSelector(), TII(*STI.getInstrInfo()),
32 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
34 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
35 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
36 const RegisterBankInfo &RBI) {
37 unsigned DstReg = I.getOperand(0).getReg();
38 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
41 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI);
43 assert(RegBank && "Can't get reg bank for virtual register");
45 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
47 unsigned SrcReg = I.getOperand(1).getReg();
48 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
50 assert((DstSize == SrcSize ||
51 // Copies are a means to setup initial types, the number of
52 // bits may not exactly match.
53 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
54 DstSize <= SrcSize)) &&
55 "Copy with different width?!");
57 assert((RegBank->getID() == ARM::GPRRegBankID ||
58 RegBank->getID() == ARM::FPRRegBankID) &&
59 "Unsupported reg bank");
61 const TargetRegisterClass *RC = &ARM::GPRRegClass;
63 if (RegBank->getID() == ARM::FPRRegBankID) {
65 RC = &ARM::SPRRegClass;
66 else if (DstSize == 64)
67 RC = &ARM::DPRRegClass;
69 llvm_unreachable("Unsupported destination size");
72 // No need to constrain SrcReg. It will get constrained when
73 // we hit another of its uses or its defs.
74 // Copies do not have constraints.
75 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
76 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
83 static bool selectFAdd(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
84 MachineRegisterInfo &MRI) {
85 assert(TII.getSubtarget().hasVFP2() && "Can't select fp add without vfp");
87 LLT Ty = MRI.getType(MIB->getOperand(0).getReg());
88 unsigned ValSize = Ty.getSizeInBits();
91 if (TII.getSubtarget().useNEONForSinglePrecisionFP())
93 MIB->setDesc(TII.get(ARM::VADDS));
95 assert(ValSize == 64 && "Unsupported size for floating point value");
96 if (TII.getSubtarget().isFPOnlySP())
98 MIB->setDesc(TII.get(ARM::VADDD));
100 MIB.add(predOps(ARMCC::AL));
105 static bool selectSequence(MachineInstrBuilder &MIB,
106 const ARMBaseInstrInfo &TII,
107 MachineRegisterInfo &MRI,
108 const TargetRegisterInfo &TRI,
109 const RegisterBankInfo &RBI) {
110 assert(TII.getSubtarget().hasVFP2() && "Can't select sequence without VFP");
112 // We only support G_SEQUENCE as a way to stick together two scalar GPRs
114 unsigned VReg0 = MIB->getOperand(0).getReg();
116 assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
117 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
118 "Unsupported operand for G_SEQUENCE");
119 unsigned VReg1 = MIB->getOperand(1).getReg();
121 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
122 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
123 "Unsupported operand for G_SEQUENCE");
124 unsigned VReg2 = MIB->getOperand(3).getReg();
126 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
127 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
128 "Unsupported operand for G_SEQUENCE");
130 // Remove the operands corresponding to the offsets.
131 MIB->RemoveOperand(4);
132 MIB->RemoveOperand(2);
134 MIB->setDesc(TII.get(ARM::VMOVDRR));
135 MIB.add(predOps(ARMCC::AL));
140 static bool selectExtract(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
141 MachineRegisterInfo &MRI,
142 const TargetRegisterInfo &TRI,
143 const RegisterBankInfo &RBI) {
144 assert(TII.getSubtarget().hasVFP2() && "Can't select extract without VFP");
146 // We only support G_EXTRACT as a way to break up one DPR into two GPRs.
147 unsigned VReg0 = MIB->getOperand(0).getReg();
149 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
150 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
151 "Unsupported operand for G_EXTRACT");
152 unsigned VReg1 = MIB->getOperand(1).getReg();
154 assert(MRI.getType(VReg1).getSizeInBits() == 64 &&
155 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::FPRRegBankID &&
156 "Unsupported operand for G_EXTRACT");
157 assert(MIB->getOperand(2).getImm() % 32 == 0 &&
158 "Unsupported operand for G_EXTRACT");
160 // Remove the operands corresponding to the offsets.
161 MIB->getOperand(2).setImm(MIB->getOperand(2).getImm() / 32);
163 MIB->setDesc(TII.get(ARM::VGETLNi32));
164 MIB.add(predOps(ARMCC::AL));
169 /// Select the opcode for simple extensions (that translate to a single SXT/UXT
170 /// instruction). Extension operations more complicated than that should not
171 /// invoke this. Returns the original opcode if it doesn't know how to select a
173 static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
174 using namespace TargetOpcode;
176 if (Size != 8 && Size != 16)
180 return Size == 8 ? ARM::SXTB : ARM::SXTH;
183 return Size == 8 ? ARM::UXTB : ARM::UXTH;
188 /// Select the opcode for simple loads and stores. For types smaller than 32
189 /// bits, the value will be zero extended. Returns the original opcode if it
190 /// doesn't know how to select a better one.
191 static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
193 bool isStore = Opc == TargetOpcode::G_STORE;
195 if (RegBank == ARM::GPRRegBankID) {
199 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
201 return isStore ? ARM::STRH : ARM::LDRH;
203 return isStore ? ARM::STRi12 : ARM::LDRi12;
209 if (RegBank == ARM::FPRRegBankID) {
212 return isStore ? ARM::VSTRS : ARM::VLDRS;
214 return isStore ? ARM::VSTRD : ARM::VLDRD;
223 bool ARMInstructionSelector::select(MachineInstr &I) const {
224 assert(I.getParent() && "Instruction should be in a basic block!");
225 assert(I.getParent()->getParent() && "Instruction should be in a function!");
227 auto &MBB = *I.getParent();
228 auto &MF = *MBB.getParent();
229 auto &MRI = MF.getRegInfo();
231 if (!isPreISelGenericOpcode(I.getOpcode())) {
233 return selectCopy(I, TII, MRI, TRI, RBI);
238 MachineInstrBuilder MIB{MF, I};
241 using namespace TargetOpcode;
242 switch (I.getOpcode()) {
247 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
248 // FIXME: Smaller destination sizes coming soon!
249 if (DstTy.getSizeInBits() != 32) {
250 DEBUG(dbgs() << "Unsupported destination size for extension");
254 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
255 unsigned SrcSize = SrcTy.getSizeInBits();
258 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
259 I.setDesc(TII.get(ARM::ANDri));
260 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
263 unsigned SExtResult = I.getOperand(0).getReg();
265 // Use a new virtual register for the result of the AND
266 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
267 I.getOperand(0).setReg(AndResult);
269 auto InsertBefore = std::next(I.getIterator());
271 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
275 .add(predOps(ARMCC::AL))
277 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
284 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
285 if (NewOpc == I.getOpcode())
287 I.setDesc(TII.get(NewOpc));
288 MIB.addImm(0).add(predOps(ARMCC::AL));
292 DEBUG(dbgs() << "Unsupported source size for extension");
299 I.setDesc(TII.get(ARM::ADDrr));
300 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
303 I.setDesc(TII.get(ARM::SUBrr));
304 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
307 if (!selectFAdd(MIB, TII, MRI))
311 // Add 0 to the given frame index and hope it will eventually be folded into
313 I.setDesc(TII.get(ARM::ADDri));
314 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
317 unsigned Reg = I.getOperand(0).getReg();
318 if (MRI.getType(Reg).getSizeInBits() != 32)
321 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
322 "Expected constant to live in a GPR");
323 I.setDesc(TII.get(ARM::MOVi));
324 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
329 const auto &MemOp = **I.memoperands_begin();
330 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
331 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
335 unsigned Reg = I.getOperand(0).getReg();
336 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
338 LLT ValTy = MRI.getType(Reg);
339 const auto ValSize = ValTy.getSizeInBits();
341 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
342 "Don't know how to load/store 64-bit value without VFP");
344 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
345 if (NewOpc == G_LOAD || NewOpc == G_STORE)
348 I.setDesc(TII.get(NewOpc));
350 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
351 // LDRH has a funny addressing mode (there's already a FIXME for it).
353 MIB.addImm(0).add(predOps(ARMCC::AL));
357 if (!selectSequence(MIB, TII, MRI, TRI, RBI))
362 if (!selectExtract(MIB, TII, MRI, TRI, RBI))
370 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);