blob: 3b62c3878c34d81598bb363a1299a504879faf73 [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;
Diana Picus64a33432017-04-21 13:16:50 +000050 // 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?!");
Diana Picus812caee2016-12-16 12:54:46 +000053
Diana Picus4fa83c02017-02-08 13:23:04 +000054 assert((RegBank->getID() == ARM::GPRRegBankID ||
55 RegBank->getID() == ARM::FPRRegBankID) &&
56 "Unsupported reg bank");
57
Diana Picus812caee2016-12-16 12:54:46 +000058 const TargetRegisterClass *RC = &ARM::GPRRegClass;
59
Diana Picus4fa83c02017-02-08 13:23:04 +000060 if (RegBank->getID() == ARM::FPRRegBankID) {
Diana Picus6beef3c2017-02-16 12:19:52 +000061 if (DstSize == 32)
62 RC = &ARM::SPRRegClass;
63 else if (DstSize == 64)
64 RC = &ARM::DPRRegClass;
65 else
66 llvm_unreachable("Unsupported destination size");
Diana Picus4fa83c02017-02-08 13:23:04 +000067 }
68
Diana Picus812caee2016-12-16 12:54:46 +000069 // 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())
74 << " operand\n");
75 return false;
76 }
77 return true;
78}
79
Diana Picus6beef3c2017-02-16 12:19:52 +000080static bool selectFAdd(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII,
81 MachineRegisterInfo &MRI) {
82 assert(TII.getSubtarget().hasVFP2() && "Can't select fp add without vfp");
83
84 LLT Ty = MRI.getType(MIB->getOperand(0).getReg());
85 unsigned ValSize = Ty.getSizeInBits();
86
87 if (ValSize == 32) {
88 if (TII.getSubtarget().useNEONForSinglePrecisionFP())
89 return false;
90 MIB->setDesc(TII.get(ARM::VADDS));
91 } else {
92 assert(ValSize == 64 && "Unsupported size for floating point value");
93 if (TII.getSubtarget().isFPOnlySP())
94 return false;
95 MIB->setDesc(TII.get(ARM::VADDD));
96 }
97 MIB.add(predOps(ARMCC::AL));
98
99 return true;
100}
101
Diana Picusb1701e02017-02-16 12:19:57 +0000102static 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");
108
109 // We only support G_SEQUENCE as a way to stick together two scalar GPRs
110 // into one DPR.
111 unsigned VReg0 = MIB->getOperand(0).getReg();
112 (void)VReg0;
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();
117 (void)VReg1;
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();
122 (void)VReg2;
123 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
124 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
125 "Unsupported operand for G_SEQUENCE");
126
127 // Remove the operands corresponding to the offsets.
128 MIB->RemoveOperand(4);
129 MIB->RemoveOperand(2);
130
131 MIB->setDesc(TII.get(ARM::VMOVDRR));
132 MIB.add(predOps(ARMCC::AL));
133
134 return true;
135}
136
137static 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");
142
143 // We only support G_EXTRACT as a way to break up one DPR into two GPRs.
144 unsigned VReg0 = MIB->getOperand(0).getReg();
145 (void)VReg0;
146 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
147 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
Tim Northoverc2c545b2017-03-06 23:50:28 +0000148 "Unsupported operand for G_EXTRACT");
Diana Picusb1701e02017-02-16 12:19:57 +0000149 unsigned VReg1 = MIB->getOperand(1).getReg();
150 (void)VReg1;
Tim Northoverc2c545b2017-03-06 23:50:28 +0000151 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");
Diana Picusb1701e02017-02-16 12:19:57 +0000156
157 // Remove the operands corresponding to the offsets.
Tim Northoverc2c545b2017-03-06 23:50:28 +0000158 MIB->getOperand(2).setImm(MIB->getOperand(2).getImm() / 32);
Diana Picusb1701e02017-02-16 12:19:57 +0000159
Tim Northoverc2c545b2017-03-06 23:50:28 +0000160 MIB->setDesc(TII.get(ARM::VGETLNi32));
Diana Picusb1701e02017-02-16 12:19:57 +0000161 MIB.add(predOps(ARMCC::AL));
162
163 return true;
164}
165
Diana Picus8b6c6be2017-01-25 08:10:40 +0000166/// Select the opcode for simple extensions (that translate to a single SXT/UXT
167/// instruction). Extension operations more complicated than that should not
Diana Picuse8368782017-02-17 13:44:19 +0000168/// invoke this. Returns the original opcode if it doesn't know how to select a
169/// better one.
Diana Picus8b6c6be2017-01-25 08:10:40 +0000170static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
171 using namespace TargetOpcode;
172
Diana Picuse8368782017-02-17 13:44:19 +0000173 if (Size != 8 && Size != 16)
174 return Opc;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000175
176 if (Opc == G_SEXT)
177 return Size == 8 ? ARM::SXTB : ARM::SXTH;
178
179 if (Opc == G_ZEXT)
180 return Size == 8 ? ARM::UXTB : ARM::UXTH;
181
Diana Picuse8368782017-02-17 13:44:19 +0000182 return Opc;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000183}
184
Diana Picus3b99c642017-02-24 14:01:27 +0000185/// 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.
188static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
189 unsigned Size) {
190 bool isStore = Opc == TargetOpcode::G_STORE;
191
Diana Picus1540b062017-02-16 14:10:50 +0000192 if (RegBank == ARM::GPRRegBankID) {
193 switch (Size) {
194 case 1:
195 case 8:
Diana Picus3b99c642017-02-24 14:01:27 +0000196 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
Diana Picus1540b062017-02-16 14:10:50 +0000197 case 16:
Diana Picus3b99c642017-02-24 14:01:27 +0000198 return isStore ? ARM::STRH : ARM::LDRH;
Diana Picus1540b062017-02-16 14:10:50 +0000199 case 32:
Diana Picus3b99c642017-02-24 14:01:27 +0000200 return isStore ? ARM::STRi12 : ARM::LDRi12;
Diana Picuse8368782017-02-17 13:44:19 +0000201 default:
Diana Picus3b99c642017-02-24 14:01:27 +0000202 return Opc;
Diana Picus1540b062017-02-16 14:10:50 +0000203 }
Diana Picus1540b062017-02-16 14:10:50 +0000204 }
205
Diana Picuse8368782017-02-17 13:44:19 +0000206 if (RegBank == ARM::FPRRegBankID) {
207 switch (Size) {
208 case 32:
Diana Picus3b99c642017-02-24 14:01:27 +0000209 return isStore ? ARM::VSTRS : ARM::VLDRS;
Diana Picuse8368782017-02-17 13:44:19 +0000210 case 64:
Diana Picus3b99c642017-02-24 14:01:27 +0000211 return isStore ? ARM::VSTRD : ARM::VLDRD;
Diana Picuse8368782017-02-17 13:44:19 +0000212 default:
Diana Picus3b99c642017-02-24 14:01:27 +0000213 return Opc;
Diana Picuse8368782017-02-17 13:44:19 +0000214 }
Diana Picus278c7222017-01-26 09:20:47 +0000215 }
216
Diana Picus3b99c642017-02-24 14:01:27 +0000217 return Opc;
Diana Picus278c7222017-01-26 09:20:47 +0000218}
219
Diana Picus812caee2016-12-16 12:54:46 +0000220bool 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!");
223
224 auto &MBB = *I.getParent();
225 auto &MF = *MBB.getParent();
226 auto &MRI = MF.getRegInfo();
227
228 if (!isPreISelGenericOpcode(I.getOpcode())) {
229 if (I.isCopy())
230 return selectCopy(I, TII, MRI, TRI, RBI);
231
232 return true;
233 }
234
Diana Picus519807f2016-12-19 11:26:31 +0000235 MachineInstrBuilder MIB{MF, I};
Diana Picusd83df5d2017-01-25 08:47:40 +0000236 bool isSExt = false;
Diana Picus519807f2016-12-19 11:26:31 +0000237
238 using namespace TargetOpcode;
239 switch (I.getOpcode()) {
Diana Picus8b6c6be2017-01-25 08:10:40 +0000240 case G_SEXT:
Diana Picusd83df5d2017-01-25 08:47:40 +0000241 isSExt = true;
242 LLVM_FALLTHROUGH;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000243 case G_ZEXT: {
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");
248 return false;
249 }
250
251 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
252 unsigned SrcSize = SrcTy.getSizeInBits();
253 switch (SrcSize) {
Diana Picusd83df5d2017-01-25 08:47:40 +0000254 case 1: {
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());
258
259 if (isSExt) {
260 unsigned SExtResult = I.getOperand(0).getReg();
261
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);
265
266 auto InsertBefore = std::next(I.getIterator());
Martin Bohme8396e142017-01-25 14:28:19 +0000267 auto SubI =
Diana Picusd83df5d2017-01-25 08:47:40 +0000268 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
269 .addDef(SExtResult)
270 .addUse(AndResult)
271 .addImm(0)
272 .add(predOps(ARMCC::AL))
273 .add(condCodeOp());
274 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
275 return false;
276 }
277 break;
278 }
Diana Picus8b6c6be2017-01-25 08:10:40 +0000279 case 8:
280 case 16: {
281 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
Diana Picuse8368782017-02-17 13:44:19 +0000282 if (NewOpc == I.getOpcode())
283 return false;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000284 I.setDesc(TII.get(NewOpc));
285 MIB.addImm(0).add(predOps(ARMCC::AL));
286 break;
287 }
288 default:
289 DEBUG(dbgs() << "Unsupported source size for extension");
290 return false;
291 }
292 break;
293 }
Diana Picus64a33432017-04-21 13:16:50 +0000294 case G_TRUNC: {
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();
299
300 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
301 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
302
303 if (SrcRegBank.getID() != DstRegBank.getID()) {
304 DEBUG(dbgs() << "G_TRUNC operands on different register banks\n");
305 return false;
306 }
307
308 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
309 DEBUG(dbgs() << "G_TRUNC on non-GPR not supported yet\n");
310 return false;
311 }
312
313 I.setDesc(TII.get(COPY));
314 return selectCopy(I, TII, MRI, TRI, RBI);
315 }
Diana Picus519807f2016-12-19 11:26:31 +0000316 case G_ADD:
Diana Picus9d070942017-02-28 10:14:38 +0000317 case G_GEP:
Diana Picus812caee2016-12-16 12:54:46 +0000318 I.setDesc(TII.get(ARM::ADDrr));
Diana Picus8a73f552017-01-13 10:18:01 +0000319 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000320 break;
Diana Picusa3a0ccc2017-04-18 12:35:28 +0000321 case G_SUB:
322 I.setDesc(TII.get(ARM::SUBrr));
323 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
324 break;
Diana Picus49472ff2017-04-19 07:29:46 +0000325 case G_MUL:
326 if (TII.getSubtarget().hasV6Ops()) {
327 I.setDesc(TII.get(ARM::MUL));
328 } else {
329 assert(TII.getSubtarget().useMulOps() && "Unsupported target");
330 I.setDesc(TII.get(ARM::MULv5));
331 MIB->getOperand(0).setIsEarlyClobber(true);
332 }
333 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
334 break;
Diana Picus4fa83c02017-02-08 13:23:04 +0000335 case G_FADD:
Diana Picus6beef3c2017-02-16 12:19:52 +0000336 if (!selectFAdd(MIB, TII, MRI))
Diana Picus4fa83c02017-02-08 13:23:04 +0000337 return false;
Diana Picus4fa83c02017-02-08 13:23:04 +0000338 break;
Diana Picus519807f2016-12-19 11:26:31 +0000339 case G_FRAME_INDEX:
340 // Add 0 to the given frame index and hope it will eventually be folded into
341 // the user(s).
342 I.setDesc(TII.get(ARM::ADDri));
Diana Picus8a73f552017-01-13 10:18:01 +0000343 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000344 break;
Diana Picus5a7203a2017-02-28 13:05:42 +0000345 case G_CONSTANT: {
346 unsigned Reg = I.getOperand(0).getReg();
347 if (MRI.getType(Reg).getSizeInBits() != 32)
348 return false;
349
350 assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID &&
351 "Expected constant to live in a GPR");
352 I.setDesc(TII.get(ARM::MOVi));
353 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
354 break;
355 }
Diana Picus3b99c642017-02-24 14:01:27 +0000356 case G_STORE:
Diana Picus278c7222017-01-26 09:20:47 +0000357 case G_LOAD: {
Diana Picus1c33c9f2017-02-20 14:45:58 +0000358 const auto &MemOp = **I.memoperands_begin();
359 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
360 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
361 return false;
362 }
363
Diana Picus1540b062017-02-16 14:10:50 +0000364 unsigned Reg = I.getOperand(0).getReg();
365 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
366
367 LLT ValTy = MRI.getType(Reg);
Diana Picus278c7222017-01-26 09:20:47 +0000368 const auto ValSize = ValTy.getSizeInBits();
369
Diana Picus1540b062017-02-16 14:10:50 +0000370 assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) &&
Diana Picus3b99c642017-02-24 14:01:27 +0000371 "Don't know how to load/store 64-bit value without VFP");
Diana Picus1540b062017-02-16 14:10:50 +0000372
Diana Picus3b99c642017-02-24 14:01:27 +0000373 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
374 if (NewOpc == G_LOAD || NewOpc == G_STORE)
Diana Picuse8368782017-02-17 13:44:19 +0000375 return false;
376
Diana Picus278c7222017-01-26 09:20:47 +0000377 I.setDesc(TII.get(NewOpc));
378
Diana Picus3b99c642017-02-24 14:01:27 +0000379 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
Diana Picus278c7222017-01-26 09:20:47 +0000380 // LDRH has a funny addressing mode (there's already a FIXME for it).
381 MIB.addReg(0);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000382 MIB.addImm(0).add(predOps(ARMCC::AL));
Diana Picus519807f2016-12-19 11:26:31 +0000383 break;
Diana Picus278c7222017-01-26 09:20:47 +0000384 }
Diana Picusb1701e02017-02-16 12:19:57 +0000385 case G_SEQUENCE: {
386 if (!selectSequence(MIB, TII, MRI, TRI, RBI))
387 return false;
388 break;
389 }
390 case G_EXTRACT: {
391 if (!selectExtract(MIB, TII, MRI, TRI, RBI))
392 return false;
393 break;
394 }
Diana Picus519807f2016-12-19 11:26:31 +0000395 default:
396 return false;
Diana Picus812caee2016-12-16 12:54:46 +0000397 }
398
Diana Picus519807f2016-12-19 11:26:31 +0000399 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Diana Picus22274932016-11-11 08:27:37 +0000400}