LLVM API Documentation

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, Use *Ops, unsigned NumOps,
00040               LinkageTypes linkage, const std::string &name = "")
00041     : Constant(Ty, vty, Ops, NumOps, name), 
00042       Parent(0), Linkage(linkage), Alignment(0) { }
00043 
00044   Module *Parent;
00045   LinkageTypes Linkage;   // The linkage of this global
00046   unsigned Alignment;     // Alignment of this symbol, must be power of two
00047   std::string Section;    // Section to emit this into, empty mean default
00048 public:
00049   ~GlobalValue() {
00050     removeDeadConstantUsers();   // remove any dead constants using this.
00051   }
00052 
00053   unsigned getAlignment() const { return Alignment; }
00054   void setAlignment(unsigned Align) {
00055     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
00056     Alignment = Align;
00057   }
00058   
00059   bool hasSection() const { return !Section.empty(); }
00060   const std::string &getSection() const { return Section; }
00061   void setSection(const std::string &S) { Section = S; }
00062   
00063   /// If the usage is empty (except transitively dead constants), then this
00064   /// global value can can be safely deleted since the destructor will
00065   /// delete the dead constants as well.
00066   /// @brief Determine if the usage of this global value is empty except
00067   /// for transitively dead constants.
00068   bool use_empty_except_constants();
00069 
00070   /// getType - Global values are always pointers.
00071   inline const PointerType *getType() const {
00072     return reinterpret_cast<const PointerType*>(User::getType());
00073   }
00074 
00075   bool hasExternalLinkage()  const { return Linkage == ExternalLinkage; }
00076   bool hasLinkOnceLinkage()  const { return Linkage == LinkOnceLinkage; }
00077   bool hasWeakLinkage()      const { return Linkage == WeakLinkage; }
00078   bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
00079   bool hasInternalLinkage()  const { return Linkage == InternalLinkage; }
00080   void setLinkage(LinkageTypes LT) { Linkage = LT; }
00081   LinkageTypes getLinkage() const { return Linkage; }
00082 
00083   /// hasNotBeenReadFromBytecode - If a module provider is being used to lazily
00084   /// stream in functions from disk, this method can be used to check to see if
00085   /// the function has been read in yet or not.  Unless you are working on the
00086   /// JIT or something else that streams stuff in lazily, you don't need to
00087   /// worry about this.
00088   bool hasNotBeenReadFromBytecode() const { return Linkage == GhostLinkage; }
00089 
00090   /// Override from Constant class. No GlobalValue's are null values so this
00091   /// always returns false.
00092   virtual bool isNullValue() const { return false; }
00093 
00094   /// Override from Constant class.
00095   virtual void destroyConstant();
00096 
00097   /// isExternal - Return true if the primary definition of this global value is
00098   /// outside of the current translation unit...
00099   virtual bool isExternal() const = 0;
00100 
00101   /// getParent - Get the module that this global value is contained inside
00102   /// of...
00103   inline Module *getParent() { return Parent; }
00104   inline const Module *getParent() const { return Parent; }
00105 
00106   /// removeDeadConstantUsers - If there are any dead constant users dangling
00107   /// off of this global value, remove them.  This method is useful for clients
00108   /// that want to check to see if a global is unused, but don't want to deal
00109   /// with potentially dead constants hanging off of the globals.
00110   ///
00111   /// This method tries to make the global dead.  If it detects a user that
00112   /// would prevent it from becoming completely dead, it gives up early,
00113   /// potentially leaving some dead constant users around.
00114   void removeDeadConstantUsers();
00115 
00116   // Methods for support type inquiry through isa, cast, and dyn_cast:
00117   static inline bool classof(const GlobalValue *) { return true; }
00118   static inline bool classof(const Value *V) {
00119     return V->getValueType() == Value::FunctionVal ||
00120            V->getValueType() == Value::GlobalVariableVal;
00121   }
00122 };
00123 
00124 } // End llvm namespace
00125 
00126 #endif