blob: 309f6128e5b428f0c4c987ddb41adf0e0def502b [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
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000015#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000016#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000017#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000020#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000021#include "MCTargetDesc/AArch64AddressingModes.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000022#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
David Blaikie62651302017-10-26 23:39:54 +000023#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
Aditya Nandakumar75ad9cc2017-04-19 20:48:50 +000024#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000025#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000029#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/IR/Type.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
34
35#define DEBUG_TYPE "aarch64-isel"
36
37using namespace llvm;
38
Daniel Sanders0b5293f2017-04-06 09:49:34 +000039namespace {
40
Daniel Sanderse7b0d662017-04-21 15:59:56 +000041#define GET_GLOBALISEL_PREDICATE_BITSET
42#include "AArch64GenGlobalISel.inc"
43#undef GET_GLOBALISEL_PREDICATE_BITSET
44
Daniel Sanders0b5293f2017-04-06 09:49:34 +000045class AArch64InstructionSelector : public InstructionSelector {
46public:
47 AArch64InstructionSelector(const AArch64TargetMachine &TM,
48 const AArch64Subtarget &STI,
49 const AArch64RegisterBankInfo &RBI);
50
Daniel Sandersf76f3152017-11-16 00:46:35 +000051 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
David Blaikie62651302017-10-26 23:39:54 +000052 static const char *getName() { return DEBUG_TYPE; }
Daniel Sanders0b5293f2017-04-06 09:49:34 +000053
54private:
55 /// tblgen-erated 'select' implementation, used as the initial selector for
56 /// the patterns that don't require complex C++.
Daniel Sandersf76f3152017-11-16 00:46:35 +000057 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000058
59 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
60 MachineRegisterInfo &MRI) const;
61 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
62 MachineRegisterInfo &MRI) const;
63
64 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
65 MachineRegisterInfo &MRI) const;
66
Daniel Sanders1e4569f2017-10-20 20:55:29 +000067 ComplexRendererFns selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000068
Daniel Sanders1e4569f2017-10-20 20:55:29 +000069 ComplexRendererFns selectAddrModeUnscaled(MachineOperand &Root,
70 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +000071
Daniel Sanders1e4569f2017-10-20 20:55:29 +000072 ComplexRendererFns selectAddrModeUnscaled8(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000073 return selectAddrModeUnscaled(Root, 1);
74 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000075 ComplexRendererFns selectAddrModeUnscaled16(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000076 return selectAddrModeUnscaled(Root, 2);
77 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000078 ComplexRendererFns selectAddrModeUnscaled32(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000079 return selectAddrModeUnscaled(Root, 4);
80 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000081 ComplexRendererFns selectAddrModeUnscaled64(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000082 return selectAddrModeUnscaled(Root, 8);
83 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000084 ComplexRendererFns selectAddrModeUnscaled128(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000085 return selectAddrModeUnscaled(Root, 16);
86 }
87
Daniel Sanders1e4569f2017-10-20 20:55:29 +000088 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root,
89 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +000090 template <int Width>
Daniel Sanders1e4569f2017-10-20 20:55:29 +000091 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000092 return selectAddrModeIndexed(Root, Width / 8);
93 }
94
Volkan Kelesf7f25682018-01-16 18:44:05 +000095 void renderTruncImm(MachineInstrBuilder &MIB, const MachineInstr &MI) const;
96
Daniel Sanders0b5293f2017-04-06 09:49:34 +000097 const AArch64TargetMachine &TM;
98 const AArch64Subtarget &STI;
99 const AArch64InstrInfo &TII;
100 const AArch64RegisterInfo &TRI;
101 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000102
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000103#define GET_GLOBALISEL_PREDICATES_DECL
104#include "AArch64GenGlobalISel.inc"
105#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000106
107// We declare the temporaries used by selectImpl() in the class to minimize the
108// cost of constructing placeholder values.
109#define GET_GLOBALISEL_TEMPORARIES_DECL
110#include "AArch64GenGlobalISel.inc"
111#undef GET_GLOBALISEL_TEMPORARIES_DECL
112};
113
114} // end anonymous namespace
115
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000116#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000117#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000118#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000119
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000120AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +0000121 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
122 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000123 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000124 TRI(*STI.getRegisterInfo()), RBI(RBI),
125#define GET_GLOBALISEL_PREDICATES_INIT
126#include "AArch64GenGlobalISel.inc"
127#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000128#define GET_GLOBALISEL_TEMPORARIES_INIT
129#include "AArch64GenGlobalISel.inc"
130#undef GET_GLOBALISEL_TEMPORARIES_INIT
131{
132}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000133
Tim Northoverfb8d9892016-10-12 22:49:15 +0000134// FIXME: This should be target-independent, inferred from the types declared
135// for each class in the bank.
136static const TargetRegisterClass *
137getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
Amara Emerson3838ed02018-02-02 18:03:30 +0000138 const RegisterBankInfo &RBI,
139 bool GetAllRegSet = false) {
Tim Northoverfb8d9892016-10-12 22:49:15 +0000140 if (RB.getID() == AArch64::GPRRegBankID) {
141 if (Ty.getSizeInBits() <= 32)
Amara Emerson3838ed02018-02-02 18:03:30 +0000142 return GetAllRegSet ? &AArch64::GPR32allRegClass
143 : &AArch64::GPR32RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000144 if (Ty.getSizeInBits() == 64)
Amara Emerson3838ed02018-02-02 18:03:30 +0000145 return GetAllRegSet ? &AArch64::GPR64allRegClass
146 : &AArch64::GPR64RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000147 return nullptr;
148 }
149
150 if (RB.getID() == AArch64::FPRRegBankID) {
Amara Emerson3838ed02018-02-02 18:03:30 +0000151 if (Ty.getSizeInBits() <= 16)
152 return &AArch64::FPR16RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000153 if (Ty.getSizeInBits() == 32)
154 return &AArch64::FPR32RegClass;
155 if (Ty.getSizeInBits() == 64)
156 return &AArch64::FPR64RegClass;
157 if (Ty.getSizeInBits() == 128)
158 return &AArch64::FPR128RegClass;
159 return nullptr;
160 }
161
162 return nullptr;
163}
164
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000165/// Check whether \p I is a currently unsupported binary operation:
166/// - it has an unsized type
167/// - an operand is not a vreg
168/// - all operands are not in the same bank
169/// These are checks that should someday live in the verifier, but right now,
170/// these are mostly limitations of the aarch64 selector.
171static bool unsupportedBinOp(const MachineInstr &I,
172 const AArch64RegisterBankInfo &RBI,
173 const MachineRegisterInfo &MRI,
174 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000175 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000176 if (!Ty.isValid()) {
177 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000178 return true;
179 }
180
181 const RegisterBank *PrevOpBank = nullptr;
182 for (auto &MO : I.operands()) {
183 // FIXME: Support non-register operands.
184 if (!MO.isReg()) {
185 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
186 return true;
187 }
188
189 // FIXME: Can generic operations have physical registers operands? If
190 // so, this will need to be taught about that, and we'll need to get the
191 // bank out of the minimal class for the register.
192 // Either way, this needs to be documented (and possibly verified).
193 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
194 DEBUG(dbgs() << "Generic inst has physical register operand\n");
195 return true;
196 }
197
198 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
199 if (!OpBank) {
200 DEBUG(dbgs() << "Generic register has no bank or class\n");
201 return true;
202 }
203
204 if (PrevOpBank && OpBank != PrevOpBank) {
205 DEBUG(dbgs() << "Generic inst operands have different banks\n");
206 return true;
207 }
208 PrevOpBank = OpBank;
209 }
210 return false;
211}
212
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000213/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000214/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000215/// and of size \p OpSize.
216/// \returns \p GenericOpc if the combination is unsupported.
217static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
218 unsigned OpSize) {
219 switch (RegBankID) {
220 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000221 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000222 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000223 case TargetOpcode::G_SHL:
224 return AArch64::LSLVWr;
225 case TargetOpcode::G_LSHR:
226 return AArch64::LSRVWr;
227 case TargetOpcode::G_ASHR:
228 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000229 default:
230 return GenericOpc;
231 }
Tim Northover55782222016-10-18 20:03:48 +0000232 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000233 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000234 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000235 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000236 case TargetOpcode::G_SHL:
237 return AArch64::LSLVXr;
238 case TargetOpcode::G_LSHR:
239 return AArch64::LSRVXr;
240 case TargetOpcode::G_ASHR:
241 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000242 default:
243 return GenericOpc;
244 }
245 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000246 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000247 case AArch64::FPRRegBankID:
248 switch (OpSize) {
249 case 32:
250 switch (GenericOpc) {
251 case TargetOpcode::G_FADD:
252 return AArch64::FADDSrr;
253 case TargetOpcode::G_FSUB:
254 return AArch64::FSUBSrr;
255 case TargetOpcode::G_FMUL:
256 return AArch64::FMULSrr;
257 case TargetOpcode::G_FDIV:
258 return AArch64::FDIVSrr;
259 default:
260 return GenericOpc;
261 }
262 case 64:
263 switch (GenericOpc) {
264 case TargetOpcode::G_FADD:
265 return AArch64::FADDDrr;
266 case TargetOpcode::G_FSUB:
267 return AArch64::FSUBDrr;
268 case TargetOpcode::G_FMUL:
269 return AArch64::FMULDrr;
270 case TargetOpcode::G_FDIV:
271 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000272 case TargetOpcode::G_OR:
273 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000274 default:
275 return GenericOpc;
276 }
277 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000278 break;
279 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000280 return GenericOpc;
281}
282
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000283/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
284/// appropriate for the (value) register bank \p RegBankID and of memory access
285/// size \p OpSize. This returns the variant with the base+unsigned-immediate
286/// addressing mode (e.g., LDRXui).
287/// \returns \p GenericOpc if the combination is unsupported.
288static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
289 unsigned OpSize) {
290 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
291 switch (RegBankID) {
292 case AArch64::GPRRegBankID:
293 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000294 case 8:
295 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
296 case 16:
297 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000298 case 32:
299 return isStore ? AArch64::STRWui : AArch64::LDRWui;
300 case 64:
301 return isStore ? AArch64::STRXui : AArch64::LDRXui;
302 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000303 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000304 case AArch64::FPRRegBankID:
305 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000306 case 8:
307 return isStore ? AArch64::STRBui : AArch64::LDRBui;
308 case 16:
309 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000310 case 32:
311 return isStore ? AArch64::STRSui : AArch64::LDRSui;
312 case 64:
313 return isStore ? AArch64::STRDui : AArch64::LDRDui;
314 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000315 break;
316 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000317 return GenericOpc;
318}
319
Amara Emersondb211892018-02-20 05:11:57 +0000320static bool selectFP16CopyFromGPR32(MachineInstr &I, const TargetInstrInfo &TII,
321 MachineRegisterInfo &MRI, unsigned SrcReg) {
322 // Copies from gpr32 to fpr16 need to use a sub-register copy.
323 unsigned CopyReg = MRI.createVirtualRegister(&AArch64::FPR32RegClass);
324 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::COPY))
325 .addDef(CopyReg)
326 .addUse(SrcReg);
327 unsigned SubRegCopy = MRI.createVirtualRegister(&AArch64::FPR16RegClass);
328 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY))
329 .addDef(SubRegCopy)
330 .addUse(CopyReg, 0, AArch64::hsub);
331
332 MachineOperand &RegOp = I.getOperand(1);
333 RegOp.setReg(SubRegCopy);
334 return true;
335}
336
Quentin Colombetcb629a82016-10-12 03:57:49 +0000337static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
338 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
339 const RegisterBankInfo &RBI) {
340
341 unsigned DstReg = I.getOperand(0).getReg();
Amara Emersondb211892018-02-20 05:11:57 +0000342 unsigned SrcReg = I.getOperand(1).getReg();
343
Quentin Colombetcb629a82016-10-12 03:57:49 +0000344 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
Amara Emersondb211892018-02-20 05:11:57 +0000345 if (TRI.getRegClass(AArch64::FPR16RegClassID)->contains(DstReg) &&
346 !TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
347 const RegisterBank &RegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
348 const TargetRegisterClass *SrcRC = getRegClassForTypeOnBank(
349 MRI.getType(SrcReg), RegBank, RBI, /* GetAllRegSet */ true);
350 if (SrcRC == &AArch64::GPR32allRegClass)
351 return selectFP16CopyFromGPR32(I, TII, MRI, SrcReg);
352 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000353 assert(I.isCopy() && "Generic operators do not allow physical registers");
354 return true;
355 }
356
357 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
358 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
Amara Emerson3838ed02018-02-02 18:03:30 +0000359 (void)DstSize;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000360 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
361 (void)SrcSize;
362 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
363 "No phys reg on generic operators");
364 assert(
365 (DstSize == SrcSize ||
366 // Copies are a mean to setup initial types, the number of
367 // bits may not exactly match.
368 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
369 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
370 // Copies are a mean to copy bits around, as long as we are
371 // on the same register class, that's fine. Otherwise, that
372 // means we need some SUBREG_TO_REG or AND & co.
373 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
374 "Copy with different width?!");
375 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
376 "GPRs cannot get more than 64-bit width values");
Quentin Colombetcb629a82016-10-12 03:57:49 +0000377
Amara Emerson3838ed02018-02-02 18:03:30 +0000378 const TargetRegisterClass *RC = getRegClassForTypeOnBank(
379 MRI.getType(DstReg), RegBank, RBI, /* GetAllRegSet */ true);
380 if (!RC) {
381 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
382 return false;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000383 }
384
Amara Emerson7e9f3482018-02-18 17:10:49 +0000385 if (!TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
Amara Emersondb211892018-02-20 05:11:57 +0000386 const RegClassOrRegBank &RegClassOrBank = MRI.getRegClassOrRegBank(SrcReg);
Amara Emerson7e9f3482018-02-18 17:10:49 +0000387 const TargetRegisterClass *SrcRC =
388 RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
389 const RegisterBank *RB = nullptr;
390 if (!SrcRC) {
391 RB = RegClassOrBank.get<const RegisterBank *>();
392 SrcRC = getRegClassForTypeOnBank(MRI.getType(SrcReg), *RB, RBI, true);
393 }
394 // Copies from fpr16 to gpr32 need to use SUBREG_TO_REG.
395 if (RC == &AArch64::GPR32allRegClass && SrcRC == &AArch64::FPR16RegClass) {
396 unsigned PromoteReg = MRI.createVirtualRegister(&AArch64::FPR32RegClass);
397 BuildMI(*I.getParent(), I, I.getDebugLoc(),
398 TII.get(AArch64::SUBREG_TO_REG))
399 .addDef(PromoteReg)
400 .addImm(0)
401 .addUse(SrcReg)
402 .addImm(AArch64::hsub);
403 MachineOperand &RegOp = I.getOperand(1);
404 RegOp.setReg(PromoteReg);
Amara Emersondb211892018-02-20 05:11:57 +0000405 } else if (RC == &AArch64::FPR16RegClass &&
406 SrcRC == &AArch64::GPR32allRegClass) {
407 selectFP16CopyFromGPR32(I, TII, MRI, SrcReg);
Amara Emerson7e9f3482018-02-18 17:10:49 +0000408 }
409 }
410
Quentin Colombetcb629a82016-10-12 03:57:49 +0000411 // No need to constrain SrcReg. It will get constrained when
412 // we hit another of its use or its defs.
413 // Copies do not have constraints.
414 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
415 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
416 << " operand\n");
417 return false;
418 }
419 I.setDesc(TII.get(AArch64::COPY));
420 return true;
421}
422
Tim Northover69271c62016-10-12 22:49:11 +0000423static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
424 if (!DstTy.isScalar() || !SrcTy.isScalar())
425 return GenericOpc;
426
427 const unsigned DstSize = DstTy.getSizeInBits();
428 const unsigned SrcSize = SrcTy.getSizeInBits();
429
430 switch (DstSize) {
431 case 32:
432 switch (SrcSize) {
433 case 32:
434 switch (GenericOpc) {
435 case TargetOpcode::G_SITOFP:
436 return AArch64::SCVTFUWSri;
437 case TargetOpcode::G_UITOFP:
438 return AArch64::UCVTFUWSri;
439 case TargetOpcode::G_FPTOSI:
440 return AArch64::FCVTZSUWSr;
441 case TargetOpcode::G_FPTOUI:
442 return AArch64::FCVTZUUWSr;
443 default:
444 return GenericOpc;
445 }
446 case 64:
447 switch (GenericOpc) {
448 case TargetOpcode::G_SITOFP:
449 return AArch64::SCVTFUXSri;
450 case TargetOpcode::G_UITOFP:
451 return AArch64::UCVTFUXSri;
452 case TargetOpcode::G_FPTOSI:
453 return AArch64::FCVTZSUWDr;
454 case TargetOpcode::G_FPTOUI:
455 return AArch64::FCVTZUUWDr;
456 default:
457 return GenericOpc;
458 }
459 default:
460 return GenericOpc;
461 }
462 case 64:
463 switch (SrcSize) {
464 case 32:
465 switch (GenericOpc) {
466 case TargetOpcode::G_SITOFP:
467 return AArch64::SCVTFUWDri;
468 case TargetOpcode::G_UITOFP:
469 return AArch64::UCVTFUWDri;
470 case TargetOpcode::G_FPTOSI:
471 return AArch64::FCVTZSUXSr;
472 case TargetOpcode::G_FPTOUI:
473 return AArch64::FCVTZUUXSr;
474 default:
475 return GenericOpc;
476 }
477 case 64:
478 switch (GenericOpc) {
479 case TargetOpcode::G_SITOFP:
480 return AArch64::SCVTFUXDri;
481 case TargetOpcode::G_UITOFP:
482 return AArch64::UCVTFUXDri;
483 case TargetOpcode::G_FPTOSI:
484 return AArch64::FCVTZSUXDr;
485 case TargetOpcode::G_FPTOUI:
486 return AArch64::FCVTZUUXDr;
487 default:
488 return GenericOpc;
489 }
490 default:
491 return GenericOpc;
492 }
493 default:
494 return GenericOpc;
495 };
496 return GenericOpc;
497}
498
Tim Northover6c02ad52016-10-12 22:49:04 +0000499static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
500 switch (P) {
501 default:
502 llvm_unreachable("Unknown condition code!");
503 case CmpInst::ICMP_NE:
504 return AArch64CC::NE;
505 case CmpInst::ICMP_EQ:
506 return AArch64CC::EQ;
507 case CmpInst::ICMP_SGT:
508 return AArch64CC::GT;
509 case CmpInst::ICMP_SGE:
510 return AArch64CC::GE;
511 case CmpInst::ICMP_SLT:
512 return AArch64CC::LT;
513 case CmpInst::ICMP_SLE:
514 return AArch64CC::LE;
515 case CmpInst::ICMP_UGT:
516 return AArch64CC::HI;
517 case CmpInst::ICMP_UGE:
518 return AArch64CC::HS;
519 case CmpInst::ICMP_ULT:
520 return AArch64CC::LO;
521 case CmpInst::ICMP_ULE:
522 return AArch64CC::LS;
523 }
524}
525
Tim Northover7dd378d2016-10-12 22:49:07 +0000526static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
527 AArch64CC::CondCode &CondCode,
528 AArch64CC::CondCode &CondCode2) {
529 CondCode2 = AArch64CC::AL;
530 switch (P) {
531 default:
532 llvm_unreachable("Unknown FP condition!");
533 case CmpInst::FCMP_OEQ:
534 CondCode = AArch64CC::EQ;
535 break;
536 case CmpInst::FCMP_OGT:
537 CondCode = AArch64CC::GT;
538 break;
539 case CmpInst::FCMP_OGE:
540 CondCode = AArch64CC::GE;
541 break;
542 case CmpInst::FCMP_OLT:
543 CondCode = AArch64CC::MI;
544 break;
545 case CmpInst::FCMP_OLE:
546 CondCode = AArch64CC::LS;
547 break;
548 case CmpInst::FCMP_ONE:
549 CondCode = AArch64CC::MI;
550 CondCode2 = AArch64CC::GT;
551 break;
552 case CmpInst::FCMP_ORD:
553 CondCode = AArch64CC::VC;
554 break;
555 case CmpInst::FCMP_UNO:
556 CondCode = AArch64CC::VS;
557 break;
558 case CmpInst::FCMP_UEQ:
559 CondCode = AArch64CC::EQ;
560 CondCode2 = AArch64CC::VS;
561 break;
562 case CmpInst::FCMP_UGT:
563 CondCode = AArch64CC::HI;
564 break;
565 case CmpInst::FCMP_UGE:
566 CondCode = AArch64CC::PL;
567 break;
568 case CmpInst::FCMP_ULT:
569 CondCode = AArch64CC::LT;
570 break;
571 case CmpInst::FCMP_ULE:
572 CondCode = AArch64CC::LE;
573 break;
574 case CmpInst::FCMP_UNE:
575 CondCode = AArch64CC::NE;
576 break;
577 }
578}
579
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000580bool AArch64InstructionSelector::selectCompareBranch(
581 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
582
583 const unsigned CondReg = I.getOperand(0).getReg();
584 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
585 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000586 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
587 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000588 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
589 return false;
590
591 unsigned LHS = CCMI->getOperand(2).getReg();
592 unsigned RHS = CCMI->getOperand(3).getReg();
593 if (!getConstantVRegVal(RHS, MRI))
594 std::swap(RHS, LHS);
595
596 const auto RHSImm = getConstantVRegVal(RHS, MRI);
597 if (!RHSImm || *RHSImm != 0)
598 return false;
599
600 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
601 if (RB.getID() != AArch64::GPRRegBankID)
602 return false;
603
604 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
605 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
606 return false;
607
608 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
609 unsigned CBOpc = 0;
610 if (CmpWidth <= 32)
611 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
612 else if (CmpWidth == 64)
613 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
614 else
615 return false;
616
Aditya Nandakumar18b3f9d2018-01-17 19:31:33 +0000617 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
618 .addUse(LHS)
619 .addMBB(DestMBB)
620 .constrainAllUses(TII, TRI, RBI);
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000621
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000622 I.eraseFromParent();
623 return true;
624}
625
Tim Northovere9600d82017-02-08 17:57:27 +0000626bool AArch64InstructionSelector::selectVaStartAAPCS(
627 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
628 return false;
629}
630
631bool AArch64InstructionSelector::selectVaStartDarwin(
632 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
633 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
634 unsigned ListReg = I.getOperand(0).getReg();
635
636 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
637
638 auto MIB =
639 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
640 .addDef(ArgsAddrReg)
641 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
642 .addImm(0)
643 .addImm(0);
644
645 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
646
647 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
648 .addUse(ArgsAddrReg)
649 .addUse(ListReg)
650 .addImm(0)
651 .addMemOperand(*I.memoperands_begin());
652
653 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
654 I.eraseFromParent();
655 return true;
656}
657
Daniel Sandersf76f3152017-11-16 00:46:35 +0000658bool AArch64InstructionSelector::select(MachineInstr &I,
659 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000660 assert(I.getParent() && "Instruction should be in a basic block!");
661 assert(I.getParent()->getParent() && "Instruction should be in a function!");
662
663 MachineBasicBlock &MBB = *I.getParent();
664 MachineFunction &MF = *MBB.getParent();
665 MachineRegisterInfo &MRI = MF.getRegInfo();
666
Tim Northovercdf23f12016-10-31 18:30:59 +0000667 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000668 // G_PHI requires same handling as PHI
669 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000670 // Certain non-generic instructions also need some special handling.
671
672 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
673 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000674
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000675 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000676 const unsigned DefReg = I.getOperand(0).getReg();
677 const LLT DefTy = MRI.getType(DefReg);
678
679 const TargetRegisterClass *DefRC = nullptr;
680 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
681 DefRC = TRI.getRegClass(DefReg);
682 } else {
683 const RegClassOrRegBank &RegClassOrBank =
684 MRI.getRegClassOrRegBank(DefReg);
685
686 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
687 if (!DefRC) {
688 if (!DefTy.isValid()) {
689 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
690 return false;
691 }
692 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
693 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
694 if (!DefRC) {
695 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
696 return false;
697 }
698 }
699 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000700 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000701
702 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
703 }
704
705 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000706 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000707
708 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000709 }
710
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000711
712 if (I.getNumOperands() != I.getNumExplicitOperands()) {
713 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
714 return false;
715 }
716
Daniel Sandersf76f3152017-11-16 00:46:35 +0000717 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000718 return true;
719
Tim Northover32a078a2016-09-15 10:09:59 +0000720 LLT Ty =
721 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000722
Tim Northover69271c62016-10-12 22:49:11 +0000723 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000724 case TargetOpcode::G_BRCOND: {
725 if (Ty.getSizeInBits() > 32) {
726 // We shouldn't need this on AArch64, but it would be implemented as an
727 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
728 // bit being tested is < 32.
729 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
730 << ", expected at most 32-bits");
731 return false;
732 }
733
734 const unsigned CondReg = I.getOperand(0).getReg();
735 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
736
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000737 if (selectCompareBranch(I, MF, MRI))
738 return true;
739
Tim Northover5e3dbf32016-10-12 22:49:01 +0000740 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
741 .addUse(CondReg)
742 .addImm(/*bit offset=*/0)
743 .addMBB(DestMBB);
744
745 I.eraseFromParent();
746 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
747 }
748
Kristof Beyls65a12c02017-01-30 09:13:18 +0000749 case TargetOpcode::G_BRINDIRECT: {
750 I.setDesc(TII.get(AArch64::BR));
751 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
752 }
753
Tim Northover4494d692016-10-18 19:47:57 +0000754 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000755 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000756 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
757
758 const LLT s32 = LLT::scalar(32);
759 const LLT s64 = LLT::scalar(64);
760 const LLT p0 = LLT::pointer(0, 64);
761
762 const unsigned DefReg = I.getOperand(0).getReg();
763 const LLT DefTy = MRI.getType(DefReg);
764 const unsigned DefSize = DefTy.getSizeInBits();
765 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
766
767 // FIXME: Redundant check, but even less readable when factored out.
768 if (isFP) {
769 if (Ty != s32 && Ty != s64) {
770 DEBUG(dbgs() << "Unable to materialize FP " << Ty
771 << " constant, expected: " << s32 << " or " << s64
772 << '\n');
773 return false;
774 }
775
776 if (RB.getID() != AArch64::FPRRegBankID) {
777 DEBUG(dbgs() << "Unable to materialize FP " << Ty
778 << " constant on bank: " << RB << ", expected: FPR\n");
779 return false;
780 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000781
782 // The case when we have 0.0 is covered by tablegen. Reject it here so we
783 // can be sure tablegen works correctly and isn't rescued by this code.
784 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
785 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000786 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000787 // s32 and s64 are covered by tablegen.
788 if (Ty != p0) {
Tim Northover4494d692016-10-18 19:47:57 +0000789 DEBUG(dbgs() << "Unable to materialize integer " << Ty
790 << " constant, expected: " << s32 << ", " << s64 << ", or "
791 << p0 << '\n');
792 return false;
793 }
794
795 if (RB.getID() != AArch64::GPRRegBankID) {
796 DEBUG(dbgs() << "Unable to materialize integer " << Ty
797 << " constant on bank: " << RB << ", expected: GPR\n");
798 return false;
799 }
800 }
801
802 const unsigned MovOpc =
803 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
804
805 I.setDesc(TII.get(MovOpc));
806
807 if (isFP) {
808 const TargetRegisterClass &GPRRC =
809 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
810 const TargetRegisterClass &FPRRC =
811 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
812
813 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
814 MachineOperand &RegOp = I.getOperand(0);
815 RegOp.setReg(DefGPRReg);
816
817 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
818 TII.get(AArch64::COPY))
819 .addDef(DefReg)
820 .addUse(DefGPRReg);
821
822 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
823 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
824 return false;
825 }
826
827 MachineOperand &ImmOp = I.getOperand(1);
828 // FIXME: Is going through int64_t always correct?
829 ImmOp.ChangeToImmediate(
830 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000831 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000832 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
833 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000834 } else if (I.getOperand(1).isImm()) {
835 uint64_t Val = I.getOperand(1).getImm();
836 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000837 }
838
839 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
840 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000841 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000842 case TargetOpcode::G_EXTRACT: {
843 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000844 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
Amara Emerson242efdb2018-02-18 17:28:34 +0000845 (void)DstTy;
Amara Emersonbc03bae2018-02-18 17:03:02 +0000846 unsigned SrcSize = SrcTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000847 // Larger extracts are vectors, same-size extracts should be something else
848 // by now (either split up or simplified to a COPY).
849 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
850 return false;
851
Amara Emersonbc03bae2018-02-18 17:03:02 +0000852 I.setDesc(TII.get(SrcSize == 64 ? AArch64::UBFMXri : AArch64::UBFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000853 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
854 Ty.getSizeInBits() - 1);
855
Amara Emersonbc03bae2018-02-18 17:03:02 +0000856 if (SrcSize < 64) {
857 assert(SrcSize == 32 && DstTy.getSizeInBits() == 16 &&
858 "unexpected G_EXTRACT types");
859 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
860 }
861
Tim Northover7b6d66c2017-07-20 22:58:38 +0000862 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
863 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
864 TII.get(AArch64::COPY))
865 .addDef(I.getOperand(0).getReg())
866 .addUse(DstReg, 0, AArch64::sub_32);
867 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
868 AArch64::GPR32RegClass, MRI);
869 I.getOperand(0).setReg(DstReg);
870
871 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
872 }
873
874 case TargetOpcode::G_INSERT: {
875 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000876 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
877 unsigned DstSize = DstTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000878 // Larger inserts are vectors, same-size ones should be something else by
879 // now (split up or turned into COPYs).
880 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
881 return false;
882
Amara Emersonbc03bae2018-02-18 17:03:02 +0000883 I.setDesc(TII.get(DstSize == 64 ? AArch64::BFMXri : AArch64::BFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000884 unsigned LSB = I.getOperand(3).getImm();
885 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
Amara Emersonbc03bae2018-02-18 17:03:02 +0000886 I.getOperand(3).setImm((DstSize - LSB) % DstSize);
Tim Northover7b6d66c2017-07-20 22:58:38 +0000887 MachineInstrBuilder(MF, I).addImm(Width - 1);
888
Amara Emersonbc03bae2018-02-18 17:03:02 +0000889 if (DstSize < 64) {
890 assert(DstSize == 32 && SrcTy.getSizeInBits() == 16 &&
891 "unexpected G_INSERT types");
892 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
893 }
894
Tim Northover7b6d66c2017-07-20 22:58:38 +0000895 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
896 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
897 TII.get(AArch64::SUBREG_TO_REG))
898 .addDef(SrcReg)
899 .addImm(0)
900 .addUse(I.getOperand(2).getReg())
901 .addImm(AArch64::sub_32);
902 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
903 AArch64::GPR32RegClass, MRI);
904 I.getOperand(2).setReg(SrcReg);
905
906 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
907 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000908 case TargetOpcode::G_FRAME_INDEX: {
909 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000910 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000911 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000912 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000913 return false;
914 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000915 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000916
917 // MOs for a #0 shifted immediate.
918 I.addOperand(MachineOperand::CreateImm(0));
919 I.addOperand(MachineOperand::CreateImm(0));
920
921 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
922 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000923
924 case TargetOpcode::G_GLOBAL_VALUE: {
925 auto GV = I.getOperand(1).getGlobal();
926 if (GV->isThreadLocal()) {
927 // FIXME: we don't support TLS yet.
928 return false;
929 }
930 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000931 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000932 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000933 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +0000934 } else if (TM.getCodeModel() == CodeModel::Large) {
935 // Materialize the global using movz/movk instructions.
936 unsigned MovZDstReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
937 auto InsertPt = std::next(I.getIterator());
938 auto MovZ =
939 BuildMI(MBB, InsertPt, I.getDebugLoc(), TII.get(AArch64::MOVZXi))
940 .addDef(MovZDstReg);
941 MovZ->addOperand(MF, I.getOperand(1));
942 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
943 AArch64II::MO_NC);
944 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
945 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
946
947 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags,
948 unsigned Offset, unsigned ForceDstReg) {
949 unsigned DstReg =
950 ForceDstReg ? ForceDstReg
951 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
952 auto MovI = BuildMI(MBB, InsertPt, MovZ->getDebugLoc(),
953 TII.get(AArch64::MOVKXi))
954 .addDef(DstReg)
955 .addReg(SrcReg);
956 MovI->addOperand(MF, MachineOperand::CreateGA(
957 GV, MovZ->getOperand(1).getOffset(), Flags));
958 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
959 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
960 return DstReg;
961 };
962 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(),
963 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
964 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
965 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
966 I.eraseFromParent();
967 return true;
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000968 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000969 I.setDesc(TII.get(AArch64::MOVaddr));
970 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
971 MachineInstrBuilder MIB(MF, I);
972 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
973 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
974 }
975 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
976 }
977
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000978 case TargetOpcode::G_LOAD:
979 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000980 LLT MemTy = Ty;
981 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000982
Tim Northover5ae83502016-09-15 09:20:34 +0000983 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000984 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000985 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000986 return false;
987 }
988
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000989 auto &MemOp = **I.memoperands_begin();
990 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
991 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
992 return false;
993 }
994
Amara Emerson4f84f882018-01-24 20:35:37 +0000995 // FIXME: PR36018: Volatile loads in some cases are incorrectly selected by
996 // folding with an extend. Until we have a G_SEXTLOAD solution bail out if
997 // we hit one.
998 if (Opcode == TargetOpcode::G_LOAD && MemOp.isVolatile())
999 return false;
1000
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001001 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001002#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001003 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001004 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001005 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
1006 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +00001007 assert(MRI.getType(PtrReg).isPointer() &&
1008 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001009#endif
1010
1011 const unsigned ValReg = I.getOperand(0).getReg();
1012 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
1013
1014 const unsigned NewOpc =
1015 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
1016 if (NewOpc == I.getOpcode())
1017 return false;
1018
1019 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001020
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001021 uint64_t Offset = 0;
1022 auto *PtrMI = MRI.getVRegDef(PtrReg);
1023
1024 // Try to fold a GEP into our unsigned immediate addressing mode.
1025 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
1026 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
1027 int64_t Imm = *COff;
1028 const unsigned Size = MemTy.getSizeInBits() / 8;
1029 const unsigned Scale = Log2_32(Size);
1030 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
1031 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
1032 I.getOperand(1).setReg(Ptr2Reg);
1033 PtrMI = MRI.getVRegDef(Ptr2Reg);
1034 Offset = Imm / Size;
1035 }
1036 }
1037 }
1038
Ahmed Bougachaf75782f2017-03-27 17:31:56 +00001039 // If we haven't folded anything into our addressing mode yet, try to fold
1040 // a frame index into the base+offset.
1041 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1042 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
1043
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001044 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +00001045
1046 // If we're storing a 0, use WZR/XZR.
1047 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
1048 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
1049 if (I.getOpcode() == AArch64::STRWui)
1050 I.getOperand(0).setReg(AArch64::WZR);
1051 else if (I.getOpcode() == AArch64::STRXui)
1052 I.getOperand(0).setReg(AArch64::XZR);
1053 }
1054 }
1055
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001056 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1057 }
1058
Tim Northover9dd78f82017-02-08 21:22:25 +00001059 case TargetOpcode::G_SMULH:
1060 case TargetOpcode::G_UMULH: {
1061 // Reject the various things we don't support yet.
1062 if (unsupportedBinOp(I, RBI, MRI, TRI))
1063 return false;
1064
1065 const unsigned DefReg = I.getOperand(0).getReg();
1066 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1067
1068 if (RB.getID() != AArch64::GPRRegBankID) {
1069 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
1070 return false;
1071 }
1072
1073 if (Ty != LLT::scalar(64)) {
1074 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1075 << ", expected: " << LLT::scalar(64) << '\n');
1076 return false;
1077 }
1078
1079 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1080 : AArch64::UMULHrr;
1081 I.setDesc(TII.get(NewOpc));
1082
1083 // Now that we selected an opcode, we need to constrain the register
1084 // operands to use appropriate classes.
1085 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1086 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001087 case TargetOpcode::G_FADD:
1088 case TargetOpcode::G_FSUB:
1089 case TargetOpcode::G_FMUL:
1090 case TargetOpcode::G_FDIV:
1091
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001092 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001093 case TargetOpcode::G_SHL:
1094 case TargetOpcode::G_LSHR:
1095 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001096 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001097 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001098 if (unsupportedBinOp(I, RBI, MRI, TRI))
1099 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001100
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001101 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001102
1103 const unsigned DefReg = I.getOperand(0).getReg();
1104 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1105
1106 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1107 if (NewOpc == I.getOpcode())
1108 return false;
1109
1110 I.setDesc(TII.get(NewOpc));
1111 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001112
1113 // Now that we selected an opcode, we need to constrain the register
1114 // operands to use appropriate classes.
1115 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1116 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001117
Tim Northover398c5f52017-02-14 20:56:29 +00001118 case TargetOpcode::G_PTR_MASK: {
1119 uint64_t Align = I.getOperand(2).getImm();
1120 if (Align >= 64 || Align == 0)
1121 return false;
1122
1123 uint64_t Mask = ~((1ULL << Align) - 1);
1124 I.setDesc(TII.get(AArch64::ANDXri));
1125 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1126
1127 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1128 }
Tim Northover037af52c2016-10-31 18:31:09 +00001129 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001130 case TargetOpcode::G_TRUNC: {
1131 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1132 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1133
1134 const unsigned DstReg = I.getOperand(0).getReg();
1135 const unsigned SrcReg = I.getOperand(1).getReg();
1136
1137 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1138 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1139
1140 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001141 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001142 return false;
1143 }
1144
1145 if (DstRB.getID() == AArch64::GPRRegBankID) {
1146 const TargetRegisterClass *DstRC =
1147 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1148 if (!DstRC)
1149 return false;
1150
1151 const TargetRegisterClass *SrcRC =
1152 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1153 if (!SrcRC)
1154 return false;
1155
1156 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1157 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001158 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001159 return false;
1160 }
1161
1162 if (DstRC == SrcRC) {
1163 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001164 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1165 SrcTy == LLT::scalar(64)) {
1166 llvm_unreachable("TableGen can import this case");
1167 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001168 } else if (DstRC == &AArch64::GPR32RegClass &&
1169 SrcRC == &AArch64::GPR64RegClass) {
1170 I.getOperand(1).setSubReg(AArch64::sub_32);
1171 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001172 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001173 return false;
1174 }
1175
1176 I.setDesc(TII.get(TargetOpcode::COPY));
1177 return true;
1178 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1179 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1180 I.setDesc(TII.get(AArch64::XTNv4i16));
1181 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1182 return true;
1183 }
1184 }
1185
1186 return false;
1187 }
1188
Tim Northover3d38b3a2016-10-11 20:50:21 +00001189 case TargetOpcode::G_ANYEXT: {
1190 const unsigned DstReg = I.getOperand(0).getReg();
1191 const unsigned SrcReg = I.getOperand(1).getReg();
1192
Quentin Colombetcb629a82016-10-12 03:57:49 +00001193 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1194 if (RBDst.getID() != AArch64::GPRRegBankID) {
1195 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1196 return false;
1197 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001198
Quentin Colombetcb629a82016-10-12 03:57:49 +00001199 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1200 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1201 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001202 return false;
1203 }
1204
1205 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1206
1207 if (DstSize == 0) {
1208 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1209 return false;
1210 }
1211
Quentin Colombetcb629a82016-10-12 03:57:49 +00001212 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001213 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1214 << ", expected: 32 or 64\n");
1215 return false;
1216 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001217 // At this point G_ANYEXT is just like a plain COPY, but we need
1218 // to explicitly form the 64-bit value if any.
1219 if (DstSize > 32) {
1220 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1221 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1222 .addDef(ExtSrc)
1223 .addImm(0)
1224 .addUse(SrcReg)
1225 .addImm(AArch64::sub_32);
1226 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001227 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001228 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001229 }
1230
1231 case TargetOpcode::G_ZEXT:
1232 case TargetOpcode::G_SEXT: {
1233 unsigned Opcode = I.getOpcode();
1234 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1235 SrcTy = MRI.getType(I.getOperand(1).getReg());
1236 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1237 const unsigned DefReg = I.getOperand(0).getReg();
1238 const unsigned SrcReg = I.getOperand(1).getReg();
1239 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1240
1241 if (RB.getID() != AArch64::GPRRegBankID) {
1242 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1243 << ", expected: GPR\n");
1244 return false;
1245 }
1246
1247 MachineInstr *ExtI;
1248 if (DstTy == LLT::scalar(64)) {
1249 // FIXME: Can we avoid manually doing this?
1250 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1251 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1252 << " operand\n");
1253 return false;
1254 }
1255
1256 const unsigned SrcXReg =
1257 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1258 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1259 .addDef(SrcXReg)
1260 .addImm(0)
1261 .addUse(SrcReg)
1262 .addImm(AArch64::sub_32);
1263
1264 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1265 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1266 .addDef(DefReg)
1267 .addUse(SrcXReg)
1268 .addImm(0)
1269 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001270 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001271 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1272 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1273 .addDef(DefReg)
1274 .addUse(SrcReg)
1275 .addImm(0)
1276 .addImm(SrcTy.getSizeInBits() - 1);
1277 } else {
1278 return false;
1279 }
1280
1281 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1282
1283 I.eraseFromParent();
1284 return true;
1285 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001286
Tim Northover69271c62016-10-12 22:49:11 +00001287 case TargetOpcode::G_SITOFP:
1288 case TargetOpcode::G_UITOFP:
1289 case TargetOpcode::G_FPTOSI:
1290 case TargetOpcode::G_FPTOUI: {
1291 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1292 SrcTy = MRI.getType(I.getOperand(1).getReg());
1293 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1294 if (NewOpc == Opcode)
1295 return false;
1296
1297 I.setDesc(TII.get(NewOpc));
1298 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1299
1300 return true;
1301 }
1302
1303
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001304 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001305 // The importer is currently unable to import pointer types since they
1306 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001307 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001308
Daniel Sandersedd07842017-08-17 09:26:14 +00001309 case TargetOpcode::G_BITCAST:
1310 // Imported SelectionDAG rules can handle every bitcast except those that
1311 // bitcast from a type to the same type. Ideally, these shouldn't occur
1312 // but we might not run an optimizer that deletes them.
1313 if (MRI.getType(I.getOperand(0).getReg()) ==
1314 MRI.getType(I.getOperand(1).getReg()))
1315 return selectCopy(I, TII, MRI, TRI, RBI);
1316 return false;
1317
Tim Northover9ac0eba2016-11-08 00:45:29 +00001318 case TargetOpcode::G_SELECT: {
1319 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1320 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1321 << ", expected: " << LLT::scalar(1) << '\n');
1322 return false;
1323 }
1324
1325 const unsigned CondReg = I.getOperand(1).getReg();
1326 const unsigned TReg = I.getOperand(2).getReg();
1327 const unsigned FReg = I.getOperand(3).getReg();
1328
1329 unsigned CSelOpc = 0;
1330
1331 if (Ty == LLT::scalar(32)) {
1332 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001333 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001334 CSelOpc = AArch64::CSELXr;
1335 } else {
1336 return false;
1337 }
1338
1339 MachineInstr &TstMI =
1340 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1341 .addDef(AArch64::WZR)
1342 .addUse(CondReg)
1343 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1344
1345 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1346 .addDef(I.getOperand(0).getReg())
1347 .addUse(TReg)
1348 .addUse(FReg)
1349 .addImm(AArch64CC::NE);
1350
1351 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1352 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1353
1354 I.eraseFromParent();
1355 return true;
1356 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001357 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001358 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001359 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001360 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001361 return false;
1362 }
1363
1364 unsigned CmpOpc = 0;
1365 unsigned ZReg = 0;
1366
1367 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1368 if (CmpTy == LLT::scalar(32)) {
1369 CmpOpc = AArch64::SUBSWrr;
1370 ZReg = AArch64::WZR;
1371 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1372 CmpOpc = AArch64::SUBSXrr;
1373 ZReg = AArch64::XZR;
1374 } else {
1375 return false;
1376 }
1377
Kristof Beyls22524402017-01-05 10:16:08 +00001378 // CSINC increments the result by one when the condition code is false.
1379 // Therefore, we have to invert the predicate to get an increment by 1 when
1380 // the predicate is true.
1381 const AArch64CC::CondCode invCC =
1382 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1383 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001384
1385 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1386 .addDef(ZReg)
1387 .addUse(I.getOperand(2).getReg())
1388 .addUse(I.getOperand(3).getReg());
1389
1390 MachineInstr &CSetMI =
1391 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1392 .addDef(I.getOperand(0).getReg())
1393 .addUse(AArch64::WZR)
1394 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001395 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001396
1397 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1398 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1399
1400 I.eraseFromParent();
1401 return true;
1402 }
1403
Tim Northover7dd378d2016-10-12 22:49:07 +00001404 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001405 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001406 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001407 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001408 return false;
1409 }
1410
1411 unsigned CmpOpc = 0;
1412 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1413 if (CmpTy == LLT::scalar(32)) {
1414 CmpOpc = AArch64::FCMPSrr;
1415 } else if (CmpTy == LLT::scalar(64)) {
1416 CmpOpc = AArch64::FCMPDrr;
1417 } else {
1418 return false;
1419 }
1420
1421 // FIXME: regbank
1422
1423 AArch64CC::CondCode CC1, CC2;
1424 changeFCMPPredToAArch64CC(
1425 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1426
1427 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1428 .addUse(I.getOperand(2).getReg())
1429 .addUse(I.getOperand(3).getReg());
1430
1431 const unsigned DefReg = I.getOperand(0).getReg();
1432 unsigned Def1Reg = DefReg;
1433 if (CC2 != AArch64CC::AL)
1434 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1435
1436 MachineInstr &CSetMI =
1437 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1438 .addDef(Def1Reg)
1439 .addUse(AArch64::WZR)
1440 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001441 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001442
1443 if (CC2 != AArch64CC::AL) {
1444 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1445 MachineInstr &CSet2MI =
1446 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1447 .addDef(Def2Reg)
1448 .addUse(AArch64::WZR)
1449 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001450 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001451 MachineInstr &OrMI =
1452 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1453 .addDef(DefReg)
1454 .addUse(Def1Reg)
1455 .addUse(Def2Reg);
1456 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1457 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1458 }
1459
1460 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1461 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1462
1463 I.eraseFromParent();
1464 return true;
1465 }
Tim Northovere9600d82017-02-08 17:57:27 +00001466 case TargetOpcode::G_VASTART:
1467 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1468 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001469 case TargetOpcode::G_IMPLICIT_DEF:
1470 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001471 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1472 const unsigned DstReg = I.getOperand(0).getReg();
1473 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1474 const TargetRegisterClass *DstRC =
1475 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1476 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001477 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001478 }
1479
1480 return false;
1481}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001482
1483/// SelectArithImmed - Select an immediate value that can be represented as
1484/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1485/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001486InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001487AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001488 MachineInstr &MI = *Root.getParent();
1489 MachineBasicBlock &MBB = *MI.getParent();
1490 MachineFunction &MF = *MBB.getParent();
1491 MachineRegisterInfo &MRI = MF.getRegInfo();
1492
1493 // This function is called from the addsub_shifted_imm ComplexPattern,
1494 // which lists [imm] as the list of opcode it's interested in, however
1495 // we still need to check whether the operand is actually an immediate
1496 // here because the ComplexPattern opcode list is only used in
1497 // root-level opcode matching.
1498 uint64_t Immed;
1499 if (Root.isImm())
1500 Immed = Root.getImm();
1501 else if (Root.isCImm())
1502 Immed = Root.getCImm()->getZExtValue();
1503 else if (Root.isReg()) {
1504 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1505 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001506 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001507 MachineOperand &Op1 = Def->getOperand(1);
1508 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001509 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001510 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001511 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001512 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001513
1514 unsigned ShiftAmt;
1515
1516 if (Immed >> 12 == 0) {
1517 ShiftAmt = 0;
1518 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1519 ShiftAmt = 12;
1520 Immed = Immed >> 12;
1521 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001522 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001523
1524 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001525 return {{
1526 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1527 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1528 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001529}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001530
Daniel Sandersea8711b2017-10-16 03:36:29 +00001531/// Select a "register plus unscaled signed 9-bit immediate" address. This
1532/// should only match when there is an offset that is not valid for a scaled
1533/// immediate addressing mode. The "Size" argument is the size in bytes of the
1534/// memory reference, which is needed here to know what is valid for a scaled
1535/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001536InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001537AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1538 unsigned Size) const {
1539 MachineRegisterInfo &MRI =
1540 Root.getParent()->getParent()->getParent()->getRegInfo();
1541
1542 if (!Root.isReg())
1543 return None;
1544
1545 if (!isBaseWithConstantOffset(Root, MRI))
1546 return None;
1547
1548 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1549 if (!RootDef)
1550 return None;
1551
1552 MachineOperand &OffImm = RootDef->getOperand(2);
1553 if (!OffImm.isReg())
1554 return None;
1555 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1556 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1557 return None;
1558 int64_t RHSC;
1559 MachineOperand &RHSOp1 = RHS->getOperand(1);
1560 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1561 return None;
1562 RHSC = RHSOp1.getCImm()->getSExtValue();
1563
1564 // If the offset is valid as a scaled immediate, don't match here.
1565 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1566 return None;
1567 if (RHSC >= -256 && RHSC < 256) {
1568 MachineOperand &Base = RootDef->getOperand(1);
1569 return {{
1570 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1571 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1572 }};
1573 }
1574 return None;
1575}
1576
1577/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1578/// "Size" argument is the size in bytes of the memory reference, which
1579/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001580InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001581AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1582 unsigned Size) const {
1583 MachineRegisterInfo &MRI =
1584 Root.getParent()->getParent()->getParent()->getRegInfo();
1585
1586 if (!Root.isReg())
1587 return None;
1588
1589 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1590 if (!RootDef)
1591 return None;
1592
1593 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1594 return {{
1595 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1596 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1597 }};
1598 }
1599
1600 if (isBaseWithConstantOffset(Root, MRI)) {
1601 MachineOperand &LHS = RootDef->getOperand(1);
1602 MachineOperand &RHS = RootDef->getOperand(2);
1603 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1604 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1605 if (LHSDef && RHSDef) {
1606 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1607 unsigned Scale = Log2_32(Size);
1608 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1609 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001610 return {{
1611 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1612 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1613 }};
1614
Daniel Sandersea8711b2017-10-16 03:36:29 +00001615 return {{
1616 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1617 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1618 }};
1619 }
1620 }
1621 }
1622
1623 // Before falling back to our general case, check if the unscaled
1624 // instructions can handle this. If so, that's preferable.
1625 if (selectAddrModeUnscaled(Root, Size).hasValue())
1626 return None;
1627
1628 return {{
1629 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1630 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1631 }};
1632}
1633
Volkan Kelesf7f25682018-01-16 18:44:05 +00001634void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1635 const MachineInstr &MI) const {
1636 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1637 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1638 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1639 assert(CstVal && "Expected constant value");
1640 MIB.addImm(CstVal.getValue());
1641}
1642
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001643namespace llvm {
1644InstructionSelector *
1645createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1646 AArch64Subtarget &Subtarget,
1647 AArch64RegisterBankInfo &RBI) {
1648 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1649}
1650}