Home | History | Annotate | Download | only in InstCombine
      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/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"
     19 
     20 #define DEBUG_TYPE "instcombine"
     21 
     22 namespace llvm {
     23 
     24 /// InstCombineWorklist - This is the worklist management logic for
     25 /// InstCombine.
     26 class InstCombineWorklist {
     27   SmallVector<Instruction*, 256> Worklist;
     28   DenseMap<Instruction*, unsigned> WorklistMap;
     29 
     30   void operator=(const InstCombineWorklist&RHS) = delete;
     31   InstCombineWorklist(const InstCombineWorklist&) = delete;
     32 public:
     33   InstCombineWorklist() {}
     34 
     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);
     41     return *this;
     42   }
     43 
     44   bool isEmpty() const { return Worklist.empty(); }
     45 
     46   /// Add - Add the specified instruction to the worklist if it isn't already
     47   /// in it.
     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);
     52     }
     53   }
     54 
     55   void AddValue(Value *V) {
     56     if (Instruction *I = dyn_cast<Instruction>(V))
     57       Add(I);
     58   }
     59 
     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);
     72     }
     73   }
     74 
     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.
     79 
     80     // Don't bother moving everything down, just null out the slot.
     81     Worklist[It->second] = nullptr;
     82 
     83     WorklistMap.erase(It);
     84   }
     85 
     86   Instruction *RemoveOne() {
     87     Instruction *I = Worklist.pop_back_val();
     88     WorklistMap.erase(I);
     89     return I;
     90   }
     91 
     92   /// AddUsersToWorkList - When an instruction is simplified, add all users of
     93   /// the instruction to the work lists because they might get more simplified
     94   /// now.
     95   ///
     96   void AddUsersToWorkList(Instruction &I) {
     97     for (User *U : I.users())
     98       Add(cast<Instruction>(U));
     99   }
    100 
    101 
    102   /// Zap - check that the worklist is empty and nuke the backing store for
    103   /// the map if it is large.
    104   void Zap() {
    105     assert(WorklistMap.empty() && "Worklist empty, but map not?");
    106 
    107     // Do an explicit clear, this shrinks the map if needed.
    108     WorklistMap.clear();
    109   }
    110 };
    111 
    112 } // end namespace llvm.
    113 
    114 #undef DEBUG_TYPE
    115 
    116 #endif
    117