blob: 1656b5c6efc70d55a0724dd25b40687e5293f810 [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());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000637 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000638 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
639 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000640 } else if (I.getOperand(1).isImm()) {
641 uint64_t Val = I.getOperand(1).getImm();
642 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000643 }
644
645 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
646 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000647 }
648
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000649 case TargetOpcode::G_FRAME_INDEX: {
650 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000651 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000652 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000653 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000654 return false;
655 }
656
657 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000658
659 // MOs for a #0 shifted immediate.
660 I.addOperand(MachineOperand::CreateImm(0));
661 I.addOperand(MachineOperand::CreateImm(0));
662
663 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
664 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000665
666 case TargetOpcode::G_GLOBAL_VALUE: {
667 auto GV = I.getOperand(1).getGlobal();
668 if (GV->isThreadLocal()) {
669 // FIXME: we don't support TLS yet.
670 return false;
671 }
672 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000673 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000674 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000675 I.getOperand(1).setTargetFlags(OpFlags);
676 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000677 I.setDesc(TII.get(AArch64::MOVaddr));
678 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
679 MachineInstrBuilder MIB(MF, I);
680 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
681 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
682 }
683 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
684 }
685
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000686 case TargetOpcode::G_LOAD:
687 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000688 LLT MemTy = Ty;
689 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000690
Tim Northover5ae83502016-09-15 09:20:34 +0000691 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000692 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000693 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000694 return false;
695 }
696
Tim Northover48dfa1a2017-02-13 22:14:16 +0000697 auto &MemOp = **I.memoperands_begin();
698 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
699 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
700 return false;
701 }
702
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000703#ifndef NDEBUG
704 // Sanity-check the pointer register.
705 const unsigned PtrReg = I.getOperand(1).getReg();
706 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
707 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
708 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000709 assert(MRI.getType(PtrReg).isPointer() &&
710 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000711#endif
712
713 const unsigned ValReg = I.getOperand(0).getReg();
714 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
715
716 const unsigned NewOpc =
717 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
718 if (NewOpc == I.getOpcode())
719 return false;
720
721 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000722
723 I.addOperand(MachineOperand::CreateImm(0));
724 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
725 }
726
Tim Northover9dd78f82017-02-08 21:22:25 +0000727 case TargetOpcode::G_SMULH:
728 case TargetOpcode::G_UMULH: {
729 // Reject the various things we don't support yet.
730 if (unsupportedBinOp(I, RBI, MRI, TRI))
731 return false;
732
733 const unsigned DefReg = I.getOperand(0).getReg();
734 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
735
736 if (RB.getID() != AArch64::GPRRegBankID) {
737 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
738 return false;
739 }
740
741 if (Ty != LLT::scalar(64)) {
742 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
743 << ", expected: " << LLT::scalar(64) << '\n');
744 return false;
745 }
746
747 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
748 : AArch64::UMULHrr;
749 I.setDesc(TII.get(NewOpc));
750
751 // Now that we selected an opcode, we need to constrain the register
752 // operands to use appropriate classes.
753 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
754 }
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000755 case TargetOpcode::G_MUL: {
756 // Reject the various things we don't support yet.
757 if (unsupportedBinOp(I, RBI, MRI, TRI))
758 return false;
759
760 const unsigned DefReg = I.getOperand(0).getReg();
761 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
762
763 if (RB.getID() != AArch64::GPRRegBankID) {
764 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
765 return false;
766 }
767
768 unsigned ZeroReg;
769 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000770 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000771 NewOpc = AArch64::MADDWrrr;
772 ZeroReg = AArch64::WZR;
773 } else if (Ty == LLT::scalar(64)) {
774 NewOpc = AArch64::MADDXrrr;
775 ZeroReg = AArch64::XZR;
776 } else {
777 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
778 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
779 return false;
780 }
781
782 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000783
784 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
785
786 // Now that we selected an opcode, we need to constrain the register
787 // operands to use appropriate classes.
788 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
789 }
790
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000791 case TargetOpcode::G_FADD:
792 case TargetOpcode::G_FSUB:
793 case TargetOpcode::G_FMUL:
794 case TargetOpcode::G_FDIV:
795
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000796 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000797 case TargetOpcode::G_SHL:
798 case TargetOpcode::G_LSHR:
799 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000800 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000801 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000802 if (unsupportedBinOp(I, RBI, MRI, TRI))
803 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000804
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000805 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000806
807 const unsigned DefReg = I.getOperand(0).getReg();
808 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
809
810 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
811 if (NewOpc == I.getOpcode())
812 return false;
813
814 I.setDesc(TII.get(NewOpc));
815 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000816
817 // Now that we selected an opcode, we need to constrain the register
818 // operands to use appropriate classes.
819 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
820 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000821
Tim Northover398c5f52017-02-14 20:56:29 +0000822 case TargetOpcode::G_PTR_MASK: {
823 uint64_t Align = I.getOperand(2).getImm();
824 if (Align >= 64 || Align == 0)
825 return false;
826
827 uint64_t Mask = ~((1ULL << Align) - 1);
828 I.setDesc(TII.get(AArch64::ANDXri));
829 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
830
831 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
832 }
Tim Northover037af52c2016-10-31 18:31:09 +0000833 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000834 case TargetOpcode::G_TRUNC: {
835 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
836 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
837
838 const unsigned DstReg = I.getOperand(0).getReg();
839 const unsigned SrcReg = I.getOperand(1).getReg();
840
841 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
842 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
843
844 if (DstRB.getID() != SrcRB.getID()) {
845 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
846 return false;
847 }
848
849 if (DstRB.getID() == AArch64::GPRRegBankID) {
850 const TargetRegisterClass *DstRC =
851 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
852 if (!DstRC)
853 return false;
854
855 const TargetRegisterClass *SrcRC =
856 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
857 if (!SrcRC)
858 return false;
859
860 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
861 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
862 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
863 return false;
864 }
865
866 if (DstRC == SrcRC) {
867 // Nothing to be done
868 } else if (DstRC == &AArch64::GPR32RegClass &&
869 SrcRC == &AArch64::GPR64RegClass) {
870 I.getOperand(1).setSubReg(AArch64::sub_32);
871 } else {
872 return false;
873 }
874
875 I.setDesc(TII.get(TargetOpcode::COPY));
876 return true;
877 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
878 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
879 I.setDesc(TII.get(AArch64::XTNv4i16));
880 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
881 return true;
882 }
883 }
884
885 return false;
886 }
887
Tim Northover3d38b3a2016-10-11 20:50:21 +0000888 case TargetOpcode::G_ANYEXT: {
889 const unsigned DstReg = I.getOperand(0).getReg();
890 const unsigned SrcReg = I.getOperand(1).getReg();
891
Quentin Colombetcb629a82016-10-12 03:57:49 +0000892 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
893 if (RBDst.getID() != AArch64::GPRRegBankID) {
894 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
895 return false;
896 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000897
Quentin Colombetcb629a82016-10-12 03:57:49 +0000898 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
899 if (RBSrc.getID() != AArch64::GPRRegBankID) {
900 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000901 return false;
902 }
903
904 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
905
906 if (DstSize == 0) {
907 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
908 return false;
909 }
910
Quentin Colombetcb629a82016-10-12 03:57:49 +0000911 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000912 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
913 << ", expected: 32 or 64\n");
914 return false;
915 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000916 // At this point G_ANYEXT is just like a plain COPY, but we need
917 // to explicitly form the 64-bit value if any.
918 if (DstSize > 32) {
919 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
920 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
921 .addDef(ExtSrc)
922 .addImm(0)
923 .addUse(SrcReg)
924 .addImm(AArch64::sub_32);
925 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000926 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000927 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000928 }
929
930 case TargetOpcode::G_ZEXT:
931 case TargetOpcode::G_SEXT: {
932 unsigned Opcode = I.getOpcode();
933 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
934 SrcTy = MRI.getType(I.getOperand(1).getReg());
935 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
936 const unsigned DefReg = I.getOperand(0).getReg();
937 const unsigned SrcReg = I.getOperand(1).getReg();
938 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
939
940 if (RB.getID() != AArch64::GPRRegBankID) {
941 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
942 << ", expected: GPR\n");
943 return false;
944 }
945
946 MachineInstr *ExtI;
947 if (DstTy == LLT::scalar(64)) {
948 // FIXME: Can we avoid manually doing this?
949 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
950 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
951 << " operand\n");
952 return false;
953 }
954
955 const unsigned SrcXReg =
956 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
957 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
958 .addDef(SrcXReg)
959 .addImm(0)
960 .addUse(SrcReg)
961 .addImm(AArch64::sub_32);
962
963 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
964 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
965 .addDef(DefReg)
966 .addUse(SrcXReg)
967 .addImm(0)
968 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000969 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000970 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
971 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
972 .addDef(DefReg)
973 .addUse(SrcReg)
974 .addImm(0)
975 .addImm(SrcTy.getSizeInBits() - 1);
976 } else {
977 return false;
978 }
979
980 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
981
982 I.eraseFromParent();
983 return true;
984 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000985
Tim Northover69271c62016-10-12 22:49:11 +0000986 case TargetOpcode::G_SITOFP:
987 case TargetOpcode::G_UITOFP:
988 case TargetOpcode::G_FPTOSI:
989 case TargetOpcode::G_FPTOUI: {
990 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
991 SrcTy = MRI.getType(I.getOperand(1).getReg());
992 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
993 if (NewOpc == Opcode)
994 return false;
995
996 I.setDesc(TII.get(NewOpc));
997 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
998
999 return true;
1000 }
1001
1002
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001003 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001004 case TargetOpcode::G_BITCAST:
1005 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001006
Tim Northover5f7dea82016-11-08 17:44:07 +00001007 case TargetOpcode::G_FPEXT: {
1008 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1009 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1010 << ", expected: " << LLT::scalar(64) << '\n');
1011 return false;
1012 }
1013
1014 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1015 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1016 << ", expected: " << LLT::scalar(32) << '\n');
1017 return false;
1018 }
1019
1020 const unsigned DefReg = I.getOperand(0).getReg();
1021 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1022
1023 if (RB.getID() != AArch64::FPRRegBankID) {
1024 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1025 return false;
1026 }
1027
1028 I.setDesc(TII.get(AArch64::FCVTDSr));
1029 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1030
1031 return true;
1032 }
1033
1034 case TargetOpcode::G_FPTRUNC: {
1035 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1036 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1037 << ", expected: " << LLT::scalar(32) << '\n');
1038 return false;
1039 }
1040
1041 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1042 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1043 << ", expected: " << LLT::scalar(64) << '\n');
1044 return false;
1045 }
1046
1047 const unsigned DefReg = I.getOperand(0).getReg();
1048 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1049
1050 if (RB.getID() != AArch64::FPRRegBankID) {
1051 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1052 return false;
1053 }
1054
1055 I.setDesc(TII.get(AArch64::FCVTSDr));
1056 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1057
1058 return true;
1059 }
1060
Tim Northover9ac0eba2016-11-08 00:45:29 +00001061 case TargetOpcode::G_SELECT: {
1062 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1063 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1064 << ", expected: " << LLT::scalar(1) << '\n');
1065 return false;
1066 }
1067
1068 const unsigned CondReg = I.getOperand(1).getReg();
1069 const unsigned TReg = I.getOperand(2).getReg();
1070 const unsigned FReg = I.getOperand(3).getReg();
1071
1072 unsigned CSelOpc = 0;
1073
1074 if (Ty == LLT::scalar(32)) {
1075 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001076 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001077 CSelOpc = AArch64::CSELXr;
1078 } else {
1079 return false;
1080 }
1081
1082 MachineInstr &TstMI =
1083 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1084 .addDef(AArch64::WZR)
1085 .addUse(CondReg)
1086 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1087
1088 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1089 .addDef(I.getOperand(0).getReg())
1090 .addUse(TReg)
1091 .addUse(FReg)
1092 .addImm(AArch64CC::NE);
1093
1094 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1095 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1096
1097 I.eraseFromParent();
1098 return true;
1099 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001100 case TargetOpcode::G_ICMP: {
1101 if (Ty != LLT::scalar(1)) {
1102 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1103 << ", expected: " << LLT::scalar(1) << '\n');
1104 return false;
1105 }
1106
1107 unsigned CmpOpc = 0;
1108 unsigned ZReg = 0;
1109
1110 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1111 if (CmpTy == LLT::scalar(32)) {
1112 CmpOpc = AArch64::SUBSWrr;
1113 ZReg = AArch64::WZR;
1114 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1115 CmpOpc = AArch64::SUBSXrr;
1116 ZReg = AArch64::XZR;
1117 } else {
1118 return false;
1119 }
1120
Kristof Beyls22524402017-01-05 10:16:08 +00001121 // CSINC increments the result by one when the condition code is false.
1122 // Therefore, we have to invert the predicate to get an increment by 1 when
1123 // the predicate is true.
1124 const AArch64CC::CondCode invCC =
1125 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1126 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001127
1128 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1129 .addDef(ZReg)
1130 .addUse(I.getOperand(2).getReg())
1131 .addUse(I.getOperand(3).getReg());
1132
1133 MachineInstr &CSetMI =
1134 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1135 .addDef(I.getOperand(0).getReg())
1136 .addUse(AArch64::WZR)
1137 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001138 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001139
1140 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1141 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1142
1143 I.eraseFromParent();
1144 return true;
1145 }
1146
Tim Northover7dd378d2016-10-12 22:49:07 +00001147 case TargetOpcode::G_FCMP: {
1148 if (Ty != LLT::scalar(1)) {
1149 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1150 << ", expected: " << LLT::scalar(1) << '\n');
1151 return false;
1152 }
1153
1154 unsigned CmpOpc = 0;
1155 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1156 if (CmpTy == LLT::scalar(32)) {
1157 CmpOpc = AArch64::FCMPSrr;
1158 } else if (CmpTy == LLT::scalar(64)) {
1159 CmpOpc = AArch64::FCMPDrr;
1160 } else {
1161 return false;
1162 }
1163
1164 // FIXME: regbank
1165
1166 AArch64CC::CondCode CC1, CC2;
1167 changeFCMPPredToAArch64CC(
1168 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1169
1170 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1171 .addUse(I.getOperand(2).getReg())
1172 .addUse(I.getOperand(3).getReg());
1173
1174 const unsigned DefReg = I.getOperand(0).getReg();
1175 unsigned Def1Reg = DefReg;
1176 if (CC2 != AArch64CC::AL)
1177 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1178
1179 MachineInstr &CSetMI =
1180 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1181 .addDef(Def1Reg)
1182 .addUse(AArch64::WZR)
1183 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001184 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001185
1186 if (CC2 != AArch64CC::AL) {
1187 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1188 MachineInstr &CSet2MI =
1189 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1190 .addDef(Def2Reg)
1191 .addUse(AArch64::WZR)
1192 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001193 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001194 MachineInstr &OrMI =
1195 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1196 .addDef(DefReg)
1197 .addUse(Def1Reg)
1198 .addUse(Def2Reg);
1199 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1200 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1201 }
1202
1203 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1204 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1205
1206 I.eraseFromParent();
1207 return true;
1208 }
Tim Northovere9600d82017-02-08 17:57:27 +00001209 case TargetOpcode::G_VASTART:
1210 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1211 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001212 }
1213
1214 return false;
1215}