Skip to content

Fix incorrectly-preserved flags in simplifier - #6847

Merged
vijaysun-omr merged 5 commits into
eclipse-omr:masterfrom
jdmpapin:simplifier-flags
Jan 31, 2023
Merged

vijaysun-omr merged 5 commits into
eclipse-omr:masterfrom
jdmpapin:simplifier-flags

Conversation

@jdmpapin

Copy link
Copy Markdown
Contributor
  • Make TR::Node::recreate() unset the cannotOverflow flag
  • Auto-set X==0, X!=0, X>=0, X<=0 flags correctly for constant nodes
  • Make VPConstraint::{add,sub}WithOverflow() freestanding functions
  • Preserve flags in simplifier transformations
  • Stop recreating iadd/isub as imul when distributing in simplifier

Please see the individual commit messages.

This flag is about the operation to be performed, which is changing.
Flags that describe the resulting value can always be kept. There are
likely other flags that should be unset here, but this is a start.

The following are a few examples of cases where the new/modified
operation could overflow even if the original cannot. The first one is
implemented in simplifier. The second will probably not happen, since
the compiler prefers isub, but it is simple and hopefully illustrative.

    imul (cannotOverflow)           ineg
      x                     ==>       imul
      iconst -2                         x
                                        iconst 2

Here, there is (in general) a possibility that x could be 2**30. In that
case, multiplying by -2 gives -2**31 (i.e. MIN) without overflow.
However, if we multiply instead by 2, the true product is 2**31, which
is not representable, so the product overflows and produces MIN,
Furthermore, if the result of imul is MIN, then ineg overflows too.

    isub (cannotOverflow)           iadd
      x                     ==>       x
      iconst MIN                      iconst MIN

For isub not to overflow, we require only that x be negative. The iadd
will have the same result, but because x is negative, it will always
overflow.
...whenever the constant value is updated, including at construction.

Also set highWordZero correctly for lconst nodes at the same time.

Most importantly, whenever a constant node is mutated in-place, this
clears any of the aforementioned flags that no longer apply to the new
value.

I thought that such mutations were considered to be incorrect, since
they change the result of the node. Either way, at least one pass
(simplifier) modifies constant nodes with refcount 1. Since there are
likely many places where it does this, and since improper preservation
of flags is more or less the only harm it can do, this commit helps such
passes by freshly determining the values of some flags that may have
become stale.

Passes that may remember some fact about a constant node based on its
identity (pointer/global index), e.g. by memoizing the results of a
function, must not update constant nodes in this way if doing so could
invalidate such a remembered fact. Or at the very least they must be
able to forget or correct the fact, though attempting to do so is error
prone.
These are useful more broadly and should be more widely available.

Additionally:

- Add static assertions to check that the argument types are signed
  integral types (as they were already required to be).

- Define a StickyOverflowFlag type and corresponding overloads for
  determining whether there is any overflow in a series of operations.
In particular, for a constant c > 0, preserve:

- zero/sign flags and cannotOverflow when normalizing iadd to isub, i.e.
  where x+c becomes x-(-c);

- cannotOverflow when rewriting an integer addition (-x)+y to y-x;

- cannotOverflow when normalizing isub to iadd, i.e. where x-c becomes
  x+(-c);

- cannotOverflow when rewriting a negation -(x-y) to y-x;

- cannotOverflow (if possible) when coalescing right-hand-side constants
  for consecutive iadd/isub operations; and

- cannotOverflow (if possible) when extracting ineg from imul, i.e.
  where x*(-c) becomes -(x*c);

In all but the first case, the zero/sign flags are already preserved.

Additionally, in the last case, i.e. when extracting ineg from imul, the
resulting ineg and the new imul are either both zero or both nonzero. If
ineg can't overflow, then neither can the new imul, and any pair of
corresponding nonzero results from imul and ineg have opposite signs.
The new multiplication does not have the same result as the original
addition or subtraction did.
@vijaysun-omr

Copy link
Copy Markdown
Contributor

I have reviewed only a couple of commits so far, and will continue as time permits over the next (working) day or two. In the meantime I will launch some more tests.

@vijaysun-omr

Copy link
Copy Markdown
Contributor

Jenkins build all

@vijaysun-omr

Copy link
Copy Markdown
Contributor

Completed my review and the main change seems to be the extraction of the routines to do the sub/add with overflow checking and their use in simplifier (checked the different cases where the overflow flag was being set and they look correct to me). Tests also seem to have passed. Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants