blob: be0f6eb44d1d6df3219e999609c28b4502076b42 [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
15#include "AArch64InstructionSelector.h"
16#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000017#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000018#include "AArch64RegisterBankInfo.h"
19#include "AArch64RegisterInfo.h"
20#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000021#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000022#include "MCTargetDesc/AArch64AddressingModes.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/IR/Type.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
31
32#define DEBUG_TYPE "aarch64-isel"
33
34using namespace llvm;
35
36#ifndef LLVM_BUILD_GLOBAL_ISEL
37#error "You shouldn't build this"
38#endif
39
Daniel Sanders8a4bae92017-03-14 21:32:08 +000040#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000041#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000042#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000043
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000044AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000045 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
46 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000047 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
48 TRI(*STI.getRegisterInfo()), RBI(RBI)
49#define GET_GLOBALISEL_TEMPORARIES_INIT
50#include "AArch64GenGlobalISel.inc"
51#undef GET_GLOBALISEL_TEMPORARIES_INIT
52{
53}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000054
Tim Northoverfb8d9892016-10-12 22:49:15 +000055// FIXME: This should be target-independent, inferred from the types declared
56// for each class in the bank.
57static const TargetRegisterClass *
58getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
59 const RegisterBankInfo &RBI) {
60 if (RB.getID() == AArch64::GPRRegBankID) {
61 if (Ty.getSizeInBits() <= 32)
62 return &AArch64::GPR32RegClass;
63 if (Ty.getSizeInBits() == 64)
64 return &AArch64::GPR64RegClass;
65 return nullptr;
66 }
67
68 if (RB.getID() == AArch64::FPRRegBankID) {
69 if (Ty.getSizeInBits() == 32)
70 return &AArch64::FPR32RegClass;
71 if (Ty.getSizeInBits() == 64)
72 return &AArch64::FPR64RegClass;
73 if (Ty.getSizeInBits() == 128)
74 return &AArch64::FPR128RegClass;
75 return nullptr;
76 }
77
78 return nullptr;
79}
80
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000081/// Check whether \p I is a currently unsupported binary operation:
82/// - it has an unsized type
83/// - an operand is not a vreg
84/// - all operands are not in the same bank
85/// These are checks that should someday live in the verifier, but right now,
86/// these are mostly limitations of the aarch64 selector.
87static bool unsupportedBinOp(const MachineInstr &I,
88 const AArch64RegisterBankInfo &RBI,
89 const MachineRegisterInfo &MRI,
90 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000091 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000092 if (!Ty.isValid()) {
93 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000094 return true;
95 }
96
97 const RegisterBank *PrevOpBank = nullptr;
98 for (auto &MO : I.operands()) {
99 // FIXME: Support non-register operands.
100 if (!MO.isReg()) {
101 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
102 return true;
103 }
104
105 // FIXME: Can generic operations have physical registers operands? If
106 // so, this will need to be taught about that, and we'll need to get the
107 // bank out of the minimal class for the register.
108 // Either way, this needs to be documented (and possibly verified).
109 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
110 DEBUG(dbgs() << "Generic inst has physical register operand\n");
111 return true;
112 }
113
114 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
115 if (!OpBank) {
116 DEBUG(dbgs() << "Generic register has no bank or class\n");
117 return true;
118 }
119
120 if (PrevOpBank && OpBank != PrevOpBank) {
121 DEBUG(dbgs() << "Generic inst operands have different banks\n");
122 return true;
123 }
124 PrevOpBank = OpBank;
125 }
126 return false;
127}
128
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000129/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000130/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000131/// and of size \p OpSize.
132/// \returns \p GenericOpc if the combination is unsupported.
133static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
134 unsigned OpSize) {
135 switch (RegBankID) {
136 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000137 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000138 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000139 case TargetOpcode::G_SHL:
140 return AArch64::LSLVWr;
141 case TargetOpcode::G_LSHR:
142 return AArch64::LSRVWr;
143 case TargetOpcode::G_ASHR:
144 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000145 default:
146 return GenericOpc;
147 }
Tim Northover55782222016-10-18 20:03:48 +0000148 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000149 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000150 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000151 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000152 case TargetOpcode::G_SHL:
153 return AArch64::LSLVXr;
154 case TargetOpcode::G_LSHR:
155 return AArch64::LSRVXr;
156 case TargetOpcode::G_ASHR:
157 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000158 default:
159 return GenericOpc;
160 }
161 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000162 case AArch64::FPRRegBankID:
163 switch (OpSize) {
164 case 32:
165 switch (GenericOpc) {
166 case TargetOpcode::G_FADD:
167 return AArch64::FADDSrr;
168 case TargetOpcode::G_FSUB:
169 return AArch64::FSUBSrr;
170 case TargetOpcode::G_FMUL:
171 return AArch64::FMULSrr;
172 case TargetOpcode::G_FDIV:
173 return AArch64::FDIVSrr;
174 default:
175 return GenericOpc;
176 }
177 case 64:
178 switch (GenericOpc) {
179 case TargetOpcode::G_FADD:
180 return AArch64::FADDDrr;
181 case TargetOpcode::G_FSUB:
182 return AArch64::FSUBDrr;
183 case TargetOpcode::G_FMUL:
184 return AArch64::FMULDrr;
185 case TargetOpcode::G_FDIV:
186 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000187 case TargetOpcode::G_OR:
188 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000189 default:
190 return GenericOpc;
191 }
192 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 };
194 return GenericOpc;
195}
196
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000197/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
198/// appropriate for the (value) register bank \p RegBankID and of memory access
199/// size \p OpSize. This returns the variant with the base+unsigned-immediate
200/// addressing mode (e.g., LDRXui).
201/// \returns \p GenericOpc if the combination is unsupported.
202static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
203 unsigned OpSize) {
204 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
205 switch (RegBankID) {
206 case AArch64::GPRRegBankID:
207 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000208 case 8:
209 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
210 case 16:
211 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000212 case 32:
213 return isStore ? AArch64::STRWui : AArch64::LDRWui;
214 case 64:
215 return isStore ? AArch64::STRXui : AArch64::LDRXui;
216 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000217 case AArch64::FPRRegBankID:
218 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000219 case 8:
220 return isStore ? AArch64::STRBui : AArch64::LDRBui;
221 case 16:
222 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000223 case 32:
224 return isStore ? AArch64::STRSui : AArch64::LDRSui;
225 case 64:
226 return isStore ? AArch64::STRDui : AArch64::LDRDui;
227 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000228 };
229 return GenericOpc;
230}
231
Quentin Colombetcb629a82016-10-12 03:57:49 +0000232static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
233 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
234 const RegisterBankInfo &RBI) {
235
236 unsigned DstReg = I.getOperand(0).getReg();
237 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
238 assert(I.isCopy() && "Generic operators do not allow physical registers");
239 return true;
240 }
241
242 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
243 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
244 unsigned SrcReg = I.getOperand(1).getReg();
245 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
246 (void)SrcSize;
247 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
248 "No phys reg on generic operators");
249 assert(
250 (DstSize == SrcSize ||
251 // Copies are a mean to setup initial types, the number of
252 // bits may not exactly match.
253 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
254 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
255 // Copies are a mean to copy bits around, as long as we are
256 // on the same register class, that's fine. Otherwise, that
257 // means we need some SUBREG_TO_REG or AND & co.
258 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
259 "Copy with different width?!");
260 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
261 "GPRs cannot get more than 64-bit width values");
262 const TargetRegisterClass *RC = nullptr;
263
264 if (RegBank.getID() == AArch64::FPRRegBankID) {
265 if (DstSize <= 32)
266 RC = &AArch64::FPR32RegClass;
267 else if (DstSize <= 64)
268 RC = &AArch64::FPR64RegClass;
269 else if (DstSize <= 128)
270 RC = &AArch64::FPR128RegClass;
271 else {
272 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
273 return false;
274 }
275 } else {
276 assert(RegBank.getID() == AArch64::GPRRegBankID &&
277 "Bitcast for the flags?");
278 RC =
279 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
280 }
281
282 // No need to constrain SrcReg. It will get constrained when
283 // we hit another of its use or its defs.
284 // Copies do not have constraints.
285 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
286 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
287 << " operand\n");
288 return false;
289 }
290 I.setDesc(TII.get(AArch64::COPY));
291 return true;
292}
293
Tim Northover69271c62016-10-12 22:49:11 +0000294static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
295 if (!DstTy.isScalar() || !SrcTy.isScalar())
296 return GenericOpc;
297
298 const unsigned DstSize = DstTy.getSizeInBits();
299 const unsigned SrcSize = SrcTy.getSizeInBits();
300
301 switch (DstSize) {
302 case 32:
303 switch (SrcSize) {
304 case 32:
305 switch (GenericOpc) {
306 case TargetOpcode::G_SITOFP:
307 return AArch64::SCVTFUWSri;
308 case TargetOpcode::G_UITOFP:
309 return AArch64::UCVTFUWSri;
310 case TargetOpcode::G_FPTOSI:
311 return AArch64::FCVTZSUWSr;
312 case TargetOpcode::G_FPTOUI:
313 return AArch64::FCVTZUUWSr;
314 default:
315 return GenericOpc;
316 }
317 case 64:
318 switch (GenericOpc) {
319 case TargetOpcode::G_SITOFP:
320 return AArch64::SCVTFUXSri;
321 case TargetOpcode::G_UITOFP:
322 return AArch64::UCVTFUXSri;
323 case TargetOpcode::G_FPTOSI:
324 return AArch64::FCVTZSUWDr;
325 case TargetOpcode::G_FPTOUI:
326 return AArch64::FCVTZUUWDr;
327 default:
328 return GenericOpc;
329 }
330 default:
331 return GenericOpc;
332 }
333 case 64:
334 switch (SrcSize) {
335 case 32:
336 switch (GenericOpc) {
337 case TargetOpcode::G_SITOFP:
338 return AArch64::SCVTFUWDri;
339 case TargetOpcode::G_UITOFP:
340 return AArch64::UCVTFUWDri;
341 case TargetOpcode::G_FPTOSI:
342 return AArch64::FCVTZSUXSr;
343 case TargetOpcode::G_FPTOUI:
344 return AArch64::FCVTZUUXSr;
345 default:
346 return GenericOpc;
347 }
348 case 64:
349 switch (GenericOpc) {
350 case TargetOpcode::G_SITOFP:
351 return AArch64::SCVTFUXDri;
352 case TargetOpcode::G_UITOFP:
353 return AArch64::UCVTFUXDri;
354 case TargetOpcode::G_FPTOSI:
355 return AArch64::FCVTZSUXDr;
356 case TargetOpcode::G_FPTOUI:
357 return AArch64::FCVTZUUXDr;
358 default:
359 return GenericOpc;
360 }
361 default:
362 return GenericOpc;
363 }
364 default:
365 return GenericOpc;
366 };
367 return GenericOpc;
368}
369
Tim Northover6c02ad52016-10-12 22:49:04 +0000370static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
371 switch (P) {
372 default:
373 llvm_unreachable("Unknown condition code!");
374 case CmpInst::ICMP_NE:
375 return AArch64CC::NE;
376 case CmpInst::ICMP_EQ:
377 return AArch64CC::EQ;
378 case CmpInst::ICMP_SGT:
379 return AArch64CC::GT;
380 case CmpInst::ICMP_SGE:
381 return AArch64CC::GE;
382 case CmpInst::ICMP_SLT:
383 return AArch64CC::LT;
384 case CmpInst::ICMP_SLE:
385 return AArch64CC::LE;
386 case CmpInst::ICMP_UGT:
387 return AArch64CC::HI;
388 case CmpInst::ICMP_UGE:
389 return AArch64CC::HS;
390 case CmpInst::ICMP_ULT:
391 return AArch64CC::LO;
392 case CmpInst::ICMP_ULE:
393 return AArch64CC::LS;
394 }
395}
396
Tim Northover7dd378d2016-10-12 22:49:07 +0000397static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
398 AArch64CC::CondCode &CondCode,
399 AArch64CC::CondCode &CondCode2) {
400 CondCode2 = AArch64CC::AL;
401 switch (P) {
402 default:
403 llvm_unreachable("Unknown FP condition!");
404 case CmpInst::FCMP_OEQ:
405 CondCode = AArch64CC::EQ;
406 break;
407 case CmpInst::FCMP_OGT:
408 CondCode = AArch64CC::GT;
409 break;
410 case CmpInst::FCMP_OGE:
411 CondCode = AArch64CC::GE;
412 break;
413 case CmpInst::FCMP_OLT:
414 CondCode = AArch64CC::MI;
415 break;
416 case CmpInst::FCMP_OLE:
417 CondCode = AArch64CC::LS;
418 break;
419 case CmpInst::FCMP_ONE:
420 CondCode = AArch64CC::MI;
421 CondCode2 = AArch64CC::GT;
422 break;
423 case CmpInst::FCMP_ORD:
424 CondCode = AArch64CC::VC;
425 break;
426 case CmpInst::FCMP_UNO:
427 CondCode = AArch64CC::VS;
428 break;
429 case CmpInst::FCMP_UEQ:
430 CondCode = AArch64CC::EQ;
431 CondCode2 = AArch64CC::VS;
432 break;
433 case CmpInst::FCMP_UGT:
434 CondCode = AArch64CC::HI;
435 break;
436 case CmpInst::FCMP_UGE:
437 CondCode = AArch64CC::PL;
438 break;
439 case CmpInst::FCMP_ULT:
440 CondCode = AArch64CC::LT;
441 break;
442 case CmpInst::FCMP_ULE:
443 CondCode = AArch64CC::LE;
444 break;
445 case CmpInst::FCMP_UNE:
446 CondCode = AArch64CC::NE;
447 break;
448 }
449}
450
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000451bool AArch64InstructionSelector::selectCompareBranch(
452 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
453
454 const unsigned CondReg = I.getOperand(0).getReg();
455 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
456 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
457 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
458 return false;
459
460 unsigned LHS = CCMI->getOperand(2).getReg();
461 unsigned RHS = CCMI->getOperand(3).getReg();
462 if (!getConstantVRegVal(RHS, MRI))
463 std::swap(RHS, LHS);
464
465 const auto RHSImm = getConstantVRegVal(RHS, MRI);
466 if (!RHSImm || *RHSImm != 0)
467 return false;
468
469 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
470 if (RB.getID() != AArch64::GPRRegBankID)
471 return false;
472
473 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
474 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
475 return false;
476
477 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
478 unsigned CBOpc = 0;
479 if (CmpWidth <= 32)
480 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
481 else if (CmpWidth == 64)
482 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
483 else
484 return false;
485
486 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
487 .addUse(LHS)
488 .addMBB(DestMBB);
489
490 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
491 I.eraseFromParent();
492 return true;
493}
494
Tim Northovere9600d82017-02-08 17:57:27 +0000495bool AArch64InstructionSelector::selectVaStartAAPCS(
496 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
497 return false;
498}
499
500bool AArch64InstructionSelector::selectVaStartDarwin(
501 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
502 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
503 unsigned ListReg = I.getOperand(0).getReg();
504
505 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
506
507 auto MIB =
508 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
509 .addDef(ArgsAddrReg)
510 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
511 .addImm(0)
512 .addImm(0);
513
514 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
515
516 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
517 .addUse(ArgsAddrReg)
518 .addUse(ListReg)
519 .addImm(0)
520 .addMemOperand(*I.memoperands_begin());
521
522 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
523 I.eraseFromParent();
524 return true;
525}
526
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000527bool AArch64InstructionSelector::select(MachineInstr &I) const {
528 assert(I.getParent() && "Instruction should be in a basic block!");
529 assert(I.getParent()->getParent() && "Instruction should be in a function!");
530
531 MachineBasicBlock &MBB = *I.getParent();
532 MachineFunction &MF = *MBB.getParent();
533 MachineRegisterInfo &MRI = MF.getRegInfo();
534
Tim Northovercdf23f12016-10-31 18:30:59 +0000535 unsigned Opcode = I.getOpcode();
536 if (!isPreISelGenericOpcode(I.getOpcode())) {
537 // Certain non-generic instructions also need some special handling.
538
539 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
540 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000541
542 if (Opcode == TargetOpcode::PHI) {
543 const unsigned DefReg = I.getOperand(0).getReg();
544 const LLT DefTy = MRI.getType(DefReg);
545
546 const TargetRegisterClass *DefRC = nullptr;
547 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
548 DefRC = TRI.getRegClass(DefReg);
549 } else {
550 const RegClassOrRegBank &RegClassOrBank =
551 MRI.getRegClassOrRegBank(DefReg);
552
553 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
554 if (!DefRC) {
555 if (!DefTy.isValid()) {
556 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
557 return false;
558 }
559 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
560 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
561 if (!DefRC) {
562 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
563 return false;
564 }
565 }
566 }
567
568 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
569 }
570
571 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000572 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000573
574 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000575 }
576
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000577
578 if (I.getNumOperands() != I.getNumExplicitOperands()) {
579 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
580 return false;
581 }
582
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000583 if (selectImpl(I))
584 return true;
585
Tim Northover32a078a2016-09-15 10:09:59 +0000586 LLT Ty =
587 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000588
Tim Northover69271c62016-10-12 22:49:11 +0000589 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000590 case TargetOpcode::G_BRCOND: {
591 if (Ty.getSizeInBits() > 32) {
592 // We shouldn't need this on AArch64, but it would be implemented as an
593 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
594 // bit being tested is < 32.
595 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
596 << ", expected at most 32-bits");
597 return false;
598 }
599
600 const unsigned CondReg = I.getOperand(0).getReg();
601 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
602
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000603 if (selectCompareBranch(I, MF, MRI))
604 return true;
605
Tim Northover5e3dbf32016-10-12 22:49:01 +0000606 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
607 .addUse(CondReg)
608 .addImm(/*bit offset=*/0)
609 .addMBB(DestMBB);
610
611 I.eraseFromParent();
612 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
613 }
614
Kristof Beyls65a12c02017-01-30 09:13:18 +0000615 case TargetOpcode::G_BRINDIRECT: {
616 I.setDesc(TII.get(AArch64::BR));
617 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
618 }
619
Tim Northover4494d692016-10-18 19:47:57 +0000620 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000621 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000622 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
623
624 const LLT s32 = LLT::scalar(32);
625 const LLT s64 = LLT::scalar(64);
626 const LLT p0 = LLT::pointer(0, 64);
627
628 const unsigned DefReg = I.getOperand(0).getReg();
629 const LLT DefTy = MRI.getType(DefReg);
630 const unsigned DefSize = DefTy.getSizeInBits();
631 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
632
633 // FIXME: Redundant check, but even less readable when factored out.
634 if (isFP) {
635 if (Ty != s32 && Ty != s64) {
636 DEBUG(dbgs() << "Unable to materialize FP " << Ty
637 << " constant, expected: " << s32 << " or " << s64
638 << '\n');
639 return false;
640 }
641
642 if (RB.getID() != AArch64::FPRRegBankID) {
643 DEBUG(dbgs() << "Unable to materialize FP " << Ty
644 << " constant on bank: " << RB << ", expected: FPR\n");
645 return false;
646 }
647 } else {
648 if (Ty != s32 && Ty != s64 && Ty != p0) {
649 DEBUG(dbgs() << "Unable to materialize integer " << Ty
650 << " constant, expected: " << s32 << ", " << s64 << ", or "
651 << p0 << '\n');
652 return false;
653 }
654
655 if (RB.getID() != AArch64::GPRRegBankID) {
656 DEBUG(dbgs() << "Unable to materialize integer " << Ty
657 << " constant on bank: " << RB << ", expected: GPR\n");
658 return false;
659 }
660 }
661
662 const unsigned MovOpc =
663 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
664
665 I.setDesc(TII.get(MovOpc));
666
667 if (isFP) {
668 const TargetRegisterClass &GPRRC =
669 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
670 const TargetRegisterClass &FPRRC =
671 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
672
673 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
674 MachineOperand &RegOp = I.getOperand(0);
675 RegOp.setReg(DefGPRReg);
676
677 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
678 TII.get(AArch64::COPY))
679 .addDef(DefReg)
680 .addUse(DefGPRReg);
681
682 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
683 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
684 return false;
685 }
686
687 MachineOperand &ImmOp = I.getOperand(1);
688 // FIXME: Is going through int64_t always correct?
689 ImmOp.ChangeToImmediate(
690 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000691 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000692 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
693 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000694 } else if (I.getOperand(1).isImm()) {
695 uint64_t Val = I.getOperand(1).getImm();
696 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000697 }
698
699 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
700 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000701 }
702
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000703 case TargetOpcode::G_FRAME_INDEX: {
704 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000705 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000706 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000707 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000708 return false;
709 }
710
711 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000712
713 // MOs for a #0 shifted immediate.
714 I.addOperand(MachineOperand::CreateImm(0));
715 I.addOperand(MachineOperand::CreateImm(0));
716
717 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
718 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000719
720 case TargetOpcode::G_GLOBAL_VALUE: {
721 auto GV = I.getOperand(1).getGlobal();
722 if (GV->isThreadLocal()) {
723 // FIXME: we don't support TLS yet.
724 return false;
725 }
726 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000727 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000728 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000729 I.getOperand(1).setTargetFlags(OpFlags);
730 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000731 I.setDesc(TII.get(AArch64::MOVaddr));
732 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
733 MachineInstrBuilder MIB(MF, I);
734 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
735 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
736 }
737 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
738 }
739
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000740 case TargetOpcode::G_LOAD:
741 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000742 LLT MemTy = Ty;
743 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000744
Tim Northover5ae83502016-09-15 09:20:34 +0000745 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000746 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000747 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000748 return false;
749 }
750
Tim Northover48dfa1a2017-02-13 22:14:16 +0000751 auto &MemOp = **I.memoperands_begin();
752 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
753 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
754 return false;
755 }
756
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000757#ifndef NDEBUG
758 // Sanity-check the pointer register.
759 const unsigned PtrReg = I.getOperand(1).getReg();
760 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
761 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
762 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000763 assert(MRI.getType(PtrReg).isPointer() &&
764 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000765#endif
766
767 const unsigned ValReg = I.getOperand(0).getReg();
768 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
769
770 const unsigned NewOpc =
771 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
772 if (NewOpc == I.getOpcode())
773 return false;
774
775 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000776
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000777 uint64_t Offset = 0;
778 auto *PtrMI = MRI.getVRegDef(PtrReg);
779
780 // Try to fold a GEP into our unsigned immediate addressing mode.
781 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
782 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
783 int64_t Imm = *COff;
784 const unsigned Size = MemTy.getSizeInBits() / 8;
785 const unsigned Scale = Log2_32(Size);
786 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
787 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
788 I.getOperand(1).setReg(Ptr2Reg);
789 PtrMI = MRI.getVRegDef(Ptr2Reg);
790 Offset = Imm / Size;
791 }
792 }
793 }
794
795 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000796
797 // If we're storing a 0, use WZR/XZR.
798 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
799 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
800 if (I.getOpcode() == AArch64::STRWui)
801 I.getOperand(0).setReg(AArch64::WZR);
802 else if (I.getOpcode() == AArch64::STRXui)
803 I.getOperand(0).setReg(AArch64::XZR);
804 }
805 }
806
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000807 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
808 }
809
Tim Northover9dd78f82017-02-08 21:22:25 +0000810 case TargetOpcode::G_SMULH:
811 case TargetOpcode::G_UMULH: {
812 // Reject the various things we don't support yet.
813 if (unsupportedBinOp(I, RBI, MRI, TRI))
814 return false;
815
816 const unsigned DefReg = I.getOperand(0).getReg();
817 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
818
819 if (RB.getID() != AArch64::GPRRegBankID) {
820 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
821 return false;
822 }
823
824 if (Ty != LLT::scalar(64)) {
825 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
826 << ", expected: " << LLT::scalar(64) << '\n');
827 return false;
828 }
829
830 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
831 : AArch64::UMULHrr;
832 I.setDesc(TII.get(NewOpc));
833
834 // Now that we selected an opcode, we need to constrain the register
835 // operands to use appropriate classes.
836 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
837 }
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000838 case TargetOpcode::G_MUL: {
839 // Reject the various things we don't support yet.
840 if (unsupportedBinOp(I, RBI, MRI, TRI))
841 return false;
842
843 const unsigned DefReg = I.getOperand(0).getReg();
844 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
845
846 if (RB.getID() != AArch64::GPRRegBankID) {
847 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
848 return false;
849 }
850
851 unsigned ZeroReg;
852 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000853 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000854 NewOpc = AArch64::MADDWrrr;
855 ZeroReg = AArch64::WZR;
856 } else if (Ty == LLT::scalar(64)) {
857 NewOpc = AArch64::MADDXrrr;
858 ZeroReg = AArch64::XZR;
859 } else {
860 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
861 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
862 return false;
863 }
864
865 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000866
867 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
868
869 // Now that we selected an opcode, we need to constrain the register
870 // operands to use appropriate classes.
871 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
872 }
873
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000874 case TargetOpcode::G_FADD:
875 case TargetOpcode::G_FSUB:
876 case TargetOpcode::G_FMUL:
877 case TargetOpcode::G_FDIV:
878
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000879 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000880 case TargetOpcode::G_SHL:
881 case TargetOpcode::G_LSHR:
882 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000883 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000884 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000885 if (unsupportedBinOp(I, RBI, MRI, TRI))
886 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000887
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000888 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000889
890 const unsigned DefReg = I.getOperand(0).getReg();
891 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
892
893 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
894 if (NewOpc == I.getOpcode())
895 return false;
896
897 I.setDesc(TII.get(NewOpc));
898 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000899
900 // Now that we selected an opcode, we need to constrain the register
901 // operands to use appropriate classes.
902 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
903 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000904
Tim Northover398c5f52017-02-14 20:56:29 +0000905 case TargetOpcode::G_PTR_MASK: {
906 uint64_t Align = I.getOperand(2).getImm();
907 if (Align >= 64 || Align == 0)
908 return false;
909
910 uint64_t Mask = ~((1ULL << Align) - 1);
911 I.setDesc(TII.get(AArch64::ANDXri));
912 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
913
914 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
915 }
Tim Northover037af52c2016-10-31 18:31:09 +0000916 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000917 case TargetOpcode::G_TRUNC: {
918 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
919 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
920
921 const unsigned DstReg = I.getOperand(0).getReg();
922 const unsigned SrcReg = I.getOperand(1).getReg();
923
924 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
925 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
926
927 if (DstRB.getID() != SrcRB.getID()) {
928 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
929 return false;
930 }
931
932 if (DstRB.getID() == AArch64::GPRRegBankID) {
933 const TargetRegisterClass *DstRC =
934 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
935 if (!DstRC)
936 return false;
937
938 const TargetRegisterClass *SrcRC =
939 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
940 if (!SrcRC)
941 return false;
942
943 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
944 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
945 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
946 return false;
947 }
948
949 if (DstRC == SrcRC) {
950 // Nothing to be done
951 } else if (DstRC == &AArch64::GPR32RegClass &&
952 SrcRC == &AArch64::GPR64RegClass) {
953 I.getOperand(1).setSubReg(AArch64::sub_32);
954 } else {
955 return false;
956 }
957
958 I.setDesc(TII.get(TargetOpcode::COPY));
959 return true;
960 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
961 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
962 I.setDesc(TII.get(AArch64::XTNv4i16));
963 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
964 return true;
965 }
966 }
967
968 return false;
969 }
970
Tim Northover3d38b3a2016-10-11 20:50:21 +0000971 case TargetOpcode::G_ANYEXT: {
972 const unsigned DstReg = I.getOperand(0).getReg();
973 const unsigned SrcReg = I.getOperand(1).getReg();
974
Quentin Colombetcb629a82016-10-12 03:57:49 +0000975 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
976 if (RBDst.getID() != AArch64::GPRRegBankID) {
977 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
978 return false;
979 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000980
Quentin Colombetcb629a82016-10-12 03:57:49 +0000981 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
982 if (RBSrc.getID() != AArch64::GPRRegBankID) {
983 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000984 return false;
985 }
986
987 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
988
989 if (DstSize == 0) {
990 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
991 return false;
992 }
993
Quentin Colombetcb629a82016-10-12 03:57:49 +0000994 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000995 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
996 << ", expected: 32 or 64\n");
997 return false;
998 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000999 // At this point G_ANYEXT is just like a plain COPY, but we need
1000 // to explicitly form the 64-bit value if any.
1001 if (DstSize > 32) {
1002 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1003 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1004 .addDef(ExtSrc)
1005 .addImm(0)
1006 .addUse(SrcReg)
1007 .addImm(AArch64::sub_32);
1008 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001009 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001010 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001011 }
1012
1013 case TargetOpcode::G_ZEXT:
1014 case TargetOpcode::G_SEXT: {
1015 unsigned Opcode = I.getOpcode();
1016 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1017 SrcTy = MRI.getType(I.getOperand(1).getReg());
1018 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1019 const unsigned DefReg = I.getOperand(0).getReg();
1020 const unsigned SrcReg = I.getOperand(1).getReg();
1021 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1022
1023 if (RB.getID() != AArch64::GPRRegBankID) {
1024 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1025 << ", expected: GPR\n");
1026 return false;
1027 }
1028
1029 MachineInstr *ExtI;
1030 if (DstTy == LLT::scalar(64)) {
1031 // FIXME: Can we avoid manually doing this?
1032 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1033 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1034 << " operand\n");
1035 return false;
1036 }
1037
1038 const unsigned SrcXReg =
1039 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1040 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1041 .addDef(SrcXReg)
1042 .addImm(0)
1043 .addUse(SrcReg)
1044 .addImm(AArch64::sub_32);
1045
1046 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1047 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1048 .addDef(DefReg)
1049 .addUse(SrcXReg)
1050 .addImm(0)
1051 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001052 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001053 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1054 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1055 .addDef(DefReg)
1056 .addUse(SrcReg)
1057 .addImm(0)
1058 .addImm(SrcTy.getSizeInBits() - 1);
1059 } else {
1060 return false;
1061 }
1062
1063 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1064
1065 I.eraseFromParent();
1066 return true;
1067 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001068
Tim Northover69271c62016-10-12 22:49:11 +00001069 case TargetOpcode::G_SITOFP:
1070 case TargetOpcode::G_UITOFP:
1071 case TargetOpcode::G_FPTOSI:
1072 case TargetOpcode::G_FPTOUI: {
1073 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1074 SrcTy = MRI.getType(I.getOperand(1).getReg());
1075 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1076 if (NewOpc == Opcode)
1077 return false;
1078
1079 I.setDesc(TII.get(NewOpc));
1080 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1081
1082 return true;
1083 }
1084
1085
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001086 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001087 case TargetOpcode::G_BITCAST:
1088 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001089
Tim Northover5f7dea82016-11-08 17:44:07 +00001090 case TargetOpcode::G_FPEXT: {
1091 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1092 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1093 << ", expected: " << LLT::scalar(64) << '\n');
1094 return false;
1095 }
1096
1097 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1098 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1099 << ", expected: " << LLT::scalar(32) << '\n');
1100 return false;
1101 }
1102
1103 const unsigned DefReg = I.getOperand(0).getReg();
1104 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1105
1106 if (RB.getID() != AArch64::FPRRegBankID) {
1107 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1108 return false;
1109 }
1110
1111 I.setDesc(TII.get(AArch64::FCVTDSr));
1112 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1113
1114 return true;
1115 }
1116
1117 case TargetOpcode::G_FPTRUNC: {
1118 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1119 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1120 << ", expected: " << LLT::scalar(32) << '\n');
1121 return false;
1122 }
1123
1124 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1125 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1126 << ", expected: " << LLT::scalar(64) << '\n');
1127 return false;
1128 }
1129
1130 const unsigned DefReg = I.getOperand(0).getReg();
1131 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1132
1133 if (RB.getID() != AArch64::FPRRegBankID) {
1134 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1135 return false;
1136 }
1137
1138 I.setDesc(TII.get(AArch64::FCVTSDr));
1139 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1140
1141 return true;
1142 }
1143
Tim Northover9ac0eba2016-11-08 00:45:29 +00001144 case TargetOpcode::G_SELECT: {
1145 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1146 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1147 << ", expected: " << LLT::scalar(1) << '\n');
1148 return false;
1149 }
1150
1151 const unsigned CondReg = I.getOperand(1).getReg();
1152 const unsigned TReg = I.getOperand(2).getReg();
1153 const unsigned FReg = I.getOperand(3).getReg();
1154
1155 unsigned CSelOpc = 0;
1156
1157 if (Ty == LLT::scalar(32)) {
1158 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001159 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001160 CSelOpc = AArch64::CSELXr;
1161 } else {
1162 return false;
1163 }
1164
1165 MachineInstr &TstMI =
1166 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1167 .addDef(AArch64::WZR)
1168 .addUse(CondReg)
1169 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1170
1171 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1172 .addDef(I.getOperand(0).getReg())
1173 .addUse(TReg)
1174 .addUse(FReg)
1175 .addImm(AArch64CC::NE);
1176
1177 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1178 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1179
1180 I.eraseFromParent();
1181 return true;
1182 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001183 case TargetOpcode::G_ICMP: {
1184 if (Ty != LLT::scalar(1)) {
1185 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1186 << ", expected: " << LLT::scalar(1) << '\n');
1187 return false;
1188 }
1189
1190 unsigned CmpOpc = 0;
1191 unsigned ZReg = 0;
1192
1193 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1194 if (CmpTy == LLT::scalar(32)) {
1195 CmpOpc = AArch64::SUBSWrr;
1196 ZReg = AArch64::WZR;
1197 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1198 CmpOpc = AArch64::SUBSXrr;
1199 ZReg = AArch64::XZR;
1200 } else {
1201 return false;
1202 }
1203
Kristof Beyls22524402017-01-05 10:16:08 +00001204 // CSINC increments the result by one when the condition code is false.
1205 // Therefore, we have to invert the predicate to get an increment by 1 when
1206 // the predicate is true.
1207 const AArch64CC::CondCode invCC =
1208 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1209 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001210
1211 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1212 .addDef(ZReg)
1213 .addUse(I.getOperand(2).getReg())
1214 .addUse(I.getOperand(3).getReg());
1215
1216 MachineInstr &CSetMI =
1217 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1218 .addDef(I.getOperand(0).getReg())
1219 .addUse(AArch64::WZR)
1220 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001221 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001222
1223 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1224 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1225
1226 I.eraseFromParent();
1227 return true;
1228 }
1229
Tim Northover7dd378d2016-10-12 22:49:07 +00001230 case TargetOpcode::G_FCMP: {
1231 if (Ty != LLT::scalar(1)) {
1232 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1233 << ", expected: " << LLT::scalar(1) << '\n');
1234 return false;
1235 }
1236
1237 unsigned CmpOpc = 0;
1238 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1239 if (CmpTy == LLT::scalar(32)) {
1240 CmpOpc = AArch64::FCMPSrr;
1241 } else if (CmpTy == LLT::scalar(64)) {
1242 CmpOpc = AArch64::FCMPDrr;
1243 } else {
1244 return false;
1245 }
1246
1247 // FIXME: regbank
1248
1249 AArch64CC::CondCode CC1, CC2;
1250 changeFCMPPredToAArch64CC(
1251 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1252
1253 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1254 .addUse(I.getOperand(2).getReg())
1255 .addUse(I.getOperand(3).getReg());
1256
1257 const unsigned DefReg = I.getOperand(0).getReg();
1258 unsigned Def1Reg = DefReg;
1259 if (CC2 != AArch64CC::AL)
1260 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1261
1262 MachineInstr &CSetMI =
1263 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1264 .addDef(Def1Reg)
1265 .addUse(AArch64::WZR)
1266 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001267 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001268
1269 if (CC2 != AArch64CC::AL) {
1270 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1271 MachineInstr &CSet2MI =
1272 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1273 .addDef(Def2Reg)
1274 .addUse(AArch64::WZR)
1275 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001276 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001277 MachineInstr &OrMI =
1278 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1279 .addDef(DefReg)
1280 .addUse(Def1Reg)
1281 .addUse(Def2Reg);
1282 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1283 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1284 }
1285
1286 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1287 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1288
1289 I.eraseFromParent();
1290 return true;
1291 }
Tim Northovere9600d82017-02-08 17:57:27 +00001292 case TargetOpcode::G_VASTART:
1293 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1294 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001295 }
1296
1297 return false;
1298}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001299
1300/// SelectArithImmed - Select an immediate value that can be represented as
1301/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1302/// Val set to the 12-bit value and Shift set to the shifter operand.
1303bool AArch64InstructionSelector::selectArithImmed(
1304 MachineOperand &Root, MachineOperand &Result1,
1305 MachineOperand &Result2) const {
1306 MachineInstr &MI = *Root.getParent();
1307 MachineBasicBlock &MBB = *MI.getParent();
1308 MachineFunction &MF = *MBB.getParent();
1309 MachineRegisterInfo &MRI = MF.getRegInfo();
1310
1311 // This function is called from the addsub_shifted_imm ComplexPattern,
1312 // which lists [imm] as the list of opcode it's interested in, however
1313 // we still need to check whether the operand is actually an immediate
1314 // here because the ComplexPattern opcode list is only used in
1315 // root-level opcode matching.
1316 uint64_t Immed;
1317 if (Root.isImm())
1318 Immed = Root.getImm();
1319 else if (Root.isCImm())
1320 Immed = Root.getCImm()->getZExtValue();
1321 else if (Root.isReg()) {
1322 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1323 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1324 return false;
Daniel Sanders0e642022017-03-16 18:04:50 +00001325 MachineOperand &Op1 = Def->getOperand(1);
1326 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1327 return false;
1328 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001329 } else
1330 return false;
1331
1332 unsigned ShiftAmt;
1333
1334 if (Immed >> 12 == 0) {
1335 ShiftAmt = 0;
1336 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1337 ShiftAmt = 12;
1338 Immed = Immed >> 12;
1339 } else
1340 return false;
1341
1342 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1343 Result1.ChangeToImmediate(Immed);
1344 Result1.clearParent();
1345 Result2.ChangeToImmediate(ShVal);
1346 Result2.clearParent();
1347 return true;
1348}