blob: dea651631bdf01672412faf0e2202ea31dcf598a [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"
17#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000020#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000021#include "MCTargetDesc/AArch64AddressingModes.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstr.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/IR/Type.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
30
31#define DEBUG_TYPE "aarch64-isel"
32
33using namespace llvm;
34
35#ifndef LLVM_BUILD_GLOBAL_ISEL
36#error "You shouldn't build this"
37#endif
38
Ahmed Bougacha36f70352016-12-21 23:26:20 +000039#include "AArch64GenGlobalISel.inc"
40
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000041AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000042 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
43 const AArch64RegisterBankInfo &RBI)
44 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000045 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
46
Tim Northoverfb8d9892016-10-12 22:49:15 +000047// FIXME: This should be target-independent, inferred from the types declared
48// for each class in the bank.
49static const TargetRegisterClass *
50getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
51 const RegisterBankInfo &RBI) {
52 if (RB.getID() == AArch64::GPRRegBankID) {
53 if (Ty.getSizeInBits() <= 32)
54 return &AArch64::GPR32RegClass;
55 if (Ty.getSizeInBits() == 64)
56 return &AArch64::GPR64RegClass;
57 return nullptr;
58 }
59
60 if (RB.getID() == AArch64::FPRRegBankID) {
61 if (Ty.getSizeInBits() == 32)
62 return &AArch64::FPR32RegClass;
63 if (Ty.getSizeInBits() == 64)
64 return &AArch64::FPR64RegClass;
65 if (Ty.getSizeInBits() == 128)
66 return &AArch64::FPR128RegClass;
67 return nullptr;
68 }
69
70 return nullptr;
71}
72
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000073/// Check whether \p I is a currently unsupported binary operation:
74/// - it has an unsized type
75/// - an operand is not a vreg
76/// - all operands are not in the same bank
77/// These are checks that should someday live in the verifier, but right now,
78/// these are mostly limitations of the aarch64 selector.
79static bool unsupportedBinOp(const MachineInstr &I,
80 const AArch64RegisterBankInfo &RBI,
81 const MachineRegisterInfo &MRI,
82 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000083 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000084 if (!Ty.isValid()) {
85 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000086 return true;
87 }
88
89 const RegisterBank *PrevOpBank = nullptr;
90 for (auto &MO : I.operands()) {
91 // FIXME: Support non-register operands.
92 if (!MO.isReg()) {
93 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
94 return true;
95 }
96
97 // FIXME: Can generic operations have physical registers operands? If
98 // so, this will need to be taught about that, and we'll need to get the
99 // bank out of the minimal class for the register.
100 // Either way, this needs to be documented (and possibly verified).
101 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
102 DEBUG(dbgs() << "Generic inst has physical register operand\n");
103 return true;
104 }
105
106 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
107 if (!OpBank) {
108 DEBUG(dbgs() << "Generic register has no bank or class\n");
109 return true;
110 }
111
112 if (PrevOpBank && OpBank != PrevOpBank) {
113 DEBUG(dbgs() << "Generic inst operands have different banks\n");
114 return true;
115 }
116 PrevOpBank = OpBank;
117 }
118 return false;
119}
120
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000121/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000122/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000123/// and of size \p OpSize.
124/// \returns \p GenericOpc if the combination is unsupported.
125static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
126 unsigned OpSize) {
127 switch (RegBankID) {
128 case AArch64::GPRRegBankID:
Tim Northover55782222016-10-18 20:03:48 +0000129 if (OpSize <= 32) {
130 assert((OpSize == 32 || (GenericOpc != TargetOpcode::G_SDIV &&
131 GenericOpc != TargetOpcode::G_UDIV &&
132 GenericOpc != TargetOpcode::G_LSHR &&
133 GenericOpc != TargetOpcode::G_ASHR)) &&
134 "operation should have been legalized before now");
135
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000136 switch (GenericOpc) {
137 case TargetOpcode::G_OR:
138 return AArch64::ORRWrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000139 case TargetOpcode::G_XOR:
140 return AArch64::EORWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000141 case TargetOpcode::G_AND:
142 return AArch64::ANDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000143 case TargetOpcode::G_SUB:
144 return AArch64::SUBWrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000145 case TargetOpcode::G_SHL:
146 return AArch64::LSLVWr;
147 case TargetOpcode::G_LSHR:
148 return AArch64::LSRVWr;
149 case TargetOpcode::G_ASHR:
150 return AArch64::ASRVWr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000151 case TargetOpcode::G_SDIV:
152 return AArch64::SDIVWr;
153 case TargetOpcode::G_UDIV:
154 return AArch64::UDIVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000155 default:
156 return GenericOpc;
157 }
Tim Northover55782222016-10-18 20:03:48 +0000158 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000159 switch (GenericOpc) {
160 case TargetOpcode::G_OR:
161 return AArch64::ORRXrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000162 case TargetOpcode::G_XOR:
163 return AArch64::EORXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000164 case TargetOpcode::G_AND:
165 return AArch64::ANDXrr;
Tim Northover2fda4b02016-10-10 21:49:49 +0000166 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000167 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000168 case TargetOpcode::G_SUB:
169 return AArch64::SUBXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000170 case TargetOpcode::G_SHL:
171 return AArch64::LSLVXr;
172 case TargetOpcode::G_LSHR:
173 return AArch64::LSRVXr;
174 case TargetOpcode::G_ASHR:
175 return AArch64::ASRVXr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000176 case TargetOpcode::G_SDIV:
177 return AArch64::SDIVXr;
178 case TargetOpcode::G_UDIV:
179 return AArch64::UDIVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000180 default:
181 return GenericOpc;
182 }
183 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000184 case AArch64::FPRRegBankID:
185 switch (OpSize) {
186 case 32:
187 switch (GenericOpc) {
188 case TargetOpcode::G_FADD:
189 return AArch64::FADDSrr;
190 case TargetOpcode::G_FSUB:
191 return AArch64::FSUBSrr;
192 case TargetOpcode::G_FMUL:
193 return AArch64::FMULSrr;
194 case TargetOpcode::G_FDIV:
195 return AArch64::FDIVSrr;
196 default:
197 return GenericOpc;
198 }
199 case 64:
200 switch (GenericOpc) {
201 case TargetOpcode::G_FADD:
202 return AArch64::FADDDrr;
203 case TargetOpcode::G_FSUB:
204 return AArch64::FSUBDrr;
205 case TargetOpcode::G_FMUL:
206 return AArch64::FMULDrr;
207 case TargetOpcode::G_FDIV:
208 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000209 case TargetOpcode::G_OR:
210 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000211 default:
212 return GenericOpc;
213 }
214 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000215 };
216 return GenericOpc;
217}
218
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000219/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
220/// appropriate for the (value) register bank \p RegBankID and of memory access
221/// size \p OpSize. This returns the variant with the base+unsigned-immediate
222/// addressing mode (e.g., LDRXui).
223/// \returns \p GenericOpc if the combination is unsupported.
224static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
225 unsigned OpSize) {
226 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
227 switch (RegBankID) {
228 case AArch64::GPRRegBankID:
229 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000230 case 8:
231 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
232 case 16:
233 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000234 case 32:
235 return isStore ? AArch64::STRWui : AArch64::LDRWui;
236 case 64:
237 return isStore ? AArch64::STRXui : AArch64::LDRXui;
238 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000239 case AArch64::FPRRegBankID:
240 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000241 case 8:
242 return isStore ? AArch64::STRBui : AArch64::LDRBui;
243 case 16:
244 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000245 case 32:
246 return isStore ? AArch64::STRSui : AArch64::LDRSui;
247 case 64:
248 return isStore ? AArch64::STRDui : AArch64::LDRDui;
249 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000250 };
251 return GenericOpc;
252}
253
Quentin Colombetcb629a82016-10-12 03:57:49 +0000254static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
255 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
256 const RegisterBankInfo &RBI) {
257
258 unsigned DstReg = I.getOperand(0).getReg();
259 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
260 assert(I.isCopy() && "Generic operators do not allow physical registers");
261 return true;
262 }
263
264 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
265 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
266 unsigned SrcReg = I.getOperand(1).getReg();
267 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
268 (void)SrcSize;
269 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
270 "No phys reg on generic operators");
271 assert(
272 (DstSize == SrcSize ||
273 // Copies are a mean to setup initial types, the number of
274 // bits may not exactly match.
275 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
276 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
277 // Copies are a mean to copy bits around, as long as we are
278 // on the same register class, that's fine. Otherwise, that
279 // means we need some SUBREG_TO_REG or AND & co.
280 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
281 "Copy with different width?!");
282 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
283 "GPRs cannot get more than 64-bit width values");
284 const TargetRegisterClass *RC = nullptr;
285
286 if (RegBank.getID() == AArch64::FPRRegBankID) {
287 if (DstSize <= 32)
288 RC = &AArch64::FPR32RegClass;
289 else if (DstSize <= 64)
290 RC = &AArch64::FPR64RegClass;
291 else if (DstSize <= 128)
292 RC = &AArch64::FPR128RegClass;
293 else {
294 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
295 return false;
296 }
297 } else {
298 assert(RegBank.getID() == AArch64::GPRRegBankID &&
299 "Bitcast for the flags?");
300 RC =
301 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
302 }
303
304 // No need to constrain SrcReg. It will get constrained when
305 // we hit another of its use or its defs.
306 // Copies do not have constraints.
307 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
308 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
309 << " operand\n");
310 return false;
311 }
312 I.setDesc(TII.get(AArch64::COPY));
313 return true;
314}
315
Tim Northover69271c62016-10-12 22:49:11 +0000316static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
317 if (!DstTy.isScalar() || !SrcTy.isScalar())
318 return GenericOpc;
319
320 const unsigned DstSize = DstTy.getSizeInBits();
321 const unsigned SrcSize = SrcTy.getSizeInBits();
322
323 switch (DstSize) {
324 case 32:
325 switch (SrcSize) {
326 case 32:
327 switch (GenericOpc) {
328 case TargetOpcode::G_SITOFP:
329 return AArch64::SCVTFUWSri;
330 case TargetOpcode::G_UITOFP:
331 return AArch64::UCVTFUWSri;
332 case TargetOpcode::G_FPTOSI:
333 return AArch64::FCVTZSUWSr;
334 case TargetOpcode::G_FPTOUI:
335 return AArch64::FCVTZUUWSr;
336 default:
337 return GenericOpc;
338 }
339 case 64:
340 switch (GenericOpc) {
341 case TargetOpcode::G_SITOFP:
342 return AArch64::SCVTFUXSri;
343 case TargetOpcode::G_UITOFP:
344 return AArch64::UCVTFUXSri;
345 case TargetOpcode::G_FPTOSI:
346 return AArch64::FCVTZSUWDr;
347 case TargetOpcode::G_FPTOUI:
348 return AArch64::FCVTZUUWDr;
349 default:
350 return GenericOpc;
351 }
352 default:
353 return GenericOpc;
354 }
355 case 64:
356 switch (SrcSize) {
357 case 32:
358 switch (GenericOpc) {
359 case TargetOpcode::G_SITOFP:
360 return AArch64::SCVTFUWDri;
361 case TargetOpcode::G_UITOFP:
362 return AArch64::UCVTFUWDri;
363 case TargetOpcode::G_FPTOSI:
364 return AArch64::FCVTZSUXSr;
365 case TargetOpcode::G_FPTOUI:
366 return AArch64::FCVTZUUXSr;
367 default:
368 return GenericOpc;
369 }
370 case 64:
371 switch (GenericOpc) {
372 case TargetOpcode::G_SITOFP:
373 return AArch64::SCVTFUXDri;
374 case TargetOpcode::G_UITOFP:
375 return AArch64::UCVTFUXDri;
376 case TargetOpcode::G_FPTOSI:
377 return AArch64::FCVTZSUXDr;
378 case TargetOpcode::G_FPTOUI:
379 return AArch64::FCVTZUUXDr;
380 default:
381 return GenericOpc;
382 }
383 default:
384 return GenericOpc;
385 }
386 default:
387 return GenericOpc;
388 };
389 return GenericOpc;
390}
391
Tim Northover6c02ad52016-10-12 22:49:04 +0000392static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
393 switch (P) {
394 default:
395 llvm_unreachable("Unknown condition code!");
396 case CmpInst::ICMP_NE:
397 return AArch64CC::NE;
398 case CmpInst::ICMP_EQ:
399 return AArch64CC::EQ;
400 case CmpInst::ICMP_SGT:
401 return AArch64CC::GT;
402 case CmpInst::ICMP_SGE:
403 return AArch64CC::GE;
404 case CmpInst::ICMP_SLT:
405 return AArch64CC::LT;
406 case CmpInst::ICMP_SLE:
407 return AArch64CC::LE;
408 case CmpInst::ICMP_UGT:
409 return AArch64CC::HI;
410 case CmpInst::ICMP_UGE:
411 return AArch64CC::HS;
412 case CmpInst::ICMP_ULT:
413 return AArch64CC::LO;
414 case CmpInst::ICMP_ULE:
415 return AArch64CC::LS;
416 }
417}
418
Tim Northover7dd378d2016-10-12 22:49:07 +0000419static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
420 AArch64CC::CondCode &CondCode,
421 AArch64CC::CondCode &CondCode2) {
422 CondCode2 = AArch64CC::AL;
423 switch (P) {
424 default:
425 llvm_unreachable("Unknown FP condition!");
426 case CmpInst::FCMP_OEQ:
427 CondCode = AArch64CC::EQ;
428 break;
429 case CmpInst::FCMP_OGT:
430 CondCode = AArch64CC::GT;
431 break;
432 case CmpInst::FCMP_OGE:
433 CondCode = AArch64CC::GE;
434 break;
435 case CmpInst::FCMP_OLT:
436 CondCode = AArch64CC::MI;
437 break;
438 case CmpInst::FCMP_OLE:
439 CondCode = AArch64CC::LS;
440 break;
441 case CmpInst::FCMP_ONE:
442 CondCode = AArch64CC::MI;
443 CondCode2 = AArch64CC::GT;
444 break;
445 case CmpInst::FCMP_ORD:
446 CondCode = AArch64CC::VC;
447 break;
448 case CmpInst::FCMP_UNO:
449 CondCode = AArch64CC::VS;
450 break;
451 case CmpInst::FCMP_UEQ:
452 CondCode = AArch64CC::EQ;
453 CondCode2 = AArch64CC::VS;
454 break;
455 case CmpInst::FCMP_UGT:
456 CondCode = AArch64CC::HI;
457 break;
458 case CmpInst::FCMP_UGE:
459 CondCode = AArch64CC::PL;
460 break;
461 case CmpInst::FCMP_ULT:
462 CondCode = AArch64CC::LT;
463 break;
464 case CmpInst::FCMP_ULE:
465 CondCode = AArch64CC::LE;
466 break;
467 case CmpInst::FCMP_UNE:
468 CondCode = AArch64CC::NE;
469 break;
470 }
471}
472
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000473bool AArch64InstructionSelector::select(MachineInstr &I) const {
474 assert(I.getParent() && "Instruction should be in a basic block!");
475 assert(I.getParent()->getParent() && "Instruction should be in a function!");
476
477 MachineBasicBlock &MBB = *I.getParent();
478 MachineFunction &MF = *MBB.getParent();
479 MachineRegisterInfo &MRI = MF.getRegInfo();
480
Tim Northovercdf23f12016-10-31 18:30:59 +0000481 unsigned Opcode = I.getOpcode();
482 if (!isPreISelGenericOpcode(I.getOpcode())) {
483 // Certain non-generic instructions also need some special handling.
484
485 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
486 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000487
488 if (Opcode == TargetOpcode::PHI) {
489 const unsigned DefReg = I.getOperand(0).getReg();
490 const LLT DefTy = MRI.getType(DefReg);
491
492 const TargetRegisterClass *DefRC = nullptr;
493 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
494 DefRC = TRI.getRegClass(DefReg);
495 } else {
496 const RegClassOrRegBank &RegClassOrBank =
497 MRI.getRegClassOrRegBank(DefReg);
498
499 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
500 if (!DefRC) {
501 if (!DefTy.isValid()) {
502 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
503 return false;
504 }
505 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
506 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
507 if (!DefRC) {
508 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
509 return false;
510 }
511 }
512 }
513
514 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
515 }
516
517 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000518 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000519
520 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000521 }
522
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000523
524 if (I.getNumOperands() != I.getNumExplicitOperands()) {
525 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
526 return false;
527 }
528
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000529 if (selectImpl(I))
530 return true;
531
Tim Northover32a078a2016-09-15 10:09:59 +0000532 LLT Ty =
533 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000534
Tim Northover69271c62016-10-12 22:49:11 +0000535 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000536 case TargetOpcode::G_BRCOND: {
537 if (Ty.getSizeInBits() > 32) {
538 // We shouldn't need this on AArch64, but it would be implemented as an
539 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
540 // bit being tested is < 32.
541 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
542 << ", expected at most 32-bits");
543 return false;
544 }
545
546 const unsigned CondReg = I.getOperand(0).getReg();
547 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
548
549 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
550 .addUse(CondReg)
551 .addImm(/*bit offset=*/0)
552 .addMBB(DestMBB);
553
554 I.eraseFromParent();
555 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
556 }
557
Tim Northover4494d692016-10-18 19:47:57 +0000558 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000559 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000560 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
561
562 const LLT s32 = LLT::scalar(32);
563 const LLT s64 = LLT::scalar(64);
564 const LLT p0 = LLT::pointer(0, 64);
565
566 const unsigned DefReg = I.getOperand(0).getReg();
567 const LLT DefTy = MRI.getType(DefReg);
568 const unsigned DefSize = DefTy.getSizeInBits();
569 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
570
571 // FIXME: Redundant check, but even less readable when factored out.
572 if (isFP) {
573 if (Ty != s32 && Ty != s64) {
574 DEBUG(dbgs() << "Unable to materialize FP " << Ty
575 << " constant, expected: " << s32 << " or " << s64
576 << '\n');
577 return false;
578 }
579
580 if (RB.getID() != AArch64::FPRRegBankID) {
581 DEBUG(dbgs() << "Unable to materialize FP " << Ty
582 << " constant on bank: " << RB << ", expected: FPR\n");
583 return false;
584 }
585 } else {
586 if (Ty != s32 && Ty != s64 && Ty != p0) {
587 DEBUG(dbgs() << "Unable to materialize integer " << Ty
588 << " constant, expected: " << s32 << ", " << s64 << ", or "
589 << p0 << '\n');
590 return false;
591 }
592
593 if (RB.getID() != AArch64::GPRRegBankID) {
594 DEBUG(dbgs() << "Unable to materialize integer " << Ty
595 << " constant on bank: " << RB << ", expected: GPR\n");
596 return false;
597 }
598 }
599
600 const unsigned MovOpc =
601 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
602
603 I.setDesc(TII.get(MovOpc));
604
605 if (isFP) {
606 const TargetRegisterClass &GPRRC =
607 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
608 const TargetRegisterClass &FPRRC =
609 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
610
611 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
612 MachineOperand &RegOp = I.getOperand(0);
613 RegOp.setReg(DefGPRReg);
614
615 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
616 TII.get(AArch64::COPY))
617 .addDef(DefReg)
618 .addUse(DefGPRReg);
619
620 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
621 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
622 return false;
623 }
624
625 MachineOperand &ImmOp = I.getOperand(1);
626 // FIXME: Is going through int64_t always correct?
627 ImmOp.ChangeToImmediate(
628 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Tim Northover9267ac52016-12-05 21:47:07 +0000629 } else {
630 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
631 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000632 }
633
634 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
635 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000636 }
637
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000638 case TargetOpcode::G_FRAME_INDEX: {
639 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000640 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000641 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000642 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000643 return false;
644 }
645
646 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000647
648 // MOs for a #0 shifted immediate.
649 I.addOperand(MachineOperand::CreateImm(0));
650 I.addOperand(MachineOperand::CreateImm(0));
651
652 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
653 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000654
655 case TargetOpcode::G_GLOBAL_VALUE: {
656 auto GV = I.getOperand(1).getGlobal();
657 if (GV->isThreadLocal()) {
658 // FIXME: we don't support TLS yet.
659 return false;
660 }
661 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000662 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000663 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000664 I.getOperand(1).setTargetFlags(OpFlags);
665 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000666 I.setDesc(TII.get(AArch64::MOVaddr));
667 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
668 MachineInstrBuilder MIB(MF, I);
669 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
670 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
671 }
672 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
673 }
674
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000675 case TargetOpcode::G_LOAD:
676 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000677 LLT MemTy = Ty;
678 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000679
Tim Northover5ae83502016-09-15 09:20:34 +0000680 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000681 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000682 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000683 return false;
684 }
685
686#ifndef NDEBUG
687 // Sanity-check the pointer register.
688 const unsigned PtrReg = I.getOperand(1).getReg();
689 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
690 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
691 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000692 assert(MRI.getType(PtrReg).isPointer() &&
693 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000694#endif
695
696 const unsigned ValReg = I.getOperand(0).getReg();
697 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
698
699 const unsigned NewOpc =
700 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
701 if (NewOpc == I.getOpcode())
702 return false;
703
704 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000705
706 I.addOperand(MachineOperand::CreateImm(0));
707 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
708 }
709
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000710 case TargetOpcode::G_MUL: {
711 // Reject the various things we don't support yet.
712 if (unsupportedBinOp(I, RBI, MRI, TRI))
713 return false;
714
715 const unsigned DefReg = I.getOperand(0).getReg();
716 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
717
718 if (RB.getID() != AArch64::GPRRegBankID) {
719 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
720 return false;
721 }
722
723 unsigned ZeroReg;
724 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000725 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000726 NewOpc = AArch64::MADDWrrr;
727 ZeroReg = AArch64::WZR;
728 } else if (Ty == LLT::scalar(64)) {
729 NewOpc = AArch64::MADDXrrr;
730 ZeroReg = AArch64::XZR;
731 } else {
732 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
733 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
734 return false;
735 }
736
737 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000738
739 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
740
741 // Now that we selected an opcode, we need to constrain the register
742 // operands to use appropriate classes.
743 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
744 }
745
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000746 case TargetOpcode::G_FADD:
747 case TargetOpcode::G_FSUB:
748 case TargetOpcode::G_FMUL:
749 case TargetOpcode::G_FDIV:
750
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000751 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000752 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000753 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000754 case TargetOpcode::G_SHL:
755 case TargetOpcode::G_LSHR:
756 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000757 case TargetOpcode::G_SDIV:
758 case TargetOpcode::G_UDIV:
Tim Northover2fda4b02016-10-10 21:49:49 +0000759 case TargetOpcode::G_SUB:
760 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000761 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000762 if (unsupportedBinOp(I, RBI, MRI, TRI))
763 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000764
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000765 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000766
767 const unsigned DefReg = I.getOperand(0).getReg();
768 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
769
770 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
771 if (NewOpc == I.getOpcode())
772 return false;
773
774 I.setDesc(TII.get(NewOpc));
775 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000776
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 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000781
Tim Northover037af52c2016-10-31 18:31:09 +0000782 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000783 case TargetOpcode::G_TRUNC: {
784 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
785 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
786
787 const unsigned DstReg = I.getOperand(0).getReg();
788 const unsigned SrcReg = I.getOperand(1).getReg();
789
790 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
791 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
792
793 if (DstRB.getID() != SrcRB.getID()) {
794 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
795 return false;
796 }
797
798 if (DstRB.getID() == AArch64::GPRRegBankID) {
799 const TargetRegisterClass *DstRC =
800 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
801 if (!DstRC)
802 return false;
803
804 const TargetRegisterClass *SrcRC =
805 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
806 if (!SrcRC)
807 return false;
808
809 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
810 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
811 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
812 return false;
813 }
814
815 if (DstRC == SrcRC) {
816 // Nothing to be done
817 } else if (DstRC == &AArch64::GPR32RegClass &&
818 SrcRC == &AArch64::GPR64RegClass) {
819 I.getOperand(1).setSubReg(AArch64::sub_32);
820 } else {
821 return false;
822 }
823
824 I.setDesc(TII.get(TargetOpcode::COPY));
825 return true;
826 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
827 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
828 I.setDesc(TII.get(AArch64::XTNv4i16));
829 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
830 return true;
831 }
832 }
833
834 return false;
835 }
836
Tim Northover3d38b3a2016-10-11 20:50:21 +0000837 case TargetOpcode::G_ANYEXT: {
838 const unsigned DstReg = I.getOperand(0).getReg();
839 const unsigned SrcReg = I.getOperand(1).getReg();
840
Quentin Colombetcb629a82016-10-12 03:57:49 +0000841 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
842 if (RBDst.getID() != AArch64::GPRRegBankID) {
843 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
844 return false;
845 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000846
Quentin Colombetcb629a82016-10-12 03:57:49 +0000847 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
848 if (RBSrc.getID() != AArch64::GPRRegBankID) {
849 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000850 return false;
851 }
852
853 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
854
855 if (DstSize == 0) {
856 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
857 return false;
858 }
859
Quentin Colombetcb629a82016-10-12 03:57:49 +0000860 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000861 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
862 << ", expected: 32 or 64\n");
863 return false;
864 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000865 // At this point G_ANYEXT is just like a plain COPY, but we need
866 // to explicitly form the 64-bit value if any.
867 if (DstSize > 32) {
868 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
869 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
870 .addDef(ExtSrc)
871 .addImm(0)
872 .addUse(SrcReg)
873 .addImm(AArch64::sub_32);
874 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000875 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000876 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000877 }
878
879 case TargetOpcode::G_ZEXT:
880 case TargetOpcode::G_SEXT: {
881 unsigned Opcode = I.getOpcode();
882 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
883 SrcTy = MRI.getType(I.getOperand(1).getReg());
884 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
885 const unsigned DefReg = I.getOperand(0).getReg();
886 const unsigned SrcReg = I.getOperand(1).getReg();
887 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
888
889 if (RB.getID() != AArch64::GPRRegBankID) {
890 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
891 << ", expected: GPR\n");
892 return false;
893 }
894
895 MachineInstr *ExtI;
896 if (DstTy == LLT::scalar(64)) {
897 // FIXME: Can we avoid manually doing this?
898 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
899 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
900 << " operand\n");
901 return false;
902 }
903
904 const unsigned SrcXReg =
905 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
906 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
907 .addDef(SrcXReg)
908 .addImm(0)
909 .addUse(SrcReg)
910 .addImm(AArch64::sub_32);
911
912 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
913 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
914 .addDef(DefReg)
915 .addUse(SrcXReg)
916 .addImm(0)
917 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000918 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000919 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
920 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
921 .addDef(DefReg)
922 .addUse(SrcReg)
923 .addImm(0)
924 .addImm(SrcTy.getSizeInBits() - 1);
925 } else {
926 return false;
927 }
928
929 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
930
931 I.eraseFromParent();
932 return true;
933 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000934
Tim Northover69271c62016-10-12 22:49:11 +0000935 case TargetOpcode::G_SITOFP:
936 case TargetOpcode::G_UITOFP:
937 case TargetOpcode::G_FPTOSI:
938 case TargetOpcode::G_FPTOUI: {
939 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
940 SrcTy = MRI.getType(I.getOperand(1).getReg());
941 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
942 if (NewOpc == Opcode)
943 return false;
944
945 I.setDesc(TII.get(NewOpc));
946 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
947
948 return true;
949 }
950
951
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000952 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000953 case TargetOpcode::G_BITCAST:
954 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000955
Tim Northover5f7dea82016-11-08 17:44:07 +0000956 case TargetOpcode::G_FPEXT: {
957 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
958 DEBUG(dbgs() << "G_FPEXT to type " << Ty
959 << ", expected: " << LLT::scalar(64) << '\n');
960 return false;
961 }
962
963 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
964 DEBUG(dbgs() << "G_FPEXT from type " << Ty
965 << ", expected: " << LLT::scalar(32) << '\n');
966 return false;
967 }
968
969 const unsigned DefReg = I.getOperand(0).getReg();
970 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
971
972 if (RB.getID() != AArch64::FPRRegBankID) {
973 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
974 return false;
975 }
976
977 I.setDesc(TII.get(AArch64::FCVTDSr));
978 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
979
980 return true;
981 }
982
983 case TargetOpcode::G_FPTRUNC: {
984 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
985 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
986 << ", expected: " << LLT::scalar(32) << '\n');
987 return false;
988 }
989
990 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
991 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
992 << ", expected: " << LLT::scalar(64) << '\n');
993 return false;
994 }
995
996 const unsigned DefReg = I.getOperand(0).getReg();
997 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
998
999 if (RB.getID() != AArch64::FPRRegBankID) {
1000 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1001 return false;
1002 }
1003
1004 I.setDesc(TII.get(AArch64::FCVTSDr));
1005 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1006
1007 return true;
1008 }
1009
Tim Northover9ac0eba2016-11-08 00:45:29 +00001010 case TargetOpcode::G_SELECT: {
1011 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1012 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1013 << ", expected: " << LLT::scalar(1) << '\n');
1014 return false;
1015 }
1016
1017 const unsigned CondReg = I.getOperand(1).getReg();
1018 const unsigned TReg = I.getOperand(2).getReg();
1019 const unsigned FReg = I.getOperand(3).getReg();
1020
1021 unsigned CSelOpc = 0;
1022
1023 if (Ty == LLT::scalar(32)) {
1024 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001025 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001026 CSelOpc = AArch64::CSELXr;
1027 } else {
1028 return false;
1029 }
1030
1031 MachineInstr &TstMI =
1032 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1033 .addDef(AArch64::WZR)
1034 .addUse(CondReg)
1035 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1036
1037 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1038 .addDef(I.getOperand(0).getReg())
1039 .addUse(TReg)
1040 .addUse(FReg)
1041 .addImm(AArch64CC::NE);
1042
1043 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1044 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1045
1046 I.eraseFromParent();
1047 return true;
1048 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001049 case TargetOpcode::G_ICMP: {
1050 if (Ty != LLT::scalar(1)) {
1051 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1052 << ", expected: " << LLT::scalar(1) << '\n');
1053 return false;
1054 }
1055
1056 unsigned CmpOpc = 0;
1057 unsigned ZReg = 0;
1058
1059 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1060 if (CmpTy == LLT::scalar(32)) {
1061 CmpOpc = AArch64::SUBSWrr;
1062 ZReg = AArch64::WZR;
1063 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1064 CmpOpc = AArch64::SUBSXrr;
1065 ZReg = AArch64::XZR;
1066 } else {
1067 return false;
1068 }
1069
Kristof Beyls22524402017-01-05 10:16:08 +00001070 // CSINC increments the result by one when the condition code is false.
1071 // Therefore, we have to invert the predicate to get an increment by 1 when
1072 // the predicate is true.
1073 const AArch64CC::CondCode invCC =
1074 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1075 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001076
1077 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1078 .addDef(ZReg)
1079 .addUse(I.getOperand(2).getReg())
1080 .addUse(I.getOperand(3).getReg());
1081
1082 MachineInstr &CSetMI =
1083 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1084 .addDef(I.getOperand(0).getReg())
1085 .addUse(AArch64::WZR)
1086 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001087 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001088
1089 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1090 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1091
1092 I.eraseFromParent();
1093 return true;
1094 }
1095
Tim Northover7dd378d2016-10-12 22:49:07 +00001096 case TargetOpcode::G_FCMP: {
1097 if (Ty != LLT::scalar(1)) {
1098 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1099 << ", expected: " << LLT::scalar(1) << '\n');
1100 return false;
1101 }
1102
1103 unsigned CmpOpc = 0;
1104 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1105 if (CmpTy == LLT::scalar(32)) {
1106 CmpOpc = AArch64::FCMPSrr;
1107 } else if (CmpTy == LLT::scalar(64)) {
1108 CmpOpc = AArch64::FCMPDrr;
1109 } else {
1110 return false;
1111 }
1112
1113 // FIXME: regbank
1114
1115 AArch64CC::CondCode CC1, CC2;
1116 changeFCMPPredToAArch64CC(
1117 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1118
1119 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1120 .addUse(I.getOperand(2).getReg())
1121 .addUse(I.getOperand(3).getReg());
1122
1123 const unsigned DefReg = I.getOperand(0).getReg();
1124 unsigned Def1Reg = DefReg;
1125 if (CC2 != AArch64CC::AL)
1126 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1127
1128 MachineInstr &CSetMI =
1129 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1130 .addDef(Def1Reg)
1131 .addUse(AArch64::WZR)
1132 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001133 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001134
1135 if (CC2 != AArch64CC::AL) {
1136 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1137 MachineInstr &CSet2MI =
1138 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1139 .addDef(Def2Reg)
1140 .addUse(AArch64::WZR)
1141 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001142 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001143 MachineInstr &OrMI =
1144 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1145 .addDef(DefReg)
1146 .addUse(Def1Reg)
1147 .addUse(Def2Reg);
1148 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1149 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1150 }
1151
1152 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1153 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1154
1155 I.eraseFromParent();
1156 return true;
1157 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001158 }
1159
1160 return false;
1161}