llvm.org GIT mirror llvm / fef8b01
[PM/LoopUnswitch] Fix a bug in the loop block set formation of the new loop unswitch. This code incorrectly added the header to the loop block set early. As a consequence we would incorrectly conclude that a nested loop body had already been visited when the header of the outer loop was the preheader of the nested loop. In retrospect, adding the header eagerly doesn't really make sense. It seems nicer to let the cycle be formed naturally. This will catch crazy bugs in the CFG reconstruction where we can't correctly form the cycle earlier rather than later, and makes the rest of the logic just fall out. I've also added various asserts that make these issues *much* easier to debug. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330707 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 1 year, 4 months ago
2 changed file(s) with 64 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
13321332 if (LoopBlockSet.empty())
13331333 return LoopBlockSet;
13341334
1335 // Add the loop header to the set.
1336 LoopBlockSet.insert(Header);
1337
13381335 // We found backedges, recurse through them to identify the loop blocks.
13391336 while (!Worklist.empty()) {
13401337 BasicBlock *BB = Worklist.pop_back_val();
13411338 assert(LoopBlockSet.count(BB) && "Didn't put block into the loop set!");
1339
1340 // No need to walk past the header.
1341 if (BB == Header)
1342 continue;
13421343
13431344 // Because we know the inner loop structure remains valid we can use the
13441345 // loop structure to jump immediately across the entire nested loop.
13871388 Worklist.push_back(Pred);
13881389 }
13891390
1391 assert(LoopBlockSet.count(Header) && "Cannot fail to add the header!");
1392
13901393 // We've found all the blocks participating in the loop, return our completed
13911394 // set.
13921395 return LoopBlockSet;
17911794 // unnecessary loops.
17921795 auto UpdateLCSSA = [&](Loop &UpdateL) {
17931796 #ifndef NDEBUG
1794 for (Loop *ChildL : UpdateL)
1797 UpdateL.verifyLoop();
1798 for (Loop *ChildL : UpdateL) {
1799 ChildL->verifyLoop();
17951800 assert(ChildL->isRecursivelyLCSSAForm(DT, LI) &&
17961801 "Perturbed a child loop's LCSSA form!");
1802 }
17971803 #endif
17981804 formLCSSA(UpdateL, DT, &LI, nullptr);
17991805 };
25072507 call void @g()
25082508 ret void
25092509 }
2510
2511 ; Test that when we are unswitching and need to rebuild the loop block set we
2512 ; correctly skip past inner loops. We want to use the inner loop to efficiently
2513 ; skip whole subregions of the outer loop blocks but just because the header of
2514 ; the outer loop is also the preheader of an inner loop shouldn't confuse this
2515 ; walk.
2516 define void @test23(i1 %arg, i1* %ptr) {
2517 ; CHECK-LABEL: define void @test23(
2518 entry:
2519 br label %outer.header
2520 ; CHECK: entry:
2521 ; CHECK-NEXT: br i1 %arg,
2522 ;
2523 ; Just verify that we unswitched the correct bits. We should call `@f` twice in
2524 ; one unswitch and `@f` and then `@g` in the other.
2525 ; CHECK: call void
2526 ; CHECK-SAME: @f
2527 ; CHECK: call void
2528 ; CHECK-SAME: @f
2529 ;
2530 ; CHECK: call void
2531 ; CHECK-SAME: @f
2532 ; CHECK: call void
2533 ; CHECK-SAME: @g
2534
2535 outer.header:
2536 br label %inner.header
2537
2538 inner.header:
2539 call void @f()
2540 br label %inner.latch
2541
2542 inner.latch:
2543 %inner.cond = load i1, i1* %ptr
2544 br i1 %inner.cond, label %inner.header, label %outer.body
2545
2546 outer.body:
2547 br i1 %arg, label %outer.body.left, label %outer.body.right
2548
2549 outer.body.left:
2550 call void @f()
2551 br label %outer.latch
2552
2553 outer.body.right:
2554 call void @g()
2555 br label %outer.latch
2556
2557 outer.latch:
2558 %outer.cond = load i1, i1* %ptr
2559 br i1 %outer.cond, label %outer.header, label %exit
2560
2561 exit:
2562 ret void
2563 }