LLVM  4.0.0
GlobalObject.h
Go to the documentation of this file.
1 //===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This represents an independent object. That is, a function or a global
11 // variable, but not an alias.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_GLOBALOBJECT_H
16 #define LLVM_IR_GLOBALOBJECT_H
17 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/Value.h"
21 #include <string>
22 #include <utility>
23 
24 namespace llvm {
25 
26 class Comdat;
27 class MDNode;
28 class Metadata;
29 
30 class GlobalObject : public GlobalValue {
31 protected:
32  GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
34  unsigned AddressSpace = 0)
35  : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace),
36  ObjComdat(nullptr) {
38  }
39 
41  enum {
45 
47  };
48  static const unsigned GlobalObjectSubClassDataBits =
50 
51 private:
52  static const unsigned AlignmentBits = LastAlignmentBit + 1;
53  static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
54  static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
55 
56 public:
57  GlobalObject(const GlobalObject &) = delete;
58 
59  unsigned getAlignment() const {
60  unsigned Data = getGlobalValueSubClassData();
61  unsigned AlignmentData = Data & AlignmentMask;
62  return (1u << AlignmentData) >> 1;
63  }
64  void setAlignment(unsigned Align);
65 
66  unsigned getGlobalObjectSubClassData() const;
67  void setGlobalObjectSubClassData(unsigned Val);
68 
69  /// Check if this global has a custom object file section.
70  ///
71  /// This is more efficient than calling getSection() and checking for an empty
72  /// string.
73  bool hasSection() const {
75  }
76 
77  /// Get the custom section of this global if it has one.
78  ///
79  /// If this global does not have a custom section, this will be empty and the
80  /// default object file section (.text, .data, etc) will be used.
82  return hasSection() ? getSectionImpl() : StringRef();
83  }
84 
85  /// Change the section for this global.
86  ///
87  /// Setting the section to the empty string tells LLVM to choose an
88  /// appropriate default object file section.
89  void setSection(StringRef S);
90 
91  bool hasComdat() const { return getComdat() != nullptr; }
92  const Comdat *getComdat() const { return ObjComdat; }
93  Comdat *getComdat() { return ObjComdat; }
94  void setComdat(Comdat *C) { ObjComdat = C; }
95 
96  /// Check if this has any metadata.
97  bool hasMetadata() const { return hasMetadataHashEntry(); }
98 
99  /// Get the current metadata attachments for the given kind, if any.
100  ///
101  /// These functions require that the function have at most a single attachment
102  /// of the given kind, and return \c nullptr if such an attachment is missing.
103  /// @{
104  MDNode *getMetadata(unsigned KindID) const;
106  /// @}
107 
108  /// Appends all attachments with the given ID to \c MDs in insertion order.
109  /// If the global has no attachments with the given ID, or if ID is invalid,
110  /// leaves MDs unchanged.
111  /// @{
112  void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
114  /// @}
115 
116  /// Set a particular kind of metadata attachment.
117  ///
118  /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
119  /// replacing it if it already exists.
120  /// @{
121  void setMetadata(unsigned KindID, MDNode *MD);
122  void setMetadata(StringRef Kind, MDNode *MD);
123  /// @}
124 
125  /// Add a metadata attachment.
126  /// @{
127  void addMetadata(unsigned KindID, MDNode &MD);
128  void addMetadata(StringRef Kind, MDNode &MD);
129  /// @}
130 
131  /// Appends all attachments for the global to \c MDs, sorting by attachment
132  /// ID. Attachments with the same ID appear in insertion order.
133  void
134  getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
135 
136  /// Erase all metadata attachments with the given kind.
137  void eraseMetadata(unsigned KindID);
138 
139  /// Copy metadata from Src, adjusting offsets by Offset.
140  void copyMetadata(const GlobalObject *Src, unsigned Offset);
141 
142  void addTypeMetadata(unsigned Offset, Metadata *TypeID);
143 
144  void copyAttributesFrom(const GlobalValue *Src) override;
145 
146  // Methods for support type inquiry through isa, cast, and dyn_cast:
147  static inline bool classof(const Value *V) {
148  return V->getValueID() == Value::FunctionVal ||
149  V->getValueID() == Value::GlobalVariableVal;
150  }
151 
152  void clearMetadata();
153 
154 private:
155  void setGlobalObjectFlag(unsigned Bit, bool Val) {
156  unsigned Mask = 1 << Bit;
158  (Val ? Mask : 0u));
159  }
160 
161  bool hasMetadataHashEntry() const {
163  }
164  void setHasMetadataHashEntry(bool HasEntry) {
165  setGlobalObjectFlag(HasMetadataHashEntryBit, HasEntry);
166  }
167 
168  StringRef getSectionImpl() const;
169 };
170 
171 } // end namespace llvm
172 
173 #endif // LLVM_IR_GLOBALOBJECT_H
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Appends all attachments for the global to MDs, sorting by attachment ID.
Definition: Metadata.cpp:1364
bool hasComdat() const
Definition: GlobalObject.h:91
Type::TypeID TypeID
Metadata node.
Definition: Metadata.h:830
void setAlignment(unsigned Align)
Definition: Globals.cpp:86
unsigned getGlobalObjectSubClassData() const
Definition: Globals.cpp:96
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1404
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
static const unsigned GlobalObjectSubClassDataBits
Definition: GlobalObject.h:48
unsigned getAlignment() const
Definition: GlobalObject.h:59
void setComdat(Comdat *C)
Definition: GlobalObject.h:94
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:81
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:73
Comdat * getComdat()
Definition: GlobalObject.h:93
GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace=0)
Definition: GlobalObject.h:32
bool hasMetadata() const
Check if this has any metadata.
Definition: GlobalObject.h:97
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1445
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
const Comdat * getComdat() const
Definition: GlobalObject.h:92
uint32_t Offset
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:434
AddressSpace
Definition: NVPTXBaseInfo.h:22
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1342
unsigned Linkage
Definition: GlobalValue.h:93
void copyAttributesFrom(const GlobalValue *Src) override
Copy all additional attributes (those not needed to create a GlobalValue) from the GlobalValue Src to...
Definition: Globals.cpp:108
ValueTy
Concrete subclass of this.
Definition: Value.h:415
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:48
void setGlobalValueSubClassData(unsigned V)
Definition: GlobalValue.h:152
static bool classof(const Value *V)
Definition: GlobalObject.h:147
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1391
void setMetadata(unsigned KindID, MDNode *MD)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1381
static const unsigned GlobalValueSubClassDataBits
Definition: GlobalValue.h:89
unsigned getGlobalValueSubClassData() const
Definition: GlobalValue.h:149
const unsigned Kind
void eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1353
LLVM Value Representation.
Definition: Value.h:71
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
Root of the metadata hierarchy.
Definition: Metadata.h:55
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:173
void setGlobalObjectSubClassData(unsigned Val)
Definition: Globals.cpp:101