blob: e69fbe66770b7ccfbd18990b4b39984d6d76d7e5 [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
Chris Lattner4d925982007-05-04 20:52:02 +000040/// These are manifest constants used by the bitcode writer. They do not need to
41/// be kept in sync with the reader, but need to be consistent within this file.
42enum {
Chris Lattner4d925982007-05-04 20:52:02 +000043 // VALUE_SYMTAB_BLOCK abbrev id's.
44 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerfd1ad102007-05-04 20:58:35 +000045 VST_ENTRY_7_ABBREV,
Chris Lattnere760d6f2007-05-05 01:26:50 +000046 VST_ENTRY_6_ABBREV,
Chris Lattnerda5e5d22007-05-05 07:36:14 +000047 VST_BBENTRY_6_ABBREV,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000048
Chris Lattnerda5e5d22007-05-05 07:36:14 +000049 // CONSTANTS_BLOCK abbrev id's.
50 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
51 CONSTANTS_INTEGER_ABBREV,
52 CONSTANTS_CE_CAST_Abbrev,
Chris Lattnerb80751d2007-05-05 07:44:49 +000053 CONSTANTS_NULL_Abbrev,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000054
Chris Lattnerb80751d2007-05-05 07:44:49 +000055 // FUNCTION_BLOCK abbrev id's.
Chris Lattnercc6d4c92007-05-06 01:28:01 +000056 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000057 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman0ebd6962009-07-20 21:19:07 +000058 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000059 FUNCTION_INST_CAST_ABBREV,
Chris Lattnercc6d4c92007-05-06 01:28:01 +000060 FUNCTION_INST_RET_VOID_ABBREV,
61 FUNCTION_INST_RET_VAL_ABBREV,
David Blaikieb5b5efd2015-02-25 01:08:52 +000062 FUNCTION_INST_UNREACHABLE_ABBREV,
63 FUNCTION_INST_GEP_ABBREV,
Chris Lattner4d925982007-05-04 20:52:02 +000064};
65
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000066/// Abstract class to manage the bitcode writing, subclassed for each bitcode
67/// file type. Owns the BitstreamWriter, and includes the main entry point for
Teresa Johnson37687f32016-04-23 04:30:47 +000068/// writing.
69class BitcodeWriter {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000070protected:
Teresa Johnson37687f32016-04-23 04:30:47 +000071 /// Pointer to the buffer allocated by caller for bitcode writing.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000072 const SmallVectorImpl<char> &Buffer;
Teresa Johnson37687f32016-04-23 04:30:47 +000073
74 /// The stream created and owned by the BitodeWriter.
75 BitstreamWriter Stream;
76
77 /// Saves the offset of the VSTOffset record that must eventually be
78 /// backpatched with the offset of the actual VST.
79 uint64_t VSTOffsetPlaceholder = 0;
80
81public:
82 /// Constructs a BitcodeWriter object, and initializes a BitstreamRecord,
83 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000084 BitcodeWriter(SmallVectorImpl<char> &Buffer)
85 : Buffer(Buffer), Stream(Buffer) {}
Teresa Johnson37687f32016-04-23 04:30:47 +000086
87 virtual ~BitcodeWriter() = default;
88
89 /// Main entry point to write the bitcode file, which writes the bitcode
90 /// header and will then invoke the virtual writeBlocks() method.
91 void write();
92
93private:
94 /// Derived classes must implement this to write the corresponding blocks for
95 /// that bitcode file type.
96 virtual void writeBlocks() = 0;
97
98protected:
99 bool hasVSTOffsetPlaceholder() { return VSTOffsetPlaceholder != 0; }
Teresa Johnson37687f32016-04-23 04:30:47 +0000100 void writeValueSymbolTableForwardDecl();
101 void writeBitcodeHeader();
102};
103
104/// Class to manage the bitcode writing for a module.
105class ModuleBitcodeWriter : public BitcodeWriter {
106 /// The Module to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000107 const Module &M;
Teresa Johnson37687f32016-04-23 04:30:47 +0000108
109 /// Enumerates ids for all values in the module.
110 ValueEnumerator VE;
111
112 /// Optional per-module index to write for ThinLTO.
113 const ModuleSummaryIndex *Index;
114
115 /// True if a module hash record should be written.
116 bool GenerateHash;
117
118 /// The start bit of the module block, for use in generating a module hash
119 uint64_t BitcodeStartBit = 0;
120
121public:
122 /// Constructs a ModuleBitcodeWriter object for the given Module,
123 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000124 ModuleBitcodeWriter(const Module *M, SmallVectorImpl<char> &Buffer,
Teresa Johnson37687f32016-04-23 04:30:47 +0000125 bool ShouldPreserveUseListOrder,
126 const ModuleSummaryIndex *Index, bool GenerateHash)
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000127 : BitcodeWriter(Buffer), M(*M), VE(*M, ShouldPreserveUseListOrder),
Teresa Johnson37687f32016-04-23 04:30:47 +0000128 Index(Index), GenerateHash(GenerateHash) {
129 // Save the start bit of the actual bitcode, in case there is space
130 // saved at the start for the darwin header above. The reader stream
131 // will start at the bitcode, and we need the offset of the VST
132 // to line up.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000133 BitcodeStartBit = Stream.GetCurrentBitNo();
Teresa Johnson37687f32016-04-23 04:30:47 +0000134 }
135
136private:
137 /// Main entry point for writing a module to bitcode, invoked by
138 /// BitcodeWriter::write() after it writes the header.
139 void writeBlocks() override;
140
141 /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
142 /// current llvm version, and a record for the epoch number.
143 void writeIdentificationBlock();
144
145 /// Emit the current module to the bitstream.
146 void writeModule();
147
148 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
149
150 void writeStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse);
151 void writeAttributeGroupTable();
152 void writeAttributeTable();
153 void writeTypeTable();
154 void writeComdats();
155 void writeModuleInfo();
156 void writeValueAsMetadata(const ValueAsMetadata *MD,
157 SmallVectorImpl<uint64_t> &Record);
158 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
159 unsigned Abbrev);
160 unsigned createDILocationAbbrev();
161 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
162 unsigned &Abbrev);
163 unsigned createGenericDINodeAbbrev();
164 void writeGenericDINode(const GenericDINode *N,
165 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
166 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
167 unsigned Abbrev);
168 void writeDIEnumerator(const DIEnumerator *N,
169 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
170 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
171 unsigned Abbrev);
172 void writeDIDerivedType(const DIDerivedType *N,
173 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
174 void writeDICompositeType(const DICompositeType *N,
175 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
176 void writeDISubroutineType(const DISubroutineType *N,
177 SmallVectorImpl<uint64_t> &Record,
178 unsigned Abbrev);
179 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
180 unsigned Abbrev);
181 void writeDICompileUnit(const DICompileUnit *N,
182 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
183 void writeDISubprogram(const DISubprogram *N,
184 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
185 void writeDILexicalBlock(const DILexicalBlock *N,
186 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
187 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
188 SmallVectorImpl<uint64_t> &Record,
189 unsigned Abbrev);
190 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
191 unsigned Abbrev);
192 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
193 unsigned Abbrev);
194 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
195 unsigned Abbrev);
196 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
197 unsigned Abbrev);
198 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
199 SmallVectorImpl<uint64_t> &Record,
200 unsigned Abbrev);
201 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
202 SmallVectorImpl<uint64_t> &Record,
203 unsigned Abbrev);
204 void writeDIGlobalVariable(const DIGlobalVariable *N,
205 SmallVectorImpl<uint64_t> &Record,
206 unsigned Abbrev);
207 void writeDILocalVariable(const DILocalVariable *N,
208 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
209 void writeDIExpression(const DIExpression *N,
210 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
211 void writeDIObjCProperty(const DIObjCProperty *N,
212 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
213 void writeDIImportedEntity(const DIImportedEntity *N,
214 SmallVectorImpl<uint64_t> &Record,
215 unsigned Abbrev);
216 unsigned createNamedMetadataAbbrev();
217 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
218 unsigned createMetadataStringsAbbrev();
219 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
220 SmallVectorImpl<uint64_t> &Record);
221 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
222 SmallVectorImpl<uint64_t> &Record);
223 void writeModuleMetadata();
224 void writeFunctionMetadata(const Function &F);
225 void writeMetadataAttachment(const Function &F);
226 void writeModuleMetadataStore();
227 void writeOperandBundleTags();
228 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
229 void writeModuleConstants();
230 bool pushValueAndType(const Value *V, unsigned InstID,
231 SmallVectorImpl<unsigned> &Vals);
232 void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
233 void pushValue(const Value *V, unsigned InstID,
234 SmallVectorImpl<unsigned> &Vals);
235 void pushValueSigned(const Value *V, unsigned InstID,
236 SmallVectorImpl<uint64_t> &Vals);
237 void writeInstruction(const Instruction &I, unsigned InstID,
238 SmallVectorImpl<unsigned> &Vals);
239 void writeValueSymbolTable(
240 const ValueSymbolTable &VST, bool IsModuleLevel = false,
241 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex = nullptr);
242 void writeUseList(UseListOrder &&Order);
243 void writeUseListBlock(const Function *F);
244 void
245 writeFunction(const Function &F,
246 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
247 void writeBlockInfo();
248 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
Teresa Johnson28e457b2016-04-24 14:57:11 +0000249 GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +0000250 unsigned ValueID,
251 unsigned FSCallsAbbrev,
252 unsigned FSCallsProfileAbbrev,
253 const Function &F);
254 void writeModuleLevelReferences(const GlobalVariable &V,
255 SmallVector<uint64_t, 64> &NameVals,
256 unsigned FSModRefsAbbrev);
257 void writePerModuleGlobalValueSummary();
258 void writeModuleHash(size_t BlockStartPos);
259};
260
261/// Class to manage the bitcode writing for a combined index.
262class IndexBitcodeWriter : public BitcodeWriter {
263 /// The combined index to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000264 const ModuleSummaryIndex &Index;
Teresa Johnson37687f32016-04-23 04:30:47 +0000265
Teresa Johnson84174c32016-05-10 13:48:23 +0000266 /// When writing a subset of the index for distributed backends, client
267 /// provides a map of modules to the corresponding GUIDs/summaries to write.
268 std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
269
Teresa Johnson37687f32016-04-23 04:30:47 +0000270 /// Map that holds the correspondence between the GUID used in the combined
271 /// index and a value id generated by this class to use in references.
272 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
273
274 /// Tracks the last value id recorded in the GUIDToValueMap.
275 unsigned GlobalValueId = 0;
276
277public:
278 /// Constructs a IndexBitcodeWriter object for the given combined index,
Teresa Johnson84174c32016-05-10 13:48:23 +0000279 /// writing to the provided \p Buffer. When writing a subset of the index
280 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000281 IndexBitcodeWriter(SmallVectorImpl<char> &Buffer,
Teresa Johnson84174c32016-05-10 13:48:23 +0000282 const ModuleSummaryIndex &Index,
283 std::map<std::string, GVSummaryMapTy>
284 *ModuleToSummariesForIndex = nullptr)
285 : BitcodeWriter(Buffer), Index(Index),
286 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
287 // Assign unique value ids to all summaries to be written, for use
Teresa Johnson37687f32016-04-23 04:30:47 +0000288 // in writing out the call graph edges. Save the mapping from GUID
289 // to the new global value id to use when writing those edges, which
290 // are currently saved in the index in terms of GUID.
Teresa Johnson84174c32016-05-10 13:48:23 +0000291 for (const auto &I : *this)
292 GUIDToValueIdMap[I.first] = ++GlobalValueId;
Teresa Johnson37687f32016-04-23 04:30:47 +0000293 }
294
Teresa Johnson84174c32016-05-10 13:48:23 +0000295 /// The below iterator returns the GUID and associated summary.
296 typedef std::pair<GlobalValue::GUID, GlobalValueSummary *> GVInfo;
297
298 /// Iterator over the value GUID and summaries to be written to bitcode,
299 /// hides the details of whether they are being pulled from the entire
300 /// index or just those in a provided ModuleToSummariesForIndex map.
301 class iterator
302 : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
303 GVInfo> {
304 /// Enables access to parent class.
305 const IndexBitcodeWriter &Writer;
306
307 // Iterators used when writing only those summaries in a provided
308 // ModuleToSummariesForIndex map:
309
310 /// Points to the last element in outer ModuleToSummariesForIndex map.
311 std::map<std::string, GVSummaryMapTy>::iterator ModuleSummariesBack;
312 /// Iterator on outer ModuleToSummariesForIndex map.
313 std::map<std::string, GVSummaryMapTy>::iterator ModuleSummariesIter;
314 /// Iterator on an inner global variable summary map.
315 GVSummaryMapTy::iterator ModuleGVSummariesIter;
316
317 // Iterators used when writing all summaries in the index:
318
319 /// Points to the last element in the Index outer GlobalValueMap.
320 const_gvsummary_iterator IndexSummariesBack;
321 /// Iterator on outer GlobalValueMap.
322 const_gvsummary_iterator IndexSummariesIter;
323 /// Iterator on an inner GlobalValueSummaryList.
324 GlobalValueSummaryList::const_iterator IndexGVSummariesIter;
325
326 public:
327 /// Construct iterator from parent \p Writer and indicate if we are
328 /// constructing the end iterator.
329 iterator(const IndexBitcodeWriter &Writer, bool IsAtEnd) : Writer(Writer) {
330 // Set up the appropriate set of iterators given whether we are writing
331 // the full index or just a subset.
332 // Can't setup the Back or inner iterators if the corresponding map
333 // is empty. This will be handled specially in operator== as well.
334 if (Writer.ModuleToSummariesForIndex &&
335 !Writer.ModuleToSummariesForIndex->empty()) {
Teresa Johnson84174c32016-05-10 13:48:23 +0000336 for (ModuleSummariesBack = Writer.ModuleToSummariesForIndex->begin();
337 std::next(ModuleSummariesBack) !=
338 Writer.ModuleToSummariesForIndex->end();
339 ModuleSummariesBack++)
340 ;
Teresa Johnsone518c802016-05-11 20:46:22 +0000341 ModuleSummariesIter = !IsAtEnd
342 ? Writer.ModuleToSummariesForIndex->begin()
343 : ModuleSummariesBack;
Teresa Johnson84174c32016-05-10 13:48:23 +0000344 ModuleGVSummariesIter = !IsAtEnd ? ModuleSummariesIter->second.begin()
345 : ModuleSummariesBack->second.end();
346 } else if (!Writer.ModuleToSummariesForIndex &&
347 Writer.Index.begin() != Writer.Index.end()) {
Teresa Johnson84174c32016-05-10 13:48:23 +0000348 for (IndexSummariesBack = Writer.Index.begin();
349 std::next(IndexSummariesBack) != Writer.Index.end();
350 IndexSummariesBack++)
351 ;
Teresa Johnsone518c802016-05-11 20:46:22 +0000352 IndexSummariesIter =
353 !IsAtEnd ? Writer.Index.begin() : IndexSummariesBack;
Teresa Johnson84174c32016-05-10 13:48:23 +0000354 IndexGVSummariesIter = !IsAtEnd ? IndexSummariesIter->second.begin()
355 : IndexSummariesBack->second.end();
356 }
357 }
358
359 /// Increment the appropriate set of iterators.
360 iterator &operator++() {
361 // First the inner iterator is incremented, then if it is at the end
362 // and there are more outer iterations to go, the inner is reset to
363 // the start of the next inner list.
364 if (Writer.ModuleToSummariesForIndex) {
365 ++ModuleGVSummariesIter;
366 if (ModuleGVSummariesIter == ModuleSummariesIter->second.end() &&
367 ModuleSummariesIter != ModuleSummariesBack) {
368 ++ModuleSummariesIter;
369 ModuleGVSummariesIter = ModuleSummariesIter->second.begin();
370 }
371 } else {
372 ++IndexGVSummariesIter;
373 if (IndexGVSummariesIter == IndexSummariesIter->second.end() &&
374 IndexSummariesIter != IndexSummariesBack) {
375 ++IndexSummariesIter;
376 IndexGVSummariesIter = IndexSummariesIter->second.begin();
377 }
378 }
379 return *this;
380 }
381
382 /// Access the <GUID,GlobalValueSummary*> pair corresponding to the current
383 /// outer and inner iterator positions.
384 GVInfo operator*() {
385 if (Writer.ModuleToSummariesForIndex)
386 return std::make_pair(ModuleGVSummariesIter->first,
387 ModuleGVSummariesIter->second);
388 return std::make_pair(IndexSummariesIter->first,
389 IndexGVSummariesIter->get());
390 }
391
392 /// Checks if the iterators are equal, with special handling for empty
393 /// indexes.
394 bool operator==(const iterator &RHS) const {
395 if (Writer.ModuleToSummariesForIndex) {
396 // First ensure that both are writing the same subset.
397 if (Writer.ModuleToSummariesForIndex !=
398 RHS.Writer.ModuleToSummariesForIndex)
399 return false;
400 // Already determined above that maps are the same, so if one is
401 // empty, they both are.
402 if (Writer.ModuleToSummariesForIndex->empty())
403 return true;
Teresa Johnsone518c802016-05-11 20:46:22 +0000404 // Ensure the ModuleGVSummariesIter are iterating over the same
405 // container before checking them below.
406 if (ModuleSummariesIter != RHS.ModuleSummariesIter)
407 return false;
Teresa Johnson84174c32016-05-10 13:48:23 +0000408 return ModuleGVSummariesIter == RHS.ModuleGVSummariesIter;
409 }
410 // First ensure RHS also writing the full index, and that both are
411 // writing the same full index.
412 if (RHS.Writer.ModuleToSummariesForIndex ||
413 &Writer.Index != &RHS.Writer.Index)
414 return false;
415 // Already determined above that maps are the same, so if one is
416 // empty, they both are.
417 if (Writer.Index.begin() == Writer.Index.end())
418 return true;
Teresa Johnsone518c802016-05-11 20:46:22 +0000419 // Ensure the IndexGVSummariesIter are iterating over the same
420 // container before checking them below.
421 if (IndexSummariesIter != RHS.IndexSummariesIter)
422 return false;
Teresa Johnson84174c32016-05-10 13:48:23 +0000423 return IndexGVSummariesIter == RHS.IndexGVSummariesIter;
424 }
425 };
426
427 /// Obtain the start iterator over the summaries to be written.
428 iterator begin() { return iterator(*this, /*IsAtEnd=*/false); }
429 /// Obtain the end iterator over the summaries to be written.
430 iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
431
Teresa Johnson37687f32016-04-23 04:30:47 +0000432private:
433 /// Main entry point for writing a combined index to bitcode, invoked by
434 /// BitcodeWriter::write() after it writes the header.
435 void writeBlocks() override;
436
437 void writeIndex();
438 void writeModStrings();
439 void writeCombinedValueSymbolTable();
440 void writeCombinedGlobalValueSummary();
441
Teresa Johnson84174c32016-05-10 13:48:23 +0000442 /// Indicates whether the provided \p ModulePath should be written into
443 /// the module string table, e.g. if full index written or if it is in
444 /// the provided subset.
445 bool doIncludeModule(StringRef ModulePath) {
446 return !ModuleToSummariesForIndex ||
447 ModuleToSummariesForIndex->count(ModulePath);
448 }
449
Teresa Johnson37687f32016-04-23 04:30:47 +0000450 bool hasValueId(GlobalValue::GUID ValGUID) {
451 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
452 return VMI != GUIDToValueIdMap.end();
453 }
454 unsigned getValueId(GlobalValue::GUID ValGUID) {
455 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
456 // If this GUID doesn't have an entry, assign one.
457 if (VMI == GUIDToValueIdMap.end()) {
458 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
459 return GlobalValueId;
460 } else {
461 return VMI->second;
462 }
463 }
Teresa Johnson37687f32016-04-23 04:30:47 +0000464 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
465};
466
467static unsigned getEncodedCastOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000468 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000469 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000470 case Instruction::Trunc : return bitc::CAST_TRUNC;
471 case Instruction::ZExt : return bitc::CAST_ZEXT;
472 case Instruction::SExt : return bitc::CAST_SEXT;
473 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
474 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
475 case Instruction::UIToFP : return bitc::CAST_UITOFP;
476 case Instruction::SIToFP : return bitc::CAST_SITOFP;
477 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
478 case Instruction::FPExt : return bitc::CAST_FPEXT;
479 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
480 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
481 case Instruction::BitCast : return bitc::CAST_BITCAST;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000482 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000483 }
484}
485
Teresa Johnson37687f32016-04-23 04:30:47 +0000486static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000487 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000488 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmana5b96452009-06-04 22:49:04 +0000489 case Instruction::Add:
490 case Instruction::FAdd: return bitc::BINOP_ADD;
491 case Instruction::Sub:
492 case Instruction::FSub: return bitc::BINOP_SUB;
493 case Instruction::Mul:
494 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000495 case Instruction::UDiv: return bitc::BINOP_UDIV;
496 case Instruction::FDiv:
497 case Instruction::SDiv: return bitc::BINOP_SDIV;
498 case Instruction::URem: return bitc::BINOP_UREM;
499 case Instruction::FRem:
500 case Instruction::SRem: return bitc::BINOP_SREM;
501 case Instruction::Shl: return bitc::BINOP_SHL;
502 case Instruction::LShr: return bitc::BINOP_LSHR;
503 case Instruction::AShr: return bitc::BINOP_ASHR;
504 case Instruction::And: return bitc::BINOP_AND;
505 case Instruction::Or: return bitc::BINOP_OR;
506 case Instruction::Xor: return bitc::BINOP_XOR;
507 }
508}
509
Teresa Johnson37687f32016-04-23 04:30:47 +0000510static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000511 switch (Op) {
512 default: llvm_unreachable("Unknown RMW operation!");
513 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
514 case AtomicRMWInst::Add: return bitc::RMW_ADD;
515 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
516 case AtomicRMWInst::And: return bitc::RMW_AND;
517 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
518 case AtomicRMWInst::Or: return bitc::RMW_OR;
519 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
520 case AtomicRMWInst::Max: return bitc::RMW_MAX;
521 case AtomicRMWInst::Min: return bitc::RMW_MIN;
522 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
523 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
524 }
525}
526
Teresa Johnson37687f32016-04-23 04:30:47 +0000527static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000528 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +0000529 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
530 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
531 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
532 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
533 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
534 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
535 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
Eli Friedmanfee02c62011-07-25 23:16:38 +0000536 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000537 llvm_unreachable("Invalid ordering");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000538}
539
Teresa Johnson37687f32016-04-23 04:30:47 +0000540static unsigned getEncodedSynchScope(SynchronizationScope SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000541 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000542 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
543 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
544 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000545 llvm_unreachable("Invalid synch scope");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000546}
547
Teresa Johnson37687f32016-04-23 04:30:47 +0000548void ModuleBitcodeWriter::writeStringRecord(unsigned Code, StringRef Str,
549 unsigned AbbrevToUse) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000550 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000551
Chris Lattnere14cb882007-05-04 19:11:41 +0000552 // Code: [strchar x N]
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000553 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
554 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
555 AbbrevToUse = 0;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000556 Vals.push_back(Str[i]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000557 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000558
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000559 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000560 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000561}
562
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000563static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
564 switch (Kind) {
565 case Attribute::Alignment:
566 return bitc::ATTR_KIND_ALIGNMENT;
George Burgess IV278199f2016-04-12 01:05:35 +0000567 case Attribute::AllocSize:
568 return bitc::ATTR_KIND_ALLOC_SIZE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000569 case Attribute::AlwaysInline:
570 return bitc::ATTR_KIND_ALWAYS_INLINE;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000571 case Attribute::ArgMemOnly:
572 return bitc::ATTR_KIND_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000573 case Attribute::Builtin:
574 return bitc::ATTR_KIND_BUILTIN;
575 case Attribute::ByVal:
576 return bitc::ATTR_KIND_BY_VAL;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000577 case Attribute::Convergent:
578 return bitc::ATTR_KIND_CONVERGENT;
Reid Klecknera534a382013-12-19 02:14:12 +0000579 case Attribute::InAlloca:
580 return bitc::ATTR_KIND_IN_ALLOCA;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000581 case Attribute::Cold:
582 return bitc::ATTR_KIND_COLD;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000583 case Attribute::InaccessibleMemOnly:
584 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
585 case Attribute::InaccessibleMemOrArgMemOnly:
586 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000587 case Attribute::InlineHint:
588 return bitc::ATTR_KIND_INLINE_HINT;
589 case Attribute::InReg:
590 return bitc::ATTR_KIND_IN_REG;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000591 case Attribute::JumpTable:
592 return bitc::ATTR_KIND_JUMP_TABLE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000593 case Attribute::MinSize:
594 return bitc::ATTR_KIND_MIN_SIZE;
595 case Attribute::Naked:
596 return bitc::ATTR_KIND_NAKED;
597 case Attribute::Nest:
598 return bitc::ATTR_KIND_NEST;
599 case Attribute::NoAlias:
600 return bitc::ATTR_KIND_NO_ALIAS;
601 case Attribute::NoBuiltin:
602 return bitc::ATTR_KIND_NO_BUILTIN;
603 case Attribute::NoCapture:
604 return bitc::ATTR_KIND_NO_CAPTURE;
605 case Attribute::NoDuplicate:
606 return bitc::ATTR_KIND_NO_DUPLICATE;
607 case Attribute::NoImplicitFloat:
608 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
609 case Attribute::NoInline:
610 return bitc::ATTR_KIND_NO_INLINE;
James Molloye6f87ca2015-11-06 10:32:53 +0000611 case Attribute::NoRecurse:
612 return bitc::ATTR_KIND_NO_RECURSE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000613 case Attribute::NonLazyBind:
614 return bitc::ATTR_KIND_NON_LAZY_BIND;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000615 case Attribute::NonNull:
616 return bitc::ATTR_KIND_NON_NULL;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000617 case Attribute::Dereferenceable:
618 return bitc::ATTR_KIND_DEREFERENCEABLE;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000619 case Attribute::DereferenceableOrNull:
620 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000621 case Attribute::NoRedZone:
622 return bitc::ATTR_KIND_NO_RED_ZONE;
623 case Attribute::NoReturn:
624 return bitc::ATTR_KIND_NO_RETURN;
625 case Attribute::NoUnwind:
626 return bitc::ATTR_KIND_NO_UNWIND;
627 case Attribute::OptimizeForSize:
628 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000629 case Attribute::OptimizeNone:
630 return bitc::ATTR_KIND_OPTIMIZE_NONE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000631 case Attribute::ReadNone:
632 return bitc::ATTR_KIND_READ_NONE;
633 case Attribute::ReadOnly:
634 return bitc::ATTR_KIND_READ_ONLY;
635 case Attribute::Returned:
636 return bitc::ATTR_KIND_RETURNED;
637 case Attribute::ReturnsTwice:
638 return bitc::ATTR_KIND_RETURNS_TWICE;
639 case Attribute::SExt:
640 return bitc::ATTR_KIND_S_EXT;
641 case Attribute::StackAlignment:
642 return bitc::ATTR_KIND_STACK_ALIGNMENT;
643 case Attribute::StackProtect:
644 return bitc::ATTR_KIND_STACK_PROTECT;
645 case Attribute::StackProtectReq:
646 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
647 case Attribute::StackProtectStrong:
648 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000649 case Attribute::SafeStack:
650 return bitc::ATTR_KIND_SAFESTACK;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000651 case Attribute::StructRet:
652 return bitc::ATTR_KIND_STRUCT_RET;
653 case Attribute::SanitizeAddress:
654 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
655 case Attribute::SanitizeThread:
656 return bitc::ATTR_KIND_SANITIZE_THREAD;
657 case Attribute::SanitizeMemory:
658 return bitc::ATTR_KIND_SANITIZE_MEMORY;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000659 case Attribute::SwiftError:
660 return bitc::ATTR_KIND_SWIFT_ERROR;
Manman Renf46262e2016-03-29 17:37:21 +0000661 case Attribute::SwiftSelf:
662 return bitc::ATTR_KIND_SWIFT_SELF;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000663 case Attribute::UWTable:
664 return bitc::ATTR_KIND_UW_TABLE;
665 case Attribute::ZExt:
666 return bitc::ATTR_KIND_Z_EXT;
667 case Attribute::EndAttrKinds:
668 llvm_unreachable("Can not encode end-attribute kinds marker.");
669 case Attribute::None:
670 llvm_unreachable("Can not encode none-attribute.");
671 }
672
673 llvm_unreachable("Trying to encode unknown attribute");
674}
675
Teresa Johnson37687f32016-04-23 04:30:47 +0000676void ModuleBitcodeWriter::writeAttributeGroupTable() {
Bill Wendling92ed7002013-02-11 22:33:26 +0000677 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
678 if (AttrGrps.empty()) return;
Bill Wendlingdc095552013-02-10 23:09:32 +0000679
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000680 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
Bill Wendlingdc095552013-02-10 23:09:32 +0000681
682 SmallVector<uint64_t, 64> Record;
Bill Wendling92ed7002013-02-11 22:33:26 +0000683 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
684 AttributeSet AS = AttrGrps[i];
Bill Wendlingdc095552013-02-10 23:09:32 +0000685 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
686 AttributeSet A = AS.getSlotAttributes(i);
687
Bill Wendling92ed7002013-02-11 22:33:26 +0000688 Record.push_back(VE.getAttributeGroupID(A));
Bill Wendlingdc095552013-02-10 23:09:32 +0000689 Record.push_back(AS.getSlotIndex(i));
690
691 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
692 I != E; ++I) {
693 Attribute Attr = *I;
694 if (Attr.isEnumAttribute()) {
695 Record.push_back(0);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000696 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Hal Finkele15442c2014-07-18 06:51:55 +0000697 } else if (Attr.isIntAttribute()) {
Bill Wendlingdc095552013-02-10 23:09:32 +0000698 Record.push_back(1);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000699 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Bill Wendlingdc095552013-02-10 23:09:32 +0000700 Record.push_back(Attr.getValueAsInt());
701 } else {
702 StringRef Kind = Attr.getKindAsString();
703 StringRef Val = Attr.getValueAsString();
704
705 Record.push_back(Val.empty() ? 3 : 4);
706 Record.append(Kind.begin(), Kind.end());
707 Record.push_back(0);
708 if (!Val.empty()) {
709 Record.append(Val.begin(), Val.end());
710 Record.push_back(0);
711 }
712 }
713 }
714
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000715 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
Bill Wendlingdc095552013-02-10 23:09:32 +0000716 Record.clear();
717 }
718 }
719
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000720 Stream.ExitBlock();
Bill Wendlingdc095552013-02-10 23:09:32 +0000721}
722
Teresa Johnson37687f32016-04-23 04:30:47 +0000723void ModuleBitcodeWriter::writeAttributeTable() {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000724 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000725 if (Attrs.empty()) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000726
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000727 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000728
729 SmallVector<uint64_t, 64> Record;
730 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000731 const AttributeSet &A = Attrs[i];
Bill Wendling0dc08912013-02-12 08:13:50 +0000732 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
733 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000734
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000735 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000736 Record.clear();
737 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000738
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000739 Stream.ExitBlock();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000740}
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000741
742/// WriteTypeTable - Write out the type table for a module.
Teresa Johnson37687f32016-04-23 04:30:47 +0000743void ModuleBitcodeWriter::writeTypeTable() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000744 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000745
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000746 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000747 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000748
David Blaikie7b028102015-02-25 00:51:52 +0000749 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
Chad Rosier9646c0d2011-12-08 00:38:45 +0000750
Chris Lattnerccee7062007-05-05 06:30:12 +0000751 // Abbrev for TYPE_CODE_POINTER.
752 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
753 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000754 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
Christopher Lamb25f50762007-12-12 08:44:39 +0000755 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000756 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000757
Chris Lattnerccee7062007-05-05 06:30:12 +0000758 // Abbrev for TYPE_CODE_FUNCTION.
759 Abbv = new BitCodeAbbrev();
760 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
761 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnerccee7062007-05-05 06:30:12 +0000762 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000763 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
764
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000765 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000766
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000767 // Abbrev for TYPE_CODE_STRUCT_ANON.
Chris Lattnerccee7062007-05-05 06:30:12 +0000768 Abbv = new BitCodeAbbrev();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000769 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
Chris Lattnerccee7062007-05-05 06:30:12 +0000770 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
771 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000772 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
773
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000774 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000775
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000776 // Abbrev for TYPE_CODE_STRUCT_NAME.
777 Abbv = new BitCodeAbbrev();
778 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
780 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000781 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000782
783 // Abbrev for TYPE_CODE_STRUCT_NAMED.
784 Abbv = new BitCodeAbbrev();
785 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
789
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000790 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000791
Chris Lattnerccee7062007-05-05 06:30:12 +0000792 // Abbrev for TYPE_CODE_ARRAY.
793 Abbv = new BitCodeAbbrev();
794 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
795 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
Chad Rosier9646c0d2011-12-08 00:38:45 +0000796 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
797
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000798 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000799
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000800 // Emit an entry count so the reader can reserve space.
801 TypeVals.push_back(TypeList.size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000802 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000803 TypeVals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000804
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000805 // Loop over all of the types, emitting each in turn.
806 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000807 Type *T = TypeList[i];
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000808 int AbbrevToUse = 0;
809 unsigned Code = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000810
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000811 switch (T->getTypeID()) {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000812 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
813 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
814 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
815 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
816 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
817 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000818 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000819 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
820 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
821 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
David Majnemerb611e3f2015-08-14 05:09:07 +0000822 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000823 case Type::IntegerTyID:
824 // INTEGER: [width]
825 Code = bitc::TYPE_CODE_INTEGER;
826 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
827 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000828 case Type::PointerTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000829 PointerType *PTy = cast<PointerType>(T);
Christopher Lamb25f50762007-12-12 08:44:39 +0000830 // POINTER: [pointee type, address space]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000831 Code = bitc::TYPE_CODE_POINTER;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000832 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lamb25f50762007-12-12 08:44:39 +0000833 unsigned AddressSpace = PTy->getAddressSpace();
834 TypeVals.push_back(AddressSpace);
835 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000836 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000837 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000838 case Type::FunctionTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000839 FunctionType *FT = cast<FunctionType>(T);
Chad Rosier95898722011-11-03 00:14:01 +0000840 // FUNCTION: [isvararg, retty, paramty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000841 Code = bitc::TYPE_CODE_FUNCTION;
842 TypeVals.push_back(FT->isVarArg());
843 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000844 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
845 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerccee7062007-05-05 06:30:12 +0000846 AbbrevToUse = FunctionAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000847 break;
848 }
849 case Type::StructTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000850 StructType *ST = cast<StructType>(T);
Chris Lattnerccee7062007-05-05 06:30:12 +0000851 // STRUCT: [ispacked, eltty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000852 TypeVals.push_back(ST->isPacked());
Chris Lattnere14cb882007-05-04 19:11:41 +0000853 // Output all of the element types.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000854 for (StructType::element_iterator I = ST->element_begin(),
855 E = ST->element_end(); I != E; ++I)
856 TypeVals.push_back(VE.getTypeID(*I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000857
Chris Lattner335d3992011-08-12 18:06:37 +0000858 if (ST->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000859 Code = bitc::TYPE_CODE_STRUCT_ANON;
860 AbbrevToUse = StructAnonAbbrev;
861 } else {
862 if (ST->isOpaque()) {
863 Code = bitc::TYPE_CODE_OPAQUE;
864 } else {
865 Code = bitc::TYPE_CODE_STRUCT_NAMED;
866 AbbrevToUse = StructNamedAbbrev;
867 }
868
869 // Emit the name if it is present.
870 if (!ST->getName().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000871 writeStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
872 StructNameAbbrev);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000873 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000874 break;
875 }
876 case Type::ArrayTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000877 ArrayType *AT = cast<ArrayType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000878 // ARRAY: [numelts, eltty]
879 Code = bitc::TYPE_CODE_ARRAY;
880 TypeVals.push_back(AT->getNumElements());
881 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerccee7062007-05-05 06:30:12 +0000882 AbbrevToUse = ArrayAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000883 break;
884 }
885 case Type::VectorTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000886 VectorType *VT = cast<VectorType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000887 // VECTOR [numelts, eltty]
888 Code = bitc::TYPE_CODE_VECTOR;
889 TypeVals.push_back(VT->getNumElements());
890 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
891 break;
892 }
893 }
894
895 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000896 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000897 TypeVals.clear();
898 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000899
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000900 Stream.ExitBlock();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000901}
902
Teresa Johnson5e22e442016-02-06 16:07:35 +0000903static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
904 switch (Linkage) {
Rafael Espindola261d25b2015-01-08 16:25:01 +0000905 case GlobalValue::ExternalLinkage:
906 return 0;
907 case GlobalValue::WeakAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000908 return 16;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000909 case GlobalValue::AppendingLinkage:
910 return 2;
911 case GlobalValue::InternalLinkage:
912 return 3;
913 case GlobalValue::LinkOnceAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000914 return 18;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000915 case GlobalValue::ExternalWeakLinkage:
916 return 7;
917 case GlobalValue::CommonLinkage:
918 return 8;
919 case GlobalValue::PrivateLinkage:
920 return 9;
921 case GlobalValue::WeakODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000922 return 17;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000923 case GlobalValue::LinkOnceODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000924 return 19;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000925 case GlobalValue::AvailableExternallyLinkage:
926 return 12;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000927 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000928 llvm_unreachable("Invalid linkage");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000929}
930
Teresa Johnson5e22e442016-02-06 16:07:35 +0000931static unsigned getEncodedLinkage(const GlobalValue &GV) {
932 return getEncodedLinkage(GV.getLinkage());
933}
934
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000935// Decode the flags for GlobalValue in the summary
936static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
937 uint64_t RawFlags = 0;
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000938
939 RawFlags |= Flags.HasSection; // bool
940
941 // Linkage don't need to be remapped at that time for the summary. Any future
942 // change to the getEncodedLinkage() function will need to be taken into
943 // account here as well.
944 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
945
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000946 return RawFlags;
947}
948
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000949static unsigned getEncodedVisibility(const GlobalValue &GV) {
950 switch (GV.getVisibility()) {
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000951 case GlobalValue::DefaultVisibility: return 0;
952 case GlobalValue::HiddenVisibility: return 1;
953 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000954 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000955 llvm_unreachable("Invalid visibility");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000956}
957
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000958static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
959 switch (GV.getDLLStorageClass()) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000960 case GlobalValue::DefaultStorageClass: return 0;
961 case GlobalValue::DLLImportStorageClass: return 1;
962 case GlobalValue::DLLExportStorageClass: return 2;
963 }
964 llvm_unreachable("Invalid DLL storage class");
965}
966
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000967static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000968 switch (GV.getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000969 case GlobalVariable::NotThreadLocal: return 0;
970 case GlobalVariable::GeneralDynamicTLSModel: return 1;
971 case GlobalVariable::LocalDynamicTLSModel: return 2;
972 case GlobalVariable::InitialExecTLSModel: return 3;
973 case GlobalVariable::LocalExecTLSModel: return 4;
974 }
975 llvm_unreachable("Invalid TLS model");
976}
977
David Majnemerdad0a642014-06-27 18:19:56 +0000978static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
979 switch (C.getSelectionKind()) {
980 case Comdat::Any:
981 return bitc::COMDAT_SELECTION_KIND_ANY;
982 case Comdat::ExactMatch:
983 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
984 case Comdat::Largest:
985 return bitc::COMDAT_SELECTION_KIND_LARGEST;
986 case Comdat::NoDuplicates:
987 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
988 case Comdat::SameSize:
989 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
990 }
991 llvm_unreachable("Invalid selection kind");
992}
993
Teresa Johnson37687f32016-04-23 04:30:47 +0000994void ModuleBitcodeWriter::writeComdats() {
David Majnemer90a021f2016-03-13 08:01:03 +0000995 SmallVector<unsigned, 64> Vals;
David Majnemerdad0a642014-06-27 18:19:56 +0000996 for (const Comdat *C : VE.getComdats()) {
997 // COMDAT: [selection_kind, name]
998 Vals.push_back(getEncodedComdatSelectionKind(*C));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +0000999 size_t Size = C->getName().size();
David Majnemer90a021f2016-03-13 08:01:03 +00001000 assert(isUInt<32>(Size));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +00001001 Vals.push_back(Size);
David Majnemerdad0a642014-06-27 18:19:56 +00001002 for (char Chr : C->getName())
1003 Vals.push_back((unsigned char)Chr);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001004 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
David Majnemerdad0a642014-06-27 18:19:56 +00001005 Vals.clear();
1006 }
1007}
1008
Teresa Johnsonff642b92015-09-17 20:12:00 +00001009/// Write a record that will eventually hold the word offset of the
1010/// module-level VST. For now the offset is 0, which will be backpatched
Teresa Johnson37687f32016-04-23 04:30:47 +00001011/// after the real VST is written. Saves the bit offset to backpatch.
1012void BitcodeWriter::writeValueSymbolTableForwardDecl() {
Teresa Johnsonff642b92015-09-17 20:12:00 +00001013 // Write a placeholder value in for the offset of the real VST,
1014 // which is written after the function blocks so that it can include
1015 // the offset of each function. The placeholder offset will be
1016 // updated when the real VST is written.
1017 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1018 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
1019 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1020 // hold the real VST offset. Must use fixed instead of VBR as we don't
1021 // know how many VBR chunks to reserve ahead of time.
1022 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001023 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001024
1025 // Emit the placeholder
1026 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001027 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001028
Teresa Johnson37687f32016-04-23 04:30:47 +00001029 // Compute and save the bit offset to the placeholder, which will be
Teresa Johnsonff642b92015-09-17 20:12:00 +00001030 // patched when the real VST is written. We can simply subtract the 32-bit
1031 // fixed size from the current bit number to get the location to backpatch.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001032 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
Teresa Johnsonff642b92015-09-17 20:12:00 +00001033}
1034
Teresa Johnsone1164de2016-02-10 21:55:02 +00001035enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
1036
1037/// Determine the encoding to use for the given string name and length.
1038static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
1039 bool isChar6 = true;
1040 for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
1041 if (isChar6)
1042 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1043 if ((unsigned char)*C & 128)
1044 // don't bother scanning the rest.
1045 return SE_Fixed8;
1046 }
1047 if (isChar6)
1048 return SE_Char6;
1049 else
1050 return SE_Fixed7;
1051}
1052
Teresa Johnsonff642b92015-09-17 20:12:00 +00001053/// Emit top-level description of module, including target triple, inline asm,
1054/// descriptors for global variables, and function prototype info.
1055/// Returns the bit offset to backpatch with the location of the real VST.
Teresa Johnson37687f32016-04-23 04:30:47 +00001056void ModuleBitcodeWriter::writeModuleInfo() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001057 // Emit various pieces of data attached to a module.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001058 if (!M.getTargetTriple().empty())
1059 writeStringRecord(bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001060 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001061 const std::string &DL = M.getDataLayoutStr();
Rafael Espindolaf863ee22014-02-25 20:01:08 +00001062 if (!DL.empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001063 writeStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001064 if (!M.getModuleInlineAsm().empty())
1065 writeStringRecord(bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001066 0 /*TODO*/);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001067
Gordon Henriksend930f912008-08-17 18:44:35 +00001068 // Emit information about sections and GC, computing how many there are. Also
1069 // compute the maximum alignment value.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001070 std::map<std::string, unsigned> SectionMap;
Gordon Henriksend930f912008-08-17 18:44:35 +00001071 std::map<std::string, unsigned> GCMap;
Chris Lattner4b00d922007-04-23 16:04:05 +00001072 unsigned MaxAlignment = 0;
Chris Lattnerb5491372007-04-23 18:58:34 +00001073 unsigned MaxGlobalType = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001074 for (const GlobalValue &GV : M.globals()) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001075 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
David Blaikie1a848da2015-04-27 19:58:56 +00001076 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001077 if (GV.hasSection()) {
Chad Rosier75ec09c2011-08-12 16:45:18 +00001078 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001079 unsigned &Entry = SectionMap[GV.getSection()];
Chad Rosier75ec09c2011-08-12 16:45:18 +00001080 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001081 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1082 0 /*TODO*/);
Chad Rosier75ec09c2011-08-12 16:45:18 +00001083 Entry = SectionMap.size();
1084 }
1085 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001086 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001087 for (const Function &F : M) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001088 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
1089 if (F.hasSection()) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001090 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001091 unsigned &Entry = SectionMap[F.getSection()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001092 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001093 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1094 0 /*TODO*/);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001095 Entry = SectionMap.size();
1096 }
1097 }
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001098 if (F.hasGC()) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001099 // Same for GC names.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001100 unsigned &Entry = GCMap[F.getGC()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001101 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001102 writeStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 0 /*TODO*/);
Gordon Henriksend930f912008-08-17 18:44:35 +00001103 Entry = GCMap.size();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001104 }
1105 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001106 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001107
Chris Lattner4b00d922007-04-23 16:04:05 +00001108 // Emit abbrev for globals, now that we know # sections and max alignment.
1109 unsigned SimpleGVarAbbrev = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001110 if (!M.global_empty()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001111 // Add an abbrev for common globals with no visibility or thread localness.
1112 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1113 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2eae59f2007-05-04 20:34:50 +00001114 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001115 Log2_32_Ceil(MaxGlobalType+1)));
David Blaikie1a848da2015-04-27 19:58:56 +00001116 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1117 //| explicitType << 1
1118 //| constant
1119 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1120 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1121 if (MaxAlignment == 0) // Alignment.
Chris Lattner4b00d922007-04-23 16:04:05 +00001122 Abbv->Add(BitCodeAbbrevOp(0));
1123 else {
1124 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2eae59f2007-05-04 20:34:50 +00001125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001126 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001127 }
1128 if (SectionMap.empty()) // Section.
1129 Abbv->Add(BitCodeAbbrevOp(0));
1130 else
Chris Lattner2eae59f2007-05-04 20:34:50 +00001131 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner1e50c292007-04-24 03:29:47 +00001132 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001133 // Don't bother emitting vis + thread local.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001134 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner4b00d922007-04-23 16:04:05 +00001135 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001136
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001137 // Emit the global variable information.
1138 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001139 for (const GlobalVariable &GV : M.globals()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001140 unsigned AbbrevToUse = 0;
1141
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001142 // GLOBALVAR: [type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001143 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +00001144 // unnamed_addr, externally_initialized, dllstorageclass,
1145 // comdat]
David Blaikie1a848da2015-04-27 19:58:56 +00001146 Vals.push_back(VE.getTypeID(GV.getValueType()));
1147 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001148 Vals.push_back(GV.isDeclaration() ? 0 :
1149 (VE.getValueID(GV.getInitializer()) + 1));
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001150 Vals.push_back(getEncodedLinkage(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001151 Vals.push_back(Log2_32(GV.getAlignment())+1);
1152 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
1153 if (GV.isThreadLocal() ||
1154 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1155 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
David Majnemerdad0a642014-06-27 18:19:56 +00001156 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1157 GV.hasComdat()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001158 Vals.push_back(getEncodedVisibility(GV));
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001159 Vals.push_back(getEncodedThreadLocalMode(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001160 Vals.push_back(GV.hasUnnamedAddr());
1161 Vals.push_back(GV.isExternallyInitialized());
Nico Rieck7157bb72014-01-14 15:22:47 +00001162 Vals.push_back(getEncodedDLLStorageClass(GV));
David Majnemerdad0a642014-06-27 18:19:56 +00001163 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
Chris Lattner4b00d922007-04-23 16:04:05 +00001164 } else {
1165 AbbrevToUse = SimpleGVarAbbrev;
1166 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001167
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001168 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001169 Vals.clear();
1170 }
1171
1172 // Emit the function proto information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001173 for (const Function &F : M) {
Chad Rosier42ee1522011-12-07 23:57:55 +00001174 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001175 // section, visibility, gc, unnamed_addr, prologuedata,
David Majnemer7fddecc2015-06-17 20:52:32 +00001176 // dllstorageclass, comdat, prefixdata, personalityfn]
David Blaikie561a1572015-04-17 16:28:26 +00001177 Vals.push_back(VE.getTypeID(F.getFunctionType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001178 Vals.push_back(F.getCallingConv());
1179 Vals.push_back(F.isDeclaration());
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001180 Vals.push_back(getEncodedLinkage(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001181 Vals.push_back(VE.getAttributeID(F.getAttributes()));
1182 Vals.push_back(Log2_32(F.getAlignment())+1);
1183 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001184 Vals.push_back(getEncodedVisibility(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001185 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1186 Vals.push_back(F.hasUnnamedAddr());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001187 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1188 : 0);
Nico Rieck7157bb72014-01-14 15:22:47 +00001189 Vals.push_back(getEncodedDLLStorageClass(F));
David Majnemerdad0a642014-06-27 18:19:56 +00001190 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001191 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1192 : 0);
David Majnemer7fddecc2015-06-17 20:52:32 +00001193 Vals.push_back(
1194 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001195
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001196 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001197 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001198 Vals.clear();
1199 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001200
Chris Lattner44c17072007-04-26 02:46:40 +00001201 // Emit the alias information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001202 for (const GlobalAlias &A : M.aliases()) {
Chad Rosiera966c312011-12-08 00:11:31 +00001203 // ALIAS: [alias type, aliasee val#, linkage, visibility]
David Blaikie6a51dbd2015-09-17 22:18:59 +00001204 Vals.push_back(VE.getTypeID(A.getValueType()));
1205 Vals.push_back(A.getType()->getAddressSpace());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001206 Vals.push_back(VE.getValueID(A.getAliasee()));
1207 Vals.push_back(getEncodedLinkage(A));
1208 Vals.push_back(getEncodedVisibility(A));
1209 Vals.push_back(getEncodedDLLStorageClass(A));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001210 Vals.push_back(getEncodedThreadLocalMode(A));
1211 Vals.push_back(A.hasUnnamedAddr());
Chris Lattner44c17072007-04-26 02:46:40 +00001212 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001213 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
Chris Lattner44c17072007-04-26 02:46:40 +00001214 Vals.clear();
1215 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001216
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001217 // Emit the ifunc information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001218 for (const GlobalIFunc &I : M.ifuncs()) {
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001219 // IFUNC: [ifunc type, address space, resolver val#, linkage, visibility]
1220 Vals.push_back(VE.getTypeID(I.getValueType()));
1221 Vals.push_back(I.getType()->getAddressSpace());
1222 Vals.push_back(VE.getValueID(I.getResolver()));
1223 Vals.push_back(getEncodedLinkage(I));
1224 Vals.push_back(getEncodedVisibility(I));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001225 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001226 Vals.clear();
1227 }
1228
Teresa Johnsone1164de2016-02-10 21:55:02 +00001229 // Emit the module's source file name.
1230 {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001231 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1232 M.getSourceFileName().size());
Teresa Johnsone1164de2016-02-10 21:55:02 +00001233 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1234 if (Bits == SE_Char6)
1235 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1236 else if (Bits == SE_Fixed7)
1237 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1238
1239 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1240 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1241 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1243 Abbv->Add(AbbrevOpToUse);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001244 unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001245
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001246 for (const auto P : M.getSourceFileName())
Teresa Johnsone1164de2016-02-10 21:55:02 +00001247 Vals.push_back((unsigned char)P);
1248
1249 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001250 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001251 Vals.clear();
1252 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001253
Teresa Johnson37687f32016-04-23 04:30:47 +00001254 // If we have a VST, write the VSTOFFSET record placeholder.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001255 if (M.getValueSymbolTable().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001256 return;
1257 writeValueSymbolTableForwardDecl();
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001258}
1259
Teresa Johnson37687f32016-04-23 04:30:47 +00001260static uint64_t getOptimizationFlags(const Value *V) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001261 uint64_t Flags = 0;
1262
Sanjay Patelc00017d2014-10-15 17:45:13 +00001263 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001264 if (OBO->hasNoSignedWrap())
1265 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1266 if (OBO->hasNoUnsignedWrap())
1267 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001268 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
Chris Lattner35315d02011-02-06 21:44:57 +00001269 if (PEO->isExact())
1270 Flags |= 1 << bitc::PEO_EXACT;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001271 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001272 if (FPMO->hasUnsafeAlgebra())
Michael Ilseman65f14352012-12-09 21:12:04 +00001273 Flags |= FastMathFlags::UnsafeAlgebra;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001274 if (FPMO->hasNoNaNs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001275 Flags |= FastMathFlags::NoNaNs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001276 if (FPMO->hasNoInfs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001277 Flags |= FastMathFlags::NoInfs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001278 if (FPMO->hasNoSignedZeros())
Michael Ilseman65f14352012-12-09 21:12:04 +00001279 Flags |= FastMathFlags::NoSignedZeros;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001280 if (FPMO->hasAllowReciprocal())
Michael Ilseman65f14352012-12-09 21:12:04 +00001281 Flags |= FastMathFlags::AllowReciprocal;
Dan Gohman0ebd6962009-07-20 21:19:07 +00001282 }
1283
1284 return Flags;
1285}
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001286
Teresa Johnson37687f32016-04-23 04:30:47 +00001287void ModuleBitcodeWriter::writeValueAsMetadata(
1288 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001289 // Mimic an MDNode with a value as one operand.
1290 Value *V = MD->getValue();
1291 Record.push_back(VE.getTypeID(V->getType()));
1292 Record.push_back(VE.getValueID(V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001293 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001294 Record.clear();
1295}
1296
Teresa Johnson37687f32016-04-23 04:30:47 +00001297void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1298 SmallVectorImpl<uint64_t> &Record,
1299 unsigned Abbrev) {
Chris Lattner9b493022009-12-31 01:22:29 +00001300 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001301 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001302 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1303 "Unexpected function-local metadata");
1304 Record.push_back(VE.getMetadataOrNullID(MD));
Devang Patel8cca7b42009-08-04 05:01:35 +00001305 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001306 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1307 : bitc::METADATA_NODE,
1308 Record, Abbrev);
Devang Patel8cca7b42009-08-04 05:01:35 +00001309 Record.clear();
1310}
Devang Patele059ba6e2009-07-23 01:07:34 +00001311
Teresa Johnson37687f32016-04-23 04:30:47 +00001312unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001313 // Assume the column is usually under 128, and always output the inlined-at
1314 // location (it's never more expensive than building an array size 1).
1315 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1316 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1317 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1318 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1319 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1320 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1321 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001322 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001323}
1324
Teresa Johnson37687f32016-04-23 04:30:47 +00001325void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1326 SmallVectorImpl<uint64_t> &Record,
1327 unsigned &Abbrev) {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001328 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001329 Abbrev = createDILocationAbbrev();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001330
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001331 Record.push_back(N->isDistinct());
1332 Record.push_back(N->getLine());
1333 Record.push_back(N->getColumn());
1334 Record.push_back(VE.getMetadataID(N->getScope()));
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001335 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001336
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001337 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001338 Record.clear();
1339}
1340
Teresa Johnson37687f32016-04-23 04:30:47 +00001341unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001342 // Assume the column is usually under 128, and always output the inlined-at
1343 // location (it's never more expensive than building an array size 1).
1344 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1345 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1346 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1347 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1348 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1349 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001352 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001353}
1354
Teresa Johnson37687f32016-04-23 04:30:47 +00001355void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1356 SmallVectorImpl<uint64_t> &Record,
1357 unsigned &Abbrev) {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001358 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001359 Abbrev = createGenericDINodeAbbrev();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001360
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001361 Record.push_back(N->isDistinct());
1362 Record.push_back(N->getTag());
1363 Record.push_back(0); // Per-tag version field; unused for now.
1364
1365 for (auto &I : N->operands())
1366 Record.push_back(VE.getMetadataOrNullID(I));
1367
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001368 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001369 Record.clear();
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001370}
1371
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001372static uint64_t rotateSign(int64_t I) {
1373 uint64_t U = I;
1374 return I < 0 ? ~(U << 1) : U << 1;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001375}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001376
Teresa Johnson37687f32016-04-23 04:30:47 +00001377void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1378 SmallVectorImpl<uint64_t> &Record,
1379 unsigned Abbrev) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001380 Record.push_back(N->isDistinct());
1381 Record.push_back(N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001382 Record.push_back(rotateSign(N->getLowerBound()));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001383
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001384 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001385 Record.clear();
1386}
1387
Teresa Johnson37687f32016-04-23 04:30:47 +00001388void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1389 SmallVectorImpl<uint64_t> &Record,
1390 unsigned Abbrev) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001391 Record.push_back(N->isDistinct());
1392 Record.push_back(rotateSign(N->getValue()));
1393 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1394
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001395 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001396 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001397}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001398
Teresa Johnson37687f32016-04-23 04:30:47 +00001399void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1400 SmallVectorImpl<uint64_t> &Record,
1401 unsigned Abbrev) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001402 Record.push_back(N->isDistinct());
1403 Record.push_back(N->getTag());
1404 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1405 Record.push_back(N->getSizeInBits());
1406 Record.push_back(N->getAlignInBits());
1407 Record.push_back(N->getEncoding());
1408
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001409 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001410 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001411}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001412
Teresa Johnson37687f32016-04-23 04:30:47 +00001413void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1414 SmallVectorImpl<uint64_t> &Record,
1415 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001416 Record.push_back(N->isDistinct());
1417 Record.push_back(N->getTag());
1418 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1419 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1420 Record.push_back(N->getLine());
1421 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001422 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001423 Record.push_back(N->getSizeInBits());
1424 Record.push_back(N->getAlignInBits());
1425 Record.push_back(N->getOffsetInBits());
1426 Record.push_back(N->getFlags());
1427 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1428
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001429 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001430 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001431}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001432
Teresa Johnson37687f32016-04-23 04:30:47 +00001433void ModuleBitcodeWriter::writeDICompositeType(
1434 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1435 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001436 const unsigned IsNotUsedInOldTypeRef = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001437 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001438 Record.push_back(N->getTag());
1439 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1440 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1441 Record.push_back(N->getLine());
1442 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1443 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1444 Record.push_back(N->getSizeInBits());
1445 Record.push_back(N->getAlignInBits());
1446 Record.push_back(N->getOffsetInBits());
1447 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001448 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001449 Record.push_back(N->getRuntimeLang());
1450 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001451 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001452 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1453
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001454 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001455 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001456}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001457
Teresa Johnson37687f32016-04-23 04:30:47 +00001458void ModuleBitcodeWriter::writeDISubroutineType(
1459 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1460 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001461 const unsigned HasNoOldTypeRefs = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001462 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001463 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001464 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001465
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001466 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001467 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001468}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001469
Teresa Johnson37687f32016-04-23 04:30:47 +00001470void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1471 SmallVectorImpl<uint64_t> &Record,
1472 unsigned Abbrev) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001473 Record.push_back(N->isDistinct());
1474 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1475 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1476
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001477 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001478 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001479}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001480
Teresa Johnson37687f32016-04-23 04:30:47 +00001481void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1482 SmallVectorImpl<uint64_t> &Record,
1483 unsigned Abbrev) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001484 assert(N->isDistinct() && "Expected distinct compile units");
1485 Record.push_back(/* IsDistinct */ true);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001486 Record.push_back(N->getSourceLanguage());
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001487 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001488 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1489 Record.push_back(N->isOptimized());
1490 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1491 Record.push_back(N->getRuntimeVersion());
1492 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1493 Record.push_back(N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001494 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1495 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001496 Record.push_back(/* subprograms */ 0);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001497 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1498 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
Adrian Prantl1f599f92015-05-21 20:37:30 +00001499 Record.push_back(N->getDWOId());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001500 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001501
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001502 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001503 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001504}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001505
Teresa Johnson37687f32016-04-23 04:30:47 +00001506void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1507 SmallVectorImpl<uint64_t> &Record,
1508 unsigned Abbrev) {
Adrian Prantl85338cb2016-05-06 22:53:06 +00001509 uint64_t HasUnitFlag = 1 << 1;
1510 Record.push_back(N->isDistinct() | HasUnitFlag);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001511 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1512 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1513 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1514 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1515 Record.push_back(N->getLine());
1516 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1517 Record.push_back(N->isLocalToUnit());
1518 Record.push_back(N->isDefinition());
1519 Record.push_back(N->getScopeLine());
1520 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1521 Record.push_back(N->getVirtuality());
1522 Record.push_back(N->getVirtualIndex());
1523 Record.push_back(N->getFlags());
1524 Record.push_back(N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001525 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001526 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001527 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001528 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001529
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001530 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001531 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001532}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001533
Teresa Johnson37687f32016-04-23 04:30:47 +00001534void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1535 SmallVectorImpl<uint64_t> &Record,
1536 unsigned Abbrev) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001537 Record.push_back(N->isDistinct());
1538 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1539 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1540 Record.push_back(N->getLine());
1541 Record.push_back(N->getColumn());
1542
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001543 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001544 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001545}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001546
Teresa Johnson37687f32016-04-23 04:30:47 +00001547void ModuleBitcodeWriter::writeDILexicalBlockFile(
1548 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1549 unsigned Abbrev) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001550 Record.push_back(N->isDistinct());
1551 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1552 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1553 Record.push_back(N->getDiscriminator());
1554
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001555 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001556 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001557}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001558
Teresa Johnson37687f32016-04-23 04:30:47 +00001559void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1560 SmallVectorImpl<uint64_t> &Record,
1561 unsigned Abbrev) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001562 Record.push_back(N->isDistinct());
1563 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1564 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1565 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1566 Record.push_back(N->getLine());
1567
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001568 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001569 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001570}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001571
Teresa Johnson37687f32016-04-23 04:30:47 +00001572void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1573 SmallVectorImpl<uint64_t> &Record,
1574 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001575 Record.push_back(N->isDistinct());
1576 Record.push_back(N->getMacinfoType());
1577 Record.push_back(N->getLine());
1578 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1579 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1580
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001581 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001582 Record.clear();
1583}
1584
Teresa Johnson37687f32016-04-23 04:30:47 +00001585void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1586 SmallVectorImpl<uint64_t> &Record,
1587 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001588 Record.push_back(N->isDistinct());
1589 Record.push_back(N->getMacinfoType());
1590 Record.push_back(N->getLine());
1591 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1592 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1593
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001594 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001595 Record.clear();
1596}
1597
Teresa Johnson37687f32016-04-23 04:30:47 +00001598void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1599 SmallVectorImpl<uint64_t> &Record,
1600 unsigned Abbrev) {
Adrian Prantlab1243f2015-06-29 23:03:47 +00001601 Record.push_back(N->isDistinct());
1602 for (auto &I : N->operands())
1603 Record.push_back(VE.getMetadataOrNullID(I));
1604
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001605 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
Adrian Prantlab1243f2015-06-29 23:03:47 +00001606 Record.clear();
1607}
1608
Teresa Johnson37687f32016-04-23 04:30:47 +00001609void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1610 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1611 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001612 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001613 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1614 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1615
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001616 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001617 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001618}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001619
Teresa Johnson37687f32016-04-23 04:30:47 +00001620void ModuleBitcodeWriter::writeDITemplateValueParameter(
1621 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1622 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001623 Record.push_back(N->isDistinct());
1624 Record.push_back(N->getTag());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001625 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1626 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1627 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1628
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001629 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001630 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001631}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001632
Teresa Johnson37687f32016-04-23 04:30:47 +00001633void ModuleBitcodeWriter::writeDIGlobalVariable(
1634 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1635 unsigned Abbrev) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001636 Record.push_back(N->isDistinct());
1637 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1638 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1639 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1640 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1641 Record.push_back(N->getLine());
1642 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1643 Record.push_back(N->isLocalToUnit());
1644 Record.push_back(N->isDefinition());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001645 Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001646 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1647
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001648 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001649 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001650}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001651
Teresa Johnson37687f32016-04-23 04:30:47 +00001652void ModuleBitcodeWriter::writeDILocalVariable(
1653 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1654 unsigned Abbrev) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001655 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001656 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1657 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1658 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1659 Record.push_back(N->getLine());
1660 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1661 Record.push_back(N->getArg());
1662 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001663
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001664 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001665 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001666}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001667
Teresa Johnson37687f32016-04-23 04:30:47 +00001668void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1669 SmallVectorImpl<uint64_t> &Record,
1670 unsigned Abbrev) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001671 Record.reserve(N->getElements().size() + 1);
1672
1673 Record.push_back(N->isDistinct());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001674 Record.append(N->elements_begin(), N->elements_end());
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001675
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001676 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001677 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001678}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001679
Teresa Johnson37687f32016-04-23 04:30:47 +00001680void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1681 SmallVectorImpl<uint64_t> &Record,
1682 unsigned Abbrev) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001683 Record.push_back(N->isDistinct());
1684 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1685 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1686 Record.push_back(N->getLine());
1687 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1688 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1689 Record.push_back(N->getAttributes());
1690 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1691
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001692 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001693 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001694}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001695
Teresa Johnson37687f32016-04-23 04:30:47 +00001696void ModuleBitcodeWriter::writeDIImportedEntity(
1697 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1698 unsigned Abbrev) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001699 Record.push_back(N->isDistinct());
1700 Record.push_back(N->getTag());
1701 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1702 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1703 Record.push_back(N->getLine());
1704 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1705
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001706 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001707 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001708}
1709
Teresa Johnson37687f32016-04-23 04:30:47 +00001710unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001711 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1712 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1713 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1714 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001715 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001716}
1717
Teresa Johnson37687f32016-04-23 04:30:47 +00001718void ModuleBitcodeWriter::writeNamedMetadata(
1719 SmallVectorImpl<uint64_t> &Record) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001720 if (M.named_metadata_empty())
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001721 return;
1722
Teresa Johnson37687f32016-04-23 04:30:47 +00001723 unsigned Abbrev = createNamedMetadataAbbrev();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001724 for (const NamedMDNode &NMD : M.named_metadata()) {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001725 // Write name.
1726 StringRef Str = NMD.getName();
1727 Record.append(Str.bytes_begin(), Str.bytes_end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001728 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001729 Record.clear();
1730
1731 // Write named metadata operands.
1732 for (const MDNode *N : NMD.operands())
1733 Record.push_back(VE.getMetadataID(N));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001734 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001735 Record.clear();
1736 }
1737}
1738
Teresa Johnson37687f32016-04-23 04:30:47 +00001739unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001740 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1741 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1742 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1743 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001745 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001746}
1747
1748/// Write out a record for MDString.
1749///
1750/// All the metadata strings in a metadata block are emitted in a single
1751/// record. The sizes and strings themselves are shoved into a blob.
Teresa Johnson37687f32016-04-23 04:30:47 +00001752void ModuleBitcodeWriter::writeMetadataStrings(
1753 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001754 if (Strings.empty())
1755 return;
1756
1757 // Start the record with the number of strings.
1758 Record.push_back(bitc::METADATA_STRINGS);
1759 Record.push_back(Strings.size());
1760
1761 // Emit the sizes of the strings in the blob.
1762 SmallString<256> Blob;
1763 {
1764 BitstreamWriter W(Blob);
1765 for (const Metadata *MD : Strings)
1766 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1767 W.FlushToWord();
1768 }
1769
1770 // Add the offset to the strings to the record.
1771 Record.push_back(Blob.size());
1772
1773 // Add the strings to the blob.
1774 for (const Metadata *MD : Strings)
1775 Blob.append(cast<MDString>(MD)->getString());
1776
1777 // Emit the final record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001778 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001779 Record.clear();
1780}
1781
Teresa Johnson37687f32016-04-23 04:30:47 +00001782void ModuleBitcodeWriter::writeMetadataRecords(
1783 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001784 if (MDs.empty())
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +00001785 return;
1786
Duncan P. N. Exon Smith6b7b6802015-02-04 21:54:12 +00001787 // Initialize MDNode abbreviations.
1788#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1789#include "llvm/IR/Metadata.def"
1790
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001791 for (const Metadata *MD : MDs) {
Duncan P. N. Exon Smith73d5aae2015-01-12 22:31:35 +00001792 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith79f8d112015-03-17 00:16:35 +00001793 assert(N->isResolved() && "Expected forward references to be resolved");
1794
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001795 switch (N->getMetadataID()) {
1796 default:
1797 llvm_unreachable("Invalid MDNode subclass");
1798#define HANDLE_MDNODE_LEAF(CLASS) \
1799 case Metadata::CLASS##Kind: \
Teresa Johnson37687f32016-04-23 04:30:47 +00001800 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001801 continue;
1802#include "llvm/IR/Metadata.def"
1803 }
Devang Patel8cca7b42009-08-04 05:01:35 +00001804 }
Teresa Johnson37687f32016-04-23 04:30:47 +00001805 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
Devang Patel8cca7b42009-08-04 05:01:35 +00001806 }
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001807}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001808
Teresa Johnson37687f32016-04-23 04:30:47 +00001809void ModuleBitcodeWriter::writeModuleMetadata() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001810 if (!VE.hasMDs() && M.named_metadata_empty())
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001811 return;
1812
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001813 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001814 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001815 writeMetadataStrings(VE.getMDStrings(), Record);
1816 writeMetadataRecords(VE.getNonMDStrings(), Record);
1817 writeNamedMetadata(Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001818 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001819}
1820
Teresa Johnson37687f32016-04-23 04:30:47 +00001821void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +00001822 if (!VE.hasMDs())
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001823 return;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001824
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001825 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001826 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001827 writeMetadataStrings(VE.getMDStrings(), Record);
1828 writeMetadataRecords(VE.getNonMDStrings(), Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001829 Stream.ExitBlock();
Victor Hernandezb00a6be2010-01-13 19:37:33 +00001830}
1831
Teresa Johnson37687f32016-04-23 04:30:47 +00001832void ModuleBitcodeWriter::writeMetadataAttachment(const Function &F) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001833 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
Chris Lattner07d09ed2010-04-03 02:17:50 +00001834
Devang Patelaf206b82009-09-18 19:26:43 +00001835 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001836
Devang Patelaf206b82009-09-18 19:26:43 +00001837 // Write metadata attachments
Chris Lattnerbb95d5e2011-06-17 17:56:00 +00001838 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001839 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001840 F.getAllMetadata(MDs);
1841 if (!MDs.empty()) {
1842 for (const auto &I : MDs) {
1843 Record.push_back(I.first);
1844 Record.push_back(VE.getMetadataID(I.second));
1845 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001846 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001847 Record.clear();
1848 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001849
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001850 for (const BasicBlock &BB : F)
1851 for (const Instruction &I : BB) {
Devang Patel6da5dbf2009-10-22 18:55:16 +00001852 MDs.clear();
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001853 I.getAllMetadataOtherThanDebugLoc(MDs);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001854
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001855 // If no metadata, ignore instruction.
1856 if (MDs.empty()) continue;
1857
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001858 Record.push_back(VE.getInstructionID(&I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001859
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001860 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1861 Record.push_back(MDs[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001862 Record.push_back(VE.getMetadataID(MDs[i].second));
Devang Patelaf206b82009-09-18 19:26:43 +00001863 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001864 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001865 Record.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00001866 }
1867
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001868 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001869}
1870
Teresa Johnson37687f32016-04-23 04:30:47 +00001871void ModuleBitcodeWriter::writeModuleMetadataStore() {
Devang Patelaf206b82009-09-18 19:26:43 +00001872 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001873
Devang Patelaf206b82009-09-18 19:26:43 +00001874 // Write metadata kinds
1875 // METADATA_KIND - [n x [id, name]]
Michael Ilseman979dfbb2012-12-03 22:57:47 +00001876 SmallVector<StringRef, 8> Names;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001877 M.getMDKindNames(Names);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001878
Dan Gohman43aa8f02010-07-20 21:42:28 +00001879 if (Names.empty()) return;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001880
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001881 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001882
Dan Gohman43aa8f02010-07-20 21:42:28 +00001883 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattnerc9558df2009-12-28 20:10:43 +00001884 Record.push_back(MDKindID);
1885 StringRef KName = Names[MDKindID];
1886 Record.append(KName.begin(), KName.end());
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001887
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001888 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
Devang Patelaf206b82009-09-18 19:26:43 +00001889 Record.clear();
1890 }
1891
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001892 Stream.ExitBlock();
Devang Patel8cca7b42009-08-04 05:01:35 +00001893}
1894
Teresa Johnson37687f32016-04-23 04:30:47 +00001895void ModuleBitcodeWriter::writeOperandBundleTags() {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001896 // Write metadata kinds
1897 //
1898 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
1899 //
1900 // OPERAND_BUNDLE_TAG - [strchr x N]
1901
1902 SmallVector<StringRef, 8> Tags;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001903 M.getOperandBundleTags(Tags);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001904
1905 if (Tags.empty())
1906 return;
1907
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001908 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001909
1910 SmallVector<uint64_t, 64> Record;
1911
1912 for (auto Tag : Tags) {
1913 Record.append(Tag.begin(), Tag.end());
1914
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001915 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001916 Record.clear();
1917 }
1918
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001919 Stream.ExitBlock();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001920}
1921
Jan Wen Voungafaced02012-10-11 20:20:40 +00001922static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1923 if ((int64_t)V >= 0)
1924 Vals.push_back(V << 1);
1925 else
1926 Vals.push_back((-V << 1) | 1);
1927}
1928
Teresa Johnson37687f32016-04-23 04:30:47 +00001929void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1930 bool isGlobal) {
Devang Patel8cca7b42009-08-04 05:01:35 +00001931 if (FirstVal == LastVal) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001932
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001933 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Devang Patel8cca7b42009-08-04 05:01:35 +00001934
1935 unsigned AggregateAbbrev = 0;
1936 unsigned String8Abbrev = 0;
1937 unsigned CString7Abbrev = 0;
1938 unsigned CString6Abbrev = 0;
1939 // If this is a constant pool for the module, emit module-specific abbrevs.
1940 if (isGlobal) {
1941 // Abbrev for CST_CODE_AGGREGATE.
1942 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1943 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1944 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1945 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001946 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001947
1948 // Abbrev for CST_CODE_STRING.
1949 Abbv = new BitCodeAbbrev();
1950 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1951 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1952 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001953 String8Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001954 // Abbrev for CST_CODE_CSTRING.
1955 Abbv = new BitCodeAbbrev();
1956 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001959 CString7Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001960 // Abbrev for CST_CODE_CSTRING.
1961 Abbv = new BitCodeAbbrev();
1962 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1963 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1964 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001965 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001966 }
1967
Devang Patel8cca7b42009-08-04 05:01:35 +00001968 SmallVector<uint64_t, 64> Record;
1969
1970 const ValueEnumerator::ValueList &Vals = VE.getValues();
Craig Topper2617dcc2014-04-15 06:32:26 +00001971 Type *LastTy = nullptr;
Devang Patel8cca7b42009-08-04 05:01:35 +00001972 for (unsigned i = FirstVal; i != LastVal; ++i) {
1973 const Value *V = Vals[i].first;
Chris Lattner52523562007-04-24 00:16:04 +00001974 // If we need to switch types, do so now.
1975 if (V->getType() != LastTy) {
1976 LastTy = V->getType();
1977 Record.push_back(VE.getTypeID(LastTy));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001978 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1979 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner52523562007-04-24 00:16:04 +00001980 Record.clear();
1981 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001982
Chris Lattner52523562007-04-24 00:16:04 +00001983 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesenfd04c742009-10-13 20:46:56 +00001984 Record.push_back(unsigned(IA->hasSideEffects()) |
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001985 unsigned(IA->isAlignStack()) << 1 |
1986 unsigned(IA->getDialect()&1) << 2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001987
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001988 // Add the asm string.
1989 const std::string &AsmStr = IA->getAsmString();
1990 Record.push_back(AsmStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001991 Record.append(AsmStr.begin(), AsmStr.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001992
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001993 // Add the constraint string.
1994 const std::string &ConstraintStr = IA->getConstraintString();
1995 Record.push_back(ConstraintStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001996 Record.append(ConstraintStr.begin(), ConstraintStr.end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001997 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001998 Record.clear();
Chris Lattner52523562007-04-24 00:16:04 +00001999 continue;
2000 }
2001 const Constant *C = cast<Constant>(V);
2002 unsigned Code = -1U;
2003 unsigned AbbrevToUse = 0;
2004 if (C->isNullValue()) {
2005 Code = bitc::CST_CODE_NULL;
2006 } else if (isa<UndefValue>(C)) {
2007 Code = bitc::CST_CODE_UNDEF;
2008 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
Bob Wilsone4077362013-09-09 19:14:35 +00002009 if (IV->getBitWidth() <= 64) {
2010 uint64_t V = IV->getSExtValue();
2011 emitSignedInt64(Record, V);
2012 Code = bitc::CST_CODE_INTEGER;
2013 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2014 } else { // Wide integers, > 64 bits in size.
2015 // We have an arbitrary precision integer value to write whose
2016 // bit width is > 64. However, in canonical unsigned integer
2017 // format it is likely that the high bits are going to be zero.
2018 // So, we only write the number of active words.
2019 unsigned NWords = IV->getValue().getActiveWords();
2020 const uint64_t *RawWords = IV->getValue().getRawData();
2021 for (unsigned i = 0; i != NWords; ++i) {
2022 emitSignedInt64(Record, RawWords[i]);
2023 }
2024 Code = bitc::CST_CODE_WIDE_INTEGER;
2025 }
Chris Lattner52523562007-04-24 00:16:04 +00002026 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2027 Code = bitc::CST_CODE_FLOAT;
Chris Lattner229907c2011-07-18 04:54:35 +00002028 Type *Ty = CFP->getType();
Dan Gohman518cda42011-12-17 00:04:22 +00002029 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002030 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +00002031 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002032 // api needed to prevent premature destruction
Dale Johannesen93eefa02009-03-23 21:16:53 +00002033 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen54306fe2008-10-09 18:53:47 +00002034 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002035 const uint64_t *p = api.getRawData();
Dale Johannesen93eefa02009-03-23 21:16:53 +00002036 Record.push_back((p[1] << 48) | (p[0] >> 16));
2037 Record.push_back(p[0] & 0xffffLL);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002038 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002039 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002040 const uint64_t *p = api.getRawData();
Dale Johannesen245dceb2007-09-11 18:32:33 +00002041 Record.push_back(p[0]);
2042 Record.push_back(p[1]);
Dale Johannesenbdad8092007-08-09 22:51:36 +00002043 } else {
2044 assert (0 && "Unknown FP type!");
Chris Lattner52523562007-04-24 00:16:04 +00002045 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00002046 } else if (isa<ConstantDataSequential>(C) &&
2047 cast<ConstantDataSequential>(C)->isString()) {
2048 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2049 // Emit constant strings specially.
2050 unsigned NumElts = Str->getNumElements();
2051 // If this is a null-terminated string, use the denser CSTRING encoding.
2052 if (Str->isCString()) {
2053 Code = bitc::CST_CODE_CSTRING;
2054 --NumElts; // Don't encode the null, which isn't allowed by char6.
2055 } else {
2056 Code = bitc::CST_CODE_STRING;
2057 AbbrevToUse = String8Abbrev;
2058 }
2059 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2060 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2061 for (unsigned i = 0; i != NumElts; ++i) {
2062 unsigned char V = Str->getElementAsInteger(i);
2063 Record.push_back(V);
2064 isCStr7 &= (V & 128) == 0;
2065 if (isCStrChar6)
2066 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2067 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002068
Chris Lattner372dd1e2012-01-30 00:51:16 +00002069 if (isCStrChar6)
2070 AbbrevToUse = CString6Abbrev;
2071 else if (isCStr7)
2072 AbbrevToUse = CString7Abbrev;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002073 } else if (const ConstantDataSequential *CDS =
Chris Lattner372dd1e2012-01-30 00:51:16 +00002074 dyn_cast<ConstantDataSequential>(C)) {
Chris Lattner5d917072012-01-30 07:36:01 +00002075 Code = bitc::CST_CODE_DATA;
Chris Lattner372dd1e2012-01-30 00:51:16 +00002076 Type *EltTy = CDS->getType()->getElementType();
2077 if (isa<IntegerType>(EltTy)) {
2078 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2079 Record.push_back(CDS->getElementAsInteger(i));
Chris Lattner372dd1e2012-01-30 00:51:16 +00002080 } else {
Justin Bognera43eacb2016-01-06 22:31:32 +00002081 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2082 Record.push_back(
2083 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
Chris Lattner372dd1e2012-01-30 00:51:16 +00002084 }
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00002085 } else if (isa<ConstantAggregate>(C)) {
Chris Lattner52523562007-04-24 00:16:04 +00002086 Code = bitc::CST_CODE_AGGREGATE;
Pete Cooper125ad172015-06-25 20:51:38 +00002087 for (const Value *Op : C->operands())
2088 Record.push_back(VE.getValueID(Op));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002089 AbbrevToUse = AggregateAbbrev;
Chris Lattner52523562007-04-24 00:16:04 +00002090 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002091 switch (CE->getOpcode()) {
2092 default:
2093 if (Instruction::isCast(CE->getOpcode())) {
2094 Code = bitc::CST_CODE_CE_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002095 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002096 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2097 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002098 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002099 } else {
2100 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2101 Code = bitc::CST_CODE_CE_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002102 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002103 Record.push_back(VE.getValueID(C->getOperand(0)));
2104 Record.push_back(VE.getValueID(C->getOperand(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002105 uint64_t Flags = getOptimizationFlags(CE);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002106 if (Flags != 0)
2107 Record.push_back(Flags);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002108 }
2109 break;
David Blaikieb9263572015-03-13 21:03:36 +00002110 case Instruction::GetElementPtr: {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002111 Code = bitc::CST_CODE_CE_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00002112 const auto *GO = cast<GEPOperator>(C);
2113 if (GO->isInBounds())
Dan Gohman1639c392009-07-27 21:53:46 +00002114 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00002115 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002116 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2117 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2118 Record.push_back(VE.getValueID(C->getOperand(i)));
2119 }
2120 break;
David Blaikieb9263572015-03-13 21:03:36 +00002121 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002122 case Instruction::Select:
2123 Code = bitc::CST_CODE_CE_SELECT;
2124 Record.push_back(VE.getValueID(C->getOperand(0)));
2125 Record.push_back(VE.getValueID(C->getOperand(1)));
2126 Record.push_back(VE.getValueID(C->getOperand(2)));
2127 break;
2128 case Instruction::ExtractElement:
2129 Code = bitc::CST_CODE_CE_EXTRACTELT;
2130 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2131 Record.push_back(VE.getValueID(C->getOperand(0)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002132 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002133 Record.push_back(VE.getValueID(C->getOperand(1)));
2134 break;
2135 case Instruction::InsertElement:
2136 Code = bitc::CST_CODE_CE_INSERTELT;
2137 Record.push_back(VE.getValueID(C->getOperand(0)));
2138 Record.push_back(VE.getValueID(C->getOperand(1)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002139 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002140 Record.push_back(VE.getValueID(C->getOperand(2)));
2141 break;
2142 case Instruction::ShuffleVector:
Nate Begeman94aa38d2009-02-12 21:28:33 +00002143 // If the return type and argument types are the same, this is a
2144 // standard shufflevector instruction. If the types are different,
2145 // then the shuffle is widening or truncating the input vectors, and
2146 // the argument type must also be encoded.
2147 if (C->getType() == C->getOperand(0)->getType()) {
2148 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2149 } else {
2150 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2151 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2152 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002153 Record.push_back(VE.getValueID(C->getOperand(0)));
2154 Record.push_back(VE.getValueID(C->getOperand(1)));
2155 Record.push_back(VE.getValueID(C->getOperand(2)));
2156 break;
2157 case Instruction::ICmp:
2158 case Instruction::FCmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002159 Code = bitc::CST_CODE_CE_CMP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002160 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2161 Record.push_back(VE.getValueID(C->getOperand(0)));
2162 Record.push_back(VE.getValueID(C->getOperand(1)));
2163 Record.push_back(CE->getPredicate());
2164 break;
2165 }
Chris Lattnerf540d742009-10-28 05:24:40 +00002166 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerf540d742009-10-28 05:24:40 +00002167 Code = bitc::CST_CODE_BLOCKADDRESS;
2168 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2169 Record.push_back(VE.getValueID(BA->getFunction()));
2170 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner52523562007-04-24 00:16:04 +00002171 } else {
Dan Gohman47dc8fd2010-07-21 21:18:37 +00002172#ifndef NDEBUG
2173 C->dump();
2174#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002175 llvm_unreachable("Unknown constant!");
Chris Lattner52523562007-04-24 00:16:04 +00002176 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002177 Stream.EmitRecord(Code, Record, AbbrevToUse);
Chris Lattner52523562007-04-24 00:16:04 +00002178 Record.clear();
2179 }
2180
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002181 Stream.ExitBlock();
Chris Lattner52523562007-04-24 00:16:04 +00002182}
2183
Teresa Johnson37687f32016-04-23 04:30:47 +00002184void ModuleBitcodeWriter::writeModuleConstants() {
Chris Lattner52523562007-04-24 00:16:04 +00002185 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002186
Chris Lattner52523562007-04-24 00:16:04 +00002187 // Find the first constant to emit, which is the first non-globalvalue value.
2188 // We know globalvalues have been emitted by WriteModuleInfo.
2189 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2190 if (!isa<GlobalValue>(Vals[i].first)) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002191 writeConstants(i, Vals.size(), true);
Chris Lattner52523562007-04-24 00:16:04 +00002192 return;
2193 }
2194 }
2195}
Chris Lattner215e9cd2007-04-23 20:35:01 +00002196
Teresa Johnson37687f32016-04-23 04:30:47 +00002197/// pushValueAndType - The file has to encode both the value and type id for
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002198/// many values, because we need to know what type to create for forward
2199/// references. However, most operands are not forward references, so this type
2200/// field is not needed.
2201///
2202/// This function adds V's value ID to Vals. If the value ID is higher than the
2203/// instruction ID, then it is a forward reference, and it also includes the
Jan Wen Voungafaced02012-10-11 20:20:40 +00002204/// type ID. The value ID that is written is encoded relative to the InstID.
Teresa Johnson37687f32016-04-23 04:30:47 +00002205bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2206 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002207 unsigned ValID = VE.getValueID(V);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002208 // Make encoding relative to the InstID.
2209 Vals.push_back(InstID - ValID);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002210 if (ValID >= InstID) {
2211 Vals.push_back(VE.getTypeID(V->getType()));
2212 return true;
2213 }
2214 return false;
2215}
2216
Teresa Johnson37687f32016-04-23 04:30:47 +00002217void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2218 unsigned InstID) {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002219 SmallVector<unsigned, 64> Record;
2220 LLVMContext &C = CS.getInstruction()->getContext();
2221
2222 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002223 const auto &Bundle = CS.getOperandBundleAt(i);
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002224 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002225
2226 for (auto &Input : Bundle.Inputs)
Teresa Johnson37687f32016-04-23 04:30:47 +00002227 pushValueAndType(Input, InstID, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002228
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002229 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002230 Record.clear();
2231 }
2232}
2233
Teresa Johnson37687f32016-04-23 04:30:47 +00002234/// pushValue - Like pushValueAndType, but where the type of the value is
Jan Wen Voungafaced02012-10-11 20:20:40 +00002235/// omitted (perhaps it was already encoded in an earlier operand).
Teresa Johnson37687f32016-04-23 04:30:47 +00002236void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2237 SmallVectorImpl<unsigned> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002238 unsigned ValID = VE.getValueID(V);
2239 Vals.push_back(InstID - ValID);
2240}
2241
Teresa Johnson37687f32016-04-23 04:30:47 +00002242void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2243 SmallVectorImpl<uint64_t> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002244 unsigned ValID = VE.getValueID(V);
2245 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2246 emitSignedInt64(Vals, diff);
2247}
2248
Chris Lattnere6e364c2007-04-26 05:53:54 +00002249/// WriteInstruction - Emit an instruction to the specified stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002250void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2251 unsigned InstID,
2252 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnere6e364c2007-04-26 05:53:54 +00002253 unsigned Code = 0;
2254 unsigned AbbrevToUse = 0;
Devang Patelaf206b82009-09-18 19:26:43 +00002255 VE.setInstructionID(&I);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002256 switch (I.getOpcode()) {
2257 default:
2258 if (Instruction::isCast(I.getOpcode())) {
Chris Lattner0a603252007-05-01 02:13:26 +00002259 Code = bitc::FUNC_CODE_INST_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002260 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002261 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002262 Vals.push_back(VE.getTypeID(I.getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002263 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
Chris Lattnere6e364c2007-04-26 05:53:54 +00002264 } else {
2265 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattner9f35f912007-05-02 04:26:36 +00002266 Code = bitc::FUNC_CODE_INST_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002267 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002268 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Teresa Johnson37687f32016-04-23 04:30:47 +00002269 pushValue(I.getOperand(1), InstID, Vals);
2270 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2271 uint64_t Flags = getOptimizationFlags(&I);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002272 if (Flags != 0) {
2273 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2274 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2275 Vals.push_back(Flags);
2276 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002277 }
2278 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002279
David Blaikieb5b5efd2015-02-25 01:08:52 +00002280 case Instruction::GetElementPtr: {
Chris Lattner0a603252007-05-01 02:13:26 +00002281 Code = bitc::FUNC_CODE_INST_GEP;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002282 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2283 auto &GEPInst = cast<GetElementPtrInst>(I);
2284 Vals.push_back(GEPInst.isInBounds());
2285 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002286 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002287 pushValueAndType(I.getOperand(i), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002288 break;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002289 }
Dan Gohmanca0256a2008-05-31 19:11:15 +00002290 case Instruction::ExtractValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002291 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002292 pushValueAndType(I.getOperand(0), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002293 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002294 Vals.append(EVI->idx_begin(), EVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002295 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002296 }
2297 case Instruction::InsertValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002298 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002299 pushValueAndType(I.getOperand(0), InstID, Vals);
2300 pushValueAndType(I.getOperand(1), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002301 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002302 Vals.append(IVI->idx_begin(), IVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002303 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002304 }
Chris Lattner0a603252007-05-01 02:13:26 +00002305 case Instruction::Select:
Dan Gohmanc5d28922008-09-16 01:01:33 +00002306 Code = bitc::FUNC_CODE_INST_VSELECT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002307 pushValueAndType(I.getOperand(1), InstID, Vals);
2308 pushValue(I.getOperand(2), InstID, Vals);
2309 pushValueAndType(I.getOperand(0), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002310 break;
2311 case Instruction::ExtractElement:
2312 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002313 pushValueAndType(I.getOperand(0), InstID, Vals);
2314 pushValueAndType(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002315 break;
2316 case Instruction::InsertElement:
2317 Code = bitc::FUNC_CODE_INST_INSERTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002318 pushValueAndType(I.getOperand(0), InstID, Vals);
2319 pushValue(I.getOperand(1), InstID, Vals);
2320 pushValueAndType(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002321 break;
2322 case Instruction::ShuffleVector:
2323 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002324 pushValueAndType(I.getOperand(0), InstID, Vals);
2325 pushValue(I.getOperand(1), InstID, Vals);
2326 pushValue(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002327 break;
2328 case Instruction::ICmp:
James Molloy88eb5352015-07-10 12:52:00 +00002329 case Instruction::FCmp: {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002330 // compare returning Int1Ty or vector of Int1Ty
2331 Code = bitc::FUNC_CODE_INST_CMP2;
Teresa Johnson37687f32016-04-23 04:30:47 +00002332 pushValueAndType(I.getOperand(0), InstID, Vals);
2333 pushValue(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002334 Vals.push_back(cast<CmpInst>(I).getPredicate());
Teresa Johnson37687f32016-04-23 04:30:47 +00002335 uint64_t Flags = getOptimizationFlags(&I);
James Molloy88eb5352015-07-10 12:52:00 +00002336 if (Flags != 0)
2337 Vals.push_back(Flags);
Chris Lattner0a603252007-05-01 02:13:26 +00002338 break;
James Molloy88eb5352015-07-10 12:52:00 +00002339 }
Chris Lattner0a603252007-05-01 02:13:26 +00002340
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002341 case Instruction::Ret:
Devang Patelbbfd8742008-02-26 01:29:32 +00002342 {
2343 Code = bitc::FUNC_CODE_INST_RET;
2344 unsigned NumOperands = I.getNumOperands();
Devang Patelbbfd8742008-02-26 01:29:32 +00002345 if (NumOperands == 0)
2346 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2347 else if (NumOperands == 1) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002348 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Devang Patelbbfd8742008-02-26 01:29:32 +00002349 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2350 } else {
2351 for (unsigned i = 0, e = NumOperands; i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002352 pushValueAndType(I.getOperand(i), InstID, Vals);
Devang Patelbbfd8742008-02-26 01:29:32 +00002353 }
2354 }
Chris Lattner0a603252007-05-01 02:13:26 +00002355 break;
2356 case Instruction::Br:
Gabor Greif1933b002009-01-30 18:27:21 +00002357 {
2358 Code = bitc::FUNC_CODE_INST_BR;
David Blaikieb78e9e52013-02-11 01:16:51 +00002359 const BranchInst &II = cast<BranchInst>(I);
Gabor Greif1933b002009-01-30 18:27:21 +00002360 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2361 if (II.isConditional()) {
2362 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002363 pushValue(II.getCondition(), InstID, Vals);
Gabor Greif1933b002009-01-30 18:27:21 +00002364 }
Chris Lattner0a603252007-05-01 02:13:26 +00002365 }
2366 break;
2367 case Instruction::Switch:
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002368 {
2369 Code = bitc::FUNC_CODE_INST_SWITCH;
David Blaikieb78e9e52013-02-11 01:16:51 +00002370 const SwitchInst &SI = cast<SwitchInst>(I);
Bob Wilsone4077362013-09-09 19:14:35 +00002371 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002372 pushValue(SI.getCondition(), InstID, Vals);
Bob Wilsone4077362013-09-09 19:14:35 +00002373 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
Sanjay Patel225d65f2015-11-13 16:21:23 +00002374 for (SwitchInst::ConstCaseIt Case : SI.cases()) {
2375 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2376 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002377 }
2378 }
Chris Lattner0a603252007-05-01 02:13:26 +00002379 break;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002380 case Instruction::IndirectBr:
2381 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattner3ed871f2009-10-27 19:13:16 +00002382 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Jan Wen Voungafaced02012-10-11 20:20:40 +00002383 // Encode the address operand as relative, but not the basic blocks.
Teresa Johnson37687f32016-04-23 04:30:47 +00002384 pushValue(I.getOperand(0), InstID, Vals);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002385 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
Chris Lattner3ed871f2009-10-27 19:13:16 +00002386 Vals.push_back(VE.getValueID(I.getOperand(i)));
2387 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002388
Chris Lattnerb811e952007-05-01 07:03:37 +00002389 case Instruction::Invoke: {
Gabor Greif4b79e472009-01-16 18:40:27 +00002390 const InvokeInst *II = cast<InvokeInst>(&I);
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002391 const Value *Callee = II->getCalledValue();
2392 FunctionType *FTy = II->getFunctionType();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002393
2394 if (II->hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002395 writeOperandBundles(II, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002396
Chris Lattner0a603252007-05-01 02:13:26 +00002397 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002398
Devang Patel4c758ea2008-09-25 21:00:45 +00002399 Vals.push_back(VE.getAttributeID(II->getAttributes()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002400 Vals.push_back(II->getCallingConv() | 1 << 13);
Gabor Greif6ecd6f42009-01-07 22:39:29 +00002401 Vals.push_back(VE.getValueID(II->getNormalDest()));
2402 Vals.push_back(VE.getValueID(II->getUnwindDest()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002403 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002404 pushValueAndType(Callee, InstID, Vals);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002405
Chris Lattner0a603252007-05-01 02:13:26 +00002406 // Emit value #'s for the fixed parameters.
Chris Lattner0a603252007-05-01 02:13:26 +00002407 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002408 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
Chris Lattner0a603252007-05-01 02:13:26 +00002409
2410 // Emit type/value pairs for varargs params.
2411 if (FTy->isVarArg()) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00002412 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002413 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002414 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
Chris Lattner0a603252007-05-01 02:13:26 +00002415 }
2416 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002417 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002418 case Instruction::Resume:
2419 Code = bitc::FUNC_CODE_INST_RESUME;
Teresa Johnson37687f32016-04-23 04:30:47 +00002420 pushValueAndType(I.getOperand(0), InstID, Vals);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002421 break;
David Majnemer654e1302015-07-31 17:58:14 +00002422 case Instruction::CleanupRet: {
2423 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2424 const auto &CRI = cast<CleanupReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002425 pushValue(CRI.getCleanupPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002426 if (CRI.hasUnwindDest())
2427 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2428 break;
2429 }
2430 case Instruction::CatchRet: {
2431 Code = bitc::FUNC_CODE_INST_CATCHRET;
2432 const auto &CRI = cast<CatchReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002433 pushValue(CRI.getCatchPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002434 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2435 break;
2436 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00002437 case Instruction::CleanupPad:
David Majnemer654e1302015-07-31 17:58:14 +00002438 case Instruction::CatchPad: {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002439 const auto &FuncletPad = cast<FuncletPadInst>(I);
2440 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2441 : bitc::FUNC_CODE_INST_CLEANUPPAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002442 pushValue(FuncletPad.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002443
2444 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
David Majnemer654e1302015-07-31 17:58:14 +00002445 Vals.push_back(NumArgOperands);
2446 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
Teresa Johnson37687f32016-04-23 04:30:47 +00002447 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002448 break;
2449 }
2450 case Instruction::CatchSwitch: {
2451 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2452 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2453
Teresa Johnson37687f32016-04-23 04:30:47 +00002454 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002455
2456 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2457 Vals.push_back(NumHandlers);
2458 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2459 Vals.push_back(VE.getValueID(CatchPadBB));
2460
2461 if (CatchSwitch.hasUnwindDest())
2462 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
David Majnemer654e1302015-07-31 17:58:14 +00002463 break;
2464 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002465 case Instruction::Unreachable:
2466 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002467 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002468 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002469
Jay Foad372ad642011-06-20 14:18:48 +00002470 case Instruction::PHI: {
2471 const PHINode &PN = cast<PHINode>(I);
Chris Lattner0a603252007-05-01 02:13:26 +00002472 Code = bitc::FUNC_CODE_INST_PHI;
Jan Wen Voungafaced02012-10-11 20:20:40 +00002473 // With the newer instruction encoding, forward references could give
2474 // negative valued IDs. This is most common for PHIs, so we use
2475 // signed VBRs.
2476 SmallVector<uint64_t, 128> Vals64;
2477 Vals64.push_back(VE.getTypeID(PN.getType()));
Jay Foad372ad642011-06-20 14:18:48 +00002478 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002479 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002480 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
Jay Foad372ad642011-06-20 14:18:48 +00002481 }
Jan Wen Voungafaced02012-10-11 20:20:40 +00002482 // Emit a Vals64 vector and exit.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002483 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002484 Vals64.clear();
2485 return;
Jay Foad372ad642011-06-20 14:18:48 +00002486 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002487
Bill Wendlingfae14752011-08-12 20:24:12 +00002488 case Instruction::LandingPad: {
2489 const LandingPadInst &LP = cast<LandingPadInst>(I);
2490 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2491 Vals.push_back(VE.getTypeID(LP.getType()));
Bill Wendlingfae14752011-08-12 20:24:12 +00002492 Vals.push_back(LP.isCleanup());
2493 Vals.push_back(LP.getNumClauses());
2494 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2495 if (LP.isCatch(I))
2496 Vals.push_back(LandingPadInst::Catch);
2497 else
2498 Vals.push_back(LandingPadInst::Filter);
Teresa Johnson37687f32016-04-23 04:30:47 +00002499 pushValueAndType(LP.getClause(I), InstID, Vals);
Bill Wendlingfae14752011-08-12 20:24:12 +00002500 }
2501 break;
2502 }
2503
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002504 case Instruction::Alloca: {
Chris Lattner0a603252007-05-01 02:13:26 +00002505 Code = bitc::FUNC_CODE_INST_ALLOCA;
David Blaikiebdb49102015-04-28 16:51:01 +00002506 const AllocaInst &AI = cast<AllocaInst>(I);
2507 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
Dan Gohman9da5bb02010-05-28 01:38:28 +00002508 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002509 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002510 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2511 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2512 "not enough bits for maximum alignment");
2513 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2514 AlignRecord |= AI.isUsedWithInAlloca() << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00002515 AlignRecord |= 1 << 6;
Manman Ren9bfd0d02016-04-01 21:41:15 +00002516 AlignRecord |= AI.isSwiftError() << 7;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002517 Vals.push_back(AlignRecord);
Chris Lattner0a603252007-05-01 02:13:26 +00002518 break;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002519 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002520
Chris Lattner0a603252007-05-01 02:13:26 +00002521 case Instruction::Load:
Eli Friedman59b66882011-08-09 23:02:53 +00002522 if (cast<LoadInst>(I).isAtomic()) {
2523 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002524 pushValueAndType(I.getOperand(0), InstID, Vals);
Eli Friedman59b66882011-08-09 23:02:53 +00002525 } else {
2526 Code = bitc::FUNC_CODE_INST_LOAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002527 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
Eli Friedman59b66882011-08-09 23:02:53 +00002528 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2529 }
David Blaikie85035652015-02-25 01:07:20 +00002530 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002531 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2532 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002533 if (cast<LoadInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002534 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2535 Vals.push_back(getEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002536 }
Chris Lattner0a603252007-05-01 02:13:26 +00002537 break;
2538 case Instruction::Store:
Eli Friedman59b66882011-08-09 23:02:53 +00002539 if (cast<StoreInst>(I).isAtomic())
2540 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2541 else
2542 Code = bitc::FUNC_CODE_INST_STORE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002543 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2544 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
Chris Lattner0a603252007-05-01 02:13:26 +00002545 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2546 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002547 if (cast<StoreInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002548 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2549 Vals.push_back(getEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002550 }
Chris Lattner0a603252007-05-01 02:13:26 +00002551 break;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002552 case Instruction::AtomicCmpXchg:
2553 Code = bitc::FUNC_CODE_INST_CMPXCHG;
Teresa Johnson37687f32016-04-23 04:30:47 +00002554 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2555 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2556 pushValue(I.getOperand(2), InstID, Vals); // newval.
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002557 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002558 Vals.push_back(
2559 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2560 Vals.push_back(
2561 getEncodedSynchScope(cast<AtomicCmpXchgInst>(I).getSynchScope()));
2562 Vals.push_back(
2563 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
Tim Northover420a2162014-06-13 14:24:07 +00002564 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002565 break;
2566 case Instruction::AtomicRMW:
2567 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
Teresa Johnson37687f32016-04-23 04:30:47 +00002568 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2569 pushValue(I.getOperand(1), InstID, Vals); // val.
2570 Vals.push_back(
2571 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002572 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002573 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2574 Vals.push_back(
2575 getEncodedSynchScope(cast<AtomicRMWInst>(I).getSynchScope()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002576 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002577 case Instruction::Fence:
2578 Code = bitc::FUNC_CODE_INST_FENCE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002579 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2580 Vals.push_back(getEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
Eli Friedmanfee02c62011-07-25 23:16:38 +00002581 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002582 case Instruction::Call: {
Gabor Greife9afee22010-06-26 09:35:09 +00002583 const CallInst &CI = cast<CallInst>(I);
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002584 FunctionType *FTy = CI.getFunctionType();
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002585
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002586 if (CI.hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002587 writeOperandBundles(&CI, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002588
Chris Lattnerc44070802011-06-17 18:17:37 +00002589 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002590
Gabor Greife9afee22010-06-26 09:35:09 +00002591 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002592
Teresa Johnson37687f32016-04-23 04:30:47 +00002593 unsigned Flags = getOptimizationFlags(&I);
Akira Hatanaka97cb3972015-11-07 02:48:49 +00002594 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2595 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2596 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2597 1 << bitc::CALL_EXPLICIT_TYPE |
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002598 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2599 unsigned(Flags != 0) << bitc::CALL_FMF);
2600 if (Flags != 0)
2601 Vals.push_back(Flags);
2602
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002603 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002604 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002605
Chris Lattner0a603252007-05-01 02:13:26 +00002606 // Emit value #'s for the fixed parameters.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002607 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2608 // Check for labels (can happen with asm labels).
2609 if (FTy->getParamType(i)->isLabelTy())
2610 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2611 else
Teresa Johnson37687f32016-04-23 04:30:47 +00002612 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002613 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002614
Chris Lattnerb811e952007-05-01 07:03:37 +00002615 // Emit type/value pairs for varargs params.
2616 if (FTy->isVarArg()) {
Gabor Greife9afee22010-06-26 09:35:09 +00002617 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002618 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002619 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
Chris Lattner0a603252007-05-01 02:13:26 +00002620 }
2621 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002622 }
Chris Lattner0a603252007-05-01 02:13:26 +00002623 case Instruction::VAArg:
2624 Code = bitc::FUNC_CODE_INST_VAARG;
2625 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
Teresa Johnson37687f32016-04-23 04:30:47 +00002626 pushValue(I.getOperand(0), InstID, Vals); // valist.
Chris Lattner0a603252007-05-01 02:13:26 +00002627 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2628 break;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002629 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002630
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002631 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002632 Vals.clear();
2633}
2634
Teresa Johnson37687f32016-04-23 04:30:47 +00002635/// Emit names for globals/functions etc. \p IsModuleLevel is true when
2636/// we are writing the module-level VST, where we are including a function
2637/// bitcode index and need to backpatch the VST forward declaration record.
2638void ModuleBitcodeWriter::writeValueSymbolTable(
2639 const ValueSymbolTable &VST, bool IsModuleLevel,
2640 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002641 if (VST.empty()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002642 // writeValueSymbolTableForwardDecl should have returned early as
Teresa Johnsonff642b92015-09-17 20:12:00 +00002643 // well. Ensure this handling remains in sync by asserting that
2644 // the placeholder offset is not set.
Teresa Johnson37687f32016-04-23 04:30:47 +00002645 assert(!IsModuleLevel || !hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002646 return;
2647 }
2648
Teresa Johnson37687f32016-04-23 04:30:47 +00002649 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002650 // Get the offset of the VST we are writing, and backpatch it into
2651 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002652 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002653 // The BitcodeStartBit was the stream offset of the actual bitcode
2654 // (e.g. excluding any initial darwin header).
Teresa Johnson37687f32016-04-23 04:30:47 +00002655 VSTOffset -= bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002656 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002657 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002658 }
2659
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002660 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2eae59f2007-05-04 20:34:50 +00002661
Teresa Johnsonff642b92015-09-17 20:12:00 +00002662 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2663 // records, which are not used in the per-function VSTs.
2664 unsigned FnEntry8BitAbbrev;
2665 unsigned FnEntry7BitAbbrev;
2666 unsigned FnEntry6BitAbbrev;
Teresa Johnson37687f32016-04-23 04:30:47 +00002667 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002668 // 8-bit fixed-width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002669 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2670 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002671 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2672 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002673 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2674 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002675 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002676
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002677 // 7-bit fixed width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002678 Abbv = new BitCodeAbbrev();
2679 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002680 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2681 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002682 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2683 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002684 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002685
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002686 // 6-bit char6 VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002687 Abbv = new BitCodeAbbrev();
2688 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002689 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2690 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002691 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2692 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002693 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002694 }
2695
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002696 // FIXME: Set up the abbrev, we know how many values there are!
2697 // FIXME: We know if the type names can use 7-bit ascii.
2698 SmallVector<unsigned, 64> NameVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002699
Yaron Keren001e2e42015-08-10 07:04:29 +00002700 for (const ValueName &Name : VST) {
Chris Lattner4d925982007-05-04 20:52:02 +00002701 // Figure out the encoding to use for the name.
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002702 StringEncoding Bits =
2703 getStringEncoding(Name.getKeyData(), Name.getKeyLength());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002704
Chris Lattnera0987962007-05-04 21:31:13 +00002705 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002706 NameVals.push_back(VE.getValueID(Name.getValue()));
2707
2708 Function *F = dyn_cast<Function>(Name.getValue());
2709 if (!F) {
2710 // If value is an alias, need to get the aliased base object to
2711 // see if it is a function.
2712 auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2713 if (GA && GA->getBaseObject())
2714 F = dyn_cast<Function>(GA->getBaseObject());
2715 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002716
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002717 // VST_CODE_ENTRY: [valueid, namechar x N]
2718 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
2719 // VST_CODE_BBENTRY: [bbid, namechar x N]
Chris Lattner6be58c62007-05-03 22:18:21 +00002720 unsigned Code;
Yaron Keren001e2e42015-08-10 07:04:29 +00002721 if (isa<BasicBlock>(Name.getValue())) {
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002722 Code = bitc::VST_CODE_BBENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002723 if (Bits == SE_Char6)
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002724 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002725 } else if (F && !F->isDeclaration()) {
2726 // Must be the module-level VST, where we pass in the Index and
2727 // have a VSTOffsetPlaceholder. The function-level VST should not
2728 // contain any Function symbols.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00002729 assert(FunctionToBitcodeIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00002730 assert(hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002731
2732 // Save the word offset of the function (from the start of the
2733 // actual bitcode written to the stream).
Teresa Johnson37687f32016-04-23 04:30:47 +00002734 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002735 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2736 NameVals.push_back(BitcodeIndex / 32);
2737
2738 Code = bitc::VST_CODE_FNENTRY;
2739 AbbrevToUse = FnEntry8BitAbbrev;
2740 if (Bits == SE_Char6)
2741 AbbrevToUse = FnEntry6BitAbbrev;
2742 else if (Bits == SE_Fixed7)
2743 AbbrevToUse = FnEntry7BitAbbrev;
Chris Lattner6be58c62007-05-03 22:18:21 +00002744 } else {
2745 Code = bitc::VST_CODE_ENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002746 if (Bits == SE_Char6)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002747 AbbrevToUse = VST_ENTRY_6_ABBREV;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002748 else if (Bits == SE_Fixed7)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002749 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002750 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002751
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002752 for (const auto P : Name.getKey())
2753 NameVals.push_back((unsigned char)P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002754
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002755 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002756 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002757 NameVals.clear();
2758 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002759 Stream.ExitBlock();
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002760}
2761
Teresa Johnson403a7872015-10-04 14:33:43 +00002762/// Emit function names and summary offsets for the combined index
2763/// used by ThinLTO.
Teresa Johnson37687f32016-04-23 04:30:47 +00002764void IndexBitcodeWriter::writeCombinedValueSymbolTable() {
2765 assert(hasVSTOffsetPlaceholder() && "Expected non-zero VSTOffsetPlaceholder");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002766 // Get the offset of the VST we are writing, and backpatch it into
2767 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002768 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002769 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002770 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002771
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002772 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00002773
Teresa Johnson403a7872015-10-04 14:33:43 +00002774 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002775 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2776 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2777 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002778 unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002779
Teresa Johnsone1164de2016-02-10 21:55:02 +00002780 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson37687f32016-04-23 04:30:47 +00002781 for (const auto &GVI : valueIds()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002782 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
2783 NameVals.push_back(GVI.second);
2784 NameVals.push_back(GVI.first);
2785
2786 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002787 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002788 NameVals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00002789 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002790 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002791}
2792
Teresa Johnson37687f32016-04-23 04:30:47 +00002793void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002794 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2795 unsigned Code;
2796 if (isa<BasicBlock>(Order.V))
2797 Code = bitc::USELIST_CODE_BB;
2798 else
2799 Code = bitc::USELIST_CODE_DEFAULT;
2800
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002801 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002802 Record.push_back(VE.getValueID(Order.V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002803 Stream.EmitRecord(Code, Record);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002804}
2805
Teresa Johnson37687f32016-04-23 04:30:47 +00002806void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002807 assert(VE.shouldPreserveUseListOrder() &&
2808 "Expected to be preserving use-list order");
2809
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002810 auto hasMore = [&]() {
2811 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2812 };
2813 if (!hasMore())
2814 // Nothing to do.
2815 return;
2816
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002817 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002818 while (hasMore()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002819 writeUseList(std::move(VE.UseListOrders.back()));
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002820 VE.UseListOrders.pop_back();
2821 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002822 Stream.ExitBlock();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002823}
2824
Teresa Johnson403a7872015-10-04 14:33:43 +00002825/// Emit a function body to the module stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002826void ModuleBitcodeWriter::writeFunction(
2827 const Function &F,
2828 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002829 // Save the bitcode index of the start of this function block for recording
2830 // in the VST.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002831 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002832
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002833 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a11b642011-06-03 17:02:19 +00002834 VE.incorporateFunction(F);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002835
2836 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002837
Chris Lattnere6e364c2007-04-26 05:53:54 +00002838 // Emit the number of basic blocks, so the reader can create them ahead of
2839 // time.
2840 Vals.push_back(VE.getBasicBlocks().size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002841 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002842 Vals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002843
Chris Lattnere6e364c2007-04-26 05:53:54 +00002844 // If there are function-local constants, emit them now.
2845 unsigned CstStart, CstEnd;
2846 VE.getFunctionConstantRange(CstStart, CstEnd);
Teresa Johnson37687f32016-04-23 04:30:47 +00002847 writeConstants(CstStart, CstEnd, false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002848
Victor Hernandez6c730de2010-01-14 01:50:08 +00002849 // If there is function-local metadata, emit it now.
Teresa Johnson37687f32016-04-23 04:30:47 +00002850 writeFunctionMetadata(F);
Victor Hernandez6c730de2010-01-14 01:50:08 +00002851
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002852 // Keep a running idea of what the instruction ID is.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002853 unsigned InstID = CstEnd;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002854
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002855 bool NeedsMetadataAttachment = F.hasMetadata();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002856
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002857 DILocation *LastDL = nullptr;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002858 // Finally, emit all the instructions, in order.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002859 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002860 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2861 I != E; ++I) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002862 writeInstruction(*I, InstID, Vals);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002863
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00002864 if (!I->getType()->isVoidTy())
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002865 ++InstID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002866
Chris Lattner07d09ed2010-04-03 02:17:50 +00002867 // If the instruction has metadata, write a metadata attachment later.
2868 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002869
Chris Lattner07d09ed2010-04-03 02:17:50 +00002870 // If the instruction has a debug location, emit it.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002871 DILocation *DL = I->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002872 if (!DL)
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002873 continue;
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002874
2875 if (DL == LastDL) {
Chris Lattner07d09ed2010-04-03 02:17:50 +00002876 // Just repeat the same debug loc as last time.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002877 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002878 continue;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002879 }
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002880
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002881 Vals.push_back(DL->getLine());
2882 Vals.push_back(DL->getColumn());
2883 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2884 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002885 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002886 Vals.clear();
Duncan P. N. Exon Smith538ef562015-05-06 22:51:12 +00002887
2888 LastDL = DL;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002889 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002890
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002891 // Emit names for all the instructions etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002892 writeValueSymbolTable(F.getValueSymbolTable());
Devang Patelaf206b82009-09-18 19:26:43 +00002893
Chris Lattner07d09ed2010-04-03 02:17:50 +00002894 if (NeedsMetadataAttachment)
Teresa Johnson37687f32016-04-23 04:30:47 +00002895 writeMetadataAttachment(F);
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002896 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00002897 writeUseListBlock(&F);
Chad Rosier6a11b642011-06-03 17:02:19 +00002898 VE.purgeFunction();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002899 Stream.ExitBlock();
Chris Lattner831d4202007-04-26 03:27:58 +00002900}
2901
Chris Lattner702658c2007-05-04 18:26:27 +00002902// Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002903void ModuleBitcodeWriter::writeBlockInfo() {
Chris Lattner702658c2007-05-04 18:26:27 +00002904 // We only want to emit block info records for blocks that have multiple
Jan Wen Voungafaced02012-10-11 20:20:40 +00002905 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
Jan Wen Voung8c9e9412012-10-11 21:45:16 +00002906 // Other blocks can define their abbrevs inline.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002907 Stream.EnterBlockInfoBlock(2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002908
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002909 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002910 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2911 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2912 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2913 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2914 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002915 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002916 VST_ENTRY_8_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002917 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002918 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002919
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002920 { // 7-bit fixed width VST_CODE_ENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002921 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2922 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2923 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2924 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2925 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002926 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002927 VST_ENTRY_7_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002928 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002929 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002930 { // 6-bit char6 VST_CODE_ENTRY strings.
Chris Lattnere760d6f2007-05-05 01:26:50 +00002931 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2932 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2933 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2934 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2935 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002936 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002937 VST_ENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002938 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere760d6f2007-05-05 01:26:50 +00002939 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002940 { // 6-bit char6 VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002941 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2942 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2943 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2944 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnere760d6f2007-05-05 01:26:50 +00002945 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002946 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002947 VST_BBENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002948 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002949 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002950
2951
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002952
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002953 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2954 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2955 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2956 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
David Blaikie7b028102015-02-25 00:51:52 +00002957 VE.computeBitsRequiredForTypeIndicies()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002958 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002959 CONSTANTS_SETTYPE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002960 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002961 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002962
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002963 { // INTEGER abbrev for CONSTANTS_BLOCK.
2964 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2965 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2966 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002967 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002968 CONSTANTS_INTEGER_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002969 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002970 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002971
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002972 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2973 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2974 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2975 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2976 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
David Blaikie7b028102015-02-25 00:51:52 +00002977 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2979
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_CE_CAST_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002982 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002983 }
2984 { // NULL abbrev for CONSTANTS_BLOCK.
2985 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2986 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002987 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002988 CONSTANTS_NULL_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002989 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002990 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002991
Chris Lattnerb80751d2007-05-05 07:44:49 +00002992 // FIXME: This should only use space for first class types!
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002993
Chris Lattnerb80751d2007-05-05 07:44:49 +00002994 { // INST_LOAD abbrev for FUNCTION_BLOCK.
2995 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2996 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattnerb80751d2007-05-05 07:44:49 +00002997 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
David Blaikie85035652015-02-25 01:07:20 +00002998 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2999 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerb80751d2007-05-05 07:44:49 +00003000 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3001 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003002 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003003 FUNCTION_INST_LOAD_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003004 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerb80751d2007-05-05 07:44:49 +00003005 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003006 { // INST_BINOP abbrev for FUNCTION_BLOCK.
3007 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3008 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3009 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3010 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3011 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003012 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003013 FUNCTION_INST_BINOP_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003014 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003015 }
Dan Gohman0ebd6962009-07-20 21:19:07 +00003016 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3017 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3018 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3019 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3020 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3021 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3022 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003023 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003024 FUNCTION_INST_BINOP_FLAGS_ABBREV)
Dan Gohman0ebd6962009-07-20 21:19:07 +00003025 llvm_unreachable("Unexpected abbrev ordering!");
3026 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003027 { // INST_CAST abbrev for FUNCTION_BLOCK.
3028 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3029 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
3030 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3031 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
David Blaikie7b028102015-02-25 00:51:52 +00003032 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003033 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003034 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003035 FUNCTION_INST_CAST_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003036 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003037 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003038
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003039 { // INST_RET abbrev for FUNCTION_BLOCK.
3040 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3041 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003042 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003043 FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003044 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003045 }
3046 { // INST_RET abbrev for FUNCTION_BLOCK.
3047 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3048 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
3049 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003050 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003051 FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003052 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003053 }
3054 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3055 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3056 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003057 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003058 FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003059 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003060 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00003061 {
3062 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3063 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
3064 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
3065 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3066 Log2_32_Ceil(VE.getTypes().size() + 1)));
3067 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3068 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003069 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
David Blaikieb5b5efd2015-02-25 01:08:52 +00003070 FUNCTION_INST_GEP_ABBREV)
3071 llvm_unreachable("Unexpected abbrev ordering!");
3072 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003073
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003074 Stream.ExitBlock();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003075}
3076
Teresa Johnson403a7872015-10-04 14:33:43 +00003077/// Write the module path strings, currently only used when generating
3078/// a combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003079void IndexBitcodeWriter::writeModStrings() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003080 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00003081
3082 // TODO: See which abbrev sizes we actually need to emit
3083
3084 // 8-bit fixed-width MST_ENTRY strings.
3085 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3086 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3087 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3088 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3089 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003090 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003091
3092 // 7-bit fixed width MST_ENTRY strings.
3093 Abbv = new BitCodeAbbrev();
3094 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3095 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3096 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3097 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003098 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003099
3100 // 6-bit char6 MST_ENTRY strings.
3101 Abbv = new BitCodeAbbrev();
3102 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3103 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3104 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3105 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003106 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003107
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003108 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3109 Abbv = new BitCodeAbbrev();
3110 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
3111 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3112 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3113 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3114 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003116 unsigned AbbrevHash = Stream.EmitAbbrev(Abbv);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003117
3118 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003119 for (const auto &MPSE : Index.modulePaths()) {
Teresa Johnson84174c32016-05-10 13:48:23 +00003120 if (!doIncludeModule(MPSE.getKey()))
3121 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00003122 StringEncoding Bits =
3123 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
3124 unsigned AbbrevToUse = Abbrev8Bit;
3125 if (Bits == SE_Char6)
3126 AbbrevToUse = Abbrev6Bit;
3127 else if (Bits == SE_Fixed7)
3128 AbbrevToUse = Abbrev7Bit;
3129
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003130 Vals.push_back(MPSE.getValue().first);
Teresa Johnson403a7872015-10-04 14:33:43 +00003131
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003132 for (const auto P : MPSE.getKey())
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003133 Vals.push_back((unsigned char)P);
Teresa Johnson403a7872015-10-04 14:33:43 +00003134
3135 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003136 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003137
3138 Vals.clear();
3139 // Emit an optional hash for the module now
3140 auto &Hash = MPSE.getValue().second;
3141 bool AllZero = true; // Detect if the hash is empty, and do not generate it
3142 for (auto Val : Hash) {
3143 if (Val)
3144 AllZero = false;
3145 Vals.push_back(Val);
3146 }
3147 if (!AllZero) {
3148 // Emit the hash record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003149 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003150 }
3151
3152 Vals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00003153 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003154 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003155}
3156
3157// Helper to emit a single function summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003158void ModuleBitcodeWriter::writePerModuleFunctionSummaryRecord(
Teresa Johnson28e457b2016-04-24 14:57:11 +00003159 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +00003160 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3161 const Function &F) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003162 NameVals.push_back(ValueID);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003163
Teresa Johnson28e457b2016-04-24 14:57:11 +00003164 FunctionSummary *FS = cast<FunctionSummary>(Summary);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003165 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003166 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003167 NameVals.push_back(FS->refs().size());
3168
3169 for (auto &RI : FS->refs())
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003170 NameVals.push_back(VE.getValueID(RI.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003171
3172 bool HasProfileData = F.getEntryCount().hasValue();
Teresa Johnsonaae26102016-03-25 18:59:13 +00003173 for (auto &ECI : FS->calls()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003174 NameVals.push_back(VE.getValueID(ECI.first.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003175 assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite");
3176 NameVals.push_back(ECI.second.CallsiteCount);
3177 if (HasProfileData)
3178 NameVals.push_back(ECI.second.ProfileCount);
3179 }
3180
3181 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3182 unsigned Code =
3183 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
Teresa Johnson403a7872015-10-04 14:33:43 +00003184
3185 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003186 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003187 NameVals.clear();
3188}
3189
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003190// Collect the global value references in the given variable's initializer,
3191// and emit them in a summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003192void ModuleBitcodeWriter::writeModuleLevelReferences(
3193 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3194 unsigned FSModRefsAbbrev) {
Teresa Johnson13968092016-03-15 19:35:45 +00003195 // Only interested in recording variable defs in the summary.
3196 if (V.isDeclaration())
3197 return;
Teresa Johnson13968092016-03-15 19:35:45 +00003198 NameVals.push_back(VE.getValueID(&V));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003199 NameVals.push_back(getEncodedGVSummaryFlags(V));
Teresa Johnson28e457b2016-04-24 14:57:11 +00003200 auto *Summary = Index->getGlobalValueSummary(V);
3201 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003202 for (auto Ref : VS->refs())
3203 NameVals.push_back(VE.getValueID(Ref.getValue()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003204 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3205 FSModRefsAbbrev);
Teresa Johnson13968092016-03-15 19:35:45 +00003206 NameVals.clear();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003207}
Teresa Johnson403a7872015-10-04 14:33:43 +00003208
Mehdi Amini8fe69362016-04-24 03:18:11 +00003209// Current version for the summary.
3210// This is bumped whenever we introduce changes in the way some record are
3211// interpreted, like flags for instance.
3212static const uint64_t INDEX_VERSION = 1;
3213
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003214/// Emit the per-module summary section alongside the rest of
3215/// the module's bitcode.
Teresa Johnson37687f32016-04-23 04:30:47 +00003216void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003217 if (M.empty())
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003218 return;
3219
Teresa Johnson37687f32016-04-23 04:30:47 +00003220 if (Index->begin() == Index->end())
Teresa Johnsonb35cc692016-04-20 14:39:45 +00003221 return;
3222
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003223 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 4);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003224
Mehdi Amini8fe69362016-04-24 03:18:11 +00003225 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3226
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003227 // Abbrev for FS_PERMODULE.
Teresa Johnson403a7872015-10-04 14:33:43 +00003228 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003229 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003230 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003231 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003232 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003233 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3234 // numrefs x valueid, n x (valueid, callsitecount)
3235 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3236 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003237 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003238
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003239 // Abbrev for FS_PERMODULE_PROFILE.
3240 Abbv = new BitCodeAbbrev();
3241 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003243 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003244 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3245 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3246 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3247 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3248 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003249 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003250
3251 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3252 Abbv = new BitCodeAbbrev();
3253 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3254 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003255 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003256 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3257 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003258 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003259
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003260 // Abbrev for FS_ALIAS.
3261 Abbv = new BitCodeAbbrev();
3262 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3263 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003264 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003265 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003266 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003267
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003268 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003269 // Iterate over the list of functions instead of the Index to
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003270 // ensure the ordering is stable.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003271 for (const Function &F : M) {
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003272 if (F.isDeclaration())
3273 continue;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003274 // Summary emission does not support anonymous functions, they have to
3275 // renamed using the anonymous function renaming pass.
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003276 if (!F.hasName())
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003277 report_fatal_error("Unexpected anonymous function when writing summary");
Teresa Johnson403a7872015-10-04 14:33:43 +00003278
Teresa Johnson28e457b2016-04-24 14:57:11 +00003279 auto *Summary = Index->getGlobalValueSummary(F);
Peter Collingbourne832e7fa2016-05-06 02:41:23 +00003280 writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
3281 FSCallsAbbrev, FSCallsProfileAbbrev, F);
Teresa Johnson403a7872015-10-04 14:33:43 +00003282 }
3283
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003284 // Capture references from GlobalVariable initializers, which are outside
3285 // of a function scope.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003286 for (const GlobalVariable &G : M.globals())
Teresa Johnson37687f32016-04-23 04:30:47 +00003287 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003288
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003289 for (const GlobalAlias &A : M.aliases()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003290 auto *Aliasee = A.getBaseObject();
3291 if (!Aliasee->hasName())
3292 // Nameless function don't have an entry in the summary, skip it.
3293 continue;
3294 auto AliasId = VE.getValueID(&A);
3295 auto AliaseeId = VE.getValueID(Aliasee);
3296 NameVals.push_back(AliasId);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003297 NameVals.push_back(getEncodedGVSummaryFlags(A));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003298 NameVals.push_back(AliaseeId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003299 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003300 NameVals.clear();
3301 }
3302
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003303 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003304}
3305
Teresa Johnson26ab5772016-03-15 00:04:37 +00003306/// Emit the combined summary section into the combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003307void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003308 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Mehdi Amini8fe69362016-04-24 03:18:11 +00003309 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
Teresa Johnson403a7872015-10-04 14:33:43 +00003310
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003311 // Abbrev for FS_COMBINED.
Teresa Johnson403a7872015-10-04 14:33:43 +00003312 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003313 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
Teresa Johnson02e98332016-04-27 13:28:35 +00003314 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003315 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003316 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003317 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3318 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3319 // numrefs x valueid, n x (valueid, callsitecount)
3320 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3321 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003322 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003323
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003324 // Abbrev for FS_COMBINED_PROFILE.
3325 Abbv = new BitCodeAbbrev();
3326 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
Teresa Johnson02e98332016-04-27 13:28:35 +00003327 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003328 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003329 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003330 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3331 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3332 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3333 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3334 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003335 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003336
3337 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
3338 Abbv = new BitCodeAbbrev();
3339 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003340 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003341 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003342 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003343 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3344 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003345 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003346
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003347 // Abbrev for FS_COMBINED_ALIAS.
3348 Abbv = new BitCodeAbbrev();
3349 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003352 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson02e98332016-04-27 13:28:35 +00003353 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003354 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003355
Teresa Johnson02e98332016-04-27 13:28:35 +00003356 // The aliases are emitted as a post-pass, and will point to the value
3357 // id of the aliasee. Save them in a vector for post-processing.
Teresa Johnson28e457b2016-04-24 14:57:11 +00003358 SmallVector<AliasSummary *, 64> Aliases;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003359
Teresa Johnson02e98332016-04-27 13:28:35 +00003360 // Save the value id for each summary for alias emission.
3361 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
3362
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003363 SmallVector<uint64_t, 64> NameVals;
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003364
3365 // For local linkage, we also emit the original name separately
3366 // immediately after the record.
3367 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3368 if (!GlobalValue::isLocalLinkage(S.linkage()))
3369 return;
3370 NameVals.push_back(S.getOriginalName());
3371 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3372 NameVals.clear();
3373 };
3374
Teresa Johnson84174c32016-05-10 13:48:23 +00003375 for (const auto &I : *this) {
3376 GlobalValueSummary *S = I.second;
3377 assert(S);
Teresa Johnson02e98332016-04-27 13:28:35 +00003378
Teresa Johnson84174c32016-05-10 13:48:23 +00003379 assert(hasValueId(I.first));
3380 unsigned ValueId = getValueId(I.first);
3381 SummaryToValueIdMap[S] = ValueId;
Teresa Johnson02e98332016-04-27 13:28:35 +00003382
Teresa Johnson84174c32016-05-10 13:48:23 +00003383 if (auto *AS = dyn_cast<AliasSummary>(S)) {
3384 // Will process aliases as a post-pass because the reader wants all
3385 // global to be loaded first.
3386 Aliases.push_back(AS);
3387 continue;
3388 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003389
Teresa Johnson84174c32016-05-10 13:48:23 +00003390 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003391 NameVals.push_back(ValueId);
Teresa Johnson84174c32016-05-10 13:48:23 +00003392 NameVals.push_back(Index.getModuleId(VS->modulePath()));
3393 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
3394 for (auto &RI : VS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003395 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003396 }
3397
Teresa Johnson403a7872015-10-04 14:33:43 +00003398 // Emit the finished record.
Teresa Johnson84174c32016-05-10 13:48:23 +00003399 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3400 FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003401 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003402 MaybeEmitOriginalName(*S);
Teresa Johnson84174c32016-05-10 13:48:23 +00003403 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00003404 }
Teresa Johnson84174c32016-05-10 13:48:23 +00003405
3406 auto *FS = cast<FunctionSummary>(S);
3407 NameVals.push_back(ValueId);
3408 NameVals.push_back(Index.getModuleId(FS->modulePath()));
3409 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
3410 NameVals.push_back(FS->instCount());
3411 NameVals.push_back(FS->refs().size());
3412
3413 for (auto &RI : FS->refs()) {
3414 NameVals.push_back(getValueId(RI.getGUID()));
3415 }
3416
3417 bool HasProfileData = false;
3418 for (auto &EI : FS->calls()) {
3419 HasProfileData |= EI.second.ProfileCount != 0;
3420 if (HasProfileData)
3421 break;
3422 }
3423
3424 for (auto &EI : FS->calls()) {
3425 // If this GUID doesn't have a value id, it doesn't have a function
3426 // summary and we don't need to record any calls to it.
3427 if (!hasValueId(EI.first.getGUID()))
3428 continue;
3429 NameVals.push_back(getValueId(EI.first.getGUID()));
3430 assert(EI.second.CallsiteCount > 0 && "Expected at least one callsite");
3431 NameVals.push_back(EI.second.CallsiteCount);
3432 if (HasProfileData)
3433 NameVals.push_back(EI.second.ProfileCount);
3434 }
3435
3436 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3437 unsigned Code =
3438 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
3439
3440 // Emit the finished record.
3441 Stream.EmitRecord(Code, NameVals, FSAbbrev);
3442 NameVals.clear();
3443 MaybeEmitOriginalName(*S);
Teresa Johnson403a7872015-10-04 14:33:43 +00003444 }
3445
Teresa Johnson28e457b2016-04-24 14:57:11 +00003446 for (auto *AS : Aliases) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003447 auto AliasValueId = SummaryToValueIdMap[AS];
3448 assert(AliasValueId);
3449 NameVals.push_back(AliasValueId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003450 NameVals.push_back(Index.getModuleId(AS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003451 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
Teresa Johnson02e98332016-04-27 13:28:35 +00003452 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
3453 assert(AliaseeValueId);
3454 NameVals.push_back(AliaseeValueId);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003455
3456 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003457 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003458 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003459 MaybeEmitOriginalName(*AS);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003460 }
3461
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003462 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003463}
3464
Teresa Johnson37687f32016-04-23 04:30:47 +00003465void ModuleBitcodeWriter::writeIdentificationBlock() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003466 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
Mehdi Amini5d303282015-10-26 18:37:00 +00003467
3468 // Write the "user readable" string identifying the bitcode producer
3469 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3470 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3471 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3472 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003473 auto StringAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson37687f32016-04-23 04:30:47 +00003474 writeStringRecord(bitc::IDENTIFICATION_CODE_STRING,
3475 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
Mehdi Amini5d303282015-10-26 18:37:00 +00003476
3477 // Write the epoch version
3478 Abbv = new BitCodeAbbrev();
3479 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3480 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003481 auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini5d303282015-10-26 18:37:00 +00003482 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003483 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3484 Stream.ExitBlock();
Mehdi Amini5d303282015-10-26 18:37:00 +00003485}
3486
Teresa Johnson37687f32016-04-23 04:30:47 +00003487void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003488 // Emit the module's hash.
3489 // MODULE_CODE_HASH: [5*i32]
3490 SHA1 Hasher;
Sjoerd Meijer41beee62016-04-27 18:35:02 +00003491 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003492 Buffer.size() - BlockStartPos));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003493 auto Hash = Hasher.result();
3494 SmallVector<uint64_t, 20> Vals;
3495 auto LShift = [&](unsigned char Val, unsigned Amount)
3496 -> uint64_t { return ((uint64_t)Val) << Amount; };
3497 for (int Pos = 0; Pos < 20; Pos += 4) {
3498 uint32_t SubHash = LShift(Hash[Pos + 0], 24);
3499 SubHash |= LShift(Hash[Pos + 1], 16) | LShift(Hash[Pos + 2], 8) |
3500 (unsigned)(unsigned char)Hash[Pos + 3];
3501 Vals.push_back(SubHash);
3502 }
3503
3504 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003505 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003506}
3507
Teresa Johnson37687f32016-04-23 04:30:47 +00003508void BitcodeWriter::write() {
3509 // Emit the file header first.
3510 writeBitcodeHeader();
3511
3512 writeBlocks();
3513}
3514
3515void ModuleBitcodeWriter::writeBlocks() {
3516 writeIdentificationBlock();
3517 writeModule();
3518}
3519
3520void IndexBitcodeWriter::writeBlocks() {
3521 // Index contains only a single outer (module) block.
3522 writeIndex();
3523}
3524
3525void ModuleBitcodeWriter::writeModule() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003526 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3527 size_t BlockStartPos = Buffer.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003528
Jan Wen Voungafaced02012-10-11 20:20:40 +00003529 SmallVector<unsigned, 1> Vals;
3530 unsigned CurVersion = 1;
3531 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003532 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003533
3534 // Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003535 writeBlockInfo();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003536
Bill Wendlingdc095552013-02-10 23:09:32 +00003537 // Emit information about attribute groups.
Teresa Johnson37687f32016-04-23 04:30:47 +00003538 writeAttributeGroupTable();
Bill Wendlingdc095552013-02-10 23:09:32 +00003539
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003540 // Emit information about parameter attributes.
Teresa Johnson37687f32016-04-23 04:30:47 +00003541 writeAttributeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003542
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003543 // Emit information describing all of the types in the module.
Teresa Johnson37687f32016-04-23 04:30:47 +00003544 writeTypeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003545
Teresa Johnson37687f32016-04-23 04:30:47 +00003546 writeComdats();
David Majnemerdad0a642014-06-27 18:19:56 +00003547
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003548 // Emit top-level description of module, including target triple, inline asm,
3549 // descriptors for global variables, and function prototype info.
Teresa Johnson37687f32016-04-23 04:30:47 +00003550 writeModuleInfo();
Devang Patel7428d8a2009-07-22 17:43:22 +00003551
Devang Patele059ba6e2009-07-23 01:07:34 +00003552 // Emit constants.
Teresa Johnson37687f32016-04-23 04:30:47 +00003553 writeModuleConstants();
Devang Patele059ba6e2009-07-23 01:07:34 +00003554
Devang Patel8cca7b42009-08-04 05:01:35 +00003555 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003556 writeModuleMetadata();
Devang Patel8cca7b42009-08-04 05:01:35 +00003557
Devang Patelaf206b82009-09-18 19:26:43 +00003558 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003559 writeModuleMetadataStore();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003560
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003561 // Emit module-level use-lists.
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003562 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003563 writeUseListBlock(nullptr);
Chad Rosierca2567b2011-12-07 21:44:12 +00003564
Teresa Johnson37687f32016-04-23 04:30:47 +00003565 writeOperandBundleTags();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003566
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003567 // Emit function bodies.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003568 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003569 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003570 if (!F->isDeclaration())
Teresa Johnson37687f32016-04-23 04:30:47 +00003571 writeFunction(*F, FunctionToBitcodeIndex);
Teresa Johnson403a7872015-10-04 14:33:43 +00003572
3573 // Need to write after the above call to WriteFunction which populates
3574 // the summary information in the index.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003575 if (Index)
Teresa Johnson37687f32016-04-23 04:30:47 +00003576 writePerModuleGlobalValueSummary();
Teresa Johnsonff642b92015-09-17 20:12:00 +00003577
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003578 writeValueSymbolTable(M.getValueSymbolTable(),
Teresa Johnson37687f32016-04-23 04:30:47 +00003579 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003580
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003581 if (GenerateHash) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003582 writeModuleHash(BlockStartPos);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003583 }
3584
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003585 Stream.ExitBlock();
Chris Lattner702658c2007-05-04 18:26:27 +00003586}
3587
Teresa Johnson37687f32016-04-23 04:30:47 +00003588static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3589 uint32_t &Position) {
3590 support::endian::write32le(&Buffer[Position], Value);
3591 Position += 4;
3592}
3593
3594/// If generating a bc file on darwin, we have to emit a
Chris Lattnera660f4b2008-07-09 05:14:23 +00003595/// header and trailer to make it compatible with the system archiver. To do
3596/// this we emit the following header, and then emit a trailer that pads the
3597/// file out to be a multiple of 16 bytes.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003598///
Chris Lattnera660f4b2008-07-09 05:14:23 +00003599/// struct bc_header {
3600/// uint32_t Magic; // 0x0B17C0DE
3601/// uint32_t Version; // Version, currently always 0.
3602/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3603/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3604/// uint32_t CPUType; // CPU specifier.
3605/// ... potentially more later ...
3606/// };
Teresa Johnson37687f32016-04-23 04:30:47 +00003607static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003608 const Triple &TT) {
Chris Lattnera660f4b2008-07-09 05:14:23 +00003609 unsigned CPUType = ~0U;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003610
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003611 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng545d3602010-02-12 20:39:35 +00003612 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3613 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3614 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattnera660f4b2008-07-09 05:14:23 +00003615 enum {
3616 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3617 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003618 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattnera660f4b2008-07-09 05:14:23 +00003619 DARWIN_CPU_TYPE_POWERPC = 18
3620 };
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003621
Evan Chengcffdcae2011-06-14 01:51:33 +00003622 Triple::ArchType Arch = TT.getArch();
3623 if (Arch == Triple::x86_64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003624 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003625 else if (Arch == Triple::x86)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003626 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chengcffdcae2011-06-14 01:51:33 +00003627 else if (Arch == Triple::ppc)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003628 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chengcffdcae2011-06-14 01:51:33 +00003629 else if (Arch == Triple::ppc64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003630 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003631 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003632 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003633
Chris Lattnera660f4b2008-07-09 05:14:23 +00003634 // Traditional Bitcode starts after header.
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003635 assert(Buffer.size() >= BWH_HeaderSize &&
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003636 "Expected header size to be reserved");
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003637 unsigned BCOffset = BWH_HeaderSize;
3638 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003639
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003640 // Write the magic and version.
3641 unsigned Position = 0;
Teresa Johnson37687f32016-04-23 04:30:47 +00003642 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
3643 writeInt32ToBuffer(0, Buffer, Position); // Version.
3644 writeInt32ToBuffer(BCOffset, Buffer, Position);
3645 writeInt32ToBuffer(BCSize, Buffer, Position);
3646 writeInt32ToBuffer(CPUType, Buffer, Position);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003647
Chris Lattnera660f4b2008-07-09 05:14:23 +00003648 // If the file is not a multiple of 16 bytes, insert dummy padding.
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003649 while (Buffer.size() & 15)
3650 Buffer.push_back(0);
Chris Lattnera660f4b2008-07-09 05:14:23 +00003651}
3652
Teresa Johnson403a7872015-10-04 14:33:43 +00003653/// Helper to write the header common to all bitcode files.
Teresa Johnson37687f32016-04-23 04:30:47 +00003654void BitcodeWriter::writeBitcodeHeader() {
Teresa Johnson403a7872015-10-04 14:33:43 +00003655 // Emit the file header.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003656 Stream.Emit((unsigned)'B', 8);
3657 Stream.Emit((unsigned)'C', 8);
3658 Stream.Emit(0x0, 4);
3659 Stream.Emit(0xC, 4);
3660 Stream.Emit(0xE, 4);
3661 Stream.Emit(0xD, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00003662}
3663
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003664/// WriteBitcodeToFile - Write the specified module to the specified output
3665/// stream.
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +00003666void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
Teresa Johnson403a7872015-10-04 14:33:43 +00003667 bool ShouldPreserveUseListOrder,
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003668 const ModuleSummaryIndex *Index,
3669 bool GenerateHash) {
Michael Ilsemane26658d2012-12-03 21:29:36 +00003670 SmallVector<char, 0> Buffer;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003671 Buffer.reserve(256*1024);
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003672
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003673 // If this is darwin or another generic macho target, reserve space for the
3674 // header.
3675 Triple TT(M->getTargetTriple());
Akira Hatanaka1235d282016-01-23 16:02:10 +00003676 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003677 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003678
3679 // Emit the module into the buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003680 ModuleBitcodeWriter ModuleWriter(M, Buffer, ShouldPreserveUseListOrder, Index,
3681 GenerateHash);
Teresa Johnson37687f32016-04-23 04:30:47 +00003682 ModuleWriter.write();
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003683
Akira Hatanaka1235d282016-01-23 16:02:10 +00003684 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Teresa Johnson37687f32016-04-23 04:30:47 +00003685 emitDarwinBCHeaderAndTrailer(Buffer, TT);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003686
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003687 // Write the generated bitstream to "Out".
3688 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003689}
Teresa Johnson403a7872015-10-04 14:33:43 +00003690
Teresa Johnson37687f32016-04-23 04:30:47 +00003691void IndexBitcodeWriter::writeIndex() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003692 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Teresa Johnson37687f32016-04-23 04:30:47 +00003693
3694 SmallVector<unsigned, 1> Vals;
3695 unsigned CurVersion = 1;
3696 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003697 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Teresa Johnson37687f32016-04-23 04:30:47 +00003698
3699 // If we have a VST, write the VSTOFFSET record placeholder.
3700 writeValueSymbolTableForwardDecl();
3701
3702 // Write the module paths in the combined index.
3703 writeModStrings();
3704
3705 // Write the summary combined index records.
3706 writeCombinedGlobalValueSummary();
3707
3708 // Need a special VST writer for the combined index (we don't have a
3709 // real VST and real values when this is invoked).
3710 writeCombinedValueSymbolTable();
3711
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003712 Stream.ExitBlock();
Teresa Johnson37687f32016-04-23 04:30:47 +00003713}
3714
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003715// Write the specified module summary index to the given raw output stream,
Teresa Johnson403a7872015-10-04 14:33:43 +00003716// where it will be written in a new bitcode block. This is used when
Teresa Johnson84174c32016-05-10 13:48:23 +00003717// writing the combined index file for ThinLTO. When writing a subset of the
3718// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
3719void llvm::WriteIndexToFile(
3720 const ModuleSummaryIndex &Index, raw_ostream &Out,
3721 std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003722 SmallVector<char, 0> Buffer;
3723 Buffer.reserve(256 * 1024);
3724
Teresa Johnson84174c32016-05-10 13:48:23 +00003725 IndexBitcodeWriter IndexWriter(Buffer, Index, ModuleToSummariesForIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00003726 IndexWriter.write();
Teresa Johnson403a7872015-10-04 14:33:43 +00003727
3728 Out.write((char *)&Buffer.front(), Buffer.size());
3729}