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, typename ItemParentClass> 27 class SymbolTableListTraits; 28 29 class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> { 30 friend class SymbolTableListTraits<GlobalAlias, Module>; 31 void operator=(const GlobalAlias &) = delete; 32 GlobalAlias(const GlobalAlias &) = delete; 33 34 void setParent(Module *parent); 35 36 GlobalAlias(PointerType *Ty, LinkageTypes Linkage, const Twine &Name, 37 Constant *Aliasee, Module *Parent); 38 39 public: 40 // allocate space for exactly one operand new(size_t s)41 void *operator new(size_t s) { 42 return User::operator new(s, 1); 43 } 44 45 /// If a parent module is specified, the alias is automatically inserted into 46 /// the end of the specified module's alias list. 47 static GlobalAlias *create(PointerType *Ty, LinkageTypes Linkage, 48 const Twine &Name, Constant *Aliasee, 49 Module *Parent); 50 51 // Without the Aliasee. 52 static GlobalAlias *create(PointerType *Ty, LinkageTypes Linkage, 53 const Twine &Name, Module *Parent); 54 55 // The module is taken from the Aliasee. 56 static GlobalAlias *create(PointerType *Ty, LinkageTypes Linkage, 57 const Twine &Name, GlobalValue *Aliasee); 58 59 // Type, Parent and AddressSpace taken from the Aliasee. 60 static GlobalAlias *create(LinkageTypes Linkage, const Twine &Name, 61 GlobalValue *Aliasee); 62 63 // Linkage, Type, Parent and AddressSpace taken from the Aliasee. 64 static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee); 65 66 /// Provide fast operand accessors 67 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 68 69 /// removeFromParent - This method unlinks 'this' from the containing module, 70 /// but does not delete it. 71 /// 72 void removeFromParent() override; 73 74 /// eraseFromParent - This method unlinks 'this' from the containing module 75 /// and deletes it. 76 /// 77 void eraseFromParent() override; 78 79 /// These methods retrive and set alias target. 80 void setAliasee(Constant *Aliasee); getAliasee()81 const Constant *getAliasee() const { 82 return const_cast<GlobalAlias *>(this)->getAliasee(); 83 } getAliasee()84 Constant *getAliasee() { 85 return getOperand(0); 86 } 87 getBaseObject()88 const GlobalObject *getBaseObject() const { 89 return const_cast<GlobalAlias *>(this)->getBaseObject(); 90 } getBaseObject()91 GlobalObject *getBaseObject() { 92 return dyn_cast<GlobalObject>(getAliasee()->stripInBoundsOffsets()); 93 } 94 getBaseObject(const DataLayout & DL,APInt & Offset)95 const GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) const { 96 return const_cast<GlobalAlias *>(this)->getBaseObject(DL, Offset); 97 } getBaseObject(const DataLayout & DL,APInt & Offset)98 GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) { 99 return dyn_cast<GlobalObject>( 100 getAliasee()->stripAndAccumulateInBoundsConstantOffsets(DL, Offset)); 101 } 102 isValidLinkage(LinkageTypes L)103 static bool isValidLinkage(LinkageTypes L) { 104 return isExternalLinkage(L) || isLocalLinkage(L) || 105 isWeakLinkage(L) || isLinkOnceLinkage(L); 106 } 107 108 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Value * V)109 static inline bool classof(const Value *V) { 110 return V->getValueID() == Value::GlobalAliasVal; 111 } 112 }; 113 114 template <> 115 struct OperandTraits<GlobalAlias> : 116 public FixedNumOperandTraits<GlobalAlias, 1> { 117 }; 118 119 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalAlias, Constant) 120 121 } // End llvm namespace 122 123 #endif 124