diff --git a/llvm/include/llvm/ADT/Bitfields.h b/llvm/include/llvm/ADT/Bitfields.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ADT/Bitfields.h @@ -0,0 +1,270 @@ +//===-- llvm/ADT/Bitfield.h - Get and Set bits in an integer ---*- 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This file implements methods to test, set and extract typed bits from packed +/// unsigned integers. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ADT_BITFIELDS_H +#define LLVM_ADT_BITFIELDS_H + +#include +#include // CHAR_BIT +#include // size_t +#include // uintXX_t +#include // numeric_limits +#include + +/// Helpers to pack / unpack typed bitfields into an unsigned integer. +/// e.g. +/// +/// uint8_t storage = 0; +/// +/// // Store and retrieve a single bit as bool. +/// using Bool = Bitfield; +/// setField(storage, true); +/// EXPECT_EQ(storage, 0b00000001); +/// // ^ +/// EXPECT_EQ(getField(storage), true); +/// +/// // Store and retrieve a 2 bit typed enum. +/// // Note: enum underlying type must be unsigned. +/// enum class SuitEnum : uint8_t { CLUBS, DIAMONDS, HEARTS, SPADES }; +/// // Note: enum maximum value needs to be passed in. +/// using Suit = Bitfield; +/// setField(storage, SuitEnum::HEARTS); +/// EXPECT_EQ(storage, 0b00000101); +/// // ^^ +/// EXPECT_EQ(getField(storage), SuitEnum::HEARTS); +/// +/// // Store and retrieve a 5 bit value as unsigned. +/// using Value = Bitfield; +/// setField(storage, 10); +/// EXPECT_EQ(storage, 0b01010101); +/// // ^^^^^ +/// EXPECT_EQ(getField(storage), 10U); +/// +/// // Interpret the same 5 bit value as signed. +/// using SignedValue = Bitfield; +/// setField(storage, -2); +/// EXPECT_EQ(storage, 0b11110101); +/// // ^^^^^ +/// EXPECT_EQ(getField(storage), -2); +/// +/// // Ability to efficiently test if a field is non zero. +/// EXPECT_TRUE(testField(storage) != 0U); +/// +/// // Alter storage changes value. +/// storage = 0; +/// EXPECT_EQ(getField(storage), false); +/// EXPECT_EQ(getField(storage), SuitEnum::CLUBS); +/// EXPECT_EQ(getField(storage), 0U); +/// EXPECT_EQ(getField(storage), 0); +/// +/// storage = 255; +/// EXPECT_EQ(getField(storage), true); +/// EXPECT_EQ(getField(storage), SuitEnum::SPADES); +/// EXPECT_EQ(getField(storage), 31U); +/// EXPECT_EQ(getField(storage), -1); + +namespace llvm { + +namespace details { + +/// A struct defining useful bit patterns for n-bits integer types. +template struct BitPatterns { + // Bit patterns are forged using the equivalent unsigned type U because of + // undefined operations over signed types (e.g. Bitwise shift operators). + // Moreover same size casting from unsigned to signed is well defined but the + // opposite is not. + using U = typename std::make_unsigned::type; + static_assert(sizeof(U) == sizeof(T), "Types must have same size"); + + static constexpr unsigned type_bits = sizeof(U) * CHAR_BIT; + static_assert(type_bits >= bits, "n-bit must fit in T"); + + // e.g. with type_bits == 8 and bits == 6. + static constexpr U all_zeros = U(0); // 0b00000000 + static constexpr U all_ones = ~U(0); // 0b11111111 + static constexpr U umin = all_zeros; // 0b00000000 + static constexpr U umax = all_ones >> (type_bits - bits); // 0b00111111 + static constexpr U sign_bit_mask = U(1) << (bits - 1U); // 0b00100000 + static constexpr U smax = umax >> 1U; // 0b00011111 + static constexpr U smin = ~smax; // 0b11100000 + static constexpr U sign_extend = smin << 1U; // 0b11000000 +}; + +/// `Compressor` is used to manipulate the bits of a (possibly signed) integer +/// type so it can be packed and unpacked into a `bits` sized integer, +/// `Compressor` is specialized on signed-ness so no runtime cost is incurred. +/// The `pack` method also checks that the passed in `user_value` is valid. +template ::value> +struct Compressor { + static_assert(std::is_unsigned::value, "type is unsigned"); + using BP = BitPatterns; + + static type pack(type user_value, type user_max_value) { + assert(user_value <= user_max_value && "value is too big"); + assert(user_value <= BP::umax && "value is too big"); + return user_value; + } + + static type unpack(type storage_value) { return storage_value; } +}; + +template struct Compressor { + static_assert(std::is_signed::value, "type is signed"); + using BP = BitPatterns; + + static type pack(type user_value, type user_max_value) { + assert(user_value <= user_max_value && "value is too big"); + assert(user_value <= type(BP::smax) && "value is too big"); + assert(user_value >= type(BP::smin) && "value is too small"); + if (user_value < 0) + user_value &= ~BP::sign_extend; + return user_value; + } + + static type unpack(type storage_value) { + if (storage_value >= type(BP::sign_bit_mask)) + storage_value |= BP::sign_extend; + return storage_value; + } +}; + +/// `bitfield_properties` stores important properties about the Bitfield. +/// It only deals with integer types (conversion from/to enum/bool are done +/// outside of this template). +template ::max(), + T MinValue = std::numeric_limits::min()> +struct bitfield_properties { + using integer_type = T; + static_assert(std::is_integral::value && + std::numeric_limits::is_integer, + "T must be an integer type"); + + static constexpr size_t type_bits = sizeof(integer_type) * CHAR_BIT; + static constexpr unsigned offset = Offset; + static constexpr unsigned bits = Bits; + static constexpr unsigned first_bit = offset; + static constexpr unsigned last_bit = offset + bits; + static constexpr integer_type min_value = MinValue; + static constexpr integer_type max_value = MaxValue; + + static_assert(bits > 0, "Bits must be non zero"); + static_assert(bits <= type_bits, "Bits may not be greater than T size"); + + /// Impl is where Bifield description and storage are put together to interact + /// with values. + template struct Impl { + static_assert(std::is_unsigned::value, + "Storage must be unsigned"); + using C = Compressor; + using BP = BitPatterns; + + static constexpr size_t storage_bits = sizeof(storage_type) * CHAR_BIT; + static_assert(first_bit <= storage_bits, "Data must fit in mask"); + static_assert(last_bit <= storage_bits, "Data must fit in mask"); + static constexpr storage_type mask = BP::umax << offset; + + /// Checks `user_value` is within bounds and packs it between `first_bit` + /// and `last_bit` of `packed_value` leaving the rest unchanged. + static void Update(storage_type &packed, integer_type user_value) { + const storage_type storage_value = C::pack(user_value, max_value); + packed &= ~mask; + packed |= storage_value << offset; + } + + /// Interprets bits between `first_bit` and `last_bit` of `packed` as an + /// `integer_value`. + static integer_type Extract(storage_type packed) { + const storage_type storage_value = (packed & mask) >> offset; + return C::unpack(storage_value); + } + }; +}; + +/// `Bitfield` deals with unsigned enums, signed/unsigned integer and `bool` +/// internally though we only manipulate integer with well defined and +/// consistent semantic, this excludes typed enums and `bool` that are +/// replaced with their unsigned counterparts. +/// The correct type is restored in the public API. +template ::value> +struct resolve_underlying_type { + using type = typename std::underlying_type::type; +}; +template struct resolve_underlying_type { + using type = T; +}; +template <> struct resolve_underlying_type { + /// In case sizeof(bool) != 1, replace `void` by an additionnal + /// std::conditionnal. + using type = std::conditional::type; +}; + +} // namespace details + +/// A struct to hold the bitfield informations. +/// \param Type, the type of the field once in unpacked form, +/// \param Offset, the position of the first bit, +/// \param Size, the size of the field, +/// \param MaxValue, For enums the maximum enum allowed. +template ::value + ? Type(0) // coupled with static_assert below + : std::numeric_limits::max()> +struct Bitfield { + using type = Type; + using integer_type = typename details::resolve_underlying_type::type; + using properties = typename details::bitfield_properties< + integer_type, Offset, Size, static_cast(MaxValue)>; + + static constexpr bool is_enum = std::is_enum::value; + static_assert(!is_enum || MaxValue != Type(0), + "Enum Bitfields must provide a MaxValue"); + static_assert(!is_enum || std::is_unsigned::value, + "Enum must be unsigned"); +}; + +/// Returns whether the two bitfields share common bits. +template static constexpr bool isOverlapping() { + using PA = typename A::properties; + using PB = typename B::properties; + return PA::last_bit > PB::first_bit && PB::last_bit > PA::first_bit; +} + +/// Unpacks the field from the packed value. +template +static typename Bitfield::type getField(StorageType packed) { + using Impl = typename Bitfield::properties::template Impl; + return static_cast(Impl::Extract(packed)); +} + +/// Return a non-zero value if the field is non-zero. +/// It is more efficient than `getField`. +template +static StorageType testField(StorageType packed) { + using Impl = typename Bitfield::properties::template Impl; + return packed & Impl::mask; +} + +/// Sets the typed value in the provided packed value. +/// The method will asserts if the provided value is too big to fit in the +/// packed field. +template +static void setField(StorageType &packed, typename Bitfield::type value) { + using Impl = typename Bitfield::properties::template Impl; + Impl::Update(packed, static_cast(value)); +} + +} // namespace llvm + +#endif // LLVM_ADT_BITFIELDS_H \ No newline at end of file diff --git a/llvm/include/llvm/IR/InstrTypes.h b/llvm/include/llvm/IR/InstrTypes.h --- a/llvm/include/llvm/IR/InstrTypes.h +++ b/llvm/include/llvm/IR/InstrTypes.h @@ -757,6 +757,8 @@ LAST_ICMP_PREDICATE = ICMP_SLE, BAD_ICMP_PREDICATE = ICMP_SLE + 1 }; + using PredicateField = + Bitfield; // Next bit:6 protected: CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, @@ -797,12 +799,10 @@ } /// Return the predicate for this instruction. - Predicate getPredicate() const { - return Predicate(getSubclassDataFromInstruction()); - } + Predicate getPredicate() const { return getSubclassData(); } /// Set the predicate for this instruction to the specified value. - void setPredicate(Predicate P) { setInstructionSubclassData(P); } + void setPredicate(Predicate P) { setSubclassData(P); } static bool isFPPredicate(Predicate P) { return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE; @@ -1096,6 +1096,10 @@ /// subclass requires. Note that accessing the end of the argument list isn't /// as cheap as most other operations on the base class. class CallBase : public Instruction { + // The first two bits are reserved by CallInst for fast retrieving, + using CallInstReservedField = Bitfield; // Next bit:2 + using CallingConvField = Bitfield; // Next bit:12 + protected: /// The last operand is the called operand. static constexpr int CalledOperandOpEndIdx = -1; @@ -1349,14 +1353,13 @@ } CallingConv::ID getCallingConv() const { - return static_cast(getSubclassDataFromInstruction() >> 2); + return getSubclassData(); } void setCallingConv(CallingConv::ID CC) { - auto ID = static_cast(CC); - assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention"); - setInstructionSubclassData((getSubclassDataFromInstruction() & 3) | - (ID << 2)); + assert(!(unsigned(CC) & ~CallingConv::MaxID) && + "Unsupported calling convention"); + setSubclassData(CC); } /// Check if this call is an inline asm statement. diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -15,6 +15,7 @@ #define LLVM_IR_INSTRUCTION_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Bitfields.h" #include "llvm/ADT/None.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/ilist_node.h" @@ -49,11 +50,14 @@ /// O(1) local dominance checks between instructions. mutable unsigned Order = 0; - enum { - /// This is a bit stored in the SubClassData field which indicates whether - /// this instruction has metadata attached to it or not. - HasMetadataBit = 1 << 15 - }; +protected: + // The 15 first bits of `Value::SubclassData` are available for subclasses of + // `Instruction` to use. + using OpaqueField = Bitfield; // Next bit:15 +private: + // The last bit is used to store whether the instruction has metadata attached + // or not. + using HasMetadataField = Bitfield; protected: ~Instruction(); // Use deleteValue() to delete a generic Instruction. @@ -471,7 +475,7 @@ private: /// Return true if we have an entry in the on-the-side metadata hash. bool hasMetadataHashEntry() const { - return (getSubclassDataFromValue() & HasMetadataBit) != 0; + return getSubclassData(); } // These are all implemented in Metadata.cpp. @@ -763,10 +767,7 @@ return Value::getSubclassDataFromValue(); } - void setHasMetadataHashEntry(bool V) { - setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) | - (V ? HasMetadataBit : 0)); - } + void setHasMetadataHashEntry(bool V) { setSubclassData(V); } void setParent(BasicBlock *P); @@ -774,14 +775,21 @@ // Instruction subclasses can stick up to 15 bits of stuff into the // SubclassData field of instruction with these members. - // Verify that only the low 15 bits are used. - void setInstructionSubclassData(unsigned short D) { - assert((D & HasMetadataBit) == 0 && "Out of range value put into field"); - setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D); - } - - unsigned getSubclassDataFromInstruction() const { - return getSubclassDataFromValue() & ~HasMetadataBit; + template typename Bitfield::type getSubclassData() const { + static_assert(std::is_same::value || + !isOverlapping(), + "Must not overlap with the metadata bit"); + return getField(getSubclassDataFromValue()); + } + + template + void setSubclassData(typename Bitfield::type value) { + static_assert(std::is_same::value || + !isOverlapping(), + "Must not overlap with the metadata bit"); + auto storage = getSubclassDataFromValue(); + setField(storage, value); + setValueSubclassData(storage); } Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -16,6 +16,7 @@ #define LLVM_IR_INSTRUCTIONS_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Bitfields.h" #include "llvm/ADT/None.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" @@ -59,6 +60,10 @@ class AllocaInst : public UnaryInstruction { Type *AllocatedType; + using AlignmentField = Bitfield; // Next bit:5 + using UsedWithInAllocaField = Bitfield; // Next bit:6 + using SwiftErrorField = Bitfield; // Next bit:7 + protected: // Note: Instruction needs to be a friend here to call cloneImpl. friend class Instruction; @@ -108,7 +113,7 @@ /// Return the alignment of the memory that is being allocated by the /// instruction. Align getAlign() const { - return *decodeMaybeAlign(getSubclassDataFromInstruction() & 31); + return *decodeMaybeAlign(getSubclassData()); } // FIXME: Remove this one transition to Align is over. unsigned getAlignment() const { return getAlign().value(); } @@ -122,25 +127,18 @@ /// Return true if this alloca is used as an inalloca argument to a call. Such /// allocas are never considered static even if they are in the entry block. bool isUsedWithInAlloca() const { - return getSubclassDataFromInstruction() & 32; + return getSubclassData(); } /// Specify whether this alloca is used to represent the arguments to a call. void setUsedWithInAlloca(bool V) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) | - (V ? 32 : 0)); + setSubclassData(V); } /// Return true if this alloca is used as a swifterror argument to a call. - bool isSwiftError() const { - return getSubclassDataFromInstruction() & 64; - } - + bool isSwiftError() const { return getSubclassData(); } /// Specify whether this alloca is used to represent a swifterror. - void setSwiftError(bool V) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) | - (V ? 64 : 0)); - } + void setSwiftError(bool V) { setSubclassData(V); } // Methods for support type inquiry through isa, cast, and dyn_cast: static bool classof(const Instruction *I) { @@ -153,8 +151,9 @@ private: // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } }; @@ -165,6 +164,11 @@ /// An instruction for reading from memory. This uses the SubclassData field in /// Value to store whether or not the load is volatile. class LoadInst : public UnaryInstruction { + using VolatileField = Bitfield; // Next bit:1 + using AlignmentField = Bitfield; // Next bit:7 + using OrderingField = Bitfield; // Next bit:10 + void AssertOK(); protected: @@ -194,13 +198,10 @@ BasicBlock *InsertAtEnd); /// Return true if this is a load from a volatile memory location. - bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } + bool isVolatile() const { return getSubclassData(); } /// Specify whether this is a volatile load or not. - void setVolatile(bool V) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | - (V ? 1 : 0)); - } + void setVolatile(bool V) { setSubclassData(V); } /// Return the alignment of the access that is being performed. /// FIXME: Remove this function once transition to Align is over. @@ -209,21 +210,19 @@ /// Return the alignment of the access that is being performed. Align getAlign() const { - return *decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31); + return *decodeMaybeAlign(getSubclassData()); } void setAlignment(Align Alignment); /// Returns the ordering constraint of this load instruction. AtomicOrdering getOrdering() const { - return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); + return getSubclassData(); } - /// Sets the ordering constraint of this load instruction. May not be Release /// or AcquireRelease. void setOrdering(AtomicOrdering Ordering) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | - ((unsigned)Ordering << 7)); + setSubclassData(Ordering); } /// Returns the synchronization scope ID of this load instruction. @@ -273,8 +272,9 @@ private: // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } /// The synchronization scope ID of this load instruction. Not quite enough @@ -289,6 +289,11 @@ /// An instruction for storing to memory. class StoreInst : public Instruction { + using VolatileField = Bitfield; // Next bit:1 + using AlignmentField = Bitfield; // Next bit:7 + using OrderingField = Bitfield; // Next bit:10 + void AssertOK(); protected: @@ -318,13 +323,10 @@ } /// Return true if this is a store to a volatile memory location. - bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } + bool isVolatile() const { return getSubclassData(); } /// Specify whether this is a volatile store or not. - void setVolatile(bool V) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | - (V ? 1 : 0)); - } + void setVolatile(bool V) { setSubclassData(V); } /// Transparently provide more efficient getOperand methods. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); @@ -335,21 +337,20 @@ unsigned getAlignment() const { return getAlign().value(); } Align getAlign() const { - return *decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31); + return *decodeMaybeAlign(getSubclassData()); } void setAlignment(Align Alignment); /// Returns the ordering constraint of this store instruction. AtomicOrdering getOrdering() const { - return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); + return getSubclassData(); } /// Sets the ordering constraint of this store instruction. May not be /// Acquire or AcquireRelease. void setOrdering(AtomicOrdering Ordering) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | - ((unsigned)Ordering << 7)); + setSubclassData(Ordering); } /// Returns the synchronization scope ID of this store instruction. @@ -402,8 +403,9 @@ private: // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } /// The synchronization scope ID of this store instruction. Not quite enough @@ -424,6 +426,9 @@ /// An instruction for ordering other memory operations. class FenceInst : public Instruction { + using OrderingField = Bitfield; // Next bit:4 + void Init(AtomicOrdering Ordering, SyncScope::ID SSID); protected: @@ -448,14 +453,13 @@ /// Returns the ordering constraint of this fence instruction. AtomicOrdering getOrdering() const { - return AtomicOrdering(getSubclassDataFromInstruction() >> 1); + return getSubclassData(); } /// Sets the ordering constraint of this fence instruction. May only be /// Acquire, Release, AcquireRelease, or SequentiallyConsistent. void setOrdering(AtomicOrdering Ordering) { - setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | - ((unsigned)Ordering << 1)); + setSubclassData(Ordering); } /// Returns the synchronization scope ID of this fence instruction. @@ -479,8 +483,9 @@ private: // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } /// The synchronization scope ID of this fence instruction. Not quite enough @@ -525,57 +530,53 @@ return User::operator new(s, 3); } + // FIXME: Reuse bit 1 that was used by `syncscope.` + using VolatileField = Bitfield; // Next bit:1 + using SuccessOrderingField = Bitfield; // Next bit:5 + using FailureOrderingField = Bitfield; // Next bit:8 + using WeakField = Bitfield; // Next bit:9 + /// Return true if this is a cmpxchg from a volatile memory /// location. /// - bool isVolatile() const { - return getSubclassDataFromInstruction() & 1; - } + bool isVolatile() const { return getSubclassData(); } /// Specify whether this is a volatile cmpxchg. /// - void setVolatile(bool V) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | - (unsigned)V); - } + void setVolatile(bool V) { setSubclassData(V); } /// Return true if this cmpxchg may spuriously fail. - bool isWeak() const { - return getSubclassDataFromInstruction() & 0x100; - } + bool isWeak() const { return getSubclassData(); } - void setWeak(bool IsWeak) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) | - (IsWeak << 8)); - } + void setWeak(bool IsWeak) { setSubclassData(IsWeak); } /// Transparently provide more efficient getOperand methods. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); /// Returns the success ordering constraint of this cmpxchg instruction. AtomicOrdering getSuccessOrdering() const { - return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); + return getSubclassData(); } /// Sets the success ordering constraint of this cmpxchg instruction. void setSuccessOrdering(AtomicOrdering Ordering) { assert(Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic."); - setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) | - ((unsigned)Ordering << 2)); + setSubclassData(Ordering); } /// Returns the failure ordering constraint of this cmpxchg instruction. AtomicOrdering getFailureOrdering() const { - return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7); + return getSubclassData(); } /// Sets the failure ordering constraint of this cmpxchg instruction. void setFailureOrdering(AtomicOrdering Ordering) { assert(Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic."); - setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) | - ((unsigned)Ordering << 5)); + setSubclassData(Ordering); } /// Returns the synchronization scope ID of this cmpxchg instruction. @@ -637,8 +638,9 @@ private: // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } /// The synchronization scope ID of this cmpxchg instruction. Not quite @@ -721,9 +723,14 @@ return User::operator new(s, 2); } - BinOp getOperation() const { - return static_cast(getSubclassDataFromInstruction() >> 5); - } + // FIXME: Reuse bit 1 that was used by `syncscope.` + using VolatileField = Bitfield; // Next bit:1 + using AtomicOrderingField = Bitfield; // Next bit:5 + using OperationField = Bitfield; // Next bit:9 + + BinOp getOperation() const { return getSubclassData(); } static StringRef getOperationName(BinOp Op); @@ -738,38 +745,30 @@ } void setOperation(BinOp Operation) { - unsigned short SubclassData = getSubclassDataFromInstruction(); - setInstructionSubclassData((SubclassData & 31) | - (Operation << 5)); + setSubclassData(Operation); } /// Return true if this is a RMW on a volatile memory location. /// - bool isVolatile() const { - return getSubclassDataFromInstruction() & 1; - } + bool isVolatile() const { return getSubclassData(); } /// Specify whether this is a volatile RMW or not. /// - void setVolatile(bool V) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | - (unsigned)V); - } + void setVolatile(bool V) { setSubclassData(V); } /// Transparently provide more efficient getOperand methods. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); /// Returns the ordering constraint of this rmw instruction. AtomicOrdering getOrdering() const { - return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); + return getSubclassData(); } /// Sets the ordering constraint of this rmw instruction. void setOrdering(AtomicOrdering Ordering) { assert(Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic."); - setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) | - ((unsigned)Ordering << 2)); + setSubclassData(Ordering); } /// Returns the synchronization scope ID of this rmw instruction. @@ -812,8 +811,9 @@ // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } /// The synchronization scope ID of this rmw instruction. Not quite enough @@ -1546,33 +1546,31 @@ TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2, - TCK_NoTail = 3 + TCK_NoTail = 3, + TCK_LAST = TCK_NoTail }; + + using TailCallKindField = Bitfield; + TailCallKind getTailCallKind() const { - return TailCallKind(getSubclassDataFromInstruction() & 3); + return getSubclassData(); } bool isTailCall() const { - unsigned Kind = getSubclassDataFromInstruction() & 3; + TailCallKind Kind = getTailCallKind(); return Kind == TCK_Tail || Kind == TCK_MustTail; } - bool isMustTailCall() const { - return (getSubclassDataFromInstruction() & 3) == TCK_MustTail; - } + bool isMustTailCall() const { return getTailCallKind() == TCK_MustTail; } - bool isNoTailCall() const { - return (getSubclassDataFromInstruction() & 3) == TCK_NoTail; - } + bool isNoTailCall() const { return getTailCallKind() == TCK_NoTail; } - void setTailCall(bool isTC = true) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | - unsigned(isTC ? TCK_Tail : TCK_None)); + void setTailCallKind(TailCallKind TCK) { + setSubclassData(TCK); } - void setTailCallKind(TailCallKind TCK) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | - unsigned(TCK)); + void setTailCall(bool isTC = true) { + setTailCallKind(isTC ? TCK_Tail : TCK_None); } /// Return true if the call can return twice @@ -1595,8 +1593,9 @@ private: // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } }; @@ -2715,6 +2714,8 @@ /// cleanup. /// class LandingPadInst : public Instruction { + using CleanupField = Bitfield; + /// The number of operands actually allocated. NumOperands is /// the number actually in use. unsigned ReservedSpace; @@ -2759,13 +2760,10 @@ /// Return 'true' if this landingpad instruction is a /// cleanup. I.e., it should be run when unwinding even if its landing pad /// doesn't catch the exception. - bool isCleanup() const { return getSubclassDataFromInstruction() & 1; } + bool isCleanup() const { return getSubclassData(); } /// Indicate that this landingpad instruction is a cleanup. - void setCleanup(bool V) { - setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | - (V ? 1 : 0)); - } + void setCleanup(bool V) { setSubclassData(V); } /// Add a catch or filter clause to the landing pad. void addClause(Constant *ClauseVal); @@ -3752,11 +3750,11 @@ } private: - // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } }; @@ -3992,11 +3990,11 @@ } private: - // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } }; @@ -4087,6 +4085,8 @@ // CatchSwitchInst Class //===----------------------------------------------------------------------===// class CatchSwitchInst : public Instruction { + using UnwindDestField = Bitfield; // Next bit:1 + /// The number of operands actually allocated. NumOperands is /// the number actually in use. unsigned ReservedSpace; @@ -4148,7 +4148,7 @@ void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); } // Accessor Methods for CatchSwitch stmt - bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } + bool hasUnwindDest() const { return getSubclassData(); } bool unwindsToCaller() const { return !hasUnwindDest(); } BasicBlock *getUnwindDest() const { if (hasUnwindDest()) @@ -4434,6 +4434,7 @@ //===----------------------------------------------------------------------===// class CleanupReturnInst : public Instruction { + using UnwindDestField = Bitfield; // Next bit:1 private: CleanupReturnInst(const CleanupReturnInst &RI); CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, @@ -4474,7 +4475,7 @@ /// Provide fast operand accessors DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); - bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } + bool hasUnwindDest() const { return getSubclassData(); } bool unwindsToCaller() const { return !hasUnwindDest(); } /// Convenience accessor. @@ -4518,8 +4519,9 @@ // Shadow Instruction::setInstructionSubclassData with a private forwarding // method so that subclasses cannot accidentally use it. - void setInstructionSubclassData(unsigned short D) { - Instruction::setInstructionSubclassData(D); + template + void setSubclassData(typename Bitfield::type value) { + Instruction::setSubclassData(value); } }; diff --git a/llvm/include/llvm/Support/AtomicOrdering.h b/llvm/include/llvm/Support/AtomicOrdering.h --- a/llvm/include/llvm/Support/AtomicOrdering.h +++ b/llvm/include/llvm/Support/AtomicOrdering.h @@ -53,7 +53,7 @@ /// /// not_atomic-->unordered-->relaxed-->release--------------->acq_rel-->seq_cst /// \-->consume-->acquire--/ -enum class AtomicOrdering { +enum class AtomicOrdering : unsigned { NotAtomic = 0, Unordered = 1, Monotonic = 2, // Equivalent to C++'s relaxed. @@ -61,7 +61,8 @@ Acquire = 4, Release = 5, AcquireRelease = 6, - SequentiallyConsistent = 7 + SequentiallyConsistent = 7, + LAST = SequentiallyConsistent }; bool operator<(AtomicOrdering, AtomicOrdering) = delete; diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -960,7 +960,8 @@ OperandTraits::op_end(this) - CRI.getNumOperands(), CRI.getNumOperands()) { - setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); + setSubclassData( + CRI.getSubclassData()); Op<0>() = CRI.Op<0>(); if (CRI.hasUnwindDest()) Op<1>() = CRI.Op<1>(); @@ -968,7 +969,7 @@ void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { if (UnwindBB) - setInstructionSubclassData(getSubclassDataFromInstruction() | 1); + setSubclassData(true); Op<0>() = CleanupPad; if (UnwindBB) @@ -1072,7 +1073,7 @@ Op<0>() = ParentPad; if (UnwindDest) { - setInstructionSubclassData(getSubclassDataFromInstruction() | 1); + setSubclassData(true); setUnwindDest(UnwindDest); } } @@ -1299,9 +1300,7 @@ void AllocaInst::setAlignment(Align Align) { assert(Align <= MaximumAlignment && "Alignment is greater than MaximumAlignment!"); - setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) | - encode(Align)); - assert(getAlignment() == Align.value() && "Alignment representation error!"); + setSubclassData(encode(Align)); } bool AllocaInst::isArrayAllocation() const { @@ -1397,9 +1396,7 @@ void LoadInst::setAlignment(Align Align) { assert(Align <= MaximumAlignment && "Alignment is greater than MaximumAlignment!"); - setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | - (encode(Align) << 1)); - assert(getAlign() == Align && "Alignment representation error!"); + setSubclassData(encode(Align)); } //===----------------------------------------------------------------------===// @@ -1476,9 +1473,7 @@ void StoreInst::setAlignment(Align Alignment) { assert(Alignment <= MaximumAlignment && "Alignment is greater than MaximumAlignment!"); - setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | - (encode(Alignment) << 1)); - assert(getAlign() == Alignment && "Alignment representation error!"); + setSubclassData(encode(Alignment)); } //===----------------------------------------------------------------------===// diff --git a/llvm/unittests/ADT/BitFieldsTest.cpp b/llvm/unittests/ADT/BitFieldsTest.cpp new file mode 100644 --- /dev/null +++ b/llvm/unittests/ADT/BitFieldsTest.cpp @@ -0,0 +1,244 @@ +//===- llvm/unittests/ADT/BitFieldsTest.cpp - BitFields unit tests --------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/Bitfields.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(BitfieldsTest, Example) { + uint8_t storage = 0; + + // Store and retrieve a single bit as bool. + using Bool = Bitfield; + setField(storage, true); + EXPECT_EQ(storage, 0b00000001); + // ^ + EXPECT_EQ(getField(storage), true); + + // Store and retrieve a 2 bit typed enum. + // Note: enum underlying type must be unsigned. + enum class SuitEnum : uint8_t { CLUBS, DIAMONDS, HEARTS, SPADES }; + // Note: enum maximum value needs to be passed in. + using Suit = Bitfield; + setField(storage, SuitEnum::HEARTS); + EXPECT_EQ(storage, 0b00000101); + // ^^ + EXPECT_EQ(getField(storage), SuitEnum::HEARTS); + + // Store and retrieve a 5 bit value as unsigned. + using Value = Bitfield; + setField(storage, 10); + EXPECT_EQ(storage, 0b01010101); + // ^^^^^ + EXPECT_EQ(getField(storage), 10U); + + // Interpret the same 5 bit value as signed. + using SignedValue = Bitfield; + setField(storage, -2); + EXPECT_EQ(storage, 0b11110101); + // ^^^^^ + EXPECT_EQ(getField(storage), -2); + + // Ability to efficiently test if a field is non zero. + EXPECT_TRUE(testField(storage) != 0U); + + // Alter storage changes value. + storage = 0; + EXPECT_EQ(getField(storage), false); + EXPECT_EQ(getField(storage), SuitEnum::CLUBS); + EXPECT_EQ(getField(storage), 0U); + EXPECT_EQ(getField(storage), 0); + + storage = 255; + EXPECT_EQ(getField(storage), true); + EXPECT_EQ(getField(storage), SuitEnum::SPADES); + EXPECT_EQ(getField(storage), 31U); + EXPECT_EQ(getField(storage), -1); +} + +TEST(BitfieldsTest, FirstBit) { + uint8_t storage = 0; + using FirstBit = Bitfield; + // Set true + setField(storage, true); + EXPECT_EQ(getField(storage), true); + EXPECT_EQ(storage, 0x1ULL); + // Set false + setField(storage, false); + EXPECT_EQ(getField(storage), false); + EXPECT_EQ(storage, 0x0ULL); +} + +TEST(BitfieldsTest, SecondBit) { + uint8_t storage = 0; + using SecondBit = Bitfield; + // Set true + setField(storage, true); + EXPECT_EQ(getField(storage), true); + EXPECT_EQ(storage, 0x2ULL); + // Set false + setField(storage, false); + EXPECT_EQ(getField(storage), false); + EXPECT_EQ(storage, 0x0ULL); +} + +TEST(BitfieldsTest, LastBit) { + uint8_t storage = 0; + using LastBit = Bitfield; + // Set true + setField(storage, true); + EXPECT_EQ(getField(storage), true); + EXPECT_EQ(storage, 0x80ULL); + // Set false + setField(storage, false); + EXPECT_EQ(getField(storage), false); + EXPECT_EQ(storage, 0x0ULL); +} + +TEST(BitfieldsTest, LastBitUint64) { + uint64_t storage = 0; + using LastBit = Bitfield; + // Set true + setField(storage, true); + EXPECT_EQ(getField(storage), true); + EXPECT_EQ(storage, 0x8000000000000000ULL); + // Set false + setField(storage, false); + EXPECT_EQ(getField(storage), false); + EXPECT_EQ(storage, 0x0ULL); +} + +TEST(BitfieldsTest, Enum) { + enum Enum { Zero = 0, Two = 2, LAST = Two }; + + uint8_t storage = 0; + using OrderingField = Bitfield; + EXPECT_EQ(getField(storage), Zero); + setField(storage, Two); + EXPECT_EQ(getField(storage), Two); + EXPECT_EQ(storage, 0b00000100); + // value 2 in ^^ +} + +TEST(BitfieldsTest, EnumClass) { + enum class Enum : unsigned { Zero = 0, Two = 2, LAST = Two }; + + uint8_t storage = 0; + using OrderingField = Bitfield; + EXPECT_EQ(getField(storage), Enum::Zero); + setField(storage, Enum::Two); + EXPECT_EQ(getField(storage), Enum::Two); + EXPECT_EQ(storage, 0b00000100); + // value 2 in ^^ +} + +TEST(BitfieldsTest, OneBitSigned) { + uint8_t storage = 0; + using SignedField = Bitfield; + EXPECT_EQ(getField(storage), 0); + EXPECT_EQ(storage, 0b00000000); + // value 0 in ^ + setField(storage, -1); + EXPECT_EQ(getField(storage), -1); + EXPECT_EQ(storage, 0b00000010); + // value 1 in ^ +} + +TEST(BitfieldsTest, TwoBitSigned) { + uint8_t storage = 0; + using SignedField = Bitfield; + EXPECT_EQ(getField(storage), 0); + EXPECT_EQ(storage, 0b00000000); + // value 0 in ^^ + setField(storage, 1); + EXPECT_EQ(getField(storage), 1); + EXPECT_EQ(storage, 0b00000010); + // value 1 in ^^ + setField(storage, -1); + EXPECT_EQ(getField(storage), -1); + EXPECT_EQ(storage, 0b00000110); + // value -1 in ^^ + setField(storage, -2); + EXPECT_EQ(getField(storage), -2); + EXPECT_EQ(storage, 0b00000100); + // value -2 in ^^ +} + +TEST(BitfieldsTest, isOverlapping) { + // 01234567 + // A: -------- + // B: --- + // C: --- + // D: --- + using A = Bitfield; + using B = Bitfield; + using C = Bitfield; + using D = Bitfield; + EXPECT_TRUE((isOverlapping())); + EXPECT_TRUE((isOverlapping())); + EXPECT_TRUE((isOverlapping())); + EXPECT_TRUE((isOverlapping())); + + EXPECT_TRUE((isOverlapping())); + EXPECT_TRUE((isOverlapping())); + EXPECT_FALSE((isOverlapping())); +} + +TEST(BitfieldsTest, FullUint64) { + uint64_t storage = 0; + using Value = Bitfield; + setField(storage, -1ULL); + EXPECT_EQ(getField(storage), -1ULL); + setField(storage, 0ULL); + EXPECT_EQ(getField(storage), 0ULL); +} + +TEST(BitfieldsTest, FullInt64) { + uint64_t storage = 0; + using Value = Bitfield; + setField(storage, -1); + EXPECT_EQ(getField(storage), -1); + setField(storage, 0); + EXPECT_EQ(getField(storage), 0); +} + +#ifdef EXPECT_DEBUG_DEATH + +TEST(BitfieldsTest, ValueTooBigBool) { + uint64_t storage = 0; + using A = Bitfield; + setField(storage, true); + setField(storage, false); + EXPECT_DEBUG_DEATH(setField(storage, 2), "value is too big"); +} + +TEST(BitfieldsTest, ValueTooBigInt) { + uint64_t storage = 0; + using A = Bitfield; + setField(storage, 3); + EXPECT_DEBUG_DEATH(setField(storage, 4), "value is too big"); + EXPECT_DEBUG_DEATH(setField(storage, -1), "value is too big"); +} + +TEST(BitfieldsTest, ValueTooBigBounded) { + uint8_t storage = 0; + using A = Bitfield; + setField(storage, 1); + setField(storage, 0); + setField(storage, -1); + setField(storage, -2); + EXPECT_DEBUG_DEATH(setField(storage, 2), "value is too big"); + EXPECT_DEBUG_DEATH(setField(storage, -3), "value is too small"); +} + +#endif + +} // namespace \ No newline at end of file diff --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt --- a/llvm/unittests/ADT/CMakeLists.txt +++ b/llvm/unittests/ADT/CMakeLists.txt @@ -8,6 +8,7 @@ APIntTest.cpp APSIntTest.cpp ArrayRefTest.cpp + BitFieldsTest.cpp BitmaskEnumTest.cpp BitVectorTest.cpp BreadthFirstIteratorTest.cpp