[PATCH] D58963: [JumpThreading] Retain debug info when replacing branch instructions

Stephen Tozer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 5 06:31:23 PST 2019


StephenTozer created this revision.
StephenTozer added reviewers: brzycki, probinson, gbedwell, aprantl.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Fixes bug 37966: https://bugs.llvm.org/show_bug.cgi?id=37966

The Jump Threading pass will replace certain conditional branch
instructions with unconditional branches when it can prove that only one
branch can occur. Prior to this patch, it would not carry the debug
info from the old instruction to the new one.

This patch fixes the bug described by copying the debug info from the
conditional branch instruction to the new unconditional branch
instruction, and adds a regression test for the Jump Threading pass that
covers this case.


Repository:
  rL LLVM

https://reviews.llvm.org/D58963

Files:
  llvm/lib/Transforms/Scalar/JumpThreading.cpp
  llvm/test/Transforms/JumpThreading/branch-debug-info.ll


Index: llvm/test/Transforms/JumpThreading/branch-debug-info.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/JumpThreading/branch-debug-info.ll
@@ -0,0 +1,34 @@
+; RUN: opt %s -debugify -jump-threading -S | FileCheck %s
+; Tests Bug 37966
+
+define dso_local void @bar(i32 %cc) local_unnamed_addr {
+; CHECK-LABEL: @bar(
+; CHECK: while.body:
+; CHECK-NEXT: br label %while.body, !dbg ![[DBG:[0-9]+]]
+; CHECK: ![[DBG]] = !DILocation(
+
+entry:
+  %dd = alloca [3 x i32], align 4
+  %tobool = icmp eq i32 %cc, 0
+  br i1 %tobool, label %while.end, label %while.body.lr.ph
+
+while.body.lr.ph:                                 ; preds = %entry
+  %idxprom = sext i32 %cc to i64
+  %arrayidx = getelementptr inbounds [3 x i32], [3 x i32]* %dd, i64 0, i64 %idxprom
+  br label %while.body
+
+while.cond:                                       ; preds = %while.body
+  store i32 2, i32* %arrayidx, align 4
+  br label %while.end
+
+; The jump threading pass will replace this branch with an unconditional branch:
+; br label %while.body
+; The debug information for the existing branch instruction should be reused
+; for the new instruction.
+while.body:                                       ; preds = %while.body.lr.ph, %while.body
+  br i1 %tobool, label %while.cond, label %while.body
+
+while.end:                                        ; preds = %entry, %while.cond
+  ret void
+}
+
Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1142,7 +1142,9 @@
         unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1;
         BasicBlock *ToRemoveSucc = CondBr->getSuccessor(ToRemove);
         ToRemoveSucc->removePredecessor(BB, true);
-        BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
+        BranchInst *UncondBr =
+          BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
+        UncondBr->setDebugLoc(CondBr->getDebugLoc());
         CondBr->eraseFromParent();
         if (CondCmp->use_empty())
           CondCmp->eraseFromParent();
@@ -1245,7 +1247,8 @@
       BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1);
       BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0);
       RemoveSucc->removePredecessor(BB);
-      BranchInst::Create(KeepSucc, BI);
+      BranchInst *UncondBI = BranchInst::Create(KeepSucc, BI);
+      UncondBI->setDebugLoc(BI->getDebugLoc());
       BI->eraseFromParent();
       DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}});
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58963.189299.patch
Type: text/x-patch
Size: 2690 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190305/c7177c0c/attachment.bin>


More information about the llvm-commits mailing list