Commit Graph

143 Commits

Author SHA1 Message Date
Florian Hahn ee605b0acc
[ConstraintElim] Use collectOffset result for chained gep support.
This slightly simplifies the code and addresses a correctness issue
where the index scaling for the precondition was not considered
properly.

Thanks to @nikic for pointing that out in D137840.
2022-12-01 17:01:07 +00:00
Florian Hahn 2de8f1f652
[ConstraintElim] Add chained GEP tests with multiple indices/diff types.
Extend test coverage for chained GEPs.
2022-12-01 16:51:40 +00:00
Florian Hahn 6a834d2f2b
[ConstraintElim] Use collectOffset instead of custom GEP idx iteration.
Use collectOffset to collect scaled indices and constant offset for GEP
instead of custom code. This simplifies the logic in decomposeGEP and
allows to handle all cases supported by the generic helper.
2022-11-28 16:54:58 +00:00
Florian Hahn b0fb71613b
[ConstraintElim] Add add nsw tests with negative constant. 2022-11-19 19:17:42 +00:00
Florian Hahn 50a5e42330
[ConstraintElim] Split up large gep-arithmetic.ll test.
This should help avoid getting a single test file too large with future
additions.
2022-11-19 16:53:39 +00:00
Florian Hahn dbe86a9016
[ConstraintElim] Rename test file to match contents.
The test file contains tests with chained/nested GEPs. Reflect that in
the name.
2022-11-19 16:15:49 +00:00
Nikita Popov 30982a595d [ConstraintElimination] Make decompose() infallible
decompose() currently returns a mix of {} and 0 + 1*V on failure.
This changes it to always return the 0 + 1*V form, thus making
decompose() infallible.

This makes the code marginally more powerful, e.g. we now fold
sub_decomp_i80 by treating the constant as a symbolic value.

Differential Revision: https://reviews.llvm.org/D137847
2022-11-14 10:42:04 +01:00
Florian Hahn 8086b0c8a8
[ConstraintElim] Drop bail out for scalable vectors after using getTrue
ConstantInt::getTrue/getFalse can materialize scalable vectors with all
lanes true/false.
2022-11-03 19:05:45 +00:00
Florian Hahn 74d8628cf7
[ConstraintElimination] Skip compares with scalable vector types.
Materializing scalable vectors with boolean values is not implemented
yet. Skip those cases for now and leave a TODO.
2022-11-02 23:57:14 +00:00
Florian Hahn b478d8b966
[ConstraintElimination] Generate true/false vectors for vector cmps.
This fixes crashes when vector compares can be simplified to true/false.
2022-11-02 18:13:35 +00:00
Bjorn Pettersson 44bb4099cd [ConstraintElimination] Do not crash on vector GEP in decomposeGEP
Commit 359bc5c541 caused
 Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"'
failures in decomposeGEP when the GEP pointer operand is a vector.

Fix is to use DataLayout::getIndexTypeSizeInBits when fetching the
index size, as it will use the scalar type in case of a ptr vector.

Differential Revision: https://reviews.llvm.org/D137185
2022-11-02 10:04:11 +01:00
Florian Hahn 27f6091bb0
[ConstraintElimination] Fix nested GEP check.
At the moment, the implementation requires that the outer GEP has a
single index, the inner GEP can have an arbitrary indices, because the
general `decompose` helper is used.
2022-11-01 14:43:42 +00:00
Florian Hahn d4210800ca
[ConstraintElimination] Add tests for nested GEPs with multiple indices. 2022-11-01 14:17:38 +00:00
Florian Hahn 0b574650d6
[ConstraintElim] Add additional GEP subtraction tests. 2022-10-21 18:58:15 +01:00
Florian Hahn 6db71b8f14
[ConstraintElim] Use helper to allow overflow for coefficients of GEPs
If the arithmetic for indices of inbounds GEPs overflows, the result is
poison. This means it is also OK for the coefficients to overflow. GEP
decomposition is limited to cases where the index size is <= 64 bit,
which can be represented by int64_t used for the coefficients in the
constraint system.
2022-10-17 20:30:43 +01:00
Florian Hahn aec0c1009f
[ConstraintElim] Replace custom GEP index handling by using existing code
Instead of duplicating the existing decomposition code for GEP indices
just use the existing code by calling the existing decompose function on
the index expression and multiply the result's coefficients by the scale of
the index.

This both reduces code duplication and generalizes the pattern we can
handle.
2022-10-16 21:53:11 +01:00
Florian Hahn a4635ec710
[ConstraintElim] Support `add nsw` for unsigned preds with positive ops.
If both operands of an `add nsw` are known positive, it can be treated
the same as `add nuw` and added to the unsigned system.

https://alive2.llvm.org/ce/z/6gprff
2022-10-16 20:25:14 +01:00
Florian Hahn 067b744dbb
[ConstraintElim] Add tests for add nsw with unsigned predicates. 2022-10-16 15:51:33 +01:00
Florian Hahn 7c1b80e35c
[ConstraintElim] Support unsigned decomposition of mul/shl nuw..const
Support decomposition for `mul/shl nuw` with constant operand for unsigned
queries. Those expressions should not wrap in the unsigned sense and can
be added directly to the unsigned system.
2022-10-15 21:28:08 +01:00
Florian Hahn f12684d36e
[ConstraintElim] Support signed decomposition of `add nsw`.
Add support decomposition for `add nsw` for signed queries.
`add nsw` won't wrap and can be directly added to the signed
system.
2022-10-15 18:34:03 +01:00
Florian Hahn fb3e2bef4c
[ConstraintElim] Add test cases for shl and mul. 2022-10-14 16:59:13 +01:00
Florian Hahn 572d5d374c
[ConstraintElim] Add support for GEPs with multiple indices.
Lift restriction on GEPs with a single index by iterating over all
indices and joining the {Coefficient, Variable} entries for all indices
together.
2022-10-13 21:08:33 +01:00
Florian Hahn 52fdbbd86d
[ConstraintElim] Add nested GEP test with scalable vectors. 2022-10-13 20:58:11 +01:00
Florian Hahn 359bc5c541
[ConstraintElim] Bail out for GEPs when index size > 64 bits.
Limit pointer decomposition to pointers with index sizes of at most 64
bits. int64_t is used for coefficients, so as long as the index size <=
64 bits we should be able to represent all pointer offsets.

Pointer decomposition is limited to inbounds GEPs, so if a index
computation would overflow the result is poison, so it doesn't matter
that the coefficient overflows.

This allows replacing MulOverflow with regular multiplications.
2022-10-13 10:19:30 +01:00
Florian Hahn e143e52c22
[ConstraintElimination] Add tests with 128 bit pointers. 2022-10-12 19:49:29 +01:00
Florian Hahn ebb0be9b68
[ConstraintElimination] Regenerate check lines for test.
Thanks @nikic for spotting that!
2022-10-10 20:23:25 +01:00
Florian Hahn 80e49f49e4
[ConstraintElimination] Bail out for GEPs with scalable vectors.
This fixes a crash with scalable vectors, thanks @nikic for spotting
this!
2022-10-10 16:01:20 +01:00
Florian Hahn fee8f561bd
[ConstraintElimination] Include index type scale.
The current decomposition for GEPs did not correctly handle cases where
GEPs access different source types. Adjust the constraints by including
the indexed type-size as coefficients.

Further generalization to allow GEPs with more than one index is a
needed general follow-up improvement.
2022-10-09 21:53:30 +01:00
Florian Hahn a40fdef734
[ConstraintElimination] Add tests for GEPs with different source types. 2022-10-09 21:51:47 +01:00
Florian Hahn 11a6e64ba7
[ConstraintElim] Move logic to get constraint for solving to helper.
Move common logic shared by callers of getConstraint that use the result
to query the constraint system to a new helper getConstraintForSolving.

This includes common legality checks (i.e. not an equality constraint,
no new variables) and the logic to query the unsigned system if possible
for signed predicates.
2022-10-09 10:44:36 +01:00
Florian Hahn 1ae405144d
[ConstraintElimination] Add tests for chained GEPs without inbounds. 2022-10-08 19:39:37 +01:00
Florian Hahn e0136a62cc
[ConstraintElimination] Support chained GEPs with constant offsets.
Handle the (gep (gep ....), C) case by incrementing the constant
coefficient of the inner GEP, if C is a constant.
2022-10-08 16:59:27 +01:00
Florian Hahn 9d31d1c214
[ConstraintElimination] Use logic from 3771310eed for queries only.
The logic added in 3771310eed was placed sub-optimally. Applying the
transform in ::getConstraint meant that it would also impact conditions
that are added to the system by the signed <-> unsigned transfer logic.

This meant we failed to add some signed facts to the signed system. To
make sure we still add as many useful facts to the signed/unsigned
systems, move the logic to the point where we query the system.
2022-10-08 11:03:45 +01:00
Florian Hahn d227029ce4
[ConstraintElimination] Add test for regression after 3771310eed. 2022-10-07 20:42:16 +01:00
Florian Hahn 3771310eed
[ConstraintElimination] Convert to unsigned Pred if possible.
Convert SLE/SLT predicates to unsigned equivalents if both operands are
known to be signed-positive.

https://alive2.llvm.org/ce/z/tBeiZr
2022-10-07 12:27:36 +01:00
Florian Hahn 1ad58989ee
[ConstraintElimination] Add test that crash in earlier var handling.
Add a test that caused a crash in the new variable handling code in
earlier versions.
2022-10-07 09:31:45 +01:00
Florian Hahn 5b70db8f00
[ConstraintElimination] Add tests where unsigned system can be queried.
Add tests with a mix of signed and unsigned predicates. In some cases,
the unsigned system can be queried for signed predicates to improve
results.
2022-10-06 22:11:51 +01:00
Florian Hahn c9fa457933
[ConstraintElimination] Remove fixme addressed in 8e3e96298f. 2022-10-06 18:24:50 +01:00
Florian Hahn c5e1ddb6fd
[ConstraintElimination] Update tests to use opaque pointers. 2022-10-06 18:07:25 +01:00
Florian Hahn a7ac0dd0cf
[ConstraintElimination] Generalize AND matching.
Extend more general matching used for chains of ORs to also support
chains of ANDs.
2022-10-06 17:17:38 +01:00
Florian Hahn 8e3e96298f
[ConstraintElimination] Order cmps for signed <-> unsigned transfer first.
Make sure conditions with constant operands come before conditions
without constant operands. This increases the effectiveness of the
current signed <-> unsigned fact transfer logic.
2022-10-06 15:56:25 +01:00
Florian Hahn a2f96a10cf
[ConstraintElimination] Add tests with logical ANDs. 2022-10-06 14:50:13 +01:00
Florian Hahn 349375d093
[ConstraintElimination] Generalize OR matching.
Extend OR handling to traverse chains of ORs.
2022-10-06 11:56:22 +01:00
Florian Hahn e09b0589a9
[ConstraintElimination] Extend test coverage for AND chains. 2022-10-06 09:11:08 +01:00
Florian Hahn cceb8c068d
[ConstraintElimination] Extend test coverage for OR chains. 2022-10-06 08:39:11 +01:00
Florian Hahn c1bfa41428
[ConstraintElimination] Add test for debug printing. 2022-10-03 20:56:39 +01:00
Florian Hahn 22fe40068c
[ConstraintElimination] Add tests for chained GEPs with const offsets.
Add extra tests for chained GEPs where the second GEP has a constant
offset. Inspired by missed optimizations from #51358.
2022-10-03 15:55:36 +01:00
Max Kazantsev a34b5212ec [Test] Add test showing that Constraint Elimination can deal with case where SCEV fails 2022-10-03 16:41:22 +07:00
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