blob: f83c7f0e6cc0a3b62aaca08a58f22fa7a66c4304 [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 Northover398c5f52017-02-14 20:56:29 +0000819 case TargetOpcode::G_PTR_MASK: {
820 uint64_t Align = I.getOperand(2).getImm();
821 if (Align >= 64 || Align == 0)
822 return false;
823
824 uint64_t Mask = ~((1ULL << Align) - 1);
825 I.setDesc(TII.get(AArch64::ANDXri));
826 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
827
828 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
829 }
Tim Northover037af52c2016-10-31 18:31:09 +0000830 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000831 case TargetOpcode::G_TRUNC: {
832 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
833 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
834
835 const unsigned DstReg = I.getOperand(0).getReg();
836 const unsigned SrcReg = I.getOperand(1).getReg();
837
838 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
839 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
840
841 if (DstRB.getID() != SrcRB.getID()) {
842 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
843 return false;
844 }
845
846 if (DstRB.getID() == AArch64::GPRRegBankID) {
847 const TargetRegisterClass *DstRC =
848 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
849 if (!DstRC)
850 return false;
851
852 const TargetRegisterClass *SrcRC =
853 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
854 if (!SrcRC)
855 return false;
856
857 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
858 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
859 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
860 return false;
861 }
862
863 if (DstRC == SrcRC) {
864 // Nothing to be done
865 } else if (DstRC == &AArch64::GPR32RegClass &&
866 SrcRC == &AArch64::GPR64RegClass) {
867 I.getOperand(1).setSubReg(AArch64::sub_32);
868 } else {
869 return false;
870 }
871
872 I.setDesc(TII.get(TargetOpcode::COPY));
873 return true;
874 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
875 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
876 I.setDesc(TII.get(AArch64::XTNv4i16));
877 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
878 return true;
879 }
880 }
881
882 return false;
883 }
884
Tim Northover3d38b3a2016-10-11 20:50:21 +0000885 case TargetOpcode::G_ANYEXT: {
886 const unsigned DstReg = I.getOperand(0).getReg();
887 const unsigned SrcReg = I.getOperand(1).getReg();
888
Quentin Colombetcb629a82016-10-12 03:57:49 +0000889 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
890 if (RBDst.getID() != AArch64::GPRRegBankID) {
891 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
892 return false;
893 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000894
Quentin Colombetcb629a82016-10-12 03:57:49 +0000895 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
896 if (RBSrc.getID() != AArch64::GPRRegBankID) {
897 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000898 return false;
899 }
900
901 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
902
903 if (DstSize == 0) {
904 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
905 return false;
906 }
907
Quentin Colombetcb629a82016-10-12 03:57:49 +0000908 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000909 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
910 << ", expected: 32 or 64\n");
911 return false;
912 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000913 // At this point G_ANYEXT is just like a plain COPY, but we need
914 // to explicitly form the 64-bit value if any.
915 if (DstSize > 32) {
916 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
917 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
918 .addDef(ExtSrc)
919 .addImm(0)
920 .addUse(SrcReg)
921 .addImm(AArch64::sub_32);
922 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000923 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000924 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000925 }
926
927 case TargetOpcode::G_ZEXT:
928 case TargetOpcode::G_SEXT: {
929 unsigned Opcode = I.getOpcode();
930 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
931 SrcTy = MRI.getType(I.getOperand(1).getReg());
932 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
933 const unsigned DefReg = I.getOperand(0).getReg();
934 const unsigned SrcReg = I.getOperand(1).getReg();
935 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
936
937 if (RB.getID() != AArch64::GPRRegBankID) {
938 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
939 << ", expected: GPR\n");
940 return false;
941 }
942
943 MachineInstr *ExtI;
944 if (DstTy == LLT::scalar(64)) {
945 // FIXME: Can we avoid manually doing this?
946 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
947 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
948 << " operand\n");
949 return false;
950 }
951
952 const unsigned SrcXReg =
953 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
954 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
955 .addDef(SrcXReg)
956 .addImm(0)
957 .addUse(SrcReg)
958 .addImm(AArch64::sub_32);
959
960 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
961 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
962 .addDef(DefReg)
963 .addUse(SrcXReg)
964 .addImm(0)
965 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000966 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000967 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
968 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
969 .addDef(DefReg)
970 .addUse(SrcReg)
971 .addImm(0)
972 .addImm(SrcTy.getSizeInBits() - 1);
973 } else {
974 return false;
975 }
976
977 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
978
979 I.eraseFromParent();
980 return true;
981 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000982
Tim Northover69271c62016-10-12 22:49:11 +0000983 case TargetOpcode::G_SITOFP:
984 case TargetOpcode::G_UITOFP:
985 case TargetOpcode::G_FPTOSI:
986 case TargetOpcode::G_FPTOUI: {
987 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
988 SrcTy = MRI.getType(I.getOperand(1).getReg());
989 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
990 if (NewOpc == Opcode)
991 return false;
992
993 I.setDesc(TII.get(NewOpc));
994 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
995
996 return true;
997 }
998
999
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001000 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001001 case TargetOpcode::G_BITCAST:
1002 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001003
Tim Northover5f7dea82016-11-08 17:44:07 +00001004 case TargetOpcode::G_FPEXT: {
1005 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1006 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1007 << ", expected: " << LLT::scalar(64) << '\n');
1008 return false;
1009 }
1010
1011 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1012 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1013 << ", expected: " << LLT::scalar(32) << '\n');
1014 return false;
1015 }
1016
1017 const unsigned DefReg = I.getOperand(0).getReg();
1018 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1019
1020 if (RB.getID() != AArch64::FPRRegBankID) {
1021 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1022 return false;
1023 }
1024
1025 I.setDesc(TII.get(AArch64::FCVTDSr));
1026 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1027
1028 return true;
1029 }
1030
1031 case TargetOpcode::G_FPTRUNC: {
1032 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1033 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1034 << ", expected: " << LLT::scalar(32) << '\n');
1035 return false;
1036 }
1037
1038 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1039 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1040 << ", expected: " << LLT::scalar(64) << '\n');
1041 return false;
1042 }
1043
1044 const unsigned DefReg = I.getOperand(0).getReg();
1045 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1046
1047 if (RB.getID() != AArch64::FPRRegBankID) {
1048 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1049 return false;
1050 }
1051
1052 I.setDesc(TII.get(AArch64::FCVTSDr));
1053 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1054
1055 return true;
1056 }
1057
Tim Northover9ac0eba2016-11-08 00:45:29 +00001058 case TargetOpcode::G_SELECT: {
1059 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1060 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1061 << ", expected: " << LLT::scalar(1) << '\n');
1062 return false;
1063 }
1064
1065 const unsigned CondReg = I.getOperand(1).getReg();
1066 const unsigned TReg = I.getOperand(2).getReg();
1067 const unsigned FReg = I.getOperand(3).getReg();
1068
1069 unsigned CSelOpc = 0;
1070
1071 if (Ty == LLT::scalar(32)) {
1072 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001073 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001074 CSelOpc = AArch64::CSELXr;
1075 } else {
1076 return false;
1077 }
1078
1079 MachineInstr &TstMI =
1080 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1081 .addDef(AArch64::WZR)
1082 .addUse(CondReg)
1083 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1084
1085 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1086 .addDef(I.getOperand(0).getReg())
1087 .addUse(TReg)
1088 .addUse(FReg)
1089 .addImm(AArch64CC::NE);
1090
1091 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1092 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1093
1094 I.eraseFromParent();
1095 return true;
1096 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001097 case TargetOpcode::G_ICMP: {
1098 if (Ty != LLT::scalar(1)) {
1099 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1100 << ", expected: " << LLT::scalar(1) << '\n');
1101 return false;
1102 }
1103
1104 unsigned CmpOpc = 0;
1105 unsigned ZReg = 0;
1106
1107 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1108 if (CmpTy == LLT::scalar(32)) {
1109 CmpOpc = AArch64::SUBSWrr;
1110 ZReg = AArch64::WZR;
1111 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1112 CmpOpc = AArch64::SUBSXrr;
1113 ZReg = AArch64::XZR;
1114 } else {
1115 return false;
1116 }
1117
Kristof Beyls22524402017-01-05 10:16:08 +00001118 // CSINC increments the result by one when the condition code is false.
1119 // Therefore, we have to invert the predicate to get an increment by 1 when
1120 // the predicate is true.
1121 const AArch64CC::CondCode invCC =
1122 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1123 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001124
1125 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1126 .addDef(ZReg)
1127 .addUse(I.getOperand(2).getReg())
1128 .addUse(I.getOperand(3).getReg());
1129
1130 MachineInstr &CSetMI =
1131 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1132 .addDef(I.getOperand(0).getReg())
1133 .addUse(AArch64::WZR)
1134 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001135 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001136
1137 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1138 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1139
1140 I.eraseFromParent();
1141 return true;
1142 }
1143
Tim Northover7dd378d2016-10-12 22:49:07 +00001144 case TargetOpcode::G_FCMP: {
1145 if (Ty != LLT::scalar(1)) {
1146 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1147 << ", expected: " << LLT::scalar(1) << '\n');
1148 return false;
1149 }
1150
1151 unsigned CmpOpc = 0;
1152 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1153 if (CmpTy == LLT::scalar(32)) {
1154 CmpOpc = AArch64::FCMPSrr;
1155 } else if (CmpTy == LLT::scalar(64)) {
1156 CmpOpc = AArch64::FCMPDrr;
1157 } else {
1158 return false;
1159 }
1160
1161 // FIXME: regbank
1162
1163 AArch64CC::CondCode CC1, CC2;
1164 changeFCMPPredToAArch64CC(
1165 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1166
1167 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1168 .addUse(I.getOperand(2).getReg())
1169 .addUse(I.getOperand(3).getReg());
1170
1171 const unsigned DefReg = I.getOperand(0).getReg();
1172 unsigned Def1Reg = DefReg;
1173 if (CC2 != AArch64CC::AL)
1174 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1175
1176 MachineInstr &CSetMI =
1177 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1178 .addDef(Def1Reg)
1179 .addUse(AArch64::WZR)
1180 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001181 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001182
1183 if (CC2 != AArch64CC::AL) {
1184 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1185 MachineInstr &CSet2MI =
1186 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1187 .addDef(Def2Reg)
1188 .addUse(AArch64::WZR)
1189 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001190 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001191 MachineInstr &OrMI =
1192 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1193 .addDef(DefReg)
1194 .addUse(Def1Reg)
1195 .addUse(Def2Reg);
1196 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1197 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1198 }
1199
1200 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1201 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1202
1203 I.eraseFromParent();
1204 return true;
1205 }
Tim Northovere9600d82017-02-08 17:57:27 +00001206 case TargetOpcode::G_VASTART:
1207 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1208 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001209 }
1210
1211 return false;
1212}