This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Replace malloc+strcpy with malloc+memcpy
AbandonedPublic

Authored by xbolva00 on May 22 2018, 8:29 AM.

Details

Reviewers
efriedma
Summary

len = strlen(s) + 1;
d = malloc(len);
strcpy(d,s); // ---> memcpy(d,s, len);

Diff Detail

Event Timeline

xbolva00 created this revision.May 22 2018, 8:29 AM
xbolva00 updated this revision to Diff 148015.May 22 2018, 8:32 AM

Updated tests

xbolva00 updated this revision to Diff 148016.May 22 2018, 8:34 AM

Removed duplicated test

xbolva00 updated this revision to Diff 148017.May 22 2018, 8:35 AM

What does the malloc have to do with this transform? The transform here is just strcpy(d, s) -> memcpy(d, s, strlen(s)+1), where strlen(s) is free because we can CSE it with the previous call. I guess in general, you can transform strcpy(malloc(strlen(s)+1), s) to strdup, but that's not what you're doing.

Also, you're making the same mistake *again*: you're not checking whether the string is modified between the strlen and the strcpy.

What does the malloc have to do with this transform? The transform here is just strcpy(d, s) -> memcpy(d, s, strlen(s)+1), where strlen(s) is free because we can CSE it with the previous call. I guess in general, you can transform strcpy(malloc(strlen(s)+1), s) to strdup, but that's not what you're doing.

Also, you're making the same mistake *again*: you're not checking whether the string is modified between the strlen and the strcpy.

Ah yes, sure (why I still think that if value comes from malloc, it cannot modified between, sigh, sorry) :/
Too bad that helper function "isModifiedBetween" cannot be used here.

But with these transformations (malloc-memset, malloc-strcpy), arent we just transforming part of InstCombine to DSE? Are all these transformations legal there, I mean the right place for them?

If yes, I could rework this patch for DSE after some time.

xbolva00 planned changes to this revision.May 22 2018, 12:05 PM
xbolva00 abandoned this revision.Jun 12 2018, 12:27 PM