[llvm] [InstCombine] Create a class to lazily track computed known bits (PR #66611)
Dhruv Chawla via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 18 06:25:42 PDT 2023
================
@@ -0,0 +1,188 @@
+//===- llvm/Analysis/CachedBitsValue.h - Value with KnownBits - -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Store a pointer to an llvm::Value along with the KnownBits information for it
+// that is computed lazily (if required).
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_VALUEWITHINFOCACHE_H
+#define LLVM_ANALYSIS_VALUEWITHINFOCACHE_H
+
+#include "llvm/IR/Value.h"
+#include "llvm/Support/KnownBits.h"
+#include <type_traits>
+
+namespace llvm {
+
+class DataLayout;
+class AssumptionCache;
+class Instruction;
+class DominatorTree;
+struct SimplifyQuery;
+
+KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
+ unsigned Depth, const SimplifyQuery &Q);
+
+KnownBits computeKnownBits(const Value *V, unsigned Depth,
+ const SimplifyQuery &Q);
+
+namespace detail {
+/// Represents a pointer to an llvm::Value with known bits information
+template <bool ConstPointer = true> class ImplCachedBitsValue {
+protected:
+ using ValuePointerType =
+ std::conditional_t<ConstPointer, const Value *, Value *>;
+ using ValueReferenceType =
+ std::conditional_t<ConstPointer, const Value &, Value &>;
+
+ template <typename T>
+ constexpr static bool ValuePointerConvertible =
+ std::is_convertible_v<T, ValuePointerType>;
+
+ ValuePointerType Pointer;
+ mutable KnownBits Known;
+ mutable bool HasKnownBits;
+
+ void calculateKnownBits(unsigned Depth, const SimplifyQuery &Q) const {
+ HasKnownBits = true;
+ Known = computeKnownBits(Pointer, Depth, Q);
+ }
+
+ void calculateKnownBits(const APInt &DemandedElts, unsigned Depth,
+ const SimplifyQuery &Q) const {
+ HasKnownBits = true;
+ Known = computeKnownBits(Pointer, DemandedElts, Depth, Q);
+ }
+
+public:
+ ImplCachedBitsValue() = default;
+ ImplCachedBitsValue(ValuePointerType Pointer)
+ : Pointer(Pointer), Known(), HasKnownBits(false) {}
+ ImplCachedBitsValue(ValuePointerType Pointer, const KnownBits &Known)
+ : Pointer(Pointer), Known(Known), HasKnownBits(true) {}
+
+ template <typename T, std::enable_if_t<ValuePointerConvertible<T>, int> = 0>
+ ImplCachedBitsValue(const T &Value)
+ : Pointer(static_cast<ValuePointerType>(Value)), Known(),
+ HasKnownBits(false) {}
+
+ template <typename T, std::enable_if_t<ValuePointerConvertible<T>, int> = 0>
+ ImplCachedBitsValue(const T &Value, const KnownBits &Known)
+ : Pointer(static_cast<ValuePointerType>(Value)), Known(Known),
+ HasKnownBits(true) {}
+
+ ImplCachedBitsValue(ImplCachedBitsValue &&) = default;
+ ImplCachedBitsValue &operator=(ImplCachedBitsValue &&) = default;
+ ImplCachedBitsValue(const ImplCachedBitsValue &) = default;
+ ImplCachedBitsValue &operator=(const ImplCachedBitsValue &) = default;
+
+ ~ImplCachedBitsValue() = default;
+
+ [[nodiscard]] ValuePointerType getValue() { return Pointer; }
+ [[nodiscard]] ValuePointerType getValue() const { return Pointer; }
+
+ [[nodiscard]] const KnownBits &getKnownBits(unsigned Depth,
----------------
dc03 wrote:
Hmm, not sure on this - it would make the code simpler, but would also make it less flexible (as compared to a more complex caching scheme). It depends on how much potential there actually is to use this class elsewhere in the code, which I think you are in a better position to answer than me.
https://github.com/llvm/llvm-project/pull/66611
More information about the llvm-commits
mailing list