84 Commits

Author SHA1 Message Date
Macsen Casaus
6fa13d79cf
[InstCombine] Add additional known bits info for self multiply (#151202)
Closes #151043 
https://alive2.llvm.org/ce/z/JyMSk8
2025-08-11 10:52:04 +02:00
Andrew Rogers
0563186a76
[llvm] properly guard dump methods in Support lib classes (#139938)
## Purpose
Add proper preprocessor guards for all `dump()` methods in the LLVM
support library. This change ensures these methods are not part of the
public ABI for release builds.

## Overview
* Annotates all `dump` methods in Support and ADT source with the
`LLVM_DUMP_METHOD` macro.
* Conditionally includes all `dump` method definitions in Support and
ADT source so they are only present on debug/assert builds and when
`LLVM_ENABLE_DUMP` is explicitly defined.

NOTE: For many of these `dump` methods, the implementation was already
properly guarded but the declaration in the header file was not.

## Background
This PR is a redo of #139804 with some changes to fix clang and unit
test build breaks.

This issue was raised in comments on #136629. I am addressing it as a
separate change since it is independent from the changes being made in
that PR.

According to [this
documentation](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L637),
`dump` methods should be annotated with `LLVM_DUMP_METHOD` and
conditionally included as follows:
```
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const;
#endif
```

## Validation
* Local release build succeeds.
* CI
2025-05-14 13:48:45 -07:00
Andrew Rogers
9a0e1c7988
Revert "[llvm] properly guard dump methods in Support lib classes" (#139927)
Reverts llvm/llvm-project#139804
2025-05-14 09:35:19 -07:00
Andrew Rogers
c9d05f3bcb
[llvm] properly guard dump methods in Support lib classes (#139804)
## Purpose
Add proper preprocessor guards for all `dump()` methods in the LLVM
support library. This change ensures these methods are not part of the
public ABI for release builds.

## Overview
* Annotates all `dump` methods in Support and ADT source with the
`LLVM_DUMP_METHOD` macro.
* Conditionally includes all `dump` method definitions in Support and
ADT source so they are only present on debug/assert builds and when
`LLVM_ENABLE_DUMP` is explicitly defined.

NOTE: For many of these `dump` methods, the implementation was already
properly guarded but the declaration in the header file was not.

## Background
This issue was raised in comments on #136629. I am addressing it as a
separate change since it is independent from the changes being made in
that PR.

According to [this
documentation](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L637),
`dump` methods should be annotated with `LLVM_DUMP_METHOD` and
conditionally included as follows:
```
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const;
#endif
```

## Validation
* Local release build succeeds.
* CI
2025-05-14 08:54:12 -07:00
goldsteinn
877cb9a2ed
[KnownBits] Make {s,u}{add,sub}_sat optimal (#113096)
Changes are:
    1) Make signed-overflow detection optimal
    2) For signed-overflow, try to rule out direction even if we can't
       totally rule out overflow.
    3) Intersect add/sub assuming no overflow with possible overflow
       clamping values as opposed to add/sub without the assumption.
2024-11-05 09:03:37 -06:00
Jay Foad
5cabf1505d
[KnownBits] Make avg{Ceil,Floor}S optimal (#110688)
Rewrite the signed functions in terms of the unsigned ones which are
already optimal.
2024-10-01 19:34:50 +01:00
Ramkumar Ramachandra
3fee3e83a8
KnownBits: refine srem for high-bits (#109121)
KnownBits::srem does not correctly set the leader zero-bits, omitting
the fact that LHS may be known-negative or known-non-negative. Fix this.

Alive2 proof: https://alive2.llvm.org/ce/z/Ugh-Dq
2024-09-27 12:00:50 +01:00
Simon Pilgrim
fa9301f35b [KnownBits] avgCompute - don't create on-the-fly Carry. NFC.
Use the internal computeForAddCarry directly since we know the exact values of the carry bit.
2024-06-13 10:17:15 +01:00
c8ef
b25b1db819
[KnownBits] Remove hasConflict() assertions (#94568)
Allow KnownBits to represent "always poison" values via conflict.

close: #94436
2024-06-07 17:01:22 +02:00
Nhat Nguyen
eab92cb7f3
[llvm] Add KnownBits implementations for avgFloor and avgCeil (#86445)
This PR is to address the issue #84640
2024-05-19 10:57:11 -04:00
Jay Foad
d8a26ca323
[KnownBits] Make abdu and abds optimal (#89081)
Fixes #84212
2024-04-18 16:27:02 +01:00
Simon Pilgrim
ffe41819e5
[Support] Add KnownBits::abds signed absolute difference and rename absdiff -> abdu (#84897)
When I created KnownBits::absdiff, I totally missed that we already have ISD::ABDS/ABDU nodes, and we use this term in other places/targets as well.

I've added the KnownBits::abds implementation and renamed KnownBits::absdiff to KnownBits::abdu.

Followup to #84791
2024-03-12 12:10:30 +00:00
Noah Goldstein
d81db0e5f5 [KnownBits] Implement knownbits lshr/ashr with exact flag
The exact flag basically allows us to set an upper bound on shift
amount when we have a known 1 in `LHS`.

Typically we deduce exact using knownbits (on non-exact incoming
shifts), so this is particularly impactful, but may be useful in some
circumstances.

Closes #84254
2024-03-11 15:51:07 -05:00
Noah Goldstein
a9d913ebcd [KnownBits] Add API support for exact in lshr/ashr; NFC 2024-03-11 15:51:06 -05:00
Noah Goldstein
17162b61c2 [KnownBits] Make nuw and nsw support in computeForAddSub optimal
Just some improvements that should hopefully strengthen analysis.

Closes #83580
2024-03-05 12:59:58 -06:00
Noah Goldstein
61c06775c9 [KnownBits] Add API for nuw flag in computeForAddSub; NFC 2024-03-05 12:59:58 -06:00
Simon Pilgrim
fcdf818a77
[KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values (#82354)
Equivalent to "umax(A, B) - umin(A, B)"

This is just an initial correctness implementation, hopefully we can make this optimal in the future.
2024-03-01 19:24:27 +00:00
Christian Kissig
730df5a437
[Support] Add KnownBits::computeForSubBorrow (#67788)
- [Support] Add KnownBits::computeForSubBorrow
- [CodeGen] Implement USUBC, USUBO_CARRY, and SSUBO_CARRY with
KnownBits::computeForSubBorrow
- [CodeGen] Compute unknown bits for Carry/Borrow for ADD/SUB
- [CodeGen] Compute known bits of Carry/Borrow for UADDO, SADDO, USUBO,
and SSUBO

Fixes #65893

---------

Co-authored-by: Shafik Yaghmour <shafik@users.noreply.github.com>
2023-10-18 13:48:47 +01:00
Noah Goldstein
85378b7663 [KnownBits] Factor out and improve the lowbit computation for {u,s}div
There are some new cases if the division is `exact`:
    1: If `TZ(LHS) == TZ(RHS)` then the result is always Odd
    2: If `TZ(LHS) > TZ(RHS)` then the `TZ(LHS)-TZ(RHS)` bits of the
       result are zero.
Proofs: https://alive2.llvm.org/ce/z/3rAZqF

As well, return zero in known poison cases to be consistent rather
than just working about the bits we are changing.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D150923
2023-06-06 15:14:10 -05:00
Noah Goldstein
809b1d834d [KnownBits] Return 0 for poison {s,u}div inputs
It seems consistent to always return zero for known poison rather than
varying the value. We do the same elsewhere.

Differential Revision: https://reviews.llvm.org/D150922
2023-06-06 15:14:10 -05:00
Noah Goldstein
8b2767f257 [KnownBits] Cleanup some misspelling / logic in {u,s}div
Chronically misspelled 'denominator' as 'denuminator' and a few other
cases.

On the logic side, no longer require `RHS` to be strictly positive in
`sdiv`. This in turn means we need to handle a possible zero `denom`
in the APInt division.

Differential Revision: https://reviews.llvm.org/D150921
2023-06-06 15:14:10 -05:00
Nikita Popov
dfb369399d [ValueTracking] Directly use KnownBits shift functions
Make ValueTracking directly call the KnownBits shift helpers, which
provides more precise results.

Unfortunately, ValueTracking has a special case where sometimes we
determine non-zero shift amounts using isKnownNonZero(). I have my
doubts about the usefulness of that special-case (it is only tested
in a single unit test), but I've reproduced the special-case via an
extra parameter to the KnownBits methods.

Differential Revision: https://reviews.llvm.org/D151816
2023-06-01 09:46:16 +02:00
Nikita Popov
103684b8e8 [KnownBits] Partially synchronize shift implementations (NFC)
And remove some bits of effectively dead code.
2023-05-26 14:16:14 +02:00
Nikita Popov
6f75c6681d [KnownBits] Add fast-path for shl with unknown shift amount (NFC)
We currently don't call into KnownBits::shl() from ValueTracking
if the shift amount is unknown. If we do try to do so, we get
significant compile-time regressions, because evaluating all 64
shift amounts if quite expensive, and mostly pointless in this case.
Add a fast-path for the case where the shift amount is the full
[0, BitWidth-1] range. This primarily requires a more accurate
estimate of the max shift amount, to avoid taking the fast-path in
too many cases.

Differential Revision: https://reviews.llvm.org/D151540
2023-05-26 13:57:33 +02:00
Jay Foad
2b1678cd06 [KnownBits] Simplify shl. NFCI.
Differential Revision: https://reviews.llvm.org/D151421
2023-05-25 16:05:32 +01:00
Nikita Popov
d2502eb091 [KnownBits] Add support for nuw/nsw on shifts
Implement precise nuw/nsw support in the KnownBits implementation,
replacing the rather crude handling in ValueTracking.

Differential Revision: https://reviews.llvm.org/D151208
2023-05-25 10:17:10 +02:00
Nikita Popov
725fcf40c3 [KnownBits] Reduce number of overflow checks for uadd/sub_sat (NFCI)
Only check for overflow on the min/max values, don't also check
for predicates in addition to that.
2023-05-24 11:16:58 +02:00
Nikita Popov
2d48a771fc [KnownBits] Use early return for unknown LHS for shifts (NFC)
Make it clear that the leading/trailing zeros handling is only
relevant for the unknown LHS case, which is a fast path to avoid
the full shift amount loop in cases where it would not produce
better results.
2023-05-24 11:02:16 +02:00
Nikita Popov
d4bfc144ea [KnownBits] Check for conflict-freedom in exhaustive tests
And make sure udiv() Exact does not produce conflicts.
2023-05-24 10:34:44 +02:00
Noah Goldstein
5f50b180c5 [KnownBits] Add implementations for saturating add/sub functions
These where previously missing. Even in the case where overflow is
indeterminate we can still deduce some of the low/high bits.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D150102
2023-05-23 13:52:40 -05:00
Noah Goldstein
4fd3401e76 [KnownBits] Improve implementation of KnownBits::abs
`abs` preserves the lowest set bit, so if we know the lowest set bit,
set it in the output.

As well, implement the case where the operand is known negative.

Reviewed By: foad, RKSimon

Differential Revision: https://reviews.llvm.org/D150100
2023-05-23 13:52:39 -05:00
Nikita Popov
f7d1baa414 [KnownBits] Return zero instead of unknown for always poison shifts
For always poison shifts, any KnownBits return value is valid.
Currently we return unknown, but returning zero is generally more
profitable. We had some code in ValueTracking that tried to do this,
but was actually dead code.

Differential Revision: https://reviews.llvm.org/D150648
2023-05-23 14:41:22 +02:00
Noah Goldstein
7d05ab99ed [KnownBits] Improve KnownBits::udiv
We can more precisely determine the upper bits doing `MaxNum /
MinDenum` as opposed to only using the MSB.

As well, if the `exact` flag is set, we can sometimes determine some
of the low-bits.

Differential Revision: https://reviews.llvm.org/D150094
2023-05-16 18:58:12 -05:00
Noah Goldstein
8c569c922b [KnownBits] Add implementation for KnownBits::sdiv
Can figure out some of the upper bits (similiar to `udiv`) if we know
the sign of the inputs.

As well, if we have the `exact` flag we can sometimes determine some
low-bits.

Differential Revision: https://reviews.llvm.org/D150093
2023-05-16 18:58:12 -05:00
Nikita Popov
0b81ff3ac5 [KnownBits] Handle shifts over wide types
Do not assert if the bit width is larger than 64 bits. This case
is currently hidden from the IR layer by other checks, but gets
exposed with future changes.
2023-05-16 11:26:39 +02:00
Jay Foad
d8229e2f14 [KnownBits] Define and use intersectWith and unionWith
Define intersectWith and unionWith as two complementary ways of
combining KnownBits. The names are chosen for consistency with
ConstantRange.

Deprecate commonBits as a synonym for intersectWith.

Differential Revision: https://reviews.llvm.org/D150443
2023-05-16 09:23:51 +01:00
Nikita Popov
9d73a8bdc6 [KnownBits] Make shl/lshr/ashr implementations optimal
The implementations for shifts were suboptimal in the case where
the max shift amount was >= bitwidth. In that case we should still
use the usual code clamped to BitWidth-1 rather than just giving up
entirely.

Additionally, there was an implementation bug where the known zero
bits for the individual shift amounts were not set in the shl/lshr
implementations. I think after these changes, we'll be able to drop
some of the code in ValueTracking which *also* evaluates all possible
shift amounts and has been papering over this issue.

For the "all poison" case I've opted to return an unknown value for
now. It would be better to return zero, but this has fairly
substantial test fallout, so I figured it's best to not mix it into
this change. (The "correct" return value would be a conflict, but
given that a lot of our APIs assert conflict-freedom, that's probably
not the best idea to actually return.)

Differential Revision: https://reviews.llvm.org/D150587
2023-05-16 09:44:26 +02:00
Nikita Popov
0f1fb626b3 [KnownBitsTest] Align with ConstantRange test infrastructure (NFC)
Align the way we perform exhaustive tests for KnownBits with what
we do for ConstantRange. Test each case separately by specifying
a function on KnownBits and one on APInts. Additionally, specify
a callback that determines which cases are supposed to be optimal,
rather than only correct. Unlike the ConstantRange case there is
a well-defined, unique notion of optimality for KnownBits.

If a failure occurs, print out the inputs, computed result and
exact result. Adjust the printing function to produce the output
in a format that is meaningful for KnownBits, i.e. print the
actual known bits, using ? to signify unknowns and ! to signify
conflicts.
2023-05-15 16:48:57 +02:00
Noah Goldstein
7770b0abfd [KnownBits] Improve KnownBits::rem(X, Y) in cases where we can deduce low-bits of output
The first `cttz(Y)` bits in `X` are translated 1-1 in the output.

Alive2 Links:
    https://alive2.llvm.org/ce/z/Qc47p7
    https://alive2.llvm.org/ce/z/19ut5H

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D149421
2023-05-07 19:11:53 -05:00
Kazu Hirata
f8f3db2756 Use APInt::count{l,r}_{zero,one} (NFC) 2023-02-19 22:04:47 -08:00
Jay Foad
6749d187c6 [KnownBits] Add blsi and blsmsk
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142519
2023-02-18 13:31:07 -06:00
Fangrui Song
b1df3a2c0b [Support] llvm::Optional => std::optional
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-16 08:49:10 +00:00
Kazu Hirata
aadaaface2 [llvm] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02 21:11:44 -08:00
Kazu Hirata
7a47ee51a1 [llvm] Don't use Optional::getValue (NFC) 2022-06-20 22:45:45 -07:00
Nikita Popov
a694546f7c [KnownBits] Add operator==
Checking whether two KnownBits are the same is somewhat common,
mainly in test code.

I don't think there is a lot of room for confusion with "determine
what the KnownBits for an icmp eq would be", as that has a
different result type (this is what the eq() method implements,
which returns Optional<bool>).

Differential Revision: https://reviews.llvm.org/D125692
2022-05-17 09:38:13 +02:00
Simon Pilgrim
94453952fc [KnownBits] Add support for X*X self-multiplication (update)
Rename the SelfMultiply argument to make it clearer that the argument must not be undef

Differential Revision: https://reviews.llvm.org/D108992
2022-02-06 19:40:08 +00:00
Sanjay Patel
892c731681 [Support] improve known bits analysis for leading zeros of multiply
Instead of summing leading zeros on the input operands, multiply the
max possible values of those inputs and count the leading zeros of
the result. This can give us an extra zero bit (typically in cases
where one of the operands is a known constant).

This allows folding away the remaining 'add' ops in the motivating
bug (modeled in the PhaseOrdering IR test):
https://github.com/llvm/llvm-project/issues/48399

Fixes #48399

Differential Revision: https://reviews.llvm.org/D115969
2021-12-20 09:10:50 -05:00
Sanjay Patel
e9179a6a02 [Support] improve known bits analysis for multiply by power-of-2 (1 set bit)
This can be viewed as recognizing that multiply-by-power-of-2 doesn't
have a carry into the top bit of an M-bit * N-bit number.

Enhancing canonicalization of mul -> select might also handle some of
these if we were ok with increasing instruction count with casts in
some cases.

This doesn't help https://llvm.org/PR49055 , but it's a simpler
pattern that we miss.
Note: "-sccp" already gets these examples using a constant
range analysis.

Differential Revision: https://reviews.llvm.org/D114962
2021-12-08 11:50:05 -05:00
Sanjay Patel
aea6b9dcee [Support] replace check with assert in known bits of mul calculation; NFC 2021-12-01 13:41:12 -05:00
Jay Foad
a9bceb2b05 [APInt] Stop using soft-deprecated constructors and methods in llvm. NFC.
Stop using APInt constructors and methods that were soft-deprecated in
D109483. This fixes all the uses I found in llvm, except for the APInt
unit tests which should still test the deprecated methods.

Differential Revision: https://reviews.llvm.org/D110807
2021-10-04 08:57:44 +01:00