xref: /llvm-project/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp (revision 29441e4f5fa5f5c7709f7cf180815ba97f611297)
1 //===- Bitcode/Writer/DXILBitcodeWriter.cpp - DXIL 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 "DXILBitcodeWriter.h"
14 #include "DXILValueEnumerator.h"
15 #include "DirectXIRPasses/PointerTypeAnalysis.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Bitcode/BitcodeCommon.h"
18 #include "llvm/Bitcode/BitcodeReader.h"
19 #include "llvm/Bitcode/LLVMBitCodes.h"
20 #include "llvm/Bitstream/BitCodes.h"
21 #include "llvm/Bitstream/BitstreamWriter.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Comdat.h"
25 #include "llvm/IR/Constant.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DebugInfoMetadata.h"
28 #include "llvm/IR/DebugLoc.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalIFunc.h"
33 #include "llvm/IR/GlobalObject.h"
34 #include "llvm/IR/GlobalValue.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/InlineAsm.h"
37 #include "llvm/IR/InstrTypes.h"
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/LLVMContext.h"
41 #include "llvm/IR/Metadata.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/IR/ModuleSummaryIndex.h"
44 #include "llvm/IR/Operator.h"
45 #include "llvm/IR/Type.h"
46 #include "llvm/IR/UseListOrder.h"
47 #include "llvm/IR/Value.h"
48 #include "llvm/IR/ValueSymbolTable.h"
49 #include "llvm/Object/IRSymtab.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include "llvm/Support/ModRef.h"
52 #include "llvm/Support/SHA1.h"
53 #include "llvm/TargetParser/Triple.h"
54 
55 namespace llvm {
56 namespace dxil {
57 
58 // Generates an enum to use as an index in the Abbrev array of Metadata record.
59 enum MetadataAbbrev : unsigned {
60 #define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
61 #include "llvm/IR/Metadata.def"
62   LastPlusOne
63 };
64 
65 class DXILBitcodeWriter {
66 
67   /// These are manifest constants used by the bitcode writer. They do not need
68   /// to be kept in sync with the reader, but need to be consistent within this
69   /// file.
70   enum {
71     // VALUE_SYMTAB_BLOCK abbrev id's.
72     VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
73     VST_ENTRY_7_ABBREV,
74     VST_ENTRY_6_ABBREV,
75     VST_BBENTRY_6_ABBREV,
76 
77     // CONSTANTS_BLOCK abbrev id's.
78     CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
79     CONSTANTS_INTEGER_ABBREV,
80     CONSTANTS_CE_CAST_Abbrev,
81     CONSTANTS_NULL_Abbrev,
82 
83     // FUNCTION_BLOCK abbrev id's.
84     FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
85     FUNCTION_INST_BINOP_ABBREV,
86     FUNCTION_INST_BINOP_FLAGS_ABBREV,
87     FUNCTION_INST_CAST_ABBREV,
88     FUNCTION_INST_RET_VOID_ABBREV,
89     FUNCTION_INST_RET_VAL_ABBREV,
90     FUNCTION_INST_UNREACHABLE_ABBREV,
91     FUNCTION_INST_GEP_ABBREV,
92   };
93 
94   // Cache some types
95   Type *I8Ty;
96   Type *I8PtrTy;
97 
98   /// The stream created and owned by the client.
99   BitstreamWriter &Stream;
100 
101   StringTableBuilder &StrtabBuilder;
102 
103   /// The Module to write to bitcode.
104   const Module &M;
105 
106   /// Enumerates ids for all values in the module.
107   ValueEnumerator VE;
108 
109   /// Map that holds the correspondence between GUIDs in the summary index,
110   /// that came from indirect call profiles, and a value id generated by this
111   /// class to use in the VST and summary block records.
112   std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
113 
114   /// Tracks the last value id recorded in the GUIDToValueMap.
115   unsigned GlobalValueId;
116 
117   /// Saves the offset of the VSTOffset record that must eventually be
118   /// backpatched with the offset of the actual VST.
119   uint64_t VSTOffsetPlaceholder = 0;
120 
121   /// Pointer to the buffer allocated by caller for bitcode writing.
122   const SmallVectorImpl<char> &Buffer;
123 
124   /// The start bit of the identification block.
125   uint64_t BitcodeStartBit;
126 
127   /// This maps values to their typed pointers
128   PointerTypeMap PointerMap;
129 
130 public:
131   /// Constructs a ModuleBitcodeWriter object for the given Module,
132   /// writing to the provided \p Buffer.
133   DXILBitcodeWriter(const Module &M, SmallVectorImpl<char> &Buffer,
134                     StringTableBuilder &StrtabBuilder, BitstreamWriter &Stream)
135       : I8Ty(Type::getInt8Ty(M.getContext())),
136         I8PtrTy(TypedPointerType::get(I8Ty, 0)), Stream(Stream),
137         StrtabBuilder(StrtabBuilder), M(M), VE(M, I8PtrTy), Buffer(Buffer),
138         BitcodeStartBit(Stream.GetCurrentBitNo()),
139         PointerMap(PointerTypeAnalysis::run(M)) {
140     GlobalValueId = VE.getValues().size();
141     // Enumerate the typed pointers
142     for (auto El : PointerMap)
143       VE.EnumerateType(El.second);
144   }
145 
146   /// Emit the current module to the bitstream.
147   void write();
148 
149   static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind);
150   static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
151                                 StringRef Str, unsigned AbbrevToUse);
152   static void writeIdentificationBlock(BitstreamWriter &Stream);
153   static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V);
154   static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A);
155 
156   static unsigned getEncodedComdatSelectionKind(const Comdat &C);
157   static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage);
158   static unsigned getEncodedLinkage(const GlobalValue &GV);
159   static unsigned getEncodedVisibility(const GlobalValue &GV);
160   static unsigned getEncodedThreadLocalMode(const GlobalValue &GV);
161   static unsigned getEncodedDLLStorageClass(const GlobalValue &GV);
162   static unsigned getEncodedCastOpcode(unsigned Opcode);
163   static unsigned getEncodedUnaryOpcode(unsigned Opcode);
164   static unsigned getEncodedBinaryOpcode(unsigned Opcode);
165   static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op);
166   static unsigned getEncodedOrdering(AtomicOrdering Ordering);
167   static uint64_t getOptimizationFlags(const Value *V);
168 
169 private:
170   void writeModuleVersion();
171   void writePerModuleGlobalValueSummary();
172 
173   void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
174                                            GlobalValueSummary *Summary,
175                                            unsigned ValueID,
176                                            unsigned FSCallsAbbrev,
177                                            unsigned FSCallsProfileAbbrev,
178                                            const Function &F);
179   void writeModuleLevelReferences(const GlobalVariable &V,
180                                   SmallVector<uint64_t, 64> &NameVals,
181                                   unsigned FSModRefsAbbrev,
182                                   unsigned FSModVTableRefsAbbrev);
183 
184   void assignValueId(GlobalValue::GUID ValGUID) {
185     GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
186   }
187 
188   unsigned getValueId(GlobalValue::GUID ValGUID) {
189     const auto &VMI = GUIDToValueIdMap.find(ValGUID);
190     // Expect that any GUID value had a value Id assigned by an
191     // earlier call to assignValueId.
192     assert(VMI != GUIDToValueIdMap.end() &&
193            "GUID does not have assigned value Id");
194     return VMI->second;
195   }
196 
197   // Helper to get the valueId for the type of value recorded in VI.
198   unsigned getValueId(ValueInfo VI) {
199     if (!VI.haveGVs() || !VI.getValue())
200       return getValueId(VI.getGUID());
201     return VE.getValueID(VI.getValue());
202   }
203 
204   std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
205 
206   uint64_t bitcodeStartBit() { return BitcodeStartBit; }
207 
208   size_t addToStrtab(StringRef Str);
209 
210   unsigned createDILocationAbbrev();
211   unsigned createGenericDINodeAbbrev();
212 
213   void writeAttributeGroupTable();
214   void writeAttributeTable();
215   void writeTypeTable();
216   void writeComdats();
217   void writeValueSymbolTableForwardDecl();
218   void writeModuleInfo();
219   void writeValueAsMetadata(const ValueAsMetadata *MD,
220                             SmallVectorImpl<uint64_t> &Record);
221   void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
222                     unsigned Abbrev);
223   void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
224                        unsigned &Abbrev);
225   void writeGenericDINode(const GenericDINode *N,
226                           SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev) {
227     llvm_unreachable("DXIL cannot contain GenericDI Nodes");
228   }
229   void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
230                        unsigned Abbrev);
231   void writeDIGenericSubrange(const DIGenericSubrange *N,
232                               SmallVectorImpl<uint64_t> &Record,
233                               unsigned Abbrev) {
234     llvm_unreachable("DXIL cannot contain DIGenericSubrange Nodes");
235   }
236   void writeDIEnumerator(const DIEnumerator *N,
237                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
238   void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
239                         unsigned Abbrev);
240   void writeDIStringType(const DIStringType *N,
241                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
242     llvm_unreachable("DXIL cannot contain DIStringType Nodes");
243   }
244   void writeDIDerivedType(const DIDerivedType *N,
245                           SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
246   void writeDICompositeType(const DICompositeType *N,
247                             SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
248   void writeDISubroutineType(const DISubroutineType *N,
249                              SmallVectorImpl<uint64_t> &Record,
250                              unsigned Abbrev);
251   void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
252                    unsigned Abbrev);
253   void writeDICompileUnit(const DICompileUnit *N,
254                           SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
255   void writeDISubprogram(const DISubprogram *N,
256                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
257   void writeDILexicalBlock(const DILexicalBlock *N,
258                            SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
259   void writeDILexicalBlockFile(const DILexicalBlockFile *N,
260                                SmallVectorImpl<uint64_t> &Record,
261                                unsigned Abbrev);
262   void writeDICommonBlock(const DICommonBlock *N,
263                           SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
264     llvm_unreachable("DXIL cannot contain DICommonBlock Nodes");
265   }
266   void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
267                         unsigned Abbrev);
268   void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
269                     unsigned Abbrev) {
270     llvm_unreachable("DXIL cannot contain DIMacro Nodes");
271   }
272   void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
273                         unsigned Abbrev) {
274     llvm_unreachable("DXIL cannot contain DIMacroFile Nodes");
275   }
276   void writeDIArgList(const DIArgList *N, SmallVectorImpl<uint64_t> &Record,
277                       unsigned Abbrev) {
278     llvm_unreachable("DXIL cannot contain DIArgList Nodes");
279   }
280   void writeDIAssignID(const DIAssignID *N, SmallVectorImpl<uint64_t> &Record,
281                        unsigned Abbrev) {
282     // DIAssignID is experimental feature to track variable location in IR..
283     // FIXME: translate DIAssignID to debug info DXIL supports.
284     //   See https://github.com/llvm/llvm-project/issues/58989
285     llvm_unreachable("DXIL cannot contain DIAssignID Nodes");
286   }
287   void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
288                      unsigned Abbrev);
289   void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
290                                     SmallVectorImpl<uint64_t> &Record,
291                                     unsigned Abbrev);
292   void writeDITemplateValueParameter(const DITemplateValueParameter *N,
293                                      SmallVectorImpl<uint64_t> &Record,
294                                      unsigned Abbrev);
295   void writeDIGlobalVariable(const DIGlobalVariable *N,
296                              SmallVectorImpl<uint64_t> &Record,
297                              unsigned Abbrev);
298   void writeDILocalVariable(const DILocalVariable *N,
299                             SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
300   void writeDILabel(const DILabel *N, SmallVectorImpl<uint64_t> &Record,
301                     unsigned Abbrev) {
302     llvm_unreachable("DXIL cannot contain DILabel Nodes");
303   }
304   void writeDIExpression(const DIExpression *N,
305                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
306   void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N,
307                                        SmallVectorImpl<uint64_t> &Record,
308                                        unsigned Abbrev) {
309     llvm_unreachable("DXIL cannot contain GlobalVariableExpression Nodes");
310   }
311   void writeDIObjCProperty(const DIObjCProperty *N,
312                            SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
313   void writeDIImportedEntity(const DIImportedEntity *N,
314                              SmallVectorImpl<uint64_t> &Record,
315                              unsigned Abbrev);
316   unsigned createNamedMetadataAbbrev();
317   void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
318   unsigned createMetadataStringsAbbrev();
319   void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
320                             SmallVectorImpl<uint64_t> &Record);
321   void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
322                             SmallVectorImpl<uint64_t> &Record,
323                             std::vector<unsigned> *MDAbbrevs = nullptr,
324                             std::vector<uint64_t> *IndexPos = nullptr);
325   void writeModuleMetadata();
326   void writeFunctionMetadata(const Function &F);
327   void writeFunctionMetadataAttachment(const Function &F);
328   void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
329                                     const GlobalObject &GO);
330   void writeModuleMetadataKinds();
331   void writeOperandBundleTags();
332   void writeSyncScopeNames();
333   void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
334   void writeModuleConstants();
335   bool pushValueAndType(const Value *V, unsigned InstID,
336                         SmallVectorImpl<unsigned> &Vals);
337   void writeOperandBundles(const CallBase &CB, unsigned InstID);
338   void pushValue(const Value *V, unsigned InstID,
339                  SmallVectorImpl<unsigned> &Vals);
340   void pushValueSigned(const Value *V, unsigned InstID,
341                        SmallVectorImpl<uint64_t> &Vals);
342   void writeInstruction(const Instruction &I, unsigned InstID,
343                         SmallVectorImpl<unsigned> &Vals);
344   void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
345   void writeGlobalValueSymbolTable(
346       DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
347   void writeFunction(const Function &F);
348   void writeBlockInfo();
349 
350   unsigned getEncodedSyncScopeID(SyncScope::ID SSID) { return unsigned(SSID); }
351 
352   unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
353 
354   unsigned getTypeID(Type *T, const Value *V = nullptr);
355   /// getGlobalObjectValueTypeID - returns the element type for a GlobalObject
356   ///
357   /// GlobalObject types are saved by PointerTypeAnalysis as pointers to the
358   /// GlobalObject, but in the bitcode writer we need the pointer element type.
359   unsigned getGlobalObjectValueTypeID(Type *T, const GlobalObject *G);
360 };
361 
362 } // namespace dxil
363 } // namespace llvm
364 
365 using namespace llvm;
366 using namespace llvm::dxil;
367 
368 ////////////////////////////////////////////////////////////////////////////////
369 /// Begin dxil::BitcodeWriter Implementation
370 ////////////////////////////////////////////////////////////////////////////////
371 
372 dxil::BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
373     : Buffer(Buffer), Stream(new BitstreamWriter(Buffer)) {
374   // Emit the file header.
375   Stream->Emit((unsigned)'B', 8);
376   Stream->Emit((unsigned)'C', 8);
377   Stream->Emit(0x0, 4);
378   Stream->Emit(0xC, 4);
379   Stream->Emit(0xE, 4);
380   Stream->Emit(0xD, 4);
381 }
382 
383 dxil::BitcodeWriter::~BitcodeWriter() { }
384 
385 /// Write the specified module to the specified output stream.
386 void dxil::WriteDXILToFile(const Module &M, raw_ostream &Out) {
387   SmallVector<char, 0> Buffer;
388   Buffer.reserve(256 * 1024);
389 
390   // If this is darwin or another generic macho target, reserve space for the
391   // header.
392   Triple TT(M.getTargetTriple());
393   if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
394     Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
395 
396   BitcodeWriter Writer(Buffer);
397   Writer.writeModule(M);
398 
399   // Write the generated bitstream to "Out".
400   if (!Buffer.empty())
401     Out.write((char *)&Buffer.front(), Buffer.size());
402 }
403 
404 void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
405   Stream->EnterSubblock(Block, 3);
406 
407   auto Abbv = std::make_shared<BitCodeAbbrev>();
408   Abbv->Add(BitCodeAbbrevOp(Record));
409   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
410   auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
411 
412   Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
413 
414   Stream->ExitBlock();
415 }
416 
417 void BitcodeWriter::writeModule(const Module &M) {
418 
419   // The Mods vector is used by irsymtab::build, which requires non-const
420   // Modules in case it needs to materialize metadata. But the bitcode writer
421   // requires that the module is materialized, so we can cast to non-const here,
422   // after checking that it is in fact materialized.
423   assert(M.isMaterialized());
424   Mods.push_back(const_cast<Module *>(&M));
425 
426   DXILBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream);
427   ModuleWriter.write();
428 }
429 
430 ////////////////////////////////////////////////////////////////////////////////
431 /// Begin dxil::BitcodeWriterBase Implementation
432 ////////////////////////////////////////////////////////////////////////////////
433 
434 unsigned DXILBitcodeWriter::getEncodedCastOpcode(unsigned Opcode) {
435   switch (Opcode) {
436   default:
437     llvm_unreachable("Unknown cast instruction!");
438   case Instruction::Trunc:
439     return bitc::CAST_TRUNC;
440   case Instruction::ZExt:
441     return bitc::CAST_ZEXT;
442   case Instruction::SExt:
443     return bitc::CAST_SEXT;
444   case Instruction::FPToUI:
445     return bitc::CAST_FPTOUI;
446   case Instruction::FPToSI:
447     return bitc::CAST_FPTOSI;
448   case Instruction::UIToFP:
449     return bitc::CAST_UITOFP;
450   case Instruction::SIToFP:
451     return bitc::CAST_SITOFP;
452   case Instruction::FPTrunc:
453     return bitc::CAST_FPTRUNC;
454   case Instruction::FPExt:
455     return bitc::CAST_FPEXT;
456   case Instruction::PtrToInt:
457     return bitc::CAST_PTRTOINT;
458   case Instruction::IntToPtr:
459     return bitc::CAST_INTTOPTR;
460   case Instruction::BitCast:
461     return bitc::CAST_BITCAST;
462   case Instruction::AddrSpaceCast:
463     return bitc::CAST_ADDRSPACECAST;
464   }
465 }
466 
467 unsigned DXILBitcodeWriter::getEncodedUnaryOpcode(unsigned Opcode) {
468   switch (Opcode) {
469   default:
470     llvm_unreachable("Unknown binary instruction!");
471   case Instruction::FNeg:
472     return bitc::UNOP_FNEG;
473   }
474 }
475 
476 unsigned DXILBitcodeWriter::getEncodedBinaryOpcode(unsigned Opcode) {
477   switch (Opcode) {
478   default:
479     llvm_unreachable("Unknown binary instruction!");
480   case Instruction::Add:
481   case Instruction::FAdd:
482     return bitc::BINOP_ADD;
483   case Instruction::Sub:
484   case Instruction::FSub:
485     return bitc::BINOP_SUB;
486   case Instruction::Mul:
487   case Instruction::FMul:
488     return bitc::BINOP_MUL;
489   case Instruction::UDiv:
490     return bitc::BINOP_UDIV;
491   case Instruction::FDiv:
492   case Instruction::SDiv:
493     return bitc::BINOP_SDIV;
494   case Instruction::URem:
495     return bitc::BINOP_UREM;
496   case Instruction::FRem:
497   case Instruction::SRem:
498     return bitc::BINOP_SREM;
499   case Instruction::Shl:
500     return bitc::BINOP_SHL;
501   case Instruction::LShr:
502     return bitc::BINOP_LSHR;
503   case Instruction::AShr:
504     return bitc::BINOP_ASHR;
505   case Instruction::And:
506     return bitc::BINOP_AND;
507   case Instruction::Or:
508     return bitc::BINOP_OR;
509   case Instruction::Xor:
510     return bitc::BINOP_XOR;
511   }
512 }
513 
514 unsigned DXILBitcodeWriter::getTypeID(Type *T, const Value *V) {
515   if (!T->isPointerTy() &&
516       // For Constant, always check PointerMap to make sure OpaquePointer in
517       // things like constant struct/array works.
518       (!V || !isa<Constant>(V)))
519     return VE.getTypeID(T);
520   auto It = PointerMap.find(V);
521   if (It != PointerMap.end())
522     return VE.getTypeID(It->second);
523   // For Constant, return T when cannot find in PointerMap.
524   // FIXME: support ConstantPointerNull which could map to more than one
525   // TypedPointerType.
526   // See https://github.com/llvm/llvm-project/issues/57942.
527   if (V && isa<Constant>(V) && !isa<ConstantPointerNull>(V))
528     return VE.getTypeID(T);
529   return VE.getTypeID(I8PtrTy);
530 }
531 
532 unsigned DXILBitcodeWriter::getGlobalObjectValueTypeID(Type *T,
533                                                        const GlobalObject *G) {
534   auto It = PointerMap.find(G);
535   if (It != PointerMap.end()) {
536     TypedPointerType *PtrTy = cast<TypedPointerType>(It->second);
537     return VE.getTypeID(PtrTy->getElementType());
538   }
539   return VE.getTypeID(T);
540 }
541 
542 unsigned DXILBitcodeWriter::getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
543   switch (Op) {
544   default:
545     llvm_unreachable("Unknown RMW operation!");
546   case AtomicRMWInst::Xchg:
547     return bitc::RMW_XCHG;
548   case AtomicRMWInst::Add:
549     return bitc::RMW_ADD;
550   case AtomicRMWInst::Sub:
551     return bitc::RMW_SUB;
552   case AtomicRMWInst::And:
553     return bitc::RMW_AND;
554   case AtomicRMWInst::Nand:
555     return bitc::RMW_NAND;
556   case AtomicRMWInst::Or:
557     return bitc::RMW_OR;
558   case AtomicRMWInst::Xor:
559     return bitc::RMW_XOR;
560   case AtomicRMWInst::Max:
561     return bitc::RMW_MAX;
562   case AtomicRMWInst::Min:
563     return bitc::RMW_MIN;
564   case AtomicRMWInst::UMax:
565     return bitc::RMW_UMAX;
566   case AtomicRMWInst::UMin:
567     return bitc::RMW_UMIN;
568   case AtomicRMWInst::FAdd:
569     return bitc::RMW_FADD;
570   case AtomicRMWInst::FSub:
571     return bitc::RMW_FSUB;
572   case AtomicRMWInst::FMax:
573     return bitc::RMW_FMAX;
574   case AtomicRMWInst::FMin:
575     return bitc::RMW_FMIN;
576   }
577 }
578 
579 unsigned DXILBitcodeWriter::getEncodedOrdering(AtomicOrdering Ordering) {
580   switch (Ordering) {
581   case AtomicOrdering::NotAtomic:
582     return bitc::ORDERING_NOTATOMIC;
583   case AtomicOrdering::Unordered:
584     return bitc::ORDERING_UNORDERED;
585   case AtomicOrdering::Monotonic:
586     return bitc::ORDERING_MONOTONIC;
587   case AtomicOrdering::Acquire:
588     return bitc::ORDERING_ACQUIRE;
589   case AtomicOrdering::Release:
590     return bitc::ORDERING_RELEASE;
591   case AtomicOrdering::AcquireRelease:
592     return bitc::ORDERING_ACQREL;
593   case AtomicOrdering::SequentiallyConsistent:
594     return bitc::ORDERING_SEQCST;
595   }
596   llvm_unreachable("Invalid ordering");
597 }
598 
599 void DXILBitcodeWriter::writeStringRecord(BitstreamWriter &Stream,
600                                           unsigned Code, StringRef Str,
601                                           unsigned AbbrevToUse) {
602   SmallVector<unsigned, 64> Vals;
603 
604   // Code: [strchar x N]
605   for (char C : Str) {
606     if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(C))
607       AbbrevToUse = 0;
608     Vals.push_back(C);
609   }
610 
611   // Emit the finished record.
612   Stream.EmitRecord(Code, Vals, AbbrevToUse);
613 }
614 
615 uint64_t DXILBitcodeWriter::getAttrKindEncoding(Attribute::AttrKind Kind) {
616   switch (Kind) {
617   case Attribute::Alignment:
618     return bitc::ATTR_KIND_ALIGNMENT;
619   case Attribute::AlwaysInline:
620     return bitc::ATTR_KIND_ALWAYS_INLINE;
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::InlineHint:
632     return bitc::ATTR_KIND_INLINE_HINT;
633   case Attribute::InReg:
634     return bitc::ATTR_KIND_IN_REG;
635   case Attribute::JumpTable:
636     return bitc::ATTR_KIND_JUMP_TABLE;
637   case Attribute::MinSize:
638     return bitc::ATTR_KIND_MIN_SIZE;
639   case Attribute::Naked:
640     return bitc::ATTR_KIND_NAKED;
641   case Attribute::Nest:
642     return bitc::ATTR_KIND_NEST;
643   case Attribute::NoAlias:
644     return bitc::ATTR_KIND_NO_ALIAS;
645   case Attribute::NoBuiltin:
646     return bitc::ATTR_KIND_NO_BUILTIN;
647   case Attribute::NoDuplicate:
648     return bitc::ATTR_KIND_NO_DUPLICATE;
649   case Attribute::NoImplicitFloat:
650     return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
651   case Attribute::NoInline:
652     return bitc::ATTR_KIND_NO_INLINE;
653   case Attribute::NonLazyBind:
654     return bitc::ATTR_KIND_NON_LAZY_BIND;
655   case Attribute::NonNull:
656     return bitc::ATTR_KIND_NON_NULL;
657   case Attribute::Dereferenceable:
658     return bitc::ATTR_KIND_DEREFERENCEABLE;
659   case Attribute::DereferenceableOrNull:
660     return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
661   case Attribute::NoRedZone:
662     return bitc::ATTR_KIND_NO_RED_ZONE;
663   case Attribute::NoReturn:
664     return bitc::ATTR_KIND_NO_RETURN;
665   case Attribute::NoUnwind:
666     return bitc::ATTR_KIND_NO_UNWIND;
667   case Attribute::OptimizeForSize:
668     return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
669   case Attribute::OptimizeNone:
670     return bitc::ATTR_KIND_OPTIMIZE_NONE;
671   case Attribute::ReadNone:
672     return bitc::ATTR_KIND_READ_NONE;
673   case Attribute::ReadOnly:
674     return bitc::ATTR_KIND_READ_ONLY;
675   case Attribute::Returned:
676     return bitc::ATTR_KIND_RETURNED;
677   case Attribute::ReturnsTwice:
678     return bitc::ATTR_KIND_RETURNS_TWICE;
679   case Attribute::SExt:
680     return bitc::ATTR_KIND_S_EXT;
681   case Attribute::StackAlignment:
682     return bitc::ATTR_KIND_STACK_ALIGNMENT;
683   case Attribute::StackProtect:
684     return bitc::ATTR_KIND_STACK_PROTECT;
685   case Attribute::StackProtectReq:
686     return bitc::ATTR_KIND_STACK_PROTECT_REQ;
687   case Attribute::StackProtectStrong:
688     return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
689   case Attribute::SafeStack:
690     return bitc::ATTR_KIND_SAFESTACK;
691   case Attribute::StructRet:
692     return bitc::ATTR_KIND_STRUCT_RET;
693   case Attribute::SanitizeAddress:
694     return bitc::ATTR_KIND_SANITIZE_ADDRESS;
695   case Attribute::SanitizeThread:
696     return bitc::ATTR_KIND_SANITIZE_THREAD;
697   case Attribute::SanitizeMemory:
698     return bitc::ATTR_KIND_SANITIZE_MEMORY;
699   case Attribute::UWTable:
700     return bitc::ATTR_KIND_UW_TABLE;
701   case Attribute::ZExt:
702     return bitc::ATTR_KIND_Z_EXT;
703   case Attribute::EndAttrKinds:
704     llvm_unreachable("Can not encode end-attribute kinds marker.");
705   case Attribute::None:
706     llvm_unreachable("Can not encode none-attribute.");
707   case Attribute::EmptyKey:
708   case Attribute::TombstoneKey:
709     llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
710   default:
711     llvm_unreachable("Trying to encode attribute not supported by DXIL. These "
712                      "should be stripped in DXILPrepare");
713   }
714 
715   llvm_unreachable("Trying to encode unknown attribute");
716 }
717 
718 void DXILBitcodeWriter::emitSignedInt64(SmallVectorImpl<uint64_t> &Vals,
719                                         uint64_t V) {
720   if ((int64_t)V >= 0)
721     Vals.push_back(V << 1);
722   else
723     Vals.push_back((-V << 1) | 1);
724 }
725 
726 void DXILBitcodeWriter::emitWideAPInt(SmallVectorImpl<uint64_t> &Vals,
727                                       const APInt &A) {
728   // We have an arbitrary precision integer value to write whose
729   // bit width is > 64. However, in canonical unsigned integer
730   // format it is likely that the high bits are going to be zero.
731   // So, we only write the number of active words.
732   unsigned NumWords = A.getActiveWords();
733   const uint64_t *RawData = A.getRawData();
734   for (unsigned i = 0; i < NumWords; i++)
735     emitSignedInt64(Vals, RawData[i]);
736 }
737 
738 uint64_t DXILBitcodeWriter::getOptimizationFlags(const Value *V) {
739   uint64_t Flags = 0;
740 
741   if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
742     if (OBO->hasNoSignedWrap())
743       Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
744     if (OBO->hasNoUnsignedWrap())
745       Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
746   } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
747     if (PEO->isExact())
748       Flags |= 1 << bitc::PEO_EXACT;
749   } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
750     if (FPMO->hasAllowReassoc() || FPMO->hasAllowContract())
751       Flags |= bitc::UnsafeAlgebra;
752     if (FPMO->hasNoNaNs())
753       Flags |= bitc::NoNaNs;
754     if (FPMO->hasNoInfs())
755       Flags |= bitc::NoInfs;
756     if (FPMO->hasNoSignedZeros())
757       Flags |= bitc::NoSignedZeros;
758     if (FPMO->hasAllowReciprocal())
759       Flags |= bitc::AllowReciprocal;
760   }
761 
762   return Flags;
763 }
764 
765 unsigned
766 DXILBitcodeWriter::getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
767   switch (Linkage) {
768   case GlobalValue::ExternalLinkage:
769     return 0;
770   case GlobalValue::WeakAnyLinkage:
771     return 16;
772   case GlobalValue::AppendingLinkage:
773     return 2;
774   case GlobalValue::InternalLinkage:
775     return 3;
776   case GlobalValue::LinkOnceAnyLinkage:
777     return 18;
778   case GlobalValue::ExternalWeakLinkage:
779     return 7;
780   case GlobalValue::CommonLinkage:
781     return 8;
782   case GlobalValue::PrivateLinkage:
783     return 9;
784   case GlobalValue::WeakODRLinkage:
785     return 17;
786   case GlobalValue::LinkOnceODRLinkage:
787     return 19;
788   case GlobalValue::AvailableExternallyLinkage:
789     return 12;
790   }
791   llvm_unreachable("Invalid linkage");
792 }
793 
794 unsigned DXILBitcodeWriter::getEncodedLinkage(const GlobalValue &GV) {
795   return getEncodedLinkage(GV.getLinkage());
796 }
797 
798 unsigned DXILBitcodeWriter::getEncodedVisibility(const GlobalValue &GV) {
799   switch (GV.getVisibility()) {
800   case GlobalValue::DefaultVisibility:
801     return 0;
802   case GlobalValue::HiddenVisibility:
803     return 1;
804   case GlobalValue::ProtectedVisibility:
805     return 2;
806   }
807   llvm_unreachable("Invalid visibility");
808 }
809 
810 unsigned DXILBitcodeWriter::getEncodedDLLStorageClass(const GlobalValue &GV) {
811   switch (GV.getDLLStorageClass()) {
812   case GlobalValue::DefaultStorageClass:
813     return 0;
814   case GlobalValue::DLLImportStorageClass:
815     return 1;
816   case GlobalValue::DLLExportStorageClass:
817     return 2;
818   }
819   llvm_unreachable("Invalid DLL storage class");
820 }
821 
822 unsigned DXILBitcodeWriter::getEncodedThreadLocalMode(const GlobalValue &GV) {
823   switch (GV.getThreadLocalMode()) {
824   case GlobalVariable::NotThreadLocal:
825     return 0;
826   case GlobalVariable::GeneralDynamicTLSModel:
827     return 1;
828   case GlobalVariable::LocalDynamicTLSModel:
829     return 2;
830   case GlobalVariable::InitialExecTLSModel:
831     return 3;
832   case GlobalVariable::LocalExecTLSModel:
833     return 4;
834   }
835   llvm_unreachable("Invalid TLS model");
836 }
837 
838 unsigned DXILBitcodeWriter::getEncodedComdatSelectionKind(const Comdat &C) {
839   switch (C.getSelectionKind()) {
840   case Comdat::Any:
841     return bitc::COMDAT_SELECTION_KIND_ANY;
842   case Comdat::ExactMatch:
843     return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
844   case Comdat::Largest:
845     return bitc::COMDAT_SELECTION_KIND_LARGEST;
846   case Comdat::NoDeduplicate:
847     return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
848   case Comdat::SameSize:
849     return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
850   }
851   llvm_unreachable("Invalid selection kind");
852 }
853 
854 ////////////////////////////////////////////////////////////////////////////////
855 /// Begin DXILBitcodeWriter Implementation
856 ////////////////////////////////////////////////////////////////////////////////
857 
858 void DXILBitcodeWriter::writeAttributeGroupTable() {
859   const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
860       VE.getAttributeGroups();
861   if (AttrGrps.empty())
862     return;
863 
864   Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
865 
866   SmallVector<uint64_t, 64> Record;
867   for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
868     unsigned AttrListIndex = Pair.first;
869     AttributeSet AS = Pair.second;
870     Record.push_back(VE.getAttributeGroupID(Pair));
871     Record.push_back(AttrListIndex);
872 
873     for (Attribute Attr : AS) {
874       if (Attr.isEnumAttribute()) {
875         uint64_t Val = getAttrKindEncoding(Attr.getKindAsEnum());
876         assert(Val <= bitc::ATTR_KIND_ARGMEMONLY &&
877                "DXIL does not support attributes above ATTR_KIND_ARGMEMONLY");
878         Record.push_back(0);
879         Record.push_back(Val);
880       } else if (Attr.isIntAttribute()) {
881         if (Attr.getKindAsEnum() == Attribute::AttrKind::Memory) {
882           MemoryEffects ME = Attr.getMemoryEffects();
883           if (ME.doesNotAccessMemory()) {
884             Record.push_back(0);
885             Record.push_back(bitc::ATTR_KIND_READ_NONE);
886           } else {
887             if (ME.onlyReadsMemory()) {
888               Record.push_back(0);
889               Record.push_back(bitc::ATTR_KIND_READ_ONLY);
890             }
891             if (ME.onlyAccessesArgPointees()) {
892               Record.push_back(0);
893               Record.push_back(bitc::ATTR_KIND_ARGMEMONLY);
894             }
895           }
896         } else {
897           uint64_t Val = getAttrKindEncoding(Attr.getKindAsEnum());
898           assert(Val <= bitc::ATTR_KIND_ARGMEMONLY &&
899                  "DXIL does not support attributes above ATTR_KIND_ARGMEMONLY");
900           Record.push_back(1);
901           Record.push_back(Val);
902           Record.push_back(Attr.getValueAsInt());
903         }
904       } else {
905         StringRef Kind = Attr.getKindAsString();
906         StringRef Val = Attr.getValueAsString();
907 
908         Record.push_back(Val.empty() ? 3 : 4);
909         Record.append(Kind.begin(), Kind.end());
910         Record.push_back(0);
911         if (!Val.empty()) {
912           Record.append(Val.begin(), Val.end());
913           Record.push_back(0);
914         }
915       }
916     }
917 
918     Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
919     Record.clear();
920   }
921 
922   Stream.ExitBlock();
923 }
924 
925 void DXILBitcodeWriter::writeAttributeTable() {
926   const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
927   if (Attrs.empty())
928     return;
929 
930   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
931 
932   SmallVector<uint64_t, 64> Record;
933   for (AttributeList AL : Attrs) {
934     for (unsigned i : AL.indexes()) {
935       AttributeSet AS = AL.getAttributes(i);
936       if (AS.hasAttributes())
937         Record.push_back(VE.getAttributeGroupID({i, AS}));
938     }
939 
940     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
941     Record.clear();
942   }
943 
944   Stream.ExitBlock();
945 }
946 
947 /// WriteTypeTable - Write out the type table for a module.
948 void DXILBitcodeWriter::writeTypeTable() {
949   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
950 
951   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
952   SmallVector<uint64_t, 64> TypeVals;
953 
954   uint64_t NumBits = VE.computeBitsRequiredForTypeIndices();
955 
956   // Abbrev for TYPE_CODE_POINTER.
957   auto Abbv = std::make_shared<BitCodeAbbrev>();
958   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
959   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
960   Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
961   unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
962 
963   // Abbrev for TYPE_CODE_FUNCTION.
964   Abbv = std::make_shared<BitCodeAbbrev>();
965   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
966   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
967   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
968   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
969   unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
970 
971   // Abbrev for TYPE_CODE_STRUCT_ANON.
972   Abbv = std::make_shared<BitCodeAbbrev>();
973   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
974   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
975   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
976   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
977   unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
978 
979   // Abbrev for TYPE_CODE_STRUCT_NAME.
980   Abbv = std::make_shared<BitCodeAbbrev>();
981   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
982   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
983   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
984   unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
985 
986   // Abbrev for TYPE_CODE_STRUCT_NAMED.
987   Abbv = std::make_shared<BitCodeAbbrev>();
988   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
989   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
990   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
991   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
992   unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
993 
994   // Abbrev for TYPE_CODE_ARRAY.
995   Abbv = std::make_shared<BitCodeAbbrev>();
996   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
997   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
998   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
999   unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1000 
1001   // Emit an entry count so the reader can reserve space.
1002   TypeVals.push_back(TypeList.size());
1003   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1004   TypeVals.clear();
1005 
1006   // Loop over all of the types, emitting each in turn.
1007   for (Type *T : TypeList) {
1008     int AbbrevToUse = 0;
1009     unsigned Code = 0;
1010 
1011     switch (T->getTypeID()) {
1012     case Type::BFloatTyID:
1013     case Type::X86_AMXTyID:
1014     case Type::TokenTyID:
1015     case Type::TargetExtTyID:
1016       llvm_unreachable("These should never be used!!!");
1017       break;
1018     case Type::VoidTyID:
1019       Code = bitc::TYPE_CODE_VOID;
1020       break;
1021     case Type::HalfTyID:
1022       Code = bitc::TYPE_CODE_HALF;
1023       break;
1024     case Type::FloatTyID:
1025       Code = bitc::TYPE_CODE_FLOAT;
1026       break;
1027     case Type::DoubleTyID:
1028       Code = bitc::TYPE_CODE_DOUBLE;
1029       break;
1030     case Type::X86_FP80TyID:
1031       Code = bitc::TYPE_CODE_X86_FP80;
1032       break;
1033     case Type::FP128TyID:
1034       Code = bitc::TYPE_CODE_FP128;
1035       break;
1036     case Type::PPC_FP128TyID:
1037       Code = bitc::TYPE_CODE_PPC_FP128;
1038       break;
1039     case Type::LabelTyID:
1040       Code = bitc::TYPE_CODE_LABEL;
1041       break;
1042     case Type::MetadataTyID:
1043       Code = bitc::TYPE_CODE_METADATA;
1044       break;
1045     case Type::IntegerTyID:
1046       // INTEGER: [width]
1047       Code = bitc::TYPE_CODE_INTEGER;
1048       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
1049       break;
1050     case Type::TypedPointerTyID: {
1051       TypedPointerType *PTy = cast<TypedPointerType>(T);
1052       // POINTER: [pointee type, address space]
1053       Code = bitc::TYPE_CODE_POINTER;
1054       TypeVals.push_back(getTypeID(PTy->getElementType()));
1055       unsigned AddressSpace = PTy->getAddressSpace();
1056       TypeVals.push_back(AddressSpace);
1057       if (AddressSpace == 0)
1058         AbbrevToUse = PtrAbbrev;
1059       break;
1060     }
1061     case Type::PointerTyID: {
1062       // POINTER: [pointee type, address space]
1063       // Emitting an empty struct type for the pointer's type allows this to be
1064       // order-independent. Non-struct types must be emitted in bitcode before
1065       // they can be referenced.
1066       TypeVals.push_back(false);
1067       Code = bitc::TYPE_CODE_OPAQUE;
1068       writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME,
1069                         "dxilOpaquePtrReservedName", StructNameAbbrev);
1070       break;
1071     }
1072     case Type::FunctionTyID: {
1073       FunctionType *FT = cast<FunctionType>(T);
1074       // FUNCTION: [isvararg, retty, paramty x N]
1075       Code = bitc::TYPE_CODE_FUNCTION;
1076       TypeVals.push_back(FT->isVarArg());
1077       TypeVals.push_back(getTypeID(FT->getReturnType()));
1078       for (Type *PTy : FT->params())
1079         TypeVals.push_back(getTypeID(PTy));
1080       AbbrevToUse = FunctionAbbrev;
1081       break;
1082     }
1083     case Type::StructTyID: {
1084       StructType *ST = cast<StructType>(T);
1085       // STRUCT: [ispacked, eltty x N]
1086       TypeVals.push_back(ST->isPacked());
1087       // Output all of the element types.
1088       for (Type *ElTy : ST->elements())
1089         TypeVals.push_back(getTypeID(ElTy));
1090 
1091       if (ST->isLiteral()) {
1092         Code = bitc::TYPE_CODE_STRUCT_ANON;
1093         AbbrevToUse = StructAnonAbbrev;
1094       } else {
1095         if (ST->isOpaque()) {
1096           Code = bitc::TYPE_CODE_OPAQUE;
1097         } else {
1098           Code = bitc::TYPE_CODE_STRUCT_NAMED;
1099           AbbrevToUse = StructNamedAbbrev;
1100         }
1101 
1102         // Emit the name if it is present.
1103         if (!ST->getName().empty())
1104           writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
1105                             StructNameAbbrev);
1106       }
1107       break;
1108     }
1109     case Type::ArrayTyID: {
1110       ArrayType *AT = cast<ArrayType>(T);
1111       // ARRAY: [numelts, eltty]
1112       Code = bitc::TYPE_CODE_ARRAY;
1113       TypeVals.push_back(AT->getNumElements());
1114       TypeVals.push_back(getTypeID(AT->getElementType()));
1115       AbbrevToUse = ArrayAbbrev;
1116       break;
1117     }
1118     case Type::FixedVectorTyID:
1119     case Type::ScalableVectorTyID: {
1120       VectorType *VT = cast<VectorType>(T);
1121       // VECTOR [numelts, eltty]
1122       Code = bitc::TYPE_CODE_VECTOR;
1123       TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1124       TypeVals.push_back(getTypeID(VT->getElementType()));
1125       break;
1126     }
1127     }
1128 
1129     // Emit the finished record.
1130     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1131     TypeVals.clear();
1132   }
1133 
1134   Stream.ExitBlock();
1135 }
1136 
1137 void DXILBitcodeWriter::writeComdats() {
1138   SmallVector<uint16_t, 64> Vals;
1139   for (const Comdat *C : VE.getComdats()) {
1140     // COMDAT: [selection_kind, name]
1141     Vals.push_back(getEncodedComdatSelectionKind(*C));
1142     size_t Size = C->getName().size();
1143     assert(isUInt<16>(Size));
1144     Vals.push_back(Size);
1145     for (char Chr : C->getName())
1146       Vals.push_back((unsigned char)Chr);
1147     Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1148     Vals.clear();
1149   }
1150 }
1151 
1152 void DXILBitcodeWriter::writeValueSymbolTableForwardDecl() {}
1153 
1154 /// Emit top-level description of module, including target triple, inline asm,
1155 /// descriptors for global variables, and function prototype info.
1156 /// Returns the bit offset to backpatch with the location of the real VST.
1157 void DXILBitcodeWriter::writeModuleInfo() {
1158   // Emit various pieces of data attached to a module.
1159   if (!M.getTargetTriple().empty())
1160     writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1161                       0 /*TODO*/);
1162   const std::string &DL = M.getDataLayoutStr();
1163   if (!DL.empty())
1164     writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
1165   if (!M.getModuleInlineAsm().empty())
1166     writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1167                       0 /*TODO*/);
1168 
1169   // Emit information about sections and GC, computing how many there are. Also
1170   // compute the maximum alignment value.
1171   std::map<std::string, unsigned> SectionMap;
1172   std::map<std::string, unsigned> GCMap;
1173   MaybeAlign MaxAlignment;
1174   unsigned MaxGlobalType = 0;
1175   const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1176     if (A)
1177       MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1178   };
1179   for (const GlobalVariable &GV : M.globals()) {
1180     UpdateMaxAlignment(GV.getAlign());
1181     // Use getGlobalObjectValueTypeID to look up the enumerated type ID for
1182     // Global Variable types.
1183     MaxGlobalType = std::max(
1184         MaxGlobalType, getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1185     if (GV.hasSection()) {
1186       // Give section names unique ID's.
1187       unsigned &Entry = SectionMap[std::string(GV.getSection())];
1188       if (!Entry) {
1189         writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME,
1190                           GV.getSection(), 0 /*TODO*/);
1191         Entry = SectionMap.size();
1192       }
1193     }
1194   }
1195   for (const Function &F : M) {
1196     UpdateMaxAlignment(F.getAlign());
1197     if (F.hasSection()) {
1198       // Give section names unique ID's.
1199       unsigned &Entry = SectionMap[std::string(F.getSection())];
1200       if (!Entry) {
1201         writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1202                           0 /*TODO*/);
1203         Entry = SectionMap.size();
1204       }
1205     }
1206     if (F.hasGC()) {
1207       // Same for GC names.
1208       unsigned &Entry = GCMap[F.getGC()];
1209       if (!Entry) {
1210         writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
1211                           0 /*TODO*/);
1212         Entry = GCMap.size();
1213       }
1214     }
1215   }
1216 
1217   // Emit abbrev for globals, now that we know # sections and max alignment.
1218   unsigned SimpleGVarAbbrev = 0;
1219   if (!M.global_empty()) {
1220     // Add an abbrev for common globals with no visibility or thread
1221     // localness.
1222     auto Abbv = std::make_shared<BitCodeAbbrev>();
1223     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1224     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1225                               Log2_32_Ceil(MaxGlobalType + 1)));
1226     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddrSpace << 2
1227                                                            //| explicitType << 1
1228                                                            //| constant
1229     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Initializer.
1230     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1231     if (!MaxAlignment)                                     // Alignment.
1232       Abbv->Add(BitCodeAbbrevOp(0));
1233     else {
1234       unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1235       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1236                                 Log2_32_Ceil(MaxEncAlignment + 1)));
1237     }
1238     if (SectionMap.empty()) // Section.
1239       Abbv->Add(BitCodeAbbrevOp(0));
1240     else
1241       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1242                                 Log2_32_Ceil(SectionMap.size() + 1)));
1243     // Don't bother emitting vis + thread local.
1244     SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1245   }
1246 
1247   // Emit the global variable information.
1248   SmallVector<unsigned, 64> Vals;
1249   for (const GlobalVariable &GV : M.globals()) {
1250     unsigned AbbrevToUse = 0;
1251 
1252     // GLOBALVAR: [type, isconst, initid,
1253     //             linkage, alignment, section, visibility, threadlocal,
1254     //             unnamed_addr, externally_initialized, dllstorageclass,
1255     //             comdat]
1256     Vals.push_back(getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1257     Vals.push_back(
1258         GV.getType()->getAddressSpace() << 2 | 2 |
1259         (GV.isConstant() ? 1 : 0)); // HLSL Change - bitwise | was used with
1260                                     // unsigned int and bool
1261     Vals.push_back(
1262         GV.isDeclaration() ? 0 : (VE.getValueID(GV.getInitializer()) + 1));
1263     Vals.push_back(getEncodedLinkage(GV));
1264     Vals.push_back(getEncodedAlign(GV.getAlign()));
1265     Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1266                                    : 0);
1267     if (GV.isThreadLocal() ||
1268         GV.getVisibility() != GlobalValue::DefaultVisibility ||
1269         GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1270         GV.isExternallyInitialized() ||
1271         GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1272         GV.hasComdat()) {
1273       Vals.push_back(getEncodedVisibility(GV));
1274       Vals.push_back(getEncodedThreadLocalMode(GV));
1275       Vals.push_back(GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1276       Vals.push_back(GV.isExternallyInitialized());
1277       Vals.push_back(getEncodedDLLStorageClass(GV));
1278       Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1279     } else {
1280       AbbrevToUse = SimpleGVarAbbrev;
1281     }
1282 
1283     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1284     Vals.clear();
1285   }
1286 
1287   // Emit the function proto information.
1288   for (const Function &F : M) {
1289     // FUNCTION:  [type, callingconv, isproto, linkage, paramattrs, alignment,
1290     //             section, visibility, gc, unnamed_addr, prologuedata,
1291     //             dllstorageclass, comdat, prefixdata, personalityfn]
1292     Vals.push_back(getGlobalObjectValueTypeID(F.getFunctionType(), &F));
1293     Vals.push_back(F.getCallingConv());
1294     Vals.push_back(F.isDeclaration());
1295     Vals.push_back(getEncodedLinkage(F));
1296     Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1297     Vals.push_back(getEncodedAlign(F.getAlign()));
1298     Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1299                                   : 0);
1300     Vals.push_back(getEncodedVisibility(F));
1301     Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1302     Vals.push_back(F.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1303     Vals.push_back(
1304         F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1) : 0);
1305     Vals.push_back(getEncodedDLLStorageClass(F));
1306     Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1307     Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1308                                      : 0);
1309     Vals.push_back(
1310         F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1311 
1312     unsigned AbbrevToUse = 0;
1313     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1314     Vals.clear();
1315   }
1316 
1317   // Emit the alias information.
1318   for (const GlobalAlias &A : M.aliases()) {
1319     // ALIAS: [alias type, aliasee val#, linkage, visibility]
1320     Vals.push_back(getTypeID(A.getValueType(), &A));
1321     Vals.push_back(VE.getValueID(A.getAliasee()));
1322     Vals.push_back(getEncodedLinkage(A));
1323     Vals.push_back(getEncodedVisibility(A));
1324     Vals.push_back(getEncodedDLLStorageClass(A));
1325     Vals.push_back(getEncodedThreadLocalMode(A));
1326     Vals.push_back(A.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1327     unsigned AbbrevToUse = 0;
1328     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS_OLD, Vals, AbbrevToUse);
1329     Vals.clear();
1330   }
1331 }
1332 
1333 void DXILBitcodeWriter::writeValueAsMetadata(
1334     const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1335   // Mimic an MDNode with a value as one operand.
1336   Value *V = MD->getValue();
1337   Type *Ty = V->getType();
1338   if (Function *F = dyn_cast<Function>(V))
1339     Ty = TypedPointerType::get(F->getFunctionType(), F->getAddressSpace());
1340   else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
1341     Ty = TypedPointerType::get(GV->getValueType(), GV->getAddressSpace());
1342   Record.push_back(getTypeID(Ty, V));
1343   Record.push_back(VE.getValueID(V));
1344   Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
1345   Record.clear();
1346 }
1347 
1348 void DXILBitcodeWriter::writeMDTuple(const MDTuple *N,
1349                                      SmallVectorImpl<uint64_t> &Record,
1350                                      unsigned Abbrev) {
1351   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1352     Metadata *MD = N->getOperand(i);
1353     assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1354            "Unexpected function-local metadata");
1355     Record.push_back(VE.getMetadataOrNullID(MD));
1356   }
1357   Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1358                                     : bitc::METADATA_NODE,
1359                     Record, Abbrev);
1360   Record.clear();
1361 }
1362 
1363 void DXILBitcodeWriter::writeDILocation(const DILocation *N,
1364                                         SmallVectorImpl<uint64_t> &Record,
1365                                         unsigned &Abbrev) {
1366   if (!Abbrev)
1367     Abbrev = createDILocationAbbrev();
1368   Record.push_back(N->isDistinct());
1369   Record.push_back(N->getLine());
1370   Record.push_back(N->getColumn());
1371   Record.push_back(VE.getMetadataID(N->getScope()));
1372   Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1373 
1374   Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1375   Record.clear();
1376 }
1377 
1378 static uint64_t rotateSign(APInt Val) {
1379   int64_t I = Val.getSExtValue();
1380   uint64_t U = I;
1381   return I < 0 ? ~(U << 1) : U << 1;
1382 }
1383 
1384 void DXILBitcodeWriter::writeDISubrange(const DISubrange *N,
1385                                         SmallVectorImpl<uint64_t> &Record,
1386                                         unsigned Abbrev) {
1387   Record.push_back(N->isDistinct());
1388 
1389   // TODO: Do we need to handle DIExpression here? What about cases where Count
1390   // isn't specified but UpperBound and such are?
1391   ConstantInt *Count = dyn_cast<ConstantInt *>(N->getCount());
1392   assert(Count && "Count is missing or not ConstantInt");
1393   Record.push_back(Count->getValue().getSExtValue());
1394 
1395   // TODO: Similarly, DIExpression is allowed here now
1396   DISubrange::BoundType LowerBound = N->getLowerBound();
1397   assert((LowerBound.isNull() || isa<ConstantInt *>(LowerBound)) &&
1398          "Lower bound provided but not ConstantInt");
1399   Record.push_back(
1400       LowerBound ? rotateSign(cast<ConstantInt *>(LowerBound)->getValue()) : 0);
1401 
1402   Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1403   Record.clear();
1404 }
1405 
1406 void DXILBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1407                                           SmallVectorImpl<uint64_t> &Record,
1408                                           unsigned Abbrev) {
1409   Record.push_back(N->isDistinct());
1410   Record.push_back(rotateSign(N->getValue()));
1411   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1412 
1413   Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
1414   Record.clear();
1415 }
1416 
1417 void DXILBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1418                                          SmallVectorImpl<uint64_t> &Record,
1419                                          unsigned Abbrev) {
1420   Record.push_back(N->isDistinct());
1421   Record.push_back(N->getTag());
1422   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1423   Record.push_back(N->getSizeInBits());
1424   Record.push_back(N->getAlignInBits());
1425   Record.push_back(N->getEncoding());
1426 
1427   Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
1428   Record.clear();
1429 }
1430 
1431 void DXILBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1432                                            SmallVectorImpl<uint64_t> &Record,
1433                                            unsigned Abbrev) {
1434   Record.push_back(N->isDistinct());
1435   Record.push_back(N->getTag());
1436   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1437   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1438   Record.push_back(N->getLine());
1439   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1440   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1441   Record.push_back(N->getSizeInBits());
1442   Record.push_back(N->getAlignInBits());
1443   Record.push_back(N->getOffsetInBits());
1444   Record.push_back(N->getFlags());
1445   Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1446 
1447   Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
1448   Record.clear();
1449 }
1450 
1451 void DXILBitcodeWriter::writeDICompositeType(const DICompositeType *N,
1452                                              SmallVectorImpl<uint64_t> &Record,
1453                                              unsigned Abbrev) {
1454   Record.push_back(N->isDistinct());
1455   Record.push_back(N->getTag());
1456   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1457   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1458   Record.push_back(N->getLine());
1459   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1460   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1461   Record.push_back(N->getSizeInBits());
1462   Record.push_back(N->getAlignInBits());
1463   Record.push_back(N->getOffsetInBits());
1464   Record.push_back(N->getFlags());
1465   Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1466   Record.push_back(N->getRuntimeLang());
1467   Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1468   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1469   Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1470 
1471   Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
1472   Record.clear();
1473 }
1474 
1475 void DXILBitcodeWriter::writeDISubroutineType(const DISubroutineType *N,
1476                                               SmallVectorImpl<uint64_t> &Record,
1477                                               unsigned Abbrev) {
1478   Record.push_back(N->isDistinct());
1479   Record.push_back(N->getFlags());
1480   Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1481 
1482   Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
1483   Record.clear();
1484 }
1485 
1486 void DXILBitcodeWriter::writeDIFile(const DIFile *N,
1487                                     SmallVectorImpl<uint64_t> &Record,
1488                                     unsigned Abbrev) {
1489   Record.push_back(N->isDistinct());
1490   Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1491   Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1492 
1493   Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1494   Record.clear();
1495 }
1496 
1497 void DXILBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1498                                            SmallVectorImpl<uint64_t> &Record,
1499                                            unsigned Abbrev) {
1500   Record.push_back(N->isDistinct());
1501   Record.push_back(N->getSourceLanguage());
1502   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1503   Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1504   Record.push_back(N->isOptimized());
1505   Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1506   Record.push_back(N->getRuntimeVersion());
1507   Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1508   Record.push_back(N->getEmissionKind());
1509   Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1510   Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1511   Record.push_back(/* subprograms */ 0);
1512   Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1513   Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1514   Record.push_back(N->getDWOId());
1515 
1516   Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1517   Record.clear();
1518 }
1519 
1520 void DXILBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1521                                           SmallVectorImpl<uint64_t> &Record,
1522                                           unsigned Abbrev) {
1523   Record.push_back(N->isDistinct());
1524   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1525   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1526   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1527   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1528   Record.push_back(N->getLine());
1529   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1530   Record.push_back(N->isLocalToUnit());
1531   Record.push_back(N->isDefinition());
1532   Record.push_back(N->getScopeLine());
1533   Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1534   Record.push_back(N->getVirtuality());
1535   Record.push_back(N->getVirtualIndex());
1536   Record.push_back(N->getFlags());
1537   Record.push_back(N->isOptimized());
1538   Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
1539   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1540   Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1541   Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
1542 
1543   Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1544   Record.clear();
1545 }
1546 
1547 void DXILBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1548                                             SmallVectorImpl<uint64_t> &Record,
1549                                             unsigned Abbrev) {
1550   Record.push_back(N->isDistinct());
1551   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1552   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1553   Record.push_back(N->getLine());
1554   Record.push_back(N->getColumn());
1555 
1556   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
1557   Record.clear();
1558 }
1559 
1560 void DXILBitcodeWriter::writeDILexicalBlockFile(
1561     const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1562     unsigned Abbrev) {
1563   Record.push_back(N->isDistinct());
1564   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1565   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1566   Record.push_back(N->getDiscriminator());
1567 
1568   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
1569   Record.clear();
1570 }
1571 
1572 void DXILBitcodeWriter::writeDINamespace(const DINamespace *N,
1573                                          SmallVectorImpl<uint64_t> &Record,
1574                                          unsigned Abbrev) {
1575   Record.push_back(N->isDistinct());
1576   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1577   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1578   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1579   Record.push_back(/* line number */ 0);
1580 
1581   Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1582   Record.clear();
1583 }
1584 
1585 void DXILBitcodeWriter::writeDIModule(const DIModule *N,
1586                                       SmallVectorImpl<uint64_t> &Record,
1587                                       unsigned Abbrev) {
1588   Record.push_back(N->isDistinct());
1589   for (auto &I : N->operands())
1590     Record.push_back(VE.getMetadataOrNullID(I));
1591 
1592   Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1593   Record.clear();
1594 }
1595 
1596 void DXILBitcodeWriter::writeDITemplateTypeParameter(
1597     const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1598     unsigned Abbrev) {
1599   Record.push_back(N->isDistinct());
1600   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1601   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1602 
1603   Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
1604   Record.clear();
1605 }
1606 
1607 void DXILBitcodeWriter::writeDITemplateValueParameter(
1608     const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1609     unsigned Abbrev) {
1610   Record.push_back(N->isDistinct());
1611   Record.push_back(N->getTag());
1612   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1613   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1614   Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1615 
1616   Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
1617   Record.clear();
1618 }
1619 
1620 void DXILBitcodeWriter::writeDIGlobalVariable(const DIGlobalVariable *N,
1621                                               SmallVectorImpl<uint64_t> &Record,
1622                                               unsigned Abbrev) {
1623   Record.push_back(N->isDistinct());
1624   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1625   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1626   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1627   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1628   Record.push_back(N->getLine());
1629   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1630   Record.push_back(N->isLocalToUnit());
1631   Record.push_back(N->isDefinition());
1632   Record.push_back(/* N->getRawVariable() */ 0);
1633   Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1634 
1635   Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1636   Record.clear();
1637 }
1638 
1639 void DXILBitcodeWriter::writeDILocalVariable(const DILocalVariable *N,
1640                                              SmallVectorImpl<uint64_t> &Record,
1641                                              unsigned Abbrev) {
1642   Record.push_back(N->isDistinct());
1643   Record.push_back(N->getTag());
1644   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1645   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1646   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1647   Record.push_back(N->getLine());
1648   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1649   Record.push_back(N->getArg());
1650   Record.push_back(N->getFlags());
1651 
1652   Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1653   Record.clear();
1654 }
1655 
1656 void DXILBitcodeWriter::writeDIExpression(const DIExpression *N,
1657                                           SmallVectorImpl<uint64_t> &Record,
1658                                           unsigned Abbrev) {
1659   Record.reserve(N->getElements().size() + 1);
1660 
1661   Record.push_back(N->isDistinct());
1662   Record.append(N->elements_begin(), N->elements_end());
1663 
1664   Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1665   Record.clear();
1666 }
1667 
1668 void DXILBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1669                                             SmallVectorImpl<uint64_t> &Record,
1670                                             unsigned Abbrev) {
1671   llvm_unreachable("DXIL does not support objc!!!");
1672 }
1673 
1674 void DXILBitcodeWriter::writeDIImportedEntity(const DIImportedEntity *N,
1675                                               SmallVectorImpl<uint64_t> &Record,
1676                                               unsigned Abbrev) {
1677   Record.push_back(N->isDistinct());
1678   Record.push_back(N->getTag());
1679   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1680   Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1681   Record.push_back(N->getLine());
1682   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1683 
1684   Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
1685   Record.clear();
1686 }
1687 
1688 unsigned DXILBitcodeWriter::createDILocationAbbrev() {
1689   // Abbrev for METADATA_LOCATION.
1690   //
1691   // Assume the column is usually under 128, and always output the inlined-at
1692   // location (it's never more expensive than building an array size 1).
1693   std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1694   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1695   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1696   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1697   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1698   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1699   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1700   return Stream.EmitAbbrev(std::move(Abbv));
1701 }
1702 
1703 unsigned DXILBitcodeWriter::createGenericDINodeAbbrev() {
1704   // Abbrev for METADATA_GENERIC_DEBUG.
1705   //
1706   // Assume the column is usually under 128, and always output the inlined-at
1707   // location (it's never more expensive than building an array size 1).
1708   std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1709   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1710   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1711   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1712   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1713   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1714   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1715   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1716   return Stream.EmitAbbrev(std::move(Abbv));
1717 }
1718 
1719 void DXILBitcodeWriter::writeMetadataRecords(ArrayRef<const Metadata *> MDs,
1720                                              SmallVectorImpl<uint64_t> &Record,
1721                                              std::vector<unsigned> *MDAbbrevs,
1722                                              std::vector<uint64_t> *IndexPos) {
1723   if (MDs.empty())
1724     return;
1725 
1726     // Initialize MDNode abbreviations.
1727 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1728 #include "llvm/IR/Metadata.def"
1729 
1730   for (const Metadata *MD : MDs) {
1731     if (IndexPos)
1732       IndexPos->push_back(Stream.GetCurrentBitNo());
1733     if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1734       assert(N->isResolved() && "Expected forward references to be resolved");
1735 
1736       switch (N->getMetadataID()) {
1737       default:
1738         llvm_unreachable("Invalid MDNode subclass");
1739 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
1740   case Metadata::CLASS##Kind:                                                  \
1741     if (MDAbbrevs)                                                             \
1742       write##CLASS(cast<CLASS>(N), Record,                                     \
1743                    (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]);             \
1744     else                                                                       \
1745       write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev);                     \
1746     continue;
1747 #include "llvm/IR/Metadata.def"
1748       }
1749     }
1750     writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
1751   }
1752 }
1753 
1754 unsigned DXILBitcodeWriter::createMetadataStringsAbbrev() {
1755   auto Abbv = std::make_shared<BitCodeAbbrev>();
1756   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING_OLD));
1757   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1758   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1759   return Stream.EmitAbbrev(std::move(Abbv));
1760 }
1761 
1762 void DXILBitcodeWriter::writeMetadataStrings(
1763     ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
1764   if (Strings.empty())
1765     return;
1766 
1767   unsigned MDSAbbrev = createMetadataStringsAbbrev();
1768 
1769   for (const Metadata *MD : Strings) {
1770     const MDString *MDS = cast<MDString>(MD);
1771     // Code: [strchar x N]
1772     Record.append(MDS->bytes_begin(), MDS->bytes_end());
1773 
1774     // Emit the finished record.
1775     Stream.EmitRecord(bitc::METADATA_STRING_OLD, Record, MDSAbbrev);
1776     Record.clear();
1777   }
1778 }
1779 
1780 void DXILBitcodeWriter::writeModuleMetadata() {
1781   if (!VE.hasMDs() && M.named_metadata_empty())
1782     return;
1783 
1784   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 5);
1785 
1786   // Emit all abbrevs upfront, so that the reader can jump in the middle of the
1787   // block and load any metadata.
1788   std::vector<unsigned> MDAbbrevs;
1789 
1790   MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
1791   MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
1792   MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
1793       createGenericDINodeAbbrev();
1794 
1795   unsigned NameAbbrev = 0;
1796   if (!M.named_metadata_empty()) {
1797     // Abbrev for METADATA_NAME.
1798     std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1799     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1800     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1801     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1802     NameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1803   }
1804 
1805   SmallVector<uint64_t, 64> Record;
1806   writeMetadataStrings(VE.getMDStrings(), Record);
1807 
1808   std::vector<uint64_t> IndexPos;
1809   IndexPos.reserve(VE.getNonMDStrings().size());
1810   writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
1811 
1812   // Write named metadata.
1813   for (const NamedMDNode &NMD : M.named_metadata()) {
1814     // Write name.
1815     StringRef Str = NMD.getName();
1816     Record.append(Str.bytes_begin(), Str.bytes_end());
1817     Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
1818     Record.clear();
1819 
1820     // Write named metadata operands.
1821     for (const MDNode *N : NMD.operands())
1822       Record.push_back(VE.getMetadataID(N));
1823     Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
1824     Record.clear();
1825   }
1826 
1827   Stream.ExitBlock();
1828 }
1829 
1830 void DXILBitcodeWriter::writeFunctionMetadata(const Function &F) {
1831   if (!VE.hasMDs())
1832     return;
1833 
1834   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4);
1835   SmallVector<uint64_t, 64> Record;
1836   writeMetadataStrings(VE.getMDStrings(), Record);
1837   writeMetadataRecords(VE.getNonMDStrings(), Record);
1838   Stream.ExitBlock();
1839 }
1840 
1841 void DXILBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
1842   Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
1843 
1844   SmallVector<uint64_t, 64> Record;
1845 
1846   // Write metadata attachments
1847   // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1848   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1849   F.getAllMetadata(MDs);
1850   if (!MDs.empty()) {
1851     for (const auto &I : MDs) {
1852       Record.push_back(I.first);
1853       Record.push_back(VE.getMetadataID(I.second));
1854     }
1855     Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1856     Record.clear();
1857   }
1858 
1859   for (const BasicBlock &BB : F)
1860     for (const Instruction &I : BB) {
1861       MDs.clear();
1862       I.getAllMetadataOtherThanDebugLoc(MDs);
1863 
1864       // If no metadata, ignore instruction.
1865       if (MDs.empty())
1866         continue;
1867 
1868       Record.push_back(VE.getInstructionID(&I));
1869 
1870       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1871         Record.push_back(MDs[i].first);
1872         Record.push_back(VE.getMetadataID(MDs[i].second));
1873       }
1874       Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1875       Record.clear();
1876     }
1877 
1878   Stream.ExitBlock();
1879 }
1880 
1881 void DXILBitcodeWriter::writeModuleMetadataKinds() {
1882   SmallVector<uint64_t, 64> Record;
1883 
1884   // Write metadata kinds
1885   // METADATA_KIND - [n x [id, name]]
1886   SmallVector<StringRef, 8> Names;
1887   M.getMDKindNames(Names);
1888 
1889   if (Names.empty())
1890     return;
1891 
1892   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1893 
1894   for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
1895     Record.push_back(MDKindID);
1896     StringRef KName = Names[MDKindID];
1897     Record.append(KName.begin(), KName.end());
1898 
1899     Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
1900     Record.clear();
1901   }
1902 
1903   Stream.ExitBlock();
1904 }
1905 
1906 void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1907                                        bool isGlobal) {
1908   if (FirstVal == LastVal)
1909     return;
1910 
1911   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
1912 
1913   unsigned AggregateAbbrev = 0;
1914   unsigned String8Abbrev = 0;
1915   unsigned CString7Abbrev = 0;
1916   unsigned CString6Abbrev = 0;
1917   // If this is a constant pool for the module, emit module-specific abbrevs.
1918   if (isGlobal) {
1919     // Abbrev for CST_CODE_AGGREGATE.
1920     auto Abbv = std::make_shared<BitCodeAbbrev>();
1921     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1922     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1923     Abbv->Add(
1924         BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal + 1)));
1925     AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1926 
1927     // Abbrev for CST_CODE_STRING.
1928     Abbv = std::make_shared<BitCodeAbbrev>();
1929     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1930     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1931     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1932     String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1933     // Abbrev for CST_CODE_CSTRING.
1934     Abbv = std::make_shared<BitCodeAbbrev>();
1935     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1936     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1937     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1938     CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1939     // Abbrev for CST_CODE_CSTRING.
1940     Abbv = std::make_shared<BitCodeAbbrev>();
1941     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1942     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1943     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1944     CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1945   }
1946 
1947   SmallVector<uint64_t, 64> Record;
1948 
1949   const ValueEnumerator::ValueList &Vals = VE.getValues();
1950   Type *LastTy = nullptr;
1951   for (unsigned i = FirstVal; i != LastVal; ++i) {
1952     const Value *V = Vals[i].first;
1953     // If we need to switch types, do so now.
1954     if (V->getType() != LastTy) {
1955       LastTy = V->getType();
1956       Record.push_back(getTypeID(LastTy, V));
1957       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1958                         CONSTANTS_SETTYPE_ABBREV);
1959       Record.clear();
1960     }
1961 
1962     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
1963       Record.push_back(unsigned(IA->hasSideEffects()) |
1964                        unsigned(IA->isAlignStack()) << 1 |
1965                        unsigned(IA->getDialect() & 1) << 2);
1966 
1967       // Add the asm string.
1968       const std::string &AsmStr = IA->getAsmString();
1969       Record.push_back(AsmStr.size());
1970       Record.append(AsmStr.begin(), AsmStr.end());
1971 
1972       // Add the constraint string.
1973       const std::string &ConstraintStr = IA->getConstraintString();
1974       Record.push_back(ConstraintStr.size());
1975       Record.append(ConstraintStr.begin(), ConstraintStr.end());
1976       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
1977       Record.clear();
1978       continue;
1979     }
1980     const Constant *C = cast<Constant>(V);
1981     unsigned Code = -1U;
1982     unsigned AbbrevToUse = 0;
1983     if (C->isNullValue()) {
1984       Code = bitc::CST_CODE_NULL;
1985     } else if (isa<UndefValue>(C)) {
1986       Code = bitc::CST_CODE_UNDEF;
1987     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
1988       if (IV->getBitWidth() <= 64) {
1989         uint64_t V = IV->getSExtValue();
1990         emitSignedInt64(Record, V);
1991         Code = bitc::CST_CODE_INTEGER;
1992         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1993       } else { // Wide integers, > 64 bits in size.
1994         // We have an arbitrary precision integer value to write whose
1995         // bit width is > 64. However, in canonical unsigned integer
1996         // format it is likely that the high bits are going to be zero.
1997         // So, we only write the number of active words.
1998         unsigned NWords = IV->getValue().getActiveWords();
1999         const uint64_t *RawWords = IV->getValue().getRawData();
2000         for (unsigned i = 0; i != NWords; ++i) {
2001           emitSignedInt64(Record, RawWords[i]);
2002         }
2003         Code = bitc::CST_CODE_WIDE_INTEGER;
2004       }
2005     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2006       Code = bitc::CST_CODE_FLOAT;
2007       Type *Ty = CFP->getType();
2008       if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
2009         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2010       } else if (Ty->isX86_FP80Ty()) {
2011         // api needed to prevent premature destruction
2012         // bits are not in the same order as a normal i80 APInt, compensate.
2013         APInt api = CFP->getValueAPF().bitcastToAPInt();
2014         const uint64_t *p = api.getRawData();
2015         Record.push_back((p[1] << 48) | (p[0] >> 16));
2016         Record.push_back(p[0] & 0xffffLL);
2017       } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2018         APInt api = CFP->getValueAPF().bitcastToAPInt();
2019         const uint64_t *p = api.getRawData();
2020         Record.push_back(p[0]);
2021         Record.push_back(p[1]);
2022       } else {
2023         assert(0 && "Unknown FP type!");
2024       }
2025     } else if (isa<ConstantDataSequential>(C) &&
2026                cast<ConstantDataSequential>(C)->isString()) {
2027       const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2028       // Emit constant strings specially.
2029       unsigned NumElts = Str->getNumElements();
2030       // If this is a null-terminated string, use the denser CSTRING encoding.
2031       if (Str->isCString()) {
2032         Code = bitc::CST_CODE_CSTRING;
2033         --NumElts; // Don't encode the null, which isn't allowed by char6.
2034       } else {
2035         Code = bitc::CST_CODE_STRING;
2036         AbbrevToUse = String8Abbrev;
2037       }
2038       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2039       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2040       for (unsigned i = 0; i != NumElts; ++i) {
2041         unsigned char V = Str->getElementAsInteger(i);
2042         Record.push_back(V);
2043         isCStr7 &= (V & 128) == 0;
2044         if (isCStrChar6)
2045           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2046       }
2047 
2048       if (isCStrChar6)
2049         AbbrevToUse = CString6Abbrev;
2050       else if (isCStr7)
2051         AbbrevToUse = CString7Abbrev;
2052     } else if (const ConstantDataSequential *CDS =
2053                    dyn_cast<ConstantDataSequential>(C)) {
2054       Code = bitc::CST_CODE_DATA;
2055       Type *EltTy = CDS->getElementType();
2056       if (isa<IntegerType>(EltTy)) {
2057         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2058           Record.push_back(CDS->getElementAsInteger(i));
2059       } else if (EltTy->isFloatTy()) {
2060         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2061           union {
2062             float F;
2063             uint32_t I;
2064           };
2065           F = CDS->getElementAsFloat(i);
2066           Record.push_back(I);
2067         }
2068       } else {
2069         assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
2070         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2071           union {
2072             double F;
2073             uint64_t I;
2074           };
2075           F = CDS->getElementAsDouble(i);
2076           Record.push_back(I);
2077         }
2078       }
2079     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
2080                isa<ConstantVector>(C)) {
2081       Code = bitc::CST_CODE_AGGREGATE;
2082       for (const Value *Op : C->operands())
2083         Record.push_back(VE.getValueID(Op));
2084       AbbrevToUse = AggregateAbbrev;
2085     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2086       switch (CE->getOpcode()) {
2087       default:
2088         if (Instruction::isCast(CE->getOpcode())) {
2089           Code = bitc::CST_CODE_CE_CAST;
2090           Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2091           Record.push_back(
2092               getTypeID(C->getOperand(0)->getType(), C->getOperand(0)));
2093           Record.push_back(VE.getValueID(C->getOperand(0)));
2094           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2095         } else {
2096           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2097           Code = bitc::CST_CODE_CE_BINOP;
2098           Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2099           Record.push_back(VE.getValueID(C->getOperand(0)));
2100           Record.push_back(VE.getValueID(C->getOperand(1)));
2101           uint64_t Flags = getOptimizationFlags(CE);
2102           if (Flags != 0)
2103             Record.push_back(Flags);
2104         }
2105         break;
2106       case Instruction::GetElementPtr: {
2107         Code = bitc::CST_CODE_CE_GEP;
2108         const auto *GO = cast<GEPOperator>(C);
2109         if (GO->isInBounds())
2110           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
2111         Record.push_back(getTypeID(GO->getSourceElementType()));
2112         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2113           Record.push_back(
2114               getTypeID(C->getOperand(i)->getType(), C->getOperand(i)));
2115           Record.push_back(VE.getValueID(C->getOperand(i)));
2116         }
2117         break;
2118       }
2119       case Instruction::Select:
2120         Code = bitc::CST_CODE_CE_SELECT;
2121         Record.push_back(VE.getValueID(C->getOperand(0)));
2122         Record.push_back(VE.getValueID(C->getOperand(1)));
2123         Record.push_back(VE.getValueID(C->getOperand(2)));
2124         break;
2125       case Instruction::ExtractElement:
2126         Code = bitc::CST_CODE_CE_EXTRACTELT;
2127         Record.push_back(getTypeID(C->getOperand(0)->getType()));
2128         Record.push_back(VE.getValueID(C->getOperand(0)));
2129         Record.push_back(getTypeID(C->getOperand(1)->getType()));
2130         Record.push_back(VE.getValueID(C->getOperand(1)));
2131         break;
2132       case Instruction::InsertElement:
2133         Code = bitc::CST_CODE_CE_INSERTELT;
2134         Record.push_back(VE.getValueID(C->getOperand(0)));
2135         Record.push_back(VE.getValueID(C->getOperand(1)));
2136         Record.push_back(getTypeID(C->getOperand(2)->getType()));
2137         Record.push_back(VE.getValueID(C->getOperand(2)));
2138         break;
2139       case Instruction::ShuffleVector:
2140         // If the return type and argument types are the same, this is a
2141         // standard shufflevector instruction.  If the types are different,
2142         // then the shuffle is widening or truncating the input vectors, and
2143         // the argument type must also be encoded.
2144         if (C->getType() == C->getOperand(0)->getType()) {
2145           Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2146         } else {
2147           Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2148           Record.push_back(getTypeID(C->getOperand(0)->getType()));
2149         }
2150         Record.push_back(VE.getValueID(C->getOperand(0)));
2151         Record.push_back(VE.getValueID(C->getOperand(1)));
2152         Record.push_back(VE.getValueID(C->getOperand(2)));
2153         break;
2154       }
2155     } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2156       Code = bitc::CST_CODE_BLOCKADDRESS;
2157       Record.push_back(getTypeID(BA->getFunction()->getType()));
2158       Record.push_back(VE.getValueID(BA->getFunction()));
2159       Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2160     } else {
2161 #ifndef NDEBUG
2162       C->dump();
2163 #endif
2164       llvm_unreachable("Unknown constant!");
2165     }
2166     Stream.EmitRecord(Code, Record, AbbrevToUse);
2167     Record.clear();
2168   }
2169 
2170   Stream.ExitBlock();
2171 }
2172 
2173 void DXILBitcodeWriter::writeModuleConstants() {
2174   const ValueEnumerator::ValueList &Vals = VE.getValues();
2175 
2176   // Find the first constant to emit, which is the first non-globalvalue value.
2177   // We know globalvalues have been emitted by WriteModuleInfo.
2178   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2179     if (!isa<GlobalValue>(Vals[i].first)) {
2180       writeConstants(i, Vals.size(), true);
2181       return;
2182     }
2183   }
2184 }
2185 
2186 /// pushValueAndType - The file has to encode both the value and type id for
2187 /// many values, because we need to know what type to create for forward
2188 /// references.  However, most operands are not forward references, so this type
2189 /// field is not needed.
2190 ///
2191 /// This function adds V's value ID to Vals.  If the value ID is higher than the
2192 /// instruction ID, then it is a forward reference, and it also includes the
2193 /// type ID.  The value ID that is written is encoded relative to the InstID.
2194 bool DXILBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2195                                          SmallVectorImpl<unsigned> &Vals) {
2196   unsigned ValID = VE.getValueID(V);
2197   // Make encoding relative to the InstID.
2198   Vals.push_back(InstID - ValID);
2199   if (ValID >= InstID) {
2200     Vals.push_back(getTypeID(V->getType(), V));
2201     return true;
2202   }
2203   return false;
2204 }
2205 
2206 /// pushValue - Like pushValueAndType, but where the type of the value is
2207 /// omitted (perhaps it was already encoded in an earlier operand).
2208 void DXILBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2209                                   SmallVectorImpl<unsigned> &Vals) {
2210   unsigned ValID = VE.getValueID(V);
2211   Vals.push_back(InstID - ValID);
2212 }
2213 
2214 void DXILBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2215                                         SmallVectorImpl<uint64_t> &Vals) {
2216   unsigned ValID = VE.getValueID(V);
2217   int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2218   emitSignedInt64(Vals, diff);
2219 }
2220 
2221 /// WriteInstruction - Emit an instruction
2222 void DXILBitcodeWriter::writeInstruction(const Instruction &I, unsigned InstID,
2223                                          SmallVectorImpl<unsigned> &Vals) {
2224   unsigned Code = 0;
2225   unsigned AbbrevToUse = 0;
2226   VE.setInstructionID(&I);
2227   switch (I.getOpcode()) {
2228   default:
2229     if (Instruction::isCast(I.getOpcode())) {
2230       Code = bitc::FUNC_CODE_INST_CAST;
2231       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2232         AbbrevToUse = (unsigned)FUNCTION_INST_CAST_ABBREV;
2233       Vals.push_back(getTypeID(I.getType(), &I));
2234       Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2235     } else {
2236       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2237       Code = bitc::FUNC_CODE_INST_BINOP;
2238       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2239         AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_ABBREV;
2240       pushValue(I.getOperand(1), InstID, Vals);
2241       Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2242       uint64_t Flags = getOptimizationFlags(&I);
2243       if (Flags != 0) {
2244         if (AbbrevToUse == (unsigned)FUNCTION_INST_BINOP_ABBREV)
2245           AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV;
2246         Vals.push_back(Flags);
2247       }
2248     }
2249     break;
2250 
2251   case Instruction::GetElementPtr: {
2252     Code = bitc::FUNC_CODE_INST_GEP;
2253     AbbrevToUse = (unsigned)FUNCTION_INST_GEP_ABBREV;
2254     auto &GEPInst = cast<GetElementPtrInst>(I);
2255     Vals.push_back(GEPInst.isInBounds());
2256     Vals.push_back(getTypeID(GEPInst.getSourceElementType()));
2257     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
2258       pushValueAndType(I.getOperand(i), InstID, Vals);
2259     break;
2260   }
2261   case Instruction::ExtractValue: {
2262     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
2263     pushValueAndType(I.getOperand(0), InstID, Vals);
2264     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
2265     Vals.append(EVI->idx_begin(), EVI->idx_end());
2266     break;
2267   }
2268   case Instruction::InsertValue: {
2269     Code = bitc::FUNC_CODE_INST_INSERTVAL;
2270     pushValueAndType(I.getOperand(0), InstID, Vals);
2271     pushValueAndType(I.getOperand(1), InstID, Vals);
2272     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
2273     Vals.append(IVI->idx_begin(), IVI->idx_end());
2274     break;
2275   }
2276   case Instruction::Select:
2277     Code = bitc::FUNC_CODE_INST_VSELECT;
2278     pushValueAndType(I.getOperand(1), InstID, Vals);
2279     pushValue(I.getOperand(2), InstID, Vals);
2280     pushValueAndType(I.getOperand(0), InstID, Vals);
2281     break;
2282   case Instruction::ExtractElement:
2283     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
2284     pushValueAndType(I.getOperand(0), InstID, Vals);
2285     pushValueAndType(I.getOperand(1), InstID, Vals);
2286     break;
2287   case Instruction::InsertElement:
2288     Code = bitc::FUNC_CODE_INST_INSERTELT;
2289     pushValueAndType(I.getOperand(0), InstID, Vals);
2290     pushValue(I.getOperand(1), InstID, Vals);
2291     pushValueAndType(I.getOperand(2), InstID, Vals);
2292     break;
2293   case Instruction::ShuffleVector:
2294     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
2295     pushValueAndType(I.getOperand(0), InstID, Vals);
2296     pushValue(I.getOperand(1), InstID, Vals);
2297     pushValue(cast<ShuffleVectorInst>(&I)->getShuffleMaskForBitcode(), InstID,
2298               Vals);
2299     break;
2300   case Instruction::ICmp:
2301   case Instruction::FCmp: {
2302     // compare returning Int1Ty or vector of Int1Ty
2303     Code = bitc::FUNC_CODE_INST_CMP2;
2304     pushValueAndType(I.getOperand(0), InstID, Vals);
2305     pushValue(I.getOperand(1), InstID, Vals);
2306     Vals.push_back(cast<CmpInst>(I).getPredicate());
2307     uint64_t Flags = getOptimizationFlags(&I);
2308     if (Flags != 0)
2309       Vals.push_back(Flags);
2310     break;
2311   }
2312 
2313   case Instruction::Ret: {
2314     Code = bitc::FUNC_CODE_INST_RET;
2315     unsigned NumOperands = I.getNumOperands();
2316     if (NumOperands == 0)
2317       AbbrevToUse = (unsigned)FUNCTION_INST_RET_VOID_ABBREV;
2318     else if (NumOperands == 1) {
2319       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2320         AbbrevToUse = (unsigned)FUNCTION_INST_RET_VAL_ABBREV;
2321     } else {
2322       for (unsigned i = 0, e = NumOperands; i != e; ++i)
2323         pushValueAndType(I.getOperand(i), InstID, Vals);
2324     }
2325   } break;
2326   case Instruction::Br: {
2327     Code = bitc::FUNC_CODE_INST_BR;
2328     const BranchInst &II = cast<BranchInst>(I);
2329     Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2330     if (II.isConditional()) {
2331       Vals.push_back(VE.getValueID(II.getSuccessor(1)));
2332       pushValue(II.getCondition(), InstID, Vals);
2333     }
2334   } break;
2335   case Instruction::Switch: {
2336     Code = bitc::FUNC_CODE_INST_SWITCH;
2337     const SwitchInst &SI = cast<SwitchInst>(I);
2338     Vals.push_back(getTypeID(SI.getCondition()->getType()));
2339     pushValue(SI.getCondition(), InstID, Vals);
2340     Vals.push_back(VE.getValueID(SI.getDefaultDest()));
2341     for (auto Case : SI.cases()) {
2342       Vals.push_back(VE.getValueID(Case.getCaseValue()));
2343       Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
2344     }
2345   } break;
2346   case Instruction::IndirectBr:
2347     Code = bitc::FUNC_CODE_INST_INDIRECTBR;
2348     Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2349     // Encode the address operand as relative, but not the basic blocks.
2350     pushValue(I.getOperand(0), InstID, Vals);
2351     for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
2352       Vals.push_back(VE.getValueID(I.getOperand(i)));
2353     break;
2354 
2355   case Instruction::Invoke: {
2356     const InvokeInst *II = cast<InvokeInst>(&I);
2357     const Value *Callee = II->getCalledOperand();
2358     FunctionType *FTy = II->getFunctionType();
2359     Code = bitc::FUNC_CODE_INST_INVOKE;
2360 
2361     Vals.push_back(VE.getAttributeListID(II->getAttributes()));
2362     Vals.push_back(II->getCallingConv() | 1 << 13);
2363     Vals.push_back(VE.getValueID(II->getNormalDest()));
2364     Vals.push_back(VE.getValueID(II->getUnwindDest()));
2365     Vals.push_back(getTypeID(FTy));
2366     pushValueAndType(Callee, InstID, Vals);
2367 
2368     // Emit value #'s for the fixed parameters.
2369     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2370       pushValue(I.getOperand(i), InstID, Vals); // fixed param.
2371 
2372     // Emit type/value pairs for varargs params.
2373     if (FTy->isVarArg()) {
2374       for (unsigned i = FTy->getNumParams(), e = I.getNumOperands() - 3; i != e;
2375            ++i)
2376         pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
2377     }
2378     break;
2379   }
2380   case Instruction::Resume:
2381     Code = bitc::FUNC_CODE_INST_RESUME;
2382     pushValueAndType(I.getOperand(0), InstID, Vals);
2383     break;
2384   case Instruction::Unreachable:
2385     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
2386     AbbrevToUse = (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV;
2387     break;
2388 
2389   case Instruction::PHI: {
2390     const PHINode &PN = cast<PHINode>(I);
2391     Code = bitc::FUNC_CODE_INST_PHI;
2392     // With the newer instruction encoding, forward references could give
2393     // negative valued IDs.  This is most common for PHIs, so we use
2394     // signed VBRs.
2395     SmallVector<uint64_t, 128> Vals64;
2396     Vals64.push_back(getTypeID(PN.getType()));
2397     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2398       pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
2399       Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2400     }
2401     // Emit a Vals64 vector and exit.
2402     Stream.EmitRecord(Code, Vals64, AbbrevToUse);
2403     Vals64.clear();
2404     return;
2405   }
2406 
2407   case Instruction::LandingPad: {
2408     const LandingPadInst &LP = cast<LandingPadInst>(I);
2409     Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2410     Vals.push_back(getTypeID(LP.getType()));
2411     Vals.push_back(LP.isCleanup());
2412     Vals.push_back(LP.getNumClauses());
2413     for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2414       if (LP.isCatch(I))
2415         Vals.push_back(LandingPadInst::Catch);
2416       else
2417         Vals.push_back(LandingPadInst::Filter);
2418       pushValueAndType(LP.getClause(I), InstID, Vals);
2419     }
2420     break;
2421   }
2422 
2423   case Instruction::Alloca: {
2424     Code = bitc::FUNC_CODE_INST_ALLOCA;
2425     const AllocaInst &AI = cast<AllocaInst>(I);
2426     Vals.push_back(getTypeID(AI.getAllocatedType()));
2427     Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2428     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
2429     unsigned AlignRecord = Log2_32(AI.getAlign().value()) + 1;
2430     assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2431     AlignRecord |= AI.isUsedWithInAlloca() << 5;
2432     AlignRecord |= 1 << 6;
2433     Vals.push_back(AlignRecord);
2434     break;
2435   }
2436 
2437   case Instruction::Load:
2438     if (cast<LoadInst>(I).isAtomic()) {
2439       Code = bitc::FUNC_CODE_INST_LOADATOMIC;
2440       pushValueAndType(I.getOperand(0), InstID, Vals);
2441     } else {
2442       Code = bitc::FUNC_CODE_INST_LOAD;
2443       if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
2444         AbbrevToUse = (unsigned)FUNCTION_INST_LOAD_ABBREV;
2445     }
2446     Vals.push_back(getTypeID(I.getType()));
2447     Vals.push_back(Log2(cast<LoadInst>(I).getAlign()) + 1);
2448     Vals.push_back(cast<LoadInst>(I).isVolatile());
2449     if (cast<LoadInst>(I).isAtomic()) {
2450       Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2451       Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
2452     }
2453     break;
2454   case Instruction::Store:
2455     if (cast<StoreInst>(I).isAtomic())
2456       Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2457     else
2458       Code = bitc::FUNC_CODE_INST_STORE;
2459     pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2460     pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
2461     Vals.push_back(Log2(cast<StoreInst>(I).getAlign()) + 1);
2462     Vals.push_back(cast<StoreInst>(I).isVolatile());
2463     if (cast<StoreInst>(I).isAtomic()) {
2464       Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2465       Vals.push_back(
2466           getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
2467     }
2468     break;
2469   case Instruction::AtomicCmpXchg:
2470     Code = bitc::FUNC_CODE_INST_CMPXCHG;
2471     pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2472     pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2473     pushValue(I.getOperand(2), InstID, Vals);        // newval.
2474     Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
2475     Vals.push_back(
2476         getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2477     Vals.push_back(
2478         getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
2479     Vals.push_back(
2480         getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
2481     Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
2482     break;
2483   case Instruction::AtomicRMW:
2484     Code = bitc::FUNC_CODE_INST_ATOMICRMW;
2485     pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2486     pushValue(I.getOperand(1), InstID, Vals);        // val.
2487     Vals.push_back(
2488         getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
2489     Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
2490     Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2491     Vals.push_back(
2492         getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
2493     break;
2494   case Instruction::Fence:
2495     Code = bitc::FUNC_CODE_INST_FENCE;
2496     Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2497     Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
2498     break;
2499   case Instruction::Call: {
2500     const CallInst &CI = cast<CallInst>(I);
2501     FunctionType *FTy = CI.getFunctionType();
2502 
2503     Code = bitc::FUNC_CODE_INST_CALL;
2504 
2505     Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
2506     Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
2507                    unsigned(CI.isMustTailCall()) << 14 | 1 << 15);
2508     Vals.push_back(getGlobalObjectValueTypeID(FTy, CI.getCalledFunction()));
2509     pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
2510 
2511     // Emit value #'s for the fixed parameters.
2512     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2513       // Check for labels (can happen with asm labels).
2514       if (FTy->getParamType(i)->isLabelTy())
2515         Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2516       else
2517         pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
2518     }
2519 
2520     // Emit type/value pairs for varargs params.
2521     if (FTy->isVarArg()) {
2522       for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
2523         pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
2524     }
2525     break;
2526   }
2527   case Instruction::VAArg:
2528     Code = bitc::FUNC_CODE_INST_VAARG;
2529     Vals.push_back(getTypeID(I.getOperand(0)->getType())); // valistty
2530     pushValue(I.getOperand(0), InstID, Vals);              // valist.
2531     Vals.push_back(getTypeID(I.getType()));                // restype.
2532     break;
2533   }
2534 
2535   Stream.EmitRecord(Code, Vals, AbbrevToUse);
2536   Vals.clear();
2537 }
2538 
2539 // Emit names for globals/functions etc.
2540 void DXILBitcodeWriter::writeFunctionLevelValueSymbolTable(
2541     const ValueSymbolTable &VST) {
2542   if (VST.empty())
2543     return;
2544   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2545 
2546   SmallVector<unsigned, 64> NameVals;
2547 
2548   // HLSL Change
2549   // Read the named values from a sorted list instead of the original list
2550   // to ensure the binary is the same no matter what values ever existed.
2551   SmallVector<const ValueName *, 16> SortedTable;
2552 
2553   for (auto &VI : VST) {
2554     SortedTable.push_back(VI.second->getValueName());
2555   }
2556   // The keys are unique, so there shouldn't be stability issues.
2557   llvm::sort(SortedTable, [](const ValueName *A, const ValueName *B) {
2558     return A->first() < B->first();
2559   });
2560 
2561   for (const ValueName *SI : SortedTable) {
2562     auto &Name = *SI;
2563 
2564     // Figure out the encoding to use for the name.
2565     bool is7Bit = true;
2566     bool isChar6 = true;
2567     for (const char *C = Name.getKeyData(), *E = C + Name.getKeyLength();
2568          C != E; ++C) {
2569       if (isChar6)
2570         isChar6 = BitCodeAbbrevOp::isChar6(*C);
2571       if ((unsigned char)*C & 128) {
2572         is7Bit = false;
2573         break; // don't bother scanning the rest.
2574       }
2575     }
2576 
2577     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
2578 
2579     // VST_ENTRY:   [valueid, namechar x N]
2580     // VST_BBENTRY: [bbid, namechar x N]
2581     unsigned Code;
2582     if (isa<BasicBlock>(SI->getValue())) {
2583       Code = bitc::VST_CODE_BBENTRY;
2584       if (isChar6)
2585         AbbrevToUse = VST_BBENTRY_6_ABBREV;
2586     } else {
2587       Code = bitc::VST_CODE_ENTRY;
2588       if (isChar6)
2589         AbbrevToUse = VST_ENTRY_6_ABBREV;
2590       else if (is7Bit)
2591         AbbrevToUse = VST_ENTRY_7_ABBREV;
2592     }
2593 
2594     NameVals.push_back(VE.getValueID(SI->getValue()));
2595     for (const char *P = Name.getKeyData(),
2596                     *E = Name.getKeyData() + Name.getKeyLength();
2597          P != E; ++P)
2598       NameVals.push_back((unsigned char)*P);
2599 
2600     // Emit the finished record.
2601     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
2602     NameVals.clear();
2603   }
2604   Stream.ExitBlock();
2605 }
2606 
2607 /// Emit a function body to the module stream.
2608 void DXILBitcodeWriter::writeFunction(const Function &F) {
2609   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
2610   VE.incorporateFunction(F);
2611 
2612   SmallVector<unsigned, 64> Vals;
2613 
2614   // Emit the number of basic blocks, so the reader can create them ahead of
2615   // time.
2616   Vals.push_back(VE.getBasicBlocks().size());
2617   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
2618   Vals.clear();
2619 
2620   // If there are function-local constants, emit them now.
2621   unsigned CstStart, CstEnd;
2622   VE.getFunctionConstantRange(CstStart, CstEnd);
2623   writeConstants(CstStart, CstEnd, false);
2624 
2625   // If there is function-local metadata, emit it now.
2626   writeFunctionMetadata(F);
2627 
2628   // Keep a running idea of what the instruction ID is.
2629   unsigned InstID = CstEnd;
2630 
2631   bool NeedsMetadataAttachment = F.hasMetadata();
2632 
2633   DILocation *LastDL = nullptr;
2634 
2635   // Finally, emit all the instructions, in order.
2636   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2637     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
2638          ++I) {
2639       writeInstruction(*I, InstID, Vals);
2640 
2641       if (!I->getType()->isVoidTy())
2642         ++InstID;
2643 
2644       // If the instruction has metadata, write a metadata attachment later.
2645       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
2646 
2647       // If the instruction has a debug location, emit it.
2648       DILocation *DL = I->getDebugLoc();
2649       if (!DL)
2650         continue;
2651 
2652       if (DL == LastDL) {
2653         // Just repeat the same debug loc as last time.
2654         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
2655         continue;
2656       }
2657 
2658       Vals.push_back(DL->getLine());
2659       Vals.push_back(DL->getColumn());
2660       Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2661       Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
2662       Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
2663       Vals.clear();
2664 
2665       LastDL = DL;
2666     }
2667 
2668   // Emit names for all the instructions etc.
2669   if (auto *Symtab = F.getValueSymbolTable())
2670     writeFunctionLevelValueSymbolTable(*Symtab);
2671 
2672   if (NeedsMetadataAttachment)
2673     writeFunctionMetadataAttachment(F);
2674 
2675   VE.purgeFunction();
2676   Stream.ExitBlock();
2677 }
2678 
2679 // Emit blockinfo, which defines the standard abbreviations etc.
2680 void DXILBitcodeWriter::writeBlockInfo() {
2681   // We only want to emit block info records for blocks that have multiple
2682   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
2683   // Other blocks can define their abbrevs inline.
2684   Stream.EnterBlockInfoBlock();
2685 
2686   { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
2687     auto Abbv = std::make_shared<BitCodeAbbrev>();
2688     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2689     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2690     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2691     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2692     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2693                                    std::move(Abbv)) != VST_ENTRY_8_ABBREV)
2694       assert(false && "Unexpected abbrev ordering!");
2695   }
2696 
2697   { // 7-bit fixed width VST_ENTRY strings.
2698     auto Abbv = std::make_shared<BitCodeAbbrev>();
2699     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2700     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2701     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2702     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2703     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2704                                    std::move(Abbv)) != VST_ENTRY_7_ABBREV)
2705       assert(false && "Unexpected abbrev ordering!");
2706   }
2707   { // 6-bit char6 VST_ENTRY strings.
2708     auto Abbv = std::make_shared<BitCodeAbbrev>();
2709     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2710     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2711     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2712     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2713     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2714                                    std::move(Abbv)) != VST_ENTRY_6_ABBREV)
2715       assert(false && "Unexpected abbrev ordering!");
2716   }
2717   { // 6-bit char6 VST_BBENTRY strings.
2718     auto Abbv = std::make_shared<BitCodeAbbrev>();
2719     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2720     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2721     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2722     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2723     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2724                                    std::move(Abbv)) != VST_BBENTRY_6_ABBREV)
2725       assert(false && "Unexpected abbrev ordering!");
2726   }
2727 
2728   { // SETTYPE abbrev for CONSTANTS_BLOCK.
2729     auto Abbv = std::make_shared<BitCodeAbbrev>();
2730     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2731     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2732                               VE.computeBitsRequiredForTypeIndices()));
2733     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2734         CONSTANTS_SETTYPE_ABBREV)
2735       assert(false && "Unexpected abbrev ordering!");
2736   }
2737 
2738   { // INTEGER abbrev for CONSTANTS_BLOCK.
2739     auto Abbv = std::make_shared<BitCodeAbbrev>();
2740     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2741     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2742     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2743         CONSTANTS_INTEGER_ABBREV)
2744       assert(false && "Unexpected abbrev ordering!");
2745   }
2746 
2747   { // CE_CAST abbrev for CONSTANTS_BLOCK.
2748     auto Abbv = std::make_shared<BitCodeAbbrev>();
2749     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2750     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2751     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,      // typeid
2752                               VE.computeBitsRequiredForTypeIndices()));
2753     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2754 
2755     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2756         CONSTANTS_CE_CAST_Abbrev)
2757       assert(false && "Unexpected abbrev ordering!");
2758   }
2759   { // NULL abbrev for CONSTANTS_BLOCK.
2760     auto Abbv = std::make_shared<BitCodeAbbrev>();
2761     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
2762     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2763         CONSTANTS_NULL_Abbrev)
2764       assert(false && "Unexpected abbrev ordering!");
2765   }
2766 
2767   // FIXME: This should only use space for first class types!
2768 
2769   { // INST_LOAD abbrev for FUNCTION_BLOCK.
2770     auto Abbv = std::make_shared<BitCodeAbbrev>();
2771     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
2772     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
2773     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,    // dest ty
2774                               VE.computeBitsRequiredForTypeIndices()));
2775     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // Align
2776     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
2777     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2778         (unsigned)FUNCTION_INST_LOAD_ABBREV)
2779       assert(false && "Unexpected abbrev ordering!");
2780   }
2781   { // INST_BINOP abbrev for FUNCTION_BLOCK.
2782     auto Abbv = std::make_shared<BitCodeAbbrev>();
2783     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2784     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LHS
2785     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // RHS
2786     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2787     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2788         (unsigned)FUNCTION_INST_BINOP_ABBREV)
2789       assert(false && "Unexpected abbrev ordering!");
2790   }
2791   { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2792     auto Abbv = std::make_shared<BitCodeAbbrev>();
2793     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2794     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LHS
2795     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // RHS
2796     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2797     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
2798     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2799         (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV)
2800       assert(false && "Unexpected abbrev ordering!");
2801   }
2802   { // INST_CAST abbrev for FUNCTION_BLOCK.
2803     auto Abbv = std::make_shared<BitCodeAbbrev>();
2804     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2805     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2806     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,    // dest ty
2807                               VE.computeBitsRequiredForTypeIndices()));
2808     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2809     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2810         (unsigned)FUNCTION_INST_CAST_ABBREV)
2811       assert(false && "Unexpected abbrev ordering!");
2812   }
2813 
2814   { // INST_RET abbrev for FUNCTION_BLOCK.
2815     auto Abbv = std::make_shared<BitCodeAbbrev>();
2816     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2817     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2818         (unsigned)FUNCTION_INST_RET_VOID_ABBREV)
2819       assert(false && "Unexpected abbrev ordering!");
2820   }
2821   { // INST_RET abbrev for FUNCTION_BLOCK.
2822     auto Abbv = std::make_shared<BitCodeAbbrev>();
2823     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2824     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
2825     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2826         (unsigned)FUNCTION_INST_RET_VAL_ABBREV)
2827       assert(false && "Unexpected abbrev ordering!");
2828   }
2829   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2830     auto Abbv = std::make_shared<BitCodeAbbrev>();
2831     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
2832     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2833         (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV)
2834       assert(false && "Unexpected abbrev ordering!");
2835   }
2836   {
2837     auto Abbv = std::make_shared<BitCodeAbbrev>();
2838     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2839     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2840     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2841                               Log2_32_Ceil(VE.getTypes().size() + 1)));
2842     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2843     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2844     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2845         (unsigned)FUNCTION_INST_GEP_ABBREV)
2846       assert(false && "Unexpected abbrev ordering!");
2847   }
2848 
2849   Stream.ExitBlock();
2850 }
2851 
2852 void DXILBitcodeWriter::writeModuleVersion() {
2853   // VERSION: [version#]
2854   Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<unsigned>{1});
2855 }
2856 
2857 /// WriteModule - Emit the specified module to the bitstream.
2858 void DXILBitcodeWriter::write() {
2859   // The identification block is new since llvm-3.7, but the old bitcode reader
2860   // will skip it.
2861   // writeIdentificationBlock(Stream);
2862 
2863   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
2864 
2865   // It is redundant to fully-specify this here, but nice to make it explicit
2866   // so that it is clear the DXIL module version is different.
2867   DXILBitcodeWriter::writeModuleVersion();
2868 
2869   // Emit blockinfo, which defines the standard abbreviations etc.
2870   writeBlockInfo();
2871 
2872   // Emit information about attribute groups.
2873   writeAttributeGroupTable();
2874 
2875   // Emit information about parameter attributes.
2876   writeAttributeTable();
2877 
2878   // Emit information describing all of the types in the module.
2879   writeTypeTable();
2880 
2881   writeComdats();
2882 
2883   // Emit top-level description of module, including target triple, inline asm,
2884   // descriptors for global variables, and function prototype info.
2885   writeModuleInfo();
2886 
2887   // Emit constants.
2888   writeModuleConstants();
2889 
2890   // Emit metadata.
2891   writeModuleMetadataKinds();
2892 
2893   // Emit metadata.
2894   writeModuleMetadata();
2895 
2896   // Emit names for globals/functions etc.
2897   // DXIL uses the same format for module-level value symbol table as for the
2898   // function level table.
2899   writeFunctionLevelValueSymbolTable(M.getValueSymbolTable());
2900 
2901   // Emit function bodies.
2902   for (const Function &F : M)
2903     if (!F.isDeclaration())
2904       writeFunction(F);
2905 
2906   Stream.ExitBlock();
2907 }
2908