blob: 04e74b01d8030df46ff12d3011da9bafe42cb69a [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 Northoverc1d8c2b2016-10-11 22:29:23 +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
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000241bool AArch64InstructionSelector::select(MachineInstr &I) const {
242 assert(I.getParent() && "Instruction should be in a basic block!");
243 assert(I.getParent()->getParent() && "Instruction should be in a function!");
244
245 MachineBasicBlock &MBB = *I.getParent();
246 MachineFunction &MF = *MBB.getParent();
247 MachineRegisterInfo &MRI = MF.getRegInfo();
248
249 // FIXME: Is there *really* nothing to be done here? This assumes that
250 // no upstream pass introduces things like generic vreg on copies or
251 // target-specific instructions.
252 // We should document (and verify) that assumption.
253 if (!isPreISelGenericOpcode(I.getOpcode()))
254 return true;
255
256 if (I.getNumOperands() != I.getNumExplicitOperands()) {
257 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
258 return false;
259 }
260
Tim Northover32a078a2016-09-15 10:09:59 +0000261 LLT Ty =
262 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000263
Ahmed Bougacha85505092016-07-28 17:15:15 +0000264 switch (I.getOpcode()) {
265 case TargetOpcode::G_BR: {
266 I.setDesc(TII.get(AArch64::B));
Ahmed Bougacha85505092016-07-28 17:15:15 +0000267 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000268 }
269
Tim Northover4edc60d2016-10-10 21:49:42 +0000270 case TargetOpcode::G_CONSTANT: {
271 if (Ty.getSizeInBits() <= 32)
272 I.setDesc(TII.get(AArch64::MOVi32imm));
273 else if (Ty.getSizeInBits() <= 64)
274 I.setDesc(TII.get(AArch64::MOVi64imm));
275 else
276 return false;
277 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
278 }
279
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000280 case TargetOpcode::G_FRAME_INDEX: {
281 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000282 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000283 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000284 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000285 return false;
286 }
287
288 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000289
290 // MOs for a #0 shifted immediate.
291 I.addOperand(MachineOperand::CreateImm(0));
292 I.addOperand(MachineOperand::CreateImm(0));
293
294 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
295 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000296
297 case TargetOpcode::G_GLOBAL_VALUE: {
298 auto GV = I.getOperand(1).getGlobal();
299 if (GV->isThreadLocal()) {
300 // FIXME: we don't support TLS yet.
301 return false;
302 }
303 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
304 if (OpFlags & AArch64II::MO_GOT)
305 I.setDesc(TII.get(AArch64::LOADgot));
306 else {
307 I.setDesc(TII.get(AArch64::MOVaddr));
308 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
309 MachineInstrBuilder MIB(MF, I);
310 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
311 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
312 }
313 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
314 }
315
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000316 case TargetOpcode::G_LOAD:
317 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000318 LLT MemTy = Ty;
319 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000320
Tim Northover5ae83502016-09-15 09:20:34 +0000321 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000322 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000323 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000324 return false;
325 }
326
327#ifndef NDEBUG
328 // Sanity-check the pointer register.
329 const unsigned PtrReg = I.getOperand(1).getReg();
330 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
331 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
332 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000333 assert(MRI.getType(PtrReg).isPointer() &&
334 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000335#endif
336
337 const unsigned ValReg = I.getOperand(0).getReg();
338 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
339
340 const unsigned NewOpc =
341 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
342 if (NewOpc == I.getOpcode())
343 return false;
344
345 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000346
347 I.addOperand(MachineOperand::CreateImm(0));
348 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
349 }
350
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000351 case TargetOpcode::G_MUL: {
352 // Reject the various things we don't support yet.
353 if (unsupportedBinOp(I, RBI, MRI, TRI))
354 return false;
355
356 const unsigned DefReg = I.getOperand(0).getReg();
357 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
358
359 if (RB.getID() != AArch64::GPRRegBankID) {
360 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
361 return false;
362 }
363
364 unsigned ZeroReg;
365 unsigned NewOpc;
366 if (Ty == LLT::scalar(32)) {
367 NewOpc = AArch64::MADDWrrr;
368 ZeroReg = AArch64::WZR;
369 } else if (Ty == LLT::scalar(64)) {
370 NewOpc = AArch64::MADDXrrr;
371 ZeroReg = AArch64::XZR;
372 } else {
373 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
374 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
375 return false;
376 }
377
378 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000379
380 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
381
382 // Now that we selected an opcode, we need to constrain the register
383 // operands to use appropriate classes.
384 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
385 }
386
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000387 case TargetOpcode::G_FADD:
388 case TargetOpcode::G_FSUB:
389 case TargetOpcode::G_FMUL:
390 case TargetOpcode::G_FDIV:
391
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000392 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000393 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000394 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000395 case TargetOpcode::G_SHL:
396 case TargetOpcode::G_LSHR:
397 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000398 case TargetOpcode::G_SDIV:
399 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000400 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000401 case TargetOpcode::G_SUB:
402 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000403 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000404 if (unsupportedBinOp(I, RBI, MRI, TRI))
405 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000406
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000407 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000408
409 const unsigned DefReg = I.getOperand(0).getReg();
410 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
411
412 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
413 if (NewOpc == I.getOpcode())
414 return false;
415
416 I.setDesc(TII.get(NewOpc));
417 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000418
419 // Now that we selected an opcode, we need to constrain the register
420 // operands to use appropriate classes.
421 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
422 }
Tim Northover3d38b3a2016-10-11 20:50:21 +0000423
424 case TargetOpcode::G_ANYEXT: {
425 const unsigned DstReg = I.getOperand(0).getReg();
426 const unsigned SrcReg = I.getOperand(1).getReg();
427
428 const RegisterBank &RB = *RBI.getRegBank(DstReg, MRI, TRI);
429
430 if (RB.getID() != AArch64::GPRRegBankID) {
431 DEBUG(dbgs() << "G_ANYEXT on bank: " << RB << ", expected: GPR\n");
432 return false;
433 }
434
435 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
436
437 if (DstSize == 0) {
438 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
439 return false;
440 }
441
442 const TargetRegisterClass *RC = nullptr;
443 if (DstSize <= 32) {
444 RC = &AArch64::GPR32RegClass;
445 } else if (DstSize == 64) {
446 RC = &AArch64::GPR64RegClass;
447 } else {
448 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
449 << ", expected: 32 or 64\n");
450 return false;
451 }
452
453 if (!RBI.constrainGenericRegister(SrcReg, *RC, MRI) ||
454 !RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
455 DEBUG(dbgs() << "Failed to constrain G_ANYEXT\n");
456 return false;
457 }
458
459 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::COPY))
460 .addDef(DstReg)
461 .addUse(SrcReg);
462
463 I.eraseFromParent();
464 return true;
465 }
466
467 case TargetOpcode::G_ZEXT:
468 case TargetOpcode::G_SEXT: {
469 unsigned Opcode = I.getOpcode();
470 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
471 SrcTy = MRI.getType(I.getOperand(1).getReg());
472 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
473 const unsigned DefReg = I.getOperand(0).getReg();
474 const unsigned SrcReg = I.getOperand(1).getReg();
475 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
476
477 if (RB.getID() != AArch64::GPRRegBankID) {
478 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
479 << ", expected: GPR\n");
480 return false;
481 }
482
483 MachineInstr *ExtI;
484 if (DstTy == LLT::scalar(64)) {
485 // FIXME: Can we avoid manually doing this?
486 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
487 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
488 << " operand\n");
489 return false;
490 }
491
492 const unsigned SrcXReg =
493 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
494 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
495 .addDef(SrcXReg)
496 .addImm(0)
497 .addUse(SrcReg)
498 .addImm(AArch64::sub_32);
499
500 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
501 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
502 .addDef(DefReg)
503 .addUse(SrcXReg)
504 .addImm(0)
505 .addImm(SrcTy.getSizeInBits() - 1);
506 } else if (DstTy == LLT::scalar(32)) {
507 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
508 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
509 .addDef(DefReg)
510 .addUse(SrcReg)
511 .addImm(0)
512 .addImm(SrcTy.getSizeInBits() - 1);
513 } else {
514 return false;
515 }
516
517 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
518
519 I.eraseFromParent();
520 return true;
521 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000522
523 case TargetOpcode::G_INTTOPTR:
524 case TargetOpcode::G_PTRTOINT:
525 case TargetOpcode::G_BITCAST: {
526 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
527 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
528
529 const unsigned DstReg = I.getOperand(0).getReg();
530 const unsigned SrcReg = I.getOperand(1).getReg();
531
532 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
533 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
534
535 const TargetRegisterClass *DstRC =
536 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
537 if (!DstRC)
538 return false;
539
540 const TargetRegisterClass *SrcRC =
541 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
542 if (!SrcRC)
543 return false;
544
545 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
546 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
547 DEBUG(dbgs() << "Failed to constrain G_BITCAST\n");
548 return false;
549 }
550
551 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::COPY))
552 .addDef(DstReg)
553 .addUse(SrcReg);
554
555 I.eraseFromParent();
556 return true;
557 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000558 }
559
560 return false;
561}