blob: 3a3090ee21e13e36e4d85a22ee937523b0f03b81 [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:
126 switch (OpSize) {
127 case 32:
128 switch (GenericOpc) {
129 case TargetOpcode::G_OR:
130 return AArch64::ORRWrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000131 case TargetOpcode::G_XOR:
132 return AArch64::EORWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000133 case TargetOpcode::G_AND:
134 return AArch64::ANDWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000135 case TargetOpcode::G_ADD:
136 return AArch64::ADDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000137 case TargetOpcode::G_SUB:
138 return AArch64::SUBWrr;
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 Bougacha1d0560b2016-08-18 15:17:13 +0000145 case TargetOpcode::G_SDIV:
146 return AArch64::SDIVWr;
147 case TargetOpcode::G_UDIV:
148 return AArch64::UDIVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000149 default:
150 return GenericOpc;
151 }
152 case 64:
153 switch (GenericOpc) {
154 case TargetOpcode::G_OR:
155 return AArch64::ORRXrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000156 case TargetOpcode::G_XOR:
157 return AArch64::EORXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000158 case TargetOpcode::G_AND:
159 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000160 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000161 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000162 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000163 case TargetOpcode::G_SUB:
164 return AArch64::SUBXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000165 case TargetOpcode::G_SHL:
166 return AArch64::LSLVXr;
167 case TargetOpcode::G_LSHR:
168 return AArch64::LSRVXr;
169 case TargetOpcode::G_ASHR:
170 return AArch64::ASRVXr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000171 case TargetOpcode::G_SDIV:
172 return AArch64::SDIVXr;
173 case TargetOpcode::G_UDIV:
174 return AArch64::UDIVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000175 default:
176 return GenericOpc;
177 }
178 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000179 case AArch64::FPRRegBankID:
180 switch (OpSize) {
181 case 32:
182 switch (GenericOpc) {
183 case TargetOpcode::G_FADD:
184 return AArch64::FADDSrr;
185 case TargetOpcode::G_FSUB:
186 return AArch64::FSUBSrr;
187 case TargetOpcode::G_FMUL:
188 return AArch64::FMULSrr;
189 case TargetOpcode::G_FDIV:
190 return AArch64::FDIVSrr;
191 default:
192 return GenericOpc;
193 }
194 case 64:
195 switch (GenericOpc) {
196 case TargetOpcode::G_FADD:
197 return AArch64::FADDDrr;
198 case TargetOpcode::G_FSUB:
199 return AArch64::FSUBDrr;
200 case TargetOpcode::G_FMUL:
201 return AArch64::FMULDrr;
202 case TargetOpcode::G_FDIV:
203 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000204 case TargetOpcode::G_OR:
205 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000206 default:
207 return GenericOpc;
208 }
209 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000210 };
211 return GenericOpc;
212}
213
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000214/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
215/// appropriate for the (value) register bank \p RegBankID and of memory access
216/// size \p OpSize. This returns the variant with the base+unsigned-immediate
217/// addressing mode (e.g., LDRXui).
218/// \returns \p GenericOpc if the combination is unsupported.
219static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
220 unsigned OpSize) {
221 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
222 switch (RegBankID) {
223 case AArch64::GPRRegBankID:
224 switch (OpSize) {
225 case 32:
226 return isStore ? AArch64::STRWui : AArch64::LDRWui;
227 case 64:
228 return isStore ? AArch64::STRXui : AArch64::LDRXui;
229 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000230 case AArch64::FPRRegBankID:
231 switch (OpSize) {
232 case 32:
233 return isStore ? AArch64::STRSui : AArch64::LDRSui;
234 case 64:
235 return isStore ? AArch64::STRDui : AArch64::LDRDui;
236 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000237 };
238 return GenericOpc;
239}
240
Quentin Colombetcb629a82016-10-12 03:57:49 +0000241static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
242 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
243 const RegisterBankInfo &RBI) {
244
245 unsigned DstReg = I.getOperand(0).getReg();
246 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
247 assert(I.isCopy() && "Generic operators do not allow physical registers");
248 return true;
249 }
250
251 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
252 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
253 unsigned SrcReg = I.getOperand(1).getReg();
254 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
255 (void)SrcSize;
256 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
257 "No phys reg on generic operators");
258 assert(
259 (DstSize == SrcSize ||
260 // Copies are a mean to setup initial types, the number of
261 // bits may not exactly match.
262 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
263 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
264 // Copies are a mean to copy bits around, as long as we are
265 // on the same register class, that's fine. Otherwise, that
266 // means we need some SUBREG_TO_REG or AND & co.
267 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
268 "Copy with different width?!");
269 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
270 "GPRs cannot get more than 64-bit width values");
271 const TargetRegisterClass *RC = nullptr;
272
273 if (RegBank.getID() == AArch64::FPRRegBankID) {
274 if (DstSize <= 32)
275 RC = &AArch64::FPR32RegClass;
276 else if (DstSize <= 64)
277 RC = &AArch64::FPR64RegClass;
278 else if (DstSize <= 128)
279 RC = &AArch64::FPR128RegClass;
280 else {
281 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
282 return false;
283 }
284 } else {
285 assert(RegBank.getID() == AArch64::GPRRegBankID &&
286 "Bitcast for the flags?");
287 RC =
288 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
289 }
290
291 // No need to constrain SrcReg. It will get constrained when
292 // we hit another of its use or its defs.
293 // Copies do not have constraints.
294 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
295 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
296 << " operand\n");
297 return false;
298 }
299 I.setDesc(TII.get(AArch64::COPY));
300 return true;
301}
302
Tim Northover69271c62016-10-12 22:49:11 +0000303static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
304 if (!DstTy.isScalar() || !SrcTy.isScalar())
305 return GenericOpc;
306
307 const unsigned DstSize = DstTy.getSizeInBits();
308 const unsigned SrcSize = SrcTy.getSizeInBits();
309
310 switch (DstSize) {
311 case 32:
312 switch (SrcSize) {
313 case 32:
314 switch (GenericOpc) {
315 case TargetOpcode::G_SITOFP:
316 return AArch64::SCVTFUWSri;
317 case TargetOpcode::G_UITOFP:
318 return AArch64::UCVTFUWSri;
319 case TargetOpcode::G_FPTOSI:
320 return AArch64::FCVTZSUWSr;
321 case TargetOpcode::G_FPTOUI:
322 return AArch64::FCVTZUUWSr;
323 default:
324 return GenericOpc;
325 }
326 case 64:
327 switch (GenericOpc) {
328 case TargetOpcode::G_SITOFP:
329 return AArch64::SCVTFUXSri;
330 case TargetOpcode::G_UITOFP:
331 return AArch64::UCVTFUXSri;
332 case TargetOpcode::G_FPTOSI:
333 return AArch64::FCVTZSUWDr;
334 case TargetOpcode::G_FPTOUI:
335 return AArch64::FCVTZUUWDr;
336 default:
337 return GenericOpc;
338 }
339 default:
340 return GenericOpc;
341 }
342 case 64:
343 switch (SrcSize) {
344 case 32:
345 switch (GenericOpc) {
346 case TargetOpcode::G_SITOFP:
347 return AArch64::SCVTFUWDri;
348 case TargetOpcode::G_UITOFP:
349 return AArch64::UCVTFUWDri;
350 case TargetOpcode::G_FPTOSI:
351 return AArch64::FCVTZSUXSr;
352 case TargetOpcode::G_FPTOUI:
353 return AArch64::FCVTZUUXSr;
354 default:
355 return GenericOpc;
356 }
357 case 64:
358 switch (GenericOpc) {
359 case TargetOpcode::G_SITOFP:
360 return AArch64::SCVTFUXDri;
361 case TargetOpcode::G_UITOFP:
362 return AArch64::UCVTFUXDri;
363 case TargetOpcode::G_FPTOSI:
364 return AArch64::FCVTZSUXDr;
365 case TargetOpcode::G_FPTOUI:
366 return AArch64::FCVTZUUXDr;
367 default:
368 return GenericOpc;
369 }
370 default:
371 return GenericOpc;
372 }
373 default:
374 return GenericOpc;
375 };
376 return GenericOpc;
377}
378
Tim Northover6c02ad52016-10-12 22:49:04 +0000379static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
380 switch (P) {
381 default:
382 llvm_unreachable("Unknown condition code!");
383 case CmpInst::ICMP_NE:
384 return AArch64CC::NE;
385 case CmpInst::ICMP_EQ:
386 return AArch64CC::EQ;
387 case CmpInst::ICMP_SGT:
388 return AArch64CC::GT;
389 case CmpInst::ICMP_SGE:
390 return AArch64CC::GE;
391 case CmpInst::ICMP_SLT:
392 return AArch64CC::LT;
393 case CmpInst::ICMP_SLE:
394 return AArch64CC::LE;
395 case CmpInst::ICMP_UGT:
396 return AArch64CC::HI;
397 case CmpInst::ICMP_UGE:
398 return AArch64CC::HS;
399 case CmpInst::ICMP_ULT:
400 return AArch64CC::LO;
401 case CmpInst::ICMP_ULE:
402 return AArch64CC::LS;
403 }
404}
405
Tim Northover7dd378d2016-10-12 22:49:07 +0000406static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
407 AArch64CC::CondCode &CondCode,
408 AArch64CC::CondCode &CondCode2) {
409 CondCode2 = AArch64CC::AL;
410 switch (P) {
411 default:
412 llvm_unreachable("Unknown FP condition!");
413 case CmpInst::FCMP_OEQ:
414 CondCode = AArch64CC::EQ;
415 break;
416 case CmpInst::FCMP_OGT:
417 CondCode = AArch64CC::GT;
418 break;
419 case CmpInst::FCMP_OGE:
420 CondCode = AArch64CC::GE;
421 break;
422 case CmpInst::FCMP_OLT:
423 CondCode = AArch64CC::MI;
424 break;
425 case CmpInst::FCMP_OLE:
426 CondCode = AArch64CC::LS;
427 break;
428 case CmpInst::FCMP_ONE:
429 CondCode = AArch64CC::MI;
430 CondCode2 = AArch64CC::GT;
431 break;
432 case CmpInst::FCMP_ORD:
433 CondCode = AArch64CC::VC;
434 break;
435 case CmpInst::FCMP_UNO:
436 CondCode = AArch64CC::VS;
437 break;
438 case CmpInst::FCMP_UEQ:
439 CondCode = AArch64CC::EQ;
440 CondCode2 = AArch64CC::VS;
441 break;
442 case CmpInst::FCMP_UGT:
443 CondCode = AArch64CC::HI;
444 break;
445 case CmpInst::FCMP_UGE:
446 CondCode = AArch64CC::PL;
447 break;
448 case CmpInst::FCMP_ULT:
449 CondCode = AArch64CC::LT;
450 break;
451 case CmpInst::FCMP_ULE:
452 CondCode = AArch64CC::LE;
453 break;
454 case CmpInst::FCMP_UNE:
455 CondCode = AArch64CC::NE;
456 break;
457 }
458}
459
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000460bool AArch64InstructionSelector::select(MachineInstr &I) const {
461 assert(I.getParent() && "Instruction should be in a basic block!");
462 assert(I.getParent()->getParent() && "Instruction should be in a function!");
463
464 MachineBasicBlock &MBB = *I.getParent();
465 MachineFunction &MF = *MBB.getParent();
466 MachineRegisterInfo &MRI = MF.getRegInfo();
467
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000468 if (!isPreISelGenericOpcode(I.getOpcode()))
Quentin Colombetcb629a82016-10-12 03:57:49 +0000469 return !I.isCopy() || selectCopy(I, TII, MRI, TRI, RBI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000470
471 if (I.getNumOperands() != I.getNumExplicitOperands()) {
472 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
473 return false;
474 }
475
Tim Northover69271c62016-10-12 22:49:11 +0000476 unsigned Opcode = I.getOpcode();
Tim Northover32a078a2016-09-15 10:09:59 +0000477 LLT Ty =
478 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000479
Tim Northover69271c62016-10-12 22:49:11 +0000480 switch (Opcode) {
Ahmed Bougacha85505092016-07-28 17:15:15 +0000481 case TargetOpcode::G_BR: {
482 I.setDesc(TII.get(AArch64::B));
Ahmed Bougacha85505092016-07-28 17:15:15 +0000483 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000484 }
485
Tim Northover5e3dbf32016-10-12 22:49:01 +0000486 case TargetOpcode::G_BRCOND: {
487 if (Ty.getSizeInBits() > 32) {
488 // We shouldn't need this on AArch64, but it would be implemented as an
489 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
490 // bit being tested is < 32.
491 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
492 << ", expected at most 32-bits");
493 return false;
494 }
495
496 const unsigned CondReg = I.getOperand(0).getReg();
497 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
498
499 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
500 .addUse(CondReg)
501 .addImm(/*bit offset=*/0)
502 .addMBB(DestMBB);
503
504 I.eraseFromParent();
505 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
506 }
507
Tim Northover4edc60d2016-10-10 21:49:42 +0000508 case TargetOpcode::G_CONSTANT: {
509 if (Ty.getSizeInBits() <= 32)
510 I.setDesc(TII.get(AArch64::MOVi32imm));
511 else if (Ty.getSizeInBits() <= 64)
512 I.setDesc(TII.get(AArch64::MOVi64imm));
513 else
514 return false;
515 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
516 }
517
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000518 case TargetOpcode::G_FRAME_INDEX: {
519 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000520 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000521 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000522 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000523 return false;
524 }
525
526 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000527
528 // MOs for a #0 shifted immediate.
529 I.addOperand(MachineOperand::CreateImm(0));
530 I.addOperand(MachineOperand::CreateImm(0));
531
532 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
533 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000534
535 case TargetOpcode::G_GLOBAL_VALUE: {
536 auto GV = I.getOperand(1).getGlobal();
537 if (GV->isThreadLocal()) {
538 // FIXME: we don't support TLS yet.
539 return false;
540 }
541 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
542 if (OpFlags & AArch64II::MO_GOT)
543 I.setDesc(TII.get(AArch64::LOADgot));
544 else {
545 I.setDesc(TII.get(AArch64::MOVaddr));
546 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
547 MachineInstrBuilder MIB(MF, I);
548 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
549 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
550 }
551 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
552 }
553
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000554 case TargetOpcode::G_LOAD:
555 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000556 LLT MemTy = Ty;
557 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000558
Tim Northover5ae83502016-09-15 09:20:34 +0000559 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000560 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000561 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000562 return false;
563 }
564
565#ifndef NDEBUG
566 // Sanity-check the pointer register.
567 const unsigned PtrReg = I.getOperand(1).getReg();
568 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
569 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
570 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000571 assert(MRI.getType(PtrReg).isPointer() &&
572 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000573#endif
574
575 const unsigned ValReg = I.getOperand(0).getReg();
576 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
577
578 const unsigned NewOpc =
579 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
580 if (NewOpc == I.getOpcode())
581 return false;
582
583 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000584
585 I.addOperand(MachineOperand::CreateImm(0));
586 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
587 }
588
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000589 case TargetOpcode::G_MUL: {
590 // Reject the various things we don't support yet.
591 if (unsupportedBinOp(I, RBI, MRI, TRI))
592 return false;
593
594 const unsigned DefReg = I.getOperand(0).getReg();
595 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
596
597 if (RB.getID() != AArch64::GPRRegBankID) {
598 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
599 return false;
600 }
601
602 unsigned ZeroReg;
603 unsigned NewOpc;
604 if (Ty == LLT::scalar(32)) {
605 NewOpc = AArch64::MADDWrrr;
606 ZeroReg = AArch64::WZR;
607 } else if (Ty == LLT::scalar(64)) {
608 NewOpc = AArch64::MADDXrrr;
609 ZeroReg = AArch64::XZR;
610 } else {
611 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
612 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
613 return false;
614 }
615
616 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000617
618 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
619
620 // Now that we selected an opcode, we need to constrain the register
621 // operands to use appropriate classes.
622 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
623 }
624
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000625 case TargetOpcode::G_FADD:
626 case TargetOpcode::G_FSUB:
627 case TargetOpcode::G_FMUL:
628 case TargetOpcode::G_FDIV:
629
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000630 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000631 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000632 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000633 case TargetOpcode::G_SHL:
634 case TargetOpcode::G_LSHR:
635 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000636 case TargetOpcode::G_SDIV:
637 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000638 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000639 case TargetOpcode::G_SUB:
640 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000641 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000642 if (unsupportedBinOp(I, RBI, MRI, TRI))
643 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000644
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000645 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000646
647 const unsigned DefReg = I.getOperand(0).getReg();
648 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
649
650 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
651 if (NewOpc == I.getOpcode())
652 return false;
653
654 I.setDesc(TII.get(NewOpc));
655 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000656
657 // Now that we selected an opcode, we need to constrain the register
658 // operands to use appropriate classes.
659 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
660 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000661
Tim Northoverfb8d9892016-10-12 22:49:15 +0000662 case TargetOpcode::G_TRUNC: {
663 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
664 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
665
666 const unsigned DstReg = I.getOperand(0).getReg();
667 const unsigned SrcReg = I.getOperand(1).getReg();
668
669 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
670 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
671
672 if (DstRB.getID() != SrcRB.getID()) {
673 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
674 return false;
675 }
676
677 if (DstRB.getID() == AArch64::GPRRegBankID) {
678 const TargetRegisterClass *DstRC =
679 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
680 if (!DstRC)
681 return false;
682
683 const TargetRegisterClass *SrcRC =
684 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
685 if (!SrcRC)
686 return false;
687
688 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
689 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
690 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
691 return false;
692 }
693
694 if (DstRC == SrcRC) {
695 // Nothing to be done
696 } else if (DstRC == &AArch64::GPR32RegClass &&
697 SrcRC == &AArch64::GPR64RegClass) {
698 I.getOperand(1).setSubReg(AArch64::sub_32);
699 } else {
700 return false;
701 }
702
703 I.setDesc(TII.get(TargetOpcode::COPY));
704 return true;
705 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
706 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
707 I.setDesc(TII.get(AArch64::XTNv4i16));
708 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
709 return true;
710 }
711 }
712
713 return false;
714 }
715
Tim Northover3d38b3a2016-10-11 20:50:21 +0000716 case TargetOpcode::G_ANYEXT: {
717 const unsigned DstReg = I.getOperand(0).getReg();
718 const unsigned SrcReg = I.getOperand(1).getReg();
719
Quentin Colombetcb629a82016-10-12 03:57:49 +0000720 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
721 if (RBDst.getID() != AArch64::GPRRegBankID) {
722 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
723 return false;
724 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000725
Quentin Colombetcb629a82016-10-12 03:57:49 +0000726 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
727 if (RBSrc.getID() != AArch64::GPRRegBankID) {
728 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000729 return false;
730 }
731
732 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
733
734 if (DstSize == 0) {
735 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
736 return false;
737 }
738
Quentin Colombetcb629a82016-10-12 03:57:49 +0000739 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000740 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
741 << ", expected: 32 or 64\n");
742 return false;
743 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000744 // At this point G_ANYEXT is just like a plain COPY, but we need
745 // to explicitly form the 64-bit value if any.
746 if (DstSize > 32) {
747 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
748 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
749 .addDef(ExtSrc)
750 .addImm(0)
751 .addUse(SrcReg)
752 .addImm(AArch64::sub_32);
753 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000754 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000755 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000756 }
757
758 case TargetOpcode::G_ZEXT:
759 case TargetOpcode::G_SEXT: {
760 unsigned Opcode = I.getOpcode();
761 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
762 SrcTy = MRI.getType(I.getOperand(1).getReg());
763 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
764 const unsigned DefReg = I.getOperand(0).getReg();
765 const unsigned SrcReg = I.getOperand(1).getReg();
766 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
767
768 if (RB.getID() != AArch64::GPRRegBankID) {
769 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
770 << ", expected: GPR\n");
771 return false;
772 }
773
774 MachineInstr *ExtI;
775 if (DstTy == LLT::scalar(64)) {
776 // FIXME: Can we avoid manually doing this?
777 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
778 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
779 << " operand\n");
780 return false;
781 }
782
783 const unsigned SrcXReg =
784 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
785 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
786 .addDef(SrcXReg)
787 .addImm(0)
788 .addUse(SrcReg)
789 .addImm(AArch64::sub_32);
790
791 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
792 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
793 .addDef(DefReg)
794 .addUse(SrcXReg)
795 .addImm(0)
796 .addImm(SrcTy.getSizeInBits() - 1);
797 } else if (DstTy == LLT::scalar(32)) {
798 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
799 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
800 .addDef(DefReg)
801 .addUse(SrcReg)
802 .addImm(0)
803 .addImm(SrcTy.getSizeInBits() - 1);
804 } else {
805 return false;
806 }
807
808 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
809
810 I.eraseFromParent();
811 return true;
812 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000813
Tim Northover69271c62016-10-12 22:49:11 +0000814 case TargetOpcode::G_SITOFP:
815 case TargetOpcode::G_UITOFP:
816 case TargetOpcode::G_FPTOSI:
817 case TargetOpcode::G_FPTOUI: {
818 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
819 SrcTy = MRI.getType(I.getOperand(1).getReg());
820 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
821 if (NewOpc == Opcode)
822 return false;
823
824 I.setDesc(TII.get(NewOpc));
825 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
826
827 return true;
828 }
829
830
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000831 case TargetOpcode::G_INTTOPTR:
832 case TargetOpcode::G_PTRTOINT:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000833 case TargetOpcode::G_BITCAST:
834 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000835
836 case TargetOpcode::G_ICMP: {
837 if (Ty != LLT::scalar(1)) {
838 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
839 << ", expected: " << LLT::scalar(1) << '\n');
840 return false;
841 }
842
843 unsigned CmpOpc = 0;
844 unsigned ZReg = 0;
845
846 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
847 if (CmpTy == LLT::scalar(32)) {
848 CmpOpc = AArch64::SUBSWrr;
849 ZReg = AArch64::WZR;
850 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
851 CmpOpc = AArch64::SUBSXrr;
852 ZReg = AArch64::XZR;
853 } else {
854 return false;
855 }
856
857 const AArch64CC::CondCode CC = changeICMPPredToAArch64CC(
858 (CmpInst::Predicate)I.getOperand(1).getPredicate());
859
860 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
861 .addDef(ZReg)
862 .addUse(I.getOperand(2).getReg())
863 .addUse(I.getOperand(3).getReg());
864
865 MachineInstr &CSetMI =
866 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
867 .addDef(I.getOperand(0).getReg())
868 .addUse(AArch64::WZR)
869 .addUse(AArch64::WZR)
870 .addImm(CC);
871
872 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
873 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
874
875 I.eraseFromParent();
876 return true;
877 }
878
Tim Northover7dd378d2016-10-12 22:49:07 +0000879 case TargetOpcode::G_FCMP: {
880 if (Ty != LLT::scalar(1)) {
881 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
882 << ", expected: " << LLT::scalar(1) << '\n');
883 return false;
884 }
885
886 unsigned CmpOpc = 0;
887 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
888 if (CmpTy == LLT::scalar(32)) {
889 CmpOpc = AArch64::FCMPSrr;
890 } else if (CmpTy == LLT::scalar(64)) {
891 CmpOpc = AArch64::FCMPDrr;
892 } else {
893 return false;
894 }
895
896 // FIXME: regbank
897
898 AArch64CC::CondCode CC1, CC2;
899 changeFCMPPredToAArch64CC(
900 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
901
902 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
903 .addUse(I.getOperand(2).getReg())
904 .addUse(I.getOperand(3).getReg());
905
906 const unsigned DefReg = I.getOperand(0).getReg();
907 unsigned Def1Reg = DefReg;
908 if (CC2 != AArch64CC::AL)
909 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
910
911 MachineInstr &CSetMI =
912 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
913 .addDef(Def1Reg)
914 .addUse(AArch64::WZR)
915 .addUse(AArch64::WZR)
916 .addImm(CC1);
917
918 if (CC2 != AArch64CC::AL) {
919 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
920 MachineInstr &CSet2MI =
921 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
922 .addDef(Def2Reg)
923 .addUse(AArch64::WZR)
924 .addUse(AArch64::WZR)
925 .addImm(CC2);
926 MachineInstr &OrMI =
927 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
928 .addDef(DefReg)
929 .addUse(Def1Reg)
930 .addUse(Def2Reg);
931 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
932 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
933 }
934
935 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
936 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
937
938 I.eraseFromParent();
939 return true;
940 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000941 }
942
943 return false;
944}