LLVM 20.0.0git
GISelWorkList.h
Go to the documentation of this file.
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"
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>
30
31#if LLVM_ENABLE_ABI_BREAKING_CHECKS
32 bool Finalized = true;
33#endif
34
35public:
36 GISelWorkList() : WorklistMap(N) {}
37
38 bool empty() const { return WorklistMap.empty(); }
39
40 unsigned size() const { return WorklistMap.size(); }
41
42 // Since we don't know ahead of time how many instructions we're going to add
43 // to the worklist, and migrating densemap's elements is quite expensive
44 // everytime we resize, only insert to the smallvector (typically during the
45 // initial phase of populating lists). Before the worklist can be used,
46 // finalize should be called. Also assert with NDEBUG if list is ever used
47 // without finalizing. Note that unlike insert, we won't check for duplicates
48 // - so the ideal place to use this is during the initial prepopulating phase
49 // of most passes.
51 Worklist.push_back(I);
52#if LLVM_ENABLE_ABI_BREAKING_CHECKS
53 Finalized = false;
54#endif
55 }
56
57 // This should only be called when using deferred_insert.
58 // This asserts that the WorklistMap is empty, and then
59 // inserts all the elements in the Worklist into the map.
60 // It also asserts if there are any duplicate elements found.
61 void finalize() {
62 assert(WorklistMap.empty() && "Expecting empty worklistmap");
63 if (Worklist.size() > N)
64 WorklistMap.reserve(Worklist.size());
65 for (unsigned i = 0; i < Worklist.size(); ++i)
66 if (!WorklistMap.try_emplace(Worklist[i], i).second)
67 llvm_unreachable("Duplicate elements in the list");
68#if LLVM_ENABLE_ABI_BREAKING_CHECKS
69 Finalized = true;
70#endif
71 }
72
73 /// Add the specified instruction to the worklist if it isn't already in it.
75#if LLVM_ENABLE_ABI_BREAKING_CHECKS
76 assert(Finalized && "GISelWorkList used without finalizing");
77#endif
78 if (WorklistMap.try_emplace(I, Worklist.size()).second)
79 Worklist.push_back(I);
80 }
81
82 /// Remove I from the worklist if it exists.
83 void remove(const MachineInstr *I) {
84#if LLVM_ENABLE_ABI_BREAKING_CHECKS
85 assert(Finalized && "GISelWorkList used without finalizing");
86#endif
87 auto It = WorklistMap.find(I);
88 if (It == WorklistMap.end())
89 return; // Not in worklist.
90
91 // Don't bother moving everything down, just null out the slot.
92 Worklist[It->second] = nullptr;
93
94 WorklistMap.erase(It);
95 }
96
97 void clear() {
98 Worklist.clear();
99 WorklistMap.clear();
100 }
101
103#if LLVM_ENABLE_ABI_BREAKING_CHECKS
104 assert(Finalized && "GISelWorkList used without finalizing");
105#endif
107 do {
108 I = Worklist.pop_back_val();
109 } while(!I);
110 assert(I && "Pop back on empty worklist");
111 WorklistMap.erase(I);
112 return I;
113 }
114};
115
116} // end namespace llvm.
117
118#endif
This file defines the DenseMap class.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:226
bool erase(const KeyT &Val)
Definition: DenseMap.h:321
unsigned size() const
Definition: DenseMap.h:99
bool empty() const
Definition: DenseMap.h:98
iterator end()
Definition: DenseMap.h:84
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
void insert(MachineInstr *I)
Add the specified instruction to the worklist if it isn't already in it.
Definition: GISelWorkList.h:74
MachineInstr * pop_back_val()
unsigned size() const
Definition: GISelWorkList.h:40
void deferred_insert(MachineInstr *I)
Definition: GISelWorkList.h:50
bool empty() const
Definition: GISelWorkList.h:38
void remove(const MachineInstr *I)
Remove I from the worklist if it exists.
Definition: GISelWorkList.h:83
Representation of each machine instruction.
Definition: MachineInstr.h:69
size_t size() const
Definition: SmallVector.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
#define N