Files
swift-mirror/include/swift/Basic/ValueEnumerator.h
Erik Eckstein ed7a8026d6 ValueEnumerator: make the index type unsigned instead of size_t
This reduces the size of the index from 8 to 4 bytes, which is important in AliasAnalysis where we use pairs of such indices.
2021-05-18 08:56:22 +02:00

62 lines
1.7 KiB
C++

//===--- ValueEnumerator.h --- Enumerates values ----------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_BASIC_VALUEENUMERATOR_H
#define SWIFT_BASIC_VALUEENUMERATOR_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/raw_ostream.h"
namespace swift {
typedef unsigned ValueIndexTy;
/// / This class maps values to unique indices.
template<class ValueTy>
class ValueEnumerator {
/// A running counter to enumerate values.
ValueIndexTy counter = 0;
/// Maps values to unique integers.
llvm::DenseMap<ValueTy, ValueIndexTy> ValueToIndex;
public:
/// Return the index of value \p v.
ValueIndexTy getIndex(const ValueTy &v) {
// Return the index of this Key, if we've assigned one already.
auto It = ValueToIndex.find(v);
if (It != ValueToIndex.end()) {
return It->second;
}
// Generate a new counter for the key.
ValueToIndex[v] = ++counter;
assert(counter != 0 && "counter overflow in ValueEnumerator");
return counter;
}
ValueEnumerator() = default;
/// Forget about key \p v.
void invalidateValue(const ValueTy &v) { ValueToIndex.erase(v); }
/// Clear the enumeration state of the
void clear() {
ValueToIndex.clear();
counter = 0;
}
};
} // end namespace swift
#endif // SWIFT_BASIC_VALUEENUMERATOR_H