blob: 869cc143496c15f84a95e018af1e540a07bcd779 [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"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000021#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
David Blaikie62651302017-10-26 23:39:54 +000022#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
Amara Emerson1e8c1642018-07-31 00:09:02 +000023#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Aditya Nandakumar75ad9cc2017-04-19 20:48:50 +000024#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000025#include "llvm/CodeGen/MachineBasicBlock.h"
Amara Emerson1abe05c2019-02-21 20:20:16 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000030#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/IR/Type.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/raw_ostream.h"
35
36#define DEBUG_TYPE "aarch64-isel"
37
38using namespace llvm;
39
Daniel Sanders0b5293f2017-04-06 09:49:34 +000040namespace {
41
Daniel Sanderse7b0d662017-04-21 15:59:56 +000042#define GET_GLOBALISEL_PREDICATE_BITSET
43#include "AArch64GenGlobalISel.inc"
44#undef GET_GLOBALISEL_PREDICATE_BITSET
45
Daniel Sanders0b5293f2017-04-06 09:49:34 +000046class AArch64InstructionSelector : public InstructionSelector {
47public:
48 AArch64InstructionSelector(const AArch64TargetMachine &TM,
49 const AArch64Subtarget &STI,
50 const AArch64RegisterBankInfo &RBI);
51
Daniel Sandersf76f3152017-11-16 00:46:35 +000052 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
David Blaikie62651302017-10-26 23:39:54 +000053 static const char *getName() { return DEBUG_TYPE; }
Daniel Sanders0b5293f2017-04-06 09:49:34 +000054
55private:
56 /// tblgen-erated 'select' implementation, used as the initial selector for
57 /// the patterns that don't require complex C++.
Daniel Sandersf76f3152017-11-16 00:46:35 +000058 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000059
60 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
61 MachineRegisterInfo &MRI) const;
62 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
63 MachineRegisterInfo &MRI) const;
64
65 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
66 MachineRegisterInfo &MRI) const;
67
Amara Emerson5ec14602018-12-10 18:44:58 +000068 // Helper to generate an equivalent of scalar_to_vector into a new register,
69 // returned via 'Dst'.
Amara Emerson8acb0d92019-03-04 19:16:00 +000070 MachineInstr *emitScalarToVector(unsigned EltSize,
Amara Emerson6bcfa1c2019-02-25 18:52:54 +000071 const TargetRegisterClass *DstRC,
72 unsigned Scalar,
73 MachineIRBuilder &MIRBuilder) const;
Amara Emerson5ec14602018-12-10 18:44:58 +000074 bool selectBuildVector(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emerson8cb186c2018-12-20 01:11:04 +000075 bool selectMergeValues(MachineInstr &I, MachineRegisterInfo &MRI) const;
Jessica Paquette245047d2019-01-24 22:00:41 +000076 bool selectUnmergeValues(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emerson5ec14602018-12-10 18:44:58 +000077
Amara Emerson1abe05c2019-02-21 20:20:16 +000078 void collectShuffleMaskIndices(MachineInstr &I, MachineRegisterInfo &MRI,
79 SmallVectorImpl<int> &Idxs) const;
80 bool selectShuffleVector(MachineInstr &I, MachineRegisterInfo &MRI) const;
Jessica Paquette607774c2019-03-11 22:18:01 +000081 bool selectExtractElt(MachineInstr &I, MachineRegisterInfo &MRI) const;
Amara Emerson1abe05c2019-02-21 20:20:16 +000082
83 unsigned emitConstantPoolEntry(Constant *CPVal, MachineFunction &MF) const;
84 MachineInstr *emitLoadFromConstantPool(Constant *CPVal,
85 MachineIRBuilder &MIRBuilder) const;
Amara Emerson8acb0d92019-03-04 19:16:00 +000086 MachineInstr *emitVectorConcat(unsigned Op1, unsigned Op2,
87 MachineIRBuilder &MIRBuilder) const;
Amara Emerson1abe05c2019-02-21 20:20:16 +000088
Daniel Sanders1e4569f2017-10-20 20:55:29 +000089 ComplexRendererFns selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000090
Daniel Sanders1e4569f2017-10-20 20:55:29 +000091 ComplexRendererFns selectAddrModeUnscaled(MachineOperand &Root,
92 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +000093
Daniel Sanders1e4569f2017-10-20 20:55:29 +000094 ComplexRendererFns selectAddrModeUnscaled8(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000095 return selectAddrModeUnscaled(Root, 1);
96 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000097 ComplexRendererFns selectAddrModeUnscaled16(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000098 return selectAddrModeUnscaled(Root, 2);
99 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000100 ComplexRendererFns selectAddrModeUnscaled32(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000101 return selectAddrModeUnscaled(Root, 4);
102 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000103 ComplexRendererFns selectAddrModeUnscaled64(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000104 return selectAddrModeUnscaled(Root, 8);
105 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000106 ComplexRendererFns selectAddrModeUnscaled128(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000107 return selectAddrModeUnscaled(Root, 16);
108 }
109
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000110 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root,
111 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +0000112 template <int Width>
Daniel Sanders1e4569f2017-10-20 20:55:29 +0000113 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +0000114 return selectAddrModeIndexed(Root, Width / 8);
115 }
116
Volkan Kelesf7f25682018-01-16 18:44:05 +0000117 void renderTruncImm(MachineInstrBuilder &MIB, const MachineInstr &MI) const;
118
Amara Emerson1e8c1642018-07-31 00:09:02 +0000119 // Materialize a GlobalValue or BlockAddress using a movz+movk sequence.
120 void materializeLargeCMVal(MachineInstr &I, const Value *V,
121 unsigned char OpFlags) const;
122
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000123 const AArch64TargetMachine &TM;
124 const AArch64Subtarget &STI;
125 const AArch64InstrInfo &TII;
126 const AArch64RegisterInfo &TRI;
127 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000128
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000129#define GET_GLOBALISEL_PREDICATES_DECL
130#include "AArch64GenGlobalISel.inc"
131#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000132
133// We declare the temporaries used by selectImpl() in the class to minimize the
134// cost of constructing placeholder values.
135#define GET_GLOBALISEL_TEMPORARIES_DECL
136#include "AArch64GenGlobalISel.inc"
137#undef GET_GLOBALISEL_TEMPORARIES_DECL
138};
139
140} // end anonymous namespace
141
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000142#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000143#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000144#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000145
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000146AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +0000147 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
148 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000149 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000150 TRI(*STI.getRegisterInfo()), RBI(RBI),
151#define GET_GLOBALISEL_PREDICATES_INIT
152#include "AArch64GenGlobalISel.inc"
153#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000154#define GET_GLOBALISEL_TEMPORARIES_INIT
155#include "AArch64GenGlobalISel.inc"
156#undef GET_GLOBALISEL_TEMPORARIES_INIT
157{
158}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000159
Tim Northoverfb8d9892016-10-12 22:49:15 +0000160// FIXME: This should be target-independent, inferred from the types declared
161// for each class in the bank.
162static const TargetRegisterClass *
163getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
Amara Emerson3838ed02018-02-02 18:03:30 +0000164 const RegisterBankInfo &RBI,
165 bool GetAllRegSet = false) {
Tim Northoverfb8d9892016-10-12 22:49:15 +0000166 if (RB.getID() == AArch64::GPRRegBankID) {
167 if (Ty.getSizeInBits() <= 32)
Amara Emerson3838ed02018-02-02 18:03:30 +0000168 return GetAllRegSet ? &AArch64::GPR32allRegClass
169 : &AArch64::GPR32RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000170 if (Ty.getSizeInBits() == 64)
Amara Emerson3838ed02018-02-02 18:03:30 +0000171 return GetAllRegSet ? &AArch64::GPR64allRegClass
172 : &AArch64::GPR64RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000173 return nullptr;
174 }
175
176 if (RB.getID() == AArch64::FPRRegBankID) {
Amara Emerson3838ed02018-02-02 18:03:30 +0000177 if (Ty.getSizeInBits() <= 16)
178 return &AArch64::FPR16RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000179 if (Ty.getSizeInBits() == 32)
180 return &AArch64::FPR32RegClass;
181 if (Ty.getSizeInBits() == 64)
182 return &AArch64::FPR64RegClass;
183 if (Ty.getSizeInBits() == 128)
184 return &AArch64::FPR128RegClass;
185 return nullptr;
186 }
187
188 return nullptr;
189}
190
Jessica Paquette245047d2019-01-24 22:00:41 +0000191/// Given a register bank, and size in bits, return the smallest register class
192/// that can represent that combination.
Benjamin Kramer711950c2019-02-11 15:16:21 +0000193static const TargetRegisterClass *
194getMinClassForRegBank(const RegisterBank &RB, unsigned SizeInBits,
195 bool GetAllRegSet = false) {
Jessica Paquette245047d2019-01-24 22:00:41 +0000196 unsigned RegBankID = RB.getID();
197
198 if (RegBankID == AArch64::GPRRegBankID) {
199 if (SizeInBits <= 32)
200 return GetAllRegSet ? &AArch64::GPR32allRegClass
201 : &AArch64::GPR32RegClass;
202 if (SizeInBits == 64)
203 return GetAllRegSet ? &AArch64::GPR64allRegClass
204 : &AArch64::GPR64RegClass;
205 }
206
207 if (RegBankID == AArch64::FPRRegBankID) {
208 switch (SizeInBits) {
209 default:
210 return nullptr;
211 case 8:
212 return &AArch64::FPR8RegClass;
213 case 16:
214 return &AArch64::FPR16RegClass;
215 case 32:
216 return &AArch64::FPR32RegClass;
217 case 64:
218 return &AArch64::FPR64RegClass;
219 case 128:
220 return &AArch64::FPR128RegClass;
221 }
222 }
223
224 return nullptr;
225}
226
227/// Returns the correct subregister to use for a given register class.
228static bool getSubRegForClass(const TargetRegisterClass *RC,
229 const TargetRegisterInfo &TRI, unsigned &SubReg) {
230 switch (TRI.getRegSizeInBits(*RC)) {
231 case 8:
232 SubReg = AArch64::bsub;
233 break;
234 case 16:
235 SubReg = AArch64::hsub;
236 break;
237 case 32:
238 if (RC == &AArch64::GPR32RegClass)
239 SubReg = AArch64::sub_32;
240 else
241 SubReg = AArch64::ssub;
242 break;
243 case 64:
244 SubReg = AArch64::dsub;
245 break;
246 default:
247 LLVM_DEBUG(
248 dbgs() << "Couldn't find appropriate subregister for register class.");
249 return false;
250 }
251
252 return true;
253}
254
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000255/// Check whether \p I is a currently unsupported binary operation:
256/// - it has an unsized type
257/// - an operand is not a vreg
258/// - all operands are not in the same bank
259/// These are checks that should someday live in the verifier, but right now,
260/// these are mostly limitations of the aarch64 selector.
261static bool unsupportedBinOp(const MachineInstr &I,
262 const AArch64RegisterBankInfo &RBI,
263 const MachineRegisterInfo &MRI,
264 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000265 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000266 if (!Ty.isValid()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000267 LLVM_DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000268 return true;
269 }
270
271 const RegisterBank *PrevOpBank = nullptr;
272 for (auto &MO : I.operands()) {
273 // FIXME: Support non-register operands.
274 if (!MO.isReg()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000275 LLVM_DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000276 return true;
277 }
278
279 // FIXME: Can generic operations have physical registers operands? If
280 // so, this will need to be taught about that, and we'll need to get the
281 // bank out of the minimal class for the register.
282 // Either way, this needs to be documented (and possibly verified).
283 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000284 LLVM_DEBUG(dbgs() << "Generic inst has physical register operand\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000285 return true;
286 }
287
288 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
289 if (!OpBank) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000290 LLVM_DEBUG(dbgs() << "Generic register has no bank or class\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000291 return true;
292 }
293
294 if (PrevOpBank && OpBank != PrevOpBank) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000295 LLVM_DEBUG(dbgs() << "Generic inst operands have different banks\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000296 return true;
297 }
298 PrevOpBank = OpBank;
299 }
300 return false;
301}
302
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000303/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000304/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000305/// and of size \p OpSize.
306/// \returns \p GenericOpc if the combination is unsupported.
307static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
308 unsigned OpSize) {
309 switch (RegBankID) {
310 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000311 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000312 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000313 case TargetOpcode::G_SHL:
314 return AArch64::LSLVWr;
315 case TargetOpcode::G_LSHR:
316 return AArch64::LSRVWr;
317 case TargetOpcode::G_ASHR:
318 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000319 default:
320 return GenericOpc;
321 }
Tim Northover55782222016-10-18 20:03:48 +0000322 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000323 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000324 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000325 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000326 case TargetOpcode::G_SHL:
327 return AArch64::LSLVXr;
328 case TargetOpcode::G_LSHR:
329 return AArch64::LSRVXr;
330 case TargetOpcode::G_ASHR:
331 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000332 default:
333 return GenericOpc;
334 }
335 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000336 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000337 case AArch64::FPRRegBankID:
338 switch (OpSize) {
339 case 32:
340 switch (GenericOpc) {
341 case TargetOpcode::G_FADD:
342 return AArch64::FADDSrr;
343 case TargetOpcode::G_FSUB:
344 return AArch64::FSUBSrr;
345 case TargetOpcode::G_FMUL:
346 return AArch64::FMULSrr;
347 case TargetOpcode::G_FDIV:
348 return AArch64::FDIVSrr;
349 default:
350 return GenericOpc;
351 }
352 case 64:
353 switch (GenericOpc) {
354 case TargetOpcode::G_FADD:
355 return AArch64::FADDDrr;
356 case TargetOpcode::G_FSUB:
357 return AArch64::FSUBDrr;
358 case TargetOpcode::G_FMUL:
359 return AArch64::FMULDrr;
360 case TargetOpcode::G_FDIV:
361 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000362 case TargetOpcode::G_OR:
363 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000364 default:
365 return GenericOpc;
366 }
367 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000368 break;
369 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000370 return GenericOpc;
371}
372
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000373/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
374/// appropriate for the (value) register bank \p RegBankID and of memory access
375/// size \p OpSize. This returns the variant with the base+unsigned-immediate
376/// addressing mode (e.g., LDRXui).
377/// \returns \p GenericOpc if the combination is unsupported.
378static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
379 unsigned OpSize) {
380 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
381 switch (RegBankID) {
382 case AArch64::GPRRegBankID:
383 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000384 case 8:
385 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
386 case 16:
387 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000388 case 32:
389 return isStore ? AArch64::STRWui : AArch64::LDRWui;
390 case 64:
391 return isStore ? AArch64::STRXui : AArch64::LDRXui;
392 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000393 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000394 case AArch64::FPRRegBankID:
395 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000396 case 8:
397 return isStore ? AArch64::STRBui : AArch64::LDRBui;
398 case 16:
399 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000400 case 32:
401 return isStore ? AArch64::STRSui : AArch64::LDRSui;
402 case 64:
403 return isStore ? AArch64::STRDui : AArch64::LDRDui;
404 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000405 break;
406 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000407 return GenericOpc;
408}
409
Benjamin Kramer1411ecf2019-01-24 23:39:47 +0000410#ifndef NDEBUG
Jessica Paquette245047d2019-01-24 22:00:41 +0000411/// Helper function that verifies that we have a valid copy at the end of
412/// selectCopy. Verifies that the source and dest have the expected sizes and
413/// then returns true.
414static bool isValidCopy(const MachineInstr &I, const RegisterBank &DstBank,
415 const MachineRegisterInfo &MRI,
416 const TargetRegisterInfo &TRI,
417 const RegisterBankInfo &RBI) {
418 const unsigned DstReg = I.getOperand(0).getReg();
419 const unsigned SrcReg = I.getOperand(1).getReg();
420 const unsigned DstSize = RBI.getSizeInBits(DstReg, MRI, TRI);
421 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
Amara Emersondb211892018-02-20 05:11:57 +0000422
Jessica Paquette245047d2019-01-24 22:00:41 +0000423 // Make sure the size of the source and dest line up.
424 assert(
425 (DstSize == SrcSize ||
426 // Copies are a mean to setup initial types, the number of
427 // bits may not exactly match.
428 (TargetRegisterInfo::isPhysicalRegister(SrcReg) && DstSize <= SrcSize) ||
429 // Copies are a mean to copy bits around, as long as we are
430 // on the same register class, that's fine. Otherwise, that
431 // means we need some SUBREG_TO_REG or AND & co.
432 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
433 "Copy with different width?!");
434
435 // Check the size of the destination.
436 assert((DstSize <= 64 || DstBank.getID() == AArch64::FPRRegBankID) &&
437 "GPRs cannot get more than 64-bit width values");
438
439 return true;
440}
Benjamin Kramer1411ecf2019-01-24 23:39:47 +0000441#endif
Jessica Paquette245047d2019-01-24 22:00:41 +0000442
443/// Helper function for selectCopy. Inserts a subregister copy from
444/// \p *From to \p *To, linking it up to \p I.
445///
446/// e.g, given I = "Dst = COPY SrcReg", we'll transform that into
447///
448/// CopyReg (From class) = COPY SrcReg
449/// SubRegCopy (To class) = COPY CopyReg:SubReg
450/// Dst = COPY SubRegCopy
451static bool selectSubregisterCopy(MachineInstr &I, const TargetInstrInfo &TII,
452 MachineRegisterInfo &MRI,
453 const RegisterBankInfo &RBI, unsigned SrcReg,
454 const TargetRegisterClass *From,
455 const TargetRegisterClass *To,
456 unsigned SubReg) {
457 unsigned CopyReg = MRI.createVirtualRegister(From);
458 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::COPY), CopyReg)
459 .addUse(SrcReg);
460 unsigned SubRegCopy = MRI.createVirtualRegister(To);
461 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
462 SubRegCopy)
463 .addUse(CopyReg, 0, SubReg);
Amara Emersondb211892018-02-20 05:11:57 +0000464 MachineOperand &RegOp = I.getOperand(1);
465 RegOp.setReg(SubRegCopy);
Jessica Paquette245047d2019-01-24 22:00:41 +0000466
467 // It's possible that the destination register won't be constrained. Make
468 // sure that happens.
469 if (!TargetRegisterInfo::isPhysicalRegister(I.getOperand(0).getReg()))
470 RBI.constrainGenericRegister(I.getOperand(0).getReg(), *To, MRI);
471
Amara Emersondb211892018-02-20 05:11:57 +0000472 return true;
473}
474
Quentin Colombetcb629a82016-10-12 03:57:49 +0000475static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
476 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
477 const RegisterBankInfo &RBI) {
478
479 unsigned DstReg = I.getOperand(0).getReg();
Amara Emersondb211892018-02-20 05:11:57 +0000480 unsigned SrcReg = I.getOperand(1).getReg();
Jessica Paquette245047d2019-01-24 22:00:41 +0000481 const RegisterBank &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
482 const RegisterBank &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
483 const TargetRegisterClass *DstRC = getMinClassForRegBank(
484 DstRegBank, RBI.getSizeInBits(DstReg, MRI, TRI), true);
485 if (!DstRC) {
486 LLVM_DEBUG(dbgs() << "Unexpected dest size "
487 << RBI.getSizeInBits(DstReg, MRI, TRI) << '\n');
Amara Emerson3838ed02018-02-02 18:03:30 +0000488 return false;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000489 }
490
Jessica Paquette245047d2019-01-24 22:00:41 +0000491 // A couple helpers below, for making sure that the copy we produce is valid.
492
493 // Set to true if we insert a SUBREG_TO_REG. If we do this, then we don't want
494 // to verify that the src and dst are the same size, since that's handled by
495 // the SUBREG_TO_REG.
496 bool KnownValid = false;
497
498 // Returns true, or asserts if something we don't expect happens. Instead of
499 // returning true, we return isValidCopy() to ensure that we verify the
500 // result.
Jessica Paquette76c40f82019-01-24 22:51:31 +0000501 auto CheckCopy = [&]() {
Jessica Paquette245047d2019-01-24 22:00:41 +0000502 // If we have a bitcast or something, we can't have physical registers.
503 assert(
Simon Pilgrimdea61742019-01-25 11:38:40 +0000504 (I.isCopy() ||
505 (!TargetRegisterInfo::isPhysicalRegister(I.getOperand(0).getReg()) &&
506 !TargetRegisterInfo::isPhysicalRegister(I.getOperand(1).getReg()))) &&
507 "No phys reg on generic operator!");
Jessica Paquette245047d2019-01-24 22:00:41 +0000508 assert(KnownValid || isValidCopy(I, DstRegBank, MRI, TRI, RBI));
Jonas Hahnfeld65a401f2019-03-04 08:51:32 +0000509 (void)KnownValid;
Jessica Paquette245047d2019-01-24 22:00:41 +0000510 return true;
511 };
512
513 // Is this a copy? If so, then we may need to insert a subregister copy, or
514 // a SUBREG_TO_REG.
515 if (I.isCopy()) {
516 // Yes. Check if there's anything to fix up.
517 const TargetRegisterClass *SrcRC = getMinClassForRegBank(
518 SrcRegBank, RBI.getSizeInBits(SrcReg, MRI, TRI), true);
Amara Emerson7e9f3482018-02-18 17:10:49 +0000519 if (!SrcRC) {
Jessica Paquette245047d2019-01-24 22:00:41 +0000520 LLVM_DEBUG(dbgs() << "Couldn't determine source register class\n");
521 return false;
Amara Emerson7e9f3482018-02-18 17:10:49 +0000522 }
Jessica Paquette245047d2019-01-24 22:00:41 +0000523
524 // Is this a cross-bank copy?
525 if (DstRegBank.getID() != SrcRegBank.getID()) {
526 // If we're doing a cross-bank copy on different-sized registers, we need
527 // to do a bit more work.
528 unsigned SrcSize = TRI.getRegSizeInBits(*SrcRC);
529 unsigned DstSize = TRI.getRegSizeInBits(*DstRC);
530
531 if (SrcSize > DstSize) {
532 // We're doing a cross-bank copy into a smaller register. We need a
533 // subregister copy. First, get a register class that's on the same bank
534 // as the destination, but the same size as the source.
535 const TargetRegisterClass *SubregRC =
536 getMinClassForRegBank(DstRegBank, SrcSize, true);
537 assert(SubregRC && "Didn't get a register class for subreg?");
538
539 // Get the appropriate subregister for the destination.
540 unsigned SubReg = 0;
541 if (!getSubRegForClass(DstRC, TRI, SubReg)) {
542 LLVM_DEBUG(dbgs() << "Couldn't determine subregister for copy.\n");
543 return false;
544 }
545
546 // Now, insert a subregister copy using the new register class.
547 selectSubregisterCopy(I, TII, MRI, RBI, SrcReg, SubregRC, DstRC,
548 SubReg);
549 return CheckCopy();
550 }
551
552 else if (DstRegBank.getID() == AArch64::GPRRegBankID && DstSize == 32 &&
553 SrcSize == 16) {
554 // Special case for FPR16 to GPR32.
555 // FIXME: This can probably be generalized like the above case.
556 unsigned PromoteReg =
557 MRI.createVirtualRegister(&AArch64::FPR32RegClass);
558 BuildMI(*I.getParent(), I, I.getDebugLoc(),
559 TII.get(AArch64::SUBREG_TO_REG), PromoteReg)
560 .addImm(0)
561 .addUse(SrcReg)
562 .addImm(AArch64::hsub);
563 MachineOperand &RegOp = I.getOperand(1);
564 RegOp.setReg(PromoteReg);
565
566 // Promise that the copy is implicitly validated by the SUBREG_TO_REG.
567 KnownValid = true;
568 }
Amara Emerson7e9f3482018-02-18 17:10:49 +0000569 }
Jessica Paquette245047d2019-01-24 22:00:41 +0000570
571 // If the destination is a physical register, then there's nothing to
572 // change, so we're done.
573 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
574 return CheckCopy();
Amara Emerson7e9f3482018-02-18 17:10:49 +0000575 }
576
Jessica Paquette245047d2019-01-24 22:00:41 +0000577 // No need to constrain SrcReg. It will get constrained when we hit another
578 // of its use or its defs. Copies do not have constraints.
579 if (!RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000580 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
581 << " operand\n");
Quentin Colombetcb629a82016-10-12 03:57:49 +0000582 return false;
583 }
584 I.setDesc(TII.get(AArch64::COPY));
Jessica Paquette245047d2019-01-24 22:00:41 +0000585 return CheckCopy();
Quentin Colombetcb629a82016-10-12 03:57:49 +0000586}
587
Tim Northover69271c62016-10-12 22:49:11 +0000588static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
589 if (!DstTy.isScalar() || !SrcTy.isScalar())
590 return GenericOpc;
591
592 const unsigned DstSize = DstTy.getSizeInBits();
593 const unsigned SrcSize = SrcTy.getSizeInBits();
594
595 switch (DstSize) {
596 case 32:
597 switch (SrcSize) {
598 case 32:
599 switch (GenericOpc) {
600 case TargetOpcode::G_SITOFP:
601 return AArch64::SCVTFUWSri;
602 case TargetOpcode::G_UITOFP:
603 return AArch64::UCVTFUWSri;
604 case TargetOpcode::G_FPTOSI:
605 return AArch64::FCVTZSUWSr;
606 case TargetOpcode::G_FPTOUI:
607 return AArch64::FCVTZUUWSr;
608 default:
609 return GenericOpc;
610 }
611 case 64:
612 switch (GenericOpc) {
613 case TargetOpcode::G_SITOFP:
614 return AArch64::SCVTFUXSri;
615 case TargetOpcode::G_UITOFP:
616 return AArch64::UCVTFUXSri;
617 case TargetOpcode::G_FPTOSI:
618 return AArch64::FCVTZSUWDr;
619 case TargetOpcode::G_FPTOUI:
620 return AArch64::FCVTZUUWDr;
621 default:
622 return GenericOpc;
623 }
624 default:
625 return GenericOpc;
626 }
627 case 64:
628 switch (SrcSize) {
629 case 32:
630 switch (GenericOpc) {
631 case TargetOpcode::G_SITOFP:
632 return AArch64::SCVTFUWDri;
633 case TargetOpcode::G_UITOFP:
634 return AArch64::UCVTFUWDri;
635 case TargetOpcode::G_FPTOSI:
636 return AArch64::FCVTZSUXSr;
637 case TargetOpcode::G_FPTOUI:
638 return AArch64::FCVTZUUXSr;
639 default:
640 return GenericOpc;
641 }
642 case 64:
643 switch (GenericOpc) {
644 case TargetOpcode::G_SITOFP:
645 return AArch64::SCVTFUXDri;
646 case TargetOpcode::G_UITOFP:
647 return AArch64::UCVTFUXDri;
648 case TargetOpcode::G_FPTOSI:
649 return AArch64::FCVTZSUXDr;
650 case TargetOpcode::G_FPTOUI:
651 return AArch64::FCVTZUUXDr;
652 default:
653 return GenericOpc;
654 }
655 default:
656 return GenericOpc;
657 }
658 default:
659 return GenericOpc;
660 };
661 return GenericOpc;
662}
663
Tim Northover6c02ad52016-10-12 22:49:04 +0000664static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
665 switch (P) {
666 default:
667 llvm_unreachable("Unknown condition code!");
668 case CmpInst::ICMP_NE:
669 return AArch64CC::NE;
670 case CmpInst::ICMP_EQ:
671 return AArch64CC::EQ;
672 case CmpInst::ICMP_SGT:
673 return AArch64CC::GT;
674 case CmpInst::ICMP_SGE:
675 return AArch64CC::GE;
676 case CmpInst::ICMP_SLT:
677 return AArch64CC::LT;
678 case CmpInst::ICMP_SLE:
679 return AArch64CC::LE;
680 case CmpInst::ICMP_UGT:
681 return AArch64CC::HI;
682 case CmpInst::ICMP_UGE:
683 return AArch64CC::HS;
684 case CmpInst::ICMP_ULT:
685 return AArch64CC::LO;
686 case CmpInst::ICMP_ULE:
687 return AArch64CC::LS;
688 }
689}
690
Tim Northover7dd378d2016-10-12 22:49:07 +0000691static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
692 AArch64CC::CondCode &CondCode,
693 AArch64CC::CondCode &CondCode2) {
694 CondCode2 = AArch64CC::AL;
695 switch (P) {
696 default:
697 llvm_unreachable("Unknown FP condition!");
698 case CmpInst::FCMP_OEQ:
699 CondCode = AArch64CC::EQ;
700 break;
701 case CmpInst::FCMP_OGT:
702 CondCode = AArch64CC::GT;
703 break;
704 case CmpInst::FCMP_OGE:
705 CondCode = AArch64CC::GE;
706 break;
707 case CmpInst::FCMP_OLT:
708 CondCode = AArch64CC::MI;
709 break;
710 case CmpInst::FCMP_OLE:
711 CondCode = AArch64CC::LS;
712 break;
713 case CmpInst::FCMP_ONE:
714 CondCode = AArch64CC::MI;
715 CondCode2 = AArch64CC::GT;
716 break;
717 case CmpInst::FCMP_ORD:
718 CondCode = AArch64CC::VC;
719 break;
720 case CmpInst::FCMP_UNO:
721 CondCode = AArch64CC::VS;
722 break;
723 case CmpInst::FCMP_UEQ:
724 CondCode = AArch64CC::EQ;
725 CondCode2 = AArch64CC::VS;
726 break;
727 case CmpInst::FCMP_UGT:
728 CondCode = AArch64CC::HI;
729 break;
730 case CmpInst::FCMP_UGE:
731 CondCode = AArch64CC::PL;
732 break;
733 case CmpInst::FCMP_ULT:
734 CondCode = AArch64CC::LT;
735 break;
736 case CmpInst::FCMP_ULE:
737 CondCode = AArch64CC::LE;
738 break;
739 case CmpInst::FCMP_UNE:
740 CondCode = AArch64CC::NE;
741 break;
742 }
743}
744
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000745bool AArch64InstructionSelector::selectCompareBranch(
746 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
747
748 const unsigned CondReg = I.getOperand(0).getReg();
749 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
750 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000751 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
752 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000753 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
754 return false;
755
756 unsigned LHS = CCMI->getOperand(2).getReg();
757 unsigned RHS = CCMI->getOperand(3).getReg();
758 if (!getConstantVRegVal(RHS, MRI))
759 std::swap(RHS, LHS);
760
761 const auto RHSImm = getConstantVRegVal(RHS, MRI);
762 if (!RHSImm || *RHSImm != 0)
763 return false;
764
765 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
766 if (RB.getID() != AArch64::GPRRegBankID)
767 return false;
768
769 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
770 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
771 return false;
772
773 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
774 unsigned CBOpc = 0;
775 if (CmpWidth <= 32)
776 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
777 else if (CmpWidth == 64)
778 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
779 else
780 return false;
781
Aditya Nandakumar18b3f9d2018-01-17 19:31:33 +0000782 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
783 .addUse(LHS)
784 .addMBB(DestMBB)
785 .constrainAllUses(TII, TRI, RBI);
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000786
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000787 I.eraseFromParent();
788 return true;
789}
790
Tim Northovere9600d82017-02-08 17:57:27 +0000791bool AArch64InstructionSelector::selectVaStartAAPCS(
792 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
793 return false;
794}
795
796bool AArch64InstructionSelector::selectVaStartDarwin(
797 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
798 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
799 unsigned ListReg = I.getOperand(0).getReg();
800
801 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
802
803 auto MIB =
804 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
805 .addDef(ArgsAddrReg)
806 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
807 .addImm(0)
808 .addImm(0);
809
810 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
811
812 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
813 .addUse(ArgsAddrReg)
814 .addUse(ListReg)
815 .addImm(0)
816 .addMemOperand(*I.memoperands_begin());
817
818 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
819 I.eraseFromParent();
820 return true;
821}
822
Amara Emerson1e8c1642018-07-31 00:09:02 +0000823void AArch64InstructionSelector::materializeLargeCMVal(
824 MachineInstr &I, const Value *V, unsigned char OpFlags) const {
825 MachineBasicBlock &MBB = *I.getParent();
826 MachineFunction &MF = *MBB.getParent();
827 MachineRegisterInfo &MRI = MF.getRegInfo();
828 MachineIRBuilder MIB(I);
829
Aditya Nandakumarcef44a22018-12-11 00:48:50 +0000830 auto MovZ = MIB.buildInstr(AArch64::MOVZXi, {&AArch64::GPR64RegClass}, {});
Amara Emerson1e8c1642018-07-31 00:09:02 +0000831 MovZ->addOperand(MF, I.getOperand(1));
832 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
833 AArch64II::MO_NC);
834 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
835 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
836
837 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags, unsigned Offset,
838 unsigned ForceDstReg) {
839 unsigned DstReg = ForceDstReg
840 ? ForceDstReg
841 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
842 auto MovI = MIB.buildInstr(AArch64::MOVKXi).addDef(DstReg).addUse(SrcReg);
843 if (auto *GV = dyn_cast<GlobalValue>(V)) {
844 MovI->addOperand(MF, MachineOperand::CreateGA(
845 GV, MovZ->getOperand(1).getOffset(), Flags));
846 } else {
847 MovI->addOperand(
848 MF, MachineOperand::CreateBA(cast<BlockAddress>(V),
849 MovZ->getOperand(1).getOffset(), Flags));
850 }
851 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
852 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
853 return DstReg;
854 };
Aditya Nandakumarfef76192019-02-05 22:14:40 +0000855 unsigned DstReg = BuildMovK(MovZ.getReg(0),
Amara Emerson1e8c1642018-07-31 00:09:02 +0000856 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
857 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
858 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
859 return;
860}
861
Daniel Sandersf76f3152017-11-16 00:46:35 +0000862bool AArch64InstructionSelector::select(MachineInstr &I,
863 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000864 assert(I.getParent() && "Instruction should be in a basic block!");
865 assert(I.getParent()->getParent() && "Instruction should be in a function!");
866
867 MachineBasicBlock &MBB = *I.getParent();
868 MachineFunction &MF = *MBB.getParent();
869 MachineRegisterInfo &MRI = MF.getRegInfo();
870
Tim Northovercdf23f12016-10-31 18:30:59 +0000871 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000872 // G_PHI requires same handling as PHI
873 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000874 // Certain non-generic instructions also need some special handling.
875
876 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
877 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000878
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000879 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000880 const unsigned DefReg = I.getOperand(0).getReg();
881 const LLT DefTy = MRI.getType(DefReg);
882
883 const TargetRegisterClass *DefRC = nullptr;
884 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
885 DefRC = TRI.getRegClass(DefReg);
886 } else {
887 const RegClassOrRegBank &RegClassOrBank =
888 MRI.getRegClassOrRegBank(DefReg);
889
890 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
891 if (!DefRC) {
892 if (!DefTy.isValid()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000893 LLVM_DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
Tim Northover7d88da62016-11-08 00:34:06 +0000894 return false;
895 }
896 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
897 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
898 if (!DefRC) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000899 LLVM_DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
Tim Northover7d88da62016-11-08 00:34:06 +0000900 return false;
901 }
902 }
903 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000904 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000905
906 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
907 }
908
909 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000910 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000911
912 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000913 }
914
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000915
916 if (I.getNumOperands() != I.getNumExplicitOperands()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000917 LLVM_DEBUG(
918 dbgs() << "Generic instruction has unexpected implicit operands\n");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000919 return false;
920 }
921
Daniel Sandersf76f3152017-11-16 00:46:35 +0000922 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000923 return true;
924
Tim Northover32a078a2016-09-15 10:09:59 +0000925 LLT Ty =
926 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000927
Tim Northover69271c62016-10-12 22:49:11 +0000928 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000929 case TargetOpcode::G_BRCOND: {
930 if (Ty.getSizeInBits() > 32) {
931 // We shouldn't need this on AArch64, but it would be implemented as an
932 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
933 // bit being tested is < 32.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000934 LLVM_DEBUG(dbgs() << "G_BRCOND has type: " << Ty
935 << ", expected at most 32-bits");
Tim Northover5e3dbf32016-10-12 22:49:01 +0000936 return false;
937 }
938
939 const unsigned CondReg = I.getOperand(0).getReg();
940 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
941
Kristof Beylse66bc1f2018-12-18 08:50:02 +0000942 // Speculation tracking/SLH assumes that optimized TB(N)Z/CB(N)Z
943 // instructions will not be produced, as they are conditional branch
944 // instructions that do not set flags.
945 bool ProduceNonFlagSettingCondBr =
946 !MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening);
947 if (ProduceNonFlagSettingCondBr && selectCompareBranch(I, MF, MRI))
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000948 return true;
949
Kristof Beylse66bc1f2018-12-18 08:50:02 +0000950 if (ProduceNonFlagSettingCondBr) {
951 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
952 .addUse(CondReg)
953 .addImm(/*bit offset=*/0)
954 .addMBB(DestMBB);
Tim Northover5e3dbf32016-10-12 22:49:01 +0000955
Kristof Beylse66bc1f2018-12-18 08:50:02 +0000956 I.eraseFromParent();
957 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
958 } else {
959 auto CMP = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
960 .addDef(AArch64::WZR)
961 .addUse(CondReg)
962 .addImm(1);
963 constrainSelectedInstRegOperands(*CMP.getInstr(), TII, TRI, RBI);
964 auto Bcc =
965 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::Bcc))
966 .addImm(AArch64CC::EQ)
967 .addMBB(DestMBB);
968
969 I.eraseFromParent();
970 return constrainSelectedInstRegOperands(*Bcc.getInstr(), TII, TRI, RBI);
971 }
Tim Northover5e3dbf32016-10-12 22:49:01 +0000972 }
973
Kristof Beyls65a12c02017-01-30 09:13:18 +0000974 case TargetOpcode::G_BRINDIRECT: {
975 I.setDesc(TII.get(AArch64::BR));
976 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
977 }
978
Tim Northover4494d692016-10-18 19:47:57 +0000979 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000980 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000981 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
982
983 const LLT s32 = LLT::scalar(32);
984 const LLT s64 = LLT::scalar(64);
985 const LLT p0 = LLT::pointer(0, 64);
986
987 const unsigned DefReg = I.getOperand(0).getReg();
988 const LLT DefTy = MRI.getType(DefReg);
989 const unsigned DefSize = DefTy.getSizeInBits();
990 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
991
992 // FIXME: Redundant check, but even less readable when factored out.
993 if (isFP) {
994 if (Ty != s32 && Ty != s64) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000995 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
996 << " constant, expected: " << s32 << " or " << s64
997 << '\n');
Tim Northover4494d692016-10-18 19:47:57 +0000998 return false;
999 }
1000
1001 if (RB.getID() != AArch64::FPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001002 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
1003 << " constant on bank: " << RB
1004 << ", expected: FPR\n");
Tim Northover4494d692016-10-18 19:47:57 +00001005 return false;
1006 }
Daniel Sanders11300ce2017-10-13 21:28:03 +00001007
1008 // The case when we have 0.0 is covered by tablegen. Reject it here so we
1009 // can be sure tablegen works correctly and isn't rescued by this code.
1010 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
1011 return false;
Tim Northover4494d692016-10-18 19:47:57 +00001012 } else {
Daniel Sanders05540042017-08-08 10:44:31 +00001013 // s32 and s64 are covered by tablegen.
1014 if (Ty != p0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001015 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
1016 << " constant, expected: " << s32 << ", " << s64
1017 << ", or " << p0 << '\n');
Tim Northover4494d692016-10-18 19:47:57 +00001018 return false;
1019 }
1020
1021 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001022 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
1023 << " constant on bank: " << RB
1024 << ", expected: GPR\n");
Tim Northover4494d692016-10-18 19:47:57 +00001025 return false;
1026 }
1027 }
1028
1029 const unsigned MovOpc =
1030 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
1031
1032 I.setDesc(TII.get(MovOpc));
1033
1034 if (isFP) {
1035 const TargetRegisterClass &GPRRC =
1036 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
1037 const TargetRegisterClass &FPRRC =
1038 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
1039
1040 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
1041 MachineOperand &RegOp = I.getOperand(0);
1042 RegOp.setReg(DefGPRReg);
1043
1044 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
1045 TII.get(AArch64::COPY))
1046 .addDef(DefReg)
1047 .addUse(DefGPRReg);
1048
1049 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001050 LLVM_DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
Tim Northover4494d692016-10-18 19:47:57 +00001051 return false;
1052 }
1053
1054 MachineOperand &ImmOp = I.getOperand(1);
1055 // FIXME: Is going through int64_t always correct?
1056 ImmOp.ChangeToImmediate(
1057 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001058 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +00001059 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
1060 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001061 } else if (I.getOperand(1).isImm()) {
1062 uint64_t Val = I.getOperand(1).getImm();
1063 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +00001064 }
1065
1066 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1067 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +00001068 }
Tim Northover7b6d66c2017-07-20 22:58:38 +00001069 case TargetOpcode::G_EXTRACT: {
1070 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +00001071 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
Amara Emerson242efdb2018-02-18 17:28:34 +00001072 (void)DstTy;
Amara Emersonbc03bae2018-02-18 17:03:02 +00001073 unsigned SrcSize = SrcTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +00001074 // Larger extracts are vectors, same-size extracts should be something else
1075 // by now (either split up or simplified to a COPY).
1076 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
1077 return false;
1078
Amara Emersonbc03bae2018-02-18 17:03:02 +00001079 I.setDesc(TII.get(SrcSize == 64 ? AArch64::UBFMXri : AArch64::UBFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +00001080 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
1081 Ty.getSizeInBits() - 1);
1082
Amara Emersonbc03bae2018-02-18 17:03:02 +00001083 if (SrcSize < 64) {
1084 assert(SrcSize == 32 && DstTy.getSizeInBits() == 16 &&
1085 "unexpected G_EXTRACT types");
1086 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1087 }
1088
Tim Northover7b6d66c2017-07-20 22:58:38 +00001089 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
1090 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
1091 TII.get(AArch64::COPY))
1092 .addDef(I.getOperand(0).getReg())
1093 .addUse(DstReg, 0, AArch64::sub_32);
1094 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
1095 AArch64::GPR32RegClass, MRI);
1096 I.getOperand(0).setReg(DstReg);
1097
1098 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1099 }
1100
1101 case TargetOpcode::G_INSERT: {
1102 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +00001103 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1104 unsigned DstSize = DstTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +00001105 // Larger inserts are vectors, same-size ones should be something else by
1106 // now (split up or turned into COPYs).
1107 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
1108 return false;
1109
Amara Emersonbc03bae2018-02-18 17:03:02 +00001110 I.setDesc(TII.get(DstSize == 64 ? AArch64::BFMXri : AArch64::BFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +00001111 unsigned LSB = I.getOperand(3).getImm();
1112 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
Amara Emersonbc03bae2018-02-18 17:03:02 +00001113 I.getOperand(3).setImm((DstSize - LSB) % DstSize);
Tim Northover7b6d66c2017-07-20 22:58:38 +00001114 MachineInstrBuilder(MF, I).addImm(Width - 1);
1115
Amara Emersonbc03bae2018-02-18 17:03:02 +00001116 if (DstSize < 64) {
1117 assert(DstSize == 32 && SrcTy.getSizeInBits() == 16 &&
1118 "unexpected G_INSERT types");
1119 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1120 }
1121
Tim Northover7b6d66c2017-07-20 22:58:38 +00001122 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
1123 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
1124 TII.get(AArch64::SUBREG_TO_REG))
1125 .addDef(SrcReg)
1126 .addImm(0)
1127 .addUse(I.getOperand(2).getReg())
1128 .addImm(AArch64::sub_32);
1129 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
1130 AArch64::GPR32RegClass, MRI);
1131 I.getOperand(2).setReg(SrcReg);
1132
1133 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1134 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001135 case TargetOpcode::G_FRAME_INDEX: {
1136 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +00001137 if (Ty != LLT::pointer(0, 64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001138 LLVM_DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
1139 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001140 return false;
1141 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001142 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +00001143
1144 // MOs for a #0 shifted immediate.
1145 I.addOperand(MachineOperand::CreateImm(0));
1146 I.addOperand(MachineOperand::CreateImm(0));
1147
1148 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1149 }
Tim Northoverbdf16242016-10-10 21:50:00 +00001150
1151 case TargetOpcode::G_GLOBAL_VALUE: {
1152 auto GV = I.getOperand(1).getGlobal();
1153 if (GV->isThreadLocal()) {
1154 // FIXME: we don't support TLS yet.
1155 return false;
1156 }
1157 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +00001158 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +00001159 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +00001160 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +00001161 } else if (TM.getCodeModel() == CodeModel::Large) {
1162 // Materialize the global using movz/movk instructions.
Amara Emerson1e8c1642018-07-31 00:09:02 +00001163 materializeLargeCMVal(I, GV, OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +00001164 I.eraseFromParent();
1165 return true;
David Green9dd1d452018-08-22 11:31:39 +00001166 } else if (TM.getCodeModel() == CodeModel::Tiny) {
1167 I.setDesc(TII.get(AArch64::ADR));
1168 I.getOperand(1).setTargetFlags(OpFlags);
Tim Northoverfe7c59a2016-12-13 18:25:38 +00001169 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +00001170 I.setDesc(TII.get(AArch64::MOVaddr));
1171 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
1172 MachineInstrBuilder MIB(MF, I);
1173 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
1174 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
1175 }
1176 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1177 }
1178
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001179 case TargetOpcode::G_LOAD:
1180 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +00001181 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001182
Tim Northover5ae83502016-09-15 09:20:34 +00001183 if (PtrTy != LLT::pointer(0, 64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001184 LLVM_DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
1185 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001186 return false;
1187 }
1188
Daniel Sanders3c1c4c02017-12-05 05:52:07 +00001189 auto &MemOp = **I.memoperands_begin();
1190 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001191 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
Daniel Sanders3c1c4c02017-12-05 05:52:07 +00001192 return false;
1193 }
Daniel Sandersf84bc372018-05-05 20:53:24 +00001194 unsigned MemSizeInBits = MemOp.getSize() * 8;
Daniel Sanders3c1c4c02017-12-05 05:52:07 +00001195
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001196 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001197#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001198 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001199 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001200 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
1201 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +00001202 assert(MRI.getType(PtrReg).isPointer() &&
1203 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001204#endif
1205
1206 const unsigned ValReg = I.getOperand(0).getReg();
1207 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
1208
1209 const unsigned NewOpc =
Daniel Sandersf84bc372018-05-05 20:53:24 +00001210 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemSizeInBits);
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001211 if (NewOpc == I.getOpcode())
1212 return false;
1213
1214 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001215
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001216 uint64_t Offset = 0;
1217 auto *PtrMI = MRI.getVRegDef(PtrReg);
1218
1219 // Try to fold a GEP into our unsigned immediate addressing mode.
1220 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
1221 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
1222 int64_t Imm = *COff;
Daniel Sandersf84bc372018-05-05 20:53:24 +00001223 const unsigned Size = MemSizeInBits / 8;
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001224 const unsigned Scale = Log2_32(Size);
1225 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
1226 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
1227 I.getOperand(1).setReg(Ptr2Reg);
1228 PtrMI = MRI.getVRegDef(Ptr2Reg);
1229 Offset = Imm / Size;
1230 }
1231 }
1232 }
1233
Ahmed Bougachaf75782f2017-03-27 17:31:56 +00001234 // If we haven't folded anything into our addressing mode yet, try to fold
1235 // a frame index into the base+offset.
1236 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1237 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
1238
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001239 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +00001240
1241 // If we're storing a 0, use WZR/XZR.
1242 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
1243 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
1244 if (I.getOpcode() == AArch64::STRWui)
1245 I.getOperand(0).setReg(AArch64::WZR);
1246 else if (I.getOpcode() == AArch64::STRXui)
1247 I.getOperand(0).setReg(AArch64::XZR);
1248 }
1249 }
1250
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001251 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1252 }
1253
Tim Northover9dd78f82017-02-08 21:22:25 +00001254 case TargetOpcode::G_SMULH:
1255 case TargetOpcode::G_UMULH: {
1256 // Reject the various things we don't support yet.
1257 if (unsupportedBinOp(I, RBI, MRI, TRI))
1258 return false;
1259
1260 const unsigned DefReg = I.getOperand(0).getReg();
1261 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1262
1263 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001264 LLVM_DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
Tim Northover9dd78f82017-02-08 21:22:25 +00001265 return false;
1266 }
1267
1268 if (Ty != LLT::scalar(64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001269 LLVM_DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1270 << ", expected: " << LLT::scalar(64) << '\n');
Tim Northover9dd78f82017-02-08 21:22:25 +00001271 return false;
1272 }
1273
1274 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1275 : AArch64::UMULHrr;
1276 I.setDesc(TII.get(NewOpc));
1277
1278 // Now that we selected an opcode, we need to constrain the register
1279 // operands to use appropriate classes.
1280 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1281 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001282 case TargetOpcode::G_FADD:
1283 case TargetOpcode::G_FSUB:
1284 case TargetOpcode::G_FMUL:
1285 case TargetOpcode::G_FDIV:
1286
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001287 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001288 case TargetOpcode::G_SHL:
1289 case TargetOpcode::G_LSHR:
1290 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001291 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001292 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001293 if (unsupportedBinOp(I, RBI, MRI, TRI))
1294 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001295
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001296 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001297
1298 const unsigned DefReg = I.getOperand(0).getReg();
1299 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1300
1301 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1302 if (NewOpc == I.getOpcode())
1303 return false;
1304
1305 I.setDesc(TII.get(NewOpc));
1306 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001307
1308 // Now that we selected an opcode, we need to constrain the register
1309 // operands to use appropriate classes.
1310 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1311 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001312
Tim Northover398c5f52017-02-14 20:56:29 +00001313 case TargetOpcode::G_PTR_MASK: {
1314 uint64_t Align = I.getOperand(2).getImm();
1315 if (Align >= 64 || Align == 0)
1316 return false;
1317
1318 uint64_t Mask = ~((1ULL << Align) - 1);
1319 I.setDesc(TII.get(AArch64::ANDXri));
1320 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1321
1322 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1323 }
Tim Northover037af52c2016-10-31 18:31:09 +00001324 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001325 case TargetOpcode::G_TRUNC: {
1326 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1327 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1328
1329 const unsigned DstReg = I.getOperand(0).getReg();
1330 const unsigned SrcReg = I.getOperand(1).getReg();
1331
1332 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1333 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1334
1335 if (DstRB.getID() != SrcRB.getID()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001336 LLVM_DEBUG(
1337 dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001338 return false;
1339 }
1340
1341 if (DstRB.getID() == AArch64::GPRRegBankID) {
1342 const TargetRegisterClass *DstRC =
1343 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1344 if (!DstRC)
1345 return false;
1346
1347 const TargetRegisterClass *SrcRC =
1348 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1349 if (!SrcRC)
1350 return false;
1351
1352 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1353 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001354 LLVM_DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001355 return false;
1356 }
1357
1358 if (DstRC == SrcRC) {
1359 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001360 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1361 SrcTy == LLT::scalar(64)) {
1362 llvm_unreachable("TableGen can import this case");
1363 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001364 } else if (DstRC == &AArch64::GPR32RegClass &&
1365 SrcRC == &AArch64::GPR64RegClass) {
1366 I.getOperand(1).setSubReg(AArch64::sub_32);
1367 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001368 LLVM_DEBUG(
1369 dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001370 return false;
1371 }
1372
1373 I.setDesc(TII.get(TargetOpcode::COPY));
1374 return true;
1375 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1376 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1377 I.setDesc(TII.get(AArch64::XTNv4i16));
1378 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1379 return true;
1380 }
1381 }
1382
1383 return false;
1384 }
1385
Tim Northover3d38b3a2016-10-11 20:50:21 +00001386 case TargetOpcode::G_ANYEXT: {
1387 const unsigned DstReg = I.getOperand(0).getReg();
1388 const unsigned SrcReg = I.getOperand(1).getReg();
1389
Quentin Colombetcb629a82016-10-12 03:57:49 +00001390 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1391 if (RBDst.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001392 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst
1393 << ", expected: GPR\n");
Quentin Colombetcb629a82016-10-12 03:57:49 +00001394 return false;
1395 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001396
Quentin Colombetcb629a82016-10-12 03:57:49 +00001397 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1398 if (RBSrc.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001399 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc
1400 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001401 return false;
1402 }
1403
1404 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1405
1406 if (DstSize == 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001407 LLVM_DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001408 return false;
1409 }
1410
Quentin Colombetcb629a82016-10-12 03:57:49 +00001411 if (DstSize != 64 && DstSize > 32) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001412 LLVM_DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1413 << ", expected: 32 or 64\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001414 return false;
1415 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001416 // At this point G_ANYEXT is just like a plain COPY, but we need
1417 // to explicitly form the 64-bit value if any.
1418 if (DstSize > 32) {
1419 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1420 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1421 .addDef(ExtSrc)
1422 .addImm(0)
1423 .addUse(SrcReg)
1424 .addImm(AArch64::sub_32);
1425 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001426 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001427 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001428 }
1429
1430 case TargetOpcode::G_ZEXT:
1431 case TargetOpcode::G_SEXT: {
1432 unsigned Opcode = I.getOpcode();
1433 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1434 SrcTy = MRI.getType(I.getOperand(1).getReg());
1435 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1436 const unsigned DefReg = I.getOperand(0).getReg();
1437 const unsigned SrcReg = I.getOperand(1).getReg();
1438 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1439
1440 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001441 LLVM_DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1442 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001443 return false;
1444 }
1445
1446 MachineInstr *ExtI;
1447 if (DstTy == LLT::scalar(64)) {
1448 // FIXME: Can we avoid manually doing this?
1449 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001450 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1451 << " operand\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001452 return false;
1453 }
1454
1455 const unsigned SrcXReg =
1456 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1457 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1458 .addDef(SrcXReg)
1459 .addImm(0)
1460 .addUse(SrcReg)
1461 .addImm(AArch64::sub_32);
1462
1463 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1464 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1465 .addDef(DefReg)
1466 .addUse(SrcXReg)
1467 .addImm(0)
1468 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001469 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001470 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1471 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1472 .addDef(DefReg)
1473 .addUse(SrcReg)
1474 .addImm(0)
1475 .addImm(SrcTy.getSizeInBits() - 1);
1476 } else {
1477 return false;
1478 }
1479
1480 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1481
1482 I.eraseFromParent();
1483 return true;
1484 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001485
Tim Northover69271c62016-10-12 22:49:11 +00001486 case TargetOpcode::G_SITOFP:
1487 case TargetOpcode::G_UITOFP:
1488 case TargetOpcode::G_FPTOSI:
1489 case TargetOpcode::G_FPTOUI: {
1490 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1491 SrcTy = MRI.getType(I.getOperand(1).getReg());
1492 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1493 if (NewOpc == Opcode)
1494 return false;
1495
1496 I.setDesc(TII.get(NewOpc));
1497 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1498
1499 return true;
1500 }
1501
1502
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001503 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001504 // The importer is currently unable to import pointer types since they
1505 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001506 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001507
Daniel Sandersedd07842017-08-17 09:26:14 +00001508 case TargetOpcode::G_BITCAST:
1509 // Imported SelectionDAG rules can handle every bitcast except those that
1510 // bitcast from a type to the same type. Ideally, these shouldn't occur
1511 // but we might not run an optimizer that deletes them.
1512 if (MRI.getType(I.getOperand(0).getReg()) ==
1513 MRI.getType(I.getOperand(1).getReg()))
1514 return selectCopy(I, TII, MRI, TRI, RBI);
1515 return false;
1516
Tim Northover9ac0eba2016-11-08 00:45:29 +00001517 case TargetOpcode::G_SELECT: {
1518 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001519 LLVM_DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1520 << ", expected: " << LLT::scalar(1) << '\n');
Tim Northover9ac0eba2016-11-08 00:45:29 +00001521 return false;
1522 }
1523
1524 const unsigned CondReg = I.getOperand(1).getReg();
1525 const unsigned TReg = I.getOperand(2).getReg();
1526 const unsigned FReg = I.getOperand(3).getReg();
1527
1528 unsigned CSelOpc = 0;
1529
1530 if (Ty == LLT::scalar(32)) {
1531 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001532 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001533 CSelOpc = AArch64::CSELXr;
1534 } else {
1535 return false;
1536 }
1537
1538 MachineInstr &TstMI =
1539 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1540 .addDef(AArch64::WZR)
1541 .addUse(CondReg)
1542 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1543
1544 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1545 .addDef(I.getOperand(0).getReg())
1546 .addUse(TReg)
1547 .addUse(FReg)
1548 .addImm(AArch64CC::NE);
1549
1550 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1551 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1552
1553 I.eraseFromParent();
1554 return true;
1555 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001556 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001557 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001558 LLVM_DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1559 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001560 return false;
1561 }
1562
1563 unsigned CmpOpc = 0;
1564 unsigned ZReg = 0;
1565
1566 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1567 if (CmpTy == LLT::scalar(32)) {
1568 CmpOpc = AArch64::SUBSWrr;
1569 ZReg = AArch64::WZR;
1570 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1571 CmpOpc = AArch64::SUBSXrr;
1572 ZReg = AArch64::XZR;
1573 } else {
1574 return false;
1575 }
1576
Kristof Beyls22524402017-01-05 10:16:08 +00001577 // CSINC increments the result by one when the condition code is false.
1578 // Therefore, we have to invert the predicate to get an increment by 1 when
1579 // the predicate is true.
1580 const AArch64CC::CondCode invCC =
1581 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1582 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001583
1584 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1585 .addDef(ZReg)
1586 .addUse(I.getOperand(2).getReg())
1587 .addUse(I.getOperand(3).getReg());
1588
1589 MachineInstr &CSetMI =
1590 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1591 .addDef(I.getOperand(0).getReg())
1592 .addUse(AArch64::WZR)
1593 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001594 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001595
1596 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1597 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1598
1599 I.eraseFromParent();
1600 return true;
1601 }
1602
Tim Northover7dd378d2016-10-12 22:49:07 +00001603 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001604 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001605 LLVM_DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1606 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001607 return false;
1608 }
1609
1610 unsigned CmpOpc = 0;
1611 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1612 if (CmpTy == LLT::scalar(32)) {
1613 CmpOpc = AArch64::FCMPSrr;
1614 } else if (CmpTy == LLT::scalar(64)) {
1615 CmpOpc = AArch64::FCMPDrr;
1616 } else {
1617 return false;
1618 }
1619
1620 // FIXME: regbank
1621
1622 AArch64CC::CondCode CC1, CC2;
1623 changeFCMPPredToAArch64CC(
1624 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1625
1626 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1627 .addUse(I.getOperand(2).getReg())
1628 .addUse(I.getOperand(3).getReg());
1629
1630 const unsigned DefReg = I.getOperand(0).getReg();
1631 unsigned Def1Reg = DefReg;
1632 if (CC2 != AArch64CC::AL)
1633 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1634
1635 MachineInstr &CSetMI =
1636 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1637 .addDef(Def1Reg)
1638 .addUse(AArch64::WZR)
1639 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001640 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001641
1642 if (CC2 != AArch64CC::AL) {
1643 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1644 MachineInstr &CSet2MI =
1645 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1646 .addDef(Def2Reg)
1647 .addUse(AArch64::WZR)
1648 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001649 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001650 MachineInstr &OrMI =
1651 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1652 .addDef(DefReg)
1653 .addUse(Def1Reg)
1654 .addUse(Def2Reg);
1655 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1656 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1657 }
1658
1659 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1660 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1661
1662 I.eraseFromParent();
1663 return true;
1664 }
Tim Northovere9600d82017-02-08 17:57:27 +00001665 case TargetOpcode::G_VASTART:
1666 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1667 : selectVaStartAAPCS(I, MF, MRI);
Amara Emerson1f5d9942018-04-25 14:43:59 +00001668 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
1669 if (!I.getOperand(0).isIntrinsicID())
1670 return false;
1671 if (I.getOperand(0).getIntrinsicID() != Intrinsic::trap)
1672 return false;
1673 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::BRK))
1674 .addImm(1);
1675 I.eraseFromParent();
1676 return true;
Amara Emerson1e8c1642018-07-31 00:09:02 +00001677 case TargetOpcode::G_IMPLICIT_DEF: {
Justin Bogner4fc69662017-07-12 17:32:32 +00001678 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001679 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1680 const unsigned DstReg = I.getOperand(0).getReg();
1681 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1682 const TargetRegisterClass *DstRC =
1683 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1684 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001685 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001686 }
Amara Emerson1e8c1642018-07-31 00:09:02 +00001687 case TargetOpcode::G_BLOCK_ADDR: {
1688 if (TM.getCodeModel() == CodeModel::Large) {
1689 materializeLargeCMVal(I, I.getOperand(1).getBlockAddress(), 0);
1690 I.eraseFromParent();
1691 return true;
1692 } else {
1693 I.setDesc(TII.get(AArch64::MOVaddrBA));
1694 auto MovMI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::MOVaddrBA),
1695 I.getOperand(0).getReg())
1696 .addBlockAddress(I.getOperand(1).getBlockAddress(),
1697 /* Offset */ 0, AArch64II::MO_PAGE)
1698 .addBlockAddress(
1699 I.getOperand(1).getBlockAddress(), /* Offset */ 0,
1700 AArch64II::MO_NC | AArch64II::MO_PAGEOFF);
1701 I.eraseFromParent();
1702 return constrainSelectedInstRegOperands(*MovMI, TII, TRI, RBI);
1703 }
1704 }
Amara Emerson5ec14602018-12-10 18:44:58 +00001705 case TargetOpcode::G_BUILD_VECTOR:
1706 return selectBuildVector(I, MRI);
Amara Emerson8cb186c2018-12-20 01:11:04 +00001707 case TargetOpcode::G_MERGE_VALUES:
1708 return selectMergeValues(I, MRI);
Jessica Paquette245047d2019-01-24 22:00:41 +00001709 case TargetOpcode::G_UNMERGE_VALUES:
1710 return selectUnmergeValues(I, MRI);
Amara Emerson1abe05c2019-02-21 20:20:16 +00001711 case TargetOpcode::G_SHUFFLE_VECTOR:
1712 return selectShuffleVector(I, MRI);
Jessica Paquette607774c2019-03-11 22:18:01 +00001713 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
1714 return selectExtractElt(I, MRI);
Amara Emerson1e8c1642018-07-31 00:09:02 +00001715 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001716
1717 return false;
1718}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001719
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001720MachineInstr *AArch64InstructionSelector::emitScalarToVector(
Amara Emerson8acb0d92019-03-04 19:16:00 +00001721 unsigned EltSize, const TargetRegisterClass *DstRC, unsigned Scalar,
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001722 MachineIRBuilder &MIRBuilder) const {
1723 auto Undef = MIRBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF, {DstRC}, {});
Amara Emerson5ec14602018-12-10 18:44:58 +00001724
1725 auto BuildFn = [&](unsigned SubregIndex) {
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001726 auto Ins =
1727 MIRBuilder
1728 .buildInstr(TargetOpcode::INSERT_SUBREG, {DstRC}, {Undef, Scalar})
1729 .addImm(SubregIndex);
1730 constrainSelectedInstRegOperands(*Undef, TII, TRI, RBI);
1731 constrainSelectedInstRegOperands(*Ins, TII, TRI, RBI);
1732 return &*Ins;
Amara Emerson5ec14602018-12-10 18:44:58 +00001733 };
1734
Amara Emerson8acb0d92019-03-04 19:16:00 +00001735 switch (EltSize) {
Jessica Paquette245047d2019-01-24 22:00:41 +00001736 case 16:
1737 return BuildFn(AArch64::hsub);
Amara Emerson5ec14602018-12-10 18:44:58 +00001738 case 32:
1739 return BuildFn(AArch64::ssub);
1740 case 64:
1741 return BuildFn(AArch64::dsub);
1742 default:
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00001743 return nullptr;
Amara Emerson5ec14602018-12-10 18:44:58 +00001744 }
1745}
1746
Amara Emerson8cb186c2018-12-20 01:11:04 +00001747bool AArch64InstructionSelector::selectMergeValues(
1748 MachineInstr &I, MachineRegisterInfo &MRI) const {
1749 assert(I.getOpcode() == TargetOpcode::G_MERGE_VALUES && "unexpected opcode");
1750 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1751 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1752 assert(!DstTy.isVector() && !SrcTy.isVector() && "invalid merge operation");
1753
1754 // At the moment we only support merging two s32s into an s64.
1755 if (I.getNumOperands() != 3)
1756 return false;
1757 if (DstTy.getSizeInBits() != 64 || SrcTy.getSizeInBits() != 32)
1758 return false;
1759 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
1760 if (RB.getID() != AArch64::GPRRegBankID)
1761 return false;
1762
1763 auto *DstRC = &AArch64::GPR64RegClass;
1764 unsigned SubToRegDef = MRI.createVirtualRegister(DstRC);
1765 MachineInstr &SubRegMI = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1766 TII.get(TargetOpcode::SUBREG_TO_REG))
1767 .addDef(SubToRegDef)
1768 .addImm(0)
1769 .addUse(I.getOperand(1).getReg())
1770 .addImm(AArch64::sub_32);
1771 unsigned SubToRegDef2 = MRI.createVirtualRegister(DstRC);
1772 // Need to anyext the second scalar before we can use bfm
1773 MachineInstr &SubRegMI2 = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1774 TII.get(TargetOpcode::SUBREG_TO_REG))
1775 .addDef(SubToRegDef2)
1776 .addImm(0)
1777 .addUse(I.getOperand(2).getReg())
1778 .addImm(AArch64::sub_32);
Amara Emerson8cb186c2018-12-20 01:11:04 +00001779 MachineInstr &BFM =
1780 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::BFMXri))
Amara Emerson321bfb22018-12-20 03:27:42 +00001781 .addDef(I.getOperand(0).getReg())
Amara Emerson8cb186c2018-12-20 01:11:04 +00001782 .addUse(SubToRegDef)
1783 .addUse(SubToRegDef2)
1784 .addImm(32)
1785 .addImm(31);
1786 constrainSelectedInstRegOperands(SubRegMI, TII, TRI, RBI);
1787 constrainSelectedInstRegOperands(SubRegMI2, TII, TRI, RBI);
1788 constrainSelectedInstRegOperands(BFM, TII, TRI, RBI);
1789 I.eraseFromParent();
1790 return true;
1791}
1792
Jessica Paquette607774c2019-03-11 22:18:01 +00001793static bool getLaneCopyOpcode(unsigned &CopyOpc, unsigned &ExtractSubReg,
1794 const unsigned EltSize) {
1795 // Choose a lane copy opcode and subregister based off of the size of the
1796 // vector's elements.
1797 switch (EltSize) {
1798 case 16:
1799 CopyOpc = AArch64::CPYi16;
1800 ExtractSubReg = AArch64::hsub;
1801 break;
1802 case 32:
1803 CopyOpc = AArch64::CPYi32;
1804 ExtractSubReg = AArch64::ssub;
1805 break;
1806 case 64:
1807 CopyOpc = AArch64::CPYi64;
1808 ExtractSubReg = AArch64::dsub;
1809 break;
1810 default:
1811 // Unknown size, bail out.
1812 LLVM_DEBUG(dbgs() << "Elt size '" << EltSize << "' unsupported.\n");
1813 return false;
1814 }
1815 return true;
1816}
1817
1818bool AArch64InstructionSelector::selectExtractElt(
1819 MachineInstr &I, MachineRegisterInfo &MRI) const {
1820 assert(I.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT &&
1821 "unexpected opcode!");
1822 unsigned DstReg = I.getOperand(0).getReg();
1823 const LLT NarrowTy = MRI.getType(DstReg);
1824 const unsigned SrcReg = I.getOperand(1).getReg();
1825 const LLT WideTy = MRI.getType(SrcReg);
1826
1827 assert(WideTy.getSizeInBits() >= NarrowTy.getSizeInBits() &&
1828 "source register size too small!");
1829 assert(NarrowTy.isScalar() && "cannot extract vector into vector!");
1830
1831 // Need the lane index to determine the correct copy opcode.
1832 MachineOperand &LaneIdxOp = I.getOperand(2);
1833 assert(LaneIdxOp.isReg() && "Lane index operand was not a register?");
1834
1835 if (RBI.getRegBank(DstReg, MRI, TRI)->getID() != AArch64::FPRRegBankID) {
1836 LLVM_DEBUG(dbgs() << "Cannot extract into GPR.\n");
1837 return false;
1838 }
1839
1840 // Find the instruction that defines the constant to extract from. There could
1841 // be any number of copies between the instruction and the definition of the
1842 // index. Skip them.
1843 MachineInstr *LaneDefInst = nullptr;
1844 for (LaneDefInst = MRI.getVRegDef(LaneIdxOp.getReg());
1845 LaneDefInst && LaneDefInst->isCopy();
1846 LaneDefInst = MRI.getVRegDef(LaneDefInst->getOperand(1).getReg())) {
1847 }
1848
1849 // Did we find a def in the first place? If not, bail.
1850 if (!LaneDefInst) {
1851 LLVM_DEBUG(dbgs() << "Did not find VReg definition for " << LaneIdxOp
1852 << "\n");
1853 return false;
1854 }
1855
1856 // TODO: Handle extracts that don't use G_CONSTANT.
1857 if (LaneDefInst->getOpcode() != TargetOpcode::G_CONSTANT) {
1858 LLVM_DEBUG(dbgs() << "VRegs defined by anything other than G_CONSTANT "
1859 "currently unsupported.\n");
1860 return false;
1861 }
1862
1863 unsigned LaneIdx = LaneDefInst->getOperand(1).getCImm()->getLimitedValue();
1864 unsigned CopyOpc = 0;
1865 unsigned ExtractSubReg = 0;
1866 if (!getLaneCopyOpcode(CopyOpc, ExtractSubReg, NarrowTy.getSizeInBits())) {
1867 LLVM_DEBUG(
1868 dbgs() << "Couldn't determine lane copy opcode for instruction.\n");
1869 return false;
1870 }
1871
1872 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1873 const TargetRegisterClass *DstRC =
1874 getRegClassForTypeOnBank(NarrowTy, DstRB, RBI, true);
1875 if (!DstRC) {
1876 LLVM_DEBUG(dbgs() << "Could not determine destination register class.\n");
1877 return false;
1878 }
1879
1880 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1881 const TargetRegisterClass *SrcRC =
1882 getRegClassForTypeOnBank(WideTy, SrcRB, RBI, true);
1883 if (!SrcRC) {
1884 LLVM_DEBUG(dbgs() << "Could not determine source register class.\n");
1885 return false;
1886 }
1887
1888 // The register that we're going to copy into.
1889 unsigned InsertReg = SrcReg;
1890 MachineIRBuilder MIRBuilder(I);
1891
1892 // If the lane index is 0, we just use a subregister COPY.
1893 if (LaneIdx == 0) {
1894 unsigned CopyTo = I.getOperand(0).getReg();
1895 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
1896 CopyTo)
1897 .addUse(SrcReg, 0, ExtractSubReg);
1898 RBI.constrainGenericRegister(CopyTo, *DstRC, MRI);
1899 I.eraseFromParent();
1900 return true;
1901 }
1902
1903 // Lane copies require 128-bit wide registers. If we're dealing with an
1904 // unpacked vector, then we need to move up to that width. Insert an implicit
1905 // def and a subregister insert to get us there.
1906 if (WideTy.getSizeInBits() != 128) {
1907 MachineInstr *ScalarToVector = emitScalarToVector(
1908 WideTy.getSizeInBits(), &AArch64::FPR128RegClass, SrcReg, MIRBuilder);
1909 if (!ScalarToVector)
1910 return false;
1911 InsertReg = ScalarToVector->getOperand(0).getReg();
1912 }
1913
1914 MachineInstr *LaneCopyMI =
1915 MIRBuilder.buildInstr(CopyOpc, {DstReg}, {InsertReg}).addImm(LaneIdx);
1916 constrainSelectedInstRegOperands(*LaneCopyMI, TII, TRI, RBI);
1917
1918 // Make sure that we actually constrain the initial copy.
1919 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
1920
1921 I.eraseFromParent();
1922 return true;
1923}
1924
Jessica Paquette245047d2019-01-24 22:00:41 +00001925bool AArch64InstructionSelector::selectUnmergeValues(
1926 MachineInstr &I, MachineRegisterInfo &MRI) const {
1927 assert(I.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
1928 "unexpected opcode");
1929
1930 // TODO: Handle unmerging into GPRs and from scalars to scalars.
1931 if (RBI.getRegBank(I.getOperand(0).getReg(), MRI, TRI)->getID() !=
1932 AArch64::FPRRegBankID ||
1933 RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI)->getID() !=
1934 AArch64::FPRRegBankID) {
1935 LLVM_DEBUG(dbgs() << "Unmerging vector-to-gpr and scalar-to-scalar "
1936 "currently unsupported.\n");
1937 return false;
1938 }
1939
1940 // The last operand is the vector source register, and every other operand is
1941 // a register to unpack into.
1942 unsigned NumElts = I.getNumOperands() - 1;
1943 unsigned SrcReg = I.getOperand(NumElts).getReg();
1944 const LLT NarrowTy = MRI.getType(I.getOperand(0).getReg());
1945 const LLT WideTy = MRI.getType(SrcReg);
Benjamin Kramer653020d2019-01-24 23:45:07 +00001946 (void)WideTy;
Jessica Paquette245047d2019-01-24 22:00:41 +00001947 assert(WideTy.isVector() && "can only unmerge from vector types!");
1948 assert(WideTy.getSizeInBits() > NarrowTy.getSizeInBits() &&
1949 "source register size too small!");
1950
Jessica Paquette1f9bc282019-01-25 21:28:27 +00001951 // TODO: Handle unmerging into vectors.
Jessica Paquette245047d2019-01-24 22:00:41 +00001952 if (!NarrowTy.isScalar()) {
1953 LLVM_DEBUG(dbgs() << "Vector-to-vector unmerges not supported yet.\n");
1954 return false;
1955 }
1956
1957 // Choose a lane copy opcode and subregister based off of the size of the
1958 // vector's elements.
1959 unsigned CopyOpc = 0;
1960 unsigned ExtractSubReg = 0;
Jessica Paquette607774c2019-03-11 22:18:01 +00001961 if (!getLaneCopyOpcode(CopyOpc, ExtractSubReg, NarrowTy.getSizeInBits()))
Jessica Paquette245047d2019-01-24 22:00:41 +00001962 return false;
Jessica Paquette245047d2019-01-24 22:00:41 +00001963
1964 // Set up for the lane copies.
1965 MachineBasicBlock &MBB = *I.getParent();
1966
1967 // Stores the registers we'll be copying from.
1968 SmallVector<unsigned, 4> InsertRegs;
1969
1970 // We'll use the first register twice, so we only need NumElts-1 registers.
1971 unsigned NumInsertRegs = NumElts - 1;
1972
1973 // If our elements fit into exactly 128 bits, then we can copy from the source
1974 // directly. Otherwise, we need to do a bit of setup with some subregister
1975 // inserts.
1976 if (NarrowTy.getSizeInBits() * NumElts == 128) {
1977 InsertRegs = SmallVector<unsigned, 4>(NumInsertRegs, SrcReg);
1978 } else {
1979 // No. We have to perform subregister inserts. For each insert, create an
1980 // implicit def and a subregister insert, and save the register we create.
1981 for (unsigned Idx = 0; Idx < NumInsertRegs; ++Idx) {
1982 unsigned ImpDefReg = MRI.createVirtualRegister(&AArch64::FPR128RegClass);
1983 MachineInstr &ImpDefMI =
1984 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(TargetOpcode::IMPLICIT_DEF),
1985 ImpDefReg);
1986
1987 // Now, create the subregister insert from SrcReg.
1988 unsigned InsertReg = MRI.createVirtualRegister(&AArch64::FPR128RegClass);
1989 MachineInstr &InsMI =
1990 *BuildMI(MBB, I, I.getDebugLoc(),
1991 TII.get(TargetOpcode::INSERT_SUBREG), InsertReg)
1992 .addUse(ImpDefReg)
1993 .addUse(SrcReg)
1994 .addImm(AArch64::dsub);
1995
1996 constrainSelectedInstRegOperands(ImpDefMI, TII, TRI, RBI);
1997 constrainSelectedInstRegOperands(InsMI, TII, TRI, RBI);
1998
1999 // Save the register so that we can copy from it after.
2000 InsertRegs.push_back(InsertReg);
2001 }
2002 }
2003
2004 // Now that we've created any necessary subregister inserts, we can
2005 // create the copies.
2006 //
2007 // Perform the first copy separately as a subregister copy.
2008 unsigned CopyTo = I.getOperand(0).getReg();
2009 MachineInstr &FirstCopy =
2010 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(TargetOpcode::COPY), CopyTo)
2011 .addUse(InsertRegs[0], 0, ExtractSubReg);
2012 constrainSelectedInstRegOperands(FirstCopy, TII, TRI, RBI);
2013
2014 // Now, perform the remaining copies as vector lane copies.
2015 unsigned LaneIdx = 1;
2016 for (unsigned InsReg : InsertRegs) {
2017 unsigned CopyTo = I.getOperand(LaneIdx).getReg();
2018 MachineInstr &CopyInst =
2019 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CopyOpc), CopyTo)
2020 .addUse(InsReg)
2021 .addImm(LaneIdx);
2022 constrainSelectedInstRegOperands(CopyInst, TII, TRI, RBI);
2023 ++LaneIdx;
2024 }
2025
2026 // Separately constrain the first copy's destination. Because of the
2027 // limitation in constrainOperandRegClass, we can't guarantee that this will
2028 // actually be constrained. So, do it ourselves using the second operand.
2029 const TargetRegisterClass *RC =
2030 MRI.getRegClassOrNull(I.getOperand(1).getReg());
2031 if (!RC) {
2032 LLVM_DEBUG(dbgs() << "Couldn't constrain copy destination.\n");
2033 return false;
2034 }
2035
2036 RBI.constrainGenericRegister(CopyTo, *RC, MRI);
2037 I.eraseFromParent();
2038 return true;
2039}
2040
Amara Emerson1abe05c2019-02-21 20:20:16 +00002041void AArch64InstructionSelector::collectShuffleMaskIndices(
2042 MachineInstr &I, MachineRegisterInfo &MRI,
2043 SmallVectorImpl<int> &Idxs) const {
2044 MachineInstr *MaskDef = MRI.getVRegDef(I.getOperand(3).getReg());
2045 assert(
2046 MaskDef->getOpcode() == TargetOpcode::G_BUILD_VECTOR &&
2047 "G_SHUFFLE_VECTOR should have a constant mask operand as G_BUILD_VECTOR");
2048 // Find the constant indices.
2049 for (unsigned i = 1, e = MaskDef->getNumOperands(); i < e; ++i) {
2050 MachineInstr *ScalarDef = MRI.getVRegDef(MaskDef->getOperand(i).getReg());
2051 assert(ScalarDef && "Could not find vreg def of shufflevec index op");
2052 // Look through copies.
2053 while (ScalarDef->getOpcode() == TargetOpcode::COPY) {
2054 ScalarDef = MRI.getVRegDef(ScalarDef->getOperand(1).getReg());
2055 assert(ScalarDef && "Could not find def of copy operand");
2056 }
2057 assert(ScalarDef->getOpcode() == TargetOpcode::G_CONSTANT);
2058 Idxs.push_back(ScalarDef->getOperand(1).getCImm()->getSExtValue());
2059 }
2060}
2061
2062unsigned
2063AArch64InstructionSelector::emitConstantPoolEntry(Constant *CPVal,
2064 MachineFunction &MF) const {
2065 Type *CPTy = CPVal->getType()->getPointerTo();
2066 unsigned Align = MF.getDataLayout().getPrefTypeAlignment(CPTy);
2067 if (Align == 0)
2068 Align = MF.getDataLayout().getTypeAllocSize(CPTy);
2069
2070 MachineConstantPool *MCP = MF.getConstantPool();
2071 return MCP->getConstantPoolIndex(CPVal, Align);
2072}
2073
2074MachineInstr *AArch64InstructionSelector::emitLoadFromConstantPool(
2075 Constant *CPVal, MachineIRBuilder &MIRBuilder) const {
2076 unsigned CPIdx = emitConstantPoolEntry(CPVal, MIRBuilder.getMF());
2077
2078 auto Adrp =
2079 MIRBuilder.buildInstr(AArch64::ADRP, {&AArch64::GPR64RegClass}, {})
2080 .addConstantPoolIndex(CPIdx, 0, AArch64II::MO_PAGE);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002081
2082 MachineInstr *LoadMI = nullptr;
2083 switch (MIRBuilder.getDataLayout().getTypeStoreSize(CPVal->getType())) {
2084 case 16:
2085 LoadMI =
2086 &*MIRBuilder
2087 .buildInstr(AArch64::LDRQui, {&AArch64::FPR128RegClass}, {Adrp})
2088 .addConstantPoolIndex(CPIdx, 0,
2089 AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2090 break;
2091 case 8:
2092 LoadMI = &*MIRBuilder
2093 .buildInstr(AArch64::LDRDui, {&AArch64::FPR64RegClass}, {Adrp})
2094 .addConstantPoolIndex(
2095 CPIdx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2096 break;
2097 default:
2098 LLVM_DEBUG(dbgs() << "Could not load from constant pool of type "
2099 << *CPVal->getType());
2100 return nullptr;
2101 }
Amara Emerson1abe05c2019-02-21 20:20:16 +00002102 constrainSelectedInstRegOperands(*Adrp, TII, TRI, RBI);
Amara Emerson8acb0d92019-03-04 19:16:00 +00002103 constrainSelectedInstRegOperands(*LoadMI, TII, TRI, RBI);
2104 return LoadMI;
2105}
2106
2107/// Return an <Opcode, SubregIndex> pair to do an vector elt insert of a given
2108/// size and RB.
2109static std::pair<unsigned, unsigned>
2110getInsertVecEltOpInfo(const RegisterBank &RB, unsigned EltSize) {
2111 unsigned Opc, SubregIdx;
2112 if (RB.getID() == AArch64::GPRRegBankID) {
2113 if (EltSize == 32) {
2114 Opc = AArch64::INSvi32gpr;
2115 SubregIdx = AArch64::ssub;
2116 } else if (EltSize == 64) {
2117 Opc = AArch64::INSvi64gpr;
2118 SubregIdx = AArch64::dsub;
2119 } else {
2120 llvm_unreachable("invalid elt size!");
2121 }
2122 } else {
2123 if (EltSize == 8) {
2124 Opc = AArch64::INSvi8lane;
2125 SubregIdx = AArch64::bsub;
2126 } else if (EltSize == 16) {
2127 Opc = AArch64::INSvi16lane;
2128 SubregIdx = AArch64::hsub;
2129 } else if (EltSize == 32) {
2130 Opc = AArch64::INSvi32lane;
2131 SubregIdx = AArch64::ssub;
2132 } else if (EltSize == 64) {
2133 Opc = AArch64::INSvi64lane;
2134 SubregIdx = AArch64::dsub;
2135 } else {
2136 llvm_unreachable("invalid elt size!");
2137 }
2138 }
2139 return std::make_pair(Opc, SubregIdx);
2140}
2141
2142MachineInstr *AArch64InstructionSelector::emitVectorConcat(
2143 unsigned Op1, unsigned Op2, MachineIRBuilder &MIRBuilder) const {
2144 // We implement a vector concat by:
2145 // 1. Use scalar_to_vector to insert the lower vector into the larger dest
2146 // 2. Insert the upper vector into the destination's upper element
2147 // TODO: some of this code is common with G_BUILD_VECTOR handling.
2148 MachineRegisterInfo &MRI = MIRBuilder.getMF().getRegInfo();
2149
2150 const LLT Op1Ty = MRI.getType(Op1);
2151 const LLT Op2Ty = MRI.getType(Op2);
2152
2153 if (Op1Ty != Op2Ty) {
2154 LLVM_DEBUG(dbgs() << "Could not do vector concat of differing vector tys");
2155 return nullptr;
2156 }
2157 assert(Op1Ty.isVector() && "Expected a vector for vector concat");
2158
2159 if (Op1Ty.getSizeInBits() >= 128) {
2160 LLVM_DEBUG(dbgs() << "Vector concat not supported for full size vectors");
2161 return nullptr;
2162 }
2163
2164 // At the moment we just support 64 bit vector concats.
2165 if (Op1Ty.getSizeInBits() != 64) {
2166 LLVM_DEBUG(dbgs() << "Vector concat supported for 64b vectors");
2167 return nullptr;
2168 }
2169
2170 const LLT ScalarTy = LLT::scalar(Op1Ty.getSizeInBits());
2171 const RegisterBank &FPRBank = *RBI.getRegBank(Op1, MRI, TRI);
2172 const TargetRegisterClass *DstRC =
2173 getMinClassForRegBank(FPRBank, Op1Ty.getSizeInBits() * 2);
2174
2175 MachineInstr *WidenedOp1 =
2176 emitScalarToVector(ScalarTy.getSizeInBits(), DstRC, Op1, MIRBuilder);
2177 MachineInstr *WidenedOp2 =
2178 emitScalarToVector(ScalarTy.getSizeInBits(), DstRC, Op2, MIRBuilder);
2179 if (!WidenedOp1 || !WidenedOp2) {
2180 LLVM_DEBUG(dbgs() << "Could not emit a vector from scalar value");
2181 return nullptr;
2182 }
2183
2184 // Now do the insert of the upper element.
2185 unsigned InsertOpc, InsSubRegIdx;
2186 std::tie(InsertOpc, InsSubRegIdx) =
2187 getInsertVecEltOpInfo(FPRBank, ScalarTy.getSizeInBits());
2188
2189 auto InsElt =
2190 MIRBuilder
2191 .buildInstr(InsertOpc, {DstRC}, {WidenedOp1->getOperand(0).getReg()})
2192 .addImm(1) /* Lane index */
2193 .addUse(WidenedOp2->getOperand(0).getReg())
2194 .addImm(0);
2195
2196 constrainSelectedInstRegOperands(*InsElt, TII, TRI, RBI);
2197 return &*InsElt;
Amara Emerson1abe05c2019-02-21 20:20:16 +00002198}
2199
2200bool AArch64InstructionSelector::selectShuffleVector(
2201 MachineInstr &I, MachineRegisterInfo &MRI) const {
2202 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
2203 unsigned Src1Reg = I.getOperand(1).getReg();
2204 const LLT Src1Ty = MRI.getType(Src1Reg);
2205 unsigned Src2Reg = I.getOperand(2).getReg();
2206 const LLT Src2Ty = MRI.getType(Src2Reg);
2207
2208 MachineBasicBlock &MBB = *I.getParent();
2209 MachineFunction &MF = *MBB.getParent();
2210 LLVMContext &Ctx = MF.getFunction().getContext();
2211
2212 // G_SHUFFLE_VECTOR doesn't really have a strictly enforced constant mask
2213 // operand, it comes in as a normal vector value which we have to analyze to
2214 // find the mask indices.
2215 SmallVector<int, 8> Mask;
2216 collectShuffleMaskIndices(I, MRI, Mask);
2217 assert(!Mask.empty() && "Expected to find mask indices");
2218
2219 // G_SHUFFLE_VECTOR is weird in that the source operands can be scalars, if
2220 // it's originated from a <1 x T> type. Those should have been lowered into
2221 // G_BUILD_VECTOR earlier.
2222 if (!Src1Ty.isVector() || !Src2Ty.isVector()) {
2223 LLVM_DEBUG(dbgs() << "Could not select a \"scalar\" G_SHUFFLE_VECTOR\n");
2224 return false;
2225 }
2226
2227 unsigned BytesPerElt = DstTy.getElementType().getSizeInBits() / 8;
2228
2229 SmallVector<Constant *, 64> CstIdxs;
2230 for (int Val : Mask) {
2231 for (unsigned Byte = 0; Byte < BytesPerElt; ++Byte) {
2232 unsigned Offset = Byte + Val * BytesPerElt;
2233 CstIdxs.emplace_back(ConstantInt::get(Type::getInt8Ty(Ctx), Offset));
2234 }
2235 }
2236
Amara Emerson8acb0d92019-03-04 19:16:00 +00002237 MachineIRBuilder MIRBuilder(I);
Amara Emerson1abe05c2019-02-21 20:20:16 +00002238
2239 // Use a constant pool to load the index vector for TBL.
2240 Constant *CPVal = ConstantVector::get(CstIdxs);
Amara Emerson1abe05c2019-02-21 20:20:16 +00002241 MachineInstr *IndexLoad = emitLoadFromConstantPool(CPVal, MIRBuilder);
2242 if (!IndexLoad) {
2243 LLVM_DEBUG(dbgs() << "Could not load from a constant pool");
2244 return false;
2245 }
2246
Amara Emerson8acb0d92019-03-04 19:16:00 +00002247 if (DstTy.getSizeInBits() != 128) {
2248 assert(DstTy.getSizeInBits() == 64 && "Unexpected shuffle result ty");
2249 // This case can be done with TBL1.
2250 MachineInstr *Concat = emitVectorConcat(Src1Reg, Src2Reg, MIRBuilder);
2251 if (!Concat) {
2252 LLVM_DEBUG(dbgs() << "Could not do vector concat for tbl1");
2253 return false;
2254 }
2255
2256 // The constant pool load will be 64 bits, so need to convert to FPR128 reg.
2257 IndexLoad =
2258 emitScalarToVector(64, &AArch64::FPR128RegClass,
2259 IndexLoad->getOperand(0).getReg(), MIRBuilder);
2260
2261 auto TBL1 = MIRBuilder.buildInstr(
2262 AArch64::TBLv16i8One, {&AArch64::FPR128RegClass},
2263 {Concat->getOperand(0).getReg(), IndexLoad->getOperand(0).getReg()});
2264 constrainSelectedInstRegOperands(*TBL1, TII, TRI, RBI);
2265
2266 auto Copy = BuildMI(*I.getParent(), I, I.getDebugLoc(),
2267 TII.get(TargetOpcode::COPY), I.getOperand(0).getReg())
2268 .addUse(TBL1->getOperand(0).getReg(), 0, AArch64::dsub);
2269 RBI.constrainGenericRegister(Copy.getReg(0), AArch64::FPR64RegClass, MRI);
2270 I.eraseFromParent();
2271 return true;
2272 }
2273
Amara Emerson1abe05c2019-02-21 20:20:16 +00002274 // For TBL2 we need to emit a REG_SEQUENCE to tie together two consecutive
2275 // Q registers for regalloc.
2276 auto RegSeq = MIRBuilder
2277 .buildInstr(TargetOpcode::REG_SEQUENCE,
2278 {&AArch64::QQRegClass}, {Src1Reg})
2279 .addImm(AArch64::qsub0)
2280 .addUse(Src2Reg)
2281 .addImm(AArch64::qsub1);
2282
2283 auto TBL2 =
2284 MIRBuilder.buildInstr(AArch64::TBLv16i8Two, {I.getOperand(0).getReg()},
2285 {RegSeq, IndexLoad->getOperand(0).getReg()});
2286 constrainSelectedInstRegOperands(*RegSeq, TII, TRI, RBI);
2287 constrainSelectedInstRegOperands(*TBL2, TII, TRI, RBI);
2288 I.eraseFromParent();
2289 return true;
2290}
2291
Amara Emerson5ec14602018-12-10 18:44:58 +00002292bool AArch64InstructionSelector::selectBuildVector(
2293 MachineInstr &I, MachineRegisterInfo &MRI) const {
2294 assert(I.getOpcode() == TargetOpcode::G_BUILD_VECTOR);
2295 // Until we port more of the optimized selections, for now just use a vector
2296 // insert sequence.
2297 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
2298 const LLT EltTy = MRI.getType(I.getOperand(1).getReg());
2299 unsigned EltSize = EltTy.getSizeInBits();
Jessica Paquette245047d2019-01-24 22:00:41 +00002300 if (EltSize < 16 || EltSize > 64)
Amara Emerson5ec14602018-12-10 18:44:58 +00002301 return false; // Don't support all element types yet.
2302 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
2303 unsigned Opc;
2304 unsigned SubregIdx;
Amara Emerson8acb0d92019-03-04 19:16:00 +00002305
2306 std::tie(Opc, SubregIdx) = getInsertVecEltOpInfo(RB, EltSize);
Amara Emerson5ec14602018-12-10 18:44:58 +00002307
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002308 MachineIRBuilder MIRBuilder(I);
Jessica Paquette245047d2019-01-24 22:00:41 +00002309
2310 const TargetRegisterClass *DstRC = &AArch64::FPR128RegClass;
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002311 MachineInstr *ScalarToVec =
Amara Emerson8acb0d92019-03-04 19:16:00 +00002312 emitScalarToVector(DstTy.getElementType().getSizeInBits(), DstRC,
2313 I.getOperand(1).getReg(), MIRBuilder);
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002314 if (!ScalarToVec)
Jessica Paquette245047d2019-01-24 22:00:41 +00002315 return false;
2316
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002317 unsigned DstVec = ScalarToVec->getOperand(0).getReg();
Jessica Paquette245047d2019-01-24 22:00:41 +00002318 unsigned DstSize = DstTy.getSizeInBits();
2319
2320 // Keep track of the last MI we inserted. Later on, we might be able to save
2321 // a copy using it.
2322 MachineInstr *PrevMI = nullptr;
2323 for (unsigned i = 2, e = DstSize / EltSize + 1; i < e; ++i) {
Jessica Paquette245047d2019-01-24 22:00:41 +00002324 // Note that if we don't do a subregister copy, we end up making one more
2325 // of these than we need.
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002326 unsigned InsDef = MRI.createVirtualRegister(DstRC);
Amara Emerson5ec14602018-12-10 18:44:58 +00002327 unsigned LaneIdx = i - 1;
2328 if (RB.getID() == AArch64::FPRRegBankID) {
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002329 auto ImpDef =
2330 MIRBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF, {DstRC}, {});
2331 auto InsSub = MIRBuilder
2332 .buildInstr(TargetOpcode::INSERT_SUBREG, {DstRC},
2333 {ImpDef, I.getOperand(i)})
2334 .addImm(SubregIdx);
2335 auto InsElt = MIRBuilder.buildInstr(Opc, {InsDef}, {DstVec})
2336 .addImm(LaneIdx)
2337 .addUse(InsSub.getReg(0))
2338 .addImm(0);
2339 constrainSelectedInstRegOperands(*ImpDef, TII, TRI, RBI);
2340 constrainSelectedInstRegOperands(*InsSub, TII, TRI, RBI);
2341 constrainSelectedInstRegOperands(*InsElt, TII, TRI, RBI);
Amara Emerson5ec14602018-12-10 18:44:58 +00002342 DstVec = InsDef;
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002343 PrevMI = &*InsElt;
Amara Emerson5ec14602018-12-10 18:44:58 +00002344 } else {
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002345 auto Ins = MIRBuilder.buildInstr(Opc, {InsDef}, {DstVec})
2346 .addImm(LaneIdx)
2347 .addUse(I.getOperand(i).getReg());
2348 constrainSelectedInstRegOperands(*Ins, TII, TRI, RBI);
Amara Emerson5ec14602018-12-10 18:44:58 +00002349 DstVec = InsDef;
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002350 PrevMI = &*Ins;
Amara Emerson5ec14602018-12-10 18:44:58 +00002351 }
2352 }
Jessica Paquette245047d2019-01-24 22:00:41 +00002353
2354 // If DstTy's size in bits is less than 128, then emit a subregister copy
2355 // from DstVec to the last register we've defined.
2356 if (DstSize < 128) {
2357 unsigned SubReg = 0;
2358
2359 // Helper lambda to decide on a register class and subregister for the
2360 // subregister copy.
2361 auto GetRegInfoForCopy = [&SubReg,
2362 &DstSize]() -> const TargetRegisterClass * {
2363 switch (DstSize) {
2364 default:
2365 LLVM_DEBUG(dbgs() << "Unknown destination size (" << DstSize << ")\n");
2366 return nullptr;
2367 case 32:
2368 SubReg = AArch64::ssub;
2369 return &AArch64::FPR32RegClass;
2370 case 64:
2371 SubReg = AArch64::dsub;
2372 return &AArch64::FPR64RegClass;
2373 }
2374 };
2375
2376 const TargetRegisterClass *RC = GetRegInfoForCopy();
2377 if (!RC)
2378 return false;
2379
2380 unsigned Reg = MRI.createVirtualRegister(RC);
2381 unsigned DstReg = I.getOperand(0).getReg();
2382
Amara Emerson6bcfa1c2019-02-25 18:52:54 +00002383 // MIRBuilder doesn't let us create uses with subregs & flags, so use
2384 // BuildMI here instead.
Jessica Paquette245047d2019-01-24 22:00:41 +00002385 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
2386 DstReg)
2387 .addUse(DstVec, 0, SubReg);
2388 MachineOperand &RegOp = I.getOperand(1);
2389 RegOp.setReg(Reg);
2390 RBI.constrainGenericRegister(DstReg, *RC, MRI);
2391 } else {
2392 // We don't need a subregister copy. Save a copy by re-using the
2393 // destination register on the final insert.
2394 assert(PrevMI && "PrevMI was null?");
2395 PrevMI->getOperand(0).setReg(I.getOperand(0).getReg());
2396 constrainSelectedInstRegOperands(*PrevMI, TII, TRI, RBI);
2397 }
2398
Amara Emerson5ec14602018-12-10 18:44:58 +00002399 I.eraseFromParent();
2400 return true;
2401}
2402
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002403/// SelectArithImmed - Select an immediate value that can be represented as
2404/// a 12-bit value shifted left by either 0 or 12. If so, return true with
2405/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002406InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00002407AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002408 MachineInstr &MI = *Root.getParent();
2409 MachineBasicBlock &MBB = *MI.getParent();
2410 MachineFunction &MF = *MBB.getParent();
2411 MachineRegisterInfo &MRI = MF.getRegInfo();
2412
2413 // This function is called from the addsub_shifted_imm ComplexPattern,
2414 // which lists [imm] as the list of opcode it's interested in, however
2415 // we still need to check whether the operand is actually an immediate
2416 // here because the ComplexPattern opcode list is only used in
2417 // root-level opcode matching.
2418 uint64_t Immed;
2419 if (Root.isImm())
2420 Immed = Root.getImm();
2421 else if (Root.isCImm())
2422 Immed = Root.getCImm()->getZExtValue();
2423 else if (Root.isReg()) {
2424 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
2425 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002426 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00002427 MachineOperand &Op1 = Def->getOperand(1);
2428 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002429 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00002430 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002431 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002432 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002433
2434 unsigned ShiftAmt;
2435
2436 if (Immed >> 12 == 0) {
2437 ShiftAmt = 0;
2438 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
2439 ShiftAmt = 12;
2440 Immed = Immed >> 12;
2441 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002442 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002443
2444 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00002445 return {{
2446 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
2447 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
2448 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002449}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00002450
Daniel Sandersea8711b2017-10-16 03:36:29 +00002451/// Select a "register plus unscaled signed 9-bit immediate" address. This
2452/// should only match when there is an offset that is not valid for a scaled
2453/// immediate addressing mode. The "Size" argument is the size in bytes of the
2454/// memory reference, which is needed here to know what is valid for a scaled
2455/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002456InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00002457AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
2458 unsigned Size) const {
2459 MachineRegisterInfo &MRI =
2460 Root.getParent()->getParent()->getParent()->getRegInfo();
2461
2462 if (!Root.isReg())
2463 return None;
2464
2465 if (!isBaseWithConstantOffset(Root, MRI))
2466 return None;
2467
2468 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
2469 if (!RootDef)
2470 return None;
2471
2472 MachineOperand &OffImm = RootDef->getOperand(2);
2473 if (!OffImm.isReg())
2474 return None;
2475 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
2476 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
2477 return None;
2478 int64_t RHSC;
2479 MachineOperand &RHSOp1 = RHS->getOperand(1);
2480 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
2481 return None;
2482 RHSC = RHSOp1.getCImm()->getSExtValue();
2483
2484 // If the offset is valid as a scaled immediate, don't match here.
2485 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
2486 return None;
2487 if (RHSC >= -256 && RHSC < 256) {
2488 MachineOperand &Base = RootDef->getOperand(1);
2489 return {{
2490 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
2491 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
2492 }};
2493 }
2494 return None;
2495}
2496
2497/// Select a "register plus scaled unsigned 12-bit immediate" address. The
2498/// "Size" argument is the size in bytes of the memory reference, which
2499/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00002500InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00002501AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
2502 unsigned Size) const {
2503 MachineRegisterInfo &MRI =
2504 Root.getParent()->getParent()->getParent()->getRegInfo();
2505
2506 if (!Root.isReg())
2507 return None;
2508
2509 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
2510 if (!RootDef)
2511 return None;
2512
2513 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
2514 return {{
2515 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
2516 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
2517 }};
2518 }
2519
2520 if (isBaseWithConstantOffset(Root, MRI)) {
2521 MachineOperand &LHS = RootDef->getOperand(1);
2522 MachineOperand &RHS = RootDef->getOperand(2);
2523 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
2524 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
2525 if (LHSDef && RHSDef) {
2526 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
2527 unsigned Scale = Log2_32(Size);
2528 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
2529 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00002530 return {{
2531 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
2532 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
2533 }};
2534
Daniel Sandersea8711b2017-10-16 03:36:29 +00002535 return {{
2536 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
2537 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
2538 }};
2539 }
2540 }
2541 }
2542
2543 // Before falling back to our general case, check if the unscaled
2544 // instructions can handle this. If so, that's preferable.
2545 if (selectAddrModeUnscaled(Root, Size).hasValue())
2546 return None;
2547
2548 return {{
2549 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
2550 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
2551 }};
2552}
2553
Volkan Kelesf7f25682018-01-16 18:44:05 +00002554void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
2555 const MachineInstr &MI) const {
2556 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2557 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
2558 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
2559 assert(CstVal && "Expected constant value");
2560 MIB.addImm(CstVal.getValue());
2561}
2562
Daniel Sanders0b5293f2017-04-06 09:49:34 +00002563namespace llvm {
2564InstructionSelector *
2565createAArch64InstructionSelector(const AArch64TargetMachine &TM,
2566 AArch64Subtarget &Subtarget,
2567 AArch64RegisterBankInfo &RBI) {
2568 return new AArch64InstructionSelector(TM, Subtarget, RBI);
2569}
2570}