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
23template <typename T, unsigned BitNum, typename BitVectorTy, bool isSigned>
25
26// This won't be necessary if we can specialize members without specializing
27// the parent template.
28template <typename T, unsigned BitNum, typename BitVectorTy>
29class PackedVectorBase<T, BitNum, BitVectorTy, false> {
30protected:
31 static T getValue(const BitVectorTy &Bits, unsigned Idx) {
32 T val = T();
33 for (unsigned i = 0; i != BitNum; ++i)
34 val = T(val | ((Bits[(Idx * BitNum) + i] ? 1UL : 0UL) << i));
35 return val;
36 }
37
38 static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
39 assert((val >> BitNum) == 0 && "value is too big");
40 for (unsigned i = 0; i != BitNum; ++i)
41 Bits[(Idx * BitNum) + i] = val & (T(1) << i);
42 }
43};
44
45template <typename T, unsigned BitNum, typename BitVectorTy>
46class PackedVectorBase<T, BitNum, BitVectorTy, true> {
47protected:
48 static T getValue(const BitVectorTy &Bits, unsigned Idx) {
49 T val = T();
50 for (unsigned i = 0; i != BitNum-1; ++i)
51 val = T(val | ((Bits[(Idx * BitNum) + i] ? 1UL : 0UL) << i));
52 if (Bits[(Idx * BitNum) + BitNum - 1])
53 val = ~val;
54 return val;
55 }
56
57 static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
58 if (val < 0) {
59 val = ~val;
60 Bits.set((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 }
66};
67
68/// Store a vector of values using a specific number of bits for each
69/// value. Both signed and unsigned types can be used, e.g
70/// @code
71/// PackedVector<signed, 2> vec;
72/// @endcode
73/// will create a vector accepting values -2, -1, 0, 1. Any other value will hit
74/// an assertion.
75template <typename T, unsigned BitNum, typename BitVectorTy = BitVector>
76class PackedVector : public PackedVectorBase<T, BitNum, BitVectorTy,
77 std::numeric_limits<T>::is_signed> {
78 BitVectorTy Bits;
79 // Keep track of the number of elements on our own.
80 // We always maintain Bits.size() == NumElements * BitNum.
81 // Used to avoid an integer division in size().
82 unsigned NumElements = 0;
83 using base = PackedVectorBase<T, BitNum, BitVectorTy,
84 std::numeric_limits<T>::is_signed>;
85
86public:
87 class reference {
88 PackedVector &Vec;
89 const unsigned Idx;
90
91 public:
92 reference() = delete;
93 reference(PackedVector &vec, unsigned idx) : Vec(vec), Idx(idx) {}
94
96 Vec.setValue(Vec.Bits, Idx, val);
97 return *this;
98 }
99
100 operator T() const {
101 return Vec.getValue(Vec.Bits, Idx);
102 }
103 };
104
105 PackedVector() = default;
106 explicit PackedVector(unsigned size)
107 : Bits(size * BitNum), NumElements(size) {}
108
109 bool empty() const { return NumElements == 0; }
110
111 unsigned size() const { return NumElements; }
112
113 void clear() {
114 Bits.clear();
115 NumElements = 0;
116 }
117
118 void resize(unsigned N) {
119 Bits.resize(N * BitNum);
120 NumElements = N;
121 }
122
123 void reserve(unsigned N) { Bits.reserve(N * BitNum); }
124
126 Bits.reset();
127 return *this;
128 }
129
130 void push_back(T val) {
131 resize(size()+1);
132 (*this)[size()-1] = val;
133 }
134
135 reference operator[](unsigned Idx) {
136 return reference(*this, Idx);
137 }
138
139 T operator[](unsigned Idx) const {
140 return base::getValue(Bits, Idx);
141 }
142
143 bool operator==(const PackedVector &RHS) const {
144 return Bits == RHS.Bits;
145 }
146
147 bool operator!=(const PackedVector &RHS) const {
148 return Bits != RHS.Bits;
149 }
150
152 Bits |= RHS.Bits;
153 return *this;
154 }
155
156 const BitVectorTy &raw_bits() const { return Bits; }
157 BitVectorTy &raw_bits() { return Bits; }
158};
159
160// Leave BitNum=0 undefined.
161template <typename T> class PackedVector<T, 0>;
162
163} // end namespace llvm
164
165#endif // LLVM_ADT_PACKEDVECTOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
#define T
Value * RHS
static T getValue(const BitVectorTy &Bits, unsigned Idx)
static void setValue(BitVectorTy &Bits, unsigned Idx, T val)
static T getValue(const BitVectorTy &Bits, unsigned Idx)
static void setValue(BitVectorTy &Bits, unsigned Idx, T val)
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