aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/APIntTest.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-05-22[APInt] APInt::clearBitsSlowCase - fix cut+paste typo when merging ↵Simon Pilgrim1-0/+9
lo/himasks (#141108) Fixes #141098
2025-05-19[APInt] Added APInt::clearBits() method (#137098)Liam Semeria1-0/+64
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-01-17add power function to APInt (#122788)Iman Hosseini1-0/+67
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>
2024-10-15[APInt] Fix getAllOnes() with zero width (#112227)Nikita Popov1-0/+1
This makes sure that APInt::getAllOnes() keeps working after the APInt constructor assertions are enabled. I'm relaxing the requirement for the signed case to either an all zeros or all ones integer. This is basically saying that we can interpret the zero-width integer as either positive or negative.
2024-09-20[UnitTests] Fix APInt signed flags (NFC)Nikita Popov1-89/+91
This makes unit tests compatible with the assertion added in https://github.com/llvm/llvm-project/pull/106524, by setting the isSigned flag to the correct value or changing how the value is constructed.
2024-09-02[APInt] Add default-disabled assertion to APInt constructor (#106524)Nikita Popov1-2/+3
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-05-13[ADT] Introduce `APInt::clearHighBits` (#91938)Yingwei Zheng1-0/+65
This patch addresses https://github.com/llvm/llvm-project/pull/90034#discussion_r1579235844.
2024-04-22[llvm] Add support for zero-width integers in MathExtras.h (#87193)Théo Degioanni1-0/+3
MLIR uses zero-width integers, but also re-uses integer logic from LLVM to avoid duplication. This creates issues when LLVM logic is used in MLIR on integers which can be zero-width. In order to avoid special-casing the bitwidth-related logic in MLIR, this PR adds support for zero-width integers in LLVM's MathExtras (and consequently APInt). While most of the logic in theory works the same way out of the box, because bitshifting right by the entire bitwidth in C++ is undefined behavior instead of being zero, some special cases had to be added. Fortunately, it seems like the performance penalty is small. In x86, this usually yields the addition of a predicated conditional move. I checked that no branch is inserted in Arm too. This happens to fix a crash in `arith.extsi` canonicalization in MLIR. I think a follow-up PR to add tests for i0 in arith would be beneficial.
2024-04-04[APInt] Remove multiplicativeInverse with explicit modulus (#87644)Jay Foad1-15/+4
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[APInt] Add a simpler overload of multiplicativeInverse (#87610)Jay Foad1-1/+2
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-02[ADT] Add signed and unsigned mulh to APInt (#84719)Atousa Duprat1-0/+52
Fixes #84207
2024-03-15[ADT] APIntTest - use APInt::getMaxValue/getSignedMinValue/getSignedMaxValue ↵Simon Pilgrim1-3/+3
instead of raw (U)INT_MAX/MIN defines Fixes warnings on MSVC builds
2024-03-15[ADT][APInt] add sfloordiv_ov APInt's member function (#84720)long.chen1-0/+64
for mlir fold to avoid too many overflow state check
2024-03-14[ADT] Add implementations for avgFloor and avgCeil to APInt (#84431)Atousa Duprat1-0/+86
Supports both signed and unsigned expansions. SelectionDAG now calls the APInt implementation of these functions. Fixes #84211.
2024-03-12[ADT] Add APIntOps::abds signed absolute difference and rename absdiff -> ↵Simon Pilgrim1-16/+50
abdu (#84791) When I created APIntOps::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 APIntOps::abds implementation and renamed APIntOps::absdiff to APIntOps::abdu. Given that APIntOps::absdiff is so young I don't think we need to create a deprecation wrapper, but I can if anyone thinks it important. I'll do a KnownBits rename patch after this.
2024-03-05[clang] Use separator for large numeric values in overflow diagnostic (#80939)Atousa Duprat1-0/+35
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-02-19[APInt] Add APIntOps::absdiff to compute the absolute difference of 2 ↵Simon Pilgrim1-0/+34
unsigned values (#82255) Equivalent to "umax(A, B) - umin(A, B)" First step towards adding knownbits support for absdiff patterns for #81765
2023-06-27[Align] Add isAligned taking an APIntGuillaume Chatelet1-0/+20
This showed up in https://reviews.llvm.org/D153308 Reviewed By: courbet, nikic Differential Revision: https://reviews.llvm.org/D153356
2023-05-31[APInt] Support zero-width extract in extractBitsAsZExtValue()Nikita Popov1-0/+1
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-19Add control of hex casing in APInt::toStringThomas Preud'homme1-0/+3
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-02-20Migrate away from the soft-deprecated functions in APInt.h (NFC)Kazu Hirata1-11/+11
Note that those functions on the left hand side are soft-deprecated in favor of those on the right hand side: getMinSignedBits -> getSignificantBits getNullValue -> getZero isNullValue -> isZero isOneValue -> isOne
2023-02-16[ADT] Provide C++20-style bit functionsKazu Hirata1-311/+311
Tihs patches adds APInt::count{l,r}_{zero,one} and APInt::popcount to be consistent with those functions in ADT/bit.h. Once this patch lands, I'll take care of the migration. For now, I am intentionally leaving isPowerOf2 as is. Differential Revision: https://reviews.llvm.org/D144165
2023-01-03[APInt] Add APInt::isOneBitSet helper.Simon Pilgrim1-0/+10
Equivalent tester for the APInt::getOneBitSet builder. This should allow us to remove a number of cases where we're doing "Val == (1 << BitNo)" style code patterns.
2022-12-15[APInt] Fix a bug in the unit testing introduced in 55968109dPeter Rong1-1/+2
Signed-off-by: Peter Rong <PeterRong96@gmail.com>
2022-12-15[APInt] provide a safe API for zext value and sext value.Peter Rong1-0/+21
Currently, APInt::getSExtValue and getZExtValue crashes on values with more than 64 bits. Users may accidently crash the compiler with this setting when the integer may be i128. As shown in https://github.com/llvm/llvm-project/issues/59316 In this patch we provide a trySExtValue and tryZExtValue to return an Optional, the user needs to explictly unwrap it and condsier the possibility where there my no value in it. Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D139683
2022-12-10[APInt] Convert GetMostSignificantDifferentBit to std::optionalKrzysztof Parzyszek1-1/+2
2022-12-08[SCEV] Convert Optional to std::optionalKrzysztof Parzyszek1-3/+2
2022-12-02[llvm/unittests] Use std::nullopt instead of None (NFC)Kazu Hirata1-4/+4
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-11-01Fix DenseMap with APInt keysWeverything1-0/+8
The empty key value for APInt was colliding with a valid zero-width APInt. Change the internal value of empty key and tombstone values for APInt to avoid this collision. Fixes: https://github.com/llvm/llvm-project/issues/58013 Differential Revision: https://reviews.llvm.org/D135741
2022-07-01[ISel] Match all bits when merge undefs for DAG combineXiang1 Zhang1-0/+9
Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D128570
2022-07-01Revert "[ISel] Match all bits when merge undef(s) for DAG combine"Xiang1 Zhang1-9/+0
This reverts commit 5fe5aa284efed1ee1492e1f266351b35f0a8bb69.
2022-07-01[ISel] Match all bits when merge undef(s) for DAG combineXiang1 Zhang1-0/+9
2022-06-25[llvm] Don't use Optional::{hasValue,getValue} (NFC)Kazu Hirata1-2/+2
2022-05-19[APInt] Deprecate truncOrSelf, zextOrSelf and sextOrSelfJay Foad1-3/+0
Differential Revision: https://reviews.llvm.org/D125558
2022-05-14[APInt] Allow extending and truncating to the same widthJay Foad1-1/+10
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-02-08Fix signed/unsigned comparison warnings on ppc buildbotsSimon Pilgrim1-6/+6
2022-02-08Fix signed/unsigned comparison warnings on ppc buildbotsSimon Pilgrim1-6/+6
2022-02-08[ADT] Add APInt/MathExtras isShiftedMask variant returning mask offset/lengthSimon Pilgrim1-0/+22
In many cases, calls to isShiftedMask are immediately followed with checks to determine the size and position of the bitmask. This patch adds variants of APInt::isShiftedMask, isShiftedMask_32 and isShiftedMask_64 that return these values as additional arguments. I've updated a number of cases that were either performing seperate size/position calculations or had created their own local wrapper versions of these. Differential Revision: https://reviews.llvm.org/D119019
2021-11-30[ADT] Remove 0-width Asserts in APInt.getZExtValueSchuyler Eldridge1-0/+3
Remove assertion that disallows getting a zero-extended value from a zero-width APInt. This check is too restrictive and makes it difficult to use APInt to model zero-width things, e.g., zero-width wires in the CIRCT project. Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com> Reviewed By: lattner, darthscsi, nikic Differential Revision: https://reviews.llvm.org/D114768
2021-10-19[ADT] Add APInt::isNegatedPowerOf2() helperSimon Pilgrim1-0/+26
Inspired by D111968, provide a isNegatedPowerOf2() wrapper instead of obfuscating code with (-Value).isPowerOf2() patterns, which I'm sure are likely avenues for typos..... Differential Revision: https://reviews.llvm.org/D111998
2021-10-18[ADT] Fix Wshift-overflow gcc warning in isPowerOf2 unit testSimon Pilgrim1-1/+1
2021-10-18[ADT] Add some basic APInt::isPowerOf2() unit test coverageSimon Pilgrim1-0/+20
2021-10-16[APInt] Fix 1-bit edge case in smul_ov()Nikita Popov1-3/+18
The sdiv used to check for overflow can itself overflow if the LHS is signed min and the RHS is -1. The code tried to account for this by also checking the commuted version. However, for 1-bit values, signed min and -1 are the same value, so both divisions overflow. As such, the overflow for -1 * -1 was not detected (which results in -1 rather than 1 for 1-bit values). Fix this by explicitly checking for this case instead. Noticed while adding exhaustive test coverage for smul_ov(), which is also part of this commit.
2021-10-06[APInt] Fix isAllOnes and extractBits for zero width values.Chris Lattner1-0/+2
isAllOnes() should return true for zero bit values because there are no zeros in it. Thanks to Jay Foad for pointing this out. Differential Revision: https://reviews.llvm.org/D111241
2021-10-05[APInt] Make insertBits and concat work with zero width APInts.Chris Lattner1-1/+10
These should both clearly work with our current model for zero width integers, but don't until now! Differential Revision: https://reviews.llvm.org/D111113
2021-09-13[APInt] Add a concat method, use LLVM_UNLIKELY to help optimizer.Chris Lattner1-0/+12
Three unrelated changes: 1) Add a concat method as a convenience to help write bitvector use cases in a nicer way. 2) Use LLVM_UNLIKELY as suggested by @xbolva00 in a previous patch. 3) Fix casing of some "slow" methods to follow naming standards. Differential Revision: https://reviews.llvm.org/D109620
2021-09-13[APInt] Add APIntOps::ScaleBitMask helperSimon Pilgrim1-0/+20
APInt is used to describe a bit mask in a variety of value tracking and demanded bits/elts functions. When traversing through dst/src operands, we have a number of places where these masks need to widened/narrowed to translate through bitcasts, reductions etc. to a different type. This patch add a APIntOps::ScaleBitMask common helper, adds unit test coverage, and updates a number of cases to use the the helper instead of their own implementation. This came up on D109065 where we currently have to add yet another implementation of the same code. Differential Revision: https://reviews.llvm.org/D109683
2021-09-09[APInt] Enable APInt to support zero bit integers.Chris Lattner1-1/+75
Motivation: APInt not supporting zero bit values leads to a lot of special cases in various bits of code, particularly when using APInt as a bit vector (where you want to start with zero bits and then concat on more. This is particularly challenging in the CIRCT project, where the absence of zero-bit ConstantOp forces duplication of ops and makes instcombine-like logic far more complicated. Approach: zero bit integers are weird. There are two reasonable approaches: either make it illegal to do general arithmetic on them (e.g. sign extends), or treat them as as implicitly having a zero value. This patch takes the conservative approach, which enables their use in bitvector applications. Differential Revision: https://reviews.llvm.org/D109555
2021-09-09[APInt] Normalize naming on keep constructors / predicate methods.Chris Lattner1-11/+11
This renames the primary methods for creating a zero value to `getZero` instead of `getNullValue` and renames predicates like `isAllOnesValue` to simply `isAllOnes`. This achieves two things: 1) This starts standardizing predicates across the LLVM codebase, following (in this case) ConstantInt. The word "Value" doesn't convey anything of merit, and is missing in some of the other things. 2) Calling an integer "null" doesn't make any sense. The original sin here is mine and I've regretted it for years. This moves us to calling it "zero" instead, which is correct! APInt is widely used and I don't think anyone is keen to take massive source breakage on anything so core, at least not all in one go. As such, this doesn't actually delete any entrypoints, it "soft deprecates" them with a comment. Included in this patch are changes to a bunch of the codebase, but there are more. We should normalize SelectionDAG and other APIs as well, which would make the API change more mechanical. Differential Revision: https://reviews.llvm.org/D109483
2021-09-08[APInt.h] Reduce the APInt header file interface a bit. NFCChris Lattner1-20/+0
This moves one mid-size function out of line, inlines the trivial tcAnd/tcOr/tcXor/tcComplement methods into their only caller, and moves the magic/umagic functions into SelectionDAG since they are implementation details of its algorithm. This also removes the unit tests for magic, but these are already tested in the divide lowering logic for various targets. This also upgrades some C style comments to C++. Differential Revision: https://reviews.llvm.org/D109476