llvm.org GIT mirror llvm / eaa40ff
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 Dan Gohman 10 years ago
3 changed file(s) with 102 addition(s) and 88 deletion(s). Raw diff Collapse all Expand all
2626 class IVUsers;
2727 class ScalarEvolution;
2828 class SCEV;
29 class SCEVAddRecExpr;
2930 class IVUsers;
3031
3132 /// IVStrideUse - Keep track of one use of a strided induction variable.
121122 LoopInfo *LI;
122123 DominatorTree *DT;
123124 ScalarEvolution *SE;
124 SmallPtrSet*,16> Processed;
125 SmallPtrSet *, 16> Processed;
125126
126127 /// IVUses - A list of all tracked IV uses of induction variable expressions
127128 /// we are interested in.
133134
134135 virtual void releaseMemory();
135136
137 const SCEVAddRecExpr *findInterestingAddRec(const SCEV *S) const;
138 bool isInterestingUser(const Instruction *User) const;
139
136140 public:
137141 static char ID; // Pass ID, replacement for typeid
138142 IVUsers();
139143
140 /// AddUsersIfInteresting - Inspect the specified Instruction. If it is a
141 /// reducible SCEV, recursively add its users to the IVUsesByStride set and
142 /// return true. Otherwise, return false.
143 bool AddUsersIfInteresting(Instruction *I);
144 /// AddUsersIfInteresting - Inspect the def-use graph starting at the
145 /// specified Instruction and add IVUsers.
146 void AddUsersIfInteresting(Instruction *I);
144147
145148 IVStrideUse &AddUser(Instruction *User, Value *Operand);
146149
3434 return new IVUsers();
3535 }
3636
37 /// isInteresting - Test whether the given expression is "interesting" when
38 /// used by the given expression, within the context of analyzing the
39 /// given loop.
40 static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
41 ScalarEvolution *SE) {
37 /// findInterestingAddRec - Test whether the given expression is interesting.
38 /// Return the addrec with the current loop which makes it interesting, or
39 /// null if it is not interesting.
40 const SCEVAddRecExpr *IVUsers::findInterestingAddRec(const SCEV *S) const {
4241 // An addrec is interesting if it's affine or if it has an interesting start.
4342 if (const SCEVAddRecExpr *AR = dyn_cast(S)) {
4443 // Keep things simple. Don't touch loop-variant strides.
4544 if (AR->getLoop() == L)
46 return AR->isAffine() || !L->contains(I);
47 // Otherwise recurse to see if the start value is interesting, and that
48 // the step value is not interesting, since we don't yet know how to
49 // do effective SCEV expansions for addrecs with interesting steps.
50 return isInteresting(AR->getStart(), I, L, SE) &&
51 !isInteresting(AR->getStepRecurrence(*SE), I, L, SE);
45 return AR;
46 // We don't yet know how to do effective SCEV expansions for addrecs
47 // with interesting steps.
48 if (findInterestingAddRec(AR->getStepRecurrence(*SE)))
49 return 0;
50 // Otherwise recurse to see if the start value is interesting.
51 return findInterestingAddRec(AR->getStart());
5252 }
5353
5454 // An add is interesting if exactly one of its operands is interesting.
5555 if (const SCEVAddExpr *Add = dyn_cast(S)) {
56 bool AnyInterestingYet = false;
5756 for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
5857 OI != OE; ++OI)
59 if (isInteresting(*OI, I, L, SE)) {
60 if (AnyInterestingYet)
61 return false;
62 AnyInterestingYet = true;
63 }
64 return AnyInterestingYet;
58 if (const SCEVAddRecExpr *AR = findInterestingAddRec(*OI))
59 return AR;
60 return 0;
6561 }
6662
6763 // Nothing else is interesting here.
68 return false;
64 return 0;
65 }
66
67 bool IVUsers::isInterestingUser(const Instruction *User) const {
68 // Void and FP expressions cannot be reduced.
69 if (!SE->isSCEVable(User->getType()))
70 return false;
71
72 // LSR is not APInt clean, do not touch integers bigger than 64-bits.
73 if (SE->getTypeSizeInBits(User->getType()) > 64)
74 return false;
75
76 // Don't descend into PHI nodes outside the current loop.
77 if (LI->getLoopFor(User->getParent()) != L &&
78 isa(User))
79 return false;
80
81 // Otherwise, it may be interesting.
82 return true;
6983 }
7084
7185 /// AddUsersIfInteresting - Inspect the specified instruction. If it is a
7286 /// reducible SCEV, recursively add its users to the IVUsesByStride set and
7387 /// return true. Otherwise, return false.
74 bool IVUsers::AddUsersIfInteresting(Instruction *I) {
88 void IVUsers::AddUsersIfInteresting(Instruction *I) {
89 // Stop if we've seen this before.
90 if (!Processed.insert(I))
91 return;
92
93 // If this PHI node is not SCEVable, ignore it.
7594 if (!SE->isSCEVable(I->getType()))
76 return false; // Void and FP expressions cannot be reduced.
77
78 // LSR is not APInt clean, do not touch integers bigger than 64-bits.
79 if (SE->getTypeSizeInBits(I->getType()) > 64)
80 return false;
81
82 if (!Processed.insert(I))
83 return true; // Instruction already handled.
84
85 // Get the symbolic expression for this instruction.
86 const SCEV *ISE = SE->getSCEV(I);
87
88 // If we've come to an uninteresting expression, stop the traversal and
89 // call this a user.
90 if (!isInteresting(ISE, I, L, SE))
91 return false;
92
93 SmallPtrSet UniqueUsers;
94 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
95 UI != E; ++UI) {
96 Instruction *User = cast(*UI);
97 if (!UniqueUsers.insert(User))
98 continue;
99
100 // Do not infinitely recurse on PHI nodes.
101 if (isa(User) && Processed.count(User))
102 continue;
103
104 // Descend recursively, but not into PHI nodes outside the current loop.
105 // It's important to see the entire expression outside the loop to get
106 // choices that depend on addressing mode use right, although we won't
107 // consider references outside the loop in all cases.
108 // If User is already in Processed, we don't want to recurse into it again,
109 // but do want to record a second reference in the same instruction.
110 bool AddUserToIVUsers = false;
111 if (LI->getLoopFor(User->getParent()) != L) {
112 if (isa(User) || Processed.count(User) ||
113 !AddUsersIfInteresting(User)) {
114 DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
115 << " OF SCEV: " << *ISE << '\n');
116 AddUserToIVUsers = true;
95 return;
96
97 // If this PHI node is not an addrec for this loop, ignore it.
98 const SCEVAddRecExpr *Expr = findInterestingAddRec(SE->getSCEV(I));
99 if (!Expr)
100 return;
101
102 // Walk the def-use graph.
103 SmallVector, 16> Worklist;
104 Worklist.push_back(std::make_pair(I, Expr));
105 do {
106 std::pair P =
107 Worklist.pop_back_val();
108 Instruction *Op = P.first;
109 const SCEVAddRecExpr *OpAR = P.second;
110
111 // Visit Op's users.
112 SmallPtrSet VisitedUsers;
113 for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end();
114 UI != E; ++UI) {
115 // Don't visit any individual user more than once.
116 Instruction *User = cast(*UI);
117 if (!VisitedUsers.insert(User))
118 continue;
119
120 // If it's an affine addrec (which we can pretty safely re-expand) inside
121 // the loop, or a potentially non-affine addrec outside the loop (which
122 // we can evaluate outside of the loop), follow it.
123 if (OpAR->isAffine() || !L->contains(User)) {
124 if (isInterestingUser(User)) {
125 const SCEV *UserExpr = SE->getSCEV(User);
126
127 if (const SCEVAddRecExpr *AR = findInterestingAddRec(UserExpr)) {
128 // Interesting. Keep searching.
129 if (Processed.insert(User))
130 Worklist.push_back(std::make_pair(User, AR));
131 continue;
132 }
133 }
117134 }
118 } else if (Processed.count(User) ||
119 !AddUsersIfInteresting(User)) {
120 DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
121 << " OF SCEV: " << *ISE << '\n');
122 AddUserToIVUsers = true;
135
136 // Otherwise, this is the point where the def-use chain
137 // becomes uninteresting. Call it an IV User.
138 AddUser(User, Op);
123139 }
124
125 if (AddUserToIVUsers) {
126 // Okay, we found a user that we cannot reduce.
127 IVUses.push_back(new IVStrideUse(this, User, I));
128 IVStrideUse &NewUse = IVUses.back();
129 // Transform the expression into a normalized form.
130 ISE = TransformForPostIncUse(NormalizeAutodetect,
131 ISE, User, I,
132 NewUse.PostIncLoops,
133 *SE, *DT);
134 DEBUG(dbgs() << " NORMALIZED TO: " << *ISE << '\n');
135 }
136 }
137 return true;
140 } while (!Worklist.empty());
138141 }
139142
140143 IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
141144 IVUses.push_back(new IVStrideUse(this, User, Operand));
142 return IVUses.back();
145 IVStrideUse &NewUse = IVUses.back();
146
147 // Auto-detect and remember post-inc loops for this expression.
148 const SCEV *S = SE->getSCEV(Operand);
149 (void)TransformForPostIncUse(NormalizeAutodetect,
150 S, User, Operand,
151 NewUse.PostIncLoops,
152 *SE, *DT);
153 return NewUse;
143154 }
144155
145156 IVUsers::IVUsers()
164175 // them by stride. Start by finding all of the PHI nodes in the header for
165176 // this loop. If they are induction variables, inspect their uses.
166177 for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ++I)
167 (void)AddUsersIfInteresting(I);
178 AddUsersIfInteresting(I);
168179
169180 return false;
170181 }
451451 ; CHECK-NEXT: addss %xmm{{.*}}, %xmm{{.*}}
452452 ; CHECK-NEXT: mulss (%r{{[^,]*}}), %xmm{{.*}}
453453 ; CHECK-NEXT: movss %xmm{{.*}}, (%r{{[^,]*}})
454 ; CHECK-NEXT: decq %r{{.*}}
454455 ; CHECK-NEXT: addq $4, %r{{.*}}
455 ; CHECK-NEXT: decq %r{{.*}}
456456 ; CHECK-NEXT: addq $4, %r{{.*}}
457457 ; CHECK-NEXT: movaps %xmm{{.*}}, %xmm{{.*}}
458458 ; CHECK-NEXT: BB10_2: