blob: 4d5f3fddde96979b2ea17c9e99c77ad2f753f255 [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);
873 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000874 I.setDesc(TII.get(AArch64::MOVaddr));
875 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
876 MachineInstrBuilder MIB(MF, I);
877 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
878 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
879 }
880 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
881 }
882
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000883 case TargetOpcode::G_LOAD:
884 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000885 LLT MemTy = Ty;
886 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000887
Tim Northover5ae83502016-09-15 09:20:34 +0000888 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000889 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000890 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000891 return false;
892 }
893
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000894 auto &MemOp = **I.memoperands_begin();
895 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
896 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
897 return false;
898 }
899
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000900 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000901#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000902 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000903 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000904 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
905 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000906 assert(MRI.getType(PtrReg).isPointer() &&
907 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000908#endif
909
910 const unsigned ValReg = I.getOperand(0).getReg();
911 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
912
913 const unsigned NewOpc =
914 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
915 if (NewOpc == I.getOpcode())
916 return false;
917
918 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000919
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000920 uint64_t Offset = 0;
921 auto *PtrMI = MRI.getVRegDef(PtrReg);
922
923 // Try to fold a GEP into our unsigned immediate addressing mode.
924 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
925 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
926 int64_t Imm = *COff;
927 const unsigned Size = MemTy.getSizeInBits() / 8;
928 const unsigned Scale = Log2_32(Size);
929 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
930 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
931 I.getOperand(1).setReg(Ptr2Reg);
932 PtrMI = MRI.getVRegDef(Ptr2Reg);
933 Offset = Imm / Size;
934 }
935 }
936 }
937
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000938 // If we haven't folded anything into our addressing mode yet, try to fold
939 // a frame index into the base+offset.
940 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
941 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
942
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000943 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000944
945 // If we're storing a 0, use WZR/XZR.
946 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
947 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
948 if (I.getOpcode() == AArch64::STRWui)
949 I.getOperand(0).setReg(AArch64::WZR);
950 else if (I.getOpcode() == AArch64::STRXui)
951 I.getOperand(0).setReg(AArch64::XZR);
952 }
953 }
954
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000955 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
956 }
957
Tim Northover9dd78f82017-02-08 21:22:25 +0000958 case TargetOpcode::G_SMULH:
959 case TargetOpcode::G_UMULH: {
960 // Reject the various things we don't support yet.
961 if (unsupportedBinOp(I, RBI, MRI, TRI))
962 return false;
963
964 const unsigned DefReg = I.getOperand(0).getReg();
965 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
966
967 if (RB.getID() != AArch64::GPRRegBankID) {
968 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
969 return false;
970 }
971
972 if (Ty != LLT::scalar(64)) {
973 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
974 << ", expected: " << LLT::scalar(64) << '\n');
975 return false;
976 }
977
978 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
979 : AArch64::UMULHrr;
980 I.setDesc(TII.get(NewOpc));
981
982 // Now that we selected an opcode, we need to constrain the register
983 // operands to use appropriate classes.
984 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
985 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000986 case TargetOpcode::G_FADD:
987 case TargetOpcode::G_FSUB:
988 case TargetOpcode::G_FMUL:
989 case TargetOpcode::G_FDIV:
990
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000991 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000992 case TargetOpcode::G_SHL:
993 case TargetOpcode::G_LSHR:
994 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000995 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000996 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000997 if (unsupportedBinOp(I, RBI, MRI, TRI))
998 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000999
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001000 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001001
1002 const unsigned DefReg = I.getOperand(0).getReg();
1003 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1004
1005 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1006 if (NewOpc == I.getOpcode())
1007 return false;
1008
1009 I.setDesc(TII.get(NewOpc));
1010 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001011
1012 // Now that we selected an opcode, we need to constrain the register
1013 // operands to use appropriate classes.
1014 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1015 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001016
Tim Northover398c5f52017-02-14 20:56:29 +00001017 case TargetOpcode::G_PTR_MASK: {
1018 uint64_t Align = I.getOperand(2).getImm();
1019 if (Align >= 64 || Align == 0)
1020 return false;
1021
1022 uint64_t Mask = ~((1ULL << Align) - 1);
1023 I.setDesc(TII.get(AArch64::ANDXri));
1024 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1025
1026 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1027 }
Tim Northover037af52c2016-10-31 18:31:09 +00001028 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001029 case TargetOpcode::G_TRUNC: {
1030 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1031 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1032
1033 const unsigned DstReg = I.getOperand(0).getReg();
1034 const unsigned SrcReg = I.getOperand(1).getReg();
1035
1036 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1037 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1038
1039 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001040 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001041 return false;
1042 }
1043
1044 if (DstRB.getID() == AArch64::GPRRegBankID) {
1045 const TargetRegisterClass *DstRC =
1046 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1047 if (!DstRC)
1048 return false;
1049
1050 const TargetRegisterClass *SrcRC =
1051 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1052 if (!SrcRC)
1053 return false;
1054
1055 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1056 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001057 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001058 return false;
1059 }
1060
1061 if (DstRC == SrcRC) {
1062 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001063 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1064 SrcTy == LLT::scalar(64)) {
1065 llvm_unreachable("TableGen can import this case");
1066 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001067 } else if (DstRC == &AArch64::GPR32RegClass &&
1068 SrcRC == &AArch64::GPR64RegClass) {
1069 I.getOperand(1).setSubReg(AArch64::sub_32);
1070 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001071 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001072 return false;
1073 }
1074
1075 I.setDesc(TII.get(TargetOpcode::COPY));
1076 return true;
1077 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1078 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1079 I.setDesc(TII.get(AArch64::XTNv4i16));
1080 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1081 return true;
1082 }
1083 }
1084
1085 return false;
1086 }
1087
Tim Northover3d38b3a2016-10-11 20:50:21 +00001088 case TargetOpcode::G_ANYEXT: {
1089 const unsigned DstReg = I.getOperand(0).getReg();
1090 const unsigned SrcReg = I.getOperand(1).getReg();
1091
Quentin Colombetcb629a82016-10-12 03:57:49 +00001092 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1093 if (RBDst.getID() != AArch64::GPRRegBankID) {
1094 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1095 return false;
1096 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001097
Quentin Colombetcb629a82016-10-12 03:57:49 +00001098 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1099 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1100 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001101 return false;
1102 }
1103
1104 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1105
1106 if (DstSize == 0) {
1107 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1108 return false;
1109 }
1110
Quentin Colombetcb629a82016-10-12 03:57:49 +00001111 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001112 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1113 << ", expected: 32 or 64\n");
1114 return false;
1115 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001116 // At this point G_ANYEXT is just like a plain COPY, but we need
1117 // to explicitly form the 64-bit value if any.
1118 if (DstSize > 32) {
1119 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1120 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1121 .addDef(ExtSrc)
1122 .addImm(0)
1123 .addUse(SrcReg)
1124 .addImm(AArch64::sub_32);
1125 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001126 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001127 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001128 }
1129
1130 case TargetOpcode::G_ZEXT:
1131 case TargetOpcode::G_SEXT: {
1132 unsigned Opcode = I.getOpcode();
1133 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1134 SrcTy = MRI.getType(I.getOperand(1).getReg());
1135 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1136 const unsigned DefReg = I.getOperand(0).getReg();
1137 const unsigned SrcReg = I.getOperand(1).getReg();
1138 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1139
1140 if (RB.getID() != AArch64::GPRRegBankID) {
1141 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1142 << ", expected: GPR\n");
1143 return false;
1144 }
1145
1146 MachineInstr *ExtI;
1147 if (DstTy == LLT::scalar(64)) {
1148 // FIXME: Can we avoid manually doing this?
1149 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1150 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1151 << " operand\n");
1152 return false;
1153 }
1154
1155 const unsigned SrcXReg =
1156 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1157 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1158 .addDef(SrcXReg)
1159 .addImm(0)
1160 .addUse(SrcReg)
1161 .addImm(AArch64::sub_32);
1162
1163 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1164 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1165 .addDef(DefReg)
1166 .addUse(SrcXReg)
1167 .addImm(0)
1168 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001169 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001170 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1171 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1172 .addDef(DefReg)
1173 .addUse(SrcReg)
1174 .addImm(0)
1175 .addImm(SrcTy.getSizeInBits() - 1);
1176 } else {
1177 return false;
1178 }
1179
1180 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1181
1182 I.eraseFromParent();
1183 return true;
1184 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001185
Tim Northover69271c62016-10-12 22:49:11 +00001186 case TargetOpcode::G_SITOFP:
1187 case TargetOpcode::G_UITOFP:
1188 case TargetOpcode::G_FPTOSI:
1189 case TargetOpcode::G_FPTOUI: {
1190 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1191 SrcTy = MRI.getType(I.getOperand(1).getReg());
1192 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1193 if (NewOpc == Opcode)
1194 return false;
1195
1196 I.setDesc(TII.get(NewOpc));
1197 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1198
1199 return true;
1200 }
1201
1202
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001203 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001204 // The importer is currently unable to import pointer types since they
1205 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001206 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001207
Daniel Sandersedd07842017-08-17 09:26:14 +00001208 case TargetOpcode::G_BITCAST:
1209 // Imported SelectionDAG rules can handle every bitcast except those that
1210 // bitcast from a type to the same type. Ideally, these shouldn't occur
1211 // but we might not run an optimizer that deletes them.
1212 if (MRI.getType(I.getOperand(0).getReg()) ==
1213 MRI.getType(I.getOperand(1).getReg()))
1214 return selectCopy(I, TII, MRI, TRI, RBI);
1215 return false;
1216
Tim Northover9ac0eba2016-11-08 00:45:29 +00001217 case TargetOpcode::G_SELECT: {
1218 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1219 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1220 << ", expected: " << LLT::scalar(1) << '\n');
1221 return false;
1222 }
1223
1224 const unsigned CondReg = I.getOperand(1).getReg();
1225 const unsigned TReg = I.getOperand(2).getReg();
1226 const unsigned FReg = I.getOperand(3).getReg();
1227
1228 unsigned CSelOpc = 0;
1229
1230 if (Ty == LLT::scalar(32)) {
1231 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001232 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001233 CSelOpc = AArch64::CSELXr;
1234 } else {
1235 return false;
1236 }
1237
1238 MachineInstr &TstMI =
1239 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1240 .addDef(AArch64::WZR)
1241 .addUse(CondReg)
1242 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1243
1244 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1245 .addDef(I.getOperand(0).getReg())
1246 .addUse(TReg)
1247 .addUse(FReg)
1248 .addImm(AArch64CC::NE);
1249
1250 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1251 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1252
1253 I.eraseFromParent();
1254 return true;
1255 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001256 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001257 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001258 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001259 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001260 return false;
1261 }
1262
1263 unsigned CmpOpc = 0;
1264 unsigned ZReg = 0;
1265
1266 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1267 if (CmpTy == LLT::scalar(32)) {
1268 CmpOpc = AArch64::SUBSWrr;
1269 ZReg = AArch64::WZR;
1270 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1271 CmpOpc = AArch64::SUBSXrr;
1272 ZReg = AArch64::XZR;
1273 } else {
1274 return false;
1275 }
1276
Kristof Beyls22524402017-01-05 10:16:08 +00001277 // CSINC increments the result by one when the condition code is false.
1278 // Therefore, we have to invert the predicate to get an increment by 1 when
1279 // the predicate is true.
1280 const AArch64CC::CondCode invCC =
1281 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1282 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001283
1284 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1285 .addDef(ZReg)
1286 .addUse(I.getOperand(2).getReg())
1287 .addUse(I.getOperand(3).getReg());
1288
1289 MachineInstr &CSetMI =
1290 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1291 .addDef(I.getOperand(0).getReg())
1292 .addUse(AArch64::WZR)
1293 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001294 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001295
1296 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1297 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1298
1299 I.eraseFromParent();
1300 return true;
1301 }
1302
Tim Northover7dd378d2016-10-12 22:49:07 +00001303 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001304 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001305 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001306 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001307 return false;
1308 }
1309
1310 unsigned CmpOpc = 0;
1311 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1312 if (CmpTy == LLT::scalar(32)) {
1313 CmpOpc = AArch64::FCMPSrr;
1314 } else if (CmpTy == LLT::scalar(64)) {
1315 CmpOpc = AArch64::FCMPDrr;
1316 } else {
1317 return false;
1318 }
1319
1320 // FIXME: regbank
1321
1322 AArch64CC::CondCode CC1, CC2;
1323 changeFCMPPredToAArch64CC(
1324 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1325
1326 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1327 .addUse(I.getOperand(2).getReg())
1328 .addUse(I.getOperand(3).getReg());
1329
1330 const unsigned DefReg = I.getOperand(0).getReg();
1331 unsigned Def1Reg = DefReg;
1332 if (CC2 != AArch64CC::AL)
1333 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1334
1335 MachineInstr &CSetMI =
1336 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1337 .addDef(Def1Reg)
1338 .addUse(AArch64::WZR)
1339 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001340 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001341
1342 if (CC2 != AArch64CC::AL) {
1343 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1344 MachineInstr &CSet2MI =
1345 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1346 .addDef(Def2Reg)
1347 .addUse(AArch64::WZR)
1348 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001349 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001350 MachineInstr &OrMI =
1351 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1352 .addDef(DefReg)
1353 .addUse(Def1Reg)
1354 .addUse(Def2Reg);
1355 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1356 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1357 }
1358
1359 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1360 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1361
1362 I.eraseFromParent();
1363 return true;
1364 }
Tim Northovere9600d82017-02-08 17:57:27 +00001365 case TargetOpcode::G_VASTART:
1366 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1367 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001368 case TargetOpcode::G_IMPLICIT_DEF:
1369 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1370 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001371 }
1372
1373 return false;
1374}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001375
1376/// SelectArithImmed - Select an immediate value that can be represented as
1377/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1378/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001379InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001380AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001381 MachineInstr &MI = *Root.getParent();
1382 MachineBasicBlock &MBB = *MI.getParent();
1383 MachineFunction &MF = *MBB.getParent();
1384 MachineRegisterInfo &MRI = MF.getRegInfo();
1385
1386 // This function is called from the addsub_shifted_imm ComplexPattern,
1387 // which lists [imm] as the list of opcode it's interested in, however
1388 // we still need to check whether the operand is actually an immediate
1389 // here because the ComplexPattern opcode list is only used in
1390 // root-level opcode matching.
1391 uint64_t Immed;
1392 if (Root.isImm())
1393 Immed = Root.getImm();
1394 else if (Root.isCImm())
1395 Immed = Root.getCImm()->getZExtValue();
1396 else if (Root.isReg()) {
1397 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1398 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001399 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001400 MachineOperand &Op1 = Def->getOperand(1);
1401 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001402 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001403 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001404 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001405 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001406
1407 unsigned ShiftAmt;
1408
1409 if (Immed >> 12 == 0) {
1410 ShiftAmt = 0;
1411 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1412 ShiftAmt = 12;
1413 Immed = Immed >> 12;
1414 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001415 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001416
1417 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001418 return {{
1419 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1420 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1421 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001422}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001423
Daniel Sandersea8711b2017-10-16 03:36:29 +00001424/// Select a "register plus unscaled signed 9-bit immediate" address. This
1425/// should only match when there is an offset that is not valid for a scaled
1426/// immediate addressing mode. The "Size" argument is the size in bytes of the
1427/// memory reference, which is needed here to know what is valid for a scaled
1428/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001429InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001430AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1431 unsigned Size) const {
1432 MachineRegisterInfo &MRI =
1433 Root.getParent()->getParent()->getParent()->getRegInfo();
1434
1435 if (!Root.isReg())
1436 return None;
1437
1438 if (!isBaseWithConstantOffset(Root, MRI))
1439 return None;
1440
1441 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1442 if (!RootDef)
1443 return None;
1444
1445 MachineOperand &OffImm = RootDef->getOperand(2);
1446 if (!OffImm.isReg())
1447 return None;
1448 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1449 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1450 return None;
1451 int64_t RHSC;
1452 MachineOperand &RHSOp1 = RHS->getOperand(1);
1453 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1454 return None;
1455 RHSC = RHSOp1.getCImm()->getSExtValue();
1456
1457 // If the offset is valid as a scaled immediate, don't match here.
1458 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1459 return None;
1460 if (RHSC >= -256 && RHSC < 256) {
1461 MachineOperand &Base = RootDef->getOperand(1);
1462 return {{
1463 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1464 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1465 }};
1466 }
1467 return None;
1468}
1469
1470/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1471/// "Size" argument is the size in bytes of the memory reference, which
1472/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001473InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001474AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1475 unsigned Size) const {
1476 MachineRegisterInfo &MRI =
1477 Root.getParent()->getParent()->getParent()->getRegInfo();
1478
1479 if (!Root.isReg())
1480 return None;
1481
1482 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1483 if (!RootDef)
1484 return None;
1485
1486 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1487 return {{
1488 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1489 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1490 }};
1491 }
1492
1493 if (isBaseWithConstantOffset(Root, MRI)) {
1494 MachineOperand &LHS = RootDef->getOperand(1);
1495 MachineOperand &RHS = RootDef->getOperand(2);
1496 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1497 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1498 if (LHSDef && RHSDef) {
1499 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1500 unsigned Scale = Log2_32(Size);
1501 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1502 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001503 return {{
1504 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1505 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1506 }};
1507
Daniel Sandersea8711b2017-10-16 03:36:29 +00001508 return {{
1509 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1510 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1511 }};
1512 }
1513 }
1514 }
1515
1516 // Before falling back to our general case, check if the unscaled
1517 // instructions can handle this. If so, that's preferable.
1518 if (selectAddrModeUnscaled(Root, Size).hasValue())
1519 return None;
1520
1521 return {{
1522 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1523 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1524 }};
1525}
1526
Volkan Kelesf7f25682018-01-16 18:44:05 +00001527void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1528 const MachineInstr &MI) const {
1529 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1530 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1531 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1532 assert(CstVal && "Expected constant value");
1533 MIB.addImm(CstVal.getValue());
1534}
1535
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001536namespace llvm {
1537InstructionSelector *
1538createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1539 AArch64Subtarget &Subtarget,
1540 AArch64RegisterBankInfo &RBI) {
1541 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1542}
1543}