LLVM 20.0.0git
InstructionWorklist.h
Go to the documentation of this file.
1//=== InstructionWorklist.h - Worklist for InstCombine & others -*- 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_TRANSFORMS_UTILS_INSTRUCTIONWORKLIST_H
10#define LLVM_TRANSFORMS_UTILS_INSTRUCTIONWORKLIST_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SetVector.h"
16#include "llvm/IR/Instruction.h"
18#include "llvm/Support/Debug.h"
20
21namespace llvm {
22
23/// InstructionWorklist - This is the worklist management logic for
24/// InstCombine and other simplification passes.
28 /// These instructions will be added in reverse order after the current
29 /// combine has finished. This means that these instructions will be visited
30 /// in the order they have been added.
32
33public:
35
38
39 bool isEmpty() const { return Worklist.empty() && Deferred.empty(); }
40
41 /// Add instruction to the worklist.
42 /// Instructions will be visited in the order they are added.
43 /// You likely want to use this method.
44 void add(Instruction *I) {
45 if (Deferred.insert(I))
46 LLVM_DEBUG(dbgs() << "ADD DEFERRED: " << *I << '\n');
47 }
48
49 /// Add value to the worklist if it is an instruction.
50 /// Instructions will be visited in the order they are added.
51 void addValue(Value *V) {
52 if (Instruction *I = dyn_cast<Instruction>(V))
53 add(I);
54 }
55
56 /// Push the instruction onto the worklist stack.
57 /// Instructions that have been added first will be visited last.
59 assert(I);
60 assert(I->getParent() && "Instruction not inserted yet?");
61
62 if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
63 LLVM_DEBUG(dbgs() << "ADD: " << *I << '\n');
64 Worklist.push_back(I);
65 }
66 }
67
68 void pushValue(Value *V) {
69 if (Instruction *I = dyn_cast<Instruction>(V))
70 push(I);
71 }
72
74 if (Deferred.empty())
75 return nullptr;
76 return Deferred.pop_back_val();
77 }
78
79 void reserve(size_t Size) {
80 Worklist.reserve(Size + 16);
81 WorklistMap.reserve(Size);
82 }
83
84 /// Remove I from the worklist if it exists.
87 if (It != WorklistMap.end()) {
88 // Don't bother moving everything down, just null out the slot.
89 Worklist[It->second] = nullptr;
90 WorklistMap.erase(It);
91 }
92
93 Deferred.remove(I);
94 }
95
97 if (Worklist.empty())
98 return nullptr;
99 Instruction *I = Worklist.pop_back_val();
100 WorklistMap.erase(I);
101 return I;
102 }
103
104 /// When an instruction is simplified, add all users of the instruction
105 /// to the work lists because they might get more simplified now.
107 for (User *U : I.users())
108 push(cast<Instruction>(U));
109 }
110
111 /// Should be called *after* decrementing the use-count on V.
113 if (auto *I = dyn_cast<Instruction>(V)) {
114 add(I);
115 // Many folds have one-use limitations. If there's only one use left,
116 // revisit that use.
117 if (I->hasOneUse())
118 add(cast<Instruction>(*I->user_begin()));
119 }
120 }
121
122 /// Check that the worklist is empty and nuke the backing store for the map.
123 void zap() {
124 assert(WorklistMap.empty() && "Worklist empty, but map not?");
125 assert(Deferred.empty() && "Deferred instructions left over");
126
127 // Do an explicit clear, this shrinks the map if needed.
128 WorklistMap.clear();
129 }
130};
131
132} // end namespace llvm.
133
134#endif
#define LLVM_DEBUG(...)
Definition: Debug.h:106
This file defines the DenseMap class.
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
bool erase(const KeyT &Val)
Definition: DenseMap.h:321
bool empty() const
Definition: DenseMap.h:98
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:211
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
InstructionWorklist - This is the worklist management logic for InstCombine and other simplification ...
void pushUsersToWorkList(Instruction &I)
When an instruction is simplified, add all users of the instruction to the work lists because they mi...
void add(Instruction *I)
Add instruction to the worklist.
void push(Instruction *I)
Push the instruction onto the worklist stack.
InstructionWorklist & operator=(InstructionWorklist &&)=default
void addValue(Value *V)
Add value to the worklist if it is an instruction.
InstructionWorklist(InstructionWorklist &&)=default
void remove(Instruction *I)
Remove I from the worklist if it exists.
void zap()
Check that the worklist is empty and nuke the backing store for the map.
void handleUseCountDecrement(Value *V)
Should be called after decrementing the use-count on V.
bool remove(const value_type &X)
Remove an item from the set vector.
Definition: SetVector.h:188
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
value_type pop_back_val()
Definition: SetVector.h:285
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
void reserve(size_type N)
Definition: SmallVector.h:663
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
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163