1//===- GISelWorkList.h - Worklist for GISel passes ----*- 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#ifndef LLVM_CODEGEN_GLOBALISEL_GISELWORKLIST_H
10#define LLVM_CODEGEN_GLOBALISEL_GISELWORKLIST_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/SmallVector.h"
14
15namespace llvm {
16
17class MachineInstr;
18
19// Worklist which mostly works similar to InstCombineWorkList, but on
20// MachineInstrs. The main difference with something like a SetVector is that
21// erasing an element doesn't move all elements over one place - instead just
22// nulls out the element of the vector.
23//
24// FIXME: Does it make sense to factor out common code with the
25// instcombinerWorkList?
26template<unsigned N>
27class GISelWorkList {
28 SmallVector<MachineInstr *, N> Worklist;
29 DenseMap<MachineInstr *, unsigned> WorklistMap;
30
31#if LLVM_ENABLE_ABI_BREAKING_CHECKS
32 bool Finalized = true;
33#endif
34
35public:
36 bool empty() const { return WorklistMap.empty(); }
37
38 unsigned size() const { return WorklistMap.size(); }
39
40 // Since we don't know ahead of time how many instructions we're going to add
41 // to the worklist, and migrating densemap's elements is quite expensive
42 // everytime we resize, only insert to the smallvector (typically during the
43 // initial phase of populating lists). Before the worklist can be used,
44 // finalize should be called. Also assert with NDEBUG if list is ever used
45 // without finalizing. Note that unlike insert, we won't check for duplicates
46 // - so the ideal place to use this is during the initial prepopulating phase
47 // of most passes.
48 void deferred_insert(MachineInstr *I) {
49 Worklist.push_back(I);
50#if LLVM_ENABLE_ABI_BREAKING_CHECKS
51 Finalized = false;
52#endif
53 }
54
55 // This should only be called when using deferred_insert.
56 // This asserts that the WorklistMap is empty, and then
57 // inserts all the elements in the Worklist into the map.
58 // It also asserts if there are any duplicate elements found.
59 void finalize() {
60 assert(WorklistMap.empty() && "Expecting empty worklistmap");
61 if (!Worklist.empty())
62 WorklistMap.reserve(NumEntries: Worklist.size() > N ? Worklist.size() : N);
63 for (unsigned i = 0; i < Worklist.size(); ++i)
64 if (!WorklistMap.try_emplace(Worklist[i], i).second)
65 llvm_unreachable("Duplicate elements in the list");
66#if LLVM_ENABLE_ABI_BREAKING_CHECKS
67 Finalized = true;
68#endif
69 }
70
71 /// Add the specified instruction to the worklist if it isn't already in it.
72 void insert(MachineInstr *I) {
73#if LLVM_ENABLE_ABI_BREAKING_CHECKS
74 assert(Finalized && "GISelWorkList used without finalizing");
75#endif
76 if (WorklistMap.try_emplace(I, Worklist.size()).second)
77 Worklist.push_back(I);
78 }
79
80 /// Remove I from the worklist if it exists.
81 void remove(const MachineInstr *I) {
82#if LLVM_ENABLE_ABI_BREAKING_CHECKS
83 assert(Finalized && "GISelWorkList used without finalizing");
84#endif
85 auto It = WorklistMap.find(Val: I);
86 if (It == WorklistMap.end())
87 return; // Not in worklist.
88
89 // Don't bother moving everything down, just null out the slot.
90 Worklist[It->second] = nullptr;
91
92 WorklistMap.erase(I: It);
93 }
94
95 void clear() {
96 Worklist.clear();
97 WorklistMap.clear();
98 }
99
100 MachineInstr *pop_back_val() {
101#if LLVM_ENABLE_ABI_BREAKING_CHECKS
102 assert(Finalized && "GISelWorkList used without finalizing");
103#endif
104 MachineInstr *I;
105 do {
106 I = Worklist.pop_back_val();
107 } while(!I);
108 assert(I && "Pop back on empty worklist");
109 WorklistMap.erase(Val: I);
110 return I;
111 }
112};
113
114} // end namespace llvm.
115
116#endif
117