blob: a5f1bafbc21217e7af184fe10f35891473ec1124 [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) {
52 if (!I.getType().isSized()) {
53 DEBUG(dbgs() << "Generic binop should be sized\n");
54 return true;
55 }
56
57 const RegisterBank *PrevOpBank = nullptr;
58 for (auto &MO : I.operands()) {
59 // FIXME: Support non-register operands.
60 if (!MO.isReg()) {
61 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
62 return true;
63 }
64
65 // FIXME: Can generic operations have physical registers operands? If
66 // so, this will need to be taught about that, and we'll need to get the
67 // bank out of the minimal class for the register.
68 // Either way, this needs to be documented (and possibly verified).
69 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
70 DEBUG(dbgs() << "Generic inst has physical register operand\n");
71 return true;
72 }
73
74 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
75 if (!OpBank) {
76 DEBUG(dbgs() << "Generic register has no bank or class\n");
77 return true;
78 }
79
80 if (PrevOpBank && OpBank != PrevOpBank) {
81 DEBUG(dbgs() << "Generic inst operands have different banks\n");
82 return true;
83 }
84 PrevOpBank = OpBank;
85 }
86 return false;
87}
88
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000089/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
90/// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID
91/// and of size \p OpSize.
92/// \returns \p GenericOpc if the combination is unsupported.
93static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
94 unsigned OpSize) {
95 switch (RegBankID) {
96 case AArch64::GPRRegBankID:
97 switch (OpSize) {
98 case 32:
99 switch (GenericOpc) {
100 case TargetOpcode::G_OR:
101 return AArch64::ORRWrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000102 case TargetOpcode::G_XOR:
103 return AArch64::EORWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000104 case TargetOpcode::G_AND:
105 return AArch64::ANDWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000106 case TargetOpcode::G_ADD:
107 return AArch64::ADDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000108 case TargetOpcode::G_SUB:
109 return AArch64::SUBWrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000110 case TargetOpcode::G_SHL:
111 return AArch64::LSLVWr;
112 case TargetOpcode::G_LSHR:
113 return AArch64::LSRVWr;
114 case TargetOpcode::G_ASHR:
115 return AArch64::ASRVWr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000116 case TargetOpcode::G_SDIV:
117 return AArch64::SDIVWr;
118 case TargetOpcode::G_UDIV:
119 return AArch64::UDIVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000120 default:
121 return GenericOpc;
122 }
123 case 64:
124 switch (GenericOpc) {
125 case TargetOpcode::G_OR:
126 return AArch64::ORRXrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000127 case TargetOpcode::G_XOR:
128 return AArch64::EORXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000129 case TargetOpcode::G_AND:
130 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000131 case TargetOpcode::G_ADD:
132 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000133 case TargetOpcode::G_SUB:
134 return AArch64::SUBXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000135 case TargetOpcode::G_SHL:
136 return AArch64::LSLVXr;
137 case TargetOpcode::G_LSHR:
138 return AArch64::LSRVXr;
139 case TargetOpcode::G_ASHR:
140 return AArch64::ASRVXr;
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000141 case TargetOpcode::G_SDIV:
142 return AArch64::SDIVXr;
143 case TargetOpcode::G_UDIV:
144 return AArch64::UDIVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000145 default:
146 return GenericOpc;
147 }
148 }
149 };
150 return GenericOpc;
151}
152
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000153/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
154/// appropriate for the (value) register bank \p RegBankID and of memory access
155/// size \p OpSize. This returns the variant with the base+unsigned-immediate
156/// addressing mode (e.g., LDRXui).
157/// \returns \p GenericOpc if the combination is unsupported.
158static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
159 unsigned OpSize) {
160 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
161 switch (RegBankID) {
162 case AArch64::GPRRegBankID:
163 switch (OpSize) {
164 case 32:
165 return isStore ? AArch64::STRWui : AArch64::LDRWui;
166 case 64:
167 return isStore ? AArch64::STRXui : AArch64::LDRXui;
168 }
169 };
170 return GenericOpc;
171}
172
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000173bool AArch64InstructionSelector::select(MachineInstr &I) const {
174 assert(I.getParent() && "Instruction should be in a basic block!");
175 assert(I.getParent()->getParent() && "Instruction should be in a function!");
176
177 MachineBasicBlock &MBB = *I.getParent();
178 MachineFunction &MF = *MBB.getParent();
179 MachineRegisterInfo &MRI = MF.getRegInfo();
180
181 // FIXME: Is there *really* nothing to be done here? This assumes that
182 // no upstream pass introduces things like generic vreg on copies or
183 // target-specific instructions.
184 // We should document (and verify) that assumption.
185 if (!isPreISelGenericOpcode(I.getOpcode()))
186 return true;
187
188 if (I.getNumOperands() != I.getNumExplicitOperands()) {
189 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
190 return false;
191 }
192
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000193 const LLT Ty = I.getType();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000194 assert(Ty.isValid() && "Generic instruction doesn't have a type");
195
Ahmed Bougacha85505092016-07-28 17:15:15 +0000196 switch (I.getOpcode()) {
197 case TargetOpcode::G_BR: {
198 I.setDesc(TII.get(AArch64::B));
199 I.removeTypes();
200 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000201 }
202
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000203 case TargetOpcode::G_FRAME_INDEX: {
204 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
205 if (I.getType() != LLT::pointer(0)) {
206 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << I.getType()
207 << ", expected: " << LLT::pointer(0) << '\n');
208 return false;
209 }
210
211 I.setDesc(TII.get(AArch64::ADDXri));
212 I.removeTypes();
213
214 // MOs for a #0 shifted immediate.
215 I.addOperand(MachineOperand::CreateImm(0));
216 I.addOperand(MachineOperand::CreateImm(0));
217
218 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
219 }
220
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000221 case TargetOpcode::G_LOAD:
222 case TargetOpcode::G_STORE: {
223 LLT MemTy = I.getType(0);
224 LLT PtrTy = I.getType(1);
225
226 if (PtrTy != LLT::pointer(0)) {
227 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
228 << ", expected: " << LLT::pointer(0) << '\n');
229 return false;
230 }
231
232#ifndef NDEBUG
233 // Sanity-check the pointer register.
234 const unsigned PtrReg = I.getOperand(1).getReg();
235 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
236 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
237 "Load/Store pointer operand isn't a GPR");
238 assert(MRI.getSize(PtrReg) == 64 &&
239 "Load/Store pointer operand isn't 64-bit");
240#endif
241
242 const unsigned ValReg = I.getOperand(0).getReg();
243 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
244
245 const unsigned NewOpc =
246 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
247 if (NewOpc == I.getOpcode())
248 return false;
249
250 I.setDesc(TII.get(NewOpc));
251 I.removeTypes();
252
253 I.addOperand(MachineOperand::CreateImm(0));
254 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
255 }
256
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000257 case TargetOpcode::G_MUL: {
258 // Reject the various things we don't support yet.
259 if (unsupportedBinOp(I, RBI, MRI, TRI))
260 return false;
261
262 const unsigned DefReg = I.getOperand(0).getReg();
263 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
264
265 if (RB.getID() != AArch64::GPRRegBankID) {
266 DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n");
267 return false;
268 }
269
270 unsigned ZeroReg;
271 unsigned NewOpc;
272 if (Ty == LLT::scalar(32)) {
273 NewOpc = AArch64::MADDWrrr;
274 ZeroReg = AArch64::WZR;
275 } else if (Ty == LLT::scalar(64)) {
276 NewOpc = AArch64::MADDXrrr;
277 ZeroReg = AArch64::XZR;
278 } else {
279 DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: "
280 << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n');
281 return false;
282 }
283
284 I.setDesc(TII.get(NewOpc));
285 I.removeTypes();
286
287 I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false));
288
289 // Now that we selected an opcode, we need to constrain the register
290 // operands to use appropriate classes.
291 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
292 }
293
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000294 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000295 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000296 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000297 case TargetOpcode::G_SHL:
298 case TargetOpcode::G_LSHR:
299 case TargetOpcode::G_ASHR:
Ahmed Bougacha1d0560b2016-08-18 15:17:13 +0000300 case TargetOpcode::G_SDIV:
301 case TargetOpcode::G_UDIV:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000302 case TargetOpcode::G_ADD:
303 case TargetOpcode::G_SUB: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000304 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000305 if (unsupportedBinOp(I, RBI, MRI, TRI))
306 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000307
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000308 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000309
310 const unsigned DefReg = I.getOperand(0).getReg();
311 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
312
313 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
314 if (NewOpc == I.getOpcode())
315 return false;
316
317 I.setDesc(TII.get(NewOpc));
318 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000319 I.removeTypes();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000320
321 // Now that we selected an opcode, we need to constrain the register
322 // operands to use appropriate classes.
323 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
324 }
325 }
326
327 return false;
328}