clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Operator.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/Operator.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "llvm/IR/Operator.h" |
| 14 | #include "llvm/IR/DataLayout.h" |
| 15 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 16 | #include "llvm/IR/Instructions.h" |
| 17 | #include "llvm/IR/Type.h" |
| 18 | |
| 19 | #include "ConstantsContext.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | Type *GEPOperator::getSourceElementType() const { |
| 23 | if (auto *I = dyn_cast<GetElementPtrInst>(this)) |
| 24 | return I->getSourceElementType(); |
| 25 | return cast<GetElementPtrConstantExpr>(this)->getSourceElementType(); |
| 26 | } |
| 27 | |
| 28 | Type *GEPOperator::getResultElementType() const { |
| 29 | if (auto *I = dyn_cast<GetElementPtrInst>(this)) |
| 30 | return I->getResultElementType(); |
| 31 | return cast<GetElementPtrConstantExpr>(this)->getResultElementType(); |
| 32 | } |
| 33 | |
| 34 | Align GEPOperator::getMaxPreservedAlignment(const DataLayout &DL) const { |
| 35 | |
| 36 | |
| 37 | |
| 38 | Align Result = Align(llvm::Value::MaximumAlignment); |
| 39 | for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this); |
| 1 | Loop condition is true. Entering loop body | |
|
| 6 | | Loop condition is true. Entering loop body | |
|
| 40 | GTI != GTE; ++GTI) { |
| 41 | int64_t Offset = 1; |
| 42 | ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); |
| 2 | | Assuming the object is a 'ConstantInt' | |
|
| 7 | | Assuming the object is a 'ConstantInt' | |
|
| 43 | |
| 44 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
| 3 | | Assuming 'STy' is non-null | |
|
| |
| 8 | | Assuming 'STy' is non-null | |
|
| |
| 45 | const StructLayout *SL = DL.getStructLayout(STy); |
| 46 | Offset = SL->getElementOffset(OpC->getZExtValue()); |
| 47 | } else { |
| 48 | assert(GTI.isSequential() && "should be sequencial"); |
| 49 | |
| 50 | |
| 51 | int64_t ElemCount = 1; |
| 52 | if (OpC) |
| 53 | ElemCount = OpC->getZExtValue(); |
| 54 | Offset = DL.getTypeAllocSize(GTI.getIndexedType()) * ElemCount; |
| 55 | } |
| 56 | Result = Align(MinAlign(Offset, Result.value())); |
| 5 | | The value 255 is assigned to 'Result.ShiftValue' | |
|
| |
| 57 | } |
| 58 | return Result; |
| 59 | } |
| 60 | |
| 61 | bool GEPOperator::accumulateConstantOffset( |
| 62 | const DataLayout &DL, APInt &Offset, |
| 63 | function_ref<bool(Value &, APInt &)> ExternalAnalysis) const { |
| 64 | assert(Offset.getBitWidth() == |
| 65 | DL.getIndexSizeInBits(getPointerAddressSpace()) && |
| 66 | "The offset bit width does not match DL specification."); |
| 67 | SmallVector<const Value *> Index(value_op_begin() + 1, value_op_end()); |
| 68 | return GEPOperator::accumulateConstantOffset(getSourceElementType(), Index, |
| 69 | DL, Offset, ExternalAnalysis); |
| 70 | } |
| 71 | |
| 72 | bool GEPOperator::accumulateConstantOffset( |
| 73 | Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL, |
| 74 | APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) { |
| 75 | bool UsedExternalAnalysis = false; |
| 76 | auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool { |
| 77 | Index = Index.sextOrTrunc(Offset.getBitWidth()); |
| 78 | APInt IndexedSize = APInt(Offset.getBitWidth(), Size); |
| 79 | |
| 80 | if (!UsedExternalAnalysis) { |
| 81 | Offset += Index * IndexedSize; |
| 82 | } else { |
| 83 | |
| 84 | |
| 85 | bool Overflow = false; |
| 86 | APInt OffsetPlus = Index.smul_ov(IndexedSize, Overflow); |
| 87 | if (Overflow) |
| 88 | return false; |
| 89 | Offset = Offset.sadd_ov(OffsetPlus, Overflow); |
| 90 | if (Overflow) |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
| 94 | }; |
| 95 | auto begin = generic_gep_type_iterator<decltype(Index.begin())>::begin( |
| 96 | SourceType, Index.begin()); |
| 97 | auto end = generic_gep_type_iterator<decltype(Index.end())>::end(Index.end()); |
| 98 | for (auto GTI = begin, GTE = end; GTI != GTE; ++GTI) { |
| 99 | |
| 100 | bool ScalableType = false; |
| 101 | if (isa<ScalableVectorType>(GTI.getIndexedType())) |
| 102 | ScalableType = true; |
| 103 | |
| 104 | Value *V = GTI.getOperand(); |
| 105 | StructType *STy = GTI.getStructTypeOrNull(); |
| 106 | |
| 107 | if (auto ConstOffset = dyn_cast<ConstantInt>(V)) { |
| 108 | if (ConstOffset->isZero()) |
| 109 | continue; |
| 110 | |
| 111 | |
| 112 | if (ScalableType) |
| 113 | return false; |
| 114 | |
| 115 | if (STy) { |
| 116 | unsigned ElementIdx = ConstOffset->getZExtValue(); |
| 117 | const StructLayout *SL = DL.getStructLayout(STy); |
| 118 | |
| 119 | if (!AccumulateOffset( |
| 120 | APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)), |
| 121 | 1)) |
| 122 | return false; |
| 123 | continue; |
| 124 | } |
| 125 | if (!AccumulateOffset(ConstOffset->getValue(), |
| 126 | DL.getTypeAllocSize(GTI.getIndexedType()))) |
| 127 | return false; |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | if (!ExternalAnalysis || STy || ScalableType) |
| 134 | return false; |
| 135 | APInt AnalysisIndex; |
| 136 | if (!ExternalAnalysis(*V, AnalysisIndex)) |
| 137 | return false; |
| 138 | UsedExternalAnalysis = true; |
| 139 | if (!AccumulateOffset(AnalysisIndex, |
| 140 | DL.getTypeAllocSize(GTI.getIndexedType()))) |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | bool GEPOperator::collectOffset( |
| 147 | const DataLayout &DL, unsigned BitWidth, |
| 148 | MapVector<Value *, APInt> &VariableOffsets, |
| 149 | APInt &ConstantOffset) const { |
| 150 | assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) && |
| 151 | "The offset bit width does not match DL specification."); |
| 152 | |
| 153 | auto CollectConstantOffset = [&](APInt Index, uint64_t Size) { |
| 154 | Index = Index.sextOrTrunc(BitWidth); |
| 155 | APInt IndexedSize = APInt(BitWidth, Size); |
| 156 | ConstantOffset += Index * IndexedSize; |
| 157 | }; |
| 158 | |
| 159 | for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this); |
| 160 | GTI != GTE; ++GTI) { |
| 161 | |
| 162 | bool ScalableType = isa<ScalableVectorType>(GTI.getIndexedType()); |
| 163 | |
| 164 | Value *V = GTI.getOperand(); |
| 165 | StructType *STy = GTI.getStructTypeOrNull(); |
| 166 | |
| 167 | if (auto ConstOffset = dyn_cast<ConstantInt>(V)) { |
| 168 | if (ConstOffset->isZero()) |
| 169 | continue; |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | if (ScalableType) |
| 176 | return false; |
| 177 | |
| 178 | if (STy) { |
| 179 | unsigned ElementIdx = ConstOffset->getZExtValue(); |
| 180 | const StructLayout *SL = DL.getStructLayout(STy); |
| 181 | |
| 182 | CollectConstantOffset(APInt(BitWidth, SL->getElementOffset(ElementIdx)), |
| 183 | 1); |
| 184 | continue; |
| 185 | } |
| 186 | CollectConstantOffset(ConstOffset->getValue(), |
| 187 | DL.getTypeAllocSize(GTI.getIndexedType())); |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | if (STy || ScalableType) |
| 192 | return false; |
| 193 | |
| 194 | |
| 195 | VariableOffsets.insert({V, APInt(BitWidth, 0)}); |
| 196 | APInt IndexedSize = |
| 197 | APInt(BitWidth, DL.getTypeAllocSize(GTI.getIndexedType())); |
| 198 | VariableOffsets[V] += IndexedSize; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 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; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) { |
| 203 | return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment); |
| 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_ |