blob: 878dac6bff1e31002e992421d5e8bee215c8a153 [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"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000027#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/IR/Type.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
32
33#define DEBUG_TYPE "aarch64-isel"
34
35using namespace llvm;
36
37#ifndef LLVM_BUILD_GLOBAL_ISEL
38#error "You shouldn't build this"
39#endif
40
Daniel Sanders0b5293f2017-04-06 09:49:34 +000041namespace {
42
43class AArch64InstructionSelector : public InstructionSelector {
44public:
45 AArch64InstructionSelector(const AArch64TargetMachine &TM,
46 const AArch64Subtarget &STI,
47 const AArch64RegisterBankInfo &RBI);
48
49 bool select(MachineInstr &I) const override;
50
51private:
52 /// tblgen-erated 'select' implementation, used as the initial selector for
53 /// the patterns that don't require complex C++.
54 bool selectImpl(MachineInstr &I) const;
55
56 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
57 MachineRegisterInfo &MRI) const;
58 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
59 MachineRegisterInfo &MRI) const;
60
61 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
62 MachineRegisterInfo &MRI) const;
63
64 bool selectArithImmed(MachineOperand &Root, MachineOperand &Result1,
65 MachineOperand &Result2) const;
66
67 const AArch64TargetMachine &TM;
68 const AArch64Subtarget &STI;
69 const AArch64InstrInfo &TII;
70 const AArch64RegisterInfo &TRI;
71 const AArch64RegisterBankInfo &RBI;
72
73// We declare the temporaries used by selectImpl() in the class to minimize the
74// cost of constructing placeholder values.
75#define GET_GLOBALISEL_TEMPORARIES_DECL
76#include "AArch64GenGlobalISel.inc"
77#undef GET_GLOBALISEL_TEMPORARIES_DECL
78};
79
80} // end anonymous namespace
81
Daniel Sanders8a4bae92017-03-14 21:32:08 +000082#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000083#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000084#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000085
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000086AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000087 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
88 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000089 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
90 TRI(*STI.getRegisterInfo()), RBI(RBI)
91#define GET_GLOBALISEL_TEMPORARIES_INIT
92#include "AArch64GenGlobalISel.inc"
93#undef GET_GLOBALISEL_TEMPORARIES_INIT
94{
95}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000096
Tim Northoverfb8d9892016-10-12 22:49:15 +000097// FIXME: This should be target-independent, inferred from the types declared
98// for each class in the bank.
99static const TargetRegisterClass *
100getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
101 const RegisterBankInfo &RBI) {
102 if (RB.getID() == AArch64::GPRRegBankID) {
103 if (Ty.getSizeInBits() <= 32)
104 return &AArch64::GPR32RegClass;
105 if (Ty.getSizeInBits() == 64)
106 return &AArch64::GPR64RegClass;
107 return nullptr;
108 }
109
110 if (RB.getID() == AArch64::FPRRegBankID) {
111 if (Ty.getSizeInBits() == 32)
112 return &AArch64::FPR32RegClass;
113 if (Ty.getSizeInBits() == 64)
114 return &AArch64::FPR64RegClass;
115 if (Ty.getSizeInBits() == 128)
116 return &AArch64::FPR128RegClass;
117 return nullptr;
118 }
119
120 return nullptr;
121}
122
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000123/// Check whether \p I is a currently unsupported binary operation:
124/// - it has an unsized type
125/// - an operand is not a vreg
126/// - all operands are not in the same bank
127/// These are checks that should someday live in the verifier, but right now,
128/// these are mostly limitations of the aarch64 selector.
129static bool unsupportedBinOp(const MachineInstr &I,
130 const AArch64RegisterBankInfo &RBI,
131 const MachineRegisterInfo &MRI,
132 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000133 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000134 if (!Ty.isValid()) {
135 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000136 return true;
137 }
138
139 const RegisterBank *PrevOpBank = nullptr;
140 for (auto &MO : I.operands()) {
141 // FIXME: Support non-register operands.
142 if (!MO.isReg()) {
143 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
144 return true;
145 }
146
147 // FIXME: Can generic operations have physical registers operands? If
148 // so, this will need to be taught about that, and we'll need to get the
149 // bank out of the minimal class for the register.
150 // Either way, this needs to be documented (and possibly verified).
151 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
152 DEBUG(dbgs() << "Generic inst has physical register operand\n");
153 return true;
154 }
155
156 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
157 if (!OpBank) {
158 DEBUG(dbgs() << "Generic register has no bank or class\n");
159 return true;
160 }
161
162 if (PrevOpBank && OpBank != PrevOpBank) {
163 DEBUG(dbgs() << "Generic inst operands have different banks\n");
164 return true;
165 }
166 PrevOpBank = OpBank;
167 }
168 return false;
169}
170
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000171/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000172/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000173/// and of size \p OpSize.
174/// \returns \p GenericOpc if the combination is unsupported.
175static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
176 unsigned OpSize) {
177 switch (RegBankID) {
178 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000179 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000180 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000181 case TargetOpcode::G_SHL:
182 return AArch64::LSLVWr;
183 case TargetOpcode::G_LSHR:
184 return AArch64::LSRVWr;
185 case TargetOpcode::G_ASHR:
186 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000187 default:
188 return GenericOpc;
189 }
Tim Northover55782222016-10-18 20:03:48 +0000190 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000191 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000192 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000194 case TargetOpcode::G_SHL:
195 return AArch64::LSLVXr;
196 case TargetOpcode::G_LSHR:
197 return AArch64::LSRVXr;
198 case TargetOpcode::G_ASHR:
199 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000200 default:
201 return GenericOpc;
202 }
203 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000204 case AArch64::FPRRegBankID:
205 switch (OpSize) {
206 case 32:
207 switch (GenericOpc) {
208 case TargetOpcode::G_FADD:
209 return AArch64::FADDSrr;
210 case TargetOpcode::G_FSUB:
211 return AArch64::FSUBSrr;
212 case TargetOpcode::G_FMUL:
213 return AArch64::FMULSrr;
214 case TargetOpcode::G_FDIV:
215 return AArch64::FDIVSrr;
216 default:
217 return GenericOpc;
218 }
219 case 64:
220 switch (GenericOpc) {
221 case TargetOpcode::G_FADD:
222 return AArch64::FADDDrr;
223 case TargetOpcode::G_FSUB:
224 return AArch64::FSUBDrr;
225 case TargetOpcode::G_FMUL:
226 return AArch64::FMULDrr;
227 case TargetOpcode::G_FDIV:
228 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000229 case TargetOpcode::G_OR:
230 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000231 default:
232 return GenericOpc;
233 }
234 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000235 };
236 return GenericOpc;
237}
238
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000239/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
240/// appropriate for the (value) register bank \p RegBankID and of memory access
241/// size \p OpSize. This returns the variant with the base+unsigned-immediate
242/// addressing mode (e.g., LDRXui).
243/// \returns \p GenericOpc if the combination is unsupported.
244static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
245 unsigned OpSize) {
246 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
247 switch (RegBankID) {
248 case AArch64::GPRRegBankID:
249 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000250 case 8:
251 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
252 case 16:
253 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000254 case 32:
255 return isStore ? AArch64::STRWui : AArch64::LDRWui;
256 case 64:
257 return isStore ? AArch64::STRXui : AArch64::LDRXui;
258 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000259 case AArch64::FPRRegBankID:
260 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000261 case 8:
262 return isStore ? AArch64::STRBui : AArch64::LDRBui;
263 case 16:
264 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000265 case 32:
266 return isStore ? AArch64::STRSui : AArch64::LDRSui;
267 case 64:
268 return isStore ? AArch64::STRDui : AArch64::LDRDui;
269 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000270 };
271 return GenericOpc;
272}
273
Quentin Colombetcb629a82016-10-12 03:57:49 +0000274static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
275 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
276 const RegisterBankInfo &RBI) {
277
278 unsigned DstReg = I.getOperand(0).getReg();
279 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
280 assert(I.isCopy() && "Generic operators do not allow physical registers");
281 return true;
282 }
283
284 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
285 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
286 unsigned SrcReg = I.getOperand(1).getReg();
287 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
288 (void)SrcSize;
289 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
290 "No phys reg on generic operators");
291 assert(
292 (DstSize == SrcSize ||
293 // Copies are a mean to setup initial types, the number of
294 // bits may not exactly match.
295 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
296 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
297 // Copies are a mean to copy bits around, as long as we are
298 // on the same register class, that's fine. Otherwise, that
299 // means we need some SUBREG_TO_REG or AND & co.
300 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
301 "Copy with different width?!");
302 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
303 "GPRs cannot get more than 64-bit width values");
304 const TargetRegisterClass *RC = nullptr;
305
306 if (RegBank.getID() == AArch64::FPRRegBankID) {
307 if (DstSize <= 32)
308 RC = &AArch64::FPR32RegClass;
309 else if (DstSize <= 64)
310 RC = &AArch64::FPR64RegClass;
311 else if (DstSize <= 128)
312 RC = &AArch64::FPR128RegClass;
313 else {
314 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
315 return false;
316 }
317 } else {
318 assert(RegBank.getID() == AArch64::GPRRegBankID &&
319 "Bitcast for the flags?");
320 RC =
321 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
322 }
323
324 // No need to constrain SrcReg. It will get constrained when
325 // we hit another of its use or its defs.
326 // Copies do not have constraints.
327 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
328 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
329 << " operand\n");
330 return false;
331 }
332 I.setDesc(TII.get(AArch64::COPY));
333 return true;
334}
335
Tim Northover69271c62016-10-12 22:49:11 +0000336static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
337 if (!DstTy.isScalar() || !SrcTy.isScalar())
338 return GenericOpc;
339
340 const unsigned DstSize = DstTy.getSizeInBits();
341 const unsigned SrcSize = SrcTy.getSizeInBits();
342
343 switch (DstSize) {
344 case 32:
345 switch (SrcSize) {
346 case 32:
347 switch (GenericOpc) {
348 case TargetOpcode::G_SITOFP:
349 return AArch64::SCVTFUWSri;
350 case TargetOpcode::G_UITOFP:
351 return AArch64::UCVTFUWSri;
352 case TargetOpcode::G_FPTOSI:
353 return AArch64::FCVTZSUWSr;
354 case TargetOpcode::G_FPTOUI:
355 return AArch64::FCVTZUUWSr;
356 default:
357 return GenericOpc;
358 }
359 case 64:
360 switch (GenericOpc) {
361 case TargetOpcode::G_SITOFP:
362 return AArch64::SCVTFUXSri;
363 case TargetOpcode::G_UITOFP:
364 return AArch64::UCVTFUXSri;
365 case TargetOpcode::G_FPTOSI:
366 return AArch64::FCVTZSUWDr;
367 case TargetOpcode::G_FPTOUI:
368 return AArch64::FCVTZUUWDr;
369 default:
370 return GenericOpc;
371 }
372 default:
373 return GenericOpc;
374 }
375 case 64:
376 switch (SrcSize) {
377 case 32:
378 switch (GenericOpc) {
379 case TargetOpcode::G_SITOFP:
380 return AArch64::SCVTFUWDri;
381 case TargetOpcode::G_UITOFP:
382 return AArch64::UCVTFUWDri;
383 case TargetOpcode::G_FPTOSI:
384 return AArch64::FCVTZSUXSr;
385 case TargetOpcode::G_FPTOUI:
386 return AArch64::FCVTZUUXSr;
387 default:
388 return GenericOpc;
389 }
390 case 64:
391 switch (GenericOpc) {
392 case TargetOpcode::G_SITOFP:
393 return AArch64::SCVTFUXDri;
394 case TargetOpcode::G_UITOFP:
395 return AArch64::UCVTFUXDri;
396 case TargetOpcode::G_FPTOSI:
397 return AArch64::FCVTZSUXDr;
398 case TargetOpcode::G_FPTOUI:
399 return AArch64::FCVTZUUXDr;
400 default:
401 return GenericOpc;
402 }
403 default:
404 return GenericOpc;
405 }
406 default:
407 return GenericOpc;
408 };
409 return GenericOpc;
410}
411
Tim Northover6c02ad52016-10-12 22:49:04 +0000412static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
413 switch (P) {
414 default:
415 llvm_unreachable("Unknown condition code!");
416 case CmpInst::ICMP_NE:
417 return AArch64CC::NE;
418 case CmpInst::ICMP_EQ:
419 return AArch64CC::EQ;
420 case CmpInst::ICMP_SGT:
421 return AArch64CC::GT;
422 case CmpInst::ICMP_SGE:
423 return AArch64CC::GE;
424 case CmpInst::ICMP_SLT:
425 return AArch64CC::LT;
426 case CmpInst::ICMP_SLE:
427 return AArch64CC::LE;
428 case CmpInst::ICMP_UGT:
429 return AArch64CC::HI;
430 case CmpInst::ICMP_UGE:
431 return AArch64CC::HS;
432 case CmpInst::ICMP_ULT:
433 return AArch64CC::LO;
434 case CmpInst::ICMP_ULE:
435 return AArch64CC::LS;
436 }
437}
438
Tim Northover7dd378d2016-10-12 22:49:07 +0000439static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
440 AArch64CC::CondCode &CondCode,
441 AArch64CC::CondCode &CondCode2) {
442 CondCode2 = AArch64CC::AL;
443 switch (P) {
444 default:
445 llvm_unreachable("Unknown FP condition!");
446 case CmpInst::FCMP_OEQ:
447 CondCode = AArch64CC::EQ;
448 break;
449 case CmpInst::FCMP_OGT:
450 CondCode = AArch64CC::GT;
451 break;
452 case CmpInst::FCMP_OGE:
453 CondCode = AArch64CC::GE;
454 break;
455 case CmpInst::FCMP_OLT:
456 CondCode = AArch64CC::MI;
457 break;
458 case CmpInst::FCMP_OLE:
459 CondCode = AArch64CC::LS;
460 break;
461 case CmpInst::FCMP_ONE:
462 CondCode = AArch64CC::MI;
463 CondCode2 = AArch64CC::GT;
464 break;
465 case CmpInst::FCMP_ORD:
466 CondCode = AArch64CC::VC;
467 break;
468 case CmpInst::FCMP_UNO:
469 CondCode = AArch64CC::VS;
470 break;
471 case CmpInst::FCMP_UEQ:
472 CondCode = AArch64CC::EQ;
473 CondCode2 = AArch64CC::VS;
474 break;
475 case CmpInst::FCMP_UGT:
476 CondCode = AArch64CC::HI;
477 break;
478 case CmpInst::FCMP_UGE:
479 CondCode = AArch64CC::PL;
480 break;
481 case CmpInst::FCMP_ULT:
482 CondCode = AArch64CC::LT;
483 break;
484 case CmpInst::FCMP_ULE:
485 CondCode = AArch64CC::LE;
486 break;
487 case CmpInst::FCMP_UNE:
488 CondCode = AArch64CC::NE;
489 break;
490 }
491}
492
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000493bool AArch64InstructionSelector::selectCompareBranch(
494 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
495
496 const unsigned CondReg = I.getOperand(0).getReg();
497 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
498 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
499 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
500 return false;
501
502 unsigned LHS = CCMI->getOperand(2).getReg();
503 unsigned RHS = CCMI->getOperand(3).getReg();
504 if (!getConstantVRegVal(RHS, MRI))
505 std::swap(RHS, LHS);
506
507 const auto RHSImm = getConstantVRegVal(RHS, MRI);
508 if (!RHSImm || *RHSImm != 0)
509 return false;
510
511 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
512 if (RB.getID() != AArch64::GPRRegBankID)
513 return false;
514
515 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
516 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
517 return false;
518
519 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
520 unsigned CBOpc = 0;
521 if (CmpWidth <= 32)
522 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
523 else if (CmpWidth == 64)
524 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
525 else
526 return false;
527
528 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
529 .addUse(LHS)
530 .addMBB(DestMBB);
531
532 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
533 I.eraseFromParent();
534 return true;
535}
536
Tim Northovere9600d82017-02-08 17:57:27 +0000537bool AArch64InstructionSelector::selectVaStartAAPCS(
538 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
539 return false;
540}
541
542bool AArch64InstructionSelector::selectVaStartDarwin(
543 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
544 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
545 unsigned ListReg = I.getOperand(0).getReg();
546
547 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
548
549 auto MIB =
550 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
551 .addDef(ArgsAddrReg)
552 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
553 .addImm(0)
554 .addImm(0);
555
556 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
557
558 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
559 .addUse(ArgsAddrReg)
560 .addUse(ListReg)
561 .addImm(0)
562 .addMemOperand(*I.memoperands_begin());
563
564 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
565 I.eraseFromParent();
566 return true;
567}
568
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000569bool AArch64InstructionSelector::select(MachineInstr &I) const {
570 assert(I.getParent() && "Instruction should be in a basic block!");
571 assert(I.getParent()->getParent() && "Instruction should be in a function!");
572
573 MachineBasicBlock &MBB = *I.getParent();
574 MachineFunction &MF = *MBB.getParent();
575 MachineRegisterInfo &MRI = MF.getRegInfo();
576
Tim Northovercdf23f12016-10-31 18:30:59 +0000577 unsigned Opcode = I.getOpcode();
578 if (!isPreISelGenericOpcode(I.getOpcode())) {
579 // Certain non-generic instructions also need some special handling.
580
581 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
582 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000583
584 if (Opcode == TargetOpcode::PHI) {
585 const unsigned DefReg = I.getOperand(0).getReg();
586 const LLT DefTy = MRI.getType(DefReg);
587
588 const TargetRegisterClass *DefRC = nullptr;
589 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
590 DefRC = TRI.getRegClass(DefReg);
591 } else {
592 const RegClassOrRegBank &RegClassOrBank =
593 MRI.getRegClassOrRegBank(DefReg);
594
595 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
596 if (!DefRC) {
597 if (!DefTy.isValid()) {
598 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
599 return false;
600 }
601 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
602 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
603 if (!DefRC) {
604 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
605 return false;
606 }
607 }
608 }
609
610 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
611 }
612
613 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000614 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000615
616 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000617 }
618
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000619
620 if (I.getNumOperands() != I.getNumExplicitOperands()) {
621 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
622 return false;
623 }
624
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000625 if (selectImpl(I))
626 return true;
627
Tim Northover32a078a2016-09-15 10:09:59 +0000628 LLT Ty =
629 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000630
Tim Northover69271c62016-10-12 22:49:11 +0000631 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000632 case TargetOpcode::G_BRCOND: {
633 if (Ty.getSizeInBits() > 32) {
634 // We shouldn't need this on AArch64, but it would be implemented as an
635 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
636 // bit being tested is < 32.
637 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
638 << ", expected at most 32-bits");
639 return false;
640 }
641
642 const unsigned CondReg = I.getOperand(0).getReg();
643 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
644
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000645 if (selectCompareBranch(I, MF, MRI))
646 return true;
647
Tim Northover5e3dbf32016-10-12 22:49:01 +0000648 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
649 .addUse(CondReg)
650 .addImm(/*bit offset=*/0)
651 .addMBB(DestMBB);
652
653 I.eraseFromParent();
654 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
655 }
656
Kristof Beyls65a12c02017-01-30 09:13:18 +0000657 case TargetOpcode::G_BRINDIRECT: {
658 I.setDesc(TII.get(AArch64::BR));
659 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
660 }
661
Tim Northover4494d692016-10-18 19:47:57 +0000662 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000663 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000664 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
665
666 const LLT s32 = LLT::scalar(32);
667 const LLT s64 = LLT::scalar(64);
668 const LLT p0 = LLT::pointer(0, 64);
669
670 const unsigned DefReg = I.getOperand(0).getReg();
671 const LLT DefTy = MRI.getType(DefReg);
672 const unsigned DefSize = DefTy.getSizeInBits();
673 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
674
675 // FIXME: Redundant check, but even less readable when factored out.
676 if (isFP) {
677 if (Ty != s32 && Ty != s64) {
678 DEBUG(dbgs() << "Unable to materialize FP " << Ty
679 << " constant, expected: " << s32 << " or " << s64
680 << '\n');
681 return false;
682 }
683
684 if (RB.getID() != AArch64::FPRRegBankID) {
685 DEBUG(dbgs() << "Unable to materialize FP " << Ty
686 << " constant on bank: " << RB << ", expected: FPR\n");
687 return false;
688 }
689 } else {
690 if (Ty != s32 && Ty != s64 && Ty != p0) {
691 DEBUG(dbgs() << "Unable to materialize integer " << Ty
692 << " constant, expected: " << s32 << ", " << s64 << ", or "
693 << p0 << '\n');
694 return false;
695 }
696
697 if (RB.getID() != AArch64::GPRRegBankID) {
698 DEBUG(dbgs() << "Unable to materialize integer " << Ty
699 << " constant on bank: " << RB << ", expected: GPR\n");
700 return false;
701 }
702 }
703
704 const unsigned MovOpc =
705 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
706
707 I.setDesc(TII.get(MovOpc));
708
709 if (isFP) {
710 const TargetRegisterClass &GPRRC =
711 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
712 const TargetRegisterClass &FPRRC =
713 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
714
715 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
716 MachineOperand &RegOp = I.getOperand(0);
717 RegOp.setReg(DefGPRReg);
718
719 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
720 TII.get(AArch64::COPY))
721 .addDef(DefReg)
722 .addUse(DefGPRReg);
723
724 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
725 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
726 return false;
727 }
728
729 MachineOperand &ImmOp = I.getOperand(1);
730 // FIXME: Is going through int64_t always correct?
731 ImmOp.ChangeToImmediate(
732 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000733 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000734 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
735 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000736 } else if (I.getOperand(1).isImm()) {
737 uint64_t Val = I.getOperand(1).getImm();
738 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000739 }
740
741 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
742 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000743 }
744
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000745 case TargetOpcode::G_FRAME_INDEX: {
746 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000747 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000748 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000749 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000750 return false;
751 }
752
753 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000754
755 // MOs for a #0 shifted immediate.
756 I.addOperand(MachineOperand::CreateImm(0));
757 I.addOperand(MachineOperand::CreateImm(0));
758
759 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
760 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000761
762 case TargetOpcode::G_GLOBAL_VALUE: {
763 auto GV = I.getOperand(1).getGlobal();
764 if (GV->isThreadLocal()) {
765 // FIXME: we don't support TLS yet.
766 return false;
767 }
768 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000769 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000770 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000771 I.getOperand(1).setTargetFlags(OpFlags);
772 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000773 I.setDesc(TII.get(AArch64::MOVaddr));
774 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
775 MachineInstrBuilder MIB(MF, I);
776 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
777 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
778 }
779 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
780 }
781
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000782 case TargetOpcode::G_LOAD:
783 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000784 LLT MemTy = Ty;
785 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000786
Tim Northover5ae83502016-09-15 09:20:34 +0000787 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000788 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000789 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000790 return false;
791 }
792
Tim Northover48dfa1a2017-02-13 22:14:16 +0000793 auto &MemOp = **I.memoperands_begin();
794 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
795 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
796 return false;
797 }
798
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000799 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000800#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000801 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000802 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000803 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
804 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000805 assert(MRI.getType(PtrReg).isPointer() &&
806 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000807#endif
808
809 const unsigned ValReg = I.getOperand(0).getReg();
810 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
811
812 const unsigned NewOpc =
813 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
814 if (NewOpc == I.getOpcode())
815 return false;
816
817 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000818
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000819 uint64_t Offset = 0;
820 auto *PtrMI = MRI.getVRegDef(PtrReg);
821
822 // Try to fold a GEP into our unsigned immediate addressing mode.
823 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
824 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
825 int64_t Imm = *COff;
826 const unsigned Size = MemTy.getSizeInBits() / 8;
827 const unsigned Scale = Log2_32(Size);
828 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
829 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
830 I.getOperand(1).setReg(Ptr2Reg);
831 PtrMI = MRI.getVRegDef(Ptr2Reg);
832 Offset = Imm / Size;
833 }
834 }
835 }
836
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000837 // If we haven't folded anything into our addressing mode yet, try to fold
838 // a frame index into the base+offset.
839 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
840 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
841
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000842 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000843
844 // If we're storing a 0, use WZR/XZR.
845 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
846 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
847 if (I.getOpcode() == AArch64::STRWui)
848 I.getOperand(0).setReg(AArch64::WZR);
849 else if (I.getOpcode() == AArch64::STRXui)
850 I.getOperand(0).setReg(AArch64::XZR);
851 }
852 }
853
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000854 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
855 }
856
Tim Northover9dd78f82017-02-08 21:22:25 +0000857 case TargetOpcode::G_SMULH:
858 case TargetOpcode::G_UMULH: {
859 // Reject the various things we don't support yet.
860 if (unsupportedBinOp(I, RBI, MRI, TRI))
861 return false;
862
863 const unsigned DefReg = I.getOperand(0).getReg();
864 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
865
866 if (RB.getID() != AArch64::GPRRegBankID) {
867 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
868 return false;
869 }
870
871 if (Ty != LLT::scalar(64)) {
872 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
873 << ", expected: " << LLT::scalar(64) << '\n');
874 return false;
875 }
876
877 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
878 : AArch64::UMULHrr;
879 I.setDesc(TII.get(NewOpc));
880
881 // Now that we selected an opcode, we need to constrain the register
882 // operands to use appropriate classes.
883 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
884 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000885 case TargetOpcode::G_FADD:
886 case TargetOpcode::G_FSUB:
887 case TargetOpcode::G_FMUL:
888 case TargetOpcode::G_FDIV:
889
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000890 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000891 case TargetOpcode::G_SHL:
892 case TargetOpcode::G_LSHR:
893 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000894 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000895 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000896 if (unsupportedBinOp(I, RBI, MRI, TRI))
897 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000898
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000899 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000900
901 const unsigned DefReg = I.getOperand(0).getReg();
902 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
903
904 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
905 if (NewOpc == I.getOpcode())
906 return false;
907
908 I.setDesc(TII.get(NewOpc));
909 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000910
911 // Now that we selected an opcode, we need to constrain the register
912 // operands to use appropriate classes.
913 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
914 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000915
Tim Northover398c5f52017-02-14 20:56:29 +0000916 case TargetOpcode::G_PTR_MASK: {
917 uint64_t Align = I.getOperand(2).getImm();
918 if (Align >= 64 || Align == 0)
919 return false;
920
921 uint64_t Mask = ~((1ULL << Align) - 1);
922 I.setDesc(TII.get(AArch64::ANDXri));
923 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
924
925 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
926 }
Tim Northover037af52c2016-10-31 18:31:09 +0000927 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000928 case TargetOpcode::G_TRUNC: {
929 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
930 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
931
932 const unsigned DstReg = I.getOperand(0).getReg();
933 const unsigned SrcReg = I.getOperand(1).getReg();
934
935 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
936 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
937
938 if (DstRB.getID() != SrcRB.getID()) {
939 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
940 return false;
941 }
942
943 if (DstRB.getID() == AArch64::GPRRegBankID) {
944 const TargetRegisterClass *DstRC =
945 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
946 if (!DstRC)
947 return false;
948
949 const TargetRegisterClass *SrcRC =
950 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
951 if (!SrcRC)
952 return false;
953
954 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
955 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
956 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
957 return false;
958 }
959
960 if (DstRC == SrcRC) {
961 // Nothing to be done
962 } else if (DstRC == &AArch64::GPR32RegClass &&
963 SrcRC == &AArch64::GPR64RegClass) {
964 I.getOperand(1).setSubReg(AArch64::sub_32);
965 } else {
966 return false;
967 }
968
969 I.setDesc(TII.get(TargetOpcode::COPY));
970 return true;
971 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
972 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
973 I.setDesc(TII.get(AArch64::XTNv4i16));
974 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
975 return true;
976 }
977 }
978
979 return false;
980 }
981
Tim Northover3d38b3a2016-10-11 20:50:21 +0000982 case TargetOpcode::G_ANYEXT: {
983 const unsigned DstReg = I.getOperand(0).getReg();
984 const unsigned SrcReg = I.getOperand(1).getReg();
985
Quentin Colombetcb629a82016-10-12 03:57:49 +0000986 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
987 if (RBDst.getID() != AArch64::GPRRegBankID) {
988 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
989 return false;
990 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000991
Quentin Colombetcb629a82016-10-12 03:57:49 +0000992 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
993 if (RBSrc.getID() != AArch64::GPRRegBankID) {
994 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000995 return false;
996 }
997
998 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
999
1000 if (DstSize == 0) {
1001 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1002 return false;
1003 }
1004
Quentin Colombetcb629a82016-10-12 03:57:49 +00001005 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001006 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1007 << ", expected: 32 or 64\n");
1008 return false;
1009 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001010 // At this point G_ANYEXT is just like a plain COPY, but we need
1011 // to explicitly form the 64-bit value if any.
1012 if (DstSize > 32) {
1013 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1014 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1015 .addDef(ExtSrc)
1016 .addImm(0)
1017 .addUse(SrcReg)
1018 .addImm(AArch64::sub_32);
1019 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001020 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001021 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001022 }
1023
1024 case TargetOpcode::G_ZEXT:
1025 case TargetOpcode::G_SEXT: {
1026 unsigned Opcode = I.getOpcode();
1027 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1028 SrcTy = MRI.getType(I.getOperand(1).getReg());
1029 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1030 const unsigned DefReg = I.getOperand(0).getReg();
1031 const unsigned SrcReg = I.getOperand(1).getReg();
1032 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1033
1034 if (RB.getID() != AArch64::GPRRegBankID) {
1035 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1036 << ", expected: GPR\n");
1037 return false;
1038 }
1039
1040 MachineInstr *ExtI;
1041 if (DstTy == LLT::scalar(64)) {
1042 // FIXME: Can we avoid manually doing this?
1043 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1044 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1045 << " operand\n");
1046 return false;
1047 }
1048
1049 const unsigned SrcXReg =
1050 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1051 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1052 .addDef(SrcXReg)
1053 .addImm(0)
1054 .addUse(SrcReg)
1055 .addImm(AArch64::sub_32);
1056
1057 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1058 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1059 .addDef(DefReg)
1060 .addUse(SrcXReg)
1061 .addImm(0)
1062 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001063 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001064 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1065 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1066 .addDef(DefReg)
1067 .addUse(SrcReg)
1068 .addImm(0)
1069 .addImm(SrcTy.getSizeInBits() - 1);
1070 } else {
1071 return false;
1072 }
1073
1074 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1075
1076 I.eraseFromParent();
1077 return true;
1078 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001079
Tim Northover69271c62016-10-12 22:49:11 +00001080 case TargetOpcode::G_SITOFP:
1081 case TargetOpcode::G_UITOFP:
1082 case TargetOpcode::G_FPTOSI:
1083 case TargetOpcode::G_FPTOUI: {
1084 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1085 SrcTy = MRI.getType(I.getOperand(1).getReg());
1086 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1087 if (NewOpc == Opcode)
1088 return false;
1089
1090 I.setDesc(TII.get(NewOpc));
1091 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1092
1093 return true;
1094 }
1095
1096
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001097 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001098 case TargetOpcode::G_BITCAST:
1099 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001100
Tim Northover5f7dea82016-11-08 17:44:07 +00001101 case TargetOpcode::G_FPEXT: {
1102 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1103 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1104 << ", expected: " << LLT::scalar(64) << '\n');
1105 return false;
1106 }
1107
1108 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1109 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1110 << ", expected: " << LLT::scalar(32) << '\n');
1111 return false;
1112 }
1113
1114 const unsigned DefReg = I.getOperand(0).getReg();
1115 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1116
1117 if (RB.getID() != AArch64::FPRRegBankID) {
1118 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1119 return false;
1120 }
1121
1122 I.setDesc(TII.get(AArch64::FCVTDSr));
1123 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1124
1125 return true;
1126 }
1127
1128 case TargetOpcode::G_FPTRUNC: {
1129 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1130 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1131 << ", expected: " << LLT::scalar(32) << '\n');
1132 return false;
1133 }
1134
1135 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1136 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1137 << ", expected: " << LLT::scalar(64) << '\n');
1138 return false;
1139 }
1140
1141 const unsigned DefReg = I.getOperand(0).getReg();
1142 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1143
1144 if (RB.getID() != AArch64::FPRRegBankID) {
1145 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1146 return false;
1147 }
1148
1149 I.setDesc(TII.get(AArch64::FCVTSDr));
1150 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1151
1152 return true;
1153 }
1154
Tim Northover9ac0eba2016-11-08 00:45:29 +00001155 case TargetOpcode::G_SELECT: {
1156 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1157 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1158 << ", expected: " << LLT::scalar(1) << '\n');
1159 return false;
1160 }
1161
1162 const unsigned CondReg = I.getOperand(1).getReg();
1163 const unsigned TReg = I.getOperand(2).getReg();
1164 const unsigned FReg = I.getOperand(3).getReg();
1165
1166 unsigned CSelOpc = 0;
1167
1168 if (Ty == LLT::scalar(32)) {
1169 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001170 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001171 CSelOpc = AArch64::CSELXr;
1172 } else {
1173 return false;
1174 }
1175
1176 MachineInstr &TstMI =
1177 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1178 .addDef(AArch64::WZR)
1179 .addUse(CondReg)
1180 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1181
1182 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1183 .addDef(I.getOperand(0).getReg())
1184 .addUse(TReg)
1185 .addUse(FReg)
1186 .addImm(AArch64CC::NE);
1187
1188 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1189 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1190
1191 I.eraseFromParent();
1192 return true;
1193 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001194 case TargetOpcode::G_ICMP: {
1195 if (Ty != LLT::scalar(1)) {
1196 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1197 << ", expected: " << LLT::scalar(1) << '\n');
1198 return false;
1199 }
1200
1201 unsigned CmpOpc = 0;
1202 unsigned ZReg = 0;
1203
1204 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1205 if (CmpTy == LLT::scalar(32)) {
1206 CmpOpc = AArch64::SUBSWrr;
1207 ZReg = AArch64::WZR;
1208 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1209 CmpOpc = AArch64::SUBSXrr;
1210 ZReg = AArch64::XZR;
1211 } else {
1212 return false;
1213 }
1214
Kristof Beyls22524402017-01-05 10:16:08 +00001215 // CSINC increments the result by one when the condition code is false.
1216 // Therefore, we have to invert the predicate to get an increment by 1 when
1217 // the predicate is true.
1218 const AArch64CC::CondCode invCC =
1219 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1220 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001221
1222 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1223 .addDef(ZReg)
1224 .addUse(I.getOperand(2).getReg())
1225 .addUse(I.getOperand(3).getReg());
1226
1227 MachineInstr &CSetMI =
1228 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1229 .addDef(I.getOperand(0).getReg())
1230 .addUse(AArch64::WZR)
1231 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001232 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001233
1234 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1235 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1236
1237 I.eraseFromParent();
1238 return true;
1239 }
1240
Tim Northover7dd378d2016-10-12 22:49:07 +00001241 case TargetOpcode::G_FCMP: {
1242 if (Ty != LLT::scalar(1)) {
1243 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1244 << ", expected: " << LLT::scalar(1) << '\n');
1245 return false;
1246 }
1247
1248 unsigned CmpOpc = 0;
1249 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1250 if (CmpTy == LLT::scalar(32)) {
1251 CmpOpc = AArch64::FCMPSrr;
1252 } else if (CmpTy == LLT::scalar(64)) {
1253 CmpOpc = AArch64::FCMPDrr;
1254 } else {
1255 return false;
1256 }
1257
1258 // FIXME: regbank
1259
1260 AArch64CC::CondCode CC1, CC2;
1261 changeFCMPPredToAArch64CC(
1262 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1263
1264 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1265 .addUse(I.getOperand(2).getReg())
1266 .addUse(I.getOperand(3).getReg());
1267
1268 const unsigned DefReg = I.getOperand(0).getReg();
1269 unsigned Def1Reg = DefReg;
1270 if (CC2 != AArch64CC::AL)
1271 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1272
1273 MachineInstr &CSetMI =
1274 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1275 .addDef(Def1Reg)
1276 .addUse(AArch64::WZR)
1277 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001278 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001279
1280 if (CC2 != AArch64CC::AL) {
1281 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1282 MachineInstr &CSet2MI =
1283 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1284 .addDef(Def2Reg)
1285 .addUse(AArch64::WZR)
1286 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001287 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001288 MachineInstr &OrMI =
1289 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1290 .addDef(DefReg)
1291 .addUse(Def1Reg)
1292 .addUse(Def2Reg);
1293 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1294 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1295 }
1296
1297 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1298 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1299
1300 I.eraseFromParent();
1301 return true;
1302 }
Tim Northovere9600d82017-02-08 17:57:27 +00001303 case TargetOpcode::G_VASTART:
1304 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1305 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001306 }
1307
1308 return false;
1309}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001310
1311/// SelectArithImmed - Select an immediate value that can be represented as
1312/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1313/// Val set to the 12-bit value and Shift set to the shifter operand.
1314bool AArch64InstructionSelector::selectArithImmed(
1315 MachineOperand &Root, MachineOperand &Result1,
1316 MachineOperand &Result2) const {
1317 MachineInstr &MI = *Root.getParent();
1318 MachineBasicBlock &MBB = *MI.getParent();
1319 MachineFunction &MF = *MBB.getParent();
1320 MachineRegisterInfo &MRI = MF.getRegInfo();
1321
1322 // This function is called from the addsub_shifted_imm ComplexPattern,
1323 // which lists [imm] as the list of opcode it's interested in, however
1324 // we still need to check whether the operand is actually an immediate
1325 // here because the ComplexPattern opcode list is only used in
1326 // root-level opcode matching.
1327 uint64_t Immed;
1328 if (Root.isImm())
1329 Immed = Root.getImm();
1330 else if (Root.isCImm())
1331 Immed = Root.getCImm()->getZExtValue();
1332 else if (Root.isReg()) {
1333 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1334 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1335 return false;
Daniel Sanders0e642022017-03-16 18:04:50 +00001336 MachineOperand &Op1 = Def->getOperand(1);
1337 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1338 return false;
1339 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001340 } else
1341 return false;
1342
1343 unsigned ShiftAmt;
1344
1345 if (Immed >> 12 == 0) {
1346 ShiftAmt = 0;
1347 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1348 ShiftAmt = 12;
1349 Immed = Immed >> 12;
1350 } else
1351 return false;
1352
1353 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1354 Result1.ChangeToImmediate(Immed);
1355 Result1.clearParent();
1356 Result2.ChangeToImmediate(ShVal);
1357 Result2.clearParent();
1358 return true;
1359}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001360
1361namespace llvm {
1362InstructionSelector *
1363createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1364 AArch64Subtarget &Subtarget,
1365 AArch64RegisterBankInfo &RBI) {
1366 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1367}
1368}