[llvm] f8bd6a7 - [SimplifyCFG] Drop debug loc in SpeculativelyExecuteBB

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 23 18:26:27 PDT 2020


Author: Vedant Kumar
Date: 2020-06-23T18:25:52-07:00
New Revision: f8bd6a75edac7560deb5fdcb31041b454dd9d7e0

URL: https://github.com/llvm/llvm-project/commit/f8bd6a75edac7560deb5fdcb31041b454dd9d7e0
DIFF: https://github.com/llvm/llvm-project/commit/f8bd6a75edac7560deb5fdcb31041b454dd9d7e0.diff

LOG: [SimplifyCFG] Drop debug loc in SpeculativelyExecuteBB

Summary:
According to HowToUpdateDebugInfo.rst:

```
Preserving the debug locations of speculated instructions can make
it seem like a condition is true when it's not (or vice versa), which
leads to a confusing single-stepping experience
```

This patch follows the recommendation to drop debug locations on
speculated instructions.

Reviewers: aprantl, davide

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D82420

Added: 
    llvm/test/Transforms/SimplifyCFG/drop-debug-loc-when-speculating.ll

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 7b36907f7011..3d72556b2aed 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2141,9 +2141,14 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
   }
 
   // Metadata can be dependent on the condition we are hoisting above.
-  // Conservatively strip all metadata on the instruction.
-  for (auto &I : *ThenBB)
+  // Conservatively strip all metadata on the instruction. Drop the debug loc
+  // to avoid making it appear as if the condition is a constant, which would
+  // be misleading while debugging.
+  for (auto &I : *ThenBB) {
+    if (!SpeculatedStoreValue || &I != SpeculatedStore)
+      I.setDebugLoc(DebugLoc());
     I.dropUnknownNonDebugMetadata();
+  }
 
   // Hoist the instructions.
   BB->getInstList().splice(BI->getIterator(), ThenBB->getInstList(),

diff  --git a/llvm/test/Transforms/SimplifyCFG/drop-debug-loc-when-speculating.ll b/llvm/test/Transforms/SimplifyCFG/drop-debug-loc-when-speculating.ll
new file mode 100644
index 000000000000..6298b4e5de35
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/drop-debug-loc-when-speculating.ll
@@ -0,0 +1,39 @@
+; RUN: opt -S -o - %s -simplifycfg | FileCheck %s
+
+declare i1 @make_condition()
+
+; CHECK-LABEL: @test1(
+; CHECK: and i1 [[COND:%.*]], [[COND]]{{$}}
+; CHECK: select i1 [[COND]]
+define void @test1() !dbg !6 {
+start:
+  %cond = call i1 @make_condition(), !dbg !8
+  br i1 %cond, label %then, label %else, !dbg !9
+
+then:                                             ; preds = %start
+  %and = and i1 %cond, %cond, !dbg !10
+  br label %else, !dbg !11
+
+else:                                             ; preds = %then, %start
+  %phi = phi i1 [ %cond, %start ], [ %and, %then ], !dbg !12
+  ret void, !dbg !13
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!3, !4}
+!llvm.module.flags = !{!5}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+!1 = !DIFile(filename: "test.ll", directory: "/")
+!2 = !{}
+!3 = !{i32 6}
+!4 = !{i32 0}
+!5 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "test1", linkageName: "test1", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
+!7 = !DISubroutineType(types: !2)
+!8 = !DILocation(line: 1, column: 1, scope: !6)
+!9 = !DILocation(line: 2, column: 1, scope: !6)
+!10 = !DILocation(line: 3, column: 2, scope: !6)
+!11 = !DILocation(line: 4, column: 2, scope: !6)
+!12 = !DILocation(line: 5, column: 3, scope: !6)
+!13 = !DILocation(line: 6, column: 3, scope: !6)


        


More information about the llvm-commits mailing list