blob: d9469faddd6138bfca57b5b987a752bf244f1ae4 [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 Bougacha6756a2c2016-07-27 14:31:55 +0000116 default:
117 return GenericOpc;
118 }
119 case 64:
120 switch (GenericOpc) {
121 case TargetOpcode::G_OR:
122 return AArch64::ORRXrr;
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000123 case TargetOpcode::G_XOR:
124 return AArch64::EORXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000125 case TargetOpcode::G_AND:
126 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000127 case TargetOpcode::G_ADD:
128 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000129 case TargetOpcode::G_SUB:
130 return AArch64::SUBXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000131 case TargetOpcode::G_SHL:
132 return AArch64::LSLVXr;
133 case TargetOpcode::G_LSHR:
134 return AArch64::LSRVXr;
135 case TargetOpcode::G_ASHR:
136 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000137 default:
138 return GenericOpc;
139 }
140 }
141 };
142 return GenericOpc;
143}
144
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000145/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
146/// appropriate for the (value) register bank \p RegBankID and of memory access
147/// size \p OpSize. This returns the variant with the base+unsigned-immediate
148/// addressing mode (e.g., LDRXui).
149/// \returns \p GenericOpc if the combination is unsupported.
150static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
151 unsigned OpSize) {
152 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
153 switch (RegBankID) {
154 case AArch64::GPRRegBankID:
155 switch (OpSize) {
156 case 32:
157 return isStore ? AArch64::STRWui : AArch64::LDRWui;
158 case 64:
159 return isStore ? AArch64::STRXui : AArch64::LDRXui;
160 }
161 };
162 return GenericOpc;
163}
164
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000165bool AArch64InstructionSelector::select(MachineInstr &I) const {
166 assert(I.getParent() && "Instruction should be in a basic block!");
167 assert(I.getParent()->getParent() && "Instruction should be in a function!");
168
169 MachineBasicBlock &MBB = *I.getParent();
170 MachineFunction &MF = *MBB.getParent();
171 MachineRegisterInfo &MRI = MF.getRegInfo();
172
173 // FIXME: Is there *really* nothing to be done here? This assumes that
174 // no upstream pass introduces things like generic vreg on copies or
175 // target-specific instructions.
176 // We should document (and verify) that assumption.
177 if (!isPreISelGenericOpcode(I.getOpcode()))
178 return true;
179
180 if (I.getNumOperands() != I.getNumExplicitOperands()) {
181 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
182 return false;
183 }
184
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000185 const LLT Ty = I.getType();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000186 assert(Ty.isValid() && "Generic instruction doesn't have a type");
187
Ahmed Bougacha85505092016-07-28 17:15:15 +0000188 switch (I.getOpcode()) {
189 case TargetOpcode::G_BR: {
190 I.setDesc(TII.get(AArch64::B));
191 I.removeTypes();
192 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000193 }
194
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000195 case TargetOpcode::G_FRAME_INDEX: {
196 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
197 if (I.getType() != LLT::pointer(0)) {
198 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << I.getType()
199 << ", expected: " << LLT::pointer(0) << '\n');
200 return false;
201 }
202
203 I.setDesc(TII.get(AArch64::ADDXri));
204 I.removeTypes();
205
206 // MOs for a #0 shifted immediate.
207 I.addOperand(MachineOperand::CreateImm(0));
208 I.addOperand(MachineOperand::CreateImm(0));
209
210 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
211 }
212
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000213 case TargetOpcode::G_LOAD:
214 case TargetOpcode::G_STORE: {
215 LLT MemTy = I.getType(0);
216 LLT PtrTy = I.getType(1);
217
218 if (PtrTy != LLT::pointer(0)) {
219 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
220 << ", expected: " << LLT::pointer(0) << '\n');
221 return false;
222 }
223
224#ifndef NDEBUG
225 // Sanity-check the pointer register.
226 const unsigned PtrReg = I.getOperand(1).getReg();
227 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
228 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
229 "Load/Store pointer operand isn't a GPR");
230 assert(MRI.getSize(PtrReg) == 64 &&
231 "Load/Store pointer operand isn't 64-bit");
232#endif
233
234 const unsigned ValReg = I.getOperand(0).getReg();
235 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
236
237 const unsigned NewOpc =
238 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
239 if (NewOpc == I.getOpcode())
240 return false;
241
242 I.setDesc(TII.get(NewOpc));
243 I.removeTypes();
244
245 I.addOperand(MachineOperand::CreateImm(0));
246 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
247 }
248
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000249 case TargetOpcode::G_OR:
Ahmed Bougacha6db3cfe2016-07-29 16:56:25 +0000250 case TargetOpcode::G_XOR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000251 case TargetOpcode::G_AND:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000252 case TargetOpcode::G_SHL:
253 case TargetOpcode::G_LSHR:
254 case TargetOpcode::G_ASHR:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000255 case TargetOpcode::G_ADD:
256 case TargetOpcode::G_SUB: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000257 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000258 if (unsupportedBinOp(I, RBI, MRI, TRI))
259 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000260
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000261 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000262
263 const unsigned DefReg = I.getOperand(0).getReg();
264 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
265
266 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
267 if (NewOpc == I.getOpcode())
268 return false;
269
270 I.setDesc(TII.get(NewOpc));
271 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000272 I.removeTypes();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000273
274 // Now that we selected an opcode, we need to constrain the register
275 // operands to use appropriate classes.
276 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
277 }
278 }
279
280 return false;
281}