blob: 43f3daf8a031fc619d5fe148435bb8df4f72d40a [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
Daniel Sanders6ab0daa2017-07-04 14:35:06 +000036#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
37
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000038using namespace llvm;
39
40#ifndef LLVM_BUILD_GLOBAL_ISEL
41#error "You shouldn't build this"
42#endif
43
Daniel Sanders0b5293f2017-04-06 09:49:34 +000044namespace {
45
Daniel Sanderse7b0d662017-04-21 15:59:56 +000046#define GET_GLOBALISEL_PREDICATE_BITSET
47#include "AArch64GenGlobalISel.inc"
48#undef GET_GLOBALISEL_PREDICATE_BITSET
49
Daniel Sanders0b5293f2017-04-06 09:49:34 +000050class AArch64InstructionSelector : public InstructionSelector {
51public:
52 AArch64InstructionSelector(const AArch64TargetMachine &TM,
53 const AArch64Subtarget &STI,
54 const AArch64RegisterBankInfo &RBI);
55
56 bool select(MachineInstr &I) const override;
57
58private:
59 /// tblgen-erated 'select' implementation, used as the initial selector for
60 /// the patterns that don't require complex C++.
61 bool selectImpl(MachineInstr &I) const;
62
63 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
64 MachineRegisterInfo &MRI) const;
65 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
66 MachineRegisterInfo &MRI) const;
67
68 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
69 MachineRegisterInfo &MRI) const;
70
Daniel Sanders2deea182017-04-22 15:11:04 +000071 ComplexRendererFn selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000072
73 const AArch64TargetMachine &TM;
74 const AArch64Subtarget &STI;
75 const AArch64InstrInfo &TII;
76 const AArch64RegisterInfo &TRI;
77 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +000078
Daniel Sanderse9fdba32017-04-29 17:30:09 +000079#define GET_GLOBALISEL_PREDICATES_DECL
80#include "AArch64GenGlobalISel.inc"
81#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +000082
83// We declare the temporaries used by selectImpl() in the class to minimize the
84// cost of constructing placeholder values.
85#define GET_GLOBALISEL_TEMPORARIES_DECL
86#include "AArch64GenGlobalISel.inc"
87#undef GET_GLOBALISEL_TEMPORARIES_DECL
88};
89
90} // end anonymous namespace
91
Daniel Sanders8a4bae92017-03-14 21:32:08 +000092#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000093#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000094#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000095
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000096AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000097 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
98 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000099 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000100 TRI(*STI.getRegisterInfo()), RBI(RBI),
101#define GET_GLOBALISEL_PREDICATES_INIT
102#include "AArch64GenGlobalISel.inc"
103#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000104#define GET_GLOBALISEL_TEMPORARIES_INIT
105#include "AArch64GenGlobalISel.inc"
106#undef GET_GLOBALISEL_TEMPORARIES_INIT
107{
108}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000109
Tim Northoverfb8d9892016-10-12 22:49:15 +0000110// FIXME: This should be target-independent, inferred from the types declared
111// for each class in the bank.
112static const TargetRegisterClass *
113getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
114 const RegisterBankInfo &RBI) {
115 if (RB.getID() == AArch64::GPRRegBankID) {
116 if (Ty.getSizeInBits() <= 32)
117 return &AArch64::GPR32RegClass;
118 if (Ty.getSizeInBits() == 64)
119 return &AArch64::GPR64RegClass;
120 return nullptr;
121 }
122
123 if (RB.getID() == AArch64::FPRRegBankID) {
124 if (Ty.getSizeInBits() == 32)
125 return &AArch64::FPR32RegClass;
126 if (Ty.getSizeInBits() == 64)
127 return &AArch64::FPR64RegClass;
128 if (Ty.getSizeInBits() == 128)
129 return &AArch64::FPR128RegClass;
130 return nullptr;
131 }
132
133 return nullptr;
134}
135
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000136/// Check whether \p I is a currently unsupported binary operation:
137/// - it has an unsized type
138/// - an operand is not a vreg
139/// - all operands are not in the same bank
140/// These are checks that should someday live in the verifier, but right now,
141/// these are mostly limitations of the aarch64 selector.
142static bool unsupportedBinOp(const MachineInstr &I,
143 const AArch64RegisterBankInfo &RBI,
144 const MachineRegisterInfo &MRI,
145 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000146 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000147 if (!Ty.isValid()) {
148 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000149 return true;
150 }
151
152 const RegisterBank *PrevOpBank = nullptr;
153 for (auto &MO : I.operands()) {
154 // FIXME: Support non-register operands.
155 if (!MO.isReg()) {
156 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
157 return true;
158 }
159
160 // FIXME: Can generic operations have physical registers operands? If
161 // so, this will need to be taught about that, and we'll need to get the
162 // bank out of the minimal class for the register.
163 // Either way, this needs to be documented (and possibly verified).
164 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
165 DEBUG(dbgs() << "Generic inst has physical register operand\n");
166 return true;
167 }
168
169 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
170 if (!OpBank) {
171 DEBUG(dbgs() << "Generic register has no bank or class\n");
172 return true;
173 }
174
175 if (PrevOpBank && OpBank != PrevOpBank) {
176 DEBUG(dbgs() << "Generic inst operands have different banks\n");
177 return true;
178 }
179 PrevOpBank = OpBank;
180 }
181 return false;
182}
183
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000184/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000185/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000186/// and of size \p OpSize.
187/// \returns \p GenericOpc if the combination is unsupported.
188static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
189 unsigned OpSize) {
190 switch (RegBankID) {
191 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000192 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000194 case TargetOpcode::G_SHL:
195 return AArch64::LSLVWr;
196 case TargetOpcode::G_LSHR:
197 return AArch64::LSRVWr;
198 case TargetOpcode::G_ASHR:
199 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000200 default:
201 return GenericOpc;
202 }
Tim Northover55782222016-10-18 20:03:48 +0000203 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000204 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000205 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000206 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000207 case TargetOpcode::G_SHL:
208 return AArch64::LSLVXr;
209 case TargetOpcode::G_LSHR:
210 return AArch64::LSRVXr;
211 case TargetOpcode::G_ASHR:
212 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000213 default:
214 return GenericOpc;
215 }
216 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000217 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000218 case AArch64::FPRRegBankID:
219 switch (OpSize) {
220 case 32:
221 switch (GenericOpc) {
222 case TargetOpcode::G_FADD:
223 return AArch64::FADDSrr;
224 case TargetOpcode::G_FSUB:
225 return AArch64::FSUBSrr;
226 case TargetOpcode::G_FMUL:
227 return AArch64::FMULSrr;
228 case TargetOpcode::G_FDIV:
229 return AArch64::FDIVSrr;
230 default:
231 return GenericOpc;
232 }
233 case 64:
234 switch (GenericOpc) {
235 case TargetOpcode::G_FADD:
236 return AArch64::FADDDrr;
237 case TargetOpcode::G_FSUB:
238 return AArch64::FSUBDrr;
239 case TargetOpcode::G_FMUL:
240 return AArch64::FMULDrr;
241 case TargetOpcode::G_FDIV:
242 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000243 case TargetOpcode::G_OR:
244 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000245 default:
246 return GenericOpc;
247 }
248 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000249 break;
250 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000251 return GenericOpc;
252}
253
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000254/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
255/// appropriate for the (value) register bank \p RegBankID and of memory access
256/// size \p OpSize. This returns the variant with the base+unsigned-immediate
257/// addressing mode (e.g., LDRXui).
258/// \returns \p GenericOpc if the combination is unsupported.
259static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
260 unsigned OpSize) {
261 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
262 switch (RegBankID) {
263 case AArch64::GPRRegBankID:
264 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000265 case 8:
266 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
267 case 16:
268 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000269 case 32:
270 return isStore ? AArch64::STRWui : AArch64::LDRWui;
271 case 64:
272 return isStore ? AArch64::STRXui : AArch64::LDRXui;
273 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000274 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000275 case AArch64::FPRRegBankID:
276 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000277 case 8:
278 return isStore ? AArch64::STRBui : AArch64::LDRBui;
279 case 16:
280 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000281 case 32:
282 return isStore ? AArch64::STRSui : AArch64::LDRSui;
283 case 64:
284 return isStore ? AArch64::STRDui : AArch64::LDRDui;
285 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000286 break;
287 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000288 return GenericOpc;
289}
290
Quentin Colombetcb629a82016-10-12 03:57:49 +0000291static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
292 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
293 const RegisterBankInfo &RBI) {
294
295 unsigned DstReg = I.getOperand(0).getReg();
296 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
297 assert(I.isCopy() && "Generic operators do not allow physical registers");
298 return true;
299 }
300
301 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
302 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
303 unsigned SrcReg = I.getOperand(1).getReg();
304 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
305 (void)SrcSize;
306 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
307 "No phys reg on generic operators");
308 assert(
309 (DstSize == SrcSize ||
310 // Copies are a mean to setup initial types, the number of
311 // bits may not exactly match.
312 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
313 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
314 // Copies are a mean to copy bits around, as long as we are
315 // on the same register class, that's fine. Otherwise, that
316 // means we need some SUBREG_TO_REG or AND & co.
317 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
318 "Copy with different width?!");
319 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
320 "GPRs cannot get more than 64-bit width values");
321 const TargetRegisterClass *RC = nullptr;
322
323 if (RegBank.getID() == AArch64::FPRRegBankID) {
324 if (DstSize <= 32)
325 RC = &AArch64::FPR32RegClass;
326 else if (DstSize <= 64)
327 RC = &AArch64::FPR64RegClass;
328 else if (DstSize <= 128)
329 RC = &AArch64::FPR128RegClass;
330 else {
331 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
332 return false;
333 }
334 } else {
335 assert(RegBank.getID() == AArch64::GPRRegBankID &&
336 "Bitcast for the flags?");
337 RC =
338 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
339 }
340
341 // No need to constrain SrcReg. It will get constrained when
342 // we hit another of its use or its defs.
343 // Copies do not have constraints.
344 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
345 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
346 << " operand\n");
347 return false;
348 }
349 I.setDesc(TII.get(AArch64::COPY));
350 return true;
351}
352
Tim Northover69271c62016-10-12 22:49:11 +0000353static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
354 if (!DstTy.isScalar() || !SrcTy.isScalar())
355 return GenericOpc;
356
357 const unsigned DstSize = DstTy.getSizeInBits();
358 const unsigned SrcSize = SrcTy.getSizeInBits();
359
360 switch (DstSize) {
361 case 32:
362 switch (SrcSize) {
363 case 32:
364 switch (GenericOpc) {
365 case TargetOpcode::G_SITOFP:
366 return AArch64::SCVTFUWSri;
367 case TargetOpcode::G_UITOFP:
368 return AArch64::UCVTFUWSri;
369 case TargetOpcode::G_FPTOSI:
370 return AArch64::FCVTZSUWSr;
371 case TargetOpcode::G_FPTOUI:
372 return AArch64::FCVTZUUWSr;
373 default:
374 return GenericOpc;
375 }
376 case 64:
377 switch (GenericOpc) {
378 case TargetOpcode::G_SITOFP:
379 return AArch64::SCVTFUXSri;
380 case TargetOpcode::G_UITOFP:
381 return AArch64::UCVTFUXSri;
382 case TargetOpcode::G_FPTOSI:
383 return AArch64::FCVTZSUWDr;
384 case TargetOpcode::G_FPTOUI:
385 return AArch64::FCVTZUUWDr;
386 default:
387 return GenericOpc;
388 }
389 default:
390 return GenericOpc;
391 }
392 case 64:
393 switch (SrcSize) {
394 case 32:
395 switch (GenericOpc) {
396 case TargetOpcode::G_SITOFP:
397 return AArch64::SCVTFUWDri;
398 case TargetOpcode::G_UITOFP:
399 return AArch64::UCVTFUWDri;
400 case TargetOpcode::G_FPTOSI:
401 return AArch64::FCVTZSUXSr;
402 case TargetOpcode::G_FPTOUI:
403 return AArch64::FCVTZUUXSr;
404 default:
405 return GenericOpc;
406 }
407 case 64:
408 switch (GenericOpc) {
409 case TargetOpcode::G_SITOFP:
410 return AArch64::SCVTFUXDri;
411 case TargetOpcode::G_UITOFP:
412 return AArch64::UCVTFUXDri;
413 case TargetOpcode::G_FPTOSI:
414 return AArch64::FCVTZSUXDr;
415 case TargetOpcode::G_FPTOUI:
416 return AArch64::FCVTZUUXDr;
417 default:
418 return GenericOpc;
419 }
420 default:
421 return GenericOpc;
422 }
423 default:
424 return GenericOpc;
425 };
426 return GenericOpc;
427}
428
Tim Northover6c02ad52016-10-12 22:49:04 +0000429static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
430 switch (P) {
431 default:
432 llvm_unreachable("Unknown condition code!");
433 case CmpInst::ICMP_NE:
434 return AArch64CC::NE;
435 case CmpInst::ICMP_EQ:
436 return AArch64CC::EQ;
437 case CmpInst::ICMP_SGT:
438 return AArch64CC::GT;
439 case CmpInst::ICMP_SGE:
440 return AArch64CC::GE;
441 case CmpInst::ICMP_SLT:
442 return AArch64CC::LT;
443 case CmpInst::ICMP_SLE:
444 return AArch64CC::LE;
445 case CmpInst::ICMP_UGT:
446 return AArch64CC::HI;
447 case CmpInst::ICMP_UGE:
448 return AArch64CC::HS;
449 case CmpInst::ICMP_ULT:
450 return AArch64CC::LO;
451 case CmpInst::ICMP_ULE:
452 return AArch64CC::LS;
453 }
454}
455
Tim Northover7dd378d2016-10-12 22:49:07 +0000456static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
457 AArch64CC::CondCode &CondCode,
458 AArch64CC::CondCode &CondCode2) {
459 CondCode2 = AArch64CC::AL;
460 switch (P) {
461 default:
462 llvm_unreachable("Unknown FP condition!");
463 case CmpInst::FCMP_OEQ:
464 CondCode = AArch64CC::EQ;
465 break;
466 case CmpInst::FCMP_OGT:
467 CondCode = AArch64CC::GT;
468 break;
469 case CmpInst::FCMP_OGE:
470 CondCode = AArch64CC::GE;
471 break;
472 case CmpInst::FCMP_OLT:
473 CondCode = AArch64CC::MI;
474 break;
475 case CmpInst::FCMP_OLE:
476 CondCode = AArch64CC::LS;
477 break;
478 case CmpInst::FCMP_ONE:
479 CondCode = AArch64CC::MI;
480 CondCode2 = AArch64CC::GT;
481 break;
482 case CmpInst::FCMP_ORD:
483 CondCode = AArch64CC::VC;
484 break;
485 case CmpInst::FCMP_UNO:
486 CondCode = AArch64CC::VS;
487 break;
488 case CmpInst::FCMP_UEQ:
489 CondCode = AArch64CC::EQ;
490 CondCode2 = AArch64CC::VS;
491 break;
492 case CmpInst::FCMP_UGT:
493 CondCode = AArch64CC::HI;
494 break;
495 case CmpInst::FCMP_UGE:
496 CondCode = AArch64CC::PL;
497 break;
498 case CmpInst::FCMP_ULT:
499 CondCode = AArch64CC::LT;
500 break;
501 case CmpInst::FCMP_ULE:
502 CondCode = AArch64CC::LE;
503 break;
504 case CmpInst::FCMP_UNE:
505 CondCode = AArch64CC::NE;
506 break;
507 }
508}
509
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000510bool AArch64InstructionSelector::selectCompareBranch(
511 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
512
513 const unsigned CondReg = I.getOperand(0).getReg();
514 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
515 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
516 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
517 return false;
518
519 unsigned LHS = CCMI->getOperand(2).getReg();
520 unsigned RHS = CCMI->getOperand(3).getReg();
521 if (!getConstantVRegVal(RHS, MRI))
522 std::swap(RHS, LHS);
523
524 const auto RHSImm = getConstantVRegVal(RHS, MRI);
525 if (!RHSImm || *RHSImm != 0)
526 return false;
527
528 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
529 if (RB.getID() != AArch64::GPRRegBankID)
530 return false;
531
532 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
533 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
534 return false;
535
536 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
537 unsigned CBOpc = 0;
538 if (CmpWidth <= 32)
539 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
540 else if (CmpWidth == 64)
541 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
542 else
543 return false;
544
545 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
546 .addUse(LHS)
547 .addMBB(DestMBB);
548
549 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
550 I.eraseFromParent();
551 return true;
552}
553
Tim Northovere9600d82017-02-08 17:57:27 +0000554bool AArch64InstructionSelector::selectVaStartAAPCS(
555 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
556 return false;
557}
558
559bool AArch64InstructionSelector::selectVaStartDarwin(
560 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
561 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
562 unsigned ListReg = I.getOperand(0).getReg();
563
564 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
565
566 auto MIB =
567 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
568 .addDef(ArgsAddrReg)
569 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
570 .addImm(0)
571 .addImm(0);
572
573 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
574
575 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
576 .addUse(ArgsAddrReg)
577 .addUse(ListReg)
578 .addImm(0)
579 .addMemOperand(*I.memoperands_begin());
580
581 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
582 I.eraseFromParent();
583 return true;
584}
585
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000586bool AArch64InstructionSelector::select(MachineInstr &I) const {
587 assert(I.getParent() && "Instruction should be in a basic block!");
588 assert(I.getParent()->getParent() && "Instruction should be in a function!");
589
590 MachineBasicBlock &MBB = *I.getParent();
591 MachineFunction &MF = *MBB.getParent();
592 MachineRegisterInfo &MRI = MF.getRegInfo();
593
Tim Northovercdf23f12016-10-31 18:30:59 +0000594 unsigned Opcode = I.getOpcode();
595 if (!isPreISelGenericOpcode(I.getOpcode())) {
596 // Certain non-generic instructions also need some special handling.
597
598 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
599 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000600
601 if (Opcode == TargetOpcode::PHI) {
602 const unsigned DefReg = I.getOperand(0).getReg();
603 const LLT DefTy = MRI.getType(DefReg);
604
605 const TargetRegisterClass *DefRC = nullptr;
606 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
607 DefRC = TRI.getRegClass(DefReg);
608 } else {
609 const RegClassOrRegBank &RegClassOrBank =
610 MRI.getRegClassOrRegBank(DefReg);
611
612 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
613 if (!DefRC) {
614 if (!DefTy.isValid()) {
615 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
616 return false;
617 }
618 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
619 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
620 if (!DefRC) {
621 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
622 return false;
623 }
624 }
625 }
626
627 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
628 }
629
630 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000631 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000632
633 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000634 }
635
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000636
637 if (I.getNumOperands() != I.getNumExplicitOperands()) {
638 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
639 return false;
640 }
641
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000642 if (selectImpl(I))
643 return true;
644
Tim Northover32a078a2016-09-15 10:09:59 +0000645 LLT Ty =
646 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000647
Tim Northover69271c62016-10-12 22:49:11 +0000648 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000649 case TargetOpcode::G_BRCOND: {
650 if (Ty.getSizeInBits() > 32) {
651 // We shouldn't need this on AArch64, but it would be implemented as an
652 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
653 // bit being tested is < 32.
654 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
655 << ", expected at most 32-bits");
656 return false;
657 }
658
659 const unsigned CondReg = I.getOperand(0).getReg();
660 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
661
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000662 if (selectCompareBranch(I, MF, MRI))
663 return true;
664
Tim Northover5e3dbf32016-10-12 22:49:01 +0000665 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
666 .addUse(CondReg)
667 .addImm(/*bit offset=*/0)
668 .addMBB(DestMBB);
669
670 I.eraseFromParent();
671 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
672 }
673
Kristof Beyls65a12c02017-01-30 09:13:18 +0000674 case TargetOpcode::G_BRINDIRECT: {
675 I.setDesc(TII.get(AArch64::BR));
676 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
677 }
678
Tim Northover4494d692016-10-18 19:47:57 +0000679 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000680 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000681 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
682
683 const LLT s32 = LLT::scalar(32);
684 const LLT s64 = LLT::scalar(64);
685 const LLT p0 = LLT::pointer(0, 64);
686
687 const unsigned DefReg = I.getOperand(0).getReg();
688 const LLT DefTy = MRI.getType(DefReg);
689 const unsigned DefSize = DefTy.getSizeInBits();
690 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
691
692 // FIXME: Redundant check, but even less readable when factored out.
693 if (isFP) {
694 if (Ty != s32 && Ty != s64) {
695 DEBUG(dbgs() << "Unable to materialize FP " << Ty
696 << " constant, expected: " << s32 << " or " << s64
697 << '\n');
698 return false;
699 }
700
701 if (RB.getID() != AArch64::FPRRegBankID) {
702 DEBUG(dbgs() << "Unable to materialize FP " << Ty
703 << " constant on bank: " << RB << ", expected: FPR\n");
704 return false;
705 }
706 } else {
707 if (Ty != s32 && Ty != s64 && Ty != p0) {
708 DEBUG(dbgs() << "Unable to materialize integer " << Ty
709 << " constant, expected: " << s32 << ", " << s64 << ", or "
710 << p0 << '\n');
711 return false;
712 }
713
714 if (RB.getID() != AArch64::GPRRegBankID) {
715 DEBUG(dbgs() << "Unable to materialize integer " << Ty
716 << " constant on bank: " << RB << ", expected: GPR\n");
717 return false;
718 }
719 }
720
721 const unsigned MovOpc =
722 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
723
724 I.setDesc(TII.get(MovOpc));
725
726 if (isFP) {
727 const TargetRegisterClass &GPRRC =
728 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
729 const TargetRegisterClass &FPRRC =
730 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
731
732 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
733 MachineOperand &RegOp = I.getOperand(0);
734 RegOp.setReg(DefGPRReg);
735
736 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
737 TII.get(AArch64::COPY))
738 .addDef(DefReg)
739 .addUse(DefGPRReg);
740
741 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
742 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
743 return false;
744 }
745
746 MachineOperand &ImmOp = I.getOperand(1);
747 // FIXME: Is going through int64_t always correct?
748 ImmOp.ChangeToImmediate(
749 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000750 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000751 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
752 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000753 } else if (I.getOperand(1).isImm()) {
754 uint64_t Val = I.getOperand(1).getImm();
755 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000756 }
757
758 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
759 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000760 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000761 case TargetOpcode::G_FRAME_INDEX: {
762 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000763 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000764 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000765 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000766 return false;
767 }
Diana Picus7534b282017-07-20 11:36:03 +0000768
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000769 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000770
771 // MOs for a #0 shifted immediate.
772 I.addOperand(MachineOperand::CreateImm(0));
773 I.addOperand(MachineOperand::CreateImm(0));
774
775 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
776 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000777
778 case TargetOpcode::G_GLOBAL_VALUE: {
779 auto GV = I.getOperand(1).getGlobal();
780 if (GV->isThreadLocal()) {
781 // FIXME: we don't support TLS yet.
782 return false;
783 }
784 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000785 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000786 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000787 I.getOperand(1).setTargetFlags(OpFlags);
788 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000789 I.setDesc(TII.get(AArch64::MOVaddr));
790 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
791 MachineInstrBuilder MIB(MF, I);
792 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
793 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
794 }
795 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
796 }
797
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000798 case TargetOpcode::G_LOAD:
799 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000800 LLT MemTy = Ty;
801 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000802
Tim Northover5ae83502016-09-15 09:20:34 +0000803 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000804 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000805 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000806 return false;
807 }
808
Tim Northover48dfa1a2017-02-13 22:14:16 +0000809 auto &MemOp = **I.memoperands_begin();
810 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
811 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
812 return false;
813 }
814
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000815 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000816#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000817 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000818 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000819 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
820 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000821 assert(MRI.getType(PtrReg).isPointer() &&
822 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000823#endif
824
825 const unsigned ValReg = I.getOperand(0).getReg();
826 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
827
828 const unsigned NewOpc =
829 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
830 if (NewOpc == I.getOpcode())
831 return false;
832
833 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000834
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000835 uint64_t Offset = 0;
836 auto *PtrMI = MRI.getVRegDef(PtrReg);
837
838 // Try to fold a GEP into our unsigned immediate addressing mode.
839 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
840 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
841 int64_t Imm = *COff;
842 const unsigned Size = MemTy.getSizeInBits() / 8;
843 const unsigned Scale = Log2_32(Size);
844 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
845 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
846 I.getOperand(1).setReg(Ptr2Reg);
847 PtrMI = MRI.getVRegDef(Ptr2Reg);
848 Offset = Imm / Size;
849 }
850 }
851 }
852
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000853 // If we haven't folded anything into our addressing mode yet, try to fold
854 // a frame index into the base+offset.
855 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
856 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
857
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000858 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000859
860 // If we're storing a 0, use WZR/XZR.
861 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
862 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
863 if (I.getOpcode() == AArch64::STRWui)
864 I.getOperand(0).setReg(AArch64::WZR);
865 else if (I.getOpcode() == AArch64::STRXui)
866 I.getOperand(0).setReg(AArch64::XZR);
867 }
868 }
869
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000870 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
871 }
872
Tim Northover9dd78f82017-02-08 21:22:25 +0000873 case TargetOpcode::G_SMULH:
874 case TargetOpcode::G_UMULH: {
875 // Reject the various things we don't support yet.
876 if (unsupportedBinOp(I, RBI, MRI, TRI))
877 return false;
878
879 const unsigned DefReg = I.getOperand(0).getReg();
880 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
881
882 if (RB.getID() != AArch64::GPRRegBankID) {
883 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
884 return false;
885 }
886
887 if (Ty != LLT::scalar(64)) {
888 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
889 << ", expected: " << LLT::scalar(64) << '\n');
890 return false;
891 }
892
893 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
894 : AArch64::UMULHrr;
895 I.setDesc(TII.get(NewOpc));
896
897 // Now that we selected an opcode, we need to constrain the register
898 // operands to use appropriate classes.
899 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
900 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000901 case TargetOpcode::G_FADD:
902 case TargetOpcode::G_FSUB:
903 case TargetOpcode::G_FMUL:
904 case TargetOpcode::G_FDIV:
905
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000906 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000907 case TargetOpcode::G_SHL:
908 case TargetOpcode::G_LSHR:
909 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000910 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000911 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000912 if (unsupportedBinOp(I, RBI, MRI, TRI))
913 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000914
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000915 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000916
917 const unsigned DefReg = I.getOperand(0).getReg();
918 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
919
920 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
921 if (NewOpc == I.getOpcode())
922 return false;
923
924 I.setDesc(TII.get(NewOpc));
925 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000926
927 // Now that we selected an opcode, we need to constrain the register
928 // operands to use appropriate classes.
929 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
930 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000931
Tim Northover398c5f52017-02-14 20:56:29 +0000932 case TargetOpcode::G_PTR_MASK: {
933 uint64_t Align = I.getOperand(2).getImm();
934 if (Align >= 64 || Align == 0)
935 return false;
936
937 uint64_t Mask = ~((1ULL << Align) - 1);
938 I.setDesc(TII.get(AArch64::ANDXri));
939 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
940
941 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
942 }
Tim Northover037af52c2016-10-31 18:31:09 +0000943 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000944 case TargetOpcode::G_TRUNC: {
945 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
946 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
947
948 const unsigned DstReg = I.getOperand(0).getReg();
949 const unsigned SrcReg = I.getOperand(1).getReg();
950
951 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
952 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
953
954 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000955 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000956 return false;
957 }
958
959 if (DstRB.getID() == AArch64::GPRRegBankID) {
960 const TargetRegisterClass *DstRC =
961 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
962 if (!DstRC)
963 return false;
964
965 const TargetRegisterClass *SrcRC =
966 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
967 if (!SrcRC)
968 return false;
969
970 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
971 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000972 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000973 return false;
974 }
975
976 if (DstRC == SrcRC) {
977 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000978 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
979 SrcTy == LLT::scalar(64)) {
980 llvm_unreachable("TableGen can import this case");
981 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000982 } else if (DstRC == &AArch64::GPR32RegClass &&
983 SrcRC == &AArch64::GPR64RegClass) {
984 I.getOperand(1).setSubReg(AArch64::sub_32);
985 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000986 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000987 return false;
988 }
989
990 I.setDesc(TII.get(TargetOpcode::COPY));
991 return true;
992 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
993 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
994 I.setDesc(TII.get(AArch64::XTNv4i16));
995 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
996 return true;
997 }
998 }
999
1000 return false;
1001 }
1002
Tim Northover3d38b3a2016-10-11 20:50:21 +00001003 case TargetOpcode::G_ANYEXT: {
1004 const unsigned DstReg = I.getOperand(0).getReg();
1005 const unsigned SrcReg = I.getOperand(1).getReg();
1006
Quentin Colombetcb629a82016-10-12 03:57:49 +00001007 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1008 if (RBDst.getID() != AArch64::GPRRegBankID) {
1009 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1010 return false;
1011 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001012
Quentin Colombetcb629a82016-10-12 03:57:49 +00001013 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1014 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1015 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001016 return false;
1017 }
1018
1019 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1020
1021 if (DstSize == 0) {
1022 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1023 return false;
1024 }
1025
Quentin Colombetcb629a82016-10-12 03:57:49 +00001026 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001027 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1028 << ", expected: 32 or 64\n");
1029 return false;
1030 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001031 // At this point G_ANYEXT is just like a plain COPY, but we need
1032 // to explicitly form the 64-bit value if any.
1033 if (DstSize > 32) {
1034 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1035 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1036 .addDef(ExtSrc)
1037 .addImm(0)
1038 .addUse(SrcReg)
1039 .addImm(AArch64::sub_32);
1040 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001041 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001042 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001043 }
1044
1045 case TargetOpcode::G_ZEXT:
1046 case TargetOpcode::G_SEXT: {
1047 unsigned Opcode = I.getOpcode();
1048 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1049 SrcTy = MRI.getType(I.getOperand(1).getReg());
1050 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1051 const unsigned DefReg = I.getOperand(0).getReg();
1052 const unsigned SrcReg = I.getOperand(1).getReg();
1053 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1054
1055 if (RB.getID() != AArch64::GPRRegBankID) {
1056 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1057 << ", expected: GPR\n");
1058 return false;
1059 }
1060
1061 MachineInstr *ExtI;
1062 if (DstTy == LLT::scalar(64)) {
1063 // FIXME: Can we avoid manually doing this?
1064 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1065 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1066 << " operand\n");
1067 return false;
1068 }
1069
1070 const unsigned SrcXReg =
1071 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1072 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1073 .addDef(SrcXReg)
1074 .addImm(0)
1075 .addUse(SrcReg)
1076 .addImm(AArch64::sub_32);
1077
1078 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1079 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1080 .addDef(DefReg)
1081 .addUse(SrcXReg)
1082 .addImm(0)
1083 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001084 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001085 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1086 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1087 .addDef(DefReg)
1088 .addUse(SrcReg)
1089 .addImm(0)
1090 .addImm(SrcTy.getSizeInBits() - 1);
1091 } else {
1092 return false;
1093 }
1094
1095 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1096
1097 I.eraseFromParent();
1098 return true;
1099 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001100
Tim Northover69271c62016-10-12 22:49:11 +00001101 case TargetOpcode::G_SITOFP:
1102 case TargetOpcode::G_UITOFP:
1103 case TargetOpcode::G_FPTOSI:
1104 case TargetOpcode::G_FPTOUI: {
1105 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1106 SrcTy = MRI.getType(I.getOperand(1).getReg());
1107 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1108 if (NewOpc == Opcode)
1109 return false;
1110
1111 I.setDesc(TII.get(NewOpc));
1112 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1113
1114 return true;
1115 }
1116
1117
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001118 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001119 case TargetOpcode::G_BITCAST:
1120 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001121
Tim Northover5f7dea82016-11-08 17:44:07 +00001122 case TargetOpcode::G_FPEXT: {
1123 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1124 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1125 << ", expected: " << LLT::scalar(64) << '\n');
1126 return false;
1127 }
1128
1129 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1130 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1131 << ", expected: " << LLT::scalar(32) << '\n');
1132 return false;
1133 }
1134
1135 const unsigned DefReg = I.getOperand(0).getReg();
1136 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1137
1138 if (RB.getID() != AArch64::FPRRegBankID) {
1139 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1140 return false;
1141 }
1142
1143 I.setDesc(TII.get(AArch64::FCVTDSr));
1144 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1145
1146 return true;
1147 }
1148
1149 case TargetOpcode::G_FPTRUNC: {
1150 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1151 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1152 << ", expected: " << LLT::scalar(32) << '\n');
1153 return false;
1154 }
1155
1156 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1157 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1158 << ", expected: " << LLT::scalar(64) << '\n');
1159 return false;
1160 }
1161
1162 const unsigned DefReg = I.getOperand(0).getReg();
1163 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1164
1165 if (RB.getID() != AArch64::FPRRegBankID) {
1166 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1167 return false;
1168 }
1169
1170 I.setDesc(TII.get(AArch64::FCVTSDr));
1171 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1172
1173 return true;
1174 }
1175
Tim Northover9ac0eba2016-11-08 00:45:29 +00001176 case TargetOpcode::G_SELECT: {
1177 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1178 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1179 << ", expected: " << LLT::scalar(1) << '\n');
1180 return false;
1181 }
1182
1183 const unsigned CondReg = I.getOperand(1).getReg();
1184 const unsigned TReg = I.getOperand(2).getReg();
1185 const unsigned FReg = I.getOperand(3).getReg();
1186
1187 unsigned CSelOpc = 0;
1188
1189 if (Ty == LLT::scalar(32)) {
1190 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001191 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001192 CSelOpc = AArch64::CSELXr;
1193 } else {
1194 return false;
1195 }
1196
1197 MachineInstr &TstMI =
1198 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1199 .addDef(AArch64::WZR)
1200 .addUse(CondReg)
1201 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1202
1203 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1204 .addDef(I.getOperand(0).getReg())
1205 .addUse(TReg)
1206 .addUse(FReg)
1207 .addImm(AArch64CC::NE);
1208
1209 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1210 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1211
1212 I.eraseFromParent();
1213 return true;
1214 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001215 case TargetOpcode::G_ICMP: {
1216 if (Ty != LLT::scalar(1)) {
1217 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1218 << ", expected: " << LLT::scalar(1) << '\n');
1219 return false;
1220 }
1221
1222 unsigned CmpOpc = 0;
1223 unsigned ZReg = 0;
1224
1225 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1226 if (CmpTy == LLT::scalar(32)) {
1227 CmpOpc = AArch64::SUBSWrr;
1228 ZReg = AArch64::WZR;
1229 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1230 CmpOpc = AArch64::SUBSXrr;
1231 ZReg = AArch64::XZR;
1232 } else {
1233 return false;
1234 }
1235
Kristof Beyls22524402017-01-05 10:16:08 +00001236 // CSINC increments the result by one when the condition code is false.
1237 // Therefore, we have to invert the predicate to get an increment by 1 when
1238 // the predicate is true.
1239 const AArch64CC::CondCode invCC =
1240 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1241 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001242
1243 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1244 .addDef(ZReg)
1245 .addUse(I.getOperand(2).getReg())
1246 .addUse(I.getOperand(3).getReg());
1247
1248 MachineInstr &CSetMI =
1249 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1250 .addDef(I.getOperand(0).getReg())
1251 .addUse(AArch64::WZR)
1252 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001253 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001254
1255 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1256 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1257
1258 I.eraseFromParent();
1259 return true;
1260 }
1261
Tim Northover7dd378d2016-10-12 22:49:07 +00001262 case TargetOpcode::G_FCMP: {
1263 if (Ty != LLT::scalar(1)) {
1264 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1265 << ", expected: " << LLT::scalar(1) << '\n');
1266 return false;
1267 }
1268
1269 unsigned CmpOpc = 0;
1270 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1271 if (CmpTy == LLT::scalar(32)) {
1272 CmpOpc = AArch64::FCMPSrr;
1273 } else if (CmpTy == LLT::scalar(64)) {
1274 CmpOpc = AArch64::FCMPDrr;
1275 } else {
1276 return false;
1277 }
1278
1279 // FIXME: regbank
1280
1281 AArch64CC::CondCode CC1, CC2;
1282 changeFCMPPredToAArch64CC(
1283 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1284
1285 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1286 .addUse(I.getOperand(2).getReg())
1287 .addUse(I.getOperand(3).getReg());
1288
1289 const unsigned DefReg = I.getOperand(0).getReg();
1290 unsigned Def1Reg = DefReg;
1291 if (CC2 != AArch64CC::AL)
1292 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1293
1294 MachineInstr &CSetMI =
1295 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1296 .addDef(Def1Reg)
1297 .addUse(AArch64::WZR)
1298 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001299 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001300
1301 if (CC2 != AArch64CC::AL) {
1302 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1303 MachineInstr &CSet2MI =
1304 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1305 .addDef(Def2Reg)
1306 .addUse(AArch64::WZR)
1307 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001308 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001309 MachineInstr &OrMI =
1310 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1311 .addDef(DefReg)
1312 .addUse(Def1Reg)
1313 .addUse(Def2Reg);
1314 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1315 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1316 }
1317
1318 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1319 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1320
1321 I.eraseFromParent();
1322 return true;
1323 }
Tim Northovere9600d82017-02-08 17:57:27 +00001324 case TargetOpcode::G_VASTART:
1325 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1326 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001327 case TargetOpcode::G_IMPLICIT_DEF:
1328 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1329 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001330 }
1331
1332 return false;
1333}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001334
1335/// SelectArithImmed - Select an immediate value that can be represented as
1336/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1337/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001338InstructionSelector::ComplexRendererFn
1339AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001340 MachineInstr &MI = *Root.getParent();
1341 MachineBasicBlock &MBB = *MI.getParent();
1342 MachineFunction &MF = *MBB.getParent();
1343 MachineRegisterInfo &MRI = MF.getRegInfo();
1344
1345 // This function is called from the addsub_shifted_imm ComplexPattern,
1346 // which lists [imm] as the list of opcode it's interested in, however
1347 // we still need to check whether the operand is actually an immediate
1348 // here because the ComplexPattern opcode list is only used in
1349 // root-level opcode matching.
1350 uint64_t Immed;
1351 if (Root.isImm())
1352 Immed = Root.getImm();
1353 else if (Root.isCImm())
1354 Immed = Root.getCImm()->getZExtValue();
1355 else if (Root.isReg()) {
1356 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1357 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sanders2deea182017-04-22 15:11:04 +00001358 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001359 MachineOperand &Op1 = Def->getOperand(1);
1360 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sanders2deea182017-04-22 15:11:04 +00001361 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001362 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001363 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001364 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001365
1366 unsigned ShiftAmt;
1367
1368 if (Immed >> 12 == 0) {
1369 ShiftAmt = 0;
1370 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1371 ShiftAmt = 12;
1372 Immed = Immed >> 12;
1373 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001374 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001375
1376 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sanders2deea182017-04-22 15:11:04 +00001377 return [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed).addImm(ShVal); };
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001378}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001379
1380namespace llvm {
1381InstructionSelector *
1382createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1383 AArch64Subtarget &Subtarget,
1384 AArch64RegisterBankInfo &RBI) {
1385 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1386}
1387}