489 Commits

Author SHA1 Message Date
Matt Arsenault
17a9170a7e
InstCombine: Fix wrong insert point for sqrt -> copysign simplify (#180838) 2026-02-11 09:15:11 +01:00
Matt Arsenault
bd4fe78c84
InstCombine: Fix wrong insert point for various fmul->copysign simplifies (#180840) 2026-02-11 09:14:55 +01:00
Matt Arsenault
8503cb6ad5
InstCombine: Fix wrong insert point for fdiv->copysign simplify (#180839) 2026-02-11 09:14:39 +01:00
Matt Arsenault
1f0577836c
InstCombine: Fix insert point for rounding intrinsic -> copysign (#180837)
This would use the wrong insert point if reached in a recursive
call.
2026-02-11 09:14:12 +01:00
Matt Arsenault
7f9965c73d
InstCombine: Fix broken insert point for fdiv replacement (#180830)
SimplifyDemandedFPClass isn't properly adjusting the IRBuilder
insert point, so this could insert at the wrong point if the
simplification happens in one of the recursive calls. There are a few
more of these to fix.
2026-02-10 21:15:58 +00:00
Matt Arsenault
302ff8fd00
InstCombine: Use SimplifyDemandedFPClass on fmul (#177490)
Start trying to use SimplifyDemandedFPClass on instructions, starting
with fmul. This subsumes the old transform on multiply of 0. The
main change is the introduction of nnan/ninf. I do not think anywhere
was systematically trying to introduce fast math flags before, though
a few odd transforms would set them.

Previously we only called SimplifyDemandedFPClass on function returns
with nofpclass annotations. Start following the pattern of
SimplifyDemandedBits, where this will be called from relevant root
instructions.

I was wondering if this should go into InstCombineAggressive, but that
apparently does not make use of InstCombineInternal's worklist.
2026-02-10 09:49:31 +00:00
hanbeom
8d2078332c
[InstCombine] Shrink added constant using LHS known zeros (#174380)
Previously, `SimplifyDemandedUseBits` for `add` instructions only
used known zeros from the RHS to simplify the LHS. It failed to
handle the symmetric case where the LHS has known zeros and the
result does not demand the low bits.

This patch implements this missing optimization, allowing the RHS
constant to be shrunk when the LHS low bits are known zero and unused.

Proof: https://alive2.llvm.org/ce/z/6v9iFY
Fixed: https://github.com/llvm/llvm-project/issues/135411
2026-02-07 20:41:58 +09:00
Matt Arsenault
294e43c72d
InstCombine: Handle minnum/maxnum in SimplifyDemandedFPClass (#179299) 2026-02-03 18:31:34 +01:00
Matt Arsenault
91c4decc01
InstCombine: Fix SimplifyDemandedFPClass bug with known-snan sources (#179244)
If the result can be a qnan, the source can be a signaling nan.
2026-02-02 17:57:21 +00:00
Matt Arsenault
12731bd6ed
InstCombine: Stop using nsz in multi-use min/max fold (#176579)
In SimplifyDemandedFPClass, stop using nsz when there's a
mismatch in the sign of 0 for the various min and maxes.

Alive2 doesn't like it: https://alive2.llvm.org/ce/z/ZyhSGA,
presumably because of the possible mismatch between the stored
value and the propagated. Maybe it would be OK if nsz is on all
the uses.
2026-02-02 18:48:12 +01:00
Matt Arsenault
e62182b83b
InstCombine: Handle multiple use copysign (#176917)
Handle multiple use copysign in SimplifyDemandedFPClass
2026-01-30 17:21:07 +01:00
Matt Arsenault
6a2d74d9ff
InstCombine: Handle nsz in copysign SimplifyDemandedFPClass (#176916)
If the only sign bit difference is for 0, fold through the source.
2026-01-30 14:59:03 +00:00
Matt Arsenault
7d9f72026a
InstCombine: Improve single-use fneg(fabs(x)) SimplifyDemandedFPClass handling (#176360)
Match the multi-use case's logic for understanding no-nan/no-inf
context.
Also only apply the nsz handling in the single use case. alive2 seems to
treat nsz as nondeterministic for each use.
2026-01-30 14:06:29 +00:00
Matt Arsenault
ff79d7d1bf
InstCombine: Apply demanded mask at recursion limit in SimplifyDemandedFPClass (#178157)
This fixes missed flag inference in some cases, due to not inferring
no-nan result implies no-nan source. Also start treating explicit
nofpclass
attributes as a leaf value, like a constant or argument.
2026-01-27 18:19:55 +01:00
Matt Arsenault
fe3f7cd1ca
InstCombine: Stop preserving undef in SimplifyDemandedFPClass (#178163)
If we know there are no valid values, fold to poison. Previously this
would leave values that started as undef alone.
2026-01-27 11:35:03 +00:00
Matt Arsenault
a80d4329ce
InstCombine: Apply parameter nofpclass in SimplifyDemandedFPClass (#176104)
Apply the use operand's nofpclass to the demanded mask.
2026-01-25 17:00:13 +01:00
Matt Arsenault
db4405e9cd
InstCombine: Infer nnan/ninf on rounding intrinsics (#177770) 2026-01-25 15:48:17 +01:00
Matt Arsenault
2f94635bae
InstCombine: Infer nnan and ninf on fptrunc (#177769)
Teach SimplifyDemandedFPClass to do this, although this is
not yet applied directly to the cast.
2026-01-25 15:39:20 +01:00
Matt Arsenault
431dea9f33
InstCombine: Infer nnan and ninf on fpext (#177768)
Teach SimplifyDemandedFPClass to do this, although this is
not yet applied directly to the cast.
2026-01-25 15:31:59 +01:00
Matt Arsenault
d77ced0692
InstCombine: Infer nnan/ninf on canonicalize (#177771) 2026-01-25 15:27:35 +01:00
Matt Arsenault
60b1e95137
InstCombine: Infer nnan and ninf flags on log intrinsics (#177767)
Use the new common utility function to try fold to constant
or introduce flags.
2026-01-25 15:22:01 +01:00
Matt Arsenault
e9aae6a994
InstCombine: Infer nnan and ninf flags on exp intrinsics (#177766)
Use the new common utility function to try fold to constant
or introduce flags.
2026-01-25 15:20:39 +01:00
Matt Arsenault
0263baa0b5
InstCombine: Use SimplifyDemandedFPClass epilog helper function for fma (#176017)
NFC refactor to use shared code for fold to constant or set fast math
flags.
2026-01-24 15:19:47 +01:00
Matt Arsenault
0c793480f5
InstCombine: Cleanup redundant result-not-nan check for canonicalize (#177765)
Missed case from 53eb230e476f69c5c879871a0d328d283a6ce172
2026-01-24 14:11:02 +00:00
Matt Arsenault
3ee9aae6f9
InstCombine: Infer fast math flags for sqrt (#176003) 2026-01-24 13:41:37 +00:00
Aiden Grossman
08d96dd619
[InstCombine] Mark select in fdiv transforms with unknown profile (#177759)
As part of the profcheck effort we are trying to explicitly annotate
select instructions where we cannot reasonably synthesize profile
information as having an unknown profile. This does that for the case
introduced in 0993d69bc35cfdd4f3a904a603701e66906e8987.
2026-01-24 13:34:22 +00:00
Matt Arsenault
53eb230e47
InstCombine: Clean up SimplifyDemandedFPClass use context application (#176886)
Clean up some now redundant propagation of known-result to known-source
cases. Also move the application of the demanded mask to individual
cases, since the intermediate results are often used.
2026-01-24 14:05:45 +01:00
Matt Arsenault
14a6f7b2b8
InstCombine: Implement SimplifyDemandedFPClass for fma (#175616)
This can't do much filtering on the sources, except for nans.
We can also attempt to introduce ninf/nnan.
2026-01-24 12:19:30 +00:00
Matt Arsenault
0993d69bc3
InstCombine: Handle fdiv in SimplifyDemandedFPClass (#175946) 2026-01-24 11:37:04 +00:00
Matt Arsenault
35efc168dd
InstCombine: Fold known-qnan results to a literal nan (#176123)
Previously we only considered fcNan to fold to qnan for canonicalizing
results, ignoring the simpler case where we know the nan is already
quiet.
2026-01-24 10:41:29 +00:00
Matt Arsenault
403502f478
InstCombine: Implement SimplifyDemandedFPClass for frexp (#176122) 2026-01-24 08:11:50 +01:00
Matt Arsenault
c991fdd4bc
InstCombine: Handle fsub in SimplifyDemandedFPClass (#175852)
alive2 fails on some of the tests, but this is due to existing
folds in instsimplify and
https://github.com/AliveToolkit/alive2/issues/1273
2026-01-23 23:59:02 +01:00
Matt Arsenault
6000703ba8
InstCombine: Handle fptrunc in SimplifyDemandedFPClass (#175421)
Also handle llvm.fptrunc.round since it's the same.
2026-01-23 23:40:59 +01:00
Matt Arsenault
5dea8aa657
InstCombine: Remove SimplifyQuery shadowing in SimplifyDemandedFPClass (#177173)
SimplifyQuery is already a member with the same name.
2026-01-21 14:39:22 +00:00
Matt Arsenault
48c0e9995a
InstCombine: Filter reported classes from SimplifyDemandedFPClass (#176885)
When reporting the known class result, apply the demanded mask to
filter out rejected cases. This can simplify known-source checks
further up the call stack. There are a few improved test diffs. This
does not yet try to clean up now redundant result checks.

Do an initial brute-force scope_exit to ensure these are cleared.
Later we can do a better job by pushing this into the individual
instruction cases.
2026-01-20 17:13:09 +01:00
Matt Arsenault
5239c5c168
InstCombine: Fix duplicated code in SimplifyDemandedFPClass fabs handling (#176442)
2b03d68398819fe3608c680d6c25aa9d5a043c03 factored this into a function
and used from the new place, but forgot to delete the old code from
the original location.
2026-01-16 18:33:13 +00:00
Matt Arsenault
2b03d68398
InstCombine: Improve single use fabs SimplifyDemandedFPClass handling (#176359)
SimplifyDemandedFPClass's handling of fabs recently became smarter in
the multiple use case than single. Unify these so the single use case
is equally as smart. This includes propagating ninf / nnan context into
the instruction, and accounting for nsz if the only bit difference is
for zero.
2026-01-16 16:15:38 +00:00
Matt Arsenault
135744c0d7
InstCombine: Consider nsz when simplifying fabs/fneg uses (#176156)
Later this trick should also be applied in the single use
case.
2026-01-16 13:47:58 +00:00
Matt Arsenault
82780c08a3
InstCombine: Handle multiple use fneg(fabs(x)) in SimplifyDemandedFPClass (#176149)
This ends up being smarter than the single use case, so these should
be merged at some point.
2026-01-16 13:07:39 +00:00
Matt Arsenault
b29ee6ed4b
InstCombine: Fix SimplifyDemandedFPClass for fadd with known-inf source
(#176204)

Ensure the result cannot be nan.

Split out from https://github.com/llvm/llvm-project/pull/175852
2026-01-15 17:54:53 +00:00
Matt Arsenault
282a065c5b
InstCombine: Handle multiple uses fabs in SimplifyDemandedFPClass (#176035) 2026-01-15 16:21:10 +01:00
Matt Arsenault
0f54961189
InstCombine: Introduce nsz flag on minimum/maximum in SimplifyDemandedFPClass (#173898)
Alive isn't particularly happy with this in the case where
one of the inputs could be zero, but I think
it's wrong: https://alive2.llvm.org/ce/z/dF7V6k

nsz shouldn't permit introducing a -0 result where
there wasn't one in the input here.
2026-01-15 09:23:27 +00:00
Matt Arsenault
fb67ef1a8b
InstCombine: Handle multiple uses for min/max in SimplifyDemandedFPClass (#175974) 2026-01-14 18:31:16 +00:00
Matt Arsenault
196433eb27
InstCombine: Improve SimplifyDemandedFPClass min/max handling (#175382)
Refine handling of minimum/maximum and minimumnum/maximumnum. The
previous folds to input were based on sign bit checks. This was too
conservative with 0s. This can now consider -0 as less than or equal
to +0 as appropriate, account for nsz. It additionally can handle
cases like one half is known positive normal and the other subnormal.
2026-01-14 18:01:14 +00:00
Matt Arsenault
4b223d40d6
InstCombine: Teach SimplifyDemandedFPClass to infer nnan and ninf on fmul (#175854) 2026-01-14 16:39:32 +01:00
Ramkumar Ramachandra
d69335bac9
[LLVM] Clean up code using [not_]equal_to (NFC) (#175824)
Use llvm::[not_]equal_to landed in d2a521750 ([ADT] Introduce
bind_{front,back}, [not_]equal_to, #175056) across LLVM for cleaner
code.
2026-01-13 21:19:39 +00:00
Matt Arsenault
4a9a13cb8c
InstCombine: Handle fadd in SimplifyDemandedFPClass (#174853)
Note some of the tests currently fail with alive, but not
due to this patch. Namely, when performing the fadd x, 0 -> x
simplification in functions with non-IEEE denormal handling.
The existing instsimplify ignores the denormals-are-zero hazard by
checking cannotBeNegativeZero instead of isKnownNeverLogicalZero.

Also note the self handling doesn't really do anything yet, other
than propagate consistent known-fpclass information until there is
multiple use support.

This also leaves behind the original ValueTracking support, without
switching to the new KnownFPClass:fadd utility. This will be easier
to clean up after the subsequent fsub support patch.
2026-01-13 17:28:35 +01:00
Matt Arsenault
acfc31a4a7
InstCombine: Handle rounding intrinsics in SimplifyDemandedFPClass (#174842) 2026-01-12 18:51:44 +00:00
Matt Arsenault
2cfa9b258e
InstCombine: SimplifyDemandedFPClass multiple use support for select (#175548) 2026-01-12 17:59:41 +01:00
Matt Arsenault
99ab1dd145
InstCombine: Implement SimplifyDemandedFPClass for sqrt (#173883) 2026-01-11 11:42:41 +00:00