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 // We use copies for trunc, so it's ok for the size of the destination to be
51 // smaller (the higher bits will just be undefined).
52 assert(DstSize <= SrcSize && "Copy with different width?!");
54 assert((RegBank->getID() == ARM::GPRRegBankID ||
55 RegBank->getID() == ARM::FPRRegBankID) &&
56 "Unsupported reg bank");
58 const TargetRegisterClass *RC = &ARM::GPRRegClass;
60 if (RegBank->getID() == ARM::FPRRegBankID) {
62 RC = &ARM::SPRRegClass;
63 else if (DstSize == 64)
64 RC = &ARM::DPRRegClass;
66 llvm_unreachable("Unsupported destination size");
69 // No need to constrain SrcReg. It will get constrained when
70 // we hit another of its uses or its defs.
71 // Copies do not have constraints.
72 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
73 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
80 static bool selectFAdd(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
81 MachineRegisterInfo &MRI) {
82 assert(TII.getSubtarget().hasVFP2() && "Can't select fp add without vfp");
84 LLT Ty = MRI.getType(MIB->getOperand(0).getReg());
85 unsigned ValSize = Ty.getSizeInBits();
88 if (TII.getSubtarget().useNEONForSinglePrecisionFP())
90 MIB->setDesc(TII.get(ARM::VADDS));
92 assert(ValSize == 64 && "Unsupported size for floating point value");
93 if (TII.getSubtarget().isFPOnlySP())
95 MIB->setDesc(TII.get(ARM::VADDD));
97 MIB.add(predOps(ARMCC::AL));
102 static bool selectSequence(MachineInstrBuilder &MIB,
103 const ARMBaseInstrInfo &TII,
104 MachineRegisterInfo &MRI,
105 const TargetRegisterInfo &TRI,
106 const RegisterBankInfo &RBI) {
107 assert(TII.getSubtarget().hasVFP2() && "Can't select sequence without VFP");
109 // We only support G_SEQUENCE as a way to stick together two scalar GPRs
111 unsigned VReg0 = MIB->getOperand(0).getReg();
113 assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
114 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
115 "Unsupported operand for G_SEQUENCE");
116 unsigned VReg1 = MIB->getOperand(1).getReg();
118 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
119 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
120 "Unsupported operand for G_SEQUENCE");
121 unsigned VReg2 = MIB->getOperand(3).getReg();
123 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
124 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
125 "Unsupported operand for G_SEQUENCE");
127 // Remove the operands corresponding to the offsets.
128 MIB->RemoveOperand(4);
129 MIB->RemoveOperand(2);
131 MIB->setDesc(TII.get(ARM::VMOVDRR));
132 MIB.add(predOps(ARMCC::AL));
137 static bool selectExtract(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
138 MachineRegisterInfo &MRI,
139 const TargetRegisterInfo &TRI,
140 const RegisterBankInfo &RBI) {
141 assert(TII.getSubtarget().hasVFP2() && "Can't select extract without VFP");
143 // We only support G_EXTRACT as a way to break up one DPR into two GPRs.
144 unsigned VReg0 = MIB->getOperand(0).getReg();
146 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
147 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
148 "Unsupported operand for G_EXTRACT");
149 unsigned VReg1 = MIB->getOperand(1).getReg();
151 assert(MRI.getType(VReg1).getSizeInBits() == 64 &&
152 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::FPRRegBankID &&
153 "Unsupported operand for G_EXTRACT");
154 assert(MIB->getOperand(2).getImm() % 32 == 0 &&
155 "Unsupported operand for G_EXTRACT");
157 // Remove the operands corresponding to the offsets.
158 MIB->getOperand(2).setImm(MIB->getOperand(2).getImm() / 32);
160 MIB->setDesc(TII.get(ARM::VGETLNi32));
161 MIB.add(predOps(ARMCC::AL));
166 /// Select the opcode for simple extensions (that translate to a single SXT/UXT
167 /// instruction). Extension operations more complicated than that should not
168 /// invoke this. Returns the original opcode if it doesn't know how to select a
170 static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
171 using namespace TargetOpcode;
173 if (Size != 8 && Size != 16)
177 return Size == 8 ? ARM::SXTB : ARM::SXTH;
180 return Size == 8 ? ARM::UXTB : ARM::UXTH;
185 /// Select the opcode for simple loads and stores. For types smaller than 32
186 /// bits, the value will be zero extended. Returns the original opcode if it
187 /// doesn't know how to select a better one.
188 static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
190 bool isStore = Opc == TargetOpcode::G_STORE;
192 if (RegBank == ARM::GPRRegBankID) {
196 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
198 return isStore ? ARM::STRH : ARM::LDRH;
200 return isStore ? ARM::STRi12 : ARM::LDRi12;
206 if (RegBank == ARM::FPRRegBankID) {
209 return isStore ? ARM::VSTRS : ARM::VLDRS;
211 return isStore ? ARM::VSTRD : ARM::VLDRD;
220 bool ARMInstructionSelector::select(MachineInstr &I) const {
221 assert(I.getParent() && "Instruction should be in a basic block!");
222 assert(I.getParent()->getParent() && "Instruction should be in a function!");
224 auto &MBB = *I.getParent();
225 auto &MF = *MBB.getParent();
226 auto &MRI = MF.getRegInfo();
228 if (!isPreISelGenericOpcode(I.getOpcode())) {
230 return selectCopy(I, TII, MRI, TRI, RBI);
235 MachineInstrBuilder MIB{MF, I};
238 using namespace TargetOpcode;
239 switch (I.getOpcode()) {
244 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
245 // FIXME: Smaller destination sizes coming soon!
246 if (DstTy.getSizeInBits() != 32) {
247 DEBUG(dbgs() << "Unsupported destination size for extension");
251 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
252 unsigned SrcSize = SrcTy.getSizeInBits();
255 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
256 I.setDesc(TII.get(ARM::ANDri));
257 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
260 unsigned SExtResult = I.getOperand(0).getReg();
262 // Use a new virtual register for the result of the AND
263 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
264 I.getOperand(0).setReg(AndResult);
266 auto InsertBefore = std::next(I.getIterator());
268 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
272 .add(predOps(ARMCC::AL))
274 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
281 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
282 if (NewOpc == I.getOpcode())
284 I.setDesc(TII.get(NewOpc));
285 MIB.addImm(0).add(predOps(ARMCC::AL));
289 DEBUG(dbgs() << "Unsupported source size for extension");
295 // The high bits are undefined, so there's nothing special to do, just
296 // treat it as a copy.
297 auto SrcReg = I.getOperand(1).getReg();
298 auto DstReg = I.getOperand(0).getReg();
300 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
301 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
303 if (SrcRegBank.getID() != DstRegBank.getID()) {
304 DEBUG(dbgs() << "G_TRUNC operands on different register banks\n");
308 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
309 DEBUG(dbgs() << "G_TRUNC on non-GPR not supported yet\n");
313 I.setDesc(TII.get(COPY));
314 return selectCopy(I, TII, MRI, TRI, RBI);
318 I.setDesc(TII.get(ARM::ADDrr));
319 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
322 I.setDesc(TII.get(ARM::SUBrr));
323 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
326 if (TII.getSubtarget().hasV6Ops()) {
327 I.setDesc(TII.get(ARM::MUL));
329 assert(TII.getSubtarget().useMulOps() && "Unsupported target");
330 I.setDesc(TII.get(ARM::MULv5));
331 MIB->getOperand(0).setIsEarlyClobber(true);
333 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
336 assert(TII.getSubtarget().hasDivideInARMMode() && "Unsupported operation");
337 I.setDesc(TII.get(ARM::SDIV));
338 MIB.add(predOps(ARMCC::AL));
341 assert(TII.getSubtarget().hasDivideInARMMode() && "Unsupported operation");
342 I.setDesc(TII.get(ARM::UDIV));
343 MIB.add(predOps(ARMCC::AL));
346 if (!selectFAdd(MIB, TII, MRI))
350 // Add 0 to the given frame index and hope it will eventually be folded into
352 I.setDesc(TII.get(ARM::ADDri));
353 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
356 unsigned Reg = I.getOperand(0).getReg();
357 if (MRI.getType(Reg).getSizeInBits() != 32)
360 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
361 "Expected constant to live in a GPR");
362 I.setDesc(TII.get(ARM::MOVi));
363 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
365 auto &Val = I.getOperand(1);
367 if (Val.getCImm()->getBitWidth() > 32)
369 Val.ChangeToImmediate(Val.getCImm()->getZExtValue());
380 const auto &MemOp = **I.memoperands_begin();
381 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
382 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
386 unsigned Reg = I.getOperand(0).getReg();
387 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
389 LLT ValTy = MRI.getType(Reg);
390 const auto ValSize = ValTy.getSizeInBits();
392 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
393 "Don't know how to load/store 64-bit value without VFP");
395 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
396 if (NewOpc == G_LOAD || NewOpc == G_STORE)
399 I.setDesc(TII.get(NewOpc));
401 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
402 // LDRH has a funny addressing mode (there's already a FIXME for it).
404 MIB.addImm(0).add(predOps(ARMCC::AL));
408 if (!selectSequence(MIB, TII, MRI, TRI, RBI))
413 if (!selectExtract(MIB, TII, MRI, TRI, RBI))
421 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);