Commit Graph

56 Commits

Author SHA1 Message Date
Florian Hahn c59d95f6a4
[ConstraintElimination] Check if const. is small enough before using it
Check if the value of a ConstantInt is small enough to be used for
solving before calling getSExtValue.

Fixes #55085
2022-04-26 13:56:32 +01:00
Florian Hahn 5bedc1f093
[ConstraintElimination] Move logic to build worklist to helper (NFC).
This refactor makes it easier to extend the logic to collect information
from blocks in the future, without even further increasing the size of
eliminateConstriants.
2022-04-02 16:55:05 +01:00
Florian Hahn 3dbb5eb2cd
[ConstraintElimination] Move ConstraintInfo after ConstraintTy. (NFC)
Code movement to it slightly easier to use ConstraintTy & co in
ConstraintInfo directly, for follow-up patches.
2022-03-29 09:59:03 +01:00
Florian Hahn 8c3281db49
[ConstraintElimination] Use AddOverflow for offset summation.
Fixes an incorrect transformation due to values overflowing
https://alive2.llvm.org/ce/z/uizoea
2022-03-25 18:08:24 +00:00
Florian Hahn 470a975c84
[ConstraintElimination] Add missing dominance check.
When dealing with an unconditional branch, the condition can only added
if BB properly dominates the successor.
2022-03-16 20:01:24 +00:00
Florian Hahn f473d4aa80
[ConstraintElimination] Support BBs with single successor in CanAdd.
If BB has a single successor, conditions can be added safely.
2022-03-16 14:13:52 +00:00
Florian Hahn e10b0ea371
[ConstraintElimination] Remove over-eager assertion.
After moving the CanAdd check in c60cdb44f7 and using it for
the assume cases as well, the passed in block may not have  a branch
instruction as terminator. This can trigger the assertion. Given the new
use case, it doesn't add value any longer and can be removed.

Fixes https://github.com/llvm/llvm-project/issues/54281
2022-03-08 22:02:08 +00:00
Florian Hahn 4bbee17ecb
[ConstraintElimination] Use ZExtValue for unsigned decomposition.
When decomposing constraints for unsigned conditions, we can use
negative values by zero-extending them, as long as they are less than
the maximum constraint value.

Fixes https://github.com/llvm/llvm-project/issues/54224
2022-03-07 13:34:01 +00:00
Florian Hahn c60cdb44f7
[ConstraintElimination] Only add cond from assume to succs if valid.
Add missing CanAdd check before adding a condition from an assume
to the successor blocks. When adding information from assume to
successor blocks we need to perform the same CanAdd as we do for adding
a condition from a branch.

Fixes https://github.com/llvm/llvm-project/issues/54217
2022-03-07 12:01:15 +00:00
Florian Hahn 542c335159
[ConstraintElimination] Remove dead variables when dropping constraints.
This patch extends ConstraintElimination to also remove dead variables
when removing a constraint. When a constraint is removed because it is
out of scope, all new variables added for this constraint can also be
removed.

This keeps the total size of the systems much smaller, because it
reduces the number of variables drastically.

It also fixes a bug where variables where removed incorrectly.

Fixes https://github.com/llvm/llvm-project/issues/54228
2022-03-07 09:04:07 +00:00
serge-sans-paille 59630917d6 Cleanup includes: Transform/Scalar
Estimated impact on preprocessor output line:
before: 1062981579
after:  1062494547

Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D120817
2022-03-03 07:56:34 +01:00
Florian Hahn 00ab91b70d
[ConstraintElimination] Remove ConstraintListTy (NFCI).
This patch simplifies constraint handling by removing the
ConstraintListTy wrapper struct and moving the Preconditions directly
into ConstraintTy. This reduces the amount of memory needed for managing
constraints.

The only use case for ConstraintListTy was adding 2 constraints to model
ICMP_EQ conditions. But this can be handled by adding an IsEq flag. When
adding an equality constraint, we need to add the constraint and the
inverted constraint.
2022-02-18 14:35:01 +00:00
Florian Hahn 66400fc2dd
[ConstraintElimination] Support add with precondition.
If we can prove that an addition without wrap flags won't wrap, decompse
the operation.

Issue #48253
2022-02-11 20:26:25 +00:00
Florian Hahn 80eea38d8d
[ConstraintElimination] Remove unnecessary recursion (NFC).
Perform predicate normalization in a single switch, rather then going
through recursions.
2022-02-10 12:26:35 +00:00
Florian Hahn 79d60b93b4
[ConstraintElimination] Skip floating point compares. (NFC)
The solver only supports integer conditions. Adding floating point
compares to the worklist only adds extra work. Just skip them.
2022-02-09 21:16:49 +00:00
Florian Hahn b71eed7e8f
[ConstraintElimination] Remove redundant lookup (NFC). 2022-02-09 18:00:03 +00:00
Florian Hahn 902db4ec1c
[ConstraintElimination] Move some definitions closer to uses (NFC). 2022-02-09 17:29:49 +00:00
Kazu Hirata 3a3cb929ab [llvm] Use = default (NFC) 2022-02-06 22:18:35 -08:00
Florian Hahn 0a781d98fb
[ConstraintElimination] Add initial signed support.
This patch adds initial support for signed conditions. To do so,
ConstraintElimination maintains two separate systems, one with facts
from signed and one for unsigned conditions.

To start with this means information from signed and unsigned conditions
is kept completely separate. When it is safe to do so, information from
signed conditions may be also transferred to the unsigned system and
vice versa. That's left for follow-ups.

In the initial version, de-composition of signed values just handles
constants and otherwise just uses the value, without trying to
decompose the operation. Again this can be extended in follow-up
changes.

The main benefit of this limited signed support is proving >=s 0
pre-conditions added in D118799. But even this initial version also
fixes PR53273.

Depends on D118799.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D118806
2022-02-04 14:02:48 +00:00
Florian Hahn 06f3ef6626
[ConstraintElimination] Allow adding pre-conditions for constraints.
With this patch pre-conditions can be added to a list of constraints.
Constraints with pre-conditions can only be used if all pre-conditions
are satisfied when the constraint is used.

The pre-conditions at the moment are specified as a list of
(Predicate, Value *,Value *) tuples. This allow easily checking them
like any other condition, using the existing infrastructure.

This then is used to limit GEP decomposition to cases where we can
prove that offsets are signed positive.

This fixes a couple of incorrect transforms where GEP offsets where
assumed to be signed positive, but they were not.

Note that this effectively disables GEP decomposition, as there's no
support for reasoning about signed predicates. D118806 adds initial
signed support.

Fixes PR49624.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D118799
2022-02-04 11:45:07 +00:00
Florian Hahn 413e47ecd4
[ConstraintElimination] Handle degenerate case with branch to same dest.
When a conditional branch has the same block as both true and false
successor it is not safe to add the condition.

Fixes PR49819.
2022-02-03 11:09:14 +00:00
Florian Hahn 9fd7a2e379
[ConstraintElimination] Use constraints with 0 or 1 coefficients.
isConditionImplied is able to correctly handle 0 or 1 coefficients, so
let it handle those cases, rather than skipping them.
2022-01-27 18:41:33 +00:00
Florian Hahn 258a0a3a55
[ConstraintElimination] Use simplified constraint for == 0.
When checking x == 0, checking x u<= 0 is sufficient and simpler than
x u>= 0 && x u<= 0.

https://alive2.llvm.org/ce/z/btM7d3

    ----------------------------------------
    define i1 @src(i4 %a) {
    %0:
      %c = icmp eq i4 %a, 0
      ret i1 %c
    }
    =>
    define i1 @tgt(i4 %a) {
    %0:
       %c = icmp ule i4 %a, 0
       ret i1 %c
    }
    Transformation seems to be correct!
2022-01-27 13:31:23 +00:00
Florian Hahn a78ce48c37
[ConstraintElimination] Introduce struct to manage constraints. (NFC)
This patch adds a struct to manage a list of constraints. It simplifies
a follow-up change, that adds pre-conditions that must hold before a
list of constraints can be used.
2022-01-27 12:40:09 +00:00
Florian Hahn 8a15caaae5
[ConstraintElimination] Fix sign of sub decomposition.
Update the decomposition code to make sure the right coefficient (-1) is
used for the second operand of the subtract.

Fixes PR53123.
2022-01-24 18:32:32 +00:00
Florian Hahn aa5b6c9779
[ConstraintElimination] Initial support for using info from assumes.
This patch adds initial support to use facts from @llvm.assume calls. It
intentionally does not handle all possible cases to keep things simple
initially.

For now, the condition from an assume is made available on entry to the
containing block, if the assume is guaranteed to execute. Otherwise it
is only made available in the successor blocks.
2021-08-26 10:08:00 +01:00
Arthur Eubanks 6b9524a05b [NewPM] Don't mark AA analyses as preserved
Currently all AA analyses marked as preserved are stateless, not taking
into account their dependent analyses. So there's no need to mark them
as preserved, they won't be invalidated unless their analyses are.

SCEVAAResults was the one exception to this, it was treated like a
typical analysis result. Make it like the others and don't invalidate
unless SCEV is invalidated.

Reviewed By: asbirlea

Differential Revision: https://reviews.llvm.org/D102032
2021-05-18 13:49:03 -07:00
Florian Hahn 4858e081d7
[ConstraintElimination] Only strip casts preserving the representation.
Things like addrspacecast may not be no-ops, so we should not look
through them.
2021-03-26 20:07:41 +00:00
Kazu Hirata 4ed47858ab [llvm] Use llvm::drop_begin (NFC) 2021-02-22 20:17:16 -08:00
Florian Hahn 95daec6a84
[ConstraintElimination] Use unsigned > 0 instead of != 0.
ICMP_NE predicates cannot be directly represented as constraint. But we
can use ICMP_UGT instead ICMP_NE for %x != 0.

See https://alive2.llvm.org/ce/z/XlLCsW
2021-02-22 17:54:36 +00:00
Florian Hahn 3df5d5aace [ConstraintElimination] Fix variables used for pattern matching.
Re-using the matched variable in the pattern does not work as expected.
This patch fixes that by introducing a new variable for the 2nd level
match.
2021-02-14 18:42:37 +00:00
Florian Hahn 68dc90b347
[ConstraintElimination] Decompose a few more GEP indices.
This patch adds handling for zero-extended GEP indices.
2021-02-08 18:06:38 +00:00
Florian Hahn 1f1f037ed3
[ConstraintElimination] Improve index handing during constraint building.
This patch improves the index management during constraint building.
Previously, the code rejected constraints which used values that were not
part of Value2Index, but after combining the coefficients of the new
indices were 0 (if ShouldAdd was 0).

In those cases, no new indices need to be added. Instead of adding to
Value2Index directly, add new indices to the NewIndices map. The caller
can then check if it needs to add any new indices.

This enables checking constraints like `a + x <= a + n` to `x <= n`,
even if there is no constraint for `a` directly.
2021-02-08 13:05:13 +00:00
Florian Hahn ca268ed285
[ConstraintElimination] Decompose zext for unsigned compares.
For unsigned compares, zext should be a no-op and we can add the
extended value to the constraint system.
2021-02-07 20:53:06 +00:00
Florian Hahn 853c52c988
[ConstraintElimination] Require GEPs to be inbounds for decomposition.
During decomposition, we assume the no-wrap properties of inbound GEPs.
Make sure the decomposed GEP is actually inbounds.
2021-02-07 11:08:53 +00:00
Florian Hahn 703f6a6828
[ConstraintElimination] Support conditions from loop preheaders
This patch extends the condition collection logic to allow adding
conditions from pre-headers to loop headers, by allowing cases where the
target block dominates some of its predecessors.
2021-02-04 13:58:32 +00:00
Florian Hahn d8e90716df
[ConstraintElimination] Skip pointer casts.
We should be able to look through pointer casts that do not impact the
value.
2021-02-02 21:25:29 +00:00
Fangrui Song 51da12680f [ConstraintElimination] Fix -Wunused-function in -DLLVM_ENABLE_ASSERTIONS=off build 2021-02-02 10:23:14 -08:00
Florian Hahn 3e09bc2500
[ConstraintElimination] Add nicer way to dump constraints (NFC).
Use ConstraintSystem::dump(Names) to display the result of decomposing a
condition.
2021-02-02 16:36:45 +00:00
Florian Hahn 0b28d756af
[ConstraintElimination] Add support for EQ predicates.
A == B map to A >= B && A <= B
(https://alive2.llvm.org/ce/z/_dwxKn).

This extends the constraint construction to return a list of
constraints, which can be used to properly de-compose nested AND & OR.
2021-02-01 20:48:31 +00:00
Florian Hahn ce190e4144
[ConstraintElimination] Negate IR condition directly.
Instead of using ConstraintSystem::negate when adding new constraints,
flip the condition in IR.

The main advantage is that EQ predicates can be represented by 2
constraints, which makes negating based on the constraint tricky. The IR
condition can easily negated.
2021-02-01 17:21:40 +00:00
Florian Hahn 39486753d5
[ConstraintElimination] Verify CS and DFSInStack are in sync.(NFC)
After the main loop is done, we should have one constraint per item in
DFSInStack. Otherwise we added a constraint without a proper DFSInStack
item.
2021-01-30 18:27:04 +00:00
Kazu Hirata 9bcc0d1040 [CodeGen, Transforms] Use llvm::sort (NFC) 2021-01-14 20:30:31 -08:00
Juneyoung Lee bfedd5d2b6 [ConstraintElimination] Add support for select form of and/or
This patch adds support for select form of and/or.
Currently there is an ongoing effort for moving towards using `select a, b, false` instead of `and i1 a, b` and
`select a, true, b` instead of `or i1 a, b` as well.
D93065 has links to relevant changes.

Alive2 proof: (undef input was disabled due to timeout :( )
- and: https://alive2.llvm.org/ce/z/AgvFbQ
- or: https://alive2.llvm.org/ce/z/KjLJyb

Differential Revision: https://reviews.llvm.org/D93935
2020-12-30 21:27:36 +09:00
Florian Hahn 4ceecc820b [ConstraintElimination] Handle constraints with all zero var coeffs.
Constraints where all variable coefficients are 0 do not add any useful
information. When checking, we can check if they are always true/false.
2020-12-05 12:06:53 +00:00
Florian Hahn 2304528bb5 [ConstraintElimination] Make sure arguments of std:pow match.
This should fix a build failure on some systems, e.g. solaris11-sparcv9
http://lab.llvm.org:8014/#/builders/22
2020-12-02 22:23:26 +00:00
Florian Hahn 7a4f1d59b8 [ConstraintElimination] Decompose GEP %ptr, ZEXT(SHL()).
Add support to decompose a GEP with a ZEXT(SHL()) operand.
2020-12-01 14:23:21 +00:00
Florian Hahn efa9728a50 [ConstraintElimination] Decompose GEP %ptr, SHL().
Add support the decompose a GEP with an SHL operand.
2020-12-01 10:58:36 +00:00
Florian Hahn 7fa14a7c69
[ConstraintElimination] Decompose GEP with arbitrary offsets.
This patch decomposes `GEP %x, %offset` as  0 + 1 * %x + 1 * %off.
2020-11-19 22:49:21 +00:00
Florian Hahn 2fead1ac61
[ConstraintElimination] Decompose add nuw/sub nuw.
Make use of the more flexible constraint handling added in
a8a79c9069 to decompose add nuw/sub nuw.
2020-11-18 20:29:30 +00:00