[llvm-dev] Redundant copies

Roger Ferrer Ibáñez via llvm-dev
Thu, 12 Mar 2020 18:06:00 -0000

Hi all,

we have encountered a case of redundant copies still left in the final code
and we would like to, at least, mitigate it. The original motivating case
comes from a context where we have large vector registers. In that context,
copies are expensive and we would like to avoid them as much as possible.

This small testcase in C, similar to the original vector case, exposes the
issue but using scalars.

long a, b;
long fn1();
long fn2() {
  long c = a, d = c;
  for (; b;) {
    long e = fn1();
    d = d + e;
  }
  long f = d - c;
  return f;
}

For instance in RISC-V we emit something like this but other backends like
ARM or X86 show the same behaviour.

add s0, zero, s2   # ← copy
beqz a0, .LBB0_3
# %bb.1:                                # %for.body.preheader
add s0, zero, s2  # ← not needed
.LBB0_2:                                # %for.body

Has anyone encountered a similar issue like this in the past?

We are looking into removing these copies with a post RA pass to address
the most obvious case: if we see a copy with the same physregs in dest and
source to an earlier one and the reaching definition of the dest and source
registers is one and the same, then that copy should be redundant.

This might be too specific though, so perhaps there are better approaches?

Thanks!

--
Roger Ferrer Ibáñez
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20200312/ecc0e034/attachment.html>