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"
|
2007-03-07 01:38:05 +00:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2011-01-02 07:35:53 +00:00
|
|
|
#include "llvm/Analysis/CodeMetrics.h"
|
2010-07-26 18:11:16 +00:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.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>
|
2010-09-10 17:57:00 +00:00
|
|
|
UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden,
|
2008-05-13 00:00:25 +00:00
|
|
|
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
|
2011-04-13 16:15:29 +00:00
|
|
|
LoopUnroll(int T = -1, int C = -1, int P = -1) : LoopPass(ID) {
|
2011-04-14 02:27:25 +00:00
|
|
|
CurrentThreshold = (T == -1) ? UnrollThreshold : unsigned(T);
|
|
|
|
CurrentCount = (C == -1) ? UnrollCount : unsigned(C);
|
2011-04-13 16:15:29 +00:00
|
|
|
CurrentAllowPartial = (P == -1) ? UnrollAllowPartial : (bool)P;
|
|
|
|
|
|
|
|
UserThreshold = (T != -1) || (UnrollThreshold.getNumOccurrences() > 0);
|
|
|
|
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeLoopUnrollPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
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;
|
2010-09-07 23:15:30 +00:00
|
|
|
|
|
|
|
// Threshold to use when optsize is specified (and there is no
|
|
|
|
// explicit -unroll-threshold).
|
|
|
|
static const unsigned OptSizeUnrollThreshold = 50;
|
|
|
|
|
2011-04-13 16:15:29 +00:00
|
|
|
unsigned CurrentCount;
|
2010-09-07 23:15:30 +00:00
|
|
|
unsigned CurrentThreshold;
|
2011-04-13 16:15:29 +00:00
|
|
|
bool CurrentAllowPartial;
|
|
|
|
bool UserThreshold; // CurrentThreshold is user-specified.
|
2007-05-11 20:53:41 +00:00
|
|
|
|
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 {
|
2010-07-26 18:11:16 +00:00
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addPreserved<LoopInfo>();
|
2004-04-18 05:20:17 +00:00
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
2010-07-26 18:11:16 +00:00
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2006-08-24 21:28:19 +00:00
|
|
|
AU.addRequiredID(LCSSAID);
|
|
|
|
AU.addPreservedID(LCSSAID);
|
2010-08-29 17:21:35 +00:00
|
|
|
AU.addPreserved<ScalarEvolution>();
|
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>();
|
2004-04-18 05:20:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char LoopUnroll::ID = 0;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(LoopUnroll, "loop-unroll", "Unroll loops", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LCSSA)
|
|
|
|
INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2011-04-13 16:15:29 +00:00
|
|
|
Pass *llvm::createLoopUnrollPass(int Threshold, int Count, int AllowPartial) {
|
2011-04-13 16:45:49 +00:00
|
|
|
return new LoopUnroll(Threshold, Count, AllowPartial);
|
2011-04-13 16:15:29 +00:00
|
|
|
}
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2007-05-08 15:14:19 +00:00
|
|
|
/// ApproximateLoopSize - Approximate the size of the loop.
|
2010-02-05 23:21:31 +00:00
|
|
|
static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls) {
|
2009-10-31 14:54:17 +00:00
|
|
|
CodeMetrics Metrics;
|
2008-06-22 20:18:58 +00:00
|
|
|
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
|
2009-10-31 14:54:17 +00:00
|
|
|
I != E; ++I)
|
|
|
|
Metrics.analyzeBasicBlock(*I);
|
2010-09-09 20:32:23 +00:00
|
|
|
NumCalls = Metrics.NumInlineCandidates;
|
2010-09-09 19:07:31 +00:00
|
|
|
|
|
|
|
unsigned LoopSize = Metrics.NumInsts;
|
|
|
|
|
|
|
|
// Don't allow an estimate of size zero. This would allows unrolling of loops
|
|
|
|
// with huge iteration counts, which is a compile time problem even if it's
|
|
|
|
// not a problem for code quality.
|
|
|
|
if (LoopSize == 0) LoopSize = 1;
|
|
|
|
|
|
|
|
return LoopSize;
|
2004-04-18 05:20:17 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 01:38:05 +00:00
|
|
|
bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
|
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();
|
2010-01-05 01:27:44 +00:00
|
|
|
DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName()
|
2009-07-25 00:23:56 +00:00
|
|
|
<< "] Loop %" << Header->getName() << "\n");
|
2009-07-27 23:14:11 +00:00
|
|
|
(void)Header;
|
2010-09-07 23:15:30 +00:00
|
|
|
|
|
|
|
// Determine the current unrolling threshold. While this is normally set
|
|
|
|
// from UnrollThreshold, it is overridden to a smaller value if the current
|
|
|
|
// function is marked as optimize-for-size, and the unroll threshold was
|
|
|
|
// not user specified.
|
2011-04-13 16:15:29 +00:00
|
|
|
unsigned Threshold = CurrentThreshold;
|
|
|
|
if (!UserThreshold &&
|
|
|
|
Header->getParent()->hasFnAttr(Attribute::OptimizeForSize))
|
|
|
|
Threshold = OptSizeUnrollThreshold;
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2008-05-14 00:24:14 +00:00
|
|
|
// Find trip count
|
|
|
|
unsigned TripCount = L->getSmallConstantTripCount();
|
2011-04-13 16:15:29 +00:00
|
|
|
unsigned Count = CurrentCount;
|
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.
|
2011-04-13 16:15:29 +00:00
|
|
|
if (Threshold != NoThreshold) {
|
2010-09-09 20:32:23 +00:00
|
|
|
unsigned NumInlineCandidates;
|
|
|
|
unsigned LoopSize = ApproximateLoopSize(L, NumInlineCandidates);
|
2010-01-05 01:27:44 +00:00
|
|
|
DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n");
|
2010-09-09 20:32:23 +00:00
|
|
|
if (NumInlineCandidates != 0) {
|
|
|
|
DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n");
|
2010-09-09 20:02:23 +00:00
|
|
|
return false;
|
2010-02-05 23:21:31 +00:00
|
|
|
}
|
2010-09-29 18:05:19 +00:00
|
|
|
uint64_t Size = (uint64_t)LoopSize*Count;
|
2011-04-13 16:15:29 +00:00
|
|
|
if (TripCount != 1 && Size > Threshold) {
|
2010-01-05 01:27:44 +00:00
|
|
|
DEBUG(dbgs() << " Too large to fully unroll with count: " << Count
|
2011-04-13 16:15:29 +00:00
|
|
|
<< " because size: " << Size << ">" << Threshold << "\n");
|
|
|
|
if (!CurrentAllowPartial) {
|
2010-01-05 01:27:44 +00:00
|
|
|
DEBUG(dbgs() << " will not try to unroll partially because "
|
2009-08-13 02:45:03 +00:00
|
|
|
<< "-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
|
2011-04-13 16:15:29 +00:00
|
|
|
Count = Threshold / LoopSize;
|
2009-08-13 03:00:57 +00:00
|
|
|
while (Count != 0 && TripCount%Count != 0) {
|
|
|
|
Count--;
|
|
|
|
}
|
|
|
|
if (Count < 2) {
|
2010-01-05 01:27:44 +00:00
|
|
|
DEBUG(dbgs() << " could not unroll partially\n");
|
2009-08-13 03:00:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-01-05 01:27:44 +00:00
|
|
|
DEBUG(dbgs() << " 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.
|
2010-08-29 17:21:35 +00:00
|
|
|
if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>())
|
2008-07-03 07:04:22 +00:00
|
|
|
DT->runOnFunction(*F);
|
2004-04-18 05:20:17 +00:00
|
|
|
return true;
|
|
|
|
}
|