[llvm-branch-commits] [llvm-branch] r309574 - Merging r309422:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jul 31 09:41:23 PDT 2017

Author: hans
Date: Mon Jul 31 09:41:22 2017
New Revision: 309574

URL: http://llvm.org/viewvc/llvm-project?rev=309574&view=rev
Merging r309422:
r309422 | rnk | 2017-07-28 12:48:40 -0700 (Fri, 28 Jul 2017) | 25 lines

Fix conditional tail call branch folding when both edges are the same

The conditional tail call logic did the wrong thing when both
destinations of a conditional branch were the same:

BB#1: derived from LLVM BB %entry
    Live Ins: %EFLAGS
    Predecessors according to CFG: BB#0
        JE_1 <BB#5>, %EFLAGS<imp-use,kill>
        JMP_1 <BB#5>

BB#5: derived from LLVM BB %sw.epilog
    Predecessors according to CFG: BB#1
        TCRETURNdi64 <ga:@mergeable_conditional_tailcall>, 0, ...

We would fold the JE_1 to a TCRETURNdi64cc, and then remove our BB#5
successor. Then BB#5 would be deleted as it had no predecessors, leaving
a dangling "JMP_1 <BB#5>" reference behind to cause assertions later.

This patch checks that both conditional branch destinations are
different before doing the transform. The standard branch folding logic
is able to remove both the JMP_1 and the JE_1, and for my test case we
end up forming a better conditional tail call later.

Fixes PR33980

      - copied unchanged from r309422, llvm/trunk/test/CodeGen/X86/conditional-tailcall-samedest.mir
    llvm/branches/release_50/   (props changed)

Propchange: llvm/branches/release_50/
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jul 31 09:41:22 2017
@@ -1,3 +1,3 @@

Modified: llvm/branches/release_50/lib/CodeGen/BranchFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_50/lib/CodeGen/BranchFolding.cpp?rev=309574&r1=309573&r2=309574&view=diff
--- llvm/branches/release_50/lib/CodeGen/BranchFolding.cpp (original)
+++ llvm/branches/release_50/lib/CodeGen/BranchFolding.cpp Mon Jul 31 09:41:22 2017
@@ -1475,13 +1475,14 @@ ReoptimizeBlock:
       bool PredAnalyzable =
           !TII->analyzeBranch(*Pred, PredTBB, PredFBB, PredCond, true);
-      if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB) {
+      if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB &&
+          PredTBB != PredFBB) {
         // The predecessor has a conditional branch to this block which consists
         // of only a tail call. Try to fold the tail call into the conditional
         // branch.
         if (TII->canMakeTailCallConditional(PredCond, TailCall)) {
           // TODO: It would be nice if analyzeBranch() could provide a pointer
-          // to the branch insturction so replaceBranchWithTailCall() doesn't
+          // to the branch instruction so replaceBranchWithTailCall() doesn't
           // have to search for it.
           TII->replaceBranchWithTailCall(*Pred, PredCond, TailCall);

More information about the llvm-branch-commits mailing list