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