| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef LLVM_IR_GLOBALVARIABLE_H |
| 20 |
#define LLVM_IR_GLOBALVARIABLE_H |
| 21 |
|
| 22 |
#include "llvm/ADT/Twine.h" |
| 23 |
#include "llvm/ADT/ilist_node.h" |
| 24 |
#include "llvm/IR/Attributes.h" |
| 25 |
#include "llvm/IR/GlobalObject.h" |
| 26 |
#include "llvm/IR/OperandTraits.h" |
| 27 |
#include "llvm/IR/Value.h" |
| 28 |
#include <cassert> |
| 29 |
#include <cstddef> |
| 30 |
|
| 31 |
namespace llvm { |
| 32 |
|
| 33 |
class Constant; |
| 34 |
class Module; |
| 35 |
|
| 36 |
template <typename ValueSubClass> class SymbolTableListTraits; |
| 37 |
class DIGlobalVariableExpression; |
| 38 |
|
| 39 |
class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> { |
| 40 |
friend class SymbolTableListTraits<GlobalVariable>; |
| 41 |
|
| 42 |
AttributeSet Attrs; |
| 43 |
bool isConstantGlobal : 1; |
| 44 |
bool isExternallyInitializedConstant : 1; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public: |
| 50 |
|
| 51 |
|
| 52 |
GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage, |
| 53 |
Constant *Initializer = nullptr, const Twine &Name = "", |
| 54 |
ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, |
| 55 |
bool isExternallyInitialized = false); |
| 56 |
|
| 57 |
|
| 58 |
GlobalVariable(Module &M, Type *Ty, bool isConstant, LinkageTypes Linkage, |
| 59 |
Constant *Initializer, const Twine &Name = "", |
| 60 |
GlobalVariable *InsertBefore = nullptr, |
| 61 |
ThreadLocalMode = NotThreadLocal, |
| 62 |
Optional<unsigned> AddressSpace = None, |
| 63 |
bool isExternallyInitialized = false); |
| 64 |
GlobalVariable(const GlobalVariable &) = delete; |
| 65 |
GlobalVariable &operator=(const GlobalVariable &) = delete; |
| 66 |
|
| 67 |
~GlobalVariable() { |
| 68 |
dropAllReferences(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
void *operator new(size_t s) { |
| 73 |
return User::operator new(s, 1); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
void operator delete(void *ptr){ |
| 78 |
assert(ptr != nullptr && "must not be nullptr"); |
| 79 |
User *Obj = static_cast<User *>(ptr); |
| 80 |
|
| 81 |
|
| 82 |
Obj->setGlobalVariableNumOperands(1); |
| 83 |
User::operator delete(Obj); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
inline bool hasInitializer() const { return !isDeclaration(); } |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
inline bool hasDefinitiveInitializer() const { |
| 110 |
return hasInitializer() && |
| 111 |
|
| 112 |
|
| 113 |
!isInterposable() && |
| 114 |
|
| 115 |
|
| 116 |
!isExternallyInitialized(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
inline bool hasUniqueInitializer() const { |
| 122 |
return |
| 123 |
|
| 124 |
isStrongDefinitionForLinker() && |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
!isExternallyInitialized(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
inline const Constant *getInitializer() const { |
| 136 |
assert(hasInitializer() && "GV doesn't have initializer!"); |
| 137 |
return static_cast<Constant*>(Op<0>().get()); |
| 138 |
} |
| 139 |
inline Constant *getInitializer() { |
| 140 |
assert(hasInitializer() && "GV doesn't have initializer!"); |
| 141 |
return static_cast<Constant*>(Op<0>().get()); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
void setInitializer(Constant *InitVal); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
bool isConstant() const { return isConstantGlobal; } |
| 153 |
void setConstant(bool Val) { isConstantGlobal = Val; } |
| 154 |
|
| 155 |
bool isExternallyInitialized() const { |
| 156 |
return isExternallyInitializedConstant; |
| 157 |
} |
| 158 |
void setExternallyInitialized(bool Val) { |
| 159 |
isExternallyInitializedConstant = Val; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
void copyAttributesFrom(const GlobalVariable *Src); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
void removeFromParent(); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
void eraseFromParent(); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
void dropAllReferences(); |
| 179 |
|
| 180 |
|
| 181 |
void addDebugInfo(DIGlobalVariableExpression *GV); |
| 182 |
|
| 183 |
|
| 184 |
void getDebugInfo(SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const; |
| 185 |
|
| 186 |
|
| 187 |
void addAttribute(Attribute::AttrKind Kind) { |
| 188 |
Attrs = Attrs.addAttribute(getContext(), Kind); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
void addAttribute(StringRef Kind, StringRef Val = StringRef()) { |
| 193 |
Attrs = Attrs.addAttribute(getContext(), Kind, Val); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
bool hasAttribute(Attribute::AttrKind Kind) const { |
| 198 |
return Attrs.hasAttribute(Kind); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
bool hasAttribute(StringRef Kind) const { |
| 203 |
return Attrs.hasAttribute(Kind); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
bool hasAttributes() const { |
| 208 |
return Attrs.hasAttributes(); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
Attribute getAttribute(Attribute::AttrKind Kind) const { |
| 213 |
return Attrs.getAttribute(Kind); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
Attribute getAttribute(StringRef Kind) const { |
| 218 |
return Attrs.getAttribute(Kind); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
AttributeSet getAttributes() const { |
| 223 |
return Attrs; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
AttributeList getAttributesAsList(unsigned index) const { |
| 230 |
if (!hasAttributes()) |
| 231 |
return AttributeList(); |
| 232 |
std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}}; |
| 233 |
return AttributeList::get(getContext(), AS); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
void setAttributes(AttributeSet A) { |
| 238 |
Attrs = A; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
bool hasImplicitSection() const { |
| 243 |
return getAttributes().hasAttribute("bss-section") || |
| 244 |
getAttributes().hasAttribute("data-section") || |
| 245 |
getAttributes().hasAttribute("relro-section") || |
| 246 |
getAttributes().hasAttribute("rodata-section"); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
static bool classof(const Value *V) { |
| 251 |
return V->getValueID() == Value::GlobalVariableVal; |
| 252 |
} |
| 253 |
}; |
| 254 |
|
| 255 |
template <> |
| 256 |
struct OperandTraits<GlobalVariable> : |
| 257 |
public OptionalOperandTraits<GlobalVariable> { |
| 258 |
}; |
| 259 |
|
| 260 |
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value) |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
#endif |