[llvm-bugs] [Bug 43445] New: Merge constant literals

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Sep 25 06:28:44 PDT 2019


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

            Bug ID: 43445
           Summary: Merge constant literals
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: zamazan4ik at tut.by
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Consider the example:

static const char data1[] = {'t','e','s','t'};
static const char data2[] = "test test";

bool index0(const char* cmp) {
    return cmp == data1 || cmp == data2;
}


Right now Clang(trunk) with 'std=c++17 -O3' generates suboptimal assembly:

index0(char const*): # @index0(char const*)
  mov eax, offset data1
  cmp rdi, rax
  sete cl
  mov eax, offset data2
  cmp rdi, rax
  sete al
  or al, cl
  ret
data1:
  .ascii "test"

data2:
  .asciz "test test"


A more efficient way to generate the code is to merge `data1` and `data2`:

index0(char const*):
  mov eax, offset data
  cmp rdi, rax
  sete al
  ret
data:
  .ascii "test test"


Constant literals merging significantly reduces binary size and cache misses.

Godbolt playground: https://godbolt.org/z/z-rJ6J

Related GCC issue: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91899

-- 
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/20190925/66593676/attachment.html>


More information about the llvm-bugs mailing list