[llvm-bugs] [Bug 52195] New: missed optimization: external pointer used as integer should fold into immediate operand

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Oct 16 16:05:13 PDT 2021


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

            Bug ID: 52195
           Summary: missed optimization: external pointer used as integer
                    should fold into immediate operand
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: jhaberman at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

When a program converts an external pointer to an integer, LLVM won't use the
resulting value as an immediate operand.

Example 1: https://godbolt.org/z/hnKoWcvGc

  #include <stdint.h>

  extern char addr;
  void g(unsigned);

  void f(unsigned x) {
    unsigned val = (unsigned)(uintptr_t)&addr;
    return g(x + val);
  }

Output:

  f:                                      # @f
        mov     eax, offset addr
        add     edi, eax
        jmp     g                               # TAILCALL

Desired output:

  f:                                      # @f
        add     edi, offset addr
        jmp     g                               # TAILCALL

Example 2: https://godbolt.org/z/46vEEjbnY

  #include <stdint.h>

  extern char addr;
  void g(unsigned long);

  void f(unsigned long x) {
    unsigned long val = (unsigned)(uintptr_t)&addr;
    return g(x + val);
  }

Output:

  f:                                      # @f
        mov     eax, offset addr
        mov     eax, eax
        add     rdi, rax
        jmp     g                               # TAILCALL  

Desired output:

  f:                                      # @f
        add     rdi, offset addr
        jmp     g   

These examples may seem unusual, but I am considering a novel JIT design where
I would use the compiler's relocatable output as JIT input, and use the emitted
relocations to tell me where to "patch in" my runtime values (which will be
integers, not actually pointers).

-- 
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/20211016/d47a0448/attachment.html>


More information about the llvm-bugs mailing list