This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Calloc-ed strings optimizations
ClosedPublic

Authored by xbolva00 on May 18 2018, 5:20 AM.

Diff Detail

Event Timeline

xbolva00 created this revision.May 18 2018, 5:20 AM
This comment was removed by xbolva00.

This looks overly specific. Can you teach GetStringLength about this instead? All of the transformations should already work if GetStringLength returns a length of zero.

xbolva00 updated this revision to Diff 147552.May 18 2018, 10:52 AM

Extend GetStringLength with knowledge if the value comes from calloc.

xbolva00 added inline comments.May 21 2018, 2:56 AM
include/llvm/Analysis/ValueTracking.h
277

Or should I leave const here and then const_cast for dyn_cast in isStringFromCalloc?

bkramer added inline comments.May 21 2018, 3:04 AM
include/llvm/Analysis/ValueTracking.h
277

Leave the const here and make the variables in isStringFromCalloc const too. There should be no need for const_cast, dyn_cast will preserve const input types.

lib/Analysis/ValueTracking.cpp
3392

APInt::isNullValue() is preferable over getZExtValue()==0 because it also handles integer widths > 64

xbolva00 updated this revision to Diff 147756.EditedMay 21 2018, 3:32 AM
  • Added const to variables in isStringFromCalloc
  • Use APInt::isNullValue()
xbolva00 updated this revision to Diff 147758.May 21 2018, 3:33 AM
xbolva00 marked 3 inline comments as done.
  • Fixed a comment
bkramer added inline comments.May 21 2018, 3:43 AM
lib/Transforms/Utils/SimplifyLibCalls.cpp
207

Is this correct? strncpy fills the rest of the string with null bytes, strncat does not.

222

What if strlen(Src) < Len?

xbolva00 added inline comments.May 21 2018, 3:53 AM
lib/Transforms/Utils/SimplifyLibCalls.cpp
207

Good point. So we can do nothing at all in this case? or use memcpy + store null byte?

222

That is ok
Len < SrcLen case is handled below...
and then we return emitStrLenMemCpy(Src, Dst, SrcLen, B);

xbolva00 updated this revision to Diff 147761.May 21 2018, 4:06 AM
xbolva00 edited the summary of this revision. (Show Details)
xbolva00 added inline comments.May 21 2018, 5:52 AM
lib/Transforms/Utils/SimplifyLibCalls.cpp
207

" memcpy + store null byte: ... Nope, cannot do this. It will copy bytes even after \0. Strncat does not do it.

I removed incorrect transformations for str(n)cat to str(n)cpy. So patch is done.

Is it ok now?

This revision is now accepted and ready to land.May 22 2018, 6:32 AM
This revision was automatically updated to reflect the committed changes.
spatel added a subscriber: spatel.May 22 2018, 9:19 AM
spatel added inline comments.
llvm/trunk/test/Transforms/InstCombine/zero-string.ll
62 ↗(On Diff #148018)

Make sure you test locally before committing. There's no final '}' here, so this broke bots.
Hopefully fixed with rL332990

62 ↗(On Diff #148018)

Wrong link:
rL332992

It looks like this patch doesn't handle the case where the memory allocated by calloc() is modified?

xbolva00 added a comment.EditedMay 22 2018, 12:27 PM

It looks like this patch doesn't handle the case where the memory allocated by calloc() is modified?

Ok, I will remove it from InstCombine - https://reviews.llvm.org/D47218

It could be moved to DSE later with that check.

Well, we cannot teach GetStringLength about it then. So it is useless anyway to move it to DSE?