blob: 9c48d32897ff5bf620f138d3376aa5c69b70acdb [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
42/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
43/// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID
44/// and of size \p OpSize.
45/// \returns \p GenericOpc if the combination is unsupported.
46static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
47 unsigned OpSize) {
48 switch (RegBankID) {
49 case AArch64::GPRRegBankID:
50 switch (OpSize) {
51 case 32:
52 switch (GenericOpc) {
53 case TargetOpcode::G_OR:
54 return AArch64::ORRWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +000055 case TargetOpcode::G_AND:
56 return AArch64::ANDWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000057 case TargetOpcode::G_ADD:
58 return AArch64::ADDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +000059 case TargetOpcode::G_SUB:
60 return AArch64::SUBWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000061 default:
62 return GenericOpc;
63 }
64 case 64:
65 switch (GenericOpc) {
66 case TargetOpcode::G_OR:
67 return AArch64::ORRXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +000068 case TargetOpcode::G_AND:
69 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000070 case TargetOpcode::G_ADD:
71 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +000072 case TargetOpcode::G_SUB:
73 return AArch64::SUBXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000074 default:
75 return GenericOpc;
76 }
77 }
78 };
79 return GenericOpc;
80}
81
Ahmed Bougacha7adfac52016-07-29 16:56:16 +000082/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
83/// appropriate for the (value) register bank \p RegBankID and of memory access
84/// size \p OpSize. This returns the variant with the base+unsigned-immediate
85/// addressing mode (e.g., LDRXui).
86/// \returns \p GenericOpc if the combination is unsupported.
87static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
88 unsigned OpSize) {
89 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
90 switch (RegBankID) {
91 case AArch64::GPRRegBankID:
92 switch (OpSize) {
93 case 32:
94 return isStore ? AArch64::STRWui : AArch64::LDRWui;
95 case 64:
96 return isStore ? AArch64::STRXui : AArch64::LDRXui;
97 }
98 };
99 return GenericOpc;
100}
101
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000102bool AArch64InstructionSelector::select(MachineInstr &I) const {
103 assert(I.getParent() && "Instruction should be in a basic block!");
104 assert(I.getParent()->getParent() && "Instruction should be in a function!");
105
106 MachineBasicBlock &MBB = *I.getParent();
107 MachineFunction &MF = *MBB.getParent();
108 MachineRegisterInfo &MRI = MF.getRegInfo();
109
110 // FIXME: Is there *really* nothing to be done here? This assumes that
111 // no upstream pass introduces things like generic vreg on copies or
112 // target-specific instructions.
113 // We should document (and verify) that assumption.
114 if (!isPreISelGenericOpcode(I.getOpcode()))
115 return true;
116
117 if (I.getNumOperands() != I.getNumExplicitOperands()) {
118 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
119 return false;
120 }
121
122 LLT Ty = I.getType();
123 assert(Ty.isValid() && "Generic instruction doesn't have a type");
124
Ahmed Bougacha85505092016-07-28 17:15:15 +0000125 switch (I.getOpcode()) {
126 case TargetOpcode::G_BR: {
127 I.setDesc(TII.get(AArch64::B));
128 I.removeTypes();
129 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000130 }
131
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000132 case TargetOpcode::G_LOAD:
133 case TargetOpcode::G_STORE: {
134 LLT MemTy = I.getType(0);
135 LLT PtrTy = I.getType(1);
136
137 if (PtrTy != LLT::pointer(0)) {
138 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
139 << ", expected: " << LLT::pointer(0) << '\n');
140 return false;
141 }
142
143#ifndef NDEBUG
144 // Sanity-check the pointer register.
145 const unsigned PtrReg = I.getOperand(1).getReg();
146 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
147 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
148 "Load/Store pointer operand isn't a GPR");
149 assert(MRI.getSize(PtrReg) == 64 &&
150 "Load/Store pointer operand isn't 64-bit");
151#endif
152
153 const unsigned ValReg = I.getOperand(0).getReg();
154 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
155
156 const unsigned NewOpc =
157 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
158 if (NewOpc == I.getOpcode())
159 return false;
160
161 I.setDesc(TII.get(NewOpc));
162 I.removeTypes();
163
164 I.addOperand(MachineOperand::CreateImm(0));
165 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
166 }
167
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000168 case TargetOpcode::G_OR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000169 case TargetOpcode::G_AND:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000170 case TargetOpcode::G_ADD:
171 case TargetOpcode::G_SUB: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000172 DEBUG(dbgs() << "AArch64: Selecting: binop\n");
173
Ahmed Bougacha85505092016-07-28 17:15:15 +0000174 if (!Ty.isSized()) {
175 DEBUG(dbgs() << "Generic binop should be sized\n");
176 return false;
177 }
178
179 // The size (in bits) of the operation, or 0 for the label type.
180 const unsigned OpSize = Ty.getSizeInBits();
181
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000182 // Reject the various things we don't support yet.
183 {
184 const RegisterBank *PrevOpBank = nullptr;
185 for (auto &MO : I.operands()) {
186 // FIXME: Support non-register operands.
187 if (!MO.isReg()) {
188 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
189 return false;
190 }
191
192 // FIXME: Can generic operations have physical registers operands? If
193 // so, this will need to be taught about that, and we'll need to get the
194 // bank out of the minimal class for the register.
195 // Either way, this needs to be documented (and possibly verified).
196 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
197 DEBUG(dbgs() << "Generic inst has physical register operand\n");
198 return false;
199 }
200
201 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
202 if (!OpBank) {
203 DEBUG(dbgs() << "Generic register has no bank or class\n");
204 return false;
205 }
206
207 if (PrevOpBank && OpBank != PrevOpBank) {
208 DEBUG(dbgs() << "Generic inst operands have different banks\n");
209 return false;
210 }
211 PrevOpBank = OpBank;
212 }
213 }
214
215 const unsigned DefReg = I.getOperand(0).getReg();
216 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
217
218 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
219 if (NewOpc == I.getOpcode())
220 return false;
221
222 I.setDesc(TII.get(NewOpc));
223 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000224 I.removeTypes();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000225
226 // Now that we selected an opcode, we need to constrain the register
227 // operands to use appropriate classes.
228 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
229 }
230 }
231
232 return false;
233}