blob: 42b4daf2318cc008514ecf3937e513255b74e63e [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
694#ifndef NDEBUG
695 // Sanity-check the pointer register.
696 const unsigned PtrReg = I.getOperand(1).getReg();
697 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
698 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
699 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000700 assert(MRI.getType(PtrReg).isPointer() &&
701 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000702#endif
703
704 const unsigned ValReg = I.getOperand(0).getReg();
705 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
706
707 const unsigned NewOpc =
708 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
709 if (NewOpc == I.getOpcode())
710 return false;
711
712 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000713
714 I.addOperand(MachineOperand::CreateImm(0));
715 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
716 }
717
Tim Northover9dd78f82017-02-08 21:22:25 +0000718 case TargetOpcode::G_SMULH:
719 case TargetOpcode::G_UMULH: {
720 // Reject the various things we don't support yet.
721 if (unsupportedBinOp(I, RBI, MRI, TRI))
722 return false;
723
724 const unsigned DefReg = I.getOperand(0).getReg();
725 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
726
727 if (RB.getID() != AArch64::GPRRegBankID) {
728 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
729 return false;
730 }
731
732 if (Ty != LLT::scalar(64)) {
733 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
734 << ", expected: " << LLT::scalar(64) << '\n');
735 return false;
736 }
737
738 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
739 : AArch64::UMULHrr;
740 I.setDesc(TII.get(NewOpc));
741
742 // Now that we selected an opcode, we need to constrain the register
743 // operands to use appropriate classes.
744 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
745 }
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000746 case TargetOpcode::G_MUL: {
747 // Reject the various things we don't support yet.
748 if (unsupportedBinOp(I, RBI, MRI, TRI))
749 return false;
750
751 const unsigned DefReg = I.getOperand(0).getReg();
752 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
753
754 if (RB.getID() != AArch64::GPRRegBankID) {
755 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
756 return false;
757 }
758
759 unsigned ZeroReg;
760 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000761 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000762 NewOpc = AArch64::MADDWrrr;
763 ZeroReg = AArch64::WZR;
764 } else if (Ty == LLT::scalar(64)) {
765 NewOpc = AArch64::MADDXrrr;
766 ZeroReg = AArch64::XZR;
767 } else {
768 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
769 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
770 return false;
771 }
772
773 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000774
775 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
776
777 // Now that we selected an opcode, we need to constrain the register
778 // operands to use appropriate classes.
779 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
780 }
781
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000782 case TargetOpcode::G_FADD:
783 case TargetOpcode::G_FSUB:
784 case TargetOpcode::G_FMUL:
785 case TargetOpcode::G_FDIV:
786
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000787 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000788 case TargetOpcode::G_SHL:
789 case TargetOpcode::G_LSHR:
790 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000791 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000792 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000793 if (unsupportedBinOp(I, RBI, MRI, TRI))
794 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000795
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000796 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000797
798 const unsigned DefReg = I.getOperand(0).getReg();
799 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
800
801 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
802 if (NewOpc == I.getOpcode())
803 return false;
804
805 I.setDesc(TII.get(NewOpc));
806 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000807
808 // Now that we selected an opcode, we need to constrain the register
809 // operands to use appropriate classes.
810 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
811 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000812
Tim Northover037af52c2016-10-31 18:31:09 +0000813 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000814 case TargetOpcode::G_TRUNC: {
815 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
816 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
817
818 const unsigned DstReg = I.getOperand(0).getReg();
819 const unsigned SrcReg = I.getOperand(1).getReg();
820
821 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
822 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
823
824 if (DstRB.getID() != SrcRB.getID()) {
825 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
826 return false;
827 }
828
829 if (DstRB.getID() == AArch64::GPRRegBankID) {
830 const TargetRegisterClass *DstRC =
831 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
832 if (!DstRC)
833 return false;
834
835 const TargetRegisterClass *SrcRC =
836 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
837 if (!SrcRC)
838 return false;
839
840 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
841 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
842 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
843 return false;
844 }
845
846 if (DstRC == SrcRC) {
847 // Nothing to be done
848 } else if (DstRC == &AArch64::GPR32RegClass &&
849 SrcRC == &AArch64::GPR64RegClass) {
850 I.getOperand(1).setSubReg(AArch64::sub_32);
851 } else {
852 return false;
853 }
854
855 I.setDesc(TII.get(TargetOpcode::COPY));
856 return true;
857 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
858 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
859 I.setDesc(TII.get(AArch64::XTNv4i16));
860 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
861 return true;
862 }
863 }
864
865 return false;
866 }
867
Tim Northover3d38b3a2016-10-11 20:50:21 +0000868 case TargetOpcode::G_ANYEXT: {
869 const unsigned DstReg = I.getOperand(0).getReg();
870 const unsigned SrcReg = I.getOperand(1).getReg();
871
Quentin Colombetcb629a82016-10-12 03:57:49 +0000872 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
873 if (RBDst.getID() != AArch64::GPRRegBankID) {
874 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
875 return false;
876 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000877
Quentin Colombetcb629a82016-10-12 03:57:49 +0000878 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
879 if (RBSrc.getID() != AArch64::GPRRegBankID) {
880 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000881 return false;
882 }
883
884 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
885
886 if (DstSize == 0) {
887 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
888 return false;
889 }
890
Quentin Colombetcb629a82016-10-12 03:57:49 +0000891 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000892 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
893 << ", expected: 32 or 64\n");
894 return false;
895 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000896 // At this point G_ANYEXT is just like a plain COPY, but we need
897 // to explicitly form the 64-bit value if any.
898 if (DstSize > 32) {
899 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
900 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
901 .addDef(ExtSrc)
902 .addImm(0)
903 .addUse(SrcReg)
904 .addImm(AArch64::sub_32);
905 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000906 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000907 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000908 }
909
910 case TargetOpcode::G_ZEXT:
911 case TargetOpcode::G_SEXT: {
912 unsigned Opcode = I.getOpcode();
913 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
914 SrcTy = MRI.getType(I.getOperand(1).getReg());
915 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
916 const unsigned DefReg = I.getOperand(0).getReg();
917 const unsigned SrcReg = I.getOperand(1).getReg();
918 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
919
920 if (RB.getID() != AArch64::GPRRegBankID) {
921 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
922 << ", expected: GPR\n");
923 return false;
924 }
925
926 MachineInstr *ExtI;
927 if (DstTy == LLT::scalar(64)) {
928 // FIXME: Can we avoid manually doing this?
929 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
930 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
931 << " operand\n");
932 return false;
933 }
934
935 const unsigned SrcXReg =
936 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
937 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
938 .addDef(SrcXReg)
939 .addImm(0)
940 .addUse(SrcReg)
941 .addImm(AArch64::sub_32);
942
943 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
944 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
945 .addDef(DefReg)
946 .addUse(SrcXReg)
947 .addImm(0)
948 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000949 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000950 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
951 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
952 .addDef(DefReg)
953 .addUse(SrcReg)
954 .addImm(0)
955 .addImm(SrcTy.getSizeInBits() - 1);
956 } else {
957 return false;
958 }
959
960 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
961
962 I.eraseFromParent();
963 return true;
964 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000965
Tim Northover69271c62016-10-12 22:49:11 +0000966 case TargetOpcode::G_SITOFP:
967 case TargetOpcode::G_UITOFP:
968 case TargetOpcode::G_FPTOSI:
969 case TargetOpcode::G_FPTOUI: {
970 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
971 SrcTy = MRI.getType(I.getOperand(1).getReg());
972 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
973 if (NewOpc == Opcode)
974 return false;
975
976 I.setDesc(TII.get(NewOpc));
977 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
978
979 return true;
980 }
981
982
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000983 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000984 case TargetOpcode::G_BITCAST:
985 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000986
Tim Northover5f7dea82016-11-08 17:44:07 +0000987 case TargetOpcode::G_FPEXT: {
988 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
989 DEBUG(dbgs() << "G_FPEXT to type " << Ty
990 << ", expected: " << LLT::scalar(64) << '\n');
991 return false;
992 }
993
994 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
995 DEBUG(dbgs() << "G_FPEXT from type " << Ty
996 << ", expected: " << LLT::scalar(32) << '\n');
997 return false;
998 }
999
1000 const unsigned DefReg = I.getOperand(0).getReg();
1001 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1002
1003 if (RB.getID() != AArch64::FPRRegBankID) {
1004 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1005 return false;
1006 }
1007
1008 I.setDesc(TII.get(AArch64::FCVTDSr));
1009 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1010
1011 return true;
1012 }
1013
1014 case TargetOpcode::G_FPTRUNC: {
1015 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1016 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1017 << ", expected: " << LLT::scalar(32) << '\n');
1018 return false;
1019 }
1020
1021 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1022 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1023 << ", expected: " << LLT::scalar(64) << '\n');
1024 return false;
1025 }
1026
1027 const unsigned DefReg = I.getOperand(0).getReg();
1028 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1029
1030 if (RB.getID() != AArch64::FPRRegBankID) {
1031 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1032 return false;
1033 }
1034
1035 I.setDesc(TII.get(AArch64::FCVTSDr));
1036 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1037
1038 return true;
1039 }
1040
Tim Northover9ac0eba2016-11-08 00:45:29 +00001041 case TargetOpcode::G_SELECT: {
1042 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1043 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1044 << ", expected: " << LLT::scalar(1) << '\n');
1045 return false;
1046 }
1047
1048 const unsigned CondReg = I.getOperand(1).getReg();
1049 const unsigned TReg = I.getOperand(2).getReg();
1050 const unsigned FReg = I.getOperand(3).getReg();
1051
1052 unsigned CSelOpc = 0;
1053
1054 if (Ty == LLT::scalar(32)) {
1055 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001056 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001057 CSelOpc = AArch64::CSELXr;
1058 } else {
1059 return false;
1060 }
1061
1062 MachineInstr &TstMI =
1063 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1064 .addDef(AArch64::WZR)
1065 .addUse(CondReg)
1066 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1067
1068 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1069 .addDef(I.getOperand(0).getReg())
1070 .addUse(TReg)
1071 .addUse(FReg)
1072 .addImm(AArch64CC::NE);
1073
1074 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1075 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1076
1077 I.eraseFromParent();
1078 return true;
1079 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001080 case TargetOpcode::G_ICMP: {
1081 if (Ty != LLT::scalar(1)) {
1082 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1083 << ", expected: " << LLT::scalar(1) << '\n');
1084 return false;
1085 }
1086
1087 unsigned CmpOpc = 0;
1088 unsigned ZReg = 0;
1089
1090 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1091 if (CmpTy == LLT::scalar(32)) {
1092 CmpOpc = AArch64::SUBSWrr;
1093 ZReg = AArch64::WZR;
1094 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1095 CmpOpc = AArch64::SUBSXrr;
1096 ZReg = AArch64::XZR;
1097 } else {
1098 return false;
1099 }
1100
Kristof Beyls22524402017-01-05 10:16:08 +00001101 // CSINC increments the result by one when the condition code is false.
1102 // Therefore, we have to invert the predicate to get an increment by 1 when
1103 // the predicate is true.
1104 const AArch64CC::CondCode invCC =
1105 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1106 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001107
1108 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1109 .addDef(ZReg)
1110 .addUse(I.getOperand(2).getReg())
1111 .addUse(I.getOperand(3).getReg());
1112
1113 MachineInstr &CSetMI =
1114 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1115 .addDef(I.getOperand(0).getReg())
1116 .addUse(AArch64::WZR)
1117 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001118 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001119
1120 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1121 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1122
1123 I.eraseFromParent();
1124 return true;
1125 }
1126
Tim Northover7dd378d2016-10-12 22:49:07 +00001127 case TargetOpcode::G_FCMP: {
1128 if (Ty != LLT::scalar(1)) {
1129 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1130 << ", expected: " << LLT::scalar(1) << '\n');
1131 return false;
1132 }
1133
1134 unsigned CmpOpc = 0;
1135 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1136 if (CmpTy == LLT::scalar(32)) {
1137 CmpOpc = AArch64::FCMPSrr;
1138 } else if (CmpTy == LLT::scalar(64)) {
1139 CmpOpc = AArch64::FCMPDrr;
1140 } else {
1141 return false;
1142 }
1143
1144 // FIXME: regbank
1145
1146 AArch64CC::CondCode CC1, CC2;
1147 changeFCMPPredToAArch64CC(
1148 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1149
1150 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1151 .addUse(I.getOperand(2).getReg())
1152 .addUse(I.getOperand(3).getReg());
1153
1154 const unsigned DefReg = I.getOperand(0).getReg();
1155 unsigned Def1Reg = DefReg;
1156 if (CC2 != AArch64CC::AL)
1157 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1158
1159 MachineInstr &CSetMI =
1160 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1161 .addDef(Def1Reg)
1162 .addUse(AArch64::WZR)
1163 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001164 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001165
1166 if (CC2 != AArch64CC::AL) {
1167 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1168 MachineInstr &CSet2MI =
1169 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1170 .addDef(Def2Reg)
1171 .addUse(AArch64::WZR)
1172 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001173 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001174 MachineInstr &OrMI =
1175 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1176 .addDef(DefReg)
1177 .addUse(Def1Reg)
1178 .addUse(Def2Reg);
1179 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1180 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1181 }
1182
1183 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1184 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1185
1186 I.eraseFromParent();
1187 return true;
1188 }
Tim Northovere9600d82017-02-08 17:57:27 +00001189 case TargetOpcode::G_VASTART:
1190 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1191 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001192 }
1193
1194 return false;
1195}