llvm.org GIT mirror llvm / f0608d8
Move getUniqueExitBlocks from LoopBase to Loop, since they depend on LoopSimplify form, which is currently only available on Loops (and not MachineLoops). Also, move the code out of the header file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80923 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
2 changed file(s) with 78 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
229229 ExitEdges.push_back(std::make_pair(*BI, *I));
230230 }
231231
232 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
233 /// These are the blocks _outside of the current loop_ which are branched to.
234 /// This assumes that loop is in canonical form.
235 ///
236 void getUniqueExitBlocks(SmallVectorImpl &ExitBlocks) const {
237 // Sort the blocks vector so that we can use binary search to do quick
238 // lookups.
239 SmallVector LoopBBs(block_begin(), block_end());
240 std::sort(LoopBBs.begin(), LoopBBs.end());
241
242 std::vector switchExitBlocks;
243
244 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
245
246 BlockT *current = *BI;
247 switchExitBlocks.clear();
248
249 typedef GraphTraits BlockTraits;
250 typedef GraphTraits > InvBlockTraits;
251 for (typename BlockTraits::ChildIteratorType I =
252 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
253 I != E; ++I) {
254 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
255 // If block is inside the loop then it is not a exit block.
256 continue;
257
258 typename InvBlockTraits::ChildIteratorType PI =
259 InvBlockTraits::child_begin(*I);
260 BlockT *firstPred = *PI;
261
262 // If current basic block is this exit block's first predecessor
263 // then only insert exit block in to the output ExitBlocks vector.
264 // This ensures that same exit block is not inserted twice into
265 // ExitBlocks vector.
266 if (current != firstPred)
267 continue;
268
269 // If a terminator has more then two successors, for example SwitchInst,
270 // then it is possible that there are multiple edges from current block
271 // to one exit block.
272 if (std::distance(BlockTraits::child_begin(current),
273 BlockTraits::child_end(current)) <= 2) {
274 ExitBlocks.push_back(*I);
275 continue;
276 }
277
278 // In case of multiple edges from current block to exit block, collect
279 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
280 // duplicate edges.
281 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
282 == switchExitBlocks.end()) {
283 switchExitBlocks.push_back(*I);
284 ExitBlocks.push_back(*I);
285 }
286 }
287 }
288 }
289
290 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
291 /// block, return that block. Otherwise return null.
292 BlockT *getUniqueExitBlock() const {
293 SmallVector UniqueExitBlocks;
294 getUniqueExitBlocks(UniqueExitBlocks);
295 if (UniqueExitBlocks.size() == 1)
296 return UniqueExitBlocks[0];
297 return 0;
298 }
299
300232 /// getLoopPreheader - If there is a preheader for this loop, return it. A
301233 /// loop has a preheader if there is only one edge to the header of the loop
302234 /// from outside of the loop. If this is the case, the block branching to the
567499 /// the LoopSimplify form transforms loops to, which is sometimes called
568500 /// normal form.
569501 bool isLoopSimplifyForm() const;
502
503 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
504 /// These are the blocks _outside of the current loop_ which are branched to.
505 /// This assumes that loop is in canonical form.
506 ///
507 void getUniqueExitBlocks(SmallVectorImpl &ExitBlocks) const;
508
509 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
510 /// block, return that block. Otherwise return null.
511 BasicBlock *getUniqueExitBlock() const;
570512
571513 private:
572514 friend class LoopInfoBase;
293293 return true;
294294 }
295295
296 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
297 /// These are the blocks _outside of the current loop_ which are branched to.
298 /// This assumes that loop is in canonical form.
299 ///
300 void
301 Loop::getUniqueExitBlocks(SmallVectorImpl &ExitBlocks) const {
302 // Sort the blocks vector so that we can use binary search to do quick
303 // lookups.
304 SmallVector LoopBBs(block_begin(), block_end());
305 std::sort(LoopBBs.begin(), LoopBBs.end());
306
307 std::vector switchExitBlocks;
308
309 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
310
311 BasicBlock *current = *BI;
312 switchExitBlocks.clear();
313
314 typedef GraphTraits BlockTraits;
315 typedef GraphTraits > InvBlockTraits;
316 for (BlockTraits::ChildIteratorType I =
317 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
318 I != E; ++I) {
319 // If block is inside the loop then it is not a exit block.
320 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
321 continue;
322
323 InvBlockTraits::ChildIteratorType PI = InvBlockTraits::child_begin(*I);
324 BasicBlock *firstPred = *PI;
325
326 // If current basic block is this exit block's first predecessor
327 // then only insert exit block in to the output ExitBlocks vector.
328 // This ensures that same exit block is not inserted twice into
329 // ExitBlocks vector.
330 if (current != firstPred)
331 continue;
332
333 // If a terminator has more then two successors, for example SwitchInst,
334 // then it is possible that there are multiple edges from current block
335 // to one exit block.
336 if (std::distance(BlockTraits::child_begin(current),
337 BlockTraits::child_end(current)) <= 2) {
338 ExitBlocks.push_back(*I);
339 continue;
340 }
341
342 // In case of multiple edges from current block to exit block, collect
343 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
344 // duplicate edges.
345 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
346 == switchExitBlocks.end()) {
347 switchExitBlocks.push_back(*I);
348 ExitBlocks.push_back(*I);
349 }
350 }
351 }
352 }
353
354 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
355 /// block, return that block. Otherwise return null.
356 BasicBlock *Loop::getUniqueExitBlock() const {
357 SmallVector UniqueExitBlocks;
358 getUniqueExitBlocks(UniqueExitBlocks);
359 if (UniqueExitBlocks.size() == 1)
360 return UniqueExitBlocks[0];
361 return 0;
362 }
363
296364 //===----------------------------------------------------------------------===//
297365 // LoopInfo implementation
298366 //