blob: 5e01b6cd2b46f68e2a268c6c853a59b4857043a3 [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
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000015#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000016#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000017#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000020#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000021#include "MCTargetDesc/AArch64AddressingModes.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000022#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Aditya Nandakumar75ad9cc2017-04-19 20:48:50 +000023#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000028#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/IR/Type.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33
34#define DEBUG_TYPE "aarch64-isel"
35
36using namespace llvm;
37
38#ifndef LLVM_BUILD_GLOBAL_ISEL
39#error "You shouldn't build this"
40#endif
41
Daniel Sanders0b5293f2017-04-06 09:49:34 +000042namespace {
43
44class AArch64InstructionSelector : public InstructionSelector {
45public:
46 AArch64InstructionSelector(const AArch64TargetMachine &TM,
47 const AArch64Subtarget &STI,
48 const AArch64RegisterBankInfo &RBI);
49
50 bool select(MachineInstr &I) const override;
51
52private:
53 /// tblgen-erated 'select' implementation, used as the initial selector for
54 /// the patterns that don't require complex C++.
55 bool selectImpl(MachineInstr &I) const;
56
57 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
58 MachineRegisterInfo &MRI) const;
59 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
60 MachineRegisterInfo &MRI) const;
61
62 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
63 MachineRegisterInfo &MRI) const;
64
65 bool selectArithImmed(MachineOperand &Root, MachineOperand &Result1,
66 MachineOperand &Result2) const;
67
68 const AArch64TargetMachine &TM;
69 const AArch64Subtarget &STI;
70 const AArch64InstrInfo &TII;
71 const AArch64RegisterInfo &TRI;
72 const AArch64RegisterBankInfo &RBI;
73
74// We declare the temporaries used by selectImpl() in the class to minimize the
75// cost of constructing placeholder values.
76#define GET_GLOBALISEL_TEMPORARIES_DECL
77#include "AArch64GenGlobalISel.inc"
78#undef GET_GLOBALISEL_TEMPORARIES_DECL
79};
80
81} // end anonymous namespace
82
Daniel Sanders8a4bae92017-03-14 21:32:08 +000083#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000084#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000085#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000086
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000087AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000088 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
89 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000090 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
91 TRI(*STI.getRegisterInfo()), RBI(RBI)
92#define GET_GLOBALISEL_TEMPORARIES_INIT
93#include "AArch64GenGlobalISel.inc"
94#undef GET_GLOBALISEL_TEMPORARIES_INIT
95{
96}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000097
Tim Northoverfb8d9892016-10-12 22:49:15 +000098// FIXME: This should be target-independent, inferred from the types declared
99// for each class in the bank.
100static const TargetRegisterClass *
101getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
102 const RegisterBankInfo &RBI) {
103 if (RB.getID() == AArch64::GPRRegBankID) {
104 if (Ty.getSizeInBits() <= 32)
105 return &AArch64::GPR32RegClass;
106 if (Ty.getSizeInBits() == 64)
107 return &AArch64::GPR64RegClass;
108 return nullptr;
109 }
110
111 if (RB.getID() == AArch64::FPRRegBankID) {
112 if (Ty.getSizeInBits() == 32)
113 return &AArch64::FPR32RegClass;
114 if (Ty.getSizeInBits() == 64)
115 return &AArch64::FPR64RegClass;
116 if (Ty.getSizeInBits() == 128)
117 return &AArch64::FPR128RegClass;
118 return nullptr;
119 }
120
121 return nullptr;
122}
123
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000124/// Check whether \p I is a currently unsupported binary operation:
125/// - it has an unsized type
126/// - an operand is not a vreg
127/// - all operands are not in the same bank
128/// These are checks that should someday live in the verifier, but right now,
129/// these are mostly limitations of the aarch64 selector.
130static bool unsupportedBinOp(const MachineInstr &I,
131 const AArch64RegisterBankInfo &RBI,
132 const MachineRegisterInfo &MRI,
133 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000134 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000135 if (!Ty.isValid()) {
136 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000137 return true;
138 }
139
140 const RegisterBank *PrevOpBank = nullptr;
141 for (auto &MO : I.operands()) {
142 // FIXME: Support non-register operands.
143 if (!MO.isReg()) {
144 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
145 return true;
146 }
147
148 // FIXME: Can generic operations have physical registers operands? If
149 // so, this will need to be taught about that, and we'll need to get the
150 // bank out of the minimal class for the register.
151 // Either way, this needs to be documented (and possibly verified).
152 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
153 DEBUG(dbgs() << "Generic inst has physical register operand\n");
154 return true;
155 }
156
157 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
158 if (!OpBank) {
159 DEBUG(dbgs() << "Generic register has no bank or class\n");
160 return true;
161 }
162
163 if (PrevOpBank && OpBank != PrevOpBank) {
164 DEBUG(dbgs() << "Generic inst operands have different banks\n");
165 return true;
166 }
167 PrevOpBank = OpBank;
168 }
169 return false;
170}
171
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000172/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000173/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000174/// and of size \p OpSize.
175/// \returns \p GenericOpc if the combination is unsupported.
176static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
177 unsigned OpSize) {
178 switch (RegBankID) {
179 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000180 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000181 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000182 case TargetOpcode::G_SHL:
183 return AArch64::LSLVWr;
184 case TargetOpcode::G_LSHR:
185 return AArch64::LSRVWr;
186 case TargetOpcode::G_ASHR:
187 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000188 default:
189 return GenericOpc;
190 }
Tim Northover55782222016-10-18 20:03:48 +0000191 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000192 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000193 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000194 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000195 case TargetOpcode::G_SHL:
196 return AArch64::LSLVXr;
197 case TargetOpcode::G_LSHR:
198 return AArch64::LSRVXr;
199 case TargetOpcode::G_ASHR:
200 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000201 default:
202 return GenericOpc;
203 }
204 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000205 case AArch64::FPRRegBankID:
206 switch (OpSize) {
207 case 32:
208 switch (GenericOpc) {
209 case TargetOpcode::G_FADD:
210 return AArch64::FADDSrr;
211 case TargetOpcode::G_FSUB:
212 return AArch64::FSUBSrr;
213 case TargetOpcode::G_FMUL:
214 return AArch64::FMULSrr;
215 case TargetOpcode::G_FDIV:
216 return AArch64::FDIVSrr;
217 default:
218 return GenericOpc;
219 }
220 case 64:
221 switch (GenericOpc) {
222 case TargetOpcode::G_FADD:
223 return AArch64::FADDDrr;
224 case TargetOpcode::G_FSUB:
225 return AArch64::FSUBDrr;
226 case TargetOpcode::G_FMUL:
227 return AArch64::FMULDrr;
228 case TargetOpcode::G_FDIV:
229 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000230 case TargetOpcode::G_OR:
231 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000232 default:
233 return GenericOpc;
234 }
235 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000236 };
237 return GenericOpc;
238}
239
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000240/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
241/// appropriate for the (value) register bank \p RegBankID and of memory access
242/// size \p OpSize. This returns the variant with the base+unsigned-immediate
243/// addressing mode (e.g., LDRXui).
244/// \returns \p GenericOpc if the combination is unsupported.
245static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
246 unsigned OpSize) {
247 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
248 switch (RegBankID) {
249 case AArch64::GPRRegBankID:
250 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000251 case 8:
252 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
253 case 16:
254 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000255 case 32:
256 return isStore ? AArch64::STRWui : AArch64::LDRWui;
257 case 64:
258 return isStore ? AArch64::STRXui : AArch64::LDRXui;
259 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000260 case AArch64::FPRRegBankID:
261 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000262 case 8:
263 return isStore ? AArch64::STRBui : AArch64::LDRBui;
264 case 16:
265 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000266 case 32:
267 return isStore ? AArch64::STRSui : AArch64::LDRSui;
268 case 64:
269 return isStore ? AArch64::STRDui : AArch64::LDRDui;
270 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000271 };
272 return GenericOpc;
273}
274
Quentin Colombetcb629a82016-10-12 03:57:49 +0000275static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
276 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
277 const RegisterBankInfo &RBI) {
278
279 unsigned DstReg = I.getOperand(0).getReg();
280 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
281 assert(I.isCopy() && "Generic operators do not allow physical registers");
282 return true;
283 }
284
285 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
286 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
287 unsigned SrcReg = I.getOperand(1).getReg();
288 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
289 (void)SrcSize;
290 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
291 "No phys reg on generic operators");
292 assert(
293 (DstSize == SrcSize ||
294 // Copies are a mean to setup initial types, the number of
295 // bits may not exactly match.
296 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
297 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
298 // Copies are a mean to copy bits around, as long as we are
299 // on the same register class, that's fine. Otherwise, that
300 // means we need some SUBREG_TO_REG or AND & co.
301 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
302 "Copy with different width?!");
303 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
304 "GPRs cannot get more than 64-bit width values");
305 const TargetRegisterClass *RC = nullptr;
306
307 if (RegBank.getID() == AArch64::FPRRegBankID) {
308 if (DstSize <= 32)
309 RC = &AArch64::FPR32RegClass;
310 else if (DstSize <= 64)
311 RC = &AArch64::FPR64RegClass;
312 else if (DstSize <= 128)
313 RC = &AArch64::FPR128RegClass;
314 else {
315 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
316 return false;
317 }
318 } else {
319 assert(RegBank.getID() == AArch64::GPRRegBankID &&
320 "Bitcast for the flags?");
321 RC =
322 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
323 }
324
325 // No need to constrain SrcReg. It will get constrained when
326 // we hit another of its use or its defs.
327 // Copies do not have constraints.
328 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
329 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
330 << " operand\n");
331 return false;
332 }
333 I.setDesc(TII.get(AArch64::COPY));
334 return true;
335}
336
Tim Northover69271c62016-10-12 22:49:11 +0000337static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
338 if (!DstTy.isScalar() || !SrcTy.isScalar())
339 return GenericOpc;
340
341 const unsigned DstSize = DstTy.getSizeInBits();
342 const unsigned SrcSize = SrcTy.getSizeInBits();
343
344 switch (DstSize) {
345 case 32:
346 switch (SrcSize) {
347 case 32:
348 switch (GenericOpc) {
349 case TargetOpcode::G_SITOFP:
350 return AArch64::SCVTFUWSri;
351 case TargetOpcode::G_UITOFP:
352 return AArch64::UCVTFUWSri;
353 case TargetOpcode::G_FPTOSI:
354 return AArch64::FCVTZSUWSr;
355 case TargetOpcode::G_FPTOUI:
356 return AArch64::FCVTZUUWSr;
357 default:
358 return GenericOpc;
359 }
360 case 64:
361 switch (GenericOpc) {
362 case TargetOpcode::G_SITOFP:
363 return AArch64::SCVTFUXSri;
364 case TargetOpcode::G_UITOFP:
365 return AArch64::UCVTFUXSri;
366 case TargetOpcode::G_FPTOSI:
367 return AArch64::FCVTZSUWDr;
368 case TargetOpcode::G_FPTOUI:
369 return AArch64::FCVTZUUWDr;
370 default:
371 return GenericOpc;
372 }
373 default:
374 return GenericOpc;
375 }
376 case 64:
377 switch (SrcSize) {
378 case 32:
379 switch (GenericOpc) {
380 case TargetOpcode::G_SITOFP:
381 return AArch64::SCVTFUWDri;
382 case TargetOpcode::G_UITOFP:
383 return AArch64::UCVTFUWDri;
384 case TargetOpcode::G_FPTOSI:
385 return AArch64::FCVTZSUXSr;
386 case TargetOpcode::G_FPTOUI:
387 return AArch64::FCVTZUUXSr;
388 default:
389 return GenericOpc;
390 }
391 case 64:
392 switch (GenericOpc) {
393 case TargetOpcode::G_SITOFP:
394 return AArch64::SCVTFUXDri;
395 case TargetOpcode::G_UITOFP:
396 return AArch64::UCVTFUXDri;
397 case TargetOpcode::G_FPTOSI:
398 return AArch64::FCVTZSUXDr;
399 case TargetOpcode::G_FPTOUI:
400 return AArch64::FCVTZUUXDr;
401 default:
402 return GenericOpc;
403 }
404 default:
405 return GenericOpc;
406 }
407 default:
408 return GenericOpc;
409 };
410 return GenericOpc;
411}
412
Tim Northover6c02ad52016-10-12 22:49:04 +0000413static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
414 switch (P) {
415 default:
416 llvm_unreachable("Unknown condition code!");
417 case CmpInst::ICMP_NE:
418 return AArch64CC::NE;
419 case CmpInst::ICMP_EQ:
420 return AArch64CC::EQ;
421 case CmpInst::ICMP_SGT:
422 return AArch64CC::GT;
423 case CmpInst::ICMP_SGE:
424 return AArch64CC::GE;
425 case CmpInst::ICMP_SLT:
426 return AArch64CC::LT;
427 case CmpInst::ICMP_SLE:
428 return AArch64CC::LE;
429 case CmpInst::ICMP_UGT:
430 return AArch64CC::HI;
431 case CmpInst::ICMP_UGE:
432 return AArch64CC::HS;
433 case CmpInst::ICMP_ULT:
434 return AArch64CC::LO;
435 case CmpInst::ICMP_ULE:
436 return AArch64CC::LS;
437 }
438}
439
Tim Northover7dd378d2016-10-12 22:49:07 +0000440static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
441 AArch64CC::CondCode &CondCode,
442 AArch64CC::CondCode &CondCode2) {
443 CondCode2 = AArch64CC::AL;
444 switch (P) {
445 default:
446 llvm_unreachable("Unknown FP condition!");
447 case CmpInst::FCMP_OEQ:
448 CondCode = AArch64CC::EQ;
449 break;
450 case CmpInst::FCMP_OGT:
451 CondCode = AArch64CC::GT;
452 break;
453 case CmpInst::FCMP_OGE:
454 CondCode = AArch64CC::GE;
455 break;
456 case CmpInst::FCMP_OLT:
457 CondCode = AArch64CC::MI;
458 break;
459 case CmpInst::FCMP_OLE:
460 CondCode = AArch64CC::LS;
461 break;
462 case CmpInst::FCMP_ONE:
463 CondCode = AArch64CC::MI;
464 CondCode2 = AArch64CC::GT;
465 break;
466 case CmpInst::FCMP_ORD:
467 CondCode = AArch64CC::VC;
468 break;
469 case CmpInst::FCMP_UNO:
470 CondCode = AArch64CC::VS;
471 break;
472 case CmpInst::FCMP_UEQ:
473 CondCode = AArch64CC::EQ;
474 CondCode2 = AArch64CC::VS;
475 break;
476 case CmpInst::FCMP_UGT:
477 CondCode = AArch64CC::HI;
478 break;
479 case CmpInst::FCMP_UGE:
480 CondCode = AArch64CC::PL;
481 break;
482 case CmpInst::FCMP_ULT:
483 CondCode = AArch64CC::LT;
484 break;
485 case CmpInst::FCMP_ULE:
486 CondCode = AArch64CC::LE;
487 break;
488 case CmpInst::FCMP_UNE:
489 CondCode = AArch64CC::NE;
490 break;
491 }
492}
493
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000494bool AArch64InstructionSelector::selectCompareBranch(
495 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
496
497 const unsigned CondReg = I.getOperand(0).getReg();
498 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
499 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
500 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
501 return false;
502
503 unsigned LHS = CCMI->getOperand(2).getReg();
504 unsigned RHS = CCMI->getOperand(3).getReg();
505 if (!getConstantVRegVal(RHS, MRI))
506 std::swap(RHS, LHS);
507
508 const auto RHSImm = getConstantVRegVal(RHS, MRI);
509 if (!RHSImm || *RHSImm != 0)
510 return false;
511
512 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
513 if (RB.getID() != AArch64::GPRRegBankID)
514 return false;
515
516 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
517 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
518 return false;
519
520 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
521 unsigned CBOpc = 0;
522 if (CmpWidth <= 32)
523 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
524 else if (CmpWidth == 64)
525 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
526 else
527 return false;
528
529 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
530 .addUse(LHS)
531 .addMBB(DestMBB);
532
533 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
534 I.eraseFromParent();
535 return true;
536}
537
Tim Northovere9600d82017-02-08 17:57:27 +0000538bool AArch64InstructionSelector::selectVaStartAAPCS(
539 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
540 return false;
541}
542
543bool AArch64InstructionSelector::selectVaStartDarwin(
544 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
545 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
546 unsigned ListReg = I.getOperand(0).getReg();
547
548 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
549
550 auto MIB =
551 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
552 .addDef(ArgsAddrReg)
553 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
554 .addImm(0)
555 .addImm(0);
556
557 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
558
559 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
560 .addUse(ArgsAddrReg)
561 .addUse(ListReg)
562 .addImm(0)
563 .addMemOperand(*I.memoperands_begin());
564
565 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
566 I.eraseFromParent();
567 return true;
568}
569
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000570bool AArch64InstructionSelector::select(MachineInstr &I) const {
571 assert(I.getParent() && "Instruction should be in a basic block!");
572 assert(I.getParent()->getParent() && "Instruction should be in a function!");
573
574 MachineBasicBlock &MBB = *I.getParent();
575 MachineFunction &MF = *MBB.getParent();
576 MachineRegisterInfo &MRI = MF.getRegInfo();
577
Tim Northovercdf23f12016-10-31 18:30:59 +0000578 unsigned Opcode = I.getOpcode();
579 if (!isPreISelGenericOpcode(I.getOpcode())) {
580 // Certain non-generic instructions also need some special handling.
581
582 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
583 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000584
585 if (Opcode == TargetOpcode::PHI) {
586 const unsigned DefReg = I.getOperand(0).getReg();
587 const LLT DefTy = MRI.getType(DefReg);
588
589 const TargetRegisterClass *DefRC = nullptr;
590 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
591 DefRC = TRI.getRegClass(DefReg);
592 } else {
593 const RegClassOrRegBank &RegClassOrBank =
594 MRI.getRegClassOrRegBank(DefReg);
595
596 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
597 if (!DefRC) {
598 if (!DefTy.isValid()) {
599 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
600 return false;
601 }
602 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
603 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
604 if (!DefRC) {
605 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
606 return false;
607 }
608 }
609 }
610
611 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
612 }
613
614 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000615 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000616
617 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000618 }
619
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000620
621 if (I.getNumOperands() != I.getNumExplicitOperands()) {
622 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
623 return false;
624 }
625
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000626 if (selectImpl(I))
627 return true;
628
Tim Northover32a078a2016-09-15 10:09:59 +0000629 LLT Ty =
630 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000631
Tim Northover69271c62016-10-12 22:49:11 +0000632 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000633 case TargetOpcode::G_BRCOND: {
634 if (Ty.getSizeInBits() > 32) {
635 // We shouldn't need this on AArch64, but it would be implemented as an
636 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
637 // bit being tested is < 32.
638 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
639 << ", expected at most 32-bits");
640 return false;
641 }
642
643 const unsigned CondReg = I.getOperand(0).getReg();
644 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
645
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000646 if (selectCompareBranch(I, MF, MRI))
647 return true;
648
Tim Northover5e3dbf32016-10-12 22:49:01 +0000649 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
650 .addUse(CondReg)
651 .addImm(/*bit offset=*/0)
652 .addMBB(DestMBB);
653
654 I.eraseFromParent();
655 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
656 }
657
Kristof Beyls65a12c02017-01-30 09:13:18 +0000658 case TargetOpcode::G_BRINDIRECT: {
659 I.setDesc(TII.get(AArch64::BR));
660 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
661 }
662
Tim Northover4494d692016-10-18 19:47:57 +0000663 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000664 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000665 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
666
667 const LLT s32 = LLT::scalar(32);
668 const LLT s64 = LLT::scalar(64);
669 const LLT p0 = LLT::pointer(0, 64);
670
671 const unsigned DefReg = I.getOperand(0).getReg();
672 const LLT DefTy = MRI.getType(DefReg);
673 const unsigned DefSize = DefTy.getSizeInBits();
674 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
675
676 // FIXME: Redundant check, but even less readable when factored out.
677 if (isFP) {
678 if (Ty != s32 && Ty != s64) {
679 DEBUG(dbgs() << "Unable to materialize FP " << Ty
680 << " constant, expected: " << s32 << " or " << s64
681 << '\n');
682 return false;
683 }
684
685 if (RB.getID() != AArch64::FPRRegBankID) {
686 DEBUG(dbgs() << "Unable to materialize FP " << Ty
687 << " constant on bank: " << RB << ", expected: FPR\n");
688 return false;
689 }
690 } else {
691 if (Ty != s32 && Ty != s64 && Ty != p0) {
692 DEBUG(dbgs() << "Unable to materialize integer " << Ty
693 << " constant, expected: " << s32 << ", " << s64 << ", or "
694 << p0 << '\n');
695 return false;
696 }
697
698 if (RB.getID() != AArch64::GPRRegBankID) {
699 DEBUG(dbgs() << "Unable to materialize integer " << Ty
700 << " constant on bank: " << RB << ", expected: GPR\n");
701 return false;
702 }
703 }
704
705 const unsigned MovOpc =
706 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
707
708 I.setDesc(TII.get(MovOpc));
709
710 if (isFP) {
711 const TargetRegisterClass &GPRRC =
712 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
713 const TargetRegisterClass &FPRRC =
714 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
715
716 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
717 MachineOperand &RegOp = I.getOperand(0);
718 RegOp.setReg(DefGPRReg);
719
720 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
721 TII.get(AArch64::COPY))
722 .addDef(DefReg)
723 .addUse(DefGPRReg);
724
725 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
726 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
727 return false;
728 }
729
730 MachineOperand &ImmOp = I.getOperand(1);
731 // FIXME: Is going through int64_t always correct?
732 ImmOp.ChangeToImmediate(
733 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000734 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000735 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
736 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000737 } else if (I.getOperand(1).isImm()) {
738 uint64_t Val = I.getOperand(1).getImm();
739 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000740 }
741
742 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
743 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000744 }
745
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000746 case TargetOpcode::G_FRAME_INDEX: {
747 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000748 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000749 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000750 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000751 return false;
752 }
753
754 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000755
756 // MOs for a #0 shifted immediate.
757 I.addOperand(MachineOperand::CreateImm(0));
758 I.addOperand(MachineOperand::CreateImm(0));
759
760 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
761 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000762
763 case TargetOpcode::G_GLOBAL_VALUE: {
764 auto GV = I.getOperand(1).getGlobal();
765 if (GV->isThreadLocal()) {
766 // FIXME: we don't support TLS yet.
767 return false;
768 }
769 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000770 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000771 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000772 I.getOperand(1).setTargetFlags(OpFlags);
773 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000774 I.setDesc(TII.get(AArch64::MOVaddr));
775 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
776 MachineInstrBuilder MIB(MF, I);
777 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
778 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
779 }
780 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
781 }
782
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000783 case TargetOpcode::G_LOAD:
784 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000785 LLT MemTy = Ty;
786 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000787
Tim Northover5ae83502016-09-15 09:20:34 +0000788 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000789 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000790 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000791 return false;
792 }
793
Tim Northover48dfa1a2017-02-13 22:14:16 +0000794 auto &MemOp = **I.memoperands_begin();
795 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
796 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
797 return false;
798 }
799
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000800 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000801#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000802 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000803 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000804 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
805 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000806 assert(MRI.getType(PtrReg).isPointer() &&
807 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000808#endif
809
810 const unsigned ValReg = I.getOperand(0).getReg();
811 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
812
813 const unsigned NewOpc =
814 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
815 if (NewOpc == I.getOpcode())
816 return false;
817
818 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000819
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000820 uint64_t Offset = 0;
821 auto *PtrMI = MRI.getVRegDef(PtrReg);
822
823 // Try to fold a GEP into our unsigned immediate addressing mode.
824 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
825 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
826 int64_t Imm = *COff;
827 const unsigned Size = MemTy.getSizeInBits() / 8;
828 const unsigned Scale = Log2_32(Size);
829 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
830 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
831 I.getOperand(1).setReg(Ptr2Reg);
832 PtrMI = MRI.getVRegDef(Ptr2Reg);
833 Offset = Imm / Size;
834 }
835 }
836 }
837
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000838 // If we haven't folded anything into our addressing mode yet, try to fold
839 // a frame index into the base+offset.
840 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
841 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
842
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000843 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000844
845 // If we're storing a 0, use WZR/XZR.
846 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
847 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
848 if (I.getOpcode() == AArch64::STRWui)
849 I.getOperand(0).setReg(AArch64::WZR);
850 else if (I.getOpcode() == AArch64::STRXui)
851 I.getOperand(0).setReg(AArch64::XZR);
852 }
853 }
854
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000855 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
856 }
857
Tim Northover9dd78f82017-02-08 21:22:25 +0000858 case TargetOpcode::G_SMULH:
859 case TargetOpcode::G_UMULH: {
860 // Reject the various things we don't support yet.
861 if (unsupportedBinOp(I, RBI, MRI, TRI))
862 return false;
863
864 const unsigned DefReg = I.getOperand(0).getReg();
865 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
866
867 if (RB.getID() != AArch64::GPRRegBankID) {
868 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
869 return false;
870 }
871
872 if (Ty != LLT::scalar(64)) {
873 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
874 << ", expected: " << LLT::scalar(64) << '\n');
875 return false;
876 }
877
878 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
879 : AArch64::UMULHrr;
880 I.setDesc(TII.get(NewOpc));
881
882 // Now that we selected an opcode, we need to constrain the register
883 // operands to use appropriate classes.
884 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
885 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000886 case TargetOpcode::G_FADD:
887 case TargetOpcode::G_FSUB:
888 case TargetOpcode::G_FMUL:
889 case TargetOpcode::G_FDIV:
890
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000891 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000892 case TargetOpcode::G_SHL:
893 case TargetOpcode::G_LSHR:
894 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000895 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000896 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000897 if (unsupportedBinOp(I, RBI, MRI, TRI))
898 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000899
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000900 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000901
902 const unsigned DefReg = I.getOperand(0).getReg();
903 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
904
905 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
906 if (NewOpc == I.getOpcode())
907 return false;
908
909 I.setDesc(TII.get(NewOpc));
910 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000911
912 // Now that we selected an opcode, we need to constrain the register
913 // operands to use appropriate classes.
914 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
915 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000916
Tim Northover398c5f52017-02-14 20:56:29 +0000917 case TargetOpcode::G_PTR_MASK: {
918 uint64_t Align = I.getOperand(2).getImm();
919 if (Align >= 64 || Align == 0)
920 return false;
921
922 uint64_t Mask = ~((1ULL << Align) - 1);
923 I.setDesc(TII.get(AArch64::ANDXri));
924 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
925
926 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
927 }
Tim Northover037af52c2016-10-31 18:31:09 +0000928 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000929 case TargetOpcode::G_TRUNC: {
930 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
931 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
932
933 const unsigned DstReg = I.getOperand(0).getReg();
934 const unsigned SrcReg = I.getOperand(1).getReg();
935
936 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
937 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
938
939 if (DstRB.getID() != SrcRB.getID()) {
940 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
941 return false;
942 }
943
944 if (DstRB.getID() == AArch64::GPRRegBankID) {
945 const TargetRegisterClass *DstRC =
946 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
947 if (!DstRC)
948 return false;
949
950 const TargetRegisterClass *SrcRC =
951 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
952 if (!SrcRC)
953 return false;
954
955 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
956 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
957 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
958 return false;
959 }
960
961 if (DstRC == SrcRC) {
962 // Nothing to be done
963 } else if (DstRC == &AArch64::GPR32RegClass &&
964 SrcRC == &AArch64::GPR64RegClass) {
965 I.getOperand(1).setSubReg(AArch64::sub_32);
966 } else {
967 return false;
968 }
969
970 I.setDesc(TII.get(TargetOpcode::COPY));
971 return true;
972 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
973 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
974 I.setDesc(TII.get(AArch64::XTNv4i16));
975 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
976 return true;
977 }
978 }
979
980 return false;
981 }
982
Tim Northover3d38b3a2016-10-11 20:50:21 +0000983 case TargetOpcode::G_ANYEXT: {
984 const unsigned DstReg = I.getOperand(0).getReg();
985 const unsigned SrcReg = I.getOperand(1).getReg();
986
Quentin Colombetcb629a82016-10-12 03:57:49 +0000987 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
988 if (RBDst.getID() != AArch64::GPRRegBankID) {
989 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
990 return false;
991 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000992
Quentin Colombetcb629a82016-10-12 03:57:49 +0000993 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
994 if (RBSrc.getID() != AArch64::GPRRegBankID) {
995 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000996 return false;
997 }
998
999 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1000
1001 if (DstSize == 0) {
1002 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1003 return false;
1004 }
1005
Quentin Colombetcb629a82016-10-12 03:57:49 +00001006 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001007 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1008 << ", expected: 32 or 64\n");
1009 return false;
1010 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001011 // At this point G_ANYEXT is just like a plain COPY, but we need
1012 // to explicitly form the 64-bit value if any.
1013 if (DstSize > 32) {
1014 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1015 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1016 .addDef(ExtSrc)
1017 .addImm(0)
1018 .addUse(SrcReg)
1019 .addImm(AArch64::sub_32);
1020 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001021 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001022 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001023 }
1024
1025 case TargetOpcode::G_ZEXT:
1026 case TargetOpcode::G_SEXT: {
1027 unsigned Opcode = I.getOpcode();
1028 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1029 SrcTy = MRI.getType(I.getOperand(1).getReg());
1030 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1031 const unsigned DefReg = I.getOperand(0).getReg();
1032 const unsigned SrcReg = I.getOperand(1).getReg();
1033 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1034
1035 if (RB.getID() != AArch64::GPRRegBankID) {
1036 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1037 << ", expected: GPR\n");
1038 return false;
1039 }
1040
1041 MachineInstr *ExtI;
1042 if (DstTy == LLT::scalar(64)) {
1043 // FIXME: Can we avoid manually doing this?
1044 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1045 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1046 << " operand\n");
1047 return false;
1048 }
1049
1050 const unsigned SrcXReg =
1051 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1052 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1053 .addDef(SrcXReg)
1054 .addImm(0)
1055 .addUse(SrcReg)
1056 .addImm(AArch64::sub_32);
1057
1058 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1059 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1060 .addDef(DefReg)
1061 .addUse(SrcXReg)
1062 .addImm(0)
1063 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001064 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001065 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1066 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1067 .addDef(DefReg)
1068 .addUse(SrcReg)
1069 .addImm(0)
1070 .addImm(SrcTy.getSizeInBits() - 1);
1071 } else {
1072 return false;
1073 }
1074
1075 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1076
1077 I.eraseFromParent();
1078 return true;
1079 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001080
Tim Northover69271c62016-10-12 22:49:11 +00001081 case TargetOpcode::G_SITOFP:
1082 case TargetOpcode::G_UITOFP:
1083 case TargetOpcode::G_FPTOSI:
1084 case TargetOpcode::G_FPTOUI: {
1085 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1086 SrcTy = MRI.getType(I.getOperand(1).getReg());
1087 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1088 if (NewOpc == Opcode)
1089 return false;
1090
1091 I.setDesc(TII.get(NewOpc));
1092 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1093
1094 return true;
1095 }
1096
1097
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001098 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001099 case TargetOpcode::G_BITCAST:
1100 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001101
Tim Northover5f7dea82016-11-08 17:44:07 +00001102 case TargetOpcode::G_FPEXT: {
1103 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1104 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1105 << ", expected: " << LLT::scalar(64) << '\n');
1106 return false;
1107 }
1108
1109 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1110 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1111 << ", expected: " << LLT::scalar(32) << '\n');
1112 return false;
1113 }
1114
1115 const unsigned DefReg = I.getOperand(0).getReg();
1116 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1117
1118 if (RB.getID() != AArch64::FPRRegBankID) {
1119 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1120 return false;
1121 }
1122
1123 I.setDesc(TII.get(AArch64::FCVTDSr));
1124 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1125
1126 return true;
1127 }
1128
1129 case TargetOpcode::G_FPTRUNC: {
1130 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1131 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1132 << ", expected: " << LLT::scalar(32) << '\n');
1133 return false;
1134 }
1135
1136 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1137 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1138 << ", expected: " << LLT::scalar(64) << '\n');
1139 return false;
1140 }
1141
1142 const unsigned DefReg = I.getOperand(0).getReg();
1143 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1144
1145 if (RB.getID() != AArch64::FPRRegBankID) {
1146 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1147 return false;
1148 }
1149
1150 I.setDesc(TII.get(AArch64::FCVTSDr));
1151 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1152
1153 return true;
1154 }
1155
Tim Northover9ac0eba2016-11-08 00:45:29 +00001156 case TargetOpcode::G_SELECT: {
1157 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1158 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1159 << ", expected: " << LLT::scalar(1) << '\n');
1160 return false;
1161 }
1162
1163 const unsigned CondReg = I.getOperand(1).getReg();
1164 const unsigned TReg = I.getOperand(2).getReg();
1165 const unsigned FReg = I.getOperand(3).getReg();
1166
1167 unsigned CSelOpc = 0;
1168
1169 if (Ty == LLT::scalar(32)) {
1170 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001171 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001172 CSelOpc = AArch64::CSELXr;
1173 } else {
1174 return false;
1175 }
1176
1177 MachineInstr &TstMI =
1178 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1179 .addDef(AArch64::WZR)
1180 .addUse(CondReg)
1181 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1182
1183 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1184 .addDef(I.getOperand(0).getReg())
1185 .addUse(TReg)
1186 .addUse(FReg)
1187 .addImm(AArch64CC::NE);
1188
1189 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1190 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1191
1192 I.eraseFromParent();
1193 return true;
1194 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001195 case TargetOpcode::G_ICMP: {
1196 if (Ty != LLT::scalar(1)) {
1197 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1198 << ", expected: " << LLT::scalar(1) << '\n');
1199 return false;
1200 }
1201
1202 unsigned CmpOpc = 0;
1203 unsigned ZReg = 0;
1204
1205 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1206 if (CmpTy == LLT::scalar(32)) {
1207 CmpOpc = AArch64::SUBSWrr;
1208 ZReg = AArch64::WZR;
1209 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1210 CmpOpc = AArch64::SUBSXrr;
1211 ZReg = AArch64::XZR;
1212 } else {
1213 return false;
1214 }
1215
Kristof Beyls22524402017-01-05 10:16:08 +00001216 // CSINC increments the result by one when the condition code is false.
1217 // Therefore, we have to invert the predicate to get an increment by 1 when
1218 // the predicate is true.
1219 const AArch64CC::CondCode invCC =
1220 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1221 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001222
1223 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1224 .addDef(ZReg)
1225 .addUse(I.getOperand(2).getReg())
1226 .addUse(I.getOperand(3).getReg());
1227
1228 MachineInstr &CSetMI =
1229 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1230 .addDef(I.getOperand(0).getReg())
1231 .addUse(AArch64::WZR)
1232 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001233 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001234
1235 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1236 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1237
1238 I.eraseFromParent();
1239 return true;
1240 }
1241
Tim Northover7dd378d2016-10-12 22:49:07 +00001242 case TargetOpcode::G_FCMP: {
1243 if (Ty != LLT::scalar(1)) {
1244 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1245 << ", expected: " << LLT::scalar(1) << '\n');
1246 return false;
1247 }
1248
1249 unsigned CmpOpc = 0;
1250 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1251 if (CmpTy == LLT::scalar(32)) {
1252 CmpOpc = AArch64::FCMPSrr;
1253 } else if (CmpTy == LLT::scalar(64)) {
1254 CmpOpc = AArch64::FCMPDrr;
1255 } else {
1256 return false;
1257 }
1258
1259 // FIXME: regbank
1260
1261 AArch64CC::CondCode CC1, CC2;
1262 changeFCMPPredToAArch64CC(
1263 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1264
1265 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1266 .addUse(I.getOperand(2).getReg())
1267 .addUse(I.getOperand(3).getReg());
1268
1269 const unsigned DefReg = I.getOperand(0).getReg();
1270 unsigned Def1Reg = DefReg;
1271 if (CC2 != AArch64CC::AL)
1272 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1273
1274 MachineInstr &CSetMI =
1275 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1276 .addDef(Def1Reg)
1277 .addUse(AArch64::WZR)
1278 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001279 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001280
1281 if (CC2 != AArch64CC::AL) {
1282 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1283 MachineInstr &CSet2MI =
1284 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1285 .addDef(Def2Reg)
1286 .addUse(AArch64::WZR)
1287 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001288 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001289 MachineInstr &OrMI =
1290 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1291 .addDef(DefReg)
1292 .addUse(Def1Reg)
1293 .addUse(Def2Reg);
1294 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1295 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1296 }
1297
1298 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1299 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1300
1301 I.eraseFromParent();
1302 return true;
1303 }
Tim Northovere9600d82017-02-08 17:57:27 +00001304 case TargetOpcode::G_VASTART:
1305 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1306 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001307 }
1308
1309 return false;
1310}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001311
1312/// SelectArithImmed - Select an immediate value that can be represented as
1313/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1314/// Val set to the 12-bit value and Shift set to the shifter operand.
1315bool AArch64InstructionSelector::selectArithImmed(
1316 MachineOperand &Root, MachineOperand &Result1,
1317 MachineOperand &Result2) const {
1318 MachineInstr &MI = *Root.getParent();
1319 MachineBasicBlock &MBB = *MI.getParent();
1320 MachineFunction &MF = *MBB.getParent();
1321 MachineRegisterInfo &MRI = MF.getRegInfo();
1322
1323 // This function is called from the addsub_shifted_imm ComplexPattern,
1324 // which lists [imm] as the list of opcode it's interested in, however
1325 // we still need to check whether the operand is actually an immediate
1326 // here because the ComplexPattern opcode list is only used in
1327 // root-level opcode matching.
1328 uint64_t Immed;
1329 if (Root.isImm())
1330 Immed = Root.getImm();
1331 else if (Root.isCImm())
1332 Immed = Root.getCImm()->getZExtValue();
1333 else if (Root.isReg()) {
1334 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1335 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1336 return false;
Daniel Sanders0e642022017-03-16 18:04:50 +00001337 MachineOperand &Op1 = Def->getOperand(1);
1338 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1339 return false;
1340 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001341 } else
1342 return false;
1343
1344 unsigned ShiftAmt;
1345
1346 if (Immed >> 12 == 0) {
1347 ShiftAmt = 0;
1348 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1349 ShiftAmt = 12;
1350 Immed = Immed >> 12;
1351 } else
1352 return false;
1353
1354 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1355 Result1.ChangeToImmediate(Immed);
1356 Result1.clearParent();
1357 Result2.ChangeToImmediate(ShVal);
1358 Result2.clearParent();
1359 return true;
1360}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001361
1362namespace llvm {
1363InstructionSelector *
1364createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1365 AArch64Subtarget &Subtarget,
1366 AArch64RegisterBankInfo &RBI) {
1367 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1368}
1369}