blob: d32a6bd7f812f6d5a23c82db322b810519ca7fe4 [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the targeting of the InstructionSelector class for
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000015#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000016#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000017#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000020#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000021#include "MCTargetDesc/AArch64AddressingModes.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000022#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Aditya Nandakumar75ad9cc2017-04-19 20:48:50 +000023#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000028#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/IR/Type.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33
34#define DEBUG_TYPE "aarch64-isel"
35
Daniel Sanders6ab0daa2017-07-04 14:35:06 +000036#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
37
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000038using namespace llvm;
39
40#ifndef LLVM_BUILD_GLOBAL_ISEL
41#error "You shouldn't build this"
42#endif
43
Daniel Sanders0b5293f2017-04-06 09:49:34 +000044namespace {
45
Daniel Sanderse7b0d662017-04-21 15:59:56 +000046#define GET_GLOBALISEL_PREDICATE_BITSET
47#include "AArch64GenGlobalISel.inc"
48#undef GET_GLOBALISEL_PREDICATE_BITSET
49
Daniel Sanders0b5293f2017-04-06 09:49:34 +000050class AArch64InstructionSelector : public InstructionSelector {
51public:
52 AArch64InstructionSelector(const AArch64TargetMachine &TM,
53 const AArch64Subtarget &STI,
54 const AArch64RegisterBankInfo &RBI);
55
56 bool select(MachineInstr &I) const override;
57
58private:
59 /// tblgen-erated 'select' implementation, used as the initial selector for
60 /// the patterns that don't require complex C++.
61 bool selectImpl(MachineInstr &I) const;
62
63 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
64 MachineRegisterInfo &MRI) const;
65 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
66 MachineRegisterInfo &MRI) const;
67
68 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
69 MachineRegisterInfo &MRI) const;
70
Daniel Sanders2deea182017-04-22 15:11:04 +000071 ComplexRendererFn selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000072
73 const AArch64TargetMachine &TM;
74 const AArch64Subtarget &STI;
75 const AArch64InstrInfo &TII;
76 const AArch64RegisterInfo &TRI;
77 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +000078
Daniel Sanderse9fdba32017-04-29 17:30:09 +000079#define GET_GLOBALISEL_PREDICATES_DECL
80#include "AArch64GenGlobalISel.inc"
81#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +000082
83// We declare the temporaries used by selectImpl() in the class to minimize the
84// cost of constructing placeholder values.
85#define GET_GLOBALISEL_TEMPORARIES_DECL
86#include "AArch64GenGlobalISel.inc"
87#undef GET_GLOBALISEL_TEMPORARIES_DECL
88};
89
90} // end anonymous namespace
91
Daniel Sanders8a4bae92017-03-14 21:32:08 +000092#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000093#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000094#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000095
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000096AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000097 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
98 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000099 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000100 TRI(*STI.getRegisterInfo()), RBI(RBI),
101#define GET_GLOBALISEL_PREDICATES_INIT
102#include "AArch64GenGlobalISel.inc"
103#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000104#define GET_GLOBALISEL_TEMPORARIES_INIT
105#include "AArch64GenGlobalISel.inc"
106#undef GET_GLOBALISEL_TEMPORARIES_INIT
107{
108}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000109
Tim Northoverfb8d9892016-10-12 22:49:15 +0000110// FIXME: This should be target-independent, inferred from the types declared
111// for each class in the bank.
112static const TargetRegisterClass *
113getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
114 const RegisterBankInfo &RBI) {
115 if (RB.getID() == AArch64::GPRRegBankID) {
116 if (Ty.getSizeInBits() <= 32)
117 return &AArch64::GPR32RegClass;
118 if (Ty.getSizeInBits() == 64)
119 return &AArch64::GPR64RegClass;
120 return nullptr;
121 }
122
123 if (RB.getID() == AArch64::FPRRegBankID) {
124 if (Ty.getSizeInBits() == 32)
125 return &AArch64::FPR32RegClass;
126 if (Ty.getSizeInBits() == 64)
127 return &AArch64::FPR64RegClass;
128 if (Ty.getSizeInBits() == 128)
129 return &AArch64::FPR128RegClass;
130 return nullptr;
131 }
132
133 return nullptr;
134}
135
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000136/// Check whether \p I is a currently unsupported binary operation:
137/// - it has an unsized type
138/// - an operand is not a vreg
139/// - all operands are not in the same bank
140/// These are checks that should someday live in the verifier, but right now,
141/// these are mostly limitations of the aarch64 selector.
142static bool unsupportedBinOp(const MachineInstr &I,
143 const AArch64RegisterBankInfo &RBI,
144 const MachineRegisterInfo &MRI,
145 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000146 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000147 if (!Ty.isValid()) {
148 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000149 return true;
150 }
151
152 const RegisterBank *PrevOpBank = nullptr;
153 for (auto &MO : I.operands()) {
154 // FIXME: Support non-register operands.
155 if (!MO.isReg()) {
156 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
157 return true;
158 }
159
160 // FIXME: Can generic operations have physical registers operands? If
161 // so, this will need to be taught about that, and we'll need to get the
162 // bank out of the minimal class for the register.
163 // Either way, this needs to be documented (and possibly verified).
164 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
165 DEBUG(dbgs() << "Generic inst has physical register operand\n");
166 return true;
167 }
168
169 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
170 if (!OpBank) {
171 DEBUG(dbgs() << "Generic register has no bank or class\n");
172 return true;
173 }
174
175 if (PrevOpBank && OpBank != PrevOpBank) {
176 DEBUG(dbgs() << "Generic inst operands have different banks\n");
177 return true;
178 }
179 PrevOpBank = OpBank;
180 }
181 return false;
182}
183
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000184/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000185/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000186/// and of size \p OpSize.
187/// \returns \p GenericOpc if the combination is unsupported.
188static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
189 unsigned OpSize) {
190 switch (RegBankID) {
191 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000192 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000194 case TargetOpcode::G_SHL:
195 return AArch64::LSLVWr;
196 case TargetOpcode::G_LSHR:
197 return AArch64::LSRVWr;
198 case TargetOpcode::G_ASHR:
199 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000200 default:
201 return GenericOpc;
202 }
Tim Northover55782222016-10-18 20:03:48 +0000203 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000204 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000205 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000206 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000207 case TargetOpcode::G_SHL:
208 return AArch64::LSLVXr;
209 case TargetOpcode::G_LSHR:
210 return AArch64::LSRVXr;
211 case TargetOpcode::G_ASHR:
212 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000213 default:
214 return GenericOpc;
215 }
216 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000217 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000218 case AArch64::FPRRegBankID:
219 switch (OpSize) {
220 case 32:
221 switch (GenericOpc) {
222 case TargetOpcode::G_FADD:
223 return AArch64::FADDSrr;
224 case TargetOpcode::G_FSUB:
225 return AArch64::FSUBSrr;
226 case TargetOpcode::G_FMUL:
227 return AArch64::FMULSrr;
228 case TargetOpcode::G_FDIV:
229 return AArch64::FDIVSrr;
230 default:
231 return GenericOpc;
232 }
233 case 64:
234 switch (GenericOpc) {
235 case TargetOpcode::G_FADD:
236 return AArch64::FADDDrr;
237 case TargetOpcode::G_FSUB:
238 return AArch64::FSUBDrr;
239 case TargetOpcode::G_FMUL:
240 return AArch64::FMULDrr;
241 case TargetOpcode::G_FDIV:
242 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000243 case TargetOpcode::G_OR:
244 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000245 default:
246 return GenericOpc;
247 }
248 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000249 break;
250 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000251 return GenericOpc;
252}
253
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000254/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
255/// appropriate for the (value) register bank \p RegBankID and of memory access
256/// size \p OpSize. This returns the variant with the base+unsigned-immediate
257/// addressing mode (e.g., LDRXui).
258/// \returns \p GenericOpc if the combination is unsupported.
259static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
260 unsigned OpSize) {
261 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
262 switch (RegBankID) {
263 case AArch64::GPRRegBankID:
264 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000265 case 8:
266 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
267 case 16:
268 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000269 case 32:
270 return isStore ? AArch64::STRWui : AArch64::LDRWui;
271 case 64:
272 return isStore ? AArch64::STRXui : AArch64::LDRXui;
273 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000274 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000275 case AArch64::FPRRegBankID:
276 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000277 case 8:
278 return isStore ? AArch64::STRBui : AArch64::LDRBui;
279 case 16:
280 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000281 case 32:
282 return isStore ? AArch64::STRSui : AArch64::LDRSui;
283 case 64:
284 return isStore ? AArch64::STRDui : AArch64::LDRDui;
285 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000286 break;
287 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000288 return GenericOpc;
289}
290
Quentin Colombetcb629a82016-10-12 03:57:49 +0000291static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
292 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
293 const RegisterBankInfo &RBI) {
294
295 unsigned DstReg = I.getOperand(0).getReg();
296 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
297 assert(I.isCopy() && "Generic operators do not allow physical registers");
298 return true;
299 }
300
301 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
302 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
303 unsigned SrcReg = I.getOperand(1).getReg();
304 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
305 (void)SrcSize;
306 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
307 "No phys reg on generic operators");
308 assert(
309 (DstSize == SrcSize ||
310 // Copies are a mean to setup initial types, the number of
311 // bits may not exactly match.
312 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
313 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
314 // Copies are a mean to copy bits around, as long as we are
315 // on the same register class, that's fine. Otherwise, that
316 // means we need some SUBREG_TO_REG or AND & co.
317 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
318 "Copy with different width?!");
319 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
320 "GPRs cannot get more than 64-bit width values");
321 const TargetRegisterClass *RC = nullptr;
322
323 if (RegBank.getID() == AArch64::FPRRegBankID) {
324 if (DstSize <= 32)
325 RC = &AArch64::FPR32RegClass;
326 else if (DstSize <= 64)
327 RC = &AArch64::FPR64RegClass;
328 else if (DstSize <= 128)
329 RC = &AArch64::FPR128RegClass;
330 else {
331 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
332 return false;
333 }
334 } else {
335 assert(RegBank.getID() == AArch64::GPRRegBankID &&
336 "Bitcast for the flags?");
337 RC =
338 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
339 }
340
341 // No need to constrain SrcReg. It will get constrained when
342 // we hit another of its use or its defs.
343 // Copies do not have constraints.
344 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
345 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
346 << " operand\n");
347 return false;
348 }
349 I.setDesc(TII.get(AArch64::COPY));
350 return true;
351}
352
Tim Northover69271c62016-10-12 22:49:11 +0000353static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
354 if (!DstTy.isScalar() || !SrcTy.isScalar())
355 return GenericOpc;
356
357 const unsigned DstSize = DstTy.getSizeInBits();
358 const unsigned SrcSize = SrcTy.getSizeInBits();
359
360 switch (DstSize) {
361 case 32:
362 switch (SrcSize) {
363 case 32:
364 switch (GenericOpc) {
365 case TargetOpcode::G_SITOFP:
366 return AArch64::SCVTFUWSri;
367 case TargetOpcode::G_UITOFP:
368 return AArch64::UCVTFUWSri;
369 case TargetOpcode::G_FPTOSI:
370 return AArch64::FCVTZSUWSr;
371 case TargetOpcode::G_FPTOUI:
372 return AArch64::FCVTZUUWSr;
373 default:
374 return GenericOpc;
375 }
376 case 64:
377 switch (GenericOpc) {
378 case TargetOpcode::G_SITOFP:
379 return AArch64::SCVTFUXSri;
380 case TargetOpcode::G_UITOFP:
381 return AArch64::UCVTFUXSri;
382 case TargetOpcode::G_FPTOSI:
383 return AArch64::FCVTZSUWDr;
384 case TargetOpcode::G_FPTOUI:
385 return AArch64::FCVTZUUWDr;
386 default:
387 return GenericOpc;
388 }
389 default:
390 return GenericOpc;
391 }
392 case 64:
393 switch (SrcSize) {
394 case 32:
395 switch (GenericOpc) {
396 case TargetOpcode::G_SITOFP:
397 return AArch64::SCVTFUWDri;
398 case TargetOpcode::G_UITOFP:
399 return AArch64::UCVTFUWDri;
400 case TargetOpcode::G_FPTOSI:
401 return AArch64::FCVTZSUXSr;
402 case TargetOpcode::G_FPTOUI:
403 return AArch64::FCVTZUUXSr;
404 default:
405 return GenericOpc;
406 }
407 case 64:
408 switch (GenericOpc) {
409 case TargetOpcode::G_SITOFP:
410 return AArch64::SCVTFUXDri;
411 case TargetOpcode::G_UITOFP:
412 return AArch64::UCVTFUXDri;
413 case TargetOpcode::G_FPTOSI:
414 return AArch64::FCVTZSUXDr;
415 case TargetOpcode::G_FPTOUI:
416 return AArch64::FCVTZUUXDr;
417 default:
418 return GenericOpc;
419 }
420 default:
421 return GenericOpc;
422 }
423 default:
424 return GenericOpc;
425 };
426 return GenericOpc;
427}
428
Tim Northover6c02ad52016-10-12 22:49:04 +0000429static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
430 switch (P) {
431 default:
432 llvm_unreachable("Unknown condition code!");
433 case CmpInst::ICMP_NE:
434 return AArch64CC::NE;
435 case CmpInst::ICMP_EQ:
436 return AArch64CC::EQ;
437 case CmpInst::ICMP_SGT:
438 return AArch64CC::GT;
439 case CmpInst::ICMP_SGE:
440 return AArch64CC::GE;
441 case CmpInst::ICMP_SLT:
442 return AArch64CC::LT;
443 case CmpInst::ICMP_SLE:
444 return AArch64CC::LE;
445 case CmpInst::ICMP_UGT:
446 return AArch64CC::HI;
447 case CmpInst::ICMP_UGE:
448 return AArch64CC::HS;
449 case CmpInst::ICMP_ULT:
450 return AArch64CC::LO;
451 case CmpInst::ICMP_ULE:
452 return AArch64CC::LS;
453 }
454}
455
Tim Northover7dd378d2016-10-12 22:49:07 +0000456static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
457 AArch64CC::CondCode &CondCode,
458 AArch64CC::CondCode &CondCode2) {
459 CondCode2 = AArch64CC::AL;
460 switch (P) {
461 default:
462 llvm_unreachable("Unknown FP condition!");
463 case CmpInst::FCMP_OEQ:
464 CondCode = AArch64CC::EQ;
465 break;
466 case CmpInst::FCMP_OGT:
467 CondCode = AArch64CC::GT;
468 break;
469 case CmpInst::FCMP_OGE:
470 CondCode = AArch64CC::GE;
471 break;
472 case CmpInst::FCMP_OLT:
473 CondCode = AArch64CC::MI;
474 break;
475 case CmpInst::FCMP_OLE:
476 CondCode = AArch64CC::LS;
477 break;
478 case CmpInst::FCMP_ONE:
479 CondCode = AArch64CC::MI;
480 CondCode2 = AArch64CC::GT;
481 break;
482 case CmpInst::FCMP_ORD:
483 CondCode = AArch64CC::VC;
484 break;
485 case CmpInst::FCMP_UNO:
486 CondCode = AArch64CC::VS;
487 break;
488 case CmpInst::FCMP_UEQ:
489 CondCode = AArch64CC::EQ;
490 CondCode2 = AArch64CC::VS;
491 break;
492 case CmpInst::FCMP_UGT:
493 CondCode = AArch64CC::HI;
494 break;
495 case CmpInst::FCMP_UGE:
496 CondCode = AArch64CC::PL;
497 break;
498 case CmpInst::FCMP_ULT:
499 CondCode = AArch64CC::LT;
500 break;
501 case CmpInst::FCMP_ULE:
502 CondCode = AArch64CC::LE;
503 break;
504 case CmpInst::FCMP_UNE:
505 CondCode = AArch64CC::NE;
506 break;
507 }
508}
509
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000510bool AArch64InstructionSelector::selectCompareBranch(
511 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
512
513 const unsigned CondReg = I.getOperand(0).getReg();
514 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
515 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
516 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
517 return false;
518
519 unsigned LHS = CCMI->getOperand(2).getReg();
520 unsigned RHS = CCMI->getOperand(3).getReg();
521 if (!getConstantVRegVal(RHS, MRI))
522 std::swap(RHS, LHS);
523
524 const auto RHSImm = getConstantVRegVal(RHS, MRI);
525 if (!RHSImm || *RHSImm != 0)
526 return false;
527
528 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
529 if (RB.getID() != AArch64::GPRRegBankID)
530 return false;
531
532 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
533 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
534 return false;
535
536 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
537 unsigned CBOpc = 0;
538 if (CmpWidth <= 32)
539 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
540 else if (CmpWidth == 64)
541 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
542 else
543 return false;
544
545 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
546 .addUse(LHS)
547 .addMBB(DestMBB);
548
549 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
550 I.eraseFromParent();
551 return true;
552}
553
Tim Northovere9600d82017-02-08 17:57:27 +0000554bool AArch64InstructionSelector::selectVaStartAAPCS(
555 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
556 return false;
557}
558
559bool AArch64InstructionSelector::selectVaStartDarwin(
560 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
561 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
562 unsigned ListReg = I.getOperand(0).getReg();
563
564 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
565
566 auto MIB =
567 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
568 .addDef(ArgsAddrReg)
569 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
570 .addImm(0)
571 .addImm(0);
572
573 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
574
575 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
576 .addUse(ArgsAddrReg)
577 .addUse(ListReg)
578 .addImm(0)
579 .addMemOperand(*I.memoperands_begin());
580
581 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
582 I.eraseFromParent();
583 return true;
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 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000761 case TargetOpcode::G_EXTRACT: {
762 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
763 // Larger extracts are vectors, same-size extracts should be something else
764 // by now (either split up or simplified to a COPY).
765 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
766 return false;
767
768 I.setDesc(TII.get(AArch64::UBFMXri));
769 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
770 Ty.getSizeInBits() - 1);
771
772 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
773 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
774 TII.get(AArch64::COPY))
775 .addDef(I.getOperand(0).getReg())
776 .addUse(DstReg, 0, AArch64::sub_32);
777 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
778 AArch64::GPR32RegClass, MRI);
779 I.getOperand(0).setReg(DstReg);
780
781 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
782 }
783
784 case TargetOpcode::G_INSERT: {
785 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
786 // Larger inserts are vectors, same-size ones should be something else by
787 // now (split up or turned into COPYs).
788 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
789 return false;
790
791 I.setDesc(TII.get(AArch64::BFMXri));
792 unsigned LSB = I.getOperand(3).getImm();
793 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
794 I.getOperand(3).setImm((64 - LSB) % 64);
795 MachineInstrBuilder(MF, I).addImm(Width - 1);
796
797 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
798 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
799 TII.get(AArch64::SUBREG_TO_REG))
800 .addDef(SrcReg)
801 .addImm(0)
802 .addUse(I.getOperand(2).getReg())
803 .addImm(AArch64::sub_32);
804 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
805 AArch64::GPR32RegClass, MRI);
806 I.getOperand(2).setReg(SrcReg);
807
808 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
809 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000810 case TargetOpcode::G_FRAME_INDEX: {
811 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000812 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000813 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000814 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000815 return false;
816 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000817 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000818
819 // MOs for a #0 shifted immediate.
820 I.addOperand(MachineOperand::CreateImm(0));
821 I.addOperand(MachineOperand::CreateImm(0));
822
823 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
824 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000825
826 case TargetOpcode::G_GLOBAL_VALUE: {
827 auto GV = I.getOperand(1).getGlobal();
828 if (GV->isThreadLocal()) {
829 // FIXME: we don't support TLS yet.
830 return false;
831 }
832 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000833 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000834 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000835 I.getOperand(1).setTargetFlags(OpFlags);
836 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000837 I.setDesc(TII.get(AArch64::MOVaddr));
838 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
839 MachineInstrBuilder MIB(MF, I);
840 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
841 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
842 }
843 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
844 }
845
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000846 case TargetOpcode::G_LOAD:
847 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000848 LLT MemTy = Ty;
849 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000850
Tim Northover5ae83502016-09-15 09:20:34 +0000851 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000852 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000853 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000854 return false;
855 }
856
Tim Northover48dfa1a2017-02-13 22:14:16 +0000857 auto &MemOp = **I.memoperands_begin();
858 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
859 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
860 return false;
861 }
862
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000863 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000864#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000865 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000866 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000867 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
868 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000869 assert(MRI.getType(PtrReg).isPointer() &&
870 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000871#endif
872
873 const unsigned ValReg = I.getOperand(0).getReg();
874 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
875
876 const unsigned NewOpc =
877 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
878 if (NewOpc == I.getOpcode())
879 return false;
880
881 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000882
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000883 uint64_t Offset = 0;
884 auto *PtrMI = MRI.getVRegDef(PtrReg);
885
886 // Try to fold a GEP into our unsigned immediate addressing mode.
887 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
888 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
889 int64_t Imm = *COff;
890 const unsigned Size = MemTy.getSizeInBits() / 8;
891 const unsigned Scale = Log2_32(Size);
892 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
893 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
894 I.getOperand(1).setReg(Ptr2Reg);
895 PtrMI = MRI.getVRegDef(Ptr2Reg);
896 Offset = Imm / Size;
897 }
898 }
899 }
900
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000901 // If we haven't folded anything into our addressing mode yet, try to fold
902 // a frame index into the base+offset.
903 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
904 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
905
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000906 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000907
908 // If we're storing a 0, use WZR/XZR.
909 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
910 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
911 if (I.getOpcode() == AArch64::STRWui)
912 I.getOperand(0).setReg(AArch64::WZR);
913 else if (I.getOpcode() == AArch64::STRXui)
914 I.getOperand(0).setReg(AArch64::XZR);
915 }
916 }
917
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000918 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
919 }
920
Tim Northover9dd78f82017-02-08 21:22:25 +0000921 case TargetOpcode::G_SMULH:
922 case TargetOpcode::G_UMULH: {
923 // Reject the various things we don't support yet.
924 if (unsupportedBinOp(I, RBI, MRI, TRI))
925 return false;
926
927 const unsigned DefReg = I.getOperand(0).getReg();
928 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
929
930 if (RB.getID() != AArch64::GPRRegBankID) {
931 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
932 return false;
933 }
934
935 if (Ty != LLT::scalar(64)) {
936 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
937 << ", expected: " << LLT::scalar(64) << '\n');
938 return false;
939 }
940
941 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
942 : AArch64::UMULHrr;
943 I.setDesc(TII.get(NewOpc));
944
945 // Now that we selected an opcode, we need to constrain the register
946 // operands to use appropriate classes.
947 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
948 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000949 case TargetOpcode::G_FADD:
950 case TargetOpcode::G_FSUB:
951 case TargetOpcode::G_FMUL:
952 case TargetOpcode::G_FDIV:
953
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000954 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000955 case TargetOpcode::G_SHL:
956 case TargetOpcode::G_LSHR:
957 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000958 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000959 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000960 if (unsupportedBinOp(I, RBI, MRI, TRI))
961 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000962
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000963 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000964
965 const unsigned DefReg = I.getOperand(0).getReg();
966 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
967
968 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
969 if (NewOpc == I.getOpcode())
970 return false;
971
972 I.setDesc(TII.get(NewOpc));
973 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000974
975 // Now that we selected an opcode, we need to constrain the register
976 // operands to use appropriate classes.
977 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
978 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000979
Tim Northover398c5f52017-02-14 20:56:29 +0000980 case TargetOpcode::G_PTR_MASK: {
981 uint64_t Align = I.getOperand(2).getImm();
982 if (Align >= 64 || Align == 0)
983 return false;
984
985 uint64_t Mask = ~((1ULL << Align) - 1);
986 I.setDesc(TII.get(AArch64::ANDXri));
987 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
988
989 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
990 }
Tim Northover037af52c2016-10-31 18:31:09 +0000991 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000992 case TargetOpcode::G_TRUNC: {
993 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
994 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
995
996 const unsigned DstReg = I.getOperand(0).getReg();
997 const unsigned SrcReg = I.getOperand(1).getReg();
998
999 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1000 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1001
1002 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001003 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001004 return false;
1005 }
1006
1007 if (DstRB.getID() == AArch64::GPRRegBankID) {
1008 const TargetRegisterClass *DstRC =
1009 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1010 if (!DstRC)
1011 return false;
1012
1013 const TargetRegisterClass *SrcRC =
1014 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1015 if (!SrcRC)
1016 return false;
1017
1018 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1019 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001020 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001021 return false;
1022 }
1023
1024 if (DstRC == SrcRC) {
1025 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001026 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1027 SrcTy == LLT::scalar(64)) {
1028 llvm_unreachable("TableGen can import this case");
1029 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001030 } else if (DstRC == &AArch64::GPR32RegClass &&
1031 SrcRC == &AArch64::GPR64RegClass) {
1032 I.getOperand(1).setSubReg(AArch64::sub_32);
1033 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001034 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001035 return false;
1036 }
1037
1038 I.setDesc(TII.get(TargetOpcode::COPY));
1039 return true;
1040 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1041 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1042 I.setDesc(TII.get(AArch64::XTNv4i16));
1043 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1044 return true;
1045 }
1046 }
1047
1048 return false;
1049 }
1050
Tim Northover3d38b3a2016-10-11 20:50:21 +00001051 case TargetOpcode::G_ANYEXT: {
1052 const unsigned DstReg = I.getOperand(0).getReg();
1053 const unsigned SrcReg = I.getOperand(1).getReg();
1054
Quentin Colombetcb629a82016-10-12 03:57:49 +00001055 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1056 if (RBDst.getID() != AArch64::GPRRegBankID) {
1057 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1058 return false;
1059 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001060
Quentin Colombetcb629a82016-10-12 03:57:49 +00001061 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1062 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1063 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001064 return false;
1065 }
1066
1067 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1068
1069 if (DstSize == 0) {
1070 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1071 return false;
1072 }
1073
Quentin Colombetcb629a82016-10-12 03:57:49 +00001074 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001075 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1076 << ", expected: 32 or 64\n");
1077 return false;
1078 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001079 // At this point G_ANYEXT is just like a plain COPY, but we need
1080 // to explicitly form the 64-bit value if any.
1081 if (DstSize > 32) {
1082 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1083 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1084 .addDef(ExtSrc)
1085 .addImm(0)
1086 .addUse(SrcReg)
1087 .addImm(AArch64::sub_32);
1088 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001089 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001090 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001091 }
1092
1093 case TargetOpcode::G_ZEXT:
1094 case TargetOpcode::G_SEXT: {
1095 unsigned Opcode = I.getOpcode();
1096 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1097 SrcTy = MRI.getType(I.getOperand(1).getReg());
1098 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1099 const unsigned DefReg = I.getOperand(0).getReg();
1100 const unsigned SrcReg = I.getOperand(1).getReg();
1101 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1102
1103 if (RB.getID() != AArch64::GPRRegBankID) {
1104 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1105 << ", expected: GPR\n");
1106 return false;
1107 }
1108
1109 MachineInstr *ExtI;
1110 if (DstTy == LLT::scalar(64)) {
1111 // FIXME: Can we avoid manually doing this?
1112 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1113 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1114 << " operand\n");
1115 return false;
1116 }
1117
1118 const unsigned SrcXReg =
1119 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1120 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1121 .addDef(SrcXReg)
1122 .addImm(0)
1123 .addUse(SrcReg)
1124 .addImm(AArch64::sub_32);
1125
1126 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1127 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1128 .addDef(DefReg)
1129 .addUse(SrcXReg)
1130 .addImm(0)
1131 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001132 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001133 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1134 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1135 .addDef(DefReg)
1136 .addUse(SrcReg)
1137 .addImm(0)
1138 .addImm(SrcTy.getSizeInBits() - 1);
1139 } else {
1140 return false;
1141 }
1142
1143 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1144
1145 I.eraseFromParent();
1146 return true;
1147 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001148
Tim Northover69271c62016-10-12 22:49:11 +00001149 case TargetOpcode::G_SITOFP:
1150 case TargetOpcode::G_UITOFP:
1151 case TargetOpcode::G_FPTOSI:
1152 case TargetOpcode::G_FPTOUI: {
1153 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1154 SrcTy = MRI.getType(I.getOperand(1).getReg());
1155 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1156 if (NewOpc == Opcode)
1157 return false;
1158
1159 I.setDesc(TII.get(NewOpc));
1160 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1161
1162 return true;
1163 }
1164
1165
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001166 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001167 case TargetOpcode::G_BITCAST:
1168 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001169
Tim Northover5f7dea82016-11-08 17:44:07 +00001170 case TargetOpcode::G_FPEXT: {
1171 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1172 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1173 << ", expected: " << LLT::scalar(64) << '\n');
1174 return false;
1175 }
1176
1177 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1178 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1179 << ", expected: " << LLT::scalar(32) << '\n');
1180 return false;
1181 }
1182
1183 const unsigned DefReg = I.getOperand(0).getReg();
1184 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1185
1186 if (RB.getID() != AArch64::FPRRegBankID) {
1187 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1188 return false;
1189 }
1190
1191 I.setDesc(TII.get(AArch64::FCVTDSr));
1192 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1193
1194 return true;
1195 }
1196
1197 case TargetOpcode::G_FPTRUNC: {
1198 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1199 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1200 << ", expected: " << LLT::scalar(32) << '\n');
1201 return false;
1202 }
1203
1204 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1205 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1206 << ", expected: " << LLT::scalar(64) << '\n');
1207 return false;
1208 }
1209
1210 const unsigned DefReg = I.getOperand(0).getReg();
1211 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1212
1213 if (RB.getID() != AArch64::FPRRegBankID) {
1214 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1215 return false;
1216 }
1217
1218 I.setDesc(TII.get(AArch64::FCVTSDr));
1219 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1220
1221 return true;
1222 }
1223
Tim Northover9ac0eba2016-11-08 00:45:29 +00001224 case TargetOpcode::G_SELECT: {
1225 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1226 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1227 << ", expected: " << LLT::scalar(1) << '\n');
1228 return false;
1229 }
1230
1231 const unsigned CondReg = I.getOperand(1).getReg();
1232 const unsigned TReg = I.getOperand(2).getReg();
1233 const unsigned FReg = I.getOperand(3).getReg();
1234
1235 unsigned CSelOpc = 0;
1236
1237 if (Ty == LLT::scalar(32)) {
1238 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001239 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001240 CSelOpc = AArch64::CSELXr;
1241 } else {
1242 return false;
1243 }
1244
1245 MachineInstr &TstMI =
1246 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1247 .addDef(AArch64::WZR)
1248 .addUse(CondReg)
1249 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1250
1251 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1252 .addDef(I.getOperand(0).getReg())
1253 .addUse(TReg)
1254 .addUse(FReg)
1255 .addImm(AArch64CC::NE);
1256
1257 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1258 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1259
1260 I.eraseFromParent();
1261 return true;
1262 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001263 case TargetOpcode::G_ICMP: {
1264 if (Ty != LLT::scalar(1)) {
1265 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1266 << ", expected: " << LLT::scalar(1) << '\n');
1267 return false;
1268 }
1269
1270 unsigned CmpOpc = 0;
1271 unsigned ZReg = 0;
1272
1273 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1274 if (CmpTy == LLT::scalar(32)) {
1275 CmpOpc = AArch64::SUBSWrr;
1276 ZReg = AArch64::WZR;
1277 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1278 CmpOpc = AArch64::SUBSXrr;
1279 ZReg = AArch64::XZR;
1280 } else {
1281 return false;
1282 }
1283
Kristof Beyls22524402017-01-05 10:16:08 +00001284 // CSINC increments the result by one when the condition code is false.
1285 // Therefore, we have to invert the predicate to get an increment by 1 when
1286 // the predicate is true.
1287 const AArch64CC::CondCode invCC =
1288 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1289 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001290
1291 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1292 .addDef(ZReg)
1293 .addUse(I.getOperand(2).getReg())
1294 .addUse(I.getOperand(3).getReg());
1295
1296 MachineInstr &CSetMI =
1297 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1298 .addDef(I.getOperand(0).getReg())
1299 .addUse(AArch64::WZR)
1300 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001301 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001302
1303 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1304 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1305
1306 I.eraseFromParent();
1307 return true;
1308 }
1309
Tim Northover7dd378d2016-10-12 22:49:07 +00001310 case TargetOpcode::G_FCMP: {
1311 if (Ty != LLT::scalar(1)) {
1312 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1313 << ", expected: " << LLT::scalar(1) << '\n');
1314 return false;
1315 }
1316
1317 unsigned CmpOpc = 0;
1318 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1319 if (CmpTy == LLT::scalar(32)) {
1320 CmpOpc = AArch64::FCMPSrr;
1321 } else if (CmpTy == LLT::scalar(64)) {
1322 CmpOpc = AArch64::FCMPDrr;
1323 } else {
1324 return false;
1325 }
1326
1327 // FIXME: regbank
1328
1329 AArch64CC::CondCode CC1, CC2;
1330 changeFCMPPredToAArch64CC(
1331 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1332
1333 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1334 .addUse(I.getOperand(2).getReg())
1335 .addUse(I.getOperand(3).getReg());
1336
1337 const unsigned DefReg = I.getOperand(0).getReg();
1338 unsigned Def1Reg = DefReg;
1339 if (CC2 != AArch64CC::AL)
1340 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1341
1342 MachineInstr &CSetMI =
1343 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1344 .addDef(Def1Reg)
1345 .addUse(AArch64::WZR)
1346 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001347 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001348
1349 if (CC2 != AArch64CC::AL) {
1350 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1351 MachineInstr &CSet2MI =
1352 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1353 .addDef(Def2Reg)
1354 .addUse(AArch64::WZR)
1355 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001356 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001357 MachineInstr &OrMI =
1358 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1359 .addDef(DefReg)
1360 .addUse(Def1Reg)
1361 .addUse(Def2Reg);
1362 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1363 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1364 }
1365
1366 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1367 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1368
1369 I.eraseFromParent();
1370 return true;
1371 }
Tim Northovere9600d82017-02-08 17:57:27 +00001372 case TargetOpcode::G_VASTART:
1373 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1374 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001375 case TargetOpcode::G_IMPLICIT_DEF:
1376 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1377 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001378 }
1379
1380 return false;
1381}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001382
1383/// SelectArithImmed - Select an immediate value that can be represented as
1384/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1385/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001386InstructionSelector::ComplexRendererFn
1387AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001388 MachineInstr &MI = *Root.getParent();
1389 MachineBasicBlock &MBB = *MI.getParent();
1390 MachineFunction &MF = *MBB.getParent();
1391 MachineRegisterInfo &MRI = MF.getRegInfo();
1392
1393 // This function is called from the addsub_shifted_imm ComplexPattern,
1394 // which lists [imm] as the list of opcode it's interested in, however
1395 // we still need to check whether the operand is actually an immediate
1396 // here because the ComplexPattern opcode list is only used in
1397 // root-level opcode matching.
1398 uint64_t Immed;
1399 if (Root.isImm())
1400 Immed = Root.getImm();
1401 else if (Root.isCImm())
1402 Immed = Root.getCImm()->getZExtValue();
1403 else if (Root.isReg()) {
1404 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1405 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sanders2deea182017-04-22 15:11:04 +00001406 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001407 MachineOperand &Op1 = Def->getOperand(1);
1408 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sanders2deea182017-04-22 15:11:04 +00001409 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001410 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001411 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001412 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001413
1414 unsigned ShiftAmt;
1415
1416 if (Immed >> 12 == 0) {
1417 ShiftAmt = 0;
1418 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1419 ShiftAmt = 12;
1420 Immed = Immed >> 12;
1421 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001422 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001423
1424 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sanders2deea182017-04-22 15:11:04 +00001425 return [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed).addImm(ShVal); };
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001426}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001427
1428namespace llvm {
1429InstructionSelector *
1430createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1431 AArch64Subtarget &Subtarget,
1432 AArch64RegisterBankInfo &RBI) {
1433 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1434}
1435}