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: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected]

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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to