Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:20

0001 //===- llvm/Analysis/WithCache.h - KnownBits cache for pointers -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Store a pointer to any type along with the KnownBits information for it
0010 // that is computed lazily (if required).
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_ANALYSIS_WITHCACHE_H
0015 #define LLVM_ANALYSIS_WITHCACHE_H
0016 
0017 #include "llvm/ADT/PointerIntPair.h"
0018 #include "llvm/IR/Value.h"
0019 #include "llvm/Support/KnownBits.h"
0020 #include <type_traits>
0021 
0022 namespace llvm {
0023 struct SimplifyQuery;
0024 KnownBits computeKnownBits(const Value *V, unsigned Depth,
0025                            const SimplifyQuery &Q);
0026 
0027 template <typename Arg> class WithCache {
0028   static_assert(std::is_pointer_v<Arg>, "WithCache requires a pointer type!");
0029 
0030   using UnderlyingType = std::remove_pointer_t<Arg>;
0031   constexpr static bool IsConst = std::is_const_v<Arg>;
0032 
0033   template <typename T, bool Const>
0034   using conditionally_const_t = std::conditional_t<Const, const T, T>;
0035 
0036   using PointerType = conditionally_const_t<UnderlyingType *, IsConst>;
0037   using ReferenceType = conditionally_const_t<UnderlyingType &, IsConst>;
0038 
0039   // Store the presence of the KnownBits information in one of the bits of
0040   // Pointer.
0041   // true  -> present
0042   // false -> absent
0043   mutable PointerIntPair<PointerType, 1, bool> Pointer;
0044   mutable KnownBits Known;
0045 
0046   void calculateKnownBits(const SimplifyQuery &Q) const {
0047     Known = computeKnownBits(Pointer.getPointer(), 0, Q);
0048     Pointer.setInt(true);
0049   }
0050 
0051 public:
0052   WithCache(PointerType Pointer) : Pointer(Pointer, false) {}
0053   WithCache(PointerType Pointer, const KnownBits &Known)
0054       : Pointer(Pointer, true), Known(Known) {}
0055 
0056   [[nodiscard]] PointerType getValue() const { return Pointer.getPointer(); }
0057 
0058   [[nodiscard]] const KnownBits &getKnownBits(const SimplifyQuery &Q) const {
0059     if (!hasKnownBits())
0060       calculateKnownBits(Q);
0061     return Known;
0062   }
0063 
0064   [[nodiscard]] bool hasKnownBits() const { return Pointer.getInt(); }
0065 
0066   operator PointerType() const { return Pointer.getPointer(); }
0067   PointerType operator->() const { return Pointer.getPointer(); }
0068   ReferenceType operator*() const { return *Pointer.getPointer(); }
0069 };
0070 } // namespace llvm
0071 
0072 #endif