Bug Summary

File:lib/Bitcode/Writer/BitcodeWriter.cpp
Warning:line 420, column 9
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name BitcodeWriter.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn326425/build-llvm/lib/Bitcode/Writer -I /build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer -I /build/llvm-toolchain-snapshot-7~svn326425/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn326425/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn326425/build-llvm/lib/Bitcode/Writer -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-03-01-211340-26438-1 -x c++ /build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp

/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp

1//===- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Bitcode/BitcodeWriter.h"
15#include "ValueEnumerator.h"
16#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/None.h"
21#include "llvm/ADT/Optional.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Triple.h"
28#include "llvm/Bitcode/BitCodes.h"
29#include "llvm/Bitcode/BitstreamWriter.h"
30#include "llvm/Bitcode/LLVMBitCodes.h"
31#include "llvm/IR/Attributes.h"
32#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/CallSite.h"
34#include "llvm/IR/Comdat.h"
35#include "llvm/IR/Constant.h"
36#include "llvm/IR/Constants.h"
37#include "llvm/IR/DebugInfoMetadata.h"
38#include "llvm/IR/DebugLoc.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalAlias.h"
42#include "llvm/IR/GlobalIFunc.h"
43#include "llvm/IR/GlobalObject.h"
44#include "llvm/IR/GlobalValue.h"
45#include "llvm/IR/GlobalVariable.h"
46#include "llvm/IR/InlineAsm.h"
47#include "llvm/IR/InstrTypes.h"
48#include "llvm/IR/Instruction.h"
49#include "llvm/IR/Instructions.h"
50#include "llvm/IR/LLVMContext.h"
51#include "llvm/IR/Metadata.h"
52#include "llvm/IR/Module.h"
53#include "llvm/IR/ModuleSummaryIndex.h"
54#include "llvm/IR/Operator.h"
55#include "llvm/IR/Type.h"
56#include "llvm/IR/UseListOrder.h"
57#include "llvm/IR/Value.h"
58#include "llvm/IR/ValueSymbolTable.h"
59#include "llvm/MC/StringTableBuilder.h"
60#include "llvm/Object/IRSymtab.h"
61#include "llvm/Support/AtomicOrdering.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/CommandLine.h"
64#include "llvm/Support/Endian.h"
65#include "llvm/Support/Error.h"
66#include "llvm/Support/ErrorHandling.h"
67#include "llvm/Support/MathExtras.h"
68#include "llvm/Support/SHA1.h"
69#include "llvm/Support/TargetRegistry.h"
70#include "llvm/Support/raw_ostream.h"
71#include <algorithm>
72#include <cassert>
73#include <cstddef>
74#include <cstdint>
75#include <iterator>
76#include <map>
77#include <memory>
78#include <string>
79#include <utility>
80#include <vector>
81
82using namespace llvm;
83
84static cl::opt<unsigned>
85 IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25),
86 cl::desc("Number of metadatas above which we emit an index "
87 "to enable lazy-loading"));
88
89cl::opt<bool> WriteRelBFToSummary(
90 "write-relbf-to-summary", cl::Hidden, cl::init(false),
91 cl::desc("Write relative block frequency to function summary "));
92namespace {
93
94/// These are manifest constants used by the bitcode writer. They do not need to
95/// be kept in sync with the reader, but need to be consistent within this file.
96enum {
97 // VALUE_SYMTAB_BLOCK abbrev id's.
98 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
99 VST_ENTRY_7_ABBREV,
100 VST_ENTRY_6_ABBREV,
101 VST_BBENTRY_6_ABBREV,
102
103 // CONSTANTS_BLOCK abbrev id's.
104 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
105 CONSTANTS_INTEGER_ABBREV,
106 CONSTANTS_CE_CAST_Abbrev,
107 CONSTANTS_NULL_Abbrev,
108
109 // FUNCTION_BLOCK abbrev id's.
110 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
111 FUNCTION_INST_BINOP_ABBREV,
112 FUNCTION_INST_BINOP_FLAGS_ABBREV,
113 FUNCTION_INST_CAST_ABBREV,
114 FUNCTION_INST_RET_VOID_ABBREV,
115 FUNCTION_INST_RET_VAL_ABBREV,
116 FUNCTION_INST_UNREACHABLE_ABBREV,
117 FUNCTION_INST_GEP_ABBREV,
118};
119
120/// Abstract class to manage the bitcode writing, subclassed for each bitcode
121/// file type.
122class BitcodeWriterBase {
123protected:
124 /// The stream created and owned by the client.
125 BitstreamWriter &Stream;
126
127 StringTableBuilder &StrtabBuilder;
128
129public:
130 /// Constructs a BitcodeWriterBase object that writes to the provided
131 /// \p Stream.
132 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
133 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
134
135protected:
136 void writeBitcodeHeader();
137 void writeModuleVersion();
138};
139
140void BitcodeWriterBase::writeModuleVersion() {
141 // VERSION: [version#]
142 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2});
143}
144
145/// Base class to manage the module bitcode writing, currently subclassed for
146/// ModuleBitcodeWriter and ThinLinkBitcodeWriter.
147class ModuleBitcodeWriterBase : public BitcodeWriterBase {
148protected:
149 /// The Module to write to bitcode.
150 const Module &M;
151
152 /// Enumerates ids for all values in the module.
153 ValueEnumerator VE;
154
155 /// Optional per-module index to write for ThinLTO.
156 const ModuleSummaryIndex *Index;
157
158 /// Map that holds the correspondence between GUIDs in the summary index,
159 /// that came from indirect call profiles, and a value id generated by this
160 /// class to use in the VST and summary block records.
161 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
162
163 /// Tracks the last value id recorded in the GUIDToValueMap.
164 unsigned GlobalValueId;
165
166 /// Saves the offset of the VSTOffset record that must eventually be
167 /// backpatched with the offset of the actual VST.
168 uint64_t VSTOffsetPlaceholder = 0;
169
170public:
171 /// Constructs a ModuleBitcodeWriterBase object for the given Module,
172 /// writing to the provided \p Buffer.
173 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
174 BitstreamWriter &Stream,
175 bool ShouldPreserveUseListOrder,
176 const ModuleSummaryIndex *Index)
177 : BitcodeWriterBase(Stream, StrtabBuilder), M(M),
178 VE(M, ShouldPreserveUseListOrder), Index(Index) {
179 // Assign ValueIds to any callee values in the index that came from
180 // indirect call profiles and were recorded as a GUID not a Value*
181 // (which would have been assigned an ID by the ValueEnumerator).
182 // The starting ValueId is just after the number of values in the
183 // ValueEnumerator, so that they can be emitted in the VST.
184 GlobalValueId = VE.getValues().size();
185 if (!Index)
186 return;
187 for (const auto &GUIDSummaryLists : *Index)
188 // Examine all summaries for this GUID.
189 for (auto &Summary : GUIDSummaryLists.second.SummaryList)
190 if (auto FS = dyn_cast<FunctionSummary>(Summary.get()))
191 // For each call in the function summary, see if the call
192 // is to a GUID (which means it is for an indirect call,
193 // otherwise we would have a Value for it). If so, synthesize
194 // a value id.
195 for (auto &CallEdge : FS->calls())
196 if (!CallEdge.first.getValue())
197 assignValueId(CallEdge.first.getGUID());
198 }
199
200protected:
201 void writePerModuleGlobalValueSummary();
202
203private:
204 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
205 GlobalValueSummary *Summary,
206 unsigned ValueID,
207 unsigned FSCallsAbbrev,
208 unsigned FSCallsProfileAbbrev,
209 const Function &F);
210 void writeModuleLevelReferences(const GlobalVariable &V,
211 SmallVector<uint64_t, 64> &NameVals,
212 unsigned FSModRefsAbbrev);
213
214 void assignValueId(GlobalValue::GUID ValGUID) {
215 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
216 }
217
218 unsigned getValueId(GlobalValue::GUID ValGUID) {
219 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
220 // Expect that any GUID value had a value Id assigned by an
221 // earlier call to assignValueId.
222 assert(VMI != GUIDToValueIdMap.end() &&(static_cast <bool> (VMI != GUIDToValueIdMap.end() &&
"GUID does not have assigned value Id") ? void (0) : __assert_fail
("VMI != GUIDToValueIdMap.end() && \"GUID does not have assigned value Id\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 223, __extension__ __PRETTY_FUNCTION__))
223 "GUID does not have assigned value Id")(static_cast <bool> (VMI != GUIDToValueIdMap.end() &&
"GUID does not have assigned value Id") ? void (0) : __assert_fail
("VMI != GUIDToValueIdMap.end() && \"GUID does not have assigned value Id\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 223, __extension__ __PRETTY_FUNCTION__))
;
224 return VMI->second;
225 }
226
227 // Helper to get the valueId for the type of value recorded in VI.
228 unsigned getValueId(ValueInfo VI) {
229 if (!VI.getValue())
230 return getValueId(VI.getGUID());
231 return VE.getValueID(VI.getValue());
232 }
233
234 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
235};
236
237/// Class to manage the bitcode writing for a module.
238class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
239 /// Pointer to the buffer allocated by caller for bitcode writing.
240 const SmallVectorImpl<char> &Buffer;
241
242 /// True if a module hash record should be written.
243 bool GenerateHash;
244
245 /// If non-null, when GenerateHash is true, the resulting hash is written
246 /// into ModHash.
247 ModuleHash *ModHash;
248
249 SHA1 Hasher;
250
251 /// The start bit of the identification block.
252 uint64_t BitcodeStartBit;
253
254public:
255 /// Constructs a ModuleBitcodeWriter object for the given Module,
256 /// writing to the provided \p Buffer.
257 ModuleBitcodeWriter(const Module &M, SmallVectorImpl<char> &Buffer,
258 StringTableBuilder &StrtabBuilder,
259 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
260 const ModuleSummaryIndex *Index, bool GenerateHash,
261 ModuleHash *ModHash = nullptr)
262 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
263 ShouldPreserveUseListOrder, Index),
264 Buffer(Buffer), GenerateHash(GenerateHash), ModHash(ModHash),
265 BitcodeStartBit(Stream.GetCurrentBitNo()) {}
266
267 /// Emit the current module to the bitstream.
268 void write();
269
270private:
271 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
272
273 size_t addToStrtab(StringRef Str);
274
275 void writeAttributeGroupTable();
276 void writeAttributeTable();
277 void writeTypeTable();
278 void writeComdats();
279 void writeValueSymbolTableForwardDecl();
280 void writeModuleInfo();
281 void writeValueAsMetadata(const ValueAsMetadata *MD,
282 SmallVectorImpl<uint64_t> &Record);
283 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
284 unsigned Abbrev);
285 unsigned createDILocationAbbrev();
286 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
287 unsigned &Abbrev);
288 unsigned createGenericDINodeAbbrev();
289 void writeGenericDINode(const GenericDINode *N,
290 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
291 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
292 unsigned Abbrev);
293 void writeDIEnumerator(const DIEnumerator *N,
294 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
295 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
296 unsigned Abbrev);
297 void writeDIDerivedType(const DIDerivedType *N,
298 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
299 void writeDICompositeType(const DICompositeType *N,
300 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
301 void writeDISubroutineType(const DISubroutineType *N,
302 SmallVectorImpl<uint64_t> &Record,
303 unsigned Abbrev);
304 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
305 unsigned Abbrev);
306 void writeDICompileUnit(const DICompileUnit *N,
307 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
308 void writeDISubprogram(const DISubprogram *N,
309 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
310 void writeDILexicalBlock(const DILexicalBlock *N,
311 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
312 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
313 SmallVectorImpl<uint64_t> &Record,
314 unsigned Abbrev);
315 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
316 unsigned Abbrev);
317 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
318 unsigned Abbrev);
319 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
320 unsigned Abbrev);
321 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
322 unsigned Abbrev);
323 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
324 SmallVectorImpl<uint64_t> &Record,
325 unsigned Abbrev);
326 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
327 SmallVectorImpl<uint64_t> &Record,
328 unsigned Abbrev);
329 void writeDIGlobalVariable(const DIGlobalVariable *N,
330 SmallVectorImpl<uint64_t> &Record,
331 unsigned Abbrev);
332 void writeDILocalVariable(const DILocalVariable *N,
333 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
334 void writeDIExpression(const DIExpression *N,
335 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
336 void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N,
337 SmallVectorImpl<uint64_t> &Record,
338 unsigned Abbrev);
339 void writeDIObjCProperty(const DIObjCProperty *N,
340 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
341 void writeDIImportedEntity(const DIImportedEntity *N,
342 SmallVectorImpl<uint64_t> &Record,
343 unsigned Abbrev);
344 unsigned createNamedMetadataAbbrev();
345 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
346 unsigned createMetadataStringsAbbrev();
347 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
348 SmallVectorImpl<uint64_t> &Record);
349 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
350 SmallVectorImpl<uint64_t> &Record,
351 std::vector<unsigned> *MDAbbrevs = nullptr,
352 std::vector<uint64_t> *IndexPos = nullptr);
353 void writeModuleMetadata();
354 void writeFunctionMetadata(const Function &F);
355 void writeFunctionMetadataAttachment(const Function &F);
356 void writeGlobalVariableMetadataAttachment(const GlobalVariable &GV);
357 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
358 const GlobalObject &GO);
359 void writeModuleMetadataKinds();
360 void writeOperandBundleTags();
361 void writeSyncScopeNames();
362 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
363 void writeModuleConstants();
364 bool pushValueAndType(const Value *V, unsigned InstID,
365 SmallVectorImpl<unsigned> &Vals);
366 void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
367 void pushValue(const Value *V, unsigned InstID,
368 SmallVectorImpl<unsigned> &Vals);
369 void pushValueSigned(const Value *V, unsigned InstID,
370 SmallVectorImpl<uint64_t> &Vals);
371 void writeInstruction(const Instruction &I, unsigned InstID,
372 SmallVectorImpl<unsigned> &Vals);
373 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
374 void writeGlobalValueSymbolTable(
375 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
376 void writeUseList(UseListOrder &&Order);
377 void writeUseListBlock(const Function *F);
378 void
379 writeFunction(const Function &F,
380 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
381 void writeBlockInfo();
382 void writeModuleHash(size_t BlockStartPos);
383
384 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) {
385 return unsigned(SSID);
386 }
387};
388
389/// Class to manage the bitcode writing for a combined index.
390class IndexBitcodeWriter : public BitcodeWriterBase {
391 /// The combined index to write to bitcode.
392 const ModuleSummaryIndex &Index;
393
394 /// When writing a subset of the index for distributed backends, client
395 /// provides a map of modules to the corresponding GUIDs/summaries to write.
396 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
397
398 /// Map that holds the correspondence between the GUID used in the combined
399 /// index and a value id generated by this class to use in references.
400 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
401
402 /// Tracks the last value id recorded in the GUIDToValueMap.
403 unsigned GlobalValueId = 0;
404
405public:
406 /// Constructs a IndexBitcodeWriter object for the given combined index,
407 /// writing to the provided \p Buffer. When writing a subset of the index
408 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
409 IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
410 const ModuleSummaryIndex &Index,
411 const std::map<std::string, GVSummaryMapTy>
412 *ModuleToSummariesForIndex = nullptr)
413 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
414 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
415 // Assign unique value ids to all summaries to be written, for use
416 // in writing out the call graph edges. Save the mapping from GUID
417 // to the new global value id to use when writing those edges, which
418 // are currently saved in the index in terms of GUID.
419 forEachSummary([&](GVInfo I, bool) {
420 GUIDToValueIdMap[I.first] = ++GlobalValueId;
421 });
422 }
423
424 /// The below iterator returns the GUID and associated summary.
425 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
426
427 /// Calls the callback for each value GUID and summary to be written to
428 /// bitcode. This hides the details of whether they are being pulled from the
429 /// entire index or just those in a provided ModuleToSummariesForIndex map.
430 template<typename Functor>
431 void forEachSummary(Functor Callback) {
432 if (ModuleToSummariesForIndex) {
433 for (auto &M : *ModuleToSummariesForIndex)
434 for (auto &Summary : M.second) {
435 Callback(Summary, false);
436 // Ensure aliasee is handled, e.g. for assigning a valueId,
437 // even if we are not importing the aliasee directly (the
438 // imported alias will contain a copy of aliasee).
439 if (auto *AS = dyn_cast<AliasSummary>(Summary.getSecond()))
440 Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true);
441 }
442 } else {
443 for (auto &Summaries : Index)
444 for (auto &Summary : Summaries.second.SummaryList)
445 Callback({Summaries.first, Summary.get()}, false);
446 }
447 }
448
449 /// Calls the callback for each entry in the modulePaths StringMap that
450 /// should be written to the module path string table. This hides the details
451 /// of whether they are being pulled from the entire index or just those in a
452 /// provided ModuleToSummariesForIndex map.
453 template <typename Functor> void forEachModule(Functor Callback) {
454 if (ModuleToSummariesForIndex) {
455 for (const auto &M : *ModuleToSummariesForIndex) {
456 const auto &MPI = Index.modulePaths().find(M.first);
457 if (MPI == Index.modulePaths().end()) {
458 // This should only happen if the bitcode file was empty, in which
459 // case we shouldn't be importing (the ModuleToSummariesForIndex
460 // would only include the module we are writing and index for).
461 assert(ModuleToSummariesForIndex->size() == 1)(static_cast <bool> (ModuleToSummariesForIndex->size
() == 1) ? void (0) : __assert_fail ("ModuleToSummariesForIndex->size() == 1"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 461, __extension__ __PRETTY_FUNCTION__))
;
462 continue;
463 }
464 Callback(*MPI);
465 }
466 } else {
467 for (const auto &MPSE : Index.modulePaths())
468 Callback(MPSE);
469 }
470 }
471
472 /// Main entry point for writing a combined index to bitcode.
473 void write();
474
475private:
476 void writeModStrings();
477 void writeCombinedGlobalValueSummary();
478
479 Optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
480 auto VMI = GUIDToValueIdMap.find(ValGUID);
481 if (VMI == GUIDToValueIdMap.end())
482 return None;
483 return VMI->second;
484 }
485
486 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
487};
488
489} // end anonymous namespace
490
491static unsigned getEncodedCastOpcode(unsigned Opcode) {
492 switch (Opcode) {
493 default: llvm_unreachable("Unknown cast instruction!")::llvm::llvm_unreachable_internal("Unknown cast instruction!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 493)
;
494 case Instruction::Trunc : return bitc::CAST_TRUNC;
495 case Instruction::ZExt : return bitc::CAST_ZEXT;
496 case Instruction::SExt : return bitc::CAST_SEXT;
497 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
498 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
499 case Instruction::UIToFP : return bitc::CAST_UITOFP;
500 case Instruction::SIToFP : return bitc::CAST_SITOFP;
501 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
502 case Instruction::FPExt : return bitc::CAST_FPEXT;
503 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
504 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
505 case Instruction::BitCast : return bitc::CAST_BITCAST;
506 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
507 }
508}
509
510static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
511 switch (Opcode) {
512 default: llvm_unreachable("Unknown binary instruction!")::llvm::llvm_unreachable_internal("Unknown binary instruction!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 512)
;
513 case Instruction::Add:
514 case Instruction::FAdd: return bitc::BINOP_ADD;
515 case Instruction::Sub:
516 case Instruction::FSub: return bitc::BINOP_SUB;
517 case Instruction::Mul:
518 case Instruction::FMul: return bitc::BINOP_MUL;
519 case Instruction::UDiv: return bitc::BINOP_UDIV;
520 case Instruction::FDiv:
521 case Instruction::SDiv: return bitc::BINOP_SDIV;
522 case Instruction::URem: return bitc::BINOP_UREM;
523 case Instruction::FRem:
524 case Instruction::SRem: return bitc::BINOP_SREM;
525 case Instruction::Shl: return bitc::BINOP_SHL;
526 case Instruction::LShr: return bitc::BINOP_LSHR;
527 case Instruction::AShr: return bitc::BINOP_ASHR;
528 case Instruction::And: return bitc::BINOP_AND;
529 case Instruction::Or: return bitc::BINOP_OR;
530 case Instruction::Xor: return bitc::BINOP_XOR;
531 }
532}
533
534static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
535 switch (Op) {
536 default: llvm_unreachable("Unknown RMW operation!")::llvm::llvm_unreachable_internal("Unknown RMW operation!", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 536)
;
537 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
538 case AtomicRMWInst::Add: return bitc::RMW_ADD;
539 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
540 case AtomicRMWInst::And: return bitc::RMW_AND;
541 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
542 case AtomicRMWInst::Or: return bitc::RMW_OR;
543 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
544 case AtomicRMWInst::Max: return bitc::RMW_MAX;
545 case AtomicRMWInst::Min: return bitc::RMW_MIN;
546 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
547 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
548 }
549}
550
551static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
552 switch (Ordering) {
553 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
554 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
555 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
556 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
557 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
558 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
559 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
560 }
561 llvm_unreachable("Invalid ordering")::llvm::llvm_unreachable_internal("Invalid ordering", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 561)
;
562}
563
564static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
565 StringRef Str, unsigned AbbrevToUse) {
566 SmallVector<unsigned, 64> Vals;
567
568 // Code: [strchar x N]
569 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
570 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
571 AbbrevToUse = 0;
572 Vals.push_back(Str[i]);
573 }
574
575 // Emit the finished record.
576 Stream.EmitRecord(Code, Vals, AbbrevToUse);
577}
578
579static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
580 switch (Kind) {
581 case Attribute::Alignment:
582 return bitc::ATTR_KIND_ALIGNMENT;
583 case Attribute::AllocSize:
584 return bitc::ATTR_KIND_ALLOC_SIZE;
585 case Attribute::AlwaysInline:
586 return bitc::ATTR_KIND_ALWAYS_INLINE;
587 case Attribute::ArgMemOnly:
588 return bitc::ATTR_KIND_ARGMEMONLY;
589 case Attribute::Builtin:
590 return bitc::ATTR_KIND_BUILTIN;
591 case Attribute::ByVal:
592 return bitc::ATTR_KIND_BY_VAL;
593 case Attribute::Convergent:
594 return bitc::ATTR_KIND_CONVERGENT;
595 case Attribute::InAlloca:
596 return bitc::ATTR_KIND_IN_ALLOCA;
597 case Attribute::Cold:
598 return bitc::ATTR_KIND_COLD;
599 case Attribute::InaccessibleMemOnly:
600 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
601 case Attribute::InaccessibleMemOrArgMemOnly:
602 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
603 case Attribute::InlineHint:
604 return bitc::ATTR_KIND_INLINE_HINT;
605 case Attribute::InReg:
606 return bitc::ATTR_KIND_IN_REG;
607 case Attribute::JumpTable:
608 return bitc::ATTR_KIND_JUMP_TABLE;
609 case Attribute::MinSize:
610 return bitc::ATTR_KIND_MIN_SIZE;
611 case Attribute::Naked:
612 return bitc::ATTR_KIND_NAKED;
613 case Attribute::Nest:
614 return bitc::ATTR_KIND_NEST;
615 case Attribute::NoAlias:
616 return bitc::ATTR_KIND_NO_ALIAS;
617 case Attribute::NoBuiltin:
618 return bitc::ATTR_KIND_NO_BUILTIN;
619 case Attribute::NoCapture:
620 return bitc::ATTR_KIND_NO_CAPTURE;
621 case Attribute::NoDuplicate:
622 return bitc::ATTR_KIND_NO_DUPLICATE;
623 case Attribute::NoImplicitFloat:
624 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
625 case Attribute::NoInline:
626 return bitc::ATTR_KIND_NO_INLINE;
627 case Attribute::NoRecurse:
628 return bitc::ATTR_KIND_NO_RECURSE;
629 case Attribute::NonLazyBind:
630 return bitc::ATTR_KIND_NON_LAZY_BIND;
631 case Attribute::NonNull:
632 return bitc::ATTR_KIND_NON_NULL;
633 case Attribute::Dereferenceable:
634 return bitc::ATTR_KIND_DEREFERENCEABLE;
635 case Attribute::DereferenceableOrNull:
636 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
637 case Attribute::NoRedZone:
638 return bitc::ATTR_KIND_NO_RED_ZONE;
639 case Attribute::NoReturn:
640 return bitc::ATTR_KIND_NO_RETURN;
641 case Attribute::NoUnwind:
642 return bitc::ATTR_KIND_NO_UNWIND;
643 case Attribute::OptimizeForSize:
644 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
645 case Attribute::OptimizeNone:
646 return bitc::ATTR_KIND_OPTIMIZE_NONE;
647 case Attribute::ReadNone:
648 return bitc::ATTR_KIND_READ_NONE;
649 case Attribute::ReadOnly:
650 return bitc::ATTR_KIND_READ_ONLY;
651 case Attribute::Returned:
652 return bitc::ATTR_KIND_RETURNED;
653 case Attribute::ReturnsTwice:
654 return bitc::ATTR_KIND_RETURNS_TWICE;
655 case Attribute::SExt:
656 return bitc::ATTR_KIND_S_EXT;
657 case Attribute::Speculatable:
658 return bitc::ATTR_KIND_SPECULATABLE;
659 case Attribute::StackAlignment:
660 return bitc::ATTR_KIND_STACK_ALIGNMENT;
661 case Attribute::StackProtect:
662 return bitc::ATTR_KIND_STACK_PROTECT;
663 case Attribute::StackProtectReq:
664 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
665 case Attribute::StackProtectStrong:
666 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
667 case Attribute::SafeStack:
668 return bitc::ATTR_KIND_SAFESTACK;
669 case Attribute::StrictFP:
670 return bitc::ATTR_KIND_STRICT_FP;
671 case Attribute::StructRet:
672 return bitc::ATTR_KIND_STRUCT_RET;
673 case Attribute::SanitizeAddress:
674 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
675 case Attribute::SanitizeHWAddress:
676 return bitc::ATTR_KIND_SANITIZE_HWADDRESS;
677 case Attribute::SanitizeThread:
678 return bitc::ATTR_KIND_SANITIZE_THREAD;
679 case Attribute::SanitizeMemory:
680 return bitc::ATTR_KIND_SANITIZE_MEMORY;
681 case Attribute::SwiftError:
682 return bitc::ATTR_KIND_SWIFT_ERROR;
683 case Attribute::SwiftSelf:
684 return bitc::ATTR_KIND_SWIFT_SELF;
685 case Attribute::UWTable:
686 return bitc::ATTR_KIND_UW_TABLE;
687 case Attribute::WriteOnly:
688 return bitc::ATTR_KIND_WRITEONLY;
689 case Attribute::ZExt:
690 return bitc::ATTR_KIND_Z_EXT;
691 case Attribute::EndAttrKinds:
692 llvm_unreachable("Can not encode end-attribute kinds marker.")::llvm::llvm_unreachable_internal("Can not encode end-attribute kinds marker."
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 692)
;
693 case Attribute::None:
694 llvm_unreachable("Can not encode none-attribute.")::llvm::llvm_unreachable_internal("Can not encode none-attribute."
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 694)
;
695 }
696
697 llvm_unreachable("Trying to encode unknown attribute")::llvm::llvm_unreachable_internal("Trying to encode unknown attribute"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 697)
;
698}
699
700void ModuleBitcodeWriter::writeAttributeGroupTable() {
701 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
702 VE.getAttributeGroups();
703 if (AttrGrps.empty()) return;
704
705 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
706
707 SmallVector<uint64_t, 64> Record;
708 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
709 unsigned AttrListIndex = Pair.first;
710 AttributeSet AS = Pair.second;
711 Record.push_back(VE.getAttributeGroupID(Pair));
712 Record.push_back(AttrListIndex);
713
714 for (Attribute Attr : AS) {
715 if (Attr.isEnumAttribute()) {
716 Record.push_back(0);
717 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
718 } else if (Attr.isIntAttribute()) {
719 Record.push_back(1);
720 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
721 Record.push_back(Attr.getValueAsInt());
722 } else {
723 StringRef Kind = Attr.getKindAsString();
724 StringRef Val = Attr.getValueAsString();
725
726 Record.push_back(Val.empty() ? 3 : 4);
727 Record.append(Kind.begin(), Kind.end());
728 Record.push_back(0);
729 if (!Val.empty()) {
730 Record.append(Val.begin(), Val.end());
731 Record.push_back(0);
732 }
733 }
734 }
735
736 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
737 Record.clear();
738 }
739
740 Stream.ExitBlock();
741}
742
743void ModuleBitcodeWriter::writeAttributeTable() {
744 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
745 if (Attrs.empty()) return;
746
747 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
748
749 SmallVector<uint64_t, 64> Record;
750 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
751 AttributeList AL = Attrs[i];
752 for (unsigned i = AL.index_begin(), e = AL.index_end(); i != e; ++i) {
753 AttributeSet AS = AL.getAttributes(i);
754 if (AS.hasAttributes())
755 Record.push_back(VE.getAttributeGroupID({i, AS}));
756 }
757
758 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
759 Record.clear();
760 }
761
762 Stream.ExitBlock();
763}
764
765/// WriteTypeTable - Write out the type table for a module.
766void ModuleBitcodeWriter::writeTypeTable() {
767 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
768
769 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
770 SmallVector<uint64_t, 64> TypeVals;
771
772 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
773
774 // Abbrev for TYPE_CODE_POINTER.
775 auto Abbv = std::make_shared<BitCodeAbbrev>();
776 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
777 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
778 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
779 unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
780
781 // Abbrev for TYPE_CODE_FUNCTION.
782 Abbv = std::make_shared<BitCodeAbbrev>();
783 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
784 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
787 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
788
789 // Abbrev for TYPE_CODE_STRUCT_ANON.
790 Abbv = std::make_shared<BitCodeAbbrev>();
791 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
792 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
793 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
794 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
795 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
796
797 // Abbrev for TYPE_CODE_STRUCT_NAME.
798 Abbv = std::make_shared<BitCodeAbbrev>();
799 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
801 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
802 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
803
804 // Abbrev for TYPE_CODE_STRUCT_NAMED.
805 Abbv = std::make_shared<BitCodeAbbrev>();
806 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
807 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
808 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
810 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
811
812 // Abbrev for TYPE_CODE_ARRAY.
813 Abbv = std::make_shared<BitCodeAbbrev>();
814 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
815 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
817 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
818
819 // Emit an entry count so the reader can reserve space.
820 TypeVals.push_back(TypeList.size());
821 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
822 TypeVals.clear();
823
824 // Loop over all of the types, emitting each in turn.
825 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
826 Type *T = TypeList[i];
827 int AbbrevToUse = 0;
828 unsigned Code = 0;
829
830 switch (T->getTypeID()) {
831 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
832 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
833 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
834 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
835 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
836 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
837 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
838 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
839 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
840 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
841 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
842 case Type::IntegerTyID:
843 // INTEGER: [width]
844 Code = bitc::TYPE_CODE_INTEGER;
845 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
846 break;
847 case Type::PointerTyID: {
848 PointerType *PTy = cast<PointerType>(T);
849 // POINTER: [pointee type, address space]
850 Code = bitc::TYPE_CODE_POINTER;
851 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
852 unsigned AddressSpace = PTy->getAddressSpace();
853 TypeVals.push_back(AddressSpace);
854 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
855 break;
856 }
857 case Type::FunctionTyID: {
858 FunctionType *FT = cast<FunctionType>(T);
859 // FUNCTION: [isvararg, retty, paramty x N]
860 Code = bitc::TYPE_CODE_FUNCTION;
861 TypeVals.push_back(FT->isVarArg());
862 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
863 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
864 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
865 AbbrevToUse = FunctionAbbrev;
866 break;
867 }
868 case Type::StructTyID: {
869 StructType *ST = cast<StructType>(T);
870 // STRUCT: [ispacked, eltty x N]
871 TypeVals.push_back(ST->isPacked());
872 // Output all of the element types.
873 for (StructType::element_iterator I = ST->element_begin(),
874 E = ST->element_end(); I != E; ++I)
875 TypeVals.push_back(VE.getTypeID(*I));
876
877 if (ST->isLiteral()) {
878 Code = bitc::TYPE_CODE_STRUCT_ANON;
879 AbbrevToUse = StructAnonAbbrev;
880 } else {
881 if (ST->isOpaque()) {
882 Code = bitc::TYPE_CODE_OPAQUE;
883 } else {
884 Code = bitc::TYPE_CODE_STRUCT_NAMED;
885 AbbrevToUse = StructNamedAbbrev;
886 }
887
888 // Emit the name if it is present.
889 if (!ST->getName().empty())
890 writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
891 StructNameAbbrev);
892 }
893 break;
894 }
895 case Type::ArrayTyID: {
896 ArrayType *AT = cast<ArrayType>(T);
897 // ARRAY: [numelts, eltty]
898 Code = bitc::TYPE_CODE_ARRAY;
899 TypeVals.push_back(AT->getNumElements());
900 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
901 AbbrevToUse = ArrayAbbrev;
902 break;
903 }
904 case Type::VectorTyID: {
905 VectorType *VT = cast<VectorType>(T);
906 // VECTOR [numelts, eltty]
907 Code = bitc::TYPE_CODE_VECTOR;
908 TypeVals.push_back(VT->getNumElements());
909 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
910 break;
911 }
912 }
913
914 // Emit the finished record.
915 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
916 TypeVals.clear();
917 }
918
919 Stream.ExitBlock();
920}
921
922static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
923 switch (Linkage) {
924 case GlobalValue::ExternalLinkage:
925 return 0;
926 case GlobalValue::WeakAnyLinkage:
927 return 16;
928 case GlobalValue::AppendingLinkage:
929 return 2;
930 case GlobalValue::InternalLinkage:
931 return 3;
932 case GlobalValue::LinkOnceAnyLinkage:
933 return 18;
934 case GlobalValue::ExternalWeakLinkage:
935 return 7;
936 case GlobalValue::CommonLinkage:
937 return 8;
938 case GlobalValue::PrivateLinkage:
939 return 9;
940 case GlobalValue::WeakODRLinkage:
941 return 17;
942 case GlobalValue::LinkOnceODRLinkage:
943 return 19;
944 case GlobalValue::AvailableExternallyLinkage:
945 return 12;
946 }
947 llvm_unreachable("Invalid linkage")::llvm::llvm_unreachable_internal("Invalid linkage", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 947)
;
948}
949
950static unsigned getEncodedLinkage(const GlobalValue &GV) {
951 return getEncodedLinkage(GV.getLinkage());
952}
953
954static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) {
955 uint64_t RawFlags = 0;
956 RawFlags |= Flags.ReadNone;
957 RawFlags |= (Flags.ReadOnly << 1);
958 RawFlags |= (Flags.NoRecurse << 2);
959 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
960 return RawFlags;
961}
962
963// Decode the flags for GlobalValue in the summary
964static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
965 uint64_t RawFlags = 0;
966
967 RawFlags |= Flags.NotEligibleToImport; // bool
968 RawFlags |= (Flags.Live << 1);
969 RawFlags |= (Flags.DSOLocal << 2);
970
971 // Linkage don't need to be remapped at that time for the summary. Any future
972 // change to the getEncodedLinkage() function will need to be taken into
973 // account here as well.
974 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
975
976 return RawFlags;
977}
978
979static unsigned getEncodedVisibility(const GlobalValue &GV) {
980 switch (GV.getVisibility()) {
981 case GlobalValue::DefaultVisibility: return 0;
982 case GlobalValue::HiddenVisibility: return 1;
983 case GlobalValue::ProtectedVisibility: return 2;
984 }
985 llvm_unreachable("Invalid visibility")::llvm::llvm_unreachable_internal("Invalid visibility", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 985)
;
986}
987
988static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
989 switch (GV.getDLLStorageClass()) {
990 case GlobalValue::DefaultStorageClass: return 0;
991 case GlobalValue::DLLImportStorageClass: return 1;
992 case GlobalValue::DLLExportStorageClass: return 2;
993 }
994 llvm_unreachable("Invalid DLL storage class")::llvm::llvm_unreachable_internal("Invalid DLL storage class"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 994)
;
995}
996
997static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
998 switch (GV.getThreadLocalMode()) {
999 case GlobalVariable::NotThreadLocal: return 0;
1000 case GlobalVariable::GeneralDynamicTLSModel: return 1;
1001 case GlobalVariable::LocalDynamicTLSModel: return 2;
1002 case GlobalVariable::InitialExecTLSModel: return 3;
1003 case GlobalVariable::LocalExecTLSModel: return 4;
1004 }
1005 llvm_unreachable("Invalid TLS model")::llvm::llvm_unreachable_internal("Invalid TLS model", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1005)
;
1006}
1007
1008static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1009 switch (C.getSelectionKind()) {
1010 case Comdat::Any:
1011 return bitc::COMDAT_SELECTION_KIND_ANY;
1012 case Comdat::ExactMatch:
1013 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
1014 case Comdat::Largest:
1015 return bitc::COMDAT_SELECTION_KIND_LARGEST;
1016 case Comdat::NoDuplicates:
1017 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
1018 case Comdat::SameSize:
1019 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
1020 }
1021 llvm_unreachable("Invalid selection kind")::llvm::llvm_unreachable_internal("Invalid selection kind", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1021)
;
1022}
1023
1024static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1025 switch (GV.getUnnamedAddr()) {
1026 case GlobalValue::UnnamedAddr::None: return 0;
1027 case GlobalValue::UnnamedAddr::Local: return 2;
1028 case GlobalValue::UnnamedAddr::Global: return 1;
1029 }
1030 llvm_unreachable("Invalid unnamed_addr")::llvm::llvm_unreachable_internal("Invalid unnamed_addr", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1030)
;
1031}
1032
1033size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1034 if (GenerateHash)
1035 Hasher.update(Str);
1036 return StrtabBuilder.add(Str);
1037}
1038
1039void ModuleBitcodeWriter::writeComdats() {
1040 SmallVector<unsigned, 64> Vals;
1041 for (const Comdat *C : VE.getComdats()) {
1042 // COMDAT: [strtab offset, strtab size, selection_kind]
1043 Vals.push_back(addToStrtab(C->getName()));
1044 Vals.push_back(C->getName().size());
1045 Vals.push_back(getEncodedComdatSelectionKind(*C));
1046 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1047 Vals.clear();
1048 }
1049}
1050
1051/// Write a record that will eventually hold the word offset of the
1052/// module-level VST. For now the offset is 0, which will be backpatched
1053/// after the real VST is written. Saves the bit offset to backpatch.
1054void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1055 // Write a placeholder value in for the offset of the real VST,
1056 // which is written after the function blocks so that it can include
1057 // the offset of each function. The placeholder offset will be
1058 // updated when the real VST is written.
1059 auto Abbv = std::make_shared<BitCodeAbbrev>();
1060 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
1061 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1062 // hold the real VST offset. Must use fixed instead of VBR as we don't
1063 // know how many VBR chunks to reserve ahead of time.
1064 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1065 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1066
1067 // Emit the placeholder
1068 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
1069 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
1070
1071 // Compute and save the bit offset to the placeholder, which will be
1072 // patched when the real VST is written. We can simply subtract the 32-bit
1073 // fixed size from the current bit number to get the location to backpatch.
1074 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
1075}
1076
1077enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
1078
1079/// Determine the encoding to use for the given string name and length.
1080static StringEncoding getStringEncoding(StringRef Str) {
1081 bool isChar6 = true;
1082 for (char C : Str) {
1083 if (isChar6)
1084 isChar6 = BitCodeAbbrevOp::isChar6(C);
1085 if ((unsigned char)C & 128)
1086 // don't bother scanning the rest.
1087 return SE_Fixed8;
1088 }
1089 if (isChar6)
1090 return SE_Char6;
1091 return SE_Fixed7;
1092}
1093
1094/// Emit top-level description of module, including target triple, inline asm,
1095/// descriptors for global variables, and function prototype info.
1096/// Returns the bit offset to backpatch with the location of the real VST.
1097void ModuleBitcodeWriter::writeModuleInfo() {
1098 // Emit various pieces of data attached to a module.
1099 if (!M.getTargetTriple().empty())
1100 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1101 0 /*TODO*/);
1102 const std::string &DL = M.getDataLayoutStr();
1103 if (!DL.empty())
1104 writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
1105 if (!M.getModuleInlineAsm().empty())
1106 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1107 0 /*TODO*/);
1108
1109 // Emit information about sections and GC, computing how many there are. Also
1110 // compute the maximum alignment value.
1111 std::map<std::string, unsigned> SectionMap;
1112 std::map<std::string, unsigned> GCMap;
1113 unsigned MaxAlignment = 0;
1114 unsigned MaxGlobalType = 0;
1115 for (const GlobalValue &GV : M.globals()) {
1116 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
1117 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1118 if (GV.hasSection()) {
1119 // Give section names unique ID's.
1120 unsigned &Entry = SectionMap[GV.getSection()];
1121 if (!Entry) {
1122 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1123 0 /*TODO*/);
1124 Entry = SectionMap.size();
1125 }
1126 }
1127 }
1128 for (const Function &F : M) {
1129 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
1130 if (F.hasSection()) {
1131 // Give section names unique ID's.
1132 unsigned &Entry = SectionMap[F.getSection()];
1133 if (!Entry) {
1134 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1135 0 /*TODO*/);
1136 Entry = SectionMap.size();
1137 }
1138 }
1139 if (F.hasGC()) {
1140 // Same for GC names.
1141 unsigned &Entry = GCMap[F.getGC()];
1142 if (!Entry) {
1143 writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
1144 0 /*TODO*/);
1145 Entry = GCMap.size();
1146 }
1147 }
1148 }
1149
1150 // Emit abbrev for globals, now that we know # sections and max alignment.
1151 unsigned SimpleGVarAbbrev = 0;
1152 if (!M.global_empty()) {
1153 // Add an abbrev for common globals with no visibility or thread localness.
1154 auto Abbv = std::make_shared<BitCodeAbbrev>();
1155 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1156 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1157 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1159 Log2_32_Ceil(MaxGlobalType+1)));
1160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1161 //| explicitType << 1
1162 //| constant
1163 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1164 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1165 if (MaxAlignment == 0) // Alignment.
1166 Abbv->Add(BitCodeAbbrevOp(0));
1167 else {
1168 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
1169 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1170 Log2_32_Ceil(MaxEncAlignment+1)));
1171 }
1172 if (SectionMap.empty()) // Section.
1173 Abbv->Add(BitCodeAbbrevOp(0));
1174 else
1175 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1176 Log2_32_Ceil(SectionMap.size()+1)));
1177 // Don't bother emitting vis + thread local.
1178 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1179 }
1180
1181 SmallVector<unsigned, 64> Vals;
1182 // Emit the module's source file name.
1183 {
1184 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
1185 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1186 if (Bits == SE_Char6)
1187 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1188 else if (Bits == SE_Fixed7)
1189 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1190
1191 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1192 auto Abbv = std::make_shared<BitCodeAbbrev>();
1193 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1195 Abbv->Add(AbbrevOpToUse);
1196 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1197
1198 for (const auto P : M.getSourceFileName())
1199 Vals.push_back((unsigned char)P);
1200
1201 // Emit the finished record.
1202 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1203 Vals.clear();
1204 }
1205
1206 // Emit the global variable information.
1207 for (const GlobalVariable &GV : M.globals()) {
1208 unsigned AbbrevToUse = 0;
1209
1210 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
1211 // linkage, alignment, section, visibility, threadlocal,
1212 // unnamed_addr, externally_initialized, dllstorageclass,
1213 // comdat, attributes, DSO_Local]
1214 Vals.push_back(addToStrtab(GV.getName()));
1215 Vals.push_back(GV.getName().size());
1216 Vals.push_back(VE.getTypeID(GV.getValueType()));
1217 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1218 Vals.push_back(GV.isDeclaration() ? 0 :
1219 (VE.getValueID(GV.getInitializer()) + 1));
1220 Vals.push_back(getEncodedLinkage(GV));
1221 Vals.push_back(Log2_32(GV.getAlignment())+1);
1222 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
1223 if (GV.isThreadLocal() ||
1224 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1225 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1226 GV.isExternallyInitialized() ||
1227 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1228 GV.hasComdat() ||
1229 GV.hasAttributes() ||
1230 GV.isDSOLocal()) {
1231 Vals.push_back(getEncodedVisibility(GV));
1232 Vals.push_back(getEncodedThreadLocalMode(GV));
1233 Vals.push_back(getEncodedUnnamedAddr(GV));
1234 Vals.push_back(GV.isExternallyInitialized());
1235 Vals.push_back(getEncodedDLLStorageClass(GV));
1236 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1237
1238 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1239 Vals.push_back(VE.getAttributeListID(AL));
1240
1241 Vals.push_back(GV.isDSOLocal());
1242 } else {
1243 AbbrevToUse = SimpleGVarAbbrev;
1244 }
1245
1246 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1247 Vals.clear();
1248 }
1249
1250 // Emit the function proto information.
1251 for (const Function &F : M) {
1252 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1253 // linkage, paramattrs, alignment, section, visibility, gc,
1254 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1255 // prefixdata, personalityfn, DSO_Local]
1256 Vals.push_back(addToStrtab(F.getName()));
1257 Vals.push_back(F.getName().size());
1258 Vals.push_back(VE.getTypeID(F.getFunctionType()));
1259 Vals.push_back(F.getCallingConv());
1260 Vals.push_back(F.isDeclaration());
1261 Vals.push_back(getEncodedLinkage(F));
1262 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1263 Vals.push_back(Log2_32(F.getAlignment())+1);
1264 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
1265 Vals.push_back(getEncodedVisibility(F));
1266 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1267 Vals.push_back(getEncodedUnnamedAddr(F));
1268 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1269 : 0);
1270 Vals.push_back(getEncodedDLLStorageClass(F));
1271 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1272 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1273 : 0);
1274 Vals.push_back(
1275 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1276
1277 Vals.push_back(F.isDSOLocal());
1278 unsigned AbbrevToUse = 0;
1279 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1280 Vals.clear();
1281 }
1282
1283 // Emit the alias information.
1284 for (const GlobalAlias &A : M.aliases()) {
1285 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1286 // visibility, dllstorageclass, threadlocal, unnamed_addr,
1287 // DSO_Local]
1288 Vals.push_back(addToStrtab(A.getName()));
1289 Vals.push_back(A.getName().size());
1290 Vals.push_back(VE.getTypeID(A.getValueType()));
1291 Vals.push_back(A.getType()->getAddressSpace());
1292 Vals.push_back(VE.getValueID(A.getAliasee()));
1293 Vals.push_back(getEncodedLinkage(A));
1294 Vals.push_back(getEncodedVisibility(A));
1295 Vals.push_back(getEncodedDLLStorageClass(A));
1296 Vals.push_back(getEncodedThreadLocalMode(A));
1297 Vals.push_back(getEncodedUnnamedAddr(A));
1298 Vals.push_back(A.isDSOLocal());
1299
1300 unsigned AbbrevToUse = 0;
1301 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
1302 Vals.clear();
1303 }
1304
1305 // Emit the ifunc information.
1306 for (const GlobalIFunc &I : M.ifuncs()) {
1307 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1308 // val#, linkage, visibility, DSO_Local]
1309 Vals.push_back(addToStrtab(I.getName()));
1310 Vals.push_back(I.getName().size());
1311 Vals.push_back(VE.getTypeID(I.getValueType()));
1312 Vals.push_back(I.getType()->getAddressSpace());
1313 Vals.push_back(VE.getValueID(I.getResolver()));
1314 Vals.push_back(getEncodedLinkage(I));
1315 Vals.push_back(getEncodedVisibility(I));
1316 Vals.push_back(I.isDSOLocal());
1317 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
1318 Vals.clear();
1319 }
1320
1321 writeValueSymbolTableForwardDecl();
1322}
1323
1324static uint64_t getOptimizationFlags(const Value *V) {
1325 uint64_t Flags = 0;
1326
1327 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1328 if (OBO->hasNoSignedWrap())
1329 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1330 if (OBO->hasNoUnsignedWrap())
1331 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
1332 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1333 if (PEO->isExact())
1334 Flags |= 1 << bitc::PEO_EXACT;
1335 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1336 if (FPMO->hasAllowReassoc())
1337 Flags |= bitc::AllowReassoc;
1338 if (FPMO->hasNoNaNs())
1339 Flags |= bitc::NoNaNs;
1340 if (FPMO->hasNoInfs())
1341 Flags |= bitc::NoInfs;
1342 if (FPMO->hasNoSignedZeros())
1343 Flags |= bitc::NoSignedZeros;
1344 if (FPMO->hasAllowReciprocal())
1345 Flags |= bitc::AllowReciprocal;
1346 if (FPMO->hasAllowContract())
1347 Flags |= bitc::AllowContract;
1348 if (FPMO->hasApproxFunc())
1349 Flags |= bitc::ApproxFunc;
1350 }
1351
1352 return Flags;
1353}
1354
1355void ModuleBitcodeWriter::writeValueAsMetadata(
1356 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1357 // Mimic an MDNode with a value as one operand.
1358 Value *V = MD->getValue();
1359 Record.push_back(VE.getTypeID(V->getType()));
1360 Record.push_back(VE.getValueID(V));
1361 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
1362 Record.clear();
1363}
1364
1365void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1366 SmallVectorImpl<uint64_t> &Record,
1367 unsigned Abbrev) {
1368 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1369 Metadata *MD = N->getOperand(i);
1370 assert(!(MD && isa<LocalAsMetadata>(MD)) &&(static_cast <bool> (!(MD && isa<LocalAsMetadata
>(MD)) && "Unexpected function-local metadata") ? void
(0) : __assert_fail ("!(MD && isa<LocalAsMetadata>(MD)) && \"Unexpected function-local metadata\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1371, __extension__ __PRETTY_FUNCTION__))
1371 "Unexpected function-local metadata")(static_cast <bool> (!(MD && isa<LocalAsMetadata
>(MD)) && "Unexpected function-local metadata") ? void
(0) : __assert_fail ("!(MD && isa<LocalAsMetadata>(MD)) && \"Unexpected function-local metadata\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1371, __extension__ __PRETTY_FUNCTION__))
;
1372 Record.push_back(VE.getMetadataOrNullID(MD));
1373 }
1374 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1375 : bitc::METADATA_NODE,
1376 Record, Abbrev);
1377 Record.clear();
1378}
1379
1380unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1381 // Assume the column is usually under 128, and always output the inlined-at
1382 // location (it's never more expensive than building an array size 1).
1383 auto Abbv = std::make_shared<BitCodeAbbrev>();
1384 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1385 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1386 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1387 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1388 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1389 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1390 return Stream.EmitAbbrev(std::move(Abbv));
1391}
1392
1393void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1394 SmallVectorImpl<uint64_t> &Record,
1395 unsigned &Abbrev) {
1396 if (!Abbrev)
1397 Abbrev = createDILocationAbbrev();
1398
1399 Record.push_back(N->isDistinct());
1400 Record.push_back(N->getLine());
1401 Record.push_back(N->getColumn());
1402 Record.push_back(VE.getMetadataID(N->getScope()));
1403 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1404
1405 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1406 Record.clear();
1407}
1408
1409unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1410 // Assume the column is usually under 128, and always output the inlined-at
1411 // location (it's never more expensive than building an array size 1).
1412 auto Abbv = std::make_shared<BitCodeAbbrev>();
1413 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1414 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1415 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1416 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1417 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1418 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1419 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1420 return Stream.EmitAbbrev(std::move(Abbv));
1421}
1422
1423void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1424 SmallVectorImpl<uint64_t> &Record,
1425 unsigned &Abbrev) {
1426 if (!Abbrev)
1427 Abbrev = createGenericDINodeAbbrev();
1428
1429 Record.push_back(N->isDistinct());
1430 Record.push_back(N->getTag());
1431 Record.push_back(0); // Per-tag version field; unused for now.
1432
1433 for (auto &I : N->operands())
1434 Record.push_back(VE.getMetadataOrNullID(I));
1435
1436 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
1437 Record.clear();
1438}
1439
1440static uint64_t rotateSign(int64_t I) {
1441 uint64_t U = I;
1442 return I < 0 ? ~(U << 1) : U << 1;
1443}
1444
1445void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1446 SmallVectorImpl<uint64_t> &Record,
1447 unsigned Abbrev) {
1448 const uint64_t Version = 1 << 1;
1449 Record.push_back((uint64_t)N->isDistinct() | Version);
1450 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1451 Record.push_back(rotateSign(N->getLowerBound()));
1452
1453 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1454 Record.clear();
1455}
1456
1457void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1458 SmallVectorImpl<uint64_t> &Record,
1459 unsigned Abbrev) {
1460 Record.push_back((N->isUnsigned() << 1) | N->isDistinct());
1461 Record.push_back(rotateSign(N->getValue()));
1462 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1463
1464 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
1465 Record.clear();
1466}
1467
1468void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1469 SmallVectorImpl<uint64_t> &Record,
1470 unsigned Abbrev) {
1471 Record.push_back(N->isDistinct());
1472 Record.push_back(N->getTag());
1473 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1474 Record.push_back(N->getSizeInBits());
1475 Record.push_back(N->getAlignInBits());
1476 Record.push_back(N->getEncoding());
1477
1478 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
1479 Record.clear();
1480}
1481
1482void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1483 SmallVectorImpl<uint64_t> &Record,
1484 unsigned Abbrev) {
1485 Record.push_back(N->isDistinct());
1486 Record.push_back(N->getTag());
1487 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1488 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1489 Record.push_back(N->getLine());
1490 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1491 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1492 Record.push_back(N->getSizeInBits());
1493 Record.push_back(N->getAlignInBits());
1494 Record.push_back(N->getOffsetInBits());
1495 Record.push_back(N->getFlags());
1496 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1497
1498 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1499 // that there is no DWARF address space associated with DIDerivedType.
1500 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1501 Record.push_back(*DWARFAddressSpace + 1);
1502 else
1503 Record.push_back(0);
1504
1505 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
1506 Record.clear();
1507}
1508
1509void ModuleBitcodeWriter::writeDICompositeType(
1510 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1511 unsigned Abbrev) {
1512 const unsigned IsNotUsedInOldTypeRef = 0x2;
1513 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
1514 Record.push_back(N->getTag());
1515 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1516 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1517 Record.push_back(N->getLine());
1518 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1519 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1520 Record.push_back(N->getSizeInBits());
1521 Record.push_back(N->getAlignInBits());
1522 Record.push_back(N->getOffsetInBits());
1523 Record.push_back(N->getFlags());
1524 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1525 Record.push_back(N->getRuntimeLang());
1526 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1527 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1528 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1529 Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
1530
1531 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
1532 Record.clear();
1533}
1534
1535void ModuleBitcodeWriter::writeDISubroutineType(
1536 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1537 unsigned Abbrev) {
1538 const unsigned HasNoOldTypeRefs = 0x2;
1539 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
1540 Record.push_back(N->getFlags());
1541 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1542 Record.push_back(N->getCC());
1543
1544 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
1545 Record.clear();
1546}
1547
1548void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1549 SmallVectorImpl<uint64_t> &Record,
1550 unsigned Abbrev) {
1551 Record.push_back(N->isDistinct());
1552 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1553 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1554 if (N->getRawChecksum()) {
1555 Record.push_back(N->getRawChecksum()->Kind);
1556 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
1557 } else {
1558 // Maintain backwards compatibility with the old internal representation of
1559 // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
1560 Record.push_back(0);
1561 Record.push_back(VE.getMetadataOrNullID(nullptr));
1562 }
1563 auto Source = N->getRawSource();
1564 if (Source)
1565 Record.push_back(VE.getMetadataOrNullID(*Source));
1566
1567 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1568 Record.clear();
1569}
1570
1571void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1572 SmallVectorImpl<uint64_t> &Record,
1573 unsigned Abbrev) {
1574 assert(N->isDistinct() && "Expected distinct compile units")(static_cast <bool> (N->isDistinct() && "Expected distinct compile units"
) ? void (0) : __assert_fail ("N->isDistinct() && \"Expected distinct compile units\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1574, __extension__ __PRETTY_FUNCTION__))
;
1575 Record.push_back(/* IsDistinct */ true);
1576 Record.push_back(N->getSourceLanguage());
1577 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1578 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1579 Record.push_back(N->isOptimized());
1580 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1581 Record.push_back(N->getRuntimeVersion());
1582 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1583 Record.push_back(N->getEmissionKind());
1584 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1585 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1586 Record.push_back(/* subprograms */ 0);
1587 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1588 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1589 Record.push_back(N->getDWOId());
1590 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
1591 Record.push_back(N->getSplitDebugInlining());
1592 Record.push_back(N->getDebugInfoForProfiling());
1593 Record.push_back(N->getGnuPubnames());
1594
1595 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1596 Record.clear();
1597}
1598
1599void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1600 SmallVectorImpl<uint64_t> &Record,
1601 unsigned Abbrev) {
1602 uint64_t HasUnitFlag = 1 << 1;
1603 Record.push_back(N->isDistinct() | HasUnitFlag);
1604 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1605 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1606 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1607 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1608 Record.push_back(N->getLine());
1609 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1610 Record.push_back(N->isLocalToUnit());
1611 Record.push_back(N->isDefinition());
1612 Record.push_back(N->getScopeLine());
1613 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1614 Record.push_back(N->getVirtuality());
1615 Record.push_back(N->getVirtualIndex());
1616 Record.push_back(N->getFlags());
1617 Record.push_back(N->isOptimized());
1618 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
1619 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1620 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1621 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
1622 Record.push_back(N->getThisAdjustment());
1623 Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
1624
1625 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1626 Record.clear();
1627}
1628
1629void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1630 SmallVectorImpl<uint64_t> &Record,
1631 unsigned Abbrev) {
1632 Record.push_back(N->isDistinct());
1633 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1634 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1635 Record.push_back(N->getLine());
1636 Record.push_back(N->getColumn());
1637
1638 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
1639 Record.clear();
1640}
1641
1642void ModuleBitcodeWriter::writeDILexicalBlockFile(
1643 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1644 unsigned Abbrev) {
1645 Record.push_back(N->isDistinct());
1646 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1647 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1648 Record.push_back(N->getDiscriminator());
1649
1650 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
1651 Record.clear();
1652}
1653
1654void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1655 SmallVectorImpl<uint64_t> &Record,
1656 unsigned Abbrev) {
1657 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
1658 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1659 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1660
1661 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1662 Record.clear();
1663}
1664
1665void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1666 SmallVectorImpl<uint64_t> &Record,
1667 unsigned Abbrev) {
1668 Record.push_back(N->isDistinct());
1669 Record.push_back(N->getMacinfoType());
1670 Record.push_back(N->getLine());
1671 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1672 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1673
1674 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
1675 Record.clear();
1676}
1677
1678void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1679 SmallVectorImpl<uint64_t> &Record,
1680 unsigned Abbrev) {
1681 Record.push_back(N->isDistinct());
1682 Record.push_back(N->getMacinfoType());
1683 Record.push_back(N->getLine());
1684 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1685 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1686
1687 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
1688 Record.clear();
1689}
1690
1691void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1692 SmallVectorImpl<uint64_t> &Record,
1693 unsigned Abbrev) {
1694 Record.push_back(N->isDistinct());
1695 for (auto &I : N->operands())
1696 Record.push_back(VE.getMetadataOrNullID(I));
1697
1698 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1699 Record.clear();
1700}
1701
1702void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1703 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1704 unsigned Abbrev) {
1705 Record.push_back(N->isDistinct());
1706 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1707 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1708
1709 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
1710 Record.clear();
1711}
1712
1713void ModuleBitcodeWriter::writeDITemplateValueParameter(
1714 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1715 unsigned Abbrev) {
1716 Record.push_back(N->isDistinct());
1717 Record.push_back(N->getTag());
1718 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1719 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1720 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1721
1722 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
1723 Record.clear();
1724}
1725
1726void ModuleBitcodeWriter::writeDIGlobalVariable(
1727 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1728 unsigned Abbrev) {
1729 const uint64_t Version = 1 << 1;
1730 Record.push_back((uint64_t)N->isDistinct() | Version);
1731 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1732 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1733 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1734 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1735 Record.push_back(N->getLine());
1736 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1737 Record.push_back(N->isLocalToUnit());
1738 Record.push_back(N->isDefinition());
1739 Record.push_back(/* expr */ 0);
1740 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1741 Record.push_back(N->getAlignInBits());
1742
1743 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1744 Record.clear();
1745}
1746
1747void ModuleBitcodeWriter::writeDILocalVariable(
1748 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1749 unsigned Abbrev) {
1750 // In order to support all possible bitcode formats in BitcodeReader we need
1751 // to distinguish the following cases:
1752 // 1) Record has no artificial tag (Record[1]),
1753 // has no obsolete inlinedAt field (Record[9]).
1754 // In this case Record size will be 8, HasAlignment flag is false.
1755 // 2) Record has artificial tag (Record[1]),
1756 // has no obsolete inlignedAt field (Record[9]).
1757 // In this case Record size will be 9, HasAlignment flag is false.
1758 // 3) Record has both artificial tag (Record[1]) and
1759 // obsolete inlignedAt field (Record[9]).
1760 // In this case Record size will be 10, HasAlignment flag is false.
1761 // 4) Record has neither artificial tag, nor inlignedAt field, but
1762 // HasAlignment flag is true and Record[8] contains alignment value.
1763 const uint64_t HasAlignmentFlag = 1 << 1;
1764 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
1765 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1766 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1767 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1768 Record.push_back(N->getLine());
1769 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1770 Record.push_back(N->getArg());
1771 Record.push_back(N->getFlags());
1772 Record.push_back(N->getAlignInBits());
1773
1774 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1775 Record.clear();
1776}
1777
1778void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1779 SmallVectorImpl<uint64_t> &Record,
1780 unsigned Abbrev) {
1781 Record.reserve(N->getElements().size() + 1);
1782 const uint64_t Version = 3 << 1;
1783 Record.push_back((uint64_t)N->isDistinct() | Version);
1784 Record.append(N->elements_begin(), N->elements_end());
1785
1786 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1787 Record.clear();
1788}
1789
1790void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
1791 const DIGlobalVariableExpression *N, SmallVectorImpl<uint64_t> &Record,
1792 unsigned Abbrev) {
1793 Record.push_back(N->isDistinct());
1794 Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
1795 Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
1796
1797 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR_EXPR, Record, Abbrev);
1798 Record.clear();
1799}
1800
1801void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1802 SmallVectorImpl<uint64_t> &Record,
1803 unsigned Abbrev) {
1804 Record.push_back(N->isDistinct());
1805 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1806 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1807 Record.push_back(N->getLine());
1808 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1809 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1810 Record.push_back(N->getAttributes());
1811 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1812
1813 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
1814 Record.clear();
1815}
1816
1817void ModuleBitcodeWriter::writeDIImportedEntity(
1818 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1819 unsigned Abbrev) {
1820 Record.push_back(N->isDistinct());
1821 Record.push_back(N->getTag());
1822 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1823 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1824 Record.push_back(N->getLine());
1825 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1826 Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
1827
1828 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
1829 Record.clear();
1830}
1831
1832unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
1833 auto Abbv = std::make_shared<BitCodeAbbrev>();
1834 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1835 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1836 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1837 return Stream.EmitAbbrev(std::move(Abbv));
1838}
1839
1840void ModuleBitcodeWriter::writeNamedMetadata(
1841 SmallVectorImpl<uint64_t> &Record) {
1842 if (M.named_metadata_empty())
1843 return;
1844
1845 unsigned Abbrev = createNamedMetadataAbbrev();
1846 for (const NamedMDNode &NMD : M.named_metadata()) {
1847 // Write name.
1848 StringRef Str = NMD.getName();
1849 Record.append(Str.bytes_begin(), Str.bytes_end());
1850 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
1851 Record.clear();
1852
1853 // Write named metadata operands.
1854 for (const MDNode *N : NMD.operands())
1855 Record.push_back(VE.getMetadataID(N));
1856 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
1857 Record.clear();
1858 }
1859}
1860
1861unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
1862 auto Abbv = std::make_shared<BitCodeAbbrev>();
1863 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1864 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1865 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1866 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1867 return Stream.EmitAbbrev(std::move(Abbv));
1868}
1869
1870/// Write out a record for MDString.
1871///
1872/// All the metadata strings in a metadata block are emitted in a single
1873/// record. The sizes and strings themselves are shoved into a blob.
1874void ModuleBitcodeWriter::writeMetadataStrings(
1875 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
1876 if (Strings.empty())
1877 return;
1878
1879 // Start the record with the number of strings.
1880 Record.push_back(bitc::METADATA_STRINGS);
1881 Record.push_back(Strings.size());
1882
1883 // Emit the sizes of the strings in the blob.
1884 SmallString<256> Blob;
1885 {
1886 BitstreamWriter W(Blob);
1887 for (const Metadata *MD : Strings)
1888 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1889 W.FlushToWord();
1890 }
1891
1892 // Add the offset to the strings to the record.
1893 Record.push_back(Blob.size());
1894
1895 // Add the strings to the blob.
1896 for (const Metadata *MD : Strings)
1897 Blob.append(cast<MDString>(MD)->getString());
1898
1899 // Emit the final record.
1900 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
1901 Record.clear();
1902}
1903
1904// Generates an enum to use as an index in the Abbrev array of Metadata record.
1905enum MetadataAbbrev : unsigned {
1906#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
1907#include "llvm/IR/Metadata.def"
1908 LastPlusOne
1909};
1910
1911void ModuleBitcodeWriter::writeMetadataRecords(
1912 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record,
1913 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
1914 if (MDs.empty())
1915 return;
1916
1917 // Initialize MDNode abbreviations.
1918#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1919#include "llvm/IR/Metadata.def"
1920
1921 for (const Metadata *MD : MDs) {
1922 if (IndexPos)
1923 IndexPos->push_back(Stream.GetCurrentBitNo());
1924 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1925 assert(N->isResolved() && "Expected forward references to be resolved")(static_cast <bool> (N->isResolved() && "Expected forward references to be resolved"
) ? void (0) : __assert_fail ("N->isResolved() && \"Expected forward references to be resolved\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1925, __extension__ __PRETTY_FUNCTION__))
;
1926
1927 switch (N->getMetadataID()) {
1928 default:
1929 llvm_unreachable("Invalid MDNode subclass")::llvm::llvm_unreachable_internal("Invalid MDNode subclass", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 1929)
;
1930#define HANDLE_MDNODE_LEAF(CLASS) \
1931 case Metadata::CLASS##Kind: \
1932 if (MDAbbrevs) \
1933 write##CLASS(cast<CLASS>(N), Record, \
1934 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
1935 else \
1936 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
1937 continue;
1938#include "llvm/IR/Metadata.def"
1939 }
1940 }
1941 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
1942 }
1943}
1944
1945void ModuleBitcodeWriter::writeModuleMetadata() {
1946 if (!VE.hasMDs() && M.named_metadata_empty())
1947 return;
1948
1949 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4);
1950 SmallVector<uint64_t, 64> Record;
1951
1952 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
1953 // block and load any metadata.
1954 std::vector<unsigned> MDAbbrevs;
1955
1956 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
1957 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
1958 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
1959 createGenericDINodeAbbrev();
1960
1961 auto Abbv = std::make_shared<BitCodeAbbrev>();
1962 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX_OFFSET));
1963 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1964 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1965 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1966
1967 Abbv = std::make_shared<BitCodeAbbrev>();
1968 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX));
1969 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1970 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1971 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1972
1973 // Emit MDStrings together upfront.
1974 writeMetadataStrings(VE.getMDStrings(), Record);
1975
1976 // We only emit an index for the metadata record if we have more than a given
1977 // (naive) threshold of metadatas, otherwise it is not worth it.
1978 if (VE.getNonMDStrings().size() > IndexThreshold) {
1979 // Write a placeholder value in for the offset of the metadata index,
1980 // which is written after the records, so that it can include
1981 // the offset of each entry. The placeholder offset will be
1982 // updated after all records are emitted.
1983 uint64_t Vals[] = {0, 0};
1984 Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
1985 }
1986
1987 // Compute and save the bit offset to the current position, which will be
1988 // patched when we emit the index later. We can simply subtract the 64-bit
1989 // fixed size from the current bit number to get the location to backpatch.
1990 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
1991
1992 // This index will contain the bitpos for each individual record.
1993 std::vector<uint64_t> IndexPos;
1994 IndexPos.reserve(VE.getNonMDStrings().size());
1995
1996 // Write all the records
1997 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
1998
1999 if (VE.getNonMDStrings().size() > IndexThreshold) {
2000 // Now that we have emitted all the records we will emit the index. But
2001 // first
2002 // backpatch the forward reference so that the reader can skip the records
2003 // efficiently.
2004 Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
2005 Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
2006
2007 // Delta encode the index.
2008 uint64_t PreviousValue = IndexOffsetRecordBitPos;
2009 for (auto &Elt : IndexPos) {
2010 auto EltDelta = Elt - PreviousValue;
2011 PreviousValue = Elt;
2012 Elt = EltDelta;
2013 }
2014 // Emit the index record.
2015 Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
2016 IndexPos.clear();
2017 }
2018
2019 // Write the named metadata now.
2020 writeNamedMetadata(Record);
2021
2022 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
2023 SmallVector<uint64_t, 4> Record;
2024 Record.push_back(VE.getValueID(&GO));
2025 pushGlobalMetadataAttachment(Record, GO);
2026 Stream.EmitRecord(bitc::METADATA_GLOBAL_DECL_ATTACHMENT, Record);
2027 };
2028 for (const Function &F : M)
2029 if (F.isDeclaration() && F.hasMetadata())
2030 AddDeclAttachedMetadata(F);
2031 // FIXME: Only store metadata for declarations here, and move data for global
2032 // variable definitions to a separate block (PR28134).
2033 for (const GlobalVariable &GV : M.globals())
2034 if (GV.hasMetadata())
2035 AddDeclAttachedMetadata(GV);
2036
2037 Stream.ExitBlock();
2038}
2039
2040void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
2041 if (!VE.hasMDs())
2042 return;
2043
2044 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
2045 SmallVector<uint64_t, 64> Record;
2046 writeMetadataStrings(VE.getMDStrings(), Record);
2047 writeMetadataRecords(VE.getNonMDStrings(), Record);
2048 Stream.ExitBlock();
2049}
2050
2051void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2052 SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) {
2053 // [n x [id, mdnode]]
2054 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2055 GO.getAllMetadata(MDs);
2056 for (const auto &I : MDs) {
2057 Record.push_back(I.first);
2058 Record.push_back(VE.getMetadataID(I.second));
2059 }
2060}
2061
2062void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
2063 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
2064
2065 SmallVector<uint64_t, 64> Record;
2066
2067 if (F.hasMetadata()) {
2068 pushGlobalMetadataAttachment(Record, F);
2069 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
2070 Record.clear();
2071 }
2072
2073 // Write metadata attachments
2074 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
2075 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2076 for (const BasicBlock &BB : F)
2077 for (const Instruction &I : BB) {
2078 MDs.clear();
2079 I.getAllMetadataOtherThanDebugLoc(MDs);
2080
2081 // If no metadata, ignore instruction.
2082 if (MDs.empty()) continue;
2083
2084 Record.push_back(VE.getInstructionID(&I));
2085
2086 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
2087 Record.push_back(MDs[i].first);
2088 Record.push_back(VE.getMetadataID(MDs[i].second));
2089 }
2090 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
2091 Record.clear();
2092 }
2093
2094 Stream.ExitBlock();
2095}
2096
2097void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2098 SmallVector<uint64_t, 64> Record;
2099
2100 // Write metadata kinds
2101 // METADATA_KIND - [n x [id, name]]
2102 SmallVector<StringRef, 8> Names;
2103 M.getMDKindNames(Names);
2104
2105 if (Names.empty()) return;
2106
2107 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
2108
2109 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
2110 Record.push_back(MDKindID);
2111 StringRef KName = Names[MDKindID];
2112 Record.append(KName.begin(), KName.end());
2113
2114 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
2115 Record.clear();
2116 }
2117
2118 Stream.ExitBlock();
2119}
2120
2121void ModuleBitcodeWriter::writeOperandBundleTags() {
2122 // Write metadata kinds
2123 //
2124 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2125 //
2126 // OPERAND_BUNDLE_TAG - [strchr x N]
2127
2128 SmallVector<StringRef, 8> Tags;
2129 M.getOperandBundleTags(Tags);
2130
2131 if (Tags.empty())
2132 return;
2133
2134 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
2135
2136 SmallVector<uint64_t, 64> Record;
2137
2138 for (auto Tag : Tags) {
2139 Record.append(Tag.begin(), Tag.end());
2140
2141 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
2142 Record.clear();
2143 }
2144
2145 Stream.ExitBlock();
2146}
2147
2148void ModuleBitcodeWriter::writeSyncScopeNames() {
2149 SmallVector<StringRef, 8> SSNs;
2150 M.getContext().getSyncScopeNames(SSNs);
2151 if (SSNs.empty())
2152 return;
2153
2154 Stream.EnterSubblock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID, 2);
2155
2156 SmallVector<uint64_t, 64> Record;
2157 for (auto SSN : SSNs) {
2158 Record.append(SSN.begin(), SSN.end());
2159 Stream.EmitRecord(bitc::SYNC_SCOPE_NAME, Record, 0);
2160 Record.clear();
2161 }
2162
2163 Stream.ExitBlock();
2164}
2165
2166static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
2167 if ((int64_t)V >= 0)
2168 Vals.push_back(V << 1);
2169 else
2170 Vals.push_back((-V << 1) | 1);
2171}
2172
2173void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2174 bool isGlobal) {
2175 if (FirstVal == LastVal) return;
2176
2177 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
2178
2179 unsigned AggregateAbbrev = 0;
2180 unsigned String8Abbrev = 0;
2181 unsigned CString7Abbrev = 0;
2182 unsigned CString6Abbrev = 0;
2183 // If this is a constant pool for the module, emit module-specific abbrevs.
2184 if (isGlobal) {
2185 // Abbrev for CST_CODE_AGGREGATE.
2186 auto Abbv = std::make_shared<BitCodeAbbrev>();
2187 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
2188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2189 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
2190 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2191
2192 // Abbrev for CST_CODE_STRING.
2193 Abbv = std::make_shared<BitCodeAbbrev>();
2194 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
2195 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2196 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2197 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2198 // Abbrev for CST_CODE_CSTRING.
2199 Abbv = std::make_shared<BitCodeAbbrev>();
2200 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2201 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2202 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2203 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2204 // Abbrev for CST_CODE_CSTRING.
2205 Abbv = std::make_shared<BitCodeAbbrev>();
2206 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2207 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2208 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2209 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2210 }
2211
2212 SmallVector<uint64_t, 64> Record;
2213
2214 const ValueEnumerator::ValueList &Vals = VE.getValues();
2215 Type *LastTy = nullptr;
2216 for (unsigned i = FirstVal; i != LastVal; ++i) {
2217 const Value *V = Vals[i].first;
2218 // If we need to switch types, do so now.
2219 if (V->getType() != LastTy) {
2220 LastTy = V->getType();
2221 Record.push_back(VE.getTypeID(LastTy));
2222 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
2223 CONSTANTS_SETTYPE_ABBREV);
2224 Record.clear();
2225 }
2226
2227 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2228 Record.push_back(unsigned(IA->hasSideEffects()) |
2229 unsigned(IA->isAlignStack()) << 1 |
2230 unsigned(IA->getDialect()&1) << 2);
2231
2232 // Add the asm string.
2233 const std::string &AsmStr = IA->getAsmString();
2234 Record.push_back(AsmStr.size());
2235 Record.append(AsmStr.begin(), AsmStr.end());
2236
2237 // Add the constraint string.
2238 const std::string &ConstraintStr = IA->getConstraintString();
2239 Record.push_back(ConstraintStr.size());
2240 Record.append(ConstraintStr.begin(), ConstraintStr.end());
2241 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
2242 Record.clear();
2243 continue;
2244 }
2245 const Constant *C = cast<Constant>(V);
2246 unsigned Code = -1U;
2247 unsigned AbbrevToUse = 0;
2248 if (C->isNullValue()) {
2249 Code = bitc::CST_CODE_NULL;
2250 } else if (isa<UndefValue>(C)) {
2251 Code = bitc::CST_CODE_UNDEF;
2252 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
2253 if (IV->getBitWidth() <= 64) {
2254 uint64_t V = IV->getSExtValue();
2255 emitSignedInt64(Record, V);
2256 Code = bitc::CST_CODE_INTEGER;
2257 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2258 } else { // Wide integers, > 64 bits in size.
2259 // We have an arbitrary precision integer value to write whose
2260 // bit width is > 64. However, in canonical unsigned integer
2261 // format it is likely that the high bits are going to be zero.
2262 // So, we only write the number of active words.
2263 unsigned NWords = IV->getValue().getActiveWords();
2264 const uint64_t *RawWords = IV->getValue().getRawData();
2265 for (unsigned i = 0; i != NWords; ++i) {
2266 emitSignedInt64(Record, RawWords[i]);
2267 }
2268 Code = bitc::CST_CODE_WIDE_INTEGER;
2269 }
2270 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2271 Code = bitc::CST_CODE_FLOAT;
2272 Type *Ty = CFP->getType();
2273 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
2274 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2275 } else if (Ty->isX86_FP80Ty()) {
2276 // api needed to prevent premature destruction
2277 // bits are not in the same order as a normal i80 APInt, compensate.
2278 APInt api = CFP->getValueAPF().bitcastToAPInt();
2279 const uint64_t *p = api.getRawData();
2280 Record.push_back((p[1] << 48) | (p[0] >> 16));
2281 Record.push_back(p[0] & 0xffffLL);
2282 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2283 APInt api = CFP->getValueAPF().bitcastToAPInt();
2284 const uint64_t *p = api.getRawData();
2285 Record.push_back(p[0]);
2286 Record.push_back(p[1]);
2287 } else {
2288 assert(0 && "Unknown FP type!")(static_cast <bool> (0 && "Unknown FP type!") ?
void (0) : __assert_fail ("0 && \"Unknown FP type!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2288, __extension__ __PRETTY_FUNCTION__))
;
2289 }
2290 } else if (isa<ConstantDataSequential>(C) &&
2291 cast<ConstantDataSequential>(C)->isString()) {
2292 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2293 // Emit constant strings specially.
2294 unsigned NumElts = Str->getNumElements();
2295 // If this is a null-terminated string, use the denser CSTRING encoding.
2296 if (Str->isCString()) {
2297 Code = bitc::CST_CODE_CSTRING;
2298 --NumElts; // Don't encode the null, which isn't allowed by char6.
2299 } else {
2300 Code = bitc::CST_CODE_STRING;
2301 AbbrevToUse = String8Abbrev;
2302 }
2303 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2304 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2305 for (unsigned i = 0; i != NumElts; ++i) {
2306 unsigned char V = Str->getElementAsInteger(i);
2307 Record.push_back(V);
2308 isCStr7 &= (V & 128) == 0;
2309 if (isCStrChar6)
2310 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2311 }
2312
2313 if (isCStrChar6)
2314 AbbrevToUse = CString6Abbrev;
2315 else if (isCStr7)
2316 AbbrevToUse = CString7Abbrev;
2317 } else if (const ConstantDataSequential *CDS =
2318 dyn_cast<ConstantDataSequential>(C)) {
2319 Code = bitc::CST_CODE_DATA;
2320 Type *EltTy = CDS->getType()->getElementType();
2321 if (isa<IntegerType>(EltTy)) {
2322 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2323 Record.push_back(CDS->getElementAsInteger(i));
2324 } else {
2325 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2326 Record.push_back(
2327 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
2328 }
2329 } else if (isa<ConstantAggregate>(C)) {
2330 Code = bitc::CST_CODE_AGGREGATE;
2331 for (const Value *Op : C->operands())
2332 Record.push_back(VE.getValueID(Op));
2333 AbbrevToUse = AggregateAbbrev;
2334 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2335 switch (CE->getOpcode()) {
2336 default:
2337 if (Instruction::isCast(CE->getOpcode())) {
2338 Code = bitc::CST_CODE_CE_CAST;
2339 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2340 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2341 Record.push_back(VE.getValueID(C->getOperand(0)));
2342 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2343 } else {
2344 assert(CE->getNumOperands() == 2 && "Unknown constant expr!")(static_cast <bool> (CE->getNumOperands() == 2 &&
"Unknown constant expr!") ? void (0) : __assert_fail ("CE->getNumOperands() == 2 && \"Unknown constant expr!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2344, __extension__ __PRETTY_FUNCTION__))
;
2345 Code = bitc::CST_CODE_CE_BINOP;
2346 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2347 Record.push_back(VE.getValueID(C->getOperand(0)));
2348 Record.push_back(VE.getValueID(C->getOperand(1)));
2349 uint64_t Flags = getOptimizationFlags(CE);
2350 if (Flags != 0)
2351 Record.push_back(Flags);
2352 }
2353 break;
2354 case Instruction::GetElementPtr: {
2355 Code = bitc::CST_CODE_CE_GEP;
2356 const auto *GO = cast<GEPOperator>(C);
2357 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
2358 if (Optional<unsigned> Idx = GO->getInRangeIndex()) {
2359 Code = bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX;
2360 Record.push_back((*Idx << 1) | GO->isInBounds());
2361 } else if (GO->isInBounds())
2362 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
2363 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2364 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2365 Record.push_back(VE.getValueID(C->getOperand(i)));
2366 }
2367 break;
2368 }
2369 case Instruction::Select:
2370 Code = bitc::CST_CODE_CE_SELECT;
2371 Record.push_back(VE.getValueID(C->getOperand(0)));
2372 Record.push_back(VE.getValueID(C->getOperand(1)));
2373 Record.push_back(VE.getValueID(C->getOperand(2)));
2374 break;
2375 case Instruction::ExtractElement:
2376 Code = bitc::CST_CODE_CE_EXTRACTELT;
2377 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2378 Record.push_back(VE.getValueID(C->getOperand(0)));
2379 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
2380 Record.push_back(VE.getValueID(C->getOperand(1)));
2381 break;
2382 case Instruction::InsertElement:
2383 Code = bitc::CST_CODE_CE_INSERTELT;
2384 Record.push_back(VE.getValueID(C->getOperand(0)));
2385 Record.push_back(VE.getValueID(C->getOperand(1)));
2386 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
2387 Record.push_back(VE.getValueID(C->getOperand(2)));
2388 break;
2389 case Instruction::ShuffleVector:
2390 // If the return type and argument types are the same, this is a
2391 // standard shufflevector instruction. If the types are different,
2392 // then the shuffle is widening or truncating the input vectors, and
2393 // the argument type must also be encoded.
2394 if (C->getType() == C->getOperand(0)->getType()) {
2395 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2396 } else {
2397 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2398 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2399 }
2400 Record.push_back(VE.getValueID(C->getOperand(0)));
2401 Record.push_back(VE.getValueID(C->getOperand(1)));
2402 Record.push_back(VE.getValueID(C->getOperand(2)));
2403 break;
2404 case Instruction::ICmp:
2405 case Instruction::FCmp:
2406 Code = bitc::CST_CODE_CE_CMP;
2407 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2408 Record.push_back(VE.getValueID(C->getOperand(0)));
2409 Record.push_back(VE.getValueID(C->getOperand(1)));
2410 Record.push_back(CE->getPredicate());
2411 break;
2412 }
2413 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2414 Code = bitc::CST_CODE_BLOCKADDRESS;
2415 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2416 Record.push_back(VE.getValueID(BA->getFunction()));
2417 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2418 } else {
2419#ifndef NDEBUG
2420 C->dump();
2421#endif
2422 llvm_unreachable("Unknown constant!")::llvm::llvm_unreachable_internal("Unknown constant!", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2422)
;
2423 }
2424 Stream.EmitRecord(Code, Record, AbbrevToUse);
2425 Record.clear();
2426 }
2427
2428 Stream.ExitBlock();
2429}
2430
2431void ModuleBitcodeWriter::writeModuleConstants() {
2432 const ValueEnumerator::ValueList &Vals = VE.getValues();
2433
2434 // Find the first constant to emit, which is the first non-globalvalue value.
2435 // We know globalvalues have been emitted by WriteModuleInfo.
2436 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2437 if (!isa<GlobalValue>(Vals[i].first)) {
2438 writeConstants(i, Vals.size(), true);
2439 return;
2440 }
2441 }
2442}
2443
2444/// pushValueAndType - The file has to encode both the value and type id for
2445/// many values, because we need to know what type to create for forward
2446/// references. However, most operands are not forward references, so this type
2447/// field is not needed.
2448///
2449/// This function adds V's value ID to Vals. If the value ID is higher than the
2450/// instruction ID, then it is a forward reference, and it also includes the
2451/// type ID. The value ID that is written is encoded relative to the InstID.
2452bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2453 SmallVectorImpl<unsigned> &Vals) {
2454 unsigned ValID = VE.getValueID(V);
2455 // Make encoding relative to the InstID.
2456 Vals.push_back(InstID - ValID);
2457 if (ValID >= InstID) {
2458 Vals.push_back(VE.getTypeID(V->getType()));
2459 return true;
2460 }
2461 return false;
2462}
2463
2464void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2465 unsigned InstID) {
2466 SmallVector<unsigned, 64> Record;
2467 LLVMContext &C = CS.getInstruction()->getContext();
2468
2469 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
2470 const auto &Bundle = CS.getOperandBundleAt(i);
2471 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
2472
2473 for (auto &Input : Bundle.Inputs)
2474 pushValueAndType(Input, InstID, Record);
2475
2476 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
2477 Record.clear();
2478 }
2479}
2480
2481/// pushValue - Like pushValueAndType, but where the type of the value is
2482/// omitted (perhaps it was already encoded in an earlier operand).
2483void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2484 SmallVectorImpl<unsigned> &Vals) {
2485 unsigned ValID = VE.getValueID(V);
2486 Vals.push_back(InstID - ValID);
2487}
2488
2489void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2490 SmallVectorImpl<uint64_t> &Vals) {
2491 unsigned ValID = VE.getValueID(V);
2492 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2493 emitSignedInt64(Vals, diff);
2494}
2495
2496/// WriteInstruction - Emit an instruction to the specified stream.
2497void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2498 unsigned InstID,
2499 SmallVectorImpl<unsigned> &Vals) {
2500 unsigned Code = 0;
2501 unsigned AbbrevToUse = 0;
2502 VE.setInstructionID(&I);
2503 switch (I.getOpcode()) {
2504 default:
2505 if (Instruction::isCast(I.getOpcode())) {
2506 Code = bitc::FUNC_CODE_INST_CAST;
2507 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2508 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
2509 Vals.push_back(VE.getTypeID(I.getType()));
2510 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2511 } else {
2512 assert(isa<BinaryOperator>(I) && "Unknown instruction!")(static_cast <bool> (isa<BinaryOperator>(I) &&
"Unknown instruction!") ? void (0) : __assert_fail ("isa<BinaryOperator>(I) && \"Unknown instruction!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2512, __extension__ __PRETTY_FUNCTION__))
;
2513 Code = bitc::FUNC_CODE_INST_BINOP;
2514 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2515 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
2516 pushValue(I.getOperand(1), InstID, Vals);
2517 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2518 uint64_t Flags = getOptimizationFlags(&I);
2519 if (Flags != 0) {
2520 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2521 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2522 Vals.push_back(Flags);
2523 }
2524 }
2525 break;
2526
2527 case Instruction::GetElementPtr: {
2528 Code = bitc::FUNC_CODE_INST_GEP;
2529 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2530 auto &GEPInst = cast<GetElementPtrInst>(I);
2531 Vals.push_back(GEPInst.isInBounds());
2532 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
2533 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
2534 pushValueAndType(I.getOperand(i), InstID, Vals);
2535 break;
2536 }
2537 case Instruction::ExtractValue: {
2538 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
2539 pushValueAndType(I.getOperand(0), InstID, Vals);
2540 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
2541 Vals.append(EVI->idx_begin(), EVI->idx_end());
2542 break;
2543 }
2544 case Instruction::InsertValue: {
2545 Code = bitc::FUNC_CODE_INST_INSERTVAL;
2546 pushValueAndType(I.getOperand(0), InstID, Vals);
2547 pushValueAndType(I.getOperand(1), InstID, Vals);
2548 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
2549 Vals.append(IVI->idx_begin(), IVI->idx_end());
2550 break;
2551 }
2552 case Instruction::Select:
2553 Code = bitc::FUNC_CODE_INST_VSELECT;
2554 pushValueAndType(I.getOperand(1), InstID, Vals);
2555 pushValue(I.getOperand(2), InstID, Vals);
2556 pushValueAndType(I.getOperand(0), InstID, Vals);
2557 break;
2558 case Instruction::ExtractElement:
2559 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
2560 pushValueAndType(I.getOperand(0), InstID, Vals);
2561 pushValueAndType(I.getOperand(1), InstID, Vals);
2562 break;
2563 case Instruction::InsertElement:
2564 Code = bitc::FUNC_CODE_INST_INSERTELT;
2565 pushValueAndType(I.getOperand(0), InstID, Vals);
2566 pushValue(I.getOperand(1), InstID, Vals);
2567 pushValueAndType(I.getOperand(2), InstID, Vals);
2568 break;
2569 case Instruction::ShuffleVector:
2570 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
2571 pushValueAndType(I.getOperand(0), InstID, Vals);
2572 pushValue(I.getOperand(1), InstID, Vals);
2573 pushValue(I.getOperand(2), InstID, Vals);
2574 break;
2575 case Instruction::ICmp:
2576 case Instruction::FCmp: {
2577 // compare returning Int1Ty or vector of Int1Ty
2578 Code = bitc::FUNC_CODE_INST_CMP2;
2579 pushValueAndType(I.getOperand(0), InstID, Vals);
2580 pushValue(I.getOperand(1), InstID, Vals);
2581 Vals.push_back(cast<CmpInst>(I).getPredicate());
2582 uint64_t Flags = getOptimizationFlags(&I);
2583 if (Flags != 0)
2584 Vals.push_back(Flags);
2585 break;
2586 }
2587
2588 case Instruction::Ret:
2589 {
2590 Code = bitc::FUNC_CODE_INST_RET;
2591 unsigned NumOperands = I.getNumOperands();
2592 if (NumOperands == 0)
2593 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2594 else if (NumOperands == 1) {
2595 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2596 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2597 } else {
2598 for (unsigned i = 0, e = NumOperands; i != e; ++i)
2599 pushValueAndType(I.getOperand(i), InstID, Vals);
2600 }
2601 }
2602 break;
2603 case Instruction::Br:
2604 {
2605 Code = bitc::FUNC_CODE_INST_BR;
2606 const BranchInst &II = cast<BranchInst>(I);
2607 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2608 if (II.isConditional()) {
2609 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
2610 pushValue(II.getCondition(), InstID, Vals);
2611 }
2612 }
2613 break;
2614 case Instruction::Switch:
2615 {
2616 Code = bitc::FUNC_CODE_INST_SWITCH;
2617 const SwitchInst &SI = cast<SwitchInst>(I);
2618 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
2619 pushValue(SI.getCondition(), InstID, Vals);
2620 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
2621 for (auto Case : SI.cases()) {
2622 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2623 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
2624 }
2625 }
2626 break;
2627 case Instruction::IndirectBr:
2628 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
2629 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
2630 // Encode the address operand as relative, but not the basic blocks.
2631 pushValue(I.getOperand(0), InstID, Vals);
2632 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
2633 Vals.push_back(VE.getValueID(I.getOperand(i)));
2634 break;
2635
2636 case Instruction::Invoke: {
2637 const InvokeInst *II = cast<InvokeInst>(&I);
2638 const Value *Callee = II->getCalledValue();
2639 FunctionType *FTy = II->getFunctionType();
2640
2641 if (II->hasOperandBundles())
2642 writeOperandBundles(II, InstID);
2643
2644 Code = bitc::FUNC_CODE_INST_INVOKE;
2645
2646 Vals.push_back(VE.getAttributeListID(II->getAttributes()));
2647 Vals.push_back(II->getCallingConv() | 1 << 13);
2648 Vals.push_back(VE.getValueID(II->getNormalDest()));
2649 Vals.push_back(VE.getValueID(II->getUnwindDest()));
2650 Vals.push_back(VE.getTypeID(FTy));
2651 pushValueAndType(Callee, InstID, Vals);
2652
2653 // Emit value #'s for the fixed parameters.
2654 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2655 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
2656
2657 // Emit type/value pairs for varargs params.
2658 if (FTy->isVarArg()) {
2659 for (unsigned i = FTy->getNumParams(), e = II->getNumArgOperands();
2660 i != e; ++i)
2661 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
2662 }
2663 break;
2664 }
2665 case Instruction::Resume:
2666 Code = bitc::FUNC_CODE_INST_RESUME;
2667 pushValueAndType(I.getOperand(0), InstID, Vals);
2668 break;
2669 case Instruction::CleanupRet: {
2670 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2671 const auto &CRI = cast<CleanupReturnInst>(I);
2672 pushValue(CRI.getCleanupPad(), InstID, Vals);
2673 if (CRI.hasUnwindDest())
2674 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2675 break;
2676 }
2677 case Instruction::CatchRet: {
2678 Code = bitc::FUNC_CODE_INST_CATCHRET;
2679 const auto &CRI = cast<CatchReturnInst>(I);
2680 pushValue(CRI.getCatchPad(), InstID, Vals);
2681 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2682 break;
2683 }
2684 case Instruction::CleanupPad:
2685 case Instruction::CatchPad: {
2686 const auto &FuncletPad = cast<FuncletPadInst>(I);
2687 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2688 : bitc::FUNC_CODE_INST_CLEANUPPAD;
2689 pushValue(FuncletPad.getParentPad(), InstID, Vals);
2690
2691 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
2692 Vals.push_back(NumArgOperands);
2693 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
2694 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
2695 break;
2696 }
2697 case Instruction::CatchSwitch: {
2698 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2699 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2700
2701 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
2702
2703 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2704 Vals.push_back(NumHandlers);
2705 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2706 Vals.push_back(VE.getValueID(CatchPadBB));
2707
2708 if (CatchSwitch.hasUnwindDest())
2709 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
2710 break;
2711 }
2712 case Instruction::Unreachable:
2713 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
2714 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
2715 break;
2716
2717 case Instruction::PHI: {
2718 const PHINode &PN = cast<PHINode>(I);
2719 Code = bitc::FUNC_CODE_INST_PHI;
2720 // With the newer instruction encoding, forward references could give
2721 // negative valued IDs. This is most common for PHIs, so we use
2722 // signed VBRs.
2723 SmallVector<uint64_t, 128> Vals64;
2724 Vals64.push_back(VE.getTypeID(PN.getType()));
2725 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2726 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
2727 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2728 }
2729 // Emit a Vals64 vector and exit.
2730 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
2731 Vals64.clear();
2732 return;
2733 }
2734
2735 case Instruction::LandingPad: {
2736 const LandingPadInst &LP = cast<LandingPadInst>(I);
2737 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2738 Vals.push_back(VE.getTypeID(LP.getType()));
2739 Vals.push_back(LP.isCleanup());
2740 Vals.push_back(LP.getNumClauses());
2741 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2742 if (LP.isCatch(I))
2743 Vals.push_back(LandingPadInst::Catch);
2744 else
2745 Vals.push_back(LandingPadInst::Filter);
2746 pushValueAndType(LP.getClause(I), InstID, Vals);
2747 }
2748 break;
2749 }
2750
2751 case Instruction::Alloca: {
2752 Code = bitc::FUNC_CODE_INST_ALLOCA;
2753 const AllocaInst &AI = cast<AllocaInst>(I);
2754 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
2755 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
2756 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
2757 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2758 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&(static_cast <bool> (Log2_32(Value::MaximumAlignment) +
1 < 1 << 5 && "not enough bits for maximum alignment"
) ? void (0) : __assert_fail ("Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 && \"not enough bits for maximum alignment\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2759, __extension__ __PRETTY_FUNCTION__))
2759 "not enough bits for maximum alignment")(static_cast <bool> (Log2_32(Value::MaximumAlignment) +
1 < 1 << 5 && "not enough bits for maximum alignment"
) ? void (0) : __assert_fail ("Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 && \"not enough bits for maximum alignment\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2759, __extension__ __PRETTY_FUNCTION__))
;
2760 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64")(static_cast <bool> (AlignRecord < 1 << 5 &&
"alignment greater than 1 << 64") ? void (0) : __assert_fail
("AlignRecord < 1 << 5 && \"alignment greater than 1 << 64\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2760, __extension__ __PRETTY_FUNCTION__))
;
2761 AlignRecord |= AI.isUsedWithInAlloca() << 5;
2762 AlignRecord |= 1 << 6;
2763 AlignRecord |= AI.isSwiftError() << 7;
2764 Vals.push_back(AlignRecord);
2765 break;
2766 }
2767
2768 case Instruction::Load:
2769 if (cast<LoadInst>(I).isAtomic()) {
2770 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
2771 pushValueAndType(I.getOperand(0), InstID, Vals);
2772 } else {
2773 Code = bitc::FUNC_CODE_INST_LOAD;
2774 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
2775 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2776 }
2777 Vals.push_back(VE.getTypeID(I.getType()));
2778 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2779 Vals.push_back(cast<LoadInst>(I).isVolatile());
2780 if (cast<LoadInst>(I).isAtomic()) {
2781 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2782 Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
2783 }
2784 break;
2785 case Instruction::Store:
2786 if (cast<StoreInst>(I).isAtomic())
2787 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2788 else
2789 Code = bitc::FUNC_CODE_INST_STORE;
2790 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2791 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
2792 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2793 Vals.push_back(cast<StoreInst>(I).isVolatile());
2794 if (cast<StoreInst>(I).isAtomic()) {
2795 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2796 Vals.push_back(
2797 getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
2798 }
2799 break;
2800 case Instruction::AtomicCmpXchg:
2801 Code = bitc::FUNC_CODE_INST_CMPXCHG;
2802 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2803 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2804 pushValue(I.getOperand(2), InstID, Vals); // newval.
2805 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
2806 Vals.push_back(
2807 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2808 Vals.push_back(
2809 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
2810 Vals.push_back(
2811 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
2812 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
2813 break;
2814 case Instruction::AtomicRMW:
2815 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
2816 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2817 pushValue(I.getOperand(1), InstID, Vals); // val.
2818 Vals.push_back(
2819 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
2820 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
2821 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2822 Vals.push_back(
2823 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
2824 break;
2825 case Instruction::Fence:
2826 Code = bitc::FUNC_CODE_INST_FENCE;
2827 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2828 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
2829 break;
2830 case Instruction::Call: {
2831 const CallInst &CI = cast<CallInst>(I);
2832 FunctionType *FTy = CI.getFunctionType();
2833
2834 if (CI.hasOperandBundles())
2835 writeOperandBundles(&CI, InstID);
2836
2837 Code = bitc::FUNC_CODE_INST_CALL;
2838
2839 Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
2840
2841 unsigned Flags = getOptimizationFlags(&I);
2842 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2843 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2844 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2845 1 << bitc::CALL_EXPLICIT_TYPE |
2846 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2847 unsigned(Flags != 0) << bitc::CALL_FMF);
2848 if (Flags != 0)
2849 Vals.push_back(Flags);
2850
2851 Vals.push_back(VE.getTypeID(FTy));
2852 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
2853
2854 // Emit value #'s for the fixed parameters.
2855 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2856 // Check for labels (can happen with asm labels).
2857 if (FTy->getParamType(i)->isLabelTy())
2858 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2859 else
2860 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
2861 }
2862
2863 // Emit type/value pairs for varargs params.
2864 if (FTy->isVarArg()) {
2865 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
2866 i != e; ++i)
2867 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
2868 }
2869 break;
2870 }
2871 case Instruction::VAArg:
2872 Code = bitc::FUNC_CODE_INST_VAARG;
2873 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
2874 pushValue(I.getOperand(0), InstID, Vals); // valist.
2875 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2876 break;
2877 }
2878
2879 Stream.EmitRecord(Code, Vals, AbbrevToUse);
2880 Vals.clear();
2881}
2882
2883/// Write a GlobalValue VST to the module. The purpose of this data structure is
2884/// to allow clients to efficiently find the function body.
2885void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
2886 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
2887 // Get the offset of the VST we are writing, and backpatch it into
2888 // the VST forward declaration record.
2889 uint64_t VSTOffset = Stream.GetCurrentBitNo();
2890 // The BitcodeStartBit was the stream offset of the identification block.
2891 VSTOffset -= bitcodeStartBit();
2892 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned")(static_cast <bool> ((VSTOffset & 31) == 0 &&
"VST block not 32-bit aligned") ? void (0) : __assert_fail (
"(VSTOffset & 31) == 0 && \"VST block not 32-bit aligned\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2892, __extension__ __PRETTY_FUNCTION__))
;
2893 // Note that we add 1 here because the offset is relative to one word
2894 // before the start of the identification block, which was historically
2895 // always the start of the regular bitcode header.
2896 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
2897
2898 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2899
2900 auto Abbv = std::make_shared<BitCodeAbbrev>();
2901 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2902 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2903 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2904 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2905
2906 for (const Function &F : M) {
2907 uint64_t Record[2];
2908
2909 if (F.isDeclaration())
10
Assuming the condition is false
11
Taking false branch
2910 continue;
2911
2912 Record[0] = VE.getValueID(&F);
2913
2914 // Save the word offset of the function (from the start of the
2915 // actual bitcode written to the stream).
2916 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
2917 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned")(static_cast <bool> ((BitcodeIndex & 31) == 0 &&
"function block not 32-bit aligned") ? void (0) : __assert_fail
("(BitcodeIndex & 31) == 0 && \"function block not 32-bit aligned\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2917, __extension__ __PRETTY_FUNCTION__))
;
2918 // Note that we add 1 here because the offset is relative to one word
2919 // before the start of the identification block, which was historically
2920 // always the start of the regular bitcode header.
2921 Record[1] = BitcodeIndex / 32 + 1;
2922
2923 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
12
Calling 'BitstreamWriter::EmitRecord'
2924 }
2925
2926 Stream.ExitBlock();
2927}
2928
2929/// Emit names for arguments, instructions and basic blocks in a function.
2930void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
2931 const ValueSymbolTable &VST) {
2932 if (VST.empty())
2933 return;
2934
2935 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2936
2937 // FIXME: Set up the abbrev, we know how many values there are!
2938 // FIXME: We know if the type names can use 7-bit ascii.
2939 SmallVector<uint64_t, 64> NameVals;
2940
2941 for (const ValueName &Name : VST) {
2942 // Figure out the encoding to use for the name.
2943 StringEncoding Bits = getStringEncoding(Name.getKey());
2944
2945 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
2946 NameVals.push_back(VE.getValueID(Name.getValue()));
2947
2948 // VST_CODE_ENTRY: [valueid, namechar x N]
2949 // VST_CODE_BBENTRY: [bbid, namechar x N]
2950 unsigned Code;
2951 if (isa<BasicBlock>(Name.getValue())) {
2952 Code = bitc::VST_CODE_BBENTRY;
2953 if (Bits == SE_Char6)
2954 AbbrevToUse = VST_BBENTRY_6_ABBREV;
2955 } else {
2956 Code = bitc::VST_CODE_ENTRY;
2957 if (Bits == SE_Char6)
2958 AbbrevToUse = VST_ENTRY_6_ABBREV;
2959 else if (Bits == SE_Fixed7)
2960 AbbrevToUse = VST_ENTRY_7_ABBREV;
2961 }
2962
2963 for (const auto P : Name.getKey())
2964 NameVals.push_back((unsigned char)P);
2965
2966 // Emit the finished record.
2967 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
2968 NameVals.clear();
2969 }
2970
2971 Stream.ExitBlock();
2972}
2973
2974void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
2975 assert(Order.Shuffle.size() >= 2 && "Shuffle too small")(static_cast <bool> (Order.Shuffle.size() >= 2 &&
"Shuffle too small") ? void (0) : __assert_fail ("Order.Shuffle.size() >= 2 && \"Shuffle too small\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2975, __extension__ __PRETTY_FUNCTION__))
;
2976 unsigned Code;
2977 if (isa<BasicBlock>(Order.V))
2978 Code = bitc::USELIST_CODE_BB;
2979 else
2980 Code = bitc::USELIST_CODE_DEFAULT;
2981
2982 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
2983 Record.push_back(VE.getValueID(Order.V));
2984 Stream.EmitRecord(Code, Record);
2985}
2986
2987void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
2988 assert(VE.shouldPreserveUseListOrder() &&(static_cast <bool> (VE.shouldPreserveUseListOrder() &&
"Expected to be preserving use-list order") ? void (0) : __assert_fail
("VE.shouldPreserveUseListOrder() && \"Expected to be preserving use-list order\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2989, __extension__ __PRETTY_FUNCTION__))
2989 "Expected to be preserving use-list order")(static_cast <bool> (VE.shouldPreserveUseListOrder() &&
"Expected to be preserving use-list order") ? void (0) : __assert_fail
("VE.shouldPreserveUseListOrder() && \"Expected to be preserving use-list order\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 2989, __extension__ __PRETTY_FUNCTION__))
;
2990
2991 auto hasMore = [&]() {
2992 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2993 };
2994 if (!hasMore())
2995 // Nothing to do.
2996 return;
2997
2998 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
2999 while (hasMore()) {
3000 writeUseList(std::move(VE.UseListOrders.back()));
3001 VE.UseListOrders.pop_back();
3002 }
3003 Stream.ExitBlock();
3004}
3005
3006/// Emit a function body to the module stream.
3007void ModuleBitcodeWriter::writeFunction(
3008 const Function &F,
3009 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3010 // Save the bitcode index of the start of this function block for recording
3011 // in the VST.
3012 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
3013
3014 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
3015 VE.incorporateFunction(F);
3016
3017 SmallVector<unsigned, 64> Vals;
3018
3019 // Emit the number of basic blocks, so the reader can create them ahead of
3020 // time.
3021 Vals.push_back(VE.getBasicBlocks().size());
3022 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
3023 Vals.clear();
3024
3025 // If there are function-local constants, emit them now.
3026 unsigned CstStart, CstEnd;
3027 VE.getFunctionConstantRange(CstStart, CstEnd);
3028 writeConstants(CstStart, CstEnd, false);
3029
3030 // If there is function-local metadata, emit it now.
3031 writeFunctionMetadata(F);
3032
3033 // Keep a running idea of what the instruction ID is.
3034 unsigned InstID = CstEnd;
3035
3036 bool NeedsMetadataAttachment = F.hasMetadata();
3037
3038 DILocation *LastDL = nullptr;
3039 // Finally, emit all the instructions, in order.
3040 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
3041 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
3042 I != E; ++I) {
3043 writeInstruction(*I, InstID, Vals);
3044
3045 if (!I->getType()->isVoidTy())
3046 ++InstID;
3047
3048 // If the instruction has metadata, write a metadata attachment later.
3049 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
3050
3051 // If the instruction has a debug location, emit it.
3052 DILocation *DL = I->getDebugLoc();
3053 if (!DL)
3054 continue;
3055
3056 if (DL == LastDL) {
3057 // Just repeat the same debug loc as last time.
3058 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
3059 continue;
3060 }
3061
3062 Vals.push_back(DL->getLine());
3063 Vals.push_back(DL->getColumn());
3064 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
3065 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
3066 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
3067 Vals.clear();
3068
3069 LastDL = DL;
3070 }
3071
3072 // Emit names for all the instructions etc.
3073 if (auto *Symtab = F.getValueSymbolTable())
3074 writeFunctionLevelValueSymbolTable(*Symtab);
3075
3076 if (NeedsMetadataAttachment)
3077 writeFunctionMetadataAttachment(F);
3078 if (VE.shouldPreserveUseListOrder())
3079 writeUseListBlock(&F);
3080 VE.purgeFunction();
3081 Stream.ExitBlock();
3082}
3083
3084// Emit blockinfo, which defines the standard abbreviations etc.
3085void ModuleBitcodeWriter::writeBlockInfo() {
3086 // We only want to emit block info records for blocks that have multiple
3087 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
3088 // Other blocks can define their abbrevs inline.
3089 Stream.EnterBlockInfoBlock();
3090
3091 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
3092 auto Abbv = std::make_shared<BitCodeAbbrev>();
3093 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
3094 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3095 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3096 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
3097 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
3098 VST_ENTRY_8_ABBREV)
3099 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3099)
;
3100 }
3101
3102 { // 7-bit fixed width VST_CODE_ENTRY strings.
3103 auto Abbv = std::make_shared<BitCodeAbbrev>();
3104 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
3105 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3106 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3107 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
3108 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
3109 VST_ENTRY_7_ABBREV)
3110 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3110)
;
3111 }
3112 { // 6-bit char6 VST_CODE_ENTRY strings.
3113 auto Abbv = std::make_shared<BitCodeAbbrev>();
3114 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
3115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3116 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3117 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
3118 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
3119 VST_ENTRY_6_ABBREV)
3120 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3120)
;
3121 }
3122 { // 6-bit char6 VST_CODE_BBENTRY strings.
3123 auto Abbv = std::make_shared<BitCodeAbbrev>();
3124 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
3125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3126 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3127 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
3128 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
3129 VST_BBENTRY_6_ABBREV)
3130 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3130)
;
3131 }
3132
3133 { // SETTYPE abbrev for CONSTANTS_BLOCK.
3134 auto Abbv = std::make_shared<BitCodeAbbrev>();
3135 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
3136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
3137 VE.computeBitsRequiredForTypeIndicies()));
3138 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3139 CONSTANTS_SETTYPE_ABBREV)
3140 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3140)
;
3141 }
3142
3143 { // INTEGER abbrev for CONSTANTS_BLOCK.
3144 auto Abbv = std::make_shared<BitCodeAbbrev>();
3145 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
3146 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3147 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3148 CONSTANTS_INTEGER_ABBREV)
3149 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3149)
;
3150 }
3151
3152 { // CE_CAST abbrev for CONSTANTS_BLOCK.
3153 auto Abbv = std::make_shared<BitCodeAbbrev>();
3154 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
3155 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
3156 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
3157 VE.computeBitsRequiredForTypeIndicies()));
3158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3159
3160 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3161 CONSTANTS_CE_CAST_Abbrev)
3162 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3162)
;
3163 }
3164 { // NULL abbrev for CONSTANTS_BLOCK.
3165 auto Abbv = std::make_shared<BitCodeAbbrev>();
3166 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
3167 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3168 CONSTANTS_NULL_Abbrev)
3169 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3169)
;
3170 }
3171
3172 // FIXME: This should only use space for first class types!
3173
3174 { // INST_LOAD abbrev for FUNCTION_BLOCK.
3175 auto Abbv = std::make_shared<BitCodeAbbrev>();
3176 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
3177 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
3178 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3179 VE.computeBitsRequiredForTypeIndicies()));
3180 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3181 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
3182 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3183 FUNCTION_INST_LOAD_ABBREV)
3184 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3184)
;
3185 }
3186 { // INST_BINOP abbrev for FUNCTION_BLOCK.
3187 auto Abbv = std::make_shared<BitCodeAbbrev>();
3188 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3189 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3190 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3191 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3192 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3193 FUNCTION_INST_BINOP_ABBREV)
3194 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3194)
;
3195 }
3196 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3197 auto Abbv = std::make_shared<BitCodeAbbrev>();
3198 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3199 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3200 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3201 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3202 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3203 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3204 FUNCTION_INST_BINOP_FLAGS_ABBREV)
3205 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3205)
;
3206 }
3207 { // INST_CAST abbrev for FUNCTION_BLOCK.
3208 auto Abbv = std::make_shared<BitCodeAbbrev>();
3209 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
3210 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3211 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3212 VE.computeBitsRequiredForTypeIndicies()));
3213 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3214 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3215 FUNCTION_INST_CAST_ABBREV)
3216 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3216)
;
3217 }
3218
3219 { // INST_RET abbrev for FUNCTION_BLOCK.
3220 auto Abbv = std::make_shared<BitCodeAbbrev>();
3221 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
3222 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3223 FUNCTION_INST_RET_VOID_ABBREV)
3224 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3224)
;
3225 }
3226 { // INST_RET abbrev for FUNCTION_BLOCK.
3227 auto Abbv = std::make_shared<BitCodeAbbrev>();
3228 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
3229 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
3230 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3231 FUNCTION_INST_RET_VAL_ABBREV)
3232 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3232)
;
3233 }
3234 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3235 auto Abbv = std::make_shared<BitCodeAbbrev>();
3236 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
3237 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3238 FUNCTION_INST_UNREACHABLE_ABBREV)
3239 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3239)
;
3240 }
3241 {
3242 auto Abbv = std::make_shared<BitCodeAbbrev>();
3243 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
3244 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
3245 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3246 Log2_32_Ceil(VE.getTypes().size() + 1)));
3247 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3248 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
3249 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3250 FUNCTION_INST_GEP_ABBREV)
3251 llvm_unreachable("Unexpected abbrev ordering!")::llvm::llvm_unreachable_internal("Unexpected abbrev ordering!"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3251)
;
3252 }
3253
3254 Stream.ExitBlock();
3255}
3256
3257/// Write the module path strings, currently only used when generating
3258/// a combined index file.
3259void IndexBitcodeWriter::writeModStrings() {
3260 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
3261
3262 // TODO: See which abbrev sizes we actually need to emit
3263
3264 // 8-bit fixed-width MST_ENTRY strings.
3265 auto Abbv = std::make_shared<BitCodeAbbrev>();
3266 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3267 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3268 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3269 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
3270 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
3271
3272 // 7-bit fixed width MST_ENTRY strings.
3273 Abbv = std::make_shared<BitCodeAbbrev>();
3274 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3275 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3276 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3277 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
3278 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
3279
3280 // 6-bit char6 MST_ENTRY strings.
3281 Abbv = std::make_shared<BitCodeAbbrev>();
3282 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3283 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3284 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3285 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
3286 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
3287
3288 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3289 Abbv = std::make_shared<BitCodeAbbrev>();
3290 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
3291 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3292 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3293 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3294 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3295 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3296 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
3297
3298 SmallVector<unsigned, 64> Vals;
3299 forEachModule(
3300 [&](const StringMapEntry<std::pair<uint64_t, ModuleHash>> &MPSE) {
3301 StringRef Key = MPSE.getKey();
3302 const auto &Value = MPSE.getValue();
3303 StringEncoding Bits = getStringEncoding(Key);
3304 unsigned AbbrevToUse = Abbrev8Bit;
3305 if (Bits == SE_Char6)
3306 AbbrevToUse = Abbrev6Bit;
3307 else if (Bits == SE_Fixed7)
3308 AbbrevToUse = Abbrev7Bit;
3309
3310 Vals.push_back(Value.first);
3311 Vals.append(Key.begin(), Key.end());
3312
3313 // Emit the finished record.
3314 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
3315
3316 // Emit an optional hash for the module now
3317 const auto &Hash = Value.second;
3318 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
3319 Vals.assign(Hash.begin(), Hash.end());
3320 // Emit the hash record.
3321 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
3322 }
3323
3324 Vals.clear();
3325 });
3326 Stream.ExitBlock();
3327}
3328
3329/// Write the function type metadata related records that need to appear before
3330/// a function summary entry (whether per-module or combined).
3331static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
3332 FunctionSummary *FS) {
3333 if (!FS->type_tests().empty())
3334 Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
3335
3336 SmallVector<uint64_t, 64> Record;
3337
3338 auto WriteVFuncIdVec = [&](uint64_t Ty,
3339 ArrayRef<FunctionSummary::VFuncId> VFs) {
3340 if (VFs.empty())
3341 return;
3342 Record.clear();
3343 for (auto &VF : VFs) {
3344 Record.push_back(VF.GUID);
3345 Record.push_back(VF.Offset);
3346 }
3347 Stream.EmitRecord(Ty, Record);
3348 };
3349
3350 WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
3351 FS->type_test_assume_vcalls());
3352 WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
3353 FS->type_checked_load_vcalls());
3354
3355 auto WriteConstVCallVec = [&](uint64_t Ty,
3356 ArrayRef<FunctionSummary::ConstVCall> VCs) {
3357 for (auto &VC : VCs) {
3358 Record.clear();
3359 Record.push_back(VC.VFunc.GUID);
3360 Record.push_back(VC.VFunc.Offset);
3361 Record.insert(Record.end(), VC.Args.begin(), VC.Args.end());
3362 Stream.EmitRecord(Ty, Record);
3363 }
3364 };
3365
3366 WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
3367 FS->type_test_assume_const_vcalls());
3368 WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
3369 FS->type_checked_load_const_vcalls());
3370}
3371
3372static void writeWholeProgramDevirtResolutionByArg(
3373 SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
3374 const WholeProgramDevirtResolution::ByArg &ByArg) {
3375 NameVals.push_back(args.size());
3376 NameVals.insert(NameVals.end(), args.begin(), args.end());
3377
3378 NameVals.push_back(ByArg.TheKind);
3379 NameVals.push_back(ByArg.Info);
3380 NameVals.push_back(ByArg.Byte);
3381 NameVals.push_back(ByArg.Bit);
3382}
3383
3384static void writeWholeProgramDevirtResolution(
3385 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
3386 uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
3387 NameVals.push_back(Id);
3388
3389 NameVals.push_back(Wpd.TheKind);
3390 NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
3391 NameVals.push_back(Wpd.SingleImplName.size());
3392
3393 NameVals.push_back(Wpd.ResByArg.size());
3394 for (auto &A : Wpd.ResByArg)
3395 writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
3396}
3397
3398static void writeTypeIdSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
3399 StringTableBuilder &StrtabBuilder,
3400 const std::string &Id,
3401 const TypeIdSummary &Summary) {
3402 NameVals.push_back(StrtabBuilder.add(Id));
3403 NameVals.push_back(Id.size());
3404
3405 NameVals.push_back(Summary.TTRes.TheKind);
3406 NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
3407 NameVals.push_back(Summary.TTRes.AlignLog2);
3408 NameVals.push_back(Summary.TTRes.SizeM1);
3409 NameVals.push_back(Summary.TTRes.BitMask);
3410 NameVals.push_back(Summary.TTRes.InlineBits);
3411
3412 for (auto &W : Summary.WPDRes)
3413 writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
3414 W.second);
3415}
3416
3417// Helper to emit a single function summary record.
3418void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
3419 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
3420 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3421 const Function &F) {
3422 NameVals.push_back(ValueID);
3423
3424 FunctionSummary *FS = cast<FunctionSummary>(Summary);
3425 writeFunctionTypeMetadataRecords(Stream, FS);
3426
3427 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
3428 NameVals.push_back(FS->instCount());
3429 NameVals.push_back(getEncodedFFlags(FS->fflags()));
3430 NameVals.push_back(FS->refs().size());
3431
3432 for (auto &RI : FS->refs())
3433 NameVals.push_back(VE.getValueID(RI.getValue()));
3434
3435 bool HasProfileData = F.hasProfileData();
3436 for (auto &ECI : FS->calls()) {
3437 NameVals.push_back(getValueId(ECI.first));
3438 if (HasProfileData)
3439 NameVals.push_back(static_cast<uint8_t>(ECI.second.Hotness));
3440 else if (WriteRelBFToSummary)
3441 NameVals.push_back(ECI.second.RelBlockFreq);
3442 }
3443
3444 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3445 unsigned Code =
3446 (HasProfileData ? bitc::FS_PERMODULE_PROFILE
3447 : (WriteRelBFToSummary ? bitc::FS_PERMODULE_RELBF
3448 : bitc::FS_PERMODULE));
3449
3450 // Emit the finished record.
3451 Stream.EmitRecord(Code, NameVals, FSAbbrev);
3452 NameVals.clear();
3453}
3454
3455// Collect the global value references in the given variable's initializer,
3456// and emit them in a summary record.
3457void ModuleBitcodeWriterBase::writeModuleLevelReferences(
3458 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3459 unsigned FSModRefsAbbrev) {
3460 auto VI = Index->getValueInfo(GlobalValue::getGUID(V.getName()));
3461 if (!VI || VI.getSummaryList().empty()) {
3462 // Only declarations should not have a summary (a declaration might however
3463 // have a summary if the def was in module level asm).
3464 assert(V.isDeclaration())(static_cast <bool> (V.isDeclaration()) ? void (0) : __assert_fail
("V.isDeclaration()", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3464, __extension__ __PRETTY_FUNCTION__))
;
3465 return;
3466 }
3467 auto *Summary = VI.getSummaryList()[0].get();
3468 NameVals.push_back(VE.getValueID(&V));
3469 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
3470 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
3471
3472 unsigned SizeBeforeRefs = NameVals.size();
3473 for (auto &RI : VS->refs())
3474 NameVals.push_back(VE.getValueID(RI.getValue()));
3475 // Sort the refs for determinism output, the vector returned by FS->refs() has
3476 // been initialized from a DenseSet.
3477 std::sort(NameVals.begin() + SizeBeforeRefs, NameVals.end());
3478
3479 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3480 FSModRefsAbbrev);
3481 NameVals.clear();
3482}
3483
3484// Current version for the summary.
3485// This is bumped whenever we introduce changes in the way some record are
3486// interpreted, like flags for instance.
3487static const uint64_t INDEX_VERSION = 4;
3488
3489/// Emit the per-module summary section alongside the rest of
3490/// the module's bitcode.
3491void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
3492 // By default we compile with ThinLTO if the module has a summary, but the
3493 // client can request full LTO with a module flag.
3494 bool IsThinLTO = true;
3495 if (auto *MD =
3496 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
3497 IsThinLTO = MD->getZExtValue();
3498 Stream.EnterSubblock(IsThinLTO ? bitc::GLOBALVAL_SUMMARY_BLOCK_ID
3499 : bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID,
3500 4);
3501
3502 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3503
3504 if (Index->begin() == Index->end()) {
3505 Stream.ExitBlock();
3506 return;
3507 }
3508
3509 for (const auto &GVI : valueIds()) {
3510 Stream.EmitRecord(bitc::FS_VALUE_GUID,
3511 ArrayRef<uint64_t>{GVI.second, GVI.first});
3512 }
3513
3514 // Abbrev for FS_PERMODULE_PROFILE.
3515 auto Abbv = std::make_shared<BitCodeAbbrev>();
3516 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3517 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3518 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3519 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3520 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
3521 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3522 // numrefs x valueid, n x (valueid, hotness)
3523 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3524 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3525 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3526
3527 // Abbrev for FS_PERMODULE or FS_PERMODULE_RELBF.
3528 Abbv = std::make_shared<BitCodeAbbrev>();
3529 if (WriteRelBFToSummary)
3530 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_RELBF));
3531 else
3532 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
3533 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3534 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3535 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3536 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
3537 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3538 // numrefs x valueid, n x (valueid [, rel_block_freq])
3539 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3540 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3541 unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3542
3543 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3544 Abbv = std::make_shared<BitCodeAbbrev>();
3545 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3546 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3547 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3548 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3549 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3550 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3551
3552 // Abbrev for FS_ALIAS.
3553 Abbv = std::make_shared<BitCodeAbbrev>();
3554 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3555 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3556 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3557 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3558 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3559
3560 SmallVector<uint64_t, 64> NameVals;
3561 // Iterate over the list of functions instead of the Index to
3562 // ensure the ordering is stable.
3563 for (const Function &F : M) {
3564 // Summary emission does not support anonymous functions, they have to
3565 // renamed using the anonymous function renaming pass.
3566 if (!F.hasName())
3567 report_fatal_error("Unexpected anonymous function when writing summary");
3568
3569 ValueInfo VI = Index->getValueInfo(GlobalValue::getGUID(F.getName()));
3570 if (!VI || VI.getSummaryList().empty()) {
3571 // Only declarations should not have a summary (a declaration might
3572 // however have a summary if the def was in module level asm).
3573 assert(F.isDeclaration())(static_cast <bool> (F.isDeclaration()) ? void (0) : __assert_fail
("F.isDeclaration()", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3573, __extension__ __PRETTY_FUNCTION__))
;
3574 continue;
3575 }
3576 auto *Summary = VI.getSummaryList()[0].get();
3577 writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
3578 FSCallsAbbrev, FSCallsProfileAbbrev, F);
3579 }
3580
3581 // Capture references from GlobalVariable initializers, which are outside
3582 // of a function scope.
3583 for (const GlobalVariable &G : M.globals())
3584 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
3585
3586 for (const GlobalAlias &A : M.aliases()) {
3587 auto *Aliasee = A.getBaseObject();
3588 if (!Aliasee->hasName())
3589 // Nameless function don't have an entry in the summary, skip it.
3590 continue;
3591 auto AliasId = VE.getValueID(&A);
3592 auto AliaseeId = VE.getValueID(Aliasee);
3593 NameVals.push_back(AliasId);
3594 auto *Summary = Index->getGlobalValueSummary(A);
3595 AliasSummary *AS = cast<AliasSummary>(Summary);
3596 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
3597 NameVals.push_back(AliaseeId);
3598 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
3599 NameVals.clear();
3600 }
3601
3602 Stream.ExitBlock();
3603}
3604
3605/// Emit the combined summary section into the combined index file.
3606void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
3607 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
3608 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3609
3610 // Write the index flags.
3611 uint64_t Flags = 0;
3612 if (Index.withGlobalValueDeadStripping())
3613 Flags |= 0x1;
3614 if (Index.skipModuleByDistributedBackend())
3615 Flags |= 0x2;
3616 Stream.EmitRecord(bitc::FS_FLAGS, ArrayRef<uint64_t>{Flags});
3617
3618 for (const auto &GVI : valueIds()) {
3619 Stream.EmitRecord(bitc::FS_VALUE_GUID,
3620 ArrayRef<uint64_t>{GVI.second, GVI.first});
3621 }
3622
3623 // Abbrev for FS_COMBINED.
3624 auto Abbv = std::make_shared<BitCodeAbbrev>();
3625 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
3626 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3627 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
3628 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3629 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
3631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3632 // numrefs x valueid, n x (valueid)
3633 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3634 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3635 unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3636
3637 // Abbrev for FS_COMBINED_PROFILE.
3638 Abbv = std::make_shared<BitCodeAbbrev>();
3639 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
3640 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3641 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
3642 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3643 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3644 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
3645 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3646 // numrefs x valueid, n x (valueid, hotness)
3647 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3648 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3649 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3650
3651 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
3652 Abbv = std::make_shared<BitCodeAbbrev>();
3653 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
3654 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3655 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
3656 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3657 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3658 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3659 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3660
3661 // Abbrev for FS_COMBINED_ALIAS.
3662 Abbv = std::make_shared<BitCodeAbbrev>();
3663 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
3664 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3665 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
3666 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3667 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3668 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3669
3670 // The aliases are emitted as a post-pass, and will point to the value
3671 // id of the aliasee. Save them in a vector for post-processing.
3672 SmallVector<AliasSummary *, 64> Aliases;
3673
3674 // Save the value id for each summary for alias emission.
3675 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
3676
3677 SmallVector<uint64_t, 64> NameVals;
3678
3679 // For local linkage, we also emit the original name separately
3680 // immediately after the record.
3681 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3682 if (!GlobalValue::isLocalLinkage(S.linkage()))
3683 return;
3684 NameVals.push_back(S.getOriginalName());
3685 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3686 NameVals.clear();
3687 };
3688
3689 forEachSummary([&](GVInfo I, bool IsAliasee) {
3690 GlobalValueSummary *S = I.second;
3691 assert(S)(static_cast <bool> (S) ? void (0) : __assert_fail ("S"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3691, __extension__ __PRETTY_FUNCTION__))
;
3692
3693 auto ValueId = getValueId(I.first);
3694 assert(ValueId)(static_cast <bool> (ValueId) ? void (0) : __assert_fail
("ValueId", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3694, __extension__ __PRETTY_FUNCTION__))
;
3695 SummaryToValueIdMap[S] = *ValueId;
3696
3697 // If this is invoked for an aliasee, we want to record the above
3698 // mapping, but then not emit a summary entry (if the aliasee is
3699 // to be imported, we will invoke this separately with IsAliasee=false).
3700 if (IsAliasee)
3701 return;
3702
3703 if (auto *AS = dyn_cast<AliasSummary>(S)) {
3704 // Will process aliases as a post-pass because the reader wants all
3705 // global to be loaded first.
3706 Aliases.push_back(AS);
3707 return;
3708 }
3709
3710 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
3711 NameVals.push_back(*ValueId);
3712 NameVals.push_back(Index.getModuleId(VS->modulePath()));
3713 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
3714 for (auto &RI : VS->refs()) {
3715 auto RefValueId = getValueId(RI.getGUID());
3716 if (!RefValueId)
3717 continue;
3718 NameVals.push_back(*RefValueId);
3719 }
3720
3721 // Emit the finished record.
3722 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3723 FSModRefsAbbrev);
3724 NameVals.clear();
3725 MaybeEmitOriginalName(*S);
3726 return;
3727 }
3728
3729 auto *FS = cast<FunctionSummary>(S);
3730 writeFunctionTypeMetadataRecords(Stream, FS);
3731
3732 NameVals.push_back(*ValueId);
3733 NameVals.push_back(Index.getModuleId(FS->modulePath()));
3734 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
3735 NameVals.push_back(FS->instCount());
3736 NameVals.push_back(getEncodedFFlags(FS->fflags()));
3737 // Fill in below
3738 NameVals.push_back(0);
3739
3740 unsigned Count = 0;
3741 for (auto &RI : FS->refs()) {
3742 auto RefValueId = getValueId(RI.getGUID());
3743 if (!RefValueId)
3744 continue;
3745 NameVals.push_back(*RefValueId);
3746 Count++;
3747 }
3748 NameVals[5] = Count;
3749
3750 bool HasProfileData = false;
3751 for (auto &EI : FS->calls()) {
3752 HasProfileData |=
3753 EI.second.getHotness() != CalleeInfo::HotnessType::Unknown;
3754 if (HasProfileData)
3755 break;
3756 }
3757
3758 for (auto &EI : FS->calls()) {
3759 // If this GUID doesn't have a value id, it doesn't have a function
3760 // summary and we don't need to record any calls to it.
3761 GlobalValue::GUID GUID = EI.first.getGUID();
3762 auto CallValueId = getValueId(GUID);
3763 if (!CallValueId) {
3764 // For SamplePGO, the indirect call targets for local functions will
3765 // have its original name annotated in profile. We try to find the
3766 // corresponding PGOFuncName as the GUID.
3767 GUID = Index.getGUIDFromOriginalID(GUID);
3768 if (GUID == 0)
3769 continue;
3770 CallValueId = getValueId(GUID);
3771 if (!CallValueId)
3772 continue;
3773 // The mapping from OriginalId to GUID may return a GUID
3774 // that corresponds to a static variable. Filter it out here.
3775 // This can happen when
3776 // 1) There is a call to a library function which does not have
3777 // a CallValidId;
3778 // 2) There is a static variable with the OriginalGUID identical
3779 // to the GUID of the library function in 1);
3780 // When this happens, the logic for SamplePGO kicks in and
3781 // the static variable in 2) will be found, which needs to be
3782 // filtered out.
3783 auto *GVSum = Index.getGlobalValueSummary(GUID, false);
3784 if (GVSum &&
3785 GVSum->getSummaryKind() == GlobalValueSummary::GlobalVarKind)
3786 continue;
3787 }
3788 NameVals.push_back(*CallValueId);
3789 if (HasProfileData)
3790 NameVals.push_back(static_cast<uint8_t>(EI.second.Hotness));
3791 }
3792
3793 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3794 unsigned Code =
3795 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
3796
3797 // Emit the finished record.
3798 Stream.EmitRecord(Code, NameVals, FSAbbrev);
3799 NameVals.clear();
3800 MaybeEmitOriginalName(*S);
3801 });
3802
3803 for (auto *AS : Aliases) {
3804 auto AliasValueId = SummaryToValueIdMap[AS];
3805 assert(AliasValueId)(static_cast <bool> (AliasValueId) ? void (0) : __assert_fail
("AliasValueId", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3805, __extension__ __PRETTY_FUNCTION__))
;
3806 NameVals.push_back(AliasValueId);
3807 NameVals.push_back(Index.getModuleId(AS->modulePath()));
3808 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
3809 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
3810 assert(AliaseeValueId)(static_cast <bool> (AliaseeValueId) ? void (0) : __assert_fail
("AliaseeValueId", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 3810, __extension__ __PRETTY_FUNCTION__))
;
3811 NameVals.push_back(AliaseeValueId);
3812
3813 // Emit the finished record.
3814 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
3815 NameVals.clear();
3816 MaybeEmitOriginalName(*AS);
3817 }
3818
3819 if (!Index.cfiFunctionDefs().empty()) {
3820 for (auto &S : Index.cfiFunctionDefs()) {
3821 NameVals.push_back(StrtabBuilder.add(S));
3822 NameVals.push_back(S.size());
3823 }
3824 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DEFS, NameVals);
3825 NameVals.clear();
3826 }
3827
3828 if (!Index.cfiFunctionDecls().empty()) {
3829 for (auto &S : Index.cfiFunctionDecls()) {
3830 NameVals.push_back(StrtabBuilder.add(S));
3831 NameVals.push_back(S.size());
3832 }
3833 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DECLS, NameVals);
3834 NameVals.clear();
3835 }
3836
3837 if (!Index.typeIds().empty()) {
3838 for (auto &S : Index.typeIds()) {
3839 writeTypeIdSummaryRecord(NameVals, StrtabBuilder, S.first, S.second);
3840 Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
3841 NameVals.clear();
3842 }
3843 }
3844
3845 Stream.ExitBlock();
3846}
3847
3848/// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
3849/// current llvm version, and a record for the epoch number.
3850static void writeIdentificationBlock(BitstreamWriter &Stream) {
3851 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
3852
3853 // Write the "user readable" string identifying the bitcode producer
3854 auto Abbv = std::make_shared<BitCodeAbbrev>();
3855 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3856 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3857 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
3858 auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3859 writeStringRecord(Stream, bitc::IDENTIFICATION_CODE_STRING,
3860 "LLVM" LLVM_VERSION_STRING"7.0.0", StringAbbrev);
3861
3862 // Write the epoch version
3863 Abbv = std::make_shared<BitCodeAbbrev>();
3864 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3865 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
3866 auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3867 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
3868 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3869 Stream.ExitBlock();
3870}
3871
3872void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
3873 // Emit the module's hash.
3874 // MODULE_CODE_HASH: [5*i32]
3875 if (GenerateHash) {
3876 uint32_t Vals[5];
3877 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
3878 Buffer.size() - BlockStartPos));
3879 StringRef Hash = Hasher.result();
3880 for (int Pos = 0; Pos < 20; Pos += 4) {
3881 Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos);
3882 }
3883
3884 // Emit the finished record.
3885 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
3886
3887 if (ModHash)
3888 // Save the written hash value.
3889 std::copy(std::begin(Vals), std::end(Vals), std::begin(*ModHash));
3890 }
3891}
3892
3893void ModuleBitcodeWriter::write() {
3894 writeIdentificationBlock(Stream);
3895
3896 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3897 size_t BlockStartPos = Buffer.size();
3898
3899 writeModuleVersion();
3900
3901 // Emit blockinfo, which defines the standard abbreviations etc.
3902 writeBlockInfo();
3903
3904 // Emit information about attribute groups.
3905 writeAttributeGroupTable();
3906
3907 // Emit information about parameter attributes.
3908 writeAttributeTable();
3909
3910 // Emit information describing all of the types in the module.
3911 writeTypeTable();
3912
3913 writeComdats();
3914
3915 // Emit top-level description of module, including target triple, inline asm,
3916 // descriptors for global variables, and function prototype info.
3917 writeModuleInfo();
3918
3919 // Emit constants.
3920 writeModuleConstants();
3921
3922 // Emit metadata kind names.
3923 writeModuleMetadataKinds();
3924
3925 // Emit metadata.
3926 writeModuleMetadata();
3927
3928 // Emit module-level use-lists.
3929 if (VE.shouldPreserveUseListOrder())
4
Assuming the condition is false
5
Taking false branch
3930 writeUseListBlock(nullptr);
3931
3932 writeOperandBundleTags();
3933 writeSyncScopeNames();
3934
3935 // Emit function bodies.
3936 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
3937 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
6
Loop condition is false. Execution continues on line 3943
3938 if (!F->isDeclaration())
3939 writeFunction(*F, FunctionToBitcodeIndex);
3940
3941 // Need to write after the above call to WriteFunction which populates
3942 // the summary information in the index.
3943 if (Index)
7
Assuming the condition is false
8
Taking false branch
3944 writePerModuleGlobalValueSummary();
3945
3946 writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
9
Calling 'ModuleBitcodeWriter::writeGlobalValueSymbolTable'
3947
3948 writeModuleHash(BlockStartPos);
3949
3950 Stream.ExitBlock();
3951}
3952
3953static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3954 uint32_t &Position) {
3955 support::endian::write32le(&Buffer[Position], Value);
3956 Position += 4;
3957}
3958
3959/// If generating a bc file on darwin, we have to emit a
3960/// header and trailer to make it compatible with the system archiver. To do
3961/// this we emit the following header, and then emit a trailer that pads the
3962/// file out to be a multiple of 16 bytes.
3963///
3964/// struct bc_header {
3965/// uint32_t Magic; // 0x0B17C0DE
3966/// uint32_t Version; // Version, currently always 0.
3967/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3968/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3969/// uint32_t CPUType; // CPU specifier.
3970/// ... potentially more later ...
3971/// };
3972static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
3973 const Triple &TT) {
3974 unsigned CPUType = ~0U;
3975
3976 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
3977 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3978 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3979 // specific constants here because they are implicitly part of the Darwin ABI.
3980 enum {
3981 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3982 DARWIN_CPU_TYPE_X86 = 7,
3983 DARWIN_CPU_TYPE_ARM = 12,
3984 DARWIN_CPU_TYPE_POWERPC = 18
3985 };
3986
3987 Triple::ArchType Arch = TT.getArch();
3988 if (Arch == Triple::x86_64)
3989 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
3990 else if (Arch == Triple::x86)
3991 CPUType = DARWIN_CPU_TYPE_X86;
3992 else if (Arch == Triple::ppc)
3993 CPUType = DARWIN_CPU_TYPE_POWERPC;
3994 else if (Arch == Triple::ppc64)
3995 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
3996 else if (Arch == Triple::arm || Arch == Triple::thumb)
3997 CPUType = DARWIN_CPU_TYPE_ARM;
3998
3999 // Traditional Bitcode starts after header.
4000 assert(Buffer.size() >= BWH_HeaderSize &&(static_cast <bool> (Buffer.size() >= BWH_HeaderSize
&& "Expected header size to be reserved") ? void (0)
: __assert_fail ("Buffer.size() >= BWH_HeaderSize && \"Expected header size to be reserved\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4001, __extension__ __PRETTY_FUNCTION__))
4001 "Expected header size to be reserved")(static_cast <bool> (Buffer.size() >= BWH_HeaderSize
&& "Expected header size to be reserved") ? void (0)
: __assert_fail ("Buffer.size() >= BWH_HeaderSize && \"Expected header size to be reserved\""
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4001, __extension__ __PRETTY_FUNCTION__))
;
4002 unsigned BCOffset = BWH_HeaderSize;
4003 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
4004
4005 // Write the magic and version.
4006 unsigned Position = 0;
4007 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
4008 writeInt32ToBuffer(0, Buffer, Position); // Version.
4009 writeInt32ToBuffer(BCOffset, Buffer, Position);
4010 writeInt32ToBuffer(BCSize, Buffer, Position);
4011 writeInt32ToBuffer(CPUType, Buffer, Position);
4012
4013 // If the file is not a multiple of 16 bytes, insert dummy padding.
4014 while (Buffer.size() & 15)
4015 Buffer.push_back(0);
4016}
4017
4018/// Helper to write the header common to all bitcode files.
4019static void writeBitcodeHeader(BitstreamWriter &Stream) {
4020 // Emit the file header.
4021 Stream.Emit((unsigned)'B', 8);
4022 Stream.Emit((unsigned)'C', 8);
4023 Stream.Emit(0x0, 4);
4024 Stream.Emit(0xC, 4);
4025 Stream.Emit(0xE, 4);
4026 Stream.Emit(0xD, 4);
4027}
4028
4029BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
4030 : Buffer(Buffer), Stream(new BitstreamWriter(Buffer)) {
4031 writeBitcodeHeader(*Stream);
4032}
4033
4034BitcodeWriter::~BitcodeWriter() { assert(WroteStrtab)(static_cast <bool> (WroteStrtab) ? void (0) : __assert_fail
("WroteStrtab", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4034, __extension__ __PRETTY_FUNCTION__))
; }
4035
4036void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
4037 Stream->EnterSubblock(Block, 3);
4038
4039 auto Abbv = std::make_shared<BitCodeAbbrev>();
4040 Abbv->Add(BitCodeAbbrevOp(Record));
4041 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4042 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
4043
4044 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
4045
4046 Stream->ExitBlock();
4047}
4048
4049void BitcodeWriter::writeSymtab() {
4050 assert(!WroteStrtab && !WroteSymtab)(static_cast <bool> (!WroteStrtab && !WroteSymtab
) ? void (0) : __assert_fail ("!WroteStrtab && !WroteSymtab"
, "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4050, __extension__ __PRETTY_FUNCTION__))
;
4051
4052 // If any module has module-level inline asm, we will require a registered asm
4053 // parser for the target so that we can create an accurate symbol table for
4054 // the module.
4055 for (Module *M : Mods) {
4056 if (M->getModuleInlineAsm().empty())
4057 continue;
4058
4059 std::string Err;
4060 const Triple TT(M->getTargetTriple());
4061 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
4062 if (!T || !T->hasMCAsmParser())
4063 return;
4064 }
4065
4066 WroteSymtab = true;
4067 SmallVector<char, 0> Symtab;
4068 // The irsymtab::build function may be unable to create a symbol table if the
4069 // module is malformed (e.g. it contains an invalid alias). Writing a symbol
4070 // table is not required for correctness, but we still want to be able to
4071 // write malformed modules to bitcode files, so swallow the error.
4072 if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
4073 consumeError(std::move(E));
4074 return;
4075 }
4076
4077 writeBlob(bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB,
4078 {Symtab.data(), Symtab.size()});
4079}
4080
4081void BitcodeWriter::writeStrtab() {
4082 assert(!WroteStrtab)(static_cast <bool> (!WroteStrtab) ? void (0) : __assert_fail
("!WroteStrtab", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4082, __extension__ __PRETTY_FUNCTION__))
;
4083
4084 std::vector<char> Strtab;
4085 StrtabBuilder.finalizeInOrder();
4086 Strtab.resize(StrtabBuilder.getSize());
4087 StrtabBuilder.write((uint8_t *)Strtab.data());
4088
4089 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB,
4090 {Strtab.data(), Strtab.size()});
4091
4092 WroteStrtab = true;
4093}
4094
4095void BitcodeWriter::copyStrtab(StringRef Strtab) {
4096 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
4097 WroteStrtab = true;
4098}
4099
4100void BitcodeWriter::writeModule(const Module &M,
4101 bool ShouldPreserveUseListOrder,
4102 const ModuleSummaryIndex *Index,
4103 bool GenerateHash, ModuleHash *ModHash) {
4104 assert(!WroteStrtab)(static_cast <bool> (!WroteStrtab) ? void (0) : __assert_fail
("!WroteStrtab", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4104, __extension__ __PRETTY_FUNCTION__))
;
4105
4106 // The Mods vector is used by irsymtab::build, which requires non-const
4107 // Modules in case it needs to materialize metadata. But the bitcode writer
4108 // requires that the module is materialized, so we can cast to non-const here,
4109 // after checking that it is in fact materialized.
4110 assert(M.isMaterialized())(static_cast <bool> (M.isMaterialized()) ? void (0) : __assert_fail
("M.isMaterialized()", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4110, __extension__ __PRETTY_FUNCTION__))
;
4111 Mods.push_back(const_cast<Module *>(&M));
4112
4113 ModuleBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream,
4114 ShouldPreserveUseListOrder, Index,
4115 GenerateHash, ModHash);
4116 ModuleWriter.write();
3
Calling 'ModuleBitcodeWriter::write'
4117}
4118
4119void BitcodeWriter::writeIndex(
4120 const ModuleSummaryIndex *Index,
4121 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
4122 IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index,
4123 ModuleToSummariesForIndex);
4124 IndexWriter.write();
4125}
4126
4127/// Write the specified module to the specified output stream.
4128void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
4129 bool ShouldPreserveUseListOrder,
4130 const ModuleSummaryIndex *Index,
4131 bool GenerateHash, ModuleHash *ModHash) {
4132 SmallVector<char, 0> Buffer;
4133 Buffer.reserve(256*1024);
4134
4135 // If this is darwin or another generic macho target, reserve space for the
4136 // header.
4137 Triple TT(M.getTargetTriple());
4138 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
1
Taking false branch
4139 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
4140
4141 BitcodeWriter Writer(Buffer);
4142 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
2
Calling 'BitcodeWriter::writeModule'
4143 ModHash);
4144 Writer.writeSymtab();
4145 Writer.writeStrtab();
4146
4147 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
4148 emitDarwinBCHeaderAndTrailer(Buffer, TT);
4149
4150 // Write the generated bitstream to "Out".
4151 Out.write((char*)&Buffer.front(), Buffer.size());
4152}
4153
4154void IndexBitcodeWriter::write() {
4155 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
4156
4157 writeModuleVersion();
4158
4159 // Write the module paths in the combined index.
4160 writeModStrings();
4161
4162 // Write the summary combined index records.
4163 writeCombinedGlobalValueSummary();
4164
4165 Stream.ExitBlock();
4166}
4167
4168// Write the specified module summary index to the given raw output stream,
4169// where it will be written in a new bitcode block. This is used when
4170// writing the combined index file for ThinLTO. When writing a subset of the
4171// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
4172void llvm::WriteIndexToFile(
4173 const ModuleSummaryIndex &Index, raw_ostream &Out,
4174 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
4175 SmallVector<char, 0> Buffer;
4176 Buffer.reserve(256 * 1024);
4177
4178 BitcodeWriter Writer(Buffer);
4179 Writer.writeIndex(&Index, ModuleToSummariesForIndex);
4180 Writer.writeStrtab();
4181
4182 Out.write((char *)&Buffer.front(), Buffer.size());
4183}
4184
4185namespace {
4186
4187/// Class to manage the bitcode writing for a thin link bitcode file.
4188class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase {
4189 /// ModHash is for use in ThinLTO incremental build, generated while writing
4190 /// the module bitcode file.
4191 const ModuleHash *ModHash;
4192
4193public:
4194 ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
4195 BitstreamWriter &Stream,
4196 const ModuleSummaryIndex &Index,
4197 const ModuleHash &ModHash)
4198 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
4199 /*ShouldPreserveUseListOrder=*/false, &Index),
4200 ModHash(&ModHash) {}
4201
4202 void write();
4203
4204private:
4205 void writeSimplifiedModuleInfo();
4206};
4207
4208} // end anonymous namespace
4209
4210// This function writes a simpilified module info for thin link bitcode file.
4211// It only contains the source file name along with the name(the offset and
4212// size in strtab) and linkage for global values. For the global value info
4213// entry, in order to keep linkage at offset 5, there are three zeros used
4214// as padding.
4215void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() {
4216 SmallVector<unsigned, 64> Vals;
4217 // Emit the module's source file name.
4218 {
4219 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
4220 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
4221 if (Bits == SE_Char6)
4222 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
4223 else if (Bits == SE_Fixed7)
4224 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
4225
4226 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
4227 auto Abbv = std::make_shared<BitCodeAbbrev>();
4228 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
4229 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4230 Abbv->Add(AbbrevOpToUse);
4231 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4232
4233 for (const auto P : M.getSourceFileName())
4234 Vals.push_back((unsigned char)P);
4235
4236 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
4237 Vals.clear();
4238 }
4239
4240 // Emit the global variable information.
4241 for (const GlobalVariable &GV : M.globals()) {
4242 // GLOBALVAR: [strtab offset, strtab size, 0, 0, 0, linkage]
4243 Vals.push_back(StrtabBuilder.add(GV.getName()));
4244 Vals.push_back(GV.getName().size());
4245 Vals.push_back(0);
4246 Vals.push_back(0);
4247 Vals.push_back(0);
4248 Vals.push_back(getEncodedLinkage(GV));
4249
4250 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals);
4251 Vals.clear();
4252 }
4253
4254 // Emit the function proto information.
4255 for (const Function &F : M) {
4256 // FUNCTION: [strtab offset, strtab size, 0, 0, 0, linkage]
4257 Vals.push_back(StrtabBuilder.add(F.getName()));
4258 Vals.push_back(F.getName().size());
4259 Vals.push_back(0);
4260 Vals.push_back(0);
4261 Vals.push_back(0);
4262 Vals.push_back(getEncodedLinkage(F));
4263
4264 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals);
4265 Vals.clear();
4266 }
4267
4268 // Emit the alias information.
4269 for (const GlobalAlias &A : M.aliases()) {
4270 // ALIAS: [strtab offset, strtab size, 0, 0, 0, linkage]
4271 Vals.push_back(StrtabBuilder.add(A.getName()));
4272 Vals.push_back(A.getName().size());
4273 Vals.push_back(0);
4274 Vals.push_back(0);
4275 Vals.push_back(0);
4276 Vals.push_back(getEncodedLinkage(A));
4277
4278 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals);
4279 Vals.clear();
4280 }
4281
4282 // Emit the ifunc information.
4283 for (const GlobalIFunc &I : M.ifuncs()) {
4284 // IFUNC: [strtab offset, strtab size, 0, 0, 0, linkage]
4285 Vals.push_back(StrtabBuilder.add(I.getName()));
4286 Vals.push_back(I.getName().size());
4287 Vals.push_back(0);
4288 Vals.push_back(0);
4289 Vals.push_back(0);
4290 Vals.push_back(getEncodedLinkage(I));
4291
4292 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
4293 Vals.clear();
4294 }
4295}
4296
4297void ThinLinkBitcodeWriter::write() {
4298 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
4299
4300 writeModuleVersion();
4301
4302 writeSimplifiedModuleInfo();
4303
4304 writePerModuleGlobalValueSummary();
4305
4306 // Write module hash.
4307 Stream.EmitRecord(bitc::MODULE_CODE_HASH, ArrayRef<uint32_t>(*ModHash));
4308
4309 Stream.ExitBlock();
4310}
4311
4312void BitcodeWriter::writeThinLinkBitcode(const Module &M,
4313 const ModuleSummaryIndex &Index,
4314 const ModuleHash &ModHash) {
4315 assert(!WroteStrtab)(static_cast <bool> (!WroteStrtab) ? void (0) : __assert_fail
("!WroteStrtab", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4315, __extension__ __PRETTY_FUNCTION__))
;
4316
4317 // The Mods vector is used by irsymtab::build, which requires non-const
4318 // Modules in case it needs to materialize metadata. But the bitcode writer
4319 // requires that the module is materialized, so we can cast to non-const here,
4320 // after checking that it is in fact materialized.
4321 assert(M.isMaterialized())(static_cast <bool> (M.isMaterialized()) ? void (0) : __assert_fail
("M.isMaterialized()", "/build/llvm-toolchain-snapshot-7~svn326425/lib/Bitcode/Writer/BitcodeWriter.cpp"
, 4321, __extension__ __PRETTY_FUNCTION__))
;
4322 Mods.push_back(const_cast<Module *>(&M));
4323
4324 ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index,
4325 ModHash);
4326 ThinLinkWriter.write();
4327}
4328
4329// Write the specified thin link bitcode file to the given raw output stream,
4330// where it will be written in a new bitcode block. This is used when
4331// writing the per-module index file for ThinLTO.
4332void llvm::WriteThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
4333 const ModuleSummaryIndex &Index,
4334 const ModuleHash &ModHash) {
4335 SmallVector<char, 0> Buffer;
4336 Buffer.reserve(256 * 1024);
4337
4338 BitcodeWriter Writer(Buffer);
4339 Writer.writeThinLinkBitcode(M, Index, ModHash);
4340 Writer.writeSymtab();
4341 Writer.writeStrtab();
4342
4343 Out.write((char *)&Buffer.front(), Buffer.size());
4344}

/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h

1//===- BitstreamWriter.h - Low-level bitstream writer interface -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the BitstreamWriter class. This class can be used to
11// write an arbitrary bitstream, regardless of its contents.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_BITCODE_BITSTREAMWRITER_H
16#define LLVM_BITCODE_BITSTREAMWRITER_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/Optional.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Bitcode/BitCodes.h"
23#include "llvm/Support/Endian.h"
24#include <vector>
25
26namespace llvm {
27
28class BitstreamWriter {
29 SmallVectorImpl<char> &Out;
30
31 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
32 unsigned CurBit;
33
34 /// CurValue - The current value. Only bits < CurBit are valid.
35 uint32_t CurValue;
36
37 /// CurCodeSize - This is the declared size of code values used for the
38 /// current block, in bits.
39 unsigned CurCodeSize;
40
41 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently
42 /// selected BLOCK ID.
43 unsigned BlockInfoCurBID;
44
45 /// CurAbbrevs - Abbrevs installed at in this block.
46 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
47
48 struct Block {
49 unsigned PrevCodeSize;
50 size_t StartSizeWord;
51 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
52 Block(unsigned PCS, size_t SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
53 };
54
55 /// BlockScope - This tracks the current blocks that we have entered.
56 std::vector<Block> BlockScope;
57
58 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
59 /// These describe abbreviations that all blocks of the specified ID inherit.
60 struct BlockInfo {
61 unsigned BlockID;
62 std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
63 };
64 std::vector<BlockInfo> BlockInfoRecords;
65
66 void WriteByte(unsigned char Value) {
67 Out.push_back(Value);
68 }
69
70 void WriteWord(unsigned Value) {
71 Value = support::endian::byte_swap<uint32_t, support::little>(Value);
72 Out.append(reinterpret_cast<const char *>(&Value),
73 reinterpret_cast<const char *>(&Value + 1));
74 }
75
76 size_t GetBufferOffset() const { return Out.size(); }
77
78 size_t GetWordIndex() const {
79 size_t Offset = GetBufferOffset();
80 assert((Offset & 3) == 0 && "Not 32-bit aligned")(static_cast <bool> ((Offset & 3) == 0 && "Not 32-bit aligned"
) ? void (0) : __assert_fail ("(Offset & 3) == 0 && \"Not 32-bit aligned\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 80, __extension__ __PRETTY_FUNCTION__))
;
81 return Offset / 4;
82 }
83
84public:
85 explicit BitstreamWriter(SmallVectorImpl<char> &O)
86 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
87
88 ~BitstreamWriter() {
89 assert(CurBit == 0 && "Unflushed data remaining")(static_cast <bool> (CurBit == 0 && "Unflushed data remaining"
) ? void (0) : __assert_fail ("CurBit == 0 && \"Unflushed data remaining\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 89, __extension__ __PRETTY_FUNCTION__))
;
90 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance")(static_cast <bool> (BlockScope.empty() && CurAbbrevs
.empty() && "Block imbalance") ? void (0) : __assert_fail
("BlockScope.empty() && CurAbbrevs.empty() && \"Block imbalance\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 90, __extension__ __PRETTY_FUNCTION__))
;
91 }
92
93 /// \brief Retrieve the current position in the stream, in bits.
94 uint64_t GetCurrentBitNo() const { return GetBufferOffset() * 8 + CurBit; }
95
96 /// \brief Retrieve the number of bits currently used to encode an abbrev ID.
97 unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
98
99 //===--------------------------------------------------------------------===//
100 // Basic Primitives for emitting bits to the stream.
101 //===--------------------------------------------------------------------===//
102
103 /// Backpatch a 32-bit word in the output at the given bit offset
104 /// with the specified value.
105 void BackpatchWord(uint64_t BitNo, unsigned NewWord) {
106 using namespace llvm::support;
107 unsigned ByteNo = BitNo / 8;
108 assert((!endian::readAtBitAlignment<uint32_t, little, unaligned>((static_cast <bool> ((!endian::readAtBitAlignment<uint32_t
, little, unaligned>( &Out[ByteNo], BitNo & 7)) &&
"Expected to be patching over 0-value placeholders") ? void (
0) : __assert_fail ("(!endian::readAtBitAlignment<uint32_t, little, unaligned>( &Out[ByteNo], BitNo & 7)) && \"Expected to be patching over 0-value placeholders\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 110, __extension__ __PRETTY_FUNCTION__))
109 &Out[ByteNo], BitNo & 7)) &&(static_cast <bool> ((!endian::readAtBitAlignment<uint32_t
, little, unaligned>( &Out[ByteNo], BitNo & 7)) &&
"Expected to be patching over 0-value placeholders") ? void (
0) : __assert_fail ("(!endian::readAtBitAlignment<uint32_t, little, unaligned>( &Out[ByteNo], BitNo & 7)) && \"Expected to be patching over 0-value placeholders\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 110, __extension__ __PRETTY_FUNCTION__))
110 "Expected to be patching over 0-value placeholders")(static_cast <bool> ((!endian::readAtBitAlignment<uint32_t
, little, unaligned>( &Out[ByteNo], BitNo & 7)) &&
"Expected to be patching over 0-value placeholders") ? void (
0) : __assert_fail ("(!endian::readAtBitAlignment<uint32_t, little, unaligned>( &Out[ByteNo], BitNo & 7)) && \"Expected to be patching over 0-value placeholders\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 110, __extension__ __PRETTY_FUNCTION__))
;
111 endian::writeAtBitAlignment<uint32_t, little, unaligned>(
112 &Out[ByteNo], NewWord, BitNo & 7);
113 }
114
115 void BackpatchWord64(uint64_t BitNo, uint64_t Val) {
116 BackpatchWord(BitNo, (uint32_t)Val);
117 BackpatchWord(BitNo + 32, (uint32_t)(Val >> 32));
118 }
119
120 void Emit(uint32_t Val, unsigned NumBits) {
121 assert(NumBits && NumBits <= 32 && "Invalid value size!")(static_cast <bool> (NumBits && NumBits <= 32
&& "Invalid value size!") ? void (0) : __assert_fail
("NumBits && NumBits <= 32 && \"Invalid value size!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 121, __extension__ __PRETTY_FUNCTION__))
;
122 assert((Val & ~(~0U >> (32-NumBits))) == 0 && "High bits set!")(static_cast <bool> ((Val & ~(~0U >> (32-NumBits
))) == 0 && "High bits set!") ? void (0) : __assert_fail
("(Val & ~(~0U >> (32-NumBits))) == 0 && \"High bits set!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 122, __extension__ __PRETTY_FUNCTION__))
;
123 CurValue |= Val << CurBit;
124 if (CurBit + NumBits < 32) {
125 CurBit += NumBits;
126 return;
127 }
128
129 // Add the current word.
130 WriteWord(CurValue);
131
132 if (CurBit)
133 CurValue = Val >> (32-CurBit);
134 else
135 CurValue = 0;
136 CurBit = (CurBit+NumBits) & 31;
137 }
138
139 void FlushToWord() {
140 if (CurBit) {
141 WriteWord(CurValue);
142 CurBit = 0;
143 CurValue = 0;
144 }
145 }
146
147 void EmitVBR(uint32_t Val, unsigned NumBits) {
148 assert(NumBits <= 32 && "Too many bits to emit!")(static_cast <bool> (NumBits <= 32 && "Too many bits to emit!"
) ? void (0) : __assert_fail ("NumBits <= 32 && \"Too many bits to emit!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 148, __extension__ __PRETTY_FUNCTION__))
;
149 uint32_t Threshold = 1U << (NumBits-1);
150
151 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
152 while (Val >= Threshold) {
153 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
154 Val >>= NumBits-1;
155 }
156
157 Emit(Val, NumBits);
158 }
159
160 void EmitVBR64(uint64_t Val, unsigned NumBits) {
161 assert(NumBits <= 32 && "Too many bits to emit!")(static_cast <bool> (NumBits <= 32 && "Too many bits to emit!"
) ? void (0) : __assert_fail ("NumBits <= 32 && \"Too many bits to emit!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 161, __extension__ __PRETTY_FUNCTION__))
;
162 if ((uint32_t)Val == Val)
163 return EmitVBR((uint32_t)Val, NumBits);
164
165 uint32_t Threshold = 1U << (NumBits-1);
166
167 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
168 while (Val >= Threshold) {
169 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
170 (1 << (NumBits-1)), NumBits);
171 Val >>= NumBits-1;
172 }
173
174 Emit((uint32_t)Val, NumBits);
175 }
176
177 /// EmitCode - Emit the specified code.
178 void EmitCode(unsigned Val) {
179 Emit(Val, CurCodeSize);
180 }
181
182 //===--------------------------------------------------------------------===//
183 // Block Manipulation
184 //===--------------------------------------------------------------------===//
185
186 /// getBlockInfo - If there is block info for the specified ID, return it,
187 /// otherwise return null.
188 BlockInfo *getBlockInfo(unsigned BlockID) {
189 // Common case, the most recent entry matches BlockID.
190 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
191 return &BlockInfoRecords.back();
192
193 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
194 i != e; ++i)
195 if (BlockInfoRecords[i].BlockID == BlockID)
196 return &BlockInfoRecords[i];
197 return nullptr;
198 }
199
200 void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
201 // Block header:
202 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
203 EmitCode(bitc::ENTER_SUBBLOCK);
204 EmitVBR(BlockID, bitc::BlockIDWidth);
205 EmitVBR(CodeLen, bitc::CodeLenWidth);
206 FlushToWord();
207
208 size_t BlockSizeWordIndex = GetWordIndex();
209 unsigned OldCodeSize = CurCodeSize;
210
211 // Emit a placeholder, which will be replaced when the block is popped.
212 Emit(0, bitc::BlockSizeWidth);
213
214 CurCodeSize = CodeLen;
215
216 // Push the outer block's abbrev set onto the stack, start out with an
217 // empty abbrev set.
218 BlockScope.emplace_back(OldCodeSize, BlockSizeWordIndex);
219 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
220
221 // If there is a blockinfo for this BlockID, add all the predefined abbrevs
222 // to the abbrev list.
223 if (BlockInfo *Info = getBlockInfo(BlockID)) {
224 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
225 Info->Abbrevs.end());
226 }
227 }
228
229 void ExitBlock() {
230 assert(!BlockScope.empty() && "Block scope imbalance!")(static_cast <bool> (!BlockScope.empty() && "Block scope imbalance!"
) ? void (0) : __assert_fail ("!BlockScope.empty() && \"Block scope imbalance!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 230, __extension__ __PRETTY_FUNCTION__))
;
231 const Block &B = BlockScope.back();
232
233 // Block tail:
234 // [END_BLOCK, <align4bytes>]
235 EmitCode(bitc::END_BLOCK);
236 FlushToWord();
237
238 // Compute the size of the block, in words, not counting the size field.
239 size_t SizeInWords = GetWordIndex() - B.StartSizeWord - 1;
240 uint64_t BitNo = uint64_t(B.StartSizeWord) * 32;
241
242 // Update the block size field in the header of this sub-block.
243 BackpatchWord(BitNo, SizeInWords);
244
245 // Restore the inner block's code size and abbrev table.
246 CurCodeSize = B.PrevCodeSize;
247 CurAbbrevs = std::move(B.PrevAbbrevs);
248 BlockScope.pop_back();
249 }
250
251 //===--------------------------------------------------------------------===//
252 // Record Emission
253 //===--------------------------------------------------------------------===//
254
255private:
256 /// EmitAbbreviatedLiteral - Emit a literal value according to its abbrev
257 /// record. This is a no-op, since the abbrev specifies the literal to use.
258 template<typename uintty>
259 void EmitAbbreviatedLiteral(const BitCodeAbbrevOp &Op, uintty V) {
260 assert(Op.isLiteral() && "Not a literal")(static_cast <bool> (Op.isLiteral() && "Not a literal"
) ? void (0) : __assert_fail ("Op.isLiteral() && \"Not a literal\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 260, __extension__ __PRETTY_FUNCTION__))
;
261 // If the abbrev specifies the literal value to use, don't emit
262 // anything.
263 assert(V == Op.getLiteralValue() &&(static_cast <bool> (V == Op.getLiteralValue() &&
"Invalid abbrev for record!") ? void (0) : __assert_fail ("V == Op.getLiteralValue() && \"Invalid abbrev for record!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 264, __extension__ __PRETTY_FUNCTION__))
264 "Invalid abbrev for record!")(static_cast <bool> (V == Op.getLiteralValue() &&
"Invalid abbrev for record!") ? void (0) : __assert_fail ("V == Op.getLiteralValue() && \"Invalid abbrev for record!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 264, __extension__ __PRETTY_FUNCTION__))
;
265 }
266
267 /// EmitAbbreviatedField - Emit a single scalar field value with the specified
268 /// encoding.
269 template<typename uintty>
270 void EmitAbbreviatedField(const BitCodeAbbrevOp &Op, uintty V) {
271 assert(!Op.isLiteral() && "Literals should use EmitAbbreviatedLiteral!")(static_cast <bool> (!Op.isLiteral() && "Literals should use EmitAbbreviatedLiteral!"
) ? void (0) : __assert_fail ("!Op.isLiteral() && \"Literals should use EmitAbbreviatedLiteral!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 271, __extension__ __PRETTY_FUNCTION__))
;
272
273 // Encode the value as we are commanded.
274 switch (Op.getEncoding()) {
275 default: llvm_unreachable("Unknown encoding!")::llvm::llvm_unreachable_internal("Unknown encoding!", "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 275)
;
276 case BitCodeAbbrevOp::Fixed:
277 if (Op.getEncodingData())
278 Emit((unsigned)V, (unsigned)Op.getEncodingData());
279 break;
280 case BitCodeAbbrevOp::VBR:
281 if (Op.getEncodingData())
282 EmitVBR64(V, (unsigned)Op.getEncodingData());
283 break;
284 case BitCodeAbbrevOp::Char6:
285 Emit(BitCodeAbbrevOp::EncodeChar6((char)V), 6);
286 break;
287 }
288 }
289
290 /// EmitRecordWithAbbrevImpl - This is the core implementation of the record
291 /// emission code. If BlobData is non-null, then it specifies an array of
292 /// data that should be emitted as part of the Blob or Array operand that is
293 /// known to exist at the end of the record. If Code is specified, then
294 /// it is the record code to emit before the Vals, which must not contain
295 /// the code.
296 template <typename uintty>
297 void EmitRecordWithAbbrevImpl(unsigned Abbrev, ArrayRef<uintty> Vals,
298 StringRef Blob, Optional<unsigned> Code) {
299 const char *BlobData = Blob.data();
300 unsigned BlobLen = (unsigned) Blob.size();
301 unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
302 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!")(static_cast <bool> (AbbrevNo < CurAbbrevs.size() &&
"Invalid abbrev #!") ? void (0) : __assert_fail ("AbbrevNo < CurAbbrevs.size() && \"Invalid abbrev #!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 302, __extension__ __PRETTY_FUNCTION__))
;
303 const BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo].get();
304
305 EmitCode(Abbrev);
306
307 unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos());
308 if (Code) {
309 assert(e && "Expected non-empty abbreviation")(static_cast <bool> (e && "Expected non-empty abbreviation"
) ? void (0) : __assert_fail ("e && \"Expected non-empty abbreviation\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 309, __extension__ __PRETTY_FUNCTION__))
;
310 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i++);
311
312 if (Op.isLiteral())
313 EmitAbbreviatedLiteral(Op, Code.getValue());
314 else {
315 assert(Op.getEncoding() != BitCodeAbbrevOp::Array &&(static_cast <bool> (Op.getEncoding() != BitCodeAbbrevOp
::Array && Op.getEncoding() != BitCodeAbbrevOp::Blob &&
"Expected literal or scalar") ? void (0) : __assert_fail ("Op.getEncoding() != BitCodeAbbrevOp::Array && Op.getEncoding() != BitCodeAbbrevOp::Blob && \"Expected literal or scalar\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 317, __extension__ __PRETTY_FUNCTION__))
316 Op.getEncoding() != BitCodeAbbrevOp::Blob &&(static_cast <bool> (Op.getEncoding() != BitCodeAbbrevOp
::Array && Op.getEncoding() != BitCodeAbbrevOp::Blob &&
"Expected literal or scalar") ? void (0) : __assert_fail ("Op.getEncoding() != BitCodeAbbrevOp::Array && Op.getEncoding() != BitCodeAbbrevOp::Blob && \"Expected literal or scalar\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 317, __extension__ __PRETTY_FUNCTION__))
317 "Expected literal or scalar")(static_cast <bool> (Op.getEncoding() != BitCodeAbbrevOp
::Array && Op.getEncoding() != BitCodeAbbrevOp::Blob &&
"Expected literal or scalar") ? void (0) : __assert_fail ("Op.getEncoding() != BitCodeAbbrevOp::Array && Op.getEncoding() != BitCodeAbbrevOp::Blob && \"Expected literal or scalar\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 317, __extension__ __PRETTY_FUNCTION__))
;
318 EmitAbbreviatedField(Op, Code.getValue());
319 }
320 }
321
322 unsigned RecordIdx = 0;
323 for (; i != e; ++i) {
324 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
325 if (Op.isLiteral()) {
326 assert(RecordIdx < Vals.size() && "Invalid abbrev/record")(static_cast <bool> (RecordIdx < Vals.size() &&
"Invalid abbrev/record") ? void (0) : __assert_fail ("RecordIdx < Vals.size() && \"Invalid abbrev/record\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 326, __extension__ __PRETTY_FUNCTION__))
;
327 EmitAbbreviatedLiteral(Op, Vals[RecordIdx]);
328 ++RecordIdx;
329 } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
330 // Array case.
331 assert(i + 2 == e && "array op not second to last?")(static_cast <bool> (i + 2 == e && "array op not second to last?"
) ? void (0) : __assert_fail ("i + 2 == e && \"array op not second to last?\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 331, __extension__ __PRETTY_FUNCTION__))
;
332 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
333
334 // If this record has blob data, emit it, otherwise we must have record
335 // entries to encode this way.
336 if (BlobData) {
337 assert(RecordIdx == Vals.size() &&(static_cast <bool> (RecordIdx == Vals.size() &&
"Blob data and record entries specified for array!") ? void (
0) : __assert_fail ("RecordIdx == Vals.size() && \"Blob data and record entries specified for array!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 338, __extension__ __PRETTY_FUNCTION__))
338 "Blob data and record entries specified for array!")(static_cast <bool> (RecordIdx == Vals.size() &&
"Blob data and record entries specified for array!") ? void (
0) : __assert_fail ("RecordIdx == Vals.size() && \"Blob data and record entries specified for array!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 338, __extension__ __PRETTY_FUNCTION__))
;
339 // Emit a vbr6 to indicate the number of elements present.
340 EmitVBR(static_cast<uint32_t>(BlobLen), 6);
341
342 // Emit each field.
343 for (unsigned i = 0; i != BlobLen; ++i)
344 EmitAbbreviatedField(EltEnc, (unsigned char)BlobData[i]);
345
346 // Know that blob data is consumed for assertion below.
347 BlobData = nullptr;
348 } else {
349 // Emit a vbr6 to indicate the number of elements present.
350 EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6);
351
352 // Emit each field.
353 for (unsigned e = Vals.size(); RecordIdx != e; ++RecordIdx)
354 EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
355 }
356 } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) {
357 // If this record has blob data, emit it, otherwise we must have record
358 // entries to encode this way.
359
360 if (BlobData) {
361 assert(RecordIdx == Vals.size() &&(static_cast <bool> (RecordIdx == Vals.size() &&
"Blob data and record entries specified for blob operand!") ?
void (0) : __assert_fail ("RecordIdx == Vals.size() && \"Blob data and record entries specified for blob operand!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 362, __extension__ __PRETTY_FUNCTION__))
362 "Blob data and record entries specified for blob operand!")(static_cast <bool> (RecordIdx == Vals.size() &&
"Blob data and record entries specified for blob operand!") ?
void (0) : __assert_fail ("RecordIdx == Vals.size() && \"Blob data and record entries specified for blob operand!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 362, __extension__ __PRETTY_FUNCTION__))
;
363
364 assert(Blob.data() == BlobData && "BlobData got moved")(static_cast <bool> (Blob.data() == BlobData &&
"BlobData got moved") ? void (0) : __assert_fail ("Blob.data() == BlobData && \"BlobData got moved\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 364, __extension__ __PRETTY_FUNCTION__))
;
365 assert(Blob.size() == BlobLen && "BlobLen got changed")(static_cast <bool> (Blob.size() == BlobLen && "BlobLen got changed"
) ? void (0) : __assert_fail ("Blob.size() == BlobLen && \"BlobLen got changed\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 365, __extension__ __PRETTY_FUNCTION__))
;
366 emitBlob(Blob);
367 BlobData = nullptr;
368 } else {
369 emitBlob(Vals.slice(RecordIdx));
370 }
371 } else { // Single scalar field.
372 assert(RecordIdx < Vals.size() && "Invalid abbrev/record")(static_cast <bool> (RecordIdx < Vals.size() &&
"Invalid abbrev/record") ? void (0) : __assert_fail ("RecordIdx < Vals.size() && \"Invalid abbrev/record\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 372, __extension__ __PRETTY_FUNCTION__))
;
373 EmitAbbreviatedField(Op, Vals[RecordIdx]);
374 ++RecordIdx;
375 }
376 }
377 assert(RecordIdx == Vals.size() && "Not all record operands emitted!")(static_cast <bool> (RecordIdx == Vals.size() &&
"Not all record operands emitted!") ? void (0) : __assert_fail
("RecordIdx == Vals.size() && \"Not all record operands emitted!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 377, __extension__ __PRETTY_FUNCTION__))
;
378 assert(BlobData == nullptr &&(static_cast <bool> (BlobData == nullptr && "Blob data specified for record that doesn't use it!"
) ? void (0) : __assert_fail ("BlobData == nullptr && \"Blob data specified for record that doesn't use it!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 379, __extension__ __PRETTY_FUNCTION__))
379 "Blob data specified for record that doesn't use it!")(static_cast <bool> (BlobData == nullptr && "Blob data specified for record that doesn't use it!"
) ? void (0) : __assert_fail ("BlobData == nullptr && \"Blob data specified for record that doesn't use it!\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 379, __extension__ __PRETTY_FUNCTION__))
;
380 }
381
382public:
383 /// Emit a blob, including flushing before and tail-padding.
384 template <class UIntTy>
385 void emitBlob(ArrayRef<UIntTy> Bytes, bool ShouldEmitSize = true) {
386 // Emit a vbr6 to indicate the number of elements present.
387 if (ShouldEmitSize)
388 EmitVBR(static_cast<uint32_t>(Bytes.size()), 6);
389
390 // Flush to a 32-bit alignment boundary.
391 FlushToWord();
392
393 // Emit literal bytes.
394 for (const auto &B : Bytes) {
395 assert(isUInt<8>(B) && "Value too large to emit as byte")(static_cast <bool> (isUInt<8>(B) && "Value too large to emit as byte"
) ? void (0) : __assert_fail ("isUInt<8>(B) && \"Value too large to emit as byte\""
, "/build/llvm-toolchain-snapshot-7~svn326425/include/llvm/Bitcode/BitstreamWriter.h"
, 395, __extension__ __PRETTY_FUNCTION__))
;
396 WriteByte((unsigned char)B);
397 }
398
399 // Align end to 32-bits.
400 while (GetBufferOffset() & 3)
401 WriteByte(0);
402 }
403 void emitBlob(StringRef Bytes, bool ShouldEmitSize = true) {
404 emitBlob(makeArrayRef((const uint8_t *)Bytes.data(), Bytes.size()),
405 ShouldEmitSize);
406 }
407
408 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
409 /// we have one to compress the output.
410 template <typename Container>
411 void EmitRecord(unsigned Code, const Container &Vals, unsigned Abbrev = 0) {
412 if (!Abbrev) {
13
Assuming 'Abbrev' is 0
14
Taking true branch
413 // If we don't have an abbrev to use, emit this in its fully unabbreviated
414 // form.
415 auto Count = static_cast<uint32_t>(makeArrayRef(Vals).size());
416 EmitCode(bitc::UNABBREV_RECORD);
417 EmitVBR(Code, 6);
418 EmitVBR(Count, 6);
419 for (unsigned i = 0, e = Count; i != e; ++i)
15
Assuming 'i' is not equal to 'e'
16
Loop condition is true. Entering loop body
17
Assuming 'i' is not equal to 'e'
18
Loop condition is true. Entering loop body
19
Assuming 'i' is not equal to 'e'
20
Loop condition is true. Entering loop body
420 EmitVBR64(Vals[i], 6);
21
1st function call argument is an uninitialized value
421 return;
422 }
423
424 EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), StringRef(), Code);
425 }
426
427 /// EmitRecordWithAbbrev - Emit a record with the specified abbreviation.
428 /// Unlike EmitRecord, the code for the record should be included in Vals as
429 /// the first entry.
430 template <typename Container>
431 void EmitRecordWithAbbrev(unsigned Abbrev, const Container &Vals) {
432 EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), StringRef(), None);
433 }
434
435 /// EmitRecordWithBlob - Emit the specified record to the stream, using an
436 /// abbrev that includes a blob at the end. The blob data to emit is
437 /// specified by the pointer and length specified at the end. In contrast to
438 /// EmitRecord, this routine expects that the first entry in Vals is the code
439 /// of the record.
440 template <typename Container>
441 void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals,
442 StringRef Blob) {
443 EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), Blob, None);
444 }
445 template <typename Container>
446 void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals,
447 const char *BlobData, unsigned BlobLen) {
448 return EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals),
449 StringRef(BlobData, BlobLen), None);
450 }
451
452 /// EmitRecordWithArray - Just like EmitRecordWithBlob, works with records
453 /// that end with an array.
454 template <typename Container>
455 void EmitRecordWithArray(unsigned Abbrev, const Container &Vals,
456 StringRef Array) {
457 EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), Array, None);
458 }
459 template <typename Container>
460 void EmitRecordWithArray(unsigned Abbrev, const Container &Vals,
461 const char *ArrayData, unsigned ArrayLen) {
462 return EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals),
463 StringRef(ArrayData, ArrayLen), None);
464 }
465
466 //===--------------------------------------------------------------------===//
467 // Abbrev Emission
468 //===--------------------------------------------------------------------===//
469
470private:
471 // Emit the abbreviation as a DEFINE_ABBREV record.
472 void EncodeAbbrev(const BitCodeAbbrev &Abbv) {
473 EmitCode(bitc::DEFINE_ABBREV);
474 EmitVBR(Abbv.getNumOperandInfos(), 5);
475 for (unsigned i = 0, e = static_cast<unsigned>(Abbv.getNumOperandInfos());
476 i != e; ++i) {
477 const BitCodeAbbrevOp &Op = Abbv.getOperandInfo(i);
478 Emit(Op.isLiteral(), 1);
479 if (Op.isLiteral()) {
480 EmitVBR64(Op.getLiteralValue(), 8);
481 } else {
482 Emit(Op.getEncoding(), 3);
483 if (Op.hasEncodingData())
484 EmitVBR64(Op.getEncodingData(), 5);
485 }
486 }
487 }
488public:
489
490 /// EmitAbbrev - This emits an abbreviation to the stream. Note that this
491 /// method takes ownership of the specified abbrev.
492 unsigned EmitAbbrev(std::shared_ptr<BitCodeAbbrev> Abbv) {
493 // Emit the abbreviation as a record.
494 EncodeAbbrev(*Abbv);
495 CurAbbrevs.push_back(std::move(Abbv));
496 return static_cast<unsigned>(CurAbbrevs.size())-1 +
497 bitc::FIRST_APPLICATION_ABBREV;
498 }
499
500 //===--------------------------------------------------------------------===//
501 // BlockInfo Block Emission
502 //===--------------------------------------------------------------------===//
503
504 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
505 void EnterBlockInfoBlock() {
506 EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, 2);
507 BlockInfoCurBID = ~0U;
508 BlockInfoRecords.clear();
509 }
510private:
511 /// SwitchToBlockID - If we aren't already talking about the specified block
512 /// ID, emit a BLOCKINFO_CODE_SETBID record.
513 void SwitchToBlockID(unsigned BlockID) {
514 if (BlockInfoCurBID == BlockID) return;
515 SmallVector<unsigned, 2> V;
516 V.push_back(BlockID);
517 EmitRecord(bitc::BLOCKINFO_CODE_SETBID, V);
518 BlockInfoCurBID = BlockID;
519 }
520
521 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
522 if (BlockInfo *BI = getBlockInfo(BlockID))
523 return *BI;
524
525 // Otherwise, add a new record.
526 BlockInfoRecords.emplace_back();
527 BlockInfoRecords.back().BlockID = BlockID;
528 return BlockInfoRecords.back();
529 }
530
531public:
532
533 /// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified
534 /// BlockID.
535 unsigned EmitBlockInfoAbbrev(unsigned BlockID, std::shared_ptr<BitCodeAbbrev> Abbv) {
536 SwitchToBlockID(BlockID);
537 EncodeAbbrev(*Abbv);
538
539 // Add the abbrev to the specified block record.
540 BlockInfo &Info = getOrCreateBlockInfo(BlockID);
541 Info.Abbrevs.push_back(std::move(Abbv));
542
543 return Info.Abbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV;
544 }
545};
546
547
548} // End llvm namespace
549
550#endif