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