blob: fb4d160718a1d8a892b94746783a10edc52a6bae [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the targeting of the InstructionSelector class for
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000015#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000016#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000017#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000020#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000021#include "MCTargetDesc/AArch64AddressingModes.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000022#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Aditya Nandakumar75ad9cc2017-04-19 20:48:50 +000023#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000028#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/IR/Type.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33
34#define DEBUG_TYPE "aarch64-isel"
35
Daniel Sanders6ab0daa2017-07-04 14:35:06 +000036#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
37
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000038using namespace llvm;
39
40#ifndef LLVM_BUILD_GLOBAL_ISEL
41#error "You shouldn't build this"
42#endif
43
Daniel Sanders0b5293f2017-04-06 09:49:34 +000044namespace {
45
Daniel Sanderse7b0d662017-04-21 15:59:56 +000046#define GET_GLOBALISEL_PREDICATE_BITSET
47#include "AArch64GenGlobalISel.inc"
48#undef GET_GLOBALISEL_PREDICATE_BITSET
49
Daniel Sanders0b5293f2017-04-06 09:49:34 +000050class AArch64InstructionSelector : public InstructionSelector {
51public:
52 AArch64InstructionSelector(const AArch64TargetMachine &TM,
53 const AArch64Subtarget &STI,
54 const AArch64RegisterBankInfo &RBI);
55
56 bool select(MachineInstr &I) const override;
57
58private:
59 /// tblgen-erated 'select' implementation, used as the initial selector for
60 /// the patterns that don't require complex C++.
61 bool selectImpl(MachineInstr &I) const;
62
63 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
64 MachineRegisterInfo &MRI) const;
65 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
66 MachineRegisterInfo &MRI) const;
67
68 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
69 MachineRegisterInfo &MRI) const;
70
Daniel Sanders2deea182017-04-22 15:11:04 +000071 ComplexRendererFn selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000072
73 const AArch64TargetMachine &TM;
74 const AArch64Subtarget &STI;
75 const AArch64InstrInfo &TII;
76 const AArch64RegisterInfo &TRI;
77 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +000078
Daniel Sanderse9fdba32017-04-29 17:30:09 +000079#define GET_GLOBALISEL_PREDICATES_DECL
80#include "AArch64GenGlobalISel.inc"
81#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +000082
83// We declare the temporaries used by selectImpl() in the class to minimize the
84// cost of constructing placeholder values.
85#define GET_GLOBALISEL_TEMPORARIES_DECL
86#include "AArch64GenGlobalISel.inc"
87#undef GET_GLOBALISEL_TEMPORARIES_DECL
88};
89
90} // end anonymous namespace
91
Daniel Sanders8a4bae92017-03-14 21:32:08 +000092#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000093#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000094#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000095
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000096AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000097 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
98 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000099 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000100 TRI(*STI.getRegisterInfo()), RBI(RBI),
101#define GET_GLOBALISEL_PREDICATES_INIT
102#include "AArch64GenGlobalISel.inc"
103#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000104#define GET_GLOBALISEL_TEMPORARIES_INIT
105#include "AArch64GenGlobalISel.inc"
106#undef GET_GLOBALISEL_TEMPORARIES_INIT
107{
108}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000109
Tim Northoverfb8d9892016-10-12 22:49:15 +0000110// FIXME: This should be target-independent, inferred from the types declared
111// for each class in the bank.
112static const TargetRegisterClass *
113getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
114 const RegisterBankInfo &RBI) {
115 if (RB.getID() == AArch64::GPRRegBankID) {
116 if (Ty.getSizeInBits() <= 32)
117 return &AArch64::GPR32RegClass;
118 if (Ty.getSizeInBits() == 64)
119 return &AArch64::GPR64RegClass;
120 return nullptr;
121 }
122
123 if (RB.getID() == AArch64::FPRRegBankID) {
124 if (Ty.getSizeInBits() == 32)
125 return &AArch64::FPR32RegClass;
126 if (Ty.getSizeInBits() == 64)
127 return &AArch64::FPR64RegClass;
128 if (Ty.getSizeInBits() == 128)
129 return &AArch64::FPR128RegClass;
130 return nullptr;
131 }
132
133 return nullptr;
134}
135
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000136/// Check whether \p I is a currently unsupported binary operation:
137/// - it has an unsized type
138/// - an operand is not a vreg
139/// - all operands are not in the same bank
140/// These are checks that should someday live in the verifier, but right now,
141/// these are mostly limitations of the aarch64 selector.
142static bool unsupportedBinOp(const MachineInstr &I,
143 const AArch64RegisterBankInfo &RBI,
144 const MachineRegisterInfo &MRI,
145 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000146 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000147 if (!Ty.isValid()) {
148 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000149 return true;
150 }
151
152 const RegisterBank *PrevOpBank = nullptr;
153 for (auto &MO : I.operands()) {
154 // FIXME: Support non-register operands.
155 if (!MO.isReg()) {
156 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
157 return true;
158 }
159
160 // FIXME: Can generic operations have physical registers operands? If
161 // so, this will need to be taught about that, and we'll need to get the
162 // bank out of the minimal class for the register.
163 // Either way, this needs to be documented (and possibly verified).
164 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
165 DEBUG(dbgs() << "Generic inst has physical register operand\n");
166 return true;
167 }
168
169 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
170 if (!OpBank) {
171 DEBUG(dbgs() << "Generic register has no bank or class\n");
172 return true;
173 }
174
175 if (PrevOpBank && OpBank != PrevOpBank) {
176 DEBUG(dbgs() << "Generic inst operands have different banks\n");
177 return true;
178 }
179 PrevOpBank = OpBank;
180 }
181 return false;
182}
183
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000184/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000185/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000186/// and of size \p OpSize.
187/// \returns \p GenericOpc if the combination is unsupported.
188static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
189 unsigned OpSize) {
190 switch (RegBankID) {
191 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000192 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000194 case TargetOpcode::G_SHL:
195 return AArch64::LSLVWr;
196 case TargetOpcode::G_LSHR:
197 return AArch64::LSRVWr;
198 case TargetOpcode::G_ASHR:
199 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000200 default:
201 return GenericOpc;
202 }
Tim Northover55782222016-10-18 20:03:48 +0000203 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000204 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000205 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000206 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000207 case TargetOpcode::G_SHL:
208 return AArch64::LSLVXr;
209 case TargetOpcode::G_LSHR:
210 return AArch64::LSRVXr;
211 case TargetOpcode::G_ASHR:
212 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000213 default:
214 return GenericOpc;
215 }
216 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000217 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000218 case AArch64::FPRRegBankID:
219 switch (OpSize) {
220 case 32:
221 switch (GenericOpc) {
222 case TargetOpcode::G_FADD:
223 return AArch64::FADDSrr;
224 case TargetOpcode::G_FSUB:
225 return AArch64::FSUBSrr;
226 case TargetOpcode::G_FMUL:
227 return AArch64::FMULSrr;
228 case TargetOpcode::G_FDIV:
229 return AArch64::FDIVSrr;
230 default:
231 return GenericOpc;
232 }
233 case 64:
234 switch (GenericOpc) {
235 case TargetOpcode::G_FADD:
236 return AArch64::FADDDrr;
237 case TargetOpcode::G_FSUB:
238 return AArch64::FSUBDrr;
239 case TargetOpcode::G_FMUL:
240 return AArch64::FMULDrr;
241 case TargetOpcode::G_FDIV:
242 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000243 case TargetOpcode::G_OR:
244 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000245 default:
246 return GenericOpc;
247 }
248 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000249 break;
250 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000251 return GenericOpc;
252}
253
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000254/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
255/// appropriate for the (value) register bank \p RegBankID and of memory access
256/// size \p OpSize. This returns the variant with the base+unsigned-immediate
257/// addressing mode (e.g., LDRXui).
258/// \returns \p GenericOpc if the combination is unsupported.
259static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
260 unsigned OpSize) {
261 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
262 switch (RegBankID) {
263 case AArch64::GPRRegBankID:
264 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000265 case 8:
266 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
267 case 16:
268 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000269 case 32:
270 return isStore ? AArch64::STRWui : AArch64::LDRWui;
271 case 64:
272 return isStore ? AArch64::STRXui : AArch64::LDRXui;
273 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000274 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000275 case AArch64::FPRRegBankID:
276 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000277 case 8:
278 return isStore ? AArch64::STRBui : AArch64::LDRBui;
279 case 16:
280 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000281 case 32:
282 return isStore ? AArch64::STRSui : AArch64::LDRSui;
283 case 64:
284 return isStore ? AArch64::STRDui : AArch64::LDRDui;
285 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000286 break;
287 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000288 return GenericOpc;
289}
290
Quentin Colombetcb629a82016-10-12 03:57:49 +0000291static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
292 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
293 const RegisterBankInfo &RBI) {
294
295 unsigned DstReg = I.getOperand(0).getReg();
296 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
297 assert(I.isCopy() && "Generic operators do not allow physical registers");
298 return true;
299 }
300
301 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
302 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
303 unsigned SrcReg = I.getOperand(1).getReg();
304 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
305 (void)SrcSize;
306 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
307 "No phys reg on generic operators");
308 assert(
309 (DstSize == SrcSize ||
310 // Copies are a mean to setup initial types, the number of
311 // bits may not exactly match.
312 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
313 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
314 // Copies are a mean to copy bits around, as long as we are
315 // on the same register class, that's fine. Otherwise, that
316 // means we need some SUBREG_TO_REG or AND & co.
317 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
318 "Copy with different width?!");
319 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
320 "GPRs cannot get more than 64-bit width values");
321 const TargetRegisterClass *RC = nullptr;
322
323 if (RegBank.getID() == AArch64::FPRRegBankID) {
324 if (DstSize <= 32)
325 RC = &AArch64::FPR32RegClass;
326 else if (DstSize <= 64)
327 RC = &AArch64::FPR64RegClass;
328 else if (DstSize <= 128)
329 RC = &AArch64::FPR128RegClass;
330 else {
331 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
332 return false;
333 }
334 } else {
335 assert(RegBank.getID() == AArch64::GPRRegBankID &&
336 "Bitcast for the flags?");
337 RC =
338 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
339 }
340
341 // No need to constrain SrcReg. It will get constrained when
342 // we hit another of its use or its defs.
343 // Copies do not have constraints.
344 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
345 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
346 << " operand\n");
347 return false;
348 }
349 I.setDesc(TII.get(AArch64::COPY));
350 return true;
351}
352
Tim Northover69271c62016-10-12 22:49:11 +0000353static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
354 if (!DstTy.isScalar() || !SrcTy.isScalar())
355 return GenericOpc;
356
357 const unsigned DstSize = DstTy.getSizeInBits();
358 const unsigned SrcSize = SrcTy.getSizeInBits();
359
360 switch (DstSize) {
361 case 32:
362 switch (SrcSize) {
363 case 32:
364 switch (GenericOpc) {
365 case TargetOpcode::G_SITOFP:
366 return AArch64::SCVTFUWSri;
367 case TargetOpcode::G_UITOFP:
368 return AArch64::UCVTFUWSri;
369 case TargetOpcode::G_FPTOSI:
370 return AArch64::FCVTZSUWSr;
371 case TargetOpcode::G_FPTOUI:
372 return AArch64::FCVTZUUWSr;
373 default:
374 return GenericOpc;
375 }
376 case 64:
377 switch (GenericOpc) {
378 case TargetOpcode::G_SITOFP:
379 return AArch64::SCVTFUXSri;
380 case TargetOpcode::G_UITOFP:
381 return AArch64::UCVTFUXSri;
382 case TargetOpcode::G_FPTOSI:
383 return AArch64::FCVTZSUWDr;
384 case TargetOpcode::G_FPTOUI:
385 return AArch64::FCVTZUUWDr;
386 default:
387 return GenericOpc;
388 }
389 default:
390 return GenericOpc;
391 }
392 case 64:
393 switch (SrcSize) {
394 case 32:
395 switch (GenericOpc) {
396 case TargetOpcode::G_SITOFP:
397 return AArch64::SCVTFUWDri;
398 case TargetOpcode::G_UITOFP:
399 return AArch64::UCVTFUWDri;
400 case TargetOpcode::G_FPTOSI:
401 return AArch64::FCVTZSUXSr;
402 case TargetOpcode::G_FPTOUI:
403 return AArch64::FCVTZUUXSr;
404 default:
405 return GenericOpc;
406 }
407 case 64:
408 switch (GenericOpc) {
409 case TargetOpcode::G_SITOFP:
410 return AArch64::SCVTFUXDri;
411 case TargetOpcode::G_UITOFP:
412 return AArch64::UCVTFUXDri;
413 case TargetOpcode::G_FPTOSI:
414 return AArch64::FCVTZSUXDr;
415 case TargetOpcode::G_FPTOUI:
416 return AArch64::FCVTZUUXDr;
417 default:
418 return GenericOpc;
419 }
420 default:
421 return GenericOpc;
422 }
423 default:
424 return GenericOpc;
425 };
426 return GenericOpc;
427}
428
Tim Northover6c02ad52016-10-12 22:49:04 +0000429static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
430 switch (P) {
431 default:
432 llvm_unreachable("Unknown condition code!");
433 case CmpInst::ICMP_NE:
434 return AArch64CC::NE;
435 case CmpInst::ICMP_EQ:
436 return AArch64CC::EQ;
437 case CmpInst::ICMP_SGT:
438 return AArch64CC::GT;
439 case CmpInst::ICMP_SGE:
440 return AArch64CC::GE;
441 case CmpInst::ICMP_SLT:
442 return AArch64CC::LT;
443 case CmpInst::ICMP_SLE:
444 return AArch64CC::LE;
445 case CmpInst::ICMP_UGT:
446 return AArch64CC::HI;
447 case CmpInst::ICMP_UGE:
448 return AArch64CC::HS;
449 case CmpInst::ICMP_ULT:
450 return AArch64CC::LO;
451 case CmpInst::ICMP_ULE:
452 return AArch64CC::LS;
453 }
454}
455
Tim Northover7dd378d2016-10-12 22:49:07 +0000456static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
457 AArch64CC::CondCode &CondCode,
458 AArch64CC::CondCode &CondCode2) {
459 CondCode2 = AArch64CC::AL;
460 switch (P) {
461 default:
462 llvm_unreachable("Unknown FP condition!");
463 case CmpInst::FCMP_OEQ:
464 CondCode = AArch64CC::EQ;
465 break;
466 case CmpInst::FCMP_OGT:
467 CondCode = AArch64CC::GT;
468 break;
469 case CmpInst::FCMP_OGE:
470 CondCode = AArch64CC::GE;
471 break;
472 case CmpInst::FCMP_OLT:
473 CondCode = AArch64CC::MI;
474 break;
475 case CmpInst::FCMP_OLE:
476 CondCode = AArch64CC::LS;
477 break;
478 case CmpInst::FCMP_ONE:
479 CondCode = AArch64CC::MI;
480 CondCode2 = AArch64CC::GT;
481 break;
482 case CmpInst::FCMP_ORD:
483 CondCode = AArch64CC::VC;
484 break;
485 case CmpInst::FCMP_UNO:
486 CondCode = AArch64CC::VS;
487 break;
488 case CmpInst::FCMP_UEQ:
489 CondCode = AArch64CC::EQ;
490 CondCode2 = AArch64CC::VS;
491 break;
492 case CmpInst::FCMP_UGT:
493 CondCode = AArch64CC::HI;
494 break;
495 case CmpInst::FCMP_UGE:
496 CondCode = AArch64CC::PL;
497 break;
498 case CmpInst::FCMP_ULT:
499 CondCode = AArch64CC::LT;
500 break;
501 case CmpInst::FCMP_ULE:
502 CondCode = AArch64CC::LE;
503 break;
504 case CmpInst::FCMP_UNE:
505 CondCode = AArch64CC::NE;
506 break;
507 }
508}
509
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000510bool AArch64InstructionSelector::selectCompareBranch(
511 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
512
513 const unsigned CondReg = I.getOperand(0).getReg();
514 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
515 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000516 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
517 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000518 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
519 return false;
520
521 unsigned LHS = CCMI->getOperand(2).getReg();
522 unsigned RHS = CCMI->getOperand(3).getReg();
523 if (!getConstantVRegVal(RHS, MRI))
524 std::swap(RHS, LHS);
525
526 const auto RHSImm = getConstantVRegVal(RHS, MRI);
527 if (!RHSImm || *RHSImm != 0)
528 return false;
529
530 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
531 if (RB.getID() != AArch64::GPRRegBankID)
532 return false;
533
534 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
535 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
536 return false;
537
538 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
539 unsigned CBOpc = 0;
540 if (CmpWidth <= 32)
541 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
542 else if (CmpWidth == 64)
543 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
544 else
545 return false;
546
547 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
548 .addUse(LHS)
549 .addMBB(DestMBB);
550
551 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
552 I.eraseFromParent();
553 return true;
554}
555
Tim Northovere9600d82017-02-08 17:57:27 +0000556bool AArch64InstructionSelector::selectVaStartAAPCS(
557 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
558 return false;
559}
560
561bool AArch64InstructionSelector::selectVaStartDarwin(
562 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
563 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
564 unsigned ListReg = I.getOperand(0).getReg();
565
566 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
567
568 auto MIB =
569 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
570 .addDef(ArgsAddrReg)
571 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
572 .addImm(0)
573 .addImm(0);
574
575 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
576
577 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
578 .addUse(ArgsAddrReg)
579 .addUse(ListReg)
580 .addImm(0)
581 .addMemOperand(*I.memoperands_begin());
582
583 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
584 I.eraseFromParent();
585 return true;
586}
587
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000588bool AArch64InstructionSelector::select(MachineInstr &I) const {
589 assert(I.getParent() && "Instruction should be in a basic block!");
590 assert(I.getParent()->getParent() && "Instruction should be in a function!");
591
592 MachineBasicBlock &MBB = *I.getParent();
593 MachineFunction &MF = *MBB.getParent();
594 MachineRegisterInfo &MRI = MF.getRegInfo();
595
Tim Northovercdf23f12016-10-31 18:30:59 +0000596 unsigned Opcode = I.getOpcode();
597 if (!isPreISelGenericOpcode(I.getOpcode())) {
598 // Certain non-generic instructions also need some special handling.
599
600 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
601 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000602
603 if (Opcode == TargetOpcode::PHI) {
604 const unsigned DefReg = I.getOperand(0).getReg();
605 const LLT DefTy = MRI.getType(DefReg);
606
607 const TargetRegisterClass *DefRC = nullptr;
608 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
609 DefRC = TRI.getRegClass(DefReg);
610 } else {
611 const RegClassOrRegBank &RegClassOrBank =
612 MRI.getRegClassOrRegBank(DefReg);
613
614 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
615 if (!DefRC) {
616 if (!DefTy.isValid()) {
617 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
618 return false;
619 }
620 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
621 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
622 if (!DefRC) {
623 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
624 return false;
625 }
626 }
627 }
628
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 }
708 } else {
709 if (Ty != s32 && Ty != s64 && Ty != p0) {
710 DEBUG(dbgs() << "Unable to materialize integer " << Ty
711 << " constant, expected: " << s32 << ", " << s64 << ", or "
712 << p0 << '\n');
713 return false;
714 }
715
716 if (RB.getID() != AArch64::GPRRegBankID) {
717 DEBUG(dbgs() << "Unable to materialize integer " << Ty
718 << " constant on bank: " << RB << ", expected: GPR\n");
719 return false;
720 }
721 }
722
723 const unsigned MovOpc =
724 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
725
726 I.setDesc(TII.get(MovOpc));
727
728 if (isFP) {
729 const TargetRegisterClass &GPRRC =
730 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
731 const TargetRegisterClass &FPRRC =
732 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
733
734 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
735 MachineOperand &RegOp = I.getOperand(0);
736 RegOp.setReg(DefGPRReg);
737
738 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
739 TII.get(AArch64::COPY))
740 .addDef(DefReg)
741 .addUse(DefGPRReg);
742
743 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
744 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
745 return false;
746 }
747
748 MachineOperand &ImmOp = I.getOperand(1);
749 // FIXME: Is going through int64_t always correct?
750 ImmOp.ChangeToImmediate(
751 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000752 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000753 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
754 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000755 } else if (I.getOperand(1).isImm()) {
756 uint64_t Val = I.getOperand(1).getImm();
757 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000758 }
759
760 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
761 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000762 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000763 case TargetOpcode::G_EXTRACT: {
764 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
765 // Larger extracts are vectors, same-size extracts should be something else
766 // by now (either split up or simplified to a COPY).
767 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
768 return false;
769
770 I.setDesc(TII.get(AArch64::UBFMXri));
771 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
772 Ty.getSizeInBits() - 1);
773
774 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
775 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
776 TII.get(AArch64::COPY))
777 .addDef(I.getOperand(0).getReg())
778 .addUse(DstReg, 0, AArch64::sub_32);
779 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
780 AArch64::GPR32RegClass, MRI);
781 I.getOperand(0).setReg(DstReg);
782
783 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
784 }
785
786 case TargetOpcode::G_INSERT: {
787 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
788 // Larger inserts are vectors, same-size ones should be something else by
789 // now (split up or turned into COPYs).
790 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
791 return false;
792
793 I.setDesc(TII.get(AArch64::BFMXri));
794 unsigned LSB = I.getOperand(3).getImm();
795 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
796 I.getOperand(3).setImm((64 - LSB) % 64);
797 MachineInstrBuilder(MF, I).addImm(Width - 1);
798
799 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
800 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
801 TII.get(AArch64::SUBREG_TO_REG))
802 .addDef(SrcReg)
803 .addImm(0)
804 .addUse(I.getOperand(2).getReg())
805 .addImm(AArch64::sub_32);
806 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
807 AArch64::GPR32RegClass, MRI);
808 I.getOperand(2).setReg(SrcReg);
809
810 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
811 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000812 case TargetOpcode::G_FRAME_INDEX: {
813 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000814 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000815 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000816 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000817 return false;
818 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000819 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000820
821 // MOs for a #0 shifted immediate.
822 I.addOperand(MachineOperand::CreateImm(0));
823 I.addOperand(MachineOperand::CreateImm(0));
824
825 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
826 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000827
828 case TargetOpcode::G_GLOBAL_VALUE: {
829 auto GV = I.getOperand(1).getGlobal();
830 if (GV->isThreadLocal()) {
831 // FIXME: we don't support TLS yet.
832 return false;
833 }
834 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000835 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000836 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000837 I.getOperand(1).setTargetFlags(OpFlags);
838 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000839 I.setDesc(TII.get(AArch64::MOVaddr));
840 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
841 MachineInstrBuilder MIB(MF, I);
842 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
843 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
844 }
845 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
846 }
847
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000848 case TargetOpcode::G_LOAD:
849 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000850 LLT MemTy = Ty;
851 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000852
Tim Northover5ae83502016-09-15 09:20:34 +0000853 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000854 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000855 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000856 return false;
857 }
858
Tim Northover48dfa1a2017-02-13 22:14:16 +0000859 auto &MemOp = **I.memoperands_begin();
860 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
861 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
862 return false;
863 }
864
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000865 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000866#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000867 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000868 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000869 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
870 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000871 assert(MRI.getType(PtrReg).isPointer() &&
872 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000873#endif
874
875 const unsigned ValReg = I.getOperand(0).getReg();
876 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
877
878 const unsigned NewOpc =
879 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
880 if (NewOpc == I.getOpcode())
881 return false;
882
883 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000884
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000885 uint64_t Offset = 0;
886 auto *PtrMI = MRI.getVRegDef(PtrReg);
887
888 // Try to fold a GEP into our unsigned immediate addressing mode.
889 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
890 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
891 int64_t Imm = *COff;
892 const unsigned Size = MemTy.getSizeInBits() / 8;
893 const unsigned Scale = Log2_32(Size);
894 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
895 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
896 I.getOperand(1).setReg(Ptr2Reg);
897 PtrMI = MRI.getVRegDef(Ptr2Reg);
898 Offset = Imm / Size;
899 }
900 }
901 }
902
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000903 // If we haven't folded anything into our addressing mode yet, try to fold
904 // a frame index into the base+offset.
905 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
906 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
907
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000908 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000909
910 // If we're storing a 0, use WZR/XZR.
911 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
912 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
913 if (I.getOpcode() == AArch64::STRWui)
914 I.getOperand(0).setReg(AArch64::WZR);
915 else if (I.getOpcode() == AArch64::STRXui)
916 I.getOperand(0).setReg(AArch64::XZR);
917 }
918 }
919
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000920 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
921 }
922
Tim Northover9dd78f82017-02-08 21:22:25 +0000923 case TargetOpcode::G_SMULH:
924 case TargetOpcode::G_UMULH: {
925 // Reject the various things we don't support yet.
926 if (unsupportedBinOp(I, RBI, MRI, TRI))
927 return false;
928
929 const unsigned DefReg = I.getOperand(0).getReg();
930 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
931
932 if (RB.getID() != AArch64::GPRRegBankID) {
933 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
934 return false;
935 }
936
937 if (Ty != LLT::scalar(64)) {
938 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
939 << ", expected: " << LLT::scalar(64) << '\n');
940 return false;
941 }
942
943 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
944 : AArch64::UMULHrr;
945 I.setDesc(TII.get(NewOpc));
946
947 // Now that we selected an opcode, we need to constrain the register
948 // operands to use appropriate classes.
949 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
950 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000951 case TargetOpcode::G_FADD:
952 case TargetOpcode::G_FSUB:
953 case TargetOpcode::G_FMUL:
954 case TargetOpcode::G_FDIV:
955
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000956 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000957 case TargetOpcode::G_SHL:
958 case TargetOpcode::G_LSHR:
959 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000960 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000961 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000962 if (unsupportedBinOp(I, RBI, MRI, TRI))
963 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000964
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000965 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000966
967 const unsigned DefReg = I.getOperand(0).getReg();
968 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
969
970 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
971 if (NewOpc == I.getOpcode())
972 return false;
973
974 I.setDesc(TII.get(NewOpc));
975 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000976
977 // Now that we selected an opcode, we need to constrain the register
978 // operands to use appropriate classes.
979 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
980 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000981
Tim Northover398c5f52017-02-14 20:56:29 +0000982 case TargetOpcode::G_PTR_MASK: {
983 uint64_t Align = I.getOperand(2).getImm();
984 if (Align >= 64 || Align == 0)
985 return false;
986
987 uint64_t Mask = ~((1ULL << Align) - 1);
988 I.setDesc(TII.get(AArch64::ANDXri));
989 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
990
991 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
992 }
Tim Northover037af52c2016-10-31 18:31:09 +0000993 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000994 case TargetOpcode::G_TRUNC: {
995 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
996 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
997
998 const unsigned DstReg = I.getOperand(0).getReg();
999 const unsigned SrcReg = I.getOperand(1).getReg();
1000
1001 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1002 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1003
1004 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001005 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001006 return false;
1007 }
1008
1009 if (DstRB.getID() == AArch64::GPRRegBankID) {
1010 const TargetRegisterClass *DstRC =
1011 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1012 if (!DstRC)
1013 return false;
1014
1015 const TargetRegisterClass *SrcRC =
1016 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1017 if (!SrcRC)
1018 return false;
1019
1020 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1021 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001022 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001023 return false;
1024 }
1025
1026 if (DstRC == SrcRC) {
1027 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001028 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1029 SrcTy == LLT::scalar(64)) {
1030 llvm_unreachable("TableGen can import this case");
1031 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001032 } else if (DstRC == &AArch64::GPR32RegClass &&
1033 SrcRC == &AArch64::GPR64RegClass) {
1034 I.getOperand(1).setSubReg(AArch64::sub_32);
1035 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001036 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001037 return false;
1038 }
1039
1040 I.setDesc(TII.get(TargetOpcode::COPY));
1041 return true;
1042 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1043 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1044 I.setDesc(TII.get(AArch64::XTNv4i16));
1045 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1046 return true;
1047 }
1048 }
1049
1050 return false;
1051 }
1052
Tim Northover3d38b3a2016-10-11 20:50:21 +00001053 case TargetOpcode::G_ANYEXT: {
1054 const unsigned DstReg = I.getOperand(0).getReg();
1055 const unsigned SrcReg = I.getOperand(1).getReg();
1056
Quentin Colombetcb629a82016-10-12 03:57:49 +00001057 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1058 if (RBDst.getID() != AArch64::GPRRegBankID) {
1059 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1060 return false;
1061 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001062
Quentin Colombetcb629a82016-10-12 03:57:49 +00001063 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1064 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1065 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001066 return false;
1067 }
1068
1069 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1070
1071 if (DstSize == 0) {
1072 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1073 return false;
1074 }
1075
Quentin Colombetcb629a82016-10-12 03:57:49 +00001076 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001077 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1078 << ", expected: 32 or 64\n");
1079 return false;
1080 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001081 // At this point G_ANYEXT is just like a plain COPY, but we need
1082 // to explicitly form the 64-bit value if any.
1083 if (DstSize > 32) {
1084 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1085 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1086 .addDef(ExtSrc)
1087 .addImm(0)
1088 .addUse(SrcReg)
1089 .addImm(AArch64::sub_32);
1090 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001091 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001092 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001093 }
1094
1095 case TargetOpcode::G_ZEXT:
1096 case TargetOpcode::G_SEXT: {
1097 unsigned Opcode = I.getOpcode();
1098 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1099 SrcTy = MRI.getType(I.getOperand(1).getReg());
1100 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1101 const unsigned DefReg = I.getOperand(0).getReg();
1102 const unsigned SrcReg = I.getOperand(1).getReg();
1103 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1104
1105 if (RB.getID() != AArch64::GPRRegBankID) {
1106 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1107 << ", expected: GPR\n");
1108 return false;
1109 }
1110
1111 MachineInstr *ExtI;
1112 if (DstTy == LLT::scalar(64)) {
1113 // FIXME: Can we avoid manually doing this?
1114 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1115 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1116 << " operand\n");
1117 return false;
1118 }
1119
1120 const unsigned SrcXReg =
1121 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1122 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1123 .addDef(SrcXReg)
1124 .addImm(0)
1125 .addUse(SrcReg)
1126 .addImm(AArch64::sub_32);
1127
1128 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1129 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1130 .addDef(DefReg)
1131 .addUse(SrcXReg)
1132 .addImm(0)
1133 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001134 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001135 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1136 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1137 .addDef(DefReg)
1138 .addUse(SrcReg)
1139 .addImm(0)
1140 .addImm(SrcTy.getSizeInBits() - 1);
1141 } else {
1142 return false;
1143 }
1144
1145 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1146
1147 I.eraseFromParent();
1148 return true;
1149 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001150
Tim Northover69271c62016-10-12 22:49:11 +00001151 case TargetOpcode::G_SITOFP:
1152 case TargetOpcode::G_UITOFP:
1153 case TargetOpcode::G_FPTOSI:
1154 case TargetOpcode::G_FPTOUI: {
1155 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1156 SrcTy = MRI.getType(I.getOperand(1).getReg());
1157 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1158 if (NewOpc == Opcode)
1159 return false;
1160
1161 I.setDesc(TII.get(NewOpc));
1162 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1163
1164 return true;
1165 }
1166
1167
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001168 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001169 case TargetOpcode::G_BITCAST:
1170 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001171
Tim Northover5f7dea82016-11-08 17:44:07 +00001172 case TargetOpcode::G_FPEXT: {
1173 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1174 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1175 << ", expected: " << LLT::scalar(64) << '\n');
1176 return false;
1177 }
1178
1179 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1180 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1181 << ", expected: " << LLT::scalar(32) << '\n');
1182 return false;
1183 }
1184
1185 const unsigned DefReg = I.getOperand(0).getReg();
1186 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1187
1188 if (RB.getID() != AArch64::FPRRegBankID) {
1189 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1190 return false;
1191 }
1192
1193 I.setDesc(TII.get(AArch64::FCVTDSr));
1194 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1195
1196 return true;
1197 }
1198
1199 case TargetOpcode::G_FPTRUNC: {
1200 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1201 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1202 << ", expected: " << LLT::scalar(32) << '\n');
1203 return false;
1204 }
1205
1206 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1207 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1208 << ", expected: " << LLT::scalar(64) << '\n');
1209 return false;
1210 }
1211
1212 const unsigned DefReg = I.getOperand(0).getReg();
1213 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1214
1215 if (RB.getID() != AArch64::FPRRegBankID) {
1216 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1217 return false;
1218 }
1219
1220 I.setDesc(TII.get(AArch64::FCVTSDr));
1221 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1222
1223 return true;
1224 }
1225
Tim Northover9ac0eba2016-11-08 00:45:29 +00001226 case TargetOpcode::G_SELECT: {
1227 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1228 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1229 << ", expected: " << LLT::scalar(1) << '\n');
1230 return false;
1231 }
1232
1233 const unsigned CondReg = I.getOperand(1).getReg();
1234 const unsigned TReg = I.getOperand(2).getReg();
1235 const unsigned FReg = I.getOperand(3).getReg();
1236
1237 unsigned CSelOpc = 0;
1238
1239 if (Ty == LLT::scalar(32)) {
1240 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001241 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001242 CSelOpc = AArch64::CSELXr;
1243 } else {
1244 return false;
1245 }
1246
1247 MachineInstr &TstMI =
1248 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1249 .addDef(AArch64::WZR)
1250 .addUse(CondReg)
1251 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1252
1253 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1254 .addDef(I.getOperand(0).getReg())
1255 .addUse(TReg)
1256 .addUse(FReg)
1257 .addImm(AArch64CC::NE);
1258
1259 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1260 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1261
1262 I.eraseFromParent();
1263 return true;
1264 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001265 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001266 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001267 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001268 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001269 return false;
1270 }
1271
1272 unsigned CmpOpc = 0;
1273 unsigned ZReg = 0;
1274
1275 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1276 if (CmpTy == LLT::scalar(32)) {
1277 CmpOpc = AArch64::SUBSWrr;
1278 ZReg = AArch64::WZR;
1279 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1280 CmpOpc = AArch64::SUBSXrr;
1281 ZReg = AArch64::XZR;
1282 } else {
1283 return false;
1284 }
1285
Kristof Beyls22524402017-01-05 10:16:08 +00001286 // CSINC increments the result by one when the condition code is false.
1287 // Therefore, we have to invert the predicate to get an increment by 1 when
1288 // the predicate is true.
1289 const AArch64CC::CondCode invCC =
1290 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1291 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001292
1293 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1294 .addDef(ZReg)
1295 .addUse(I.getOperand(2).getReg())
1296 .addUse(I.getOperand(3).getReg());
1297
1298 MachineInstr &CSetMI =
1299 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1300 .addDef(I.getOperand(0).getReg())
1301 .addUse(AArch64::WZR)
1302 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001303 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001304
1305 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1306 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1307
1308 I.eraseFromParent();
1309 return true;
1310 }
1311
Tim Northover7dd378d2016-10-12 22:49:07 +00001312 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001313 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001314 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001315 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001316 return false;
1317 }
1318
1319 unsigned CmpOpc = 0;
1320 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1321 if (CmpTy == LLT::scalar(32)) {
1322 CmpOpc = AArch64::FCMPSrr;
1323 } else if (CmpTy == LLT::scalar(64)) {
1324 CmpOpc = AArch64::FCMPDrr;
1325 } else {
1326 return false;
1327 }
1328
1329 // FIXME: regbank
1330
1331 AArch64CC::CondCode CC1, CC2;
1332 changeFCMPPredToAArch64CC(
1333 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1334
1335 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1336 .addUse(I.getOperand(2).getReg())
1337 .addUse(I.getOperand(3).getReg());
1338
1339 const unsigned DefReg = I.getOperand(0).getReg();
1340 unsigned Def1Reg = DefReg;
1341 if (CC2 != AArch64CC::AL)
1342 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1343
1344 MachineInstr &CSetMI =
1345 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1346 .addDef(Def1Reg)
1347 .addUse(AArch64::WZR)
1348 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001349 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001350
1351 if (CC2 != AArch64CC::AL) {
1352 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1353 MachineInstr &CSet2MI =
1354 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1355 .addDef(Def2Reg)
1356 .addUse(AArch64::WZR)
1357 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001358 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001359 MachineInstr &OrMI =
1360 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1361 .addDef(DefReg)
1362 .addUse(Def1Reg)
1363 .addUse(Def2Reg);
1364 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1365 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1366 }
1367
1368 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1369 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1370
1371 I.eraseFromParent();
1372 return true;
1373 }
Tim Northovere9600d82017-02-08 17:57:27 +00001374 case TargetOpcode::G_VASTART:
1375 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1376 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001377 case TargetOpcode::G_IMPLICIT_DEF:
1378 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1379 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001380 }
1381
1382 return false;
1383}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001384
1385/// SelectArithImmed - Select an immediate value that can be represented as
1386/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1387/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001388InstructionSelector::ComplexRendererFn
1389AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001390 MachineInstr &MI = *Root.getParent();
1391 MachineBasicBlock &MBB = *MI.getParent();
1392 MachineFunction &MF = *MBB.getParent();
1393 MachineRegisterInfo &MRI = MF.getRegInfo();
1394
1395 // This function is called from the addsub_shifted_imm ComplexPattern,
1396 // which lists [imm] as the list of opcode it's interested in, however
1397 // we still need to check whether the operand is actually an immediate
1398 // here because the ComplexPattern opcode list is only used in
1399 // root-level opcode matching.
1400 uint64_t Immed;
1401 if (Root.isImm())
1402 Immed = Root.getImm();
1403 else if (Root.isCImm())
1404 Immed = Root.getCImm()->getZExtValue();
1405 else if (Root.isReg()) {
1406 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1407 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sanders2deea182017-04-22 15:11:04 +00001408 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001409 MachineOperand &Op1 = Def->getOperand(1);
1410 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sanders2deea182017-04-22 15:11:04 +00001411 return nullptr;
Daniel Sanders0e642022017-03-16 18:04:50 +00001412 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001413 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001414 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001415
1416 unsigned ShiftAmt;
1417
1418 if (Immed >> 12 == 0) {
1419 ShiftAmt = 0;
1420 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1421 ShiftAmt = 12;
1422 Immed = Immed >> 12;
1423 } else
Daniel Sanders2deea182017-04-22 15:11:04 +00001424 return nullptr;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001425
1426 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sanders2deea182017-04-22 15:11:04 +00001427 return [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed).addImm(ShVal); };
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001428}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001429
1430namespace llvm {
1431InstructionSelector *
1432createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1433 AArch64Subtarget &Subtarget,
1434 AArch64RegisterBankInfo &RBI) {
1435 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1436}
1437}