clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name OMPContext.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 static -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" -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 -stack-protector 2 -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/Frontend/OpenMP/OMPContext.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #include "llvm/Frontend/OpenMP/OMPContext.h" |
| 16 | #include "llvm/ADT/SetOperations.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/ADT/StringSwitch.h" |
| 19 | #include "llvm/ADT/Triple.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| 23 | #define DEBUG_TYPE "openmp-ir-builder" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | using namespace omp; |
| 27 | |
| 28 | OMPContext::OMPContext(bool IsDeviceCompilation, Triple TargetTriple) { |
| 29 | |
| 30 | |
| 31 | ActiveTraits.set(unsigned(IsDeviceCompilation |
| 32 | ? TraitProperty::device_kind_nohost |
| 33 | : TraitProperty::device_kind_host)); |
| 34 | switch (TargetTriple.getArch()) { |
| 35 | case Triple::arm: |
| 36 | case Triple::armeb: |
| 37 | case Triple::aarch64: |
| 38 | case Triple::aarch64_be: |
| 39 | case Triple::aarch64_32: |
| 40 | case Triple::mips: |
| 41 | case Triple::mipsel: |
| 42 | case Triple::mips64: |
| 43 | case Triple::mips64el: |
| 44 | case Triple::ppc: |
| 45 | case Triple::ppcle: |
| 46 | case Triple::ppc64: |
| 47 | case Triple::ppc64le: |
| 48 | case Triple::x86: |
| 49 | case Triple::x86_64: |
| 50 | ActiveTraits.set(unsigned(TraitProperty::device_kind_cpu)); |
| 51 | break; |
| 52 | case Triple::amdgcn: |
| 53 | case Triple::nvptx: |
| 54 | case Triple::nvptx64: |
| 55 | ActiveTraits.set(unsigned(TraitProperty::device_kind_gpu)); |
| 56 | break; |
| 57 | default: |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 63 | if (TraitSelector::TraitSelectorEnum == TraitSelector::device_arch) { \ |
| 64 | if (TargetTriple.getArch() == TargetTriple.getArchTypeForLLVMName(Str)) \ |
| 65 | ActiveTraits.set(unsigned(TraitProperty::Enum)); \ |
| 66 | if (StringRef(Str) == StringRef("x86_64") && \ |
| 67 | TargetTriple.getArch() == Triple::x86_64) \ |
| 68 | ActiveTraits.set(unsigned(TraitProperty::Enum)); \ |
| 69 | } |
| 70 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | ActiveTraits.set(unsigned(TraitProperty::implementation_vendor_llvm)); |
| 79 | |
| 80 | |
| 81 | ActiveTraits.set(unsigned(TraitProperty::user_condition_true)); |
| 82 | |
| 83 | |
| 84 | ActiveTraits.set(unsigned(TraitProperty::device_kind_any)); |
| 85 | |
| 86 | LLVM_DEBUG({ |
| 87 | dbgs() << "[" << DEBUG_TYPE |
| 88 | << "] New OpenMP context with the following properties:\n"; |
| 89 | for (unsigned Bit : ActiveTraits.set_bits()) { |
| 90 | TraitProperty Property = TraitProperty(Bit); |
| 91 | dbgs() << "\t " << getOpenMPContextTraitPropertyFullName(Property) |
| 92 | << "\n"; |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | template <typename T> static bool isSubset(ArrayRef<T> C0, ArrayRef<T> C1) { |
| 100 | #ifdef EXPENSIVE_CHECKS |
| 101 | assert(llvm::is_sorted(C0) && llvm::is_sorted(C1) && |
| 102 | "Expected sorted arrays!"); |
| 103 | #endif |
| 104 | if (C0.size() > C1.size()) |
| 105 | return false; |
| 106 | auto It0 = C0.begin(), End0 = C0.end(); |
| 107 | auto It1 = C1.begin(), End1 = C1.end(); |
| 108 | while (It0 != End0) { |
| 109 | if (It1 == End1) |
| 110 | return false; |
| 111 | if (*It0 == *It1) { |
| 112 | ++It0; |
| 113 | ++It1; |
| 114 | continue; |
| 115 | } |
| 116 | ++It0; |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | template <typename T> |
| 124 | static bool isStrictSubset(ArrayRef<T> C0, ArrayRef<T> C1) { |
| 125 | if (C0.size() >= C1.size()) |
| 126 | return false; |
| 127 | return isSubset<T>(C0, C1); |
| 128 | } |
| 129 | |
| 130 | static bool isStrictSubset(const VariantMatchInfo &VMI0, |
| 131 | const VariantMatchInfo &VMI1) { |
| 132 | |
| 133 | |
| 134 | |
| 135 | if (VMI0.RequiredTraits.count() >= VMI1.RequiredTraits.count()) |
| 136 | return false; |
| 137 | for (unsigned Bit : VMI0.RequiredTraits.set_bits()) |
| 138 | if (!VMI1.RequiredTraits.test(Bit)) |
| 139 | return false; |
| 140 | if (!isSubset<TraitProperty>(VMI0.ConstructTraits, VMI1.ConstructTraits)) |
| 141 | return false; |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | static int isVariantApplicableInContextHelper( |
| 146 | const VariantMatchInfo &VMI, const OMPContext &Ctx, |
| 147 | SmallVectorImpl<unsigned> *ConstructMatches, bool DeviceSetOnly) { |
| 148 | |
| 149 | |
| 150 | |
| 151 | enum MatchKind { MK_ALL, MK_ANY, MK_NONE }; |
| 152 | |
| 153 | MatchKind MK = MK_ALL; |
| 154 | |
| 155 | |
| 156 | if (VMI.RequiredTraits.test( |
| |
| 157 | unsigned(TraitProperty::implementation_extension_match_any))) |
| 158 | MK = MK_ANY; |
| 159 | if (VMI.RequiredTraits.test( |
| |
| 160 | unsigned(TraitProperty::implementation_extension_match_none))) |
| 161 | MK = MK_NONE; |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | auto HandleTrait = [MK](TraitProperty Property, |
| 167 | bool WasFound) -> Optional<bool> { |
| 168 | |
| 169 | |
| 170 | if (MK == MK_ANY) { |
| 171 | if (WasFound) |
| 172 | return true; |
| 173 | return None; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | if ((WasFound && MK == MK_ALL) || (!WasFound && MK == MK_NONE)) |
| 179 | return None; |
| 180 | |
| 181 | |
| 182 | LLVM_DEBUG({ |
| 183 | if (MK == MK_ALL) |
| 184 | dbgs() << "[" << DEBUG_TYPE << "] Property " |
| 185 | << getOpenMPContextTraitPropertyName(Property, "") |
| 186 | << " was not in the OpenMP context but match kind is all.\n"; |
| 187 | if (MK == MK_NONE) |
| 188 | dbgs() << "[" << DEBUG_TYPE << "] Property " |
| 189 | << getOpenMPContextTraitPropertyName(Property, "") |
| 190 | << " was in the OpenMP context but match kind is none.\n"; |
| 191 | }); |
| 192 | return false; |
| 193 | }; |
| 194 | |
| 195 | for (unsigned Bit : VMI.RequiredTraits.set_bits()) { |
| 196 | TraitProperty Property = TraitProperty(Bit); |
| 197 | if (DeviceSetOnly && |
| 198 | getOpenMPContextTraitSetForProperty(Property) != TraitSet::device) |
| 199 | continue; |
| 200 | |
| 201 | |
| 202 | |
| 203 | if (getOpenMPContextTraitSelectorForProperty(Property) == |
| 204 | TraitSelector::implementation_extension) |
| 205 | continue; |
| 206 | |
| 207 | bool IsActiveTrait = Ctx.ActiveTraits.test(unsigned(Property)); |
| 208 | |
| 209 | |
| 210 | |
| 211 | if (Property == TraitProperty::device_isa___ANY) |
| 212 | IsActiveTrait = llvm::all_of(VMI.ISATraits, [&](StringRef RawString) { |
| 213 | return Ctx.matchesISATrait(RawString); |
| 214 | }); |
| 215 | |
| 216 | Optional<bool> Result = HandleTrait(Property, IsActiveTrait); |
| 217 | if (Result.hasValue()) |
| 218 | return Result.getValue(); |
| 219 | } |
| 220 | |
| 221 | if (!DeviceSetOnly) { |
| |
| 222 | |
| 223 | |
| 224 | unsigned ConstructIdx = 0, NoConstructTraits = Ctx.ConstructTraits.size(); |
| 225 | for (TraitProperty Property : VMI.ConstructTraits) { |
| 8 | | Assuming '__begin2' is equal to '__end2' | |
|
| 226 | assert(getOpenMPContextTraitSetForProperty(Property) == |
| 227 | TraitSet::construct && |
| 228 | "Variant context is ill-formed!"); |
| 229 | |
| 230 | |
| 231 | bool FoundInOrder = false; |
| 232 | while (!FoundInOrder && ConstructIdx != NoConstructTraits) |
| 233 | FoundInOrder = (Ctx.ConstructTraits[ConstructIdx++] == Property); |
| 234 | if (ConstructMatches) |
| 235 | ConstructMatches->push_back(ConstructIdx - 1); |
| 236 | |
| 237 | Optional<bool> Result = HandleTrait(Property, FoundInOrder); |
| 238 | if (Result.hasValue()) |
| 239 | return Result.getValue(); |
| 240 | |
| 241 | if (!FoundInOrder) { |
| 242 | LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] Construct property " |
| 243 | << getOpenMPContextTraitPropertyName(Property, "") |
| 244 | << " was not nested properly.\n"); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | } |
| 250 | |
| 251 | assert(isSubset<TraitProperty>(VMI.ConstructTraits, Ctx.ConstructTraits) && |
| 252 | "Broken invariant!"); |
| 253 | } |
| 254 | |
| 255 | if (MK == MK_ANY) { |
| |
| 256 | LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE |
| 257 | << "] None of the properties was in the OpenMP context " |
| 258 | "but match kind is any.\n"); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | return true; |
| 10 | | Returning the value 1, which participates in a condition later | |
|
| 263 | } |
| 264 | |
| 265 | bool llvm::omp::isVariantApplicableInContext(const VariantMatchInfo &VMI, |
| 266 | const OMPContext &Ctx, |
| 267 | bool DeviceSetOnly) { |
| 268 | return isVariantApplicableInContextHelper( |
| 269 | VMI, Ctx, nullptr, DeviceSetOnly); |
| 270 | } |
| 271 | |
| 272 | static APInt getVariantMatchScore(const VariantMatchInfo &VMI, |
| 273 | const OMPContext &Ctx, |
| 274 | SmallVectorImpl<unsigned> &ConstructMatches) { |
| 275 | APInt Score(64, 1); |
| 276 | |
| 277 | unsigned NoConstructTraits = VMI.ConstructTraits.size(); |
| 278 | for (unsigned Bit : VMI.RequiredTraits.set_bits()) { |
| 279 | TraitProperty Property = TraitProperty(Bit); |
| 280 | |
| 281 | if (VMI.ScoreMap.count(Property)) { |
| 282 | const APInt &UserScore = VMI.ScoreMap.lookup(Property); |
| 283 | assert(UserScore.uge(0) && "Expect non-negative user scores!"); |
| 284 | Score += UserScore.getZExtValue(); |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | switch (getOpenMPContextTraitSetForProperty(Property)) { |
| 289 | case TraitSet::construct: |
| 290 | |
| 291 | |
| 292 | continue; |
| 293 | case TraitSet::implementation: |
| 294 | |
| 295 | continue; |
| 296 | case TraitSet::user: |
| 297 | |
| 298 | continue; |
| 299 | case TraitSet::device: |
| 300 | |
| 301 | break; |
| 302 | case TraitSet::invalid: |
| 303 | llvm_unreachable("Unknown trait set is not to be used!"); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | if (Property == TraitProperty::device_kind_any) |
| 308 | continue; |
| 309 | |
| 310 | switch (getOpenMPContextTraitSelectorForProperty(Property)) { |
| 311 | case TraitSelector::device_kind: |
| 312 | Score += (1ULL << (NoConstructTraits + 0)); |
| 313 | continue; |
| 314 | case TraitSelector::device_arch: |
| 315 | Score += (1ULL << (NoConstructTraits + 1)); |
| 316 | continue; |
| 317 | case TraitSelector::device_isa: |
| 318 | Score += (1ULL << (NoConstructTraits + 2)); |
| 319 | continue; |
| 320 | default: |
| 321 | continue; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | unsigned ConstructIdx = 0; |
| 326 | assert(NoConstructTraits == ConstructMatches.size() && |
| 327 | "Mismatch in the construct traits!"); |
| 328 | for (TraitProperty Property : VMI.ConstructTraits) { |
| 329 | assert(getOpenMPContextTraitSetForProperty(Property) == |
| 330 | TraitSet::construct && |
| 331 | "Ill-formed variant match info!"); |
| 332 | (void)Property; |
| 333 | |
| 334 | Score += (1ULL << ConstructMatches[ConstructIdx++]); |
| 335 | } |
| 336 | |
| 337 | LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] Variant has a score of " << Score |
| 338 | << "\n"); |
| 339 | return Score; |
| 340 | } |
| 341 | |
| 342 | int llvm::omp::getBestVariantMatchForContext( |
| 343 | const SmallVectorImpl<VariantMatchInfo> &VMIs, const OMPContext &Ctx) { |
| 344 | |
| 345 | APInt BestScore(64, 0); |
| 346 | int BestVMIIdx = -1; |
| 347 | const VariantMatchInfo *BestVMI = nullptr; |
| 1 | 'BestVMI' initialized to a null pointer value | |
|
| 348 | |
| 349 | for (unsigned u = 0, e = VMIs.size(); u < e; ++u) { |
| |
| 3 | | Loop condition is true. Entering loop body | |
|
| 350 | const VariantMatchInfo &VMI = VMIs[u]; |
| 351 | |
| 352 | SmallVector<unsigned, 8> ConstructMatches; |
| 353 | |
| 354 | if (!isVariantApplicableInContextHelper(VMI, Ctx, &ConstructMatches, |
| 4 | | Calling 'isVariantApplicableInContextHelper' | |
|
| 11 | | Returning from 'isVariantApplicableInContextHelper' | |
|
| |
| 355 | false)) |
| 356 | continue; |
| 357 | |
| 358 | APInt Score = getVariantMatchScore(VMI, Ctx, ConstructMatches); |
| 359 | if (Score.ult(BestScore)) |
| |
| 16 | | Returning from 'APInt::ult' | |
|
| |
| 360 | continue; |
| 361 | |
| 362 | if (Score.eq(BestScore)) { |
| |
| 28 | | Returning from 'APInt::eq' | |
|
| 29 | | Assuming the condition is true | |
|
| |
| 363 | |
| 364 | if (isStrictSubset(VMI, *BestVMI)) |
| 31 | | Forming reference to null pointer |
|
| 365 | continue; |
| 366 | |
| 367 | if (!isStrictSubset(*BestVMI, VMI)) |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | BestVMI = &VMI; |
| 372 | BestVMIIdx = u; |
| 373 | BestScore = Score; |
| 374 | } |
| 375 | |
| 376 | return BestVMIIdx; |
| 377 | } |
| 378 | |
| 379 | TraitSet llvm::omp::getOpenMPContextTraitSetKind(StringRef S) { |
| 380 | return StringSwitch<TraitSet>(S) |
| 381 | #define OMP_TRAIT_SET(Enum, Str) .Case(Str, TraitSet::Enum) |
| 382 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 383 | .Default(TraitSet::invalid); |
| 384 | } |
| 385 | |
| 386 | TraitSet |
| 387 | llvm::omp::getOpenMPContextTraitSetForSelector(TraitSelector Selector) { |
| 388 | switch (Selector) { |
| 389 | #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \ |
| 390 | case TraitSelector::Enum: \ |
| 391 | return TraitSet::TraitSetEnum; |
| 392 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 393 | } |
| 394 | llvm_unreachable("Unknown trait selector!"); |
| 395 | } |
| 396 | TraitSet |
| 397 | llvm::omp::getOpenMPContextTraitSetForProperty(TraitProperty Property) { |
| 398 | switch (Property) { |
| 399 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 400 | case TraitProperty::Enum: \ |
| 401 | return TraitSet::TraitSetEnum; |
| 402 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 403 | } |
| 404 | llvm_unreachable("Unknown trait set!"); |
| 405 | } |
| 406 | StringRef llvm::omp::getOpenMPContextTraitSetName(TraitSet Kind) { |
| 407 | switch (Kind) { |
| 408 | #define OMP_TRAIT_SET(Enum, Str) \ |
| 409 | case TraitSet::Enum: \ |
| 410 | return Str; |
| 411 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 412 | } |
| 413 | llvm_unreachable("Unknown trait set!"); |
| 414 | } |
| 415 | |
| 416 | TraitSelector llvm::omp::getOpenMPContextTraitSelectorKind(StringRef S) { |
| 417 | return StringSwitch<TraitSelector>(S) |
| 418 | #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \ |
| 419 | .Case(Str, TraitSelector::Enum) |
| 420 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 421 | .Default(TraitSelector::invalid); |
| 422 | } |
| 423 | TraitSelector |
| 424 | llvm::omp::getOpenMPContextTraitSelectorForProperty(TraitProperty Property) { |
| 425 | switch (Property) { |
| 426 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 427 | case TraitProperty::Enum: \ |
| 428 | return TraitSelector::TraitSelectorEnum; |
| 429 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 430 | } |
| 431 | llvm_unreachable("Unknown trait set!"); |
| 432 | } |
| 433 | StringRef llvm::omp::getOpenMPContextTraitSelectorName(TraitSelector Kind) { |
| 434 | switch (Kind) { |
| 435 | #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \ |
| 436 | case TraitSelector::Enum: \ |
| 437 | return Str; |
| 438 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 439 | } |
| 440 | llvm_unreachable("Unknown trait selector!"); |
| 441 | } |
| 442 | |
| 443 | TraitProperty llvm::omp::getOpenMPContextTraitPropertyKind( |
| 444 | TraitSet Set, TraitSelector Selector, StringRef S) { |
| 445 | |
| 446 | |
| 447 | if (Set == TraitSet::device && Selector == TraitSelector::device_isa) |
| 448 | return TraitProperty::device_isa___ANY; |
| 449 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 450 | if (Set == TraitSet::TraitSetEnum && Str == S) \ |
| 451 | return TraitProperty::Enum; |
| 452 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 453 | return TraitProperty::invalid; |
| 454 | } |
| 455 | TraitProperty |
| 456 | llvm::omp::getOpenMPContextTraitPropertyForSelector(TraitSelector Selector) { |
| 457 | return StringSwitch<TraitProperty>( |
| 458 | getOpenMPContextTraitSelectorName(Selector)) |
| 459 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 460 | .Case(Str, Selector == TraitSelector::TraitSelectorEnum \ |
| 461 | ? TraitProperty::Enum \ |
| 462 | : TraitProperty::invalid) |
| 463 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 464 | .Default(TraitProperty::invalid); |
| 465 | } |
| 466 | StringRef llvm::omp::getOpenMPContextTraitPropertyName(TraitProperty Kind, |
| 467 | StringRef RawString) { |
| 468 | if (Kind == TraitProperty::device_isa___ANY) |
| 469 | return RawString; |
| 470 | switch (Kind) { |
| 471 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 472 | case TraitProperty::Enum: \ |
| 473 | return Str; |
| 474 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 475 | } |
| 476 | llvm_unreachable("Unknown trait property!"); |
| 477 | } |
| 478 | StringRef llvm::omp::getOpenMPContextTraitPropertyFullName(TraitProperty Kind) { |
| 479 | switch (Kind) { |
| 480 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 481 | case TraitProperty::Enum: \ |
| 482 | return "(" #TraitSetEnum "," #TraitSelectorEnum "," Str ")"; |
| 483 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 484 | } |
| 485 | llvm_unreachable("Unknown trait property!"); |
| 486 | } |
| 487 | |
| 488 | bool llvm::omp::isValidTraitSelectorForTraitSet(TraitSelector Selector, |
| 489 | TraitSet Set, |
| 490 | bool &AllowsTraitScore, |
| 491 | bool &RequiresProperty) { |
| 492 | AllowsTraitScore = Set != TraitSet::construct && Set != TraitSet::device; |
| 493 | switch (Selector) { |
| 494 | #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \ |
| 495 | case TraitSelector::Enum: \ |
| 496 | RequiresProperty = ReqProp; \ |
| 497 | return Set == TraitSet::TraitSetEnum; |
| 498 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 499 | } |
| 500 | llvm_unreachable("Unknown trait selector!"); |
| 501 | } |
| 502 | |
| 503 | bool llvm::omp::isValidTraitPropertyForTraitSetAndSelector( |
| 504 | TraitProperty Property, TraitSelector Selector, TraitSet Set) { |
| 505 | switch (Property) { |
| 506 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 507 | case TraitProperty::Enum: \ |
| 508 | return Set == TraitSet::TraitSetEnum && \ |
| 509 | Selector == TraitSelector::TraitSelectorEnum; |
| 510 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 511 | } |
| 512 | llvm_unreachable("Unknown trait property!"); |
| 513 | } |
| 514 | |
| 515 | std::string llvm::omp::listOpenMPContextTraitSets() { |
| 516 | std::string S; |
| 517 | #define OMP_TRAIT_SET(Enum, Str) \ |
| 518 | if (StringRef(Str) != "invalid") \ |
| 519 | S.append("'").append(Str).append("'").append(" "); |
| 520 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 521 | S.pop_back(); |
| 522 | return S; |
| 523 | } |
| 524 | |
| 525 | std::string llvm::omp::listOpenMPContextTraitSelectors(TraitSet Set) { |
| 526 | std::string S; |
| 527 | #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \ |
| 528 | if (TraitSet::TraitSetEnum == Set && StringRef(Str) != "Invalid") \ |
| 529 | S.append("'").append(Str).append("'").append(" "); |
| 530 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 531 | S.pop_back(); |
| 532 | return S; |
| 533 | } |
| 534 | |
| 535 | std::string |
| 536 | llvm::omp::listOpenMPContextTraitProperties(TraitSet Set, |
| 537 | TraitSelector Selector) { |
| 538 | std::string S; |
| 539 | #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \ |
| 540 | if (TraitSet::TraitSetEnum == Set && \ |
| 541 | TraitSelector::TraitSelectorEnum == Selector && \ |
| 542 | StringRef(Str) != "invalid") \ |
| 543 | S.append("'").append(Str).append("'").append(" "); |
| 544 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
| 545 | if (S.empty()) |
| 546 | return "<none>"; |
| 547 | S.pop_back(); |
| 548 | return S; |
| 549 | } |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #ifndef LLVM_ADT_APINT_H |
| 16 | #define LLVM_ADT_APINT_H |
| 17 | |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/MathExtras.h" |
| 20 | #include <cassert> |
| 21 | #include <climits> |
| 22 | #include <cstring> |
| 23 | #include <utility> |
| 24 | |
| 25 | namespace llvm { |
| 26 | class FoldingSetNodeID; |
| 27 | class StringRef; |
| 28 | class hash_code; |
| 29 | class raw_ostream; |
| 30 | |
| 31 | template <typename T> class SmallVectorImpl; |
| 32 | template <typename T> class ArrayRef; |
| 33 | template <typename T> class Optional; |
| 34 | template <typename T> struct DenseMapInfo; |
| 35 | |
| 36 | class APInt; |
| 37 | |
| 38 | inline APInt operator-(APInt); |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | class LLVM_NODISCARD APInt { |
| 71 | public: |
| 72 | typedef uint64_t WordType; |
| 73 | |
| 74 | |
| 75 | enum : unsigned { |
| 76 | |
| 77 | APINT_WORD_SIZE = sizeof(WordType), |
| 78 | |
| 79 | APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT |
| 80 | }; |
| 81 | |
| 82 | enum class Rounding { |
| 83 | DOWN, |
| 84 | TOWARD_ZERO, |
| 85 | UP, |
| 86 | }; |
| 87 | |
| 88 | static constexpr WordType WORDTYPE_MAX = ~WordType(0); |
| 89 | |
| 90 | private: |
| 91 | |
| 92 | |
| 93 | union { |
| 94 | uint64_t VAL; |
| 95 | uint64_t *pVal; |
| 96 | } U; |
| 97 | |
| 98 | unsigned BitWidth; |
| 99 | |
| 100 | friend struct DenseMapInfo<APInt>; |
| 101 | |
| 102 | friend class APSInt; |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | APInt(uint64_t *val, unsigned bits) : BitWidth(bits) { |
| 109 | U.pVal = val; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | static unsigned whichWord(unsigned bitPosition) { |
| 116 | return bitPosition / APINT_BITS_PER_WORD; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | static unsigned whichBit(unsigned bitPosition) { |
| 124 | return bitPosition % APINT_BITS_PER_WORD; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | static uint64_t maskBit(unsigned bitPosition) { |
| 134 | return 1ULL << whichBit(bitPosition); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | APInt &clearUnusedBits() { |
| 144 | |
| 145 | unsigned WordBits = ((BitWidth-1) % APINT_BITS_PER_WORD) + 1; |
| 146 | |
| 147 | |
| 148 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits); |
| 149 | if (isSingleWord()) |
| 150 | U.VAL &= mask; |
| 151 | else |
| 152 | U.pVal[getNumWords() - 1] &= mask; |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | uint64_t getWord(unsigned bitPosition) const { |
| 159 | return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)]; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | void reallocate(unsigned NewBitWidth); |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | void fromString(unsigned numBits, StringRef str, uint8_t radix); |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | static void divide(const WordType *LHS, unsigned lhsWords, |
| 188 | const WordType *RHS, unsigned rhsWords, WordType *Quotient, |
| 189 | WordType *Remainder); |
| 190 | |
| 191 | |
| 192 | void initSlowCase(uint64_t val, bool isSigned); |
| 193 | |
| 194 | |
| 195 | void initFromArray(ArrayRef<uint64_t> array); |
| 196 | |
| 197 | |
| 198 | void initSlowCase(const APInt &that); |
| 199 | |
| 200 | |
| 201 | void shlSlowCase(unsigned ShiftAmt); |
| 202 | |
| 203 | |
| 204 | void lshrSlowCase(unsigned ShiftAmt); |
| 205 | |
| 206 | |
| 207 | void ashrSlowCase(unsigned ShiftAmt); |
| 208 | |
| 209 | |
| 210 | void AssignSlowCase(const APInt &RHS); |
| 211 | |
| 212 | |
| 213 | bool EqualSlowCase(const APInt &RHS) const LLVM_READONLY; |
| 214 | |
| 215 | |
| 216 | unsigned countLeadingZerosSlowCase() const LLVM_READONLY; |
| 217 | |
| 218 | |
| 219 | unsigned countLeadingOnesSlowCase() const LLVM_READONLY; |
| 220 | |
| 221 | |
| 222 | unsigned countTrailingZerosSlowCase() const LLVM_READONLY; |
| 223 | |
| 224 | |
| 225 | unsigned countTrailingOnesSlowCase() const LLVM_READONLY; |
| 226 | |
| 227 | |
| 228 | unsigned countPopulationSlowCase() const LLVM_READONLY; |
| 229 | |
| 230 | |
| 231 | bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY; |
| 232 | |
| 233 | |
| 234 | bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY; |
| 235 | |
| 236 | |
| 237 | void setBitsSlowCase(unsigned loBit, unsigned hiBit); |
| 238 | |
| 239 | |
| 240 | void flipAllBitsSlowCase(); |
| 241 | |
| 242 | |
| 243 | void AndAssignSlowCase(const APInt& RHS); |
| 244 | |
| 245 | |
| 246 | void OrAssignSlowCase(const APInt& RHS); |
| 247 | |
| 248 | |
| 249 | void XorAssignSlowCase(const APInt& RHS); |
| 250 | |
| 251 | |
| 252 | |
| 253 | int compare(const APInt &RHS) const LLVM_READONLY; |
| 254 | |
| 255 | |
| 256 | |
| 257 | int compareSigned(const APInt &RHS) const LLVM_READONLY; |
| 258 | |
| 259 | public: |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | APInt(unsigned numBits, uint64_t val, bool isSigned = false) |
| 274 | : BitWidth(numBits) { |
| 275 | assert(BitWidth && "bitwidth too small"); |
| 276 | if (isSingleWord()) { |
| 277 | U.VAL = val; |
| 278 | clearUnusedBits(); |
| 279 | } else { |
| 280 | initSlowCase(val, isSigned); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | APInt(unsigned numBits, ArrayRef<uint64_t> bigVal); |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]); |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | APInt(unsigned numBits, StringRef str, uint8_t radix); |
| 314 | |
| 315 | |
| 316 | |
| 317 | APInt(const APInt &that) : BitWidth(that.BitWidth) { |
| 318 | if (isSingleWord()) |
| 319 | U.VAL = that.U.VAL; |
| 320 | else |
| 321 | initSlowCase(that); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | APInt(APInt &&that) : BitWidth(that.BitWidth) { |
| 326 | memcpy(&U, &that.U, sizeof(U)); |
| 327 | that.BitWidth = 0; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | ~APInt() { |
| 332 | if (needsCleanup()) |
| 333 | delete[] U.pVal; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | explicit APInt() : BitWidth(1) { U.VAL = 0; } |
| 342 | |
| 343 | |
| 344 | bool needsCleanup() const { return !isSingleWord(); } |
| 345 | |
| 346 | |
| 347 | |
| 348 | void Profile(FoldingSetNodeID &id) const; |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; } |
| 21 | | Assuming field 'BitWidth' is > APINT_BITS_PER_WORD | |
|
| 22 | | Returning zero, which participates in a condition later | |
|
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | bool isNegative() const { return (*this)[BitWidth - 1]; } |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | bool isNonNegative() const { return !isNegative(); } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | bool isSignBitSet() const { return (*this)[BitWidth-1]; } |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | bool isSignBitClear() const { return !isSignBitSet(); } |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); } |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | bool isNonPositive() const { return !isStrictlyPositive(); } |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | bool isAllOnesValue() const { |
| 402 | if (isSingleWord()) |
| 403 | return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth); |
| 404 | return countTrailingOnesSlowCase() == BitWidth; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | bool isNullValue() const { return !*this; } |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | bool isOneValue() const { |
| 417 | if (isSingleWord()) |
| 418 | return U.VAL == 1; |
| 419 | return countLeadingZerosSlowCase() == BitWidth - 1; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | bool isMaxValue() const { return isAllOnesValue(); } |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | bool isMaxSignedValue() const { |
| 433 | if (isSingleWord()) |
| 434 | return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1); |
| 435 | return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | bool isMinValue() const { return isNullValue(); } |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | bool isMinSignedValue() const { |
| 449 | if (isSingleWord()) |
| 450 | return U.VAL == (WordType(1) << (BitWidth - 1)); |
| 451 | return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | bool isIntN(unsigned N) const { |
| 456 | assert(N && "N == 0 ???"); |
| 457 | return getActiveBits() <= N; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | bool isSignedIntN(unsigned N) const { |
| 462 | assert(N && "N == 0 ???"); |
| 463 | return getMinSignedBits() <= N; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | bool isPowerOf2() const { |
| 470 | if (isSingleWord()) |
| 471 | return isPowerOf2_64(U.VAL); |
| 472 | return countPopulationSlowCase() == 1; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | bool isSignMask() const { return isMinSignedValue(); } |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | bool getBoolValue() const { return !!*this; } |
| 484 | |
| 485 | |
| 486 | |
| 487 | uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) const { |
| 488 | return ugt(Limit) ? Limit : getZExtValue(); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | bool isSplat(unsigned SplatSizeInBits) const; |
| 497 | |
| 498 | |
| 499 | |
| 500 | bool isMask(unsigned numBits) const { |
| 501 | assert(numBits != 0 && "numBits must be non-zero"); |
| 502 | assert(numBits <= BitWidth && "numBits out of range"); |
| 503 | if (isSingleWord()) |
| 504 | return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits)); |
| 505 | unsigned Ones = countTrailingOnesSlowCase(); |
| 506 | return (numBits == Ones) && |
| 507 | ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | bool isMask() const { |
| 514 | if (isSingleWord()) |
| 515 | return isMask_64(U.VAL); |
| 516 | unsigned Ones = countTrailingOnesSlowCase(); |
| 517 | return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | |
| 522 | bool isShiftedMask() const { |
| 523 | if (isSingleWord()) |
| 524 | return isShiftedMask_64(U.VAL); |
| 525 | unsigned Ones = countPopulationSlowCase(); |
| 526 | unsigned LeadZ = countLeadingZerosSlowCase(); |
| 527 | return (Ones + LeadZ + countTrailingZeros()) == BitWidth; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | static APInt getMaxValue(unsigned numBits) { |
| 536 | return getAllOnesValue(numBits); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | static APInt getSignedMaxValue(unsigned numBits) { |
| 541 | APInt API = getAllOnesValue(numBits); |
| 542 | API.clearBit(numBits - 1); |
| 543 | return API; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); } |
| 548 | |
| 549 | |
| 550 | static APInt getSignedMinValue(unsigned numBits) { |
| 551 | APInt API(numBits, 0); |
| 552 | API.setBit(numBits - 1); |
| 553 | return API; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | static APInt getSignMask(unsigned BitWidth) { |
| 561 | return getSignedMinValue(BitWidth); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | static APInt getAllOnesValue(unsigned numBits) { |
| 568 | return APInt(numBits, WORDTYPE_MAX, true); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); } |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | APInt getHiBits(unsigned numBits) const; |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | APInt getLoBits(unsigned numBits) const; |
| 591 | |
| 592 | |
| 593 | static APInt getOneBitSet(unsigned numBits, unsigned BitNo) { |
| 594 | APInt Res(numBits, 0); |
| 595 | Res.setBit(BitNo); |
| 596 | return Res; |
| 597 | } |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) { |
| 613 | assert(loBit <= hiBit && "loBit greater than hiBit"); |
| 614 | APInt Res(numBits, 0); |
| 615 | Res.setBits(loBit, hiBit); |
| 616 | return Res; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit, |
| 626 | unsigned hiBit) { |
| 627 | APInt Res(numBits, 0); |
| 628 | Res.setBitsWithWrap(loBit, hiBit); |
| 629 | return Res; |
| 630 | } |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) { |
| 644 | APInt Res(numBits, 0); |
| 645 | Res.setBitsFrom(loBit); |
| 646 | return Res; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) { |
| 656 | APInt Res(numBits, 0); |
| 657 | Res.setHighBits(hiBitsSet); |
| 658 | return Res; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) { |
| 668 | APInt Res(numBits, 0); |
| 669 | Res.setLowBits(loBitsSet); |
| 670 | return Res; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | static APInt getSplat(unsigned NewLen, const APInt &V); |
| 675 | |
| 676 | |
| 677 | |
| 678 | static bool isSameValue(const APInt &I1, const APInt &I2) { |
| 679 | if (I1.getBitWidth() == I2.getBitWidth()) |
| 680 | return I1 == I2; |
| 681 | |
| 682 | if (I1.getBitWidth() > I2.getBitWidth()) |
| 683 | return I1 == I2.zext(I1.getBitWidth()); |
| 684 | |
| 685 | return I1.zext(I2.getBitWidth()) == I2; |
| 686 | } |
| 687 | |
| 688 | |
| 689 | friend hash_code hash_value(const APInt &Arg); |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | const uint64_t *getRawData() const { |
| 695 | if (isSingleWord()) |
| 696 | return &U.VAL; |
| 697 | return &U.pVal[0]; |
| 698 | } |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | const APInt operator++(int) { |
| 710 | APInt API(*this); |
| 711 | ++(*this); |
| 712 | return API; |
| 713 | } |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | APInt &operator++(); |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | const APInt operator--(int) { |
| 726 | APInt API(*this); |
| 727 | --(*this); |
| 728 | return API; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | APInt &operator--(); |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | bool operator!() const { |
| 742 | if (isSingleWord()) |
| 743 | return U.VAL == 0; |
| 744 | return countLeadingZerosSlowCase() == BitWidth; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | APInt &operator=(const APInt &RHS) { |
| 755 | |
| 756 | if (isSingleWord() && RHS.isSingleWord()) { |
| 757 | U.VAL = RHS.U.VAL; |
| 758 | BitWidth = RHS.BitWidth; |
| 759 | return clearUnusedBits(); |
| 760 | } |
| 761 | |
| 762 | AssignSlowCase(RHS); |
| 763 | return *this; |
| 764 | } |
| 765 | |
| 766 | |
| 767 | APInt &operator=(APInt &&that) { |
| 768 | #ifdef EXPENSIVE_CHECKS |
| 769 | |
| 770 | if (this == &that) |
| 771 | return *this; |
| 772 | #endif |
| 773 | assert(this != &that && "Self-move not supported"); |
| 774 | if (!isSingleWord()) |
| 775 | delete[] U.pVal; |
| 776 | |
| 777 | |
| 778 | |
| 779 | memcpy(&U, &that.U, sizeof(U)); |
| 780 | |
| 781 | BitWidth = that.BitWidth; |
| 782 | that.BitWidth = 0; |
| 783 | |
| 784 | return *this; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | APInt &operator=(uint64_t RHS) { |
| 795 | if (isSingleWord()) { |
| 796 | U.VAL = RHS; |
| 797 | return clearUnusedBits(); |
| 798 | } |
| 799 | U.pVal[0] = RHS; |
| 800 | memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
| 801 | return *this; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | APInt &operator&=(const APInt &RHS) { |
| 811 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 812 | if (isSingleWord()) |
| 813 | U.VAL &= RHS.U.VAL; |
| 814 | else |
| 815 | AndAssignSlowCase(RHS); |
| 816 | return *this; |
| 817 | } |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | APInt &operator&=(uint64_t RHS) { |
| 825 | if (isSingleWord()) { |
| 826 | U.VAL &= RHS; |
| 827 | return *this; |
| 828 | } |
| 829 | U.pVal[0] &= RHS; |
| 830 | memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
| 831 | return *this; |
| 832 | } |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | APInt &operator|=(const APInt &RHS) { |
| 841 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 842 | if (isSingleWord()) |
| 843 | U.VAL |= RHS.U.VAL; |
| 844 | else |
| 845 | OrAssignSlowCase(RHS); |
| 846 | return *this; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | APInt &operator|=(uint64_t RHS) { |
| 855 | if (isSingleWord()) { |
| 856 | U.VAL |= RHS; |
| 857 | return clearUnusedBits(); |
| 858 | } |
| 859 | U.pVal[0] |= RHS; |
| 860 | return *this; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | APInt &operator^=(const APInt &RHS) { |
| 870 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 871 | if (isSingleWord()) |
| 872 | U.VAL ^= RHS.U.VAL; |
| 873 | else |
| 874 | XorAssignSlowCase(RHS); |
| 875 | return *this; |
| 876 | } |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | APInt &operator^=(uint64_t RHS) { |
| 884 | if (isSingleWord()) { |
| 885 | U.VAL ^= RHS; |
| 886 | return clearUnusedBits(); |
| 887 | } |
| 888 | U.pVal[0] ^= RHS; |
| 889 | return *this; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | APInt &operator*=(const APInt &RHS); |
| 898 | APInt &operator*=(uint64_t RHS); |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | APInt &operator+=(const APInt &RHS); |
| 906 | APInt &operator+=(uint64_t RHS); |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | APInt &operator-=(const APInt &RHS); |
| 914 | APInt &operator-=(uint64_t RHS); |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | APInt &operator<<=(unsigned ShiftAmt) { |
| 922 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
| 923 | if (isSingleWord()) { |
| 924 | if (ShiftAmt == BitWidth) |
| 925 | U.VAL = 0; |
| 926 | else |
| 927 | U.VAL <<= ShiftAmt; |
| 928 | return clearUnusedBits(); |
| 929 | } |
| 930 | shlSlowCase(ShiftAmt); |
| 931 | return *this; |
| 932 | } |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | APInt &operator<<=(const APInt &ShiftAmt); |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | APInt operator*(const APInt &RHS) const; |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | APInt operator<<(unsigned Bits) const { return shl(Bits); } |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | APInt operator<<(const APInt &Bits) const { return shl(Bits); } |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | APInt ashr(unsigned ShiftAmt) const { |
| 964 | APInt R(*this); |
| 965 | R.ashrInPlace(ShiftAmt); |
| 966 | return R; |
| 967 | } |
| 968 | |
| 969 | |
| 970 | void ashrInPlace(unsigned ShiftAmt) { |
| 971 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
| 972 | if (isSingleWord()) { |
| 973 | int64_t SExtVAL = SignExtend64(U.VAL, BitWidth); |
| 974 | if (ShiftAmt == BitWidth) |
| 975 | U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); |
| 976 | else |
| 977 | U.VAL = SExtVAL >> ShiftAmt; |
| 978 | clearUnusedBits(); |
| 979 | return; |
| 980 | } |
| 981 | ashrSlowCase(ShiftAmt); |
| 982 | } |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | APInt lshr(unsigned shiftAmt) const { |
| 988 | APInt R(*this); |
| 989 | R.lshrInPlace(shiftAmt); |
| 990 | return R; |
| 991 | } |
| 992 | |
| 993 | |
| 994 | void lshrInPlace(unsigned ShiftAmt) { |
| 995 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
| 996 | if (isSingleWord()) { |
| 997 | if (ShiftAmt == BitWidth) |
| 998 | U.VAL = 0; |
| 999 | else |
| 1000 | U.VAL >>= ShiftAmt; |
| 1001 | return; |
| 1002 | } |
| 1003 | lshrSlowCase(ShiftAmt); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | APInt shl(unsigned shiftAmt) const { |
| 1010 | APInt R(*this); |
| 1011 | R <<= shiftAmt; |
| 1012 | return R; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | APInt rotl(unsigned rotateAmt) const; |
| 1017 | |
| 1018 | |
| 1019 | APInt rotr(unsigned rotateAmt) const; |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | APInt ashr(const APInt &ShiftAmt) const { |
| 1025 | APInt R(*this); |
| 1026 | R.ashrInPlace(ShiftAmt); |
| 1027 | return R; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | void ashrInPlace(const APInt &shiftAmt); |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | APInt lshr(const APInt &ShiftAmt) const { |
| 1037 | APInt R(*this); |
| 1038 | R.lshrInPlace(ShiftAmt); |
| 1039 | return R; |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | void lshrInPlace(const APInt &ShiftAmt); |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | APInt shl(const APInt &ShiftAmt) const { |
| 1049 | APInt R(*this); |
| 1050 | R <<= ShiftAmt; |
| 1051 | return R; |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | APInt rotl(const APInt &rotateAmt) const; |
| 1056 | |
| 1057 | |
| 1058 | APInt rotr(const APInt &rotateAmt) const; |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | APInt udiv(const APInt &RHS) const; |
| 1068 | APInt udiv(uint64_t RHS) const; |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | APInt sdiv(const APInt &RHS) const; |
| 1076 | APInt sdiv(int64_t RHS) const; |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | APInt urem(const APInt &RHS) const; |
| 1088 | uint64_t urem(uint64_t RHS) const; |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | APInt srem(const APInt &RHS) const; |
| 1094 | int64_t srem(int64_t RHS) const; |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
| 1104 | APInt &Remainder); |
| 1105 | static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, |
| 1106 | uint64_t &Remainder); |
| 1107 | |
| 1108 | static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
| 1109 | APInt &Remainder); |
| 1110 | static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient, |
| 1111 | int64_t &Remainder); |
| 1112 | |
| 1113 | |
| 1114 | APInt sadd_ov(const APInt &RHS, bool &Overflow) const; |
| 1115 | APInt uadd_ov(const APInt &RHS, bool &Overflow) const; |
| 1116 | APInt ssub_ov(const APInt &RHS, bool &Overflow) const; |
| 1117 | APInt usub_ov(const APInt &RHS, bool &Overflow) const; |
| 1118 | APInt sdiv_ov(const APInt &RHS, bool &Overflow) const; |
| 1119 | APInt smul_ov(const APInt &RHS, bool &Overflow) const; |
| 1120 | APInt umul_ov(const APInt &RHS, bool &Overflow) const; |
| 1121 | APInt sshl_ov(const APInt &Amt, bool &Overflow) const; |
| 1122 | APInt ushl_ov(const APInt &Amt, bool &Overflow) const; |
| 1123 | |
| 1124 | |
| 1125 | APInt sadd_sat(const APInt &RHS) const; |
| 1126 | APInt uadd_sat(const APInt &RHS) const; |
| 1127 | APInt ssub_sat(const APInt &RHS) const; |
| 1128 | APInt usub_sat(const APInt &RHS) const; |
| 1129 | APInt smul_sat(const APInt &RHS) const; |
| 1130 | APInt umul_sat(const APInt &RHS) const; |
| 1131 | APInt sshl_sat(const APInt &RHS) const; |
| 1132 | APInt ushl_sat(const APInt &RHS) const; |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | bool operator[](unsigned bitPosition) const { |
| 1138 | assert(bitPosition < getBitWidth() && "Bit position out of bounds!"); |
| 1139 | return (maskBit(bitPosition) & getWord(bitPosition)) != 0; |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | bool operator==(const APInt &RHS) const { |
| 1151 | assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"); |
| 1152 | if (isSingleWord()) |
| 20 | | Calling 'APInt::isSingleWord' | |
|
| 23 | | Returning from 'APInt::isSingleWord' | |
|
| |
| 1153 | return U.VAL == RHS.U.VAL; |
| 1154 | return EqualSlowCase(RHS); |
| 25 | | Returning value, which participates in a condition later | |
|
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | bool operator==(uint64_t Val) const { |
| 1164 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val; |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | bool eq(const APInt &RHS) const { return (*this) == RHS; } |
| 19 | | Calling 'APInt::operator==' | |
|
| 26 | | Returning from 'APInt::operator==' | |
|
| 27 | | Returning value, which participates in a condition later | |
|
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | bool operator!=(const APInt &RHS) const { return !((*this) == RHS); } |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | bool operator!=(uint64_t Val) const { return !((*this) == Val); } |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | bool ne(const APInt &RHS) const { return !((*this) == RHS); } |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | bool ult(const APInt &RHS) const { return compare(RHS) < 0; } |
| 14 | | Assuming the condition is false | |
|
| 15 | | Returning zero, which participates in a condition later | |
|
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | bool ult(uint64_t RHS) const { |
| 1214 | |
| 1215 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS; |
| 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; } |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | bool slt(int64_t RHS) const { |
| 1233 | return (!isSingleWord() && getMinSignedBits() > 64) ? isNegative() |
| 1234 | : getSExtValue() < RHS; |
| 1235 | } |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | bool ule(const APInt &RHS) const { return compare(RHS) <= 0; } |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | bool ule(uint64_t RHS) const { return !ugt(RHS); } |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; } |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | bool sle(uint64_t RHS) const { return !sgt(RHS); } |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | bool ugt(const APInt &RHS) const { return !ule(RHS); } |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | bool ugt(uint64_t RHS) const { |
| 1284 | |
| 1285 | return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS; |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | bool sgt(const APInt &RHS) const { return !sle(RHS); } |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | bool sgt(int64_t RHS) const { |
| 1303 | return (!isSingleWord() && getMinSignedBits() > 64) ? !isNegative() |
| 1304 | : getSExtValue() > RHS; |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | bool uge(const APInt &RHS) const { return !ult(RHS); } |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | bool uge(uint64_t RHS) const { return !ult(RHS); } |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | bool sge(const APInt &RHS) const { return !slt(RHS); } |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | bool sge(int64_t RHS) const { return !slt(RHS); } |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | bool intersects(const APInt &RHS) const { |
| 1342 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 1343 | if (isSingleWord()) |
| 1344 | return (U.VAL & RHS.U.VAL) != 0; |
| 1345 | return intersectsSlowCase(RHS); |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | bool isSubsetOf(const APInt &RHS) const { |
| 1350 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 1351 | if (isSingleWord()) |
| 1352 | return (U.VAL & ~RHS.U.VAL) == 0; |
| 1353 | return isSubsetOfSlowCase(RHS); |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | |
| 1363 | |
| 1364 | APInt trunc(unsigned width) const; |
| 1365 | |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | APInt truncUSat(unsigned width) const; |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | |
| 1376 | |
| 1377 | APInt truncSSat(unsigned width) const; |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | APInt sext(unsigned width) const; |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | APInt zext(unsigned width) const; |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | APInt sextOrTrunc(unsigned width) const; |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | APInt zextOrTrunc(unsigned width) const; |
| 1405 | |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | APInt truncOrSelf(unsigned width) const; |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | APInt sextOrSelf(unsigned width) const; |
| 1417 | |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | APInt zextOrSelf(unsigned width) const; |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | void setAllBits() { |
| 1430 | if (isSingleWord()) |
| 1431 | U.VAL = WORDTYPE_MAX; |
| 1432 | else |
| 1433 | |
| 1434 | memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE); |
| 1435 | |
| 1436 | clearUnusedBits(); |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | void setBit(unsigned BitPosition) { |
| 1443 | assert(BitPosition < BitWidth && "BitPosition out of range"); |
| 1444 | WordType Mask = maskBit(BitPosition); |
| 1445 | if (isSingleWord()) |
| 1446 | U.VAL |= Mask; |
| 1447 | else |
| 1448 | U.pVal[whichWord(BitPosition)] |= Mask; |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | void setSignBit() { |
| 1453 | setBit(BitWidth - 1); |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | void setBitVal(unsigned BitPosition, bool BitValue) { |
| 1458 | if (BitValue) |
| 1459 | setBit(BitPosition); |
| 1460 | else |
| 1461 | clearBit(BitPosition); |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | void setBitsWithWrap(unsigned loBit, unsigned hiBit) { |
| 1469 | assert(hiBit <= BitWidth && "hiBit out of range"); |
| 1470 | assert(loBit <= BitWidth && "loBit out of range"); |
| 1471 | if (loBit < hiBit) { |
| 1472 | setBits(loBit, hiBit); |
| 1473 | return; |
| 1474 | } |
| 1475 | setLowBits(hiBit); |
| 1476 | setHighBits(BitWidth - loBit); |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | |
| 1481 | void setBits(unsigned loBit, unsigned hiBit) { |
| 1482 | assert(hiBit <= BitWidth && "hiBit out of range"); |
| 1483 | assert(loBit <= BitWidth && "loBit out of range"); |
| 1484 | assert(loBit <= hiBit && "loBit greater than hiBit"); |
| 1485 | if (loBit == hiBit) |
| 1486 | return; |
| 1487 | if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { |
| 1488 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit)); |
| 1489 | mask <<= loBit; |
| 1490 | if (isSingleWord()) |
| 1491 | U.VAL |= mask; |
| 1492 | else |
| 1493 | U.pVal[0] |= mask; |
| 1494 | } else { |
| 1495 | setBitsSlowCase(loBit, hiBit); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | void setBitsFrom(unsigned loBit) { |
| 1501 | return setBits(loBit, BitWidth); |
| 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | void setLowBits(unsigned loBits) { |
| 1506 | return setBits(0, loBits); |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | void setHighBits(unsigned hiBits) { |
| 1511 | return setBits(BitWidth - hiBits, BitWidth); |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | void clearAllBits() { |
| 1516 | if (isSingleWord()) |
| 1517 | U.VAL = 0; |
| 1518 | else |
| 1519 | memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE); |
| 1520 | } |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | |
| 1525 | void clearBit(unsigned BitPosition) { |
| 1526 | assert(BitPosition < BitWidth && "BitPosition out of range"); |
| 1527 | WordType Mask = ~maskBit(BitPosition); |
| 1528 | if (isSingleWord()) |
| 1529 | U.VAL &= Mask; |
| 1530 | else |
| 1531 | U.pVal[whichWord(BitPosition)] &= Mask; |
| 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | void clearLowBits(unsigned loBits) { |
| 1536 | assert(loBits <= BitWidth && "More bits than bitwidth"); |
| 1537 | APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits); |
| 1538 | *this &= Keep; |
| 1539 | } |
| 1540 | |
| 1541 | |
| 1542 | void clearSignBit() { |
| 1543 | clearBit(BitWidth - 1); |
| 1544 | } |
| 1545 | |
| 1546 | |
| 1547 | void flipAllBits() { |
| 1548 | if (isSingleWord()) { |
| 1549 | U.VAL ^= WORDTYPE_MAX; |
| 1550 | clearUnusedBits(); |
| 1551 | } else { |
| 1552 | flipAllBitsSlowCase(); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | void flipBit(unsigned bitPosition); |
| 1561 | |
| 1562 | |
| 1563 | void negate() { |
| 1564 | flipAllBits(); |
| 1565 | ++(*this); |
| 1566 | } |
| 1567 | |
| 1568 | |
| 1569 | void insertBits(const APInt &SubBits, unsigned bitPosition); |
| 1570 | void insertBits(uint64_t SubBits, unsigned bitPosition, unsigned numBits); |
| 1571 | |
| 1572 | |
| 1573 | APInt extractBits(unsigned numBits, unsigned bitPosition) const; |
| 1574 | uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const; |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | |
| 1579 | |
| 1580 | |
| 1581 | unsigned getBitWidth() const { return BitWidth; } |
| 1582 | |
| 1583 | |
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | unsigned getNumWords() const { return getNumWords(BitWidth); } |
| 1589 | |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | static unsigned getNumWords(unsigned BitWidth) { |
| 1597 | return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; |
| 1598 | } |
| 1599 | |
| 1600 | |
| 1601 | |
| 1602 | |
| 1603 | |
| 1604 | |
| 1605 | unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); } |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | |
| 1610 | |
| 1611 | unsigned getActiveWords() const { |
| 1612 | unsigned numActiveBits = getActiveBits(); |
| 1613 | return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1; |
| 1614 | } |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | |
| 1623 | |
| 1624 | unsigned getMinSignedBits() const { return BitWidth - getNumSignBits() + 1; } |
| 1625 | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | |
| 1631 | uint64_t getZExtValue() const { |
| 1632 | if (isSingleWord()) |
| 1633 | return U.VAL; |
| 1634 | assert(getActiveBits() <= 64 && "Too many bits for uint64_t"); |
| 1635 | return U.pVal[0]; |
| 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | int64_t getSExtValue() const { |
| 1644 | if (isSingleWord()) |
| 1645 | return SignExtend64(U.VAL, BitWidth); |
| 1646 | assert(getMinSignedBits() <= 64 && "Too many bits for int64_t"); |
| 1647 | return int64_t(U.pVal[0]); |
| 1648 | } |
| 1649 | |
| 1650 | |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | static unsigned getBitsNeeded(StringRef str, uint8_t radix); |
| 1655 | |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | |
| 1661 | |
| 1662 | |
| 1663 | |
| 1664 | unsigned countLeadingZeros() const { |
| 1665 | if (isSingleWord()) { |
| 1666 | unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth; |
| 1667 | return llvm::countLeadingZeros(U.VAL) - unusedBits; |
| 1668 | } |
| 1669 | return countLeadingZerosSlowCase(); |
| 1670 | } |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | |
| 1680 | unsigned countLeadingOnes() const { |
| 1681 | if (isSingleWord()) |
| 1682 | return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth)); |
| 1683 | return countLeadingOnesSlowCase(); |
| 1684 | } |
| 1685 | |
| 1686 | |
| 1687 | |
| 1688 | unsigned getNumSignBits() const { |
| 1689 | return isNegative() ? countLeadingOnes() : countLeadingZeros(); |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | |
| 1695 | |
| 1696 | |
| 1697 | |
| 1698 | |
| 1699 | |
| 1700 | unsigned countTrailingZeros() const { |
| 1701 | if (isSingleWord()) { |
| 1702 | unsigned TrailingZeros = llvm::countTrailingZeros(U.VAL); |
| 1703 | return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros); |
| 1704 | } |
| 1705 | return countTrailingZerosSlowCase(); |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | |
| 1711 | |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | |
| 1716 | unsigned countTrailingOnes() const { |
| 1717 | if (isSingleWord()) |
| 1718 | return llvm::countTrailingOnes(U.VAL); |
| 1719 | return countTrailingOnesSlowCase(); |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | |
| 1724 | |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | unsigned countPopulation() const { |
| 1729 | if (isSingleWord()) |
| 1730 | return llvm::countPopulation(U.VAL); |
| 1731 | return countPopulationSlowCase(); |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | |
| 1736 | |
| 1737 | void print(raw_ostream &OS, bool isSigned) const; |
| 1738 | |
| 1739 | |
| 1740 | |
| 1741 | void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed, |
| 1742 | bool formatAsCLiteral = false) const; |
| 1743 | |
| 1744 | |
| 1745 | |
| 1746 | void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
| 1747 | toString(Str, Radix, false, false); |
| 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | |
| 1752 | void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
| 1753 | toString(Str, Radix, true, false); |
| 1754 | } |
| 1755 | |
| 1756 | |
| 1757 | APInt byteSwap() const; |
| 1758 | |
| 1759 | |
| 1760 | |
| 1761 | APInt reverseBits() const; |
| 1762 | |
| 1763 | |
| 1764 | double roundToDouble(bool isSigned) const; |
| 1765 | |
| 1766 | |
| 1767 | double roundToDouble() const { return roundToDouble(false); } |
| 1768 | |
| 1769 | |
| 1770 | double signedRoundToDouble() const { return roundToDouble(true); } |
| 1771 | |
| 1772 | |
| 1773 | |
| 1774 | |
| 1775 | |
| 1776 | |
| 1777 | double bitsToDouble() const { |
| 1778 | return BitsToDouble(getWord(0)); |
| 1779 | } |
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | |
| 1786 | float bitsToFloat() const { |
| 1787 | return BitsToFloat(static_cast<uint32_t>(getWord(0))); |
| 1788 | } |
| 1789 | |
| 1790 | |
| 1791 | |
| 1792 | |
| 1793 | |
| 1794 | static APInt doubleToBits(double V) { |
| 1795 | return APInt(sizeof(double) * CHAR_BIT, DoubleToBits(V)); |
| 1796 | } |
| 1797 | |
| 1798 | |
| 1799 | |
| 1800 | |
| 1801 | |
| 1802 | static APInt floatToBits(float V) { |
| 1803 | return APInt(sizeof(float) * CHAR_BIT, FloatToBits(V)); |
| 1804 | } |
| 1805 | |
| 1806 | |
| 1807 | |
| 1808 | |
| 1809 | |
| 1810 | |
| 1811 | unsigned logBase2() const { return getActiveBits() - 1; } |
| 1812 | |
| 1813 | |
| 1814 | unsigned ceilLogBase2() const { |
| 1815 | APInt temp(*this); |
| 1816 | --temp; |
| 1817 | return temp.getActiveBits(); |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | |
| 1826 | |
| 1827 | |
| 1828 | |
| 1829 | unsigned nearestLogBase2() const { |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | if (BitWidth == 1) |
| 1834 | return U.VAL - 1; |
| 1835 | |
| 1836 | |
| 1837 | if (isNullValue()) |
| 1838 | return UINT32_MAX; |
| 1839 | |
| 1840 | |
| 1841 | |
| 1842 | |
| 1843 | |
| 1844 | |
| 1845 | unsigned lg = logBase2(); |
| 1846 | return lg + unsigned((*this)[lg - 1]); |
| 1847 | } |
| 1848 | |
| 1849 | |
| 1850 | |
| 1851 | int32_t exactLogBase2() const { |
| 1852 | if (!isPowerOf2()) |
| 1853 | return -1; |
| 1854 | return logBase2(); |
| 1855 | } |
| 1856 | |
| 1857 | |
| 1858 | APInt sqrt() const; |
| 1859 | |
| 1860 | |
| 1861 | |
| 1862 | |
| 1863 | APInt abs() const { |
| 1864 | if (isNegative()) |
| 1865 | return -(*this); |
| 1866 | return *this; |
| 1867 | } |
| 1868 | |
| 1869 | |
| 1870 | APInt multiplicativeInverse(const APInt &modulo) const; |
| 1871 | |
| 1872 | |
| 1873 | |
| 1874 | |
| 1875 | |
| 1876 | |
| 1877 | struct ms; |
| 1878 | ms magic() const; |
| 1879 | |
| 1880 | |
| 1881 | struct mu; |
| 1882 | mu magicu(unsigned LeadingZeros = 0) const; |
| 1883 | |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | static void tcSet(WordType *, WordType, unsigned); |
| 1897 | |
| 1898 | |
| 1899 | static void tcAssign(WordType *, const WordType *, unsigned); |
| 1900 | |
| 1901 | |
| 1902 | static bool tcIsZero(const WordType *, unsigned); |
| 1903 | |
| 1904 | |
| 1905 | static int tcExtractBit(const WordType *, unsigned bit); |
| 1906 | |
| 1907 | |
| 1908 | |
| 1909 | |
| 1910 | |
| 1911 | static void tcExtract(WordType *, unsigned dstCount, |
| 1912 | const WordType *, unsigned srcBits, |
| 1913 | unsigned srcLSB); |
| 1914 | |
| 1915 | |
| 1916 | static void tcSetBit(WordType *, unsigned bit); |
| 1917 | |
| 1918 | |
| 1919 | static void tcClearBit(WordType *, unsigned bit); |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | static unsigned tcLSB(const WordType *, unsigned n); |
| 1924 | static unsigned tcMSB(const WordType *parts, unsigned n); |
| 1925 | |
| 1926 | |
| 1927 | static void tcNegate(WordType *, unsigned); |
| 1928 | |
| 1929 | |
| 1930 | static WordType tcAdd(WordType *, const WordType *, |
| 1931 | WordType carry, unsigned); |
| 1932 | |
| 1933 | static WordType tcAddPart(WordType *, WordType, unsigned); |
| 1934 | |
| 1935 | |
| 1936 | static WordType tcSubtract(WordType *, const WordType *, |
| 1937 | WordType carry, unsigned); |
| 1938 | |
| 1939 | static WordType tcSubtractPart(WordType *, WordType, unsigned); |
| 1940 | |
| 1941 | |
| 1942 | |
| 1943 | |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | static int tcMultiplyPart(WordType *dst, const WordType *src, |
| 1952 | WordType multiplier, WordType carry, |
| 1953 | unsigned srcParts, unsigned dstParts, |
| 1954 | bool add); |
| 1955 | |
| 1956 | |
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 | static int tcMultiply(WordType *, const WordType *, const WordType *, |
| 1961 | unsigned); |
| 1962 | |
| 1963 | |
| 1964 | |
| 1965 | static void tcFullMultiply(WordType *, const WordType *, |
| 1966 | const WordType *, unsigned, unsigned); |
| 1967 | |
| 1968 | |
| 1969 | |
| 1970 | |
| 1971 | |
| 1972 | |
| 1973 | |
| 1974 | |
| 1975 | |
| 1976 | |
| 1977 | static int tcDivide(WordType *lhs, const WordType *rhs, |
| 1978 | WordType *remainder, WordType *scratch, |
| 1979 | unsigned parts); |
| 1980 | |
| 1981 | |
| 1982 | |
| 1983 | static void tcShiftLeft(WordType *, unsigned Words, unsigned Count); |
| 1984 | |
| 1985 | |
| 1986 | |
| 1987 | static void tcShiftRight(WordType *, unsigned Words, unsigned Count); |
| 1988 | |
| 1989 | |
| 1990 | static void tcAnd(WordType *, const WordType *, unsigned); |
| 1991 | static void tcOr(WordType *, const WordType *, unsigned); |
| 1992 | static void tcXor(WordType *, const WordType *, unsigned); |
| 1993 | static void tcComplement(WordType *, unsigned); |
| 1994 | |
| 1995 | |
| 1996 | static int tcCompare(const WordType *, const WordType *, unsigned); |
| 1997 | |
| 1998 | |
| 1999 | static WordType tcIncrement(WordType *dst, unsigned parts) { |
| 2000 | return tcAddPart(dst, 1, parts); |
| 2001 | } |
| 2002 | |
| 2003 | |
| 2004 | static WordType tcDecrement(WordType *dst, unsigned parts) { |
| 2005 | return tcSubtractPart(dst, 1, parts); |
| 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits); |
| 2010 | |
| 2011 | |
| 2012 | void dump() const; |
| 2013 | |
| 2014 | |
| 2015 | }; |
| 2016 | |
| 2017 | |
| 2018 | struct APInt::ms { |
| 2019 | APInt m; |
| 2020 | unsigned s; |
| 2021 | }; |
| 2022 | |
| 2023 | |
| 2024 | struct APInt::mu { |
| 2025 | APInt m; |
| 2026 | bool a; |
| 2027 | unsigned s; |
| 2028 | }; |
| 2029 | |
| 2030 | inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; } |
| 2031 | |
| 2032 | inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; } |
| 2033 | |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | inline APInt operator~(APInt v) { |
| 2038 | v.flipAllBits(); |
| 2039 | return v; |
| 2040 | } |
| 2041 | |
| 2042 | inline APInt operator&(APInt a, const APInt &b) { |
| 2043 | a &= b; |
| 2044 | return a; |
| 2045 | } |
| 2046 | |
| 2047 | inline APInt operator&(const APInt &a, APInt &&b) { |
| 2048 | b &= a; |
| 2049 | return std::move(b); |
| 2050 | } |
| 2051 | |
| 2052 | inline APInt operator&(APInt a, uint64_t RHS) { |
| 2053 | a &= RHS; |
| 2054 | return a; |
| 2055 | } |
| 2056 | |
| 2057 | inline APInt operator&(uint64_t LHS, APInt b) { |
| 2058 | b &= LHS; |
| 2059 | return b; |
| 2060 | } |
| 2061 | |
| 2062 | inline APInt operator|(APInt a, const APInt &b) { |
| 2063 | a |= b; |
| 2064 | return a; |
| 2065 | } |
| 2066 | |
| 2067 | inline APInt operator|(const APInt &a, APInt &&b) { |
| 2068 | b |= a; |
| 2069 | return std::move(b); |
| 2070 | } |
| 2071 | |
| 2072 | inline APInt operator|(APInt a, uint64_t RHS) { |
| 2073 | a |= RHS; |
| 2074 | return a; |
| 2075 | } |
| 2076 | |
| 2077 | inline APInt operator|(uint64_t LHS, APInt b) { |
| 2078 | b |= LHS; |
| 2079 | return b; |
| 2080 | } |
| 2081 | |
| 2082 | inline APInt operator^(APInt a, const APInt &b) { |
| 2083 | a ^= b; |
| 2084 | return a; |
| 2085 | } |
| 2086 | |
| 2087 | inline APInt operator^(const APInt &a, APInt &&b) { |
| 2088 | b ^= a; |
| 2089 | return std::move(b); |
| 2090 | } |
| 2091 | |
| 2092 | inline APInt operator^(APInt a, uint64_t RHS) { |
| 2093 | a ^= RHS; |
| 2094 | return a; |
| 2095 | } |
| 2096 | |
| 2097 | inline APInt operator^(uint64_t LHS, APInt b) { |
| 2098 | b ^= LHS; |
| 2099 | return b; |
| 2100 | } |
| 2101 | |
| 2102 | inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) { |
| 2103 | I.print(OS, true); |
| 2104 | return OS; |
| 2105 | } |
| 2106 | |
| 2107 | inline APInt operator-(APInt v) { |
| 2108 | v.negate(); |
| 2109 | return v; |
| 2110 | } |
| 2111 | |
| 2112 | inline APInt operator+(APInt a, const APInt &b) { |
| 2113 | a += b; |
| 2114 | return a; |
| 2115 | } |
| 2116 | |
| 2117 | inline APInt operator+(const APInt &a, APInt &&b) { |
| 2118 | b += a; |
| 2119 | return std::move(b); |
| 2120 | } |
| 2121 | |
| 2122 | inline APInt operator+(APInt a, uint64_t RHS) { |
| 2123 | a += RHS; |
| 2124 | return a; |
| 2125 | } |
| 2126 | |
| 2127 | inline APInt operator+(uint64_t LHS, APInt b) { |
| 2128 | b += LHS; |
| 2129 | return b; |
| 2130 | } |
| 2131 | |
| 2132 | inline APInt operator-(APInt a, const APInt &b) { |
| 2133 | a -= b; |
| 2134 | return a; |
| 2135 | } |
| 2136 | |
| 2137 | inline APInt operator-(const APInt &a, APInt &&b) { |
| 2138 | b.negate(); |
| 2139 | b += a; |
| 2140 | return std::move(b); |
| 2141 | } |
| 2142 | |
| 2143 | inline APInt operator-(APInt a, uint64_t RHS) { |
| 2144 | a -= RHS; |
| 2145 | return a; |
| 2146 | } |
| 2147 | |
| 2148 | inline APInt operator-(uint64_t LHS, APInt b) { |
| 2149 | b.negate(); |
| 2150 | b += LHS; |
| 2151 | return b; |
| 2152 | } |
| 2153 | |
| 2154 | inline APInt operator*(APInt a, uint64_t RHS) { |
| 2155 | a *= RHS; |
| 2156 | return a; |
| 2157 | } |
| 2158 | |
| 2159 | inline APInt operator*(uint64_t LHS, APInt b) { |
| 2160 | b *= LHS; |
| 2161 | return b; |
| 2162 | } |
| 2163 | |
| 2164 | |
| 2165 | namespace APIntOps { |
| 2166 | |
| 2167 | |
| 2168 | inline const APInt &smin(const APInt &A, const APInt &B) { |
| 2169 | return A.slt(B) ? A : B; |
| 2170 | } |
| 2171 | |
| 2172 | |
| 2173 | inline const APInt &smax(const APInt &A, const APInt &B) { |
| 2174 | return A.sgt(B) ? A : B; |
| 2175 | } |
| 2176 | |
| 2177 | |
| 2178 | inline const APInt &umin(const APInt &A, const APInt &B) { |
| 2179 | return A.ult(B) ? A : B; |
| 2180 | } |
| 2181 | |
| 2182 | |
| 2183 | inline const APInt &umax(const APInt &A, const APInt &B) { |
| 2184 | return A.ugt(B) ? A : B; |
| 2185 | } |
| 2186 | |
| 2187 | |
| 2188 | |
| 2189 | |
| 2190 | |
| 2191 | |
| 2192 | |
| 2193 | APInt GreatestCommonDivisor(APInt A, APInt B); |
| 2194 | |
| 2195 | |
| 2196 | |
| 2197 | |
| 2198 | inline double RoundAPIntToDouble(const APInt &APIVal) { |
| 2199 | return APIVal.roundToDouble(); |
| 2200 | } |
| 2201 | |
| 2202 | |
| 2203 | |
| 2204 | |
| 2205 | inline double RoundSignedAPIntToDouble(const APInt &APIVal) { |
| 2206 | return APIVal.signedRoundToDouble(); |
| 2207 | } |
| 2208 | |
| 2209 | |
| 2210 | inline float RoundAPIntToFloat(const APInt &APIVal) { |
| 2211 | return float(RoundAPIntToDouble(APIVal)); |
| 2212 | } |
| 2213 | |
| 2214 | |
| 2215 | |
| 2216 | |
| 2217 | inline float RoundSignedAPIntToFloat(const APInt &APIVal) { |
| 2218 | return float(APIVal.signedRoundToDouble()); |
| 2219 | } |
| 2220 | |
| 2221 | |
| 2222 | |
| 2223 | |
| 2224 | APInt RoundDoubleToAPInt(double Double, unsigned width); |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | |
| 2229 | inline APInt RoundFloatToAPInt(float Float, unsigned width) { |
| 2230 | return RoundDoubleToAPInt(double(Float), width); |
| 2231 | } |
| 2232 | |
| 2233 | |
| 2234 | APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
| 2235 | |
| 2236 | |
| 2237 | APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | |
| 2243 | |
| 2244 | |
| 2245 | |
| 2246 | |
| 2247 | |
| 2248 | |
| 2249 | |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | |
| 2254 | |
| 2255 | |
| 2256 | |
| 2257 | |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | |
| 2262 | |
| 2263 | |
| 2264 | |
| 2265 | |
| 2266 | |
| 2267 | |
| 2268 | |
| 2269 | |
| 2270 | |
| 2271 | |
| 2272 | Optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, |
| 2273 | unsigned RangeWidth); |
| 2274 | |
| 2275 | |
| 2276 | |
| 2277 | Optional<unsigned> GetMostSignificantDifferentBit(const APInt &A, |
| 2278 | const APInt &B); |
| 2279 | |
| 2280 | } |
| 2281 | |
| 2282 | |
| 2283 | |
| 2284 | hash_code hash_value(const APInt &Arg); |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 | void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes); |
| 2289 | |
| 2290 | |
| 2291 | |
| 2292 | void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes); |
| 2293 | |
| 2294 | |
| 2295 | template <> struct DenseMapInfo<APInt> { |
| 2296 | static inline APInt getEmptyKey() { |
| 2297 | APInt V(nullptr, 0); |
| 2298 | V.U.VAL = 0; |
| 2299 | return V; |
| 2300 | } |
| 2301 | |
| 2302 | static inline APInt getTombstoneKey() { |
| 2303 | APInt V(nullptr, 0); |
| 2304 | V.U.VAL = 1; |
| 2305 | return V; |
| 2306 | } |
| 2307 | |
| 2308 | static unsigned getHashValue(const APInt &Key); |
| 2309 | |
| 2310 | static bool isEqual(const APInt &LHS, const APInt &RHS) { |
| 2311 | return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; |
| 2312 | } |
| 2313 | }; |
| 2314 | |
| 2315 | } |
| 2316 | |
| 2317 | #endif |