[PATCH] D27856: [mips] Compact branch hazard detection

Aleksandar Beserminji via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 16 10:45:04 PST 2016


abeserminji created this revision.
abeserminji added reviewers: zoran.jovanovic, sdardis, petarj.
abeserminji added a subscriber: llvm-commits.
abeserminji set the repository for this revision to rL LLVM.
Herald added a reviewer: vkalintiris.

Follow up to https://reviews.llvm.org/D27209 fix, this patch now also covers cases such as when unsafe instruction appears in forbidden slot of branch instruction.


Repository:
  rL LLVM

https://reviews.llvm.org/D27856

Files:
  lib/Target/Mips/MipsHazardSchedule.cpp
  test/CodeGen/Mips/compactbranches/unsafe-in-forbidden-slot.ll


Index: test/CodeGen/Mips/compactbranches/unsafe-in-forbidden-slot.ll
===================================================================
--- /dev/null
+++ test/CodeGen/Mips/compactbranches/unsafe-in-forbidden-slot.ll
@@ -0,0 +1,34 @@
+; RUN: llc -march=mips64el -O0 -mcpu=mips64r6 < %s | FileCheck %s
+; RUN: llc -march=mips64 -O0 -mcpu=mips64r6 < %s | FileCheck %s
+
+ at boo = global i32 0, align 4
+
+; Function Attrs: nounwind
+define void @_Z3foov() #0 {
+entry:
+  %0 = load volatile i32, i32* @boo, align 4
+  switch i32 %0, label %sw.epilog [
+    i32 0, label %sw.bb
+    i32 1, label %sw.bb1
+    i32 2, label %sw.bb1
+  ]
+
+sw.bb:                                            ; preds = %entry
+  store volatile i32 1, i32* @boo, align 4
+  br label %sw.epilog
+; CHECK: beqzc
+; CHECK-NEXT: nop
+; CHECK-NEXT: .LBB
+; CHECK-NEXT: j
+
+sw.bb1:                                           ; preds = %entry, %entry
+  store volatile i32 2, i32* @boo, align 4
+  br label %sw.epilog
+; CHECK: bnezc
+; CHECK-NEXT: nop
+; CHECK-NEXT: .LBB
+; CHECK-NEXT: j
+
+sw.epilog:                                        ; preds = %entry, %sw.bb1, %sw.bb
+  ret void
+}
Index: lib/Target/Mips/MipsHazardSchedule.cpp
===================================================================
--- lib/Target/Mips/MipsHazardSchedule.cpp
+++ lib/Target/Mips/MipsHazardSchedule.cpp
@@ -103,24 +103,23 @@
 
 // Find the next real instruction from the current position, looking through
 // basic block boundaries.
-static Iter getNextMachineInstr(Iter Position) {
-  if (std::next(Position) == Position->getParent()->end()) {
-    const MachineBasicBlock * MBB = (&*Position)->getParent();
-    for (auto *Succ : MBB->successors()) {
-      if (MBB->isLayoutSuccessor(Succ)) {
-        Iter I = Succ->begin();
-        Iter Next = getNextMachineInstrInBB(I);
-        if (Next == Succ->end()) {
-          return getNextMachineInstr(I);
-        } else {
-          return I;
-        }
-      }
+static Iter getNextMachineInstr(Iter Position, MachineBasicBlock *Parent) {
+  if (Position == Parent->end()) {
+    MachineBasicBlock *Succ = Parent->getNextNode();
+    if (Succ != nullptr && Parent->isSuccessor(Succ)) {
+      Position = Succ->begin();
+      Parent = Succ;
+    } else {
+      llvm_unreachable(
+          "Should have identified the end of the function earlier!");
     }
-    llvm_unreachable("Should have identified the end of the function earlier!");
   }
 
-  return getNextMachineInstrInBB(Position);
+  Iter Instr = getNextMachineInstrInBB(Position);
+  if (Instr == Parent->end()) {
+    return getNextMachineInstr(Instr, Parent);
+  }
+  return Instr;
 }
 
 bool MipsHazardSchedule::runOnMachineFunction(MachineFunction &MF) {
@@ -146,13 +145,7 @@
       bool LastInstInFunction =
           std::next(I) == FI->end() && std::next(FI) == MF.end();
       if (!LastInstInFunction) {
-        if (std::next(I) != FI->end()) {
-          // Start looking from the next instruction in the basic block.
-          Inst = getNextMachineInstr(std::next(I));
-        } else {
-          // Next instruction in the physical successor basic block.
-          Inst = getNextMachineInstr(I);
-        }
+        Inst = getNextMachineInstr(std::next(I), &*FI);
       }
 
       if (LastInstInFunction || !TII->SafeInForbiddenSlot(*Inst)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27856.81771.patch
Type: text/x-patch
Size: 3331 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161216/1aa938fc/attachment.bin>


More information about the llvm-commits mailing list