OSDN Git Service

[Reassociate] swap binop operands to increase factoring potential
authorSanjay Patel <spatel@rotateright.com>
Sun, 2 Sep 2018 14:22:54 +0000 (14:22 +0000)
committerSanjay Patel <spatel@rotateright.com>
Sun, 2 Sep 2018 14:22:54 +0000 (14:22 +0000)
commit722ea18b54e263b61132cb7ccc62cd9cec871490
treec34fe78afb55c3f2825fd783780c01e4aa51fad2
parent41b17bcd0d14214488b50b973780766d6302d6d5
[Reassociate] swap binop operands to increase factoring potential

If we have a pair of binops feeding another pair of binops, rearrange the operands so
the matching pair are together because that allows easy factorization folds to happen
in instcombine:
((X << S) & Y) & (Z << S) --> ((X << S) & (Z << S)) & Y (reassociation)

--> ((X & Z) << S) & Y (factorize shift from 'and' ops optimization)

This is part of solving PR37098:
https://bugs.llvm.org/show_bug.cgi?id=37098

Note that there's an instcombine version of this patch attached there, but we're trying
to make instcombine have less responsibility to improve compile-time efficiency.

For reasons I still don't completely understand, reassociate does this kind of transform
sometimes, but misses everything in my motivating cases.

This patch on its own is gluing an independent cleanup chunk to the end of the existing
RewriteExprTree() loop. We can build on it and do something stronger to better order the
full expression tree like D40049. That might be an alternative to the proposal to add a
separate reassociation pass like D41574.

Differential Revision: https://reviews.llvm.org/D45842

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341288 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/Transforms/Scalar/Reassociate.h
lib/Transforms/Scalar/Reassociate.cpp
test/Transforms/Reassociate/matching-binops.ll