xref: /llvm-project/llvm/include/llvm/IR/GlobalObject.h (revision e3f936eb755d9ae37019ffcc7f53d71d2d58d188)
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 Metadata;
26 
27 class GlobalObject : public GlobalValue {
28 public:
29   // VCallVisibility - values for visibility metadata attached to vtables. This
30   // describes the scope in which a virtual call could end up being dispatched
31   // through this vtable.
32   enum VCallVisibility {
33     // Type is potentially visible to external code.
34     VCallVisibilityPublic = 0,
35     // Type is only visible to code which will be in the current Module after
36     // LTO internalization.
37     VCallVisibilityLinkageUnit = 1,
38     // Type is only visible to code in the current Module.
39     VCallVisibilityTranslationUnit = 2,
40   };
41 
42 protected:
43   GlobalObject(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage,
44                const Twine &Name, unsigned AddressSpace = 0)
45       : GlobalValue(Ty, VTy, AllocInfo, Linkage, Name, AddressSpace) {
46     setGlobalValueSubClassData(0);
47   }
48   ~GlobalObject();
49 
50   Comdat *ObjComdat = nullptr;
51   enum {
52     LastAlignmentBit = 5,
53     LastCodeModelBit = 8,
54     HasSectionHashEntryBit,
55 
56     GlobalObjectBits,
57   };
58   static const unsigned GlobalObjectSubClassDataBits =
59       GlobalValueSubClassDataBits - GlobalObjectBits;
60 
61 private:
62   static const unsigned AlignmentBits = LastAlignmentBit + 1;
63   static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
64   static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
65 
66 public:
67   GlobalObject(const GlobalObject &) = delete;
68 
69   /// FIXME: Remove this function once transition to Align is over.
70   uint64_t getAlignment() const {
71     MaybeAlign Align = getAlign();
72     return Align ? Align->value() : 0;
73   }
74 
75   /// Returns the alignment of the given variable or function.
76   ///
77   /// Note that for functions this is the alignment of the code, not the
78   /// alignment of a function pointer.
79   MaybeAlign getAlign() const {
80     unsigned Data = getGlobalValueSubClassData();
81     unsigned AlignmentData = Data & AlignmentMask;
82     return decodeMaybeAlign(AlignmentData);
83   }
84 
85   /// Sets the alignment attribute of the GlobalObject.
86   void setAlignment(Align Align);
87 
88   /// Sets the alignment attribute of the GlobalObject.
89   /// This method will be deprecated as the alignment property should always be
90   /// defined.
91   void setAlignment(MaybeAlign Align);
92 
93   unsigned getGlobalObjectSubClassData() const {
94     unsigned ValueData = getGlobalValueSubClassData();
95     return ValueData >> GlobalObjectBits;
96   }
97 
98   void setGlobalObjectSubClassData(unsigned Val) {
99     unsigned OldData = getGlobalValueSubClassData();
100     setGlobalValueSubClassData((OldData & GlobalObjectMask) |
101                                (Val << GlobalObjectBits));
102     assert(getGlobalObjectSubClassData() == Val && "representation error");
103   }
104 
105   /// Check if this global has a custom object file section.
106   ///
107   /// This is more efficient than calling getSection() and checking for an empty
108   /// string.
109   bool hasSection() const {
110     return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
111   }
112 
113   /// Get the custom section of this global if it has one.
114   ///
115   /// If this global does not have a custom section, this will be empty and the
116   /// default object file section (.text, .data, etc) will be used.
117   StringRef getSection() const {
118     return hasSection() ? getSectionImpl() : StringRef();
119   }
120 
121   /// Change the section for this global.
122   ///
123   /// Setting the section to the empty string tells LLVM to choose an
124   /// appropriate default object file section.
125   void setSection(StringRef S);
126 
127   bool hasComdat() const { return getComdat() != nullptr; }
128   const Comdat *getComdat() const { return ObjComdat; }
129   Comdat *getComdat() { return ObjComdat; }
130   void setComdat(Comdat *C);
131 
132   using Value::addMetadata;
133   using Value::clearMetadata;
134   using Value::eraseMetadata;
135   using Value::eraseMetadataIf;
136   using Value::getAllMetadata;
137   using Value::getMetadata;
138   using Value::hasMetadata;
139   using Value::setMetadata;
140 
141   /// Copy metadata from Src, adjusting offsets by Offset.
142   void copyMetadata(const GlobalObject *Src, unsigned Offset);
143 
144   void addTypeMetadata(unsigned Offset, Metadata *TypeID);
145   void setVCallVisibilityMetadata(VCallVisibility Visibility);
146   VCallVisibility getVCallVisibility() const;
147 
148   /// Returns true if the alignment of the value can be unilaterally
149   /// increased.
150   ///
151   /// Note that for functions this is the alignment of the code, not the
152   /// alignment of a function pointer.
153   bool canIncreaseAlignment() const;
154 
155 protected:
156   void copyAttributesFrom(const GlobalObject *Src);
157 
158 public:
159   // Methods for support type inquiry through isa, cast, and dyn_cast:
160   static bool classof(const Value *V) {
161     return V->getValueID() == Value::FunctionVal ||
162            V->getValueID() == Value::GlobalVariableVal ||
163            V->getValueID() == Value::GlobalIFuncVal;
164   }
165 
166 private:
167   void setGlobalObjectFlag(unsigned Bit, bool Val) {
168     unsigned Mask = 1 << Bit;
169     setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
170                                (Val ? Mask : 0u));
171   }
172 
173   StringRef getSectionImpl() const;
174 };
175 
176 } // end namespace llvm
177 
178 #endif // LLVM_IR_GLOBALOBJECT_H
179