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 INSTCOMBINE_WORKLIST_H
11 #define INSTCOMBINE_WORKLIST_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"
22 /// InstCombineWorklist - This is the worklist management logic for
24 class LLVM_LIBRARY_VISIBILITY InstCombineWorklist {
25 SmallVector<Instruction*, 256> Worklist;
26 DenseMap<Instruction*, unsigned> WorklistMap;
28 void operator=(const InstCombineWorklist&RHS) LLVM_DELETED_FUNCTION;
29 InstCombineWorklist(const InstCombineWorklist&) LLVM_DELETED_FUNCTION;
31 InstCombineWorklist() {}
33 bool isEmpty() const { return Worklist.empty(); }
35 /// Add - Add the specified instruction to the worklist if it isn't already
37 void Add(Instruction *I) {
38 if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
39 DEBUG(dbgs() << "IC: ADD: " << *I << '\n');
40 Worklist.push_back(I);
44 void AddValue(Value *V) {
45 if (Instruction *I = dyn_cast<Instruction>(V))
49 /// AddInitialGroup - Add the specified batch of stuff in reverse order.
50 /// which should only be done when the worklist is empty and when the group
51 /// has no duplicates.
52 void AddInitialGroup(Instruction *const *List, unsigned NumEntries) {
53 assert(Worklist.empty() && "Worklist must be empty to add initial group");
54 Worklist.reserve(NumEntries+16);
55 WorklistMap.resize(NumEntries);
56 DEBUG(dbgs() << "IC: ADDING: " << NumEntries << " instrs to worklist\n");
57 for (unsigned Idx = 0; NumEntries; --NumEntries) {
58 Instruction *I = List[NumEntries-1];
59 WorklistMap.insert(std::make_pair(I, Idx++));
60 Worklist.push_back(I);
64 // Remove - remove I from the worklist if it exists.
65 void Remove(Instruction *I) {
66 DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
67 if (It == WorklistMap.end()) return; // Not in worklist.
69 // Don't bother moving everything down, just null out the slot.
70 Worklist[It->second] = 0;
72 WorklistMap.erase(It);
75 Instruction *RemoveOne() {
76 Instruction *I = Worklist.pop_back_val();
81 /// AddUsersToWorkList - When an instruction is simplified, add all users of
82 /// the instruction to the work lists because they might get more simplified
85 void AddUsersToWorkList(Instruction &I) {
86 for (User *U : I.users())
87 Add(cast<Instruction>(U));
91 /// Zap - check that the worklist is empty and nuke the backing store for
92 /// the map if it is large.
94 assert(WorklistMap.empty() && "Worklist empty, but map not?");
96 // Do an explicit clear, this shrinks the map if needed.
101 } // end namespace llvm.