@@ -66,6 +66,17 @@ namespace {
66
66
using BlockVector = SmallVector<MachineBasicBlock *, 4 >;
67
67
using BlockSet = SmallPtrSet<MachineBasicBlock *, 4 >;
68
68
69
+ static BlockVector getSortedEntries (const BlockSet &Entries) {
70
+ BlockVector SortedEntries (Entries.begin (), Entries.end ());
71
+ llvm::sort (SortedEntries,
72
+ [](const MachineBasicBlock *A, const MachineBasicBlock *B) {
73
+ auto ANum = A->getNumber ();
74
+ auto BNum = B->getNumber ();
75
+ return ANum < BNum;
76
+ });
77
+ return SortedEntries;
78
+ }
79
+
69
80
// Calculates reachability in a region. Ignores branches to blocks outside of
70
81
// the region, and ignores branches to the region entry (for the case where
71
82
// the region is the inner part of a loop).
@@ -241,20 +252,25 @@ class WebAssemblyFixIrreducibleControlFlow final : public MachineFunctionPass {
241
252
bool WebAssemblyFixIrreducibleControlFlow::processRegion (
242
253
MachineBasicBlock *Entry, BlockSet &Blocks, MachineFunction &MF) {
243
254
bool Changed = false ;
244
-
245
255
// Remove irreducibility before processing child loops, which may take
246
256
// multiple iterations.
247
257
while (true ) {
248
258
ReachabilityGraph Graph (Entry, Blocks);
249
259
250
260
bool FoundIrreducibility = false ;
251
261
252
- for (auto *LoopEntry : Graph.getLoopEntries ()) {
262
+ for (auto *LoopEntry : getSortedEntries ( Graph.getLoopEntries () )) {
253
263
// Find mutual entries - all entries which can reach this one, and
254
264
// are reached by it (that always includes LoopEntry itself). All mutual
255
265
// entries must be in the same loop, so if we have more than one, then we
256
266
// have irreducible control flow.
257
267
//
268
+ // (Note that we need to sort the entries here, as otherwise the order can
269
+ // matter: being mutual is a symmetric relationship, and each set of
270
+ // mutuals will be handled properly no matter which we see first. However,
271
+ // there can be multiple disjoint sets of mutuals, and which we process
272
+ // first changes the output.)
273
+ //
258
274
// Note that irreducibility may involve inner loops, e.g. imagine A
259
275
// starts one loop, and it has B inside it which starts an inner loop.
260
276
// If we add a branch from all the way on the outside to B, then in a
@@ -325,13 +341,7 @@ void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
325
341
assert (Entries.size () >= 2 );
326
342
327
343
// Sort the entries to ensure a deterministic build.
328
- BlockVector SortedEntries (Entries.begin (), Entries.end ());
329
- llvm::sort (SortedEntries,
330
- [&](const MachineBasicBlock *A, const MachineBasicBlock *B) {
331
- auto ANum = A->getNumber ();
332
- auto BNum = B->getNumber ();
333
- return ANum < BNum;
334
- });
344
+ BlockVector SortedEntries = getSortedEntries (Entries);
335
345
336
346
#ifndef NDEBUG
337
347
for (auto Block : SortedEntries)
0 commit comments