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