xref: /llvm-project/llvm/include/llvm/IR/GlobalAlias.h (revision e3f936eb755d9ae37019ffcc7f53d71d2d58d188)
1 //===-------- llvm/GlobalAlias.h - GlobalAlias class ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the declaration of the GlobalAlias class, which
10 // represents a single function or variable alias in the IR.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_GLOBALALIAS_H
15 #define LLVM_IR_GLOBALALIAS_H
16 
17 #include "llvm/ADT/ilist_node.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/IR/OperandTraits.h"
20 #include "llvm/IR/Value.h"
21 
22 namespace llvm {
23 
24 class Twine;
25 class Module;
26 template <typename ValueSubClass, typename... Args> class SymbolTableListTraits;
27 
28 class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
29   friend class SymbolTableListTraits<GlobalAlias>;
30 
31   constexpr static IntrusiveOperandsAllocMarker AllocMarker{1};
32 
33   GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage,
34               const Twine &Name, Constant *Aliasee, Module *Parent);
35 
36 public:
37   GlobalAlias(const GlobalAlias &) = delete;
38   GlobalAlias &operator=(const GlobalAlias &) = delete;
39 
40   /// If a parent module is specified, the alias is automatically inserted into
41   /// the end of the specified module's alias list.
42   static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
43                              LinkageTypes Linkage, const Twine &Name,
44                              Constant *Aliasee, Module *Parent);
45 
46   // Without the Aliasee.
47   static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
48                              LinkageTypes Linkage, const Twine &Name,
49                              Module *Parent);
50 
51   // The module is taken from the Aliasee.
52   static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
53                              LinkageTypes Linkage, const Twine &Name,
54                              GlobalValue *Aliasee);
55 
56   // Type, Parent and AddressSpace taken from the Aliasee.
57   static GlobalAlias *create(LinkageTypes Linkage, const Twine &Name,
58                              GlobalValue *Aliasee);
59 
60   // Linkage, Type, Parent and AddressSpace taken from the Aliasee.
61   static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee);
62 
63   // allocate space for exactly one operand
64   void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
65   void operator delete(void *Ptr) { User::operator delete(Ptr); }
66 
67   /// Provide fast operand accessors
68   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
69 
70   void copyAttributesFrom(const GlobalAlias *Src) {
71     GlobalValue::copyAttributesFrom(Src);
72   }
73 
74   /// removeFromParent - This method unlinks 'this' from the containing module,
75   /// but does not delete it.
76   ///
77   void removeFromParent();
78 
79   /// eraseFromParent - This method unlinks 'this' from the containing module
80   /// and deletes it.
81   ///
82   void eraseFromParent();
83 
84   /// These methods retrieve and set alias target.
85   void setAliasee(Constant *Aliasee);
86   const Constant *getAliasee() const {
87     return static_cast<Constant *>(Op<0>().get());
88   }
89   Constant *getAliasee() { return static_cast<Constant *>(Op<0>().get()); }
90 
91   const GlobalObject *getAliaseeObject() const;
92   GlobalObject *getAliaseeObject() {
93     return const_cast<GlobalObject *>(
94         static_cast<const GlobalAlias *>(this)->getAliaseeObject());
95   }
96 
97   static bool isValidLinkage(LinkageTypes L) {
98     return isExternalLinkage(L) || isLocalLinkage(L) || isWeakLinkage(L) ||
99            isLinkOnceLinkage(L) || isAvailableExternallyLinkage(L);
100   }
101 
102   // Methods for support type inquiry through isa, cast, and dyn_cast:
103   static bool classof(const Value *V) {
104     return V->getValueID() == Value::GlobalAliasVal;
105   }
106 };
107 
108 template <>
109 struct OperandTraits<GlobalAlias>
110     : public FixedNumOperandTraits<GlobalAlias, 1> {};
111 
112 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalAlias, Constant)
113 
114 } // end namespace llvm
115 
116 #endif // LLVM_IR_GLOBALALIAS_H
117