[LLVMbugs] [Bug 23814] New: instcombine issue with statically linking in libc and whole-program optimizations

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Jun 10 17:48:44 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=23814

            Bug ID: 23814
           Summary: instcombine issue with statically linking in libc and
                    whole-program optimizations
           Product: tools
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: opt
          Assignee: unassignedbugs at nondot.org
          Reporter: alonzakai at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

-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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150611/7fd43e5d/attachment.html>


More information about the llvm-bugs mailing list