[llvm-bugs] [Bug 43888] New: Inline memcpy/memmove/memset with unknown size but bounded by derefenceable info

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Nov 2 16:34:48 PDT 2019


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

            Bug ID: 43888
           Summary: Inline memcpy/memmove/memset with unknown size but
                    bounded by derefenceable info
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define C 8
int d[C];
int s[C];

__attribute__((noinline))
void old(int *d, int *s, int n) {
    memcpy(d, s, n*sizeof(int));
}

__attribute__((noinline))
void new(int *d, int *s, int n) {

    switch(n) {
        case 8: memcpy(d, s, n*sizeof(int)); break;
        case 7: memcpy(d, s, n*sizeof(int)); break;
        case 6: memcpy(d, s, n*sizeof(int)); break;
        case 5: memcpy(d, s, n*sizeof(int)); break;
        case 4: memcpy(d, s, n*sizeof(int)); break;
        case 3: memcpy(d, s, n*sizeof(int)); break;
        case 2: memcpy(d, s, n*sizeof(int)); break;
        case 1: memcpy(d, s, n*sizeof(int)); break;
        case 0: break;
    }
}


int main(void) {
    for (int i = 0; i < 1000000000; ++i) {
        new(d, s, C);
    }
}



Idea:

Analyze memcpy's dst an src buffers - for example: infer that sizeof(dst) is 8
ints, sizeof(dst) is 8 ints. Replace small memcpy with jump table with inlined
memcpys (see "new" function).

Performance improvements:

For code above:

time ./old.out 

real    0m2,535s
user    0m2,534s
sys     0m0,000s

time ./new.out 

real    0m1,981s
user    0m1,981s
sys     0m0,000s


Or C = 8 ints and copy 6 ints.

time ./old.out 

real    0m3,123s
user    0m3,123s
sys     0m0,000s

time ./new.out 

real    0m1,979s
user    0m1,973s
sys     0m0,004s

-- 
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/20191102/bb0e957f/attachment.html>


More information about the llvm-bugs mailing list