LLVM API Documentation

InstCombineWorklist.h
Go to the documentation of this file.
00001 //===- InstCombineWorklist.h - Worklist for InstCombine pass ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef INSTCOMBINE_WORKLIST_H
00011 #define INSTCOMBINE_WORKLIST_H
00012 
00013 #define DEBUG_TYPE "instcombine"
00014 #include "llvm/ADT/DenseMap.h"
00015 #include "llvm/ADT/SmallVector.h"
00016 #include "llvm/IR/Instruction.h"
00017 #include "llvm/Support/Compiler.h"
00018 #include "llvm/Support/Debug.h"
00019 #include "llvm/Support/raw_ostream.h"
00020 
00021 namespace llvm {
00022 
00023 /// InstCombineWorklist - This is the worklist management logic for
00024 /// InstCombine.
00025 class LLVM_LIBRARY_VISIBILITY InstCombineWorklist {
00026   SmallVector<Instruction*, 256> Worklist;
00027   DenseMap<Instruction*, unsigned> WorklistMap;
00028 
00029   void operator=(const InstCombineWorklist&RHS) LLVM_DELETED_FUNCTION;
00030   InstCombineWorklist(const InstCombineWorklist&) LLVM_DELETED_FUNCTION;
00031 public:
00032   InstCombineWorklist() {}
00033 
00034   bool isEmpty() const { return Worklist.empty(); }
00035 
00036   /// Add - Add the specified instruction to the worklist if it isn't already
00037   /// in it.
00038   void Add(Instruction *I) {
00039     if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
00040       DEBUG(dbgs() << "IC: ADD: " << *I << '\n');
00041       Worklist.push_back(I);
00042     }
00043   }
00044 
00045   void AddValue(Value *V) {
00046     if (Instruction *I = dyn_cast<Instruction>(V))
00047       Add(I);
00048   }
00049 
00050   /// AddInitialGroup - Add the specified batch of stuff in reverse order.
00051   /// which should only be done when the worklist is empty and when the group
00052   /// has no duplicates.
00053   void AddInitialGroup(Instruction *const *List, unsigned NumEntries) {
00054     assert(Worklist.empty() && "Worklist must be empty to add initial group");
00055     Worklist.reserve(NumEntries+16);
00056     WorklistMap.resize(NumEntries);
00057     DEBUG(dbgs() << "IC: ADDING: " << NumEntries << " instrs to worklist\n");
00058     for (unsigned Idx = 0; NumEntries; --NumEntries) {
00059       Instruction *I = List[NumEntries-1];
00060       WorklistMap.insert(std::make_pair(I, Idx++));
00061       Worklist.push_back(I);
00062     }
00063   }
00064 
00065   // Remove - remove I from the worklist if it exists.
00066   void Remove(Instruction *I) {
00067     DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
00068     if (It == WorklistMap.end()) return; // Not in worklist.
00069 
00070     // Don't bother moving everything down, just null out the slot.
00071     Worklist[It->second] = 0;
00072 
00073     WorklistMap.erase(It);
00074   }
00075 
00076   Instruction *RemoveOne() {
00077     Instruction *I = Worklist.pop_back_val();
00078     WorklistMap.erase(I);
00079     return I;
00080   }
00081 
00082   /// AddUsersToWorkList - When an instruction is simplified, add all users of
00083   /// the instruction to the work lists because they might get more simplified
00084   /// now.
00085   ///
00086   void AddUsersToWorkList(Instruction &I) {
00087     for (User *U : I.users())
00088       Add(cast<Instruction>(U));
00089   }
00090 
00091 
00092   /// Zap - check that the worklist is empty and nuke the backing store for
00093   /// the map if it is large.
00094   void Zap() {
00095     assert(WorklistMap.empty() && "Worklist empty, but map not?");
00096 
00097     // Do an explicit clear, this shrinks the map if needed.
00098     WorklistMap.clear();
00099   }
00100 };
00101 
00102 } // end namespace llvm.
00103 
00104 #endif