Inherit CallGraphSCCPass directly from Pass.
[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   enum LinkageTypes {
31     ExternalLinkage,     /// Externally visible function
32     LinkOnceLinkage,     /// Keep one copy of function when linking (inline)
33     WeakLinkage,         /// Keep one copy of named function when linking (weak)
34     AppendingLinkage,    /// Special purpose, only applies to global arrays
35     InternalLinkage,     /// Rename collisions when linking (static functions)
36     DLLImportLinkage,    /// Function to be imported from DLL
37     DLLExportLinkage,    /// Function to be accessible from DLL
38     ExternalWeakLinkage, /// ExternalWeak linkage description
39     GhostLinkage         /// Stand-in functions for streaming fns from BC files    
40   };
41   enum VisibilityTypes {
42     DefaultVisibility,
43     HiddenVisibility
44   };
45 protected:
46   GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
47               LinkageTypes linkage, const std::string &name = "")
48     : Constant(Ty, vty, Ops, NumOps, name), Parent(0),
49       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) { }
50
51   Module *Parent;
52   LinkageTypes Linkage;   // The linkage of this global
53   VisibilityTypes Visibility;  // The visibility style of this global
54   unsigned Alignment;     // Alignment of this symbol, must be power of two
55   std::string Section;    // Section to emit this into, empty mean default
56 public:
57   ~GlobalValue() {
58     removeDeadConstantUsers();   // remove any dead constants using this.
59   }
60
61   unsigned getAlignment() const { return Alignment; }
62   void setAlignment(unsigned Align) {
63     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
64     Alignment = Align;
65   }
66
67   VisibilityTypes getVisibility() const { return Visibility; }
68   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
69   void setVisibility(VisibilityTypes V) { Visibility = V; }
70   
71   bool hasSection() const { return !Section.empty(); }
72   const std::string &getSection() const { return Section; }
73   void setSection(const std::string &S) { Section = S; }
74   
75   /// If the usage is empty (except transitively dead constants), then this
76   /// global value can can be safely deleted since the destructor will
77   /// delete the dead constants as well.
78   /// @brief Determine if the usage of this global value is empty except
79   /// for transitively dead constants.
80   bool use_empty_except_constants();
81
82   /// getType - Global values are always pointers.
83   inline const PointerType *getType() const {
84     return reinterpret_cast<const PointerType*>(User::getType());
85   }
86
87   bool hasExternalLinkage()   const { return Linkage == ExternalLinkage; }
88   bool hasLinkOnceLinkage()   const { return Linkage == LinkOnceLinkage; }
89   bool hasWeakLinkage()       const { return Linkage == WeakLinkage; }
90   bool hasAppendingLinkage()  const { return Linkage == AppendingLinkage; }
91   bool hasInternalLinkage()   const { return Linkage == InternalLinkage; }
92   bool hasDLLImportLinkage()  const { return Linkage == DLLImportLinkage; }
93   bool hasDLLExportLinkage()  const { return Linkage == DLLExportLinkage; }
94   bool hasExternalWeakLinkage()  const { return Linkage == ExternalWeakLinkage; }
95   void setLinkage(LinkageTypes LT) { Linkage = LT; }
96   LinkageTypes getLinkage() const { return Linkage; }
97
98   /// hasNotBeenReadFromBytecode - If a module provider is being used to lazily
99   /// stream in functions from disk, this method can be used to check to see if
100   /// the function has been read in yet or not.  Unless you are working on the
101   /// JIT or something else that streams stuff in lazily, you don't need to
102   /// worry about this.
103   bool hasNotBeenReadFromBytecode() const { return Linkage == GhostLinkage; }
104
105   /// Override from Constant class. No GlobalValue's are null values so this
106   /// always returns false.
107   virtual bool isNullValue() const { return false; }
108
109   /// Override from Constant class.
110   virtual void destroyConstant();
111
112   /// isExternal - Return true if the primary definition of this global value is
113   /// outside of the current translation unit...
114   virtual bool isExternal() const = 0;
115
116   /// getParent - Get the module that this global value is contained inside
117   /// of...
118   inline Module *getParent() { return Parent; }
119   inline const Module *getParent() const { return Parent; }
120
121   /// removeDeadConstantUsers - If there are any dead constant users dangling
122   /// off of this global value, remove them.  This method is useful for clients
123   /// that want to check to see if a global is unused, but don't want to deal
124   /// with potentially dead constants hanging off of the globals.
125   ///
126   /// This method tries to make the global dead.  If it detects a user that
127   /// would prevent it from becoming completely dead, it gives up early,
128   /// potentially leaving some dead constant users around.
129   void removeDeadConstantUsers();
130
131   // Methods for support type inquiry through isa, cast, and dyn_cast:
132   static inline bool classof(const GlobalValue *) { return true; }
133   static inline bool classof(const Value *V) {
134     return V->getValueType() == Value::FunctionVal ||
135            V->getValueType() == Value::GlobalVariableVal;
136   }
137 };
138
139 } // End llvm namespace
140
141 #endif