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 = getAnyConstantVRegValWithLookThrough(
27 Src, MRI, /*LookThroughInstrs=*/true, /*LookThroughAnyExt=*/true))
28 return RegOrConstant(ValAndVReg->Value.getSExtValue());
29 return RegOrConstant(Src);
30}
31
32std::optional<int64_t>
34 const MachineRegisterInfo &MRI) {
35 auto Splat = getAArch64VectorSplat(MI, 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 assert(MI.getOpcode() == TargetOpcode::G_MEMSET);
66
67 MIRBuilder.setInstrAndDebugLoc(MI);
68 MIRBuilder
69 .buildInstr(TargetOpcode::G_BZERO, {},
70 {MI.getOperand(0), MI.getOperand(2)})
71 .addImm(MI.getOperand(3).getImm())
72 .addMemOperand(*MI.memoperands_begin());
73 MI.eraseFromParent();
74}
75
77 const MachineRegisterInfo &MRI,
78 const LibcallLoweringInfo &Libcalls,
79 bool MinSize) {
80 assert(MI.getOpcode() == TargetOpcode::G_MEMSET);
81 if (Libcalls.getLibcallImpl(RTLIB::BZERO) == RTLIB::Unsupported)
82 return false;
83
84 auto Zero =
85 getIConstantVRegValWithLookThrough(MI.getOperand(1).getReg(), MRI);
86 if (!Zero || Zero->Value.getSExtValue() != 0)
87 return false;
88
89 // It's not faster to use bzero rather than memset for sizes <= 256.
90 // However, it *does* save us a mov from wzr, so if we're going for
91 // minsize, use bzero even if it's slower.
92 if (!MinSize) {
93 // If the size is known, check it. If it is not known, assume using bzero is
94 // better.
96 MI.getOperand(2).getReg(), MRI)) {
97 if (Size->Value.getSExtValue() <= 256)
98 return false;
99 }
100 }
101 return true;
102}
103
104std::tuple<uint16_t, Register>
106 MachineRegisterInfo &MRI) {
107 Register AddrDisc = Disc;
108 uint16_t ConstDisc = 0;
109
110 if (auto ConstDiscVal = getIConstantVRegVal(Disc, MRI)) {
111 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
112 ConstDisc = ConstDiscVal->getZExtValue();
113 AddrDisc = AArch64::NoRegister;
114 }
115 return std::make_tuple(ConstDisc, AddrDisc);
116 }
117
118 const MachineInstr *DiscMI = MRI.getVRegDef(Disc);
119 if (!DiscMI || DiscMI->getOpcode() != TargetOpcode::G_INTRINSIC ||
120 DiscMI->getOperand(1).getIntrinsicID() != Intrinsic::ptrauth_blend)
121 return std::make_tuple(ConstDisc, AddrDisc);
122
123 if (auto ConstDiscVal =
124 getIConstantVRegVal(DiscMI->getOperand(3).getReg(), MRI)) {
125 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
126 ConstDisc = ConstDiscVal->getZExtValue();
127 AddrDisc = DiscMI->getOperand(2).getReg();
128 }
129 }
130 return std::make_tuple(ConstDisc, AddrDisc);
131}
132
135 AArch64CC::CondCode &CondCode2) {
136 CondCode2 = AArch64CC::AL;
137 switch (P) {
138 default:
139 llvm_unreachable("Unknown FP condition!");
141 CondCode = AArch64CC::EQ;
142 break;
144 CondCode = AArch64CC::GT;
145 break;
147 CondCode = AArch64CC::GE;
148 break;
150 CondCode = AArch64CC::MI;
151 break;
153 CondCode = AArch64CC::LS;
154 break;
156 CondCode = AArch64CC::MI;
157 CondCode2 = AArch64CC::GT;
158 break;
160 CondCode = AArch64CC::VC;
161 break;
163 CondCode = AArch64CC::VS;
164 break;
166 CondCode = AArch64CC::EQ;
167 CondCode2 = AArch64CC::VS;
168 break;
170 CondCode = AArch64CC::HI;
171 break;
173 CondCode = AArch64CC::PL;
174 break;
176 CondCode = AArch64CC::LT;
177 break;
179 CondCode = AArch64CC::LE;
180 break;
182 CondCode = AArch64CC::NE;
183 break;
185 CondCode = AArch64CC::AL;
186 break;
188 CondCode = AArch64CC::NV;
189 break;
190 }
191}
192
195 AArch64CC::CondCode &CondCode2, bool &Invert) {
196 Invert = false;
197 switch (P) {
198 default:
199 // Mostly the scalar mappings work fine.
200 changeFCMPPredToAArch64CC(P, CondCode, CondCode2);
201 break;
203 Invert = true;
204 [[fallthrough]];
206 CondCode = AArch64CC::MI;
207 CondCode2 = AArch64CC::GE;
208 break;
214 // All of the compare-mask comparisons are ordered, but we can switch
215 // between the two by a double inversion. E.g. ULE == !OGT.
216 Invert = true;
218 CondCode2);
219 break;
220 }
221}
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:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
bool isSigned() const
Definition InstrTypes.h:993
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:852
Tracks which library functions to use for a particular subtarget.
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.
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,...
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
Represents a value which can be a Register or a constant.
Definition Utils.h:408
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)
bool matchEmitBZero(const MachineInstr &MI, const MachineRegisterInfo &MRI, 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 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.
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)
void applyEmitBZero(MachineInstr &MI, MachineIRBuilder &MIRBuilder)
Replace MI with a G_BZERO.
std::optional< int64_t > getAArch64VectorSplatScalar(const MachineInstr &MI, const MachineRegisterInfo &MRI)
This is an optimization pass for GlobalISel generic memory operations.
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:297
LLVM_ABI std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1460
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:442
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:436