<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 --- - instcombine issue with statically linking in libc and whole-program optimizations" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D23814&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=52i9mWKcO6GuQVyp1yw4CSKl8xNfBntCzFMXMADGt08&s=b_ZPSr62u0M9GImWkf4jFXsgsuiYTft2eGlHSbJujj0&e=">23814</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>instcombine issue with statically linking in libc and whole-program optimizations
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>tools
          </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>opt
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>alonzakai@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>-instcombine did something that I found surprising: transformed @printf into
@puts, even though @printf was defined, not declared, and it was internal. In
more detail, imagine that we link in libc statically and strip out the parts we
don't actually use, and call internalize, giving us this:

  @.str = private unnamed_addr constant [18 x i8] c"printf from main\0A\00",
align 1

  define internal i32 @printf(i8* %c, ...) #0 {
    call void asm sideeffect " magic! ", "~{dirflag},~{fpsr},~{flags}"() #1,
!srcloc !1
    ret i32 0
  }

  define internal i32 @main() #0 {
    %0 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18
x i8]* @.str, i32 0, i32 0))
    ret i32 0
  }

(the @printf here just has an asm that does "magic!" instead of writing out a
full printf). Now, if we want to do some whole-program optimizations, we might
run -instcombine, giving us

  define internal i32 @printf(i8* %c, ...) #0 {
    call void asm sideeffect " magic! ", "~{dirflag},~{fpsr},~{flags}"() #1,
!srcloc !1
    ret i32 0
  }

  define internal i32 @main() #0 {
    %puts = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]*
@str, i64 0, i64 0))
    ret i32 0
  }

  declare i32 @puts(i8* nocapture) #1

@main's call to the internal @printf, a define, has been turned into a call of
a declare of @puts. But, since we already linked in libc statically, this is
not what we wanted - we are not going to link in anything else.

It surprises me that -instcombine is willing to transform a call to an
internally defined method. Should it perhaps leave such calls alone?

If it does not leave them alone, I think there might be other dangers. Imagine
if @puts were present in this file. And if some whole-program optimization
noticed that all @puts calls have some property, and it then optimized @puts
given that assumption (say, that the input is 5 chars or less). This could be
valid, because everything is internalized, so we see all the possible calls to
@puts. But then turning a @printf into a @puts might lead to surprising
results.</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>