Debian Codegraph @

gcc/testsuite/g++.dg/opt/pr110515.C

raw
1 // { dg-do run }
2 // { dg-require-effective-target c++11 }
3 // { dg-options "-O2" }
4
5 typedef __UINT64_TYPE__ u64;
6
7 struct SmallDenseMap {
8 static constexpr u64 EmptyKey = 0xC0FFEUL;
9 struct V { u64 v; };
10
11 bool contains(u64 Val) {
12 V *TheSlot = nullptr;
13 return (LookupSlotFor(Val, TheSlot) ? 1 : 0);
14 }
15
16 void try_emplace(u64 Key) {
17 V *TheSlot = nullptr;
18 if (LookupSlotFor(Key, TheSlot))
19 return;
20
21 // Otherwise, insert the new element.
22 InsertIntoSlot(TheSlot, Key);
23 }
24
25 void moveFromOldSlots(V *OldSlotsBegin, V *OldSlotsEnd) {
26 Size = 0;
27
28 V *B_ = u.o.Slots;
29 V *E_ = B_ + u.o.Capacity;
30 for (; B_ != E_; ++B_)
31 B_->v = EmptyKey;
32
33 // Insert all the old elements.
34 V *O = OldSlotsBegin;
35 V *E = OldSlotsEnd;
36 for (; O != E; ++O) {
37 if (O->v != EmptyKey) {
38 // Insert the key/value into the new table.
39 V * N = nullptr;
40 LookupSlotFor(O->v, N);
41 N->v = O->v;
42 Size++;
43 }
44 }
45 }
46
47 void InsertIntoSlot(V *TheSlot, u64 Key) {
48 unsigned NewSize = Size + 1;
49 unsigned Capacity = getCapacity();
50 // Make sure we always keep at least one Empty value
51 if (NewSize >= Capacity) {
52 //fprintf(stderr, "GROW: size=%u capacity=%u -> ...\n", Size, Capacity);
53 grow();
54 LookupSlotFor(Key, TheSlot);
55 Capacity = getCapacity();
56 //fprintf(stderr, "GROW: ... -> size=%u capacity=%u\n", NewSize, Capacity);
57 }
58
59 Size++;
60
61 TheSlot->v = Key;
62 }
63
64 bool LookupSlotFor(u64 Val,
65 V *&FoundSlot) {
66 V *SlotsPtr = getSlots();
67 const unsigned Capacity = getCapacity();
68
69 for (unsigned i = 0; i < Capacity; ++i) {
70 V *ThisSlot = SlotsPtr + i;
71 if (Val == ThisSlot->v) {
72 FoundSlot = ThisSlot;
73 return true;
74 }
75
76 if (ThisSlot->v == EmptyKey) {
77 FoundSlot = ThisSlot;
78 return false;
79 }
80 }
81 // Guarantee that within an array there is a match
82 // or Empty value where to insert a new vaue.
83 __builtin_trap();
84 }
85
86 // Needs to bea at least 1 to hld one empty value
87 static constexpr unsigned InlineSlots = 2;
88
89 bool Small;
90 unsigned Size;
91
92 struct LargeRep {
93 V *Slots;
94 unsigned Capacity;
95 };
96
97 union {
98 V i[InlineSlots]; // Small = true
99 LargeRep o; // Small = false
100 } u;
101
102 explicit SmallDenseMap() : Small(true), Size(0) {
103 Size = 0;
104
105 V *B = u.i;
106 V *E = B + InlineSlots;
107 for (; B != E; ++B)
108 B->v = EmptyKey;
109 }
110
111 void grow() {
112 // assert:
113 if (!Small) __builtin_trap();
114
115 // First move the inline Slots into a temporary storage.
116 V TmpStorage[InlineSlots];
117 V *TmpBegin = TmpStorage;
118 V *TmpEnd = TmpBegin;
119
120 // Loop over the Slots, moving non-empty, non-tombstones into the
121 // temporary storage. Have the loop move the TmpEnd forward as it goes.
122 V *P = u.i;
123 V *E = P + InlineSlots;
124 for (; P != E; ++P) {
125 if (P->v != EmptyKey) {
126 TmpEnd->v = P->v;
127 ++TmpEnd;
128 }
129 }
130
131 Small = false;
132 u.o = LargeRep{new V[128], 128};
133 moveFromOldSlots(TmpBegin, TmpEnd);
134 }
135
136 V *getSlots() {
137 if (Small) {
138 V * inl = u.i;
139 return inl;
140 }
141 else {
142 LargeRep * rep = &u.o;
143 return rep->Slots;
144 }
145 }
146
147 unsigned getCapacity() {
148 if (Small) {
149 return InlineSlots;
150 }
151 else {
152 LargeRep * rep = &u.o;
153 return rep->Capacity;
154 }
155 }
156 };
157
158 #pragma GCC optimize(0)
159
160 struct P {
161 u64 f;
162 bool s;
163 };
164
165 static u64 ws = 0;
166 static P WorkList[128];
167
168 __attribute__((noipa))
169 static void popupateIni() {
170 for (u64 Var : (u64[]){8,7,6,5,4,3,0}) {
171 WorkList[ws++] = P{Var, false};
172 }
173 }
174
175 __attribute__((noipa))
176 static void checkCycle(u64 Var) {
177 // Detect cycles.
178 static bool seen[256];
179 if (Var >= 256 || seen[Var]) __builtin_trap();
180 seen[Var] = true;
181 }
182
183
184 __attribute__((noipa))
185 static void populateDeps(u64 Var) {
186
187 WorkList[ws++] = P{Var, true};
188 if (Var == 8)
189 WorkList[ws++] = P{0, false};
190 }
191
192
193 __attribute__((noipa)) __attribute__((optimize(3)))
194 static void bug() {
195
196 // triggers growth on insert
197 SmallDenseMap Visited;
198
199 popupateIni();
200
201 while (ws > 0) {
202 P Item = WorkList[--ws];
203 u64 Var = Item.f;
204 bool visitedAllDependencies = Item.s;
205
206 if (Visited.contains(Var)) {
207 continue;
208 }
209
210 if (visitedAllDependencies) {
211 Visited.try_emplace(Var);
212 continue;
213 }
214
215 checkCycle(Var);
216 populateDeps(Var);
217 }
218 }
219
220 __attribute__((noipa))
221 int main() {
222 bug();
223 }