LLVM 22.0.0git
WithCache.h
Go to the documentation of this file.
1//===- llvm/Analysis/WithCache.h - KnownBits cache for pointers -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Store a pointer to any type along with the KnownBits information for it
10// that is computed lazily (if required).
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_WITHCACHE_H
15#define LLVM_ANALYSIS_WITHCACHE_H
16
18#include "llvm/IR/Value.h"
21#include <type_traits>
22
23namespace llvm {
24struct SimplifyQuery;
25LLVM_ABI KnownBits computeKnownBits(const Value *V, const SimplifyQuery &Q,
26 unsigned Depth);
27
28template <typename Arg> class WithCache {
29 static_assert(std::is_pointer_v<Arg>, "WithCache requires a pointer type!");
30
31 using UnderlyingType = std::remove_pointer_t<Arg>;
32 constexpr static bool IsConst = std::is_const_v<Arg>;
33
34 template <typename T, bool Const>
35 using conditionally_const_t = std::conditional_t<Const, const T, T>;
36
37 using PointerType = conditionally_const_t<UnderlyingType *, IsConst>;
38 using ReferenceType = conditionally_const_t<UnderlyingType &, IsConst>;
39
40 // Store the presence of the KnownBits information in one of the bits of
41 // Pointer.
42 // true -> present
43 // false -> absent
45 mutable KnownBits Known;
46
47 void calculateKnownBits(const SimplifyQuery &Q) const {
48 Known = computeKnownBits(Pointer.getPointer(), Q, 0);
49 Pointer.setInt(true);
50 }
51
52public:
53 WithCache(PointerType Pointer) : Pointer(Pointer, false) {}
54 WithCache(PointerType Pointer, const KnownBits &Known)
55 : Pointer(Pointer, true), Known(Known) {}
56
57 [[nodiscard]] PointerType getValue() const { return Pointer.getPointer(); }
58
59 [[nodiscard]] const KnownBits &getKnownBits(const SimplifyQuery &Q) const {
60 if (!hasKnownBits())
61 calculateKnownBits(Q);
62 return Known;
63 }
64
65 [[nodiscard]] bool hasKnownBits() const { return Pointer.getInt(); }
66
67 operator PointerType() const { return Pointer.getPointer(); }
68 PointerType operator->() const { return Pointer.getPointer(); }
69 ReferenceType operator*() const { return *Pointer.getPointer(); }
70};
71} // namespace llvm
72
73#endif
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the PointerIntPair class.
PointerIntPair - This class implements a pair of a pointer and small integer.
PointerType operator->() const
Definition: WithCache.h:68
bool hasKnownBits() const
Definition: WithCache.h:65
WithCache(PointerType Pointer)
Definition: WithCache.h:53
WithCache(PointerType Pointer, const KnownBits &Known)
Definition: WithCache.h:54
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition: WithCache.h:59
ReferenceType operator*() const
Definition: WithCache.h:69
PointerType getValue() const
Definition: WithCache.h:57
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...