1 //===- InstCombineWorklist.h - Worklist for InstCombine pass ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
11 #define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/IR/Instruction.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/raw_ostream.h"
20 #define DEBUG_TYPE "instcombine"
24 /// InstCombineWorklist - This is the worklist management logic for
26 class InstCombineWorklist {
27 SmallVector<Instruction*, 256> Worklist;
28 DenseMap<Instruction*, unsigned> WorklistMap;
30 void operator=(const InstCombineWorklist&RHS) LLVM_DELETED_FUNCTION;
31 InstCombineWorklist(const InstCombineWorklist&) LLVM_DELETED_FUNCTION;
33 InstCombineWorklist() {}
35 InstCombineWorklist(InstCombineWorklist &&Arg)
36 : Worklist(std::move(Arg.Worklist)),
37 WorklistMap(std::move(Arg.WorklistMap)) {}
38 InstCombineWorklist &operator=(InstCombineWorklist &&RHS) {
39 Worklist = std::move(RHS.Worklist);
40 WorklistMap = std::move(RHS.WorklistMap);
44 bool isEmpty() const { return Worklist.empty(); }
46 /// Add - Add the specified instruction to the worklist if it isn't already
48 void Add(Instruction *I) {
49 if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
50 DEBUG(dbgs() << "IC: ADD: " << *I << '\n');
51 Worklist.push_back(I);
55 void AddValue(Value *V) {
56 if (Instruction *I = dyn_cast<Instruction>(V))
60 /// AddInitialGroup - Add the specified batch of stuff in reverse order.
61 /// which should only be done when the worklist is empty and when the group
62 /// has no duplicates.
63 void AddInitialGroup(Instruction *const *List, unsigned NumEntries) {
64 assert(Worklist.empty() && "Worklist must be empty to add initial group");
65 Worklist.reserve(NumEntries+16);
66 WorklistMap.resize(NumEntries);
67 DEBUG(dbgs() << "IC: ADDING: " << NumEntries << " instrs to worklist\n");
68 for (unsigned Idx = 0; NumEntries; --NumEntries) {
69 Instruction *I = List[NumEntries-1];
70 WorklistMap.insert(std::make_pair(I, Idx++));
71 Worklist.push_back(I);
75 // Remove - remove I from the worklist if it exists.
76 void Remove(Instruction *I) {
77 DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
78 if (It == WorklistMap.end()) return; // Not in worklist.
80 // Don't bother moving everything down, just null out the slot.
81 Worklist[It->second] = nullptr;
83 WorklistMap.erase(It);
86 Instruction *RemoveOne() {
87 Instruction *I = Worklist.pop_back_val();
92 /// AddUsersToWorkList - When an instruction is simplified, add all users of
93 /// the instruction to the work lists because they might get more simplified
96 void AddUsersToWorkList(Instruction &I) {
97 for (User *U : I.users())
98 Add(cast<Instruction>(U));
102 /// Zap - check that the worklist is empty and nuke the backing store for
103 /// the map if it is large.
105 assert(WorklistMap.empty() && "Worklist empty, but map not?");
107 // Do an explicit clear, this shrinks the map if needed.
112 } // end namespace llvm.