blob: d684578d29170eb20f1c2911bd5b0959b222890b [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
Daniel Sanders0b5293f2017-04-06 09:49:34 +000040namespace {
41
Daniel Sanderse7b0d662017-04-21 15:59:56 +000042#define GET_GLOBALISEL_PREDICATE_BITSET
43#include "AArch64GenGlobalISel.inc"
44#undef GET_GLOBALISEL_PREDICATE_BITSET
45
Daniel Sanders0b5293f2017-04-06 09:49:34 +000046class AArch64InstructionSelector : public InstructionSelector {
47public:
48 AArch64InstructionSelector(const AArch64TargetMachine &TM,
49 const AArch64Subtarget &STI,
50 const AArch64RegisterBankInfo &RBI);
51
52 bool select(MachineInstr &I) const override;
53
54private:
55 /// tblgen-erated 'select' implementation, used as the initial selector for
56 /// the patterns that don't require complex C++.
57 bool selectImpl(MachineInstr &I) const;
58
59 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
60 MachineRegisterInfo &MRI) const;
61 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
62 MachineRegisterInfo &MRI) const;
63
64 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
65 MachineRegisterInfo &MRI) const;
66
Daniel Sanders2deea182017-04-22 15:11:04 +000067 ComplexRendererFn selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000068
69 const AArch64TargetMachine &TM;
70 const AArch64Subtarget &STI;
71 const AArch64InstrInfo &TII;
72 const AArch64RegisterInfo &TRI;
73 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +000074
Daniel Sanderse9fdba32017-04-29 17:30:09 +000075#define GET_GLOBALISEL_PREDICATES_DECL
76#include "AArch64GenGlobalISel.inc"
77#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +000078
79// We declare the temporaries used by selectImpl() in the class to minimize the
80// cost of constructing placeholder values.
81#define GET_GLOBALISEL_TEMPORARIES_DECL
82#include "AArch64GenGlobalISel.inc"
83#undef GET_GLOBALISEL_TEMPORARIES_DECL
84};
85
86} // end anonymous namespace
87
Daniel Sanders8a4bae92017-03-14 21:32:08 +000088#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000089#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000090#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000091
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000092AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000093 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
94 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000095 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +000096 TRI(*STI.getRegisterInfo()), RBI(RBI),
97#define GET_GLOBALISEL_PREDICATES_INIT
98#include "AArch64GenGlobalISel.inc"
99#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000100#define GET_GLOBALISEL_TEMPORARIES_INIT
101#include "AArch64GenGlobalISel.inc"
102#undef GET_GLOBALISEL_TEMPORARIES_INIT
103{
104}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000105
Tim Northoverfb8d9892016-10-12 22:49:15 +0000106// FIXME: This should be target-independent, inferred from the types declared
107// for each class in the bank.
108static const TargetRegisterClass *
109getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
110 const RegisterBankInfo &RBI) {
111 if (RB.getID() == AArch64::GPRRegBankID) {
112 if (Ty.getSizeInBits() <= 32)
113 return &AArch64::GPR32RegClass;
114 if (Ty.getSizeInBits() == 64)
115 return &AArch64::GPR64RegClass;
116 return nullptr;
117 }
118
119 if (RB.getID() == AArch64::FPRRegBankID) {
120 if (Ty.getSizeInBits() == 32)
121 return &AArch64::FPR32RegClass;
122 if (Ty.getSizeInBits() == 64)
123 return &AArch64::FPR64RegClass;
124 if (Ty.getSizeInBits() == 128)
125 return &AArch64::FPR128RegClass;
126 return nullptr;
127 }
128
129 return nullptr;
130}
131
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000132/// Check whether \p I is a currently unsupported binary operation:
133/// - it has an unsized type
134/// - an operand is not a vreg
135/// - all operands are not in the same bank
136/// These are checks that should someday live in the verifier, but right now,
137/// these are mostly limitations of the aarch64 selector.
138static bool unsupportedBinOp(const MachineInstr &I,
139 const AArch64RegisterBankInfo &RBI,
140 const MachineRegisterInfo &MRI,
141 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000142 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000143 if (!Ty.isValid()) {
144 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000145 return true;
146 }
147
148 const RegisterBank *PrevOpBank = nullptr;
149 for (auto &MO : I.operands()) {
150 // FIXME: Support non-register operands.
151 if (!MO.isReg()) {
152 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
153 return true;
154 }
155
156 // FIXME: Can generic operations have physical registers operands? If
157 // so, this will need to be taught about that, and we'll need to get the
158 // bank out of the minimal class for the register.
159 // Either way, this needs to be documented (and possibly verified).
160 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
161 DEBUG(dbgs() << "Generic inst has physical register operand\n");
162 return true;
163 }
164
165 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
166 if (!OpBank) {
167 DEBUG(dbgs() << "Generic register has no bank or class\n");
168 return true;
169 }
170
171 if (PrevOpBank && OpBank != PrevOpBank) {
172 DEBUG(dbgs() << "Generic inst operands have different banks\n");
173 return true;
174 }
175 PrevOpBank = OpBank;
176 }
177 return false;
178}
179
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000180/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000181/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000182/// and of size \p OpSize.
183/// \returns \p GenericOpc if the combination is unsupported.
184static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
185 unsigned OpSize) {
186 switch (RegBankID) {
187 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000188 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000189 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000190 case TargetOpcode::G_SHL:
191 return AArch64::LSLVWr;
192 case TargetOpcode::G_LSHR:
193 return AArch64::LSRVWr;
194 case TargetOpcode::G_ASHR:
195 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000196 default:
197 return GenericOpc;
198 }
Tim Northover55782222016-10-18 20:03:48 +0000199 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000200 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000201 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000202 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000203 case TargetOpcode::G_SHL:
204 return AArch64::LSLVXr;
205 case TargetOpcode::G_LSHR:
206 return AArch64::LSRVXr;
207 case TargetOpcode::G_ASHR:
208 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000209 default:
210 return GenericOpc;
211 }
212 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000213 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000214 case AArch64::FPRRegBankID:
215 switch (OpSize) {
216 case 32:
217 switch (GenericOpc) {
218 case TargetOpcode::G_FADD:
219 return AArch64::FADDSrr;
220 case TargetOpcode::G_FSUB:
221 return AArch64::FSUBSrr;
222 case TargetOpcode::G_FMUL:
223 return AArch64::FMULSrr;
224 case TargetOpcode::G_FDIV:
225 return AArch64::FDIVSrr;
226 default:
227 return GenericOpc;
228 }
229 case 64:
230 switch (GenericOpc) {
231 case TargetOpcode::G_FADD:
232 return AArch64::FADDDrr;
233 case TargetOpcode::G_FSUB:
234 return AArch64::FSUBDrr;
235 case TargetOpcode::G_FMUL:
236 return AArch64::FMULDrr;
237 case TargetOpcode::G_FDIV:
238 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000239 case TargetOpcode::G_OR:
240 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000241 default:
242 return GenericOpc;
243 }
244 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000245 break;
246 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000247 return GenericOpc;
248}
249
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000250/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
251/// appropriate for the (value) register bank \p RegBankID and of memory access
252/// size \p OpSize. This returns the variant with the base+unsigned-immediate
253/// addressing mode (e.g., LDRXui).
254/// \returns \p GenericOpc if the combination is unsupported.
255static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
256 unsigned OpSize) {
257 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
258 switch (RegBankID) {
259 case AArch64::GPRRegBankID:
260 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000261 case 8:
262 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
263 case 16:
264 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000265 case 32:
266 return isStore ? AArch64::STRWui : AArch64::LDRWui;
267 case 64:
268 return isStore ? AArch64::STRXui : AArch64::LDRXui;
269 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000270 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000271 case AArch64::FPRRegBankID:
272 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000273 case 8:
274 return isStore ? AArch64::STRBui : AArch64::LDRBui;
275 case 16:
276 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000277 case 32:
278 return isStore ? AArch64::STRSui : AArch64::LDRSui;
279 case 64:
280 return isStore ? AArch64::STRDui : AArch64::LDRDui;
281 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000282 break;
283 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000284 return GenericOpc;
285}
286
Quentin Colombetcb629a82016-10-12 03:57:49 +0000287static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
288 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
289 const RegisterBankInfo &RBI) {
290
291 unsigned DstReg = I.getOperand(0).getReg();
292 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
293 assert(I.isCopy() && "Generic operators do not allow physical registers");
294 return true;
295 }
296
297 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
298 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
299 unsigned SrcReg = I.getOperand(1).getReg();
300 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
301 (void)SrcSize;
302 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
303 "No phys reg on generic operators");
304 assert(
305 (DstSize == SrcSize ||
306 // Copies are a mean to setup initial types, the number of
307 // bits may not exactly match.
308 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
309 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
310 // Copies are a mean to copy bits around, as long as we are
311 // on the same register class, that's fine. Otherwise, that
312 // means we need some SUBREG_TO_REG or AND & co.
313 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
314 "Copy with different width?!");
315 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
316 "GPRs cannot get more than 64-bit width values");
317 const TargetRegisterClass *RC = nullptr;
318
319 if (RegBank.getID() == AArch64::FPRRegBankID) {
320 if (DstSize <= 32)
321 RC = &AArch64::FPR32RegClass;
322 else if (DstSize <= 64)
323 RC = &AArch64::FPR64RegClass;
324 else if (DstSize <= 128)
325 RC = &AArch64::FPR128RegClass;
326 else {
327 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
328 return false;
329 }
330 } else {
331 assert(RegBank.getID() == AArch64::GPRRegBankID &&
332 "Bitcast for the flags?");
333 RC =
334 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
335 }
336
337 // No need to constrain SrcReg. It will get constrained when
338 // we hit another of its use or its defs.
339 // Copies do not have constraints.
340 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
341 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
342 << " operand\n");
343 return false;
344 }
345 I.setDesc(TII.get(AArch64::COPY));
346 return true;
347}
348
Tim Northover69271c62016-10-12 22:49:11 +0000349static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
350 if (!DstTy.isScalar() || !SrcTy.isScalar())
351 return GenericOpc;
352
353 const unsigned DstSize = DstTy.getSizeInBits();
354 const unsigned SrcSize = SrcTy.getSizeInBits();
355
356 switch (DstSize) {
357 case 32:
358 switch (SrcSize) {
359 case 32:
360 switch (GenericOpc) {
361 case TargetOpcode::G_SITOFP:
362 return AArch64::SCVTFUWSri;
363 case TargetOpcode::G_UITOFP:
364 return AArch64::UCVTFUWSri;
365 case TargetOpcode::G_FPTOSI:
366 return AArch64::FCVTZSUWSr;
367 case TargetOpcode::G_FPTOUI:
368 return AArch64::FCVTZUUWSr;
369 default:
370 return GenericOpc;
371 }
372 case 64:
373 switch (GenericOpc) {
374 case TargetOpcode::G_SITOFP:
375 return AArch64::SCVTFUXSri;
376 case TargetOpcode::G_UITOFP:
377 return AArch64::UCVTFUXSri;
378 case TargetOpcode::G_FPTOSI:
379 return AArch64::FCVTZSUWDr;
380 case TargetOpcode::G_FPTOUI:
381 return AArch64::FCVTZUUWDr;
382 default:
383 return GenericOpc;
384 }
385 default:
386 return GenericOpc;
387 }
388 case 64:
389 switch (SrcSize) {
390 case 32:
391 switch (GenericOpc) {
392 case TargetOpcode::G_SITOFP:
393 return AArch64::SCVTFUWDri;
394 case TargetOpcode::G_UITOFP:
395 return AArch64::UCVTFUWDri;
396 case TargetOpcode::G_FPTOSI:
397 return AArch64::FCVTZSUXSr;
398 case TargetOpcode::G_FPTOUI:
399 return AArch64::FCVTZUUXSr;
400 default:
401 return GenericOpc;
402 }
403 case 64:
404 switch (GenericOpc) {
405 case TargetOpcode::G_SITOFP:
406 return AArch64::SCVTFUXDri;
407 case TargetOpcode::G_UITOFP:
408 return AArch64::UCVTFUXDri;
409 case TargetOpcode::G_FPTOSI:
410 return AArch64::FCVTZSUXDr;
411 case TargetOpcode::G_FPTOUI:
412 return AArch64::FCVTZUUXDr;
413 default:
414 return GenericOpc;
415 }
416 default:
417 return GenericOpc;
418 }
419 default:
420 return GenericOpc;
421 };
422 return GenericOpc;
423}
424
Tim Northover6c02ad52016-10-12 22:49:04 +0000425static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
426 switch (P) {
427 default:
428 llvm_unreachable("Unknown condition code!");
429 case CmpInst::ICMP_NE:
430 return AArch64CC::NE;
431 case CmpInst::ICMP_EQ:
432 return AArch64CC::EQ;
433 case CmpInst::ICMP_SGT:
434 return AArch64CC::GT;
435 case CmpInst::ICMP_SGE:
436 return AArch64CC::GE;
437 case CmpInst::ICMP_SLT:
438 return AArch64CC::LT;
439 case CmpInst::ICMP_SLE:
440 return AArch64CC::LE;
441 case CmpInst::ICMP_UGT:
442 return AArch64CC::HI;
443 case CmpInst::ICMP_UGE:
444 return AArch64CC::HS;
445 case CmpInst::ICMP_ULT:
446 return AArch64CC::LO;
447 case CmpInst::ICMP_ULE:
448 return AArch64CC::LS;
449 }
450}
451
Tim Northover7dd378d2016-10-12 22:49:07 +0000452static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
453 AArch64CC::CondCode &CondCode,
454 AArch64CC::CondCode &CondCode2) {
455 CondCode2 = AArch64CC::AL;
456 switch (P) {
457 default:
458 llvm_unreachable("Unknown FP condition!");
459 case CmpInst::FCMP_OEQ:
460 CondCode = AArch64CC::EQ;
461 break;
462 case CmpInst::FCMP_OGT:
463 CondCode = AArch64CC::GT;
464 break;
465 case CmpInst::FCMP_OGE:
466 CondCode = AArch64CC::GE;
467 break;
468 case CmpInst::FCMP_OLT:
469 CondCode = AArch64CC::MI;
470 break;
471 case CmpInst::FCMP_OLE:
472 CondCode = AArch64CC::LS;
473 break;
474 case CmpInst::FCMP_ONE:
475 CondCode = AArch64CC::MI;
476 CondCode2 = AArch64CC::GT;
477 break;
478 case CmpInst::FCMP_ORD:
479 CondCode = AArch64CC::VC;
480 break;
481 case CmpInst::FCMP_UNO:
482 CondCode = AArch64CC::VS;
483 break;
484 case CmpInst::FCMP_UEQ:
485 CondCode = AArch64CC::EQ;
486 CondCode2 = AArch64CC::VS;
487 break;
488 case CmpInst::FCMP_UGT:
489 CondCode = AArch64CC::HI;
490 break;
491 case CmpInst::FCMP_UGE:
492 CondCode = AArch64CC::PL;
493 break;
494 case CmpInst::FCMP_ULT:
495 CondCode = AArch64CC::LT;
496 break;
497 case CmpInst::FCMP_ULE:
498 CondCode = AArch64CC::LE;
499 break;
500 case CmpInst::FCMP_UNE:
501 CondCode = AArch64CC::NE;
502 break;
503 }
504}
505
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000506bool AArch64InstructionSelector::selectCompareBranch(
507 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
508
509 const unsigned CondReg = I.getOperand(0).getReg();
510 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
511 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000512 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
513 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000514 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
515 return false;
516
517 unsigned LHS = CCMI->getOperand(2).getReg();
518 unsigned RHS = CCMI->getOperand(3).getReg();
519 if (!getConstantVRegVal(RHS, MRI))
520 std::swap(RHS, LHS);
521
522 const auto RHSImm = getConstantVRegVal(RHS, MRI);
523 if (!RHSImm || *RHSImm != 0)
524 return false;
525
526 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
527 if (RB.getID() != AArch64::GPRRegBankID)
528 return false;
529
530 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
531 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
532 return false;
533
534 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
535 unsigned CBOpc = 0;
536 if (CmpWidth <= 32)
537 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
538 else if (CmpWidth == 64)
539 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
540 else
541 return false;
542
543 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
544 .addUse(LHS)
545 .addMBB(DestMBB);
546
547 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
548 I.eraseFromParent();
549 return true;
550}
551
Tim Northovere9600d82017-02-08 17:57:27 +0000552bool AArch64InstructionSelector::selectVaStartAAPCS(
553 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
554 return false;
555}
556
557bool AArch64InstructionSelector::selectVaStartDarwin(
558 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
559 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
560 unsigned ListReg = I.getOperand(0).getReg();
561
562 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
563
564 auto MIB =
565 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
566 .addDef(ArgsAddrReg)
567 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
568 .addImm(0)
569 .addImm(0);
570
571 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
572
573 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
574 .addUse(ArgsAddrReg)
575 .addUse(ListReg)
576 .addImm(0)
577 .addMemOperand(*I.memoperands_begin());
578
579 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
580 I.eraseFromParent();
581 return true;
582}
583
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000584bool AArch64InstructionSelector::select(MachineInstr &I) const {
585 assert(I.getParent() && "Instruction should be in a basic block!");
586 assert(I.getParent()->getParent() && "Instruction should be in a function!");
587
588 MachineBasicBlock &MBB = *I.getParent();
589 MachineFunction &MF = *MBB.getParent();
590 MachineRegisterInfo &MRI = MF.getRegInfo();
591
Tim Northovercdf23f12016-10-31 18:30:59 +0000592 unsigned Opcode = I.getOpcode();
593 if (!isPreISelGenericOpcode(I.getOpcode())) {
594 // Certain non-generic instructions also need some special handling.
595
596 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
597 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000598
599 if (Opcode == TargetOpcode::PHI) {
600 const unsigned DefReg = I.getOperand(0).getReg();
601 const LLT DefTy = MRI.getType(DefReg);
602
603 const TargetRegisterClass *DefRC = nullptr;
604 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
605 DefRC = TRI.getRegClass(DefReg);
606 } else {
607 const RegClassOrRegBank &RegClassOrBank =
608 MRI.getRegClassOrRegBank(DefReg);
609
610 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
611 if (!DefRC) {
612 if (!DefTy.isValid()) {
613 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
614 return false;
615 }
616 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
617 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
618 if (!DefRC) {
619 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
620 return false;
621 }
622 }
623 }
624
625 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
626 }
627
628 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000629 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000630
631 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000632 }
633
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000634
635 if (I.getNumOperands() != I.getNumExplicitOperands()) {
636 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
637 return false;
638 }
639
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000640 if (selectImpl(I))
641 return true;
642
Tim Northover32a078a2016-09-15 10:09:59 +0000643 LLT Ty =
644 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000645
Tim Northover69271c62016-10-12 22:49:11 +0000646 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000647 case TargetOpcode::G_BRCOND: {
648 if (Ty.getSizeInBits() > 32) {
649 // We shouldn't need this on AArch64, but it would be implemented as an
650 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
651 // bit being tested is < 32.
652 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
653 << ", expected at most 32-bits");
654 return false;
655 }
656
657 const unsigned CondReg = I.getOperand(0).getReg();
658 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
659
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000660 if (selectCompareBranch(I, MF, MRI))
661 return true;
662
Tim Northover5e3dbf32016-10-12 22:49:01 +0000663 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
664 .addUse(CondReg)
665 .addImm(/*bit offset=*/0)
666 .addMBB(DestMBB);
667
668 I.eraseFromParent();
669 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
670 }
671
Kristof Beyls65a12c02017-01-30 09:13:18 +0000672 case TargetOpcode::G_BRINDIRECT: {
673 I.setDesc(TII.get(AArch64::BR));
674 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
675 }
676
Tim Northover4494d692016-10-18 19:47:57 +0000677 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000678 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000679 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
680
681 const LLT s32 = LLT::scalar(32);
682 const LLT s64 = LLT::scalar(64);
683 const LLT p0 = LLT::pointer(0, 64);
684
685 const unsigned DefReg = I.getOperand(0).getReg();
686 const LLT DefTy = MRI.getType(DefReg);
687 const unsigned DefSize = DefTy.getSizeInBits();
688 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
689
690 // FIXME: Redundant check, but even less readable when factored out.
691 if (isFP) {
692 if (Ty != s32 && Ty != s64) {
693 DEBUG(dbgs() << "Unable to materialize FP " << Ty
694 << " constant, expected: " << s32 << " or " << s64
695 << '\n');
696 return false;
697 }
698
699 if (RB.getID() != AArch64::FPRRegBankID) {
700 DEBUG(dbgs() << "Unable to materialize FP " << Ty
701 << " constant on bank: " << RB << ", expected: FPR\n");
702 return false;
703 }
704 } else {
705 if (Ty != s32 && Ty != s64 && Ty != p0) {
706 DEBUG(dbgs() << "Unable to materialize integer " << Ty
707 << " constant, expected: " << s32 << ", " << s64 << ", or "
708 << p0 << '\n');
709 return false;
710 }
711
712 if (RB.getID() != AArch64::GPRRegBankID) {
713 DEBUG(dbgs() << "Unable to materialize integer " << Ty
714 << " constant on bank: " << RB << ", expected: GPR\n");
715 return false;
716 }
717 }
718
719 const unsigned MovOpc =
720 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
721
722 I.setDesc(TII.get(MovOpc));
723
724 if (isFP) {
725 const TargetRegisterClass &GPRRC =
726 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
727 const TargetRegisterClass &FPRRC =
728 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
729
730 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
731 MachineOperand &RegOp = I.getOperand(0);
732 RegOp.setReg(DefGPRReg);
733
734 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
735 TII.get(AArch64::COPY))
736 .addDef(DefReg)
737 .addUse(DefGPRReg);
738
739 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
740 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
741 return false;
742 }
743
744 MachineOperand &ImmOp = I.getOperand(1);
745 // FIXME: Is going through int64_t always correct?
746 ImmOp.ChangeToImmediate(
747 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000748 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000749 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
750 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000751 } else if (I.getOperand(1).isImm()) {
752 uint64_t Val = I.getOperand(1).getImm();
753 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000754 }
755
756 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
757 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000758 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000759 case TargetOpcode::G_EXTRACT: {
760 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
761 // Larger extracts are vectors, same-size extracts should be something else
762 // by now (either split up or simplified to a COPY).
763 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
764 return false;
765
766 I.setDesc(TII.get(AArch64::UBFMXri));
767 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
768 Ty.getSizeInBits() - 1);
769
770 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
771 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
772 TII.get(AArch64::COPY))
773 .addDef(I.getOperand(0).getReg())
774 .addUse(DstReg, 0, AArch64::sub_32);
775 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
776 AArch64::GPR32RegClass, MRI);
777 I.getOperand(0).setReg(DstReg);
778
779 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
780 }
781
782 case TargetOpcode::G_INSERT: {
783 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
784 // Larger inserts are vectors, same-size ones should be something else by
785 // now (split up or turned into COPYs).
786 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
787 return false;
788
789 I.setDesc(TII.get(AArch64::BFMXri));
790 unsigned LSB = I.getOperand(3).getImm();
791 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
792 I.getOperand(3).setImm((64 - LSB) % 64);
793 MachineInstrBuilder(MF, I).addImm(Width - 1);
794
795 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
796 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
797 TII.get(AArch64::SUBREG_TO_REG))
798 .addDef(SrcReg)
799 .addImm(0)
800 .addUse(I.getOperand(2).getReg())
801 .addImm(AArch64::sub_32);
802 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
803 AArch64::GPR32RegClass, MRI);
804 I.getOperand(2).setReg(SrcReg);
805
806 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
807 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000808 case TargetOpcode::G_FRAME_INDEX: {
809 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000810 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000811 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000812 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000813 return false;
814 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000815 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000816
817 // MOs for a #0 shifted immediate.
818 I.addOperand(MachineOperand::CreateImm(0));
819 I.addOperand(MachineOperand::CreateImm(0));
820
821 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
822 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000823
824 case TargetOpcode::G_GLOBAL_VALUE: {
825 auto GV = I.getOperand(1).getGlobal();
826 if (GV->isThreadLocal()) {
827 // FIXME: we don't support TLS yet.
828 return false;
829 }
830 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000831 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000832 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000833 I.getOperand(1).setTargetFlags(OpFlags);
834 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000835 I.setDesc(TII.get(AArch64::MOVaddr));
836 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
837 MachineInstrBuilder MIB(MF, I);
838 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
839 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
840 }
841 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
842 }
843
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000844 case TargetOpcode::G_LOAD:
845 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000846 LLT MemTy = Ty;
847 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000848
Tim Northover5ae83502016-09-15 09:20:34 +0000849 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000850 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000851 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000852 return false;
853 }
854
Tim Northover48dfa1a2017-02-13 22:14:16 +0000855 auto &MemOp = **I.memoperands_begin();
856 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
857 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
858 return false;
859 }
860
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000861 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000862#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000863 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000864 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000865 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
866 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000867 assert(MRI.getType(PtrReg).isPointer() &&
868 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000869#endif
870
871 const unsigned ValReg = I.getOperand(0).getReg();
872 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
873
874 const unsigned NewOpc =
875 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
876 if (NewOpc == I.getOpcode())
877 return false;
878
879 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000880
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000881 uint64_t Offset = 0;
882 auto *PtrMI = MRI.getVRegDef(PtrReg);
883
884 // Try to fold a GEP into our unsigned immediate addressing mode.
885 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
886 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
887 int64_t Imm = *COff;
888 const unsigned Size = MemTy.getSizeInBits() / 8;
889 const unsigned Scale = Log2_32(Size);
890 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
891 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
892 I.getOperand(1).setReg(Ptr2Reg);
893 PtrMI = MRI.getVRegDef(Ptr2Reg);
894 Offset = Imm / Size;
895 }
896 }
897 }
898
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000899 // If we haven't folded anything into our addressing mode yet, try to fold
900 // a frame index into the base+offset.
901 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
902 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
903
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000904 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000905
906 // If we're storing a 0, use WZR/XZR.
907 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
908 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
909 if (I.getOpcode() == AArch64::STRWui)
910 I.getOperand(0).setReg(AArch64::WZR);
911 else if (I.getOpcode() == AArch64::STRXui)
912 I.getOperand(0).setReg(AArch64::XZR);
913 }
914 }
915
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000916 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
917 }
918
Tim Northover9dd78f82017-02-08 21:22:25 +0000919 case TargetOpcode::G_SMULH:
920 case TargetOpcode::G_UMULH: {
921 // Reject the various things we don't support yet.
922 if (unsupportedBinOp(I, RBI, MRI, TRI))
923 return false;
924
925 const unsigned DefReg = I.getOperand(0).getReg();
926 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
927
928 if (RB.getID() != AArch64::GPRRegBankID) {
929 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
930 return false;
931 }
932
933 if (Ty != LLT::scalar(64)) {
934 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
935 << ", expected: " << LLT::scalar(64) << '\n');
936 return false;
937 }
938
939 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
940 : AArch64::UMULHrr;
941 I.setDesc(TII.get(NewOpc));
942
943 // Now that we selected an opcode, we need to constrain the register
944 // operands to use appropriate classes.
945 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
946 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000947 case TargetOpcode::G_FADD:
948 case TargetOpcode::G_FSUB:
949 case TargetOpcode::G_FMUL:
950 case TargetOpcode::G_FDIV:
951
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000952 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000953 case TargetOpcode::G_SHL:
954 case TargetOpcode::G_LSHR:
955 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000956 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000957 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000958 if (unsupportedBinOp(I, RBI, MRI, TRI))
959 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000960
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000961 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000962
963 const unsigned DefReg = I.getOperand(0).getReg();
964 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
965
966 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
967 if (NewOpc == I.getOpcode())
968 return false;
969
970 I.setDesc(TII.get(NewOpc));
971 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000972
973 // Now that we selected an opcode, we need to constrain the register
974 // operands to use appropriate classes.
975 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
976 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000977
Tim Northover398c5f52017-02-14 20:56:29 +0000978 case TargetOpcode::G_PTR_MASK: {
979 uint64_t Align = I.getOperand(2).getImm();
980 if (Align >= 64 || Align == 0)
981 return false;
982
983 uint64_t Mask = ~((1ULL << Align) - 1);
984 I.setDesc(TII.get(AArch64::ANDXri));
985 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
986
987 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
988 }
Tim Northover037af52c2016-10-31 18:31:09 +0000989 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000990 case TargetOpcode::G_TRUNC: {
991 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
992 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
993
994 const unsigned DstReg = I.getOperand(0).getReg();
995 const unsigned SrcReg = I.getOperand(1).getReg();
996
997 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
998 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
999
1000 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001001 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001002 return false;
1003 }
1004
1005 if (DstRB.getID() == AArch64::GPRRegBankID) {
1006 const TargetRegisterClass *DstRC =
1007 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1008 if (!DstRC)
1009 return false;
1010
1011 const TargetRegisterClass *SrcRC =
1012 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1013 if (!SrcRC)
1014 return false;
1015
1016 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1017 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001018 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001019 return false;
1020 }
1021
1022 if (DstRC == SrcRC) {
1023 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001024 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1025 SrcTy == LLT::scalar(64)) {
1026 llvm_unreachable("TableGen can import this case");
1027 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001028 } else if (DstRC == &AArch64::GPR32RegClass &&
1029 SrcRC == &AArch64::GPR64RegClass) {
1030 I.getOperand(1).setSubReg(AArch64::sub_32);
1031 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001032 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001033 return false;
1034 }
1035
1036 I.setDesc(TII.get(TargetOpcode::COPY));
1037 return true;
1038 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1039 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1040 I.setDesc(TII.get(AArch64::XTNv4i16));
1041 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1042 return true;
1043 }
1044 }
1045
1046 return false;
1047 }
1048
Tim Northover3d38b3a2016-10-11 20:50:21 +00001049 case TargetOpcode::G_ANYEXT: {
1050 const unsigned DstReg = I.getOperand(0).getReg();
1051 const unsigned SrcReg = I.getOperand(1).getReg();
1052
Quentin Colombetcb629a82016-10-12 03:57:49 +00001053 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1054 if (RBDst.getID() != AArch64::GPRRegBankID) {
1055 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1056 return false;
1057 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001058
Quentin Colombetcb629a82016-10-12 03:57:49 +00001059 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1060 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1061 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001062 return false;
1063 }
1064
1065 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1066
1067 if (DstSize == 0) {
1068 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1069 return false;
1070 }
1071
Quentin Colombetcb629a82016-10-12 03:57:49 +00001072 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001073 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1074 << ", expected: 32 or 64\n");
1075 return false;
1076 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001077 // At this point G_ANYEXT is just like a plain COPY, but we need
1078 // to explicitly form the 64-bit value if any.
1079 if (DstSize > 32) {
1080 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1081 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1082 .addDef(ExtSrc)
1083 .addImm(0)
1084 .addUse(SrcReg)
1085 .addImm(AArch64::sub_32);
1086 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001087 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001088 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001089 }
1090
1091 case TargetOpcode::G_ZEXT:
1092 case TargetOpcode::G_SEXT: {
1093 unsigned Opcode = I.getOpcode();
1094 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1095 SrcTy = MRI.getType(I.getOperand(1).getReg());
1096 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1097 const unsigned DefReg = I.getOperand(0).getReg();
1098 const unsigned SrcReg = I.getOperand(1).getReg();
1099 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1100
1101 if (RB.getID() != AArch64::GPRRegBankID) {
1102 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1103 << ", expected: GPR\n");
1104 return false;
1105 }
1106
1107 MachineInstr *ExtI;
1108 if (DstTy == LLT::scalar(64)) {
1109 // FIXME: Can we avoid manually doing this?
1110 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1111 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1112 << " operand\n");
1113 return false;
1114 }
1115
1116 const unsigned SrcXReg =
1117 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1118 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1119 .addDef(SrcXReg)
1120 .addImm(0)
1121 .addUse(SrcReg)
1122 .addImm(AArch64::sub_32);
1123
1124 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1125 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1126 .addDef(DefReg)
1127 .addUse(SrcXReg)
1128 .addImm(0)
1129 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001130 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001131 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1132 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1133 .addDef(DefReg)
1134 .addUse(SrcReg)
1135 .addImm(0)
1136 .addImm(SrcTy.getSizeInBits() - 1);
1137 } else {
1138 return false;
1139 }
1140
1141 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1142
1143 I.eraseFromParent();
1144 return true;
1145 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001146
Tim Northover69271c62016-10-12 22:49:11 +00001147 case TargetOpcode::G_SITOFP:
1148 case TargetOpcode::G_UITOFP:
1149 case TargetOpcode::G_FPTOSI:
1150 case TargetOpcode::G_FPTOUI: {
1151 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1152 SrcTy = MRI.getType(I.getOperand(1).getReg());
1153 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1154 if (NewOpc == Opcode)
1155 return false;
1156
1157 I.setDesc(TII.get(NewOpc));
1158 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1159
1160 return true;
1161 }
1162
1163
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001164 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001165 case TargetOpcode::G_BITCAST:
1166 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001167
Tim Northover5f7dea82016-11-08 17:44:07 +00001168 case TargetOpcode::G_FPEXT: {
1169 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1170 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1171 << ", expected: " << LLT::scalar(64) << '\n');
1172 return false;
1173 }
1174
1175 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1176 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1177 << ", expected: " << LLT::scalar(32) << '\n');
1178 return false;
1179 }
1180
1181 const unsigned DefReg = I.getOperand(0).getReg();
1182 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1183
1184 if (RB.getID() != AArch64::FPRRegBankID) {
1185 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1186 return false;
1187 }
1188
1189 I.setDesc(TII.get(AArch64::FCVTDSr));
1190 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1191
1192 return true;
1193 }
1194
1195 case TargetOpcode::G_FPTRUNC: {
1196 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1197 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1198 << ", expected: " << LLT::scalar(32) << '\n');
1199 return false;
1200 }
1201
1202 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1203 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1204 << ", expected: " << LLT::scalar(64) << '\n');
1205 return false;
1206 }
1207
1208 const unsigned DefReg = I.getOperand(0).getReg();
1209 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1210
1211 if (RB.getID() != AArch64::FPRRegBankID) {
1212 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1213 return false;
1214 }
1215
1216 I.setDesc(TII.get(AArch64::FCVTSDr));
1217 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1218
1219 return true;
1220 }
1221
Tim Northover9ac0eba2016-11-08 00:45:29 +00001222 case TargetOpcode::G_SELECT: {
1223 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1224 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1225 << ", expected: " << LLT::scalar(1) << '\n');
1226 return false;
1227 }
1228
1229 const unsigned CondReg = I.getOperand(1).getReg();
1230 const unsigned TReg = I.getOperand(2).getReg();
1231 const unsigned FReg = I.getOperand(3).getReg();
1232
1233 unsigned CSelOpc = 0;
1234
1235 if (Ty == LLT::scalar(32)) {
1236 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001237 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001238 CSelOpc = AArch64::CSELXr;
1239 } else {
1240 return false;
1241 }
1242
1243 MachineInstr &TstMI =
1244 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1245 .addDef(AArch64::WZR)
1246 .addUse(CondReg)
1247 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1248
1249 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1250 .addDef(I.getOperand(0).getReg())
1251 .addUse(TReg)
1252 .addUse(FReg)
1253 .addImm(AArch64CC::NE);
1254
1255 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1256 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1257
1258 I.eraseFromParent();
1259 return true;
1260 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001261 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001262 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001263 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001264 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001265 return false;
1266 }
1267
1268 unsigned CmpOpc = 0;
1269 unsigned ZReg = 0;
1270
1271 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1272 if (CmpTy == LLT::scalar(32)) {
1273 CmpOpc = AArch64::SUBSWrr;
1274 ZReg = AArch64::WZR;
1275 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1276 CmpOpc = AArch64::SUBSXrr;
1277 ZReg = AArch64::XZR;
1278 } else {
1279 return false;
1280 }
1281
Kristof Beyls22524402017-01-05 10:16:08 +00001282 // CSINC increments the result by one when the condition code is false.
1283 // Therefore, we have to invert the predicate to get an increment by 1 when
1284 // the predicate is true.
1285 const AArch64CC::CondCode invCC =
1286 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1287 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001288
1289 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1290 .addDef(ZReg)
1291 .addUse(I.getOperand(2).getReg())
1292 .addUse(I.getOperand(3).getReg());
1293
1294 MachineInstr &CSetMI =
1295 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1296 .addDef(I.getOperand(0).getReg())
1297 .addUse(AArch64::WZR)
1298 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001299 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001300
1301 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1302 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1303
1304 I.eraseFromParent();
1305 return true;
1306 }
1307
Tim Northover7dd378d2016-10-12 22:49:07 +00001308 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001309 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001310 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001311 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001312 return false;
1313 }
1314
1315 unsigned CmpOpc = 0;
1316 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1317 if (CmpTy == LLT::scalar(32)) {
1318 CmpOpc = AArch64::FCMPSrr;
1319 } else if (CmpTy == LLT::scalar(64)) {
1320 CmpOpc = AArch64::FCMPDrr;
1321 } else {
1322 return false;
1323 }
1324
1325 // FIXME: regbank
1326
1327 AArch64CC::CondCode CC1, CC2;
1328 changeFCMPPredToAArch64CC(
1329 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1330
1331 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1332 .addUse(I.getOperand(2).getReg())
1333 .addUse(I.getOperand(3).getReg());
1334
1335 const unsigned DefReg = I.getOperand(0).getReg();
1336 unsigned Def1Reg = DefReg;
1337 if (CC2 != AArch64CC::AL)
1338 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1339
1340 MachineInstr &CSetMI =
1341 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1342 .addDef(Def1Reg)
1343 .addUse(AArch64::WZR)
1344 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001345 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001346
1347 if (CC2 != AArch64CC::AL) {
1348 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1349 MachineInstr &CSet2MI =
1350 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1351 .addDef(Def2Reg)
1352 .addUse(AArch64::WZR)
1353 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001354 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001355 MachineInstr &OrMI =
1356 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1357 .addDef(DefReg)
1358 .addUse(Def1Reg)
1359 .addUse(Def2Reg);
1360 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1361 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1362 }
1363
1364 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1365 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1366
1367 I.eraseFromParent();
1368 return true;
1369 }
Tim Northovere9600d82017-02-08 17:57:27 +00001370 case TargetOpcode::G_VASTART:
1371 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1372 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001373 case TargetOpcode::G_IMPLICIT_DEF:
1374 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1375 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001376 }
1377
1378 return false;
1379}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001380
1381/// SelectArithImmed - Select an immediate value that can be represented as
1382/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1383/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001384InstructionSelector::ComplexRendererFn
1385AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001386 MachineInstr &MI = *Root.getParent();
1387 MachineBasicBlock &MBB = *MI.getParent();
1388 MachineFunction &MF = *MBB.getParent();
1389 MachineRegisterInfo &MRI = MF.getRegInfo();
1390
1391 // This function is called from the addsub_shifted_imm ComplexPattern,
1392 // which lists [imm] as the list of opcode it's interested in, however
1393 // we still need to check whether the operand is actually an immediate
1394 // here because the ComplexPattern opcode list is only used in
1395 // root-level opcode matching.
1396 uint64_t Immed;
1397 if (Root.isImm())
1398 Immed = Root.getImm();
1399 else if (Root.isCImm())
1400 Immed = Root.getCImm()->getZExtValue();
1401 else if (Root.isReg()) {
1402 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1403 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sanders2deea182017-04-22 15:11:04 +00001404 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001405 MachineOperand &Op1 = Def->getOperand(1);
1406 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sanders2deea182017-04-22 15:11:04 +00001407 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001408 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001409 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001410 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001411
1412 unsigned ShiftAmt;
1413
1414 if (Immed >> 12 == 0) {
1415 ShiftAmt = 0;
1416 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1417 ShiftAmt = 12;
1418 Immed = Immed >> 12;
1419 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001420 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001421
1422 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sanders2deea182017-04-22 15:11:04 +00001423 return [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed).addImm(ShVal); };
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001424}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001425
1426namespace llvm {
1427InstructionSelector *
1428createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1429 AArch64Subtarget &Subtarget,
1430 AArch64RegisterBankInfo &RBI) {
1431 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1432}
1433}