blob: 4d7ca2349ed1392e6e931c0f662e3f31c68b1d50 [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the targeting of the InstructionSelector class for
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000015#include "AArch64InstrInfo.h"
Tim Northovere9600d82017-02-08 17:57:27 +000016#include "AArch64MachineFunctionInfo.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000017#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
Tim Northoverbdf16242016-10-10 21:50:00 +000020#include "AArch64TargetMachine.h"
Tim Northover9ac0eba2016-11-08 00:45:29 +000021#include "MCTargetDesc/AArch64AddressingModes.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000022#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
David Blaikie62651302017-10-26 23:39:54 +000023#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
Aditya Nandakumar75ad9cc2017-04-19 20:48:50 +000024#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000025#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders0b5293f2017-04-06 09:49:34 +000029#include "llvm/CodeGen/MachineOperand.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/IR/Type.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
34
35#define DEBUG_TYPE "aarch64-isel"
36
37using namespace llvm;
38
Daniel Sanders0b5293f2017-04-06 09:49:34 +000039namespace {
40
Daniel Sanderse7b0d662017-04-21 15:59:56 +000041#define GET_GLOBALISEL_PREDICATE_BITSET
42#include "AArch64GenGlobalISel.inc"
43#undef GET_GLOBALISEL_PREDICATE_BITSET
44
Daniel Sanders0b5293f2017-04-06 09:49:34 +000045class AArch64InstructionSelector : public InstructionSelector {
46public:
47 AArch64InstructionSelector(const AArch64TargetMachine &TM,
48 const AArch64Subtarget &STI,
49 const AArch64RegisterBankInfo &RBI);
50
Daniel Sandersf76f3152017-11-16 00:46:35 +000051 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
David Blaikie62651302017-10-26 23:39:54 +000052 static const char *getName() { return DEBUG_TYPE; }
Daniel Sanders0b5293f2017-04-06 09:49:34 +000053
54private:
55 /// tblgen-erated 'select' implementation, used as the initial selector for
56 /// the patterns that don't require complex C++.
Daniel Sandersf76f3152017-11-16 00:46:35 +000057 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000058
59 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
60 MachineRegisterInfo &MRI) const;
61 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
62 MachineRegisterInfo &MRI) const;
63
64 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
65 MachineRegisterInfo &MRI) const;
66
Daniel Sanders1e4569f2017-10-20 20:55:29 +000067 ComplexRendererFns selectArithImmed(MachineOperand &Root) const;
Daniel Sanders0b5293f2017-04-06 09:49:34 +000068
Daniel Sanders1e4569f2017-10-20 20:55:29 +000069 ComplexRendererFns selectAddrModeUnscaled(MachineOperand &Root,
70 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +000071
Daniel Sanders1e4569f2017-10-20 20:55:29 +000072 ComplexRendererFns selectAddrModeUnscaled8(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000073 return selectAddrModeUnscaled(Root, 1);
74 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000075 ComplexRendererFns selectAddrModeUnscaled16(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000076 return selectAddrModeUnscaled(Root, 2);
77 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000078 ComplexRendererFns selectAddrModeUnscaled32(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000079 return selectAddrModeUnscaled(Root, 4);
80 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000081 ComplexRendererFns selectAddrModeUnscaled64(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000082 return selectAddrModeUnscaled(Root, 8);
83 }
Daniel Sanders1e4569f2017-10-20 20:55:29 +000084 ComplexRendererFns selectAddrModeUnscaled128(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000085 return selectAddrModeUnscaled(Root, 16);
86 }
87
Daniel Sanders1e4569f2017-10-20 20:55:29 +000088 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root,
89 unsigned Size) const;
Daniel Sandersea8711b2017-10-16 03:36:29 +000090 template <int Width>
Daniel Sanders1e4569f2017-10-20 20:55:29 +000091 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root) const {
Daniel Sandersea8711b2017-10-16 03:36:29 +000092 return selectAddrModeIndexed(Root, Width / 8);
93 }
94
Volkan Kelesf7f25682018-01-16 18:44:05 +000095 void renderTruncImm(MachineInstrBuilder &MIB, const MachineInstr &MI) const;
96
Daniel Sanders0b5293f2017-04-06 09:49:34 +000097 const AArch64TargetMachine &TM;
98 const AArch64Subtarget &STI;
99 const AArch64InstrInfo &TII;
100 const AArch64RegisterInfo &TRI;
101 const AArch64RegisterBankInfo &RBI;
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000102
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000103#define GET_GLOBALISEL_PREDICATES_DECL
104#include "AArch64GenGlobalISel.inc"
105#undef GET_GLOBALISEL_PREDICATES_DECL
Daniel Sanders0b5293f2017-04-06 09:49:34 +0000106
107// We declare the temporaries used by selectImpl() in the class to minimize the
108// cost of constructing placeholder values.
109#define GET_GLOBALISEL_TEMPORARIES_DECL
110#include "AArch64GenGlobalISel.inc"
111#undef GET_GLOBALISEL_TEMPORARIES_DECL
112};
113
114} // end anonymous namespace
115
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000116#define GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000117#include "AArch64GenGlobalISel.inc"
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000118#undef GET_GLOBALISEL_IMPL
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000119
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000120AArch64InstructionSelector::AArch64InstructionSelector(
Tim Northoverbdf16242016-10-10 21:50:00 +0000121 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
122 const AArch64RegisterBankInfo &RBI)
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000123 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000124 TRI(*STI.getRegisterInfo()), RBI(RBI),
125#define GET_GLOBALISEL_PREDICATES_INIT
126#include "AArch64GenGlobalISel.inc"
127#undef GET_GLOBALISEL_PREDICATES_INIT
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000128#define GET_GLOBALISEL_TEMPORARIES_INIT
129#include "AArch64GenGlobalISel.inc"
130#undef GET_GLOBALISEL_TEMPORARIES_INIT
131{
132}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000133
Tim Northoverfb8d9892016-10-12 22:49:15 +0000134// FIXME: This should be target-independent, inferred from the types declared
135// for each class in the bank.
136static const TargetRegisterClass *
137getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
Amara Emerson3838ed02018-02-02 18:03:30 +0000138 const RegisterBankInfo &RBI,
139 bool GetAllRegSet = false) {
Tim Northoverfb8d9892016-10-12 22:49:15 +0000140 if (RB.getID() == AArch64::GPRRegBankID) {
141 if (Ty.getSizeInBits() <= 32)
Amara Emerson3838ed02018-02-02 18:03:30 +0000142 return GetAllRegSet ? &AArch64::GPR32allRegClass
143 : &AArch64::GPR32RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000144 if (Ty.getSizeInBits() == 64)
Amara Emerson3838ed02018-02-02 18:03:30 +0000145 return GetAllRegSet ? &AArch64::GPR64allRegClass
146 : &AArch64::GPR64RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000147 return nullptr;
148 }
149
150 if (RB.getID() == AArch64::FPRRegBankID) {
Amara Emerson3838ed02018-02-02 18:03:30 +0000151 if (Ty.getSizeInBits() <= 16)
152 return &AArch64::FPR16RegClass;
Tim Northoverfb8d9892016-10-12 22:49:15 +0000153 if (Ty.getSizeInBits() == 32)
154 return &AArch64::FPR32RegClass;
155 if (Ty.getSizeInBits() == 64)
156 return &AArch64::FPR64RegClass;
157 if (Ty.getSizeInBits() == 128)
158 return &AArch64::FPR128RegClass;
159 return nullptr;
160 }
161
162 return nullptr;
163}
164
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000165/// Check whether \p I is a currently unsupported binary operation:
166/// - it has an unsized type
167/// - an operand is not a vreg
168/// - all operands are not in the same bank
169/// These are checks that should someday live in the verifier, but right now,
170/// these are mostly limitations of the aarch64 selector.
171static bool unsupportedBinOp(const MachineInstr &I,
172 const AArch64RegisterBankInfo &RBI,
173 const MachineRegisterInfo &MRI,
174 const AArch64RegisterInfo &TRI) {
Tim Northover0f140c72016-09-09 11:46:34 +0000175 LLT Ty = MRI.getType(I.getOperand(0).getReg());
Tim Northover32a078a2016-09-15 10:09:59 +0000176 if (!Ty.isValid()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000177 LLVM_DEBUG(dbgs() << "Generic binop register should be typed\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000178 return true;
179 }
180
181 const RegisterBank *PrevOpBank = nullptr;
182 for (auto &MO : I.operands()) {
183 // FIXME: Support non-register operands.
184 if (!MO.isReg()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000185 LLVM_DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000186 return true;
187 }
188
189 // FIXME: Can generic operations have physical registers operands? If
190 // so, this will need to be taught about that, and we'll need to get the
191 // bank out of the minimal class for the register.
192 // Either way, this needs to be documented (and possibly verified).
193 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000194 LLVM_DEBUG(dbgs() << "Generic inst has physical register operand\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000195 return true;
196 }
197
198 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
199 if (!OpBank) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000200 LLVM_DEBUG(dbgs() << "Generic register has no bank or class\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000201 return true;
202 }
203
204 if (PrevOpBank && OpBank != PrevOpBank) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000205 LLVM_DEBUG(dbgs() << "Generic inst operands have different banks\n");
Ahmed Bougacha59e160a2016-08-16 14:37:40 +0000206 return true;
207 }
208 PrevOpBank = OpBank;
209 }
210 return false;
211}
212
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000213/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000214/// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000215/// and of size \p OpSize.
216/// \returns \p GenericOpc if the combination is unsupported.
217static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
218 unsigned OpSize) {
219 switch (RegBankID) {
220 case AArch64::GPRRegBankID:
Ahmed Bougacha05a5f7d2017-01-25 02:41:38 +0000221 if (OpSize == 32) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000222 switch (GenericOpc) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000223 case TargetOpcode::G_SHL:
224 return AArch64::LSLVWr;
225 case TargetOpcode::G_LSHR:
226 return AArch64::LSRVWr;
227 case TargetOpcode::G_ASHR:
228 return AArch64::ASRVWr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000229 default:
230 return GenericOpc;
231 }
Tim Northover55782222016-10-18 20:03:48 +0000232 } else if (OpSize == 64) {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000233 switch (GenericOpc) {
Tim Northover2fda4b02016-10-10 21:49:49 +0000234 case TargetOpcode::G_GEP:
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000235 return AArch64::ADDXrr;
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000236 case TargetOpcode::G_SHL:
237 return AArch64::LSLVXr;
238 case TargetOpcode::G_LSHR:
239 return AArch64::LSRVXr;
240 case TargetOpcode::G_ASHR:
241 return AArch64::ASRVXr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000242 default:
243 return GenericOpc;
244 }
245 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000246 break;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000247 case AArch64::FPRRegBankID:
248 switch (OpSize) {
249 case 32:
250 switch (GenericOpc) {
251 case TargetOpcode::G_FADD:
252 return AArch64::FADDSrr;
253 case TargetOpcode::G_FSUB:
254 return AArch64::FSUBSrr;
255 case TargetOpcode::G_FMUL:
256 return AArch64::FMULSrr;
257 case TargetOpcode::G_FDIV:
258 return AArch64::FDIVSrr;
259 default:
260 return GenericOpc;
261 }
262 case 64:
263 switch (GenericOpc) {
264 case TargetOpcode::G_FADD:
265 return AArch64::FADDDrr;
266 case TargetOpcode::G_FSUB:
267 return AArch64::FSUBDrr;
268 case TargetOpcode::G_FMUL:
269 return AArch64::FMULDrr;
270 case TargetOpcode::G_FDIV:
271 return AArch64::FDIVDrr;
Quentin Colombet0e531272016-10-11 00:21:11 +0000272 case TargetOpcode::G_OR:
273 return AArch64::ORRv8i8;
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000274 default:
275 return GenericOpc;
276 }
277 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000278 break;
279 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000280 return GenericOpc;
281}
282
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000283/// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
284/// appropriate for the (value) register bank \p RegBankID and of memory access
285/// size \p OpSize. This returns the variant with the base+unsigned-immediate
286/// addressing mode (e.g., LDRXui).
287/// \returns \p GenericOpc if the combination is unsupported.
288static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
289 unsigned OpSize) {
290 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
291 switch (RegBankID) {
292 case AArch64::GPRRegBankID:
293 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000294 case 8:
295 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
296 case 16:
297 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000298 case 32:
299 return isStore ? AArch64::STRWui : AArch64::LDRWui;
300 case 64:
301 return isStore ? AArch64::STRXui : AArch64::LDRXui;
302 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000303 break;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000304 case AArch64::FPRRegBankID:
305 switch (OpSize) {
Tim Northover020d1042016-10-17 18:36:53 +0000306 case 8:
307 return isStore ? AArch64::STRBui : AArch64::LDRBui;
308 case 16:
309 return isStore ? AArch64::STRHui : AArch64::LDRHui;
Quentin Colombetd2623f8e2016-10-11 00:21:14 +0000310 case 32:
311 return isStore ? AArch64::STRSui : AArch64::LDRSui;
312 case 64:
313 return isStore ? AArch64::STRDui : AArch64::LDRDui;
314 }
Simon Pilgrim9e901522017-07-08 19:28:24 +0000315 break;
316 }
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000317 return GenericOpc;
318}
319
Amara Emersondb211892018-02-20 05:11:57 +0000320static bool selectFP16CopyFromGPR32(MachineInstr &I, const TargetInstrInfo &TII,
321 MachineRegisterInfo &MRI, unsigned SrcReg) {
322 // Copies from gpr32 to fpr16 need to use a sub-register copy.
323 unsigned CopyReg = MRI.createVirtualRegister(&AArch64::FPR32RegClass);
324 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::COPY))
325 .addDef(CopyReg)
326 .addUse(SrcReg);
327 unsigned SubRegCopy = MRI.createVirtualRegister(&AArch64::FPR16RegClass);
328 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY))
329 .addDef(SubRegCopy)
330 .addUse(CopyReg, 0, AArch64::hsub);
331
332 MachineOperand &RegOp = I.getOperand(1);
333 RegOp.setReg(SubRegCopy);
334 return true;
335}
336
Quentin Colombetcb629a82016-10-12 03:57:49 +0000337static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
338 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
339 const RegisterBankInfo &RBI) {
340
341 unsigned DstReg = I.getOperand(0).getReg();
Amara Emersondb211892018-02-20 05:11:57 +0000342 unsigned SrcReg = I.getOperand(1).getReg();
343
Quentin Colombetcb629a82016-10-12 03:57:49 +0000344 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
Amara Emersondb211892018-02-20 05:11:57 +0000345 if (TRI.getRegClass(AArch64::FPR16RegClassID)->contains(DstReg) &&
346 !TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
347 const RegisterBank &RegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
348 const TargetRegisterClass *SrcRC = getRegClassForTypeOnBank(
349 MRI.getType(SrcReg), RegBank, RBI, /* GetAllRegSet */ true);
350 if (SrcRC == &AArch64::GPR32allRegClass)
351 return selectFP16CopyFromGPR32(I, TII, MRI, SrcReg);
352 }
Quentin Colombetcb629a82016-10-12 03:57:49 +0000353 assert(I.isCopy() && "Generic operators do not allow physical registers");
354 return true;
355 }
356
357 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
358 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
Amara Emerson3838ed02018-02-02 18:03:30 +0000359 (void)DstSize;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000360 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
361 (void)SrcSize;
362 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
363 "No phys reg on generic operators");
364 assert(
365 (DstSize == SrcSize ||
366 // Copies are a mean to setup initial types, the number of
367 // bits may not exactly match.
368 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
369 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
370 // Copies are a mean to copy bits around, as long as we are
371 // on the same register class, that's fine. Otherwise, that
372 // means we need some SUBREG_TO_REG or AND & co.
373 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
374 "Copy with different width?!");
375 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
376 "GPRs cannot get more than 64-bit width values");
Quentin Colombetcb629a82016-10-12 03:57:49 +0000377
Amara Emerson3838ed02018-02-02 18:03:30 +0000378 const TargetRegisterClass *RC = getRegClassForTypeOnBank(
379 MRI.getType(DstReg), RegBank, RBI, /* GetAllRegSet */ true);
380 if (!RC) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000381 LLVM_DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
Amara Emerson3838ed02018-02-02 18:03:30 +0000382 return false;
Quentin Colombetcb629a82016-10-12 03:57:49 +0000383 }
384
Amara Emerson7e9f3482018-02-18 17:10:49 +0000385 if (!TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
Amara Emersondb211892018-02-20 05:11:57 +0000386 const RegClassOrRegBank &RegClassOrBank = MRI.getRegClassOrRegBank(SrcReg);
Amara Emerson7e9f3482018-02-18 17:10:49 +0000387 const TargetRegisterClass *SrcRC =
388 RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
389 const RegisterBank *RB = nullptr;
390 if (!SrcRC) {
391 RB = RegClassOrBank.get<const RegisterBank *>();
392 SrcRC = getRegClassForTypeOnBank(MRI.getType(SrcReg), *RB, RBI, true);
393 }
394 // Copies from fpr16 to gpr32 need to use SUBREG_TO_REG.
395 if (RC == &AArch64::GPR32allRegClass && SrcRC == &AArch64::FPR16RegClass) {
396 unsigned PromoteReg = MRI.createVirtualRegister(&AArch64::FPR32RegClass);
397 BuildMI(*I.getParent(), I, I.getDebugLoc(),
398 TII.get(AArch64::SUBREG_TO_REG))
399 .addDef(PromoteReg)
400 .addImm(0)
401 .addUse(SrcReg)
402 .addImm(AArch64::hsub);
403 MachineOperand &RegOp = I.getOperand(1);
404 RegOp.setReg(PromoteReg);
Amara Emersondb211892018-02-20 05:11:57 +0000405 } else if (RC == &AArch64::FPR16RegClass &&
406 SrcRC == &AArch64::GPR32allRegClass) {
407 selectFP16CopyFromGPR32(I, TII, MRI, SrcReg);
Amara Emerson7e9f3482018-02-18 17:10:49 +0000408 }
409 }
410
Quentin Colombetcb629a82016-10-12 03:57:49 +0000411 // No need to constrain SrcReg. It will get constrained when
412 // we hit another of its use or its defs.
413 // Copies do not have constraints.
414 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000415 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
416 << " operand\n");
Quentin Colombetcb629a82016-10-12 03:57:49 +0000417 return false;
418 }
419 I.setDesc(TII.get(AArch64::COPY));
420 return true;
421}
422
Tim Northover69271c62016-10-12 22:49:11 +0000423static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
424 if (!DstTy.isScalar() || !SrcTy.isScalar())
425 return GenericOpc;
426
427 const unsigned DstSize = DstTy.getSizeInBits();
428 const unsigned SrcSize = SrcTy.getSizeInBits();
429
430 switch (DstSize) {
431 case 32:
432 switch (SrcSize) {
433 case 32:
434 switch (GenericOpc) {
435 case TargetOpcode::G_SITOFP:
436 return AArch64::SCVTFUWSri;
437 case TargetOpcode::G_UITOFP:
438 return AArch64::UCVTFUWSri;
439 case TargetOpcode::G_FPTOSI:
440 return AArch64::FCVTZSUWSr;
441 case TargetOpcode::G_FPTOUI:
442 return AArch64::FCVTZUUWSr;
443 default:
444 return GenericOpc;
445 }
446 case 64:
447 switch (GenericOpc) {
448 case TargetOpcode::G_SITOFP:
449 return AArch64::SCVTFUXSri;
450 case TargetOpcode::G_UITOFP:
451 return AArch64::UCVTFUXSri;
452 case TargetOpcode::G_FPTOSI:
453 return AArch64::FCVTZSUWDr;
454 case TargetOpcode::G_FPTOUI:
455 return AArch64::FCVTZUUWDr;
456 default:
457 return GenericOpc;
458 }
459 default:
460 return GenericOpc;
461 }
462 case 64:
463 switch (SrcSize) {
464 case 32:
465 switch (GenericOpc) {
466 case TargetOpcode::G_SITOFP:
467 return AArch64::SCVTFUWDri;
468 case TargetOpcode::G_UITOFP:
469 return AArch64::UCVTFUWDri;
470 case TargetOpcode::G_FPTOSI:
471 return AArch64::FCVTZSUXSr;
472 case TargetOpcode::G_FPTOUI:
473 return AArch64::FCVTZUUXSr;
474 default:
475 return GenericOpc;
476 }
477 case 64:
478 switch (GenericOpc) {
479 case TargetOpcode::G_SITOFP:
480 return AArch64::SCVTFUXDri;
481 case TargetOpcode::G_UITOFP:
482 return AArch64::UCVTFUXDri;
483 case TargetOpcode::G_FPTOSI:
484 return AArch64::FCVTZSUXDr;
485 case TargetOpcode::G_FPTOUI:
486 return AArch64::FCVTZUUXDr;
487 default:
488 return GenericOpc;
489 }
490 default:
491 return GenericOpc;
492 }
493 default:
494 return GenericOpc;
495 };
496 return GenericOpc;
497}
498
Tim Northover6c02ad52016-10-12 22:49:04 +0000499static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
500 switch (P) {
501 default:
502 llvm_unreachable("Unknown condition code!");
503 case CmpInst::ICMP_NE:
504 return AArch64CC::NE;
505 case CmpInst::ICMP_EQ:
506 return AArch64CC::EQ;
507 case CmpInst::ICMP_SGT:
508 return AArch64CC::GT;
509 case CmpInst::ICMP_SGE:
510 return AArch64CC::GE;
511 case CmpInst::ICMP_SLT:
512 return AArch64CC::LT;
513 case CmpInst::ICMP_SLE:
514 return AArch64CC::LE;
515 case CmpInst::ICMP_UGT:
516 return AArch64CC::HI;
517 case CmpInst::ICMP_UGE:
518 return AArch64CC::HS;
519 case CmpInst::ICMP_ULT:
520 return AArch64CC::LO;
521 case CmpInst::ICMP_ULE:
522 return AArch64CC::LS;
523 }
524}
525
Tim Northover7dd378d2016-10-12 22:49:07 +0000526static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
527 AArch64CC::CondCode &CondCode,
528 AArch64CC::CondCode &CondCode2) {
529 CondCode2 = AArch64CC::AL;
530 switch (P) {
531 default:
532 llvm_unreachable("Unknown FP condition!");
533 case CmpInst::FCMP_OEQ:
534 CondCode = AArch64CC::EQ;
535 break;
536 case CmpInst::FCMP_OGT:
537 CondCode = AArch64CC::GT;
538 break;
539 case CmpInst::FCMP_OGE:
540 CondCode = AArch64CC::GE;
541 break;
542 case CmpInst::FCMP_OLT:
543 CondCode = AArch64CC::MI;
544 break;
545 case CmpInst::FCMP_OLE:
546 CondCode = AArch64CC::LS;
547 break;
548 case CmpInst::FCMP_ONE:
549 CondCode = AArch64CC::MI;
550 CondCode2 = AArch64CC::GT;
551 break;
552 case CmpInst::FCMP_ORD:
553 CondCode = AArch64CC::VC;
554 break;
555 case CmpInst::FCMP_UNO:
556 CondCode = AArch64CC::VS;
557 break;
558 case CmpInst::FCMP_UEQ:
559 CondCode = AArch64CC::EQ;
560 CondCode2 = AArch64CC::VS;
561 break;
562 case CmpInst::FCMP_UGT:
563 CondCode = AArch64CC::HI;
564 break;
565 case CmpInst::FCMP_UGE:
566 CondCode = AArch64CC::PL;
567 break;
568 case CmpInst::FCMP_ULT:
569 CondCode = AArch64CC::LT;
570 break;
571 case CmpInst::FCMP_ULE:
572 CondCode = AArch64CC::LE;
573 break;
574 case CmpInst::FCMP_UNE:
575 CondCode = AArch64CC::NE;
576 break;
577 }
578}
579
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000580bool AArch64InstructionSelector::selectCompareBranch(
581 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
582
583 const unsigned CondReg = I.getOperand(0).getReg();
584 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
585 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000586 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
587 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000588 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
589 return false;
590
591 unsigned LHS = CCMI->getOperand(2).getReg();
592 unsigned RHS = CCMI->getOperand(3).getReg();
593 if (!getConstantVRegVal(RHS, MRI))
594 std::swap(RHS, LHS);
595
596 const auto RHSImm = getConstantVRegVal(RHS, MRI);
597 if (!RHSImm || *RHSImm != 0)
598 return false;
599
600 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
601 if (RB.getID() != AArch64::GPRRegBankID)
602 return false;
603
604 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
605 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
606 return false;
607
608 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
609 unsigned CBOpc = 0;
610 if (CmpWidth <= 32)
611 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
612 else if (CmpWidth == 64)
613 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
614 else
615 return false;
616
Aditya Nandakumar18b3f9d2018-01-17 19:31:33 +0000617 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
618 .addUse(LHS)
619 .addMBB(DestMBB)
620 .constrainAllUses(TII, TRI, RBI);
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000621
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000622 I.eraseFromParent();
623 return true;
624}
625
Tim Northovere9600d82017-02-08 17:57:27 +0000626bool AArch64InstructionSelector::selectVaStartAAPCS(
627 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
628 return false;
629}
630
631bool AArch64InstructionSelector::selectVaStartDarwin(
632 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
633 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
634 unsigned ListReg = I.getOperand(0).getReg();
635
636 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
637
638 auto MIB =
639 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
640 .addDef(ArgsAddrReg)
641 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
642 .addImm(0)
643 .addImm(0);
644
645 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
646
647 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
648 .addUse(ArgsAddrReg)
649 .addUse(ListReg)
650 .addImm(0)
651 .addMemOperand(*I.memoperands_begin());
652
653 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
654 I.eraseFromParent();
655 return true;
656}
657
Daniel Sandersf76f3152017-11-16 00:46:35 +0000658bool AArch64InstructionSelector::select(MachineInstr &I,
659 CodeGenCoverage &CoverageInfo) const {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000660 assert(I.getParent() && "Instruction should be in a basic block!");
661 assert(I.getParent()->getParent() && "Instruction should be in a function!");
662
663 MachineBasicBlock &MBB = *I.getParent();
664 MachineFunction &MF = *MBB.getParent();
665 MachineRegisterInfo &MRI = MF.getRegInfo();
666
Tim Northovercdf23f12016-10-31 18:30:59 +0000667 unsigned Opcode = I.getOpcode();
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000668 // G_PHI requires same handling as PHI
669 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
Tim Northovercdf23f12016-10-31 18:30:59 +0000670 // Certain non-generic instructions also need some special handling.
671
672 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
673 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000674
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000675 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
Tim Northover7d88da62016-11-08 00:34:06 +0000676 const unsigned DefReg = I.getOperand(0).getReg();
677 const LLT DefTy = MRI.getType(DefReg);
678
679 const TargetRegisterClass *DefRC = nullptr;
680 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
681 DefRC = TRI.getRegClass(DefReg);
682 } else {
683 const RegClassOrRegBank &RegClassOrBank =
684 MRI.getRegClassOrRegBank(DefReg);
685
686 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
687 if (!DefRC) {
688 if (!DefTy.isValid()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000689 LLVM_DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
Tim Northover7d88da62016-11-08 00:34:06 +0000690 return false;
691 }
692 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
693 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
694 if (!DefRC) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000695 LLVM_DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
Tim Northover7d88da62016-11-08 00:34:06 +0000696 return false;
697 }
698 }
699 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000700 I.setDesc(TII.get(TargetOpcode::PHI));
Tim Northover7d88da62016-11-08 00:34:06 +0000701
702 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
703 }
704
705 if (I.isCopy())
Tim Northovercdf23f12016-10-31 18:30:59 +0000706 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover7d88da62016-11-08 00:34:06 +0000707
708 return true;
Tim Northovercdf23f12016-10-31 18:30:59 +0000709 }
710
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000711
712 if (I.getNumOperands() != I.getNumExplicitOperands()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000713 LLVM_DEBUG(
714 dbgs() << "Generic instruction has unexpected implicit operands\n");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000715 return false;
716 }
717
Daniel Sandersf76f3152017-11-16 00:46:35 +0000718 if (selectImpl(I, CoverageInfo))
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000719 return true;
720
Tim Northover32a078a2016-09-15 10:09:59 +0000721 LLT Ty =
722 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000723
Tim Northover69271c62016-10-12 22:49:11 +0000724 switch (Opcode) {
Tim Northover5e3dbf32016-10-12 22:49:01 +0000725 case TargetOpcode::G_BRCOND: {
726 if (Ty.getSizeInBits() > 32) {
727 // We shouldn't need this on AArch64, but it would be implemented as an
728 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
729 // bit being tested is < 32.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000730 LLVM_DEBUG(dbgs() << "G_BRCOND has type: " << Ty
731 << ", expected at most 32-bits");
Tim Northover5e3dbf32016-10-12 22:49:01 +0000732 return false;
733 }
734
735 const unsigned CondReg = I.getOperand(0).getReg();
736 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
737
Ahmed Bougacha641cb202017-03-27 16:35:31 +0000738 if (selectCompareBranch(I, MF, MRI))
739 return true;
740
Tim Northover5e3dbf32016-10-12 22:49:01 +0000741 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
742 .addUse(CondReg)
743 .addImm(/*bit offset=*/0)
744 .addMBB(DestMBB);
745
746 I.eraseFromParent();
747 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
748 }
749
Kristof Beyls65a12c02017-01-30 09:13:18 +0000750 case TargetOpcode::G_BRINDIRECT: {
751 I.setDesc(TII.get(AArch64::BR));
752 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
753 }
754
Tim Northover4494d692016-10-18 19:47:57 +0000755 case TargetOpcode::G_FCONSTANT:
Tim Northover4edc60d2016-10-10 21:49:42 +0000756 case TargetOpcode::G_CONSTANT: {
Tim Northover4494d692016-10-18 19:47:57 +0000757 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
758
759 const LLT s32 = LLT::scalar(32);
760 const LLT s64 = LLT::scalar(64);
761 const LLT p0 = LLT::pointer(0, 64);
762
763 const unsigned DefReg = I.getOperand(0).getReg();
764 const LLT DefTy = MRI.getType(DefReg);
765 const unsigned DefSize = DefTy.getSizeInBits();
766 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
767
768 // FIXME: Redundant check, but even less readable when factored out.
769 if (isFP) {
770 if (Ty != s32 && Ty != s64) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000771 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
772 << " constant, expected: " << s32 << " or " << s64
773 << '\n');
Tim Northover4494d692016-10-18 19:47:57 +0000774 return false;
775 }
776
777 if (RB.getID() != AArch64::FPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000778 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
779 << " constant on bank: " << RB
780 << ", expected: FPR\n");
Tim Northover4494d692016-10-18 19:47:57 +0000781 return false;
782 }
Daniel Sanders11300ce2017-10-13 21:28:03 +0000783
784 // The case when we have 0.0 is covered by tablegen. Reject it here so we
785 // can be sure tablegen works correctly and isn't rescued by this code.
786 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
787 return false;
Tim Northover4494d692016-10-18 19:47:57 +0000788 } else {
Daniel Sanders05540042017-08-08 10:44:31 +0000789 // s32 and s64 are covered by tablegen.
790 if (Ty != p0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000791 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
792 << " constant, expected: " << s32 << ", " << s64
793 << ", or " << p0 << '\n');
Tim Northover4494d692016-10-18 19:47:57 +0000794 return false;
795 }
796
797 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000798 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
799 << " constant on bank: " << RB
800 << ", expected: GPR\n");
Tim Northover4494d692016-10-18 19:47:57 +0000801 return false;
802 }
803 }
804
805 const unsigned MovOpc =
806 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
807
808 I.setDesc(TII.get(MovOpc));
809
810 if (isFP) {
811 const TargetRegisterClass &GPRRC =
812 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
813 const TargetRegisterClass &FPRRC =
814 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
815
816 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
817 MachineOperand &RegOp = I.getOperand(0);
818 RegOp.setReg(DefGPRReg);
819
820 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
821 TII.get(AArch64::COPY))
822 .addDef(DefReg)
823 .addUse(DefGPRReg);
824
825 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000826 LLVM_DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
Tim Northover4494d692016-10-18 19:47:57 +0000827 return false;
828 }
829
830 MachineOperand &ImmOp = I.getOperand(1);
831 // FIXME: Is going through int64_t always correct?
832 ImmOp.ChangeToImmediate(
833 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000834 } else if (I.getOperand(1).isCImm()) {
Tim Northover9267ac52016-12-05 21:47:07 +0000835 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
836 I.getOperand(1).ChangeToImmediate(Val);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000837 } else if (I.getOperand(1).isImm()) {
838 uint64_t Val = I.getOperand(1).getImm();
839 I.getOperand(1).ChangeToImmediate(Val);
Tim Northover4494d692016-10-18 19:47:57 +0000840 }
841
842 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
843 return true;
Tim Northover4edc60d2016-10-10 21:49:42 +0000844 }
Tim Northover7b6d66c2017-07-20 22:58:38 +0000845 case TargetOpcode::G_EXTRACT: {
846 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000847 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
Amara Emerson242efdb2018-02-18 17:28:34 +0000848 (void)DstTy;
Amara Emersonbc03bae2018-02-18 17:03:02 +0000849 unsigned SrcSize = SrcTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000850 // Larger extracts are vectors, same-size extracts should be something else
851 // by now (either split up or simplified to a COPY).
852 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
853 return false;
854
Amara Emersonbc03bae2018-02-18 17:03:02 +0000855 I.setDesc(TII.get(SrcSize == 64 ? AArch64::UBFMXri : AArch64::UBFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000856 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
857 Ty.getSizeInBits() - 1);
858
Amara Emersonbc03bae2018-02-18 17:03:02 +0000859 if (SrcSize < 64) {
860 assert(SrcSize == 32 && DstTy.getSizeInBits() == 16 &&
861 "unexpected G_EXTRACT types");
862 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
863 }
864
Tim Northover7b6d66c2017-07-20 22:58:38 +0000865 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
866 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
867 TII.get(AArch64::COPY))
868 .addDef(I.getOperand(0).getReg())
869 .addUse(DstReg, 0, AArch64::sub_32);
870 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
871 AArch64::GPR32RegClass, MRI);
872 I.getOperand(0).setReg(DstReg);
873
874 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
875 }
876
877 case TargetOpcode::G_INSERT: {
878 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
Amara Emersonbc03bae2018-02-18 17:03:02 +0000879 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
880 unsigned DstSize = DstTy.getSizeInBits();
Tim Northover7b6d66c2017-07-20 22:58:38 +0000881 // Larger inserts are vectors, same-size ones should be something else by
882 // now (split up or turned into COPYs).
883 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
884 return false;
885
Amara Emersonbc03bae2018-02-18 17:03:02 +0000886 I.setDesc(TII.get(DstSize == 64 ? AArch64::BFMXri : AArch64::BFMWri));
Tim Northover7b6d66c2017-07-20 22:58:38 +0000887 unsigned LSB = I.getOperand(3).getImm();
888 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
Amara Emersonbc03bae2018-02-18 17:03:02 +0000889 I.getOperand(3).setImm((DstSize - LSB) % DstSize);
Tim Northover7b6d66c2017-07-20 22:58:38 +0000890 MachineInstrBuilder(MF, I).addImm(Width - 1);
891
Amara Emersonbc03bae2018-02-18 17:03:02 +0000892 if (DstSize < 64) {
893 assert(DstSize == 32 && SrcTy.getSizeInBits() == 16 &&
894 "unexpected G_INSERT types");
895 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
896 }
897
Tim Northover7b6d66c2017-07-20 22:58:38 +0000898 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
899 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
900 TII.get(AArch64::SUBREG_TO_REG))
901 .addDef(SrcReg)
902 .addImm(0)
903 .addUse(I.getOperand(2).getReg())
904 .addImm(AArch64::sub_32);
905 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
906 AArch64::GPR32RegClass, MRI);
907 I.getOperand(2).setReg(SrcReg);
908
909 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
910 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000911 case TargetOpcode::G_FRAME_INDEX: {
912 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
Tim Northover5ae83502016-09-15 09:20:34 +0000913 if (Ty != LLT::pointer(0, 64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000914 LLVM_DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
915 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000916 return false;
917 }
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000918 I.setDesc(TII.get(AArch64::ADDXri));
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000919
920 // MOs for a #0 shifted immediate.
921 I.addOperand(MachineOperand::CreateImm(0));
922 I.addOperand(MachineOperand::CreateImm(0));
923
924 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
925 }
Tim Northoverbdf16242016-10-10 21:50:00 +0000926
927 case TargetOpcode::G_GLOBAL_VALUE: {
928 auto GV = I.getOperand(1).getGlobal();
929 if (GV->isThreadLocal()) {
930 // FIXME: we don't support TLS yet.
931 return false;
932 }
933 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000934 if (OpFlags & AArch64II::MO_GOT) {
Tim Northoverbdf16242016-10-10 21:50:00 +0000935 I.setDesc(TII.get(AArch64::LOADgot));
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000936 I.getOperand(1).setTargetFlags(OpFlags);
Amara Emersond5785772018-01-18 19:21:27 +0000937 } else if (TM.getCodeModel() == CodeModel::Large) {
938 // Materialize the global using movz/movk instructions.
939 unsigned MovZDstReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
940 auto InsertPt = std::next(I.getIterator());
941 auto MovZ =
942 BuildMI(MBB, InsertPt, I.getDebugLoc(), TII.get(AArch64::MOVZXi))
943 .addDef(MovZDstReg);
944 MovZ->addOperand(MF, I.getOperand(1));
945 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
946 AArch64II::MO_NC);
947 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
948 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
949
950 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags,
951 unsigned Offset, unsigned ForceDstReg) {
952 unsigned DstReg =
953 ForceDstReg ? ForceDstReg
954 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
955 auto MovI = BuildMI(MBB, InsertPt, MovZ->getDebugLoc(),
956 TII.get(AArch64::MOVKXi))
957 .addDef(DstReg)
958 .addReg(SrcReg);
959 MovI->addOperand(MF, MachineOperand::CreateGA(
960 GV, MovZ->getOperand(1).getOffset(), Flags));
961 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
962 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
963 return DstReg;
964 };
965 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(),
966 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
967 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
968 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
969 I.eraseFromParent();
970 return true;
Tim Northoverfe7c59a2016-12-13 18:25:38 +0000971 } else {
Tim Northoverbdf16242016-10-10 21:50:00 +0000972 I.setDesc(TII.get(AArch64::MOVaddr));
973 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
974 MachineInstrBuilder MIB(MF, I);
975 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
976 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
977 }
978 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
979 }
980
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000981 case TargetOpcode::G_LOAD:
982 case TargetOpcode::G_STORE: {
Tim Northover0f140c72016-09-09 11:46:34 +0000983 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000984
Tim Northover5ae83502016-09-15 09:20:34 +0000985 if (PtrTy != LLT::pointer(0, 64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000986 LLVM_DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
987 << ", expected: " << LLT::pointer(0, 64) << '\n');
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000988 return false;
989 }
990
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000991 auto &MemOp = **I.memoperands_begin();
992 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000993 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000994 return false;
995 }
Daniel Sandersf84bc372018-05-05 20:53:24 +0000996 unsigned MemSizeInBits = MemOp.getSize() * 8;
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000997
Amara Emerson4f84f882018-01-24 20:35:37 +0000998 // FIXME: PR36018: Volatile loads in some cases are incorrectly selected by
999 // folding with an extend. Until we have a G_SEXTLOAD solution bail out if
1000 // we hit one.
1001 if (Opcode == TargetOpcode::G_LOAD && MemOp.isVolatile())
1002 return false;
1003
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001004 const unsigned PtrReg = I.getOperand(1).getReg();
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001005#ifndef NDEBUG
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001006 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
Ahmed Bougachaf0b22c42017-03-27 18:14:20 +00001007 // Sanity-check the pointer register.
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001008 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
1009 "Load/Store pointer operand isn't a GPR");
Tim Northover0f140c72016-09-09 11:46:34 +00001010 assert(MRI.getType(PtrReg).isPointer() &&
1011 "Load/Store pointer operand isn't a pointer");
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001012#endif
1013
1014 const unsigned ValReg = I.getOperand(0).getReg();
1015 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
1016
1017 const unsigned NewOpc =
Daniel Sandersf84bc372018-05-05 20:53:24 +00001018 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemSizeInBits);
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001019 if (NewOpc == I.getOpcode())
1020 return false;
1021
1022 I.setDesc(TII.get(NewOpc));
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001023
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001024 uint64_t Offset = 0;
1025 auto *PtrMI = MRI.getVRegDef(PtrReg);
1026
1027 // Try to fold a GEP into our unsigned immediate addressing mode.
1028 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
1029 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
1030 int64_t Imm = *COff;
Daniel Sandersf84bc372018-05-05 20:53:24 +00001031 const unsigned Size = MemSizeInBits / 8;
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001032 const unsigned Scale = Log2_32(Size);
1033 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
1034 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
1035 I.getOperand(1).setReg(Ptr2Reg);
1036 PtrMI = MRI.getVRegDef(Ptr2Reg);
1037 Offset = Imm / Size;
1038 }
1039 }
1040 }
1041
Ahmed Bougachaf75782f2017-03-27 17:31:56 +00001042 // If we haven't folded anything into our addressing mode yet, try to fold
1043 // a frame index into the base+offset.
1044 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1045 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
1046
Ahmed Bougacha8a654082017-03-27 17:31:52 +00001047 I.addOperand(MachineOperand::CreateImm(Offset));
Ahmed Bougacha85a66a62017-03-27 17:31:48 +00001048
1049 // If we're storing a 0, use WZR/XZR.
1050 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
1051 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
1052 if (I.getOpcode() == AArch64::STRWui)
1053 I.getOperand(0).setReg(AArch64::WZR);
1054 else if (I.getOpcode() == AArch64::STRXui)
1055 I.getOperand(0).setReg(AArch64::XZR);
1056 }
1057 }
1058
Ahmed Bougacha7adfac52016-07-29 16:56:16 +00001059 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1060 }
1061
Tim Northover9dd78f82017-02-08 21:22:25 +00001062 case TargetOpcode::G_SMULH:
1063 case TargetOpcode::G_UMULH: {
1064 // Reject the various things we don't support yet.
1065 if (unsupportedBinOp(I, RBI, MRI, TRI))
1066 return false;
1067
1068 const unsigned DefReg = I.getOperand(0).getReg();
1069 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1070
1071 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001072 LLVM_DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
Tim Northover9dd78f82017-02-08 21:22:25 +00001073 return false;
1074 }
1075
1076 if (Ty != LLT::scalar(64)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001077 LLVM_DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1078 << ", expected: " << LLT::scalar(64) << '\n');
Tim Northover9dd78f82017-02-08 21:22:25 +00001079 return false;
1080 }
1081
1082 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1083 : AArch64::UMULHrr;
1084 I.setDesc(TII.get(NewOpc));
1085
1086 // Now that we selected an opcode, we need to constrain the register
1087 // operands to use appropriate classes.
1088 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1089 }
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +00001090 case TargetOpcode::G_FADD:
1091 case TargetOpcode::G_FSUB:
1092 case TargetOpcode::G_FMUL:
1093 case TargetOpcode::G_FDIV:
1094
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001095 case TargetOpcode::G_OR:
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +00001096 case TargetOpcode::G_SHL:
1097 case TargetOpcode::G_LSHR:
1098 case TargetOpcode::G_ASHR:
Tim Northover2fda4b02016-10-10 21:49:49 +00001099 case TargetOpcode::G_GEP: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001100 // Reject the various things we don't support yet.
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001101 if (unsupportedBinOp(I, RBI, MRI, TRI))
1102 return false;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001103
Ahmed Bougacha59e160a2016-08-16 14:37:40 +00001104 const unsigned OpSize = Ty.getSizeInBits();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001105
1106 const unsigned DefReg = I.getOperand(0).getReg();
1107 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1108
1109 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1110 if (NewOpc == I.getOpcode())
1111 return false;
1112
1113 I.setDesc(TII.get(NewOpc));
1114 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001115
1116 // Now that we selected an opcode, we need to constrain the register
1117 // operands to use appropriate classes.
1118 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1119 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001120
Tim Northover398c5f52017-02-14 20:56:29 +00001121 case TargetOpcode::G_PTR_MASK: {
1122 uint64_t Align = I.getOperand(2).getImm();
1123 if (Align >= 64 || Align == 0)
1124 return false;
1125
1126 uint64_t Mask = ~((1ULL << Align) - 1);
1127 I.setDesc(TII.get(AArch64::ANDXri));
1128 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1129
1130 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1131 }
Tim Northover037af52c2016-10-31 18:31:09 +00001132 case TargetOpcode::G_PTRTOINT:
Tim Northoverfb8d9892016-10-12 22:49:15 +00001133 case TargetOpcode::G_TRUNC: {
1134 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1135 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1136
1137 const unsigned DstReg = I.getOperand(0).getReg();
1138 const unsigned SrcReg = I.getOperand(1).getReg();
1139
1140 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1141 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1142
1143 if (DstRB.getID() != SrcRB.getID()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001144 LLVM_DEBUG(
1145 dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001146 return false;
1147 }
1148
1149 if (DstRB.getID() == AArch64::GPRRegBankID) {
1150 const TargetRegisterClass *DstRC =
1151 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1152 if (!DstRC)
1153 return false;
1154
1155 const TargetRegisterClass *SrcRC =
1156 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1157 if (!SrcRC)
1158 return false;
1159
1160 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1161 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001162 LLVM_DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001163 return false;
1164 }
1165
1166 if (DstRC == SrcRC) {
1167 // Nothing to be done
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001168 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1169 SrcTy == LLT::scalar(64)) {
1170 llvm_unreachable("TableGen can import this case");
1171 return false;
Tim Northoverfb8d9892016-10-12 22:49:15 +00001172 } else if (DstRC == &AArch64::GPR32RegClass &&
1173 SrcRC == &AArch64::GPR64RegClass) {
1174 I.getOperand(1).setSubReg(AArch64::sub_32);
1175 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001176 LLVM_DEBUG(
1177 dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
Tim Northoverfb8d9892016-10-12 22:49:15 +00001178 return false;
1179 }
1180
1181 I.setDesc(TII.get(TargetOpcode::COPY));
1182 return true;
1183 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1184 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1185 I.setDesc(TII.get(AArch64::XTNv4i16));
1186 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1187 return true;
1188 }
1189 }
1190
1191 return false;
1192 }
1193
Tim Northover3d38b3a2016-10-11 20:50:21 +00001194 case TargetOpcode::G_ANYEXT: {
1195 const unsigned DstReg = I.getOperand(0).getReg();
1196 const unsigned SrcReg = I.getOperand(1).getReg();
1197
Quentin Colombetcb629a82016-10-12 03:57:49 +00001198 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1199 if (RBDst.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001200 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst
1201 << ", expected: GPR\n");
Quentin Colombetcb629a82016-10-12 03:57:49 +00001202 return false;
1203 }
Tim Northover3d38b3a2016-10-11 20:50:21 +00001204
Quentin Colombetcb629a82016-10-12 03:57:49 +00001205 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1206 if (RBSrc.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001207 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc
1208 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001209 return false;
1210 }
1211
1212 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1213
1214 if (DstSize == 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001215 LLVM_DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001216 return false;
1217 }
1218
Quentin Colombetcb629a82016-10-12 03:57:49 +00001219 if (DstSize != 64 && DstSize > 32) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001220 LLVM_DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1221 << ", expected: 32 or 64\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001222 return false;
1223 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001224 // At this point G_ANYEXT is just like a plain COPY, but we need
1225 // to explicitly form the 64-bit value if any.
1226 if (DstSize > 32) {
1227 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1228 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1229 .addDef(ExtSrc)
1230 .addImm(0)
1231 .addUse(SrcReg)
1232 .addImm(AArch64::sub_32);
1233 I.getOperand(1).setReg(ExtSrc);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001234 }
Quentin Colombetcb629a82016-10-12 03:57:49 +00001235 return selectCopy(I, TII, MRI, TRI, RBI);
Tim Northover3d38b3a2016-10-11 20:50:21 +00001236 }
1237
1238 case TargetOpcode::G_ZEXT:
1239 case TargetOpcode::G_SEXT: {
1240 unsigned Opcode = I.getOpcode();
1241 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1242 SrcTy = MRI.getType(I.getOperand(1).getReg());
1243 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1244 const unsigned DefReg = I.getOperand(0).getReg();
1245 const unsigned SrcReg = I.getOperand(1).getReg();
1246 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1247
1248 if (RB.getID() != AArch64::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001249 LLVM_DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1250 << ", expected: GPR\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001251 return false;
1252 }
1253
1254 MachineInstr *ExtI;
1255 if (DstTy == LLT::scalar(64)) {
1256 // FIXME: Can we avoid manually doing this?
1257 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001258 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1259 << " operand\n");
Tim Northover3d38b3a2016-10-11 20:50:21 +00001260 return false;
1261 }
1262
1263 const unsigned SrcXReg =
1264 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1265 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1266 .addDef(SrcXReg)
1267 .addImm(0)
1268 .addUse(SrcReg)
1269 .addImm(AArch64::sub_32);
1270
1271 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1272 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1273 .addDef(DefReg)
1274 .addUse(SrcXReg)
1275 .addImm(0)
1276 .addImm(SrcTy.getSizeInBits() - 1);
Tim Northovera9105be2016-11-09 22:39:54 +00001277 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
Tim Northover3d38b3a2016-10-11 20:50:21 +00001278 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1279 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1280 .addDef(DefReg)
1281 .addUse(SrcReg)
1282 .addImm(0)
1283 .addImm(SrcTy.getSizeInBits() - 1);
1284 } else {
1285 return false;
1286 }
1287
1288 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1289
1290 I.eraseFromParent();
1291 return true;
1292 }
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001293
Tim Northover69271c62016-10-12 22:49:11 +00001294 case TargetOpcode::G_SITOFP:
1295 case TargetOpcode::G_UITOFP:
1296 case TargetOpcode::G_FPTOSI:
1297 case TargetOpcode::G_FPTOUI: {
1298 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1299 SrcTy = MRI.getType(I.getOperand(1).getReg());
1300 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1301 if (NewOpc == Opcode)
1302 return false;
1303
1304 I.setDesc(TII.get(NewOpc));
1305 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1306
1307 return true;
1308 }
1309
1310
Tim Northoverc1d8c2b2016-10-11 22:29:23 +00001311 case TargetOpcode::G_INTTOPTR:
Daniel Sandersedd07842017-08-17 09:26:14 +00001312 // The importer is currently unable to import pointer types since they
1313 // didn't exist in SelectionDAG.
Daniel Sanderseb2f5f32017-08-15 15:10:31 +00001314 return selectCopy(I, TII, MRI, TRI, RBI);
Daniel Sanders16e6dd32017-08-15 13:50:09 +00001315
Daniel Sandersedd07842017-08-17 09:26:14 +00001316 case TargetOpcode::G_BITCAST:
1317 // Imported SelectionDAG rules can handle every bitcast except those that
1318 // bitcast from a type to the same type. Ideally, these shouldn't occur
1319 // but we might not run an optimizer that deletes them.
1320 if (MRI.getType(I.getOperand(0).getReg()) ==
1321 MRI.getType(I.getOperand(1).getReg()))
1322 return selectCopy(I, TII, MRI, TRI, RBI);
1323 return false;
1324
Tim Northover9ac0eba2016-11-08 00:45:29 +00001325 case TargetOpcode::G_SELECT: {
1326 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001327 LLVM_DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1328 << ", expected: " << LLT::scalar(1) << '\n');
Tim Northover9ac0eba2016-11-08 00:45:29 +00001329 return false;
1330 }
1331
1332 const unsigned CondReg = I.getOperand(1).getReg();
1333 const unsigned TReg = I.getOperand(2).getReg();
1334 const unsigned FReg = I.getOperand(3).getReg();
1335
1336 unsigned CSelOpc = 0;
1337
1338 if (Ty == LLT::scalar(32)) {
1339 CSelOpc = AArch64::CSELWr;
Kristof Beylse9412b42017-01-19 13:32:14 +00001340 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
Tim Northover9ac0eba2016-11-08 00:45:29 +00001341 CSelOpc = AArch64::CSELXr;
1342 } else {
1343 return false;
1344 }
1345
1346 MachineInstr &TstMI =
1347 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1348 .addDef(AArch64::WZR)
1349 .addUse(CondReg)
1350 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1351
1352 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1353 .addDef(I.getOperand(0).getReg())
1354 .addUse(TReg)
1355 .addUse(FReg)
1356 .addImm(AArch64CC::NE);
1357
1358 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1359 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1360
1361 I.eraseFromParent();
1362 return true;
1363 }
Tim Northover6c02ad52016-10-12 22:49:04 +00001364 case TargetOpcode::G_ICMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001365 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001366 LLVM_DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1367 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover6c02ad52016-10-12 22:49:04 +00001368 return false;
1369 }
1370
1371 unsigned CmpOpc = 0;
1372 unsigned ZReg = 0;
1373
1374 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1375 if (CmpTy == LLT::scalar(32)) {
1376 CmpOpc = AArch64::SUBSWrr;
1377 ZReg = AArch64::WZR;
1378 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1379 CmpOpc = AArch64::SUBSXrr;
1380 ZReg = AArch64::XZR;
1381 } else {
1382 return false;
1383 }
1384
Kristof Beyls22524402017-01-05 10:16:08 +00001385 // CSINC increments the result by one when the condition code is false.
1386 // Therefore, we have to invert the predicate to get an increment by 1 when
1387 // the predicate is true.
1388 const AArch64CC::CondCode invCC =
1389 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1390 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
Tim Northover6c02ad52016-10-12 22:49:04 +00001391
1392 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1393 .addDef(ZReg)
1394 .addUse(I.getOperand(2).getReg())
1395 .addUse(I.getOperand(3).getReg());
1396
1397 MachineInstr &CSetMI =
1398 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1399 .addDef(I.getOperand(0).getReg())
1400 .addUse(AArch64::WZR)
1401 .addUse(AArch64::WZR)
Kristof Beyls22524402017-01-05 10:16:08 +00001402 .addImm(invCC);
Tim Northover6c02ad52016-10-12 22:49:04 +00001403
1404 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1405 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1406
1407 I.eraseFromParent();
1408 return true;
1409 }
1410
Tim Northover7dd378d2016-10-12 22:49:07 +00001411 case TargetOpcode::G_FCMP: {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +00001412 if (Ty != LLT::scalar(32)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001413 LLVM_DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1414 << ", expected: " << LLT::scalar(32) << '\n');
Tim Northover7dd378d2016-10-12 22:49:07 +00001415 return false;
1416 }
1417
1418 unsigned CmpOpc = 0;
1419 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1420 if (CmpTy == LLT::scalar(32)) {
1421 CmpOpc = AArch64::FCMPSrr;
1422 } else if (CmpTy == LLT::scalar(64)) {
1423 CmpOpc = AArch64::FCMPDrr;
1424 } else {
1425 return false;
1426 }
1427
1428 // FIXME: regbank
1429
1430 AArch64CC::CondCode CC1, CC2;
1431 changeFCMPPredToAArch64CC(
1432 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1433
1434 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1435 .addUse(I.getOperand(2).getReg())
1436 .addUse(I.getOperand(3).getReg());
1437
1438 const unsigned DefReg = I.getOperand(0).getReg();
1439 unsigned Def1Reg = DefReg;
1440 if (CC2 != AArch64CC::AL)
1441 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1442
1443 MachineInstr &CSetMI =
1444 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1445 .addDef(Def1Reg)
1446 .addUse(AArch64::WZR)
1447 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001448 .addImm(getInvertedCondCode(CC1));
Tim Northover7dd378d2016-10-12 22:49:07 +00001449
1450 if (CC2 != AArch64CC::AL) {
1451 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1452 MachineInstr &CSet2MI =
1453 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1454 .addDef(Def2Reg)
1455 .addUse(AArch64::WZR)
1456 .addUse(AArch64::WZR)
Tim Northover33a1a0b2017-01-17 23:04:01 +00001457 .addImm(getInvertedCondCode(CC2));
Tim Northover7dd378d2016-10-12 22:49:07 +00001458 MachineInstr &OrMI =
1459 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1460 .addDef(DefReg)
1461 .addUse(Def1Reg)
1462 .addUse(Def2Reg);
1463 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1464 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1465 }
1466
1467 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1468 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1469
1470 I.eraseFromParent();
1471 return true;
1472 }
Tim Northovere9600d82017-02-08 17:57:27 +00001473 case TargetOpcode::G_VASTART:
1474 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1475 : selectVaStartAAPCS(I, MF, MRI);
Amara Emerson1f5d9942018-04-25 14:43:59 +00001476 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
1477 if (!I.getOperand(0).isIntrinsicID())
1478 return false;
1479 if (I.getOperand(0).getIntrinsicID() != Intrinsic::trap)
1480 return false;
1481 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::BRK))
1482 .addImm(1);
1483 I.eraseFromParent();
1484 return true;
Justin Bogner4fc69662017-07-12 17:32:32 +00001485 case TargetOpcode::G_IMPLICIT_DEF:
1486 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
Amara Emerson58aea522018-02-02 01:44:43 +00001487 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1488 const unsigned DstReg = I.getOperand(0).getReg();
1489 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1490 const TargetRegisterClass *DstRC =
1491 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1492 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
Justin Bogner4fc69662017-07-12 17:32:32 +00001493 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001494 }
1495
1496 return false;
1497}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001498
1499/// SelectArithImmed - Select an immediate value that can be represented as
1500/// a 12-bit value shifted left by either 0 or 12. If so, return true with
1501/// Val set to the 12-bit value and Shift set to the shifter operand.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001502InstructionSelector::ComplexRendererFns
Daniel Sanders2deea182017-04-22 15:11:04 +00001503AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001504 MachineInstr &MI = *Root.getParent();
1505 MachineBasicBlock &MBB = *MI.getParent();
1506 MachineFunction &MF = *MBB.getParent();
1507 MachineRegisterInfo &MRI = MF.getRegInfo();
1508
1509 // This function is called from the addsub_shifted_imm ComplexPattern,
1510 // which lists [imm] as the list of opcode it's interested in, however
1511 // we still need to check whether the operand is actually an immediate
1512 // here because the ComplexPattern opcode list is only used in
1513 // root-level opcode matching.
1514 uint64_t Immed;
1515 if (Root.isImm())
1516 Immed = Root.getImm();
1517 else if (Root.isCImm())
1518 Immed = Root.getCImm()->getZExtValue();
1519 else if (Root.isReg()) {
1520 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1521 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001522 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001523 MachineOperand &Op1 = Def->getOperand(1);
1524 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001525 return None;
Daniel Sanders0e642022017-03-16 18:04:50 +00001526 Immed = Op1.getCImm()->getZExtValue();
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001527 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001528 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001529
1530 unsigned ShiftAmt;
1531
1532 if (Immed >> 12 == 0) {
1533 ShiftAmt = 0;
1534 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1535 ShiftAmt = 12;
1536 Immed = Immed >> 12;
1537 } else
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001538 return None;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001539
1540 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Daniel Sandersdf39cba2017-10-15 18:22:54 +00001541 return {{
1542 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1543 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1544 }};
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001545}
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001546
Daniel Sandersea8711b2017-10-16 03:36:29 +00001547/// Select a "register plus unscaled signed 9-bit immediate" address. This
1548/// should only match when there is an offset that is not valid for a scaled
1549/// immediate addressing mode. The "Size" argument is the size in bytes of the
1550/// memory reference, which is needed here to know what is valid for a scaled
1551/// immediate.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001552InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001553AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1554 unsigned Size) const {
1555 MachineRegisterInfo &MRI =
1556 Root.getParent()->getParent()->getParent()->getRegInfo();
1557
1558 if (!Root.isReg())
1559 return None;
1560
1561 if (!isBaseWithConstantOffset(Root, MRI))
1562 return None;
1563
1564 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1565 if (!RootDef)
1566 return None;
1567
1568 MachineOperand &OffImm = RootDef->getOperand(2);
1569 if (!OffImm.isReg())
1570 return None;
1571 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1572 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1573 return None;
1574 int64_t RHSC;
1575 MachineOperand &RHSOp1 = RHS->getOperand(1);
1576 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1577 return None;
1578 RHSC = RHSOp1.getCImm()->getSExtValue();
1579
1580 // If the offset is valid as a scaled immediate, don't match here.
1581 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1582 return None;
1583 if (RHSC >= -256 && RHSC < 256) {
1584 MachineOperand &Base = RootDef->getOperand(1);
1585 return {{
1586 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1587 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1588 }};
1589 }
1590 return None;
1591}
1592
1593/// Select a "register plus scaled unsigned 12-bit immediate" address. The
1594/// "Size" argument is the size in bytes of the memory reference, which
1595/// determines the scale.
Daniel Sanders1e4569f2017-10-20 20:55:29 +00001596InstructionSelector::ComplexRendererFns
Daniel Sandersea8711b2017-10-16 03:36:29 +00001597AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1598 unsigned Size) const {
1599 MachineRegisterInfo &MRI =
1600 Root.getParent()->getParent()->getParent()->getRegInfo();
1601
1602 if (!Root.isReg())
1603 return None;
1604
1605 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1606 if (!RootDef)
1607 return None;
1608
1609 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1610 return {{
1611 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1612 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1613 }};
1614 }
1615
1616 if (isBaseWithConstantOffset(Root, MRI)) {
1617 MachineOperand &LHS = RootDef->getOperand(1);
1618 MachineOperand &RHS = RootDef->getOperand(2);
1619 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1620 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1621 if (LHSDef && RHSDef) {
1622 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1623 unsigned Scale = Log2_32(Size);
1624 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1625 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
Daniel Sanders01805b62017-10-16 05:39:30 +00001626 return {{
1627 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1628 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1629 }};
1630
Daniel Sandersea8711b2017-10-16 03:36:29 +00001631 return {{
1632 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1633 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1634 }};
1635 }
1636 }
1637 }
1638
1639 // Before falling back to our general case, check if the unscaled
1640 // instructions can handle this. If so, that's preferable.
1641 if (selectAddrModeUnscaled(Root, Size).hasValue())
1642 return None;
1643
1644 return {{
1645 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1646 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1647 }};
1648}
1649
Volkan Kelesf7f25682018-01-16 18:44:05 +00001650void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1651 const MachineInstr &MI) const {
1652 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1653 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1654 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1655 assert(CstVal && "Expected constant value");
1656 MIB.addImm(CstVal.getValue());
1657}
1658
Daniel Sanders0b5293f2017-04-06 09:49:34 +00001659namespace llvm {
1660InstructionSelector *
1661createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1662 AArch64Subtarget &Subtarget,
1663 AArch64RegisterBankInfo &RBI) {
1664 return new AArch64InstructionSelector(TM, Subtarget, RBI);
1665}
1666}