[llvm] r255803 - [WebAssembly] Fix the CFG Stackifier to handle unoptimized branches

Dan Gohman via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 16 11:06:41 PST 2015


Author: djg
Date: Wed Dec 16 13:06:41 2015
New Revision: 255803

URL: http://llvm.org/viewvc/llvm-project?rev=255803&view=rev
Log:
[WebAssembly] Fix the CFG Stackifier to handle unoptimized branches

If a branch both branches to and falls through to the same block, treat it as
an explicit branch.

Modified:
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
    llvm/trunk/test/CodeGen/WebAssembly/cfg-stackify.ll

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp?rev=255803&r1=255802&r2=255803&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp Wed Dec 16 13:06:41 2015
@@ -251,6 +251,19 @@ static void SortBlocks(MachineFunction &
 #endif
 }
 
+/// Test whether Pred has any terminators explicitly branching to MBB, as
+/// opposed to falling through. Note that it's possible (eg. in unoptimized
+/// code) for a branch instruction to both branch to a block and fallthrough
+/// to it, so we check the actual branch operands to see if there are any
+/// explicit mentions.
+static bool ExplicitlyBranchesTo(MachineBasicBlock *Pred, MachineBasicBlock *MBB) {
+  for (MachineInstr &MI : Pred->terminators())
+    for (MachineOperand &MO : MI.explicit_operands())
+      if (MO.isMBB() && MO.getMBB() == MBB)
+        return true;
+  return false;
+}
+
 /// Insert a BLOCK marker for branches to MBB (if needed).
 static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF,
                              SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
@@ -266,8 +279,7 @@ static void PlaceBlockMarker(MachineBasi
   for (MachineBasicBlock *Pred : MBB.predecessors())
     if (Pred->getNumber() < MBBNumber) {
       Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
-      if (!Pred->isLayoutSuccessor(&MBB) ||
-          !(Pred->empty() || !Pred->back().isBarrier()))
+      if (ExplicitlyBranchesTo(Pred, &MBB))
         IsBranchedTo = true;
     }
   if (!Header)

Modified: llvm/trunk/test/CodeGen/WebAssembly/cfg-stackify.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/cfg-stackify.ll?rev=255803&r1=255802&r2=255803&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/cfg-stackify.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/cfg-stackify.ll Wed Dec 16 13:06:41 2015
@@ -1054,3 +1054,49 @@ bb4:
 bb7:
   ret void
 }
+
+; A block can be "branched to" from another even if it is also reachable via
+; fallthrough from the other. This would normally be optimized away, so use
+; optnone to disable optimizations to test this case.
+
+; CHECK-LABEL: test13:
+; CHECK-NEXT:  .local i32{{$}}
+; CHECK:       block BB22_2{{$}}
+; CHECK:       br_if $pop4, BB22_2{{$}}
+; CHECK-NEXT:  return{{$}}
+; CHECK-NEXT:  BB22_2:
+; CHECK:       block BB22_4{{$}}
+; CHECK-NEXT:  br_if $0, BB22_4{{$}}
+; CHECK:       BB22_4:
+; CHECK:       block BB22_5{{$}}
+; CHECK:       br_if $pop6, BB22_5{{$}}
+; CHECK-NEXT:  BB22_5:
+; CHECK-NEXT:  unreachable{{$}}
+; OPT-LABEL: test13:
+; OPT-NEXT:  .local i32{{$}}
+; OPT:       block BB22_2{{$}}
+; OPT:       br_if $pop4, BB22_2{{$}}
+; OPT-NEXT:  return{{$}}
+; OPT-NEXT:  BB22_2:
+; OPT:       block BB22_4{{$}}
+; OPT-NEXT:  br_if $0, BB22_4{{$}}
+; OPT:       BB22_4:
+; OPT:       block BB22_5{{$}}
+; OPT:       br_if $pop6, BB22_5{{$}}
+; OPT-NEXT:  BB22_5:
+; OPT-NEXT:  unreachable{{$}}
+define void @test13() noinline optnone {
+bb:
+  br i1 undef, label %bb5, label %bb2
+bb1:
+  unreachable
+bb2:
+  br i1 undef, label %bb3, label %bb4
+bb3:
+  br label %bb4
+bb4:
+  %tmp = phi i1 [ false, %bb2 ], [ false, %bb3 ]
+  br i1 %tmp, label %bb1, label %bb1
+bb5:
+  ret void
+}




More information about the llvm-commits mailing list