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 "clang/AST/CharUnits.h" 18 #include "llvm/ADT/PointerIntPair.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/Support/MathExtras.h" 21 22 namespace clang { 23 namespace CodeGen { 24 25 namespace { 26 // We try to save some space by using 6 bits over two PointerIntPairs to store 27 // the alignment. However, some arches don't support 3 bits in a PointerIntPair 28 // so we fallback to storing the alignment separately. 29 template <typename T, bool = alignof(llvm::Value *) >= 8> class AddressImpl {}; 30 31 template <typename T> class AddressImpl<T, false> { 32 llvm::Value *Pointer; 33 llvm::Type *ElementType; 34 CharUnits Alignment; 35 36 public: 37 AddressImpl(llvm::Value *Pointer, llvm::Type *ElementType, 38 CharUnits Alignment) 39 : Pointer(Pointer), ElementType(ElementType), Alignment(Alignment) {} 40 llvm::Value *getPointer() const { return Pointer; } 41 llvm::Type *getElementType() const { return ElementType; } 42 CharUnits getAlignment() const { return Alignment; } 43 }; 44 45 template <typename T> class AddressImpl<T, true> { 46 // Int portion stores upper 3 bits of the log of the alignment. 47 llvm::PointerIntPair<llvm::Value *, 3, unsigned> Pointer; 48 // Int portion stores lower 3 bits of the log of the alignment. 49 llvm::PointerIntPair<llvm::Type *, 3, unsigned> ElementType; 50 51 public: 52 AddressImpl(llvm::Value *Pointer, llvm::Type *ElementType, 53 CharUnits Alignment) 54 : Pointer(Pointer), ElementType(ElementType) { 55 if (Alignment.isZero()) 56 return; 57 // Currently the max supported alignment is much less than 1 << 63 and is 58 // guaranteed to be a power of 2, so we can store the log of the alignment 59 // into 6 bits. 60 assert(Alignment.isPowerOfTwo() && "Alignment cannot be zero"); 61 auto AlignLog = llvm::Log2_64(Alignment.getQuantity()); 62 assert(AlignLog < (1 << 6) && "cannot fit alignment into 6 bits"); 63 this->Pointer.setInt(AlignLog >> 3); 64 this->ElementType.setInt(AlignLog & 7); 65 } 66 llvm::Value *getPointer() const { return Pointer.getPointer(); } 67 llvm::Type *getElementType() const { return ElementType.getPointer(); } 68 CharUnits getAlignment() const { 69 unsigned AlignLog = (Pointer.getInt() << 3) | ElementType.getInt(); 70 return CharUnits::fromQuantity(1UL << AlignLog); 71 } 72 }; 73 } // namespace 74 75 /// An aligned address. 76 class Address { 77 AddressImpl<void> A; 78 79 protected: 80 Address(std::nullptr_t) : A(nullptr, nullptr, CharUnits::Zero()) {} 81 82 public: 83 Address(llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment) 84 : A(Pointer, ElementType, Alignment) { 85 assert(Pointer != nullptr && "Pointer cannot be null"); 86 assert(ElementType != nullptr && "Element type cannot be null"); 87 assert(llvm::cast<llvm::PointerType>(Pointer->getType()) 88 ->isOpaqueOrPointeeTypeMatches(ElementType) && 89 "Incorrect pointer element type"); 90 } 91 92 // Deprecated: Use constructor with explicit element type instead. 93 Address(llvm::Value *Pointer, CharUnits Alignment) 94 : Address(Pointer, Pointer->getType()->getPointerElementType(), 95 Alignment) {} 96 97 static Address invalid() { return Address(nullptr); } 98 bool isValid() const { return A.getPointer() != nullptr; } 99 100 llvm::Value *getPointer() const { 101 assert(isValid()); 102 return A.getPointer(); 103 } 104 105 /// Return the type of the pointer value. 106 llvm::PointerType *getType() const { 107 return llvm::cast<llvm::PointerType>(getPointer()->getType()); 108 } 109 110 /// Return the type of the values stored in this address. 111 llvm::Type *getElementType() const { 112 assert(isValid()); 113 return A.getElementType(); 114 } 115 116 /// Return the address space that this address resides in. 117 unsigned getAddressSpace() const { 118 return getType()->getAddressSpace(); 119 } 120 121 /// Return the IR name of the pointer value. 122 llvm::StringRef getName() const { 123 return getPointer()->getName(); 124 } 125 126 /// Return the alignment of this pointer. 127 CharUnits getAlignment() const { 128 assert(isValid()); 129 return A.getAlignment(); 130 } 131 132 /// Return address with different pointer, but same element type and 133 /// alignment. 134 Address withPointer(llvm::Value *NewPointer) const { 135 return Address(NewPointer, getElementType(), getAlignment()); 136 } 137 138 /// Return address with different alignment, but same pointer and element 139 /// type. 140 Address withAlignment(CharUnits NewAlignment) const { 141 return Address(getPointer(), getElementType(), NewAlignment); 142 } 143 }; 144 145 /// A specialization of Address that requires the address to be an 146 /// LLVM Constant. 147 class ConstantAddress : public Address { 148 ConstantAddress(std::nullptr_t) : Address(nullptr) {} 149 150 public: 151 ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType, 152 CharUnits alignment) 153 : Address(pointer, elementType, alignment) {} 154 155 static ConstantAddress invalid() { 156 return ConstantAddress(nullptr); 157 } 158 159 llvm::Constant *getPointer() const { 160 return llvm::cast<llvm::Constant>(Address::getPointer()); 161 } 162 163 ConstantAddress getElementBitCast(llvm::Type *ElemTy) const { 164 llvm::Constant *BitCast = llvm::ConstantExpr::getBitCast( 165 getPointer(), ElemTy->getPointerTo(getAddressSpace())); 166 return ConstantAddress(BitCast, ElemTy, getAlignment()); 167 } 168 169 static bool isaImpl(Address addr) { 170 return llvm::isa<llvm::Constant>(addr.getPointer()); 171 } 172 static ConstantAddress castImpl(Address addr) { 173 return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()), 174 addr.getElementType(), addr.getAlignment()); 175 } 176 }; 177 178 } 179 180 // Present a minimal LLVM-like casting interface. 181 template <class U> inline U cast(CodeGen::Address addr) { 182 return U::castImpl(addr); 183 } 184 template <class U> inline bool isa(CodeGen::Address addr) { 185 return U::isaImpl(addr); 186 } 187 188 } 189 190 #endif 191