[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 Oct 16 05:40:13 PDT 2023


================
@@ -0,0 +1,97 @@
+//===- llvm/Analysis/WithCache.h - KnownBits cache for pointers -*- 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 any type along with the KnownBits information for it
+// that is computed lazily (if required).
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_WITHCACHE_H
+#define LLVM_ANALYSIS_WITHCACHE_H
+
+#include "llvm/IR/Value.h"
+#include "llvm/Support/KnownBits.h"
+#include <type_traits>
+
+namespace llvm {
+struct SimplifyQuery;
+}
+
+llvm::KnownBits computeKnownBits(const llvm::Value *V, unsigned Depth,
+                                 const llvm::SimplifyQuery &Q);
+
+namespace llvm {
+template <typename Arg> class WithCache {
+  static_assert(std::is_pointer_v<Arg>, "WithCache requires a pointer type!");
+
+  using UnderlyingType = std::remove_pointer_t<Arg>;
+  constexpr static bool IsConst = std::is_const_v<Arg>;
+
+  template <typename T, bool Const>
+  using conditionally_const_t = std::conditional_t<Const, const T, T>;
+
+  using PointerType = conditionally_const_t<UnderlyingType *, IsConst>;
+  using ReferenceType = conditionally_const_t<UnderlyingType &, IsConst>;
+
+  template <typename T>
+  constexpr static bool PointerConvertible =
+      std::is_convertible_v<T, UnderlyingType>;
+
+  // Store the presence of the KnownBits information in one of the bits of
+  // Pointer.
+  // true  -> present
+  // false -> absent
+  mutable PointerIntPair<PointerType, 1, bool> Pointer;
+  mutable KnownBits Known;
+
+  void calculateKnownBits(const SimplifyQuery &Q) const {
+    Known = computeKnownBits(Pointer.getPointer(), 0, Q);
+    Pointer.setInt(true);
+  }
+
+public:
+  WithCache() = default;
+  WithCache(PointerType Pointer) : Pointer(Pointer, false) {}
+  WithCache(PointerType Pointer, const KnownBits &Known)
+      : Pointer(Pointer, true), Known(Known) {}
+
+  template <typename T, std::enable_if_t<PointerConvertible<T>, int> = 0>
+  WithCache(const T &Value) : Pointer(static_cast<PointerType>(Value), false) {}
----------------
dc03 wrote:

These are artifacts from `CachedBitsValue`, they aren't even ever called :facepalm: 

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


More information about the llvm-commits mailing list