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"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.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-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2007-03-07 01:38:05 +00:00
|
|
|
class VISIBILITY_HIDDEN 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
|
2007-05-08 15:19:19 +00:00
|
|
|
LoopUnroll() : LoopPass((intptr_t)&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>();
|
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");
|
|
|
|
|
2007-03-07 01:38:05 +00:00
|
|
|
LoopPass *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;
|
|
|
|
for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
|
|
|
|
BasicBlock *BB = L->getBlocks()[i];
|
|
|
|
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
|
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.
|
2008-03-17 23:41:20 +00:00
|
|
|
if (isa<IntrinsicInst>(I))
|
|
|
|
Size = Size + 3;
|
|
|
|
else
|
|
|
|
Size = Size + 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();
|
2007-05-11 20:53:41 +00:00
|
|
|
DOUT << "Loop Unroll: F[" << Header->getParent()->getName()
|
|
|
|
<< "] Loop %" << Header->getName() << "\n";
|
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;
|
|
|
|
|
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
|
|
|
|
// don't unroll.
|
|
|
|
if (TripCount != 0) {
|
|
|
|
Count = TripCount;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
DOUT << " Loop Size = " << LoopSize << "\n";
|
|
|
|
uint64_t Size = (uint64_t)LoopSize*Count;
|
2008-05-14 00:24:14 +00:00
|
|
|
if (TripCount != 1 && Size > UnrollThreshold) {
|
2007-05-11 20:53:41 +00:00
|
|
|
DOUT << " TOO LARGE TO UNROLL: "
|
2008-05-14 00:24:14 +00:00
|
|
|
<< Size << ">" << UnrollThreshold << "\n";
|
2007-05-11 20:53:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
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.
|
|
|
|
if (!UnrollLoop(L, Count, LI, &LPM))
|
|
|
|
return false;
|
2004-04-18 05:20:17 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|