Commit 99149df7 authored by Toshihito Kikuchi's avatar Toshihito Kikuchi
Browse files

Bug 1650419 - Backport the tail merge fix to our version of Clang 9.0.1. r=dmajor

This patch applies the fix for https://bugs.llvm.org/show_bug.cgi?id=45858 to
our version of Clang so that we can update Chromium sandbox code (bug 1639030).

Differential Revision: https://phabricator.services.mozilla.com/D82589
parent c28c0b93
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
      "unpoison-thread-stacks.patch",
      "downgrade-mangling-error.patch",
      "tsan-hang-be41a98ac222.patch",
      "llvmorg-11-init-15486-gfc937806efd-dont-jump-to-landing-pads.patch",
      "loosen-msvc-detection.patch"
    ]
}
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
      "downgrade-mangling-error.patch",
      "rG7e18aeba5062.patch",
      "llvmorg-10-init-5191-ga84b200e604-windows-pgo.patch",
      "llvmorg-11-init-15486-gfc937806efd-dont-jump-to-landing-pads.patch",
      "loosen-msvc-detection.patch"
    ]
}
+100 −0
Original line number Diff line number Diff line
From d1c09fb47e2778538c5b1f918724d31d05497883 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks@google.com>
Date: Wed, 13 May 2020 16:33:09 -0700
Subject: [PATCH] Don't jump to landing pads in Control Flow Optimizer

Summary: Likely fixes https://bugs.llvm.org/show_bug.cgi?id=45858.

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D80047
---
 llvm/lib/CodeGen/BranchFolding.cpp            | 18 ++++++------
 llvm/test/CodeGen/X86/branchfolding-ehpad.mir | 28 +++++++++++++++++++
 2 files changed, 38 insertions(+), 8 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/branchfolding-ehpad.mir

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index fb54b5d6c8d..4a822b58446 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -991,10 +991,10 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
       continue;
     }
 
-    // If one of the blocks is the entire common tail (and not the entry
-    // block, which we can't jump to), we can treat all blocks with this same
-    // tail at once.  Use PredBB if that is one of the possibilities, as that
-    // will not introduce any extra branches.
+    // If one of the blocks is the entire common tail (and is not the entry
+    // block/an EH pad, which we can't jump to), we can treat all blocks with
+    // this same tail at once.  Use PredBB if that is one of the possibilities,
+    // as that will not introduce any extra branches.
     MachineBasicBlock *EntryBB =
         &MergePotentials.front().getBlock()->getParent()->front();
     unsigned commonTailIndex = SameTails.size();
@@ -1002,19 +1002,21 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
     // into the other.
     if (SameTails.size() == 2 &&
         SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
-        SameTails[1].tailIsWholeBlock())
+        SameTails[1].tailIsWholeBlock() && !SameTails[1].getBlock()->isEHPad())
       commonTailIndex = 1;
     else if (SameTails.size() == 2 &&
              SameTails[1].getBlock()->isLayoutSuccessor(
-                                                     SameTails[0].getBlock()) &&
-             SameTails[0].tailIsWholeBlock())
+                 SameTails[0].getBlock()) &&
+             SameTails[0].tailIsWholeBlock() &&
+             !SameTails[0].getBlock()->isEHPad())
       commonTailIndex = 0;
     else {
       // Otherwise just pick one, favoring the fall-through predecessor if
       // there is one.
       for (unsigned i = 0, e = SameTails.size(); i != e; ++i) {
         MachineBasicBlock *MBB = SameTails[i].getBlock();
-        if (MBB == EntryBB && SameTails[i].tailIsWholeBlock())
+        if ((MBB == EntryBB || MBB->isEHPad()) &&
+            SameTails[i].tailIsWholeBlock())
           continue;
         if (MBB == PredBB) {
           commonTailIndex = i;
diff --git a/llvm/test/CodeGen/X86/branchfolding-ehpad.mir b/llvm/test/CodeGen/X86/branchfolding-ehpad.mir
new file mode 100644
index 00000000000..d445cd20680
--- /dev/null
+++ b/llvm/test/CodeGen/X86/branchfolding-ehpad.mir
@@ -0,0 +1,28 @@
+# RUN: llc -mtriple=x86_64-windows-msvc -verify-machineinstrs -run-pass branch-folder -o - %s | FileCheck %s
+
+# Check that branch-folder does not create a fallthrough to a landing pad.
+# Also make sure that the landing pad still can be tail merged.
+---
+name: foo
+body: |
+  ; CHECK-LABEL: name: foo
+  bb.0:
+    successors: %bb.1, %bb.3
+  bb.1:
+    JCC_1 %bb.4, 5, implicit killed $eflags
+  bb.2:
+    MOV8mi $r13, 1, $noreg, 0, $noreg, 0
+    JMP_1 %bb.5
+  ; CHECK: bb.2:
+  ; CHECK-NOT: successors: {{.*}}bb.3
+  ; CHECK: bb.3 (landing-pad):
+  ; CHECK-NOT: MOV8mi
+  bb.3(landing-pad):
+    MOV8mi $r13, 1, $noreg, 0, $noreg, 0
+    JMP_1 %bb.5
+  ; CHECK: bb.4:
+  bb.4:
+    MOV8mi $r13, 2, $noreg, 0, $noreg, 0
+  bb.5:
+    RET 0
+...
-- 
2.24.1.windows.2