blob: 5c03ea930af823a87516bc5476029cead2e05ed3 [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
Tim Northover4494d692016-10-18 19:47:57 +0000528 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000529 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000530 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
531
532 const LLT s32 = LLT::scalar(32);
533 const LLT s64 = LLT::scalar(64);
534 const LLT p0 = LLT::pointer(0, 64);
535
536 const unsigned DefReg = I.getOperand(0).getReg();
537 const LLT DefTy = MRI.getType(DefReg);
538 const unsigned DefSize = DefTy.getSizeInBits();
539 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
540
541 // FIXME: Redundant check, but even less readable when factored out.
542 if (isFP) {
543 if (Ty != s32 && Ty != s64) {
544 DEBUG(dbgs() << "Unable to materialize FP " << Ty
545 << " constant, expected: " << s32 << " or " << s64
546 << '\n');
547 return false;
548 }
549
550 if (RB.getID() != AArch64::FPRRegBankID) {
551 DEBUG(dbgs() << "Unable to materialize FP " << Ty
552 << " constant on bank: " << RB << ", expected: FPR\n");
553 return false;
554 }
555 } else {
556 if (Ty != s32 && Ty != s64 && Ty != p0) {
557 DEBUG(dbgs() << "Unable to materialize integer " << Ty
558 << " constant, expected: " << s32 << ", " << s64 << ", or "
559 << p0 << '\n');
560 return false;
561 }
562
563 if (RB.getID() != AArch64::GPRRegBankID) {
564 DEBUG(dbgs() << "Unable to materialize integer " << Ty
565 << " constant on bank: " << RB << ", expected: GPR\n");
566 return false;
567 }
568 }
569
570 const unsigned MovOpc =
571 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
572
573 I.setDesc(TII.get(MovOpc));
574
575 if (isFP) {
576 const TargetRegisterClass &GPRRC =
577 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
578 const TargetRegisterClass &FPRRC =
579 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
580
581 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
582 MachineOperand &RegOp = I.getOperand(0);
583 RegOp.setReg(DefGPRReg);
584
585 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
586 TII.get(AArch64::COPY))
587 .addDef(DefReg)
588 .addUse(DefGPRReg);
589
590 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
591 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
592 return false;
593 }
594
595 MachineOperand &ImmOp = I.getOperand(1);
596 // FIXME: Is going through int64_t always correct?
597 ImmOp.ChangeToImmediate(
598 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Tim Northover9267ac52016-12-05 21:47:07 +0000599 } else {
600 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
601 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000602 }
603
604 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
605 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000606 }
607
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000608 case TargetOpcode::G_FRAME_INDEX: {
609 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000610 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000611 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000612 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000613 return false;
614 }
615
616 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000617
618 // MOs for a #0 shifted immediate.
619 I.addOperand(MachineOperand::CreateImm(0));
620 I.addOperand(MachineOperand::CreateImm(0));
621
622 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
623 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000624
625 case TargetOpcode::G_GLOBAL_VALUE: {
626 auto GV = I.getOperand(1).getGlobal();
627 if (GV->isThreadLocal()) {
628 // FIXME: we don't support TLS yet.
629 return false;
630 }
631 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000632 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000633 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000634 I.getOperand(1).setTargetFlags(OpFlags);
635 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000636 I.setDesc(TII.get(AArch64::MOVaddr));
637 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
638 MachineInstrBuilder MIB(MF, I);
639 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
640 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
641 }
642 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
643 }
644
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000645 case TargetOpcode::G_LOAD:
646 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000647 LLT MemTy = Ty;
648 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000649
Tim Northover5ae83502016-09-15 09:20:34 +0000650 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000651 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000652 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000653 return false;
654 }
655
656#ifndef NDEBUG
657 // Sanity-check the pointer register.
658 const unsigned PtrReg = I.getOperand(1).getReg();
659 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
660 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
661 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000662 assert(MRI.getType(PtrReg).isPointer() &&
663 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000664#endif
665
666 const unsigned ValReg = I.getOperand(0).getReg();
667 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
668
669 const unsigned NewOpc =
670 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
671 if (NewOpc == I.getOpcode())
672 return false;
673
674 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000675
676 I.addOperand(MachineOperand::CreateImm(0));
677 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
678 }
679
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000680 case TargetOpcode::G_MUL: {
681 // Reject the various things we don't support yet.
682 if (unsupportedBinOp(I, RBI, MRI, TRI))
683 return false;
684
685 const unsigned DefReg = I.getOperand(0).getReg();
686 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
687
688 if (RB.getID() != AArch64::GPRRegBankID) {
689 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
690 return false;
691 }
692
693 unsigned ZeroReg;
694 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000695 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000696 NewOpc = AArch64::MADDWrrr;
697 ZeroReg = AArch64::WZR;
698 } else if (Ty == LLT::scalar(64)) {
699 NewOpc = AArch64::MADDXrrr;
700 ZeroReg = AArch64::XZR;
701 } else {
702 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
703 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
704 return false;
705 }
706
707 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000708
709 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
710
711 // Now that we selected an opcode, we need to constrain the register
712 // operands to use appropriate classes.
713 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
714 }
715
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000716 case TargetOpcode::G_FADD:
717 case TargetOpcode::G_FSUB:
718 case TargetOpcode::G_FMUL:
719 case TargetOpcode::G_FDIV:
720
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000721 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000722 case TargetOpcode::G_SHL:
723 case TargetOpcode::G_LSHR:
724 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000725 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000726 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000727 if (unsupportedBinOp(I, RBI, MRI, TRI))
728 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000729
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000730 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000731
732 const unsigned DefReg = I.getOperand(0).getReg();
733 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
734
735 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
736 if (NewOpc == I.getOpcode())
737 return false;
738
739 I.setDesc(TII.get(NewOpc));
740 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000741
742 // Now that we selected an opcode, we need to constrain the register
743 // operands to use appropriate classes.
744 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
745 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000746
Tim Northover037af52c2016-10-31 18:31:09 +0000747 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000748 case TargetOpcode::G_TRUNC: {
749 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
750 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
751
752 const unsigned DstReg = I.getOperand(0).getReg();
753 const unsigned SrcReg = I.getOperand(1).getReg();
754
755 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
756 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
757
758 if (DstRB.getID() != SrcRB.getID()) {
759 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
760 return false;
761 }
762
763 if (DstRB.getID() == AArch64::GPRRegBankID) {
764 const TargetRegisterClass *DstRC =
765 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
766 if (!DstRC)
767 return false;
768
769 const TargetRegisterClass *SrcRC =
770 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
771 if (!SrcRC)
772 return false;
773
774 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
775 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
776 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
777 return false;
778 }
779
780 if (DstRC == SrcRC) {
781 // Nothing to be done
782 } else if (DstRC == &AArch64::GPR32RegClass &&
783 SrcRC == &AArch64::GPR64RegClass) {
784 I.getOperand(1).setSubReg(AArch64::sub_32);
785 } else {
786 return false;
787 }
788
789 I.setDesc(TII.get(TargetOpcode::COPY));
790 return true;
791 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
792 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
793 I.setDesc(TII.get(AArch64::XTNv4i16));
794 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
795 return true;
796 }
797 }
798
799 return false;
800 }
801
Tim Northover3d38b3a2016-10-11 20:50:21 +0000802 case TargetOpcode::G_ANYEXT: {
803 const unsigned DstReg = I.getOperand(0).getReg();
804 const unsigned SrcReg = I.getOperand(1).getReg();
805
Quentin Colombetcb629a82016-10-12 03:57:49 +0000806 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
807 if (RBDst.getID() != AArch64::GPRRegBankID) {
808 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
809 return false;
810 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000811
Quentin Colombetcb629a82016-10-12 03:57:49 +0000812 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
813 if (RBSrc.getID() != AArch64::GPRRegBankID) {
814 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000815 return false;
816 }
817
818 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
819
820 if (DstSize == 0) {
821 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
822 return false;
823 }
824
Quentin Colombetcb629a82016-10-12 03:57:49 +0000825 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000826 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
827 << ", expected: 32 or 64\n");
828 return false;
829 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000830 // At this point G_ANYEXT is just like a plain COPY, but we need
831 // to explicitly form the 64-bit value if any.
832 if (DstSize > 32) {
833 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
834 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
835 .addDef(ExtSrc)
836 .addImm(0)
837 .addUse(SrcReg)
838 .addImm(AArch64::sub_32);
839 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000840 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000841 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000842 }
843
844 case TargetOpcode::G_ZEXT:
845 case TargetOpcode::G_SEXT: {
846 unsigned Opcode = I.getOpcode();
847 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
848 SrcTy = MRI.getType(I.getOperand(1).getReg());
849 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
850 const unsigned DefReg = I.getOperand(0).getReg();
851 const unsigned SrcReg = I.getOperand(1).getReg();
852 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
853
854 if (RB.getID() != AArch64::GPRRegBankID) {
855 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
856 << ", expected: GPR\n");
857 return false;
858 }
859
860 MachineInstr *ExtI;
861 if (DstTy == LLT::scalar(64)) {
862 // FIXME: Can we avoid manually doing this?
863 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
864 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
865 << " operand\n");
866 return false;
867 }
868
869 const unsigned SrcXReg =
870 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
871 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
872 .addDef(SrcXReg)
873 .addImm(0)
874 .addUse(SrcReg)
875 .addImm(AArch64::sub_32);
876
877 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
878 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
879 .addDef(DefReg)
880 .addUse(SrcXReg)
881 .addImm(0)
882 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000883 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000884 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
885 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
886 .addDef(DefReg)
887 .addUse(SrcReg)
888 .addImm(0)
889 .addImm(SrcTy.getSizeInBits() - 1);
890 } else {
891 return false;
892 }
893
894 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
895
896 I.eraseFromParent();
897 return true;
898 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000899
Tim Northover69271c62016-10-12 22:49:11 +0000900 case TargetOpcode::G_SITOFP:
901 case TargetOpcode::G_UITOFP:
902 case TargetOpcode::G_FPTOSI:
903 case TargetOpcode::G_FPTOUI: {
904 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
905 SrcTy = MRI.getType(I.getOperand(1).getReg());
906 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
907 if (NewOpc == Opcode)
908 return false;
909
910 I.setDesc(TII.get(NewOpc));
911 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
912
913 return true;
914 }
915
916
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000917 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000918 case TargetOpcode::G_BITCAST:
919 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000920
Tim Northover5f7dea82016-11-08 17:44:07 +0000921 case TargetOpcode::G_FPEXT: {
922 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
923 DEBUG(dbgs() << "G_FPEXT to type " << Ty
924 << ", expected: " << LLT::scalar(64) << '\n');
925 return false;
926 }
927
928 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
929 DEBUG(dbgs() << "G_FPEXT from type " << Ty
930 << ", expected: " << LLT::scalar(32) << '\n');
931 return false;
932 }
933
934 const unsigned DefReg = I.getOperand(0).getReg();
935 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
936
937 if (RB.getID() != AArch64::FPRRegBankID) {
938 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
939 return false;
940 }
941
942 I.setDesc(TII.get(AArch64::FCVTDSr));
943 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
944
945 return true;
946 }
947
948 case TargetOpcode::G_FPTRUNC: {
949 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
950 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
951 << ", expected: " << LLT::scalar(32) << '\n');
952 return false;
953 }
954
955 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
956 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
957 << ", expected: " << LLT::scalar(64) << '\n');
958 return false;
959 }
960
961 const unsigned DefReg = I.getOperand(0).getReg();
962 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
963
964 if (RB.getID() != AArch64::FPRRegBankID) {
965 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
966 return false;
967 }
968
969 I.setDesc(TII.get(AArch64::FCVTSDr));
970 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
971
972 return true;
973 }
974
Tim Northover9ac0eba2016-11-08 00:45:29 +0000975 case TargetOpcode::G_SELECT: {
976 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
977 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
978 << ", expected: " << LLT::scalar(1) << '\n');
979 return false;
980 }
981
982 const unsigned CondReg = I.getOperand(1).getReg();
983 const unsigned TReg = I.getOperand(2).getReg();
984 const unsigned FReg = I.getOperand(3).getReg();
985
986 unsigned CSelOpc = 0;
987
988 if (Ty == LLT::scalar(32)) {
989 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +0000990 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +0000991 CSelOpc = AArch64::CSELXr;
992 } else {
993 return false;
994 }
995
996 MachineInstr &TstMI =
997 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
998 .addDef(AArch64::WZR)
999 .addUse(CondReg)
1000 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1001
1002 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1003 .addDef(I.getOperand(0).getReg())
1004 .addUse(TReg)
1005 .addUse(FReg)
1006 .addImm(AArch64CC::NE);
1007
1008 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1009 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1010
1011 I.eraseFromParent();
1012 return true;
1013 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001014 case TargetOpcode::G_ICMP: {
1015 if (Ty != LLT::scalar(1)) {
1016 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1017 << ", expected: " << LLT::scalar(1) << '\n');
1018 return false;
1019 }
1020
1021 unsigned CmpOpc = 0;
1022 unsigned ZReg = 0;
1023
1024 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1025 if (CmpTy == LLT::scalar(32)) {
1026 CmpOpc = AArch64::SUBSWrr;
1027 ZReg = AArch64::WZR;
1028 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1029 CmpOpc = AArch64::SUBSXrr;
1030 ZReg = AArch64::XZR;
1031 } else {
1032 return false;
1033 }
1034
Kristof Beyls22524402017-01-05 10:16:08 +00001035 // CSINC increments the result by one when the condition code is false.
1036 // Therefore, we have to invert the predicate to get an increment by 1 when
1037 // the predicate is true.
1038 const AArch64CC::CondCode invCC =
1039 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1040 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001041
1042 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1043 .addDef(ZReg)
1044 .addUse(I.getOperand(2).getReg())
1045 .addUse(I.getOperand(3).getReg());
1046
1047 MachineInstr &CSetMI =
1048 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1049 .addDef(I.getOperand(0).getReg())
1050 .addUse(AArch64::WZR)
1051 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001052 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001053
1054 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1055 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1056
1057 I.eraseFromParent();
1058 return true;
1059 }
1060
Tim Northover7dd378d2016-10-12 22:49:07 +00001061 case TargetOpcode::G_FCMP: {
1062 if (Ty != LLT::scalar(1)) {
1063 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1064 << ", expected: " << LLT::scalar(1) << '\n');
1065 return false;
1066 }
1067
1068 unsigned CmpOpc = 0;
1069 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1070 if (CmpTy == LLT::scalar(32)) {
1071 CmpOpc = AArch64::FCMPSrr;
1072 } else if (CmpTy == LLT::scalar(64)) {
1073 CmpOpc = AArch64::FCMPDrr;
1074 } else {
1075 return false;
1076 }
1077
1078 // FIXME: regbank
1079
1080 AArch64CC::CondCode CC1, CC2;
1081 changeFCMPPredToAArch64CC(
1082 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1083
1084 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1085 .addUse(I.getOperand(2).getReg())
1086 .addUse(I.getOperand(3).getReg());
1087
1088 const unsigned DefReg = I.getOperand(0).getReg();
1089 unsigned Def1Reg = DefReg;
1090 if (CC2 != AArch64CC::AL)
1091 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1092
1093 MachineInstr &CSetMI =
1094 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1095 .addDef(Def1Reg)
1096 .addUse(AArch64::WZR)
1097 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001098 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001099
1100 if (CC2 != AArch64CC::AL) {
1101 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1102 MachineInstr &CSet2MI =
1103 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1104 .addDef(Def2Reg)
1105 .addUse(AArch64::WZR)
1106 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001107 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001108 MachineInstr &OrMI =
1109 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1110 .addDef(DefReg)
1111 .addUse(Def1Reg)
1112 .addUse(Def2Reg);
1113 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1114 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1115 }
1116
1117 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1118 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1119
1120 I.eraseFromParent();
1121 return true;
1122 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001123 }
1124
1125 return false;
1126}