LLVM 23.0.0git
AArch64SMEAttributes.h
Go to the documentation of this file.
1//===-- AArch64SMEAttributes.h - Helper for interpreting SME attributes -*-===//
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
9#ifndef LLVM_LIB_TARGET_AARCH64_UTILS_AARCH64SMEATTRIBUTES_H
10#define LLVM_LIB_TARGET_AARCH64_UTILS_AARCH64SMEATTRIBUTES_H
11
12#include "llvm/IR/Function.h"
13
14namespace llvm {
15namespace RTLIB {
17}
18
19class Function;
20class CallBase;
21class AttributeList;
22
23/// SMEAttrs is a utility class to parse the SME ACLE attributes on functions.
24/// It helps determine a function's requirements for PSTATE.ZA and PSTATE.SM.
25class SMEAttrs {
26 unsigned Bitmask = Normal;
27
28public:
29 enum class StateValue {
30 None = 0,
31 In = 1, // aarch64_in_zt0
32 Out = 2, // aarch64_out_zt0
33 InOut = 3, // aarch64_inout_zt0
34 Preserved = 4, // aarch64_preserves_zt0
35 New = 5 // aarch64_new_zt0
36 };
37
38 // Enum with bitmasks for each individual SME feature.
39 enum Mask {
40 Normal = 0,
41 SM_Enabled = 1 << 0, // aarch64_pstate_sm_enabled
42 SM_Compatible = 1 << 1, // aarch64_pstate_sm_compatible
43 SM_Body = 1 << 2, // aarch64_pstate_sm_body
44 SME_ABI_Routine = 1 << 3, // Used for SME ABI routines to avoid lazy saves
47 ZA_Mask = 0b111 << ZA_Shift,
50 };
51
52 SMEAttrs() = default;
53 SMEAttrs(unsigned Mask) { set(Mask); }
54 SMEAttrs(const Function &F, const RTLIB::RuntimeLibcallsInfo *RTLCI = nullptr)
55 : SMEAttrs(F.getAttributes()) {
56 if (RTLCI)
57 addKnownFunctionAttrs(F.getName(), *RTLCI);
58 }
59 SMEAttrs(const AttributeList &L);
61 addKnownFunctionAttrs(FuncName, RTLCI);
62 };
63
64 void set(unsigned M, bool Enable = true) {
65 if (Enable)
66 Bitmask |= M;
67 else
68 Bitmask &= ~M;
69#ifndef NDEBUG
70 validate();
71#endif
72 }
73
74 // Interfaces to query PSTATE.SM
75 bool hasStreamingBody() const { return Bitmask & SM_Body; }
76 bool hasStreamingInterface() const { return Bitmask & SM_Enabled; }
81 return Bitmask & SM_Compatible;
82 }
89
90 // Interfaces to query ZA
91 static StateValue decodeZAState(unsigned Bitmask) {
92 return static_cast<StateValue>((Bitmask & ZA_Mask) >> ZA_Shift);
93 }
94 static unsigned encodeZAState(StateValue S) {
95 return static_cast<unsigned>(S) << ZA_Shift;
96 }
97
98 bool isNewZA() const { return decodeZAState(Bitmask) == StateValue::New; }
99 bool isInZA() const { return decodeZAState(Bitmask) == StateValue::In; }
100 bool isOutZA() const { return decodeZAState(Bitmask) == StateValue::Out; }
101 bool isInOutZA() const { return decodeZAState(Bitmask) == StateValue::InOut; }
102 bool isPreservesZA() const {
103 return decodeZAState(Bitmask) == StateValue::Preserved;
104 }
105 bool sharesZA() const {
106 StateValue State = decodeZAState(Bitmask);
107 return State == StateValue::In || State == StateValue::Out ||
108 State == StateValue::InOut || State == StateValue::Preserved;
109 }
110 bool hasAgnosticZAInterface() const { return Bitmask & ZA_State_Agnostic; }
111 bool hasSharedZAInterface() const { return sharesZA() || sharesZT0(); }
114 }
115 bool hasZAState() const { return isNewZA() || sharesZA(); }
116 bool isSMEABIRoutine() const { return Bitmask & SME_ABI_Routine; }
117
118 // Interfaces to query ZT0 State
119 static StateValue decodeZT0State(unsigned Bitmask) {
120 return static_cast<StateValue>((Bitmask & ZT0_Mask) >> ZT0_Shift);
121 }
122 static unsigned encodeZT0State(StateValue S) {
123 return static_cast<unsigned>(S) << ZT0_Shift;
124 }
125
126 bool isNewZT0() const { return decodeZT0State(Bitmask) == StateValue::New; }
127 bool isInZT0() const { return decodeZT0State(Bitmask) == StateValue::In; }
128 bool isOutZT0() const { return decodeZT0State(Bitmask) == StateValue::Out; }
129 bool isInOutZT0() const {
130 return decodeZT0State(Bitmask) == StateValue::InOut;
131 }
132 bool isPreservesZT0() const {
133 return decodeZT0State(Bitmask) == StateValue::Preserved;
134 }
135 bool sharesZT0() const {
136 StateValue State = decodeZT0State(Bitmask);
137 return State == StateValue::In || State == StateValue::Out ||
138 State == StateValue::InOut || State == StateValue::Preserved;
139 }
140 bool hasZT0State() const { return isNewZT0() || sharesZT0(); }
141
143 SMEAttrs Merged(*this);
144 Merged.set(Other.Bitmask);
145 return Merged;
146 }
147
148 bool operator==(SMEAttrs const &Other) const {
149 return Bitmask == Other.Bitmask;
150 }
151
152private:
153 void addKnownFunctionAttrs(StringRef FuncName,
154 const RTLIB::RuntimeLibcallsInfo &RTLCI);
155 void validate() const;
156};
157
158/// SMECallAttrs is a utility class to hold the SMEAttrs for a callsite. It has
159/// interfaces to query whether a streaming mode change or lazy-save mechanism
160/// is required when going from one function to another (e.g. through a call).
162 SMEAttrs CallerFn;
163 SMEAttrs CalledFn;
164 SMEAttrs Callsite;
165 bool IsIndirect = false;
166
167public:
169 SMEAttrs Callsite = SMEAttrs::Normal)
170 : CallerFn(Caller), CalledFn(Callee), Callsite(Callsite) {}
171
172 SMECallAttrs(const CallBase &CB, const RTLIB::RuntimeLibcallsInfo *RTLCI);
173
174 SMEAttrs &caller() { return CallerFn; }
175 SMEAttrs &callee() { return IsIndirect ? Callsite : CalledFn; }
176 SMEAttrs &callsite() { return Callsite; }
177 SMEAttrs const &caller() const { return CallerFn; }
178 SMEAttrs const &callee() const {
179 return const_cast<SMECallAttrs *>(this)->callee();
180 }
181 SMEAttrs const &callsite() const { return Callsite; }
182
183 /// \return true if a call from Caller -> Callee requires a change in
184 /// streaming mode.
185 bool requiresSMChange() const;
186
187 bool requiresLazySave() const {
188 return caller().hasZAState() && callee().hasPrivateZAInterface() &&
190 }
191
193 return caller().hasZT0State() && !callee().sharesZT0() &&
195 }
196
201};
202
203} // namespace llvm
204
205#endif // LLVM_LIB_TARGET_AARCH64_UTILS_AARCH64SMEATTRIBUTES_H
#define F(x, y, z)
Definition MD5.cpp:54
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
SMEAttrs is a utility class to parse the SME ACLE attributes on functions.
bool isPreservesZT0() const
bool hasStreamingInterface() const
static unsigned encodeZAState(StateValue S)
bool hasNonStreamingInterfaceAndBody() const
SMEAttrs()=default
SMEAttrs(unsigned Mask)
bool hasStreamingCompatibleInterface() const
SMEAttrs(StringRef FuncName, const RTLIB::RuntimeLibcallsInfo &RTLCI)
SMEAttrs operator|(SMEAttrs Other) const
bool hasAgnosticZAInterface() const
bool hasStreamingInterfaceOrBody() const
static StateValue decodeZAState(unsigned Bitmask)
bool hasNonStreamingInterface() const
bool isSMEABIRoutine() const
bool operator==(SMEAttrs const &Other) const
static StateValue decodeZT0State(unsigned Bitmask)
bool hasStreamingBody() const
SMEAttrs(const Function &F, const RTLIB::RuntimeLibcallsInfo *RTLCI=nullptr)
bool isPreservesZA() const
bool hasPrivateZAInterface() const
void set(unsigned M, bool Enable=true)
bool hasSharedZAInterface() const
static unsigned encodeZT0State(StateValue S)
bool requiresPreservingZT0() const
SMEAttrs const & callsite() const
SMECallAttrs(SMEAttrs Caller, SMEAttrs Callee, SMEAttrs Callsite=SMEAttrs::Normal)
SMEAttrs const & caller() const
SMEAttrs const & callee() const
bool requiresPreservingAllZAState() const
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
@ Other
Any other memory.
Definition ModRef.h:68
@ Enable
Enable colors.
Definition WithColor.h:47
A simple container for information about the supported runtime calls.