LLVM 20.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"
20#include <type_traits>
21
22namespace llvm {
23struct SimplifyQuery;
24KnownBits computeKnownBits(const Value *V, unsigned Depth,
25 const SimplifyQuery &Q);
26
27template <typename Arg> class WithCache {
28 static_assert(std::is_pointer_v<Arg>, "WithCache requires a pointer type!");
29
30 using UnderlyingType = std::remove_pointer_t<Arg>;
31 constexpr static bool IsConst = std::is_const_v<Arg>;
32
33 template <typename T, bool Const>
34 using conditionally_const_t = std::conditional_t<Const, const T, T>;
35
36 using PointerType = conditionally_const_t<UnderlyingType *, IsConst>;
37 using ReferenceType = conditionally_const_t<UnderlyingType &, IsConst>;
38
39 // Store the presence of the KnownBits information in one of the bits of
40 // Pointer.
41 // true -> present
42 // false -> absent
44 mutable KnownBits Known;
45
46 void calculateKnownBits(const SimplifyQuery &Q) const {
47 Known = computeKnownBits(Pointer.getPointer(), 0, Q);
48 Pointer.setInt(true);
49 }
50
51public:
52 WithCache(PointerType Pointer) : Pointer(Pointer, false) {}
53 WithCache(PointerType Pointer, const KnownBits &Known)
54 : Pointer(Pointer, true), Known(Known) {}
55
56 [[nodiscard]] PointerType getValue() const { return Pointer.getPointer(); }
57
58 [[nodiscard]] const KnownBits &getKnownBits(const SimplifyQuery &Q) const {
59 if (!hasKnownBits())
60 calculateKnownBits(Q);
61 return Known;
62 }
63
64 [[nodiscard]] bool hasKnownBits() const { return Pointer.getInt(); }
65
66 operator PointerType() const { return Pointer.getPointer(); }
67 PointerType operator->() const { return Pointer.getPointer(); }
68 ReferenceType operator*() const { return *Pointer.getPointer(); }
69};
70} // namespace llvm
71
72#endif
basic Basic Alias true
This file defines the PointerIntPair class.
PointerIntPair - This class implements a pair of a pointer and small integer.
PointerType operator->() const
Definition: WithCache.h:67
bool hasKnownBits() const
Definition: WithCache.h:64
WithCache(PointerType Pointer)
Definition: WithCache.h:52
WithCache(PointerType Pointer, const KnownBits &Known)
Definition: WithCache.h:53
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition: WithCache.h:58
ReferenceType operator*() const
Definition: WithCache.h:68
PointerType getValue() const
Definition: WithCache.h:56
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...