1 //===-- Address.h - An aligned address -------------------------*- 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 class provides a simple wrapper for a pair of a pointer and an
10 // alignment.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
15 #define LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
16 
17 #include "llvm/IR/Constants.h"
18 #include "clang/AST/CharUnits.h"
19 
20 namespace clang {
21 namespace CodeGen {
22 
23 /// An aligned address.
24 class Address {
25   llvm::Value *Pointer;
26   llvm::Type *ElementType;
27   CharUnits Alignment;
28 
29 protected:
30   Address(std::nullptr_t) : Pointer(nullptr), ElementType(nullptr) {}
31 
32 public:
33   Address(llvm::Value *pointer, llvm::Type *elementType, CharUnits alignment)
34       : Pointer(pointer), ElementType(elementType), Alignment(alignment) {
35     assert(pointer != nullptr && "Pointer cannot be null");
36     assert(elementType != nullptr && "Element type cannot be null");
37     assert(llvm::cast<llvm::PointerType>(pointer->getType())
38                ->isOpaqueOrPointeeTypeMatches(elementType) &&
39            "Incorrect pointer element type");
40     assert(!alignment.isZero() && "Alignment cannot be zero");
41   }
42 
43   // Deprecated: Use constructor with explicit element type instead.
44   Address(llvm::Value *Pointer, CharUnits Alignment)
45       : Address(Pointer, Pointer->getType()->getPointerElementType(),
46                 Alignment) {}
47 
48   static Address invalid() { return Address(nullptr); }
49   bool isValid() const { return Pointer != nullptr; }
50 
51   llvm::Value *getPointer() const {
52     assert(isValid());
53     return Pointer;
54   }
55 
56   /// Return the type of the pointer value.
57   llvm::PointerType *getType() const {
58     return llvm::cast<llvm::PointerType>(getPointer()->getType());
59   }
60 
61   /// Return the type of the values stored in this address.
62   llvm::Type *getElementType() const {
63     assert(isValid());
64     return ElementType;
65   }
66 
67   /// Return the address space that this address resides in.
68   unsigned getAddressSpace() const {
69     return getType()->getAddressSpace();
70   }
71 
72   /// Return the IR name of the pointer value.
73   llvm::StringRef getName() const {
74     return getPointer()->getName();
75   }
76 
77   /// Return the alignment of this pointer.
78   CharUnits getAlignment() const {
79     assert(isValid());
80     return Alignment;
81   }
82 };
83 
84 /// A specialization of Address that requires the address to be an
85 /// LLVM Constant.
86 class ConstantAddress : public Address {
87   ConstantAddress(std::nullptr_t) : Address(nullptr) {}
88 
89 public:
90   ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType,
91                   CharUnits alignment)
92       : Address(pointer, elementType, alignment) {}
93 
94   static ConstantAddress invalid() {
95     return ConstantAddress(nullptr);
96   }
97 
98   llvm::Constant *getPointer() const {
99     return llvm::cast<llvm::Constant>(Address::getPointer());
100   }
101 
102   ConstantAddress getElementBitCast(llvm::Type *ElemTy) const {
103     llvm::Constant *BitCast = llvm::ConstantExpr::getBitCast(
104         getPointer(), ElemTy->getPointerTo(getAddressSpace()));
105     return ConstantAddress(BitCast, ElemTy, getAlignment());
106   }
107 
108   static bool isaImpl(Address addr) {
109     return llvm::isa<llvm::Constant>(addr.getPointer());
110   }
111   static ConstantAddress castImpl(Address addr) {
112     return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()),
113                            addr.getElementType(), addr.getAlignment());
114   }
115 };
116 
117 }
118 
119 // Present a minimal LLVM-like casting interface.
120 template <class U> inline U cast(CodeGen::Address addr) {
121   return U::castImpl(addr);
122 }
123 template <class U> inline bool isa(CodeGen::Address addr) {
124   return U::isaImpl(addr);
125 }
126 
127 }
128 
129 #endif
130