LLVM 20.0.0git
LVStringPool.h
Go to the documentation of this file.
1//===-- LVStringPool.h ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the LVStringPool class, which is used to implement a
10// basic string pool table.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
15#define LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
16
17#include "llvm/ADT/StringMap.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/Format.h"
22#include <iomanip>
23#include <vector>
24
25namespace llvm {
26namespace logicalview {
27
29 static constexpr size_t BadIndex = std::numeric_limits<size_t>::max();
31 using ValueType = TableType::value_type;
32 BumpPtrAllocator Allocator;
34 std::vector<ValueType *> Entries;
35
36public:
38 LVStringPool(LVStringPool const &other) = delete;
39 LVStringPool(LVStringPool &&other) = delete;
40 ~LVStringPool() = default;
41
42 bool isValidIndex(size_t Index) const { return Index != BadIndex; }
43
44 // Return number of strings in the pool. The empty string is allocated
45 // at the slot zero. We substract 1 to indicate the number of non empty
46 // strings.
47 size_t getSize() const { return Entries.size() - 1; }
48
49 // Return the index for the specified key, otherwise 'BadIndex'.
50 size_t findIndex(StringRef Key) const {
52 if (Iter != StringTable.end())
53 return Iter->second;
54 return BadIndex;
55 }
56
57 // Return an index for the specified key.
58 size_t getIndex(StringRef Key) {
59 size_t Index = findIndex(Key);
61 return Index;
62 size_t Value = Entries.size();
63 ValueType *Entry = ValueType::create(Key, Allocator, Value);
64 StringTable.insert(Entry);
65 Entries.push_back(Entry);
66 return Value;
67 }
68
69 // Given the index, return its corresponding string.
70 StringRef getString(size_t Index) const {
71 return (Index >= Entries.size()) ? StringRef() : Entries[Index]->getKey();
72 }
73
74 void print(raw_ostream &OS) const {
75 if (!Entries.empty()) {
76 OS << "\nString Pool:\n";
77 for (const ValueType *Entry : Entries)
78 OS << "Index: " << Entry->getValue() << ", "
79 << "Key: '" << Entry->getKey() << "'\n";
80 }
81 }
82
83#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
84 void dump() const { print(dbgs()); }
85#endif
86};
87
88} // namespace logicalview
89} // end namespace llvm
90
91#endif // LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
This file defines the StringMap class.
This file defines the BumpPtrAllocator interface.
uint32_t Index
Basic Register Allocator
raw_pwrite_stream & OS
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
static StringMapEntry * create(StringRef key, AllocatorTy &allocator, InitTy &&...initVals)
Create a StringMapEntry for the specified key construct the value using InitiVals.
StringMapEntry< size_t > value_type
Definition: StringMap.h:213
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
A table of densely packed, null-terminated strings indexed by offset.
Definition: StringTable.h:31
LLVM Value Representation.
Definition: Value.h:74
bool isValidIndex(size_t Index) const
Definition: LVStringPool.h:42
size_t findIndex(StringRef Key) const
Definition: LVStringPool.h:50
void print(raw_ostream &OS) const
Definition: LVStringPool.h:74
LVStringPool(LVStringPool const &other)=delete
LVStringPool(LVStringPool &&other)=delete
StringRef getString(size_t Index) const
Definition: LVStringPool.h:70
size_t getIndex(StringRef Key)
Definition: LVStringPool.h:58
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163