blob: 84f9d4e5cfbba480fe717581fe3a3c84c32f3809 [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.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
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
15#include "AArch64InstructionSelector.h"
16#include "AArch64InstrInfo.h"
17#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000020#include "AArch64TargetMachine.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/IR/Type.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
29
30#define DEBUG_TYPE "aarch64-isel"
31
32using namespace llvm;
33
34#ifndef LLVM_BUILD_GLOBAL_ISEL
35#error "You shouldn't build this"
36#endif
37
38AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000039 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
40 const AArch64RegisterBankInfo &RBI)
41 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000042 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
43
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000044/// Check whether \p I is a currently unsupported binary operation:
45/// - it has an unsized type
46/// - an operand is not a vreg
47/// - all operands are not in the same bank
48/// These are checks that should someday live in the verifier, but right now,
49/// these are mostly limitations of the aarch64 selector.
50static bool unsupportedBinOp(const MachineInstr &I,
51 const AArch64RegisterBankInfo &RBI,
52 const MachineRegisterInfo &MRI,
53 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000054 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000055 if (!Ty.isValid()) {
56 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000057 return true;
58 }
59
60 const RegisterBank *PrevOpBank = nullptr;
61 for (auto &MO : I.operands()) {
62 // FIXME: Support non-register operands.
63 if (!MO.isReg()) {
64 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
65 return true;
66 }
67
68 // FIXME: Can generic operations have physical registers operands? If
69 // so, this will need to be taught about that, and we'll need to get the
70 // bank out of the minimal class for the register.
71 // Either way, this needs to be documented (and possibly verified).
72 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
73 DEBUG(dbgs() << "Generic inst has physical register operand\n");
74 return true;
75 }
76
77 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
78 if (!OpBank) {
79 DEBUG(dbgs() << "Generic register has no bank or class\n");
80 return true;
81 }
82
83 if (PrevOpBank && OpBank != PrevOpBank) {
84 DEBUG(dbgs() << "Generic inst operands have different banks\n");
85 return true;
86 }
87 PrevOpBank = OpBank;
88 }
89 return false;
90}
91
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000092/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
93/// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID
94/// and of size \p OpSize.
95/// \returns \p GenericOpc if the combination is unsupported.
96static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
97 unsigned OpSize) {
98 switch (RegBankID) {
99 case AArch64::GPRRegBankID:
100 switch (OpSize) {
101 case 32:
102 switch (GenericOpc) {
103 case TargetOpcode::G_OR:
104 return AArch64::ORRWrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000105 case TargetOpcode::G_XOR:
106 return AArch64::EORWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000107 case TargetOpcode::G_AND:
108 return AArch64::ANDWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000109 case TargetOpcode::G_ADD:
110 return AArch64::ADDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000111 case TargetOpcode::G_SUB:
112 return AArch64::SUBWrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000113 case TargetOpcode::G_SHL:
114 return AArch64::LSLVWr;
115 case TargetOpcode::G_LSHR:
116 return AArch64::LSRVWr;
117 case TargetOpcode::G_ASHR:
118 return AArch64::ASRVWr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000119 case TargetOpcode::G_SDIV:
120 return AArch64::SDIVWr;
121 case TargetOpcode::G_UDIV:
122 return AArch64::UDIVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000123 default:
124 return GenericOpc;
125 }
126 case 64:
127 switch (GenericOpc) {
128 case TargetOpcode::G_OR:
129 return AArch64::ORRXrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000130 case TargetOpcode::G_XOR:
131 return AArch64::EORXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000132 case TargetOpcode::G_AND:
133 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000134 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000135 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000136 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000137 case TargetOpcode::G_SUB:
138 return AArch64::SUBXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000139 case TargetOpcode::G_SHL:
140 return AArch64::LSLVXr;
141 case TargetOpcode::G_LSHR:
142 return AArch64::LSRVXr;
143 case TargetOpcode::G_ASHR:
144 return AArch64::ASRVXr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000145 case TargetOpcode::G_SDIV:
146 return AArch64::SDIVXr;
147 case TargetOpcode::G_UDIV:
148 return AArch64::UDIVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000149 default:
150 return GenericOpc;
151 }
152 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000153 case AArch64::FPRRegBankID:
154 switch (OpSize) {
155 case 32:
156 switch (GenericOpc) {
157 case TargetOpcode::G_FADD:
158 return AArch64::FADDSrr;
159 case TargetOpcode::G_FSUB:
160 return AArch64::FSUBSrr;
161 case TargetOpcode::G_FMUL:
162 return AArch64::FMULSrr;
163 case TargetOpcode::G_FDIV:
164 return AArch64::FDIVSrr;
165 default:
166 return GenericOpc;
167 }
168 case 64:
169 switch (GenericOpc) {
170 case TargetOpcode::G_FADD:
171 return AArch64::FADDDrr;
172 case TargetOpcode::G_FSUB:
173 return AArch64::FSUBDrr;
174 case TargetOpcode::G_FMUL:
175 return AArch64::FMULDrr;
176 case TargetOpcode::G_FDIV:
177 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000178 case TargetOpcode::G_OR:
179 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000180 default:
181 return GenericOpc;
182 }
183 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000184 };
185 return GenericOpc;
186}
187
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000188/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
189/// appropriate for the (value) register bank \p RegBankID and of memory access
190/// size \p OpSize. This returns the variant with the base+unsigned-immediate
191/// addressing mode (e.g., LDRXui).
192/// \returns \p GenericOpc if the combination is unsupported.
193static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
194 unsigned OpSize) {
195 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
196 switch (RegBankID) {
197 case AArch64::GPRRegBankID:
198 switch (OpSize) {
199 case 32:
200 return isStore ? AArch64::STRWui : AArch64::LDRWui;
201 case 64:
202 return isStore ? AArch64::STRXui : AArch64::LDRXui;
203 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000204 case AArch64::FPRRegBankID:
205 switch (OpSize) {
206 case 32:
207 return isStore ? AArch64::STRSui : AArch64::LDRSui;
208 case 64:
209 return isStore ? AArch64::STRDui : AArch64::LDRDui;
210 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000211 };
212 return GenericOpc;
213}
214
Quentin Colombetcb629a82016-10-12 03:57:49 +0000215static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
216 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
217 const RegisterBankInfo &RBI) {
218
219 unsigned DstReg = I.getOperand(0).getReg();
220 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
221 assert(I.isCopy() && "Generic operators do not allow physical registers");
222 return true;
223 }
224
225 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
226 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
227 unsigned SrcReg = I.getOperand(1).getReg();
228 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
229 (void)SrcSize;
230 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
231 "No phys reg on generic operators");
232 assert(
233 (DstSize == SrcSize ||
234 // Copies are a mean to setup initial types, the number of
235 // bits may not exactly match.
236 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
237 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
238 // Copies are a mean to copy bits around, as long as we are
239 // on the same register class, that's fine. Otherwise, that
240 // means we need some SUBREG_TO_REG or AND & co.
241 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
242 "Copy with different width?!");
243 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
244 "GPRs cannot get more than 64-bit width values");
245 const TargetRegisterClass *RC = nullptr;
246
247 if (RegBank.getID() == AArch64::FPRRegBankID) {
248 if (DstSize <= 32)
249 RC = &AArch64::FPR32RegClass;
250 else if (DstSize <= 64)
251 RC = &AArch64::FPR64RegClass;
252 else if (DstSize <= 128)
253 RC = &AArch64::FPR128RegClass;
254 else {
255 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
256 return false;
257 }
258 } else {
259 assert(RegBank.getID() == AArch64::GPRRegBankID &&
260 "Bitcast for the flags?");
261 RC =
262 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
263 }
264
265 // No need to constrain SrcReg. It will get constrained when
266 // we hit another of its use or its defs.
267 // Copies do not have constraints.
268 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
269 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
270 << " operand\n");
271 return false;
272 }
273 I.setDesc(TII.get(AArch64::COPY));
274 return true;
275}
276
Tim Northover6c02ad52016-10-12 22:49:04 +0000277static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
278 switch (P) {
279 default:
280 llvm_unreachable("Unknown condition code!");
281 case CmpInst::ICMP_NE:
282 return AArch64CC::NE;
283 case CmpInst::ICMP_EQ:
284 return AArch64CC::EQ;
285 case CmpInst::ICMP_SGT:
286 return AArch64CC::GT;
287 case CmpInst::ICMP_SGE:
288 return AArch64CC::GE;
289 case CmpInst::ICMP_SLT:
290 return AArch64CC::LT;
291 case CmpInst::ICMP_SLE:
292 return AArch64CC::LE;
293 case CmpInst::ICMP_UGT:
294 return AArch64CC::HI;
295 case CmpInst::ICMP_UGE:
296 return AArch64CC::HS;
297 case CmpInst::ICMP_ULT:
298 return AArch64CC::LO;
299 case CmpInst::ICMP_ULE:
300 return AArch64CC::LS;
301 }
302}
303
Tim Northover7dd378d2016-10-12 22:49:07 +0000304static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
305 AArch64CC::CondCode &CondCode,
306 AArch64CC::CondCode &CondCode2) {
307 CondCode2 = AArch64CC::AL;
308 switch (P) {
309 default:
310 llvm_unreachable("Unknown FP condition!");
311 case CmpInst::FCMP_OEQ:
312 CondCode = AArch64CC::EQ;
313 break;
314 case CmpInst::FCMP_OGT:
315 CondCode = AArch64CC::GT;
316 break;
317 case CmpInst::FCMP_OGE:
318 CondCode = AArch64CC::GE;
319 break;
320 case CmpInst::FCMP_OLT:
321 CondCode = AArch64CC::MI;
322 break;
323 case CmpInst::FCMP_OLE:
324 CondCode = AArch64CC::LS;
325 break;
326 case CmpInst::FCMP_ONE:
327 CondCode = AArch64CC::MI;
328 CondCode2 = AArch64CC::GT;
329 break;
330 case CmpInst::FCMP_ORD:
331 CondCode = AArch64CC::VC;
332 break;
333 case CmpInst::FCMP_UNO:
334 CondCode = AArch64CC::VS;
335 break;
336 case CmpInst::FCMP_UEQ:
337 CondCode = AArch64CC::EQ;
338 CondCode2 = AArch64CC::VS;
339 break;
340 case CmpInst::FCMP_UGT:
341 CondCode = AArch64CC::HI;
342 break;
343 case CmpInst::FCMP_UGE:
344 CondCode = AArch64CC::PL;
345 break;
346 case CmpInst::FCMP_ULT:
347 CondCode = AArch64CC::LT;
348 break;
349 case CmpInst::FCMP_ULE:
350 CondCode = AArch64CC::LE;
351 break;
352 case CmpInst::FCMP_UNE:
353 CondCode = AArch64CC::NE;
354 break;
355 }
356}
357
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000358bool AArch64InstructionSelector::select(MachineInstr &I) const {
359 assert(I.getParent() && "Instruction should be in a basic block!");
360 assert(I.getParent()->getParent() && "Instruction should be in a function!");
361
362 MachineBasicBlock &MBB = *I.getParent();
363 MachineFunction &MF = *MBB.getParent();
364 MachineRegisterInfo &MRI = MF.getRegInfo();
365
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000366 if (!isPreISelGenericOpcode(I.getOpcode()))
Quentin Colombetcb629a82016-10-12 03:57:49 +0000367 return !I.isCopy() || selectCopy(I, TII, MRI, TRI, RBI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000368
369 if (I.getNumOperands() != I.getNumExplicitOperands()) {
370 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
371 return false;
372 }
373
Tim Northover32a078a2016-09-15 10:09:59 +0000374 LLT Ty =
375 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000376
Ahmed Bougacha85505092016-07-28 17:15:15 +0000377 switch (I.getOpcode()) {
378 case TargetOpcode::G_BR: {
379 I.setDesc(TII.get(AArch64::B));
Ahmed Bougacha85505092016-07-28 17:15:15 +0000380 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000381 }
382
Tim Northover5e3dbf32016-10-12 22:49:01 +0000383 case TargetOpcode::G_BRCOND: {
384 if (Ty.getSizeInBits() > 32) {
385 // We shouldn't need this on AArch64, but it would be implemented as an
386 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
387 // bit being tested is < 32.
388 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
389 << ", expected at most 32-bits");
390 return false;
391 }
392
393 const unsigned CondReg = I.getOperand(0).getReg();
394 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
395
396 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
397 .addUse(CondReg)
398 .addImm(/*bit offset=*/0)
399 .addMBB(DestMBB);
400
401 I.eraseFromParent();
402 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
403 }
404
Tim Northover4edc60d2016-10-10 21:49:42 +0000405 case TargetOpcode::G_CONSTANT: {
406 if (Ty.getSizeInBits() <= 32)
407 I.setDesc(TII.get(AArch64::MOVi32imm));
408 else if (Ty.getSizeInBits() <= 64)
409 I.setDesc(TII.get(AArch64::MOVi64imm));
410 else
411 return false;
412 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
413 }
414
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000415 case TargetOpcode::G_FRAME_INDEX: {
416 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000417 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000418 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000419 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000420 return false;
421 }
422
423 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000424
425 // MOs for a #0 shifted immediate.
426 I.addOperand(MachineOperand::CreateImm(0));
427 I.addOperand(MachineOperand::CreateImm(0));
428
429 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
430 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000431
432 case TargetOpcode::G_GLOBAL_VALUE: {
433 auto GV = I.getOperand(1).getGlobal();
434 if (GV->isThreadLocal()) {
435 // FIXME: we don't support TLS yet.
436 return false;
437 }
438 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
439 if (OpFlags & AArch64II::MO_GOT)
440 I.setDesc(TII.get(AArch64::LOADgot));
441 else {
442 I.setDesc(TII.get(AArch64::MOVaddr));
443 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
444 MachineInstrBuilder MIB(MF, I);
445 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
446 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
447 }
448 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
449 }
450
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000451 case TargetOpcode::G_LOAD:
452 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000453 LLT MemTy = Ty;
454 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000455
Tim Northover5ae83502016-09-15 09:20:34 +0000456 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000457 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000458 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000459 return false;
460 }
461
462#ifndef NDEBUG
463 // Sanity-check the pointer register.
464 const unsigned PtrReg = I.getOperand(1).getReg();
465 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
466 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
467 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000468 assert(MRI.getType(PtrReg).isPointer() &&
469 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000470#endif
471
472 const unsigned ValReg = I.getOperand(0).getReg();
473 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
474
475 const unsigned NewOpc =
476 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
477 if (NewOpc == I.getOpcode())
478 return false;
479
480 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000481
482 I.addOperand(MachineOperand::CreateImm(0));
483 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
484 }
485
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000486 case TargetOpcode::G_MUL: {
487 // Reject the various things we don't support yet.
488 if (unsupportedBinOp(I, RBI, MRI, TRI))
489 return false;
490
491 const unsigned DefReg = I.getOperand(0).getReg();
492 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
493
494 if (RB.getID() != AArch64::GPRRegBankID) {
495 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
496 return false;
497 }
498
499 unsigned ZeroReg;
500 unsigned NewOpc;
501 if (Ty == LLT::scalar(32)) {
502 NewOpc = AArch64::MADDWrrr;
503 ZeroReg = AArch64::WZR;
504 } else if (Ty == LLT::scalar(64)) {
505 NewOpc = AArch64::MADDXrrr;
506 ZeroReg = AArch64::XZR;
507 } else {
508 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
509 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
510 return false;
511 }
512
513 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000514
515 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
516
517 // Now that we selected an opcode, we need to constrain the register
518 // operands to use appropriate classes.
519 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
520 }
521
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000522 case TargetOpcode::G_FADD:
523 case TargetOpcode::G_FSUB:
524 case TargetOpcode::G_FMUL:
525 case TargetOpcode::G_FDIV:
526
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000527 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000528 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000529 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000530 case TargetOpcode::G_SHL:
531 case TargetOpcode::G_LSHR:
532 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000533 case TargetOpcode::G_SDIV:
534 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000535 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000536 case TargetOpcode::G_SUB:
537 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000538 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000539 if (unsupportedBinOp(I, RBI, MRI, TRI))
540 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000541
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000542 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000543
544 const unsigned DefReg = I.getOperand(0).getReg();
545 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
546
547 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
548 if (NewOpc == I.getOpcode())
549 return false;
550
551 I.setDesc(TII.get(NewOpc));
552 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000553
554 // Now that we selected an opcode, we need to constrain the register
555 // operands to use appropriate classes.
556 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
557 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000558
559 case TargetOpcode::G_ANYEXT: {
560 const unsigned DstReg = I.getOperand(0).getReg();
561 const unsigned SrcReg = I.getOperand(1).getReg();
562
Quentin Colombetcb629a82016-10-12 03:57:49 +0000563 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
564 if (RBDst.getID() != AArch64::GPRRegBankID) {
565 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
566 return false;
567 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000568
Quentin Colombetcb629a82016-10-12 03:57:49 +0000569 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
570 if (RBSrc.getID() != AArch64::GPRRegBankID) {
571 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000572 return false;
573 }
574
575 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
576
577 if (DstSize == 0) {
578 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
579 return false;
580 }
581
Quentin Colombetcb629a82016-10-12 03:57:49 +0000582 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000583 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
584 << ", expected: 32 or 64\n");
585 return false;
586 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000587 // At this point G_ANYEXT is just like a plain COPY, but we need
588 // to explicitly form the 64-bit value if any.
589 if (DstSize > 32) {
590 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
591 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
592 .addDef(ExtSrc)
593 .addImm(0)
594 .addUse(SrcReg)
595 .addImm(AArch64::sub_32);
596 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000597 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000598 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000599 }
600
601 case TargetOpcode::G_ZEXT:
602 case TargetOpcode::G_SEXT: {
603 unsigned Opcode = I.getOpcode();
604 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
605 SrcTy = MRI.getType(I.getOperand(1).getReg());
606 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
607 const unsigned DefReg = I.getOperand(0).getReg();
608 const unsigned SrcReg = I.getOperand(1).getReg();
609 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
610
611 if (RB.getID() != AArch64::GPRRegBankID) {
612 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
613 << ", expected: GPR\n");
614 return false;
615 }
616
617 MachineInstr *ExtI;
618 if (DstTy == LLT::scalar(64)) {
619 // FIXME: Can we avoid manually doing this?
620 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
621 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
622 << " operand\n");
623 return false;
624 }
625
626 const unsigned SrcXReg =
627 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
628 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
629 .addDef(SrcXReg)
630 .addImm(0)
631 .addUse(SrcReg)
632 .addImm(AArch64::sub_32);
633
634 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
635 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
636 .addDef(DefReg)
637 .addUse(SrcXReg)
638 .addImm(0)
639 .addImm(SrcTy.getSizeInBits() - 1);
640 } else if (DstTy == LLT::scalar(32)) {
641 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
642 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
643 .addDef(DefReg)
644 .addUse(SrcReg)
645 .addImm(0)
646 .addImm(SrcTy.getSizeInBits() - 1);
647 } else {
648 return false;
649 }
650
651 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
652
653 I.eraseFromParent();
654 return true;
655 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000656
657 case TargetOpcode::G_INTTOPTR:
658 case TargetOpcode::G_PTRTOINT:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000659 case TargetOpcode::G_BITCAST:
660 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000661
662 case TargetOpcode::G_ICMP: {
663 if (Ty != LLT::scalar(1)) {
664 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
665 << ", expected: " << LLT::scalar(1) << '\n');
666 return false;
667 }
668
669 unsigned CmpOpc = 0;
670 unsigned ZReg = 0;
671
672 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
673 if (CmpTy == LLT::scalar(32)) {
674 CmpOpc = AArch64::SUBSWrr;
675 ZReg = AArch64::WZR;
676 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
677 CmpOpc = AArch64::SUBSXrr;
678 ZReg = AArch64::XZR;
679 } else {
680 return false;
681 }
682
683 const AArch64CC::CondCode CC = changeICMPPredToAArch64CC(
684 (CmpInst::Predicate)I.getOperand(1).getPredicate());
685
686 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
687 .addDef(ZReg)
688 .addUse(I.getOperand(2).getReg())
689 .addUse(I.getOperand(3).getReg());
690
691 MachineInstr &CSetMI =
692 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
693 .addDef(I.getOperand(0).getReg())
694 .addUse(AArch64::WZR)
695 .addUse(AArch64::WZR)
696 .addImm(CC);
697
698 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
699 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
700
701 I.eraseFromParent();
702 return true;
703 }
704
Tim Northover7dd378d2016-10-12 22:49:07 +0000705 case TargetOpcode::G_FCMP: {
706 if (Ty != LLT::scalar(1)) {
707 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
708 << ", expected: " << LLT::scalar(1) << '\n');
709 return false;
710 }
711
712 unsigned CmpOpc = 0;
713 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
714 if (CmpTy == LLT::scalar(32)) {
715 CmpOpc = AArch64::FCMPSrr;
716 } else if (CmpTy == LLT::scalar(64)) {
717 CmpOpc = AArch64::FCMPDrr;
718 } else {
719 return false;
720 }
721
722 // FIXME: regbank
723
724 AArch64CC::CondCode CC1, CC2;
725 changeFCMPPredToAArch64CC(
726 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
727
728 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
729 .addUse(I.getOperand(2).getReg())
730 .addUse(I.getOperand(3).getReg());
731
732 const unsigned DefReg = I.getOperand(0).getReg();
733 unsigned Def1Reg = DefReg;
734 if (CC2 != AArch64CC::AL)
735 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
736
737 MachineInstr &CSetMI =
738 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
739 .addDef(Def1Reg)
740 .addUse(AArch64::WZR)
741 .addUse(AArch64::WZR)
742 .addImm(CC1);
743
744 if (CC2 != AArch64CC::AL) {
745 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
746 MachineInstr &CSet2MI =
747 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
748 .addDef(Def2Reg)
749 .addUse(AArch64::WZR)
750 .addUse(AArch64::WZR)
751 .addImm(CC2);
752 MachineInstr &OrMI =
753 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
754 .addDef(DefReg)
755 .addUse(Def1Reg)
756 .addUse(Def2Reg);
757 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
758 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
759 }
760
761 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
762 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
763
764 I.eraseFromParent();
765 return true;
766 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000767 }
768
769 return false;
770}