clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Type.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/IR/Type.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "llvm/IR/Type.h" |
| 14 | #include "LLVMContextImpl.h" |
| 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/None.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/IR/Constant.h" |
| 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/DerivedTypes.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/Value.h" |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/MathExtras.h" |
| 28 | #include "llvm/Support/TypeSize.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include <cassert> |
| 31 | #include <utility> |
| 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) { |
| 40 | switch (IDNumber) { |
| 41 | case VoidTyID : return getVoidTy(C); |
| 42 | case HalfTyID : return getHalfTy(C); |
| 43 | case BFloatTyID : return getBFloatTy(C); |
| 44 | case FloatTyID : return getFloatTy(C); |
| 45 | case DoubleTyID : return getDoubleTy(C); |
| 46 | case X86_FP80TyID : return getX86_FP80Ty(C); |
| 47 | case FP128TyID : return getFP128Ty(C); |
| 48 | case PPC_FP128TyID : return getPPC_FP128Ty(C); |
| 49 | case LabelTyID : return getLabelTy(C); |
| 50 | case MetadataTyID : return getMetadataTy(C); |
| 51 | case X86_MMXTyID : return getX86_MMXTy(C); |
| 52 | case X86_AMXTyID : return getX86_AMXTy(C); |
| 53 | case TokenTyID : return getTokenTy(C); |
| 54 | default: |
| 55 | return nullptr; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool Type::isIntegerTy(unsigned Bitwidth) const { |
| 60 | return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth; |
| 61 | } |
| 62 | |
| 63 | bool Type::isOpaquePointerTy() const { |
| 64 | if (auto *PTy = dyn_cast<PointerType>(this)) |
| 65 | return PTy->isOpaque(); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | bool Type::canLosslesslyBitCastTo(Type *Ty) const { |
| 70 | |
| 71 | if (this == Ty) |
| 72 | return true; |
| 73 | |
| 74 | |
| 75 | if (!this->isFirstClassType() || !Ty->isFirstClassType()) |
| 76 | return false; |
| 77 | |
| 78 | |
| 79 | |
| 80 | if (isa<VectorType>(this) && isa<VectorType>(Ty)) |
| 81 | return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits(); |
| 82 | |
| 83 | |
| 84 | if (((isa<FixedVectorType>(this)) && Ty->isX86_MMXTy()) && |
| 85 | getPrimitiveSizeInBits().getFixedSize() == 64) |
| 86 | return true; |
| 87 | if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) && |
| 88 | Ty->getPrimitiveSizeInBits().getFixedSize() == 64) |
| 89 | return true; |
| 90 | |
| 91 | |
| 92 | if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) && |
| 93 | getPrimitiveSizeInBits().getFixedSize() == 8192) |
| 94 | return true; |
| 95 | if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) && |
| 96 | Ty->getPrimitiveSizeInBits().getFixedSize() == 8192) |
| 97 | return true; |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | if (auto *PTy = dyn_cast<PointerType>(this)) { |
| 104 | if (auto *OtherPTy = dyn_cast<PointerType>(Ty)) |
| 105 | return PTy->getAddressSpace() == OtherPTy->getAddressSpace(); |
| 106 | return false; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | bool Type::isEmptyTy() const { |
| 112 | if (auto *ATy = dyn_cast<ArrayType>(this)) { |
| 113 | unsigned NumElements = ATy->getNumElements(); |
| 114 | return NumElements == 0 || ATy->getElementType()->isEmptyTy(); |
| 115 | } |
| 116 | |
| 117 | if (auto *STy = dyn_cast<StructType>(this)) { |
| 118 | unsigned NumElements = STy->getNumElements(); |
| 119 | for (unsigned i = 0; i < NumElements; ++i) |
| 120 | if (!STy->getElementType(i)->isEmptyTy()) |
| 121 | return false; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | TypeSize Type::getPrimitiveSizeInBits() const { |
| 129 | switch (getTypeID()) { |
| 130 | case Type::HalfTyID: return TypeSize::Fixed(16); |
| 131 | case Type::BFloatTyID: return TypeSize::Fixed(16); |
| 132 | case Type::FloatTyID: return TypeSize::Fixed(32); |
| 133 | case Type::DoubleTyID: return TypeSize::Fixed(64); |
| 134 | case Type::X86_FP80TyID: return TypeSize::Fixed(80); |
| 135 | case Type::FP128TyID: return TypeSize::Fixed(128); |
| 136 | case Type::PPC_FP128TyID: return TypeSize::Fixed(128); |
| 137 | case Type::X86_MMXTyID: return TypeSize::Fixed(64); |
| 138 | case Type::X86_AMXTyID: return TypeSize::Fixed(8192); |
| 139 | case Type::IntegerTyID: |
| 140 | return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth()); |
| 141 | case Type::FixedVectorTyID: |
| 142 | case Type::ScalableVectorTyID: { |
| 143 | const VectorType *VTy = cast<VectorType>(this); |
| 144 | ElementCount EC = VTy->getElementCount(); |
| 145 | TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits(); |
| 146 | assert(!ETS.isScalable() && "Vector type should have fixed-width elements"); |
| 147 | return {ETS.getFixedSize() * EC.getKnownMinValue(), EC.isScalable()}; |
| 148 | } |
| 149 | default: return TypeSize::Fixed(0); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | unsigned Type::getScalarSizeInBits() const { |
| 154 | |
| 155 | return getScalarType()->getPrimitiveSizeInBits().getFixedSize(); |
| 156 | } |
| 157 | |
| 158 | int Type::getFPMantissaWidth() const { |
| 159 | if (auto *VTy = dyn_cast<VectorType>(this)) |
| 160 | return VTy->getElementType()->getFPMantissaWidth(); |
| 161 | assert(isFloatingPointTy() && "Not a floating point type!"); |
| 162 | if (getTypeID() == HalfTyID) return 11; |
| 163 | if (getTypeID() == BFloatTyID) return 8; |
| 164 | if (getTypeID() == FloatTyID) return 24; |
| 165 | if (getTypeID() == DoubleTyID) return 53; |
| 166 | if (getTypeID() == X86_FP80TyID) return 64; |
| 167 | if (getTypeID() == FP128TyID) return 113; |
| 168 | assert(getTypeID() == PPC_FP128TyID && "unknown fp type"); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const { |
| 173 | if (auto *ATy = dyn_cast<ArrayType>(this)) |
| 174 | return ATy->getElementType()->isSized(Visited); |
| 175 | |
| 176 | if (auto *VTy = dyn_cast<VectorType>(this)) |
| 177 | return VTy->getElementType()->isSized(Visited); |
| 178 | |
| 179 | return cast<StructType>(this)->isSized(Visited); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; } |
| 187 | Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; } |
| 188 | Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; } |
| 189 | Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; } |
| 190 | Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; } |
| 191 | Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; } |
| 192 | Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; } |
| 193 | Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; } |
| 194 | Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; } |
| 195 | Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; } |
| 196 | Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; } |
| 197 | Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; } |
| 198 | Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; } |
| 199 | |
| 200 | IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; } |
| 201 | IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; } |
| 202 | IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; } |
| 203 | IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; } |
| 204 | IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; } |
| 205 | IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; } |
| 206 | |
| 207 | IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) { |
| 208 | return IntegerType::get(C, N); |
| 209 | } |
| 210 | |
| 211 | PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) { |
| 212 | return getHalfTy(C)->getPointerTo(AS); |
| 213 | } |
| 214 | |
| 215 | PointerType *Type::getBFloatPtrTy(LLVMContext &C, unsigned AS) { |
| 216 | return getBFloatTy(C)->getPointerTo(AS); |
| 217 | } |
| 218 | |
| 219 | PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) { |
| 220 | return getFloatTy(C)->getPointerTo(AS); |
| 221 | } |
| 222 | |
| 223 | PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) { |
| 224 | return getDoubleTy(C)->getPointerTo(AS); |
| 225 | } |
| 226 | |
| 227 | PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) { |
| 228 | return getX86_FP80Ty(C)->getPointerTo(AS); |
| 229 | } |
| 230 | |
| 231 | PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) { |
| 232 | return getFP128Ty(C)->getPointerTo(AS); |
| 233 | } |
| 234 | |
| 235 | PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) { |
| 236 | return getPPC_FP128Ty(C)->getPointerTo(AS); |
| 237 | } |
| 238 | |
| 239 | PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) { |
| 240 | return getX86_MMXTy(C)->getPointerTo(AS); |
| 241 | } |
| 242 | |
| 243 | PointerType *Type::getX86_AMXPtrTy(LLVMContext &C, unsigned AS) { |
| 244 | return getX86_AMXTy(C)->getPointerTo(AS); |
| 245 | } |
| 246 | |
| 247 | PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) { |
| 248 | return getIntNTy(C, N)->getPointerTo(AS); |
| 249 | } |
| 250 | |
| 251 | PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) { |
| 252 | return getInt1Ty(C)->getPointerTo(AS); |
| 253 | } |
| 254 | |
| 255 | PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) { |
| 256 | return getInt8Ty(C)->getPointerTo(AS); |
| 257 | } |
| 258 | |
| 259 | PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) { |
| 260 | return getInt16Ty(C)->getPointerTo(AS); |
| 261 | } |
| 262 | |
| 263 | PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) { |
| 264 | return getInt32Ty(C)->getPointerTo(AS); |
| 265 | } |
| 266 | |
| 267 | PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) { |
| 268 | return getInt64Ty(C)->getPointerTo(AS); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) { |
| 276 | assert(NumBits >= MIN_INT_BITS && "bitwidth too small"); |
| 277 | assert(NumBits <= MAX_INT_BITS && "bitwidth too large"); |
| 278 | |
| 279 | |
| 280 | switch (NumBits) { |
| 281 | case 1: return cast<IntegerType>(Type::getInt1Ty(C)); |
| 282 | case 8: return cast<IntegerType>(Type::getInt8Ty(C)); |
| 283 | case 16: return cast<IntegerType>(Type::getInt16Ty(C)); |
| 284 | case 32: return cast<IntegerType>(Type::getInt32Ty(C)); |
| 285 | case 64: return cast<IntegerType>(Type::getInt64Ty(C)); |
| 286 | case 128: return cast<IntegerType>(Type::getInt128Ty(C)); |
| 287 | default: |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits]; |
| 292 | |
| 293 | if (!Entry) |
| 294 | Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits); |
| 295 | |
| 296 | return Entry; |
| 297 | } |
| 298 | |
| 299 | APInt IntegerType::getMask() const { |
| 300 | return APInt::getAllOnesValue(getBitWidth()); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params, |
| 308 | bool IsVarArgs) |
| 309 | : Type(Result->getContext(), FunctionTyID) { |
| 310 | Type **SubTys = reinterpret_cast<Type**>(this+1); |
| 311 | assert(isValidReturnType(Result) && "invalid return type for function"); |
| 312 | setSubclassData(IsVarArgs); |
| 313 | |
| 314 | SubTys[0] = Result; |
| 315 | |
| 316 | for (unsigned i = 0, e = Params.size(); i != e; ++i) { |
| 317 | assert(isValidArgumentType(Params[i]) && |
| 318 | "Not a valid type for function argument!"); |
| 319 | SubTys[i+1] = Params[i]; |
| 320 | } |
| 321 | |
| 322 | ContainedTys = SubTys; |
| 323 | NumContainedTys = Params.size() + 1; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | FunctionType *FunctionType::get(Type *ReturnType, |
| 328 | ArrayRef<Type*> Params, bool isVarArg) { |
| 329 | LLVMContextImpl *pImpl = ReturnType->getContext().pImpl; |
| 330 | const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg); |
| 331 | FunctionType *FT; |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key); |
| 338 | if (Insertion.second) { |
| 339 | |
| 340 | |
| 341 | FT = (FunctionType *)pImpl->Alloc.Allocate( |
| 342 | sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1), |
| 343 | alignof(FunctionType)); |
| 344 | new (FT) FunctionType(ReturnType, Params, isVarArg); |
| 345 | *Insertion.first = FT; |
| 346 | } else { |
| 347 | |
| 348 | FT = *Insertion.first; |
| 349 | } |
| 350 | return FT; |
| 351 | } |
| 352 | |
| 353 | FunctionType *FunctionType::get(Type *Result, bool isVarArg) { |
| 354 | return get(Result, None, isVarArg); |
| 355 | } |
| 356 | |
| 357 | bool FunctionType::isValidReturnType(Type *RetTy) { |
| 358 | return !RetTy->isFunctionTy() && !RetTy->isLabelTy() && |
| 359 | !RetTy->isMetadataTy(); |
| 360 | } |
| 361 | |
| 362 | bool FunctionType::isValidArgumentType(Type *ArgTy) { |
| 363 | return ArgTy->isFirstClassType(); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, |
| 373 | bool isPacked) { |
| 374 | LLVMContextImpl *pImpl = Context.pImpl; |
| 375 | const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked); |
| 376 | |
| 377 | StructType *ST; |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key); |
| 384 | if (Insertion.second) { |
| 385 | |
| 386 | |
| 387 | ST = new (Context.pImpl->Alloc) StructType(Context); |
| 388 | ST->setSubclassData(SCDB_IsLiteral); |
| 389 | ST->setBody(ETypes, isPacked); |
| 390 | *Insertion.first = ST; |
| 391 | } else { |
| 392 | |
| 393 | ST = *Insertion.first; |
| 394 | } |
| 395 | |
| 396 | return ST; |
| 397 | } |
| 398 | |
| 399 | bool StructType::containsScalableVectorType() const { |
| 400 | for (Type *Ty : elements()) { |
| 401 | if (isa<ScalableVectorType>(Ty)) |
| 402 | return true; |
| 403 | if (auto *STy = dyn_cast<StructType>(Ty)) |
| 404 | if (STy->containsScalableVectorType()) |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) { |
| 412 | assert(isOpaque() && "Struct body already set!"); |
| 413 | |
| 414 | setSubclassData(getSubclassData() | SCDB_HasBody); |
| 415 | if (isPacked) |
| 416 | setSubclassData(getSubclassData() | SCDB_Packed); |
| 417 | |
| 418 | NumContainedTys = Elements.size(); |
| 419 | |
| 420 | if (Elements.empty()) { |
| 421 | ContainedTys = nullptr; |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | ContainedTys = Elements.copy(getContext().pImpl->Alloc).data(); |
| 426 | } |
| 427 | |
| 428 | void StructType::setName(StringRef Name) { |
| 429 | if (Name == getName()) return; |
| 430 | |
| 431 | StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes; |
| 432 | |
| 433 | using EntryTy = StringMap<StructType *>::MapEntryTy; |
| 434 | |
| 435 | |
| 436 | |
| 437 | if (SymbolTableEntry) |
| 438 | SymbolTable.remove((EntryTy *)SymbolTableEntry); |
| 439 | |
| 440 | |
| 441 | if (Name.empty()) { |
| 442 | if (SymbolTableEntry) { |
| 443 | |
| 444 | ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); |
| 445 | SymbolTableEntry = nullptr; |
| 446 | } |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | auto IterBool = |
| 452 | getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this)); |
| 453 | |
| 454 | |
| 455 | if (!IterBool.second) { |
| 456 | SmallString<64> TempStr(Name); |
| 457 | TempStr.push_back('.'); |
| 458 | raw_svector_ostream TmpStream(TempStr); |
| 459 | unsigned NameSize = Name.size(); |
| 460 | |
| 461 | do { |
| 462 | TempStr.resize(NameSize + 1); |
| 463 | TmpStream << getContext().pImpl->NamedStructTypesUniqueID++; |
| 464 | |
| 465 | IterBool = getContext().pImpl->NamedStructTypes.insert( |
| 466 | std::make_pair(TmpStream.str(), this)); |
| 467 | } while (!IterBool.second); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | if (SymbolTableEntry) |
| 472 | ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); |
| 473 | SymbolTableEntry = &*IterBool.first; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | StructType *StructType::create(LLVMContext &Context, StringRef Name) { |
| 480 | StructType *ST = new (Context.pImpl->Alloc) StructType(Context); |
| 3 | | Calling 'operator new<llvm::MallocAllocator, 4096UL, 4096UL, 128UL>' | |
|
| 481 | if (!Name.empty()) |
| 482 | ST->setName(Name); |
| 483 | return ST; |
| 484 | } |
| 485 | |
| 486 | StructType *StructType::get(LLVMContext &Context, bool isPacked) { |
| 487 | return get(Context, None, isPacked); |
| 488 | } |
| 489 | |
| 490 | StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements, |
| 491 | StringRef Name, bool isPacked) { |
| 492 | StructType *ST = create(Context, Name); |
| 2 | | Calling 'StructType::create' | |
|
| 493 | ST->setBody(Elements, isPacked); |
| 494 | return ST; |
| 495 | } |
| 496 | |
| 497 | StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) { |
| 498 | return create(Context, Elements, StringRef()); |
| 499 | } |
| 500 | |
| 501 | StructType *StructType::create(LLVMContext &Context) { |
| 502 | return create(Context, StringRef()); |
| 503 | } |
| 504 | |
| 505 | StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name, |
| 506 | bool isPacked) { |
| 507 | assert(!Elements.empty() && |
| 508 | "This method may not be invoked with an empty list"); |
| 509 | return create(Elements[0]->getContext(), Elements, Name, isPacked); |
| 510 | } |
| 511 | |
| 512 | StructType *StructType::create(ArrayRef<Type*> Elements) { |
| 513 | assert(!Elements.empty() && |
| 514 | "This method may not be invoked with an empty list"); |
| 515 | return create(Elements[0]->getContext(), Elements, StringRef()); |
| 1 | Calling 'StructType::create' | |
|
| 516 | } |
| 517 | |
| 518 | bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const { |
| 519 | if ((getSubclassData() & SCDB_IsSized) != 0) |
| 520 | return true; |
| 521 | if (isOpaque()) |
| 522 | return false; |
| 523 | |
| 524 | if (Visited && !Visited->insert(const_cast<StructType*>(this)).second) |
| 525 | return false; |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | for (Type *Ty : elements()) { |
| 531 | |
| 532 | |
| 533 | if (isa<ScalableVectorType>(Ty)) |
| 534 | return false; |
| 535 | if (!Ty->isSized(Visited)) |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | const_cast<StructType*>(this)->setSubclassData( |
| 543 | getSubclassData() | SCDB_IsSized); |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | StringRef StructType::getName() const { |
| 548 | assert(!isLiteral() && "Literal structs never have names"); |
| 549 | if (!SymbolTableEntry) return StringRef(); |
| 550 | |
| 551 | return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey(); |
| 552 | } |
| 553 | |
| 554 | bool StructType::isValidElementType(Type *ElemTy) { |
| 555 | return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && |
| 556 | !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() && |
| 557 | !ElemTy->isTokenTy(); |
| 558 | } |
| 559 | |
| 560 | bool StructType::isLayoutIdentical(StructType *Other) const { |
| 561 | if (this == Other) return true; |
| 562 | |
| 563 | if (isPacked() != Other->isPacked()) |
| 564 | return false; |
| 565 | |
| 566 | return elements() == Other->elements(); |
| 567 | } |
| 568 | |
| 569 | Type *StructType::getTypeAtIndex(const Value *V) const { |
| 570 | unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue(); |
| 571 | assert(indexValid(Idx) && "Invalid structure index!"); |
| 572 | return getElementType(Idx); |
| 573 | } |
| 574 | |
| 575 | bool StructType::indexValid(const Value *V) const { |
| 576 | |
| 577 | |
| 578 | if (!V->getType()->isIntOrIntVectorTy(32)) |
| 579 | return false; |
| 580 | if (isa<ScalableVectorType>(V->getType())) |
| 581 | return false; |
| 582 | const Constant *C = dyn_cast<Constant>(V); |
| 583 | if (C && V->getType()->isVectorTy()) |
| 584 | C = C->getSplatValue(); |
| 585 | const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C); |
| 586 | return CU && CU->getZExtValue() < getNumElements(); |
| 587 | } |
| 588 | |
| 589 | StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) { |
| 590 | return C.pImpl->NamedStructTypes.lookup(Name); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | ArrayType::ArrayType(Type *ElType, uint64_t NumEl) |
| 598 | : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType), |
| 599 | NumElements(NumEl) { |
| 600 | ContainedTys = &ContainedType; |
| 601 | NumContainedTys = 1; |
| 602 | } |
| 603 | |
| 604 | ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) { |
| 605 | assert(isValidElementType(ElementType) && "Invalid type for array element!"); |
| 606 | |
| 607 | LLVMContextImpl *pImpl = ElementType->getContext().pImpl; |
| 608 | ArrayType *&Entry = |
| 609 | pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)]; |
| 610 | |
| 611 | if (!Entry) |
| 612 | Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements); |
| 613 | return Entry; |
| 614 | } |
| 615 | |
| 616 | bool ArrayType::isValidElementType(Type *ElemTy) { |
| 617 | return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && |
| 618 | !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() && |
| 619 | !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy() && |
| 620 | !isa<ScalableVectorType>(ElemTy); |
| 621 | } |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID) |
| 628 | : Type(ElType->getContext(), TID), ContainedType(ElType), |
| 629 | ElementQuantity(EQ) { |
| 630 | ContainedTys = &ContainedType; |
| 631 | NumContainedTys = 1; |
| 632 | } |
| 633 | |
| 634 | VectorType *VectorType::get(Type *ElementType, ElementCount EC) { |
| 635 | if (EC.isScalable()) |
| 636 | return ScalableVectorType::get(ElementType, EC.getKnownMinValue()); |
| 637 | else |
| 638 | return FixedVectorType::get(ElementType, EC.getKnownMinValue()); |
| 639 | } |
| 640 | |
| 641 | bool VectorType::isValidElementType(Type *ElemTy) { |
| 642 | return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() || |
| 643 | ElemTy->isPointerTy(); |
| 644 | } |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) { |
| 651 | assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0"); |
| 652 | assert(isValidElementType(ElementType) && "Element type of a VectorType must " |
| 653 | "be an integer, floating point, or " |
| 654 | "pointer type."); |
| 655 | |
| 656 | auto EC = ElementCount::getFixed(NumElts); |
| 657 | |
| 658 | LLVMContextImpl *pImpl = ElementType->getContext().pImpl; |
| 659 | VectorType *&Entry = ElementType->getContext() |
| 660 | .pImpl->VectorTypes[std::make_pair(ElementType, EC)]; |
| 661 | |
| 662 | if (!Entry) |
| 663 | Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts); |
| 664 | return cast<FixedVectorType>(Entry); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | ScalableVectorType *ScalableVectorType::get(Type *ElementType, |
| 672 | unsigned MinNumElts) { |
| 673 | assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0"); |
| 674 | assert(isValidElementType(ElementType) && "Element type of a VectorType must " |
| 675 | "be an integer, floating point, or " |
| 676 | "pointer type."); |
| 677 | |
| 678 | auto EC = ElementCount::getScalable(MinNumElts); |
| 679 | |
| 680 | LLVMContextImpl *pImpl = ElementType->getContext().pImpl; |
| 681 | VectorType *&Entry = ElementType->getContext() |
| 682 | .pImpl->VectorTypes[std::make_pair(ElementType, EC)]; |
| 683 | |
| 684 | if (!Entry) |
| 685 | Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts); |
| 686 | return cast<ScalableVectorType>(Entry); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) { |
| 694 | assert(EltTy && "Can't get a pointer to <null> type!"); |
| 695 | assert(isValidElementType(EltTy) && "Invalid type for pointer element!"); |
| 696 | |
| 697 | LLVMContextImpl *CImpl = EltTy->getContext().pImpl; |
| 698 | |
| 699 | |
| 700 | if (CImpl->ForceOpaquePointers || EltTy->isOpaquePointerTy()) |
| 701 | return get(EltTy->getContext(), AddressSpace); |
| 702 | |
| 703 | |
| 704 | PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy] |
| 705 | : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)]; |
| 706 | |
| 707 | if (!Entry) |
| 708 | Entry = new (CImpl->Alloc) PointerType(EltTy, AddressSpace); |
| 709 | return Entry; |
| 710 | } |
| 711 | |
| 712 | PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) { |
| 713 | LLVMContextImpl *CImpl = C.pImpl; |
| 714 | |
| 715 | |
| 716 | PointerType *&Entry = |
| 717 | AddressSpace == 0 |
| 718 | ? CImpl->PointerTypes[nullptr] |
| 719 | : CImpl->ASPointerTypes[std::make_pair(nullptr, AddressSpace)]; |
| 720 | |
| 721 | if (!Entry) |
| 722 | Entry = new (CImpl->Alloc) PointerType(C, AddressSpace); |
| 723 | return Entry; |
| 724 | } |
| 725 | |
| 726 | PointerType::PointerType(Type *E, unsigned AddrSpace) |
| 727 | : Type(E->getContext(), PointerTyID), PointeeTy(E) { |
| 728 | ContainedTys = &PointeeTy; |
| 729 | NumContainedTys = 1; |
| 730 | setSubclassData(AddrSpace); |
| 731 | } |
| 732 | |
| 733 | PointerType::PointerType(LLVMContext &C, unsigned AddrSpace) |
| 734 | : Type(C, PointerTyID), PointeeTy(nullptr) { |
| 735 | setSubclassData(AddrSpace); |
| 736 | } |
| 737 | |
| 738 | PointerType *Type::getPointerTo(unsigned AddrSpace) const { |
| 739 | return PointerType::get(const_cast<Type*>(this), AddrSpace); |
| 740 | } |
| 741 | |
| 742 | bool PointerType::isValidElementType(Type *ElemTy) { |
| 743 | return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && |
| 744 | !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() && |
| 745 | !ElemTy->isX86_AMXTy(); |
| 746 | } |
| 747 | |
| 748 | bool PointerType::isLoadableOrStorableType(Type *ElemTy) { |
| 749 | return isValidElementType(ElemTy) && !ElemTy->isFunctionTy(); |
| 750 | } |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | #ifndef LLVM_SUPPORT_ALLOCATOR_H |
| 18 | #define LLVM_SUPPORT_ALLOCATOR_H |
| 19 | |
| 20 | #include "llvm/ADT/Optional.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/Support/Alignment.h" |
| 23 | #include "llvm/Support/AllocatorBase.h" |
| 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
| 26 | #include "llvm/Support/MathExtras.h" |
| 27 | #include "llvm/Support/MemAlloc.h" |
| 28 | #include <algorithm> |
| 29 | #include <cassert> |
| 30 | #include <cstddef> |
| 31 | #include <cstdint> |
| 32 | #include <cstdlib> |
| 33 | #include <iterator> |
| 34 | #include <type_traits> |
| 35 | #include <utility> |
| 36 | |
| 37 | namespace llvm { |
| 38 | |
| 39 | namespace detail { |
| 40 | |
| 41 | |
| 42 | |
| 43 | void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated, |
| 44 | size_t TotalMemory); |
| 45 | |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096, |
| 66 | size_t SizeThreshold = SlabSize, size_t GrowthDelay = 128> |
| 67 | class BumpPtrAllocatorImpl |
| 68 | : public AllocatorBase<BumpPtrAllocatorImpl<AllocatorT, SlabSize, |
| 69 | SizeThreshold, GrowthDelay>>, |
| 70 | private AllocatorT { |
| 71 | public: |
| 72 | static_assert(SizeThreshold <= SlabSize, |
| 73 | "The SizeThreshold must be at most the SlabSize to ensure " |
| 74 | "that objects larger than a slab go into their own memory " |
| 75 | "allocation."); |
| 76 | static_assert(GrowthDelay > 0, |
| 77 | "GrowthDelay must be at least 1 which already increases the" |
| 78 | "slab size after each allocated slab."); |
| 79 | |
| 80 | BumpPtrAllocatorImpl() = default; |
| 81 | |
| 82 | template <typename T> |
| 83 | BumpPtrAllocatorImpl(T &&Allocator) |
| 84 | : AllocatorT(std::forward<T &&>(Allocator)) {} |
| 85 | |
| 86 | |
| 87 | |
| 88 | BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old) |
| 89 | : AllocatorT(static_cast<AllocatorT &&>(Old)), CurPtr(Old.CurPtr), |
| 90 | End(Old.End), Slabs(std::move(Old.Slabs)), |
| 91 | CustomSizedSlabs(std::move(Old.CustomSizedSlabs)), |
| 92 | BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize) { |
| 93 | Old.CurPtr = Old.End = nullptr; |
| 94 | Old.BytesAllocated = 0; |
| 95 | Old.Slabs.clear(); |
| 96 | Old.CustomSizedSlabs.clear(); |
| 97 | } |
| 98 | |
| 99 | ~BumpPtrAllocatorImpl() { |
| 100 | DeallocateSlabs(Slabs.begin(), Slabs.end()); |
| 101 | DeallocateCustomSizedSlabs(); |
| 102 | } |
| 103 | |
| 104 | BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) { |
| 105 | DeallocateSlabs(Slabs.begin(), Slabs.end()); |
| 106 | DeallocateCustomSizedSlabs(); |
| 107 | |
| 108 | CurPtr = RHS.CurPtr; |
| 109 | End = RHS.End; |
| 110 | BytesAllocated = RHS.BytesAllocated; |
| 111 | RedZoneSize = RHS.RedZoneSize; |
| 112 | Slabs = std::move(RHS.Slabs); |
| 113 | CustomSizedSlabs = std::move(RHS.CustomSizedSlabs); |
| 114 | AllocatorT::operator=(static_cast<AllocatorT &&>(RHS)); |
| 115 | |
| 116 | RHS.CurPtr = RHS.End = nullptr; |
| 117 | RHS.BytesAllocated = 0; |
| 118 | RHS.Slabs.clear(); |
| 119 | RHS.CustomSizedSlabs.clear(); |
| 120 | return *this; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | void Reset() { |
| 126 | |
| 127 | DeallocateCustomSizedSlabs(); |
| 128 | CustomSizedSlabs.clear(); |
| 129 | |
| 130 | if (Slabs.empty()) |
| 131 | return; |
| 132 | |
| 133 | |
| 134 | BytesAllocated = 0; |
| 135 | CurPtr = (char *)Slabs.front(); |
| 136 | End = CurPtr + SlabSize; |
| 137 | |
| 138 | __asan_poison_memory_region(*Slabs.begin(), computeSlabSize(0)); |
| 139 | DeallocateSlabs(std::next(Slabs.begin()), Slabs.end()); |
| 140 | Slabs.erase(std::next(Slabs.begin()), Slabs.end()); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * |
| 145 | Allocate(size_t Size, Align Alignment) { |
| 146 | |
| 147 | BytesAllocated += Size; |
| 148 | |
| 149 | size_t Adjustment = offsetToAlignedAddr(CurPtr, Alignment); |
| 6 | | Calling 'offsetToAlignedAddr' | |
|
| 150 | assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow"); |
| 151 | |
| 152 | size_t SizeToAllocate = Size; |
| 153 | #if LLVM_ADDRESS_SANITIZER_BUILD |
| 154 | |
| 155 | SizeToAllocate += RedZoneSize; |
| 156 | #endif |
| 157 | |
| 158 | |
| 159 | if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)) { |
| 160 | char *AlignedPtr = CurPtr + Adjustment; |
| 161 | CurPtr = AlignedPtr + SizeToAllocate; |
| 162 | |
| 163 | |
| 164 | |
| 165 | __msan_allocated_memory(AlignedPtr, Size); |
| 166 | |
| 167 | __asan_unpoison_memory_region(AlignedPtr, Size); |
| 168 | return AlignedPtr; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | size_t PaddedSize = SizeToAllocate + Alignment.value() - 1; |
| 173 | if (PaddedSize > SizeThreshold) { |
| 174 | void *NewSlab = |
| 175 | AllocatorT::Allocate(PaddedSize, alignof(std::max_align_t)); |
| 176 | |
| 177 | |
| 178 | __asan_poison_memory_region(NewSlab, PaddedSize); |
| 179 | CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize)); |
| 180 | |
| 181 | uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment); |
| 182 | assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize); |
| 183 | char *AlignedPtr = (char*)AlignedAddr; |
| 184 | __msan_allocated_memory(AlignedPtr, Size); |
| 185 | __asan_unpoison_memory_region(AlignedPtr, Size); |
| 186 | return AlignedPtr; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | StartNewSlab(); |
| 191 | uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment); |
| 192 | assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End && |
| 193 | "Unable to allocate memory!"); |
| 194 | char *AlignedPtr = (char*)AlignedAddr; |
| 195 | CurPtr = AlignedPtr + SizeToAllocate; |
| 196 | __msan_allocated_memory(AlignedPtr, Size); |
| 197 | __asan_unpoison_memory_region(AlignedPtr, Size); |
| 198 | return AlignedPtr; |
| 199 | } |
| 200 | |
| 201 | inline LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * |
| 202 | Allocate(size_t Size, size_t Alignment) { |
| 203 | assert(Alignment > 0 && "0-byte alignment is not allowed. Use 1 instead."); |
| 204 | return Allocate(Size, Align(Alignment)); |
| 5 | | Calling 'BumpPtrAllocatorImpl::Allocate' | |
|
| 205 | } |
| 206 | |
| 207 | |
| 208 | using AllocatorBase<BumpPtrAllocatorImpl>::Allocate; |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | void Deallocate(const void *Ptr, size_t Size, size_t ) { |
| 214 | __asan_poison_memory_region(Ptr, Size); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate; |
| 219 | |
| 220 | size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); } |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | llvm::Optional<int64_t> identifyObject(const void *Ptr) { |
| 228 | const char *P = static_cast<const char *>(Ptr); |
| 229 | int64_t InSlabIdx = 0; |
| 230 | for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) { |
| 231 | const char *S = static_cast<const char *>(Slabs[Idx]); |
| 232 | if (P >= S && P < S + computeSlabSize(Idx)) |
| 233 | return InSlabIdx + static_cast<int64_t>(P - S); |
| 234 | InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx)); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | int64_t InCustomSizedSlabIdx = -1; |
| 239 | for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) { |
| 240 | const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first); |
| 241 | size_t Size = CustomSizedSlabs[Idx].second; |
| 242 | if (P >= S && P < S + Size) |
| 243 | return InCustomSizedSlabIdx - static_cast<int64_t>(P - S); |
| 244 | InCustomSizedSlabIdx -= static_cast<int64_t>(Size); |
| 245 | } |
| 246 | return None; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | int64_t identifyKnownObject(const void *Ptr) { |
| 254 | Optional<int64_t> Out = identifyObject(Ptr); |
| 255 | assert(Out && "Wrong allocator used"); |
| 256 | return *Out; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | template <typename T> |
| 270 | int64_t identifyKnownAlignedObject(const void *Ptr) { |
| 271 | int64_t Out = identifyKnownObject(Ptr); |
| 272 | assert(Out % alignof(T) == 0 && "Wrong alignment information"); |
| 273 | return Out / alignof(T); |
| 274 | } |
| 275 | |
| 276 | size_t getTotalMemory() const { |
| 277 | size_t TotalMemory = 0; |
| 278 | for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I) |
| 279 | TotalMemory += computeSlabSize(std::distance(Slabs.begin(), I)); |
| 280 | for (auto &PtrAndSize : CustomSizedSlabs) |
| 281 | TotalMemory += PtrAndSize.second; |
| 282 | return TotalMemory; |
| 283 | } |
| 284 | |
| 285 | size_t getBytesAllocated() const { return BytesAllocated; } |
| 286 | |
| 287 | void setRedZoneSize(size_t NewSize) { |
| 288 | RedZoneSize = NewSize; |
| 289 | } |
| 290 | |
| 291 | void PrintStats() const { |
| 292 | detail::printBumpPtrAllocatorStats(Slabs.size(), BytesAllocated, |
| 293 | getTotalMemory()); |
| 294 | } |
| 295 | |
| 296 | private: |
| 297 | |
| 298 | |
| 299 | |
| 300 | char *CurPtr = nullptr; |
| 301 | |
| 302 | |
| 303 | char *End = nullptr; |
| 304 | |
| 305 | |
| 306 | SmallVector<void *, 4> Slabs; |
| 307 | |
| 308 | |
| 309 | SmallVector<std::pair<void *, size_t>, 0> CustomSizedSlabs; |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | size_t BytesAllocated = 0; |
| 315 | |
| 316 | |
| 317 | |
| 318 | size_t RedZoneSize = 1; |
| 319 | |
| 320 | static size_t computeSlabSize(unsigned SlabIdx) { |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | return SlabSize * |
| 326 | ((size_t)1 << std::min<size_t>(30, SlabIdx / GrowthDelay)); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | void StartNewSlab() { |
| 332 | size_t AllocatedSlabSize = computeSlabSize(Slabs.size()); |
| 333 | |
| 334 | void *NewSlab = |
| 335 | AllocatorT::Allocate(AllocatedSlabSize, alignof(std::max_align_t)); |
| 336 | |
| 337 | |
| 338 | __asan_poison_memory_region(NewSlab, AllocatedSlabSize); |
| 339 | |
| 340 | Slabs.push_back(NewSlab); |
| 341 | CurPtr = (char *)(NewSlab); |
| 342 | End = ((char *)NewSlab) + AllocatedSlabSize; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | void DeallocateSlabs(SmallVectorImpl<void *>::iterator I, |
| 347 | SmallVectorImpl<void *>::iterator E) { |
| 348 | for (; I != E; ++I) { |
| 349 | size_t AllocatedSlabSize = |
| 350 | computeSlabSize(std::distance(Slabs.begin(), I)); |
| 351 | AllocatorT::Deallocate(*I, AllocatedSlabSize, alignof(std::max_align_t)); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | void DeallocateCustomSizedSlabs() { |
| 357 | for (auto &PtrAndSize : CustomSizedSlabs) { |
| 358 | void *Ptr = PtrAndSize.first; |
| 359 | size_t Size = PtrAndSize.second; |
| 360 | AllocatorT::Deallocate(Ptr, Size, alignof(std::max_align_t)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | template <typename T> friend class SpecificBumpPtrAllocator; |
| 365 | }; |
| 366 | |
| 367 | |
| 368 | |
| 369 | typedef BumpPtrAllocatorImpl<> BumpPtrAllocator; |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | template <typename T> class SpecificBumpPtrAllocator { |
| 377 | BumpPtrAllocator Allocator; |
| 378 | |
| 379 | public: |
| 380 | SpecificBumpPtrAllocator() { |
| 381 | |
| 382 | |
| 383 | Allocator.setRedZoneSize(0); |
| 384 | } |
| 385 | SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old) |
| 386 | : Allocator(std::move(Old.Allocator)) {} |
| 387 | ~SpecificBumpPtrAllocator() { DestroyAll(); } |
| 388 | |
| 389 | SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) { |
| 390 | Allocator = std::move(RHS.Allocator); |
| 391 | return *this; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | void DestroyAll() { |
| 398 | auto DestroyElements = [](char *Begin, char *End) { |
| 399 | assert(Begin == (char *)alignAddr(Begin, Align::Of<T>())); |
| 400 | for (char *Ptr = Begin; Ptr + sizeof(T) <= End; Ptr += sizeof(T)) |
| 401 | reinterpret_cast<T *>(Ptr)->~T(); |
| 402 | }; |
| 403 | |
| 404 | for (auto I = Allocator.Slabs.begin(), E = Allocator.Slabs.end(); I != E; |
| 405 | ++I) { |
| 406 | size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize( |
| 407 | std::distance(Allocator.Slabs.begin(), I)); |
| 408 | char *Begin = (char *)alignAddr(*I, Align::Of<T>()); |
| 409 | char *End = *I == Allocator.Slabs.back() ? Allocator.CurPtr |
| 410 | : (char *)*I + AllocatedSlabSize; |
| 411 | |
| 412 | DestroyElements(Begin, End); |
| 413 | } |
| 414 | |
| 415 | for (auto &PtrAndSize : Allocator.CustomSizedSlabs) { |
| 416 | void *Ptr = PtrAndSize.first; |
| 417 | size_t Size = PtrAndSize.second; |
| 418 | DestroyElements((char *)alignAddr(Ptr, Align::Of<T>()), |
| 419 | (char *)Ptr + Size); |
| 420 | } |
| 421 | |
| 422 | Allocator.Reset(); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); } |
| 427 | }; |
| 428 | |
| 429 | } |
| 430 | |
| 431 | template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, |
| 432 | size_t GrowthDelay> |
| 433 | void * |
| 434 | operator new(size_t Size, |
| 435 | llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold, |
| 436 | GrowthDelay> &Allocator) { |
| 437 | return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size), |
| 4 | | Calling 'BumpPtrAllocatorImpl::Allocate' | |
|
| 438 | alignof(std::max_align_t))); |
| 439 | } |
| 440 | |
| 441 | template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, |
| 442 | size_t GrowthDelay> |
| 443 | void operator delete(void *, |
| 444 | llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, |
| 445 | SizeThreshold, GrowthDelay> &) { |
| 446 | } |
| 447 | |
| 448 | #endif // LLVM_SUPPORT_ALLOCATOR_H |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | #ifndef LLVM_SUPPORT_ALIGNMENT_H_ |
| 22 | #define LLVM_SUPPORT_ALIGNMENT_H_ |
| 23 | |
| 24 | #include "llvm/ADT/Optional.h" |
| 25 | #include "llvm/Support/MathExtras.h" |
| 26 | #include <cassert> |
| 27 | #ifndef NDEBUG |
| 28 | #include <string> |
| 29 | #endif // NDEBUG |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
| 33 | #define ALIGN_CHECK_ISPOSITIVE(decl) \ |
| 34 | assert(decl > 0 && (#decl " should be defined")) |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | struct Align { |
| 40 | private: |
| 41 | uint8_t ShiftValue = 0; |
| 42 | |
| 43 | |
| 44 | friend struct MaybeAlign; |
| 45 | friend unsigned Log2(Align); |
| 46 | friend bool operator==(Align Lhs, Align Rhs); |
| 47 | friend bool operator!=(Align Lhs, Align Rhs); |
| 48 | friend bool operator<=(Align Lhs, Align Rhs); |
| 49 | friend bool operator>=(Align Lhs, Align Rhs); |
| 50 | friend bool operator<(Align Lhs, Align Rhs); |
| 51 | friend bool operator>(Align Lhs, Align Rhs); |
| 52 | friend unsigned encode(struct MaybeAlign A); |
| 53 | friend struct MaybeAlign decodeMaybeAlign(unsigned Value); |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | struct LogValue { |
| 63 | uint8_t Log; |
| 64 | }; |
| 65 | |
| 66 | public: |
| 67 | |
| 68 | constexpr Align() = default; |
| 69 | |
| 70 | |
| 71 | constexpr Align(const Align &Other) = default; |
| 72 | constexpr Align(Align &&Other) = default; |
| 73 | Align &operator=(const Align &Other) = default; |
| 74 | Align &operator=(Align &&Other) = default; |
| 75 | |
| 76 | explicit Align(uint64_t Value) { |
| 77 | assert(Value > 0 && "Value must not be 0"); |
| 78 | assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2"); |
| 79 | ShiftValue = Log2_64(Value); |
| 80 | assert(ShiftValue < 64 && "Broken invariant"); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | uint64_t value() const { return uint64_t(1) << ShiftValue; } |
| 11 | | The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'uint64_t' |
|
| 86 | |
| 87 | |
| 88 | template <size_t kValue> constexpr static LogValue Constant() { |
| 89 | return LogValue{static_cast<uint8_t>(CTLog2<kValue>())}; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | template <typename T> constexpr static LogValue Of() { |
| 95 | return Constant<std::alignment_of<T>::value>(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | constexpr Align(LogValue CA) : ShiftValue(CA.Log) {} |
| 100 | }; |
| 101 | |
| 102 | |
| 103 | inline Align assumeAligned(uint64_t Value) { |
| 104 | return Value ? Align(Value) : Align(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | struct MaybeAlign : public llvm::Optional<Align> { |
| 110 | private: |
| 111 | using UP = llvm::Optional<Align>; |
| 112 | |
| 113 | public: |
| 114 | |
| 115 | MaybeAlign() = default; |
| 116 | |
| 117 | |
| 118 | MaybeAlign(const MaybeAlign &Other) = default; |
| 119 | MaybeAlign &operator=(const MaybeAlign &Other) = default; |
| 120 | MaybeAlign(MaybeAlign &&Other) = default; |
| 121 | MaybeAlign &operator=(MaybeAlign &&Other) = default; |
| 122 | |
| 123 | |
| 124 | using UP::UP; |
| 125 | |
| 126 | explicit MaybeAlign(uint64_t Value) { |
| 127 | assert((Value == 0 || llvm::isPowerOf2_64(Value)) && |
| 128 | "Alignment is neither 0 nor a power of 2"); |
| 129 | if (Value) |
| 130 | emplace(Value); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | Align valueOrOne() const { return hasValue() ? getValue() : Align(); } |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | inline bool isAligned(Align Lhs, uint64_t SizeInBytes) { |
| 139 | return SizeInBytes % Lhs.value() == 0; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | inline bool isAddrAligned(Align Lhs, const void *Addr) { |
| 144 | return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr)); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | inline uint64_t alignTo(uint64_t Size, Align A) { |
| 149 | const uint64_t Value = A.value(); |
| |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | return (Size + Value - 1) & ~(Value - 1U); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) { |
| 174 | const uint64_t Value = A.value(); |
| 175 | Skew %= Value; |
| 176 | return ((Size + Value - 1 - Skew) & ~(Value - 1U)) + Skew; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | inline uint64_t alignTo(uint64_t Size, MaybeAlign A) { |
| 182 | return A ? alignTo(Size, A.getValue()) : Size; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | inline uintptr_t alignAddr(const void *Addr, Align Alignment) { |
| 187 | uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr); |
| 188 | assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >= |
| 189 | ArithAddr && |
| 190 | "Overflow"); |
| 191 | return alignTo(ArithAddr, Alignment); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) { |
| 197 | return alignTo(Value, Alignment) - Value; |
| 8 | | The value 255 is assigned to 'A.ShiftValue' | |
|
| |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) { |
| 203 | return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment); |
| 7 | | Calling 'offsetToAlignment' | |
|
| 204 | } |
| 205 | |
| 206 | |
| 207 | inline unsigned Log2(Align A) { return A.ShiftValue; } |
| 208 | |
| 209 | |
| 210 | |
| 211 | inline Align commonAlignment(Align A, Align B) { return std::min(A, B); } |
| 212 | |
| 213 | |
| 214 | |
| 215 | inline Align commonAlignment(Align A, uint64_t Offset) { |
| 216 | return Align(MinAlign(A.value(), Offset)); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) { |
| 222 | return A && B ? commonAlignment(*A, *B) : A ? A : B; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) { |
| 228 | return MaybeAlign(MinAlign((*A).value(), Offset)); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; } |
| 233 | |
| 234 | |
| 235 | inline MaybeAlign decodeMaybeAlign(unsigned Value) { |
| 236 | if (Value == 0) |
| 237 | return MaybeAlign(); |
| 238 | Align Out; |
| 239 | Out.ShiftValue = Value - 1; |
| 240 | return Out; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | |
| 245 | inline unsigned encode(Align A) { return encode(MaybeAlign(A)); } |
| 246 | |
| 247 | |
| 248 | inline bool operator==(Align Lhs, uint64_t Rhs) { |
| 249 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
| 250 | return Lhs.value() == Rhs; |
| 251 | } |
| 252 | inline bool operator!=(Align Lhs, uint64_t Rhs) { |
| 253 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
| 254 | return Lhs.value() != Rhs; |
| 255 | } |
| 256 | inline bool operator<=(Align Lhs, uint64_t Rhs) { |
| 257 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
| 258 | return Lhs.value() <= Rhs; |
| 259 | } |
| 260 | inline bool operator>=(Align Lhs, uint64_t Rhs) { |
| 261 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
| 262 | return Lhs.value() >= Rhs; |
| 263 | } |
| 264 | inline bool operator<(Align Lhs, uint64_t Rhs) { |
| 265 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
| 266 | return Lhs.value() < Rhs; |
| 267 | } |
| 268 | inline bool operator>(Align Lhs, uint64_t Rhs) { |
| 269 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
| 270 | return Lhs.value() > Rhs; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) { |
| 275 | return Lhs ? (*Lhs).value() == Rhs : Rhs == 0; |
| 276 | } |
| 277 | inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) { |
| 278 | return Lhs ? (*Lhs).value() != Rhs : Rhs != 0; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | inline bool operator==(Align Lhs, Align Rhs) { |
| 283 | return Lhs.ShiftValue == Rhs.ShiftValue; |
| 284 | } |
| 285 | inline bool operator!=(Align Lhs, Align Rhs) { |
| 286 | return Lhs.ShiftValue != Rhs.ShiftValue; |
| 287 | } |
| 288 | inline bool operator<=(Align Lhs, Align Rhs) { |
| 289 | return Lhs.ShiftValue <= Rhs.ShiftValue; |
| 290 | } |
| 291 | inline bool operator>=(Align Lhs, Align Rhs) { |
| 292 | return Lhs.ShiftValue >= Rhs.ShiftValue; |
| 293 | } |
| 294 | inline bool operator<(Align Lhs, Align Rhs) { |
| 295 | return Lhs.ShiftValue < Rhs.ShiftValue; |
| 296 | } |
| 297 | inline bool operator>(Align Lhs, Align Rhs) { |
| 298 | return Lhs.ShiftValue > Rhs.ShiftValue; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | bool operator<=(Align Lhs, MaybeAlign Rhs) = delete; |
| 303 | bool operator>=(Align Lhs, MaybeAlign Rhs) = delete; |
| 304 | bool operator<(Align Lhs, MaybeAlign Rhs) = delete; |
| 305 | bool operator>(Align Lhs, MaybeAlign Rhs) = delete; |
| 306 | |
| 307 | bool operator<=(MaybeAlign Lhs, Align Rhs) = delete; |
| 308 | bool operator>=(MaybeAlign Lhs, Align Rhs) = delete; |
| 309 | bool operator<(MaybeAlign Lhs, Align Rhs) = delete; |
| 310 | bool operator>(MaybeAlign Lhs, Align Rhs) = delete; |
| 311 | |
| 312 | bool operator<=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
| 313 | bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
| 314 | bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
| 315 | bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
| 316 | |
| 317 | inline Align operator*(Align Lhs, uint64_t Rhs) { |
| 318 | assert(Rhs > 0 && "Rhs must be positive"); |
| 319 | return Align(Lhs.value() * Rhs); |
| 320 | } |
| 321 | |
| 322 | inline MaybeAlign operator*(MaybeAlign Lhs, uint64_t Rhs) { |
| 323 | assert(Rhs > 0 && "Rhs must be positive"); |
| 324 | return Lhs ? Lhs.getValue() * Rhs : MaybeAlign(); |
| 325 | } |
| 326 | |
| 327 | inline Align operator/(Align Lhs, uint64_t Divisor) { |
| 328 | assert(llvm::isPowerOf2_64(Divisor) && |
| 329 | "Divisor must be positive and a power of 2"); |
| 330 | assert(Lhs != 1 && "Can't halve byte alignment"); |
| 331 | return Align(Lhs.value() / Divisor); |
| 332 | } |
| 333 | |
| 334 | inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) { |
| 335 | assert(llvm::isPowerOf2_64(Divisor) && |
| 336 | "Divisor must be positive and a power of 2"); |
| 337 | return Lhs ? Lhs.getValue() / Divisor : MaybeAlign(); |
| 338 | } |
| 339 | |
| 340 | inline Align max(MaybeAlign Lhs, Align Rhs) { |
| 341 | return Lhs && *Lhs > Rhs ? *Lhs : Rhs; |
| 342 | } |
| 343 | |
| 344 | inline Align max(Align Lhs, MaybeAlign Rhs) { |
| 345 | return Rhs && *Rhs > Lhs ? *Rhs : Lhs; |
| 346 | } |
| 347 | |
| 348 | #ifndef NDEBUG |
| 349 | |
| 350 | inline std::string DebugStr(const Align &A) { |
| 351 | return std::to_string(A.value()); |
| 352 | } |
| 353 | |
| 354 | inline std::string DebugStr(const MaybeAlign &MA) { |
| 355 | if (MA) |
| 356 | return std::to_string(MA->value()); |
| 357 | return "None"; |
| 358 | } |
| 359 | #endif // NDEBUG |
| 360 | |
| 361 | #undef ALIGN_CHECK_ISPOSITIVE |
| 362 | |
| 363 | } |
| 364 | |
| 365 | #endif // LLVM_SUPPORT_ALIGNMENT_H_ |