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//===----------------------------------------------------------------------===//
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 if (!MaybeSub || MaybeSub->getOpcode() != TargetOpcode::G_SUB ||
55 return false;
56 auto MaybeZero =
58 return MaybeZero && MaybeZero->Value.getZExtValue() == 0;
59}
60
62 MachineIRBuilder &MIRBuilder,
63 bool MinSize) {
64 assert(MI.getOpcode() == TargetOpcode::G_MEMSET);
65 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
66 auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering();
67 if (!TLI.getLibcallName(RTLIB::BZERO))
68 return false;
69 auto Zero =
70 getIConstantVRegValWithLookThrough(MI.getOperand(1).getReg(), MRI);
71 if (!Zero || Zero->Value.getSExtValue() != 0)
72 return false;
73
74 // It's not faster to use bzero rather than memset for sizes <= 256.
75 // However, it *does* save us a mov from wzr, so if we're going for
76 // minsize, use bzero even if it's slower.
77 if (!MinSize) {
78 // If the size is known, check it. If it is not known, assume using bzero is
79 // better.
81 MI.getOperand(2).getReg(), MRI)) {
82 if (Size->Value.getSExtValue() <= 256)
83 return false;
84 }
85 }
86
87 MIRBuilder.setInstrAndDebugLoc(MI);
88 MIRBuilder
89 .buildInstr(TargetOpcode::G_BZERO, {},
90 {MI.getOperand(0), MI.getOperand(2)})
91 .addImm(MI.getOperand(3).getImm())
92 .addMemOperand(*MI.memoperands_begin());
93 MI.eraseFromParent();
94 return true;
95}
96
97std::tuple<uint16_t, Register>
100 Register AddrDisc = Disc;
101 uint16_t ConstDisc = 0;
102
103 if (auto ConstDiscVal = getIConstantVRegVal(Disc, MRI)) {
104 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
105 ConstDisc = ConstDiscVal->getZExtValue();
106 AddrDisc = AArch64::NoRegister;
107 }
108 return std::make_tuple(ConstDisc, AddrDisc);
109 }
110
111 const MachineInstr *DiscMI = MRI.getVRegDef(Disc);
112 if (!DiscMI || DiscMI->getOpcode() != TargetOpcode::G_INTRINSIC ||
113 DiscMI->getOperand(1).getIntrinsicID() != Intrinsic::ptrauth_blend)
114 return std::make_tuple(ConstDisc, AddrDisc);
115
116 if (auto ConstDiscVal =
117 getIConstantVRegVal(DiscMI->getOperand(3).getReg(), MRI)) {
118 if (isUInt<16>(ConstDiscVal->getZExtValue())) {
119 ConstDisc = ConstDiscVal->getZExtValue();
120 AddrDisc = DiscMI->getOperand(2).getReg();
121 }
122 }
123 return std::make_tuple(ConstDisc, AddrDisc);
124}
125
128 AArch64CC::CondCode &CondCode2) {
129 CondCode2 = AArch64CC::AL;
130 switch (P) {
131 default:
132 llvm_unreachable("Unknown FP condition!");
134 CondCode = AArch64CC::EQ;
135 break;
137 CondCode = AArch64CC::GT;
138 break;
140 CondCode = AArch64CC::GE;
141 break;
143 CondCode = AArch64CC::MI;
144 break;
146 CondCode = AArch64CC::LS;
147 break;
149 CondCode = AArch64CC::MI;
150 CondCode2 = AArch64CC::GT;
151 break;
153 CondCode = AArch64CC::VC;
154 break;
156 CondCode = AArch64CC::VS;
157 break;
159 CondCode = AArch64CC::EQ;
160 CondCode2 = AArch64CC::VS;
161 break;
163 CondCode = AArch64CC::HI;
164 break;
166 CondCode = AArch64CC::PL;
167 break;
169 CondCode = AArch64CC::LT;
170 break;
172 CondCode = AArch64CC::LE;
173 break;
175 CondCode = AArch64CC::NE;
176 break;
178 CondCode = AArch64CC::AL;
179 break;
181 CondCode = AArch64CC::NV;
182 break;
183 }
184}
185
188 AArch64CC::CondCode &CondCode2, bool &Invert) {
189 Invert = false;
190 switch (P) {
191 default:
192 // Mostly the scalar mappings work fine.
193 changeFCMPPredToAArch64CC(P, CondCode, CondCode2);
194 break;
196 Invert = true;
197 [[fallthrough]];
199 CondCode = AArch64CC::MI;
200 CondCode2 = AArch64CC::GE;
201 break;
207 // All of the compare-mask comparisons are ordered, but we can switch
208 // between the two by a double inversion. E.g. ULE == !OGT.
209 Invert = true;
211 CondCode2);
212 break;
213 }
214}
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:913
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:690
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:679
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:688
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:677
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:678
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:687
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:681
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:684
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:685
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:680
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:682
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:689
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:686
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:675
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:683
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:787
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:575
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:585
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:279
std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:1438
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:424
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:418