LLVM  10.0.0svn
GlobalObject.h
Go to the documentation of this file.
1 //===-- llvm/GlobalObject.h - Class to represent global objects -*- 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 //
9 // This represents an independent object. That is, a function or a global
10 // variable, but not an alias.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_GLOBALOBJECT_H
15 #define LLVM_IR_GLOBALOBJECT_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/IR/Value.h"
20 #include "llvm/Support/Alignment.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  MaybeAlign Align = decodeMaybeAlign(AlignmentData);
63  return Align ? Align->value() : 0;
64  }
65 
66  /// FIXME: Remove this setter once the migration to MaybeAlign is over.
67  void setAlignment(unsigned Align);
68  void setAlignment(MaybeAlign Align);
69 
70  unsigned getGlobalObjectSubClassData() const {
71  unsigned ValueData = getGlobalValueSubClassData();
72  return ValueData >> GlobalObjectBits;
73  }
74 
75  void setGlobalObjectSubClassData(unsigned Val) {
76  unsigned OldData = getGlobalValueSubClassData();
77  setGlobalValueSubClassData((OldData & GlobalObjectMask) |
78  (Val << GlobalObjectBits));
79  assert(getGlobalObjectSubClassData() == Val && "representation error");
80  }
81 
82  /// Check if this global has a custom object file section.
83  ///
84  /// This is more efficient than calling getSection() and checking for an empty
85  /// string.
86  bool hasSection() const {
88  }
89 
90  /// Get the custom section of this global if it has one.
91  ///
92  /// If this global does not have a custom section, this will be empty and the
93  /// default object file section (.text, .data, etc) will be used.
95  return hasSection() ? getSectionImpl() : StringRef();
96  }
97 
98  /// Change the section for this global.
99  ///
100  /// Setting the section to the empty string tells LLVM to choose an
101  /// appropriate default object file section.
102  void setSection(StringRef S);
103 
104  bool hasComdat() const { return getComdat() != nullptr; }
105  const Comdat *getComdat() const { return ObjComdat; }
106  Comdat *getComdat() { return ObjComdat; }
107  void setComdat(Comdat *C) { ObjComdat = C; }
108 
109  /// Check if this has any metadata.
110  bool hasMetadata() const { return hasMetadataHashEntry(); }
111 
112  /// Check if this has any metadata of the given kind.
113  bool hasMetadata(unsigned KindID) const {
114  return getMetadata(KindID) != nullptr;
115  }
116  bool hasMetadata(StringRef Kind) const {
117  return getMetadata(Kind) != nullptr;
118  }
119 
120  /// Get the current metadata attachments for the given kind, if any.
121  ///
122  /// These functions require that the function have at most a single attachment
123  /// of the given kind, and return \c nullptr if such an attachment is missing.
124  /// @{
125  MDNode *getMetadata(unsigned KindID) const;
127  /// @}
128 
129  /// Appends all attachments with the given ID to \c MDs in insertion order.
130  /// If the global has no attachments with the given ID, or if ID is invalid,
131  /// leaves MDs unchanged.
132  /// @{
133  void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
134  void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const;
135  /// @}
136 
137  /// Set a particular kind of metadata attachment.
138  ///
139  /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
140  /// replacing it if it already exists.
141  /// @{
142  void setMetadata(unsigned KindID, MDNode *MD);
143  void setMetadata(StringRef Kind, MDNode *MD);
144  /// @}
145 
146  /// Add a metadata attachment.
147  /// @{
148  void addMetadata(unsigned KindID, MDNode &MD);
149  void addMetadata(StringRef Kind, MDNode &MD);
150  /// @}
151 
152  /// Appends all attachments for the global to \c MDs, sorting by attachment
153  /// ID. Attachments with the same ID appear in insertion order.
154  void
155  getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
156 
157  /// Erase all metadata attachments with the given kind.
158  ///
159  /// \returns true if any metadata was removed.
160  bool eraseMetadata(unsigned KindID);
161 
162  /// Copy metadata from Src, adjusting offsets by Offset.
163  void copyMetadata(const GlobalObject *Src, unsigned Offset);
164 
165  void addTypeMetadata(unsigned Offset, Metadata *TypeID);
166 
167 protected:
168  void copyAttributesFrom(const GlobalObject *Src);
169 
170 public:
171  // Methods for support type inquiry through isa, cast, and dyn_cast:
172  static bool classof(const Value *V) {
173  return V->getValueID() == Value::FunctionVal ||
174  V->getValueID() == Value::GlobalVariableVal;
175  }
176 
177  void clearMetadata();
178 
179 private:
180  void setGlobalObjectFlag(unsigned Bit, bool Val) {
181  unsigned Mask = 1 << Bit;
183  (Val ? Mask : 0u));
184  }
185 
186  bool hasMetadataHashEntry() const {
188  }
189  void setHasMetadataHashEntry(bool HasEntry) {
190  setGlobalObjectFlag(HasMetadataHashEntryBit, HasEntry);
191  }
192 
193  StringRef getSectionImpl() const;
194 };
195 
196 } // end namespace llvm
197 
198 #endif // LLVM_IR_GLOBALOBJECT_H
uint64_t CallInst * C
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:94
unsigned getAlignment() const
Definition: GlobalObject.h:59
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:483
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Metadata node.
Definition: Metadata.h:863
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition: Alignment.h:178
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode *>> &MDs) const
Appends all attachments for the global to MDs, sorting by attachment ID.
Definition: Metadata.cpp:1413
void setAlignment(unsigned Align)
FIXME: Remove this setter once the migration to MaybeAlign is over.
Definition: Globals.cpp:116
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1450
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1440
static const unsigned GlobalObjectSubClassDataBits
Definition: GlobalObject.h:48
void setComdat(Comdat *C)
Definition: GlobalObject.h:107
Comdat * getComdat()
Definition: GlobalObject.h:106
GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace=0)
Definition: GlobalObject.h:32
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1491
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
unsigned getGlobalObjectSubClassData() const
Definition: GlobalObject.h:70
Type::TypeID TypeID
unsigned getGlobalValueSubClassData() const
Definition: GlobalValue.h:164
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
Definition: Alignment.h:85
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:86
AddressSpace
Definition: NVPTXBaseInfo.h:21
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1390
void setGlobalObjectSubClassData(unsigned Val)
Definition: GlobalObject.h:75
unsigned Linkage
Definition: GlobalValue.h:94
ValueTy
Concrete subclass of this.
Definition: Value.h:464
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
void setGlobalValueSubClassData(unsigned V)
Definition: GlobalValue.h:167
static bool classof(const Value *V)
Definition: GlobalObject.h:172
bool hasComdat() const
Definition: GlobalObject.h:104
void setMetadata(unsigned KindID, MDNode *MD)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1430
const Comdat * getComdat() const
Definition: GlobalObject.h:105
static const unsigned GlobalValueSubClassDataBits
Definition: GlobalValue.h:90
bool hasMetadata(StringRef Kind) const
Definition: GlobalObject.h:116
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
bool hasMetadata() const
Check if this has any metadata.
Definition: GlobalObject.h:110
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1401
bool hasMetadata(unsigned KindID) const
Check if this has any metadata of the given kind.
Definition: GlobalObject.h:113
void copyAttributesFrom(const GlobalObject *Src)
Definition: Globals.cpp:130
Root of the metadata hierarchy.
Definition: Metadata.h:57
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:215