blob: 1f2be90210f5083f5ec15432b1907268fc1a3d70 [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 Northover4edc60d2016-10-10 21:49:42 +0000516 case TargetOpcode::G_CONSTANT: {
517 if (Ty.getSizeInBits() <= 32)
518 I.setDesc(TII.get(AArch64::MOVi32imm));
519 else if (Ty.getSizeInBits() <= 64)
520 I.setDesc(TII.get(AArch64::MOVi64imm));
521 else
522 return false;
523 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
524 }
525
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000526 case TargetOpcode::G_FRAME_INDEX: {
527 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000528 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000529 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000530 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000531 return false;
532 }
533
534 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000535
536 // MOs for a #0 shifted immediate.
537 I.addOperand(MachineOperand::CreateImm(0));
538 I.addOperand(MachineOperand::CreateImm(0));
539
540 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
541 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000542
543 case TargetOpcode::G_GLOBAL_VALUE: {
544 auto GV = I.getOperand(1).getGlobal();
545 if (GV->isThreadLocal()) {
546 // FIXME: we don't support TLS yet.
547 return false;
548 }
549 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
550 if (OpFlags & AArch64II::MO_GOT)
551 I.setDesc(TII.get(AArch64::LOADgot));
552 else {
553 I.setDesc(TII.get(AArch64::MOVaddr));
554 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
555 MachineInstrBuilder MIB(MF, I);
556 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
557 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
558 }
559 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
560 }
561
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000562 case TargetOpcode::G_LOAD:
563 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000564 LLT MemTy = Ty;
565 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000566
Tim Northover5ae83502016-09-15 09:20:34 +0000567 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000568 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000569 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000570 return false;
571 }
572
573#ifndef NDEBUG
574 // Sanity-check the pointer register.
575 const unsigned PtrReg = I.getOperand(1).getReg();
576 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
577 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
578 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000579 assert(MRI.getType(PtrReg).isPointer() &&
580 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000581#endif
582
583 const unsigned ValReg = I.getOperand(0).getReg();
584 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
585
586 const unsigned NewOpc =
587 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
588 if (NewOpc == I.getOpcode())
589 return false;
590
591 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000592
593 I.addOperand(MachineOperand::CreateImm(0));
594 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
595 }
596
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000597 case TargetOpcode::G_MUL: {
598 // Reject the various things we don't support yet.
599 if (unsupportedBinOp(I, RBI, MRI, TRI))
600 return false;
601
602 const unsigned DefReg = I.getOperand(0).getReg();
603 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
604
605 if (RB.getID() != AArch64::GPRRegBankID) {
606 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
607 return false;
608 }
609
610 unsigned ZeroReg;
611 unsigned NewOpc;
612 if (Ty == LLT::scalar(32)) {
613 NewOpc = AArch64::MADDWrrr;
614 ZeroReg = AArch64::WZR;
615 } else if (Ty == LLT::scalar(64)) {
616 NewOpc = AArch64::MADDXrrr;
617 ZeroReg = AArch64::XZR;
618 } else {
619 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
620 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
621 return false;
622 }
623
624 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000625
626 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
627
628 // Now that we selected an opcode, we need to constrain the register
629 // operands to use appropriate classes.
630 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
631 }
632
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000633 case TargetOpcode::G_FADD:
634 case TargetOpcode::G_FSUB:
635 case TargetOpcode::G_FMUL:
636 case TargetOpcode::G_FDIV:
637
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000638 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000639 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000640 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000641 case TargetOpcode::G_SHL:
642 case TargetOpcode::G_LSHR:
643 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000644 case TargetOpcode::G_SDIV:
645 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000646 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000647 case TargetOpcode::G_SUB:
648 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000649 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000650 if (unsupportedBinOp(I, RBI, MRI, TRI))
651 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000652
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000653 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000654
655 const unsigned DefReg = I.getOperand(0).getReg();
656 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
657
658 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
659 if (NewOpc == I.getOpcode())
660 return false;
661
662 I.setDesc(TII.get(NewOpc));
663 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000664
665 // Now that we selected an opcode, we need to constrain the register
666 // operands to use appropriate classes.
667 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
668 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000669
Tim Northoverfb8d9892016-10-12 22:49:15 +0000670 case TargetOpcode::G_TRUNC: {
671 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
672 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
673
674 const unsigned DstReg = I.getOperand(0).getReg();
675 const unsigned SrcReg = I.getOperand(1).getReg();
676
677 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
678 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
679
680 if (DstRB.getID() != SrcRB.getID()) {
681 DEBUG(dbgs() << "G_TRUNC input/output on different banks\n");
682 return false;
683 }
684
685 if (DstRB.getID() == AArch64::GPRRegBankID) {
686 const TargetRegisterClass *DstRC =
687 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
688 if (!DstRC)
689 return false;
690
691 const TargetRegisterClass *SrcRC =
692 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
693 if (!SrcRC)
694 return false;
695
696 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
697 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
698 DEBUG(dbgs() << "Failed to constrain G_TRUNC\n");
699 return false;
700 }
701
702 if (DstRC == SrcRC) {
703 // Nothing to be done
704 } else if (DstRC == &AArch64::GPR32RegClass &&
705 SrcRC == &AArch64::GPR64RegClass) {
706 I.getOperand(1).setSubReg(AArch64::sub_32);
707 } else {
708 return false;
709 }
710
711 I.setDesc(TII.get(TargetOpcode::COPY));
712 return true;
713 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
714 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
715 I.setDesc(TII.get(AArch64::XTNv4i16));
716 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
717 return true;
718 }
719 }
720
721 return false;
722 }
723
Tim Northover3d38b3a2016-10-11 20:50:21 +0000724 case TargetOpcode::G_ANYEXT: {
725 const unsigned DstReg = I.getOperand(0).getReg();
726 const unsigned SrcReg = I.getOperand(1).getReg();
727
Quentin Colombetcb629a82016-10-12 03:57:49 +0000728 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
729 if (RBDst.getID() != AArch64::GPRRegBankID) {
730 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
731 return false;
732 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000733
Quentin Colombetcb629a82016-10-12 03:57:49 +0000734 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
735 if (RBSrc.getID() != AArch64::GPRRegBankID) {
736 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +0000737 return false;
738 }
739
740 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
741
742 if (DstSize == 0) {
743 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
744 return false;
745 }
746
Quentin Colombetcb629a82016-10-12 03:57:49 +0000747 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +0000748 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
749 << ", expected: 32 or 64\n");
750 return false;
751 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000752 // At this point G_ANYEXT is just like a plain COPY, but we need
753 // to explicitly form the 64-bit value if any.
754 if (DstSize > 32) {
755 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
756 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
757 .addDef(ExtSrc)
758 .addImm(0)
759 .addUse(SrcReg)
760 .addImm(AArch64::sub_32);
761 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000762 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000763 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +0000764 }
765
766 case TargetOpcode::G_ZEXT:
767 case TargetOpcode::G_SEXT: {
768 unsigned Opcode = I.getOpcode();
769 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
770 SrcTy = MRI.getType(I.getOperand(1).getReg());
771 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
772 const unsigned DefReg = I.getOperand(0).getReg();
773 const unsigned SrcReg = I.getOperand(1).getReg();
774 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
775
776 if (RB.getID() != AArch64::GPRRegBankID) {
777 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
778 << ", expected: GPR\n");
779 return false;
780 }
781
782 MachineInstr *ExtI;
783 if (DstTy == LLT::scalar(64)) {
784 // FIXME: Can we avoid manually doing this?
785 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
786 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
787 << " operand\n");
788 return false;
789 }
790
791 const unsigned SrcXReg =
792 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
793 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
794 .addDef(SrcXReg)
795 .addImm(0)
796 .addUse(SrcReg)
797 .addImm(AArch64::sub_32);
798
799 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
800 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
801 .addDef(DefReg)
802 .addUse(SrcXReg)
803 .addImm(0)
804 .addImm(SrcTy.getSizeInBits() - 1);
805 } else if (DstTy == LLT::scalar(32)) {
806 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
807 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
808 .addDef(DefReg)
809 .addUse(SrcReg)
810 .addImm(0)
811 .addImm(SrcTy.getSizeInBits() - 1);
812 } else {
813 return false;
814 }
815
816 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
817
818 I.eraseFromParent();
819 return true;
820 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000821
Tim Northover69271c62016-10-12 22:49:11 +0000822 case TargetOpcode::G_SITOFP:
823 case TargetOpcode::G_UITOFP:
824 case TargetOpcode::G_FPTOSI:
825 case TargetOpcode::G_FPTOUI: {
826 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
827 SrcTy = MRI.getType(I.getOperand(1).getReg());
828 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
829 if (NewOpc == Opcode)
830 return false;
831
832 I.setDesc(TII.get(NewOpc));
833 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
834
835 return true;
836 }
837
838
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000839 case TargetOpcode::G_INTTOPTR:
840 case TargetOpcode::G_PTRTOINT:
Quentin Colombet9de30fa2016-10-12 03:57:52 +0000841 case TargetOpcode::G_BITCAST:
842 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover6c02ad52016-10-12 22:49:04 +0000843
844 case TargetOpcode::G_ICMP: {
845 if (Ty != LLT::scalar(1)) {
846 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
847 << ", expected: " << LLT::scalar(1) << '\n');
848 return false;
849 }
850
851 unsigned CmpOpc = 0;
852 unsigned ZReg = 0;
853
854 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
855 if (CmpTy == LLT::scalar(32)) {
856 CmpOpc = AArch64::SUBSWrr;
857 ZReg = AArch64::WZR;
858 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
859 CmpOpc = AArch64::SUBSXrr;
860 ZReg = AArch64::XZR;
861 } else {
862 return false;
863 }
864
865 const AArch64CC::CondCode CC = changeICMPPredToAArch64CC(
866 (CmpInst::Predicate)I.getOperand(1).getPredicate());
867
868 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
869 .addDef(ZReg)
870 .addUse(I.getOperand(2).getReg())
871 .addUse(I.getOperand(3).getReg());
872
873 MachineInstr &CSetMI =
874 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
875 .addDef(I.getOperand(0).getReg())
876 .addUse(AArch64::WZR)
877 .addUse(AArch64::WZR)
878 .addImm(CC);
879
880 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
881 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
882
883 I.eraseFromParent();
884 return true;
885 }
886
Tim Northover7dd378d2016-10-12 22:49:07 +0000887 case TargetOpcode::G_FCMP: {
888 if (Ty != LLT::scalar(1)) {
889 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
890 << ", expected: " << LLT::scalar(1) << '\n');
891 return false;
892 }
893
894 unsigned CmpOpc = 0;
895 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
896 if (CmpTy == LLT::scalar(32)) {
897 CmpOpc = AArch64::FCMPSrr;
898 } else if (CmpTy == LLT::scalar(64)) {
899 CmpOpc = AArch64::FCMPDrr;
900 } else {
901 return false;
902 }
903
904 // FIXME: regbank
905
906 AArch64CC::CondCode CC1, CC2;
907 changeFCMPPredToAArch64CC(
908 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
909
910 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
911 .addUse(I.getOperand(2).getReg())
912 .addUse(I.getOperand(3).getReg());
913
914 const unsigned DefReg = I.getOperand(0).getReg();
915 unsigned Def1Reg = DefReg;
916 if (CC2 != AArch64CC::AL)
917 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
918
919 MachineInstr &CSetMI =
920 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
921 .addDef(Def1Reg)
922 .addUse(AArch64::WZR)
923 .addUse(AArch64::WZR)
924 .addImm(CC1);
925
926 if (CC2 != AArch64CC::AL) {
927 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
928 MachineInstr &CSet2MI =
929 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
930 .addDef(Def2Reg)
931 .addUse(AArch64::WZR)
932 .addUse(AArch64::WZR)
933 .addImm(CC2);
934 MachineInstr &OrMI =
935 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
936 .addDef(DefReg)
937 .addUse(Def1Reg)
938 .addUse(Def2Reg);
939 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
940 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
941 }
942
943 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
944 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
945
946 I.eraseFromParent();
947 return true;
948 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000949 }
950
951 return false;
952}