summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorEvan Cheng <evan.cheng@apple.com>2008-06-11 19:07:54 +0000
committerEvan Cheng <evan.cheng@apple.com>2008-06-11 19:07:54 +0000
commitf3c2902ead028a1afbf460387c3e5cdcfca6ab92 (patch)
tree1056c070ead7185e922e592a291fdd0eb8a2fd18 /llvm/lib/Transforms
parent3715385cf03fb010f5ea1630265619e70ea4921e (diff)
downloadbcm5719-llvm-f3c2902ead028a1afbf460387c3e5cdcfca6ab92.tar.gz
bcm5719-llvm-f3c2902ead028a1afbf460387c3e5cdcfca6ab92.zip
Avoid duplicating loop header which leads to unnatural loops (and just seem like general badness to me, likely to cause code explosion).
Patch by Florian Brandner. llvm-svn: 52223
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/Scalar/TailDuplication.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Scalar/TailDuplication.cpp b/llvm/lib/Transforms/Scalar/TailDuplication.cpp
index d0998ab5a65..4e0949a2b47 100644
--- a/llvm/lib/Transforms/Scalar/TailDuplication.cpp
+++ b/llvm/lib/Transforms/Scalar/TailDuplication.cpp
@@ -33,6 +33,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/Analysis/LoopInfo.h"
#include <map>
using namespace llvm;
@@ -50,10 +51,12 @@ namespace {
static char ID; // Pass identification, replacement for typeid
TailDup() : FunctionPass((intptr_t)&ID) {}
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const;
private:
inline bool shouldEliminateUnconditionalBranch(TerminatorInst *, unsigned);
inline void eliminateUnconditionalBranch(BranchInst *BI);
SmallPtrSet<BasicBlock*, 4> CycleDetector;
+ LoopInfo *LI; // The current loop information
};
}
@@ -69,6 +72,9 @@ FunctionPass *llvm::createTailDuplicationPass() { return new TailDup(); }
/// a place it already pointed to earlier; see PR 2323.
bool TailDup::runOnFunction(Function &F) {
bool Changed = false;
+
+ LI = &getAnalysis<LoopInfo>();
+
CycleDetector.clear();
for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
if (shouldEliminateUnconditionalBranch(I->getTerminator(),
@@ -83,6 +89,10 @@ bool TailDup::runOnFunction(Function &F) {
return Changed;
}
+void TailDup::getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.addRequired<LoopInfo>();
+}
+
/// shouldEliminateUnconditionalBranch - Return true if this branch looks
/// attractive to eliminate. We eliminate the branch if the destination basic
/// block has <= 5 instructions in it, not counting PHI nodes. In practice,
@@ -186,6 +196,14 @@ bool TailDup::shouldEliminateUnconditionalBranch(TerminatorInst *TI,
if (!CycleDetector.insert(Dest))
return false;
+ // Avoid non-natural loops:
+ // If a loop header is duplicated, the former natural loop will contain two
+ // paths into the loop --> the loop it not natural anymore. We want to avoid
+ // this, because other optimizaions may fail to improve the loop because of
+ // this.
+ if (LI->isLoopHeader(Dest))
+ return false;
+
return true;
}
OpenPOWER on IntegriCloud