Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:01

0001 //===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This represents an independent object. That is, a function or a global
0010 // variable, but not an alias.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_IR_GLOBALOBJECT_H
0015 #define LLVM_IR_GLOBALOBJECT_H
0016 
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/IR/GlobalValue.h"
0019 #include "llvm/IR/Value.h"
0020 #include "llvm/Support/Alignment.h"
0021 
0022 namespace llvm {
0023 
0024 class Comdat;
0025 class Metadata;
0026 
0027 class GlobalObject : public GlobalValue {
0028 public:
0029   // VCallVisibility - values for visibility metadata attached to vtables. This
0030   // describes the scope in which a virtual call could end up being dispatched
0031   // through this vtable.
0032   enum VCallVisibility {
0033     // Type is potentially visible to external code.
0034     VCallVisibilityPublic = 0,
0035     // Type is only visible to code which will be in the current Module after
0036     // LTO internalization.
0037     VCallVisibilityLinkageUnit = 1,
0038     // Type is only visible to code in the current Module.
0039     VCallVisibilityTranslationUnit = 2,
0040   };
0041 
0042 protected:
0043   GlobalObject(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage,
0044                const Twine &Name, unsigned AddressSpace = 0)
0045       : GlobalValue(Ty, VTy, AllocInfo, Linkage, Name, AddressSpace) {
0046     setGlobalValueSubClassData(0);
0047   }
0048   ~GlobalObject();
0049 
0050   Comdat *ObjComdat = nullptr;
0051   enum {
0052     LastAlignmentBit = 5,
0053     LastCodeModelBit = 8,
0054     HasSectionHashEntryBit,
0055 
0056     GlobalObjectBits,
0057   };
0058   static const unsigned GlobalObjectSubClassDataBits =
0059       GlobalValueSubClassDataBits - GlobalObjectBits;
0060 
0061 private:
0062   static const unsigned AlignmentBits = LastAlignmentBit + 1;
0063   static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
0064   static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
0065 
0066 public:
0067   GlobalObject(const GlobalObject &) = delete;
0068 
0069   /// FIXME: Remove this function once transition to Align is over.
0070   uint64_t getAlignment() const {
0071     MaybeAlign Align = getAlign();
0072     return Align ? Align->value() : 0;
0073   }
0074 
0075   /// Returns the alignment of the given variable or function.
0076   ///
0077   /// Note that for functions this is the alignment of the code, not the
0078   /// alignment of a function pointer.
0079   MaybeAlign getAlign() const {
0080     unsigned Data = getGlobalValueSubClassData();
0081     unsigned AlignmentData = Data & AlignmentMask;
0082     return decodeMaybeAlign(AlignmentData);
0083   }
0084 
0085   /// Sets the alignment attribute of the GlobalObject.
0086   void setAlignment(Align Align);
0087 
0088   /// Sets the alignment attribute of the GlobalObject.
0089   /// This method will be deprecated as the alignment property should always be
0090   /// defined.
0091   void setAlignment(MaybeAlign Align);
0092 
0093   unsigned getGlobalObjectSubClassData() const {
0094     unsigned ValueData = getGlobalValueSubClassData();
0095     return ValueData >> GlobalObjectBits;
0096   }
0097 
0098   void setGlobalObjectSubClassData(unsigned Val) {
0099     unsigned OldData = getGlobalValueSubClassData();
0100     setGlobalValueSubClassData((OldData & GlobalObjectMask) |
0101                                (Val << GlobalObjectBits));
0102     assert(getGlobalObjectSubClassData() == Val && "representation error");
0103   }
0104 
0105   /// Check if this global has a custom object file section.
0106   ///
0107   /// This is more efficient than calling getSection() and checking for an empty
0108   /// string.
0109   bool hasSection() const {
0110     return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
0111   }
0112 
0113   /// Get the custom section of this global if it has one.
0114   ///
0115   /// If this global does not have a custom section, this will be empty and the
0116   /// default object file section (.text, .data, etc) will be used.
0117   StringRef getSection() const {
0118     return hasSection() ? getSectionImpl() : StringRef();
0119   }
0120 
0121   /// Change the section for this global.
0122   ///
0123   /// Setting the section to the empty string tells LLVM to choose an
0124   /// appropriate default object file section.
0125   void setSection(StringRef S);
0126 
0127   bool hasComdat() const { return getComdat() != nullptr; }
0128   const Comdat *getComdat() const { return ObjComdat; }
0129   Comdat *getComdat() { return ObjComdat; }
0130   void setComdat(Comdat *C);
0131 
0132   using Value::addMetadata;
0133   using Value::clearMetadata;
0134   using Value::eraseMetadata;
0135   using Value::eraseMetadataIf;
0136   using Value::getAllMetadata;
0137   using Value::getMetadata;
0138   using Value::hasMetadata;
0139   using Value::setMetadata;
0140 
0141   /// Copy metadata from Src, adjusting offsets by Offset.
0142   void copyMetadata(const GlobalObject *Src, unsigned Offset);
0143 
0144   void addTypeMetadata(unsigned Offset, Metadata *TypeID);
0145   void setVCallVisibilityMetadata(VCallVisibility Visibility);
0146   VCallVisibility getVCallVisibility() const;
0147 
0148   /// Returns true if the alignment of the value can be unilaterally
0149   /// increased.
0150   ///
0151   /// Note that for functions this is the alignment of the code, not the
0152   /// alignment of a function pointer.
0153   bool canIncreaseAlignment() const;
0154 
0155 protected:
0156   void copyAttributesFrom(const GlobalObject *Src);
0157 
0158 public:
0159   // Methods for support type inquiry through isa, cast, and dyn_cast:
0160   static bool classof(const Value *V) {
0161     return V->getValueID() == Value::FunctionVal ||
0162            V->getValueID() == Value::GlobalVariableVal ||
0163            V->getValueID() == Value::GlobalIFuncVal;
0164   }
0165 
0166 private:
0167   void setGlobalObjectFlag(unsigned Bit, bool Val) {
0168     unsigned Mask = 1 << Bit;
0169     setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
0170                                (Val ? Mask : 0u));
0171   }
0172 
0173   StringRef getSectionImpl() const;
0174 };
0175 
0176 } // end namespace llvm
0177 
0178 #endif // LLVM_IR_GLOBALOBJECT_H