1 //===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==//
3 // 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
7 //===----------------------------------------------------------------------===//
9 /// This file implements the targeting of the InstructionSelector class for
11 /// \todo This should be generated by TableGen.
12 //===----------------------------------------------------------------------===//
14 #include "AArch64InstrInfo.h"
15 #include "AArch64MachineFunctionInfo.h"
16 #include "AArch64RegisterBankInfo.h"
17 #include "AArch64RegisterInfo.h"
18 #include "AArch64Subtarget.h"
19 #include "AArch64TargetMachine.h"
20 #include "MCTargetDesc/AArch64AddressingModes.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
23 #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
24 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
25 #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
26 #include "llvm/CodeGen/GlobalISel/Utils.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineOperand.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
38 #define DEBUG_TYPE "aarch64-isel"
44 #define GET_GLOBALISEL_PREDICATE_BITSET
45 #include "AArch64GenGlobalISel.inc"
46 #undef GET_GLOBALISEL_PREDICATE_BITSET
48 class AArch64InstructionSelector
: public InstructionSelector
{
50 AArch64InstructionSelector(const AArch64TargetMachine
&TM
,
51 const AArch64Subtarget
&STI
,
52 const AArch64RegisterBankInfo
&RBI
);
54 bool select(MachineInstr
&I
) override
;
55 static const char *getName() { return DEBUG_TYPE
; }
57 void setupMF(MachineFunction
&MF
, GISelKnownBits
&KB
,
58 CodeGenCoverage
&CoverageInfo
) override
{
59 InstructionSelector::setupMF(MF
, KB
, CoverageInfo
);
61 // hasFnAttribute() is expensive to call on every BRCOND selection, so
62 // cache it here for each run of the selector.
63 ProduceNonFlagSettingCondBr
=
64 !MF
.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening
);
68 /// tblgen-erated 'select' implementation, used as the initial selector for
69 /// the patterns that don't require complex C++.
70 bool selectImpl(MachineInstr
&I
, CodeGenCoverage
&CoverageInfo
) const;
72 // A lowering phase that runs before any selection attempts.
74 void preISelLower(MachineInstr
&I
) const;
76 // An early selection function that runs before the selectImpl() call.
77 bool earlySelect(MachineInstr
&I
) const;
79 bool earlySelectSHL(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
81 /// Eliminate same-sized cross-bank copies into stores before selectImpl().
82 void contractCrossBankCopyIntoStore(MachineInstr
&I
,
83 MachineRegisterInfo
&MRI
) const;
85 bool selectVaStartAAPCS(MachineInstr
&I
, MachineFunction
&MF
,
86 MachineRegisterInfo
&MRI
) const;
87 bool selectVaStartDarwin(MachineInstr
&I
, MachineFunction
&MF
,
88 MachineRegisterInfo
&MRI
) const;
90 bool selectCompareBranch(MachineInstr
&I
, MachineFunction
&MF
,
91 MachineRegisterInfo
&MRI
) const;
93 bool selectVectorASHR(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
94 bool selectVectorSHL(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
96 // Helper to generate an equivalent of scalar_to_vector into a new register,
97 // returned via 'Dst'.
98 MachineInstr
*emitScalarToVector(unsigned EltSize
,
99 const TargetRegisterClass
*DstRC
,
101 MachineIRBuilder
&MIRBuilder
) const;
103 /// Emit a lane insert into \p DstReg, or a new vector register if None is
106 /// The lane inserted into is defined by \p LaneIdx. The vector source
107 /// register is given by \p SrcReg. The register containing the element is
108 /// given by \p EltReg.
109 MachineInstr
*emitLaneInsert(Optional
<Register
> DstReg
, Register SrcReg
,
110 Register EltReg
, unsigned LaneIdx
,
111 const RegisterBank
&RB
,
112 MachineIRBuilder
&MIRBuilder
) const;
113 bool selectInsertElt(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
114 bool selectBuildVector(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
115 bool selectMergeValues(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
116 bool selectUnmergeValues(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
118 bool selectShuffleVector(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
119 bool selectExtractElt(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
120 bool selectConcatVectors(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
121 bool selectSplitVectorUnmerge(MachineInstr
&I
,
122 MachineRegisterInfo
&MRI
) const;
123 bool selectIntrinsicWithSideEffects(MachineInstr
&I
,
124 MachineRegisterInfo
&MRI
) const;
125 bool selectIntrinsic(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
126 bool selectVectorICmp(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
127 bool selectIntrinsicTrunc(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
128 bool selectIntrinsicRound(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
129 bool selectJumpTable(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
130 bool selectBrJT(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
131 bool selectTLSGlobalValue(MachineInstr
&I
, MachineRegisterInfo
&MRI
) const;
133 unsigned emitConstantPoolEntry(Constant
*CPVal
, MachineFunction
&MF
) const;
134 MachineInstr
*emitLoadFromConstantPool(Constant
*CPVal
,
135 MachineIRBuilder
&MIRBuilder
) const;
137 // Emit a vector concat operation.
138 MachineInstr
*emitVectorConcat(Optional
<Register
> Dst
, Register Op1
,
140 MachineIRBuilder
&MIRBuilder
) const;
141 MachineInstr
*emitIntegerCompare(MachineOperand
&LHS
, MachineOperand
&RHS
,
142 MachineOperand
&Predicate
,
143 MachineIRBuilder
&MIRBuilder
) const;
144 MachineInstr
*emitADD(Register DefReg
, MachineOperand
&LHS
, MachineOperand
&RHS
,
145 MachineIRBuilder
&MIRBuilder
) const;
146 MachineInstr
*emitCMN(MachineOperand
&LHS
, MachineOperand
&RHS
,
147 MachineIRBuilder
&MIRBuilder
) const;
148 MachineInstr
*emitTST(const Register
&LHS
, const Register
&RHS
,
149 MachineIRBuilder
&MIRBuilder
) const;
150 MachineInstr
*emitExtractVectorElt(Optional
<Register
> DstReg
,
151 const RegisterBank
&DstRB
, LLT ScalarTy
,
152 Register VecReg
, unsigned LaneIdx
,
153 MachineIRBuilder
&MIRBuilder
) const;
155 /// Helper function for selecting G_FCONSTANT. If the G_FCONSTANT can be
156 /// materialized using a FMOV instruction, then update MI and return it.
157 /// Otherwise, do nothing and return a nullptr.
158 MachineInstr
*emitFMovForFConstant(MachineInstr
&MI
,
159 MachineRegisterInfo
&MRI
) const;
161 /// Emit a CSet for a compare.
162 MachineInstr
*emitCSetForICMP(Register DefReg
, unsigned Pred
,
163 MachineIRBuilder
&MIRBuilder
) const;
165 // Equivalent to the i32shift_a and friends from AArch64InstrInfo.td.
166 // We use these manually instead of using the importer since it doesn't
167 // support SDNodeXForm.
168 ComplexRendererFns
selectShiftA_32(const MachineOperand
&Root
) const;
169 ComplexRendererFns
selectShiftB_32(const MachineOperand
&Root
) const;
170 ComplexRendererFns
selectShiftA_64(const MachineOperand
&Root
) const;
171 ComplexRendererFns
selectShiftB_64(const MachineOperand
&Root
) const;
173 ComplexRendererFns
select12BitValueWithLeftShift(uint64_t Immed
) const;
174 ComplexRendererFns
selectArithImmed(MachineOperand
&Root
) const;
175 ComplexRendererFns
selectNegArithImmed(MachineOperand
&Root
) const;
177 ComplexRendererFns
selectAddrModeUnscaled(MachineOperand
&Root
,
178 unsigned Size
) const;
180 ComplexRendererFns
selectAddrModeUnscaled8(MachineOperand
&Root
) const {
181 return selectAddrModeUnscaled(Root
, 1);
183 ComplexRendererFns
selectAddrModeUnscaled16(MachineOperand
&Root
) const {
184 return selectAddrModeUnscaled(Root
, 2);
186 ComplexRendererFns
selectAddrModeUnscaled32(MachineOperand
&Root
) const {
187 return selectAddrModeUnscaled(Root
, 4);
189 ComplexRendererFns
selectAddrModeUnscaled64(MachineOperand
&Root
) const {
190 return selectAddrModeUnscaled(Root
, 8);
192 ComplexRendererFns
selectAddrModeUnscaled128(MachineOperand
&Root
) const {
193 return selectAddrModeUnscaled(Root
, 16);
196 ComplexRendererFns
selectAddrModeIndexed(MachineOperand
&Root
,
197 unsigned Size
) const;
199 ComplexRendererFns
selectAddrModeIndexed(MachineOperand
&Root
) const {
200 return selectAddrModeIndexed(Root
, Width
/ 8);
203 bool isWorthFoldingIntoExtendedReg(MachineInstr
&MI
,
204 const MachineRegisterInfo
&MRI
) const;
206 selectAddrModeShiftedExtendXReg(MachineOperand
&Root
,
207 unsigned SizeInBytes
) const;
208 ComplexRendererFns
selectAddrModeRegisterOffset(MachineOperand
&Root
) const;
209 ComplexRendererFns
selectAddrModeXRO(MachineOperand
&Root
,
210 unsigned SizeInBytes
) const;
212 ComplexRendererFns
selectAddrModeXRO(MachineOperand
&Root
) const {
213 return selectAddrModeXRO(Root
, Width
/ 8);
216 ComplexRendererFns
selectShiftedRegister(MachineOperand
&Root
) const;
218 ComplexRendererFns
selectArithShiftedRegister(MachineOperand
&Root
) const {
219 return selectShiftedRegister(Root
);
222 ComplexRendererFns
selectLogicalShiftedRegister(MachineOperand
&Root
) const {
223 // TODO: selectShiftedRegister should allow for rotates on logical shifts.
224 // For now, make them the same. The only difference between the two is that
225 // logical shifts are allowed to fold in rotates. Otherwise, these are
226 // functionally the same.
227 return selectShiftedRegister(Root
);
230 /// Instructions that accept extend modifiers like UXTW expect the register
231 /// being extended to be a GPR32. Narrow ExtReg to a 32-bit register using a
232 /// subregister copy if necessary. Return either ExtReg, or the result of the
234 Register
narrowExtendRegIfNeeded(Register ExtReg
,
235 MachineIRBuilder
&MIB
) const;
236 ComplexRendererFns
selectArithExtendedRegister(MachineOperand
&Root
) const;
238 void renderTruncImm(MachineInstrBuilder
&MIB
, const MachineInstr
&MI
) const;
239 void renderLogicalImm32(MachineInstrBuilder
&MIB
, const MachineInstr
&I
) const;
240 void renderLogicalImm64(MachineInstrBuilder
&MIB
, const MachineInstr
&I
) const;
242 // Materialize a GlobalValue or BlockAddress using a movz+movk sequence.
243 void materializeLargeCMVal(MachineInstr
&I
, const Value
*V
,
244 unsigned OpFlags
) const;
246 // Optimization methods.
247 bool tryOptVectorShuffle(MachineInstr
&I
) const;
248 bool tryOptVectorDup(MachineInstr
&MI
) const;
249 bool tryOptSelect(MachineInstr
&MI
) const;
250 MachineInstr
*tryFoldIntegerCompare(MachineOperand
&LHS
, MachineOperand
&RHS
,
251 MachineOperand
&Predicate
,
252 MachineIRBuilder
&MIRBuilder
) const;
254 /// Return true if \p MI is a load or store of \p NumBytes bytes.
255 bool isLoadStoreOfNumBytes(const MachineInstr
&MI
, unsigned NumBytes
) const;
257 /// Returns true if \p MI is guaranteed to have the high-half of a 64-bit
258 /// register zeroed out. In other words, the result of MI has been explicitly
260 bool isDef32(const MachineInstr
&MI
) const;
262 const AArch64TargetMachine
&TM
;
263 const AArch64Subtarget
&STI
;
264 const AArch64InstrInfo
&TII
;
265 const AArch64RegisterInfo
&TRI
;
266 const AArch64RegisterBankInfo
&RBI
;
268 bool ProduceNonFlagSettingCondBr
= false;
270 #define GET_GLOBALISEL_PREDICATES_DECL
271 #include "AArch64GenGlobalISel.inc"
272 #undef GET_GLOBALISEL_PREDICATES_DECL
274 // We declare the temporaries used by selectImpl() in the class to minimize the
275 // cost of constructing placeholder values.
276 #define GET_GLOBALISEL_TEMPORARIES_DECL
277 #include "AArch64GenGlobalISel.inc"
278 #undef GET_GLOBALISEL_TEMPORARIES_DECL
281 } // end anonymous namespace
283 #define GET_GLOBALISEL_IMPL
284 #include "AArch64GenGlobalISel.inc"
285 #undef GET_GLOBALISEL_IMPL
287 AArch64InstructionSelector::AArch64InstructionSelector(
288 const AArch64TargetMachine
&TM
, const AArch64Subtarget
&STI
,
289 const AArch64RegisterBankInfo
&RBI
)
290 : InstructionSelector(), TM(TM
), STI(STI
), TII(*STI
.getInstrInfo()),
291 TRI(*STI
.getRegisterInfo()), RBI(RBI
),
292 #define GET_GLOBALISEL_PREDICATES_INIT
293 #include "AArch64GenGlobalISel.inc"
294 #undef GET_GLOBALISEL_PREDICATES_INIT
295 #define GET_GLOBALISEL_TEMPORARIES_INIT
296 #include "AArch64GenGlobalISel.inc"
297 #undef GET_GLOBALISEL_TEMPORARIES_INIT
301 // FIXME: This should be target-independent, inferred from the types declared
302 // for each class in the bank.
303 static const TargetRegisterClass
*
304 getRegClassForTypeOnBank(LLT Ty
, const RegisterBank
&RB
,
305 const RegisterBankInfo
&RBI
,
306 bool GetAllRegSet
= false) {
307 if (RB
.getID() == AArch64::GPRRegBankID
) {
308 if (Ty
.getSizeInBits() <= 32)
309 return GetAllRegSet
? &AArch64::GPR32allRegClass
310 : &AArch64::GPR32RegClass
;
311 if (Ty
.getSizeInBits() == 64)
312 return GetAllRegSet
? &AArch64::GPR64allRegClass
313 : &AArch64::GPR64RegClass
;
317 if (RB
.getID() == AArch64::FPRRegBankID
) {
318 if (Ty
.getSizeInBits() <= 16)
319 return &AArch64::FPR16RegClass
;
320 if (Ty
.getSizeInBits() == 32)
321 return &AArch64::FPR32RegClass
;
322 if (Ty
.getSizeInBits() == 64)
323 return &AArch64::FPR64RegClass
;
324 if (Ty
.getSizeInBits() == 128)
325 return &AArch64::FPR128RegClass
;
332 /// Given a register bank, and size in bits, return the smallest register class
333 /// that can represent that combination.
334 static const TargetRegisterClass
*
335 getMinClassForRegBank(const RegisterBank
&RB
, unsigned SizeInBits
,
336 bool GetAllRegSet
= false) {
337 unsigned RegBankID
= RB
.getID();
339 if (RegBankID
== AArch64::GPRRegBankID
) {
340 if (SizeInBits
<= 32)
341 return GetAllRegSet
? &AArch64::GPR32allRegClass
342 : &AArch64::GPR32RegClass
;
343 if (SizeInBits
== 64)
344 return GetAllRegSet
? &AArch64::GPR64allRegClass
345 : &AArch64::GPR64RegClass
;
348 if (RegBankID
== AArch64::FPRRegBankID
) {
349 switch (SizeInBits
) {
353 return &AArch64::FPR8RegClass
;
355 return &AArch64::FPR16RegClass
;
357 return &AArch64::FPR32RegClass
;
359 return &AArch64::FPR64RegClass
;
361 return &AArch64::FPR128RegClass
;
368 /// Returns the correct subregister to use for a given register class.
369 static bool getSubRegForClass(const TargetRegisterClass
*RC
,
370 const TargetRegisterInfo
&TRI
, unsigned &SubReg
) {
371 switch (TRI
.getRegSizeInBits(*RC
)) {
373 SubReg
= AArch64::bsub
;
376 SubReg
= AArch64::hsub
;
379 if (RC
!= &AArch64::FPR32RegClass
)
380 SubReg
= AArch64::sub_32
;
382 SubReg
= AArch64::ssub
;
385 SubReg
= AArch64::dsub
;
389 dbgs() << "Couldn't find appropriate subregister for register class.");
396 /// Check whether \p I is a currently unsupported binary operation:
397 /// - it has an unsized type
398 /// - an operand is not a vreg
399 /// - all operands are not in the same bank
400 /// These are checks that should someday live in the verifier, but right now,
401 /// these are mostly limitations of the aarch64 selector.
402 static bool unsupportedBinOp(const MachineInstr
&I
,
403 const AArch64RegisterBankInfo
&RBI
,
404 const MachineRegisterInfo
&MRI
,
405 const AArch64RegisterInfo
&TRI
) {
406 LLT Ty
= MRI
.getType(I
.getOperand(0).getReg());
408 LLVM_DEBUG(dbgs() << "Generic binop register should be typed\n");
412 const RegisterBank
*PrevOpBank
= nullptr;
413 for (auto &MO
: I
.operands()) {
414 // FIXME: Support non-register operands.
416 LLVM_DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
420 // FIXME: Can generic operations have physical registers operands? If
421 // so, this will need to be taught about that, and we'll need to get the
422 // bank out of the minimal class for the register.
423 // Either way, this needs to be documented (and possibly verified).
424 if (!Register::isVirtualRegister(MO
.getReg())) {
425 LLVM_DEBUG(dbgs() << "Generic inst has physical register operand\n");
429 const RegisterBank
*OpBank
= RBI
.getRegBank(MO
.getReg(), MRI
, TRI
);
431 LLVM_DEBUG(dbgs() << "Generic register has no bank or class\n");
435 if (PrevOpBank
&& OpBank
!= PrevOpBank
) {
436 LLVM_DEBUG(dbgs() << "Generic inst operands have different banks\n");
444 /// Select the AArch64 opcode for the basic binary operation \p GenericOpc
445 /// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
446 /// and of size \p OpSize.
447 /// \returns \p GenericOpc if the combination is unsupported.
448 static unsigned selectBinaryOp(unsigned GenericOpc
, unsigned RegBankID
,
451 case AArch64::GPRRegBankID
:
453 switch (GenericOpc
) {
454 case TargetOpcode::G_SHL
:
455 return AArch64::LSLVWr
;
456 case TargetOpcode::G_LSHR
:
457 return AArch64::LSRVWr
;
458 case TargetOpcode::G_ASHR
:
459 return AArch64::ASRVWr
;
463 } else if (OpSize
== 64) {
464 switch (GenericOpc
) {
465 case TargetOpcode::G_GEP
:
466 return AArch64::ADDXrr
;
467 case TargetOpcode::G_SHL
:
468 return AArch64::LSLVXr
;
469 case TargetOpcode::G_LSHR
:
470 return AArch64::LSRVXr
;
471 case TargetOpcode::G_ASHR
:
472 return AArch64::ASRVXr
;
478 case AArch64::FPRRegBankID
:
481 switch (GenericOpc
) {
482 case TargetOpcode::G_FADD
:
483 return AArch64::FADDSrr
;
484 case TargetOpcode::G_FSUB
:
485 return AArch64::FSUBSrr
;
486 case TargetOpcode::G_FMUL
:
487 return AArch64::FMULSrr
;
488 case TargetOpcode::G_FDIV
:
489 return AArch64::FDIVSrr
;
494 switch (GenericOpc
) {
495 case TargetOpcode::G_FADD
:
496 return AArch64::FADDDrr
;
497 case TargetOpcode::G_FSUB
:
498 return AArch64::FSUBDrr
;
499 case TargetOpcode::G_FMUL
:
500 return AArch64::FMULDrr
;
501 case TargetOpcode::G_FDIV
:
502 return AArch64::FDIVDrr
;
503 case TargetOpcode::G_OR
:
504 return AArch64::ORRv8i8
;
514 /// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
515 /// appropriate for the (value) register bank \p RegBankID and of memory access
516 /// size \p OpSize. This returns the variant with the base+unsigned-immediate
517 /// addressing mode (e.g., LDRXui).
518 /// \returns \p GenericOpc if the combination is unsupported.
519 static unsigned selectLoadStoreUIOp(unsigned GenericOpc
, unsigned RegBankID
,
521 const bool isStore
= GenericOpc
== TargetOpcode::G_STORE
;
523 case AArch64::GPRRegBankID
:
526 return isStore
? AArch64::STRBBui
: AArch64::LDRBBui
;
528 return isStore
? AArch64::STRHHui
: AArch64::LDRHHui
;
530 return isStore
? AArch64::STRWui
: AArch64::LDRWui
;
532 return isStore
? AArch64::STRXui
: AArch64::LDRXui
;
535 case AArch64::FPRRegBankID
:
538 return isStore
? AArch64::STRBui
: AArch64::LDRBui
;
540 return isStore
? AArch64::STRHui
: AArch64::LDRHui
;
542 return isStore
? AArch64::STRSui
: AArch64::LDRSui
;
544 return isStore
? AArch64::STRDui
: AArch64::LDRDui
;
552 /// Helper function that verifies that we have a valid copy at the end of
553 /// selectCopy. Verifies that the source and dest have the expected sizes and
554 /// then returns true.
555 static bool isValidCopy(const MachineInstr
&I
, const RegisterBank
&DstBank
,
556 const MachineRegisterInfo
&MRI
,
557 const TargetRegisterInfo
&TRI
,
558 const RegisterBankInfo
&RBI
) {
559 const Register DstReg
= I
.getOperand(0).getReg();
560 const Register SrcReg
= I
.getOperand(1).getReg();
561 const unsigned DstSize
= RBI
.getSizeInBits(DstReg
, MRI
, TRI
);
562 const unsigned SrcSize
= RBI
.getSizeInBits(SrcReg
, MRI
, TRI
);
564 // Make sure the size of the source and dest line up.
566 (DstSize
== SrcSize
||
567 // Copies are a mean to setup initial types, the number of
568 // bits may not exactly match.
569 (Register::isPhysicalRegister(SrcReg
) && DstSize
<= SrcSize
) ||
570 // Copies are a mean to copy bits around, as long as we are
571 // on the same register class, that's fine. Otherwise, that
572 // means we need some SUBREG_TO_REG or AND & co.
573 (((DstSize
+ 31) / 32 == (SrcSize
+ 31) / 32) && DstSize
> SrcSize
)) &&
574 "Copy with different width?!");
576 // Check the size of the destination.
577 assert((DstSize
<= 64 || DstBank
.getID() == AArch64::FPRRegBankID
) &&
578 "GPRs cannot get more than 64-bit width values");
584 /// Helper function for selectCopy. Inserts a subregister copy from
585 /// \p *From to \p *To, linking it up to \p I.
587 /// e.g, given I = "Dst = COPY SrcReg", we'll transform that into
589 /// CopyReg (From class) = COPY SrcReg
590 /// SubRegCopy (To class) = COPY CopyReg:SubReg
591 /// Dst = COPY SubRegCopy
592 static bool selectSubregisterCopy(MachineInstr
&I
, MachineRegisterInfo
&MRI
,
593 const RegisterBankInfo
&RBI
, Register SrcReg
,
594 const TargetRegisterClass
*From
,
595 const TargetRegisterClass
*To
,
597 MachineIRBuilder
MIB(I
);
598 auto Copy
= MIB
.buildCopy({From
}, {SrcReg
});
599 auto SubRegCopy
= MIB
.buildInstr(TargetOpcode::COPY
, {To
}, {})
600 .addReg(Copy
.getReg(0), 0, SubReg
);
601 MachineOperand
&RegOp
= I
.getOperand(1);
602 RegOp
.setReg(SubRegCopy
.getReg(0));
604 // It's possible that the destination register won't be constrained. Make
605 // sure that happens.
606 if (!Register::isPhysicalRegister(I
.getOperand(0).getReg()))
607 RBI
.constrainGenericRegister(I
.getOperand(0).getReg(), *To
, MRI
);
612 /// Helper function to get the source and destination register classes for a
613 /// copy. Returns a std::pair containing the source register class for the
614 /// copy, and the destination register class for the copy. If a register class
615 /// cannot be determined, then it will be nullptr.
616 static std::pair
<const TargetRegisterClass
*, const TargetRegisterClass
*>
617 getRegClassesForCopy(MachineInstr
&I
, const TargetInstrInfo
&TII
,
618 MachineRegisterInfo
&MRI
, const TargetRegisterInfo
&TRI
,
619 const RegisterBankInfo
&RBI
) {
620 Register DstReg
= I
.getOperand(0).getReg();
621 Register SrcReg
= I
.getOperand(1).getReg();
622 const RegisterBank
&DstRegBank
= *RBI
.getRegBank(DstReg
, MRI
, TRI
);
623 const RegisterBank
&SrcRegBank
= *RBI
.getRegBank(SrcReg
, MRI
, TRI
);
624 unsigned DstSize
= RBI
.getSizeInBits(DstReg
, MRI
, TRI
);
625 unsigned SrcSize
= RBI
.getSizeInBits(SrcReg
, MRI
, TRI
);
627 // Special casing for cross-bank copies of s1s. We can technically represent
628 // a 1-bit value with any size of register. The minimum size for a GPR is 32
629 // bits. So, we need to put the FPR on 32 bits as well.
631 // FIXME: I'm not sure if this case holds true outside of copies. If it does,
632 // then we can pull it into the helpers that get the appropriate class for a
633 // register bank. Or make a new helper that carries along some constraint
635 if (SrcRegBank
!= DstRegBank
&& (DstSize
== 1 && SrcSize
== 1))
636 SrcSize
= DstSize
= 32;
638 return {getMinClassForRegBank(SrcRegBank
, SrcSize
, true),
639 getMinClassForRegBank(DstRegBank
, DstSize
, true)};
642 static bool selectCopy(MachineInstr
&I
, const TargetInstrInfo
&TII
,
643 MachineRegisterInfo
&MRI
, const TargetRegisterInfo
&TRI
,
644 const RegisterBankInfo
&RBI
) {
646 Register DstReg
= I
.getOperand(0).getReg();
647 Register SrcReg
= I
.getOperand(1).getReg();
648 const RegisterBank
&DstRegBank
= *RBI
.getRegBank(DstReg
, MRI
, TRI
);
649 const RegisterBank
&SrcRegBank
= *RBI
.getRegBank(SrcReg
, MRI
, TRI
);
651 // Find the correct register classes for the source and destination registers.
652 const TargetRegisterClass
*SrcRC
;
653 const TargetRegisterClass
*DstRC
;
654 std::tie(SrcRC
, DstRC
) = getRegClassesForCopy(I
, TII
, MRI
, TRI
, RBI
);
657 LLVM_DEBUG(dbgs() << "Unexpected dest size "
658 << RBI
.getSizeInBits(DstReg
, MRI
, TRI
) << '\n');
662 // A couple helpers below, for making sure that the copy we produce is valid.
664 // Set to true if we insert a SUBREG_TO_REG. If we do this, then we don't want
665 // to verify that the src and dst are the same size, since that's handled by
666 // the SUBREG_TO_REG.
667 bool KnownValid
= false;
669 // Returns true, or asserts if something we don't expect happens. Instead of
670 // returning true, we return isValidCopy() to ensure that we verify the
672 auto CheckCopy
= [&]() {
673 // If we have a bitcast or something, we can't have physical registers.
674 assert((I
.isCopy() ||
675 (!Register::isPhysicalRegister(I
.getOperand(0).getReg()) &&
676 !Register::isPhysicalRegister(I
.getOperand(1).getReg()))) &&
677 "No phys reg on generic operator!");
678 assert(KnownValid
|| isValidCopy(I
, DstRegBank
, MRI
, TRI
, RBI
));
683 // Is this a copy? If so, then we may need to insert a subregister copy, or
686 // Yes. Check if there's anything to fix up.
688 LLVM_DEBUG(dbgs() << "Couldn't determine source register class\n");
692 unsigned SrcSize
= TRI
.getRegSizeInBits(*SrcRC
);
693 unsigned DstSize
= TRI
.getRegSizeInBits(*DstRC
);
695 // If we're doing a cross-bank copy on different-sized registers, we need
696 // to do a bit more work.
697 if (SrcSize
> DstSize
) {
698 // We're doing a cross-bank copy into a smaller register. We need a
699 // subregister copy. First, get a register class that's on the same bank
700 // as the destination, but the same size as the source.
701 const TargetRegisterClass
*SubregRC
=
702 getMinClassForRegBank(DstRegBank
, SrcSize
, true);
703 assert(SubregRC
&& "Didn't get a register class for subreg?");
705 // Get the appropriate subregister for the destination.
707 if (!getSubRegForClass(DstRC
, TRI
, SubReg
)) {
708 LLVM_DEBUG(dbgs() << "Couldn't determine subregister for copy.\n");
712 // Now, insert a subregister copy using the new register class.
713 selectSubregisterCopy(I
, MRI
, RBI
, SrcReg
, SubregRC
, DstRC
, SubReg
);
717 // Is this a cross-bank copy?
718 if (DstRegBank
.getID() != SrcRegBank
.getID()) {
719 if (DstRegBank
.getID() == AArch64::GPRRegBankID
&& DstSize
== 32 &&
721 // Special case for FPR16 to GPR32.
722 // FIXME: This can probably be generalized like the above case.
723 Register PromoteReg
=
724 MRI
.createVirtualRegister(&AArch64::FPR32RegClass
);
725 BuildMI(*I
.getParent(), I
, I
.getDebugLoc(),
726 TII
.get(AArch64::SUBREG_TO_REG
), PromoteReg
)
729 .addImm(AArch64::hsub
);
730 MachineOperand
&RegOp
= I
.getOperand(1);
731 RegOp
.setReg(PromoteReg
);
733 // Promise that the copy is implicitly validated by the SUBREG_TO_REG.
738 // If the destination is a physical register, then there's nothing to
739 // change, so we're done.
740 if (Register::isPhysicalRegister(DstReg
))
744 // No need to constrain SrcReg. It will get constrained when we hit another
745 // of its use or its defs. Copies do not have constraints.
746 if (!RBI
.constrainGenericRegister(DstReg
, *DstRC
, MRI
)) {
747 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII
.getName(I
.getOpcode())
751 I
.setDesc(TII
.get(AArch64::COPY
));
755 static unsigned selectFPConvOpc(unsigned GenericOpc
, LLT DstTy
, LLT SrcTy
) {
756 if (!DstTy
.isScalar() || !SrcTy
.isScalar())
759 const unsigned DstSize
= DstTy
.getSizeInBits();
760 const unsigned SrcSize
= SrcTy
.getSizeInBits();
766 switch (GenericOpc
) {
767 case TargetOpcode::G_SITOFP
:
768 return AArch64::SCVTFUWSri
;
769 case TargetOpcode::G_UITOFP
:
770 return AArch64::UCVTFUWSri
;
771 case TargetOpcode::G_FPTOSI
:
772 return AArch64::FCVTZSUWSr
;
773 case TargetOpcode::G_FPTOUI
:
774 return AArch64::FCVTZUUWSr
;
779 switch (GenericOpc
) {
780 case TargetOpcode::G_SITOFP
:
781 return AArch64::SCVTFUXSri
;
782 case TargetOpcode::G_UITOFP
:
783 return AArch64::UCVTFUXSri
;
784 case TargetOpcode::G_FPTOSI
:
785 return AArch64::FCVTZSUWDr
;
786 case TargetOpcode::G_FPTOUI
:
787 return AArch64::FCVTZUUWDr
;
797 switch (GenericOpc
) {
798 case TargetOpcode::G_SITOFP
:
799 return AArch64::SCVTFUWDri
;
800 case TargetOpcode::G_UITOFP
:
801 return AArch64::UCVTFUWDri
;
802 case TargetOpcode::G_FPTOSI
:
803 return AArch64::FCVTZSUXSr
;
804 case TargetOpcode::G_FPTOUI
:
805 return AArch64::FCVTZUUXSr
;
810 switch (GenericOpc
) {
811 case TargetOpcode::G_SITOFP
:
812 return AArch64::SCVTFUXDri
;
813 case TargetOpcode::G_UITOFP
:
814 return AArch64::UCVTFUXDri
;
815 case TargetOpcode::G_FPTOSI
:
816 return AArch64::FCVTZSUXDr
;
817 case TargetOpcode::G_FPTOUI
:
818 return AArch64::FCVTZUUXDr
;
831 static unsigned selectSelectOpc(MachineInstr
&I
, MachineRegisterInfo
&MRI
,
832 const RegisterBankInfo
&RBI
) {
833 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
834 bool IsFP
= (RBI
.getRegBank(I
.getOperand(0).getReg(), MRI
, TRI
)->getID() !=
835 AArch64::GPRRegBankID
);
836 LLT Ty
= MRI
.getType(I
.getOperand(0).getReg());
837 if (Ty
== LLT::scalar(32))
838 return IsFP
? AArch64::FCSELSrrr
: AArch64::CSELWr
;
839 else if (Ty
== LLT::scalar(64) || Ty
== LLT::pointer(0, 64))
840 return IsFP
? AArch64::FCSELDrrr
: AArch64::CSELXr
;
844 /// Helper function to select the opcode for a G_FCMP.
845 static unsigned selectFCMPOpc(MachineInstr
&I
, MachineRegisterInfo
&MRI
) {
846 // If this is a compare against +0.0, then we don't have to explicitly
847 // materialize a constant.
848 const ConstantFP
*FPImm
= getConstantFPVRegVal(I
.getOperand(3).getReg(), MRI
);
849 bool ShouldUseImm
= FPImm
&& (FPImm
->isZero() && !FPImm
->isNegative());
850 unsigned OpSize
= MRI
.getType(I
.getOperand(2).getReg()).getSizeInBits();
851 if (OpSize
!= 32 && OpSize
!= 64)
853 unsigned CmpOpcTbl
[2][2] = {{AArch64::FCMPSrr
, AArch64::FCMPDrr
},
854 {AArch64::FCMPSri
, AArch64::FCMPDri
}};
855 return CmpOpcTbl
[ShouldUseImm
][OpSize
== 64];
858 /// Returns true if \p P is an unsigned integer comparison predicate.
859 static bool isUnsignedICMPPred(const CmpInst::Predicate P
) {
863 case CmpInst::ICMP_UGT
:
864 case CmpInst::ICMP_UGE
:
865 case CmpInst::ICMP_ULT
:
866 case CmpInst::ICMP_ULE
:
871 static AArch64CC::CondCode
changeICMPPredToAArch64CC(CmpInst::Predicate P
) {
874 llvm_unreachable("Unknown condition code!");
875 case CmpInst::ICMP_NE
:
876 return AArch64CC::NE
;
877 case CmpInst::ICMP_EQ
:
878 return AArch64CC::EQ
;
879 case CmpInst::ICMP_SGT
:
880 return AArch64CC::GT
;
881 case CmpInst::ICMP_SGE
:
882 return AArch64CC::GE
;
883 case CmpInst::ICMP_SLT
:
884 return AArch64CC::LT
;
885 case CmpInst::ICMP_SLE
:
886 return AArch64CC::LE
;
887 case CmpInst::ICMP_UGT
:
888 return AArch64CC::HI
;
889 case CmpInst::ICMP_UGE
:
890 return AArch64CC::HS
;
891 case CmpInst::ICMP_ULT
:
892 return AArch64CC::LO
;
893 case CmpInst::ICMP_ULE
:
894 return AArch64CC::LS
;
898 static void changeFCMPPredToAArch64CC(CmpInst::Predicate P
,
899 AArch64CC::CondCode
&CondCode
,
900 AArch64CC::CondCode
&CondCode2
) {
901 CondCode2
= AArch64CC::AL
;
904 llvm_unreachable("Unknown FP condition!");
905 case CmpInst::FCMP_OEQ
:
906 CondCode
= AArch64CC::EQ
;
908 case CmpInst::FCMP_OGT
:
909 CondCode
= AArch64CC::GT
;
911 case CmpInst::FCMP_OGE
:
912 CondCode
= AArch64CC::GE
;
914 case CmpInst::FCMP_OLT
:
915 CondCode
= AArch64CC::MI
;
917 case CmpInst::FCMP_OLE
:
918 CondCode
= AArch64CC::LS
;
920 case CmpInst::FCMP_ONE
:
921 CondCode
= AArch64CC::MI
;
922 CondCode2
= AArch64CC::GT
;
924 case CmpInst::FCMP_ORD
:
925 CondCode
= AArch64CC::VC
;
927 case CmpInst::FCMP_UNO
:
928 CondCode
= AArch64CC::VS
;
930 case CmpInst::FCMP_UEQ
:
931 CondCode
= AArch64CC::EQ
;
932 CondCode2
= AArch64CC::VS
;
934 case CmpInst::FCMP_UGT
:
935 CondCode
= AArch64CC::HI
;
937 case CmpInst::FCMP_UGE
:
938 CondCode
= AArch64CC::PL
;
940 case CmpInst::FCMP_ULT
:
941 CondCode
= AArch64CC::LT
;
943 case CmpInst::FCMP_ULE
:
944 CondCode
= AArch64CC::LE
;
946 case CmpInst::FCMP_UNE
:
947 CondCode
= AArch64CC::NE
;
952 bool AArch64InstructionSelector::selectCompareBranch(
953 MachineInstr
&I
, MachineFunction
&MF
, MachineRegisterInfo
&MRI
) const {
955 const Register CondReg
= I
.getOperand(0).getReg();
956 MachineBasicBlock
*DestMBB
= I
.getOperand(1).getMBB();
957 MachineInstr
*CCMI
= MRI
.getVRegDef(CondReg
);
958 if (CCMI
->getOpcode() == TargetOpcode::G_TRUNC
)
959 CCMI
= MRI
.getVRegDef(CCMI
->getOperand(1).getReg());
960 if (CCMI
->getOpcode() != TargetOpcode::G_ICMP
)
963 Register LHS
= CCMI
->getOperand(2).getReg();
964 Register RHS
= CCMI
->getOperand(3).getReg();
965 auto VRegAndVal
= getConstantVRegValWithLookThrough(RHS
, MRI
);
969 VRegAndVal
= getConstantVRegValWithLookThrough(RHS
, MRI
);
970 if (!VRegAndVal
|| VRegAndVal
->Value
!= 0) {
971 MachineIRBuilder
MIB(I
);
972 // If we can't select a CBZ then emit a cmp + Bcc.
973 if (!emitIntegerCompare(CCMI
->getOperand(2), CCMI
->getOperand(3),
974 CCMI
->getOperand(1), MIB
))
976 const AArch64CC::CondCode CC
= changeICMPPredToAArch64CC(
977 (CmpInst::Predicate
)CCMI
->getOperand(1).getPredicate());
978 MIB
.buildInstr(AArch64::Bcc
, {}, {}).addImm(CC
).addMBB(DestMBB
);
983 const RegisterBank
&RB
= *RBI
.getRegBank(LHS
, MRI
, TRI
);
984 if (RB
.getID() != AArch64::GPRRegBankID
)
987 const auto Pred
= (CmpInst::Predicate
)CCMI
->getOperand(1).getPredicate();
988 if (Pred
!= CmpInst::ICMP_NE
&& Pred
!= CmpInst::ICMP_EQ
)
991 const unsigned CmpWidth
= MRI
.getType(LHS
).getSizeInBits();
994 CBOpc
= (Pred
== CmpInst::ICMP_EQ
? AArch64::CBZW
: AArch64::CBNZW
);
995 else if (CmpWidth
== 64)
996 CBOpc
= (Pred
== CmpInst::ICMP_EQ
? AArch64::CBZX
: AArch64::CBNZX
);
1000 BuildMI(*I
.getParent(), I
, I
.getDebugLoc(), TII
.get(CBOpc
))
1003 .constrainAllUses(TII
, TRI
, RBI
);
1005 I
.eraseFromParent();
1009 bool AArch64InstructionSelector::selectVectorSHL(
1010 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
1011 assert(I
.getOpcode() == TargetOpcode::G_SHL
);
1012 Register DstReg
= I
.getOperand(0).getReg();
1013 const LLT Ty
= MRI
.getType(DstReg
);
1014 Register Src1Reg
= I
.getOperand(1).getReg();
1015 Register Src2Reg
= I
.getOperand(2).getReg();
1021 if (Ty
== LLT::vector(4, 32)) {
1022 Opc
= AArch64::USHLv4i32
;
1023 } else if (Ty
== LLT::vector(2, 32)) {
1024 Opc
= AArch64::USHLv2i32
;
1026 LLVM_DEBUG(dbgs() << "Unhandled G_SHL type");
1030 MachineIRBuilder
MIB(I
);
1031 auto UShl
= MIB
.buildInstr(Opc
, {DstReg
}, {Src1Reg
, Src2Reg
});
1032 constrainSelectedInstRegOperands(*UShl
, TII
, TRI
, RBI
);
1033 I
.eraseFromParent();
1037 bool AArch64InstructionSelector::selectVectorASHR(
1038 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
1039 assert(I
.getOpcode() == TargetOpcode::G_ASHR
);
1040 Register DstReg
= I
.getOperand(0).getReg();
1041 const LLT Ty
= MRI
.getType(DstReg
);
1042 Register Src1Reg
= I
.getOperand(1).getReg();
1043 Register Src2Reg
= I
.getOperand(2).getReg();
1048 // There is not a shift right register instruction, but the shift left
1049 // register instruction takes a signed value, where negative numbers specify a
1053 unsigned NegOpc
= 0;
1054 const TargetRegisterClass
*RC
= nullptr;
1055 if (Ty
== LLT::vector(4, 32)) {
1056 Opc
= AArch64::SSHLv4i32
;
1057 NegOpc
= AArch64::NEGv4i32
;
1058 RC
= &AArch64::FPR128RegClass
;
1059 } else if (Ty
== LLT::vector(2, 32)) {
1060 Opc
= AArch64::SSHLv2i32
;
1061 NegOpc
= AArch64::NEGv2i32
;
1062 RC
= &AArch64::FPR64RegClass
;
1064 LLVM_DEBUG(dbgs() << "Unhandled G_ASHR type");
1068 MachineIRBuilder
MIB(I
);
1069 auto Neg
= MIB
.buildInstr(NegOpc
, {RC
}, {Src2Reg
});
1070 constrainSelectedInstRegOperands(*Neg
, TII
, TRI
, RBI
);
1071 auto SShl
= MIB
.buildInstr(Opc
, {DstReg
}, {Src1Reg
, Neg
});
1072 constrainSelectedInstRegOperands(*SShl
, TII
, TRI
, RBI
);
1073 I
.eraseFromParent();
1077 bool AArch64InstructionSelector::selectVaStartAAPCS(
1078 MachineInstr
&I
, MachineFunction
&MF
, MachineRegisterInfo
&MRI
) const {
1082 bool AArch64InstructionSelector::selectVaStartDarwin(
1083 MachineInstr
&I
, MachineFunction
&MF
, MachineRegisterInfo
&MRI
) const {
1084 AArch64FunctionInfo
*FuncInfo
= MF
.getInfo
<AArch64FunctionInfo
>();
1085 Register ListReg
= I
.getOperand(0).getReg();
1087 Register ArgsAddrReg
= MRI
.createVirtualRegister(&AArch64::GPR64RegClass
);
1090 BuildMI(*I
.getParent(), I
, I
.getDebugLoc(), TII
.get(AArch64::ADDXri
))
1091 .addDef(ArgsAddrReg
)
1092 .addFrameIndex(FuncInfo
->getVarArgsStackIndex())
1096 constrainSelectedInstRegOperands(*MIB
, TII
, TRI
, RBI
);
1098 MIB
= BuildMI(*I
.getParent(), I
, I
.getDebugLoc(), TII
.get(AArch64::STRXui
))
1099 .addUse(ArgsAddrReg
)
1102 .addMemOperand(*I
.memoperands_begin());
1104 constrainSelectedInstRegOperands(*MIB
, TII
, TRI
, RBI
);
1105 I
.eraseFromParent();
1109 void AArch64InstructionSelector::materializeLargeCMVal(
1110 MachineInstr
&I
, const Value
*V
, unsigned OpFlags
) const {
1111 MachineBasicBlock
&MBB
= *I
.getParent();
1112 MachineFunction
&MF
= *MBB
.getParent();
1113 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1114 MachineIRBuilder
MIB(I
);
1116 auto MovZ
= MIB
.buildInstr(AArch64::MOVZXi
, {&AArch64::GPR64RegClass
}, {});
1117 MovZ
->addOperand(MF
, I
.getOperand(1));
1118 MovZ
->getOperand(1).setTargetFlags(OpFlags
| AArch64II::MO_G0
|
1120 MovZ
->addOperand(MF
, MachineOperand::CreateImm(0));
1121 constrainSelectedInstRegOperands(*MovZ
, TII
, TRI
, RBI
);
1123 auto BuildMovK
= [&](Register SrcReg
, unsigned char Flags
, unsigned Offset
,
1124 Register ForceDstReg
) {
1125 Register DstReg
= ForceDstReg
1127 : MRI
.createVirtualRegister(&AArch64::GPR64RegClass
);
1128 auto MovI
= MIB
.buildInstr(AArch64::MOVKXi
).addDef(DstReg
).addUse(SrcReg
);
1129 if (auto *GV
= dyn_cast
<GlobalValue
>(V
)) {
1130 MovI
->addOperand(MF
, MachineOperand::CreateGA(
1131 GV
, MovZ
->getOperand(1).getOffset(), Flags
));
1134 MF
, MachineOperand::CreateBA(cast
<BlockAddress
>(V
),
1135 MovZ
->getOperand(1).getOffset(), Flags
));
1137 MovI
->addOperand(MF
, MachineOperand::CreateImm(Offset
));
1138 constrainSelectedInstRegOperands(*MovI
, TII
, TRI
, RBI
);
1141 Register DstReg
= BuildMovK(MovZ
.getReg(0),
1142 AArch64II::MO_G1
| AArch64II::MO_NC
, 16, 0);
1143 DstReg
= BuildMovK(DstReg
, AArch64II::MO_G2
| AArch64II::MO_NC
, 32, 0);
1144 BuildMovK(DstReg
, AArch64II::MO_G3
, 48, I
.getOperand(0).getReg());
1148 void AArch64InstructionSelector::preISelLower(MachineInstr
&I
) const {
1149 MachineBasicBlock
&MBB
= *I
.getParent();
1150 MachineFunction
&MF
= *MBB
.getParent();
1151 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1153 switch (I
.getOpcode()) {
1154 case TargetOpcode::G_SHL
:
1155 case TargetOpcode::G_ASHR
:
1156 case TargetOpcode::G_LSHR
: {
1157 // These shifts are legalized to have 64 bit shift amounts because we want
1158 // to take advantage of the existing imported selection patterns that assume
1159 // the immediates are s64s. However, if the shifted type is 32 bits and for
1160 // some reason we receive input GMIR that has an s64 shift amount that's not
1161 // a G_CONSTANT, insert a truncate so that we can still select the s32
1162 // register-register variant.
1163 Register SrcReg
= I
.getOperand(1).getReg();
1164 Register ShiftReg
= I
.getOperand(2).getReg();
1165 const LLT ShiftTy
= MRI
.getType(ShiftReg
);
1166 const LLT SrcTy
= MRI
.getType(SrcReg
);
1167 if (SrcTy
.isVector())
1169 assert(!ShiftTy
.isVector() && "unexpected vector shift ty");
1170 if (SrcTy
.getSizeInBits() != 32 || ShiftTy
.getSizeInBits() != 64)
1172 auto *AmtMI
= MRI
.getVRegDef(ShiftReg
);
1173 assert(AmtMI
&& "could not find a vreg definition for shift amount");
1174 if (AmtMI
->getOpcode() != TargetOpcode::G_CONSTANT
) {
1175 // Insert a subregister copy to implement a 64->32 trunc
1176 MachineIRBuilder
MIB(I
);
1177 auto Trunc
= MIB
.buildInstr(TargetOpcode::COPY
, {SrcTy
}, {})
1178 .addReg(ShiftReg
, 0, AArch64::sub_32
);
1179 MRI
.setRegBank(Trunc
.getReg(0), RBI
.getRegBank(AArch64::GPRRegBankID
));
1180 I
.getOperand(2).setReg(Trunc
.getReg(0));
1184 case TargetOpcode::G_STORE
:
1185 contractCrossBankCopyIntoStore(I
, MRI
);
1192 bool AArch64InstructionSelector::earlySelectSHL(
1193 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
1194 // We try to match the immediate variant of LSL, which is actually an alias
1195 // for a special case of UBFM. Otherwise, we fall back to the imported
1196 // selector which will match the register variant.
1197 assert(I
.getOpcode() == TargetOpcode::G_SHL
&& "unexpected op");
1198 const auto &MO
= I
.getOperand(2);
1199 auto VRegAndVal
= getConstantVRegVal(MO
.getReg(), MRI
);
1203 const LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg());
1204 if (DstTy
.isVector())
1206 bool Is64Bit
= DstTy
.getSizeInBits() == 64;
1207 auto Imm1Fn
= Is64Bit
? selectShiftA_64(MO
) : selectShiftA_32(MO
);
1208 auto Imm2Fn
= Is64Bit
? selectShiftB_64(MO
) : selectShiftB_32(MO
);
1209 MachineIRBuilder
MIB(I
);
1211 if (!Imm1Fn
|| !Imm2Fn
)
1215 MIB
.buildInstr(Is64Bit
? AArch64::UBFMXri
: AArch64::UBFMWri
,
1216 {I
.getOperand(0).getReg()}, {I
.getOperand(1).getReg()});
1218 for (auto &RenderFn
: *Imm1Fn
)
1220 for (auto &RenderFn
: *Imm2Fn
)
1223 I
.eraseFromParent();
1224 return constrainSelectedInstRegOperands(*NewI
, TII
, TRI
, RBI
);
1227 void AArch64InstructionSelector::contractCrossBankCopyIntoStore(
1228 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
1229 assert(I
.getOpcode() == TargetOpcode::G_STORE
&& "Expected G_STORE");
1230 // If we're storing a scalar, it doesn't matter what register bank that
1231 // scalar is on. All that matters is the size.
1233 // So, if we see something like this (with a 32-bit scalar as an example):
1235 // %x:gpr(s32) = ... something ...
1236 // %y:fpr(s32) = COPY %x:gpr(s32)
1237 // G_STORE %y:fpr(s32)
1239 // We can fix this up into something like this:
1241 // G_STORE %x:gpr(s32)
1243 // And then continue the selection process normally.
1244 MachineInstr
*Def
= getDefIgnoringCopies(I
.getOperand(0).getReg(), MRI
);
1247 Register DefDstReg
= Def
->getOperand(0).getReg();
1248 LLT DefDstTy
= MRI
.getType(DefDstReg
);
1249 Register StoreSrcReg
= I
.getOperand(0).getReg();
1250 LLT StoreSrcTy
= MRI
.getType(StoreSrcReg
);
1252 // If we get something strange like a physical register, then we shouldn't
1254 if (!DefDstTy
.isValid())
1257 // Are the source and dst types the same size?
1258 if (DefDstTy
.getSizeInBits() != StoreSrcTy
.getSizeInBits())
1261 if (RBI
.getRegBank(StoreSrcReg
, MRI
, TRI
) ==
1262 RBI
.getRegBank(DefDstReg
, MRI
, TRI
))
1265 // We have a cross-bank copy, which is entering a store. Let's fold it.
1266 I
.getOperand(0).setReg(DefDstReg
);
1269 bool AArch64InstructionSelector::earlySelect(MachineInstr
&I
) const {
1270 assert(I
.getParent() && "Instruction should be in a basic block!");
1271 assert(I
.getParent()->getParent() && "Instruction should be in a function!");
1273 MachineBasicBlock
&MBB
= *I
.getParent();
1274 MachineFunction
&MF
= *MBB
.getParent();
1275 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1277 switch (I
.getOpcode()) {
1278 case TargetOpcode::G_SHL
:
1279 return earlySelectSHL(I
, MRI
);
1280 case TargetOpcode::G_CONSTANT
: {
1281 bool IsZero
= false;
1282 if (I
.getOperand(1).isCImm())
1283 IsZero
= I
.getOperand(1).getCImm()->getZExtValue() == 0;
1284 else if (I
.getOperand(1).isImm())
1285 IsZero
= I
.getOperand(1).getImm() == 0;
1290 Register DefReg
= I
.getOperand(0).getReg();
1291 LLT Ty
= MRI
.getType(DefReg
);
1292 if (Ty
!= LLT::scalar(64) && Ty
!= LLT::scalar(32))
1295 if (Ty
== LLT::scalar(64)) {
1296 I
.getOperand(1).ChangeToRegister(AArch64::XZR
, false);
1297 RBI
.constrainGenericRegister(DefReg
, AArch64::GPR64RegClass
, MRI
);
1299 I
.getOperand(1).ChangeToRegister(AArch64::WZR
, false);
1300 RBI
.constrainGenericRegister(DefReg
, AArch64::GPR32RegClass
, MRI
);
1302 I
.setDesc(TII
.get(TargetOpcode::COPY
));
1310 bool AArch64InstructionSelector::select(MachineInstr
&I
) {
1311 assert(I
.getParent() && "Instruction should be in a basic block!");
1312 assert(I
.getParent()->getParent() && "Instruction should be in a function!");
1314 MachineBasicBlock
&MBB
= *I
.getParent();
1315 MachineFunction
&MF
= *MBB
.getParent();
1316 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1318 unsigned Opcode
= I
.getOpcode();
1319 // G_PHI requires same handling as PHI
1320 if (!isPreISelGenericOpcode(Opcode
) || Opcode
== TargetOpcode::G_PHI
) {
1321 // Certain non-generic instructions also need some special handling.
1323 if (Opcode
== TargetOpcode::LOAD_STACK_GUARD
)
1324 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1326 if (Opcode
== TargetOpcode::PHI
|| Opcode
== TargetOpcode::G_PHI
) {
1327 const Register DefReg
= I
.getOperand(0).getReg();
1328 const LLT DefTy
= MRI
.getType(DefReg
);
1330 const RegClassOrRegBank
&RegClassOrBank
=
1331 MRI
.getRegClassOrRegBank(DefReg
);
1333 const TargetRegisterClass
*DefRC
1334 = RegClassOrBank
.dyn_cast
<const TargetRegisterClass
*>();
1336 if (!DefTy
.isValid()) {
1337 LLVM_DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
1340 const RegisterBank
&RB
= *RegClassOrBank
.get
<const RegisterBank
*>();
1341 DefRC
= getRegClassForTypeOnBank(DefTy
, RB
, RBI
);
1343 LLVM_DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
1348 I
.setDesc(TII
.get(TargetOpcode::PHI
));
1350 return RBI
.constrainGenericRegister(DefReg
, *DefRC
, MRI
);
1354 return selectCopy(I
, TII
, MRI
, TRI
, RBI
);
1360 if (I
.getNumOperands() != I
.getNumExplicitOperands()) {
1362 dbgs() << "Generic instruction has unexpected implicit operands\n");
1366 // Try to do some lowering before we start instruction selecting. These
1367 // lowerings are purely transformations on the input G_MIR and so selection
1368 // must continue after any modification of the instruction.
1371 // There may be patterns where the importer can't deal with them optimally,
1372 // but does select it to a suboptimal sequence so our custom C++ selection
1373 // code later never has a chance to work on it. Therefore, we have an early
1374 // selection attempt here to give priority to certain selection routines
1375 // over the imported ones.
1379 if (selectImpl(I
, *CoverageInfo
))
1383 I
.getOperand(0).isReg() ? MRI
.getType(I
.getOperand(0).getReg()) : LLT
{};
1385 MachineIRBuilder
MIB(I
);
1388 case TargetOpcode::G_BRCOND
: {
1389 if (Ty
.getSizeInBits() > 32) {
1390 // We shouldn't need this on AArch64, but it would be implemented as an
1391 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
1392 // bit being tested is < 32.
1393 LLVM_DEBUG(dbgs() << "G_BRCOND has type: " << Ty
1394 << ", expected at most 32-bits");
1398 const Register CondReg
= I
.getOperand(0).getReg();
1399 MachineBasicBlock
*DestMBB
= I
.getOperand(1).getMBB();
1401 // Speculation tracking/SLH assumes that optimized TB(N)Z/CB(N)Z
1402 // instructions will not be produced, as they are conditional branch
1403 // instructions that do not set flags.
1404 bool ProduceNonFlagSettingCondBr
=
1405 !MF
.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening
);
1406 if (ProduceNonFlagSettingCondBr
&& selectCompareBranch(I
, MF
, MRI
))
1409 if (ProduceNonFlagSettingCondBr
) {
1410 auto MIB
= BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::TBNZW
))
1412 .addImm(/*bit offset=*/0)
1415 I
.eraseFromParent();
1416 return constrainSelectedInstRegOperands(*MIB
.getInstr(), TII
, TRI
, RBI
);
1418 auto CMP
= BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::ANDSWri
))
1419 .addDef(AArch64::WZR
)
1422 constrainSelectedInstRegOperands(*CMP
.getInstr(), TII
, TRI
, RBI
);
1424 BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::Bcc
))
1425 .addImm(AArch64CC::EQ
)
1428 I
.eraseFromParent();
1429 return constrainSelectedInstRegOperands(*Bcc
.getInstr(), TII
, TRI
, RBI
);
1433 case TargetOpcode::G_BRINDIRECT
: {
1434 I
.setDesc(TII
.get(AArch64::BR
));
1435 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1438 case TargetOpcode::G_BRJT
:
1439 return selectBrJT(I
, MRI
);
1441 case TargetOpcode::G_BSWAP
: {
1442 // Handle vector types for G_BSWAP directly.
1443 Register DstReg
= I
.getOperand(0).getReg();
1444 LLT DstTy
= MRI
.getType(DstReg
);
1446 // We should only get vector types here; everything else is handled by the
1447 // importer right now.
1448 if (!DstTy
.isVector() || DstTy
.getSizeInBits() > 128) {
1449 LLVM_DEBUG(dbgs() << "Dst type for G_BSWAP currently unsupported.\n");
1453 // Only handle 4 and 2 element vectors for now.
1454 // TODO: 16-bit elements.
1455 unsigned NumElts
= DstTy
.getNumElements();
1456 if (NumElts
!= 4 && NumElts
!= 2) {
1457 LLVM_DEBUG(dbgs() << "Unsupported number of elements for G_BSWAP.\n");
1461 // Choose the correct opcode for the supported types. Right now, that's
1462 // v2s32, v4s32, and v2s64.
1464 unsigned EltSize
= DstTy
.getElementType().getSizeInBits();
1466 Opc
= (DstTy
.getNumElements() == 2) ? AArch64::REV32v8i8
1467 : AArch64::REV32v16i8
;
1468 else if (EltSize
== 64)
1469 Opc
= AArch64::REV64v16i8
;
1471 // We should always get something by the time we get here...
1472 assert(Opc
!= 0 && "Didn't get an opcode for G_BSWAP?");
1474 I
.setDesc(TII
.get(Opc
));
1475 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1478 case TargetOpcode::G_FCONSTANT
:
1479 case TargetOpcode::G_CONSTANT
: {
1480 const bool isFP
= Opcode
== TargetOpcode::G_FCONSTANT
;
1482 const LLT s8
= LLT::scalar(8);
1483 const LLT s16
= LLT::scalar(16);
1484 const LLT s32
= LLT::scalar(32);
1485 const LLT s64
= LLT::scalar(64);
1486 const LLT p0
= LLT::pointer(0, 64);
1488 const Register DefReg
= I
.getOperand(0).getReg();
1489 const LLT DefTy
= MRI
.getType(DefReg
);
1490 const unsigned DefSize
= DefTy
.getSizeInBits();
1491 const RegisterBank
&RB
= *RBI
.getRegBank(DefReg
, MRI
, TRI
);
1493 // FIXME: Redundant check, but even less readable when factored out.
1495 if (Ty
!= s32
&& Ty
!= s64
) {
1496 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
1497 << " constant, expected: " << s32
<< " or " << s64
1502 if (RB
.getID() != AArch64::FPRRegBankID
) {
1503 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
1504 << " constant on bank: " << RB
1505 << ", expected: FPR\n");
1509 // The case when we have 0.0 is covered by tablegen. Reject it here so we
1510 // can be sure tablegen works correctly and isn't rescued by this code.
1511 if (I
.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
1514 // s32 and s64 are covered by tablegen.
1515 if (Ty
!= p0
&& Ty
!= s8
&& Ty
!= s16
) {
1516 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
1517 << " constant, expected: " << s32
<< ", " << s64
1518 << ", or " << p0
<< '\n');
1522 if (RB
.getID() != AArch64::GPRRegBankID
) {
1523 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
1524 << " constant on bank: " << RB
1525 << ", expected: GPR\n");
1530 // We allow G_CONSTANT of types < 32b.
1531 const unsigned MovOpc
=
1532 DefSize
== 64 ? AArch64::MOVi64imm
: AArch64::MOVi32imm
;
1535 // Either emit a FMOV, or emit a copy to emit a normal mov.
1536 const TargetRegisterClass
&GPRRC
=
1537 DefSize
== 32 ? AArch64::GPR32RegClass
: AArch64::GPR64RegClass
;
1538 const TargetRegisterClass
&FPRRC
=
1539 DefSize
== 32 ? AArch64::FPR32RegClass
: AArch64::FPR64RegClass
;
1541 // Can we use a FMOV instruction to represent the immediate?
1542 if (emitFMovForFConstant(I
, MRI
))
1545 // Nope. Emit a copy and use a normal mov instead.
1546 const Register DefGPRReg
= MRI
.createVirtualRegister(&GPRRC
);
1547 MachineOperand
&RegOp
= I
.getOperand(0);
1548 RegOp
.setReg(DefGPRReg
);
1549 MIB
.setInsertPt(MIB
.getMBB(), std::next(I
.getIterator()));
1550 MIB
.buildCopy({DefReg
}, {DefGPRReg
});
1552 if (!RBI
.constrainGenericRegister(DefReg
, FPRRC
, MRI
)) {
1553 LLVM_DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
1557 MachineOperand
&ImmOp
= I
.getOperand(1);
1558 // FIXME: Is going through int64_t always correct?
1559 ImmOp
.ChangeToImmediate(
1560 ImmOp
.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
1561 } else if (I
.getOperand(1).isCImm()) {
1562 uint64_t Val
= I
.getOperand(1).getCImm()->getZExtValue();
1563 I
.getOperand(1).ChangeToImmediate(Val
);
1564 } else if (I
.getOperand(1).isImm()) {
1565 uint64_t Val
= I
.getOperand(1).getImm();
1566 I
.getOperand(1).ChangeToImmediate(Val
);
1569 I
.setDesc(TII
.get(MovOpc
));
1570 constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1573 case TargetOpcode::G_EXTRACT
: {
1574 Register DstReg
= I
.getOperand(0).getReg();
1575 Register SrcReg
= I
.getOperand(1).getReg();
1576 LLT SrcTy
= MRI
.getType(SrcReg
);
1577 LLT DstTy
= MRI
.getType(DstReg
);
1579 unsigned SrcSize
= SrcTy
.getSizeInBits();
1581 if (SrcTy
.getSizeInBits() > 64) {
1582 // This should be an extract of an s128, which is like a vector extract.
1583 if (SrcTy
.getSizeInBits() != 128)
1585 // Only support extracting 64 bits from an s128 at the moment.
1586 if (DstTy
.getSizeInBits() != 64)
1589 const RegisterBank
&SrcRB
= *RBI
.getRegBank(SrcReg
, MRI
, TRI
);
1590 const RegisterBank
&DstRB
= *RBI
.getRegBank(DstReg
, MRI
, TRI
);
1591 // Check we have the right regbank always.
1592 assert(SrcRB
.getID() == AArch64::FPRRegBankID
&&
1593 DstRB
.getID() == AArch64::FPRRegBankID
&&
1594 "Wrong extract regbank!");
1597 // Emit the same code as a vector extract.
1598 // Offset must be a multiple of 64.
1599 unsigned Offset
= I
.getOperand(2).getImm();
1600 if (Offset
% 64 != 0)
1602 unsigned LaneIdx
= Offset
/ 64;
1603 MachineIRBuilder
MIB(I
);
1604 MachineInstr
*Extract
= emitExtractVectorElt(
1605 DstReg
, DstRB
, LLT::scalar(64), SrcReg
, LaneIdx
, MIB
);
1608 I
.eraseFromParent();
1612 I
.setDesc(TII
.get(SrcSize
== 64 ? AArch64::UBFMXri
: AArch64::UBFMWri
));
1613 MachineInstrBuilder(MF
, I
).addImm(I
.getOperand(2).getImm() +
1614 Ty
.getSizeInBits() - 1);
1617 assert(SrcSize
== 32 && DstTy
.getSizeInBits() == 16 &&
1618 "unexpected G_EXTRACT types");
1619 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1622 DstReg
= MRI
.createGenericVirtualRegister(LLT::scalar(64));
1623 MIB
.setInsertPt(MIB
.getMBB(), std::next(I
.getIterator()));
1624 MIB
.buildInstr(TargetOpcode::COPY
, {I
.getOperand(0).getReg()}, {})
1625 .addReg(DstReg
, 0, AArch64::sub_32
);
1626 RBI
.constrainGenericRegister(I
.getOperand(0).getReg(),
1627 AArch64::GPR32RegClass
, MRI
);
1628 I
.getOperand(0).setReg(DstReg
);
1630 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1633 case TargetOpcode::G_INSERT
: {
1634 LLT SrcTy
= MRI
.getType(I
.getOperand(2).getReg());
1635 LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg());
1636 unsigned DstSize
= DstTy
.getSizeInBits();
1637 // Larger inserts are vectors, same-size ones should be something else by
1638 // now (split up or turned into COPYs).
1639 if (Ty
.getSizeInBits() > 64 || SrcTy
.getSizeInBits() > 32)
1642 I
.setDesc(TII
.get(DstSize
== 64 ? AArch64::BFMXri
: AArch64::BFMWri
));
1643 unsigned LSB
= I
.getOperand(3).getImm();
1644 unsigned Width
= MRI
.getType(I
.getOperand(2).getReg()).getSizeInBits();
1645 I
.getOperand(3).setImm((DstSize
- LSB
) % DstSize
);
1646 MachineInstrBuilder(MF
, I
).addImm(Width
- 1);
1649 assert(DstSize
== 32 && SrcTy
.getSizeInBits() == 16 &&
1650 "unexpected G_INSERT types");
1651 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1654 Register SrcReg
= MRI
.createGenericVirtualRegister(LLT::scalar(64));
1655 BuildMI(MBB
, I
.getIterator(), I
.getDebugLoc(),
1656 TII
.get(AArch64::SUBREG_TO_REG
))
1659 .addUse(I
.getOperand(2).getReg())
1660 .addImm(AArch64::sub_32
);
1661 RBI
.constrainGenericRegister(I
.getOperand(2).getReg(),
1662 AArch64::GPR32RegClass
, MRI
);
1663 I
.getOperand(2).setReg(SrcReg
);
1665 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1667 case TargetOpcode::G_FRAME_INDEX
: {
1668 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
1669 if (Ty
!= LLT::pointer(0, 64)) {
1670 LLVM_DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
1671 << ", expected: " << LLT::pointer(0, 64) << '\n');
1674 I
.setDesc(TII
.get(AArch64::ADDXri
));
1676 // MOs for a #0 shifted immediate.
1677 I
.addOperand(MachineOperand::CreateImm(0));
1678 I
.addOperand(MachineOperand::CreateImm(0));
1680 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1683 case TargetOpcode::G_GLOBAL_VALUE
: {
1684 auto GV
= I
.getOperand(1).getGlobal();
1685 if (GV
->isThreadLocal())
1686 return selectTLSGlobalValue(I
, MRI
);
1688 unsigned OpFlags
= STI
.ClassifyGlobalReference(GV
, TM
);
1689 if (OpFlags
& AArch64II::MO_GOT
) {
1690 I
.setDesc(TII
.get(AArch64::LOADgot
));
1691 I
.getOperand(1).setTargetFlags(OpFlags
);
1692 } else if (TM
.getCodeModel() == CodeModel::Large
) {
1693 // Materialize the global using movz/movk instructions.
1694 materializeLargeCMVal(I
, GV
, OpFlags
);
1695 I
.eraseFromParent();
1697 } else if (TM
.getCodeModel() == CodeModel::Tiny
) {
1698 I
.setDesc(TII
.get(AArch64::ADR
));
1699 I
.getOperand(1).setTargetFlags(OpFlags
);
1701 I
.setDesc(TII
.get(AArch64::MOVaddr
));
1702 I
.getOperand(1).setTargetFlags(OpFlags
| AArch64II::MO_PAGE
);
1703 MachineInstrBuilder
MIB(MF
, I
);
1704 MIB
.addGlobalAddress(GV
, I
.getOperand(1).getOffset(),
1705 OpFlags
| AArch64II::MO_PAGEOFF
| AArch64II::MO_NC
);
1707 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1710 case TargetOpcode::G_ZEXTLOAD
:
1711 case TargetOpcode::G_LOAD
:
1712 case TargetOpcode::G_STORE
: {
1713 bool IsZExtLoad
= I
.getOpcode() == TargetOpcode::G_ZEXTLOAD
;
1714 MachineIRBuilder
MIB(I
);
1716 LLT PtrTy
= MRI
.getType(I
.getOperand(1).getReg());
1718 if (PtrTy
!= LLT::pointer(0, 64)) {
1719 LLVM_DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
1720 << ", expected: " << LLT::pointer(0, 64) << '\n');
1724 auto &MemOp
= **I
.memoperands_begin();
1725 if (MemOp
.isAtomic()) {
1726 // For now we just support s8 acquire loads to be able to compile stack
1728 if (MemOp
.getOrdering() == AtomicOrdering::Acquire
&&
1729 MemOp
.getSize() == 1) {
1730 I
.setDesc(TII
.get(AArch64::LDARB
));
1731 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1733 LLVM_DEBUG(dbgs() << "Atomic load/store not fully supported yet\n");
1736 unsigned MemSizeInBits
= MemOp
.getSize() * 8;
1738 const Register PtrReg
= I
.getOperand(1).getReg();
1740 const RegisterBank
&PtrRB
= *RBI
.getRegBank(PtrReg
, MRI
, TRI
);
1741 // Sanity-check the pointer register.
1742 assert(PtrRB
.getID() == AArch64::GPRRegBankID
&&
1743 "Load/Store pointer operand isn't a GPR");
1744 assert(MRI
.getType(PtrReg
).isPointer() &&
1745 "Load/Store pointer operand isn't a pointer");
1748 const Register ValReg
= I
.getOperand(0).getReg();
1749 const RegisterBank
&RB
= *RBI
.getRegBank(ValReg
, MRI
, TRI
);
1751 const unsigned NewOpc
=
1752 selectLoadStoreUIOp(I
.getOpcode(), RB
.getID(), MemSizeInBits
);
1753 if (NewOpc
== I
.getOpcode())
1756 I
.setDesc(TII
.get(NewOpc
));
1758 uint64_t Offset
= 0;
1759 auto *PtrMI
= MRI
.getVRegDef(PtrReg
);
1761 // Try to fold a GEP into our unsigned immediate addressing mode.
1762 if (PtrMI
->getOpcode() == TargetOpcode::G_GEP
) {
1763 if (auto COff
= getConstantVRegVal(PtrMI
->getOperand(2).getReg(), MRI
)) {
1764 int64_t Imm
= *COff
;
1765 const unsigned Size
= MemSizeInBits
/ 8;
1766 const unsigned Scale
= Log2_32(Size
);
1767 if ((Imm
& (Size
- 1)) == 0 && Imm
>= 0 && Imm
< (0x1000 << Scale
)) {
1768 Register Ptr2Reg
= PtrMI
->getOperand(1).getReg();
1769 I
.getOperand(1).setReg(Ptr2Reg
);
1770 PtrMI
= MRI
.getVRegDef(Ptr2Reg
);
1771 Offset
= Imm
/ Size
;
1776 // If we haven't folded anything into our addressing mode yet, try to fold
1777 // a frame index into the base+offset.
1778 if (!Offset
&& PtrMI
->getOpcode() == TargetOpcode::G_FRAME_INDEX
)
1779 I
.getOperand(1).ChangeToFrameIndex(PtrMI
->getOperand(1).getIndex());
1781 I
.addOperand(MachineOperand::CreateImm(Offset
));
1783 // If we're storing a 0, use WZR/XZR.
1784 if (auto CVal
= getConstantVRegVal(ValReg
, MRI
)) {
1785 if (*CVal
== 0 && Opcode
== TargetOpcode::G_STORE
) {
1786 if (I
.getOpcode() == AArch64::STRWui
)
1787 I
.getOperand(0).setReg(AArch64::WZR
);
1788 else if (I
.getOpcode() == AArch64::STRXui
)
1789 I
.getOperand(0).setReg(AArch64::XZR
);
1794 // The zextload from a smaller type to i32 should be handled by the importer.
1795 if (MRI
.getType(ValReg
).getSizeInBits() != 64)
1797 // If we have a ZEXTLOAD then change the load's type to be a narrower reg
1798 //and zero_extend with SUBREG_TO_REG.
1799 Register LdReg
= MRI
.createVirtualRegister(&AArch64::GPR32RegClass
);
1800 Register DstReg
= I
.getOperand(0).getReg();
1801 I
.getOperand(0).setReg(LdReg
);
1803 MIB
.setInsertPt(MIB
.getMBB(), std::next(I
.getIterator()));
1804 MIB
.buildInstr(AArch64::SUBREG_TO_REG
, {DstReg
}, {})
1807 .addImm(AArch64::sub_32
);
1808 constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1809 return RBI
.constrainGenericRegister(DstReg
, AArch64::GPR64allRegClass
,
1812 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1815 case TargetOpcode::G_SMULH
:
1816 case TargetOpcode::G_UMULH
: {
1817 // Reject the various things we don't support yet.
1818 if (unsupportedBinOp(I
, RBI
, MRI
, TRI
))
1821 const Register DefReg
= I
.getOperand(0).getReg();
1822 const RegisterBank
&RB
= *RBI
.getRegBank(DefReg
, MRI
, TRI
);
1824 if (RB
.getID() != AArch64::GPRRegBankID
) {
1825 LLVM_DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB
<< ", expected: GPR\n");
1829 if (Ty
!= LLT::scalar(64)) {
1830 LLVM_DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1831 << ", expected: " << LLT::scalar(64) << '\n');
1835 unsigned NewOpc
= I
.getOpcode() == TargetOpcode::G_SMULH
? AArch64::SMULHrr
1837 I
.setDesc(TII
.get(NewOpc
));
1839 // Now that we selected an opcode, we need to constrain the register
1840 // operands to use appropriate classes.
1841 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1843 case TargetOpcode::G_FADD
:
1844 case TargetOpcode::G_FSUB
:
1845 case TargetOpcode::G_FMUL
:
1846 case TargetOpcode::G_FDIV
:
1848 case TargetOpcode::G_ASHR
:
1849 if (MRI
.getType(I
.getOperand(0).getReg()).isVector())
1850 return selectVectorASHR(I
, MRI
);
1852 case TargetOpcode::G_SHL
:
1853 if (Opcode
== TargetOpcode::G_SHL
&&
1854 MRI
.getType(I
.getOperand(0).getReg()).isVector())
1855 return selectVectorSHL(I
, MRI
);
1857 case TargetOpcode::G_OR
:
1858 case TargetOpcode::G_LSHR
: {
1859 // Reject the various things we don't support yet.
1860 if (unsupportedBinOp(I
, RBI
, MRI
, TRI
))
1863 const unsigned OpSize
= Ty
.getSizeInBits();
1865 const Register DefReg
= I
.getOperand(0).getReg();
1866 const RegisterBank
&RB
= *RBI
.getRegBank(DefReg
, MRI
, TRI
);
1868 const unsigned NewOpc
= selectBinaryOp(I
.getOpcode(), RB
.getID(), OpSize
);
1869 if (NewOpc
== I
.getOpcode())
1872 I
.setDesc(TII
.get(NewOpc
));
1873 // FIXME: Should the type be always reset in setDesc?
1875 // Now that we selected an opcode, we need to constrain the register
1876 // operands to use appropriate classes.
1877 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1880 case TargetOpcode::G_GEP
: {
1881 MachineIRBuilder
MIRBuilder(I
);
1882 emitADD(I
.getOperand(0).getReg(), I
.getOperand(1), I
.getOperand(2),
1884 I
.eraseFromParent();
1887 case TargetOpcode::G_UADDO
: {
1888 // TODO: Support other types.
1889 unsigned OpSize
= Ty
.getSizeInBits();
1890 if (OpSize
!= 32 && OpSize
!= 64) {
1893 << "G_UADDO currently only supported for 32 and 64 b types.\n");
1897 // TODO: Support vectors.
1898 if (Ty
.isVector()) {
1899 LLVM_DEBUG(dbgs() << "G_UADDO currently only supported for scalars.\n");
1903 // Add and set the set condition flag.
1904 unsigned AddsOpc
= OpSize
== 32 ? AArch64::ADDSWrr
: AArch64::ADDSXrr
;
1905 MachineIRBuilder
MIRBuilder(I
);
1906 auto AddsMI
= MIRBuilder
.buildInstr(
1907 AddsOpc
, {I
.getOperand(0).getReg()},
1908 {I
.getOperand(2).getReg(), I
.getOperand(3).getReg()});
1909 constrainSelectedInstRegOperands(*AddsMI
, TII
, TRI
, RBI
);
1911 // Now, put the overflow result in the register given by the first operand
1912 // to the G_UADDO. CSINC increments the result when the predicate is false,
1913 // so to get the increment when it's true, we need to use the inverse. In
1914 // this case, we want to increment when carry is set.
1915 auto CsetMI
= MIRBuilder
1916 .buildInstr(AArch64::CSINCWr
, {I
.getOperand(1).getReg()},
1917 {Register(AArch64::WZR
), Register(AArch64::WZR
)})
1918 .addImm(getInvertedCondCode(AArch64CC::HS
));
1919 constrainSelectedInstRegOperands(*CsetMI
, TII
, TRI
, RBI
);
1920 I
.eraseFromParent();
1924 case TargetOpcode::G_PTR_MASK
: {
1925 uint64_t Align
= I
.getOperand(2).getImm();
1926 if (Align
>= 64 || Align
== 0)
1929 uint64_t Mask
= ~((1ULL << Align
) - 1);
1930 I
.setDesc(TII
.get(AArch64::ANDXri
));
1931 I
.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask
, 64));
1933 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1935 case TargetOpcode::G_PTRTOINT
:
1936 case TargetOpcode::G_TRUNC
: {
1937 const LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg());
1938 const LLT SrcTy
= MRI
.getType(I
.getOperand(1).getReg());
1940 const Register DstReg
= I
.getOperand(0).getReg();
1941 const Register SrcReg
= I
.getOperand(1).getReg();
1943 const RegisterBank
&DstRB
= *RBI
.getRegBank(DstReg
, MRI
, TRI
);
1944 const RegisterBank
&SrcRB
= *RBI
.getRegBank(SrcReg
, MRI
, TRI
);
1946 if (DstRB
.getID() != SrcRB
.getID()) {
1948 dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
1952 if (DstRB
.getID() == AArch64::GPRRegBankID
) {
1953 const TargetRegisterClass
*DstRC
=
1954 getRegClassForTypeOnBank(DstTy
, DstRB
, RBI
);
1958 const TargetRegisterClass
*SrcRC
=
1959 getRegClassForTypeOnBank(SrcTy
, SrcRB
, RBI
);
1963 if (!RBI
.constrainGenericRegister(SrcReg
, *SrcRC
, MRI
) ||
1964 !RBI
.constrainGenericRegister(DstReg
, *DstRC
, MRI
)) {
1965 LLVM_DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
1969 if (DstRC
== SrcRC
) {
1970 // Nothing to be done
1971 } else if (Opcode
== TargetOpcode::G_TRUNC
&& DstTy
== LLT::scalar(32) &&
1972 SrcTy
== LLT::scalar(64)) {
1973 llvm_unreachable("TableGen can import this case");
1975 } else if (DstRC
== &AArch64::GPR32RegClass
&&
1976 SrcRC
== &AArch64::GPR64RegClass
) {
1977 I
.getOperand(1).setSubReg(AArch64::sub_32
);
1980 dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
1984 I
.setDesc(TII
.get(TargetOpcode::COPY
));
1986 } else if (DstRB
.getID() == AArch64::FPRRegBankID
) {
1987 if (DstTy
== LLT::vector(4, 16) && SrcTy
== LLT::vector(4, 32)) {
1988 I
.setDesc(TII
.get(AArch64::XTNv4i16
));
1989 constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
1993 if (!SrcTy
.isVector() && SrcTy
.getSizeInBits() == 128) {
1994 MachineIRBuilder
MIB(I
);
1995 MachineInstr
*Extract
= emitExtractVectorElt(
1996 DstReg
, DstRB
, LLT::scalar(DstTy
.getSizeInBits()), SrcReg
, 0, MIB
);
1999 I
.eraseFromParent();
2007 case TargetOpcode::G_ANYEXT
: {
2008 const Register DstReg
= I
.getOperand(0).getReg();
2009 const Register SrcReg
= I
.getOperand(1).getReg();
2011 const RegisterBank
&RBDst
= *RBI
.getRegBank(DstReg
, MRI
, TRI
);
2012 if (RBDst
.getID() != AArch64::GPRRegBankID
) {
2013 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst
2014 << ", expected: GPR\n");
2018 const RegisterBank
&RBSrc
= *RBI
.getRegBank(SrcReg
, MRI
, TRI
);
2019 if (RBSrc
.getID() != AArch64::GPRRegBankID
) {
2020 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc
2021 << ", expected: GPR\n");
2025 const unsigned DstSize
= MRI
.getType(DstReg
).getSizeInBits();
2028 LLVM_DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
2032 if (DstSize
!= 64 && DstSize
> 32) {
2033 LLVM_DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
2034 << ", expected: 32 or 64\n");
2037 // At this point G_ANYEXT is just like a plain COPY, but we need
2038 // to explicitly form the 64-bit value if any.
2040 Register ExtSrc
= MRI
.createVirtualRegister(&AArch64::GPR64allRegClass
);
2041 BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::SUBREG_TO_REG
))
2045 .addImm(AArch64::sub_32
);
2046 I
.getOperand(1).setReg(ExtSrc
);
2048 return selectCopy(I
, TII
, MRI
, TRI
, RBI
);
2051 case TargetOpcode::G_ZEXT
:
2052 case TargetOpcode::G_SEXT
: {
2053 unsigned Opcode
= I
.getOpcode();
2054 const bool IsSigned
= Opcode
== TargetOpcode::G_SEXT
;
2055 const Register DefReg
= I
.getOperand(0).getReg();
2056 const Register SrcReg
= I
.getOperand(1).getReg();
2057 const LLT DstTy
= MRI
.getType(DefReg
);
2058 const LLT SrcTy
= MRI
.getType(SrcReg
);
2059 unsigned DstSize
= DstTy
.getSizeInBits();
2060 unsigned SrcSize
= SrcTy
.getSizeInBits();
2062 assert((*RBI
.getRegBank(DefReg
, MRI
, TRI
)).getID() ==
2063 AArch64::GPRRegBankID
&&
2064 "Unexpected ext regbank");
2066 MachineIRBuilder
MIB(I
);
2068 if (DstTy
.isVector())
2069 return false; // Should be handled by imported patterns.
2071 // First check if we're extending the result of a load which has a dest type
2072 // smaller than 32 bits, then this zext is redundant. GPR32 is the smallest
2073 // GPR register on AArch64 and all loads which are smaller automatically
2074 // zero-extend the upper bits. E.g.
2075 // %v(s8) = G_LOAD %p, :: (load 1)
2076 // %v2(s32) = G_ZEXT %v(s8)
2078 auto *LoadMI
= getOpcodeDef(TargetOpcode::G_LOAD
, SrcReg
, MRI
);
2080 RBI
.getRegBank(SrcReg
, MRI
, TRI
)->getID() == AArch64::GPRRegBankID
) {
2081 const MachineMemOperand
*MemOp
= *LoadMI
->memoperands_begin();
2082 unsigned BytesLoaded
= MemOp
->getSize();
2083 if (BytesLoaded
< 4 && SrcTy
.getSizeInBytes() == BytesLoaded
)
2084 return selectCopy(I
, TII
, MRI
, TRI
, RBI
);
2088 if (DstSize
== 64) {
2089 // FIXME: Can we avoid manually doing this?
2090 if (!RBI
.constrainGenericRegister(SrcReg
, AArch64::GPR32RegClass
, MRI
)) {
2091 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII
.getName(Opcode
)
2097 MIB
.buildInstr(AArch64::SUBREG_TO_REG
, {&AArch64::GPR64RegClass
}, {})
2100 .addImm(AArch64::sub_32
);
2102 ExtI
= MIB
.buildInstr(IsSigned
? AArch64::SBFMXri
: AArch64::UBFMXri
,
2103 {DefReg
}, {SubregToReg
})
2105 .addImm(SrcSize
- 1);
2106 } else if (DstSize
<= 32) {
2107 ExtI
= MIB
.buildInstr(IsSigned
? AArch64::SBFMWri
: AArch64::UBFMWri
,
2110 .addImm(SrcSize
- 1);
2115 constrainSelectedInstRegOperands(*ExtI
, TII
, TRI
, RBI
);
2116 I
.eraseFromParent();
2120 case TargetOpcode::G_SITOFP
:
2121 case TargetOpcode::G_UITOFP
:
2122 case TargetOpcode::G_FPTOSI
:
2123 case TargetOpcode::G_FPTOUI
: {
2124 const LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg()),
2125 SrcTy
= MRI
.getType(I
.getOperand(1).getReg());
2126 const unsigned NewOpc
= selectFPConvOpc(Opcode
, DstTy
, SrcTy
);
2127 if (NewOpc
== Opcode
)
2130 I
.setDesc(TII
.get(NewOpc
));
2131 constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
2137 case TargetOpcode::G_INTTOPTR
:
2138 // The importer is currently unable to import pointer types since they
2139 // didn't exist in SelectionDAG.
2140 return selectCopy(I
, TII
, MRI
, TRI
, RBI
);
2142 case TargetOpcode::G_BITCAST
:
2143 // Imported SelectionDAG rules can handle every bitcast except those that
2144 // bitcast from a type to the same type. Ideally, these shouldn't occur
2145 // but we might not run an optimizer that deletes them. The other exception
2146 // is bitcasts involving pointer types, as SelectionDAG has no knowledge
2148 return selectCopy(I
, TII
, MRI
, TRI
, RBI
);
2150 case TargetOpcode::G_SELECT
: {
2151 if (MRI
.getType(I
.getOperand(1).getReg()) != LLT::scalar(1)) {
2152 LLVM_DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
2153 << ", expected: " << LLT::scalar(1) << '\n');
2157 const Register CondReg
= I
.getOperand(1).getReg();
2158 const Register TReg
= I
.getOperand(2).getReg();
2159 const Register FReg
= I
.getOperand(3).getReg();
2161 if (tryOptSelect(I
))
2164 Register CSelOpc
= selectSelectOpc(I
, MRI
, RBI
);
2165 MachineInstr
&TstMI
=
2166 *BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::ANDSWri
))
2167 .addDef(AArch64::WZR
)
2169 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
2171 MachineInstr
&CSelMI
= *BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(CSelOpc
))
2172 .addDef(I
.getOperand(0).getReg())
2175 .addImm(AArch64CC::NE
);
2177 constrainSelectedInstRegOperands(TstMI
, TII
, TRI
, RBI
);
2178 constrainSelectedInstRegOperands(CSelMI
, TII
, TRI
, RBI
);
2180 I
.eraseFromParent();
2183 case TargetOpcode::G_ICMP
: {
2185 return selectVectorICmp(I
, MRI
);
2187 if (Ty
!= LLT::scalar(32)) {
2188 LLVM_DEBUG(dbgs() << "G_ICMP result has type: " << Ty
2189 << ", expected: " << LLT::scalar(32) << '\n');
2193 MachineIRBuilder
MIRBuilder(I
);
2194 if (!emitIntegerCompare(I
.getOperand(2), I
.getOperand(3), I
.getOperand(1),
2197 emitCSetForICMP(I
.getOperand(0).getReg(), I
.getOperand(1).getPredicate(),
2199 I
.eraseFromParent();
2203 case TargetOpcode::G_FCMP
: {
2204 if (Ty
!= LLT::scalar(32)) {
2205 LLVM_DEBUG(dbgs() << "G_FCMP result has type: " << Ty
2206 << ", expected: " << LLT::scalar(32) << '\n');
2210 unsigned CmpOpc
= selectFCMPOpc(I
, MRI
);
2216 AArch64CC::CondCode CC1
, CC2
;
2217 changeFCMPPredToAArch64CC(
2218 (CmpInst::Predicate
)I
.getOperand(1).getPredicate(), CC1
, CC2
);
2220 // Partially build the compare. Decide if we need to add a use for the
2221 // third operand based off whether or not we're comparing against 0.0.
2222 auto CmpMI
= BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(CmpOpc
))
2223 .addUse(I
.getOperand(2).getReg());
2225 // If we don't have an immediate compare, then we need to add a use of the
2226 // register which wasn't used for the immediate.
2227 // Note that the immediate will always be the last operand.
2228 if (CmpOpc
!= AArch64::FCMPSri
&& CmpOpc
!= AArch64::FCMPDri
)
2229 CmpMI
= CmpMI
.addUse(I
.getOperand(3).getReg());
2231 const Register DefReg
= I
.getOperand(0).getReg();
2232 Register Def1Reg
= DefReg
;
2233 if (CC2
!= AArch64CC::AL
)
2234 Def1Reg
= MRI
.createVirtualRegister(&AArch64::GPR32RegClass
);
2236 MachineInstr
&CSetMI
=
2237 *BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::CSINCWr
))
2239 .addUse(AArch64::WZR
)
2240 .addUse(AArch64::WZR
)
2241 .addImm(getInvertedCondCode(CC1
));
2243 if (CC2
!= AArch64CC::AL
) {
2244 Register Def2Reg
= MRI
.createVirtualRegister(&AArch64::GPR32RegClass
);
2245 MachineInstr
&CSet2MI
=
2246 *BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::CSINCWr
))
2248 .addUse(AArch64::WZR
)
2249 .addUse(AArch64::WZR
)
2250 .addImm(getInvertedCondCode(CC2
));
2251 MachineInstr
&OrMI
=
2252 *BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::ORRWrr
))
2256 constrainSelectedInstRegOperands(OrMI
, TII
, TRI
, RBI
);
2257 constrainSelectedInstRegOperands(CSet2MI
, TII
, TRI
, RBI
);
2259 constrainSelectedInstRegOperands(*CmpMI
, TII
, TRI
, RBI
);
2260 constrainSelectedInstRegOperands(CSetMI
, TII
, TRI
, RBI
);
2262 I
.eraseFromParent();
2265 case TargetOpcode::G_VASTART
:
2266 return STI
.isTargetDarwin() ? selectVaStartDarwin(I
, MF
, MRI
)
2267 : selectVaStartAAPCS(I
, MF
, MRI
);
2268 case TargetOpcode::G_INTRINSIC
:
2269 return selectIntrinsic(I
, MRI
);
2270 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS
:
2271 return selectIntrinsicWithSideEffects(I
, MRI
);
2272 case TargetOpcode::G_IMPLICIT_DEF
: {
2273 I
.setDesc(TII
.get(TargetOpcode::IMPLICIT_DEF
));
2274 const LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg());
2275 const Register DstReg
= I
.getOperand(0).getReg();
2276 const RegisterBank
&DstRB
= *RBI
.getRegBank(DstReg
, MRI
, TRI
);
2277 const TargetRegisterClass
*DstRC
=
2278 getRegClassForTypeOnBank(DstTy
, DstRB
, RBI
);
2279 RBI
.constrainGenericRegister(DstReg
, *DstRC
, MRI
);
2282 case TargetOpcode::G_BLOCK_ADDR
: {
2283 if (TM
.getCodeModel() == CodeModel::Large
) {
2284 materializeLargeCMVal(I
, I
.getOperand(1).getBlockAddress(), 0);
2285 I
.eraseFromParent();
2288 I
.setDesc(TII
.get(AArch64::MOVaddrBA
));
2289 auto MovMI
= BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(AArch64::MOVaddrBA
),
2290 I
.getOperand(0).getReg())
2291 .addBlockAddress(I
.getOperand(1).getBlockAddress(),
2292 /* Offset */ 0, AArch64II::MO_PAGE
)
2294 I
.getOperand(1).getBlockAddress(), /* Offset */ 0,
2295 AArch64II::MO_NC
| AArch64II::MO_PAGEOFF
);
2296 I
.eraseFromParent();
2297 return constrainSelectedInstRegOperands(*MovMI
, TII
, TRI
, RBI
);
2300 case TargetOpcode::G_INTRINSIC_TRUNC
:
2301 return selectIntrinsicTrunc(I
, MRI
);
2302 case TargetOpcode::G_INTRINSIC_ROUND
:
2303 return selectIntrinsicRound(I
, MRI
);
2304 case TargetOpcode::G_BUILD_VECTOR
:
2305 return selectBuildVector(I
, MRI
);
2306 case TargetOpcode::G_MERGE_VALUES
:
2307 return selectMergeValues(I
, MRI
);
2308 case TargetOpcode::G_UNMERGE_VALUES
:
2309 return selectUnmergeValues(I
, MRI
);
2310 case TargetOpcode::G_SHUFFLE_VECTOR
:
2311 return selectShuffleVector(I
, MRI
);
2312 case TargetOpcode::G_EXTRACT_VECTOR_ELT
:
2313 return selectExtractElt(I
, MRI
);
2314 case TargetOpcode::G_INSERT_VECTOR_ELT
:
2315 return selectInsertElt(I
, MRI
);
2316 case TargetOpcode::G_CONCAT_VECTORS
:
2317 return selectConcatVectors(I
, MRI
);
2318 case TargetOpcode::G_JUMP_TABLE
:
2319 return selectJumpTable(I
, MRI
);
2325 bool AArch64InstructionSelector::selectBrJT(MachineInstr
&I
,
2326 MachineRegisterInfo
&MRI
) const {
2327 assert(I
.getOpcode() == TargetOpcode::G_BRJT
&& "Expected G_BRJT");
2328 Register JTAddr
= I
.getOperand(0).getReg();
2329 unsigned JTI
= I
.getOperand(1).getIndex();
2330 Register Index
= I
.getOperand(2).getReg();
2331 MachineIRBuilder
MIB(I
);
2333 Register TargetReg
= MRI
.createVirtualRegister(&AArch64::GPR64RegClass
);
2334 Register ScratchReg
= MRI
.createVirtualRegister(&AArch64::GPR64spRegClass
);
2335 MIB
.buildInstr(AArch64::JumpTableDest32
, {TargetReg
, ScratchReg
},
2337 .addJumpTableIndex(JTI
);
2339 // Build the indirect branch.
2340 MIB
.buildInstr(AArch64::BR
, {}, {TargetReg
});
2341 I
.eraseFromParent();
2345 bool AArch64InstructionSelector::selectJumpTable(
2346 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2347 assert(I
.getOpcode() == TargetOpcode::G_JUMP_TABLE
&& "Expected jump table");
2348 assert(I
.getOperand(1).isJTI() && "Jump table op should have a JTI!");
2350 Register DstReg
= I
.getOperand(0).getReg();
2351 unsigned JTI
= I
.getOperand(1).getIndex();
2352 // We generate a MOVaddrJT which will get expanded to an ADRP + ADD later.
2353 MachineIRBuilder
MIB(I
);
2355 MIB
.buildInstr(AArch64::MOVaddrJT
, {DstReg
}, {})
2356 .addJumpTableIndex(JTI
, AArch64II::MO_PAGE
)
2357 .addJumpTableIndex(JTI
, AArch64II::MO_NC
| AArch64II::MO_PAGEOFF
);
2358 I
.eraseFromParent();
2359 return constrainSelectedInstRegOperands(*MovMI
, TII
, TRI
, RBI
);
2362 bool AArch64InstructionSelector::selectTLSGlobalValue(
2363 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2364 if (!STI
.isTargetMachO())
2366 MachineFunction
&MF
= *I
.getParent()->getParent();
2367 MF
.getFrameInfo().setAdjustsStack(true);
2369 const GlobalValue
&GV
= *I
.getOperand(1).getGlobal();
2370 MachineIRBuilder
MIB(I
);
2372 MIB
.buildInstr(AArch64::LOADgot
, {AArch64::X0
}, {})
2373 .addGlobalAddress(&GV
, 0, AArch64II::MO_TLS
);
2375 auto Load
= MIB
.buildInstr(AArch64::LDRXui
, {&AArch64::GPR64commonRegClass
},
2376 {Register(AArch64::X0
)})
2379 // TLS calls preserve all registers except those that absolutely must be
2380 // trashed: X0 (it takes an argument), LR (it's a call) and NZCV (let's not be
2382 MIB
.buildInstr(AArch64::BLR
, {}, {Load
})
2383 .addDef(AArch64::X0
, RegState::Implicit
)
2384 .addRegMask(TRI
.getTLSCallPreservedMask());
2386 MIB
.buildCopy(I
.getOperand(0).getReg(), Register(AArch64::X0
));
2387 RBI
.constrainGenericRegister(I
.getOperand(0).getReg(), AArch64::GPR64RegClass
,
2389 I
.eraseFromParent();
2393 bool AArch64InstructionSelector::selectIntrinsicTrunc(
2394 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2395 const LLT SrcTy
= MRI
.getType(I
.getOperand(0).getReg());
2397 // Select the correct opcode.
2399 if (!SrcTy
.isVector()) {
2400 switch (SrcTy
.getSizeInBits()) {
2403 Opc
= AArch64::FRINTZHr
;
2406 Opc
= AArch64::FRINTZSr
;
2409 Opc
= AArch64::FRINTZDr
;
2413 unsigned NumElts
= SrcTy
.getNumElements();
2414 switch (SrcTy
.getElementType().getSizeInBits()) {
2419 Opc
= AArch64::FRINTZv4f16
;
2420 else if (NumElts
== 8)
2421 Opc
= AArch64::FRINTZv8f16
;
2425 Opc
= AArch64::FRINTZv2f32
;
2426 else if (NumElts
== 4)
2427 Opc
= AArch64::FRINTZv4f32
;
2431 Opc
= AArch64::FRINTZv2f64
;
2437 // Didn't get an opcode above, bail.
2438 LLVM_DEBUG(dbgs() << "Unsupported type for G_INTRINSIC_TRUNC!\n");
2442 // Legalization would have set us up perfectly for this; we just need to
2443 // set the opcode and move on.
2444 I
.setDesc(TII
.get(Opc
));
2445 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
2448 bool AArch64InstructionSelector::selectIntrinsicRound(
2449 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2450 const LLT SrcTy
= MRI
.getType(I
.getOperand(0).getReg());
2452 // Select the correct opcode.
2454 if (!SrcTy
.isVector()) {
2455 switch (SrcTy
.getSizeInBits()) {
2458 Opc
= AArch64::FRINTAHr
;
2461 Opc
= AArch64::FRINTASr
;
2464 Opc
= AArch64::FRINTADr
;
2468 unsigned NumElts
= SrcTy
.getNumElements();
2469 switch (SrcTy
.getElementType().getSizeInBits()) {
2474 Opc
= AArch64::FRINTAv4f16
;
2475 else if (NumElts
== 8)
2476 Opc
= AArch64::FRINTAv8f16
;
2480 Opc
= AArch64::FRINTAv2f32
;
2481 else if (NumElts
== 4)
2482 Opc
= AArch64::FRINTAv4f32
;
2486 Opc
= AArch64::FRINTAv2f64
;
2492 // Didn't get an opcode above, bail.
2493 LLVM_DEBUG(dbgs() << "Unsupported type for G_INTRINSIC_ROUND!\n");
2497 // Legalization would have set us up perfectly for this; we just need to
2498 // set the opcode and move on.
2499 I
.setDesc(TII
.get(Opc
));
2500 return constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
2503 bool AArch64InstructionSelector::selectVectorICmp(
2504 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2505 Register DstReg
= I
.getOperand(0).getReg();
2506 LLT DstTy
= MRI
.getType(DstReg
);
2507 Register SrcReg
= I
.getOperand(2).getReg();
2508 Register Src2Reg
= I
.getOperand(3).getReg();
2509 LLT SrcTy
= MRI
.getType(SrcReg
);
2511 unsigned SrcEltSize
= SrcTy
.getElementType().getSizeInBits();
2512 unsigned NumElts
= DstTy
.getNumElements();
2514 // First index is element size, 0 == 8b, 1 == 16b, 2 == 32b, 3 == 64b
2515 // Second index is num elts, 0 == v2, 1 == v4, 2 == v8, 3 == v16
2516 // Third index is cc opcode:
2526 // ne is done by negating 'eq' result.
2528 // This table below assumes that for some comparisons the operands will be
2530 // ult op == commute + ugt op
2531 // ule op == commute + uge op
2532 // slt op == commute + sgt op
2533 // sle op == commute + sge op
2534 unsigned PredIdx
= 0;
2535 bool SwapOperands
= false;
2536 CmpInst::Predicate Pred
= (CmpInst::Predicate
)I
.getOperand(1).getPredicate();
2538 case CmpInst::ICMP_NE
:
2539 case CmpInst::ICMP_EQ
:
2542 case CmpInst::ICMP_UGT
:
2545 case CmpInst::ICMP_UGE
:
2548 case CmpInst::ICMP_ULT
:
2550 SwapOperands
= true;
2552 case CmpInst::ICMP_ULE
:
2554 SwapOperands
= true;
2556 case CmpInst::ICMP_SGT
:
2559 case CmpInst::ICMP_SGE
:
2562 case CmpInst::ICMP_SLT
:
2564 SwapOperands
= true;
2566 case CmpInst::ICMP_SLE
:
2568 SwapOperands
= true;
2571 llvm_unreachable("Unhandled icmp predicate");
2575 // This table obviously should be tablegen'd when we have our GISel native
2576 // tablegen selector.
2578 static const unsigned OpcTable
[4][4][9] = {
2580 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2581 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2583 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2584 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2586 {AArch64::CMEQv8i8
, AArch64::CMHIv8i8
, AArch64::CMHSv8i8
,
2587 AArch64::CMHIv8i8
, AArch64::CMHSv8i8
, AArch64::CMGTv8i8
,
2588 AArch64::CMGEv8i8
, AArch64::CMGTv8i8
, AArch64::CMGEv8i8
},
2589 {AArch64::CMEQv16i8
, AArch64::CMHIv16i8
, AArch64::CMHSv16i8
,
2590 AArch64::CMHIv16i8
, AArch64::CMHSv16i8
, AArch64::CMGTv16i8
,
2591 AArch64::CMGEv16i8
, AArch64::CMGTv16i8
, AArch64::CMGEv16i8
}
2594 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2595 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2597 {AArch64::CMEQv4i16
, AArch64::CMHIv4i16
, AArch64::CMHSv4i16
,
2598 AArch64::CMHIv4i16
, AArch64::CMHSv4i16
, AArch64::CMGTv4i16
,
2599 AArch64::CMGEv4i16
, AArch64::CMGTv4i16
, AArch64::CMGEv4i16
},
2600 {AArch64::CMEQv8i16
, AArch64::CMHIv8i16
, AArch64::CMHSv8i16
,
2601 AArch64::CMHIv8i16
, AArch64::CMHSv8i16
, AArch64::CMGTv8i16
,
2602 AArch64::CMGEv8i16
, AArch64::CMGTv8i16
, AArch64::CMGEv8i16
},
2603 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2604 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2608 {AArch64::CMEQv2i32
, AArch64::CMHIv2i32
, AArch64::CMHSv2i32
,
2609 AArch64::CMHIv2i32
, AArch64::CMHSv2i32
, AArch64::CMGTv2i32
,
2610 AArch64::CMGEv2i32
, AArch64::CMGTv2i32
, AArch64::CMGEv2i32
},
2611 {AArch64::CMEQv4i32
, AArch64::CMHIv4i32
, AArch64::CMHSv4i32
,
2612 AArch64::CMHIv4i32
, AArch64::CMHSv4i32
, AArch64::CMGTv4i32
,
2613 AArch64::CMGEv4i32
, AArch64::CMGTv4i32
, AArch64::CMGEv4i32
},
2614 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2615 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2617 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2618 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2622 {AArch64::CMEQv2i64
, AArch64::CMHIv2i64
, AArch64::CMHSv2i64
,
2623 AArch64::CMHIv2i64
, AArch64::CMHSv2i64
, AArch64::CMGTv2i64
,
2624 AArch64::CMGEv2i64
, AArch64::CMGTv2i64
, AArch64::CMGEv2i64
},
2625 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2626 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2628 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2629 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2631 {0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2632 0 /* invalid */, 0 /* invalid */, 0 /* invalid */, 0 /* invalid */,
2636 unsigned EltIdx
= Log2_32(SrcEltSize
/ 8);
2637 unsigned NumEltsIdx
= Log2_32(NumElts
/ 2);
2638 unsigned Opc
= OpcTable
[EltIdx
][NumEltsIdx
][PredIdx
];
2640 LLVM_DEBUG(dbgs() << "Could not map G_ICMP to cmp opcode");
2644 const RegisterBank
&VecRB
= *RBI
.getRegBank(SrcReg
, MRI
, TRI
);
2645 const TargetRegisterClass
*SrcRC
=
2646 getRegClassForTypeOnBank(SrcTy
, VecRB
, RBI
, true);
2648 LLVM_DEBUG(dbgs() << "Could not determine source register class.\n");
2652 unsigned NotOpc
= Pred
== ICmpInst::ICMP_NE
? AArch64::NOTv8i8
: 0;
2653 if (SrcTy
.getSizeInBits() == 128)
2654 NotOpc
= NotOpc
? AArch64::NOTv16i8
: 0;
2657 std::swap(SrcReg
, Src2Reg
);
2659 MachineIRBuilder
MIB(I
);
2660 auto Cmp
= MIB
.buildInstr(Opc
, {SrcRC
}, {SrcReg
, Src2Reg
});
2661 constrainSelectedInstRegOperands(*Cmp
, TII
, TRI
, RBI
);
2663 // Invert if we had a 'ne' cc.
2665 Cmp
= MIB
.buildInstr(NotOpc
, {DstReg
}, {Cmp
});
2666 constrainSelectedInstRegOperands(*Cmp
, TII
, TRI
, RBI
);
2668 MIB
.buildCopy(DstReg
, Cmp
.getReg(0));
2670 RBI
.constrainGenericRegister(DstReg
, *SrcRC
, MRI
);
2671 I
.eraseFromParent();
2675 MachineInstr
*AArch64InstructionSelector::emitScalarToVector(
2676 unsigned EltSize
, const TargetRegisterClass
*DstRC
, Register Scalar
,
2677 MachineIRBuilder
&MIRBuilder
) const {
2678 auto Undef
= MIRBuilder
.buildInstr(TargetOpcode::IMPLICIT_DEF
, {DstRC
}, {});
2680 auto BuildFn
= [&](unsigned SubregIndex
) {
2683 .buildInstr(TargetOpcode::INSERT_SUBREG
, {DstRC
}, {Undef
, Scalar
})
2684 .addImm(SubregIndex
);
2685 constrainSelectedInstRegOperands(*Undef
, TII
, TRI
, RBI
);
2686 constrainSelectedInstRegOperands(*Ins
, TII
, TRI
, RBI
);
2692 return BuildFn(AArch64::hsub
);
2694 return BuildFn(AArch64::ssub
);
2696 return BuildFn(AArch64::dsub
);
2702 bool AArch64InstructionSelector::selectMergeValues(
2703 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2704 assert(I
.getOpcode() == TargetOpcode::G_MERGE_VALUES
&& "unexpected opcode");
2705 const LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg());
2706 const LLT SrcTy
= MRI
.getType(I
.getOperand(1).getReg());
2707 assert(!DstTy
.isVector() && !SrcTy
.isVector() && "invalid merge operation");
2708 const RegisterBank
&RB
= *RBI
.getRegBank(I
.getOperand(1).getReg(), MRI
, TRI
);
2710 if (I
.getNumOperands() != 3)
2713 // Merging 2 s64s into an s128.
2714 if (DstTy
== LLT::scalar(128)) {
2715 if (SrcTy
.getSizeInBits() != 64)
2717 MachineIRBuilder
MIB(I
);
2718 Register DstReg
= I
.getOperand(0).getReg();
2719 Register Src1Reg
= I
.getOperand(1).getReg();
2720 Register Src2Reg
= I
.getOperand(2).getReg();
2721 auto Tmp
= MIB
.buildInstr(TargetOpcode::IMPLICIT_DEF
, {DstTy
}, {});
2722 MachineInstr
*InsMI
=
2723 emitLaneInsert(None
, Tmp
.getReg(0), Src1Reg
, /* LaneIdx */ 0, RB
, MIB
);
2726 MachineInstr
*Ins2MI
= emitLaneInsert(DstReg
, InsMI
->getOperand(0).getReg(),
2727 Src2Reg
, /* LaneIdx */ 1, RB
, MIB
);
2730 constrainSelectedInstRegOperands(*InsMI
, TII
, TRI
, RBI
);
2731 constrainSelectedInstRegOperands(*Ins2MI
, TII
, TRI
, RBI
);
2732 I
.eraseFromParent();
2736 if (RB
.getID() != AArch64::GPRRegBankID
)
2739 if (DstTy
.getSizeInBits() != 64 || SrcTy
.getSizeInBits() != 32)
2742 auto *DstRC
= &AArch64::GPR64RegClass
;
2743 Register SubToRegDef
= MRI
.createVirtualRegister(DstRC
);
2744 MachineInstr
&SubRegMI
= *BuildMI(*I
.getParent(), I
, I
.getDebugLoc(),
2745 TII
.get(TargetOpcode::SUBREG_TO_REG
))
2746 .addDef(SubToRegDef
)
2748 .addUse(I
.getOperand(1).getReg())
2749 .addImm(AArch64::sub_32
);
2750 Register SubToRegDef2
= MRI
.createVirtualRegister(DstRC
);
2751 // Need to anyext the second scalar before we can use bfm
2752 MachineInstr
&SubRegMI2
= *BuildMI(*I
.getParent(), I
, I
.getDebugLoc(),
2753 TII
.get(TargetOpcode::SUBREG_TO_REG
))
2754 .addDef(SubToRegDef2
)
2756 .addUse(I
.getOperand(2).getReg())
2757 .addImm(AArch64::sub_32
);
2759 *BuildMI(*I
.getParent(), I
, I
.getDebugLoc(), TII
.get(AArch64::BFMXri
))
2760 .addDef(I
.getOperand(0).getReg())
2761 .addUse(SubToRegDef
)
2762 .addUse(SubToRegDef2
)
2765 constrainSelectedInstRegOperands(SubRegMI
, TII
, TRI
, RBI
);
2766 constrainSelectedInstRegOperands(SubRegMI2
, TII
, TRI
, RBI
);
2767 constrainSelectedInstRegOperands(BFM
, TII
, TRI
, RBI
);
2768 I
.eraseFromParent();
2772 static bool getLaneCopyOpcode(unsigned &CopyOpc
, unsigned &ExtractSubReg
,
2773 const unsigned EltSize
) {
2774 // Choose a lane copy opcode and subregister based off of the size of the
2775 // vector's elements.
2778 CopyOpc
= AArch64::CPYi16
;
2779 ExtractSubReg
= AArch64::hsub
;
2782 CopyOpc
= AArch64::CPYi32
;
2783 ExtractSubReg
= AArch64::ssub
;
2786 CopyOpc
= AArch64::CPYi64
;
2787 ExtractSubReg
= AArch64::dsub
;
2790 // Unknown size, bail out.
2791 LLVM_DEBUG(dbgs() << "Elt size '" << EltSize
<< "' unsupported.\n");
2797 MachineInstr
*AArch64InstructionSelector::emitExtractVectorElt(
2798 Optional
<Register
> DstReg
, const RegisterBank
&DstRB
, LLT ScalarTy
,
2799 Register VecReg
, unsigned LaneIdx
, MachineIRBuilder
&MIRBuilder
) const {
2800 MachineRegisterInfo
&MRI
= *MIRBuilder
.getMRI();
2801 unsigned CopyOpc
= 0;
2802 unsigned ExtractSubReg
= 0;
2803 if (!getLaneCopyOpcode(CopyOpc
, ExtractSubReg
, ScalarTy
.getSizeInBits())) {
2805 dbgs() << "Couldn't determine lane copy opcode for instruction.\n");
2809 const TargetRegisterClass
*DstRC
=
2810 getRegClassForTypeOnBank(ScalarTy
, DstRB
, RBI
, true);
2812 LLVM_DEBUG(dbgs() << "Could not determine destination register class.\n");
2816 const RegisterBank
&VecRB
= *RBI
.getRegBank(VecReg
, MRI
, TRI
);
2817 const LLT
&VecTy
= MRI
.getType(VecReg
);
2818 const TargetRegisterClass
*VecRC
=
2819 getRegClassForTypeOnBank(VecTy
, VecRB
, RBI
, true);
2821 LLVM_DEBUG(dbgs() << "Could not determine source register class.\n");
2825 // The register that we're going to copy into.
2826 Register InsertReg
= VecReg
;
2828 DstReg
= MRI
.createVirtualRegister(DstRC
);
2829 // If the lane index is 0, we just use a subregister COPY.
2831 auto Copy
= MIRBuilder
.buildInstr(TargetOpcode::COPY
, {*DstReg
}, {})
2832 .addReg(VecReg
, 0, ExtractSubReg
);
2833 RBI
.constrainGenericRegister(*DstReg
, *DstRC
, MRI
);
2837 // Lane copies require 128-bit wide registers. If we're dealing with an
2838 // unpacked vector, then we need to move up to that width. Insert an implicit
2839 // def and a subregister insert to get us there.
2840 if (VecTy
.getSizeInBits() != 128) {
2841 MachineInstr
*ScalarToVector
= emitScalarToVector(
2842 VecTy
.getSizeInBits(), &AArch64::FPR128RegClass
, VecReg
, MIRBuilder
);
2843 if (!ScalarToVector
)
2845 InsertReg
= ScalarToVector
->getOperand(0).getReg();
2848 MachineInstr
*LaneCopyMI
=
2849 MIRBuilder
.buildInstr(CopyOpc
, {*DstReg
}, {InsertReg
}).addImm(LaneIdx
);
2850 constrainSelectedInstRegOperands(*LaneCopyMI
, TII
, TRI
, RBI
);
2852 // Make sure that we actually constrain the initial copy.
2853 RBI
.constrainGenericRegister(*DstReg
, *DstRC
, MRI
);
2857 bool AArch64InstructionSelector::selectExtractElt(
2858 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2859 assert(I
.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT
&&
2860 "unexpected opcode!");
2861 Register DstReg
= I
.getOperand(0).getReg();
2862 const LLT NarrowTy
= MRI
.getType(DstReg
);
2863 const Register SrcReg
= I
.getOperand(1).getReg();
2864 const LLT WideTy
= MRI
.getType(SrcReg
);
2866 assert(WideTy
.getSizeInBits() >= NarrowTy
.getSizeInBits() &&
2867 "source register size too small!");
2868 assert(NarrowTy
.isScalar() && "cannot extract vector into vector!");
2870 // Need the lane index to determine the correct copy opcode.
2871 MachineOperand
&LaneIdxOp
= I
.getOperand(2);
2872 assert(LaneIdxOp
.isReg() && "Lane index operand was not a register?");
2874 if (RBI
.getRegBank(DstReg
, MRI
, TRI
)->getID() != AArch64::FPRRegBankID
) {
2875 LLVM_DEBUG(dbgs() << "Cannot extract into GPR.\n");
2879 // Find the index to extract from.
2880 auto VRegAndVal
= getConstantVRegValWithLookThrough(LaneIdxOp
.getReg(), MRI
);
2883 unsigned LaneIdx
= VRegAndVal
->Value
;
2885 MachineIRBuilder
MIRBuilder(I
);
2887 const RegisterBank
&DstRB
= *RBI
.getRegBank(DstReg
, MRI
, TRI
);
2888 MachineInstr
*Extract
= emitExtractVectorElt(DstReg
, DstRB
, NarrowTy
, SrcReg
,
2889 LaneIdx
, MIRBuilder
);
2893 I
.eraseFromParent();
2897 bool AArch64InstructionSelector::selectSplitVectorUnmerge(
2898 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2899 unsigned NumElts
= I
.getNumOperands() - 1;
2900 Register SrcReg
= I
.getOperand(NumElts
).getReg();
2901 const LLT NarrowTy
= MRI
.getType(I
.getOperand(0).getReg());
2902 const LLT SrcTy
= MRI
.getType(SrcReg
);
2904 assert(NarrowTy
.isVector() && "Expected an unmerge into vectors");
2905 if (SrcTy
.getSizeInBits() > 128) {
2906 LLVM_DEBUG(dbgs() << "Unexpected vector type for vec split unmerge");
2910 MachineIRBuilder
MIB(I
);
2912 // We implement a split vector operation by treating the sub-vectors as
2913 // scalars and extracting them.
2914 const RegisterBank
&DstRB
=
2915 *RBI
.getRegBank(I
.getOperand(0).getReg(), MRI
, TRI
);
2916 for (unsigned OpIdx
= 0; OpIdx
< NumElts
; ++OpIdx
) {
2917 Register Dst
= I
.getOperand(OpIdx
).getReg();
2918 MachineInstr
*Extract
=
2919 emitExtractVectorElt(Dst
, DstRB
, NarrowTy
, SrcReg
, OpIdx
, MIB
);
2923 I
.eraseFromParent();
2927 bool AArch64InstructionSelector::selectUnmergeValues(
2928 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
2929 assert(I
.getOpcode() == TargetOpcode::G_UNMERGE_VALUES
&&
2930 "unexpected opcode");
2932 // TODO: Handle unmerging into GPRs and from scalars to scalars.
2933 if (RBI
.getRegBank(I
.getOperand(0).getReg(), MRI
, TRI
)->getID() !=
2934 AArch64::FPRRegBankID
||
2935 RBI
.getRegBank(I
.getOperand(1).getReg(), MRI
, TRI
)->getID() !=
2936 AArch64::FPRRegBankID
) {
2937 LLVM_DEBUG(dbgs() << "Unmerging vector-to-gpr and scalar-to-scalar "
2938 "currently unsupported.\n");
2942 // The last operand is the vector source register, and every other operand is
2943 // a register to unpack into.
2944 unsigned NumElts
= I
.getNumOperands() - 1;
2945 Register SrcReg
= I
.getOperand(NumElts
).getReg();
2946 const LLT NarrowTy
= MRI
.getType(I
.getOperand(0).getReg());
2947 const LLT WideTy
= MRI
.getType(SrcReg
);
2949 assert((WideTy
.isVector() || WideTy
.getSizeInBits() == 128) &&
2950 "can only unmerge from vector or s128 types!");
2951 assert(WideTy
.getSizeInBits() > NarrowTy
.getSizeInBits() &&
2952 "source register size too small!");
2954 if (!NarrowTy
.isScalar())
2955 return selectSplitVectorUnmerge(I
, MRI
);
2957 MachineIRBuilder
MIB(I
);
2959 // Choose a lane copy opcode and subregister based off of the size of the
2960 // vector's elements.
2961 unsigned CopyOpc
= 0;
2962 unsigned ExtractSubReg
= 0;
2963 if (!getLaneCopyOpcode(CopyOpc
, ExtractSubReg
, NarrowTy
.getSizeInBits()))
2966 // Set up for the lane copies.
2967 MachineBasicBlock
&MBB
= *I
.getParent();
2969 // Stores the registers we'll be copying from.
2970 SmallVector
<Register
, 4> InsertRegs
;
2972 // We'll use the first register twice, so we only need NumElts-1 registers.
2973 unsigned NumInsertRegs
= NumElts
- 1;
2975 // If our elements fit into exactly 128 bits, then we can copy from the source
2976 // directly. Otherwise, we need to do a bit of setup with some subregister
2978 if (NarrowTy
.getSizeInBits() * NumElts
== 128) {
2979 InsertRegs
= SmallVector
<Register
, 4>(NumInsertRegs
, SrcReg
);
2981 // No. We have to perform subregister inserts. For each insert, create an
2982 // implicit def and a subregister insert, and save the register we create.
2983 for (unsigned Idx
= 0; Idx
< NumInsertRegs
; ++Idx
) {
2984 Register ImpDefReg
= MRI
.createVirtualRegister(&AArch64::FPR128RegClass
);
2985 MachineInstr
&ImpDefMI
=
2986 *BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(TargetOpcode::IMPLICIT_DEF
),
2989 // Now, create the subregister insert from SrcReg.
2990 Register InsertReg
= MRI
.createVirtualRegister(&AArch64::FPR128RegClass
);
2991 MachineInstr
&InsMI
=
2992 *BuildMI(MBB
, I
, I
.getDebugLoc(),
2993 TII
.get(TargetOpcode::INSERT_SUBREG
), InsertReg
)
2996 .addImm(AArch64::dsub
);
2998 constrainSelectedInstRegOperands(ImpDefMI
, TII
, TRI
, RBI
);
2999 constrainSelectedInstRegOperands(InsMI
, TII
, TRI
, RBI
);
3001 // Save the register so that we can copy from it after.
3002 InsertRegs
.push_back(InsertReg
);
3006 // Now that we've created any necessary subregister inserts, we can
3007 // create the copies.
3009 // Perform the first copy separately as a subregister copy.
3010 Register CopyTo
= I
.getOperand(0).getReg();
3011 auto FirstCopy
= MIB
.buildInstr(TargetOpcode::COPY
, {CopyTo
}, {})
3012 .addReg(InsertRegs
[0], 0, ExtractSubReg
);
3013 constrainSelectedInstRegOperands(*FirstCopy
, TII
, TRI
, RBI
);
3015 // Now, perform the remaining copies as vector lane copies.
3016 unsigned LaneIdx
= 1;
3017 for (Register InsReg
: InsertRegs
) {
3018 Register CopyTo
= I
.getOperand(LaneIdx
).getReg();
3019 MachineInstr
&CopyInst
=
3020 *BuildMI(MBB
, I
, I
.getDebugLoc(), TII
.get(CopyOpc
), CopyTo
)
3023 constrainSelectedInstRegOperands(CopyInst
, TII
, TRI
, RBI
);
3027 // Separately constrain the first copy's destination. Because of the
3028 // limitation in constrainOperandRegClass, we can't guarantee that this will
3029 // actually be constrained. So, do it ourselves using the second operand.
3030 const TargetRegisterClass
*RC
=
3031 MRI
.getRegClassOrNull(I
.getOperand(1).getReg());
3033 LLVM_DEBUG(dbgs() << "Couldn't constrain copy destination.\n");
3037 RBI
.constrainGenericRegister(CopyTo
, *RC
, MRI
);
3038 I
.eraseFromParent();
3042 bool AArch64InstructionSelector::selectConcatVectors(
3043 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
3044 assert(I
.getOpcode() == TargetOpcode::G_CONCAT_VECTORS
&&
3045 "Unexpected opcode");
3046 Register Dst
= I
.getOperand(0).getReg();
3047 Register Op1
= I
.getOperand(1).getReg();
3048 Register Op2
= I
.getOperand(2).getReg();
3049 MachineIRBuilder
MIRBuilder(I
);
3050 MachineInstr
*ConcatMI
= emitVectorConcat(Dst
, Op1
, Op2
, MIRBuilder
);
3053 I
.eraseFromParent();
3058 AArch64InstructionSelector::emitConstantPoolEntry(Constant
*CPVal
,
3059 MachineFunction
&MF
) const {
3060 Type
*CPTy
= CPVal
->getType();
3061 unsigned Align
= MF
.getDataLayout().getPrefTypeAlignment(CPTy
);
3063 Align
= MF
.getDataLayout().getTypeAllocSize(CPTy
);
3065 MachineConstantPool
*MCP
= MF
.getConstantPool();
3066 return MCP
->getConstantPoolIndex(CPVal
, Align
);
3069 MachineInstr
*AArch64InstructionSelector::emitLoadFromConstantPool(
3070 Constant
*CPVal
, MachineIRBuilder
&MIRBuilder
) const {
3071 unsigned CPIdx
= emitConstantPoolEntry(CPVal
, MIRBuilder
.getMF());
3074 MIRBuilder
.buildInstr(AArch64::ADRP
, {&AArch64::GPR64RegClass
}, {})
3075 .addConstantPoolIndex(CPIdx
, 0, AArch64II::MO_PAGE
);
3077 MachineInstr
*LoadMI
= nullptr;
3078 switch (MIRBuilder
.getDataLayout().getTypeStoreSize(CPVal
->getType())) {
3082 .buildInstr(AArch64::LDRQui
, {&AArch64::FPR128RegClass
}, {Adrp
})
3083 .addConstantPoolIndex(CPIdx
, 0,
3084 AArch64II::MO_PAGEOFF
| AArch64II::MO_NC
);
3087 LoadMI
= &*MIRBuilder
3088 .buildInstr(AArch64::LDRDui
, {&AArch64::FPR64RegClass
}, {Adrp
})
3089 .addConstantPoolIndex(
3090 CPIdx
, 0, AArch64II::MO_PAGEOFF
| AArch64II::MO_NC
);
3093 LLVM_DEBUG(dbgs() << "Could not load from constant pool of type "
3094 << *CPVal
->getType());
3097 constrainSelectedInstRegOperands(*Adrp
, TII
, TRI
, RBI
);
3098 constrainSelectedInstRegOperands(*LoadMI
, TII
, TRI
, RBI
);
3102 /// Return an <Opcode, SubregIndex> pair to do an vector elt insert of a given
3104 static std::pair
<unsigned, unsigned>
3105 getInsertVecEltOpInfo(const RegisterBank
&RB
, unsigned EltSize
) {
3106 unsigned Opc
, SubregIdx
;
3107 if (RB
.getID() == AArch64::GPRRegBankID
) {
3108 if (EltSize
== 32) {
3109 Opc
= AArch64::INSvi32gpr
;
3110 SubregIdx
= AArch64::ssub
;
3111 } else if (EltSize
== 64) {
3112 Opc
= AArch64::INSvi64gpr
;
3113 SubregIdx
= AArch64::dsub
;
3115 llvm_unreachable("invalid elt size!");
3119 Opc
= AArch64::INSvi8lane
;
3120 SubregIdx
= AArch64::bsub
;
3121 } else if (EltSize
== 16) {
3122 Opc
= AArch64::INSvi16lane
;
3123 SubregIdx
= AArch64::hsub
;
3124 } else if (EltSize
== 32) {
3125 Opc
= AArch64::INSvi32lane
;
3126 SubregIdx
= AArch64::ssub
;
3127 } else if (EltSize
== 64) {
3128 Opc
= AArch64::INSvi64lane
;
3129 SubregIdx
= AArch64::dsub
;
3131 llvm_unreachable("invalid elt size!");
3134 return std::make_pair(Opc
, SubregIdx
);
3138 AArch64InstructionSelector::emitADD(Register DefReg
, MachineOperand
&LHS
,
3139 MachineOperand
&RHS
,
3140 MachineIRBuilder
&MIRBuilder
) const {
3141 assert(LHS
.isReg() && RHS
.isReg() && "Expected LHS and RHS to be registers!");
3142 MachineRegisterInfo
&MRI
= MIRBuilder
.getMF().getRegInfo();
3143 static const unsigned OpcTable
[2][2]{{AArch64::ADDXrr
, AArch64::ADDXri
},
3144 {AArch64::ADDWrr
, AArch64::ADDWri
}};
3145 bool Is32Bit
= MRI
.getType(LHS
.getReg()).getSizeInBits() == 32;
3146 auto ImmFns
= selectArithImmed(RHS
);
3147 unsigned Opc
= OpcTable
[Is32Bit
][ImmFns
.hasValue()];
3148 auto AddMI
= MIRBuilder
.buildInstr(Opc
, {DefReg
}, {LHS
.getReg()});
3150 // If we matched a valid constant immediate, add those operands.
3152 for (auto &RenderFn
: *ImmFns
)
3155 AddMI
.addUse(RHS
.getReg());
3158 constrainSelectedInstRegOperands(*AddMI
, TII
, TRI
, RBI
);
3163 AArch64InstructionSelector::emitCMN(MachineOperand
&LHS
, MachineOperand
&RHS
,
3164 MachineIRBuilder
&MIRBuilder
) const {
3165 assert(LHS
.isReg() && RHS
.isReg() && "Expected LHS and RHS to be registers!");
3166 MachineRegisterInfo
&MRI
= MIRBuilder
.getMF().getRegInfo();
3167 static const unsigned OpcTable
[2][2]{{AArch64::ADDSXrr
, AArch64::ADDSXri
},
3168 {AArch64::ADDSWrr
, AArch64::ADDSWri
}};
3169 bool Is32Bit
= (MRI
.getType(LHS
.getReg()).getSizeInBits() == 32);
3170 auto ImmFns
= selectArithImmed(RHS
);
3171 unsigned Opc
= OpcTable
[Is32Bit
][ImmFns
.hasValue()];
3172 Register ZReg
= Is32Bit
? AArch64::WZR
: AArch64::XZR
;
3174 auto CmpMI
= MIRBuilder
.buildInstr(Opc
, {ZReg
}, {LHS
.getReg()});
3176 // If we matched a valid constant immediate, add those operands.
3178 for (auto &RenderFn
: *ImmFns
)
3181 CmpMI
.addUse(RHS
.getReg());
3184 constrainSelectedInstRegOperands(*CmpMI
, TII
, TRI
, RBI
);
3189 AArch64InstructionSelector::emitTST(const Register
&LHS
, const Register
&RHS
,
3190 MachineIRBuilder
&MIRBuilder
) const {
3191 MachineRegisterInfo
&MRI
= MIRBuilder
.getMF().getRegInfo();
3192 unsigned RegSize
= MRI
.getType(LHS
).getSizeInBits();
3193 bool Is32Bit
= (RegSize
== 32);
3194 static const unsigned OpcTable
[2][2]{{AArch64::ANDSXrr
, AArch64::ANDSXri
},
3195 {AArch64::ANDSWrr
, AArch64::ANDSWri
}};
3196 Register ZReg
= Is32Bit
? AArch64::WZR
: AArch64::XZR
;
3198 // We might be able to fold in an immediate into the TST. We need to make sure
3199 // it's a logical immediate though, since ANDS requires that.
3200 auto ValAndVReg
= getConstantVRegValWithLookThrough(RHS
, MRI
);
3201 bool IsImmForm
= ValAndVReg
.hasValue() &&
3202 AArch64_AM::isLogicalImmediate(ValAndVReg
->Value
, RegSize
);
3203 unsigned Opc
= OpcTable
[Is32Bit
][IsImmForm
];
3204 auto TstMI
= MIRBuilder
.buildInstr(Opc
, {ZReg
}, {LHS
});
3208 AArch64_AM::encodeLogicalImmediate(ValAndVReg
->Value
, RegSize
));
3212 constrainSelectedInstRegOperands(*TstMI
, TII
, TRI
, RBI
);
3216 MachineInstr
*AArch64InstructionSelector::emitIntegerCompare(
3217 MachineOperand
&LHS
, MachineOperand
&RHS
, MachineOperand
&Predicate
,
3218 MachineIRBuilder
&MIRBuilder
) const {
3219 assert(LHS
.isReg() && RHS
.isReg() && "Expected LHS and RHS to be registers!");
3220 MachineRegisterInfo
&MRI
= MIRBuilder
.getMF().getRegInfo();
3222 // Fold the compare if possible.
3223 MachineInstr
*FoldCmp
=
3224 tryFoldIntegerCompare(LHS
, RHS
, Predicate
, MIRBuilder
);
3228 // Can't fold into a CMN. Just emit a normal compare.
3229 unsigned CmpOpc
= 0;
3232 LLT CmpTy
= MRI
.getType(LHS
.getReg());
3233 assert((CmpTy
.isScalar() || CmpTy
.isPointer()) &&
3234 "Expected scalar or pointer");
3235 if (CmpTy
== LLT::scalar(32)) {
3236 CmpOpc
= AArch64::SUBSWrr
;
3237 ZReg
= AArch64::WZR
;
3238 } else if (CmpTy
== LLT::scalar(64) || CmpTy
.isPointer()) {
3239 CmpOpc
= AArch64::SUBSXrr
;
3240 ZReg
= AArch64::XZR
;
3245 // Try to match immediate forms.
3246 auto ImmFns
= selectArithImmed(RHS
);
3248 CmpOpc
= CmpOpc
== AArch64::SUBSWrr
? AArch64::SUBSWri
: AArch64::SUBSXri
;
3250 auto CmpMI
= MIRBuilder
.buildInstr(CmpOpc
).addDef(ZReg
).addUse(LHS
.getReg());
3251 // If we matched a valid constant immediate, add those operands.
3253 for (auto &RenderFn
: *ImmFns
)
3256 CmpMI
.addUse(RHS
.getReg());
3259 // Make sure that we can constrain the compare that we emitted.
3260 constrainSelectedInstRegOperands(*CmpMI
, TII
, TRI
, RBI
);
3264 MachineInstr
*AArch64InstructionSelector::emitVectorConcat(
3265 Optional
<Register
> Dst
, Register Op1
, Register Op2
,
3266 MachineIRBuilder
&MIRBuilder
) const {
3267 // We implement a vector concat by:
3268 // 1. Use scalar_to_vector to insert the lower vector into the larger dest
3269 // 2. Insert the upper vector into the destination's upper element
3270 // TODO: some of this code is common with G_BUILD_VECTOR handling.
3271 MachineRegisterInfo
&MRI
= MIRBuilder
.getMF().getRegInfo();
3273 const LLT Op1Ty
= MRI
.getType(Op1
);
3274 const LLT Op2Ty
= MRI
.getType(Op2
);
3276 if (Op1Ty
!= Op2Ty
) {
3277 LLVM_DEBUG(dbgs() << "Could not do vector concat of differing vector tys");
3280 assert(Op1Ty
.isVector() && "Expected a vector for vector concat");
3282 if (Op1Ty
.getSizeInBits() >= 128) {
3283 LLVM_DEBUG(dbgs() << "Vector concat not supported for full size vectors");
3287 // At the moment we just support 64 bit vector concats.
3288 if (Op1Ty
.getSizeInBits() != 64) {
3289 LLVM_DEBUG(dbgs() << "Vector concat supported for 64b vectors");
3293 const LLT ScalarTy
= LLT::scalar(Op1Ty
.getSizeInBits());
3294 const RegisterBank
&FPRBank
= *RBI
.getRegBank(Op1
, MRI
, TRI
);
3295 const TargetRegisterClass
*DstRC
=
3296 getMinClassForRegBank(FPRBank
, Op1Ty
.getSizeInBits() * 2);
3298 MachineInstr
*WidenedOp1
=
3299 emitScalarToVector(ScalarTy
.getSizeInBits(), DstRC
, Op1
, MIRBuilder
);
3300 MachineInstr
*WidenedOp2
=
3301 emitScalarToVector(ScalarTy
.getSizeInBits(), DstRC
, Op2
, MIRBuilder
);
3302 if (!WidenedOp1
|| !WidenedOp2
) {
3303 LLVM_DEBUG(dbgs() << "Could not emit a vector from scalar value");
3307 // Now do the insert of the upper element.
3308 unsigned InsertOpc
, InsSubRegIdx
;
3309 std::tie(InsertOpc
, InsSubRegIdx
) =
3310 getInsertVecEltOpInfo(FPRBank
, ScalarTy
.getSizeInBits());
3313 Dst
= MRI
.createVirtualRegister(DstRC
);
3316 .buildInstr(InsertOpc
, {*Dst
}, {WidenedOp1
->getOperand(0).getReg()})
3317 .addImm(1) /* Lane index */
3318 .addUse(WidenedOp2
->getOperand(0).getReg())
3320 constrainSelectedInstRegOperands(*InsElt
, TII
, TRI
, RBI
);
3324 MachineInstr
*AArch64InstructionSelector::emitFMovForFConstant(
3325 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
3326 assert(I
.getOpcode() == TargetOpcode::G_FCONSTANT
&&
3327 "Expected a G_FCONSTANT!");
3328 MachineOperand
&ImmOp
= I
.getOperand(1);
3329 unsigned DefSize
= MRI
.getType(I
.getOperand(0).getReg()).getSizeInBits();
3331 // Only handle 32 and 64 bit defs for now.
3332 if (DefSize
!= 32 && DefSize
!= 64)
3335 // Don't handle null values using FMOV.
3336 if (ImmOp
.getFPImm()->isNullValue())
3339 // Get the immediate representation for the FMOV.
3340 const APFloat
&ImmValAPF
= ImmOp
.getFPImm()->getValueAPF();
3341 int Imm
= DefSize
== 32 ? AArch64_AM::getFP32Imm(ImmValAPF
)
3342 : AArch64_AM::getFP64Imm(ImmValAPF
);
3344 // If this is -1, it means the immediate can't be represented as the requested
3345 // floating point value. Bail.
3349 // Update MI to represent the new FMOV instruction, constrain it, and return.
3350 ImmOp
.ChangeToImmediate(Imm
);
3351 unsigned MovOpc
= DefSize
== 32 ? AArch64::FMOVSi
: AArch64::FMOVDi
;
3352 I
.setDesc(TII
.get(MovOpc
));
3353 constrainSelectedInstRegOperands(I
, TII
, TRI
, RBI
);
3358 AArch64InstructionSelector::emitCSetForICMP(Register DefReg
, unsigned Pred
,
3359 MachineIRBuilder
&MIRBuilder
) const {
3360 // CSINC increments the result when the predicate is false. Invert it.
3361 const AArch64CC::CondCode InvCC
= changeICMPPredToAArch64CC(
3362 CmpInst::getInversePredicate((CmpInst::Predicate
)Pred
));
3365 .buildInstr(AArch64::CSINCWr
, {DefReg
}, {Register(AArch64::WZR
), Register(AArch64::WZR
)})
3367 constrainSelectedInstRegOperands(*I
, TII
, TRI
, RBI
);
3371 bool AArch64InstructionSelector::tryOptSelect(MachineInstr
&I
) const {
3372 MachineIRBuilder
MIB(I
);
3373 MachineRegisterInfo
&MRI
= *MIB
.getMRI();
3374 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
3376 // We want to recognize this pattern:
3378 // $z = G_FCMP pred, $x, $y
3380 // $w = G_SELECT $z, $a, $b
3382 // Where the value of $z is *only* ever used by the G_SELECT (possibly with
3383 // some copies/truncs in between.)
3385 // If we see this, then we can emit something like this:
3388 // fcsel $w, $a, $b, pred
3390 // Rather than emitting both of the rather long sequences in the standard
3391 // G_FCMP/G_SELECT select methods.
3393 // First, check if the condition is defined by a compare.
3394 MachineInstr
*CondDef
= MRI
.getVRegDef(I
.getOperand(1).getReg());
3396 // We can only fold if all of the defs have one use.
3397 if (!MRI
.hasOneUse(CondDef
->getOperand(0).getReg()))
3400 // We can skip over G_TRUNC since the condition is 1-bit.
3401 // Truncating/extending can have no impact on the value.
3402 unsigned Opc
= CondDef
->getOpcode();
3403 if (Opc
!= TargetOpcode::COPY
&& Opc
!= TargetOpcode::G_TRUNC
)
3406 // Can't see past copies from physregs.
3407 if (Opc
== TargetOpcode::COPY
&&
3408 Register::isPhysicalRegister(CondDef
->getOperand(1).getReg()))
3411 CondDef
= MRI
.getVRegDef(CondDef
->getOperand(1).getReg());
3414 // Is the condition defined by a compare?
3418 unsigned CondOpc
= CondDef
->getOpcode();
3419 if (CondOpc
!= TargetOpcode::G_ICMP
&& CondOpc
!= TargetOpcode::G_FCMP
)
3422 AArch64CC::CondCode CondCode
;
3423 if (CondOpc
== TargetOpcode::G_ICMP
) {
3424 CondCode
= changeICMPPredToAArch64CC(
3425 (CmpInst::Predicate
)CondDef
->getOperand(1).getPredicate());
3426 if (!emitIntegerCompare(CondDef
->getOperand(2), CondDef
->getOperand(3),
3427 CondDef
->getOperand(1), MIB
)) {
3428 LLVM_DEBUG(dbgs() << "Couldn't emit compare for select!\n");
3432 // Get the condition code for the select.
3433 AArch64CC::CondCode CondCode2
;
3434 changeFCMPPredToAArch64CC(
3435 (CmpInst::Predicate
)CondDef
->getOperand(1).getPredicate(), CondCode
,
3438 // changeFCMPPredToAArch64CC sets CondCode2 to AL when we require two
3439 // instructions to emit the comparison.
3440 // TODO: Handle FCMP_UEQ and FCMP_ONE. After that, this check will be
3442 if (CondCode2
!= AArch64CC::AL
)
3445 // Make sure we'll be able to select the compare.
3446 unsigned CmpOpc
= selectFCMPOpc(*CondDef
, MRI
);
3450 // Emit a new compare.
3451 auto Cmp
= MIB
.buildInstr(CmpOpc
, {}, {CondDef
->getOperand(2).getReg()});
3452 if (CmpOpc
!= AArch64::FCMPSri
&& CmpOpc
!= AArch64::FCMPDri
)
3453 Cmp
.addUse(CondDef
->getOperand(3).getReg());
3454 constrainSelectedInstRegOperands(*Cmp
, TII
, TRI
, RBI
);
3458 unsigned CSelOpc
= selectSelectOpc(I
, MRI
, RBI
);
3460 MIB
.buildInstr(CSelOpc
, {I
.getOperand(0).getReg()},
3461 {I
.getOperand(2).getReg(), I
.getOperand(3).getReg()})
3463 constrainSelectedInstRegOperands(*CSel
, TII
, TRI
, RBI
);
3464 I
.eraseFromParent();
3468 MachineInstr
*AArch64InstructionSelector::tryFoldIntegerCompare(
3469 MachineOperand
&LHS
, MachineOperand
&RHS
, MachineOperand
&Predicate
,
3470 MachineIRBuilder
&MIRBuilder
) const {
3471 assert(LHS
.isReg() && RHS
.isReg() && Predicate
.isPredicate() &&
3472 "Unexpected MachineOperand");
3473 MachineRegisterInfo
&MRI
= *MIRBuilder
.getMRI();
3474 // We want to find this sort of thing:
3478 // In this case, we can fold the G_SUB into the G_ICMP using a CMN instead.
3483 // Helper lambda to detect the subtract followed by the compare.
3484 // Takes in the def of the LHS or RHS, and checks if it's a subtract from 0.
3485 auto IsCMN
= [&](MachineInstr
*DefMI
, const AArch64CC::CondCode
&CC
) {
3486 if (!DefMI
|| DefMI
->getOpcode() != TargetOpcode::G_SUB
)
3489 // Need to make sure NZCV is the same at the end of the transformation.
3490 if (CC
!= AArch64CC::EQ
&& CC
!= AArch64CC::NE
)
3493 // We want to match against SUBs.
3494 if (DefMI
->getOpcode() != TargetOpcode::G_SUB
)
3497 // Make sure that we're getting
3500 getConstantVRegValWithLookThrough(DefMI
->getOperand(1).getReg(), MRI
);
3501 if (!ValAndVReg
|| ValAndVReg
->Value
!= 0)
3504 // This can safely be represented as a CMN.
3508 // Check if the RHS or LHS of the G_ICMP is defined by a SUB
3509 MachineInstr
*LHSDef
= getDefIgnoringCopies(LHS
.getReg(), MRI
);
3510 MachineInstr
*RHSDef
= getDefIgnoringCopies(RHS
.getReg(), MRI
);
3511 CmpInst::Predicate P
= (CmpInst::Predicate
)Predicate
.getPredicate();
3512 const AArch64CC::CondCode CC
= changeICMPPredToAArch64CC(P
);
3522 if (IsCMN(LHSDef
, CC
))
3523 return emitCMN(LHSDef
->getOperand(2), RHS
, MIRBuilder
);
3525 // Same idea here, but with the RHS of the compare instead:
3535 if (IsCMN(RHSDef
, CC
))
3536 return emitCMN(LHS
, RHSDef
->getOperand(2), MIRBuilder
);
3543 // Produce this if the compare is signed:
3546 if (!isUnsignedICMPPred(P
) && LHSDef
&&
3547 LHSDef
->getOpcode() == TargetOpcode::G_AND
) {
3548 // Make sure that the RHS is 0.
3549 auto ValAndVReg
= getConstantVRegValWithLookThrough(RHS
.getReg(), MRI
);
3550 if (!ValAndVReg
|| ValAndVReg
->Value
!= 0)
3553 return emitTST(LHSDef
->getOperand(1).getReg(),
3554 LHSDef
->getOperand(2).getReg(), MIRBuilder
);
3560 bool AArch64InstructionSelector::tryOptVectorDup(MachineInstr
&I
) const {
3561 // Try to match a vector splat operation into a dup instruction.
3562 // We're looking for this pattern:
3563 // %scalar:gpr(s64) = COPY $x0
3564 // %undef:fpr(<2 x s64>) = G_IMPLICIT_DEF
3565 // %cst0:gpr(s32) = G_CONSTANT i32 0
3566 // %zerovec:fpr(<2 x s32>) = G_BUILD_VECTOR %cst0(s32), %cst0(s32)
3567 // %ins:fpr(<2 x s64>) = G_INSERT_VECTOR_ELT %undef, %scalar(s64), %cst0(s32)
3568 // %splat:fpr(<2 x s64>) = G_SHUFFLE_VECTOR %ins(<2 x s64>), %undef,
3569 // %zerovec(<2 x s32>)
3572 // %splat = DUP %scalar
3573 // We use the regbank of the scalar to determine which kind of dup to use.
3574 MachineIRBuilder
MIB(I
);
3575 MachineRegisterInfo
&MRI
= *MIB
.getMRI();
3576 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
3577 using namespace TargetOpcode
;
3578 using namespace MIPatternMatch
;
3580 // Begin matching the insert.
3582 getOpcodeDef(G_INSERT_VECTOR_ELT
, I
.getOperand(1).getReg(), MRI
);
3585 // Match the undef vector operand.
3587 getOpcodeDef(G_IMPLICIT_DEF
, InsMI
->getOperand(1).getReg(), MRI
);
3590 // Match the scalar being splatted.
3591 Register ScalarReg
= InsMI
->getOperand(2).getReg();
3592 const RegisterBank
*ScalarRB
= RBI
.getRegBank(ScalarReg
, MRI
, TRI
);
3593 // Match the index constant 0.
3595 if (!mi_match(InsMI
->getOperand(3).getReg(), MRI
, m_ICst(Index
)) || Index
)
3598 // The shuffle's second operand doesn't matter if the mask is all zero.
3599 const Constant
*Mask
= I
.getOperand(3).getShuffleMask();
3600 if (!isa
<ConstantAggregateZero
>(Mask
))
3603 // We're done, now find out what kind of splat we need.
3604 LLT VecTy
= MRI
.getType(I
.getOperand(0).getReg());
3605 LLT EltTy
= VecTy
.getElementType();
3606 if (VecTy
.getSizeInBits() != 128 || EltTy
.getSizeInBits() < 32) {
3607 LLVM_DEBUG(dbgs() << "Could not optimize splat pattern < 128b yet");
3610 bool IsFP
= ScalarRB
->getID() == AArch64::FPRRegBankID
;
3611 static const unsigned OpcTable
[2][2] = {
3612 {AArch64::DUPv4i32gpr
, AArch64::DUPv2i64gpr
},
3613 {AArch64::DUPv4i32lane
, AArch64::DUPv2i64lane
}};
3614 unsigned Opc
= OpcTable
[IsFP
][EltTy
.getSizeInBits() == 64];
3616 // For FP splats, we need to widen the scalar reg via undef too.
3618 MachineInstr
*Widen
= emitScalarToVector(
3619 EltTy
.getSizeInBits(), &AArch64::FPR128RegClass
, ScalarReg
, MIB
);
3622 ScalarReg
= Widen
->getOperand(0).getReg();
3624 auto Dup
= MIB
.buildInstr(Opc
, {I
.getOperand(0).getReg()}, {ScalarReg
});
3627 constrainSelectedInstRegOperands(*Dup
, TII
, TRI
, RBI
);
3628 I
.eraseFromParent();
3632 bool AArch64InstructionSelector::tryOptVectorShuffle(MachineInstr
&I
) const {
3633 if (TM
.getOptLevel() == CodeGenOpt::None
)
3635 if (tryOptVectorDup(I
))
3640 bool AArch64InstructionSelector::selectShuffleVector(
3641 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
3642 if (tryOptVectorShuffle(I
))
3644 const LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg());
3645 Register Src1Reg
= I
.getOperand(1).getReg();
3646 const LLT Src1Ty
= MRI
.getType(Src1Reg
);
3647 Register Src2Reg
= I
.getOperand(2).getReg();
3648 const LLT Src2Ty
= MRI
.getType(Src2Reg
);
3649 const Constant
*ShuffleMask
= I
.getOperand(3).getShuffleMask();
3651 MachineBasicBlock
&MBB
= *I
.getParent();
3652 MachineFunction
&MF
= *MBB
.getParent();
3653 LLVMContext
&Ctx
= MF
.getFunction().getContext();
3655 SmallVector
<int, 8> Mask
;
3656 ShuffleVectorInst::getShuffleMask(ShuffleMask
, Mask
);
3658 // G_SHUFFLE_VECTOR is weird in that the source operands can be scalars, if
3659 // it's originated from a <1 x T> type. Those should have been lowered into
3660 // G_BUILD_VECTOR earlier.
3661 if (!Src1Ty
.isVector() || !Src2Ty
.isVector()) {
3662 LLVM_DEBUG(dbgs() << "Could not select a \"scalar\" G_SHUFFLE_VECTOR\n");
3666 unsigned BytesPerElt
= DstTy
.getElementType().getSizeInBits() / 8;
3668 SmallVector
<Constant
*, 64> CstIdxs
;
3669 for (int Val
: Mask
) {
3670 // For now, any undef indexes we'll just assume to be 0. This should be
3671 // optimized in future, e.g. to select DUP etc.
3672 Val
= Val
< 0 ? 0 : Val
;
3673 for (unsigned Byte
= 0; Byte
< BytesPerElt
; ++Byte
) {
3674 unsigned Offset
= Byte
+ Val
* BytesPerElt
;
3675 CstIdxs
.emplace_back(ConstantInt::get(Type::getInt8Ty(Ctx
), Offset
));
3679 MachineIRBuilder
MIRBuilder(I
);
3681 // Use a constant pool to load the index vector for TBL.
3682 Constant
*CPVal
= ConstantVector::get(CstIdxs
);
3683 MachineInstr
*IndexLoad
= emitLoadFromConstantPool(CPVal
, MIRBuilder
);
3685 LLVM_DEBUG(dbgs() << "Could not load from a constant pool");
3689 if (DstTy
.getSizeInBits() != 128) {
3690 assert(DstTy
.getSizeInBits() == 64 && "Unexpected shuffle result ty");
3691 // This case can be done with TBL1.
3692 MachineInstr
*Concat
= emitVectorConcat(None
, Src1Reg
, Src2Reg
, MIRBuilder
);
3694 LLVM_DEBUG(dbgs() << "Could not do vector concat for tbl1");
3698 // The constant pool load will be 64 bits, so need to convert to FPR128 reg.
3700 emitScalarToVector(64, &AArch64::FPR128RegClass
,
3701 IndexLoad
->getOperand(0).getReg(), MIRBuilder
);
3703 auto TBL1
= MIRBuilder
.buildInstr(
3704 AArch64::TBLv16i8One
, {&AArch64::FPR128RegClass
},
3705 {Concat
->getOperand(0).getReg(), IndexLoad
->getOperand(0).getReg()});
3706 constrainSelectedInstRegOperands(*TBL1
, TII
, TRI
, RBI
);
3710 .buildInstr(TargetOpcode::COPY
, {I
.getOperand(0).getReg()}, {})
3711 .addReg(TBL1
.getReg(0), 0, AArch64::dsub
);
3712 RBI
.constrainGenericRegister(Copy
.getReg(0), AArch64::FPR64RegClass
, MRI
);
3713 I
.eraseFromParent();
3717 // For TBL2 we need to emit a REG_SEQUENCE to tie together two consecutive
3718 // Q registers for regalloc.
3719 auto RegSeq
= MIRBuilder
3720 .buildInstr(TargetOpcode::REG_SEQUENCE
,
3721 {&AArch64::QQRegClass
}, {Src1Reg
})
3722 .addImm(AArch64::qsub0
)
3724 .addImm(AArch64::qsub1
);
3727 MIRBuilder
.buildInstr(AArch64::TBLv16i8Two
, {I
.getOperand(0).getReg()},
3728 {RegSeq
, IndexLoad
->getOperand(0).getReg()});
3729 constrainSelectedInstRegOperands(*RegSeq
, TII
, TRI
, RBI
);
3730 constrainSelectedInstRegOperands(*TBL2
, TII
, TRI
, RBI
);
3731 I
.eraseFromParent();
3735 MachineInstr
*AArch64InstructionSelector::emitLaneInsert(
3736 Optional
<Register
> DstReg
, Register SrcReg
, Register EltReg
,
3737 unsigned LaneIdx
, const RegisterBank
&RB
,
3738 MachineIRBuilder
&MIRBuilder
) const {
3739 MachineInstr
*InsElt
= nullptr;
3740 const TargetRegisterClass
*DstRC
= &AArch64::FPR128RegClass
;
3741 MachineRegisterInfo
&MRI
= *MIRBuilder
.getMRI();
3743 // Create a register to define with the insert if one wasn't passed in.
3745 DstReg
= MRI
.createVirtualRegister(DstRC
);
3747 unsigned EltSize
= MRI
.getType(EltReg
).getSizeInBits();
3748 unsigned Opc
= getInsertVecEltOpInfo(RB
, EltSize
).first
;
3750 if (RB
.getID() == AArch64::FPRRegBankID
) {
3751 auto InsSub
= emitScalarToVector(EltSize
, DstRC
, EltReg
, MIRBuilder
);
3752 InsElt
= MIRBuilder
.buildInstr(Opc
, {*DstReg
}, {SrcReg
})
3754 .addUse(InsSub
->getOperand(0).getReg())
3757 InsElt
= MIRBuilder
.buildInstr(Opc
, {*DstReg
}, {SrcReg
})
3762 constrainSelectedInstRegOperands(*InsElt
, TII
, TRI
, RBI
);
3766 bool AArch64InstructionSelector::selectInsertElt(
3767 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
3768 assert(I
.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT
);
3770 // Get information on the destination.
3771 Register DstReg
= I
.getOperand(0).getReg();
3772 const LLT DstTy
= MRI
.getType(DstReg
);
3773 unsigned VecSize
= DstTy
.getSizeInBits();
3775 // Get information on the element we want to insert into the destination.
3776 Register EltReg
= I
.getOperand(2).getReg();
3777 const LLT EltTy
= MRI
.getType(EltReg
);
3778 unsigned EltSize
= EltTy
.getSizeInBits();
3779 if (EltSize
< 16 || EltSize
> 64)
3780 return false; // Don't support all element types yet.
3782 // Find the definition of the index. Bail out if it's not defined by a
3784 Register IdxReg
= I
.getOperand(3).getReg();
3785 auto VRegAndVal
= getConstantVRegValWithLookThrough(IdxReg
, MRI
);
3788 unsigned LaneIdx
= VRegAndVal
->Value
;
3790 // Perform the lane insert.
3791 Register SrcReg
= I
.getOperand(1).getReg();
3792 const RegisterBank
&EltRB
= *RBI
.getRegBank(EltReg
, MRI
, TRI
);
3793 MachineIRBuilder
MIRBuilder(I
);
3795 if (VecSize
< 128) {
3796 // If the vector we're inserting into is smaller than 128 bits, widen it
3797 // to 128 to do the insert.
3798 MachineInstr
*ScalarToVec
= emitScalarToVector(
3799 VecSize
, &AArch64::FPR128RegClass
, SrcReg
, MIRBuilder
);
3802 SrcReg
= ScalarToVec
->getOperand(0).getReg();
3805 // Create an insert into a new FPR128 register.
3806 // Note that if our vector is already 128 bits, we end up emitting an extra
3808 MachineInstr
*InsMI
=
3809 emitLaneInsert(None
, SrcReg
, EltReg
, LaneIdx
, EltRB
, MIRBuilder
);
3811 if (VecSize
< 128) {
3812 // If we had to widen to perform the insert, then we have to demote back to
3813 // the original size to get the result we want.
3814 Register DemoteVec
= InsMI
->getOperand(0).getReg();
3815 const TargetRegisterClass
*RC
=
3816 getMinClassForRegBank(*RBI
.getRegBank(DemoteVec
, MRI
, TRI
), VecSize
);
3817 if (RC
!= &AArch64::FPR32RegClass
&& RC
!= &AArch64::FPR64RegClass
) {
3818 LLVM_DEBUG(dbgs() << "Unsupported register class!\n");
3821 unsigned SubReg
= 0;
3822 if (!getSubRegForClass(RC
, TRI
, SubReg
))
3824 if (SubReg
!= AArch64::ssub
&& SubReg
!= AArch64::dsub
) {
3825 LLVM_DEBUG(dbgs() << "Unsupported destination size! (" << VecSize
3829 MIRBuilder
.buildInstr(TargetOpcode::COPY
, {DstReg
}, {})
3830 .addReg(DemoteVec
, 0, SubReg
);
3831 RBI
.constrainGenericRegister(DstReg
, *RC
, MRI
);
3833 // No widening needed.
3834 InsMI
->getOperand(0).setReg(DstReg
);
3835 constrainSelectedInstRegOperands(*InsMI
, TII
, TRI
, RBI
);
3838 I
.eraseFromParent();
3842 bool AArch64InstructionSelector::selectBuildVector(
3843 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
3844 assert(I
.getOpcode() == TargetOpcode::G_BUILD_VECTOR
);
3845 // Until we port more of the optimized selections, for now just use a vector
3847 const LLT DstTy
= MRI
.getType(I
.getOperand(0).getReg());
3848 const LLT EltTy
= MRI
.getType(I
.getOperand(1).getReg());
3849 unsigned EltSize
= EltTy
.getSizeInBits();
3850 if (EltSize
< 16 || EltSize
> 64)
3851 return false; // Don't support all element types yet.
3852 const RegisterBank
&RB
= *RBI
.getRegBank(I
.getOperand(1).getReg(), MRI
, TRI
);
3853 MachineIRBuilder
MIRBuilder(I
);
3855 const TargetRegisterClass
*DstRC
= &AArch64::FPR128RegClass
;
3856 MachineInstr
*ScalarToVec
=
3857 emitScalarToVector(DstTy
.getElementType().getSizeInBits(), DstRC
,
3858 I
.getOperand(1).getReg(), MIRBuilder
);
3862 Register DstVec
= ScalarToVec
->getOperand(0).getReg();
3863 unsigned DstSize
= DstTy
.getSizeInBits();
3865 // Keep track of the last MI we inserted. Later on, we might be able to save
3867 MachineInstr
*PrevMI
= nullptr;
3868 for (unsigned i
= 2, e
= DstSize
/ EltSize
+ 1; i
< e
; ++i
) {
3869 // Note that if we don't do a subregister copy, we can end up making an
3871 PrevMI
= &*emitLaneInsert(None
, DstVec
, I
.getOperand(i
).getReg(), i
- 1, RB
,
3873 DstVec
= PrevMI
->getOperand(0).getReg();
3876 // If DstTy's size in bits is less than 128, then emit a subregister copy
3877 // from DstVec to the last register we've defined.
3878 if (DstSize
< 128) {
3879 // Force this to be FPR using the destination vector.
3880 const TargetRegisterClass
*RC
=
3881 getMinClassForRegBank(*RBI
.getRegBank(DstVec
, MRI
, TRI
), DstSize
);
3884 if (RC
!= &AArch64::FPR32RegClass
&& RC
!= &AArch64::FPR64RegClass
) {
3885 LLVM_DEBUG(dbgs() << "Unsupported register class!\n");
3889 unsigned SubReg
= 0;
3890 if (!getSubRegForClass(RC
, TRI
, SubReg
))
3892 if (SubReg
!= AArch64::ssub
&& SubReg
!= AArch64::dsub
) {
3893 LLVM_DEBUG(dbgs() << "Unsupported destination size! (" << DstSize
3898 Register Reg
= MRI
.createVirtualRegister(RC
);
3899 Register DstReg
= I
.getOperand(0).getReg();
3901 MIRBuilder
.buildInstr(TargetOpcode::COPY
, {DstReg
}, {})
3902 .addReg(DstVec
, 0, SubReg
);
3903 MachineOperand
&RegOp
= I
.getOperand(1);
3905 RBI
.constrainGenericRegister(DstReg
, *RC
, MRI
);
3907 // We don't need a subregister copy. Save a copy by re-using the
3908 // destination register on the final insert.
3909 assert(PrevMI
&& "PrevMI was null?");
3910 PrevMI
->getOperand(0).setReg(I
.getOperand(0).getReg());
3911 constrainSelectedInstRegOperands(*PrevMI
, TII
, TRI
, RBI
);
3914 I
.eraseFromParent();
3918 /// Helper function to find an intrinsic ID on an a MachineInstr. Returns the
3919 /// ID if it exists, and 0 otherwise.
3920 static unsigned findIntrinsicID(MachineInstr
&I
) {
3921 auto IntrinOp
= find_if(I
.operands(), [&](const MachineOperand
&Op
) {
3922 return Op
.isIntrinsicID();
3924 if (IntrinOp
== I
.operands_end())
3926 return IntrinOp
->getIntrinsicID();
3929 bool AArch64InstructionSelector::selectIntrinsicWithSideEffects(
3930 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
3931 // Find the intrinsic ID.
3932 unsigned IntrinID
= findIntrinsicID(I
);
3935 MachineIRBuilder
MIRBuilder(I
);
3937 // Select the instruction.
3941 case Intrinsic::trap
:
3942 MIRBuilder
.buildInstr(AArch64::BRK
, {}, {}).addImm(1);
3944 case Intrinsic::debugtrap
:
3945 if (!STI
.isTargetWindows())
3947 MIRBuilder
.buildInstr(AArch64::BRK
, {}, {}).addImm(0xF000);
3951 I
.eraseFromParent();
3955 bool AArch64InstructionSelector::selectIntrinsic(
3956 MachineInstr
&I
, MachineRegisterInfo
&MRI
) const {
3957 unsigned IntrinID
= findIntrinsicID(I
);
3960 MachineIRBuilder
MIRBuilder(I
);
3965 case Intrinsic::aarch64_crypto_sha1h
:
3966 Register DstReg
= I
.getOperand(0).getReg();
3967 Register SrcReg
= I
.getOperand(2).getReg();
3969 // FIXME: Should this be an assert?
3970 if (MRI
.getType(DstReg
).getSizeInBits() != 32 ||
3971 MRI
.getType(SrcReg
).getSizeInBits() != 32)
3974 // The operation has to happen on FPRs. Set up some new FPR registers for
3975 // the source and destination if they are on GPRs.
3976 if (RBI
.getRegBank(SrcReg
, MRI
, TRI
)->getID() != AArch64::FPRRegBankID
) {
3977 SrcReg
= MRI
.createVirtualRegister(&AArch64::FPR32RegClass
);
3978 MIRBuilder
.buildCopy({SrcReg
}, {I
.getOperand(2)});
3980 // Make sure the copy ends up getting constrained properly.
3981 RBI
.constrainGenericRegister(I
.getOperand(2).getReg(),
3982 AArch64::GPR32RegClass
, MRI
);
3985 if (RBI
.getRegBank(DstReg
, MRI
, TRI
)->getID() != AArch64::FPRRegBankID
)
3986 DstReg
= MRI
.createVirtualRegister(&AArch64::FPR32RegClass
);
3988 // Actually insert the instruction.
3989 auto SHA1Inst
= MIRBuilder
.buildInstr(AArch64::SHA1Hrr
, {DstReg
}, {SrcReg
});
3990 constrainSelectedInstRegOperands(*SHA1Inst
, TII
, TRI
, RBI
);
3992 // Did we create a new register for the destination?
3993 if (DstReg
!= I
.getOperand(0).getReg()) {
3994 // Yep. Copy the result of the instruction back into the original
3996 MIRBuilder
.buildCopy({I
.getOperand(0)}, {DstReg
});
3997 RBI
.constrainGenericRegister(I
.getOperand(0).getReg(),
3998 AArch64::GPR32RegClass
, MRI
);
4001 I
.eraseFromParent();
4007 static Optional
<uint64_t> getImmedFromMO(const MachineOperand
&Root
) {
4008 auto &MI
= *Root
.getParent();
4009 auto &MBB
= *MI
.getParent();
4010 auto &MF
= *MBB
.getParent();
4011 auto &MRI
= MF
.getRegInfo();
4014 Immed
= Root
.getImm();
4015 else if (Root
.isCImm())
4016 Immed
= Root
.getCImm()->getZExtValue();
4017 else if (Root
.isReg()) {
4019 getConstantVRegValWithLookThrough(Root
.getReg(), MRI
, true);
4022 Immed
= ValAndVReg
->Value
;
4028 InstructionSelector::ComplexRendererFns
4029 AArch64InstructionSelector::selectShiftA_32(const MachineOperand
&Root
) const {
4030 auto MaybeImmed
= getImmedFromMO(Root
);
4031 if (MaybeImmed
== None
|| *MaybeImmed
> 31)
4033 uint64_t Enc
= (32 - *MaybeImmed
) & 0x1f;
4034 return {{[=](MachineInstrBuilder
&MIB
) { MIB
.addImm(Enc
); }}};
4037 InstructionSelector::ComplexRendererFns
4038 AArch64InstructionSelector::selectShiftB_32(const MachineOperand
&Root
) const {
4039 auto MaybeImmed
= getImmedFromMO(Root
);
4040 if (MaybeImmed
== None
|| *MaybeImmed
> 31)
4042 uint64_t Enc
= 31 - *MaybeImmed
;
4043 return {{[=](MachineInstrBuilder
&MIB
) { MIB
.addImm(Enc
); }}};
4046 InstructionSelector::ComplexRendererFns
4047 AArch64InstructionSelector::selectShiftA_64(const MachineOperand
&Root
) const {
4048 auto MaybeImmed
= getImmedFromMO(Root
);
4049 if (MaybeImmed
== None
|| *MaybeImmed
> 63)
4051 uint64_t Enc
= (64 - *MaybeImmed
) & 0x3f;
4052 return {{[=](MachineInstrBuilder
&MIB
) { MIB
.addImm(Enc
); }}};
4055 InstructionSelector::ComplexRendererFns
4056 AArch64InstructionSelector::selectShiftB_64(const MachineOperand
&Root
) const {
4057 auto MaybeImmed
= getImmedFromMO(Root
);
4058 if (MaybeImmed
== None
|| *MaybeImmed
> 63)
4060 uint64_t Enc
= 63 - *MaybeImmed
;
4061 return {{[=](MachineInstrBuilder
&MIB
) { MIB
.addImm(Enc
); }}};
4064 /// Helper to select an immediate value that can be represented as a 12-bit
4065 /// value shifted left by either 0 or 12. If it is possible to do so, return
4066 /// the immediate and shift value. If not, return None.
4068 /// Used by selectArithImmed and selectNegArithImmed.
4069 InstructionSelector::ComplexRendererFns
4070 AArch64InstructionSelector::select12BitValueWithLeftShift(
4071 uint64_t Immed
) const {
4073 if (Immed
>> 12 == 0) {
4075 } else if ((Immed
& 0xfff) == 0 && Immed
>> 24 == 0) {
4077 Immed
= Immed
>> 12;
4081 unsigned ShVal
= AArch64_AM::getShifterImm(AArch64_AM::LSL
, ShiftAmt
);
4083 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(Immed
); },
4084 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(ShVal
); },
4088 /// SelectArithImmed - Select an immediate value that can be represented as
4089 /// a 12-bit value shifted left by either 0 or 12. If so, return true with
4090 /// Val set to the 12-bit value and Shift set to the shifter operand.
4091 InstructionSelector::ComplexRendererFns
4092 AArch64InstructionSelector::selectArithImmed(MachineOperand
&Root
) const {
4093 // This function is called from the addsub_shifted_imm ComplexPattern,
4094 // which lists [imm] as the list of opcode it's interested in, however
4095 // we still need to check whether the operand is actually an immediate
4096 // here because the ComplexPattern opcode list is only used in
4097 // root-level opcode matching.
4098 auto MaybeImmed
= getImmedFromMO(Root
);
4099 if (MaybeImmed
== None
)
4101 return select12BitValueWithLeftShift(*MaybeImmed
);
4104 /// SelectNegArithImmed - As above, but negates the value before trying to
4106 InstructionSelector::ComplexRendererFns
4107 AArch64InstructionSelector::selectNegArithImmed(MachineOperand
&Root
) const {
4108 // We need a register here, because we need to know if we have a 64 or 32
4112 auto MaybeImmed
= getImmedFromMO(Root
);
4113 if (MaybeImmed
== None
)
4115 uint64_t Immed
= *MaybeImmed
;
4117 // This negation is almost always valid, but "cmp wN, #0" and "cmn wN, #0"
4118 // have the opposite effect on the C flag, so this pattern mustn't match under
4119 // those circumstances.
4123 // Check if we're dealing with a 32-bit type on the root or a 64-bit type on
4125 MachineRegisterInfo
&MRI
= Root
.getParent()->getMF()->getRegInfo();
4126 if (MRI
.getType(Root
.getReg()).getSizeInBits() == 32)
4127 Immed
= ~((uint32_t)Immed
) + 1;
4129 Immed
= ~Immed
+ 1ULL;
4131 if (Immed
& 0xFFFFFFFFFF000000ULL
)
4134 Immed
&= 0xFFFFFFULL
;
4135 return select12BitValueWithLeftShift(Immed
);
4138 /// Return true if it is worth folding MI into an extended register. That is,
4139 /// if it's safe to pull it into the addressing mode of a load or store as a
4141 bool AArch64InstructionSelector::isWorthFoldingIntoExtendedReg(
4142 MachineInstr
&MI
, const MachineRegisterInfo
&MRI
) const {
4143 // Always fold if there is one use, or if we're optimizing for size.
4144 Register DefReg
= MI
.getOperand(0).getReg();
4145 if (MRI
.hasOneUse(DefReg
) ||
4146 MI
.getParent()->getParent()->getFunction().hasMinSize())
4149 // It's better to avoid folding and recomputing shifts when we don't have a
4151 if (!STI
.hasLSLFast())
4154 // We have a fastpath, so folding a shift in and potentially computing it
4155 // many times may be beneficial. Check if this is only used in memory ops.
4156 // If it is, then we should fold.
4157 return all_of(MRI
.use_instructions(DefReg
),
4158 [](MachineInstr
&Use
) { return Use
.mayLoadOrStore(); });
4161 /// This is used for computing addresses like this:
4163 /// ldr x1, [x2, x3, lsl #3]
4165 /// Where x2 is the base register, and x3 is an offset register. The shift-left
4166 /// is a constant value specific to this load instruction. That is, we'll never
4167 /// see anything other than a 3 here (which corresponds to the size of the
4168 /// element being loaded.)
4169 InstructionSelector::ComplexRendererFns
4170 AArch64InstructionSelector::selectAddrModeShiftedExtendXReg(
4171 MachineOperand
&Root
, unsigned SizeInBytes
) const {
4174 MachineRegisterInfo
&MRI
= Root
.getParent()->getMF()->getRegInfo();
4176 // Make sure that the memory op is a valid size.
4177 int64_t LegalShiftVal
= Log2_32(SizeInBytes
);
4178 if (LegalShiftVal
== 0)
4181 // We want to find something like this:
4183 // val = G_CONSTANT LegalShiftVal
4184 // shift = G_SHL off_reg val
4185 // ptr = G_GEP base_reg shift
4188 // And fold it into this addressing mode:
4190 // ldr x, [base_reg, off_reg, lsl #LegalShiftVal]
4192 // Check if we can find the G_GEP.
4193 MachineInstr
*Gep
= getOpcodeDef(TargetOpcode::G_GEP
, Root
.getReg(), MRI
);
4194 if (!Gep
|| !isWorthFoldingIntoExtendedReg(*Gep
, MRI
))
4197 // Now, try to match an opcode which will match our specific offset.
4198 // We want a G_SHL or a G_MUL.
4199 MachineInstr
*OffsetInst
= getDefIgnoringCopies(Gep
->getOperand(2).getReg(), MRI
);
4203 unsigned OffsetOpc
= OffsetInst
->getOpcode();
4204 if (OffsetOpc
!= TargetOpcode::G_SHL
&& OffsetOpc
!= TargetOpcode::G_MUL
)
4207 if (!isWorthFoldingIntoExtendedReg(*OffsetInst
, MRI
))
4210 // Now, try to find the specific G_CONSTANT. Start by assuming that the
4211 // register we will offset is the LHS, and the register containing the
4212 // constant is the RHS.
4213 Register OffsetReg
= OffsetInst
->getOperand(1).getReg();
4214 Register ConstantReg
= OffsetInst
->getOperand(2).getReg();
4215 auto ValAndVReg
= getConstantVRegValWithLookThrough(ConstantReg
, MRI
);
4217 // We didn't get a constant on the RHS. If the opcode is a shift, then
4219 if (OffsetOpc
== TargetOpcode::G_SHL
)
4222 // If we have a G_MUL, we can use either register. Try looking at the RHS.
4223 std::swap(OffsetReg
, ConstantReg
);
4224 ValAndVReg
= getConstantVRegValWithLookThrough(ConstantReg
, MRI
);
4229 // The value must fit into 3 bits, and must be positive. Make sure that is
4231 int64_t ImmVal
= ValAndVReg
->Value
;
4233 // Since we're going to pull this into a shift, the constant value must be
4234 // a power of 2. If we got a multiply, then we need to check this.
4235 if (OffsetOpc
== TargetOpcode::G_MUL
) {
4236 if (!isPowerOf2_32(ImmVal
))
4239 // Got a power of 2. So, the amount we'll shift is the log base-2 of that.
4240 ImmVal
= Log2_32(ImmVal
);
4243 if ((ImmVal
& 0x7) != ImmVal
)
4246 // We are only allowed to shift by LegalShiftVal. This shift value is built
4247 // into the instruction, so we can't just use whatever we want.
4248 if (ImmVal
!= LegalShiftVal
)
4251 // We can use the LHS of the GEP as the base, and the LHS of the shift as an
4252 // offset. Signify that we are shifting by setting the shift flag to 1.
4253 return {{[=](MachineInstrBuilder
&MIB
) {
4254 MIB
.addUse(Gep
->getOperand(1).getReg());
4256 [=](MachineInstrBuilder
&MIB
) { MIB
.addUse(OffsetReg
); },
4257 [=](MachineInstrBuilder
&MIB
) {
4258 // Need to add both immediates here to make sure that they are both
4259 // added to the instruction.
4265 /// This is used for computing addresses like this:
4267 /// ldr x1, [x2, x3]
4269 /// Where x2 is the base register, and x3 is an offset register.
4271 /// When possible (or profitable) to fold a G_GEP into the address calculation,
4272 /// this will do so. Otherwise, it will return None.
4273 InstructionSelector::ComplexRendererFns
4274 AArch64InstructionSelector::selectAddrModeRegisterOffset(
4275 MachineOperand
&Root
) const {
4276 MachineRegisterInfo
&MRI
= Root
.getParent()->getMF()->getRegInfo();
4279 MachineInstr
*Gep
= MRI
.getVRegDef(Root
.getReg());
4280 if (!Gep
|| Gep
->getOpcode() != TargetOpcode::G_GEP
)
4283 // If this is used more than once, let's not bother folding.
4284 // TODO: Check if they are memory ops. If they are, then we can still fold
4285 // without having to recompute anything.
4286 if (!MRI
.hasOneUse(Gep
->getOperand(0).getReg()))
4289 // Base is the GEP's LHS, offset is its RHS.
4290 return {{[=](MachineInstrBuilder
&MIB
) {
4291 MIB
.addUse(Gep
->getOperand(1).getReg());
4293 [=](MachineInstrBuilder
&MIB
) {
4294 MIB
.addUse(Gep
->getOperand(2).getReg());
4296 [=](MachineInstrBuilder
&MIB
) {
4297 // Need to add both immediates here to make sure that they are both
4298 // added to the instruction.
4304 /// This is intended to be equivalent to selectAddrModeXRO in
4305 /// AArch64ISelDAGtoDAG. It's used for selecting X register offset loads.
4306 InstructionSelector::ComplexRendererFns
4307 AArch64InstructionSelector::selectAddrModeXRO(MachineOperand
&Root
,
4308 unsigned SizeInBytes
) const {
4309 MachineRegisterInfo
&MRI
= Root
.getParent()->getMF()->getRegInfo();
4311 // If we have a constant offset, then we probably don't want to match a
4313 if (isBaseWithConstantOffset(Root
, MRI
))
4316 // Try to fold shifts into the addressing mode.
4317 auto AddrModeFns
= selectAddrModeShiftedExtendXReg(Root
, SizeInBytes
);
4321 // If that doesn't work, see if it's possible to fold in registers from
4323 return selectAddrModeRegisterOffset(Root
);
4326 /// Select a "register plus unscaled signed 9-bit immediate" address. This
4327 /// should only match when there is an offset that is not valid for a scaled
4328 /// immediate addressing mode. The "Size" argument is the size in bytes of the
4329 /// memory reference, which is needed here to know what is valid for a scaled
4331 InstructionSelector::ComplexRendererFns
4332 AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand
&Root
,
4333 unsigned Size
) const {
4334 MachineRegisterInfo
&MRI
=
4335 Root
.getParent()->getParent()->getParent()->getRegInfo();
4340 if (!isBaseWithConstantOffset(Root
, MRI
))
4343 MachineInstr
*RootDef
= MRI
.getVRegDef(Root
.getReg());
4347 MachineOperand
&OffImm
= RootDef
->getOperand(2);
4348 if (!OffImm
.isReg())
4350 MachineInstr
*RHS
= MRI
.getVRegDef(OffImm
.getReg());
4351 if (!RHS
|| RHS
->getOpcode() != TargetOpcode::G_CONSTANT
)
4354 MachineOperand
&RHSOp1
= RHS
->getOperand(1);
4355 if (!RHSOp1
.isCImm() || RHSOp1
.getCImm()->getBitWidth() > 64)
4357 RHSC
= RHSOp1
.getCImm()->getSExtValue();
4359 // If the offset is valid as a scaled immediate, don't match here.
4360 if ((RHSC
& (Size
- 1)) == 0 && RHSC
>= 0 && RHSC
< (0x1000 << Log2_32(Size
)))
4362 if (RHSC
>= -256 && RHSC
< 256) {
4363 MachineOperand
&Base
= RootDef
->getOperand(1);
4365 [=](MachineInstrBuilder
&MIB
) { MIB
.add(Base
); },
4366 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(RHSC
); },
4372 /// Select a "register plus scaled unsigned 12-bit immediate" address. The
4373 /// "Size" argument is the size in bytes of the memory reference, which
4374 /// determines the scale.
4375 InstructionSelector::ComplexRendererFns
4376 AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand
&Root
,
4377 unsigned Size
) const {
4378 MachineRegisterInfo
&MRI
=
4379 Root
.getParent()->getParent()->getParent()->getRegInfo();
4384 MachineInstr
*RootDef
= MRI
.getVRegDef(Root
.getReg());
4388 if (RootDef
->getOpcode() == TargetOpcode::G_FRAME_INDEX
) {
4390 [=](MachineInstrBuilder
&MIB
) { MIB
.add(RootDef
->getOperand(1)); },
4391 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(0); },
4395 if (isBaseWithConstantOffset(Root
, MRI
)) {
4396 MachineOperand
&LHS
= RootDef
->getOperand(1);
4397 MachineOperand
&RHS
= RootDef
->getOperand(2);
4398 MachineInstr
*LHSDef
= MRI
.getVRegDef(LHS
.getReg());
4399 MachineInstr
*RHSDef
= MRI
.getVRegDef(RHS
.getReg());
4400 if (LHSDef
&& RHSDef
) {
4401 int64_t RHSC
= (int64_t)RHSDef
->getOperand(1).getCImm()->getZExtValue();
4402 unsigned Scale
= Log2_32(Size
);
4403 if ((RHSC
& (Size
- 1)) == 0 && RHSC
>= 0 && RHSC
< (0x1000 << Scale
)) {
4404 if (LHSDef
->getOpcode() == TargetOpcode::G_FRAME_INDEX
)
4406 [=](MachineInstrBuilder
&MIB
) { MIB
.add(LHSDef
->getOperand(1)); },
4407 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(RHSC
>> Scale
); },
4411 [=](MachineInstrBuilder
&MIB
) { MIB
.add(LHS
); },
4412 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(RHSC
>> Scale
); },
4418 // Before falling back to our general case, check if the unscaled
4419 // instructions can handle this. If so, that's preferable.
4420 if (selectAddrModeUnscaled(Root
, Size
).hasValue())
4424 [=](MachineInstrBuilder
&MIB
) { MIB
.add(Root
); },
4425 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(0); },
4429 /// Given a shift instruction, return the correct shift type for that
4431 static AArch64_AM::ShiftExtendType
getShiftTypeForInst(MachineInstr
&MI
) {
4432 // TODO: Handle AArch64_AM::ROR
4433 switch (MI
.getOpcode()) {
4435 return AArch64_AM::InvalidShiftExtend
;
4436 case TargetOpcode::G_SHL
:
4437 return AArch64_AM::LSL
;
4438 case TargetOpcode::G_LSHR
:
4439 return AArch64_AM::LSR
;
4440 case TargetOpcode::G_ASHR
:
4441 return AArch64_AM::ASR
;
4445 /// Select a "shifted register" operand. If the value is not shifted, set the
4446 /// shift operand to a default value of "lsl 0".
4448 /// TODO: Allow shifted register to be rotated in logical instructions.
4449 InstructionSelector::ComplexRendererFns
4450 AArch64InstructionSelector::selectShiftedRegister(MachineOperand
&Root
) const {
4453 MachineRegisterInfo
&MRI
=
4454 Root
.getParent()->getParent()->getParent()->getRegInfo();
4456 // Check if the operand is defined by an instruction which corresponds to
4457 // a ShiftExtendType. E.g. a G_SHL, G_LSHR, etc.
4459 // TODO: Handle AArch64_AM::ROR for logical instructions.
4460 MachineInstr
*ShiftInst
= MRI
.getVRegDef(Root
.getReg());
4463 AArch64_AM::ShiftExtendType ShType
= getShiftTypeForInst(*ShiftInst
);
4464 if (ShType
== AArch64_AM::InvalidShiftExtend
)
4466 if (!isWorthFoldingIntoExtendedReg(*ShiftInst
, MRI
))
4469 // Need an immediate on the RHS.
4470 MachineOperand
&ShiftRHS
= ShiftInst
->getOperand(2);
4471 auto Immed
= getImmedFromMO(ShiftRHS
);
4475 // We have something that we can fold. Fold in the shift's LHS and RHS into
4477 MachineOperand
&ShiftLHS
= ShiftInst
->getOperand(1);
4478 Register ShiftReg
= ShiftLHS
.getReg();
4480 unsigned NumBits
= MRI
.getType(ShiftReg
).getSizeInBits();
4481 unsigned Val
= *Immed
& (NumBits
- 1);
4482 unsigned ShiftVal
= AArch64_AM::getShifterImm(ShType
, Val
);
4484 return {{[=](MachineInstrBuilder
&MIB
) { MIB
.addUse(ShiftReg
); },
4485 [=](MachineInstrBuilder
&MIB
) { MIB
.addImm(ShiftVal
); }}};
4488 /// Get the correct ShiftExtendType for an extend instruction.
4489 static AArch64_AM::ShiftExtendType
4490 getExtendTypeForInst(MachineInstr
&MI
, MachineRegisterInfo
&MRI
) {
4491 unsigned Opc
= MI
.getOpcode();
4493 // Handle explicit extend instructions first.
4494 if (Opc
== TargetOpcode::G_SEXT
|| Opc
== TargetOpcode::G_SEXT_INREG
) {
4495 unsigned Size
= MRI
.getType(MI
.getOperand(1).getReg()).getSizeInBits();
4496 assert(Size
!= 64 && "Extend from 64 bits?");
4499 return AArch64_AM::SXTB
;
4501 return AArch64_AM::SXTH
;
4503 return AArch64_AM::SXTW
;
4505 return AArch64_AM::InvalidShiftExtend
;
4509 if (Opc
== TargetOpcode::G_ZEXT
|| Opc
== TargetOpcode::G_ANYEXT
) {
4510 unsigned Size
= MRI
.getType(MI
.getOperand(1).getReg()).getSizeInBits();
4511 assert(Size
!= 64 && "Extend from 64 bits?");
4514 return AArch64_AM::UXTB
;
4516 return AArch64_AM::UXTH
;
4518 return AArch64_AM::UXTW
;
4520 return AArch64_AM::InvalidShiftExtend
;
4524 // Don't have an explicit extend. Try to handle a G_AND with a constant mask
4526 if (Opc
!= TargetOpcode::G_AND
)
4527 return AArch64_AM::InvalidShiftExtend
;
4529 Optional
<uint64_t> MaybeAndMask
= getImmedFromMO(MI
.getOperand(2));
4531 return AArch64_AM::InvalidShiftExtend
;
4532 uint64_t AndMask
= *MaybeAndMask
;
4535 return AArch64_AM::InvalidShiftExtend
;
4537 return AArch64_AM::UXTB
;
4539 return AArch64_AM::UXTH
;
4541 return AArch64_AM::UXTW
;
4545 Register
AArch64InstructionSelector::narrowExtendRegIfNeeded(
4546 Register ExtReg
, MachineIRBuilder
&MIB
) const {
4547 MachineRegisterInfo
&MRI
= *MIB
.getMRI();
4548 if (MRI
.getType(ExtReg
).getSizeInBits() == 32)
4551 // Insert a copy to move ExtReg to GPR32.
4552 Register NarrowReg
= MRI
.createVirtualRegister(&AArch64::GPR32RegClass
);
4553 auto Copy
= MIB
.buildCopy({NarrowReg
}, {ExtReg
});
4555 // Select the copy into a subregister copy.
4556 selectCopy(*Copy
, TII
, MRI
, TRI
, RBI
);
4557 return Copy
.getReg(0);
4560 /// Select an "extended register" operand. This operand folds in an extend
4561 /// followed by an optional left shift.
4562 InstructionSelector::ComplexRendererFns
4563 AArch64InstructionSelector::selectArithExtendedRegister(
4564 MachineOperand
&Root
) const {
4567 MachineRegisterInfo
&MRI
=
4568 Root
.getParent()->getParent()->getParent()->getRegInfo();
4570 uint64_t ShiftVal
= 0;
4572 AArch64_AM::ShiftExtendType Ext
;
4573 MachineInstr
*RootDef
= getDefIgnoringCopies(Root
.getReg(), MRI
);
4577 if (!isWorthFoldingIntoExtendedReg(*RootDef
, MRI
))
4580 // Check if we can fold a shift and an extend.
4581 if (RootDef
->getOpcode() == TargetOpcode::G_SHL
) {
4582 // Look for a constant on the RHS of the shift.
4583 MachineOperand
&RHS
= RootDef
->getOperand(2);
4584 Optional
<uint64_t> MaybeShiftVal
= getImmedFromMO(RHS
);
4587 ShiftVal
= *MaybeShiftVal
;
4590 // Look for a valid extend instruction on the LHS of the shift.
4591 MachineOperand
&LHS
= RootDef
->getOperand(1);
4592 MachineInstr
*ExtDef
= getDefIgnoringCopies(LHS
.getReg(), MRI
);
4595 Ext
= getExtendTypeForInst(*ExtDef
, MRI
);
4596 if (Ext
== AArch64_AM::InvalidShiftExtend
)
4598 ExtReg
= ExtDef
->getOperand(1).getReg();
4600 // Didn't get a shift. Try just folding an extend.
4601 Ext
= getExtendTypeForInst(*RootDef
, MRI
);
4602 if (Ext
== AArch64_AM::InvalidShiftExtend
)
4604 ExtReg
= RootDef
->getOperand(1).getReg();
4606 // If we have a 32 bit instruction which zeroes out the high half of a
4607 // register, we get an implicit zero extend for free. Check if we have one.
4608 // FIXME: We actually emit the extend right now even though we don't have
4610 if (Ext
== AArch64_AM::UXTW
&& MRI
.getType(ExtReg
).getSizeInBits() == 32) {
4611 MachineInstr
*ExtInst
= MRI
.getVRegDef(ExtReg
);
4612 if (ExtInst
&& isDef32(*ExtInst
))
4617 // We require a GPR32 here. Narrow the ExtReg if needed using a subregister
4619 MachineIRBuilder
MIB(*RootDef
);
4620 ExtReg
= narrowExtendRegIfNeeded(ExtReg
, MIB
);
4622 return {{[=](MachineInstrBuilder
&MIB
) { MIB
.addUse(ExtReg
); },
4623 [=](MachineInstrBuilder
&MIB
) {
4624 MIB
.addImm(getArithExtendImm(Ext
, ShiftVal
));
4628 void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder
&MIB
,
4629 const MachineInstr
&MI
) const {
4630 const MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
4631 assert(MI
.getOpcode() == TargetOpcode::G_CONSTANT
&& "Expected G_CONSTANT");
4632 Optional
<int64_t> CstVal
= getConstantVRegVal(MI
.getOperand(0).getReg(), MRI
);
4633 assert(CstVal
&& "Expected constant value");
4634 MIB
.addImm(CstVal
.getValue());
4637 void AArch64InstructionSelector::renderLogicalImm32(
4638 MachineInstrBuilder
&MIB
, const MachineInstr
&I
) const {
4639 assert(I
.getOpcode() == TargetOpcode::G_CONSTANT
&& "Expected G_CONSTANT");
4640 uint64_t CstVal
= I
.getOperand(1).getCImm()->getZExtValue();
4641 uint64_t Enc
= AArch64_AM::encodeLogicalImmediate(CstVal
, 32);
4645 void AArch64InstructionSelector::renderLogicalImm64(
4646 MachineInstrBuilder
&MIB
, const MachineInstr
&I
) const {
4647 assert(I
.getOpcode() == TargetOpcode::G_CONSTANT
&& "Expected G_CONSTANT");
4648 uint64_t CstVal
= I
.getOperand(1).getCImm()->getZExtValue();
4649 uint64_t Enc
= AArch64_AM::encodeLogicalImmediate(CstVal
, 64);
4653 bool AArch64InstructionSelector::isLoadStoreOfNumBytes(
4654 const MachineInstr
&MI
, unsigned NumBytes
) const {
4655 if (!MI
.mayLoadOrStore())
4657 assert(MI
.hasOneMemOperand() &&
4658 "Expected load/store to have only one mem op!");
4659 return (*MI
.memoperands_begin())->getSize() == NumBytes
;
4662 bool AArch64InstructionSelector::isDef32(const MachineInstr
&MI
) const {
4663 const MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
4664 if (MRI
.getType(MI
.getOperand(0).getReg()).getSizeInBits() != 32)
4667 // Only return true if we know the operation will zero-out the high half of
4668 // the 64-bit register. Truncates can be subregister copies, which don't
4669 // zero out the high bits. Copies and other copy-like instructions can be
4670 // fed by truncates, or could be lowered as subregister copies.
4671 switch (MI
.getOpcode()) {
4674 case TargetOpcode::COPY
:
4675 case TargetOpcode::G_BITCAST
:
4676 case TargetOpcode::G_TRUNC
:
4677 case TargetOpcode::G_PHI
:
4683 InstructionSelector
*
4684 createAArch64InstructionSelector(const AArch64TargetMachine
&TM
,
4685 AArch64Subtarget
&Subtarget
,
4686 AArch64RegisterBankInfo
&RBI
) {
4687 return new AArch64InstructionSelector(TM
, Subtarget
, RBI
);