blob: 99aa60f45e97bc6ea8cc11fb66c0f0ca43a71587 [file] [log] [blame]
Chris Lattner87351e22007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerc1d10d62007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "ValueEnumerator.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000015#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000017#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner362b4a12007-04-23 01:01:37 +000018#include "llvm/Bitcode/LLVMBitCodes.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000019#include "llvm/Bitcode/ReaderWriter.h"
Sanjoy Dasb513a9f2015-09-24 23:34:52 +000020#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000022#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/InlineAsm.h"
25#include "llvm/IR/Instructions.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000026#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
28#include "llvm/IR/Operator.h"
Duncan P. N. Exon Smith6b6fdc92014-07-25 14:49:26 +000029#include "llvm/IR/UseListOrder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000032#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000033#include "llvm/Support/Program.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000034#include "llvm/Support/SHA1.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000035#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000036#include <cctype>
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000037#include <map>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000038using namespace llvm;
39
Benjamin Kramera65b6102016-05-15 15:18:11 +000040namespace {
Chris Lattner4d925982007-05-04 20:52:02 +000041/// These are manifest constants used by the bitcode writer. They do not need to
42/// be kept in sync with the reader, but need to be consistent within this file.
43enum {
Chris Lattner4d925982007-05-04 20:52:02 +000044 // VALUE_SYMTAB_BLOCK abbrev id's.
45 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerfd1ad102007-05-04 20:58:35 +000046 VST_ENTRY_7_ABBREV,
Chris Lattnere760d6f2007-05-05 01:26:50 +000047 VST_ENTRY_6_ABBREV,
Chris Lattnerda5e5d22007-05-05 07:36:14 +000048 VST_BBENTRY_6_ABBREV,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000049
Chris Lattnerda5e5d22007-05-05 07:36:14 +000050 // CONSTANTS_BLOCK abbrev id's.
51 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
52 CONSTANTS_INTEGER_ABBREV,
53 CONSTANTS_CE_CAST_Abbrev,
Chris Lattnerb80751d2007-05-05 07:44:49 +000054 CONSTANTS_NULL_Abbrev,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000055
Chris Lattnerb80751d2007-05-05 07:44:49 +000056 // FUNCTION_BLOCK abbrev id's.
Chris Lattnercc6d4c92007-05-06 01:28:01 +000057 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000058 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman0ebd6962009-07-20 21:19:07 +000059 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000060 FUNCTION_INST_CAST_ABBREV,
Chris Lattnercc6d4c92007-05-06 01:28:01 +000061 FUNCTION_INST_RET_VOID_ABBREV,
62 FUNCTION_INST_RET_VAL_ABBREV,
David Blaikieb5b5efd2015-02-25 01:08:52 +000063 FUNCTION_INST_UNREACHABLE_ABBREV,
64 FUNCTION_INST_GEP_ABBREV,
Chris Lattner4d925982007-05-04 20:52:02 +000065};
66
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000067/// Abstract class to manage the bitcode writing, subclassed for each bitcode
68/// file type. Owns the BitstreamWriter, and includes the main entry point for
Teresa Johnson37687f32016-04-23 04:30:47 +000069/// writing.
70class BitcodeWriter {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000071protected:
Teresa Johnson37687f32016-04-23 04:30:47 +000072 /// Pointer to the buffer allocated by caller for bitcode writing.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000073 const SmallVectorImpl<char> &Buffer;
Teresa Johnson37687f32016-04-23 04:30:47 +000074
75 /// The stream created and owned by the BitodeWriter.
76 BitstreamWriter Stream;
77
78 /// Saves the offset of the VSTOffset record that must eventually be
79 /// backpatched with the offset of the actual VST.
80 uint64_t VSTOffsetPlaceholder = 0;
81
82public:
83 /// Constructs a BitcodeWriter object, and initializes a BitstreamRecord,
84 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000085 BitcodeWriter(SmallVectorImpl<char> &Buffer)
86 : Buffer(Buffer), Stream(Buffer) {}
Teresa Johnson37687f32016-04-23 04:30:47 +000087
88 virtual ~BitcodeWriter() = default;
89
90 /// Main entry point to write the bitcode file, which writes the bitcode
91 /// header and will then invoke the virtual writeBlocks() method.
92 void write();
93
94private:
95 /// Derived classes must implement this to write the corresponding blocks for
96 /// that bitcode file type.
97 virtual void writeBlocks() = 0;
98
99protected:
100 bool hasVSTOffsetPlaceholder() { return VSTOffsetPlaceholder != 0; }
Teresa Johnson37687f32016-04-23 04:30:47 +0000101 void writeValueSymbolTableForwardDecl();
102 void writeBitcodeHeader();
103};
104
105/// Class to manage the bitcode writing for a module.
106class ModuleBitcodeWriter : public BitcodeWriter {
107 /// The Module to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000108 const Module &M;
Teresa Johnson37687f32016-04-23 04:30:47 +0000109
110 /// Enumerates ids for all values in the module.
111 ValueEnumerator VE;
112
113 /// Optional per-module index to write for ThinLTO.
114 const ModuleSummaryIndex *Index;
115
116 /// True if a module hash record should be written.
117 bool GenerateHash;
118
119 /// The start bit of the module block, for use in generating a module hash
120 uint64_t BitcodeStartBit = 0;
121
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000122 /// Map that holds the correspondence between GUIDs in the summary index,
123 /// that came from indirect call profiles, and a value id generated by this
124 /// class to use in the VST and summary block records.
125 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
126
127 /// Tracks the last value id recorded in the GUIDToValueMap.
128 unsigned GlobalValueId;
129
Teresa Johnson37687f32016-04-23 04:30:47 +0000130public:
131 /// Constructs a ModuleBitcodeWriter object for the given Module,
132 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000133 ModuleBitcodeWriter(const Module *M, SmallVectorImpl<char> &Buffer,
Teresa Johnson37687f32016-04-23 04:30:47 +0000134 bool ShouldPreserveUseListOrder,
135 const ModuleSummaryIndex *Index, bool GenerateHash)
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000136 : BitcodeWriter(Buffer), M(*M), VE(*M, ShouldPreserveUseListOrder),
Teresa Johnson37687f32016-04-23 04:30:47 +0000137 Index(Index), GenerateHash(GenerateHash) {
138 // Save the start bit of the actual bitcode, in case there is space
139 // saved at the start for the darwin header above. The reader stream
140 // will start at the bitcode, and we need the offset of the VST
141 // to line up.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000142 BitcodeStartBit = Stream.GetCurrentBitNo();
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000143
144 // Assign ValueIds to any callee values in the index that came from
145 // indirect call profiles and were recorded as a GUID not a Value*
146 // (which would have been assigned an ID by the ValueEnumerator).
147 // The starting ValueId is just after the number of values in the
148 // ValueEnumerator, so that they can be emitted in the VST.
149 GlobalValueId = VE.getValues().size();
Teresa Johnsonbb5c4042016-07-18 18:31:50 +0000150 if (!Index)
151 return;
152 for (const auto &GUIDSummaryLists : *Index)
153 // Examine all summaries for this GUID.
154 for (auto &Summary : GUIDSummaryLists.second)
155 if (auto FS = dyn_cast<FunctionSummary>(Summary.get()))
156 // For each call in the function summary, see if the call
157 // is to a GUID (which means it is for an indirect call,
158 // otherwise we would have a Value for it). If so, synthesize
159 // a value id.
160 for (auto &CallEdge : FS->calls())
161 if (CallEdge.first.isGUID())
162 assignValueId(CallEdge.first.getGUID());
Teresa Johnson37687f32016-04-23 04:30:47 +0000163 }
164
165private:
166 /// Main entry point for writing a module to bitcode, invoked by
167 /// BitcodeWriter::write() after it writes the header.
168 void writeBlocks() override;
169
170 /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
171 /// current llvm version, and a record for the epoch number.
172 void writeIdentificationBlock();
173
174 /// Emit the current module to the bitstream.
175 void writeModule();
176
177 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
178
179 void writeStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse);
180 void writeAttributeGroupTable();
181 void writeAttributeTable();
182 void writeTypeTable();
183 void writeComdats();
184 void writeModuleInfo();
185 void writeValueAsMetadata(const ValueAsMetadata *MD,
186 SmallVectorImpl<uint64_t> &Record);
187 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
188 unsigned Abbrev);
189 unsigned createDILocationAbbrev();
190 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
191 unsigned &Abbrev);
192 unsigned createGenericDINodeAbbrev();
193 void writeGenericDINode(const GenericDINode *N,
194 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
195 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
196 unsigned Abbrev);
197 void writeDIEnumerator(const DIEnumerator *N,
198 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
199 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
200 unsigned Abbrev);
201 void writeDIDerivedType(const DIDerivedType *N,
202 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
203 void writeDICompositeType(const DICompositeType *N,
204 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
205 void writeDISubroutineType(const DISubroutineType *N,
206 SmallVectorImpl<uint64_t> &Record,
207 unsigned Abbrev);
208 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
209 unsigned Abbrev);
210 void writeDICompileUnit(const DICompileUnit *N,
211 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
212 void writeDISubprogram(const DISubprogram *N,
213 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
214 void writeDILexicalBlock(const DILexicalBlock *N,
215 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
216 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
217 SmallVectorImpl<uint64_t> &Record,
218 unsigned Abbrev);
219 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
220 unsigned Abbrev);
221 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
222 unsigned Abbrev);
223 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
224 unsigned Abbrev);
225 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
226 unsigned Abbrev);
227 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
228 SmallVectorImpl<uint64_t> &Record,
229 unsigned Abbrev);
230 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
231 SmallVectorImpl<uint64_t> &Record,
232 unsigned Abbrev);
233 void writeDIGlobalVariable(const DIGlobalVariable *N,
234 SmallVectorImpl<uint64_t> &Record,
235 unsigned Abbrev);
236 void writeDILocalVariable(const DILocalVariable *N,
237 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
238 void writeDIExpression(const DIExpression *N,
239 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
240 void writeDIObjCProperty(const DIObjCProperty *N,
241 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
242 void writeDIImportedEntity(const DIImportedEntity *N,
243 SmallVectorImpl<uint64_t> &Record,
244 unsigned Abbrev);
245 unsigned createNamedMetadataAbbrev();
246 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
247 unsigned createMetadataStringsAbbrev();
248 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
249 SmallVectorImpl<uint64_t> &Record);
250 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
251 SmallVectorImpl<uint64_t> &Record);
252 void writeModuleMetadata();
253 void writeFunctionMetadata(const Function &F);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000254 void writeFunctionMetadataAttachment(const Function &F);
255 void writeGlobalVariableMetadataAttachment(const GlobalVariable &GV);
256 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
257 const GlobalObject &GO);
Peter Collingbourne21521892016-06-21 23:42:48 +0000258 void writeModuleMetadataKinds();
Teresa Johnson37687f32016-04-23 04:30:47 +0000259 void writeOperandBundleTags();
260 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
261 void writeModuleConstants();
262 bool pushValueAndType(const Value *V, unsigned InstID,
263 SmallVectorImpl<unsigned> &Vals);
264 void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
265 void pushValue(const Value *V, unsigned InstID,
266 SmallVectorImpl<unsigned> &Vals);
267 void pushValueSigned(const Value *V, unsigned InstID,
268 SmallVectorImpl<uint64_t> &Vals);
269 void writeInstruction(const Instruction &I, unsigned InstID,
270 SmallVectorImpl<unsigned> &Vals);
271 void writeValueSymbolTable(
272 const ValueSymbolTable &VST, bool IsModuleLevel = false,
273 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex = nullptr);
274 void writeUseList(UseListOrder &&Order);
275 void writeUseListBlock(const Function *F);
276 void
277 writeFunction(const Function &F,
278 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
279 void writeBlockInfo();
280 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
Teresa Johnson28e457b2016-04-24 14:57:11 +0000281 GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +0000282 unsigned ValueID,
283 unsigned FSCallsAbbrev,
284 unsigned FSCallsProfileAbbrev,
285 const Function &F);
286 void writeModuleLevelReferences(const GlobalVariable &V,
287 SmallVector<uint64_t, 64> &NameVals,
288 unsigned FSModRefsAbbrev);
289 void writePerModuleGlobalValueSummary();
290 void writeModuleHash(size_t BlockStartPos);
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000291
292 void assignValueId(GlobalValue::GUID ValGUID) {
293 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
294 }
295 unsigned getValueId(GlobalValue::GUID ValGUID) {
296 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
Teresa Johnsonbb5c4042016-07-18 18:31:50 +0000297 // Expect that any GUID value had a value Id assigned by an
298 // earlier call to assignValueId.
299 assert(VMI != GUIDToValueIdMap.end() &&
300 "GUID does not have assigned value Id");
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000301 return VMI->second;
302 }
303 // Helper to get the valueId for the type of value recorded in VI.
304 unsigned getValueId(ValueInfo VI) {
305 if (VI.isGUID())
306 return getValueId(VI.getGUID());
307 return VE.getValueID(VI.getValue());
308 }
309 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
Teresa Johnson37687f32016-04-23 04:30:47 +0000310};
311
312/// Class to manage the bitcode writing for a combined index.
313class IndexBitcodeWriter : public BitcodeWriter {
314 /// The combined index to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000315 const ModuleSummaryIndex &Index;
Teresa Johnson37687f32016-04-23 04:30:47 +0000316
Teresa Johnson84174c32016-05-10 13:48:23 +0000317 /// When writing a subset of the index for distributed backends, client
318 /// provides a map of modules to the corresponding GUIDs/summaries to write.
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +0000319 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
Teresa Johnson84174c32016-05-10 13:48:23 +0000320
Teresa Johnson37687f32016-04-23 04:30:47 +0000321 /// Map that holds the correspondence between the GUID used in the combined
322 /// index and a value id generated by this class to use in references.
323 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
324
325 /// Tracks the last value id recorded in the GUIDToValueMap.
326 unsigned GlobalValueId = 0;
327
328public:
329 /// Constructs a IndexBitcodeWriter object for the given combined index,
Teresa Johnson84174c32016-05-10 13:48:23 +0000330 /// writing to the provided \p Buffer. When writing a subset of the index
331 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000332 IndexBitcodeWriter(SmallVectorImpl<char> &Buffer,
Teresa Johnson84174c32016-05-10 13:48:23 +0000333 const ModuleSummaryIndex &Index,
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +0000334 const std::map<std::string, GVSummaryMapTy>
Teresa Johnson84174c32016-05-10 13:48:23 +0000335 *ModuleToSummariesForIndex = nullptr)
336 : BitcodeWriter(Buffer), Index(Index),
337 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
338 // Assign unique value ids to all summaries to be written, for use
Teresa Johnson37687f32016-04-23 04:30:47 +0000339 // in writing out the call graph edges. Save the mapping from GUID
340 // to the new global value id to use when writing those edges, which
341 // are currently saved in the index in terms of GUID.
Teresa Johnson84174c32016-05-10 13:48:23 +0000342 for (const auto &I : *this)
343 GUIDToValueIdMap[I.first] = ++GlobalValueId;
Teresa Johnson37687f32016-04-23 04:30:47 +0000344 }
345
Teresa Johnson84174c32016-05-10 13:48:23 +0000346 /// The below iterator returns the GUID and associated summary.
347 typedef std::pair<GlobalValue::GUID, GlobalValueSummary *> GVInfo;
348
349 /// Iterator over the value GUID and summaries to be written to bitcode,
350 /// hides the details of whether they are being pulled from the entire
351 /// index or just those in a provided ModuleToSummariesForIndex map.
352 class iterator
353 : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
354 GVInfo> {
355 /// Enables access to parent class.
356 const IndexBitcodeWriter &Writer;
357
358 // Iterators used when writing only those summaries in a provided
359 // ModuleToSummariesForIndex map:
360
361 /// Points to the last element in outer ModuleToSummariesForIndex map.
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +0000362 std::map<std::string, GVSummaryMapTy>::const_iterator ModuleSummariesBack;
Teresa Johnson84174c32016-05-10 13:48:23 +0000363 /// Iterator on outer ModuleToSummariesForIndex map.
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +0000364 std::map<std::string, GVSummaryMapTy>::const_iterator ModuleSummariesIter;
Teresa Johnson84174c32016-05-10 13:48:23 +0000365 /// Iterator on an inner global variable summary map.
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +0000366 GVSummaryMapTy::const_iterator ModuleGVSummariesIter;
Teresa Johnson84174c32016-05-10 13:48:23 +0000367
368 // Iterators used when writing all summaries in the index:
369
370 /// Points to the last element in the Index outer GlobalValueMap.
371 const_gvsummary_iterator IndexSummariesBack;
372 /// Iterator on outer GlobalValueMap.
373 const_gvsummary_iterator IndexSummariesIter;
374 /// Iterator on an inner GlobalValueSummaryList.
375 GlobalValueSummaryList::const_iterator IndexGVSummariesIter;
376
377 public:
378 /// Construct iterator from parent \p Writer and indicate if we are
379 /// constructing the end iterator.
380 iterator(const IndexBitcodeWriter &Writer, bool IsAtEnd) : Writer(Writer) {
381 // Set up the appropriate set of iterators given whether we are writing
382 // the full index or just a subset.
383 // Can't setup the Back or inner iterators if the corresponding map
384 // is empty. This will be handled specially in operator== as well.
385 if (Writer.ModuleToSummariesForIndex &&
386 !Writer.ModuleToSummariesForIndex->empty()) {
Teresa Johnson84174c32016-05-10 13:48:23 +0000387 for (ModuleSummariesBack = Writer.ModuleToSummariesForIndex->begin();
388 std::next(ModuleSummariesBack) !=
389 Writer.ModuleToSummariesForIndex->end();
390 ModuleSummariesBack++)
391 ;
Teresa Johnsone518c802016-05-11 20:46:22 +0000392 ModuleSummariesIter = !IsAtEnd
393 ? Writer.ModuleToSummariesForIndex->begin()
394 : ModuleSummariesBack;
Teresa Johnson84174c32016-05-10 13:48:23 +0000395 ModuleGVSummariesIter = !IsAtEnd ? ModuleSummariesIter->second.begin()
396 : ModuleSummariesBack->second.end();
397 } else if (!Writer.ModuleToSummariesForIndex &&
398 Writer.Index.begin() != Writer.Index.end()) {
Teresa Johnson84174c32016-05-10 13:48:23 +0000399 for (IndexSummariesBack = Writer.Index.begin();
400 std::next(IndexSummariesBack) != Writer.Index.end();
401 IndexSummariesBack++)
402 ;
Teresa Johnsone518c802016-05-11 20:46:22 +0000403 IndexSummariesIter =
404 !IsAtEnd ? Writer.Index.begin() : IndexSummariesBack;
Teresa Johnson84174c32016-05-10 13:48:23 +0000405 IndexGVSummariesIter = !IsAtEnd ? IndexSummariesIter->second.begin()
406 : IndexSummariesBack->second.end();
407 }
408 }
409
410 /// Increment the appropriate set of iterators.
411 iterator &operator++() {
412 // First the inner iterator is incremented, then if it is at the end
413 // and there are more outer iterations to go, the inner is reset to
414 // the start of the next inner list.
415 if (Writer.ModuleToSummariesForIndex) {
416 ++ModuleGVSummariesIter;
417 if (ModuleGVSummariesIter == ModuleSummariesIter->second.end() &&
418 ModuleSummariesIter != ModuleSummariesBack) {
419 ++ModuleSummariesIter;
420 ModuleGVSummariesIter = ModuleSummariesIter->second.begin();
421 }
422 } else {
423 ++IndexGVSummariesIter;
424 if (IndexGVSummariesIter == IndexSummariesIter->second.end() &&
425 IndexSummariesIter != IndexSummariesBack) {
426 ++IndexSummariesIter;
427 IndexGVSummariesIter = IndexSummariesIter->second.begin();
428 }
429 }
430 return *this;
431 }
432
433 /// Access the <GUID,GlobalValueSummary*> pair corresponding to the current
434 /// outer and inner iterator positions.
435 GVInfo operator*() {
436 if (Writer.ModuleToSummariesForIndex)
437 return std::make_pair(ModuleGVSummariesIter->first,
438 ModuleGVSummariesIter->second);
439 return std::make_pair(IndexSummariesIter->first,
440 IndexGVSummariesIter->get());
441 }
442
443 /// Checks if the iterators are equal, with special handling for empty
444 /// indexes.
445 bool operator==(const iterator &RHS) const {
446 if (Writer.ModuleToSummariesForIndex) {
447 // First ensure that both are writing the same subset.
448 if (Writer.ModuleToSummariesForIndex !=
449 RHS.Writer.ModuleToSummariesForIndex)
450 return false;
451 // Already determined above that maps are the same, so if one is
452 // empty, they both are.
453 if (Writer.ModuleToSummariesForIndex->empty())
454 return true;
Teresa Johnsone518c802016-05-11 20:46:22 +0000455 // Ensure the ModuleGVSummariesIter are iterating over the same
456 // container before checking them below.
457 if (ModuleSummariesIter != RHS.ModuleSummariesIter)
458 return false;
Teresa Johnson84174c32016-05-10 13:48:23 +0000459 return ModuleGVSummariesIter == RHS.ModuleGVSummariesIter;
460 }
461 // First ensure RHS also writing the full index, and that both are
462 // writing the same full index.
463 if (RHS.Writer.ModuleToSummariesForIndex ||
464 &Writer.Index != &RHS.Writer.Index)
465 return false;
466 // Already determined above that maps are the same, so if one is
467 // empty, they both are.
468 if (Writer.Index.begin() == Writer.Index.end())
469 return true;
Teresa Johnsone518c802016-05-11 20:46:22 +0000470 // Ensure the IndexGVSummariesIter are iterating over the same
471 // container before checking them below.
472 if (IndexSummariesIter != RHS.IndexSummariesIter)
473 return false;
Teresa Johnson84174c32016-05-10 13:48:23 +0000474 return IndexGVSummariesIter == RHS.IndexGVSummariesIter;
475 }
476 };
477
478 /// Obtain the start iterator over the summaries to be written.
479 iterator begin() { return iterator(*this, /*IsAtEnd=*/false); }
480 /// Obtain the end iterator over the summaries to be written.
481 iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
482
Teresa Johnson37687f32016-04-23 04:30:47 +0000483private:
484 /// Main entry point for writing a combined index to bitcode, invoked by
485 /// BitcodeWriter::write() after it writes the header.
486 void writeBlocks() override;
487
488 void writeIndex();
489 void writeModStrings();
490 void writeCombinedValueSymbolTable();
491 void writeCombinedGlobalValueSummary();
492
Teresa Johnson84174c32016-05-10 13:48:23 +0000493 /// Indicates whether the provided \p ModulePath should be written into
494 /// the module string table, e.g. if full index written or if it is in
495 /// the provided subset.
496 bool doIncludeModule(StringRef ModulePath) {
497 return !ModuleToSummariesForIndex ||
498 ModuleToSummariesForIndex->count(ModulePath);
499 }
500
Teresa Johnson37687f32016-04-23 04:30:47 +0000501 bool hasValueId(GlobalValue::GUID ValGUID) {
502 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
503 return VMI != GUIDToValueIdMap.end();
504 }
505 unsigned getValueId(GlobalValue::GUID ValGUID) {
506 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
507 // If this GUID doesn't have an entry, assign one.
508 if (VMI == GUIDToValueIdMap.end()) {
509 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
510 return GlobalValueId;
511 } else {
512 return VMI->second;
513 }
514 }
Teresa Johnson37687f32016-04-23 04:30:47 +0000515 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
516};
Benjamin Kramera65b6102016-05-15 15:18:11 +0000517} // end anonymous namespace
Teresa Johnson37687f32016-04-23 04:30:47 +0000518
519static unsigned getEncodedCastOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000520 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000521 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000522 case Instruction::Trunc : return bitc::CAST_TRUNC;
523 case Instruction::ZExt : return bitc::CAST_ZEXT;
524 case Instruction::SExt : return bitc::CAST_SEXT;
525 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
526 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
527 case Instruction::UIToFP : return bitc::CAST_UITOFP;
528 case Instruction::SIToFP : return bitc::CAST_SITOFP;
529 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
530 case Instruction::FPExt : return bitc::CAST_FPEXT;
531 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
532 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
533 case Instruction::BitCast : return bitc::CAST_BITCAST;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000534 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000535 }
536}
537
Teresa Johnson37687f32016-04-23 04:30:47 +0000538static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000539 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000540 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmana5b96452009-06-04 22:49:04 +0000541 case Instruction::Add:
542 case Instruction::FAdd: return bitc::BINOP_ADD;
543 case Instruction::Sub:
544 case Instruction::FSub: return bitc::BINOP_SUB;
545 case Instruction::Mul:
546 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000547 case Instruction::UDiv: return bitc::BINOP_UDIV;
548 case Instruction::FDiv:
549 case Instruction::SDiv: return bitc::BINOP_SDIV;
550 case Instruction::URem: return bitc::BINOP_UREM;
551 case Instruction::FRem:
552 case Instruction::SRem: return bitc::BINOP_SREM;
553 case Instruction::Shl: return bitc::BINOP_SHL;
554 case Instruction::LShr: return bitc::BINOP_LSHR;
555 case Instruction::AShr: return bitc::BINOP_ASHR;
556 case Instruction::And: return bitc::BINOP_AND;
557 case Instruction::Or: return bitc::BINOP_OR;
558 case Instruction::Xor: return bitc::BINOP_XOR;
559 }
560}
561
Teresa Johnson37687f32016-04-23 04:30:47 +0000562static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000563 switch (Op) {
564 default: llvm_unreachable("Unknown RMW operation!");
565 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
566 case AtomicRMWInst::Add: return bitc::RMW_ADD;
567 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
568 case AtomicRMWInst::And: return bitc::RMW_AND;
569 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
570 case AtomicRMWInst::Or: return bitc::RMW_OR;
571 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
572 case AtomicRMWInst::Max: return bitc::RMW_MAX;
573 case AtomicRMWInst::Min: return bitc::RMW_MIN;
574 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
575 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
576 }
577}
578
Teresa Johnson37687f32016-04-23 04:30:47 +0000579static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000580 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +0000581 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
582 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
583 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
584 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
585 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
586 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
587 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
Eli Friedmanfee02c62011-07-25 23:16:38 +0000588 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000589 llvm_unreachable("Invalid ordering");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000590}
591
Teresa Johnson37687f32016-04-23 04:30:47 +0000592static unsigned getEncodedSynchScope(SynchronizationScope SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000593 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000594 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
595 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
596 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000597 llvm_unreachable("Invalid synch scope");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000598}
599
Teresa Johnson37687f32016-04-23 04:30:47 +0000600void ModuleBitcodeWriter::writeStringRecord(unsigned Code, StringRef Str,
601 unsigned AbbrevToUse) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000602 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000603
Chris Lattnere14cb882007-05-04 19:11:41 +0000604 // Code: [strchar x N]
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000605 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
606 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
607 AbbrevToUse = 0;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000608 Vals.push_back(Str[i]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000609 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000610
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000611 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000612 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000613}
614
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000615static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
616 switch (Kind) {
617 case Attribute::Alignment:
618 return bitc::ATTR_KIND_ALIGNMENT;
George Burgess IV278199f2016-04-12 01:05:35 +0000619 case Attribute::AllocSize:
620 return bitc::ATTR_KIND_ALLOC_SIZE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000621 case Attribute::AlwaysInline:
622 return bitc::ATTR_KIND_ALWAYS_INLINE;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000623 case Attribute::ArgMemOnly:
624 return bitc::ATTR_KIND_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000625 case Attribute::Builtin:
626 return bitc::ATTR_KIND_BUILTIN;
627 case Attribute::ByVal:
628 return bitc::ATTR_KIND_BY_VAL;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000629 case Attribute::Convergent:
630 return bitc::ATTR_KIND_CONVERGENT;
Reid Klecknera534a382013-12-19 02:14:12 +0000631 case Attribute::InAlloca:
632 return bitc::ATTR_KIND_IN_ALLOCA;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000633 case Attribute::Cold:
634 return bitc::ATTR_KIND_COLD;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000635 case Attribute::InaccessibleMemOnly:
636 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
637 case Attribute::InaccessibleMemOrArgMemOnly:
638 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000639 case Attribute::InlineHint:
640 return bitc::ATTR_KIND_INLINE_HINT;
641 case Attribute::InReg:
642 return bitc::ATTR_KIND_IN_REG;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000643 case Attribute::JumpTable:
644 return bitc::ATTR_KIND_JUMP_TABLE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000645 case Attribute::MinSize:
646 return bitc::ATTR_KIND_MIN_SIZE;
647 case Attribute::Naked:
648 return bitc::ATTR_KIND_NAKED;
649 case Attribute::Nest:
650 return bitc::ATTR_KIND_NEST;
651 case Attribute::NoAlias:
652 return bitc::ATTR_KIND_NO_ALIAS;
653 case Attribute::NoBuiltin:
654 return bitc::ATTR_KIND_NO_BUILTIN;
655 case Attribute::NoCapture:
656 return bitc::ATTR_KIND_NO_CAPTURE;
657 case Attribute::NoDuplicate:
658 return bitc::ATTR_KIND_NO_DUPLICATE;
659 case Attribute::NoImplicitFloat:
660 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
661 case Attribute::NoInline:
662 return bitc::ATTR_KIND_NO_INLINE;
James Molloye6f87ca2015-11-06 10:32:53 +0000663 case Attribute::NoRecurse:
664 return bitc::ATTR_KIND_NO_RECURSE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000665 case Attribute::NonLazyBind:
666 return bitc::ATTR_KIND_NON_LAZY_BIND;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000667 case Attribute::NonNull:
668 return bitc::ATTR_KIND_NON_NULL;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000669 case Attribute::Dereferenceable:
670 return bitc::ATTR_KIND_DEREFERENCEABLE;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000671 case Attribute::DereferenceableOrNull:
672 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000673 case Attribute::NoRedZone:
674 return bitc::ATTR_KIND_NO_RED_ZONE;
675 case Attribute::NoReturn:
676 return bitc::ATTR_KIND_NO_RETURN;
677 case Attribute::NoUnwind:
678 return bitc::ATTR_KIND_NO_UNWIND;
679 case Attribute::OptimizeForSize:
680 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000681 case Attribute::OptimizeNone:
682 return bitc::ATTR_KIND_OPTIMIZE_NONE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000683 case Attribute::ReadNone:
684 return bitc::ATTR_KIND_READ_NONE;
685 case Attribute::ReadOnly:
686 return bitc::ATTR_KIND_READ_ONLY;
687 case Attribute::Returned:
688 return bitc::ATTR_KIND_RETURNED;
689 case Attribute::ReturnsTwice:
690 return bitc::ATTR_KIND_RETURNS_TWICE;
691 case Attribute::SExt:
692 return bitc::ATTR_KIND_S_EXT;
693 case Attribute::StackAlignment:
694 return bitc::ATTR_KIND_STACK_ALIGNMENT;
695 case Attribute::StackProtect:
696 return bitc::ATTR_KIND_STACK_PROTECT;
697 case Attribute::StackProtectReq:
698 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
699 case Attribute::StackProtectStrong:
700 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000701 case Attribute::SafeStack:
702 return bitc::ATTR_KIND_SAFESTACK;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000703 case Attribute::StructRet:
704 return bitc::ATTR_KIND_STRUCT_RET;
705 case Attribute::SanitizeAddress:
706 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
707 case Attribute::SanitizeThread:
708 return bitc::ATTR_KIND_SANITIZE_THREAD;
709 case Attribute::SanitizeMemory:
710 return bitc::ATTR_KIND_SANITIZE_MEMORY;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000711 case Attribute::SwiftError:
712 return bitc::ATTR_KIND_SWIFT_ERROR;
Manman Renf46262e2016-03-29 17:37:21 +0000713 case Attribute::SwiftSelf:
714 return bitc::ATTR_KIND_SWIFT_SELF;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000715 case Attribute::UWTable:
716 return bitc::ATTR_KIND_UW_TABLE;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000717 case Attribute::WriteOnly:
718 return bitc::ATTR_KIND_WRITEONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000719 case Attribute::ZExt:
720 return bitc::ATTR_KIND_Z_EXT;
721 case Attribute::EndAttrKinds:
722 llvm_unreachable("Can not encode end-attribute kinds marker.");
723 case Attribute::None:
724 llvm_unreachable("Can not encode none-attribute.");
725 }
726
727 llvm_unreachable("Trying to encode unknown attribute");
728}
729
Teresa Johnson37687f32016-04-23 04:30:47 +0000730void ModuleBitcodeWriter::writeAttributeGroupTable() {
Bill Wendling92ed7002013-02-11 22:33:26 +0000731 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
732 if (AttrGrps.empty()) return;
Bill Wendlingdc095552013-02-10 23:09:32 +0000733
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000734 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
Bill Wendlingdc095552013-02-10 23:09:32 +0000735
736 SmallVector<uint64_t, 64> Record;
Bill Wendling92ed7002013-02-11 22:33:26 +0000737 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
738 AttributeSet AS = AttrGrps[i];
Bill Wendlingdc095552013-02-10 23:09:32 +0000739 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
740 AttributeSet A = AS.getSlotAttributes(i);
741
Bill Wendling92ed7002013-02-11 22:33:26 +0000742 Record.push_back(VE.getAttributeGroupID(A));
Bill Wendlingdc095552013-02-10 23:09:32 +0000743 Record.push_back(AS.getSlotIndex(i));
744
745 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
746 I != E; ++I) {
747 Attribute Attr = *I;
748 if (Attr.isEnumAttribute()) {
749 Record.push_back(0);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000750 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Hal Finkele15442c2014-07-18 06:51:55 +0000751 } else if (Attr.isIntAttribute()) {
Bill Wendlingdc095552013-02-10 23:09:32 +0000752 Record.push_back(1);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000753 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Bill Wendlingdc095552013-02-10 23:09:32 +0000754 Record.push_back(Attr.getValueAsInt());
755 } else {
756 StringRef Kind = Attr.getKindAsString();
757 StringRef Val = Attr.getValueAsString();
758
759 Record.push_back(Val.empty() ? 3 : 4);
760 Record.append(Kind.begin(), Kind.end());
761 Record.push_back(0);
762 if (!Val.empty()) {
763 Record.append(Val.begin(), Val.end());
764 Record.push_back(0);
765 }
766 }
767 }
768
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000769 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
Bill Wendlingdc095552013-02-10 23:09:32 +0000770 Record.clear();
771 }
772 }
773
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000774 Stream.ExitBlock();
Bill Wendlingdc095552013-02-10 23:09:32 +0000775}
776
Teresa Johnson37687f32016-04-23 04:30:47 +0000777void ModuleBitcodeWriter::writeAttributeTable() {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000778 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000779 if (Attrs.empty()) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000780
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000781 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000782
783 SmallVector<uint64_t, 64> Record;
784 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000785 const AttributeSet &A = Attrs[i];
Bill Wendling0dc08912013-02-12 08:13:50 +0000786 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
787 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000788
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000789 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000790 Record.clear();
791 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000792
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000793 Stream.ExitBlock();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000794}
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000795
796/// WriteTypeTable - Write out the type table for a module.
Teresa Johnson37687f32016-04-23 04:30:47 +0000797void ModuleBitcodeWriter::writeTypeTable() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000798 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000799
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000800 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000801 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000802
David Blaikie7b028102015-02-25 00:51:52 +0000803 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
Chad Rosier9646c0d2011-12-08 00:38:45 +0000804
Chris Lattnerccee7062007-05-05 06:30:12 +0000805 // Abbrev for TYPE_CODE_POINTER.
806 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
807 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000808 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
Christopher Lamb25f50762007-12-12 08:44:39 +0000809 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000810 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000811
Chris Lattnerccee7062007-05-05 06:30:12 +0000812 // Abbrev for TYPE_CODE_FUNCTION.
813 Abbv = new BitCodeAbbrev();
814 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
815 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnerccee7062007-05-05 06:30:12 +0000816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
818
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000819 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000820
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000821 // Abbrev for TYPE_CODE_STRUCT_ANON.
Chris Lattnerccee7062007-05-05 06:30:12 +0000822 Abbv = new BitCodeAbbrev();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000823 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
Chris Lattnerccee7062007-05-05 06:30:12 +0000824 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
825 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000826 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
827
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000828 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000829
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000830 // Abbrev for TYPE_CODE_STRUCT_NAME.
831 Abbv = new BitCodeAbbrev();
832 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
833 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
834 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000835 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000836
837 // Abbrev for TYPE_CODE_STRUCT_NAMED.
838 Abbv = new BitCodeAbbrev();
839 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
840 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
841 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000842 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
843
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000844 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000845
Chris Lattnerccee7062007-05-05 06:30:12 +0000846 // Abbrev for TYPE_CODE_ARRAY.
847 Abbv = new BitCodeAbbrev();
848 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
849 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
Chad Rosier9646c0d2011-12-08 00:38:45 +0000850 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
851
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000852 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000853
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000854 // Emit an entry count so the reader can reserve space.
855 TypeVals.push_back(TypeList.size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000856 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000857 TypeVals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000858
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000859 // Loop over all of the types, emitting each in turn.
860 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000861 Type *T = TypeList[i];
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000862 int AbbrevToUse = 0;
863 unsigned Code = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000864
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000865 switch (T->getTypeID()) {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000866 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
867 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
868 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
869 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
870 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
871 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000872 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000873 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
874 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
875 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
David Majnemerb611e3f2015-08-14 05:09:07 +0000876 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000877 case Type::IntegerTyID:
878 // INTEGER: [width]
879 Code = bitc::TYPE_CODE_INTEGER;
880 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
881 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000882 case Type::PointerTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000883 PointerType *PTy = cast<PointerType>(T);
Christopher Lamb25f50762007-12-12 08:44:39 +0000884 // POINTER: [pointee type, address space]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000885 Code = bitc::TYPE_CODE_POINTER;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000886 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lamb25f50762007-12-12 08:44:39 +0000887 unsigned AddressSpace = PTy->getAddressSpace();
888 TypeVals.push_back(AddressSpace);
889 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000890 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000891 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000892 case Type::FunctionTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000893 FunctionType *FT = cast<FunctionType>(T);
Chad Rosier95898722011-11-03 00:14:01 +0000894 // FUNCTION: [isvararg, retty, paramty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000895 Code = bitc::TYPE_CODE_FUNCTION;
896 TypeVals.push_back(FT->isVarArg());
897 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000898 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
899 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerccee7062007-05-05 06:30:12 +0000900 AbbrevToUse = FunctionAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000901 break;
902 }
903 case Type::StructTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000904 StructType *ST = cast<StructType>(T);
Chris Lattnerccee7062007-05-05 06:30:12 +0000905 // STRUCT: [ispacked, eltty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000906 TypeVals.push_back(ST->isPacked());
Chris Lattnere14cb882007-05-04 19:11:41 +0000907 // Output all of the element types.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000908 for (StructType::element_iterator I = ST->element_begin(),
909 E = ST->element_end(); I != E; ++I)
910 TypeVals.push_back(VE.getTypeID(*I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000911
Chris Lattner335d3992011-08-12 18:06:37 +0000912 if (ST->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000913 Code = bitc::TYPE_CODE_STRUCT_ANON;
914 AbbrevToUse = StructAnonAbbrev;
915 } else {
916 if (ST->isOpaque()) {
917 Code = bitc::TYPE_CODE_OPAQUE;
918 } else {
919 Code = bitc::TYPE_CODE_STRUCT_NAMED;
920 AbbrevToUse = StructNamedAbbrev;
921 }
922
923 // Emit the name if it is present.
924 if (!ST->getName().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000925 writeStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
926 StructNameAbbrev);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000927 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000928 break;
929 }
930 case Type::ArrayTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000931 ArrayType *AT = cast<ArrayType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000932 // ARRAY: [numelts, eltty]
933 Code = bitc::TYPE_CODE_ARRAY;
934 TypeVals.push_back(AT->getNumElements());
935 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerccee7062007-05-05 06:30:12 +0000936 AbbrevToUse = ArrayAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000937 break;
938 }
939 case Type::VectorTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000940 VectorType *VT = cast<VectorType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000941 // VECTOR [numelts, eltty]
942 Code = bitc::TYPE_CODE_VECTOR;
943 TypeVals.push_back(VT->getNumElements());
944 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
945 break;
946 }
947 }
948
949 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000950 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000951 TypeVals.clear();
952 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000953
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000954 Stream.ExitBlock();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000955}
956
Teresa Johnson5e22e442016-02-06 16:07:35 +0000957static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
958 switch (Linkage) {
Rafael Espindola261d25b2015-01-08 16:25:01 +0000959 case GlobalValue::ExternalLinkage:
960 return 0;
961 case GlobalValue::WeakAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000962 return 16;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000963 case GlobalValue::AppendingLinkage:
964 return 2;
965 case GlobalValue::InternalLinkage:
966 return 3;
967 case GlobalValue::LinkOnceAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000968 return 18;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000969 case GlobalValue::ExternalWeakLinkage:
970 return 7;
971 case GlobalValue::CommonLinkage:
972 return 8;
973 case GlobalValue::PrivateLinkage:
974 return 9;
975 case GlobalValue::WeakODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000976 return 17;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000977 case GlobalValue::LinkOnceODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000978 return 19;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000979 case GlobalValue::AvailableExternallyLinkage:
980 return 12;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000981 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000982 llvm_unreachable("Invalid linkage");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000983}
984
Teresa Johnson5e22e442016-02-06 16:07:35 +0000985static unsigned getEncodedLinkage(const GlobalValue &GV) {
986 return getEncodedLinkage(GV.getLinkage());
987}
988
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000989// Decode the flags for GlobalValue in the summary
990static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
991 uint64_t RawFlags = 0;
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000992
993 RawFlags |= Flags.HasSection; // bool
Piotr Padlewski332b3b22016-08-11 22:13:57 +0000994 RawFlags |= (Flags.IsNotViableToInline << 1);
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000995 // Linkage don't need to be remapped at that time for the summary. Any future
996 // change to the getEncodedLinkage() function will need to be taken into
997 // account here as well.
998 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
999
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00001000 return RawFlags;
1001}
1002
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001003static unsigned getEncodedVisibility(const GlobalValue &GV) {
1004 switch (GV.getVisibility()) {
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +00001005 case GlobalValue::DefaultVisibility: return 0;
1006 case GlobalValue::HiddenVisibility: return 1;
1007 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001008 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00001009 llvm_unreachable("Invalid visibility");
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001010}
1011
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001012static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
1013 switch (GV.getDLLStorageClass()) {
Nico Rieck7157bb72014-01-14 15:22:47 +00001014 case GlobalValue::DefaultStorageClass: return 0;
1015 case GlobalValue::DLLImportStorageClass: return 1;
1016 case GlobalValue::DLLExportStorageClass: return 2;
1017 }
1018 llvm_unreachable("Invalid DLL storage class");
1019}
1020
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001021static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001022 switch (GV.getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001023 case GlobalVariable::NotThreadLocal: return 0;
1024 case GlobalVariable::GeneralDynamicTLSModel: return 1;
1025 case GlobalVariable::LocalDynamicTLSModel: return 2;
1026 case GlobalVariable::InitialExecTLSModel: return 3;
1027 case GlobalVariable::LocalExecTLSModel: return 4;
1028 }
1029 llvm_unreachable("Invalid TLS model");
1030}
1031
David Majnemerdad0a642014-06-27 18:19:56 +00001032static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1033 switch (C.getSelectionKind()) {
1034 case Comdat::Any:
1035 return bitc::COMDAT_SELECTION_KIND_ANY;
1036 case Comdat::ExactMatch:
1037 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
1038 case Comdat::Largest:
1039 return bitc::COMDAT_SELECTION_KIND_LARGEST;
1040 case Comdat::NoDuplicates:
1041 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
1042 case Comdat::SameSize:
1043 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
1044 }
1045 llvm_unreachable("Invalid selection kind");
1046}
1047
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001048static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1049 switch (GV.getUnnamedAddr()) {
1050 case GlobalValue::UnnamedAddr::None: return 0;
1051 case GlobalValue::UnnamedAddr::Local: return 2;
1052 case GlobalValue::UnnamedAddr::Global: return 1;
1053 }
1054 llvm_unreachable("Invalid unnamed_addr");
1055}
1056
Teresa Johnson37687f32016-04-23 04:30:47 +00001057void ModuleBitcodeWriter::writeComdats() {
David Majnemer90a021f2016-03-13 08:01:03 +00001058 SmallVector<unsigned, 64> Vals;
David Majnemerdad0a642014-06-27 18:19:56 +00001059 for (const Comdat *C : VE.getComdats()) {
1060 // COMDAT: [selection_kind, name]
1061 Vals.push_back(getEncodedComdatSelectionKind(*C));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +00001062 size_t Size = C->getName().size();
David Majnemer90a021f2016-03-13 08:01:03 +00001063 assert(isUInt<32>(Size));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +00001064 Vals.push_back(Size);
David Majnemerdad0a642014-06-27 18:19:56 +00001065 for (char Chr : C->getName())
1066 Vals.push_back((unsigned char)Chr);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001067 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
David Majnemerdad0a642014-06-27 18:19:56 +00001068 Vals.clear();
1069 }
1070}
1071
Teresa Johnsonff642b92015-09-17 20:12:00 +00001072/// Write a record that will eventually hold the word offset of the
1073/// module-level VST. For now the offset is 0, which will be backpatched
Teresa Johnson37687f32016-04-23 04:30:47 +00001074/// after the real VST is written. Saves the bit offset to backpatch.
1075void BitcodeWriter::writeValueSymbolTableForwardDecl() {
Teresa Johnsonff642b92015-09-17 20:12:00 +00001076 // Write a placeholder value in for the offset of the real VST,
1077 // which is written after the function blocks so that it can include
1078 // the offset of each function. The placeholder offset will be
1079 // updated when the real VST is written.
1080 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1081 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
1082 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1083 // hold the real VST offset. Must use fixed instead of VBR as we don't
1084 // know how many VBR chunks to reserve ahead of time.
1085 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001086 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001087
1088 // Emit the placeholder
1089 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001090 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001091
Teresa Johnson37687f32016-04-23 04:30:47 +00001092 // Compute and save the bit offset to the placeholder, which will be
Teresa Johnsonff642b92015-09-17 20:12:00 +00001093 // patched when the real VST is written. We can simply subtract the 32-bit
1094 // fixed size from the current bit number to get the location to backpatch.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001095 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
Teresa Johnsonff642b92015-09-17 20:12:00 +00001096}
1097
Teresa Johnsone1164de2016-02-10 21:55:02 +00001098enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
1099
1100/// Determine the encoding to use for the given string name and length.
1101static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
1102 bool isChar6 = true;
1103 for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
1104 if (isChar6)
1105 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1106 if ((unsigned char)*C & 128)
1107 // don't bother scanning the rest.
1108 return SE_Fixed8;
1109 }
1110 if (isChar6)
1111 return SE_Char6;
1112 else
1113 return SE_Fixed7;
1114}
1115
Teresa Johnsonff642b92015-09-17 20:12:00 +00001116/// Emit top-level description of module, including target triple, inline asm,
1117/// descriptors for global variables, and function prototype info.
1118/// Returns the bit offset to backpatch with the location of the real VST.
Teresa Johnson37687f32016-04-23 04:30:47 +00001119void ModuleBitcodeWriter::writeModuleInfo() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001120 // Emit various pieces of data attached to a module.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001121 if (!M.getTargetTriple().empty())
1122 writeStringRecord(bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001123 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001124 const std::string &DL = M.getDataLayoutStr();
Rafael Espindolaf863ee22014-02-25 20:01:08 +00001125 if (!DL.empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001126 writeStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001127 if (!M.getModuleInlineAsm().empty())
1128 writeStringRecord(bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001129 0 /*TODO*/);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001130
Gordon Henriksend930f912008-08-17 18:44:35 +00001131 // Emit information about sections and GC, computing how many there are. Also
1132 // compute the maximum alignment value.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001133 std::map<std::string, unsigned> SectionMap;
Gordon Henriksend930f912008-08-17 18:44:35 +00001134 std::map<std::string, unsigned> GCMap;
Chris Lattner4b00d922007-04-23 16:04:05 +00001135 unsigned MaxAlignment = 0;
Chris Lattnerb5491372007-04-23 18:58:34 +00001136 unsigned MaxGlobalType = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001137 for (const GlobalValue &GV : M.globals()) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001138 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
David Blaikie1a848da2015-04-27 19:58:56 +00001139 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001140 if (GV.hasSection()) {
Chad Rosier75ec09c2011-08-12 16:45:18 +00001141 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001142 unsigned &Entry = SectionMap[GV.getSection()];
Chad Rosier75ec09c2011-08-12 16:45:18 +00001143 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001144 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1145 0 /*TODO*/);
Chad Rosier75ec09c2011-08-12 16:45:18 +00001146 Entry = SectionMap.size();
1147 }
1148 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001149 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001150 for (const Function &F : M) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001151 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
1152 if (F.hasSection()) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001153 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001154 unsigned &Entry = SectionMap[F.getSection()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001155 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001156 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1157 0 /*TODO*/);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001158 Entry = SectionMap.size();
1159 }
1160 }
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001161 if (F.hasGC()) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001162 // Same for GC names.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001163 unsigned &Entry = GCMap[F.getGC()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001164 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +00001165 writeStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 0 /*TODO*/);
Gordon Henriksend930f912008-08-17 18:44:35 +00001166 Entry = GCMap.size();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001167 }
1168 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001169 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001170
Chris Lattner4b00d922007-04-23 16:04:05 +00001171 // Emit abbrev for globals, now that we know # sections and max alignment.
1172 unsigned SimpleGVarAbbrev = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001173 if (!M.global_empty()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001174 // Add an abbrev for common globals with no visibility or thread localness.
1175 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1176 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2eae59f2007-05-04 20:34:50 +00001177 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001178 Log2_32_Ceil(MaxGlobalType+1)));
David Blaikie1a848da2015-04-27 19:58:56 +00001179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1180 //| explicitType << 1
1181 //| constant
1182 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1183 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1184 if (MaxAlignment == 0) // Alignment.
Chris Lattner4b00d922007-04-23 16:04:05 +00001185 Abbv->Add(BitCodeAbbrevOp(0));
1186 else {
1187 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2eae59f2007-05-04 20:34:50 +00001188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001189 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001190 }
1191 if (SectionMap.empty()) // Section.
1192 Abbv->Add(BitCodeAbbrevOp(0));
1193 else
Chris Lattner2eae59f2007-05-04 20:34:50 +00001194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner1e50c292007-04-24 03:29:47 +00001195 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001196 // Don't bother emitting vis + thread local.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001197 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner4b00d922007-04-23 16:04:05 +00001198 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001199
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001200 // Emit the global variable information.
1201 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001202 for (const GlobalVariable &GV : M.globals()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001203 unsigned AbbrevToUse = 0;
1204
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001205 // GLOBALVAR: [type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001206 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +00001207 // unnamed_addr, externally_initialized, dllstorageclass,
1208 // comdat]
David Blaikie1a848da2015-04-27 19:58:56 +00001209 Vals.push_back(VE.getTypeID(GV.getValueType()));
1210 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001211 Vals.push_back(GV.isDeclaration() ? 0 :
1212 (VE.getValueID(GV.getInitializer()) + 1));
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001213 Vals.push_back(getEncodedLinkage(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001214 Vals.push_back(Log2_32(GV.getAlignment())+1);
1215 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
1216 if (GV.isThreadLocal() ||
1217 GV.getVisibility() != GlobalValue::DefaultVisibility ||
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001218 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1219 GV.isExternallyInitialized() ||
David Majnemerdad0a642014-06-27 18:19:56 +00001220 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1221 GV.hasComdat()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001222 Vals.push_back(getEncodedVisibility(GV));
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001223 Vals.push_back(getEncodedThreadLocalMode(GV));
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001224 Vals.push_back(getEncodedUnnamedAddr(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001225 Vals.push_back(GV.isExternallyInitialized());
Nico Rieck7157bb72014-01-14 15:22:47 +00001226 Vals.push_back(getEncodedDLLStorageClass(GV));
David Majnemerdad0a642014-06-27 18:19:56 +00001227 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
Chris Lattner4b00d922007-04-23 16:04:05 +00001228 } else {
1229 AbbrevToUse = SimpleGVarAbbrev;
1230 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001231
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001232 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001233 Vals.clear();
1234 }
1235
1236 // Emit the function proto information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001237 for (const Function &F : M) {
Chad Rosier42ee1522011-12-07 23:57:55 +00001238 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001239 // section, visibility, gc, unnamed_addr, prologuedata,
David Majnemer7fddecc2015-06-17 20:52:32 +00001240 // dllstorageclass, comdat, prefixdata, personalityfn]
David Blaikie561a1572015-04-17 16:28:26 +00001241 Vals.push_back(VE.getTypeID(F.getFunctionType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001242 Vals.push_back(F.getCallingConv());
1243 Vals.push_back(F.isDeclaration());
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001244 Vals.push_back(getEncodedLinkage(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001245 Vals.push_back(VE.getAttributeID(F.getAttributes()));
1246 Vals.push_back(Log2_32(F.getAlignment())+1);
1247 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001248 Vals.push_back(getEncodedVisibility(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001249 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001250 Vals.push_back(getEncodedUnnamedAddr(F));
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001251 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1252 : 0);
Nico Rieck7157bb72014-01-14 15:22:47 +00001253 Vals.push_back(getEncodedDLLStorageClass(F));
David Majnemerdad0a642014-06-27 18:19:56 +00001254 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001255 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1256 : 0);
David Majnemer7fddecc2015-06-17 20:52:32 +00001257 Vals.push_back(
1258 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001259
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001260 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001261 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001262 Vals.clear();
1263 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001264
Chris Lattner44c17072007-04-26 02:46:40 +00001265 // Emit the alias information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001266 for (const GlobalAlias &A : M.aliases()) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001267 // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass,
1268 // threadlocal, unnamed_addr]
David Blaikie6a51dbd2015-09-17 22:18:59 +00001269 Vals.push_back(VE.getTypeID(A.getValueType()));
1270 Vals.push_back(A.getType()->getAddressSpace());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001271 Vals.push_back(VE.getValueID(A.getAliasee()));
1272 Vals.push_back(getEncodedLinkage(A));
1273 Vals.push_back(getEncodedVisibility(A));
1274 Vals.push_back(getEncodedDLLStorageClass(A));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001275 Vals.push_back(getEncodedThreadLocalMode(A));
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001276 Vals.push_back(getEncodedUnnamedAddr(A));
Chris Lattner44c17072007-04-26 02:46:40 +00001277 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001278 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
Chris Lattner44c17072007-04-26 02:46:40 +00001279 Vals.clear();
1280 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001281
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001282 // Emit the ifunc information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001283 for (const GlobalIFunc &I : M.ifuncs()) {
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001284 // IFUNC: [ifunc type, address space, resolver val#, linkage, visibility]
1285 Vals.push_back(VE.getTypeID(I.getValueType()));
1286 Vals.push_back(I.getType()->getAddressSpace());
1287 Vals.push_back(VE.getValueID(I.getResolver()));
1288 Vals.push_back(getEncodedLinkage(I));
1289 Vals.push_back(getEncodedVisibility(I));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001290 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001291 Vals.clear();
1292 }
1293
Teresa Johnsone1164de2016-02-10 21:55:02 +00001294 // Emit the module's source file name.
1295 {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001296 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1297 M.getSourceFileName().size());
Teresa Johnsone1164de2016-02-10 21:55:02 +00001298 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1299 if (Bits == SE_Char6)
1300 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1301 else if (Bits == SE_Fixed7)
1302 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1303
1304 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1305 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1306 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1307 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1308 Abbv->Add(AbbrevOpToUse);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001309 unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001310
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001311 for (const auto P : M.getSourceFileName())
Teresa Johnsone1164de2016-02-10 21:55:02 +00001312 Vals.push_back((unsigned char)P);
1313
1314 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001315 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001316 Vals.clear();
1317 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001318
Teresa Johnson37687f32016-04-23 04:30:47 +00001319 // If we have a VST, write the VSTOFFSET record placeholder.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001320 if (M.getValueSymbolTable().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001321 return;
1322 writeValueSymbolTableForwardDecl();
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001323}
1324
Teresa Johnson37687f32016-04-23 04:30:47 +00001325static uint64_t getOptimizationFlags(const Value *V) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001326 uint64_t Flags = 0;
1327
Sanjay Patelc00017d2014-10-15 17:45:13 +00001328 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001329 if (OBO->hasNoSignedWrap())
1330 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1331 if (OBO->hasNoUnsignedWrap())
1332 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001333 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
Chris Lattner35315d02011-02-06 21:44:57 +00001334 if (PEO->isExact())
1335 Flags |= 1 << bitc::PEO_EXACT;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001336 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001337 if (FPMO->hasUnsafeAlgebra())
Michael Ilseman65f14352012-12-09 21:12:04 +00001338 Flags |= FastMathFlags::UnsafeAlgebra;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001339 if (FPMO->hasNoNaNs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001340 Flags |= FastMathFlags::NoNaNs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001341 if (FPMO->hasNoInfs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001342 Flags |= FastMathFlags::NoInfs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001343 if (FPMO->hasNoSignedZeros())
Michael Ilseman65f14352012-12-09 21:12:04 +00001344 Flags |= FastMathFlags::NoSignedZeros;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001345 if (FPMO->hasAllowReciprocal())
Michael Ilseman65f14352012-12-09 21:12:04 +00001346 Flags |= FastMathFlags::AllowReciprocal;
Dan Gohman0ebd6962009-07-20 21:19:07 +00001347 }
1348
1349 return Flags;
1350}
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001351
Teresa Johnson37687f32016-04-23 04:30:47 +00001352void ModuleBitcodeWriter::writeValueAsMetadata(
1353 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001354 // Mimic an MDNode with a value as one operand.
1355 Value *V = MD->getValue();
1356 Record.push_back(VE.getTypeID(V->getType()));
1357 Record.push_back(VE.getValueID(V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001358 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001359 Record.clear();
1360}
1361
Teresa Johnson37687f32016-04-23 04:30:47 +00001362void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1363 SmallVectorImpl<uint64_t> &Record,
1364 unsigned Abbrev) {
Chris Lattner9b493022009-12-31 01:22:29 +00001365 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001366 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001367 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1368 "Unexpected function-local metadata");
1369 Record.push_back(VE.getMetadataOrNullID(MD));
Devang Patel8cca7b42009-08-04 05:01:35 +00001370 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001371 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1372 : bitc::METADATA_NODE,
1373 Record, Abbrev);
Devang Patel8cca7b42009-08-04 05:01:35 +00001374 Record.clear();
1375}
Devang Patele059ba6e2009-07-23 01:07:34 +00001376
Teresa Johnson37687f32016-04-23 04:30:47 +00001377unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001378 // Assume the column is usually under 128, and always output the inlined-at
1379 // location (it's never more expensive than building an array size 1).
1380 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1381 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1382 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1383 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1384 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1385 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1386 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001387 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001388}
1389
Teresa Johnson37687f32016-04-23 04:30:47 +00001390void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1391 SmallVectorImpl<uint64_t> &Record,
1392 unsigned &Abbrev) {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001393 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001394 Abbrev = createDILocationAbbrev();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001395
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001396 Record.push_back(N->isDistinct());
1397 Record.push_back(N->getLine());
1398 Record.push_back(N->getColumn());
1399 Record.push_back(VE.getMetadataID(N->getScope()));
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001400 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001401
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001402 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001403 Record.clear();
1404}
1405
Teresa Johnson37687f32016-04-23 04:30:47 +00001406unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001407 // Assume the column is usually under 128, and always output the inlined-at
1408 // location (it's never more expensive than building an array size 1).
1409 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1410 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1411 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1412 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1413 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1414 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1415 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1416 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001417 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001418}
1419
Teresa Johnson37687f32016-04-23 04:30:47 +00001420void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1421 SmallVectorImpl<uint64_t> &Record,
1422 unsigned &Abbrev) {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001423 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001424 Abbrev = createGenericDINodeAbbrev();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001425
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001426 Record.push_back(N->isDistinct());
1427 Record.push_back(N->getTag());
1428 Record.push_back(0); // Per-tag version field; unused for now.
1429
1430 for (auto &I : N->operands())
1431 Record.push_back(VE.getMetadataOrNullID(I));
1432
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001433 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001434 Record.clear();
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001435}
1436
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001437static uint64_t rotateSign(int64_t I) {
1438 uint64_t U = I;
1439 return I < 0 ? ~(U << 1) : U << 1;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001440}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001441
Teresa Johnson37687f32016-04-23 04:30:47 +00001442void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1443 SmallVectorImpl<uint64_t> &Record,
1444 unsigned Abbrev) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001445 Record.push_back(N->isDistinct());
1446 Record.push_back(N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001447 Record.push_back(rotateSign(N->getLowerBound()));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001448
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001449 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001450 Record.clear();
1451}
1452
Teresa Johnson37687f32016-04-23 04:30:47 +00001453void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1454 SmallVectorImpl<uint64_t> &Record,
1455 unsigned Abbrev) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001456 Record.push_back(N->isDistinct());
1457 Record.push_back(rotateSign(N->getValue()));
1458 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1459
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001460 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001461 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001462}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001463
Teresa Johnson37687f32016-04-23 04:30:47 +00001464void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1465 SmallVectorImpl<uint64_t> &Record,
1466 unsigned Abbrev) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001467 Record.push_back(N->isDistinct());
1468 Record.push_back(N->getTag());
1469 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1470 Record.push_back(N->getSizeInBits());
1471 Record.push_back(N->getAlignInBits());
1472 Record.push_back(N->getEncoding());
1473
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001474 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001475 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001476}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001477
Teresa Johnson37687f32016-04-23 04:30:47 +00001478void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1479 SmallVectorImpl<uint64_t> &Record,
1480 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001481 Record.push_back(N->isDistinct());
1482 Record.push_back(N->getTag());
1483 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1484 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1485 Record.push_back(N->getLine());
1486 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001487 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001488 Record.push_back(N->getSizeInBits());
1489 Record.push_back(N->getAlignInBits());
1490 Record.push_back(N->getOffsetInBits());
1491 Record.push_back(N->getFlags());
1492 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1493
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001494 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001495 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001496}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001497
Teresa Johnson37687f32016-04-23 04:30:47 +00001498void ModuleBitcodeWriter::writeDICompositeType(
1499 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1500 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001501 const unsigned IsNotUsedInOldTypeRef = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001502 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001503 Record.push_back(N->getTag());
1504 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1505 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1506 Record.push_back(N->getLine());
1507 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1508 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1509 Record.push_back(N->getSizeInBits());
1510 Record.push_back(N->getAlignInBits());
1511 Record.push_back(N->getOffsetInBits());
1512 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001513 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001514 Record.push_back(N->getRuntimeLang());
1515 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001516 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001517 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1518
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001519 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001520 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001521}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001522
Teresa Johnson37687f32016-04-23 04:30:47 +00001523void ModuleBitcodeWriter::writeDISubroutineType(
1524 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1525 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001526 const unsigned HasNoOldTypeRefs = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001527 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001528 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001529 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001530 Record.push_back(N->getCC());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001531
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001532 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001533 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001534}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001535
Teresa Johnson37687f32016-04-23 04:30:47 +00001536void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1537 SmallVectorImpl<uint64_t> &Record,
1538 unsigned Abbrev) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001539 Record.push_back(N->isDistinct());
1540 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1541 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1542
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001543 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001544 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001545}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001546
Teresa Johnson37687f32016-04-23 04:30:47 +00001547void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1548 SmallVectorImpl<uint64_t> &Record,
1549 unsigned Abbrev) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001550 assert(N->isDistinct() && "Expected distinct compile units");
1551 Record.push_back(/* IsDistinct */ true);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001552 Record.push_back(N->getSourceLanguage());
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001553 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001554 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1555 Record.push_back(N->isOptimized());
1556 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1557 Record.push_back(N->getRuntimeVersion());
1558 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1559 Record.push_back(N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001560 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1561 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001562 Record.push_back(/* subprograms */ 0);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001563 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1564 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
Adrian Prantl1f599f92015-05-21 20:37:30 +00001565 Record.push_back(N->getDWOId());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001566 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
David Blaikiea01f2952016-08-24 18:29:49 +00001567 Record.push_back(N->getSplitDebugInlining());
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001568
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001569 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001570 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001571}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001572
Teresa Johnson37687f32016-04-23 04:30:47 +00001573void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1574 SmallVectorImpl<uint64_t> &Record,
1575 unsigned Abbrev) {
Adrian Prantl85338cb2016-05-06 22:53:06 +00001576 uint64_t HasUnitFlag = 1 << 1;
1577 Record.push_back(N->isDistinct() | HasUnitFlag);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001578 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1579 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1580 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1581 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1582 Record.push_back(N->getLine());
1583 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1584 Record.push_back(N->isLocalToUnit());
1585 Record.push_back(N->isDefinition());
1586 Record.push_back(N->getScopeLine());
1587 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1588 Record.push_back(N->getVirtuality());
1589 Record.push_back(N->getVirtualIndex());
1590 Record.push_back(N->getFlags());
1591 Record.push_back(N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001592 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001593 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001594 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001595 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001596 Record.push_back(N->getThisAdjustment());
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001597
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001598 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001599 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001600}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001601
Teresa Johnson37687f32016-04-23 04:30:47 +00001602void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1603 SmallVectorImpl<uint64_t> &Record,
1604 unsigned Abbrev) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001605 Record.push_back(N->isDistinct());
1606 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1607 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1608 Record.push_back(N->getLine());
1609 Record.push_back(N->getColumn());
1610
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001611 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001612 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001613}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001614
Teresa Johnson37687f32016-04-23 04:30:47 +00001615void ModuleBitcodeWriter::writeDILexicalBlockFile(
1616 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1617 unsigned Abbrev) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001618 Record.push_back(N->isDistinct());
1619 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1620 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1621 Record.push_back(N->getDiscriminator());
1622
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001623 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001624 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001625}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001626
Teresa Johnson37687f32016-04-23 04:30:47 +00001627void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1628 SmallVectorImpl<uint64_t> &Record,
1629 unsigned Abbrev) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001630 Record.push_back(N->isDistinct());
1631 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1632 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1633 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1634 Record.push_back(N->getLine());
1635
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001636 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001637 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001638}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001639
Teresa Johnson37687f32016-04-23 04:30:47 +00001640void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1641 SmallVectorImpl<uint64_t> &Record,
1642 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001643 Record.push_back(N->isDistinct());
1644 Record.push_back(N->getMacinfoType());
1645 Record.push_back(N->getLine());
1646 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1647 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1648
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001649 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001650 Record.clear();
1651}
1652
Teresa Johnson37687f32016-04-23 04:30:47 +00001653void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1654 SmallVectorImpl<uint64_t> &Record,
1655 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001656 Record.push_back(N->isDistinct());
1657 Record.push_back(N->getMacinfoType());
1658 Record.push_back(N->getLine());
1659 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1660 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1661
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001662 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001663 Record.clear();
1664}
1665
Teresa Johnson37687f32016-04-23 04:30:47 +00001666void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1667 SmallVectorImpl<uint64_t> &Record,
1668 unsigned Abbrev) {
Adrian Prantlab1243f2015-06-29 23:03:47 +00001669 Record.push_back(N->isDistinct());
1670 for (auto &I : N->operands())
1671 Record.push_back(VE.getMetadataOrNullID(I));
1672
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001673 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
Adrian Prantlab1243f2015-06-29 23:03:47 +00001674 Record.clear();
1675}
1676
Teresa Johnson37687f32016-04-23 04:30:47 +00001677void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1678 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1679 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001680 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001681 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1682 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1683
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001684 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001685 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001686}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001687
Teresa Johnson37687f32016-04-23 04:30:47 +00001688void ModuleBitcodeWriter::writeDITemplateValueParameter(
1689 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1690 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001691 Record.push_back(N->isDistinct());
1692 Record.push_back(N->getTag());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001693 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1694 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1695 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1696
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001697 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001698 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001699}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001700
Teresa Johnson37687f32016-04-23 04:30:47 +00001701void ModuleBitcodeWriter::writeDIGlobalVariable(
1702 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1703 unsigned Abbrev) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001704 Record.push_back(N->isDistinct());
1705 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1706 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1707 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1708 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1709 Record.push_back(N->getLine());
1710 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1711 Record.push_back(N->isLocalToUnit());
1712 Record.push_back(N->isDefinition());
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001713 Record.push_back(VE.getMetadataOrNullID(N->getRawExpr()));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001714 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
Victor Leschuk2ede1262016-10-20 00:13:12 +00001715 Record.push_back(N->getAlignInBits());
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001716
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001717 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001718 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001719}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001720
Teresa Johnson37687f32016-04-23 04:30:47 +00001721void ModuleBitcodeWriter::writeDILocalVariable(
1722 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1723 unsigned Abbrev) {
Victor Leschuk2ede1262016-10-20 00:13:12 +00001724 // In order to support all possible bitcode formats in BitcodeReader we need
1725 // to distiguish the following cases:
1726 // 1) Record has no artificial tag (Record[1]),
1727 // has no obsolete inlinedAt field (Record[9]).
1728 // In this case Record size will be 8, HasAlignment flag is false.
1729 // 2) Record has artificial tag (Record[1]),
1730 // has no obsolete inlignedAt field (Record[9]).
1731 // In this case Record size will be 9, HasAlignment flag is false.
1732 // 3) Record has both artificial tag (Record[1]) and
1733 // obsolete inlignedAt field (Record[9]).
1734 // In this case Record size will be 10, HasAlignment flag is false.
1735 // 4) Record has neither artificial tag, nor inlignedAt field, but
1736 // HasAlignment flag is true and Record[8] contains alignment value.
1737 const uint64_t HasAlignmentFlag = 1 << 1;
1738 Record.push_back(N->isDistinct() | HasAlignmentFlag);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001739 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1740 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1741 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1742 Record.push_back(N->getLine());
1743 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1744 Record.push_back(N->getArg());
1745 Record.push_back(N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00001746 Record.push_back(N->getAlignInBits());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001747
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001748 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001749 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001750}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001751
Teresa Johnson37687f32016-04-23 04:30:47 +00001752void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1753 SmallVectorImpl<uint64_t> &Record,
1754 unsigned Abbrev) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001755 Record.reserve(N->getElements().size() + 1);
1756
1757 Record.push_back(N->isDistinct());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001758 Record.append(N->elements_begin(), N->elements_end());
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001759
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001760 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001761 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001762}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001763
Teresa Johnson37687f32016-04-23 04:30:47 +00001764void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1765 SmallVectorImpl<uint64_t> &Record,
1766 unsigned Abbrev) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001767 Record.push_back(N->isDistinct());
1768 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1769 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1770 Record.push_back(N->getLine());
1771 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1772 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1773 Record.push_back(N->getAttributes());
1774 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1775
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001776 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001777 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001778}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001779
Teresa Johnson37687f32016-04-23 04:30:47 +00001780void ModuleBitcodeWriter::writeDIImportedEntity(
1781 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1782 unsigned Abbrev) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001783 Record.push_back(N->isDistinct());
1784 Record.push_back(N->getTag());
1785 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1786 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1787 Record.push_back(N->getLine());
1788 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1789
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001790 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001791 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001792}
1793
Teresa Johnson37687f32016-04-23 04:30:47 +00001794unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001795 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1796 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001799 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001800}
1801
Teresa Johnson37687f32016-04-23 04:30:47 +00001802void ModuleBitcodeWriter::writeNamedMetadata(
1803 SmallVectorImpl<uint64_t> &Record) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001804 if (M.named_metadata_empty())
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001805 return;
1806
Teresa Johnson37687f32016-04-23 04:30:47 +00001807 unsigned Abbrev = createNamedMetadataAbbrev();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001808 for (const NamedMDNode &NMD : M.named_metadata()) {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001809 // Write name.
1810 StringRef Str = NMD.getName();
1811 Record.append(Str.bytes_begin(), Str.bytes_end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001812 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001813 Record.clear();
1814
1815 // Write named metadata operands.
1816 for (const MDNode *N : NMD.operands())
1817 Record.push_back(VE.getMetadataID(N));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001818 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001819 Record.clear();
1820 }
1821}
1822
Teresa Johnson37687f32016-04-23 04:30:47 +00001823unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001824 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1825 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1826 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1828 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001829 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001830}
1831
1832/// Write out a record for MDString.
1833///
1834/// All the metadata strings in a metadata block are emitted in a single
1835/// record. The sizes and strings themselves are shoved into a blob.
Teresa Johnson37687f32016-04-23 04:30:47 +00001836void ModuleBitcodeWriter::writeMetadataStrings(
1837 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001838 if (Strings.empty())
1839 return;
1840
1841 // Start the record with the number of strings.
1842 Record.push_back(bitc::METADATA_STRINGS);
1843 Record.push_back(Strings.size());
1844
1845 // Emit the sizes of the strings in the blob.
1846 SmallString<256> Blob;
1847 {
1848 BitstreamWriter W(Blob);
1849 for (const Metadata *MD : Strings)
1850 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1851 W.FlushToWord();
1852 }
1853
1854 // Add the offset to the strings to the record.
1855 Record.push_back(Blob.size());
1856
1857 // Add the strings to the blob.
1858 for (const Metadata *MD : Strings)
1859 Blob.append(cast<MDString>(MD)->getString());
1860
1861 // Emit the final record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001862 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001863 Record.clear();
1864}
1865
Teresa Johnson37687f32016-04-23 04:30:47 +00001866void ModuleBitcodeWriter::writeMetadataRecords(
1867 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001868 if (MDs.empty())
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +00001869 return;
1870
Duncan P. N. Exon Smith6b7b6802015-02-04 21:54:12 +00001871 // Initialize MDNode abbreviations.
1872#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1873#include "llvm/IR/Metadata.def"
1874
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001875 for (const Metadata *MD : MDs) {
Duncan P. N. Exon Smith73d5aae2015-01-12 22:31:35 +00001876 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith79f8d112015-03-17 00:16:35 +00001877 assert(N->isResolved() && "Expected forward references to be resolved");
1878
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001879 switch (N->getMetadataID()) {
1880 default:
1881 llvm_unreachable("Invalid MDNode subclass");
1882#define HANDLE_MDNODE_LEAF(CLASS) \
1883 case Metadata::CLASS##Kind: \
Teresa Johnson37687f32016-04-23 04:30:47 +00001884 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001885 continue;
1886#include "llvm/IR/Metadata.def"
1887 }
Devang Patel8cca7b42009-08-04 05:01:35 +00001888 }
Teresa Johnson37687f32016-04-23 04:30:47 +00001889 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
Devang Patel8cca7b42009-08-04 05:01:35 +00001890 }
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001891}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001892
Teresa Johnson37687f32016-04-23 04:30:47 +00001893void ModuleBitcodeWriter::writeModuleMetadata() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001894 if (!VE.hasMDs() && M.named_metadata_empty())
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001895 return;
1896
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001897 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001898 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001899 writeMetadataStrings(VE.getMDStrings(), Record);
1900 writeMetadataRecords(VE.getNonMDStrings(), Record);
1901 writeNamedMetadata(Record);
Peter Collingbourne21521892016-06-21 23:42:48 +00001902
1903 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
1904 SmallVector<uint64_t, 4> Record;
1905 Record.push_back(VE.getValueID(&GO));
1906 pushGlobalMetadataAttachment(Record, GO);
1907 Stream.EmitRecord(bitc::METADATA_GLOBAL_DECL_ATTACHMENT, Record);
1908 };
1909 for (const Function &F : M)
1910 if (F.isDeclaration() && F.hasMetadata())
1911 AddDeclAttachedMetadata(F);
1912 // FIXME: Only store metadata for declarations here, and move data for global
1913 // variable definitions to a separate block (PR28134).
1914 for (const GlobalVariable &GV : M.globals())
1915 if (GV.hasMetadata())
1916 AddDeclAttachedMetadata(GV);
1917
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001918 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001919}
1920
Teresa Johnson37687f32016-04-23 04:30:47 +00001921void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +00001922 if (!VE.hasMDs())
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001923 return;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001924
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001925 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001926 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001927 writeMetadataStrings(VE.getMDStrings(), Record);
1928 writeMetadataRecords(VE.getNonMDStrings(), Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001929 Stream.ExitBlock();
Victor Hernandezb00a6be2010-01-13 19:37:33 +00001930}
1931
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001932void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
1933 SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) {
1934 // [n x [id, mdnode]]
1935 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1936 GO.getAllMetadata(MDs);
1937 for (const auto &I : MDs) {
1938 Record.push_back(I.first);
1939 Record.push_back(VE.getMetadataID(I.second));
1940 }
1941}
1942
1943void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001944 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
Chris Lattner07d09ed2010-04-03 02:17:50 +00001945
Devang Patelaf206b82009-09-18 19:26:43 +00001946 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001947
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001948 if (F.hasMetadata()) {
1949 pushGlobalMetadataAttachment(Record, F);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001950 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001951 Record.clear();
1952 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001953
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001954 // Write metadata attachments
1955 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1956 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001957 for (const BasicBlock &BB : F)
1958 for (const Instruction &I : BB) {
Devang Patel6da5dbf2009-10-22 18:55:16 +00001959 MDs.clear();
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001960 I.getAllMetadataOtherThanDebugLoc(MDs);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001961
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001962 // If no metadata, ignore instruction.
1963 if (MDs.empty()) continue;
1964
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001965 Record.push_back(VE.getInstructionID(&I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001966
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001967 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1968 Record.push_back(MDs[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001969 Record.push_back(VE.getMetadataID(MDs[i].second));
Devang Patelaf206b82009-09-18 19:26:43 +00001970 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001971 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001972 Record.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00001973 }
1974
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001975 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001976}
1977
Peter Collingbourne21521892016-06-21 23:42:48 +00001978void ModuleBitcodeWriter::writeModuleMetadataKinds() {
Devang Patelaf206b82009-09-18 19:26:43 +00001979 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001980
Devang Patelaf206b82009-09-18 19:26:43 +00001981 // Write metadata kinds
1982 // METADATA_KIND - [n x [id, name]]
Michael Ilseman979dfbb2012-12-03 22:57:47 +00001983 SmallVector<StringRef, 8> Names;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001984 M.getMDKindNames(Names);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001985
Dan Gohman43aa8f02010-07-20 21:42:28 +00001986 if (Names.empty()) return;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001987
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001988 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001989
Dan Gohman43aa8f02010-07-20 21:42:28 +00001990 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattnerc9558df2009-12-28 20:10:43 +00001991 Record.push_back(MDKindID);
1992 StringRef KName = Names[MDKindID];
1993 Record.append(KName.begin(), KName.end());
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001994
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001995 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
Devang Patelaf206b82009-09-18 19:26:43 +00001996 Record.clear();
1997 }
1998
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001999 Stream.ExitBlock();
Devang Patel8cca7b42009-08-04 05:01:35 +00002000}
2001
Teresa Johnson37687f32016-04-23 04:30:47 +00002002void ModuleBitcodeWriter::writeOperandBundleTags() {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002003 // Write metadata kinds
2004 //
2005 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2006 //
2007 // OPERAND_BUNDLE_TAG - [strchr x N]
2008
2009 SmallVector<StringRef, 8> Tags;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002010 M.getOperandBundleTags(Tags);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002011
2012 if (Tags.empty())
2013 return;
2014
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002015 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002016
2017 SmallVector<uint64_t, 64> Record;
2018
2019 for (auto Tag : Tags) {
2020 Record.append(Tag.begin(), Tag.end());
2021
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002022 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002023 Record.clear();
2024 }
2025
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002026 Stream.ExitBlock();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002027}
2028
Jan Wen Voungafaced02012-10-11 20:20:40 +00002029static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
2030 if ((int64_t)V >= 0)
2031 Vals.push_back(V << 1);
2032 else
2033 Vals.push_back((-V << 1) | 1);
2034}
2035
Teresa Johnson37687f32016-04-23 04:30:47 +00002036void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2037 bool isGlobal) {
Devang Patel8cca7b42009-08-04 05:01:35 +00002038 if (FirstVal == LastVal) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002039
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002040 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Devang Patel8cca7b42009-08-04 05:01:35 +00002041
2042 unsigned AggregateAbbrev = 0;
2043 unsigned String8Abbrev = 0;
2044 unsigned CString7Abbrev = 0;
2045 unsigned CString6Abbrev = 0;
2046 // If this is a constant pool for the module, emit module-specific abbrevs.
2047 if (isGlobal) {
2048 // Abbrev for CST_CODE_AGGREGATE.
2049 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2050 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
2051 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2052 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002053 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00002054
2055 // Abbrev for CST_CODE_STRING.
2056 Abbv = new BitCodeAbbrev();
2057 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
2058 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2059 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002060 String8Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00002061 // Abbrev for CST_CODE_CSTRING.
2062 Abbv = new BitCodeAbbrev();
2063 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2064 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2065 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002066 CString7Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00002067 // Abbrev for CST_CODE_CSTRING.
2068 Abbv = new BitCodeAbbrev();
2069 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2070 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2071 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002072 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002073 }
2074
Devang Patel8cca7b42009-08-04 05:01:35 +00002075 SmallVector<uint64_t, 64> Record;
2076
2077 const ValueEnumerator::ValueList &Vals = VE.getValues();
Craig Topper2617dcc2014-04-15 06:32:26 +00002078 Type *LastTy = nullptr;
Devang Patel8cca7b42009-08-04 05:01:35 +00002079 for (unsigned i = FirstVal; i != LastVal; ++i) {
2080 const Value *V = Vals[i].first;
Chris Lattner52523562007-04-24 00:16:04 +00002081 // If we need to switch types, do so now.
2082 if (V->getType() != LastTy) {
2083 LastTy = V->getType();
2084 Record.push_back(VE.getTypeID(LastTy));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002085 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
2086 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner52523562007-04-24 00:16:04 +00002087 Record.clear();
2088 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002089
Chris Lattner52523562007-04-24 00:16:04 +00002090 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesenfd04c742009-10-13 20:46:56 +00002091 Record.push_back(unsigned(IA->hasSideEffects()) |
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002092 unsigned(IA->isAlignStack()) << 1 |
2093 unsigned(IA->getDialect()&1) << 2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002094
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002095 // Add the asm string.
2096 const std::string &AsmStr = IA->getAsmString();
2097 Record.push_back(AsmStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002098 Record.append(AsmStr.begin(), AsmStr.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002099
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002100 // Add the constraint string.
2101 const std::string &ConstraintStr = IA->getConstraintString();
2102 Record.push_back(ConstraintStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002103 Record.append(ConstraintStr.begin(), ConstraintStr.end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002104 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002105 Record.clear();
Chris Lattner52523562007-04-24 00:16:04 +00002106 continue;
2107 }
2108 const Constant *C = cast<Constant>(V);
2109 unsigned Code = -1U;
2110 unsigned AbbrevToUse = 0;
2111 if (C->isNullValue()) {
2112 Code = bitc::CST_CODE_NULL;
2113 } else if (isa<UndefValue>(C)) {
2114 Code = bitc::CST_CODE_UNDEF;
2115 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
Bob Wilsone4077362013-09-09 19:14:35 +00002116 if (IV->getBitWidth() <= 64) {
2117 uint64_t V = IV->getSExtValue();
2118 emitSignedInt64(Record, V);
2119 Code = bitc::CST_CODE_INTEGER;
2120 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2121 } else { // Wide integers, > 64 bits in size.
2122 // We have an arbitrary precision integer value to write whose
2123 // bit width is > 64. However, in canonical unsigned integer
2124 // format it is likely that the high bits are going to be zero.
2125 // So, we only write the number of active words.
2126 unsigned NWords = IV->getValue().getActiveWords();
2127 const uint64_t *RawWords = IV->getValue().getRawData();
2128 for (unsigned i = 0; i != NWords; ++i) {
2129 emitSignedInt64(Record, RawWords[i]);
2130 }
2131 Code = bitc::CST_CODE_WIDE_INTEGER;
2132 }
Chris Lattner52523562007-04-24 00:16:04 +00002133 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2134 Code = bitc::CST_CODE_FLOAT;
Chris Lattner229907c2011-07-18 04:54:35 +00002135 Type *Ty = CFP->getType();
Dan Gohman518cda42011-12-17 00:04:22 +00002136 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002137 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +00002138 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002139 // api needed to prevent premature destruction
Dale Johannesen93eefa02009-03-23 21:16:53 +00002140 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen54306fe2008-10-09 18:53:47 +00002141 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002142 const uint64_t *p = api.getRawData();
Dale Johannesen93eefa02009-03-23 21:16:53 +00002143 Record.push_back((p[1] << 48) | (p[0] >> 16));
2144 Record.push_back(p[0] & 0xffffLL);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002145 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002146 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002147 const uint64_t *p = api.getRawData();
Dale Johannesen245dceb2007-09-11 18:32:33 +00002148 Record.push_back(p[0]);
2149 Record.push_back(p[1]);
Dale Johannesenbdad8092007-08-09 22:51:36 +00002150 } else {
2151 assert (0 && "Unknown FP type!");
Chris Lattner52523562007-04-24 00:16:04 +00002152 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00002153 } else if (isa<ConstantDataSequential>(C) &&
2154 cast<ConstantDataSequential>(C)->isString()) {
2155 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2156 // Emit constant strings specially.
2157 unsigned NumElts = Str->getNumElements();
2158 // If this is a null-terminated string, use the denser CSTRING encoding.
2159 if (Str->isCString()) {
2160 Code = bitc::CST_CODE_CSTRING;
2161 --NumElts; // Don't encode the null, which isn't allowed by char6.
2162 } else {
2163 Code = bitc::CST_CODE_STRING;
2164 AbbrevToUse = String8Abbrev;
2165 }
2166 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2167 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2168 for (unsigned i = 0; i != NumElts; ++i) {
2169 unsigned char V = Str->getElementAsInteger(i);
2170 Record.push_back(V);
2171 isCStr7 &= (V & 128) == 0;
2172 if (isCStrChar6)
2173 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2174 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002175
Chris Lattner372dd1e2012-01-30 00:51:16 +00002176 if (isCStrChar6)
2177 AbbrevToUse = CString6Abbrev;
2178 else if (isCStr7)
2179 AbbrevToUse = CString7Abbrev;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002180 } else if (const ConstantDataSequential *CDS =
Chris Lattner372dd1e2012-01-30 00:51:16 +00002181 dyn_cast<ConstantDataSequential>(C)) {
Chris Lattner5d917072012-01-30 07:36:01 +00002182 Code = bitc::CST_CODE_DATA;
Chris Lattner372dd1e2012-01-30 00:51:16 +00002183 Type *EltTy = CDS->getType()->getElementType();
2184 if (isa<IntegerType>(EltTy)) {
2185 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2186 Record.push_back(CDS->getElementAsInteger(i));
Chris Lattner372dd1e2012-01-30 00:51:16 +00002187 } else {
Justin Bognera43eacb2016-01-06 22:31:32 +00002188 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2189 Record.push_back(
2190 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
Chris Lattner372dd1e2012-01-30 00:51:16 +00002191 }
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00002192 } else if (isa<ConstantAggregate>(C)) {
Chris Lattner52523562007-04-24 00:16:04 +00002193 Code = bitc::CST_CODE_AGGREGATE;
Pete Cooper125ad172015-06-25 20:51:38 +00002194 for (const Value *Op : C->operands())
2195 Record.push_back(VE.getValueID(Op));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002196 AbbrevToUse = AggregateAbbrev;
Chris Lattner52523562007-04-24 00:16:04 +00002197 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002198 switch (CE->getOpcode()) {
2199 default:
2200 if (Instruction::isCast(CE->getOpcode())) {
2201 Code = bitc::CST_CODE_CE_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002202 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002203 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2204 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002205 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002206 } else {
2207 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2208 Code = bitc::CST_CODE_CE_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002209 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002210 Record.push_back(VE.getValueID(C->getOperand(0)));
2211 Record.push_back(VE.getValueID(C->getOperand(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002212 uint64_t Flags = getOptimizationFlags(CE);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002213 if (Flags != 0)
2214 Record.push_back(Flags);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002215 }
2216 break;
David Blaikieb9263572015-03-13 21:03:36 +00002217 case Instruction::GetElementPtr: {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002218 Code = bitc::CST_CODE_CE_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00002219 const auto *GO = cast<GEPOperator>(C);
2220 if (GO->isInBounds())
Dan Gohman1639c392009-07-27 21:53:46 +00002221 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00002222 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002223 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2224 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2225 Record.push_back(VE.getValueID(C->getOperand(i)));
2226 }
2227 break;
David Blaikieb9263572015-03-13 21:03:36 +00002228 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002229 case Instruction::Select:
2230 Code = bitc::CST_CODE_CE_SELECT;
2231 Record.push_back(VE.getValueID(C->getOperand(0)));
2232 Record.push_back(VE.getValueID(C->getOperand(1)));
2233 Record.push_back(VE.getValueID(C->getOperand(2)));
2234 break;
2235 case Instruction::ExtractElement:
2236 Code = bitc::CST_CODE_CE_EXTRACTELT;
2237 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2238 Record.push_back(VE.getValueID(C->getOperand(0)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002239 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002240 Record.push_back(VE.getValueID(C->getOperand(1)));
2241 break;
2242 case Instruction::InsertElement:
2243 Code = bitc::CST_CODE_CE_INSERTELT;
2244 Record.push_back(VE.getValueID(C->getOperand(0)));
2245 Record.push_back(VE.getValueID(C->getOperand(1)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002246 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002247 Record.push_back(VE.getValueID(C->getOperand(2)));
2248 break;
2249 case Instruction::ShuffleVector:
Nate Begeman94aa38d2009-02-12 21:28:33 +00002250 // If the return type and argument types are the same, this is a
2251 // standard shufflevector instruction. If the types are different,
2252 // then the shuffle is widening or truncating the input vectors, and
2253 // the argument type must also be encoded.
2254 if (C->getType() == C->getOperand(0)->getType()) {
2255 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2256 } else {
2257 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2258 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2259 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002260 Record.push_back(VE.getValueID(C->getOperand(0)));
2261 Record.push_back(VE.getValueID(C->getOperand(1)));
2262 Record.push_back(VE.getValueID(C->getOperand(2)));
2263 break;
2264 case Instruction::ICmp:
2265 case Instruction::FCmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002266 Code = bitc::CST_CODE_CE_CMP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002267 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2268 Record.push_back(VE.getValueID(C->getOperand(0)));
2269 Record.push_back(VE.getValueID(C->getOperand(1)));
2270 Record.push_back(CE->getPredicate());
2271 break;
2272 }
Chris Lattnerf540d742009-10-28 05:24:40 +00002273 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerf540d742009-10-28 05:24:40 +00002274 Code = bitc::CST_CODE_BLOCKADDRESS;
2275 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2276 Record.push_back(VE.getValueID(BA->getFunction()));
2277 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner52523562007-04-24 00:16:04 +00002278 } else {
Dan Gohman47dc8fd2010-07-21 21:18:37 +00002279#ifndef NDEBUG
2280 C->dump();
2281#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002282 llvm_unreachable("Unknown constant!");
Chris Lattner52523562007-04-24 00:16:04 +00002283 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002284 Stream.EmitRecord(Code, Record, AbbrevToUse);
Chris Lattner52523562007-04-24 00:16:04 +00002285 Record.clear();
2286 }
2287
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002288 Stream.ExitBlock();
Chris Lattner52523562007-04-24 00:16:04 +00002289}
2290
Teresa Johnson37687f32016-04-23 04:30:47 +00002291void ModuleBitcodeWriter::writeModuleConstants() {
Chris Lattner52523562007-04-24 00:16:04 +00002292 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002293
Chris Lattner52523562007-04-24 00:16:04 +00002294 // Find the first constant to emit, which is the first non-globalvalue value.
2295 // We know globalvalues have been emitted by WriteModuleInfo.
2296 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2297 if (!isa<GlobalValue>(Vals[i].first)) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002298 writeConstants(i, Vals.size(), true);
Chris Lattner52523562007-04-24 00:16:04 +00002299 return;
2300 }
2301 }
2302}
Chris Lattner215e9cd2007-04-23 20:35:01 +00002303
Teresa Johnson37687f32016-04-23 04:30:47 +00002304/// pushValueAndType - The file has to encode both the value and type id for
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002305/// many values, because we need to know what type to create for forward
2306/// references. However, most operands are not forward references, so this type
2307/// field is not needed.
2308///
2309/// This function adds V's value ID to Vals. If the value ID is higher than the
2310/// instruction ID, then it is a forward reference, and it also includes the
Jan Wen Voungafaced02012-10-11 20:20:40 +00002311/// type ID. The value ID that is written is encoded relative to the InstID.
Teresa Johnson37687f32016-04-23 04:30:47 +00002312bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2313 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002314 unsigned ValID = VE.getValueID(V);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002315 // Make encoding relative to the InstID.
2316 Vals.push_back(InstID - ValID);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002317 if (ValID >= InstID) {
2318 Vals.push_back(VE.getTypeID(V->getType()));
2319 return true;
2320 }
2321 return false;
2322}
2323
Teresa Johnson37687f32016-04-23 04:30:47 +00002324void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2325 unsigned InstID) {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002326 SmallVector<unsigned, 64> Record;
2327 LLVMContext &C = CS.getInstruction()->getContext();
2328
2329 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002330 const auto &Bundle = CS.getOperandBundleAt(i);
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002331 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002332
2333 for (auto &Input : Bundle.Inputs)
Teresa Johnson37687f32016-04-23 04:30:47 +00002334 pushValueAndType(Input, InstID, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002335
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002336 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002337 Record.clear();
2338 }
2339}
2340
Teresa Johnson37687f32016-04-23 04:30:47 +00002341/// pushValue - Like pushValueAndType, but where the type of the value is
Jan Wen Voungafaced02012-10-11 20:20:40 +00002342/// omitted (perhaps it was already encoded in an earlier operand).
Teresa Johnson37687f32016-04-23 04:30:47 +00002343void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2344 SmallVectorImpl<unsigned> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002345 unsigned ValID = VE.getValueID(V);
2346 Vals.push_back(InstID - ValID);
2347}
2348
Teresa Johnson37687f32016-04-23 04:30:47 +00002349void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2350 SmallVectorImpl<uint64_t> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002351 unsigned ValID = VE.getValueID(V);
2352 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2353 emitSignedInt64(Vals, diff);
2354}
2355
Chris Lattnere6e364c2007-04-26 05:53:54 +00002356/// WriteInstruction - Emit an instruction to the specified stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002357void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2358 unsigned InstID,
2359 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnere6e364c2007-04-26 05:53:54 +00002360 unsigned Code = 0;
2361 unsigned AbbrevToUse = 0;
Devang Patelaf206b82009-09-18 19:26:43 +00002362 VE.setInstructionID(&I);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002363 switch (I.getOpcode()) {
2364 default:
2365 if (Instruction::isCast(I.getOpcode())) {
Chris Lattner0a603252007-05-01 02:13:26 +00002366 Code = bitc::FUNC_CODE_INST_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002367 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002368 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002369 Vals.push_back(VE.getTypeID(I.getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002370 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
Chris Lattnere6e364c2007-04-26 05:53:54 +00002371 } else {
2372 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattner9f35f912007-05-02 04:26:36 +00002373 Code = bitc::FUNC_CODE_INST_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002374 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002375 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Teresa Johnson37687f32016-04-23 04:30:47 +00002376 pushValue(I.getOperand(1), InstID, Vals);
2377 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2378 uint64_t Flags = getOptimizationFlags(&I);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002379 if (Flags != 0) {
2380 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2381 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2382 Vals.push_back(Flags);
2383 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002384 }
2385 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002386
David Blaikieb5b5efd2015-02-25 01:08:52 +00002387 case Instruction::GetElementPtr: {
Chris Lattner0a603252007-05-01 02:13:26 +00002388 Code = bitc::FUNC_CODE_INST_GEP;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002389 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2390 auto &GEPInst = cast<GetElementPtrInst>(I);
2391 Vals.push_back(GEPInst.isInBounds());
2392 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002393 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002394 pushValueAndType(I.getOperand(i), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002395 break;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002396 }
Dan Gohmanca0256a2008-05-31 19:11:15 +00002397 case Instruction::ExtractValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002398 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002399 pushValueAndType(I.getOperand(0), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002400 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002401 Vals.append(EVI->idx_begin(), EVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002402 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002403 }
2404 case Instruction::InsertValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002405 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002406 pushValueAndType(I.getOperand(0), InstID, Vals);
2407 pushValueAndType(I.getOperand(1), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002408 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002409 Vals.append(IVI->idx_begin(), IVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002410 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002411 }
Chris Lattner0a603252007-05-01 02:13:26 +00002412 case Instruction::Select:
Dan Gohmanc5d28922008-09-16 01:01:33 +00002413 Code = bitc::FUNC_CODE_INST_VSELECT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002414 pushValueAndType(I.getOperand(1), InstID, Vals);
2415 pushValue(I.getOperand(2), InstID, Vals);
2416 pushValueAndType(I.getOperand(0), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002417 break;
2418 case Instruction::ExtractElement:
2419 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002420 pushValueAndType(I.getOperand(0), InstID, Vals);
2421 pushValueAndType(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002422 break;
2423 case Instruction::InsertElement:
2424 Code = bitc::FUNC_CODE_INST_INSERTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002425 pushValueAndType(I.getOperand(0), InstID, Vals);
2426 pushValue(I.getOperand(1), InstID, Vals);
2427 pushValueAndType(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002428 break;
2429 case Instruction::ShuffleVector:
2430 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002431 pushValueAndType(I.getOperand(0), InstID, Vals);
2432 pushValue(I.getOperand(1), InstID, Vals);
2433 pushValue(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002434 break;
2435 case Instruction::ICmp:
James Molloy88eb5352015-07-10 12:52:00 +00002436 case Instruction::FCmp: {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002437 // compare returning Int1Ty or vector of Int1Ty
2438 Code = bitc::FUNC_CODE_INST_CMP2;
Teresa Johnson37687f32016-04-23 04:30:47 +00002439 pushValueAndType(I.getOperand(0), InstID, Vals);
2440 pushValue(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002441 Vals.push_back(cast<CmpInst>(I).getPredicate());
Teresa Johnson37687f32016-04-23 04:30:47 +00002442 uint64_t Flags = getOptimizationFlags(&I);
James Molloy88eb5352015-07-10 12:52:00 +00002443 if (Flags != 0)
2444 Vals.push_back(Flags);
Chris Lattner0a603252007-05-01 02:13:26 +00002445 break;
James Molloy88eb5352015-07-10 12:52:00 +00002446 }
Chris Lattner0a603252007-05-01 02:13:26 +00002447
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002448 case Instruction::Ret:
Devang Patelbbfd8742008-02-26 01:29:32 +00002449 {
2450 Code = bitc::FUNC_CODE_INST_RET;
2451 unsigned NumOperands = I.getNumOperands();
Devang Patelbbfd8742008-02-26 01:29:32 +00002452 if (NumOperands == 0)
2453 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2454 else if (NumOperands == 1) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002455 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Devang Patelbbfd8742008-02-26 01:29:32 +00002456 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2457 } else {
2458 for (unsigned i = 0, e = NumOperands; i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002459 pushValueAndType(I.getOperand(i), InstID, Vals);
Devang Patelbbfd8742008-02-26 01:29:32 +00002460 }
2461 }
Chris Lattner0a603252007-05-01 02:13:26 +00002462 break;
2463 case Instruction::Br:
Gabor Greif1933b002009-01-30 18:27:21 +00002464 {
2465 Code = bitc::FUNC_CODE_INST_BR;
David Blaikieb78e9e52013-02-11 01:16:51 +00002466 const BranchInst &II = cast<BranchInst>(I);
Gabor Greif1933b002009-01-30 18:27:21 +00002467 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2468 if (II.isConditional()) {
2469 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002470 pushValue(II.getCondition(), InstID, Vals);
Gabor Greif1933b002009-01-30 18:27:21 +00002471 }
Chris Lattner0a603252007-05-01 02:13:26 +00002472 }
2473 break;
2474 case Instruction::Switch:
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002475 {
2476 Code = bitc::FUNC_CODE_INST_SWITCH;
David Blaikieb78e9e52013-02-11 01:16:51 +00002477 const SwitchInst &SI = cast<SwitchInst>(I);
Bob Wilsone4077362013-09-09 19:14:35 +00002478 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002479 pushValue(SI.getCondition(), InstID, Vals);
Bob Wilsone4077362013-09-09 19:14:35 +00002480 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
Sanjay Patel225d65f2015-11-13 16:21:23 +00002481 for (SwitchInst::ConstCaseIt Case : SI.cases()) {
2482 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2483 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002484 }
2485 }
Chris Lattner0a603252007-05-01 02:13:26 +00002486 break;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002487 case Instruction::IndirectBr:
2488 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattner3ed871f2009-10-27 19:13:16 +00002489 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Jan Wen Voungafaced02012-10-11 20:20:40 +00002490 // Encode the address operand as relative, but not the basic blocks.
Teresa Johnson37687f32016-04-23 04:30:47 +00002491 pushValue(I.getOperand(0), InstID, Vals);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002492 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
Chris Lattner3ed871f2009-10-27 19:13:16 +00002493 Vals.push_back(VE.getValueID(I.getOperand(i)));
2494 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002495
Chris Lattnerb811e952007-05-01 07:03:37 +00002496 case Instruction::Invoke: {
Gabor Greif4b79e472009-01-16 18:40:27 +00002497 const InvokeInst *II = cast<InvokeInst>(&I);
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002498 const Value *Callee = II->getCalledValue();
2499 FunctionType *FTy = II->getFunctionType();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002500
2501 if (II->hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002502 writeOperandBundles(II, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002503
Chris Lattner0a603252007-05-01 02:13:26 +00002504 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002505
Devang Patel4c758ea2008-09-25 21:00:45 +00002506 Vals.push_back(VE.getAttributeID(II->getAttributes()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002507 Vals.push_back(II->getCallingConv() | 1 << 13);
Gabor Greif6ecd6f42009-01-07 22:39:29 +00002508 Vals.push_back(VE.getValueID(II->getNormalDest()));
2509 Vals.push_back(VE.getValueID(II->getUnwindDest()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002510 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002511 pushValueAndType(Callee, InstID, Vals);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002512
Chris Lattner0a603252007-05-01 02:13:26 +00002513 // Emit value #'s for the fixed parameters.
Chris Lattner0a603252007-05-01 02:13:26 +00002514 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002515 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
Chris Lattner0a603252007-05-01 02:13:26 +00002516
2517 // Emit type/value pairs for varargs params.
2518 if (FTy->isVarArg()) {
Mehdi Amini7cf63822016-09-19 21:27:04 +00002519 for (unsigned i = FTy->getNumParams(), e = II->getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002520 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002521 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
Chris Lattner0a603252007-05-01 02:13:26 +00002522 }
2523 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002524 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002525 case Instruction::Resume:
2526 Code = bitc::FUNC_CODE_INST_RESUME;
Teresa Johnson37687f32016-04-23 04:30:47 +00002527 pushValueAndType(I.getOperand(0), InstID, Vals);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002528 break;
David Majnemer654e1302015-07-31 17:58:14 +00002529 case Instruction::CleanupRet: {
2530 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2531 const auto &CRI = cast<CleanupReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002532 pushValue(CRI.getCleanupPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002533 if (CRI.hasUnwindDest())
2534 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2535 break;
2536 }
2537 case Instruction::CatchRet: {
2538 Code = bitc::FUNC_CODE_INST_CATCHRET;
2539 const auto &CRI = cast<CatchReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002540 pushValue(CRI.getCatchPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002541 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2542 break;
2543 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00002544 case Instruction::CleanupPad:
David Majnemer654e1302015-07-31 17:58:14 +00002545 case Instruction::CatchPad: {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002546 const auto &FuncletPad = cast<FuncletPadInst>(I);
2547 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2548 : bitc::FUNC_CODE_INST_CLEANUPPAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002549 pushValue(FuncletPad.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002550
2551 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
David Majnemer654e1302015-07-31 17:58:14 +00002552 Vals.push_back(NumArgOperands);
2553 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
Teresa Johnson37687f32016-04-23 04:30:47 +00002554 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002555 break;
2556 }
2557 case Instruction::CatchSwitch: {
2558 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2559 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2560
Teresa Johnson37687f32016-04-23 04:30:47 +00002561 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002562
2563 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2564 Vals.push_back(NumHandlers);
2565 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2566 Vals.push_back(VE.getValueID(CatchPadBB));
2567
2568 if (CatchSwitch.hasUnwindDest())
2569 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
David Majnemer654e1302015-07-31 17:58:14 +00002570 break;
2571 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002572 case Instruction::Unreachable:
2573 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002574 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002575 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002576
Jay Foad372ad642011-06-20 14:18:48 +00002577 case Instruction::PHI: {
2578 const PHINode &PN = cast<PHINode>(I);
Chris Lattner0a603252007-05-01 02:13:26 +00002579 Code = bitc::FUNC_CODE_INST_PHI;
Jan Wen Voungafaced02012-10-11 20:20:40 +00002580 // With the newer instruction encoding, forward references could give
2581 // negative valued IDs. This is most common for PHIs, so we use
2582 // signed VBRs.
2583 SmallVector<uint64_t, 128> Vals64;
2584 Vals64.push_back(VE.getTypeID(PN.getType()));
Jay Foad372ad642011-06-20 14:18:48 +00002585 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002586 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002587 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
Jay Foad372ad642011-06-20 14:18:48 +00002588 }
Jan Wen Voungafaced02012-10-11 20:20:40 +00002589 // Emit a Vals64 vector and exit.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002590 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002591 Vals64.clear();
2592 return;
Jay Foad372ad642011-06-20 14:18:48 +00002593 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002594
Bill Wendlingfae14752011-08-12 20:24:12 +00002595 case Instruction::LandingPad: {
2596 const LandingPadInst &LP = cast<LandingPadInst>(I);
2597 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2598 Vals.push_back(VE.getTypeID(LP.getType()));
Bill Wendlingfae14752011-08-12 20:24:12 +00002599 Vals.push_back(LP.isCleanup());
2600 Vals.push_back(LP.getNumClauses());
2601 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2602 if (LP.isCatch(I))
2603 Vals.push_back(LandingPadInst::Catch);
2604 else
2605 Vals.push_back(LandingPadInst::Filter);
Teresa Johnson37687f32016-04-23 04:30:47 +00002606 pushValueAndType(LP.getClause(I), InstID, Vals);
Bill Wendlingfae14752011-08-12 20:24:12 +00002607 }
2608 break;
2609 }
2610
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002611 case Instruction::Alloca: {
Chris Lattner0a603252007-05-01 02:13:26 +00002612 Code = bitc::FUNC_CODE_INST_ALLOCA;
David Blaikiebdb49102015-04-28 16:51:01 +00002613 const AllocaInst &AI = cast<AllocaInst>(I);
2614 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
Dan Gohman9da5bb02010-05-28 01:38:28 +00002615 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002616 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002617 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2618 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2619 "not enough bits for maximum alignment");
2620 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2621 AlignRecord |= AI.isUsedWithInAlloca() << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00002622 AlignRecord |= 1 << 6;
Manman Ren9bfd0d02016-04-01 21:41:15 +00002623 AlignRecord |= AI.isSwiftError() << 7;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002624 Vals.push_back(AlignRecord);
Chris Lattner0a603252007-05-01 02:13:26 +00002625 break;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002626 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002627
Chris Lattner0a603252007-05-01 02:13:26 +00002628 case Instruction::Load:
Eli Friedman59b66882011-08-09 23:02:53 +00002629 if (cast<LoadInst>(I).isAtomic()) {
2630 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002631 pushValueAndType(I.getOperand(0), InstID, Vals);
Eli Friedman59b66882011-08-09 23:02:53 +00002632 } else {
2633 Code = bitc::FUNC_CODE_INST_LOAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002634 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
Eli Friedman59b66882011-08-09 23:02:53 +00002635 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2636 }
David Blaikie85035652015-02-25 01:07:20 +00002637 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002638 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2639 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002640 if (cast<LoadInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002641 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2642 Vals.push_back(getEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002643 }
Chris Lattner0a603252007-05-01 02:13:26 +00002644 break;
2645 case Instruction::Store:
Eli Friedman59b66882011-08-09 23:02:53 +00002646 if (cast<StoreInst>(I).isAtomic())
2647 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2648 else
2649 Code = bitc::FUNC_CODE_INST_STORE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002650 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2651 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
Chris Lattner0a603252007-05-01 02:13:26 +00002652 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2653 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002654 if (cast<StoreInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002655 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2656 Vals.push_back(getEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002657 }
Chris Lattner0a603252007-05-01 02:13:26 +00002658 break;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002659 case Instruction::AtomicCmpXchg:
2660 Code = bitc::FUNC_CODE_INST_CMPXCHG;
Teresa Johnson37687f32016-04-23 04:30:47 +00002661 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2662 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2663 pushValue(I.getOperand(2), InstID, Vals); // newval.
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002664 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002665 Vals.push_back(
2666 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2667 Vals.push_back(
2668 getEncodedSynchScope(cast<AtomicCmpXchgInst>(I).getSynchScope()));
2669 Vals.push_back(
2670 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
Tim Northover420a2162014-06-13 14:24:07 +00002671 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002672 break;
2673 case Instruction::AtomicRMW:
2674 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
Teresa Johnson37687f32016-04-23 04:30:47 +00002675 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2676 pushValue(I.getOperand(1), InstID, Vals); // val.
2677 Vals.push_back(
2678 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002679 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002680 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2681 Vals.push_back(
2682 getEncodedSynchScope(cast<AtomicRMWInst>(I).getSynchScope()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002683 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002684 case Instruction::Fence:
2685 Code = bitc::FUNC_CODE_INST_FENCE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002686 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2687 Vals.push_back(getEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
Eli Friedmanfee02c62011-07-25 23:16:38 +00002688 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002689 case Instruction::Call: {
Gabor Greife9afee22010-06-26 09:35:09 +00002690 const CallInst &CI = cast<CallInst>(I);
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002691 FunctionType *FTy = CI.getFunctionType();
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002692
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002693 if (CI.hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002694 writeOperandBundles(&CI, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002695
Chris Lattnerc44070802011-06-17 18:17:37 +00002696 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002697
Gabor Greife9afee22010-06-26 09:35:09 +00002698 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002699
Teresa Johnson37687f32016-04-23 04:30:47 +00002700 unsigned Flags = getOptimizationFlags(&I);
Akira Hatanaka97cb3972015-11-07 02:48:49 +00002701 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2702 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2703 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2704 1 << bitc::CALL_EXPLICIT_TYPE |
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002705 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2706 unsigned(Flags != 0) << bitc::CALL_FMF);
2707 if (Flags != 0)
2708 Vals.push_back(Flags);
2709
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002710 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002711 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002712
Chris Lattner0a603252007-05-01 02:13:26 +00002713 // Emit value #'s for the fixed parameters.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002714 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2715 // Check for labels (can happen with asm labels).
2716 if (FTy->getParamType(i)->isLabelTy())
2717 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2718 else
Teresa Johnson37687f32016-04-23 04:30:47 +00002719 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002720 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002721
Chris Lattnerb811e952007-05-01 07:03:37 +00002722 // Emit type/value pairs for varargs params.
2723 if (FTy->isVarArg()) {
Gabor Greife9afee22010-06-26 09:35:09 +00002724 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002725 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002726 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
Chris Lattner0a603252007-05-01 02:13:26 +00002727 }
2728 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002729 }
Chris Lattner0a603252007-05-01 02:13:26 +00002730 case Instruction::VAArg:
2731 Code = bitc::FUNC_CODE_INST_VAARG;
2732 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
Teresa Johnson37687f32016-04-23 04:30:47 +00002733 pushValue(I.getOperand(0), InstID, Vals); // valist.
Chris Lattner0a603252007-05-01 02:13:26 +00002734 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2735 break;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002736 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002737
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002738 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002739 Vals.clear();
2740}
2741
Teresa Johnson37687f32016-04-23 04:30:47 +00002742/// Emit names for globals/functions etc. \p IsModuleLevel is true when
2743/// we are writing the module-level VST, where we are including a function
2744/// bitcode index and need to backpatch the VST forward declaration record.
2745void ModuleBitcodeWriter::writeValueSymbolTable(
2746 const ValueSymbolTable &VST, bool IsModuleLevel,
2747 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002748 if (VST.empty()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002749 // writeValueSymbolTableForwardDecl should have returned early as
Teresa Johnsonff642b92015-09-17 20:12:00 +00002750 // well. Ensure this handling remains in sync by asserting that
2751 // the placeholder offset is not set.
Teresa Johnson37687f32016-04-23 04:30:47 +00002752 assert(!IsModuleLevel || !hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002753 return;
2754 }
2755
Teresa Johnson37687f32016-04-23 04:30:47 +00002756 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002757 // Get the offset of the VST we are writing, and backpatch it into
2758 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002759 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002760 // The BitcodeStartBit was the stream offset of the actual bitcode
2761 // (e.g. excluding any initial darwin header).
Teresa Johnson37687f32016-04-23 04:30:47 +00002762 VSTOffset -= bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002763 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002764 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002765 }
2766
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002767 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2eae59f2007-05-04 20:34:50 +00002768
Teresa Johnsonff642b92015-09-17 20:12:00 +00002769 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2770 // records, which are not used in the per-function VSTs.
2771 unsigned FnEntry8BitAbbrev;
2772 unsigned FnEntry7BitAbbrev;
2773 unsigned FnEntry6BitAbbrev;
Teresa Johnsoncd21a642016-07-17 14:47:01 +00002774 unsigned GUIDEntryAbbrev;
Teresa Johnson37687f32016-04-23 04:30:47 +00002775 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002776 // 8-bit fixed-width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002777 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2778 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2780 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002781 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2782 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002783 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002784
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002785 // 7-bit fixed width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002786 Abbv = new BitCodeAbbrev();
2787 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002790 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2791 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002792 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002793
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002794 // 6-bit char6 VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002795 Abbv = new BitCodeAbbrev();
2796 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002799 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002801 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsoncd21a642016-07-17 14:47:01 +00002802
2803 // FIXME: Change the name of this record as it is now used by
2804 // the per-module index as well.
2805 Abbv = new BitCodeAbbrev();
2806 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2807 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2808 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
2809 GUIDEntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002810 }
2811
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002812 // FIXME: Set up the abbrev, we know how many values there are!
2813 // FIXME: We know if the type names can use 7-bit ascii.
Teresa Johnsoncd21a642016-07-17 14:47:01 +00002814 SmallVector<uint64_t, 64> NameVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002815
Yaron Keren001e2e42015-08-10 07:04:29 +00002816 for (const ValueName &Name : VST) {
Chris Lattner4d925982007-05-04 20:52:02 +00002817 // Figure out the encoding to use for the name.
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002818 StringEncoding Bits =
2819 getStringEncoding(Name.getKeyData(), Name.getKeyLength());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002820
Chris Lattnera0987962007-05-04 21:31:13 +00002821 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002822 NameVals.push_back(VE.getValueID(Name.getValue()));
2823
2824 Function *F = dyn_cast<Function>(Name.getValue());
2825 if (!F) {
2826 // If value is an alias, need to get the aliased base object to
2827 // see if it is a function.
2828 auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2829 if (GA && GA->getBaseObject())
2830 F = dyn_cast<Function>(GA->getBaseObject());
2831 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002832
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002833 // VST_CODE_ENTRY: [valueid, namechar x N]
2834 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
2835 // VST_CODE_BBENTRY: [bbid, namechar x N]
Chris Lattner6be58c62007-05-03 22:18:21 +00002836 unsigned Code;
Yaron Keren001e2e42015-08-10 07:04:29 +00002837 if (isa<BasicBlock>(Name.getValue())) {
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002838 Code = bitc::VST_CODE_BBENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002839 if (Bits == SE_Char6)
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002840 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002841 } else if (F && !F->isDeclaration()) {
2842 // Must be the module-level VST, where we pass in the Index and
2843 // have a VSTOffsetPlaceholder. The function-level VST should not
2844 // contain any Function symbols.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00002845 assert(FunctionToBitcodeIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00002846 assert(hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002847
2848 // Save the word offset of the function (from the start of the
2849 // actual bitcode written to the stream).
Teresa Johnson37687f32016-04-23 04:30:47 +00002850 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002851 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2852 NameVals.push_back(BitcodeIndex / 32);
2853
2854 Code = bitc::VST_CODE_FNENTRY;
2855 AbbrevToUse = FnEntry8BitAbbrev;
2856 if (Bits == SE_Char6)
2857 AbbrevToUse = FnEntry6BitAbbrev;
2858 else if (Bits == SE_Fixed7)
2859 AbbrevToUse = FnEntry7BitAbbrev;
Chris Lattner6be58c62007-05-03 22:18:21 +00002860 } else {
2861 Code = bitc::VST_CODE_ENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002862 if (Bits == SE_Char6)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002863 AbbrevToUse = VST_ENTRY_6_ABBREV;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002864 else if (Bits == SE_Fixed7)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002865 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002866 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002867
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002868 for (const auto P : Name.getKey())
2869 NameVals.push_back((unsigned char)P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002870
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002871 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002872 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002873 NameVals.clear();
2874 }
Teresa Johnsoncd21a642016-07-17 14:47:01 +00002875 // Emit any GUID valueIDs created for indirect call edges into the
2876 // module-level VST.
2877 if (IsModuleLevel && hasVSTOffsetPlaceholder())
2878 for (const auto &GI : valueIds()) {
2879 NameVals.push_back(GI.second);
2880 NameVals.push_back(GI.first);
2881 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals,
2882 GUIDEntryAbbrev);
2883 NameVals.clear();
2884 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002885 Stream.ExitBlock();
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002886}
2887
Teresa Johnson403a7872015-10-04 14:33:43 +00002888/// Emit function names and summary offsets for the combined index
2889/// used by ThinLTO.
Teresa Johnson37687f32016-04-23 04:30:47 +00002890void IndexBitcodeWriter::writeCombinedValueSymbolTable() {
2891 assert(hasVSTOffsetPlaceholder() && "Expected non-zero VSTOffsetPlaceholder");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002892 // Get the offset of the VST we are writing, and backpatch it into
2893 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002894 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002895 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002896 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002897
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002898 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00002899
Teresa Johnson403a7872015-10-04 14:33:43 +00002900 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002901 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2902 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2903 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002904 unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002905
Teresa Johnsone1164de2016-02-10 21:55:02 +00002906 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson37687f32016-04-23 04:30:47 +00002907 for (const auto &GVI : valueIds()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002908 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
2909 NameVals.push_back(GVI.second);
2910 NameVals.push_back(GVI.first);
2911
2912 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002913 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002914 NameVals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00002915 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002916 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002917}
2918
Teresa Johnson37687f32016-04-23 04:30:47 +00002919void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002920 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2921 unsigned Code;
2922 if (isa<BasicBlock>(Order.V))
2923 Code = bitc::USELIST_CODE_BB;
2924 else
2925 Code = bitc::USELIST_CODE_DEFAULT;
2926
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002927 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002928 Record.push_back(VE.getValueID(Order.V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002929 Stream.EmitRecord(Code, Record);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002930}
2931
Teresa Johnson37687f32016-04-23 04:30:47 +00002932void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002933 assert(VE.shouldPreserveUseListOrder() &&
2934 "Expected to be preserving use-list order");
2935
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002936 auto hasMore = [&]() {
2937 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2938 };
2939 if (!hasMore())
2940 // Nothing to do.
2941 return;
2942
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002943 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002944 while (hasMore()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002945 writeUseList(std::move(VE.UseListOrders.back()));
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002946 VE.UseListOrders.pop_back();
2947 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002948 Stream.ExitBlock();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002949}
2950
Teresa Johnson403a7872015-10-04 14:33:43 +00002951/// Emit a function body to the module stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002952void ModuleBitcodeWriter::writeFunction(
2953 const Function &F,
2954 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002955 // Save the bitcode index of the start of this function block for recording
2956 // in the VST.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002957 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002958
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002959 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a11b642011-06-03 17:02:19 +00002960 VE.incorporateFunction(F);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002961
2962 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002963
Chris Lattnere6e364c2007-04-26 05:53:54 +00002964 // Emit the number of basic blocks, so the reader can create them ahead of
2965 // time.
2966 Vals.push_back(VE.getBasicBlocks().size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002967 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002968 Vals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002969
Chris Lattnere6e364c2007-04-26 05:53:54 +00002970 // If there are function-local constants, emit them now.
2971 unsigned CstStart, CstEnd;
2972 VE.getFunctionConstantRange(CstStart, CstEnd);
Teresa Johnson37687f32016-04-23 04:30:47 +00002973 writeConstants(CstStart, CstEnd, false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002974
Victor Hernandez6c730de2010-01-14 01:50:08 +00002975 // If there is function-local metadata, emit it now.
Teresa Johnson37687f32016-04-23 04:30:47 +00002976 writeFunctionMetadata(F);
Victor Hernandez6c730de2010-01-14 01:50:08 +00002977
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002978 // Keep a running idea of what the instruction ID is.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002979 unsigned InstID = CstEnd;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002980
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002981 bool NeedsMetadataAttachment = F.hasMetadata();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002982
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002983 DILocation *LastDL = nullptr;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002984 // Finally, emit all the instructions, in order.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002985 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002986 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2987 I != E; ++I) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002988 writeInstruction(*I, InstID, Vals);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002989
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00002990 if (!I->getType()->isVoidTy())
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002991 ++InstID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002992
Chris Lattner07d09ed2010-04-03 02:17:50 +00002993 // If the instruction has metadata, write a metadata attachment later.
2994 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002995
Chris Lattner07d09ed2010-04-03 02:17:50 +00002996 // If the instruction has a debug location, emit it.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002997 DILocation *DL = I->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002998 if (!DL)
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002999 continue;
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00003000
3001 if (DL == LastDL) {
Chris Lattner07d09ed2010-04-03 02:17:50 +00003002 // Just repeat the same debug loc as last time.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003003 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00003004 continue;
Chris Lattner07d09ed2010-04-03 02:17:50 +00003005 }
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00003006
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00003007 Vals.push_back(DL->getLine());
3008 Vals.push_back(DL->getColumn());
3009 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
3010 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003011 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00003012 Vals.clear();
Duncan P. N. Exon Smith538ef562015-05-06 22:51:12 +00003013
3014 LastDL = DL;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003015 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003016
Chris Lattnerfb6f9402007-05-01 02:14:57 +00003017 // Emit names for all the instructions etc.
Mehdi Aminia53d49e2016-09-17 06:00:02 +00003018 if (auto *Symtab = F.getValueSymbolTable())
3019 writeValueSymbolTable(*Symtab);
Devang Patelaf206b82009-09-18 19:26:43 +00003020
Chris Lattner07d09ed2010-04-03 02:17:50 +00003021 if (NeedsMetadataAttachment)
Peter Collingbournecceae7f2016-05-31 23:01:54 +00003022 writeFunctionMetadataAttachment(F);
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003023 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003024 writeUseListBlock(&F);
Chad Rosier6a11b642011-06-03 17:02:19 +00003025 VE.purgeFunction();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003026 Stream.ExitBlock();
Chris Lattner831d4202007-04-26 03:27:58 +00003027}
3028
Chris Lattner702658c2007-05-04 18:26:27 +00003029// Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003030void ModuleBitcodeWriter::writeBlockInfo() {
Chris Lattner702658c2007-05-04 18:26:27 +00003031 // We only want to emit block info records for blocks that have multiple
Jan Wen Voungafaced02012-10-11 20:20:40 +00003032 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
Jan Wen Voung8c9e9412012-10-11 21:45:16 +00003033 // Other blocks can define their abbrevs inline.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003034 Stream.EnterBlockInfoBlock(2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003035
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00003036 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00003037 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3038 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
3039 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3040 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3041 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003042 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003043 VST_ENTRY_8_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003044 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00003045 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003046
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00003047 { // 7-bit fixed width VST_CODE_ENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00003048 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3049 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
3050 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3051 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3052 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003053 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003054 VST_ENTRY_7_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003055 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00003056 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00003057 { // 6-bit char6 VST_CODE_ENTRY strings.
Chris Lattnere760d6f2007-05-05 01:26:50 +00003058 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3059 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
3060 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3061 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3062 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003063 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003064 VST_ENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003065 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere760d6f2007-05-05 01:26:50 +00003066 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00003067 { // 6-bit char6 VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00003068 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3069 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
3070 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3071 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnere760d6f2007-05-05 01:26:50 +00003072 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003073 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003074 VST_BBENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003075 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00003076 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00003077
3078
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003079
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003080 { // SETTYPE abbrev for CONSTANTS_BLOCK.
3081 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3082 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
3083 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
David Blaikie7b028102015-02-25 00:51:52 +00003084 VE.computeBitsRequiredForTypeIndicies()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003085 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003086 CONSTANTS_SETTYPE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003087 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003088 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003089
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003090 { // INTEGER abbrev for CONSTANTS_BLOCK.
3091 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3092 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
3093 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003094 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003095 CONSTANTS_INTEGER_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003096 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003097 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003098
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003099 { // CE_CAST abbrev for CONSTANTS_BLOCK.
3100 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3101 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
3102 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
3103 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
David Blaikie7b028102015-02-25 00:51:52 +00003104 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003105 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3106
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003107 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003108 CONSTANTS_CE_CAST_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003109 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003110 }
3111 { // NULL abbrev for CONSTANTS_BLOCK.
3112 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3113 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003114 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003115 CONSTANTS_NULL_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003116 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003117 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003118
Chris Lattnerb80751d2007-05-05 07:44:49 +00003119 // FIXME: This should only use space for first class types!
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003120
Chris Lattnerb80751d2007-05-05 07:44:49 +00003121 { // INST_LOAD abbrev for FUNCTION_BLOCK.
3122 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3123 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattnerb80751d2007-05-05 07:44:49 +00003124 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
David Blaikie85035652015-02-25 01:07:20 +00003125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3126 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerb80751d2007-05-05 07:44:49 +00003127 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3128 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003129 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003130 FUNCTION_INST_LOAD_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003131 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerb80751d2007-05-05 07:44:49 +00003132 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003133 { // INST_BINOP abbrev for FUNCTION_BLOCK.
3134 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3135 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3137 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3138 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003139 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003140 FUNCTION_INST_BINOP_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003141 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003142 }
Dan Gohman0ebd6962009-07-20 21:19:07 +00003143 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3144 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3145 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3146 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3147 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3148 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3149 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003150 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003151 FUNCTION_INST_BINOP_FLAGS_ABBREV)
Dan Gohman0ebd6962009-07-20 21:19:07 +00003152 llvm_unreachable("Unexpected abbrev ordering!");
3153 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003154 { // INST_CAST abbrev for FUNCTION_BLOCK.
3155 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3156 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
3157 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
David Blaikie7b028102015-02-25 00:51:52 +00003159 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003161 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003162 FUNCTION_INST_CAST_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003163 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003164 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003165
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003166 { // INST_RET abbrev for FUNCTION_BLOCK.
3167 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3168 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003169 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003170 FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003171 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003172 }
3173 { // INST_RET abbrev for FUNCTION_BLOCK.
3174 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3175 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
3176 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003177 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003178 FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003179 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003180 }
3181 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3182 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3183 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003184 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003185 FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003186 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003187 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00003188 {
3189 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3190 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
3191 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
3192 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3193 Log2_32_Ceil(VE.getTypes().size() + 1)));
3194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3195 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003196 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
David Blaikieb5b5efd2015-02-25 01:08:52 +00003197 FUNCTION_INST_GEP_ABBREV)
3198 llvm_unreachable("Unexpected abbrev ordering!");
3199 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003200
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003201 Stream.ExitBlock();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003202}
3203
Teresa Johnson403a7872015-10-04 14:33:43 +00003204/// Write the module path strings, currently only used when generating
3205/// a combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003206void IndexBitcodeWriter::writeModStrings() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003207 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00003208
3209 // TODO: See which abbrev sizes we actually need to emit
3210
3211 // 8-bit fixed-width MST_ENTRY strings.
3212 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3213 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3214 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3215 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3216 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003217 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003218
3219 // 7-bit fixed width MST_ENTRY strings.
3220 Abbv = new BitCodeAbbrev();
3221 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3222 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3223 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3224 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003225 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003226
3227 // 6-bit char6 MST_ENTRY strings.
3228 Abbv = new BitCodeAbbrev();
3229 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3230 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3231 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3232 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003233 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003234
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003235 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3236 Abbv = new BitCodeAbbrev();
3237 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
3238 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3239 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3240 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3241 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003243 unsigned AbbrevHash = Stream.EmitAbbrev(Abbv);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003244
3245 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003246 for (const auto &MPSE : Index.modulePaths()) {
Teresa Johnson84174c32016-05-10 13:48:23 +00003247 if (!doIncludeModule(MPSE.getKey()))
3248 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00003249 StringEncoding Bits =
3250 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
3251 unsigned AbbrevToUse = Abbrev8Bit;
3252 if (Bits == SE_Char6)
3253 AbbrevToUse = Abbrev6Bit;
3254 else if (Bits == SE_Fixed7)
3255 AbbrevToUse = Abbrev7Bit;
3256
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003257 Vals.push_back(MPSE.getValue().first);
Teresa Johnson403a7872015-10-04 14:33:43 +00003258
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003259 for (const auto P : MPSE.getKey())
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003260 Vals.push_back((unsigned char)P);
Teresa Johnson403a7872015-10-04 14:33:43 +00003261
3262 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003263 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003264
3265 Vals.clear();
3266 // Emit an optional hash for the module now
3267 auto &Hash = MPSE.getValue().second;
3268 bool AllZero = true; // Detect if the hash is empty, and do not generate it
3269 for (auto Val : Hash) {
3270 if (Val)
3271 AllZero = false;
3272 Vals.push_back(Val);
3273 }
3274 if (!AllZero) {
3275 // Emit the hash record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003276 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003277 }
3278
3279 Vals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00003280 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003281 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003282}
3283
3284// Helper to emit a single function summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003285void ModuleBitcodeWriter::writePerModuleFunctionSummaryRecord(
Teresa Johnson28e457b2016-04-24 14:57:11 +00003286 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +00003287 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3288 const Function &F) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003289 NameVals.push_back(ValueID);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003290
Teresa Johnson28e457b2016-04-24 14:57:11 +00003291 FunctionSummary *FS = cast<FunctionSummary>(Summary);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003292 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003293 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003294 NameVals.push_back(FS->refs().size());
3295
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003296 unsigned SizeBeforeRefs = NameVals.size();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003297 for (auto &RI : FS->refs())
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003298 NameVals.push_back(VE.getValueID(RI.getValue()));
3299 // Sort the refs for determinism output, the vector returned by FS->refs() has
3300 // been initialized from a DenseSet.
3301 std::sort(NameVals.begin() + SizeBeforeRefs, NameVals.end());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003302
Mehdi Amini367577a2016-05-16 09:04:55 +00003303 std::vector<FunctionSummary::EdgeTy> Calls = FS->calls();
3304 std::sort(Calls.begin(), Calls.end(),
3305 [this](const FunctionSummary::EdgeTy &L,
3306 const FunctionSummary::EdgeTy &R) {
Teresa Johnsoncd21a642016-07-17 14:47:01 +00003307 return getValueId(L.first) < getValueId(R.first);
Mehdi Amini367577a2016-05-16 09:04:55 +00003308 });
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003309 bool HasProfileData = F.getEntryCount().hasValue();
Mehdi Amini367577a2016-05-16 09:04:55 +00003310 for (auto &ECI : Calls) {
Teresa Johnsoncd21a642016-07-17 14:47:01 +00003311 NameVals.push_back(getValueId(ECI.first));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003312 if (HasProfileData)
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003313 NameVals.push_back(static_cast<uint8_t>(ECI.second.Hotness));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003314 }
3315
3316 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3317 unsigned Code =
3318 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
Teresa Johnson403a7872015-10-04 14:33:43 +00003319
3320 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003321 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003322 NameVals.clear();
3323}
3324
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003325// Collect the global value references in the given variable's initializer,
3326// and emit them in a summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003327void ModuleBitcodeWriter::writeModuleLevelReferences(
3328 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3329 unsigned FSModRefsAbbrev) {
Teresa Johnson13968092016-03-15 19:35:45 +00003330 // Only interested in recording variable defs in the summary.
3331 if (V.isDeclaration())
3332 return;
Teresa Johnson13968092016-03-15 19:35:45 +00003333 NameVals.push_back(VE.getValueID(&V));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003334 NameVals.push_back(getEncodedGVSummaryFlags(V));
Teresa Johnson28e457b2016-04-24 14:57:11 +00003335 auto *Summary = Index->getGlobalValueSummary(V);
3336 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003337
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003338 unsigned SizeBeforeRefs = NameVals.size();
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003339 for (auto &RI : VS->refs())
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003340 NameVals.push_back(VE.getValueID(RI.getValue()));
3341 // Sort the refs for determinism output, the vector returned by FS->refs() has
3342 // been initialized from a DenseSet.
3343 std::sort(NameVals.begin() + SizeBeforeRefs, NameVals.end());
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003344
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003345 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3346 FSModRefsAbbrev);
Teresa Johnson13968092016-03-15 19:35:45 +00003347 NameVals.clear();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003348}
Teresa Johnson403a7872015-10-04 14:33:43 +00003349
Mehdi Amini8fe69362016-04-24 03:18:11 +00003350// Current version for the summary.
3351// This is bumped whenever we introduce changes in the way some record are
3352// interpreted, like flags for instance.
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003353static const uint64_t INDEX_VERSION = 2;
Mehdi Amini8fe69362016-04-24 03:18:11 +00003354
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003355/// Emit the per-module summary section alongside the rest of
3356/// the module's bitcode.
Teresa Johnson37687f32016-04-23 04:30:47 +00003357void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() {
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003358 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 4);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003359
Mehdi Amini8fe69362016-04-24 03:18:11 +00003360 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3361
Teresa Johnson620c1402016-09-20 23:07:17 +00003362 if (Index->begin() == Index->end()) {
3363 Stream.ExitBlock();
3364 return;
3365 }
3366
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003367 // Abbrev for FS_PERMODULE.
Teresa Johnson403a7872015-10-04 14:33:43 +00003368 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003369 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003373 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003374 // numrefs x valueid, n x (valueid)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003375 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3376 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003377 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003378
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003379 // Abbrev for FS_PERMODULE_PROFILE.
3380 Abbv = new BitCodeAbbrev();
3381 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3382 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003383 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003384 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3385 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003386 // numrefs x valueid, n x (valueid, hotness)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003387 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3388 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003389 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003390
3391 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3392 Abbv = new BitCodeAbbrev();
3393 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3394 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003395 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003396 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3397 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003398 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003399
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003400 // Abbrev for FS_ALIAS.
3401 Abbv = new BitCodeAbbrev();
3402 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3403 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003404 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003405 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003406 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003407
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003408 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003409 // Iterate over the list of functions instead of the Index to
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003410 // ensure the ordering is stable.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003411 for (const Function &F : M) {
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003412 if (F.isDeclaration())
3413 continue;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003414 // Summary emission does not support anonymous functions, they have to
3415 // renamed using the anonymous function renaming pass.
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003416 if (!F.hasName())
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003417 report_fatal_error("Unexpected anonymous function when writing summary");
Teresa Johnson403a7872015-10-04 14:33:43 +00003418
Teresa Johnson28e457b2016-04-24 14:57:11 +00003419 auto *Summary = Index->getGlobalValueSummary(F);
Peter Collingbourne832e7fa2016-05-06 02:41:23 +00003420 writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
3421 FSCallsAbbrev, FSCallsProfileAbbrev, F);
Teresa Johnson403a7872015-10-04 14:33:43 +00003422 }
3423
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003424 // Capture references from GlobalVariable initializers, which are outside
3425 // of a function scope.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003426 for (const GlobalVariable &G : M.globals())
Teresa Johnson37687f32016-04-23 04:30:47 +00003427 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003428
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003429 for (const GlobalAlias &A : M.aliases()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003430 auto *Aliasee = A.getBaseObject();
3431 if (!Aliasee->hasName())
3432 // Nameless function don't have an entry in the summary, skip it.
3433 continue;
3434 auto AliasId = VE.getValueID(&A);
3435 auto AliaseeId = VE.getValueID(Aliasee);
3436 NameVals.push_back(AliasId);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003437 NameVals.push_back(getEncodedGVSummaryFlags(A));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003438 NameVals.push_back(AliaseeId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003439 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003440 NameVals.clear();
3441 }
3442
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003443 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003444}
3445
Teresa Johnson26ab5772016-03-15 00:04:37 +00003446/// Emit the combined summary section into the combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003447void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003448 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Mehdi Amini8fe69362016-04-24 03:18:11 +00003449 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
Teresa Johnson403a7872015-10-04 14:33:43 +00003450
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003451 // Abbrev for FS_COMBINED.
Teresa Johnson403a7872015-10-04 14:33:43 +00003452 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003453 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
Teresa Johnson02e98332016-04-27 13:28:35 +00003454 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003455 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003456 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003457 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3458 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003459 // numrefs x valueid, n x (valueid)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003460 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3461 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003462 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003463
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003464 // Abbrev for FS_COMBINED_PROFILE.
3465 Abbv = new BitCodeAbbrev();
3466 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
Teresa Johnson02e98332016-04-27 13:28:35 +00003467 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003468 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003469 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003470 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3471 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003472 // numrefs x valueid, n x (valueid, hotness)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003473 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3474 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003475 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003476
3477 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
3478 Abbv = new BitCodeAbbrev();
3479 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003480 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003481 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003482 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003483 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3484 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003485 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003486
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003487 // Abbrev for FS_COMBINED_ALIAS.
3488 Abbv = new BitCodeAbbrev();
3489 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003490 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003491 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003492 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson02e98332016-04-27 13:28:35 +00003493 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003494 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003495
Teresa Johnson02e98332016-04-27 13:28:35 +00003496 // The aliases are emitted as a post-pass, and will point to the value
3497 // id of the aliasee. Save them in a vector for post-processing.
Teresa Johnson28e457b2016-04-24 14:57:11 +00003498 SmallVector<AliasSummary *, 64> Aliases;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003499
Teresa Johnson02e98332016-04-27 13:28:35 +00003500 // Save the value id for each summary for alias emission.
3501 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
3502
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003503 SmallVector<uint64_t, 64> NameVals;
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003504
3505 // For local linkage, we also emit the original name separately
3506 // immediately after the record.
3507 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3508 if (!GlobalValue::isLocalLinkage(S.linkage()))
3509 return;
3510 NameVals.push_back(S.getOriginalName());
3511 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3512 NameVals.clear();
3513 };
3514
Teresa Johnson84174c32016-05-10 13:48:23 +00003515 for (const auto &I : *this) {
3516 GlobalValueSummary *S = I.second;
3517 assert(S);
Teresa Johnson02e98332016-04-27 13:28:35 +00003518
Teresa Johnson84174c32016-05-10 13:48:23 +00003519 assert(hasValueId(I.first));
3520 unsigned ValueId = getValueId(I.first);
3521 SummaryToValueIdMap[S] = ValueId;
Teresa Johnson02e98332016-04-27 13:28:35 +00003522
Teresa Johnson84174c32016-05-10 13:48:23 +00003523 if (auto *AS = dyn_cast<AliasSummary>(S)) {
3524 // Will process aliases as a post-pass because the reader wants all
3525 // global to be loaded first.
3526 Aliases.push_back(AS);
3527 continue;
3528 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003529
Teresa Johnson84174c32016-05-10 13:48:23 +00003530 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003531 NameVals.push_back(ValueId);
Teresa Johnson84174c32016-05-10 13:48:23 +00003532 NameVals.push_back(Index.getModuleId(VS->modulePath()));
3533 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
3534 for (auto &RI : VS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003535 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003536 }
3537
Teresa Johnson403a7872015-10-04 14:33:43 +00003538 // Emit the finished record.
Teresa Johnson84174c32016-05-10 13:48:23 +00003539 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3540 FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003541 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003542 MaybeEmitOriginalName(*S);
Teresa Johnson84174c32016-05-10 13:48:23 +00003543 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00003544 }
Teresa Johnson84174c32016-05-10 13:48:23 +00003545
3546 auto *FS = cast<FunctionSummary>(S);
3547 NameVals.push_back(ValueId);
3548 NameVals.push_back(Index.getModuleId(FS->modulePath()));
3549 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
3550 NameVals.push_back(FS->instCount());
3551 NameVals.push_back(FS->refs().size());
3552
3553 for (auto &RI : FS->refs()) {
3554 NameVals.push_back(getValueId(RI.getGUID()));
3555 }
3556
3557 bool HasProfileData = false;
3558 for (auto &EI : FS->calls()) {
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003559 HasProfileData |= EI.second.Hotness != CalleeInfo::HotnessType::Unknown;
Teresa Johnson84174c32016-05-10 13:48:23 +00003560 if (HasProfileData)
3561 break;
3562 }
3563
3564 for (auto &EI : FS->calls()) {
3565 // If this GUID doesn't have a value id, it doesn't have a function
3566 // summary and we don't need to record any calls to it.
3567 if (!hasValueId(EI.first.getGUID()))
3568 continue;
3569 NameVals.push_back(getValueId(EI.first.getGUID()));
Teresa Johnson84174c32016-05-10 13:48:23 +00003570 if (HasProfileData)
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003571 NameVals.push_back(static_cast<uint8_t>(EI.second.Hotness));
Teresa Johnson84174c32016-05-10 13:48:23 +00003572 }
3573
3574 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3575 unsigned Code =
3576 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
3577
3578 // Emit the finished record.
3579 Stream.EmitRecord(Code, NameVals, FSAbbrev);
3580 NameVals.clear();
3581 MaybeEmitOriginalName(*S);
Teresa Johnson403a7872015-10-04 14:33:43 +00003582 }
3583
Teresa Johnson28e457b2016-04-24 14:57:11 +00003584 for (auto *AS : Aliases) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003585 auto AliasValueId = SummaryToValueIdMap[AS];
3586 assert(AliasValueId);
3587 NameVals.push_back(AliasValueId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003588 NameVals.push_back(Index.getModuleId(AS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003589 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
Teresa Johnson02e98332016-04-27 13:28:35 +00003590 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
3591 assert(AliaseeValueId);
3592 NameVals.push_back(AliaseeValueId);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003593
3594 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003595 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003596 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003597 MaybeEmitOriginalName(*AS);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003598 }
3599
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003600 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003601}
3602
Teresa Johnson37687f32016-04-23 04:30:47 +00003603void ModuleBitcodeWriter::writeIdentificationBlock() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003604 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
Mehdi Amini5d303282015-10-26 18:37:00 +00003605
3606 // Write the "user readable" string identifying the bitcode producer
3607 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3608 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3609 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3610 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003611 auto StringAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson37687f32016-04-23 04:30:47 +00003612 writeStringRecord(bitc::IDENTIFICATION_CODE_STRING,
3613 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
Mehdi Amini5d303282015-10-26 18:37:00 +00003614
3615 // Write the epoch version
3616 Abbv = new BitCodeAbbrev();
3617 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3618 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003619 auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini5d303282015-10-26 18:37:00 +00003620 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003621 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3622 Stream.ExitBlock();
Mehdi Amini5d303282015-10-26 18:37:00 +00003623}
3624
Teresa Johnson37687f32016-04-23 04:30:47 +00003625void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003626 // Emit the module's hash.
3627 // MODULE_CODE_HASH: [5*i32]
3628 SHA1 Hasher;
Sjoerd Meijer41beee62016-04-27 18:35:02 +00003629 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003630 Buffer.size() - BlockStartPos));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003631 auto Hash = Hasher.result();
3632 SmallVector<uint64_t, 20> Vals;
3633 auto LShift = [&](unsigned char Val, unsigned Amount)
3634 -> uint64_t { return ((uint64_t)Val) << Amount; };
3635 for (int Pos = 0; Pos < 20; Pos += 4) {
3636 uint32_t SubHash = LShift(Hash[Pos + 0], 24);
3637 SubHash |= LShift(Hash[Pos + 1], 16) | LShift(Hash[Pos + 2], 8) |
3638 (unsigned)(unsigned char)Hash[Pos + 3];
3639 Vals.push_back(SubHash);
3640 }
3641
3642 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003643 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003644}
3645
Teresa Johnson37687f32016-04-23 04:30:47 +00003646void BitcodeWriter::write() {
3647 // Emit the file header first.
3648 writeBitcodeHeader();
3649
3650 writeBlocks();
3651}
3652
3653void ModuleBitcodeWriter::writeBlocks() {
3654 writeIdentificationBlock();
3655 writeModule();
3656}
3657
3658void IndexBitcodeWriter::writeBlocks() {
3659 // Index contains only a single outer (module) block.
3660 writeIndex();
3661}
3662
3663void ModuleBitcodeWriter::writeModule() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003664 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3665 size_t BlockStartPos = Buffer.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003666
Jan Wen Voungafaced02012-10-11 20:20:40 +00003667 SmallVector<unsigned, 1> Vals;
3668 unsigned CurVersion = 1;
3669 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003670 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003671
3672 // Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003673 writeBlockInfo();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003674
Bill Wendlingdc095552013-02-10 23:09:32 +00003675 // Emit information about attribute groups.
Teresa Johnson37687f32016-04-23 04:30:47 +00003676 writeAttributeGroupTable();
Bill Wendlingdc095552013-02-10 23:09:32 +00003677
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003678 // Emit information about parameter attributes.
Teresa Johnson37687f32016-04-23 04:30:47 +00003679 writeAttributeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003680
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003681 // Emit information describing all of the types in the module.
Teresa Johnson37687f32016-04-23 04:30:47 +00003682 writeTypeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003683
Teresa Johnson37687f32016-04-23 04:30:47 +00003684 writeComdats();
David Majnemerdad0a642014-06-27 18:19:56 +00003685
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003686 // Emit top-level description of module, including target triple, inline asm,
3687 // descriptors for global variables, and function prototype info.
Teresa Johnson37687f32016-04-23 04:30:47 +00003688 writeModuleInfo();
Devang Patel7428d8a2009-07-22 17:43:22 +00003689
Devang Patele059ba6e2009-07-23 01:07:34 +00003690 // Emit constants.
Teresa Johnson37687f32016-04-23 04:30:47 +00003691 writeModuleConstants();
Devang Patele059ba6e2009-07-23 01:07:34 +00003692
Peter Collingbourne21521892016-06-21 23:42:48 +00003693 // Emit metadata kind names.
3694 writeModuleMetadataKinds();
Devang Patel8cca7b42009-08-04 05:01:35 +00003695
Devang Patelaf206b82009-09-18 19:26:43 +00003696 // Emit metadata.
Peter Collingbourne21521892016-06-21 23:42:48 +00003697 writeModuleMetadata();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003698
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003699 // Emit module-level use-lists.
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003700 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003701 writeUseListBlock(nullptr);
Chad Rosierca2567b2011-12-07 21:44:12 +00003702
Teresa Johnson37687f32016-04-23 04:30:47 +00003703 writeOperandBundleTags();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003704
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003705 // Emit function bodies.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003706 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003707 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003708 if (!F->isDeclaration())
Teresa Johnson37687f32016-04-23 04:30:47 +00003709 writeFunction(*F, FunctionToBitcodeIndex);
Teresa Johnson403a7872015-10-04 14:33:43 +00003710
3711 // Need to write after the above call to WriteFunction which populates
3712 // the summary information in the index.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003713 if (Index)
Teresa Johnson37687f32016-04-23 04:30:47 +00003714 writePerModuleGlobalValueSummary();
Teresa Johnsonff642b92015-09-17 20:12:00 +00003715
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003716 writeValueSymbolTable(M.getValueSymbolTable(),
Teresa Johnson37687f32016-04-23 04:30:47 +00003717 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003718
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003719 if (GenerateHash) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003720 writeModuleHash(BlockStartPos);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003721 }
3722
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003723 Stream.ExitBlock();
Chris Lattner702658c2007-05-04 18:26:27 +00003724}
3725
Teresa Johnson37687f32016-04-23 04:30:47 +00003726static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3727 uint32_t &Position) {
3728 support::endian::write32le(&Buffer[Position], Value);
3729 Position += 4;
3730}
3731
3732/// If generating a bc file on darwin, we have to emit a
Chris Lattnera660f4b2008-07-09 05:14:23 +00003733/// header and trailer to make it compatible with the system archiver. To do
3734/// this we emit the following header, and then emit a trailer that pads the
3735/// file out to be a multiple of 16 bytes.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003736///
Chris Lattnera660f4b2008-07-09 05:14:23 +00003737/// struct bc_header {
3738/// uint32_t Magic; // 0x0B17C0DE
3739/// uint32_t Version; // Version, currently always 0.
3740/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3741/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3742/// uint32_t CPUType; // CPU specifier.
3743/// ... potentially more later ...
3744/// };
Teresa Johnson37687f32016-04-23 04:30:47 +00003745static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003746 const Triple &TT) {
Chris Lattnera660f4b2008-07-09 05:14:23 +00003747 unsigned CPUType = ~0U;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003748
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003749 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng545d3602010-02-12 20:39:35 +00003750 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3751 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3752 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattnera660f4b2008-07-09 05:14:23 +00003753 enum {
3754 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3755 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003756 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattnera660f4b2008-07-09 05:14:23 +00003757 DARWIN_CPU_TYPE_POWERPC = 18
3758 };
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003759
Evan Chengcffdcae2011-06-14 01:51:33 +00003760 Triple::ArchType Arch = TT.getArch();
3761 if (Arch == Triple::x86_64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003762 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003763 else if (Arch == Triple::x86)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003764 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chengcffdcae2011-06-14 01:51:33 +00003765 else if (Arch == Triple::ppc)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003766 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chengcffdcae2011-06-14 01:51:33 +00003767 else if (Arch == Triple::ppc64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003768 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003769 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003770 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003771
Chris Lattnera660f4b2008-07-09 05:14:23 +00003772 // Traditional Bitcode starts after header.
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003773 assert(Buffer.size() >= BWH_HeaderSize &&
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003774 "Expected header size to be reserved");
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003775 unsigned BCOffset = BWH_HeaderSize;
3776 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003777
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003778 // Write the magic and version.
3779 unsigned Position = 0;
Teresa Johnson37687f32016-04-23 04:30:47 +00003780 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
3781 writeInt32ToBuffer(0, Buffer, Position); // Version.
3782 writeInt32ToBuffer(BCOffset, Buffer, Position);
3783 writeInt32ToBuffer(BCSize, Buffer, Position);
3784 writeInt32ToBuffer(CPUType, Buffer, Position);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003785
Chris Lattnera660f4b2008-07-09 05:14:23 +00003786 // If the file is not a multiple of 16 bytes, insert dummy padding.
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003787 while (Buffer.size() & 15)
3788 Buffer.push_back(0);
Chris Lattnera660f4b2008-07-09 05:14:23 +00003789}
3790
Teresa Johnson403a7872015-10-04 14:33:43 +00003791/// Helper to write the header common to all bitcode files.
Teresa Johnson37687f32016-04-23 04:30:47 +00003792void BitcodeWriter::writeBitcodeHeader() {
Teresa Johnson403a7872015-10-04 14:33:43 +00003793 // Emit the file header.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003794 Stream.Emit((unsigned)'B', 8);
3795 Stream.Emit((unsigned)'C', 8);
3796 Stream.Emit(0x0, 4);
3797 Stream.Emit(0xC, 4);
3798 Stream.Emit(0xE, 4);
3799 Stream.Emit(0xD, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00003800}
3801
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003802/// WriteBitcodeToFile - Write the specified module to the specified output
3803/// stream.
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +00003804void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
Teresa Johnson403a7872015-10-04 14:33:43 +00003805 bool ShouldPreserveUseListOrder,
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003806 const ModuleSummaryIndex *Index,
3807 bool GenerateHash) {
Michael Ilsemane26658d2012-12-03 21:29:36 +00003808 SmallVector<char, 0> Buffer;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003809 Buffer.reserve(256*1024);
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003810
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003811 // If this is darwin or another generic macho target, reserve space for the
3812 // header.
3813 Triple TT(M->getTargetTriple());
Akira Hatanaka1235d282016-01-23 16:02:10 +00003814 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003815 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003816
3817 // Emit the module into the buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003818 ModuleBitcodeWriter ModuleWriter(M, Buffer, ShouldPreserveUseListOrder, Index,
3819 GenerateHash);
Teresa Johnson37687f32016-04-23 04:30:47 +00003820 ModuleWriter.write();
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003821
Akira Hatanaka1235d282016-01-23 16:02:10 +00003822 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Teresa Johnson37687f32016-04-23 04:30:47 +00003823 emitDarwinBCHeaderAndTrailer(Buffer, TT);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003824
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003825 // Write the generated bitstream to "Out".
3826 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003827}
Teresa Johnson403a7872015-10-04 14:33:43 +00003828
Teresa Johnson37687f32016-04-23 04:30:47 +00003829void IndexBitcodeWriter::writeIndex() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003830 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Teresa Johnson37687f32016-04-23 04:30:47 +00003831
3832 SmallVector<unsigned, 1> Vals;
3833 unsigned CurVersion = 1;
3834 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003835 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Teresa Johnson37687f32016-04-23 04:30:47 +00003836
3837 // If we have a VST, write the VSTOFFSET record placeholder.
3838 writeValueSymbolTableForwardDecl();
3839
3840 // Write the module paths in the combined index.
3841 writeModStrings();
3842
3843 // Write the summary combined index records.
3844 writeCombinedGlobalValueSummary();
3845
3846 // Need a special VST writer for the combined index (we don't have a
3847 // real VST and real values when this is invoked).
3848 writeCombinedValueSymbolTable();
3849
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003850 Stream.ExitBlock();
Teresa Johnson37687f32016-04-23 04:30:47 +00003851}
3852
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003853// Write the specified module summary index to the given raw output stream,
Teresa Johnson403a7872015-10-04 14:33:43 +00003854// where it will be written in a new bitcode block. This is used when
Teresa Johnson84174c32016-05-10 13:48:23 +00003855// writing the combined index file for ThinLTO. When writing a subset of the
3856// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
3857void llvm::WriteIndexToFile(
3858 const ModuleSummaryIndex &Index, raw_ostream &Out,
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +00003859 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003860 SmallVector<char, 0> Buffer;
3861 Buffer.reserve(256 * 1024);
3862
Teresa Johnson84174c32016-05-10 13:48:23 +00003863 IndexBitcodeWriter IndexWriter(Buffer, Index, ModuleToSummariesForIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00003864 IndexWriter.write();
Teresa Johnson403a7872015-10-04 14:33:43 +00003865
3866 Out.write((char *)&Buffer.front(), Buffer.size());
3867}