blob: 6bced17d09d79730147eae9da15e52b729bb7704 [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
Ahmed Bougacha36f70352016-12-21 23:26:20 +000040#include "AArch64GenGlobalISel.inc"
41
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000042AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000043 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
44 const AArch64RegisterBankInfo &RBI)
45 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000046 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
47
Tim Northoverfb8d9892016-10-12 22:49:15 +000048// FIXME: This should be target-independent, inferred from the types declared
49// for each class in the bank.
50static const TargetRegisterClass *
51getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
52 const RegisterBankInfo &RBI) {
53 if (RB.getID() == AArch64::GPRRegBankID) {
54 if (Ty.getSizeInBits() <= 32)
55 return &AArch64::GPR32RegClass;
56 if (Ty.getSizeInBits() == 64)
57 return &AArch64::GPR64RegClass;
58 return nullptr;
59 }
60
61 if (RB.getID() == AArch64::FPRRegBankID) {
62 if (Ty.getSizeInBits() == 32)
63 return &AArch64::FPR32RegClass;
64 if (Ty.getSizeInBits() == 64)
65 return &AArch64::FPR64RegClass;
66 if (Ty.getSizeInBits() == 128)
67 return &AArch64::FPR128RegClass;
68 return nullptr;
69 }
70
71 return nullptr;
72}
73
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000074/// Check whether \p I is a currently unsupported binary operation:
75/// - it has an unsized type
76/// - an operand is not a vreg
77/// - all operands are not in the same bank
78/// These are checks that should someday live in the verifier, but right now,
79/// these are mostly limitations of the aarch64 selector.
80static bool unsupportedBinOp(const MachineInstr &I,
81 const AArch64RegisterBankInfo &RBI,
82 const MachineRegisterInfo &MRI,
83 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000084 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000085 if (!Ty.isValid()) {
86 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000087 return true;
88 }
89
90 const RegisterBank *PrevOpBank = nullptr;
91 for (auto &MO : I.operands()) {
92 // FIXME: Support non-register operands.
93 if (!MO.isReg()) {
94 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
95 return true;
96 }
97
98 // FIXME: Can generic operations have physical registers operands? If
99 // so, this will need to be taught about that, and we'll need to get the
100 // bank out of the minimal class for the register.
101 // Either way, this needs to be documented (and possibly verified).
102 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
103 DEBUG(dbgs() << "Generic inst has physical register operand\n");
104 return true;
105 }
106
107 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
108 if (!OpBank) {
109 DEBUG(dbgs() << "Generic register has no bank or class\n");
110 return true;
111 }
112
113 if (PrevOpBank && OpBank != PrevOpBank) {
114 DEBUG(dbgs() << "Generic inst operands have different banks\n");
115 return true;
116 }
117 PrevOpBank = OpBank;
118 }
119 return false;
120}
121
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000122/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000123/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000124/// and of size \p OpSize.
125/// \returns \p GenericOpc if the combination is unsupported.
126static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
127 unsigned OpSize) {
128 switch (RegBankID) {
129 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000130 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000131 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000132 case TargetOpcode::G_SHL:
133 return AArch64::LSLVWr;
134 case TargetOpcode::G_LSHR:
135 return AArch64::LSRVWr;
136 case TargetOpcode::G_ASHR:
137 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000138 default:
139 return GenericOpc;
140 }
Tim Northover55782222016-10-18 20:03:48 +0000141 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000142 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000143 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000144 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000145 case TargetOpcode::G_SHL:
146 return AArch64::LSLVXr;
147 case TargetOpcode::G_LSHR:
148 return AArch64::LSRVXr;
149 case TargetOpcode::G_ASHR:
150 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000151 default:
152 return GenericOpc;
153 }
154 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000155 case AArch64::FPRRegBankID:
156 switch (OpSize) {
157 case 32:
158 switch (GenericOpc) {
159 case TargetOpcode::G_FADD:
160 return AArch64::FADDSrr;
161 case TargetOpcode::G_FSUB:
162 return AArch64::FSUBSrr;
163 case TargetOpcode::G_FMUL:
164 return AArch64::FMULSrr;
165 case TargetOpcode::G_FDIV:
166 return AArch64::FDIVSrr;
167 default:
168 return GenericOpc;
169 }
170 case 64:
171 switch (GenericOpc) {
172 case TargetOpcode::G_FADD:
173 return AArch64::FADDDrr;
174 case TargetOpcode::G_FSUB:
175 return AArch64::FSUBDrr;
176 case TargetOpcode::G_FMUL:
177 return AArch64::FMULDrr;
178 case TargetOpcode::G_FDIV:
179 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000180 case TargetOpcode::G_OR:
181 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000182 default:
183 return GenericOpc;
184 }
185 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000186 };
187 return GenericOpc;
188}
189
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000190/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
191/// appropriate for the (value) register bank \p RegBankID and of memory access
192/// size \p OpSize. This returns the variant with the base+unsigned-immediate
193/// addressing mode (e.g., LDRXui).
194/// \returns \p GenericOpc if the combination is unsupported.
195static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
196 unsigned OpSize) {
197 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
198 switch (RegBankID) {
199 case AArch64::GPRRegBankID:
200 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000201 case 8:
202 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
203 case 16:
204 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000205 case 32:
206 return isStore ? AArch64::STRWui : AArch64::LDRWui;
207 case 64:
208 return isStore ? AArch64::STRXui : AArch64::LDRXui;
209 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000210 case AArch64::FPRRegBankID:
211 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000212 case 8:
213 return isStore ? AArch64::STRBui : AArch64::LDRBui;
214 case 16:
215 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000216 case 32:
217 return isStore ? AArch64::STRSui : AArch64::LDRSui;
218 case 64:
219 return isStore ? AArch64::STRDui : AArch64::LDRDui;
220 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000221 };
222 return GenericOpc;
223}
224
Quentin Colombetcb629a82016-10-12 03:57:49 +0000225static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
226 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
227 const RegisterBankInfo &RBI) {
228
229 unsigned DstReg = I.getOperand(0).getReg();
230 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
231 assert(I.isCopy() && "Generic operators do not allow physical registers");
232 return true;
233 }
234
235 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
236 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
237 unsigned SrcReg = I.getOperand(1).getReg();
238 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
239 (void)SrcSize;
240 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
241 "No phys reg on generic operators");
242 assert(
243 (DstSize == SrcSize ||
244 // Copies are a mean to setup initial types, the number of
245 // bits may not exactly match.
246 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
247 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
248 // Copies are a mean to copy bits around, as long as we are
249 // on the same register class, that's fine. Otherwise, that
250 // means we need some SUBREG_TO_REG or AND & co.
251 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
252 "Copy with different width?!");
253 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
254 "GPRs cannot get more than 64-bit width values");
255 const TargetRegisterClass *RC = nullptr;
256
257 if (RegBank.getID() == AArch64::FPRRegBankID) {
258 if (DstSize <= 32)
259 RC = &AArch64::FPR32RegClass;
260 else if (DstSize <= 64)
261 RC = &AArch64::FPR64RegClass;
262 else if (DstSize <= 128)
263 RC = &AArch64::FPR128RegClass;
264 else {
265 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
266 return false;
267 }
268 } else {
269 assert(RegBank.getID() == AArch64::GPRRegBankID &&
270 "Bitcast for the flags?");
271 RC =
272 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
273 }
274
275 // No need to constrain SrcReg. It will get constrained when
276 // we hit another of its use or its defs.
277 // Copies do not have constraints.
278 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
279 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
280 << " operand\n");
281 return false;
282 }
283 I.setDesc(TII.get(AArch64::COPY));
284 return true;
285}
286
Tim Northover69271c62016-10-12 22:49:11 +0000287static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
288 if (!DstTy.isScalar() || !SrcTy.isScalar())
289 return GenericOpc;
290
291 const unsigned DstSize = DstTy.getSizeInBits();
292 const unsigned SrcSize = SrcTy.getSizeInBits();
293
294 switch (DstSize) {
295 case 32:
296 switch (SrcSize) {
297 case 32:
298 switch (GenericOpc) {
299 case TargetOpcode::G_SITOFP:
300 return AArch64::SCVTFUWSri;
301 case TargetOpcode::G_UITOFP:
302 return AArch64::UCVTFUWSri;
303 case TargetOpcode::G_FPTOSI:
304 return AArch64::FCVTZSUWSr;
305 case TargetOpcode::G_FPTOUI:
306 return AArch64::FCVTZUUWSr;
307 default:
308 return GenericOpc;
309 }
310 case 64:
311 switch (GenericOpc) {
312 case TargetOpcode::G_SITOFP:
313 return AArch64::SCVTFUXSri;
314 case TargetOpcode::G_UITOFP:
315 return AArch64::UCVTFUXSri;
316 case TargetOpcode::G_FPTOSI:
317 return AArch64::FCVTZSUWDr;
318 case TargetOpcode::G_FPTOUI:
319 return AArch64::FCVTZUUWDr;
320 default:
321 return GenericOpc;
322 }
323 default:
324 return GenericOpc;
325 }
326 case 64:
327 switch (SrcSize) {
328 case 32:
329 switch (GenericOpc) {
330 case TargetOpcode::G_SITOFP:
331 return AArch64::SCVTFUWDri;
332 case TargetOpcode::G_UITOFP:
333 return AArch64::UCVTFUWDri;
334 case TargetOpcode::G_FPTOSI:
335 return AArch64::FCVTZSUXSr;
336 case TargetOpcode::G_FPTOUI:
337 return AArch64::FCVTZUUXSr;
338 default:
339 return GenericOpc;
340 }
341 case 64:
342 switch (GenericOpc) {
343 case TargetOpcode::G_SITOFP:
344 return AArch64::SCVTFUXDri;
345 case TargetOpcode::G_UITOFP:
346 return AArch64::UCVTFUXDri;
347 case TargetOpcode::G_FPTOSI:
348 return AArch64::FCVTZSUXDr;
349 case TargetOpcode::G_FPTOUI:
350 return AArch64::FCVTZUUXDr;
351 default:
352 return GenericOpc;
353 }
354 default:
355 return GenericOpc;
356 }
357 default:
358 return GenericOpc;
359 };
360 return GenericOpc;
361}
362
Tim Northover6c02ad52016-10-12 22:49:04 +0000363static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
364 switch (P) {
365 default:
366 llvm_unreachable("Unknown condition code!");
367 case CmpInst::ICMP_NE:
368 return AArch64CC::NE;
369 case CmpInst::ICMP_EQ:
370 return AArch64CC::EQ;
371 case CmpInst::ICMP_SGT:
372 return AArch64CC::GT;
373 case CmpInst::ICMP_SGE:
374 return AArch64CC::GE;
375 case CmpInst::ICMP_SLT:
376 return AArch64CC::LT;
377 case CmpInst::ICMP_SLE:
378 return AArch64CC::LE;
379 case CmpInst::ICMP_UGT:
380 return AArch64CC::HI;
381 case CmpInst::ICMP_UGE:
382 return AArch64CC::HS;
383 case CmpInst::ICMP_ULT:
384 return AArch64CC::LO;
385 case CmpInst::ICMP_ULE:
386 return AArch64CC::LS;
387 }
388}
389
Tim Northover7dd378d2016-10-12 22:49:07 +0000390static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
391 AArch64CC::CondCode &CondCode,
392 AArch64CC::CondCode &CondCode2) {
393 CondCode2 = AArch64CC::AL;
394 switch (P) {
395 default:
396 llvm_unreachable("Unknown FP condition!");
397 case CmpInst::FCMP_OEQ:
398 CondCode = AArch64CC::EQ;
399 break;
400 case CmpInst::FCMP_OGT:
401 CondCode = AArch64CC::GT;
402 break;
403 case CmpInst::FCMP_OGE:
404 CondCode = AArch64CC::GE;
405 break;
406 case CmpInst::FCMP_OLT:
407 CondCode = AArch64CC::MI;
408 break;
409 case CmpInst::FCMP_OLE:
410 CondCode = AArch64CC::LS;
411 break;
412 case CmpInst::FCMP_ONE:
413 CondCode = AArch64CC::MI;
414 CondCode2 = AArch64CC::GT;
415 break;
416 case CmpInst::FCMP_ORD:
417 CondCode = AArch64CC::VC;
418 break;
419 case CmpInst::FCMP_UNO:
420 CondCode = AArch64CC::VS;
421 break;
422 case CmpInst::FCMP_UEQ:
423 CondCode = AArch64CC::EQ;
424 CondCode2 = AArch64CC::VS;
425 break;
426 case CmpInst::FCMP_UGT:
427 CondCode = AArch64CC::HI;
428 break;
429 case CmpInst::FCMP_UGE:
430 CondCode = AArch64CC::PL;
431 break;
432 case CmpInst::FCMP_ULT:
433 CondCode = AArch64CC::LT;
434 break;
435 case CmpInst::FCMP_ULE:
436 CondCode = AArch64CC::LE;
437 break;
438 case CmpInst::FCMP_UNE:
439 CondCode = AArch64CC::NE;
440 break;
441 }
442}
443
Tim Northovere9600d82017-02-08 17:57:27 +0000444bool AArch64InstructionSelector::selectVaStartAAPCS(
445 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
446 return false;
447}
448
449bool AArch64InstructionSelector::selectVaStartDarwin(
450 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
451 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
452 unsigned ListReg = I.getOperand(0).getReg();
453
454 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
455
456 auto MIB =
457 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
458 .addDef(ArgsAddrReg)
459 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
460 .addImm(0)
461 .addImm(0);
462
463 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
464
465 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
466 .addUse(ArgsAddrReg)
467 .addUse(ListReg)
468 .addImm(0)
469 .addMemOperand(*I.memoperands_begin());
470
471 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
472 I.eraseFromParent();
473 return true;
474}
475
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000476bool AArch64InstructionSelector::select(MachineInstr &I) const {
477 assert(I.getParent() && "Instruction should be in a basic block!");
478 assert(I.getParent()->getParent() && "Instruction should be in a function!");
479
480 MachineBasicBlock &MBB = *I.getParent();
481 MachineFunction &MF = *MBB.getParent();
482 MachineRegisterInfo &MRI = MF.getRegInfo();
483
Tim Northovercdf23f12016-10-31 18:30:59 +0000484 unsigned Opcode = I.getOpcode();
485 if (!isPreISelGenericOpcode(I.getOpcode())) {
486 // Certain non-generic instructions also need some special handling.
487
488 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
489 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000490
491 if (Opcode == TargetOpcode::PHI) {
492 const unsigned DefReg = I.getOperand(0).getReg();
493 const LLT DefTy = MRI.getType(DefReg);
494
495 const TargetRegisterClass *DefRC = nullptr;
496 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
497 DefRC = TRI.getRegClass(DefReg);
498 } else {
499 const RegClassOrRegBank &RegClassOrBank =
500 MRI.getRegClassOrRegBank(DefReg);
501
502 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
503 if (!DefRC) {
504 if (!DefTy.isValid()) {
505 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
506 return false;
507 }
508 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
509 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
510 if (!DefRC) {
511 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
512 return false;
513 }
514 }
515 }
516
517 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
518 }
519
520 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000521 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000522
523 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000524 }
525
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000526
527 if (I.getNumOperands() != I.getNumExplicitOperands()) {
528 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
529 return false;
530 }
531
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000532 if (selectImpl(I))
533 return true;
534
Tim Northover32a078a2016-09-15 10:09:59 +0000535 LLT Ty =
536 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000537
Tim Northover69271c62016-10-12 22:49:11 +0000538 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000539 case TargetOpcode::G_BRCOND: {
540 if (Ty.getSizeInBits() > 32) {
541 // We shouldn't need this on AArch64, but it would be implemented as an
542 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
543 // bit being tested is < 32.
544 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
545 << ", expected at most 32-bits");
546 return false;
547 }
548
549 const unsigned CondReg = I.getOperand(0).getReg();
550 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
551
552 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
553 .addUse(CondReg)
554 .addImm(/*bit offset=*/0)
555 .addMBB(DestMBB);
556
557 I.eraseFromParent();
558 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
559 }
560
Kristof Beyls65a12c02017-01-30 09:13:18 +0000561 case TargetOpcode::G_BRINDIRECT: {
562 I.setDesc(TII.get(AArch64::BR));
563 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
564 }
565
Tim Northover4494d692016-10-18 19:47:57 +0000566 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000567 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000568 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
569
570 const LLT s32 = LLT::scalar(32);
571 const LLT s64 = LLT::scalar(64);
572 const LLT p0 = LLT::pointer(0, 64);
573
574 const unsigned DefReg = I.getOperand(0).getReg();
575 const LLT DefTy = MRI.getType(DefReg);
576 const unsigned DefSize = DefTy.getSizeInBits();
577 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
578
579 // FIXME: Redundant check, but even less readable when factored out.
580 if (isFP) {
581 if (Ty != s32 && Ty != s64) {
582 DEBUG(dbgs() << "Unable to materialize FP " << Ty
583 << " constant, expected: " << s32 << " or " << s64
584 << '\n');
585 return false;
586 }
587
588 if (RB.getID() != AArch64::FPRRegBankID) {
589 DEBUG(dbgs() << "Unable to materialize FP " << Ty
590 << " constant on bank: " << RB << ", expected: FPR\n");
591 return false;
592 }
593 } else {
594 if (Ty != s32 && Ty != s64 && Ty != p0) {
595 DEBUG(dbgs() << "Unable to materialize integer " << Ty
596 << " constant, expected: " << s32 << ", " << s64 << ", or "
597 << p0 << '\n');
598 return false;
599 }
600
601 if (RB.getID() != AArch64::GPRRegBankID) {
602 DEBUG(dbgs() << "Unable to materialize integer " << Ty
603 << " constant on bank: " << RB << ", expected: GPR\n");
604 return false;
605 }
606 }
607
608 const unsigned MovOpc =
609 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
610
611 I.setDesc(TII.get(MovOpc));
612
613 if (isFP) {
614 const TargetRegisterClass &GPRRC =
615 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
616 const TargetRegisterClass &FPRRC =
617 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
618
619 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
620 MachineOperand &RegOp = I.getOperand(0);
621 RegOp.setReg(DefGPRReg);
622
623 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
624 TII.get(AArch64::COPY))
625 .addDef(DefReg)
626 .addUse(DefGPRReg);
627
628 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
629 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
630 return false;
631 }
632
633 MachineOperand &ImmOp = I.getOperand(1);
634 // FIXME: Is going through int64_t always correct?
635 ImmOp.ChangeToImmediate(
636 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Tim Northover9267ac52016-12-05 21:47:07 +0000637 } else {
638 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
639 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000640 }
641
642 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
643 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000644 }
645
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000646 case TargetOpcode::G_FRAME_INDEX: {
647 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000648 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000649 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000650 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000651 return false;
652 }
653
654 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000655
656 // MOs for a #0 shifted immediate.
657 I.addOperand(MachineOperand::CreateImm(0));
658 I.addOperand(MachineOperand::CreateImm(0));
659
660 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
661 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000662
663 case TargetOpcode::G_GLOBAL_VALUE: {
664 auto GV = I.getOperand(1).getGlobal();
665 if (GV->isThreadLocal()) {
666 // FIXME: we don't support TLS yet.
667 return false;
668 }
669 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000670 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000671 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000672 I.getOperand(1).setTargetFlags(OpFlags);
673 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000674 I.setDesc(TII.get(AArch64::MOVaddr));
675 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
676 MachineInstrBuilder MIB(MF, I);
677 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
678 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
679 }
680 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
681 }
682
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000683 case TargetOpcode::G_LOAD:
684 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000685 LLT MemTy = Ty;
686 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000687
Tim Northover5ae83502016-09-15 09:20:34 +0000688 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000689 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000690 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000691 return false;
692 }
693
Tim Northover48dfa1a2017-02-13 22:14:16 +0000694 auto &MemOp = **I.memoperands_begin();
695 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
696 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
697 return false;
698 }
699
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000700#ifndef NDEBUG
701 // Sanity-check the pointer register.
702 const unsigned PtrReg = I.getOperand(1).getReg();
703 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
704 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
705 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000706 assert(MRI.getType(PtrReg).isPointer() &&
707 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000708#endif
709
710 const unsigned ValReg = I.getOperand(0).getReg();
711 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
712
713 const unsigned NewOpc =
714 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
715 if (NewOpc == I.getOpcode())
716 return false;
717
718 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000719
720 I.addOperand(MachineOperand::CreateImm(0));
721 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
722 }
723
Tim Northover9dd78f82017-02-08 21:22:25 +0000724 case TargetOpcode::G_SMULH:
725 case TargetOpcode::G_UMULH: {
726 // Reject the various things we don't support yet.
727 if (unsupportedBinOp(I, RBI, MRI, TRI))
728 return false;
729
730 const unsigned DefReg = I.getOperand(0).getReg();
731 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
732
733 if (RB.getID() != AArch64::GPRRegBankID) {
734 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
735 return false;
736 }
737
738 if (Ty != LLT::scalar(64)) {
739 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
740 << ", expected: " << LLT::scalar(64) << '\n');
741 return false;
742 }
743
744 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
745 : AArch64::UMULHrr;
746 I.setDesc(TII.get(NewOpc));
747
748 // Now that we selected an opcode, we need to constrain the register
749 // operands to use appropriate classes.
750 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
751 }
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000752 case TargetOpcode::G_MUL: {
753 // Reject the various things we don't support yet.
754 if (unsupportedBinOp(I, RBI, MRI, TRI))
755 return false;
756
757 const unsigned DefReg = I.getOperand(0).getReg();
758 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
759
760 if (RB.getID() != AArch64::GPRRegBankID) {
761 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
762 return false;
763 }
764
765 unsigned ZeroReg;
766 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000767 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000768 NewOpc = AArch64::MADDWrrr;
769 ZeroReg = AArch64::WZR;
770 } else if (Ty == LLT::scalar(64)) {
771 NewOpc = AArch64::MADDXrrr;
772 ZeroReg = AArch64::XZR;
773 } else {
774 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
775 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
776 return false;
777 }
778
779 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000780
781 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
782
783 // Now that we selected an opcode, we need to constrain the register
784 // operands to use appropriate classes.
785 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
786 }
787
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000788 case TargetOpcode::G_FADD:
789 case TargetOpcode::G_FSUB:
790 case TargetOpcode::G_FMUL:
791 case TargetOpcode::G_FDIV:
792
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000793 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000794 case TargetOpcode::G_SHL:
795 case TargetOpcode::G_LSHR:
796 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000797 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000798 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000799 if (unsupportedBinOp(I, RBI, MRI, TRI))
800 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000801
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000802 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000803
804 const unsigned DefReg = I.getOperand(0).getReg();
805 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
806
807 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
808 if (NewOpc == I.getOpcode())
809 return false;
810
811 I.setDesc(TII.get(NewOpc));
812 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000813
814 // Now that we selected an opcode, we need to constrain the register
815 // operands to use appropriate classes.
816 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
817 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000818
Tim Northover037af52c2016-10-31 18:31:09 +0000819 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000820 case TargetOpcode::G_TRUNC: {
821 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
822 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
823
824 const unsigned DstReg = I.getOperand(0).getReg();
825 const unsigned SrcReg = I.getOperand(1).getReg();
826
827 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
828 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
829
830 if (DstRB.getID() != SrcRB.getID()) {
831 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
832 return false;
833 }
834
835 if (DstRB.getID() == AArch64::GPRRegBankID) {
836 const TargetRegisterClass *DstRC =
837 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
838 if (!DstRC)
839 return false;
840
841 const TargetRegisterClass *SrcRC =
842 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
843 if (!SrcRC)
844 return false;
845
846 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
847 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
848 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
849 return false;
850 }
851
852 if (DstRC == SrcRC) {
853 // Nothing to be done
854 } else if (DstRC == &AArch64::GPR32RegClass &&
855 SrcRC == &AArch64::GPR64RegClass) {
856 I.getOperand(1).setSubReg(AArch64::sub_32);
857 } else {
858 return false;
859 }
860
861 I.setDesc(TII.get(TargetOpcode::COPY));
862 return true;
863 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
864 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
865 I.setDesc(TII.get(AArch64::XTNv4i16));
866 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
867 return true;
868 }
869 }
870
871 return false;
872 }
873
Tim Northover3d38b3a2016-10-11 20:50:21 +0000874 case TargetOpcode::G_ANYEXT: {
875 const unsigned DstReg = I.getOperand(0).getReg();
876 const unsigned SrcReg = I.getOperand(1).getReg();
877
Quentin Colombetcb629a82016-10-12 03:57:49 +0000878 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
879 if (RBDst.getID() != AArch64::GPRRegBankID) {
880 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
881 return false;
882 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000883
Quentin Colombetcb629a82016-10-12 03:57:49 +0000884 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
885 if (RBSrc.getID() != AArch64::GPRRegBankID) {
886 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000887 return false;
888 }
889
890 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
891
892 if (DstSize == 0) {
893 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
894 return false;
895 }
896
Quentin Colombetcb629a82016-10-12 03:57:49 +0000897 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000898 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
899 << ", expected: 32 or 64\n");
900 return false;
901 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000902 // At this point G_ANYEXT is just like a plain COPY, but we need
903 // to explicitly form the 64-bit value if any.
904 if (DstSize > 32) {
905 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
906 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
907 .addDef(ExtSrc)
908 .addImm(0)
909 .addUse(SrcReg)
910 .addImm(AArch64::sub_32);
911 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000912 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000913 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000914 }
915
916 case TargetOpcode::G_ZEXT:
917 case TargetOpcode::G_SEXT: {
918 unsigned Opcode = I.getOpcode();
919 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
920 SrcTy = MRI.getType(I.getOperand(1).getReg());
921 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
922 const unsigned DefReg = I.getOperand(0).getReg();
923 const unsigned SrcReg = I.getOperand(1).getReg();
924 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
925
926 if (RB.getID() != AArch64::GPRRegBankID) {
927 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
928 << ", expected: GPR\n");
929 return false;
930 }
931
932 MachineInstr *ExtI;
933 if (DstTy == LLT::scalar(64)) {
934 // FIXME: Can we avoid manually doing this?
935 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
936 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
937 << " operand\n");
938 return false;
939 }
940
941 const unsigned SrcXReg =
942 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
943 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
944 .addDef(SrcXReg)
945 .addImm(0)
946 .addUse(SrcReg)
947 .addImm(AArch64::sub_32);
948
949 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
950 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
951 .addDef(DefReg)
952 .addUse(SrcXReg)
953 .addImm(0)
954 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000955 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000956 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
957 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
958 .addDef(DefReg)
959 .addUse(SrcReg)
960 .addImm(0)
961 .addImm(SrcTy.getSizeInBits() - 1);
962 } else {
963 return false;
964 }
965
966 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
967
968 I.eraseFromParent();
969 return true;
970 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000971
Tim Northover69271c62016-10-12 22:49:11 +0000972 case TargetOpcode::G_SITOFP:
973 case TargetOpcode::G_UITOFP:
974 case TargetOpcode::G_FPTOSI:
975 case TargetOpcode::G_FPTOUI: {
976 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
977 SrcTy = MRI.getType(I.getOperand(1).getReg());
978 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
979 if (NewOpc == Opcode)
980 return false;
981
982 I.setDesc(TII.get(NewOpc));
983 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
984
985 return true;
986 }
987
988
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000989 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000990 case TargetOpcode::G_BITCAST:
991 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000992
Tim Northover5f7dea82016-11-08 17:44:07 +0000993 case TargetOpcode::G_FPEXT: {
994 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
995 DEBUG(dbgs() << "G_FPEXT to type " << Ty
996 << ", expected: " << LLT::scalar(64) << '\n');
997 return false;
998 }
999
1000 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1001 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1002 << ", expected: " << LLT::scalar(32) << '\n');
1003 return false;
1004 }
1005
1006 const unsigned DefReg = I.getOperand(0).getReg();
1007 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1008
1009 if (RB.getID() != AArch64::FPRRegBankID) {
1010 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1011 return false;
1012 }
1013
1014 I.setDesc(TII.get(AArch64::FCVTDSr));
1015 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1016
1017 return true;
1018 }
1019
1020 case TargetOpcode::G_FPTRUNC: {
1021 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1022 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1023 << ", expected: " << LLT::scalar(32) << '\n');
1024 return false;
1025 }
1026
1027 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1028 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1029 << ", expected: " << LLT::scalar(64) << '\n');
1030 return false;
1031 }
1032
1033 const unsigned DefReg = I.getOperand(0).getReg();
1034 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1035
1036 if (RB.getID() != AArch64::FPRRegBankID) {
1037 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1038 return false;
1039 }
1040
1041 I.setDesc(TII.get(AArch64::FCVTSDr));
1042 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1043
1044 return true;
1045 }
1046
Tim Northover9ac0eba2016-11-08 00:45:29 +00001047 case TargetOpcode::G_SELECT: {
1048 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1049 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1050 << ", expected: " << LLT::scalar(1) << '\n');
1051 return false;
1052 }
1053
1054 const unsigned CondReg = I.getOperand(1).getReg();
1055 const unsigned TReg = I.getOperand(2).getReg();
1056 const unsigned FReg = I.getOperand(3).getReg();
1057
1058 unsigned CSelOpc = 0;
1059
1060 if (Ty == LLT::scalar(32)) {
1061 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001062 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001063 CSelOpc = AArch64::CSELXr;
1064 } else {
1065 return false;
1066 }
1067
1068 MachineInstr &TstMI =
1069 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1070 .addDef(AArch64::WZR)
1071 .addUse(CondReg)
1072 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1073
1074 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1075 .addDef(I.getOperand(0).getReg())
1076 .addUse(TReg)
1077 .addUse(FReg)
1078 .addImm(AArch64CC::NE);
1079
1080 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1081 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1082
1083 I.eraseFromParent();
1084 return true;
1085 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001086 case TargetOpcode::G_ICMP: {
1087 if (Ty != LLT::scalar(1)) {
1088 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1089 << ", expected: " << LLT::scalar(1) << '\n');
1090 return false;
1091 }
1092
1093 unsigned CmpOpc = 0;
1094 unsigned ZReg = 0;
1095
1096 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1097 if (CmpTy == LLT::scalar(32)) {
1098 CmpOpc = AArch64::SUBSWrr;
1099 ZReg = AArch64::WZR;
1100 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1101 CmpOpc = AArch64::SUBSXrr;
1102 ZReg = AArch64::XZR;
1103 } else {
1104 return false;
1105 }
1106
Kristof Beyls22524402017-01-05 10:16:08 +00001107 // CSINC increments the result by one when the condition code is false.
1108 // Therefore, we have to invert the predicate to get an increment by 1 when
1109 // the predicate is true.
1110 const AArch64CC::CondCode invCC =
1111 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1112 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001113
1114 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1115 .addDef(ZReg)
1116 .addUse(I.getOperand(2).getReg())
1117 .addUse(I.getOperand(3).getReg());
1118
1119 MachineInstr &CSetMI =
1120 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1121 .addDef(I.getOperand(0).getReg())
1122 .addUse(AArch64::WZR)
1123 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001124 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001125
1126 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1127 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1128
1129 I.eraseFromParent();
1130 return true;
1131 }
1132
Tim Northover7dd378d2016-10-12 22:49:07 +00001133 case TargetOpcode::G_FCMP: {
1134 if (Ty != LLT::scalar(1)) {
1135 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1136 << ", expected: " << LLT::scalar(1) << '\n');
1137 return false;
1138 }
1139
1140 unsigned CmpOpc = 0;
1141 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1142 if (CmpTy == LLT::scalar(32)) {
1143 CmpOpc = AArch64::FCMPSrr;
1144 } else if (CmpTy == LLT::scalar(64)) {
1145 CmpOpc = AArch64::FCMPDrr;
1146 } else {
1147 return false;
1148 }
1149
1150 // FIXME: regbank
1151
1152 AArch64CC::CondCode CC1, CC2;
1153 changeFCMPPredToAArch64CC(
1154 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1155
1156 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1157 .addUse(I.getOperand(2).getReg())
1158 .addUse(I.getOperand(3).getReg());
1159
1160 const unsigned DefReg = I.getOperand(0).getReg();
1161 unsigned Def1Reg = DefReg;
1162 if (CC2 != AArch64CC::AL)
1163 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1164
1165 MachineInstr &CSetMI =
1166 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1167 .addDef(Def1Reg)
1168 .addUse(AArch64::WZR)
1169 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001170 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001171
1172 if (CC2 != AArch64CC::AL) {
1173 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1174 MachineInstr &CSet2MI =
1175 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1176 .addDef(Def2Reg)
1177 .addUse(AArch64::WZR)
1178 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001179 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001180 MachineInstr &OrMI =
1181 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1182 .addDef(DefReg)
1183 .addUse(Def1Reg)
1184 .addUse(Def2Reg);
1185 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1186 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1187 }
1188
1189 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1190 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1191
1192 I.eraseFromParent();
1193 return true;
1194 }
Tim Northovere9600d82017-02-08 17:57:27 +00001195 case TargetOpcode::G_VASTART:
1196 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1197 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001198 }
1199
1200 return false;
1201}