blob: d20438237626ab5511e6756fc07f70a6e147ab01 [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
Daniel Sanderse7b0d662017-04-21 15:59:56 +000054 void beginFunction(const MachineFunction &MF) override;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000055 bool select(MachineInstr &I) const override;
56
57private:
58 /// tblgen-erated 'select' implementation, used as the initial selector for
59 /// the patterns that don't require complex C++.
60 bool selectImpl(MachineInstr &I) const;
61
62 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
63 MachineRegisterInfo &MRI) const;
64 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
65 MachineRegisterInfo &MRI) const;
66
67 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
68 MachineRegisterInfo &MRI) const;
69
70 bool selectArithImmed(MachineOperand &Root, MachineOperand &Result1,
71 MachineOperand &Result2) const;
72
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 bool ForCodeSize;
79
80 PredicateBitset AvailableFeatures;
81 PredicateBitset
82 computeAvailableFeatures(const MachineFunction *MF,
83 const AArch64Subtarget *Subtarget) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000084
85// We declare the temporaries used by selectImpl() in the class to minimize the
86// cost of constructing placeholder values.
87#define GET_GLOBALISEL_TEMPORARIES_DECL
88#include "AArch64GenGlobalISel.inc"
89#undef GET_GLOBALISEL_TEMPORARIES_DECL
90};
91
92} // end anonymous namespace
93
Daniel Sanders8a4bae92017-03-14 21:32:08 +000094#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000095#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000096#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000097
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000098AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000099 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
100 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000101 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000102 TRI(*STI.getRegisterInfo()), RBI(RBI), ForCodeSize(), AvailableFeatures()
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000103#define GET_GLOBALISEL_TEMPORARIES_INIT
104#include "AArch64GenGlobalISel.inc"
105#undef GET_GLOBALISEL_TEMPORARIES_INIT
106{
107}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000108
Tim Northoverfb8d9892016-10-12 22:49:15 +0000109// FIXME: This should be target-independent, inferred from the types declared
110// for each class in the bank.
111static const TargetRegisterClass *
112getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
113 const RegisterBankInfo &RBI) {
114 if (RB.getID() == AArch64::GPRRegBankID) {
115 if (Ty.getSizeInBits() <= 32)
116 return &AArch64::GPR32RegClass;
117 if (Ty.getSizeInBits() == 64)
118 return &AArch64::GPR64RegClass;
119 return nullptr;
120 }
121
122 if (RB.getID() == AArch64::FPRRegBankID) {
123 if (Ty.getSizeInBits() == 32)
124 return &AArch64::FPR32RegClass;
125 if (Ty.getSizeInBits() == 64)
126 return &AArch64::FPR64RegClass;
127 if (Ty.getSizeInBits() == 128)
128 return &AArch64::FPR128RegClass;
129 return nullptr;
130 }
131
132 return nullptr;
133}
134
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000135/// Check whether \p I is a currently unsupported binary operation:
136/// - it has an unsized type
137/// - an operand is not a vreg
138/// - all operands are not in the same bank
139/// These are checks that should someday live in the verifier, but right now,
140/// these are mostly limitations of the aarch64 selector.
141static bool unsupportedBinOp(const MachineInstr &I,
142 const AArch64RegisterBankInfo &RBI,
143 const MachineRegisterInfo &MRI,
144 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000145 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000146 if (!Ty.isValid()) {
147 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000148 return true;
149 }
150
151 const RegisterBank *PrevOpBank = nullptr;
152 for (auto &MO : I.operands()) {
153 // FIXME: Support non-register operands.
154 if (!MO.isReg()) {
155 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
156 return true;
157 }
158
159 // FIXME: Can generic operations have physical registers operands? If
160 // so, this will need to be taught about that, and we'll need to get the
161 // bank out of the minimal class for the register.
162 // Either way, this needs to be documented (and possibly verified).
163 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
164 DEBUG(dbgs() << "Generic inst has physical register operand\n");
165 return true;
166 }
167
168 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
169 if (!OpBank) {
170 DEBUG(dbgs() << "Generic register has no bank or class\n");
171 return true;
172 }
173
174 if (PrevOpBank && OpBank != PrevOpBank) {
175 DEBUG(dbgs() << "Generic inst operands have different banks\n");
176 return true;
177 }
178 PrevOpBank = OpBank;
179 }
180 return false;
181}
182
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000183/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000184/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000185/// and of size \p OpSize.
186/// \returns \p GenericOpc if the combination is unsupported.
187static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
188 unsigned OpSize) {
189 switch (RegBankID) {
190 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000191 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000192 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000193 case TargetOpcode::G_SHL:
194 return AArch64::LSLVWr;
195 case TargetOpcode::G_LSHR:
196 return AArch64::LSRVWr;
197 case TargetOpcode::G_ASHR:
198 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000199 default:
200 return GenericOpc;
201 }
Tim Northover55782222016-10-18 20:03:48 +0000202 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000203 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000204 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000205 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000206 case TargetOpcode::G_SHL:
207 return AArch64::LSLVXr;
208 case TargetOpcode::G_LSHR:
209 return AArch64::LSRVXr;
210 case TargetOpcode::G_ASHR:
211 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000212 default:
213 return GenericOpc;
214 }
215 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000216 case AArch64::FPRRegBankID:
217 switch (OpSize) {
218 case 32:
219 switch (GenericOpc) {
220 case TargetOpcode::G_FADD:
221 return AArch64::FADDSrr;
222 case TargetOpcode::G_FSUB:
223 return AArch64::FSUBSrr;
224 case TargetOpcode::G_FMUL:
225 return AArch64::FMULSrr;
226 case TargetOpcode::G_FDIV:
227 return AArch64::FDIVSrr;
228 default:
229 return GenericOpc;
230 }
231 case 64:
232 switch (GenericOpc) {
233 case TargetOpcode::G_FADD:
234 return AArch64::FADDDrr;
235 case TargetOpcode::G_FSUB:
236 return AArch64::FSUBDrr;
237 case TargetOpcode::G_FMUL:
238 return AArch64::FMULDrr;
239 case TargetOpcode::G_FDIV:
240 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000241 case TargetOpcode::G_OR:
242 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000243 default:
244 return GenericOpc;
245 }
246 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000247 };
248 return GenericOpc;
249}
250
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000251/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
252/// appropriate for the (value) register bank \p RegBankID and of memory access
253/// size \p OpSize. This returns the variant with the base+unsigned-immediate
254/// addressing mode (e.g., LDRXui).
255/// \returns \p GenericOpc if the combination is unsupported.
256static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
257 unsigned OpSize) {
258 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
259 switch (RegBankID) {
260 case AArch64::GPRRegBankID:
261 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000262 case 8:
263 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
264 case 16:
265 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000266 case 32:
267 return isStore ? AArch64::STRWui : AArch64::LDRWui;
268 case 64:
269 return isStore ? AArch64::STRXui : AArch64::LDRXui;
270 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000271 case AArch64::FPRRegBankID:
272 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000273 case 8:
274 return isStore ? AArch64::STRBui : AArch64::LDRBui;
275 case 16:
276 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000277 case 32:
278 return isStore ? AArch64::STRSui : AArch64::LDRSui;
279 case 64:
280 return isStore ? AArch64::STRDui : AArch64::LDRDui;
281 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000282 };
283 return GenericOpc;
284}
285
Quentin Colombetcb629a82016-10-12 03:57:49 +0000286static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
287 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
288 const RegisterBankInfo &RBI) {
289
290 unsigned DstReg = I.getOperand(0).getReg();
291 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
292 assert(I.isCopy() && "Generic operators do not allow physical registers");
293 return true;
294 }
295
296 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
297 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
298 unsigned SrcReg = I.getOperand(1).getReg();
299 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
300 (void)SrcSize;
301 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
302 "No phys reg on generic operators");
303 assert(
304 (DstSize == SrcSize ||
305 // Copies are a mean to setup initial types, the number of
306 // bits may not exactly match.
307 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
308 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
309 // Copies are a mean to copy bits around, as long as we are
310 // on the same register class, that's fine. Otherwise, that
311 // means we need some SUBREG_TO_REG or AND & co.
312 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
313 "Copy with different width?!");
314 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
315 "GPRs cannot get more than 64-bit width values");
316 const TargetRegisterClass *RC = nullptr;
317
318 if (RegBank.getID() == AArch64::FPRRegBankID) {
319 if (DstSize <= 32)
320 RC = &AArch64::FPR32RegClass;
321 else if (DstSize <= 64)
322 RC = &AArch64::FPR64RegClass;
323 else if (DstSize <= 128)
324 RC = &AArch64::FPR128RegClass;
325 else {
326 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
327 return false;
328 }
329 } else {
330 assert(RegBank.getID() == AArch64::GPRRegBankID &&
331 "Bitcast for the flags?");
332 RC =
333 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
334 }
335
336 // No need to constrain SrcReg. It will get constrained when
337 // we hit another of its use or its defs.
338 // Copies do not have constraints.
339 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
340 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
341 << " operand\n");
342 return false;
343 }
344 I.setDesc(TII.get(AArch64::COPY));
345 return true;
346}
347
Tim Northover69271c62016-10-12 22:49:11 +0000348static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
349 if (!DstTy.isScalar() || !SrcTy.isScalar())
350 return GenericOpc;
351
352 const unsigned DstSize = DstTy.getSizeInBits();
353 const unsigned SrcSize = SrcTy.getSizeInBits();
354
355 switch (DstSize) {
356 case 32:
357 switch (SrcSize) {
358 case 32:
359 switch (GenericOpc) {
360 case TargetOpcode::G_SITOFP:
361 return AArch64::SCVTFUWSri;
362 case TargetOpcode::G_UITOFP:
363 return AArch64::UCVTFUWSri;
364 case TargetOpcode::G_FPTOSI:
365 return AArch64::FCVTZSUWSr;
366 case TargetOpcode::G_FPTOUI:
367 return AArch64::FCVTZUUWSr;
368 default:
369 return GenericOpc;
370 }
371 case 64:
372 switch (GenericOpc) {
373 case TargetOpcode::G_SITOFP:
374 return AArch64::SCVTFUXSri;
375 case TargetOpcode::G_UITOFP:
376 return AArch64::UCVTFUXSri;
377 case TargetOpcode::G_FPTOSI:
378 return AArch64::FCVTZSUWDr;
379 case TargetOpcode::G_FPTOUI:
380 return AArch64::FCVTZUUWDr;
381 default:
382 return GenericOpc;
383 }
384 default:
385 return GenericOpc;
386 }
387 case 64:
388 switch (SrcSize) {
389 case 32:
390 switch (GenericOpc) {
391 case TargetOpcode::G_SITOFP:
392 return AArch64::SCVTFUWDri;
393 case TargetOpcode::G_UITOFP:
394 return AArch64::UCVTFUWDri;
395 case TargetOpcode::G_FPTOSI:
396 return AArch64::FCVTZSUXSr;
397 case TargetOpcode::G_FPTOUI:
398 return AArch64::FCVTZUUXSr;
399 default:
400 return GenericOpc;
401 }
402 case 64:
403 switch (GenericOpc) {
404 case TargetOpcode::G_SITOFP:
405 return AArch64::SCVTFUXDri;
406 case TargetOpcode::G_UITOFP:
407 return AArch64::UCVTFUXDri;
408 case TargetOpcode::G_FPTOSI:
409 return AArch64::FCVTZSUXDr;
410 case TargetOpcode::G_FPTOUI:
411 return AArch64::FCVTZUUXDr;
412 default:
413 return GenericOpc;
414 }
415 default:
416 return GenericOpc;
417 }
418 default:
419 return GenericOpc;
420 };
421 return GenericOpc;
422}
423
Tim Northover6c02ad52016-10-12 22:49:04 +0000424static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
425 switch (P) {
426 default:
427 llvm_unreachable("Unknown condition code!");
428 case CmpInst::ICMP_NE:
429 return AArch64CC::NE;
430 case CmpInst::ICMP_EQ:
431 return AArch64CC::EQ;
432 case CmpInst::ICMP_SGT:
433 return AArch64CC::GT;
434 case CmpInst::ICMP_SGE:
435 return AArch64CC::GE;
436 case CmpInst::ICMP_SLT:
437 return AArch64CC::LT;
438 case CmpInst::ICMP_SLE:
439 return AArch64CC::LE;
440 case CmpInst::ICMP_UGT:
441 return AArch64CC::HI;
442 case CmpInst::ICMP_UGE:
443 return AArch64CC::HS;
444 case CmpInst::ICMP_ULT:
445 return AArch64CC::LO;
446 case CmpInst::ICMP_ULE:
447 return AArch64CC::LS;
448 }
449}
450
Tim Northover7dd378d2016-10-12 22:49:07 +0000451static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
452 AArch64CC::CondCode &CondCode,
453 AArch64CC::CondCode &CondCode2) {
454 CondCode2 = AArch64CC::AL;
455 switch (P) {
456 default:
457 llvm_unreachable("Unknown FP condition!");
458 case CmpInst::FCMP_OEQ:
459 CondCode = AArch64CC::EQ;
460 break;
461 case CmpInst::FCMP_OGT:
462 CondCode = AArch64CC::GT;
463 break;
464 case CmpInst::FCMP_OGE:
465 CondCode = AArch64CC::GE;
466 break;
467 case CmpInst::FCMP_OLT:
468 CondCode = AArch64CC::MI;
469 break;
470 case CmpInst::FCMP_OLE:
471 CondCode = AArch64CC::LS;
472 break;
473 case CmpInst::FCMP_ONE:
474 CondCode = AArch64CC::MI;
475 CondCode2 = AArch64CC::GT;
476 break;
477 case CmpInst::FCMP_ORD:
478 CondCode = AArch64CC::VC;
479 break;
480 case CmpInst::FCMP_UNO:
481 CondCode = AArch64CC::VS;
482 break;
483 case CmpInst::FCMP_UEQ:
484 CondCode = AArch64CC::EQ;
485 CondCode2 = AArch64CC::VS;
486 break;
487 case CmpInst::FCMP_UGT:
488 CondCode = AArch64CC::HI;
489 break;
490 case CmpInst::FCMP_UGE:
491 CondCode = AArch64CC::PL;
492 break;
493 case CmpInst::FCMP_ULT:
494 CondCode = AArch64CC::LT;
495 break;
496 case CmpInst::FCMP_ULE:
497 CondCode = AArch64CC::LE;
498 break;
499 case CmpInst::FCMP_UNE:
500 CondCode = AArch64CC::NE;
501 break;
502 }
503}
504
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000505bool AArch64InstructionSelector::selectCompareBranch(
506 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
507
508 const unsigned CondReg = I.getOperand(0).getReg();
509 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
510 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
511 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
512 return false;
513
514 unsigned LHS = CCMI->getOperand(2).getReg();
515 unsigned RHS = CCMI->getOperand(3).getReg();
516 if (!getConstantVRegVal(RHS, MRI))
517 std::swap(RHS, LHS);
518
519 const auto RHSImm = getConstantVRegVal(RHS, MRI);
520 if (!RHSImm || *RHSImm != 0)
521 return false;
522
523 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
524 if (RB.getID() != AArch64::GPRRegBankID)
525 return false;
526
527 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
528 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
529 return false;
530
531 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
532 unsigned CBOpc = 0;
533 if (CmpWidth <= 32)
534 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
535 else if (CmpWidth == 64)
536 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
537 else
538 return false;
539
540 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
541 .addUse(LHS)
542 .addMBB(DestMBB);
543
544 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
545 I.eraseFromParent();
546 return true;
547}
548
Tim Northovere9600d82017-02-08 17:57:27 +0000549bool AArch64InstructionSelector::selectVaStartAAPCS(
550 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
551 return false;
552}
553
554bool AArch64InstructionSelector::selectVaStartDarwin(
555 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
556 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
557 unsigned ListReg = I.getOperand(0).getReg();
558
559 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
560
561 auto MIB =
562 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
563 .addDef(ArgsAddrReg)
564 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
565 .addImm(0)
566 .addImm(0);
567
568 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
569
570 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
571 .addUse(ArgsAddrReg)
572 .addUse(ListReg)
573 .addImm(0)
574 .addMemOperand(*I.memoperands_begin());
575
576 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
577 I.eraseFromParent();
578 return true;
579}
580
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000581void AArch64InstructionSelector::beginFunction(
582 const MachineFunction &MF) {
583 ForCodeSize = MF.getFunction()->optForSize();
584 AvailableFeatures = computeAvailableFeatures(&MF, &STI);
585}
586
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000587bool AArch64InstructionSelector::select(MachineInstr &I) const {
588 assert(I.getParent() && "Instruction should be in a basic block!");
589 assert(I.getParent()->getParent() && "Instruction should be in a function!");
590
591 MachineBasicBlock &MBB = *I.getParent();
592 MachineFunction &MF = *MBB.getParent();
593 MachineRegisterInfo &MRI = MF.getRegInfo();
594
Tim Northovercdf23f12016-10-31 18:30:59 +0000595 unsigned Opcode = I.getOpcode();
596 if (!isPreISelGenericOpcode(I.getOpcode())) {
597 // Certain non-generic instructions also need some special handling.
598
599 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
600 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000601
602 if (Opcode == TargetOpcode::PHI) {
603 const unsigned DefReg = I.getOperand(0).getReg();
604 const LLT DefTy = MRI.getType(DefReg);
605
606 const TargetRegisterClass *DefRC = nullptr;
607 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
608 DefRC = TRI.getRegClass(DefReg);
609 } else {
610 const RegClassOrRegBank &RegClassOrBank =
611 MRI.getRegClassOrRegBank(DefReg);
612
613 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
614 if (!DefRC) {
615 if (!DefTy.isValid()) {
616 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
617 return false;
618 }
619 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
620 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
621 if (!DefRC) {
622 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
623 return false;
624 }
625 }
626 }
627
628 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
629 }
630
631 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000632 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000633
634 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000635 }
636
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000637
638 if (I.getNumOperands() != I.getNumExplicitOperands()) {
639 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
640 return false;
641 }
642
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000643 if (selectImpl(I))
644 return true;
645
Tim Northover32a078a2016-09-15 10:09:59 +0000646 LLT Ty =
647 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000648
Tim Northover69271c62016-10-12 22:49:11 +0000649 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000650 case TargetOpcode::G_BRCOND: {
651 if (Ty.getSizeInBits() > 32) {
652 // We shouldn't need this on AArch64, but it would be implemented as an
653 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
654 // bit being tested is < 32.
655 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
656 << ", expected at most 32-bits");
657 return false;
658 }
659
660 const unsigned CondReg = I.getOperand(0).getReg();
661 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
662
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000663 if (selectCompareBranch(I, MF, MRI))
664 return true;
665
Tim Northover5e3dbf32016-10-12 22:49:01 +0000666 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
667 .addUse(CondReg)
668 .addImm(/*bit offset=*/0)
669 .addMBB(DestMBB);
670
671 I.eraseFromParent();
672 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
673 }
674
Kristof Beyls65a12c02017-01-30 09:13:18 +0000675 case TargetOpcode::G_BRINDIRECT: {
676 I.setDesc(TII.get(AArch64::BR));
677 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
678 }
679
Tim Northover4494d692016-10-18 19:47:57 +0000680 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000681 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000682 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
683
684 const LLT s32 = LLT::scalar(32);
685 const LLT s64 = LLT::scalar(64);
686 const LLT p0 = LLT::pointer(0, 64);
687
688 const unsigned DefReg = I.getOperand(0).getReg();
689 const LLT DefTy = MRI.getType(DefReg);
690 const unsigned DefSize = DefTy.getSizeInBits();
691 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
692
693 // FIXME: Redundant check, but even less readable when factored out.
694 if (isFP) {
695 if (Ty != s32 && Ty != s64) {
696 DEBUG(dbgs() << "Unable to materialize FP " << Ty
697 << " constant, expected: " << s32 << " or " << s64
698 << '\n');
699 return false;
700 }
701
702 if (RB.getID() != AArch64::FPRRegBankID) {
703 DEBUG(dbgs() << "Unable to materialize FP " << Ty
704 << " constant on bank: " << RB << ", expected: FPR\n");
705 return false;
706 }
707 } else {
708 if (Ty != s32 && Ty != s64 && Ty != p0) {
709 DEBUG(dbgs() << "Unable to materialize integer " << Ty
710 << " constant, expected: " << s32 << ", " << s64 << ", or "
711 << p0 << '\n');
712 return false;
713 }
714
715 if (RB.getID() != AArch64::GPRRegBankID) {
716 DEBUG(dbgs() << "Unable to materialize integer " << Ty
717 << " constant on bank: " << RB << ", expected: GPR\n");
718 return false;
719 }
720 }
721
722 const unsigned MovOpc =
723 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
724
725 I.setDesc(TII.get(MovOpc));
726
727 if (isFP) {
728 const TargetRegisterClass &GPRRC =
729 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
730 const TargetRegisterClass &FPRRC =
731 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
732
733 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
734 MachineOperand &RegOp = I.getOperand(0);
735 RegOp.setReg(DefGPRReg);
736
737 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
738 TII.get(AArch64::COPY))
739 .addDef(DefReg)
740 .addUse(DefGPRReg);
741
742 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
743 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
744 return false;
745 }
746
747 MachineOperand &ImmOp = I.getOperand(1);
748 // FIXME: Is going through int64_t always correct?
749 ImmOp.ChangeToImmediate(
750 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000751 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000752 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
753 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000754 } else if (I.getOperand(1).isImm()) {
755 uint64_t Val = I.getOperand(1).getImm();
756 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000757 }
758
759 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
760 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000761 }
762
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000763 case TargetOpcode::G_FRAME_INDEX: {
764 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000765 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000766 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000767 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000768 return false;
769 }
770
771 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000772
773 // MOs for a #0 shifted immediate.
774 I.addOperand(MachineOperand::CreateImm(0));
775 I.addOperand(MachineOperand::CreateImm(0));
776
777 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
778 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000779
780 case TargetOpcode::G_GLOBAL_VALUE: {
781 auto GV = I.getOperand(1).getGlobal();
782 if (GV->isThreadLocal()) {
783 // FIXME: we don't support TLS yet.
784 return false;
785 }
786 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000787 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000788 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000789 I.getOperand(1).setTargetFlags(OpFlags);
790 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000791 I.setDesc(TII.get(AArch64::MOVaddr));
792 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
793 MachineInstrBuilder MIB(MF, I);
794 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
795 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
796 }
797 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
798 }
799
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000800 case TargetOpcode::G_LOAD:
801 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000802 LLT MemTy = Ty;
803 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000804
Tim Northover5ae83502016-09-15 09:20:34 +0000805 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000806 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000807 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000808 return false;
809 }
810
Tim Northover48dfa1a2017-02-13 22:14:16 +0000811 auto &MemOp = **I.memoperands_begin();
812 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
813 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
814 return false;
815 }
816
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000817 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000818#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000819 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000820 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000821 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
822 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000823 assert(MRI.getType(PtrReg).isPointer() &&
824 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000825#endif
826
827 const unsigned ValReg = I.getOperand(0).getReg();
828 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
829
830 const unsigned NewOpc =
831 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
832 if (NewOpc == I.getOpcode())
833 return false;
834
835 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000836
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000837 uint64_t Offset = 0;
838 auto *PtrMI = MRI.getVRegDef(PtrReg);
839
840 // Try to fold a GEP into our unsigned immediate addressing mode.
841 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
842 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
843 int64_t Imm = *COff;
844 const unsigned Size = MemTy.getSizeInBits() / 8;
845 const unsigned Scale = Log2_32(Size);
846 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
847 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
848 I.getOperand(1).setReg(Ptr2Reg);
849 PtrMI = MRI.getVRegDef(Ptr2Reg);
850 Offset = Imm / Size;
851 }
852 }
853 }
854
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000855 // If we haven't folded anything into our addressing mode yet, try to fold
856 // a frame index into the base+offset.
857 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
858 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
859
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000860 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000861
862 // If we're storing a 0, use WZR/XZR.
863 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
864 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
865 if (I.getOpcode() == AArch64::STRWui)
866 I.getOperand(0).setReg(AArch64::WZR);
867 else if (I.getOpcode() == AArch64::STRXui)
868 I.getOperand(0).setReg(AArch64::XZR);
869 }
870 }
871
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000872 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
873 }
874
Tim Northover9dd78f82017-02-08 21:22:25 +0000875 case TargetOpcode::G_SMULH:
876 case TargetOpcode::G_UMULH: {
877 // Reject the various things we don't support yet.
878 if (unsupportedBinOp(I, RBI, MRI, TRI))
879 return false;
880
881 const unsigned DefReg = I.getOperand(0).getReg();
882 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
883
884 if (RB.getID() != AArch64::GPRRegBankID) {
885 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
886 return false;
887 }
888
889 if (Ty != LLT::scalar(64)) {
890 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
891 << ", expected: " << LLT::scalar(64) << '\n');
892 return false;
893 }
894
895 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
896 : AArch64::UMULHrr;
897 I.setDesc(TII.get(NewOpc));
898
899 // Now that we selected an opcode, we need to constrain the register
900 // operands to use appropriate classes.
901 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
902 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000903 case TargetOpcode::G_FADD:
904 case TargetOpcode::G_FSUB:
905 case TargetOpcode::G_FMUL:
906 case TargetOpcode::G_FDIV:
907
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000908 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000909 case TargetOpcode::G_SHL:
910 case TargetOpcode::G_LSHR:
911 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000912 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000913 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000914 if (unsupportedBinOp(I, RBI, MRI, TRI))
915 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000916
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000917 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000918
919 const unsigned DefReg = I.getOperand(0).getReg();
920 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
921
922 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
923 if (NewOpc == I.getOpcode())
924 return false;
925
926 I.setDesc(TII.get(NewOpc));
927 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000928
929 // Now that we selected an opcode, we need to constrain the register
930 // operands to use appropriate classes.
931 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
932 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000933
Tim Northover398c5f52017-02-14 20:56:29 +0000934 case TargetOpcode::G_PTR_MASK: {
935 uint64_t Align = I.getOperand(2).getImm();
936 if (Align >= 64 || Align == 0)
937 return false;
938
939 uint64_t Mask = ~((1ULL << Align) - 1);
940 I.setDesc(TII.get(AArch64::ANDXri));
941 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
942
943 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
944 }
Tim Northover037af52c2016-10-31 18:31:09 +0000945 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000946 case TargetOpcode::G_TRUNC: {
947 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
948 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
949
950 const unsigned DstReg = I.getOperand(0).getReg();
951 const unsigned SrcReg = I.getOperand(1).getReg();
952
953 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
954 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
955
956 if (DstRB.getID() != SrcRB.getID()) {
957 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
958 return false;
959 }
960
961 if (DstRB.getID() == AArch64::GPRRegBankID) {
962 const TargetRegisterClass *DstRC =
963 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
964 if (!DstRC)
965 return false;
966
967 const TargetRegisterClass *SrcRC =
968 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
969 if (!SrcRC)
970 return false;
971
972 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
973 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
974 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
975 return false;
976 }
977
978 if (DstRC == SrcRC) {
979 // Nothing to be done
980 } else if (DstRC == &AArch64::GPR32RegClass &&
981 SrcRC == &AArch64::GPR64RegClass) {
982 I.getOperand(1).setSubReg(AArch64::sub_32);
983 } else {
984 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.
1332bool AArch64InstructionSelector::selectArithImmed(
1333 MachineOperand &Root, MachineOperand &Result1,
1334 MachineOperand &Result2) const {
1335 MachineInstr &MI = *Root.getParent();
1336 MachineBasicBlock &MBB = *MI.getParent();
1337 MachineFunction &MF = *MBB.getParent();
1338 MachineRegisterInfo &MRI = MF.getRegInfo();
1339
1340 // This function is called from the addsub_shifted_imm ComplexPattern,
1341 // which lists [imm] as the list of opcode it's interested in, however
1342 // we still need to check whether the operand is actually an immediate
1343 // here because the ComplexPattern opcode list is only used in
1344 // root-level opcode matching.
1345 uint64_t Immed;
1346 if (Root.isImm())
1347 Immed = Root.getImm();
1348 else if (Root.isCImm())
1349 Immed = Root.getCImm()->getZExtValue();
1350 else if (Root.isReg()) {
1351 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1352 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1353 return false;
Daniel Sanders0e642022017-03-16 18:04:50 +00001354 MachineOperand &Op1 = Def->getOperand(1);
1355 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1356 return false;
1357 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001358 } else
1359 return false;
1360
1361 unsigned ShiftAmt;
1362
1363 if (Immed >> 12 == 0) {
1364 ShiftAmt = 0;
1365 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1366 ShiftAmt = 12;
1367 Immed = Immed >> 12;
1368 } else
1369 return false;
1370
1371 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1372 Result1.ChangeToImmediate(Immed);
1373 Result1.clearParent();
1374 Result2.ChangeToImmediate(ShVal);
1375 Result2.clearParent();
1376 return true;
1377}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001378
1379namespace llvm {
1380InstructionSelector *
1381createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1382 AArch64Subtarget &Subtarget,
1383 AArch64RegisterBankInfo &RBI) {
1384 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1385}
1386}