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