llvm.org GIT mirror llvm / 16295fc
Tweak the loop rotation logic to check whether the loop is naturally laid out in a form with a fallthrough into the header and a fallthrough out of the bottom. In that case, leave the loop alone because any rotation will introduce unnecessary branches. If either side looks like it will require an explicit branch, then the rotation won't add any, do it to ensure the branch occurs outside of the loop (if possible) and maximize the benefit of the fallthrough in the bottom. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154806 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 7 years ago
2 changed file(s) with 83 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
214214 MachineLoop &L,
215215 const BlockFilterSet &LoopBlockSet);
216216 void buildLoopChains(MachineFunction &F, MachineLoop &L);
217 void rotateLoop(BlockChain &LoopChain, MachineBasicBlock *ExitingBB,
218 const BlockFilterSet &LoopBlockSet);
217219 void buildCFGChains(MachineFunction &F);
218220
219221 public:
658660 return ExitingBB;
659661 }
660662
663 /// \brief Attempt to rotate an exiting block to the bottom of the loop.
664 ///
665 /// Once we have built a chain, try to rotate it to line up the hot exit block
666 /// with fallthrough out of the loop if doing so doesn't introduce unnecessary
667 /// branches. For example, if the loop has fallthrough into its header and out
668 /// of its bottom already, don't rotate it.
669 void MachineBlockPlacement::rotateLoop(BlockChain &LoopChain,
670 MachineBasicBlock *ExitingBB,
671 const BlockFilterSet &LoopBlockSet) {
672 if (!ExitingBB)
673 return;
674
675 MachineBasicBlock *Top = *LoopChain.begin();
676 bool ViableTopFallthrough = false;
677 for (MachineBasicBlock::pred_iterator PI = Top->pred_begin(),
678 PE = Top->pred_end();
679 PI != PE; ++PI) {
680 BlockChain *PredChain = BlockToChain[*PI];
681 if (!LoopBlockSet.count(*PI) &&
682 (!PredChain || *PI == *llvm::prior(PredChain->end()))) {
683 ViableTopFallthrough = true;
684 break;
685 }
686 }
687
688 // If the header has viable fallthrough, check whether the current loop
689 // bottom is a viable exiting block. If so, bail out as rotating will
690 // introduce an unnecessary branch.
691 if (ViableTopFallthrough) {
692 MachineBasicBlock *Bottom = *llvm::prior(LoopChain.end());
693 for (MachineBasicBlock::succ_iterator SI = Bottom->succ_begin(),
694 SE = Bottom->succ_end();
695 SI != SE; ++SI) {
696 BlockChain *SuccChain = BlockToChain[*SI];
697 if (!LoopBlockSet.count(*SI) &&
698 (!SuccChain || *SI == *SuccChain->begin()))
699 return;
700 }
701 }
702
703 BlockChain::iterator ExitIt = std::find(LoopChain.begin(), LoopChain.end(),
704 ExitingBB);
705 if (ExitIt == LoopChain.end())
706 return;
707
708 std::rotate(LoopChain.begin(), llvm::next(ExitIt), LoopChain.end());
709 }
710
661711 /// \brief Forms basic block chains from the natural loop structures.
662712 ///
663713 /// These chains are designed to preserve the existing *structure* of the code
708758 }
709759
710760 buildChain(L.getHeader(), LoopChain, BlockWorkList, &LoopBlockSet);
711
712 // Once we have built a chain, try to rotate it to line up the hot exit block
713 // with fallthrough out of the loop (if we have a valid exit block for that).
714 if (ExitingBB) {
715 BlockChain::iterator ExitIt = std::find(LoopChain.begin(), LoopChain.end(),
716 ExitingBB);
717
718 if (ExitIt != LoopChain.end()) {
719 std::rotate(LoopChain.begin(), llvm::next(ExitIt), LoopChain.end());
720 }
721 }
761 rotateLoop(LoopChain, ExitingBB, LoopBlockSet);
722762
723763 DEBUG({
724764 // Crash at the end so we get all of the debugging output first.
121121 ; Check that we sink early exit blocks out of loop bodies.
122122 ; CHECK: test_loop_early_exits:
123123 ; CHECK: %entry
124 ; CHECK: %body1
124125 ; CHECK: %body2
125126 ; CHECK: %body3
126127 ; CHECK: %body4
127 ; CHECK: %body1
128 ; CHECK: %exit
128129 ; CHECK: %bail1
129130 ; CHECK: %bail2
130131 ; CHECK: %bail3
131 ; CHECK: %exit
132132
133133 entry:
134134 br label %body1
193193 %sum = add nsw i32 %0, %base
194194 %bailcond1 = icmp eq i32 %sum, 42
195195 br label %body0
196
197 exit:
198 ret i32 %base
199 }
200
201 define i32 @test_no_loop_rotate(i32 %i, i32* %a) {
202 ; Check that we don't try to rotate a loop which is already laid out with
203 ; fallthrough opportunities into the top and out of the bottom.
204 ; CHECK: test_no_loop_rotate:
205 ; CHECK: %entry
206 ; CHECK: %body0
207 ; CHECK: %body1
208 ; CHECK: %exit
209
210 entry:
211 br label %body0
212
213 body0:
214 %iv = phi i32 [ 0, %entry ], [ %next, %body1 ]
215 %base = phi i32 [ 0, %entry ], [ %sum, %body1 ]
216 %arrayidx = getelementptr inbounds i32* %a, i32 %iv
217 %0 = load i32* %arrayidx
218 %sum = add nsw i32 %0, %base
219 %bailcond1 = icmp eq i32 %sum, 42
220 br i1 %bailcond1, label %exit, label %body1
221
222 body1:
223 %next = add i32 %iv, 1
224 %exitcond = icmp eq i32 %next, %i
225 br i1 %exitcond, label %exit, label %body0
196226
197227 exit:
198228 ret i32 %base