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
This commit is contained in:
Cameron Zwarich 2011-01-04 00:12:46 +00:00
parent 7c90b90f4e
commit a1cb585384

View File

@ -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<DominatorTree>();
AU.addPreserved<DominatorTree>();
AU.addRequired<LoopInfo>();
AU.addPreserved<LoopInfo>();
AU.addPreservedID(LCSSAID);
@ -57,9 +57,9 @@ Pass* llvm::createLoopInstSimplifyPass() {
return new LoopInstSimplify();
}
bool LoopInstSimplify::runOnLoop(Loop* L, LPPassManager& LPM) {
DominatorTree* DT = &getAnalysis<DominatorTree>();
const LoopInfo* LI = &getAnalysis<LoopInfo>();
bool LoopInstSimplify::runOnFunction(Function& F) {
DominatorTree* DT = getAnalysisIfAvailable<DominatorTree>();
LoopInfo* LI = &getAnalysis<LoopInfo>();
const TargetData* TD = getAnalysisIfAvailable<TargetData>();
bool Changed = false;
@ -67,24 +67,14 @@ bool LoopInstSimplify::runOnLoop(Loop* L, LPPassManager& LPM) {
do {
LocalChanged = false;
SmallPtrSet<BasicBlock*, 32> Visited;
SmallVector<BasicBlock*, 32> 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<BasicBlock*> 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<DomTreeNode*>& 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;
}