<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In order to identify loops I'm using the DILexicalScope metadata<br>
attached to the loop latch, but with some combinations of optimisations<br>
that metadata seems to disappear.<br>
<br></blockquote><div><br></div><div style>Looks like line number information yes?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
For example, when -simplifycfg removes a block because it only contains<br>
a branch, and -loop-simplify recreates that block because it turned out<br>
to be a back-edge of some loop, the metadata gets removed.<br>
<br>
Specifically, using the following testcase:<br>
  while (while_test()) {<br>
    if (if_test()) {<br>
      foo();<br>
    } else {<br>
      bar();<br>
    }<br>
  }<br>
<br>
... Clang emits* the following IR:<br>
*after slightly modifying the code generator so that it also emits<br>
metadata for unconditional branches<br>
  while.cond:<br>
    %call = call zeroext i1 (...)* @while_test(), !dbg !8<br>
    br i1 %call, label %while.body, label %while.end, !dbg !8<br>
  while.body:<br>
    %call1 = call zeroext i1 (...)* @if_test(), !dbg !9<br>
    br i1 %call1, label %if.then, label %if.else, !dbg !9<br>
  if.then:<br>
    call void (...)* @foo(), !dbg !11<br>
    br label %if.end, !dbg !13<br>
  if.else:<br>
    call void (...)* @bar(), !dbg !14<br>
    br label %if.end, !dbg !16<br>
  if.end:<br>
    br label %while.cond, !dbg !17<br>
  while.end:<br>
    ret i32 0, !dbg !18<br>
<br>
If I run -simplifycfg, the if.then, if.else and if.end blocks get<br>
transformed as follows:<br>
  if.then:<br>
    call void (...)* @foo(), !dbg !11<br>
    br label %while.cond, !dbg !13<br>
  if.else:<br>
    call void (...)* @bar(), !dbg !14<br>
    br label %while.cond, !dbg !16<br>
<br>
Removal of the back-edge (and its metadata) is problematic for other<br>
loop passes, which is why -loop-simplify creates an artificial back-edge<br>
like this:<br>
  if.then:<br>
    call void (...)* @foo(), !dbg !11<br>
    br label %while.cond.backedge, !dbg !13<br>
  if.else:<br>
    call void (...)* @bar(), !dbg !14<br>
    br label %while.cond.backedge, !dbg !16<br>
  while.cond.backedge:<br>
    br label %while.cond<br>
<br>
Ultimately I end up with a back-edge with no metadata whatsoever,<br>
breaking my loop identification. What is the best way to work around<br>
this? Modify -simplifycfg so that it does not simplify loop back-edges?<br>
Or somehow add the metadata to the new back-edges?<br><span class="HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br></div><div style>You're identifying loops in IR based on line number information?</div>
<div style><br></div><div style>That said, we could probably figure out a way to add line information to</div><div style>the back edge if we're getting something wrong in our optimized line tables -</div><div style>not a high priority though.</div>
<div style><br></div><div style>-eric </div></div><br></div></div>