blob: 4479ce889f7e6173292d8b6f6b9bc0815e66e74d [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements the targeting of the InstructionSelector class for
10/// AArch64.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000014#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000015#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000016#include "AArch64RegisterBankInfo.h"
17#include "AArch64RegisterInfo.h"
18#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000019#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000020#include "MCTargetDesc/AArch64AddressingModes.h"
Amara Emerson2ff22982019-03-14 22:48:15 +000021#include "llvm/ADT/Optional.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"
Amara Emerson1e8c1642018-07-31 00:09:02 +000024#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Aditya Nandakumar75ad9cc2017-04-19 20:48:50 +000025#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000026#include "llvm/CodeGen/MachineBasicBlock.h"
Amara Emerson1abe05c2019-02-21 20:20:16 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000031#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/IR/Type.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
36
37#define DEBUG_TYPE "aarch64-isel"
38
39using namespace llvm;
40
Daniel Sanders0b5293f2017-04-06 09:49:34 +000041namespace {
42
Daniel Sanderse7b0d662017-04-21 15:59:56 +000043#define GET_GLOBALISEL_PREDICATE_BITSET
44#include "AArch64GenGlobalISel.inc"
45#undef GET_GLOBALISEL_PREDICATE_BITSET
46
Daniel Sanders0b5293f2017-04-06 09:49:34 +000047class AArch64InstructionSelector : public InstructionSelector {
48public:
49 AArch64InstructionSelector(const AArch64TargetMachine &TM,
50 const AArch64Subtarget &STI,
51 const AArch64RegisterBankInfo &RBI);
52
Daniel Sandersf76f3152017-11-16 00:46:35 +000053 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
David Blaikie62651302017-10-26 23:39:54 +000054 static const char *getName() { return DEBUG_TYPE; }
Daniel Sanders0b5293f2017-04-06 09:49:34 +000055
56private:
57 /// tblgen-erated 'select' implementation, used as the initial selector for
58 /// the patterns that don't require complex C++.
Daniel Sandersf76f3152017-11-16 00:46:35 +000059 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000060
61 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
62 MachineRegisterInfo &MRI) const;
63 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
64 MachineRegisterInfo &MRI) const;
65
66 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
67 MachineRegisterInfo &MRI) const;
68
Amara Emerson5ec14602018-12-10 18:44:58 +000069 // Helper to generate an equivalent of scalar_to_vector into a new register,
70 // returned via 'Dst'.
Amara Emerson8acb0d92019-03-04 19:16:00 +000071 MachineInstr *emitScalarToVector(unsigned EltSize,
Amara Emerson6bcfa1c2019-02-25 18:52:54 +000072 const TargetRegisterClass *DstRC,
73 unsigned Scalar,
74 MachineIRBuilder &MIRBuilder) const;
Jessica Paquette16d67a32019-03-13 23:22:23 +000075
76 /// Emit a lane insert into \p DstReg, or a new vector register if None is
77 /// provided.
78 ///
79 /// The lane inserted into is defined by \p LaneIdx. The vector source
80 /// register is given by \p SrcReg. The register containing the element is
81 /// given by \p EltReg.
82 MachineInstr *emitLaneInsert(Optional<unsigned> DstReg, unsigned SrcReg,
83 unsigned EltReg, unsigned LaneIdx,
84 const RegisterBank &RB,
85 MachineIRBuilder &MIRBuilder) const;
Jessica Paquette5aff1f42019-03-14 18:01:30 +000086 bool selectInsertElt(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emerson5ec14602018-12-10 18:44:58 +000087 bool selectBuildVector(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emerson8cb186c2018-12-20 01:11:04 +000088 bool selectMergeValues(MachineInstr &I, MachineRegisterInfo &MRI) const;
Jessica Paquette245047d2019-01-24 22:00:41 +000089 bool selectUnmergeValues(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emerson5ec14602018-12-10 18:44:58 +000090
Amara Emerson1abe05c2019-02-21 20:20:16 +000091 void collectShuffleMaskIndices(MachineInstr &I, MachineRegisterInfo &MRI,
92 SmallVectorImpl<int> &Idxs) const;
93 bool selectShuffleVector(MachineInstr &I, MachineRegisterInfo &MRI) const;
Jessica Paquette607774c2019-03-11 22:18:01 +000094 bool selectExtractElt(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emerson2ff22982019-03-14 22:48:15 +000095 bool selectConcatVectors(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emersond61b89b2019-03-14 22:48:18 +000096 bool selectSplitVectorUnmerge(MachineInstr &I,
97 MachineRegisterInfo &MRI) const;
Amara Emerson1abe05c2019-02-21 20:20:16 +000098
99 unsigned emitConstantPoolEntry(Constant *CPVal, MachineFunction &MF) const;
100 MachineInstr *emitLoadFromConstantPool(Constant *CPVal,
101 MachineIRBuilder &MIRBuilder) const;
Amara Emerson2ff22982019-03-14 22:48:15 +0000102
103 // Emit a vector concat operation.
104 MachineInstr *emitVectorConcat(Optional<unsigned> Dst, unsigned Op1,
105 unsigned Op2,
Amara Emerson8acb0d92019-03-04 19:16:00 +0000106 MachineIRBuilder &MIRBuilder) const;
Amara Emersond61b89b2019-03-14 22:48:18 +0000107 MachineInstr *emitExtractVectorElt(Optional<unsigned> DstReg,
108 const RegisterBank &DstRB, LLT ScalarTy,
109 unsigned VecReg, unsigned LaneIdx,
110 MachineIRBuilder &MIRBuilder) const;
Amara Emerson1abe05c2019-02-21 20:20:16 +0000111
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000112 ComplexRendererFns selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000113
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000114 ComplexRendererFns selectAddrModeUnscaled(MachineOperand &Root,
115 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +0000116
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000117 ComplexRendererFns selectAddrModeUnscaled8(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000118 return selectAddrModeUnscaled(Root, 1);
119 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000120 ComplexRendererFns selectAddrModeUnscaled16(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000121 return selectAddrModeUnscaled(Root, 2);
122 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000123 ComplexRendererFns selectAddrModeUnscaled32(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000124 return selectAddrModeUnscaled(Root, 4);
125 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000126 ComplexRendererFns selectAddrModeUnscaled64(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000127 return selectAddrModeUnscaled(Root, 8);
128 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000129 ComplexRendererFns selectAddrModeUnscaled128(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000130 return selectAddrModeUnscaled(Root, 16);
131 }
132
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000133 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root,
134 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +0000135 template <int Width>
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000136 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000137 return selectAddrModeIndexed(Root, Width / 8);
138 }
139
Volkan Kelesf7f25682018-01-16 18:44:05 +0000140 void renderTruncImm(MachineInstrBuilder &MIB, const MachineInstr &MI) const;
141
Amara Emerson1e8c1642018-07-31 00:09:02 +0000142 // Materialize a GlobalValue or BlockAddress using a movz+movk sequence.
143 void materializeLargeCMVal(MachineInstr &I, const Value *V,
144 unsigned char OpFlags) const;
145
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000146 const AArch64TargetMachine &TM;
147 const AArch64Subtarget &STI;
148 const AArch64InstrInfo &TII;
149 const AArch64RegisterInfo &TRI;
150 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000151
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000152#define GET_GLOBALISEL_PREDICATES_DECL
153#include "AArch64GenGlobalISel.inc"
154#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000155
156// We declare the temporaries used by selectImpl() in the class to minimize the
157// cost of constructing placeholder values.
158#define GET_GLOBALISEL_TEMPORARIES_DECL
159#include "AArch64GenGlobalISel.inc"
160#undef GET_GLOBALISEL_TEMPORARIES_DECL
161};
162
163} // end anonymous namespace
164
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000165#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000166#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000167#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000168
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000169AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +0000170 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
171 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000172 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000173 TRI(*STI.getRegisterInfo()), RBI(RBI),
174#define GET_GLOBALISEL_PREDICATES_INIT
175#include "AArch64GenGlobalISel.inc"
176#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000177#define GET_GLOBALISEL_TEMPORARIES_INIT
178#include "AArch64GenGlobalISel.inc"
179#undef GET_GLOBALISEL_TEMPORARIES_INIT
180{
181}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000182
Tim Northoverfb8d9892016-10-12 22:49:15 +0000183// FIXME: This should be target-independent, inferred from the types declared
184// for each class in the bank.
185static const TargetRegisterClass *
186getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
Amara Emerson3838ed02018-02-02 18:03:30 +0000187 const RegisterBankInfo &RBI,
188 bool GetAllRegSet = false) {
Tim Northoverfb8d9892016-10-12 22:49:15 +0000189 if (RB.getID() == AArch64::GPRRegBankID) {
190 if (Ty.getSizeInBits() <= 32)
Amara Emerson3838ed02018-02-02 18:03:30 +0000191 return GetAllRegSet ? &AArch64::GPR32allRegClass
192 : &AArch64::GPR32RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000193 if (Ty.getSizeInBits() == 64)
Amara Emerson3838ed02018-02-02 18:03:30 +0000194 return GetAllRegSet ? &AArch64::GPR64allRegClass
195 : &AArch64::GPR64RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000196 return nullptr;
197 }
198
199 if (RB.getID() == AArch64::FPRRegBankID) {
Amara Emerson3838ed02018-02-02 18:03:30 +0000200 if (Ty.getSizeInBits() <= 16)
201 return &AArch64::FPR16RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000202 if (Ty.getSizeInBits() == 32)
203 return &AArch64::FPR32RegClass;
204 if (Ty.getSizeInBits() == 64)
205 return &AArch64::FPR64RegClass;
206 if (Ty.getSizeInBits() == 128)
207 return &AArch64::FPR128RegClass;
208 return nullptr;
209 }
210
211 return nullptr;
212}
213
Jessica Paquette245047d2019-01-24 22:00:41 +0000214/// Given a register bank, and size in bits, return the smallest register class
215/// that can represent that combination.
Benjamin Kramer711950c2019-02-11 15:16:21 +0000216static const TargetRegisterClass *
217getMinClassForRegBank(const RegisterBank &RB, unsigned SizeInBits,
218 bool GetAllRegSet = false) {
Jessica Paquette245047d2019-01-24 22:00:41 +0000219 unsigned RegBankID = RB.getID();
220
221 if (RegBankID == AArch64::GPRRegBankID) {
222 if (SizeInBits <= 32)
223 return GetAllRegSet ? &AArch64::GPR32allRegClass
224 : &AArch64::GPR32RegClass;
225 if (SizeInBits == 64)
226 return GetAllRegSet ? &AArch64::GPR64allRegClass
227 : &AArch64::GPR64RegClass;
228 }
229
230 if (RegBankID == AArch64::FPRRegBankID) {
231 switch (SizeInBits) {
232 default:
233 return nullptr;
234 case 8:
235 return &AArch64::FPR8RegClass;
236 case 16:
237 return &AArch64::FPR16RegClass;
238 case 32:
239 return &AArch64::FPR32RegClass;
240 case 64:
241 return &AArch64::FPR64RegClass;
242 case 128:
243 return &AArch64::FPR128RegClass;
244 }
245 }
246
247 return nullptr;
248}
249
250/// Returns the correct subregister to use for a given register class.
251static bool getSubRegForClass(const TargetRegisterClass *RC,
252 const TargetRegisterInfo &TRI, unsigned &SubReg) {
253 switch (TRI.getRegSizeInBits(*RC)) {
254 case 8:
255 SubReg = AArch64::bsub;
256 break;
257 case 16:
258 SubReg = AArch64::hsub;
259 break;
260 case 32:
261 if (RC == &AArch64::GPR32RegClass)
262 SubReg = AArch64::sub_32;
263 else
264 SubReg = AArch64::ssub;
265 break;
266 case 64:
267 SubReg = AArch64::dsub;
268 break;
269 default:
270 LLVM_DEBUG(
271 dbgs() << "Couldn't find appropriate subregister for register class.");
272 return false;
273 }
274
275 return true;
276}
277
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000278/// Check whether \p I is a currently unsupported binary operation:
279/// - it has an unsized type
280/// - an operand is not a vreg
281/// - all operands are not in the same bank
282/// These are checks that should someday live in the verifier, but right now,
283/// these are mostly limitations of the aarch64 selector.
284static bool unsupportedBinOp(const MachineInstr &I,
285 const AArch64RegisterBankInfo &RBI,
286 const MachineRegisterInfo &MRI,
287 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000288 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000289 if (!Ty.isValid()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000290 LLVM_DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000291 return true;
292 }
293
294 const RegisterBank *PrevOpBank = nullptr;
295 for (auto &MO : I.operands()) {
296 // FIXME: Support non-register operands.
297 if (!MO.isReg()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000298 LLVM_DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000299 return true;
300 }
301
302 // FIXME: Can generic operations have physical registers operands? If
303 // so, this will need to be taught about that, and we'll need to get the
304 // bank out of the minimal class for the register.
305 // Either way, this needs to be documented (and possibly verified).
306 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000307 LLVM_DEBUG(dbgs() << "Generic inst has physical register operand\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000308 return true;
309 }
310
311 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
312 if (!OpBank) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000313 LLVM_DEBUG(dbgs() << "Generic register has no bank or class\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000314 return true;
315 }
316
317 if (PrevOpBank && OpBank != PrevOpBank) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000318 LLVM_DEBUG(dbgs() << "Generic inst operands have different banks\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000319 return true;
320 }
321 PrevOpBank = OpBank;
322 }
323 return false;
324}
325
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000326/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000327/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000328/// and of size \p OpSize.
329/// \returns \p GenericOpc if the combination is unsupported.
330static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
331 unsigned OpSize) {
332 switch (RegBankID) {
333 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000334 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000335 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000336 case TargetOpcode::G_SHL:
337 return AArch64::LSLVWr;
338 case TargetOpcode::G_LSHR:
339 return AArch64::LSRVWr;
340 case TargetOpcode::G_ASHR:
341 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000342 default:
343 return GenericOpc;
344 }
Tim Northover55782222016-10-18 20:03:48 +0000345 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000346 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000347 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000348 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000349 case TargetOpcode::G_SHL:
350 return AArch64::LSLVXr;
351 case TargetOpcode::G_LSHR:
352 return AArch64::LSRVXr;
353 case TargetOpcode::G_ASHR:
354 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000355 default:
356 return GenericOpc;
357 }
358 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000359 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000360 case AArch64::FPRRegBankID:
361 switch (OpSize) {
362 case 32:
363 switch (GenericOpc) {
364 case TargetOpcode::G_FADD:
365 return AArch64::FADDSrr;
366 case TargetOpcode::G_FSUB:
367 return AArch64::FSUBSrr;
368 case TargetOpcode::G_FMUL:
369 return AArch64::FMULSrr;
370 case TargetOpcode::G_FDIV:
371 return AArch64::FDIVSrr;
372 default:
373 return GenericOpc;
374 }
375 case 64:
376 switch (GenericOpc) {
377 case TargetOpcode::G_FADD:
378 return AArch64::FADDDrr;
379 case TargetOpcode::G_FSUB:
380 return AArch64::FSUBDrr;
381 case TargetOpcode::G_FMUL:
382 return AArch64::FMULDrr;
383 case TargetOpcode::G_FDIV:
384 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000385 case TargetOpcode::G_OR:
386 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000387 default:
388 return GenericOpc;
389 }
390 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000391 break;
392 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000393 return GenericOpc;
394}
395
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000396/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
397/// appropriate for the (value) register bank \p RegBankID and of memory access
398/// size \p OpSize. This returns the variant with the base+unsigned-immediate
399/// addressing mode (e.g., LDRXui).
400/// \returns \p GenericOpc if the combination is unsupported.
401static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
402 unsigned OpSize) {
403 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
404 switch (RegBankID) {
405 case AArch64::GPRRegBankID:
406 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000407 case 8:
408 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
409 case 16:
410 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000411 case 32:
412 return isStore ? AArch64::STRWui : AArch64::LDRWui;
413 case 64:
414 return isStore ? AArch64::STRXui : AArch64::LDRXui;
415 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000416 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000417 case AArch64::FPRRegBankID:
418 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000419 case 8:
420 return isStore ? AArch64::STRBui : AArch64::LDRBui;
421 case 16:
422 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000423 case 32:
424 return isStore ? AArch64::STRSui : AArch64::LDRSui;
425 case 64:
426 return isStore ? AArch64::STRDui : AArch64::LDRDui;
427 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000428 break;
429 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000430 return GenericOpc;
431}
432
Benjamin Kramer1411ecf2019-01-24 23:39:47 +0000433#ifndef NDEBUG
Jessica Paquette245047d2019-01-24 22:00:41 +0000434/// Helper function that verifies that we have a valid copy at the end of
435/// selectCopy. Verifies that the source and dest have the expected sizes and
436/// then returns true.
437static bool isValidCopy(const MachineInstr &I, const RegisterBank &DstBank,
438 const MachineRegisterInfo &MRI,
439 const TargetRegisterInfo &TRI,
440 const RegisterBankInfo &RBI) {
441 const unsigned DstReg = I.getOperand(0).getReg();
442 const unsigned SrcReg = I.getOperand(1).getReg();
443 const unsigned DstSize = RBI.getSizeInBits(DstReg, MRI, TRI);
444 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
Amara Emersondb211892018-02-20 05:11:57 +0000445
Jessica Paquette245047d2019-01-24 22:00:41 +0000446 // Make sure the size of the source and dest line up.
447 assert(
448 (DstSize == SrcSize ||
449 // Copies are a mean to setup initial types, the number of
450 // bits may not exactly match.
451 (TargetRegisterInfo::isPhysicalRegister(SrcReg) && DstSize <= SrcSize) ||
452 // Copies are a mean to copy bits around, as long as we are
453 // on the same register class, that's fine. Otherwise, that
454 // means we need some SUBREG_TO_REG or AND & co.
455 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
456 "Copy with different width?!");
457
458 // Check the size of the destination.
459 assert((DstSize <= 64 || DstBank.getID() == AArch64::FPRRegBankID) &&
460 "GPRs cannot get more than 64-bit width values");
461
462 return true;
463}
Benjamin Kramer1411ecf2019-01-24 23:39:47 +0000464#endif
Jessica Paquette245047d2019-01-24 22:00:41 +0000465
466/// Helper function for selectCopy. Inserts a subregister copy from
467/// \p *From to \p *To, linking it up to \p I.
468///
469/// e.g, given I = "Dst = COPY SrcReg", we'll transform that into
470///
471/// CopyReg (From class) = COPY SrcReg
472/// SubRegCopy (To class) = COPY CopyReg:SubReg
473/// Dst = COPY SubRegCopy
474static bool selectSubregisterCopy(MachineInstr &I, const TargetInstrInfo &TII,
475 MachineRegisterInfo &MRI,
476 const RegisterBankInfo &RBI, unsigned SrcReg,
477 const TargetRegisterClass *From,
478 const TargetRegisterClass *To,
479 unsigned SubReg) {
480 unsigned CopyReg = MRI.createVirtualRegister(From);
481 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::COPY), CopyReg)
482 .addUse(SrcReg);
483 unsigned SubRegCopy = MRI.createVirtualRegister(To);
484 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
485 SubRegCopy)
486 .addUse(CopyReg, 0, SubReg);
Amara Emersondb211892018-02-20 05:11:57 +0000487 MachineOperand &RegOp = I.getOperand(1);
488 RegOp.setReg(SubRegCopy);
Jessica Paquette245047d2019-01-24 22:00:41 +0000489
490 // It's possible that the destination register won't be constrained. Make
491 // sure that happens.
492 if (!TargetRegisterInfo::isPhysicalRegister(I.getOperand(0).getReg()))
493 RBI.constrainGenericRegister(I.getOperand(0).getReg(), *To, MRI);
494
Amara Emersondb211892018-02-20 05:11:57 +0000495 return true;
496}
497
Quentin Colombetcb629a82016-10-12 03:57:49 +0000498static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
499 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
500 const RegisterBankInfo &RBI) {
501
502 unsigned DstReg = I.getOperand(0).getReg();
Amara Emersondb211892018-02-20 05:11:57 +0000503 unsigned SrcReg = I.getOperand(1).getReg();
Jessica Paquette245047d2019-01-24 22:00:41 +0000504 const RegisterBank &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
505 const RegisterBank &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
506 const TargetRegisterClass *DstRC = getMinClassForRegBank(
507 DstRegBank, RBI.getSizeInBits(DstReg, MRI, TRI), true);
508 if (!DstRC) {
509 LLVM_DEBUG(dbgs() << "Unexpected dest size "
510 << RBI.getSizeInBits(DstReg, MRI, TRI) << '\n');
Amara Emerson3838ed02018-02-02 18:03:30 +0000511 return false;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000512 }
513
Jessica Paquette245047d2019-01-24 22:00:41 +0000514 // A couple helpers below, for making sure that the copy we produce is valid.
515
516 // Set to true if we insert a SUBREG_TO_REG. If we do this, then we don't want
517 // to verify that the src and dst are the same size, since that's handled by
518 // the SUBREG_TO_REG.
519 bool KnownValid = false;
520
521 // Returns true, or asserts if something we don't expect happens. Instead of
522 // returning true, we return isValidCopy() to ensure that we verify the
523 // result.
Jessica Paquette76c40f82019-01-24 22:51:31 +0000524 auto CheckCopy = [&]() {
Jessica Paquette245047d2019-01-24 22:00:41 +0000525 // If we have a bitcast or something, we can't have physical registers.
526 assert(
Simon Pilgrimdea61742019-01-25 11:38:40 +0000527 (I.isCopy() ||
528 (!TargetRegisterInfo::isPhysicalRegister(I.getOperand(0).getReg()) &&
529 !TargetRegisterInfo::isPhysicalRegister(I.getOperand(1).getReg()))) &&
530 "No phys reg on generic operator!");
Jessica Paquette245047d2019-01-24 22:00:41 +0000531 assert(KnownValid || isValidCopy(I, DstRegBank, MRI, TRI, RBI));
Jonas Hahnfeld65a401f2019-03-04 08:51:32 +0000532 (void)KnownValid;
Jessica Paquette245047d2019-01-24 22:00:41 +0000533 return true;
534 };
535
536 // Is this a copy? If so, then we may need to insert a subregister copy, or
537 // a SUBREG_TO_REG.
538 if (I.isCopy()) {
539 // Yes. Check if there's anything to fix up.
540 const TargetRegisterClass *SrcRC = getMinClassForRegBank(
541 SrcRegBank, RBI.getSizeInBits(SrcReg, MRI, TRI), true);
Amara Emerson7e9f3482018-02-18 17:10:49 +0000542 if (!SrcRC) {
Jessica Paquette245047d2019-01-24 22:00:41 +0000543 LLVM_DEBUG(dbgs() << "Couldn't determine source register class\n");
544 return false;
Amara Emerson7e9f3482018-02-18 17:10:49 +0000545 }
Jessica Paquette245047d2019-01-24 22:00:41 +0000546
547 // Is this a cross-bank copy?
548 if (DstRegBank.getID() != SrcRegBank.getID()) {
549 // If we're doing a cross-bank copy on different-sized registers, we need
550 // to do a bit more work.
551 unsigned SrcSize = TRI.getRegSizeInBits(*SrcRC);
552 unsigned DstSize = TRI.getRegSizeInBits(*DstRC);
553
554 if (SrcSize > DstSize) {
555 // We're doing a cross-bank copy into a smaller register. We need a
556 // subregister copy. First, get a register class that's on the same bank
557 // as the destination, but the same size as the source.
558 const TargetRegisterClass *SubregRC =
559 getMinClassForRegBank(DstRegBank, SrcSize, true);
560 assert(SubregRC && "Didn't get a register class for subreg?");
561
562 // Get the appropriate subregister for the destination.
563 unsigned SubReg = 0;
564 if (!getSubRegForClass(DstRC, TRI, SubReg)) {
565 LLVM_DEBUG(dbgs() << "Couldn't determine subregister for copy.\n");
566 return false;
567 }
568
569 // Now, insert a subregister copy using the new register class.
570 selectSubregisterCopy(I, TII, MRI, RBI, SrcReg, SubregRC, DstRC,
571 SubReg);
572 return CheckCopy();
573 }
574
575 else if (DstRegBank.getID() == AArch64::GPRRegBankID && DstSize == 32 &&
576 SrcSize == 16) {
577 // Special case for FPR16 to GPR32.
578 // FIXME: This can probably be generalized like the above case.
579 unsigned PromoteReg =
580 MRI.createVirtualRegister(&AArch64::FPR32RegClass);
581 BuildMI(*I.getParent(), I, I.getDebugLoc(),
582 TII.get(AArch64::SUBREG_TO_REG), PromoteReg)
583 .addImm(0)
584 .addUse(SrcReg)
585 .addImm(AArch64::hsub);
586 MachineOperand &RegOp = I.getOperand(1);
587 RegOp.setReg(PromoteReg);
588
589 // Promise that the copy is implicitly validated by the SUBREG_TO_REG.
590 KnownValid = true;
591 }
Amara Emerson7e9f3482018-02-18 17:10:49 +0000592 }
Jessica Paquette245047d2019-01-24 22:00:41 +0000593
594 // If the destination is a physical register, then there's nothing to
595 // change, so we're done.
596 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
597 return CheckCopy();
Amara Emerson7e9f3482018-02-18 17:10:49 +0000598 }
599
Jessica Paquette245047d2019-01-24 22:00:41 +0000600 // No need to constrain SrcReg. It will get constrained when we hit another
601 // of its use or its defs. Copies do not have constraints.
602 if (!RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000603 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
604 << " operand\n");
Quentin Colombetcb629a82016-10-12 03:57:49 +0000605 return false;
606 }
607 I.setDesc(TII.get(AArch64::COPY));
Jessica Paquette245047d2019-01-24 22:00:41 +0000608 return CheckCopy();
Quentin Colombetcb629a82016-10-12 03:57:49 +0000609}
610
Tim Northover69271c62016-10-12 22:49:11 +0000611static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
612 if (!DstTy.isScalar() || !SrcTy.isScalar())
613 return GenericOpc;
614
615 const unsigned DstSize = DstTy.getSizeInBits();
616 const unsigned SrcSize = SrcTy.getSizeInBits();
617
618 switch (DstSize) {
619 case 32:
620 switch (SrcSize) {
621 case 32:
622 switch (GenericOpc) {
623 case TargetOpcode::G_SITOFP:
624 return AArch64::SCVTFUWSri;
625 case TargetOpcode::G_UITOFP:
626 return AArch64::UCVTFUWSri;
627 case TargetOpcode::G_FPTOSI:
628 return AArch64::FCVTZSUWSr;
629 case TargetOpcode::G_FPTOUI:
630 return AArch64::FCVTZUUWSr;
631 default:
632 return GenericOpc;
633 }
634 case 64:
635 switch (GenericOpc) {
636 case TargetOpcode::G_SITOFP:
637 return AArch64::SCVTFUXSri;
638 case TargetOpcode::G_UITOFP:
639 return AArch64::UCVTFUXSri;
640 case TargetOpcode::G_FPTOSI:
641 return AArch64::FCVTZSUWDr;
642 case TargetOpcode::G_FPTOUI:
643 return AArch64::FCVTZUUWDr;
644 default:
645 return GenericOpc;
646 }
647 default:
648 return GenericOpc;
649 }
650 case 64:
651 switch (SrcSize) {
652 case 32:
653 switch (GenericOpc) {
654 case TargetOpcode::G_SITOFP:
655 return AArch64::SCVTFUWDri;
656 case TargetOpcode::G_UITOFP:
657 return AArch64::UCVTFUWDri;
658 case TargetOpcode::G_FPTOSI:
659 return AArch64::FCVTZSUXSr;
660 case TargetOpcode::G_FPTOUI:
661 return AArch64::FCVTZUUXSr;
662 default:
663 return GenericOpc;
664 }
665 case 64:
666 switch (GenericOpc) {
667 case TargetOpcode::G_SITOFP:
668 return AArch64::SCVTFUXDri;
669 case TargetOpcode::G_UITOFP:
670 return AArch64::UCVTFUXDri;
671 case TargetOpcode::G_FPTOSI:
672 return AArch64::FCVTZSUXDr;
673 case TargetOpcode::G_FPTOUI:
674 return AArch64::FCVTZUUXDr;
675 default:
676 return GenericOpc;
677 }
678 default:
679 return GenericOpc;
680 }
681 default:
682 return GenericOpc;
683 };
684 return GenericOpc;
685}
686
Tim Northover6c02ad52016-10-12 22:49:04 +0000687static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
688 switch (P) {
689 default:
690 llvm_unreachable("Unknown condition code!");
691 case CmpInst::ICMP_NE:
692 return AArch64CC::NE;
693 case CmpInst::ICMP_EQ:
694 return AArch64CC::EQ;
695 case CmpInst::ICMP_SGT:
696 return AArch64CC::GT;
697 case CmpInst::ICMP_SGE:
698 return AArch64CC::GE;
699 case CmpInst::ICMP_SLT:
700 return AArch64CC::LT;
701 case CmpInst::ICMP_SLE:
702 return AArch64CC::LE;
703 case CmpInst::ICMP_UGT:
704 return AArch64CC::HI;
705 case CmpInst::ICMP_UGE:
706 return AArch64CC::HS;
707 case CmpInst::ICMP_ULT:
708 return AArch64CC::LO;
709 case CmpInst::ICMP_ULE:
710 return AArch64CC::LS;
711 }
712}
713
Tim Northover7dd378d2016-10-12 22:49:07 +0000714static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
715 AArch64CC::CondCode &CondCode,
716 AArch64CC::CondCode &CondCode2) {
717 CondCode2 = AArch64CC::AL;
718 switch (P) {
719 default:
720 llvm_unreachable("Unknown FP condition!");
721 case CmpInst::FCMP_OEQ:
722 CondCode = AArch64CC::EQ;
723 break;
724 case CmpInst::FCMP_OGT:
725 CondCode = AArch64CC::GT;
726 break;
727 case CmpInst::FCMP_OGE:
728 CondCode = AArch64CC::GE;
729 break;
730 case CmpInst::FCMP_OLT:
731 CondCode = AArch64CC::MI;
732 break;
733 case CmpInst::FCMP_OLE:
734 CondCode = AArch64CC::LS;
735 break;
736 case CmpInst::FCMP_ONE:
737 CondCode = AArch64CC::MI;
738 CondCode2 = AArch64CC::GT;
739 break;
740 case CmpInst::FCMP_ORD:
741 CondCode = AArch64CC::VC;
742 break;
743 case CmpInst::FCMP_UNO:
744 CondCode = AArch64CC::VS;
745 break;
746 case CmpInst::FCMP_UEQ:
747 CondCode = AArch64CC::EQ;
748 CondCode2 = AArch64CC::VS;
749 break;
750 case CmpInst::FCMP_UGT:
751 CondCode = AArch64CC::HI;
752 break;
753 case CmpInst::FCMP_UGE:
754 CondCode = AArch64CC::PL;
755 break;
756 case CmpInst::FCMP_ULT:
757 CondCode = AArch64CC::LT;
758 break;
759 case CmpInst::FCMP_ULE:
760 CondCode = AArch64CC::LE;
761 break;
762 case CmpInst::FCMP_UNE:
763 CondCode = AArch64CC::NE;
764 break;
765 }
766}
767
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000768bool AArch64InstructionSelector::selectCompareBranch(
769 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
770
771 const unsigned CondReg = I.getOperand(0).getReg();
772 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
773 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000774 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
775 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000776 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
777 return false;
778
779 unsigned LHS = CCMI->getOperand(2).getReg();
780 unsigned RHS = CCMI->getOperand(3).getReg();
781 if (!getConstantVRegVal(RHS, MRI))
782 std::swap(RHS, LHS);
783
784 const auto RHSImm = getConstantVRegVal(RHS, MRI);
785 if (!RHSImm || *RHSImm != 0)
786 return false;
787
788 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
789 if (RB.getID() != AArch64::GPRRegBankID)
790 return false;
791
792 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
793 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
794 return false;
795
796 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
797 unsigned CBOpc = 0;
798 if (CmpWidth <= 32)
799 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
800 else if (CmpWidth == 64)
801 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
802 else
803 return false;
804
Aditya Nandakumar18b3f9d2018-01-17 19:31:33 +0000805 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
806 .addUse(LHS)
807 .addMBB(DestMBB)
808 .constrainAllUses(TII, TRI, RBI);
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000809
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000810 I.eraseFromParent();
811 return true;
812}
813
Tim Northovere9600d82017-02-08 17:57:27 +0000814bool AArch64InstructionSelector::selectVaStartAAPCS(
815 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
816 return false;
817}
818
819bool AArch64InstructionSelector::selectVaStartDarwin(
820 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
821 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
822 unsigned ListReg = I.getOperand(0).getReg();
823
824 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
825
826 auto MIB =
827 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
828 .addDef(ArgsAddrReg)
829 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
830 .addImm(0)
831 .addImm(0);
832
833 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
834
835 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
836 .addUse(ArgsAddrReg)
837 .addUse(ListReg)
838 .addImm(0)
839 .addMemOperand(*I.memoperands_begin());
840
841 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
842 I.eraseFromParent();
843 return true;
844}
845
Amara Emerson1e8c1642018-07-31 00:09:02 +0000846void AArch64InstructionSelector::materializeLargeCMVal(
847 MachineInstr &I, const Value *V, unsigned char OpFlags) const {
848 MachineBasicBlock &MBB = *I.getParent();
849 MachineFunction &MF = *MBB.getParent();
850 MachineRegisterInfo &MRI = MF.getRegInfo();
851 MachineIRBuilder MIB(I);
852
Aditya Nandakumarcef44a22018-12-11 00:48:50 +0000853 auto MovZ = MIB.buildInstr(AArch64::MOVZXi, {&AArch64::GPR64RegClass}, {});
Amara Emerson1e8c1642018-07-31 00:09:02 +0000854 MovZ->addOperand(MF, I.getOperand(1));
855 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
856 AArch64II::MO_NC);
857 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
858 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
859
860 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags, unsigned Offset,
861 unsigned ForceDstReg) {
862 unsigned DstReg = ForceDstReg
863 ? ForceDstReg
864 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
865 auto MovI = MIB.buildInstr(AArch64::MOVKXi).addDef(DstReg).addUse(SrcReg);
866 if (auto *GV = dyn_cast<GlobalValue>(V)) {
867 MovI->addOperand(MF, MachineOperand::CreateGA(
868 GV, MovZ->getOperand(1).getOffset(), Flags));
869 } else {
870 MovI->addOperand(
871 MF, MachineOperand::CreateBA(cast<BlockAddress>(V),
872 MovZ->getOperand(1).getOffset(), Flags));
873 }
874 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
875 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
876 return DstReg;
877 };
Aditya Nandakumarfef76192019-02-05 22:14:40 +0000878 unsigned DstReg = BuildMovK(MovZ.getReg(0),
Amara Emerson1e8c1642018-07-31 00:09:02 +0000879 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
880 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
881 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
882 return;
883}
884
Daniel Sandersf76f3152017-11-16 00:46:35 +0000885bool AArch64InstructionSelector::select(MachineInstr &I,
886 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000887 assert(I.getParent() && "Instruction should be in a basic block!");
888 assert(I.getParent()->getParent() && "Instruction should be in a function!");
889
890 MachineBasicBlock &MBB = *I.getParent();
891 MachineFunction &MF = *MBB.getParent();
892 MachineRegisterInfo &MRI = MF.getRegInfo();
893
Tim Northovercdf23f12016-10-31 18:30:59 +0000894 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000895 // G_PHI requires same handling as PHI
896 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000897 // Certain non-generic instructions also need some special handling.
898
899 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
900 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000901
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000902 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000903 const unsigned DefReg = I.getOperand(0).getReg();
904 const LLT DefTy = MRI.getType(DefReg);
905
906 const TargetRegisterClass *DefRC = nullptr;
907 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
908 DefRC = TRI.getRegClass(DefReg);
909 } else {
910 const RegClassOrRegBank &RegClassOrBank =
911 MRI.getRegClassOrRegBank(DefReg);
912
913 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
914 if (!DefRC) {
915 if (!DefTy.isValid()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000916 LLVM_DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
Tim Northover7d88da62016-11-08 00:34:06 +0000917 return false;
918 }
919 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
920 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
921 if (!DefRC) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000922 LLVM_DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
Tim Northover7d88da62016-11-08 00:34:06 +0000923 return false;
924 }
925 }
926 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000927 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000928
929 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
930 }
931
932 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000933 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000934
935 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000936 }
937
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000938
939 if (I.getNumOperands() != I.getNumExplicitOperands()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000940 LLVM_DEBUG(
941 dbgs() << "Generic instruction has unexpected implicit operands\n");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000942 return false;
943 }
944
Daniel Sandersf76f3152017-11-16 00:46:35 +0000945 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000946 return true;
947
Tim Northover32a078a2016-09-15 10:09:59 +0000948 LLT Ty =
949 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000950
Tim Northover69271c62016-10-12 22:49:11 +0000951 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000952 case TargetOpcode::G_BRCOND: {
953 if (Ty.getSizeInBits() > 32) {
954 // We shouldn't need this on AArch64, but it would be implemented as an
955 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
956 // bit being tested is < 32.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000957 LLVM_DEBUG(dbgs() << "G_BRCOND has type: " << Ty
958 << ", expected at most 32-bits");
Tim Northover5e3dbf32016-10-12 22:49:01 +0000959 return false;
960 }
961
962 const unsigned CondReg = I.getOperand(0).getReg();
963 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
964
Kristof Beylse66bc1f2018-12-18 08:50:02 +0000965 // Speculation tracking/SLH assumes that optimized TB(N)Z/CB(N)Z
966 // instructions will not be produced, as they are conditional branch
967 // instructions that do not set flags.
968 bool ProduceNonFlagSettingCondBr =
969 !MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening);
970 if (ProduceNonFlagSettingCondBr && selectCompareBranch(I, MF, MRI))
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000971 return true;
972
Kristof Beylse66bc1f2018-12-18 08:50:02 +0000973 if (ProduceNonFlagSettingCondBr) {
974 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
975 .addUse(CondReg)
976 .addImm(/*bit offset=*/0)
977 .addMBB(DestMBB);
Tim Northover5e3dbf32016-10-12 22:49:01 +0000978
Kristof Beylse66bc1f2018-12-18 08:50:02 +0000979 I.eraseFromParent();
980 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
981 } else {
982 auto CMP = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
983 .addDef(AArch64::WZR)
984 .addUse(CondReg)
985 .addImm(1);
986 constrainSelectedInstRegOperands(*CMP.getInstr(), TII, TRI, RBI);
987 auto Bcc =
988 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::Bcc))
989 .addImm(AArch64CC::EQ)
990 .addMBB(DestMBB);
991
992 I.eraseFromParent();
993 return constrainSelectedInstRegOperands(*Bcc.getInstr(), TII, TRI, RBI);
994 }
Tim Northover5e3dbf32016-10-12 22:49:01 +0000995 }
996
Kristof Beyls65a12c02017-01-30 09:13:18 +0000997 case TargetOpcode::G_BRINDIRECT: {
998 I.setDesc(TII.get(AArch64::BR));
999 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1000 }
1001
Tim Northover4494d692016-10-18 19:47:57 +00001002 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +00001003 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +00001004 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
1005
1006 const LLT s32 = LLT::scalar(32);
1007 const LLT s64 = LLT::scalar(64);
1008 const LLT p0 = LLT::pointer(0, 64);
1009
1010 const unsigned DefReg = I.getOperand(0).getReg();
1011 const LLT DefTy = MRI.getType(DefReg);
1012 const unsigned DefSize = DefTy.getSizeInBits();
1013 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1014
1015 // FIXME: Redundant check, but even less readable when factored out.
1016 if (isFP) {
1017 if (Ty != s32 && Ty != s64) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001018 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
1019 << " constant, expected: " << s32 << " or " << s64
1020 << '\n');
Tim Northover4494d692016-10-18 19:47:57 +00001021 return false;
1022 }
1023
1024 if (RB.getID() != AArch64::FPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001025 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
1026 << " constant on bank: " << RB
1027 << ", expected: FPR\n");
Tim Northover4494d692016-10-18 19:47:57 +00001028 return false;
1029 }
Daniel Sanders11300ce2017-10-13 21:28:03 +00001030
1031 // The case when we have 0.0 is covered by tablegen. Reject it here so we
1032 // can be sure tablegen works correctly and isn't rescued by this code.
1033 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
1034 return false;
Tim Northover4494d692016-10-18 19:47:57 +00001035 } else {
Daniel Sanders05540042017-08-08 10:44:31 +00001036 // s32 and s64 are covered by tablegen.
1037 if (Ty != p0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001038 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
1039 << " constant, expected: " << s32 << ", " << s64
1040 << ", or " << p0 << '\n');
Tim Northover4494d692016-10-18 19:47:57 +00001041 return false;
1042 }
1043
1044 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001045 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
1046 << " constant on bank: " << RB
1047 << ", expected: GPR\n");
Tim Northover4494d692016-10-18 19:47:57 +00001048 return false;
1049 }
1050 }
1051
1052 const unsigned MovOpc =
1053 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
1054
1055 I.setDesc(TII.get(MovOpc));
1056
1057 if (isFP) {
1058 const TargetRegisterClass &GPRRC =
1059 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
1060 const TargetRegisterClass &FPRRC =
1061 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
1062
1063 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
1064 MachineOperand &RegOp = I.getOperand(0);
1065 RegOp.setReg(DefGPRReg);
1066
1067 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
1068 TII.get(AArch64::COPY))
1069 .addDef(DefReg)
1070 .addUse(DefGPRReg);
1071
1072 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001073 LLVM_DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
Tim Northover4494d692016-10-18 19:47:57 +00001074 return false;
1075 }
1076
1077 MachineOperand &ImmOp = I.getOperand(1);
1078 // FIXME: Is going through int64_t always correct?
1079 ImmOp.ChangeToImmediate(
1080 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001081 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +00001082 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
1083 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001084 } else if (I.getOperand(1).isImm()) {
1085 uint64_t Val = I.getOperand(1).getImm();
1086 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +00001087 }
1088
1089 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1090 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +00001091 }
Tim Northover7b6d66c2017-07-20 22:58:38 +00001092 case TargetOpcode::G_EXTRACT: {
1093 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +00001094 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
Amara Emerson242efdb2018-02-18 17:28:34 +00001095 (void)DstTy;
Amara Emersonbc03bae2018-02-18 17:03:02 +00001096 unsigned SrcSize = SrcTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +00001097 // Larger extracts are vectors, same-size extracts should be something else
1098 // by now (either split up or simplified to a COPY).
1099 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
1100 return false;
1101
Amara Emersonbc03bae2018-02-18 17:03:02 +00001102 I.setDesc(TII.get(SrcSize == 64 ? AArch64::UBFMXri : AArch64::UBFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +00001103 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
1104 Ty.getSizeInBits() - 1);
1105
Amara Emersonbc03bae2018-02-18 17:03:02 +00001106 if (SrcSize < 64) {
1107 assert(SrcSize == 32 && DstTy.getSizeInBits() == 16 &&
1108 "unexpected G_EXTRACT types");
1109 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1110 }
1111
Tim Northover7b6d66c2017-07-20 22:58:38 +00001112 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
1113 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
1114 TII.get(AArch64::COPY))
1115 .addDef(I.getOperand(0).getReg())
1116 .addUse(DstReg, 0, AArch64::sub_32);
1117 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
1118 AArch64::GPR32RegClass, MRI);
1119 I.getOperand(0).setReg(DstReg);
1120
1121 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1122 }
1123
1124 case TargetOpcode::G_INSERT: {
1125 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +00001126 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1127 unsigned DstSize = DstTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +00001128 // Larger inserts are vectors, same-size ones should be something else by
1129 // now (split up or turned into COPYs).
1130 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
1131 return false;
1132
Amara Emersonbc03bae2018-02-18 17:03:02 +00001133 I.setDesc(TII.get(DstSize == 64 ? AArch64::BFMXri : AArch64::BFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +00001134 unsigned LSB = I.getOperand(3).getImm();
1135 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
Amara Emersonbc03bae2018-02-18 17:03:02 +00001136 I.getOperand(3).setImm((DstSize - LSB) % DstSize);
Tim Northover7b6d66c2017-07-20 22:58:38 +00001137 MachineInstrBuilder(MF, I).addImm(Width - 1);
1138
Amara Emersonbc03bae2018-02-18 17:03:02 +00001139 if (DstSize < 64) {
1140 assert(DstSize == 32 && SrcTy.getSizeInBits() == 16 &&
1141 "unexpected G_INSERT types");
1142 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1143 }
1144
Tim Northover7b6d66c2017-07-20 22:58:38 +00001145 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
1146 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
1147 TII.get(AArch64::SUBREG_TO_REG))
1148 .addDef(SrcReg)
1149 .addImm(0)
1150 .addUse(I.getOperand(2).getReg())
1151 .addImm(AArch64::sub_32);
1152 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
1153 AArch64::GPR32RegClass, MRI);
1154 I.getOperand(2).setReg(SrcReg);
1155
1156 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1157 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001158 case TargetOpcode::G_FRAME_INDEX: {
1159 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +00001160 if (Ty != LLT::pointer(0, 64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001161 LLVM_DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
1162 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001163 return false;
1164 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001165 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001166
1167 // MOs for a #0 shifted immediate.
1168 I.addOperand(MachineOperand::CreateImm(0));
1169 I.addOperand(MachineOperand::CreateImm(0));
1170
1171 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1172 }
Tim Northoverbdf16242016-10-10 21:50:00 +00001173
1174 case TargetOpcode::G_GLOBAL_VALUE: {
1175 auto GV = I.getOperand(1).getGlobal();
1176 if (GV->isThreadLocal()) {
1177 // FIXME: we don't support TLS yet.
1178 return false;
1179 }
1180 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +00001181 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +00001182 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +00001183 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +00001184 } else if (TM.getCodeModel() == CodeModel::Large) {
1185 // Materialize the global using movz/movk instructions.
Amara Emerson1e8c1642018-07-31 00:09:02 +00001186 materializeLargeCMVal(I, GV, OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +00001187 I.eraseFromParent();
1188 return true;
David Green9dd1d452018-08-22 11:31:39 +00001189 } else if (TM.getCodeModel() == CodeModel::Tiny) {
1190 I.setDesc(TII.get(AArch64::ADR));
1191 I.getOperand(1).setTargetFlags(OpFlags);
Tim Northoverfe7c59a2016-12-13 18:25:38 +00001192 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +00001193 I.setDesc(TII.get(AArch64::MOVaddr));
1194 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
1195 MachineInstrBuilder MIB(MF, I);
1196 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
1197 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
1198 }
1199 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1200 }
1201
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001202 case TargetOpcode::G_LOAD:
1203 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +00001204 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001205
Tim Northover5ae83502016-09-15 09:20:34 +00001206 if (PtrTy != LLT::pointer(0, 64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001207 LLVM_DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
1208 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001209 return false;
1210 }
1211
Daniel Sanders3c1c4c02017-12-05 05:52:07 +00001212 auto &MemOp = **I.memoperands_begin();
1213 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001214 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
Daniel Sanders3c1c4c02017-12-05 05:52:07 +00001215 return false;
1216 }
Daniel Sandersf84bc372018-05-05 20:53:24 +00001217 unsigned MemSizeInBits = MemOp.getSize() * 8;
Daniel Sanders3c1c4c02017-12-05 05:52:07 +00001218
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001219 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001220#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001221 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001222 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001223 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
1224 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +00001225 assert(MRI.getType(PtrReg).isPointer() &&
1226 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001227#endif
1228
1229 const unsigned ValReg = I.getOperand(0).getReg();
1230 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
1231
1232 const unsigned NewOpc =
Daniel Sandersf84bc372018-05-05 20:53:24 +00001233 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemSizeInBits);
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001234 if (NewOpc == I.getOpcode())
1235 return false;
1236
1237 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001238
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001239 uint64_t Offset = 0;
1240 auto *PtrMI = MRI.getVRegDef(PtrReg);
1241
1242 // Try to fold a GEP into our unsigned immediate addressing mode.
1243 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
1244 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
1245 int64_t Imm = *COff;
Daniel Sandersf84bc372018-05-05 20:53:24 +00001246 const unsigned Size = MemSizeInBits / 8;
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001247 const unsigned Scale = Log2_32(Size);
1248 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
1249 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
1250 I.getOperand(1).setReg(Ptr2Reg);
1251 PtrMI = MRI.getVRegDef(Ptr2Reg);
1252 Offset = Imm / Size;
1253 }
1254 }
1255 }
1256
Ahmed Bougachaf75782f2017-03-27 17:31:56 +00001257 // If we haven't folded anything into our addressing mode yet, try to fold
1258 // a frame index into the base+offset.
1259 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1260 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
1261
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001262 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +00001263
1264 // If we're storing a 0, use WZR/XZR.
1265 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
1266 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
1267 if (I.getOpcode() == AArch64::STRWui)
1268 I.getOperand(0).setReg(AArch64::WZR);
1269 else if (I.getOpcode() == AArch64::STRXui)
1270 I.getOperand(0).setReg(AArch64::XZR);
1271 }
1272 }
1273
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001274 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1275 }
1276
Tim Northover9dd78f82017-02-08 21:22:25 +00001277 case TargetOpcode::G_SMULH:
1278 case TargetOpcode::G_UMULH: {
1279 // Reject the various things we don't support yet.
1280 if (unsupportedBinOp(I, RBI, MRI, TRI))
1281 return false;
1282
1283 const unsigned DefReg = I.getOperand(0).getReg();
1284 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1285
1286 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001287 LLVM_DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
Tim Northover9dd78f82017-02-08 21:22:25 +00001288 return false;
1289 }
1290
1291 if (Ty != LLT::scalar(64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001292 LLVM_DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1293 << ", expected: " << LLT::scalar(64) << '\n');
Tim Northover9dd78f82017-02-08 21:22:25 +00001294 return false;
1295 }
1296
1297 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1298 : AArch64::UMULHrr;
1299 I.setDesc(TII.get(NewOpc));
1300
1301 // Now that we selected an opcode, we need to constrain the register
1302 // operands to use appropriate classes.
1303 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1304 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001305 case TargetOpcode::G_FADD:
1306 case TargetOpcode::G_FSUB:
1307 case TargetOpcode::G_FMUL:
1308 case TargetOpcode::G_FDIV:
1309
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001310 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001311 case TargetOpcode::G_SHL:
1312 case TargetOpcode::G_LSHR:
1313 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001314 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001315 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001316 if (unsupportedBinOp(I, RBI, MRI, TRI))
1317 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001318
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001319 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001320
1321 const unsigned DefReg = I.getOperand(0).getReg();
1322 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1323
1324 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1325 if (NewOpc == I.getOpcode())
1326 return false;
1327
1328 I.setDesc(TII.get(NewOpc));
1329 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001330
1331 // Now that we selected an opcode, we need to constrain the register
1332 // operands to use appropriate classes.
1333 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1334 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001335
Jessica Paquette7d6784f2019-03-14 22:54:29 +00001336 case TargetOpcode::G_UADDO: {
1337 // TODO: Support other types.
1338 unsigned OpSize = Ty.getSizeInBits();
1339 if (OpSize != 32 && OpSize != 64) {
1340 LLVM_DEBUG(
1341 dbgs()
1342 << "G_UADDO currently only supported for 32 and 64 b types.\n");
1343 return false;
1344 }
1345
1346 // TODO: Support vectors.
1347 if (Ty.isVector()) {
1348 LLVM_DEBUG(dbgs() << "G_UADDO currently only supported for scalars.\n");
1349 return false;
1350 }
1351
1352 // Add and set the set condition flag.
1353 unsigned AddsOpc = OpSize == 32 ? AArch64::ADDSWrr : AArch64::ADDSXrr;
1354 MachineIRBuilder MIRBuilder(I);
1355 auto AddsMI = MIRBuilder.buildInstr(
1356 AddsOpc, {I.getOperand(0).getReg()},
1357 {I.getOperand(2).getReg(), I.getOperand(3).getReg()});
1358 constrainSelectedInstRegOperands(*AddsMI, TII, TRI, RBI);
1359
1360 // Now, put the overflow result in the register given by the first operand
1361 // to the G_UADDO. CSINC increments the result when the predicate is false,
1362 // so to get the increment when it's true, we need to use the inverse. In
1363 // this case, we want to increment when carry is set.
1364 auto CsetMI = MIRBuilder
1365 .buildInstr(AArch64::CSINCWr, {I.getOperand(1).getReg()},
1366 {AArch64::WZR, AArch64::WZR})
1367 .addImm(getInvertedCondCode(AArch64CC::HS));
1368 constrainSelectedInstRegOperands(*CsetMI, TII, TRI, RBI);
1369 I.eraseFromParent();
1370 return true;
1371 }
1372
Tim Northover398c5f52017-02-14 20:56:29 +00001373 case TargetOpcode::G_PTR_MASK: {
1374 uint64_t Align = I.getOperand(2).getImm();
1375 if (Align >= 64 || Align == 0)
1376 return false;
1377
1378 uint64_t Mask = ~((1ULL << Align) - 1);
1379 I.setDesc(TII.get(AArch64::ANDXri));
1380 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1381
1382 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1383 }
Tim Northover037af52c2016-10-31 18:31:09 +00001384 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001385 case TargetOpcode::G_TRUNC: {
1386 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1387 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1388
1389 const unsigned DstReg = I.getOperand(0).getReg();
1390 const unsigned SrcReg = I.getOperand(1).getReg();
1391
1392 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1393 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1394
1395 if (DstRB.getID() != SrcRB.getID()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001396 LLVM_DEBUG(
1397 dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001398 return false;
1399 }
1400
1401 if (DstRB.getID() == AArch64::GPRRegBankID) {
1402 const TargetRegisterClass *DstRC =
1403 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1404 if (!DstRC)
1405 return false;
1406
1407 const TargetRegisterClass *SrcRC =
1408 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1409 if (!SrcRC)
1410 return false;
1411
1412 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1413 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001414 LLVM_DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001415 return false;
1416 }
1417
1418 if (DstRC == SrcRC) {
1419 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001420 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1421 SrcTy == LLT::scalar(64)) {
1422 llvm_unreachable("TableGen can import this case");
1423 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001424 } else if (DstRC == &AArch64::GPR32RegClass &&
1425 SrcRC == &AArch64::GPR64RegClass) {
1426 I.getOperand(1).setSubReg(AArch64::sub_32);
1427 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001428 LLVM_DEBUG(
1429 dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001430 return false;
1431 }
1432
1433 I.setDesc(TII.get(TargetOpcode::COPY));
1434 return true;
1435 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1436 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1437 I.setDesc(TII.get(AArch64::XTNv4i16));
1438 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1439 return true;
1440 }
1441 }
1442
1443 return false;
1444 }
1445
Tim Northover3d38b3a2016-10-11 20:50:21 +00001446 case TargetOpcode::G_ANYEXT: {
1447 const unsigned DstReg = I.getOperand(0).getReg();
1448 const unsigned SrcReg = I.getOperand(1).getReg();
1449
Quentin Colombetcb629a82016-10-12 03:57:49 +00001450 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1451 if (RBDst.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001452 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst
1453 << ", expected: GPR\n");
Quentin Colombetcb629a82016-10-12 03:57:49 +00001454 return false;
1455 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001456
Quentin Colombetcb629a82016-10-12 03:57:49 +00001457 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1458 if (RBSrc.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001459 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc
1460 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001461 return false;
1462 }
1463
1464 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1465
1466 if (DstSize == 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001467 LLVM_DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001468 return false;
1469 }
1470
Quentin Colombetcb629a82016-10-12 03:57:49 +00001471 if (DstSize != 64 && DstSize > 32) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001472 LLVM_DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1473 << ", expected: 32 or 64\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001474 return false;
1475 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001476 // At this point G_ANYEXT is just like a plain COPY, but we need
1477 // to explicitly form the 64-bit value if any.
1478 if (DstSize > 32) {
1479 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1480 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1481 .addDef(ExtSrc)
1482 .addImm(0)
1483 .addUse(SrcReg)
1484 .addImm(AArch64::sub_32);
1485 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001486 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001487 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001488 }
1489
1490 case TargetOpcode::G_ZEXT:
1491 case TargetOpcode::G_SEXT: {
1492 unsigned Opcode = I.getOpcode();
1493 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1494 SrcTy = MRI.getType(I.getOperand(1).getReg());
1495 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1496 const unsigned DefReg = I.getOperand(0).getReg();
1497 const unsigned SrcReg = I.getOperand(1).getReg();
1498 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1499
1500 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001501 LLVM_DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1502 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001503 return false;
1504 }
1505
1506 MachineInstr *ExtI;
1507 if (DstTy == LLT::scalar(64)) {
1508 // FIXME: Can we avoid manually doing this?
1509 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001510 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1511 << " operand\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001512 return false;
1513 }
1514
1515 const unsigned SrcXReg =
1516 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1517 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1518 .addDef(SrcXReg)
1519 .addImm(0)
1520 .addUse(SrcReg)
1521 .addImm(AArch64::sub_32);
1522
1523 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1524 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1525 .addDef(DefReg)
1526 .addUse(SrcXReg)
1527 .addImm(0)
1528 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001529 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001530 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1531 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1532 .addDef(DefReg)
1533 .addUse(SrcReg)
1534 .addImm(0)
1535 .addImm(SrcTy.getSizeInBits() - 1);
1536 } else {
1537 return false;
1538 }
1539
1540 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1541
1542 I.eraseFromParent();
1543 return true;
1544 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001545
Tim Northover69271c62016-10-12 22:49:11 +00001546 case TargetOpcode::G_SITOFP:
1547 case TargetOpcode::G_UITOFP:
1548 case TargetOpcode::G_FPTOSI:
1549 case TargetOpcode::G_FPTOUI: {
1550 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1551 SrcTy = MRI.getType(I.getOperand(1).getReg());
1552 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1553 if (NewOpc == Opcode)
1554 return false;
1555
1556 I.setDesc(TII.get(NewOpc));
1557 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1558
1559 return true;
1560 }
1561
1562
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001563 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001564 // The importer is currently unable to import pointer types since they
1565 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001566 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001567
Daniel Sandersedd07842017-08-17 09:26:14 +00001568 case TargetOpcode::G_BITCAST:
1569 // Imported SelectionDAG rules can handle every bitcast except those that
1570 // bitcast from a type to the same type. Ideally, these shouldn't occur
1571 // but we might not run an optimizer that deletes them.
1572 if (MRI.getType(I.getOperand(0).getReg()) ==
1573 MRI.getType(I.getOperand(1).getReg()))
1574 return selectCopy(I, TII, MRI, TRI, RBI);
1575 return false;
1576
Tim Northover9ac0eba2016-11-08 00:45:29 +00001577 case TargetOpcode::G_SELECT: {
1578 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001579 LLVM_DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1580 << ", expected: " << LLT::scalar(1) << '\n');
Tim Northover9ac0eba2016-11-08 00:45:29 +00001581 return false;
1582 }
1583
1584 const unsigned CondReg = I.getOperand(1).getReg();
1585 const unsigned TReg = I.getOperand(2).getReg();
1586 const unsigned FReg = I.getOperand(3).getReg();
1587
1588 unsigned CSelOpc = 0;
1589
1590 if (Ty == LLT::scalar(32)) {
1591 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001592 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001593 CSelOpc = AArch64::CSELXr;
1594 } else {
1595 return false;
1596 }
1597
1598 MachineInstr &TstMI =
1599 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1600 .addDef(AArch64::WZR)
1601 .addUse(CondReg)
1602 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1603
1604 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1605 .addDef(I.getOperand(0).getReg())
1606 .addUse(TReg)
1607 .addUse(FReg)
1608 .addImm(AArch64CC::NE);
1609
1610 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1611 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1612
1613 I.eraseFromParent();
1614 return true;
1615 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001616 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001617 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001618 LLVM_DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1619 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001620 return false;
1621 }
1622
1623 unsigned CmpOpc = 0;
1624 unsigned ZReg = 0;
1625
1626 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1627 if (CmpTy == LLT::scalar(32)) {
1628 CmpOpc = AArch64::SUBSWrr;
1629 ZReg = AArch64::WZR;
1630 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1631 CmpOpc = AArch64::SUBSXrr;
1632 ZReg = AArch64::XZR;
1633 } else {
1634 return false;
1635 }
1636
Kristof Beyls22524402017-01-05 10:16:08 +00001637 // CSINC increments the result by one when the condition code is false.
1638 // Therefore, we have to invert the predicate to get an increment by 1 when
1639 // the predicate is true.
1640 const AArch64CC::CondCode invCC =
1641 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1642 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001643
1644 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1645 .addDef(ZReg)
1646 .addUse(I.getOperand(2).getReg())
1647 .addUse(I.getOperand(3).getReg());
1648
1649 MachineInstr &CSetMI =
1650 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1651 .addDef(I.getOperand(0).getReg())
1652 .addUse(AArch64::WZR)
1653 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001654 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001655
1656 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1657 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1658
1659 I.eraseFromParent();
1660 return true;
1661 }
1662
Tim Northover7dd378d2016-10-12 22:49:07 +00001663 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001664 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001665 LLVM_DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1666 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001667 return false;
1668 }
1669
1670 unsigned CmpOpc = 0;
1671 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1672 if (CmpTy == LLT::scalar(32)) {
1673 CmpOpc = AArch64::FCMPSrr;
1674 } else if (CmpTy == LLT::scalar(64)) {
1675 CmpOpc = AArch64::FCMPDrr;
1676 } else {
1677 return false;
1678 }
1679
1680 // FIXME: regbank
1681
1682 AArch64CC::CondCode CC1, CC2;
1683 changeFCMPPredToAArch64CC(
1684 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1685
1686 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1687 .addUse(I.getOperand(2).getReg())
1688 .addUse(I.getOperand(3).getReg());
1689
1690 const unsigned DefReg = I.getOperand(0).getReg();
1691 unsigned Def1Reg = DefReg;
1692 if (CC2 != AArch64CC::AL)
1693 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1694
1695 MachineInstr &CSetMI =
1696 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1697 .addDef(Def1Reg)
1698 .addUse(AArch64::WZR)
1699 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001700 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001701
1702 if (CC2 != AArch64CC::AL) {
1703 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1704 MachineInstr &CSet2MI =
1705 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1706 .addDef(Def2Reg)
1707 .addUse(AArch64::WZR)
1708 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001709 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001710 MachineInstr &OrMI =
1711 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1712 .addDef(DefReg)
1713 .addUse(Def1Reg)
1714 .addUse(Def2Reg);
1715 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1716 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1717 }
1718
1719 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1720 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1721
1722 I.eraseFromParent();
1723 return true;
1724 }
Tim Northovere9600d82017-02-08 17:57:27 +00001725 case TargetOpcode::G_VASTART:
1726 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1727 : selectVaStartAAPCS(I, MF, MRI);
Amara Emerson1f5d9942018-04-25 14:43:59 +00001728 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
1729 if (!I.getOperand(0).isIntrinsicID())
1730 return false;
1731 if (I.getOperand(0).getIntrinsicID() != Intrinsic::trap)
1732 return false;
1733 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::BRK))
1734 .addImm(1);
1735 I.eraseFromParent();
1736 return true;
Amara Emerson1e8c1642018-07-31 00:09:02 +00001737 case TargetOpcode::G_IMPLICIT_DEF: {
Justin Bogner4fc69662017-07-12 17:32:32 +00001738 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001739 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1740 const unsigned DstReg = I.getOperand(0).getReg();
1741 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1742 const TargetRegisterClass *DstRC =
1743 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1744 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001745 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001746 }
Amara Emerson1e8c1642018-07-31 00:09:02 +00001747 case TargetOpcode::G_BLOCK_ADDR: {
1748 if (TM.getCodeModel() == CodeModel::Large) {
1749 materializeLargeCMVal(I, I.getOperand(1).getBlockAddress(), 0);
1750 I.eraseFromParent();
1751 return true;
1752 } else {
1753 I.setDesc(TII.get(AArch64::MOVaddrBA));
1754 auto MovMI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::MOVaddrBA),
1755 I.getOperand(0).getReg())
1756 .addBlockAddress(I.getOperand(1).getBlockAddress(),
1757 /* Offset */ 0, AArch64II::MO_PAGE)
1758 .addBlockAddress(
1759 I.getOperand(1).getBlockAddress(), /* Offset */ 0,
1760 AArch64II::MO_NC | AArch64II::MO_PAGEOFF);
1761 I.eraseFromParent();
1762 return constrainSelectedInstRegOperands(*MovMI, TII, TRI, RBI);
1763 }
1764 }
Amara Emerson5ec14602018-12-10 18:44:58 +00001765 case TargetOpcode::G_BUILD_VECTOR:
1766 return selectBuildVector(I, MRI);
Amara Emerson8cb186c2018-12-20 01:11:04 +00001767 case TargetOpcode::G_MERGE_VALUES:
1768 return selectMergeValues(I, MRI);
Jessica Paquette245047d2019-01-24 22:00:41 +00001769 case TargetOpcode::G_UNMERGE_VALUES:
1770 return selectUnmergeValues(I, MRI);
Amara Emerson1abe05c2019-02-21 20:20:16 +00001771 case TargetOpcode::G_SHUFFLE_VECTOR:
1772 return selectShuffleVector(I, MRI);
Jessica Paquette607774c2019-03-11 22:18:01 +00001773 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
1774 return selectExtractElt(I, MRI);
Jessica Paquette5aff1f42019-03-14 18:01:30 +00001775 case TargetOpcode::G_INSERT_VECTOR_ELT:
1776 return selectInsertElt(I, MRI);
Amara Emerson2ff22982019-03-14 22:48:15 +00001777 case TargetOpcode::G_CONCAT_VECTORS:
1778 return selectConcatVectors(I, MRI);
Amara Emerson1e8c1642018-07-31 00:09:02 +00001779 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001780
1781 return false;
1782}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001783
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001784MachineInstr *AArch64InstructionSelector::emitScalarToVector(
Amara Emerson8acb0d92019-03-04 19:16:00 +00001785 unsigned EltSize, const TargetRegisterClass *DstRC, unsigned Scalar,
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001786 MachineIRBuilder &MIRBuilder) const {
1787 auto Undef = MIRBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF, {DstRC}, {});
Amara Emerson5ec14602018-12-10 18:44:58 +00001788
1789 auto BuildFn = [&](unsigned SubregIndex) {
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001790 auto Ins =
1791 MIRBuilder
1792 .buildInstr(TargetOpcode::INSERT_SUBREG, {DstRC}, {Undef, Scalar})
1793 .addImm(SubregIndex);
1794 constrainSelectedInstRegOperands(*Undef, TII, TRI, RBI);
1795 constrainSelectedInstRegOperands(*Ins, TII, TRI, RBI);
1796 return &*Ins;
Amara Emerson5ec14602018-12-10 18:44:58 +00001797 };
1798
Amara Emerson8acb0d92019-03-04 19:16:00 +00001799 switch (EltSize) {
Jessica Paquette245047d2019-01-24 22:00:41 +00001800 case 16:
1801 return BuildFn(AArch64::hsub);
Amara Emerson5ec14602018-12-10 18:44:58 +00001802 case 32:
1803 return BuildFn(AArch64::ssub);
1804 case 64:
1805 return BuildFn(AArch64::dsub);
1806 default:
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001807 return nullptr;
Amara Emerson5ec14602018-12-10 18:44:58 +00001808 }
1809}
1810
Amara Emerson8cb186c2018-12-20 01:11:04 +00001811bool AArch64InstructionSelector::selectMergeValues(
1812 MachineInstr &I, MachineRegisterInfo &MRI) const {
1813 assert(I.getOpcode() == TargetOpcode::G_MERGE_VALUES && "unexpected opcode");
1814 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1815 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1816 assert(!DstTy.isVector() && !SrcTy.isVector() && "invalid merge operation");
1817
1818 // At the moment we only support merging two s32s into an s64.
1819 if (I.getNumOperands() != 3)
1820 return false;
1821 if (DstTy.getSizeInBits() != 64 || SrcTy.getSizeInBits() != 32)
1822 return false;
1823 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
1824 if (RB.getID() != AArch64::GPRRegBankID)
1825 return false;
1826
1827 auto *DstRC = &AArch64::GPR64RegClass;
1828 unsigned SubToRegDef = MRI.createVirtualRegister(DstRC);
1829 MachineInstr &SubRegMI = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1830 TII.get(TargetOpcode::SUBREG_TO_REG))
1831 .addDef(SubToRegDef)
1832 .addImm(0)
1833 .addUse(I.getOperand(1).getReg())
1834 .addImm(AArch64::sub_32);
1835 unsigned SubToRegDef2 = MRI.createVirtualRegister(DstRC);
1836 // Need to anyext the second scalar before we can use bfm
1837 MachineInstr &SubRegMI2 = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1838 TII.get(TargetOpcode::SUBREG_TO_REG))
1839 .addDef(SubToRegDef2)
1840 .addImm(0)
1841 .addUse(I.getOperand(2).getReg())
1842 .addImm(AArch64::sub_32);
Amara Emerson8cb186c2018-12-20 01:11:04 +00001843 MachineInstr &BFM =
1844 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::BFMXri))
Amara Emerson321bfb22018-12-20 03:27:42 +00001845 .addDef(I.getOperand(0).getReg())
Amara Emerson8cb186c2018-12-20 01:11:04 +00001846 .addUse(SubToRegDef)
1847 .addUse(SubToRegDef2)
1848 .addImm(32)
1849 .addImm(31);
1850 constrainSelectedInstRegOperands(SubRegMI, TII, TRI, RBI);
1851 constrainSelectedInstRegOperands(SubRegMI2, TII, TRI, RBI);
1852 constrainSelectedInstRegOperands(BFM, TII, TRI, RBI);
1853 I.eraseFromParent();
1854 return true;
1855}
1856
Jessica Paquette607774c2019-03-11 22:18:01 +00001857static bool getLaneCopyOpcode(unsigned &CopyOpc, unsigned &ExtractSubReg,
1858 const unsigned EltSize) {
1859 // Choose a lane copy opcode and subregister based off of the size of the
1860 // vector's elements.
1861 switch (EltSize) {
1862 case 16:
1863 CopyOpc = AArch64::CPYi16;
1864 ExtractSubReg = AArch64::hsub;
1865 break;
1866 case 32:
1867 CopyOpc = AArch64::CPYi32;
1868 ExtractSubReg = AArch64::ssub;
1869 break;
1870 case 64:
1871 CopyOpc = AArch64::CPYi64;
1872 ExtractSubReg = AArch64::dsub;
1873 break;
1874 default:
1875 // Unknown size, bail out.
1876 LLVM_DEBUG(dbgs() << "Elt size '" << EltSize << "' unsupported.\n");
1877 return false;
1878 }
1879 return true;
1880}
1881
Jessica Paquettebb1aced2019-03-13 21:19:29 +00001882/// Given a register \p Reg, find the value of a constant defining \p Reg.
1883/// Return true if one could be found, and store it in \p Val. Return false
1884/// otherwise.
1885static bool getConstantValueForReg(unsigned Reg, MachineRegisterInfo &MRI,
1886 unsigned &Val) {
1887 // Look at the def of the register.
1888 MachineInstr *Def = MRI.getVRegDef(Reg);
1889 if (!Def)
1890 return false;
1891
1892 // Find the first definition which isn't a copy.
1893 if (Def->isCopy()) {
1894 Reg = Def->getOperand(1).getReg();
1895 auto It = find_if_not(MRI.reg_nodbg_instructions(Reg),
1896 [](const MachineInstr &MI) { return MI.isCopy(); });
1897 if (It == MRI.reg_instr_nodbg_end()) {
1898 LLVM_DEBUG(dbgs() << "Couldn't find non-copy def for register\n");
1899 return false;
1900 }
1901 Def = &*It;
1902 }
1903
1904 // TODO: Handle opcodes other than G_CONSTANT.
1905 if (Def->getOpcode() != TargetOpcode::G_CONSTANT) {
1906 LLVM_DEBUG(dbgs() << "VRegs defined by anything other than G_CONSTANT "
1907 "currently unsupported.\n");
1908 return false;
1909 }
1910
1911 // Return the constant value associated with the operand.
1912 Val = Def->getOperand(1).getCImm()->getLimitedValue();
1913 return true;
1914}
1915
Amara Emersond61b89b2019-03-14 22:48:18 +00001916MachineInstr *AArch64InstructionSelector::emitExtractVectorElt(
1917 Optional<unsigned> DstReg, const RegisterBank &DstRB, LLT ScalarTy,
1918 unsigned VecReg, unsigned LaneIdx, MachineIRBuilder &MIRBuilder) const {
1919 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
1920 unsigned CopyOpc = 0;
1921 unsigned ExtractSubReg = 0;
1922 if (!getLaneCopyOpcode(CopyOpc, ExtractSubReg, ScalarTy.getSizeInBits())) {
1923 LLVM_DEBUG(
1924 dbgs() << "Couldn't determine lane copy opcode for instruction.\n");
1925 return nullptr;
1926 }
1927
1928 const TargetRegisterClass *DstRC =
1929 getRegClassForTypeOnBank(ScalarTy, DstRB, RBI, true);
1930 if (!DstRC) {
1931 LLVM_DEBUG(dbgs() << "Could not determine destination register class.\n");
1932 return nullptr;
1933 }
1934
1935 const RegisterBank &VecRB = *RBI.getRegBank(VecReg, MRI, TRI);
1936 const LLT &VecTy = MRI.getType(VecReg);
1937 const TargetRegisterClass *VecRC =
1938 getRegClassForTypeOnBank(VecTy, VecRB, RBI, true);
1939 if (!VecRC) {
1940 LLVM_DEBUG(dbgs() << "Could not determine source register class.\n");
1941 return nullptr;
1942 }
1943
1944 // The register that we're going to copy into.
1945 unsigned InsertReg = VecReg;
1946 if (!DstReg)
1947 DstReg = MRI.createVirtualRegister(DstRC);
1948 // If the lane index is 0, we just use a subregister COPY.
1949 if (LaneIdx == 0) {
1950 auto CopyMI =
1951 BuildMI(MIRBuilder.getMBB(), MIRBuilder.getInsertPt(),
1952 MIRBuilder.getDL(), TII.get(TargetOpcode::COPY), *DstReg)
1953 .addUse(VecReg, 0, ExtractSubReg);
1954 RBI.constrainGenericRegister(*DstReg, *DstRC, MRI);
1955 return &*CopyMI;
1956 }
1957
1958 // Lane copies require 128-bit wide registers. If we're dealing with an
1959 // unpacked vector, then we need to move up to that width. Insert an implicit
1960 // def and a subregister insert to get us there.
1961 if (VecTy.getSizeInBits() != 128) {
1962 MachineInstr *ScalarToVector = emitScalarToVector(
1963 VecTy.getSizeInBits(), &AArch64::FPR128RegClass, VecReg, MIRBuilder);
1964 if (!ScalarToVector)
1965 return nullptr;
1966 InsertReg = ScalarToVector->getOperand(0).getReg();
1967 }
1968
1969 MachineInstr *LaneCopyMI =
1970 MIRBuilder.buildInstr(CopyOpc, {*DstReg}, {InsertReg}).addImm(LaneIdx);
1971 constrainSelectedInstRegOperands(*LaneCopyMI, TII, TRI, RBI);
1972
1973 // Make sure that we actually constrain the initial copy.
1974 RBI.constrainGenericRegister(*DstReg, *DstRC, MRI);
1975 return LaneCopyMI;
1976}
1977
Jessica Paquette607774c2019-03-11 22:18:01 +00001978bool AArch64InstructionSelector::selectExtractElt(
1979 MachineInstr &I, MachineRegisterInfo &MRI) const {
1980 assert(I.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT &&
1981 "unexpected opcode!");
1982 unsigned DstReg = I.getOperand(0).getReg();
1983 const LLT NarrowTy = MRI.getType(DstReg);
1984 const unsigned SrcReg = I.getOperand(1).getReg();
1985 const LLT WideTy = MRI.getType(SrcReg);
Amara Emersond61b89b2019-03-14 22:48:18 +00001986 (void)WideTy;
Jessica Paquette607774c2019-03-11 22:18:01 +00001987 assert(WideTy.getSizeInBits() >= NarrowTy.getSizeInBits() &&
1988 "source register size too small!");
1989 assert(NarrowTy.isScalar() && "cannot extract vector into vector!");
1990
1991 // Need the lane index to determine the correct copy opcode.
1992 MachineOperand &LaneIdxOp = I.getOperand(2);
1993 assert(LaneIdxOp.isReg() && "Lane index operand was not a register?");
1994
1995 if (RBI.getRegBank(DstReg, MRI, TRI)->getID() != AArch64::FPRRegBankID) {
1996 LLVM_DEBUG(dbgs() << "Cannot extract into GPR.\n");
1997 return false;
1998 }
1999
Jessica Paquettebb1aced2019-03-13 21:19:29 +00002000 // Find the index to extract from.
2001 unsigned LaneIdx = 0;
2002 if (!getConstantValueForReg(LaneIdxOp.getReg(), MRI, LaneIdx))
Jessica Paquette607774c2019-03-11 22:18:01 +00002003 return false;
Jessica Paquette607774c2019-03-11 22:18:01 +00002004
Jessica Paquette607774c2019-03-11 22:18:01 +00002005 MachineIRBuilder MIRBuilder(I);
2006
Amara Emersond61b89b2019-03-14 22:48:18 +00002007 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
2008 MachineInstr *Extract = emitExtractVectorElt(DstReg, DstRB, NarrowTy, SrcReg,
2009 LaneIdx, MIRBuilder);
2010 if (!Extract)
2011 return false;
2012
2013 I.eraseFromParent();
2014 return true;
2015}
2016
2017bool AArch64InstructionSelector::selectSplitVectorUnmerge(
2018 MachineInstr &I, MachineRegisterInfo &MRI) const {
2019 unsigned NumElts = I.getNumOperands() - 1;
2020 unsigned SrcReg = I.getOperand(NumElts).getReg();
2021 const LLT NarrowTy = MRI.getType(I.getOperand(0).getReg());
2022 const LLT SrcTy = MRI.getType(SrcReg);
2023
2024 assert(NarrowTy.isVector() && "Expected an unmerge into vectors");
2025 if (SrcTy.getSizeInBits() > 128) {
2026 LLVM_DEBUG(dbgs() << "Unexpected vector type for vec split unmerge");
2027 return false;
Jessica Paquette607774c2019-03-11 22:18:01 +00002028 }
2029
Amara Emersond61b89b2019-03-14 22:48:18 +00002030 MachineIRBuilder MIB(I);
2031
2032 // We implement a split vector operation by treating the sub-vectors as
2033 // scalars and extracting them.
2034 const RegisterBank &DstRB =
2035 *RBI.getRegBank(I.getOperand(0).getReg(), MRI, TRI);
2036 for (unsigned OpIdx = 0; OpIdx < NumElts; ++OpIdx) {
2037 unsigned Dst = I.getOperand(OpIdx).getReg();
2038 MachineInstr *Extract =
2039 emitExtractVectorElt(Dst, DstRB, NarrowTy, SrcReg, OpIdx, MIB);
2040 if (!Extract)
Jessica Paquette607774c2019-03-11 22:18:01 +00002041 return false;
Jessica Paquette607774c2019-03-11 22:18:01 +00002042 }
Jessica Paquette607774c2019-03-11 22:18:01 +00002043 I.eraseFromParent();
2044 return true;
2045}
2046
Jessica Paquette245047d2019-01-24 22:00:41 +00002047bool AArch64InstructionSelector::selectUnmergeValues(
2048 MachineInstr &I, MachineRegisterInfo &MRI) const {
2049 assert(I.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2050 "unexpected opcode");
2051
2052 // TODO: Handle unmerging into GPRs and from scalars to scalars.
2053 if (RBI.getRegBank(I.getOperand(0).getReg(), MRI, TRI)->getID() !=
2054 AArch64::FPRRegBankID ||
2055 RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI)->getID() !=
2056 AArch64::FPRRegBankID) {
2057 LLVM_DEBUG(dbgs() << "Unmerging vector-to-gpr and scalar-to-scalar "
2058 "currently unsupported.\n");
2059 return false;
2060 }
2061
2062 // The last operand is the vector source register, and every other operand is
2063 // a register to unpack into.
2064 unsigned NumElts = I.getNumOperands() - 1;
2065 unsigned SrcReg = I.getOperand(NumElts).getReg();
2066 const LLT NarrowTy = MRI.getType(I.getOperand(0).getReg());
2067 const LLT WideTy = MRI.getType(SrcReg);
Benjamin Kramer653020d2019-01-24 23:45:07 +00002068 (void)WideTy;
Jessica Paquette245047d2019-01-24 22:00:41 +00002069 assert(WideTy.isVector() && "can only unmerge from vector types!");
2070 assert(WideTy.getSizeInBits() > NarrowTy.getSizeInBits() &&
2071 "source register size too small!");
2072
Amara Emersond61b89b2019-03-14 22:48:18 +00002073 if (!NarrowTy.isScalar())
2074 return selectSplitVectorUnmerge(I, MRI);
Jessica Paquette245047d2019-01-24 22:00:41 +00002075
2076 // Choose a lane copy opcode and subregister based off of the size of the
2077 // vector's elements.
2078 unsigned CopyOpc = 0;
2079 unsigned ExtractSubReg = 0;
Jessica Paquette607774c2019-03-11 22:18:01 +00002080 if (!getLaneCopyOpcode(CopyOpc, ExtractSubReg, NarrowTy.getSizeInBits()))
Jessica Paquette245047d2019-01-24 22:00:41 +00002081 return false;
Jessica Paquette245047d2019-01-24 22:00:41 +00002082
2083 // Set up for the lane copies.
2084 MachineBasicBlock &MBB = *I.getParent();
2085
2086 // Stores the registers we'll be copying from.
2087 SmallVector<unsigned, 4> InsertRegs;
2088
2089 // We'll use the first register twice, so we only need NumElts-1 registers.
2090 unsigned NumInsertRegs = NumElts - 1;
2091
2092 // If our elements fit into exactly 128 bits, then we can copy from the source
2093 // directly. Otherwise, we need to do a bit of setup with some subregister
2094 // inserts.
2095 if (NarrowTy.getSizeInBits() * NumElts == 128) {
2096 InsertRegs = SmallVector<unsigned, 4>(NumInsertRegs, SrcReg);
2097 } else {
2098 // No. We have to perform subregister inserts. For each insert, create an
2099 // implicit def and a subregister insert, and save the register we create.
2100 for (unsigned Idx = 0; Idx < NumInsertRegs; ++Idx) {
2101 unsigned ImpDefReg = MRI.createVirtualRegister(&AArch64::FPR128RegClass);
2102 MachineInstr &ImpDefMI =
2103 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(TargetOpcode::IMPLICIT_DEF),
2104 ImpDefReg);
2105
2106 // Now, create the subregister insert from SrcReg.
2107 unsigned InsertReg = MRI.createVirtualRegister(&AArch64::FPR128RegClass);
2108 MachineInstr &InsMI =
2109 *BuildMI(MBB, I, I.getDebugLoc(),
2110 TII.get(TargetOpcode::INSERT_SUBREG), InsertReg)
2111 .addUse(ImpDefReg)
2112 .addUse(SrcReg)
2113 .addImm(AArch64::dsub);
2114
2115 constrainSelectedInstRegOperands(ImpDefMI, TII, TRI, RBI);
2116 constrainSelectedInstRegOperands(InsMI, TII, TRI, RBI);
2117
2118 // Save the register so that we can copy from it after.
2119 InsertRegs.push_back(InsertReg);
2120 }
2121 }
2122
2123 // Now that we've created any necessary subregister inserts, we can
2124 // create the copies.
2125 //
2126 // Perform the first copy separately as a subregister copy.
2127 unsigned CopyTo = I.getOperand(0).getReg();
2128 MachineInstr &FirstCopy =
2129 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(TargetOpcode::COPY), CopyTo)
2130 .addUse(InsertRegs[0], 0, ExtractSubReg);
2131 constrainSelectedInstRegOperands(FirstCopy, TII, TRI, RBI);
2132
2133 // Now, perform the remaining copies as vector lane copies.
2134 unsigned LaneIdx = 1;
2135 for (unsigned InsReg : InsertRegs) {
2136 unsigned CopyTo = I.getOperand(LaneIdx).getReg();
2137 MachineInstr &CopyInst =
2138 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CopyOpc), CopyTo)
2139 .addUse(InsReg)
2140 .addImm(LaneIdx);
2141 constrainSelectedInstRegOperands(CopyInst, TII, TRI, RBI);
2142 ++LaneIdx;
2143 }
2144
2145 // Separately constrain the first copy's destination. Because of the
2146 // limitation in constrainOperandRegClass, we can't guarantee that this will
2147 // actually be constrained. So, do it ourselves using the second operand.
2148 const TargetRegisterClass *RC =
2149 MRI.getRegClassOrNull(I.getOperand(1).getReg());
2150 if (!RC) {
2151 LLVM_DEBUG(dbgs() << "Couldn't constrain copy destination.\n");
2152 return false;
2153 }
2154
2155 RBI.constrainGenericRegister(CopyTo, *RC, MRI);
2156 I.eraseFromParent();
2157 return true;
2158}
2159
Amara Emerson2ff22982019-03-14 22:48:15 +00002160bool AArch64InstructionSelector::selectConcatVectors(
2161 MachineInstr &I, MachineRegisterInfo &MRI) const {
2162 assert(I.getOpcode() == TargetOpcode::G_CONCAT_VECTORS &&
2163 "Unexpected opcode");
2164 unsigned Dst = I.getOperand(0).getReg();
2165 unsigned Op1 = I.getOperand(1).getReg();
2166 unsigned Op2 = I.getOperand(2).getReg();
2167 MachineIRBuilder MIRBuilder(I);
2168 MachineInstr *ConcatMI = emitVectorConcat(Dst, Op1, Op2, MIRBuilder);
2169 if (!ConcatMI)
2170 return false;
2171 I.eraseFromParent();
2172 return true;
2173}
2174
Amara Emerson1abe05c2019-02-21 20:20:16 +00002175void AArch64InstructionSelector::collectShuffleMaskIndices(
2176 MachineInstr &I, MachineRegisterInfo &MRI,
2177 SmallVectorImpl<int> &Idxs) const {
2178 MachineInstr *MaskDef = MRI.getVRegDef(I.getOperand(3).getReg());
2179 assert(
2180 MaskDef->getOpcode() == TargetOpcode::G_BUILD_VECTOR &&
2181 "G_SHUFFLE_VECTOR should have a constant mask operand as G_BUILD_VECTOR");
2182 // Find the constant indices.
2183 for (unsigned i = 1, e = MaskDef->getNumOperands(); i < e; ++i) {
2184 MachineInstr *ScalarDef = MRI.getVRegDef(MaskDef->getOperand(i).getReg());
2185 assert(ScalarDef && "Could not find vreg def of shufflevec index op");
2186 // Look through copies.
2187 while (ScalarDef->getOpcode() == TargetOpcode::COPY) {
2188 ScalarDef = MRI.getVRegDef(ScalarDef->getOperand(1).getReg());
2189 assert(ScalarDef && "Could not find def of copy operand");
2190 }
2191 assert(ScalarDef->getOpcode() == TargetOpcode::G_CONSTANT);
2192 Idxs.push_back(ScalarDef->getOperand(1).getCImm()->getSExtValue());
2193 }
2194}
2195
2196unsigned
2197AArch64InstructionSelector::emitConstantPoolEntry(Constant *CPVal,
2198 MachineFunction &MF) const {
2199 Type *CPTy = CPVal->getType()->getPointerTo();
2200 unsigned Align = MF.getDataLayout().getPrefTypeAlignment(CPTy);
2201 if (Align == 0)
2202 Align = MF.getDataLayout().getTypeAllocSize(CPTy);
2203
2204 MachineConstantPool *MCP = MF.getConstantPool();
2205 return MCP->getConstantPoolIndex(CPVal, Align);
2206}
2207
2208MachineInstr *AArch64InstructionSelector::emitLoadFromConstantPool(
2209 Constant *CPVal, MachineIRBuilder &MIRBuilder) const {
2210 unsigned CPIdx = emitConstantPoolEntry(CPVal, MIRBuilder.getMF());
2211
2212 auto Adrp =
2213 MIRBuilder.buildInstr(AArch64::ADRP, {&AArch64::GPR64RegClass}, {})
2214 .addConstantPoolIndex(CPIdx, 0, AArch64II::MO_PAGE);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002215
2216 MachineInstr *LoadMI = nullptr;
2217 switch (MIRBuilder.getDataLayout().getTypeStoreSize(CPVal->getType())) {
2218 case 16:
2219 LoadMI =
2220 &*MIRBuilder
2221 .buildInstr(AArch64::LDRQui, {&AArch64::FPR128RegClass}, {Adrp})
2222 .addConstantPoolIndex(CPIdx, 0,
2223 AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2224 break;
2225 case 8:
2226 LoadMI = &*MIRBuilder
2227 .buildInstr(AArch64::LDRDui, {&AArch64::FPR64RegClass}, {Adrp})
2228 .addConstantPoolIndex(
2229 CPIdx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2230 break;
2231 default:
2232 LLVM_DEBUG(dbgs() << "Could not load from constant pool of type "
2233 << *CPVal->getType());
2234 return nullptr;
2235 }
Amara Emerson1abe05c2019-02-21 20:20:16 +00002236 constrainSelectedInstRegOperands(*Adrp, TII, TRI, RBI);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002237 constrainSelectedInstRegOperands(*LoadMI, TII, TRI, RBI);
2238 return LoadMI;
2239}
2240
2241/// Return an <Opcode, SubregIndex> pair to do an vector elt insert of a given
2242/// size and RB.
2243static std::pair<unsigned, unsigned>
2244getInsertVecEltOpInfo(const RegisterBank &RB, unsigned EltSize) {
2245 unsigned Opc, SubregIdx;
2246 if (RB.getID() == AArch64::GPRRegBankID) {
2247 if (EltSize == 32) {
2248 Opc = AArch64::INSvi32gpr;
2249 SubregIdx = AArch64::ssub;
2250 } else if (EltSize == 64) {
2251 Opc = AArch64::INSvi64gpr;
2252 SubregIdx = AArch64::dsub;
2253 } else {
2254 llvm_unreachable("invalid elt size!");
2255 }
2256 } else {
2257 if (EltSize == 8) {
2258 Opc = AArch64::INSvi8lane;
2259 SubregIdx = AArch64::bsub;
2260 } else if (EltSize == 16) {
2261 Opc = AArch64::INSvi16lane;
2262 SubregIdx = AArch64::hsub;
2263 } else if (EltSize == 32) {
2264 Opc = AArch64::INSvi32lane;
2265 SubregIdx = AArch64::ssub;
2266 } else if (EltSize == 64) {
2267 Opc = AArch64::INSvi64lane;
2268 SubregIdx = AArch64::dsub;
2269 } else {
2270 llvm_unreachable("invalid elt size!");
2271 }
2272 }
2273 return std::make_pair(Opc, SubregIdx);
2274}
2275
2276MachineInstr *AArch64InstructionSelector::emitVectorConcat(
Amara Emerson2ff22982019-03-14 22:48:15 +00002277 Optional<unsigned> Dst, unsigned Op1, unsigned Op2,
2278 MachineIRBuilder &MIRBuilder) const {
Amara Emerson8acb0d92019-03-04 19:16:00 +00002279 // We implement a vector concat by:
2280 // 1. Use scalar_to_vector to insert the lower vector into the larger dest
2281 // 2. Insert the upper vector into the destination's upper element
2282 // TODO: some of this code is common with G_BUILD_VECTOR handling.
2283 MachineRegisterInfo &MRI = MIRBuilder.getMF().getRegInfo();
2284
2285 const LLT Op1Ty = MRI.getType(Op1);
2286 const LLT Op2Ty = MRI.getType(Op2);
2287
2288 if (Op1Ty != Op2Ty) {
2289 LLVM_DEBUG(dbgs() << "Could not do vector concat of differing vector tys");
2290 return nullptr;
2291 }
2292 assert(Op1Ty.isVector() && "Expected a vector for vector concat");
2293
2294 if (Op1Ty.getSizeInBits() >= 128) {
2295 LLVM_DEBUG(dbgs() << "Vector concat not supported for full size vectors");
2296 return nullptr;
2297 }
2298
2299 // At the moment we just support 64 bit vector concats.
2300 if (Op1Ty.getSizeInBits() != 64) {
2301 LLVM_DEBUG(dbgs() << "Vector concat supported for 64b vectors");
2302 return nullptr;
2303 }
2304
2305 const LLT ScalarTy = LLT::scalar(Op1Ty.getSizeInBits());
2306 const RegisterBank &FPRBank = *RBI.getRegBank(Op1, MRI, TRI);
2307 const TargetRegisterClass *DstRC =
2308 getMinClassForRegBank(FPRBank, Op1Ty.getSizeInBits() * 2);
2309
2310 MachineInstr *WidenedOp1 =
2311 emitScalarToVector(ScalarTy.getSizeInBits(), DstRC, Op1, MIRBuilder);
2312 MachineInstr *WidenedOp2 =
2313 emitScalarToVector(ScalarTy.getSizeInBits(), DstRC, Op2, MIRBuilder);
2314 if (!WidenedOp1 || !WidenedOp2) {
2315 LLVM_DEBUG(dbgs() << "Could not emit a vector from scalar value");
2316 return nullptr;
2317 }
2318
2319 // Now do the insert of the upper element.
2320 unsigned InsertOpc, InsSubRegIdx;
2321 std::tie(InsertOpc, InsSubRegIdx) =
2322 getInsertVecEltOpInfo(FPRBank, ScalarTy.getSizeInBits());
2323
Amara Emerson2ff22982019-03-14 22:48:15 +00002324 if (!Dst)
2325 Dst = MRI.createVirtualRegister(DstRC);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002326 auto InsElt =
2327 MIRBuilder
Amara Emerson2ff22982019-03-14 22:48:15 +00002328 .buildInstr(InsertOpc, {*Dst}, {WidenedOp1->getOperand(0).getReg()})
Amara Emerson8acb0d92019-03-04 19:16:00 +00002329 .addImm(1) /* Lane index */
2330 .addUse(WidenedOp2->getOperand(0).getReg())
2331 .addImm(0);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002332 constrainSelectedInstRegOperands(*InsElt, TII, TRI, RBI);
2333 return &*InsElt;
Amara Emerson1abe05c2019-02-21 20:20:16 +00002334}
2335
2336bool AArch64InstructionSelector::selectShuffleVector(
2337 MachineInstr &I, MachineRegisterInfo &MRI) const {
2338 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
2339 unsigned Src1Reg = I.getOperand(1).getReg();
2340 const LLT Src1Ty = MRI.getType(Src1Reg);
2341 unsigned Src2Reg = I.getOperand(2).getReg();
2342 const LLT Src2Ty = MRI.getType(Src2Reg);
2343
2344 MachineBasicBlock &MBB = *I.getParent();
2345 MachineFunction &MF = *MBB.getParent();
2346 LLVMContext &Ctx = MF.getFunction().getContext();
2347
2348 // G_SHUFFLE_VECTOR doesn't really have a strictly enforced constant mask
2349 // operand, it comes in as a normal vector value which we have to analyze to
2350 // find the mask indices.
2351 SmallVector<int, 8> Mask;
2352 collectShuffleMaskIndices(I, MRI, Mask);
2353 assert(!Mask.empty() && "Expected to find mask indices");
2354
2355 // G_SHUFFLE_VECTOR is weird in that the source operands can be scalars, if
2356 // it's originated from a <1 x T> type. Those should have been lowered into
2357 // G_BUILD_VECTOR earlier.
2358 if (!Src1Ty.isVector() || !Src2Ty.isVector()) {
2359 LLVM_DEBUG(dbgs() << "Could not select a \"scalar\" G_SHUFFLE_VECTOR\n");
2360 return false;
2361 }
2362
2363 unsigned BytesPerElt = DstTy.getElementType().getSizeInBits() / 8;
2364
2365 SmallVector<Constant *, 64> CstIdxs;
2366 for (int Val : Mask) {
2367 for (unsigned Byte = 0; Byte < BytesPerElt; ++Byte) {
2368 unsigned Offset = Byte + Val * BytesPerElt;
2369 CstIdxs.emplace_back(ConstantInt::get(Type::getInt8Ty(Ctx), Offset));
2370 }
2371 }
2372
Amara Emerson8acb0d92019-03-04 19:16:00 +00002373 MachineIRBuilder MIRBuilder(I);
Amara Emerson1abe05c2019-02-21 20:20:16 +00002374
2375 // Use a constant pool to load the index vector for TBL.
2376 Constant *CPVal = ConstantVector::get(CstIdxs);
Amara Emerson1abe05c2019-02-21 20:20:16 +00002377 MachineInstr *IndexLoad = emitLoadFromConstantPool(CPVal, MIRBuilder);
2378 if (!IndexLoad) {
2379 LLVM_DEBUG(dbgs() << "Could not load from a constant pool");
2380 return false;
2381 }
2382
Amara Emerson8acb0d92019-03-04 19:16:00 +00002383 if (DstTy.getSizeInBits() != 128) {
2384 assert(DstTy.getSizeInBits() == 64 && "Unexpected shuffle result ty");
2385 // This case can be done with TBL1.
Amara Emerson2ff22982019-03-14 22:48:15 +00002386 MachineInstr *Concat = emitVectorConcat(None, Src1Reg, Src2Reg, MIRBuilder);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002387 if (!Concat) {
2388 LLVM_DEBUG(dbgs() << "Could not do vector concat for tbl1");
2389 return false;
2390 }
2391
2392 // The constant pool load will be 64 bits, so need to convert to FPR128 reg.
2393 IndexLoad =
2394 emitScalarToVector(64, &AArch64::FPR128RegClass,
2395 IndexLoad->getOperand(0).getReg(), MIRBuilder);
2396
2397 auto TBL1 = MIRBuilder.buildInstr(
2398 AArch64::TBLv16i8One, {&AArch64::FPR128RegClass},
2399 {Concat->getOperand(0).getReg(), IndexLoad->getOperand(0).getReg()});
2400 constrainSelectedInstRegOperands(*TBL1, TII, TRI, RBI);
2401
2402 auto Copy = BuildMI(*I.getParent(), I, I.getDebugLoc(),
2403 TII.get(TargetOpcode::COPY), I.getOperand(0).getReg())
2404 .addUse(TBL1->getOperand(0).getReg(), 0, AArch64::dsub);
2405 RBI.constrainGenericRegister(Copy.getReg(0), AArch64::FPR64RegClass, MRI);
2406 I.eraseFromParent();
2407 return true;
2408 }
2409
Amara Emerson1abe05c2019-02-21 20:20:16 +00002410 // For TBL2 we need to emit a REG_SEQUENCE to tie together two consecutive
2411 // Q registers for regalloc.
2412 auto RegSeq = MIRBuilder
2413 .buildInstr(TargetOpcode::REG_SEQUENCE,
2414 {&AArch64::QQRegClass}, {Src1Reg})
2415 .addImm(AArch64::qsub0)
2416 .addUse(Src2Reg)
2417 .addImm(AArch64::qsub1);
2418
2419 auto TBL2 =
2420 MIRBuilder.buildInstr(AArch64::TBLv16i8Two, {I.getOperand(0).getReg()},
2421 {RegSeq, IndexLoad->getOperand(0).getReg()});
2422 constrainSelectedInstRegOperands(*RegSeq, TII, TRI, RBI);
2423 constrainSelectedInstRegOperands(*TBL2, TII, TRI, RBI);
2424 I.eraseFromParent();
2425 return true;
2426}
2427
Jessica Paquette16d67a32019-03-13 23:22:23 +00002428MachineInstr *AArch64InstructionSelector::emitLaneInsert(
2429 Optional<unsigned> DstReg, unsigned SrcReg, unsigned EltReg,
2430 unsigned LaneIdx, const RegisterBank &RB,
2431 MachineIRBuilder &MIRBuilder) const {
2432 MachineInstr *InsElt = nullptr;
2433 const TargetRegisterClass *DstRC = &AArch64::FPR128RegClass;
2434 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
2435
2436 // Create a register to define with the insert if one wasn't passed in.
2437 if (!DstReg)
2438 DstReg = MRI.createVirtualRegister(DstRC);
2439
2440 unsigned EltSize = MRI.getType(EltReg).getSizeInBits();
2441 unsigned Opc = getInsertVecEltOpInfo(RB, EltSize).first;
2442
2443 if (RB.getID() == AArch64::FPRRegBankID) {
2444 auto InsSub = emitScalarToVector(EltSize, DstRC, EltReg, MIRBuilder);
2445 InsElt = MIRBuilder.buildInstr(Opc, {*DstReg}, {SrcReg})
2446 .addImm(LaneIdx)
2447 .addUse(InsSub->getOperand(0).getReg())
2448 .addImm(0);
2449 } else {
2450 InsElt = MIRBuilder.buildInstr(Opc, {*DstReg}, {SrcReg})
2451 .addImm(LaneIdx)
2452 .addUse(EltReg);
2453 }
2454
2455 constrainSelectedInstRegOperands(*InsElt, TII, TRI, RBI);
2456 return InsElt;
2457}
2458
Jessica Paquette5aff1f42019-03-14 18:01:30 +00002459bool AArch64InstructionSelector::selectInsertElt(
2460 MachineInstr &I, MachineRegisterInfo &MRI) const {
2461 assert(I.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT);
2462
2463 // Get information on the destination.
2464 unsigned DstReg = I.getOperand(0).getReg();
2465 const LLT DstTy = MRI.getType(DstReg);
2466 if (DstTy.getSizeInBits() < 128) {
2467 // TODO: Handle unpacked vectors.
2468 LLVM_DEBUG(dbgs() << "Unpacked vectors not supported yet!");
2469 return false;
2470 }
2471
2472 // Get information on the element we want to insert into the destination.
2473 unsigned EltReg = I.getOperand(2).getReg();
2474 const LLT EltTy = MRI.getType(EltReg);
2475 unsigned EltSize = EltTy.getSizeInBits();
2476 if (EltSize < 16 || EltSize > 64)
2477 return false; // Don't support all element types yet.
2478
2479 // Find the definition of the index. Bail out if it's not defined by a
2480 // G_CONSTANT.
2481 unsigned IdxReg = I.getOperand(3).getReg();
2482 unsigned LaneIdx = 0;
2483 if (!getConstantValueForReg(IdxReg, MRI, LaneIdx))
2484 return false;
2485
2486 // Perform the lane insert.
2487 unsigned SrcReg = I.getOperand(1).getReg();
2488 const RegisterBank &EltRB = *RBI.getRegBank(EltReg, MRI, TRI);
2489 MachineIRBuilder MIRBuilder(I);
2490 emitLaneInsert(DstReg, SrcReg, EltReg, LaneIdx, EltRB, MIRBuilder);
2491 I.eraseFromParent();
2492 return true;
2493}
2494
Amara Emerson5ec14602018-12-10 18:44:58 +00002495bool AArch64InstructionSelector::selectBuildVector(
2496 MachineInstr &I, MachineRegisterInfo &MRI) const {
2497 assert(I.getOpcode() == TargetOpcode::G_BUILD_VECTOR);
2498 // Until we port more of the optimized selections, for now just use a vector
2499 // insert sequence.
2500 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
2501 const LLT EltTy = MRI.getType(I.getOperand(1).getReg());
2502 unsigned EltSize = EltTy.getSizeInBits();
Jessica Paquette245047d2019-01-24 22:00:41 +00002503 if (EltSize < 16 || EltSize > 64)
Amara Emerson5ec14602018-12-10 18:44:58 +00002504 return false; // Don't support all element types yet.
2505 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002506 MachineIRBuilder MIRBuilder(I);
Jessica Paquette245047d2019-01-24 22:00:41 +00002507
2508 const TargetRegisterClass *DstRC = &AArch64::FPR128RegClass;
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002509 MachineInstr *ScalarToVec =
Amara Emerson8acb0d92019-03-04 19:16:00 +00002510 emitScalarToVector(DstTy.getElementType().getSizeInBits(), DstRC,
2511 I.getOperand(1).getReg(), MIRBuilder);
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002512 if (!ScalarToVec)
Jessica Paquette245047d2019-01-24 22:00:41 +00002513 return false;
2514
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002515 unsigned DstVec = ScalarToVec->getOperand(0).getReg();
Jessica Paquette245047d2019-01-24 22:00:41 +00002516 unsigned DstSize = DstTy.getSizeInBits();
2517
2518 // Keep track of the last MI we inserted. Later on, we might be able to save
2519 // a copy using it.
2520 MachineInstr *PrevMI = nullptr;
2521 for (unsigned i = 2, e = DstSize / EltSize + 1; i < e; ++i) {
Jessica Paquette16d67a32019-03-13 23:22:23 +00002522 // Note that if we don't do a subregister copy, we can end up making an
2523 // extra register.
2524 PrevMI = &*emitLaneInsert(None, DstVec, I.getOperand(i).getReg(), i - 1, RB,
2525 MIRBuilder);
2526 DstVec = PrevMI->getOperand(0).getReg();
Amara Emerson5ec14602018-12-10 18:44:58 +00002527 }
Jessica Paquette245047d2019-01-24 22:00:41 +00002528
2529 // If DstTy's size in bits is less than 128, then emit a subregister copy
2530 // from DstVec to the last register we've defined.
2531 if (DstSize < 128) {
Jessica Paquette85ace622019-03-13 23:29:54 +00002532 // Force this to be FPR using the destination vector.
2533 const TargetRegisterClass *RC =
2534 getMinClassForRegBank(*RBI.getRegBank(DstVec, MRI, TRI), DstSize);
Jessica Paquette245047d2019-01-24 22:00:41 +00002535 if (!RC)
2536 return false;
Jessica Paquette85ace622019-03-13 23:29:54 +00002537 if (RC != &AArch64::FPR32RegClass && RC != &AArch64::FPR64RegClass) {
2538 LLVM_DEBUG(dbgs() << "Unsupported register class!\n");
2539 return false;
2540 }
2541
2542 unsigned SubReg = 0;
2543 if (!getSubRegForClass(RC, TRI, SubReg))
2544 return false;
2545 if (SubReg != AArch64::ssub && SubReg != AArch64::dsub) {
2546 LLVM_DEBUG(dbgs() << "Unsupported destination size! (" << DstSize
2547 << "\n");
2548 return false;
2549 }
Jessica Paquette245047d2019-01-24 22:00:41 +00002550
2551 unsigned Reg = MRI.createVirtualRegister(RC);
2552 unsigned DstReg = I.getOperand(0).getReg();
2553
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002554 // MIRBuilder doesn't let us create uses with subregs & flags, so use
2555 // BuildMI here instead.
Jessica Paquette245047d2019-01-24 22:00:41 +00002556 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
2557 DstReg)
2558 .addUse(DstVec, 0, SubReg);
2559 MachineOperand &RegOp = I.getOperand(1);
2560 RegOp.setReg(Reg);
2561 RBI.constrainGenericRegister(DstReg, *RC, MRI);
2562 } else {
2563 // We don't need a subregister copy. Save a copy by re-using the
2564 // destination register on the final insert.
2565 assert(PrevMI && "PrevMI was null?");
2566 PrevMI->getOperand(0).setReg(I.getOperand(0).getReg());
2567 constrainSelectedInstRegOperands(*PrevMI, TII, TRI, RBI);
2568 }
2569
Amara Emerson5ec14602018-12-10 18:44:58 +00002570 I.eraseFromParent();
2571 return true;
2572}
2573
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002574/// SelectArithImmed - Select an immediate value that can be represented as
2575/// a 12-bit value shifted left by either 0 or 12. If so, return true with
2576/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002577InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00002578AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002579 MachineInstr &MI = *Root.getParent();
2580 MachineBasicBlock &MBB = *MI.getParent();
2581 MachineFunction &MF = *MBB.getParent();
2582 MachineRegisterInfo &MRI = MF.getRegInfo();
2583
2584 // This function is called from the addsub_shifted_imm ComplexPattern,
2585 // which lists [imm] as the list of opcode it's interested in, however
2586 // we still need to check whether the operand is actually an immediate
2587 // here because the ComplexPattern opcode list is only used in
2588 // root-level opcode matching.
2589 uint64_t Immed;
2590 if (Root.isImm())
2591 Immed = Root.getImm();
2592 else if (Root.isCImm())
2593 Immed = Root.getCImm()->getZExtValue();
2594 else if (Root.isReg()) {
2595 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
2596 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002597 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00002598 MachineOperand &Op1 = Def->getOperand(1);
2599 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002600 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00002601 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002602 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002603 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002604
2605 unsigned ShiftAmt;
2606
2607 if (Immed >> 12 == 0) {
2608 ShiftAmt = 0;
2609 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
2610 ShiftAmt = 12;
2611 Immed = Immed >> 12;
2612 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002613 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002614
2615 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002616 return {{
2617 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
2618 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
2619 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002620}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00002621
Daniel Sandersea8711b2017-10-16 03:36:29 +00002622/// Select a "register plus unscaled signed 9-bit immediate" address. This
2623/// should only match when there is an offset that is not valid for a scaled
2624/// immediate addressing mode. The "Size" argument is the size in bytes of the
2625/// memory reference, which is needed here to know what is valid for a scaled
2626/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002627InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00002628AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
2629 unsigned Size) const {
2630 MachineRegisterInfo &MRI =
2631 Root.getParent()->getParent()->getParent()->getRegInfo();
2632
2633 if (!Root.isReg())
2634 return None;
2635
2636 if (!isBaseWithConstantOffset(Root, MRI))
2637 return None;
2638
2639 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
2640 if (!RootDef)
2641 return None;
2642
2643 MachineOperand &OffImm = RootDef->getOperand(2);
2644 if (!OffImm.isReg())
2645 return None;
2646 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
2647 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
2648 return None;
2649 int64_t RHSC;
2650 MachineOperand &RHSOp1 = RHS->getOperand(1);
2651 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
2652 return None;
2653 RHSC = RHSOp1.getCImm()->getSExtValue();
2654
2655 // If the offset is valid as a scaled immediate, don't match here.
2656 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
2657 return None;
2658 if (RHSC >= -256 && RHSC < 256) {
2659 MachineOperand &Base = RootDef->getOperand(1);
2660 return {{
2661 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
2662 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
2663 }};
2664 }
2665 return None;
2666}
2667
2668/// Select a "register plus scaled unsigned 12-bit immediate" address. The
2669/// "Size" argument is the size in bytes of the memory reference, which
2670/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002671InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00002672AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
2673 unsigned Size) const {
2674 MachineRegisterInfo &MRI =
2675 Root.getParent()->getParent()->getParent()->getRegInfo();
2676
2677 if (!Root.isReg())
2678 return None;
2679
2680 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
2681 if (!RootDef)
2682 return None;
2683
2684 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
2685 return {{
2686 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
2687 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
2688 }};
2689 }
2690
2691 if (isBaseWithConstantOffset(Root, MRI)) {
2692 MachineOperand &LHS = RootDef->getOperand(1);
2693 MachineOperand &RHS = RootDef->getOperand(2);
2694 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
2695 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
2696 if (LHSDef && RHSDef) {
2697 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
2698 unsigned Scale = Log2_32(Size);
2699 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
2700 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00002701 return {{
2702 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
2703 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
2704 }};
2705
Daniel Sandersea8711b2017-10-16 03:36:29 +00002706 return {{
2707 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
2708 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
2709 }};
2710 }
2711 }
2712 }
2713
2714 // Before falling back to our general case, check if the unscaled
2715 // instructions can handle this. If so, that's preferable.
2716 if (selectAddrModeUnscaled(Root, Size).hasValue())
2717 return None;
2718
2719 return {{
2720 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
2721 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
2722 }};
2723}
2724
Volkan Kelesf7f25682018-01-16 18:44:05 +00002725void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
2726 const MachineInstr &MI) const {
2727 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2728 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
2729 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
2730 assert(CstVal && "Expected constant value");
2731 MIB.addImm(CstVal.getValue());
2732}
2733
Daniel Sanders0b5293f2017-04-06 09:49:34 +00002734namespace llvm {
2735InstructionSelector *
2736createAArch64InstructionSelector(const AArch64TargetMachine &TM,
2737 AArch64Subtarget &Subtarget,
2738 AArch64RegisterBankInfo &RBI) {
2739 return new AArch64InstructionSelector(TM, Subtarget, RBI);
2740}
2741}