[ConstraintElimination] Add constraint elimination pass.
authorFlorian Hahn <flo@fhahn.com>
Tue, 15 Sep 2020 13:47:23 +0000 (14:47 +0100)
committerFlorian Hahn <flo@fhahn.com>
Tue, 15 Sep 2020 18:31:11 +0000 (19:31 +0100)
commit3d42d549554889ca182e1f3d31b23fa1383c6678
tree7b5d789e5eec15de65fad50aee4e08901a27479d
parentca76d6e94a30b8fe11a63d3a55d3903c7cd25b5d
[ConstraintElimination] Add constraint elimination pass.

This patch is a first draft of a new pass that adds a more flexible way
to eliminate compares based on more complex constraints collected from
dominating conditions.

In particular, it aims at simplifying conditions of the forms below
using a forward propagation approach, rather than instcomine-style
ad-hoc backwards walking of def-use chains.

    if (x < y)
      if (y < z)
        if (x < z) <- simplify

or

    if (x + 2 < y)
        if (x + 1 < y) <- simplify assuming no wraps

The general approach is to collect conditions and blocks, sort them by
dominance and then iterate over the sorted list. Conditions are turned
into a linear inequality and add it to a system containing the linear
inequalities that hold on entry to the block. For blocks, we check each
compare against the system and see if it is implied by the constraints
in the system.

We also keep a stack of processed conditions and remove conditions from
the stack and the constraint system once they go out-of-scope (= do not
dominate the current block any longer).

Currently there still are the least the following areas for improvements

* Currently large unsigned constants cannot be added to the system
  (coefficients must be represented as integers)
* The way constraints are managed currently is not very optimized.

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D84547
16 files changed:
llvm/include/llvm/Analysis/ConstraintSystem.h
llvm/include/llvm/InitializePasses.h
llvm/include/llvm/Transforms/Scalar.h
llvm/lib/Transforms/IPO/PassManagerBuilder.cpp
llvm/lib/Transforms/Scalar/CMakeLists.txt
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp [new file with mode: 0644]
llvm/lib/Transforms/Scalar/Scalar.cpp
llvm/test/Transforms/ConstraintElimination/dom.ll
llvm/test/Transforms/ConstraintElimination/geps.2d.ll
llvm/test/Transforms/ConstraintElimination/geps.ll
llvm/test/Transforms/ConstraintElimination/i128.ll
llvm/test/Transforms/ConstraintElimination/loops.ll
llvm/test/Transforms/ConstraintElimination/mixed.ll
llvm/test/Transforms/ConstraintElimination/uge.ll
llvm/test/Transforms/ConstraintElimination/ugt-ule.ll
llvm/test/Transforms/ConstraintElimination/ule.ll