<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Inliner applies incorrect value mapping for recursive calls"
   href="https://llvm.org/bugs/show_bug.cgi?id=31278">31278</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Inliner applies incorrect value mapping for recursive calls
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bjoern.boenninghoff@tu-dortmund.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=17713" name="attach_17713" title="code for simple demo reproducing incorrect inlining">attachment 17713</a> <a href="attachment.cgi?id=17713&action=edit" title="code for simple demo reproducing incorrect inlining">[details]</a></span>
code for simple demo reproducing incorrect inlining

"clang -O3" or "opt -inline" may work incorrectly on recursive functions. I
could narrow down and reproduce this for a simple function:

define void @tryme(i32* %ret, i32 %mode, i32 %val) #0 {
  %1 = icmp ne i32 %mode, 0
  br i1 %1, label %4, label %2

; <label>:2                                       ; preds = %0
  %3 = add nsw i32 %val, 5
  store i32 %3, i32* %ret, align 4
  br label %6

; <label>:4                                       ; preds = %0
  %5 = sub nsw i32 %val, 5
  call void @tryme(i32* %ret, i32 0, i32 %5)
  br label %6

; <label>:6                                       ; preds = %4, %2
  ret void
}

This should store %val + 5 to %ret if %mode == 0, and %val otherwise.

opt -inline will transform this to: 

define void @tryme(i32* %ret, i32 %mode, i32 %val) #0 {
  %1 = icmp ne i32 %mode, 0
  br i1 %1, label %4, label %2

; <label>:2                                       ; preds = %0
  %3 = add nsw i32 %val, 5
  store i32 %3, i32* %ret, align 4
  br label %6

; <label>:4                                       ; preds = %0
  %5 = sub nsw i32 %val, 5
  store i32 %5, i32* %ret, align 4
  br label %6

; <label>:6                                       ; preds = %4, %2
  ret void
}

Notice how the second store now stores %5 = %val - 5 (instead of %val - 5 + 5 =
%val).

Attached you will find some C code that produces above and also breaks with
clang -O3, I assume for the same reason.

I tried to track down the root cause in the inliner, and got to the
per-instruction loop in the PruningFunctionCloner::CloneBlock in
lib/Transforms/Utils/CloneFunction.cpp. Here, %val in %3 of the called function
gets mapped to %5, then %3 is correctly simplified to %val of the caller. It
seems like, as caller and callee are identical, %val is now incorrectly mapped
back to %5, thus %3 in is replaced by %5, not %val. It appears to me as if
currently VMap content is not suitable to distinguish caller and callee
versions of Values and their respective mappings for recursive functions.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>