From: Chris Lattner Date: Sun, 30 Aug 2009 05:55:36 +0000 (+0000) Subject: refactor instcombine's worklist processing stuff out to its own class. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=873ff0158e266feb2dec43adfa7d484272cd691a;p=oota-llvm.git refactor instcombine's worklist processing stuff out to its own class. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80481 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 9358837d123..3527eee2c74 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -74,30 +74,27 @@ STATISTIC(NumDeadStore, "Number of dead stores eliminated"); STATISTIC(NumSunkInst , "Number of instructions sunk"); namespace { - class VISIBILITY_HIDDEN InstCombiner - : public FunctionPass, - public InstVisitor { - // Worklist of all of the instructions that need to be simplified. + /// InstCombineWorklist - This is the worklist management logic for + /// InstCombine. + class InstCombineWorklist { SmallVector Worklist; DenseMap WorklistMap; - TargetData *TD; - bool MustPreserveLCSSA; + + void operator=(const InstCombineWorklist&RHS); // DO NOT IMPLEMENT + InstCombineWorklist(const InstCombineWorklist&); // DO NOT IMPLEMENT public: - static char ID; // Pass identification, replacement for typeid - InstCombiner() : FunctionPass(&ID) {} - - LLVMContext *Context; - LLVMContext *getContext() const { return Context; } - - /// AddToWorkList - Add the specified instruction to the worklist if it - /// isn't already in it. - void AddToWorkList(Instruction *I) { + InstCombineWorklist() {} + + bool isEmpty() const { return Worklist.empty(); } + + /// Add - Add the specified instruction to the worklist if it isn't already + /// in it. + void Add(Instruction *I) { if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) Worklist.push_back(I); } - // RemoveFromWorkList - remove I from the worklist if it exists. - void RemoveFromWorkList(Instruction *I) { + void Remove(Instruction *I) { DenseMap::iterator It = WorklistMap.find(I); if (It == WorklistMap.end()) return; // Not in worklist. @@ -107,7 +104,7 @@ namespace { WorklistMap.erase(It); } - Instruction *RemoveOneFromWorkList() { + Instruction *RemoveOne() { Instruction *I = Worklist.back(); Worklist.pop_back(); WorklistMap.erase(I); @@ -115,6 +112,44 @@ namespace { } + /// Zap - check that the worklist is empty and nuke the backing store for + /// the map if it is large. + void Zap() { + assert(WorklistMap.empty() && "Worklist empty, but map not?"); + + // Do an explicit clear, this shrinks the map if needed. + WorklistMap.clear(); + } + }; +} // end anonymous namespace. + + +namespace { + class VISIBILITY_HIDDEN InstCombiner + : public FunctionPass, + public InstVisitor { + // Worklist of all of the instructions that need to be simplified. + InstCombineWorklist Worklist; + TargetData *TD; + bool MustPreserveLCSSA; + public: + static char ID; // Pass identification, replacement for typeid + InstCombiner() : FunctionPass(&ID) {} + + LLVMContext *Context; + LLVMContext *getContext() const { return Context; } + + /// AddToWorkList - Add the specified instruction to the worklist if it + /// isn't already in it. + void AddToWorkList(Instruction *I) { + Worklist.Add(I); + } + + // RemoveFromWorkList - remove I from the worklist if it exists. + void RemoveFromWorkList(Instruction *I) { + Worklist.Remove(I); + } + /// AddUsersToWorkList - When an instruction is simplified, add all users of /// the instruction to the work lists because they might get more simplified /// now. @@ -404,7 +439,7 @@ namespace { unsigned PrefAlign = 0); }; -} +} // end anonymous namespace char InstCombiner::ID = 0; static RegisterPass @@ -11227,7 +11262,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { } /// See if we can simplify: - /// X = bitcast A to B* + /// X = bitcast A* to B* /// Y = gep X, <...constant indices...> /// into a gep of the original struct. This is important for SROA and alias /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged. @@ -12925,8 +12960,8 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { } } - while (!Worklist.empty()) { - Instruction *I = RemoveOneFromWorkList(); + while (!Worklist.isEmpty()) { + Instruction *I = Worklist.RemoveOne(); if (I == 0) continue; // skip null values. // Check to see if we can DCE the instruction. @@ -13062,10 +13097,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { } } - assert(WorklistMap.empty() && "Worklist empty, but map not?"); - - // Do an explicit clear, this shrinks the map if needed. - WorklistMap.clear(); + Worklist.Zap(); return Changed; }