blob: 4e5af6fcbab180abe0bdf161ee426620246d4df5 [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
359 // No need to constrain SrcReg. It will get constrained when
360 // we hit another of its use or its defs.
361 // Copies do not have constraints.
362 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
363 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
364 << " operand\n");
365 return false;
366 }
367 I.setDesc(TII.get(AArch64::COPY));
368 return true;
369}
370
Tim Northover69271c62016-10-12 22:49:11 +0000371static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
372 if (!DstTy.isScalar() || !SrcTy.isScalar())
373 return GenericOpc;
374
375 const unsigned DstSize = DstTy.getSizeInBits();
376 const unsigned SrcSize = SrcTy.getSizeInBits();
377
378 switch (DstSize) {
379 case 32:
380 switch (SrcSize) {
381 case 32:
382 switch (GenericOpc) {
383 case TargetOpcode::G_SITOFP:
384 return AArch64::SCVTFUWSri;
385 case TargetOpcode::G_UITOFP:
386 return AArch64::UCVTFUWSri;
387 case TargetOpcode::G_FPTOSI:
388 return AArch64::FCVTZSUWSr;
389 case TargetOpcode::G_FPTOUI:
390 return AArch64::FCVTZUUWSr;
391 default:
392 return GenericOpc;
393 }
394 case 64:
395 switch (GenericOpc) {
396 case TargetOpcode::G_SITOFP:
397 return AArch64::SCVTFUXSri;
398 case TargetOpcode::G_UITOFP:
399 return AArch64::UCVTFUXSri;
400 case TargetOpcode::G_FPTOSI:
401 return AArch64::FCVTZSUWDr;
402 case TargetOpcode::G_FPTOUI:
403 return AArch64::FCVTZUUWDr;
404 default:
405 return GenericOpc;
406 }
407 default:
408 return GenericOpc;
409 }
410 case 64:
411 switch (SrcSize) {
412 case 32:
413 switch (GenericOpc) {
414 case TargetOpcode::G_SITOFP:
415 return AArch64::SCVTFUWDri;
416 case TargetOpcode::G_UITOFP:
417 return AArch64::UCVTFUWDri;
418 case TargetOpcode::G_FPTOSI:
419 return AArch64::FCVTZSUXSr;
420 case TargetOpcode::G_FPTOUI:
421 return AArch64::FCVTZUUXSr;
422 default:
423 return GenericOpc;
424 }
425 case 64:
426 switch (GenericOpc) {
427 case TargetOpcode::G_SITOFP:
428 return AArch64::SCVTFUXDri;
429 case TargetOpcode::G_UITOFP:
430 return AArch64::UCVTFUXDri;
431 case TargetOpcode::G_FPTOSI:
432 return AArch64::FCVTZSUXDr;
433 case TargetOpcode::G_FPTOUI:
434 return AArch64::FCVTZUUXDr;
435 default:
436 return GenericOpc;
437 }
438 default:
439 return GenericOpc;
440 }
441 default:
442 return GenericOpc;
443 };
444 return GenericOpc;
445}
446
Tim Northover6c02ad52016-10-12 22:49:04 +0000447static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
448 switch (P) {
449 default:
450 llvm_unreachable("Unknown condition code!");
451 case CmpInst::ICMP_NE:
452 return AArch64CC::NE;
453 case CmpInst::ICMP_EQ:
454 return AArch64CC::EQ;
455 case CmpInst::ICMP_SGT:
456 return AArch64CC::GT;
457 case CmpInst::ICMP_SGE:
458 return AArch64CC::GE;
459 case CmpInst::ICMP_SLT:
460 return AArch64CC::LT;
461 case CmpInst::ICMP_SLE:
462 return AArch64CC::LE;
463 case CmpInst::ICMP_UGT:
464 return AArch64CC::HI;
465 case CmpInst::ICMP_UGE:
466 return AArch64CC::HS;
467 case CmpInst::ICMP_ULT:
468 return AArch64CC::LO;
469 case CmpInst::ICMP_ULE:
470 return AArch64CC::LS;
471 }
472}
473
Tim Northover7dd378d2016-10-12 22:49:07 +0000474static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
475 AArch64CC::CondCode &CondCode,
476 AArch64CC::CondCode &CondCode2) {
477 CondCode2 = AArch64CC::AL;
478 switch (P) {
479 default:
480 llvm_unreachable("Unknown FP condition!");
481 case CmpInst::FCMP_OEQ:
482 CondCode = AArch64CC::EQ;
483 break;
484 case CmpInst::FCMP_OGT:
485 CondCode = AArch64CC::GT;
486 break;
487 case CmpInst::FCMP_OGE:
488 CondCode = AArch64CC::GE;
489 break;
490 case CmpInst::FCMP_OLT:
491 CondCode = AArch64CC::MI;
492 break;
493 case CmpInst::FCMP_OLE:
494 CondCode = AArch64CC::LS;
495 break;
496 case CmpInst::FCMP_ONE:
497 CondCode = AArch64CC::MI;
498 CondCode2 = AArch64CC::GT;
499 break;
500 case CmpInst::FCMP_ORD:
501 CondCode = AArch64CC::VC;
502 break;
503 case CmpInst::FCMP_UNO:
504 CondCode = AArch64CC::VS;
505 break;
506 case CmpInst::FCMP_UEQ:
507 CondCode = AArch64CC::EQ;
508 CondCode2 = AArch64CC::VS;
509 break;
510 case CmpInst::FCMP_UGT:
511 CondCode = AArch64CC::HI;
512 break;
513 case CmpInst::FCMP_UGE:
514 CondCode = AArch64CC::PL;
515 break;
516 case CmpInst::FCMP_ULT:
517 CondCode = AArch64CC::LT;
518 break;
519 case CmpInst::FCMP_ULE:
520 CondCode = AArch64CC::LE;
521 break;
522 case CmpInst::FCMP_UNE:
523 CondCode = AArch64CC::NE;
524 break;
525 }
526}
527
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000528bool AArch64InstructionSelector::selectCompareBranch(
529 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
530
531 const unsigned CondReg = I.getOperand(0).getReg();
532 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
533 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000534 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
535 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000536 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
537 return false;
538
539 unsigned LHS = CCMI->getOperand(2).getReg();
540 unsigned RHS = CCMI->getOperand(3).getReg();
541 if (!getConstantVRegVal(RHS, MRI))
542 std::swap(RHS, LHS);
543
544 const auto RHSImm = getConstantVRegVal(RHS, MRI);
545 if (!RHSImm || *RHSImm != 0)
546 return false;
547
548 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
549 if (RB.getID() != AArch64::GPRRegBankID)
550 return false;
551
552 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
553 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
554 return false;
555
556 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
557 unsigned CBOpc = 0;
558 if (CmpWidth <= 32)
559 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
560 else if (CmpWidth == 64)
561 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
562 else
563 return false;
564
Aditya Nandakumar18b3f9d2018-01-17 19:31:33 +0000565 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
566 .addUse(LHS)
567 .addMBB(DestMBB)
568 .constrainAllUses(TII, TRI, RBI);
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000569
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000570 I.eraseFromParent();
571 return true;
572}
573
Tim Northovere9600d82017-02-08 17:57:27 +0000574bool AArch64InstructionSelector::selectVaStartAAPCS(
575 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
576 return false;
577}
578
579bool AArch64InstructionSelector::selectVaStartDarwin(
580 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
581 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
582 unsigned ListReg = I.getOperand(0).getReg();
583
584 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
585
586 auto MIB =
587 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
588 .addDef(ArgsAddrReg)
589 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
590 .addImm(0)
591 .addImm(0);
592
593 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
594
595 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
596 .addUse(ArgsAddrReg)
597 .addUse(ListReg)
598 .addImm(0)
599 .addMemOperand(*I.memoperands_begin());
600
601 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
602 I.eraseFromParent();
603 return true;
604}
605
Daniel Sandersf76f3152017-11-16 00:46:35 +0000606bool AArch64InstructionSelector::select(MachineInstr &I,
607 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000608 assert(I.getParent() && "Instruction should be in a basic block!");
609 assert(I.getParent()->getParent() && "Instruction should be in a function!");
610
611 MachineBasicBlock &MBB = *I.getParent();
612 MachineFunction &MF = *MBB.getParent();
613 MachineRegisterInfo &MRI = MF.getRegInfo();
614
Tim Northovercdf23f12016-10-31 18:30:59 +0000615 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000616 // G_PHI requires same handling as PHI
617 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000618 // Certain non-generic instructions also need some special handling.
619
620 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
621 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000622
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000623 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000624 const unsigned DefReg = I.getOperand(0).getReg();
625 const LLT DefTy = MRI.getType(DefReg);
626
627 const TargetRegisterClass *DefRC = nullptr;
628 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
629 DefRC = TRI.getRegClass(DefReg);
630 } else {
631 const RegClassOrRegBank &RegClassOrBank =
632 MRI.getRegClassOrRegBank(DefReg);
633
634 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
635 if (!DefRC) {
636 if (!DefTy.isValid()) {
637 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
638 return false;
639 }
640 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
641 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
642 if (!DefRC) {
643 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
644 return false;
645 }
646 }
647 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000648 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000649
650 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
651 }
652
653 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000654 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000655
656 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000657 }
658
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000659
660 if (I.getNumOperands() != I.getNumExplicitOperands()) {
661 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
662 return false;
663 }
664
Daniel Sandersf76f3152017-11-16 00:46:35 +0000665 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000666 return true;
667
Tim Northover32a078a2016-09-15 10:09:59 +0000668 LLT Ty =
669 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000670
Tim Northover69271c62016-10-12 22:49:11 +0000671 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000672 case TargetOpcode::G_BRCOND: {
673 if (Ty.getSizeInBits() > 32) {
674 // We shouldn't need this on AArch64, but it would be implemented as an
675 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
676 // bit being tested is < 32.
677 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
678 << ", expected at most 32-bits");
679 return false;
680 }
681
682 const unsigned CondReg = I.getOperand(0).getReg();
683 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
684
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000685 if (selectCompareBranch(I, MF, MRI))
686 return true;
687
Tim Northover5e3dbf32016-10-12 22:49:01 +0000688 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
689 .addUse(CondReg)
690 .addImm(/*bit offset=*/0)
691 .addMBB(DestMBB);
692
693 I.eraseFromParent();
694 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
695 }
696
Kristof Beyls65a12c02017-01-30 09:13:18 +0000697 case TargetOpcode::G_BRINDIRECT: {
698 I.setDesc(TII.get(AArch64::BR));
699 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
700 }
701
Tim Northover4494d692016-10-18 19:47:57 +0000702 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000703 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000704 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
705
706 const LLT s32 = LLT::scalar(32);
707 const LLT s64 = LLT::scalar(64);
708 const LLT p0 = LLT::pointer(0, 64);
709
710 const unsigned DefReg = I.getOperand(0).getReg();
711 const LLT DefTy = MRI.getType(DefReg);
712 const unsigned DefSize = DefTy.getSizeInBits();
713 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
714
715 // FIXME: Redundant check, but even less readable when factored out.
716 if (isFP) {
717 if (Ty != s32 && Ty != s64) {
718 DEBUG(dbgs() << "Unable to materialize FP " << Ty
719 << " constant, expected: " << s32 << " or " << s64
720 << '\n');
721 return false;
722 }
723
724 if (RB.getID() != AArch64::FPRRegBankID) {
725 DEBUG(dbgs() << "Unable to materialize FP " << Ty
726 << " constant on bank: " << RB << ", expected: FPR\n");
727 return false;
728 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000729
730 // The case when we have 0.0 is covered by tablegen. Reject it here so we
731 // can be sure tablegen works correctly and isn't rescued by this code.
732 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
733 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000734 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000735 // s32 and s64 are covered by tablegen.
736 if (Ty != p0) {
Tim Northover4494d692016-10-18 19:47:57 +0000737 DEBUG(dbgs() << "Unable to materialize integer " << Ty
738 << " constant, expected: " << s32 << ", " << s64 << ", or "
739 << p0 << '\n');
740 return false;
741 }
742
743 if (RB.getID() != AArch64::GPRRegBankID) {
744 DEBUG(dbgs() << "Unable to materialize integer " << Ty
745 << " constant on bank: " << RB << ", expected: GPR\n");
746 return false;
747 }
748 }
749
750 const unsigned MovOpc =
751 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
752
753 I.setDesc(TII.get(MovOpc));
754
755 if (isFP) {
756 const TargetRegisterClass &GPRRC =
757 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
758 const TargetRegisterClass &FPRRC =
759 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
760
761 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
762 MachineOperand &RegOp = I.getOperand(0);
763 RegOp.setReg(DefGPRReg);
764
765 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
766 TII.get(AArch64::COPY))
767 .addDef(DefReg)
768 .addUse(DefGPRReg);
769
770 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
771 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
772 return false;
773 }
774
775 MachineOperand &ImmOp = I.getOperand(1);
776 // FIXME: Is going through int64_t always correct?
777 ImmOp.ChangeToImmediate(
778 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000779 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000780 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
781 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000782 } else if (I.getOperand(1).isImm()) {
783 uint64_t Val = I.getOperand(1).getImm();
784 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000785 }
786
787 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
788 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000789 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000790 case TargetOpcode::G_EXTRACT: {
791 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
792 // Larger extracts are vectors, same-size extracts should be something else
793 // by now (either split up or simplified to a COPY).
794 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
795 return false;
796
797 I.setDesc(TII.get(AArch64::UBFMXri));
798 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
799 Ty.getSizeInBits() - 1);
800
801 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
802 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
803 TII.get(AArch64::COPY))
804 .addDef(I.getOperand(0).getReg())
805 .addUse(DstReg, 0, AArch64::sub_32);
806 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
807 AArch64::GPR32RegClass, MRI);
808 I.getOperand(0).setReg(DstReg);
809
810 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
811 }
812
813 case TargetOpcode::G_INSERT: {
814 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
815 // Larger inserts are vectors, same-size ones should be something else by
816 // now (split up or turned into COPYs).
817 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
818 return false;
819
820 I.setDesc(TII.get(AArch64::BFMXri));
821 unsigned LSB = I.getOperand(3).getImm();
822 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
823 I.getOperand(3).setImm((64 - LSB) % 64);
824 MachineInstrBuilder(MF, I).addImm(Width - 1);
825
826 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
827 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
828 TII.get(AArch64::SUBREG_TO_REG))
829 .addDef(SrcReg)
830 .addImm(0)
831 .addUse(I.getOperand(2).getReg())
832 .addImm(AArch64::sub_32);
833 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
834 AArch64::GPR32RegClass, MRI);
835 I.getOperand(2).setReg(SrcReg);
836
837 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
838 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000839 case TargetOpcode::G_FRAME_INDEX: {
840 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000841 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000842 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000843 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000844 return false;
845 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000846 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000847
848 // MOs for a #0 shifted immediate.
849 I.addOperand(MachineOperand::CreateImm(0));
850 I.addOperand(MachineOperand::CreateImm(0));
851
852 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
853 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000854
855 case TargetOpcode::G_GLOBAL_VALUE: {
856 auto GV = I.getOperand(1).getGlobal();
857 if (GV->isThreadLocal()) {
858 // FIXME: we don't support TLS yet.
859 return false;
860 }
861 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000862 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000863 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000864 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +0000865 } else if (TM.getCodeModel() == CodeModel::Large) {
866 // Materialize the global using movz/movk instructions.
867 unsigned MovZDstReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
868 auto InsertPt = std::next(I.getIterator());
869 auto MovZ =
870 BuildMI(MBB, InsertPt, I.getDebugLoc(), TII.get(AArch64::MOVZXi))
871 .addDef(MovZDstReg);
872 MovZ->addOperand(MF, I.getOperand(1));
873 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
874 AArch64II::MO_NC);
875 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
876 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
877
878 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags,
879 unsigned Offset, unsigned ForceDstReg) {
880 unsigned DstReg =
881 ForceDstReg ? ForceDstReg
882 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
883 auto MovI = BuildMI(MBB, InsertPt, MovZ->getDebugLoc(),
884 TII.get(AArch64::MOVKXi))
885 .addDef(DstReg)
886 .addReg(SrcReg);
887 MovI->addOperand(MF, MachineOperand::CreateGA(
888 GV, MovZ->getOperand(1).getOffset(), Flags));
889 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
890 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
891 return DstReg;
892 };
893 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(),
894 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
895 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
896 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
897 I.eraseFromParent();
898 return true;
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000899 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000900 I.setDesc(TII.get(AArch64::MOVaddr));
901 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
902 MachineInstrBuilder MIB(MF, I);
903 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
904 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
905 }
906 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
907 }
908
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000909 case TargetOpcode::G_LOAD:
910 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000911 LLT MemTy = Ty;
912 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000913
Tim Northover5ae83502016-09-15 09:20:34 +0000914 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000915 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000916 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000917 return false;
918 }
919
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000920 auto &MemOp = **I.memoperands_begin();
921 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
922 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
923 return false;
924 }
925
Amara Emerson4f84f882018-01-24 20:35:37 +0000926 // FIXME: PR36018: Volatile loads in some cases are incorrectly selected by
927 // folding with an extend. Until we have a G_SEXTLOAD solution bail out if
928 // we hit one.
929 if (Opcode == TargetOpcode::G_LOAD && MemOp.isVolatile())
930 return false;
931
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000932 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000933#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000934 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000935 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000936 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
937 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000938 assert(MRI.getType(PtrReg).isPointer() &&
939 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000940#endif
941
942 const unsigned ValReg = I.getOperand(0).getReg();
943 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
944
945 const unsigned NewOpc =
946 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
947 if (NewOpc == I.getOpcode())
948 return false;
949
950 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000951
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000952 uint64_t Offset = 0;
953 auto *PtrMI = MRI.getVRegDef(PtrReg);
954
955 // Try to fold a GEP into our unsigned immediate addressing mode.
956 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
957 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
958 int64_t Imm = *COff;
959 const unsigned Size = MemTy.getSizeInBits() / 8;
960 const unsigned Scale = Log2_32(Size);
961 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
962 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
963 I.getOperand(1).setReg(Ptr2Reg);
964 PtrMI = MRI.getVRegDef(Ptr2Reg);
965 Offset = Imm / Size;
966 }
967 }
968 }
969
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000970 // If we haven't folded anything into our addressing mode yet, try to fold
971 // a frame index into the base+offset.
972 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
973 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
974
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000975 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000976
977 // If we're storing a 0, use WZR/XZR.
978 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
979 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
980 if (I.getOpcode() == AArch64::STRWui)
981 I.getOperand(0).setReg(AArch64::WZR);
982 else if (I.getOpcode() == AArch64::STRXui)
983 I.getOperand(0).setReg(AArch64::XZR);
984 }
985 }
986
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000987 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
988 }
989
Tim Northover9dd78f82017-02-08 21:22:25 +0000990 case TargetOpcode::G_SMULH:
991 case TargetOpcode::G_UMULH: {
992 // Reject the various things we don't support yet.
993 if (unsupportedBinOp(I, RBI, MRI, TRI))
994 return false;
995
996 const unsigned DefReg = I.getOperand(0).getReg();
997 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
998
999 if (RB.getID() != AArch64::GPRRegBankID) {
1000 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
1001 return false;
1002 }
1003
1004 if (Ty != LLT::scalar(64)) {
1005 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1006 << ", expected: " << LLT::scalar(64) << '\n');
1007 return false;
1008 }
1009
1010 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1011 : AArch64::UMULHrr;
1012 I.setDesc(TII.get(NewOpc));
1013
1014 // Now that we selected an opcode, we need to constrain the register
1015 // operands to use appropriate classes.
1016 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1017 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001018 case TargetOpcode::G_FADD:
1019 case TargetOpcode::G_FSUB:
1020 case TargetOpcode::G_FMUL:
1021 case TargetOpcode::G_FDIV:
1022
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001023 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001024 case TargetOpcode::G_SHL:
1025 case TargetOpcode::G_LSHR:
1026 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001027 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001028 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001029 if (unsupportedBinOp(I, RBI, MRI, TRI))
1030 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001031
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001032 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001033
1034 const unsigned DefReg = I.getOperand(0).getReg();
1035 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1036
1037 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1038 if (NewOpc == I.getOpcode())
1039 return false;
1040
1041 I.setDesc(TII.get(NewOpc));
1042 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001043
1044 // Now that we selected an opcode, we need to constrain the register
1045 // operands to use appropriate classes.
1046 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1047 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001048
Tim Northover398c5f52017-02-14 20:56:29 +00001049 case TargetOpcode::G_PTR_MASK: {
1050 uint64_t Align = I.getOperand(2).getImm();
1051 if (Align >= 64 || Align == 0)
1052 return false;
1053
1054 uint64_t Mask = ~((1ULL << Align) - 1);
1055 I.setDesc(TII.get(AArch64::ANDXri));
1056 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1057
1058 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1059 }
Tim Northover037af52c2016-10-31 18:31:09 +00001060 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001061 case TargetOpcode::G_TRUNC: {
1062 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1063 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1064
1065 const unsigned DstReg = I.getOperand(0).getReg();
1066 const unsigned SrcReg = I.getOperand(1).getReg();
1067
1068 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1069 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1070
1071 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001072 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001073 return false;
1074 }
1075
1076 if (DstRB.getID() == AArch64::GPRRegBankID) {
1077 const TargetRegisterClass *DstRC =
1078 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1079 if (!DstRC)
1080 return false;
1081
1082 const TargetRegisterClass *SrcRC =
1083 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1084 if (!SrcRC)
1085 return false;
1086
1087 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1088 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001089 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001090 return false;
1091 }
1092
1093 if (DstRC == SrcRC) {
1094 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001095 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1096 SrcTy == LLT::scalar(64)) {
1097 llvm_unreachable("TableGen can import this case");
1098 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001099 } else if (DstRC == &AArch64::GPR32RegClass &&
1100 SrcRC == &AArch64::GPR64RegClass) {
1101 I.getOperand(1).setSubReg(AArch64::sub_32);
1102 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001103 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001104 return false;
1105 }
1106
1107 I.setDesc(TII.get(TargetOpcode::COPY));
1108 return true;
1109 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1110 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1111 I.setDesc(TII.get(AArch64::XTNv4i16));
1112 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1113 return true;
1114 }
1115 }
1116
1117 return false;
1118 }
1119
Tim Northover3d38b3a2016-10-11 20:50:21 +00001120 case TargetOpcode::G_ANYEXT: {
1121 const unsigned DstReg = I.getOperand(0).getReg();
1122 const unsigned SrcReg = I.getOperand(1).getReg();
1123
Quentin Colombetcb629a82016-10-12 03:57:49 +00001124 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1125 if (RBDst.getID() != AArch64::GPRRegBankID) {
1126 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1127 return false;
1128 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001129
Quentin Colombetcb629a82016-10-12 03:57:49 +00001130 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1131 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1132 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001133 return false;
1134 }
1135
1136 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1137
1138 if (DstSize == 0) {
1139 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1140 return false;
1141 }
1142
Quentin Colombetcb629a82016-10-12 03:57:49 +00001143 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001144 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1145 << ", expected: 32 or 64\n");
1146 return false;
1147 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001148 // At this point G_ANYEXT is just like a plain COPY, but we need
1149 // to explicitly form the 64-bit value if any.
1150 if (DstSize > 32) {
1151 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1152 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1153 .addDef(ExtSrc)
1154 .addImm(0)
1155 .addUse(SrcReg)
1156 .addImm(AArch64::sub_32);
1157 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001158 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001159 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001160 }
1161
1162 case TargetOpcode::G_ZEXT:
1163 case TargetOpcode::G_SEXT: {
1164 unsigned Opcode = I.getOpcode();
1165 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1166 SrcTy = MRI.getType(I.getOperand(1).getReg());
1167 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1168 const unsigned DefReg = I.getOperand(0).getReg();
1169 const unsigned SrcReg = I.getOperand(1).getReg();
1170 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1171
1172 if (RB.getID() != AArch64::GPRRegBankID) {
1173 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1174 << ", expected: GPR\n");
1175 return false;
1176 }
1177
1178 MachineInstr *ExtI;
1179 if (DstTy == LLT::scalar(64)) {
1180 // FIXME: Can we avoid manually doing this?
1181 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1182 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1183 << " operand\n");
1184 return false;
1185 }
1186
1187 const unsigned SrcXReg =
1188 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1189 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1190 .addDef(SrcXReg)
1191 .addImm(0)
1192 .addUse(SrcReg)
1193 .addImm(AArch64::sub_32);
1194
1195 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1196 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1197 .addDef(DefReg)
1198 .addUse(SrcXReg)
1199 .addImm(0)
1200 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001201 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001202 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1203 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1204 .addDef(DefReg)
1205 .addUse(SrcReg)
1206 .addImm(0)
1207 .addImm(SrcTy.getSizeInBits() - 1);
1208 } else {
1209 return false;
1210 }
1211
1212 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1213
1214 I.eraseFromParent();
1215 return true;
1216 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001217
Tim Northover69271c62016-10-12 22:49:11 +00001218 case TargetOpcode::G_SITOFP:
1219 case TargetOpcode::G_UITOFP:
1220 case TargetOpcode::G_FPTOSI:
1221 case TargetOpcode::G_FPTOUI: {
1222 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1223 SrcTy = MRI.getType(I.getOperand(1).getReg());
1224 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1225 if (NewOpc == Opcode)
1226 return false;
1227
1228 I.setDesc(TII.get(NewOpc));
1229 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1230
1231 return true;
1232 }
1233
1234
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001235 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001236 // The importer is currently unable to import pointer types since they
1237 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001238 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001239
Daniel Sandersedd07842017-08-17 09:26:14 +00001240 case TargetOpcode::G_BITCAST:
1241 // Imported SelectionDAG rules can handle every bitcast except those that
1242 // bitcast from a type to the same type. Ideally, these shouldn't occur
1243 // but we might not run an optimizer that deletes them.
1244 if (MRI.getType(I.getOperand(0).getReg()) ==
1245 MRI.getType(I.getOperand(1).getReg()))
1246 return selectCopy(I, TII, MRI, TRI, RBI);
1247 return false;
1248
Tim Northover9ac0eba2016-11-08 00:45:29 +00001249 case TargetOpcode::G_SELECT: {
1250 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1251 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1252 << ", expected: " << LLT::scalar(1) << '\n');
1253 return false;
1254 }
1255
1256 const unsigned CondReg = I.getOperand(1).getReg();
1257 const unsigned TReg = I.getOperand(2).getReg();
1258 const unsigned FReg = I.getOperand(3).getReg();
1259
1260 unsigned CSelOpc = 0;
1261
1262 if (Ty == LLT::scalar(32)) {
1263 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001264 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001265 CSelOpc = AArch64::CSELXr;
1266 } else {
1267 return false;
1268 }
1269
1270 MachineInstr &TstMI =
1271 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1272 .addDef(AArch64::WZR)
1273 .addUse(CondReg)
1274 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1275
1276 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1277 .addDef(I.getOperand(0).getReg())
1278 .addUse(TReg)
1279 .addUse(FReg)
1280 .addImm(AArch64CC::NE);
1281
1282 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1283 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1284
1285 I.eraseFromParent();
1286 return true;
1287 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001288 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001289 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001290 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001291 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001292 return false;
1293 }
1294
1295 unsigned CmpOpc = 0;
1296 unsigned ZReg = 0;
1297
1298 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1299 if (CmpTy == LLT::scalar(32)) {
1300 CmpOpc = AArch64::SUBSWrr;
1301 ZReg = AArch64::WZR;
1302 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1303 CmpOpc = AArch64::SUBSXrr;
1304 ZReg = AArch64::XZR;
1305 } else {
1306 return false;
1307 }
1308
Kristof Beyls22524402017-01-05 10:16:08 +00001309 // CSINC increments the result by one when the condition code is false.
1310 // Therefore, we have to invert the predicate to get an increment by 1 when
1311 // the predicate is true.
1312 const AArch64CC::CondCode invCC =
1313 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1314 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001315
1316 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1317 .addDef(ZReg)
1318 .addUse(I.getOperand(2).getReg())
1319 .addUse(I.getOperand(3).getReg());
1320
1321 MachineInstr &CSetMI =
1322 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1323 .addDef(I.getOperand(0).getReg())
1324 .addUse(AArch64::WZR)
1325 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001326 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001327
1328 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1329 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1330
1331 I.eraseFromParent();
1332 return true;
1333 }
1334
Tim Northover7dd378d2016-10-12 22:49:07 +00001335 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001336 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001337 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001338 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001339 return false;
1340 }
1341
1342 unsigned CmpOpc = 0;
1343 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1344 if (CmpTy == LLT::scalar(32)) {
1345 CmpOpc = AArch64::FCMPSrr;
1346 } else if (CmpTy == LLT::scalar(64)) {
1347 CmpOpc = AArch64::FCMPDrr;
1348 } else {
1349 return false;
1350 }
1351
1352 // FIXME: regbank
1353
1354 AArch64CC::CondCode CC1, CC2;
1355 changeFCMPPredToAArch64CC(
1356 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1357
1358 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1359 .addUse(I.getOperand(2).getReg())
1360 .addUse(I.getOperand(3).getReg());
1361
1362 const unsigned DefReg = I.getOperand(0).getReg();
1363 unsigned Def1Reg = DefReg;
1364 if (CC2 != AArch64CC::AL)
1365 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1366
1367 MachineInstr &CSetMI =
1368 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1369 .addDef(Def1Reg)
1370 .addUse(AArch64::WZR)
1371 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001372 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001373
1374 if (CC2 != AArch64CC::AL) {
1375 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1376 MachineInstr &CSet2MI =
1377 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1378 .addDef(Def2Reg)
1379 .addUse(AArch64::WZR)
1380 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001381 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001382 MachineInstr &OrMI =
1383 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1384 .addDef(DefReg)
1385 .addUse(Def1Reg)
1386 .addUse(Def2Reg);
1387 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1388 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1389 }
1390
1391 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1392 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1393
1394 I.eraseFromParent();
1395 return true;
1396 }
Tim Northovere9600d82017-02-08 17:57:27 +00001397 case TargetOpcode::G_VASTART:
1398 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1399 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001400 case TargetOpcode::G_IMPLICIT_DEF:
1401 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001402 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1403 const unsigned DstReg = I.getOperand(0).getReg();
1404 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1405 const TargetRegisterClass *DstRC =
1406 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1407 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001408 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001409 }
1410
1411 return false;
1412}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001413
1414/// SelectArithImmed - Select an immediate value that can be represented as
1415/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1416/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001417InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001418AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001419 MachineInstr &MI = *Root.getParent();
1420 MachineBasicBlock &MBB = *MI.getParent();
1421 MachineFunction &MF = *MBB.getParent();
1422 MachineRegisterInfo &MRI = MF.getRegInfo();
1423
1424 // This function is called from the addsub_shifted_imm ComplexPattern,
1425 // which lists [imm] as the list of opcode it's interested in, however
1426 // we still need to check whether the operand is actually an immediate
1427 // here because the ComplexPattern opcode list is only used in
1428 // root-level opcode matching.
1429 uint64_t Immed;
1430 if (Root.isImm())
1431 Immed = Root.getImm();
1432 else if (Root.isCImm())
1433 Immed = Root.getCImm()->getZExtValue();
1434 else if (Root.isReg()) {
1435 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1436 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001437 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001438 MachineOperand &Op1 = Def->getOperand(1);
1439 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001440 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001441 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001442 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001443 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001444
1445 unsigned ShiftAmt;
1446
1447 if (Immed >> 12 == 0) {
1448 ShiftAmt = 0;
1449 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1450 ShiftAmt = 12;
1451 Immed = Immed >> 12;
1452 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001453 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001454
1455 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001456 return {{
1457 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1458 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1459 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001460}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001461
Daniel Sandersea8711b2017-10-16 03:36:29 +00001462/// Select a "register plus unscaled signed 9-bit immediate" address. This
1463/// should only match when there is an offset that is not valid for a scaled
1464/// immediate addressing mode. The "Size" argument is the size in bytes of the
1465/// memory reference, which is needed here to know what is valid for a scaled
1466/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001467InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001468AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1469 unsigned Size) const {
1470 MachineRegisterInfo &MRI =
1471 Root.getParent()->getParent()->getParent()->getRegInfo();
1472
1473 if (!Root.isReg())
1474 return None;
1475
1476 if (!isBaseWithConstantOffset(Root, MRI))
1477 return None;
1478
1479 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1480 if (!RootDef)
1481 return None;
1482
1483 MachineOperand &OffImm = RootDef->getOperand(2);
1484 if (!OffImm.isReg())
1485 return None;
1486 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1487 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1488 return None;
1489 int64_t RHSC;
1490 MachineOperand &RHSOp1 = RHS->getOperand(1);
1491 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1492 return None;
1493 RHSC = RHSOp1.getCImm()->getSExtValue();
1494
1495 // If the offset is valid as a scaled immediate, don't match here.
1496 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1497 return None;
1498 if (RHSC >= -256 && RHSC < 256) {
1499 MachineOperand &Base = RootDef->getOperand(1);
1500 return {{
1501 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1502 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1503 }};
1504 }
1505 return None;
1506}
1507
1508/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1509/// "Size" argument is the size in bytes of the memory reference, which
1510/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001511InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001512AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1513 unsigned Size) const {
1514 MachineRegisterInfo &MRI =
1515 Root.getParent()->getParent()->getParent()->getRegInfo();
1516
1517 if (!Root.isReg())
1518 return None;
1519
1520 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1521 if (!RootDef)
1522 return None;
1523
1524 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1525 return {{
1526 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1527 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1528 }};
1529 }
1530
1531 if (isBaseWithConstantOffset(Root, MRI)) {
1532 MachineOperand &LHS = RootDef->getOperand(1);
1533 MachineOperand &RHS = RootDef->getOperand(2);
1534 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1535 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1536 if (LHSDef && RHSDef) {
1537 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1538 unsigned Scale = Log2_32(Size);
1539 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1540 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001541 return {{
1542 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1543 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1544 }};
1545
Daniel Sandersea8711b2017-10-16 03:36:29 +00001546 return {{
1547 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1548 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1549 }};
1550 }
1551 }
1552 }
1553
1554 // Before falling back to our general case, check if the unscaled
1555 // instructions can handle this. If so, that's preferable.
1556 if (selectAddrModeUnscaled(Root, Size).hasValue())
1557 return None;
1558
1559 return {{
1560 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1561 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1562 }};
1563}
1564
Volkan Kelesf7f25682018-01-16 18:44:05 +00001565void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1566 const MachineInstr &MI) const {
1567 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1568 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1569 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1570 assert(CstVal && "Expected constant value");
1571 MIB.addImm(CstVal.getValue());
1572}
1573
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001574namespace llvm {
1575InstructionSelector *
1576createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1577 AArch64Subtarget &Subtarget,
1578 AArch64RegisterBankInfo &RBI) {
1579 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1580}
1581}