Commit Graph

95 Commits

Author SHA1 Message Date
Florian Hahn 3fe6ddd999
[ConstraintElimination] Update Changed status in ssub simplification.
Update tryToSimplifyOverflowMath to indicate whether the function made
any changes to the IR.
2022-10-02 14:25:51 +01:00
Florian Hahn 586784a2e4
[ConstraintElimination] Simplify check lines in test added in 2812a141.
The CHECK lines in the test are too specific and cause mis-matches on
some platforms. Reduce them to make them less fragile.
2022-09-30 19:51:05 +01:00
Florian Hahn 2812a1413f
[ConstraintElimination] Add test showing bug in analysis invalidation. 2022-09-30 19:37:58 +01:00
Florian Hahn 04c711c78d
[ConstraintElimination] Make sure the variable is available before use.
This fixes a crash when trying to access an index for a value where we
don't have a known index.

Fixes #58009.
2022-09-30 18:09:01 +01:00
Zain Jaffal aac6629f6d
[ConstraintElimination] Add initial usub.with.overflow tests.
Optimizations can be used to eliminate unecessary overflow checks. This patch introduces some test cases where the checks can be safely removed

Reviewed By: fhahn, fcloutier

Differential Revision: https://reviews.llvm.org/D134038
2022-09-26 16:44:36 +01:00
Florian Hahn 7914e53e31
[ConstraintElimination] Fix crash when combining results.
f213128b29 didn't account for the possibility that the result of
decompose may be empty. Fix that by explicitly checking. Use a newly
introduced helper to also reduce some duplication.

Thanks @bjope for finding the issue!
2022-09-17 14:47:38 +01:00
Florian Hahn 7f3ff9d3c0
[ConstraintElimination] Track if variables are positive in constraint.
Keep track if variables are known positive during constraint
decomposition, aggregate the information when building the constraint
object and encode the extra information as constraints to be used during
reasoning.
2022-09-14 18:43:54 +01:00
Florian Hahn f213128b29
[ConstraintElimination] Further de-compose operands of add operations.
This simply extends the existing logic to look through adds and combine
the components as done in other places already.
2022-09-14 12:00:32 +01:00
Florian Hahn aba2085e52
[ConstraintElimination] Add tests where info from zext can be used. 2022-09-14 10:04:07 +01:00
Florian Hahn 62c1928437
[ConstraintElimination] Add tests for chained adds.
Add test coverage for reasoning about chains of adds.
2022-09-14 09:27:18 +01:00
Florian Hahn 92f87787b3
Recommit "[ConstraintElimination] Transfer info from ULT to signed system."
This reverts commit 94ed2caf70.

The issue with no-determinism with the test has been fixed in
d9526e8a52.
2022-06-24 09:27:14 +02:00
Florian Hahn 94ed2caf70
Revert "[ConstraintElimination] Transfer info from ULT to signed system."
This reverts commit 316e106f49.

This breaks a bot with expensive checks.
2022-06-23 17:27:33 +02:00
Florian Hahn 316e106f49
[ConstraintElimination] Transfer info from ULT to signed system.
If A u< B holds, then A s>= 0 && A s< B holds if B s>= 0.

https://alive2.llvm.org/ce/z/RrNxHh
2022-06-23 17:17:01 +02:00
Florian Hahn 9a33f3975e
[ConstraintElimination] Transfer info from SLT to unsigned system.
If A s< B holds, then A u< also holds, if A s>= 0.

https://alive2.llvm.org/ce/z/J4JZuN
2022-06-23 15:57:59 +02:00
Florian Hahn 24a98881cd
[ConstraintElimination] Transfer info from SGT to unsigned system.
If A >s B then A >=u 0, if B >=s -1.

https://alive2.llvm.org/ce/z/cncGKi
2022-06-23 11:04:51 +02:00
Florian Hahn 098b0b18a7
[ConstraintElimination] Transfer info from SGE to unsigned system.
This patch adds a new transferToOtherSystem helper that tries to
transfer information from signed predicates to the unsigned system and
vice versa.

The initial version adds A >=u B for A >=s B && B >=s 0

https://alive2.llvm.org/ce/z/8b6F9i
2022-06-22 15:27:59 +02:00
Florian Hahn c259a2b94f
[ConstraintElimination] Add tests for transferring info between systems. 2022-06-21 23:34:03 +02:00
Florian Hahn 782e912246
[ConstraintElimination] Support constraints with only const ops.
Remove the early exit if both constraints contain no variables. This
restriction is unnecessayr for correctness and removing it simplifies
handling of trivial constant conditions in follow-up changes.
2022-06-14 10:37:12 +01:00
Florian Hahn 68df5c5c13
[ConstraintElimination] Add tests with cmps with constant ops only.
Add extra test coverage for conditions with constant ops.
2022-06-13 22:57:54 +01:00
Florian Hahn 8e6d481f3b
[ConstraintElimination] Simplify ssub(A,B) if B s>=b && B s>=0.
A first patch to use the reasoning in ConstraintElimination to simplify
sub with overflow to a regular sub, if the operation is guaranteed to
not overflow.

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D125264
2022-05-13 13:19:41 +01:00
Florian Hahn 1911843c31
[ConstraintElimination] Add extra tests for different overflows.
Additional tests for D125264, inspired by @spatel.
2022-05-11 21:20:42 +01:00
Florian Hahn 301fe084bf
[ConstraintElimination] Add test where ssub result is not used.
Extra tests for D125264.
2022-05-11 16:10:25 +01:00
Florian Hahn 61bb2e4ea8
[ConstraintElimination] Add initial ssub.with.overflow tests. 2022-05-09 10:02:59 +01:00
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 6778e2f441
[ConstraintElimination] Add tests with signed predicates and ADDs. 2022-03-28 18:00:18 +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 8530259985
[ConstraintElimination] Add test where offset additions overflow. 2022-03-25 18:08:18 +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 2229674cb7
[ConstraintElimination] Add additional tests with uncond branches. 2022-03-16 09:46:04 +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 873e630d6c
[ConstraintElimination] Add tests inspired by PR54224. 2022-03-07 13:34:00 +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 12ffa9c2aa
[ConstraintElimination] Add test case for PR54217.
Adds test case for https://github.com/llvm/llvm-project/issues/54217.
2022-03-07 12:01:10 +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
Florian Hahn 4ad1ed3a2e
[ConstraintElimination] Add test from PR54228.
Test for https://github.com/llvm/llvm-project/issues/54228
2022-03-07 09:04:07 +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 d1e1a40527
[ConstraintElimination] Add test for #48253.
Test from https://github.com/llvm/llvm-project/issues/48253.
2022-02-11 17:07:13 +00:00
Florian Hahn bec1aa3069
[ConstraintElimination] Add test with trivially false condition in and. 2022-02-05 14:17:08 +00: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 e39bbe9a83
[ConstraintElimination] Add test cases from PR49819. 2022-02-03 11:09:10 +00:00
Florian Hahn 49d6e3eb33
[ConstraintElimination] Add tests with signed predicates and GEPs. 2022-02-02 15:56:24 +00:00
Florian Hahn cd79ca6136
[ConstraintElimination] Add sub nuw test with signed predicates.
Add missing test coverage for `sub nuw` combined with both unsigned and
signed predicates.
2022-01-28 11:20:57 +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 a405ecffde
[ConstraintElimination] Add additional GEP arithmetic tests. 2022-01-27 17:15:43 +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 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