2009-05-12 02:17:14 +00:00
|
|
|
//===- IVUsers.cpp - Induction Variable Users -------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements bookkeeping for "interesting" users of expressions
|
|
|
|
// computed from induction variables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "iv-users"
|
|
|
|
#include "llvm/Analysis/IVUsers.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2011-05-04 02:10:13 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-03-18 16:50:32 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2011-01-02 22:09:33 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2009-05-12 02:17:14 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
char IVUsers::ID = 0;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(IVUsers, "iv-users",
|
|
|
|
"Induction Variable Users", false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
|
|
|
|
INITIALIZE_PASS_END(IVUsers, "iv-users",
|
|
|
|
"Induction Variable Users", false, true)
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2011-05-04 02:10:13 +00:00
|
|
|
// IVUsers behavior currently depends on this temporary indvars mode. The
|
|
|
|
// option must be defined upstream from its uses.
|
|
|
|
namespace llvm {
|
|
|
|
bool DisableIVRewrite = false;
|
|
|
|
}
|
|
|
|
cl::opt<bool, true> DisableIVRewriteOpt(
|
|
|
|
"disable-iv-rewrite", cl::Hidden, cl::location(llvm::DisableIVRewrite),
|
|
|
|
cl::desc("Disable canonical induction variable rewriting"));
|
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
Pass *llvm::createIVUsersPass() {
|
|
|
|
return new IVUsers();
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:45:53 +00:00
|
|
|
/// isInteresting - Test whether the given expression is "interesting" when
|
|
|
|
/// used by the given expression, within the context of analyzing the
|
|
|
|
/// given loop.
|
|
|
|
static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
|
|
|
|
ScalarEvolution *SE) {
|
2010-04-07 22:27:08 +00:00
|
|
|
// An addrec is interesting if it's affine or if it has an interesting start.
|
|
|
|
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
|
|
|
|
// Keep things simple. Don't touch loop-variant strides.
|
2010-04-09 01:22:56 +00:00
|
|
|
if (AR->getLoop() == L)
|
2010-09-01 01:45:53 +00:00
|
|
|
return AR->isAffine() || !L->contains(I);
|
|
|
|
// Otherwise recurse to see if the start value is interesting, and that
|
|
|
|
// the step value is not interesting, since we don't yet know how to
|
|
|
|
// do effective SCEV expansions for addrecs with interesting steps.
|
|
|
|
return isInteresting(AR->getStart(), I, L, SE) &&
|
|
|
|
!isInteresting(AR->getStepRecurrence(*SE), I, L, SE);
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 22:50:37 +00:00
|
|
|
// An add is interesting if exactly one of its operands is interesting.
|
2010-04-07 22:27:08 +00:00
|
|
|
if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
|
2010-09-01 01:45:53 +00:00
|
|
|
bool AnyInterestingYet = false;
|
2010-04-07 22:27:08 +00:00
|
|
|
for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
|
|
|
|
OI != OE; ++OI)
|
2010-09-01 01:45:53 +00:00
|
|
|
if (isInteresting(*OI, I, L, SE)) {
|
|
|
|
if (AnyInterestingYet)
|
|
|
|
return false;
|
|
|
|
AnyInterestingYet = true;
|
|
|
|
}
|
|
|
|
return AnyInterestingYet;
|
2010-04-07 22:27:08 +00:00
|
|
|
}
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2010-04-07 22:27:08 +00:00
|
|
|
// Nothing else is interesting here.
|
2010-09-01 01:45:53 +00:00
|
|
|
return false;
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 01:45:53 +00:00
|
|
|
/// AddUsersIfInteresting - Inspect the specified instruction. If it is a
|
|
|
|
/// reducible SCEV, recursively add its users to the IVUsesByStride set and
|
|
|
|
/// return true. Otherwise, return false.
|
2011-06-21 15:43:52 +00:00
|
|
|
bool IVUsers::AddUsersIfInteresting(Instruction *I) {
|
2010-09-01 01:45:53 +00:00
|
|
|
if (!SE->isSCEVable(I->getType()))
|
|
|
|
return false; // Void and FP expressions cannot be reduced.
|
2009-05-12 02:17:14 +00:00
|
|
|
|
|
|
|
// LSR is not APInt clean, do not touch integers bigger than 64-bits.
|
2011-03-18 16:50:32 +00:00
|
|
|
// Also avoid creating IVs of non-native types. For example, we don't want a
|
|
|
|
// 64-bit IV in 32-bit code just because the loop has one 64-bit cast.
|
|
|
|
uint64_t Width = SE->getTypeSizeInBits(I->getType());
|
|
|
|
if (Width > 64 || (TD && !TD->isLegalInteger(Width)))
|
2009-05-12 02:17:14 +00:00
|
|
|
return false;
|
|
|
|
|
2011-05-04 02:10:13 +00:00
|
|
|
// We expect Sign/Zero extension to be eliminated from the IR before analyzing
|
|
|
|
// any downstream uses.
|
|
|
|
if (DisableIVRewrite && (isa<SExtInst>(I) || isa<ZExtInst>(I)))
|
|
|
|
return false;
|
|
|
|
|
2010-09-01 01:45:53 +00:00
|
|
|
if (!Processed.insert(I))
|
|
|
|
return true; // Instruction already handled.
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2010-09-01 01:45:53 +00:00
|
|
|
// Get the symbolic expression for this instruction.
|
|
|
|
const SCEV *ISE = SE->getSCEV(I);
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2010-09-01 01:45:53 +00:00
|
|
|
// If we've come to an uninteresting expression, stop the traversal and
|
|
|
|
// call this a user.
|
|
|
|
if (!isInteresting(ISE, I, L, SE))
|
|
|
|
return false;
|
2009-11-19 02:05:44 +00:00
|
|
|
|
2010-09-01 01:45:53 +00:00
|
|
|
SmallPtrSet<Instruction *, 4> UniqueUsers;
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI);
|
|
|
|
if (!UniqueUsers.insert(User))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Do not infinitely recurse on PHI nodes.
|
|
|
|
if (isa<PHINode>(User) && Processed.count(User))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Descend recursively, but not into PHI nodes outside the current loop.
|
|
|
|
// It's important to see the entire expression outside the loop to get
|
|
|
|
// choices that depend on addressing mode use right, although we won't
|
|
|
|
// consider references outside the loop in all cases.
|
|
|
|
// If User is already in Processed, we don't want to recurse into it again,
|
|
|
|
// but do want to record a second reference in the same instruction.
|
|
|
|
bool AddUserToIVUsers = false;
|
|
|
|
if (LI->getLoopFor(User->getParent()) != L) {
|
|
|
|
if (isa<PHINode>(User) || Processed.count(User) ||
|
2011-06-21 15:43:52 +00:00
|
|
|
!AddUsersIfInteresting(User)) {
|
2010-09-01 01:45:53 +00:00
|
|
|
DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
|
|
|
|
<< " OF SCEV: " << *ISE << '\n');
|
|
|
|
AddUserToIVUsers = true;
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
2011-06-21 15:43:52 +00:00
|
|
|
} else if (Processed.count(User) || !AddUsersIfInteresting(User)) {
|
2010-09-01 01:45:53 +00:00
|
|
|
DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
|
|
|
|
<< " OF SCEV: " << *ISE << '\n');
|
|
|
|
AddUserToIVUsers = true;
|
|
|
|
}
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2010-09-01 01:45:53 +00:00
|
|
|
if (AddUserToIVUsers) {
|
|
|
|
// Okay, we found a user that we cannot reduce.
|
2011-06-21 15:43:52 +00:00
|
|
|
IVUses.push_back(new IVStrideUse(this, User, I));
|
2010-09-01 01:45:53 +00:00
|
|
|
IVStrideUse &NewUse = IVUses.back();
|
2011-05-27 18:42:33 +00:00
|
|
|
// Autodetect the post-inc loop set, populating NewUse.PostIncLoops.
|
|
|
|
// The regular return value here is discarded; instead of recording
|
|
|
|
// it, we just recompute it when we need it.
|
2010-09-01 01:45:53 +00:00
|
|
|
ISE = TransformForPostIncUse(NormalizeAutodetect,
|
|
|
|
ISE, User, I,
|
|
|
|
NewUse.PostIncLoops,
|
|
|
|
*SE, *DT);
|
|
|
|
DEBUG(dbgs() << " NORMALIZED TO: " << *ISE << '\n');
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
2010-09-01 01:45:53 +00:00
|
|
|
}
|
|
|
|
return true;
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
|
2011-06-21 15:43:52 +00:00
|
|
|
IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
|
|
|
|
IVUses.push_back(new IVStrideUse(this, User, Operand));
|
2010-09-01 01:45:53 +00:00
|
|
|
return IVUses.back();
|
2009-11-12 07:35:05 +00:00
|
|
|
}
|
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
IVUsers::IVUsers()
|
2010-10-19 17:21:58 +00:00
|
|
|
: LoopPass(ID) {
|
|
|
|
initializeIVUsersPass(*PassRegistry::getPassRegistry());
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
AU.addRequired<ScalarEvolution>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
|
|
|
|
|
|
|
|
L = l;
|
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
DT = &getAnalysis<DominatorTree>();
|
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
2011-03-18 16:50:32 +00:00
|
|
|
TD = getAnalysisIfAvailable<TargetData>();
|
2009-05-12 02:17:14 +00:00
|
|
|
|
|
|
|
// Find all uses of induction variables in this loop, and categorize
|
|
|
|
// them by stride. Start by finding all of the PHI nodes in the header for
|
|
|
|
// this loop. If they are induction variables, inspect their uses.
|
|
|
|
for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
|
2011-06-21 15:43:52 +00:00
|
|
|
(void)AddUsersIfInteresting(I);
|
2009-05-12 02:17:14 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IVUsers::print(raw_ostream &OS, const Module *M) const {
|
|
|
|
OS << "IV Users for loop ";
|
|
|
|
WriteAsOperand(OS, L->getHeader(), false);
|
|
|
|
if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
|
|
|
|
OS << " with backedge-taken count "
|
|
|
|
<< *SE->getBackedgeTakenCount(L);
|
|
|
|
}
|
|
|
|
OS << ":\n";
|
|
|
|
|
2010-02-12 10:34:29 +00:00
|
|
|
for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(),
|
|
|
|
E = IVUses.end(); UI != E; ++UI) {
|
|
|
|
OS << " ";
|
|
|
|
WriteAsOperand(OS, UI->getOperandValToReplace(), false);
|
2010-04-19 21:48:58 +00:00
|
|
|
OS << " = " << *getReplacementExpr(*UI);
|
2010-04-07 22:27:08 +00:00
|
|
|
for (PostIncLoopSet::const_iterator
|
|
|
|
I = UI->PostIncLoops.begin(),
|
|
|
|
E = UI->PostIncLoops.end(); I != E; ++I) {
|
|
|
|
OS << " (post-inc with loop ";
|
|
|
|
WriteAsOperand(OS, (*I)->getHeader(), false);
|
|
|
|
OS << ")";
|
|
|
|
}
|
2010-02-12 10:34:29 +00:00
|
|
|
OS << " in ";
|
2010-09-02 23:03:10 +00:00
|
|
|
UI->getUser()->print(OS);
|
2010-02-12 10:34:29 +00:00
|
|
|
OS << '\n';
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IVUsers::dump() const {
|
2009-12-23 20:20:46 +00:00
|
|
|
print(dbgs());
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IVUsers::releaseMemory() {
|
2009-12-17 09:39:49 +00:00
|
|
|
Processed.clear();
|
2009-12-18 00:06:20 +00:00
|
|
|
IVUses.clear();
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
|
2010-04-19 21:48:58 +00:00
|
|
|
/// getReplacementExpr - Return a SCEV expression which computes the
|
|
|
|
/// value of the OperandValToReplace.
|
|
|
|
const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const {
|
|
|
|
return SE->getSCEV(IU.getOperandValToReplace());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getExpr - Return the expression for the use.
|
|
|
|
const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const {
|
|
|
|
return
|
|
|
|
TransformForPostIncUse(Normalize, getReplacementExpr(IU),
|
|
|
|
IU.getUser(), IU.getOperandValToReplace(),
|
|
|
|
const_cast<PostIncLoopSet &>(IU.getPostIncLoops()),
|
|
|
|
*SE, *DT);
|
|
|
|
}
|
|
|
|
|
2010-04-07 22:27:08 +00:00
|
|
|
static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
|
|
|
|
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
|
|
|
|
if (AR->getLoop() == L)
|
|
|
|
return AR;
|
|
|
|
return findAddRecForLoop(AR->getStart(), L);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
|
|
|
|
for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L))
|
|
|
|
return AR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-19 21:48:58 +00:00
|
|
|
const SCEV *IVUsers::getStride(const IVStrideUse &IU, const Loop *L) const {
|
|
|
|
if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(IU), L))
|
|
|
|
return AR->getStepRecurrence(*SE);
|
2010-04-07 22:27:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IVStrideUse::transformToPostInc(const Loop *L) {
|
|
|
|
PostIncLoops.insert(L);
|
|
|
|
}
|
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
void IVStrideUse::deleted() {
|
|
|
|
// Remove this user from the list.
|
2010-02-12 10:34:29 +00:00
|
|
|
Parent->IVUses.erase(this);
|
2009-05-12 02:17:14 +00:00
|
|
|
// this now dangles!
|
|
|
|
}
|