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