InstCombineWorklist.h revision 309124
1//===- InstCombineWorklist.h - Worklist for InstCombine pass ----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
11#define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
12
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/IR/Instruction.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/raw_ostream.h"
20
21#define DEBUG_TYPE "instcombine"
22
23namespace llvm {
24
25/// InstCombineWorklist - This is the worklist management logic for
26/// InstCombine.
27class InstCombineWorklist {
28  SmallVector<Instruction*, 256> Worklist;
29  DenseMap<Instruction*, unsigned> WorklistMap;
30
31  void operator=(const InstCombineWorklist&RHS) = delete;
32  InstCombineWorklist(const InstCombineWorklist&) = delete;
33public:
34  InstCombineWorklist() {}
35
36  InstCombineWorklist(InstCombineWorklist &&Arg)
37      : Worklist(std::move(Arg.Worklist)),
38        WorklistMap(std::move(Arg.WorklistMap)) {}
39  InstCombineWorklist &operator=(InstCombineWorklist &&RHS) {
40    Worklist = std::move(RHS.Worklist);
41    WorklistMap = std::move(RHS.WorklistMap);
42    return *this;
43  }
44
45  bool isEmpty() const { return Worklist.empty(); }
46
47  /// Add - Add the specified instruction to the worklist if it isn't already
48  /// in it.
49  void Add(Instruction *I) {
50    if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
51      DEBUG(dbgs() << "IC: ADD: " << *I << '\n');
52      Worklist.push_back(I);
53    }
54  }
55
56  void AddValue(Value *V) {
57    if (Instruction *I = dyn_cast<Instruction>(V))
58      Add(I);
59  }
60
61  /// AddInitialGroup - Add the specified batch of stuff in reverse order.
62  /// which should only be done when the worklist is empty and when the group
63  /// has no duplicates.
64  void AddInitialGroup(ArrayRef<Instruction *> List) {
65    assert(Worklist.empty() && "Worklist must be empty to add initial group");
66    Worklist.reserve(List.size()+16);
67    WorklistMap.reserve(List.size());
68    DEBUG(dbgs() << "IC: ADDING: " << List.size() << " instrs to worklist\n");
69    unsigned Idx = 0;
70    for (Instruction *I : reverse(List)) {
71      WorklistMap.insert(std::make_pair(I, Idx++));
72      Worklist.push_back(I);
73    }
74  }
75
76  // Remove - remove I from the worklist if it exists.
77  void Remove(Instruction *I) {
78    DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
79    if (It == WorklistMap.end()) return; // Not in worklist.
80
81    // Don't bother moving everything down, just null out the slot.
82    Worklist[It->second] = nullptr;
83
84    WorklistMap.erase(It);
85  }
86
87  Instruction *RemoveOne() {
88    Instruction *I = Worklist.pop_back_val();
89    WorklistMap.erase(I);
90    return I;
91  }
92
93  /// AddUsersToWorkList - When an instruction is simplified, add all users of
94  /// the instruction to the work lists because they might get more simplified
95  /// now.
96  ///
97  void AddUsersToWorkList(Instruction &I) {
98    for (User *U : I.users())
99      Add(cast<Instruction>(U));
100  }
101
102
103  /// Zap - check that the worklist is empty and nuke the backing store for
104  /// the map if it is large.
105  void Zap() {
106    assert(WorklistMap.empty() && "Worklist empty, but map not?");
107
108    // Do an explicit clear, this shrinks the map if needed.
109    WorklistMap.clear();
110  }
111};
112
113} // end namespace llvm.
114
115#undef DEBUG_TYPE
116
117#endif
118