blob: a2eb920571a0dae64655bfc5c76b20e6cca6af40 [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
Tim Northover398c5f52017-02-14 20:56:29 +00001336 case TargetOpcode::G_PTR_MASK: {
1337 uint64_t Align = I.getOperand(2).getImm();
1338 if (Align >= 64 || Align == 0)
1339 return false;
1340
1341 uint64_t Mask = ~((1ULL << Align) - 1);
1342 I.setDesc(TII.get(AArch64::ANDXri));
1343 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1344
1345 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1346 }
Tim Northover037af52c2016-10-31 18:31:09 +00001347 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001348 case TargetOpcode::G_TRUNC: {
1349 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1350 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1351
1352 const unsigned DstReg = I.getOperand(0).getReg();
1353 const unsigned SrcReg = I.getOperand(1).getReg();
1354
1355 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1356 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1357
1358 if (DstRB.getID() != SrcRB.getID()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001359 LLVM_DEBUG(
1360 dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001361 return false;
1362 }
1363
1364 if (DstRB.getID() == AArch64::GPRRegBankID) {
1365 const TargetRegisterClass *DstRC =
1366 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1367 if (!DstRC)
1368 return false;
1369
1370 const TargetRegisterClass *SrcRC =
1371 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1372 if (!SrcRC)
1373 return false;
1374
1375 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1376 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001377 LLVM_DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001378 return false;
1379 }
1380
1381 if (DstRC == SrcRC) {
1382 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001383 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1384 SrcTy == LLT::scalar(64)) {
1385 llvm_unreachable("TableGen can import this case");
1386 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001387 } else if (DstRC == &AArch64::GPR32RegClass &&
1388 SrcRC == &AArch64::GPR64RegClass) {
1389 I.getOperand(1).setSubReg(AArch64::sub_32);
1390 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001391 LLVM_DEBUG(
1392 dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001393 return false;
1394 }
1395
1396 I.setDesc(TII.get(TargetOpcode::COPY));
1397 return true;
1398 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1399 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1400 I.setDesc(TII.get(AArch64::XTNv4i16));
1401 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1402 return true;
1403 }
1404 }
1405
1406 return false;
1407 }
1408
Tim Northover3d38b3a2016-10-11 20:50:21 +00001409 case TargetOpcode::G_ANYEXT: {
1410 const unsigned DstReg = I.getOperand(0).getReg();
1411 const unsigned SrcReg = I.getOperand(1).getReg();
1412
Quentin Colombetcb629a82016-10-12 03:57:49 +00001413 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1414 if (RBDst.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001415 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst
1416 << ", expected: GPR\n");
Quentin Colombetcb629a82016-10-12 03:57:49 +00001417 return false;
1418 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001419
Quentin Colombetcb629a82016-10-12 03:57:49 +00001420 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1421 if (RBSrc.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001422 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc
1423 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001424 return false;
1425 }
1426
1427 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1428
1429 if (DstSize == 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001430 LLVM_DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001431 return false;
1432 }
1433
Quentin Colombetcb629a82016-10-12 03:57:49 +00001434 if (DstSize != 64 && DstSize > 32) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001435 LLVM_DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1436 << ", expected: 32 or 64\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001437 return false;
1438 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001439 // At this point G_ANYEXT is just like a plain COPY, but we need
1440 // to explicitly form the 64-bit value if any.
1441 if (DstSize > 32) {
1442 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1443 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1444 .addDef(ExtSrc)
1445 .addImm(0)
1446 .addUse(SrcReg)
1447 .addImm(AArch64::sub_32);
1448 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001449 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001450 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001451 }
1452
1453 case TargetOpcode::G_ZEXT:
1454 case TargetOpcode::G_SEXT: {
1455 unsigned Opcode = I.getOpcode();
1456 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1457 SrcTy = MRI.getType(I.getOperand(1).getReg());
1458 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1459 const unsigned DefReg = I.getOperand(0).getReg();
1460 const unsigned SrcReg = I.getOperand(1).getReg();
1461 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1462
1463 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001464 LLVM_DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1465 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001466 return false;
1467 }
1468
1469 MachineInstr *ExtI;
1470 if (DstTy == LLT::scalar(64)) {
1471 // FIXME: Can we avoid manually doing this?
1472 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001473 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1474 << " operand\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001475 return false;
1476 }
1477
1478 const unsigned SrcXReg =
1479 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1480 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1481 .addDef(SrcXReg)
1482 .addImm(0)
1483 .addUse(SrcReg)
1484 .addImm(AArch64::sub_32);
1485
1486 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1487 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1488 .addDef(DefReg)
1489 .addUse(SrcXReg)
1490 .addImm(0)
1491 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001492 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001493 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1494 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1495 .addDef(DefReg)
1496 .addUse(SrcReg)
1497 .addImm(0)
1498 .addImm(SrcTy.getSizeInBits() - 1);
1499 } else {
1500 return false;
1501 }
1502
1503 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1504
1505 I.eraseFromParent();
1506 return true;
1507 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001508
Tim Northover69271c62016-10-12 22:49:11 +00001509 case TargetOpcode::G_SITOFP:
1510 case TargetOpcode::G_UITOFP:
1511 case TargetOpcode::G_FPTOSI:
1512 case TargetOpcode::G_FPTOUI: {
1513 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1514 SrcTy = MRI.getType(I.getOperand(1).getReg());
1515 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1516 if (NewOpc == Opcode)
1517 return false;
1518
1519 I.setDesc(TII.get(NewOpc));
1520 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1521
1522 return true;
1523 }
1524
1525
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001526 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001527 // The importer is currently unable to import pointer types since they
1528 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001529 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001530
Daniel Sandersedd07842017-08-17 09:26:14 +00001531 case TargetOpcode::G_BITCAST:
1532 // Imported SelectionDAG rules can handle every bitcast except those that
1533 // bitcast from a type to the same type. Ideally, these shouldn't occur
1534 // but we might not run an optimizer that deletes them.
1535 if (MRI.getType(I.getOperand(0).getReg()) ==
1536 MRI.getType(I.getOperand(1).getReg()))
1537 return selectCopy(I, TII, MRI, TRI, RBI);
1538 return false;
1539
Tim Northover9ac0eba2016-11-08 00:45:29 +00001540 case TargetOpcode::G_SELECT: {
1541 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001542 LLVM_DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1543 << ", expected: " << LLT::scalar(1) << '\n');
Tim Northover9ac0eba2016-11-08 00:45:29 +00001544 return false;
1545 }
1546
1547 const unsigned CondReg = I.getOperand(1).getReg();
1548 const unsigned TReg = I.getOperand(2).getReg();
1549 const unsigned FReg = I.getOperand(3).getReg();
1550
1551 unsigned CSelOpc = 0;
1552
1553 if (Ty == LLT::scalar(32)) {
1554 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001555 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001556 CSelOpc = AArch64::CSELXr;
1557 } else {
1558 return false;
1559 }
1560
1561 MachineInstr &TstMI =
1562 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1563 .addDef(AArch64::WZR)
1564 .addUse(CondReg)
1565 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1566
1567 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1568 .addDef(I.getOperand(0).getReg())
1569 .addUse(TReg)
1570 .addUse(FReg)
1571 .addImm(AArch64CC::NE);
1572
1573 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1574 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1575
1576 I.eraseFromParent();
1577 return true;
1578 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001579 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001580 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001581 LLVM_DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1582 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001583 return false;
1584 }
1585
1586 unsigned CmpOpc = 0;
1587 unsigned ZReg = 0;
1588
1589 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1590 if (CmpTy == LLT::scalar(32)) {
1591 CmpOpc = AArch64::SUBSWrr;
1592 ZReg = AArch64::WZR;
1593 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1594 CmpOpc = AArch64::SUBSXrr;
1595 ZReg = AArch64::XZR;
1596 } else {
1597 return false;
1598 }
1599
Kristof Beyls22524402017-01-05 10:16:08 +00001600 // CSINC increments the result by one when the condition code is false.
1601 // Therefore, we have to invert the predicate to get an increment by 1 when
1602 // the predicate is true.
1603 const AArch64CC::CondCode invCC =
1604 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1605 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001606
1607 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1608 .addDef(ZReg)
1609 .addUse(I.getOperand(2).getReg())
1610 .addUse(I.getOperand(3).getReg());
1611
1612 MachineInstr &CSetMI =
1613 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1614 .addDef(I.getOperand(0).getReg())
1615 .addUse(AArch64::WZR)
1616 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001617 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001618
1619 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1620 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1621
1622 I.eraseFromParent();
1623 return true;
1624 }
1625
Tim Northover7dd378d2016-10-12 22:49:07 +00001626 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001627 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001628 LLVM_DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1629 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001630 return false;
1631 }
1632
1633 unsigned CmpOpc = 0;
1634 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1635 if (CmpTy == LLT::scalar(32)) {
1636 CmpOpc = AArch64::FCMPSrr;
1637 } else if (CmpTy == LLT::scalar(64)) {
1638 CmpOpc = AArch64::FCMPDrr;
1639 } else {
1640 return false;
1641 }
1642
1643 // FIXME: regbank
1644
1645 AArch64CC::CondCode CC1, CC2;
1646 changeFCMPPredToAArch64CC(
1647 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1648
1649 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1650 .addUse(I.getOperand(2).getReg())
1651 .addUse(I.getOperand(3).getReg());
1652
1653 const unsigned DefReg = I.getOperand(0).getReg();
1654 unsigned Def1Reg = DefReg;
1655 if (CC2 != AArch64CC::AL)
1656 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1657
1658 MachineInstr &CSetMI =
1659 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1660 .addDef(Def1Reg)
1661 .addUse(AArch64::WZR)
1662 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001663 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001664
1665 if (CC2 != AArch64CC::AL) {
1666 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1667 MachineInstr &CSet2MI =
1668 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1669 .addDef(Def2Reg)
1670 .addUse(AArch64::WZR)
1671 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001672 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001673 MachineInstr &OrMI =
1674 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1675 .addDef(DefReg)
1676 .addUse(Def1Reg)
1677 .addUse(Def2Reg);
1678 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1679 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1680 }
1681
1682 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1683 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1684
1685 I.eraseFromParent();
1686 return true;
1687 }
Tim Northovere9600d82017-02-08 17:57:27 +00001688 case TargetOpcode::G_VASTART:
1689 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1690 : selectVaStartAAPCS(I, MF, MRI);
Amara Emerson1f5d9942018-04-25 14:43:59 +00001691 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
1692 if (!I.getOperand(0).isIntrinsicID())
1693 return false;
1694 if (I.getOperand(0).getIntrinsicID() != Intrinsic::trap)
1695 return false;
1696 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::BRK))
1697 .addImm(1);
1698 I.eraseFromParent();
1699 return true;
Amara Emerson1e8c1642018-07-31 00:09:02 +00001700 case TargetOpcode::G_IMPLICIT_DEF: {
Justin Bogner4fc69662017-07-12 17:32:32 +00001701 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001702 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1703 const unsigned DstReg = I.getOperand(0).getReg();
1704 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1705 const TargetRegisterClass *DstRC =
1706 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1707 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001708 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001709 }
Amara Emerson1e8c1642018-07-31 00:09:02 +00001710 case TargetOpcode::G_BLOCK_ADDR: {
1711 if (TM.getCodeModel() == CodeModel::Large) {
1712 materializeLargeCMVal(I, I.getOperand(1).getBlockAddress(), 0);
1713 I.eraseFromParent();
1714 return true;
1715 } else {
1716 I.setDesc(TII.get(AArch64::MOVaddrBA));
1717 auto MovMI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::MOVaddrBA),
1718 I.getOperand(0).getReg())
1719 .addBlockAddress(I.getOperand(1).getBlockAddress(),
1720 /* Offset */ 0, AArch64II::MO_PAGE)
1721 .addBlockAddress(
1722 I.getOperand(1).getBlockAddress(), /* Offset */ 0,
1723 AArch64II::MO_NC | AArch64II::MO_PAGEOFF);
1724 I.eraseFromParent();
1725 return constrainSelectedInstRegOperands(*MovMI, TII, TRI, RBI);
1726 }
1727 }
Amara Emerson5ec14602018-12-10 18:44:58 +00001728 case TargetOpcode::G_BUILD_VECTOR:
1729 return selectBuildVector(I, MRI);
Amara Emerson8cb186c2018-12-20 01:11:04 +00001730 case TargetOpcode::G_MERGE_VALUES:
1731 return selectMergeValues(I, MRI);
Jessica Paquette245047d2019-01-24 22:00:41 +00001732 case TargetOpcode::G_UNMERGE_VALUES:
1733 return selectUnmergeValues(I, MRI);
Amara Emerson1abe05c2019-02-21 20:20:16 +00001734 case TargetOpcode::G_SHUFFLE_VECTOR:
1735 return selectShuffleVector(I, MRI);
Jessica Paquette607774c2019-03-11 22:18:01 +00001736 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
1737 return selectExtractElt(I, MRI);
Jessica Paquette5aff1f42019-03-14 18:01:30 +00001738 case TargetOpcode::G_INSERT_VECTOR_ELT:
1739 return selectInsertElt(I, MRI);
Amara Emerson2ff22982019-03-14 22:48:15 +00001740 case TargetOpcode::G_CONCAT_VECTORS:
1741 return selectConcatVectors(I, MRI);
Amara Emerson1e8c1642018-07-31 00:09:02 +00001742 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001743
1744 return false;
1745}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001746
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001747MachineInstr *AArch64InstructionSelector::emitScalarToVector(
Amara Emerson8acb0d92019-03-04 19:16:00 +00001748 unsigned EltSize, const TargetRegisterClass *DstRC, unsigned Scalar,
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001749 MachineIRBuilder &MIRBuilder) const {
1750 auto Undef = MIRBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF, {DstRC}, {});
Amara Emerson5ec14602018-12-10 18:44:58 +00001751
1752 auto BuildFn = [&](unsigned SubregIndex) {
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001753 auto Ins =
1754 MIRBuilder
1755 .buildInstr(TargetOpcode::INSERT_SUBREG, {DstRC}, {Undef, Scalar})
1756 .addImm(SubregIndex);
1757 constrainSelectedInstRegOperands(*Undef, TII, TRI, RBI);
1758 constrainSelectedInstRegOperands(*Ins, TII, TRI, RBI);
1759 return &*Ins;
Amara Emerson5ec14602018-12-10 18:44:58 +00001760 };
1761
Amara Emerson8acb0d92019-03-04 19:16:00 +00001762 switch (EltSize) {
Jessica Paquette245047d2019-01-24 22:00:41 +00001763 case 16:
1764 return BuildFn(AArch64::hsub);
Amara Emerson5ec14602018-12-10 18:44:58 +00001765 case 32:
1766 return BuildFn(AArch64::ssub);
1767 case 64:
1768 return BuildFn(AArch64::dsub);
1769 default:
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001770 return nullptr;
Amara Emerson5ec14602018-12-10 18:44:58 +00001771 }
1772}
1773
Amara Emerson8cb186c2018-12-20 01:11:04 +00001774bool AArch64InstructionSelector::selectMergeValues(
1775 MachineInstr &I, MachineRegisterInfo &MRI) const {
1776 assert(I.getOpcode() == TargetOpcode::G_MERGE_VALUES && "unexpected opcode");
1777 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1778 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1779 assert(!DstTy.isVector() && !SrcTy.isVector() && "invalid merge operation");
1780
1781 // At the moment we only support merging two s32s into an s64.
1782 if (I.getNumOperands() != 3)
1783 return false;
1784 if (DstTy.getSizeInBits() != 64 || SrcTy.getSizeInBits() != 32)
1785 return false;
1786 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
1787 if (RB.getID() != AArch64::GPRRegBankID)
1788 return false;
1789
1790 auto *DstRC = &AArch64::GPR64RegClass;
1791 unsigned SubToRegDef = MRI.createVirtualRegister(DstRC);
1792 MachineInstr &SubRegMI = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1793 TII.get(TargetOpcode::SUBREG_TO_REG))
1794 .addDef(SubToRegDef)
1795 .addImm(0)
1796 .addUse(I.getOperand(1).getReg())
1797 .addImm(AArch64::sub_32);
1798 unsigned SubToRegDef2 = MRI.createVirtualRegister(DstRC);
1799 // Need to anyext the second scalar before we can use bfm
1800 MachineInstr &SubRegMI2 = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1801 TII.get(TargetOpcode::SUBREG_TO_REG))
1802 .addDef(SubToRegDef2)
1803 .addImm(0)
1804 .addUse(I.getOperand(2).getReg())
1805 .addImm(AArch64::sub_32);
Amara Emerson8cb186c2018-12-20 01:11:04 +00001806 MachineInstr &BFM =
1807 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::BFMXri))
Amara Emerson321bfb22018-12-20 03:27:42 +00001808 .addDef(I.getOperand(0).getReg())
Amara Emerson8cb186c2018-12-20 01:11:04 +00001809 .addUse(SubToRegDef)
1810 .addUse(SubToRegDef2)
1811 .addImm(32)
1812 .addImm(31);
1813 constrainSelectedInstRegOperands(SubRegMI, TII, TRI, RBI);
1814 constrainSelectedInstRegOperands(SubRegMI2, TII, TRI, RBI);
1815 constrainSelectedInstRegOperands(BFM, TII, TRI, RBI);
1816 I.eraseFromParent();
1817 return true;
1818}
1819
Jessica Paquette607774c2019-03-11 22:18:01 +00001820static bool getLaneCopyOpcode(unsigned &CopyOpc, unsigned &ExtractSubReg,
1821 const unsigned EltSize) {
1822 // Choose a lane copy opcode and subregister based off of the size of the
1823 // vector's elements.
1824 switch (EltSize) {
1825 case 16:
1826 CopyOpc = AArch64::CPYi16;
1827 ExtractSubReg = AArch64::hsub;
1828 break;
1829 case 32:
1830 CopyOpc = AArch64::CPYi32;
1831 ExtractSubReg = AArch64::ssub;
1832 break;
1833 case 64:
1834 CopyOpc = AArch64::CPYi64;
1835 ExtractSubReg = AArch64::dsub;
1836 break;
1837 default:
1838 // Unknown size, bail out.
1839 LLVM_DEBUG(dbgs() << "Elt size '" << EltSize << "' unsupported.\n");
1840 return false;
1841 }
1842 return true;
1843}
1844
Jessica Paquettebb1aced2019-03-13 21:19:29 +00001845/// Given a register \p Reg, find the value of a constant defining \p Reg.
1846/// Return true if one could be found, and store it in \p Val. Return false
1847/// otherwise.
1848static bool getConstantValueForReg(unsigned Reg, MachineRegisterInfo &MRI,
1849 unsigned &Val) {
1850 // Look at the def of the register.
1851 MachineInstr *Def = MRI.getVRegDef(Reg);
1852 if (!Def)
1853 return false;
1854
1855 // Find the first definition which isn't a copy.
1856 if (Def->isCopy()) {
1857 Reg = Def->getOperand(1).getReg();
1858 auto It = find_if_not(MRI.reg_nodbg_instructions(Reg),
1859 [](const MachineInstr &MI) { return MI.isCopy(); });
1860 if (It == MRI.reg_instr_nodbg_end()) {
1861 LLVM_DEBUG(dbgs() << "Couldn't find non-copy def for register\n");
1862 return false;
1863 }
1864 Def = &*It;
1865 }
1866
1867 // TODO: Handle opcodes other than G_CONSTANT.
1868 if (Def->getOpcode() != TargetOpcode::G_CONSTANT) {
1869 LLVM_DEBUG(dbgs() << "VRegs defined by anything other than G_CONSTANT "
1870 "currently unsupported.\n");
1871 return false;
1872 }
1873
1874 // Return the constant value associated with the operand.
1875 Val = Def->getOperand(1).getCImm()->getLimitedValue();
1876 return true;
1877}
1878
Amara Emersond61b89b2019-03-14 22:48:18 +00001879MachineInstr *AArch64InstructionSelector::emitExtractVectorElt(
1880 Optional<unsigned> DstReg, const RegisterBank &DstRB, LLT ScalarTy,
1881 unsigned VecReg, unsigned LaneIdx, MachineIRBuilder &MIRBuilder) const {
1882 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
1883 unsigned CopyOpc = 0;
1884 unsigned ExtractSubReg = 0;
1885 if (!getLaneCopyOpcode(CopyOpc, ExtractSubReg, ScalarTy.getSizeInBits())) {
1886 LLVM_DEBUG(
1887 dbgs() << "Couldn't determine lane copy opcode for instruction.\n");
1888 return nullptr;
1889 }
1890
1891 const TargetRegisterClass *DstRC =
1892 getRegClassForTypeOnBank(ScalarTy, DstRB, RBI, true);
1893 if (!DstRC) {
1894 LLVM_DEBUG(dbgs() << "Could not determine destination register class.\n");
1895 return nullptr;
1896 }
1897
1898 const RegisterBank &VecRB = *RBI.getRegBank(VecReg, MRI, TRI);
1899 const LLT &VecTy = MRI.getType(VecReg);
1900 const TargetRegisterClass *VecRC =
1901 getRegClassForTypeOnBank(VecTy, VecRB, RBI, true);
1902 if (!VecRC) {
1903 LLVM_DEBUG(dbgs() << "Could not determine source register class.\n");
1904 return nullptr;
1905 }
1906
1907 // The register that we're going to copy into.
1908 unsigned InsertReg = VecReg;
1909 if (!DstReg)
1910 DstReg = MRI.createVirtualRegister(DstRC);
1911 // If the lane index is 0, we just use a subregister COPY.
1912 if (LaneIdx == 0) {
1913 auto CopyMI =
1914 BuildMI(MIRBuilder.getMBB(), MIRBuilder.getInsertPt(),
1915 MIRBuilder.getDL(), TII.get(TargetOpcode::COPY), *DstReg)
1916 .addUse(VecReg, 0, ExtractSubReg);
1917 RBI.constrainGenericRegister(*DstReg, *DstRC, MRI);
1918 return &*CopyMI;
1919 }
1920
1921 // Lane copies require 128-bit wide registers. If we're dealing with an
1922 // unpacked vector, then we need to move up to that width. Insert an implicit
1923 // def and a subregister insert to get us there.
1924 if (VecTy.getSizeInBits() != 128) {
1925 MachineInstr *ScalarToVector = emitScalarToVector(
1926 VecTy.getSizeInBits(), &AArch64::FPR128RegClass, VecReg, MIRBuilder);
1927 if (!ScalarToVector)
1928 return nullptr;
1929 InsertReg = ScalarToVector->getOperand(0).getReg();
1930 }
1931
1932 MachineInstr *LaneCopyMI =
1933 MIRBuilder.buildInstr(CopyOpc, {*DstReg}, {InsertReg}).addImm(LaneIdx);
1934 constrainSelectedInstRegOperands(*LaneCopyMI, TII, TRI, RBI);
1935
1936 // Make sure that we actually constrain the initial copy.
1937 RBI.constrainGenericRegister(*DstReg, *DstRC, MRI);
1938 return LaneCopyMI;
1939}
1940
Jessica Paquette607774c2019-03-11 22:18:01 +00001941bool AArch64InstructionSelector::selectExtractElt(
1942 MachineInstr &I, MachineRegisterInfo &MRI) const {
1943 assert(I.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT &&
1944 "unexpected opcode!");
1945 unsigned DstReg = I.getOperand(0).getReg();
1946 const LLT NarrowTy = MRI.getType(DstReg);
1947 const unsigned SrcReg = I.getOperand(1).getReg();
1948 const LLT WideTy = MRI.getType(SrcReg);
Amara Emersond61b89b2019-03-14 22:48:18 +00001949 (void)WideTy;
Jessica Paquette607774c2019-03-11 22:18:01 +00001950 assert(WideTy.getSizeInBits() >= NarrowTy.getSizeInBits() &&
1951 "source register size too small!");
1952 assert(NarrowTy.isScalar() && "cannot extract vector into vector!");
1953
1954 // Need the lane index to determine the correct copy opcode.
1955 MachineOperand &LaneIdxOp = I.getOperand(2);
1956 assert(LaneIdxOp.isReg() && "Lane index operand was not a register?");
1957
1958 if (RBI.getRegBank(DstReg, MRI, TRI)->getID() != AArch64::FPRRegBankID) {
1959 LLVM_DEBUG(dbgs() << "Cannot extract into GPR.\n");
1960 return false;
1961 }
1962
Jessica Paquettebb1aced2019-03-13 21:19:29 +00001963 // Find the index to extract from.
1964 unsigned LaneIdx = 0;
1965 if (!getConstantValueForReg(LaneIdxOp.getReg(), MRI, LaneIdx))
Jessica Paquette607774c2019-03-11 22:18:01 +00001966 return false;
Jessica Paquette607774c2019-03-11 22:18:01 +00001967
Jessica Paquette607774c2019-03-11 22:18:01 +00001968 MachineIRBuilder MIRBuilder(I);
1969
Amara Emersond61b89b2019-03-14 22:48:18 +00001970 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1971 MachineInstr *Extract = emitExtractVectorElt(DstReg, DstRB, NarrowTy, SrcReg,
1972 LaneIdx, MIRBuilder);
1973 if (!Extract)
1974 return false;
1975
1976 I.eraseFromParent();
1977 return true;
1978}
1979
1980bool AArch64InstructionSelector::selectSplitVectorUnmerge(
1981 MachineInstr &I, MachineRegisterInfo &MRI) const {
1982 unsigned NumElts = I.getNumOperands() - 1;
1983 unsigned SrcReg = I.getOperand(NumElts).getReg();
1984 const LLT NarrowTy = MRI.getType(I.getOperand(0).getReg());
1985 const LLT SrcTy = MRI.getType(SrcReg);
1986
1987 assert(NarrowTy.isVector() && "Expected an unmerge into vectors");
1988 if (SrcTy.getSizeInBits() > 128) {
1989 LLVM_DEBUG(dbgs() << "Unexpected vector type for vec split unmerge");
1990 return false;
Jessica Paquette607774c2019-03-11 22:18:01 +00001991 }
1992
Amara Emersond61b89b2019-03-14 22:48:18 +00001993 MachineIRBuilder MIB(I);
1994
1995 // We implement a split vector operation by treating the sub-vectors as
1996 // scalars and extracting them.
1997 const RegisterBank &DstRB =
1998 *RBI.getRegBank(I.getOperand(0).getReg(), MRI, TRI);
1999 for (unsigned OpIdx = 0; OpIdx < NumElts; ++OpIdx) {
2000 unsigned Dst = I.getOperand(OpIdx).getReg();
2001 MachineInstr *Extract =
2002 emitExtractVectorElt(Dst, DstRB, NarrowTy, SrcReg, OpIdx, MIB);
2003 if (!Extract)
Jessica Paquette607774c2019-03-11 22:18:01 +00002004 return false;
Jessica Paquette607774c2019-03-11 22:18:01 +00002005 }
Jessica Paquette607774c2019-03-11 22:18:01 +00002006 I.eraseFromParent();
2007 return true;
2008}
2009
Jessica Paquette245047d2019-01-24 22:00:41 +00002010bool AArch64InstructionSelector::selectUnmergeValues(
2011 MachineInstr &I, MachineRegisterInfo &MRI) const {
2012 assert(I.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2013 "unexpected opcode");
2014
2015 // TODO: Handle unmerging into GPRs and from scalars to scalars.
2016 if (RBI.getRegBank(I.getOperand(0).getReg(), MRI, TRI)->getID() !=
2017 AArch64::FPRRegBankID ||
2018 RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI)->getID() !=
2019 AArch64::FPRRegBankID) {
2020 LLVM_DEBUG(dbgs() << "Unmerging vector-to-gpr and scalar-to-scalar "
2021 "currently unsupported.\n");
2022 return false;
2023 }
2024
2025 // The last operand is the vector source register, and every other operand is
2026 // a register to unpack into.
2027 unsigned NumElts = I.getNumOperands() - 1;
2028 unsigned SrcReg = I.getOperand(NumElts).getReg();
2029 const LLT NarrowTy = MRI.getType(I.getOperand(0).getReg());
2030 const LLT WideTy = MRI.getType(SrcReg);
Benjamin Kramer653020d2019-01-24 23:45:07 +00002031 (void)WideTy;
Jessica Paquette245047d2019-01-24 22:00:41 +00002032 assert(WideTy.isVector() && "can only unmerge from vector types!");
2033 assert(WideTy.getSizeInBits() > NarrowTy.getSizeInBits() &&
2034 "source register size too small!");
2035
Amara Emersond61b89b2019-03-14 22:48:18 +00002036 if (!NarrowTy.isScalar())
2037 return selectSplitVectorUnmerge(I, MRI);
Jessica Paquette245047d2019-01-24 22:00:41 +00002038
2039 // Choose a lane copy opcode and subregister based off of the size of the
2040 // vector's elements.
2041 unsigned CopyOpc = 0;
2042 unsigned ExtractSubReg = 0;
Jessica Paquette607774c2019-03-11 22:18:01 +00002043 if (!getLaneCopyOpcode(CopyOpc, ExtractSubReg, NarrowTy.getSizeInBits()))
Jessica Paquette245047d2019-01-24 22:00:41 +00002044 return false;
Jessica Paquette245047d2019-01-24 22:00:41 +00002045
2046 // Set up for the lane copies.
2047 MachineBasicBlock &MBB = *I.getParent();
2048
2049 // Stores the registers we'll be copying from.
2050 SmallVector<unsigned, 4> InsertRegs;
2051
2052 // We'll use the first register twice, so we only need NumElts-1 registers.
2053 unsigned NumInsertRegs = NumElts - 1;
2054
2055 // If our elements fit into exactly 128 bits, then we can copy from the source
2056 // directly. Otherwise, we need to do a bit of setup with some subregister
2057 // inserts.
2058 if (NarrowTy.getSizeInBits() * NumElts == 128) {
2059 InsertRegs = SmallVector<unsigned, 4>(NumInsertRegs, SrcReg);
2060 } else {
2061 // No. We have to perform subregister inserts. For each insert, create an
2062 // implicit def and a subregister insert, and save the register we create.
2063 for (unsigned Idx = 0; Idx < NumInsertRegs; ++Idx) {
2064 unsigned ImpDefReg = MRI.createVirtualRegister(&AArch64::FPR128RegClass);
2065 MachineInstr &ImpDefMI =
2066 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(TargetOpcode::IMPLICIT_DEF),
2067 ImpDefReg);
2068
2069 // Now, create the subregister insert from SrcReg.
2070 unsigned InsertReg = MRI.createVirtualRegister(&AArch64::FPR128RegClass);
2071 MachineInstr &InsMI =
2072 *BuildMI(MBB, I, I.getDebugLoc(),
2073 TII.get(TargetOpcode::INSERT_SUBREG), InsertReg)
2074 .addUse(ImpDefReg)
2075 .addUse(SrcReg)
2076 .addImm(AArch64::dsub);
2077
2078 constrainSelectedInstRegOperands(ImpDefMI, TII, TRI, RBI);
2079 constrainSelectedInstRegOperands(InsMI, TII, TRI, RBI);
2080
2081 // Save the register so that we can copy from it after.
2082 InsertRegs.push_back(InsertReg);
2083 }
2084 }
2085
2086 // Now that we've created any necessary subregister inserts, we can
2087 // create the copies.
2088 //
2089 // Perform the first copy separately as a subregister copy.
2090 unsigned CopyTo = I.getOperand(0).getReg();
2091 MachineInstr &FirstCopy =
2092 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(TargetOpcode::COPY), CopyTo)
2093 .addUse(InsertRegs[0], 0, ExtractSubReg);
2094 constrainSelectedInstRegOperands(FirstCopy, TII, TRI, RBI);
2095
2096 // Now, perform the remaining copies as vector lane copies.
2097 unsigned LaneIdx = 1;
2098 for (unsigned InsReg : InsertRegs) {
2099 unsigned CopyTo = I.getOperand(LaneIdx).getReg();
2100 MachineInstr &CopyInst =
2101 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CopyOpc), CopyTo)
2102 .addUse(InsReg)
2103 .addImm(LaneIdx);
2104 constrainSelectedInstRegOperands(CopyInst, TII, TRI, RBI);
2105 ++LaneIdx;
2106 }
2107
2108 // Separately constrain the first copy's destination. Because of the
2109 // limitation in constrainOperandRegClass, we can't guarantee that this will
2110 // actually be constrained. So, do it ourselves using the second operand.
2111 const TargetRegisterClass *RC =
2112 MRI.getRegClassOrNull(I.getOperand(1).getReg());
2113 if (!RC) {
2114 LLVM_DEBUG(dbgs() << "Couldn't constrain copy destination.\n");
2115 return false;
2116 }
2117
2118 RBI.constrainGenericRegister(CopyTo, *RC, MRI);
2119 I.eraseFromParent();
2120 return true;
2121}
2122
Amara Emerson2ff22982019-03-14 22:48:15 +00002123bool AArch64InstructionSelector::selectConcatVectors(
2124 MachineInstr &I, MachineRegisterInfo &MRI) const {
2125 assert(I.getOpcode() == TargetOpcode::G_CONCAT_VECTORS &&
2126 "Unexpected opcode");
2127 unsigned Dst = I.getOperand(0).getReg();
2128 unsigned Op1 = I.getOperand(1).getReg();
2129 unsigned Op2 = I.getOperand(2).getReg();
2130 MachineIRBuilder MIRBuilder(I);
2131 MachineInstr *ConcatMI = emitVectorConcat(Dst, Op1, Op2, MIRBuilder);
2132 if (!ConcatMI)
2133 return false;
2134 I.eraseFromParent();
2135 return true;
2136}
2137
Amara Emerson1abe05c2019-02-21 20:20:16 +00002138void AArch64InstructionSelector::collectShuffleMaskIndices(
2139 MachineInstr &I, MachineRegisterInfo &MRI,
2140 SmallVectorImpl<int> &Idxs) const {
2141 MachineInstr *MaskDef = MRI.getVRegDef(I.getOperand(3).getReg());
2142 assert(
2143 MaskDef->getOpcode() == TargetOpcode::G_BUILD_VECTOR &&
2144 "G_SHUFFLE_VECTOR should have a constant mask operand as G_BUILD_VECTOR");
2145 // Find the constant indices.
2146 for (unsigned i = 1, e = MaskDef->getNumOperands(); i < e; ++i) {
2147 MachineInstr *ScalarDef = MRI.getVRegDef(MaskDef->getOperand(i).getReg());
2148 assert(ScalarDef && "Could not find vreg def of shufflevec index op");
2149 // Look through copies.
2150 while (ScalarDef->getOpcode() == TargetOpcode::COPY) {
2151 ScalarDef = MRI.getVRegDef(ScalarDef->getOperand(1).getReg());
2152 assert(ScalarDef && "Could not find def of copy operand");
2153 }
2154 assert(ScalarDef->getOpcode() == TargetOpcode::G_CONSTANT);
2155 Idxs.push_back(ScalarDef->getOperand(1).getCImm()->getSExtValue());
2156 }
2157}
2158
2159unsigned
2160AArch64InstructionSelector::emitConstantPoolEntry(Constant *CPVal,
2161 MachineFunction &MF) const {
2162 Type *CPTy = CPVal->getType()->getPointerTo();
2163 unsigned Align = MF.getDataLayout().getPrefTypeAlignment(CPTy);
2164 if (Align == 0)
2165 Align = MF.getDataLayout().getTypeAllocSize(CPTy);
2166
2167 MachineConstantPool *MCP = MF.getConstantPool();
2168 return MCP->getConstantPoolIndex(CPVal, Align);
2169}
2170
2171MachineInstr *AArch64InstructionSelector::emitLoadFromConstantPool(
2172 Constant *CPVal, MachineIRBuilder &MIRBuilder) const {
2173 unsigned CPIdx = emitConstantPoolEntry(CPVal, MIRBuilder.getMF());
2174
2175 auto Adrp =
2176 MIRBuilder.buildInstr(AArch64::ADRP, {&AArch64::GPR64RegClass}, {})
2177 .addConstantPoolIndex(CPIdx, 0, AArch64II::MO_PAGE);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002178
2179 MachineInstr *LoadMI = nullptr;
2180 switch (MIRBuilder.getDataLayout().getTypeStoreSize(CPVal->getType())) {
2181 case 16:
2182 LoadMI =
2183 &*MIRBuilder
2184 .buildInstr(AArch64::LDRQui, {&AArch64::FPR128RegClass}, {Adrp})
2185 .addConstantPoolIndex(CPIdx, 0,
2186 AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2187 break;
2188 case 8:
2189 LoadMI = &*MIRBuilder
2190 .buildInstr(AArch64::LDRDui, {&AArch64::FPR64RegClass}, {Adrp})
2191 .addConstantPoolIndex(
2192 CPIdx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2193 break;
2194 default:
2195 LLVM_DEBUG(dbgs() << "Could not load from constant pool of type "
2196 << *CPVal->getType());
2197 return nullptr;
2198 }
Amara Emerson1abe05c2019-02-21 20:20:16 +00002199 constrainSelectedInstRegOperands(*Adrp, TII, TRI, RBI);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002200 constrainSelectedInstRegOperands(*LoadMI, TII, TRI, RBI);
2201 return LoadMI;
2202}
2203
2204/// Return an <Opcode, SubregIndex> pair to do an vector elt insert of a given
2205/// size and RB.
2206static std::pair<unsigned, unsigned>
2207getInsertVecEltOpInfo(const RegisterBank &RB, unsigned EltSize) {
2208 unsigned Opc, SubregIdx;
2209 if (RB.getID() == AArch64::GPRRegBankID) {
2210 if (EltSize == 32) {
2211 Opc = AArch64::INSvi32gpr;
2212 SubregIdx = AArch64::ssub;
2213 } else if (EltSize == 64) {
2214 Opc = AArch64::INSvi64gpr;
2215 SubregIdx = AArch64::dsub;
2216 } else {
2217 llvm_unreachable("invalid elt size!");
2218 }
2219 } else {
2220 if (EltSize == 8) {
2221 Opc = AArch64::INSvi8lane;
2222 SubregIdx = AArch64::bsub;
2223 } else if (EltSize == 16) {
2224 Opc = AArch64::INSvi16lane;
2225 SubregIdx = AArch64::hsub;
2226 } else if (EltSize == 32) {
2227 Opc = AArch64::INSvi32lane;
2228 SubregIdx = AArch64::ssub;
2229 } else if (EltSize == 64) {
2230 Opc = AArch64::INSvi64lane;
2231 SubregIdx = AArch64::dsub;
2232 } else {
2233 llvm_unreachable("invalid elt size!");
2234 }
2235 }
2236 return std::make_pair(Opc, SubregIdx);
2237}
2238
2239MachineInstr *AArch64InstructionSelector::emitVectorConcat(
Amara Emerson2ff22982019-03-14 22:48:15 +00002240 Optional<unsigned> Dst, unsigned Op1, unsigned Op2,
2241 MachineIRBuilder &MIRBuilder) const {
Amara Emerson8acb0d92019-03-04 19:16:00 +00002242 // We implement a vector concat by:
2243 // 1. Use scalar_to_vector to insert the lower vector into the larger dest
2244 // 2. Insert the upper vector into the destination's upper element
2245 // TODO: some of this code is common with G_BUILD_VECTOR handling.
2246 MachineRegisterInfo &MRI = MIRBuilder.getMF().getRegInfo();
2247
2248 const LLT Op1Ty = MRI.getType(Op1);
2249 const LLT Op2Ty = MRI.getType(Op2);
2250
2251 if (Op1Ty != Op2Ty) {
2252 LLVM_DEBUG(dbgs() << "Could not do vector concat of differing vector tys");
2253 return nullptr;
2254 }
2255 assert(Op1Ty.isVector() && "Expected a vector for vector concat");
2256
2257 if (Op1Ty.getSizeInBits() >= 128) {
2258 LLVM_DEBUG(dbgs() << "Vector concat not supported for full size vectors");
2259 return nullptr;
2260 }
2261
2262 // At the moment we just support 64 bit vector concats.
2263 if (Op1Ty.getSizeInBits() != 64) {
2264 LLVM_DEBUG(dbgs() << "Vector concat supported for 64b vectors");
2265 return nullptr;
2266 }
2267
2268 const LLT ScalarTy = LLT::scalar(Op1Ty.getSizeInBits());
2269 const RegisterBank &FPRBank = *RBI.getRegBank(Op1, MRI, TRI);
2270 const TargetRegisterClass *DstRC =
2271 getMinClassForRegBank(FPRBank, Op1Ty.getSizeInBits() * 2);
2272
2273 MachineInstr *WidenedOp1 =
2274 emitScalarToVector(ScalarTy.getSizeInBits(), DstRC, Op1, MIRBuilder);
2275 MachineInstr *WidenedOp2 =
2276 emitScalarToVector(ScalarTy.getSizeInBits(), DstRC, Op2, MIRBuilder);
2277 if (!WidenedOp1 || !WidenedOp2) {
2278 LLVM_DEBUG(dbgs() << "Could not emit a vector from scalar value");
2279 return nullptr;
2280 }
2281
2282 // Now do the insert of the upper element.
2283 unsigned InsertOpc, InsSubRegIdx;
2284 std::tie(InsertOpc, InsSubRegIdx) =
2285 getInsertVecEltOpInfo(FPRBank, ScalarTy.getSizeInBits());
2286
Amara Emerson2ff22982019-03-14 22:48:15 +00002287 if (!Dst)
2288 Dst = MRI.createVirtualRegister(DstRC);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002289 auto InsElt =
2290 MIRBuilder
Amara Emerson2ff22982019-03-14 22:48:15 +00002291 .buildInstr(InsertOpc, {*Dst}, {WidenedOp1->getOperand(0).getReg()})
Amara Emerson8acb0d92019-03-04 19:16:00 +00002292 .addImm(1) /* Lane index */
2293 .addUse(WidenedOp2->getOperand(0).getReg())
2294 .addImm(0);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002295 constrainSelectedInstRegOperands(*InsElt, TII, TRI, RBI);
2296 return &*InsElt;
Amara Emerson1abe05c2019-02-21 20:20:16 +00002297}
2298
2299bool AArch64InstructionSelector::selectShuffleVector(
2300 MachineInstr &I, MachineRegisterInfo &MRI) const {
2301 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
2302 unsigned Src1Reg = I.getOperand(1).getReg();
2303 const LLT Src1Ty = MRI.getType(Src1Reg);
2304 unsigned Src2Reg = I.getOperand(2).getReg();
2305 const LLT Src2Ty = MRI.getType(Src2Reg);
2306
2307 MachineBasicBlock &MBB = *I.getParent();
2308 MachineFunction &MF = *MBB.getParent();
2309 LLVMContext &Ctx = MF.getFunction().getContext();
2310
2311 // G_SHUFFLE_VECTOR doesn't really have a strictly enforced constant mask
2312 // operand, it comes in as a normal vector value which we have to analyze to
2313 // find the mask indices.
2314 SmallVector<int, 8> Mask;
2315 collectShuffleMaskIndices(I, MRI, Mask);
2316 assert(!Mask.empty() && "Expected to find mask indices");
2317
2318 // G_SHUFFLE_VECTOR is weird in that the source operands can be scalars, if
2319 // it's originated from a <1 x T> type. Those should have been lowered into
2320 // G_BUILD_VECTOR earlier.
2321 if (!Src1Ty.isVector() || !Src2Ty.isVector()) {
2322 LLVM_DEBUG(dbgs() << "Could not select a \"scalar\" G_SHUFFLE_VECTOR\n");
2323 return false;
2324 }
2325
2326 unsigned BytesPerElt = DstTy.getElementType().getSizeInBits() / 8;
2327
2328 SmallVector<Constant *, 64> CstIdxs;
2329 for (int Val : Mask) {
2330 for (unsigned Byte = 0; Byte < BytesPerElt; ++Byte) {
2331 unsigned Offset = Byte + Val * BytesPerElt;
2332 CstIdxs.emplace_back(ConstantInt::get(Type::getInt8Ty(Ctx), Offset));
2333 }
2334 }
2335
Amara Emerson8acb0d92019-03-04 19:16:00 +00002336 MachineIRBuilder MIRBuilder(I);
Amara Emerson1abe05c2019-02-21 20:20:16 +00002337
2338 // Use a constant pool to load the index vector for TBL.
2339 Constant *CPVal = ConstantVector::get(CstIdxs);
Amara Emerson1abe05c2019-02-21 20:20:16 +00002340 MachineInstr *IndexLoad = emitLoadFromConstantPool(CPVal, MIRBuilder);
2341 if (!IndexLoad) {
2342 LLVM_DEBUG(dbgs() << "Could not load from a constant pool");
2343 return false;
2344 }
2345
Amara Emerson8acb0d92019-03-04 19:16:00 +00002346 if (DstTy.getSizeInBits() != 128) {
2347 assert(DstTy.getSizeInBits() == 64 && "Unexpected shuffle result ty");
2348 // This case can be done with TBL1.
Amara Emerson2ff22982019-03-14 22:48:15 +00002349 MachineInstr *Concat = emitVectorConcat(None, Src1Reg, Src2Reg, MIRBuilder);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002350 if (!Concat) {
2351 LLVM_DEBUG(dbgs() << "Could not do vector concat for tbl1");
2352 return false;
2353 }
2354
2355 // The constant pool load will be 64 bits, so need to convert to FPR128 reg.
2356 IndexLoad =
2357 emitScalarToVector(64, &AArch64::FPR128RegClass,
2358 IndexLoad->getOperand(0).getReg(), MIRBuilder);
2359
2360 auto TBL1 = MIRBuilder.buildInstr(
2361 AArch64::TBLv16i8One, {&AArch64::FPR128RegClass},
2362 {Concat->getOperand(0).getReg(), IndexLoad->getOperand(0).getReg()});
2363 constrainSelectedInstRegOperands(*TBL1, TII, TRI, RBI);
2364
2365 auto Copy = BuildMI(*I.getParent(), I, I.getDebugLoc(),
2366 TII.get(TargetOpcode::COPY), I.getOperand(0).getReg())
2367 .addUse(TBL1->getOperand(0).getReg(), 0, AArch64::dsub);
2368 RBI.constrainGenericRegister(Copy.getReg(0), AArch64::FPR64RegClass, MRI);
2369 I.eraseFromParent();
2370 return true;
2371 }
2372
Amara Emerson1abe05c2019-02-21 20:20:16 +00002373 // For TBL2 we need to emit a REG_SEQUENCE to tie together two consecutive
2374 // Q registers for regalloc.
2375 auto RegSeq = MIRBuilder
2376 .buildInstr(TargetOpcode::REG_SEQUENCE,
2377 {&AArch64::QQRegClass}, {Src1Reg})
2378 .addImm(AArch64::qsub0)
2379 .addUse(Src2Reg)
2380 .addImm(AArch64::qsub1);
2381
2382 auto TBL2 =
2383 MIRBuilder.buildInstr(AArch64::TBLv16i8Two, {I.getOperand(0).getReg()},
2384 {RegSeq, IndexLoad->getOperand(0).getReg()});
2385 constrainSelectedInstRegOperands(*RegSeq, TII, TRI, RBI);
2386 constrainSelectedInstRegOperands(*TBL2, TII, TRI, RBI);
2387 I.eraseFromParent();
2388 return true;
2389}
2390
Jessica Paquette16d67a32019-03-13 23:22:23 +00002391MachineInstr *AArch64InstructionSelector::emitLaneInsert(
2392 Optional<unsigned> DstReg, unsigned SrcReg, unsigned EltReg,
2393 unsigned LaneIdx, const RegisterBank &RB,
2394 MachineIRBuilder &MIRBuilder) const {
2395 MachineInstr *InsElt = nullptr;
2396 const TargetRegisterClass *DstRC = &AArch64::FPR128RegClass;
2397 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
2398
2399 // Create a register to define with the insert if one wasn't passed in.
2400 if (!DstReg)
2401 DstReg = MRI.createVirtualRegister(DstRC);
2402
2403 unsigned EltSize = MRI.getType(EltReg).getSizeInBits();
2404 unsigned Opc = getInsertVecEltOpInfo(RB, EltSize).first;
2405
2406 if (RB.getID() == AArch64::FPRRegBankID) {
2407 auto InsSub = emitScalarToVector(EltSize, DstRC, EltReg, MIRBuilder);
2408 InsElt = MIRBuilder.buildInstr(Opc, {*DstReg}, {SrcReg})
2409 .addImm(LaneIdx)
2410 .addUse(InsSub->getOperand(0).getReg())
2411 .addImm(0);
2412 } else {
2413 InsElt = MIRBuilder.buildInstr(Opc, {*DstReg}, {SrcReg})
2414 .addImm(LaneIdx)
2415 .addUse(EltReg);
2416 }
2417
2418 constrainSelectedInstRegOperands(*InsElt, TII, TRI, RBI);
2419 return InsElt;
2420}
2421
Jessica Paquette5aff1f42019-03-14 18:01:30 +00002422bool AArch64InstructionSelector::selectInsertElt(
2423 MachineInstr &I, MachineRegisterInfo &MRI) const {
2424 assert(I.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT);
2425
2426 // Get information on the destination.
2427 unsigned DstReg = I.getOperand(0).getReg();
2428 const LLT DstTy = MRI.getType(DstReg);
2429 if (DstTy.getSizeInBits() < 128) {
2430 // TODO: Handle unpacked vectors.
2431 LLVM_DEBUG(dbgs() << "Unpacked vectors not supported yet!");
2432 return false;
2433 }
2434
2435 // Get information on the element we want to insert into the destination.
2436 unsigned EltReg = I.getOperand(2).getReg();
2437 const LLT EltTy = MRI.getType(EltReg);
2438 unsigned EltSize = EltTy.getSizeInBits();
2439 if (EltSize < 16 || EltSize > 64)
2440 return false; // Don't support all element types yet.
2441
2442 // Find the definition of the index. Bail out if it's not defined by a
2443 // G_CONSTANT.
2444 unsigned IdxReg = I.getOperand(3).getReg();
2445 unsigned LaneIdx = 0;
2446 if (!getConstantValueForReg(IdxReg, MRI, LaneIdx))
2447 return false;
2448
2449 // Perform the lane insert.
2450 unsigned SrcReg = I.getOperand(1).getReg();
2451 const RegisterBank &EltRB = *RBI.getRegBank(EltReg, MRI, TRI);
2452 MachineIRBuilder MIRBuilder(I);
2453 emitLaneInsert(DstReg, SrcReg, EltReg, LaneIdx, EltRB, MIRBuilder);
2454 I.eraseFromParent();
2455 return true;
2456}
2457
Amara Emerson5ec14602018-12-10 18:44:58 +00002458bool AArch64InstructionSelector::selectBuildVector(
2459 MachineInstr &I, MachineRegisterInfo &MRI) const {
2460 assert(I.getOpcode() == TargetOpcode::G_BUILD_VECTOR);
2461 // Until we port more of the optimized selections, for now just use a vector
2462 // insert sequence.
2463 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
2464 const LLT EltTy = MRI.getType(I.getOperand(1).getReg());
2465 unsigned EltSize = EltTy.getSizeInBits();
Jessica Paquette245047d2019-01-24 22:00:41 +00002466 if (EltSize < 16 || EltSize > 64)
Amara Emerson5ec14602018-12-10 18:44:58 +00002467 return false; // Don't support all element types yet.
2468 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002469 MachineIRBuilder MIRBuilder(I);
Jessica Paquette245047d2019-01-24 22:00:41 +00002470
2471 const TargetRegisterClass *DstRC = &AArch64::FPR128RegClass;
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002472 MachineInstr *ScalarToVec =
Amara Emerson8acb0d92019-03-04 19:16:00 +00002473 emitScalarToVector(DstTy.getElementType().getSizeInBits(), DstRC,
2474 I.getOperand(1).getReg(), MIRBuilder);
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002475 if (!ScalarToVec)
Jessica Paquette245047d2019-01-24 22:00:41 +00002476 return false;
2477
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002478 unsigned DstVec = ScalarToVec->getOperand(0).getReg();
Jessica Paquette245047d2019-01-24 22:00:41 +00002479 unsigned DstSize = DstTy.getSizeInBits();
2480
2481 // Keep track of the last MI we inserted. Later on, we might be able to save
2482 // a copy using it.
2483 MachineInstr *PrevMI = nullptr;
2484 for (unsigned i = 2, e = DstSize / EltSize + 1; i < e; ++i) {
Jessica Paquette16d67a32019-03-13 23:22:23 +00002485 // Note that if we don't do a subregister copy, we can end up making an
2486 // extra register.
2487 PrevMI = &*emitLaneInsert(None, DstVec, I.getOperand(i).getReg(), i - 1, RB,
2488 MIRBuilder);
2489 DstVec = PrevMI->getOperand(0).getReg();
Amara Emerson5ec14602018-12-10 18:44:58 +00002490 }
Jessica Paquette245047d2019-01-24 22:00:41 +00002491
2492 // If DstTy's size in bits is less than 128, then emit a subregister copy
2493 // from DstVec to the last register we've defined.
2494 if (DstSize < 128) {
Jessica Paquette85ace622019-03-13 23:29:54 +00002495 // Force this to be FPR using the destination vector.
2496 const TargetRegisterClass *RC =
2497 getMinClassForRegBank(*RBI.getRegBank(DstVec, MRI, TRI), DstSize);
Jessica Paquette245047d2019-01-24 22:00:41 +00002498 if (!RC)
2499 return false;
Jessica Paquette85ace622019-03-13 23:29:54 +00002500 if (RC != &AArch64::FPR32RegClass && RC != &AArch64::FPR64RegClass) {
2501 LLVM_DEBUG(dbgs() << "Unsupported register class!\n");
2502 return false;
2503 }
2504
2505 unsigned SubReg = 0;
2506 if (!getSubRegForClass(RC, TRI, SubReg))
2507 return false;
2508 if (SubReg != AArch64::ssub && SubReg != AArch64::dsub) {
2509 LLVM_DEBUG(dbgs() << "Unsupported destination size! (" << DstSize
2510 << "\n");
2511 return false;
2512 }
Jessica Paquette245047d2019-01-24 22:00:41 +00002513
2514 unsigned Reg = MRI.createVirtualRegister(RC);
2515 unsigned DstReg = I.getOperand(0).getReg();
2516
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002517 // MIRBuilder doesn't let us create uses with subregs & flags, so use
2518 // BuildMI here instead.
Jessica Paquette245047d2019-01-24 22:00:41 +00002519 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
2520 DstReg)
2521 .addUse(DstVec, 0, SubReg);
2522 MachineOperand &RegOp = I.getOperand(1);
2523 RegOp.setReg(Reg);
2524 RBI.constrainGenericRegister(DstReg, *RC, MRI);
2525 } else {
2526 // We don't need a subregister copy. Save a copy by re-using the
2527 // destination register on the final insert.
2528 assert(PrevMI && "PrevMI was null?");
2529 PrevMI->getOperand(0).setReg(I.getOperand(0).getReg());
2530 constrainSelectedInstRegOperands(*PrevMI, TII, TRI, RBI);
2531 }
2532
Amara Emerson5ec14602018-12-10 18:44:58 +00002533 I.eraseFromParent();
2534 return true;
2535}
2536
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002537/// SelectArithImmed - Select an immediate value that can be represented as
2538/// a 12-bit value shifted left by either 0 or 12. If so, return true with
2539/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002540InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00002541AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002542 MachineInstr &MI = *Root.getParent();
2543 MachineBasicBlock &MBB = *MI.getParent();
2544 MachineFunction &MF = *MBB.getParent();
2545 MachineRegisterInfo &MRI = MF.getRegInfo();
2546
2547 // This function is called from the addsub_shifted_imm ComplexPattern,
2548 // which lists [imm] as the list of opcode it's interested in, however
2549 // we still need to check whether the operand is actually an immediate
2550 // here because the ComplexPattern opcode list is only used in
2551 // root-level opcode matching.
2552 uint64_t Immed;
2553 if (Root.isImm())
2554 Immed = Root.getImm();
2555 else if (Root.isCImm())
2556 Immed = Root.getCImm()->getZExtValue();
2557 else if (Root.isReg()) {
2558 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
2559 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002560 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00002561 MachineOperand &Op1 = Def->getOperand(1);
2562 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002563 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00002564 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002565 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002566 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002567
2568 unsigned ShiftAmt;
2569
2570 if (Immed >> 12 == 0) {
2571 ShiftAmt = 0;
2572 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
2573 ShiftAmt = 12;
2574 Immed = Immed >> 12;
2575 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002576 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002577
2578 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002579 return {{
2580 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
2581 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
2582 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002583}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00002584
Daniel Sandersea8711b2017-10-16 03:36:29 +00002585/// Select a "register plus unscaled signed 9-bit immediate" address. This
2586/// should only match when there is an offset that is not valid for a scaled
2587/// immediate addressing mode. The "Size" argument is the size in bytes of the
2588/// memory reference, which is needed here to know what is valid for a scaled
2589/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002590InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00002591AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
2592 unsigned Size) const {
2593 MachineRegisterInfo &MRI =
2594 Root.getParent()->getParent()->getParent()->getRegInfo();
2595
2596 if (!Root.isReg())
2597 return None;
2598
2599 if (!isBaseWithConstantOffset(Root, MRI))
2600 return None;
2601
2602 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
2603 if (!RootDef)
2604 return None;
2605
2606 MachineOperand &OffImm = RootDef->getOperand(2);
2607 if (!OffImm.isReg())
2608 return None;
2609 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
2610 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
2611 return None;
2612 int64_t RHSC;
2613 MachineOperand &RHSOp1 = RHS->getOperand(1);
2614 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
2615 return None;
2616 RHSC = RHSOp1.getCImm()->getSExtValue();
2617
2618 // If the offset is valid as a scaled immediate, don't match here.
2619 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
2620 return None;
2621 if (RHSC >= -256 && RHSC < 256) {
2622 MachineOperand &Base = RootDef->getOperand(1);
2623 return {{
2624 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
2625 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
2626 }};
2627 }
2628 return None;
2629}
2630
2631/// Select a "register plus scaled unsigned 12-bit immediate" address. The
2632/// "Size" argument is the size in bytes of the memory reference, which
2633/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002634InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00002635AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
2636 unsigned Size) const {
2637 MachineRegisterInfo &MRI =
2638 Root.getParent()->getParent()->getParent()->getRegInfo();
2639
2640 if (!Root.isReg())
2641 return None;
2642
2643 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
2644 if (!RootDef)
2645 return None;
2646
2647 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
2648 return {{
2649 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
2650 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
2651 }};
2652 }
2653
2654 if (isBaseWithConstantOffset(Root, MRI)) {
2655 MachineOperand &LHS = RootDef->getOperand(1);
2656 MachineOperand &RHS = RootDef->getOperand(2);
2657 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
2658 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
2659 if (LHSDef && RHSDef) {
2660 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
2661 unsigned Scale = Log2_32(Size);
2662 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
2663 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00002664 return {{
2665 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
2666 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
2667 }};
2668
Daniel Sandersea8711b2017-10-16 03:36:29 +00002669 return {{
2670 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
2671 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
2672 }};
2673 }
2674 }
2675 }
2676
2677 // Before falling back to our general case, check if the unscaled
2678 // instructions can handle this. If so, that's preferable.
2679 if (selectAddrModeUnscaled(Root, Size).hasValue())
2680 return None;
2681
2682 return {{
2683 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
2684 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
2685 }};
2686}
2687
Volkan Kelesf7f25682018-01-16 18:44:05 +00002688void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
2689 const MachineInstr &MI) const {
2690 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2691 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
2692 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
2693 assert(CstVal && "Expected constant value");
2694 MIB.addImm(CstVal.getValue());
2695}
2696
Daniel Sanders0b5293f2017-04-06 09:49:34 +00002697namespace llvm {
2698InstructionSelector *
2699createAArch64InstructionSelector(const AArch64TargetMachine &TM,
2700 AArch64Subtarget &Subtarget,
2701 AArch64RegisterBankInfo &RBI) {
2702 return new AArch64InstructionSelector(TM, Subtarget, RBI);
2703}
2704}