1 //===- llvm/GlobalIndirectSymbol.h - GlobalIndirectSymbol 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 GlobalIndirectSymbol class, which 10 // is a base class for GlobalAlias and GlobalIFunc. It contains all common code 11 // for aliases and ifuncs. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_IR_GLOBALINDIRECTSYMBOL_H 16 #define LLVM_IR_GLOBALINDIRECTSYMBOL_H 17 18 #include "llvm/IR/GlobalObject.h" 19 #include "llvm/IR/GlobalValue.h" 20 #include "llvm/IR/OperandTraits.h" 21 #include "llvm/IR/User.h" 22 #include "llvm/IR/Value.h" 23 #include "llvm/Support/Casting.h" 24 #include <cstddef> 25 26 namespace llvm { 27 28 class GlobalIndirectSymbol : public GlobalValue { 29 protected: 30 GlobalIndirectSymbol(Type *Ty, ValueTy VTy, unsigned AddressSpace, 31 LinkageTypes Linkage, const Twine &Name, Constant *Symbol); 32 33 public: 34 GlobalIndirectSymbol(const GlobalIndirectSymbol &) = delete; 35 GlobalIndirectSymbol &operator=(const GlobalIndirectSymbol &) = delete; 36 37 // allocate space for exactly one operand new(size_t S)38 void *operator new(size_t S) { return User::operator new(S, 1); } delete(void * Ptr)39 void operator delete(void *Ptr) { User::operator delete(Ptr); } 40 41 /// Provide fast operand accessors 42 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 43 copyAttributesFrom(const GlobalValue * Src)44 void copyAttributesFrom(const GlobalValue *Src) { 45 GlobalValue::copyAttributesFrom(Src); 46 } 47 48 /// These methods set and retrieve indirect symbol. setIndirectSymbol(Constant * Symbol)49 void setIndirectSymbol(Constant *Symbol) { 50 setOperand(0, Symbol); 51 } getIndirectSymbol()52 const Constant *getIndirectSymbol() const { 53 return getOperand(0); 54 } getIndirectSymbol()55 Constant *getIndirectSymbol() { 56 return const_cast<Constant *>( 57 static_cast<const GlobalIndirectSymbol *>(this)->getIndirectSymbol()); 58 } 59 60 const GlobalObject *getBaseObject() const; getBaseObject()61 GlobalObject *getBaseObject() { 62 return const_cast<GlobalObject *>( 63 static_cast<const GlobalIndirectSymbol *>(this)->getBaseObject()); 64 } 65 getBaseObject(const DataLayout & DL,APInt & Offset)66 const GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) const { 67 return dyn_cast<GlobalObject>( 68 getIndirectSymbol()->stripAndAccumulateInBoundsConstantOffsets(DL, 69 Offset)); 70 } getBaseObject(const DataLayout & DL,APInt & Offset)71 GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) { 72 return const_cast<GlobalObject *>( 73 static_cast<const GlobalIndirectSymbol *>(this) 74 ->getBaseObject(DL, Offset)); 75 } 76 77 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Value * V)78 static bool classof(const Value *V) { 79 return V->getValueID() == Value::GlobalAliasVal || 80 V->getValueID() == Value::GlobalIFuncVal; 81 } 82 }; 83 84 template <> 85 struct OperandTraits<GlobalIndirectSymbol> : 86 public FixedNumOperandTraits<GlobalIndirectSymbol, 1> { 87 }; 88 89 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalIndirectSymbol, Constant) 90 91 } // end namespace llvm 92 93 #endif // LLVM_IR_GLOBALINDIRECTSYMBOL_H 94