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