blob: f1dfe00f1a496f81eed1a18481eb9a58a08858fa [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());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000792 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
793 unsigned SrcSize = SrcTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000794 // Larger extracts are vectors, same-size extracts should be something else
795 // by now (either split up or simplified to a COPY).
796 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
797 return false;
798
Amara Emersonbc03bae2018-02-18 17:03:02 +0000799 I.setDesc(TII.get(SrcSize == 64 ? AArch64::UBFMXri : AArch64::UBFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000800 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
801 Ty.getSizeInBits() - 1);
802
Amara Emersonbc03bae2018-02-18 17:03:02 +0000803 if (SrcSize < 64) {
804 assert(SrcSize == 32 && DstTy.getSizeInBits() == 16 &&
805 "unexpected G_EXTRACT types");
806 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
807 }
808
Tim Northover7b6d66c2017-07-20 22:58:38 +0000809 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
810 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
811 TII.get(AArch64::COPY))
812 .addDef(I.getOperand(0).getReg())
813 .addUse(DstReg, 0, AArch64::sub_32);
814 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
815 AArch64::GPR32RegClass, MRI);
816 I.getOperand(0).setReg(DstReg);
817
818 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
819 }
820
821 case TargetOpcode::G_INSERT: {
822 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000823 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
824 unsigned DstSize = DstTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000825 // Larger inserts are vectors, same-size ones should be something else by
826 // now (split up or turned into COPYs).
827 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
828 return false;
829
Amara Emersonbc03bae2018-02-18 17:03:02 +0000830 I.setDesc(TII.get(DstSize == 64 ? AArch64::BFMXri : AArch64::BFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000831 unsigned LSB = I.getOperand(3).getImm();
832 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
Amara Emersonbc03bae2018-02-18 17:03:02 +0000833 I.getOperand(3).setImm((DstSize - LSB) % DstSize);
Tim Northover7b6d66c2017-07-20 22:58:38 +0000834 MachineInstrBuilder(MF, I).addImm(Width - 1);
835
Amara Emersonbc03bae2018-02-18 17:03:02 +0000836 if (DstSize < 64) {
837 assert(DstSize == 32 && SrcTy.getSizeInBits() == 16 &&
838 "unexpected G_INSERT types");
839 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
840 }
841
Tim Northover7b6d66c2017-07-20 22:58:38 +0000842 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
843 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
844 TII.get(AArch64::SUBREG_TO_REG))
845 .addDef(SrcReg)
846 .addImm(0)
847 .addUse(I.getOperand(2).getReg())
848 .addImm(AArch64::sub_32);
849 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
850 AArch64::GPR32RegClass, MRI);
851 I.getOperand(2).setReg(SrcReg);
852
853 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
854 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000855 case TargetOpcode::G_FRAME_INDEX: {
856 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000857 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000858 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000859 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000860 return false;
861 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000862 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000863
864 // MOs for a #0 shifted immediate.
865 I.addOperand(MachineOperand::CreateImm(0));
866 I.addOperand(MachineOperand::CreateImm(0));
867
868 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
869 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000870
871 case TargetOpcode::G_GLOBAL_VALUE: {
872 auto GV = I.getOperand(1).getGlobal();
873 if (GV->isThreadLocal()) {
874 // FIXME: we don't support TLS yet.
875 return false;
876 }
877 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000878 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000879 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000880 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +0000881 } else if (TM.getCodeModel() == CodeModel::Large) {
882 // Materialize the global using movz/movk instructions.
883 unsigned MovZDstReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
884 auto InsertPt = std::next(I.getIterator());
885 auto MovZ =
886 BuildMI(MBB, InsertPt, I.getDebugLoc(), TII.get(AArch64::MOVZXi))
887 .addDef(MovZDstReg);
888 MovZ->addOperand(MF, I.getOperand(1));
889 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
890 AArch64II::MO_NC);
891 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
892 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
893
894 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags,
895 unsigned Offset, unsigned ForceDstReg) {
896 unsigned DstReg =
897 ForceDstReg ? ForceDstReg
898 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
899 auto MovI = BuildMI(MBB, InsertPt, MovZ->getDebugLoc(),
900 TII.get(AArch64::MOVKXi))
901 .addDef(DstReg)
902 .addReg(SrcReg);
903 MovI->addOperand(MF, MachineOperand::CreateGA(
904 GV, MovZ->getOperand(1).getOffset(), Flags));
905 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
906 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
907 return DstReg;
908 };
909 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(),
910 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
911 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
912 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
913 I.eraseFromParent();
914 return true;
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000915 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000916 I.setDesc(TII.get(AArch64::MOVaddr));
917 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
918 MachineInstrBuilder MIB(MF, I);
919 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
920 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
921 }
922 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
923 }
924
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000925 case TargetOpcode::G_LOAD:
926 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000927 LLT MemTy = Ty;
928 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000929
Tim Northover5ae83502016-09-15 09:20:34 +0000930 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000931 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000932 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000933 return false;
934 }
935
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000936 auto &MemOp = **I.memoperands_begin();
937 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
938 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
939 return false;
940 }
941
Amara Emerson4f84f882018-01-24 20:35:37 +0000942 // FIXME: PR36018: Volatile loads in some cases are incorrectly selected by
943 // folding with an extend. Until we have a G_SEXTLOAD solution bail out if
944 // we hit one.
945 if (Opcode == TargetOpcode::G_LOAD && MemOp.isVolatile())
946 return false;
947
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000948 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000949#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000950 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000951 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000952 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
953 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000954 assert(MRI.getType(PtrReg).isPointer() &&
955 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000956#endif
957
958 const unsigned ValReg = I.getOperand(0).getReg();
959 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
960
961 const unsigned NewOpc =
962 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
963 if (NewOpc == I.getOpcode())
964 return false;
965
966 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000967
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000968 uint64_t Offset = 0;
969 auto *PtrMI = MRI.getVRegDef(PtrReg);
970
971 // Try to fold a GEP into our unsigned immediate addressing mode.
972 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
973 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
974 int64_t Imm = *COff;
975 const unsigned Size = MemTy.getSizeInBits() / 8;
976 const unsigned Scale = Log2_32(Size);
977 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
978 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
979 I.getOperand(1).setReg(Ptr2Reg);
980 PtrMI = MRI.getVRegDef(Ptr2Reg);
981 Offset = Imm / Size;
982 }
983 }
984 }
985
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000986 // If we haven't folded anything into our addressing mode yet, try to fold
987 // a frame index into the base+offset.
988 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
989 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
990
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000991 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000992
993 // If we're storing a 0, use WZR/XZR.
994 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
995 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
996 if (I.getOpcode() == AArch64::STRWui)
997 I.getOperand(0).setReg(AArch64::WZR);
998 else if (I.getOpcode() == AArch64::STRXui)
999 I.getOperand(0).setReg(AArch64::XZR);
1000 }
1001 }
1002
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001003 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1004 }
1005
Tim Northover9dd78f82017-02-08 21:22:25 +00001006 case TargetOpcode::G_SMULH:
1007 case TargetOpcode::G_UMULH: {
1008 // Reject the various things we don't support yet.
1009 if (unsupportedBinOp(I, RBI, MRI, TRI))
1010 return false;
1011
1012 const unsigned DefReg = I.getOperand(0).getReg();
1013 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1014
1015 if (RB.getID() != AArch64::GPRRegBankID) {
1016 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
1017 return false;
1018 }
1019
1020 if (Ty != LLT::scalar(64)) {
1021 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1022 << ", expected: " << LLT::scalar(64) << '\n');
1023 return false;
1024 }
1025
1026 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1027 : AArch64::UMULHrr;
1028 I.setDesc(TII.get(NewOpc));
1029
1030 // Now that we selected an opcode, we need to constrain the register
1031 // operands to use appropriate classes.
1032 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1033 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001034 case TargetOpcode::G_FADD:
1035 case TargetOpcode::G_FSUB:
1036 case TargetOpcode::G_FMUL:
1037 case TargetOpcode::G_FDIV:
1038
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001039 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001040 case TargetOpcode::G_SHL:
1041 case TargetOpcode::G_LSHR:
1042 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001043 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001044 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001045 if (unsupportedBinOp(I, RBI, MRI, TRI))
1046 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001047
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001048 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001049
1050 const unsigned DefReg = I.getOperand(0).getReg();
1051 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1052
1053 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1054 if (NewOpc == I.getOpcode())
1055 return false;
1056
1057 I.setDesc(TII.get(NewOpc));
1058 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001059
1060 // Now that we selected an opcode, we need to constrain the register
1061 // operands to use appropriate classes.
1062 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1063 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001064
Tim Northover398c5f52017-02-14 20:56:29 +00001065 case TargetOpcode::G_PTR_MASK: {
1066 uint64_t Align = I.getOperand(2).getImm();
1067 if (Align >= 64 || Align == 0)
1068 return false;
1069
1070 uint64_t Mask = ~((1ULL << Align) - 1);
1071 I.setDesc(TII.get(AArch64::ANDXri));
1072 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1073
1074 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1075 }
Tim Northover037af52c2016-10-31 18:31:09 +00001076 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001077 case TargetOpcode::G_TRUNC: {
1078 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1079 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1080
1081 const unsigned DstReg = I.getOperand(0).getReg();
1082 const unsigned SrcReg = I.getOperand(1).getReg();
1083
1084 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1085 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1086
1087 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001088 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001089 return false;
1090 }
1091
1092 if (DstRB.getID() == AArch64::GPRRegBankID) {
1093 const TargetRegisterClass *DstRC =
1094 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1095 if (!DstRC)
1096 return false;
1097
1098 const TargetRegisterClass *SrcRC =
1099 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1100 if (!SrcRC)
1101 return false;
1102
1103 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1104 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001105 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001106 return false;
1107 }
1108
1109 if (DstRC == SrcRC) {
1110 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001111 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1112 SrcTy == LLT::scalar(64)) {
1113 llvm_unreachable("TableGen can import this case");
1114 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001115 } else if (DstRC == &AArch64::GPR32RegClass &&
1116 SrcRC == &AArch64::GPR64RegClass) {
1117 I.getOperand(1).setSubReg(AArch64::sub_32);
1118 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001119 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001120 return false;
1121 }
1122
1123 I.setDesc(TII.get(TargetOpcode::COPY));
1124 return true;
1125 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1126 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1127 I.setDesc(TII.get(AArch64::XTNv4i16));
1128 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1129 return true;
1130 }
1131 }
1132
1133 return false;
1134 }
1135
Tim Northover3d38b3a2016-10-11 20:50:21 +00001136 case TargetOpcode::G_ANYEXT: {
1137 const unsigned DstReg = I.getOperand(0).getReg();
1138 const unsigned SrcReg = I.getOperand(1).getReg();
1139
Quentin Colombetcb629a82016-10-12 03:57:49 +00001140 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1141 if (RBDst.getID() != AArch64::GPRRegBankID) {
1142 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1143 return false;
1144 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001145
Quentin Colombetcb629a82016-10-12 03:57:49 +00001146 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1147 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1148 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001149 return false;
1150 }
1151
1152 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1153
1154 if (DstSize == 0) {
1155 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1156 return false;
1157 }
1158
Quentin Colombetcb629a82016-10-12 03:57:49 +00001159 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001160 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1161 << ", expected: 32 or 64\n");
1162 return false;
1163 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001164 // At this point G_ANYEXT is just like a plain COPY, but we need
1165 // to explicitly form the 64-bit value if any.
1166 if (DstSize > 32) {
1167 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1168 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1169 .addDef(ExtSrc)
1170 .addImm(0)
1171 .addUse(SrcReg)
1172 .addImm(AArch64::sub_32);
1173 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001174 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001175 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001176 }
1177
1178 case TargetOpcode::G_ZEXT:
1179 case TargetOpcode::G_SEXT: {
1180 unsigned Opcode = I.getOpcode();
1181 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1182 SrcTy = MRI.getType(I.getOperand(1).getReg());
1183 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1184 const unsigned DefReg = I.getOperand(0).getReg();
1185 const unsigned SrcReg = I.getOperand(1).getReg();
1186 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1187
1188 if (RB.getID() != AArch64::GPRRegBankID) {
1189 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1190 << ", expected: GPR\n");
1191 return false;
1192 }
1193
1194 MachineInstr *ExtI;
1195 if (DstTy == LLT::scalar(64)) {
1196 // FIXME: Can we avoid manually doing this?
1197 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1198 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1199 << " operand\n");
1200 return false;
1201 }
1202
1203 const unsigned SrcXReg =
1204 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1205 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1206 .addDef(SrcXReg)
1207 .addImm(0)
1208 .addUse(SrcReg)
1209 .addImm(AArch64::sub_32);
1210
1211 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1212 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1213 .addDef(DefReg)
1214 .addUse(SrcXReg)
1215 .addImm(0)
1216 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001217 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001218 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1219 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1220 .addDef(DefReg)
1221 .addUse(SrcReg)
1222 .addImm(0)
1223 .addImm(SrcTy.getSizeInBits() - 1);
1224 } else {
1225 return false;
1226 }
1227
1228 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1229
1230 I.eraseFromParent();
1231 return true;
1232 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001233
Tim Northover69271c62016-10-12 22:49:11 +00001234 case TargetOpcode::G_SITOFP:
1235 case TargetOpcode::G_UITOFP:
1236 case TargetOpcode::G_FPTOSI:
1237 case TargetOpcode::G_FPTOUI: {
1238 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1239 SrcTy = MRI.getType(I.getOperand(1).getReg());
1240 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1241 if (NewOpc == Opcode)
1242 return false;
1243
1244 I.setDesc(TII.get(NewOpc));
1245 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1246
1247 return true;
1248 }
1249
1250
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001251 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001252 // The importer is currently unable to import pointer types since they
1253 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001254 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001255
Daniel Sandersedd07842017-08-17 09:26:14 +00001256 case TargetOpcode::G_BITCAST:
1257 // Imported SelectionDAG rules can handle every bitcast except those that
1258 // bitcast from a type to the same type. Ideally, these shouldn't occur
1259 // but we might not run an optimizer that deletes them.
1260 if (MRI.getType(I.getOperand(0).getReg()) ==
1261 MRI.getType(I.getOperand(1).getReg()))
1262 return selectCopy(I, TII, MRI, TRI, RBI);
1263 return false;
1264
Tim Northover9ac0eba2016-11-08 00:45:29 +00001265 case TargetOpcode::G_SELECT: {
1266 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1267 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1268 << ", expected: " << LLT::scalar(1) << '\n');
1269 return false;
1270 }
1271
1272 const unsigned CondReg = I.getOperand(1).getReg();
1273 const unsigned TReg = I.getOperand(2).getReg();
1274 const unsigned FReg = I.getOperand(3).getReg();
1275
1276 unsigned CSelOpc = 0;
1277
1278 if (Ty == LLT::scalar(32)) {
1279 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001280 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001281 CSelOpc = AArch64::CSELXr;
1282 } else {
1283 return false;
1284 }
1285
1286 MachineInstr &TstMI =
1287 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1288 .addDef(AArch64::WZR)
1289 .addUse(CondReg)
1290 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1291
1292 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1293 .addDef(I.getOperand(0).getReg())
1294 .addUse(TReg)
1295 .addUse(FReg)
1296 .addImm(AArch64CC::NE);
1297
1298 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1299 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1300
1301 I.eraseFromParent();
1302 return true;
1303 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001304 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001305 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001306 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001307 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001308 return false;
1309 }
1310
1311 unsigned CmpOpc = 0;
1312 unsigned ZReg = 0;
1313
1314 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1315 if (CmpTy == LLT::scalar(32)) {
1316 CmpOpc = AArch64::SUBSWrr;
1317 ZReg = AArch64::WZR;
1318 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1319 CmpOpc = AArch64::SUBSXrr;
1320 ZReg = AArch64::XZR;
1321 } else {
1322 return false;
1323 }
1324
Kristof Beyls22524402017-01-05 10:16:08 +00001325 // CSINC increments the result by one when the condition code is false.
1326 // Therefore, we have to invert the predicate to get an increment by 1 when
1327 // the predicate is true.
1328 const AArch64CC::CondCode invCC =
1329 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1330 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001331
1332 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1333 .addDef(ZReg)
1334 .addUse(I.getOperand(2).getReg())
1335 .addUse(I.getOperand(3).getReg());
1336
1337 MachineInstr &CSetMI =
1338 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1339 .addDef(I.getOperand(0).getReg())
1340 .addUse(AArch64::WZR)
1341 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001342 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001343
1344 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1345 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1346
1347 I.eraseFromParent();
1348 return true;
1349 }
1350
Tim Northover7dd378d2016-10-12 22:49:07 +00001351 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001352 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001353 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001354 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001355 return false;
1356 }
1357
1358 unsigned CmpOpc = 0;
1359 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1360 if (CmpTy == LLT::scalar(32)) {
1361 CmpOpc = AArch64::FCMPSrr;
1362 } else if (CmpTy == LLT::scalar(64)) {
1363 CmpOpc = AArch64::FCMPDrr;
1364 } else {
1365 return false;
1366 }
1367
1368 // FIXME: regbank
1369
1370 AArch64CC::CondCode CC1, CC2;
1371 changeFCMPPredToAArch64CC(
1372 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1373
1374 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1375 .addUse(I.getOperand(2).getReg())
1376 .addUse(I.getOperand(3).getReg());
1377
1378 const unsigned DefReg = I.getOperand(0).getReg();
1379 unsigned Def1Reg = DefReg;
1380 if (CC2 != AArch64CC::AL)
1381 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1382
1383 MachineInstr &CSetMI =
1384 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1385 .addDef(Def1Reg)
1386 .addUse(AArch64::WZR)
1387 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001388 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001389
1390 if (CC2 != AArch64CC::AL) {
1391 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1392 MachineInstr &CSet2MI =
1393 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1394 .addDef(Def2Reg)
1395 .addUse(AArch64::WZR)
1396 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001397 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001398 MachineInstr &OrMI =
1399 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1400 .addDef(DefReg)
1401 .addUse(Def1Reg)
1402 .addUse(Def2Reg);
1403 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1404 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1405 }
1406
1407 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1408 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1409
1410 I.eraseFromParent();
1411 return true;
1412 }
Tim Northovere9600d82017-02-08 17:57:27 +00001413 case TargetOpcode::G_VASTART:
1414 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1415 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001416 case TargetOpcode::G_IMPLICIT_DEF:
1417 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001418 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1419 const unsigned DstReg = I.getOperand(0).getReg();
1420 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1421 const TargetRegisterClass *DstRC =
1422 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1423 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001424 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001425 }
1426
1427 return false;
1428}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001429
1430/// SelectArithImmed - Select an immediate value that can be represented as
1431/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1432/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001433InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001434AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001435 MachineInstr &MI = *Root.getParent();
1436 MachineBasicBlock &MBB = *MI.getParent();
1437 MachineFunction &MF = *MBB.getParent();
1438 MachineRegisterInfo &MRI = MF.getRegInfo();
1439
1440 // This function is called from the addsub_shifted_imm ComplexPattern,
1441 // which lists [imm] as the list of opcode it's interested in, however
1442 // we still need to check whether the operand is actually an immediate
1443 // here because the ComplexPattern opcode list is only used in
1444 // root-level opcode matching.
1445 uint64_t Immed;
1446 if (Root.isImm())
1447 Immed = Root.getImm();
1448 else if (Root.isCImm())
1449 Immed = Root.getCImm()->getZExtValue();
1450 else if (Root.isReg()) {
1451 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1452 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001453 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001454 MachineOperand &Op1 = Def->getOperand(1);
1455 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001456 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001457 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001458 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001459 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001460
1461 unsigned ShiftAmt;
1462
1463 if (Immed >> 12 == 0) {
1464 ShiftAmt = 0;
1465 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1466 ShiftAmt = 12;
1467 Immed = Immed >> 12;
1468 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001469 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001470
1471 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001472 return {{
1473 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1474 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1475 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001476}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001477
Daniel Sandersea8711b2017-10-16 03:36:29 +00001478/// Select a "register plus unscaled signed 9-bit immediate" address. This
1479/// should only match when there is an offset that is not valid for a scaled
1480/// immediate addressing mode. The "Size" argument is the size in bytes of the
1481/// memory reference, which is needed here to know what is valid for a scaled
1482/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001483InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001484AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1485 unsigned Size) const {
1486 MachineRegisterInfo &MRI =
1487 Root.getParent()->getParent()->getParent()->getRegInfo();
1488
1489 if (!Root.isReg())
1490 return None;
1491
1492 if (!isBaseWithConstantOffset(Root, MRI))
1493 return None;
1494
1495 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1496 if (!RootDef)
1497 return None;
1498
1499 MachineOperand &OffImm = RootDef->getOperand(2);
1500 if (!OffImm.isReg())
1501 return None;
1502 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1503 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1504 return None;
1505 int64_t RHSC;
1506 MachineOperand &RHSOp1 = RHS->getOperand(1);
1507 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1508 return None;
1509 RHSC = RHSOp1.getCImm()->getSExtValue();
1510
1511 // If the offset is valid as a scaled immediate, don't match here.
1512 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1513 return None;
1514 if (RHSC >= -256 && RHSC < 256) {
1515 MachineOperand &Base = RootDef->getOperand(1);
1516 return {{
1517 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1518 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1519 }};
1520 }
1521 return None;
1522}
1523
1524/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1525/// "Size" argument is the size in bytes of the memory reference, which
1526/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001527InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001528AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1529 unsigned Size) const {
1530 MachineRegisterInfo &MRI =
1531 Root.getParent()->getParent()->getParent()->getRegInfo();
1532
1533 if (!Root.isReg())
1534 return None;
1535
1536 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1537 if (!RootDef)
1538 return None;
1539
1540 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1541 return {{
1542 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1543 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1544 }};
1545 }
1546
1547 if (isBaseWithConstantOffset(Root, MRI)) {
1548 MachineOperand &LHS = RootDef->getOperand(1);
1549 MachineOperand &RHS = RootDef->getOperand(2);
1550 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1551 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1552 if (LHSDef && RHSDef) {
1553 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1554 unsigned Scale = Log2_32(Size);
1555 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1556 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001557 return {{
1558 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1559 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1560 }};
1561
Daniel Sandersea8711b2017-10-16 03:36:29 +00001562 return {{
1563 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1564 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1565 }};
1566 }
1567 }
1568 }
1569
1570 // Before falling back to our general case, check if the unscaled
1571 // instructions can handle this. If so, that's preferable.
1572 if (selectAddrModeUnscaled(Root, Size).hasValue())
1573 return None;
1574
1575 return {{
1576 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1577 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1578 }};
1579}
1580
Volkan Kelesf7f25682018-01-16 18:44:05 +00001581void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1582 const MachineInstr &MI) const {
1583 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1584 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1585 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1586 assert(CstVal && "Expected constant value");
1587 MIB.addImm(CstVal.getValue());
1588}
1589
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001590namespace llvm {
1591InstructionSelector *
1592createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1593 AArch64Subtarget &Subtarget,
1594 AArch64RegisterBankInfo &RBI) {
1595 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1596}
1597}