blob: 816596b85721440fd78bb34b2c753c349b9440b2 [file] [log] [blame]
Diana Picus22274932016-11-11 08:27:37 +00001//===- ARMInstructionSelector.cpp ----------------------------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the targeting of the InstructionSelector class for ARM.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
14#include "ARMInstructionSelector.h"
15#include "ARMRegisterBankInfo.h"
16#include "ARMSubtarget.h"
17#include "ARMTargetMachine.h"
Diana Picus812caee2016-12-16 12:54:46 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000019#include "llvm/Support/Debug.h"
20
21#define DEBUG_TYPE "arm-isel"
22
23using namespace llvm;
24
25#ifndef LLVM_BUILD_GLOBAL_ISEL
26#error "You shouldn't build this"
27#endif
28
Diana Picus895c6aa2016-11-15 16:42:10 +000029ARMInstructionSelector::ARMInstructionSelector(const ARMSubtarget &STI,
Diana Picus22274932016-11-11 08:27:37 +000030 const ARMRegisterBankInfo &RBI)
Diana Picus895c6aa2016-11-15 16:42:10 +000031 : InstructionSelector(), TII(*STI.getInstrInfo()),
Diana Picus812caee2016-12-16 12:54:46 +000032 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
Diana Picus22274932016-11-11 08:27:37 +000033
Diana Picus812caee2016-12-16 12:54:46 +000034static 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))
39 return true;
40
41 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI);
Benjamin Kramer24bf8682016-12-16 13:13:03 +000042 (void)RegBank;
Diana Picus812caee2016-12-16 12:54:46 +000043 assert(RegBank && "Can't get reg bank for virtual register");
44
Diana Picus36aa09f2016-12-19 14:07:50 +000045 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
Daniel Jasper24218d52016-12-19 14:24:22 +000046 (void)DstSize;
Diana Picus36aa09f2016-12-19 14:07:50 +000047 unsigned SrcReg = I.getOperand(1).getReg();
48 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
49 (void)SrcSize;
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)) &&
Benjamin Kramer24bf8682016-12-16 13:13:03 +000055 "Copy with different width?!");
Diana Picus812caee2016-12-16 12:54:46 +000056
Diana Picus4fa83c02017-02-08 13:23:04 +000057 assert((RegBank->getID() == ARM::GPRRegBankID ||
58 RegBank->getID() == ARM::FPRRegBankID) &&
59 "Unsupported reg bank");
60
Diana Picus812caee2016-12-16 12:54:46 +000061 const TargetRegisterClass *RC = &ARM::GPRRegClass;
62
Diana Picus4fa83c02017-02-08 13:23:04 +000063 if (RegBank->getID() == ARM::FPRRegBankID) {
Diana Picus6beef3c2017-02-16 12:19:52 +000064 if (DstSize == 32)
65 RC = &ARM::SPRRegClass;
66 else if (DstSize == 64)
67 RC = &ARM::DPRRegClass;
68 else
69 llvm_unreachable("Unsupported destination size");
Diana Picus4fa83c02017-02-08 13:23:04 +000070 }
71
Diana Picus812caee2016-12-16 12:54:46 +000072 // 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())
77 << " operand\n");
78 return false;
79 }
80 return true;
81}
82
Diana Picus6beef3c2017-02-16 12:19:52 +000083static bool selectFAdd(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
84 MachineRegisterInfo &MRI) {
85 assert(TII.getSubtarget().hasVFP2() && "Can't select fp add without vfp");
86
87 LLT Ty = MRI.getType(MIB->getOperand(0).getReg());
88 unsigned ValSize = Ty.getSizeInBits();
89
90 if (ValSize == 32) {
91 if (TII.getSubtarget().useNEONForSinglePrecisionFP())
92 return false;
93 MIB->setDesc(TII.get(ARM::VADDS));
94 } else {
95 assert(ValSize == 64 && "Unsupported size for floating point value");
96 if (TII.getSubtarget().isFPOnlySP())
97 return false;
98 MIB->setDesc(TII.get(ARM::VADDD));
99 }
100 MIB.add(predOps(ARMCC::AL));
101
102 return true;
103}
104
Diana Picusb1701e02017-02-16 12:19:57 +0000105static 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");
111
112 // We only support G_SEQUENCE as a way to stick together two scalar GPRs
113 // into one DPR.
114 unsigned VReg0 = MIB->getOperand(0).getReg();
115 (void)VReg0;
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();
120 (void)VReg1;
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();
125 (void)VReg2;
126 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
127 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
128 "Unsupported operand for G_SEQUENCE");
129
130 // Remove the operands corresponding to the offsets.
131 MIB->RemoveOperand(4);
132 MIB->RemoveOperand(2);
133
134 MIB->setDesc(TII.get(ARM::VMOVDRR));
135 MIB.add(predOps(ARMCC::AL));
136
137 return true;
138}
139
140static 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");
145
146 // We only support G_EXTRACT as a way to break up one DPR into two GPRs.
147 unsigned VReg0 = MIB->getOperand(0).getReg();
148 (void)VReg0;
149 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
150 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
Tim Northoverc2c545b2017-03-06 23:50:28 +0000151 "Unsupported operand for G_EXTRACT");
Diana Picusb1701e02017-02-16 12:19:57 +0000152 unsigned VReg1 = MIB->getOperand(1).getReg();
153 (void)VReg1;
Tim Northoverc2c545b2017-03-06 23:50:28 +0000154 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");
Diana Picusb1701e02017-02-16 12:19:57 +0000159
160 // Remove the operands corresponding to the offsets.
Tim Northoverc2c545b2017-03-06 23:50:28 +0000161 MIB->getOperand(2).setImm(MIB->getOperand(2).getImm() / 32);
Diana Picusb1701e02017-02-16 12:19:57 +0000162
Tim Northoverc2c545b2017-03-06 23:50:28 +0000163 MIB->setDesc(TII.get(ARM::VGETLNi32));
Diana Picusb1701e02017-02-16 12:19:57 +0000164 MIB.add(predOps(ARMCC::AL));
165
166 return true;
167}
168
Diana Picus8b6c6be2017-01-25 08:10:40 +0000169/// Select the opcode for simple extensions (that translate to a single SXT/UXT
170/// instruction). Extension operations more complicated than that should not
Diana Picuse8368782017-02-17 13:44:19 +0000171/// invoke this. Returns the original opcode if it doesn't know how to select a
172/// better one.
Diana Picus8b6c6be2017-01-25 08:10:40 +0000173static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
174 using namespace TargetOpcode;
175
Diana Picuse8368782017-02-17 13:44:19 +0000176 if (Size != 8 && Size != 16)
177 return Opc;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000178
179 if (Opc == G_SEXT)
180 return Size == 8 ? ARM::SXTB : ARM::SXTH;
181
182 if (Opc == G_ZEXT)
183 return Size == 8 ? ARM::UXTB : ARM::UXTH;
184
Diana Picuse8368782017-02-17 13:44:19 +0000185 return Opc;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000186}
187
Diana Picus3b99c642017-02-24 14:01:27 +0000188/// 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.
191static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
192 unsigned Size) {
193 bool isStore = Opc == TargetOpcode::G_STORE;
194
Diana Picus1540b062017-02-16 14:10:50 +0000195 if (RegBank == ARM::GPRRegBankID) {
196 switch (Size) {
197 case 1:
198 case 8:
Diana Picus3b99c642017-02-24 14:01:27 +0000199 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
Diana Picus1540b062017-02-16 14:10:50 +0000200 case 16:
Diana Picus3b99c642017-02-24 14:01:27 +0000201 return isStore ? ARM::STRH : ARM::LDRH;
Diana Picus1540b062017-02-16 14:10:50 +0000202 case 32:
Diana Picus3b99c642017-02-24 14:01:27 +0000203 return isStore ? ARM::STRi12 : ARM::LDRi12;
Diana Picuse8368782017-02-17 13:44:19 +0000204 default:
Diana Picus3b99c642017-02-24 14:01:27 +0000205 return Opc;
Diana Picus1540b062017-02-16 14:10:50 +0000206 }
Diana Picus1540b062017-02-16 14:10:50 +0000207 }
208
Diana Picuse8368782017-02-17 13:44:19 +0000209 if (RegBank == ARM::FPRRegBankID) {
210 switch (Size) {
211 case 32:
Diana Picus3b99c642017-02-24 14:01:27 +0000212 return isStore ? ARM::VSTRS : ARM::VLDRS;
Diana Picuse8368782017-02-17 13:44:19 +0000213 case 64:
Diana Picus3b99c642017-02-24 14:01:27 +0000214 return isStore ? ARM::VSTRD : ARM::VLDRD;
Diana Picuse8368782017-02-17 13:44:19 +0000215 default:
Diana Picus3b99c642017-02-24 14:01:27 +0000216 return Opc;
Diana Picuse8368782017-02-17 13:44:19 +0000217 }
Diana Picus278c7222017-01-26 09:20:47 +0000218 }
219
Diana Picus3b99c642017-02-24 14:01:27 +0000220 return Opc;
Diana Picus278c7222017-01-26 09:20:47 +0000221}
222
Diana Picus812caee2016-12-16 12:54:46 +0000223bool 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!");
226
227 auto &MBB = *I.getParent();
228 auto &MF = *MBB.getParent();
229 auto &MRI = MF.getRegInfo();
230
231 if (!isPreISelGenericOpcode(I.getOpcode())) {
232 if (I.isCopy())
233 return selectCopy(I, TII, MRI, TRI, RBI);
234
235 return true;
236 }
237
Diana Picus519807f2016-12-19 11:26:31 +0000238 MachineInstrBuilder MIB{MF, I};
Diana Picusd83df5d2017-01-25 08:47:40 +0000239 bool isSExt = false;
Diana Picus519807f2016-12-19 11:26:31 +0000240
241 using namespace TargetOpcode;
242 switch (I.getOpcode()) {
Diana Picus8b6c6be2017-01-25 08:10:40 +0000243 case G_SEXT:
Diana Picusd83df5d2017-01-25 08:47:40 +0000244 isSExt = true;
245 LLVM_FALLTHROUGH;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000246 case G_ZEXT: {
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");
251 return false;
252 }
253
254 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
255 unsigned SrcSize = SrcTy.getSizeInBits();
256 switch (SrcSize) {
Diana Picusd83df5d2017-01-25 08:47:40 +0000257 case 1: {
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());
261
262 if (isSExt) {
263 unsigned SExtResult = I.getOperand(0).getReg();
264
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);
268
269 auto InsertBefore = std::next(I.getIterator());
Martin Bohme8396e142017-01-25 14:28:19 +0000270 auto SubI =
Diana Picusd83df5d2017-01-25 08:47:40 +0000271 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
272 .addDef(SExtResult)
273 .addUse(AndResult)
274 .addImm(0)
275 .add(predOps(ARMCC::AL))
276 .add(condCodeOp());
277 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
278 return false;
279 }
280 break;
281 }
Diana Picus8b6c6be2017-01-25 08:10:40 +0000282 case 8:
283 case 16: {
284 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
Diana Picuse8368782017-02-17 13:44:19 +0000285 if (NewOpc == I.getOpcode())
286 return false;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000287 I.setDesc(TII.get(NewOpc));
288 MIB.addImm(0).add(predOps(ARMCC::AL));
289 break;
290 }
291 default:
292 DEBUG(dbgs() << "Unsupported source size for extension");
293 return false;
294 }
295 break;
296 }
Diana Picus519807f2016-12-19 11:26:31 +0000297 case G_ADD:
Diana Picus9d070942017-02-28 10:14:38 +0000298 case G_GEP:
Diana Picus812caee2016-12-16 12:54:46 +0000299 I.setDesc(TII.get(ARM::ADDrr));
Diana Picus8a73f552017-01-13 10:18:01 +0000300 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000301 break;
Diana Picusa3a0ccc2017-04-18 12:35:28 +0000302 case G_SUB:
303 I.setDesc(TII.get(ARM::SUBrr));
304 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
305 break;
Diana Picus49472ff2017-04-19 07:29:46 +0000306 case G_MUL:
307 if (TII.getSubtarget().hasV6Ops()) {
308 I.setDesc(TII.get(ARM::MUL));
309 } else {
310 assert(TII.getSubtarget().useMulOps() && "Unsupported target");
311 I.setDesc(TII.get(ARM::MULv5));
312 MIB->getOperand(0).setIsEarlyClobber(true);
313 }
314 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
315 break;
Diana Picus4fa83c02017-02-08 13:23:04 +0000316 case G_FADD:
Diana Picus6beef3c2017-02-16 12:19:52 +0000317 if (!selectFAdd(MIB, TII, MRI))
Diana Picus4fa83c02017-02-08 13:23:04 +0000318 return false;
Diana Picus4fa83c02017-02-08 13:23:04 +0000319 break;
Diana Picus519807f2016-12-19 11:26:31 +0000320 case G_FRAME_INDEX:
321 // Add 0 to the given frame index and hope it will eventually be folded into
322 // the user(s).
323 I.setDesc(TII.get(ARM::ADDri));
Diana Picus8a73f552017-01-13 10:18:01 +0000324 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000325 break;
Diana Picus5a7203a2017-02-28 13:05:42 +0000326 case G_CONSTANT: {
327 unsigned Reg = I.getOperand(0).getReg();
328 if (MRI.getType(Reg).getSizeInBits() != 32)
329 return false;
330
331 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
332 "Expected constant to live in a GPR");
333 I.setDesc(TII.get(ARM::MOVi));
334 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
335 break;
336 }
Diana Picus3b99c642017-02-24 14:01:27 +0000337 case G_STORE:
Diana Picus278c7222017-01-26 09:20:47 +0000338 case G_LOAD: {
Diana Picus1c33c9f2017-02-20 14:45:58 +0000339 const auto &MemOp = **I.memoperands_begin();
340 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
341 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
342 return false;
343 }
344
Diana Picus1540b062017-02-16 14:10:50 +0000345 unsigned Reg = I.getOperand(0).getReg();
346 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
347
348 LLT ValTy = MRI.getType(Reg);
Diana Picus278c7222017-01-26 09:20:47 +0000349 const auto ValSize = ValTy.getSizeInBits();
350
Diana Picus1540b062017-02-16 14:10:50 +0000351 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
Diana Picus3b99c642017-02-24 14:01:27 +0000352 "Don't know how to load/store 64-bit value without VFP");
Diana Picus1540b062017-02-16 14:10:50 +0000353
Diana Picus3b99c642017-02-24 14:01:27 +0000354 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
355 if (NewOpc == G_LOAD || NewOpc == G_STORE)
Diana Picuse8368782017-02-17 13:44:19 +0000356 return false;
357
Diana Picus278c7222017-01-26 09:20:47 +0000358 I.setDesc(TII.get(NewOpc));
359
Diana Picus3b99c642017-02-24 14:01:27 +0000360 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
Diana Picus278c7222017-01-26 09:20:47 +0000361 // LDRH has a funny addressing mode (there's already a FIXME for it).
362 MIB.addReg(0);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000363 MIB.addImm(0).add(predOps(ARMCC::AL));
Diana Picus519807f2016-12-19 11:26:31 +0000364 break;
Diana Picus278c7222017-01-26 09:20:47 +0000365 }
Diana Picusb1701e02017-02-16 12:19:57 +0000366 case G_SEQUENCE: {
367 if (!selectSequence(MIB, TII, MRI, TRI, RBI))
368 return false;
369 break;
370 }
371 case G_EXTRACT: {
372 if (!selectExtract(MIB, TII, MRI, TRI, RBI))
373 return false;
374 break;
375 }
Diana Picus519807f2016-12-19 11:26:31 +0000376 default:
377 return false;
Diana Picus812caee2016-12-16 12:54:46 +0000378 }
379
Diana Picus519807f2016-12-19 11:26:31 +0000380 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Diana Picus22274932016-11-11 08:27:37 +0000381}