This is an archive of the discontinued LLVM Phabricator instance.

[instsimplify] Generalize offset handling when compare pointers derived from allocas
AbandonedPublic

Authored by reames on Feb 17 2022, 1:14 PM.

Details

Summary

This generalizes the offset check used when figuring out if two pointers derived from two different allocas (or one alloca and one global) can be equal.

Specifically, it handles the following cases:

  • Both offsets are positive, but one or more crosses into the second object, but by less than the offset on the other pointer. (i.e. offset b is strictly greater than the offset a can obtain into object b.)
  • Both negative offsets
  • Mix of negative and positive offsets

I vaguely think there should be a more concise way to write these checks. Anyone see one?

Diff Detail

Event Timeline

reames created this revision.Feb 17 2022, 1:14 PM
reames requested review of this revision.Feb 17 2022, 1:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 17 2022, 1:14 PM
nikic added a comment.Feb 17 2022, 1:33 PM

I vaguely think there should be a more concise way to write these checks. Anyone see one?

As we're dealing with equality comparisons here, we can subtract a common offset from both sides. So rather than dealing with LHS + LHSOffset == RHS + RHSOffset, we can instead deal with LHS + Offset == RHS, where Offset = LHSOffset - RHSOffset. We should be able to fold that if and only if Offset > -MinSize(RHS) and Offset < MinSize(LHS).

Does that sound about right?

I vaguely think there should be a more concise way to write these checks. Anyone see one?

As we're dealing with equality comparisons here, we can subtract a common offset from both sides. So rather than dealing with LHS + LHSOffset == RHS + RHSOffset, we can instead deal with LHS + Offset == RHS, where Offset = LHSOffset - RHSOffset. We should be able to fold that if and only if Offset > -MinSize(RHS) and Offset < MinSize(LHS).

Does that sound about right?

It took some work to convince myself, but I think that's right.

nikic requested changes to this revision.Mar 15 2022, 1:23 AM

I like the general improvement here, but I think this will be a lot clearer if restructured as discussed above.

This revision now requires changes to proceed.Mar 15 2022, 1:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2022, 1:23 AM
reames abandoned this revision.Oct 24 2022, 9:43 AM

Closing review I'm not currently working on, and am unlikely to get back to in near future. Will reopen if priorities change.