LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GlobalValue.h

Go to the documentation of this file.
00001 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file is a common base class of all globally definable objects.  As such,
00011 // it is subclassed by GlobalVariable and by Function.  This is used because you
00012 // can do certain things with these global objects that you can't do to anything
00013 // else.  For example, use the address of one as a constant.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_GLOBALVALUE_H
00018 #define LLVM_GLOBALVALUE_H
00019 
00020 #include "llvm/Constant.h"
00021 
00022 namespace llvm {
00023 
00024 class PointerType;
00025 class Module;
00026 
00027 class GlobalValue : public Constant {
00028   GlobalValue(const GlobalValue &);             // do not implement
00029 public:
00030   enum LinkageTypes {
00031     ExternalLinkage,   // Externally visible function
00032     LinkOnceLinkage,   // Keep one copy of named function when linking (inline)
00033     WeakLinkage,       // Keep one copy of named function when linking (weak)
00034     AppendingLinkage,  // Special purpose, only applies to global arrays
00035     InternalLinkage,   // Rename collisions when linking (static functions)
00036     GhostLinkage       // Stand-in functions for streaming fns from BC files
00037   };
00038 protected:
00039   GlobalValue(const Type *Ty, ValueTy vty, LinkageTypes linkage,
00040         const std::string &name = "")
00041     : Constant(Ty, vty, name), Linkage(linkage), Parent(0) { }
00042 
00043   LinkageTypes Linkage;   // The linkage of this global
00044   Module *Parent;
00045 public:
00046   ~GlobalValue() {
00047     removeDeadConstantUsers();   // remove any dead constants using this.
00048   }
00049 
00050   /// If the usage is empty (except transitively dead constants), then this
00051   /// global value can can be safely deleted since the destructor will 
00052   /// delete the dead constants as well.
00053   /// @brief Determine if the usage of this global value is empty except 
00054   /// for transitively dead constants.
00055   bool use_empty_except_constants();
00056 
00057   /// getType - Global values are always pointers.
00058   inline const PointerType *getType() const {
00059     return reinterpret_cast<const PointerType*>(User::getType());
00060   }
00061 
00062   bool hasExternalLinkage()  const { return Linkage == ExternalLinkage; }
00063   bool hasLinkOnceLinkage()  const { return Linkage == LinkOnceLinkage; }
00064   bool hasWeakLinkage()      const { return Linkage == WeakLinkage; }
00065   bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
00066   bool hasInternalLinkage()  const { return Linkage == InternalLinkage; }
00067   void setLinkage(LinkageTypes LT) { Linkage = LT; }
00068   LinkageTypes getLinkage() const { return Linkage; }
00069 
00070   /// hasNotBeenReadFromBytecode - If a module provider is being used to lazily
00071   /// stream in functions from disk, this method can be used to check to see if
00072   /// the function has been read in yet or not.  Unless you are working on the
00073   /// JIT or something else that streams stuff in lazily, you don't need to
00074   /// worry about this.
00075   bool hasNotBeenReadFromBytecode() const { return Linkage == GhostLinkage; }
00076 
00077   /// Override from Constant class. No GlobalValue's are null values so this
00078   /// always returns false.
00079   virtual bool isNullValue() const { return false; }
00080 
00081   /// Override from Constant class.
00082   virtual void destroyConstant();
00083 
00084   /// isExternal - Return true if the primary definition of this global value is
00085   /// outside of the current translation unit...
00086   virtual bool isExternal() const = 0;
00087 
00088   /// getParent - Get the module that this global value is contained inside
00089   /// of...
00090   inline Module *getParent() { return Parent; }
00091   inline const Module *getParent() const { return Parent; }
00092 
00093   /// removeDeadConstantUsers - If there are any dead constant users dangling
00094   /// off of this global value, remove them.  This method is useful for clients
00095   /// that want to check to see if a global is unused, but don't want to deal
00096   /// with potentially dead constants hanging off of the globals.
00097   ///
00098   /// This method tries to make the global dead.  If it detects a user that
00099   /// would prevent it from becoming completely dead, it gives up early,
00100   /// potentially leaving some dead constant users around.
00101   void removeDeadConstantUsers();
00102 
00103   // Methods for support type inquiry through isa, cast, and dyn_cast:
00104   static inline bool classof(const GlobalValue *T) { return true; }
00105   static inline bool classof(const Value *V) {
00106     return V->getValueType() == Value::FunctionVal || 
00107            V->getValueType() == Value::GlobalVariableVal;
00108   }
00109 };
00110 
00111 } // End llvm namespace
00112 
00113 #endif