[llvm] [InstCombine] Create a class to lazily track computed known bits (PR #66611)

Dhruv Chawla via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 20 09:45:03 PDT 2023


================
@@ -0,0 +1,162 @@
+//===- 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_CACHEDBITSVALUE_H
+#define LLVM_ANALYSIS_CACHEDBITSVALUE_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 std::optional<KnownBits> Known;
----------------
dc03 wrote:

That is a great idea! I have implemented this.

https://github.com/llvm/llvm-project/pull/66611


More information about the llvm-commits mailing list