[PATCH] D22521: [MBP] do not reorder and move up loop latch block

Sjoerd Meijer via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 22 02:46:04 PDT 2016


SjoerdMeijer updated this revision to Diff 65040.
SjoerdMeijer added a comment.

Hi David, thanks for reviewing.
I will separate the functional changes from the tidy up. This patch implements the functional change.
Cheers.


https://reviews.llvm.org/D22521

Files:
  lib/CodeGen/MachineBlockPlacement.cpp
  test/CodeGen/X86/loop-blocks.ll

Index: test/CodeGen/X86/loop-blocks.ll
===================================================================
--- test/CodeGen/X86/loop-blocks.ll
+++ test/CodeGen/X86/loop-blocks.ll
@@ -228,6 +228,42 @@
   ret void
 }
 
+; This is exactly the same function as slightly_more_involved.
+; The difference is that when optimising for size, we do not want
+; to see this reordering.
+
+; CHECK-LABEL: slightly_more_involved_2:
+; CHECK-NOT:      jmp .LBB5_1
+; CHECK:          .LBB5_1:
+; CHECK-NEXT:     callq body
+; CECK-NOT-NEXT:  callq bar99
+
+define void @slightly_more_involved_2() #0 {
+entry:
+  br label %loop
+
+loop:
+  call void @body()
+  %t0 = call i32 @get()
+  %t1 = icmp slt i32 %t0, 2
+  br i1 %t1, label %block_a, label %bb
+
+bb:
+  %t2 = call i32 @get()
+  %t3 = icmp slt i32 %t2, 99
+  br i1 %t3, label %exit, label %loop
+
+block_a:
+  call void @bar99()
+  br label %loop
+
+exit:
+  call void @exit()
+  ret void
+}
+
+attributes #0 = { minsize norecurse nounwind optsize readnone uwtable }
+
 declare void @bar99() nounwind
 declare void @bar100() nounwind
 declare void @bar101() nounwind
Index: lib/CodeGen/MachineBlockPlacement.cpp
===================================================================
--- lib/CodeGen/MachineBlockPlacement.cpp
+++ lib/CodeGen/MachineBlockPlacement.cpp
@@ -922,6 +922,12 @@
 MachineBasicBlock *
 MachineBlockPlacement::findBestLoopTop(MachineLoop &L,
                                        const BlockFilterSet &LoopBlockSet) {
+  // Moving a block up introduces an unconditional jump that skips this block
+  // the first time the loop is executed. This extra jump increases
+  // code size, so we don't do this reordering when optimising for size.
+  if (L.getHeader()->getParent()->getFunction()->optForSize())
+    return L.getHeader();
+
   // Check that the header hasn't been fused with a preheader block due to
   // crazy branches. If it has, we need to start with the header at the top to
   // prevent pulling the preheader into the loop body.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22521.65040.patch
Type: text/x-patch
Size: 2010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160722/18d3c01c/attachment.bin>


More information about the llvm-commits mailing list