This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Lower unknown @llvm.objectsize early.
Needs ReviewPublic

Authored by ab on Jan 22 2015, 9:41 AM.
This revision needs review, but there are no reviewers specified.

Details

Reviewers
None
Summary

We already try to lower objectsize in InstCombine, but only if the
result is known. When it is unknown, the intrinsic calls would survive
the mid-level optimizers, to be lowered late, in CodeGenPrepare.

We can lower them in InstCombine as well, to 0 or -1, depending on the
min argument.
One could argue this is a bit early to do the lowering, since the size
might be made apparent by later optimizations. In practice however,
the majority of cases has a never-known objectsize, and in a lot of
the remaining few, the size is immediately apparent (say from a
global value, or an alloca).
Always keeping the intrinsic call intact prevents optimizations, and
makes memcpyopt useless when libcall fortification is enabled (the
default on a few major targets).

There are a few alternatives to doing it here; all seem overkill:

  • teach the libcall simplifier to look through unknown @llvm.objectsize, so it's able to replace the fortified version of a libcall with the non-fortified one if the size is unknown.
  • make a dedicated pass that lowers the intrinsic, even when the size is unknown, run it before memcpyopt & friends.
  • in memcpyopt, start by running the libcall simplifier, to lower @llvm.objectsize, even when the size is unknown.

Also, Nuno proposes a related improvement to the associated
APIs (MemoryBuiltins.h): "[the analyzer] could be improved to
produce an interval instead. If we know that the minimum
objectsize is larger than the written size, then the check could
go away, even if we don't know the exact size of the buffer."

Diff Detail

Event Timeline

ab updated this revision to Diff 18619.Jan 22 2015, 9:41 AM
ab retitled this revision from to [InstCombine] Lower unknown @llvm.objectsize early..
ab updated this object.
ab edited the test plan for this revision. (Show Details)
ab added subscribers: Unknown Object (MLST), nlopes.

Ahmed Bougacha wrote:

We already try to lower objectsize in InstCombine, but only if the
result is known. When it is unknown, the intrinsic calls would survive
the mid-level optimizers, to be lowered late, in CodeGenPrepare.

We can lower them in InstCombine as well, to 0 or -1, depending on the
min argument.
One could argue this is a bit early to do the lowering, since the size
might be made apparent by later optimizations.

Yep.

In practice however,

the majority of cases has a never-known objectsize, and in a lot of
the remaining few, the size is immediately apparent (say from a
global value, or an alloca).

Sure I'd expect that, but that isn't really a problem on its own.

Always keeping the intrinsic call intact prevents optimizations, and
makes memcpyopt useless when libcall fortification is enabled (the
default on a few major targets).

Can we fix that instead? Not waiting as long as possible to lower
unknown llvm.objectsize just can't be right.

There are a few alternatives to doing it here; all seem overkill:

  • teach the libcall simplifier to look through unknown @llvm.objectsize, so it's able to replace the fortified version of a libcall with the non-fortified one if the size is unknown.
  • make a dedicated pass that lowers the intrinsic, even when the size is unknown, run it before memcpyopt& friends.
  • in memcpyopt, start by running the libcall simplifier, to lower @llvm.objectsize, even when the size is unknown.

The first of those sounds best to me.

Also, Nuno proposes a related improvement to the associated
APIs (MemoryBuiltins.h): "[the analyzer] could be improved to
produce an interval instead. If we know that the minimum
objectsize is larger than the written size, then the check could
go away, even if we don't know the exact size of the buffer."

http://reviews.llvm.org/D7129

Files:

lib/Transforms/InstCombine/InstCombineCalls.cpp
test/Transforms/InstCombine/objsize.ll
test/Transforms/InstCombine/stpcpy_chk-1.ll
test/Transforms/InstCombine/strcpy_chk-1.ll

EMAIL PREFERENCES

http://reviews.llvm.org/settings/panel/emailpreferences/

llvm-commits mailing list
llvm-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits