1//===- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Bitcode writer implementation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Bitcode/BitcodeWriter.h"
14#include "ValueEnumerator.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/Triple.h"
27#include "llvm/Bitcode/BitcodeCommon.h"
28#include "llvm/Bitcode/BitcodeReader.h"
29#include "llvm/Bitcode/LLVMBitCodes.h"
30#include "llvm/Bitstream/BitCodes.h"
31#include "llvm/Bitstream/BitstreamWriter.h"
32#include "llvm/Config/llvm-config.h"
33#include "llvm/IR/Attributes.h"
34#include "llvm/IR/BasicBlock.h"
35#include "llvm/IR/Comdat.h"
36#include "llvm/IR/Constant.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/DebugInfoMetadata.h"
39#include "llvm/IR/DebugLoc.h"
40#include "llvm/IR/DerivedTypes.h"
41#include "llvm/IR/Function.h"
42#include "llvm/IR/GlobalAlias.h"
43#include "llvm/IR/GlobalIFunc.h"
44#include "llvm/IR/GlobalObject.h"
45#include "llvm/IR/GlobalValue.h"
46#include "llvm/IR/GlobalVariable.h"
47#include "llvm/IR/InlineAsm.h"
48#include "llvm/IR/InstrTypes.h"
49#include "llvm/IR/Instruction.h"
50#include "llvm/IR/Instructions.h"
51#include "llvm/IR/LLVMContext.h"
52#include "llvm/IR/Metadata.h"
53#include "llvm/IR/Module.h"
54#include "llvm/IR/ModuleSummaryIndex.h"
55#include "llvm/IR/Operator.h"
56#include "llvm/IR/Type.h"
57#include "llvm/IR/UseListOrder.h"
58#include "llvm/IR/Value.h"
59#include "llvm/IR/ValueSymbolTable.h"
60#include "llvm/MC/StringTableBuilder.h"
61#include "llvm/Object/IRSymtab.h"
62#include "llvm/Support/AtomicOrdering.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/CommandLine.h"
65#include "llvm/Support/Endian.h"
66#include "llvm/Support/Error.h"
67#include "llvm/Support/ErrorHandling.h"
68#include "llvm/Support/MathExtras.h"
69#include "llvm/Support/SHA1.h"
70#include "llvm/Support/TargetRegistry.h"
71#include "llvm/Support/raw_ostream.h"
72#include <algorithm>
73#include <cassert>
74#include <cstddef>
75#include <cstdint>
76#include <iterator>
77#include <map>
78#include <memory>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84
85static cl::opt<unsigned>
86 IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25),
87 cl::desc("Number of metadatas above which we emit an index "
88 "to enable lazy-loading"));
89static cl::opt<uint32_t> FlushThreshold(
90 "bitcode-flush-threshold", cl::Hidden, cl::init(512),
91 cl::desc("The threshold (unit M) for flushing LLVM bitcode."));
92
93static cl::opt<bool> WriteRelBFToSummary(
94 "write-relbf-to-summary", cl::Hidden, cl::init(false),
95 cl::desc("Write relative block frequency to function summary "));
96
97extern FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold;
98
99namespace {
100
101/// These are manifest constants used by the bitcode writer. They do not need to
102/// be kept in sync with the reader, but need to be consistent within this file.
103enum {
104 // VALUE_SYMTAB_BLOCK abbrev id's.
105 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
106 VST_ENTRY_7_ABBREV,
107 VST_ENTRY_6_ABBREV,
108 VST_BBENTRY_6_ABBREV,
109
110 // CONSTANTS_BLOCK abbrev id's.
111 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
112 CONSTANTS_INTEGER_ABBREV,
113 CONSTANTS_CE_CAST_Abbrev,
114 CONSTANTS_NULL_Abbrev,
115
116 // FUNCTION_BLOCK abbrev id's.
117 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
118 FUNCTION_INST_UNOP_ABBREV,
119 FUNCTION_INST_UNOP_FLAGS_ABBREV,
120 FUNCTION_INST_BINOP_ABBREV,
121 FUNCTION_INST_BINOP_FLAGS_ABBREV,
122 FUNCTION_INST_CAST_ABBREV,
123 FUNCTION_INST_RET_VOID_ABBREV,
124 FUNCTION_INST_RET_VAL_ABBREV,
125 FUNCTION_INST_UNREACHABLE_ABBREV,
126 FUNCTION_INST_GEP_ABBREV,
127};
128
129/// Abstract class to manage the bitcode writing, subclassed for each bitcode
130/// file type.
131class BitcodeWriterBase {
132protected:
133 /// The stream created and owned by the client.
134 BitstreamWriter &Stream;
135
136 StringTableBuilder &StrtabBuilder;
137
138public:
139 /// Constructs a BitcodeWriterBase object that writes to the provided
140 /// \p Stream.
141 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
142 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
143
144protected:
145 void writeBitcodeHeader();
146 void writeModuleVersion();
147};
148
149void BitcodeWriterBase::writeModuleVersion() {
150 // VERSION: [version#]
151 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2});
152}
153
154/// Base class to manage the module bitcode writing, currently subclassed for
155/// ModuleBitcodeWriter and ThinLinkBitcodeWriter.
156class ModuleBitcodeWriterBase : public BitcodeWriterBase {
157protected:
158 /// The Module to write to bitcode.
159 const Module &M;
160
161 /// Enumerates ids for all values in the module.
162 ValueEnumerator VE;
163
164 /// Optional per-module index to write for ThinLTO.
165 const ModuleSummaryIndex *Index;
166
167 /// Map that holds the correspondence between GUIDs in the summary index,
168 /// that came from indirect call profiles, and a value id generated by this
169 /// class to use in the VST and summary block records.
170 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
171
172 /// Tracks the last value id recorded in the GUIDToValueMap.
173 unsigned GlobalValueId;
174
175 /// Saves the offset of the VSTOffset record that must eventually be
176 /// backpatched with the offset of the actual VST.
177 uint64_t VSTOffsetPlaceholder = 0;
178
179public:
180 /// Constructs a ModuleBitcodeWriterBase object for the given Module,
181 /// writing to the provided \p Buffer.
182 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
183 BitstreamWriter &Stream,
184 bool ShouldPreserveUseListOrder,
185 const ModuleSummaryIndex *Index)
186 : BitcodeWriterBase(Stream, StrtabBuilder), M(M),
187 VE(M, ShouldPreserveUseListOrder), Index(Index) {
188 // Assign ValueIds to any callee values in the index that came from
189 // indirect call profiles and were recorded as a GUID not a Value*
190 // (which would have been assigned an ID by the ValueEnumerator).
191 // The starting ValueId is just after the number of values in the
192 // ValueEnumerator, so that they can be emitted in the VST.
193 GlobalValueId = VE.getValues().size();
194 if (!Index)
195 return;
196 for (const auto &GUIDSummaryLists : *Index)
197 // Examine all summaries for this GUID.
198 for (auto &Summary : GUIDSummaryLists.second.SummaryList)
199 if (auto FS = dyn_cast<FunctionSummary>(Summary.get()))
200 // For each call in the function summary, see if the call
201 // is to a GUID (which means it is for an indirect call,
202 // otherwise we would have a Value for it). If so, synthesize
203 // a value id.
204 for (auto &CallEdge : FS->calls())
205 if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue())
206 assignValueId(CallEdge.first.getGUID());
207 }
208
209protected:
210 void writePerModuleGlobalValueSummary();
211
212private:
213 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
214 GlobalValueSummary *Summary,
215 unsigned ValueID,
216 unsigned FSCallsAbbrev,
217 unsigned FSCallsProfileAbbrev,
218 const Function &F);
219 void writeModuleLevelReferences(const GlobalVariable &V,
220 SmallVector<uint64_t, 64> &NameVals,
221 unsigned FSModRefsAbbrev,
222 unsigned FSModVTableRefsAbbrev);
223
224 void assignValueId(GlobalValue::GUID ValGUID) {
225 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
226 }
227
228 unsigned getValueId(GlobalValue::GUID ValGUID) {
229 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
230 // Expect that any GUID value had a value Id assigned by an
231 // earlier call to assignValueId.
232 assert(VMI != GUIDToValueIdMap.end() &&
233 "GUID does not have assigned value Id");
234 return VMI->second;
235 }
236
237 // Helper to get the valueId for the type of value recorded in VI.
238 unsigned getValueId(ValueInfo VI) {
239 if (!VI.haveGVs() || !VI.getValue())
240 return getValueId(VI.getGUID());
241 return VE.getValueID(VI.getValue());
242 }
243
244 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
245};
246
247/// Class to manage the bitcode writing for a module.
248class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
249 /// Pointer to the buffer allocated by caller for bitcode writing.
250 const SmallVectorImpl<char> &Buffer;
251
252 /// True if a module hash record should be written.
253 bool GenerateHash;
254
255 /// If non-null, when GenerateHash is true, the resulting hash is written
256 /// into ModHash.
257 ModuleHash *ModHash;
258
259 SHA1 Hasher;
260
261 /// The start bit of the identification block.
262 uint64_t BitcodeStartBit;
263
264public:
265 /// Constructs a ModuleBitcodeWriter object for the given Module,
266 /// writing to the provided \p Buffer.
267 ModuleBitcodeWriter(const Module &M, SmallVectorImpl<char> &Buffer,
268 StringTableBuilder &StrtabBuilder,
269 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
270 const ModuleSummaryIndex *Index, bool GenerateHash,
271 ModuleHash *ModHash = nullptr)
272 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
273 ShouldPreserveUseListOrder, Index),
274 Buffer(Buffer), GenerateHash(GenerateHash), ModHash(ModHash),
275 BitcodeStartBit(Stream.GetCurrentBitNo()) {}
276
277 /// Emit the current module to the bitstream.
278 void write();
279
280private:
281 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
282
283 size_t addToStrtab(StringRef Str);
284
285 void writeAttributeGroupTable();
286 void writeAttributeTable();
287 void writeTypeTable();
288 void writeComdats();
289 void writeValueSymbolTableForwardDecl();
290 void writeModuleInfo();
291 void writeValueAsMetadata(const ValueAsMetadata *MD,
292 SmallVectorImpl<uint64_t> &Record);
293 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
294 unsigned Abbrev);
295 unsigned createDILocationAbbrev();
296 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
297 unsigned &Abbrev);
298 unsigned createGenericDINodeAbbrev();
299 void writeGenericDINode(const GenericDINode *N,
300 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
301 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
302 unsigned Abbrev);
303 void writeDIGenericSubrange(const DIGenericSubrange *N,
304 SmallVectorImpl<uint64_t> &Record,
305 unsigned Abbrev);
306 void writeDIEnumerator(const DIEnumerator *N,
307 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
308 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
309 unsigned Abbrev);
310 void writeDIStringType(const DIStringType *N,
311 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
312 void writeDIDerivedType(const DIDerivedType *N,
313 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
314 void writeDICompositeType(const DICompositeType *N,
315 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
316 void writeDISubroutineType(const DISubroutineType *N,
317 SmallVectorImpl<uint64_t> &Record,
318 unsigned Abbrev);
319 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
320 unsigned Abbrev);
321 void writeDICompileUnit(const DICompileUnit *N,
322 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
323 void writeDISubprogram(const DISubprogram *N,
324 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
325 void writeDILexicalBlock(const DILexicalBlock *N,
326 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
327 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
328 SmallVectorImpl<uint64_t> &Record,
329 unsigned Abbrev);
330 void writeDICommonBlock(const DICommonBlock *N,
331 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
332 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
333 unsigned Abbrev);
334 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
335 unsigned Abbrev);
336 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
337 unsigned Abbrev);
338 void writeDIArgList(const DIArgList *N, SmallVectorImpl<uint64_t> &Record,
339 unsigned Abbrev);
340 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
341 unsigned Abbrev);
342 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
343 SmallVectorImpl<uint64_t> &Record,
344 unsigned Abbrev);
345 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
346 SmallVectorImpl<uint64_t> &Record,
347 unsigned Abbrev);
348 void writeDIGlobalVariable(const DIGlobalVariable *N,
349 SmallVectorImpl<uint64_t> &Record,
350 unsigned Abbrev);
351 void writeDILocalVariable(const DILocalVariable *N,
352 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
353 void writeDILabel(const DILabel *N,
354 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
355 void writeDIExpression(const DIExpression *N,
356 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
357 void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N,
358 SmallVectorImpl<uint64_t> &Record,
359 unsigned Abbrev);
360 void writeDIObjCProperty(const DIObjCProperty *N,
361 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
362 void writeDIImportedEntity(const DIImportedEntity *N,
363 SmallVectorImpl<uint64_t> &Record,
364 unsigned Abbrev);
365 unsigned createNamedMetadataAbbrev();
366 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
367 unsigned createMetadataStringsAbbrev();
368 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
369 SmallVectorImpl<uint64_t> &Record);
370 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
371 SmallVectorImpl<uint64_t> &Record,
372 std::vector<unsigned> *MDAbbrevs = nullptr,
373 std::vector<uint64_t> *IndexPos = nullptr);
374 void writeModuleMetadata();
375 void writeFunctionMetadata(const Function &F);
376 void writeFunctionMetadataAttachment(const Function &F);
377 void writeGlobalVariableMetadataAttachment(const GlobalVariable &GV);
378 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
379 const GlobalObject &GO);
380 void writeModuleMetadataKinds();
381 void writeOperandBundleTags();
382 void writeSyncScopeNames();
383 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
384 void writeModuleConstants();
385 bool pushValueAndType(const Value *V, unsigned InstID,
386 SmallVectorImpl<unsigned> &Vals);
387 void writeOperandBundles(const CallBase &CB, unsigned InstID);
388 void pushValue(const Value *V, unsigned InstID,
389 SmallVectorImpl<unsigned> &Vals);
390 void pushValueSigned(const Value *V, unsigned InstID,
391 SmallVectorImpl<uint64_t> &Vals);
392 void writeInstruction(const Instruction &I, unsigned InstID,
393 SmallVectorImpl<unsigned> &Vals);
394 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
395 void writeGlobalValueSymbolTable(
396 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
397 void writeUseList(UseListOrder &&Order);
398 void writeUseListBlock(const Function *F);
399 void
400 writeFunction(const Function &F,
401 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
402 void writeBlockInfo();
403 void writeModuleHash(size_t BlockStartPos);
404
405 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) {
406 return unsigned(SSID);
407 }
408
409 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
410};
411
412/// Class to manage the bitcode writing for a combined index.
413class IndexBitcodeWriter : public BitcodeWriterBase {
414 /// The combined index to write to bitcode.
415 const ModuleSummaryIndex &Index;
416
417 /// When writing a subset of the index for distributed backends, client
418 /// provides a map of modules to the corresponding GUIDs/summaries to write.
419 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
420
421 /// Map that holds the correspondence between the GUID used in the combined
422 /// index and a value id generated by this class to use in references.
423 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
424
425 /// Tracks the last value id recorded in the GUIDToValueMap.
426 unsigned GlobalValueId = 0;
427
428public:
429 /// Constructs a IndexBitcodeWriter object for the given combined index,
430 /// writing to the provided \p Buffer. When writing a subset of the index
431 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
432 IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
433 const ModuleSummaryIndex &Index,
434 const std::map<std::string, GVSummaryMapTy>
435 *ModuleToSummariesForIndex = nullptr)
436 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
437 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
438 // Assign unique value ids to all summaries to be written, for use
439 // in writing out the call graph edges. Save the mapping from GUID
440 // to the new global value id to use when writing those edges, which
441 // are currently saved in the index in terms of GUID.
442 forEachSummary([&](GVInfo I, bool) {
443 GUIDToValueIdMap[I.first] = ++GlobalValueId;
444 });
445 }
446
447 /// The below iterator returns the GUID and associated summary.
448 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
449
450 /// Calls the callback for each value GUID and summary to be written to
451 /// bitcode. This hides the details of whether they are being pulled from the
452 /// entire index or just those in a provided ModuleToSummariesForIndex map.
453 template<typename Functor>
454 void forEachSummary(Functor Callback) {
455 if (ModuleToSummariesForIndex) {
456 for (auto &M : *ModuleToSummariesForIndex)
457 for (auto &Summary : M.second) {
458 Callback(Summary, false);
459 // Ensure aliasee is handled, e.g. for assigning a valueId,
460 // even if we are not importing the aliasee directly (the
461 // imported alias will contain a copy of aliasee).
462 if (auto *AS = dyn_cast<AliasSummary>(Summary.getSecond()))
463 Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true);
464 }
465 } else {
466 for (auto &Summaries : Index)
467 for (auto &Summary : Summaries.second.SummaryList)
468 Callback({Summaries.first, Summary.get()}, false);
469 }
470 }
471
472 /// Calls the callback for each entry in the modulePaths StringMap that
473 /// should be written to the module path string table. This hides the details
474 /// of whether they are being pulled from the entire index or just those in a
475 /// provided ModuleToSummariesForIndex map.
476 template <typename Functor> void forEachModule(Functor Callback) {
477 if (ModuleToSummariesForIndex) {
478 for (const auto &M : *ModuleToSummariesForIndex) {
479 const auto &MPI = Index.modulePaths().find(M.first);
480 if (MPI == Index.modulePaths().end()) {
481 // This should only happen if the bitcode file was empty, in which
482 // case we shouldn't be importing (the ModuleToSummariesForIndex
483 // would only include the module we are writing and index for).
484 assert(ModuleToSummariesForIndex->size() == 1);
485 continue;
486 }
487 Callback(*MPI);
488 }
489 } else {
490 for (const auto &MPSE : Index.modulePaths())
491 Callback(MPSE);
492 }
493 }
494
495 /// Main entry point for writing a combined index to bitcode.
496 void write();
497
498private:
499 void writeModStrings();
500 void writeCombinedGlobalValueSummary();
501
502 Optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
503 auto VMI = GUIDToValueIdMap.find(ValGUID);
504 if (VMI == GUIDToValueIdMap.end())
505 return None;
506 return VMI->second;
507 }
508
509 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
510};
511
512} // end anonymous namespace
513
514static unsigned getEncodedCastOpcode(unsigned Opcode) {
515 switch (Opcode) {
516 default: llvm_unreachable("Unknown cast instruction!");
517 case Instruction::Trunc : return bitc::CAST_TRUNC;
518 case Instruction::ZExt : return bitc::CAST_ZEXT;
519 case Instruction::SExt : return bitc::CAST_SEXT;
520 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
521 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
522 case Instruction::UIToFP : return bitc::CAST_UITOFP;
523 case Instruction::SIToFP : return bitc::CAST_SITOFP;
524 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
525 case Instruction::FPExt : return bitc::CAST_FPEXT;
526 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
527 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
528 case Instruction::BitCast : return bitc::CAST_BITCAST;
529 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
530 }
531}
532
533static unsigned getEncodedUnaryOpcode(unsigned Opcode) {
534 switch (Opcode) {
535 default: llvm_unreachable("Unknown binary instruction!");
536 case Instruction::FNeg: return bitc::UNOP_FNEG;
537 }
538}
539
540static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
541 switch (Opcode) {
542 default: llvm_unreachable("Unknown binary instruction!");
543 case Instruction::Add:
544 case Instruction::FAdd: return bitc::BINOP_ADD;
545 case Instruction::Sub:
546 case Instruction::FSub: return bitc::BINOP_SUB;
547 case Instruction::Mul:
548 case Instruction::FMul: return bitc::BINOP_MUL;
549 case Instruction::UDiv: return bitc::BINOP_UDIV;
550 case Instruction::FDiv:
551 case Instruction::SDiv: return bitc::BINOP_SDIV;
552 case Instruction::URem: return bitc::BINOP_UREM;
553 case Instruction::FRem:
554 case Instruction::SRem: return bitc::BINOP_SREM;
555 case Instruction::Shl: return bitc::BINOP_SHL;
556 case Instruction::LShr: return bitc::BINOP_LSHR;
557 case Instruction::AShr: return bitc::BINOP_ASHR;
558 case Instruction::And: return bitc::BINOP_AND;
559 case Instruction::Or: return bitc::BINOP_OR;
560 case Instruction::Xor: return bitc::BINOP_XOR;
561 }
562}
563
564static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
565 switch (Op) {
566 default: llvm_unreachable("Unknown RMW operation!");
567 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
568 case AtomicRMWInst::Add: return bitc::RMW_ADD;
569 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
570 case AtomicRMWInst::And: return bitc::RMW_AND;
571 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
572 case AtomicRMWInst::Or: return bitc::RMW_OR;
573 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
574 case AtomicRMWInst::Max: return bitc::RMW_MAX;
575 case AtomicRMWInst::Min: return bitc::RMW_MIN;
576 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
577 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
578 case AtomicRMWInst::FAdd: return bitc::RMW_FADD;
579 case AtomicRMWInst::FSub: return bitc::RMW_FSUB;
580 }
581}
582
583static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
584 switch (Ordering) {
585 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
586 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
587 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
588 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
589 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
590 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
591 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
592 }
593 llvm_unreachable("Invalid ordering");
594}
595
596static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
597 StringRef Str, unsigned AbbrevToUse) {
598 SmallVector<unsigned, 64> Vals;
599
600 // Code: [strchar x N]
601 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
602 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
603 AbbrevToUse = 0;
604 Vals.push_back(Str[i]);
605 }
606
607 // Emit the finished record.
608 Stream.EmitRecord(Code, Vals, AbbrevToUse);
609}
610
611static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
612 switch (Kind) {
613 case Attribute::Alignment:
614 return bitc::ATTR_KIND_ALIGNMENT;
615 case Attribute::AllocSize:
616 return bitc::ATTR_KIND_ALLOC_SIZE;
617 case Attribute::AlwaysInline:
618 return bitc::ATTR_KIND_ALWAYS_INLINE;
619 case Attribute::ArgMemOnly:
620 return bitc::ATTR_KIND_ARGMEMONLY;
621 case Attribute::Builtin:
622 return bitc::ATTR_KIND_BUILTIN;
623 case Attribute::ByVal:
624 return bitc::ATTR_KIND_BY_VAL;
625 case Attribute::Convergent:
626 return bitc::ATTR_KIND_CONVERGENT;
627 case Attribute::InAlloca:
628 return bitc::ATTR_KIND_IN_ALLOCA;
629 case Attribute::Cold:
630 return bitc::ATTR_KIND_COLD;
631 case Attribute::Hot:
632 return bitc::ATTR_KIND_HOT;
633 case Attribute::InaccessibleMemOnly:
634 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
635 case Attribute::InaccessibleMemOrArgMemOnly:
636 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
637 case Attribute::InlineHint:
638 return bitc::ATTR_KIND_INLINE_HINT;
639 case Attribute::InReg:
640 return bitc::ATTR_KIND_IN_REG;
641 case Attribute::JumpTable:
642 return bitc::ATTR_KIND_JUMP_TABLE;
643 case Attribute::MinSize:
644 return bitc::ATTR_KIND_MIN_SIZE;
645 case Attribute::Naked:
646 return bitc::ATTR_KIND_NAKED;
647 case Attribute::Nest:
648 return bitc::ATTR_KIND_NEST;
649 case Attribute::NoAlias:
650 return bitc::ATTR_KIND_NO_ALIAS;
651 case Attribute::NoBuiltin:
652 return bitc::ATTR_KIND_NO_BUILTIN;
653 case Attribute::NoCallback:
654 return bitc::ATTR_KIND_NO_CALLBACK;
655 case Attribute::NoCapture:
656 return bitc::ATTR_KIND_NO_CAPTURE;
657 case Attribute::NoDuplicate:
658 return bitc::ATTR_KIND_NO_DUPLICATE;
659 case Attribute::NoFree:
660 return bitc::ATTR_KIND_NOFREE;
661 case Attribute::NoImplicitFloat:
662 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
663 case Attribute::NoInline:
664 return bitc::ATTR_KIND_NO_INLINE;
665 case Attribute::NoRecurse:
666 return bitc::ATTR_KIND_NO_RECURSE;
667 case Attribute::NoMerge:
668 return bitc::ATTR_KIND_NO_MERGE;
669 case Attribute::NonLazyBind:
670 return bitc::ATTR_KIND_NON_LAZY_BIND;
671 case Attribute::NonNull:
672 return bitc::ATTR_KIND_NON_NULL;
673 case Attribute::Dereferenceable:
674 return bitc::ATTR_KIND_DEREFERENCEABLE;
675 case Attribute::DereferenceableOrNull:
676 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
677 case Attribute::NoRedZone:
678 return bitc::ATTR_KIND_NO_RED_ZONE;
679 case Attribute::NoReturn:
680 return bitc::ATTR_KIND_NO_RETURN;
681 case Attribute::NoSync:
682 return bitc::ATTR_KIND_NOSYNC;
683 case Attribute::NoCfCheck:
684 return bitc::ATTR_KIND_NOCF_CHECK;
685 case Attribute::NoProfile:
686 return bitc::ATTR_KIND_NO_PROFILE;
687 case Attribute::NoUnwind:
688 return bitc::ATTR_KIND_NO_UNWIND;
689 case Attribute::NullPointerIsValid:
690 return bitc::ATTR_KIND_NULL_POINTER_IS_VALID;
691 case Attribute::OptForFuzzing:
692 return bitc::ATTR_KIND_OPT_FOR_FUZZING;
693 case Attribute::OptimizeForSize:
694 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
695 case Attribute::OptimizeNone:
696 return bitc::ATTR_KIND_OPTIMIZE_NONE;
697 case Attribute::ReadNone:
698 return bitc::ATTR_KIND_READ_NONE;
699 case Attribute::ReadOnly:
700 return bitc::ATTR_KIND_READ_ONLY;
701 case Attribute::Returned:
702 return bitc::ATTR_KIND_RETURNED;
703 case Attribute::ReturnsTwice:
704 return bitc::ATTR_KIND_RETURNS_TWICE;
705 case Attribute::SExt:
706 return bitc::ATTR_KIND_S_EXT;
707 case Attribute::Speculatable:
708 return bitc::ATTR_KIND_SPECULATABLE;
709 case Attribute::StackAlignment:
710 return bitc::ATTR_KIND_STACK_ALIGNMENT;
711 case Attribute::StackProtect:
712 return bitc::ATTR_KIND_STACK_PROTECT;
713 case Attribute::StackProtectReq:
714 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
715 case Attribute::StackProtectStrong:
716 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
717 case Attribute::SafeStack:
718 return bitc::ATTR_KIND_SAFESTACK;
719 case Attribute::ShadowCallStack:
720 return bitc::ATTR_KIND_SHADOWCALLSTACK;
721 case Attribute::StrictFP:
722 return bitc::ATTR_KIND_STRICT_FP;
723 case Attribute::StructRet:
724 return bitc::ATTR_KIND_STRUCT_RET;
725 case Attribute::SanitizeAddress:
726 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
727 case Attribute::SanitizeHWAddress:
728 return bitc::ATTR_KIND_SANITIZE_HWADDRESS;
729 case Attribute::SanitizeThread:
730 return bitc::ATTR_KIND_SANITIZE_THREAD;
731 case Attribute::SanitizeMemory:
732 return bitc::ATTR_KIND_SANITIZE_MEMORY;
733 case Attribute::SpeculativeLoadHardening:
734 return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING;
735 case Attribute::SwiftError:
736 return bitc::ATTR_KIND_SWIFT_ERROR;
737 case Attribute::SwiftSelf:
738 return bitc::ATTR_KIND_SWIFT_SELF;
739 case Attribute::UWTable:
740 return bitc::ATTR_KIND_UW_TABLE;
741 case Attribute::VScaleRange:
742 return bitc::ATTR_KIND_VSCALE_RANGE;
743 case Attribute::WillReturn:
744 return bitc::ATTR_KIND_WILLRETURN;
745 case Attribute::WriteOnly:
746 return bitc::ATTR_KIND_WRITEONLY;
747 case Attribute::ZExt:
748 return bitc::ATTR_KIND_Z_EXT;
749 case Attribute::ImmArg:
750 return bitc::ATTR_KIND_IMMARG;
751 case Attribute::SanitizeMemTag:
752 return bitc::ATTR_KIND_SANITIZE_MEMTAG;
753 case Attribute::Preallocated:
754 return bitc::ATTR_KIND_PREALLOCATED;
755 case Attribute::NoUndef:
756 return bitc::ATTR_KIND_NOUNDEF;
757 case Attribute::ByRef:
758 return bitc::ATTR_KIND_BYREF;
759 case Attribute::MustProgress:
760 return bitc::ATTR_KIND_MUSTPROGRESS;
761 case Attribute::EndAttrKinds:
762 llvm_unreachable("Can not encode end-attribute kinds marker.");
763 case Attribute::None:
764 llvm_unreachable("Can not encode none-attribute.");
765 case Attribute::EmptyKey:
766 case Attribute::TombstoneKey:
767 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
768 }
769
770 llvm_unreachable("Trying to encode unknown attribute");
771}
772
773void ModuleBitcodeWriter::writeAttributeGroupTable() {
774 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
775 VE.getAttributeGroups();
776 if (AttrGrps.empty()) return;
777
778 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
779
780 SmallVector<uint64_t, 64> Record;
781 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
782 unsigned AttrListIndex = Pair.first;
783 AttributeSet AS = Pair.second;
784 Record.push_back(VE.getAttributeGroupID(Pair));
785 Record.push_back(AttrListIndex);
786
787 for (Attribute Attr : AS) {
788 if (Attr.isEnumAttribute()) {
789 Record.push_back(0);
790 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
791 } else if (Attr.isIntAttribute()) {
792 Record.push_back(1);
793 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
794 Record.push_back(Attr.getValueAsInt());
795 } else if (Attr.isStringAttribute()) {
796 StringRef Kind = Attr.getKindAsString();
797 StringRef Val = Attr.getValueAsString();
798
799 Record.push_back(Val.empty() ? 3 : 4);
800 Record.append(Kind.begin(), Kind.end());
801 Record.push_back(0);
802 if (!Val.empty()) {
803 Record.append(Val.begin(), Val.end());
804 Record.push_back(0);
805 }
806 } else {
807 assert(Attr.isTypeAttribute());
808 Type *Ty = Attr.getValueAsType();
809 Record.push_back(Ty ? 6 : 5);
810 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
811 if (Ty)
812 Record.push_back(VE.getTypeID(Attr.getValueAsType()));
813 }
814 }
815
816 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
817 Record.clear();
818 }
819
820 Stream.ExitBlock();
821}
822
823void ModuleBitcodeWriter::writeAttributeTable() {
824 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
825 if (Attrs.empty()) return;
826
827 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
828
829 SmallVector<uint64_t, 64> Record;
830 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
831 AttributeList AL = Attrs[i];
832 for (unsigned i = AL.index_begin(), e = AL.index_end(); i != e; ++i) {
833 AttributeSet AS = AL.getAttributes(i);
834 if (AS.hasAttributes())
835 Record.push_back(VE.getAttributeGroupID({i, AS}));
836 }
837
838 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
839 Record.clear();
840 }
841
842 Stream.ExitBlock();
843}
844
845/// WriteTypeTable - Write out the type table for a module.
846void ModuleBitcodeWriter::writeTypeTable() {
847 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
848
849 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
850 SmallVector<uint64_t, 64> TypeVals;
851
852 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
853
854 // Abbrev for TYPE_CODE_POINTER.
855 auto Abbv = std::make_shared<BitCodeAbbrev>();
856 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
857 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
858 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
859 unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
860
861 // Abbrev for TYPE_CODE_FUNCTION.
862 Abbv = std::make_shared<BitCodeAbbrev>();
863 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
864 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
865 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
866 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
867 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
868
869 // Abbrev for TYPE_CODE_STRUCT_ANON.
870 Abbv = std::make_shared<BitCodeAbbrev>();
871 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
872 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
873 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
874 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
875 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
876
877 // Abbrev for TYPE_CODE_STRUCT_NAME.
878 Abbv = std::make_shared<BitCodeAbbrev>();
879 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
880 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
881 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
882 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
883
884 // Abbrev for TYPE_CODE_STRUCT_NAMED.
885 Abbv = std::make_shared<BitCodeAbbrev>();
886 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
887 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
888 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
889 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
890 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
891
892 // Abbrev for TYPE_CODE_ARRAY.
893 Abbv = std::make_shared<BitCodeAbbrev>();
894 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
895 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
896 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
897 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
898
899 // Emit an entry count so the reader can reserve space.
900 TypeVals.push_back(TypeList.size());
901 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
902 TypeVals.clear();
903
904 // Loop over all of the types, emitting each in turn.
905 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
906 Type *T = TypeList[i];
907 int AbbrevToUse = 0;
908 unsigned Code = 0;
909
910 switch (T->getTypeID()) {
911 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
912 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
913 case Type::BFloatTyID: Code = bitc::TYPE_CODE_BFLOAT; break;
914 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
915 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
916 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
917 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
918 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
919 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
920 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
921 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
922 case Type::X86_AMXTyID: Code = bitc::TYPE_CODE_X86_AMX; break;
923 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
924 case Type::IntegerTyID:
925 // INTEGER: [width]
926 Code = bitc::TYPE_CODE_INTEGER;
927 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
928 break;
929 case Type::PointerTyID: {
930 PointerType *PTy = cast<PointerType>(T);
931 // POINTER: [pointee type, address space]
932 Code = bitc::TYPE_CODE_POINTER;
933 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
934 unsigned AddressSpace = PTy->getAddressSpace();
935 TypeVals.push_back(AddressSpace);
936 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
937 break;
938 }
939 case Type::FunctionTyID: {
940 FunctionType *FT = cast<FunctionType>(T);
941 // FUNCTION: [isvararg, retty, paramty x N]
942 Code = bitc::TYPE_CODE_FUNCTION;
943 TypeVals.push_back(FT->isVarArg());
944 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
945 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
946 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
947 AbbrevToUse = FunctionAbbrev;
948 break;
949 }
950 case Type::StructTyID: {
951 StructType *ST = cast<StructType>(T);
952 // STRUCT: [ispacked, eltty x N]
953 TypeVals.push_back(ST->isPacked());
954 // Output all of the element types.
955 for (StructType::element_iterator I = ST->element_begin(),
956 E = ST->element_end(); I != E; ++I)
957 TypeVals.push_back(VE.getTypeID(*I));
958
959 if (ST->isLiteral()) {
960 Code = bitc::TYPE_CODE_STRUCT_ANON;
961 AbbrevToUse = StructAnonAbbrev;
962 } else {
963 if (ST->isOpaque()) {
964 Code = bitc::TYPE_CODE_OPAQUE;
965 } else {
966 Code = bitc::TYPE_CODE_STRUCT_NAMED;
967 AbbrevToUse = StructNamedAbbrev;
968 }
969
970 // Emit the name if it is present.
971 if (!ST->getName().empty())
972 writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
973 StructNameAbbrev);
974 }
975 break;
976 }
977 case Type::ArrayTyID: {
978 ArrayType *AT = cast<ArrayType>(T);
979 // ARRAY: [numelts, eltty]
980 Code = bitc::TYPE_CODE_ARRAY;
981 TypeVals.push_back(AT->getNumElements());
982 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
983 AbbrevToUse = ArrayAbbrev;
984 break;
985 }
986 case Type::FixedVectorTyID:
987 case Type::ScalableVectorTyID: {
988 VectorType *VT = cast<VectorType>(T);
989 // VECTOR [numelts, eltty] or
990 // [numelts, eltty, scalable]
991 Code = bitc::TYPE_CODE_VECTOR;
992 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
993 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
994 if (isa<ScalableVectorType>(VT))
995 TypeVals.push_back(true);
996 break;
997 }
998 }
999
1000 // Emit the finished record.
1001 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1002 TypeVals.clear();
1003 }
1004
1005 Stream.ExitBlock();
1006}
1007
1008static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
1009 switch (Linkage) {
1010 case GlobalValue::ExternalLinkage:
1011 return 0;
1012 case GlobalValue::WeakAnyLinkage:
1013 return 16;
1014 case GlobalValue::AppendingLinkage:
1015 return 2;
1016 case GlobalValue::InternalLinkage:
1017 return 3;
1018 case GlobalValue::LinkOnceAnyLinkage:
1019 return 18;
1020 case GlobalValue::ExternalWeakLinkage:
1021 return 7;
1022 case GlobalValue::CommonLinkage:
1023 return 8;
1024 case GlobalValue::PrivateLinkage:
1025 return 9;
1026 case GlobalValue::WeakODRLinkage:
1027 return 17;
1028 case GlobalValue::LinkOnceODRLinkage:
1029 return 19;
1030 case GlobalValue::AvailableExternallyLinkage:
1031 return 12;
1032 }
1033 llvm_unreachable("Invalid linkage");
1034}
1035
1036static unsigned getEncodedLinkage(const GlobalValue &GV) {
1037 return getEncodedLinkage(GV.getLinkage());
1038}
1039
1040static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) {
1041 uint64_t RawFlags = 0;
1042 RawFlags |= Flags.ReadNone;
1043 RawFlags |= (Flags.ReadOnly << 1);
1044 RawFlags |= (Flags.NoRecurse << 2);
1045 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1046 RawFlags |= (Flags.NoInline << 4);
1047 RawFlags |= (Flags.AlwaysInline << 5);
1048 return RawFlags;
1049}
1050
1051// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
1052// in BitcodeReader.cpp.
1053static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
1054 uint64_t RawFlags = 0;
1055
1056 RawFlags |= Flags.NotEligibleToImport; // bool
1057 RawFlags |= (Flags.Live << 1);
1058 RawFlags |= (Flags.DSOLocal << 2);
1059 RawFlags |= (Flags.CanAutoHide << 3);
1060
1061 // Linkage don't need to be remapped at that time for the summary. Any future
1062 // change to the getEncodedLinkage() function will need to be taken into
1063 // account here as well.
1064 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
1065
1066 RawFlags |= (Flags.Visibility << 8); // 2 bits
1067
1068 return RawFlags;
1069}
1070
1071static uint64_t getEncodedGVarFlags(GlobalVarSummary::GVarFlags Flags) {
1072 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1073 (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1074 return RawFlags;
1075}
1076
1077static unsigned getEncodedVisibility(const GlobalValue &GV) {
1078 switch (GV.getVisibility()) {
1079 case GlobalValue::DefaultVisibility: return 0;
1080 case GlobalValue::HiddenVisibility: return 1;
1081 case GlobalValue::ProtectedVisibility: return 2;
1082 }
1083 llvm_unreachable("Invalid visibility");
1084}
1085
1086static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
1087 switch (GV.getDLLStorageClass()) {
1088 case GlobalValue::DefaultStorageClass: return 0;
1089 case GlobalValue::DLLImportStorageClass: return 1;
1090 case GlobalValue::DLLExportStorageClass: return 2;
1091 }
1092 llvm_unreachable("Invalid DLL storage class");
1093}
1094
1095static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
1096 switch (GV.getThreadLocalMode()) {
1097 case GlobalVariable::NotThreadLocal: return 0;
1098 case GlobalVariable::GeneralDynamicTLSModel: return 1;
1099 case GlobalVariable::LocalDynamicTLSModel: return 2;
1100 case GlobalVariable::InitialExecTLSModel: return 3;
1101 case GlobalVariable::LocalExecTLSModel: return 4;
1102 }
1103 llvm_unreachable("Invalid TLS model");
1104}
1105
1106static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1107 switch (C.getSelectionKind()) {
1108 case Comdat::Any:
1109 return bitc::COMDAT_SELECTION_KIND_ANY;
1110 case Comdat::ExactMatch:
1111 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
1112 case Comdat::Largest:
1113 return bitc::COMDAT_SELECTION_KIND_LARGEST;
1114 case Comdat::NoDuplicates:
1115 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
1116 case Comdat::SameSize:
1117 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
1118 }
1119 llvm_unreachable("Invalid selection kind");
1120}
1121
1122static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1123 switch (GV.getUnnamedAddr()) {
1124 case GlobalValue::UnnamedAddr::None: return 0;
1125 case GlobalValue::UnnamedAddr::Local: return 2;
1126 case GlobalValue::UnnamedAddr::Global: return 1;
1127 }
1128 llvm_unreachable("Invalid unnamed_addr");
1129}
1130
1131size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1132 if (GenerateHash)
1133 Hasher.update(Str);
1134 return StrtabBuilder.add(Str);
1135}
1136
1137void ModuleBitcodeWriter::writeComdats() {
1138 SmallVector<unsigned, 64> Vals;
1139 for (const Comdat *C : VE.getComdats()) {
1140 // COMDAT: [strtab offset, strtab size, selection_kind]
1141 Vals.push_back(addToStrtab(C->getName()));
1142 Vals.push_back(C->getName().size());
1143 Vals.push_back(getEncodedComdatSelectionKind(*C));
1144 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1145 Vals.clear();
1146 }
1147}
1148
1149/// Write a record that will eventually hold the word offset of the
1150/// module-level VST. For now the offset is 0, which will be backpatched
1151/// after the real VST is written. Saves the bit offset to backpatch.
1152void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1153 // Write a placeholder value in for the offset of the real VST,
1154 // which is written after the function blocks so that it can include
1155 // the offset of each function. The placeholder offset will be
1156 // updated when the real VST is written.
1157 auto Abbv = std::make_shared<BitCodeAbbrev>();
1158 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
1159 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1160 // hold the real VST offset. Must use fixed instead of VBR as we don't
1161 // know how many VBR chunks to reserve ahead of time.
1162 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1163 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1164
1165 // Emit the placeholder
1166 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
1167 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
1168
1169 // Compute and save the bit offset to the placeholder, which will be
1170 // patched when the real VST is written. We can simply subtract the 32-bit
1171 // fixed size from the current bit number to get the location to backpatch.
1172 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
1173}
1174
1175enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
1176
1177/// Determine the encoding to use for the given string name and length.
1178static StringEncoding getStringEncoding(StringRef Str) {
1179 bool isChar6 = true;
1180 for (char C : Str) {
1181 if (isChar6)
1182 isChar6 = BitCodeAbbrevOp::isChar6(C);
1183 if ((unsigned char)C & 128)
1184 // don't bother scanning the rest.
1185 return SE_Fixed8;
1186 }
1187 if (isChar6)
1188 return SE_Char6;
1189 return SE_Fixed7;
1190}
1191
1192/// Emit top-level description of module, including target triple, inline asm,
1193/// descriptors for global variables, and function prototype info.
1194/// Returns the bit offset to backpatch with the location of the real VST.
1195void ModuleBitcodeWriter::writeModuleInfo() {
1196 // Emit various pieces of data attached to a module.
1197 if (!M.getTargetTriple().empty())
1198 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1199 0 /*TODO*/);
1200 const std::string &DL = M.getDataLayoutStr();
1201 if (!DL.empty())
1202 writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
1203 if (!M.getModuleInlineAsm().empty())
1204 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1205 0 /*TODO*/);
1206
1207 // Emit information about sections and GC, computing how many there are. Also
1208 // compute the maximum alignment value.
1209 std::map<std::string, unsigned> SectionMap;
1210 std::map<std::string, unsigned> GCMap;
1211 MaybeAlign MaxAlignment;
1212 unsigned MaxGlobalType = 0;
1213 const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1214 if (A)
1215 MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1216 };
1217 for (const GlobalVariable &GV : M.globals()) {
1218 UpdateMaxAlignment(GV.getAlign());
1219 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1220 if (GV.hasSection()) {
1221 // Give section names unique ID's.
1222 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1223 if (!Entry) {
1224 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1225 0 /*TODO*/);
1226 Entry = SectionMap.size();
1227 }
1228 }
1229 }
1230 for (const Function &F : M) {
1231 UpdateMaxAlignment(F.getAlign());
1232 if (F.hasSection()) {
1233 // Give section names unique ID's.
1234 unsigned &Entry = SectionMap[std::string(F.getSection())];
1235 if (!Entry) {
1236 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1237 0 /*TODO*/);
1238 Entry = SectionMap.size();
1239 }
1240 }
1241 if (F.hasGC()) {
1242 // Same for GC names.
1243 unsigned &Entry = GCMap[F.getGC()];
1244 if (!Entry) {
1245 writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
1246 0 /*TODO*/);
1247 Entry = GCMap.size();
1248 }
1249 }
1250 }
1251
1252 // Emit abbrev for globals, now that we know # sections and max alignment.
1253 unsigned SimpleGVarAbbrev = 0;
1254 if (!M.global_empty()) {
1255 // Add an abbrev for common globals with no visibility or thread localness.
1256 auto Abbv = std::make_shared<BitCodeAbbrev>();
1257 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1258 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1259 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1260 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1261 Log2_32_Ceil(MaxGlobalType+1)));
1262 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1263 //| explicitType << 1
1264 //| constant
1265 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1266 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1267 if (!MaxAlignment) // Alignment.
1268 Abbv->Add(BitCodeAbbrevOp(0));
1269 else {
1270 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1271 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1272 Log2_32_Ceil(MaxEncAlignment+1)));
1273 }
1274 if (SectionMap.empty()) // Section.
1275 Abbv->Add(BitCodeAbbrevOp(0));
1276 else
1277 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1278 Log2_32_Ceil(SectionMap.size()+1)));
1279 // Don't bother emitting vis + thread local.
1280 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1281 }
1282
1283 SmallVector<unsigned, 64> Vals;
1284 // Emit the module's source file name.
1285 {
1286 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
1287 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1288 if (Bits == SE_Char6)
1289 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1290 else if (Bits == SE_Fixed7)
1291 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1292
1293 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1294 auto Abbv = std::make_shared<BitCodeAbbrev>();
1295 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1296 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1297 Abbv->Add(AbbrevOpToUse);
1298 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1299
1300 for (const auto P : M.getSourceFileName())
1301 Vals.push_back((unsigned char)P);
1302
1303 // Emit the finished record.
1304 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1305 Vals.clear();
1306 }
1307
1308 // Emit the global variable information.
1309 for (const GlobalVariable &GV : M.globals()) {
1310 unsigned AbbrevToUse = 0;
1311
1312 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
1313 // linkage, alignment, section, visibility, threadlocal,
1314 // unnamed_addr, externally_initialized, dllstorageclass,
1315 // comdat, attributes, DSO_Local]
1316 Vals.push_back(addToStrtab(GV.getName()));
1317 Vals.push_back(GV.getName().size());
1318 Vals.push_back(VE.getTypeID(GV.getValueType()));
1319 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1320 Vals.push_back(GV.isDeclaration() ? 0 :
1321 (VE.getValueID(GV.getInitializer()) + 1));
1322 Vals.push_back(getEncodedLinkage(GV));
1323 Vals.push_back(getEncodedAlign(GV.getAlign()));
1324 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1325 : 0);
1326 if (GV.isThreadLocal() ||
1327 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1328 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1329 GV.isExternallyInitialized() ||
1330 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1331 GV.hasComdat() ||
1332 GV.hasAttributes() ||
1333 GV.isDSOLocal() ||
1334 GV.hasPartition()) {
1335 Vals.push_back(getEncodedVisibility(GV));
1336 Vals.push_back(getEncodedThreadLocalMode(GV));
1337 Vals.push_back(getEncodedUnnamedAddr(GV));
1338 Vals.push_back(GV.isExternallyInitialized());
1339 Vals.push_back(getEncodedDLLStorageClass(GV));
1340 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1341
1342 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1343 Vals.push_back(VE.getAttributeListID(AL));
1344
1345 Vals.push_back(GV.isDSOLocal());
1346 Vals.push_back(addToStrtab(GV.getPartition()));
1347 Vals.push_back(GV.getPartition().size());
1348 } else {
1349 AbbrevToUse = SimpleGVarAbbrev;
1350 }
1351
1352 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1353 Vals.clear();
1354 }
1355
1356 // Emit the function proto information.
1357 for (const Function &F : M) {
1358 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1359 // linkage, paramattrs, alignment, section, visibility, gc,
1360 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1361 // prefixdata, personalityfn, DSO_Local, addrspace]
1362 Vals.push_back(addToStrtab(F.getName()));
1363 Vals.push_back(F.getName().size());
1364 Vals.push_back(VE.getTypeID(F.getFunctionType()));
1365 Vals.push_back(F.getCallingConv());
1366 Vals.push_back(F.isDeclaration());
1367 Vals.push_back(getEncodedLinkage(F));
1368 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1369 Vals.push_back(getEncodedAlign(F.getAlign()));
1370 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1371 : 0);
1372 Vals.push_back(getEncodedVisibility(F));
1373 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1374 Vals.push_back(getEncodedUnnamedAddr(F));
1375 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1376 : 0);
1377 Vals.push_back(getEncodedDLLStorageClass(F));
1378 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1379 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1380 : 0);
1381 Vals.push_back(
1382 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1383
1384 Vals.push_back(F.isDSOLocal());
1385 Vals.push_back(F.getAddressSpace());
1386 Vals.push_back(addToStrtab(F.getPartition()));
1387 Vals.push_back(F.getPartition().size());
1388
1389 unsigned AbbrevToUse = 0;
1390 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1391 Vals.clear();
1392 }
1393
1394 // Emit the alias information.
1395 for (const GlobalAlias &A : M.aliases()) {
1396 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1397 // visibility, dllstorageclass, threadlocal, unnamed_addr,
1398 // DSO_Local]
1399 Vals.push_back(addToStrtab(A.getName()));
1400 Vals.push_back(A.getName().size());
1401 Vals.push_back(VE.getTypeID(A.getValueType()));
1402 Vals.push_back(A.getType()->getAddressSpace());
1403 Vals.push_back(VE.getValueID(A.getAliasee()));
1404 Vals.push_back(getEncodedLinkage(A));
1405 Vals.push_back(getEncodedVisibility(A));
1406 Vals.push_back(getEncodedDLLStorageClass(A));
1407 Vals.push_back(getEncodedThreadLocalMode(A));
1408 Vals.push_back(getEncodedUnnamedAddr(A));
1409 Vals.push_back(A.isDSOLocal());
1410 Vals.push_back(addToStrtab(A.getPartition()));
1411 Vals.push_back(A.getPartition().size());
1412
1413 unsigned AbbrevToUse = 0;
1414 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
1415 Vals.clear();
1416 }
1417
1418 // Emit the ifunc information.
1419 for (const GlobalIFunc &I : M.ifuncs()) {
1420 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1421 // val#, linkage, visibility, DSO_Local]
1422 Vals.push_back(addToStrtab(I.getName()));
1423 Vals.push_back(I.getName().size());
1424 Vals.push_back(VE.getTypeID(I.getValueType()));
1425 Vals.push_back(I.getType()->getAddressSpace());
1426 Vals.push_back(VE.getValueID(I.getResolver()));
1427 Vals.push_back(getEncodedLinkage(I));
1428 Vals.push_back(getEncodedVisibility(I));
1429 Vals.push_back(I.isDSOLocal());
1430 Vals.push_back(addToStrtab(I.getPartition()));
1431 Vals.push_back(I.getPartition().size());
1432 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
1433 Vals.clear();
1434 }
1435
1436 writeValueSymbolTableForwardDecl();
1437}
1438
1439static uint64_t getOptimizationFlags(const Value *V) {
1440 uint64_t Flags = 0;
1441
1442 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1443 if (OBO->hasNoSignedWrap())
1444 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1445 if (OBO->hasNoUnsignedWrap())
1446 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
1447 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1448 if (PEO->isExact())
1449 Flags |= 1 << bitc::PEO_EXACT;
1450 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1451 if (FPMO->hasAllowReassoc())
1452 Flags |= bitc::AllowReassoc;
1453 if (FPMO->hasNoNaNs())
1454 Flags |= bitc::NoNaNs;
1455 if (FPMO->hasNoInfs())
1456 Flags |= bitc::NoInfs;
1457 if (FPMO->hasNoSignedZeros())
1458 Flags |= bitc::NoSignedZeros;
1459 if (FPMO->hasAllowReciprocal())
1460 Flags |= bitc::AllowReciprocal;
1461 if (FPMO->hasAllowContract())
1462 Flags |= bitc::AllowContract;
1463 if (FPMO->hasApproxFunc())
1464 Flags |= bitc::ApproxFunc;
1465 }
1466
1467 return Flags;
1468}
1469
1470void ModuleBitcodeWriter::writeValueAsMetadata(
1471 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1472 // Mimic an MDNode with a value as one operand.
1473 Value *V = MD->getValue();
1474 Record.push_back(VE.getTypeID(V->getType()));
1475 Record.push_back(VE.getValueID(V));
1476 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
1477 Record.clear();
1478}
1479
1480void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1481 SmallVectorImpl<uint64_t> &Record,
1482 unsigned Abbrev) {
1483 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1484 Metadata *MD = N->getOperand(i);
1485 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1486 "Unexpected function-local metadata");
1487 Record.push_back(VE.getMetadataOrNullID(MD));
1488 }
1489 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1490 : bitc::METADATA_NODE,
1491 Record, Abbrev);
1492 Record.clear();
1493}
1494
1495unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1496 // Assume the column is usually under 128, and always output the inlined-at
1497 // location (it's never more expensive than building an array size 1).
1498 auto Abbv = std::make_shared<BitCodeAbbrev>();
1499 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1500 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1501 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1502 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1503 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1504 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1505 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1506 return Stream.EmitAbbrev(std::move(Abbv));
1507}
1508
1509void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1510 SmallVectorImpl<uint64_t> &Record,
1511 unsigned &Abbrev) {
1512 if (!Abbrev)
1513 Abbrev = createDILocationAbbrev();
1514
1515 Record.push_back(N->isDistinct());
1516 Record.push_back(N->getLine());
1517 Record.push_back(N->getColumn());
1518 Record.push_back(VE.getMetadataID(N->getScope()));
1519 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1520 Record.push_back(N->isImplicitCode());
1521
1522 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1523 Record.clear();
1524}
1525
1526unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1527 // Assume the column is usually under 128, and always output the inlined-at
1528 // location (it's never more expensive than building an array size 1).
1529 auto Abbv = std::make_shared<BitCodeAbbrev>();
1530 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1531 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1532 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1533 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1534 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1535 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1536 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1537 return Stream.EmitAbbrev(std::move(Abbv));
1538}
1539
1540void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1541 SmallVectorImpl<uint64_t> &Record,
1542 unsigned &Abbrev) {
1543 if (!Abbrev)
1544 Abbrev = createGenericDINodeAbbrev();
1545
1546 Record.push_back(N->isDistinct());
1547 Record.push_back(N->getTag());
1548 Record.push_back(0); // Per-tag version field; unused for now.
1549
1550 for (auto &I : N->operands())
1551 Record.push_back(VE.getMetadataOrNullID(I));
1552
1553 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
1554 Record.clear();
1555}
1556
1557void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1558 SmallVectorImpl<uint64_t> &Record,
1559 unsigned Abbrev) {
1560 const uint64_t Version = 2 << 1;
1561 Record.push_back((uint64_t)N->isDistinct() | Version);
1562 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1563 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1564 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1565 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1566
1567 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1568 Record.clear();
1569}
1570
1571void ModuleBitcodeWriter::writeDIGenericSubrange(
1572 const DIGenericSubrange *N, SmallVectorImpl<uint64_t> &Record,
1573 unsigned Abbrev) {
1574 Record.push_back((uint64_t)N->isDistinct());
1575 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1576 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1577 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1578 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1579
1580 Stream.EmitRecord(bitc::METADATA_GENERIC_SUBRANGE, Record, Abbrev);
1581 Record.clear();
1582}
1583
1584static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1585 if ((int64_t)V >= 0)
1586 Vals.push_back(V << 1);
1587 else
1588 Vals.push_back((-V << 1) | 1);
1589}
1590
1591static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) {
1592 // We have an arbitrary precision integer value to write whose
1593 // bit width is > 64. However, in canonical unsigned integer
1594 // format it is likely that the high bits are going to be zero.
1595 // So, we only write the number of active words.
1596 unsigned NumWords = A.getActiveWords();
1597 const uint64_t *RawData = A.getRawData();
1598 for (unsigned i = 0; i < NumWords; i++)
1599 emitSignedInt64(Vals, RawData[i]);
1600}
1601
1602void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1603 SmallVectorImpl<uint64_t> &Record,
1604 unsigned Abbrev) {
1605 const uint64_t IsBigInt = 1 << 2;
1606 Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct());
1607 Record.push_back(N->getValue().getBitWidth());
1608 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1609 emitWideAPInt(Record, N->getValue());
1610
1611 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
1612 Record.clear();
1613}
1614
1615void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1616 SmallVectorImpl<uint64_t> &Record,
1617 unsigned Abbrev) {
1618 Record.push_back(N->isDistinct());
1619 Record.push_back(N->getTag());
1620 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1621 Record.push_back(N->getSizeInBits());
1622 Record.push_back(N->getAlignInBits());
1623 Record.push_back(N->getEncoding());
1624 Record.push_back(N->getFlags());
1625
1626 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
1627 Record.clear();
1628}
1629
1630void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N,
1631 SmallVectorImpl<uint64_t> &Record,
1632 unsigned Abbrev) {
1633 Record.push_back(N->isDistinct());
1634 Record.push_back(N->getTag());
1635 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1636 Record.push_back(VE.getMetadataOrNullID(N->getStringLength()));
1637 Record.push_back(VE.getMetadataOrNullID(N->getStringLengthExp()));
1638 Record.push_back(N->getSizeInBits());
1639 Record.push_back(N->getAlignInBits());
1640 Record.push_back(N->getEncoding());
1641
1642 Stream.EmitRecord(bitc::METADATA_STRING_TYPE, Record, Abbrev);
1643 Record.clear();
1644}
1645
1646void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1647 SmallVectorImpl<uint64_t> &Record,
1648 unsigned Abbrev) {
1649 Record.push_back(N->isDistinct());
1650 Record.push_back(N->getTag());
1651 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1652 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1653 Record.push_back(N->getLine());
1654 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1655 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1656 Record.push_back(N->getSizeInBits());
1657 Record.push_back(N->getAlignInBits());
1658 Record.push_back(N->getOffsetInBits());
1659 Record.push_back(N->getFlags());
1660 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1661
1662 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1663 // that there is no DWARF address space associated with DIDerivedType.
1664 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1665 Record.push_back(*DWARFAddressSpace + 1);
1666 else
1667 Record.push_back(0);
1668
1669 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
1670 Record.clear();
1671}
1672
1673void ModuleBitcodeWriter::writeDICompositeType(
1674 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1675 unsigned Abbrev) {
1676 const unsigned IsNotUsedInOldTypeRef = 0x2;
1677 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
1678 Record.push_back(N->getTag());
1679 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1680 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1681 Record.push_back(N->getLine());
1682 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1683 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1684 Record.push_back(N->getSizeInBits());
1685 Record.push_back(N->getAlignInBits());
1686 Record.push_back(N->getOffsetInBits());
1687 Record.push_back(N->getFlags());
1688 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1689 Record.push_back(N->getRuntimeLang());
1690 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1691 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1692 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1693 Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
1694 Record.push_back(VE.getMetadataOrNullID(N->getRawDataLocation()));
1695 Record.push_back(VE.getMetadataOrNullID(N->getRawAssociated()));
1696 Record.push_back(VE.getMetadataOrNullID(N->getRawAllocated()));
1697 Record.push_back(VE.getMetadataOrNullID(N->getRawRank()));
1698
1699 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
1700 Record.clear();
1701}
1702
1703void ModuleBitcodeWriter::writeDISubroutineType(
1704 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1705 unsigned Abbrev) {
1706 const unsigned HasNoOldTypeRefs = 0x2;
1707 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
1708 Record.push_back(N->getFlags());
1709 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1710 Record.push_back(N->getCC());
1711
1712 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
1713 Record.clear();
1714}
1715
1716void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1717 SmallVectorImpl<uint64_t> &Record,
1718 unsigned Abbrev) {
1719 Record.push_back(N->isDistinct());
1720 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1721 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1722 if (N->getRawChecksum()) {
1723 Record.push_back(N->getRawChecksum()->Kind);
1724 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
1725 } else {
1726 // Maintain backwards compatibility with the old internal representation of
1727 // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
1728 Record.push_back(0);
1729 Record.push_back(VE.getMetadataOrNullID(nullptr));
1730 }
1731 auto Source = N->getRawSource();
1732 if (Source)
1733 Record.push_back(VE.getMetadataOrNullID(*Source));
1734
1735 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1736 Record.clear();
1737}
1738
1739void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1740 SmallVectorImpl<uint64_t> &Record,
1741 unsigned Abbrev) {
1742 assert(N->isDistinct() && "Expected distinct compile units");
1743 Record.push_back(/* IsDistinct */ true);
1744 Record.push_back(N->getSourceLanguage());
1745 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1746 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1747 Record.push_back(N->isOptimized());
1748 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1749 Record.push_back(N->getRuntimeVersion());
1750 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1751 Record.push_back(N->getEmissionKind());
1752 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1753 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1754 Record.push_back(/* subprograms */ 0);
1755 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1756 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1757 Record.push_back(N->getDWOId());
1758 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
1759 Record.push_back(N->getSplitDebugInlining());
1760 Record.push_back(N->getDebugInfoForProfiling());
1761 Record.push_back((unsigned)N->getNameTableKind());
1762 Record.push_back(N->getRangesBaseAddress());
1763 Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot()));
1764 Record.push_back(VE.getMetadataOrNullID(N->getRawSDK()));
1765
1766 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1767 Record.clear();
1768}
1769
1770void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1771 SmallVectorImpl<uint64_t> &Record,
1772 unsigned Abbrev) {
1773 const uint64_t HasUnitFlag = 1 << 1;
1774 const uint64_t HasSPFlagsFlag = 1 << 2;
1775 Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
1776 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1777 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1778 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1779 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1780 Record.push_back(N->getLine());
1781 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1782 Record.push_back(N->getScopeLine());
1783 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1784 Record.push_back(N->getSPFlags());
1785 Record.push_back(N->getVirtualIndex());
1786 Record.push_back(N->getFlags());
1787 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
1788 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1789 Record.