xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/Address.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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   Address(llvm::Value *Pointer, CharUnits Alignment)
92       : Address(Pointer, Pointer->getType()->getPointerElementType(),
93                 Alignment) {}
94 
95   static Address invalid() { return Address(nullptr); }
96   bool isValid() const { return A.getPointer() != nullptr; }
97 
98   llvm::Value *getPointer() const {
99     assert(isValid());
100     return A.getPointer();
101   }
102 
103   /// Return the type of the pointer value.
104   llvm::PointerType *getType() const {
105     return llvm::cast<llvm::PointerType>(getPointer()->getType());
106   }
107 
108   /// Return the type of the values stored in this address.
109   llvm::Type *getElementType() const {
110     assert(isValid());
111     return A.getElementType();
112   }
113 
114   /// Return the address space that this address resides in.
115   unsigned getAddressSpace() const {
116     return getType()->getAddressSpace();
117   }
118 
119   /// Return the IR name of the pointer value.
120   llvm::StringRef getName() const {
121     return getPointer()->getName();
122   }
123 
124   /// Return the alignment of this pointer.
125   CharUnits getAlignment() const {
126     assert(isValid());
127     return A.getAlignment();
128   }
129 
130   /// Return address with different pointer, but same element type and
131   /// alignment.
132   Address withPointer(llvm::Value *NewPointer) const {
133     return Address(NewPointer, getElementType(), getAlignment());
134   }
135 
136   /// Return address with different alignment, but same pointer and element
137   /// type.
138   Address withAlignment(CharUnits NewAlignment) const {
139     return Address(getPointer(), getElementType(), NewAlignment);
140   }
141 };
142 
143 /// A specialization of Address that requires the address to be an
144 /// LLVM Constant.
145 class ConstantAddress : public Address {
146   ConstantAddress(std::nullptr_t) : Address(nullptr) {}
147 
148 public:
149   ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType,
150                   CharUnits alignment)
151       : Address(pointer, elementType, alignment) {}
152 
153   static ConstantAddress invalid() {
154     return ConstantAddress(nullptr);
155   }
156 
157   llvm::Constant *getPointer() const {
158     return llvm::cast<llvm::Constant>(Address::getPointer());
159   }
160 
161   ConstantAddress getElementBitCast(llvm::Type *ElemTy) const {
162     llvm::Constant *BitCast = llvm::ConstantExpr::getBitCast(
163         getPointer(), ElemTy->getPointerTo(getAddressSpace()));
164     return ConstantAddress(BitCast, ElemTy, getAlignment());
165   }
166 
167   static bool isaImpl(Address addr) {
168     return llvm::isa<llvm::Constant>(addr.getPointer());
169   }
170   static ConstantAddress castImpl(Address addr) {
171     return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()),
172                            addr.getElementType(), addr.getAlignment());
173   }
174 };
175 
176 }
177 
178 // Present a minimal LLVM-like casting interface.
179 template <class U> inline U cast(CodeGen::Address addr) {
180   return U::castImpl(addr);
181 }
182 template <class U> inline bool isa(CodeGen::Address addr) {
183   return U::isaImpl(addr);
184 }
185 
186 }
187 
188 #endif
189