From: Cameron Zwarich Date: Tue, 4 Jan 2011 00:12:46 +0000 (+0000) Subject: Address most of Duncan's review comments. Also, make LoopInstSimplify a simple X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a1cb585384f593517a9b8f48693f5e478b833fba;p=oota-llvm.git Address most of Duncan's review comments. Also, make LoopInstSimplify a simple FunctionPass. It probably doesn't have a reason to be a LoopPass, as it will probably drop the simple fixed point and either use RPO iteration or Duncan's approach in instsimplify of only revisiting instructions that have changed. The next step is to preserve LoopSimplify. This looks like it won't be too hard, although the pass manager doesn't actually seem to respect when non-loop passes claim to preserve LCSSA or LoopSimplify. This will have to be fixed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122791 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/LoopInstSimplify.cpp b/lib/Transforms/Scalar/LoopInstSimplify.cpp index 0400288031b..301e600fb9d 100644 --- a/lib/Transforms/Scalar/LoopInstSimplify.cpp +++ b/lib/Transforms/Scalar/LoopInstSimplify.cpp @@ -12,9 +12,11 @@ //===----------------------------------------------------------------------===// #define DEBUG_TYPE "loop-instsimplify" -#include "llvm/Analysis/LoopPass.h" +#include "llvm/Function.h" +#include "llvm/Pass.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/InstructionSimplify.h" +#include "llvm/Analysis/LoopInfo.h" #include "llvm/Target/TargetData.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/Local.h" @@ -24,19 +26,17 @@ using namespace llvm; STATISTIC(NumSimplified, "Number of redundant instructions simplified"); namespace { - class LoopInstSimplify : public LoopPass { + class LoopInstSimplify : public FunctionPass { public: static char ID; // Pass ID, replacement for typeid - LoopInstSimplify() : LoopPass(ID) { + LoopInstSimplify() : FunctionPass(ID) { initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry()); } - bool runOnLoop(Loop*, LPPassManager&); + bool runOnFunction(Function&); virtual void getAnalysisUsage(AnalysisUsage& AU) const { AU.setPreservesCFG(); - AU.addRequired(); - AU.addPreserved(); AU.addRequired(); AU.addPreserved(); AU.addPreservedID(LCSSAID); @@ -57,9 +57,9 @@ Pass* llvm::createLoopInstSimplifyPass() { return new LoopInstSimplify(); } -bool LoopInstSimplify::runOnLoop(Loop* L, LPPassManager& LPM) { - DominatorTree* DT = &getAnalysis(); - const LoopInfo* LI = &getAnalysis(); +bool LoopInstSimplify::runOnFunction(Function& F) { + DominatorTree* DT = getAnalysisIfAvailable(); + LoopInfo* LI = &getAnalysis(); const TargetData* TD = getAnalysisIfAvailable(); bool Changed = false; @@ -67,24 +67,14 @@ bool LoopInstSimplify::runOnLoop(Loop* L, LPPassManager& LPM) { do { LocalChanged = false; - SmallPtrSet Visited; - SmallVector VisitStack; - - VisitStack.push_back(L->getHeader()); - - while (!VisitStack.empty()) { - BasicBlock* BB = VisitStack.back(); - VisitStack.pop_back(); - - if (Visited.count(BB)) - continue; - Visited.insert(BB); - - for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { + for (df_iterator DI = df_begin(&F.getEntryBlock()), + DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) + for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) { Instruction* I = BI++; // Don't bother simplifying unused instructions. if (!I->use_empty()) { - if (Value* V = SimplifyInstruction(I, TD, DT)) { + Value* V = SimplifyInstruction(I, TD, DT); + if (V && LI->replacementPreservesLCSSAForm(I, V)) { I->replaceAllUsesWith(V); LocalChanged = true; ++NumSimplified; @@ -92,21 +82,9 @@ bool LoopInstSimplify::runOnLoop(Loop* L, LPPassManager& LPM) { } LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I); } - Changed |= LocalChanged; - DomTreeNode* Node = DT->getNode(BB); - const std::vector& Children = Node->getChildren(); - for (unsigned i = 0; i < Children.size(); ++i) { - // Only visit children that are in the same loop. - BasicBlock* ChildBB = Children[i]->getBlock(); - if (!Visited.count(ChildBB) && LI->getLoopFor(ChildBB) == L) - VisitStack.push_back(ChildBB); - } - } + Changed |= LocalChanged; } while (LocalChanged); - // Nothing that SimplifyInstruction() does should invalidate LCSSA form. - assert(L->isLCSSAForm(*DT)); - return Changed; }