This is an archive of the discontinued LLVM Phabricator instance.

[IndVarSimplify][DebugLoc] When widening the exit loop condition, correctly reuse the debug location of the original comparison.
ClosedPublic

Authored by andreadb on Oct 25 2016, 11:17 AM.

Details

Summary

This is a follow-up of D25872.

When the loop exit condition is canonicalized as a != comparison, reuse the debug loc associated to the original (non canonical) comparison.

extern int foo(int);  // line 1

int bar(int X, int *A) {
  for (int i = 0; i < X; ++i)  // line 4
    A[i] = foo(i);

  return A[0];
}

Before IndVarSimplify is run, the optimized LLVM IR of the loop latch looks like this:

for.inc:
  %inc = add nsw i32 %i.06, 1, !dbg !13
  %cmp = icmp slt i32 %inc, %X, !dbg !7
  br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge, !dbg !9, !llvm.loop !15

Where !dbg !7 is defined as:

!7 = !DILocation(line: 4, column: 21, scope: !8)
!8 = !DILexicalBlockFile(scope: !5, file: !1, discriminator: 1)

IndVarSimplify rewrites the exit condition of the loop to be a canonical != comparison. In this particular case, it rewrites the comparison in the loop latch as follows:

%wide.trip.count = zext i32 %X to i64, !dbg !9
%exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count, !dbg !9

However, !dbg !9 is defined as:

!8 = !DILexicalBlockFile(scope: !5, file: !1, discriminator: 1)
!9 = !DILocation(line: 4, column: 3, scope: !8)

Instead, it should be:

!7 = !DILocation(line: 4, column: 21, scope: !8)
!8 = !DILexicalBlockFile(scope: !5, file: !1, discriminator: 1)

The problem is indirectly caused by the fact that the debug location of the new icmp is obtained from the loop latch terminator (i.e. our IRBuilder "insertion point").

This patch fixes the issue by correctly setting the IRBuilder's "current debug location" to the location of the original compare instruction.

Please let me know if okay to commit.

Thanks,
Andrea

Diff Detail

Event Timeline

andreadb updated this revision to Diff 75734.Oct 25 2016, 11:17 AM
andreadb retitled this revision from to [IndVarSimplify][DebugLoc] When widening the exit loop condition, correctly reuse the debug location of the original comparison..
andreadb updated this object.
andreadb added a subscriber: llvm-commits.
aprantl accepted this revision.Oct 25 2016, 1:11 PM
aprantl edited edge metadata.

Seems plausible, thanks!

This revision is now accepted and ready to land.Oct 25 2016, 1:11 PM
This revision was automatically updated to reflect the committed changes.