blob: 392ba13d74a8f5b91f2d3382e766fb12e601c232 [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,
138 const RegisterBankInfo &RBI) {
139 if (RB.getID() == AArch64::GPRRegBankID) {
140 if (Ty.getSizeInBits() <= 32)
141 return &AArch64::GPR32RegClass;
142 if (Ty.getSizeInBits() == 64)
143 return &AArch64::GPR64RegClass;
144 return nullptr;
145 }
146
147 if (RB.getID() == AArch64::FPRRegBankID) {
148 if (Ty.getSizeInBits() == 32)
149 return &AArch64::FPR32RegClass;
150 if (Ty.getSizeInBits() == 64)
151 return &AArch64::FPR64RegClass;
152 if (Ty.getSizeInBits() == 128)
153 return &AArch64::FPR128RegClass;
154 return nullptr;
155 }
156
157 return nullptr;
158}
159
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000160/// Check whether \p I is a currently unsupported binary operation:
161/// - it has an unsized type
162/// - an operand is not a vreg
163/// - all operands are not in the same bank
164/// These are checks that should someday live in the verifier, but right now,
165/// these are mostly limitations of the aarch64 selector.
166static bool unsupportedBinOp(const MachineInstr &I,
167 const AArch64RegisterBankInfo &RBI,
168 const MachineRegisterInfo &MRI,
169 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000170 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000171 if (!Ty.isValid()) {
172 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000173 return true;
174 }
175
176 const RegisterBank *PrevOpBank = nullptr;
177 for (auto &MO : I.operands()) {
178 // FIXME: Support non-register operands.
179 if (!MO.isReg()) {
180 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
181 return true;
182 }
183
184 // FIXME: Can generic operations have physical registers operands? If
185 // so, this will need to be taught about that, and we'll need to get the
186 // bank out of the minimal class for the register.
187 // Either way, this needs to be documented (and possibly verified).
188 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
189 DEBUG(dbgs() << "Generic inst has physical register operand\n");
190 return true;
191 }
192
193 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
194 if (!OpBank) {
195 DEBUG(dbgs() << "Generic register has no bank or class\n");
196 return true;
197 }
198
199 if (PrevOpBank && OpBank != PrevOpBank) {
200 DEBUG(dbgs() << "Generic inst operands have different banks\n");
201 return true;
202 }
203 PrevOpBank = OpBank;
204 }
205 return false;
206}
207
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000208/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000209/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000210/// and of size \p OpSize.
211/// \returns \p GenericOpc if the combination is unsupported.
212static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
213 unsigned OpSize) {
214 switch (RegBankID) {
215 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000216 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000217 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000218 case TargetOpcode::G_SHL:
219 return AArch64::LSLVWr;
220 case TargetOpcode::G_LSHR:
221 return AArch64::LSRVWr;
222 case TargetOpcode::G_ASHR:
223 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000224 default:
225 return GenericOpc;
226 }
Tim Northover55782222016-10-18 20:03:48 +0000227 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000228 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000229 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000230 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000231 case TargetOpcode::G_SHL:
232 return AArch64::LSLVXr;
233 case TargetOpcode::G_LSHR:
234 return AArch64::LSRVXr;
235 case TargetOpcode::G_ASHR:
236 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000237 default:
238 return GenericOpc;
239 }
240 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000241 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000242 case AArch64::FPRRegBankID:
243 switch (OpSize) {
244 case 32:
245 switch (GenericOpc) {
246 case TargetOpcode::G_FADD:
247 return AArch64::FADDSrr;
248 case TargetOpcode::G_FSUB:
249 return AArch64::FSUBSrr;
250 case TargetOpcode::G_FMUL:
251 return AArch64::FMULSrr;
252 case TargetOpcode::G_FDIV:
253 return AArch64::FDIVSrr;
254 default:
255 return GenericOpc;
256 }
257 case 64:
258 switch (GenericOpc) {
259 case TargetOpcode::G_FADD:
260 return AArch64::FADDDrr;
261 case TargetOpcode::G_FSUB:
262 return AArch64::FSUBDrr;
263 case TargetOpcode::G_FMUL:
264 return AArch64::FMULDrr;
265 case TargetOpcode::G_FDIV:
266 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000267 case TargetOpcode::G_OR:
268 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000269 default:
270 return GenericOpc;
271 }
272 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000273 break;
274 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000275 return GenericOpc;
276}
277
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000278/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
279/// appropriate for the (value) register bank \p RegBankID and of memory access
280/// size \p OpSize. This returns the variant with the base+unsigned-immediate
281/// addressing mode (e.g., LDRXui).
282/// \returns \p GenericOpc if the combination is unsupported.
283static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
284 unsigned OpSize) {
285 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
286 switch (RegBankID) {
287 case AArch64::GPRRegBankID:
288 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000289 case 8:
290 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
291 case 16:
292 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000293 case 32:
294 return isStore ? AArch64::STRWui : AArch64::LDRWui;
295 case 64:
296 return isStore ? AArch64::STRXui : AArch64::LDRXui;
297 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000298 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000299 case AArch64::FPRRegBankID:
300 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000301 case 8:
302 return isStore ? AArch64::STRBui : AArch64::LDRBui;
303 case 16:
304 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000305 case 32:
306 return isStore ? AArch64::STRSui : AArch64::LDRSui;
307 case 64:
308 return isStore ? AArch64::STRDui : AArch64::LDRDui;
309 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000310 break;
311 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000312 return GenericOpc;
313}
314
Quentin Colombetcb629a82016-10-12 03:57:49 +0000315static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
316 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
317 const RegisterBankInfo &RBI) {
318
319 unsigned DstReg = I.getOperand(0).getReg();
320 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
321 assert(I.isCopy() && "Generic operators do not allow physical registers");
322 return true;
323 }
324
325 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
326 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
327 unsigned SrcReg = I.getOperand(1).getReg();
328 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
329 (void)SrcSize;
330 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
331 "No phys reg on generic operators");
332 assert(
333 (DstSize == SrcSize ||
334 // Copies are a mean to setup initial types, the number of
335 // bits may not exactly match.
336 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
337 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
338 // Copies are a mean to copy bits around, as long as we are
339 // on the same register class, that's fine. Otherwise, that
340 // means we need some SUBREG_TO_REG or AND & co.
341 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
342 "Copy with different width?!");
343 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
344 "GPRs cannot get more than 64-bit width values");
345 const TargetRegisterClass *RC = nullptr;
346
347 if (RegBank.getID() == AArch64::FPRRegBankID) {
Ahmed Bougachaa7aa2a92017-09-12 21:04:10 +0000348 if (DstSize <= 16)
349 RC = &AArch64::FPR16RegClass;
350 else if (DstSize <= 32)
Quentin Colombetcb629a82016-10-12 03:57:49 +0000351 RC = &AArch64::FPR32RegClass;
352 else if (DstSize <= 64)
353 RC = &AArch64::FPR64RegClass;
354 else if (DstSize <= 128)
355 RC = &AArch64::FPR128RegClass;
356 else {
357 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
358 return false;
359 }
360 } else {
361 assert(RegBank.getID() == AArch64::GPRRegBankID &&
362 "Bitcast for the flags?");
363 RC =
364 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
365 }
366
367 // No need to constrain SrcReg. It will get constrained when
368 // we hit another of its use or its defs.
369 // Copies do not have constraints.
370 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
371 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
372 << " operand\n");
373 return false;
374 }
375 I.setDesc(TII.get(AArch64::COPY));
376 return true;
377}
378
Tim Northover69271c62016-10-12 22:49:11 +0000379static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
380 if (!DstTy.isScalar() || !SrcTy.isScalar())
381 return GenericOpc;
382
383 const unsigned DstSize = DstTy.getSizeInBits();
384 const unsigned SrcSize = SrcTy.getSizeInBits();
385
386 switch (DstSize) {
387 case 32:
388 switch (SrcSize) {
389 case 32:
390 switch (GenericOpc) {
391 case TargetOpcode::G_SITOFP:
392 return AArch64::SCVTFUWSri;
393 case TargetOpcode::G_UITOFP:
394 return AArch64::UCVTFUWSri;
395 case TargetOpcode::G_FPTOSI:
396 return AArch64::FCVTZSUWSr;
397 case TargetOpcode::G_FPTOUI:
398 return AArch64::FCVTZUUWSr;
399 default:
400 return GenericOpc;
401 }
402 case 64:
403 switch (GenericOpc) {
404 case TargetOpcode::G_SITOFP:
405 return AArch64::SCVTFUXSri;
406 case TargetOpcode::G_UITOFP:
407 return AArch64::UCVTFUXSri;
408 case TargetOpcode::G_FPTOSI:
409 return AArch64::FCVTZSUWDr;
410 case TargetOpcode::G_FPTOUI:
411 return AArch64::FCVTZUUWDr;
412 default:
413 return GenericOpc;
414 }
415 default:
416 return GenericOpc;
417 }
418 case 64:
419 switch (SrcSize) {
420 case 32:
421 switch (GenericOpc) {
422 case TargetOpcode::G_SITOFP:
423 return AArch64::SCVTFUWDri;
424 case TargetOpcode::G_UITOFP:
425 return AArch64::UCVTFUWDri;
426 case TargetOpcode::G_FPTOSI:
427 return AArch64::FCVTZSUXSr;
428 case TargetOpcode::G_FPTOUI:
429 return AArch64::FCVTZUUXSr;
430 default:
431 return GenericOpc;
432 }
433 case 64:
434 switch (GenericOpc) {
435 case TargetOpcode::G_SITOFP:
436 return AArch64::SCVTFUXDri;
437 case TargetOpcode::G_UITOFP:
438 return AArch64::UCVTFUXDri;
439 case TargetOpcode::G_FPTOSI:
440 return AArch64::FCVTZSUXDr;
441 case TargetOpcode::G_FPTOUI:
442 return AArch64::FCVTZUUXDr;
443 default:
444 return GenericOpc;
445 }
446 default:
447 return GenericOpc;
448 }
449 default:
450 return GenericOpc;
451 };
452 return GenericOpc;
453}
454
Tim Northover6c02ad52016-10-12 22:49:04 +0000455static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
456 switch (P) {
457 default:
458 llvm_unreachable("Unknown condition code!");
459 case CmpInst::ICMP_NE:
460 return AArch64CC::NE;
461 case CmpInst::ICMP_EQ:
462 return AArch64CC::EQ;
463 case CmpInst::ICMP_SGT:
464 return AArch64CC::GT;
465 case CmpInst::ICMP_SGE:
466 return AArch64CC::GE;
467 case CmpInst::ICMP_SLT:
468 return AArch64CC::LT;
469 case CmpInst::ICMP_SLE:
470 return AArch64CC::LE;
471 case CmpInst::ICMP_UGT:
472 return AArch64CC::HI;
473 case CmpInst::ICMP_UGE:
474 return AArch64CC::HS;
475 case CmpInst::ICMP_ULT:
476 return AArch64CC::LO;
477 case CmpInst::ICMP_ULE:
478 return AArch64CC::LS;
479 }
480}
481
Tim Northover7dd378d2016-10-12 22:49:07 +0000482static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
483 AArch64CC::CondCode &CondCode,
484 AArch64CC::CondCode &CondCode2) {
485 CondCode2 = AArch64CC::AL;
486 switch (P) {
487 default:
488 llvm_unreachable("Unknown FP condition!");
489 case CmpInst::FCMP_OEQ:
490 CondCode = AArch64CC::EQ;
491 break;
492 case CmpInst::FCMP_OGT:
493 CondCode = AArch64CC::GT;
494 break;
495 case CmpInst::FCMP_OGE:
496 CondCode = AArch64CC::GE;
497 break;
498 case CmpInst::FCMP_OLT:
499 CondCode = AArch64CC::MI;
500 break;
501 case CmpInst::FCMP_OLE:
502 CondCode = AArch64CC::LS;
503 break;
504 case CmpInst::FCMP_ONE:
505 CondCode = AArch64CC::MI;
506 CondCode2 = AArch64CC::GT;
507 break;
508 case CmpInst::FCMP_ORD:
509 CondCode = AArch64CC::VC;
510 break;
511 case CmpInst::FCMP_UNO:
512 CondCode = AArch64CC::VS;
513 break;
514 case CmpInst::FCMP_UEQ:
515 CondCode = AArch64CC::EQ;
516 CondCode2 = AArch64CC::VS;
517 break;
518 case CmpInst::FCMP_UGT:
519 CondCode = AArch64CC::HI;
520 break;
521 case CmpInst::FCMP_UGE:
522 CondCode = AArch64CC::PL;
523 break;
524 case CmpInst::FCMP_ULT:
525 CondCode = AArch64CC::LT;
526 break;
527 case CmpInst::FCMP_ULE:
528 CondCode = AArch64CC::LE;
529 break;
530 case CmpInst::FCMP_UNE:
531 CondCode = AArch64CC::NE;
532 break;
533 }
534}
535
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000536bool AArch64InstructionSelector::selectCompareBranch(
537 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
538
539 const unsigned CondReg = I.getOperand(0).getReg();
540 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
541 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000542 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
543 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000544 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
545 return false;
546
547 unsigned LHS = CCMI->getOperand(2).getReg();
548 unsigned RHS = CCMI->getOperand(3).getReg();
549 if (!getConstantVRegVal(RHS, MRI))
550 std::swap(RHS, LHS);
551
552 const auto RHSImm = getConstantVRegVal(RHS, MRI);
553 if (!RHSImm || *RHSImm != 0)
554 return false;
555
556 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
557 if (RB.getID() != AArch64::GPRRegBankID)
558 return false;
559
560 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
561 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
562 return false;
563
564 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
565 unsigned CBOpc = 0;
566 if (CmpWidth <= 32)
567 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
568 else if (CmpWidth == 64)
569 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
570 else
571 return false;
572
Aditya Nandakumar18b3f9d2018-01-17 19:31:33 +0000573 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
574 .addUse(LHS)
575 .addMBB(DestMBB)
576 .constrainAllUses(TII, TRI, RBI);
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000577
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000578 I.eraseFromParent();
579 return true;
580}
581
Tim Northovere9600d82017-02-08 17:57:27 +0000582bool AArch64InstructionSelector::selectVaStartAAPCS(
583 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
584 return false;
585}
586
587bool AArch64InstructionSelector::selectVaStartDarwin(
588 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
589 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
590 unsigned ListReg = I.getOperand(0).getReg();
591
592 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
593
594 auto MIB =
595 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
596 .addDef(ArgsAddrReg)
597 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
598 .addImm(0)
599 .addImm(0);
600
601 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
602
603 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
604 .addUse(ArgsAddrReg)
605 .addUse(ListReg)
606 .addImm(0)
607 .addMemOperand(*I.memoperands_begin());
608
609 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
610 I.eraseFromParent();
611 return true;
612}
613
Daniel Sandersf76f3152017-11-16 00:46:35 +0000614bool AArch64InstructionSelector::select(MachineInstr &I,
615 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000616 assert(I.getParent() && "Instruction should be in a basic block!");
617 assert(I.getParent()->getParent() && "Instruction should be in a function!");
618
619 MachineBasicBlock &MBB = *I.getParent();
620 MachineFunction &MF = *MBB.getParent();
621 MachineRegisterInfo &MRI = MF.getRegInfo();
622
Tim Northovercdf23f12016-10-31 18:30:59 +0000623 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000624 // G_PHI requires same handling as PHI
625 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000626 // Certain non-generic instructions also need some special handling.
627
628 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
629 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000630
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000631 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000632 const unsigned DefReg = I.getOperand(0).getReg();
633 const LLT DefTy = MRI.getType(DefReg);
634
635 const TargetRegisterClass *DefRC = nullptr;
636 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
637 DefRC = TRI.getRegClass(DefReg);
638 } else {
639 const RegClassOrRegBank &RegClassOrBank =
640 MRI.getRegClassOrRegBank(DefReg);
641
642 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
643 if (!DefRC) {
644 if (!DefTy.isValid()) {
645 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
646 return false;
647 }
648 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
649 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
650 if (!DefRC) {
651 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
652 return false;
653 }
654 }
655 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000656 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000657
658 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
659 }
660
661 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000662 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000663
664 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000665 }
666
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000667
668 if (I.getNumOperands() != I.getNumExplicitOperands()) {
669 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
670 return false;
671 }
672
Daniel Sandersf76f3152017-11-16 00:46:35 +0000673 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000674 return true;
675
Tim Northover32a078a2016-09-15 10:09:59 +0000676 LLT Ty =
677 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000678
Tim Northover69271c62016-10-12 22:49:11 +0000679 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000680 case TargetOpcode::G_BRCOND: {
681 if (Ty.getSizeInBits() > 32) {
682 // We shouldn't need this on AArch64, but it would be implemented as an
683 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
684 // bit being tested is < 32.
685 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
686 << ", expected at most 32-bits");
687 return false;
688 }
689
690 const unsigned CondReg = I.getOperand(0).getReg();
691 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
692
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000693 if (selectCompareBranch(I, MF, MRI))
694 return true;
695
Tim Northover5e3dbf32016-10-12 22:49:01 +0000696 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
697 .addUse(CondReg)
698 .addImm(/*bit offset=*/0)
699 .addMBB(DestMBB);
700
701 I.eraseFromParent();
702 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
703 }
704
Kristof Beyls65a12c02017-01-30 09:13:18 +0000705 case TargetOpcode::G_BRINDIRECT: {
706 I.setDesc(TII.get(AArch64::BR));
707 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
708 }
709
Tim Northover4494d692016-10-18 19:47:57 +0000710 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000711 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000712 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
713
714 const LLT s32 = LLT::scalar(32);
715 const LLT s64 = LLT::scalar(64);
716 const LLT p0 = LLT::pointer(0, 64);
717
718 const unsigned DefReg = I.getOperand(0).getReg();
719 const LLT DefTy = MRI.getType(DefReg);
720 const unsigned DefSize = DefTy.getSizeInBits();
721 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
722
723 // FIXME: Redundant check, but even less readable when factored out.
724 if (isFP) {
725 if (Ty != s32 && Ty != s64) {
726 DEBUG(dbgs() << "Unable to materialize FP " << Ty
727 << " constant, expected: " << s32 << " or " << s64
728 << '\n');
729 return false;
730 }
731
732 if (RB.getID() != AArch64::FPRRegBankID) {
733 DEBUG(dbgs() << "Unable to materialize FP " << Ty
734 << " constant on bank: " << RB << ", expected: FPR\n");
735 return false;
736 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000737
738 // The case when we have 0.0 is covered by tablegen. Reject it here so we
739 // can be sure tablegen works correctly and isn't rescued by this code.
740 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
741 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000742 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000743 // s32 and s64 are covered by tablegen.
744 if (Ty != p0) {
Tim Northover4494d692016-10-18 19:47:57 +0000745 DEBUG(dbgs() << "Unable to materialize integer " << Ty
746 << " constant, expected: " << s32 << ", " << s64 << ", or "
747 << p0 << '\n');
748 return false;
749 }
750
751 if (RB.getID() != AArch64::GPRRegBankID) {
752 DEBUG(dbgs() << "Unable to materialize integer " << Ty
753 << " constant on bank: " << RB << ", expected: GPR\n");
754 return false;
755 }
756 }
757
758 const unsigned MovOpc =
759 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
760
761 I.setDesc(TII.get(MovOpc));
762
763 if (isFP) {
764 const TargetRegisterClass &GPRRC =
765 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
766 const TargetRegisterClass &FPRRC =
767 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
768
769 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
770 MachineOperand &RegOp = I.getOperand(0);
771 RegOp.setReg(DefGPRReg);
772
773 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
774 TII.get(AArch64::COPY))
775 .addDef(DefReg)
776 .addUse(DefGPRReg);
777
778 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
779 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
780 return false;
781 }
782
783 MachineOperand &ImmOp = I.getOperand(1);
784 // FIXME: Is going through int64_t always correct?
785 ImmOp.ChangeToImmediate(
786 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000787 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000788 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
789 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000790 } else if (I.getOperand(1).isImm()) {
791 uint64_t Val = I.getOperand(1).getImm();
792 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000793 }
794
795 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
796 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000797 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000798 case TargetOpcode::G_EXTRACT: {
799 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
800 // Larger extracts are vectors, same-size extracts should be something else
801 // by now (either split up or simplified to a COPY).
802 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
803 return false;
804
805 I.setDesc(TII.get(AArch64::UBFMXri));
806 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
807 Ty.getSizeInBits() - 1);
808
809 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());
823 // Larger inserts are vectors, same-size ones should be something else by
824 // now (split up or turned into COPYs).
825 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
826 return false;
827
828 I.setDesc(TII.get(AArch64::BFMXri));
829 unsigned LSB = I.getOperand(3).getImm();
830 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
831 I.getOperand(3).setImm((64 - LSB) % 64);
832 MachineInstrBuilder(MF, I).addImm(Width - 1);
833
834 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
835 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
836 TII.get(AArch64::SUBREG_TO_REG))
837 .addDef(SrcReg)
838 .addImm(0)
839 .addUse(I.getOperand(2).getReg())
840 .addImm(AArch64::sub_32);
841 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
842 AArch64::GPR32RegClass, MRI);
843 I.getOperand(2).setReg(SrcReg);
844
845 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
846 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000847 case TargetOpcode::G_FRAME_INDEX: {
848 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000849 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000850 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000851 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000852 return false;
853 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000854 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000855
856 // MOs for a #0 shifted immediate.
857 I.addOperand(MachineOperand::CreateImm(0));
858 I.addOperand(MachineOperand::CreateImm(0));
859
860 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
861 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000862
863 case TargetOpcode::G_GLOBAL_VALUE: {
864 auto GV = I.getOperand(1).getGlobal();
865 if (GV->isThreadLocal()) {
866 // FIXME: we don't support TLS yet.
867 return false;
868 }
869 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000870 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000871 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000872 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +0000873 } else if (TM.getCodeModel() == CodeModel::Large) {
874 // Materialize the global using movz/movk instructions.
875 unsigned MovZDstReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
876 auto InsertPt = std::next(I.getIterator());
877 auto MovZ =
878 BuildMI(MBB, InsertPt, I.getDebugLoc(), TII.get(AArch64::MOVZXi))
879 .addDef(MovZDstReg);
880 MovZ->addOperand(MF, I.getOperand(1));
881 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
882 AArch64II::MO_NC);
883 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
884 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
885
886 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags,
887 unsigned Offset, unsigned ForceDstReg) {
888 unsigned DstReg =
889 ForceDstReg ? ForceDstReg
890 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
891 auto MovI = BuildMI(MBB, InsertPt, MovZ->getDebugLoc(),
892 TII.get(AArch64::MOVKXi))
893 .addDef(DstReg)
894 .addReg(SrcReg);
895 MovI->addOperand(MF, MachineOperand::CreateGA(
896 GV, MovZ->getOperand(1).getOffset(), Flags));
897 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
898 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
899 return DstReg;
900 };
901 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(),
902 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
903 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
904 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
905 I.eraseFromParent();
906 return true;
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000907 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000908 I.setDesc(TII.get(AArch64::MOVaddr));
909 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
910 MachineInstrBuilder MIB(MF, I);
911 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
912 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
913 }
914 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
915 }
916
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000917 case TargetOpcode::G_LOAD:
918 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000919 LLT MemTy = Ty;
920 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000921
Tim Northover5ae83502016-09-15 09:20:34 +0000922 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000923 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000924 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000925 return false;
926 }
927
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000928 auto &MemOp = **I.memoperands_begin();
929 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
930 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
931 return false;
932 }
933
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000934 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000935#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000936 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000937 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000938 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
939 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000940 assert(MRI.getType(PtrReg).isPointer() &&
941 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000942#endif
943
944 const unsigned ValReg = I.getOperand(0).getReg();
945 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
946
947 const unsigned NewOpc =
948 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
949 if (NewOpc == I.getOpcode())
950 return false;
951
952 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000953
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000954 uint64_t Offset = 0;
955 auto *PtrMI = MRI.getVRegDef(PtrReg);
956
957 // Try to fold a GEP into our unsigned immediate addressing mode.
958 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
959 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
960 int64_t Imm = *COff;
961 const unsigned Size = MemTy.getSizeInBits() / 8;
962 const unsigned Scale = Log2_32(Size);
963 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
964 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
965 I.getOperand(1).setReg(Ptr2Reg);
966 PtrMI = MRI.getVRegDef(Ptr2Reg);
967 Offset = Imm / Size;
968 }
969 }
970 }
971
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000972 // If we haven't folded anything into our addressing mode yet, try to fold
973 // a frame index into the base+offset.
974 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
975 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
976
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000977 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000978
979 // If we're storing a 0, use WZR/XZR.
980 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
981 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
982 if (I.getOpcode() == AArch64::STRWui)
983 I.getOperand(0).setReg(AArch64::WZR);
984 else if (I.getOpcode() == AArch64::STRXui)
985 I.getOperand(0).setReg(AArch64::XZR);
986 }
987 }
988
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000989 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
990 }
991
Tim Northover9dd78f82017-02-08 21:22:25 +0000992 case TargetOpcode::G_SMULH:
993 case TargetOpcode::G_UMULH: {
994 // Reject the various things we don't support yet.
995 if (unsupportedBinOp(I, RBI, MRI, TRI))
996 return false;
997
998 const unsigned DefReg = I.getOperand(0).getReg();
999 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1000
1001 if (RB.getID() != AArch64::GPRRegBankID) {
1002 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
1003 return false;
1004 }
1005
1006 if (Ty != LLT::scalar(64)) {
1007 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1008 << ", expected: " << LLT::scalar(64) << '\n');
1009 return false;
1010 }
1011
1012 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1013 : AArch64::UMULHrr;
1014 I.setDesc(TII.get(NewOpc));
1015
1016 // Now that we selected an opcode, we need to constrain the register
1017 // operands to use appropriate classes.
1018 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1019 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001020 case TargetOpcode::G_FADD:
1021 case TargetOpcode::G_FSUB:
1022 case TargetOpcode::G_FMUL:
1023 case TargetOpcode::G_FDIV:
1024
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001025 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001026 case TargetOpcode::G_SHL:
1027 case TargetOpcode::G_LSHR:
1028 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001029 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001030 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001031 if (unsupportedBinOp(I, RBI, MRI, TRI))
1032 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001033
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001034 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001035
1036 const unsigned DefReg = I.getOperand(0).getReg();
1037 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1038
1039 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1040 if (NewOpc == I.getOpcode())
1041 return false;
1042
1043 I.setDesc(TII.get(NewOpc));
1044 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001045
1046 // Now that we selected an opcode, we need to constrain the register
1047 // operands to use appropriate classes.
1048 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1049 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001050
Tim Northover398c5f52017-02-14 20:56:29 +00001051 case TargetOpcode::G_PTR_MASK: {
1052 uint64_t Align = I.getOperand(2).getImm();
1053 if (Align >= 64 || Align == 0)
1054 return false;
1055
1056 uint64_t Mask = ~((1ULL << Align) - 1);
1057 I.setDesc(TII.get(AArch64::ANDXri));
1058 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1059
1060 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1061 }
Tim Northover037af52c2016-10-31 18:31:09 +00001062 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001063 case TargetOpcode::G_TRUNC: {
1064 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1065 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1066
1067 const unsigned DstReg = I.getOperand(0).getReg();
1068 const unsigned SrcReg = I.getOperand(1).getReg();
1069
1070 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1071 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1072
1073 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001074 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001075 return false;
1076 }
1077
1078 if (DstRB.getID() == AArch64::GPRRegBankID) {
1079 const TargetRegisterClass *DstRC =
1080 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1081 if (!DstRC)
1082 return false;
1083
1084 const TargetRegisterClass *SrcRC =
1085 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1086 if (!SrcRC)
1087 return false;
1088
1089 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1090 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001091 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001092 return false;
1093 }
1094
1095 if (DstRC == SrcRC) {
1096 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001097 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1098 SrcTy == LLT::scalar(64)) {
1099 llvm_unreachable("TableGen can import this case");
1100 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001101 } else if (DstRC == &AArch64::GPR32RegClass &&
1102 SrcRC == &AArch64::GPR64RegClass) {
1103 I.getOperand(1).setSubReg(AArch64::sub_32);
1104 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001105 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001106 return false;
1107 }
1108
1109 I.setDesc(TII.get(TargetOpcode::COPY));
1110 return true;
1111 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1112 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1113 I.setDesc(TII.get(AArch64::XTNv4i16));
1114 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1115 return true;
1116 }
1117 }
1118
1119 return false;
1120 }
1121
Tim Northover3d38b3a2016-10-11 20:50:21 +00001122 case TargetOpcode::G_ANYEXT: {
1123 const unsigned DstReg = I.getOperand(0).getReg();
1124 const unsigned SrcReg = I.getOperand(1).getReg();
1125
Quentin Colombetcb629a82016-10-12 03:57:49 +00001126 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1127 if (RBDst.getID() != AArch64::GPRRegBankID) {
1128 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1129 return false;
1130 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001131
Quentin Colombetcb629a82016-10-12 03:57:49 +00001132 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1133 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1134 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001135 return false;
1136 }
1137
1138 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1139
1140 if (DstSize == 0) {
1141 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1142 return false;
1143 }
1144
Quentin Colombetcb629a82016-10-12 03:57:49 +00001145 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001146 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1147 << ", expected: 32 or 64\n");
1148 return false;
1149 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001150 // At this point G_ANYEXT is just like a plain COPY, but we need
1151 // to explicitly form the 64-bit value if any.
1152 if (DstSize > 32) {
1153 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1154 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1155 .addDef(ExtSrc)
1156 .addImm(0)
1157 .addUse(SrcReg)
1158 .addImm(AArch64::sub_32);
1159 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001160 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001161 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001162 }
1163
1164 case TargetOpcode::G_ZEXT:
1165 case TargetOpcode::G_SEXT: {
1166 unsigned Opcode = I.getOpcode();
1167 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1168 SrcTy = MRI.getType(I.getOperand(1).getReg());
1169 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1170 const unsigned DefReg = I.getOperand(0).getReg();
1171 const unsigned SrcReg = I.getOperand(1).getReg();
1172 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1173
1174 if (RB.getID() != AArch64::GPRRegBankID) {
1175 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1176 << ", expected: GPR\n");
1177 return false;
1178 }
1179
1180 MachineInstr *ExtI;
1181 if (DstTy == LLT::scalar(64)) {
1182 // FIXME: Can we avoid manually doing this?
1183 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1184 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1185 << " operand\n");
1186 return false;
1187 }
1188
1189 const unsigned SrcXReg =
1190 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1191 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1192 .addDef(SrcXReg)
1193 .addImm(0)
1194 .addUse(SrcReg)
1195 .addImm(AArch64::sub_32);
1196
1197 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1198 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1199 .addDef(DefReg)
1200 .addUse(SrcXReg)
1201 .addImm(0)
1202 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001203 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001204 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1205 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1206 .addDef(DefReg)
1207 .addUse(SrcReg)
1208 .addImm(0)
1209 .addImm(SrcTy.getSizeInBits() - 1);
1210 } else {
1211 return false;
1212 }
1213
1214 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1215
1216 I.eraseFromParent();
1217 return true;
1218 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001219
Tim Northover69271c62016-10-12 22:49:11 +00001220 case TargetOpcode::G_SITOFP:
1221 case TargetOpcode::G_UITOFP:
1222 case TargetOpcode::G_FPTOSI:
1223 case TargetOpcode::G_FPTOUI: {
1224 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1225 SrcTy = MRI.getType(I.getOperand(1).getReg());
1226 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1227 if (NewOpc == Opcode)
1228 return false;
1229
1230 I.setDesc(TII.get(NewOpc));
1231 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1232
1233 return true;
1234 }
1235
1236
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001237 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001238 // The importer is currently unable to import pointer types since they
1239 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001240 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001241
Daniel Sandersedd07842017-08-17 09:26:14 +00001242 case TargetOpcode::G_BITCAST:
1243 // Imported SelectionDAG rules can handle every bitcast except those that
1244 // bitcast from a type to the same type. Ideally, these shouldn't occur
1245 // but we might not run an optimizer that deletes them.
1246 if (MRI.getType(I.getOperand(0).getReg()) ==
1247 MRI.getType(I.getOperand(1).getReg()))
1248 return selectCopy(I, TII, MRI, TRI, RBI);
1249 return false;
1250
Tim Northover9ac0eba2016-11-08 00:45:29 +00001251 case TargetOpcode::G_SELECT: {
1252 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1253 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1254 << ", expected: " << LLT::scalar(1) << '\n');
1255 return false;
1256 }
1257
1258 const unsigned CondReg = I.getOperand(1).getReg();
1259 const unsigned TReg = I.getOperand(2).getReg();
1260 const unsigned FReg = I.getOperand(3).getReg();
1261
1262 unsigned CSelOpc = 0;
1263
1264 if (Ty == LLT::scalar(32)) {
1265 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001266 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001267 CSelOpc = AArch64::CSELXr;
1268 } else {
1269 return false;
1270 }
1271
1272 MachineInstr &TstMI =
1273 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1274 .addDef(AArch64::WZR)
1275 .addUse(CondReg)
1276 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1277
1278 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1279 .addDef(I.getOperand(0).getReg())
1280 .addUse(TReg)
1281 .addUse(FReg)
1282 .addImm(AArch64CC::NE);
1283
1284 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1285 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1286
1287 I.eraseFromParent();
1288 return true;
1289 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001290 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001291 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001292 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001293 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001294 return false;
1295 }
1296
1297 unsigned CmpOpc = 0;
1298 unsigned ZReg = 0;
1299
1300 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1301 if (CmpTy == LLT::scalar(32)) {
1302 CmpOpc = AArch64::SUBSWrr;
1303 ZReg = AArch64::WZR;
1304 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1305 CmpOpc = AArch64::SUBSXrr;
1306 ZReg = AArch64::XZR;
1307 } else {
1308 return false;
1309 }
1310
Kristof Beyls22524402017-01-05 10:16:08 +00001311 // CSINC increments the result by one when the condition code is false.
1312 // Therefore, we have to invert the predicate to get an increment by 1 when
1313 // the predicate is true.
1314 const AArch64CC::CondCode invCC =
1315 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1316 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001317
1318 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1319 .addDef(ZReg)
1320 .addUse(I.getOperand(2).getReg())
1321 .addUse(I.getOperand(3).getReg());
1322
1323 MachineInstr &CSetMI =
1324 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1325 .addDef(I.getOperand(0).getReg())
1326 .addUse(AArch64::WZR)
1327 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001328 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001329
1330 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1331 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1332
1333 I.eraseFromParent();
1334 return true;
1335 }
1336
Tim Northover7dd378d2016-10-12 22:49:07 +00001337 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001338 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001339 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001340 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001341 return false;
1342 }
1343
1344 unsigned CmpOpc = 0;
1345 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1346 if (CmpTy == LLT::scalar(32)) {
1347 CmpOpc = AArch64::FCMPSrr;
1348 } else if (CmpTy == LLT::scalar(64)) {
1349 CmpOpc = AArch64::FCMPDrr;
1350 } else {
1351 return false;
1352 }
1353
1354 // FIXME: regbank
1355
1356 AArch64CC::CondCode CC1, CC2;
1357 changeFCMPPredToAArch64CC(
1358 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1359
1360 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1361 .addUse(I.getOperand(2).getReg())
1362 .addUse(I.getOperand(3).getReg());
1363
1364 const unsigned DefReg = I.getOperand(0).getReg();
1365 unsigned Def1Reg = DefReg;
1366 if (CC2 != AArch64CC::AL)
1367 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1368
1369 MachineInstr &CSetMI =
1370 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1371 .addDef(Def1Reg)
1372 .addUse(AArch64::WZR)
1373 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001374 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001375
1376 if (CC2 != AArch64CC::AL) {
1377 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1378 MachineInstr &CSet2MI =
1379 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1380 .addDef(Def2Reg)
1381 .addUse(AArch64::WZR)
1382 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001383 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001384 MachineInstr &OrMI =
1385 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1386 .addDef(DefReg)
1387 .addUse(Def1Reg)
1388 .addUse(Def2Reg);
1389 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1390 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1391 }
1392
1393 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1394 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1395
1396 I.eraseFromParent();
1397 return true;
1398 }
Tim Northovere9600d82017-02-08 17:57:27 +00001399 case TargetOpcode::G_VASTART:
1400 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1401 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001402 case TargetOpcode::G_IMPLICIT_DEF:
1403 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1404 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001405 }
1406
1407 return false;
1408}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001409
1410/// SelectArithImmed - Select an immediate value that can be represented as
1411/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1412/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001413InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001414AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001415 MachineInstr &MI = *Root.getParent();
1416 MachineBasicBlock &MBB = *MI.getParent();
1417 MachineFunction &MF = *MBB.getParent();
1418 MachineRegisterInfo &MRI = MF.getRegInfo();
1419
1420 // This function is called from the addsub_shifted_imm ComplexPattern,
1421 // which lists [imm] as the list of opcode it's interested in, however
1422 // we still need to check whether the operand is actually an immediate
1423 // here because the ComplexPattern opcode list is only used in
1424 // root-level opcode matching.
1425 uint64_t Immed;
1426 if (Root.isImm())
1427 Immed = Root.getImm();
1428 else if (Root.isCImm())
1429 Immed = Root.getCImm()->getZExtValue();
1430 else if (Root.isReg()) {
1431 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1432 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001433 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001434 MachineOperand &Op1 = Def->getOperand(1);
1435 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001436 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001437 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001438 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001439 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001440
1441 unsigned ShiftAmt;
1442
1443 if (Immed >> 12 == 0) {
1444 ShiftAmt = 0;
1445 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1446 ShiftAmt = 12;
1447 Immed = Immed >> 12;
1448 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001449 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001450
1451 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001452 return {{
1453 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1454 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1455 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001456}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001457
Daniel Sandersea8711b2017-10-16 03:36:29 +00001458/// Select a "register plus unscaled signed 9-bit immediate" address. This
1459/// should only match when there is an offset that is not valid for a scaled
1460/// immediate addressing mode. The "Size" argument is the size in bytes of the
1461/// memory reference, which is needed here to know what is valid for a scaled
1462/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001463InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001464AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1465 unsigned Size) const {
1466 MachineRegisterInfo &MRI =
1467 Root.getParent()->getParent()->getParent()->getRegInfo();
1468
1469 if (!Root.isReg())
1470 return None;
1471
1472 if (!isBaseWithConstantOffset(Root, MRI))
1473 return None;
1474
1475 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1476 if (!RootDef)
1477 return None;
1478
1479 MachineOperand &OffImm = RootDef->getOperand(2);
1480 if (!OffImm.isReg())
1481 return None;
1482 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1483 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1484 return None;
1485 int64_t RHSC;
1486 MachineOperand &RHSOp1 = RHS->getOperand(1);
1487 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1488 return None;
1489 RHSC = RHSOp1.getCImm()->getSExtValue();
1490
1491 // If the offset is valid as a scaled immediate, don't match here.
1492 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1493 return None;
1494 if (RHSC >= -256 && RHSC < 256) {
1495 MachineOperand &Base = RootDef->getOperand(1);
1496 return {{
1497 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1498 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1499 }};
1500 }
1501 return None;
1502}
1503
1504/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1505/// "Size" argument is the size in bytes of the memory reference, which
1506/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001507InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001508AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1509 unsigned Size) const {
1510 MachineRegisterInfo &MRI =
1511 Root.getParent()->getParent()->getParent()->getRegInfo();
1512
1513 if (!Root.isReg())
1514 return None;
1515
1516 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1517 if (!RootDef)
1518 return None;
1519
1520 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1521 return {{
1522 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1523 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1524 }};
1525 }
1526
1527 if (isBaseWithConstantOffset(Root, MRI)) {
1528 MachineOperand &LHS = RootDef->getOperand(1);
1529 MachineOperand &RHS = RootDef->getOperand(2);
1530 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1531 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1532 if (LHSDef && RHSDef) {
1533 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1534 unsigned Scale = Log2_32(Size);
1535 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1536 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001537 return {{
1538 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1539 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1540 }};
1541
Daniel Sandersea8711b2017-10-16 03:36:29 +00001542 return {{
1543 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1544 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1545 }};
1546 }
1547 }
1548 }
1549
1550 // Before falling back to our general case, check if the unscaled
1551 // instructions can handle this. If so, that's preferable.
1552 if (selectAddrModeUnscaled(Root, Size).hasValue())
1553 return None;
1554
1555 return {{
1556 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1557 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1558 }};
1559}
1560
Volkan Kelesf7f25682018-01-16 18:44:05 +00001561void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1562 const MachineInstr &MI) const {
1563 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1564 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1565 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1566 assert(CstVal && "Expected constant value");
1567 MIB.addImm(CstVal.getValue());
1568}
1569
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001570namespace llvm {
1571InstructionSelector *
1572createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1573 AArch64Subtarget &Subtarget,
1574 AArch64RegisterBankInfo &RBI) {
1575 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1576}
1577}