[NewGVN] Plan for GVNPRE?


In some of our internal benchmarks, I observe that LLVM performs worse than GCC because LLVM fails to perform PRE when GCC can. I hope this problem goes away when NewGVN equipped with PRE, and wonder if anyone has an idea about the status of PRE on top of NewGVN. Thanks!



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:

  1. the need for full redundancy elimination
  2. 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)


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)

Hi Daniel,

Thank you for your detailed reply, and thank you for working on GVNPRE. I’d more than happy to test/evaluate it with our benchmark once it is ready. Please let me know if you need any help.



Of course As a heads up, it’s likely to be a while (ie 3-6 months or maybe longer) if you leave it to me.
I try to stay out of the critical path, because my real day job is managing folks who work on llvm (and other things), not working on llvm, and that has a lot of interrupts :slight_smile:

This is why i try to hand work off if it’s going to become important to someone.

Hi Daniel,

Got it. If that’s the case, can I implement it under the guidance of your insights/prototype? I think I can spend more time on implementation.


Sure, if you are up for it

We can start by building a simple, complete, PRE that is like the current GVN load-pre one, and improve it from there.
It shouldn’t be that hard.
The prototypes i had worked identically to GVN’s current Load PRE, just run after elimination.

That should give you pretty good results when put on top of newgvn, and contain the functionality you need to build any value based PRE (ie being able to take GVNExpressions and recreate usable instructions from them, etc).

The one thing i’d do differently would be to try to separate it entirely from the newgvn pass itself, and only rely on the congruence classes and gvn expressions.
(I’d also like to separate the instruction elimination from NewGVN as well, eventually)

That sounds like a good starting point. Thanks!