blob: 07ce0e863c5e02479470ba9de189add8f3237098 [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
Daniel Sanderse7b0d662017-04-21 15:59:56 +000044#define GET_GLOBALISEL_PREDICATE_BITSET
45#include "AArch64GenGlobalISel.inc"
46#undef GET_GLOBALISEL_PREDICATE_BITSET
47
Daniel Sanders0b5293f2017-04-06 09:49:34 +000048class AArch64InstructionSelector : public InstructionSelector {
49public:
50 AArch64InstructionSelector(const AArch64TargetMachine &TM,
51 const AArch64Subtarget &STI,
52 const AArch64RegisterBankInfo &RBI);
53
54 bool select(MachineInstr &I) const override;
55
56private:
57 /// tblgen-erated 'select' implementation, used as the initial selector for
58 /// the patterns that don't require complex C++.
59 bool selectImpl(MachineInstr &I) const;
60
61 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
62 MachineRegisterInfo &MRI) const;
63 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
64 MachineRegisterInfo &MRI) const;
65
66 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
67 MachineRegisterInfo &MRI) const;
68
Daniel Sanders2deea182017-04-22 15:11:04 +000069 ComplexRendererFn selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000070
71 const AArch64TargetMachine &TM;
72 const AArch64Subtarget &STI;
73 const AArch64InstrInfo &TII;
74 const AArch64RegisterInfo &TRI;
75 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +000076
Daniel Sanderse9fdba32017-04-29 17:30:09 +000077#define GET_GLOBALISEL_PREDICATES_DECL
78#include "AArch64GenGlobalISel.inc"
79#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +000080
81// We declare the temporaries used by selectImpl() in the class to minimize the
82// cost of constructing placeholder values.
83#define GET_GLOBALISEL_TEMPORARIES_DECL
84#include "AArch64GenGlobalISel.inc"
85#undef GET_GLOBALISEL_TEMPORARIES_DECL
86};
87
88} // end anonymous namespace
89
Daniel Sanders8a4bae92017-03-14 21:32:08 +000090#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000091#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000092#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000093
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000094AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000095 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
96 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000097 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +000098 TRI(*STI.getRegisterInfo()), RBI(RBI),
99#define GET_GLOBALISEL_PREDICATES_INIT
100#include "AArch64GenGlobalISel.inc"
101#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000102#define GET_GLOBALISEL_TEMPORARIES_INIT
103#include "AArch64GenGlobalISel.inc"
104#undef GET_GLOBALISEL_TEMPORARIES_INIT
105{
106}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000107
Tim Northoverfb8d9892016-10-12 22:49:15 +0000108// FIXME: This should be target-independent, inferred from the types declared
109// for each class in the bank.
110static const TargetRegisterClass *
111getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
112 const RegisterBankInfo &RBI) {
113 if (RB.getID() == AArch64::GPRRegBankID) {
114 if (Ty.getSizeInBits() <= 32)
115 return &AArch64::GPR32RegClass;
116 if (Ty.getSizeInBits() == 64)
117 return &AArch64::GPR64RegClass;
118 return nullptr;
119 }
120
121 if (RB.getID() == AArch64::FPRRegBankID) {
122 if (Ty.getSizeInBits() == 32)
123 return &AArch64::FPR32RegClass;
124 if (Ty.getSizeInBits() == 64)
125 return &AArch64::FPR64RegClass;
126 if (Ty.getSizeInBits() == 128)
127 return &AArch64::FPR128RegClass;
128 return nullptr;
129 }
130
131 return nullptr;
132}
133
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000134/// Check whether \p I is a currently unsupported binary operation:
135/// - it has an unsized type
136/// - an operand is not a vreg
137/// - all operands are not in the same bank
138/// These are checks that should someday live in the verifier, but right now,
139/// these are mostly limitations of the aarch64 selector.
140static bool unsupportedBinOp(const MachineInstr &I,
141 const AArch64RegisterBankInfo &RBI,
142 const MachineRegisterInfo &MRI,
143 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000144 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000145 if (!Ty.isValid()) {
146 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000147 return true;
148 }
149
150 const RegisterBank *PrevOpBank = nullptr;
151 for (auto &MO : I.operands()) {
152 // FIXME: Support non-register operands.
153 if (!MO.isReg()) {
154 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
155 return true;
156 }
157
158 // FIXME: Can generic operations have physical registers operands? If
159 // so, this will need to be taught about that, and we'll need to get the
160 // bank out of the minimal class for the register.
161 // Either way, this needs to be documented (and possibly verified).
162 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
163 DEBUG(dbgs() << "Generic inst has physical register operand\n");
164 return true;
165 }
166
167 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
168 if (!OpBank) {
169 DEBUG(dbgs() << "Generic register has no bank or class\n");
170 return true;
171 }
172
173 if (PrevOpBank && OpBank != PrevOpBank) {
174 DEBUG(dbgs() << "Generic inst operands have different banks\n");
175 return true;
176 }
177 PrevOpBank = OpBank;
178 }
179 return false;
180}
181
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000182/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000183/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000184/// and of size \p OpSize.
185/// \returns \p GenericOpc if the combination is unsupported.
186static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
187 unsigned OpSize) {
188 switch (RegBankID) {
189 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000190 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000191 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000192 case TargetOpcode::G_SHL:
193 return AArch64::LSLVWr;
194 case TargetOpcode::G_LSHR:
195 return AArch64::LSRVWr;
196 case TargetOpcode::G_ASHR:
197 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000198 default:
199 return GenericOpc;
200 }
Tim Northover55782222016-10-18 20:03:48 +0000201 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000202 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000203 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000204 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000205 case TargetOpcode::G_SHL:
206 return AArch64::LSLVXr;
207 case TargetOpcode::G_LSHR:
208 return AArch64::LSRVXr;
209 case TargetOpcode::G_ASHR:
210 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000211 default:
212 return GenericOpc;
213 }
214 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000215 case AArch64::FPRRegBankID:
216 switch (OpSize) {
217 case 32:
218 switch (GenericOpc) {
219 case TargetOpcode::G_FADD:
220 return AArch64::FADDSrr;
221 case TargetOpcode::G_FSUB:
222 return AArch64::FSUBSrr;
223 case TargetOpcode::G_FMUL:
224 return AArch64::FMULSrr;
225 case TargetOpcode::G_FDIV:
226 return AArch64::FDIVSrr;
227 default:
228 return GenericOpc;
229 }
230 case 64:
231 switch (GenericOpc) {
232 case TargetOpcode::G_FADD:
233 return AArch64::FADDDrr;
234 case TargetOpcode::G_FSUB:
235 return AArch64::FSUBDrr;
236 case TargetOpcode::G_FMUL:
237 return AArch64::FMULDrr;
238 case TargetOpcode::G_FDIV:
239 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000240 case TargetOpcode::G_OR:
241 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000242 default:
243 return GenericOpc;
244 }
245 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000246 };
247 return GenericOpc;
248}
249
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000250/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
251/// appropriate for the (value) register bank \p RegBankID and of memory access
252/// size \p OpSize. This returns the variant with the base+unsigned-immediate
253/// addressing mode (e.g., LDRXui).
254/// \returns \p GenericOpc if the combination is unsupported.
255static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
256 unsigned OpSize) {
257 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
258 switch (RegBankID) {
259 case AArch64::GPRRegBankID:
260 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000261 case 8:
262 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
263 case 16:
264 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000265 case 32:
266 return isStore ? AArch64::STRWui : AArch64::LDRWui;
267 case 64:
268 return isStore ? AArch64::STRXui : AArch64::LDRXui;
269 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000270 case AArch64::FPRRegBankID:
271 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000272 case 8:
273 return isStore ? AArch64::STRBui : AArch64::LDRBui;
274 case 16:
275 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000276 case 32:
277 return isStore ? AArch64::STRSui : AArch64::LDRSui;
278 case 64:
279 return isStore ? AArch64::STRDui : AArch64::LDRDui;
280 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000281 };
282 return GenericOpc;
283}
284
Quentin Colombetcb629a82016-10-12 03:57:49 +0000285static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
286 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
287 const RegisterBankInfo &RBI) {
288
289 unsigned DstReg = I.getOperand(0).getReg();
290 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
291 assert(I.isCopy() && "Generic operators do not allow physical registers");
292 return true;
293 }
294
295 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
296 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
297 unsigned SrcReg = I.getOperand(1).getReg();
298 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
299 (void)SrcSize;
300 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
301 "No phys reg on generic operators");
302 assert(
303 (DstSize == SrcSize ||
304 // Copies are a mean to setup initial types, the number of
305 // bits may not exactly match.
306 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
307 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
308 // Copies are a mean to copy bits around, as long as we are
309 // on the same register class, that's fine. Otherwise, that
310 // means we need some SUBREG_TO_REG or AND & co.
311 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
312 "Copy with different width?!");
313 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
314 "GPRs cannot get more than 64-bit width values");
315 const TargetRegisterClass *RC = nullptr;
316
317 if (RegBank.getID() == AArch64::FPRRegBankID) {
318 if (DstSize <= 32)
319 RC = &AArch64::FPR32RegClass;
320 else if (DstSize <= 64)
321 RC = &AArch64::FPR64RegClass;
322 else if (DstSize <= 128)
323 RC = &AArch64::FPR128RegClass;
324 else {
325 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
326 return false;
327 }
328 } else {
329 assert(RegBank.getID() == AArch64::GPRRegBankID &&
330 "Bitcast for the flags?");
331 RC =
332 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
333 }
334
335 // No need to constrain SrcReg. It will get constrained when
336 // we hit another of its use or its defs.
337 // Copies do not have constraints.
338 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
339 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
340 << " operand\n");
341 return false;
342 }
343 I.setDesc(TII.get(AArch64::COPY));
344 return true;
345}
346
Tim Northover69271c62016-10-12 22:49:11 +0000347static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
348 if (!DstTy.isScalar() || !SrcTy.isScalar())
349 return GenericOpc;
350
351 const unsigned DstSize = DstTy.getSizeInBits();
352 const unsigned SrcSize = SrcTy.getSizeInBits();
353
354 switch (DstSize) {
355 case 32:
356 switch (SrcSize) {
357 case 32:
358 switch (GenericOpc) {
359 case TargetOpcode::G_SITOFP:
360 return AArch64::SCVTFUWSri;
361 case TargetOpcode::G_UITOFP:
362 return AArch64::UCVTFUWSri;
363 case TargetOpcode::G_FPTOSI:
364 return AArch64::FCVTZSUWSr;
365 case TargetOpcode::G_FPTOUI:
366 return AArch64::FCVTZUUWSr;
367 default:
368 return GenericOpc;
369 }
370 case 64:
371 switch (GenericOpc) {
372 case TargetOpcode::G_SITOFP:
373 return AArch64::SCVTFUXSri;
374 case TargetOpcode::G_UITOFP:
375 return AArch64::UCVTFUXSri;
376 case TargetOpcode::G_FPTOSI:
377 return AArch64::FCVTZSUWDr;
378 case TargetOpcode::G_FPTOUI:
379 return AArch64::FCVTZUUWDr;
380 default:
381 return GenericOpc;
382 }
383 default:
384 return GenericOpc;
385 }
386 case 64:
387 switch (SrcSize) {
388 case 32:
389 switch (GenericOpc) {
390 case TargetOpcode::G_SITOFP:
391 return AArch64::SCVTFUWDri;
392 case TargetOpcode::G_UITOFP:
393 return AArch64::UCVTFUWDri;
394 case TargetOpcode::G_FPTOSI:
395 return AArch64::FCVTZSUXSr;
396 case TargetOpcode::G_FPTOUI:
397 return AArch64::FCVTZUUXSr;
398 default:
399 return GenericOpc;
400 }
401 case 64:
402 switch (GenericOpc) {
403 case TargetOpcode::G_SITOFP:
404 return AArch64::SCVTFUXDri;
405 case TargetOpcode::G_UITOFP:
406 return AArch64::UCVTFUXDri;
407 case TargetOpcode::G_FPTOSI:
408 return AArch64::FCVTZSUXDr;
409 case TargetOpcode::G_FPTOUI:
410 return AArch64::FCVTZUUXDr;
411 default:
412 return GenericOpc;
413 }
414 default:
415 return GenericOpc;
416 }
417 default:
418 return GenericOpc;
419 };
420 return GenericOpc;
421}
422
Tim Northover6c02ad52016-10-12 22:49:04 +0000423static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
424 switch (P) {
425 default:
426 llvm_unreachable("Unknown condition code!");
427 case CmpInst::ICMP_NE:
428 return AArch64CC::NE;
429 case CmpInst::ICMP_EQ:
430 return AArch64CC::EQ;
431 case CmpInst::ICMP_SGT:
432 return AArch64CC::GT;
433 case CmpInst::ICMP_SGE:
434 return AArch64CC::GE;
435 case CmpInst::ICMP_SLT:
436 return AArch64CC::LT;
437 case CmpInst::ICMP_SLE:
438 return AArch64CC::LE;
439 case CmpInst::ICMP_UGT:
440 return AArch64CC::HI;
441 case CmpInst::ICMP_UGE:
442 return AArch64CC::HS;
443 case CmpInst::ICMP_ULT:
444 return AArch64CC::LO;
445 case CmpInst::ICMP_ULE:
446 return AArch64CC::LS;
447 }
448}
449
Tim Northover7dd378d2016-10-12 22:49:07 +0000450static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
451 AArch64CC::CondCode &CondCode,
452 AArch64CC::CondCode &CondCode2) {
453 CondCode2 = AArch64CC::AL;
454 switch (P) {
455 default:
456 llvm_unreachable("Unknown FP condition!");
457 case CmpInst::FCMP_OEQ:
458 CondCode = AArch64CC::EQ;
459 break;
460 case CmpInst::FCMP_OGT:
461 CondCode = AArch64CC::GT;
462 break;
463 case CmpInst::FCMP_OGE:
464 CondCode = AArch64CC::GE;
465 break;
466 case CmpInst::FCMP_OLT:
467 CondCode = AArch64CC::MI;
468 break;
469 case CmpInst::FCMP_OLE:
470 CondCode = AArch64CC::LS;
471 break;
472 case CmpInst::FCMP_ONE:
473 CondCode = AArch64CC::MI;
474 CondCode2 = AArch64CC::GT;
475 break;
476 case CmpInst::FCMP_ORD:
477 CondCode = AArch64CC::VC;
478 break;
479 case CmpInst::FCMP_UNO:
480 CondCode = AArch64CC::VS;
481 break;
482 case CmpInst::FCMP_UEQ:
483 CondCode = AArch64CC::EQ;
484 CondCode2 = AArch64CC::VS;
485 break;
486 case CmpInst::FCMP_UGT:
487 CondCode = AArch64CC::HI;
488 break;
489 case CmpInst::FCMP_UGE:
490 CondCode = AArch64CC::PL;
491 break;
492 case CmpInst::FCMP_ULT:
493 CondCode = AArch64CC::LT;
494 break;
495 case CmpInst::FCMP_ULE:
496 CondCode = AArch64CC::LE;
497 break;
498 case CmpInst::FCMP_UNE:
499 CondCode = AArch64CC::NE;
500 break;
501 }
502}
503
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000504bool AArch64InstructionSelector::selectCompareBranch(
505 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
506
507 const unsigned CondReg = I.getOperand(0).getReg();
508 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
509 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
510 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
511 return false;
512
513 unsigned LHS = CCMI->getOperand(2).getReg();
514 unsigned RHS = CCMI->getOperand(3).getReg();
515 if (!getConstantVRegVal(RHS, MRI))
516 std::swap(RHS, LHS);
517
518 const auto RHSImm = getConstantVRegVal(RHS, MRI);
519 if (!RHSImm || *RHSImm != 0)
520 return false;
521
522 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
523 if (RB.getID() != AArch64::GPRRegBankID)
524 return false;
525
526 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
527 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
528 return false;
529
530 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
531 unsigned CBOpc = 0;
532 if (CmpWidth <= 32)
533 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
534 else if (CmpWidth == 64)
535 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
536 else
537 return false;
538
539 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
540 .addUse(LHS)
541 .addMBB(DestMBB);
542
543 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
544 I.eraseFromParent();
545 return true;
546}
547
Tim Northovere9600d82017-02-08 17:57:27 +0000548bool AArch64InstructionSelector::selectVaStartAAPCS(
549 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
550 return false;
551}
552
553bool AArch64InstructionSelector::selectVaStartDarwin(
554 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
555 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
556 unsigned ListReg = I.getOperand(0).getReg();
557
558 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
559
560 auto MIB =
561 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
562 .addDef(ArgsAddrReg)
563 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
564 .addImm(0)
565 .addImm(0);
566
567 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
568
569 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
570 .addUse(ArgsAddrReg)
571 .addUse(ListReg)
572 .addImm(0)
573 .addMemOperand(*I.memoperands_begin());
574
575 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
576 I.eraseFromParent();
577 return true;
578}
579
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000580bool AArch64InstructionSelector::select(MachineInstr &I) const {
581 assert(I.getParent() && "Instruction should be in a basic block!");
582 assert(I.getParent()->getParent() && "Instruction should be in a function!");
583
584 MachineBasicBlock &MBB = *I.getParent();
585 MachineFunction &MF = *MBB.getParent();
586 MachineRegisterInfo &MRI = MF.getRegInfo();
587
Tim Northovercdf23f12016-10-31 18:30:59 +0000588 unsigned Opcode = I.getOpcode();
589 if (!isPreISelGenericOpcode(I.getOpcode())) {
590 // Certain non-generic instructions also need some special handling.
591
592 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
593 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000594
595 if (Opcode == TargetOpcode::PHI) {
596 const unsigned DefReg = I.getOperand(0).getReg();
597 const LLT DefTy = MRI.getType(DefReg);
598
599 const TargetRegisterClass *DefRC = nullptr;
600 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
601 DefRC = TRI.getRegClass(DefReg);
602 } else {
603 const RegClassOrRegBank &RegClassOrBank =
604 MRI.getRegClassOrRegBank(DefReg);
605
606 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
607 if (!DefRC) {
608 if (!DefTy.isValid()) {
609 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
610 return false;
611 }
612 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
613 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
614 if (!DefRC) {
615 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
616 return false;
617 }
618 }
619 }
620
621 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
622 }
623
624 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000625 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000626
627 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000628 }
629
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000630
631 if (I.getNumOperands() != I.getNumExplicitOperands()) {
632 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
633 return false;
634 }
635
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000636 if (selectImpl(I))
637 return true;
638
Tim Northover32a078a2016-09-15 10:09:59 +0000639 LLT Ty =
640 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000641
Tim Northover69271c62016-10-12 22:49:11 +0000642 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000643 case TargetOpcode::G_BRCOND: {
644 if (Ty.getSizeInBits() > 32) {
645 // We shouldn't need this on AArch64, but it would be implemented as an
646 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
647 // bit being tested is < 32.
648 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
649 << ", expected at most 32-bits");
650 return false;
651 }
652
653 const unsigned CondReg = I.getOperand(0).getReg();
654 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
655
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000656 if (selectCompareBranch(I, MF, MRI))
657 return true;
658
Tim Northover5e3dbf32016-10-12 22:49:01 +0000659 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
660 .addUse(CondReg)
661 .addImm(/*bit offset=*/0)
662 .addMBB(DestMBB);
663
664 I.eraseFromParent();
665 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
666 }
667
Kristof Beyls65a12c02017-01-30 09:13:18 +0000668 case TargetOpcode::G_BRINDIRECT: {
669 I.setDesc(TII.get(AArch64::BR));
670 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
671 }
672
Tim Northover4494d692016-10-18 19:47:57 +0000673 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000674 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000675 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
676
677 const LLT s32 = LLT::scalar(32);
678 const LLT s64 = LLT::scalar(64);
679 const LLT p0 = LLT::pointer(0, 64);
680
681 const unsigned DefReg = I.getOperand(0).getReg();
682 const LLT DefTy = MRI.getType(DefReg);
683 const unsigned DefSize = DefTy.getSizeInBits();
684 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
685
686 // FIXME: Redundant check, but even less readable when factored out.
687 if (isFP) {
688 if (Ty != s32 && Ty != s64) {
689 DEBUG(dbgs() << "Unable to materialize FP " << Ty
690 << " constant, expected: " << s32 << " or " << s64
691 << '\n');
692 return false;
693 }
694
695 if (RB.getID() != AArch64::FPRRegBankID) {
696 DEBUG(dbgs() << "Unable to materialize FP " << Ty
697 << " constant on bank: " << RB << ", expected: FPR\n");
698 return false;
699 }
700 } else {
701 if (Ty != s32 && Ty != s64 && Ty != p0) {
702 DEBUG(dbgs() << "Unable to materialize integer " << Ty
703 << " constant, expected: " << s32 << ", " << s64 << ", or "
704 << p0 << '\n');
705 return false;
706 }
707
708 if (RB.getID() != AArch64::GPRRegBankID) {
709 DEBUG(dbgs() << "Unable to materialize integer " << Ty
710 << " constant on bank: " << RB << ", expected: GPR\n");
711 return false;
712 }
713 }
714
715 const unsigned MovOpc =
716 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
717
718 I.setDesc(TII.get(MovOpc));
719
720 if (isFP) {
721 const TargetRegisterClass &GPRRC =
722 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
723 const TargetRegisterClass &FPRRC =
724 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
725
726 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
727 MachineOperand &RegOp = I.getOperand(0);
728 RegOp.setReg(DefGPRReg);
729
730 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
731 TII.get(AArch64::COPY))
732 .addDef(DefReg)
733 .addUse(DefGPRReg);
734
735 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
736 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
737 return false;
738 }
739
740 MachineOperand &ImmOp = I.getOperand(1);
741 // FIXME: Is going through int64_t always correct?
742 ImmOp.ChangeToImmediate(
743 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000744 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000745 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
746 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000747 } else if (I.getOperand(1).isImm()) {
748 uint64_t Val = I.getOperand(1).getImm();
749 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000750 }
751
752 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
753 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000754 }
755
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000756 case TargetOpcode::G_FRAME_INDEX: {
757 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000758 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000759 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000760 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000761 return false;
762 }
763
764 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000765
766 // MOs for a #0 shifted immediate.
767 I.addOperand(MachineOperand::CreateImm(0));
768 I.addOperand(MachineOperand::CreateImm(0));
769
770 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
771 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000772
773 case TargetOpcode::G_GLOBAL_VALUE: {
774 auto GV = I.getOperand(1).getGlobal();
775 if (GV->isThreadLocal()) {
776 // FIXME: we don't support TLS yet.
777 return false;
778 }
779 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000780 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000781 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000782 I.getOperand(1).setTargetFlags(OpFlags);
783 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000784 I.setDesc(TII.get(AArch64::MOVaddr));
785 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
786 MachineInstrBuilder MIB(MF, I);
787 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
788 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
789 }
790 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
791 }
792
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000793 case TargetOpcode::G_LOAD:
794 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000795 LLT MemTy = Ty;
796 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000797
Tim Northover5ae83502016-09-15 09:20:34 +0000798 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000799 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000800 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000801 return false;
802 }
803
Tim Northover48dfa1a2017-02-13 22:14:16 +0000804 auto &MemOp = **I.memoperands_begin();
805 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
806 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
807 return false;
808 }
809
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000810 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000811#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000812 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000813 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000814 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
815 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000816 assert(MRI.getType(PtrReg).isPointer() &&
817 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000818#endif
819
820 const unsigned ValReg = I.getOperand(0).getReg();
821 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
822
823 const unsigned NewOpc =
824 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
825 if (NewOpc == I.getOpcode())
826 return false;
827
828 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000829
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000830 uint64_t Offset = 0;
831 auto *PtrMI = MRI.getVRegDef(PtrReg);
832
833 // Try to fold a GEP into our unsigned immediate addressing mode.
834 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
835 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
836 int64_t Imm = *COff;
837 const unsigned Size = MemTy.getSizeInBits() / 8;
838 const unsigned Scale = Log2_32(Size);
839 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
840 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
841 I.getOperand(1).setReg(Ptr2Reg);
842 PtrMI = MRI.getVRegDef(Ptr2Reg);
843 Offset = Imm / Size;
844 }
845 }
846 }
847
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000848 // If we haven't folded anything into our addressing mode yet, try to fold
849 // a frame index into the base+offset.
850 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
851 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
852
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000853 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000854
855 // If we're storing a 0, use WZR/XZR.
856 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
857 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
858 if (I.getOpcode() == AArch64::STRWui)
859 I.getOperand(0).setReg(AArch64::WZR);
860 else if (I.getOpcode() == AArch64::STRXui)
861 I.getOperand(0).setReg(AArch64::XZR);
862 }
863 }
864
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000865 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
866 }
867
Tim Northover9dd78f82017-02-08 21:22:25 +0000868 case TargetOpcode::G_SMULH:
869 case TargetOpcode::G_UMULH: {
870 // Reject the various things we don't support yet.
871 if (unsupportedBinOp(I, RBI, MRI, TRI))
872 return false;
873
874 const unsigned DefReg = I.getOperand(0).getReg();
875 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
876
877 if (RB.getID() != AArch64::GPRRegBankID) {
878 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
879 return false;
880 }
881
882 if (Ty != LLT::scalar(64)) {
883 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
884 << ", expected: " << LLT::scalar(64) << '\n');
885 return false;
886 }
887
888 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
889 : AArch64::UMULHrr;
890 I.setDesc(TII.get(NewOpc));
891
892 // Now that we selected an opcode, we need to constrain the register
893 // operands to use appropriate classes.
894 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
895 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000896 case TargetOpcode::G_FADD:
897 case TargetOpcode::G_FSUB:
898 case TargetOpcode::G_FMUL:
899 case TargetOpcode::G_FDIV:
900
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000901 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000902 case TargetOpcode::G_SHL:
903 case TargetOpcode::G_LSHR:
904 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000905 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000906 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000907 if (unsupportedBinOp(I, RBI, MRI, TRI))
908 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000909
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000910 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000911
912 const unsigned DefReg = I.getOperand(0).getReg();
913 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
914
915 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
916 if (NewOpc == I.getOpcode())
917 return false;
918
919 I.setDesc(TII.get(NewOpc));
920 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000921
922 // Now that we selected an opcode, we need to constrain the register
923 // operands to use appropriate classes.
924 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
925 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000926
Tim Northover398c5f52017-02-14 20:56:29 +0000927 case TargetOpcode::G_PTR_MASK: {
928 uint64_t Align = I.getOperand(2).getImm();
929 if (Align >= 64 || Align == 0)
930 return false;
931
932 uint64_t Mask = ~((1ULL << Align) - 1);
933 I.setDesc(TII.get(AArch64::ANDXri));
934 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
935
936 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
937 }
Tim Northover037af52c2016-10-31 18:31:09 +0000938 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000939 case TargetOpcode::G_TRUNC: {
940 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
941 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
942
943 const unsigned DstReg = I.getOperand(0).getReg();
944 const unsigned SrcReg = I.getOperand(1).getReg();
945
946 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
947 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
948
949 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000950 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000951 return false;
952 }
953
954 if (DstRB.getID() == AArch64::GPRRegBankID) {
955 const TargetRegisterClass *DstRC =
956 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
957 if (!DstRC)
958 return false;
959
960 const TargetRegisterClass *SrcRC =
961 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
962 if (!SrcRC)
963 return false;
964
965 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
966 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000967 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000968 return false;
969 }
970
971 if (DstRC == SrcRC) {
972 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000973 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
974 SrcTy == LLT::scalar(64)) {
975 llvm_unreachable("TableGen can import this case");
976 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000977 } else if (DstRC == &AArch64::GPR32RegClass &&
978 SrcRC == &AArch64::GPR64RegClass) {
979 I.getOperand(1).setSubReg(AArch64::sub_32);
980 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000981 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000982 return false;
983 }
984
985 I.setDesc(TII.get(TargetOpcode::COPY));
986 return true;
987 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
988 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
989 I.setDesc(TII.get(AArch64::XTNv4i16));
990 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
991 return true;
992 }
993 }
994
995 return false;
996 }
997
Tim Northover3d38b3a2016-10-11 20:50:21 +0000998 case TargetOpcode::G_ANYEXT: {
999 const unsigned DstReg = I.getOperand(0).getReg();
1000 const unsigned SrcReg = I.getOperand(1).getReg();
1001
Quentin Colombetcb629a82016-10-12 03:57:49 +00001002 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1003 if (RBDst.getID() != AArch64::GPRRegBankID) {
1004 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1005 return false;
1006 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001007
Quentin Colombetcb629a82016-10-12 03:57:49 +00001008 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1009 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1010 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001011 return false;
1012 }
1013
1014 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1015
1016 if (DstSize == 0) {
1017 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1018 return false;
1019 }
1020
Quentin Colombetcb629a82016-10-12 03:57:49 +00001021 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001022 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1023 << ", expected: 32 or 64\n");
1024 return false;
1025 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001026 // At this point G_ANYEXT is just like a plain COPY, but we need
1027 // to explicitly form the 64-bit value if any.
1028 if (DstSize > 32) {
1029 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1030 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1031 .addDef(ExtSrc)
1032 .addImm(0)
1033 .addUse(SrcReg)
1034 .addImm(AArch64::sub_32);
1035 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001036 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001037 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001038 }
1039
1040 case TargetOpcode::G_ZEXT:
1041 case TargetOpcode::G_SEXT: {
1042 unsigned Opcode = I.getOpcode();
1043 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1044 SrcTy = MRI.getType(I.getOperand(1).getReg());
1045 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1046 const unsigned DefReg = I.getOperand(0).getReg();
1047 const unsigned SrcReg = I.getOperand(1).getReg();
1048 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1049
1050 if (RB.getID() != AArch64::GPRRegBankID) {
1051 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1052 << ", expected: GPR\n");
1053 return false;
1054 }
1055
1056 MachineInstr *ExtI;
1057 if (DstTy == LLT::scalar(64)) {
1058 // FIXME: Can we avoid manually doing this?
1059 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1060 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1061 << " operand\n");
1062 return false;
1063 }
1064
1065 const unsigned SrcXReg =
1066 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1067 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1068 .addDef(SrcXReg)
1069 .addImm(0)
1070 .addUse(SrcReg)
1071 .addImm(AArch64::sub_32);
1072
1073 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1074 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1075 .addDef(DefReg)
1076 .addUse(SrcXReg)
1077 .addImm(0)
1078 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001079 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001080 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1081 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1082 .addDef(DefReg)
1083 .addUse(SrcReg)
1084 .addImm(0)
1085 .addImm(SrcTy.getSizeInBits() - 1);
1086 } else {
1087 return false;
1088 }
1089
1090 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1091
1092 I.eraseFromParent();
1093 return true;
1094 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001095
Tim Northover69271c62016-10-12 22:49:11 +00001096 case TargetOpcode::G_SITOFP:
1097 case TargetOpcode::G_UITOFP:
1098 case TargetOpcode::G_FPTOSI:
1099 case TargetOpcode::G_FPTOUI: {
1100 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1101 SrcTy = MRI.getType(I.getOperand(1).getReg());
1102 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1103 if (NewOpc == Opcode)
1104 return false;
1105
1106 I.setDesc(TII.get(NewOpc));
1107 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1108
1109 return true;
1110 }
1111
1112
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001113 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001114 case TargetOpcode::G_BITCAST:
1115 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001116
Tim Northover5f7dea82016-11-08 17:44:07 +00001117 case TargetOpcode::G_FPEXT: {
1118 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1119 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1120 << ", expected: " << LLT::scalar(64) << '\n');
1121 return false;
1122 }
1123
1124 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1125 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1126 << ", expected: " << LLT::scalar(32) << '\n');
1127 return false;
1128 }
1129
1130 const unsigned DefReg = I.getOperand(0).getReg();
1131 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1132
1133 if (RB.getID() != AArch64::FPRRegBankID) {
1134 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1135 return false;
1136 }
1137
1138 I.setDesc(TII.get(AArch64::FCVTDSr));
1139 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1140
1141 return true;
1142 }
1143
1144 case TargetOpcode::G_FPTRUNC: {
1145 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1146 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1147 << ", expected: " << LLT::scalar(32) << '\n');
1148 return false;
1149 }
1150
1151 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1152 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1153 << ", expected: " << LLT::scalar(64) << '\n');
1154 return false;
1155 }
1156
1157 const unsigned DefReg = I.getOperand(0).getReg();
1158 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1159
1160 if (RB.getID() != AArch64::FPRRegBankID) {
1161 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1162 return false;
1163 }
1164
1165 I.setDesc(TII.get(AArch64::FCVTSDr));
1166 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1167
1168 return true;
1169 }
1170
Tim Northover9ac0eba2016-11-08 00:45:29 +00001171 case TargetOpcode::G_SELECT: {
1172 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1173 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1174 << ", expected: " << LLT::scalar(1) << '\n');
1175 return false;
1176 }
1177
1178 const unsigned CondReg = I.getOperand(1).getReg();
1179 const unsigned TReg = I.getOperand(2).getReg();
1180 const unsigned FReg = I.getOperand(3).getReg();
1181
1182 unsigned CSelOpc = 0;
1183
1184 if (Ty == LLT::scalar(32)) {
1185 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001186 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001187 CSelOpc = AArch64::CSELXr;
1188 } else {
1189 return false;
1190 }
1191
1192 MachineInstr &TstMI =
1193 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1194 .addDef(AArch64::WZR)
1195 .addUse(CondReg)
1196 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1197
1198 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1199 .addDef(I.getOperand(0).getReg())
1200 .addUse(TReg)
1201 .addUse(FReg)
1202 .addImm(AArch64CC::NE);
1203
1204 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1205 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1206
1207 I.eraseFromParent();
1208 return true;
1209 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001210 case TargetOpcode::G_ICMP: {
1211 if (Ty != LLT::scalar(1)) {
1212 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1213 << ", expected: " << LLT::scalar(1) << '\n');
1214 return false;
1215 }
1216
1217 unsigned CmpOpc = 0;
1218 unsigned ZReg = 0;
1219
1220 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1221 if (CmpTy == LLT::scalar(32)) {
1222 CmpOpc = AArch64::SUBSWrr;
1223 ZReg = AArch64::WZR;
1224 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1225 CmpOpc = AArch64::SUBSXrr;
1226 ZReg = AArch64::XZR;
1227 } else {
1228 return false;
1229 }
1230
Kristof Beyls22524402017-01-05 10:16:08 +00001231 // CSINC increments the result by one when the condition code is false.
1232 // Therefore, we have to invert the predicate to get an increment by 1 when
1233 // the predicate is true.
1234 const AArch64CC::CondCode invCC =
1235 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1236 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001237
1238 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1239 .addDef(ZReg)
1240 .addUse(I.getOperand(2).getReg())
1241 .addUse(I.getOperand(3).getReg());
1242
1243 MachineInstr &CSetMI =
1244 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1245 .addDef(I.getOperand(0).getReg())
1246 .addUse(AArch64::WZR)
1247 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001248 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001249
1250 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1251 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1252
1253 I.eraseFromParent();
1254 return true;
1255 }
1256
Tim Northover7dd378d2016-10-12 22:49:07 +00001257 case TargetOpcode::G_FCMP: {
1258 if (Ty != LLT::scalar(1)) {
1259 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1260 << ", expected: " << LLT::scalar(1) << '\n');
1261 return false;
1262 }
1263
1264 unsigned CmpOpc = 0;
1265 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1266 if (CmpTy == LLT::scalar(32)) {
1267 CmpOpc = AArch64::FCMPSrr;
1268 } else if (CmpTy == LLT::scalar(64)) {
1269 CmpOpc = AArch64::FCMPDrr;
1270 } else {
1271 return false;
1272 }
1273
1274 // FIXME: regbank
1275
1276 AArch64CC::CondCode CC1, CC2;
1277 changeFCMPPredToAArch64CC(
1278 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1279
1280 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1281 .addUse(I.getOperand(2).getReg())
1282 .addUse(I.getOperand(3).getReg());
1283
1284 const unsigned DefReg = I.getOperand(0).getReg();
1285 unsigned Def1Reg = DefReg;
1286 if (CC2 != AArch64CC::AL)
1287 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1288
1289 MachineInstr &CSetMI =
1290 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1291 .addDef(Def1Reg)
1292 .addUse(AArch64::WZR)
1293 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001294 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001295
1296 if (CC2 != AArch64CC::AL) {
1297 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1298 MachineInstr &CSet2MI =
1299 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1300 .addDef(Def2Reg)
1301 .addUse(AArch64::WZR)
1302 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001303 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001304 MachineInstr &OrMI =
1305 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1306 .addDef(DefReg)
1307 .addUse(Def1Reg)
1308 .addUse(Def2Reg);
1309 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1310 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1311 }
1312
1313 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1314 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1315
1316 I.eraseFromParent();
1317 return true;
1318 }
Tim Northovere9600d82017-02-08 17:57:27 +00001319 case TargetOpcode::G_VASTART:
1320 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1321 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001322 }
1323
1324 return false;
1325}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001326
1327/// SelectArithImmed - Select an immediate value that can be represented as
1328/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1329/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001330InstructionSelector::ComplexRendererFn
1331AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001332 MachineInstr &MI = *Root.getParent();
1333 MachineBasicBlock &MBB = *MI.getParent();
1334 MachineFunction &MF = *MBB.getParent();
1335 MachineRegisterInfo &MRI = MF.getRegInfo();
1336
1337 // This function is called from the addsub_shifted_imm ComplexPattern,
1338 // which lists [imm] as the list of opcode it's interested in, however
1339 // we still need to check whether the operand is actually an immediate
1340 // here because the ComplexPattern opcode list is only used in
1341 // root-level opcode matching.
1342 uint64_t Immed;
1343 if (Root.isImm())
1344 Immed = Root.getImm();
1345 else if (Root.isCImm())
1346 Immed = Root.getCImm()->getZExtValue();
1347 else if (Root.isReg()) {
1348 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1349 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sanders2deea182017-04-22 15:11:04 +00001350 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001351 MachineOperand &Op1 = Def->getOperand(1);
1352 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sanders2deea182017-04-22 15:11:04 +00001353 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001354 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001355 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001356 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001357
1358 unsigned ShiftAmt;
1359
1360 if (Immed >> 12 == 0) {
1361 ShiftAmt = 0;
1362 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1363 ShiftAmt = 12;
1364 Immed = Immed >> 12;
1365 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001366 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001367
1368 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sanders2deea182017-04-22 15:11:04 +00001369 return [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed).addImm(ShVal); };
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001370}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001371
1372namespace llvm {
1373InstructionSelector *
1374createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1375 AArch64Subtarget &Subtarget,
1376 AArch64RegisterBankInfo &RBI) {
1377 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1378}
1379}