summaryrefslogtreecommitdiff
path: root/llvm/lib/Support/SmallPtrSet.cpp
AgeCommit message (Collapse)Author
2025-09-24[Support] Use list-initialization for returning pairs (#160447)Kazu Hirata
In C++17 and later, "return {A, B};" guarantees copy elision for a std::pair return type, ensuring the object is constructed directly in the return slot. This patch updates those instances under Support/.
2025-08-18[ADT] Refactor SmallPtrSetImplBase::swap (NFC) (#154261)Kazu Hirata
SmallPtrSetImplBase::swap needs to deal with four cases depending on whether LHS is small and whether RHS is small. Now, the code to swap small LHS and large RHS is symmetric with the code to swap large LHS and small RHS. This patch rearranges code so that we first take care of the case where both LHS and RHS are small. Then we compute references SmallSide and LargeSide and actually swap the two instances. This refactoing saves about 11 lines of code. Note that SmallDenseMap::swap also uses a similar trick.
2025-08-16[ADT] Use llvm::copy in SmallPtrSet.cpp (NFC) (#153930)Kazu Hirata
This patch uses llvm::copy in combination with buckets() and small_buckets().
2025-08-15[ADT] Rename NumNonEmpty to NumEntries in SmallPtrSet (NFC) (#153757)Kazu Hirata
Without this patch, we use NumNonEmpty, which keeps track of the number of valid entries plus tombstones even though we have a separate variable to keep track of the number of tombstones. This patch simplifies the metadata. Specifically, it changes the name and semantics of the variable to NumEntries to keep track of the number of valid entries. The difference in semantics requires some code changes aside from mechanical replacements: - size() just returns NumEntries. - erase_imp() and remove_if() need to decrement NumEntries in the large mode. - insert_imp_big() increments NumEntries for successful insertions, regardless of whether a tombstone is being replaced with a valid entry. It also computes the number of non-tombstone empty slots as: CurArraySize - NumEntries - NumTombstones - Grow() no longer needs NumNonEmpty -= NumTombstones. Overall, the resulting code should look more intuitive and more consistent with DenseMapSet.
2025-08-09[ADT] Use range-based for loops in SmallPtrSet (NFC) (#152882)Kazu Hirata
This patch introduces helper function buckets() to convert two loops to range-based for loops.
2024-11-30[SmallPtrSet] Remove SmallArray member (NFC) (#118099)Nikita Popov
Currently SmallPtrSet stores CurArray and SmallArray, with the former pointing to the latter in small representation. Instead, only use CurArray and a separate IsSmall boolean. Most of the implementation doesn't need the separate SmallArray member -- we only need it for copy/move/swap operations, where we may switch back from large to small representation. In those cases, we explicitly pass down the pointer to SmallStorage. This reduces the size of SmallPtrSet and improves compile-time.
2024-08-19[SmallPtrSet] Optimize find/eraseFangrui Song
Port #100517 for DenseMap. Pull Request: https://github.com/llvm/llvm-project/pull/104740
2022-01-21[llvm] Cleanup header dependencies in ADT and Supportserge-sans-paille
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
2021-06-03[ADT] Move DenseMapInfo for ArrayRef/StringRef into respective headers (NFC)Nikita Popov
This is a followup to D103422. The DenseMapInfo implementations for ArrayRef and StringRef are moved into the ArrayRef.h and StringRef.h headers, which means that these two headers no longer need to be included by DenseMapInfo.h. This required adding a few additional includes, as many files were relying on various things pulled in by ArrayRef.h. Differential Revision: https://reviews.llvm.org/D103491
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-06-09Use uniform mechanism for OOM errors handlingSerge Pavlov
This is a recommit of r333506, which was reverted in r333518. The original commit message is below. In r325551 many calls of malloc/calloc/realloc were replaces with calls of their safe counterparts defined in the namespace llvm. There functions generate crash if memory cannot be allocated, such behavior facilitates handling of out of memory errors on Windows. If the result of *alloc function were checked for success, the function was not replaced with the safe variant. In these cases the calling function made the error handling, like: T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T))); if (NewElts == nullptr) report_bad_alloc_error("Allocation of SmallVector element failed."); Actually knowledge about the function where OOM occurred is useless. Moreover having a single entry point for OOM handling is convenient for investigation of memory problems. This change removes custom OOM errors handling and replaces them with calls to functions `llvm::safe_*alloc`. Declarations of `safe_*alloc` are moved to a separate include file, to avoid cyclic dependency in SmallVector.h Differential Revision: https://reviews.llvm.org/D47440 llvm-svn: 334344
2018-05-30Revert commit 333506Serge Pavlov
It looks like this commit is responsible for the fail: http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-autoconf/builds/24382. llvm-svn: 333518
2018-05-30Use uniform mechanism for OOM errors handlingSerge Pavlov
This is a recommit of r333390, which was reverted in r333395, because it caused cyclic dependency when building shared library `LLVMDemangle.so`. In this commit `ItaniumDemangler.cpp` was not changed. The original commit message is below. In r325551 many calls of malloc/calloc/realloc were replaces with calls of their safe counterparts defined in the namespace llvm. There functions generate crash if memory cannot be allocated, such behavior facilitates handling of out of memory errors on Windows. If the result of *alloc function were checked for success, the function was not replaced with the safe variant. In these cases the calling function made the error handling, like: T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T))); if (NewElts == nullptr) report_bad_alloc_error("Allocation of SmallVector element failed."); Actually knowledge about the function where OOM occurred is useless. Moreover having a single entry point for OOM handling is convenient for investigation of memory problems. This change removes custom OOM errors handling and replaces them with calls to functions `llvm::safe_*alloc`. Declarations of `safe_*alloc` are moved to a separate include file, to avoid cyclic dependency in SmallVector.h Differential Revision: https://reviews.llvm.org/D47440 llvm-svn: 333506
2018-05-29Reverted commits 333390, 333391 and 333394Serge Pavlov
Build of shared library LLVMDemangle.so fails due to dependency problem. llvm-svn: 333395
2018-05-29Use uniform mechanism for OOM errors handlingSerge Pavlov
In r325551 many calls of malloc/calloc/realloc were replaces with calls of their safe counterparts defined in the namespace llvm. There functions generate crash if memory cannot be allocated, such behavior facilitates handling of out of memory errors on Windows. If the result of *alloc function were checked for success, the function was not replaced with the safe variant. In these cases the calling function made the error handling, like: T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T))); if (NewElts == nullptr) report_bad_alloc_error("Allocation of SmallVector element failed."); Actually knowledge about the function where OOM occurred is useless. Moreover having a single entry point for OOM handling is convenient for investigation of memory problems. This change removes custom OOM errors handling and replaces them with calls to functions `llvm::safe_*alloc`. Declarations of `safe_*alloc` are moved to a separate include file, to avoid cyclic dependency in SmallVector.h Differential Revision: https://reviews.llvm.org/D47440 llvm-svn: 333390
2017-10-13[SmallPtrSet] Add iterator epoch tracking.Benjamin Kramer
This will detect invalid iterators when ABI breaking checks are enabled. llvm-svn: 315746
2017-07-20Support, IR, ADT: Check nullptr after allocation with malloc/realloc or callocMatthias Braun
As a follow up of the bad alloc handler patch, this patch introduces nullptr checks on pointers returned from the malloc/realloc/calloc functions. In addition some memory size assignments are moved behind the allocation of the corresponding memory to fulfill exception safe memory management (RAII). patch by Klaus Kretzschmar Differential Revision: https://reviews.llvm.org/D35414 llvm-svn: 308576
2016-12-31[SmallPtrSet] Introduce a find primitive and rewrite count/erase in terms of itPhilip Reames
This was originally motivated by a compile time problem I've since figured out how to solve differently, but the cleanup seemed useful. We had the same logic - which essentially implemented find - in several places. By commoning them out, I can implement find and allow erase to be inlined at the call sites if profitable. Differential Revision: https://reviews.llvm.org/D28183 llvm-svn: 290779
2016-08-23Fix some Clang-tidy modernize-use-using and Include What You Use warnings; ↵Eugene Zelenko
other minor fixes. Differential revision: https://reviews.llvm.org/D23789 llvm-svn: 279535
2016-02-15SmallPtrSet: Avoid initializing Array in the small case.Matthias Braun
This patch avoids the initial memset at the cost of making iterators slightly more complex. This should be beneficial as most SmallPtrSets hold no or only a few elements, while iterating over them is less common. Differential Revision: http://reviews.llvm.org/D16672 llvm-svn: 260913
2016-01-28SmallPtrSet: Make destructor available for inliningMatthias Braun
llvm-svn: 259019
2016-01-28SmallPtrSet: Share some code between copy/move constructor/assignment operatorMatthias Braun
llvm-svn: 259018
2016-01-28SmallPtrSet: Remove trailing whitespace, fix indentationMatthias Braun
llvm-svn: 259017
2016-01-27SmallPtrSet: Inline the part of insert_imp in the small caseMatthias Braun
Most of the time we only hit the small case, so it is beneficial to pull it out of the insert_imp() implementation. This improves compile time at least for non-LTO builds. Differential Revision: http://reviews.llvm.org/D16619 llvm-svn: 258908
2015-02-23Sync the __builtin_expects for our 3 quadratically probed hash table ↵Benjamin Kramer
implementations. This assumes that a) finding the bucket containing the value is LIKELY b) finding an empty bucket is LIKELY c) growing the table is UNLIKELY I also switched the a) and b) cases for SmallPtrSet as we seem to use the set mostly more for insertion than for checking existence. In a simple benchmark consisting of 2^21 insertions of 2^20 unique pointers into a DenseMap or SmallPtrSet a few percent speedup on average, but nothing statistically significant. llvm-svn: 230232
2014-11-19Update SetVector to rely on the underlying set's insert to return a ↵David Blaikie
pair<iterator, bool> This is to be consistent with StringSet and ultimately with the standard library's associative container insert function. This lead to updating SmallSet::insert to return pair<iterator, bool>, and then to update SmallPtrSet::insert to return pair<iterator, bool>, and then to update all the existing users of those functions... llvm-svn: 222334
2014-08-20Fix an off by 1 bug that prevented SmallPtrSet from using all of its 'small' ↵Craig Topper
capacity. Then fix the early return in the move constructor that prevented 'small' moves from clearing the NumElements in the moved from object. The directed test missed this because it was always testing large moves due to the off by 1 bug. llvm-svn: 216044
2014-04-07[C++11] Make use of 'nullptr' in the Support library.Craig Topper
llvm-svn: 205697
2014-03-01[C++11] Remove the R-value reference #if usage from the ADT and SupportChandler Carruth
libraries. It is now always 1 in LLVM builds. llvm-svn: 202580
2014-02-03Rename the non-templated base class of SmallPtrSet toChandler Carruth
'SmallPtrSetImplBase'. This more closely matches the organization of SmallVector and should allow introducing a SmallPtrSetImpl which serves the same purpose as SmallVectorImpl: isolating the element type from the particular small size chosen. This in turn allows a lot of simplification of APIs by not coding them against a specific small size which is rarely needed. llvm-svn: 200687
2013-11-26Lift self-copy protection up to the header file and add self-moveChandler Carruth
protection to the same layer. This is in line with Howard's advice on how best to handle self-move assignment as he explained on SO[1]. It also ensures that implementing swap with move assignment continues to work in the case of self-swap. [1]: http://stackoverflow.com/questions/9322174/move-assignment-operator-and-if-this-rhs llvm-svn: 195705
2013-11-26Fix a self-memcpy which only breaks under Valgrind's memcpyChandler Carruth
implementation. Silliness, but it'll be a trivial performance optimization. This should clear up a failure on the vg_leak bot. llvm-svn: 195704
2013-11-20Make the moved-from SmallPtrSet be a valid, empty, small-state object.Chandler Carruth
Enhance the tests to actually require moves in C++11 mode, in addition to testing the moved-from state. Further enhance the tests to cover copy-assignment into a moved-from object and moving a large-state object. (Note that we can't really test small-state vs. large-state as that isn't an observable property of the API really.) This should finish addressing review on r195239. llvm-svn: 195261
2013-11-20Give SmallPtrSet move semantics when we have R-value references.Chandler Carruth
Somehow, this ADT got missed which is moderately terrifying considering the efficiency of move for it. The code to implement move semantics for it is pretty horrible currently but was written to reasonably closely match the rest of the code. Unittests that cover both copying and moving (at a basic level) added. llvm-svn: 195239
2013-11-18Fixing a possible memory leak from a failing realloc() call.Aaron Ballman
llvm-svn: 195018
2013-03-29SmallVector and SmallPtrSet allocations now power-of-two aligned.Jean-Luc Duprat
This time tested on both OSX and Linux. llvm-svn: 178377
2013-03-29Revert "Fix allocations of SmallVector and SmallPtrSet so they are more ↵Rafael Espindola
prone to" This reverts commit 617330909f0c26a3f2ab8601a029b9bdca48aa61. It broke the bots: /home/clangbuild2/clang-ppc64-2/llvm.src/unittests/ADT/SmallVectorTest.cpp:150: PushPopTest /home/clangbuild2/clang-ppc64-2/llvm.src/unittests/ADT/SmallVectorTest.cpp:118: Failure Value of: v[i].getValue() Actual: 0 Expected: value Which is: 2 llvm-svn: 178334
2013-03-29Fix allocations of SmallVector and SmallPtrSet so they are more prone toJean-Luc Duprat
being power-of-two sized. llvm-svn: 178332
2012-04-18SmallPtrSet: Reuse DenseMapInfo's pointer hash function instead of inventing ↵Benjamin Kramer
a bad one ourselves. DenseMap's hash function uses slightly more entropy and reduces hash collisions significantly. I also experimented with Hashing.h, but it didn't gave a lot of improvement while being much more expensive to compute. llvm-svn: 154996
2012-03-07Copy the right amount of elements.Benjamin Kramer
llvm-svn: 152254
2012-03-07SmallPtrSet: Copy all the elements when swapping, not just numelements.Benjamin Kramer
This fixes a build failure in webkit. Copying all elements shouldn't be necessary, I'll look out for a better fix soon. llvm-svn: 152252
2012-03-06SmallPtrSet: Provide a more efficient implementation of swap than the ↵Benjamin Kramer
default triple-copy std::swap. This currently assumes that both sets have the same SmallSize to keep the implementation simple, a limitation that can be lifted if someone cares. llvm-svn: 152143
2011-03-30Prevent infinite growth of SmallPtrSet instances.Jakob Stoklund Olesen
Rehash but don't grow when full of tombstones. Patch by José Fonseca! llvm-svn: 128566
2010-06-30Rather than giving SmallPtrSetImpl a member field SmallArray which is magicallyDuncan Sands
replaced by a bigger array in SmallPtrSet (by overridding it), instead just use a pointer to the start of the storage, and have SmallPtrSet pass in the value to use. This has the disadvantage that SmallPtrSet becomes bigger by one pointer. It has the advantage that it no longer uses tricky C++ rules, and is clearly correct while I'm not sure the previous version was. This was inspired by g++-4.6 pointing out that SmallPtrSetImpl was writing off the end of SmallArray, which it was. Since SmallArray is replaced with a bigger array in SmallPtrSet, the write was still to valid memory. But it was writing off the end of the declared array type - sounds kind of dubious to me, like it sounded dubious to g++-4.6. Maybe g++-4.6 is wrong and this construct is perfectly valid and correctly compiled by all compilers, but I think it is better to avoid the whole can of worms by avoiding this construct. llvm-svn: 107285
2008-08-05Fix several const-correctness issues, resolving some -Wcast-qual warnings.Dan Gohman
llvm-svn: 54349
2007-12-29Remove attribution from file headers, per discussion on llvmdev.Chris Lattner
llvm-svn: 45418
2007-11-06make smallptrset more const and type correct, which caught a fewChris Lattner
minor bugs. llvm-svn: 43782
2007-08-15Properly use const qualifiersAnton Korobeynikov
llvm-svn: 41111
2007-08-05When clearing a SmallPtrSet, if the set had a huge capacity, but theChris Lattner
contents of the set were small, deallocate and shrink the set. This avoids having us to memset as much data, significantly speeding up some pathological cases. For example, this speeds up the verifier from 0.3899s to 0.0763 (5.1x) on the testcase from PR1432 in a release build. llvm-svn: 40837
2007-07-27Allow SmallPtrSet to hold pointers to const data.Owen Anderson
llvm-svn: 40556