[PATCH] D55272: [DebugInfo] Remove now un-necessary logic from HoistThenElseCodeToIf

Jeremy Morse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 4 07:51:09 PST 2018


jmorse created this revision.
jmorse added reviewers: aprantl, vsk, nikic, gbedwell.
Herald added a subscriber: llvm-commits.

tl;dr: minor cleanup of line number computation to remove un-necessary complexity and avoid keeping dead line numbers alive.

This is a follow-up to D54997 <https://reviews.llvm.org/D54997>, where we saw that applyMergedLocation was required for correctly calculating debug locations when hoisting terminators. Using applyMergedLocation is correct, but makes the code necessary for fixing PR39187 [0,1] redundant: we now cannot leak debug locations from the hoisted blocks into the parent block via the terminator.

IMHO, when converting phis to selects in HoistThenElseCodeToIf we should remove the logic looking for prior non-debug instructions, and just inherit from the (merged) terminator location. The net effect is that code looking like this:

  %10 = insn1...   # Line 1
  %11 = insn2...   # Line 2
  %12 = select...   # Line 2 (hoisted phi converted to select)
  %13 = invoke @something to label %blah unwind label %fail   # Line 0

where the select inherited the line number from the insn above, will instead become:

  %10 = insn1...   # Line 1
  %11 = insn2...   # Line 2
  %12 = select...   # Line 0 (hoisted phi converted to select)
  %13 = invoke @something to label %blah unwind label %fail   # Line 0

where the line number comes from the hoisted terminator. There are two benefits:

- Simpler code in HoistThenElseCodeToIf
- Line numbers that get optimised away won't be kept alive through selects that appeared below them

I've adapted the original regression test to check for selects introduced by HoistThenElseCodeToIf  getting an unknown location. The main point of the original PR was that they shouldn't get the line number from the hoisted terminator, which this behaviour honours.

This leaves the "getPrevNonDebugInstruction" methods without any users, but they seem like useful facilities to keep around.

[0] https://bugs.llvm.org/show_bug.cgi?id=39187
[1] https://reviews.llvm.org/D53390


Repository:
  rL LLVM

https://reviews.llvm.org/D55272

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/CodeGen/X86/pr39187-g.ll


Index: test/CodeGen/X86/pr39187-g.ll
===================================================================
--- test/CodeGen/X86/pr39187-g.ll
+++ test/CodeGen/X86/pr39187-g.ll
@@ -7,22 +7,8 @@
 ; location keep references to its basic block, causing the debug information
 ; to become ambiguous. It causes the debugger to display unreached lines.
 
-; Change the debug location associated with the hoisted instruction, to
-; the debug location from the insertion point in the 'if' block.
-
-; The insertion point is the previous non-debug instruction before the
-; terminator in the parent basic block of the hoisted instruction.
-
-; IR with '-g':
-;
-;  [...]
-;  %frombool = zext i1 %cmp to i8, !dbg !26
-;  call void @llvm.dbg.value(metadata i8 %frombool, metadata !15, metadata !DIExpression()), !dbg !26
-;  call void @llvm.dbg.value(metadata i32 0, metadata !17, metadata !DIExpression()), !dbg !27
-;  br i1 %cmp, label %if.then, label %if.else
-;  [...]
-;
-; Insertion point is: %frombool = zext i1 %cmp to i8, !dbg !26
+; Check that hoisted instructions get unknown-location line numbers -- there
+; is no correct line number for code that has been common'd in this way.
 
 ; IR generated with:
 ; clang -S -g -gno-column-info -O2 -emit-llvm pr39187.cpp -o pr39187-g.ll -mllvm -opt-bisect-limit=10
@@ -54,7 +40,8 @@
 ; CHECK:  %frombool = zext i1 %cmp to i8, !dbg !16
 ; CHECK:  call void @llvm.dbg.value(metadata i8 %frombool, metadata !13, metadata !DIExpression()), !dbg !16
 ; CHECK:  call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !DIExpression()), !dbg !17
-; CHECK:  %. = select i1 %cmp, i32 8, i32 4, !dbg !16
+; CHECK:  %. = select i1 %cmp, i32 8, i32 4, !dbg ![[MERGEDLOC:[0-9]+]]
+; CHECK:  ![[MERGEDLOC]] = !DILocation(line: 0, scope: !7)
 
 ; ModuleID = 'pr39187.cpp'
 source_filename = "pr39187.cpp"
@@ -77,11 +64,11 @@
 
 if.then:                                          ; preds = %entry
   call void @llvm.dbg.value(metadata i32 8, metadata !14, metadata !DIExpression()), !dbg !25
-  br label %if.end
+  br label %if.end, !dbg !25
 
 if.else:                                          ; preds = %entry
-  call void @llvm.dbg.value(metadata i32 4, metadata !14, metadata !DIExpression()), !dbg !25
-  br label %if.end
+  call void @llvm.dbg.value(metadata i32 4, metadata !14, metadata !DIExpression()), !dbg !27
+  br label %if.end, !dbg !27
 
 if.end:                                           ; preds = %if.else, %if.then
   %beards.0 = phi i32 [ 8, %if.then ], [ 4, %if.else ]
Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1372,14 +1372,6 @@
     }
   }
 
-  // As the parent basic block terminator is a branch instruction which is
-  // removed at the end of the current transformation, use its previous
-  // non-debug instruction, as the reference insertion point, which will
-  // provide the debug location for generated select instructions. For BBs
-  // with only debug instructions, use an empty debug location.
-  Instruction *InsertPt =
-      BIParent->getTerminator()->getPrevNonDebugInstruction();
-
   // Okay, it is safe to hoist the terminator.
   Instruction *NT = I1->clone();
   BIParent->getInstList().insert(BI->getIterator(), NT);
@@ -1393,11 +1385,8 @@
   // it involves inlinable calls.
   NT->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
 
+  // Phis created below will adopt NTs merged DebugLoc
   IRBuilder<NoFolder> Builder(NT);
-  // If an earlier instruction in this BB had a location, adopt it, otherwise
-  // clear debug locations.
-  Builder.SetCurrentDebugLocation(InsertPt ? InsertPt->getDebugLoc()
-                                           : DebugLoc());
 
   // Hoisting one of the terminators from our successor is a great thing.
   // Unfortunately, the successors of the if/else blocks may have PHI nodes in


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55272.176629.patch
Type: text/x-patch
Size: 3962 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181204/05986946/attachment.bin>


More information about the llvm-commits mailing list