blob: 628e17086843048a9b1316139df9918a6c3f0ea7 [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"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/IR/Type.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28
29#define DEBUG_TYPE "aarch64-isel"
30
31using namespace llvm;
32
33#ifndef LLVM_BUILD_GLOBAL_ISEL
34#error "You shouldn't build this"
35#endif
36
37AArch64InstructionSelector::AArch64InstructionSelector(
38 const AArch64Subtarget &STI, const AArch64RegisterBankInfo &RBI)
39 : InstructionSelector(), TII(*STI.getInstrInfo()),
40 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
41
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000042/// Check whether \p I is a currently unsupported binary operation:
43/// - it has an unsized type
44/// - an operand is not a vreg
45/// - all operands are not in the same bank
46/// These are checks that should someday live in the verifier, but right now,
47/// these are mostly limitations of the aarch64 selector.
48static bool unsupportedBinOp(const MachineInstr &I,
49 const AArch64RegisterBankInfo &RBI,
50 const MachineRegisterInfo &MRI,
51 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +000052 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +000053 if (!Ty.isValid()) {
54 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000055 return true;
56 }
57
58 const RegisterBank *PrevOpBank = nullptr;
59 for (auto &MO : I.operands()) {
60 // FIXME: Support non-register operands.
61 if (!MO.isReg()) {
62 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
63 return true;
64 }
65
66 // FIXME: Can generic operations have physical registers operands? If
67 // so, this will need to be taught about that, and we'll need to get the
68 // bank out of the minimal class for the register.
69 // Either way, this needs to be documented (and possibly verified).
70 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
71 DEBUG(dbgs() << "Generic inst has physical register operand\n");
72 return true;
73 }
74
75 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
76 if (!OpBank) {
77 DEBUG(dbgs() << "Generic register has no bank or class\n");
78 return true;
79 }
80
81 if (PrevOpBank && OpBank != PrevOpBank) {
82 DEBUG(dbgs() << "Generic inst operands have different banks\n");
83 return true;
84 }
85 PrevOpBank = OpBank;
86 }
87 return false;
88}
89
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000090/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
91/// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID
92/// and of size \p OpSize.
93/// \returns \p GenericOpc if the combination is unsupported.
94static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
95 unsigned OpSize) {
96 switch (RegBankID) {
97 case AArch64::GPRRegBankID:
98 switch (OpSize) {
99 case 32:
100 switch (GenericOpc) {
101 case TargetOpcode::G_OR:
102 return AArch64::ORRWrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000103 case TargetOpcode::G_XOR:
104 return AArch64::EORWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000105 case TargetOpcode::G_AND:
106 return AArch64::ANDWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000107 case TargetOpcode::G_ADD:
108 return AArch64::ADDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000109 case TargetOpcode::G_SUB:
110 return AArch64::SUBWrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000111 case TargetOpcode::G_SHL:
112 return AArch64::LSLVWr;
113 case TargetOpcode::G_LSHR:
114 return AArch64::LSRVWr;
115 case TargetOpcode::G_ASHR:
116 return AArch64::ASRVWr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000117 case TargetOpcode::G_SDIV:
118 return AArch64::SDIVWr;
119 case TargetOpcode::G_UDIV:
120 return AArch64::UDIVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000121 default:
122 return GenericOpc;
123 }
124 case 64:
125 switch (GenericOpc) {
126 case TargetOpcode::G_OR:
127 return AArch64::ORRXrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000128 case TargetOpcode::G_XOR:
129 return AArch64::EORXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000130 case TargetOpcode::G_AND:
131 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000132 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000133 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000134 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000135 case TargetOpcode::G_SUB:
136 return AArch64::SUBXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000137 case TargetOpcode::G_SHL:
138 return AArch64::LSLVXr;
139 case TargetOpcode::G_LSHR:
140 return AArch64::LSRVXr;
141 case TargetOpcode::G_ASHR:
142 return AArch64::ASRVXr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000143 case TargetOpcode::G_SDIV:
144 return AArch64::SDIVXr;
145 case TargetOpcode::G_UDIV:
146 return AArch64::UDIVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000147 default:
148 return GenericOpc;
149 }
150 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000151 case AArch64::FPRRegBankID:
152 switch (OpSize) {
153 case 32:
154 switch (GenericOpc) {
155 case TargetOpcode::G_FADD:
156 return AArch64::FADDSrr;
157 case TargetOpcode::G_FSUB:
158 return AArch64::FSUBSrr;
159 case TargetOpcode::G_FMUL:
160 return AArch64::FMULSrr;
161 case TargetOpcode::G_FDIV:
162 return AArch64::FDIVSrr;
163 default:
164 return GenericOpc;
165 }
166 case 64:
167 switch (GenericOpc) {
168 case TargetOpcode::G_FADD:
169 return AArch64::FADDDrr;
170 case TargetOpcode::G_FSUB:
171 return AArch64::FSUBDrr;
172 case TargetOpcode::G_FMUL:
173 return AArch64::FMULDrr;
174 case TargetOpcode::G_FDIV:
175 return AArch64::FDIVDrr;
176 default:
177 return GenericOpc;
178 }
179 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000180 };
181 return GenericOpc;
182}
183
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000184/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
185/// appropriate for the (value) register bank \p RegBankID and of memory access
186/// size \p OpSize. This returns the variant with the base+unsigned-immediate
187/// addressing mode (e.g., LDRXui).
188/// \returns \p GenericOpc if the combination is unsupported.
189static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
190 unsigned OpSize) {
191 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
192 switch (RegBankID) {
193 case AArch64::GPRRegBankID:
194 switch (OpSize) {
195 case 32:
196 return isStore ? AArch64::STRWui : AArch64::LDRWui;
197 case 64:
198 return isStore ? AArch64::STRXui : AArch64::LDRXui;
199 }
200 };
201 return GenericOpc;
202}
203
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000204bool AArch64InstructionSelector::select(MachineInstr &I) const {
205 assert(I.getParent() && "Instruction should be in a basic block!");
206 assert(I.getParent()->getParent() && "Instruction should be in a function!");
207
208 MachineBasicBlock &MBB = *I.getParent();
209 MachineFunction &MF = *MBB.getParent();
210 MachineRegisterInfo &MRI = MF.getRegInfo();
211
212 // FIXME: Is there *really* nothing to be done here? This assumes that
213 // no upstream pass introduces things like generic vreg on copies or
214 // target-specific instructions.
215 // We should document (and verify) that assumption.
216 if (!isPreISelGenericOpcode(I.getOpcode()))
217 return true;
218
219 if (I.getNumOperands() != I.getNumExplicitOperands()) {
220 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
221 return false;
222 }
223
Tim Northover32a078a2016-09-15 10:09:59 +0000224 LLT Ty =
225 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000226
Ahmed Bougacha85505092016-07-28 17:15:15 +0000227 switch (I.getOpcode()) {
228 case TargetOpcode::G_BR: {
229 I.setDesc(TII.get(AArch64::B));
Ahmed Bougacha85505092016-07-28 17:15:15 +0000230 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000231 }
232
Tim Northover4edc60d2016-10-10 21:49:42 +0000233 case TargetOpcode::G_CONSTANT: {
234 if (Ty.getSizeInBits() <= 32)
235 I.setDesc(TII.get(AArch64::MOVi32imm));
236 else if (Ty.getSizeInBits() <= 64)
237 I.setDesc(TII.get(AArch64::MOVi64imm));
238 else
239 return false;
240 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
241 }
242
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000243 case TargetOpcode::G_FRAME_INDEX: {
244 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000245 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000246 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000247 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000248 return false;
249 }
250
251 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000252
253 // MOs for a #0 shifted immediate.
254 I.addOperand(MachineOperand::CreateImm(0));
255 I.addOperand(MachineOperand::CreateImm(0));
256
257 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
258 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000259 case TargetOpcode::G_LOAD:
260 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000261 LLT MemTy = Ty;
262 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000263
Tim Northover5ae83502016-09-15 09:20:34 +0000264 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000265 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000266 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000267 return false;
268 }
269
270#ifndef NDEBUG
271 // Sanity-check the pointer register.
272 const unsigned PtrReg = I.getOperand(1).getReg();
273 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
274 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
275 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000276 assert(MRI.getType(PtrReg).isPointer() &&
277 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000278#endif
279
280 const unsigned ValReg = I.getOperand(0).getReg();
281 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
282
283 const unsigned NewOpc =
284 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
285 if (NewOpc == I.getOpcode())
286 return false;
287
288 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000289
290 I.addOperand(MachineOperand::CreateImm(0));
291 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
292 }
293
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000294 case TargetOpcode::G_MUL: {
295 // Reject the various things we don't support yet.
296 if (unsupportedBinOp(I, RBI, MRI, TRI))
297 return false;
298
299 const unsigned DefReg = I.getOperand(0).getReg();
300 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
301
302 if (RB.getID() != AArch64::GPRRegBankID) {
303 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
304 return false;
305 }
306
307 unsigned ZeroReg;
308 unsigned NewOpc;
309 if (Ty == LLT::scalar(32)) {
310 NewOpc = AArch64::MADDWrrr;
311 ZeroReg = AArch64::WZR;
312 } else if (Ty == LLT::scalar(64)) {
313 NewOpc = AArch64::MADDXrrr;
314 ZeroReg = AArch64::XZR;
315 } else {
316 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
317 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
318 return false;
319 }
320
321 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000322
323 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
324
325 // Now that we selected an opcode, we need to constrain the register
326 // operands to use appropriate classes.
327 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
328 }
329
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000330 case TargetOpcode::G_FADD:
331 case TargetOpcode::G_FSUB:
332 case TargetOpcode::G_FMUL:
333 case TargetOpcode::G_FDIV:
334
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000335 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000336 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000337 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000338 case TargetOpcode::G_SHL:
339 case TargetOpcode::G_LSHR:
340 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000341 case TargetOpcode::G_SDIV:
342 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000343 case TargetOpcode::G_ADD:
Tim Northover2fda4b02016-10-10 21:49:49 +0000344 case TargetOpcode::G_SUB:
345 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000346 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000347 if (unsupportedBinOp(I, RBI, MRI, TRI))
348 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000349
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000350 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000351
352 const unsigned DefReg = I.getOperand(0).getReg();
353 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
354
355 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
356 if (NewOpc == I.getOpcode())
357 return false;
358
359 I.setDesc(TII.get(NewOpc));
360 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000361
362 // Now that we selected an opcode, we need to constrain the register
363 // operands to use appropriate classes.
364 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
365 }
366 }
367
368 return false;
369}