LLVM 20.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//===----------------------------------------------------------------------===//
12#include "AArch64InstrInfo.h"
15#include "llvm/IR/InstrTypes.h"
17
18using namespace llvm;
19
20std::optional<RegOrConstant>
22 const MachineRegisterInfo &MRI) {
23 if (auto Splat = getVectorSplat(MI, MRI))
24 return Splat;
25 if (MI.getOpcode() != AArch64::G_DUP)
26 return std::nullopt;
27 Register Src = MI.getOperand(1).getReg();
28 if (auto ValAndVReg =
29 getAnyConstantVRegValWithLookThrough(MI.getOperand(1).getReg(), MRI))
30 return RegOrConstant(ValAndVReg->Value.getSExtValue());
31 return RegOrConstant(Src);
32}
33
34std::optional<int64_t>
36 const MachineRegisterInfo &MRI) {
38 if (!Splat || Splat->isReg())
39 return std::nullopt;
40 return Splat->getCst();
41}
42
44 const CmpInst::Predicate &Pred,
45 const MachineRegisterInfo &MRI) {
46 // Match:
47 //
48 // %sub = G_SUB 0, %y
49 // %cmp = G_ICMP eq/ne, %sub, %z
50 //
51 // Or
52 //
53 // %sub = G_SUB 0, %y
54 // %cmp = G_ICMP eq/ne, %z, %sub
55 if (!MaybeSub || MaybeSub->getOpcode() != TargetOpcode::G_SUB ||
57 return false;
58 auto MaybeZero =
60 return MaybeZero && MaybeZero->Value.getZExtValue() == 0;
61}
62
64 MachineIRBuilder &MIRBuilder,
65 bool MinSize) {
66 assert(MI.getOpcode() == TargetOpcode::G_MEMSET);
67 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
68 auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering();
69 if (!TLI.getLibcallName(RTLIB::BZERO))
70 return false;
71 auto Zero =
72 getIConstantVRegValWithLookThrough(MI.getOperand(1).getReg(), MRI);
73 if (!Zero || Zero->Value.getSExtValue() != 0)
74 return false;
75
76 // It's not faster to use bzero rather than memset for sizes <= 256.
77 // However, it *does* save us a mov from wzr, so if we're going for
78 // minsize, use bzero even if it's slower.
79 if (!MinSize) {
80 // If the size is known, check it. If it is not known, assume using bzero is
81 // better.
83 MI.getOperand(2).getReg(), MRI)) {
84 if (Size->Value.getSExtValue() <= 256)
85 return false;
86 }
87 }
88
89 MIRBuilder.setInstrAndDebugLoc(MI);
90 MIRBuilder
91 .buildInstr(TargetOpcode::G_BZERO, {},
92 {MI.getOperand(0), MI.getOperand(2)})
93 .addImm(MI.getOperand(3).getImm())
94 .addMemOperand(*MI.memoperands_begin());
95 MI.eraseFromParent();
96 return true;
97}
98
99std::tuple<uint16_t, Register>
102 Register AddrDisc = Disc;
103 uint16_t ConstDisc = 0;
104
105 if (auto ConstDiscVal = getIConstantVRegVal(Disc, MRI)) {
106 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
107 ConstDisc = ConstDiscVal->getZExtValue();
108 AddrDisc = AArch64::NoRegister;
109 }
110 return std::make_tuple(ConstDisc, AddrDisc);
111 }
112
113 const MachineInstr *DiscMI = MRI.getVRegDef(Disc);
114 if (!DiscMI || DiscMI->getOpcode() != TargetOpcode::G_INTRINSIC ||
115 DiscMI->getOperand(1).getIntrinsicID() != Intrinsic::ptrauth_blend)
116 return std::make_tuple(ConstDisc, AddrDisc);
117
118 if (auto ConstDiscVal =
119 getIConstantVRegVal(DiscMI->getOperand(3).getReg(), MRI)) {
120 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
121 ConstDisc = ConstDiscVal->getZExtValue();
122 AddrDisc = DiscMI->getOperand(2).getReg();
123 }
124 }
125 return std::make_tuple(ConstDisc, AddrDisc);
126}
127
130 AArch64CC::CondCode &CondCode2) {
131 CondCode2 = AArch64CC::AL;
132 switch (P) {
133 default:
134 llvm_unreachable("Unknown FP condition!");
136 CondCode = AArch64CC::EQ;
137 break;
139 CondCode = AArch64CC::GT;
140 break;
142 CondCode = AArch64CC::GE;
143 break;
145 CondCode = AArch64CC::MI;
146 break;
148 CondCode = AArch64CC::LS;
149 break;
151 CondCode = AArch64CC::MI;
152 CondCode2 = AArch64CC::GT;
153 break;
155 CondCode = AArch64CC::VC;
156 break;
158 CondCode = AArch64CC::VS;
159 break;
161 CondCode = AArch64CC::EQ;
162 CondCode2 = AArch64CC::VS;
163 break;
165 CondCode = AArch64CC::HI;
166 break;
168 CondCode = AArch64CC::PL;
169 break;
171 CondCode = AArch64CC::LT;
172 break;
174 CondCode = AArch64CC::LE;
175 break;
177 CondCode = AArch64CC::NE;
178 break;
180 CondCode = AArch64CC::AL;
181 break;
183 CondCode = AArch64CC::NV;
184 break;
185 }
186}
187
190 AArch64CC::CondCode &CondCode2, bool &Invert) {
191 Invert = false;
192 switch (P) {
193 default:
194 // Mostly the scalar mappings work fine.
195 changeFCMPPredToAArch64CC(P, CondCode, CondCode2);
196 break;
198 Invert = true;
199 [[fallthrough]];
201 CondCode = AArch64CC::MI;
202 CondCode2 = AArch64CC::GE;
203 break;
209 // All of the compare-mask comparisons are ordered, but we can switch
210 // between the two by a double inversion. E.g. ULE == !OGT.
211 Invert = true;
213 CondCode2);
214 break;
215 }
216}
unsigned const MachineRegisterInfo * MRI
uint64_t Size
IRTranslator LLVM IR MI
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file describes how to lower LLVM code to machine code.
bool isEquality() const
Determine if this is an equals/not equals predicate.
Definition: InstrTypes.h:997
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:757
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:760
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:774
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:763
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:772
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:761
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:762
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:771
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:765
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:768
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:769
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:764
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:766
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:773
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:770
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:759
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:767
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:871
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Helper class to build MachineInstr.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
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.
Definition: MachineInstr.h:69
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:569
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:579
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:395
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
virtual const TargetLowering * getTargetLowering() const
#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, 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: AddressRanges.h:18
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
std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:1453
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:439
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:433