LLVM API Documentation
00001 //===- InstCombineWorklist.h - Worklist for the InstCombine pass ----------===// 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(errs() << "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(errs() << "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.back(); 00078 Worklist.pop_back(); 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 (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); 00089 UI != UE; ++UI) 00090 Add(cast<Instruction>(*UI)); 00091 } 00092 00093 00094 /// Zap - check that the worklist is empty and nuke the backing store for 00095 /// the map if it is large. 00096 void Zap() { 00097 assert(WorklistMap.empty() && "Worklist empty, but map not?"); 00098 00099 // Do an explicit clear, this shrinks the map if needed. 00100 WorklistMap.clear(); 00101 } 00102 }; 00103 00104 } // end namespace llvm. 00105 00106 #endif