439 Commits

Author SHA1 Message Date
Kazu Hirata
1f3c38f125
[Support] Remove an unnecessary cast (NFC) (#154048)
qp is already of uint64_t.
2025-08-17 23:46:20 -07: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
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
Rahul Joshi
a76bf4da53
[NFC][ADT/Support] Add {} for else when if body has {} (#140758) 2025-05-21 13:19:09 -07: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
Iman Hosseini
63b0ab8425
remove extra ; (#123352)
Remove erroneous extra semicolon in:
https://github.com/llvm/llvm-project/pull/122788

Co-authored-by: ImanHosseini <imanhosseini.17@gmail.com>
2025-01-17 15:11:52 +00:00
Iman Hosseini
8ae1cb2bcb
add power function to APInt (#122788)
I am trying to calculate power function for APFloat, APInt to constant
fold vector reductions: https://github.com/llvm/llvm-project/pull/122450
I need this utility to fold N `mul`s into power.

---------

Co-authored-by: ImanHosseini <imanhosseini.17@gmail.com>
Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
2025-01-17 14:40:31 +00:00
NAKAMURA Takumi
83953c7df1 APInt.cpp: Prune a stray semicolon. 2024-10-17 20:04:00 +09:00
Craig Topper
bcb15d0059
[APInt] Slightly simplify APInt::ashrSlowCase. NFC (#111220)
Use an arithmetic shift for the last word copy when BitShift!=0. This
avoids an explicit sign extend after the shift.
2024-10-05 10:25:50 -07:00
Princeton Ferro
427e202a40
[APInt] improve initialization performance (#106945)
The purpose is to save an extra memset in both cases:

1. When `int64_t(val) < 0`, zeroing out is redundant as the subsequent
for-loop will initialize to `val .. 0xFFFFF ....`. Instead we should
only create an uninitialized buffer, and transform the slow for-loop
into a memset to initialize the higher words to `0xFF`.
2. In the other case, first we create an uninitialized array (`new
int64_t[]`) and _then_ we zero it out with `memset`. But this can be
combined in one operation with `new int64_t[]()`, which
default-initializes the array.

On one example where use of APInt was heavy, this improved compile time
by 1%.
2024-09-04 08:54:36 +02:00
Nikita Popov
30cc198c2d
[APInt] Add default-disabled assertion to APInt constructor (#106524)
If the uint64_t constructor is used, assert that the value is actually a
signed or unsigned N-bit integer depending on whether the isSigned flag
is set. Provide an implicitTrunc flag to restore the previous behavior,
where the argument is silently truncated instead.

In this commit, implicitTrunc is enabled by default, which means that
the new assertions are disabled and no actual change in behavior occurs.
The plan is to flip the default once all places violating the assertion
have been fixed. See #80309 for the scope of the necessary changes.

The primary motivation for this change is to avoid incorrectly specified
isSigned flags. A recurring problem we have is that people write
something like `APInt(BW, -1)` and this works perfectly fine -- until
the code path is hit with `BW > 64`. Most of our i128 specific
miscompilations are caused by variants of this issue.

The cost of the change is that we have to specify the correct isSigned
flag (and make sure there are no excess bits) for uses where BW is
always <= 64 as well.
2024-09-02 09:48:54 +02:00
Craig Topper
cbd306806a
[APInt] Correct backwards static_assert condition. (#103641)
In order to guarantee that extracting 64 bits doesn't require more than
2 words, the word size would need to be 64 bits or more. If the word
size was smaller than 64, like 32, you may need to read 3 words to get
64 bits.
2024-08-14 00:28:24 -07:00
Craig Topper
7f1f3afd37 [APInt] Use APINT_BITS_PER_WORD instead of recomputing it. NFC 2024-08-13 22:12:37 -07:00
Victor Toni
900be9013f
Fix typos (#88565) 2024-04-12 14:33:35 -07:00
Craig Topper
d8f1e5d289
[APInt] Remove accumulator initialization from tcMultiply and tcFullMultiply. NFCI (#88202)
The tcMultiplyPart routine has a flag that says whether to add to the
accumulator or overwrite it. By using the overwrite mode on the first
iteration we don't need to initialize the accumulator to zero.

Note, the initialization in tcFullMultiply was only initializing the
first rhsParts of dst. tcMultiplyPart always overwrites the rhsParts+1
part that just contains the last carry. The first write to each part of
dst past rhsParts is a carry write so that's how the upper part of dst
is initialized.
2024-04-10 15:07:16 -07:00
Craig Topper
04f33a3ac2
[APInt] Use a std::move() to avoid a copy in the loop in multiplicativeInverse. (#87655)
This allows the subtract to reuse the storage of T. T will be assigned
over by the condition on the next iteration. I think assigning over a
moved from value should be ok.
2024-04-08 20:38:29 -07:00
Jay Foad
0b293e8c36
[APInt] Remove multiplicativeInverse with explicit modulus (#87644)
All callers have been changed to use the new simpler overload with an
implicit modulus of 2^BitWidth. The old form was never used or tested
with non-power-of-two modulus anyway.
2024-04-04 17:24:16 +01:00
Jay Foad
1b761205f2
[APInt] Add a simpler overload of multiplicativeInverse (#87610)
The current APInt::multiplicativeInverse takes a modulus which can be
any value, but all in-tree callers use a power of two. Moreover, most
callers want to use two to the power of the width of an existing APInt,
which is awkward because 2^N is not representable as an N-bit APInt.

Add a new overload of multiplicativeInverse which implicitly uses
2^BitWidth as the modulus.
2024-04-04 16:11:06 +01:00
Atousa Duprat
4aba595f09
[ADT] Add signed and unsigned mulh to APInt (#84719)
Fixes #84207
2024-04-02 17:07:56 +01:00
long.chen
6d30223f7c
[ADT][APInt] add sfloordiv_ov APInt's member function (#84720)
for mlir fold to avoid too many overflow state check
2024-03-15 19:09:46 +08:00
Jay Foad
4e232cab0d
[APInt] Implement average functions without sign/zero-extension. NFC. (#85212)
Removing the extension to FullWidth should make them much more efficient
in the 64-bit case, because 65-bit APInts use a separate allocation for
their bits.
2024-03-14 17:36:06 +00:00
Atousa Duprat
aff0570891
[ADT] Add implementations for avgFloor and avgCeil to APInt (#84431)
Supports both signed and unsigned expansions.

SelectionDAG now calls the APInt implementation of these functions.

Fixes #84211.
2024-03-14 10:00:08 +00:00
Atousa Duprat
c00c901f1c
[clang] Use separator for large numeric values in overflow diagnostic (#80939)
Add functionality to APInt::toString() that allows it to insert
separators between groups of digits, using the C++ literal
separator ' between groups.

Fixes issue #58228

Reviewers:  @AaronBallman, @cjdb, @tbaederr
2024-03-05 07:38:42 -05:00
Guillaume Chatelet
b745c12385 [Align] Add isAligned taking an APInt
This showed up in https://reviews.llvm.org/D153308

Reviewed By: courbet, nikic

Differential Revision: https://reviews.llvm.org/D153356
2023-06-27 09:27:47 +00:00
Nikita Popov
42e98c6ae8 [APInt] Support zero-width extract in extractBitsAsZExtValue()
D111241 added support for extractBits() with zero width. Extend this
to extractBitsAsZExtValue() as well for consistency (in which case
it will always return zero).

Differential Revision: https://reviews.llvm.org/D151788
2023-05-31 17:13:48 +02:00
Jay Foad
bf358e27a4 [APInt] Add unsigned overloads of shift functions
Add overloads of sshl_ov, ushl_ov, sshl_sat and ushl_sat that take the
shift amount as unsigned instead of APInt. This matches what we do for
the normal shift operators and can help to avoid creating temporary
APInts in some cases.

Differential Revision: https://reviews.llvm.org/D151420
2023-05-25 16:05:32 +01:00
Thomas Preud'homme
a17083403f Add control of hex casing in APInt::toString
This will be used in implementing arbitrary precision support to
FileCheck's numeric variables and expressions.

Reviewed By: foad, RKSimon

Differential Revision: https://reviews.llvm.org/D150879
2023-05-19 14:20:47 +01:00
Kazu Hirata
f8f3db2756 Use APInt::count{l,r}_{zero,one} (NFC) 2023-02-19 22:04:47 -08:00
Pavel Kopyl
36606cf070 [NFC] Replace -1U{LL} expressions with appropriate *_MAX macros in Support library.
This makes a code a bit more clear and also gets rid of C4146 warning
on MSVC compiler:
 'unary minus operator applied to unsigned type, result still unsigned'.

In case uint64_t variable is initialized or compared against -1U expression,
which corresponds to 32-bit constant, UINT_MAX macro is used to preserve
NFC semantics; -1ULL is replaced with UINT64_MAX.

Reviewed By: dblaikie, craig.topper

Differential Revision: https://reviews.llvm.org/D143942
2023-02-14 22:16:19 +01:00
Kazu Hirata
f6b8f05bb3 Use llvm::byteswap instead of ByteSwap_{16,32,64} (NFC) 2023-01-28 15:22:37 -08:00
Kazu Hirata
55e2cd1609 Use llvm::count{lr}_{zero,one} (NFC) 2023-01-28 12:41:20 -08:00
Kazu Hirata
49f7d2c4f0 [Support] Use llvm::countr_zero and llvm::Log2_64 in APInt.cpp (NFC)
partMSB and partLSB never get 0 as the argument.  That is, we don't
rely on find{First,Last}Set's ability to return
std::numeric_limits<T>::max() on input 0.

This patch replaces partLSB and partMSB with llvm::countr_zero and
llvm::Log2_64, respectively.

FWIW, nobody in LLVM (except unit test MathExtrasTest.cpp) relies on
find{First,Last}Set's ability to return std::numeric_limits<T>::max()
on input 0.
2023-01-25 21:22:11 -08:00
Kazu Hirata
caa99a01f5 Use llvm::popcount instead of llvm::countPopulation(NFC) 2023-01-22 12:48:51 -08:00
Kazu Hirata
1b2d34e28e [Support] clang-format partMSBpartMSB and partLSB (NFC) 2023-01-15 09:13:26 -08:00
Kazu Hirata
9ce7b40ad4 Use the default parameters of countTrailingZeros and find{First,Last}Set (NFC)
This patch uses the default parameters of countTrailingZeros,
findFirstSet, and findLastSet, which are ZB_Width, ZB_Max, and ZB_Max,
respectively.
2023-01-15 09:04:57 -08: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
Kazu Hirata
6eb0b0a045 Don't include Optional.h
These files no longer use llvm::Optional.
2022-12-14 21:16:22 -08:00
Krzysztof Parzyszek
29041bc050 [APInt] Convert GetMostSignificantDifferentBit to std::optional 2022-12-10 14:03:29 -06:00
Krzysztof Parzyszek
ea6ed399b2 [SCEV] Convert Optional to std::optional 2022-12-08 08:35:11 -06: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
Simon Pilgrim
e35d50b7f2 [ADT] APInt.cpp - remove <cstring> duplicate. NFC.
This is already included in APInt.h
2022-10-23 12:44:20 +01:00
Xiang1 Zhang
72a23cef7e [ISel] Match all bits when merge undefs for DAG combine
Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D128570
2022-07-01 09:09:43 +08:00
Xiang1 Zhang
64f44a90ef Revert "[ISel] Match all bits when merge undef(s) for DAG combine"
This reverts commit 5fe5aa284efed1ee1492e1f266351b35f0a8bb69.
2022-07-01 08:59:04 +08:00
Xiang1 Zhang
5fe5aa284e [ISel] Match all bits when merge undef(s) for DAG combine 2022-07-01 08:58:00 +08:00
Jay Foad
1feed6691a [APInt] Remove truncOrSelf, zextOrSelf and sextOrSelf
Differential Revision: https://reviews.llvm.org/D125559
2022-06-07 10:01:49 +01:00
Jay Foad
6bec3e9303 [APInt] Remove all uses of zextOrSelf, sextOrSelf and truncOrSelf
Most clients only used these methods because they wanted to be able to
extend or truncate to the same bit width (which is a no-op). Now that
the standard zext, sext and trunc allow this, there is no reason to use
the OrSelf versions.

The OrSelf versions additionally have the strange behaviour of allowing
extending to a *smaller* width, or truncating to a *larger* width, which
are also treated as no-ops. A small amount of client code relied on this
(ConstantRange::castOp and MicrosoftCXXNameMangler::mangleNumber) and
needed rewriting.

Differential Revision: https://reviews.llvm.org/D125557
2022-05-19 11:23:13 +01:00
Jay Foad
169ae6db69 [APInt] Allow extending and truncating to the same width
Allow zext, sext, trunc, truncUSat and truncSSat to extend or truncate
to the same bit width, which is a no-op.

Disallowing this forced clients to use workarounds like using
zextOrTrunc (even though they never wanted truncation) or zextOrSelf
(even though they did not want its strange behaviour of allowing a
*smaller* bit width, which is also treated as a no-op).

Differential Revision: https://reviews.llvm.org/D125556
2022-05-14 09:54:24 +01:00
Aaron Ballman
8cba72177d Implement literal suffixes for _BitInt
WG14 adopted N2775 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2775.pdf)
at our Feb 2022 meeting. This paper adds a literal suffix for
bit-precise types that automatically sizes the bit-precise type to be
the smallest possible legal _BitInt type that can represent the literal
value. The suffix chosen is wb (for a signed bit-precise type) which
can be combined with the u suffix (for an unsigned bit-precise type).

The preprocessor continues to operate as-if all integer types were
intmax_t/uintmax_t, including bit-precise integer types. It is a
constraint violation if the bit-precise literal is too large to fit
within that type in the context of the preprocessor (when still using
a pp-number preprocessing token), but it is not a constraint violation
in other circumstances. This allows you to make bit-precise integer
literals that are wider than what the preprocessor currently supports
in order to initialize variables, etc.
2022-03-14 09:24:19 -04:00
serge-sans-paille
75e164f61d [llvm] Cleanup header dependencies in ADT and Support
The cleanup was manual, but assisted by "include-what-you-use". It consists in

1. Removing unused forward declaration. No impact expected.
2. Removing unused headers in .cpp files. No impact expected.
3. Removing unused headers in .h files. This removes implicit dependencies and
   is generally considered a good thing, but this may break downstream builds.
   I've updated llvm, clang, lld, lldb and mlir deps, and included a list of the
   modification in the second part of the commit.
4. Replacing header inclusion by forward declaration. This has the same impact
   as 3.

Notable changes:

- llvm/Support/TargetParser.h no longer includes llvm/Support/AArch64TargetParser.h nor llvm/Support/ARMTargetParser.h
- llvm/Support/TypeSize.h no longer includes llvm/Support/WithColor.h
- llvm/Support/YAMLTraits.h no longer includes llvm/Support/Regex.h
- llvm/ADT/SmallVector.h no longer includes llvm/Support/MemAlloc.h nor llvm/Support/ErrorHandling.h

You may need to add some of these headers in your compilation units, if needs be.

As an hint to the impact of the cleanup, running

clang++ -E  -Iinclude -I../llvm/include ../llvm/lib/Support/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l

before: 8000919 lines
after:  7917500 lines

Reduced dependencies also helps incremental rebuilds and is more ccache
friendly, something not shown by the above metric :-)

Discourse thread on the topic: https://llvm.discourse.group/t/include-what-you-use-include-cleanup/5831
2022-01-21 13:54:49 +01:00
River Riddle
4c484f11d3 [llvm] Add a SFINAE template parameter to DenseMapInfo
This allows for using SFINAE partial specialization for DenseMapInfo.
In MLIR, this is particularly useful as it will allow for defining partial
specializations that support all Attribute, Op, and Type classes without
needing to specialize DenseMapInfo for each individual class.

Differential Revision: https://reviews.llvm.org/D113641
2021-11-16 18:54:14 +00:00