blob: 2e199d9c0d96eac14737e40c07961584780c2f1a [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"
Tim Northovere9600d82017-02-08 17:57:27 +000017#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000018#include "AArch64RegisterBankInfo.h"
19#include "AArch64RegisterInfo.h"
20#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000021#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000022#include "MCTargetDesc/AArch64AddressingModes.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/IR/Type.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
31
32#define DEBUG_TYPE "aarch64-isel"
33
34using namespace llvm;
35
36#ifndef LLVM_BUILD_GLOBAL_ISEL
37#error "You shouldn't build this"
38#endif
39
Daniel Sanders8a4bae92017-03-14 21:32:08 +000040#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000041#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000042#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000043
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000044AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000045 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
46 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000047 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
48 TRI(*STI.getRegisterInfo()), RBI(RBI)
49#define GET_GLOBALISEL_TEMPORARIES_INIT
50#include "AArch64GenGlobalISel.inc"
51#undef GET_GLOBALISEL_TEMPORARIES_INIT
52{
53}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000054
Tim Northoverfb8d9892016-10-12 22:49:15 +000055// FIXME: This should be target-independent, inferred from the types declared
56// for each class in the bank.
57static const TargetRegisterClass *
58getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
59 const RegisterBankInfo &RBI) {
60 if (RB.getID() == AArch64::GPRRegBankID) {
61 if (Ty.getSizeInBits() <= 32)
62 return &AArch64::GPR32RegClass;
63 if (Ty.getSizeInBits() == 64)
64 return &AArch64::GPR64RegClass;
65 return nullptr;
66 }
67
68 if (RB.getID() == AArch64::FPRRegBankID) {
69 if (Ty.getSizeInBits() == 32)
70 return &AArch64::FPR32RegClass;
71 if (Ty.getSizeInBits() == 64)
72 return &AArch64::FPR64RegClass;
73 if (Ty.getSizeInBits() == 128)
74 return &AArch64::FPR128RegClass;
75 return nullptr;
76 }
77
78 return nullptr;
79}
80
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000081/// Check whether \p I is a currently unsupported binary operation:
82/// - it has an unsized type
83/// - an operand is not a vreg
84/// - all operands are not in the same bank
85/// These are checks that should someday live in the verifier, but right now,
86/// these are mostly limitations of the aarch64 selector.
87static bool unsupportedBinOp(const MachineInstr &I,
88 const AArch64RegisterBankInfo &RBI,
89 const MachineRegisterInfo &MRI,
90 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000091 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000092 if (!Ty.isValid()) {
93 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000094 return true;
95 }
96
97 const RegisterBank *PrevOpBank = nullptr;
98 for (auto &MO : I.operands()) {
99 // FIXME: Support non-register operands.
100 if (!MO.isReg()) {
101 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
102 return true;
103 }
104
105 // FIXME: Can generic operations have physical registers operands? If
106 // so, this will need to be taught about that, and we'll need to get the
107 // bank out of the minimal class for the register.
108 // Either way, this needs to be documented (and possibly verified).
109 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
110 DEBUG(dbgs() << "Generic inst has physical register operand\n");
111 return true;
112 }
113
114 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
115 if (!OpBank) {
116 DEBUG(dbgs() << "Generic register has no bank or class\n");
117 return true;
118 }
119
120 if (PrevOpBank && OpBank != PrevOpBank) {
121 DEBUG(dbgs() << "Generic inst operands have different banks\n");
122 return true;
123 }
124 PrevOpBank = OpBank;
125 }
126 return false;
127}
128
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000129/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000130/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000131/// and of size \p OpSize.
132/// \returns \p GenericOpc if the combination is unsupported.
133static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
134 unsigned OpSize) {
135 switch (RegBankID) {
136 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000137 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000138 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000139 case TargetOpcode::G_SHL:
140 return AArch64::LSLVWr;
141 case TargetOpcode::G_LSHR:
142 return AArch64::LSRVWr;
143 case TargetOpcode::G_ASHR:
144 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000145 default:
146 return GenericOpc;
147 }
Tim Northover55782222016-10-18 20:03:48 +0000148 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000149 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000150 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000151 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000152 case TargetOpcode::G_SHL:
153 return AArch64::LSLVXr;
154 case TargetOpcode::G_LSHR:
155 return AArch64::LSRVXr;
156 case TargetOpcode::G_ASHR:
157 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000158 default:
159 return GenericOpc;
160 }
161 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000162 case AArch64::FPRRegBankID:
163 switch (OpSize) {
164 case 32:
165 switch (GenericOpc) {
166 case TargetOpcode::G_FADD:
167 return AArch64::FADDSrr;
168 case TargetOpcode::G_FSUB:
169 return AArch64::FSUBSrr;
170 case TargetOpcode::G_FMUL:
171 return AArch64::FMULSrr;
172 case TargetOpcode::G_FDIV:
173 return AArch64::FDIVSrr;
174 default:
175 return GenericOpc;
176 }
177 case 64:
178 switch (GenericOpc) {
179 case TargetOpcode::G_FADD:
180 return AArch64::FADDDrr;
181 case TargetOpcode::G_FSUB:
182 return AArch64::FSUBDrr;
183 case TargetOpcode::G_FMUL:
184 return AArch64::FMULDrr;
185 case TargetOpcode::G_FDIV:
186 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000187 case TargetOpcode::G_OR:
188 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000189 default:
190 return GenericOpc;
191 }
192 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 };
194 return GenericOpc;
195}
196
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000197/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
198/// appropriate for the (value) register bank \p RegBankID and of memory access
199/// size \p OpSize. This returns the variant with the base+unsigned-immediate
200/// addressing mode (e.g., LDRXui).
201/// \returns \p GenericOpc if the combination is unsupported.
202static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
203 unsigned OpSize) {
204 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
205 switch (RegBankID) {
206 case AArch64::GPRRegBankID:
207 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000208 case 8:
209 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
210 case 16:
211 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000212 case 32:
213 return isStore ? AArch64::STRWui : AArch64::LDRWui;
214 case 64:
215 return isStore ? AArch64::STRXui : AArch64::LDRXui;
216 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000217 case AArch64::FPRRegBankID:
218 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000219 case 8:
220 return isStore ? AArch64::STRBui : AArch64::LDRBui;
221 case 16:
222 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000223 case 32:
224 return isStore ? AArch64::STRSui : AArch64::LDRSui;
225 case 64:
226 return isStore ? AArch64::STRDui : AArch64::LDRDui;
227 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000228 };
229 return GenericOpc;
230}
231
Quentin Colombetcb629a82016-10-12 03:57:49 +0000232static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
233 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
234 const RegisterBankInfo &RBI) {
235
236 unsigned DstReg = I.getOperand(0).getReg();
237 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
238 assert(I.isCopy() && "Generic operators do not allow physical registers");
239 return true;
240 }
241
242 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
243 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
244 unsigned SrcReg = I.getOperand(1).getReg();
245 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
246 (void)SrcSize;
247 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
248 "No phys reg on generic operators");
249 assert(
250 (DstSize == SrcSize ||
251 // Copies are a mean to setup initial types, the number of
252 // bits may not exactly match.
253 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
254 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
255 // Copies are a mean to copy bits around, as long as we are
256 // on the same register class, that's fine. Otherwise, that
257 // means we need some SUBREG_TO_REG or AND & co.
258 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
259 "Copy with different width?!");
260 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
261 "GPRs cannot get more than 64-bit width values");
262 const TargetRegisterClass *RC = nullptr;
263
264 if (RegBank.getID() == AArch64::FPRRegBankID) {
265 if (DstSize <= 32)
266 RC = &AArch64::FPR32RegClass;
267 else if (DstSize <= 64)
268 RC = &AArch64::FPR64RegClass;
269 else if (DstSize <= 128)
270 RC = &AArch64::FPR128RegClass;
271 else {
272 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
273 return false;
274 }
275 } else {
276 assert(RegBank.getID() == AArch64::GPRRegBankID &&
277 "Bitcast for the flags?");
278 RC =
279 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
280 }
281
282 // No need to constrain SrcReg. It will get constrained when
283 // we hit another of its use or its defs.
284 // Copies do not have constraints.
285 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
286 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
287 << " operand\n");
288 return false;
289 }
290 I.setDesc(TII.get(AArch64::COPY));
291 return true;
292}
293
Tim Northover69271c62016-10-12 22:49:11 +0000294static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
295 if (!DstTy.isScalar() || !SrcTy.isScalar())
296 return GenericOpc;
297
298 const unsigned DstSize = DstTy.getSizeInBits();
299 const unsigned SrcSize = SrcTy.getSizeInBits();
300
301 switch (DstSize) {
302 case 32:
303 switch (SrcSize) {
304 case 32:
305 switch (GenericOpc) {
306 case TargetOpcode::G_SITOFP:
307 return AArch64::SCVTFUWSri;
308 case TargetOpcode::G_UITOFP:
309 return AArch64::UCVTFUWSri;
310 case TargetOpcode::G_FPTOSI:
311 return AArch64::FCVTZSUWSr;
312 case TargetOpcode::G_FPTOUI:
313 return AArch64::FCVTZUUWSr;
314 default:
315 return GenericOpc;
316 }
317 case 64:
318 switch (GenericOpc) {
319 case TargetOpcode::G_SITOFP:
320 return AArch64::SCVTFUXSri;
321 case TargetOpcode::G_UITOFP:
322 return AArch64::UCVTFUXSri;
323 case TargetOpcode::G_FPTOSI:
324 return AArch64::FCVTZSUWDr;
325 case TargetOpcode::G_FPTOUI:
326 return AArch64::FCVTZUUWDr;
327 default:
328 return GenericOpc;
329 }
330 default:
331 return GenericOpc;
332 }
333 case 64:
334 switch (SrcSize) {
335 case 32:
336 switch (GenericOpc) {
337 case TargetOpcode::G_SITOFP:
338 return AArch64::SCVTFUWDri;
339 case TargetOpcode::G_UITOFP:
340 return AArch64::UCVTFUWDri;
341 case TargetOpcode::G_FPTOSI:
342 return AArch64::FCVTZSUXSr;
343 case TargetOpcode::G_FPTOUI:
344 return AArch64::FCVTZUUXSr;
345 default:
346 return GenericOpc;
347 }
348 case 64:
349 switch (GenericOpc) {
350 case TargetOpcode::G_SITOFP:
351 return AArch64::SCVTFUXDri;
352 case TargetOpcode::G_UITOFP:
353 return AArch64::UCVTFUXDri;
354 case TargetOpcode::G_FPTOSI:
355 return AArch64::FCVTZSUXDr;
356 case TargetOpcode::G_FPTOUI:
357 return AArch64::FCVTZUUXDr;
358 default:
359 return GenericOpc;
360 }
361 default:
362 return GenericOpc;
363 }
364 default:
365 return GenericOpc;
366 };
367 return GenericOpc;
368}
369
Tim Northover6c02ad52016-10-12 22:49:04 +0000370static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
371 switch (P) {
372 default:
373 llvm_unreachable("Unknown condition code!");
374 case CmpInst::ICMP_NE:
375 return AArch64CC::NE;
376 case CmpInst::ICMP_EQ:
377 return AArch64CC::EQ;
378 case CmpInst::ICMP_SGT:
379 return AArch64CC::GT;
380 case CmpInst::ICMP_SGE:
381 return AArch64CC::GE;
382 case CmpInst::ICMP_SLT:
383 return AArch64CC::LT;
384 case CmpInst::ICMP_SLE:
385 return AArch64CC::LE;
386 case CmpInst::ICMP_UGT:
387 return AArch64CC::HI;
388 case CmpInst::ICMP_UGE:
389 return AArch64CC::HS;
390 case CmpInst::ICMP_ULT:
391 return AArch64CC::LO;
392 case CmpInst::ICMP_ULE:
393 return AArch64CC::LS;
394 }
395}
396
Tim Northover7dd378d2016-10-12 22:49:07 +0000397static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
398 AArch64CC::CondCode &CondCode,
399 AArch64CC::CondCode &CondCode2) {
400 CondCode2 = AArch64CC::AL;
401 switch (P) {
402 default:
403 llvm_unreachable("Unknown FP condition!");
404 case CmpInst::FCMP_OEQ:
405 CondCode = AArch64CC::EQ;
406 break;
407 case CmpInst::FCMP_OGT:
408 CondCode = AArch64CC::GT;
409 break;
410 case CmpInst::FCMP_OGE:
411 CondCode = AArch64CC::GE;
412 break;
413 case CmpInst::FCMP_OLT:
414 CondCode = AArch64CC::MI;
415 break;
416 case CmpInst::FCMP_OLE:
417 CondCode = AArch64CC::LS;
418 break;
419 case CmpInst::FCMP_ONE:
420 CondCode = AArch64CC::MI;
421 CondCode2 = AArch64CC::GT;
422 break;
423 case CmpInst::FCMP_ORD:
424 CondCode = AArch64CC::VC;
425 break;
426 case CmpInst::FCMP_UNO:
427 CondCode = AArch64CC::VS;
428 break;
429 case CmpInst::FCMP_UEQ:
430 CondCode = AArch64CC::EQ;
431 CondCode2 = AArch64CC::VS;
432 break;
433 case CmpInst::FCMP_UGT:
434 CondCode = AArch64CC::HI;
435 break;
436 case CmpInst::FCMP_UGE:
437 CondCode = AArch64CC::PL;
438 break;
439 case CmpInst::FCMP_ULT:
440 CondCode = AArch64CC::LT;
441 break;
442 case CmpInst::FCMP_ULE:
443 CondCode = AArch64CC::LE;
444 break;
445 case CmpInst::FCMP_UNE:
446 CondCode = AArch64CC::NE;
447 break;
448 }
449}
450
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000451bool AArch64InstructionSelector::selectCompareBranch(
452 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
453
454 const unsigned CondReg = I.getOperand(0).getReg();
455 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
456 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
457 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
458 return false;
459
460 unsigned LHS = CCMI->getOperand(2).getReg();
461 unsigned RHS = CCMI->getOperand(3).getReg();
462 if (!getConstantVRegVal(RHS, MRI))
463 std::swap(RHS, LHS);
464
465 const auto RHSImm = getConstantVRegVal(RHS, MRI);
466 if (!RHSImm || *RHSImm != 0)
467 return false;
468
469 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
470 if (RB.getID() != AArch64::GPRRegBankID)
471 return false;
472
473 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
474 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
475 return false;
476
477 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
478 unsigned CBOpc = 0;
479 if (CmpWidth <= 32)
480 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
481 else if (CmpWidth == 64)
482 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
483 else
484 return false;
485
486 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
487 .addUse(LHS)
488 .addMBB(DestMBB);
489
490 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
491 I.eraseFromParent();
492 return true;
493}
494
Tim Northovere9600d82017-02-08 17:57:27 +0000495bool AArch64InstructionSelector::selectVaStartAAPCS(
496 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
497 return false;
498}
499
500bool AArch64InstructionSelector::selectVaStartDarwin(
501 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
502 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
503 unsigned ListReg = I.getOperand(0).getReg();
504
505 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
506
507 auto MIB =
508 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
509 .addDef(ArgsAddrReg)
510 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
511 .addImm(0)
512 .addImm(0);
513
514 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
515
516 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
517 .addUse(ArgsAddrReg)
518 .addUse(ListReg)
519 .addImm(0)
520 .addMemOperand(*I.memoperands_begin());
521
522 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
523 I.eraseFromParent();
524 return true;
525}
526
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000527bool AArch64InstructionSelector::select(MachineInstr &I) const {
528 assert(I.getParent() && "Instruction should be in a basic block!");
529 assert(I.getParent()->getParent() && "Instruction should be in a function!");
530
531 MachineBasicBlock &MBB = *I.getParent();
532 MachineFunction &MF = *MBB.getParent();
533 MachineRegisterInfo &MRI = MF.getRegInfo();
534
Tim Northovercdf23f12016-10-31 18:30:59 +0000535 unsigned Opcode = I.getOpcode();
536 if (!isPreISelGenericOpcode(I.getOpcode())) {
537 // Certain non-generic instructions also need some special handling.
538
539 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
540 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000541
542 if (Opcode == TargetOpcode::PHI) {
543 const unsigned DefReg = I.getOperand(0).getReg();
544 const LLT DefTy = MRI.getType(DefReg);
545
546 const TargetRegisterClass *DefRC = nullptr;
547 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
548 DefRC = TRI.getRegClass(DefReg);
549 } else {
550 const RegClassOrRegBank &RegClassOrBank =
551 MRI.getRegClassOrRegBank(DefReg);
552
553 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
554 if (!DefRC) {
555 if (!DefTy.isValid()) {
556 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
557 return false;
558 }
559 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
560 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
561 if (!DefRC) {
562 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
563 return false;
564 }
565 }
566 }
567
568 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
569 }
570
571 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000572 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000573
574 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000575 }
576
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000577
578 if (I.getNumOperands() != I.getNumExplicitOperands()) {
579 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
580 return false;
581 }
582
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000583 if (selectImpl(I))
584 return true;
585
Tim Northover32a078a2016-09-15 10:09:59 +0000586 LLT Ty =
587 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000588
Tim Northover69271c62016-10-12 22:49:11 +0000589 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000590 case TargetOpcode::G_BRCOND: {
591 if (Ty.getSizeInBits() > 32) {
592 // We shouldn't need this on AArch64, but it would be implemented as an
593 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
594 // bit being tested is < 32.
595 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
596 << ", expected at most 32-bits");
597 return false;
598 }
599
600 const unsigned CondReg = I.getOperand(0).getReg();
601 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
602
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000603 if (selectCompareBranch(I, MF, MRI))
604 return true;
605
Tim Northover5e3dbf32016-10-12 22:49:01 +0000606 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
607 .addUse(CondReg)
608 .addImm(/*bit offset=*/0)
609 .addMBB(DestMBB);
610
611 I.eraseFromParent();
612 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
613 }
614
Kristof Beyls65a12c02017-01-30 09:13:18 +0000615 case TargetOpcode::G_BRINDIRECT: {
616 I.setDesc(TII.get(AArch64::BR));
617 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
618 }
619
Tim Northover4494d692016-10-18 19:47:57 +0000620 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000621 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000622 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
623
624 const LLT s32 = LLT::scalar(32);
625 const LLT s64 = LLT::scalar(64);
626 const LLT p0 = LLT::pointer(0, 64);
627
628 const unsigned DefReg = I.getOperand(0).getReg();
629 const LLT DefTy = MRI.getType(DefReg);
630 const unsigned DefSize = DefTy.getSizeInBits();
631 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
632
633 // FIXME: Redundant check, but even less readable when factored out.
634 if (isFP) {
635 if (Ty != s32 && Ty != s64) {
636 DEBUG(dbgs() << "Unable to materialize FP " << Ty
637 << " constant, expected: " << s32 << " or " << s64
638 << '\n');
639 return false;
640 }
641
642 if (RB.getID() != AArch64::FPRRegBankID) {
643 DEBUG(dbgs() << "Unable to materialize FP " << Ty
644 << " constant on bank: " << RB << ", expected: FPR\n");
645 return false;
646 }
647 } else {
648 if (Ty != s32 && Ty != s64 && Ty != p0) {
649 DEBUG(dbgs() << "Unable to materialize integer " << Ty
650 << " constant, expected: " << s32 << ", " << s64 << ", or "
651 << p0 << '\n');
652 return false;
653 }
654
655 if (RB.getID() != AArch64::GPRRegBankID) {
656 DEBUG(dbgs() << "Unable to materialize integer " << Ty
657 << " constant on bank: " << RB << ", expected: GPR\n");
658 return false;
659 }
660 }
661
662 const unsigned MovOpc =
663 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
664
665 I.setDesc(TII.get(MovOpc));
666
667 if (isFP) {
668 const TargetRegisterClass &GPRRC =
669 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
670 const TargetRegisterClass &FPRRC =
671 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
672
673 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
674 MachineOperand &RegOp = I.getOperand(0);
675 RegOp.setReg(DefGPRReg);
676
677 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
678 TII.get(AArch64::COPY))
679 .addDef(DefReg)
680 .addUse(DefGPRReg);
681
682 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
683 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
684 return false;
685 }
686
687 MachineOperand &ImmOp = I.getOperand(1);
688 // FIXME: Is going through int64_t always correct?
689 ImmOp.ChangeToImmediate(
690 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000691 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000692 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
693 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000694 } else if (I.getOperand(1).isImm()) {
695 uint64_t Val = I.getOperand(1).getImm();
696 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000697 }
698
699 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
700 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000701 }
702
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000703 case TargetOpcode::G_FRAME_INDEX: {
704 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000705 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000706 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000707 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000708 return false;
709 }
710
711 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000712
713 // MOs for a #0 shifted immediate.
714 I.addOperand(MachineOperand::CreateImm(0));
715 I.addOperand(MachineOperand::CreateImm(0));
716
717 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
718 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000719
720 case TargetOpcode::G_GLOBAL_VALUE: {
721 auto GV = I.getOperand(1).getGlobal();
722 if (GV->isThreadLocal()) {
723 // FIXME: we don't support TLS yet.
724 return false;
725 }
726 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000727 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000728 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000729 I.getOperand(1).setTargetFlags(OpFlags);
730 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000731 I.setDesc(TII.get(AArch64::MOVaddr));
732 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
733 MachineInstrBuilder MIB(MF, I);
734 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
735 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
736 }
737 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
738 }
739
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000740 case TargetOpcode::G_LOAD:
741 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000742 LLT MemTy = Ty;
743 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000744
Tim Northover5ae83502016-09-15 09:20:34 +0000745 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000746 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000747 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000748 return false;
749 }
750
Tim Northover48dfa1a2017-02-13 22:14:16 +0000751 auto &MemOp = **I.memoperands_begin();
752 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
753 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
754 return false;
755 }
756
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000757#ifndef NDEBUG
758 // Sanity-check the pointer register.
759 const unsigned PtrReg = I.getOperand(1).getReg();
760 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
761 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
762 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000763 assert(MRI.getType(PtrReg).isPointer() &&
764 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000765#endif
766
767 const unsigned ValReg = I.getOperand(0).getReg();
768 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
769
770 const unsigned NewOpc =
771 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
772 if (NewOpc == I.getOpcode())
773 return false;
774
775 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000776
777 I.addOperand(MachineOperand::CreateImm(0));
778 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
779 }
780
Tim Northover9dd78f82017-02-08 21:22:25 +0000781 case TargetOpcode::G_SMULH:
782 case TargetOpcode::G_UMULH: {
783 // Reject the various things we don't support yet.
784 if (unsupportedBinOp(I, RBI, MRI, TRI))
785 return false;
786
787 const unsigned DefReg = I.getOperand(0).getReg();
788 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
789
790 if (RB.getID() != AArch64::GPRRegBankID) {
791 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
792 return false;
793 }
794
795 if (Ty != LLT::scalar(64)) {
796 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
797 << ", expected: " << LLT::scalar(64) << '\n');
798 return false;
799 }
800
801 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
802 : AArch64::UMULHrr;
803 I.setDesc(TII.get(NewOpc));
804
805 // Now that we selected an opcode, we need to constrain the register
806 // operands to use appropriate classes.
807 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
808 }
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000809 case TargetOpcode::G_MUL: {
810 // Reject the various things we don't support yet.
811 if (unsupportedBinOp(I, RBI, MRI, TRI))
812 return false;
813
814 const unsigned DefReg = I.getOperand(0).getReg();
815 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
816
817 if (RB.getID() != AArch64::GPRRegBankID) {
818 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
819 return false;
820 }
821
822 unsigned ZeroReg;
823 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000824 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000825 NewOpc = AArch64::MADDWrrr;
826 ZeroReg = AArch64::WZR;
827 } else if (Ty == LLT::scalar(64)) {
828 NewOpc = AArch64::MADDXrrr;
829 ZeroReg = AArch64::XZR;
830 } else {
831 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
832 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
833 return false;
834 }
835
836 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000837
838 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
839
840 // Now that we selected an opcode, we need to constrain the register
841 // operands to use appropriate classes.
842 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
843 }
844
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000845 case TargetOpcode::G_FADD:
846 case TargetOpcode::G_FSUB:
847 case TargetOpcode::G_FMUL:
848 case TargetOpcode::G_FDIV:
849
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000850 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000851 case TargetOpcode::G_SHL:
852 case TargetOpcode::G_LSHR:
853 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000854 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000855 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000856 if (unsupportedBinOp(I, RBI, MRI, TRI))
857 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000858
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000859 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000860
861 const unsigned DefReg = I.getOperand(0).getReg();
862 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
863
864 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
865 if (NewOpc == I.getOpcode())
866 return false;
867
868 I.setDesc(TII.get(NewOpc));
869 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000870
871 // Now that we selected an opcode, we need to constrain the register
872 // operands to use appropriate classes.
873 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
874 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000875
Tim Northover398c5f52017-02-14 20:56:29 +0000876 case TargetOpcode::G_PTR_MASK: {
877 uint64_t Align = I.getOperand(2).getImm();
878 if (Align >= 64 || Align == 0)
879 return false;
880
881 uint64_t Mask = ~((1ULL << Align) - 1);
882 I.setDesc(TII.get(AArch64::ANDXri));
883 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
884
885 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
886 }
Tim Northover037af52c2016-10-31 18:31:09 +0000887 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000888 case TargetOpcode::G_TRUNC: {
889 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
890 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
891
892 const unsigned DstReg = I.getOperand(0).getReg();
893 const unsigned SrcReg = I.getOperand(1).getReg();
894
895 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
896 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
897
898 if (DstRB.getID() != SrcRB.getID()) {
899 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
900 return false;
901 }
902
903 if (DstRB.getID() == AArch64::GPRRegBankID) {
904 const TargetRegisterClass *DstRC =
905 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
906 if (!DstRC)
907 return false;
908
909 const TargetRegisterClass *SrcRC =
910 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
911 if (!SrcRC)
912 return false;
913
914 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
915 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
916 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
917 return false;
918 }
919
920 if (DstRC == SrcRC) {
921 // Nothing to be done
922 } else if (DstRC == &AArch64::GPR32RegClass &&
923 SrcRC == &AArch64::GPR64RegClass) {
924 I.getOperand(1).setSubReg(AArch64::sub_32);
925 } else {
926 return false;
927 }
928
929 I.setDesc(TII.get(TargetOpcode::COPY));
930 return true;
931 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
932 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
933 I.setDesc(TII.get(AArch64::XTNv4i16));
934 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
935 return true;
936 }
937 }
938
939 return false;
940 }
941
Tim Northover3d38b3a2016-10-11 20:50:21 +0000942 case TargetOpcode::G_ANYEXT: {
943 const unsigned DstReg = I.getOperand(0).getReg();
944 const unsigned SrcReg = I.getOperand(1).getReg();
945
Quentin Colombetcb629a82016-10-12 03:57:49 +0000946 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
947 if (RBDst.getID() != AArch64::GPRRegBankID) {
948 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
949 return false;
950 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000951
Quentin Colombetcb629a82016-10-12 03:57:49 +0000952 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
953 if (RBSrc.getID() != AArch64::GPRRegBankID) {
954 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000955 return false;
956 }
957
958 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
959
960 if (DstSize == 0) {
961 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
962 return false;
963 }
964
Quentin Colombetcb629a82016-10-12 03:57:49 +0000965 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000966 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
967 << ", expected: 32 or 64\n");
968 return false;
969 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000970 // At this point G_ANYEXT is just like a plain COPY, but we need
971 // to explicitly form the 64-bit value if any.
972 if (DstSize > 32) {
973 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
974 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
975 .addDef(ExtSrc)
976 .addImm(0)
977 .addUse(SrcReg)
978 .addImm(AArch64::sub_32);
979 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000980 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000981 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000982 }
983
984 case TargetOpcode::G_ZEXT:
985 case TargetOpcode::G_SEXT: {
986 unsigned Opcode = I.getOpcode();
987 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
988 SrcTy = MRI.getType(I.getOperand(1).getReg());
989 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
990 const unsigned DefReg = I.getOperand(0).getReg();
991 const unsigned SrcReg = I.getOperand(1).getReg();
992 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
993
994 if (RB.getID() != AArch64::GPRRegBankID) {
995 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
996 << ", expected: GPR\n");
997 return false;
998 }
999
1000 MachineInstr *ExtI;
1001 if (DstTy == LLT::scalar(64)) {
1002 // FIXME: Can we avoid manually doing this?
1003 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1004 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1005 << " operand\n");
1006 return false;
1007 }
1008
1009 const unsigned SrcXReg =
1010 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1011 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1012 .addDef(SrcXReg)
1013 .addImm(0)
1014 .addUse(SrcReg)
1015 .addImm(AArch64::sub_32);
1016
1017 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1018 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1019 .addDef(DefReg)
1020 .addUse(SrcXReg)
1021 .addImm(0)
1022 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001023 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001024 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1025 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1026 .addDef(DefReg)
1027 .addUse(SrcReg)
1028 .addImm(0)
1029 .addImm(SrcTy.getSizeInBits() - 1);
1030 } else {
1031 return false;
1032 }
1033
1034 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1035
1036 I.eraseFromParent();
1037 return true;
1038 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001039
Tim Northover69271c62016-10-12 22:49:11 +00001040 case TargetOpcode::G_SITOFP:
1041 case TargetOpcode::G_UITOFP:
1042 case TargetOpcode::G_FPTOSI:
1043 case TargetOpcode::G_FPTOUI: {
1044 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1045 SrcTy = MRI.getType(I.getOperand(1).getReg());
1046 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1047 if (NewOpc == Opcode)
1048 return false;
1049
1050 I.setDesc(TII.get(NewOpc));
1051 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1052
1053 return true;
1054 }
1055
1056
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001057 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001058 case TargetOpcode::G_BITCAST:
1059 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001060
Tim Northover5f7dea82016-11-08 17:44:07 +00001061 case TargetOpcode::G_FPEXT: {
1062 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1063 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1064 << ", expected: " << LLT::scalar(64) << '\n');
1065 return false;
1066 }
1067
1068 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1069 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1070 << ", expected: " << LLT::scalar(32) << '\n');
1071 return false;
1072 }
1073
1074 const unsigned DefReg = I.getOperand(0).getReg();
1075 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1076
1077 if (RB.getID() != AArch64::FPRRegBankID) {
1078 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1079 return false;
1080 }
1081
1082 I.setDesc(TII.get(AArch64::FCVTDSr));
1083 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1084
1085 return true;
1086 }
1087
1088 case TargetOpcode::G_FPTRUNC: {
1089 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1090 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1091 << ", expected: " << LLT::scalar(32) << '\n');
1092 return false;
1093 }
1094
1095 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1096 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1097 << ", expected: " << LLT::scalar(64) << '\n');
1098 return false;
1099 }
1100
1101 const unsigned DefReg = I.getOperand(0).getReg();
1102 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1103
1104 if (RB.getID() != AArch64::FPRRegBankID) {
1105 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1106 return false;
1107 }
1108
1109 I.setDesc(TII.get(AArch64::FCVTSDr));
1110 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1111
1112 return true;
1113 }
1114
Tim Northover9ac0eba2016-11-08 00:45:29 +00001115 case TargetOpcode::G_SELECT: {
1116 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1117 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1118 << ", expected: " << LLT::scalar(1) << '\n');
1119 return false;
1120 }
1121
1122 const unsigned CondReg = I.getOperand(1).getReg();
1123 const unsigned TReg = I.getOperand(2).getReg();
1124 const unsigned FReg = I.getOperand(3).getReg();
1125
1126 unsigned CSelOpc = 0;
1127
1128 if (Ty == LLT::scalar(32)) {
1129 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001130 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001131 CSelOpc = AArch64::CSELXr;
1132 } else {
1133 return false;
1134 }
1135
1136 MachineInstr &TstMI =
1137 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1138 .addDef(AArch64::WZR)
1139 .addUse(CondReg)
1140 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1141
1142 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1143 .addDef(I.getOperand(0).getReg())
1144 .addUse(TReg)
1145 .addUse(FReg)
1146 .addImm(AArch64CC::NE);
1147
1148 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1149 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1150
1151 I.eraseFromParent();
1152 return true;
1153 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001154 case TargetOpcode::G_ICMP: {
1155 if (Ty != LLT::scalar(1)) {
1156 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1157 << ", expected: " << LLT::scalar(1) << '\n');
1158 return false;
1159 }
1160
1161 unsigned CmpOpc = 0;
1162 unsigned ZReg = 0;
1163
1164 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1165 if (CmpTy == LLT::scalar(32)) {
1166 CmpOpc = AArch64::SUBSWrr;
1167 ZReg = AArch64::WZR;
1168 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1169 CmpOpc = AArch64::SUBSXrr;
1170 ZReg = AArch64::XZR;
1171 } else {
1172 return false;
1173 }
1174
Kristof Beyls22524402017-01-05 10:16:08 +00001175 // CSINC increments the result by one when the condition code is false.
1176 // Therefore, we have to invert the predicate to get an increment by 1 when
1177 // the predicate is true.
1178 const AArch64CC::CondCode invCC =
1179 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1180 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001181
1182 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1183 .addDef(ZReg)
1184 .addUse(I.getOperand(2).getReg())
1185 .addUse(I.getOperand(3).getReg());
1186
1187 MachineInstr &CSetMI =
1188 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1189 .addDef(I.getOperand(0).getReg())
1190 .addUse(AArch64::WZR)
1191 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001192 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001193
1194 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1195 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1196
1197 I.eraseFromParent();
1198 return true;
1199 }
1200
Tim Northover7dd378d2016-10-12 22:49:07 +00001201 case TargetOpcode::G_FCMP: {
1202 if (Ty != LLT::scalar(1)) {
1203 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1204 << ", expected: " << LLT::scalar(1) << '\n');
1205 return false;
1206 }
1207
1208 unsigned CmpOpc = 0;
1209 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1210 if (CmpTy == LLT::scalar(32)) {
1211 CmpOpc = AArch64::FCMPSrr;
1212 } else if (CmpTy == LLT::scalar(64)) {
1213 CmpOpc = AArch64::FCMPDrr;
1214 } else {
1215 return false;
1216 }
1217
1218 // FIXME: regbank
1219
1220 AArch64CC::CondCode CC1, CC2;
1221 changeFCMPPredToAArch64CC(
1222 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1223
1224 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1225 .addUse(I.getOperand(2).getReg())
1226 .addUse(I.getOperand(3).getReg());
1227
1228 const unsigned DefReg = I.getOperand(0).getReg();
1229 unsigned Def1Reg = DefReg;
1230 if (CC2 != AArch64CC::AL)
1231 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1232
1233 MachineInstr &CSetMI =
1234 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1235 .addDef(Def1Reg)
1236 .addUse(AArch64::WZR)
1237 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001238 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001239
1240 if (CC2 != AArch64CC::AL) {
1241 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1242 MachineInstr &CSet2MI =
1243 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1244 .addDef(Def2Reg)
1245 .addUse(AArch64::WZR)
1246 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001247 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001248 MachineInstr &OrMI =
1249 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1250 .addDef(DefReg)
1251 .addUse(Def1Reg)
1252 .addUse(Def2Reg);
1253 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1254 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1255 }
1256
1257 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1258 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1259
1260 I.eraseFromParent();
1261 return true;
1262 }
Tim Northovere9600d82017-02-08 17:57:27 +00001263 case TargetOpcode::G_VASTART:
1264 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1265 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001266 }
1267
1268 return false;
1269}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001270
1271/// SelectArithImmed - Select an immediate value that can be represented as
1272/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1273/// Val set to the 12-bit value and Shift set to the shifter operand.
1274bool AArch64InstructionSelector::selectArithImmed(
1275 MachineOperand &Root, MachineOperand &Result1,
1276 MachineOperand &Result2) const {
1277 MachineInstr &MI = *Root.getParent();
1278 MachineBasicBlock &MBB = *MI.getParent();
1279 MachineFunction &MF = *MBB.getParent();
1280 MachineRegisterInfo &MRI = MF.getRegInfo();
1281
1282 // This function is called from the addsub_shifted_imm ComplexPattern,
1283 // which lists [imm] as the list of opcode it's interested in, however
1284 // we still need to check whether the operand is actually an immediate
1285 // here because the ComplexPattern opcode list is only used in
1286 // root-level opcode matching.
1287 uint64_t Immed;
1288 if (Root.isImm())
1289 Immed = Root.getImm();
1290 else if (Root.isCImm())
1291 Immed = Root.getCImm()->getZExtValue();
1292 else if (Root.isReg()) {
1293 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1294 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1295 return false;
Daniel Sanders0e642022017-03-16 18:04:50 +00001296 MachineOperand &Op1 = Def->getOperand(1);
1297 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1298 return false;
1299 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001300 } else
1301 return false;
1302
1303 unsigned ShiftAmt;
1304
1305 if (Immed >> 12 == 0) {
1306 ShiftAmt = 0;
1307 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1308 ShiftAmt = 12;
1309 Immed = Immed >> 12;
1310 } else
1311 return false;
1312
1313 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1314 Result1.ChangeToImmediate(Immed);
1315 Result1.clearParent();
1316 Result2.ChangeToImmediate(ShVal);
1317 Result2.clearParent();
1318 return true;
1319}