[PATCH] D37163: [LICM] sink through non-trivially replicable PHI

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 7 11:36:55 PDT 2017


efriedma added a comment.

It's possible to get quadratic code growth from LICM sinking.  For example:

  int foo();
  int unconditional_break(int n, int *p) {
  int r = 0;
  for (int i = 0; i < n; ++i) {
    int x = foo();
    int a = *p;
    int b = a * a;
    int c = b * b;
    int d = c * c;
    int e = d * d;
    int f = e * e;
    if (x == 1) { r = a; break; }
    if (x == 2) { r = b; break; }
    if (x == 3) { r = c; break; }
    if (x == 4) { r = d; break; }
    if (x == 5) { r = e; break; }
    if (x == 6) { r = f; break; }
  }
  return r;
  }
  
  int foo();
  int conditional_break(int n, int *p) {
  int r = 0;
  for (int i = 0; i < n; ++i) {
    int x = foo();
    int a = *p;
    int b = a * a;
    int c = b * b;
    int d = c * c;
    int e = d * d;
    int f = e * e;
    if (x == 1) { r = a; if (foo()) break; }
    if (x == 2) { r = b; if (foo()) break; }
    if (x == 3) { r = c; if (foo()) break; }
    if (x == 4) { r = d; if (foo()) break; }
    if (x == 5) { r = e; if (foo()) break; }
    if (x == 6) { r = f; if (foo()) break; }
  }
  return r;
  }

The source contains 5 multiplies, but the optimized IR has 15 due to sinking.  For the first function, sinking happens on trunk.  For the second function, this patch will allow sinking, I think.

That said, codesize bloat from hasn't ever shown up as a problem in practice; I doubt it will cause problems here.


https://reviews.llvm.org/D37163





More information about the llvm-commits mailing list