blob: 146eea07a56113451ba07494831d2ad0f025b926 [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
777 I.addOperand(MachineOperand::CreateImm(0));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000778
779 // If we're storing a 0, use WZR/XZR.
780 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
781 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
782 if (I.getOpcode() == AArch64::STRWui)
783 I.getOperand(0).setReg(AArch64::WZR);
784 else if (I.getOpcode() == AArch64::STRXui)
785 I.getOperand(0).setReg(AArch64::XZR);
786 }
787 }
788
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000789 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
790 }
791
Tim Northover9dd78f82017-02-08 21:22:25 +0000792 case TargetOpcode::G_SMULH:
793 case TargetOpcode::G_UMULH: {
794 // Reject the various things we don't support yet.
795 if (unsupportedBinOp(I, RBI, MRI, TRI))
796 return false;
797
798 const unsigned DefReg = I.getOperand(0).getReg();
799 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
800
801 if (RB.getID() != AArch64::GPRRegBankID) {
802 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
803 return false;
804 }
805
806 if (Ty != LLT::scalar(64)) {
807 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
808 << ", expected: " << LLT::scalar(64) << '\n');
809 return false;
810 }
811
812 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
813 : AArch64::UMULHrr;
814 I.setDesc(TII.get(NewOpc));
815
816 // Now that we selected an opcode, we need to constrain the register
817 // operands to use appropriate classes.
818 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
819 }
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000820 case TargetOpcode::G_MUL: {
821 // Reject the various things we don't support yet.
822 if (unsupportedBinOp(I, RBI, MRI, TRI))
823 return false;
824
825 const unsigned DefReg = I.getOperand(0).getReg();
826 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
827
828 if (RB.getID() != AArch64::GPRRegBankID) {
829 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
830 return false;
831 }
832
833 unsigned ZeroReg;
834 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000835 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000836 NewOpc = AArch64::MADDWrrr;
837 ZeroReg = AArch64::WZR;
838 } else if (Ty == LLT::scalar(64)) {
839 NewOpc = AArch64::MADDXrrr;
840 ZeroReg = AArch64::XZR;
841 } else {
842 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
843 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
844 return false;
845 }
846
847 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000848
849 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
850
851 // Now that we selected an opcode, we need to constrain the register
852 // operands to use appropriate classes.
853 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
854 }
855
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000856 case TargetOpcode::G_FADD:
857 case TargetOpcode::G_FSUB:
858 case TargetOpcode::G_FMUL:
859 case TargetOpcode::G_FDIV:
860
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000861 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000862 case TargetOpcode::G_SHL:
863 case TargetOpcode::G_LSHR:
864 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000865 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000866 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000867 if (unsupportedBinOp(I, RBI, MRI, TRI))
868 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000869
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000870 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000871
872 const unsigned DefReg = I.getOperand(0).getReg();
873 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
874
875 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
876 if (NewOpc == I.getOpcode())
877 return false;
878
879 I.setDesc(TII.get(NewOpc));
880 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000881
882 // Now that we selected an opcode, we need to constrain the register
883 // operands to use appropriate classes.
884 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
885 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000886
Tim Northover398c5f52017-02-14 20:56:29 +0000887 case TargetOpcode::G_PTR_MASK: {
888 uint64_t Align = I.getOperand(2).getImm();
889 if (Align >= 64 || Align == 0)
890 return false;
891
892 uint64_t Mask = ~((1ULL << Align) - 1);
893 I.setDesc(TII.get(AArch64::ANDXri));
894 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
895
896 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
897 }
Tim Northover037af52c2016-10-31 18:31:09 +0000898 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000899 case TargetOpcode::G_TRUNC: {
900 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
901 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
902
903 const unsigned DstReg = I.getOperand(0).getReg();
904 const unsigned SrcReg = I.getOperand(1).getReg();
905
906 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
907 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
908
909 if (DstRB.getID() != SrcRB.getID()) {
910 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
911 return false;
912 }
913
914 if (DstRB.getID() == AArch64::GPRRegBankID) {
915 const TargetRegisterClass *DstRC =
916 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
917 if (!DstRC)
918 return false;
919
920 const TargetRegisterClass *SrcRC =
921 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
922 if (!SrcRC)
923 return false;
924
925 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
926 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
927 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
928 return false;
929 }
930
931 if (DstRC == SrcRC) {
932 // Nothing to be done
933 } else if (DstRC == &AArch64::GPR32RegClass &&
934 SrcRC == &AArch64::GPR64RegClass) {
935 I.getOperand(1).setSubReg(AArch64::sub_32);
936 } else {
937 return false;
938 }
939
940 I.setDesc(TII.get(TargetOpcode::COPY));
941 return true;
942 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
943 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
944 I.setDesc(TII.get(AArch64::XTNv4i16));
945 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
946 return true;
947 }
948 }
949
950 return false;
951 }
952
Tim Northover3d38b3a2016-10-11 20:50:21 +0000953 case TargetOpcode::G_ANYEXT: {
954 const unsigned DstReg = I.getOperand(0).getReg();
955 const unsigned SrcReg = I.getOperand(1).getReg();
956
Quentin Colombetcb629a82016-10-12 03:57:49 +0000957 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
958 if (RBDst.getID() != AArch64::GPRRegBankID) {
959 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
960 return false;
961 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000962
Quentin Colombetcb629a82016-10-12 03:57:49 +0000963 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
964 if (RBSrc.getID() != AArch64::GPRRegBankID) {
965 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000966 return false;
967 }
968
969 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
970
971 if (DstSize == 0) {
972 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
973 return false;
974 }
975
Quentin Colombetcb629a82016-10-12 03:57:49 +0000976 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000977 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
978 << ", expected: 32 or 64\n");
979 return false;
980 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000981 // At this point G_ANYEXT is just like a plain COPY, but we need
982 // to explicitly form the 64-bit value if any.
983 if (DstSize > 32) {
984 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
985 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
986 .addDef(ExtSrc)
987 .addImm(0)
988 .addUse(SrcReg)
989 .addImm(AArch64::sub_32);
990 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000991 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000992 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000993 }
994
995 case TargetOpcode::G_ZEXT:
996 case TargetOpcode::G_SEXT: {
997 unsigned Opcode = I.getOpcode();
998 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
999 SrcTy = MRI.getType(I.getOperand(1).getReg());
1000 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1001 const unsigned DefReg = I.getOperand(0).getReg();
1002 const unsigned SrcReg = I.getOperand(1).getReg();
1003 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1004
1005 if (RB.getID() != AArch64::GPRRegBankID) {
1006 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1007 << ", expected: GPR\n");
1008 return false;
1009 }
1010
1011 MachineInstr *ExtI;
1012 if (DstTy == LLT::scalar(64)) {
1013 // FIXME: Can we avoid manually doing this?
1014 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1015 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1016 << " operand\n");
1017 return false;
1018 }
1019
1020 const unsigned SrcXReg =
1021 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1022 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1023 .addDef(SrcXReg)
1024 .addImm(0)
1025 .addUse(SrcReg)
1026 .addImm(AArch64::sub_32);
1027
1028 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1029 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1030 .addDef(DefReg)
1031 .addUse(SrcXReg)
1032 .addImm(0)
1033 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001034 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001035 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1036 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1037 .addDef(DefReg)
1038 .addUse(SrcReg)
1039 .addImm(0)
1040 .addImm(SrcTy.getSizeInBits() - 1);
1041 } else {
1042 return false;
1043 }
1044
1045 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1046
1047 I.eraseFromParent();
1048 return true;
1049 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001050
Tim Northover69271c62016-10-12 22:49:11 +00001051 case TargetOpcode::G_SITOFP:
1052 case TargetOpcode::G_UITOFP:
1053 case TargetOpcode::G_FPTOSI:
1054 case TargetOpcode::G_FPTOUI: {
1055 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1056 SrcTy = MRI.getType(I.getOperand(1).getReg());
1057 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1058 if (NewOpc == Opcode)
1059 return false;
1060
1061 I.setDesc(TII.get(NewOpc));
1062 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1063
1064 return true;
1065 }
1066
1067
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001068 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001069 case TargetOpcode::G_BITCAST:
1070 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001071
Tim Northover5f7dea82016-11-08 17:44:07 +00001072 case TargetOpcode::G_FPEXT: {
1073 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1074 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1075 << ", expected: " << LLT::scalar(64) << '\n');
1076 return false;
1077 }
1078
1079 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1080 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1081 << ", expected: " << LLT::scalar(32) << '\n');
1082 return false;
1083 }
1084
1085 const unsigned DefReg = I.getOperand(0).getReg();
1086 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1087
1088 if (RB.getID() != AArch64::FPRRegBankID) {
1089 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1090 return false;
1091 }
1092
1093 I.setDesc(TII.get(AArch64::FCVTDSr));
1094 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1095
1096 return true;
1097 }
1098
1099 case TargetOpcode::G_FPTRUNC: {
1100 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1101 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1102 << ", expected: " << LLT::scalar(32) << '\n');
1103 return false;
1104 }
1105
1106 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1107 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1108 << ", expected: " << LLT::scalar(64) << '\n');
1109 return false;
1110 }
1111
1112 const unsigned DefReg = I.getOperand(0).getReg();
1113 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1114
1115 if (RB.getID() != AArch64::FPRRegBankID) {
1116 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1117 return false;
1118 }
1119
1120 I.setDesc(TII.get(AArch64::FCVTSDr));
1121 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1122
1123 return true;
1124 }
1125
Tim Northover9ac0eba2016-11-08 00:45:29 +00001126 case TargetOpcode::G_SELECT: {
1127 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1128 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1129 << ", expected: " << LLT::scalar(1) << '\n');
1130 return false;
1131 }
1132
1133 const unsigned CondReg = I.getOperand(1).getReg();
1134 const unsigned TReg = I.getOperand(2).getReg();
1135 const unsigned FReg = I.getOperand(3).getReg();
1136
1137 unsigned CSelOpc = 0;
1138
1139 if (Ty == LLT::scalar(32)) {
1140 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001141 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001142 CSelOpc = AArch64::CSELXr;
1143 } else {
1144 return false;
1145 }
1146
1147 MachineInstr &TstMI =
1148 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1149 .addDef(AArch64::WZR)
1150 .addUse(CondReg)
1151 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1152
1153 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1154 .addDef(I.getOperand(0).getReg())
1155 .addUse(TReg)
1156 .addUse(FReg)
1157 .addImm(AArch64CC::NE);
1158
1159 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1160 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1161
1162 I.eraseFromParent();
1163 return true;
1164 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001165 case TargetOpcode::G_ICMP: {
1166 if (Ty != LLT::scalar(1)) {
1167 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1168 << ", expected: " << LLT::scalar(1) << '\n');
1169 return false;
1170 }
1171
1172 unsigned CmpOpc = 0;
1173 unsigned ZReg = 0;
1174
1175 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1176 if (CmpTy == LLT::scalar(32)) {
1177 CmpOpc = AArch64::SUBSWrr;
1178 ZReg = AArch64::WZR;
1179 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1180 CmpOpc = AArch64::SUBSXrr;
1181 ZReg = AArch64::XZR;
1182 } else {
1183 return false;
1184 }
1185
Kristof Beyls22524402017-01-05 10:16:08 +00001186 // CSINC increments the result by one when the condition code is false.
1187 // Therefore, we have to invert the predicate to get an increment by 1 when
1188 // the predicate is true.
1189 const AArch64CC::CondCode invCC =
1190 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1191 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001192
1193 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1194 .addDef(ZReg)
1195 .addUse(I.getOperand(2).getReg())
1196 .addUse(I.getOperand(3).getReg());
1197
1198 MachineInstr &CSetMI =
1199 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1200 .addDef(I.getOperand(0).getReg())
1201 .addUse(AArch64::WZR)
1202 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001203 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001204
1205 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1206 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1207
1208 I.eraseFromParent();
1209 return true;
1210 }
1211
Tim Northover7dd378d2016-10-12 22:49:07 +00001212 case TargetOpcode::G_FCMP: {
1213 if (Ty != LLT::scalar(1)) {
1214 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1215 << ", expected: " << LLT::scalar(1) << '\n');
1216 return false;
1217 }
1218
1219 unsigned CmpOpc = 0;
1220 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1221 if (CmpTy == LLT::scalar(32)) {
1222 CmpOpc = AArch64::FCMPSrr;
1223 } else if (CmpTy == LLT::scalar(64)) {
1224 CmpOpc = AArch64::FCMPDrr;
1225 } else {
1226 return false;
1227 }
1228
1229 // FIXME: regbank
1230
1231 AArch64CC::CondCode CC1, CC2;
1232 changeFCMPPredToAArch64CC(
1233 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1234
1235 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1236 .addUse(I.getOperand(2).getReg())
1237 .addUse(I.getOperand(3).getReg());
1238
1239 const unsigned DefReg = I.getOperand(0).getReg();
1240 unsigned Def1Reg = DefReg;
1241 if (CC2 != AArch64CC::AL)
1242 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1243
1244 MachineInstr &CSetMI =
1245 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1246 .addDef(Def1Reg)
1247 .addUse(AArch64::WZR)
1248 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001249 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001250
1251 if (CC2 != AArch64CC::AL) {
1252 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1253 MachineInstr &CSet2MI =
1254 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1255 .addDef(Def2Reg)
1256 .addUse(AArch64::WZR)
1257 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001258 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001259 MachineInstr &OrMI =
1260 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1261 .addDef(DefReg)
1262 .addUse(Def1Reg)
1263 .addUse(Def2Reg);
1264 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1265 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1266 }
1267
1268 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1269 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1270
1271 I.eraseFromParent();
1272 return true;
1273 }
Tim Northovere9600d82017-02-08 17:57:27 +00001274 case TargetOpcode::G_VASTART:
1275 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1276 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001277 }
1278
1279 return false;
1280}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001281
1282/// SelectArithImmed - Select an immediate value that can be represented as
1283/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1284/// Val set to the 12-bit value and Shift set to the shifter operand.
1285bool AArch64InstructionSelector::selectArithImmed(
1286 MachineOperand &Root, MachineOperand &Result1,
1287 MachineOperand &Result2) const {
1288 MachineInstr &MI = *Root.getParent();
1289 MachineBasicBlock &MBB = *MI.getParent();
1290 MachineFunction &MF = *MBB.getParent();
1291 MachineRegisterInfo &MRI = MF.getRegInfo();
1292
1293 // This function is called from the addsub_shifted_imm ComplexPattern,
1294 // which lists [imm] as the list of opcode it's interested in, however
1295 // we still need to check whether the operand is actually an immediate
1296 // here because the ComplexPattern opcode list is only used in
1297 // root-level opcode matching.
1298 uint64_t Immed;
1299 if (Root.isImm())
1300 Immed = Root.getImm();
1301 else if (Root.isCImm())
1302 Immed = Root.getCImm()->getZExtValue();
1303 else if (Root.isReg()) {
1304 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1305 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1306 return false;
Daniel Sanders0e642022017-03-16 18:04:50 +00001307 MachineOperand &Op1 = Def->getOperand(1);
1308 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1309 return false;
1310 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001311 } else
1312 return false;
1313
1314 unsigned ShiftAmt;
1315
1316 if (Immed >> 12 == 0) {
1317 ShiftAmt = 0;
1318 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1319 ShiftAmt = 12;
1320 Immed = Immed >> 12;
1321 } else
1322 return false;
1323
1324 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1325 Result1.ChangeToImmediate(Immed);
1326 Result1.clearParent();
1327 Result2.ChangeToImmediate(ShVal);
1328 Result2.clearParent();
1329 return true;
1330}