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"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2011-01-02 07:35:53 +00:00
|
|
|
#include "llvm/Analysis/CodeMetrics.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2010-07-26 18:11:16 +00:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2013-01-21 13:04:33 +00:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.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."));
|
|
|
|
|
2011-12-09 06:19:40 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Unroll loops with run-time trip counts"));
|
|
|
|
|
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);
|
2011-07-23 00:29:16 +00:00
|
|
|
|
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;
|
2011-07-23 00:29:16 +00:00
|
|
|
|
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-07-23 00:29:16 +00:00
|
|
|
|
2011-12-09 06:19:40 +00:00
|
|
|
// Default unroll count for loops with run-time trip count if
|
|
|
|
// -unroll-count is not set
|
|
|
|
static const unsigned UnrollRuntimeCount = 8;
|
|
|
|
|
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);
|
2011-08-10 04:29:49 +00:00
|
|
|
AU.addRequired<ScalarEvolution>();
|
2010-08-29 17:21:35 +00:00
|
|
|
AU.addPreserved<ScalarEvolution>();
|
2013-01-21 13:04:33 +00:00
|
|
|
AU.addRequired<TargetTransformInfo>();
|
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)
|
2013-01-21 13:04:33 +00:00
|
|
|
INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LCSSA)
|
2011-10-19 23:56:07 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
|
2010-10-12 19:48:12 +00:00
|
|
|
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.
|
2011-10-01 01:39:05 +00:00
|
|
|
static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls,
|
2013-01-21 13:04:33 +00:00
|
|
|
bool &NotDuplicatable,
|
|
|
|
const TargetTransformInfo &TTI) {
|
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)
|
2013-01-21 13:04:33 +00:00
|
|
|
Metrics.analyzeBasicBlock(*I, TTI);
|
2010-09-09 20:32:23 +00:00
|
|
|
NumCalls = Metrics.NumInlineCandidates;
|
2012-12-20 16:04:27 +00:00
|
|
|
NotDuplicatable = Metrics.notDuplicatable;
|
2011-07-23 00:29:16 +00:00
|
|
|
|
2010-09-09 19:07:31 +00:00
|
|
|
unsigned LoopSize = Metrics.NumInsts;
|
2011-07-23 00:29:16 +00:00
|
|
|
|
2010-09-09 19:07:31 +00:00
|
|
|
// 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;
|
2011-07-23 00:29:16 +00:00
|
|
|
|
2010-09-09 19:07:31 +00:00
|
|
|
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>();
|
2011-08-11 23:36:16 +00:00
|
|
|
ScalarEvolution *SE = &getAnalysis<ScalarEvolution>();
|
2013-01-21 13:04:33 +00:00
|
|
|
const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();
|
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;
|
2011-07-23 00:29:16 +00:00
|
|
|
|
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;
|
2011-07-23 00:29:16 +00:00
|
|
|
if (!UserThreshold &&
|
2012-12-30 10:32:01 +00:00
|
|
|
Header->getParent()->getAttributes().
|
|
|
|
hasAttribute(AttributeSet::FunctionIndex,
|
|
|
|
Attribute::OptimizeForSize))
|
2011-04-13 16:15:29 +00:00
|
|
|
Threshold = OptSizeUnrollThreshold;
|
2004-04-18 05:20:17 +00:00
|
|
|
|
2011-08-11 23:36:16 +00:00
|
|
|
// Find trip count and trip multiple if count is not available
|
|
|
|
unsigned TripCount = 0;
|
2011-07-23 00:33:05 +00:00
|
|
|
unsigned TripMultiple = 1;
|
2011-11-28 19:22:09 +00:00
|
|
|
// Find "latch trip count". UnrollLoop assumes that control cannot exit
|
|
|
|
// via the loop latch on any iteration prior to TripCount. The loop may exit
|
|
|
|
// early via an earlier branch.
|
|
|
|
BasicBlock *LatchBlock = L->getLoopLatch();
|
|
|
|
if (LatchBlock) {
|
|
|
|
TripCount = SE->getSmallConstantTripCount(L, LatchBlock);
|
|
|
|
TripMultiple = SE->getSmallConstantTripMultiple(L, LatchBlock);
|
2011-08-11 23:36:16 +00:00
|
|
|
}
|
2011-12-09 06:19:40 +00:00
|
|
|
// Use a default unroll-count if the user doesn't specify a value
|
|
|
|
// and the trip count is a run-time value. The default is different
|
|
|
|
// for run-time or compile-time trip count loops.
|
2011-07-23 00:33:05 +00:00
|
|
|
unsigned Count = CurrentCount;
|
2011-12-09 06:19:40 +00:00
|
|
|
if (UnrollRuntime && CurrentCount == 0 && TripCount == 0)
|
|
|
|
Count = UnrollRuntimeCount;
|
|
|
|
|
2007-05-11 20:53:41 +00:00
|
|
|
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;
|
2012-12-20 16:04:27 +00:00
|
|
|
bool notDuplicatable;
|
|
|
|
unsigned LoopSize = ApproximateLoopSize(L, NumInlineCandidates,
|
2013-01-21 13:04:33 +00:00
|
|
|
notDuplicatable, TTI);
|
2010-01-05 01:27:44 +00:00
|
|
|
DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n");
|
2012-12-20 16:04:27 +00:00
|
|
|
if (notDuplicatable) {
|
|
|
|
DEBUG(dbgs() << " Not unrolling loop which contains non duplicatable"
|
|
|
|
<< " instructions.\n");
|
|
|
|
return false;
|
|
|
|
}
|
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");
|
2011-12-09 06:19:40 +00:00
|
|
|
if (!CurrentAllowPartial && !(UnrollRuntime && TripCount == 0)) {
|
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;
|
|
|
|
}
|
2011-12-09 06:19:40 +00:00
|
|
|
if (TripCount) {
|
|
|
|
// Reduce unroll count to be modulo of TripCount for partial unrolling
|
2012-04-04 11:44:08 +00:00
|
|
|
Count = Threshold / LoopSize;
|
2011-12-09 06:19:40 +00:00
|
|
|
while (Count != 0 && TripCount%Count != 0)
|
|
|
|
Count--;
|
|
|
|
}
|
|
|
|
else if (UnrollRuntime) {
|
|
|
|
// Reduce unroll count to be a lower power-of-two value
|
2012-04-04 11:44:08 +00:00
|
|
|
while (Count != 0 && Size > Threshold) {
|
2011-12-09 06:19:40 +00:00
|
|
|
Count >>= 1;
|
|
|
|
Size = LoopSize*Count;
|
|
|
|
}
|
2009-08-13 03:00:57 +00:00
|
|
|
}
|
|
|
|
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.
|
2011-12-09 06:19:40 +00:00
|
|
|
if (!UnrollLoop(L, Count, TripCount, UnrollRuntime, TripMultiple, LI, &LPM))
|
2008-05-14 00:24:14 +00:00
|
|
|
return false;
|
2004-04-18 05:20:17 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|