blob: 06a4ef1892de349e8a72a0fcd8ed2e9a393f37d8 [file] [log] [blame]
Chris Lattner87351e22007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerc1d10d62007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "ValueEnumerator.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000015#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000017#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner362b4a12007-04-23 01:01:37 +000018#include "llvm/Bitcode/LLVMBitCodes.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000019#include "llvm/Bitcode/ReaderWriter.h"
Sanjoy Dasb513a9f2015-09-24 23:34:52 +000020#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000022#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/InlineAsm.h"
25#include "llvm/IR/Instructions.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000026#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
28#include "llvm/IR/Operator.h"
Duncan P. N. Exon Smith6b6fdc92014-07-25 14:49:26 +000029#include "llvm/IR/UseListOrder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000032#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000033#include "llvm/Support/Program.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000034#include "llvm/Support/SHA1.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000035#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000036#include <cctype>
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000037#include <map>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000038using namespace llvm;
39
Benjamin Kramera65b6102016-05-15 15:18:11 +000040namespace {
Chris Lattner4d925982007-05-04 20:52:02 +000041/// These are manifest constants used by the bitcode writer. They do not need to
42/// be kept in sync with the reader, but need to be consistent within this file.
43enum {
Chris Lattner4d925982007-05-04 20:52:02 +000044 // VALUE_SYMTAB_BLOCK abbrev id's.
45 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerfd1ad102007-05-04 20:58:35 +000046 VST_ENTRY_7_ABBREV,
Chris Lattnere760d6f2007-05-05 01:26:50 +000047 VST_ENTRY_6_ABBREV,
Chris Lattnerda5e5d22007-05-05 07:36:14 +000048 VST_BBENTRY_6_ABBREV,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000049
Chris Lattnerda5e5d22007-05-05 07:36:14 +000050 // CONSTANTS_BLOCK abbrev id's.
51 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
52 CONSTANTS_INTEGER_ABBREV,
53 CONSTANTS_CE_CAST_Abbrev,
Chris Lattnerb80751d2007-05-05 07:44:49 +000054 CONSTANTS_NULL_Abbrev,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000055
Chris Lattnerb80751d2007-05-05 07:44:49 +000056 // FUNCTION_BLOCK abbrev id's.
Chris Lattnercc6d4c92007-05-06 01:28:01 +000057 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000058 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman0ebd6962009-07-20 21:19:07 +000059 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000060 FUNCTION_INST_CAST_ABBREV,
Chris Lattnercc6d4c92007-05-06 01:28:01 +000061 FUNCTION_INST_RET_VOID_ABBREV,
62 FUNCTION_INST_RET_VAL_ABBREV,
David Blaikieb5b5efd2015-02-25 01:08:52 +000063 FUNCTION_INST_UNREACHABLE_ABBREV,
64 FUNCTION_INST_GEP_ABBREV,
Chris Lattner4d925982007-05-04 20:52:02 +000065};
66
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000067/// Abstract class to manage the bitcode writing, subclassed for each bitcode
68/// file type. Owns the BitstreamWriter, and includes the main entry point for
Teresa Johnson37687f32016-04-23 04:30:47 +000069/// writing.
70class BitcodeWriter {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000071protected:
Teresa Johnson37687f32016-04-23 04:30:47 +000072 /// Pointer to the buffer allocated by caller for bitcode writing.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000073 const SmallVectorImpl<char> &Buffer;
Teresa Johnson37687f32016-04-23 04:30:47 +000074
75 /// The stream created and owned by the BitodeWriter.
76 BitstreamWriter Stream;
77
78 /// Saves the offset of the VSTOffset record that must eventually be
79 /// backpatched with the offset of the actual VST.
80 uint64_t VSTOffsetPlaceholder = 0;
81
82public:
83 /// Constructs a BitcodeWriter object, and initializes a BitstreamRecord,
84 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000085 BitcodeWriter(SmallVectorImpl<char> &Buffer)
86 : Buffer(Buffer), Stream(Buffer) {}
Teresa Johnson37687f32016-04-23 04:30:47 +000087
88 virtual ~BitcodeWriter() = default;
89
90 /// Main entry point to write the bitcode file, which writes the bitcode
91 /// header and will then invoke the virtual writeBlocks() method.
92 void write();
93
94private:
95 /// Derived classes must implement this to write the corresponding blocks for
96 /// that bitcode file type.
97 virtual void writeBlocks() = 0;
98
99protected:
100 bool hasVSTOffsetPlaceholder() { return VSTOffsetPlaceholder != 0; }
Teresa Johnson37687f32016-04-23 04:30:47 +0000101 void writeValueSymbolTableForwardDecl();
102 void writeBitcodeHeader();
103};
104
105/// Class to manage the bitcode writing for a module.
106class ModuleBitcodeWriter : public BitcodeWriter {
107 /// The Module to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000108 const Module &M;
Teresa Johnson37687f32016-04-23 04:30:47 +0000109
110 /// Enumerates ids for all values in the module.
111 ValueEnumerator VE;
112
113 /// Optional per-module index to write for ThinLTO.
114 const ModuleSummaryIndex *Index;
115
116 /// True if a module hash record should be written.
117 bool GenerateHash;
118
119 /// The start bit of the module block, for use in generating a module hash
120 uint64_t BitcodeStartBit = 0;
121
122public:
123 /// Constructs a ModuleBitcodeWriter object for the given Module,
124 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000125 ModuleBitcodeWriter(const Module *M, SmallVectorImpl<char> &Buffer,
Teresa Johnson37687f32016-04-23 04:30:47 +0000126 bool ShouldPreserveUseListOrder,
127 const ModuleSummaryIndex *Index, bool GenerateHash)
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000128 : BitcodeWriter(Buffer), M(*M), VE(*M, ShouldPreserveUseListOrder),
Teresa Johnson37687f32016-04-23 04:30:47 +0000129 Index(Index), GenerateHash(GenerateHash) {
130 // Save the start bit of the actual bitcode, in case there is space
131 // saved at the start for the darwin header above. The reader stream
132 // will start at the bitcode, and we need the offset of the VST
133 // to line up.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000134 BitcodeStartBit = Stream.GetCurrentBitNo();
Teresa Johnson37687f32016-04-23 04:30:47 +0000135 }
136
137private:
138 /// Main entry point for writing a module to bitcode, invoked by
139 /// BitcodeWriter::write() after it writes the header.
140 void writeBlocks() override;
141
142 /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
143 /// current llvm version, and a record for the epoch number.
144 void writeIdentificationBlock();
145
146 /// Emit the current module to the bitstream.
147 void writeModule();
148
149 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
150
151 void writeStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse);
152 void writeAttributeGroupTable();
153 void writeAttributeTable();
154 void writeTypeTable();
155 void writeComdats();
156 void writeModuleInfo();
157 void writeValueAsMetadata(const ValueAsMetadata *MD,
158 SmallVectorImpl<uint64_t> &Record);
159 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
160 unsigned Abbrev);
161 unsigned createDILocationAbbrev();
162 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
163 unsigned &Abbrev);
164 unsigned createGenericDINodeAbbrev();
165 void writeGenericDINode(const GenericDINode *N,
166 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
167 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
168 unsigned Abbrev);
169 void writeDIEnumerator(const DIEnumerator *N,
170 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
171 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
172 unsigned Abbrev);
173 void writeDIDerivedType(const DIDerivedType *N,
174 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
175 void writeDICompositeType(const DICompositeType *N,
176 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
177 void writeDISubroutineType(const DISubroutineType *N,
178 SmallVectorImpl<uint64_t> &Record,
179 unsigned Abbrev);
180 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
181 unsigned Abbrev);
182 void writeDICompileUnit(const DICompileUnit *N,
183 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
184 void writeDISubprogram(const DISubprogram *N,
185 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
186 void writeDILexicalBlock(const DILexicalBlock *N,
187 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
188 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
189 SmallVectorImpl<uint64_t> &Record,
190 unsigned Abbrev);
191 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
192 unsigned Abbrev);
193 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
194 unsigned Abbrev);
195 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
196 unsigned Abbrev);
197 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
198 unsigned Abbrev);
199 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
200 SmallVectorImpl<uint64_t> &Record,
201 unsigned Abbrev);
202 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
203 SmallVectorImpl<uint64_t> &Record,
204 unsigned Abbrev);
205 void writeDIGlobalVariable(const DIGlobalVariable *N,
206 SmallVectorImpl<uint64_t> &Record,
207 unsigned Abbrev);
208 void writeDILocalVariable(const DILocalVariable *N,
209 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
210 void writeDIExpression(const DIExpression *N,
211 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
212 void writeDIObjCProperty(const DIObjCProperty *N,
213 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
214 void writeDIImportedEntity(const DIImportedEntity *N,
215 SmallVectorImpl<uint64_t> &Record,
216 unsigned Abbrev);
217 unsigned createNamedMetadataAbbrev();
218 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
219 unsigned createMetadataStringsAbbrev();
220 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
221 SmallVectorImpl<uint64_t> &Record);
222 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
223 SmallVectorImpl<uint64_t> &Record);
224 void writeModuleMetadata();
225 void writeFunctionMetadata(const Function &F);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000226 void writeFunctionMetadataAttachment(const Function &F);
227 void writeGlobalVariableMetadataAttachment(const GlobalVariable &GV);
228 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
229 const GlobalObject &GO);
Teresa Johnson37687f32016-04-23 04:30:47 +0000230 void writeModuleMetadataStore();
231 void writeOperandBundleTags();
232 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
233 void writeModuleConstants();
234 bool pushValueAndType(const Value *V, unsigned InstID,
235 SmallVectorImpl<unsigned> &Vals);
236 void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
237 void pushValue(const Value *V, unsigned InstID,
238 SmallVectorImpl<unsigned> &Vals);
239 void pushValueSigned(const Value *V, unsigned InstID,
240 SmallVectorImpl<uint64_t> &Vals);
241 void writeInstruction(const Instruction &I, unsigned InstID,
242 SmallVectorImpl<unsigned> &Vals);
243 void writeValueSymbolTable(
244 const ValueSymbolTable &VST, bool IsModuleLevel = false,
245 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex = nullptr);
246 void writeUseList(UseListOrder &&Order);
247 void writeUseListBlock(const Function *F);
248 void
249 writeFunction(const Function &F,
250 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
251 void writeBlockInfo();
252 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
Teresa Johnson28e457b2016-04-24 14:57:11 +0000253 GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +0000254 unsigned ValueID,
255 unsigned FSCallsAbbrev,
256 unsigned FSCallsProfileAbbrev,
257 const Function &F);
258 void writeModuleLevelReferences(const GlobalVariable &V,
259 SmallVector<uint64_t, 64> &NameVals,
260 unsigned FSModRefsAbbrev);
261 void writePerModuleGlobalValueSummary();
262 void writeModuleHash(size_t BlockStartPos);
263};
264
265/// Class to manage the bitcode writing for a combined index.
266class IndexBitcodeWriter : public BitcodeWriter {
267 /// The combined index to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000268 const ModuleSummaryIndex &Index;
Teresa Johnson37687f32016-04-23 04:30:47 +0000269
Teresa Johnson84174c32016-05-10 13:48:23 +0000270 /// When writing a subset of the index for distributed backends, client
271 /// provides a map of modules to the corresponding GUIDs/summaries to write.
272 std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
273
Teresa Johnson37687f32016-04-23 04:30:47 +0000274 /// Map that holds the correspondence between the GUID used in the combined
275 /// index and a value id generated by this class to use in references.
276 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
277
278 /// Tracks the last value id recorded in the GUIDToValueMap.
279 unsigned GlobalValueId = 0;
280
281public:
282 /// Constructs a IndexBitcodeWriter object for the given combined index,
Teresa Johnson84174c32016-05-10 13:48:23 +0000283 /// writing to the provided \p Buffer. When writing a subset of the index
284 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000285 IndexBitcodeWriter(SmallVectorImpl<char> &Buffer,
Teresa Johnson84174c32016-05-10 13:48:23 +0000286 const ModuleSummaryIndex &Index,
287 std::map<std::string, GVSummaryMapTy>
288 *ModuleToSummariesForIndex = nullptr)
289 : BitcodeWriter(Buffer), Index(Index),
290 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
291 // Assign unique value ids to all summaries to be written, for use
Teresa Johnson37687f32016-04-23 04:30:47 +0000292 // in writing out the call graph edges. Save the mapping from GUID
293 // to the new global value id to use when writing those edges, which
294 // are currently saved in the index in terms of GUID.
Teresa Johnson84174c32016-05-10 13:48:23 +0000295 for (const auto &I : *this)
296 GUIDToValueIdMap[I.first] = ++GlobalValueId;
Teresa Johnson37687f32016-04-23 04:30:47 +0000297 }
298
Teresa Johnson84174c32016-05-10 13:48:23 +0000299 /// The below iterator returns the GUID and associated summary.
300 typedef std::pair<GlobalValue::GUID, GlobalValueSummary *> GVInfo;
301
302 /// Iterator over the value GUID and summaries to be written to bitcode,
303 /// hides the details of whether they are being pulled from the entire
304 /// index or just those in a provided ModuleToSummariesForIndex map.
305 class iterator
306 : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
307 GVInfo> {
308 /// Enables access to parent class.
309 const IndexBitcodeWriter &Writer;
310
311 // Iterators used when writing only those summaries in a provided
312 // ModuleToSummariesForIndex map:
313
314 /// Points to the last element in outer ModuleToSummariesForIndex map.
315 std::map<std::string, GVSummaryMapTy>::iterator ModuleSummariesBack;
316 /// Iterator on outer ModuleToSummariesForIndex map.
317 std::map<std::string, GVSummaryMapTy>::iterator ModuleSummariesIter;
318 /// Iterator on an inner global variable summary map.
319 GVSummaryMapTy::iterator ModuleGVSummariesIter;
320
321 // Iterators used when writing all summaries in the index:
322
323 /// Points to the last element in the Index outer GlobalValueMap.
324 const_gvsummary_iterator IndexSummariesBack;
325 /// Iterator on outer GlobalValueMap.
326 const_gvsummary_iterator IndexSummariesIter;
327 /// Iterator on an inner GlobalValueSummaryList.
328 GlobalValueSummaryList::const_iterator IndexGVSummariesIter;
329
330 public:
331 /// Construct iterator from parent \p Writer and indicate if we are
332 /// constructing the end iterator.
333 iterator(const IndexBitcodeWriter &Writer, bool IsAtEnd) : Writer(Writer) {
334 // Set up the appropriate set of iterators given whether we are writing
335 // the full index or just a subset.
336 // Can't setup the Back or inner iterators if the corresponding map
337 // is empty. This will be handled specially in operator== as well.
338 if (Writer.ModuleToSummariesForIndex &&
339 !Writer.ModuleToSummariesForIndex->empty()) {
Teresa Johnson84174c32016-05-10 13:48:23 +0000340 for (ModuleSummariesBack = Writer.ModuleToSummariesForIndex->begin();
341 std::next(ModuleSummariesBack) !=
342 Writer.ModuleToSummariesForIndex->end();
343 ModuleSummariesBack++)
344 ;
Teresa Johnsone518c802016-05-11 20:46:22 +0000345 ModuleSummariesIter = !IsAtEnd
346 ? Writer.ModuleToSummariesForIndex->begin()
347 : ModuleSummariesBack;
Teresa Johnson84174c32016-05-10 13:48:23 +0000348 ModuleGVSummariesIter = !IsAtEnd ? ModuleSummariesIter->second.begin()
349 : ModuleSummariesBack->second.end();
350 } else if (!Writer.ModuleToSummariesForIndex &&
351 Writer.Index.begin() != Writer.Index.end()) {
Teresa Johnson84174c32016-05-10 13:48:23 +0000352 for (IndexSummariesBack = Writer.Index.begin();
353 std::next(IndexSummariesBack) != Writer.Index.end();
354 IndexSummariesBack++)
355 ;
Teresa Johnsone518c802016-05-11 20:46:22 +0000356 IndexSummariesIter =
357 !IsAtEnd ? Writer.Index.begin() : IndexSummariesBack;
Teresa Johnson84174c32016-05-10 13:48:23 +0000358 IndexGVSummariesIter = !IsAtEnd ? IndexSummariesIter->second.begin()
359 : IndexSummariesBack->second.end();
360 }
361 }
362
363 /// Increment the appropriate set of iterators.
364 iterator &operator++() {
365 // First the inner iterator is incremented, then if it is at the end
366 // and there are more outer iterations to go, the inner is reset to
367 // the start of the next inner list.
368 if (Writer.ModuleToSummariesForIndex) {
369 ++ModuleGVSummariesIter;
370 if (ModuleGVSummariesIter == ModuleSummariesIter->second.end() &&
371 ModuleSummariesIter != ModuleSummariesBack) {
372 ++ModuleSummariesIter;
373 ModuleGVSummariesIter = ModuleSummariesIter->second.begin();
374 }
375 } else {
376 ++IndexGVSummariesIter;
377 if (IndexGVSummariesIter == IndexSummariesIter->second.end() &&
378 IndexSummariesIter != IndexSummariesBack) {
379 ++IndexSummariesIter;
380 IndexGVSummariesIter = IndexSummariesIter->second.begin();
381 }
382 }
383 return *this;
384 }
385
386 /// Access the <GUID,GlobalValueSummary*> pair corresponding to the current
387 /// outer and inner iterator positions.
388 GVInfo operator*() {
389 if (Writer.ModuleToSummariesForIndex)
390 return std::make_pair(ModuleGVSummariesIter->first,
391 ModuleGVSummariesIter->second);
392 return std::make_pair(IndexSummariesIter->first,
393 IndexGVSummariesIter->get());
394 }
395
396 /// Checks if the iterators are equal, with special handling for empty
397 /// indexes.
398 bool operator==(const iterator &RHS) const {
399 if (Writer.ModuleToSummariesForIndex) {
400 // First ensure that both are writing the same subset.
401 if (Writer.ModuleToSummariesForIndex !=
402 RHS.Writer.ModuleToSummariesForIndex)
403 return false;
404 // Already determined above that maps are the same, so if one is
405 // empty, they both are.
406 if (Writer.ModuleToSummariesForIndex->empty())
407 return true;
Teresa Johnsone518c802016-05-11 20:46:22 +0000408 // Ensure the ModuleGVSummariesIter are iterating over the same
409 // container before checking them below.
410 if (ModuleSummariesIter != RHS.ModuleSummariesIter)
411 return false;
Teresa Johnson84174c32016-05-10 13:48:23 +0000412 return ModuleGVSummariesIter == RHS.ModuleGVSummariesIter;
413 }
414 // First ensure RHS also writing the full index, and that both are
415 // writing the same full index.
416 if (RHS.Writer.ModuleToSummariesForIndex ||
417 &Writer.Index != &RHS.Writer.Index)
418 return false;
419 // Already determined above that maps are the same, so if one is
420 // empty, they both are.
421 if (Writer.Index.begin() == Writer.Index.end())
422 return true;
Teresa Johnsone518c802016-05-11 20:46:22 +0000423 // Ensure the IndexGVSummariesIter are iterating over the same
424 // container before checking them below.
425 if (IndexSummariesIter != RHS.IndexSummariesIter)
426 return false;
Teresa Johnson84174c32016-05-10 13:48:23 +0000427 return IndexGVSummariesIter == RHS.IndexGVSummariesIter;
428 }
429 };
430
431 /// Obtain the start iterator over the summaries to be written.
432 iterator begin() { return iterator(*this, /*IsAtEnd=*/false); }
433 /// Obtain the end iterator over the summaries to be written.
434 iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
435
Teresa Johnson37687f32016-04-23 04:30:47 +0000436private:
437 /// Main entry point for writing a combined index to bitcode, invoked by
438 /// BitcodeWriter::write() after it writes the header.
439 void writeBlocks() override;
440
441 void writeIndex();
442 void writeModStrings();
443 void writeCombinedValueSymbolTable();
444 void writeCombinedGlobalValueSummary();
445
Teresa Johnson84174c32016-05-10 13:48:23 +0000446 /// Indicates whether the provided \p ModulePath should be written into
447 /// the module string table, e.g. if full index written or if it is in
448 /// the provided subset.
449 bool doIncludeModule(StringRef ModulePath) {
450 return !ModuleToSummariesForIndex ||
451 ModuleToSummariesForIndex->count(ModulePath);
452 }
453
Teresa Johnson37687f32016-04-23 04:30:47 +0000454 bool hasValueId(GlobalValue::GUID ValGUID) {
455 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
456 return VMI != GUIDToValueIdMap.end();
457 }
458 unsigned getValueId(GlobalValue::GUID ValGUID) {
459 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
460 // If this GUID doesn't have an entry, assign one.
461 if (VMI == GUIDToValueIdMap.end()) {
462 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
463 return GlobalValueId;
464 } else {
465 return VMI->second;
466 }
467 }
Teresa Johnson37687f32016-04-23 04:30:47 +0000468 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
469};
Benjamin Kramera65b6102016-05-15 15:18:11 +0000470} // end anonymous namespace
Teresa Johnson37687f32016-04-23 04:30:47 +0000471
472static unsigned getEncodedCastOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000473 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000474 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000475 case Instruction::Trunc : return bitc::CAST_TRUNC;
476 case Instruction::ZExt : return bitc::CAST_ZEXT;
477 case Instruction::SExt : return bitc::CAST_SEXT;
478 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
479 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
480 case Instruction::UIToFP : return bitc::CAST_UITOFP;
481 case Instruction::SIToFP : return bitc::CAST_SITOFP;
482 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
483 case Instruction::FPExt : return bitc::CAST_FPEXT;
484 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
485 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
486 case Instruction::BitCast : return bitc::CAST_BITCAST;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000487 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000488 }
489}
490
Teresa Johnson37687f32016-04-23 04:30:47 +0000491static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000492 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000493 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmana5b96452009-06-04 22:49:04 +0000494 case Instruction::Add:
495 case Instruction::FAdd: return bitc::BINOP_ADD;
496 case Instruction::Sub:
497 case Instruction::FSub: return bitc::BINOP_SUB;
498 case Instruction::Mul:
499 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000500 case Instruction::UDiv: return bitc::BINOP_UDIV;
501 case Instruction::FDiv:
502 case Instruction::SDiv: return bitc::BINOP_SDIV;
503 case Instruction::URem: return bitc::BINOP_UREM;
504 case Instruction::FRem:
505 case Instruction::SRem: return bitc::BINOP_SREM;
506 case Instruction::Shl: return bitc::BINOP_SHL;
507 case Instruction::LShr: return bitc::BINOP_LSHR;
508 case Instruction::AShr: return bitc::BINOP_ASHR;
509 case Instruction::And: return bitc::BINOP_AND;
510 case Instruction::Or: return bitc::BINOP_OR;
511 case Instruction::Xor: return bitc::BINOP_XOR;
512 }
513}
514
Teresa Johnson37687f32016-04-23 04:30:47 +0000515static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000516 switch (Op) {
517 default: llvm_unreachable("Unknown RMW operation!");
518 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
519 case AtomicRMWInst::Add: return bitc::RMW_ADD;
520 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
521 case AtomicRMWInst::And: return bitc::RMW_AND;
522 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
523 case AtomicRMWInst::Or: return bitc::RMW_OR;
524 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
525 case AtomicRMWInst::Max: return bitc::RMW_MAX;
526 case AtomicRMWInst::Min: return bitc::RMW_MIN;
527 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
528 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
529 }
530}
531
Teresa Johnson37687f32016-04-23 04:30:47 +0000532static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000533 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +0000534 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
535 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
536 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
537 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
538 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
539 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
540 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
Eli Friedmanfee02c62011-07-25 23:16:38 +0000541 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000542 llvm_unreachable("Invalid ordering");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000543}
544
Teresa Johnson37687f32016-04-23 04:30:47 +0000545static unsigned getEncodedSynchScope(SynchronizationScope SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000546 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000547 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
548 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
549 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000550 llvm_unreachable("Invalid synch scope");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000551}
552
Teresa Johnson37687f32016-04-23 04:30:47 +0000553void ModuleBitcodeWriter::writeStringRecord(unsigned Code, StringRef Str,
554 unsigned AbbrevToUse) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000555 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000556
Chris Lattnere14cb882007-05-04 19:11:41 +0000557 // Code: [strchar x N]
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000558 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
559 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
560 AbbrevToUse = 0;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000561 Vals.push_back(Str[i]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000562 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000563
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000564 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000565 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000566}
567
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000568static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
569 switch (Kind) {
570 case Attribute::Alignment:
571 return bitc::ATTR_KIND_ALIGNMENT;
George Burgess IV278199f2016-04-12 01:05:35 +0000572 case Attribute::AllocSize:
573 return bitc::ATTR_KIND_ALLOC_SIZE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000574 case Attribute::AlwaysInline:
575 return bitc::ATTR_KIND_ALWAYS_INLINE;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000576 case Attribute::ArgMemOnly:
577 return bitc::ATTR_KIND_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000578 case Attribute::Builtin:
579 return bitc::ATTR_KIND_BUILTIN;
580 case Attribute::ByVal:
581 return bitc::ATTR_KIND_BY_VAL;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000582 case Attribute::Convergent:
583 return bitc::ATTR_KIND_CONVERGENT;
Reid Klecknera534a382013-12-19 02:14:12 +0000584 case Attribute::InAlloca:
585 return bitc::ATTR_KIND_IN_ALLOCA;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000586 case Attribute::Cold:
587 return bitc::ATTR_KIND_COLD;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000588 case Attribute::InaccessibleMemOnly:
589 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
590 case Attribute::InaccessibleMemOrArgMemOnly:
591 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000592 case Attribute::InlineHint:
593 return bitc::ATTR_KIND_INLINE_HINT;
594 case Attribute::InReg:
595 return bitc::ATTR_KIND_IN_REG;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000596 case Attribute::JumpTable:
597 return bitc::ATTR_KIND_JUMP_TABLE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000598 case Attribute::MinSize:
599 return bitc::ATTR_KIND_MIN_SIZE;
600 case Attribute::Naked:
601 return bitc::ATTR_KIND_NAKED;
602 case Attribute::Nest:
603 return bitc::ATTR_KIND_NEST;
604 case Attribute::NoAlias:
605 return bitc::ATTR_KIND_NO_ALIAS;
606 case Attribute::NoBuiltin:
607 return bitc::ATTR_KIND_NO_BUILTIN;
608 case Attribute::NoCapture:
609 return bitc::ATTR_KIND_NO_CAPTURE;
610 case Attribute::NoDuplicate:
611 return bitc::ATTR_KIND_NO_DUPLICATE;
612 case Attribute::NoImplicitFloat:
613 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
614 case Attribute::NoInline:
615 return bitc::ATTR_KIND_NO_INLINE;
James Molloye6f87ca2015-11-06 10:32:53 +0000616 case Attribute::NoRecurse:
617 return bitc::ATTR_KIND_NO_RECURSE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000618 case Attribute::NonLazyBind:
619 return bitc::ATTR_KIND_NON_LAZY_BIND;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000620 case Attribute::NonNull:
621 return bitc::ATTR_KIND_NON_NULL;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000622 case Attribute::Dereferenceable:
623 return bitc::ATTR_KIND_DEREFERENCEABLE;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000624 case Attribute::DereferenceableOrNull:
625 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000626 case Attribute::NoRedZone:
627 return bitc::ATTR_KIND_NO_RED_ZONE;
628 case Attribute::NoReturn:
629 return bitc::ATTR_KIND_NO_RETURN;
630 case Attribute::NoUnwind:
631 return bitc::ATTR_KIND_NO_UNWIND;
632 case Attribute::OptimizeForSize:
633 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000634 case Attribute::OptimizeNone:
635 return bitc::ATTR_KIND_OPTIMIZE_NONE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000636 case Attribute::ReadNone:
637 return bitc::ATTR_KIND_READ_NONE;
638 case Attribute::ReadOnly:
639 return bitc::ATTR_KIND_READ_ONLY;
640 case Attribute::Returned:
641 return bitc::ATTR_KIND_RETURNED;
642 case Attribute::ReturnsTwice:
643 return bitc::ATTR_KIND_RETURNS_TWICE;
644 case Attribute::SExt:
645 return bitc::ATTR_KIND_S_EXT;
646 case Attribute::StackAlignment:
647 return bitc::ATTR_KIND_STACK_ALIGNMENT;
648 case Attribute::StackProtect:
649 return bitc::ATTR_KIND_STACK_PROTECT;
650 case Attribute::StackProtectReq:
651 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
652 case Attribute::StackProtectStrong:
653 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000654 case Attribute::SafeStack:
655 return bitc::ATTR_KIND_SAFESTACK;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000656 case Attribute::StructRet:
657 return bitc::ATTR_KIND_STRUCT_RET;
658 case Attribute::SanitizeAddress:
659 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
660 case Attribute::SanitizeThread:
661 return bitc::ATTR_KIND_SANITIZE_THREAD;
662 case Attribute::SanitizeMemory:
663 return bitc::ATTR_KIND_SANITIZE_MEMORY;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000664 case Attribute::SwiftError:
665 return bitc::ATTR_KIND_SWIFT_ERROR;
Manman Renf46262e2016-03-29 17:37:21 +0000666 case Attribute::SwiftSelf:
667 return bitc::ATTR_KIND_SWIFT_SELF;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000668 case Attribute::UWTable:
669 return bitc::ATTR_KIND_UW_TABLE;
670 case Attribute::ZExt:
671 return bitc::ATTR_KIND_Z_EXT;
672 case Attribute::EndAttrKinds:
673 llvm_unreachable("Can not encode end-attribute kinds marker.");
674 case Attribute::None:
675 llvm_unreachable("Can not encode none-attribute.");
676 }
677
678 llvm_unreachable("Trying to encode unknown attribute");
679}
680
Teresa Johnson37687f32016-04-23 04:30:47 +0000681void ModuleBitcodeWriter::writeAttributeGroupTable() {
Bill Wendling92ed7002013-02-11 22:33:26 +0000682 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
683 if (AttrGrps.empty()) return;
Bill Wendlingdc095552013-02-10 23:09:32 +0000684
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000685 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
Bill Wendlingdc095552013-02-10 23:09:32 +0000686
687 SmallVector<uint64_t, 64> Record;
Bill Wendling92ed7002013-02-11 22:33:26 +0000688 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
689 AttributeSet AS = AttrGrps[i];
Bill Wendlingdc095552013-02-10 23:09:32 +0000690 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
691 AttributeSet A = AS.getSlotAttributes(i);
692
Bill Wendling92ed7002013-02-11 22:33:26 +0000693 Record.push_back(VE.getAttributeGroupID(A));
Bill Wendlingdc095552013-02-10 23:09:32 +0000694 Record.push_back(AS.getSlotIndex(i));
695
696 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
697 I != E; ++I) {
698 Attribute Attr = *I;
699 if (Attr.isEnumAttribute()) {
700 Record.push_back(0);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000701 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Hal Finkele15442c2014-07-18 06:51:55 +0000702 } else if (Attr.isIntAttribute()) {
Bill Wendlingdc095552013-02-10 23:09:32 +0000703 Record.push_back(1);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000704 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Bill Wendlingdc095552013-02-10 23:09:32 +0000705 Record.push_back(Attr.getValueAsInt());
706 } else {
707 StringRef Kind = Attr.getKindAsString();
708 StringRef Val = Attr.getValueAsString();
709
710 Record.push_back(Val.empty() ? 3 : 4);
711 Record.append(Kind.begin(), Kind.end());
712 Record.push_back(0);
713 if (!Val.empty()) {
714 Record.append(Val.begin(), Val.end());
715 Record.push_back(0);
716 }
717 }
718 }
719
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000720 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
Bill Wendlingdc095552013-02-10 23:09:32 +0000721 Record.clear();
722 }
723 }
724
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000725 Stream.ExitBlock();
Bill Wendlingdc095552013-02-10 23:09:32 +0000726}
727
Teresa Johnson37687f32016-04-23 04:30:47 +0000728void ModuleBitcodeWriter::writeAttributeTable() {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000729 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000730 if (Attrs.empty()) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000731
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000732 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000733
734 SmallVector<uint64_t, 64> Record;
735 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000736 const AttributeSet &A = Attrs[i];
Bill Wendling0dc08912013-02-12 08:13:50 +0000737 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
738 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000739
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000740 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000741 Record.clear();
742 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000743
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000744 Stream.ExitBlock();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000745}
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000746
747/// WriteTypeTable - Write out the type table for a module.
Teresa Johnson37687f32016-04-23 04:30:47 +0000748void ModuleBitcodeWriter::writeTypeTable() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000749 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000750
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000751 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000752 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000753
David Blaikie7b028102015-02-25 00:51:52 +0000754 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
Chad Rosier9646c0d2011-12-08 00:38:45 +0000755
Chris Lattnerccee7062007-05-05 06:30:12 +0000756 // Abbrev for TYPE_CODE_POINTER.
757 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
758 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000759 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
Christopher Lamb25f50762007-12-12 08:44:39 +0000760 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000761 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000762
Chris Lattnerccee7062007-05-05 06:30:12 +0000763 // Abbrev for TYPE_CODE_FUNCTION.
764 Abbv = new BitCodeAbbrev();
765 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
766 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnerccee7062007-05-05 06:30:12 +0000767 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000768 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
769
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000770 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000771
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000772 // Abbrev for TYPE_CODE_STRUCT_ANON.
Chris Lattnerccee7062007-05-05 06:30:12 +0000773 Abbv = new BitCodeAbbrev();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000774 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
Chris Lattnerccee7062007-05-05 06:30:12 +0000775 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
776 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000777 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
778
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000779 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000780
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000781 // Abbrev for TYPE_CODE_STRUCT_NAME.
782 Abbv = new BitCodeAbbrev();
783 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
784 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000786 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000787
788 // Abbrev for TYPE_CODE_STRUCT_NAMED.
789 Abbv = new BitCodeAbbrev();
790 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
791 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
792 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000793 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
794
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000795 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000796
Chris Lattnerccee7062007-05-05 06:30:12 +0000797 // Abbrev for TYPE_CODE_ARRAY.
798 Abbv = new BitCodeAbbrev();
799 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
Chad Rosier9646c0d2011-12-08 00:38:45 +0000801 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
802
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000803 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000804
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000805 // Emit an entry count so the reader can reserve space.
806 TypeVals.push_back(TypeList.size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000807 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000808 TypeVals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000809
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000810 // Loop over all of the types, emitting each in turn.
811 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000812 Type *T = TypeList[i];
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000813 int AbbrevToUse = 0;
814 unsigned Code = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000815
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000816 switch (T->getTypeID()) {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000817 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
818 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
819 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
820 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
821 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
822 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000823 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000824 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
825 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
826 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
David Majnemerb611e3f2015-08-14 05:09:07 +0000827 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000828 case Type::IntegerTyID:
829 // INTEGER: [width]
830 Code = bitc::TYPE_CODE_INTEGER;
831 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
832 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000833 case Type::PointerTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000834 PointerType *PTy = cast<PointerType>(T);
Christopher Lamb25f50762007-12-12 08:44:39 +0000835 // POINTER: [pointee type, address space]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000836 Code = bitc::TYPE_CODE_POINTER;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000837 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lamb25f50762007-12-12 08:44:39 +0000838 unsigned AddressSpace = PTy->getAddressSpace();
839 TypeVals.push_back(AddressSpace);
840 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000841 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000842 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000843 case Type::FunctionTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000844 FunctionType *FT = cast<FunctionType>(T);
Chad Rosier95898722011-11-03 00:14:01 +0000845 // FUNCTION: [isvararg, retty, paramty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000846 Code = bitc::TYPE_CODE_FUNCTION;
847 TypeVals.push_back(FT->isVarArg());
848 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000849 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
850 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerccee7062007-05-05 06:30:12 +0000851 AbbrevToUse = FunctionAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000852 break;
853 }
854 case Type::StructTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000855 StructType *ST = cast<StructType>(T);
Chris Lattnerccee7062007-05-05 06:30:12 +0000856 // STRUCT: [ispacked, eltty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000857 TypeVals.push_back(ST->isPacked());
Chris Lattnere14cb882007-05-04 19:11:41 +0000858 // Output all of the element types.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000859 for (StructType::element_iterator I = ST->element_begin(),
860 E = ST->element_end(); I != E; ++I)
861 TypeVals.push_back(VE.getTypeID(*I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000862
Chris Lattner335d3992011-08-12 18:06:37 +0000863 if (ST->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000864 Code = bitc::TYPE_CODE_STRUCT_ANON;
865 AbbrevToUse = StructAnonAbbrev;
866 } else {
867 if (ST->isOpaque()) {
868 Code = bitc::TYPE_CODE_OPAQUE;
869 } else {
870 Code = bitc::TYPE_CODE_STRUCT_NAMED;
871 AbbrevToUse = StructNamedAbbrev;
872 }
873
874 // Emit the name if it is present.
875 if (!ST->getName().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000876 writeStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
877 StructNameAbbrev);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000878 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000879 break;
880 }
881 case Type::ArrayTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000882 ArrayType *AT = cast<ArrayType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000883 // ARRAY: [numelts, eltty]
884 Code = bitc::TYPE_CODE_ARRAY;
885 TypeVals.push_back(AT->getNumElements());
886 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerccee7062007-05-05 06:30:12 +0000887 AbbrevToUse = ArrayAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000888 break;
889 }
890 case Type::VectorTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000891 VectorType *VT = cast<VectorType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000892 // VECTOR [numelts, eltty]
893 Code = bitc::TYPE_CODE_VECTOR;
894 TypeVals.push_back(VT->getNumElements());
895 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
896 break;
897 }
898 }
899
900 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000901 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000902 TypeVals.clear();
903 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000904
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000905 Stream.ExitBlock();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000906}
907
Teresa Johnson5e22e442016-02-06 16:07:35 +0000908static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
909 switch (Linkage) {
Rafael Espindola261d25b2015-01-08 16:25:01 +0000910 case GlobalValue::ExternalLinkage:
911 return 0;
912 case GlobalValue::WeakAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000913 return 16;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000914 case GlobalValue::AppendingLinkage:
915 return 2;
916 case GlobalValue::InternalLinkage:
917 return 3;
918 case GlobalValue::LinkOnceAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000919 return 18;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000920 case GlobalValue::ExternalWeakLinkage:
921 return 7;
922 case GlobalValue::CommonLinkage:
923 return 8;
924 case GlobalValue::PrivateLinkage:
925 return 9;
926 case GlobalValue::WeakODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000927 return 17;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000928 case GlobalValue::LinkOnceODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000929 return 19;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000930 case GlobalValue::AvailableExternallyLinkage:
931 return 12;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000932 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000933 llvm_unreachable("Invalid linkage");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000934}
935
Teresa Johnson5e22e442016-02-06 16:07:35 +0000936static unsigned getEncodedLinkage(const GlobalValue &GV) {
937 return getEncodedLinkage(GV.getLinkage());
938}
939
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000940// Decode the flags for GlobalValue in the summary
941static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
942 uint64_t RawFlags = 0;
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000943
944 RawFlags |= Flags.HasSection; // bool
945
946 // Linkage don't need to be remapped at that time for the summary. Any future
947 // change to the getEncodedLinkage() function will need to be taken into
948 // account here as well.
949 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
950
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000951 return RawFlags;
952}
953
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000954static unsigned getEncodedVisibility(const GlobalValue &GV) {
955 switch (GV.getVisibility()) {
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000956 case GlobalValue::DefaultVisibility: return 0;
957 case GlobalValue::HiddenVisibility: return 1;
958 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000959 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000960 llvm_unreachable("Invalid visibility");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000961}
962
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000963static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
964 switch (GV.getDLLStorageClass()) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000965 case GlobalValue::DefaultStorageClass: return 0;
966 case GlobalValue::DLLImportStorageClass: return 1;
967 case GlobalValue::DLLExportStorageClass: return 2;
968 }
969 llvm_unreachable("Invalid DLL storage class");
970}
971
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000972static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000973 switch (GV.getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000974 case GlobalVariable::NotThreadLocal: return 0;
975 case GlobalVariable::GeneralDynamicTLSModel: return 1;
976 case GlobalVariable::LocalDynamicTLSModel: return 2;
977 case GlobalVariable::InitialExecTLSModel: return 3;
978 case GlobalVariable::LocalExecTLSModel: return 4;
979 }
980 llvm_unreachable("Invalid TLS model");
981}
982
David Majnemerdad0a642014-06-27 18:19:56 +0000983static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
984 switch (C.getSelectionKind()) {
985 case Comdat::Any:
986 return bitc::COMDAT_SELECTION_KIND_ANY;
987 case Comdat::ExactMatch:
988 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
989 case Comdat::Largest:
990 return bitc::COMDAT_SELECTION_KIND_LARGEST;
991 case Comdat::NoDuplicates:
992 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
993 case Comdat::SameSize:
994 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
995 }
996 llvm_unreachable("Invalid selection kind");
997}
998
Teresa Johnson37687f32016-04-23 04:30:47 +0000999void ModuleBitcodeWriter::writeComdats() {
David Majnemer90a021f2016-03-13 08:01:03 +00001000 SmallVector<unsigned, 64> Vals;
David Majnemerdad0a642014-06-27 18:19:56 +00001001 for (const Comdat *C : VE.getComdats()) {
1002 // COMDAT: [selection_kind, name]
1003 Vals.push_back(getEncodedComdatSelectionKind(*C));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +00001004 size_t Size = C->getName().size();
David Majnemer90a021f2016-03-13 08:01:03 +00001005 assert(isUInt<32>(Size));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +00001006 Vals.push_back(Size);
David Majnemerdad0a642014-06-27 18:19:56 +00001007 for (char Chr : C->getName())
1008 Vals.push_back((unsigned char)Chr);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001009 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
David Majnemerdad0a642014-06-27 18:19:56 +00001010 Vals.clear();
1011 }
1012}
1013
Teresa Johnsonff642b92015-09-17 20:12:00 +00001014/// Write a record that will eventually hold the word offset of the
1015/// module-level VST. For now the offset is 0, which will be backpatched
Teresa Johnson37687f32016-04-23 04:30:47 +00001016/// after the real VST is written. Saves the bit offset to backpatch.
1017void BitcodeWriter::writeValueSymbolTableForwardDecl() {
Teresa Johnsonff642b92015-09-17 20:12:00 +00001018 // Write a placeholder value in for the offset of the real VST,
1019 // which is written after the function blocks so that it can include
1020 // the offset of each function. The placeholder offset will be
1021 // updated when the real VST is written.
1022 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1023 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
1024 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1025 // hold the real VST offset. Must use fixed instead of VBR as we don't
1026 // know how many VBR chunks to reserve ahead of time.
1027 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001028 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001029
1030 // Emit the placeholder
1031 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001032 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001033
Teresa Johnson37687f32016-04-23 04:30:47 +00001034 // Compute and save the bit offset to the placeholder, which will be
Teresa Johnsonff642b92015-09-17 20:12:00 +00001035 // patched when the real VST is written. We can simply subtract the 32-bit
1036 // fixed size from the current bit number to get the location to backpatch.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001037 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
Teresa Johnsonff642b92015-09-17 20:12:00 +00001038}
1039
Teresa Johnsone1164de2016-02-10 21:55:02 +00001040enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
1041
1042/// Determine the encoding to use for the given string name and length.
1043static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
1044 bool isChar6 = true;
1045 for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
1046 if (isChar6)
1047 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1048 if ((unsigned char)*C & 128)
1049 // don't bother scanning the rest.
1050 return SE_Fixed8;
1051 }
1052 if (isChar6)
1053 return SE_Char6;
1054 else
1055 return SE_Fixed7;
1056}
1057
Teresa Johnsonff642b92015-09-17 20:12:00 +00001058/// Emit top-level description of module, including target triple, inline asm,
1059/// descriptors for global variables, and function prototype info.
1060/// Returns the bit offset to backpatch with the location of the real VST.
Teresa Johnson37687f32016-04-23 04:30:47 +00001061void ModuleBitcodeWriter::writeModuleInfo() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001062 // Emit various pieces of data attached to a module.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001063 if (!M.getTargetTriple().empty())
1064 writeStringRecord(bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001065 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001066 const std::string &DL = M.getDataLayoutStr();
Rafael Espindolaf863ee22014-02-25 20:01:08 +00001067 if (!DL.empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001068 writeStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001069 if (!M.getModuleInlineAsm().empty())
1070 writeStringRecord(bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001071 0 /*TODO*/);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001072
Gordon Henriksend930f912008-08-17 18:44:35 +00001073 // Emit information about sections and GC, computing how many there are. Also
1074 // compute the maximum alignment value.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001075 std::map<std::string, unsigned> SectionMap;
Gordon Henriksend930f912008-08-17 18:44:35 +00001076 std::map<std::string, unsigned> GCMap;
Chris Lattner4b00d922007-04-23 16:04:05 +00001077 unsigned MaxAlignment = 0;
Chris Lattnerb5491372007-04-23 18:58:34 +00001078 unsigned MaxGlobalType = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001079 for (const GlobalValue &GV : M.globals()) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001080 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
David Blaikie1a848da2015-04-27 19:58:56 +00001081 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001082 if (GV.hasSection()) {
Chad Rosier75ec09c2011-08-12 16:45:18 +00001083 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001084 unsigned &Entry = SectionMap[GV.getSection()];
Chad Rosier75ec09c2011-08-12 16:45:18 +00001085 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001086 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1087 0 /*TODO*/);
Chad Rosier75ec09c2011-08-12 16:45:18 +00001088 Entry = SectionMap.size();
1089 }
1090 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001091 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001092 for (const Function &F : M) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001093 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
1094 if (F.hasSection()) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001095 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001096 unsigned &Entry = SectionMap[F.getSection()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001097 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001098 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1099 0 /*TODO*/);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001100 Entry = SectionMap.size();
1101 }
1102 }
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001103 if (F.hasGC()) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001104 // Same for GC names.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001105 unsigned &Entry = GCMap[F.getGC()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001106 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001107 writeStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 0 /*TODO*/);
Gordon Henriksend930f912008-08-17 18:44:35 +00001108 Entry = GCMap.size();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001109 }
1110 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001111 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001112
Chris Lattner4b00d922007-04-23 16:04:05 +00001113 // Emit abbrev for globals, now that we know # sections and max alignment.
1114 unsigned SimpleGVarAbbrev = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001115 if (!M.global_empty()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001116 // Add an abbrev for common globals with no visibility or thread localness.
1117 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1118 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2eae59f2007-05-04 20:34:50 +00001119 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001120 Log2_32_Ceil(MaxGlobalType+1)));
David Blaikie1a848da2015-04-27 19:58:56 +00001121 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1122 //| explicitType << 1
1123 //| constant
1124 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1126 if (MaxAlignment == 0) // Alignment.
Chris Lattner4b00d922007-04-23 16:04:05 +00001127 Abbv->Add(BitCodeAbbrevOp(0));
1128 else {
1129 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2eae59f2007-05-04 20:34:50 +00001130 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001131 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001132 }
1133 if (SectionMap.empty()) // Section.
1134 Abbv->Add(BitCodeAbbrevOp(0));
1135 else
Chris Lattner2eae59f2007-05-04 20:34:50 +00001136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner1e50c292007-04-24 03:29:47 +00001137 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001138 // Don't bother emitting vis + thread local.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001139 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner4b00d922007-04-23 16:04:05 +00001140 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001141
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001142 // Emit the global variable information.
1143 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001144 for (const GlobalVariable &GV : M.globals()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001145 unsigned AbbrevToUse = 0;
1146
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001147 // GLOBALVAR: [type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001148 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +00001149 // unnamed_addr, externally_initialized, dllstorageclass,
1150 // comdat]
David Blaikie1a848da2015-04-27 19:58:56 +00001151 Vals.push_back(VE.getTypeID(GV.getValueType()));
1152 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001153 Vals.push_back(GV.isDeclaration() ? 0 :
1154 (VE.getValueID(GV.getInitializer()) + 1));
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001155 Vals.push_back(getEncodedLinkage(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001156 Vals.push_back(Log2_32(GV.getAlignment())+1);
1157 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
1158 if (GV.isThreadLocal() ||
1159 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1160 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
David Majnemerdad0a642014-06-27 18:19:56 +00001161 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1162 GV.hasComdat()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001163 Vals.push_back(getEncodedVisibility(GV));
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001164 Vals.push_back(getEncodedThreadLocalMode(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001165 Vals.push_back(GV.hasUnnamedAddr());
1166 Vals.push_back(GV.isExternallyInitialized());
Nico Rieck7157bb72014-01-14 15:22:47 +00001167 Vals.push_back(getEncodedDLLStorageClass(GV));
David Majnemerdad0a642014-06-27 18:19:56 +00001168 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
Chris Lattner4b00d922007-04-23 16:04:05 +00001169 } else {
1170 AbbrevToUse = SimpleGVarAbbrev;
1171 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001172
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001173 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001174 Vals.clear();
1175 }
1176
1177 // Emit the function proto information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001178 for (const Function &F : M) {
Chad Rosier42ee1522011-12-07 23:57:55 +00001179 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001180 // section, visibility, gc, unnamed_addr, prologuedata,
David Majnemer7fddecc2015-06-17 20:52:32 +00001181 // dllstorageclass, comdat, prefixdata, personalityfn]
David Blaikie561a1572015-04-17 16:28:26 +00001182 Vals.push_back(VE.getTypeID(F.getFunctionType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001183 Vals.push_back(F.getCallingConv());
1184 Vals.push_back(F.isDeclaration());
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001185 Vals.push_back(getEncodedLinkage(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001186 Vals.push_back(VE.getAttributeID(F.getAttributes()));
1187 Vals.push_back(Log2_32(F.getAlignment())+1);
1188 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001189 Vals.push_back(getEncodedVisibility(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001190 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1191 Vals.push_back(F.hasUnnamedAddr());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001192 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1193 : 0);
Nico Rieck7157bb72014-01-14 15:22:47 +00001194 Vals.push_back(getEncodedDLLStorageClass(F));
David Majnemerdad0a642014-06-27 18:19:56 +00001195 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001196 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1197 : 0);
David Majnemer7fddecc2015-06-17 20:52:32 +00001198 Vals.push_back(
1199 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001200
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001201 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001202 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001203 Vals.clear();
1204 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001205
Chris Lattner44c17072007-04-26 02:46:40 +00001206 // Emit the alias information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001207 for (const GlobalAlias &A : M.aliases()) {
Chad Rosiera966c312011-12-08 00:11:31 +00001208 // ALIAS: [alias type, aliasee val#, linkage, visibility]
David Blaikie6a51dbd2015-09-17 22:18:59 +00001209 Vals.push_back(VE.getTypeID(A.getValueType()));
1210 Vals.push_back(A.getType()->getAddressSpace());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001211 Vals.push_back(VE.getValueID(A.getAliasee()));
1212 Vals.push_back(getEncodedLinkage(A));
1213 Vals.push_back(getEncodedVisibility(A));
1214 Vals.push_back(getEncodedDLLStorageClass(A));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001215 Vals.push_back(getEncodedThreadLocalMode(A));
1216 Vals.push_back(A.hasUnnamedAddr());
Chris Lattner44c17072007-04-26 02:46:40 +00001217 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001218 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
Chris Lattner44c17072007-04-26 02:46:40 +00001219 Vals.clear();
1220 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001221
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001222 // Emit the ifunc information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001223 for (const GlobalIFunc &I : M.ifuncs()) {
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001224 // IFUNC: [ifunc type, address space, resolver val#, linkage, visibility]
1225 Vals.push_back(VE.getTypeID(I.getValueType()));
1226 Vals.push_back(I.getType()->getAddressSpace());
1227 Vals.push_back(VE.getValueID(I.getResolver()));
1228 Vals.push_back(getEncodedLinkage(I));
1229 Vals.push_back(getEncodedVisibility(I));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001230 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001231 Vals.clear();
1232 }
1233
Teresa Johnsone1164de2016-02-10 21:55:02 +00001234 // Emit the module's source file name.
1235 {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001236 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1237 M.getSourceFileName().size());
Teresa Johnsone1164de2016-02-10 21:55:02 +00001238 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1239 if (Bits == SE_Char6)
1240 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1241 else if (Bits == SE_Fixed7)
1242 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1243
1244 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1245 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1246 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1247 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1248 Abbv->Add(AbbrevOpToUse);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001249 unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001250
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001251 for (const auto P : M.getSourceFileName())
Teresa Johnsone1164de2016-02-10 21:55:02 +00001252 Vals.push_back((unsigned char)P);
1253
1254 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001255 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001256 Vals.clear();
1257 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001258
Teresa Johnson37687f32016-04-23 04:30:47 +00001259 // If we have a VST, write the VSTOFFSET record placeholder.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001260 if (M.getValueSymbolTable().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001261 return;
1262 writeValueSymbolTableForwardDecl();
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001263}
1264
Teresa Johnson37687f32016-04-23 04:30:47 +00001265static uint64_t getOptimizationFlags(const Value *V) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001266 uint64_t Flags = 0;
1267
Sanjay Patelc00017d2014-10-15 17:45:13 +00001268 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001269 if (OBO->hasNoSignedWrap())
1270 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1271 if (OBO->hasNoUnsignedWrap())
1272 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001273 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
Chris Lattner35315d02011-02-06 21:44:57 +00001274 if (PEO->isExact())
1275 Flags |= 1 << bitc::PEO_EXACT;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001276 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001277 if (FPMO->hasUnsafeAlgebra())
Michael Ilseman65f14352012-12-09 21:12:04 +00001278 Flags |= FastMathFlags::UnsafeAlgebra;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001279 if (FPMO->hasNoNaNs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001280 Flags |= FastMathFlags::NoNaNs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001281 if (FPMO->hasNoInfs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001282 Flags |= FastMathFlags::NoInfs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001283 if (FPMO->hasNoSignedZeros())
Michael Ilseman65f14352012-12-09 21:12:04 +00001284 Flags |= FastMathFlags::NoSignedZeros;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001285 if (FPMO->hasAllowReciprocal())
Michael Ilseman65f14352012-12-09 21:12:04 +00001286 Flags |= FastMathFlags::AllowReciprocal;
Dan Gohman0ebd6962009-07-20 21:19:07 +00001287 }
1288
1289 return Flags;
1290}
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001291
Teresa Johnson37687f32016-04-23 04:30:47 +00001292void ModuleBitcodeWriter::writeValueAsMetadata(
1293 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001294 // Mimic an MDNode with a value as one operand.
1295 Value *V = MD->getValue();
1296 Record.push_back(VE.getTypeID(V->getType()));
1297 Record.push_back(VE.getValueID(V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001298 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001299 Record.clear();
1300}
1301
Teresa Johnson37687f32016-04-23 04:30:47 +00001302void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1303 SmallVectorImpl<uint64_t> &Record,
1304 unsigned Abbrev) {
Chris Lattner9b493022009-12-31 01:22:29 +00001305 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001306 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001307 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1308 "Unexpected function-local metadata");
1309 Record.push_back(VE.getMetadataOrNullID(MD));
Devang Patel8cca7b42009-08-04 05:01:35 +00001310 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001311 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1312 : bitc::METADATA_NODE,
1313 Record, Abbrev);
Devang Patel8cca7b42009-08-04 05:01:35 +00001314 Record.clear();
1315}
Devang Patele059ba6e2009-07-23 01:07:34 +00001316
Teresa Johnson37687f32016-04-23 04:30:47 +00001317unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001318 // Assume the column is usually under 128, and always output the inlined-at
1319 // location (it's never more expensive than building an array size 1).
1320 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1321 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1322 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1323 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1324 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1325 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1326 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001327 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001328}
1329
Teresa Johnson37687f32016-04-23 04:30:47 +00001330void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1331 SmallVectorImpl<uint64_t> &Record,
1332 unsigned &Abbrev) {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001333 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001334 Abbrev = createDILocationAbbrev();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001335
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001336 Record.push_back(N->isDistinct());
1337 Record.push_back(N->getLine());
1338 Record.push_back(N->getColumn());
1339 Record.push_back(VE.getMetadataID(N->getScope()));
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001340 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001341
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001342 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001343 Record.clear();
1344}
1345
Teresa Johnson37687f32016-04-23 04:30:47 +00001346unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001347 // Assume the column is usually under 128, and always output the inlined-at
1348 // location (it's never more expensive than building an array size 1).
1349 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1350 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1352 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1353 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1354 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1355 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1356 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001357 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001358}
1359
Teresa Johnson37687f32016-04-23 04:30:47 +00001360void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1361 SmallVectorImpl<uint64_t> &Record,
1362 unsigned &Abbrev) {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001363 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001364 Abbrev = createGenericDINodeAbbrev();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001365
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001366 Record.push_back(N->isDistinct());
1367 Record.push_back(N->getTag());
1368 Record.push_back(0); // Per-tag version field; unused for now.
1369
1370 for (auto &I : N->operands())
1371 Record.push_back(VE.getMetadataOrNullID(I));
1372
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001373 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001374 Record.clear();
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001375}
1376
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001377static uint64_t rotateSign(int64_t I) {
1378 uint64_t U = I;
1379 return I < 0 ? ~(U << 1) : U << 1;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001380}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001381
Teresa Johnson37687f32016-04-23 04:30:47 +00001382void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1383 SmallVectorImpl<uint64_t> &Record,
1384 unsigned Abbrev) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001385 Record.push_back(N->isDistinct());
1386 Record.push_back(N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001387 Record.push_back(rotateSign(N->getLowerBound()));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001388
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001389 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001390 Record.clear();
1391}
1392
Teresa Johnson37687f32016-04-23 04:30:47 +00001393void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1394 SmallVectorImpl<uint64_t> &Record,
1395 unsigned Abbrev) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001396 Record.push_back(N->isDistinct());
1397 Record.push_back(rotateSign(N->getValue()));
1398 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1399
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001400 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001401 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001402}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001403
Teresa Johnson37687f32016-04-23 04:30:47 +00001404void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1405 SmallVectorImpl<uint64_t> &Record,
1406 unsigned Abbrev) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001407 Record.push_back(N->isDistinct());
1408 Record.push_back(N->getTag());
1409 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1410 Record.push_back(N->getSizeInBits());
1411 Record.push_back(N->getAlignInBits());
1412 Record.push_back(N->getEncoding());
1413
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001414 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001415 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001416}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001417
Teresa Johnson37687f32016-04-23 04:30:47 +00001418void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1419 SmallVectorImpl<uint64_t> &Record,
1420 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001421 Record.push_back(N->isDistinct());
1422 Record.push_back(N->getTag());
1423 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1424 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1425 Record.push_back(N->getLine());
1426 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001427 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001428 Record.push_back(N->getSizeInBits());
1429 Record.push_back(N->getAlignInBits());
1430 Record.push_back(N->getOffsetInBits());
1431 Record.push_back(N->getFlags());
1432 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1433
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001434 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001435 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001436}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001437
Teresa Johnson37687f32016-04-23 04:30:47 +00001438void ModuleBitcodeWriter::writeDICompositeType(
1439 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1440 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001441 const unsigned IsNotUsedInOldTypeRef = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001442 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001443 Record.push_back(N->getTag());
1444 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1445 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1446 Record.push_back(N->getLine());
1447 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1448 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1449 Record.push_back(N->getSizeInBits());
1450 Record.push_back(N->getAlignInBits());
1451 Record.push_back(N->getOffsetInBits());
1452 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001453 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001454 Record.push_back(N->getRuntimeLang());
1455 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001456 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001457 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1458
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001459 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001460 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001461}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001462
Teresa Johnson37687f32016-04-23 04:30:47 +00001463void ModuleBitcodeWriter::writeDISubroutineType(
1464 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1465 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001466 const unsigned HasNoOldTypeRefs = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001467 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001468 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001469 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001470 Record.push_back(N->getCC());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001471
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001472 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001473 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001474}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001475
Teresa Johnson37687f32016-04-23 04:30:47 +00001476void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1477 SmallVectorImpl<uint64_t> &Record,
1478 unsigned Abbrev) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001479 Record.push_back(N->isDistinct());
1480 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1481 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1482
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001483 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001484 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001485}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001486
Teresa Johnson37687f32016-04-23 04:30:47 +00001487void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1488 SmallVectorImpl<uint64_t> &Record,
1489 unsigned Abbrev) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001490 assert(N->isDistinct() && "Expected distinct compile units");
1491 Record.push_back(/* IsDistinct */ true);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001492 Record.push_back(N->getSourceLanguage());
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001493 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001494 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1495 Record.push_back(N->isOptimized());
1496 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1497 Record.push_back(N->getRuntimeVersion());
1498 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1499 Record.push_back(N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001500 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1501 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001502 Record.push_back(/* subprograms */ 0);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001503 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1504 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
Adrian Prantl1f599f92015-05-21 20:37:30 +00001505 Record.push_back(N->getDWOId());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001506 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001507
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001508 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001509 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001510}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001511
Teresa Johnson37687f32016-04-23 04:30:47 +00001512void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1513 SmallVectorImpl<uint64_t> &Record,
1514 unsigned Abbrev) {
Adrian Prantl85338cb2016-05-06 22:53:06 +00001515 uint64_t HasUnitFlag = 1 << 1;
1516 Record.push_back(N->isDistinct() | HasUnitFlag);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001517 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1518 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1519 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1520 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1521 Record.push_back(N->getLine());
1522 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1523 Record.push_back(N->isLocalToUnit());
1524 Record.push_back(N->isDefinition());
1525 Record.push_back(N->getScopeLine());
1526 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1527 Record.push_back(N->getVirtuality());
1528 Record.push_back(N->getVirtualIndex());
1529 Record.push_back(N->getFlags());
1530 Record.push_back(N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001531 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001532 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001533 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001534 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001535
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001536 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001537 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001538}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001539
Teresa Johnson37687f32016-04-23 04:30:47 +00001540void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1541 SmallVectorImpl<uint64_t> &Record,
1542 unsigned Abbrev) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001543 Record.push_back(N->isDistinct());
1544 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1545 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1546 Record.push_back(N->getLine());
1547 Record.push_back(N->getColumn());
1548
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001549 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001550 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001551}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001552
Teresa Johnson37687f32016-04-23 04:30:47 +00001553void ModuleBitcodeWriter::writeDILexicalBlockFile(
1554 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1555 unsigned Abbrev) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001556 Record.push_back(N->isDistinct());
1557 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1558 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1559 Record.push_back(N->getDiscriminator());
1560
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001561 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001562 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001563}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001564
Teresa Johnson37687f32016-04-23 04:30:47 +00001565void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1566 SmallVectorImpl<uint64_t> &Record,
1567 unsigned Abbrev) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001568 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(VE.getMetadataOrNullID(N->getRawName()));
1572 Record.push_back(N->getLine());
1573
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001574 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001575 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001576}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001577
Teresa Johnson37687f32016-04-23 04:30:47 +00001578void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1579 SmallVectorImpl<uint64_t> &Record,
1580 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001581 Record.push_back(N->isDistinct());
1582 Record.push_back(N->getMacinfoType());
1583 Record.push_back(N->getLine());
1584 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1585 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1586
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001587 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001588 Record.clear();
1589}
1590
Teresa Johnson37687f32016-04-23 04:30:47 +00001591void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1592 SmallVectorImpl<uint64_t> &Record,
1593 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001594 Record.push_back(N->isDistinct());
1595 Record.push_back(N->getMacinfoType());
1596 Record.push_back(N->getLine());
1597 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1598 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1599
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001600 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001601 Record.clear();
1602}
1603
Teresa Johnson37687f32016-04-23 04:30:47 +00001604void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1605 SmallVectorImpl<uint64_t> &Record,
1606 unsigned Abbrev) {
Adrian Prantlab1243f2015-06-29 23:03:47 +00001607 Record.push_back(N->isDistinct());
1608 for (auto &I : N->operands())
1609 Record.push_back(VE.getMetadataOrNullID(I));
1610
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001611 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
Adrian Prantlab1243f2015-06-29 23:03:47 +00001612 Record.clear();
1613}
1614
Teresa Johnson37687f32016-04-23 04:30:47 +00001615void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1616 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1617 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001618 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001619 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1620 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1621
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001622 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001623 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001624}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001625
Teresa Johnson37687f32016-04-23 04:30:47 +00001626void ModuleBitcodeWriter::writeDITemplateValueParameter(
1627 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1628 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001629 Record.push_back(N->isDistinct());
1630 Record.push_back(N->getTag());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001631 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1632 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1633 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1634
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001635 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001636 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001637}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001638
Teresa Johnson37687f32016-04-23 04:30:47 +00001639void ModuleBitcodeWriter::writeDIGlobalVariable(
1640 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1641 unsigned Abbrev) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001642 Record.push_back(N->isDistinct());
1643 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1644 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1645 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1646 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1647 Record.push_back(N->getLine());
1648 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1649 Record.push_back(N->isLocalToUnit());
1650 Record.push_back(N->isDefinition());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001651 Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001652 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1653
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001654 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001655 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001656}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001657
Teresa Johnson37687f32016-04-23 04:30:47 +00001658void ModuleBitcodeWriter::writeDILocalVariable(
1659 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1660 unsigned Abbrev) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001661 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001662 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());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001669
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001670 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001671 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001672}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001673
Teresa Johnson37687f32016-04-23 04:30:47 +00001674void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1675 SmallVectorImpl<uint64_t> &Record,
1676 unsigned Abbrev) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001677 Record.reserve(N->getElements().size() + 1);
1678
1679 Record.push_back(N->isDistinct());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001680 Record.append(N->elements_begin(), N->elements_end());
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001681
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001682 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001683 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001684}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001685
Teresa Johnson37687f32016-04-23 04:30:47 +00001686void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1687 SmallVectorImpl<uint64_t> &Record,
1688 unsigned Abbrev) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001689 Record.push_back(N->isDistinct());
1690 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1691 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1692 Record.push_back(N->getLine());
1693 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1694 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1695 Record.push_back(N->getAttributes());
1696 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1697
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001698 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001699 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001700}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001701
Teresa Johnson37687f32016-04-23 04:30:47 +00001702void ModuleBitcodeWriter::writeDIImportedEntity(
1703 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1704 unsigned Abbrev) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001705 Record.push_back(N->isDistinct());
1706 Record.push_back(N->getTag());
1707 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1708 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1709 Record.push_back(N->getLine());
1710 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1711
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001712 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001713 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001714}
1715
Teresa Johnson37687f32016-04-23 04:30:47 +00001716unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001717 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1718 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1719 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1720 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001721 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001722}
1723
Teresa Johnson37687f32016-04-23 04:30:47 +00001724void ModuleBitcodeWriter::writeNamedMetadata(
1725 SmallVectorImpl<uint64_t> &Record) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001726 if (M.named_metadata_empty())
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001727 return;
1728
Teresa Johnson37687f32016-04-23 04:30:47 +00001729 unsigned Abbrev = createNamedMetadataAbbrev();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001730 for (const NamedMDNode &NMD : M.named_metadata()) {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001731 // Write name.
1732 StringRef Str = NMD.getName();
1733 Record.append(Str.bytes_begin(), Str.bytes_end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001734 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001735 Record.clear();
1736
1737 // Write named metadata operands.
1738 for (const MDNode *N : NMD.operands())
1739 Record.push_back(VE.getMetadataID(N));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001740 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001741 Record.clear();
1742 }
1743}
1744
Teresa Johnson37687f32016-04-23 04:30:47 +00001745unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001746 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1747 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1748 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1749 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1750 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001751 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001752}
1753
1754/// Write out a record for MDString.
1755///
1756/// All the metadata strings in a metadata block are emitted in a single
1757/// record. The sizes and strings themselves are shoved into a blob.
Teresa Johnson37687f32016-04-23 04:30:47 +00001758void ModuleBitcodeWriter::writeMetadataStrings(
1759 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001760 if (Strings.empty())
1761 return;
1762
1763 // Start the record with the number of strings.
1764 Record.push_back(bitc::METADATA_STRINGS);
1765 Record.push_back(Strings.size());
1766
1767 // Emit the sizes of the strings in the blob.
1768 SmallString<256> Blob;
1769 {
1770 BitstreamWriter W(Blob);
1771 for (const Metadata *MD : Strings)
1772 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1773 W.FlushToWord();
1774 }
1775
1776 // Add the offset to the strings to the record.
1777 Record.push_back(Blob.size());
1778
1779 // Add the strings to the blob.
1780 for (const Metadata *MD : Strings)
1781 Blob.append(cast<MDString>(MD)->getString());
1782
1783 // Emit the final record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001784 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001785 Record.clear();
1786}
1787
Teresa Johnson37687f32016-04-23 04:30:47 +00001788void ModuleBitcodeWriter::writeMetadataRecords(
1789 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001790 if (MDs.empty())
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +00001791 return;
1792
Duncan P. N. Exon Smith6b7b6802015-02-04 21:54:12 +00001793 // Initialize MDNode abbreviations.
1794#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1795#include "llvm/IR/Metadata.def"
1796
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001797 for (const Metadata *MD : MDs) {
Duncan P. N. Exon Smith73d5aae2015-01-12 22:31:35 +00001798 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith79f8d112015-03-17 00:16:35 +00001799 assert(N->isResolved() && "Expected forward references to be resolved");
1800
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001801 switch (N->getMetadataID()) {
1802 default:
1803 llvm_unreachable("Invalid MDNode subclass");
1804#define HANDLE_MDNODE_LEAF(CLASS) \
1805 case Metadata::CLASS##Kind: \
Teresa Johnson37687f32016-04-23 04:30:47 +00001806 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001807 continue;
1808#include "llvm/IR/Metadata.def"
1809 }
Devang Patel8cca7b42009-08-04 05:01:35 +00001810 }
Teresa Johnson37687f32016-04-23 04:30:47 +00001811 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
Devang Patel8cca7b42009-08-04 05:01:35 +00001812 }
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001813}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001814
Teresa Johnson37687f32016-04-23 04:30:47 +00001815void ModuleBitcodeWriter::writeModuleMetadata() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001816 if (!VE.hasMDs() && M.named_metadata_empty())
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001817 return;
1818
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001819 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001820 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001821 writeMetadataStrings(VE.getMDStrings(), Record);
1822 writeMetadataRecords(VE.getNonMDStrings(), Record);
1823 writeNamedMetadata(Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001824 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001825}
1826
Teresa Johnson37687f32016-04-23 04:30:47 +00001827void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +00001828 if (!VE.hasMDs())
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001829 return;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001830
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001831 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001832 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001833 writeMetadataStrings(VE.getMDStrings(), Record);
1834 writeMetadataRecords(VE.getNonMDStrings(), Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001835 Stream.ExitBlock();
Victor Hernandezb00a6be2010-01-13 19:37:33 +00001836}
1837
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001838void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
1839 SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) {
1840 // [n x [id, mdnode]]
1841 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1842 GO.getAllMetadata(MDs);
1843 for (const auto &I : MDs) {
1844 Record.push_back(I.first);
1845 Record.push_back(VE.getMetadataID(I.second));
1846 }
1847}
1848
1849void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001850 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
Chris Lattner07d09ed2010-04-03 02:17:50 +00001851
Devang Patelaf206b82009-09-18 19:26:43 +00001852 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001853
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001854 if (F.hasMetadata()) {
1855 pushGlobalMetadataAttachment(Record, F);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001856 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001857 Record.clear();
1858 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001859
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001860 // Write metadata attachments
1861 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1862 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001863 for (const BasicBlock &BB : F)
1864 for (const Instruction &I : BB) {
Devang Patel6da5dbf2009-10-22 18:55:16 +00001865 MDs.clear();
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001866 I.getAllMetadataOtherThanDebugLoc(MDs);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001867
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001868 // If no metadata, ignore instruction.
1869 if (MDs.empty()) continue;
1870
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001871 Record.push_back(VE.getInstructionID(&I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001872
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001873 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1874 Record.push_back(MDs[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001875 Record.push_back(VE.getMetadataID(MDs[i].second));
Devang Patelaf206b82009-09-18 19:26:43 +00001876 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001877 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001878 Record.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00001879 }
1880
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001881 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001882}
1883
Teresa Johnson37687f32016-04-23 04:30:47 +00001884void ModuleBitcodeWriter::writeModuleMetadataStore() {
Devang Patelaf206b82009-09-18 19:26:43 +00001885 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001886
Devang Patelaf206b82009-09-18 19:26:43 +00001887 // Write metadata kinds
1888 // METADATA_KIND - [n x [id, name]]
Michael Ilseman979dfbb2012-12-03 22:57:47 +00001889 SmallVector<StringRef, 8> Names;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001890 M.getMDKindNames(Names);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001891
Dan Gohman43aa8f02010-07-20 21:42:28 +00001892 if (Names.empty()) return;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001893
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001894 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001895
Dan Gohman43aa8f02010-07-20 21:42:28 +00001896 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattnerc9558df2009-12-28 20:10:43 +00001897 Record.push_back(MDKindID);
1898 StringRef KName = Names[MDKindID];
1899 Record.append(KName.begin(), KName.end());
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001900
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001901 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
Devang Patelaf206b82009-09-18 19:26:43 +00001902 Record.clear();
1903 }
1904
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001905 Stream.ExitBlock();
Devang Patel8cca7b42009-08-04 05:01:35 +00001906}
1907
Teresa Johnson37687f32016-04-23 04:30:47 +00001908void ModuleBitcodeWriter::writeOperandBundleTags() {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001909 // Write metadata kinds
1910 //
1911 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
1912 //
1913 // OPERAND_BUNDLE_TAG - [strchr x N]
1914
1915 SmallVector<StringRef, 8> Tags;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001916 M.getOperandBundleTags(Tags);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001917
1918 if (Tags.empty())
1919 return;
1920
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001921 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001922
1923 SmallVector<uint64_t, 64> Record;
1924
1925 for (auto Tag : Tags) {
1926 Record.append(Tag.begin(), Tag.end());
1927
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001928 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001929 Record.clear();
1930 }
1931
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001932 Stream.ExitBlock();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001933}
1934
Jan Wen Voungafaced02012-10-11 20:20:40 +00001935static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1936 if ((int64_t)V >= 0)
1937 Vals.push_back(V << 1);
1938 else
1939 Vals.push_back((-V << 1) | 1);
1940}
1941
Teresa Johnson37687f32016-04-23 04:30:47 +00001942void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1943 bool isGlobal) {
Devang Patel8cca7b42009-08-04 05:01:35 +00001944 if (FirstVal == LastVal) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001945
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001946 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Devang Patel8cca7b42009-08-04 05:01:35 +00001947
1948 unsigned AggregateAbbrev = 0;
1949 unsigned String8Abbrev = 0;
1950 unsigned CString7Abbrev = 0;
1951 unsigned CString6Abbrev = 0;
1952 // If this is a constant pool for the module, emit module-specific abbrevs.
1953 if (isGlobal) {
1954 // Abbrev for CST_CODE_AGGREGATE.
1955 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1956 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001959 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001960
1961 // Abbrev for CST_CODE_STRING.
1962 Abbv = new BitCodeAbbrev();
1963 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1964 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001966 String8Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001967 // Abbrev for CST_CODE_CSTRING.
1968 Abbv = new BitCodeAbbrev();
1969 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1970 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1971 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001972 CString7Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001973 // Abbrev for CST_CODE_CSTRING.
1974 Abbv = new BitCodeAbbrev();
1975 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1976 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1977 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001978 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001979 }
1980
Devang Patel8cca7b42009-08-04 05:01:35 +00001981 SmallVector<uint64_t, 64> Record;
1982
1983 const ValueEnumerator::ValueList &Vals = VE.getValues();
Craig Topper2617dcc2014-04-15 06:32:26 +00001984 Type *LastTy = nullptr;
Devang Patel8cca7b42009-08-04 05:01:35 +00001985 for (unsigned i = FirstVal; i != LastVal; ++i) {
1986 const Value *V = Vals[i].first;
Chris Lattner52523562007-04-24 00:16:04 +00001987 // If we need to switch types, do so now.
1988 if (V->getType() != LastTy) {
1989 LastTy = V->getType();
1990 Record.push_back(VE.getTypeID(LastTy));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001991 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1992 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner52523562007-04-24 00:16:04 +00001993 Record.clear();
1994 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001995
Chris Lattner52523562007-04-24 00:16:04 +00001996 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesenfd04c742009-10-13 20:46:56 +00001997 Record.push_back(unsigned(IA->hasSideEffects()) |
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001998 unsigned(IA->isAlignStack()) << 1 |
1999 unsigned(IA->getDialect()&1) << 2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002000
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002001 // Add the asm string.
2002 const std::string &AsmStr = IA->getAsmString();
2003 Record.push_back(AsmStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002004 Record.append(AsmStr.begin(), AsmStr.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002005
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002006 // Add the constraint string.
2007 const std::string &ConstraintStr = IA->getConstraintString();
2008 Record.push_back(ConstraintStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002009 Record.append(ConstraintStr.begin(), ConstraintStr.end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002010 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002011 Record.clear();
Chris Lattner52523562007-04-24 00:16:04 +00002012 continue;
2013 }
2014 const Constant *C = cast<Constant>(V);
2015 unsigned Code = -1U;
2016 unsigned AbbrevToUse = 0;
2017 if (C->isNullValue()) {
2018 Code = bitc::CST_CODE_NULL;
2019 } else if (isa<UndefValue>(C)) {
2020 Code = bitc::CST_CODE_UNDEF;
2021 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
Bob Wilsone4077362013-09-09 19:14:35 +00002022 if (IV->getBitWidth() <= 64) {
2023 uint64_t V = IV->getSExtValue();
2024 emitSignedInt64(Record, V);
2025 Code = bitc::CST_CODE_INTEGER;
2026 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2027 } else { // Wide integers, > 64 bits in size.
2028 // We have an arbitrary precision integer value to write whose
2029 // bit width is > 64. However, in canonical unsigned integer
2030 // format it is likely that the high bits are going to be zero.
2031 // So, we only write the number of active words.
2032 unsigned NWords = IV->getValue().getActiveWords();
2033 const uint64_t *RawWords = IV->getValue().getRawData();
2034 for (unsigned i = 0; i != NWords; ++i) {
2035 emitSignedInt64(Record, RawWords[i]);
2036 }
2037 Code = bitc::CST_CODE_WIDE_INTEGER;
2038 }
Chris Lattner52523562007-04-24 00:16:04 +00002039 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2040 Code = bitc::CST_CODE_FLOAT;
Chris Lattner229907c2011-07-18 04:54:35 +00002041 Type *Ty = CFP->getType();
Dan Gohman518cda42011-12-17 00:04:22 +00002042 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002043 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +00002044 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002045 // api needed to prevent premature destruction
Dale Johannesen93eefa02009-03-23 21:16:53 +00002046 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen54306fe2008-10-09 18:53:47 +00002047 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002048 const uint64_t *p = api.getRawData();
Dale Johannesen93eefa02009-03-23 21:16:53 +00002049 Record.push_back((p[1] << 48) | (p[0] >> 16));
2050 Record.push_back(p[0] & 0xffffLL);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002051 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002052 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002053 const uint64_t *p = api.getRawData();
Dale Johannesen245dceb2007-09-11 18:32:33 +00002054 Record.push_back(p[0]);
2055 Record.push_back(p[1]);
Dale Johannesenbdad8092007-08-09 22:51:36 +00002056 } else {
2057 assert (0 && "Unknown FP type!");
Chris Lattner52523562007-04-24 00:16:04 +00002058 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00002059 } 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()) {
2066 Code = bitc::CST_CODE_CSTRING;
2067 --NumElts; // Don't encode the null, which isn't allowed by char6.
2068 } else {
2069 Code = bitc::CST_CODE_STRING;
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 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002081
Chris Lattner372dd1e2012-01-30 00:51:16 +00002082 if (isCStrChar6)
2083 AbbrevToUse = CString6Abbrev;
2084 else if (isCStr7)
2085 AbbrevToUse = CString7Abbrev;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002086 } else if (const ConstantDataSequential *CDS =
Chris Lattner372dd1e2012-01-30 00:51:16 +00002087 dyn_cast<ConstantDataSequential>(C)) {
Chris Lattner5d917072012-01-30 07:36:01 +00002088 Code = bitc::CST_CODE_DATA;
Chris Lattner372dd1e2012-01-30 00:51:16 +00002089 Type *EltTy = CDS->getType()->getElementType();
2090 if (isa<IntegerType>(EltTy)) {
2091 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2092 Record.push_back(CDS->getElementAsInteger(i));
Chris Lattner372dd1e2012-01-30 00:51:16 +00002093 } else {
Justin Bognera43eacb2016-01-06 22:31:32 +00002094 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2095 Record.push_back(
2096 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
Chris Lattner372dd1e2012-01-30 00:51:16 +00002097 }
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00002098 } else if (isa<ConstantAggregate>(C)) {
Chris Lattner52523562007-04-24 00:16:04 +00002099 Code = bitc::CST_CODE_AGGREGATE;
Pete Cooper125ad172015-06-25 20:51:38 +00002100 for (const Value *Op : C->operands())
2101 Record.push_back(VE.getValueID(Op));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002102 AbbrevToUse = AggregateAbbrev;
Chris Lattner52523562007-04-24 00:16:04 +00002103 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002104 switch (CE->getOpcode()) {
2105 default:
2106 if (Instruction::isCast(CE->getOpcode())) {
2107 Code = bitc::CST_CODE_CE_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002108 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002109 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2110 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002111 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002112 } else {
2113 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2114 Code = bitc::CST_CODE_CE_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002115 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002116 Record.push_back(VE.getValueID(C->getOperand(0)));
2117 Record.push_back(VE.getValueID(C->getOperand(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002118 uint64_t Flags = getOptimizationFlags(CE);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002119 if (Flags != 0)
2120 Record.push_back(Flags);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002121 }
2122 break;
David Blaikieb9263572015-03-13 21:03:36 +00002123 case Instruction::GetElementPtr: {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002124 Code = bitc::CST_CODE_CE_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00002125 const auto *GO = cast<GEPOperator>(C);
2126 if (GO->isInBounds())
Dan Gohman1639c392009-07-27 21:53:46 +00002127 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00002128 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002129 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2130 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2131 Record.push_back(VE.getValueID(C->getOperand(i)));
2132 }
2133 break;
David Blaikieb9263572015-03-13 21:03:36 +00002134 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002135 case Instruction::Select:
2136 Code = bitc::CST_CODE_CE_SELECT;
2137 Record.push_back(VE.getValueID(C->getOperand(0)));
2138 Record.push_back(VE.getValueID(C->getOperand(1)));
2139 Record.push_back(VE.getValueID(C->getOperand(2)));
2140 break;
2141 case Instruction::ExtractElement:
2142 Code = bitc::CST_CODE_CE_EXTRACTELT;
2143 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2144 Record.push_back(VE.getValueID(C->getOperand(0)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002145 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002146 Record.push_back(VE.getValueID(C->getOperand(1)));
2147 break;
2148 case Instruction::InsertElement:
2149 Code = bitc::CST_CODE_CE_INSERTELT;
2150 Record.push_back(VE.getValueID(C->getOperand(0)));
2151 Record.push_back(VE.getValueID(C->getOperand(1)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002152 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002153 Record.push_back(VE.getValueID(C->getOperand(2)));
2154 break;
2155 case Instruction::ShuffleVector:
Nate Begeman94aa38d2009-02-12 21:28:33 +00002156 // If the return type and argument types are the same, this is a
2157 // standard shufflevector instruction. If the types are different,
2158 // then the shuffle is widening or truncating the input vectors, and
2159 // the argument type must also be encoded.
2160 if (C->getType() == C->getOperand(0)->getType()) {
2161 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2162 } else {
2163 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2164 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2165 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002166 Record.push_back(VE.getValueID(C->getOperand(0)));
2167 Record.push_back(VE.getValueID(C->getOperand(1)));
2168 Record.push_back(VE.getValueID(C->getOperand(2)));
2169 break;
2170 case Instruction::ICmp:
2171 case Instruction::FCmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002172 Code = bitc::CST_CODE_CE_CMP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002173 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2174 Record.push_back(VE.getValueID(C->getOperand(0)));
2175 Record.push_back(VE.getValueID(C->getOperand(1)));
2176 Record.push_back(CE->getPredicate());
2177 break;
2178 }
Chris Lattnerf540d742009-10-28 05:24:40 +00002179 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerf540d742009-10-28 05:24:40 +00002180 Code = bitc::CST_CODE_BLOCKADDRESS;
2181 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2182 Record.push_back(VE.getValueID(BA->getFunction()));
2183 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner52523562007-04-24 00:16:04 +00002184 } else {
Dan Gohman47dc8fd2010-07-21 21:18:37 +00002185#ifndef NDEBUG
2186 C->dump();
2187#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002188 llvm_unreachable("Unknown constant!");
Chris Lattner52523562007-04-24 00:16:04 +00002189 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002190 Stream.EmitRecord(Code, Record, AbbrevToUse);
Chris Lattner52523562007-04-24 00:16:04 +00002191 Record.clear();
2192 }
2193
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002194 Stream.ExitBlock();
Chris Lattner52523562007-04-24 00:16:04 +00002195}
2196
Teresa Johnson37687f32016-04-23 04:30:47 +00002197void ModuleBitcodeWriter::writeModuleConstants() {
Chris Lattner52523562007-04-24 00:16:04 +00002198 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002199
Chris Lattner52523562007-04-24 00:16:04 +00002200 // Find the first constant to emit, which is the first non-globalvalue value.
2201 // We know globalvalues have been emitted by WriteModuleInfo.
2202 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2203 if (!isa<GlobalValue>(Vals[i].first)) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002204 writeConstants(i, Vals.size(), true);
Chris Lattner52523562007-04-24 00:16:04 +00002205 return;
2206 }
2207 }
2208}
Chris Lattner215e9cd2007-04-23 20:35:01 +00002209
Teresa Johnson37687f32016-04-23 04:30:47 +00002210/// pushValueAndType - The file has to encode both the value and type id for
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002211/// many values, because we need to know what type to create for forward
2212/// references. However, most operands are not forward references, so this type
2213/// field is not needed.
2214///
2215/// This function adds V's value ID to Vals. If the value ID is higher than the
2216/// instruction ID, then it is a forward reference, and it also includes the
Jan Wen Voungafaced02012-10-11 20:20:40 +00002217/// type ID. The value ID that is written is encoded relative to the InstID.
Teresa Johnson37687f32016-04-23 04:30:47 +00002218bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2219 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002220 unsigned ValID = VE.getValueID(V);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002221 // Make encoding relative to the InstID.
2222 Vals.push_back(InstID - ValID);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002223 if (ValID >= InstID) {
2224 Vals.push_back(VE.getTypeID(V->getType()));
2225 return true;
2226 }
2227 return false;
2228}
2229
Teresa Johnson37687f32016-04-23 04:30:47 +00002230void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2231 unsigned InstID) {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002232 SmallVector<unsigned, 64> Record;
2233 LLVMContext &C = CS.getInstruction()->getContext();
2234
2235 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002236 const auto &Bundle = CS.getOperandBundleAt(i);
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002237 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002238
2239 for (auto &Input : Bundle.Inputs)
Teresa Johnson37687f32016-04-23 04:30:47 +00002240 pushValueAndType(Input, InstID, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002241
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002242 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002243 Record.clear();
2244 }
2245}
2246
Teresa Johnson37687f32016-04-23 04:30:47 +00002247/// pushValue - Like pushValueAndType, but where the type of the value is
Jan Wen Voungafaced02012-10-11 20:20:40 +00002248/// omitted (perhaps it was already encoded in an earlier operand).
Teresa Johnson37687f32016-04-23 04:30:47 +00002249void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2250 SmallVectorImpl<unsigned> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002251 unsigned ValID = VE.getValueID(V);
2252 Vals.push_back(InstID - ValID);
2253}
2254
Teresa Johnson37687f32016-04-23 04:30:47 +00002255void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2256 SmallVectorImpl<uint64_t> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002257 unsigned ValID = VE.getValueID(V);
2258 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2259 emitSignedInt64(Vals, diff);
2260}
2261
Chris Lattnere6e364c2007-04-26 05:53:54 +00002262/// WriteInstruction - Emit an instruction to the specified stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002263void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2264 unsigned InstID,
2265 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnere6e364c2007-04-26 05:53:54 +00002266 unsigned Code = 0;
2267 unsigned AbbrevToUse = 0;
Devang Patelaf206b82009-09-18 19:26:43 +00002268 VE.setInstructionID(&I);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002269 switch (I.getOpcode()) {
2270 default:
2271 if (Instruction::isCast(I.getOpcode())) {
Chris Lattner0a603252007-05-01 02:13:26 +00002272 Code = bitc::FUNC_CODE_INST_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002273 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002274 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002275 Vals.push_back(VE.getTypeID(I.getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002276 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
Chris Lattnere6e364c2007-04-26 05:53:54 +00002277 } else {
2278 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattner9f35f912007-05-02 04:26:36 +00002279 Code = bitc::FUNC_CODE_INST_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002280 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002281 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Teresa Johnson37687f32016-04-23 04:30:47 +00002282 pushValue(I.getOperand(1), InstID, Vals);
2283 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2284 uint64_t Flags = getOptimizationFlags(&I);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002285 if (Flags != 0) {
2286 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2287 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2288 Vals.push_back(Flags);
2289 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002290 }
2291 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002292
David Blaikieb5b5efd2015-02-25 01:08:52 +00002293 case Instruction::GetElementPtr: {
Chris Lattner0a603252007-05-01 02:13:26 +00002294 Code = bitc::FUNC_CODE_INST_GEP;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002295 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2296 auto &GEPInst = cast<GetElementPtrInst>(I);
2297 Vals.push_back(GEPInst.isInBounds());
2298 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002299 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002300 pushValueAndType(I.getOperand(i), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002301 break;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002302 }
Dan Gohmanca0256a2008-05-31 19:11:15 +00002303 case Instruction::ExtractValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002304 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002305 pushValueAndType(I.getOperand(0), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002306 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002307 Vals.append(EVI->idx_begin(), EVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002308 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002309 }
2310 case Instruction::InsertValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002311 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002312 pushValueAndType(I.getOperand(0), InstID, Vals);
2313 pushValueAndType(I.getOperand(1), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002314 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002315 Vals.append(IVI->idx_begin(), IVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002316 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002317 }
Chris Lattner0a603252007-05-01 02:13:26 +00002318 case Instruction::Select:
Dan Gohmanc5d28922008-09-16 01:01:33 +00002319 Code = bitc::FUNC_CODE_INST_VSELECT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002320 pushValueAndType(I.getOperand(1), InstID, Vals);
2321 pushValue(I.getOperand(2), InstID, Vals);
2322 pushValueAndType(I.getOperand(0), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002323 break;
2324 case Instruction::ExtractElement:
2325 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002326 pushValueAndType(I.getOperand(0), InstID, Vals);
2327 pushValueAndType(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002328 break;
2329 case Instruction::InsertElement:
2330 Code = bitc::FUNC_CODE_INST_INSERTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002331 pushValueAndType(I.getOperand(0), InstID, Vals);
2332 pushValue(I.getOperand(1), InstID, Vals);
2333 pushValueAndType(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002334 break;
2335 case Instruction::ShuffleVector:
2336 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002337 pushValueAndType(I.getOperand(0), InstID, Vals);
2338 pushValue(I.getOperand(1), InstID, Vals);
2339 pushValue(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002340 break;
2341 case Instruction::ICmp:
James Molloy88eb5352015-07-10 12:52:00 +00002342 case Instruction::FCmp: {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002343 // compare returning Int1Ty or vector of Int1Ty
2344 Code = bitc::FUNC_CODE_INST_CMP2;
Teresa Johnson37687f32016-04-23 04:30:47 +00002345 pushValueAndType(I.getOperand(0), InstID, Vals);
2346 pushValue(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002347 Vals.push_back(cast<CmpInst>(I).getPredicate());
Teresa Johnson37687f32016-04-23 04:30:47 +00002348 uint64_t Flags = getOptimizationFlags(&I);
James Molloy88eb5352015-07-10 12:52:00 +00002349 if (Flags != 0)
2350 Vals.push_back(Flags);
Chris Lattner0a603252007-05-01 02:13:26 +00002351 break;
James Molloy88eb5352015-07-10 12:52:00 +00002352 }
Chris Lattner0a603252007-05-01 02:13:26 +00002353
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002354 case Instruction::Ret:
Devang Patelbbfd8742008-02-26 01:29:32 +00002355 {
2356 Code = bitc::FUNC_CODE_INST_RET;
2357 unsigned NumOperands = I.getNumOperands();
Devang Patelbbfd8742008-02-26 01:29:32 +00002358 if (NumOperands == 0)
2359 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2360 else if (NumOperands == 1) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002361 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Devang Patelbbfd8742008-02-26 01:29:32 +00002362 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2363 } else {
2364 for (unsigned i = 0, e = NumOperands; i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002365 pushValueAndType(I.getOperand(i), InstID, Vals);
Devang Patelbbfd8742008-02-26 01:29:32 +00002366 }
2367 }
Chris Lattner0a603252007-05-01 02:13:26 +00002368 break;
2369 case Instruction::Br:
Gabor Greif1933b002009-01-30 18:27:21 +00002370 {
2371 Code = bitc::FUNC_CODE_INST_BR;
David Blaikieb78e9e52013-02-11 01:16:51 +00002372 const BranchInst &II = cast<BranchInst>(I);
Gabor Greif1933b002009-01-30 18:27:21 +00002373 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2374 if (II.isConditional()) {
2375 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002376 pushValue(II.getCondition(), InstID, Vals);
Gabor Greif1933b002009-01-30 18:27:21 +00002377 }
Chris Lattner0a603252007-05-01 02:13:26 +00002378 }
2379 break;
2380 case Instruction::Switch:
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002381 {
2382 Code = bitc::FUNC_CODE_INST_SWITCH;
David Blaikieb78e9e52013-02-11 01:16:51 +00002383 const SwitchInst &SI = cast<SwitchInst>(I);
Bob Wilsone4077362013-09-09 19:14:35 +00002384 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002385 pushValue(SI.getCondition(), InstID, Vals);
Bob Wilsone4077362013-09-09 19:14:35 +00002386 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
Sanjay Patel225d65f2015-11-13 16:21:23 +00002387 for (SwitchInst::ConstCaseIt Case : SI.cases()) {
2388 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2389 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002390 }
2391 }
Chris Lattner0a603252007-05-01 02:13:26 +00002392 break;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002393 case Instruction::IndirectBr:
2394 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattner3ed871f2009-10-27 19:13:16 +00002395 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Jan Wen Voungafaced02012-10-11 20:20:40 +00002396 // Encode the address operand as relative, but not the basic blocks.
Teresa Johnson37687f32016-04-23 04:30:47 +00002397 pushValue(I.getOperand(0), InstID, Vals);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002398 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
Chris Lattner3ed871f2009-10-27 19:13:16 +00002399 Vals.push_back(VE.getValueID(I.getOperand(i)));
2400 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002401
Chris Lattnerb811e952007-05-01 07:03:37 +00002402 case Instruction::Invoke: {
Gabor Greif4b79e472009-01-16 18:40:27 +00002403 const InvokeInst *II = cast<InvokeInst>(&I);
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002404 const Value *Callee = II->getCalledValue();
2405 FunctionType *FTy = II->getFunctionType();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002406
2407 if (II->hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002408 writeOperandBundles(II, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002409
Chris Lattner0a603252007-05-01 02:13:26 +00002410 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002411
Devang Patel4c758ea2008-09-25 21:00:45 +00002412 Vals.push_back(VE.getAttributeID(II->getAttributes()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002413 Vals.push_back(II->getCallingConv() | 1 << 13);
Gabor Greif6ecd6f42009-01-07 22:39:29 +00002414 Vals.push_back(VE.getValueID(II->getNormalDest()));
2415 Vals.push_back(VE.getValueID(II->getUnwindDest()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002416 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002417 pushValueAndType(Callee, InstID, Vals);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002418
Chris Lattner0a603252007-05-01 02:13:26 +00002419 // Emit value #'s for the fixed parameters.
Chris Lattner0a603252007-05-01 02:13:26 +00002420 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002421 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
Chris Lattner0a603252007-05-01 02:13:26 +00002422
2423 // Emit type/value pairs for varargs params.
2424 if (FTy->isVarArg()) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00002425 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002426 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002427 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
Chris Lattner0a603252007-05-01 02:13:26 +00002428 }
2429 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002430 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002431 case Instruction::Resume:
2432 Code = bitc::FUNC_CODE_INST_RESUME;
Teresa Johnson37687f32016-04-23 04:30:47 +00002433 pushValueAndType(I.getOperand(0), InstID, Vals);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002434 break;
David Majnemer654e1302015-07-31 17:58:14 +00002435 case Instruction::CleanupRet: {
2436 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2437 const auto &CRI = cast<CleanupReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002438 pushValue(CRI.getCleanupPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002439 if (CRI.hasUnwindDest())
2440 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2441 break;
2442 }
2443 case Instruction::CatchRet: {
2444 Code = bitc::FUNC_CODE_INST_CATCHRET;
2445 const auto &CRI = cast<CatchReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002446 pushValue(CRI.getCatchPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002447 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2448 break;
2449 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00002450 case Instruction::CleanupPad:
David Majnemer654e1302015-07-31 17:58:14 +00002451 case Instruction::CatchPad: {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002452 const auto &FuncletPad = cast<FuncletPadInst>(I);
2453 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2454 : bitc::FUNC_CODE_INST_CLEANUPPAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002455 pushValue(FuncletPad.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002456
2457 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
David Majnemer654e1302015-07-31 17:58:14 +00002458 Vals.push_back(NumArgOperands);
2459 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
Teresa Johnson37687f32016-04-23 04:30:47 +00002460 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002461 break;
2462 }
2463 case Instruction::CatchSwitch: {
2464 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2465 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2466
Teresa Johnson37687f32016-04-23 04:30:47 +00002467 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002468
2469 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2470 Vals.push_back(NumHandlers);
2471 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2472 Vals.push_back(VE.getValueID(CatchPadBB));
2473
2474 if (CatchSwitch.hasUnwindDest())
2475 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
David Majnemer654e1302015-07-31 17:58:14 +00002476 break;
2477 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002478 case Instruction::Unreachable:
2479 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002480 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002481 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002482
Jay Foad372ad642011-06-20 14:18:48 +00002483 case Instruction::PHI: {
2484 const PHINode &PN = cast<PHINode>(I);
Chris Lattner0a603252007-05-01 02:13:26 +00002485 Code = bitc::FUNC_CODE_INST_PHI;
Jan Wen Voungafaced02012-10-11 20:20:40 +00002486 // With the newer instruction encoding, forward references could give
2487 // negative valued IDs. This is most common for PHIs, so we use
2488 // signed VBRs.
2489 SmallVector<uint64_t, 128> Vals64;
2490 Vals64.push_back(VE.getTypeID(PN.getType()));
Jay Foad372ad642011-06-20 14:18:48 +00002491 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002492 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002493 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
Jay Foad372ad642011-06-20 14:18:48 +00002494 }
Jan Wen Voungafaced02012-10-11 20:20:40 +00002495 // Emit a Vals64 vector and exit.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002496 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002497 Vals64.clear();
2498 return;
Jay Foad372ad642011-06-20 14:18:48 +00002499 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002500
Bill Wendlingfae14752011-08-12 20:24:12 +00002501 case Instruction::LandingPad: {
2502 const LandingPadInst &LP = cast<LandingPadInst>(I);
2503 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2504 Vals.push_back(VE.getTypeID(LP.getType()));
Bill Wendlingfae14752011-08-12 20:24:12 +00002505 Vals.push_back(LP.isCleanup());
2506 Vals.push_back(LP.getNumClauses());
2507 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2508 if (LP.isCatch(I))
2509 Vals.push_back(LandingPadInst::Catch);
2510 else
2511 Vals.push_back(LandingPadInst::Filter);
Teresa Johnson37687f32016-04-23 04:30:47 +00002512 pushValueAndType(LP.getClause(I), InstID, Vals);
Bill Wendlingfae14752011-08-12 20:24:12 +00002513 }
2514 break;
2515 }
2516
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002517 case Instruction::Alloca: {
Chris Lattner0a603252007-05-01 02:13:26 +00002518 Code = bitc::FUNC_CODE_INST_ALLOCA;
David Blaikiebdb49102015-04-28 16:51:01 +00002519 const AllocaInst &AI = cast<AllocaInst>(I);
2520 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
Dan Gohman9da5bb02010-05-28 01:38:28 +00002521 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002522 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002523 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2524 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2525 "not enough bits for maximum alignment");
2526 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2527 AlignRecord |= AI.isUsedWithInAlloca() << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00002528 AlignRecord |= 1 << 6;
Manman Ren9bfd0d02016-04-01 21:41:15 +00002529 AlignRecord |= AI.isSwiftError() << 7;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002530 Vals.push_back(AlignRecord);
Chris Lattner0a603252007-05-01 02:13:26 +00002531 break;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002532 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002533
Chris Lattner0a603252007-05-01 02:13:26 +00002534 case Instruction::Load:
Eli Friedman59b66882011-08-09 23:02:53 +00002535 if (cast<LoadInst>(I).isAtomic()) {
2536 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002537 pushValueAndType(I.getOperand(0), InstID, Vals);
Eli Friedman59b66882011-08-09 23:02:53 +00002538 } else {
2539 Code = bitc::FUNC_CODE_INST_LOAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002540 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
Eli Friedman59b66882011-08-09 23:02:53 +00002541 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2542 }
David Blaikie85035652015-02-25 01:07:20 +00002543 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002544 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2545 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002546 if (cast<LoadInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002547 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2548 Vals.push_back(getEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002549 }
Chris Lattner0a603252007-05-01 02:13:26 +00002550 break;
2551 case Instruction::Store:
Eli Friedman59b66882011-08-09 23:02:53 +00002552 if (cast<StoreInst>(I).isAtomic())
2553 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2554 else
2555 Code = bitc::FUNC_CODE_INST_STORE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002556 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2557 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
Chris Lattner0a603252007-05-01 02:13:26 +00002558 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2559 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002560 if (cast<StoreInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002561 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2562 Vals.push_back(getEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002563 }
Chris Lattner0a603252007-05-01 02:13:26 +00002564 break;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002565 case Instruction::AtomicCmpXchg:
2566 Code = bitc::FUNC_CODE_INST_CMPXCHG;
Teresa Johnson37687f32016-04-23 04:30:47 +00002567 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2568 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2569 pushValue(I.getOperand(2), InstID, Vals); // newval.
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002570 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002571 Vals.push_back(
2572 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2573 Vals.push_back(
2574 getEncodedSynchScope(cast<AtomicCmpXchgInst>(I).getSynchScope()));
2575 Vals.push_back(
2576 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
Tim Northover420a2162014-06-13 14:24:07 +00002577 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002578 break;
2579 case Instruction::AtomicRMW:
2580 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
Teresa Johnson37687f32016-04-23 04:30:47 +00002581 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2582 pushValue(I.getOperand(1), InstID, Vals); // val.
2583 Vals.push_back(
2584 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002585 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002586 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2587 Vals.push_back(
2588 getEncodedSynchScope(cast<AtomicRMWInst>(I).getSynchScope()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002589 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002590 case Instruction::Fence:
2591 Code = bitc::FUNC_CODE_INST_FENCE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002592 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2593 Vals.push_back(getEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
Eli Friedmanfee02c62011-07-25 23:16:38 +00002594 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002595 case Instruction::Call: {
Gabor Greife9afee22010-06-26 09:35:09 +00002596 const CallInst &CI = cast<CallInst>(I);
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002597 FunctionType *FTy = CI.getFunctionType();
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002598
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002599 if (CI.hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002600 writeOperandBundles(&CI, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002601
Chris Lattnerc44070802011-06-17 18:17:37 +00002602 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002603
Gabor Greife9afee22010-06-26 09:35:09 +00002604 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002605
Teresa Johnson37687f32016-04-23 04:30:47 +00002606 unsigned Flags = getOptimizationFlags(&I);
Akira Hatanaka97cb3972015-11-07 02:48:49 +00002607 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2608 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2609 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2610 1 << bitc::CALL_EXPLICIT_TYPE |
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002611 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2612 unsigned(Flags != 0) << bitc::CALL_FMF);
2613 if (Flags != 0)
2614 Vals.push_back(Flags);
2615
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002616 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002617 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002618
Chris Lattner0a603252007-05-01 02:13:26 +00002619 // Emit value #'s for the fixed parameters.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002620 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2621 // Check for labels (can happen with asm labels).
2622 if (FTy->getParamType(i)->isLabelTy())
2623 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2624 else
Teresa Johnson37687f32016-04-23 04:30:47 +00002625 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002626 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002627
Chris Lattnerb811e952007-05-01 07:03:37 +00002628 // Emit type/value pairs for varargs params.
2629 if (FTy->isVarArg()) {
Gabor Greife9afee22010-06-26 09:35:09 +00002630 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002631 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002632 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
Chris Lattner0a603252007-05-01 02:13:26 +00002633 }
2634 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002635 }
Chris Lattner0a603252007-05-01 02:13:26 +00002636 case Instruction::VAArg:
2637 Code = bitc::FUNC_CODE_INST_VAARG;
2638 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
Teresa Johnson37687f32016-04-23 04:30:47 +00002639 pushValue(I.getOperand(0), InstID, Vals); // valist.
Chris Lattner0a603252007-05-01 02:13:26 +00002640 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2641 break;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002642 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002643
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002644 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002645 Vals.clear();
2646}
2647
Teresa Johnson37687f32016-04-23 04:30:47 +00002648/// Emit names for globals/functions etc. \p IsModuleLevel is true when
2649/// we are writing the module-level VST, where we are including a function
2650/// bitcode index and need to backpatch the VST forward declaration record.
2651void ModuleBitcodeWriter::writeValueSymbolTable(
2652 const ValueSymbolTable &VST, bool IsModuleLevel,
2653 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002654 if (VST.empty()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002655 // writeValueSymbolTableForwardDecl should have returned early as
Teresa Johnsonff642b92015-09-17 20:12:00 +00002656 // well. Ensure this handling remains in sync by asserting that
2657 // the placeholder offset is not set.
Teresa Johnson37687f32016-04-23 04:30:47 +00002658 assert(!IsModuleLevel || !hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002659 return;
2660 }
2661
Teresa Johnson37687f32016-04-23 04:30:47 +00002662 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002663 // Get the offset of the VST we are writing, and backpatch it into
2664 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002665 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002666 // The BitcodeStartBit was the stream offset of the actual bitcode
2667 // (e.g. excluding any initial darwin header).
Teresa Johnson37687f32016-04-23 04:30:47 +00002668 VSTOffset -= bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002669 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002670 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002671 }
2672
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002673 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2eae59f2007-05-04 20:34:50 +00002674
Teresa Johnsonff642b92015-09-17 20:12:00 +00002675 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2676 // records, which are not used in the per-function VSTs.
2677 unsigned FnEntry8BitAbbrev;
2678 unsigned FnEntry7BitAbbrev;
2679 unsigned FnEntry6BitAbbrev;
Teresa Johnson37687f32016-04-23 04:30:47 +00002680 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002681 // 8-bit fixed-width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002682 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2683 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002684 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2685 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002686 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2687 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002688 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002689
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002690 // 7-bit fixed width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002691 Abbv = new BitCodeAbbrev();
2692 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002693 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2694 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002695 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2696 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002697 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002698
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002699 // 6-bit char6 VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002700 Abbv = new BitCodeAbbrev();
2701 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002702 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2703 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002704 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2705 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002706 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002707 }
2708
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002709 // FIXME: Set up the abbrev, we know how many values there are!
2710 // FIXME: We know if the type names can use 7-bit ascii.
2711 SmallVector<unsigned, 64> NameVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002712
Yaron Keren001e2e42015-08-10 07:04:29 +00002713 for (const ValueName &Name : VST) {
Chris Lattner4d925982007-05-04 20:52:02 +00002714 // Figure out the encoding to use for the name.
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002715 StringEncoding Bits =
2716 getStringEncoding(Name.getKeyData(), Name.getKeyLength());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002717
Chris Lattnera0987962007-05-04 21:31:13 +00002718 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002719 NameVals.push_back(VE.getValueID(Name.getValue()));
2720
2721 Function *F = dyn_cast<Function>(Name.getValue());
2722 if (!F) {
2723 // If value is an alias, need to get the aliased base object to
2724 // see if it is a function.
2725 auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2726 if (GA && GA->getBaseObject())
2727 F = dyn_cast<Function>(GA->getBaseObject());
2728 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002729
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002730 // VST_CODE_ENTRY: [valueid, namechar x N]
2731 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
2732 // VST_CODE_BBENTRY: [bbid, namechar x N]
Chris Lattner6be58c62007-05-03 22:18:21 +00002733 unsigned Code;
Yaron Keren001e2e42015-08-10 07:04:29 +00002734 if (isa<BasicBlock>(Name.getValue())) {
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002735 Code = bitc::VST_CODE_BBENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002736 if (Bits == SE_Char6)
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002737 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002738 } else if (F && !F->isDeclaration()) {
2739 // Must be the module-level VST, where we pass in the Index and
2740 // have a VSTOffsetPlaceholder. The function-level VST should not
2741 // contain any Function symbols.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00002742 assert(FunctionToBitcodeIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00002743 assert(hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002744
2745 // Save the word offset of the function (from the start of the
2746 // actual bitcode written to the stream).
Teresa Johnson37687f32016-04-23 04:30:47 +00002747 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002748 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2749 NameVals.push_back(BitcodeIndex / 32);
2750
2751 Code = bitc::VST_CODE_FNENTRY;
2752 AbbrevToUse = FnEntry8BitAbbrev;
2753 if (Bits == SE_Char6)
2754 AbbrevToUse = FnEntry6BitAbbrev;
2755 else if (Bits == SE_Fixed7)
2756 AbbrevToUse = FnEntry7BitAbbrev;
Chris Lattner6be58c62007-05-03 22:18:21 +00002757 } else {
2758 Code = bitc::VST_CODE_ENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002759 if (Bits == SE_Char6)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002760 AbbrevToUse = VST_ENTRY_6_ABBREV;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002761 else if (Bits == SE_Fixed7)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002762 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002763 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002764
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002765 for (const auto P : Name.getKey())
2766 NameVals.push_back((unsigned char)P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002767
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002768 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002769 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002770 NameVals.clear();
2771 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002772 Stream.ExitBlock();
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002773}
2774
Teresa Johnson403a7872015-10-04 14:33:43 +00002775/// Emit function names and summary offsets for the combined index
2776/// used by ThinLTO.
Teresa Johnson37687f32016-04-23 04:30:47 +00002777void IndexBitcodeWriter::writeCombinedValueSymbolTable() {
2778 assert(hasVSTOffsetPlaceholder() && "Expected non-zero VSTOffsetPlaceholder");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002779 // Get the offset of the VST we are writing, and backpatch it into
2780 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002781 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002782 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002783 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002784
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002785 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00002786
Teresa Johnson403a7872015-10-04 14:33:43 +00002787 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002788 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2790 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002791 unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002792
Teresa Johnsone1164de2016-02-10 21:55:02 +00002793 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson37687f32016-04-23 04:30:47 +00002794 for (const auto &GVI : valueIds()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002795 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
2796 NameVals.push_back(GVI.second);
2797 NameVals.push_back(GVI.first);
2798
2799 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002800 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002801 NameVals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00002802 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002803 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002804}
2805
Teresa Johnson37687f32016-04-23 04:30:47 +00002806void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002807 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2808 unsigned Code;
2809 if (isa<BasicBlock>(Order.V))
2810 Code = bitc::USELIST_CODE_BB;
2811 else
2812 Code = bitc::USELIST_CODE_DEFAULT;
2813
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002814 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002815 Record.push_back(VE.getValueID(Order.V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002816 Stream.EmitRecord(Code, Record);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002817}
2818
Teresa Johnson37687f32016-04-23 04:30:47 +00002819void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002820 assert(VE.shouldPreserveUseListOrder() &&
2821 "Expected to be preserving use-list order");
2822
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002823 auto hasMore = [&]() {
2824 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2825 };
2826 if (!hasMore())
2827 // Nothing to do.
2828 return;
2829
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002830 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002831 while (hasMore()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002832 writeUseList(std::move(VE.UseListOrders.back()));
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002833 VE.UseListOrders.pop_back();
2834 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002835 Stream.ExitBlock();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002836}
2837
Teresa Johnson403a7872015-10-04 14:33:43 +00002838/// Emit a function body to the module stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002839void ModuleBitcodeWriter::writeFunction(
2840 const Function &F,
2841 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002842 // Save the bitcode index of the start of this function block for recording
2843 // in the VST.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002844 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002845
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002846 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a11b642011-06-03 17:02:19 +00002847 VE.incorporateFunction(F);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002848
2849 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002850
Chris Lattnere6e364c2007-04-26 05:53:54 +00002851 // Emit the number of basic blocks, so the reader can create them ahead of
2852 // time.
2853 Vals.push_back(VE.getBasicBlocks().size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002854 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002855 Vals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002856
Chris Lattnere6e364c2007-04-26 05:53:54 +00002857 // If there are function-local constants, emit them now.
2858 unsigned CstStart, CstEnd;
2859 VE.getFunctionConstantRange(CstStart, CstEnd);
Teresa Johnson37687f32016-04-23 04:30:47 +00002860 writeConstants(CstStart, CstEnd, false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002861
Victor Hernandez6c730de2010-01-14 01:50:08 +00002862 // If there is function-local metadata, emit it now.
Teresa Johnson37687f32016-04-23 04:30:47 +00002863 writeFunctionMetadata(F);
Victor Hernandez6c730de2010-01-14 01:50:08 +00002864
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002865 // Keep a running idea of what the instruction ID is.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002866 unsigned InstID = CstEnd;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002867
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002868 bool NeedsMetadataAttachment = F.hasMetadata();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002869
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002870 DILocation *LastDL = nullptr;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002871 // Finally, emit all the instructions, in order.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002872 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002873 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2874 I != E; ++I) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002875 writeInstruction(*I, InstID, Vals);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002876
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00002877 if (!I->getType()->isVoidTy())
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002878 ++InstID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002879
Chris Lattner07d09ed2010-04-03 02:17:50 +00002880 // If the instruction has metadata, write a metadata attachment later.
2881 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002882
Chris Lattner07d09ed2010-04-03 02:17:50 +00002883 // If the instruction has a debug location, emit it.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002884 DILocation *DL = I->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002885 if (!DL)
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002886 continue;
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002887
2888 if (DL == LastDL) {
Chris Lattner07d09ed2010-04-03 02:17:50 +00002889 // Just repeat the same debug loc as last time.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002890 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002891 continue;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002892 }
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002893
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002894 Vals.push_back(DL->getLine());
2895 Vals.push_back(DL->getColumn());
2896 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2897 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002898 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002899 Vals.clear();
Duncan P. N. Exon Smith538ef562015-05-06 22:51:12 +00002900
2901 LastDL = DL;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002902 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002903
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002904 // Emit names for all the instructions etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002905 writeValueSymbolTable(F.getValueSymbolTable());
Devang Patelaf206b82009-09-18 19:26:43 +00002906
Chris Lattner07d09ed2010-04-03 02:17:50 +00002907 if (NeedsMetadataAttachment)
Peter Collingbournecceae7f2016-05-31 23:01:54 +00002908 writeFunctionMetadataAttachment(F);
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002909 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00002910 writeUseListBlock(&F);
Chad Rosier6a11b642011-06-03 17:02:19 +00002911 VE.purgeFunction();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002912 Stream.ExitBlock();
Chris Lattner831d4202007-04-26 03:27:58 +00002913}
2914
Chris Lattner702658c2007-05-04 18:26:27 +00002915// Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002916void ModuleBitcodeWriter::writeBlockInfo() {
Chris Lattner702658c2007-05-04 18:26:27 +00002917 // We only want to emit block info records for blocks that have multiple
Jan Wen Voungafaced02012-10-11 20:20:40 +00002918 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
Jan Wen Voung8c9e9412012-10-11 21:45:16 +00002919 // Other blocks can define their abbrevs inline.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002920 Stream.EnterBlockInfoBlock(2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002921
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002922 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002923 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2924 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2925 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2927 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002928 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002929 VST_ENTRY_8_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002930 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002931 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002932
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002933 { // 7-bit fixed width VST_CODE_ENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002934 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2935 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2936 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2937 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2938 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002939 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002940 VST_ENTRY_7_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002941 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002942 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002943 { // 6-bit char6 VST_CODE_ENTRY strings.
Chris Lattnere760d6f2007-05-05 01:26:50 +00002944 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2945 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2946 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2947 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2948 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002949 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002950 VST_ENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002951 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere760d6f2007-05-05 01:26:50 +00002952 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002953 { // 6-bit char6 VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002954 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2955 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2956 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnere760d6f2007-05-05 01:26:50 +00002958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002959 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002960 VST_BBENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002961 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002962 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002963
2964
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002965
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002966 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2967 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2968 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2969 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
David Blaikie7b028102015-02-25 00:51:52 +00002970 VE.computeBitsRequiredForTypeIndicies()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002971 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002972 CONSTANTS_SETTYPE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002973 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002974 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002975
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002976 { // INTEGER abbrev for CONSTANTS_BLOCK.
2977 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2978 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2979 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002980 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002981 CONSTANTS_INTEGER_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002982 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002983 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002984
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002985 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2986 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2987 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2988 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2989 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
David Blaikie7b028102015-02-25 00:51:52 +00002990 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002991 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2992
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002993 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002994 CONSTANTS_CE_CAST_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002995 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002996 }
2997 { // NULL abbrev for CONSTANTS_BLOCK.
2998 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2999 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003000 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003001 CONSTANTS_NULL_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003002 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003003 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003004
Chris Lattnerb80751d2007-05-05 07:44:49 +00003005 // FIXME: This should only use space for first class types!
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003006
Chris Lattnerb80751d2007-05-05 07:44:49 +00003007 { // INST_LOAD abbrev for FUNCTION_BLOCK.
3008 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3009 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattnerb80751d2007-05-05 07:44:49 +00003010 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
David Blaikie85035652015-02-25 01:07:20 +00003011 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3012 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerb80751d2007-05-05 07:44:49 +00003013 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3014 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003015 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003016 FUNCTION_INST_LOAD_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003017 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerb80751d2007-05-05 07:44:49 +00003018 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003019 { // INST_BINOP abbrev for FUNCTION_BLOCK.
3020 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3021 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3022 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3023 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3024 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003025 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003026 FUNCTION_INST_BINOP_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003027 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003028 }
Dan Gohman0ebd6962009-07-20 21:19:07 +00003029 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3030 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3031 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3032 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3033 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3034 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3035 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003036 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003037 FUNCTION_INST_BINOP_FLAGS_ABBREV)
Dan Gohman0ebd6962009-07-20 21:19:07 +00003038 llvm_unreachable("Unexpected abbrev ordering!");
3039 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003040 { // INST_CAST abbrev for FUNCTION_BLOCK.
3041 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3042 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
3043 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3044 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
David Blaikie7b028102015-02-25 00:51:52 +00003045 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003046 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003047 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003048 FUNCTION_INST_CAST_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003049 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003050 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003051
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003052 { // INST_RET abbrev for FUNCTION_BLOCK.
3053 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3054 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003055 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003056 FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003057 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003058 }
3059 { // INST_RET abbrev for FUNCTION_BLOCK.
3060 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3061 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
3062 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003063 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003064 FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003065 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003066 }
3067 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3068 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3069 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003070 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003071 FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003072 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003073 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00003074 {
3075 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3076 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
3077 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
3078 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3079 Log2_32_Ceil(VE.getTypes().size() + 1)));
3080 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3081 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003082 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
David Blaikieb5b5efd2015-02-25 01:08:52 +00003083 FUNCTION_INST_GEP_ABBREV)
3084 llvm_unreachable("Unexpected abbrev ordering!");
3085 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003086
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003087 Stream.ExitBlock();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003088}
3089
Teresa Johnson403a7872015-10-04 14:33:43 +00003090/// Write the module path strings, currently only used when generating
3091/// a combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003092void IndexBitcodeWriter::writeModStrings() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003093 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00003094
3095 // TODO: See which abbrev sizes we actually need to emit
3096
3097 // 8-bit fixed-width MST_ENTRY strings.
3098 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3099 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3100 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3101 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3102 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003103 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003104
3105 // 7-bit fixed width MST_ENTRY strings.
3106 Abbv = new BitCodeAbbrev();
3107 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3108 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3109 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3110 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003111 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003112
3113 // 6-bit char6 MST_ENTRY strings.
3114 Abbv = new BitCodeAbbrev();
3115 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3116 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3117 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3118 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003119 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003120
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003121 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3122 Abbv = new BitCodeAbbrev();
3123 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
3124 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3126 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3127 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3128 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003129 unsigned AbbrevHash = Stream.EmitAbbrev(Abbv);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003130
3131 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003132 for (const auto &MPSE : Index.modulePaths()) {
Teresa Johnson84174c32016-05-10 13:48:23 +00003133 if (!doIncludeModule(MPSE.getKey()))
3134 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00003135 StringEncoding Bits =
3136 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
3137 unsigned AbbrevToUse = Abbrev8Bit;
3138 if (Bits == SE_Char6)
3139 AbbrevToUse = Abbrev6Bit;
3140 else if (Bits == SE_Fixed7)
3141 AbbrevToUse = Abbrev7Bit;
3142
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003143 Vals.push_back(MPSE.getValue().first);
Teresa Johnson403a7872015-10-04 14:33:43 +00003144
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003145 for (const auto P : MPSE.getKey())
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003146 Vals.push_back((unsigned char)P);
Teresa Johnson403a7872015-10-04 14:33:43 +00003147
3148 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003149 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003150
3151 Vals.clear();
3152 // Emit an optional hash for the module now
3153 auto &Hash = MPSE.getValue().second;
3154 bool AllZero = true; // Detect if the hash is empty, and do not generate it
3155 for (auto Val : Hash) {
3156 if (Val)
3157 AllZero = false;
3158 Vals.push_back(Val);
3159 }
3160 if (!AllZero) {
3161 // Emit the hash record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003162 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003163 }
3164
3165 Vals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00003166 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003167 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003168}
3169
3170// Helper to emit a single function summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003171void ModuleBitcodeWriter::writePerModuleFunctionSummaryRecord(
Teresa Johnson28e457b2016-04-24 14:57:11 +00003172 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +00003173 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3174 const Function &F) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003175 NameVals.push_back(ValueID);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003176
Teresa Johnson28e457b2016-04-24 14:57:11 +00003177 FunctionSummary *FS = cast<FunctionSummary>(Summary);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003178 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003179 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003180 NameVals.push_back(FS->refs().size());
3181
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003182 unsigned SizeBeforeRefs = NameVals.size();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003183 for (auto &RI : FS->refs())
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003184 NameVals.push_back(VE.getValueID(RI.getValue()));
3185 // Sort the refs for determinism output, the vector returned by FS->refs() has
3186 // been initialized from a DenseSet.
3187 std::sort(NameVals.begin() + SizeBeforeRefs, NameVals.end());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003188
Mehdi Amini367577a2016-05-16 09:04:55 +00003189 std::vector<FunctionSummary::EdgeTy> Calls = FS->calls();
3190 std::sort(Calls.begin(), Calls.end(),
3191 [this](const FunctionSummary::EdgeTy &L,
3192 const FunctionSummary::EdgeTy &R) {
3193 return VE.getValueID(L.first.getValue()) <
3194 VE.getValueID(R.first.getValue());
3195 });
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003196 bool HasProfileData = F.getEntryCount().hasValue();
Mehdi Amini367577a2016-05-16 09:04:55 +00003197 for (auto &ECI : Calls) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003198 NameVals.push_back(VE.getValueID(ECI.first.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003199 assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite");
3200 NameVals.push_back(ECI.second.CallsiteCount);
3201 if (HasProfileData)
3202 NameVals.push_back(ECI.second.ProfileCount);
3203 }
3204
3205 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3206 unsigned Code =
3207 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
Teresa Johnson403a7872015-10-04 14:33:43 +00003208
3209 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003210 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003211 NameVals.clear();
3212}
3213
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003214// Collect the global value references in the given variable's initializer,
3215// and emit them in a summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003216void ModuleBitcodeWriter::writeModuleLevelReferences(
3217 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3218 unsigned FSModRefsAbbrev) {
Teresa Johnson13968092016-03-15 19:35:45 +00003219 // Only interested in recording variable defs in the summary.
3220 if (V.isDeclaration())
3221 return;
Teresa Johnson13968092016-03-15 19:35:45 +00003222 NameVals.push_back(VE.getValueID(&V));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003223 NameVals.push_back(getEncodedGVSummaryFlags(V));
Teresa Johnson28e457b2016-04-24 14:57:11 +00003224 auto *Summary = Index->getGlobalValueSummary(V);
3225 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003226
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003227 unsigned SizeBeforeRefs = NameVals.size();
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003228 for (auto &RI : VS->refs())
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003229 NameVals.push_back(VE.getValueID(RI.getValue()));
3230 // Sort the refs for determinism output, the vector returned by FS->refs() has
3231 // been initialized from a DenseSet.
3232 std::sort(NameVals.begin() + SizeBeforeRefs, NameVals.end());
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003233
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003234 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3235 FSModRefsAbbrev);
Teresa Johnson13968092016-03-15 19:35:45 +00003236 NameVals.clear();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003237}
Teresa Johnson403a7872015-10-04 14:33:43 +00003238
Mehdi Amini8fe69362016-04-24 03:18:11 +00003239// Current version for the summary.
3240// This is bumped whenever we introduce changes in the way some record are
3241// interpreted, like flags for instance.
3242static const uint64_t INDEX_VERSION = 1;
3243
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003244/// Emit the per-module summary section alongside the rest of
3245/// the module's bitcode.
Teresa Johnson37687f32016-04-23 04:30:47 +00003246void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003247 if (M.empty())
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003248 return;
3249
Teresa Johnson37687f32016-04-23 04:30:47 +00003250 if (Index->begin() == Index->end())
Teresa Johnsonb35cc692016-04-20 14:39:45 +00003251 return;
3252
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003253 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 4);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003254
Mehdi Amini8fe69362016-04-24 03:18:11 +00003255 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3256
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003257 // Abbrev for FS_PERMODULE.
Teresa Johnson403a7872015-10-04 14:33:43 +00003258 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003259 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003260 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003261 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003262 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003263 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3264 // numrefs x valueid, n x (valueid, callsitecount)
3265 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3266 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003267 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003268
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003269 // Abbrev for FS_PERMODULE_PROFILE.
3270 Abbv = new BitCodeAbbrev();
3271 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3272 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003273 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003274 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3275 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3276 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3277 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3278 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003279 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003280
3281 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3282 Abbv = new BitCodeAbbrev();
3283 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3284 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003285 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003286 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3287 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003288 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003289
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003290 // Abbrev for FS_ALIAS.
3291 Abbv = new BitCodeAbbrev();
3292 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3293 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003294 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003295 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003296 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003297
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003298 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003299 // Iterate over the list of functions instead of the Index to
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003300 // ensure the ordering is stable.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003301 for (const Function &F : M) {
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003302 if (F.isDeclaration())
3303 continue;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003304 // Summary emission does not support anonymous functions, they have to
3305 // renamed using the anonymous function renaming pass.
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003306 if (!F.hasName())
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003307 report_fatal_error("Unexpected anonymous function when writing summary");
Teresa Johnson403a7872015-10-04 14:33:43 +00003308
Teresa Johnson28e457b2016-04-24 14:57:11 +00003309 auto *Summary = Index->getGlobalValueSummary(F);
Peter Collingbourne832e7fa2016-05-06 02:41:23 +00003310 writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
3311 FSCallsAbbrev, FSCallsProfileAbbrev, F);
Teresa Johnson403a7872015-10-04 14:33:43 +00003312 }
3313
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003314 // Capture references from GlobalVariable initializers, which are outside
3315 // of a function scope.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003316 for (const GlobalVariable &G : M.globals())
Teresa Johnson37687f32016-04-23 04:30:47 +00003317 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003318
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003319 for (const GlobalAlias &A : M.aliases()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003320 auto *Aliasee = A.getBaseObject();
3321 if (!Aliasee->hasName())
3322 // Nameless function don't have an entry in the summary, skip it.
3323 continue;
3324 auto AliasId = VE.getValueID(&A);
3325 auto AliaseeId = VE.getValueID(Aliasee);
3326 NameVals.push_back(AliasId);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003327 NameVals.push_back(getEncodedGVSummaryFlags(A));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003328 NameVals.push_back(AliaseeId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003329 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003330 NameVals.clear();
3331 }
3332
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003333 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003334}
3335
Teresa Johnson26ab5772016-03-15 00:04:37 +00003336/// Emit the combined summary section into the combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003337void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003338 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Mehdi Amini8fe69362016-04-24 03:18:11 +00003339 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
Teresa Johnson403a7872015-10-04 14:33:43 +00003340
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003341 // Abbrev for FS_COMBINED.
Teresa Johnson403a7872015-10-04 14:33:43 +00003342 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003343 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
Teresa Johnson02e98332016-04-27 13:28:35 +00003344 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003345 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003346 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003347 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3348 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3349 // numrefs x valueid, n x (valueid, callsitecount)
3350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003352 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003353
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003354 // Abbrev for FS_COMBINED_PROFILE.
3355 Abbv = new BitCodeAbbrev();
3356 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
Teresa Johnson02e98332016-04-27 13:28:35 +00003357 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003358 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003359 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003360 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3361 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3362 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3363 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3364 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003365 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003366
3367 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
3368 Abbv = new BitCodeAbbrev();
3369 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003373 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3374 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003375 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003376
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003377 // Abbrev for FS_COMBINED_ALIAS.
3378 Abbv = new BitCodeAbbrev();
3379 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003380 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003381 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003382 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson02e98332016-04-27 13:28:35 +00003383 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003384 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003385
Teresa Johnson02e98332016-04-27 13:28:35 +00003386 // The aliases are emitted as a post-pass, and will point to the value
3387 // id of the aliasee. Save them in a vector for post-processing.
Teresa Johnson28e457b2016-04-24 14:57:11 +00003388 SmallVector<AliasSummary *, 64> Aliases;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003389
Teresa Johnson02e98332016-04-27 13:28:35 +00003390 // Save the value id for each summary for alias emission.
3391 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
3392
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003393 SmallVector<uint64_t, 64> NameVals;
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003394
3395 // For local linkage, we also emit the original name separately
3396 // immediately after the record.
3397 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3398 if (!GlobalValue::isLocalLinkage(S.linkage()))
3399 return;
3400 NameVals.push_back(S.getOriginalName());
3401 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3402 NameVals.clear();
3403 };
3404
Teresa Johnson84174c32016-05-10 13:48:23 +00003405 for (const auto &I : *this) {
3406 GlobalValueSummary *S = I.second;
3407 assert(S);
Teresa Johnson02e98332016-04-27 13:28:35 +00003408
Teresa Johnson84174c32016-05-10 13:48:23 +00003409 assert(hasValueId(I.first));
3410 unsigned ValueId = getValueId(I.first);
3411 SummaryToValueIdMap[S] = ValueId;
Teresa Johnson02e98332016-04-27 13:28:35 +00003412
Teresa Johnson84174c32016-05-10 13:48:23 +00003413 if (auto *AS = dyn_cast<AliasSummary>(S)) {
3414 // Will process aliases as a post-pass because the reader wants all
3415 // global to be loaded first.
3416 Aliases.push_back(AS);
3417 continue;
3418 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003419
Teresa Johnson84174c32016-05-10 13:48:23 +00003420 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003421 NameVals.push_back(ValueId);
Teresa Johnson84174c32016-05-10 13:48:23 +00003422 NameVals.push_back(Index.getModuleId(VS->modulePath()));
3423 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
3424 for (auto &RI : VS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003425 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003426 }
3427
Teresa Johnson403a7872015-10-04 14:33:43 +00003428 // Emit the finished record.
Teresa Johnson84174c32016-05-10 13:48:23 +00003429 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3430 FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003431 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003432 MaybeEmitOriginalName(*S);
Teresa Johnson84174c32016-05-10 13:48:23 +00003433 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00003434 }
Teresa Johnson84174c32016-05-10 13:48:23 +00003435
3436 auto *FS = cast<FunctionSummary>(S);
3437 NameVals.push_back(ValueId);
3438 NameVals.push_back(Index.getModuleId(FS->modulePath()));
3439 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
3440 NameVals.push_back(FS->instCount());
3441 NameVals.push_back(FS->refs().size());
3442
3443 for (auto &RI : FS->refs()) {
3444 NameVals.push_back(getValueId(RI.getGUID()));
3445 }
3446
3447 bool HasProfileData = false;
3448 for (auto &EI : FS->calls()) {
3449 HasProfileData |= EI.second.ProfileCount != 0;
3450 if (HasProfileData)
3451 break;
3452 }
3453
3454 for (auto &EI : FS->calls()) {
3455 // If this GUID doesn't have a value id, it doesn't have a function
3456 // summary and we don't need to record any calls to it.
3457 if (!hasValueId(EI.first.getGUID()))
3458 continue;
3459 NameVals.push_back(getValueId(EI.first.getGUID()));
3460 assert(EI.second.CallsiteCount > 0 && "Expected at least one callsite");
3461 NameVals.push_back(EI.second.CallsiteCount);
3462 if (HasProfileData)
3463 NameVals.push_back(EI.second.ProfileCount);
3464 }
3465
3466 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3467 unsigned Code =
3468 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
3469
3470 // Emit the finished record.
3471 Stream.EmitRecord(Code, NameVals, FSAbbrev);
3472 NameVals.clear();
3473 MaybeEmitOriginalName(*S);
Teresa Johnson403a7872015-10-04 14:33:43 +00003474 }
3475
Teresa Johnson28e457b2016-04-24 14:57:11 +00003476 for (auto *AS : Aliases) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003477 auto AliasValueId = SummaryToValueIdMap[AS];
3478 assert(AliasValueId);
3479 NameVals.push_back(AliasValueId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003480 NameVals.push_back(Index.getModuleId(AS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003481 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
Teresa Johnson02e98332016-04-27 13:28:35 +00003482 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
3483 assert(AliaseeValueId);
3484 NameVals.push_back(AliaseeValueId);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003485
3486 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003487 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003488 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003489 MaybeEmitOriginalName(*AS);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003490 }
3491
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003492 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003493}
3494
Teresa Johnson37687f32016-04-23 04:30:47 +00003495void ModuleBitcodeWriter::writeIdentificationBlock() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003496 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
Mehdi Amini5d303282015-10-26 18:37:00 +00003497
3498 // Write the "user readable" string identifying the bitcode producer
3499 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3500 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3501 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3502 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003503 auto StringAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson37687f32016-04-23 04:30:47 +00003504 writeStringRecord(bitc::IDENTIFICATION_CODE_STRING,
3505 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
Mehdi Amini5d303282015-10-26 18:37:00 +00003506
3507 // Write the epoch version
3508 Abbv = new BitCodeAbbrev();
3509 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3510 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003511 auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini5d303282015-10-26 18:37:00 +00003512 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003513 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3514 Stream.ExitBlock();
Mehdi Amini5d303282015-10-26 18:37:00 +00003515}
3516
Teresa Johnson37687f32016-04-23 04:30:47 +00003517void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003518 // Emit the module's hash.
3519 // MODULE_CODE_HASH: [5*i32]
3520 SHA1 Hasher;
Sjoerd Meijer41beee62016-04-27 18:35:02 +00003521 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003522 Buffer.size() - BlockStartPos));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003523 auto Hash = Hasher.result();
3524 SmallVector<uint64_t, 20> Vals;
3525 auto LShift = [&](unsigned char Val, unsigned Amount)
3526 -> uint64_t { return ((uint64_t)Val) << Amount; };
3527 for (int Pos = 0; Pos < 20; Pos += 4) {
3528 uint32_t SubHash = LShift(Hash[Pos + 0], 24);
3529 SubHash |= LShift(Hash[Pos + 1], 16) | LShift(Hash[Pos + 2], 8) |
3530 (unsigned)(unsigned char)Hash[Pos + 3];
3531 Vals.push_back(SubHash);
3532 }
3533
3534 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003535 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003536}
3537
Teresa Johnson37687f32016-04-23 04:30:47 +00003538void BitcodeWriter::write() {
3539 // Emit the file header first.
3540 writeBitcodeHeader();
3541
3542 writeBlocks();
3543}
3544
3545void ModuleBitcodeWriter::writeBlocks() {
3546 writeIdentificationBlock();
3547 writeModule();
3548}
3549
3550void IndexBitcodeWriter::writeBlocks() {
3551 // Index contains only a single outer (module) block.
3552 writeIndex();
3553}
3554
3555void ModuleBitcodeWriter::writeModule() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003556 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3557 size_t BlockStartPos = Buffer.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003558
Jan Wen Voungafaced02012-10-11 20:20:40 +00003559 SmallVector<unsigned, 1> Vals;
3560 unsigned CurVersion = 1;
3561 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003562 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003563
3564 // Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003565 writeBlockInfo();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003566
Bill Wendlingdc095552013-02-10 23:09:32 +00003567 // Emit information about attribute groups.
Teresa Johnson37687f32016-04-23 04:30:47 +00003568 writeAttributeGroupTable();
Bill Wendlingdc095552013-02-10 23:09:32 +00003569
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003570 // Emit information about parameter attributes.
Teresa Johnson37687f32016-04-23 04:30:47 +00003571 writeAttributeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003572
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003573 // Emit information describing all of the types in the module.
Teresa Johnson37687f32016-04-23 04:30:47 +00003574 writeTypeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003575
Teresa Johnson37687f32016-04-23 04:30:47 +00003576 writeComdats();
David Majnemerdad0a642014-06-27 18:19:56 +00003577
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003578 // Emit top-level description of module, including target triple, inline asm,
3579 // descriptors for global variables, and function prototype info.
Teresa Johnson37687f32016-04-23 04:30:47 +00003580 writeModuleInfo();
Devang Patel7428d8a2009-07-22 17:43:22 +00003581
Devang Patele059ba6e2009-07-23 01:07:34 +00003582 // Emit constants.
Teresa Johnson37687f32016-04-23 04:30:47 +00003583 writeModuleConstants();
Devang Patele059ba6e2009-07-23 01:07:34 +00003584
Devang Patel8cca7b42009-08-04 05:01:35 +00003585 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003586 writeModuleMetadata();
Devang Patel8cca7b42009-08-04 05:01:35 +00003587
Devang Patelaf206b82009-09-18 19:26:43 +00003588 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003589 writeModuleMetadataStore();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003590
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003591 // Emit module-level use-lists.
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003592 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003593 writeUseListBlock(nullptr);
Chad Rosierca2567b2011-12-07 21:44:12 +00003594
Teresa Johnson37687f32016-04-23 04:30:47 +00003595 writeOperandBundleTags();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003596
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003597 // Emit function bodies.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003598 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003599 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003600 if (!F->isDeclaration())
Teresa Johnson37687f32016-04-23 04:30:47 +00003601 writeFunction(*F, FunctionToBitcodeIndex);
Teresa Johnson403a7872015-10-04 14:33:43 +00003602
3603 // Need to write after the above call to WriteFunction which populates
3604 // the summary information in the index.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003605 if (Index)
Teresa Johnson37687f32016-04-23 04:30:47 +00003606 writePerModuleGlobalValueSummary();
Teresa Johnsonff642b92015-09-17 20:12:00 +00003607
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003608 writeValueSymbolTable(M.getValueSymbolTable(),
Teresa Johnson37687f32016-04-23 04:30:47 +00003609 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003610
Peter Collingbournecceae7f2016-05-31 23:01:54 +00003611 for (const GlobalVariable &GV : M.globals())
3612 if (GV.hasMetadata()) {
3613 SmallVector<uint64_t, 4> Record;
3614 Record.push_back(VE.getValueID(&GV));
3615 pushGlobalMetadataAttachment(Record, GV);
3616 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR_ATTACHMENT, Record);
3617 }
3618
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003619 if (GenerateHash) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003620 writeModuleHash(BlockStartPos);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003621 }
3622
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003623 Stream.ExitBlock();
Chris Lattner702658c2007-05-04 18:26:27 +00003624}
3625
Teresa Johnson37687f32016-04-23 04:30:47 +00003626static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3627 uint32_t &Position) {
3628 support::endian::write32le(&Buffer[Position], Value);
3629 Position += 4;
3630}
3631
3632/// If generating a bc file on darwin, we have to emit a
Chris Lattnera660f4b2008-07-09 05:14:23 +00003633/// header and trailer to make it compatible with the system archiver. To do
3634/// this we emit the following header, and then emit a trailer that pads the
3635/// file out to be a multiple of 16 bytes.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003636///
Chris Lattnera660f4b2008-07-09 05:14:23 +00003637/// struct bc_header {
3638/// uint32_t Magic; // 0x0B17C0DE
3639/// uint32_t Version; // Version, currently always 0.
3640/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3641/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3642/// uint32_t CPUType; // CPU specifier.
3643/// ... potentially more later ...
3644/// };
Teresa Johnson37687f32016-04-23 04:30:47 +00003645static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003646 const Triple &TT) {
Chris Lattnera660f4b2008-07-09 05:14:23 +00003647 unsigned CPUType = ~0U;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003648
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003649 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng545d3602010-02-12 20:39:35 +00003650 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3651 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3652 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattnera660f4b2008-07-09 05:14:23 +00003653 enum {
3654 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3655 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003656 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattnera660f4b2008-07-09 05:14:23 +00003657 DARWIN_CPU_TYPE_POWERPC = 18
3658 };
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003659
Evan Chengcffdcae2011-06-14 01:51:33 +00003660 Triple::ArchType Arch = TT.getArch();
3661 if (Arch == Triple::x86_64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003662 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003663 else if (Arch == Triple::x86)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003664 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chengcffdcae2011-06-14 01:51:33 +00003665 else if (Arch == Triple::ppc)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003666 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chengcffdcae2011-06-14 01:51:33 +00003667 else if (Arch == Triple::ppc64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003668 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003669 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003670 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003671
Chris Lattnera660f4b2008-07-09 05:14:23 +00003672 // Traditional Bitcode starts after header.
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003673 assert(Buffer.size() >= BWH_HeaderSize &&
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003674 "Expected header size to be reserved");
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003675 unsigned BCOffset = BWH_HeaderSize;
3676 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003677
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003678 // Write the magic and version.
3679 unsigned Position = 0;
Teresa Johnson37687f32016-04-23 04:30:47 +00003680 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
3681 writeInt32ToBuffer(0, Buffer, Position); // Version.
3682 writeInt32ToBuffer(BCOffset, Buffer, Position);
3683 writeInt32ToBuffer(BCSize, Buffer, Position);
3684 writeInt32ToBuffer(CPUType, Buffer, Position);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003685
Chris Lattnera660f4b2008-07-09 05:14:23 +00003686 // If the file is not a multiple of 16 bytes, insert dummy padding.
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003687 while (Buffer.size() & 15)
3688 Buffer.push_back(0);
Chris Lattnera660f4b2008-07-09 05:14:23 +00003689}
3690
Teresa Johnson403a7872015-10-04 14:33:43 +00003691/// Helper to write the header common to all bitcode files.
Teresa Johnson37687f32016-04-23 04:30:47 +00003692void BitcodeWriter::writeBitcodeHeader() {
Teresa Johnson403a7872015-10-04 14:33:43 +00003693 // Emit the file header.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003694 Stream.Emit((unsigned)'B', 8);
3695 Stream.Emit((unsigned)'C', 8);
3696 Stream.Emit(0x0, 4);
3697 Stream.Emit(0xC, 4);
3698 Stream.Emit(0xE, 4);
3699 Stream.Emit(0xD, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00003700}
3701
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003702/// WriteBitcodeToFile - Write the specified module to the specified output
3703/// stream.
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +00003704void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
Teresa Johnson403a7872015-10-04 14:33:43 +00003705 bool ShouldPreserveUseListOrder,
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003706 const ModuleSummaryIndex *Index,
3707 bool GenerateHash) {
Michael Ilsemane26658d2012-12-03 21:29:36 +00003708 SmallVector<char, 0> Buffer;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003709 Buffer.reserve(256*1024);
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003710
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003711 // If this is darwin or another generic macho target, reserve space for the
3712 // header.
3713 Triple TT(M->getTargetTriple());
Akira Hatanaka1235d282016-01-23 16:02:10 +00003714 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003715 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003716
3717 // Emit the module into the buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003718 ModuleBitcodeWriter ModuleWriter(M, Buffer, ShouldPreserveUseListOrder, Index,
3719 GenerateHash);
Teresa Johnson37687f32016-04-23 04:30:47 +00003720 ModuleWriter.write();
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003721
Akira Hatanaka1235d282016-01-23 16:02:10 +00003722 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Teresa Johnson37687f32016-04-23 04:30:47 +00003723 emitDarwinBCHeaderAndTrailer(Buffer, TT);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003724
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003725 // Write the generated bitstream to "Out".
3726 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003727}
Teresa Johnson403a7872015-10-04 14:33:43 +00003728
Teresa Johnson37687f32016-04-23 04:30:47 +00003729void IndexBitcodeWriter::writeIndex() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003730 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Teresa Johnson37687f32016-04-23 04:30:47 +00003731
3732 SmallVector<unsigned, 1> Vals;
3733 unsigned CurVersion = 1;
3734 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003735 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Teresa Johnson37687f32016-04-23 04:30:47 +00003736
3737 // If we have a VST, write the VSTOFFSET record placeholder.
3738 writeValueSymbolTableForwardDecl();
3739
3740 // Write the module paths in the combined index.
3741 writeModStrings();
3742
3743 // Write the summary combined index records.
3744 writeCombinedGlobalValueSummary();
3745
3746 // Need a special VST writer for the combined index (we don't have a
3747 // real VST and real values when this is invoked).
3748 writeCombinedValueSymbolTable();
3749
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003750 Stream.ExitBlock();
Teresa Johnson37687f32016-04-23 04:30:47 +00003751}
3752
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003753// Write the specified module summary index to the given raw output stream,
Teresa Johnson403a7872015-10-04 14:33:43 +00003754// where it will be written in a new bitcode block. This is used when
Teresa Johnson84174c32016-05-10 13:48:23 +00003755// writing the combined index file for ThinLTO. When writing a subset of the
3756// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
3757void llvm::WriteIndexToFile(
3758 const ModuleSummaryIndex &Index, raw_ostream &Out,
3759 std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003760 SmallVector<char, 0> Buffer;
3761 Buffer.reserve(256 * 1024);
3762
Teresa Johnson84174c32016-05-10 13:48:23 +00003763 IndexBitcodeWriter IndexWriter(Buffer, Index, ModuleToSummariesForIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00003764 IndexWriter.write();
Teresa Johnson403a7872015-10-04 14:33:43 +00003765
3766 Out.write((char *)&Buffer.front(), Buffer.size());
3767}