[llvm] r289992 - Preserve loop metadata when folding branches to a common destination.

Michael Kuperstein via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 16 13:23:59 PST 2016


Author: mkuper
Date: Fri Dec 16 15:23:59 2016
New Revision: 289992

URL: http://llvm.org/viewvc/llvm-project?rev=289992&view=rev
Log:
Preserve loop metadata when folding branches to a common destination.

Differential Revision: https://reviews.llvm.org/D27830

Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/trunk/test/Transforms/LoopSimplify/preserve-llvm-loop-metadata.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=289992&r1=289991&r2=289992&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Fri Dec 16 15:23:59 2016
@@ -2792,6 +2792,11 @@ bool llvm::FoldBranchToCommonDest(Branch
       PBI = New_PBI;
     }
 
+    // If BI was a loop latch, it may have had associated loop metadata.
+    // We need to copy it to the new latch, that is, PBI.
+    if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
+      PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
+
     // TODO: If BB is reachable from all paths through PredBlock, then we
     // could replace PBI's branch probabilities with BI's.
 

Modified: llvm/trunk/test/Transforms/LoopSimplify/preserve-llvm-loop-metadata.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopSimplify/preserve-llvm-loop-metadata.ll?rev=289992&r1=289991&r2=289992&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopSimplify/preserve-llvm-loop-metadata.ll (original)
+++ llvm/trunk/test/Transforms/LoopSimplify/preserve-llvm-loop-metadata.ll Fri Dec 16 15:23:59 2016
@@ -1,5 +1,6 @@
 ; RUN: opt -loop-simplify -S < %s | FileCheck %s
 
+; CHECK-LABEL: @test1
 define void @test1(i32 %n) {
 entry:
   br label %while.cond
@@ -35,6 +36,33 @@ while.end:
 ; CHECK: if.else
 ; CHECK-NOT: br {{.*}}!llvm.loop{{.*}}
 
+; CHECK-LABEL: @test2
+; CHECK: for.body:
+; CHECK: br i1 %{{.*}}, label %for.body, label %cleanup.loopexit, !llvm.loop !0
+define void @test2(i32 %k)  {
+entry: 
+  %cmp9 = icmp sgt i32 %k, 0
+  br i1 %cmp9, label %for.body.preheader, label %cleanup
+
+for.body.preheader:                               ; preds = %entry
+  br label %for.body
+
+for.cond:                                         ; preds = %for.body
+  %cmp = icmp slt i32 %inc, %k
+  br i1 %cmp, label %for.body, label %cleanup.loopexit, !llvm.loop !0
+
+for.body:                                         ; preds = %for.body.preheader, %for.cond
+  %i.010 = phi i32 [ %inc, %for.cond ], [ 0, %for.body.preheader ]
+  %cmp3 = icmp sgt i32 %i.010, 3
+  %inc = add nsw i32 %i.010, 1
+  br i1 %cmp3, label %cleanup.loopexit, label %for.cond
+
+cleanup.loopexit:                                 ; preds = %for.body, %for.cond
+  br label %cleanup
+
+cleanup:                                          ; preds = %cleanup.loopexit, %entry
+  ret void
+}
 
 !0 = distinct !{!0, !1}
 !1 = !{!"llvm.loop.distribute.enable", i1 true}




More information about the llvm-commits mailing list