LLVM 22.0.0git
StringTableBuilder.h
Go to the documentation of this file.
1//===- StringTableBuilder.h - String table building utility -----*- 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#ifndef LLVM_MC_STRINGTABLEBUILDER_H
10#define LLVM_MC_STRINGTABLEBUILDER_H
11
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/StringRef.h"
17#include <cstddef>
18#include <cstdint>
19
20namespace llvm {
21
22class raw_ostream;
23
24/// Utility for building string tables with deduplicated suffixes.
26public:
39
40private:
41 // Only non-zero priority will be recorded.
44 size_t Size = 0;
45 Kind K;
46 Align Alignment;
47 bool Finalized = false;
48
49 void finalizeStringTable(bool Optimize);
50 void initSize();
51
52public:
53 LLVM_ABI StringTableBuilder(Kind K, Align Alignment = Align(1));
55
56 /// Add a string to the builder. Returns the position of S in the table. The
57 /// position will be changed if finalize is used. Can only be used before the
58 /// table is finalized. Priority is only useful with reordering. Strings with
59 /// the same priority will be put together. Strings with higher priority are
60 /// placed closer to the begin of string table. When adding same string with
61 /// different priority, the maximum priority win.
62 LLVM_ABI size_t add(CachedHashStringRef S, uint8_t Priority = 0);
63 size_t add(StringRef S, uint8_t Priority = 0) {
64 return add(CachedHashStringRef(S), Priority);
65 }
66
67 /// Analyze the strings and build the final table. No more strings can
68 /// be added after this point.
69 LLVM_ABI void finalize();
70
71 /// Finalize the string table without reording it. In this mode, offsets
72 /// returned by add will still be valid.
74
75 /// Get the offest of a string in the string table. Can only be used
76 /// after the table is finalized.
78 size_t getOffset(StringRef S) const {
80 }
81
82 /// Check if a string is contained in the string table. Since this class
83 /// doesn't store the string values, this function can be used to check if
84 /// storage needs to be done prior to adding the string.
85 bool contains(StringRef S) const { return contains(CachedHashStringRef(S)); }
86 bool contains(CachedHashStringRef S) const { return StringIndexMap.count(S); }
87
88 bool empty() const { return StringIndexMap.empty(); }
89 size_t getSize() const { return Size; }
90 LLVM_ABI void clear();
91
92 LLVM_ABI void write(raw_ostream &OS) const;
93 LLVM_ABI void write(uint8_t *Buf) const;
94
95 bool isFinalized() const { return Finalized; }
96};
97
98} // end namespace llvm
99
100#endif // LLVM_MC_STRINGTABLEBUILDER_H
This file defines CachedHashString and CachedHashStringRef.
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
A container which contains a StringRef plus a precomputed hash.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM_ABI void finalizeInOrder()
Finalize the string table without reording it.
LLVM_ABI size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
LLVM_ABI ~StringTableBuilder()
bool contains(StringRef S) const
Check if a string is contained in the string table.
LLVM_ABI size_t add(CachedHashStringRef S, uint8_t Priority=0)
Add a string to the builder.
size_t add(StringRef S, uint8_t Priority=0)
bool contains(CachedHashStringRef S) const
size_t getOffset(StringRef S) const
LLVM_ABI void write(raw_ostream &OS) const
LLVM_ABI void finalize()
Analyze the strings and build the final table.
LLVM_ABI StringTableBuilder(Kind K, Align Alignment=Align(1))
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39