blob: ca93d1feaa61a546d0cd5e6053354989a2cb96ec [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) {
Ahmed Bougachaa7aa2a92017-09-12 21:04:10 +0000320 if (DstSize <= 16)
321 RC = &AArch64::FPR16RegClass;
322 else if (DstSize <= 32)
Quentin Colombetcb629a82016-10-12 03:57:49 +0000323 RC = &AArch64::FPR32RegClass;
324 else if (DstSize <= 64)
325 RC = &AArch64::FPR64RegClass;
326 else if (DstSize <= 128)
327 RC = &AArch64::FPR128RegClass;
328 else {
329 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
330 return false;
331 }
332 } else {
333 assert(RegBank.getID() == AArch64::GPRRegBankID &&
334 "Bitcast for the flags?");
335 RC =
336 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
337 }
338
339 // No need to constrain SrcReg. It will get constrained when
340 // we hit another of its use or its defs.
341 // Copies do not have constraints.
342 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
343 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
344 << " operand\n");
345 return false;
346 }
347 I.setDesc(TII.get(AArch64::COPY));
348 return true;
349}
350
Tim Northover69271c62016-10-12 22:49:11 +0000351static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
352 if (!DstTy.isScalar() || !SrcTy.isScalar())
353 return GenericOpc;
354
355 const unsigned DstSize = DstTy.getSizeInBits();
356 const unsigned SrcSize = SrcTy.getSizeInBits();
357
358 switch (DstSize) {
359 case 32:
360 switch (SrcSize) {
361 case 32:
362 switch (GenericOpc) {
363 case TargetOpcode::G_SITOFP:
364 return AArch64::SCVTFUWSri;
365 case TargetOpcode::G_UITOFP:
366 return AArch64::UCVTFUWSri;
367 case TargetOpcode::G_FPTOSI:
368 return AArch64::FCVTZSUWSr;
369 case TargetOpcode::G_FPTOUI:
370 return AArch64::FCVTZUUWSr;
371 default:
372 return GenericOpc;
373 }
374 case 64:
375 switch (GenericOpc) {
376 case TargetOpcode::G_SITOFP:
377 return AArch64::SCVTFUXSri;
378 case TargetOpcode::G_UITOFP:
379 return AArch64::UCVTFUXSri;
380 case TargetOpcode::G_FPTOSI:
381 return AArch64::FCVTZSUWDr;
382 case TargetOpcode::G_FPTOUI:
383 return AArch64::FCVTZUUWDr;
384 default:
385 return GenericOpc;
386 }
387 default:
388 return GenericOpc;
389 }
390 case 64:
391 switch (SrcSize) {
392 case 32:
393 switch (GenericOpc) {
394 case TargetOpcode::G_SITOFP:
395 return AArch64::SCVTFUWDri;
396 case TargetOpcode::G_UITOFP:
397 return AArch64::UCVTFUWDri;
398 case TargetOpcode::G_FPTOSI:
399 return AArch64::FCVTZSUXSr;
400 case TargetOpcode::G_FPTOUI:
401 return AArch64::FCVTZUUXSr;
402 default:
403 return GenericOpc;
404 }
405 case 64:
406 switch (GenericOpc) {
407 case TargetOpcode::G_SITOFP:
408 return AArch64::SCVTFUXDri;
409 case TargetOpcode::G_UITOFP:
410 return AArch64::UCVTFUXDri;
411 case TargetOpcode::G_FPTOSI:
412 return AArch64::FCVTZSUXDr;
413 case TargetOpcode::G_FPTOUI:
414 return AArch64::FCVTZUUXDr;
415 default:
416 return GenericOpc;
417 }
418 default:
419 return GenericOpc;
420 }
421 default:
422 return GenericOpc;
423 };
424 return GenericOpc;
425}
426
Tim Northover6c02ad52016-10-12 22:49:04 +0000427static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
428 switch (P) {
429 default:
430 llvm_unreachable("Unknown condition code!");
431 case CmpInst::ICMP_NE:
432 return AArch64CC::NE;
433 case CmpInst::ICMP_EQ:
434 return AArch64CC::EQ;
435 case CmpInst::ICMP_SGT:
436 return AArch64CC::GT;
437 case CmpInst::ICMP_SGE:
438 return AArch64CC::GE;
439 case CmpInst::ICMP_SLT:
440 return AArch64CC::LT;
441 case CmpInst::ICMP_SLE:
442 return AArch64CC::LE;
443 case CmpInst::ICMP_UGT:
444 return AArch64CC::HI;
445 case CmpInst::ICMP_UGE:
446 return AArch64CC::HS;
447 case CmpInst::ICMP_ULT:
448 return AArch64CC::LO;
449 case CmpInst::ICMP_ULE:
450 return AArch64CC::LS;
451 }
452}
453
Tim Northover7dd378d2016-10-12 22:49:07 +0000454static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
455 AArch64CC::CondCode &CondCode,
456 AArch64CC::CondCode &CondCode2) {
457 CondCode2 = AArch64CC::AL;
458 switch (P) {
459 default:
460 llvm_unreachable("Unknown FP condition!");
461 case CmpInst::FCMP_OEQ:
462 CondCode = AArch64CC::EQ;
463 break;
464 case CmpInst::FCMP_OGT:
465 CondCode = AArch64CC::GT;
466 break;
467 case CmpInst::FCMP_OGE:
468 CondCode = AArch64CC::GE;
469 break;
470 case CmpInst::FCMP_OLT:
471 CondCode = AArch64CC::MI;
472 break;
473 case CmpInst::FCMP_OLE:
474 CondCode = AArch64CC::LS;
475 break;
476 case CmpInst::FCMP_ONE:
477 CondCode = AArch64CC::MI;
478 CondCode2 = AArch64CC::GT;
479 break;
480 case CmpInst::FCMP_ORD:
481 CondCode = AArch64CC::VC;
482 break;
483 case CmpInst::FCMP_UNO:
484 CondCode = AArch64CC::VS;
485 break;
486 case CmpInst::FCMP_UEQ:
487 CondCode = AArch64CC::EQ;
488 CondCode2 = AArch64CC::VS;
489 break;
490 case CmpInst::FCMP_UGT:
491 CondCode = AArch64CC::HI;
492 break;
493 case CmpInst::FCMP_UGE:
494 CondCode = AArch64CC::PL;
495 break;
496 case CmpInst::FCMP_ULT:
497 CondCode = AArch64CC::LT;
498 break;
499 case CmpInst::FCMP_ULE:
500 CondCode = AArch64CC::LE;
501 break;
502 case CmpInst::FCMP_UNE:
503 CondCode = AArch64CC::NE;
504 break;
505 }
506}
507
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000508bool AArch64InstructionSelector::selectCompareBranch(
509 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
510
511 const unsigned CondReg = I.getOperand(0).getReg();
512 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
513 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000514 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
515 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000516 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();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000595 // G_PHI requires same handling as PHI
596 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000597 // Certain non-generic instructions also need some special handling.
598
599 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
600 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000601
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000602 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000603 const unsigned DefReg = I.getOperand(0).getReg();
604 const LLT DefTy = MRI.getType(DefReg);
605
606 const TargetRegisterClass *DefRC = nullptr;
607 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
608 DefRC = TRI.getRegClass(DefReg);
609 } else {
610 const RegClassOrRegBank &RegClassOrBank =
611 MRI.getRegClassOrRegBank(DefReg);
612
613 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
614 if (!DefRC) {
615 if (!DefTy.isValid()) {
616 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
617 return false;
618 }
619 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
620 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
621 if (!DefRC) {
622 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
623 return false;
624 }
625 }
626 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000627 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000628
629 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
630 }
631
632 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000633 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000634
635 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000636 }
637
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000638
639 if (I.getNumOperands() != I.getNumExplicitOperands()) {
640 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
641 return false;
642 }
643
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000644 if (selectImpl(I))
645 return true;
646
Tim Northover32a078a2016-09-15 10:09:59 +0000647 LLT Ty =
648 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000649
Tim Northover69271c62016-10-12 22:49:11 +0000650 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000651 case TargetOpcode::G_BRCOND: {
652 if (Ty.getSizeInBits() > 32) {
653 // We shouldn't need this on AArch64, but it would be implemented as an
654 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
655 // bit being tested is < 32.
656 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
657 << ", expected at most 32-bits");
658 return false;
659 }
660
661 const unsigned CondReg = I.getOperand(0).getReg();
662 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
663
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000664 if (selectCompareBranch(I, MF, MRI))
665 return true;
666
Tim Northover5e3dbf32016-10-12 22:49:01 +0000667 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
668 .addUse(CondReg)
669 .addImm(/*bit offset=*/0)
670 .addMBB(DestMBB);
671
672 I.eraseFromParent();
673 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
674 }
675
Kristof Beyls65a12c02017-01-30 09:13:18 +0000676 case TargetOpcode::G_BRINDIRECT: {
677 I.setDesc(TII.get(AArch64::BR));
678 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
679 }
680
Tim Northover4494d692016-10-18 19:47:57 +0000681 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000682 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000683 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
684
685 const LLT s32 = LLT::scalar(32);
686 const LLT s64 = LLT::scalar(64);
687 const LLT p0 = LLT::pointer(0, 64);
688
689 const unsigned DefReg = I.getOperand(0).getReg();
690 const LLT DefTy = MRI.getType(DefReg);
691 const unsigned DefSize = DefTy.getSizeInBits();
692 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
693
694 // FIXME: Redundant check, but even less readable when factored out.
695 if (isFP) {
696 if (Ty != s32 && Ty != s64) {
697 DEBUG(dbgs() << "Unable to materialize FP " << Ty
698 << " constant, expected: " << s32 << " or " << s64
699 << '\n');
700 return false;
701 }
702
703 if (RB.getID() != AArch64::FPRRegBankID) {
704 DEBUG(dbgs() << "Unable to materialize FP " << Ty
705 << " constant on bank: " << RB << ", expected: FPR\n");
706 return false;
707 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000708
709 // The case when we have 0.0 is covered by tablegen. Reject it here so we
710 // can be sure tablegen works correctly and isn't rescued by this code.
711 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
712 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000713 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000714 // s32 and s64 are covered by tablegen.
715 if (Ty != p0) {
Tim Northover4494d692016-10-18 19:47:57 +0000716 DEBUG(dbgs() << "Unable to materialize integer " << Ty
717 << " constant, expected: " << s32 << ", " << s64 << ", or "
718 << p0 << '\n');
719 return false;
720 }
721
722 if (RB.getID() != AArch64::GPRRegBankID) {
723 DEBUG(dbgs() << "Unable to materialize integer " << Ty
724 << " constant on bank: " << RB << ", expected: GPR\n");
725 return false;
726 }
727 }
728
729 const unsigned MovOpc =
730 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
731
732 I.setDesc(TII.get(MovOpc));
733
734 if (isFP) {
735 const TargetRegisterClass &GPRRC =
736 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
737 const TargetRegisterClass &FPRRC =
738 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
739
740 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
741 MachineOperand &RegOp = I.getOperand(0);
742 RegOp.setReg(DefGPRReg);
743
744 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
745 TII.get(AArch64::COPY))
746 .addDef(DefReg)
747 .addUse(DefGPRReg);
748
749 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
750 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
751 return false;
752 }
753
754 MachineOperand &ImmOp = I.getOperand(1);
755 // FIXME: Is going through int64_t always correct?
756 ImmOp.ChangeToImmediate(
757 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000758 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000759 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
760 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000761 } else if (I.getOperand(1).isImm()) {
762 uint64_t Val = I.getOperand(1).getImm();
763 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000764 }
765
766 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
767 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000768 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000769 case TargetOpcode::G_EXTRACT: {
770 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
771 // Larger extracts are vectors, same-size extracts should be something else
772 // by now (either split up or simplified to a COPY).
773 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
774 return false;
775
776 I.setDesc(TII.get(AArch64::UBFMXri));
777 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
778 Ty.getSizeInBits() - 1);
779
780 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
781 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
782 TII.get(AArch64::COPY))
783 .addDef(I.getOperand(0).getReg())
784 .addUse(DstReg, 0, AArch64::sub_32);
785 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
786 AArch64::GPR32RegClass, MRI);
787 I.getOperand(0).setReg(DstReg);
788
789 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
790 }
791
792 case TargetOpcode::G_INSERT: {
793 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
794 // Larger inserts are vectors, same-size ones should be something else by
795 // now (split up or turned into COPYs).
796 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
797 return false;
798
799 I.setDesc(TII.get(AArch64::BFMXri));
800 unsigned LSB = I.getOperand(3).getImm();
801 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
802 I.getOperand(3).setImm((64 - LSB) % 64);
803 MachineInstrBuilder(MF, I).addImm(Width - 1);
804
805 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
806 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
807 TII.get(AArch64::SUBREG_TO_REG))
808 .addDef(SrcReg)
809 .addImm(0)
810 .addUse(I.getOperand(2).getReg())
811 .addImm(AArch64::sub_32);
812 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
813 AArch64::GPR32RegClass, MRI);
814 I.getOperand(2).setReg(SrcReg);
815
816 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
817 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000818 case TargetOpcode::G_FRAME_INDEX: {
819 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000820 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000821 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000822 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000823 return false;
824 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000825 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000826
827 // MOs for a #0 shifted immediate.
828 I.addOperand(MachineOperand::CreateImm(0));
829 I.addOperand(MachineOperand::CreateImm(0));
830
831 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
832 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000833
834 case TargetOpcode::G_GLOBAL_VALUE: {
835 auto GV = I.getOperand(1).getGlobal();
836 if (GV->isThreadLocal()) {
837 // FIXME: we don't support TLS yet.
838 return false;
839 }
840 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000841 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000842 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000843 I.getOperand(1).setTargetFlags(OpFlags);
844 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000845 I.setDesc(TII.get(AArch64::MOVaddr));
846 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
847 MachineInstrBuilder MIB(MF, I);
848 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
849 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
850 }
851 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
852 }
853
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000854 case TargetOpcode::G_LOAD:
855 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000856 LLT MemTy = Ty;
857 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000858
Tim Northover5ae83502016-09-15 09:20:34 +0000859 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000860 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000861 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000862 return false;
863 }
864
Tim Northover48dfa1a2017-02-13 22:14:16 +0000865 auto &MemOp = **I.memoperands_begin();
866 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
867 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
868 return false;
869 }
870
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000871 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000872#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000873 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000874 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000875 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
876 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000877 assert(MRI.getType(PtrReg).isPointer() &&
878 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000879#endif
880
881 const unsigned ValReg = I.getOperand(0).getReg();
882 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
883
884 const unsigned NewOpc =
885 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
886 if (NewOpc == I.getOpcode())
887 return false;
888
889 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000890
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000891 uint64_t Offset = 0;
892 auto *PtrMI = MRI.getVRegDef(PtrReg);
893
894 // Try to fold a GEP into our unsigned immediate addressing mode.
895 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
896 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
897 int64_t Imm = *COff;
898 const unsigned Size = MemTy.getSizeInBits() / 8;
899 const unsigned Scale = Log2_32(Size);
900 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
901 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
902 I.getOperand(1).setReg(Ptr2Reg);
903 PtrMI = MRI.getVRegDef(Ptr2Reg);
904 Offset = Imm / Size;
905 }
906 }
907 }
908
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000909 // If we haven't folded anything into our addressing mode yet, try to fold
910 // a frame index into the base+offset.
911 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
912 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
913
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000914 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000915
916 // If we're storing a 0, use WZR/XZR.
917 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
918 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
919 if (I.getOpcode() == AArch64::STRWui)
920 I.getOperand(0).setReg(AArch64::WZR);
921 else if (I.getOpcode() == AArch64::STRXui)
922 I.getOperand(0).setReg(AArch64::XZR);
923 }
924 }
925
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000926 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
927 }
928
Tim Northover9dd78f82017-02-08 21:22:25 +0000929 case TargetOpcode::G_SMULH:
930 case TargetOpcode::G_UMULH: {
931 // Reject the various things we don't support yet.
932 if (unsupportedBinOp(I, RBI, MRI, TRI))
933 return false;
934
935 const unsigned DefReg = I.getOperand(0).getReg();
936 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
937
938 if (RB.getID() != AArch64::GPRRegBankID) {
939 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
940 return false;
941 }
942
943 if (Ty != LLT::scalar(64)) {
944 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
945 << ", expected: " << LLT::scalar(64) << '\n');
946 return false;
947 }
948
949 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
950 : AArch64::UMULHrr;
951 I.setDesc(TII.get(NewOpc));
952
953 // Now that we selected an opcode, we need to constrain the register
954 // operands to use appropriate classes.
955 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
956 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000957 case TargetOpcode::G_FADD:
958 case TargetOpcode::G_FSUB:
959 case TargetOpcode::G_FMUL:
960 case TargetOpcode::G_FDIV:
961
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000962 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000963 case TargetOpcode::G_SHL:
964 case TargetOpcode::G_LSHR:
965 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000966 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000967 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000968 if (unsupportedBinOp(I, RBI, MRI, TRI))
969 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000970
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000971 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000972
973 const unsigned DefReg = I.getOperand(0).getReg();
974 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
975
976 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
977 if (NewOpc == I.getOpcode())
978 return false;
979
980 I.setDesc(TII.get(NewOpc));
981 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000982
983 // Now that we selected an opcode, we need to constrain the register
984 // operands to use appropriate classes.
985 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
986 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000987
Tim Northover398c5f52017-02-14 20:56:29 +0000988 case TargetOpcode::G_PTR_MASK: {
989 uint64_t Align = I.getOperand(2).getImm();
990 if (Align >= 64 || Align == 0)
991 return false;
992
993 uint64_t Mask = ~((1ULL << Align) - 1);
994 I.setDesc(TII.get(AArch64::ANDXri));
995 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
996
997 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
998 }
Tim Northover037af52c2016-10-31 18:31:09 +0000999 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001000 case TargetOpcode::G_TRUNC: {
1001 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1002 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1003
1004 const unsigned DstReg = I.getOperand(0).getReg();
1005 const unsigned SrcReg = I.getOperand(1).getReg();
1006
1007 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1008 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1009
1010 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001011 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001012 return false;
1013 }
1014
1015 if (DstRB.getID() == AArch64::GPRRegBankID) {
1016 const TargetRegisterClass *DstRC =
1017 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1018 if (!DstRC)
1019 return false;
1020
1021 const TargetRegisterClass *SrcRC =
1022 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1023 if (!SrcRC)
1024 return false;
1025
1026 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1027 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001028 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001029 return false;
1030 }
1031
1032 if (DstRC == SrcRC) {
1033 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001034 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1035 SrcTy == LLT::scalar(64)) {
1036 llvm_unreachable("TableGen can import this case");
1037 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001038 } else if (DstRC == &AArch64::GPR32RegClass &&
1039 SrcRC == &AArch64::GPR64RegClass) {
1040 I.getOperand(1).setSubReg(AArch64::sub_32);
1041 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001042 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001043 return false;
1044 }
1045
1046 I.setDesc(TII.get(TargetOpcode::COPY));
1047 return true;
1048 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1049 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1050 I.setDesc(TII.get(AArch64::XTNv4i16));
1051 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1052 return true;
1053 }
1054 }
1055
1056 return false;
1057 }
1058
Tim Northover3d38b3a2016-10-11 20:50:21 +00001059 case TargetOpcode::G_ANYEXT: {
1060 const unsigned DstReg = I.getOperand(0).getReg();
1061 const unsigned SrcReg = I.getOperand(1).getReg();
1062
Quentin Colombetcb629a82016-10-12 03:57:49 +00001063 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1064 if (RBDst.getID() != AArch64::GPRRegBankID) {
1065 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1066 return false;
1067 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001068
Quentin Colombetcb629a82016-10-12 03:57:49 +00001069 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1070 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1071 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001072 return false;
1073 }
1074
1075 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1076
1077 if (DstSize == 0) {
1078 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1079 return false;
1080 }
1081
Quentin Colombetcb629a82016-10-12 03:57:49 +00001082 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001083 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1084 << ", expected: 32 or 64\n");
1085 return false;
1086 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001087 // At this point G_ANYEXT is just like a plain COPY, but we need
1088 // to explicitly form the 64-bit value if any.
1089 if (DstSize > 32) {
1090 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1091 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1092 .addDef(ExtSrc)
1093 .addImm(0)
1094 .addUse(SrcReg)
1095 .addImm(AArch64::sub_32);
1096 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001097 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001098 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001099 }
1100
1101 case TargetOpcode::G_ZEXT:
1102 case TargetOpcode::G_SEXT: {
1103 unsigned Opcode = I.getOpcode();
1104 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1105 SrcTy = MRI.getType(I.getOperand(1).getReg());
1106 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1107 const unsigned DefReg = I.getOperand(0).getReg();
1108 const unsigned SrcReg = I.getOperand(1).getReg();
1109 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1110
1111 if (RB.getID() != AArch64::GPRRegBankID) {
1112 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1113 << ", expected: GPR\n");
1114 return false;
1115 }
1116
1117 MachineInstr *ExtI;
1118 if (DstTy == LLT::scalar(64)) {
1119 // FIXME: Can we avoid manually doing this?
1120 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1121 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1122 << " operand\n");
1123 return false;
1124 }
1125
1126 const unsigned SrcXReg =
1127 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1128 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1129 .addDef(SrcXReg)
1130 .addImm(0)
1131 .addUse(SrcReg)
1132 .addImm(AArch64::sub_32);
1133
1134 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1135 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1136 .addDef(DefReg)
1137 .addUse(SrcXReg)
1138 .addImm(0)
1139 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001140 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001141 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1142 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1143 .addDef(DefReg)
1144 .addUse(SrcReg)
1145 .addImm(0)
1146 .addImm(SrcTy.getSizeInBits() - 1);
1147 } else {
1148 return false;
1149 }
1150
1151 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1152
1153 I.eraseFromParent();
1154 return true;
1155 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001156
Tim Northover69271c62016-10-12 22:49:11 +00001157 case TargetOpcode::G_SITOFP:
1158 case TargetOpcode::G_UITOFP:
1159 case TargetOpcode::G_FPTOSI:
1160 case TargetOpcode::G_FPTOUI: {
1161 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1162 SrcTy = MRI.getType(I.getOperand(1).getReg());
1163 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1164 if (NewOpc == Opcode)
1165 return false;
1166
1167 I.setDesc(TII.get(NewOpc));
1168 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1169
1170 return true;
1171 }
1172
1173
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001174 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001175 // The importer is currently unable to import pointer types since they
1176 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001177 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001178
Daniel Sandersedd07842017-08-17 09:26:14 +00001179 case TargetOpcode::G_BITCAST:
1180 // Imported SelectionDAG rules can handle every bitcast except those that
1181 // bitcast from a type to the same type. Ideally, these shouldn't occur
1182 // but we might not run an optimizer that deletes them.
1183 if (MRI.getType(I.getOperand(0).getReg()) ==
1184 MRI.getType(I.getOperand(1).getReg()))
1185 return selectCopy(I, TII, MRI, TRI, RBI);
1186 return false;
1187
Tim Northover9ac0eba2016-11-08 00:45:29 +00001188 case TargetOpcode::G_SELECT: {
1189 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1190 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1191 << ", expected: " << LLT::scalar(1) << '\n');
1192 return false;
1193 }
1194
1195 const unsigned CondReg = I.getOperand(1).getReg();
1196 const unsigned TReg = I.getOperand(2).getReg();
1197 const unsigned FReg = I.getOperand(3).getReg();
1198
1199 unsigned CSelOpc = 0;
1200
1201 if (Ty == LLT::scalar(32)) {
1202 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001203 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001204 CSelOpc = AArch64::CSELXr;
1205 } else {
1206 return false;
1207 }
1208
1209 MachineInstr &TstMI =
1210 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1211 .addDef(AArch64::WZR)
1212 .addUse(CondReg)
1213 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1214
1215 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1216 .addDef(I.getOperand(0).getReg())
1217 .addUse(TReg)
1218 .addUse(FReg)
1219 .addImm(AArch64CC::NE);
1220
1221 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1222 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1223
1224 I.eraseFromParent();
1225 return true;
1226 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001227 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001228 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001229 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001230 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001231 return false;
1232 }
1233
1234 unsigned CmpOpc = 0;
1235 unsigned ZReg = 0;
1236
1237 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1238 if (CmpTy == LLT::scalar(32)) {
1239 CmpOpc = AArch64::SUBSWrr;
1240 ZReg = AArch64::WZR;
1241 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1242 CmpOpc = AArch64::SUBSXrr;
1243 ZReg = AArch64::XZR;
1244 } else {
1245 return false;
1246 }
1247
Kristof Beyls22524402017-01-05 10:16:08 +00001248 // CSINC increments the result by one when the condition code is false.
1249 // Therefore, we have to invert the predicate to get an increment by 1 when
1250 // the predicate is true.
1251 const AArch64CC::CondCode invCC =
1252 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1253 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001254
1255 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1256 .addDef(ZReg)
1257 .addUse(I.getOperand(2).getReg())
1258 .addUse(I.getOperand(3).getReg());
1259
1260 MachineInstr &CSetMI =
1261 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1262 .addDef(I.getOperand(0).getReg())
1263 .addUse(AArch64::WZR)
1264 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001265 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001266
1267 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1268 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1269
1270 I.eraseFromParent();
1271 return true;
1272 }
1273
Tim Northover7dd378d2016-10-12 22:49:07 +00001274 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001275 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001276 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001277 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001278 return false;
1279 }
1280
1281 unsigned CmpOpc = 0;
1282 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1283 if (CmpTy == LLT::scalar(32)) {
1284 CmpOpc = AArch64::FCMPSrr;
1285 } else if (CmpTy == LLT::scalar(64)) {
1286 CmpOpc = AArch64::FCMPDrr;
1287 } else {
1288 return false;
1289 }
1290
1291 // FIXME: regbank
1292
1293 AArch64CC::CondCode CC1, CC2;
1294 changeFCMPPredToAArch64CC(
1295 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1296
1297 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1298 .addUse(I.getOperand(2).getReg())
1299 .addUse(I.getOperand(3).getReg());
1300
1301 const unsigned DefReg = I.getOperand(0).getReg();
1302 unsigned Def1Reg = DefReg;
1303 if (CC2 != AArch64CC::AL)
1304 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1305
1306 MachineInstr &CSetMI =
1307 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1308 .addDef(Def1Reg)
1309 .addUse(AArch64::WZR)
1310 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001311 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001312
1313 if (CC2 != AArch64CC::AL) {
1314 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1315 MachineInstr &CSet2MI =
1316 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1317 .addDef(Def2Reg)
1318 .addUse(AArch64::WZR)
1319 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001320 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001321 MachineInstr &OrMI =
1322 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1323 .addDef(DefReg)
1324 .addUse(Def1Reg)
1325 .addUse(Def2Reg);
1326 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1327 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1328 }
1329
1330 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1331 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1332
1333 I.eraseFromParent();
1334 return true;
1335 }
Tim Northovere9600d82017-02-08 17:57:27 +00001336 case TargetOpcode::G_VASTART:
1337 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1338 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001339 case TargetOpcode::G_IMPLICIT_DEF:
1340 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1341 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001342 }
1343
1344 return false;
1345}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001346
1347/// SelectArithImmed - Select an immediate value that can be represented as
1348/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1349/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001350InstructionSelector::ComplexRendererFn
1351AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001352 MachineInstr &MI = *Root.getParent();
1353 MachineBasicBlock &MBB = *MI.getParent();
1354 MachineFunction &MF = *MBB.getParent();
1355 MachineRegisterInfo &MRI = MF.getRegInfo();
1356
1357 // This function is called from the addsub_shifted_imm ComplexPattern,
1358 // which lists [imm] as the list of opcode it's interested in, however
1359 // we still need to check whether the operand is actually an immediate
1360 // here because the ComplexPattern opcode list is only used in
1361 // root-level opcode matching.
1362 uint64_t Immed;
1363 if (Root.isImm())
1364 Immed = Root.getImm();
1365 else if (Root.isCImm())
1366 Immed = Root.getCImm()->getZExtValue();
1367 else if (Root.isReg()) {
1368 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1369 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001370 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001371 MachineOperand &Op1 = Def->getOperand(1);
1372 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001373 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001374 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001375 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001376 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001377
1378 unsigned ShiftAmt;
1379
1380 if (Immed >> 12 == 0) {
1381 ShiftAmt = 0;
1382 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1383 ShiftAmt = 12;
1384 Immed = Immed >> 12;
1385 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001386 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001387
1388 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001389 return {{
1390 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1391 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1392 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001393}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001394
1395namespace llvm {
1396InstructionSelector *
1397createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1398 AArch64Subtarget &Subtarget,
1399 AArch64RegisterBankInfo &RBI) {
1400 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1401}
1402}