LLVM  15.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"
13 #include "llvm/ADT/SmallVector.h"
14 
15 namespace llvm {
16 
17 class 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?
26 template<unsigned N>
30 
31 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
32  bool Finalized = true;
33 #endif
34 
35 public:
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 || WorklistMap.empty()) && "Neither finalized nor empty");
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
106  MachineInstr *I;
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
i
i
Definition: README.txt:29
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::GISelWorkList::deferred_insert
void deferred_insert(MachineInstr *I)
Definition: GISelWorkList.h:50
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::erase
bool erase(const KeyT &Val)
Definition: DenseMap.h:304
DenseMap.h
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:654
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::clear
void clear()
Definition: DenseMap.h:112
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::DenseMap
Definition: DenseMap.h:716
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:152
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::GISelWorkList::pop_back_val
MachineInstr * pop_back_val()
Definition: GISelWorkList.h:102
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::GISelWorkList::remove
void remove(const MachineInstr *I)
Remove I from the worklist if it exists.
Definition: GISelWorkList.h:83
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::empty
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:98
llvm::GISelWorkList::insert
void insert(MachineInstr *I)
Add the specified instruction to the worklist if it isn't already in it.
Definition: GISelWorkList.h:74
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end
iterator end()
Definition: DenseMap.h:84
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::size
unsigned size() const
Definition: DenseMap.h:101
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:591
SmallVector.h
llvm::GISelWorkList
Definition: GISelWorkList.h:27
N
#define N
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::try_emplace
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:224
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::reserve
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:105
llvm::GISelWorkList::clear
void clear()
Definition: GISelWorkList.h:97
llvm::GISelWorkList::size
unsigned size() const
Definition: GISelWorkList.h:40
llvm::GISelWorkList::empty
bool empty() const
Definition: GISelWorkList.h:38
llvm::GISelWorkList::GISelWorkList
GISelWorkList()
Definition: GISelWorkList.h:36
llvm::GISelWorkList::finalize
void finalize()
Definition: GISelWorkList.h:61