[llvm-commits] [llvm] r111255 - in /llvm/trunk: lib/Transforms/Scalar/LoopRotation.cpp test/Transforms/LoopRotate/phi-duplicate.ll
Dan Gohman
gohman at apple.com
Tue Aug 17 10:39:21 PDT 2010
Author: djg
Date: Tue Aug 17 12:39:21 2010
New Revision: 111255
URL: http://llvm.org/viewvc/llvm-project?rev=111255&view=rev
Log:
When rotating loops, put the original header at the bottom of the
loop, making the resulting loop significantly less ugly. Also, zap
its trivial PHI nodes, since it's easy.
Modified:
llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp
llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll
Modified: llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp?rev=111255&r1=111254&r2=111255&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp Tue Aug 17 12:39:21 2010
@@ -261,6 +261,26 @@
// NewHeader is now the header of the loop.
L->moveToHeader(NewHeader);
+ // Move the original header to the bottom of the loop, where it now more
+ // naturally belongs. This isn't necessary for correctness, and CodeGen can
+ // usually reorder blocks on its own to fix things like this up, but it's
+ // still nice to keep the IR readable.
+ //
+ // The original header should have only one predecessor at this point, since
+ // we checked that the loop had a proper preheader and unique backedge before
+ // we started.
+ assert(OrigHeader->getSinglePredecessor() &&
+ "Original loop header has too many predecessors after loop rotation!");
+ OrigHeader->moveAfter(OrigHeader->getSinglePredecessor());
+
+ // Also, since this original header only has one predecessor, zap its
+ // PHI nodes, which are now trivial.
+ FoldSingleEntryPHINodes(OrigHeader);
+
+ // TODO: We could just go ahead and merge OrigHeader into its predecessor
+ // at this point, if we don't mind updating dominator info.
+
+ // Establish a new preheader, update dominators, etc.
preserveCanonicalLoopForm(LPM);
++NumRotated;
Modified: llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll?rev=111255&r1=111254&r2=111255&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll (original)
+++ llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll Tue Aug 17 12:39:21 2010
@@ -27,9 +27,21 @@
for.end: ; preds = %for.cond
ret void
}
-; Should only end up with one phi.
-; CHECK: for.body:
-; CHECK-NEXT: %j.02 = phi i64
-; CHECK-NOT: phi
-; CHECK: ret void
+; Should only end up with one phi. Also, the original for.cond block should
+; be moved to the end of the loop so that the new loop header pleasantly
+; ends up at the top.
+
+; CHECK: define void @test
+; CHECK-NEXT: entry:
+; CHECK-NEXT: icmp slt i64
+; CHECK-NEXT: br i1
+; CHECK-NOT: :
+; CHECK: bb.nph:
+; CHECK-NEXT: br label %for.body
+; CHECK-NOT: :
+; CHECK: for.body:
+; CHECK-NEXT: %j.02 = phi i64
+; CHECK-NOT: phi
+; CHECK: ret void
+; CHECK-NEXT: }
More information about the llvm-commits
mailing list