This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Improve strncpy to memcpy optimization
AbandonedPublic

Authored by xbolva00 on Aug 10 2018, 2:51 AM.

Details

Reviewers
efriedma
spatel
Summary

We optimize
int f1(void) {

char buf[20];
strncpy(buf, "hi", 3);
puts(buf);

}

to memcpy, but we bail out in the following case:

int f2(void) {

char buf[20];
strncpy(buf, "hi", 4);
puts(buf);

}

This patch fixes it.

Godbolt: https://godbolt.org/g/ganpSH

Diff Detail

Event Timeline

xbolva00 created this revision.Aug 10 2018, 2:51 AM
xbolva00 updated this revision to Diff 160077.Aug 10 2018, 2:58 AM
xbolva00 added a reviewer: spatel.
xbolva00 updated this revision to Diff 160080.Aug 10 2018, 3:03 AM
xbolva00 edited the summary of this revision. (Show Details)Aug 10 2018, 4:14 AM
xbolva00 updated this revision to Diff 160087.Aug 10 2018, 4:41 AM
xbolva00 edited the summary of this revision. (Show Details)
  • Updated tests and description
xbolva00 abandoned this revision.Aug 10 2018, 6:03 AM

"strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written"

So we cant do this. (or we can if we know it the buf is zero initialized)