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

Pointer analysis and store forwarding (remove loads after writes) #434

Merged
merged 56 commits into from
Apr 29, 2024
Merged

Conversation

rtjoa
Copy link
Collaborator

@rtjoa rtjoa commented Apr 5, 2024

Key types and relations

PtrPointees represents where a single pointer can point. The List<i64+IntInterval> is used as an association list; the i64 keys (corresponding to alloc ids) are always unique and sorted, the IntInterval values correspond to offset ranges.

(datatype PtrPointees
  (PointsTo List<i64+IntInterval>)
  (PointsAnywhere))

Pointees represents where an Expr can point to.

(datatype Pointees
  (TuplePointsTo List<PtrPointees>)
  (PtrPointsTo PtrPointees))

As an example: (TuplePointsTo [(PointsTo {0->[4,5], 1->[0,0]}), (PointsAnywhere)]) indicates a tuple with two components.

  • The first component might point to Alloc 0 at offset 4 or 5 or Alloc 1 at offset 0.
  • The second component might point anywhere.

The key function of the pointer analysis is:

(function PointsToCells (Expr Pointees) Pointees)

(= pointees (PointsToCells e arg-pointees)) indicates that when the (Arg) contained in e points to arg-pointees, e points to pointees.

Example optimization

This corresponds to test pqrs_deep_loop_swap:

p = alloc(alloc_id, 4, int*)
q = ptradd(p, 1)
r = ptradd(p, 2)
s = ptradd(p, 3)
// Note (p, r), (p, s), (q, r), (q, s) don't alias
do {
  do {
    p, q = q, p
  } while true;
  r, s = r, s
} while true;
// (p, r), (p, s), (q, r), (q, s) still shouldn't alias
*p = 10
*r = 20

return *p // should be unioned with 10, as p and r don't alias

@rtjoa rtjoa marked this pull request as ready for review April 19, 2024 21:57
@rtjoa rtjoa changed the title [Memory] Add PointsToCells analysis Add pointer analysis and optimize loads-after-writes Apr 19, 2024
@rtjoa rtjoa requested review from oflatt and ezrosent April 19, 2024 22:33
Copy link
Member

@oflatt oflatt left a comment

Choose a reason for hiding this comment

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

I approve!

@rtjoa rtjoa merged commit 44fa339 into main Apr 29, 2024
4 checks passed
@rtjoa rtjoa deleted the memory branch April 29, 2024 23:08
@rtjoa rtjoa changed the title Add pointer analysis and optimize loads-after-writes Add pointer analysis and store forwarding optimization May 1, 2024
@rtjoa rtjoa changed the title Add pointer analysis and store forwarding optimization Add pointer analysis and store forwarding (loads-after-writes) optimization May 1, 2024
@rtjoa rtjoa changed the title Add pointer analysis and store forwarding (loads-after-writes) optimization Pointer analysis and store forwarding (remove loads after writes) May 2, 2024
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.

4 participants