[PATCH] D60012: [Attributor] Introduce bit-encodings for abstract states

Johannes Doerfert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 29 14:33:14 PDT 2019


jdoerfert created this revision.
Herald added subscribers: bollu, hiraditya.
Herald added a project: LLVM.

The IntegerState, and its sepecialization BooleanState, can be used to
simplify the implementation of abstract attributes. The two abstract
state implementations provide storage and helpers to deal with bit-wise
encoded state.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D60012

Files:
  llvm/lib/Transforms/IPO/Attributor.cpp


Index: llvm/lib/Transforms/IPO/Attributor.cpp
===================================================================
--- llvm/lib/Transforms/IPO/Attributor.cpp
+++ llvm/lib/Transforms/IPO/Attributor.cpp
@@ -60,6 +60,75 @@
 }
 ///}
 
+/// Simple state with integers encoding. The worst/known and best/assumed state
+/// encodings, as well as the underlying integer type, are provided as template
+/// parameters.
+template <typename BaseTy, BaseTy InitKnown, BaseTy InitAssumed>
+struct IntegerState : public AbstractState {
+  IntegerState() : Known(InitKnown), Assumed(InitAssumed) {}
+
+  /// See AbstractState::isValidState()
+  /// NOTE: For now we simply pretend that the worst possible state is invalid.
+  bool isValidState() const override { return Assumed != InitKnown; }
+
+  /// See AbstractState::isAtFixpoint()
+  /// NOTE: This requires the bits in Known to be set in Assumed at all times!
+  bool isAtFixpoint() const override { return Assumed == Known; }
+
+  /// See AbstractState::indicateFixpoint(...)
+  void indicateFixpoint(bool Optimistic) override {
+    if (Optimistic)
+      Known = Assumed;
+    else
+      Assumed = Known;
+  }
+
+  /// Return the known (integer) state.
+  BaseTy getKnown() const { return Known; }
+
+  /// Return the assumed (integer) state.
+  BaseTy getAssumed() const { return Assumed; }
+
+protected:
+  /// Copy the bits set in \p BitsEncoding from \p From to \p To.
+  static void copyBits(BaseTy &To, const BaseTy From, BaseTy BitsEncoding) {
+    BaseTy UnaffectedBits = To & (~BitsEncoding);
+    BaseTy AffectedBits = From & BitsEncoding;
+    To = UnaffectedBits | AffectedBits;
+  }
+
+  /// Set the bits of \p Target also set in \p BitsEncoding.
+  static void setBits(BaseTy &Target, BaseTy BitsEncoding) {
+    Target = Target | BitsEncoding;
+  }
+
+  /// Unset the bits of \p Target also set in \p BitsEncoding.
+  static BaseTy &unsetBits(BaseTy &Target, BaseTy BitsEncoding) {
+    Target = Target & ~BitsEncoding;
+    return Target;
+  }
+
+  /// Intersect the bits of \p Target also set in \p BitsEncoding.
+  static BaseTy &intersectBits(BaseTy &Target, BaseTy BitsEncoding) {
+    Target = Target & BitsEncoding;
+    return Target;
+  }
+
+  /// Return true if the bits set in \p BitsEncoding are set in \p Source.
+  static bool testBits(BaseTy Source, BaseTy BitsEncoding) {
+    return (Source & BitsEncoding) == BitsEncoding;
+  }
+
+  /// The known state encoding in an integer of type BaseTy.
+  BaseTy Known;
+
+  /// The assumed state encoding in an integer of type BaseTy.
+  BaseTy Assumed;
+};
+
+/// Specialization of the integer state for booleans.
+using BooleanState = IntegerState<bool, 0, 1>;
+
 /// Helper to adjust the statistics.
 static void bookkeeping(AbstractAttribute::ManifestPosition MP,
                         const Attribute &Attr) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60012.192910.patch
Type: text/x-patch
Size: 2836 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190329/88767691/attachment.bin>


More information about the llvm-commits mailing list