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