llvm.org GIT mirror llvm / 9dc2b94
[LoopSimplify] Factor the logic to form dedicated exits into a utility. I want to use the same logic as LoopSimplify to form dedicated exits in another pass (SimpleLoopUnswitch) so I wanted to factor it out here. I also noticed that there is a pretty significantly more efficient way to implement this than the way the code in LoopSimplify worked. We don't need to actually retain the set of unique exit blocks, we can just rewrite them as we find them and use only a set to deduplicate. This did require changing one part of LoopSimplify to not re-use the unique set of exits, but it only used it to check that there was a single unique exit. That part of the code is about to walk the exiting blocks anyways, so it seemed better to rewrite it to use those exiting blocks to compute this property on-demand. I also had to ditch a statistic, but it doesn't seem terribly valuable. Differential Revision: https://reviews.llvm.org/D34049 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306081 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 2 years ago
6 changed file(s) with 91 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
361361 BasicBlock *InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI,
362362 bool PreserveLCSSA);
363363
364 /// Ensure that all exit blocks of the loop are dedicated exits.
365 ///
366 /// For any loop exit block with non-loop predecessors, we split the loop
367 /// predecessors to use a dedicated loop exit block. We update the dominator
368 /// tree and loop info if provided, and will preserve LCSSA if requested.
369 bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
370 bool PreserveLCSSA);
371
364372 /// Ensures LCSSA form for every instruction from the Worklist in the scope of
365373 /// innermost containing loop.
366374 ///
7171
7272 #define DEBUG_TYPE "loop-simplify"
7373
74 STATISTIC(NumInserted, "Number of pre-header or exit blocks inserted");
7574 STATISTIC(NumNested , "Number of nested loops split out");
7675
7776 // If the block isn't already, move the new block to right after some 'outside
149148 placeSplitBlockCarefully(PreheaderBB, OutsideBlocks, L);
150149
151150 return PreheaderBB;
152 }
153
154 /// \brief Ensure that the loop preheader dominates all exit blocks.
155 ///
156 /// This method is used to split exit blocks that have predecessors outside of
157 /// the loop.
158 static BasicBlock *rewriteLoopExitBlock(Loop *L, BasicBlock *Exit,
159 DominatorTree *DT, LoopInfo *LI,
160 bool PreserveLCSSA) {
161 SmallVector LoopBlocks;
162 for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {
163 BasicBlock *P = *I;
164 if (L->contains(P)) {
165 // Don't do this if the loop is exited via an indirect branch.
166 if (isa(P->getTerminator())) return nullptr;
167
168 LoopBlocks.push_back(P);
169 }
170 }
171
172 assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?");
173 BasicBlock *NewExitBB = nullptr;
174
175 NewExitBB = SplitBlockPredecessors(Exit, LoopBlocks, ".loopexit", DT, LI,
176 PreserveLCSSA);
177 if (!NewExitBB)
178 return nullptr;
179
180 DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
181 << NewExitBB->getName() << "\n");
182 return NewExitBB;
183151 }
184152
185153 /// Add the specified block, and all of its predecessors, to the specified set,
345313
346314 // Split edges to exit blocks from the inner loop, if they emerged in the
347315 // process of separating the outer one.
348 SmallVector ExitBlocks;
349 L->getExitBlocks(ExitBlocks);
350 SmallSetVector ExitBlockSet(ExitBlocks.begin(),
351 ExitBlocks.end());
352 for (BasicBlock *ExitBlock : ExitBlockSet) {
353 if (any_of(predecessors(ExitBlock),
354 [L](BasicBlock *BB) { return !L->contains(BB); })) {
355 rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
356 }
357 }
316 formDedicatedExitBlocks(L, DT, LI, PreserveLCSSA);
358317
359318 if (PreserveLCSSA) {
360319 // Fix LCSSA form for L. Some values, which previously were only used inside
562521 BasicBlock *Preheader = L->getLoopPreheader();
563522 if (!Preheader) {
564523 Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);
565 if (Preheader) {
566 ++NumInserted;
524 if (Preheader)
567525 Changed = true;
568 }
569526 }
570527
571528 // Next, check to make sure that all exit nodes of the loop only have
572529 // predecessors that are inside of the loop. This check guarantees that the
573530 // loop preheader/header will dominate the exit blocks. If the exit block has
574531 // predecessors from outside of the loop, split the edge now.
575 SmallVector ExitBlocks;
576 L->getExitBlocks(ExitBlocks);
577
578 SmallSetVector ExitBlockSet(ExitBlocks.begin(),
579 ExitBlocks.end());
580 for (BasicBlock *ExitBlock : ExitBlockSet) {
581 if (any_of(predecessors(ExitBlock),
582 [L](BasicBlock *BB) { return !L->contains(BB); })) {
583 rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
584 ++NumInserted;
585 Changed = true;
586 }
587 }
532 if (formDedicatedExitBlocks(L, DT, LI, PreserveLCSSA))
533 Changed = true;
588534
589535 // If the header has more than two predecessors at this point (from the
590536 // preheader and from multiple backedges), we must adjust the loop.
613559 // insert a new block that all backedges target, then make it jump to the
614560 // loop header.
615561 LoopLatch = insertUniqueBackedgeBlock(L, Preheader, DT, LI);
616 if (LoopLatch) {
617 ++NumInserted;
562 if (LoopLatch)
618563 Changed = true;
619 }
620564 }
621565
622566 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
644588 // loop-invariant instructions out of the way to open up more
645589 // opportunities, and the disadvantage of having the responsibility
646590 // to preserve dominator information.
647 if (ExitBlockSet.size() == 1) {
591 auto HasUniqueExitBlock = [&]() {
592 BasicBlock *UniqueExit = nullptr;
593 for (auto *ExitingBB : ExitingBlocks)
594 for (auto *SuccBB : successors(ExitingBB)) {
595 if (L->contains(SuccBB))
596 continue;
597
598 if (!UniqueExit)
599 UniqueExit = SuccBB;
600 else if (UniqueExit != SuccBB)
601 return false;
602 }
603
604 return true;
605 };
606 if (HasUniqueExitBlock()) {
648607 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
649608 BasicBlock *ExitingBlock = ExitingBlocks[i];
650609 if (!ExitingBlock->getSinglePredecessor()) continue;
2828 #include "llvm/IR/ValueHandle.h"
2929 #include "llvm/Pass.h"
3030 #include "llvm/Support/Debug.h"
31 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3132
3233 using namespace llvm;
3334 using namespace llvm::PatternMatch;
920921 true /* signed */);
921922 D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue);
922923 return true;
924 }
925
926 bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
927 bool PreserveLCSSA) {
928 bool Changed = false;
929
930 // We re-use a vector for the in-loop predecesosrs.
931 SmallVector InLoopPredecessors;
932
933 auto RewriteExit = [&](BasicBlock *BB) {
934 // See if there are any non-loop predecessors of this exit block and
935 // keep track of the in-loop predecessors.
936 bool IsDedicatedExit = true;
937 for (auto *PredBB : predecessors(BB))
938 if (L->contains(PredBB)) {
939 if (isa(PredBB->getTerminator()))
940 // We cannot rewrite exiting edges from an indirectbr.
941 return false;
942
943 InLoopPredecessors.push_back(PredBB);
944 } else {
945 IsDedicatedExit = false;
946 }
947
948 // Nothing to do if this is already a dedicated exit.
949 if (IsDedicatedExit) {
950 InLoopPredecessors.clear();
951 return false;
952 }
953
954 assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
955 auto *NewExitBB = SplitBlockPredecessors(
956 BB, InLoopPredecessors, ".loopexit", DT, LI, PreserveLCSSA);
957
958 if (!NewExitBB)
959 DEBUG(dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
960 << *L << "\n");
961 else
962 DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
963 << NewExitBB->getName() << "\n");
964 InLoopPredecessors.clear();
965 return true;
966 };
967
968 // Walk the exit blocks directly rather than building up a data structure for
969 // them, but only visit each one once.
970 SmallPtrSet Visited;
971 for (auto *BB : L->blocks())
972 for (auto *SuccBB : successors(BB)) {
973 // We're looking for exit blocks so skip in-loop successors.
974 if (L->contains(SuccBB))
975 continue;
976
977 // Visit each exit block exactly once.
978 if (!Visited.insert(SuccBB).second)
979 continue;
980
981 Changed |= RewriteExit(SuccBB);
982 }
983
984 return Changed;
923985 }
924986
925987 /// \brief Returns the instructions that use values defined in the loop.
11 ; RUN: opt -loop-unswitch -disable-output -stats -info-output-file - < %s | FileCheck --check-prefix=STATS %s
22 ; RUN: opt -S -loop-unswitch -verify-loop-info -verify-dom-info < %s | FileCheck %s
33
4 ; STATS: 1 loop-simplify - Number of pre-header or exit blocks inserted
54 ; STATS: 2 loop-unswitch - Number of switches unswitched
65
76 ; CHECK: %1 = icmp eq i32 %c, 1
11 ; RUN: opt -loop-unswitch -loop-unswitch-threshold 13 -disable-output -stats -info-output-file - < %s | FileCheck --check-prefix=STATS %s
22 ; RUN: opt -S -loop-unswitch -loop-unswitch-threshold 13 -verify-loop-info -verify-dom-info < %s | FileCheck %s
33
4 ; STATS: 1 loop-simplify - Number of pre-header or exit blocks inserted
54 ; STATS: 1 loop-unswitch - Number of switches unswitched
65
76 ; ModuleID = '../llvm/test/Transforms/LoopUnswitch/2011-11-18-TwoSwitches.ll'
11 ; RUN: opt -loop-unswitch -loop-unswitch-threshold 1000 -disable-output -stats -info-output-file - < %s | FileCheck --check-prefix=STATS %s
22 ; RUN: opt -S -loop-unswitch -loop-unswitch-threshold 1000 -verify-loop-info -verify-dom-info < %s | FileCheck %s
33
4 ; STATS: 1 loop-simplify - Number of pre-header or exit blocks inserted
54 ; STATS: 3 loop-unswitch - Number of switches unswitched
65
76 ; CHECK: %1 = icmp eq i32 %c, 1