Skip to content

Commit e1a0e82

Browse files
committed
[GlobalISel] Copy the implementation of SubtargetFeature and use it for PredicateBitset.
PredicateBitset currently uses std::bitset, but std::bitset doesn't have a constexpr constructor or any constexpr methods until C++23. Each target that supports GlobalIsel has as an array of PredicateBitset objects that currently use a global constructor. SubtargetFeature used by the MC layer for feature bits, has its own implementation of std::bitset that has constexpr constructor and methods that provides all the capabilities that PredicateBitset needs. This patch copies the implementation from SubtargetFeature, makes it a template class, and puts it in ADT. I'll migrate SubtargetFeature in a separate patch. Adapting all existing users to it being a template was distracting from the goal of this patch. This reduces the binary size of llc built with gcc 8.5.0 on my local build by ~15k. Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D158576
1 parent f862d62 commit e1a0e82

File tree

4 files changed

+165
-30
lines changed

4 files changed

+165
-30
lines changed

llvm/include/llvm/ADT/Bitset.h

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
//=== llvm/ADT/Bitset.h - constexpr std::bitset -----------------*- 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+
// Defines a std::bitset like container that can be used in constexprs.
10+
// That constructor and many of the methods are constexpr. std::bitset doesn't
11+
// get constexpr methods until C++23. This class also provides a constexpr
12+
// constructor that accepts an initializer_list of bits to set.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_ADT_BITSET_H
17+
#define LLVM_ADT_BITSET_H
18+
19+
#include <llvm/ADT/STLExtras.h>
20+
#include <array>
21+
#include <climits>
22+
#include <cstdint>
23+
24+
namespace llvm {
25+
26+
/// Container class for subtarget features.
27+
/// This is a constexpr reimplementation of a subset of std::bitset. It would be
28+
/// nice to use std::bitset directly, but it doesn't support constant
29+
/// initialization.
30+
template <unsigned NumBits>
31+
class Bitset {
32+
typedef uintptr_t BitWord;
33+
34+
enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
35+
36+
static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
37+
"Unsupported word size");
38+
39+
static constexpr unsigned NumWords = (NumBits + BITWORD_SIZE-1) / BITWORD_SIZE;
40+
std::array<BitWord, NumWords> Bits{};
41+
42+
protected:
43+
constexpr Bitset(const std::array<BitWord, NumWords> &B)
44+
: Bits{B} {}
45+
46+
public:
47+
constexpr Bitset() = default;
48+
constexpr Bitset(std::initializer_list<unsigned> Init) {
49+
for (auto I : Init)
50+
set(I);
51+
}
52+
53+
Bitset &set() {
54+
std::fill(std::begin(Bits), std::end(Bits), -BitWord(0));
55+
return *this;
56+
}
57+
58+
constexpr Bitset &set(unsigned I) {
59+
// GCC <6.2 crashes if this is written in a single statement.
60+
BitWord NewBits = Bits[I / BITWORD_SIZE] | (BitWord(1) << (I % BITWORD_SIZE));
61+
Bits[I / BITWORD_SIZE] = NewBits;
62+
return *this;
63+
}
64+
65+
constexpr Bitset &reset(unsigned I) {
66+
// GCC <6.2 crashes if this is written in a single statement.
67+
BitWord NewBits = Bits[I / BITWORD_SIZE] & ~(BitWord(1) << (I % BITWORD_SIZE));
68+
Bits[I / BITWORD_SIZE] = NewBits;
69+
return *this;
70+
}
71+
72+
constexpr Bitset &flip(unsigned I) {
73+
// GCC <6.2 crashes if this is written in a single statement.
74+
BitWord NewBits = Bits[I / BITWORD_SIZE] ^ (BitWord(1) << (I % BITWORD_SIZE));
75+
Bits[I / BITWORD_SIZE] = NewBits;
76+
return *this;
77+
}
78+
79+
constexpr bool operator[](unsigned I) const {
80+
BitWord Mask = BitWord(1) << (I % BITWORD_SIZE);
81+
return (Bits[I / BITWORD_SIZE] & Mask) != 0;
82+
}
83+
84+
constexpr bool test(unsigned I) const { return (*this)[I]; }
85+
86+
constexpr size_t size() const { return NumBits; }
87+
88+
bool any() const {
89+
return llvm::any_of(Bits, [](BitWord I) { return I != 0; });
90+
}
91+
bool none() const { return !any(); }
92+
size_t count() const {
93+
size_t Count = 0;
94+
for (auto B : Bits)
95+
Count += llvm::popcount(B);
96+
return Count;
97+
}
98+
99+
constexpr Bitset &operator^=(const Bitset &RHS) {
100+
for (unsigned I = 0, E = Bits.size(); I != E; ++I) {
101+
Bits[I] ^= RHS.Bits[I];
102+
}
103+
return *this;
104+
}
105+
constexpr Bitset operator^(const Bitset &RHS) const {
106+
Bitset Result = *this;
107+
Result ^= RHS;
108+
return Result;
109+
}
110+
111+
constexpr Bitset &operator&=(const Bitset &RHS) {
112+
for (unsigned I = 0, E = Bits.size(); I != E; ++I) {
113+
Bits[I] &= RHS.Bits[I];
114+
}
115+
return *this;
116+
}
117+
constexpr Bitset operator&(const Bitset &RHS) const {
118+
Bitset Result = *this;
119+
Result &= RHS;
120+
return Result;
121+
}
122+
123+
constexpr Bitset &operator|=(const Bitset &RHS) {
124+
for (unsigned I = 0, E = Bits.size(); I != E; ++I) {
125+
Bits[I] |= RHS.Bits[I];
126+
}
127+
return *this;
128+
}
129+
constexpr Bitset operator|(const Bitset &RHS) const {
130+
Bitset Result = *this;
131+
Result |= RHS;
132+
return Result;
133+
}
134+
135+
constexpr Bitset operator~() const {
136+
Bitset Result = *this;
137+
for (auto &B : Result.Bits)
138+
B = ~B;
139+
return Result;
140+
}
141+
142+
bool operator==(const Bitset &RHS) const {
143+
return std::equal(std::begin(Bits), std::end(Bits), std::begin(RHS.Bits));
144+
}
145+
146+
bool operator!=(const Bitset &RHS) const { return !(*this == RHS); }
147+
148+
bool operator < (const Bitset &Other) const {
149+
for (unsigned I = 0, E = size(); I != E; ++I) {
150+
bool LHS = test(I), RHS = Other.test(I);
151+
if (LHS != RHS)
152+
return LHS < RHS;
153+
}
154+
return false;
155+
}
156+
};
157+
158+
} // end namespace llvm
159+
160+
#endif

llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
1616
#define LLVM_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
1717

18+
#include "llvm/ADT/Bitset.h"
1819
#include "llvm/ADT/DenseMap.h"
1920
#include "llvm/ADT/SmallVector.h"
2021
#include "llvm/CodeGen/GlobalISel/Utils.h"
@@ -47,32 +48,6 @@ class RegisterBankInfo;
4748
class TargetInstrInfo;
4849
class TargetRegisterInfo;
4950

50-
/// Container class for CodeGen predicate results.
51-
/// This is convenient because std::bitset does not have a constructor
52-
/// with an initializer list of set bits.
53-
///
54-
/// Each GIMatchTableExecutor subclass should define a PredicateBitset class
55-
/// with:
56-
/// const unsigned MAX_SUBTARGET_PREDICATES = 192;
57-
/// using PredicateBitset = PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;
58-
/// and updating the constant to suit the target. Tablegen provides a suitable
59-
/// definition for the predicates in use in <Target>GenGlobalISel.inc when
60-
/// GET_GLOBALISEL_PREDICATE_BITSET is defined.
61-
template <std::size_t MaxPredicates>
62-
class PredicateBitsetImpl : public std::bitset<MaxPredicates> {
63-
public:
64-
// Cannot inherit constructors because it's not supported by VC++..
65-
PredicateBitsetImpl() = default;
66-
67-
PredicateBitsetImpl(const std::bitset<MaxPredicates> &B)
68-
: std::bitset<MaxPredicates>(B) {}
69-
70-
PredicateBitsetImpl(std::initializer_list<unsigned> Init) {
71-
for (auto I : Init)
72-
std::bitset<MaxPredicates>::set(I);
73-
}
74-
};
75-
7651
enum {
7752
GICXXPred_Invalid = 0,
7853
GICXXCustomAction_Invalid = 0,

llvm/test/TableGen/GlobalISelEmitter.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def HasC : Predicate<"Subtarget->hasC()"> { let RecomputePerFunction = 1; }
6767
//===- Test the function boilerplate. -------------------------------------===//
6868

6969
// CHECK: const unsigned MAX_SUBTARGET_PREDICATES = 3;
70-
// CHECK: using PredicateBitset = llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;
70+
// CHECK: using PredicateBitset = llvm::Bitset<MAX_SUBTARGET_PREDICATES>;
7171

7272
// CHECK-LABEL: #ifdef GET_GLOBALISEL_TEMPORARIES_DECL
7373
// CHECK-NEXT: mutable MatcherState State;
@@ -131,7 +131,7 @@ def HasC : Predicate<"Subtarget->hasC()"> { let RecomputePerFunction = 1; }
131131
// CHECK-NEXT: GIFBS_HasA,
132132
// CHECK-NEXT: GIFBS_HasA_HasB_HasC,
133133
// CHECK-NEXT: }
134-
// CHECK-NEXT: const static PredicateBitset FeatureBitsets[] {
134+
// CHECK-NEXT: constexpr static PredicateBitset FeatureBitsets[] {
135135
// CHECK-NEXT: {}, // GIFBS_Invalid
136136
// CHECK-NEXT: {Feature_HasABit, },
137137
// CHECK-NEXT: {Feature_HasABit, Feature_HasBBit, Feature_HasCBit, },

llvm/utils/TableGen/GlobalISelMatchTableExecutorEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void GlobalISelMatchTableExecutorEmitter::emitSubtargetFeatureBitsetImpl(
7979
OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n";
8080
}
8181
OS << "};\n"
82-
<< "const static PredicateBitset FeatureBitsets[] {\n"
82+
<< "constexpr static PredicateBitset FeatureBitsets[] {\n"
8383
<< " {}, // GIFBS_Invalid\n";
8484
for (const auto &FeatureBitset : FeatureBitsets) {
8585
if (FeatureBitset.empty())
@@ -188,7 +188,7 @@ void GlobalISelMatchTableExecutorEmitter::emitPredicateBitset(
188188
<< "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size()
189189
<< ";\n"
190190
<< "using PredicateBitset = "
191-
"llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n"
191+
"llvm::Bitset<MAX_SUBTARGET_PREDICATES>;\n"
192192
<< "#endif // ifdef " << IfDefName << "\n\n";
193193
}
194194

0 commit comments

Comments
 (0)