LLVM  13.0.0git
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 
22 namespace llvm {
23 
24 class Comdat;
25 class MDNode;
26 class Metadata;
27 
28 class GlobalObject : public GlobalValue {
29 public:
30  // VCallVisibility - values for visibility metadata attached to vtables. This
31  // describes the scope in which a virtual call could end up being dispatched
32  // through this vtable.
34  // Type is potentially visible to external code.
36  // Type is only visible to code which will be in the current Module after
37  // LTO internalization.
39  // Type is only visible to code in the current Module.
41  };
42 
43 protected:
44  GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
46  unsigned AddressSpace = 0)
47  : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace),
48  ObjComdat(nullptr) {
50  }
51 
53  enum {
56 
58  };
59  static const unsigned GlobalObjectSubClassDataBits =
61 
62 private:
63  static const unsigned AlignmentBits = LastAlignmentBit + 1;
64  static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
65  static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
66 
67 public:
68  GlobalObject(const GlobalObject &) = delete;
69 
70  /// FIXME: Remove this function once transition to Align is over.
71  unsigned getAlignment() const {
73  return Align ? Align->value() : 0;
74  }
75 
76  /// Returns the alignment of the given variable or function.
77  ///
78  /// Note that for functions this is the alignment of the code, not the
79  /// alignment of a function pointer.
80  MaybeAlign getAlign() const {
81  unsigned Data = getGlobalValueSubClassData();
82  unsigned AlignmentData = Data & AlignmentMask;
83  return decodeMaybeAlign(AlignmentData);
84  }
85 
87 
88  unsigned getGlobalObjectSubClassData() const {
89  unsigned ValueData = getGlobalValueSubClassData();
90  return ValueData >> GlobalObjectBits;
91  }
92 
93  void setGlobalObjectSubClassData(unsigned Val) {
94  unsigned OldData = getGlobalValueSubClassData();
95  setGlobalValueSubClassData((OldData & GlobalObjectMask) |
96  (Val << GlobalObjectBits));
97  assert(getGlobalObjectSubClassData() == Val && "representation error");
98  }
99 
100  /// Check if this global has a custom object file section.
101  ///
102  /// This is more efficient than calling getSection() and checking for an empty
103  /// string.
104  bool hasSection() const {
106  }
107 
108  /// Get the custom section of this global if it has one.
109  ///
110  /// If this global does not have a custom section, this will be empty and the
111  /// default object file section (.text, .data, etc) will be used.
113  return hasSection() ? getSectionImpl() : StringRef();
114  }
115 
116  /// Change the section for this global.
117  ///
118  /// Setting the section to the empty string tells LLVM to choose an
119  /// appropriate default object file section.
120  void setSection(StringRef S);
121 
122  bool hasComdat() const { return getComdat() != nullptr; }
123  const Comdat *getComdat() const { return ObjComdat; }
124  Comdat *getComdat() { return ObjComdat; }
125  void setComdat(Comdat *C) { ObjComdat = C; }
126 
127  using Value::addMetadata;
128  using Value::clearMetadata;
129  using Value::eraseMetadata;
130  using Value::getAllMetadata;
131  using Value::getMetadata;
132  using Value::hasMetadata;
133  using Value::setMetadata;
134 
135  /// Copy metadata from Src, adjusting offsets by Offset.
136  void copyMetadata(const GlobalObject *Src, unsigned Offset);
137 
138  void addTypeMetadata(unsigned Offset, Metadata *TypeID);
141 
142  /// Returns true if the alignment of the value can be unilaterally
143  /// increased.
144  ///
145  /// Note that for functions this is the alignment of the code, not the
146  /// alignment of a function pointer.
147  bool canIncreaseAlignment() const;
148 
149 protected:
150  void copyAttributesFrom(const GlobalObject *Src);
151 
152 public:
153  // Methods for support type inquiry through isa, cast, and dyn_cast:
154  static bool classof(const Value *V) {
155  return V->getValueID() == Value::FunctionVal ||
156  V->getValueID() == Value::GlobalVariableVal;
157  }
158 
159 private:
160  void setGlobalObjectFlag(unsigned Bit, bool Val) {
161  unsigned Mask = 1 << Bit;
163  (Val ? Mask : 0u));
164  }
165 
166  StringRef getSectionImpl() const;
167 };
168 
169 } // end namespace llvm
170 
171 #endif // LLVM_IR_GLOBALOBJECT_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::GlobalObject::getComdat
Comdat * getComdat()
Definition: GlobalObject.h:124
llvm::GlobalObject::setGlobalObjectSubClassData
void setGlobalObjectSubClassData(unsigned Val)
Definition: GlobalObject.h:93
llvm::GlobalObject::GlobalObjectBits
@ GlobalObjectBits
Definition: GlobalObject.h:57
llvm::GlobalValue::Visibility
unsigned Visibility
Definition: GlobalValue.h:95
llvm::GlobalObject::VCallVisibilityTranslationUnit
@ VCallVisibilityTranslationUnit
Definition: GlobalObject.h:40
StringRef.h
llvm::GlobalObject::classof
static bool classof(const Value *V)
Definition: GlobalObject.h:154
llvm::GlobalObject::getSection
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:112
llvm::GlobalObject::getAlign
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:80
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::GlobalObject::GlobalObject
GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace=0)
Definition: GlobalObject.h:44
llvm::GlobalObject::LastAlignmentBit
@ LastAlignmentBit
Definition: GlobalObject.h:54
llvm::GlobalValue::LinkageTypes
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
llvm::BitmaskEnumDetail::Mask
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::GlobalValue::GlobalValueSubClassDataBits
static const unsigned GlobalValueSubClassDataBits
Definition: GlobalValue.h:90
llvm::GlobalObject::GlobalObjectSubClassDataBits
static const unsigned GlobalObjectSubClassDataBits
Definition: GlobalObject.h:59
GlobalValue.h
llvm::GlobalObject::setSection
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:212
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::GlobalObject
Definition: GlobalObject.h:28
llvm::GlobalObject::setVCallVisibilityMetadata
void setVCallVisibilityMetadata(VCallVisibility Visibility)
Definition: Metadata.cpp:1507
llvm::GlobalObject::getComdat
const Comdat * getComdat() const
Definition: GlobalObject.h:123
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:109
llvm::Value::getValueID
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:529
llvm::GlobalObject::getGlobalObjectSubClassData
unsigned getGlobalObjectSubClassData() const
Definition: GlobalObject.h:88
llvm::GlobalObject::VCallVisibilityPublic
@ VCallVisibilityPublic
Definition: GlobalObject.h:35
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::Comdat
Definition: Comdat.h:31
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::AddressSpace
AddressSpace
Definition: NVPTXBaseInfo.h:21
TypeID
Type::TypeID TypeID
Definition: Mips16HardFloat.cpp:102
llvm::GlobalObject::hasSection
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:104
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Value::addMetadata
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1268
llvm::GlobalObject::canIncreaseAlignment
bool canIncreaseAlignment() const
Returns true if the alignment of the value can be unilaterally increased.
Definition: Globals.cpp:242
llvm::GlobalObject::copyMetadata
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1457
llvm::GlobalObject::setComdat
void setComdat(Comdat *C)
Definition: GlobalObject.h:125
llvm::GlobalValue::Linkage
unsigned Linkage
Definition: GlobalValue.h:94
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::GlobalObject::getAlignment
unsigned getAlignment() const
FIXME: Remove this function once transition to Align is over.
Definition: GlobalObject.h:71
llvm::Value::clearMetadata
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1291
llvm::GlobalObject::hasComdat
bool hasComdat() const
Definition: GlobalObject.h:122
llvm::Value::ValueTy
ValueTy
Concrete subclass of this.
Definition: Value.h:510
llvm::GlobalValue::getGlobalValueSubClassData
unsigned getGlobalValueSubClassData() const
Definition: GlobalValue.h:158
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Value::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1197
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Value::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1234
llvm::Value::eraseMetadata
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1279
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
Alignment.h
llvm::GlobalObject::VCallVisibility
VCallVisibility
Definition: GlobalObject.h:33
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::GlobalObject::addTypeMetadata
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1498
llvm::Align::value
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
llvm::GlobalObject::HasSectionHashEntryBit
@ HasSectionHashEntryBit
Definition: GlobalObject.h:55
llvm::GlobalValue::setGlobalValueSubClassData
void setGlobalValueSubClassData(unsigned V)
Definition: GlobalValue.h:161
llvm::decodeMaybeAlign
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition: Alignment.h:235
LineType::Metadata
@ Metadata
llvm::GlobalObject::getVCallVisibility
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1517
llvm::tgtok::Bit
@ Bit
Definition: TGLexer.h:50
llvm::GlobalObject::copyAttributesFrom
void copyAttributesFrom(const GlobalObject *Src)
Definition: Globals.cpp:127
llvm::Value::getAllMetadata
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1223
llvm::GlobalObject::ObjComdat
Comdat * ObjComdat
Definition: GlobalObject.h:52
llvm::GlobalObject::setAlignment
void setAlignment(MaybeAlign Align)
Definition: Globals.cpp:117
Value.h
llvm::Value::hasMetadata
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition: Value.h:585
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::GlobalObject::VCallVisibilityLinkageUnit
@ VCallVisibilityLinkageUnit
Definition: GlobalObject.h:38
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44