LLVM  16.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 
21 namespace llvm {
22 
23 template <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.
28 template <typename T, unsigned BitNum, typename BitVectorTy>
29 class PackedVectorBase<T, BitNum, BitVectorTy, false> {
30 protected:
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-1)) + 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-1)) + i] = val & (T(1) << i);
42  }
43 };
44 
45 template <typename T, unsigned BitNum, typename BitVectorTy>
46 class PackedVectorBase<T, BitNum, BitVectorTy, true> {
47 protected:
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-1)) + i] ? 1UL : 0UL) << i));
52  if (Bits[(Idx << (BitNum-1)) + 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-1)) + 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-1)) + 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.
75 template <typename T, unsigned BitNum, typename BitVectorTy = BitVector>
76 class PackedVector : public PackedVectorBase<T, BitNum, BitVectorTy,
77  std::numeric_limits<T>::is_signed> {
78  BitVectorTy Bits;
79  using base = PackedVectorBase<T, BitNum, BitVectorTy,
80  std::numeric_limits<T>::is_signed>;
81 
82 public:
83  class reference {
84  PackedVector &Vec;
85  const unsigned Idx;
86 
87  public:
88  reference() = delete;
89  reference(PackedVector &vec, unsigned idx) : Vec(vec), Idx(idx) {}
90 
92  Vec.setValue(Vec.Bits, Idx, val);
93  return *this;
94  }
95 
96  operator T() const {
97  return Vec.getValue(Vec.Bits, Idx);
98  }
99  };
100 
101  PackedVector() = default;
102  explicit PackedVector(unsigned size) : Bits(size << (BitNum-1)) {}
103 
104  bool empty() const { return Bits.empty(); }
105 
106  unsigned size() const { return Bits.size() >> (BitNum - 1); }
107 
108  void clear() { Bits.clear(); }
109 
110  void resize(unsigned N) { Bits.resize(N << (BitNum - 1)); }
111 
112  void reserve(unsigned N) { Bits.reserve(N << (BitNum-1)); }
113 
115  Bits.reset();
116  return *this;
117  }
118 
119  void push_back(T val) {
120  resize(size()+1);
121  (*this)[size()-1] = val;
122  }
123 
124  reference operator[](unsigned Idx) {
125  return reference(*this, Idx);
126  }
127 
128  T operator[](unsigned Idx) const {
129  return base::getValue(Bits, Idx);
130  }
131 
132  bool operator==(const PackedVector &RHS) const {
133  return Bits == RHS.Bits;
134  }
135 
136  bool operator!=(const PackedVector &RHS) const {
137  return Bits != RHS.Bits;
138  }
139 
141  Bits |= RHS.Bits;
142  return *this;
143  }
144 };
145 
146 // Leave BitNum=0 undefined.
147 template <typename T> class PackedVector<T, 0>;
148 
149 } // end namespace llvm
150 
151 #endif // LLVM_ADT_PACKEDVECTOR_H
i
i
Definition: README.txt:29
llvm::PackedVectorBase< T, BitNum, BitVectorTy, false >::getValue
static T getValue(const BitVectorTy &Bits, unsigned Idx)
Definition: PackedVector.h:31
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1794
llvm::tgtok::Bits
@ Bits
Definition: TGLexer.h:50
T
#define T
Definition: Mips16ISelLowering.cpp:341
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::PackedVector::operator[]
reference operator[](unsigned Idx)
Definition: PackedVector.h:124
llvm::PackedVectorBase< T, BitNum, BitVectorTy, true >::setValue
static void setValue(BitVectorTy &Bits, unsigned Idx, T val)
Definition: PackedVector.h:57
llvm::PackedVector::operator==
bool operator==(const PackedVector &RHS) const
Definition: PackedVector.h:132
llvm::PackedVector::reference::operator=
reference & operator=(T val)
Definition: PackedVector.h:91
llvm::PackedVectorBase
Definition: PackedVector.h:24
llvm::PackedVector::resize
void resize(unsigned N)
Definition: PackedVector.h:110
llvm::PackedVector::operator!=
bool operator!=(const PackedVector &RHS) const
Definition: PackedVector.h:136
false
Definition: StackSlotColoring.cpp:141
BitVector.h
llvm::PackedVector::empty
bool empty() const
Definition: PackedVector.h:104
llvm::PackedVector::reference::reference
reference(PackedVector &vec, unsigned idx)
Definition: PackedVector.h:89
llvm::PackedVector::size
unsigned size() const
Definition: PackedVector.h:106
llvm::PackedVector::PackedVector
PackedVector()=default
val
The initial backend is deliberately restricted to z10 We should add support for later architectures at some point If an asm ties an i32 r result to an i64 the input will be treated as an leaving the upper bits uninitialised For i64 store i32 val
Definition: README.txt:15
llvm::PackedVector::reserve
void reserve(unsigned N)
Definition: PackedVector.h:112
llvm::PackedVector::reference
Definition: PackedVector.h:83
llvm::PackedVectorBase< T, BitNum, BitVectorTy, false >::setValue
static void setValue(BitVectorTy &Bits, unsigned Idx, T val)
Definition: PackedVector.h:38
llvm::PackedVector::reference::reference
reference()=delete
llvm::PackedVector::reset
PackedVector & reset()
Definition: PackedVector.h:114
llvm::PackedVector::push_back
void push_back(T val)
Definition: PackedVector.h:119
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::PackedVector::operator[]
T operator[](unsigned Idx) const
Definition: PackedVector.h:128
llvm::PackedVector::clear
void clear()
Definition: PackedVector.h:108
llvm::PackedVector
Store a vector of values using a specific number of bits for each value.
Definition: PackedVector.h:76
N
#define N
llvm::PackedVector::operator|=
PackedVector & operator|=(const PackedVector &RHS)
Definition: PackedVector.h:140
llvm::PackedVectorBase< T, BitNum, BitVectorTy, true >::getValue
static T getValue(const BitVectorTy &Bits, unsigned Idx)
Definition: PackedVector.h:48
llvm::PackedVector::PackedVector
PackedVector(unsigned size)
Definition: PackedVector.h:102