blob: 622bf995147d21a333c0b6bdfbbe68ad27435a2b [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
Daniel Sandersea8711b2017-10-16 03:36:29 +000069 ComplexRendererFn selectAddrModeUnscaled(MachineOperand &Root,
70 unsigned Size) const;
71
72 ComplexRendererFn selectAddrModeUnscaled8(MachineOperand &Root) const {
73 return selectAddrModeUnscaled(Root, 1);
74 }
75 ComplexRendererFn selectAddrModeUnscaled16(MachineOperand &Root) const {
76 return selectAddrModeUnscaled(Root, 2);
77 }
78 ComplexRendererFn selectAddrModeUnscaled32(MachineOperand &Root) const {
79 return selectAddrModeUnscaled(Root, 4);
80 }
81 ComplexRendererFn selectAddrModeUnscaled64(MachineOperand &Root) const {
82 return selectAddrModeUnscaled(Root, 8);
83 }
84 ComplexRendererFn selectAddrModeUnscaled128(MachineOperand &Root) const {
85 return selectAddrModeUnscaled(Root, 16);
86 }
87
88 ComplexRendererFn selectAddrModeIndexed(MachineOperand &Root,
89 unsigned Size) const;
90 template <int Width>
91 ComplexRendererFn selectAddrModeIndexed(MachineOperand &Root) const {
92 return selectAddrModeIndexed(Root, Width / 8);
93 }
94
Daniel Sanders0b5293f2017-04-06 09:49:34 +000095 const AArch64TargetMachine &TM;
96 const AArch64Subtarget &STI;
97 const AArch64InstrInfo &TII;
98 const AArch64RegisterInfo &TRI;
99 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000100
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000101#define GET_GLOBALISEL_PREDICATES_DECL
102#include "AArch64GenGlobalISel.inc"
103#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000104
105// We declare the temporaries used by selectImpl() in the class to minimize the
106// cost of constructing placeholder values.
107#define GET_GLOBALISEL_TEMPORARIES_DECL
108#include "AArch64GenGlobalISel.inc"
109#undef GET_GLOBALISEL_TEMPORARIES_DECL
110};
111
112} // end anonymous namespace
113
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000114#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000115#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000116#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000117
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000118AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +0000119 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
120 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000121 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000122 TRI(*STI.getRegisterInfo()), RBI(RBI),
123#define GET_GLOBALISEL_PREDICATES_INIT
124#include "AArch64GenGlobalISel.inc"
125#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000126#define GET_GLOBALISEL_TEMPORARIES_INIT
127#include "AArch64GenGlobalISel.inc"
128#undef GET_GLOBALISEL_TEMPORARIES_INIT
129{
130}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000131
Tim Northoverfb8d9892016-10-12 22:49:15 +0000132// FIXME: This should be target-independent, inferred from the types declared
133// for each class in the bank.
134static const TargetRegisterClass *
135getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
136 const RegisterBankInfo &RBI) {
137 if (RB.getID() == AArch64::GPRRegBankID) {
138 if (Ty.getSizeInBits() <= 32)
139 return &AArch64::GPR32RegClass;
140 if (Ty.getSizeInBits() == 64)
141 return &AArch64::GPR64RegClass;
142 return nullptr;
143 }
144
145 if (RB.getID() == AArch64::FPRRegBankID) {
146 if (Ty.getSizeInBits() == 32)
147 return &AArch64::FPR32RegClass;
148 if (Ty.getSizeInBits() == 64)
149 return &AArch64::FPR64RegClass;
150 if (Ty.getSizeInBits() == 128)
151 return &AArch64::FPR128RegClass;
152 return nullptr;
153 }
154
155 return nullptr;
156}
157
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000158/// Check whether \p I is a currently unsupported binary operation:
159/// - it has an unsized type
160/// - an operand is not a vreg
161/// - all operands are not in the same bank
162/// These are checks that should someday live in the verifier, but right now,
163/// these are mostly limitations of the aarch64 selector.
164static bool unsupportedBinOp(const MachineInstr &I,
165 const AArch64RegisterBankInfo &RBI,
166 const MachineRegisterInfo &MRI,
167 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000168 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000169 if (!Ty.isValid()) {
170 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000171 return true;
172 }
173
174 const RegisterBank *PrevOpBank = nullptr;
175 for (auto &MO : I.operands()) {
176 // FIXME: Support non-register operands.
177 if (!MO.isReg()) {
178 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
179 return true;
180 }
181
182 // FIXME: Can generic operations have physical registers operands? If
183 // so, this will need to be taught about that, and we'll need to get the
184 // bank out of the minimal class for the register.
185 // Either way, this needs to be documented (and possibly verified).
186 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
187 DEBUG(dbgs() << "Generic inst has physical register operand\n");
188 return true;
189 }
190
191 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
192 if (!OpBank) {
193 DEBUG(dbgs() << "Generic register has no bank or class\n");
194 return true;
195 }
196
197 if (PrevOpBank && OpBank != PrevOpBank) {
198 DEBUG(dbgs() << "Generic inst operands have different banks\n");
199 return true;
200 }
201 PrevOpBank = OpBank;
202 }
203 return false;
204}
205
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000206/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000207/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000208/// and of size \p OpSize.
209/// \returns \p GenericOpc if the combination is unsupported.
210static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
211 unsigned OpSize) {
212 switch (RegBankID) {
213 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000214 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000215 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000216 case TargetOpcode::G_SHL:
217 return AArch64::LSLVWr;
218 case TargetOpcode::G_LSHR:
219 return AArch64::LSRVWr;
220 case TargetOpcode::G_ASHR:
221 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000222 default:
223 return GenericOpc;
224 }
Tim Northover55782222016-10-18 20:03:48 +0000225 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000226 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000227 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000228 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000229 case TargetOpcode::G_SHL:
230 return AArch64::LSLVXr;
231 case TargetOpcode::G_LSHR:
232 return AArch64::LSRVXr;
233 case TargetOpcode::G_ASHR:
234 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000235 default:
236 return GenericOpc;
237 }
238 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000239 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000240 case AArch64::FPRRegBankID:
241 switch (OpSize) {
242 case 32:
243 switch (GenericOpc) {
244 case TargetOpcode::G_FADD:
245 return AArch64::FADDSrr;
246 case TargetOpcode::G_FSUB:
247 return AArch64::FSUBSrr;
248 case TargetOpcode::G_FMUL:
249 return AArch64::FMULSrr;
250 case TargetOpcode::G_FDIV:
251 return AArch64::FDIVSrr;
252 default:
253 return GenericOpc;
254 }
255 case 64:
256 switch (GenericOpc) {
257 case TargetOpcode::G_FADD:
258 return AArch64::FADDDrr;
259 case TargetOpcode::G_FSUB:
260 return AArch64::FSUBDrr;
261 case TargetOpcode::G_FMUL:
262 return AArch64::FMULDrr;
263 case TargetOpcode::G_FDIV:
264 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000265 case TargetOpcode::G_OR:
266 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000267 default:
268 return GenericOpc;
269 }
270 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000271 break;
272 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000273 return GenericOpc;
274}
275
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000276/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
277/// appropriate for the (value) register bank \p RegBankID and of memory access
278/// size \p OpSize. This returns the variant with the base+unsigned-immediate
279/// addressing mode (e.g., LDRXui).
280/// \returns \p GenericOpc if the combination is unsupported.
281static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
282 unsigned OpSize) {
283 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
284 switch (RegBankID) {
285 case AArch64::GPRRegBankID:
286 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000287 case 8:
288 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
289 case 16:
290 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000291 case 32:
292 return isStore ? AArch64::STRWui : AArch64::LDRWui;
293 case 64:
294 return isStore ? AArch64::STRXui : AArch64::LDRXui;
295 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000296 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000297 case AArch64::FPRRegBankID:
298 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000299 case 8:
300 return isStore ? AArch64::STRBui : AArch64::LDRBui;
301 case 16:
302 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000303 case 32:
304 return isStore ? AArch64::STRSui : AArch64::LDRSui;
305 case 64:
306 return isStore ? AArch64::STRDui : AArch64::LDRDui;
307 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000308 break;
309 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000310 return GenericOpc;
311}
312
Quentin Colombetcb629a82016-10-12 03:57:49 +0000313static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
314 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
315 const RegisterBankInfo &RBI) {
316
317 unsigned DstReg = I.getOperand(0).getReg();
318 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
319 assert(I.isCopy() && "Generic operators do not allow physical registers");
320 return true;
321 }
322
323 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
324 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
325 unsigned SrcReg = I.getOperand(1).getReg();
326 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
327 (void)SrcSize;
328 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
329 "No phys reg on generic operators");
330 assert(
331 (DstSize == SrcSize ||
332 // Copies are a mean to setup initial types, the number of
333 // bits may not exactly match.
334 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
335 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
336 // Copies are a mean to copy bits around, as long as we are
337 // on the same register class, that's fine. Otherwise, that
338 // means we need some SUBREG_TO_REG or AND & co.
339 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
340 "Copy with different width?!");
341 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
342 "GPRs cannot get more than 64-bit width values");
343 const TargetRegisterClass *RC = nullptr;
344
345 if (RegBank.getID() == AArch64::FPRRegBankID) {
Ahmed Bougachaa7aa2a92017-09-12 21:04:10 +0000346 if (DstSize <= 16)
347 RC = &AArch64::FPR16RegClass;
348 else if (DstSize <= 32)
Quentin Colombetcb629a82016-10-12 03:57:49 +0000349 RC = &AArch64::FPR32RegClass;
350 else if (DstSize <= 64)
351 RC = &AArch64::FPR64RegClass;
352 else if (DstSize <= 128)
353 RC = &AArch64::FPR128RegClass;
354 else {
355 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
356 return false;
357 }
358 } else {
359 assert(RegBank.getID() == AArch64::GPRRegBankID &&
360 "Bitcast for the flags?");
361 RC =
362 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
363 }
364
365 // No need to constrain SrcReg. It will get constrained when
366 // we hit another of its use or its defs.
367 // Copies do not have constraints.
368 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
369 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
370 << " operand\n");
371 return false;
372 }
373 I.setDesc(TII.get(AArch64::COPY));
374 return true;
375}
376
Tim Northover69271c62016-10-12 22:49:11 +0000377static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
378 if (!DstTy.isScalar() || !SrcTy.isScalar())
379 return GenericOpc;
380
381 const unsigned DstSize = DstTy.getSizeInBits();
382 const unsigned SrcSize = SrcTy.getSizeInBits();
383
384 switch (DstSize) {
385 case 32:
386 switch (SrcSize) {
387 case 32:
388 switch (GenericOpc) {
389 case TargetOpcode::G_SITOFP:
390 return AArch64::SCVTFUWSri;
391 case TargetOpcode::G_UITOFP:
392 return AArch64::UCVTFUWSri;
393 case TargetOpcode::G_FPTOSI:
394 return AArch64::FCVTZSUWSr;
395 case TargetOpcode::G_FPTOUI:
396 return AArch64::FCVTZUUWSr;
397 default:
398 return GenericOpc;
399 }
400 case 64:
401 switch (GenericOpc) {
402 case TargetOpcode::G_SITOFP:
403 return AArch64::SCVTFUXSri;
404 case TargetOpcode::G_UITOFP:
405 return AArch64::UCVTFUXSri;
406 case TargetOpcode::G_FPTOSI:
407 return AArch64::FCVTZSUWDr;
408 case TargetOpcode::G_FPTOUI:
409 return AArch64::FCVTZUUWDr;
410 default:
411 return GenericOpc;
412 }
413 default:
414 return GenericOpc;
415 }
416 case 64:
417 switch (SrcSize) {
418 case 32:
419 switch (GenericOpc) {
420 case TargetOpcode::G_SITOFP:
421 return AArch64::SCVTFUWDri;
422 case TargetOpcode::G_UITOFP:
423 return AArch64::UCVTFUWDri;
424 case TargetOpcode::G_FPTOSI:
425 return AArch64::FCVTZSUXSr;
426 case TargetOpcode::G_FPTOUI:
427 return AArch64::FCVTZUUXSr;
428 default:
429 return GenericOpc;
430 }
431 case 64:
432 switch (GenericOpc) {
433 case TargetOpcode::G_SITOFP:
434 return AArch64::SCVTFUXDri;
435 case TargetOpcode::G_UITOFP:
436 return AArch64::UCVTFUXDri;
437 case TargetOpcode::G_FPTOSI:
438 return AArch64::FCVTZSUXDr;
439 case TargetOpcode::G_FPTOUI:
440 return AArch64::FCVTZUUXDr;
441 default:
442 return GenericOpc;
443 }
444 default:
445 return GenericOpc;
446 }
447 default:
448 return GenericOpc;
449 };
450 return GenericOpc;
451}
452
Tim Northover6c02ad52016-10-12 22:49:04 +0000453static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
454 switch (P) {
455 default:
456 llvm_unreachable("Unknown condition code!");
457 case CmpInst::ICMP_NE:
458 return AArch64CC::NE;
459 case CmpInst::ICMP_EQ:
460 return AArch64CC::EQ;
461 case CmpInst::ICMP_SGT:
462 return AArch64CC::GT;
463 case CmpInst::ICMP_SGE:
464 return AArch64CC::GE;
465 case CmpInst::ICMP_SLT:
466 return AArch64CC::LT;
467 case CmpInst::ICMP_SLE:
468 return AArch64CC::LE;
469 case CmpInst::ICMP_UGT:
470 return AArch64CC::HI;
471 case CmpInst::ICMP_UGE:
472 return AArch64CC::HS;
473 case CmpInst::ICMP_ULT:
474 return AArch64CC::LO;
475 case CmpInst::ICMP_ULE:
476 return AArch64CC::LS;
477 }
478}
479
Tim Northover7dd378d2016-10-12 22:49:07 +0000480static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
481 AArch64CC::CondCode &CondCode,
482 AArch64CC::CondCode &CondCode2) {
483 CondCode2 = AArch64CC::AL;
484 switch (P) {
485 default:
486 llvm_unreachable("Unknown FP condition!");
487 case CmpInst::FCMP_OEQ:
488 CondCode = AArch64CC::EQ;
489 break;
490 case CmpInst::FCMP_OGT:
491 CondCode = AArch64CC::GT;
492 break;
493 case CmpInst::FCMP_OGE:
494 CondCode = AArch64CC::GE;
495 break;
496 case CmpInst::FCMP_OLT:
497 CondCode = AArch64CC::MI;
498 break;
499 case CmpInst::FCMP_OLE:
500 CondCode = AArch64CC::LS;
501 break;
502 case CmpInst::FCMP_ONE:
503 CondCode = AArch64CC::MI;
504 CondCode2 = AArch64CC::GT;
505 break;
506 case CmpInst::FCMP_ORD:
507 CondCode = AArch64CC::VC;
508 break;
509 case CmpInst::FCMP_UNO:
510 CondCode = AArch64CC::VS;
511 break;
512 case CmpInst::FCMP_UEQ:
513 CondCode = AArch64CC::EQ;
514 CondCode2 = AArch64CC::VS;
515 break;
516 case CmpInst::FCMP_UGT:
517 CondCode = AArch64CC::HI;
518 break;
519 case CmpInst::FCMP_UGE:
520 CondCode = AArch64CC::PL;
521 break;
522 case CmpInst::FCMP_ULT:
523 CondCode = AArch64CC::LT;
524 break;
525 case CmpInst::FCMP_ULE:
526 CondCode = AArch64CC::LE;
527 break;
528 case CmpInst::FCMP_UNE:
529 CondCode = AArch64CC::NE;
530 break;
531 }
532}
533
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000534bool AArch64InstructionSelector::selectCompareBranch(
535 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
536
537 const unsigned CondReg = I.getOperand(0).getReg();
538 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
539 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000540 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
541 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000542 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
543 return false;
544
545 unsigned LHS = CCMI->getOperand(2).getReg();
546 unsigned RHS = CCMI->getOperand(3).getReg();
547 if (!getConstantVRegVal(RHS, MRI))
548 std::swap(RHS, LHS);
549
550 const auto RHSImm = getConstantVRegVal(RHS, MRI);
551 if (!RHSImm || *RHSImm != 0)
552 return false;
553
554 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
555 if (RB.getID() != AArch64::GPRRegBankID)
556 return false;
557
558 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
559 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
560 return false;
561
562 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
563 unsigned CBOpc = 0;
564 if (CmpWidth <= 32)
565 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
566 else if (CmpWidth == 64)
567 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
568 else
569 return false;
570
571 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
572 .addUse(LHS)
573 .addMBB(DestMBB);
574
575 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
576 I.eraseFromParent();
577 return true;
578}
579
Tim Northovere9600d82017-02-08 17:57:27 +0000580bool AArch64InstructionSelector::selectVaStartAAPCS(
581 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
582 return false;
583}
584
585bool AArch64InstructionSelector::selectVaStartDarwin(
586 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
587 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
588 unsigned ListReg = I.getOperand(0).getReg();
589
590 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
591
592 auto MIB =
593 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
594 .addDef(ArgsAddrReg)
595 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
596 .addImm(0)
597 .addImm(0);
598
599 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
600
601 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
602 .addUse(ArgsAddrReg)
603 .addUse(ListReg)
604 .addImm(0)
605 .addMemOperand(*I.memoperands_begin());
606
607 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
608 I.eraseFromParent();
609 return true;
610}
611
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000612bool AArch64InstructionSelector::select(MachineInstr &I) const {
613 assert(I.getParent() && "Instruction should be in a basic block!");
614 assert(I.getParent()->getParent() && "Instruction should be in a function!");
615
616 MachineBasicBlock &MBB = *I.getParent();
617 MachineFunction &MF = *MBB.getParent();
618 MachineRegisterInfo &MRI = MF.getRegInfo();
619
Tim Northovercdf23f12016-10-31 18:30:59 +0000620 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000621 // G_PHI requires same handling as PHI
622 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000623 // Certain non-generic instructions also need some special handling.
624
625 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
626 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000627
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000628 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000629 const unsigned DefReg = I.getOperand(0).getReg();
630 const LLT DefTy = MRI.getType(DefReg);
631
632 const TargetRegisterClass *DefRC = nullptr;
633 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
634 DefRC = TRI.getRegClass(DefReg);
635 } else {
636 const RegClassOrRegBank &RegClassOrBank =
637 MRI.getRegClassOrRegBank(DefReg);
638
639 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
640 if (!DefRC) {
641 if (!DefTy.isValid()) {
642 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
643 return false;
644 }
645 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
646 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
647 if (!DefRC) {
648 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
649 return false;
650 }
651 }
652 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000653 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000654
655 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
656 }
657
658 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000659 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000660
661 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000662 }
663
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000664
665 if (I.getNumOperands() != I.getNumExplicitOperands()) {
666 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
667 return false;
668 }
669
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000670 if (selectImpl(I))
671 return true;
672
Tim Northover32a078a2016-09-15 10:09:59 +0000673 LLT Ty =
674 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000675
Tim Northover69271c62016-10-12 22:49:11 +0000676 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000677 case TargetOpcode::G_BRCOND: {
678 if (Ty.getSizeInBits() > 32) {
679 // We shouldn't need this on AArch64, but it would be implemented as an
680 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
681 // bit being tested is < 32.
682 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
683 << ", expected at most 32-bits");
684 return false;
685 }
686
687 const unsigned CondReg = I.getOperand(0).getReg();
688 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
689
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000690 if (selectCompareBranch(I, MF, MRI))
691 return true;
692
Tim Northover5e3dbf32016-10-12 22:49:01 +0000693 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
694 .addUse(CondReg)
695 .addImm(/*bit offset=*/0)
696 .addMBB(DestMBB);
697
698 I.eraseFromParent();
699 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
700 }
701
Kristof Beyls65a12c02017-01-30 09:13:18 +0000702 case TargetOpcode::G_BRINDIRECT: {
703 I.setDesc(TII.get(AArch64::BR));
704 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
705 }
706
Tim Northover4494d692016-10-18 19:47:57 +0000707 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000708 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000709 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
710
711 const LLT s32 = LLT::scalar(32);
712 const LLT s64 = LLT::scalar(64);
713 const LLT p0 = LLT::pointer(0, 64);
714
715 const unsigned DefReg = I.getOperand(0).getReg();
716 const LLT DefTy = MRI.getType(DefReg);
717 const unsigned DefSize = DefTy.getSizeInBits();
718 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
719
720 // FIXME: Redundant check, but even less readable when factored out.
721 if (isFP) {
722 if (Ty != s32 && Ty != s64) {
723 DEBUG(dbgs() << "Unable to materialize FP " << Ty
724 << " constant, expected: " << s32 << " or " << s64
725 << '\n');
726 return false;
727 }
728
729 if (RB.getID() != AArch64::FPRRegBankID) {
730 DEBUG(dbgs() << "Unable to materialize FP " << Ty
731 << " constant on bank: " << RB << ", expected: FPR\n");
732 return false;
733 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000734
735 // The case when we have 0.0 is covered by tablegen. Reject it here so we
736 // can be sure tablegen works correctly and isn't rescued by this code.
737 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
738 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000739 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000740 // s32 and s64 are covered by tablegen.
741 if (Ty != p0) {
Tim Northover4494d692016-10-18 19:47:57 +0000742 DEBUG(dbgs() << "Unable to materialize integer " << Ty
743 << " constant, expected: " << s32 << ", " << s64 << ", or "
744 << p0 << '\n');
745 return false;
746 }
747
748 if (RB.getID() != AArch64::GPRRegBankID) {
749 DEBUG(dbgs() << "Unable to materialize integer " << Ty
750 << " constant on bank: " << RB << ", expected: GPR\n");
751 return false;
752 }
753 }
754
755 const unsigned MovOpc =
756 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
757
758 I.setDesc(TII.get(MovOpc));
759
760 if (isFP) {
761 const TargetRegisterClass &GPRRC =
762 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
763 const TargetRegisterClass &FPRRC =
764 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
765
766 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
767 MachineOperand &RegOp = I.getOperand(0);
768 RegOp.setReg(DefGPRReg);
769
770 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
771 TII.get(AArch64::COPY))
772 .addDef(DefReg)
773 .addUse(DefGPRReg);
774
775 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
776 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
777 return false;
778 }
779
780 MachineOperand &ImmOp = I.getOperand(1);
781 // FIXME: Is going through int64_t always correct?
782 ImmOp.ChangeToImmediate(
783 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000784 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000785 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
786 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000787 } else if (I.getOperand(1).isImm()) {
788 uint64_t Val = I.getOperand(1).getImm();
789 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000790 }
791
792 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
793 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000794 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000795 case TargetOpcode::G_EXTRACT: {
796 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
797 // Larger extracts are vectors, same-size extracts should be something else
798 // by now (either split up or simplified to a COPY).
799 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
800 return false;
801
802 I.setDesc(TII.get(AArch64::UBFMXri));
803 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
804 Ty.getSizeInBits() - 1);
805
806 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
807 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
808 TII.get(AArch64::COPY))
809 .addDef(I.getOperand(0).getReg())
810 .addUse(DstReg, 0, AArch64::sub_32);
811 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
812 AArch64::GPR32RegClass, MRI);
813 I.getOperand(0).setReg(DstReg);
814
815 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
816 }
817
818 case TargetOpcode::G_INSERT: {
819 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
820 // Larger inserts are vectors, same-size ones should be something else by
821 // now (split up or turned into COPYs).
822 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
823 return false;
824
825 I.setDesc(TII.get(AArch64::BFMXri));
826 unsigned LSB = I.getOperand(3).getImm();
827 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
828 I.getOperand(3).setImm((64 - LSB) % 64);
829 MachineInstrBuilder(MF, I).addImm(Width - 1);
830
831 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
832 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
833 TII.get(AArch64::SUBREG_TO_REG))
834 .addDef(SrcReg)
835 .addImm(0)
836 .addUse(I.getOperand(2).getReg())
837 .addImm(AArch64::sub_32);
838 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
839 AArch64::GPR32RegClass, MRI);
840 I.getOperand(2).setReg(SrcReg);
841
842 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
843 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000844 case TargetOpcode::G_FRAME_INDEX: {
845 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000846 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000847 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000848 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000849 return false;
850 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000851 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000852
853 // MOs for a #0 shifted immediate.
854 I.addOperand(MachineOperand::CreateImm(0));
855 I.addOperand(MachineOperand::CreateImm(0));
856
857 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
858 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000859
860 case TargetOpcode::G_GLOBAL_VALUE: {
861 auto GV = I.getOperand(1).getGlobal();
862 if (GV->isThreadLocal()) {
863 // FIXME: we don't support TLS yet.
864 return false;
865 }
866 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000867 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000868 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000869 I.getOperand(1).setTargetFlags(OpFlags);
870 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000871 I.setDesc(TII.get(AArch64::MOVaddr));
872 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
873 MachineInstrBuilder MIB(MF, I);
874 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
875 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
876 }
877 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
878 }
879
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000880 case TargetOpcode::G_LOAD:
881 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000882 LLT MemTy = Ty;
883 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000884
Tim Northover5ae83502016-09-15 09:20:34 +0000885 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000886 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000887 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000888 return false;
889 }
890
Tim Northover48dfa1a2017-02-13 22:14:16 +0000891 auto &MemOp = **I.memoperands_begin();
892 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
893 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
894 return false;
895 }
896
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000897 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000898#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000899 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000900 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000901 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
902 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000903 assert(MRI.getType(PtrReg).isPointer() &&
904 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000905#endif
906
907 const unsigned ValReg = I.getOperand(0).getReg();
908 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
909
910 const unsigned NewOpc =
911 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
912 if (NewOpc == I.getOpcode())
913 return false;
914
915 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000916
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000917 uint64_t Offset = 0;
918 auto *PtrMI = MRI.getVRegDef(PtrReg);
919
920 // Try to fold a GEP into our unsigned immediate addressing mode.
921 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
922 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
923 int64_t Imm = *COff;
924 const unsigned Size = MemTy.getSizeInBits() / 8;
925 const unsigned Scale = Log2_32(Size);
926 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
927 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
928 I.getOperand(1).setReg(Ptr2Reg);
929 PtrMI = MRI.getVRegDef(Ptr2Reg);
930 Offset = Imm / Size;
931 }
932 }
933 }
934
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000935 // If we haven't folded anything into our addressing mode yet, try to fold
936 // a frame index into the base+offset.
937 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
938 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
939
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000940 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000941
942 // If we're storing a 0, use WZR/XZR.
943 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
944 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
945 if (I.getOpcode() == AArch64::STRWui)
946 I.getOperand(0).setReg(AArch64::WZR);
947 else if (I.getOpcode() == AArch64::STRXui)
948 I.getOperand(0).setReg(AArch64::XZR);
949 }
950 }
951
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000952 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
953 }
954
Tim Northover9dd78f82017-02-08 21:22:25 +0000955 case TargetOpcode::G_SMULH:
956 case TargetOpcode::G_UMULH: {
957 // Reject the various things we don't support yet.
958 if (unsupportedBinOp(I, RBI, MRI, TRI))
959 return false;
960
961 const unsigned DefReg = I.getOperand(0).getReg();
962 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
963
964 if (RB.getID() != AArch64::GPRRegBankID) {
965 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
966 return false;
967 }
968
969 if (Ty != LLT::scalar(64)) {
970 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
971 << ", expected: " << LLT::scalar(64) << '\n');
972 return false;
973 }
974
975 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
976 : AArch64::UMULHrr;
977 I.setDesc(TII.get(NewOpc));
978
979 // Now that we selected an opcode, we need to constrain the register
980 // operands to use appropriate classes.
981 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
982 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000983 case TargetOpcode::G_FADD:
984 case TargetOpcode::G_FSUB:
985 case TargetOpcode::G_FMUL:
986 case TargetOpcode::G_FDIV:
987
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000988 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000989 case TargetOpcode::G_SHL:
990 case TargetOpcode::G_LSHR:
991 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000992 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000993 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000994 if (unsupportedBinOp(I, RBI, MRI, TRI))
995 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000996
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000997 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000998
999 const unsigned DefReg = I.getOperand(0).getReg();
1000 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1001
1002 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1003 if (NewOpc == I.getOpcode())
1004 return false;
1005
1006 I.setDesc(TII.get(NewOpc));
1007 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001008
1009 // Now that we selected an opcode, we need to constrain the register
1010 // operands to use appropriate classes.
1011 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1012 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001013
Tim Northover398c5f52017-02-14 20:56:29 +00001014 case TargetOpcode::G_PTR_MASK: {
1015 uint64_t Align = I.getOperand(2).getImm();
1016 if (Align >= 64 || Align == 0)
1017 return false;
1018
1019 uint64_t Mask = ~((1ULL << Align) - 1);
1020 I.setDesc(TII.get(AArch64::ANDXri));
1021 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1022
1023 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1024 }
Tim Northover037af52c2016-10-31 18:31:09 +00001025 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001026 case TargetOpcode::G_TRUNC: {
1027 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1028 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1029
1030 const unsigned DstReg = I.getOperand(0).getReg();
1031 const unsigned SrcReg = I.getOperand(1).getReg();
1032
1033 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1034 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1035
1036 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001037 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001038 return false;
1039 }
1040
1041 if (DstRB.getID() == AArch64::GPRRegBankID) {
1042 const TargetRegisterClass *DstRC =
1043 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1044 if (!DstRC)
1045 return false;
1046
1047 const TargetRegisterClass *SrcRC =
1048 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1049 if (!SrcRC)
1050 return false;
1051
1052 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1053 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001054 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001055 return false;
1056 }
1057
1058 if (DstRC == SrcRC) {
1059 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001060 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1061 SrcTy == LLT::scalar(64)) {
1062 llvm_unreachable("TableGen can import this case");
1063 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001064 } else if (DstRC == &AArch64::GPR32RegClass &&
1065 SrcRC == &AArch64::GPR64RegClass) {
1066 I.getOperand(1).setSubReg(AArch64::sub_32);
1067 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001068 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001069 return false;
1070 }
1071
1072 I.setDesc(TII.get(TargetOpcode::COPY));
1073 return true;
1074 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1075 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1076 I.setDesc(TII.get(AArch64::XTNv4i16));
1077 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1078 return true;
1079 }
1080 }
1081
1082 return false;
1083 }
1084
Tim Northover3d38b3a2016-10-11 20:50:21 +00001085 case TargetOpcode::G_ANYEXT: {
1086 const unsigned DstReg = I.getOperand(0).getReg();
1087 const unsigned SrcReg = I.getOperand(1).getReg();
1088
Quentin Colombetcb629a82016-10-12 03:57:49 +00001089 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1090 if (RBDst.getID() != AArch64::GPRRegBankID) {
1091 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1092 return false;
1093 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001094
Quentin Colombetcb629a82016-10-12 03:57:49 +00001095 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1096 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1097 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001098 return false;
1099 }
1100
1101 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1102
1103 if (DstSize == 0) {
1104 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1105 return false;
1106 }
1107
Quentin Colombetcb629a82016-10-12 03:57:49 +00001108 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001109 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1110 << ", expected: 32 or 64\n");
1111 return false;
1112 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001113 // At this point G_ANYEXT is just like a plain COPY, but we need
1114 // to explicitly form the 64-bit value if any.
1115 if (DstSize > 32) {
1116 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1117 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1118 .addDef(ExtSrc)
1119 .addImm(0)
1120 .addUse(SrcReg)
1121 .addImm(AArch64::sub_32);
1122 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001123 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001124 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001125 }
1126
1127 case TargetOpcode::G_ZEXT:
1128 case TargetOpcode::G_SEXT: {
1129 unsigned Opcode = I.getOpcode();
1130 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1131 SrcTy = MRI.getType(I.getOperand(1).getReg());
1132 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1133 const unsigned DefReg = I.getOperand(0).getReg();
1134 const unsigned SrcReg = I.getOperand(1).getReg();
1135 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1136
1137 if (RB.getID() != AArch64::GPRRegBankID) {
1138 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1139 << ", expected: GPR\n");
1140 return false;
1141 }
1142
1143 MachineInstr *ExtI;
1144 if (DstTy == LLT::scalar(64)) {
1145 // FIXME: Can we avoid manually doing this?
1146 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1147 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1148 << " operand\n");
1149 return false;
1150 }
1151
1152 const unsigned SrcXReg =
1153 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1154 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1155 .addDef(SrcXReg)
1156 .addImm(0)
1157 .addUse(SrcReg)
1158 .addImm(AArch64::sub_32);
1159
1160 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1161 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1162 .addDef(DefReg)
1163 .addUse(SrcXReg)
1164 .addImm(0)
1165 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001166 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001167 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1168 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1169 .addDef(DefReg)
1170 .addUse(SrcReg)
1171 .addImm(0)
1172 .addImm(SrcTy.getSizeInBits() - 1);
1173 } else {
1174 return false;
1175 }
1176
1177 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1178
1179 I.eraseFromParent();
1180 return true;
1181 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001182
Tim Northover69271c62016-10-12 22:49:11 +00001183 case TargetOpcode::G_SITOFP:
1184 case TargetOpcode::G_UITOFP:
1185 case TargetOpcode::G_FPTOSI:
1186 case TargetOpcode::G_FPTOUI: {
1187 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1188 SrcTy = MRI.getType(I.getOperand(1).getReg());
1189 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1190 if (NewOpc == Opcode)
1191 return false;
1192
1193 I.setDesc(TII.get(NewOpc));
1194 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1195
1196 return true;
1197 }
1198
1199
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001200 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001201 // The importer is currently unable to import pointer types since they
1202 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001203 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001204
Daniel Sandersedd07842017-08-17 09:26:14 +00001205 case TargetOpcode::G_BITCAST:
1206 // Imported SelectionDAG rules can handle every bitcast except those that
1207 // bitcast from a type to the same type. Ideally, these shouldn't occur
1208 // but we might not run an optimizer that deletes them.
1209 if (MRI.getType(I.getOperand(0).getReg()) ==
1210 MRI.getType(I.getOperand(1).getReg()))
1211 return selectCopy(I, TII, MRI, TRI, RBI);
1212 return false;
1213
Tim Northover9ac0eba2016-11-08 00:45:29 +00001214 case TargetOpcode::G_SELECT: {
1215 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1216 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1217 << ", expected: " << LLT::scalar(1) << '\n');
1218 return false;
1219 }
1220
1221 const unsigned CondReg = I.getOperand(1).getReg();
1222 const unsigned TReg = I.getOperand(2).getReg();
1223 const unsigned FReg = I.getOperand(3).getReg();
1224
1225 unsigned CSelOpc = 0;
1226
1227 if (Ty == LLT::scalar(32)) {
1228 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001229 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001230 CSelOpc = AArch64::CSELXr;
1231 } else {
1232 return false;
1233 }
1234
1235 MachineInstr &TstMI =
1236 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1237 .addDef(AArch64::WZR)
1238 .addUse(CondReg)
1239 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1240
1241 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1242 .addDef(I.getOperand(0).getReg())
1243 .addUse(TReg)
1244 .addUse(FReg)
1245 .addImm(AArch64CC::NE);
1246
1247 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1248 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1249
1250 I.eraseFromParent();
1251 return true;
1252 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001253 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001254 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001255 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001256 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001257 return false;
1258 }
1259
1260 unsigned CmpOpc = 0;
1261 unsigned ZReg = 0;
1262
1263 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1264 if (CmpTy == LLT::scalar(32)) {
1265 CmpOpc = AArch64::SUBSWrr;
1266 ZReg = AArch64::WZR;
1267 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1268 CmpOpc = AArch64::SUBSXrr;
1269 ZReg = AArch64::XZR;
1270 } else {
1271 return false;
1272 }
1273
Kristof Beyls22524402017-01-05 10:16:08 +00001274 // CSINC increments the result by one when the condition code is false.
1275 // Therefore, we have to invert the predicate to get an increment by 1 when
1276 // the predicate is true.
1277 const AArch64CC::CondCode invCC =
1278 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1279 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001280
1281 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1282 .addDef(ZReg)
1283 .addUse(I.getOperand(2).getReg())
1284 .addUse(I.getOperand(3).getReg());
1285
1286 MachineInstr &CSetMI =
1287 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1288 .addDef(I.getOperand(0).getReg())
1289 .addUse(AArch64::WZR)
1290 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001291 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001292
1293 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1294 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1295
1296 I.eraseFromParent();
1297 return true;
1298 }
1299
Tim Northover7dd378d2016-10-12 22:49:07 +00001300 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001301 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001302 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001303 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001304 return false;
1305 }
1306
1307 unsigned CmpOpc = 0;
1308 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1309 if (CmpTy == LLT::scalar(32)) {
1310 CmpOpc = AArch64::FCMPSrr;
1311 } else if (CmpTy == LLT::scalar(64)) {
1312 CmpOpc = AArch64::FCMPDrr;
1313 } else {
1314 return false;
1315 }
1316
1317 // FIXME: regbank
1318
1319 AArch64CC::CondCode CC1, CC2;
1320 changeFCMPPredToAArch64CC(
1321 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1322
1323 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1324 .addUse(I.getOperand(2).getReg())
1325 .addUse(I.getOperand(3).getReg());
1326
1327 const unsigned DefReg = I.getOperand(0).getReg();
1328 unsigned Def1Reg = DefReg;
1329 if (CC2 != AArch64CC::AL)
1330 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1331
1332 MachineInstr &CSetMI =
1333 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1334 .addDef(Def1Reg)
1335 .addUse(AArch64::WZR)
1336 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001337 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001338
1339 if (CC2 != AArch64CC::AL) {
1340 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1341 MachineInstr &CSet2MI =
1342 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1343 .addDef(Def2Reg)
1344 .addUse(AArch64::WZR)
1345 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001346 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001347 MachineInstr &OrMI =
1348 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1349 .addDef(DefReg)
1350 .addUse(Def1Reg)
1351 .addUse(Def2Reg);
1352 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1353 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1354 }
1355
1356 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1357 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1358
1359 I.eraseFromParent();
1360 return true;
1361 }
Tim Northovere9600d82017-02-08 17:57:27 +00001362 case TargetOpcode::G_VASTART:
1363 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1364 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001365 case TargetOpcode::G_IMPLICIT_DEF:
1366 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1367 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001368 }
1369
1370 return false;
1371}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001372
1373/// SelectArithImmed - Select an immediate value that can be represented as
1374/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1375/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders2deea182017-04-22 15:11:04 +00001376InstructionSelector::ComplexRendererFn
1377AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001378 MachineInstr &MI = *Root.getParent();
1379 MachineBasicBlock &MBB = *MI.getParent();
1380 MachineFunction &MF = *MBB.getParent();
1381 MachineRegisterInfo &MRI = MF.getRegInfo();
1382
1383 // This function is called from the addsub_shifted_imm ComplexPattern,
1384 // which lists [imm] as the list of opcode it's interested in, however
1385 // we still need to check whether the operand is actually an immediate
1386 // here because the ComplexPattern opcode list is only used in
1387 // root-level opcode matching.
1388 uint64_t Immed;
1389 if (Root.isImm())
1390 Immed = Root.getImm();
1391 else if (Root.isCImm())
1392 Immed = Root.getCImm()->getZExtValue();
1393 else if (Root.isReg()) {
1394 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1395 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001396 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001397 MachineOperand &Op1 = Def->getOperand(1);
1398 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001399 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001400 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001401 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001402 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001403
1404 unsigned ShiftAmt;
1405
1406 if (Immed >> 12 == 0) {
1407 ShiftAmt = 0;
1408 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1409 ShiftAmt = 12;
1410 Immed = Immed >> 12;
1411 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001412 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001413
1414 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001415 return {{
1416 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1417 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1418 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001419}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001420
Daniel Sandersea8711b2017-10-16 03:36:29 +00001421/// Select a "register plus unscaled signed 9-bit immediate" address. This
1422/// should only match when there is an offset that is not valid for a scaled
1423/// immediate addressing mode. The "Size" argument is the size in bytes of the
1424/// memory reference, which is needed here to know what is valid for a scaled
1425/// immediate.
1426InstructionSelector::ComplexRendererFn
1427AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1428 unsigned Size) const {
1429 MachineRegisterInfo &MRI =
1430 Root.getParent()->getParent()->getParent()->getRegInfo();
1431
1432 if (!Root.isReg())
1433 return None;
1434
1435 if (!isBaseWithConstantOffset(Root, MRI))
1436 return None;
1437
1438 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1439 if (!RootDef)
1440 return None;
1441
1442 MachineOperand &OffImm = RootDef->getOperand(2);
1443 if (!OffImm.isReg())
1444 return None;
1445 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1446 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1447 return None;
1448 int64_t RHSC;
1449 MachineOperand &RHSOp1 = RHS->getOperand(1);
1450 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1451 return None;
1452 RHSC = RHSOp1.getCImm()->getSExtValue();
1453
1454 // If the offset is valid as a scaled immediate, don't match here.
1455 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1456 return None;
1457 if (RHSC >= -256 && RHSC < 256) {
1458 MachineOperand &Base = RootDef->getOperand(1);
1459 return {{
1460 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1461 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1462 }};
1463 }
1464 return None;
1465}
1466
1467/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1468/// "Size" argument is the size in bytes of the memory reference, which
1469/// determines the scale.
1470InstructionSelector::ComplexRendererFn
1471AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1472 unsigned Size) const {
1473 MachineRegisterInfo &MRI =
1474 Root.getParent()->getParent()->getParent()->getRegInfo();
1475
1476 if (!Root.isReg())
1477 return None;
1478
1479 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1480 if (!RootDef)
1481 return None;
1482
1483 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1484 return {{
1485 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1486 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1487 }};
1488 }
1489
1490 if (isBaseWithConstantOffset(Root, MRI)) {
1491 MachineOperand &LHS = RootDef->getOperand(1);
1492 MachineOperand &RHS = RootDef->getOperand(2);
1493 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1494 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1495 if (LHSDef && RHSDef) {
1496 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1497 unsigned Scale = Log2_32(Size);
1498 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1499 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1500 LHSDef = MRI.getVRegDef(LHSDef->getOperand(1).getReg());
1501 return {{
1502 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1503 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1504 }};
1505 }
1506 }
1507 }
1508
1509 // Before falling back to our general case, check if the unscaled
1510 // instructions can handle this. If so, that's preferable.
1511 if (selectAddrModeUnscaled(Root, Size).hasValue())
1512 return None;
1513
1514 return {{
1515 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1516 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1517 }};
1518}
1519
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001520namespace llvm {
1521InstructionSelector *
1522createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1523 AArch64Subtarget &Subtarget,
1524 AArch64RegisterBankInfo &RBI) {
1525 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1526}
1527}