blob: 6490d88a3c8c7b30cd0dbe7c6af0a30a7d78d501 [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the targeting of the InstructionSelector class for
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
15#include "AArch64InstructionSelector.h"
16#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000017#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000018#include "AArch64RegisterBankInfo.h"
19#include "AArch64RegisterInfo.h"
20#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000021#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000022#include "MCTargetDesc/AArch64AddressingModes.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/IR/Type.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
31
32#define DEBUG_TYPE "aarch64-isel"
33
34using namespace llvm;
35
36#ifndef LLVM_BUILD_GLOBAL_ISEL
37#error "You shouldn't build this"
38#endif
39
Daniel Sanders8a4bae92017-03-14 21:32:08 +000040#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000041#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +000042#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +000043
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000044AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +000045 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
46 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +000047 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
48 TRI(*STI.getRegisterInfo()), RBI(RBI)
49#define GET_GLOBALISEL_TEMPORARIES_INIT
50#include "AArch64GenGlobalISel.inc"
51#undef GET_GLOBALISEL_TEMPORARIES_INIT
52{
53}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000054
Tim Northoverfb8d9892016-10-12 22:49:15 +000055// FIXME: This should be target-independent, inferred from the types declared
56// for each class in the bank.
57static const TargetRegisterClass *
58getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
59 const RegisterBankInfo &RBI) {
60 if (RB.getID() == AArch64::GPRRegBankID) {
61 if (Ty.getSizeInBits() <= 32)
62 return &AArch64::GPR32RegClass;
63 if (Ty.getSizeInBits() == 64)
64 return &AArch64::GPR64RegClass;
65 return nullptr;
66 }
67
68 if (RB.getID() == AArch64::FPRRegBankID) {
69 if (Ty.getSizeInBits() == 32)
70 return &AArch64::FPR32RegClass;
71 if (Ty.getSizeInBits() == 64)
72 return &AArch64::FPR64RegClass;
73 if (Ty.getSizeInBits() == 128)
74 return &AArch64::FPR128RegClass;
75 return nullptr;
76 }
77
78 return nullptr;
79}
80
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000081/// Check whether \p I is a currently unsupported binary operation:
82/// - it has an unsized type
83/// - an operand is not a vreg
84/// - all operands are not in the same bank
85/// These are checks that should someday live in the verifier, but right now,
86/// these are mostly limitations of the aarch64 selector.
87static bool unsupportedBinOp(const MachineInstr &I,
88 const AArch64RegisterBankInfo &RBI,
89 const MachineRegisterInfo &MRI,
90 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000091 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000092 if (!Ty.isValid()) {
93 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000094 return true;
95 }
96
97 const RegisterBank *PrevOpBank = nullptr;
98 for (auto &MO : I.operands()) {
99 // FIXME: Support non-register operands.
100 if (!MO.isReg()) {
101 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
102 return true;
103 }
104
105 // FIXME: Can generic operations have physical registers operands? If
106 // so, this will need to be taught about that, and we'll need to get the
107 // bank out of the minimal class for the register.
108 // Either way, this needs to be documented (and possibly verified).
109 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
110 DEBUG(dbgs() << "Generic inst has physical register operand\n");
111 return true;
112 }
113
114 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
115 if (!OpBank) {
116 DEBUG(dbgs() << "Generic register has no bank or class\n");
117 return true;
118 }
119
120 if (PrevOpBank && OpBank != PrevOpBank) {
121 DEBUG(dbgs() << "Generic inst operands have different banks\n");
122 return true;
123 }
124 PrevOpBank = OpBank;
125 }
126 return false;
127}
128
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000129/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000130/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000131/// and of size \p OpSize.
132/// \returns \p GenericOpc if the combination is unsupported.
133static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
134 unsigned OpSize) {
135 switch (RegBankID) {
136 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000137 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000138 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000139 case TargetOpcode::G_SHL:
140 return AArch64::LSLVWr;
141 case TargetOpcode::G_LSHR:
142 return AArch64::LSRVWr;
143 case TargetOpcode::G_ASHR:
144 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000145 default:
146 return GenericOpc;
147 }
Tim Northover55782222016-10-18 20:03:48 +0000148 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000149 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000150 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000151 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000152 case TargetOpcode::G_SHL:
153 return AArch64::LSLVXr;
154 case TargetOpcode::G_LSHR:
155 return AArch64::LSRVXr;
156 case TargetOpcode::G_ASHR:
157 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000158 default:
159 return GenericOpc;
160 }
161 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000162 case AArch64::FPRRegBankID:
163 switch (OpSize) {
164 case 32:
165 switch (GenericOpc) {
166 case TargetOpcode::G_FADD:
167 return AArch64::FADDSrr;
168 case TargetOpcode::G_FSUB:
169 return AArch64::FSUBSrr;
170 case TargetOpcode::G_FMUL:
171 return AArch64::FMULSrr;
172 case TargetOpcode::G_FDIV:
173 return AArch64::FDIVSrr;
174 default:
175 return GenericOpc;
176 }
177 case 64:
178 switch (GenericOpc) {
179 case TargetOpcode::G_FADD:
180 return AArch64::FADDDrr;
181 case TargetOpcode::G_FSUB:
182 return AArch64::FSUBDrr;
183 case TargetOpcode::G_FMUL:
184 return AArch64::FMULDrr;
185 case TargetOpcode::G_FDIV:
186 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000187 case TargetOpcode::G_OR:
188 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000189 default:
190 return GenericOpc;
191 }
192 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 };
194 return GenericOpc;
195}
196
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000197/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
198/// appropriate for the (value) register bank \p RegBankID and of memory access
199/// size \p OpSize. This returns the variant with the base+unsigned-immediate
200/// addressing mode (e.g., LDRXui).
201/// \returns \p GenericOpc if the combination is unsupported.
202static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
203 unsigned OpSize) {
204 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
205 switch (RegBankID) {
206 case AArch64::GPRRegBankID:
207 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000208 case 8:
209 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
210 case 16:
211 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000212 case 32:
213 return isStore ? AArch64::STRWui : AArch64::LDRWui;
214 case 64:
215 return isStore ? AArch64::STRXui : AArch64::LDRXui;
216 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000217 case AArch64::FPRRegBankID:
218 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000219 case 8:
220 return isStore ? AArch64::STRBui : AArch64::LDRBui;
221 case 16:
222 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000223 case 32:
224 return isStore ? AArch64::STRSui : AArch64::LDRSui;
225 case 64:
226 return isStore ? AArch64::STRDui : AArch64::LDRDui;
227 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000228 };
229 return GenericOpc;
230}
231
Quentin Colombetcb629a82016-10-12 03:57:49 +0000232static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
233 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
234 const RegisterBankInfo &RBI) {
235
236 unsigned DstReg = I.getOperand(0).getReg();
237 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
238 assert(I.isCopy() && "Generic operators do not allow physical registers");
239 return true;
240 }
241
242 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
243 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
244 unsigned SrcReg = I.getOperand(1).getReg();
245 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
246 (void)SrcSize;
247 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
248 "No phys reg on generic operators");
249 assert(
250 (DstSize == SrcSize ||
251 // Copies are a mean to setup initial types, the number of
252 // bits may not exactly match.
253 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
254 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
255 // Copies are a mean to copy bits around, as long as we are
256 // on the same register class, that's fine. Otherwise, that
257 // means we need some SUBREG_TO_REG or AND & co.
258 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
259 "Copy with different width?!");
260 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
261 "GPRs cannot get more than 64-bit width values");
262 const TargetRegisterClass *RC = nullptr;
263
264 if (RegBank.getID() == AArch64::FPRRegBankID) {
265 if (DstSize <= 32)
266 RC = &AArch64::FPR32RegClass;
267 else if (DstSize <= 64)
268 RC = &AArch64::FPR64RegClass;
269 else if (DstSize <= 128)
270 RC = &AArch64::FPR128RegClass;
271 else {
272 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
273 return false;
274 }
275 } else {
276 assert(RegBank.getID() == AArch64::GPRRegBankID &&
277 "Bitcast for the flags?");
278 RC =
279 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
280 }
281
282 // No need to constrain SrcReg. It will get constrained when
283 // we hit another of its use or its defs.
284 // Copies do not have constraints.
285 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
286 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
287 << " operand\n");
288 return false;
289 }
290 I.setDesc(TII.get(AArch64::COPY));
291 return true;
292}
293
Tim Northover69271c62016-10-12 22:49:11 +0000294static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
295 if (!DstTy.isScalar() || !SrcTy.isScalar())
296 return GenericOpc;
297
298 const unsigned DstSize = DstTy.getSizeInBits();
299 const unsigned SrcSize = SrcTy.getSizeInBits();
300
301 switch (DstSize) {
302 case 32:
303 switch (SrcSize) {
304 case 32:
305 switch (GenericOpc) {
306 case TargetOpcode::G_SITOFP:
307 return AArch64::SCVTFUWSri;
308 case TargetOpcode::G_UITOFP:
309 return AArch64::UCVTFUWSri;
310 case TargetOpcode::G_FPTOSI:
311 return AArch64::FCVTZSUWSr;
312 case TargetOpcode::G_FPTOUI:
313 return AArch64::FCVTZUUWSr;
314 default:
315 return GenericOpc;
316 }
317 case 64:
318 switch (GenericOpc) {
319 case TargetOpcode::G_SITOFP:
320 return AArch64::SCVTFUXSri;
321 case TargetOpcode::G_UITOFP:
322 return AArch64::UCVTFUXSri;
323 case TargetOpcode::G_FPTOSI:
324 return AArch64::FCVTZSUWDr;
325 case TargetOpcode::G_FPTOUI:
326 return AArch64::FCVTZUUWDr;
327 default:
328 return GenericOpc;
329 }
330 default:
331 return GenericOpc;
332 }
333 case 64:
334 switch (SrcSize) {
335 case 32:
336 switch (GenericOpc) {
337 case TargetOpcode::G_SITOFP:
338 return AArch64::SCVTFUWDri;
339 case TargetOpcode::G_UITOFP:
340 return AArch64::UCVTFUWDri;
341 case TargetOpcode::G_FPTOSI:
342 return AArch64::FCVTZSUXSr;
343 case TargetOpcode::G_FPTOUI:
344 return AArch64::FCVTZUUXSr;
345 default:
346 return GenericOpc;
347 }
348 case 64:
349 switch (GenericOpc) {
350 case TargetOpcode::G_SITOFP:
351 return AArch64::SCVTFUXDri;
352 case TargetOpcode::G_UITOFP:
353 return AArch64::UCVTFUXDri;
354 case TargetOpcode::G_FPTOSI:
355 return AArch64::FCVTZSUXDr;
356 case TargetOpcode::G_FPTOUI:
357 return AArch64::FCVTZUUXDr;
358 default:
359 return GenericOpc;
360 }
361 default:
362 return GenericOpc;
363 }
364 default:
365 return GenericOpc;
366 };
367 return GenericOpc;
368}
369
Tim Northover6c02ad52016-10-12 22:49:04 +0000370static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
371 switch (P) {
372 default:
373 llvm_unreachable("Unknown condition code!");
374 case CmpInst::ICMP_NE:
375 return AArch64CC::NE;
376 case CmpInst::ICMP_EQ:
377 return AArch64CC::EQ;
378 case CmpInst::ICMP_SGT:
379 return AArch64CC::GT;
380 case CmpInst::ICMP_SGE:
381 return AArch64CC::GE;
382 case CmpInst::ICMP_SLT:
383 return AArch64CC::LT;
384 case CmpInst::ICMP_SLE:
385 return AArch64CC::LE;
386 case CmpInst::ICMP_UGT:
387 return AArch64CC::HI;
388 case CmpInst::ICMP_UGE:
389 return AArch64CC::HS;
390 case CmpInst::ICMP_ULT:
391 return AArch64CC::LO;
392 case CmpInst::ICMP_ULE:
393 return AArch64CC::LS;
394 }
395}
396
Tim Northover7dd378d2016-10-12 22:49:07 +0000397static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
398 AArch64CC::CondCode &CondCode,
399 AArch64CC::CondCode &CondCode2) {
400 CondCode2 = AArch64CC::AL;
401 switch (P) {
402 default:
403 llvm_unreachable("Unknown FP condition!");
404 case CmpInst::FCMP_OEQ:
405 CondCode = AArch64CC::EQ;
406 break;
407 case CmpInst::FCMP_OGT:
408 CondCode = AArch64CC::GT;
409 break;
410 case CmpInst::FCMP_OGE:
411 CondCode = AArch64CC::GE;
412 break;
413 case CmpInst::FCMP_OLT:
414 CondCode = AArch64CC::MI;
415 break;
416 case CmpInst::FCMP_OLE:
417 CondCode = AArch64CC::LS;
418 break;
419 case CmpInst::FCMP_ONE:
420 CondCode = AArch64CC::MI;
421 CondCode2 = AArch64CC::GT;
422 break;
423 case CmpInst::FCMP_ORD:
424 CondCode = AArch64CC::VC;
425 break;
426 case CmpInst::FCMP_UNO:
427 CondCode = AArch64CC::VS;
428 break;
429 case CmpInst::FCMP_UEQ:
430 CondCode = AArch64CC::EQ;
431 CondCode2 = AArch64CC::VS;
432 break;
433 case CmpInst::FCMP_UGT:
434 CondCode = AArch64CC::HI;
435 break;
436 case CmpInst::FCMP_UGE:
437 CondCode = AArch64CC::PL;
438 break;
439 case CmpInst::FCMP_ULT:
440 CondCode = AArch64CC::LT;
441 break;
442 case CmpInst::FCMP_ULE:
443 CondCode = AArch64CC::LE;
444 break;
445 case CmpInst::FCMP_UNE:
446 CondCode = AArch64CC::NE;
447 break;
448 }
449}
450
Tim Northovere9600d82017-02-08 17:57:27 +0000451bool AArch64InstructionSelector::selectVaStartAAPCS(
452 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
453 return false;
454}
455
456bool AArch64InstructionSelector::selectVaStartDarwin(
457 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
458 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
459 unsigned ListReg = I.getOperand(0).getReg();
460
461 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
462
463 auto MIB =
464 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
465 .addDef(ArgsAddrReg)
466 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
467 .addImm(0)
468 .addImm(0);
469
470 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
471
472 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
473 .addUse(ArgsAddrReg)
474 .addUse(ListReg)
475 .addImm(0)
476 .addMemOperand(*I.memoperands_begin());
477
478 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
479 I.eraseFromParent();
480 return true;
481}
482
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000483bool AArch64InstructionSelector::select(MachineInstr &I) const {
484 assert(I.getParent() && "Instruction should be in a basic block!");
485 assert(I.getParent()->getParent() && "Instruction should be in a function!");
486
487 MachineBasicBlock &MBB = *I.getParent();
488 MachineFunction &MF = *MBB.getParent();
489 MachineRegisterInfo &MRI = MF.getRegInfo();
490
Tim Northovercdf23f12016-10-31 18:30:59 +0000491 unsigned Opcode = I.getOpcode();
492 if (!isPreISelGenericOpcode(I.getOpcode())) {
493 // Certain non-generic instructions also need some special handling.
494
495 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
496 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000497
498 if (Opcode == TargetOpcode::PHI) {
499 const unsigned DefReg = I.getOperand(0).getReg();
500 const LLT DefTy = MRI.getType(DefReg);
501
502 const TargetRegisterClass *DefRC = nullptr;
503 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
504 DefRC = TRI.getRegClass(DefReg);
505 } else {
506 const RegClassOrRegBank &RegClassOrBank =
507 MRI.getRegClassOrRegBank(DefReg);
508
509 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
510 if (!DefRC) {
511 if (!DefTy.isValid()) {
512 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
513 return false;
514 }
515 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
516 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
517 if (!DefRC) {
518 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
519 return false;
520 }
521 }
522 }
523
524 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
525 }
526
527 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000528 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000529
530 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000531 }
532
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000533
534 if (I.getNumOperands() != I.getNumExplicitOperands()) {
535 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
536 return false;
537 }
538
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000539 if (selectImpl(I))
540 return true;
541
Tim Northover32a078a2016-09-15 10:09:59 +0000542 LLT Ty =
543 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000544
Tim Northover69271c62016-10-12 22:49:11 +0000545 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000546 case TargetOpcode::G_BRCOND: {
547 if (Ty.getSizeInBits() > 32) {
548 // We shouldn't need this on AArch64, but it would be implemented as an
549 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
550 // bit being tested is < 32.
551 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
552 << ", expected at most 32-bits");
553 return false;
554 }
555
556 const unsigned CondReg = I.getOperand(0).getReg();
557 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
558
559 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
560 .addUse(CondReg)
561 .addImm(/*bit offset=*/0)
562 .addMBB(DestMBB);
563
564 I.eraseFromParent();
565 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
566 }
567
Kristof Beyls65a12c02017-01-30 09:13:18 +0000568 case TargetOpcode::G_BRINDIRECT: {
569 I.setDesc(TII.get(AArch64::BR));
570 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
571 }
572
Tim Northover4494d692016-10-18 19:47:57 +0000573 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000574 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000575 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
576
577 const LLT s32 = LLT::scalar(32);
578 const LLT s64 = LLT::scalar(64);
579 const LLT p0 = LLT::pointer(0, 64);
580
581 const unsigned DefReg = I.getOperand(0).getReg();
582 const LLT DefTy = MRI.getType(DefReg);
583 const unsigned DefSize = DefTy.getSizeInBits();
584 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
585
586 // FIXME: Redundant check, but even less readable when factored out.
587 if (isFP) {
588 if (Ty != s32 && Ty != s64) {
589 DEBUG(dbgs() << "Unable to materialize FP " << Ty
590 << " constant, expected: " << s32 << " or " << s64
591 << '\n');
592 return false;
593 }
594
595 if (RB.getID() != AArch64::FPRRegBankID) {
596 DEBUG(dbgs() << "Unable to materialize FP " << Ty
597 << " constant on bank: " << RB << ", expected: FPR\n");
598 return false;
599 }
600 } else {
601 if (Ty != s32 && Ty != s64 && Ty != p0) {
602 DEBUG(dbgs() << "Unable to materialize integer " << Ty
603 << " constant, expected: " << s32 << ", " << s64 << ", or "
604 << p0 << '\n');
605 return false;
606 }
607
608 if (RB.getID() != AArch64::GPRRegBankID) {
609 DEBUG(dbgs() << "Unable to materialize integer " << Ty
610 << " constant on bank: " << RB << ", expected: GPR\n");
611 return false;
612 }
613 }
614
615 const unsigned MovOpc =
616 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
617
618 I.setDesc(TII.get(MovOpc));
619
620 if (isFP) {
621 const TargetRegisterClass &GPRRC =
622 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
623 const TargetRegisterClass &FPRRC =
624 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
625
626 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
627 MachineOperand &RegOp = I.getOperand(0);
628 RegOp.setReg(DefGPRReg);
629
630 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
631 TII.get(AArch64::COPY))
632 .addDef(DefReg)
633 .addUse(DefGPRReg);
634
635 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
636 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
637 return false;
638 }
639
640 MachineOperand &ImmOp = I.getOperand(1);
641 // FIXME: Is going through int64_t always correct?
642 ImmOp.ChangeToImmediate(
643 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000644 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000645 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
646 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000647 } else if (I.getOperand(1).isImm()) {
648 uint64_t Val = I.getOperand(1).getImm();
649 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000650 }
651
652 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
653 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000654 }
655
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000656 case TargetOpcode::G_FRAME_INDEX: {
657 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000658 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000659 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000660 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000661 return false;
662 }
663
664 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000665
666 // MOs for a #0 shifted immediate.
667 I.addOperand(MachineOperand::CreateImm(0));
668 I.addOperand(MachineOperand::CreateImm(0));
669
670 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
671 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000672
673 case TargetOpcode::G_GLOBAL_VALUE: {
674 auto GV = I.getOperand(1).getGlobal();
675 if (GV->isThreadLocal()) {
676 // FIXME: we don't support TLS yet.
677 return false;
678 }
679 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000680 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000681 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000682 I.getOperand(1).setTargetFlags(OpFlags);
683 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000684 I.setDesc(TII.get(AArch64::MOVaddr));
685 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
686 MachineInstrBuilder MIB(MF, I);
687 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
688 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
689 }
690 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
691 }
692
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000693 case TargetOpcode::G_LOAD:
694 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000695 LLT MemTy = Ty;
696 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000697
Tim Northover5ae83502016-09-15 09:20:34 +0000698 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000699 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000700 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000701 return false;
702 }
703
Tim Northover48dfa1a2017-02-13 22:14:16 +0000704 auto &MemOp = **I.memoperands_begin();
705 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
706 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
707 return false;
708 }
709
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000710#ifndef NDEBUG
711 // Sanity-check the pointer register.
712 const unsigned PtrReg = I.getOperand(1).getReg();
713 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
714 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
715 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000716 assert(MRI.getType(PtrReg).isPointer() &&
717 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000718#endif
719
720 const unsigned ValReg = I.getOperand(0).getReg();
721 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
722
723 const unsigned NewOpc =
724 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
725 if (NewOpc == I.getOpcode())
726 return false;
727
728 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000729
730 I.addOperand(MachineOperand::CreateImm(0));
731 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
732 }
733
Tim Northover9dd78f82017-02-08 21:22:25 +0000734 case TargetOpcode::G_SMULH:
735 case TargetOpcode::G_UMULH: {
736 // Reject the various things we don't support yet.
737 if (unsupportedBinOp(I, RBI, MRI, TRI))
738 return false;
739
740 const unsigned DefReg = I.getOperand(0).getReg();
741 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
742
743 if (RB.getID() != AArch64::GPRRegBankID) {
744 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
745 return false;
746 }
747
748 if (Ty != LLT::scalar(64)) {
749 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
750 << ", expected: " << LLT::scalar(64) << '\n');
751 return false;
752 }
753
754 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
755 : AArch64::UMULHrr;
756 I.setDesc(TII.get(NewOpc));
757
758 // Now that we selected an opcode, we need to constrain the register
759 // operands to use appropriate classes.
760 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
761 }
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000762 case TargetOpcode::G_MUL: {
763 // Reject the various things we don't support yet.
764 if (unsupportedBinOp(I, RBI, MRI, TRI))
765 return false;
766
767 const unsigned DefReg = I.getOperand(0).getReg();
768 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
769
770 if (RB.getID() != AArch64::GPRRegBankID) {
771 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
772 return false;
773 }
774
775 unsigned ZeroReg;
776 unsigned NewOpc;
Tim Northover55782222016-10-18 20:03:48 +0000777 if (Ty.isScalar() && Ty.getSizeInBits() <= 32) {
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000778 NewOpc = AArch64::MADDWrrr;
779 ZeroReg = AArch64::WZR;
780 } else if (Ty == LLT::scalar(64)) {
781 NewOpc = AArch64::MADDXrrr;
782 ZeroReg = AArch64::XZR;
783 } else {
784 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
785 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
786 return false;
787 }
788
789 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000790
791 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
792
793 // Now that we selected an opcode, we need to constrain the register
794 // operands to use appropriate classes.
795 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
796 }
797
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000798 case TargetOpcode::G_FADD:
799 case TargetOpcode::G_FSUB:
800 case TargetOpcode::G_FMUL:
801 case TargetOpcode::G_FDIV:
802
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000803 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000804 case TargetOpcode::G_SHL:
805 case TargetOpcode::G_LSHR:
806 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000807 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000808 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000809 if (unsupportedBinOp(I, RBI, MRI, TRI))
810 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000811
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000812 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000813
814 const unsigned DefReg = I.getOperand(0).getReg();
815 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
816
817 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
818 if (NewOpc == I.getOpcode())
819 return false;
820
821 I.setDesc(TII.get(NewOpc));
822 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000823
824 // Now that we selected an opcode, we need to constrain the register
825 // operands to use appropriate classes.
826 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
827 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000828
Tim Northover398c5f52017-02-14 20:56:29 +0000829 case TargetOpcode::G_PTR_MASK: {
830 uint64_t Align = I.getOperand(2).getImm();
831 if (Align >= 64 || Align == 0)
832 return false;
833
834 uint64_t Mask = ~((1ULL << Align) - 1);
835 I.setDesc(TII.get(AArch64::ANDXri));
836 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
837
838 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
839 }
Tim Northover037af52c2016-10-31 18:31:09 +0000840 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +0000841 case TargetOpcode::G_TRUNC: {
842 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
843 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
844
845 const unsigned DstReg = I.getOperand(0).getReg();
846 const unsigned SrcReg = I.getOperand(1).getReg();
847
848 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
849 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
850
851 if (DstRB.getID() != SrcRB.getID()) {
852 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
853 return false;
854 }
855
856 if (DstRB.getID() == AArch64::GPRRegBankID) {
857 const TargetRegisterClass *DstRC =
858 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
859 if (!DstRC)
860 return false;
861
862 const TargetRegisterClass *SrcRC =
863 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
864 if (!SrcRC)
865 return false;
866
867 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
868 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
869 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
870 return false;
871 }
872
873 if (DstRC == SrcRC) {
874 // Nothing to be done
875 } else if (DstRC == &AArch64::GPR32RegClass &&
876 SrcRC == &AArch64::GPR64RegClass) {
877 I.getOperand(1).setSubReg(AArch64::sub_32);
878 } else {
879 return false;
880 }
881
882 I.setDesc(TII.get(TargetOpcode::COPY));
883 return true;
884 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
885 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
886 I.setDesc(TII.get(AArch64::XTNv4i16));
887 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
888 return true;
889 }
890 }
891
892 return false;
893 }
894
Tim Northover3d38b3a2016-10-11 20:50:21 +0000895 case TargetOpcode::G_ANYEXT: {
896 const unsigned DstReg = I.getOperand(0).getReg();
897 const unsigned SrcReg = I.getOperand(1).getReg();
898
Quentin Colombetcb629a82016-10-12 03:57:49 +0000899 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
900 if (RBDst.getID() != AArch64::GPRRegBankID) {
901 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
902 return false;
903 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000904
Quentin Colombetcb629a82016-10-12 03:57:49 +0000905 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
906 if (RBSrc.getID() != AArch64::GPRRegBankID) {
907 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000908 return false;
909 }
910
911 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
912
913 if (DstSize == 0) {
914 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
915 return false;
916 }
917
Quentin Colombetcb629a82016-10-12 03:57:49 +0000918 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000919 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
920 << ", expected: 32 or 64\n");
921 return false;
922 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000923 // At this point G_ANYEXT is just like a plain COPY, but we need
924 // to explicitly form the 64-bit value if any.
925 if (DstSize > 32) {
926 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
927 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
928 .addDef(ExtSrc)
929 .addImm(0)
930 .addUse(SrcReg)
931 .addImm(AArch64::sub_32);
932 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000933 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000934 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000935 }
936
937 case TargetOpcode::G_ZEXT:
938 case TargetOpcode::G_SEXT: {
939 unsigned Opcode = I.getOpcode();
940 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
941 SrcTy = MRI.getType(I.getOperand(1).getReg());
942 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
943 const unsigned DefReg = I.getOperand(0).getReg();
944 const unsigned SrcReg = I.getOperand(1).getReg();
945 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
946
947 if (RB.getID() != AArch64::GPRRegBankID) {
948 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
949 << ", expected: GPR\n");
950 return false;
951 }
952
953 MachineInstr *ExtI;
954 if (DstTy == LLT::scalar(64)) {
955 // FIXME: Can we avoid manually doing this?
956 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
957 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
958 << " operand\n");
959 return false;
960 }
961
962 const unsigned SrcXReg =
963 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
964 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
965 .addDef(SrcXReg)
966 .addImm(0)
967 .addUse(SrcReg)
968 .addImm(AArch64::sub_32);
969
970 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
971 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
972 .addDef(DefReg)
973 .addUse(SrcXReg)
974 .addImm(0)
975 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +0000976 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000977 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
978 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
979 .addDef(DefReg)
980 .addUse(SrcReg)
981 .addImm(0)
982 .addImm(SrcTy.getSizeInBits() - 1);
983 } else {
984 return false;
985 }
986
987 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
988
989 I.eraseFromParent();
990 return true;
991 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000992
Tim Northover69271c62016-10-12 22:49:11 +0000993 case TargetOpcode::G_SITOFP:
994 case TargetOpcode::G_UITOFP:
995 case TargetOpcode::G_FPTOSI:
996 case TargetOpcode::G_FPTOUI: {
997 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
998 SrcTy = MRI.getType(I.getOperand(1).getReg());
999 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1000 if (NewOpc == Opcode)
1001 return false;
1002
1003 I.setDesc(TII.get(NewOpc));
1004 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1005
1006 return true;
1007 }
1008
1009
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001010 case TargetOpcode::G_INTTOPTR:
Quentin Colombet9de30fa2016-10-12 03:57:52 +00001011 case TargetOpcode::G_BITCAST:
1012 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +00001013
Tim Northover5f7dea82016-11-08 17:44:07 +00001014 case TargetOpcode::G_FPEXT: {
1015 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(64)) {
1016 DEBUG(dbgs() << "G_FPEXT to type " << Ty
1017 << ", expected: " << LLT::scalar(64) << '\n');
1018 return false;
1019 }
1020
1021 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(32)) {
1022 DEBUG(dbgs() << "G_FPEXT from type " << Ty
1023 << ", expected: " << LLT::scalar(32) << '\n');
1024 return false;
1025 }
1026
1027 const unsigned DefReg = I.getOperand(0).getReg();
1028 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1029
1030 if (RB.getID() != AArch64::FPRRegBankID) {
1031 DEBUG(dbgs() << "G_FPEXT on bank: " << RB << ", expected: FPR\n");
1032 return false;
1033 }
1034
1035 I.setDesc(TII.get(AArch64::FCVTDSr));
1036 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1037
1038 return true;
1039 }
1040
1041 case TargetOpcode::G_FPTRUNC: {
1042 if (MRI.getType(I.getOperand(0).getReg()) != LLT::scalar(32)) {
1043 DEBUG(dbgs() << "G_FPTRUNC to type " << Ty
1044 << ", expected: " << LLT::scalar(32) << '\n');
1045 return false;
1046 }
1047
1048 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(64)) {
1049 DEBUG(dbgs() << "G_FPTRUNC from type " << Ty
1050 << ", expected: " << LLT::scalar(64) << '\n');
1051 return false;
1052 }
1053
1054 const unsigned DefReg = I.getOperand(0).getReg();
1055 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1056
1057 if (RB.getID() != AArch64::FPRRegBankID) {
1058 DEBUG(dbgs() << "G_FPTRUNC on bank: " << RB << ", expected: FPR\n");
1059 return false;
1060 }
1061
1062 I.setDesc(TII.get(AArch64::FCVTSDr));
1063 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1064
1065 return true;
1066 }
1067
Tim Northover9ac0eba2016-11-08 00:45:29 +00001068 case TargetOpcode::G_SELECT: {
1069 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1070 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1071 << ", expected: " << LLT::scalar(1) << '\n');
1072 return false;
1073 }
1074
1075 const unsigned CondReg = I.getOperand(1).getReg();
1076 const unsigned TReg = I.getOperand(2).getReg();
1077 const unsigned FReg = I.getOperand(3).getReg();
1078
1079 unsigned CSelOpc = 0;
1080
1081 if (Ty == LLT::scalar(32)) {
1082 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001083 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001084 CSelOpc = AArch64::CSELXr;
1085 } else {
1086 return false;
1087 }
1088
1089 MachineInstr &TstMI =
1090 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1091 .addDef(AArch64::WZR)
1092 .addUse(CondReg)
1093 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1094
1095 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1096 .addDef(I.getOperand(0).getReg())
1097 .addUse(TReg)
1098 .addUse(FReg)
1099 .addImm(AArch64CC::NE);
1100
1101 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1102 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1103
1104 I.eraseFromParent();
1105 return true;
1106 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001107 case TargetOpcode::G_ICMP: {
1108 if (Ty != LLT::scalar(1)) {
1109 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1110 << ", expected: " << LLT::scalar(1) << '\n');
1111 return false;
1112 }
1113
1114 unsigned CmpOpc = 0;
1115 unsigned ZReg = 0;
1116
1117 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1118 if (CmpTy == LLT::scalar(32)) {
1119 CmpOpc = AArch64::SUBSWrr;
1120 ZReg = AArch64::WZR;
1121 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1122 CmpOpc = AArch64::SUBSXrr;
1123 ZReg = AArch64::XZR;
1124 } else {
1125 return false;
1126 }
1127
Kristof Beyls22524402017-01-05 10:16:08 +00001128 // CSINC increments the result by one when the condition code is false.
1129 // Therefore, we have to invert the predicate to get an increment by 1 when
1130 // the predicate is true.
1131 const AArch64CC::CondCode invCC =
1132 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1133 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001134
1135 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1136 .addDef(ZReg)
1137 .addUse(I.getOperand(2).getReg())
1138 .addUse(I.getOperand(3).getReg());
1139
1140 MachineInstr &CSetMI =
1141 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1142 .addDef(I.getOperand(0).getReg())
1143 .addUse(AArch64::WZR)
1144 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001145 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001146
1147 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1148 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1149
1150 I.eraseFromParent();
1151 return true;
1152 }
1153
Tim Northover7dd378d2016-10-12 22:49:07 +00001154 case TargetOpcode::G_FCMP: {
1155 if (Ty != LLT::scalar(1)) {
1156 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1157 << ", expected: " << LLT::scalar(1) << '\n');
1158 return false;
1159 }
1160
1161 unsigned CmpOpc = 0;
1162 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1163 if (CmpTy == LLT::scalar(32)) {
1164 CmpOpc = AArch64::FCMPSrr;
1165 } else if (CmpTy == LLT::scalar(64)) {
1166 CmpOpc = AArch64::FCMPDrr;
1167 } else {
1168 return false;
1169 }
1170
1171 // FIXME: regbank
1172
1173 AArch64CC::CondCode CC1, CC2;
1174 changeFCMPPredToAArch64CC(
1175 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1176
1177 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1178 .addUse(I.getOperand(2).getReg())
1179 .addUse(I.getOperand(3).getReg());
1180
1181 const unsigned DefReg = I.getOperand(0).getReg();
1182 unsigned Def1Reg = DefReg;
1183 if (CC2 != AArch64CC::AL)
1184 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1185
1186 MachineInstr &CSetMI =
1187 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1188 .addDef(Def1Reg)
1189 .addUse(AArch64::WZR)
1190 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001191 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001192
1193 if (CC2 != AArch64CC::AL) {
1194 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1195 MachineInstr &CSet2MI =
1196 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1197 .addDef(Def2Reg)
1198 .addUse(AArch64::WZR)
1199 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001200 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001201 MachineInstr &OrMI =
1202 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1203 .addDef(DefReg)
1204 .addUse(Def1Reg)
1205 .addUse(Def2Reg);
1206 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1207 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1208 }
1209
1210 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1211 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1212
1213 I.eraseFromParent();
1214 return true;
1215 }
Tim Northovere9600d82017-02-08 17:57:27 +00001216 case TargetOpcode::G_VASTART:
1217 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1218 : selectVaStartAAPCS(I, MF, MRI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001219 }
1220
1221 return false;
1222}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001223
1224/// SelectArithImmed - Select an immediate value that can be represented as
1225/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1226/// Val set to the 12-bit value and Shift set to the shifter operand.
1227bool AArch64InstructionSelector::selectArithImmed(
1228 MachineOperand &Root, MachineOperand &Result1,
1229 MachineOperand &Result2) const {
1230 MachineInstr &MI = *Root.getParent();
1231 MachineBasicBlock &MBB = *MI.getParent();
1232 MachineFunction &MF = *MBB.getParent();
1233 MachineRegisterInfo &MRI = MF.getRegInfo();
1234
1235 // This function is called from the addsub_shifted_imm ComplexPattern,
1236 // which lists [imm] as the list of opcode it's interested in, however
1237 // we still need to check whether the operand is actually an immediate
1238 // here because the ComplexPattern opcode list is only used in
1239 // root-level opcode matching.
1240 uint64_t Immed;
1241 if (Root.isImm())
1242 Immed = Root.getImm();
1243 else if (Root.isCImm())
1244 Immed = Root.getCImm()->getZExtValue();
1245 else if (Root.isReg()) {
1246 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1247 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1248 return false;
Daniel Sanders0e642022017-03-16 18:04:50 +00001249 MachineOperand &Op1 = Def->getOperand(1);
1250 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1251 return false;
1252 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001253 } else
1254 return false;
1255
1256 unsigned ShiftAmt;
1257
1258 if (Immed >> 12 == 0) {
1259 ShiftAmt = 0;
1260 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1261 ShiftAmt = 12;
1262 Immed = Immed >> 12;
1263 } else
1264 return false;
1265
1266 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1267 Result1.ChangeToImmediate(Immed);
1268 Result1.clearParent();
1269 Result2.ChangeToImmediate(ShVal);
1270 Result2.clearParent();
1271 return true;
1272}