Make IVUsers iterative instead of recursive.

This has the side effect of reversing the order of most of
IVUser's results.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112442 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman
2010-08-29 16:40:03 +00:00
parent 4f7e18dee3
commit eaa40ff74e
3 changed files with 96 additions and 82 deletions

View File

@@ -27,6 +27,7 @@ class Value;
class IVUsers; class IVUsers;
class ScalarEvolution; class ScalarEvolution;
class SCEV; class SCEV;
class SCEVAddRecExpr;
class IVUsers; class IVUsers;
/// IVStrideUse - Keep track of one use of a strided induction variable. /// IVStrideUse - Keep track of one use of a strided induction variable.
@@ -122,7 +123,7 @@ class IVUsers : public LoopPass {
LoopInfo *LI; LoopInfo *LI;
DominatorTree *DT; DominatorTree *DT;
ScalarEvolution *SE; ScalarEvolution *SE;
SmallPtrSet<Instruction*,16> Processed; SmallPtrSet<Instruction *, 16> Processed;
/// IVUses - A list of all tracked IV uses of induction variable expressions /// IVUses - A list of all tracked IV uses of induction variable expressions
/// we are interested in. /// we are interested in.
@@ -134,14 +135,16 @@ class IVUsers : public LoopPass {
virtual void releaseMemory(); virtual void releaseMemory();
const SCEVAddRecExpr *findInterestingAddRec(const SCEV *S) const;
bool isInterestingUser(const Instruction *User) const;
public: public:
static char ID; // Pass ID, replacement for typeid static char ID; // Pass ID, replacement for typeid
IVUsers(); IVUsers();
/// AddUsersIfInteresting - Inspect the specified Instruction. If it is a /// AddUsersIfInteresting - Inspect the def-use graph starting at the
/// reducible SCEV, recursively add its users to the IVUsesByStride set and /// specified Instruction and add IVUsers.
/// return true. Otherwise, return false. void AddUsersIfInteresting(Instruction *I);
bool AddUsersIfInteresting(Instruction *I);
IVStrideUse &AddUser(Instruction *User, Value *Operand); IVStrideUse &AddUser(Instruction *User, Value *Operand);

View File

@@ -35,112 +35,123 @@ Pass *llvm::createIVUsersPass() {
return new IVUsers(); return new IVUsers();
} }
/// isInteresting - Test whether the given expression is "interesting" when /// findInterestingAddRec - Test whether the given expression is interesting.
/// used by the given expression, within the context of analyzing the /// Return the addrec with the current loop which makes it interesting, or
/// given loop. /// null if it is not interesting.
static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L, const SCEVAddRecExpr *IVUsers::findInterestingAddRec(const SCEV *S) const {
ScalarEvolution *SE) {
// An addrec is interesting if it's affine or if it has an interesting start. // An addrec is interesting if it's affine or if it has an interesting start.
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
// Keep things simple. Don't touch loop-variant strides. // Keep things simple. Don't touch loop-variant strides.
if (AR->getLoop() == L) if (AR->getLoop() == L)
return AR->isAffine() || !L->contains(I); return AR;
// Otherwise recurse to see if the start value is interesting, and that // We don't yet know how to do effective SCEV expansions for addrecs
// the step value is not interesting, since we don't yet know how to // with interesting steps.
// do effective SCEV expansions for addrecs with interesting steps. if (findInterestingAddRec(AR->getStepRecurrence(*SE)))
return isInteresting(AR->getStart(), I, L, SE) && return 0;
!isInteresting(AR->getStepRecurrence(*SE), I, L, SE); // Otherwise recurse to see if the start value is interesting.
return findInterestingAddRec(AR->getStart());
} }
// An add is interesting if exactly one of its operands is interesting. // An add is interesting if exactly one of its operands is interesting.
if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
bool AnyInterestingYet = false;
for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end(); for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
OI != OE; ++OI) OI != OE; ++OI)
if (isInteresting(*OI, I, L, SE)) { if (const SCEVAddRecExpr *AR = findInterestingAddRec(*OI))
if (AnyInterestingYet) return AR;
return false; return 0;
AnyInterestingYet = true;
}
return AnyInterestingYet;
} }
// Nothing else is interesting here. // Nothing else is interesting here.
return false; return 0;
}
bool IVUsers::isInterestingUser(const Instruction *User) const {
// Void and FP expressions cannot be reduced.
if (!SE->isSCEVable(User->getType()))
return false;
// LSR is not APInt clean, do not touch integers bigger than 64-bits.
if (SE->getTypeSizeInBits(User->getType()) > 64)
return false;
// Don't descend into PHI nodes outside the current loop.
if (LI->getLoopFor(User->getParent()) != L &&
isa<PHINode>(User))
return false;
// Otherwise, it may be interesting.
return true;
} }
/// AddUsersIfInteresting - Inspect the specified instruction. If it is a /// AddUsersIfInteresting - Inspect the specified instruction. If it is a
/// reducible SCEV, recursively add its users to the IVUsesByStride set and /// reducible SCEV, recursively add its users to the IVUsesByStride set and
/// return true. Otherwise, return false. /// return true. Otherwise, return false.
bool IVUsers::AddUsersIfInteresting(Instruction *I) { void IVUsers::AddUsersIfInteresting(Instruction *I) {
if (!SE->isSCEVable(I->getType())) // Stop if we've seen this before.
return false; // Void and FP expressions cannot be reduced.
// LSR is not APInt clean, do not touch integers bigger than 64-bits.
if (SE->getTypeSizeInBits(I->getType()) > 64)
return false;
if (!Processed.insert(I)) if (!Processed.insert(I))
return true; // Instruction already handled. return;
// Get the symbolic expression for this instruction. // If this PHI node is not SCEVable, ignore it.
const SCEV *ISE = SE->getSCEV(I); if (!SE->isSCEVable(I->getType()))
return;
// If we've come to an uninteresting expression, stop the traversal and // If this PHI node is not an addrec for this loop, ignore it.
// call this a user. const SCEVAddRecExpr *Expr = findInterestingAddRec(SE->getSCEV(I));
if (!isInteresting(ISE, I, L, SE)) if (!Expr)
return false; return;
SmallPtrSet<Instruction *, 4> UniqueUsers; // Walk the def-use graph.
for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); SmallVector<std::pair<Instruction *, const SCEVAddRecExpr *>, 16> Worklist;
UI != E; ++UI) { Worklist.push_back(std::make_pair(I, Expr));
Instruction *User = cast<Instruction>(*UI); do {
if (!UniqueUsers.insert(User)) std::pair<Instruction *, const SCEVAddRecExpr *> P =
continue; Worklist.pop_back_val();
Instruction *Op = P.first;
const SCEVAddRecExpr *OpAR = P.second;
// Do not infinitely recurse on PHI nodes. // Visit Op's users.
if (isa<PHINode>(User) && Processed.count(User)) SmallPtrSet<Instruction *, 8> VisitedUsers;
continue; for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end();
UI != E; ++UI) {
// Don't visit any individual user more than once.
Instruction *User = cast<Instruction>(*UI);
if (!VisitedUsers.insert(User))
continue;
// Descend recursively, but not into PHI nodes outside the current loop. // If it's an affine addrec (which we can pretty safely re-expand) inside
// It's important to see the entire expression outside the loop to get // the loop, or a potentially non-affine addrec outside the loop (which
// choices that depend on addressing mode use right, although we won't // we can evaluate outside of the loop), follow it.
// consider references outside the loop in all cases. if (OpAR->isAffine() || !L->contains(User)) {
// If User is already in Processed, we don't want to recurse into it again, if (isInterestingUser(User)) {
// but do want to record a second reference in the same instruction. const SCEV *UserExpr = SE->getSCEV(User);
bool AddUserToIVUsers = false;
if (LI->getLoopFor(User->getParent()) != L) { if (const SCEVAddRecExpr *AR = findInterestingAddRec(UserExpr)) {
if (isa<PHINode>(User) || Processed.count(User) || // Interesting. Keep searching.
!AddUsersIfInteresting(User)) { if (Processed.insert(User))
DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n' Worklist.push_back(std::make_pair(User, AR));
<< " OF SCEV: " << *ISE << '\n'); continue;
AddUserToIVUsers = true; }
}
} }
} else if (Processed.count(User) ||
!AddUsersIfInteresting(User)) {
DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
<< " OF SCEV: " << *ISE << '\n');
AddUserToIVUsers = true;
}
if (AddUserToIVUsers) { // Otherwise, this is the point where the def-use chain
// Okay, we found a user that we cannot reduce. // becomes uninteresting. Call it an IV User.
IVUses.push_back(new IVStrideUse(this, User, I)); AddUser(User, Op);
IVStrideUse &NewUse = IVUses.back();
// Transform the expression into a normalized form.
ISE = TransformForPostIncUse(NormalizeAutodetect,
ISE, User, I,
NewUse.PostIncLoops,
*SE, *DT);
DEBUG(dbgs() << " NORMALIZED TO: " << *ISE << '\n');
} }
} } while (!Worklist.empty());
return true;
} }
IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) { IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
IVUses.push_back(new IVStrideUse(this, User, Operand)); IVUses.push_back(new IVStrideUse(this, User, Operand));
return IVUses.back(); IVStrideUse &NewUse = IVUses.back();
// Auto-detect and remember post-inc loops for this expression.
const SCEV *S = SE->getSCEV(Operand);
(void)TransformForPostIncUse(NormalizeAutodetect,
S, User, Operand,
NewUse.PostIncLoops,
*SE, *DT);
return NewUse;
} }
IVUsers::IVUsers() IVUsers::IVUsers()
@@ -165,7 +176,7 @@ bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
// them by stride. Start by finding all of the PHI nodes in the header for // 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. // this loop. If they are induction variables, inspect their uses.
for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
(void)AddUsersIfInteresting(I); AddUsersIfInteresting(I);
return false; return false;
} }

View File

@@ -452,9 +452,9 @@ bb5: ; preds = %bb3, %entry
; CHECK-NEXT: addss %xmm{{.*}}, %xmm{{.*}} ; CHECK-NEXT: addss %xmm{{.*}}, %xmm{{.*}}
; CHECK-NEXT: mulss (%r{{[^,]*}}), %xmm{{.*}} ; CHECK-NEXT: mulss (%r{{[^,]*}}), %xmm{{.*}}
; CHECK-NEXT: movss %xmm{{.*}}, (%r{{[^,]*}}) ; CHECK-NEXT: movss %xmm{{.*}}, (%r{{[^,]*}})
; CHECK-NEXT: addq $4, %r{{.*}}
; CHECK-NEXT: decq %r{{.*}} ; CHECK-NEXT: decq %r{{.*}}
; CHECK-NEXT: addq $4, %r{{.*}} ; CHECK-NEXT: addq $4, %r{{.*}}
; CHECK-NEXT: addq $4, %r{{.*}}
; CHECK-NEXT: movaps %xmm{{.*}}, %xmm{{.*}} ; CHECK-NEXT: movaps %xmm{{.*}}, %xmm{{.*}}
; CHECK-NEXT: BB10_2: ; CHECK-NEXT: BB10_2:
; CHECK-NEXT: testq %r{{.*}}, %r{{.*}} ; CHECK-NEXT: testq %r{{.*}}, %r{{.*}}