Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare Expressions: Fix normalization of exprs by adding zero #944

Merged
merged 1 commit into from Nov 18, 2020

Conversation

mgrang
Copy link
Contributor

@mgrang mgrang commented Nov 17, 2020

Previously, in order to normalize exprs we added zero only to exprs not
containing any other constants. Also, for binary nodes with + operator we added
+0 and for nodes with * operator we added *1. This resulted in exprs like "p -
i" which do not contain constants but also do not have + or * operator to not
get normalized. So we simply the logic to add zero. Now we simply add a binary
node with the + operator and a 0 child at the root of the AST. This makes the
logic simpler and handles a wider variety of cases.

This fixes issue #933

Previously, in order to normalize exprs we added zero only to exprs not
containing any other constants. Also, for binary nodes with + operator we added
+0 and for nodes with * operator we added *1. This resulted in exprs like "p -
i" which do not contain constants but also do not have + or * operator to not
get normalized. So we simply the logic to add zero. Now we simply add a binary
node with the + operator and a 0 child at the root of the AST. This makes the
logic simpler and handles a wider variety of cases.

This fixes issue #933
@sulekhark
Copy link
Contributor

Have we confirmed that constant folding will not fold i + 0 to i ? Will there be some circumstances where we want i + 0 to constant-fold to i, and some other circumstances where we do not want this to happen (ex: to maintain normalization)?

@mgrang
Copy link
Contributor Author

mgrang commented Nov 18, 2020

Have we confirmed that constant folding will not fold i + 0 to i ? Will there be some circumstances where we want i + 0 to constant-fold to i, and some other circumstances where we do not want this to happen (ex: to maintain normalization)?

Yes, I have confirmed that i + 0 will not constant fold to i. For example,

void f(int i) {
  _Nt_array_ptr<char> p : bounds(p, p + 0) = "a";

  if (*(p + 0))
  {}
}

On dumping the preorder AST we see that +0 is preserved:

+
ImplicitCastExpr 0x8927c50 '_Nt_array_ptr<char>' <LValueToRValue>
`-DeclRefExpr 0x8927c08 '_Nt_array_ptr<char>' lvalue Var 0x8927b68 'p' '_Nt_array_ptr<char>'
IntegerLiteral 0x8927f48 'int' 0

For the purpose of the preorder AST normalization we always want to preserve +0.

@sulekhark
Copy link
Contributor

LGTM.

Copy link
Contributor

@sulekhark sulekhark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Correctly widen bounds for exprs not containing addition or multiplication
2 participants