This is an archive of the discontinued LLVM Phabricator instance.

[ValueLattice] Add move constructor
ClosedPublic

Authored by nikic on Apr 18 2020, 6:59 AM.

Details

Summary

Following the rule of five, declare move constructor and move assignment operator for ValueLatticeElement. This allows moving the ConstantRange (and the APInts it contains) rather than copying it.

This gives no improvement for most practical purposes, because we tend to work on ranges with <= 64 bit, in which case copies are cheap. This would only make a difference if the APInts have allocated backing storage.

So, not sure if worthwhile, but I figured it put it out there anyway...

Diff Detail

Event Timeline

nikic created this revision.Apr 18 2020, 6:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 18 2020, 6:59 AM
nikic marked 2 inline comments as done.Apr 18 2020, 7:03 AM
nikic added inline comments.
include/llvm/Analysis/ValueLattice.h
129 ↗(On Diff #258517)

Happy to commit this part separately: This nulling of ConstVal looks unnecessary to me. ConstVal is not an owned pointer, so this doesn't free it. If we change to a state that does not the ConstVal, then the value simply does not matter and we can leave it alone.

153 ↗(On Diff #258517)

I went for a slightly simpler implementation than the copy assignment operator here by always destroying and constructing, rather than trying to do an assignment if we go from one range to another range. That makes (theoretical, but probably not practical) sense for copy assignment, but not for move assignment.

fhahn added inline comments.Apr 18 2020, 12:47 PM
include/llvm/Analysis/ValueLattice.h
129 ↗(On Diff #258517)

It would be great if you could commit that separately.

153 ↗(On Diff #258517)

Yeah that should be fine. The main reason was to avoid constructing a range unnecessarily, but as the vast majority of ranges has tbitwidths <= 64 that should not matter in practice. Best to keep the assignment & move operators in sync in that respect.

nikic updated this revision to Diff 258553.Apr 18 2020, 1:55 PM

Rebase, simplify copy constructor.

nikic updated this revision to Diff 258594.Apr 19 2020, 1:54 AM
nikic marked an inline comment as done.

Base assignment operators on constructors rather than the other way around.

nikic marked an inline comment as done.Apr 19 2020, 1:58 AM
nikic added inline comments.
include/llvm/Analysis/ValueLattice.h
153 ↗(On Diff #258517)

After doing that change, I think it makes sense to implement the assignment operators in terms of destruct + construct, rather than implementing the constructors in terms of default construct and assign. The copy/move constructors are the more primitive operations.

I wasn't completely sure whether writing new (this) is safe, but it looks like there's already parts of LLVM using exactly this pattern, such as ImplicitConversionSequence.

nikic marked an inline comment as done.Apr 19 2020, 2:05 AM
nikic added inline comments.
include/llvm/Analysis/ValueLattice.h
145 ↗(On Diff #258594)

A better implementation of this would be:

memcpy(this, &Other, sizeof(ValueLatticeElement));
Other.Tag = unknown;

I'm not sure though whether this is legal C++ or not :)

fhahn accepted this revision.Apr 19 2020, 12:11 PM

LGTM, thanks. I think it would be good to update the description after the latest changes.

include/llvm/Analysis/ValueLattice.h
107 ↗(On Diff #258594)

might be a good opportunity to update Const -> ConstVal.

This revision is now accepted and ready to land.Apr 19 2020, 12:11 PM
This revision was automatically updated to reflect the committed changes.