blob: 64531d7e40179991749b467b0c8d98cbc4a1c75e [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
Quentin Colombetcb629a82016-10-12 03:57:49 +0000320static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
321 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
322 const RegisterBankInfo &RBI) {
323
324 unsigned DstReg = I.getOperand(0).getReg();
325 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
326 assert(I.isCopy() && "Generic operators do not allow physical registers");
327 return true;
328 }
329
330 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
331 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
Amara Emerson3838ed02018-02-02 18:03:30 +0000332 (void)DstSize;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000333 unsigned SrcReg = I.getOperand(1).getReg();
334 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
335 (void)SrcSize;
336 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
337 "No phys reg on generic operators");
338 assert(
339 (DstSize == SrcSize ||
340 // Copies are a mean to setup initial types, the number of
341 // bits may not exactly match.
342 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
343 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
344 // Copies are a mean to copy bits around, as long as we are
345 // on the same register class, that's fine. Otherwise, that
346 // means we need some SUBREG_TO_REG or AND & co.
347 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
348 "Copy with different width?!");
349 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
350 "GPRs cannot get more than 64-bit width values");
Quentin Colombetcb629a82016-10-12 03:57:49 +0000351
Amara Emerson3838ed02018-02-02 18:03:30 +0000352 const TargetRegisterClass *RC = getRegClassForTypeOnBank(
353 MRI.getType(DstReg), RegBank, RBI, /* GetAllRegSet */ true);
354 if (!RC) {
355 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
356 return false;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000357 }
358
Amara Emerson7e9f3482018-02-18 17:10:49 +0000359 if (!TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
360 const RegClassOrRegBank &RegClassOrBank =
361 MRI.getRegClassOrRegBank(SrcReg);
362
363 const TargetRegisterClass *SrcRC =
364 RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
365 const RegisterBank *RB = nullptr;
366 if (!SrcRC) {
367 RB = RegClassOrBank.get<const RegisterBank *>();
368 SrcRC = getRegClassForTypeOnBank(MRI.getType(SrcReg), *RB, RBI, true);
369 }
370 // Copies from fpr16 to gpr32 need to use SUBREG_TO_REG.
371 if (RC == &AArch64::GPR32allRegClass && SrcRC == &AArch64::FPR16RegClass) {
372 unsigned PromoteReg = MRI.createVirtualRegister(&AArch64::FPR32RegClass);
373 BuildMI(*I.getParent(), I, I.getDebugLoc(),
374 TII.get(AArch64::SUBREG_TO_REG))
375 .addDef(PromoteReg)
376 .addImm(0)
377 .addUse(SrcReg)
378 .addImm(AArch64::hsub);
379 MachineOperand &RegOp = I.getOperand(1);
380 RegOp.setReg(PromoteReg);
381 }
382 }
383
Quentin Colombetcb629a82016-10-12 03:57:49 +0000384 // No need to constrain SrcReg. It will get constrained when
385 // we hit another of its use or its defs.
386 // Copies do not have constraints.
387 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
388 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
389 << " operand\n");
390 return false;
391 }
392 I.setDesc(TII.get(AArch64::COPY));
393 return true;
394}
395
Tim Northover69271c62016-10-12 22:49:11 +0000396static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
397 if (!DstTy.isScalar() || !SrcTy.isScalar())
398 return GenericOpc;
399
400 const unsigned DstSize = DstTy.getSizeInBits();
401 const unsigned SrcSize = SrcTy.getSizeInBits();
402
403 switch (DstSize) {
404 case 32:
405 switch (SrcSize) {
406 case 32:
407 switch (GenericOpc) {
408 case TargetOpcode::G_SITOFP:
409 return AArch64::SCVTFUWSri;
410 case TargetOpcode::G_UITOFP:
411 return AArch64::UCVTFUWSri;
412 case TargetOpcode::G_FPTOSI:
413 return AArch64::FCVTZSUWSr;
414 case TargetOpcode::G_FPTOUI:
415 return AArch64::FCVTZUUWSr;
416 default:
417 return GenericOpc;
418 }
419 case 64:
420 switch (GenericOpc) {
421 case TargetOpcode::G_SITOFP:
422 return AArch64::SCVTFUXSri;
423 case TargetOpcode::G_UITOFP:
424 return AArch64::UCVTFUXSri;
425 case TargetOpcode::G_FPTOSI:
426 return AArch64::FCVTZSUWDr;
427 case TargetOpcode::G_FPTOUI:
428 return AArch64::FCVTZUUWDr;
429 default:
430 return GenericOpc;
431 }
432 default:
433 return GenericOpc;
434 }
435 case 64:
436 switch (SrcSize) {
437 case 32:
438 switch (GenericOpc) {
439 case TargetOpcode::G_SITOFP:
440 return AArch64::SCVTFUWDri;
441 case TargetOpcode::G_UITOFP:
442 return AArch64::UCVTFUWDri;
443 case TargetOpcode::G_FPTOSI:
444 return AArch64::FCVTZSUXSr;
445 case TargetOpcode::G_FPTOUI:
446 return AArch64::FCVTZUUXSr;
447 default:
448 return GenericOpc;
449 }
450 case 64:
451 switch (GenericOpc) {
452 case TargetOpcode::G_SITOFP:
453 return AArch64::SCVTFUXDri;
454 case TargetOpcode::G_UITOFP:
455 return AArch64::UCVTFUXDri;
456 case TargetOpcode::G_FPTOSI:
457 return AArch64::FCVTZSUXDr;
458 case TargetOpcode::G_FPTOUI:
459 return AArch64::FCVTZUUXDr;
460 default:
461 return GenericOpc;
462 }
463 default:
464 return GenericOpc;
465 }
466 default:
467 return GenericOpc;
468 };
469 return GenericOpc;
470}
471
Tim Northover6c02ad52016-10-12 22:49:04 +0000472static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
473 switch (P) {
474 default:
475 llvm_unreachable("Unknown condition code!");
476 case CmpInst::ICMP_NE:
477 return AArch64CC::NE;
478 case CmpInst::ICMP_EQ:
479 return AArch64CC::EQ;
480 case CmpInst::ICMP_SGT:
481 return AArch64CC::GT;
482 case CmpInst::ICMP_SGE:
483 return AArch64CC::GE;
484 case CmpInst::ICMP_SLT:
485 return AArch64CC::LT;
486 case CmpInst::ICMP_SLE:
487 return AArch64CC::LE;
488 case CmpInst::ICMP_UGT:
489 return AArch64CC::HI;
490 case CmpInst::ICMP_UGE:
491 return AArch64CC::HS;
492 case CmpInst::ICMP_ULT:
493 return AArch64CC::LO;
494 case CmpInst::ICMP_ULE:
495 return AArch64CC::LS;
496 }
497}
498
Tim Northover7dd378d2016-10-12 22:49:07 +0000499static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
500 AArch64CC::CondCode &CondCode,
501 AArch64CC::CondCode &CondCode2) {
502 CondCode2 = AArch64CC::AL;
503 switch (P) {
504 default:
505 llvm_unreachable("Unknown FP condition!");
506 case CmpInst::FCMP_OEQ:
507 CondCode = AArch64CC::EQ;
508 break;
509 case CmpInst::FCMP_OGT:
510 CondCode = AArch64CC::GT;
511 break;
512 case CmpInst::FCMP_OGE:
513 CondCode = AArch64CC::GE;
514 break;
515 case CmpInst::FCMP_OLT:
516 CondCode = AArch64CC::MI;
517 break;
518 case CmpInst::FCMP_OLE:
519 CondCode = AArch64CC::LS;
520 break;
521 case CmpInst::FCMP_ONE:
522 CondCode = AArch64CC::MI;
523 CondCode2 = AArch64CC::GT;
524 break;
525 case CmpInst::FCMP_ORD:
526 CondCode = AArch64CC::VC;
527 break;
528 case CmpInst::FCMP_UNO:
529 CondCode = AArch64CC::VS;
530 break;
531 case CmpInst::FCMP_UEQ:
532 CondCode = AArch64CC::EQ;
533 CondCode2 = AArch64CC::VS;
534 break;
535 case CmpInst::FCMP_UGT:
536 CondCode = AArch64CC::HI;
537 break;
538 case CmpInst::FCMP_UGE:
539 CondCode = AArch64CC::PL;
540 break;
541 case CmpInst::FCMP_ULT:
542 CondCode = AArch64CC::LT;
543 break;
544 case CmpInst::FCMP_ULE:
545 CondCode = AArch64CC::LE;
546 break;
547 case CmpInst::FCMP_UNE:
548 CondCode = AArch64CC::NE;
549 break;
550 }
551}
552
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000553bool AArch64InstructionSelector::selectCompareBranch(
554 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
555
556 const unsigned CondReg = I.getOperand(0).getReg();
557 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
558 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000559 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
560 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000561 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
562 return false;
563
564 unsigned LHS = CCMI->getOperand(2).getReg();
565 unsigned RHS = CCMI->getOperand(3).getReg();
566 if (!getConstantVRegVal(RHS, MRI))
567 std::swap(RHS, LHS);
568
569 const auto RHSImm = getConstantVRegVal(RHS, MRI);
570 if (!RHSImm || *RHSImm != 0)
571 return false;
572
573 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
574 if (RB.getID() != AArch64::GPRRegBankID)
575 return false;
576
577 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
578 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
579 return false;
580
581 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
582 unsigned CBOpc = 0;
583 if (CmpWidth <= 32)
584 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
585 else if (CmpWidth == 64)
586 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
587 else
588 return false;
589
Aditya Nandakumar18b3f9d2018-01-17 19:31:33 +0000590 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
591 .addUse(LHS)
592 .addMBB(DestMBB)
593 .constrainAllUses(TII, TRI, RBI);
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000594
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000595 I.eraseFromParent();
596 return true;
597}
598
Tim Northovere9600d82017-02-08 17:57:27 +0000599bool AArch64InstructionSelector::selectVaStartAAPCS(
600 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
601 return false;
602}
603
604bool AArch64InstructionSelector::selectVaStartDarwin(
605 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
606 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
607 unsigned ListReg = I.getOperand(0).getReg();
608
609 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
610
611 auto MIB =
612 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
613 .addDef(ArgsAddrReg)
614 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
615 .addImm(0)
616 .addImm(0);
617
618 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
619
620 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
621 .addUse(ArgsAddrReg)
622 .addUse(ListReg)
623 .addImm(0)
624 .addMemOperand(*I.memoperands_begin());
625
626 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
627 I.eraseFromParent();
628 return true;
629}
630
Daniel Sandersf76f3152017-11-16 00:46:35 +0000631bool AArch64InstructionSelector::select(MachineInstr &I,
632 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000633 assert(I.getParent() && "Instruction should be in a basic block!");
634 assert(I.getParent()->getParent() && "Instruction should be in a function!");
635
636 MachineBasicBlock &MBB = *I.getParent();
637 MachineFunction &MF = *MBB.getParent();
638 MachineRegisterInfo &MRI = MF.getRegInfo();
639
Tim Northovercdf23f12016-10-31 18:30:59 +0000640 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000641 // G_PHI requires same handling as PHI
642 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000643 // Certain non-generic instructions also need some special handling.
644
645 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
646 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000647
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000648 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000649 const unsigned DefReg = I.getOperand(0).getReg();
650 const LLT DefTy = MRI.getType(DefReg);
651
652 const TargetRegisterClass *DefRC = nullptr;
653 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
654 DefRC = TRI.getRegClass(DefReg);
655 } else {
656 const RegClassOrRegBank &RegClassOrBank =
657 MRI.getRegClassOrRegBank(DefReg);
658
659 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
660 if (!DefRC) {
661 if (!DefTy.isValid()) {
662 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
663 return false;
664 }
665 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
666 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
667 if (!DefRC) {
668 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
669 return false;
670 }
671 }
672 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000673 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000674
675 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
676 }
677
678 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000679 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000680
681 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000682 }
683
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000684
685 if (I.getNumOperands() != I.getNumExplicitOperands()) {
686 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
687 return false;
688 }
689
Daniel Sandersf76f3152017-11-16 00:46:35 +0000690 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000691 return true;
692
Tim Northover32a078a2016-09-15 10:09:59 +0000693 LLT Ty =
694 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000695
Tim Northover69271c62016-10-12 22:49:11 +0000696 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000697 case TargetOpcode::G_BRCOND: {
698 if (Ty.getSizeInBits() > 32) {
699 // We shouldn't need this on AArch64, but it would be implemented as an
700 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
701 // bit being tested is < 32.
702 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
703 << ", expected at most 32-bits");
704 return false;
705 }
706
707 const unsigned CondReg = I.getOperand(0).getReg();
708 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
709
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000710 if (selectCompareBranch(I, MF, MRI))
711 return true;
712
Tim Northover5e3dbf32016-10-12 22:49:01 +0000713 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
714 .addUse(CondReg)
715 .addImm(/*bit offset=*/0)
716 .addMBB(DestMBB);
717
718 I.eraseFromParent();
719 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
720 }
721
Kristof Beyls65a12c02017-01-30 09:13:18 +0000722 case TargetOpcode::G_BRINDIRECT: {
723 I.setDesc(TII.get(AArch64::BR));
724 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
725 }
726
Tim Northover4494d692016-10-18 19:47:57 +0000727 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000728 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000729 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
730
731 const LLT s32 = LLT::scalar(32);
732 const LLT s64 = LLT::scalar(64);
733 const LLT p0 = LLT::pointer(0, 64);
734
735 const unsigned DefReg = I.getOperand(0).getReg();
736 const LLT DefTy = MRI.getType(DefReg);
737 const unsigned DefSize = DefTy.getSizeInBits();
738 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
739
740 // FIXME: Redundant check, but even less readable when factored out.
741 if (isFP) {
742 if (Ty != s32 && Ty != s64) {
743 DEBUG(dbgs() << "Unable to materialize FP " << Ty
744 << " constant, expected: " << s32 << " or " << s64
745 << '\n');
746 return false;
747 }
748
749 if (RB.getID() != AArch64::FPRRegBankID) {
750 DEBUG(dbgs() << "Unable to materialize FP " << Ty
751 << " constant on bank: " << RB << ", expected: FPR\n");
752 return false;
753 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000754
755 // The case when we have 0.0 is covered by tablegen. Reject it here so we
756 // can be sure tablegen works correctly and isn't rescued by this code.
757 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
758 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000759 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000760 // s32 and s64 are covered by tablegen.
761 if (Ty != p0) {
Tim Northover4494d692016-10-18 19:47:57 +0000762 DEBUG(dbgs() << "Unable to materialize integer " << Ty
763 << " constant, expected: " << s32 << ", " << s64 << ", or "
764 << p0 << '\n');
765 return false;
766 }
767
768 if (RB.getID() != AArch64::GPRRegBankID) {
769 DEBUG(dbgs() << "Unable to materialize integer " << Ty
770 << " constant on bank: " << RB << ", expected: GPR\n");
771 return false;
772 }
773 }
774
775 const unsigned MovOpc =
776 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
777
778 I.setDesc(TII.get(MovOpc));
779
780 if (isFP) {
781 const TargetRegisterClass &GPRRC =
782 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
783 const TargetRegisterClass &FPRRC =
784 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
785
786 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
787 MachineOperand &RegOp = I.getOperand(0);
788 RegOp.setReg(DefGPRReg);
789
790 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
791 TII.get(AArch64::COPY))
792 .addDef(DefReg)
793 .addUse(DefGPRReg);
794
795 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
796 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
797 return false;
798 }
799
800 MachineOperand &ImmOp = I.getOperand(1);
801 // FIXME: Is going through int64_t always correct?
802 ImmOp.ChangeToImmediate(
803 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000804 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000805 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
806 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000807 } else if (I.getOperand(1).isImm()) {
808 uint64_t Val = I.getOperand(1).getImm();
809 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000810 }
811
812 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
813 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000814 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000815 case TargetOpcode::G_EXTRACT: {
816 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000817 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
818 unsigned SrcSize = SrcTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000819 // Larger extracts are vectors, same-size extracts should be something else
820 // by now (either split up or simplified to a COPY).
821 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
822 return false;
823
Amara Emersonbc03bae2018-02-18 17:03:02 +0000824 I.setDesc(TII.get(SrcSize == 64 ? AArch64::UBFMXri : AArch64::UBFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000825 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
826 Ty.getSizeInBits() - 1);
827
Amara Emersonbc03bae2018-02-18 17:03:02 +0000828 if (SrcSize < 64) {
829 assert(SrcSize == 32 && DstTy.getSizeInBits() == 16 &&
830 "unexpected G_EXTRACT types");
831 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
832 }
833
Tim Northover7b6d66c2017-07-20 22:58:38 +0000834 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
835 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
836 TII.get(AArch64::COPY))
837 .addDef(I.getOperand(0).getReg())
838 .addUse(DstReg, 0, AArch64::sub_32);
839 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
840 AArch64::GPR32RegClass, MRI);
841 I.getOperand(0).setReg(DstReg);
842
843 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
844 }
845
846 case TargetOpcode::G_INSERT: {
847 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000848 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
849 unsigned DstSize = DstTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000850 // Larger inserts are vectors, same-size ones should be something else by
851 // now (split up or turned into COPYs).
852 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
853 return false;
854
Amara Emersonbc03bae2018-02-18 17:03:02 +0000855 I.setDesc(TII.get(DstSize == 64 ? AArch64::BFMXri : AArch64::BFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000856 unsigned LSB = I.getOperand(3).getImm();
857 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
Amara Emersonbc03bae2018-02-18 17:03:02 +0000858 I.getOperand(3).setImm((DstSize - LSB) % DstSize);
Tim Northover7b6d66c2017-07-20 22:58:38 +0000859 MachineInstrBuilder(MF, I).addImm(Width - 1);
860
Amara Emersonbc03bae2018-02-18 17:03:02 +0000861 if (DstSize < 64) {
862 assert(DstSize == 32 && SrcTy.getSizeInBits() == 16 &&
863 "unexpected G_INSERT types");
864 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
865 }
866
Tim Northover7b6d66c2017-07-20 22:58:38 +0000867 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
868 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
869 TII.get(AArch64::SUBREG_TO_REG))
870 .addDef(SrcReg)
871 .addImm(0)
872 .addUse(I.getOperand(2).getReg())
873 .addImm(AArch64::sub_32);
874 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
875 AArch64::GPR32RegClass, MRI);
876 I.getOperand(2).setReg(SrcReg);
877
878 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
879 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000880 case TargetOpcode::G_FRAME_INDEX: {
881 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000882 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000883 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000884 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000885 return false;
886 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000887 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000888
889 // MOs for a #0 shifted immediate.
890 I.addOperand(MachineOperand::CreateImm(0));
891 I.addOperand(MachineOperand::CreateImm(0));
892
893 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
894 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000895
896 case TargetOpcode::G_GLOBAL_VALUE: {
897 auto GV = I.getOperand(1).getGlobal();
898 if (GV->isThreadLocal()) {
899 // FIXME: we don't support TLS yet.
900 return false;
901 }
902 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000903 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000904 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000905 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +0000906 } else if (TM.getCodeModel() == CodeModel::Large) {
907 // Materialize the global using movz/movk instructions.
908 unsigned MovZDstReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
909 auto InsertPt = std::next(I.getIterator());
910 auto MovZ =
911 BuildMI(MBB, InsertPt, I.getDebugLoc(), TII.get(AArch64::MOVZXi))
912 .addDef(MovZDstReg);
913 MovZ->addOperand(MF, I.getOperand(1));
914 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
915 AArch64II::MO_NC);
916 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
917 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
918
919 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags,
920 unsigned Offset, unsigned ForceDstReg) {
921 unsigned DstReg =
922 ForceDstReg ? ForceDstReg
923 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
924 auto MovI = BuildMI(MBB, InsertPt, MovZ->getDebugLoc(),
925 TII.get(AArch64::MOVKXi))
926 .addDef(DstReg)
927 .addReg(SrcReg);
928 MovI->addOperand(MF, MachineOperand::CreateGA(
929 GV, MovZ->getOperand(1).getOffset(), Flags));
930 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
931 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
932 return DstReg;
933 };
934 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(),
935 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
936 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
937 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
938 I.eraseFromParent();
939 return true;
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000940 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000941 I.setDesc(TII.get(AArch64::MOVaddr));
942 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
943 MachineInstrBuilder MIB(MF, I);
944 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
945 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
946 }
947 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
948 }
949
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000950 case TargetOpcode::G_LOAD:
951 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000952 LLT MemTy = Ty;
953 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000954
Tim Northover5ae83502016-09-15 09:20:34 +0000955 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000956 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000957 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000958 return false;
959 }
960
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000961 auto &MemOp = **I.memoperands_begin();
962 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
963 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
964 return false;
965 }
966
Amara Emerson4f84f882018-01-24 20:35:37 +0000967 // FIXME: PR36018: Volatile loads in some cases are incorrectly selected by
968 // folding with an extend. Until we have a G_SEXTLOAD solution bail out if
969 // we hit one.
970 if (Opcode == TargetOpcode::G_LOAD && MemOp.isVolatile())
971 return false;
972
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000973 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000974#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000975 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000976 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000977 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
978 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000979 assert(MRI.getType(PtrReg).isPointer() &&
980 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000981#endif
982
983 const unsigned ValReg = I.getOperand(0).getReg();
984 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
985
986 const unsigned NewOpc =
987 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
988 if (NewOpc == I.getOpcode())
989 return false;
990
991 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000992
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000993 uint64_t Offset = 0;
994 auto *PtrMI = MRI.getVRegDef(PtrReg);
995
996 // Try to fold a GEP into our unsigned immediate addressing mode.
997 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
998 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
999 int64_t Imm = *COff;
1000 const unsigned Size = MemTy.getSizeInBits() / 8;
1001 const unsigned Scale = Log2_32(Size);
1002 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
1003 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
1004 I.getOperand(1).setReg(Ptr2Reg);
1005 PtrMI = MRI.getVRegDef(Ptr2Reg);
1006 Offset = Imm / Size;
1007 }
1008 }
1009 }
1010
Ahmed Bougachaf75782f2017-03-27 17:31:56 +00001011 // If we haven't folded anything into our addressing mode yet, try to fold
1012 // a frame index into the base+offset.
1013 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1014 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
1015
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001016 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +00001017
1018 // If we're storing a 0, use WZR/XZR.
1019 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
1020 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
1021 if (I.getOpcode() == AArch64::STRWui)
1022 I.getOperand(0).setReg(AArch64::WZR);
1023 else if (I.getOpcode() == AArch64::STRXui)
1024 I.getOperand(0).setReg(AArch64::XZR);
1025 }
1026 }
1027
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001028 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1029 }
1030
Tim Northover9dd78f82017-02-08 21:22:25 +00001031 case TargetOpcode::G_SMULH:
1032 case TargetOpcode::G_UMULH: {
1033 // Reject the various things we don't support yet.
1034 if (unsupportedBinOp(I, RBI, MRI, TRI))
1035 return false;
1036
1037 const unsigned DefReg = I.getOperand(0).getReg();
1038 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1039
1040 if (RB.getID() != AArch64::GPRRegBankID) {
1041 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
1042 return false;
1043 }
1044
1045 if (Ty != LLT::scalar(64)) {
1046 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1047 << ", expected: " << LLT::scalar(64) << '\n');
1048 return false;
1049 }
1050
1051 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1052 : AArch64::UMULHrr;
1053 I.setDesc(TII.get(NewOpc));
1054
1055 // Now that we selected an opcode, we need to constrain the register
1056 // operands to use appropriate classes.
1057 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1058 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001059 case TargetOpcode::G_FADD:
1060 case TargetOpcode::G_FSUB:
1061 case TargetOpcode::G_FMUL:
1062 case TargetOpcode::G_FDIV:
1063
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001064 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001065 case TargetOpcode::G_SHL:
1066 case TargetOpcode::G_LSHR:
1067 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001068 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001069 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001070 if (unsupportedBinOp(I, RBI, MRI, TRI))
1071 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001072
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001073 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001074
1075 const unsigned DefReg = I.getOperand(0).getReg();
1076 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1077
1078 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1079 if (NewOpc == I.getOpcode())
1080 return false;
1081
1082 I.setDesc(TII.get(NewOpc));
1083 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001084
1085 // Now that we selected an opcode, we need to constrain the register
1086 // operands to use appropriate classes.
1087 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1088 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001089
Tim Northover398c5f52017-02-14 20:56:29 +00001090 case TargetOpcode::G_PTR_MASK: {
1091 uint64_t Align = I.getOperand(2).getImm();
1092 if (Align >= 64 || Align == 0)
1093 return false;
1094
1095 uint64_t Mask = ~((1ULL << Align) - 1);
1096 I.setDesc(TII.get(AArch64::ANDXri));
1097 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1098
1099 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1100 }
Tim Northover037af52c2016-10-31 18:31:09 +00001101 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001102 case TargetOpcode::G_TRUNC: {
1103 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1104 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1105
1106 const unsigned DstReg = I.getOperand(0).getReg();
1107 const unsigned SrcReg = I.getOperand(1).getReg();
1108
1109 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1110 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1111
1112 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001113 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001114 return false;
1115 }
1116
1117 if (DstRB.getID() == AArch64::GPRRegBankID) {
1118 const TargetRegisterClass *DstRC =
1119 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1120 if (!DstRC)
1121 return false;
1122
1123 const TargetRegisterClass *SrcRC =
1124 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1125 if (!SrcRC)
1126 return false;
1127
1128 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1129 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001130 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001131 return false;
1132 }
1133
1134 if (DstRC == SrcRC) {
1135 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001136 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1137 SrcTy == LLT::scalar(64)) {
1138 llvm_unreachable("TableGen can import this case");
1139 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001140 } else if (DstRC == &AArch64::GPR32RegClass &&
1141 SrcRC == &AArch64::GPR64RegClass) {
1142 I.getOperand(1).setSubReg(AArch64::sub_32);
1143 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001144 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001145 return false;
1146 }
1147
1148 I.setDesc(TII.get(TargetOpcode::COPY));
1149 return true;
1150 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1151 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1152 I.setDesc(TII.get(AArch64::XTNv4i16));
1153 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1154 return true;
1155 }
1156 }
1157
1158 return false;
1159 }
1160
Tim Northover3d38b3a2016-10-11 20:50:21 +00001161 case TargetOpcode::G_ANYEXT: {
1162 const unsigned DstReg = I.getOperand(0).getReg();
1163 const unsigned SrcReg = I.getOperand(1).getReg();
1164
Quentin Colombetcb629a82016-10-12 03:57:49 +00001165 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1166 if (RBDst.getID() != AArch64::GPRRegBankID) {
1167 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1168 return false;
1169 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001170
Quentin Colombetcb629a82016-10-12 03:57:49 +00001171 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1172 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1173 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001174 return false;
1175 }
1176
1177 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1178
1179 if (DstSize == 0) {
1180 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1181 return false;
1182 }
1183
Quentin Colombetcb629a82016-10-12 03:57:49 +00001184 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001185 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1186 << ", expected: 32 or 64\n");
1187 return false;
1188 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001189 // At this point G_ANYEXT is just like a plain COPY, but we need
1190 // to explicitly form the 64-bit value if any.
1191 if (DstSize > 32) {
1192 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1193 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1194 .addDef(ExtSrc)
1195 .addImm(0)
1196 .addUse(SrcReg)
1197 .addImm(AArch64::sub_32);
1198 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001199 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001200 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001201 }
1202
1203 case TargetOpcode::G_ZEXT:
1204 case TargetOpcode::G_SEXT: {
1205 unsigned Opcode = I.getOpcode();
1206 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1207 SrcTy = MRI.getType(I.getOperand(1).getReg());
1208 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1209 const unsigned DefReg = I.getOperand(0).getReg();
1210 const unsigned SrcReg = I.getOperand(1).getReg();
1211 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1212
1213 if (RB.getID() != AArch64::GPRRegBankID) {
1214 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1215 << ", expected: GPR\n");
1216 return false;
1217 }
1218
1219 MachineInstr *ExtI;
1220 if (DstTy == LLT::scalar(64)) {
1221 // FIXME: Can we avoid manually doing this?
1222 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1223 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1224 << " operand\n");
1225 return false;
1226 }
1227
1228 const unsigned SrcXReg =
1229 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1230 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1231 .addDef(SrcXReg)
1232 .addImm(0)
1233 .addUse(SrcReg)
1234 .addImm(AArch64::sub_32);
1235
1236 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1237 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1238 .addDef(DefReg)
1239 .addUse(SrcXReg)
1240 .addImm(0)
1241 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001242 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001243 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1244 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1245 .addDef(DefReg)
1246 .addUse(SrcReg)
1247 .addImm(0)
1248 .addImm(SrcTy.getSizeInBits() - 1);
1249 } else {
1250 return false;
1251 }
1252
1253 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1254
1255 I.eraseFromParent();
1256 return true;
1257 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001258
Tim Northover69271c62016-10-12 22:49:11 +00001259 case TargetOpcode::G_SITOFP:
1260 case TargetOpcode::G_UITOFP:
1261 case TargetOpcode::G_FPTOSI:
1262 case TargetOpcode::G_FPTOUI: {
1263 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1264 SrcTy = MRI.getType(I.getOperand(1).getReg());
1265 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1266 if (NewOpc == Opcode)
1267 return false;
1268
1269 I.setDesc(TII.get(NewOpc));
1270 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1271
1272 return true;
1273 }
1274
1275
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001276 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001277 // The importer is currently unable to import pointer types since they
1278 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001279 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001280
Daniel Sandersedd07842017-08-17 09:26:14 +00001281 case TargetOpcode::G_BITCAST:
1282 // Imported SelectionDAG rules can handle every bitcast except those that
1283 // bitcast from a type to the same type. Ideally, these shouldn't occur
1284 // but we might not run an optimizer that deletes them.
1285 if (MRI.getType(I.getOperand(0).getReg()) ==
1286 MRI.getType(I.getOperand(1).getReg()))
1287 return selectCopy(I, TII, MRI, TRI, RBI);
1288 return false;
1289
Tim Northover9ac0eba2016-11-08 00:45:29 +00001290 case TargetOpcode::G_SELECT: {
1291 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1292 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1293 << ", expected: " << LLT::scalar(1) << '\n');
1294 return false;
1295 }
1296
1297 const unsigned CondReg = I.getOperand(1).getReg();
1298 const unsigned TReg = I.getOperand(2).getReg();
1299 const unsigned FReg = I.getOperand(3).getReg();
1300
1301 unsigned CSelOpc = 0;
1302
1303 if (Ty == LLT::scalar(32)) {
1304 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001305 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001306 CSelOpc = AArch64::CSELXr;
1307 } else {
1308 return false;
1309 }
1310
1311 MachineInstr &TstMI =
1312 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1313 .addDef(AArch64::WZR)
1314 .addUse(CondReg)
1315 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1316
1317 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1318 .addDef(I.getOperand(0).getReg())
1319 .addUse(TReg)
1320 .addUse(FReg)
1321 .addImm(AArch64CC::NE);
1322
1323 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1324 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1325
1326 I.eraseFromParent();
1327 return true;
1328 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001329 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001330 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001331 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001332 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001333 return false;
1334 }
1335
1336 unsigned CmpOpc = 0;
1337 unsigned ZReg = 0;
1338
1339 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1340 if (CmpTy == LLT::scalar(32)) {
1341 CmpOpc = AArch64::SUBSWrr;
1342 ZReg = AArch64::WZR;
1343 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1344 CmpOpc = AArch64::SUBSXrr;
1345 ZReg = AArch64::XZR;
1346 } else {
1347 return false;
1348 }
1349
Kristof Beyls22524402017-01-05 10:16:08 +00001350 // CSINC increments the result by one when the condition code is false.
1351 // Therefore, we have to invert the predicate to get an increment by 1 when
1352 // the predicate is true.
1353 const AArch64CC::CondCode invCC =
1354 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1355 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001356
1357 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1358 .addDef(ZReg)
1359 .addUse(I.getOperand(2).getReg())
1360 .addUse(I.getOperand(3).getReg());
1361
1362 MachineInstr &CSetMI =
1363 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1364 .addDef(I.getOperand(0).getReg())
1365 .addUse(AArch64::WZR)
1366 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001367 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001368
1369 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1370 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1371
1372 I.eraseFromParent();
1373 return true;
1374 }
1375
Tim Northover7dd378d2016-10-12 22:49:07 +00001376 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001377 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001378 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001379 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001380 return false;
1381 }
1382
1383 unsigned CmpOpc = 0;
1384 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1385 if (CmpTy == LLT::scalar(32)) {
1386 CmpOpc = AArch64::FCMPSrr;
1387 } else if (CmpTy == LLT::scalar(64)) {
1388 CmpOpc = AArch64::FCMPDrr;
1389 } else {
1390 return false;
1391 }
1392
1393 // FIXME: regbank
1394
1395 AArch64CC::CondCode CC1, CC2;
1396 changeFCMPPredToAArch64CC(
1397 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1398
1399 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1400 .addUse(I.getOperand(2).getReg())
1401 .addUse(I.getOperand(3).getReg());
1402
1403 const unsigned DefReg = I.getOperand(0).getReg();
1404 unsigned Def1Reg = DefReg;
1405 if (CC2 != AArch64CC::AL)
1406 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1407
1408 MachineInstr &CSetMI =
1409 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1410 .addDef(Def1Reg)
1411 .addUse(AArch64::WZR)
1412 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001413 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001414
1415 if (CC2 != AArch64CC::AL) {
1416 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1417 MachineInstr &CSet2MI =
1418 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1419 .addDef(Def2Reg)
1420 .addUse(AArch64::WZR)
1421 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001422 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001423 MachineInstr &OrMI =
1424 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1425 .addDef(DefReg)
1426 .addUse(Def1Reg)
1427 .addUse(Def2Reg);
1428 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1429 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1430 }
1431
1432 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1433 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1434
1435 I.eraseFromParent();
1436 return true;
1437 }
Tim Northovere9600d82017-02-08 17:57:27 +00001438 case TargetOpcode::G_VASTART:
1439 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1440 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001441 case TargetOpcode::G_IMPLICIT_DEF:
1442 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001443 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1444 const unsigned DstReg = I.getOperand(0).getReg();
1445 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1446 const TargetRegisterClass *DstRC =
1447 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1448 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001449 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001450 }
1451
1452 return false;
1453}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001454
1455/// SelectArithImmed - Select an immediate value that can be represented as
1456/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1457/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001458InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001459AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001460 MachineInstr &MI = *Root.getParent();
1461 MachineBasicBlock &MBB = *MI.getParent();
1462 MachineFunction &MF = *MBB.getParent();
1463 MachineRegisterInfo &MRI = MF.getRegInfo();
1464
1465 // This function is called from the addsub_shifted_imm ComplexPattern,
1466 // which lists [imm] as the list of opcode it's interested in, however
1467 // we still need to check whether the operand is actually an immediate
1468 // here because the ComplexPattern opcode list is only used in
1469 // root-level opcode matching.
1470 uint64_t Immed;
1471 if (Root.isImm())
1472 Immed = Root.getImm();
1473 else if (Root.isCImm())
1474 Immed = Root.getCImm()->getZExtValue();
1475 else if (Root.isReg()) {
1476 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1477 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001478 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001479 MachineOperand &Op1 = Def->getOperand(1);
1480 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001481 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001482 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001483 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001484 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001485
1486 unsigned ShiftAmt;
1487
1488 if (Immed >> 12 == 0) {
1489 ShiftAmt = 0;
1490 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1491 ShiftAmt = 12;
1492 Immed = Immed >> 12;
1493 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001494 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001495
1496 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001497 return {{
1498 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1499 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1500 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001501}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001502
Daniel Sandersea8711b2017-10-16 03:36:29 +00001503/// Select a "register plus unscaled signed 9-bit immediate" address. This
1504/// should only match when there is an offset that is not valid for a scaled
1505/// immediate addressing mode. The "Size" argument is the size in bytes of the
1506/// memory reference, which is needed here to know what is valid for a scaled
1507/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001508InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001509AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1510 unsigned Size) const {
1511 MachineRegisterInfo &MRI =
1512 Root.getParent()->getParent()->getParent()->getRegInfo();
1513
1514 if (!Root.isReg())
1515 return None;
1516
1517 if (!isBaseWithConstantOffset(Root, MRI))
1518 return None;
1519
1520 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1521 if (!RootDef)
1522 return None;
1523
1524 MachineOperand &OffImm = RootDef->getOperand(2);
1525 if (!OffImm.isReg())
1526 return None;
1527 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1528 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1529 return None;
1530 int64_t RHSC;
1531 MachineOperand &RHSOp1 = RHS->getOperand(1);
1532 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1533 return None;
1534 RHSC = RHSOp1.getCImm()->getSExtValue();
1535
1536 // If the offset is valid as a scaled immediate, don't match here.
1537 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1538 return None;
1539 if (RHSC >= -256 && RHSC < 256) {
1540 MachineOperand &Base = RootDef->getOperand(1);
1541 return {{
1542 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1543 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1544 }};
1545 }
1546 return None;
1547}
1548
1549/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1550/// "Size" argument is the size in bytes of the memory reference, which
1551/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001552InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001553AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1554 unsigned Size) const {
1555 MachineRegisterInfo &MRI =
1556 Root.getParent()->getParent()->getParent()->getRegInfo();
1557
1558 if (!Root.isReg())
1559 return None;
1560
1561 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1562 if (!RootDef)
1563 return None;
1564
1565 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1566 return {{
1567 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1568 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1569 }};
1570 }
1571
1572 if (isBaseWithConstantOffset(Root, MRI)) {
1573 MachineOperand &LHS = RootDef->getOperand(1);
1574 MachineOperand &RHS = RootDef->getOperand(2);
1575 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1576 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1577 if (LHSDef && RHSDef) {
1578 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1579 unsigned Scale = Log2_32(Size);
1580 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1581 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001582 return {{
1583 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1584 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1585 }};
1586
Daniel Sandersea8711b2017-10-16 03:36:29 +00001587 return {{
1588 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1589 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1590 }};
1591 }
1592 }
1593 }
1594
1595 // Before falling back to our general case, check if the unscaled
1596 // instructions can handle this. If so, that's preferable.
1597 if (selectAddrModeUnscaled(Root, Size).hasValue())
1598 return None;
1599
1600 return {{
1601 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1602 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1603 }};
1604}
1605
Volkan Kelesf7f25682018-01-16 18:44:05 +00001606void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1607 const MachineInstr &MI) const {
1608 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1609 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1610 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1611 assert(CstVal && "Expected constant value");
1612 MIB.addImm(CstVal.getValue());
1613}
1614
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001615namespace llvm {
1616InstructionSelector *
1617createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1618 AArch64Subtarget &Subtarget,
1619 AArch64RegisterBankInfo &RBI) {
1620 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1621}
1622}