LLVM 23.0.0git
AArch64GlobalISelUtils.cpp
Go to the documentation of this file.
1//===- AArch64GlobalISelUtils.cpp --------------------------------*- C++ -*-==//
2//
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
6//
7//===----------------------------------------------------------------------===//
8/// \file Implementations of AArch64-specific helper functions used in the
9/// GlobalISel pipeline.
10//===----------------------------------------------------------------------===//
14#include "llvm/IR/InstrTypes.h"
15
16using namespace llvm;
17
18std::optional<RegOrConstant>
20 const MachineRegisterInfo &MRI) {
21 if (auto Splat = getVectorSplat(MI, MRI))
22 return Splat;
23 if (MI.getOpcode() != AArch64::G_DUP)
24 return std::nullopt;
25 Register Src = MI.getOperand(1).getReg();
26 if (auto ValAndVReg =
27 getAnyConstantVRegValWithLookThrough(MI.getOperand(1).getReg(), MRI))
28 return RegOrConstant(ValAndVReg->Value.getSExtValue());
29 return RegOrConstant(Src);
30}
31
32std::optional<int64_t>
34 const MachineRegisterInfo &MRI) {
36 if (!Splat || Splat->isReg())
37 return std::nullopt;
38 return Splat->getCst();
39}
40
42 const CmpInst::Predicate &Pred,
43 const MachineRegisterInfo &MRI) {
44 // Match:
45 //
46 // %sub = G_SUB 0, %y
47 // %cmp = G_ICMP eq/ne, %sub, %z
48 //
49 // Or
50 //
51 // %sub = G_SUB 0, %y
52 // %cmp = G_ICMP eq/ne, %z, %sub
53 // or with signed comparisons with the no-signed-wrap flag set
54 if (!MaybeSub || MaybeSub->getOpcode() != TargetOpcode::G_SUB ||
55 (!CmpInst::isEquality(Pred) &&
56 !(CmpInst::isSigned(Pred) && MaybeSub->getFlag(MachineInstr::NoSWrap))))
57 return false;
58 auto MaybeZero =
60 return MaybeZero && MaybeZero->Value.getZExtValue() == 0;
61}
62
64 MachineIRBuilder &MIRBuilder,
65 const LibcallLoweringInfo &Libcalls,
66 bool MinSize) {
67 assert(MI.getOpcode() == TargetOpcode::G_MEMSET);
68 if (Libcalls.getLibcallImpl(RTLIB::BZERO) == RTLIB::Unsupported)
69 return false;
70
71 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
72 auto Zero =
73 getIConstantVRegValWithLookThrough(MI.getOperand(1).getReg(), MRI);
74 if (!Zero || Zero->Value.getSExtValue() != 0)
75 return false;
76
77 // It's not faster to use bzero rather than memset for sizes <= 256.
78 // However, it *does* save us a mov from wzr, so if we're going for
79 // minsize, use bzero even if it's slower.
80 if (!MinSize) {
81 // If the size is known, check it. If it is not known, assume using bzero is
82 // better.
84 MI.getOperand(2).getReg(), MRI)) {
85 if (Size->Value.getSExtValue() <= 256)
86 return false;
87 }
88 }
89
90 MIRBuilder.setInstrAndDebugLoc(MI);
91 MIRBuilder
92 .buildInstr(TargetOpcode::G_BZERO, {},
93 {MI.getOperand(0), MI.getOperand(2)})
94 .addImm(MI.getOperand(3).getImm())
95 .addMemOperand(*MI.memoperands_begin());
96 MI.eraseFromParent();
97 return true;
98}
99
100std::tuple<uint16_t, Register>
103 Register AddrDisc = Disc;
104 uint16_t ConstDisc = 0;
105
106 if (auto ConstDiscVal = getIConstantVRegVal(Disc, MRI)) {
107 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
108 ConstDisc = ConstDiscVal->getZExtValue();
109 AddrDisc = AArch64::NoRegister;
110 }
111 return std::make_tuple(ConstDisc, AddrDisc);
112 }
113
114 const MachineInstr *DiscMI = MRI.getVRegDef(Disc);
115 if (!DiscMI || DiscMI->getOpcode() != TargetOpcode::G_INTRINSIC ||
116 DiscMI->getOperand(1).getIntrinsicID() != Intrinsic::ptrauth_blend)
117 return std::make_tuple(ConstDisc, AddrDisc);
118
119 if (auto ConstDiscVal =
120 getIConstantVRegVal(DiscMI->getOperand(3).getReg(), MRI)) {
121 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
122 ConstDisc = ConstDiscVal->getZExtValue();
123 AddrDisc = DiscMI->getOperand(2).getReg();
124 }
125 }
126 return std::make_tuple(ConstDisc, AddrDisc);
127}
128
131 AArch64CC::CondCode &CondCode2) {
132 CondCode2 = AArch64CC::AL;
133 switch (P) {
134 default:
135 llvm_unreachable("Unknown FP condition!");
137 CondCode = AArch64CC::EQ;
138 break;
140 CondCode = AArch64CC::GT;
141 break;
143 CondCode = AArch64CC::GE;
144 break;
146 CondCode = AArch64CC::MI;
147 break;
149 CondCode = AArch64CC::LS;
150 break;
152 CondCode = AArch64CC::MI;
153 CondCode2 = AArch64CC::GT;
154 break;
156 CondCode = AArch64CC::VC;
157 break;
159 CondCode = AArch64CC::VS;
160 break;
162 CondCode = AArch64CC::EQ;
163 CondCode2 = AArch64CC::VS;
164 break;
166 CondCode = AArch64CC::HI;
167 break;
169 CondCode = AArch64CC::PL;
170 break;
172 CondCode = AArch64CC::LT;
173 break;
175 CondCode = AArch64CC::LE;
176 break;
178 CondCode = AArch64CC::NE;
179 break;
181 CondCode = AArch64CC::AL;
182 break;
184 CondCode = AArch64CC::NV;
185 break;
186 }
187}
188
191 AArch64CC::CondCode &CondCode2, bool &Invert) {
192 Invert = false;
193 switch (P) {
194 default:
195 // Mostly the scalar mappings work fine.
196 changeFCMPPredToAArch64CC(P, CondCode, CondCode2);
197 break;
199 Invert = true;
200 [[fallthrough]];
202 CondCode = AArch64CC::MI;
203 CondCode2 = AArch64CC::GE;
204 break;
210 // All of the compare-mask comparisons are ordered, but we can switch
211 // between the two by a double inversion. E.g. ULE == !OGT.
212 Invert = true;
214 CondCode2);
215 break;
216 }
217}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
#define P(N)
This file describes how to lower LLVM code to machine code.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:679
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:685
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:692
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:686
bool isSigned() const
Definition InstrTypes.h:930
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
Tracks which library functions to use for a particular subtarget.
LLVM_ABI RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
Helper class to build MachineInstr.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
void setInstrAndDebugLoc(MachineInstr &MI)
Set the insertion point to before MI, and set the debug loc to MI's loc.
MachineRegisterInfo * getMRI()
Getter for MRI.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
const MachineOperand & getOperand(unsigned i) const
Register getReg() const
getReg - Returns the register number.
Intrinsic::ID getIntrinsicID() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Represents a value which can be a Register or a constant.
Definition Utils.h:404
Wrapper class representing virtual and physical registers.
Definition Register.h:20
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::tuple< uint16_t, Register > extractPtrauthBlendDiscriminators(Register Disc, MachineRegisterInfo &MRI)
Analyze a ptrauth discriminator value to try to find the constant integer and address parts,...
std::optional< RegOrConstant > getAArch64VectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
void changeFCMPPredToAArch64CC(const CmpInst::Predicate P, AArch64CC::CondCode &CondCode, AArch64CC::CondCode &CondCode2)
Find the AArch64 condition codes necessary to represent P for a scalar floating point comparison.
bool tryEmitBZero(MachineInstr &MI, MachineIRBuilder &MIRBuilder, const LibcallLoweringInfo &Libcalls, bool MinSize)
Replace a G_MEMSET with a value of 0 with a G_BZERO instruction if it is supported and beneficial to ...
void changeVectorFCMPPredToAArch64CC(const CmpInst::Predicate P, AArch64CC::CondCode &CondCode, AArch64CC::CondCode &CondCode2, bool &Invert)
Find the AArch64 condition codes necessary to represent P for a vector floating point comparison.
bool isCMN(const MachineInstr *MaybeSub, const CmpInst::Predicate &Pred, const MachineRegisterInfo &MRI)
std::optional< int64_t > getAArch64VectorSplatScalar(const MachineInstr &MI, const MachineRegisterInfo &MRI)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:295
LLVM_ABI std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1501
LLVM_ABI std::optional< ValueAndVReg > getAnyConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true, bool LookThroughAnyExt=false)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT or G_FCONST...
Definition Utils.cpp:440
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
LLVM_ABI std::optional< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
Definition Utils.cpp:434