1644 Commits

Author SHA1 Message Date
David Majnemer
0a7eabcc56 Reapply "[APFloat] Fix getExactInverse for DoubleAPFloat"
The previous implementation of getExactInverse used the following check
to identify powers of two:

  // Check that the number is a power of two by making sure that only the
  // integer bit is set in the significand.
  if (significandLSB() != semantics->precision - 1)
    return false;

This condition verifies that the only set bit in the significand is the
integer bit, which is correct for normal numbers. However, this logic is
not correct for subnormal values.

APFloat represents subnormal numbers by shifting the significand right
while holding the exponent at its minimum value. For a power of two in
the subnormal range, its single set bit will therefore be at a position
lower than precision - 1. The original check would consequently fail,
causing the function to determine that these numbers do not have an
exact multiplicative inverse.

The new logic calculated this correctly but it seems that
test/CodeGen/Thumb2/mve-vcvt-fixed-to-float.ll expected the old
behavior.

Seeing as how getExactInverse does not have tests or documentation, we
conservatively maintain (and document) this behavior.

This reverts commit 47e62e846beb267aad50eb9195dfd855e160483e.
2025-08-20 14:02:36 -07:00
Jakub Kuderski
1633e0ba8b
[ADT] Add from_range constructor for (Small)DenseMap (#153515)
This follows how we support range construction for (Small)DenseSet.
2025-08-14 08:53:52 -04:00
Aiden Grossman
47e62e846b Revert "[APFloat] Fix getExactInverse for DoubleAPFloat"
This reverts commit f4941319cba19d7691baa6ec783c84be4d847637.

This broke llvm/test/CodeGen/Thumb2/mve-vcvt-fixed-to-float.ll which
took out a ton of buildbots and also broke premerge.
2025-08-14 04:39:50 +00:00
Pedro Lobo
08eff57444
[ADT] Add signed and unsigned mulExtended to APInt (#153399)
Adds `mulsExtended` and `muluExtended` methods to `APInt`, as suggested
in #153293.
These are based on the `MULDQ` and `MULUDQ` x86 intrinsics.
2025-08-13 22:37:33 +01:00
David Majnemer
f4941319cb [APFloat] Fix getExactInverse for DoubleAPFloat
Some background: getExactInverse()'s callers expect that
the result is not subnormal.

DoubleAPFloat implemented getExactInverse() by going through
semPPCDoubleDoubleLegacy.

This means that numbers like 0x1p1022 which would have a normal inverse
in semPPCDoubleDouble would not in semPPCDoubleDoubleLegacy.

This commit refactors the logic into a single method on APFloat which
uses getExactLog2Abs() and scalbn() to calculate the inverse without
having to compute a reciprocal and test if it is inexact.
This approach works for both IEEEFloat and DoubleAPFloat.
2025-08-13 11:39:53 -07:00
David Majnemer
acef1db3b2 [APFloat] Remove some overly optimistic assertions
An earlier draft of DoubleAPFloat::convertToSignExtendedInteger had
arranged for overflow to be handled in a different way.  However, these
assertions are now possible if Hi+Lo are out of range and Lo != 0.

A test has been added to defend against a regression.
2025-08-12 18:32:58 -07:00
David Majnemer
f6d143fd1f [APFloat] Properly implement frexp(DoubleAPFloat)
The prior implementation did not consider that the Lo component may
underflow when it undergoes scaling.  This means that we need to
carefully handle things like binade crossings or how to handle
roundTowardZero when Hi and Lo have different signs.

Particularly annoying is roundTiesToAway when Hi and Lo have different
signs.  It basically requires us to implement roundTiesTowardZero.
2025-08-12 17:03:27 -07:00
David Majnemer
e722ef4956 Reapply "[APFloat] Properly implement DoubleAPFloat::convertToSignExtendedInteger"
This reverts commit 8b44945a9231d4d7be0858a1c5d9c13d397bc512.

The compilation failure under !NDEBUG has been fixed.
2025-08-12 17:01:49 -07:00
Kazu Hirata
f90ded5b94
[ADT] Reduce memory allocation in SmallPtrSet::reserve() (#153126)
Previously, reserve() allocated double the required number of buckets.
For example, for NumEntries in the range [49, 96], it would reserve
256 buckets when only 128 are needed to maintain the load factor.

This patch removes "+ 1" in the NewSize calculation.
2025-08-11 22:51:32 -07:00
Kazu Hirata
8b44945a92 Revert "[APFloat] Properly implement DoubleAPFloat::convertToSignExtendedInteger"
This reverts commit 052c38be824d9dabb1e8fb64c1c7c3908d786e83.

I'm getting:

llvm/lib/Support/APFloat.cpp:5627:29: error:
use of undeclared identifier 'Parts'
 5627 |     assert(DstPartsCount <= Parts.size() && "Integer too big");
      |                             ^
1 error generated.
2025-08-10 00:36:29 -07:00
David Majnemer
052c38be82 [APFloat] Properly implement DoubleAPFloat::convertToSignExtendedInteger
Use DoubleAPFloat::roundToIntegral to get a pair of APFloat values which
hold integral values.  Then we sum the pair, taking care to make sure
that we handle edge cases like (hi=2^128, lo=-1) and ensuring that they
fit in an unsigned i128.
2025-08-09 23:23:01 -07:00
Ilia Kuklin
2ff44d7d65
[ADT] Make getAutoSenseRadix in StringRef global (#152503)
Needed in #152308
2025-08-07 22:48:17 +05:00
David Majnemer
0a23b22d1d [APFloat] Properly implement DoubleAPFloat::roundToIntegral
The previous implementation did not correctly handle double-doubles like
0x1p100 + 0x1p1 as the low order component would need more than a
106-bit significand to represent.
2025-08-06 10:42:07 -07:00
David Majnemer
9fdb5e1fef [APFloat] Properly implement next for DoubleAPFloat
Rather than converting to the legacy 106-bit format, perform next() on the
low APFloat. Of course, we need to renormalize the two APFloats if
either of the two constraints are violated:
1. abs(low) <= ulp(high)/2
2. high = rtne(high + low)

Should renormalization be needed, it will increment the high component
and set low to the smallest value which obeys these rules.
2025-08-01 12:34:33 -07:00
Krzysztof Parzyszek
6acc6991f8
[STLForwardCompat] Improve category handling in transformOptional (#149539)
The old version would prefer the "const &" overload over the "&&" one
unless the former was not allowed in the given situation. In particular,
if the function passed was "[](auto &&)" the argument would be "const &"
even if the value passed to transformOptional was an rvalue reference.

This version improves the handling of expression categories, and the
lambda argument category will reflect the argument category in the above
scenario.
2025-07-18 13:34:15 -05:00
Kazu Hirata
682d6c4942 [ADT] Fix a warning
This patch fixes:

  llvm/unittests/ADT/DenseMapTest.cpp:94:25: error: unused function
  'getTestValue' [-Werror,-Wunused-function]
2025-07-10 12:26:42 -07:00
Elijah Kin
49b87cd779
DenseMapInfo: support std::optional<T> (#147851) 2025-07-10 10:46:12 -07:00
Jay Foad
37f87194ed
[ADT] Implement ArrayRef::operator< and other comparisons (#147277)
Order ArrayRefs using std::lexicographical_compare, just like
std::vector and SmallVector.
2025-07-08 09:23:30 +01:00
Longsheng Mou
65537539e1
[llvm][ADT] Add wrappers to std::fill (#146681)
This PR adds `llvm::fill` that accepts a range instead of begin/end
iterator.
2025-07-03 14:32:47 +08:00
Rahul Joshi
2725765432
[LLVM][ADT] Add consume_front and consume_back to ArrayRef (#146741)
Add `consume_front` that returns the first element and drops it from the
current ArrayRef, and `consume_back` that returns the last element and
drops it from the current ArrayRef.
2025-07-02 13:32:27 -07:00
Zakk Chen
3cc200f46b
DynamicAPInt: Support APInt constructor. (#146301)
This PR introduces a constructor for `DynamicAPInt` that takes an
`APInt` object as input to simplifies the creation of a large numbers.
2025-07-01 15:44:12 +08:00
Krzysztof Parzyszek
4c7d3e9315
[STLForwardCompat] Implement llvm::type_identity (#146390)
A basic implementation until we get it in `std` in C++20.
2025-06-30 15:05:49 -05:00
Benjamin Kramer
e8f85cf51f [ArrayRef] Bring MutableArrayRef's constructor in line with ArrayRef
This time when the argument has a data member returning a mutable
pointer.
2025-06-26 11:12:05 +02:00
Benjamin Kramer
0556a2aa18
[ArrayRef] Provide constructors from any type with a data() member (#145735)
The assumption here is that if data() returns a pointer and size()
exists it's something representing contiguous storage.

Modeled after the behavior of C++20 std::span and enables two-way
conversion between std::span (or really any span-like type) and
ArrayRef. Add a unit test that verifies this if std::span is around.

This also means we can get rid of specific conversions for std::vector
and SmallVector.
2025-06-25 19:33:34 +02:00
Kazu Hirata
3b672e1d7b
[llvm] Use "= delete" to delete constructors (NFC) (#144938)
None of the constructors touched in this patch has a corresponding
definition.  This patch explicitly deletes them with "= delete" while
moving them to the public section of respective classes.  Note that "=
delete" itself serves as documentation.

Identified with modernize-use-equals-delete.
2025-06-19 13:17:39 -07:00
Longsheng Mou
52360d195b
[NFC] Use llvm::includes instead of std::includes (#143542)
This PR follows up #143297.
2025-06-12 09:27:27 +08:00
Longsheng Mou
e30cba5464
[llvm][ADT] Add wrappers to std::includes (#143297)
Add `llvm::includes` that accepts a range instead of start/end iterator.
2025-06-10 11:15:22 +08:00
Bushev Dmitry
4f392f9d0d
[ADT][bugfix] Fixed extra leading zero in uhextostr (#141097)
fixed bug: if fixed-width mode uhextostr() is used with value zero, it
prints extra '0' character.
2025-05-30 14:58:30 +03:00
Ramkumar Ramachandra
4bf67cdf02
[DenseMap] Fix constness issues with lookup_or (#139247)
Also demonstrate its use in ScalarEvolution.
2025-05-28 20:32:31 +02:00
Simon Pilgrim
23f0fbf8ff
[APInt] APInt::clearBitsSlowCase - fix cut+paste typo when merging lo/himasks (#141108)
Fixes #141098
2025-05-22 21:52:59 +01:00
Liam Semeria
d067014f13
[APInt] Added APInt::clearBits() method (#137098)
Added APInt::clearBits(unsigned loBit, unsigned hiBit) that clears bits within a certain range.

Fixes #136550

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-05-19 12:41:04 +01:00
Krzysztof Parzyszek
41fcd7e78b
[ADT] Add operator! to BitmaskEnum (#139958)
Add a logical (boolean) "not" operator.
2025-05-14 19:36:42 -05:00
Ramkumar Ramachandra
20169cb4ac
[DenseMap] Introduce emplace_or_assign (#138886)
Introduce emplace_or_assign, a variant of insert_or_assign that has
slightly better characteristics.
2025-05-08 11:25:22 +01:00
Ramkumar Ramachandra
358ebddeb8
[DenseMap] Introduce lookup_or (#138887)
Introduce lookup_or, a variant of lookup, for non-default-constructible
values.
2025-05-08 11:22:10 +01:00
Ramkumar Ramachandra
8602a655a8
[DenseMap] Introduce keys, values iterators (#138848) 2025-05-07 21:55:44 +01:00
Mingming Liu
ad5b3e01fb
[ADT]Add helper function to return a ArrayRef of MapVector's underlying vector (#138726)
SetVector currently has a [similar
method](c956ed06dc/llvm/include/llvm/ADT/SetVector.h (L90)),
and https://github.com/llvm/llvm-project/pull/138170 has a use case to
get an ArrayRef of MapVector's underlying vector.
2025-05-06 11:26:53 -07:00
Florian Hahn
0d6c9f3273
[ArrayRefTest] Use plain array instead of std::array in unit tests.
std::array<> doesn't seem to decay to plain pointers on some MSVC
versions, causing windows build failures after 101fd87f98c9.

Use plain arrays to fix windows builds, including
https://lab.llvm.org/buildbot/#/builders/2/builds/22803
2025-04-30 13:47:44 +01:00
Florian Hahn
101fd87f98
[ArrayRef] Add constructor from iterator_range<U*> (NFC). (#137796)
Add a new constructor to ArrayRef that takes an iterator_range with a
random access iterator that can be converted.

This can help to avoid creating unnecessary iterator_ranges for types
where an ArrayRef can already be constructed. To be used in
https://github.com/llvm/llvm-project/pull/137798.


PR: https://github.com/llvm/llvm-project/pull/137796
2025-04-30 12:56:59 +01:00
Kazu Hirata
f0cc50cdc9
[ADT] Add hash_combine_range that takes a range (NFC) (#136459)
The new function will allow us to replace:

  hash_combine_range(Ops.begin(), Ops.end())

with:

  hash_combine_range(Ops)
2025-04-20 11:20:32 -07:00
Yingwei Zheng
e710a5a9f2
[InstCombine] Fold fneg/fabs patterns with ppc_f128 (#130557)
This patch is needed by
https://github.com/llvm/llvm-project/pull/130496.
2025-04-14 14:30:00 +08:00
donald chen
27ca4837ee
[EquivalenceClasses] Introduce erase member function (#134660)
Introduce 'erase(const ElemTy &V)' member function to allow the deletion
of a certain value from EquivClasses. This is essential for certain
scenarios that require modifying the contents of EquivClasses.

---------

Co-authored-by: Florian Hahn <flo@fhahn.com>
2025-04-10 12:42:10 +08:00
Florian Hahn
13799998c0
[EquivalenceClasses] Use DenseMap instead of std::set. (NFC) (#134264)
Replace the std::set with DenseMap, which removes the requirement for an
ordering predicate. This also requires to allocate the ECValue objects
separately. This patch uses a BumpPtrAllocator.

Follow-up to https://github.com/llvm/llvm-project/pull/134075.

Compile-time impact is mostly neutral or slightly positive:

https://llvm-compile-time-tracker.com/compare.php?from=ee4e8197fa67dd1ed6e9470e00708e7feeaacd97&to=242e6a8e42889eebfc0bb5d433a4de7dd9e224a7&stat=instructions:u
2025-04-05 12:24:39 +01:00
Ramkumar Ramachandra
fd6260f13b
[EquivClasses] Shorten members_{begin,end} idiom (#134373)
Introduce members() iterator-helper to shorten the members_{begin,end}
idiom. A previous attempt of this patch was #130319, which had to be
reverted due to unit-test failures when attempting to call members() on
the end iterator. In this patch, members() accepts either an ECValue or
an ElemTy, which is more intuitive and doesn't suffer from the same
issue.
2025-04-04 14:34:08 +01:00
Baranov Victor
8ecb2f9c4a
[ADT] Add DenseMap::insert_range (#133600)
This PR add `DenseMap::insert_range` to `DenseMap` for consistency with
existing `DenseSet::insert_range`, `SmallSet::insert_range` and
`std::map::insert_range`.
2025-03-30 14:39:32 -04:00
Kazu Hirata
d4427f308e
[llvm] Use range constructors of *Set (NFC) (#133549) 2025-03-28 19:55:18 -07:00
Kazu Hirata
7cc17fb085
[ADT] Remove old range constructors of SmallSet and StringSet (#133205)
This patch removes the old range constructors of SmallSet and
StringSet that do not take the llvm::from_range tag.  Since there are
so few uses, this patch directly removes them without going through
the deprecation process.
2025-03-27 07:52:13 -07:00
Kazu Hirata
0175dce138
[ADT] Add a range constructor to SmallSetVector (#132645)
DenseSet recently gained a range constructor:

  DenseSet<T> Dest(llvm::from_range, Src);

This patch adds the same signature to SmallSetVector for consistency.
2025-03-23 19:12:53 -07:00
Kazu Hirata
46880fe12b
[ADT] Add range constructors to *Set (#132623)
DenseSet recently gained a range constructor:

  DenseSet<T> Dest(llvm::from_range, Src);

This patch adds the same signature to SetVector, SmallPtrSet,
SmallSet, and StringSet for consistency.
2025-03-23 12:13:42 -07:00
Kazu Hirata
4379d22013
[ADT] Add DenseSet(llvm::from_t, Range) (#131832)
This patch adds a constructor of the form:

  DenseSet Set(llvm::from_range, Range)

while forward-porting std::from_range from c++23.
2025-03-22 18:17:45 -07:00
Kazu Hirata
9654df3f5e
[ADT] Add StringSet::insert_range (#131957)
This pach adds StringSet::insert_range for consistency with
DenseSet::insert_range and std::set::insert_range from C++23.

In the unit test, I'm using contains instead of
testing::UnorderedElementsAre because the latter doesn't seem to work
with char *.
2025-03-19 07:13:01 -07:00