2004-04-18 05:20:17 +00:00
|
|
|
//===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-04-18 05:20:17 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-04-18 05:20:17 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass implements a simple loop unroller. It works best when loops have
|
|
|
|
// been canonicalized by the -indvars pass, allowing it to determine the trip
|
|
|
|
// counts of loops easily.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "loop-unroll"
|
2008-05-14 00:24:14 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2004-04-18 05:20:17 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2007-03-07 01:38:05 +00:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-05-14 00:24:14 +00:00
|
|
|
#include "llvm/Transforms/Utils/UnrollLoop.h"
|
2008-05-16 09:30:00 +00:00
|
|
|
#include <climits>
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2008-05-14 00:24:14 +00:00
|
|
|
using namespace llvm;
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
UnrollThreshold("unroll-threshold", cl::init(100), cl::Hidden,
|
|
|
|
cl::desc("The cut-off point for automatic loop unrolling"));
|
2007-05-11 20:53:41 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
UnrollCount("unroll-count", cl::init(0), cl::Hidden,
|
|
|
|
cl::desc("Use this unroll count for all loops, for testing purposes"));
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2008-07-29 13:21:23 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Allows loops to be partially unrolled until "
|
|
|
|
"-unroll-threshold loop size is reached."));
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2009-09-02 06:11:42 +00:00
|
|
|
class LoopUnroll : public LoopPass {
|
2004-04-18 05:20:17 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
LoopUnroll() : LoopPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2007-05-11 20:53:41 +00:00
|
|
|
/// A magic value for use with the Threshold parameter to indicate
|
|
|
|
/// that the loop unroll should be performed regardless of how much
|
|
|
|
/// code expansion would result.
|
|
|
|
static const unsigned NoThreshold = UINT_MAX;
|
|
|
|
|
2007-03-07 01:38:05 +00:00
|
|
|
bool runOnLoop(Loop *L, LPPassManager &LPM);
|
2004-04-18 05:20:17 +00:00
|
|
|
|
|
|
|
/// This transformation requires natural loop information & requires that
|
|
|
|
/// loop preheaders be inserted into the CFG...
|
|
|
|
///
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
2006-08-24 21:28:19 +00:00
|
|
|
AU.addRequiredID(LCSSAID);
|
2004-04-18 05:20:17 +00:00
|
|
|
AU.addRequired<LoopInfo>();
|
2006-08-24 21:28:19 +00:00
|
|
|
AU.addPreservedID(LCSSAID);
|
2004-04-18 05:38:37 +00:00
|
|
|
AU.addPreserved<LoopInfo>();
|
2008-07-03 07:04:22 +00:00
|
|
|
// FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info.
|
|
|
|
// If loop unroll does not preserve dom info then LCSSA pass on next
|
|
|
|
// loop will receive invalid dom info.
|
|
|
|
// For now, recreate dom info, if loop is unrolled.
|
|
|
|
AU.addPreserved<DominatorTree>();
|
|
|
|
AU.addPreserved<DominanceFrontier>();
|
2004-04-18 05:20:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char LoopUnroll::ID = 0;
|
|
|
|
static RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops");
|
|
|
|
|
2008-10-22 23:32:42 +00:00
|
|
|
Pass *llvm::createLoopUnrollPass() { return new LoopUnroll(); }
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2007-05-08 15:14:19 +00:00
|
|
|
/// ApproximateLoopSize - Approximate the size of the loop.
|
2004-04-18 05:20:17 +00:00
|
|
|
static unsigned ApproximateLoopSize(const Loop *L) {
|
|
|
|
unsigned Size = 0;
|
2008-06-22 20:18:58 +00:00
|
|
|
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
2004-04-18 05:20:17 +00:00
|
|
|
Instruction *Term = BB->getTerminator();
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
|
|
|
|
if (isa<PHINode>(I) && BB == L->getHeader()) {
|
|
|
|
// Ignore PHI nodes in the header.
|
|
|
|
} else if (I->hasOneUse() && I->use_back() == Term) {
|
|
|
|
// Ignore instructions only used by the loop terminator.
|
2006-11-02 20:25:50 +00:00
|
|
|
} else if (isa<DbgInfoIntrinsic>(I)) {
|
2005-04-23 21:38:35 +00:00
|
|
|
// Ignore debug instructions
|
2009-04-20 22:19:33 +00:00
|
|
|
} else if (isa<GetElementPtrInst>(I) && I->hasOneUse()) {
|
|
|
|
// Ignore GEP as they generally are subsumed into a load or store.
|
2008-03-17 23:41:20 +00:00
|
|
|
} else if (isa<CallInst>(I)) {
|
2008-03-19 23:05:52 +00:00
|
|
|
// Estimate size overhead introduced by call instructions which
|
|
|
|
// is higher than other instructions. Here 3 and 10 are magic
|
|
|
|
// numbers that help one isolated test case from PR2067 without
|
|
|
|
// negatively impacting measured benchmarks.
|
2009-08-13 03:05:20 +00:00
|
|
|
Size += isa<IntrinsicInst>(I) ? 3 : 10;
|
2004-04-18 05:20:17 +00:00
|
|
|
} else {
|
|
|
|
++Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Ignore expressions derived from PHI and constants if inval of phi
|
|
|
|
// is a constant, or if operation is associative. This will get induction
|
|
|
|
// variables.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2007-03-07 01:38:05 +00:00
|
|
|
bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
|
2007-05-11 20:53:41 +00:00
|
|
|
assert(L->isLCSSAForm());
|
2008-05-14 00:24:14 +00:00
|
|
|
LoopInfo *LI = &getAnalysis<LoopInfo>();
|
2007-05-11 20:53:41 +00:00
|
|
|
|
2007-05-08 15:19:19 +00:00
|
|
|
BasicBlock *Header = L->getHeader();
|
2009-07-25 00:23:56 +00:00
|
|
|
DEBUG(errs() << "Loop Unroll: F[" << Header->getParent()->getName()
|
|
|
|
<< "] Loop %" << Header->getName() << "\n");
|
2009-07-27 23:14:11 +00:00
|
|
|
(void)Header;
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2008-05-14 00:24:14 +00:00
|
|
|
// Find trip count
|
|
|
|
unsigned TripCount = L->getSmallConstantTripCount();
|
|
|
|
unsigned Count = UnrollCount;
|
2009-08-13 02:40:50 +00:00
|
|
|
|
2007-05-11 20:53:41 +00:00
|
|
|
// Automatically select an unroll count.
|
|
|
|
if (Count == 0) {
|
|
|
|
// Conservative heuristic: if we know the trip count, see if we can
|
|
|
|
// completely unroll (subject to the threshold, checked below); otherwise
|
2009-08-13 02:40:50 +00:00
|
|
|
// try to find greatest modulo of the trip count which is still under
|
2008-07-29 13:21:23 +00:00
|
|
|
// threshold value.
|
2009-08-13 03:00:57 +00:00
|
|
|
if (TripCount == 0)
|
2007-05-11 20:53:41 +00:00
|
|
|
return false;
|
2009-08-13 03:00:57 +00:00
|
|
|
Count = TripCount;
|
2007-05-11 20:53:41 +00:00
|
|
|
}
|
2007-03-02 23:31:34 +00:00
|
|
|
|
2007-05-11 20:53:41 +00:00
|
|
|
// Enforce the threshold.
|
2008-05-14 00:24:14 +00:00
|
|
|
if (UnrollThreshold != NoThreshold) {
|
2007-05-11 20:53:41 +00:00
|
|
|
unsigned LoopSize = ApproximateLoopSize(L);
|
2009-08-13 02:45:03 +00:00
|
|
|
DEBUG(errs() << " Loop Size = " << LoopSize << "\n");
|
2007-05-11 20:53:41 +00:00
|
|
|
uint64_t Size = (uint64_t)LoopSize*Count;
|
2008-05-14 00:24:14 +00:00
|
|
|
if (TripCount != 1 && Size > UnrollThreshold) {
|
2009-08-13 02:45:03 +00:00
|
|
|
DEBUG(errs() << " Too large to fully unroll with count: " << Count
|
|
|
|
<< " because size: " << Size << ">" << UnrollThreshold << "\n");
|
2009-08-13 03:00:57 +00:00
|
|
|
if (!UnrollAllowPartial) {
|
2009-08-13 02:45:03 +00:00
|
|
|
DEBUG(errs() << " will not try to unroll partially because "
|
|
|
|
<< "-unroll-allow-partial not given\n");
|
2008-07-29 13:21:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-13 03:00:57 +00:00
|
|
|
// Reduce unroll count to be modulo of TripCount for partial unrolling
|
|
|
|
Count = UnrollThreshold / LoopSize;
|
|
|
|
while (Count != 0 && TripCount%Count != 0) {
|
|
|
|
Count--;
|
|
|
|
}
|
|
|
|
if (Count < 2) {
|
|
|
|
DEBUG(errs() << " could not unroll partially\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DEBUG(errs() << " partially unrolling with count: " << Count << "\n");
|
2007-05-11 20:53:41 +00:00
|
|
|
}
|
2004-04-18 05:20:17 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2008-05-14 00:24:14 +00:00
|
|
|
// Unroll the loop.
|
2008-07-03 07:04:22 +00:00
|
|
|
Function *F = L->getHeader()->getParent();
|
2008-05-14 00:24:14 +00:00
|
|
|
if (!UnrollLoop(L, Count, LI, &LPM))
|
|
|
|
return false;
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2008-07-03 07:04:22 +00:00
|
|
|
// FIXME: Reconstruct dom info, because it is not preserved properly.
|
2009-01-28 13:14:17 +00:00
|
|
|
DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
|
2008-07-03 07:04:22 +00:00
|
|
|
if (DT) {
|
|
|
|
DT->runOnFunction(*F);
|
2009-01-28 13:14:17 +00:00
|
|
|
DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>();
|
2008-07-03 07:04:22 +00:00
|
|
|
if (DF)
|
|
|
|
DF->runOnFunction(*F);
|
|
|
|
}
|
2004-04-18 05:20:17 +00:00
|
|
|
return true;
|
|
|
|
}
|