Implement review feedback. Aliasees can be either GlobalValue's or
[oota-llvm.git] / include / llvm / GlobalValue.h
1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a common base class of all globally definable objects.  As such,
11 // it is subclassed by GlobalVariable and by Function.  This is used because you
12 // can do certain things with these global objects that you can't do to anything
13 // else.  For example, use the address of one as a constant.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_GLOBALVALUE_H
18 #define LLVM_GLOBALVALUE_H
19
20 #include "llvm/Constant.h"
21
22 namespace llvm {
23
24 class PointerType;
25 class Module;
26
27 class GlobalValue : public Constant {
28   GlobalValue(const GlobalValue &);             // do not implement
29 public:
30   /// @brief An enumeration for the kinds of linkage for global values.
31   enum LinkageTypes {
32     ExternalLinkage = 0,///< Externally visible function
33     LinkOnceLinkage,    ///< Keep one copy of function when linking (inline)
34     WeakLinkage,        ///< Keep one copy of named function when linking (weak)
35     AppendingLinkage,   ///< Special purpose, only applies to global arrays
36     InternalLinkage,    ///< Rename collisions when linking (static functions)
37     DLLImportLinkage,   ///< Function to be imported from DLL
38     DLLExportLinkage,   ///< Function to be accessible from DLL
39     ExternalWeakLinkage,///< ExternalWeak linkage description
40     GhostLinkage        ///< Stand-in functions for streaming fns from BC files    
41   };
42
43   /// @brief An enumeration for the kinds of visibility of global values.
44   enum VisibilityTypes {
45     DefaultVisibility = 0,  ///< The GV is visible
46     HiddenVisibility        ///< The GV is hidden
47   };
48
49 protected:
50   GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
51               LinkageTypes linkage, const std::string &name = "")
52     : Constant(Ty, vty, Ops, NumOps), Parent(0),
53       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
54     if (!name.empty()) setName(name);
55   }
56
57   Module *Parent;
58   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
59   // Linkage and Visibility from turning into negative values.
60   LinkageTypes Linkage : 5;   // The linkage of this global
61   unsigned Visibility : 1;    // The visibility style of this global
62   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
63   std::string Section;        // Section to emit this into, empty mean default
64 public:
65   ~GlobalValue() {
66     removeDeadConstantUsers();   // remove any dead constants using this.
67   }
68
69   unsigned getAlignment() const { return Alignment; }
70   void setAlignment(unsigned Align) {
71     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
72     Alignment = Align;
73   }
74
75   VisibilityTypes getVisibility() const { return (VisibilityTypes)Visibility; }
76   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
77   void setVisibility(VisibilityTypes V) { Visibility = V; }
78   
79   bool hasSection() const { return !Section.empty(); }
80   const std::string &getSection() const { return Section; }
81   void setSection(const std::string &S) { Section = S; }
82   
83   /// If the usage is empty (except transitively dead constants), then this
84   /// global value can can be safely deleted since the destructor will
85   /// delete the dead constants as well.
86   /// @brief Determine if the usage of this global value is empty except
87   /// for transitively dead constants.
88   bool use_empty_except_constants();
89
90   /// getType - Global values are always pointers.
91   inline const PointerType *getType() const {
92     return reinterpret_cast<const PointerType*>(User::getType());
93   }
94
95   bool hasExternalLinkage()   const { return Linkage == ExternalLinkage; }
96   bool hasLinkOnceLinkage()   const { return Linkage == LinkOnceLinkage; }
97   bool hasWeakLinkage()       const { return Linkage == WeakLinkage; }
98   bool hasAppendingLinkage()  const { return Linkage == AppendingLinkage; }
99   bool hasInternalLinkage()   const { return Linkage == InternalLinkage; }
100   bool hasDLLImportLinkage()  const { return Linkage == DLLImportLinkage; }
101   bool hasDLLExportLinkage()  const { return Linkage == DLLExportLinkage; }
102   bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
103   void setLinkage(LinkageTypes LT) { Linkage = LT; }
104   LinkageTypes getLinkage() const { return Linkage; }
105
106   /// hasNotBeenReadFromBytecode - If a module provider is being used to lazily
107   /// stream in functions from disk, this method can be used to check to see if
108   /// the function has been read in yet or not.  Unless you are working on the
109   /// JIT or something else that streams stuff in lazily, you don't need to
110   /// worry about this.
111   bool hasNotBeenReadFromBytecode() const { return Linkage == GhostLinkage; }
112
113   /// Override from Constant class. No GlobalValue's are null values so this
114   /// always returns false.
115   virtual bool isNullValue() const { return false; }
116
117   /// Override from Constant class.
118   virtual void destroyConstant();
119
120   /// isDeclaration - Return true if the primary definition of this global 
121   /// value is outside of the current translation unit...
122   virtual bool isDeclaration() const = 0;
123
124   /// getParent - Get the module that this global value is contained inside
125   /// of...
126   inline Module *getParent() { return Parent; }
127   inline const Module *getParent() const { return Parent; }
128
129   /// removeDeadConstantUsers - If there are any dead constant users dangling
130   /// off of this global value, remove them.  This method is useful for clients
131   /// that want to check to see if a global is unused, but don't want to deal
132   /// with potentially dead constants hanging off of the globals.
133   void removeDeadConstantUsers();
134
135   // Methods for support type inquiry through isa, cast, and dyn_cast:
136   static inline bool classof(const GlobalValue *) { return true; }
137   static inline bool classof(const Value *V) {
138     return V->getValueID() == Value::FunctionVal ||
139            V->getValueID() == Value::GlobalVariableVal ||
140            V->getValueID() == Value::GlobalAliasVal;
141   }
142 };
143
144 } // End llvm namespace
145
146 #endif