This is an archive of the discontinued LLVM Phabricator instance.

[SLSR] simplify ternary adds by reassociation
AbandonedPublic

Authored by jingyue on Apr 8 2015, 9:27 AM.

Details

Summary

Similar to handling existing candidate forms, this update
pattern-matches

x = a + c
y = (a + b) + c // x dominates y

and rewrites y as

y = x + b.

To be conservative, we only do this when y is the only user of (a + b).

Diff Detail

Event Timeline

jingyue updated this revision to Diff 23424.Apr 8 2015, 9:27 AM
jingyue retitled this revision from to [SLSR] simplify ternary adds by reassociation.
jingyue updated this object.
jingyue edited the test plan for this revision. (Show Details)
jingyue added reviewers: broune, hfinkel, meheff, sanjoy.
jingyue added a subscriber: Unknown Object (MLST).
sanjoy edited edge metadata.Apr 8 2015, 1:23 PM

Should this rule live inside -reassociate? Optimizing

 define void @reassociate(i32 %a, i32 %b, i32 %c) {
  %1 = add i32 %a, %c
  call void @foo(i32 %1)
  %2 = add i32 %a, %b
  %3 = add i32 %2, %c
  call void @foo(i32 %3)
  ret void
}

to

 define void @reassociate(i32 %a, i32 %b, i32 %c) {
  %1 = add i32 %a, %c
  call void @foo(i32 %1)
  %3 = add i32 %1, %b
  call void @foo(i32 %3)
  ret void
}

looks like a generally good idea to me.

Hi Sanjoy,

I agree the reassociation performed here is generally good, and I wish I
could put it in -reassociate.

However, I have two major concerns.

  1. The algorithm in -reassociate is local in the sense that it ranks nodes

in each individual expression tree and reorders them according to their
ranks. The algorithm implemented here is global: it reassociates an
expression in the way that we can leverage other instructions in the same
function. I also found the workflow of my algorithm nicely fit into SLSR
which scans instructions in the DFS order and simplifies them with respect
to their dominators.

  1. -reassociate happens too early. Many opportunities I discovered are

exposed after loop unrolling, but -reassociate happens before loop
unrolling.

Jingyue

jingyue abandoned this revision.Apr 9 2015, 9:13 AM

I am thinking of a better alternative to implement this optimization in a separate NaryReassociate pass. I'll see how that goes.