blob: 489f9ab2e937984b38b1a98059c88d46a5c22713 [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());
53 if (!Ty.isSized()) {
Ahmed Bougacha59e160a2016-08-16 14:37:40 +000054 DEBUG(dbgs() << "Generic binop should be sized\n");
55 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 Northover0f140c72016-09-09 11:46:34 +0000223 const LLT Ty = I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg())
224 : LLT::unsized();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000225 assert(Ty.isValid() && "Generic instruction doesn't have a type");
226
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 Northover11a23542016-08-31 21:24:02 +0000233 case TargetOpcode::G_TYPE: {
234 I.setDesc(TII.get(TargetOpcode::COPY));
Tim Northover11a23542016-08-31 21:24:02 +0000235 return true;
236 }
237
Tim Northover8d8812c2016-09-01 20:45:41 +0000238 case TargetOpcode::G_PHI: {
239 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover8d8812c2016-09-01 20:45:41 +0000240 return true;
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 Northover0f140c72016-09-09 11:46:34 +0000245 if (Ty != LLT::pointer(0)) {
246 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000247 << ", expected: " << LLT::pointer(0) << '\n');
248 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
264 if (PtrTy != LLT::pointer(0)) {
265 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
266 << ", expected: " << LLT::pointer(0) << '\n');
267 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:
344 case TargetOpcode::G_SUB: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000345 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000346 if (unsupportedBinOp(I, RBI, MRI, TRI))
347 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000348
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000349 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000350
351 const unsigned DefReg = I.getOperand(0).getReg();
352 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
353
354 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
355 if (NewOpc == I.getOpcode())
356 return false;
357
358 I.setDesc(TII.get(NewOpc));
359 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000360
361 // Now that we selected an opcode, we need to constrain the register
362 // operands to use appropriate classes.
363 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
364 }
365 }
366
367 return false;
368}