clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DIE.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/CodeGen/AsmPrinter/DIE.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "llvm/CodeGen/DIE.h" |
14 | #include "DwarfCompileUnit.h" |
15 | #include "DwarfDebug.h" |
16 | #include "DwarfUnit.h" |
17 | #include "llvm/ADT/Twine.h" |
18 | #include "llvm/CodeGen/AsmPrinter.h" |
19 | #include "llvm/Config/llvm-config.h" |
20 | #include "llvm/IR/DataLayout.h" |
21 | #include "llvm/MC/MCAsmInfo.h" |
22 | #include "llvm/MC/MCContext.h" |
23 | #include "llvm/MC/MCStreamer.h" |
24 | #include "llvm/MC/MCSymbol.h" |
25 | #include "llvm/Support/Debug.h" |
26 | #include "llvm/Support/ErrorHandling.h" |
27 | #include "llvm/Support/Format.h" |
28 | #include "llvm/Support/FormattedStream.h" |
29 | #include "llvm/Support/LEB128.h" |
30 | #include "llvm/Support/MD5.h" |
31 | #include "llvm/Support/raw_ostream.h" |
32 | using namespace llvm; |
33 | |
34 | #define DEBUG_TYPE "dwarfdebug" |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const { |
43 | |
44 | |
45 | ID.AddInteger(unsigned(Attribute)); |
46 | ID.AddInteger(unsigned(Form)); |
47 | if (Form == dwarf::DW_FORM_implicit_const) |
48 | ID.AddInteger(Value); |
49 | } |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | void DIEAbbrev::Profile(FoldingSetNodeID &ID) const { |
58 | ID.AddInteger(unsigned(Tag)); |
59 | ID.AddInteger(unsigned(Children)); |
60 | |
61 | |
62 | for (unsigned i = 0, N = Data.size(); i < N; ++i) |
63 | Data[i].Profile(ID); |
64 | } |
65 | |
66 | |
67 | |
68 | void DIEAbbrev::Emit(const AsmPrinter *AP) const { |
69 | |
70 | AP->emitULEB128(Tag, dwarf::TagString(Tag).data()); |
71 | |
72 | |
73 | AP->emitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data()); |
74 | |
75 | |
76 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
77 | const DIEAbbrevData &AttrData = Data[i]; |
78 | |
79 | |
80 | AP->emitULEB128(AttrData.getAttribute(), |
81 | dwarf::AttributeString(AttrData.getAttribute()).data()); |
82 | |
83 | |
84 | #ifndef NDEBUG |
85 | |
86 | |
87 | if (!dwarf::isValidFormForVersion(AttrData.getForm(), |
88 | AP->getDwarfVersion())) { |
89 | LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm()) |
90 | << " for DWARF version " << AP->getDwarfVersion() |
91 | << "\n"); |
92 | llvm_unreachable("Invalid form for specified DWARF version"); |
93 | } |
94 | #endif |
95 | AP->emitULEB128(AttrData.getForm(), |
96 | dwarf::FormEncodingString(AttrData.getForm()).data()); |
97 | |
98 | |
99 | if (AttrData.getForm() == dwarf::DW_FORM_implicit_const) |
100 | AP->emitSLEB128(AttrData.getValue()); |
101 | } |
102 | |
103 | |
104 | AP->emitULEB128(0, "EOM(1)"); |
105 | AP->emitULEB128(0, "EOM(2)"); |
106 | } |
107 | |
108 | LLVM_DUMP_METHOD |
109 | void DIEAbbrev::print(raw_ostream &O) const { |
110 | O << "Abbreviation @" |
111 | << format("0x%lx", (long)(intptr_t)this) |
112 | << " " |
113 | << dwarf::TagString(Tag) |
114 | << " " |
115 | << dwarf::ChildrenString(Children) |
116 | << '\n'; |
117 | |
118 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
119 | O << " " |
120 | << dwarf::AttributeString(Data[i].getAttribute()) |
121 | << " " |
122 | << dwarf::FormEncodingString(Data[i].getForm()); |
123 | |
124 | if (Data[i].getForm() == dwarf::DW_FORM_implicit_const) |
125 | O << " " << Data[i].getValue(); |
126 | |
127 | O << '\n'; |
128 | } |
129 | } |
130 | |
131 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
132 | LLVM_DUMP_METHOD void DIEAbbrev::dump() const { |
133 | print(dbgs()); |
134 | } |
135 | #endif |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | DIEAbbrevSet::~DIEAbbrevSet() { |
142 | for (DIEAbbrev *Abbrev : Abbreviations) |
143 | Abbrev->~DIEAbbrev(); |
144 | } |
145 | |
146 | DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) { |
147 | |
148 | FoldingSetNodeID ID; |
149 | DIEAbbrev Abbrev = Die.generateAbbrev(); |
150 | Abbrev.Profile(ID); |
151 | |
152 | void *InsertPos; |
153 | if (DIEAbbrev *Existing = |
| 2 | | Assuming 'Existing' is null | |
|
| |
154 | AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) { |
155 | Die.setAbbrevNumber(Existing->getNumber()); |
156 | return *Existing; |
157 | } |
158 | |
159 | |
160 | DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev)); |
| 4 | | Calling 'operator new<llvm::MallocAllocator, 4096UL, 4096UL, 128UL>' | |
|
161 | Abbreviations.push_back(New); |
162 | New->setNumber(Abbreviations.size()); |
163 | Die.setAbbrevNumber(Abbreviations.size()); |
164 | |
165 | |
166 | AbbreviationsSet.InsertNode(New, InsertPos); |
167 | return *New; |
168 | } |
169 | |
170 | void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const { |
171 | if (!Abbreviations.empty()) { |
172 | |
173 | AP->OutStreamer->SwitchSection(Section); |
174 | AP->emitDwarfAbbrevs(Abbreviations); |
175 | } |
176 | } |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | DIE *DIE::getParent() const { |
183 | return Owner.dyn_cast<DIE*>(); |
184 | } |
185 | |
186 | DIEAbbrev DIE::generateAbbrev() const { |
187 | DIEAbbrev Abbrev(Tag, hasChildren()); |
188 | for (const DIEValue &V : values()) |
189 | if (V.getForm() == dwarf::DW_FORM_implicit_const) |
190 | Abbrev.AddImplicitConstAttribute(V.getAttribute(), |
191 | V.getDIEInteger().getValue()); |
192 | else |
193 | Abbrev.AddAttribute(V.getAttribute(), V.getForm()); |
194 | return Abbrev; |
195 | } |
196 | |
197 | uint64_t DIE::getDebugSectionOffset() const { |
198 | const DIEUnit *Unit = getUnit(); |
199 | assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset"); |
200 | return Unit->getDebugSectionOffset() + getOffset(); |
201 | } |
202 | |
203 | const DIE *DIE::getUnitDie() const { |
204 | const DIE *p = this; |
205 | while (p) { |
206 | if (p->getTag() == dwarf::DW_TAG_compile_unit || |
207 | p->getTag() == dwarf::DW_TAG_type_unit) |
208 | return p; |
209 | p = p->getParent(); |
210 | } |
211 | return nullptr; |
212 | } |
213 | |
214 | DIEUnit *DIE::getUnit() const { |
215 | const DIE *UnitDie = getUnitDie(); |
216 | if (UnitDie) |
217 | return UnitDie->Owner.dyn_cast<DIEUnit*>(); |
218 | return nullptr; |
219 | } |
220 | |
221 | DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const { |
222 | |
223 | |
224 | for (const auto &V : values()) |
225 | if (V.getAttribute() == Attribute) |
226 | return V; |
227 | return DIEValue(); |
228 | } |
229 | |
230 | LLVM_DUMP_METHOD |
231 | static void printValues(raw_ostream &O, const DIEValueList &Values, |
232 | StringRef Type, unsigned Size, unsigned IndentCount) { |
233 | O << Type << ": Size: " << Size << "\n"; |
234 | |
235 | unsigned I = 0; |
236 | const std::string Indent(IndentCount, ' '); |
237 | for (const auto &V : Values.values()) { |
238 | O << Indent; |
239 | O << "Blk[" << I++ << "]"; |
240 | O << " " << dwarf::FormEncodingString(V.getForm()) << " "; |
241 | V.print(O); |
242 | O << "\n"; |
243 | } |
244 | } |
245 | |
246 | LLVM_DUMP_METHOD |
247 | void DIE::print(raw_ostream &O, unsigned IndentCount) const { |
248 | const std::string Indent(IndentCount, ' '); |
249 | O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this) |
250 | << ", Offset: " << Offset << ", Size: " << Size << "\n"; |
251 | |
252 | O << Indent << dwarf::TagString(getTag()) << " " |
253 | << dwarf::ChildrenString(hasChildren()) << "\n"; |
254 | |
255 | IndentCount += 2; |
256 | for (const auto &V : values()) { |
257 | O << Indent; |
258 | O << dwarf::AttributeString(V.getAttribute()); |
259 | O << " " << dwarf::FormEncodingString(V.getForm()) << " "; |
260 | V.print(O); |
261 | O << "\n"; |
262 | } |
263 | IndentCount -= 2; |
264 | |
265 | for (const auto &Child : children()) |
266 | Child.print(O, IndentCount + 4); |
267 | |
268 | O << "\n"; |
269 | } |
270 | |
271 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
272 | LLVM_DUMP_METHOD void DIE::dump() const { |
273 | print(dbgs()); |
274 | } |
275 | #endif |
276 | |
277 | unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter *AP, |
278 | DIEAbbrevSet &AbbrevSet, |
279 | unsigned CUOffset) { |
280 | |
281 | |
282 | const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this); |
| 1 | Calling 'DIEAbbrevSet::uniqueAbbreviation' | |
|
283 | |
284 | |
285 | setOffset(CUOffset); |
286 | |
287 | |
288 | CUOffset += getULEB128Size(getAbbrevNumber()); |
289 | |
290 | |
291 | for (const auto &V : values()) |
292 | CUOffset += V.SizeOf(AP); |
293 | |
294 | |
295 | if (hasChildren()) { |
296 | (void)Abbrev; |
297 | assert(Abbrev.hasChildren() && "Children flag not set"); |
298 | |
299 | for (auto &Child : children()) |
300 | CUOffset = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset); |
301 | |
302 | |
303 | CUOffset += sizeof(int8_t); |
304 | } |
305 | |
306 | |
307 | |
308 | |
309 | setSize(CUOffset - getOffset()); |
310 | return CUOffset; |
311 | } |
312 | |
313 | |
314 | |
315 | |
316 | DIEUnit::DIEUnit(dwarf::Tag UnitTag) |
317 | : Die(UnitTag), Section(nullptr), Offset(0) { |
318 | Die.Owner = this; |
319 | assert((UnitTag == dwarf::DW_TAG_compile_unit || |
320 | UnitTag == dwarf::DW_TAG_skeleton_unit || |
321 | UnitTag == dwarf::DW_TAG_type_unit || |
322 | UnitTag == dwarf::DW_TAG_partial_unit) && |
323 | "expected a unit TAG"); |
324 | } |
325 | |
326 | void DIEValue::emitValue(const AsmPrinter *AP) const { |
327 | switch (Ty) { |
328 | case isNone: |
329 | llvm_unreachable("Expected valid DIEValue"); |
330 | #define HANDLE_DIEVALUE(T) \ |
331 | case is##T: \ |
332 | getDIE##T().emitValue(AP, Form); \ |
333 | break; |
334 | #include "llvm/CodeGen/DIEValue.def" |
335 | } |
336 | } |
337 | |
338 | unsigned DIEValue::SizeOf(const AsmPrinter *AP) const { |
339 | switch (Ty) { |
340 | case isNone: |
341 | llvm_unreachable("Expected valid DIEValue"); |
342 | #define HANDLE_DIEVALUE(T) \ |
343 | case is##T: \ |
344 | return getDIE##T().SizeOf(AP, Form); |
345 | #include "llvm/CodeGen/DIEValue.def" |
346 | } |
347 | llvm_unreachable("Unknown DIE kind"); |
348 | } |
349 | |
350 | LLVM_DUMP_METHOD |
351 | void DIEValue::print(raw_ostream &O) const { |
352 | switch (Ty) { |
353 | case isNone: |
354 | llvm_unreachable("Expected valid DIEValue"); |
355 | #define HANDLE_DIEVALUE(T) \ |
356 | case is##T: \ |
357 | getDIE##T().print(O); \ |
358 | break; |
359 | #include "llvm/CodeGen/DIEValue.def" |
360 | } |
361 | } |
362 | |
363 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
364 | LLVM_DUMP_METHOD void DIEValue::dump() const { |
365 | print(dbgs()); |
366 | } |
367 | #endif |
368 | |
369 | |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | void DIEInteger::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const { |
376 | switch (Form) { |
377 | case dwarf::DW_FORM_implicit_const: |
378 | case dwarf::DW_FORM_flag_present: |
379 | |
380 | |
381 | Asm->OutStreamer->AddBlankLine(); |
382 | return; |
383 | case dwarf::DW_FORM_flag: |
384 | case dwarf::DW_FORM_ref1: |
385 | case dwarf::DW_FORM_data1: |
386 | case dwarf::DW_FORM_strx1: |
387 | case dwarf::DW_FORM_addrx1: |
388 | case dwarf::DW_FORM_ref2: |
389 | case dwarf::DW_FORM_data2: |
390 | case dwarf::DW_FORM_strx2: |
391 | case dwarf::DW_FORM_addrx2: |
392 | case dwarf::DW_FORM_strx3: |
393 | case dwarf::DW_FORM_strp: |
394 | case dwarf::DW_FORM_ref4: |
395 | case dwarf::DW_FORM_data4: |
396 | case dwarf::DW_FORM_ref_sup4: |
397 | case dwarf::DW_FORM_strx4: |
398 | case dwarf::DW_FORM_addrx4: |
399 | case dwarf::DW_FORM_ref8: |
400 | case dwarf::DW_FORM_ref_sig8: |
401 | case dwarf::DW_FORM_data8: |
402 | case dwarf::DW_FORM_ref_sup8: |
403 | case dwarf::DW_FORM_GNU_ref_alt: |
404 | case dwarf::DW_FORM_GNU_strp_alt: |
405 | case dwarf::DW_FORM_line_strp: |
406 | case dwarf::DW_FORM_sec_offset: |
407 | case dwarf::DW_FORM_strp_sup: |
408 | case dwarf::DW_FORM_addr: |
409 | case dwarf::DW_FORM_ref_addr: |
410 | Asm->OutStreamer->emitIntValue(Integer, SizeOf(Asm, Form)); |
411 | return; |
412 | case dwarf::DW_FORM_GNU_str_index: |
413 | case dwarf::DW_FORM_GNU_addr_index: |
414 | case dwarf::DW_FORM_ref_udata: |
415 | case dwarf::DW_FORM_strx: |
416 | case dwarf::DW_FORM_addrx: |
417 | case dwarf::DW_FORM_rnglistx: |
418 | case dwarf::DW_FORM_udata: |
419 | Asm->emitULEB128(Integer); |
420 | return; |
421 | case dwarf::DW_FORM_sdata: |
422 | Asm->emitSLEB128(Integer); |
423 | return; |
424 | default: llvm_unreachable("DIE Value form not supported yet"); |
425 | } |
426 | } |
427 | |
428 | |
429 | |
430 | unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
431 | assert(AP && "AsmPrinter is required to set FormParams"); |
432 | dwarf::FormParams Params = {AP->getDwarfVersion(), |
433 | uint8_t(AP->getPointerSize()), |
434 | AP->OutStreamer->getContext().getDwarfFormat()}; |
435 | |
436 | if (Optional<uint8_t> FixedSize = dwarf::getFixedFormByteSize(Form, Params)) |
437 | return *FixedSize; |
438 | |
439 | switch (Form) { |
440 | case dwarf::DW_FORM_GNU_str_index: |
441 | case dwarf::DW_FORM_GNU_addr_index: |
442 | case dwarf::DW_FORM_ref_udata: |
443 | case dwarf::DW_FORM_strx: |
444 | case dwarf::DW_FORM_addrx: |
445 | case dwarf::DW_FORM_rnglistx: |
446 | case dwarf::DW_FORM_udata: |
447 | return getULEB128Size(Integer); |
448 | case dwarf::DW_FORM_sdata: |
449 | return getSLEB128Size(Integer); |
450 | default: llvm_unreachable("DIE Value form not supported yet"); |
451 | } |
452 | } |
453 | |
454 | LLVM_DUMP_METHOD |
455 | void DIEInteger::print(raw_ostream &O) const { |
456 | O << "Int: " << (int64_t)Integer << " 0x"; |
457 | O.write_hex(Integer); |
458 | } |
459 | |
460 | |
461 | |
462 | |
463 | |
464 | |
465 | |
466 | void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
467 | AP->emitDebugValue(Expr, SizeOf(AP, Form)); |
468 | } |
469 | |
470 | |
471 | |
472 | unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
473 | switch (Form) { |
474 | case dwarf::DW_FORM_data4: |
475 | return 4; |
476 | case dwarf::DW_FORM_data8: |
477 | return 8; |
478 | case dwarf::DW_FORM_sec_offset: |
479 | return AP->getDwarfOffsetByteSize(); |
480 | default: |
481 | llvm_unreachable("DIE Value form not supported yet"); |
482 | } |
483 | } |
484 | |
485 | LLVM_DUMP_METHOD |
486 | void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; } |
487 | |
488 | |
489 | |
490 | |
491 | |
492 | |
493 | |
494 | void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
495 | bool IsSectionRelative = Form != dwarf::DW_FORM_addr; |
496 | AP->emitLabelReference(Label, SizeOf(AP, Form), IsSectionRelative); |
497 | } |
498 | |
499 | |
500 | |
501 | unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
502 | switch (Form) { |
503 | case dwarf::DW_FORM_data4: |
504 | return 4; |
505 | case dwarf::DW_FORM_data8: |
506 | return 8; |
507 | case dwarf::DW_FORM_sec_offset: |
508 | case dwarf::DW_FORM_strp: |
509 | return AP->getDwarfOffsetByteSize(); |
510 | case dwarf::DW_FORM_addr: |
511 | return AP->MAI->getCodePointerSize(); |
512 | default: |
513 | llvm_unreachable("DIE Value form not supported yet"); |
514 | } |
515 | } |
516 | |
517 | LLVM_DUMP_METHOD |
518 | void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); } |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
525 | uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset(); |
526 | assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit"); |
527 | AP->emitULEB128(Offset, nullptr, ULEB128PadSize); |
528 | } |
529 | |
530 | unsigned DIEBaseTypeRef::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
531 | return ULEB128PadSize; |
532 | } |
533 | |
534 | LLVM_DUMP_METHOD |
535 | void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; } |
536 | |
537 | |
538 | |
539 | |
540 | |
541 | |
542 | |
543 | void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
544 | AP->emitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form)); |
545 | } |
546 | |
547 | |
548 | |
549 | unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
550 | switch (Form) { |
551 | case dwarf::DW_FORM_data4: |
552 | return 4; |
553 | case dwarf::DW_FORM_data8: |
554 | return 8; |
555 | case dwarf::DW_FORM_sec_offset: |
556 | return AP->getDwarfOffsetByteSize(); |
557 | default: |
558 | llvm_unreachable("DIE Value form not supported yet"); |
559 | } |
560 | } |
561 | |
562 | LLVM_DUMP_METHOD |
563 | void DIEDelta::print(raw_ostream &O) const { |
564 | O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName(); |
565 | } |
566 | |
567 | |
568 | |
569 | |
570 | |
571 | |
572 | |
573 | void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
574 | |
575 | switch (Form) { |
576 | case dwarf::DW_FORM_GNU_str_index: |
577 | case dwarf::DW_FORM_strx: |
578 | case dwarf::DW_FORM_strx1: |
579 | case dwarf::DW_FORM_strx2: |
580 | case dwarf::DW_FORM_strx3: |
581 | case dwarf::DW_FORM_strx4: |
582 | DIEInteger(S.getIndex()).emitValue(AP, Form); |
583 | return; |
584 | case dwarf::DW_FORM_strp: |
585 | if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) |
586 | DIELabel(S.getSymbol()).emitValue(AP, Form); |
587 | else |
588 | DIEInteger(S.getOffset()).emitValue(AP, Form); |
589 | return; |
590 | default: |
591 | llvm_unreachable("Expected valid string form"); |
592 | } |
593 | } |
594 | |
595 | |
596 | |
597 | unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
598 | |
599 | switch (Form) { |
600 | case dwarf::DW_FORM_GNU_str_index: |
601 | case dwarf::DW_FORM_strx: |
602 | case dwarf::DW_FORM_strx1: |
603 | case dwarf::DW_FORM_strx2: |
604 | case dwarf::DW_FORM_strx3: |
605 | case dwarf::DW_FORM_strx4: |
606 | return DIEInteger(S.getIndex()).SizeOf(AP, Form); |
607 | case dwarf::DW_FORM_strp: |
608 | if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) |
609 | return DIELabel(S.getSymbol()).SizeOf(AP, Form); |
610 | return DIEInteger(S.getOffset()).SizeOf(AP, Form); |
611 | default: |
612 | llvm_unreachable("Expected valid string form"); |
613 | } |
614 | } |
615 | |
616 | LLVM_DUMP_METHOD |
617 | void DIEString::print(raw_ostream &O) const { |
618 | O << "String: " << S.getString(); |
619 | } |
620 | |
621 | |
622 | |
623 | |
624 | void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
625 | if (Form == dwarf::DW_FORM_string) { |
626 | AP->OutStreamer->emitBytes(S); |
627 | AP->emitInt8(0); |
628 | return; |
629 | } |
630 | llvm_unreachable("Expected valid string form"); |
631 | } |
632 | |
633 | unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
634 | |
635 | return S.size() + 1; |
636 | } |
637 | |
638 | LLVM_DUMP_METHOD |
639 | void DIEInlineString::print(raw_ostream &O) const { |
640 | O << "InlineString: " << S; |
641 | } |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
650 | |
651 | switch (Form) { |
652 | case dwarf::DW_FORM_ref1: |
653 | case dwarf::DW_FORM_ref2: |
654 | case dwarf::DW_FORM_ref4: |
655 | case dwarf::DW_FORM_ref8: |
656 | AP->OutStreamer->emitIntValue(Entry->getOffset(), SizeOf(AP, Form)); |
657 | return; |
658 | |
659 | case dwarf::DW_FORM_ref_udata: |
660 | AP->emitULEB128(Entry->getOffset()); |
661 | return; |
662 | |
663 | case dwarf::DW_FORM_ref_addr: { |
664 | |
665 | uint64_t Addr = Entry->getDebugSectionOffset(); |
666 | if (const MCSymbol *SectionSym = |
667 | Entry->getUnit()->getCrossSectionRelativeBaseAddress()) { |
668 | AP->emitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true); |
669 | return; |
670 | } |
671 | |
672 | AP->OutStreamer->emitIntValue(Addr, SizeOf(AP, Form)); |
673 | return; |
674 | } |
675 | default: |
676 | llvm_unreachable("Improper form for DIE reference"); |
677 | } |
678 | } |
679 | |
680 | unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
681 | switch (Form) { |
682 | case dwarf::DW_FORM_ref1: |
683 | return 1; |
684 | case dwarf::DW_FORM_ref2: |
685 | return 2; |
686 | case dwarf::DW_FORM_ref4: |
687 | return 4; |
688 | case dwarf::DW_FORM_ref8: |
689 | return 8; |
690 | case dwarf::DW_FORM_ref_udata: |
691 | return getULEB128Size(Entry->getOffset()); |
692 | case dwarf::DW_FORM_ref_addr: |
693 | if (AP->getDwarfVersion() == 2) |
694 | return AP->MAI->getCodePointerSize(); |
695 | switch (AP->OutStreamer->getContext().getDwarfFormat()) { |
696 | case dwarf::DWARF32: |
697 | return 4; |
698 | case dwarf::DWARF64: |
699 | return 8; |
700 | } |
701 | llvm_unreachable("Invalid DWARF format"); |
702 | |
703 | default: |
704 | llvm_unreachable("Improper form for DIE reference"); |
705 | } |
706 | } |
707 | |
708 | LLVM_DUMP_METHOD |
709 | void DIEEntry::print(raw_ostream &O) const { |
710 | O << format("Die: 0x%lx", (long)(intptr_t)&Entry); |
711 | } |
712 | |
713 | |
714 | |
715 | |
716 | |
717 | |
718 | |
719 | unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const { |
720 | if (!Size) { |
721 | for (const auto &V : values()) |
722 | Size += V.SizeOf(AP); |
723 | } |
724 | |
725 | return Size; |
726 | } |
727 | |
728 | |
729 | |
730 | void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const { |
731 | switch (Form) { |
732 | default: llvm_unreachable("Improper form for block"); |
733 | case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break; |
734 | case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break; |
735 | case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break; |
736 | case dwarf::DW_FORM_block: |
737 | case dwarf::DW_FORM_exprloc: |
738 | Asm->emitULEB128(Size); |
739 | break; |
740 | } |
741 | |
742 | for (const auto &V : values()) |
743 | V.emitValue(Asm); |
744 | } |
745 | |
746 | |
747 | |
748 | unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
749 | switch (Form) { |
750 | case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); |
751 | case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); |
752 | case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); |
753 | case dwarf::DW_FORM_block: |
754 | case dwarf::DW_FORM_exprloc: |
755 | return Size + getULEB128Size(Size); |
756 | default: llvm_unreachable("Improper form for block"); |
757 | } |
758 | } |
759 | |
760 | LLVM_DUMP_METHOD |
761 | void DIELoc::print(raw_ostream &O) const { |
762 | printValues(O, *this, "ExprLoc", Size, 5); |
763 | } |
764 | |
765 | |
766 | |
767 | |
768 | |
769 | |
770 | |
771 | unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const { |
772 | if (!Size) { |
773 | for (const auto &V : values()) |
774 | Size += V.SizeOf(AP); |
775 | } |
776 | |
777 | return Size; |
778 | } |
779 | |
780 | |
781 | |
782 | void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const { |
783 | switch (Form) { |
784 | default: llvm_unreachable("Improper form for block"); |
785 | case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break; |
786 | case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break; |
787 | case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break; |
788 | case dwarf::DW_FORM_exprloc: |
789 | case dwarf::DW_FORM_block: |
790 | Asm->emitULEB128(Size); |
791 | break; |
792 | case dwarf::DW_FORM_string: break; |
793 | case dwarf::DW_FORM_data16: break; |
794 | } |
795 | |
796 | for (const auto &V : values()) |
797 | V.emitValue(Asm); |
798 | } |
799 | |
800 | |
801 | |
802 | unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
803 | switch (Form) { |
804 | case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); |
805 | case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); |
806 | case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); |
807 | case dwarf::DW_FORM_exprloc: |
808 | case dwarf::DW_FORM_block: return Size + getULEB128Size(Size); |
809 | case dwarf::DW_FORM_data16: return 16; |
810 | default: llvm_unreachable("Improper form for block"); |
811 | } |
812 | } |
813 | |
814 | LLVM_DUMP_METHOD |
815 | void DIEBlock::print(raw_ostream &O) const { |
816 | printValues(O, *this, "Blk", Size, 5); |
817 | } |
818 | |
819 | |
820 | |
821 | |
822 | |
823 | unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
824 | switch (Form) { |
825 | case dwarf::DW_FORM_loclistx: |
826 | return getULEB128Size(Index); |
827 | case dwarf::DW_FORM_data4: |
828 | assert(!AP->isDwarf64() && |
829 | "DW_FORM_data4 is not suitable to emit a pointer to a location list " |
830 | "in the 64-bit DWARF format"); |
831 | return 4; |
832 | case dwarf::DW_FORM_data8: |
833 | assert(AP->isDwarf64() && |
834 | "DW_FORM_data8 is not suitable to emit a pointer to a location list " |
835 | "in the 32-bit DWARF format"); |
836 | return 8; |
837 | case dwarf::DW_FORM_sec_offset: |
838 | return AP->getDwarfOffsetByteSize(); |
839 | default: |
840 | llvm_unreachable("DIE Value form not supported yet"); |
841 | } |
842 | } |
843 | |
844 | |
845 | |
846 | void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
847 | if (Form == dwarf::DW_FORM_loclistx) { |
848 | AP->emitULEB128(Index); |
849 | return; |
850 | } |
851 | DwarfDebug *DD = AP->getDwarfDebug(); |
852 | MCSymbol *Label = DD->getDebugLocs().getList(Index).Label; |
853 | AP->emitDwarfSymbolReference(Label, DD->useSplitDwarf()); |
854 | } |
855 | |
856 | LLVM_DUMP_METHOD |
857 | void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; } |
858 | |
859 | |
860 | |
861 | |
862 | |
863 | unsigned DIEAddrOffset::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { |
864 | return Addr.SizeOf(AP, dwarf::DW_FORM_addrx) + |
865 | Offset.SizeOf(AP, dwarf::DW_FORM_data4); |
866 | } |
867 | |
868 | |
869 | |
870 | void DIEAddrOffset::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
871 | Addr.emitValue(AP, dwarf::DW_FORM_addrx); |
872 | Offset.emitValue(AP, dwarf::DW_FORM_data4); |
873 | } |
874 | |
875 | LLVM_DUMP_METHOD |
876 | void DIEAddrOffset::print(raw_ostream &O) const { |
877 | O << "AddrOffset: "; |
878 | Addr.print(O); |
879 | O << " + "; |
880 | Offset.print(O); |
881 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | #ifndef LLVM_SUPPORT_ALLOCATOR_H |
18 | #define LLVM_SUPPORT_ALLOCATOR_H |
19 | |
20 | #include "llvm/ADT/Optional.h" |
21 | #include "llvm/ADT/SmallVector.h" |
22 | #include "llvm/Support/Alignment.h" |
23 | #include "llvm/Support/AllocatorBase.h" |
24 | #include "llvm/Support/Compiler.h" |
25 | #include "llvm/Support/ErrorHandling.h" |
26 | #include "llvm/Support/MathExtras.h" |
27 | #include "llvm/Support/MemAlloc.h" |
28 | #include <algorithm> |
29 | #include <cassert> |
30 | #include <cstddef> |
31 | #include <cstdint> |
32 | #include <cstdlib> |
33 | #include <iterator> |
34 | #include <type_traits> |
35 | #include <utility> |
36 | |
37 | namespace llvm { |
38 | |
39 | namespace detail { |
40 | |
41 | |
42 | |
43 | void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated, |
44 | size_t TotalMemory); |
45 | |
46 | } |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096, |
66 | size_t SizeThreshold = SlabSize, size_t GrowthDelay = 128> |
67 | class BumpPtrAllocatorImpl |
68 | : public AllocatorBase<BumpPtrAllocatorImpl<AllocatorT, SlabSize, |
69 | SizeThreshold, GrowthDelay>>, |
70 | private AllocatorT { |
71 | public: |
72 | static_assert(SizeThreshold <= SlabSize, |
73 | "The SizeThreshold must be at most the SlabSize to ensure " |
74 | "that objects larger than a slab go into their own memory " |
75 | "allocation."); |
76 | static_assert(GrowthDelay > 0, |
77 | "GrowthDelay must be at least 1 which already increases the" |
78 | "slab size after each allocated slab."); |
79 | |
80 | BumpPtrAllocatorImpl() = default; |
81 | |
82 | template <typename T> |
83 | BumpPtrAllocatorImpl(T &&Allocator) |
84 | : AllocatorT(std::forward<T &&>(Allocator)) {} |
85 | |
86 | |
87 | |
88 | BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old) |
89 | : AllocatorT(static_cast<AllocatorT &&>(Old)), CurPtr(Old.CurPtr), |
90 | End(Old.End), Slabs(std::move(Old.Slabs)), |
91 | CustomSizedSlabs(std::move(Old.CustomSizedSlabs)), |
92 | BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize) { |
93 | Old.CurPtr = Old.End = nullptr; |
94 | Old.BytesAllocated = 0; |
95 | Old.Slabs.clear(); |
96 | Old.CustomSizedSlabs.clear(); |
97 | } |
98 | |
99 | ~BumpPtrAllocatorImpl() { |
100 | DeallocateSlabs(Slabs.begin(), Slabs.end()); |
101 | DeallocateCustomSizedSlabs(); |
102 | } |
103 | |
104 | BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) { |
105 | DeallocateSlabs(Slabs.begin(), Slabs.end()); |
106 | DeallocateCustomSizedSlabs(); |
107 | |
108 | CurPtr = RHS.CurPtr; |
109 | End = RHS.End; |
110 | BytesAllocated = RHS.BytesAllocated; |
111 | RedZoneSize = RHS.RedZoneSize; |
112 | Slabs = std::move(RHS.Slabs); |
113 | CustomSizedSlabs = std::move(RHS.CustomSizedSlabs); |
114 | AllocatorT::operator=(static_cast<AllocatorT &&>(RHS)); |
115 | |
116 | RHS.CurPtr = RHS.End = nullptr; |
117 | RHS.BytesAllocated = 0; |
118 | RHS.Slabs.clear(); |
119 | RHS.CustomSizedSlabs.clear(); |
120 | return *this; |
121 | } |
122 | |
123 | |
124 | |
125 | void Reset() { |
126 | |
127 | DeallocateCustomSizedSlabs(); |
128 | CustomSizedSlabs.clear(); |
129 | |
130 | if (Slabs.empty()) |
131 | return; |
132 | |
133 | |
134 | BytesAllocated = 0; |
135 | CurPtr = (char *)Slabs.front(); |
136 | End = CurPtr + SlabSize; |
137 | |
138 | __asan_poison_memory_region(*Slabs.begin(), computeSlabSize(0)); |
139 | DeallocateSlabs(std::next(Slabs.begin()), Slabs.end()); |
140 | Slabs.erase(std::next(Slabs.begin()), Slabs.end()); |
141 | } |
142 | |
143 | |
144 | LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * |
145 | Allocate(size_t Size, Align Alignment) { |
146 | |
147 | BytesAllocated += Size; |
148 | |
149 | size_t Adjustment = offsetToAlignedAddr(CurPtr, Alignment); |
| 7 | | Calling 'offsetToAlignedAddr' | |
|
150 | assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow"); |
151 | |
152 | size_t SizeToAllocate = Size; |
153 | #if LLVM_ADDRESS_SANITIZER_BUILD |
154 | |
155 | SizeToAllocate += RedZoneSize; |
156 | #endif |
157 | |
158 | |
159 | if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)) { |
160 | char *AlignedPtr = CurPtr + Adjustment; |
161 | CurPtr = AlignedPtr + SizeToAllocate; |
162 | |
163 | |
164 | |
165 | __msan_allocated_memory(AlignedPtr, Size); |
166 | |
167 | __asan_unpoison_memory_region(AlignedPtr, Size); |
168 | return AlignedPtr; |
169 | } |
170 | |
171 | |
172 | size_t PaddedSize = SizeToAllocate + Alignment.value() - 1; |
173 | if (PaddedSize > SizeThreshold) { |
174 | void *NewSlab = |
175 | AllocatorT::Allocate(PaddedSize, alignof(std::max_align_t)); |
176 | |
177 | |
178 | __asan_poison_memory_region(NewSlab, PaddedSize); |
179 | CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize)); |
180 | |
181 | uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment); |
182 | assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize); |
183 | char *AlignedPtr = (char*)AlignedAddr; |
184 | __msan_allocated_memory(AlignedPtr, Size); |
185 | __asan_unpoison_memory_region(AlignedPtr, Size); |
186 | return AlignedPtr; |
187 | } |
188 | |
189 | |
190 | StartNewSlab(); |
191 | uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment); |
192 | assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End && |
193 | "Unable to allocate memory!"); |
194 | char *AlignedPtr = (char*)AlignedAddr; |
195 | CurPtr = AlignedPtr + SizeToAllocate; |
196 | __msan_allocated_memory(AlignedPtr, Size); |
197 | __asan_unpoison_memory_region(AlignedPtr, Size); |
198 | return AlignedPtr; |
199 | } |
200 | |
201 | inline LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * |
202 | Allocate(size_t Size, size_t Alignment) { |
203 | assert(Alignment > 0 && "0-byte alignment is not allowed. Use 1 instead."); |
204 | return Allocate(Size, Align(Alignment)); |
| 6 | | Calling 'BumpPtrAllocatorImpl::Allocate' | |
|
205 | } |
206 | |
207 | |
208 | using AllocatorBase<BumpPtrAllocatorImpl>::Allocate; |
209 | |
210 | |
211 | |
212 | |
213 | void Deallocate(const void *Ptr, size_t Size, size_t ) { |
214 | __asan_poison_memory_region(Ptr, Size); |
215 | } |
216 | |
217 | |
218 | using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate; |
219 | |
220 | size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); } |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | llvm::Optional<int64_t> identifyObject(const void *Ptr) { |
228 | const char *P = static_cast<const char *>(Ptr); |
229 | int64_t InSlabIdx = 0; |
230 | for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) { |
231 | const char *S = static_cast<const char *>(Slabs[Idx]); |
232 | if (P >= S && P < S + computeSlabSize(Idx)) |
233 | return InSlabIdx + static_cast<int64_t>(P - S); |
234 | InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx)); |
235 | } |
236 | |
237 | |
238 | int64_t InCustomSizedSlabIdx = -1; |
239 | for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) { |
240 | const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first); |
241 | size_t Size = CustomSizedSlabs[Idx].second; |
242 | if (P >= S && P < S + Size) |
243 | return InCustomSizedSlabIdx - static_cast<int64_t>(P - S); |
244 | InCustomSizedSlabIdx -= static_cast<int64_t>(Size); |
245 | } |
246 | return None; |
247 | } |
248 | |
249 | |
250 | |
251 | |
252 | |
253 | int64_t identifyKnownObject(const void *Ptr) { |
254 | Optional<int64_t> Out = identifyObject(Ptr); |
255 | assert(Out && "Wrong allocator used"); |
256 | return *Out; |
257 | } |
258 | |
259 | |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | template <typename T> |
270 | int64_t identifyKnownAlignedObject(const void *Ptr) { |
271 | int64_t Out = identifyKnownObject(Ptr); |
272 | assert(Out % alignof(T) == 0 && "Wrong alignment information"); |
273 | return Out / alignof(T); |
274 | } |
275 | |
276 | size_t getTotalMemory() const { |
277 | size_t TotalMemory = 0; |
278 | for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I) |
279 | TotalMemory += computeSlabSize(std::distance(Slabs.begin(), I)); |
280 | for (auto &PtrAndSize : CustomSizedSlabs) |
281 | TotalMemory += PtrAndSize.second; |
282 | return TotalMemory; |
283 | } |
284 | |
285 | size_t getBytesAllocated() const { return BytesAllocated; } |
286 | |
287 | void setRedZoneSize(size_t NewSize) { |
288 | RedZoneSize = NewSize; |
289 | } |
290 | |
291 | void PrintStats() const { |
292 | detail::printBumpPtrAllocatorStats(Slabs.size(), BytesAllocated, |
293 | getTotalMemory()); |
294 | } |
295 | |
296 | private: |
297 | |
298 | |
299 | |
300 | char *CurPtr = nullptr; |
301 | |
302 | |
303 | char *End = nullptr; |
304 | |
305 | |
306 | SmallVector<void *, 4> Slabs; |
307 | |
308 | |
309 | SmallVector<std::pair<void *, size_t>, 0> CustomSizedSlabs; |
310 | |
311 | |
312 | |
313 | |
314 | size_t BytesAllocated = 0; |
315 | |
316 | |
317 | |
318 | size_t RedZoneSize = 1; |
319 | |
320 | static size_t computeSlabSize(unsigned SlabIdx) { |
321 | |
322 | |
323 | |
324 | |
325 | return SlabSize * |
326 | ((size_t)1 << std::min<size_t>(30, SlabIdx / GrowthDelay)); |
327 | } |
328 | |
329 | |
330 | |
331 | void StartNewSlab() { |
332 | size_t AllocatedSlabSize = computeSlabSize(Slabs.size()); |
333 | |
334 | void *NewSlab = |
335 | AllocatorT::Allocate(AllocatedSlabSize, alignof(std::max_align_t)); |
336 | |
337 | |
338 | __asan_poison_memory_region(NewSlab, AllocatedSlabSize); |
339 | |
340 | Slabs.push_back(NewSlab); |
341 | CurPtr = (char *)(NewSlab); |
342 | End = ((char *)NewSlab) + AllocatedSlabSize; |
343 | } |
344 | |
345 | |
346 | void DeallocateSlabs(SmallVectorImpl<void *>::iterator I, |
347 | SmallVectorImpl<void *>::iterator E) { |
348 | for (; I != E; ++I) { |
349 | size_t AllocatedSlabSize = |
350 | computeSlabSize(std::distance(Slabs.begin(), I)); |
351 | AllocatorT::Deallocate(*I, AllocatedSlabSize, alignof(std::max_align_t)); |
352 | } |
353 | } |
354 | |
355 | |
356 | void DeallocateCustomSizedSlabs() { |
357 | for (auto &PtrAndSize : CustomSizedSlabs) { |
358 | void *Ptr = PtrAndSize.first; |
359 | size_t Size = PtrAndSize.second; |
360 | AllocatorT::Deallocate(Ptr, Size, alignof(std::max_align_t)); |
361 | } |
362 | } |
363 | |
364 | template <typename T> friend class SpecificBumpPtrAllocator; |
365 | }; |
366 | |
367 | |
368 | |
369 | typedef BumpPtrAllocatorImpl<> BumpPtrAllocator; |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | template <typename T> class SpecificBumpPtrAllocator { |
377 | BumpPtrAllocator Allocator; |
378 | |
379 | public: |
380 | SpecificBumpPtrAllocator() { |
381 | |
382 | |
383 | Allocator.setRedZoneSize(0); |
384 | } |
385 | SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old) |
386 | : Allocator(std::move(Old.Allocator)) {} |
387 | ~SpecificBumpPtrAllocator() { DestroyAll(); } |
388 | |
389 | SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) { |
390 | Allocator = std::move(RHS.Allocator); |
391 | return *this; |
392 | } |
393 | |
394 | |
395 | |
396 | |
397 | void DestroyAll() { |
398 | auto DestroyElements = [](char *Begin, char *End) { |
399 | assert(Begin == (char *)alignAddr(Begin, Align::Of<T>())); |
400 | for (char *Ptr = Begin; Ptr + sizeof(T) <= End; Ptr += sizeof(T)) |
401 | reinterpret_cast<T *>(Ptr)->~T(); |
402 | }; |
403 | |
404 | for (auto I = Allocator.Slabs.begin(), E = Allocator.Slabs.end(); I != E; |
405 | ++I) { |
406 | size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize( |
407 | std::distance(Allocator.Slabs.begin(), I)); |
408 | char *Begin = (char *)alignAddr(*I, Align::Of<T>()); |
409 | char *End = *I == Allocator.Slabs.back() ? Allocator.CurPtr |
410 | : (char *)*I + AllocatedSlabSize; |
411 | |
412 | DestroyElements(Begin, End); |
413 | } |
414 | |
415 | for (auto &PtrAndSize : Allocator.CustomSizedSlabs) { |
416 | void *Ptr = PtrAndSize.first; |
417 | size_t Size = PtrAndSize.second; |
418 | DestroyElements((char *)alignAddr(Ptr, Align::Of<T>()), |
419 | (char *)Ptr + Size); |
420 | } |
421 | |
422 | Allocator.Reset(); |
423 | } |
424 | |
425 | |
426 | T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); } |
427 | }; |
428 | |
429 | } |
430 | |
431 | template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, |
432 | size_t GrowthDelay> |
433 | void * |
434 | operator new(size_t Size, |
435 | llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold, |
436 | GrowthDelay> &Allocator) { |
437 | return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size), |
| 5 | | Calling 'BumpPtrAllocatorImpl::Allocate' | |
|
438 | alignof(std::max_align_t))); |
439 | } |
440 | |
441 | template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, |
442 | size_t GrowthDelay> |
443 | void operator delete(void *, |
444 | llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, |
445 | SizeThreshold, GrowthDelay> &) { |
446 | } |
447 | |
448 | #endif // LLVM_SUPPORT_ALLOCATOR_H |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | #ifndef LLVM_SUPPORT_ALIGNMENT_H_ |
22 | #define LLVM_SUPPORT_ALIGNMENT_H_ |
23 | |
24 | #include "llvm/ADT/Optional.h" |
25 | #include "llvm/Support/MathExtras.h" |
26 | #include <cassert> |
27 | #ifndef NDEBUG |
28 | #include <string> |
29 | #endif // NDEBUG |
30 | |
31 | namespace llvm { |
32 | |
33 | #define ALIGN_CHECK_ISPOSITIVE(decl) \ |
34 | assert(decl > 0 && (#decl " should be defined")) |
35 | |
36 | |
37 | |
38 | |
39 | struct Align { |
40 | private: |
41 | uint8_t ShiftValue = 0; |
42 | |
43 | |
44 | friend struct MaybeAlign; |
45 | friend unsigned Log2(Align); |
46 | friend bool operator==(Align Lhs, Align Rhs); |
47 | friend bool operator!=(Align Lhs, Align Rhs); |
48 | friend bool operator<=(Align Lhs, Align Rhs); |
49 | friend bool operator>=(Align Lhs, Align Rhs); |
50 | friend bool operator<(Align Lhs, Align Rhs); |
51 | friend bool operator>(Align Lhs, Align Rhs); |
52 | friend unsigned encode(struct MaybeAlign A); |
53 | friend struct MaybeAlign decodeMaybeAlign(unsigned Value); |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | struct LogValue { |
63 | uint8_t Log; |
64 | }; |
65 | |
66 | public: |
67 | |
68 | constexpr Align() = default; |
69 | |
70 | |
71 | constexpr Align(const Align &Other) = default; |
72 | constexpr Align(Align &&Other) = default; |
73 | Align &operator=(const Align &Other) = default; |
74 | Align &operator=(Align &&Other) = default; |
75 | |
76 | explicit Align(uint64_t Value) { |
77 | assert(Value > 0 && "Value must not be 0"); |
78 | assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2"); |
79 | ShiftValue = Log2_64(Value); |
80 | assert(ShiftValue < 64 && "Broken invariant"); |
81 | } |
82 | |
83 | |
84 | |
85 | uint64_t value() const { return uint64_t(1) << ShiftValue; } |
| 12 | | The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'uint64_t' |
|
86 | |
87 | |
88 | template <size_t kValue> constexpr static LogValue Constant() { |
89 | return LogValue{static_cast<uint8_t>(CTLog2<kValue>())}; |
90 | } |
91 | |
92 | |
93 | |
94 | template <typename T> constexpr static LogValue Of() { |
95 | return Constant<std::alignment_of<T>::value>(); |
96 | } |
97 | |
98 | |
99 | constexpr Align(LogValue CA) : ShiftValue(CA.Log) {} |
100 | }; |
101 | |
102 | |
103 | inline Align assumeAligned(uint64_t Value) { |
104 | return Value ? Align(Value) : Align(); |
105 | } |
106 | |
107 | |
108 | |
109 | struct MaybeAlign : public llvm::Optional<Align> { |
110 | private: |
111 | using UP = llvm::Optional<Align>; |
112 | |
113 | public: |
114 | |
115 | MaybeAlign() = default; |
116 | |
117 | |
118 | MaybeAlign(const MaybeAlign &Other) = default; |
119 | MaybeAlign &operator=(const MaybeAlign &Other) = default; |
120 | MaybeAlign(MaybeAlign &&Other) = default; |
121 | MaybeAlign &operator=(MaybeAlign &&Other) = default; |
122 | |
123 | |
124 | using UP::UP; |
125 | |
126 | explicit MaybeAlign(uint64_t Value) { |
127 | assert((Value == 0 || llvm::isPowerOf2_64(Value)) && |
128 | "Alignment is neither 0 nor a power of 2"); |
129 | if (Value) |
130 | emplace(Value); |
131 | } |
132 | |
133 | |
134 | Align valueOrOne() const { return hasValue() ? getValue() : Align(); } |
135 | }; |
136 | |
137 | |
138 | inline bool isAligned(Align Lhs, uint64_t SizeInBytes) { |
139 | return SizeInBytes % Lhs.value() == 0; |
140 | } |
141 | |
142 | |
143 | inline bool isAddrAligned(Align Lhs, const void *Addr) { |
144 | return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr)); |
145 | } |
146 | |
147 | |
148 | inline uint64_t alignTo(uint64_t Size, Align A) { |
149 | const uint64_t Value = A.value(); |
| |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | return (Size + Value - 1) & ~(Value - 1U); |
160 | } |
161 | |
162 | |
163 | |
164 | |
165 | |
166 | |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) { |
174 | const uint64_t Value = A.value(); |
175 | Skew %= Value; |
176 | return ((Size + Value - 1 - Skew) & ~(Value - 1U)) + Skew; |
177 | } |
178 | |
179 | |
180 | |
181 | inline uint64_t alignTo(uint64_t Size, MaybeAlign A) { |
182 | return A ? alignTo(Size, A.getValue()) : Size; |
183 | } |
184 | |
185 | |
186 | inline uintptr_t alignAddr(const void *Addr, Align Alignment) { |
187 | uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr); |
188 | assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >= |
189 | ArithAddr && |
190 | "Overflow"); |
191 | return alignTo(ArithAddr, Alignment); |
192 | } |
193 | |
194 | |
195 | |
196 | inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) { |
197 | return alignTo(Value, Alignment) - Value; |
| 9 | | The value 255 is assigned to 'A.ShiftValue' | |
|
| |
198 | } |
199 | |
200 | |
201 | |
202 | inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) { |
203 | return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment); |
| 8 | | Calling 'offsetToAlignment' | |
|
204 | } |
205 | |
206 | |
207 | inline unsigned Log2(Align A) { return A.ShiftValue; } |
208 | |
209 | |
210 | |
211 | inline Align commonAlignment(Align A, Align B) { return std::min(A, B); } |
212 | |
213 | |
214 | |
215 | inline Align commonAlignment(Align A, uint64_t Offset) { |
216 | return Align(MinAlign(A.value(), Offset)); |
217 | } |
218 | |
219 | |
220 | |
221 | inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) { |
222 | return A && B ? commonAlignment(*A, *B) : A ? A : B; |
223 | } |
224 | |
225 | |
226 | |
227 | inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) { |
228 | return MaybeAlign(MinAlign((*A).value(), Offset)); |
229 | } |
230 | |
231 | |
232 | inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; } |
233 | |
234 | |
235 | inline MaybeAlign decodeMaybeAlign(unsigned Value) { |
236 | if (Value == 0) |
237 | return MaybeAlign(); |
238 | Align Out; |
239 | Out.ShiftValue = Value - 1; |
240 | return Out; |
241 | } |
242 | |
243 | |
244 | |
245 | inline unsigned encode(Align A) { return encode(MaybeAlign(A)); } |
246 | |
247 | |
248 | inline bool operator==(Align Lhs, uint64_t Rhs) { |
249 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
250 | return Lhs.value() == Rhs; |
251 | } |
252 | inline bool operator!=(Align Lhs, uint64_t Rhs) { |
253 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
254 | return Lhs.value() != Rhs; |
255 | } |
256 | inline bool operator<=(Align Lhs, uint64_t Rhs) { |
257 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
258 | return Lhs.value() <= Rhs; |
259 | } |
260 | inline bool operator>=(Align Lhs, uint64_t Rhs) { |
261 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
262 | return Lhs.value() >= Rhs; |
263 | } |
264 | inline bool operator<(Align Lhs, uint64_t Rhs) { |
265 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
266 | return Lhs.value() < Rhs; |
267 | } |
268 | inline bool operator>(Align Lhs, uint64_t Rhs) { |
269 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
270 | return Lhs.value() > Rhs; |
271 | } |
272 | |
273 | |
274 | inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) { |
275 | return Lhs ? (*Lhs).value() == Rhs : Rhs == 0; |
276 | } |
277 | inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) { |
278 | return Lhs ? (*Lhs).value() != Rhs : Rhs != 0; |
279 | } |
280 | |
281 | |
282 | inline bool operator==(Align Lhs, Align Rhs) { |
283 | return Lhs.ShiftValue == Rhs.ShiftValue; |
284 | } |
285 | inline bool operator!=(Align Lhs, Align Rhs) { |
286 | return Lhs.ShiftValue != Rhs.ShiftValue; |
287 | } |
288 | inline bool operator<=(Align Lhs, Align Rhs) { |
289 | return Lhs.ShiftValue <= Rhs.ShiftValue; |
290 | } |
291 | inline bool operator>=(Align Lhs, Align Rhs) { |
292 | return Lhs.ShiftValue >= Rhs.ShiftValue; |
293 | } |
294 | inline bool operator<(Align Lhs, Align Rhs) { |
295 | return Lhs.ShiftValue < Rhs.ShiftValue; |
296 | } |
297 | inline bool operator>(Align Lhs, Align Rhs) { |
298 | return Lhs.ShiftValue > Rhs.ShiftValue; |
299 | } |
300 | |
301 | |
302 | bool operator<=(Align Lhs, MaybeAlign Rhs) = delete; |
303 | bool operator>=(Align Lhs, MaybeAlign Rhs) = delete; |
304 | bool operator<(Align Lhs, MaybeAlign Rhs) = delete; |
305 | bool operator>(Align Lhs, MaybeAlign Rhs) = delete; |
306 | |
307 | bool operator<=(MaybeAlign Lhs, Align Rhs) = delete; |
308 | bool operator>=(MaybeAlign Lhs, Align Rhs) = delete; |
309 | bool operator<(MaybeAlign Lhs, Align Rhs) = delete; |
310 | bool operator>(MaybeAlign Lhs, Align Rhs) = delete; |
311 | |
312 | bool operator<=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
313 | bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
314 | bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
315 | bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
316 | |
317 | inline Align operator*(Align Lhs, uint64_t Rhs) { |
318 | assert(Rhs > 0 && "Rhs must be positive"); |
319 | return Align(Lhs.value() * Rhs); |
320 | } |
321 | |
322 | inline MaybeAlign operator*(MaybeAlign Lhs, uint64_t Rhs) { |
323 | assert(Rhs > 0 && "Rhs must be positive"); |
324 | return Lhs ? Lhs.getValue() * Rhs : MaybeAlign(); |
325 | } |
326 | |
327 | inline Align operator/(Align Lhs, uint64_t Divisor) { |
328 | assert(llvm::isPowerOf2_64(Divisor) && |
329 | "Divisor must be positive and a power of 2"); |
330 | assert(Lhs != 1 && "Can't halve byte alignment"); |
331 | return Align(Lhs.value() / Divisor); |
332 | } |
333 | |
334 | inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) { |
335 | assert(llvm::isPowerOf2_64(Divisor) && |
336 | "Divisor must be positive and a power of 2"); |
337 | return Lhs ? Lhs.getValue() / Divisor : MaybeAlign(); |
338 | } |
339 | |
340 | inline Align max(MaybeAlign Lhs, Align Rhs) { |
341 | return Lhs && *Lhs > Rhs ? *Lhs : Rhs; |
342 | } |
343 | |
344 | inline Align max(Align Lhs, MaybeAlign Rhs) { |
345 | return Rhs && *Rhs > Lhs ? *Rhs : Lhs; |
346 | } |
347 | |
348 | #ifndef NDEBUG |
349 | |
350 | inline std::string DebugStr(const Align &A) { |
351 | return std::to_string(A.value()); |
352 | } |
353 | |
354 | inline std::string DebugStr(const MaybeAlign &MA) { |
355 | if (MA) |
356 | return std::to_string(MA->value()); |
357 | return "None"; |
358 | } |
359 | #endif // NDEBUG |
360 | |
361 | #undef ALIGN_CHECK_ISPOSITIVE |
362 | |
363 | } |
364 | |
365 | #endif // LLVM_SUPPORT_ALIGNMENT_H_ |