Hi Taewook,

I have prototypes, and they work well enough that i was convinced it’s not a big deal to implement.

If you are interested in working on it, let me know.

Otherwise, i’ll get to it.

I’m just knocking down the rest of the generated code perf differences i can find that matter between gvn and newgvn.

we are fairly close (IMHO) at this point.

Though i wrote GCC’s current GVN-PRE, i am unlikely to take exactly the same approach for LLVM (iterative dataflow)

I am also still testing out various non-“lifetime optimal” approaches.

I am busy making the current GVN a complete one, which will eliminate:

- the need for full redundancy elimination
- issues that pop up where we discover we could phi values together (this is really a case of #1).

That will leave *literally* partial redundancies as the only thing that needs to be eliminated, simplifying our task.

The only source of incompleteness in NewGVN is the inability to consider phi of ops and op of phis the same thing (and the related phi + op and op + phi)

Here is an example (pull from a real program where it makes a significant perf difference)

:

; :9: ; preds = %7, %4

%10 = phi i64 [ %0, %4 ], [ %11, %7 ]

%11 = add nsw i64 %10, -1

%12 = load i64, i64* getelementptr inbounds ([100 x i64], [100 x i64]* @a, i64 0, i64 0), align 16, !tbaa !2

%13 = load i64, i64* getelementptr inbounds ([100 x i64], [100 x i64]* @b, i64 0, i64 0), align 16, !tbaa !2

%14 = mul nsw i64 %13, %12

%15 = icmp eq i64 %14, 0

br i1 %15, label %7, label %16

; : 17

%18 = phi i64 [ %26, %17 ], [ %13, %16 ]

%19 = phi i64 [ %24, %17 ], [ %12, %16 ]

%20 = phi i64 [ %22, %17 ], [ 0, %16 ]

%21 = mul nsw i64 %18, %19

store i64 2, i64* %2, align 8, !tbaa !2

%22 = add nuw nsw i64 %20, 1

%23 = getelementptr inbounds [100 x i64], [100 x i64]* @a, i64 0, i64 %22

%24 = load i64, i64* %23, align 8, !tbaa !2

%25 = getelementptr inbounds [100 x i64], [100 x i64]* @b, i64 0, i64 %22

%26 = load i64, i64* %25, align 8, !tbaa !2

%27 = mul nsw i64 %26, %24

%28 = icmp eq i64 %22, %27

br i1 %28, label %6, label %17

The multiply at 18, 19 is completely redundant.

It is a phi of the computations in 9 and 17.

Once done (almost!), newgvn will actually detect that a simple phi inserted in 17 would have the same value as %21.

The work is https://bugs.llvm.org//show_bug.cgi?id=31868

Wwhen we see op of phis, we use fake instructions to treat it like we saw ops in the predecessor blocks, and see whether the fake phi we build ends up with real instructions as for the operands. if it does, we insert it.

Note this computation is similar to the one SSAPRE does to determine whether anything is redundant.

Once you do this, i believe all initial partial redundancies (IE not second order effects) in the program will be of a form of a fake phi with one real leader, one fake., recursively [1]

Then, again, you perform a similar sparse dataflow that SSAPRE does to figure out which will, if inserted, make other computations redundant.

IE c = a + b

if (foo)

b = 50

d = a + b

this will end up as d = op + phi

WE will convert to phi of ops form, phi(a + 50, a +b)

One real (a+b), one fake (a+50).

full availability will show up as real,real:

int x, c, y;

x = 3;

if (c)

x = 2;

y = x + 1;

return y;

phi(2 + 1, 3 + 1)

etc

The only practical difference will be the computation of what we want to insert (in FRE, we would insert only fully-available phis, in PRE, we insert instructions to make the not-fully-available ones real)