InstCombineWorklist.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
  10. #define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/IR/Instruction.h"
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #define DEBUG_TYPE "instcombine"
  18. namespace llvm {
  19. /// InstCombineWorklist - This is the worklist management logic for
  20. /// InstCombine.
  21. class InstCombineWorklist {
  22. SmallVector<Instruction*, 256> Worklist;
  23. DenseMap<Instruction*, unsigned> WorklistMap;
  24. void operator=(const InstCombineWorklist&RHS) = delete;
  25. InstCombineWorklist(const InstCombineWorklist&) = delete;
  26. public:
  27. InstCombineWorklist() {}
  28. InstCombineWorklist(InstCombineWorklist &&Arg)
  29. : Worklist(std::move(Arg.Worklist)),
  30. WorklistMap(std::move(Arg.WorklistMap)) {}
  31. InstCombineWorklist &operator=(InstCombineWorklist &&RHS) {
  32. Worklist = std::move(RHS.Worklist);
  33. WorklistMap = std::move(RHS.WorklistMap);
  34. return *this;
  35. }
  36. bool isEmpty() const { return Worklist.empty(); }
  37. /// Add - Add the specified instruction to the worklist if it isn't already
  38. /// in it.
  39. void Add(Instruction *I) {
  40. if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
  41. DEBUG(dbgs() << "IC: ADD: " << *I << '\n');
  42. Worklist.push_back(I);
  43. }
  44. }
  45. void AddValue(Value *V) {
  46. if (Instruction *I = dyn_cast<Instruction>(V))
  47. Add(I);
  48. }
  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);
  61. }
  62. }
  63. // Remove - remove I from the worklist if it exists.
  64. void Remove(Instruction *I) {
  65. DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
  66. if (It == WorklistMap.end()) return; // Not in worklist.
  67. // Don't bother moving everything down, just null out the slot.
  68. Worklist[It->second] = nullptr;
  69. WorklistMap.erase(It);
  70. }
  71. Instruction *RemoveOne() {
  72. Instruction *I = Worklist.pop_back_val();
  73. WorklistMap.erase(I);
  74. return I;
  75. }
  76. /// AddUsersToWorkList - When an instruction is simplified, add all users of
  77. /// the instruction to the work lists because they might get more simplified
  78. /// now.
  79. ///
  80. void AddUsersToWorkList(Instruction &I) {
  81. for (User *U : I.users())
  82. Add(cast<Instruction>(U));
  83. }
  84. /// Zap - check that the worklist is empty and nuke the backing store for
  85. /// the map if it is large.
  86. void Zap() {
  87. assert(WorklistMap.empty() && "Worklist empty, but map not?");
  88. // Do an explicit clear, this shrinks the map if needed.
  89. WorklistMap.clear();
  90. }
  91. };
  92. } // end namespace llvm.
  93. #undef DEBUG_TYPE
  94. #endif