LLVM 22.0.0git
PackedVector.h
Go to the documentation of this file.
1//===- llvm/ADT/PackedVector.h - Packed values vector -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the PackedVector class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_PACKEDVECTOR_H
15#define LLVM_ADT_PACKEDVECTOR_H
16
17#include "llvm/ADT/BitVector.h"
18#include <cassert>
19#include <limits>
20
21namespace llvm {
22
23/// Store a vector of values using a specific number of bits for each
24/// value. Both signed and unsigned types can be used, e.g
25/// @code
26/// PackedVector<signed, 2> vec;
27/// @endcode
28/// will create a vector accepting values -2, -1, 0, 1. Any other value will hit
29/// an assertion.
30template <typename T, unsigned BitNum, typename BitVectorTy = BitVector>
32 BitVectorTy Bits;
33 // Keep track of the number of elements on our own.
34 // We always maintain Bits.size() == NumElements * BitNum.
35 // Used to avoid an integer division in size().
36 unsigned NumElements = 0;
37
38 static T getValue(const BitVectorTy &Bits, unsigned Idx) {
39 if constexpr (std::numeric_limits<T>::is_signed) {
40 T val = T();
41 for (unsigned i = 0; i != BitNum - 1; ++i)
42 val = T(val | ((Bits[(Idx * BitNum) + i] ? 1UL : 0UL) << i));
43 if (Bits[(Idx * BitNum) + BitNum - 1])
44 val = ~val;
45 return val;
46 } else {
47 T val = T();
48 for (unsigned i = 0; i != BitNum; ++i)
49 val = T(val | ((Bits[(Idx * BitNum) + i] ? 1UL : 0UL) << i));
50 return val;
51 }
52 }
53
54 static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
55 if constexpr (std::numeric_limits<T>::is_signed) {
56 if (val < 0) {
57 val = ~val;
58 Bits.set((Idx * BitNum) + BitNum - 1);
59 } else {
60 Bits.reset((Idx * BitNum) + BitNum - 1);
61 }
62 assert((val >> (BitNum - 1)) == 0 && "value is too big");
63 for (unsigned i = 0; i != BitNum - 1; ++i)
64 Bits[(Idx * BitNum) + i] = val & (T(1) << i);
65 } else {
66 assert((val >> BitNum) == 0 && "value is too big");
67 for (unsigned i = 0; i != BitNum; ++i)
68 Bits[(Idx * BitNum) + i] = val & (T(1) << i);
69 }
70 }
71
72public:
73 class reference {
74 PackedVector &Vec;
75 const unsigned Idx;
76
77 public:
78 reference() = delete;
79 reference(PackedVector &vec, unsigned idx) : Vec(vec), Idx(idx) {}
80
82 Vec.setValue(Vec.Bits, Idx, val);
83 return *this;
84 }
85
86 operator T() const { return Vec.getValue(Vec.Bits, Idx); }
87 };
88
89 PackedVector() = default;
90 explicit PackedVector(unsigned size)
91 : Bits(size * BitNum), NumElements(size) {}
92
93 bool empty() const { return NumElements == 0; }
94
95 unsigned size() const { return NumElements; }
96
97 void clear() {
98 Bits.clear();
99 NumElements = 0;
100 }
101
102 void resize(unsigned N) {
103 Bits.resize(N * BitNum);
104 NumElements = N;
105 }
106
107 void reserve(unsigned N) { Bits.reserve(N * BitNum); }
108
110 Bits.reset();
111 return *this;
112 }
113
114 void push_back(T val) {
115 resize(size() + 1);
116 (*this)[size() - 1] = val;
117 }
118
119 reference operator[](unsigned Idx) { return reference(*this, Idx); }
120
121 T operator[](unsigned Idx) const { return getValue(Bits, Idx); }
122
123 bool operator==(const PackedVector &RHS) const { return Bits == RHS.Bits; }
124
125 bool operator!=(const PackedVector &RHS) const { return Bits != RHS.Bits; }
126
128 Bits |= RHS.Bits;
129 return *this;
130 }
131
132 const BitVectorTy &raw_bits() const { return Bits; }
133 BitVectorTy &raw_bits() { return Bits; }
134};
135
136// Leave BitNum=0 undefined.
137template <typename T> class PackedVector<T, 0>;
138
139} // end namespace llvm
140
141#endif // LLVM_ADT_PACKEDVECTOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
#define T
Value * RHS
reference(PackedVector &vec, unsigned idx)
reference & operator=(T val)
void resize(unsigned N)
const BitVectorTy & raw_bits() const
PackedVector & reset()
bool empty() const
PackedVector()=default
T operator[](unsigned Idx) const
PackedVector(unsigned size)
void push_back(T val)
BitVectorTy & raw_bits()
PackedVector & operator|=(const PackedVector &RHS)
bool operator!=(const PackedVector &RHS) const
void reserve(unsigned N)
bool operator==(const PackedVector &RHS) const
reference operator[](unsigned Idx)
This is an optimization pass for GlobalISel generic memory operations.
#define N