[llvm] ec32dff - [BranchFolding] skip debug instr to avoid code change

Jeremy Morse via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 29 04:46:21 PDT 2019


Author: Jeremy Morse
Date: 2019-10-29T11:45:38Z
New Revision: ec32dff0b075055b30140c543e9f2bef608adc14

URL: https://github.com/llvm/llvm-project/commit/ec32dff0b075055b30140c543e9f2bef608adc14
DIFF: https://github.com/llvm/llvm-project/commit/ec32dff0b075055b30140c543e9f2bef608adc14.diff

LOG: [BranchFolding] skip debug instr to avoid code change

Use the existing helper function in BranchFolding, "countsAsInstruction",
to skip over non-instructions. Otherwise debug instructions can be
identified as the last real instruction in a block, leading to different
codegen decisions when debug is enabled as demonstrated by the test case.

Patch by: yechunliang (Chris Ye)!

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

Added: 
    llvm/test/CodeGen/MIR/X86/branch-folder-with-debug.mir

Modified: 
    llvm/lib/CodeGen/BranchFolding.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 455916eeb82f..b6e7c9f404e5 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -402,11 +402,12 @@ static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1,
   // the CFI instruction. Later on, this leads to BB2 being 'hacked off' at the
   // wrong place (in ReplaceTailWithBranchTo()) which results in losing this CFI
   // instruction.
-  while (I1 != MBB1->end() && I1->isCFIInstruction()) {
+  // Skip CFI_INSTRUCTION and debugging instruction; necessary to avoid changing the code.
+  while (I1 != MBB1->end() && !countsAsInstruction(*I1)) {
     ++I1;
   }
 
-  while (I2 != MBB2->end() && I2->isCFIInstruction()) {
+  while (I2 != MBB2->end() && !countsAsInstruction(*I2)) {
     ++I2;
   }
 

diff  --git a/llvm/test/CodeGen/MIR/X86/branch-folder-with-debug.mir b/llvm/test/CodeGen/MIR/X86/branch-folder-with-debug.mir
new file mode 100644
index 000000000000..672df28a6933
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/X86/branch-folder-with-debug.mir
@@ -0,0 +1,109 @@
+# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=branch-folder | FileCheck %s
+# Branch folder should ignore the DBG_VALUE between block bb.2 and bb.3,
+# set these blocks as bb.3, so that bb.6 will succeed to merge between bb.2 and bb.3.
+# if the DBG_VALUE is not ignored, bb.6 will merge between bb.1 and bb.2, the result is
+# 
diff erent with Codegen without -g.
+#
+# Generated with
+#
+# clang++ -g -w -O1 -S -emit-llvm test.cc
+# llc -stop-before=branch-folder test.ll
+#
+# template <typename, typename = int> class e;
+# class allocator {
+# public:
+#   ~allocator();
+# };
+# template <typename, typename> class e {
+# public:
+#   e(char *, allocator = allocator());
+# };
+# template <typename b, typename c, typename d> bool operator==(e<c, d>, b);
+# class f {
+# public:
+#   f(int *, int *, int *, int, int, int, int);
+#   e<char> g();
+#   void j();
+# };
+# int h, i;
+# class k {
+#   void l();
+#   bool m_fn4();
+#   int m;
+#   int n;
+#   int q;
+#   int fmap;
+# };
+# void k::l() {
+#   e<char> o = "";
+#   for (;;) {
+#     int p = 0;
+#     for (;;) {
+#       if (m_fn4())
+#         break;
+#       f a(&q, &fmap, &m, n, h, i, 0);
+#       if (a.g() == "")
+#         a.j();
+#     }
+#   }
+# }
+
+--- |
+
+  define dso_local void @l() {
+    ret void
+  }
+
+...
+---
+name:            l
+body:             |
+  bb.0:
+    ; CHECK: bb.0:
+    ; CHECK-NEXT: successors: %bb.1({{.*}}), %bb.7
+    successors: %bb.1, %bb.3
+
+  bb.1:
+    $rdi = MOV64rr $rsp
+
+  bb.2:
+    ; CHECK: bb.2:
+    ; CHECK-NEXT: successors: %bb.3
+    successors: %bb.2, %bb.4
+    DBG_VALUE
+    CFI_INSTRUCTION def_cfa_offset 8
+    ; CHECK: bb.3
+    ; CHECK-NEXT: successors: %bb.2({{.*}}), %bb.4
+    TEST8rr killed renamable $al, renamable $al, implicit-def $eflags
+    JCC_1 %bb.2, 5, implicit killed $eflags
+    JMP_1 %bb.4
+
+  bb.3 (landing-pad):
+    ; CHECK: bb.4:
+    ; CHECK-NEXT: successors: %bb.5({{.*}}), %bb.6
+    successors:
+
+  bb.4:
+    successors: %bb.5, %bb.6
+    JCC_1 %bb.6, 4, implicit killed $eflags
+    ; CHECK: JCC_1 %bb.6, 4, implicit $eflags
+    JMP_1 %bb.5
+
+  bb.5:
+    ; CHECK: bb.5:
+    ; CHECK-NEXT: successors: %bb.6
+    $rdi = COPY renamable $r12
+
+  bb.6:
+    ; CHECK: bb.6:
+    ; CHECK-NEXT: successors: %bb.3
+    successors: %bb.2, %bb.4
+    ; CHECK: JMP_1 %bb.3
+
+  ; CHECK: bb.7 (landing-pad):
+    $rdi = COPY renamable $rbx
+    TEST8rr killed renamable $al, renamable $al, implicit-def $eflags
+    JCC_1 %bb.2, 5, implicit killed $eflags
+    JMP_1 %bb.4
+
+...


        


More information about the llvm-commits mailing list