1403 Commits

Author SHA1 Message Date
Kazu Hirata
f8f3db2756 Use APInt::count{l,r}_{zero,one} (NFC) 2023-02-19 22:04:47 -08:00
Noah Goldstein
3bd38f6639 [ValueTracking] Add cases for additional ops in isKnownNonZero
Add cases for the following ops:
    - 0-X            -- https://alive2.llvm.org/ce/z/6C75Li
    - bitreverse(X)  -- https://alive2.llvm.org/ce/z/SGG1q9
    - bswap(X)       -- https://alive2.llvm.org/ce/z/p7pzwh
    - ctpop(X)       -- https://alive2.llvm.org/ce/z/c5y3BC
    - abs(X)         -- https://alive2.llvm.org/ce/z/yxXGz_
                        https://alive2.llvm.org/ce/z/rSRg4K
    - uadd_sat(X, Y) -- https://alive2.llvm.org/ce/z/Zw-y4W
                        https://alive2.llvm.org/ce/z/2NRqRz
                        https://alive2.llvm.org/ce/z/M1OpF8

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142828
2023-02-18 13:45:15 -06:00
Noah Goldstein
9a8f517f57 [ValueTracking] Add KnownBits patterns xor(x, x - 1) and and(x, -x) for knowing upper bits to be zero
These two BMI pattern will clear the upper bits of result past the
first set bit. So if we know a single bit in `x` is set, we know that
`results[bitwidth - 1, log2(x) + 1] = 0`.

Alive2:
blsmsk: https://alive2.llvm.org/ce/z/a397BS
blsi: https://alive2.llvm.org/ce/z/tsbQhC

Differential Revision: https://reviews.llvm.org/D142271
2023-02-18 13:31:17 -06:00
Craig Topper
68c906811b [ValueTracking] Replace an always false condition with an assert. NFC
The one caller of this function already checked that V isn't a
Constant.

Alternatively, we could remove the check from the caller if reviewers
prefer.

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D143677
2023-02-09 15:19:28 -08:00
Craig Topper
2919ec041f [RISCV] Remove side effects from vsetvli intrinsics.
Delete the opt intrinsics since they are now identical.

I left the side effects due to user expectations about how these
interact with things like inline assembly or function calls. Or
that they wouldn't be hoisted. I think we should look at other
ways to address thoughs.

If I could, I'd rename them these somehow to distance them from
the vsetvli instruction. In some sense they only query the VL for
a particular SEW and LMUL. They don't guarantee a vsetvli
instruction will be emitted.

Fixes https://github.com/llvm/llvm-project/issues/59359

Reviewed By: rogfer01, kito-cheng

Differential Revision: https://reviews.llvm.org/D143220
2023-02-03 13:03:56 -08:00
Kazu Hirata
55e2cd1609 Use llvm::count{lr}_{zero,one} (NFC) 2023-01-28 12:41:20 -08:00
Craig Topper
4ea6d42b63 [RISCV] Teach computeKnownBits that vsetvli returns <= 65536.
Resolves a FIXME. We could do even better taking into account SEW/LMUL.
2023-01-27 16:39:54 -08:00
Craig Topper
1ce3afd977 [ValueTracking] Teach computeKnownBits about riscv.vsetvli.opt and riscv.vsetvlimax.opt intrinsics.
These are like the intrinsic without opt, but don't have side effects.

Add missing test cases for riscv.vsetvlimax.
2023-01-27 16:26:38 -08:00
Stefan Gränitz
3b387d1070 Lift EHPersonalities from Analysis to IR (NFC)
Computing EH-related information was only relevant for analysis passes so far. Lifting it to IR will allow the IR Verifier to calculate EH funclet coloring and validate funclet operand bundles in a follow-up step.

Reviewed By: rnk, compnerd

Differential Revision: https://reviews.llvm.org/D138122
2023-01-27 18:05:13 +01:00
Joshua Cao
f9599bbc7a [AssumptionCache] caches @llvm.experimental.guard's
As discussed in https://github.com/llvm/llvm-project/issues/59901

This change is not NFC. There is one SCEV and EarlyCSE test that have an
improved analysis/optimization case. Rest of the tests are not failing.

I've mostly only added cleanup to SCEV since that is where this issue
started. As a follow up, I believe there is more cleanup opportunity in
SCEV and other affected passes.

There could be cases where there are missed registerAssumption of
guards, but this case is not so bad because there will be no
miscompilation. AssumptionCacheTracker should take care of deleted
guards.

Differential Revision: https://reviews.llvm.org/D142330
2023-01-24 20:16:46 -08:00
Kazu Hirata
188ec33726 [llvm] Use llvm::bit_width (NFC) 2023-01-21 14:48:32 -08:00
Kazu Hirata
5638156a1c [llvm] Use llvm::bit_width (NFC) 2023-01-21 13:56:47 -08:00
Nikita Popov
bf23b4031e [ValueTracking] Take poison-generating metadata into account (PR59888)
In canCreateUndefOrPoison(), take not only poison-generating flags,
but also poison-generating metadata into account. The helpers are
written generically, but I believe the only case that can actually
matter is !range on calls -- !nonnull and !align are only valid on
loads, and those can create undef/poison anyway.

Unfortunately, this negatively impacts logical to bitwise and/or
conversion: For ctpop/ctlz/cttz we always attach !range metadata,
which will now block the transform, because it might introduce
poison. It would be possible to recover this regression by supporting
a ConsiderFlagsAndMetadata=false mode in impliesPoison() and clearing
flags/metadata on visited instructions.

Fixes https://github.com/llvm/llvm-project/issues/59888.

Differential Revision: https://reviews.llvm.org/D142115
2023-01-20 12:18:32 +01:00
Matt Arsenault
c663b8c429 ValueTracking: Teach CannotBeOrderedLessThanZero about rounding intrinsics
These should obviously preserve the sign although the variety of these
always confuses me.
2023-01-15 09:44:45 -05:00
Guillaume Chatelet
8fd5558b29 [NFC] Use TypeSize::geFixedValue() instead of TypeSize::getFixedSize()
This change is one of a series to implement the discussion from
https://reviews.llvm.org/D141134.
2023-01-11 16:49:38 +00:00
Guillaume Chatelet
48f5d77eee [NFC] Use TypeSize::getKnownMinValue() instead of TypeSize::getKnownMinSize()
This change is one of a series to implement the discussion from
https://reviews.llvm.org/D141134.
2023-01-11 16:36:39 +00:00
Noah Goldstein
cc845e9de8 [InstCombine] Handle assume(X & Pow2 != 0) in computeKnownBits()
If we know that X & Pow2 != 0, then the bit at that position is
known one.

Differential Revision: https://reviews.llvm.org/D140851
2023-01-11 10:21:15 +01:00
serge-sans-paille
38818b60c5
Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ part
Use deduction guides instead of helper functions.

The only non-automatic changes have been:

1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*))
2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase.
3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated.
4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that).

Per reviewers' comment, some useless makeArrayRef have been removed in the process.

This is a follow-up to https://reviews.llvm.org/D140896 that introduced
the deduction guides.

Differential Revision: https://reviews.llvm.org/D140955
2023-01-05 14:11:08 +01:00
Owen Anderson
ec40c8f6fe [ValueTracking] Improve ComputeNumSignBits to handle Trunc
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D140796
2023-01-03 15:26:21 -07:00
Nikita Popov
3f04553e5c [ValueTracking] Use SmallVector for non-undef/poison ops
The way these APIs are used, there isn't really a benefit to
deduplicating the ops as part of the API. The only place that
benefits from this is PoisonChecking, and for that particular
use the assertion emission was potentially non-deterministic.
We should populate a vector for deterministic order and then
deduplicate via a separate set.
2023-01-02 14:40:15 +01:00
Nikita Popov
e44b11d9b6 [ValueTracking] Treat branch on undef as UB as well
We were already treating branch on poison as UB, but branch on
undef is also UB. Move the checks into the correct function.

From LangRef for br:

> If ‘cond’ is poison or undef, this instruction has undefined behavior.

From LangRef for switch:

> If ‘value’ is poison or undef, this instruction has undefined behavior.

There is a minor regression in dont-distribute-phi.ll, apparently
we handle that pattern in logical but not bitwise form.
2023-01-02 12:34:23 +01:00
Nikita Popov
86195b8361 [ValueTracking] Remove branch-on-poison-as-ub flag (NFC)
This has been enabled by default without issue for a while now,
remove the flag.
2023-01-02 11:05:01 +01:00
Matt Arsenault
7e720b010a ValueTracking: Fix canCreateUndefOrPoison for saturating shifts
These need to consider the shift amount.
2022-12-30 11:28:28 -05:00
Matt Arsenault
de8e0a4397 ValueTracking: Teach canCreateUndefOrPoison about saturating intrinsics 2022-12-23 09:42:33 -05:00
Matt Arsenault
876f3d6c91 ValueTracking: Add test for isKnownNeverInfinity for fptrunc 2022-12-22 09:38:14 -05:00
Matt Arsenault
2c52c811ee ValueTracking: Document some difficult isKnownNeverInfinity cases
Add a comment and some negative tests. I'd like to have test coverage
and explicit handling of all the math operations for clarity.
2022-12-20 13:22:22 -05:00
Matt Arsenault
2bf17cc048 ValueTracking: Teach isKnownNeverInfinity about llvm.sin/llvm.cos 2022-12-20 13:17:03 -05:00
Matt Arsenault
9a21475651 ValueTracking: Teach isKnownNeverInfinity about sqrt 2022-12-20 13:03:07 -05:00
Matt Arsenault
41dd02e857 ValueTracking: Teach isKnownNeverInfinity about min/max functions 2022-12-20 12:52:59 -05:00
Matt Arsenault
4e37d00b9d ValueTracking: Teach isKnownNeverInfinity about rounding intrinsics 2022-12-20 12:45:07 -05:00
Florian Hahn
8a3efcd40b
[ValueTracking] Consider single poison operands in propgatesPoison.
This patch updates propgatesPoison to take a Use as argument and
propagatesPoison now returns true if the passed in operand causes the
user to yield poison if the operand is poison

This allows propagating poison if the condition of a select is poison.
This helps improve results for programUndefinedIfUndefOrPoison.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D111643
2022-12-19 11:47:51 +00:00
Kazu Hirata
6eb0b0a045 Don't include Optional.h
These files no longer use llvm::Optional.
2022-12-14 21:16:22 -08:00
Fangrui Song
d4b6fcb32e [Analysis] llvm::Optional => std::optional 2022-12-14 07:32:24 +00:00
Sanjay Patel
6e6fe27689 [ValueTracking] peek through extends in haveNoCommonBitsSet (2nd try)
The 1st try was not clean because a portion of the code diff
made it into the pre-commit patch to add tests. This should
be the same end result without the muddied code diff.

Original commit message:

In cases with matching extends, this allows changing an 'add'
into an 'or' and narrowing the 'or' which then simplifies to
a constant.

In cases with opposite extends, we just convert to an 'or'
currently, but that could be reduced too.

https://alive2.llvm.org/ce/z/fTHzdb
2022-12-13 16:57:45 -05:00
Sanjay Patel
41513bc7a2 Revert "[InstCombine] add tests for add-of-extends; NFC"
This reverts commit c8cba0bc4a8c9f4f3f10e17f601ed924dfb82bef.
An unintended code change snuck into this (was supposed to just add tests).
2022-12-13 16:12:09 -05:00
Sanjay Patel
3ed6143e81 Revert "[ValueTracking] peek through extends in haveNoCommonBitsSet"
This reverts commit 7520d187cf0dedcf5085f71bc1a5472c75cc8dbb.

The pre-commit patch that was supposed to NFC add tests for this
patch picked up some code that should not be here. I'll clean up
and re-commit.
2022-12-13 16:12:09 -05:00
Sanjay Patel
7520d187cf [ValueTracking] peek through extends in haveNoCommonBitsSet
In cases with matching extends, this allows changing an 'add'
into an 'or' and narrowing the 'or' which then simplifies to
a constant.

In cases with opposite extends, we just convert to an 'or'
currently, but that could be reduced too.

https://alive2.llvm.org/ce/z/fTHzdb
2022-12-13 15:53:20 -05:00
Sanjay Patel
c8cba0bc4a [InstCombine] add tests for add-of-extends; NFC 2022-12-13 15:53:20 -05:00
Matt Arsenault
28e3220a5c ValueTracking: Teach canCreateUndefOrPoison about FP ops
Probably could replace the switch by marking the intrinsic definitions
with NoUndef<RetIndex>.
2022-12-12 22:57:44 -05:00
Manuel Brito
45a892d012 Use poison instead of undef where its used as a placeholder [NFC]
Differential Revision: https://reviews.llvm.org/D139789
2022-12-11 17:18:00 +00:00
Kazu Hirata
405fc404bf [ADT] Don't including None.h (NFC)
These source files no longer use None, so they do not need to include
None.h.

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-06 20:14:51 -08:00
Matt Arsenault
7f4429c0e4 ValueTracking: Teach CannotBeOrderedLessThanZero about copysign 2022-12-06 09:01:39 -05:00
Matt Arsenault
51af4ddfc2 ValueTracking: Teach canCreateUndefOrPoison about more intrinsics
I tried to test the fallthrough to noundef callsite return attribute
case, but it seems that folds out as-is.
2022-12-05 10:04:13 -05:00
Matt Arsenault
dbca874faa ValueTracking: Teach CannotBeOrderedLessThanZero about trivial ops
Handle canonicalize and arithmetic.fence
2022-12-05 08:39:07 -05:00
Matt Arsenault
db0f258479 ValueTracking: Teach isKnownNeverNaN about arithmetic_fence 2022-12-05 08:39:07 -05:00
Matt Arsenault
dac496fb1f ValueTracking: Teach isKnownNeverInfinity about arithmetic.fence 2022-12-05 08:39:07 -05:00
Kazu Hirata
3c09ed006a [llvm] Use std::nullopt instead of None in comments (NFC)
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-04 17:12:44 -08:00
Kazu Hirata
19aff0f37d [Analysis] 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 19:43:04 -08:00
Krzysztof Parzyszek
26424c96c0 Attributes: convert Optional to std::optional 2022-12-02 08:15:45 -06:00
Kazu Hirata
5ea708375f [Analysis] Use std::optional in ValueTracking.cpp (NFC)
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-11-25 11:30:34 -08:00