llvm.org GIT mirror llvm / 198959c
Tighten the invariants around LoopBase::invalidate Summary: With this change: - Methods in LoopBase trip an assert if the receiver has been invalidated - LoopBase::clear frees up the memory held the LoopBase instance This change also shuffles things around as necessary to work with this stricter invariant. Reviewers: chandlerc Subscribers: mehdi_amini, mcrosier, llvm-commits Differential Revision: https://reviews.llvm.org/D38055 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313708 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
9 changed file(s) with 132 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
8080 const LoopBase &
8181 operator=(const LoopBase &) = delete;
8282
83 void clear() {
84 IsInvalid = true;
85 SubLoops.clear();
86 Blocks.clear();
87 DenseBlockSet.clear();
88 ParentLoop = nullptr;
89 }
90
8391 public:
8492 /// This creates an empty loop.
8593 LoopBase() : ParentLoop(nullptr) {}
8896 /// for consistency with loop depth values used for basic blocks, where depth
8997 /// 0 is used for blocks not inside any loops.
9098 unsigned getLoopDepth() const {
99 assert(!isInvalid() && "Loop not in a valid state!");
91100 unsigned D = 1;
92101 for (const LoopT *CurLoop = ParentLoop; CurLoop;
93102 CurLoop = CurLoop->ParentLoop)
94103 ++D;
95104 return D;
96105 }
97 BlockT *getHeader() const { return Blocks.front(); }
106 BlockT *getHeader() const { return getBlocks().front(); }
98107 LoopT *getParentLoop() const { return ParentLoop; }
99108
100109 /// This is a raw interface for bypassing addChildLoop.
101 void setParentLoop(LoopT *L) { ParentLoop = L; }
110 void setParentLoop(LoopT *L) {
111 assert(!isInvalid() && "Loop not in a valid state!");
112 ParentLoop = L;
113 }
102114
103115 /// Return true if the specified loop is contained within in this loop.
104116 bool contains(const LoopT *L) const {
117 assert(!isInvalid() && "Loop not in a valid state!");
105118 if (L == this)
106119 return true;
107120 if (!L)
110123 }
111124
112125 /// Return true if the specified basic block is in this loop.
113 bool contains(const BlockT *BB) const { return DenseBlockSet.count(BB); }
126 bool contains(const BlockT *BB) const {
127 assert(!isInvalid() && "Loop not in a valid state!");
128 return DenseBlockSet.count(BB);
129 }
114130
115131 /// Return true if the specified instruction is in this loop.
116132 template bool contains(const InstT *Inst) const {
118134 }
119135
120136 /// Return the loops contained entirely within this loop.
121 const std::vector &getSubLoops() const { return SubLoops; }
122 std::vector &getSubLoopsVector() { return SubLoops; }
137 const std::vector &getSubLoops() const {
138 assert(!isInvalid() && "Loop not in a valid state!");
139 return SubLoops;
140 }
141 std::vector &getSubLoopsVector() {
142 assert(!isInvalid() && "Loop not in a valid state!");
143 return SubLoops;
144 }
123145 typedef typename std::vector::const_iterator iterator;
124146 typedef
125147 typename std::vector::const_reverse_iterator reverse_iterator;
126 iterator begin() const { return SubLoops.begin(); }
127 iterator end() const { return SubLoops.end(); }
128 reverse_iterator rbegin() const { return SubLoops.rbegin(); }
129 reverse_iterator rend() const { return SubLoops.rend(); }
130 bool empty() const { return SubLoops.empty(); }
148 iterator begin() const { return getSubLoops().begin(); }
149 iterator end() const { return getSubLoops().end(); }
150 reverse_iterator rbegin() const { return getSubLoops().rbegin(); }
151 reverse_iterator rend() const { return getSubLoops().rend(); }
152 bool empty() const { return getSubLoops().empty(); }
131153
132154 /// Get a list of the basic blocks which make up this loop.
133 const std::vector &getBlocks() const { return Blocks; }
155 const std::vector &getBlocks() const {
156 assert(!isInvalid() && "Loop not in a valid state!");
157 return Blocks;
158 }
134159 typedef typename std::vector::const_iterator block_iterator;
135 block_iterator block_begin() const { return Blocks.begin(); }
136 block_iterator block_end() const { return Blocks.end(); }
160 block_iterator block_begin() const { return getBlocks().begin(); }
161 block_iterator block_end() const { return getBlocks().end(); }
137162 inline iterator_range blocks() const {
163 assert(!isInvalid() && "Loop not in a valid state!");
138164 return make_range(block_begin(), block_end());
139165 }
140166
141167 /// Get the number of blocks in this loop in constant time.
142 unsigned getNumBlocks() const { return Blocks.size(); }
143
144168 /// Invalidate the loop, indicating that it is no longer a loop.
145 void invalidate() { IsInvalid = true; }
169 unsigned getNumBlocks() const {
170 assert(!isInvalid() && "Loop not in a valid state!");
171 return Blocks.size();
172 }
146173
147174 /// Return true if this loop is no longer valid.
148 bool isInvalid() { return IsInvalid; }
175 bool isInvalid() const { return IsInvalid; }
149176
150177 /// True if terminator in the block can branch to another block that is
151178 /// outside of the current loop.
152179 bool isLoopExiting(const BlockT *BB) const {
180 assert(!isInvalid() && "Loop not in a valid state!");
153181 for (const auto &Succ : children(BB)) {
154182 if (!contains(Succ))
155183 return true;
162190 /// This function is useful when there are multiple latches in a loop
163191 /// because \fn getLoopLatch will return nullptr in that case.
164192 bool isLoopLatch(const BlockT *BB) const {
193 assert(!isInvalid() && "Loop not in a valid state!");
165194 assert(contains(BB) && "block does not belong to the loop");
166195
167196 BlockT *Header = getHeader();
172201
173202 /// Calculate the number of back edges to the loop header.
174203 unsigned getNumBackEdges() const {
204 assert(!isInvalid() && "Loop not in a valid state!");
175205 unsigned NumBackEdges = 0;
176206 BlockT *H = getHeader();
177207
234264 /// Return all loop latch blocks of this loop. A latch block is a block that
235265 /// contains a branch back to the header.
236266 void getLoopLatches(SmallVectorImpl &LoopLatches) const {
267 assert(!isInvalid() && "Loop not in a valid state!");
237268 BlockT *H = getHeader();
238269 for (const auto Pred : children>(H))
239270 if (contains(Pred))
260291 /// Add the specified loop to be a child of this loop.
261292 /// This updates the loop depth of the new child.
262293 void addChildLoop(LoopT *NewChild) {
294 assert(!isInvalid() && "Loop not in a valid state!");
263295 assert(!NewChild->ParentLoop && "NewChild already has a parent!");
264296 NewChild->ParentLoop = static_cast(this);
265297 SubLoops.push_back(NewChild);
268300 /// This removes the specified child from being a subloop of this loop. The
269301 /// loop is not deleted, as it will presumably be inserted into another loop.
270302 LoopT *removeChildLoop(iterator I) {
303 assert(!isInvalid() && "Loop not in a valid state!");
271304 assert(I != SubLoops.end() && "Cannot remove end iterator!");
272305 LoopT *Child = *I;
273306 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
280313 /// This should only be used by transformations that create new loops. Other
281314 /// transformations should use addBasicBlockToLoop.
282315 void addBlockEntry(BlockT *BB) {
316 assert(!isInvalid() && "Loop not in a valid state!");
283317 Blocks.push_back(BB);
284318 DenseBlockSet.insert(BB);
285319 }
286320
287321 /// interface to reverse Blocks[from, end of loop] in this loop
288322 void reverseBlock(unsigned from) {
323 assert(!isInvalid() && "Loop not in a valid state!");
289324 std::reverse(Blocks.begin() + from, Blocks.end());
290325 }
291326
292327 /// interface to do reserve() for Blocks
293 void reserveBlocks(unsigned size) { Blocks.reserve(size); }
328 void reserveBlocks(unsigned size) {
329 assert(!isInvalid() && "Loop not in a valid state!");
330 Blocks.reserve(size);
331 }
294332
295333 /// This method is used to move BB (which must be part of this loop) to be the
296334 /// loop header of the loop (the block that dominates all others).
297335 void moveToHeader(BlockT *BB) {
336 assert(!isInvalid() && "Loop not in a valid state!");
298337 if (Blocks[0] == BB)
299338 return;
300339 for (unsigned i = 0;; ++i) {
311350 /// Blocks as appropriate. This does not update the mapping in the LoopInfo
312351 /// class.
313352 void removeBlockFromLoop(BlockT *BB) {
353 assert(!isInvalid() && "Loop not in a valid state!");
314354 auto I = find(Blocks, BB);
315355 assert(I != Blocks.end() && "N is not in this list!");
316356 Blocks.erase(I);
493533 LocRange getLocRange() const;
494534
495535 StringRef getName() const {
536 if (isInvalid())
537 return "";
496538 if (BasicBlock *Header = getHeader())
497539 if (Header->hasName())
498540 return Header->getName();
672714 void print(raw_ostream &OS) const;
673715
674716 void verify(const DominatorTreeBase &DomTree) const;
717
718 protected:
719 static void clearLoop(LoopT &L) { L.clear(); }
675720 };
676721
677722 // Implementation in LoopInfoImpl.h
3333 template
3434 void LoopBase::getExitingBlocks(
3535 SmallVectorImpl &ExitingBlocks) const {
36 assert(!isInvalid() && "Loop not in a valid state!");
3637 for (const auto BB : blocks())
3738 for (const auto &Succ : children(BB))
3839 if (!contains(Succ)) {
4647 /// return that block. Otherwise return null.
4748 template
4849 BlockT *LoopBase::getExitingBlock() const {
50 assert(!isInvalid() && "Loop not in a valid state!");
4951 SmallVector ExitingBlocks;
5052 getExitingBlocks(ExitingBlocks);
5153 if (ExitingBlocks.size() == 1)
5961 template
6062 void LoopBase::getExitBlocks(
6163 SmallVectorImpl &ExitBlocks) const {
64 assert(!isInvalid() && "Loop not in a valid state!");
6265 for (const auto BB : blocks())
6366 for (const auto &Succ : children(BB))
6467 if (!contains(Succ))
7073 /// return that block. Otherwise return null.
7174 template
7275 BlockT *LoopBase::getExitBlock() const {
76 assert(!isInvalid() && "Loop not in a valid state!");
7377 SmallVector ExitBlocks;
7478 getExitBlocks(ExitBlocks);
7579 if (ExitBlocks.size() == 1)
8185 template
8286 void LoopBase::getExitEdges(
8387 SmallVectorImpl &ExitEdges) const {
88 assert(!isInvalid() && "Loop not in a valid state!");
8489 for (const auto BB : blocks())
8590 for (const auto &Succ : children(BB))
8691 if (!contains(Succ))
98103 ///
99104 template
100105 BlockT *LoopBase::getLoopPreheader() const {
106 assert(!isInvalid() && "Loop not in a valid state!");
101107 // Keep track of nodes outside the loop branching to the header...
102108 BlockT *Out = getLoopPredecessor();
103109 if (!Out)
125131 ///
126132 template
127133 BlockT *LoopBase::getLoopPredecessor() const {
134 assert(!isInvalid() && "Loop not in a valid state!");
128135 // Keep track of nodes outside the loop branching to the header...
129136 BlockT *Out = nullptr;
130137
147154 /// A latch block is a block that contains a branch back to the header.
148155 template
149156 BlockT *LoopBase::getLoopLatch() const {
157 assert(!isInvalid() && "Loop not in a valid state!");
150158 BlockT *Header = getHeader();
151159 BlockT *Latch = nullptr;
152160 for (const auto Pred : children>(Header)) {
173181 template
174182 void LoopBase::addBasicBlockToLoop(
175183 BlockT *NewBB, LoopInfoBase &LIB) {
184 assert(!isInvalid() && "Loop not in a valid state!");
176185 #ifndef NDEBUG
177186 if (!Blocks.empty()) {
178187 auto SameHeader = LIB[getHeader()];
202211 template
203212 void LoopBase::replaceChildLoopWith(LoopT *OldChild,
204213 LoopT *NewChild) {
214 assert(!isInvalid() && "Loop not in a valid state!");
205215 assert(OldChild->ParentLoop == this && "This loop is already broken!");
206216 assert(!NewChild->ParentLoop && "NewChild already has a parent!");
207217 typename std::vector::iterator I = find(SubLoops, OldChild);
214224 /// verifyLoop - Verify loop structure
215225 template
216226 void LoopBase::verifyLoop() const {
227 assert(!isInvalid() && "Loop not in a valid state!");
217228 #ifndef NDEBUG
218229 assert(!Blocks.empty() && "Loop header is missing");
219230
300311 template
301312 void LoopBase::verifyLoopNest(
302313 DenseSet *Loops) const {
314 assert(!isInvalid() && "Loop not in a valid state!");
303315 Loops->insert(static_cast(this));
304316 // Verify this loop.
305317 verifyLoop();
165165 /// the rest of the pass management infrastructure.
166166 void markLoopAsDeleted(Loop &L) {
167167 LAM.clear(L);
168 assert(CurrentL->contains(&L) && "Cannot delete a loop outside of the "
168 assert(&L == CurrentL ||
169 CurrentL->contains(&L) && "Cannot delete a loop outside of the "
169170 "subloop tree currently being processed.");
170171 if (&L == CurrentL)
171172 SkipCurrentLoop = true;
3838 BasicBlock *ClonedBB, LoopInfo *LI,
3939 NewLoopsMap &NewLoops);
4040
41 bool UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, bool Force,
42 bool AllowRuntime, bool AllowExpensiveTripCount,
43 bool PreserveCondBr, bool PreserveOnlyFirst,
44 unsigned TripMultiple, unsigned PeelCount, bool UnrollRemainder,
45 LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
46 AssumptionCache *AC, OptimizationRemarkEmitter *ORE,
47 bool PreserveLCSSA);
41 /// Represents the result of a \c UnrollLoop invocation.
42 enum class LoopUnrollStatus {
43 /// The loop was not modified.
44 Unmodified,
45
46 /// The loop was partially unrolled -- we still have a loop, but with a
47 /// smaller trip count. We may also have emitted epilogue loop if the loop
48 /// had a non-constant trip count.
49 PartiallyUnrolled,
50
51 /// The loop was fully unrolled into straight-line code. We no longer have
52 /// any back-edges.
53 FullyUnrolled
54 };
55
56 LoopUnrollStatus UnrollLoop(Loop *L, unsigned Count, unsigned TripCount,
57 bool Force, bool AllowRuntime,
58 bool AllowExpensiveTripCount, bool PreserveCondBr,
59 bool PreserveOnlyFirst, unsigned TripMultiple,
60 unsigned PeelCount, bool UnrollRemainder,
61 LoopInfo *LI, ScalarEvolution *SE,
62 DominatorTree *DT, AssumptionCache *AC,
63 OptimizationRemarkEmitter *ORE, bool PreserveLCSSA);
4864
4965 bool UnrollRuntimeLoopRemainder(Loop *L, unsigned Count,
5066 bool AllowExpensiveTripCount,
1515
1616 #include "llvm/Analysis/LoopInfo.h"
1717 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/ScopeExit.h"
1819 #include "llvm/ADT/SmallPtrSet.h"
1920 #include "llvm/Analysis/LoopInfoImpl.h"
2021 #include "llvm/Analysis/LoopIterator.h"
618619
619620 void LoopInfo::markAsErased(Loop *Unloop) {
620621 assert(!Unloop->isInvalid() && "Loop has already been erased!");
621 Unloop->invalidate();
622622 RemovedLoops.push_back(Unloop);
623
624 auto InvalidateOnExit =
625 make_scope_exit([&]() { BaseT::clearLoop(*Unloop); });
623626
624627 // First handle the special case of no parent loop to simplify the algorithm.
625628 if (!Unloop->getParentLoop()) {
197197 LoopWasDeleted = CurrentLoop->isInvalid();
198198
199199 if (Changed)
200 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG,
201 LoopWasDeleted ? ""
202 : CurrentLoop->getHeader()->getName());
200 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, CurrentLoop->getName());
203201 dumpPreservedSet(P);
204202
205203 if (LoopWasDeleted) {
10441044 UP.Count = TripCount;
10451045
10461046 // Unroll the loop.
1047 if (!UnrollLoop(L, UP.Count, TripCount, UP.Force, UP.Runtime,
1048 UP.AllowExpensiveTripCount, UseUpperBound, MaxOrZero,
1049 TripMultiple, UP.PeelCount, UP.UnrollRemainder,
1050 LI, &SE, &DT, &AC, &ORE,
1051 PreserveLCSSA))
1047 LoopUnrollStatus UnrollStatus = UnrollLoop(
1048 L, UP.Count, TripCount, UP.Force, UP.Runtime, UP.AllowExpensiveTripCount,
1049 UseUpperBound, MaxOrZero, TripMultiple, UP.PeelCount, UP.UnrollRemainder,
1050 LI, &SE, &DT, &AC, &ORE, PreserveLCSSA);
1051 if (UnrollStatus == LoopUnrollStatus::Unmodified)
10521052 return false;
10531053
10541054 // If loop has an unroll count pragma or unrolled by explicitly set count
10551055 // mark loop as unrolled to prevent unrolling beyond that requested.
10561056 // If the loop was peeled, we already "used up" the profile information
10571057 // we had, so we don't want to unroll or peel again.
1058 if (IsCountSetExplicitly || UP.PeelCount)
1058 if (UnrollStatus != LoopUnrollStatus::FullyUnrolled &&
1059 (IsCountSetExplicitly || UP.PeelCount))
10591060 SetLoopAlreadyUnrolled(L);
10601061
10611062 return true;
254254 return false;
255255 }
256256
257 /// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
258 /// if unrolling was successful, or false if the loop was unmodified. Unrolling
257 /// Unroll the given loop by Count. The loop must be in LCSSA form. Unrolling
259258 /// can only fail when the loop's latch block is not terminated by a conditional
260259 /// branch instruction. However, if the trip count (and multiple) are not known,
261260 /// loop unrolling will mostly produce more code that is no faster.
284283 /// runtime-unroll the loop if computing RuntimeTripCount will be expensive and
285284 /// AllowExpensiveTripCount is false.
286285 ///
287 /// If we want to perform PGO-based loop peeling, PeelCount is set to the
286 /// If we want to perform PGO-based loop peeling, PeelCount is set to the
288287 /// number of iterations we want to peel off.
289288 ///
290289 /// The LoopInfo Analysis that is passed will be kept consistent.
291290 ///
292291 /// This utility preserves LoopInfo. It will also preserve ScalarEvolution and
293292 /// DominatorTree if they are non-null.
294 bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, bool Force,
295 bool AllowRuntime, bool AllowExpensiveTripCount,
296 bool PreserveCondBr, bool PreserveOnlyFirst,
297 unsigned TripMultiple, unsigned PeelCount,
298 bool UnrollRemainder, LoopInfo *LI,
299 ScalarEvolution *SE, DominatorTree *DT,
300 AssumptionCache *AC, OptimizationRemarkEmitter *ORE,
301 bool PreserveLCSSA) {
293 LoopUnrollStatus llvm::UnrollLoop(
294 Loop *L, unsigned Count, unsigned TripCount, bool Force, bool AllowRuntime,
295 bool AllowExpensiveTripCount, bool PreserveCondBr, bool PreserveOnlyFirst,
296 unsigned TripMultiple, unsigned PeelCount, bool UnrollRemainder,
297 LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
298 OptimizationRemarkEmitter *ORE, bool PreserveLCSSA) {
302299
303300 BasicBlock *Preheader = L->getLoopPreheader();
304301 if (!Preheader) {
305302 DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n");
306 return false;
303 return LoopUnrollStatus::Unmodified;
307304 }
308305
309306 BasicBlock *LatchBlock = L->getLoopLatch();
310307 if (!LatchBlock) {
311308 DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n");
312 return false;
309 return LoopUnrollStatus::Unmodified;
313310 }
314311
315312 // Loops with indirectbr cannot be cloned.
316313 if (!L->isSafeToClone()) {
317314 DEBUG(dbgs() << " Can't unroll; Loop body cannot be cloned.\n");
318 return false;
315 return LoopUnrollStatus::Unmodified;
319316 }
320317
321318 // The current loop unroll pass can only unroll loops with a single latch
329326 // The loop-rotate pass can be helpful to avoid this in many cases.
330327 DEBUG(dbgs() <<
331328 " Can't unroll; loop not terminated by a conditional branch.\n");
332 return false;
329 return LoopUnrollStatus::Unmodified;
333330 }
334331
335332 auto CheckSuccessors = [&](unsigned S1, unsigned S2) {
339336 if (!CheckSuccessors(0, 1) && !CheckSuccessors(1, 0)) {
340337 DEBUG(dbgs() << "Can't unroll; only loops with one conditional latch"
341338 " exiting the loop can be unrolled\n");
342 return false;
339 return LoopUnrollStatus::Unmodified;
343340 }
344341
345342 if (Header->hasAddressTaken()) {
346343 // The loop-rotate pass can be helpful to avoid this in many cases.
347344 DEBUG(dbgs() <<
348345 " Won't unroll loop: address of header block is taken.\n");
349 return false;
346 return LoopUnrollStatus::Unmodified;
350347 }
351348
352349 if (TripCount != 0)
362359 // Don't enter the unroll code if there is nothing to do.
363360 if (TripCount == 0 && Count < 2 && PeelCount == 0) {
364361 DEBUG(dbgs() << "Won't unroll; almost nothing to do\n");
365 return false;
362 return LoopUnrollStatus::Unmodified;
366363 }
367364
368365 assert(Count > 0);
438435 DEBUG(
439436 dbgs() << "Wont unroll; remainder loop could not be generated"
440437 "when assuming runtime trip count\n");
441 return false;
438 return LoopUnrollStatus::Unmodified;
442439 }
443440 }
444441
863860 }
864861 }
865862
866 return true;
863 return CompletelyUnroll ? LoopUnrollStatus::FullyUnrolled
864 : LoopUnrollStatus::PartiallyUnrolled;
867865 }
868866
869867 /// Given an llvm.loop loop id metadata node, returns the loop hint metadata
2121 ; CHECK: Running analysis: LoopAccessAnalysis on outer.header
2222 ; CHECK: Finished Loop pass manager run.
2323 ; CHECK: Running pass: LoopUnrollPass
24 ; CHECK: Clearing all analysis results for: inner2.header
25 ; CHECK: Clearing all analysis results for: outer.header
24 ; CHECK: Clearing all analysis results for:
25 ; CHECK: Clearing all analysis results for:
2626 ; CHECK: Invalidating all non-preserved analyses for: test
2727 ; CHECK: Invalidating all non-preserved analyses for: inner1.header
2828 ; CHECK: Invalidating analysis: LoopAccessAnalysis on inner1.header