diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/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 +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; + /// Helper to adjust the statistics. static void bookkeeping(AbstractAttribute::ManifestPosition MP, const Attribute &Attr) {