From 62c50fdf69064c0d086aedfadb6ceb7c303bbcb9 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 2 Jan 2011 19:01:03 +0000 Subject: [PATCH] enhance loop idiom recognition to scan *all* unconditionally executed blocks in a loop, instead of just the header block. This makes it more aggressive, able to handle Duncan's Ada examples. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122704 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopIdiomRecognize.cpp | 47 ++++++++++++++++---- test/Transforms/LoopIdiom/basic.ll | 23 ++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/lib/Transforms/Scalar/LoopIdiomRecognize.cpp index 72fee7012e1..8b022bab83a 100644 --- a/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ b/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -58,6 +58,7 @@ namespace { class LoopIdiomRecognize : public LoopPass { Loop *CurLoop; const TargetData *TD; + DominatorTree *DT; ScalarEvolution *SE; public: static char ID; @@ -66,6 +67,8 @@ namespace { } bool runOnLoop(Loop *L, LPPassManager &LPM); + bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount, + SmallVectorImpl &ExitBlocks); bool processLoopStore(StoreInst *SI, const SCEV *BECount); @@ -93,6 +96,7 @@ namespace { AU.addRequired(); AU.addPreserved(); AU.addPreserved(); + AU.addRequired(); } }; } @@ -101,6 +105,7 @@ char LoopIdiomRecognize::ID = 0; INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms", false, false) INITIALIZE_PASS_DEPENDENCY(LoopInfo) +INITIALIZE_PASS_DEPENDENCY(DominatorTree) INITIALIZE_PASS_DEPENDENCY(LoopSimplify) INITIALIZE_PASS_DEPENDENCY(LCSSA) INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) @@ -157,16 +162,41 @@ bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) { // We require target data for now. TD = getAnalysisIfAvailable(); if (TD == 0) return false; + + DT = &getAnalysis(); + LoopInfo &LI = getAnalysis(); - // TODO: We currently only scan the header of the loop, because it is the only - // part that is known to execute and we don't want to make a conditional store - // into an unconditional one in the preheader. However, there can be diamonds - // and other things in the loop that would make other blocks "always executed" - // we should get the full set and scan each block. - BasicBlock *BB = L->getHeader(); - DEBUG(dbgs() << "loop-idiom Scanning: F[" << BB->getParent()->getName() - << "] Loop %" << BB->getName() << "\n"); + SmallVector ExitBlocks; + CurLoop->getUniqueExitBlocks(ExitBlocks); + bool MadeChange = false; + // Scan all the blocks in the loop that are not in subloops. + for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E; + ++BI) { + // Ignore blocks in subloops. + if (LI.getLoopFor(*BI) != CurLoop) + continue; + + MadeChange |= runOnLoopBlock(*BI, BECount, ExitBlocks); + } + return MadeChange; +} + +/// runOnLoopBlock - Process the specified block, which lives in a counted loop +/// with the specified backedge count. This block is known to be in the current +/// loop and not in any subloops. +bool LoopIdiomRecognize::runOnLoopBlock(BasicBlock *BB, const SCEV *BECount, + SmallVectorImpl &ExitBlocks) { + // We can only promote stores in this block if they are unconditionally + // executed in the loop. For a block to be unconditionally executed, it has + // to dominate all the exit blocks of the loop. Verify this now. + for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) + if (!DT->dominates(BB, ExitBlocks[i])) + return false; + + DEBUG(dbgs() << "loop-idiom Scanning: F[" << BB->getParent()->getName() + << "] Loop %" << BB->getName() << "\n"); + bool MadeChange = false; for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { // Look for store instructions, which may be memsets. @@ -187,6 +217,7 @@ bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) { return MadeChange; } + /// scanBlock - Look over a block to see if we can promote anything out of it. bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) { Value *StoredVal = SI->getValueOperand(); diff --git a/test/Transforms/LoopIdiom/basic.ll b/test/Transforms/LoopIdiom/basic.ll index 589fea4f3a3..3fa6eb900b4 100644 --- a/test/Transforms/LoopIdiom/basic.ll +++ b/test/Transforms/LoopIdiom/basic.ll @@ -165,4 +165,27 @@ for.end: ; preds = %for.body, %entry } +; This is a loop that was rotated but where the blocks weren't merged. This +; shouldn't perturb us. +define void @test7(i8* %Base, i64 %Size) nounwind ssp { +bb.nph: ; preds = %entry + br label %for.body + +for.body: ; preds = %bb.nph, %for.body + %indvar = phi i64 [ 0, %bb.nph ], [ %indvar.next, %for.body.cont ] + br label %for.body.cont +for.body.cont: + %I.0.014 = getelementptr i8* %Base, i64 %indvar + store i8 0, i8* %I.0.014, align 1 + %indvar.next = add i64 %indvar, 1 + %exitcond = icmp eq i64 %indvar.next, %Size + br i1 %exitcond, label %for.end, label %for.body + +for.end: ; preds = %for.body, %entry + ret void +; CHECK: @test7 +; CHECK: call void @llvm.memset.p0i8.i64(i8* %Base, i8 0, i64 %Size, i32 1, i1 false) +; CHECK-NOT: store +} + -- 2.34.1