blob: 69c7b42dedc7c10980d8c5ba05197b1c5f33c9da [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 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000217 case AArch64::FPRRegBankID:
218 switch (OpSize) {
219 case 32:
220 switch (GenericOpc) {
221 case TargetOpcode::G_FADD:
222 return AArch64::FADDSrr;
223 case TargetOpcode::G_FSUB:
224 return AArch64::FSUBSrr;
225 case TargetOpcode::G_FMUL:
226 return AArch64::FMULSrr;
227 case TargetOpcode::G_FDIV:
228 return AArch64::FDIVSrr;
229 default:
230 return GenericOpc;
231 }
232 case 64:
233 switch (GenericOpc) {
234 case TargetOpcode::G_FADD:
235 return AArch64::FADDDrr;
236 case TargetOpcode::G_FSUB:
237 return AArch64::FSUBDrr;
238 case TargetOpcode::G_FMUL:
239 return AArch64::FMULDrr;
240 case TargetOpcode::G_FDIV:
241 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000242 case TargetOpcode::G_OR:
243 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000244 default:
245 return GenericOpc;
246 }
247 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000248 };
249 return GenericOpc;
250}
251
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000252/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
253/// appropriate for the (value) register bank \p RegBankID and of memory access
254/// size \p OpSize. This returns the variant with the base+unsigned-immediate
255/// addressing mode (e.g., LDRXui).
256/// \returns \p GenericOpc if the combination is unsupported.
257static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
258 unsigned OpSize) {
259 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
260 switch (RegBankID) {
261 case AArch64::GPRRegBankID:
262 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000263 case 8:
264 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
265 case 16:
266 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000267 case 32:
268 return isStore ? AArch64::STRWui : AArch64::LDRWui;
269 case 64:
270 return isStore ? AArch64::STRXui : AArch64::LDRXui;
271 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000272 case AArch64::FPRRegBankID:
273 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000274 case 8:
275 return isStore ? AArch64::STRBui : AArch64::LDRBui;
276 case 16:
277 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000278 case 32:
279 return isStore ? AArch64::STRSui : AArch64::LDRSui;
280 case 64:
281 return isStore ? AArch64::STRDui : AArch64::LDRDui;
282 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000283 };
284 return GenericOpc;
285}
286
Quentin Colombetcb629a82016-10-12 03:57:49 +0000287static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
288 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
289 const RegisterBankInfo &RBI) {
290
291 unsigned DstReg = I.getOperand(0).getReg();
292 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
293 assert(I.isCopy() && "Generic operators do not allow physical registers");
294 return true;
295 }
296
297 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
298 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
299 unsigned SrcReg = I.getOperand(1).getReg();
300 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
301 (void)SrcSize;
302 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
303 "No phys reg on generic operators");
304 assert(
305 (DstSize == SrcSize ||
306 // Copies are a mean to setup initial types, the number of
307 // bits may not exactly match.
308 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
309 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
310 // Copies are a mean to copy bits around, as long as we are
311 // on the same register class, that's fine. Otherwise, that
312 // means we need some SUBREG_TO_REG or AND & co.
313 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
314 "Copy with different width?!");
315 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
316 "GPRs cannot get more than 64-bit width values");
317 const TargetRegisterClass *RC = nullptr;
318
319 if (RegBank.getID() == AArch64::FPRRegBankID) {
320 if (DstSize <= 32)
321 RC = &AArch64::FPR32RegClass;
322 else if (DstSize <= 64)
323 RC = &AArch64::FPR64RegClass;
324 else if (DstSize <= 128)
325 RC = &AArch64::FPR128RegClass;
326 else {
327 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
328 return false;
329 }
330 } else {
331 assert(RegBank.getID() == AArch64::GPRRegBankID &&
332 "Bitcast for the flags?");
333 RC =
334 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
335 }
336
337 // No need to constrain SrcReg. It will get constrained when
338 // we hit another of its use or its defs.
339 // Copies do not have constraints.
340 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
341 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
342 << " operand\n");
343 return false;
344 }
345 I.setDesc(TII.get(AArch64::COPY));
346 return true;
347}
348
Tim Northover69271c62016-10-12 22:49:11 +0000349static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
350 if (!DstTy.isScalar() || !SrcTy.isScalar())
351 return GenericOpc;
352
353 const unsigned DstSize = DstTy.getSizeInBits();
354 const unsigned SrcSize = SrcTy.getSizeInBits();
355
356 switch (DstSize) {
357 case 32:
358 switch (SrcSize) {
359 case 32:
360 switch (GenericOpc) {
361 case TargetOpcode::G_SITOFP:
362 return AArch64::SCVTFUWSri;
363 case TargetOpcode::G_UITOFP:
364 return AArch64::UCVTFUWSri;
365 case TargetOpcode::G_FPTOSI:
366 return AArch64::FCVTZSUWSr;
367 case TargetOpcode::G_FPTOUI:
368 return AArch64::FCVTZUUWSr;
369 default:
370 return GenericOpc;
371 }
372 case 64:
373 switch (GenericOpc) {
374 case TargetOpcode::G_SITOFP:
375 return AArch64::SCVTFUXSri;
376 case TargetOpcode::G_UITOFP:
377 return AArch64::UCVTFUXSri;
378 case TargetOpcode::G_FPTOSI:
379 return AArch64::FCVTZSUWDr;
380 case TargetOpcode::G_FPTOUI:
381 return AArch64::FCVTZUUWDr;
382 default:
383 return GenericOpc;
384 }
385 default:
386 return GenericOpc;
387 }
388 case 64:
389 switch (SrcSize) {
390 case 32:
391 switch (GenericOpc) {
392 case TargetOpcode::G_SITOFP:
393 return AArch64::SCVTFUWDri;
394 case TargetOpcode::G_UITOFP:
395 return AArch64::UCVTFUWDri;
396 case TargetOpcode::G_FPTOSI:
397 return AArch64::FCVTZSUXSr;
398 case TargetOpcode::G_FPTOUI:
399 return AArch64::FCVTZUUXSr;
400 default:
401 return GenericOpc;
402 }
403 case 64:
404 switch (GenericOpc) {
405 case TargetOpcode::G_SITOFP:
406 return AArch64::SCVTFUXDri;
407 case TargetOpcode::G_UITOFP:
408 return AArch64::UCVTFUXDri;
409 case TargetOpcode::G_FPTOSI:
410 return AArch64::FCVTZSUXDr;
411 case TargetOpcode::G_FPTOUI:
412 return AArch64::FCVTZUUXDr;
413 default:
414 return GenericOpc;
415 }
416 default:
417 return GenericOpc;
418 }
419 default:
420 return GenericOpc;
421 };
422 return GenericOpc;
423}
424
Tim Northover6c02ad52016-10-12 22:49:04 +0000425static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
426 switch (P) {
427 default:
428 llvm_unreachable("Unknown condition code!");
429 case CmpInst::ICMP_NE:
430 return AArch64CC::NE;
431 case CmpInst::ICMP_EQ:
432 return AArch64CC::EQ;
433 case CmpInst::ICMP_SGT:
434 return AArch64CC::GT;
435 case CmpInst::ICMP_SGE:
436 return AArch64CC::GE;
437 case CmpInst::ICMP_SLT:
438 return AArch64CC::LT;
439 case CmpInst::ICMP_SLE:
440 return AArch64CC::LE;
441 case CmpInst::ICMP_UGT:
442 return AArch64CC::HI;
443 case CmpInst::ICMP_UGE:
444 return AArch64CC::HS;
445 case CmpInst::ICMP_ULT:
446 return AArch64CC::LO;
447 case CmpInst::ICMP_ULE:
448 return AArch64CC::LS;
449 }
450}
451
Tim Northover7dd378d2016-10-12 22:49:07 +0000452static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
453 AArch64CC::CondCode &CondCode,
454 AArch64CC::CondCode &CondCode2) {
455 CondCode2 = AArch64CC::AL;
456 switch (P) {
457 default:
458 llvm_unreachable("Unknown FP condition!");
459 case CmpInst::FCMP_OEQ:
460 CondCode = AArch64CC::EQ;
461 break;
462 case CmpInst::FCMP_OGT:
463 CondCode = AArch64CC::GT;
464 break;
465 case CmpInst::FCMP_OGE:
466 CondCode = AArch64CC::GE;
467 break;
468 case CmpInst::FCMP_OLT:
469 CondCode = AArch64CC::MI;
470 break;
471 case CmpInst::FCMP_OLE:
472 CondCode = AArch64CC::LS;
473 break;
474 case CmpInst::FCMP_ONE:
475 CondCode = AArch64CC::MI;
476 CondCode2 = AArch64CC::GT;
477 break;
478 case CmpInst::FCMP_ORD:
479 CondCode = AArch64CC::VC;
480 break;
481 case CmpInst::FCMP_UNO:
482 CondCode = AArch64CC::VS;
483 break;
484 case CmpInst::FCMP_UEQ:
485 CondCode = AArch64CC::EQ;
486 CondCode2 = AArch64CC::VS;
487 break;
488 case CmpInst::FCMP_UGT:
489 CondCode = AArch64CC::HI;
490 break;
491 case CmpInst::FCMP_UGE:
492 CondCode = AArch64CC::PL;
493 break;
494 case CmpInst::FCMP_ULT:
495 CondCode = AArch64CC::LT;
496 break;
497 case CmpInst::FCMP_ULE:
498 CondCode = AArch64CC::LE;
499 break;
500 case CmpInst::FCMP_UNE:
501 CondCode = AArch64CC::NE;
502 break;
503 }
504}
505
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000506bool AArch64InstructionSelector::selectCompareBranch(
507 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
508
509 const unsigned CondReg = I.getOperand(0).getReg();
510 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
511 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
512 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
513 return false;
514
515 unsigned LHS = CCMI->getOperand(2).getReg();
516 unsigned RHS = CCMI->getOperand(3).getReg();
517 if (!getConstantVRegVal(RHS, MRI))
518 std::swap(RHS, LHS);
519
520 const auto RHSImm = getConstantVRegVal(RHS, MRI);
521 if (!RHSImm || *RHSImm != 0)
522 return false;
523
524 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
525 if (RB.getID() != AArch64::GPRRegBankID)
526 return false;
527
528 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
529 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
530 return false;
531
532 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
533 unsigned CBOpc = 0;
534 if (CmpWidth <= 32)
535 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
536 else if (CmpWidth == 64)
537 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
538 else
539 return false;
540
541 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
542 .addUse(LHS)
543 .addMBB(DestMBB);
544
545 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
546 I.eraseFromParent();
547 return true;
548}
549
Tim Northovere9600d82017-02-08 17:57:27 +0000550bool AArch64InstructionSelector::selectVaStartAAPCS(
551 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
552 return false;
553}
554
555bool AArch64InstructionSelector::selectVaStartDarwin(
556 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
557 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
558 unsigned ListReg = I.getOperand(0).getReg();
559
560 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
561
562 auto MIB =
563 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
564 .addDef(ArgsAddrReg)
565 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
566 .addImm(0)
567 .addImm(0);
568
569 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
570
571 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
572 .addUse(ArgsAddrReg)
573 .addUse(ListReg)
574 .addImm(0)
575 .addMemOperand(*I.memoperands_begin());
576
577 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
578 I.eraseFromParent();
579 return true;
580}
581
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000582bool AArch64InstructionSelector::select(MachineInstr &I) const {
583 assert(I.getParent() && "Instruction should be in a basic block!");
584 assert(I.getParent()->getParent() && "Instruction should be in a function!");
585
586 MachineBasicBlock &MBB = *I.getParent();
587 MachineFunction &MF = *MBB.getParent();
588 MachineRegisterInfo &MRI = MF.getRegInfo();
589
Tim Northovercdf23f12016-10-31 18:30:59 +0000590 unsigned Opcode = I.getOpcode();
591 if (!isPreISelGenericOpcode(I.getOpcode())) {
592 // Certain non-generic instructions also need some special handling.
593
594 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
595 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000596
597 if (Opcode == TargetOpcode::PHI) {
598 const unsigned DefReg = I.getOperand(0).getReg();
599 const LLT DefTy = MRI.getType(DefReg);
600
601 const TargetRegisterClass *DefRC = nullptr;
602 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
603 DefRC = TRI.getRegClass(DefReg);
604 } else {
605 const RegClassOrRegBank &RegClassOrBank =
606 MRI.getRegClassOrRegBank(DefReg);
607
608 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
609 if (!DefRC) {
610 if (!DefTy.isValid()) {
611 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
612 return false;
613 }
614 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
615 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
616 if (!DefRC) {
617 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
618 return false;
619 }
620 }
621 }
622
623 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
624 }
625
626 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000627 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000628
629 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000630 }
631
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000632
633 if (I.getNumOperands() != I.getNumExplicitOperands()) {
634 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
635 return false;
636 }
637
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000638 if (selectImpl(I))
639 return true;
640
Tim Northover32a078a2016-09-15 10:09:59 +0000641 LLT Ty =
642 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000643
Tim Northover69271c62016-10-12 22:49:11 +0000644 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000645 case TargetOpcode::G_BRCOND: {
646 if (Ty.getSizeInBits() > 32) {
647 // We shouldn't need this on AArch64, but it would be implemented as an
648 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
649 // bit being tested is < 32.
650 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
651 << ", expected at most 32-bits");
652 return false;
653 }
654
655 const unsigned CondReg = I.getOperand(0).getReg();
656 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
657
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000658 if (selectCompareBranch(I, MF, MRI))
659 return true;
660
Tim Northover5e3dbf32016-10-12 22:49:01 +0000661 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
662 .addUse(CondReg)
663 .addImm(/*bit offset=*/0)
664 .addMBB(DestMBB);
665
666 I.eraseFromParent();
667 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
668 }
669
Kristof Beyls65a12c02017-01-30 09:13:18 +0000670 case TargetOpcode::G_BRINDIRECT: {
671 I.setDesc(TII.get(AArch64::BR));
672 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
673 }
674
Tim Northover4494d692016-10-18 19:47:57 +0000675 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000676 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000677 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
678
679 const LLT s32 = LLT::scalar(32);
680 const LLT s64 = LLT::scalar(64);
681 const LLT p0 = LLT::pointer(0, 64);
682
683 const unsigned DefReg = I.getOperand(0).getReg();
684 const LLT DefTy = MRI.getType(DefReg);
685 const unsigned DefSize = DefTy.getSizeInBits();
686 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
687
688 // FIXME: Redundant check, but even less readable when factored out.
689 if (isFP) {
690 if (Ty != s32 && Ty != s64) {
691 DEBUG(dbgs() << "Unable to materialize FP " << Ty
692 << " constant, expected: " << s32 << " or " << s64
693 << '\n');
694 return false;
695 }
696
697 if (RB.getID() != AArch64::FPRRegBankID) {
698 DEBUG(dbgs() << "Unable to materialize FP " << Ty
699 << " constant on bank: " << RB << ", expected: FPR\n");
700 return false;
701 }
702 } else {
703 if (Ty != s32 && Ty != s64 && Ty != p0) {
704 DEBUG(dbgs() << "Unable to materialize integer " << Ty
705 << " constant, expected: " << s32 << ", " << s64 << ", or "
706 << p0 << '\n');
707 return false;
708 }
709
710 if (RB.getID() != AArch64::GPRRegBankID) {
711 DEBUG(dbgs() << "Unable to materialize integer " << Ty
712 << " constant on bank: " << RB << ", expected: GPR\n");
713 return false;
714 }
715 }
716
717 const unsigned MovOpc =
718 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
719
720 I.setDesc(TII.get(MovOpc));
721
722 if (isFP) {
723 const TargetRegisterClass &GPRRC =
724 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
725 const TargetRegisterClass &FPRRC =
726 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
727
728 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
729 MachineOperand &RegOp = I.getOperand(0);
730 RegOp.setReg(DefGPRReg);
731
732 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
733 TII.get(AArch64::COPY))
734 .addDef(DefReg)
735 .addUse(DefGPRReg);
736
737 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
738 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
739 return false;
740 }
741
742 MachineOperand &ImmOp = I.getOperand(1);
743 // FIXME: Is going through int64_t always correct?
744 ImmOp.ChangeToImmediate(
745 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000746 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000747 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
748 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000749 } else if (I.getOperand(1).isImm()) {
750 uint64_t Val = I.getOperand(1).getImm();
751 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000752 }
753
754 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
755 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000756 }
757
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000758 case TargetOpcode::G_FRAME_INDEX: {
759 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000760 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000761 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000762 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000763 return false;
764 }
765
766 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000767
768 // MOs for a #0 shifted immediate.
769 I.addOperand(MachineOperand::CreateImm(0));
770 I.addOperand(MachineOperand::CreateImm(0));
771
772 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
773 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000774
775 case TargetOpcode::G_GLOBAL_VALUE: {
776 auto GV = I.getOperand(1).getGlobal();
777 if (GV->isThreadLocal()) {
778 // FIXME: we don't support TLS yet.
779 return false;
780 }
781 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000782 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000783 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000784 I.getOperand(1).setTargetFlags(OpFlags);
785 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000786 I.setDesc(TII.get(AArch64::MOVaddr));
787 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
788 MachineInstrBuilder MIB(MF, I);
789 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
790 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
791 }
792 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
793 }
794
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000795 case TargetOpcode::G_LOAD:
796 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000797 LLT MemTy = Ty;
798 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000799
Tim Northover5ae83502016-09-15 09:20:34 +0000800 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000801 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000802 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000803 return false;
804 }
805
Tim Northover48dfa1a2017-02-13 22:14:16 +0000806 auto &MemOp = **I.memoperands_begin();
807 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
808 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
809 return false;
810 }
811
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000812 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000813#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000814 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000815 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000816 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
817 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000818 assert(MRI.getType(PtrReg).isPointer() &&
819 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000820#endif
821
822 const unsigned ValReg = I.getOperand(0).getReg();
823 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
824
825 const unsigned NewOpc =
826 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
827 if (NewOpc == I.getOpcode())
828 return false;
829
830 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000831
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000832 uint64_t Offset = 0;
833 auto *PtrMI = MRI.getVRegDef(PtrReg);
834
835 // Try to fold a GEP into our unsigned immediate addressing mode.
836 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
837 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
838 int64_t Imm = *COff;
839 const unsigned Size = MemTy.getSizeInBits() / 8;
840 const unsigned Scale = Log2_32(Size);
841 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
842 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
843 I.getOperand(1).setReg(Ptr2Reg);
844 PtrMI = MRI.getVRegDef(Ptr2Reg);
845 Offset = Imm / Size;
846 }
847 }
848 }
849
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000850 // If we haven't folded anything into our addressing mode yet, try to fold
851 // a frame index into the base+offset.
852 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
853 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
854
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000855 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000856
857 // If we're storing a 0, use WZR/XZR.
858 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
859 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
860 if (I.getOpcode() == AArch64::STRWui)
861 I.getOperand(0).setReg(AArch64::WZR);
862 else if (I.getOpcode() == AArch64::STRXui)
863 I.getOperand(0).setReg(AArch64::XZR);
864 }
865 }
866
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000867 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
868 }
869
Tim Northover9dd78f82017-02-08 21:22:25 +0000870 case TargetOpcode::G_SMULH:
871 case TargetOpcode::G_UMULH: {
872 // Reject the various things we don't support yet.
873 if (unsupportedBinOp(I, RBI, MRI, TRI))
874 return false;
875
876 const unsigned DefReg = I.getOperand(0).getReg();
877 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
878
879 if (RB.getID() != AArch64::GPRRegBankID) {
880 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
881 return false;
882 }
883
884 if (Ty != LLT::scalar(64)) {
885 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
886 << ", expected: " << LLT::scalar(64) << '\n');
887 return false;
888 }
889
890 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
891 : AArch64::UMULHrr;
892 I.setDesc(TII.get(NewOpc));
893
894 // Now that we selected an opcode, we need to constrain the register
895 // operands to use appropriate classes.
896 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
897 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000898 case TargetOpcode::G_FADD:
899 case TargetOpcode::G_FSUB:
900 case TargetOpcode::G_FMUL:
901 case TargetOpcode::G_FDIV:
902
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000903 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000904 case TargetOpcode::G_SHL:
905 case TargetOpcode::G_LSHR:
906 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000907 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000908 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000909 if (unsupportedBinOp(I, RBI, MRI, TRI))
910 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000911
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000912 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000913
914 const unsigned DefReg = I.getOperand(0).getReg();
915 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
916
917 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
918 if (NewOpc == I.getOpcode())
919 return false;
920
921 I.setDesc(TII.get(NewOpc));
922 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000923
924 // Now that we selected an opcode, we need to constrain the register
925 // operands to use appropriate classes.
926 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
927 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000928
Tim Northover398c5f52017-02-14 20:56:29 +0000929 case TargetOpcode::G_PTR_MASK: {
930 uint64_t Align = I.getOperand(2).getImm();
931 if (Align >= 64 || Align == 0)
932 return false;
933
934 uint64_t Mask = ~((1ULL << Align) - 1);
935 I.setDesc(TII.get(AArch64::ANDXri));
936 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
937
938 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
939 }
Tim Northover037af52c2016-10-31 18:31:09 +0000940 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000941 case TargetOpcode::G_TRUNC: {
942 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
943 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
944
945 const unsigned DstReg = I.getOperand(0).getReg();
946 const unsigned SrcReg = I.getOperand(1).getReg();
947
948 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
949 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
950
951 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000952 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000953 return false;
954 }
955
956 if (DstRB.getID() == AArch64::GPRRegBankID) {
957 const TargetRegisterClass *DstRC =
958 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
959 if (!DstRC)
960 return false;
961
962 const TargetRegisterClass *SrcRC =
963 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
964 if (!SrcRC)
965 return false;
966
967 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
968 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000969 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000970 return false;
971 }
972
973 if (DstRC == SrcRC) {
974 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000975 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
976 SrcTy == LLT::scalar(64)) {
977 llvm_unreachable("TableGen can import this case");
978 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000979 } else if (DstRC == &AArch64::GPR32RegClass &&
980 SrcRC == &AArch64::GPR64RegClass) {
981 I.getOperand(1).setSubReg(AArch64::sub_32);
982 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000983 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +0000984 return false;
985 }
986
987 I.setDesc(TII.get(TargetOpcode::COPY));
988 return true;
989 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
990 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
991 I.setDesc(TII.get(AArch64::XTNv4i16));
992 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
993 return true;
994 }
995 }
996
997 return false;
998 }
999
Tim Northover3d38b3a2016-10-11 20:50:21 +00001000 case TargetOpcode::G_ANYEXT: {
1001 const unsigned DstReg = I.getOperand(0).getReg();
1002 const unsigned SrcReg = I.getOperand(1).getReg();
1003
Quentin Colombetcb629a82016-10-12 03:57:49 +00001004 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1005 if (RBDst.getID() != AArch64::GPRRegBankID) {
1006 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1007 return false;
1008 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001009
Quentin Colombetcb629a82016-10-12 03:57:49 +00001010 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1011 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1012 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001013 return false;
1014 }
1015
1016 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1017
1018 if (DstSize == 0) {
1019 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1020 return false;
1021 }
1022
Quentin Colombetcb629a82016-10-12 03:57:49 +00001023 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001024 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1025 << ", expected: 32 or 64\n");
1026 return false;
1027 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001028 // At this point G_ANYEXT is just like a plain COPY, but we need
1029 // to explicitly form the 64-bit value if any.
1030 if (DstSize > 32) {
1031 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1032 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1033 .addDef(ExtSrc)
1034 .addImm(0)
1035 .addUse(SrcReg)
1036 .addImm(AArch64::sub_32);
1037 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001038 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001039 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001040 }
1041
1042 case TargetOpcode::G_ZEXT:
1043 case TargetOpcode::G_SEXT: {
1044 unsigned Opcode = I.getOpcode();
1045 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1046 SrcTy = MRI.getType(I.getOperand(1).getReg());
1047 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1048 const unsigned DefReg = I.getOperand(0).getReg();
1049 const unsigned SrcReg = I.getOperand(1).getReg();
1050 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1051
1052 if (RB.getID() != AArch64::GPRRegBankID) {
1053 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1054 << ", expected: GPR\n");
1055 return false;
1056 }
1057
1058 MachineInstr *ExtI;
1059 if (DstTy == LLT::scalar(64)) {
1060 // FIXME: Can we avoid manually doing this?
1061 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1062 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1063 << " operand\n");
1064 return false;
1065 }
1066
1067 const unsigned SrcXReg =
1068 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1069 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1070 .addDef(SrcXReg)
1071 .addImm(0)
1072 .addUse(SrcReg)
1073 .addImm(AArch64::sub_32);
1074
1075 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1076 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1077 .addDef(DefReg)
1078 .addUse(SrcXReg)
1079 .addImm(0)
1080 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001081 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001082 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1083 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1084 .addDef(DefReg)
1085 .addUse(SrcReg)
1086 .addImm(0)
1087 .addImm(SrcTy.getSizeInBits() - 1);
1088 } else {
1089 return false;
1090 }
1091
1092 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1093
1094 I.eraseFromParent();
1095 return true;
1096 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001097
Tim Northover69271c62016-10-12 22:49:11 +00001098 case TargetOpcode::G_SITOFP:
1099 case TargetOpcode::G_UITOFP:
1100 case TargetOpcode::G_FPTOSI:
1101 case TargetOpcode::G_FPTOUI: {
1102 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1103 SrcTy = MRI.getType(I.getOperand(1).getReg());
1104 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1105 if (NewOpc == Opcode)
1106 return false;
1107
1108 I.setDesc(TII.get(NewOpc));
1109 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1110
1111 return true;
1112 }
1113
1114
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001115 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001116 case TargetOpcode::G_BITCAST:
1117 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001118
Tim Northover5f7dea82016-11-08 17:44:07 +00001119 case TargetOpcode::G_FPEXT: {
1120 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1121 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1122 << ", expected: " << LLT::scalar(64) << '\n');
1123 return false;
1124 }
1125
1126 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1127 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1128 << ", expected: " << LLT::scalar(32) << '\n');
1129 return false;
1130 }
1131
1132 const unsigned DefReg = I.getOperand(0).getReg();
1133 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1134
1135 if (RB.getID() != AArch64::FPRRegBankID) {
1136 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1137 return false;
1138 }
1139
1140 I.setDesc(TII.get(AArch64::FCVTDSr));
1141 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1142
1143 return true;
1144 }
1145
1146 case TargetOpcode::G_FPTRUNC: {
1147 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1148 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1149 << ", expected: " << LLT::scalar(32) << '\n');
1150 return false;
1151 }
1152
1153 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1154 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1155 << ", expected: " << LLT::scalar(64) << '\n');
1156 return false;
1157 }
1158
1159 const unsigned DefReg = I.getOperand(0).getReg();
1160 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1161
1162 if (RB.getID() != AArch64::FPRRegBankID) {
1163 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1164 return false;
1165 }
1166
1167 I.setDesc(TII.get(AArch64::FCVTSDr));
1168 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1169
1170 return true;
1171 }
1172
Tim Northover9ac0eba2016-11-08 00:45:29 +00001173 case TargetOpcode::G_SELECT: {
1174 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1175 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1176 << ", expected: " << LLT::scalar(1) << '\n');
1177 return false;
1178 }
1179
1180 const unsigned CondReg = I.getOperand(1).getReg();
1181 const unsigned TReg = I.getOperand(2).getReg();
1182 const unsigned FReg = I.getOperand(3).getReg();
1183
1184 unsigned CSelOpc = 0;
1185
1186 if (Ty == LLT::scalar(32)) {
1187 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001188 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001189 CSelOpc = AArch64::CSELXr;
1190 } else {
1191 return false;
1192 }
1193
1194 MachineInstr &TstMI =
1195 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1196 .addDef(AArch64::WZR)
1197 .addUse(CondReg)
1198 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1199
1200 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1201 .addDef(I.getOperand(0).getReg())
1202 .addUse(TReg)
1203 .addUse(FReg)
1204 .addImm(AArch64CC::NE);
1205
1206 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1207 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1208
1209 I.eraseFromParent();
1210 return true;
1211 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001212 case TargetOpcode::G_ICMP: {
1213 if (Ty != LLT::scalar(1)) {
1214 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1215 << ", expected: " << LLT::scalar(1) << '\n');
1216 return false;
1217 }
1218
1219 unsigned CmpOpc = 0;
1220 unsigned ZReg = 0;
1221
1222 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1223 if (CmpTy == LLT::scalar(32)) {
1224 CmpOpc = AArch64::SUBSWrr;
1225 ZReg = AArch64::WZR;
1226 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1227 CmpOpc = AArch64::SUBSXrr;
1228 ZReg = AArch64::XZR;
1229 } else {
1230 return false;
1231 }
1232
Kristof Beyls22524402017-01-05 10:16:08 +00001233 // CSINC increments the result by one when the condition code is false.
1234 // Therefore, we have to invert the predicate to get an increment by 1 when
1235 // the predicate is true.
1236 const AArch64CC::CondCode invCC =
1237 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1238 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001239
1240 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1241 .addDef(ZReg)
1242 .addUse(I.getOperand(2).getReg())
1243 .addUse(I.getOperand(3).getReg());
1244
1245 MachineInstr &CSetMI =
1246 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1247 .addDef(I.getOperand(0).getReg())
1248 .addUse(AArch64::WZR)
1249 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001250 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001251
1252 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1253 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1254
1255 I.eraseFromParent();
1256 return true;
1257 }
1258
Tim Northover7dd378d2016-10-12 22:49:07 +00001259 case TargetOpcode::G_FCMP: {
1260 if (Ty != LLT::scalar(1)) {
1261 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1262 << ", expected: " << LLT::scalar(1) << '\n');
1263 return false;
1264 }
1265
1266 unsigned CmpOpc = 0;
1267 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1268 if (CmpTy == LLT::scalar(32)) {
1269 CmpOpc = AArch64::FCMPSrr;
1270 } else if (CmpTy == LLT::scalar(64)) {
1271 CmpOpc = AArch64::FCMPDrr;
1272 } else {
1273 return false;
1274 }
1275
1276 // FIXME: regbank
1277
1278 AArch64CC::CondCode CC1, CC2;
1279 changeFCMPPredToAArch64CC(
1280 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1281
1282 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1283 .addUse(I.getOperand(2).getReg())
1284 .addUse(I.getOperand(3).getReg());
1285
1286 const unsigned DefReg = I.getOperand(0).getReg();
1287 unsigned Def1Reg = DefReg;
1288 if (CC2 != AArch64CC::AL)
1289 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1290
1291 MachineInstr &CSetMI =
1292 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1293 .addDef(Def1Reg)
1294 .addUse(AArch64::WZR)
1295 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001296 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001297
1298 if (CC2 != AArch64CC::AL) {
1299 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1300 MachineInstr &CSet2MI =
1301 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1302 .addDef(Def2Reg)
1303 .addUse(AArch64::WZR)
1304 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001305 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001306 MachineInstr &OrMI =
1307 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1308 .addDef(DefReg)
1309 .addUse(Def1Reg)
1310 .addUse(Def2Reg);
1311 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1312 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1313 }
1314
1315 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1316 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1317
1318 I.eraseFromParent();
1319 return true;
1320 }
Tim Northovere9600d82017-02-08 17:57:27 +00001321 case TargetOpcode::G_VASTART:
1322 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1323 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001324 }
1325
1326 return false;
1327}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001328
1329/// SelectArithImmed - Select an immediate value that can be represented as
1330/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1331/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001332InstructionSelector::ComplexRendererFn
1333AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001334 MachineInstr &MI = *Root.getParent();
1335 MachineBasicBlock &MBB = *MI.getParent();
1336 MachineFunction &MF = *MBB.getParent();
1337 MachineRegisterInfo &MRI = MF.getRegInfo();
1338
1339 // This function is called from the addsub_shifted_imm ComplexPattern,
1340 // which lists [imm] as the list of opcode it's interested in, however
1341 // we still need to check whether the operand is actually an immediate
1342 // here because the ComplexPattern opcode list is only used in
1343 // root-level opcode matching.
1344 uint64_t Immed;
1345 if (Root.isImm())
1346 Immed = Root.getImm();
1347 else if (Root.isCImm())
1348 Immed = Root.getCImm()->getZExtValue();
1349 else if (Root.isReg()) {
1350 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1351 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sanders2deea182017-04-22 15:11:04 +00001352 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001353 MachineOperand &Op1 = Def->getOperand(1);
1354 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sanders2deea182017-04-22 15:11:04 +00001355 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001356 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001357 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001358 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001359
1360 unsigned ShiftAmt;
1361
1362 if (Immed >> 12 == 0) {
1363 ShiftAmt = 0;
1364 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1365 ShiftAmt = 12;
1366 Immed = Immed >> 12;
1367 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001368 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001369
1370 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sanders2deea182017-04-22 15:11:04 +00001371 return [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed).addImm(ShVal); };
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001372}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001373
1374namespace llvm {
1375InstructionSelector *
1376createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1377 AArch64Subtarget &Subtarget,
1378 AArch64RegisterBankInfo &RBI) {
1379 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1380}
1381}