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