Skip to content

Commit

Permalink
SmallSet/SmallPtrSet: Refuse huge Small numbers
Browse files Browse the repository at this point in the history
These sets do linear searching in small mode; It is not a good idea to
use huge numbers as the small value here, save people from themselves by
adding a static_assert.

Differential Revision: http://reviews.llvm.org/D16706

llvm-svn: 259419
  • Loading branch information
MatzeB committed Feb 1, 2016
1 parent d8da15f commit 3f88eab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions llvm/include/llvm/ADT/SmallPtrSet.h
Expand Up @@ -329,6 +329,11 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
/// SmallPtrSetImplBase for details of the algorithm.
template<class PtrType, unsigned SmallSize>
class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
// In small mode SmallPtrSet uses linear search for the elements, so it is
// not a good idea to choose this value too high. You may consider using a
// DenseSet<> instead if you expect many elements in the set.
static_assert(SmallSize <= 32, "SmallSize should be small");

typedef SmallPtrSetImpl<PtrType> BaseT;

// Make sure that SmallSize is a power of two, round up if not.
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/ADT/SmallSet.h
Expand Up @@ -38,6 +38,11 @@ class SmallSet {
typedef typename SmallVector<T, N>::const_iterator VIterator;
typedef typename SmallVector<T, N>::iterator mutable_iterator;

// In small mode SmallPtrSet uses linear search for the elements, so it is
// not a good idea to choose this value too high. You may consider using a
// DenseSet<> instead if you expect many elements in the set.
static_assert(N <= 32, "N should be small");

public:
typedef size_t size_type;
SmallSet() {}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/CppBackend/CPPBackend.cpp
Expand Up @@ -1651,8 +1651,8 @@ void CppWriter::printFunctionUses(const Function* F) {

// Print type definitions for every type referenced by an instruction and
// make a note of any global values or constants that are referenced
SmallPtrSet<GlobalValue*,64> gvs;
SmallPtrSet<Constant*,64> consts;
SmallPtrSet<GlobalValue*,32> gvs;
SmallPtrSet<Constant*,32> consts;
for (Function::const_iterator BB = F->begin(), BE = F->end();
BB != BE; ++BB){
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
Expand Down

0 comments on commit 3f88eab

Please sign in to comment.