blob: 6981194525f6066310f9cc0cee1d3a2d90fe40d7 [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
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000718 case TargetOpcode::G_MUL: {
719 // Reject the various things we don't support yet.
720 if (unsupportedBinOp(I, RBI, MRI, TRI))
721 return false;
722
723 const unsigned DefReg = I.getOperand(0).getReg();
724 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
725
726 if (RB.getID() != AArch64::GPRRegBankID) {
727 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
728 return false;
729 }
730
731 unsigned ZeroReg;
732 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000733 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000734 NewOpc = AArch64::MADDWrrr;
735 ZeroReg = AArch64::WZR;
736 } else if (Ty == LLT::scalar(64)) {
737 NewOpc = AArch64::MADDXrrr;
738 ZeroReg = AArch64::XZR;
739 } else {
740 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
741 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
742 return false;
743 }
744
745 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000746
747 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
748
749 // Now that we selected an opcode, we need to constrain the register
750 // operands to use appropriate classes.
751 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
752 }
753
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000754 case TargetOpcode::G_FADD:
755 case TargetOpcode::G_FSUB:
756 case TargetOpcode::G_FMUL:
757 case TargetOpcode::G_FDIV:
758
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000759 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000760 case TargetOpcode::G_SHL:
761 case TargetOpcode::G_LSHR:
762 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000763 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000764 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000765 if (unsupportedBinOp(I, RBI, MRI, TRI))
766 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000767
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000768 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000769
770 const unsigned DefReg = I.getOperand(0).getReg();
771 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
772
773 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
774 if (NewOpc == I.getOpcode())
775 return false;
776
777 I.setDesc(TII.get(NewOpc));
778 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000779
780 // Now that we selected an opcode, we need to constrain the register
781 // operands to use appropriate classes.
782 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
783 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000784
Tim Northover037af52c2016-10-31 18:31:09 +0000785 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000786 case TargetOpcode::G_TRUNC: {
787 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
788 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
789
790 const unsigned DstReg = I.getOperand(0).getReg();
791 const unsigned SrcReg = I.getOperand(1).getReg();
792
793 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
794 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
795
796 if (DstRB.getID() != SrcRB.getID()) {
797 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
798 return false;
799 }
800
801 if (DstRB.getID() == AArch64::GPRRegBankID) {
802 const TargetRegisterClass *DstRC =
803 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
804 if (!DstRC)
805 return false;
806
807 const TargetRegisterClass *SrcRC =
808 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
809 if (!SrcRC)
810 return false;
811
812 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
813 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
814 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
815 return false;
816 }
817
818 if (DstRC == SrcRC) {
819 // Nothing to be done
820 } else if (DstRC == &AArch64::GPR32RegClass &&
821 SrcRC == &AArch64::GPR64RegClass) {
822 I.getOperand(1).setSubReg(AArch64::sub_32);
823 } else {
824 return false;
825 }
826
827 I.setDesc(TII.get(TargetOpcode::COPY));
828 return true;
829 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
830 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
831 I.setDesc(TII.get(AArch64::XTNv4i16));
832 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
833 return true;
834 }
835 }
836
837 return false;
838 }
839
Tim Northover3d38b3a2016-10-11 20:50:21 +0000840 case TargetOpcode::G_ANYEXT: {
841 const unsigned DstReg = I.getOperand(0).getReg();
842 const unsigned SrcReg = I.getOperand(1).getReg();
843
Quentin Colombetcb629a82016-10-12 03:57:49 +0000844 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
845 if (RBDst.getID() != AArch64::GPRRegBankID) {
846 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
847 return false;
848 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000849
Quentin Colombetcb629a82016-10-12 03:57:49 +0000850 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
851 if (RBSrc.getID() != AArch64::GPRRegBankID) {
852 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000853 return false;
854 }
855
856 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
857
858 if (DstSize == 0) {
859 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
860 return false;
861 }
862
Quentin Colombetcb629a82016-10-12 03:57:49 +0000863 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000864 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
865 << ", expected: 32 or 64\n");
866 return false;
867 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000868 // At this point G_ANYEXT is just like a plain COPY, but we need
869 // to explicitly form the 64-bit value if any.
870 if (DstSize > 32) {
871 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
872 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
873 .addDef(ExtSrc)
874 .addImm(0)
875 .addUse(SrcReg)
876 .addImm(AArch64::sub_32);
877 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000878 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000879 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000880 }
881
882 case TargetOpcode::G_ZEXT:
883 case TargetOpcode::G_SEXT: {
884 unsigned Opcode = I.getOpcode();
885 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
886 SrcTy = MRI.getType(I.getOperand(1).getReg());
887 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
888 const unsigned DefReg = I.getOperand(0).getReg();
889 const unsigned SrcReg = I.getOperand(1).getReg();
890 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
891
892 if (RB.getID() != AArch64::GPRRegBankID) {
893 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
894 << ", expected: GPR\n");
895 return false;
896 }
897
898 MachineInstr *ExtI;
899 if (DstTy == LLT::scalar(64)) {
900 // FIXME: Can we avoid manually doing this?
901 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
902 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
903 << " operand\n");
904 return false;
905 }
906
907 const unsigned SrcXReg =
908 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
909 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
910 .addDef(SrcXReg)
911 .addImm(0)
912 .addUse(SrcReg)
913 .addImm(AArch64::sub_32);
914
915 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
916 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
917 .addDef(DefReg)
918 .addUse(SrcXReg)
919 .addImm(0)
920 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000921 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000922 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
923 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
924 .addDef(DefReg)
925 .addUse(SrcReg)
926 .addImm(0)
927 .addImm(SrcTy.getSizeInBits() - 1);
928 } else {
929 return false;
930 }
931
932 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
933
934 I.eraseFromParent();
935 return true;
936 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000937
Tim Northover69271c62016-10-12 22:49:11 +0000938 case TargetOpcode::G_SITOFP:
939 case TargetOpcode::G_UITOFP:
940 case TargetOpcode::G_FPTOSI:
941 case TargetOpcode::G_FPTOUI: {
942 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
943 SrcTy = MRI.getType(I.getOperand(1).getReg());
944 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
945 if (NewOpc == Opcode)
946 return false;
947
948 I.setDesc(TII.get(NewOpc));
949 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
950
951 return true;
952 }
953
954
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000955 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000956 case TargetOpcode::G_BITCAST:
957 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000958
Tim Northover5f7dea82016-11-08 17:44:07 +0000959 case TargetOpcode::G_FPEXT: {
960 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
961 DEBUG(dbgs() << "G_FPEXT to type " << Ty
962 << ", expected: " << LLT::scalar(64) << '\n');
963 return false;
964 }
965
966 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
967 DEBUG(dbgs() << "G_FPEXT from type " << Ty
968 << ", expected: " << LLT::scalar(32) << '\n');
969 return false;
970 }
971
972 const unsigned DefReg = I.getOperand(0).getReg();
973 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
974
975 if (RB.getID() != AArch64::FPRRegBankID) {
976 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
977 return false;
978 }
979
980 I.setDesc(TII.get(AArch64::FCVTDSr));
981 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
982
983 return true;
984 }
985
986 case TargetOpcode::G_FPTRUNC: {
987 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
988 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
989 << ", expected: " << LLT::scalar(32) << '\n');
990 return false;
991 }
992
993 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
994 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
995 << ", expected: " << LLT::scalar(64) << '\n');
996 return false;
997 }
998
999 const unsigned DefReg = I.getOperand(0).getReg();
1000 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1001
1002 if (RB.getID() != AArch64::FPRRegBankID) {
1003 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1004 return false;
1005 }
1006
1007 I.setDesc(TII.get(AArch64::FCVTSDr));
1008 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1009
1010 return true;
1011 }
1012
Tim Northover9ac0eba2016-11-08 00:45:29 +00001013 case TargetOpcode::G_SELECT: {
1014 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1015 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1016 << ", expected: " << LLT::scalar(1) << '\n');
1017 return false;
1018 }
1019
1020 const unsigned CondReg = I.getOperand(1).getReg();
1021 const unsigned TReg = I.getOperand(2).getReg();
1022 const unsigned FReg = I.getOperand(3).getReg();
1023
1024 unsigned CSelOpc = 0;
1025
1026 if (Ty == LLT::scalar(32)) {
1027 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001028 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001029 CSelOpc = AArch64::CSELXr;
1030 } else {
1031 return false;
1032 }
1033
1034 MachineInstr &TstMI =
1035 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1036 .addDef(AArch64::WZR)
1037 .addUse(CondReg)
1038 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1039
1040 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1041 .addDef(I.getOperand(0).getReg())
1042 .addUse(TReg)
1043 .addUse(FReg)
1044 .addImm(AArch64CC::NE);
1045
1046 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1047 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1048
1049 I.eraseFromParent();
1050 return true;
1051 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001052 case TargetOpcode::G_ICMP: {
1053 if (Ty != LLT::scalar(1)) {
1054 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1055 << ", expected: " << LLT::scalar(1) << '\n');
1056 return false;
1057 }
1058
1059 unsigned CmpOpc = 0;
1060 unsigned ZReg = 0;
1061
1062 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1063 if (CmpTy == LLT::scalar(32)) {
1064 CmpOpc = AArch64::SUBSWrr;
1065 ZReg = AArch64::WZR;
1066 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1067 CmpOpc = AArch64::SUBSXrr;
1068 ZReg = AArch64::XZR;
1069 } else {
1070 return false;
1071 }
1072
Kristof Beyls22524402017-01-05 10:16:08 +00001073 // CSINC increments the result by one when the condition code is false.
1074 // Therefore, we have to invert the predicate to get an increment by 1 when
1075 // the predicate is true.
1076 const AArch64CC::CondCode invCC =
1077 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1078 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001079
1080 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1081 .addDef(ZReg)
1082 .addUse(I.getOperand(2).getReg())
1083 .addUse(I.getOperand(3).getReg());
1084
1085 MachineInstr &CSetMI =
1086 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1087 .addDef(I.getOperand(0).getReg())
1088 .addUse(AArch64::WZR)
1089 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001090 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001091
1092 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1093 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1094
1095 I.eraseFromParent();
1096 return true;
1097 }
1098
Tim Northover7dd378d2016-10-12 22:49:07 +00001099 case TargetOpcode::G_FCMP: {
1100 if (Ty != LLT::scalar(1)) {
1101 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1102 << ", expected: " << LLT::scalar(1) << '\n');
1103 return false;
1104 }
1105
1106 unsigned CmpOpc = 0;
1107 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1108 if (CmpTy == LLT::scalar(32)) {
1109 CmpOpc = AArch64::FCMPSrr;
1110 } else if (CmpTy == LLT::scalar(64)) {
1111 CmpOpc = AArch64::FCMPDrr;
1112 } else {
1113 return false;
1114 }
1115
1116 // FIXME: regbank
1117
1118 AArch64CC::CondCode CC1, CC2;
1119 changeFCMPPredToAArch64CC(
1120 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1121
1122 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1123 .addUse(I.getOperand(2).getReg())
1124 .addUse(I.getOperand(3).getReg());
1125
1126 const unsigned DefReg = I.getOperand(0).getReg();
1127 unsigned Def1Reg = DefReg;
1128 if (CC2 != AArch64CC::AL)
1129 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1130
1131 MachineInstr &CSetMI =
1132 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1133 .addDef(Def1Reg)
1134 .addUse(AArch64::WZR)
1135 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001136 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001137
1138 if (CC2 != AArch64CC::AL) {
1139 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1140 MachineInstr &CSet2MI =
1141 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1142 .addDef(Def2Reg)
1143 .addUse(AArch64::WZR)
1144 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001145 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001146 MachineInstr &OrMI =
1147 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1148 .addDef(DefReg)
1149 .addUse(Def1Reg)
1150 .addUse(Def2Reg);
1151 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1152 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1153 }
1154
1155 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1156 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1157
1158 I.eraseFromParent();
1159 return true;
1160 }
Tim Northovere9600d82017-02-08 17:57:27 +00001161 case TargetOpcode::G_VASTART:
1162 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1163 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001164 }
1165
1166 return false;
1167}