blob: 7f2ce779a0cb34d7969d261e82472c8f352b5696 [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"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/IR/Type.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
29
30#define DEBUG_TYPE "aarch64-isel"
31
32using namespace llvm;
33
34#ifndef LLVM_BUILD_GLOBAL_ISEL
35#error "You shouldn't build this"
36#endif
37
38AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000039 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
40 const AArch64RegisterBankInfo &RBI)
41 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000042 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
43
Tim Northoverfb8d9892016-10-12 22:49:15 +000044// FIXME: This should be target-independent, inferred from the types declared
45// for each class in the bank.
46static const TargetRegisterClass *
47getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
48 const RegisterBankInfo &RBI) {
49 if (RB.getID() == AArch64::GPRRegBankID) {
50 if (Ty.getSizeInBits() <= 32)
51 return &AArch64::GPR32RegClass;
52 if (Ty.getSizeInBits() == 64)
53 return &AArch64::GPR64RegClass;
54 return nullptr;
55 }
56
57 if (RB.getID() == AArch64::FPRRegBankID) {
58 if (Ty.getSizeInBits() == 32)
59 return &AArch64::FPR32RegClass;
60 if (Ty.getSizeInBits() == 64)
61 return &AArch64::FPR64RegClass;
62 if (Ty.getSizeInBits() == 128)
63 return &AArch64::FPR128RegClass;
64 return nullptr;
65 }
66
67 return nullptr;
68}
69
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000070/// Check whether \p I is a currently unsupported binary operation:
71/// - it has an unsized type
72/// - an operand is not a vreg
73/// - all operands are not in the same bank
74/// These are checks that should someday live in the verifier, but right now,
75/// these are mostly limitations of the aarch64 selector.
76static bool unsupportedBinOp(const MachineInstr &I,
77 const AArch64RegisterBankInfo &RBI,
78 const MachineRegisterInfo &MRI,
79 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000080 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000081 if (!Ty.isValid()) {
82 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000083 return true;
84 }
85
86 const RegisterBank *PrevOpBank = nullptr;
87 for (auto &MO : I.operands()) {
88 // FIXME: Support non-register operands.
89 if (!MO.isReg()) {
90 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
91 return true;
92 }
93
94 // FIXME: Can generic operations have physical registers operands? If
95 // so, this will need to be taught about that, and we'll need to get the
96 // bank out of the minimal class for the register.
97 // Either way, this needs to be documented (and possibly verified).
98 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
99 DEBUG(dbgs() << "Generic inst has physical register operand\n");
100 return true;
101 }
102
103 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
104 if (!OpBank) {
105 DEBUG(dbgs() << "Generic register has no bank or class\n");
106 return true;
107 }
108
109 if (PrevOpBank && OpBank != PrevOpBank) {
110 DEBUG(dbgs() << "Generic inst operands have different banks\n");
111 return true;
112 }
113 PrevOpBank = OpBank;
114 }
115 return false;
116}
117
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000118/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
119/// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID
120/// and of size \p OpSize.
121/// \returns \p GenericOpc if the combination is unsupported.
122static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
123 unsigned OpSize) {
124 switch (RegBankID) {
125 case AArch64::GPRRegBankID:
Tim Northover55782222016-10-18 20:03:48 +0000126 if (OpSize <= 32) {
127 assert((OpSize == 32 || (GenericOpc != TargetOpcode::G_SDIV &&
128 GenericOpc != TargetOpcode::G_UDIV &&
129 GenericOpc != TargetOpcode::G_LSHR &&
130 GenericOpc != TargetOpcode::G_ASHR)) &&
131 "operation should have been legalized before now");
132
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000133 switch (GenericOpc) {
134 case TargetOpcode::G_OR:
135 return AArch64::ORRWrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000136 case TargetOpcode::G_XOR:
137 return AArch64::EORWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000138 case TargetOpcode::G_AND:
139 return AArch64::ANDWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000140 case TargetOpcode::G_ADD:
141 return AArch64::ADDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000142 case TargetOpcode::G_SUB:
143 return AArch64::SUBWrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000144 case TargetOpcode::G_SHL:
145 return AArch64::LSLVWr;
146 case TargetOpcode::G_LSHR:
147 return AArch64::LSRVWr;
148 case TargetOpcode::G_ASHR:
149 return AArch64::ASRVWr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000150 case TargetOpcode::G_SDIV:
151 return AArch64::SDIVWr;
152 case TargetOpcode::G_UDIV:
153 return AArch64::UDIVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000154 default:
155 return GenericOpc;
156 }
Tim Northover55782222016-10-18 20:03:48 +0000157 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000158 switch (GenericOpc) {
159 case TargetOpcode::G_OR:
160 return AArch64::ORRXrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000161 case TargetOpcode::G_XOR:
162 return AArch64::EORXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000163 case TargetOpcode::G_AND:
164 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000165 case TargetOpcode::G_ADD:
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
Tim Northover32a078a2016-09-15 10:09:59 +0000529 LLT Ty =
530 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000531
Tim Northover69271c62016-10-12 22:49:11 +0000532 switch (Opcode) {
Ahmed Bougacha85505092016-07-28 17:15:15 +0000533 case TargetOpcode::G_BR: {
534 I.setDesc(TII.get(AArch64::B));
Ahmed Bougacha85505092016-07-28 17:15:15 +0000535 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000536 }
537
Tim Northover5e3dbf32016-10-12 22:49:01 +0000538 case TargetOpcode::G_BRCOND: {
539 if (Ty.getSizeInBits() > 32) {
540 // We shouldn't need this on AArch64, but it would be implemented as an
541 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
542 // bit being tested is < 32.
543 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
544 << ", expected at most 32-bits");
545 return false;
546 }
547
548 const unsigned CondReg = I.getOperand(0).getReg();
549 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
550
551 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
552 .addUse(CondReg)
553 .addImm(/*bit offset=*/0)
554 .addMBB(DestMBB);
555
556 I.eraseFromParent();
557 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
558 }
559
Tim Northover4494d692016-10-18 19:47:57 +0000560 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000561 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000562 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
563
564 const LLT s32 = LLT::scalar(32);
565 const LLT s64 = LLT::scalar(64);
566 const LLT p0 = LLT::pointer(0, 64);
567
568 const unsigned DefReg = I.getOperand(0).getReg();
569 const LLT DefTy = MRI.getType(DefReg);
570 const unsigned DefSize = DefTy.getSizeInBits();
571 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
572
573 // FIXME: Redundant check, but even less readable when factored out.
574 if (isFP) {
575 if (Ty != s32 && Ty != s64) {
576 DEBUG(dbgs() << "Unable to materialize FP " << Ty
577 << " constant, expected: " << s32 << " or " << s64
578 << '\n');
579 return false;
580 }
581
582 if (RB.getID() != AArch64::FPRRegBankID) {
583 DEBUG(dbgs() << "Unable to materialize FP " << Ty
584 << " constant on bank: " << RB << ", expected: FPR\n");
585 return false;
586 }
587 } else {
588 if (Ty != s32 && Ty != s64 && Ty != p0) {
589 DEBUG(dbgs() << "Unable to materialize integer " << Ty
590 << " constant, expected: " << s32 << ", " << s64 << ", or "
591 << p0 << '\n');
592 return false;
593 }
594
595 if (RB.getID() != AArch64::GPRRegBankID) {
596 DEBUG(dbgs() << "Unable to materialize integer " << Ty
597 << " constant on bank: " << RB << ", expected: GPR\n");
598 return false;
599 }
600 }
601
602 const unsigned MovOpc =
603 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
604
605 I.setDesc(TII.get(MovOpc));
606
607 if (isFP) {
608 const TargetRegisterClass &GPRRC =
609 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
610 const TargetRegisterClass &FPRRC =
611 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
612
613 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
614 MachineOperand &RegOp = I.getOperand(0);
615 RegOp.setReg(DefGPRReg);
616
617 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
618 TII.get(AArch64::COPY))
619 .addDef(DefReg)
620 .addUse(DefGPRReg);
621
622 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
623 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
624 return false;
625 }
626
627 MachineOperand &ImmOp = I.getOperand(1);
628 // FIXME: Is going through int64_t always correct?
629 ImmOp.ChangeToImmediate(
630 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
631 }
632
633 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
634 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000635 }
636
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000637 case TargetOpcode::G_FRAME_INDEX: {
638 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000639 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000640 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000641 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000642 return false;
643 }
644
645 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000646
647 // MOs for a #0 shifted immediate.
648 I.addOperand(MachineOperand::CreateImm(0));
649 I.addOperand(MachineOperand::CreateImm(0));
650
651 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
652 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000653
654 case TargetOpcode::G_GLOBAL_VALUE: {
655 auto GV = I.getOperand(1).getGlobal();
656 if (GV->isThreadLocal()) {
657 // FIXME: we don't support TLS yet.
658 return false;
659 }
660 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
661 if (OpFlags & AArch64II::MO_GOT)
662 I.setDesc(TII.get(AArch64::LOADgot));
663 else {
664 I.setDesc(TII.get(AArch64::MOVaddr));
665 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
666 MachineInstrBuilder MIB(MF, I);
667 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
668 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
669 }
670 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
671 }
672
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000673 case TargetOpcode::G_LOAD:
674 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000675 LLT MemTy = Ty;
676 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000677
Tim Northover5ae83502016-09-15 09:20:34 +0000678 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000679 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000680 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000681 return false;
682 }
683
684#ifndef NDEBUG
685 // Sanity-check the pointer register.
686 const unsigned PtrReg = I.getOperand(1).getReg();
687 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
688 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
689 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000690 assert(MRI.getType(PtrReg).isPointer() &&
691 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000692#endif
693
694 const unsigned ValReg = I.getOperand(0).getReg();
695 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
696
697 const unsigned NewOpc =
698 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
699 if (NewOpc == I.getOpcode())
700 return false;
701
702 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000703
704 I.addOperand(MachineOperand::CreateImm(0));
705 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
706 }
707
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000708 case TargetOpcode::G_MUL: {
709 // Reject the various things we don't support yet.
710 if (unsupportedBinOp(I, RBI, MRI, TRI))
711 return false;
712
713 const unsigned DefReg = I.getOperand(0).getReg();
714 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
715
716 if (RB.getID() != AArch64::GPRRegBankID) {
717 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
718 return false;
719 }
720
721 unsigned ZeroReg;
722 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000723 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000724 NewOpc = AArch64::MADDWrrr;
725 ZeroReg = AArch64::WZR;
726 } else if (Ty == LLT::scalar(64)) {
727 NewOpc = AArch64::MADDXrrr;
728 ZeroReg = AArch64::XZR;
729 } else {
730 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
731 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
732 return false;
733 }
734
735 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000736
737 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
738
739 // Now that we selected an opcode, we need to constrain the register
740 // operands to use appropriate classes.
741 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
742 }
743
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000744 case TargetOpcode::G_FADD:
745 case TargetOpcode::G_FSUB:
746 case TargetOpcode::G_FMUL:
747 case TargetOpcode::G_FDIV:
748
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000749 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000750 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000751 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000752 case TargetOpcode::G_SHL:
753 case TargetOpcode::G_LSHR:
754 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000755 case TargetOpcode::G_SDIV:
756 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000757 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000758 case TargetOpcode::G_SUB:
759 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000760 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000761 if (unsupportedBinOp(I, RBI, MRI, TRI))
762 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000763
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000764 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000765
766 const unsigned DefReg = I.getOperand(0).getReg();
767 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
768
769 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
770 if (NewOpc == I.getOpcode())
771 return false;
772
773 I.setDesc(TII.get(NewOpc));
774 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000775
776 // Now that we selected an opcode, we need to constrain the register
777 // operands to use appropriate classes.
778 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
779 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000780
Tim Northover037af52c2016-10-31 18:31:09 +0000781 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000782 case TargetOpcode::G_TRUNC: {
783 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
784 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
785
786 const unsigned DstReg = I.getOperand(0).getReg();
787 const unsigned SrcReg = I.getOperand(1).getReg();
788
789 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
790 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
791
792 if (DstRB.getID() != SrcRB.getID()) {
793 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
794 return false;
795 }
796
797 if (DstRB.getID() == AArch64::GPRRegBankID) {
798 const TargetRegisterClass *DstRC =
799 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
800 if (!DstRC)
801 return false;
802
803 const TargetRegisterClass *SrcRC =
804 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
805 if (!SrcRC)
806 return false;
807
808 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
809 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
810 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
811 return false;
812 }
813
814 if (DstRC == SrcRC) {
815 // Nothing to be done
816 } else if (DstRC == &AArch64::GPR32RegClass &&
817 SrcRC == &AArch64::GPR64RegClass) {
818 I.getOperand(1).setSubReg(AArch64::sub_32);
819 } else {
820 return false;
821 }
822
823 I.setDesc(TII.get(TargetOpcode::COPY));
824 return true;
825 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
826 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
827 I.setDesc(TII.get(AArch64::XTNv4i16));
828 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
829 return true;
830 }
831 }
832
833 return false;
834 }
835
Tim Northover3d38b3a2016-10-11 20:50:21 +0000836 case TargetOpcode::G_ANYEXT: {
837 const unsigned DstReg = I.getOperand(0).getReg();
838 const unsigned SrcReg = I.getOperand(1).getReg();
839
Quentin Colombetcb629a82016-10-12 03:57:49 +0000840 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
841 if (RBDst.getID() != AArch64::GPRRegBankID) {
842 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
843 return false;
844 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000845
Quentin Colombetcb629a82016-10-12 03:57:49 +0000846 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
847 if (RBSrc.getID() != AArch64::GPRRegBankID) {
848 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000849 return false;
850 }
851
852 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
853
854 if (DstSize == 0) {
855 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
856 return false;
857 }
858
Quentin Colombetcb629a82016-10-12 03:57:49 +0000859 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000860 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
861 << ", expected: 32 or 64\n");
862 return false;
863 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000864 // At this point G_ANYEXT is just like a plain COPY, but we need
865 // to explicitly form the 64-bit value if any.
866 if (DstSize > 32) {
867 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
868 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
869 .addDef(ExtSrc)
870 .addImm(0)
871 .addUse(SrcReg)
872 .addImm(AArch64::sub_32);
873 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000874 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000875 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000876 }
877
878 case TargetOpcode::G_ZEXT:
879 case TargetOpcode::G_SEXT: {
880 unsigned Opcode = I.getOpcode();
881 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
882 SrcTy = MRI.getType(I.getOperand(1).getReg());
883 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
884 const unsigned DefReg = I.getOperand(0).getReg();
885 const unsigned SrcReg = I.getOperand(1).getReg();
886 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
887
888 if (RB.getID() != AArch64::GPRRegBankID) {
889 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
890 << ", expected: GPR\n");
891 return false;
892 }
893
894 MachineInstr *ExtI;
895 if (DstTy == LLT::scalar(64)) {
896 // FIXME: Can we avoid manually doing this?
897 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
898 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
899 << " operand\n");
900 return false;
901 }
902
903 const unsigned SrcXReg =
904 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
905 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
906 .addDef(SrcXReg)
907 .addImm(0)
908 .addUse(SrcReg)
909 .addImm(AArch64::sub_32);
910
911 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
912 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
913 .addDef(DefReg)
914 .addUse(SrcXReg)
915 .addImm(0)
916 .addImm(SrcTy.getSizeInBits() - 1);
917 } else if (DstTy == LLT::scalar(32)) {
918 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
919 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
920 .addDef(DefReg)
921 .addUse(SrcReg)
922 .addImm(0)
923 .addImm(SrcTy.getSizeInBits() - 1);
924 } else {
925 return false;
926 }
927
928 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
929
930 I.eraseFromParent();
931 return true;
932 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000933
Tim Northover69271c62016-10-12 22:49:11 +0000934 case TargetOpcode::G_SITOFP:
935 case TargetOpcode::G_UITOFP:
936 case TargetOpcode::G_FPTOSI:
937 case TargetOpcode::G_FPTOUI: {
938 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
939 SrcTy = MRI.getType(I.getOperand(1).getReg());
940 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
941 if (NewOpc == Opcode)
942 return false;
943
944 I.setDesc(TII.get(NewOpc));
945 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
946
947 return true;
948 }
949
950
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000951 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000952 case TargetOpcode::G_BITCAST:
953 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000954
955 case TargetOpcode::G_ICMP: {
956 if (Ty != LLT::scalar(1)) {
957 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
958 << ", expected: " << LLT::scalar(1) << '\n');
959 return false;
960 }
961
962 unsigned CmpOpc = 0;
963 unsigned ZReg = 0;
964
965 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
966 if (CmpTy == LLT::scalar(32)) {
967 CmpOpc = AArch64::SUBSWrr;
968 ZReg = AArch64::WZR;
969 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
970 CmpOpc = AArch64::SUBSXrr;
971 ZReg = AArch64::XZR;
972 } else {
973 return false;
974 }
975
976 const AArch64CC::CondCode CC = changeICMPPredToAArch64CC(
977 (CmpInst::Predicate)I.getOperand(1).getPredicate());
978
979 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
980 .addDef(ZReg)
981 .addUse(I.getOperand(2).getReg())
982 .addUse(I.getOperand(3).getReg());
983
984 MachineInstr &CSetMI =
985 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
986 .addDef(I.getOperand(0).getReg())
987 .addUse(AArch64::WZR)
988 .addUse(AArch64::WZR)
989 .addImm(CC);
990
991 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
992 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
993
994 I.eraseFromParent();
995 return true;
996 }
997
Tim Northover7dd378d2016-10-12 22:49:07 +0000998 case TargetOpcode::G_FCMP: {
999 if (Ty != LLT::scalar(1)) {
1000 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1001 << ", expected: " << LLT::scalar(1) << '\n');
1002 return false;
1003 }
1004
1005 unsigned CmpOpc = 0;
1006 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1007 if (CmpTy == LLT::scalar(32)) {
1008 CmpOpc = AArch64::FCMPSrr;
1009 } else if (CmpTy == LLT::scalar(64)) {
1010 CmpOpc = AArch64::FCMPDrr;
1011 } else {
1012 return false;
1013 }
1014
1015 // FIXME: regbank
1016
1017 AArch64CC::CondCode CC1, CC2;
1018 changeFCMPPredToAArch64CC(
1019 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1020
1021 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1022 .addUse(I.getOperand(2).getReg())
1023 .addUse(I.getOperand(3).getReg());
1024
1025 const unsigned DefReg = I.getOperand(0).getReg();
1026 unsigned Def1Reg = DefReg;
1027 if (CC2 != AArch64CC::AL)
1028 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1029
1030 MachineInstr &CSetMI =
1031 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1032 .addDef(Def1Reg)
1033 .addUse(AArch64::WZR)
1034 .addUse(AArch64::WZR)
1035 .addImm(CC1);
1036
1037 if (CC2 != AArch64CC::AL) {
1038 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1039 MachineInstr &CSet2MI =
1040 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1041 .addDef(Def2Reg)
1042 .addUse(AArch64::WZR)
1043 .addUse(AArch64::WZR)
1044 .addImm(CC2);
1045 MachineInstr &OrMI =
1046 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1047 .addDef(DefReg)
1048 .addUse(Def1Reg)
1049 .addUse(Def2Reg);
1050 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1051 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1052 }
1053
1054 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1055 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1056
1057 I.eraseFromParent();
1058 return true;
1059 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001060 }
1061
1062 return false;
1063}