LLVM 24.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"
18#include <algorithm>
19#include <cassert>
20#include <cstddef>
21#include <cstdint>
22
23namespace llvm {
24
25class raw_ostream;
26
27/// Utility for building string tables with deduplicated suffixes.
29public:
42
43private:
44 // Only non-zero priority will be recorded.
47 size_t Size = 0;
48 Kind K;
49 Align Alignment;
50 bool Finalized = false;
51
52 void finalizeStringTable(bool Optimize);
53 void initSize();
54
55public:
56 LLVM_ABI StringTableBuilder(Kind K, Align Alignment = Align(1));
58
59 /// Add a string to the builder. Returns the position of S in the table. The
60 /// position will be changed if finalize is used. Can only be used before the
61 /// table is finalized. Priority is only useful with reordering. Strings with
62 /// the same priority will be put together. Strings with higher priority are
63 /// placed closer to the begin of string table. When adding same string with
64 /// different priority, the maximum priority win.
65 size_t add(CachedHashStringRef S, uint8_t Priority = 0) {
66 // Inline: lld calls this for every mergeable string piece.
67 if (K == WinCOFF)
68 assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
69
71 if (Priority)
72 StringPriorityMap[S] = std::max(Priority, StringPriorityMap[S]);
73
74 auto P = StringIndexMap.try_emplace(S);
75 if (P.second) {
76 size_t Start = alignTo(Size, Alignment);
77 P.first->second = Start;
78 Size = Start + S.size() + (K != RAW);
79 }
80 return P.first->second;
81 }
82 size_t add(StringRef S, uint8_t Priority = 0) {
83 return add(CachedHashStringRef(S), Priority);
84 }
85
86 /// Analyze the strings and build the final table. No more strings can
87 /// be added after this point.
88 LLVM_ABI void finalize();
89
90 /// Finalize the string table without reording it. In this mode, offsets
91 /// returned by add will still be valid.
93
94 /// Get the offest of a string in the string table. Can only be used
95 /// after the table is finalized.
97 size_t getOffset(StringRef S) const {
99 }
100
101 /// Check if a string is contained in the string table. Since this class
102 /// doesn't store the string values, this function can be used to check if
103 /// storage needs to be done prior to adding the string.
104 bool contains(StringRef S) const { return contains(CachedHashStringRef(S)); }
105 bool contains(CachedHashStringRef S) const { return StringIndexMap.count(S); }
106
107 bool empty() const { return StringIndexMap.empty(); }
108 size_t getSize() const { return Size; }
109 LLVM_ABI void clear();
110
111 LLVM_ABI void write(raw_ostream &OS) const;
112 LLVM_ABI void write(uint8_t *Buf) const;
113
114 bool isFinalized() const { return Finalized; }
115};
116
117} // end namespace llvm
118
119#endif // LLVM_MC_STRINGTABLEBUILDER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines CachedHashString and CachedHashStringRef.
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
#define P(N)
A container which contains a StringRef plus a precomputed hash.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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.
size_t add(StringRef S, uint8_t Priority=0)
bool contains(CachedHashStringRef S) const
size_t getOffset(StringRef S) const
size_t add(CachedHashStringRef S, uint8_t Priority=0)
Add a string to the builder.
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
@ NameSize
Definition COFF.h:58
This is an optimization pass for GlobalISel generic memory operations.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:149
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39