Home | History | Annotate | Download | only in IR
      1 //===-------- llvm/GlobalAlias.h - GlobalAlias class ------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains the declaration of the GlobalAlias class, which
     11 // represents a single function or variable alias in the IR.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_IR_GLOBALALIAS_H
     16 #define LLVM_IR_GLOBALALIAS_H
     17 
     18 #include "llvm/ADT/Twine.h"
     19 #include "llvm/ADT/ilist_node.h"
     20 #include "llvm/IR/GlobalValue.h"
     21 #include "llvm/IR/OperandTraits.h"
     22 
     23 namespace llvm {
     24 
     25 class Module;
     26 template <typename ValueSubClass> class SymbolTableListTraits;
     27 
     28 class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
     29   friend class SymbolTableListTraits<GlobalAlias>;
     30   void operator=(const GlobalAlias &) = delete;
     31   GlobalAlias(const GlobalAlias &) = delete;
     32 
     33   void setParent(Module *parent);
     34 
     35   GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage,
     36               const Twine &Name, Constant *Aliasee, Module *Parent);
     37 
     38 public:
     39   // allocate space for exactly one operand
     40   void *operator new(size_t s) {
     41     return User::operator new(s, 1);
     42   }
     43 
     44   /// If a parent module is specified, the alias is automatically inserted into
     45   /// the end of the specified module's alias list.
     46   static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
     47                              LinkageTypes Linkage, const Twine &Name,
     48                              Constant *Aliasee, Module *Parent);
     49 
     50   // Without the Aliasee.
     51   static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
     52                              LinkageTypes Linkage, const Twine &Name,
     53                              Module *Parent);
     54 
     55   // The module is taken from the Aliasee.
     56   static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
     57                              LinkageTypes Linkage, const Twine &Name,
     58                              GlobalValue *Aliasee);
     59 
     60   // Type, Parent and AddressSpace taken from the Aliasee.
     61   static GlobalAlias *create(LinkageTypes Linkage, const Twine &Name,
     62                              GlobalValue *Aliasee);
     63 
     64   // Linkage, Type, Parent and AddressSpace taken from the Aliasee.
     65   static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee);
     66 
     67   /// Provide fast operand accessors
     68   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
     69 
     70   /// removeFromParent - This method unlinks 'this' from the containing module,
     71   /// but does not delete it.
     72   ///
     73   void removeFromParent() override;
     74 
     75   /// eraseFromParent - This method unlinks 'this' from the containing module
     76   /// and deletes it.
     77   ///
     78   void eraseFromParent() override;
     79 
     80   /// These methods retrive and set alias target.
     81   void setAliasee(Constant *Aliasee);
     82   const Constant *getAliasee() const {
     83     return const_cast<GlobalAlias *>(this)->getAliasee();
     84   }
     85   Constant *getAliasee() {
     86     return getOperand(0);
     87   }
     88 
     89   const GlobalObject *getBaseObject() const {
     90     return const_cast<GlobalAlias *>(this)->getBaseObject();
     91   }
     92   GlobalObject *getBaseObject() {
     93     return dyn_cast<GlobalObject>(getAliasee()->stripInBoundsOffsets());
     94   }
     95 
     96   const GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) const {
     97     return const_cast<GlobalAlias *>(this)->getBaseObject(DL, Offset);
     98   }
     99   GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) {
    100     return dyn_cast<GlobalObject>(
    101         getAliasee()->stripAndAccumulateInBoundsConstantOffsets(DL, Offset));
    102   }
    103 
    104   static bool isValidLinkage(LinkageTypes L) {
    105     return isExternalLinkage(L) || isLocalLinkage(L) ||
    106       isWeakLinkage(L) || isLinkOnceLinkage(L);
    107   }
    108 
    109   // Methods for support type inquiry through isa, cast, and dyn_cast:
    110   static inline bool classof(const Value *V) {
    111     return V->getValueID() == Value::GlobalAliasVal;
    112   }
    113 };
    114 
    115 template <>
    116 struct OperandTraits<GlobalAlias> :
    117   public FixedNumOperandTraits<GlobalAlias, 1> {
    118 };
    119 
    120 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalAlias, Constant)
    121 
    122 } // End llvm namespace
    123 
    124 #endif
    125