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,268 @@ +//===-- 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 because of + // undefined operations over signed types (e.g. Bitwise shift operators). + // Moreover same size casting from unsigned to signed is well defined but not + // the other way around. + using Unsigned = typename std::make_unsigned::type; + static_assert(sizeof(Unsigned) == sizeof(T), "Types must have same size"); + + static constexpr unsigned TypeBits = sizeof(Unsigned) * CHAR_BIT; + static_assert(TypeBits >= Bits, "n-bit must fit in T"); + + // e.g. with TypeBits == 8 and Bits == 6. + static constexpr Unsigned AllZeros = Unsigned(0); // 00000000 + static constexpr Unsigned AllOnes = ~Unsigned(0); // 11111111 + static constexpr Unsigned Umin = AllZeros; // 00000000 + static constexpr Unsigned Umax = AllOnes >> (TypeBits - Bits); // 00111111 + static constexpr Unsigned SignBitMask = Unsigned(1) << (Bits - 1); // 00100000 + static constexpr Unsigned Smax = Umax >> 1U; // 00011111 + static constexpr Unsigned Smin = ~Smax; // 11100000 + static constexpr Unsigned SignExtend = Smin << 1U; // 11000000 +}; + +/// `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 `UserValue` is valid. +template ::value> +struct Compressor { + static_assert(std::is_unsigned::value, "T is unsigned"); + using BP = BitPatterns; + + static T pack(T UserValue, T UserMaxValue) { + assert(UserValue <= UserMaxValue && "value is too big"); + assert(UserValue <= BP::Umax && "value is too big"); + return UserValue; + } + + static T unpack(T StorageValue) { return StorageValue; } +}; + +template struct Compressor { + static_assert(std::is_signed::value, "T is signed"); + using BP = BitPatterns; + + static T pack(T UserValue, T UserMaxValue) { + assert(UserValue <= UserMaxValue && "value is too big"); + assert(UserValue <= T(BP::Smax) && "value is too big"); + assert(UserValue >= T(BP::Smin) && "value is too small"); + if (UserValue < 0) + UserValue &= ~BP::SignExtend; + return UserValue; + } + + static T unpack(T StorageValue) { + if (StorageValue >= T(BP::SignBitMask)) + StorageValue |= BP::SignExtend; + return StorageValue; + } +}; + +/// `BitfieldProperties` stores important properties about the Bitfield. +/// It only deals with proper integer types. The conversion from/to enum/bool +/// are done in the public API outside of this template. +template ::max(), + T MinValue = std::numeric_limits::min()> +struct BitfieldProperties { + using IntegerType = T; + static_assert(std::is_integral::value && + std::numeric_limits::is_integer, + "T must be an integer type"); + + static constexpr size_t TypeBits = sizeof(IntegerType) * CHAR_BIT; + static constexpr unsigned FirstBit = Offset; + static constexpr unsigned LastBit = Offset + Bits; + + static_assert(Bits > 0, "Bits must be non zero"); + static_assert(Bits <= TypeBits, "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 StorageBits = sizeof(StorageType) * CHAR_BIT; + static_assert(FirstBit <= StorageBits, "Data must fit in mask"); + static_assert(LastBit <= StorageBits, "Data must fit in mask"); + static constexpr StorageType Mask = BP::Umax << Offset; + + /// Checks `UserValue` is within bounds and packs it between `FirstBit` + /// and `LastBit` of `Packed` leaving the rest unchanged. + static void update(StorageType &Packed, IntegerType UserValue) { + const StorageType StorageValue = C::pack(UserValue, MaxValue); + Packed &= ~Mask; + Packed |= StorageValue << Offset; + } + + /// Interprets bits between `FirstBit` and `LastBit` of `Packed` as an + /// `IntegerType`. + static IntegerType extract(StorageType Packed) { + const StorageType StorageValue = (Packed & Mask) >> Offset; + return C::unpack(StorageValue); + } + }; +}; + +/// `Bitfield` deals with the following type: +/// - unsigned enums +/// - signed and unsigned integer +/// - `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 ResolveUnderlyingType { + using type = typename std::underlying_type::type; +}; +template struct ResolveUnderlyingType { + using type = T; +}; +template <> struct ResolveUnderlyingType { + /// 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 T, 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 + ? T(0) // coupled with static_assert below + : std::numeric_limits::max()> +struct Bitfield { + using Type = T; + using IntegerType = typename details::ResolveUnderlyingType::type; + using Properties = + typename details::BitfieldProperties(MaxValue)>; + + static_assert(!std::is_enum::value || MaxValue != T(0), + "Enum Bitfields must provide a MaxValue"); + static_assert(!std::is_enum::value || 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::LastBit > PB::FirstBit && PB::LastBit > PA::FirstBit; +} + +/// 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. +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 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 @@ -722,41 +722,43 @@ /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of /// FCMP_* values. Changing the bit patterns requires a potential change to /// those passes. - enum Predicate { + enum Predicate : unsigned { // Opcode U L G E Intuitive operation - FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded) - FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal - FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than - FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal - FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than - FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal - FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal - FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans) - FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y) - FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal - FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than - FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal - FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than - FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal - FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal - FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded) + FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded) + FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal + FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than + FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal + FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than + FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal + FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal + FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans) + FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y) + FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal + FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than + FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal + FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than + FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal + FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal + FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded) FIRST_FCMP_PREDICATE = FCMP_FALSE, LAST_FCMP_PREDICATE = FCMP_TRUE, BAD_FCMP_PREDICATE = FCMP_TRUE + 1, - ICMP_EQ = 32, ///< equal - ICMP_NE = 33, ///< not equal - ICMP_UGT = 34, ///< unsigned greater than - ICMP_UGE = 35, ///< unsigned greater or equal - ICMP_ULT = 36, ///< unsigned less than - ICMP_ULE = 37, ///< unsigned less or equal - ICMP_SGT = 38, ///< signed greater than - ICMP_SGE = 39, ///< signed greater or equal - ICMP_SLT = 40, ///< signed less than - ICMP_SLE = 41, ///< signed less or equal + ICMP_EQ = 32, ///< equal + ICMP_NE = 33, ///< not equal + ICMP_UGT = 34, ///< unsigned greater than + ICMP_UGE = 35, ///< unsigned greater or equal + ICMP_ULT = 36, ///< unsigned less than + ICMP_ULE = 37, ///< unsigned less or equal + ICMP_SGT = 38, ///< signed greater than + ICMP_SGE = 39, ///< signed greater or equal + ICMP_SLT = 40, ///< signed less than + ICMP_SLE = 41, ///< signed less or equal FIRST_ICMP_PREDICATE = ICMP_EQ, 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,11 @@ /// 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 +1354,11 @@ } 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)); + 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 @@ -674,7 +676,7 @@ /// the descriptions, 'p' is the pointer to the instruction's memory location, /// 'old' is the initial value of *p, and 'v' is the other value passed to the /// instruction. These instructions always return 'old'. - enum BinOp { + enum BinOp : unsigned { /// *p = v Xchg, /// *p = old + v @@ -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 @@ -1542,37 +1542,35 @@ BasicBlock *InsertAtEnd); // Note that 'musttail' implies 'tail'. - enum TailCallKind { + enum TailCallKind : unsigned { 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/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp --- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp +++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp @@ -4552,7 +4552,7 @@ SDNode *N, SelectionDAG &DAG) { EVT VT = N->getValueType(0); const auto *CD = cast(N->getOperand(3)); - int CondCode = CD->getSExtValue(); + unsigned CondCode = CD->getZExtValue(); if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE || CondCode > ICmpInst::Predicate::LAST_ICMP_PREDICATE) return DAG.getUNDEF(VT); @@ -4589,7 +4589,7 @@ EVT VT = N->getValueType(0); const auto *CD = cast(N->getOperand(3)); - int CondCode = CD->getSExtValue(); + unsigned CondCode = CD->getZExtValue(); if (CondCode < FCmpInst::Predicate::FIRST_FCMP_PREDICATE || CondCode > FCmpInst::Predicate::LAST_FCMP_PREDICATE) { return DAG.getUNDEF(VT); diff --git a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp @@ -278,28 +278,28 @@ TsanAtomicStore[i] = M.getOrInsertFunction( AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy); - for (int op = AtomicRMWInst::FIRST_BINOP; - op <= AtomicRMWInst::LAST_BINOP; ++op) { - TsanAtomicRMW[op][i] = nullptr; + for (unsigned Op = AtomicRMWInst::FIRST_BINOP; + Op <= AtomicRMWInst::LAST_BINOP; ++Op) { + TsanAtomicRMW[Op][i] = nullptr; const char *NamePart = nullptr; - if (op == AtomicRMWInst::Xchg) + if (Op == AtomicRMWInst::Xchg) NamePart = "_exchange"; - else if (op == AtomicRMWInst::Add) + else if (Op == AtomicRMWInst::Add) NamePart = "_fetch_add"; - else if (op == AtomicRMWInst::Sub) + else if (Op == AtomicRMWInst::Sub) NamePart = "_fetch_sub"; - else if (op == AtomicRMWInst::And) + else if (Op == AtomicRMWInst::And) NamePart = "_fetch_and"; - else if (op == AtomicRMWInst::Or) + else if (Op == AtomicRMWInst::Or) NamePart = "_fetch_or"; - else if (op == AtomicRMWInst::Xor) + else if (Op == AtomicRMWInst::Xor) NamePart = "_fetch_xor"; - else if (op == AtomicRMWInst::Nand) + else if (Op == AtomicRMWInst::Nand) NamePart = "_fetch_nand"; else continue; SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart); - TsanAtomicRMW[op][i] = + TsanAtomicRMW[Op][i] = M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy); } 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 : unsigned { 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 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