File indexing completed on 2026-05-10 08:44:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #ifndef LLVM_IR_GLOBALVARIABLE_H
0020 #define LLVM_IR_GLOBALVARIABLE_H
0021
0022 #include "llvm/ADT/Twine.h"
0023 #include "llvm/ADT/ilist_node.h"
0024 #include "llvm/IR/Attributes.h"
0025 #include "llvm/IR/GlobalObject.h"
0026 #include "llvm/IR/OperandTraits.h"
0027 #include "llvm/IR/Value.h"
0028 #include <cassert>
0029 #include <cstddef>
0030
0031 namespace llvm {
0032
0033 class Constant;
0034 class Module;
0035
0036 template <typename ValueSubClass, typename... Args> class SymbolTableListTraits;
0037 class DIGlobalVariableExpression;
0038
0039 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
0040 friend class SymbolTableListTraits<GlobalVariable>;
0041
0042 constexpr static IntrusiveOperandsAllocMarker AllocMarker{1};
0043
0044 AttributeSet Attrs;
0045
0046
0047 bool isConstantGlobal : 1;
0048
0049
0050 bool isExternallyInitializedConstant : 1;
0051
0052 private:
0053 static const unsigned CodeModelBits = LastCodeModelBit - LastAlignmentBit;
0054 static const unsigned CodeModelMask = (1 << CodeModelBits) - 1;
0055 static const unsigned CodeModelShift = LastAlignmentBit + 1;
0056
0057 public:
0058
0059
0060 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
0061 Constant *Initializer = nullptr, const Twine &Name = "",
0062 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
0063 bool isExternallyInitialized = false);
0064
0065
0066 GlobalVariable(Module &M, Type *Ty, bool isConstant, LinkageTypes Linkage,
0067 Constant *Initializer, const Twine &Name = "",
0068 GlobalVariable *InsertBefore = nullptr,
0069 ThreadLocalMode = NotThreadLocal,
0070 std::optional<unsigned> AddressSpace = std::nullopt,
0071 bool isExternallyInitialized = false);
0072 GlobalVariable(const GlobalVariable &) = delete;
0073 GlobalVariable &operator=(const GlobalVariable &) = delete;
0074
0075 private:
0076
0077
0078
0079
0080 void setGlobalVariableNumOperands(unsigned NumOps) {
0081 assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
0082 NumUserOperands = NumOps;
0083 }
0084
0085 public:
0086 ~GlobalVariable() {
0087 dropAllReferences();
0088
0089
0090
0091
0092 setGlobalVariableNumOperands(1);
0093 }
0094
0095
0096 void *operator new(size_t s) { return User::operator new(s, AllocMarker); }
0097
0098
0099 void operator delete(void *ptr) { User::operator delete(ptr); }
0100
0101
0102 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
0103
0104
0105
0106 inline bool hasInitializer() const { return !isDeclaration(); }
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 inline bool hasDefinitiveInitializer() const {
0125 return hasInitializer() &&
0126
0127
0128 !isInterposable() &&
0129
0130
0131 !isExternallyInitialized();
0132 }
0133
0134
0135
0136 inline bool hasUniqueInitializer() const {
0137 return
0138
0139 isStrongDefinitionForLinker() &&
0140
0141
0142
0143 !isExternallyInitialized();
0144 }
0145
0146
0147
0148
0149
0150 inline const Constant *getInitializer() const {
0151 assert(hasInitializer() && "GV doesn't have initializer!");
0152 return static_cast<Constant*>(Op<0>().get());
0153 }
0154 inline Constant *getInitializer() {
0155 assert(hasInitializer() && "GV doesn't have initializer!");
0156 return static_cast<Constant*>(Op<0>().get());
0157 }
0158
0159
0160
0161 void setInitializer(Constant *InitVal);
0162
0163
0164
0165
0166
0167 void replaceInitializer(Constant *InitVal);
0168
0169
0170
0171
0172
0173 bool isConstant() const { return isConstantGlobal; }
0174 void setConstant(bool Val) { isConstantGlobal = Val; }
0175
0176 bool isExternallyInitialized() const {
0177 return isExternallyInitializedConstant;
0178 }
0179 void setExternallyInitialized(bool Val) {
0180 isExternallyInitializedConstant = Val;
0181 }
0182
0183
0184
0185 void copyAttributesFrom(const GlobalVariable *Src);
0186
0187
0188
0189
0190 void removeFromParent();
0191
0192
0193
0194
0195 void eraseFromParent();
0196
0197
0198
0199 void dropAllReferences();
0200
0201
0202 void addDebugInfo(DIGlobalVariableExpression *GV);
0203
0204
0205 void getDebugInfo(SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const;
0206
0207
0208 void addAttribute(Attribute::AttrKind Kind) {
0209 Attrs = Attrs.addAttribute(getContext(), Kind);
0210 }
0211
0212
0213 void addAttribute(StringRef Kind, StringRef Val = StringRef()) {
0214 Attrs = Attrs.addAttribute(getContext(), Kind, Val);
0215 }
0216
0217
0218 bool hasAttribute(Attribute::AttrKind Kind) const {
0219 return Attrs.hasAttribute(Kind);
0220 }
0221
0222
0223 bool hasAttribute(StringRef Kind) const {
0224 return Attrs.hasAttribute(Kind);
0225 }
0226
0227
0228 bool hasAttributes() const {
0229 return Attrs.hasAttributes();
0230 }
0231
0232
0233 Attribute getAttribute(Attribute::AttrKind Kind) const {
0234 return Attrs.getAttribute(Kind);
0235 }
0236
0237
0238 Attribute getAttribute(StringRef Kind) const {
0239 return Attrs.getAttribute(Kind);
0240 }
0241
0242
0243 AttributeSet getAttributes() const {
0244 return Attrs;
0245 }
0246
0247
0248
0249
0250 AttributeList getAttributesAsList(unsigned index) const {
0251 if (!hasAttributes())
0252 return AttributeList();
0253 std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}};
0254 return AttributeList::get(getContext(), AS);
0255 }
0256
0257
0258 void setAttributes(AttributeSet A) {
0259 Attrs = A;
0260 }
0261
0262
0263 bool hasImplicitSection() const {
0264 return getAttributes().hasAttribute("bss-section") ||
0265 getAttributes().hasAttribute("data-section") ||
0266 getAttributes().hasAttribute("relro-section") ||
0267 getAttributes().hasAttribute("rodata-section");
0268 }
0269
0270
0271
0272 unsigned getCodeModelRaw() const {
0273 unsigned Data = getGlobalValueSubClassData();
0274 return (Data >> CodeModelShift) & CodeModelMask;
0275 }
0276
0277
0278
0279
0280
0281 std::optional<CodeModel::Model> getCodeModel() const {
0282 unsigned CodeModelData = getCodeModelRaw();
0283 if (CodeModelData > 0)
0284 return static_cast<CodeModel::Model>(CodeModelData - 1);
0285 return {};
0286 }
0287
0288
0289
0290 void setCodeModel(CodeModel::Model CM);
0291
0292
0293 static bool classof(const Value *V) {
0294 return V->getValueID() == Value::GlobalVariableVal;
0295 }
0296 };
0297
0298 template <>
0299 struct OperandTraits<GlobalVariable> :
0300 public OptionalOperandTraits<GlobalVariable> {
0301 };
0302
0303 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
0304
0305 }
0306
0307 #endif