Skip to content

Commit f575ec4

Browse files
committed
IR: Optimize size of use-list order shuffle vectors
Since we're storing lots of these, save two-pointers per vector with a custom type rather than using the relatively heavy `SmallVector`. Part of PR5680. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214135 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 807538b commit f575ec4

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

include/llvm/IR/UseListOrder.h

+49-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,58 @@ class Module;
2525
class Function;
2626
class Value;
2727

28+
/// \brief Structure to hold a use-list shuffle vector.
29+
///
30+
/// Stores most use-lists locally, but large use-lists use an extra heap entry.
31+
/// Costs two fewer pointers than the equivalent \a SmallVector.
32+
class UseListShuffleVector {
33+
unsigned Size;
34+
union {
35+
unsigned *Ptr;
36+
unsigned Array[6];
37+
} Storage;
38+
39+
bool isSmall() const { return Size <= 6; }
40+
unsigned *data() { return isSmall() ? Storage.Array : Storage.Ptr; }
41+
const unsigned *data() const {
42+
return isSmall() ? Storage.Array : Storage.Ptr;
43+
}
44+
45+
public:
46+
UseListShuffleVector() : Size(0) {}
47+
UseListShuffleVector(UseListShuffleVector &&X) {
48+
std::memcpy(this, &X, sizeof(UseListShuffleVector));
49+
X.Size = 0;
50+
}
51+
explicit UseListShuffleVector(size_t Size) : Size(Size) {
52+
if (!isSmall())
53+
Storage.Ptr = new unsigned[Size];
54+
}
55+
~UseListShuffleVector() {
56+
if (!isSmall())
57+
delete Storage.Ptr;
58+
}
59+
60+
typedef unsigned *iterator;
61+
typedef const unsigned *const_iterator;
62+
63+
size_t size() const { return Size; }
64+
iterator begin() { return data(); }
65+
iterator end() { return begin() + size(); }
66+
const_iterator begin() const { return data(); }
67+
const_iterator end() const { return begin() + size(); }
68+
unsigned &operator[](size_t I) { return data()[I]; }
69+
unsigned operator[](size_t I) const { return data()[I]; }
70+
};
71+
2872
/// \brief Structure to hold a use-list order.
2973
struct UseListOrder {
30-
const Function *F;
3174
const Value *V;
32-
SmallVector<unsigned, 8> Shuffle;
75+
const Function *F;
76+
UseListShuffleVector Shuffle;
77+
78+
UseListOrder(const Value *V, const Function *F, size_t ShuffleSize)
79+
: V(V), F(F), Shuffle(ShuffleSize) {}
3380
};
3481

3582
typedef std::vector<UseListOrder> UseListOrderStack;

lib/Bitcode/Writer/ValueEnumerator.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,11 @@ static void predictValueUseListOrderImpl(const Value *V, const Function *F,
133133
return;
134134

135135
// Store the shuffle.
136-
UseListOrder O;
137-
O.V = V;
138-
O.F = F;
139-
for (auto &I : List)
140-
O.Shuffle.push_back(I.second);
141-
Stack.push_back(O);
136+
UseListOrder O(V, F, List.size());
137+
assert(List.size() == O.Shuffle.size() && "Wrong size");
138+
for (size_t I = 0, E = List.size(); I != E; ++I)
139+
O.Shuffle[I] = List[I].second;
140+
Stack.emplace_back(std::move(O));
142141
}
143142

144143
static void predictValueUseListOrder(const Value *V, const Function *F,

0 commit comments

Comments
 (0)