@@ -181,7 +181,7 @@ llvm::UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
181
181
182
182
// Don't enter the unroll code if there is nothing to do.
183
183
if (TripCount == 0 && Count < 2 ) {
184
- LLVM_DEBUG (dbgs () << " Won't unroll; almost nothing to do\n " );
184
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam ; almost nothing to do\n " );
185
185
return LoopUnrollResult::Unmodified;
186
186
}
187
187
@@ -619,16 +619,28 @@ static bool checkDependencies(SmallVector<Value *, 4> &Earlier,
619
619
if (auto D = DI.depends (Src, Dst, true )) {
620
620
assert (D->isOrdered () && " Expected an output, flow or anti dep." );
621
621
622
- if (D->isConfused ())
622
+ if (D->isConfused ()) {
623
+ LLVM_DEBUG (dbgs () << " Confused dependency between:\n "
624
+ << " " << *Src << " \n "
625
+ << " " << *Dst << " \n " );
623
626
return false ;
627
+ }
624
628
if (!InnerLoop) {
625
- if (D->getDirection (LoopDepth) & Dependence::DVEntry::GT)
629
+ if (D->getDirection (LoopDepth) & Dependence::DVEntry::GT) {
630
+ LLVM_DEBUG (dbgs () << " > dependency between:\n "
631
+ << " " << *Src << " \n "
632
+ << " " << *Dst << " \n " );
626
633
return false ;
634
+ }
627
635
} else {
628
636
assert (LoopDepth + 1 <= D->getLevels ());
629
637
if (D->getDirection (LoopDepth) & Dependence::DVEntry::GT &&
630
- D->getDirection (LoopDepth + 1 ) & Dependence::DVEntry::LT)
638
+ D->getDirection (LoopDepth + 1 ) & Dependence::DVEntry::LT) {
639
+ LLVM_DEBUG (dbgs () << " < > dependency between:\n "
640
+ << " " << *Src << " \n "
641
+ << " " << *Dst << " \n " );
631
642
return false ;
643
+ }
632
644
}
633
645
}
634
646
}
@@ -716,38 +728,58 @@ bool llvm::isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
716
728
if (SubLoopLatch != SubLoopExit)
717
729
return false ;
718
730
719
- if (Header->hasAddressTaken () || SubLoopHeader->hasAddressTaken ())
731
+ if (Header->hasAddressTaken () || SubLoopHeader->hasAddressTaken ()) {
732
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam; Address taken\n " );
720
733
return false ;
734
+ }
721
735
722
736
// Split blocks into Fore/SubLoop/Aft based on dominators
723
737
BasicBlockSet SubLoopBlocks;
724
738
BasicBlockSet ForeBlocks;
725
739
BasicBlockSet AftBlocks;
726
740
if (!partitionOuterLoopBlocks (L, SubLoop, ForeBlocks, SubLoopBlocks,
727
- AftBlocks, &DT))
741
+ AftBlocks, &DT)) {
742
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam; Incompatible loop layout\n " );
728
743
return false ;
744
+ }
729
745
730
746
// Aft blocks may need to move instructions to fore blocks, which becomes more
731
747
// difficult if there are multiple (potentially conditionally executed)
732
748
// blocks. For now we just exclude loops with multiple aft blocks.
733
- if (AftBlocks.size () != 1 )
749
+ if (AftBlocks.size () != 1 ) {
750
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam; Can't currently handle "
751
+ " multiple blocks after the loop\n " );
734
752
return false ;
753
+ }
735
754
736
- // Check inner loop IV is consistent between all iterations
737
- const SCEV *SubLoopBECountSC = SE.getExitCount (SubLoop, SubLoopLatch);
738
- if (isa<SCEVCouldNotCompute>(SubLoopBECountSC) ||
739
- !SubLoopBECountSC->getType ()->isIntegerTy ())
740
- return false ;
741
- ScalarEvolution::LoopDisposition LD =
742
- SE.getLoopDisposition (SubLoopBECountSC, L);
743
- if (LD != ScalarEvolution::LoopInvariant)
755
+ // Check inner loop backedge count is consistent on all iterations of the
756
+ // outer loop
757
+ auto CheckInnerLoopIterationCountInvariant = [](Loop *SubLoop, Loop *OuterL,
758
+ ScalarEvolution &SE) {
759
+ BasicBlock *SubLoopLatch = SubLoop->getLoopLatch ();
760
+ const SCEV *SubLoopBECountSC = SE.getExitCount (SubLoop, SubLoopLatch);
761
+ if (isa<SCEVCouldNotCompute>(SubLoopBECountSC) ||
762
+ !SubLoopBECountSC->getType ()->isIntegerTy ())
763
+ return false ;
764
+ ScalarEvolution::LoopDisposition LD =
765
+ SE.getLoopDisposition (SubLoopBECountSC, OuterL);
766
+ if (LD != ScalarEvolution::LoopInvariant)
767
+ return false ;
768
+ return true ;
769
+ };
770
+ if (!CheckInnerLoopIterationCountInvariant (SubLoop, L, SE)) {
771
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam; Inner loop iteration count is "
772
+ " not consistent on each iteration\n " );
744
773
return false ;
774
+ }
745
775
746
776
// Check the loop safety info for exceptions.
747
777
LoopSafetyInfo LSI;
748
778
computeLoopSafetyInfo (&LSI, L);
749
- if (LSI.MayThrow )
779
+ if (LSI.MayThrow ) {
780
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam; Something may throw\n " );
750
781
return false ;
782
+ }
751
783
752
784
// We've ruled out the easy stuff and now need to check that there are no
753
785
// interdependencies which may prevent us from moving the:
@@ -772,14 +804,19 @@ bool llvm::isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
772
804
}
773
805
// Keep going
774
806
return true ;
775
- }))
807
+ })) {
808
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam; can't move required "
809
+ " instructions after subloop to before it\n " );
776
810
return false ;
811
+ }
777
812
778
813
// Check for memory dependencies which prohibit the unrolling we are doing.
779
814
// Because of the way we are unrolling Fore/Sub/Aft blocks, we need to check
780
815
// there are no dependencies between Fore-Sub, Fore-Aft, Sub-Aft and Sub-Sub.
781
- if (!checkDependencies (L, ForeBlocks, SubLoopBlocks, AftBlocks, DI))
816
+ if (!checkDependencies (L, ForeBlocks, SubLoopBlocks, AftBlocks, DI)) {
817
+ LLVM_DEBUG (dbgs () << " Won't unroll-and-jam; failed dependency check\n " );
782
818
return false ;
819
+ }
783
820
784
821
return true ;
785
822
}
0 commit comments