blob: efb2dbc64a945fb96c35caf8bd9cadad5ffe735a [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:
133 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000134 case TargetOpcode::G_SUB:
135 return AArch64::SUBXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000136 case TargetOpcode::G_SHL:
137 return AArch64::LSLVXr;
138 case TargetOpcode::G_LSHR:
139 return AArch64::LSRVXr;
140 case TargetOpcode::G_ASHR:
141 return AArch64::ASRVXr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000142 case TargetOpcode::G_SDIV:
143 return AArch64::SDIVXr;
144 case TargetOpcode::G_UDIV:
145 return AArch64::UDIVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000146 default:
147 return GenericOpc;
148 }
149 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000150 case AArch64::FPRRegBankID:
151 switch (OpSize) {
152 case 32:
153 switch (GenericOpc) {
154 case TargetOpcode::G_FADD:
155 return AArch64::FADDSrr;
156 case TargetOpcode::G_FSUB:
157 return AArch64::FSUBSrr;
158 case TargetOpcode::G_FMUL:
159 return AArch64::FMULSrr;
160 case TargetOpcode::G_FDIV:
161 return AArch64::FDIVSrr;
162 default:
163 return GenericOpc;
164 }
165 case 64:
166 switch (GenericOpc) {
167 case TargetOpcode::G_FADD:
168 return AArch64::FADDDrr;
169 case TargetOpcode::G_FSUB:
170 return AArch64::FSUBDrr;
171 case TargetOpcode::G_FMUL:
172 return AArch64::FMULDrr;
173 case TargetOpcode::G_FDIV:
174 return AArch64::FDIVDrr;
175 default:
176 return GenericOpc;
177 }
178 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000179 };
180 return GenericOpc;
181}
182
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000183/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
184/// appropriate for the (value) register bank \p RegBankID and of memory access
185/// size \p OpSize. This returns the variant with the base+unsigned-immediate
186/// addressing mode (e.g., LDRXui).
187/// \returns \p GenericOpc if the combination is unsupported.
188static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
189 unsigned OpSize) {
190 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
191 switch (RegBankID) {
192 case AArch64::GPRRegBankID:
193 switch (OpSize) {
194 case 32:
195 return isStore ? AArch64::STRWui : AArch64::LDRWui;
196 case 64:
197 return isStore ? AArch64::STRXui : AArch64::LDRXui;
198 }
199 };
200 return GenericOpc;
201}
202
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000203bool AArch64InstructionSelector::select(MachineInstr &I) const {
204 assert(I.getParent() && "Instruction should be in a basic block!");
205 assert(I.getParent()->getParent() && "Instruction should be in a function!");
206
207 MachineBasicBlock &MBB = *I.getParent();
208 MachineFunction &MF = *MBB.getParent();
209 MachineRegisterInfo &MRI = MF.getRegInfo();
210
211 // FIXME: Is there *really* nothing to be done here? This assumes that
212 // no upstream pass introduces things like generic vreg on copies or
213 // target-specific instructions.
214 // We should document (and verify) that assumption.
215 if (!isPreISelGenericOpcode(I.getOpcode()))
216 return true;
217
218 if (I.getNumOperands() != I.getNumExplicitOperands()) {
219 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
220 return false;
221 }
222
Tim Northover32a078a2016-09-15 10:09:59 +0000223 LLT Ty =
224 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000225
Ahmed Bougacha85505092016-07-28 17:15:15 +0000226 switch (I.getOpcode()) {
227 case TargetOpcode::G_BR: {
228 I.setDesc(TII.get(AArch64::B));
Ahmed Bougacha85505092016-07-28 17:15:15 +0000229 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000230 }
231
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000232 case TargetOpcode::G_FRAME_INDEX: {
233 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000234 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000235 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000236 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000237 return false;
238 }
239
240 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000241
242 // MOs for a #0 shifted immediate.
243 I.addOperand(MachineOperand::CreateImm(0));
244 I.addOperand(MachineOperand::CreateImm(0));
245
246 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
247 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000248 case TargetOpcode::G_LOAD:
249 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000250 LLT MemTy = Ty;
251 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000252
Tim Northover5ae83502016-09-15 09:20:34 +0000253 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000254 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000255 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000256 return false;
257 }
258
259#ifndef NDEBUG
260 // Sanity-check the pointer register.
261 const unsigned PtrReg = I.getOperand(1).getReg();
262 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
263 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
264 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000265 assert(MRI.getType(PtrReg).isPointer() &&
266 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000267#endif
268
269 const unsigned ValReg = I.getOperand(0).getReg();
270 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
271
272 const unsigned NewOpc =
273 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
274 if (NewOpc == I.getOpcode())
275 return false;
276
277 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000278
279 I.addOperand(MachineOperand::CreateImm(0));
280 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
281 }
282
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000283 case TargetOpcode::G_MUL: {
284 // Reject the various things we don't support yet.
285 if (unsupportedBinOp(I, RBI, MRI, TRI))
286 return false;
287
288 const unsigned DefReg = I.getOperand(0).getReg();
289 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
290
291 if (RB.getID() != AArch64::GPRRegBankID) {
292 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
293 return false;
294 }
295
296 unsigned ZeroReg;
297 unsigned NewOpc;
298 if (Ty == LLT::scalar(32)) {
299 NewOpc = AArch64::MADDWrrr;
300 ZeroReg = AArch64::WZR;
301 } else if (Ty == LLT::scalar(64)) {
302 NewOpc = AArch64::MADDXrrr;
303 ZeroReg = AArch64::XZR;
304 } else {
305 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
306 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
307 return false;
308 }
309
310 I.setDesc(TII.get(NewOpc));
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000311
312 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
313
314 // Now that we selected an opcode, we need to constrain the register
315 // operands to use appropriate classes.
316 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
317 }
318
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000319 case TargetOpcode::G_FADD:
320 case TargetOpcode::G_FSUB:
321 case TargetOpcode::G_FMUL:
322 case TargetOpcode::G_FDIV:
323
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000324 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000325 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000326 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000327 case TargetOpcode::G_SHL:
328 case TargetOpcode::G_LSHR:
329 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000330 case TargetOpcode::G_SDIV:
331 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000332 case TargetOpcode::G_ADD:
333 case TargetOpcode::G_SUB: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000334 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000335 if (unsupportedBinOp(I, RBI, MRI, TRI))
336 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000337
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000338 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000339
340 const unsigned DefReg = I.getOperand(0).getReg();
341 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
342
343 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
344 if (NewOpc == I.getOpcode())
345 return false;
346
347 I.setDesc(TII.get(NewOpc));
348 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000349
350 // Now that we selected an opcode, we need to constrain the register
351 // operands to use appropriate classes.
352 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
353 }
354 }
355
356 return false;
357}