InstCombineWorklist.h revision 309124
1228753Smm//===- InstCombineWorklist.h - Worklist for InstCombine pass ----*- C++ -*-===//
2228753Smm//
3228753Smm//                     The LLVM Compiler Infrastructure
4228753Smm//
5228753Smm// This file is distributed under the University of Illinois Open Source
6228753Smm// License. See LICENSE.TXT for details.
7228753Smm//
8228753Smm//===----------------------------------------------------------------------===//
9228753Smm
10228753Smm#ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
11228753Smm#define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
12228753Smm
13228753Smm#include "llvm/ADT/DenseMap.h"
14228753Smm#include "llvm/ADT/STLExtras.h"
15228753Smm#include "llvm/ADT/SmallVector.h"
16228753Smm#include "llvm/IR/Instruction.h"
17228753Smm#include "llvm/Support/Compiler.h"
18228753Smm#include "llvm/Support/Debug.h"
19228753Smm#include "llvm/Support/raw_ostream.h"
20228753Smm
21228753Smm#define DEBUG_TYPE "instcombine"
22228753Smm
23228753Smmnamespace llvm {
24228753Smm
25228753Smm/// InstCombineWorklist - This is the worklist management logic for
26228753Smm/// InstCombine.
27228753Smmclass InstCombineWorklist {
28228753Smm  SmallVector<Instruction*, 256> Worklist;
29228753Smm  DenseMap<Instruction*, unsigned> WorklistMap;
30228753Smm
31228753Smm  void operator=(const InstCombineWorklist&RHS) = delete;
32228753Smm  InstCombineWorklist(const InstCombineWorklist&) = delete;
33228753Smmpublic:
34228753Smm  InstCombineWorklist() {}
35228753Smm
36228753Smm  InstCombineWorklist(InstCombineWorklist &&Arg)
37228753Smm      : Worklist(std::move(Arg.Worklist)),
38228753Smm        WorklistMap(std::move(Arg.WorklistMap)) {}
39228753Smm  InstCombineWorklist &operator=(InstCombineWorklist &&RHS) {
40228753Smm    Worklist = std::move(RHS.Worklist);
41228753Smm    WorklistMap = std::move(RHS.WorklistMap);
42228753Smm    return *this;
43228753Smm  }
44228753Smm
45228753Smm  bool isEmpty() const { return Worklist.empty(); }
46228753Smm
47228753Smm  /// Add - Add the specified instruction to the worklist if it isn't already
48228753Smm  /// in it.
49228753Smm  void Add(Instruction *I) {
50228753Smm    if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
51228753Smm      DEBUG(dbgs() << "IC: ADD: " << *I << '\n');
52228753Smm      Worklist.push_back(I);
53228753Smm    }
54228753Smm  }
55228753Smm
56228753Smm  void AddValue(Value *V) {
57228753Smm    if (Instruction *I = dyn_cast<Instruction>(V))
58228753Smm      Add(I);
59228753Smm  }
60228753Smm
61228753Smm  /// AddInitialGroup - Add the specified batch of stuff in reverse order.
62228753Smm  /// which should only be done when the worklist is empty and when the group
63228753Smm  /// has no duplicates.
64228753Smm  void AddInitialGroup(ArrayRef<Instruction *> List) {
65228753Smm    assert(Worklist.empty() && "Worklist must be empty to add initial group");
66228753Smm    Worklist.reserve(List.size()+16);
67228753Smm    WorklistMap.reserve(List.size());
68228753Smm    DEBUG(dbgs() << "IC: ADDING: " << List.size() << " instrs to worklist\n");
69228753Smm    unsigned Idx = 0;
70228753Smm    for (Instruction *I : reverse(List)) {
71228753Smm      WorklistMap.insert(std::make_pair(I, Idx++));
72228753Smm      Worklist.push_back(I);
73228753Smm    }
74228753Smm  }
75228753Smm
76228753Smm  // Remove - remove I from the worklist if it exists.
77228753Smm  void Remove(Instruction *I) {
78228753Smm    DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
79228753Smm    if (It == WorklistMap.end()) return; // Not in worklist.
80228753Smm
81228753Smm    // Don't bother moving everything down, just null out the slot.
82228753Smm    Worklist[It->second] = nullptr;
83228753Smm
84228753Smm    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