[PATCH] D82062: [SimplifyCFG] Update debug location when folding branch to common destination

Davide Italiano via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 17:17:30 PDT 2020


davide created this revision.
davide added reviewers: vsk, aprantl, jmorse.
Herald added subscribers: kosarev, hiraditya.
Herald added a project: LLVM.
davide updated this revision to Diff 271527.
davide added a comment.

Correct diff.


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

SimplifyCFG flattens the CFG transforming:

  @a = local_unnamed_addr global i16 1, align 2, !dbg !0
  @c = local_unnamed_addr global i32 0, align 4, !dbg !9
  @b = local_unnamed_addr global i32 0, align 4, !dbg !6
  
  ; Function Attrs: nounwind ssp uwtable
  define void @d() #0 !dbg !17 {
    ret void, !dbg !20
  }
  
  ; Function Attrs: nounwind ssp uwtable
  define i32 @main() local_unnamed_addr #0 !dbg !21 {
    call void @llvm.dbg.value(metadata i32* @c, metadata !25, metadata !DIExpression()), !dbg !27
    br label %1, !dbg !28
  
  1:                                                ; preds = %7, %0
    %2 = load i16, i16* @a, align 2, !dbg !29, !tbaa !32
    %3 = icmp slt i16 %2, 1, !dbg !36
    br i1 %3, label %4, label %8, !dbg !37
  
  4:                                                ; preds = %1
    %5 = load i32, i32* @c, align 4, !dbg !38, !tbaa !40
    %6 = icmp eq i32 %5, 0, !dbg !42
    br i1 %6, label %7, label %8, !dbg !43
  
  7:                                                ; preds = %4
    store i16 ptrtoint (void ()* @d to i16), i16* @a, align 2, !dbg !44, !tbaa !32
    br label %1, !dbg !45, !llvm.loop !46
  
  8:                                                ; preds = %4, %1
    %9 = load i32, i32* @c, align 4, !dbg !49, !tbaa !40
    store i32 %9, i32* @b, align 4, !dbg !50, !tbaa !40
    ret i32 0, !dbg !51
  }

into

  ; Function Attrs: nounwind ssp uwtable
  define i32 @main() local_unnamed_addr #0 !dbg !21 {
    call void @llvm.dbg.value(metadata i32* @c, metadata !25, metadata !DIExpression()), !dbg !27
    br label %1, !dbg !28
  
  1:                                                ; preds = %6, %0
    %2 = load i16, i16* @a, align 2, !dbg !29, !tbaa !32
    %3 = icmp slt i16 %2, 1, !dbg !36
    %4 = load i32, i32* @c, align 4, !dbg !37
    %5 = icmp eq i32 %4, 0, !dbg !39
    %or.cond = and i1 %3, %5, !dbg !40
    br i1 %or.cond, label %6, label %7, !dbg !40
  
  6:                                                ; preds = %1
    store i16 ptrtoint (void ()* @d to i16), i16* @a, align 2, !dbg !41, !tbaa !32
    br label %1, !dbg !42, !llvm.loop !43
  
  7:                                                ; preds = %1
    %8 = load i32, i32* @c, align 4, !dbg !46, !tbaa !47
    store i32 %8, i32* @b, align 4, !dbg !49, !tbaa !47
    ret i32 0, !dbg !50
  }

When the instructions get folded, a dead block gets folded and the debug information is still retained. This manifests as jumpy stepping in lldb, see the bugzilla PR for an end-to-end C testcase


https://reviews.llvm.org/D82062

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/basictest.ll
  llvm/test/Transforms/SimplifyCFG/fold-debug-location.ll


Index: llvm/test/Transforms/SimplifyCFG/fold-debug-location.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SimplifyCFG/fold-debug-location.ll
@@ -0,0 +1,47 @@
+; RUN: opt -S -simplifycfg < %s | FileCheck %s --match-full-lines
+
+; Make sure we reset the debug location when folding instructions.
+; CHECK: [[VAL:%.*]] = and i32 %c2, %k
+; CHECK-NEXT: [[VAL2:%.*]] icmp eq i32 [[VAL]], 0
+
+declare i32 @bar(...)
+
+define i32 @patatino(i32 %k, i32 %c1, i32 %c2) !dbg !6 {
+  %1 = and i32 %c1, %k, !dbg !8
+  %2 = icmp eq i32 %1, 0, !dbg !9
+  br i1 %2, label %8, label %3, !dbg !10
+
+3:
+  %4 = and i32 %c2, %k, !dbg !11
+  %5 = icmp eq i32 %4, 0, !dbg !12
+  br i1 %5, label %8, label %6, !dbg !13
+
+6:
+  %7 = tail call i32 (...) @bar(), !dbg !14
+  br label %8, !dbg !15
+
+8:
+  ret i32 undef, !dbg !16
+}
+
+!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: "a.ll", directory: "/")
+!2 = !{}
+!3 = !{i32 9}
+!4 = !{i32 0}
+!5 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "patatino", linkageName: "patatino", 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: 1, scope: !6)
+!11 = !DILocation(line: 4, column: 1, scope: !6)
+!12 = !DILocation(line: 5, column: 1, scope: !6)
+!13 = !DILocation(line: 6, column: 1, scope: !6)
+!14 = !DILocation(line: 7, column: 1, scope: !6)
+!15 = !DILocation(line: 8, column: 1, scope: !6)
+!16 = !DILocation(line: 9, column: 1, scope: !6)
Index: llvm/test/Transforms/SimplifyCFG/basictest.ll
===================================================================
--- llvm/test/Transforms/SimplifyCFG/basictest.ll
+++ llvm/test/Transforms/SimplifyCFG/basictest.ll
@@ -98,7 +98,7 @@
 ; CHECK: alloca i8, align 1
 ; CHECK-NEXT: call i8 @test6g
 ; CHECK-NEXT: icmp eq i8 %tmp, 0
-; CHECK-NEXT: load i8, i8* %r, align 1, !dbg !{{[0-9]+$}}
+; CHECK-NEXT: load i8, i8* %r, align 1{{$}}
 
 bb0:
   %r = alloca i8, align 1
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2776,6 +2776,12 @@
       if (isa<DbgInfoIntrinsic>(BonusInst))
         continue;
       Instruction *NewBonusInst = BonusInst->clone();
+
+      // When we fold the bonus instructions we want to make sure we
+      // reset their debug locations in order to avoid stepping on dead
+      // code caused by folding dead branches.
+      NewBonusInst->setDebugLoc(DebugLoc());
+
       RemapInstruction(NewBonusInst, VMap,
                        RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
       VMap[&*BonusInst] = NewBonusInst;
@@ -2795,6 +2801,11 @@
     // Clone Cond into the predecessor basic block, and or/and the
     // two conditions together.
     Instruction *CondInPred = Cond->clone();
+
+    // Reset the condition debug location to avoid jumping on dead code
+    // as the result of folding dead branches.
+    CondInPred->setDebugLoc(DebugLoc());
+
     RemapInstruction(CondInPred, VMap,
                      RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
     PredBlock->getInstList().insert(PBI->getIterator(), CondInPred);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82062.271527.patch
Type: text/x-patch
Size: 3680 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200618/9efdee7d/attachment-0001.bin>


More information about the llvm-commits mailing list