blob: c2d3ae31c624304d490dfda8476b76e627b12079 [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
Daniel Sanders0b5293f2017-04-06 09:49:34 +000095 const AArch64TargetMachine &TM;
96 const AArch64Subtarget &STI;
97 const AArch64InstrInfo &TII;
98 const AArch64RegisterInfo &TRI;
99 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000100
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000101#define GET_GLOBALISEL_PREDICATES_DECL
102#include "AArch64GenGlobalISel.inc"
103#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000104
105// We declare the temporaries used by selectImpl() in the class to minimize the
106// cost of constructing placeholder values.
107#define GET_GLOBALISEL_TEMPORARIES_DECL
108#include "AArch64GenGlobalISel.inc"
109#undef GET_GLOBALISEL_TEMPORARIES_DECL
110};
111
112} // end anonymous namespace
113
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000114#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000115#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000116#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000117
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000118AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +0000119 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
120 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000121 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000122 TRI(*STI.getRegisterInfo()), RBI(RBI),
123#define GET_GLOBALISEL_PREDICATES_INIT
124#include "AArch64GenGlobalISel.inc"
125#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000126#define GET_GLOBALISEL_TEMPORARIES_INIT
127#include "AArch64GenGlobalISel.inc"
128#undef GET_GLOBALISEL_TEMPORARIES_INIT
129{
130}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000131
Tim Northoverfb8d9892016-10-12 22:49:15 +0000132// FIXME: This should be target-independent, inferred from the types declared
133// for each class in the bank.
134static const TargetRegisterClass *
135getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
136 const RegisterBankInfo &RBI) {
137 if (RB.getID() == AArch64::GPRRegBankID) {
138 if (Ty.getSizeInBits() <= 32)
139 return &AArch64::GPR32RegClass;
140 if (Ty.getSizeInBits() == 64)
141 return &AArch64::GPR64RegClass;
142 return nullptr;
143 }
144
145 if (RB.getID() == AArch64::FPRRegBankID) {
146 if (Ty.getSizeInBits() == 32)
147 return &AArch64::FPR32RegClass;
148 if (Ty.getSizeInBits() == 64)
149 return &AArch64::FPR64RegClass;
150 if (Ty.getSizeInBits() == 128)
151 return &AArch64::FPR128RegClass;
152 return nullptr;
153 }
154
155 return nullptr;
156}
157
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000158/// Check whether \p I is a currently unsupported binary operation:
159/// - it has an unsized type
160/// - an operand is not a vreg
161/// - all operands are not in the same bank
162/// These are checks that should someday live in the verifier, but right now,
163/// these are mostly limitations of the aarch64 selector.
164static bool unsupportedBinOp(const MachineInstr &I,
165 const AArch64RegisterBankInfo &RBI,
166 const MachineRegisterInfo &MRI,
167 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000168 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000169 if (!Ty.isValid()) {
170 DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000171 return true;
172 }
173
174 const RegisterBank *PrevOpBank = nullptr;
175 for (auto &MO : I.operands()) {
176 // FIXME: Support non-register operands.
177 if (!MO.isReg()) {
178 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
179 return true;
180 }
181
182 // FIXME: Can generic operations have physical registers operands? If
183 // so, this will need to be taught about that, and we'll need to get the
184 // bank out of the minimal class for the register.
185 // Either way, this needs to be documented (and possibly verified).
186 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
187 DEBUG(dbgs() << "Generic inst has physical register operand\n");
188 return true;
189 }
190
191 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
192 if (!OpBank) {
193 DEBUG(dbgs() << "Generic register has no bank or class\n");
194 return true;
195 }
196
197 if (PrevOpBank && OpBank != PrevOpBank) {
198 DEBUG(dbgs() << "Generic inst operands have different banks\n");
199 return true;
200 }
201 PrevOpBank = OpBank;
202 }
203 return false;
204}
205
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000206/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000207/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000208/// and of size \p OpSize.
209/// \returns \p GenericOpc if the combination is unsupported.
210static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
211 unsigned OpSize) {
212 switch (RegBankID) {
213 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000214 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000215 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000216 case TargetOpcode::G_SHL:
217 return AArch64::LSLVWr;
218 case TargetOpcode::G_LSHR:
219 return AArch64::LSRVWr;
220 case TargetOpcode::G_ASHR:
221 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000222 default:
223 return GenericOpc;
224 }
Tim Northover55782222016-10-18 20:03:48 +0000225 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000226 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000227 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000228 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000229 case TargetOpcode::G_SHL:
230 return AArch64::LSLVXr;
231 case TargetOpcode::G_LSHR:
232 return AArch64::LSRVXr;
233 case TargetOpcode::G_ASHR:
234 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000235 default:
236 return GenericOpc;
237 }
238 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000239 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000240 case AArch64::FPRRegBankID:
241 switch (OpSize) {
242 case 32:
243 switch (GenericOpc) {
244 case TargetOpcode::G_FADD:
245 return AArch64::FADDSrr;
246 case TargetOpcode::G_FSUB:
247 return AArch64::FSUBSrr;
248 case TargetOpcode::G_FMUL:
249 return AArch64::FMULSrr;
250 case TargetOpcode::G_FDIV:
251 return AArch64::FDIVSrr;
252 default:
253 return GenericOpc;
254 }
255 case 64:
256 switch (GenericOpc) {
257 case TargetOpcode::G_FADD:
258 return AArch64::FADDDrr;
259 case TargetOpcode::G_FSUB:
260 return AArch64::FSUBDrr;
261 case TargetOpcode::G_FMUL:
262 return AArch64::FMULDrr;
263 case TargetOpcode::G_FDIV:
264 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000265 case TargetOpcode::G_OR:
266 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000267 default:
268 return GenericOpc;
269 }
270 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000271 break;
272 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000273 return GenericOpc;
274}
275
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000276/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
277/// appropriate for the (value) register bank \p RegBankID and of memory access
278/// size \p OpSize. This returns the variant with the base+unsigned-immediate
279/// addressing mode (e.g., LDRXui).
280/// \returns \p GenericOpc if the combination is unsupported.
281static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
282 unsigned OpSize) {
283 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
284 switch (RegBankID) {
285 case AArch64::GPRRegBankID:
286 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000287 case 8:
288 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
289 case 16:
290 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000291 case 32:
292 return isStore ? AArch64::STRWui : AArch64::LDRWui;
293 case 64:
294 return isStore ? AArch64::STRXui : AArch64::LDRXui;
295 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000296 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000297 case AArch64::FPRRegBankID:
298 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000299 case 8:
300 return isStore ? AArch64::STRBui : AArch64::LDRBui;
301 case 16:
302 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000303 case 32:
304 return isStore ? AArch64::STRSui : AArch64::LDRSui;
305 case 64:
306 return isStore ? AArch64::STRDui : AArch64::LDRDui;
307 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000308 break;
309 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000310 return GenericOpc;
311}
312
Quentin Colombetcb629a82016-10-12 03:57:49 +0000313static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
314 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
315 const RegisterBankInfo &RBI) {
316
317 unsigned DstReg = I.getOperand(0).getReg();
318 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
319 assert(I.isCopy() && "Generic operators do not allow physical registers");
320 return true;
321 }
322
323 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
324 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
325 unsigned SrcReg = I.getOperand(1).getReg();
326 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
327 (void)SrcSize;
328 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
329 "No phys reg on generic operators");
330 assert(
331 (DstSize == SrcSize ||
332 // Copies are a mean to setup initial types, the number of
333 // bits may not exactly match.
334 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
335 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
336 // Copies are a mean to copy bits around, as long as we are
337 // on the same register class, that's fine. Otherwise, that
338 // means we need some SUBREG_TO_REG or AND & co.
339 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
340 "Copy with different width?!");
341 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
342 "GPRs cannot get more than 64-bit width values");
343 const TargetRegisterClass *RC = nullptr;
344
345 if (RegBank.getID() == AArch64::FPRRegBankID) {
Ahmed Bougachaa7aa2a92017-09-12 21:04:10 +0000346 if (DstSize <= 16)
347 RC = &AArch64::FPR16RegClass;
348 else if (DstSize <= 32)
Quentin Colombetcb629a82016-10-12 03:57:49 +0000349 RC = &AArch64::FPR32RegClass;
350 else if (DstSize <= 64)
351 RC = &AArch64::FPR64RegClass;
352 else if (DstSize <= 128)
353 RC = &AArch64::FPR128RegClass;
354 else {
355 DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
356 return false;
357 }
358 } else {
359 assert(RegBank.getID() == AArch64::GPRRegBankID &&
360 "Bitcast for the flags?");
361 RC =
362 DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass;
363 }
364
365 // No need to constrain SrcReg. It will get constrained when
366 // we hit another of its use or its defs.
367 // Copies do not have constraints.
368 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
369 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
370 << " operand\n");
371 return false;
372 }
373 I.setDesc(TII.get(AArch64::COPY));
374 return true;
375}
376
Tim Northover69271c62016-10-12 22:49:11 +0000377static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
378 if (!DstTy.isScalar() || !SrcTy.isScalar())
379 return GenericOpc;
380
381 const unsigned DstSize = DstTy.getSizeInBits();
382 const unsigned SrcSize = SrcTy.getSizeInBits();
383
384 switch (DstSize) {
385 case 32:
386 switch (SrcSize) {
387 case 32:
388 switch (GenericOpc) {
389 case TargetOpcode::G_SITOFP:
390 return AArch64::SCVTFUWSri;
391 case TargetOpcode::G_UITOFP:
392 return AArch64::UCVTFUWSri;
393 case TargetOpcode::G_FPTOSI:
394 return AArch64::FCVTZSUWSr;
395 case TargetOpcode::G_FPTOUI:
396 return AArch64::FCVTZUUWSr;
397 default:
398 return GenericOpc;
399 }
400 case 64:
401 switch (GenericOpc) {
402 case TargetOpcode::G_SITOFP:
403 return AArch64::SCVTFUXSri;
404 case TargetOpcode::G_UITOFP:
405 return AArch64::UCVTFUXSri;
406 case TargetOpcode::G_FPTOSI:
407 return AArch64::FCVTZSUWDr;
408 case TargetOpcode::G_FPTOUI:
409 return AArch64::FCVTZUUWDr;
410 default:
411 return GenericOpc;
412 }
413 default:
414 return GenericOpc;
415 }
416 case 64:
417 switch (SrcSize) {
418 case 32:
419 switch (GenericOpc) {
420 case TargetOpcode::G_SITOFP:
421 return AArch64::SCVTFUWDri;
422 case TargetOpcode::G_UITOFP:
423 return AArch64::UCVTFUWDri;
424 case TargetOpcode::G_FPTOSI:
425 return AArch64::FCVTZSUXSr;
426 case TargetOpcode::G_FPTOUI:
427 return AArch64::FCVTZUUXSr;
428 default:
429 return GenericOpc;
430 }
431 case 64:
432 switch (GenericOpc) {
433 case TargetOpcode::G_SITOFP:
434 return AArch64::SCVTFUXDri;
435 case TargetOpcode::G_UITOFP:
436 return AArch64::UCVTFUXDri;
437 case TargetOpcode::G_FPTOSI:
438 return AArch64::FCVTZSUXDr;
439 case TargetOpcode::G_FPTOUI:
440 return AArch64::FCVTZUUXDr;
441 default:
442 return GenericOpc;
443 }
444 default:
445 return GenericOpc;
446 }
447 default:
448 return GenericOpc;
449 };
450 return GenericOpc;
451}
452
Tim Northover6c02ad52016-10-12 22:49:04 +0000453static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
454 switch (P) {
455 default:
456 llvm_unreachable("Unknown condition code!");
457 case CmpInst::ICMP_NE:
458 return AArch64CC::NE;
459 case CmpInst::ICMP_EQ:
460 return AArch64CC::EQ;
461 case CmpInst::ICMP_SGT:
462 return AArch64CC::GT;
463 case CmpInst::ICMP_SGE:
464 return AArch64CC::GE;
465 case CmpInst::ICMP_SLT:
466 return AArch64CC::LT;
467 case CmpInst::ICMP_SLE:
468 return AArch64CC::LE;
469 case CmpInst::ICMP_UGT:
470 return AArch64CC::HI;
471 case CmpInst::ICMP_UGE:
472 return AArch64CC::HS;
473 case CmpInst::ICMP_ULT:
474 return AArch64CC::LO;
475 case CmpInst::ICMP_ULE:
476 return AArch64CC::LS;
477 }
478}
479
Tim Northover7dd378d2016-10-12 22:49:07 +0000480static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
481 AArch64CC::CondCode &CondCode,
482 AArch64CC::CondCode &CondCode2) {
483 CondCode2 = AArch64CC::AL;
484 switch (P) {
485 default:
486 llvm_unreachable("Unknown FP condition!");
487 case CmpInst::FCMP_OEQ:
488 CondCode = AArch64CC::EQ;
489 break;
490 case CmpInst::FCMP_OGT:
491 CondCode = AArch64CC::GT;
492 break;
493 case CmpInst::FCMP_OGE:
494 CondCode = AArch64CC::GE;
495 break;
496 case CmpInst::FCMP_OLT:
497 CondCode = AArch64CC::MI;
498 break;
499 case CmpInst::FCMP_OLE:
500 CondCode = AArch64CC::LS;
501 break;
502 case CmpInst::FCMP_ONE:
503 CondCode = AArch64CC::MI;
504 CondCode2 = AArch64CC::GT;
505 break;
506 case CmpInst::FCMP_ORD:
507 CondCode = AArch64CC::VC;
508 break;
509 case CmpInst::FCMP_UNO:
510 CondCode = AArch64CC::VS;
511 break;
512 case CmpInst::FCMP_UEQ:
513 CondCode = AArch64CC::EQ;
514 CondCode2 = AArch64CC::VS;
515 break;
516 case CmpInst::FCMP_UGT:
517 CondCode = AArch64CC::HI;
518 break;
519 case CmpInst::FCMP_UGE:
520 CondCode = AArch64CC::PL;
521 break;
522 case CmpInst::FCMP_ULT:
523 CondCode = AArch64CC::LT;
524 break;
525 case CmpInst::FCMP_ULE:
526 CondCode = AArch64CC::LE;
527 break;
528 case CmpInst::FCMP_UNE:
529 CondCode = AArch64CC::NE;
530 break;
531 }
532}
533
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000534bool AArch64InstructionSelector::selectCompareBranch(
535 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
536
537 const unsigned CondReg = I.getOperand(0).getReg();
538 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
539 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000540 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
541 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000542 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
543 return false;
544
545 unsigned LHS = CCMI->getOperand(2).getReg();
546 unsigned RHS = CCMI->getOperand(3).getReg();
547 if (!getConstantVRegVal(RHS, MRI))
548 std::swap(RHS, LHS);
549
550 const auto RHSImm = getConstantVRegVal(RHS, MRI);
551 if (!RHSImm || *RHSImm != 0)
552 return false;
553
554 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
555 if (RB.getID() != AArch64::GPRRegBankID)
556 return false;
557
558 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
559 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
560 return false;
561
562 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
563 unsigned CBOpc = 0;
564 if (CmpWidth <= 32)
565 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
566 else if (CmpWidth == 64)
567 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
568 else
569 return false;
570
571 auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
572 .addUse(LHS)
573 .addMBB(DestMBB);
574
575 constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
576 I.eraseFromParent();
577 return true;
578}
579
Tim Northovere9600d82017-02-08 17:57:27 +0000580bool AArch64InstructionSelector::selectVaStartAAPCS(
581 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
582 return false;
583}
584
585bool AArch64InstructionSelector::selectVaStartDarwin(
586 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
587 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
588 unsigned ListReg = I.getOperand(0).getReg();
589
590 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
591
592 auto MIB =
593 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
594 .addDef(ArgsAddrReg)
595 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
596 .addImm(0)
597 .addImm(0);
598
599 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
600
601 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
602 .addUse(ArgsAddrReg)
603 .addUse(ListReg)
604 .addImm(0)
605 .addMemOperand(*I.memoperands_begin());
606
607 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
608 I.eraseFromParent();
609 return true;
610}
611
Daniel Sandersf76f3152017-11-16 00:46:35 +0000612bool AArch64InstructionSelector::select(MachineInstr &I,
613 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000614 assert(I.getParent() && "Instruction should be in a basic block!");
615 assert(I.getParent()->getParent() && "Instruction should be in a function!");
616
617 MachineBasicBlock &MBB = *I.getParent();
618 MachineFunction &MF = *MBB.getParent();
619 MachineRegisterInfo &MRI = MF.getRegInfo();
620
Tim Northovercdf23f12016-10-31 18:30:59 +0000621 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000622 // G_PHI requires same handling as PHI
623 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000624 // Certain non-generic instructions also need some special handling.
625
626 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
627 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000628
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000629 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000630 const unsigned DefReg = I.getOperand(0).getReg();
631 const LLT DefTy = MRI.getType(DefReg);
632
633 const TargetRegisterClass *DefRC = nullptr;
634 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
635 DefRC = TRI.getRegClass(DefReg);
636 } else {
637 const RegClassOrRegBank &RegClassOrBank =
638 MRI.getRegClassOrRegBank(DefReg);
639
640 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
641 if (!DefRC) {
642 if (!DefTy.isValid()) {
643 DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
644 return false;
645 }
646 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
647 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
648 if (!DefRC) {
649 DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
650 return false;
651 }
652 }
653 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000654 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000655
656 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
657 }
658
659 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000660 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000661
662 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000663 }
664
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000665
666 if (I.getNumOperands() != I.getNumExplicitOperands()) {
667 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
668 return false;
669 }
670
Daniel Sandersf76f3152017-11-16 00:46:35 +0000671 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000672 return true;
673
Tim Northover32a078a2016-09-15 10:09:59 +0000674 LLT Ty =
675 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000676
Tim Northover69271c62016-10-12 22:49:11 +0000677 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000678 case TargetOpcode::G_BRCOND: {
679 if (Ty.getSizeInBits() > 32) {
680 // We shouldn't need this on AArch64, but it would be implemented as an
681 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
682 // bit being tested is < 32.
683 DEBUG(dbgs() << "G_BRCOND has type: " << Ty
684 << ", expected at most 32-bits");
685 return false;
686 }
687
688 const unsigned CondReg = I.getOperand(0).getReg();
689 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
690
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000691 if (selectCompareBranch(I, MF, MRI))
692 return true;
693
Tim Northover5e3dbf32016-10-12 22:49:01 +0000694 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
695 .addUse(CondReg)
696 .addImm(/*bit offset=*/0)
697 .addMBB(DestMBB);
698
699 I.eraseFromParent();
700 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
701 }
702
Kristof Beyls65a12c02017-01-30 09:13:18 +0000703 case TargetOpcode::G_BRINDIRECT: {
704 I.setDesc(TII.get(AArch64::BR));
705 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
706 }
707
Tim Northover4494d692016-10-18 19:47:57 +0000708 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000709 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000710 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
711
712 const LLT s32 = LLT::scalar(32);
713 const LLT s64 = LLT::scalar(64);
714 const LLT p0 = LLT::pointer(0, 64);
715
716 const unsigned DefReg = I.getOperand(0).getReg();
717 const LLT DefTy = MRI.getType(DefReg);
718 const unsigned DefSize = DefTy.getSizeInBits();
719 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
720
721 // FIXME: Redundant check, but even less readable when factored out.
722 if (isFP) {
723 if (Ty != s32 && Ty != s64) {
724 DEBUG(dbgs() << "Unable to materialize FP " << Ty
725 << " constant, expected: " << s32 << " or " << s64
726 << '\n');
727 return false;
728 }
729
730 if (RB.getID() != AArch64::FPRRegBankID) {
731 DEBUG(dbgs() << "Unable to materialize FP " << Ty
732 << " constant on bank: " << RB << ", expected: FPR\n");
733 return false;
734 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000735
736 // The case when we have 0.0 is covered by tablegen. Reject it here so we
737 // can be sure tablegen works correctly and isn't rescued by this code.
738 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
739 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000740 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000741 // s32 and s64 are covered by tablegen.
742 if (Ty != p0) {
Tim Northover4494d692016-10-18 19:47:57 +0000743 DEBUG(dbgs() << "Unable to materialize integer " << Ty
744 << " constant, expected: " << s32 << ", " << s64 << ", or "
745 << p0 << '\n');
746 return false;
747 }
748
749 if (RB.getID() != AArch64::GPRRegBankID) {
750 DEBUG(dbgs() << "Unable to materialize integer " << Ty
751 << " constant on bank: " << RB << ", expected: GPR\n");
752 return false;
753 }
754 }
755
756 const unsigned MovOpc =
757 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
758
759 I.setDesc(TII.get(MovOpc));
760
761 if (isFP) {
762 const TargetRegisterClass &GPRRC =
763 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
764 const TargetRegisterClass &FPRRC =
765 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
766
767 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
768 MachineOperand &RegOp = I.getOperand(0);
769 RegOp.setReg(DefGPRReg);
770
771 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
772 TII.get(AArch64::COPY))
773 .addDef(DefReg)
774 .addUse(DefGPRReg);
775
776 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
777 DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
778 return false;
779 }
780
781 MachineOperand &ImmOp = I.getOperand(1);
782 // FIXME: Is going through int64_t always correct?
783 ImmOp.ChangeToImmediate(
784 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000785 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000786 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
787 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000788 } else if (I.getOperand(1).isImm()) {
789 uint64_t Val = I.getOperand(1).getImm();
790 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000791 }
792
793 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
794 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000795 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000796 case TargetOpcode::G_EXTRACT: {
797 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
798 // Larger extracts are vectors, same-size extracts should be something else
799 // by now (either split up or simplified to a COPY).
800 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
801 return false;
802
803 I.setDesc(TII.get(AArch64::UBFMXri));
804 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
805 Ty.getSizeInBits() - 1);
806
807 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
808 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
809 TII.get(AArch64::COPY))
810 .addDef(I.getOperand(0).getReg())
811 .addUse(DstReg, 0, AArch64::sub_32);
812 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
813 AArch64::GPR32RegClass, MRI);
814 I.getOperand(0).setReg(DstReg);
815
816 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
817 }
818
819 case TargetOpcode::G_INSERT: {
820 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
821 // Larger inserts are vectors, same-size ones should be something else by
822 // now (split up or turned into COPYs).
823 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
824 return false;
825
826 I.setDesc(TII.get(AArch64::BFMXri));
827 unsigned LSB = I.getOperand(3).getImm();
828 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
829 I.getOperand(3).setImm((64 - LSB) % 64);
830 MachineInstrBuilder(MF, I).addImm(Width - 1);
831
832 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
833 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
834 TII.get(AArch64::SUBREG_TO_REG))
835 .addDef(SrcReg)
836 .addImm(0)
837 .addUse(I.getOperand(2).getReg())
838 .addImm(AArch64::sub_32);
839 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
840 AArch64::GPR32RegClass, MRI);
841 I.getOperand(2).setReg(SrcReg);
842
843 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
844 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000845 case TargetOpcode::G_FRAME_INDEX: {
846 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000847 if (Ty != LLT::pointer(0, 64)) {
Tim Northover0f140c72016-09-09 11:46:34 +0000848 DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
Tim Northover5ae83502016-09-15 09:20:34 +0000849 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000850 return false;
851 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000852 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000853
854 // MOs for a #0 shifted immediate.
855 I.addOperand(MachineOperand::CreateImm(0));
856 I.addOperand(MachineOperand::CreateImm(0));
857
858 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
859 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000860
861 case TargetOpcode::G_GLOBAL_VALUE: {
862 auto GV = I.getOperand(1).getGlobal();
863 if (GV->isThreadLocal()) {
864 // FIXME: we don't support TLS yet.
865 return false;
866 }
867 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000868 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000869 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000870 I.getOperand(1).setTargetFlags(OpFlags);
871 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000872 I.setDesc(TII.get(AArch64::MOVaddr));
873 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
874 MachineInstrBuilder MIB(MF, I);
875 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
876 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
877 }
878 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
879 }
880
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000881 case TargetOpcode::G_LOAD:
882 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000883 LLT MemTy = Ty;
884 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000885
Tim Northover5ae83502016-09-15 09:20:34 +0000886 if (PtrTy != LLT::pointer(0, 64)) {
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000887 DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
Tim Northover5ae83502016-09-15 09:20:34 +0000888 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000889 return false;
890 }
891
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000892 auto &MemOp = **I.memoperands_begin();
893 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
894 DEBUG(dbgs() << "Atomic load/store not supported yet\n");
895 return false;
896 }
897
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000898 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000899#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000900 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +0000901 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000902 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
903 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +0000904 assert(MRI.getType(PtrReg).isPointer() &&
905 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000906#endif
907
908 const unsigned ValReg = I.getOperand(0).getReg();
909 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
910
911 const unsigned NewOpc =
912 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits());
913 if (NewOpc == I.getOpcode())
914 return false;
915
916 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000917
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000918 uint64_t Offset = 0;
919 auto *PtrMI = MRI.getVRegDef(PtrReg);
920
921 // Try to fold a GEP into our unsigned immediate addressing mode.
922 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
923 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
924 int64_t Imm = *COff;
925 const unsigned Size = MemTy.getSizeInBits() / 8;
926 const unsigned Scale = Log2_32(Size);
927 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
928 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
929 I.getOperand(1).setReg(Ptr2Reg);
930 PtrMI = MRI.getVRegDef(Ptr2Reg);
931 Offset = Imm / Size;
932 }
933 }
934 }
935
Ahmed Bougachaf75782f2017-03-27 17:31:56 +0000936 // If we haven't folded anything into our addressing mode yet, try to fold
937 // a frame index into the base+offset.
938 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
939 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
940
Ahmed Bougacha8a654082017-03-27 17:31:52 +0000941 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +0000942
943 // If we're storing a 0, use WZR/XZR.
944 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
945 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
946 if (I.getOpcode() == AArch64::STRWui)
947 I.getOperand(0).setReg(AArch64::WZR);
948 else if (I.getOpcode() == AArch64::STRXui)
949 I.getOperand(0).setReg(AArch64::XZR);
950 }
951 }
952
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000953 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
954 }
955
Tim Northover9dd78f82017-02-08 21:22:25 +0000956 case TargetOpcode::G_SMULH:
957 case TargetOpcode::G_UMULH: {
958 // Reject the various things we don't support yet.
959 if (unsupportedBinOp(I, RBI, MRI, TRI))
960 return false;
961
962 const unsigned DefReg = I.getOperand(0).getReg();
963 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
964
965 if (RB.getID() != AArch64::GPRRegBankID) {
966 DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
967 return false;
968 }
969
970 if (Ty != LLT::scalar(64)) {
971 DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
972 << ", expected: " << LLT::scalar(64) << '\n');
973 return false;
974 }
975
976 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
977 : AArch64::UMULHrr;
978 I.setDesc(TII.get(NewOpc));
979
980 // Now that we selected an opcode, we need to constrain the register
981 // operands to use appropriate classes.
982 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
983 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000984 case TargetOpcode::G_FADD:
985 case TargetOpcode::G_FSUB:
986 case TargetOpcode::G_FMUL:
987 case TargetOpcode::G_FDIV:
988
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000989 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000990 case TargetOpcode::G_SHL:
991 case TargetOpcode::G_LSHR:
992 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +0000993 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000994 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000995 if (unsupportedBinOp(I, RBI, MRI, TRI))
996 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000997
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000998 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000999
1000 const unsigned DefReg = I.getOperand(0).getReg();
1001 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1002
1003 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1004 if (NewOpc == I.getOpcode())
1005 return false;
1006
1007 I.setDesc(TII.get(NewOpc));
1008 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001009
1010 // Now that we selected an opcode, we need to constrain the register
1011 // operands to use appropriate classes.
1012 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1013 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001014
Tim Northover398c5f52017-02-14 20:56:29 +00001015 case TargetOpcode::G_PTR_MASK: {
1016 uint64_t Align = I.getOperand(2).getImm();
1017 if (Align >= 64 || Align == 0)
1018 return false;
1019
1020 uint64_t Mask = ~((1ULL << Align) - 1);
1021 I.setDesc(TII.get(AArch64::ANDXri));
1022 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1023
1024 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1025 }
Tim Northover037af52c2016-10-31 18:31:09 +00001026 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001027 case TargetOpcode::G_TRUNC: {
1028 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1029 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1030
1031 const unsigned DstReg = I.getOperand(0).getReg();
1032 const unsigned SrcReg = I.getOperand(1).getReg();
1033
1034 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1035 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1036
1037 if (DstRB.getID() != SrcRB.getID()) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001038 DEBUG(dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001039 return false;
1040 }
1041
1042 if (DstRB.getID() == AArch64::GPRRegBankID) {
1043 const TargetRegisterClass *DstRC =
1044 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1045 if (!DstRC)
1046 return false;
1047
1048 const TargetRegisterClass *SrcRC =
1049 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1050 if (!SrcRC)
1051 return false;
1052
1053 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1054 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001055 DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001056 return false;
1057 }
1058
1059 if (DstRC == SrcRC) {
1060 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001061 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1062 SrcTy == LLT::scalar(64)) {
1063 llvm_unreachable("TableGen can import this case");
1064 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001065 } else if (DstRC == &AArch64::GPR32RegClass &&
1066 SrcRC == &AArch64::GPR64RegClass) {
1067 I.getOperand(1).setSubReg(AArch64::sub_32);
1068 } else {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001069 DEBUG(dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001070 return false;
1071 }
1072
1073 I.setDesc(TII.get(TargetOpcode::COPY));
1074 return true;
1075 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1076 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1077 I.setDesc(TII.get(AArch64::XTNv4i16));
1078 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1079 return true;
1080 }
1081 }
1082
1083 return false;
1084 }
1085
Tim Northover3d38b3a2016-10-11 20:50:21 +00001086 case TargetOpcode::G_ANYEXT: {
1087 const unsigned DstReg = I.getOperand(0).getReg();
1088 const unsigned SrcReg = I.getOperand(1).getReg();
1089
Quentin Colombetcb629a82016-10-12 03:57:49 +00001090 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1091 if (RBDst.getID() != AArch64::GPRRegBankID) {
1092 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n");
1093 return false;
1094 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001095
Quentin Colombetcb629a82016-10-12 03:57:49 +00001096 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1097 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1098 DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001099 return false;
1100 }
1101
1102 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1103
1104 if (DstSize == 0) {
1105 DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1106 return false;
1107 }
1108
Quentin Colombetcb629a82016-10-12 03:57:49 +00001109 if (DstSize != 64 && DstSize > 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001110 DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1111 << ", expected: 32 or 64\n");
1112 return false;
1113 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001114 // At this point G_ANYEXT is just like a plain COPY, but we need
1115 // to explicitly form the 64-bit value if any.
1116 if (DstSize > 32) {
1117 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1118 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1119 .addDef(ExtSrc)
1120 .addImm(0)
1121 .addUse(SrcReg)
1122 .addImm(AArch64::sub_32);
1123 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001124 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001125 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001126 }
1127
1128 case TargetOpcode::G_ZEXT:
1129 case TargetOpcode::G_SEXT: {
1130 unsigned Opcode = I.getOpcode();
1131 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1132 SrcTy = MRI.getType(I.getOperand(1).getReg());
1133 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1134 const unsigned DefReg = I.getOperand(0).getReg();
1135 const unsigned SrcReg = I.getOperand(1).getReg();
1136 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1137
1138 if (RB.getID() != AArch64::GPRRegBankID) {
1139 DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1140 << ", expected: GPR\n");
1141 return false;
1142 }
1143
1144 MachineInstr *ExtI;
1145 if (DstTy == LLT::scalar(64)) {
1146 // FIXME: Can we avoid manually doing this?
1147 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1148 DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1149 << " operand\n");
1150 return false;
1151 }
1152
1153 const unsigned SrcXReg =
1154 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1155 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1156 .addDef(SrcXReg)
1157 .addImm(0)
1158 .addUse(SrcReg)
1159 .addImm(AArch64::sub_32);
1160
1161 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1162 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1163 .addDef(DefReg)
1164 .addUse(SrcXReg)
1165 .addImm(0)
1166 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001167 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001168 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1169 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1170 .addDef(DefReg)
1171 .addUse(SrcReg)
1172 .addImm(0)
1173 .addImm(SrcTy.getSizeInBits() - 1);
1174 } else {
1175 return false;
1176 }
1177
1178 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1179
1180 I.eraseFromParent();
1181 return true;
1182 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001183
Tim Northover69271c62016-10-12 22:49:11 +00001184 case TargetOpcode::G_SITOFP:
1185 case TargetOpcode::G_UITOFP:
1186 case TargetOpcode::G_FPTOSI:
1187 case TargetOpcode::G_FPTOUI: {
1188 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1189 SrcTy = MRI.getType(I.getOperand(1).getReg());
1190 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1191 if (NewOpc == Opcode)
1192 return false;
1193
1194 I.setDesc(TII.get(NewOpc));
1195 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1196
1197 return true;
1198 }
1199
1200
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001201 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001202 // The importer is currently unable to import pointer types since they
1203 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001204 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001205
Daniel Sandersedd07842017-08-17 09:26:14 +00001206 case TargetOpcode::G_BITCAST:
1207 // Imported SelectionDAG rules can handle every bitcast except those that
1208 // bitcast from a type to the same type. Ideally, these shouldn't occur
1209 // but we might not run an optimizer that deletes them.
1210 if (MRI.getType(I.getOperand(0).getReg()) ==
1211 MRI.getType(I.getOperand(1).getReg()))
1212 return selectCopy(I, TII, MRI, TRI, RBI);
1213 return false;
1214
Tim Northover9ac0eba2016-11-08 00:45:29 +00001215 case TargetOpcode::G_SELECT: {
1216 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1217 DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1218 << ", expected: " << LLT::scalar(1) << '\n');
1219 return false;
1220 }
1221
1222 const unsigned CondReg = I.getOperand(1).getReg();
1223 const unsigned TReg = I.getOperand(2).getReg();
1224 const unsigned FReg = I.getOperand(3).getReg();
1225
1226 unsigned CSelOpc = 0;
1227
1228 if (Ty == LLT::scalar(32)) {
1229 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001230 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001231 CSelOpc = AArch64::CSELXr;
1232 } else {
1233 return false;
1234 }
1235
1236 MachineInstr &TstMI =
1237 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1238 .addDef(AArch64::WZR)
1239 .addUse(CondReg)
1240 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1241
1242 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1243 .addDef(I.getOperand(0).getReg())
1244 .addUse(TReg)
1245 .addUse(FReg)
1246 .addImm(AArch64CC::NE);
1247
1248 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1249 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1250
1251 I.eraseFromParent();
1252 return true;
1253 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001254 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001255 if (Ty != LLT::scalar(32)) {
Tim Northover6c02ad52016-10-12 22:49:04 +00001256 DEBUG(dbgs() << "G_ICMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001257 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001258 return false;
1259 }
1260
1261 unsigned CmpOpc = 0;
1262 unsigned ZReg = 0;
1263
1264 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1265 if (CmpTy == LLT::scalar(32)) {
1266 CmpOpc = AArch64::SUBSWrr;
1267 ZReg = AArch64::WZR;
1268 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1269 CmpOpc = AArch64::SUBSXrr;
1270 ZReg = AArch64::XZR;
1271 } else {
1272 return false;
1273 }
1274
Kristof Beyls22524402017-01-05 10:16:08 +00001275 // CSINC increments the result by one when the condition code is false.
1276 // Therefore, we have to invert the predicate to get an increment by 1 when
1277 // the predicate is true.
1278 const AArch64CC::CondCode invCC =
1279 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1280 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001281
1282 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1283 .addDef(ZReg)
1284 .addUse(I.getOperand(2).getReg())
1285 .addUse(I.getOperand(3).getReg());
1286
1287 MachineInstr &CSetMI =
1288 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1289 .addDef(I.getOperand(0).getReg())
1290 .addUse(AArch64::WZR)
1291 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001292 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001293
1294 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1295 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1296
1297 I.eraseFromParent();
1298 return true;
1299 }
1300
Tim Northover7dd378d2016-10-12 22:49:07 +00001301 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001302 if (Ty != LLT::scalar(32)) {
Tim Northover7dd378d2016-10-12 22:49:07 +00001303 DEBUG(dbgs() << "G_FCMP result has type: " << Ty
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001304 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001305 return false;
1306 }
1307
1308 unsigned CmpOpc = 0;
1309 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1310 if (CmpTy == LLT::scalar(32)) {
1311 CmpOpc = AArch64::FCMPSrr;
1312 } else if (CmpTy == LLT::scalar(64)) {
1313 CmpOpc = AArch64::FCMPDrr;
1314 } else {
1315 return false;
1316 }
1317
1318 // FIXME: regbank
1319
1320 AArch64CC::CondCode CC1, CC2;
1321 changeFCMPPredToAArch64CC(
1322 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1323
1324 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1325 .addUse(I.getOperand(2).getReg())
1326 .addUse(I.getOperand(3).getReg());
1327
1328 const unsigned DefReg = I.getOperand(0).getReg();
1329 unsigned Def1Reg = DefReg;
1330 if (CC2 != AArch64CC::AL)
1331 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1332
1333 MachineInstr &CSetMI =
1334 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1335 .addDef(Def1Reg)
1336 .addUse(AArch64::WZR)
1337 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001338 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001339
1340 if (CC2 != AArch64CC::AL) {
1341 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1342 MachineInstr &CSet2MI =
1343 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1344 .addDef(Def2Reg)
1345 .addUse(AArch64::WZR)
1346 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001347 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001348 MachineInstr &OrMI =
1349 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1350 .addDef(DefReg)
1351 .addUse(Def1Reg)
1352 .addUse(Def2Reg);
1353 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1354 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1355 }
1356
1357 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1358 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1359
1360 I.eraseFromParent();
1361 return true;
1362 }
Tim Northovere9600d82017-02-08 17:57:27 +00001363 case TargetOpcode::G_VASTART:
1364 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1365 : selectVaStartAAPCS(I, MF, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001366 case TargetOpcode::G_IMPLICIT_DEF:
1367 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1368 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001369 }
1370
1371 return false;
1372}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001373
1374/// SelectArithImmed - Select an immediate value that can be represented as
1375/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1376/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001377InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001378AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001379 MachineInstr &MI = *Root.getParent();
1380 MachineBasicBlock &MBB = *MI.getParent();
1381 MachineFunction &MF = *MBB.getParent();
1382 MachineRegisterInfo &MRI = MF.getRegInfo();
1383
1384 // This function is called from the addsub_shifted_imm ComplexPattern,
1385 // which lists [imm] as the list of opcode it's interested in, however
1386 // we still need to check whether the operand is actually an immediate
1387 // here because the ComplexPattern opcode list is only used in
1388 // root-level opcode matching.
1389 uint64_t Immed;
1390 if (Root.isImm())
1391 Immed = Root.getImm();
1392 else if (Root.isCImm())
1393 Immed = Root.getCImm()->getZExtValue();
1394 else if (Root.isReg()) {
1395 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1396 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001397 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001398 MachineOperand &Op1 = Def->getOperand(1);
1399 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001400 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001401 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001402 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001403 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001404
1405 unsigned ShiftAmt;
1406
1407 if (Immed >> 12 == 0) {
1408 ShiftAmt = 0;
1409 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1410 ShiftAmt = 12;
1411 Immed = Immed >> 12;
1412 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001413 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001414
1415 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001416 return {{
1417 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1418 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1419 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001420}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001421
Daniel Sandersea8711b2017-10-16 03:36:29 +00001422/// Select a "register plus unscaled signed 9-bit immediate" address. This
1423/// should only match when there is an offset that is not valid for a scaled
1424/// immediate addressing mode. The "Size" argument is the size in bytes of the
1425/// memory reference, which is needed here to know what is valid for a scaled
1426/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001427InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001428AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1429 unsigned Size) const {
1430 MachineRegisterInfo &MRI =
1431 Root.getParent()->getParent()->getParent()->getRegInfo();
1432
1433 if (!Root.isReg())
1434 return None;
1435
1436 if (!isBaseWithConstantOffset(Root, MRI))
1437 return None;
1438
1439 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1440 if (!RootDef)
1441 return None;
1442
1443 MachineOperand &OffImm = RootDef->getOperand(2);
1444 if (!OffImm.isReg())
1445 return None;
1446 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1447 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1448 return None;
1449 int64_t RHSC;
1450 MachineOperand &RHSOp1 = RHS->getOperand(1);
1451 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1452 return None;
1453 RHSC = RHSOp1.getCImm()->getSExtValue();
1454
1455 // If the offset is valid as a scaled immediate, don't match here.
1456 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1457 return None;
1458 if (RHSC >= -256 && RHSC < 256) {
1459 MachineOperand &Base = RootDef->getOperand(1);
1460 return {{
1461 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1462 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1463 }};
1464 }
1465 return None;
1466}
1467
1468/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1469/// "Size" argument is the size in bytes of the memory reference, which
1470/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001471InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001472AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1473 unsigned Size) const {
1474 MachineRegisterInfo &MRI =
1475 Root.getParent()->getParent()->getParent()->getRegInfo();
1476
1477 if (!Root.isReg())
1478 return None;
1479
1480 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1481 if (!RootDef)
1482 return None;
1483
1484 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1485 return {{
1486 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1487 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1488 }};
1489 }
1490
1491 if (isBaseWithConstantOffset(Root, MRI)) {
1492 MachineOperand &LHS = RootDef->getOperand(1);
1493 MachineOperand &RHS = RootDef->getOperand(2);
1494 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1495 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1496 if (LHSDef && RHSDef) {
1497 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1498 unsigned Scale = Log2_32(Size);
1499 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1500 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001501 return {{
1502 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1503 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1504 }};
1505
Daniel Sandersea8711b2017-10-16 03:36:29 +00001506 return {{
1507 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1508 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1509 }};
1510 }
1511 }
1512 }
1513
1514 // Before falling back to our general case, check if the unscaled
1515 // instructions can handle this. If so, that's preferable.
1516 if (selectAddrModeUnscaled(Root, Size).hasValue())
1517 return None;
1518
1519 return {{
1520 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1521 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1522 }};
1523}
1524
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001525namespace llvm {
1526InstructionSelector *
1527createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1528 AArch64Subtarget &Subtarget,
1529 AArch64RegisterBankInfo &RBI) {
1530 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1531}
1532}