blob: ebfd78ba2b2fe563b2a2e841e6a27db194e7fc9b [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) {
Tim Northover020d1042016-10-17 18:36:53 +0000225 case 8:
226 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
227 case 16:
228 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000229 case 32:
230 return isStore ? AArch64::STRWui : AArch64::LDRWui;
231 case 64:
232 return isStore ? AArch64::STRXui : AArch64::LDRXui;
233 }
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000234 case AArch64::FPRRegBankID:
235 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000236 case 8:
237 return isStore ? AArch64::STRBui : AArch64::LDRBui;
238 case 16:
239 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000240 case 32:
241 return isStore ? AArch64::STRSui : AArch64::LDRSui;
242 case 64:
243 return isStore ? AArch64::STRDui : AArch64::LDRDui;
244 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000245 };
246 return GenericOpc;
247}
248
Quentin Colombetcb629a82016-10-12 03:57:49 +0000249static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
250 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
251 const RegisterBankInfo &RBI) {
252
253 unsigned DstReg = I.getOperand(0).getReg();
254 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
255 assert(I.isCopy() && "Generic operators do not allow physical registers");
256 return true;
257 }
258
259 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
260 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
261 unsigned SrcReg = I.getOperand(1).getReg();
262 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
263 (void)SrcSize;
264 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
265 "No phys reg on generic operators");
266 assert(
267 (DstSize == SrcSize ||
268 // Copies are a mean to setup initial types, the number of
269 // bits may not exactly match.
270 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
271 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
272 // Copies are a mean to copy bits around, as long as we are
273 // on the same register class, that's fine. Otherwise, that
274 // means we need some SUBREG_TO_REG or AND & co.
275 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
276 "Copy with different width?!");
277 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
278 "GPRs cannot get more than 64-bit width values");
279 const TargetRegisterClass *RC = nullptr;
280
281 if (RegBank.getID() == AArch64::FPRRegBankID) {
282 if (DstSize <= 32)
283 RC = &AArch64::FPR32RegClass;
284 else if (DstSize <= 64)
285 RC = &AArch64::FPR64RegClass;
286 else if (DstSize <= 128)
287 RC = &AArch64::FPR128RegClass;
288 else {
289 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
290 return false;
291 }
292 } else {
293 assert(RegBank.getID() == AArch64::GPRRegBankID &&
294 "Bitcast for the flags?");
295 RC =
296 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
297 }
298
299 // No need to constrain SrcReg. It will get constrained when
300 // we hit another of its use or its defs.
301 // Copies do not have constraints.
302 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
303 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
304 << " operand\n");
305 return false;
306 }
307 I.setDesc(TII.get(AArch64::COPY));
308 return true;
309}
310
Tim Northover69271c62016-10-12 22:49:11 +0000311static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
312 if (!DstTy.isScalar() || !SrcTy.isScalar())
313 return GenericOpc;
314
315 const unsigned DstSize = DstTy.getSizeInBits();
316 const unsigned SrcSize = SrcTy.getSizeInBits();
317
318 switch (DstSize) {
319 case 32:
320 switch (SrcSize) {
321 case 32:
322 switch (GenericOpc) {
323 case TargetOpcode::G_SITOFP:
324 return AArch64::SCVTFUWSri;
325 case TargetOpcode::G_UITOFP:
326 return AArch64::UCVTFUWSri;
327 case TargetOpcode::G_FPTOSI:
328 return AArch64::FCVTZSUWSr;
329 case TargetOpcode::G_FPTOUI:
330 return AArch64::FCVTZUUWSr;
331 default:
332 return GenericOpc;
333 }
334 case 64:
335 switch (GenericOpc) {
336 case TargetOpcode::G_SITOFP:
337 return AArch64::SCVTFUXSri;
338 case TargetOpcode::G_UITOFP:
339 return AArch64::UCVTFUXSri;
340 case TargetOpcode::G_FPTOSI:
341 return AArch64::FCVTZSUWDr;
342 case TargetOpcode::G_FPTOUI:
343 return AArch64::FCVTZUUWDr;
344 default:
345 return GenericOpc;
346 }
347 default:
348 return GenericOpc;
349 }
350 case 64:
351 switch (SrcSize) {
352 case 32:
353 switch (GenericOpc) {
354 case TargetOpcode::G_SITOFP:
355 return AArch64::SCVTFUWDri;
356 case TargetOpcode::G_UITOFP:
357 return AArch64::UCVTFUWDri;
358 case TargetOpcode::G_FPTOSI:
359 return AArch64::FCVTZSUXSr;
360 case TargetOpcode::G_FPTOUI:
361 return AArch64::FCVTZUUXSr;
362 default:
363 return GenericOpc;
364 }
365 case 64:
366 switch (GenericOpc) {
367 case TargetOpcode::G_SITOFP:
368 return AArch64::SCVTFUXDri;
369 case TargetOpcode::G_UITOFP:
370 return AArch64::UCVTFUXDri;
371 case TargetOpcode::G_FPTOSI:
372 return AArch64::FCVTZSUXDr;
373 case TargetOpcode::G_FPTOUI:
374 return AArch64::FCVTZUUXDr;
375 default:
376 return GenericOpc;
377 }
378 default:
379 return GenericOpc;
380 }
381 default:
382 return GenericOpc;
383 };
384 return GenericOpc;
385}
386
Tim Northover6c02ad52016-10-12 22:49:04 +0000387static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
388 switch (P) {
389 default:
390 llvm_unreachable("Unknown condition code!");
391 case CmpInst::ICMP_NE:
392 return AArch64CC::NE;
393 case CmpInst::ICMP_EQ:
394 return AArch64CC::EQ;
395 case CmpInst::ICMP_SGT:
396 return AArch64CC::GT;
397 case CmpInst::ICMP_SGE:
398 return AArch64CC::GE;
399 case CmpInst::ICMP_SLT:
400 return AArch64CC::LT;
401 case CmpInst::ICMP_SLE:
402 return AArch64CC::LE;
403 case CmpInst::ICMP_UGT:
404 return AArch64CC::HI;
405 case CmpInst::ICMP_UGE:
406 return AArch64CC::HS;
407 case CmpInst::ICMP_ULT:
408 return AArch64CC::LO;
409 case CmpInst::ICMP_ULE:
410 return AArch64CC::LS;
411 }
412}
413
Tim Northover7dd378d2016-10-12 22:49:07 +0000414static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
415 AArch64CC::CondCode &CondCode,
416 AArch64CC::CondCode &CondCode2) {
417 CondCode2 = AArch64CC::AL;
418 switch (P) {
419 default:
420 llvm_unreachable("Unknown FP condition!");
421 case CmpInst::FCMP_OEQ:
422 CondCode = AArch64CC::EQ;
423 break;
424 case CmpInst::FCMP_OGT:
425 CondCode = AArch64CC::GT;
426 break;
427 case CmpInst::FCMP_OGE:
428 CondCode = AArch64CC::GE;
429 break;
430 case CmpInst::FCMP_OLT:
431 CondCode = AArch64CC::MI;
432 break;
433 case CmpInst::FCMP_OLE:
434 CondCode = AArch64CC::LS;
435 break;
436 case CmpInst::FCMP_ONE:
437 CondCode = AArch64CC::MI;
438 CondCode2 = AArch64CC::GT;
439 break;
440 case CmpInst::FCMP_ORD:
441 CondCode = AArch64CC::VC;
442 break;
443 case CmpInst::FCMP_UNO:
444 CondCode = AArch64CC::VS;
445 break;
446 case CmpInst::FCMP_UEQ:
447 CondCode = AArch64CC::EQ;
448 CondCode2 = AArch64CC::VS;
449 break;
450 case CmpInst::FCMP_UGT:
451 CondCode = AArch64CC::HI;
452 break;
453 case CmpInst::FCMP_UGE:
454 CondCode = AArch64CC::PL;
455 break;
456 case CmpInst::FCMP_ULT:
457 CondCode = AArch64CC::LT;
458 break;
459 case CmpInst::FCMP_ULE:
460 CondCode = AArch64CC::LE;
461 break;
462 case CmpInst::FCMP_UNE:
463 CondCode = AArch64CC::NE;
464 break;
465 }
466}
467
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000468bool AArch64InstructionSelector::select(MachineInstr &I) const {
469 assert(I.getParent() && "Instruction should be in a basic block!");
470 assert(I.getParent()->getParent() && "Instruction should be in a function!");
471
472 MachineBasicBlock &MBB = *I.getParent();
473 MachineFunction &MF = *MBB.getParent();
474 MachineRegisterInfo &MRI = MF.getRegInfo();
475
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000476 if (!isPreISelGenericOpcode(I.getOpcode()))
Quentin Colombetcb629a82016-10-12 03:57:49 +0000477 return !I.isCopy() || selectCopy(I, TII, MRI, TRI, RBI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000478
479 if (I.getNumOperands() != I.getNumExplicitOperands()) {
480 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
481 return false;
482 }
483
Tim Northover69271c62016-10-12 22:49:11 +0000484 unsigned Opcode = I.getOpcode();
Tim Northover32a078a2016-09-15 10:09:59 +0000485 LLT Ty =
486 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000487
Tim Northover69271c62016-10-12 22:49:11 +0000488 switch (Opcode) {
Ahmed Bougacha85505092016-07-28 17:15:15 +0000489 case TargetOpcode::G_BR: {
490 I.setDesc(TII.get(AArch64::B));
Ahmed Bougacha85505092016-07-28 17:15:15 +0000491 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000492 }
493
Tim Northover5e3dbf32016-10-12 22:49:01 +0000494 case TargetOpcode::G_BRCOND: {
495 if (Ty.getSizeInBits() > 32) {
496 // We shouldn't need this on AArch64, but it would be implemented as an
497 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
498 // bit being tested is < 32.
499 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
500 << ", expected at most 32-bits");
501 return false;
502 }
503
504 const unsigned CondReg = I.getOperand(0).getReg();
505 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
506
507 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
508 .addUse(CondReg)
509 .addImm(/*bit offset=*/0)
510 .addMBB(DestMBB);
511
512 I.eraseFromParent();
513 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
514 }
515
Tim Northover4494d692016-10-18 19:47:57 +0000516 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000517 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000518 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
519
520 const LLT s32 = LLT::scalar(32);
521 const LLT s64 = LLT::scalar(64);
522 const LLT p0 = LLT::pointer(0, 64);
523
524 const unsigned DefReg = I.getOperand(0).getReg();
525 const LLT DefTy = MRI.getType(DefReg);
526 const unsigned DefSize = DefTy.getSizeInBits();
527 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
528
529 // FIXME: Redundant check, but even less readable when factored out.
530 if (isFP) {
531 if (Ty != s32 && Ty != s64) {
532 DEBUG(dbgs() << "Unable to materialize FP " << Ty
533 << " constant, expected: " << s32 << " or " << s64
534 << '\n');
535 return false;
536 }
537
538 if (RB.getID() != AArch64::FPRRegBankID) {
539 DEBUG(dbgs() << "Unable to materialize FP " << Ty
540 << " constant on bank: " << RB << ", expected: FPR\n");
541 return false;
542 }
543 } else {
544 if (Ty != s32 && Ty != s64 && Ty != p0) {
545 DEBUG(dbgs() << "Unable to materialize integer " << Ty
546 << " constant, expected: " << s32 << ", " << s64 << ", or "
547 << p0 << '\n');
548 return false;
549 }
550
551 if (RB.getID() != AArch64::GPRRegBankID) {
552 DEBUG(dbgs() << "Unable to materialize integer " << Ty
553 << " constant on bank: " << RB << ", expected: GPR\n");
554 return false;
555 }
556 }
557
558 const unsigned MovOpc =
559 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
560
561 I.setDesc(TII.get(MovOpc));
562
563 if (isFP) {
564 const TargetRegisterClass &GPRRC =
565 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
566 const TargetRegisterClass &FPRRC =
567 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
568
569 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
570 MachineOperand &RegOp = I.getOperand(0);
571 RegOp.setReg(DefGPRReg);
572
573 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
574 TII.get(AArch64::COPY))
575 .addDef(DefReg)
576 .addUse(DefGPRReg);
577
578 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
579 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
580 return false;
581 }
582
583 MachineOperand &ImmOp = I.getOperand(1);
584 // FIXME: Is going through int64_t always correct?
585 ImmOp.ChangeToImmediate(
586 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
587 }
588
589 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
590 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000591 }
592
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000593 case TargetOpcode::G_FRAME_INDEX: {
594 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000595 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000596 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000597 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000598 return false;
599 }
600
601 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000602
603 // MOs for a #0 shifted immediate.
604 I.addOperand(MachineOperand::CreateImm(0));
605 I.addOperand(MachineOperand::CreateImm(0));
606
607 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
608 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000609
610 case TargetOpcode::G_GLOBAL_VALUE: {
611 auto GV = I.getOperand(1).getGlobal();
612 if (GV->isThreadLocal()) {
613 // FIXME: we don't support TLS yet.
614 return false;
615 }
616 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
617 if (OpFlags & AArch64II::MO_GOT)
618 I.setDesc(TII.get(AArch64::LOADgot));
619 else {
620 I.setDesc(TII.get(AArch64::MOVaddr));
621 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
622 MachineInstrBuilder MIB(MF, I);
623 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
624 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
625 }
626 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
627 }
628
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000629 case TargetOpcode::G_LOAD:
630 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000631 LLT MemTy = Ty;
632 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000633
Tim Northover5ae83502016-09-15 09:20:34 +0000634 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000635 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000636 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000637 return false;
638 }
639
640#ifndef NDEBUG
641 // Sanity-check the pointer register.
642 const unsigned PtrReg = I.getOperand(1).getReg();
643 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
644 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
645 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000646 assert(MRI.getType(PtrReg).isPointer() &&
647 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000648#endif
649
650 const unsigned ValReg = I.getOperand(0).getReg();
651 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
652
653 const unsigned NewOpc =
654 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
655 if (NewOpc == I.getOpcode())
656 return false;
657
658 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000659
660 I.addOperand(MachineOperand::CreateImm(0));
661 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
662 }
663
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000664 case TargetOpcode::G_MUL: {
665 // Reject the various things we don't support yet.
666 if (unsupportedBinOp(I, RBI, MRI, TRI))
667 return false;
668
669 const unsigned DefReg = I.getOperand(0).getReg();
670 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
671
672 if (RB.getID() != AArch64::GPRRegBankID) {
673 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
674 return false;
675 }
676
677 unsigned ZeroReg;
678 unsigned NewOpc;
679 if (Ty == LLT::scalar(32)) {
680 NewOpc = AArch64::MADDWrrr;
681 ZeroReg = AArch64::WZR;
682 } else if (Ty == LLT::scalar(64)) {
683 NewOpc = AArch64::MADDXrrr;
684 ZeroReg = AArch64::XZR;
685 } else {
686 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
687 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
688 return false;
689 }
690
691 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000692
693 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
694
695 // Now that we selected an opcode, we need to constrain the register
696 // operands to use appropriate classes.
697 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
698 }
699
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000700 case TargetOpcode::G_FADD:
701 case TargetOpcode::G_FSUB:
702 case TargetOpcode::G_FMUL:
703 case TargetOpcode::G_FDIV:
704
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000705 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000706 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000707 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000708 case TargetOpcode::G_SHL:
709 case TargetOpcode::G_LSHR:
710 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000711 case TargetOpcode::G_SDIV:
712 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000713 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000714 case TargetOpcode::G_SUB:
715 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000716 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000717 if (unsupportedBinOp(I, RBI, MRI, TRI))
718 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000719
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000720 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000721
722 const unsigned DefReg = I.getOperand(0).getReg();
723 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
724
725 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
726 if (NewOpc == I.getOpcode())
727 return false;
728
729 I.setDesc(TII.get(NewOpc));
730 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000731
732 // Now that we selected an opcode, we need to constrain the register
733 // operands to use appropriate classes.
734 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
735 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000736
Tim Northoverfb8d9892016-10-12 22:49:15 +0000737 case TargetOpcode::G_TRUNC: {
738 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
739 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
740
741 const unsigned DstReg = I.getOperand(0).getReg();
742 const unsigned SrcReg = I.getOperand(1).getReg();
743
744 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
745 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
746
747 if (DstRB.getID() != SrcRB.getID()) {
748 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
749 return false;
750 }
751
752 if (DstRB.getID() == AArch64::GPRRegBankID) {
753 const TargetRegisterClass *DstRC =
754 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
755 if (!DstRC)
756 return false;
757
758 const TargetRegisterClass *SrcRC =
759 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
760 if (!SrcRC)
761 return false;
762
763 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
764 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
765 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
766 return false;
767 }
768
769 if (DstRC == SrcRC) {
770 // Nothing to be done
771 } else if (DstRC == &AArch64::GPR32RegClass &&
772 SrcRC == &AArch64::GPR64RegClass) {
773 I.getOperand(1).setSubReg(AArch64::sub_32);
774 } else {
775 return false;
776 }
777
778 I.setDesc(TII.get(TargetOpcode::COPY));
779 return true;
780 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
781 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
782 I.setDesc(TII.get(AArch64::XTNv4i16));
783 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
784 return true;
785 }
786 }
787
788 return false;
789 }
790
Tim Northover3d38b3a2016-10-11 20:50:21 +0000791 case TargetOpcode::G_ANYEXT: {
792 const unsigned DstReg = I.getOperand(0).getReg();
793 const unsigned SrcReg = I.getOperand(1).getReg();
794
Quentin Colombetcb629a82016-10-12 03:57:49 +0000795 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
796 if (RBDst.getID() != AArch64::GPRRegBankID) {
797 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
798 return false;
799 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000800
Quentin Colombetcb629a82016-10-12 03:57:49 +0000801 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
802 if (RBSrc.getID() != AArch64::GPRRegBankID) {
803 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000804 return false;
805 }
806
807 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
808
809 if (DstSize == 0) {
810 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
811 return false;
812 }
813
Quentin Colombetcb629a82016-10-12 03:57:49 +0000814 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000815 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
816 << ", expected: 32 or 64\n");
817 return false;
818 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000819 // At this point G_ANYEXT is just like a plain COPY, but we need
820 // to explicitly form the 64-bit value if any.
821 if (DstSize > 32) {
822 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
823 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
824 .addDef(ExtSrc)
825 .addImm(0)
826 .addUse(SrcReg)
827 .addImm(AArch64::sub_32);
828 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000829 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000830 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000831 }
832
833 case TargetOpcode::G_ZEXT:
834 case TargetOpcode::G_SEXT: {
835 unsigned Opcode = I.getOpcode();
836 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
837 SrcTy = MRI.getType(I.getOperand(1).getReg());
838 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
839 const unsigned DefReg = I.getOperand(0).getReg();
840 const unsigned SrcReg = I.getOperand(1).getReg();
841 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
842
843 if (RB.getID() != AArch64::GPRRegBankID) {
844 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
845 << ", expected: GPR\n");
846 return false;
847 }
848
849 MachineInstr *ExtI;
850 if (DstTy == LLT::scalar(64)) {
851 // FIXME: Can we avoid manually doing this?
852 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
853 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
854 << " operand\n");
855 return false;
856 }
857
858 const unsigned SrcXReg =
859 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
860 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
861 .addDef(SrcXReg)
862 .addImm(0)
863 .addUse(SrcReg)
864 .addImm(AArch64::sub_32);
865
866 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
867 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
868 .addDef(DefReg)
869 .addUse(SrcXReg)
870 .addImm(0)
871 .addImm(SrcTy.getSizeInBits() - 1);
872 } else if (DstTy == LLT::scalar(32)) {
873 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
874 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
875 .addDef(DefReg)
876 .addUse(SrcReg)
877 .addImm(0)
878 .addImm(SrcTy.getSizeInBits() - 1);
879 } else {
880 return false;
881 }
882
883 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
884
885 I.eraseFromParent();
886 return true;
887 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000888
Tim Northover69271c62016-10-12 22:49:11 +0000889 case TargetOpcode::G_SITOFP:
890 case TargetOpcode::G_UITOFP:
891 case TargetOpcode::G_FPTOSI:
892 case TargetOpcode::G_FPTOUI: {
893 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
894 SrcTy = MRI.getType(I.getOperand(1).getReg());
895 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
896 if (NewOpc == Opcode)
897 return false;
898
899 I.setDesc(TII.get(NewOpc));
900 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
901
902 return true;
903 }
904
905
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000906 case TargetOpcode::G_INTTOPTR:
907 case TargetOpcode::G_PTRTOINT:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000908 case TargetOpcode::G_BITCAST:
909 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000910
911 case TargetOpcode::G_ICMP: {
912 if (Ty != LLT::scalar(1)) {
913 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
914 << ", expected: " << LLT::scalar(1) << '\n');
915 return false;
916 }
917
918 unsigned CmpOpc = 0;
919 unsigned ZReg = 0;
920
921 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
922 if (CmpTy == LLT::scalar(32)) {
923 CmpOpc = AArch64::SUBSWrr;
924 ZReg = AArch64::WZR;
925 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
926 CmpOpc = AArch64::SUBSXrr;
927 ZReg = AArch64::XZR;
928 } else {
929 return false;
930 }
931
932 const AArch64CC::CondCode CC = changeICMPPredToAArch64CC(
933 (CmpInst::Predicate)I.getOperand(1).getPredicate());
934
935 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
936 .addDef(ZReg)
937 .addUse(I.getOperand(2).getReg())
938 .addUse(I.getOperand(3).getReg());
939
940 MachineInstr &CSetMI =
941 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
942 .addDef(I.getOperand(0).getReg())
943 .addUse(AArch64::WZR)
944 .addUse(AArch64::WZR)
945 .addImm(CC);
946
947 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
948 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
949
950 I.eraseFromParent();
951 return true;
952 }
953
Tim Northover7dd378d2016-10-12 22:49:07 +0000954 case TargetOpcode::G_FCMP: {
955 if (Ty != LLT::scalar(1)) {
956 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
957 << ", expected: " << LLT::scalar(1) << '\n');
958 return false;
959 }
960
961 unsigned CmpOpc = 0;
962 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
963 if (CmpTy == LLT::scalar(32)) {
964 CmpOpc = AArch64::FCMPSrr;
965 } else if (CmpTy == LLT::scalar(64)) {
966 CmpOpc = AArch64::FCMPDrr;
967 } else {
968 return false;
969 }
970
971 // FIXME: regbank
972
973 AArch64CC::CondCode CC1, CC2;
974 changeFCMPPredToAArch64CC(
975 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
976
977 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
978 .addUse(I.getOperand(2).getReg())
979 .addUse(I.getOperand(3).getReg());
980
981 const unsigned DefReg = I.getOperand(0).getReg();
982 unsigned Def1Reg = DefReg;
983 if (CC2 != AArch64CC::AL)
984 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
985
986 MachineInstr &CSetMI =
987 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
988 .addDef(Def1Reg)
989 .addUse(AArch64::WZR)
990 .addUse(AArch64::WZR)
991 .addImm(CC1);
992
993 if (CC2 != AArch64CC::AL) {
994 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
995 MachineInstr &CSet2MI =
996 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
997 .addDef(Def2Reg)
998 .addUse(AArch64::WZR)
999 .addUse(AArch64::WZR)
1000 .addImm(CC2);
1001 MachineInstr &OrMI =
1002 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1003 .addDef(DefReg)
1004 .addUse(Def1Reg)
1005 .addUse(Def2Reg);
1006 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1007 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1008 }
1009
1010 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1011 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1012
1013 I.eraseFromParent();
1014 return true;
1015 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001016 }
1017
1018 return false;
1019}