blob: 21d80ca9f812f9a98be1c78e2c0e64096b59069a [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:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000129 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000130 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000131 case TargetOpcode::G_SHL:
132 return AArch64::LSLVWr;
133 case TargetOpcode::G_LSHR:
134 return AArch64::LSRVWr;
135 case TargetOpcode::G_ASHR:
136 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000137 default:
138 return GenericOpc;
139 }
Tim Northover55782222016-10-18 20:03:48 +0000140 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000141 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000142 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000143 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000144 case TargetOpcode::G_SHL:
145 return AArch64::LSLVXr;
146 case TargetOpcode::G_LSHR:
147 return AArch64::LSRVXr;
148 case TargetOpcode::G_ASHR:
149 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000150 default:
151 return GenericOpc;
152 }
153 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000154 case AArch64::FPRRegBankID:
155 switch (OpSize) {
156 case 32:
157 switch (GenericOpc) {
158 case TargetOpcode::G_FADD:
159 return AArch64::FADDSrr;
160 case TargetOpcode::G_FSUB:
161 return AArch64::FSUBSrr;
162 case TargetOpcode::G_FMUL:
163 return AArch64::FMULSrr;
164 case TargetOpcode::G_FDIV:
165 return AArch64::FDIVSrr;
166 default:
167 return GenericOpc;
168 }
169 case 64:
170 switch (GenericOpc) {
171 case TargetOpcode::G_FADD:
172 return AArch64::FADDDrr;
173 case TargetOpcode::G_FSUB:
174 return AArch64::FSUBDrr;
175 case TargetOpcode::G_FMUL:
176 return AArch64::FMULDrr;
177 case TargetOpcode::G_FDIV:
178 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000179 case TargetOpcode::G_OR:
180 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000181 default:
182 return GenericOpc;
183 }
184 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000185 };
186 return GenericOpc;
187}
188
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000189/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
190/// appropriate for the (value) register bank \p RegBankID and of memory access
191/// size \p OpSize. This returns the variant with the base+unsigned-immediate
192/// addressing mode (e.g., LDRXui).
193/// \returns \p GenericOpc if the combination is unsupported.
194static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
195 unsigned OpSize) {
196 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
197 switch (RegBankID) {
198 case AArch64::GPRRegBankID:
199 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000200 case 8:
201 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
202 case 16:
203 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000204 case 32:
205 return isStore ? AArch64::STRWui : AArch64::LDRWui;
206 case 64:
207 return isStore ? AArch64::STRXui : AArch64::LDRXui;
208 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000209 case AArch64::FPRRegBankID:
210 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000211 case 8:
212 return isStore ? AArch64::STRBui : AArch64::LDRBui;
213 case 16:
214 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000215 case 32:
216 return isStore ? AArch64::STRSui : AArch64::LDRSui;
217 case 64:
218 return isStore ? AArch64::STRDui : AArch64::LDRDui;
219 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000220 };
221 return GenericOpc;
222}
223
Quentin Colombetcb629a82016-10-12 03:57:49 +0000224static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
225 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
226 const RegisterBankInfo &RBI) {
227
228 unsigned DstReg = I.getOperand(0).getReg();
229 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
230 assert(I.isCopy() && "Generic operators do not allow physical registers");
231 return true;
232 }
233
234 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
235 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
236 unsigned SrcReg = I.getOperand(1).getReg();
237 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
238 (void)SrcSize;
239 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
240 "No phys reg on generic operators");
241 assert(
242 (DstSize == SrcSize ||
243 // Copies are a mean to setup initial types, the number of
244 // bits may not exactly match.
245 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
246 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
247 // Copies are a mean to copy bits around, as long as we are
248 // on the same register class, that's fine. Otherwise, that
249 // means we need some SUBREG_TO_REG or AND & co.
250 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
251 "Copy with different width?!");
252 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
253 "GPRs cannot get more than 64-bit width values");
254 const TargetRegisterClass *RC = nullptr;
255
256 if (RegBank.getID() == AArch64::FPRRegBankID) {
257 if (DstSize <= 32)
258 RC = &AArch64::FPR32RegClass;
259 else if (DstSize <= 64)
260 RC = &AArch64::FPR64RegClass;
261 else if (DstSize <= 128)
262 RC = &AArch64::FPR128RegClass;
263 else {
264 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
265 return false;
266 }
267 } else {
268 assert(RegBank.getID() == AArch64::GPRRegBankID &&
269 "Bitcast for the flags?");
270 RC =
271 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
272 }
273
274 // No need to constrain SrcReg. It will get constrained when
275 // we hit another of its use or its defs.
276 // Copies do not have constraints.
277 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
278 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
279 << " operand\n");
280 return false;
281 }
282 I.setDesc(TII.get(AArch64::COPY));
283 return true;
284}
285
Tim Northover69271c62016-10-12 22:49:11 +0000286static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
287 if (!DstTy.isScalar() || !SrcTy.isScalar())
288 return GenericOpc;
289
290 const unsigned DstSize = DstTy.getSizeInBits();
291 const unsigned SrcSize = SrcTy.getSizeInBits();
292
293 switch (DstSize) {
294 case 32:
295 switch (SrcSize) {
296 case 32:
297 switch (GenericOpc) {
298 case TargetOpcode::G_SITOFP:
299 return AArch64::SCVTFUWSri;
300 case TargetOpcode::G_UITOFP:
301 return AArch64::UCVTFUWSri;
302 case TargetOpcode::G_FPTOSI:
303 return AArch64::FCVTZSUWSr;
304 case TargetOpcode::G_FPTOUI:
305 return AArch64::FCVTZUUWSr;
306 default:
307 return GenericOpc;
308 }
309 case 64:
310 switch (GenericOpc) {
311 case TargetOpcode::G_SITOFP:
312 return AArch64::SCVTFUXSri;
313 case TargetOpcode::G_UITOFP:
314 return AArch64::UCVTFUXSri;
315 case TargetOpcode::G_FPTOSI:
316 return AArch64::FCVTZSUWDr;
317 case TargetOpcode::G_FPTOUI:
318 return AArch64::FCVTZUUWDr;
319 default:
320 return GenericOpc;
321 }
322 default:
323 return GenericOpc;
324 }
325 case 64:
326 switch (SrcSize) {
327 case 32:
328 switch (GenericOpc) {
329 case TargetOpcode::G_SITOFP:
330 return AArch64::SCVTFUWDri;
331 case TargetOpcode::G_UITOFP:
332 return AArch64::UCVTFUWDri;
333 case TargetOpcode::G_FPTOSI:
334 return AArch64::FCVTZSUXSr;
335 case TargetOpcode::G_FPTOUI:
336 return AArch64::FCVTZUUXSr;
337 default:
338 return GenericOpc;
339 }
340 case 64:
341 switch (GenericOpc) {
342 case TargetOpcode::G_SITOFP:
343 return AArch64::SCVTFUXDri;
344 case TargetOpcode::G_UITOFP:
345 return AArch64::UCVTFUXDri;
346 case TargetOpcode::G_FPTOSI:
347 return AArch64::FCVTZSUXDr;
348 case TargetOpcode::G_FPTOUI:
349 return AArch64::FCVTZUUXDr;
350 default:
351 return GenericOpc;
352 }
353 default:
354 return GenericOpc;
355 }
356 default:
357 return GenericOpc;
358 };
359 return GenericOpc;
360}
361
Tim Northover6c02ad52016-10-12 22:49:04 +0000362static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
363 switch (P) {
364 default:
365 llvm_unreachable("Unknown condition code!");
366 case CmpInst::ICMP_NE:
367 return AArch64CC::NE;
368 case CmpInst::ICMP_EQ:
369 return AArch64CC::EQ;
370 case CmpInst::ICMP_SGT:
371 return AArch64CC::GT;
372 case CmpInst::ICMP_SGE:
373 return AArch64CC::GE;
374 case CmpInst::ICMP_SLT:
375 return AArch64CC::LT;
376 case CmpInst::ICMP_SLE:
377 return AArch64CC::LE;
378 case CmpInst::ICMP_UGT:
379 return AArch64CC::HI;
380 case CmpInst::ICMP_UGE:
381 return AArch64CC::HS;
382 case CmpInst::ICMP_ULT:
383 return AArch64CC::LO;
384 case CmpInst::ICMP_ULE:
385 return AArch64CC::LS;
386 }
387}
388
Tim Northover7dd378d2016-10-12 22:49:07 +0000389static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
390 AArch64CC::CondCode &CondCode,
391 AArch64CC::CondCode &CondCode2) {
392 CondCode2 = AArch64CC::AL;
393 switch (P) {
394 default:
395 llvm_unreachable("Unknown FP condition!");
396 case CmpInst::FCMP_OEQ:
397 CondCode = AArch64CC::EQ;
398 break;
399 case CmpInst::FCMP_OGT:
400 CondCode = AArch64CC::GT;
401 break;
402 case CmpInst::FCMP_OGE:
403 CondCode = AArch64CC::GE;
404 break;
405 case CmpInst::FCMP_OLT:
406 CondCode = AArch64CC::MI;
407 break;
408 case CmpInst::FCMP_OLE:
409 CondCode = AArch64CC::LS;
410 break;
411 case CmpInst::FCMP_ONE:
412 CondCode = AArch64CC::MI;
413 CondCode2 = AArch64CC::GT;
414 break;
415 case CmpInst::FCMP_ORD:
416 CondCode = AArch64CC::VC;
417 break;
418 case CmpInst::FCMP_UNO:
419 CondCode = AArch64CC::VS;
420 break;
421 case CmpInst::FCMP_UEQ:
422 CondCode = AArch64CC::EQ;
423 CondCode2 = AArch64CC::VS;
424 break;
425 case CmpInst::FCMP_UGT:
426 CondCode = AArch64CC::HI;
427 break;
428 case CmpInst::FCMP_UGE:
429 CondCode = AArch64CC::PL;
430 break;
431 case CmpInst::FCMP_ULT:
432 CondCode = AArch64CC::LT;
433 break;
434 case CmpInst::FCMP_ULE:
435 CondCode = AArch64CC::LE;
436 break;
437 case CmpInst::FCMP_UNE:
438 CondCode = AArch64CC::NE;
439 break;
440 }
441}
442
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000443bool AArch64InstructionSelector::select(MachineInstr &I) const {
444 assert(I.getParent() && "Instruction should be in a basic block!");
445 assert(I.getParent()->getParent() && "Instruction should be in a function!");
446
447 MachineBasicBlock &MBB = *I.getParent();
448 MachineFunction &MF = *MBB.getParent();
449 MachineRegisterInfo &MRI = MF.getRegInfo();
450
Tim Northovercdf23f12016-10-31 18:30:59 +0000451 unsigned Opcode = I.getOpcode();
452 if (!isPreISelGenericOpcode(I.getOpcode())) {
453 // Certain non-generic instructions also need some special handling.
454
455 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
456 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000457
458 if (Opcode == TargetOpcode::PHI) {
459 const unsigned DefReg = I.getOperand(0).getReg();
460 const LLT DefTy = MRI.getType(DefReg);
461
462 const TargetRegisterClass *DefRC = nullptr;
463 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
464 DefRC = TRI.getRegClass(DefReg);
465 } else {
466 const RegClassOrRegBank &RegClassOrBank =
467 MRI.getRegClassOrRegBank(DefReg);
468
469 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
470 if (!DefRC) {
471 if (!DefTy.isValid()) {
472 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
473 return false;
474 }
475 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
476 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
477 if (!DefRC) {
478 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
479 return false;
480 }
481 }
482 }
483
484 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
485 }
486
487 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000488 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000489
490 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000491 }
492
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000493
494 if (I.getNumOperands() != I.getNumExplicitOperands()) {
495 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
496 return false;
497 }
498
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000499 if (selectImpl(I))
500 return true;
501
Tim Northover32a078a2016-09-15 10:09:59 +0000502 LLT Ty =
503 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000504
Tim Northover69271c62016-10-12 22:49:11 +0000505 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000506 case TargetOpcode::G_BRCOND: {
507 if (Ty.getSizeInBits() > 32) {
508 // We shouldn't need this on AArch64, but it would be implemented as an
509 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
510 // bit being tested is < 32.
511 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
512 << ", expected at most 32-bits");
513 return false;
514 }
515
516 const unsigned CondReg = I.getOperand(0).getReg();
517 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
518
519 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
520 .addUse(CondReg)
521 .addImm(/*bit offset=*/0)
522 .addMBB(DestMBB);
523
524 I.eraseFromParent();
525 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
526 }
527
Kristof Beyls65a12c02017-01-30 09:13:18 +0000528 case TargetOpcode::G_BRINDIRECT: {
529 I.setDesc(TII.get(AArch64::BR));
530 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
531 }
532
Tim Northover4494d692016-10-18 19:47:57 +0000533 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000534 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000535 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
536
537 const LLT s32 = LLT::scalar(32);
538 const LLT s64 = LLT::scalar(64);
539 const LLT p0 = LLT::pointer(0, 64);
540
541 const unsigned DefReg = I.getOperand(0).getReg();
542 const LLT DefTy = MRI.getType(DefReg);
543 const unsigned DefSize = DefTy.getSizeInBits();
544 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
545
546 // FIXME: Redundant check, but even less readable when factored out.
547 if (isFP) {
548 if (Ty != s32 && Ty != s64) {
549 DEBUG(dbgs() << "Unable to materialize FP " << Ty
550 << " constant, expected: " << s32 << " or " << s64
551 << '\n');
552 return false;
553 }
554
555 if (RB.getID() != AArch64::FPRRegBankID) {
556 DEBUG(dbgs() << "Unable to materialize FP " << Ty
557 << " constant on bank: " << RB << ", expected: FPR\n");
558 return false;
559 }
560 } else {
561 if (Ty != s32 && Ty != s64 && Ty != p0) {
562 DEBUG(dbgs() << "Unable to materialize integer " << Ty
563 << " constant, expected: " << s32 << ", " << s64 << ", or "
564 << p0 << '\n');
565 return false;
566 }
567
568 if (RB.getID() != AArch64::GPRRegBankID) {
569 DEBUG(dbgs() << "Unable to materialize integer " << Ty
570 << " constant on bank: " << RB << ", expected: GPR\n");
571 return false;
572 }
573 }
574
575 const unsigned MovOpc =
576 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
577
578 I.setDesc(TII.get(MovOpc));
579
580 if (isFP) {
581 const TargetRegisterClass &GPRRC =
582 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
583 const TargetRegisterClass &FPRRC =
584 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
585
586 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
587 MachineOperand &RegOp = I.getOperand(0);
588 RegOp.setReg(DefGPRReg);
589
590 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
591 TII.get(AArch64::COPY))
592 .addDef(DefReg)
593 .addUse(DefGPRReg);
594
595 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
596 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
597 return false;
598 }
599
600 MachineOperand &ImmOp = I.getOperand(1);
601 // FIXME: Is going through int64_t always correct?
602 ImmOp.ChangeToImmediate(
603 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Tim Northover9267ac52016-12-05 21:47:07 +0000604 } else {
605 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
606 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000607 }
608
609 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
610 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000611 }
612
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000613 case TargetOpcode::G_FRAME_INDEX: {
614 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000615 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000616 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000617 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000618 return false;
619 }
620
621 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000622
623 // MOs for a #0 shifted immediate.
624 I.addOperand(MachineOperand::CreateImm(0));
625 I.addOperand(MachineOperand::CreateImm(0));
626
627 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
628 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000629
630 case TargetOpcode::G_GLOBAL_VALUE: {
631 auto GV = I.getOperand(1).getGlobal();
632 if (GV->isThreadLocal()) {
633 // FIXME: we don't support TLS yet.
634 return false;
635 }
636 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000637 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000638 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000639 I.getOperand(1).setTargetFlags(OpFlags);
640 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000641 I.setDesc(TII.get(AArch64::MOVaddr));
642 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
643 MachineInstrBuilder MIB(MF, I);
644 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
645 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
646 }
647 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
648 }
649
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000650 case TargetOpcode::G_LOAD:
651 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000652 LLT MemTy = Ty;
653 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000654
Tim Northover5ae83502016-09-15 09:20:34 +0000655 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000656 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000657 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000658 return false;
659 }
660
661#ifndef NDEBUG
662 // Sanity-check the pointer register.
663 const unsigned PtrReg = I.getOperand(1).getReg();
664 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
665 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
666 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000667 assert(MRI.getType(PtrReg).isPointer() &&
668 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000669#endif
670
671 const unsigned ValReg = I.getOperand(0).getReg();
672 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
673
674 const unsigned NewOpc =
675 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
676 if (NewOpc == I.getOpcode())
677 return false;
678
679 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000680
681 I.addOperand(MachineOperand::CreateImm(0));
682 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
683 }
684
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000685 case TargetOpcode::G_MUL: {
686 // Reject the various things we don't support yet.
687 if (unsupportedBinOp(I, RBI, MRI, TRI))
688 return false;
689
690 const unsigned DefReg = I.getOperand(0).getReg();
691 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
692
693 if (RB.getID() != AArch64::GPRRegBankID) {
694 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
695 return false;
696 }
697
698 unsigned ZeroReg;
699 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000700 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000701 NewOpc = AArch64::MADDWrrr;
702 ZeroReg = AArch64::WZR;
703 } else if (Ty == LLT::scalar(64)) {
704 NewOpc = AArch64::MADDXrrr;
705 ZeroReg = AArch64::XZR;
706 } else {
707 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
708 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
709 return false;
710 }
711
712 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000713
714 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
715
716 // Now that we selected an opcode, we need to constrain the register
717 // operands to use appropriate classes.
718 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
719 }
720
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000721 case TargetOpcode::G_FADD:
722 case TargetOpcode::G_FSUB:
723 case TargetOpcode::G_FMUL:
724 case TargetOpcode::G_FDIV:
725
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000726 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000727 case TargetOpcode::G_SHL:
728 case TargetOpcode::G_LSHR:
729 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000730 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000731 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000732 if (unsupportedBinOp(I, RBI, MRI, TRI))
733 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000734
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000735 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000736
737 const unsigned DefReg = I.getOperand(0).getReg();
738 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
739
740 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
741 if (NewOpc == I.getOpcode())
742 return false;
743
744 I.setDesc(TII.get(NewOpc));
745 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000746
747 // Now that we selected an opcode, we need to constrain the register
748 // operands to use appropriate classes.
749 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
750 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000751
Tim Northover037af52c2016-10-31 18:31:09 +0000752 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000753 case TargetOpcode::G_TRUNC: {
754 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
755 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
756
757 const unsigned DstReg = I.getOperand(0).getReg();
758 const unsigned SrcReg = I.getOperand(1).getReg();
759
760 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
761 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
762
763 if (DstRB.getID() != SrcRB.getID()) {
764 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
765 return false;
766 }
767
768 if (DstRB.getID() == AArch64::GPRRegBankID) {
769 const TargetRegisterClass *DstRC =
770 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
771 if (!DstRC)
772 return false;
773
774 const TargetRegisterClass *SrcRC =
775 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
776 if (!SrcRC)
777 return false;
778
779 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
780 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
781 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
782 return false;
783 }
784
785 if (DstRC == SrcRC) {
786 // Nothing to be done
787 } else if (DstRC == &AArch64::GPR32RegClass &&
788 SrcRC == &AArch64::GPR64RegClass) {
789 I.getOperand(1).setSubReg(AArch64::sub_32);
790 } else {
791 return false;
792 }
793
794 I.setDesc(TII.get(TargetOpcode::COPY));
795 return true;
796 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
797 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
798 I.setDesc(TII.get(AArch64::XTNv4i16));
799 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
800 return true;
801 }
802 }
803
804 return false;
805 }
806
Tim Northover3d38b3a2016-10-11 20:50:21 +0000807 case TargetOpcode::G_ANYEXT: {
808 const unsigned DstReg = I.getOperand(0).getReg();
809 const unsigned SrcReg = I.getOperand(1).getReg();
810
Quentin Colombetcb629a82016-10-12 03:57:49 +0000811 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
812 if (RBDst.getID() != AArch64::GPRRegBankID) {
813 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
814 return false;
815 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000816
Quentin Colombetcb629a82016-10-12 03:57:49 +0000817 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
818 if (RBSrc.getID() != AArch64::GPRRegBankID) {
819 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000820 return false;
821 }
822
823 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
824
825 if (DstSize == 0) {
826 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
827 return false;
828 }
829
Quentin Colombetcb629a82016-10-12 03:57:49 +0000830 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000831 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
832 << ", expected: 32 or 64\n");
833 return false;
834 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000835 // At this point G_ANYEXT is just like a plain COPY, but we need
836 // to explicitly form the 64-bit value if any.
837 if (DstSize > 32) {
838 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
839 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
840 .addDef(ExtSrc)
841 .addImm(0)
842 .addUse(SrcReg)
843 .addImm(AArch64::sub_32);
844 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000845 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000846 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000847 }
848
849 case TargetOpcode::G_ZEXT:
850 case TargetOpcode::G_SEXT: {
851 unsigned Opcode = I.getOpcode();
852 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
853 SrcTy = MRI.getType(I.getOperand(1).getReg());
854 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
855 const unsigned DefReg = I.getOperand(0).getReg();
856 const unsigned SrcReg = I.getOperand(1).getReg();
857 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
858
859 if (RB.getID() != AArch64::GPRRegBankID) {
860 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
861 << ", expected: GPR\n");
862 return false;
863 }
864
865 MachineInstr *ExtI;
866 if (DstTy == LLT::scalar(64)) {
867 // FIXME: Can we avoid manually doing this?
868 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
869 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
870 << " operand\n");
871 return false;
872 }
873
874 const unsigned SrcXReg =
875 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
876 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
877 .addDef(SrcXReg)
878 .addImm(0)
879 .addUse(SrcReg)
880 .addImm(AArch64::sub_32);
881
882 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
883 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
884 .addDef(DefReg)
885 .addUse(SrcXReg)
886 .addImm(0)
887 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000888 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000889 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
890 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
891 .addDef(DefReg)
892 .addUse(SrcReg)
893 .addImm(0)
894 .addImm(SrcTy.getSizeInBits() - 1);
895 } else {
896 return false;
897 }
898
899 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
900
901 I.eraseFromParent();
902 return true;
903 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000904
Tim Northover69271c62016-10-12 22:49:11 +0000905 case TargetOpcode::G_SITOFP:
906 case TargetOpcode::G_UITOFP:
907 case TargetOpcode::G_FPTOSI:
908 case TargetOpcode::G_FPTOUI: {
909 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
910 SrcTy = MRI.getType(I.getOperand(1).getReg());
911 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
912 if (NewOpc == Opcode)
913 return false;
914
915 I.setDesc(TII.get(NewOpc));
916 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
917
918 return true;
919 }
920
921
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000922 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000923 case TargetOpcode::G_BITCAST:
924 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000925
Tim Northover5f7dea82016-11-08 17:44:07 +0000926 case TargetOpcode::G_FPEXT: {
927 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
928 DEBUG(dbgs() << "G_FPEXT to type " << Ty
929 << ", expected: " << LLT::scalar(64) << '\n');
930 return false;
931 }
932
933 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
934 DEBUG(dbgs() << "G_FPEXT from type " << Ty
935 << ", expected: " << LLT::scalar(32) << '\n');
936 return false;
937 }
938
939 const unsigned DefReg = I.getOperand(0).getReg();
940 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
941
942 if (RB.getID() != AArch64::FPRRegBankID) {
943 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
944 return false;
945 }
946
947 I.setDesc(TII.get(AArch64::FCVTDSr));
948 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
949
950 return true;
951 }
952
953 case TargetOpcode::G_FPTRUNC: {
954 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
955 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
956 << ", expected: " << LLT::scalar(32) << '\n');
957 return false;
958 }
959
960 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
961 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
962 << ", expected: " << LLT::scalar(64) << '\n');
963 return false;
964 }
965
966 const unsigned DefReg = I.getOperand(0).getReg();
967 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
968
969 if (RB.getID() != AArch64::FPRRegBankID) {
970 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
971 return false;
972 }
973
974 I.setDesc(TII.get(AArch64::FCVTSDr));
975 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
976
977 return true;
978 }
979
Tim Northover9ac0eba2016-11-08 00:45:29 +0000980 case TargetOpcode::G_SELECT: {
981 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
982 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
983 << ", expected: " << LLT::scalar(1) << '\n');
984 return false;
985 }
986
987 const unsigned CondReg = I.getOperand(1).getReg();
988 const unsigned TReg = I.getOperand(2).getReg();
989 const unsigned FReg = I.getOperand(3).getReg();
990
991 unsigned CSelOpc = 0;
992
993 if (Ty == LLT::scalar(32)) {
994 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +0000995 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +0000996 CSelOpc = AArch64::CSELXr;
997 } else {
998 return false;
999 }
1000
1001 MachineInstr &TstMI =
1002 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1003 .addDef(AArch64::WZR)
1004 .addUse(CondReg)
1005 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1006
1007 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1008 .addDef(I.getOperand(0).getReg())
1009 .addUse(TReg)
1010 .addUse(FReg)
1011 .addImm(AArch64CC::NE);
1012
1013 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1014 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1015
1016 I.eraseFromParent();
1017 return true;
1018 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001019 case TargetOpcode::G_ICMP: {
1020 if (Ty != LLT::scalar(1)) {
1021 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1022 << ", expected: " << LLT::scalar(1) << '\n');
1023 return false;
1024 }
1025
1026 unsigned CmpOpc = 0;
1027 unsigned ZReg = 0;
1028
1029 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1030 if (CmpTy == LLT::scalar(32)) {
1031 CmpOpc = AArch64::SUBSWrr;
1032 ZReg = AArch64::WZR;
1033 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1034 CmpOpc = AArch64::SUBSXrr;
1035 ZReg = AArch64::XZR;
1036 } else {
1037 return false;
1038 }
1039
Kristof Beyls22524402017-01-05 10:16:08 +00001040 // CSINC increments the result by one when the condition code is false.
1041 // Therefore, we have to invert the predicate to get an increment by 1 when
1042 // the predicate is true.
1043 const AArch64CC::CondCode invCC =
1044 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1045 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001046
1047 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1048 .addDef(ZReg)
1049 .addUse(I.getOperand(2).getReg())
1050 .addUse(I.getOperand(3).getReg());
1051
1052 MachineInstr &CSetMI =
1053 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1054 .addDef(I.getOperand(0).getReg())
1055 .addUse(AArch64::WZR)
1056 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001057 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001058
1059 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1060 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1061
1062 I.eraseFromParent();
1063 return true;
1064 }
1065
Tim Northover7dd378d2016-10-12 22:49:07 +00001066 case TargetOpcode::G_FCMP: {
1067 if (Ty != LLT::scalar(1)) {
1068 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1069 << ", expected: " << LLT::scalar(1) << '\n');
1070 return false;
1071 }
1072
1073 unsigned CmpOpc = 0;
1074 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1075 if (CmpTy == LLT::scalar(32)) {
1076 CmpOpc = AArch64::FCMPSrr;
1077 } else if (CmpTy == LLT::scalar(64)) {
1078 CmpOpc = AArch64::FCMPDrr;
1079 } else {
1080 return false;
1081 }
1082
1083 // FIXME: regbank
1084
1085 AArch64CC::CondCode CC1, CC2;
1086 changeFCMPPredToAArch64CC(
1087 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1088
1089 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1090 .addUse(I.getOperand(2).getReg())
1091 .addUse(I.getOperand(3).getReg());
1092
1093 const unsigned DefReg = I.getOperand(0).getReg();
1094 unsigned Def1Reg = DefReg;
1095 if (CC2 != AArch64CC::AL)
1096 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1097
1098 MachineInstr &CSetMI =
1099 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1100 .addDef(Def1Reg)
1101 .addUse(AArch64::WZR)
1102 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001103 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001104
1105 if (CC2 != AArch64CC::AL) {
1106 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1107 MachineInstr &CSet2MI =
1108 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1109 .addDef(Def2Reg)
1110 .addUse(AArch64::WZR)
1111 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001112 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001113 MachineInstr &OrMI =
1114 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1115 .addDef(DefReg)
1116 .addUse(Def1Reg)
1117 .addUse(Def2Reg);
1118 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1119 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1120 }
1121
1122 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1123 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1124
1125 I.eraseFromParent();
1126 return true;
1127 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001128 }
1129
1130 return false;
1131}