blob: feeba31908ae5b75725b3ff432bf92bb108eee89 [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
Teresa Johnsonad176792016-11-11 05:34:58 +000014#include "llvm/Bitcode/BitcodeWriter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "ValueEnumerator.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Triple.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000018#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner362b4a12007-04-23 01:01:37 +000019#include "llvm/Bitcode/LLVMBitCodes.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"
Peter Collingbournea0f371a2017-04-17 17:51:36 +000031#include "llvm/MC/StringTableBuilder.h"
Torok Edwin56d06592009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000033#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000034#include "llvm/Support/Program.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000035#include "llvm/Support/SHA1.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000037#include <cctype>
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000038#include <map>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000039using namespace llvm;
40
Benjamin Kramera65b6102016-05-15 15:18:11 +000041namespace {
Mehdi Aminie98f9252016-12-28 22:30:28 +000042
43cl::opt<unsigned>
44 IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25),
45 cl::desc("Number of metadatas above which we emit an index "
46 "to enable lazy-loading"));
Chris Lattner4d925982007-05-04 20:52:02 +000047/// These are manifest constants used by the bitcode writer. They do not need to
48/// be kept in sync with the reader, but need to be consistent within this file.
49enum {
Chris Lattner4d925982007-05-04 20:52:02 +000050 // VALUE_SYMTAB_BLOCK abbrev id's.
51 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerfd1ad102007-05-04 20:58:35 +000052 VST_ENTRY_7_ABBREV,
Chris Lattnere760d6f2007-05-05 01:26:50 +000053 VST_ENTRY_6_ABBREV,
Chris Lattnerda5e5d22007-05-05 07:36:14 +000054 VST_BBENTRY_6_ABBREV,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000055
Chris Lattnerda5e5d22007-05-05 07:36:14 +000056 // CONSTANTS_BLOCK abbrev id's.
57 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
58 CONSTANTS_INTEGER_ABBREV,
59 CONSTANTS_CE_CAST_Abbrev,
Chris Lattnerb80751d2007-05-05 07:44:49 +000060 CONSTANTS_NULL_Abbrev,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000061
Chris Lattnerb80751d2007-05-05 07:44:49 +000062 // FUNCTION_BLOCK abbrev id's.
Chris Lattnercc6d4c92007-05-06 01:28:01 +000063 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000064 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman0ebd6962009-07-20 21:19:07 +000065 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000066 FUNCTION_INST_CAST_ABBREV,
Chris Lattnercc6d4c92007-05-06 01:28:01 +000067 FUNCTION_INST_RET_VOID_ABBREV,
68 FUNCTION_INST_RET_VAL_ABBREV,
David Blaikieb5b5efd2015-02-25 01:08:52 +000069 FUNCTION_INST_UNREACHABLE_ABBREV,
70 FUNCTION_INST_GEP_ABBREV,
Chris Lattner4d925982007-05-04 20:52:02 +000071};
72
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000073/// Abstract class to manage the bitcode writing, subclassed for each bitcode
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +000074/// file type.
75class BitcodeWriterBase {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000076protected:
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +000077 /// The stream created and owned by the client.
78 BitstreamWriter &Stream;
Teresa Johnson37687f32016-04-23 04:30:47 +000079
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +000080 StringTableBuilder &StrtabBuilder;
81
Teresa Johnson37687f32016-04-23 04:30:47 +000082public:
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +000083 /// Constructs a BitcodeWriterBase object that writes to the provided
84 /// \p Stream.
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +000085 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
86 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
Teresa Johnson37687f32016-04-23 04:30:47 +000087
88protected:
Teresa Johnson37687f32016-04-23 04:30:47 +000089 void writeBitcodeHeader();
Peter Collingbournea0f371a2017-04-17 17:51:36 +000090 void writeModuleVersion();
Teresa Johnson37687f32016-04-23 04:30:47 +000091};
92
Peter Collingbournea0f371a2017-04-17 17:51:36 +000093void BitcodeWriterBase::writeModuleVersion() {
94 // VERSION: [version#]
95 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2});
96}
97
Teresa Johnson37687f32016-04-23 04:30:47 +000098/// Class to manage the bitcode writing for a module.
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +000099class ModuleBitcodeWriter : public BitcodeWriterBase {
100 /// Pointer to the buffer allocated by caller for bitcode writing.
101 const SmallVectorImpl<char> &Buffer;
102
Teresa Johnson37687f32016-04-23 04:30:47 +0000103 /// The Module to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000104 const Module &M;
Teresa Johnson37687f32016-04-23 04:30:47 +0000105
106 /// Enumerates ids for all values in the module.
107 ValueEnumerator VE;
108
109 /// Optional per-module index to write for ThinLTO.
110 const ModuleSummaryIndex *Index;
111
112 /// True if a module hash record should be written.
113 bool GenerateHash;
114
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000115 /// If non-null, when GenerateHash is true, the resulting hash is written
116 /// into ModHash. When GenerateHash is false, that specified value
117 /// is used as the hash instead of computing from the generated bitcode.
118 /// Can be used to produce the same module hash for a minimized bitcode
119 /// used just for the thin link as in the regular full bitcode that will
120 /// be used in the backend.
121 ModuleHash *ModHash;
122
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000123 /// The start bit of the identification block.
124 uint64_t BitcodeStartBit;
Teresa Johnson37687f32016-04-23 04:30:47 +0000125
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000126 /// Map that holds the correspondence between GUIDs in the summary index,
127 /// that came from indirect call profiles, and a value id generated by this
128 /// class to use in the VST and summary block records.
129 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
130
131 /// Tracks the last value id recorded in the GUIDToValueMap.
132 unsigned GlobalValueId;
133
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000134 /// Saves the offset of the VSTOffset record that must eventually be
135 /// backpatched with the offset of the actual VST.
136 uint64_t VSTOffsetPlaceholder = 0;
137
Teresa Johnson37687f32016-04-23 04:30:47 +0000138public:
139 /// Constructs a ModuleBitcodeWriter object for the given Module,
140 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000141 ModuleBitcodeWriter(const Module *M, SmallVectorImpl<char> &Buffer,
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000142 StringTableBuilder &StrtabBuilder,
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000143 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000144 const ModuleSummaryIndex *Index, bool GenerateHash,
145 ModuleHash *ModHash = nullptr)
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000146 : BitcodeWriterBase(Stream, StrtabBuilder), Buffer(Buffer), M(*M),
147 VE(*M, ShouldPreserveUseListOrder), Index(Index),
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000148 GenerateHash(GenerateHash), ModHash(ModHash),
149 BitcodeStartBit(Stream.GetCurrentBitNo()) {
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000150 // Assign ValueIds to any callee values in the index that came from
151 // indirect call profiles and were recorded as a GUID not a Value*
152 // (which would have been assigned an ID by the ValueEnumerator).
153 // The starting ValueId is just after the number of values in the
154 // ValueEnumerator, so that they can be emitted in the VST.
155 GlobalValueId = VE.getValues().size();
Teresa Johnsonbb5c4042016-07-18 18:31:50 +0000156 if (!Index)
157 return;
158 for (const auto &GUIDSummaryLists : *Index)
159 // Examine all summaries for this GUID.
Peter Collingbourne9667b912017-05-04 18:03:25 +0000160 for (auto &Summary : GUIDSummaryLists.second.SummaryList)
Teresa Johnsonbb5c4042016-07-18 18:31:50 +0000161 if (auto FS = dyn_cast<FunctionSummary>(Summary.get()))
162 // For each call in the function summary, see if the call
163 // is to a GUID (which means it is for an indirect call,
164 // otherwise we would have a Value for it). If so, synthesize
165 // a value id.
166 for (auto &CallEdge : FS->calls())
Peter Collingbourne9667b912017-05-04 18:03:25 +0000167 if (!CallEdge.first.getValue())
Teresa Johnsonbb5c4042016-07-18 18:31:50 +0000168 assignValueId(CallEdge.first.getGUID());
Teresa Johnson37687f32016-04-23 04:30:47 +0000169 }
170
Teresa Johnson37687f32016-04-23 04:30:47 +0000171 /// Emit the current module to the bitstream.
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000172 void write();
Teresa Johnson37687f32016-04-23 04:30:47 +0000173
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000174private:
Teresa Johnson37687f32016-04-23 04:30:47 +0000175 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
176
Teresa Johnson37687f32016-04-23 04:30:47 +0000177 void writeAttributeGroupTable();
178 void writeAttributeTable();
179 void writeTypeTable();
180 void writeComdats();
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000181 void writeValueSymbolTableForwardDecl();
Teresa Johnson37687f32016-04-23 04:30:47 +0000182 void writeModuleInfo();
183 void writeValueAsMetadata(const ValueAsMetadata *MD,
184 SmallVectorImpl<uint64_t> &Record);
185 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
186 unsigned Abbrev);
187 unsigned createDILocationAbbrev();
188 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
189 unsigned &Abbrev);
190 unsigned createGenericDINodeAbbrev();
191 void writeGenericDINode(const GenericDINode *N,
192 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
193 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
194 unsigned Abbrev);
195 void writeDIEnumerator(const DIEnumerator *N,
196 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
197 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
198 unsigned Abbrev);
199 void writeDIDerivedType(const DIDerivedType *N,
200 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
201 void writeDICompositeType(const DICompositeType *N,
202 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
203 void writeDISubroutineType(const DISubroutineType *N,
204 SmallVectorImpl<uint64_t> &Record,
205 unsigned Abbrev);
206 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
207 unsigned Abbrev);
208 void writeDICompileUnit(const DICompileUnit *N,
209 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
210 void writeDISubprogram(const DISubprogram *N,
211 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
212 void writeDILexicalBlock(const DILexicalBlock *N,
213 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
214 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
215 SmallVectorImpl<uint64_t> &Record,
216 unsigned Abbrev);
217 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
218 unsigned Abbrev);
219 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
220 unsigned Abbrev);
221 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
222 unsigned Abbrev);
223 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
224 unsigned Abbrev);
225 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
226 SmallVectorImpl<uint64_t> &Record,
227 unsigned Abbrev);
228 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
229 SmallVectorImpl<uint64_t> &Record,
230 unsigned Abbrev);
231 void writeDIGlobalVariable(const DIGlobalVariable *N,
232 SmallVectorImpl<uint64_t> &Record,
233 unsigned Abbrev);
234 void writeDILocalVariable(const DILocalVariable *N,
235 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
236 void writeDIExpression(const DIExpression *N,
237 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000238 void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N,
239 SmallVectorImpl<uint64_t> &Record,
240 unsigned Abbrev);
Teresa Johnson37687f32016-04-23 04:30:47 +0000241 void writeDIObjCProperty(const DIObjCProperty *N,
242 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
243 void writeDIImportedEntity(const DIImportedEntity *N,
244 SmallVectorImpl<uint64_t> &Record,
245 unsigned Abbrev);
246 unsigned createNamedMetadataAbbrev();
247 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
248 unsigned createMetadataStringsAbbrev();
249 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
250 SmallVectorImpl<uint64_t> &Record);
251 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
Mehdi Aminie98f9252016-12-28 22:30:28 +0000252 SmallVectorImpl<uint64_t> &Record,
253 std::vector<unsigned> *MDAbbrevs = nullptr,
254 std::vector<uint64_t> *IndexPos = nullptr);
Teresa Johnson37687f32016-04-23 04:30:47 +0000255 void writeModuleMetadata();
256 void writeFunctionMetadata(const Function &F);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000257 void writeFunctionMetadataAttachment(const Function &F);
258 void writeGlobalVariableMetadataAttachment(const GlobalVariable &GV);
259 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
260 const GlobalObject &GO);
Peter Collingbourne21521892016-06-21 23:42:48 +0000261 void writeModuleMetadataKinds();
Teresa Johnson37687f32016-04-23 04:30:47 +0000262 void writeOperandBundleTags();
263 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
264 void writeModuleConstants();
265 bool pushValueAndType(const Value *V, unsigned InstID,
266 SmallVectorImpl<unsigned> &Vals);
267 void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
268 void pushValue(const Value *V, unsigned InstID,
269 SmallVectorImpl<unsigned> &Vals);
270 void pushValueSigned(const Value *V, unsigned InstID,
271 SmallVectorImpl<uint64_t> &Vals);
272 void writeInstruction(const Instruction &I, unsigned InstID,
273 SmallVectorImpl<unsigned> &Vals);
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000274 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
275 void writeGlobalValueSymbolTable(
276 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +0000277 void writeUseList(UseListOrder &&Order);
278 void writeUseListBlock(const Function *F);
279 void
280 writeFunction(const Function &F,
281 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
282 void writeBlockInfo();
283 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
Teresa Johnson28e457b2016-04-24 14:57:11 +0000284 GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +0000285 unsigned ValueID,
286 unsigned FSCallsAbbrev,
287 unsigned FSCallsProfileAbbrev,
288 const Function &F);
289 void writeModuleLevelReferences(const GlobalVariable &V,
290 SmallVector<uint64_t, 64> &NameVals,
291 unsigned FSModRefsAbbrev);
292 void writePerModuleGlobalValueSummary();
293 void writeModuleHash(size_t BlockStartPos);
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000294
295 void assignValueId(GlobalValue::GUID ValGUID) {
296 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
297 }
298 unsigned getValueId(GlobalValue::GUID ValGUID) {
299 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
Teresa Johnsonbb5c4042016-07-18 18:31:50 +0000300 // Expect that any GUID value had a value Id assigned by an
301 // earlier call to assignValueId.
302 assert(VMI != GUIDToValueIdMap.end() &&
303 "GUID does not have assigned value Id");
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000304 return VMI->second;
305 }
306 // Helper to get the valueId for the type of value recorded in VI.
307 unsigned getValueId(ValueInfo VI) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000308 if (!VI.getValue())
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000309 return getValueId(VI.getGUID());
310 return VE.getValueID(VI.getValue());
311 }
312 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
Teresa Johnson37687f32016-04-23 04:30:47 +0000313};
314
315/// Class to manage the bitcode writing for a combined index.
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000316class IndexBitcodeWriter : public BitcodeWriterBase {
Teresa Johnson37687f32016-04-23 04:30:47 +0000317 /// The combined index to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000318 const ModuleSummaryIndex &Index;
Teresa Johnson37687f32016-04-23 04:30:47 +0000319
Teresa Johnson84174c32016-05-10 13:48:23 +0000320 /// When writing a subset of the index for distributed backends, client
321 /// provides a map of modules to the corresponding GUIDs/summaries to write.
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +0000322 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
Teresa Johnson84174c32016-05-10 13:48:23 +0000323
Teresa Johnson37687f32016-04-23 04:30:47 +0000324 /// Map that holds the correspondence between the GUID used in the combined
325 /// index and a value id generated by this class to use in references.
326 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
327
328 /// Tracks the last value id recorded in the GUIDToValueMap.
329 unsigned GlobalValueId = 0;
330
331public:
332 /// Constructs a IndexBitcodeWriter object for the given combined index,
Teresa Johnson84174c32016-05-10 13:48:23 +0000333 /// writing to the provided \p Buffer. When writing a subset of the index
334 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000335 IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
336 const ModuleSummaryIndex &Index,
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +0000337 const std::map<std::string, GVSummaryMapTy>
Teresa Johnson84174c32016-05-10 13:48:23 +0000338 *ModuleToSummariesForIndex = nullptr)
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000339 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
Teresa Johnson84174c32016-05-10 13:48:23 +0000340 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
341 // Assign unique value ids to all summaries to be written, for use
Teresa Johnson37687f32016-04-23 04:30:47 +0000342 // in writing out the call graph edges. Save the mapping from GUID
343 // to the new global value id to use when writing those edges, which
344 // are currently saved in the index in terms of GUID.
Peter Collingbourne7c2c4092017-05-02 17:48:39 +0000345 forEachSummary([&](GVInfo I) {
Teresa Johnson84174c32016-05-10 13:48:23 +0000346 GUIDToValueIdMap[I.first] = ++GlobalValueId;
Peter Collingbourne7c2c4092017-05-02 17:48:39 +0000347 });
Teresa Johnson37687f32016-04-23 04:30:47 +0000348 }
349
Teresa Johnson84174c32016-05-10 13:48:23 +0000350 /// The below iterator returns the GUID and associated summary.
351 typedef std::pair<GlobalValue::GUID, GlobalValueSummary *> GVInfo;
352
Peter Collingbourne7c2c4092017-05-02 17:48:39 +0000353 /// Calls the callback for each value GUID and summary to be written to
354 /// bitcode. This hides the details of whether they are being pulled from the
355 /// entire index or just those in a provided ModuleToSummariesForIndex map.
David Blaikie358c0122017-06-02 18:25:29 +0000356 template<typename Functor>
357 void forEachSummary(Functor Callback) {
Peter Collingbourne7c2c4092017-05-02 17:48:39 +0000358 if (ModuleToSummariesForIndex) {
359 for (auto &M : *ModuleToSummariesForIndex)
360 for (auto &Summary : M.second)
361 Callback(Summary);
362 } else {
363 for (auto &Summaries : Index)
Peter Collingbourne9667b912017-05-04 18:03:25 +0000364 for (auto &Summary : Summaries.second.SummaryList)
Peter Collingbourne7c2c4092017-05-02 17:48:39 +0000365 Callback({Summaries.first, Summary.get()});
Teresa Johnson84174c32016-05-10 13:48:23 +0000366 }
Peter Collingbourne7c2c4092017-05-02 17:48:39 +0000367 }
Teresa Johnson84174c32016-05-10 13:48:23 +0000368
Teresa Johnson7a27b132017-06-02 01:56:02 +0000369 /// Calls the callback for each entry in the modulePaths StringMap that
370 /// should be written to the module path string table. This hides the details
371 /// of whether they are being pulled from the entire index or just those in a
372 /// provided ModuleToSummariesForIndex map.
David Blaikieb6b42e02017-06-02 17:24:26 +0000373 template <typename Functor> void forEachModule(Functor Callback) {
Teresa Johnson7a27b132017-06-02 01:56:02 +0000374 if (ModuleToSummariesForIndex) {
375 for (const auto &M : *ModuleToSummariesForIndex) {
376 const auto &MPI = Index.modulePaths().find(M.first);
377 if (MPI == Index.modulePaths().end()) {
378 // This should only happen if the bitcode file was empty, in which
379 // case we shouldn't be importing (the ModuleToSummariesForIndex
380 // would only include the module we are writing and index for).
381 assert(ModuleToSummariesForIndex->size() == 1);
382 continue;
383 }
384 Callback(*MPI);
385 }
386 } else {
387 for (const auto &MPSE : Index.modulePaths())
388 Callback(MPSE);
389 }
390 }
391
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000392 /// Main entry point for writing a combined index to bitcode.
393 void write();
Teresa Johnson37687f32016-04-23 04:30:47 +0000394
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000395private:
Teresa Johnson37687f32016-04-23 04:30:47 +0000396 void writeModStrings();
Teresa Johnson37687f32016-04-23 04:30:47 +0000397 void writeCombinedGlobalValueSummary();
398
Teresa Johnsona6a3fb52017-05-31 18:58:11 +0000399 Optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000400 auto VMI = GUIDToValueIdMap.find(ValGUID);
Teresa Johnsona6a3fb52017-05-31 18:58:11 +0000401 if (VMI == GUIDToValueIdMap.end())
402 return None;
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000403 return VMI->second;
Teresa Johnson37687f32016-04-23 04:30:47 +0000404 }
Teresa Johnson37687f32016-04-23 04:30:47 +0000405 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
406};
Benjamin Kramera65b6102016-05-15 15:18:11 +0000407} // end anonymous namespace
Teresa Johnson37687f32016-04-23 04:30:47 +0000408
409static unsigned getEncodedCastOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000410 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000411 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000412 case Instruction::Trunc : return bitc::CAST_TRUNC;
413 case Instruction::ZExt : return bitc::CAST_ZEXT;
414 case Instruction::SExt : return bitc::CAST_SEXT;
415 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
416 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
417 case Instruction::UIToFP : return bitc::CAST_UITOFP;
418 case Instruction::SIToFP : return bitc::CAST_SITOFP;
419 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
420 case Instruction::FPExt : return bitc::CAST_FPEXT;
421 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
422 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
423 case Instruction::BitCast : return bitc::CAST_BITCAST;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000424 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000425 }
426}
427
Teresa Johnson37687f32016-04-23 04:30:47 +0000428static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000429 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000430 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmana5b96452009-06-04 22:49:04 +0000431 case Instruction::Add:
432 case Instruction::FAdd: return bitc::BINOP_ADD;
433 case Instruction::Sub:
434 case Instruction::FSub: return bitc::BINOP_SUB;
435 case Instruction::Mul:
436 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000437 case Instruction::UDiv: return bitc::BINOP_UDIV;
438 case Instruction::FDiv:
439 case Instruction::SDiv: return bitc::BINOP_SDIV;
440 case Instruction::URem: return bitc::BINOP_UREM;
441 case Instruction::FRem:
442 case Instruction::SRem: return bitc::BINOP_SREM;
443 case Instruction::Shl: return bitc::BINOP_SHL;
444 case Instruction::LShr: return bitc::BINOP_LSHR;
445 case Instruction::AShr: return bitc::BINOP_ASHR;
446 case Instruction::And: return bitc::BINOP_AND;
447 case Instruction::Or: return bitc::BINOP_OR;
448 case Instruction::Xor: return bitc::BINOP_XOR;
449 }
450}
451
Teresa Johnson37687f32016-04-23 04:30:47 +0000452static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000453 switch (Op) {
454 default: llvm_unreachable("Unknown RMW operation!");
455 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
456 case AtomicRMWInst::Add: return bitc::RMW_ADD;
457 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
458 case AtomicRMWInst::And: return bitc::RMW_AND;
459 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
460 case AtomicRMWInst::Or: return bitc::RMW_OR;
461 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
462 case AtomicRMWInst::Max: return bitc::RMW_MAX;
463 case AtomicRMWInst::Min: return bitc::RMW_MIN;
464 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
465 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
466 }
467}
468
Teresa Johnson37687f32016-04-23 04:30:47 +0000469static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000470 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +0000471 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
472 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
473 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
474 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
475 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
476 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
477 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
Eli Friedmanfee02c62011-07-25 23:16:38 +0000478 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000479 llvm_unreachable("Invalid ordering");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000480}
481
Teresa Johnson37687f32016-04-23 04:30:47 +0000482static unsigned getEncodedSynchScope(SynchronizationScope SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000483 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000484 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
485 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
486 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000487 llvm_unreachable("Invalid synch scope");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000488}
489
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000490static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
491 StringRef Str, unsigned AbbrevToUse) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000492 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000493
Chris Lattnere14cb882007-05-04 19:11:41 +0000494 // Code: [strchar x N]
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000495 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
496 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
497 AbbrevToUse = 0;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000498 Vals.push_back(Str[i]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000499 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000500
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000501 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000502 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000503}
504
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000505static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
506 switch (Kind) {
507 case Attribute::Alignment:
508 return bitc::ATTR_KIND_ALIGNMENT;
George Burgess IV278199f2016-04-12 01:05:35 +0000509 case Attribute::AllocSize:
510 return bitc::ATTR_KIND_ALLOC_SIZE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000511 case Attribute::AlwaysInline:
512 return bitc::ATTR_KIND_ALWAYS_INLINE;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000513 case Attribute::ArgMemOnly:
514 return bitc::ATTR_KIND_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000515 case Attribute::Builtin:
516 return bitc::ATTR_KIND_BUILTIN;
517 case Attribute::ByVal:
518 return bitc::ATTR_KIND_BY_VAL;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000519 case Attribute::Convergent:
520 return bitc::ATTR_KIND_CONVERGENT;
Reid Klecknera534a382013-12-19 02:14:12 +0000521 case Attribute::InAlloca:
522 return bitc::ATTR_KIND_IN_ALLOCA;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000523 case Attribute::Cold:
524 return bitc::ATTR_KIND_COLD;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000525 case Attribute::InaccessibleMemOnly:
526 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
527 case Attribute::InaccessibleMemOrArgMemOnly:
528 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000529 case Attribute::InlineHint:
530 return bitc::ATTR_KIND_INLINE_HINT;
531 case Attribute::InReg:
532 return bitc::ATTR_KIND_IN_REG;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000533 case Attribute::JumpTable:
534 return bitc::ATTR_KIND_JUMP_TABLE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000535 case Attribute::MinSize:
536 return bitc::ATTR_KIND_MIN_SIZE;
537 case Attribute::Naked:
538 return bitc::ATTR_KIND_NAKED;
539 case Attribute::Nest:
540 return bitc::ATTR_KIND_NEST;
541 case Attribute::NoAlias:
542 return bitc::ATTR_KIND_NO_ALIAS;
543 case Attribute::NoBuiltin:
544 return bitc::ATTR_KIND_NO_BUILTIN;
545 case Attribute::NoCapture:
546 return bitc::ATTR_KIND_NO_CAPTURE;
547 case Attribute::NoDuplicate:
548 return bitc::ATTR_KIND_NO_DUPLICATE;
549 case Attribute::NoImplicitFloat:
550 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
551 case Attribute::NoInline:
552 return bitc::ATTR_KIND_NO_INLINE;
James Molloye6f87ca2015-11-06 10:32:53 +0000553 case Attribute::NoRecurse:
554 return bitc::ATTR_KIND_NO_RECURSE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000555 case Attribute::NonLazyBind:
556 return bitc::ATTR_KIND_NON_LAZY_BIND;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000557 case Attribute::NonNull:
558 return bitc::ATTR_KIND_NON_NULL;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000559 case Attribute::Dereferenceable:
560 return bitc::ATTR_KIND_DEREFERENCEABLE;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000561 case Attribute::DereferenceableOrNull:
562 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000563 case Attribute::NoRedZone:
564 return bitc::ATTR_KIND_NO_RED_ZONE;
565 case Attribute::NoReturn:
566 return bitc::ATTR_KIND_NO_RETURN;
567 case Attribute::NoUnwind:
568 return bitc::ATTR_KIND_NO_UNWIND;
569 case Attribute::OptimizeForSize:
570 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000571 case Attribute::OptimizeNone:
572 return bitc::ATTR_KIND_OPTIMIZE_NONE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000573 case Attribute::ReadNone:
574 return bitc::ATTR_KIND_READ_NONE;
575 case Attribute::ReadOnly:
576 return bitc::ATTR_KIND_READ_ONLY;
577 case Attribute::Returned:
578 return bitc::ATTR_KIND_RETURNED;
579 case Attribute::ReturnsTwice:
580 return bitc::ATTR_KIND_RETURNS_TWICE;
581 case Attribute::SExt:
582 return bitc::ATTR_KIND_S_EXT;
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000583 case Attribute::Speculatable:
584 return bitc::ATTR_KIND_SPECULATABLE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000585 case Attribute::StackAlignment:
586 return bitc::ATTR_KIND_STACK_ALIGNMENT;
587 case Attribute::StackProtect:
588 return bitc::ATTR_KIND_STACK_PROTECT;
589 case Attribute::StackProtectReq:
590 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
591 case Attribute::StackProtectStrong:
592 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000593 case Attribute::SafeStack:
594 return bitc::ATTR_KIND_SAFESTACK;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000595 case Attribute::StructRet:
596 return bitc::ATTR_KIND_STRUCT_RET;
597 case Attribute::SanitizeAddress:
598 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
599 case Attribute::SanitizeThread:
600 return bitc::ATTR_KIND_SANITIZE_THREAD;
601 case Attribute::SanitizeMemory:
602 return bitc::ATTR_KIND_SANITIZE_MEMORY;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000603 case Attribute::SwiftError:
604 return bitc::ATTR_KIND_SWIFT_ERROR;
Manman Renf46262e2016-03-29 17:37:21 +0000605 case Attribute::SwiftSelf:
606 return bitc::ATTR_KIND_SWIFT_SELF;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000607 case Attribute::UWTable:
608 return bitc::ATTR_KIND_UW_TABLE;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000609 case Attribute::WriteOnly:
610 return bitc::ATTR_KIND_WRITEONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000611 case Attribute::ZExt:
612 return bitc::ATTR_KIND_Z_EXT;
613 case Attribute::EndAttrKinds:
614 llvm_unreachable("Can not encode end-attribute kinds marker.");
615 case Attribute::None:
616 llvm_unreachable("Can not encode none-attribute.");
617 }
618
619 llvm_unreachable("Trying to encode unknown attribute");
620}
621
Teresa Johnson37687f32016-04-23 04:30:47 +0000622void ModuleBitcodeWriter::writeAttributeGroupTable() {
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000623 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
624 VE.getAttributeGroups();
Bill Wendling92ed7002013-02-11 22:33:26 +0000625 if (AttrGrps.empty()) return;
Bill Wendlingdc095552013-02-10 23:09:32 +0000626
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000627 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
Bill Wendlingdc095552013-02-10 23:09:32 +0000628
629 SmallVector<uint64_t, 64> Record;
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000630 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
631 unsigned AttrListIndex = Pair.first;
632 AttributeSet AS = Pair.second;
633 Record.push_back(VE.getAttributeGroupID(Pair));
634 Record.push_back(AttrListIndex);
Bill Wendlingdc095552013-02-10 23:09:32 +0000635
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000636 for (Attribute Attr : AS) {
637 if (Attr.isEnumAttribute()) {
638 Record.push_back(0);
639 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
640 } else if (Attr.isIntAttribute()) {
641 Record.push_back(1);
642 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
643 Record.push_back(Attr.getValueAsInt());
644 } else {
645 StringRef Kind = Attr.getKindAsString();
646 StringRef Val = Attr.getValueAsString();
Bill Wendlingdc095552013-02-10 23:09:32 +0000647
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000648 Record.push_back(Val.empty() ? 3 : 4);
649 Record.append(Kind.begin(), Kind.end());
650 Record.push_back(0);
651 if (!Val.empty()) {
652 Record.append(Val.begin(), Val.end());
Bill Wendlingdc095552013-02-10 23:09:32 +0000653 Record.push_back(0);
Bill Wendlingdc095552013-02-10 23:09:32 +0000654 }
655 }
Bill Wendlingdc095552013-02-10 23:09:32 +0000656 }
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000657
658 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
659 Record.clear();
Bill Wendlingdc095552013-02-10 23:09:32 +0000660 }
661
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000662 Stream.ExitBlock();
Bill Wendlingdc095552013-02-10 23:09:32 +0000663}
664
Teresa Johnson37687f32016-04-23 04:30:47 +0000665void ModuleBitcodeWriter::writeAttributeTable() {
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000666 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000667 if (Attrs.empty()) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000668
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000669 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000670
671 SmallVector<uint64_t, 64> Record;
672 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000673 AttributeList AL = Attrs[i];
674 for (unsigned i = AL.index_begin(), e = AL.index_end(); i != e; ++i) {
675 AttributeSet AS = AL.getAttributes(i);
676 if (AS.hasAttributes())
677 Record.push_back(VE.getAttributeGroupID({i, AS}));
678 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000679
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000680 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000681 Record.clear();
682 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000683
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000684 Stream.ExitBlock();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000685}
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000686
687/// WriteTypeTable - Write out the type table for a module.
Teresa Johnson37687f32016-04-23 04:30:47 +0000688void ModuleBitcodeWriter::writeTypeTable() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000689 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000690
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000691 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000692 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000693
David Blaikie7b028102015-02-25 00:51:52 +0000694 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
Chad Rosier9646c0d2011-12-08 00:38:45 +0000695
Chris Lattnerccee7062007-05-05 06:30:12 +0000696 // Abbrev for TYPE_CODE_POINTER.
David Blaikie7ad9dc12017-01-04 22:36:33 +0000697 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerccee7062007-05-05 06:30:12 +0000698 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000699 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
Christopher Lamb25f50762007-12-12 08:44:39 +0000700 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
David Blaikie7ad9dc12017-01-04 22:36:33 +0000701 unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000702
Chris Lattnerccee7062007-05-05 06:30:12 +0000703 // Abbrev for TYPE_CODE_FUNCTION.
David Blaikie7ad9dc12017-01-04 22:36:33 +0000704 Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerccee7062007-05-05 06:30:12 +0000705 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
706 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnerccee7062007-05-05 06:30:12 +0000707 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000708 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
709
David Blaikie7ad9dc12017-01-04 22:36:33 +0000710 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000711
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000712 // Abbrev for TYPE_CODE_STRUCT_ANON.
David Blaikie7ad9dc12017-01-04 22:36:33 +0000713 Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000714 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
Chris Lattnerccee7062007-05-05 06:30:12 +0000715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
716 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000717 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
718
David Blaikie7ad9dc12017-01-04 22:36:33 +0000719 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000720
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000721 // Abbrev for TYPE_CODE_STRUCT_NAME.
David Blaikie7ad9dc12017-01-04 22:36:33 +0000722 Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000723 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
724 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
725 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
David Blaikie7ad9dc12017-01-04 22:36:33 +0000726 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000727
728 // Abbrev for TYPE_CODE_STRUCT_NAMED.
David Blaikie7ad9dc12017-01-04 22:36:33 +0000729 Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000730 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
732 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000733 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
734
David Blaikie7ad9dc12017-01-04 22:36:33 +0000735 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000736
Chris Lattnerccee7062007-05-05 06:30:12 +0000737 // Abbrev for TYPE_CODE_ARRAY.
David Blaikie7ad9dc12017-01-04 22:36:33 +0000738 Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerccee7062007-05-05 06:30:12 +0000739 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
740 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
Chad Rosier9646c0d2011-12-08 00:38:45 +0000741 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
742
David Blaikie7ad9dc12017-01-04 22:36:33 +0000743 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000744
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000745 // Emit an entry count so the reader can reserve space.
746 TypeVals.push_back(TypeList.size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000747 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000748 TypeVals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000749
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000750 // Loop over all of the types, emitting each in turn.
751 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000752 Type *T = TypeList[i];
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000753 int AbbrevToUse = 0;
754 unsigned Code = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000755
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000756 switch (T->getTypeID()) {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000757 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
758 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
759 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
760 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
761 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
762 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000763 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000764 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
765 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
766 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
David Majnemerb611e3f2015-08-14 05:09:07 +0000767 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000768 case Type::IntegerTyID:
769 // INTEGER: [width]
770 Code = bitc::TYPE_CODE_INTEGER;
771 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
772 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000773 case Type::PointerTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000774 PointerType *PTy = cast<PointerType>(T);
Christopher Lamb25f50762007-12-12 08:44:39 +0000775 // POINTER: [pointee type, address space]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000776 Code = bitc::TYPE_CODE_POINTER;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000777 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lamb25f50762007-12-12 08:44:39 +0000778 unsigned AddressSpace = PTy->getAddressSpace();
779 TypeVals.push_back(AddressSpace);
780 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000781 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000782 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000783 case Type::FunctionTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000784 FunctionType *FT = cast<FunctionType>(T);
Chad Rosier95898722011-11-03 00:14:01 +0000785 // FUNCTION: [isvararg, retty, paramty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000786 Code = bitc::TYPE_CODE_FUNCTION;
787 TypeVals.push_back(FT->isVarArg());
788 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000789 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
790 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerccee7062007-05-05 06:30:12 +0000791 AbbrevToUse = FunctionAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000792 break;
793 }
794 case Type::StructTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000795 StructType *ST = cast<StructType>(T);
Chris Lattnerccee7062007-05-05 06:30:12 +0000796 // STRUCT: [ispacked, eltty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000797 TypeVals.push_back(ST->isPacked());
Chris Lattnere14cb882007-05-04 19:11:41 +0000798 // Output all of the element types.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000799 for (StructType::element_iterator I = ST->element_begin(),
800 E = ST->element_end(); I != E; ++I)
801 TypeVals.push_back(VE.getTypeID(*I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000802
Chris Lattner335d3992011-08-12 18:06:37 +0000803 if (ST->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000804 Code = bitc::TYPE_CODE_STRUCT_ANON;
805 AbbrevToUse = StructAnonAbbrev;
806 } else {
807 if (ST->isOpaque()) {
808 Code = bitc::TYPE_CODE_OPAQUE;
809 } else {
810 Code = bitc::TYPE_CODE_STRUCT_NAMED;
811 AbbrevToUse = StructNamedAbbrev;
812 }
813
814 // Emit the name if it is present.
815 if (!ST->getName().empty())
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +0000816 writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
Teresa Johnson37687f32016-04-23 04:30:47 +0000817 StructNameAbbrev);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000818 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000819 break;
820 }
821 case Type::ArrayTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000822 ArrayType *AT = cast<ArrayType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000823 // ARRAY: [numelts, eltty]
824 Code = bitc::TYPE_CODE_ARRAY;
825 TypeVals.push_back(AT->getNumElements());
826 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerccee7062007-05-05 06:30:12 +0000827 AbbrevToUse = ArrayAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000828 break;
829 }
830 case Type::VectorTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000831 VectorType *VT = cast<VectorType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000832 // VECTOR [numelts, eltty]
833 Code = bitc::TYPE_CODE_VECTOR;
834 TypeVals.push_back(VT->getNumElements());
835 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
836 break;
837 }
838 }
839
840 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000841 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000842 TypeVals.clear();
843 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000844
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000845 Stream.ExitBlock();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000846}
847
Teresa Johnson5e22e442016-02-06 16:07:35 +0000848static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
849 switch (Linkage) {
Rafael Espindola261d25b2015-01-08 16:25:01 +0000850 case GlobalValue::ExternalLinkage:
851 return 0;
852 case GlobalValue::WeakAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000853 return 16;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000854 case GlobalValue::AppendingLinkage:
855 return 2;
856 case GlobalValue::InternalLinkage:
857 return 3;
858 case GlobalValue::LinkOnceAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000859 return 18;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000860 case GlobalValue::ExternalWeakLinkage:
861 return 7;
862 case GlobalValue::CommonLinkage:
863 return 8;
864 case GlobalValue::PrivateLinkage:
865 return 9;
866 case GlobalValue::WeakODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000867 return 17;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000868 case GlobalValue::LinkOnceODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000869 return 19;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000870 case GlobalValue::AvailableExternallyLinkage:
871 return 12;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000872 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000873 llvm_unreachable("Invalid linkage");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000874}
875
Teresa Johnson5e22e442016-02-06 16:07:35 +0000876static unsigned getEncodedLinkage(const GlobalValue &GV) {
877 return getEncodedLinkage(GV.getLinkage());
878}
879
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000880// Decode the flags for GlobalValue in the summary
881static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
882 uint64_t RawFlags = 0;
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000883
Mehdi Amini1380edf2017-02-03 07:41:43 +0000884 RawFlags |= Flags.NotEligibleToImport; // bool
Evgeniy Stepanov56584bb2017-06-01 20:30:06 +0000885 RawFlags |= (Flags.Live << 1);
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000886 // Linkage don't need to be remapped at that time for the summary. Any future
887 // change to the getEncodedLinkage() function will need to be taken into
888 // account here as well.
Mehdi Amini1380edf2017-02-03 07:41:43 +0000889 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
890
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000891 return RawFlags;
892}
893
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000894static unsigned getEncodedVisibility(const GlobalValue &GV) {
895 switch (GV.getVisibility()) {
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000896 case GlobalValue::DefaultVisibility: return 0;
897 case GlobalValue::HiddenVisibility: return 1;
898 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000899 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000900 llvm_unreachable("Invalid visibility");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000901}
902
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000903static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
904 switch (GV.getDLLStorageClass()) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000905 case GlobalValue::DefaultStorageClass: return 0;
906 case GlobalValue::DLLImportStorageClass: return 1;
907 case GlobalValue::DLLExportStorageClass: return 2;
908 }
909 llvm_unreachable("Invalid DLL storage class");
910}
911
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000912static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000913 switch (GV.getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000914 case GlobalVariable::NotThreadLocal: return 0;
915 case GlobalVariable::GeneralDynamicTLSModel: return 1;
916 case GlobalVariable::LocalDynamicTLSModel: return 2;
917 case GlobalVariable::InitialExecTLSModel: return 3;
918 case GlobalVariable::LocalExecTLSModel: return 4;
919 }
920 llvm_unreachable("Invalid TLS model");
921}
922
David Majnemerdad0a642014-06-27 18:19:56 +0000923static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
924 switch (C.getSelectionKind()) {
925 case Comdat::Any:
926 return bitc::COMDAT_SELECTION_KIND_ANY;
927 case Comdat::ExactMatch:
928 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
929 case Comdat::Largest:
930 return bitc::COMDAT_SELECTION_KIND_LARGEST;
931 case Comdat::NoDuplicates:
932 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
933 case Comdat::SameSize:
934 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
935 }
936 llvm_unreachable("Invalid selection kind");
937}
938
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000939static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
940 switch (GV.getUnnamedAddr()) {
941 case GlobalValue::UnnamedAddr::None: return 0;
942 case GlobalValue::UnnamedAddr::Local: return 2;
943 case GlobalValue::UnnamedAddr::Global: return 1;
944 }
945 llvm_unreachable("Invalid unnamed_addr");
946}
947
Teresa Johnson37687f32016-04-23 04:30:47 +0000948void ModuleBitcodeWriter::writeComdats() {
David Majnemer90a021f2016-03-13 08:01:03 +0000949 SmallVector<unsigned, 64> Vals;
David Majnemerdad0a642014-06-27 18:19:56 +0000950 for (const Comdat *C : VE.getComdats()) {
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000951 // COMDAT: [strtab offset, strtab size, selection_kind]
952 Vals.push_back(StrtabBuilder.add(C->getName()));
953 Vals.push_back(C->getName().size());
David Majnemerdad0a642014-06-27 18:19:56 +0000954 Vals.push_back(getEncodedComdatSelectionKind(*C));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000955 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
David Majnemerdad0a642014-06-27 18:19:56 +0000956 Vals.clear();
957 }
958}
959
Teresa Johnsonff642b92015-09-17 20:12:00 +0000960/// Write a record that will eventually hold the word offset of the
961/// module-level VST. For now the offset is 0, which will be backpatched
Teresa Johnson37687f32016-04-23 04:30:47 +0000962/// after the real VST is written. Saves the bit offset to backpatch.
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000963void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
Teresa Johnsonff642b92015-09-17 20:12:00 +0000964 // Write a placeholder value in for the offset of the real VST,
965 // which is written after the function blocks so that it can include
966 // the offset of each function. The placeholder offset will be
967 // updated when the real VST is written.
David Blaikie7ad9dc12017-01-04 22:36:33 +0000968 auto Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnsonff642b92015-09-17 20:12:00 +0000969 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
970 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
971 // hold the real VST offset. Must use fixed instead of VBR as we don't
972 // know how many VBR chunks to reserve ahead of time.
973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
David Blaikie7ad9dc12017-01-04 22:36:33 +0000974 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnsonff642b92015-09-17 20:12:00 +0000975
976 // Emit the placeholder
977 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000978 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
Teresa Johnsonff642b92015-09-17 20:12:00 +0000979
Teresa Johnson37687f32016-04-23 04:30:47 +0000980 // Compute and save the bit offset to the placeholder, which will be
Teresa Johnsonff642b92015-09-17 20:12:00 +0000981 // patched when the real VST is written. We can simply subtract the 32-bit
982 // fixed size from the current bit number to get the location to backpatch.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000983 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
Teresa Johnsonff642b92015-09-17 20:12:00 +0000984}
985
Teresa Johnsone1164de2016-02-10 21:55:02 +0000986enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
987
988/// Determine the encoding to use for the given string name and length.
David Blaikieb6b42e02017-06-02 17:24:26 +0000989static StringEncoding getStringEncoding(StringRef Str) {
Teresa Johnsone1164de2016-02-10 21:55:02 +0000990 bool isChar6 = true;
David Blaikieb6b42e02017-06-02 17:24:26 +0000991 for (char C : Str) {
Teresa Johnsone1164de2016-02-10 21:55:02 +0000992 if (isChar6)
David Blaikieb6b42e02017-06-02 17:24:26 +0000993 isChar6 = BitCodeAbbrevOp::isChar6(C);
994 if ((unsigned char)C & 128)
Teresa Johnsone1164de2016-02-10 21:55:02 +0000995 // don't bother scanning the rest.
996 return SE_Fixed8;
997 }
998 if (isChar6)
999 return SE_Char6;
David Blaikieb6b42e02017-06-02 17:24:26 +00001000 return SE_Fixed7;
Teresa Johnsone1164de2016-02-10 21:55:02 +00001001}
1002
Teresa Johnsonff642b92015-09-17 20:12:00 +00001003/// Emit top-level description of module, including target triple, inline asm,
1004/// descriptors for global variables, and function prototype info.
1005/// Returns the bit offset to backpatch with the location of the real VST.
Teresa Johnson37687f32016-04-23 04:30:47 +00001006void ModuleBitcodeWriter::writeModuleInfo() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001007 // Emit various pieces of data attached to a module.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001008 if (!M.getTargetTriple().empty())
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00001009 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001010 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001011 const std::string &DL = M.getDataLayoutStr();
Rafael Espindolaf863ee22014-02-25 20:01:08 +00001012 if (!DL.empty())
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00001013 writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001014 if (!M.getModuleInlineAsm().empty())
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00001015 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001016 0 /*TODO*/);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001017
Gordon Henriksend930f912008-08-17 18:44:35 +00001018 // Emit information about sections and GC, computing how many there are. Also
1019 // compute the maximum alignment value.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001020 std::map<std::string, unsigned> SectionMap;
Gordon Henriksend930f912008-08-17 18:44:35 +00001021 std::map<std::string, unsigned> GCMap;
Chris Lattner4b00d922007-04-23 16:04:05 +00001022 unsigned MaxAlignment = 0;
Chris Lattnerb5491372007-04-23 18:58:34 +00001023 unsigned MaxGlobalType = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001024 for (const GlobalValue &GV : M.globals()) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001025 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
David Blaikie1a848da2015-04-27 19:58:56 +00001026 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001027 if (GV.hasSection()) {
Chad Rosier75ec09c2011-08-12 16:45:18 +00001028 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001029 unsigned &Entry = SectionMap[GV.getSection()];
Chad Rosier75ec09c2011-08-12 16:45:18 +00001030 if (!Entry) {
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00001031 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001032 0 /*TODO*/);
Chad Rosier75ec09c2011-08-12 16:45:18 +00001033 Entry = SectionMap.size();
1034 }
1035 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001036 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001037 for (const Function &F : M) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001038 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
1039 if (F.hasSection()) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001040 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001041 unsigned &Entry = SectionMap[F.getSection()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001042 if (!Entry) {
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00001043 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
Teresa Johnson37687f32016-04-23 04:30:47 +00001044 0 /*TODO*/);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001045 Entry = SectionMap.size();
1046 }
1047 }
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001048 if (F.hasGC()) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001049 // Same for GC names.
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001050 unsigned &Entry = GCMap[F.getGC()];
Gordon Henriksen71183b62007-12-10 03:18:06 +00001051 if (!Entry) {
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00001052 writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
1053 0 /*TODO*/);
Gordon Henriksend930f912008-08-17 18:44:35 +00001054 Entry = GCMap.size();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001055 }
1056 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001057 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001058
Chris Lattner4b00d922007-04-23 16:04:05 +00001059 // Emit abbrev for globals, now that we know # sections and max alignment.
1060 unsigned SimpleGVarAbbrev = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001061 if (!M.global_empty()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001062 // Add an abbrev for common globals with no visibility or thread localness.
David Blaikie7ad9dc12017-01-04 22:36:33 +00001063 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattner4b00d922007-04-23 16:04:05 +00001064 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001065 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1066 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Chris Lattner2eae59f2007-05-04 20:34:50 +00001067 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001068 Log2_32_Ceil(MaxGlobalType+1)));
David Blaikie1a848da2015-04-27 19:58:56 +00001069 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1070 //| explicitType << 1
1071 //| constant
1072 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1073 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1074 if (MaxAlignment == 0) // Alignment.
Chris Lattner4b00d922007-04-23 16:04:05 +00001075 Abbv->Add(BitCodeAbbrevOp(0));
1076 else {
1077 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2eae59f2007-05-04 20:34:50 +00001078 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +00001079 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001080 }
1081 if (SectionMap.empty()) // Section.
1082 Abbv->Add(BitCodeAbbrevOp(0));
1083 else
Chris Lattner2eae59f2007-05-04 20:34:50 +00001084 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner1e50c292007-04-24 03:29:47 +00001085 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +00001086 // Don't bother emitting vis + thread local.
David Blaikie7ad9dc12017-01-04 22:36:33 +00001087 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Chris Lattner4b00d922007-04-23 16:04:05 +00001088 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001089
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001090 SmallVector<unsigned, 64> Vals;
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001091 // Emit the module's source file name.
1092 {
David Blaikieb6b42e02017-06-02 17:24:26 +00001093 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001094 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1095 if (Bits == SE_Char6)
1096 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1097 else if (Bits == SE_Fixed7)
1098 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1099
1100 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1101 auto Abbv = std::make_shared<BitCodeAbbrev>();
1102 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1103 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1104 Abbv->Add(AbbrevOpToUse);
1105 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1106
1107 for (const auto P : M.getSourceFileName())
1108 Vals.push_back((unsigned char)P);
1109
1110 // Emit the finished record.
1111 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1112 Vals.clear();
1113 }
1114
1115 // Emit the global variable information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001116 for (const GlobalVariable &GV : M.globals()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001117 unsigned AbbrevToUse = 0;
1118
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001119 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001120 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +00001121 // unnamed_addr, externally_initialized, dllstorageclass,
Javed Absarf3d79042017-05-11 12:28:08 +00001122 // comdat, attributes]
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001123 Vals.push_back(StrtabBuilder.add(GV.getName()));
1124 Vals.push_back(GV.getName().size());
David Blaikie1a848da2015-04-27 19:58:56 +00001125 Vals.push_back(VE.getTypeID(GV.getValueType()));
1126 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001127 Vals.push_back(GV.isDeclaration() ? 0 :
1128 (VE.getValueID(GV.getInitializer()) + 1));
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001129 Vals.push_back(getEncodedLinkage(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001130 Vals.push_back(Log2_32(GV.getAlignment())+1);
1131 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
1132 if (GV.isThreadLocal() ||
1133 GV.getVisibility() != GlobalValue::DefaultVisibility ||
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001134 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1135 GV.isExternallyInitialized() ||
David Majnemerdad0a642014-06-27 18:19:56 +00001136 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
Javed Absarf3d79042017-05-11 12:28:08 +00001137 GV.hasComdat() ||
1138 GV.hasAttributes()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001139 Vals.push_back(getEncodedVisibility(GV));
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001140 Vals.push_back(getEncodedThreadLocalMode(GV));
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001141 Vals.push_back(getEncodedUnnamedAddr(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001142 Vals.push_back(GV.isExternallyInitialized());
Nico Rieck7157bb72014-01-14 15:22:47 +00001143 Vals.push_back(getEncodedDLLStorageClass(GV));
David Majnemerdad0a642014-06-27 18:19:56 +00001144 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
Javed Absarf3d79042017-05-11 12:28:08 +00001145
1146 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1147 Vals.push_back(VE.getAttributeListID(AL));
Chris Lattner4b00d922007-04-23 16:04:05 +00001148 } else {
1149 AbbrevToUse = SimpleGVarAbbrev;
1150 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001151
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001152 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001153 Vals.clear();
1154 }
1155
1156 // Emit the function proto information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001157 for (const Function &F : M) {
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001158 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1159 // linkage, paramattrs, alignment, section, visibility, gc,
1160 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1161 // prefixdata, personalityfn]
1162 Vals.push_back(StrtabBuilder.add(F.getName()));
1163 Vals.push_back(F.getName().size());
David Blaikie561a1572015-04-17 16:28:26 +00001164 Vals.push_back(VE.getTypeID(F.getFunctionType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001165 Vals.push_back(F.getCallingConv());
1166 Vals.push_back(F.isDeclaration());
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001167 Vals.push_back(getEncodedLinkage(F));
Reid Klecknerb4a2d182017-04-24 20:38:30 +00001168 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001169 Vals.push_back(Log2_32(F.getAlignment())+1);
1170 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001171 Vals.push_back(getEncodedVisibility(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001172 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001173 Vals.push_back(getEncodedUnnamedAddr(F));
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001174 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1175 : 0);
Nico Rieck7157bb72014-01-14 15:22:47 +00001176 Vals.push_back(getEncodedDLLStorageClass(F));
David Majnemerdad0a642014-06-27 18:19:56 +00001177 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001178 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1179 : 0);
David Majnemer7fddecc2015-06-17 20:52:32 +00001180 Vals.push_back(
1181 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001182
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001183 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001184 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001185 Vals.clear();
1186 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001187
Chris Lattner44c17072007-04-26 02:46:40 +00001188 // Emit the alias information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001189 for (const GlobalAlias &A : M.aliases()) {
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001190 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1191 // visibility, dllstorageclass, threadlocal, unnamed_addr]
1192 Vals.push_back(StrtabBuilder.add(A.getName()));
1193 Vals.push_back(A.getName().size());
David Blaikie6a51dbd2015-09-17 22:18:59 +00001194 Vals.push_back(VE.getTypeID(A.getValueType()));
1195 Vals.push_back(A.getType()->getAddressSpace());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001196 Vals.push_back(VE.getValueID(A.getAliasee()));
1197 Vals.push_back(getEncodedLinkage(A));
1198 Vals.push_back(getEncodedVisibility(A));
1199 Vals.push_back(getEncodedDLLStorageClass(A));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001200 Vals.push_back(getEncodedThreadLocalMode(A));
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001201 Vals.push_back(getEncodedUnnamedAddr(A));
Chris Lattner44c17072007-04-26 02:46:40 +00001202 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001203 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
Chris Lattner44c17072007-04-26 02:46:40 +00001204 Vals.clear();
1205 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001206
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001207 // Emit the ifunc information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001208 for (const GlobalIFunc &I : M.ifuncs()) {
Peter Collingbournea0f371a2017-04-17 17:51:36 +00001209 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1210 // val#, linkage, visibility]
1211 Vals.push_back(StrtabBuilder.add(I.getName()));
1212 Vals.push_back(I.getName().size());
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001213 Vals.push_back(VE.getTypeID(I.getValueType()));
1214 Vals.push_back(I.getType()->getAddressSpace());
1215 Vals.push_back(VE.getValueID(I.getResolver()));
1216 Vals.push_back(getEncodedLinkage(I));
1217 Vals.push_back(getEncodedVisibility(I));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001218 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001219 Vals.clear();
1220 }
1221
Teresa Johnson37687f32016-04-23 04:30:47 +00001222 writeValueSymbolTableForwardDecl();
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001223}
1224
Teresa Johnson37687f32016-04-23 04:30:47 +00001225static uint64_t getOptimizationFlags(const Value *V) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001226 uint64_t Flags = 0;
1227
Sanjay Patelc00017d2014-10-15 17:45:13 +00001228 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001229 if (OBO->hasNoSignedWrap())
1230 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1231 if (OBO->hasNoUnsignedWrap())
1232 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001233 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
Chris Lattner35315d02011-02-06 21:44:57 +00001234 if (PEO->isExact())
1235 Flags |= 1 << bitc::PEO_EXACT;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001236 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001237 if (FPMO->hasUnsafeAlgebra())
Michael Ilseman65f14352012-12-09 21:12:04 +00001238 Flags |= FastMathFlags::UnsafeAlgebra;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001239 if (FPMO->hasNoNaNs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001240 Flags |= FastMathFlags::NoNaNs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001241 if (FPMO->hasNoInfs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001242 Flags |= FastMathFlags::NoInfs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001243 if (FPMO->hasNoSignedZeros())
Michael Ilseman65f14352012-12-09 21:12:04 +00001244 Flags |= FastMathFlags::NoSignedZeros;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001245 if (FPMO->hasAllowReciprocal())
Michael Ilseman65f14352012-12-09 21:12:04 +00001246 Flags |= FastMathFlags::AllowReciprocal;
Adam Nemetcd847a82017-03-28 20:11:52 +00001247 if (FPMO->hasAllowContract())
1248 Flags |= FastMathFlags::AllowContract;
Dan Gohman0ebd6962009-07-20 21:19:07 +00001249 }
1250
1251 return Flags;
1252}
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001253
Teresa Johnson37687f32016-04-23 04:30:47 +00001254void ModuleBitcodeWriter::writeValueAsMetadata(
1255 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001256 // Mimic an MDNode with a value as one operand.
1257 Value *V = MD->getValue();
1258 Record.push_back(VE.getTypeID(V->getType()));
1259 Record.push_back(VE.getValueID(V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001260 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001261 Record.clear();
1262}
1263
Teresa Johnson37687f32016-04-23 04:30:47 +00001264void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1265 SmallVectorImpl<uint64_t> &Record,
1266 unsigned Abbrev) {
Chris Lattner9b493022009-12-31 01:22:29 +00001267 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001268 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001269 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1270 "Unexpected function-local metadata");
1271 Record.push_back(VE.getMetadataOrNullID(MD));
Devang Patel8cca7b42009-08-04 05:01:35 +00001272 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001273 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1274 : bitc::METADATA_NODE,
1275 Record, Abbrev);
Devang Patel8cca7b42009-08-04 05:01:35 +00001276 Record.clear();
1277}
Devang Patele059ba6e2009-07-23 01:07:34 +00001278
Teresa Johnson37687f32016-04-23 04:30:47 +00001279unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001280 // Assume the column is usually under 128, and always output the inlined-at
1281 // location (it's never more expensive than building an array size 1).
David Blaikie7ad9dc12017-01-04 22:36:33 +00001282 auto Abbv = std::make_shared<BitCodeAbbrev>();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001283 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1284 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1285 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1286 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1287 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1288 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
David Blaikie7ad9dc12017-01-04 22:36:33 +00001289 return Stream.EmitAbbrev(std::move(Abbv));
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001290}
1291
Teresa Johnson37687f32016-04-23 04:30:47 +00001292void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1293 SmallVectorImpl<uint64_t> &Record,
1294 unsigned &Abbrev) {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001295 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001296 Abbrev = createDILocationAbbrev();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001297
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001298 Record.push_back(N->isDistinct());
1299 Record.push_back(N->getLine());
1300 Record.push_back(N->getColumn());
1301 Record.push_back(VE.getMetadataID(N->getScope()));
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001302 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001303
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001304 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001305 Record.clear();
1306}
1307
Teresa Johnson37687f32016-04-23 04:30:47 +00001308unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001309 // Assume the column is usually under 128, and always output the inlined-at
1310 // location (it's never more expensive than building an array size 1).
David Blaikie7ad9dc12017-01-04 22:36:33 +00001311 auto Abbv = std::make_shared<BitCodeAbbrev>();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001312 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1313 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1314 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1315 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1316 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1317 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1318 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
David Blaikie7ad9dc12017-01-04 22:36:33 +00001319 return Stream.EmitAbbrev(std::move(Abbv));
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001320}
1321
Teresa Johnson37687f32016-04-23 04:30:47 +00001322void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1323 SmallVectorImpl<uint64_t> &Record,
1324 unsigned &Abbrev) {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001325 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001326 Abbrev = createGenericDINodeAbbrev();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001327
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001328 Record.push_back(N->isDistinct());
1329 Record.push_back(N->getTag());
1330 Record.push_back(0); // Per-tag version field; unused for now.
1331
1332 for (auto &I : N->operands())
1333 Record.push_back(VE.getMetadataOrNullID(I));
1334
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001335 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001336 Record.clear();
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001337}
1338
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001339static uint64_t rotateSign(int64_t I) {
1340 uint64_t U = I;
1341 return I < 0 ? ~(U << 1) : U << 1;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001342}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001343
Teresa Johnson37687f32016-04-23 04:30:47 +00001344void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1345 SmallVectorImpl<uint64_t> &Record,
1346 unsigned Abbrev) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001347 Record.push_back(N->isDistinct());
1348 Record.push_back(N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001349 Record.push_back(rotateSign(N->getLowerBound()));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001350
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001351 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001352 Record.clear();
1353}
1354
Teresa Johnson37687f32016-04-23 04:30:47 +00001355void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1356 SmallVectorImpl<uint64_t> &Record,
1357 unsigned Abbrev) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001358 Record.push_back(N->isDistinct());
1359 Record.push_back(rotateSign(N->getValue()));
1360 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1361
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001362 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001363 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001364}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001365
Teresa Johnson37687f32016-04-23 04:30:47 +00001366void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1367 SmallVectorImpl<uint64_t> &Record,
1368 unsigned Abbrev) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001369 Record.push_back(N->isDistinct());
1370 Record.push_back(N->getTag());
1371 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1372 Record.push_back(N->getSizeInBits());
1373 Record.push_back(N->getAlignInBits());
1374 Record.push_back(N->getEncoding());
1375
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001376 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001377 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001378}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001379
Teresa Johnson37687f32016-04-23 04:30:47 +00001380void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1381 SmallVectorImpl<uint64_t> &Record,
1382 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001383 Record.push_back(N->isDistinct());
1384 Record.push_back(N->getTag());
1385 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1386 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1387 Record.push_back(N->getLine());
1388 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001389 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001390 Record.push_back(N->getSizeInBits());
1391 Record.push_back(N->getAlignInBits());
1392 Record.push_back(N->getOffsetInBits());
1393 Record.push_back(N->getFlags());
1394 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1395
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001396 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1397 // that there is no DWARF address space associated with DIDerivedType.
1398 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1399 Record.push_back(*DWARFAddressSpace + 1);
1400 else
1401 Record.push_back(0);
1402
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001403 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001404 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001405}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001406
Teresa Johnson37687f32016-04-23 04:30:47 +00001407void ModuleBitcodeWriter::writeDICompositeType(
1408 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1409 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001410 const unsigned IsNotUsedInOldTypeRef = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001411 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001412 Record.push_back(N->getTag());
1413 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1414 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1415 Record.push_back(N->getLine());
1416 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1417 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1418 Record.push_back(N->getSizeInBits());
1419 Record.push_back(N->getAlignInBits());
1420 Record.push_back(N->getOffsetInBits());
1421 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001422 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001423 Record.push_back(N->getRuntimeLang());
1424 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001425 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001426 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1427
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001428 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001429 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001430}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001431
Teresa Johnson37687f32016-04-23 04:30:47 +00001432void ModuleBitcodeWriter::writeDISubroutineType(
1433 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1434 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001435 const unsigned HasNoOldTypeRefs = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001436 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001437 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001438 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001439 Record.push_back(N->getCC());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001440
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001441 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001442 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001443}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001444
Teresa Johnson37687f32016-04-23 04:30:47 +00001445void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1446 SmallVectorImpl<uint64_t> &Record,
1447 unsigned Abbrev) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001448 Record.push_back(N->isDistinct());
1449 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1450 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001451 Record.push_back(N->getChecksumKind());
1452 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()));
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001453
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001454 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001455 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001456}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001457
Teresa Johnson37687f32016-04-23 04:30:47 +00001458void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1459 SmallVectorImpl<uint64_t> &Record,
1460 unsigned Abbrev) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001461 assert(N->isDistinct() && "Expected distinct compile units");
1462 Record.push_back(/* IsDistinct */ true);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001463 Record.push_back(N->getSourceLanguage());
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001464 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001465 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1466 Record.push_back(N->isOptimized());
1467 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1468 Record.push_back(N->getRuntimeVersion());
1469 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1470 Record.push_back(N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001471 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1472 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001473 Record.push_back(/* subprograms */ 0);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001474 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1475 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
Adrian Prantl1f599f92015-05-21 20:37:30 +00001476 Record.push_back(N->getDWOId());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001477 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
David Blaikiea01f2952016-08-24 18:29:49 +00001478 Record.push_back(N->getSplitDebugInlining());
Dehao Chen0944a8c2017-02-01 22:45:09 +00001479 Record.push_back(N->getDebugInfoForProfiling());
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001480
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001481 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001482 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001483}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001484
Teresa Johnson37687f32016-04-23 04:30:47 +00001485void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1486 SmallVectorImpl<uint64_t> &Record,
1487 unsigned Abbrev) {
Adrian Prantl85338cb2016-05-06 22:53:06 +00001488 uint64_t HasUnitFlag = 1 << 1;
1489 Record.push_back(N->isDistinct() | HasUnitFlag);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001490 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1491 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1492 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1493 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1494 Record.push_back(N->getLine());
1495 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1496 Record.push_back(N->isLocalToUnit());
1497 Record.push_back(N->isDefinition());
1498 Record.push_back(N->getScopeLine());
1499 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1500 Record.push_back(N->getVirtuality());
1501 Record.push_back(N->getVirtualIndex());
1502 Record.push_back(N->getFlags());
1503 Record.push_back(N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001504 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001505 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001506 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001507 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001508 Record.push_back(N->getThisAdjustment());
Adrian Prantl1d12b882017-04-26 22:56:44 +00001509 Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001510
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001511 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001512 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001513}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001514
Teresa Johnson37687f32016-04-23 04:30:47 +00001515void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1516 SmallVectorImpl<uint64_t> &Record,
1517 unsigned Abbrev) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001518 Record.push_back(N->isDistinct());
1519 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1520 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1521 Record.push_back(N->getLine());
1522 Record.push_back(N->getColumn());
1523
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001524 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001525 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001526}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001527
Teresa Johnson37687f32016-04-23 04:30:47 +00001528void ModuleBitcodeWriter::writeDILexicalBlockFile(
1529 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1530 unsigned Abbrev) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001531 Record.push_back(N->isDistinct());
1532 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1533 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1534 Record.push_back(N->getDiscriminator());
1535
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001536 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001537 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001538}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001539
Teresa Johnson37687f32016-04-23 04:30:47 +00001540void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1541 SmallVectorImpl<uint64_t> &Record,
1542 unsigned Abbrev) {
Adrian Prantldbfda632016-11-03 19:42:02 +00001543 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001544 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001545 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001546
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001547 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001548 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001549}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001550
Teresa Johnson37687f32016-04-23 04:30:47 +00001551void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1552 SmallVectorImpl<uint64_t> &Record,
1553 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001554 Record.push_back(N->isDistinct());
1555 Record.push_back(N->getMacinfoType());
1556 Record.push_back(N->getLine());
1557 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1558 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1559
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001560 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001561 Record.clear();
1562}
1563
Teresa Johnson37687f32016-04-23 04:30:47 +00001564void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1565 SmallVectorImpl<uint64_t> &Record,
1566 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001567 Record.push_back(N->isDistinct());
1568 Record.push_back(N->getMacinfoType());
1569 Record.push_back(N->getLine());
1570 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1571 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1572
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001573 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001574 Record.clear();
1575}
1576
Teresa Johnson37687f32016-04-23 04:30:47 +00001577void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1578 SmallVectorImpl<uint64_t> &Record,
1579 unsigned Abbrev) {
Adrian Prantlab1243f2015-06-29 23:03:47 +00001580 Record.push_back(N->isDistinct());
1581 for (auto &I : N->operands())
1582 Record.push_back(VE.getMetadataOrNullID(I));
1583
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001584 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
Adrian Prantlab1243f2015-06-29 23:03:47 +00001585 Record.clear();
1586}
1587
Teresa Johnson37687f32016-04-23 04:30:47 +00001588void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1589 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1590 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001591 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001592 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1593 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1594
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001595 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001596 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001597}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001598
Teresa Johnson37687f32016-04-23 04:30:47 +00001599void ModuleBitcodeWriter::writeDITemplateValueParameter(
1600 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1601 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001602 Record.push_back(N->isDistinct());
1603 Record.push_back(N->getTag());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001604 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1605 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1606 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1607
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001608 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001609 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001610}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001611
Teresa Johnson37687f32016-04-23 04:30:47 +00001612void ModuleBitcodeWriter::writeDIGlobalVariable(
1613 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1614 unsigned Abbrev) {
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001615 const uint64_t Version = 1 << 1;
1616 Record.push_back((uint64_t)N->isDistinct() | Version);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001617 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1618 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1619 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1620 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1621 Record.push_back(N->getLine());
1622 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1623 Record.push_back(N->isLocalToUnit());
1624 Record.push_back(N->isDefinition());
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001625 Record.push_back(/* expr */ 0);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001626 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
Victor Leschuk2ede1262016-10-20 00:13:12 +00001627 Record.push_back(N->getAlignInBits());
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001628
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001629 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001630 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001631}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001632
Teresa Johnson37687f32016-04-23 04:30:47 +00001633void ModuleBitcodeWriter::writeDILocalVariable(
1634 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1635 unsigned Abbrev) {
Victor Leschuk2ede1262016-10-20 00:13:12 +00001636 // In order to support all possible bitcode formats in BitcodeReader we need
Simon Pilgrim25059362016-10-20 10:42:14 +00001637 // to distinguish the following cases:
Victor Leschuk2ede1262016-10-20 00:13:12 +00001638 // 1) Record has no artificial tag (Record[1]),
1639 // has no obsolete inlinedAt field (Record[9]).
1640 // In this case Record size will be 8, HasAlignment flag is false.
1641 // 2) Record has artificial tag (Record[1]),
1642 // has no obsolete inlignedAt field (Record[9]).
1643 // In this case Record size will be 9, HasAlignment flag is false.
1644 // 3) Record has both artificial tag (Record[1]) and
1645 // obsolete inlignedAt field (Record[9]).
1646 // In this case Record size will be 10, HasAlignment flag is false.
1647 // 4) Record has neither artificial tag, nor inlignedAt field, but
1648 // HasAlignment flag is true and Record[8] contains alignment value.
1649 const uint64_t HasAlignmentFlag = 1 << 1;
Simon Pilgrim071da462016-10-20 10:37:58 +00001650 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001651 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1652 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1653 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1654 Record.push_back(N->getLine());
1655 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1656 Record.push_back(N->getArg());
1657 Record.push_back(N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00001658 Record.push_back(N->getAlignInBits());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001659
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001660 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001661 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001662}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001663
Teresa Johnson37687f32016-04-23 04:30:47 +00001664void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1665 SmallVectorImpl<uint64_t> &Record,
1666 unsigned Abbrev) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001667 Record.reserve(N->getElements().size() + 1);
Florian Hahnffc498d2017-06-14 13:14:38 +00001668 const uint64_t Version = 3 << 1;
Adrian Prantl6825fb62017-04-18 01:21:53 +00001669 Record.push_back((uint64_t)N->isDistinct() | Version);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001670 Record.append(N->elements_begin(), N->elements_end());
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001671
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001672 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001673 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001674}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001675
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001676void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
1677 const DIGlobalVariableExpression *N, SmallVectorImpl<uint64_t> &Record,
1678 unsigned Abbrev) {
1679 Record.push_back(N->isDistinct());
1680 Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
1681 Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
1682
1683 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR_EXPR, Record, Abbrev);
1684 Record.clear();
1685}
1686
Teresa Johnson37687f32016-04-23 04:30:47 +00001687void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1688 SmallVectorImpl<uint64_t> &Record,
1689 unsigned Abbrev) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001690 Record.push_back(N->isDistinct());
1691 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1692 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1693 Record.push_back(N->getLine());
1694 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1695 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1696 Record.push_back(N->getAttributes());
1697 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1698
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001699 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001700 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001701}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001702
Teresa Johnson37687f32016-04-23 04:30:47 +00001703void ModuleBitcodeWriter::writeDIImportedEntity(
1704 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1705 unsigned Abbrev) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001706 Record.push_back(N->isDistinct());
1707 Record.push_back(N->getTag());
1708 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1709 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1710 Record.push_back(N->getLine());
1711 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1712
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001713 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001714 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001715}
1716
Teresa Johnson37687f32016-04-23 04:30:47 +00001717unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
David Blaikie7ad9dc12017-01-04 22:36:33 +00001718 auto Abbv = std::make_shared<BitCodeAbbrev>();
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001719 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1720 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1721 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00001722 return Stream.EmitAbbrev(std::move(Abbv));
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001723}
1724
Teresa Johnson37687f32016-04-23 04:30:47 +00001725void ModuleBitcodeWriter::writeNamedMetadata(
1726 SmallVectorImpl<uint64_t> &Record) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001727 if (M.named_metadata_empty())
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001728 return;
1729
Teresa Johnson37687f32016-04-23 04:30:47 +00001730 unsigned Abbrev = createNamedMetadataAbbrev();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001731 for (const NamedMDNode &NMD : M.named_metadata()) {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001732 // Write name.
1733 StringRef Str = NMD.getName();
1734 Record.append(Str.bytes_begin(), Str.bytes_end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001735 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001736 Record.clear();
1737
1738 // Write named metadata operands.
1739 for (const MDNode *N : NMD.operands())
1740 Record.push_back(VE.getMetadataID(N));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001741 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001742 Record.clear();
1743 }
1744}
1745
Teresa Johnson37687f32016-04-23 04:30:47 +00001746unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
David Blaikie7ad9dc12017-01-04 22:36:33 +00001747 auto Abbv = std::make_shared<BitCodeAbbrev>();
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001748 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1749 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1750 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1751 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikie7ad9dc12017-01-04 22:36:33 +00001752 return Stream.EmitAbbrev(std::move(Abbv));
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001753}
1754
1755/// Write out a record for MDString.
1756///
1757/// All the metadata strings in a metadata block are emitted in a single
1758/// record. The sizes and strings themselves are shoved into a blob.
Teresa Johnson37687f32016-04-23 04:30:47 +00001759void ModuleBitcodeWriter::writeMetadataStrings(
1760 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001761 if (Strings.empty())
1762 return;
1763
1764 // Start the record with the number of strings.
1765 Record.push_back(bitc::METADATA_STRINGS);
1766 Record.push_back(Strings.size());
1767
1768 // Emit the sizes of the strings in the blob.
1769 SmallString<256> Blob;
1770 {
1771 BitstreamWriter W(Blob);
1772 for (const Metadata *MD : Strings)
1773 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1774 W.FlushToWord();
1775 }
1776
1777 // Add the offset to the strings to the record.
1778 Record.push_back(Blob.size());
1779
1780 // Add the strings to the blob.
1781 for (const Metadata *MD : Strings)
1782 Blob.append(cast<MDString>(MD)->getString());
1783
1784 // Emit the final record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001785 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001786 Record.clear();
1787}
1788
Mehdi Aminie98f9252016-12-28 22:30:28 +00001789// Generates an enum to use as an index in the Abbrev array of Metadata record.
1790enum MetadataAbbrev : unsigned {
1791#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
1792#include "llvm/IR/Metadata.def"
1793 LastPlusOne
1794};
1795
Teresa Johnson37687f32016-04-23 04:30:47 +00001796void ModuleBitcodeWriter::writeMetadataRecords(
Mehdi Aminie98f9252016-12-28 22:30:28 +00001797 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record,
1798 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001799 if (MDs.empty())
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +00001800 return;
1801
Duncan P. N. Exon Smith6b7b6802015-02-04 21:54:12 +00001802 // Initialize MDNode abbreviations.
1803#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1804#include "llvm/IR/Metadata.def"
1805
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001806 for (const Metadata *MD : MDs) {
Mehdi Aminie98f9252016-12-28 22:30:28 +00001807 if (IndexPos)
1808 IndexPos->push_back(Stream.GetCurrentBitNo());
Duncan P. N. Exon Smith73d5aae2015-01-12 22:31:35 +00001809 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith79f8d112015-03-17 00:16:35 +00001810 assert(N->isResolved() && "Expected forward references to be resolved");
1811
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001812 switch (N->getMetadataID()) {
1813 default:
1814 llvm_unreachable("Invalid MDNode subclass");
1815#define HANDLE_MDNODE_LEAF(CLASS) \
1816 case Metadata::CLASS##Kind: \
Mehdi Aminie98f9252016-12-28 22:30:28 +00001817 if (MDAbbrevs) \
1818 write##CLASS(cast<CLASS>(N), Record, \
1819 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
1820 else \
1821 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001822 continue;
1823#include "llvm/IR/Metadata.def"
1824 }
Devang Patel8cca7b42009-08-04 05:01:35 +00001825 }
Teresa Johnson37687f32016-04-23 04:30:47 +00001826 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
Devang Patel8cca7b42009-08-04 05:01:35 +00001827 }
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001828}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001829
Teresa Johnson37687f32016-04-23 04:30:47 +00001830void ModuleBitcodeWriter::writeModuleMetadata() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001831 if (!VE.hasMDs() && M.named_metadata_empty())
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001832 return;
1833
Mehdi Aminie98f9252016-12-28 22:30:28 +00001834 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4);
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001835 SmallVector<uint64_t, 64> Record;
Mehdi Aminie98f9252016-12-28 22:30:28 +00001836
1837 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
1838 // block and load any metadata.
1839 std::vector<unsigned> MDAbbrevs;
1840
1841 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
1842 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
1843 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
1844 createGenericDINodeAbbrev();
1845
David Blaikie7ad9dc12017-01-04 22:36:33 +00001846 auto Abbv = std::make_shared<BitCodeAbbrev>();
Mehdi Aminie98f9252016-12-28 22:30:28 +00001847 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX_OFFSET));
Mehdi Amini5022bb72016-12-28 23:45:54 +00001848 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1849 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
David Blaikie7ad9dc12017-01-04 22:36:33 +00001850 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Mehdi Aminie98f9252016-12-28 22:30:28 +00001851
David Blaikie7ad9dc12017-01-04 22:36:33 +00001852 Abbv = std::make_shared<BitCodeAbbrev>();
Mehdi Aminie98f9252016-12-28 22:30:28 +00001853 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX));
1854 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1855 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
David Blaikie7ad9dc12017-01-04 22:36:33 +00001856 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Mehdi Aminie98f9252016-12-28 22:30:28 +00001857
1858 // Emit MDStrings together upfront.
Teresa Johnson37687f32016-04-23 04:30:47 +00001859 writeMetadataStrings(VE.getMDStrings(), Record);
Mehdi Aminie98f9252016-12-28 22:30:28 +00001860
1861 // We only emit an index for the metadata record if we have more than a given
1862 // (naive) threshold of metadatas, otherwise it is not worth it.
1863 if (VE.getNonMDStrings().size() > IndexThreshold) {
1864 // Write a placeholder value in for the offset of the metadata index,
1865 // which is written after the records, so that it can include
1866 // the offset of each entry. The placeholder offset will be
1867 // updated after all records are emitted.
Mehdi Amini5022bb72016-12-28 23:45:54 +00001868 uint64_t Vals[] = {0, 0};
Mehdi Aminie98f9252016-12-28 22:30:28 +00001869 Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
1870 }
1871
1872 // Compute and save the bit offset to the current position, which will be
1873 // patched when we emit the index later. We can simply subtract the 64-bit
1874 // fixed size from the current bit number to get the location to backpatch.
1875 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
1876
1877 // This index will contain the bitpos for each individual record.
1878 std::vector<uint64_t> IndexPos;
1879 IndexPos.reserve(VE.getNonMDStrings().size());
1880
1881 // Write all the records
1882 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
1883
1884 if (VE.getNonMDStrings().size() > IndexThreshold) {
1885 // Now that we have emitted all the records we will emit the index. But
1886 // first
1887 // backpatch the forward reference so that the reader can skip the records
1888 // efficiently.
1889 Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
1890 Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
1891
1892 // Delta encode the index.
1893 uint64_t PreviousValue = IndexOffsetRecordBitPos;
1894 for (auto &Elt : IndexPos) {
1895 auto EltDelta = Elt - PreviousValue;
1896 PreviousValue = Elt;
1897 Elt = EltDelta;
1898 }
1899 // Emit the index record.
1900 Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
1901 IndexPos.clear();
1902 }
1903
1904 // Write the named metadata now.
Teresa Johnson37687f32016-04-23 04:30:47 +00001905 writeNamedMetadata(Record);
Peter Collingbourne21521892016-06-21 23:42:48 +00001906
1907 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
1908 SmallVector<uint64_t, 4> Record;
1909 Record.push_back(VE.getValueID(&GO));
1910 pushGlobalMetadataAttachment(Record, GO);
1911 Stream.EmitRecord(bitc::METADATA_GLOBAL_DECL_ATTACHMENT, Record);
1912 };
1913 for (const Function &F : M)
1914 if (F.isDeclaration() && F.hasMetadata())
1915 AddDeclAttachedMetadata(F);
1916 // FIXME: Only store metadata for declarations here, and move data for global
1917 // variable definitions to a separate block (PR28134).
1918 for (const GlobalVariable &GV : M.globals())
1919 if (GV.hasMetadata())
1920 AddDeclAttachedMetadata(GV);
1921
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001922 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001923}
1924
Teresa Johnson37687f32016-04-23 04:30:47 +00001925void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +00001926 if (!VE.hasMDs())
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001927 return;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001928
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001929 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001930 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001931 writeMetadataStrings(VE.getMDStrings(), Record);
1932 writeMetadataRecords(VE.getNonMDStrings(), Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001933 Stream.ExitBlock();
Victor Hernandezb00a6be2010-01-13 19:37:33 +00001934}
1935
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001936void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
1937 SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) {
1938 // [n x [id, mdnode]]
1939 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1940 GO.getAllMetadata(MDs);
1941 for (const auto &I : MDs) {
1942 Record.push_back(I.first);
1943 Record.push_back(VE.getMetadataID(I.second));
1944 }
1945}
1946
1947void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001948 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
Chris Lattner07d09ed2010-04-03 02:17:50 +00001949
Devang Patelaf206b82009-09-18 19:26:43 +00001950 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001951
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001952 if (F.hasMetadata()) {
1953 pushGlobalMetadataAttachment(Record, F);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001954 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001955 Record.clear();
1956 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001957
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001958 // Write metadata attachments
1959 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1960 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001961 for (const BasicBlock &BB : F)
1962 for (const Instruction &I : BB) {
Devang Patel6da5dbf2009-10-22 18:55:16 +00001963 MDs.clear();
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001964 I.getAllMetadataOtherThanDebugLoc(MDs);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001965
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001966 // If no metadata, ignore instruction.
1967 if (MDs.empty()) continue;
1968
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001969 Record.push_back(VE.getInstructionID(&I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001970
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001971 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1972 Record.push_back(MDs[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001973 Record.push_back(VE.getMetadataID(MDs[i].second));
Devang Patelaf206b82009-09-18 19:26:43 +00001974 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001975 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001976 Record.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00001977 }
1978
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001979 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001980}
1981
Peter Collingbourne21521892016-06-21 23:42:48 +00001982void ModuleBitcodeWriter::writeModuleMetadataKinds() {
Devang Patelaf206b82009-09-18 19:26:43 +00001983 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001984
Devang Patelaf206b82009-09-18 19:26:43 +00001985 // Write metadata kinds
1986 // METADATA_KIND - [n x [id, name]]
Michael Ilseman979dfbb2012-12-03 22:57:47 +00001987 SmallVector<StringRef, 8> Names;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001988 M.getMDKindNames(Names);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001989
Dan Gohman43aa8f02010-07-20 21:42:28 +00001990 if (Names.empty()) return;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001991
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001992 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001993
Dan Gohman43aa8f02010-07-20 21:42:28 +00001994 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattnerc9558df2009-12-28 20:10:43 +00001995 Record.push_back(MDKindID);
1996 StringRef KName = Names[MDKindID];
1997 Record.append(KName.begin(), KName.end());
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001998
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001999 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
Devang Patelaf206b82009-09-18 19:26:43 +00002000 Record.clear();
2001 }
2002
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002003 Stream.ExitBlock();
Devang Patel8cca7b42009-08-04 05:01:35 +00002004}
2005
Teresa Johnson37687f32016-04-23 04:30:47 +00002006void ModuleBitcodeWriter::writeOperandBundleTags() {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002007 // Write metadata kinds
2008 //
2009 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2010 //
2011 // OPERAND_BUNDLE_TAG - [strchr x N]
2012
2013 SmallVector<StringRef, 8> Tags;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002014 M.getOperandBundleTags(Tags);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002015
2016 if (Tags.empty())
2017 return;
2018
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002019 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002020
2021 SmallVector<uint64_t, 64> Record;
2022
2023 for (auto Tag : Tags) {
2024 Record.append(Tag.begin(), Tag.end());
2025
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002026 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002027 Record.clear();
2028 }
2029
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002030 Stream.ExitBlock();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002031}
2032
Jan Wen Voungafaced02012-10-11 20:20:40 +00002033static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
2034 if ((int64_t)V >= 0)
2035 Vals.push_back(V << 1);
2036 else
2037 Vals.push_back((-V << 1) | 1);
2038}
2039
Teresa Johnson37687f32016-04-23 04:30:47 +00002040void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2041 bool isGlobal) {
Devang Patel8cca7b42009-08-04 05:01:35 +00002042 if (FirstVal == LastVal) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002043
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002044 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Devang Patel8cca7b42009-08-04 05:01:35 +00002045
2046 unsigned AggregateAbbrev = 0;
2047 unsigned String8Abbrev = 0;
2048 unsigned CString7Abbrev = 0;
2049 unsigned CString6Abbrev = 0;
2050 // If this is a constant pool for the module, emit module-specific abbrevs.
2051 if (isGlobal) {
2052 // Abbrev for CST_CODE_AGGREGATE.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002053 auto Abbv = std::make_shared<BitCodeAbbrev>();
Devang Patel8cca7b42009-08-04 05:01:35 +00002054 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
2055 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2056 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
David Blaikie7ad9dc12017-01-04 22:36:33 +00002057 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Devang Patel8cca7b42009-08-04 05:01:35 +00002058
2059 // Abbrev for CST_CODE_STRING.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002060 Abbv = std::make_shared<BitCodeAbbrev>();
Devang Patel8cca7b42009-08-04 05:01:35 +00002061 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
2062 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2063 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00002064 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
Devang Patel8cca7b42009-08-04 05:01:35 +00002065 // Abbrev for CST_CODE_CSTRING.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002066 Abbv = std::make_shared<BitCodeAbbrev>();
Devang Patel8cca7b42009-08-04 05:01:35 +00002067 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2068 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2069 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
David Blaikie7ad9dc12017-01-04 22:36:33 +00002070 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
Devang Patel8cca7b42009-08-04 05:01:35 +00002071 // Abbrev for CST_CODE_CSTRING.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002072 Abbv = std::make_shared<BitCodeAbbrev>();
Devang Patel8cca7b42009-08-04 05:01:35 +00002073 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2074 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2075 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
David Blaikie7ad9dc12017-01-04 22:36:33 +00002076 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002077 }
2078
Devang Patel8cca7b42009-08-04 05:01:35 +00002079 SmallVector<uint64_t, 64> Record;
2080
2081 const ValueEnumerator::ValueList &Vals = VE.getValues();
Craig Topper2617dcc2014-04-15 06:32:26 +00002082 Type *LastTy = nullptr;
Devang Patel8cca7b42009-08-04 05:01:35 +00002083 for (unsigned i = FirstVal; i != LastVal; ++i) {
2084 const Value *V = Vals[i].first;
Chris Lattner52523562007-04-24 00:16:04 +00002085 // If we need to switch types, do so now.
2086 if (V->getType() != LastTy) {
2087 LastTy = V->getType();
2088 Record.push_back(VE.getTypeID(LastTy));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002089 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
2090 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner52523562007-04-24 00:16:04 +00002091 Record.clear();
2092 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002093
Chris Lattner52523562007-04-24 00:16:04 +00002094 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesenfd04c742009-10-13 20:46:56 +00002095 Record.push_back(unsigned(IA->hasSideEffects()) |
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002096 unsigned(IA->isAlignStack()) << 1 |
2097 unsigned(IA->getDialect()&1) << 2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002098
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002099 // Add the asm string.
2100 const std::string &AsmStr = IA->getAsmString();
2101 Record.push_back(AsmStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002102 Record.append(AsmStr.begin(), AsmStr.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002103
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002104 // Add the constraint string.
2105 const std::string &ConstraintStr = IA->getConstraintString();
2106 Record.push_back(ConstraintStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002107 Record.append(ConstraintStr.begin(), ConstraintStr.end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002108 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002109 Record.clear();
Chris Lattner52523562007-04-24 00:16:04 +00002110 continue;
2111 }
2112 const Constant *C = cast<Constant>(V);
2113 unsigned Code = -1U;
2114 unsigned AbbrevToUse = 0;
2115 if (C->isNullValue()) {
2116 Code = bitc::CST_CODE_NULL;
2117 } else if (isa<UndefValue>(C)) {
2118 Code = bitc::CST_CODE_UNDEF;
2119 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
Bob Wilsone4077362013-09-09 19:14:35 +00002120 if (IV->getBitWidth() <= 64) {
2121 uint64_t V = IV->getSExtValue();
2122 emitSignedInt64(Record, V);
2123 Code = bitc::CST_CODE_INTEGER;
2124 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2125 } else { // Wide integers, > 64 bits in size.
2126 // We have an arbitrary precision integer value to write whose
2127 // bit width is > 64. However, in canonical unsigned integer
2128 // format it is likely that the high bits are going to be zero.
2129 // So, we only write the number of active words.
2130 unsigned NWords = IV->getValue().getActiveWords();
2131 const uint64_t *RawWords = IV->getValue().getRawData();
2132 for (unsigned i = 0; i != NWords; ++i) {
2133 emitSignedInt64(Record, RawWords[i]);
2134 }
2135 Code = bitc::CST_CODE_WIDE_INTEGER;
2136 }
Chris Lattner52523562007-04-24 00:16:04 +00002137 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2138 Code = bitc::CST_CODE_FLOAT;
Chris Lattner229907c2011-07-18 04:54:35 +00002139 Type *Ty = CFP->getType();
Dan Gohman518cda42011-12-17 00:04:22 +00002140 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002141 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +00002142 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002143 // api needed to prevent premature destruction
Dale Johannesen93eefa02009-03-23 21:16:53 +00002144 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen54306fe2008-10-09 18:53:47 +00002145 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002146 const uint64_t *p = api.getRawData();
Dale Johannesen93eefa02009-03-23 21:16:53 +00002147 Record.push_back((p[1] << 48) | (p[0] >> 16));
2148 Record.push_back(p[0] & 0xffffLL);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002149 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002150 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00002151 const uint64_t *p = api.getRawData();
Dale Johannesen245dceb2007-09-11 18:32:33 +00002152 Record.push_back(p[0]);
2153 Record.push_back(p[1]);
Dale Johannesenbdad8092007-08-09 22:51:36 +00002154 } else {
2155 assert (0 && "Unknown FP type!");
Chris Lattner52523562007-04-24 00:16:04 +00002156 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00002157 } else if (isa<ConstantDataSequential>(C) &&
2158 cast<ConstantDataSequential>(C)->isString()) {
2159 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2160 // Emit constant strings specially.
2161 unsigned NumElts = Str->getNumElements();
2162 // If this is a null-terminated string, use the denser CSTRING encoding.
2163 if (Str->isCString()) {
2164 Code = bitc::CST_CODE_CSTRING;
2165 --NumElts; // Don't encode the null, which isn't allowed by char6.
2166 } else {
2167 Code = bitc::CST_CODE_STRING;
2168 AbbrevToUse = String8Abbrev;
2169 }
2170 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2171 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2172 for (unsigned i = 0; i != NumElts; ++i) {
2173 unsigned char V = Str->getElementAsInteger(i);
2174 Record.push_back(V);
2175 isCStr7 &= (V & 128) == 0;
2176 if (isCStrChar6)
2177 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2178 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002179
Chris Lattner372dd1e2012-01-30 00:51:16 +00002180 if (isCStrChar6)
2181 AbbrevToUse = CString6Abbrev;
2182 else if (isCStr7)
2183 AbbrevToUse = CString7Abbrev;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002184 } else if (const ConstantDataSequential *CDS =
Chris Lattner372dd1e2012-01-30 00:51:16 +00002185 dyn_cast<ConstantDataSequential>(C)) {
Chris Lattner5d917072012-01-30 07:36:01 +00002186 Code = bitc::CST_CODE_DATA;
Chris Lattner372dd1e2012-01-30 00:51:16 +00002187 Type *EltTy = CDS->getType()->getElementType();
2188 if (isa<IntegerType>(EltTy)) {
2189 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2190 Record.push_back(CDS->getElementAsInteger(i));
Chris Lattner372dd1e2012-01-30 00:51:16 +00002191 } else {
Justin Bognera43eacb2016-01-06 22:31:32 +00002192 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2193 Record.push_back(
2194 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
Chris Lattner372dd1e2012-01-30 00:51:16 +00002195 }
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00002196 } else if (isa<ConstantAggregate>(C)) {
Chris Lattner52523562007-04-24 00:16:04 +00002197 Code = bitc::CST_CODE_AGGREGATE;
Pete Cooper125ad172015-06-25 20:51:38 +00002198 for (const Value *Op : C->operands())
2199 Record.push_back(VE.getValueID(Op));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002200 AbbrevToUse = AggregateAbbrev;
Chris Lattner52523562007-04-24 00:16:04 +00002201 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002202 switch (CE->getOpcode()) {
2203 default:
2204 if (Instruction::isCast(CE->getOpcode())) {
2205 Code = bitc::CST_CODE_CE_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002206 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002207 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2208 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002209 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002210 } else {
2211 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2212 Code = bitc::CST_CODE_CE_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002213 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002214 Record.push_back(VE.getValueID(C->getOperand(0)));
2215 Record.push_back(VE.getValueID(C->getOperand(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002216 uint64_t Flags = getOptimizationFlags(CE);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002217 if (Flags != 0)
2218 Record.push_back(Flags);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002219 }
2220 break;
David Blaikieb9263572015-03-13 21:03:36 +00002221 case Instruction::GetElementPtr: {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002222 Code = bitc::CST_CODE_CE_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00002223 const auto *GO = cast<GEPOperator>(C);
David Blaikieb9263572015-03-13 21:03:36 +00002224 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
Peter Collingbourned93620b2016-11-10 22:34:55 +00002225 if (Optional<unsigned> Idx = GO->getInRangeIndex()) {
2226 Code = bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX;
2227 Record.push_back((*Idx << 1) | GO->isInBounds());
2228 } else if (GO->isInBounds())
2229 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002230 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2231 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2232 Record.push_back(VE.getValueID(C->getOperand(i)));
2233 }
2234 break;
David Blaikieb9263572015-03-13 21:03:36 +00002235 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002236 case Instruction::Select:
2237 Code = bitc::CST_CODE_CE_SELECT;
2238 Record.push_back(VE.getValueID(C->getOperand(0)));
2239 Record.push_back(VE.getValueID(C->getOperand(1)));
2240 Record.push_back(VE.getValueID(C->getOperand(2)));
2241 break;
2242 case Instruction::ExtractElement:
2243 Code = bitc::CST_CODE_CE_EXTRACTELT;
2244 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2245 Record.push_back(VE.getValueID(C->getOperand(0)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002246 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002247 Record.push_back(VE.getValueID(C->getOperand(1)));
2248 break;
2249 case Instruction::InsertElement:
2250 Code = bitc::CST_CODE_CE_INSERTELT;
2251 Record.push_back(VE.getValueID(C->getOperand(0)));
2252 Record.push_back(VE.getValueID(C->getOperand(1)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002253 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002254 Record.push_back(VE.getValueID(C->getOperand(2)));
2255 break;
2256 case Instruction::ShuffleVector:
Nate Begeman94aa38d2009-02-12 21:28:33 +00002257 // If the return type and argument types are the same, this is a
2258 // standard shufflevector instruction. If the types are different,
2259 // then the shuffle is widening or truncating the input vectors, and
2260 // the argument type must also be encoded.
2261 if (C->getType() == C->getOperand(0)->getType()) {
2262 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2263 } else {
2264 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2265 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2266 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002267 Record.push_back(VE.getValueID(C->getOperand(0)));
2268 Record.push_back(VE.getValueID(C->getOperand(1)));
2269 Record.push_back(VE.getValueID(C->getOperand(2)));
2270 break;
2271 case Instruction::ICmp:
2272 case Instruction::FCmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002273 Code = bitc::CST_CODE_CE_CMP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002274 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2275 Record.push_back(VE.getValueID(C->getOperand(0)));
2276 Record.push_back(VE.getValueID(C->getOperand(1)));
2277 Record.push_back(CE->getPredicate());
2278 break;
2279 }
Chris Lattnerf540d742009-10-28 05:24:40 +00002280 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerf540d742009-10-28 05:24:40 +00002281 Code = bitc::CST_CODE_BLOCKADDRESS;
2282 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2283 Record.push_back(VE.getValueID(BA->getFunction()));
2284 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner52523562007-04-24 00:16:04 +00002285 } else {
Dan Gohman47dc8fd2010-07-21 21:18:37 +00002286#ifndef NDEBUG
2287 C->dump();
2288#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002289 llvm_unreachable("Unknown constant!");
Chris Lattner52523562007-04-24 00:16:04 +00002290 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002291 Stream.EmitRecord(Code, Record, AbbrevToUse);
Chris Lattner52523562007-04-24 00:16:04 +00002292 Record.clear();
2293 }
2294
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002295 Stream.ExitBlock();
Chris Lattner52523562007-04-24 00:16:04 +00002296}
2297
Teresa Johnson37687f32016-04-23 04:30:47 +00002298void ModuleBitcodeWriter::writeModuleConstants() {
Chris Lattner52523562007-04-24 00:16:04 +00002299 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002300
Chris Lattner52523562007-04-24 00:16:04 +00002301 // Find the first constant to emit, which is the first non-globalvalue value.
2302 // We know globalvalues have been emitted by WriteModuleInfo.
2303 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2304 if (!isa<GlobalValue>(Vals[i].first)) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002305 writeConstants(i, Vals.size(), true);
Chris Lattner52523562007-04-24 00:16:04 +00002306 return;
2307 }
2308 }
2309}
Chris Lattner215e9cd2007-04-23 20:35:01 +00002310
Teresa Johnson37687f32016-04-23 04:30:47 +00002311/// pushValueAndType - The file has to encode both the value and type id for
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002312/// many values, because we need to know what type to create for forward
2313/// references. However, most operands are not forward references, so this type
2314/// field is not needed.
2315///
2316/// This function adds V's value ID to Vals. If the value ID is higher than the
2317/// instruction ID, then it is a forward reference, and it also includes the
Jan Wen Voungafaced02012-10-11 20:20:40 +00002318/// type ID. The value ID that is written is encoded relative to the InstID.
Teresa Johnson37687f32016-04-23 04:30:47 +00002319bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2320 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002321 unsigned ValID = VE.getValueID(V);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002322 // Make encoding relative to the InstID.
2323 Vals.push_back(InstID - ValID);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002324 if (ValID >= InstID) {
2325 Vals.push_back(VE.getTypeID(V->getType()));
2326 return true;
2327 }
2328 return false;
2329}
2330
Teresa Johnson37687f32016-04-23 04:30:47 +00002331void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2332 unsigned InstID) {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002333 SmallVector<unsigned, 64> Record;
2334 LLVMContext &C = CS.getInstruction()->getContext();
2335
2336 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002337 const auto &Bundle = CS.getOperandBundleAt(i);
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002338 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002339
2340 for (auto &Input : Bundle.Inputs)
Teresa Johnson37687f32016-04-23 04:30:47 +00002341 pushValueAndType(Input, InstID, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002342
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002343 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002344 Record.clear();
2345 }
2346}
2347
Teresa Johnson37687f32016-04-23 04:30:47 +00002348/// pushValue - Like pushValueAndType, but where the type of the value is
Jan Wen Voungafaced02012-10-11 20:20:40 +00002349/// omitted (perhaps it was already encoded in an earlier operand).
Teresa Johnson37687f32016-04-23 04:30:47 +00002350void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2351 SmallVectorImpl<unsigned> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002352 unsigned ValID = VE.getValueID(V);
2353 Vals.push_back(InstID - ValID);
2354}
2355
Teresa Johnson37687f32016-04-23 04:30:47 +00002356void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2357 SmallVectorImpl<uint64_t> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002358 unsigned ValID = VE.getValueID(V);
2359 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2360 emitSignedInt64(Vals, diff);
2361}
2362
Chris Lattnere6e364c2007-04-26 05:53:54 +00002363/// WriteInstruction - Emit an instruction to the specified stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002364void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2365 unsigned InstID,
2366 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnere6e364c2007-04-26 05:53:54 +00002367 unsigned Code = 0;
2368 unsigned AbbrevToUse = 0;
Devang Patelaf206b82009-09-18 19:26:43 +00002369 VE.setInstructionID(&I);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002370 switch (I.getOpcode()) {
2371 default:
2372 if (Instruction::isCast(I.getOpcode())) {
Chris Lattner0a603252007-05-01 02:13:26 +00002373 Code = bitc::FUNC_CODE_INST_CAST;
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_CAST_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002376 Vals.push_back(VE.getTypeID(I.getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002377 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
Chris Lattnere6e364c2007-04-26 05:53:54 +00002378 } else {
2379 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattner9f35f912007-05-02 04:26:36 +00002380 Code = bitc::FUNC_CODE_INST_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002381 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002382 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Teresa Johnson37687f32016-04-23 04:30:47 +00002383 pushValue(I.getOperand(1), InstID, Vals);
2384 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2385 uint64_t Flags = getOptimizationFlags(&I);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002386 if (Flags != 0) {
2387 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2388 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2389 Vals.push_back(Flags);
2390 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002391 }
2392 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002393
David Blaikieb5b5efd2015-02-25 01:08:52 +00002394 case Instruction::GetElementPtr: {
Chris Lattner0a603252007-05-01 02:13:26 +00002395 Code = bitc::FUNC_CODE_INST_GEP;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002396 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2397 auto &GEPInst = cast<GetElementPtrInst>(I);
2398 Vals.push_back(GEPInst.isInBounds());
2399 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002400 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002401 pushValueAndType(I.getOperand(i), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002402 break;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002403 }
Dan Gohmanca0256a2008-05-31 19:11:15 +00002404 case Instruction::ExtractValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002405 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002406 pushValueAndType(I.getOperand(0), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002407 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002408 Vals.append(EVI->idx_begin(), EVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002409 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002410 }
2411 case Instruction::InsertValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002412 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002413 pushValueAndType(I.getOperand(0), InstID, Vals);
2414 pushValueAndType(I.getOperand(1), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002415 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002416 Vals.append(IVI->idx_begin(), IVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002417 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002418 }
Chris Lattner0a603252007-05-01 02:13:26 +00002419 case Instruction::Select:
Dan Gohmanc5d28922008-09-16 01:01:33 +00002420 Code = bitc::FUNC_CODE_INST_VSELECT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002421 pushValueAndType(I.getOperand(1), InstID, Vals);
2422 pushValue(I.getOperand(2), InstID, Vals);
2423 pushValueAndType(I.getOperand(0), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002424 break;
2425 case Instruction::ExtractElement:
2426 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002427 pushValueAndType(I.getOperand(0), InstID, Vals);
2428 pushValueAndType(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002429 break;
2430 case Instruction::InsertElement:
2431 Code = bitc::FUNC_CODE_INST_INSERTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002432 pushValueAndType(I.getOperand(0), InstID, Vals);
2433 pushValue(I.getOperand(1), InstID, Vals);
2434 pushValueAndType(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002435 break;
2436 case Instruction::ShuffleVector:
2437 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002438 pushValueAndType(I.getOperand(0), InstID, Vals);
2439 pushValue(I.getOperand(1), InstID, Vals);
2440 pushValue(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002441 break;
2442 case Instruction::ICmp:
James Molloy88eb5352015-07-10 12:52:00 +00002443 case Instruction::FCmp: {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002444 // compare returning Int1Ty or vector of Int1Ty
2445 Code = bitc::FUNC_CODE_INST_CMP2;
Teresa Johnson37687f32016-04-23 04:30:47 +00002446 pushValueAndType(I.getOperand(0), InstID, Vals);
2447 pushValue(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002448 Vals.push_back(cast<CmpInst>(I).getPredicate());
Teresa Johnson37687f32016-04-23 04:30:47 +00002449 uint64_t Flags = getOptimizationFlags(&I);
James Molloy88eb5352015-07-10 12:52:00 +00002450 if (Flags != 0)
2451 Vals.push_back(Flags);
Chris Lattner0a603252007-05-01 02:13:26 +00002452 break;
James Molloy88eb5352015-07-10 12:52:00 +00002453 }
Chris Lattner0a603252007-05-01 02:13:26 +00002454
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002455 case Instruction::Ret:
Devang Patelbbfd8742008-02-26 01:29:32 +00002456 {
2457 Code = bitc::FUNC_CODE_INST_RET;
2458 unsigned NumOperands = I.getNumOperands();
Devang Patelbbfd8742008-02-26 01:29:32 +00002459 if (NumOperands == 0)
2460 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2461 else if (NumOperands == 1) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002462 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Devang Patelbbfd8742008-02-26 01:29:32 +00002463 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2464 } else {
2465 for (unsigned i = 0, e = NumOperands; i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002466 pushValueAndType(I.getOperand(i), InstID, Vals);
Devang Patelbbfd8742008-02-26 01:29:32 +00002467 }
2468 }
Chris Lattner0a603252007-05-01 02:13:26 +00002469 break;
2470 case Instruction::Br:
Gabor Greif1933b002009-01-30 18:27:21 +00002471 {
2472 Code = bitc::FUNC_CODE_INST_BR;
David Blaikieb78e9e52013-02-11 01:16:51 +00002473 const BranchInst &II = cast<BranchInst>(I);
Gabor Greif1933b002009-01-30 18:27:21 +00002474 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2475 if (II.isConditional()) {
2476 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002477 pushValue(II.getCondition(), InstID, Vals);
Gabor Greif1933b002009-01-30 18:27:21 +00002478 }
Chris Lattner0a603252007-05-01 02:13:26 +00002479 }
2480 break;
2481 case Instruction::Switch:
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002482 {
2483 Code = bitc::FUNC_CODE_INST_SWITCH;
David Blaikieb78e9e52013-02-11 01:16:51 +00002484 const SwitchInst &SI = cast<SwitchInst>(I);
Bob Wilsone4077362013-09-09 19:14:35 +00002485 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002486 pushValue(SI.getCondition(), InstID, Vals);
Bob Wilsone4077362013-09-09 19:14:35 +00002487 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
Chandler Carruth927d8e62017-04-12 07:27:28 +00002488 for (auto Case : SI.cases()) {
Sanjay Patel225d65f2015-11-13 16:21:23 +00002489 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2490 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002491 }
2492 }
Chris Lattner0a603252007-05-01 02:13:26 +00002493 break;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002494 case Instruction::IndirectBr:
2495 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattner3ed871f2009-10-27 19:13:16 +00002496 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Jan Wen Voungafaced02012-10-11 20:20:40 +00002497 // Encode the address operand as relative, but not the basic blocks.
Teresa Johnson37687f32016-04-23 04:30:47 +00002498 pushValue(I.getOperand(0), InstID, Vals);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002499 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
Chris Lattner3ed871f2009-10-27 19:13:16 +00002500 Vals.push_back(VE.getValueID(I.getOperand(i)));
2501 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002502
Chris Lattnerb811e952007-05-01 07:03:37 +00002503 case Instruction::Invoke: {
Gabor Greif4b79e472009-01-16 18:40:27 +00002504 const InvokeInst *II = cast<InvokeInst>(&I);
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002505 const Value *Callee = II->getCalledValue();
2506 FunctionType *FTy = II->getFunctionType();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002507
2508 if (II->hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002509 writeOperandBundles(II, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002510
Chris Lattner0a603252007-05-01 02:13:26 +00002511 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002512
Reid Klecknerb4a2d182017-04-24 20:38:30 +00002513 Vals.push_back(VE.getAttributeListID(II->getAttributes()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002514 Vals.push_back(II->getCallingConv() | 1 << 13);
Gabor Greif6ecd6f42009-01-07 22:39:29 +00002515 Vals.push_back(VE.getValueID(II->getNormalDest()));
2516 Vals.push_back(VE.getValueID(II->getUnwindDest()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002517 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002518 pushValueAndType(Callee, InstID, Vals);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002519
Chris Lattner0a603252007-05-01 02:13:26 +00002520 // Emit value #'s for the fixed parameters.
Chris Lattner0a603252007-05-01 02:13:26 +00002521 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002522 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
Chris Lattner0a603252007-05-01 02:13:26 +00002523
2524 // Emit type/value pairs for varargs params.
2525 if (FTy->isVarArg()) {
Mehdi Amini7cf63822016-09-19 21:27:04 +00002526 for (unsigned i = FTy->getNumParams(), e = II->getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002527 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002528 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
Chris Lattner0a603252007-05-01 02:13:26 +00002529 }
2530 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002531 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002532 case Instruction::Resume:
2533 Code = bitc::FUNC_CODE_INST_RESUME;
Teresa Johnson37687f32016-04-23 04:30:47 +00002534 pushValueAndType(I.getOperand(0), InstID, Vals);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002535 break;
David Majnemer654e1302015-07-31 17:58:14 +00002536 case Instruction::CleanupRet: {
2537 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2538 const auto &CRI = cast<CleanupReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002539 pushValue(CRI.getCleanupPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002540 if (CRI.hasUnwindDest())
2541 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2542 break;
2543 }
2544 case Instruction::CatchRet: {
2545 Code = bitc::FUNC_CODE_INST_CATCHRET;
2546 const auto &CRI = cast<CatchReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002547 pushValue(CRI.getCatchPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002548 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2549 break;
2550 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00002551 case Instruction::CleanupPad:
David Majnemer654e1302015-07-31 17:58:14 +00002552 case Instruction::CatchPad: {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002553 const auto &FuncletPad = cast<FuncletPadInst>(I);
2554 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2555 : bitc::FUNC_CODE_INST_CLEANUPPAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002556 pushValue(FuncletPad.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002557
2558 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
David Majnemer654e1302015-07-31 17:58:14 +00002559 Vals.push_back(NumArgOperands);
2560 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
Teresa Johnson37687f32016-04-23 04:30:47 +00002561 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002562 break;
2563 }
2564 case Instruction::CatchSwitch: {
2565 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2566 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2567
Teresa Johnson37687f32016-04-23 04:30:47 +00002568 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002569
2570 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2571 Vals.push_back(NumHandlers);
2572 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2573 Vals.push_back(VE.getValueID(CatchPadBB));
2574
2575 if (CatchSwitch.hasUnwindDest())
2576 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
David Majnemer654e1302015-07-31 17:58:14 +00002577 break;
2578 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002579 case Instruction::Unreachable:
2580 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002581 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002582 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002583
Jay Foad372ad642011-06-20 14:18:48 +00002584 case Instruction::PHI: {
2585 const PHINode &PN = cast<PHINode>(I);
Chris Lattner0a603252007-05-01 02:13:26 +00002586 Code = bitc::FUNC_CODE_INST_PHI;
Jan Wen Voungafaced02012-10-11 20:20:40 +00002587 // With the newer instruction encoding, forward references could give
2588 // negative valued IDs. This is most common for PHIs, so we use
2589 // signed VBRs.
2590 SmallVector<uint64_t, 128> Vals64;
2591 Vals64.push_back(VE.getTypeID(PN.getType()));
Jay Foad372ad642011-06-20 14:18:48 +00002592 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002593 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002594 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
Jay Foad372ad642011-06-20 14:18:48 +00002595 }
Jan Wen Voungafaced02012-10-11 20:20:40 +00002596 // Emit a Vals64 vector and exit.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002597 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002598 Vals64.clear();
2599 return;
Jay Foad372ad642011-06-20 14:18:48 +00002600 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002601
Bill Wendlingfae14752011-08-12 20:24:12 +00002602 case Instruction::LandingPad: {
2603 const LandingPadInst &LP = cast<LandingPadInst>(I);
2604 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2605 Vals.push_back(VE.getTypeID(LP.getType()));
Bill Wendlingfae14752011-08-12 20:24:12 +00002606 Vals.push_back(LP.isCleanup());
2607 Vals.push_back(LP.getNumClauses());
2608 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2609 if (LP.isCatch(I))
2610 Vals.push_back(LandingPadInst::Catch);
2611 else
2612 Vals.push_back(LandingPadInst::Filter);
Teresa Johnson37687f32016-04-23 04:30:47 +00002613 pushValueAndType(LP.getClause(I), InstID, Vals);
Bill Wendlingfae14752011-08-12 20:24:12 +00002614 }
2615 break;
2616 }
2617
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002618 case Instruction::Alloca: {
Chris Lattner0a603252007-05-01 02:13:26 +00002619 Code = bitc::FUNC_CODE_INST_ALLOCA;
David Blaikiebdb49102015-04-28 16:51:01 +00002620 const AllocaInst &AI = cast<AllocaInst>(I);
2621 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
Dan Gohman9da5bb02010-05-28 01:38:28 +00002622 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002623 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002624 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2625 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2626 "not enough bits for maximum alignment");
2627 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2628 AlignRecord |= AI.isUsedWithInAlloca() << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00002629 AlignRecord |= 1 << 6;
Manman Ren9bfd0d02016-04-01 21:41:15 +00002630 AlignRecord |= AI.isSwiftError() << 7;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002631 Vals.push_back(AlignRecord);
Chris Lattner0a603252007-05-01 02:13:26 +00002632 break;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002633 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002634
Chris Lattner0a603252007-05-01 02:13:26 +00002635 case Instruction::Load:
Eli Friedman59b66882011-08-09 23:02:53 +00002636 if (cast<LoadInst>(I).isAtomic()) {
2637 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002638 pushValueAndType(I.getOperand(0), InstID, Vals);
Eli Friedman59b66882011-08-09 23:02:53 +00002639 } else {
2640 Code = bitc::FUNC_CODE_INST_LOAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002641 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
Eli Friedman59b66882011-08-09 23:02:53 +00002642 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2643 }
David Blaikie85035652015-02-25 01:07:20 +00002644 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002645 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2646 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002647 if (cast<LoadInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002648 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2649 Vals.push_back(getEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002650 }
Chris Lattner0a603252007-05-01 02:13:26 +00002651 break;
2652 case Instruction::Store:
Eli Friedman59b66882011-08-09 23:02:53 +00002653 if (cast<StoreInst>(I).isAtomic())
2654 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2655 else
2656 Code = bitc::FUNC_CODE_INST_STORE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002657 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2658 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
Chris Lattner0a603252007-05-01 02:13:26 +00002659 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2660 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002661 if (cast<StoreInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002662 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2663 Vals.push_back(getEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002664 }
Chris Lattner0a603252007-05-01 02:13:26 +00002665 break;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002666 case Instruction::AtomicCmpXchg:
2667 Code = bitc::FUNC_CODE_INST_CMPXCHG;
Teresa Johnson37687f32016-04-23 04:30:47 +00002668 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2669 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2670 pushValue(I.getOperand(2), InstID, Vals); // newval.
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002671 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002672 Vals.push_back(
2673 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2674 Vals.push_back(
2675 getEncodedSynchScope(cast<AtomicCmpXchgInst>(I).getSynchScope()));
2676 Vals.push_back(
2677 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
Tim Northover420a2162014-06-13 14:24:07 +00002678 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002679 break;
2680 case Instruction::AtomicRMW:
2681 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
Teresa Johnson37687f32016-04-23 04:30:47 +00002682 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2683 pushValue(I.getOperand(1), InstID, Vals); // val.
2684 Vals.push_back(
2685 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002686 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002687 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2688 Vals.push_back(
2689 getEncodedSynchScope(cast<AtomicRMWInst>(I).getSynchScope()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002690 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002691 case Instruction::Fence:
2692 Code = bitc::FUNC_CODE_INST_FENCE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002693 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2694 Vals.push_back(getEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
Eli Friedmanfee02c62011-07-25 23:16:38 +00002695 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002696 case Instruction::Call: {
Gabor Greife9afee22010-06-26 09:35:09 +00002697 const CallInst &CI = cast<CallInst>(I);
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002698 FunctionType *FTy = CI.getFunctionType();
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002699
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002700 if (CI.hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002701 writeOperandBundles(&CI, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002702
Chris Lattnerc44070802011-06-17 18:17:37 +00002703 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002704
Reid Klecknerb4a2d182017-04-24 20:38:30 +00002705 Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002706
Teresa Johnson37687f32016-04-23 04:30:47 +00002707 unsigned Flags = getOptimizationFlags(&I);
Akira Hatanaka97cb3972015-11-07 02:48:49 +00002708 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2709 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2710 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2711 1 << bitc::CALL_EXPLICIT_TYPE |
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002712 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2713 unsigned(Flags != 0) << bitc::CALL_FMF);
2714 if (Flags != 0)
2715 Vals.push_back(Flags);
2716
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002717 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002718 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002719
Chris Lattner0a603252007-05-01 02:13:26 +00002720 // Emit value #'s for the fixed parameters.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002721 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2722 // Check for labels (can happen with asm labels).
2723 if (FTy->getParamType(i)->isLabelTy())
2724 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2725 else
Teresa Johnson37687f32016-04-23 04:30:47 +00002726 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002727 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002728
Chris Lattnerb811e952007-05-01 07:03:37 +00002729 // Emit type/value pairs for varargs params.
2730 if (FTy->isVarArg()) {
Gabor Greife9afee22010-06-26 09:35:09 +00002731 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002732 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002733 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
Chris Lattner0a603252007-05-01 02:13:26 +00002734 }
2735 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002736 }
Chris Lattner0a603252007-05-01 02:13:26 +00002737 case Instruction::VAArg:
2738 Code = bitc::FUNC_CODE_INST_VAARG;
2739 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
Teresa Johnson37687f32016-04-23 04:30:47 +00002740 pushValue(I.getOperand(0), InstID, Vals); // valist.
Chris Lattner0a603252007-05-01 02:13:26 +00002741 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2742 break;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002743 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002744
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002745 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002746 Vals.clear();
2747}
2748
Peter Collingbournea0f371a2017-04-17 17:51:36 +00002749/// Write a GlobalValue VST to the module. The purpose of this data structure is
2750/// to allow clients to efficiently find the function body.
2751void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
2752 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
2753 // Get the offset of the VST we are writing, and backpatch it into
2754 // the VST forward declaration record.
2755 uint64_t VSTOffset = Stream.GetCurrentBitNo();
2756 // The BitcodeStartBit was the stream offset of the identification block.
2757 VSTOffset -= bitcodeStartBit();
2758 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
2759 // Note that we add 1 here because the offset is relative to one word
2760 // before the start of the identification block, which was historically
2761 // always the start of the regular bitcode header.
2762 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002763
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002764 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2eae59f2007-05-04 20:34:50 +00002765
Peter Collingbournea0f371a2017-04-17 17:51:36 +00002766 auto Abbv = std::make_shared<BitCodeAbbrev>();
2767 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2768 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2769 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2770 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnsonff642b92015-09-17 20:12:00 +00002771
Peter Collingbournea0f371a2017-04-17 17:51:36 +00002772 for (const Function &F : M) {
2773 uint64_t Record[2];
Teresa Johnsonff642b92015-09-17 20:12:00 +00002774
Peter Collingbournea0f371a2017-04-17 17:51:36 +00002775 if (F.isDeclaration())
2776 continue;
Teresa Johnsoncd21a642016-07-17 14:47:01 +00002777
Peter Collingbournea0f371a2017-04-17 17:51:36 +00002778 Record[0] = VE.getValueID(&F);
2779
2780 // Save the word offset of the function (from the start of the
2781 // actual bitcode written to the stream).
2782 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
2783 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2784 // Note that we add 1 here because the offset is relative to one word
2785 // before the start of the identification block, which was historically
2786 // always the start of the regular bitcode header.
2787 Record[1] = BitcodeIndex / 32 + 1;
2788
2789 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002790 }
2791
Peter Collingbournea0f371a2017-04-17 17:51:36 +00002792 Stream.ExitBlock();
2793}
2794
2795/// Emit names for arguments, instructions and basic blocks in a function.
2796void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
2797 const ValueSymbolTable &VST) {
2798 if (VST.empty())
2799 return;
2800
2801 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2802
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002803 // FIXME: Set up the abbrev, we know how many values there are!
2804 // FIXME: We know if the type names can use 7-bit ascii.
Teresa Johnsoncd21a642016-07-17 14:47:01 +00002805 SmallVector<uint64_t, 64> NameVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002806
Yaron Keren001e2e42015-08-10 07:04:29 +00002807 for (const ValueName &Name : VST) {
Chris Lattner4d925982007-05-04 20:52:02 +00002808 // Figure out the encoding to use for the name.
David Blaikieb6b42e02017-06-02 17:24:26 +00002809 StringEncoding Bits = getStringEncoding(Name.getKey());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002810
Chris Lattnera0987962007-05-04 21:31:13 +00002811 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002812 NameVals.push_back(VE.getValueID(Name.getValue()));
2813
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002814 // VST_CODE_ENTRY: [valueid, namechar x N]
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002815 // VST_CODE_BBENTRY: [bbid, namechar x N]
Chris Lattner6be58c62007-05-03 22:18:21 +00002816 unsigned Code;
Yaron Keren001e2e42015-08-10 07:04:29 +00002817 if (isa<BasicBlock>(Name.getValue())) {
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002818 Code = bitc::VST_CODE_BBENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002819 if (Bits == SE_Char6)
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002820 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002821 } else {
2822 Code = bitc::VST_CODE_ENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002823 if (Bits == SE_Char6)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002824 AbbrevToUse = VST_ENTRY_6_ABBREV;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002825 else if (Bits == SE_Fixed7)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002826 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002827 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002828
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002829 for (const auto P : Name.getKey())
2830 NameVals.push_back((unsigned char)P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002831
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002832 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002833 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002834 NameVals.clear();
2835 }
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002836
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002837 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002838}
2839
Teresa Johnson37687f32016-04-23 04:30:47 +00002840void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002841 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2842 unsigned Code;
2843 if (isa<BasicBlock>(Order.V))
2844 Code = bitc::USELIST_CODE_BB;
2845 else
2846 Code = bitc::USELIST_CODE_DEFAULT;
2847
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002848 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002849 Record.push_back(VE.getValueID(Order.V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002850 Stream.EmitRecord(Code, Record);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002851}
2852
Teresa Johnson37687f32016-04-23 04:30:47 +00002853void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002854 assert(VE.shouldPreserveUseListOrder() &&
2855 "Expected to be preserving use-list order");
2856
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002857 auto hasMore = [&]() {
2858 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2859 };
2860 if (!hasMore())
2861 // Nothing to do.
2862 return;
2863
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002864 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002865 while (hasMore()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002866 writeUseList(std::move(VE.UseListOrders.back()));
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002867 VE.UseListOrders.pop_back();
2868 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002869 Stream.ExitBlock();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002870}
2871
Teresa Johnson403a7872015-10-04 14:33:43 +00002872/// Emit a function body to the module stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002873void ModuleBitcodeWriter::writeFunction(
2874 const Function &F,
2875 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002876 // Save the bitcode index of the start of this function block for recording
2877 // in the VST.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002878 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002879
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002880 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a11b642011-06-03 17:02:19 +00002881 VE.incorporateFunction(F);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002882
2883 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002884
Chris Lattnere6e364c2007-04-26 05:53:54 +00002885 // Emit the number of basic blocks, so the reader can create them ahead of
2886 // time.
2887 Vals.push_back(VE.getBasicBlocks().size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002888 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002889 Vals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002890
Chris Lattnere6e364c2007-04-26 05:53:54 +00002891 // If there are function-local constants, emit them now.
2892 unsigned CstStart, CstEnd;
2893 VE.getFunctionConstantRange(CstStart, CstEnd);
Teresa Johnson37687f32016-04-23 04:30:47 +00002894 writeConstants(CstStart, CstEnd, false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002895
Victor Hernandez6c730de2010-01-14 01:50:08 +00002896 // If there is function-local metadata, emit it now.
Teresa Johnson37687f32016-04-23 04:30:47 +00002897 writeFunctionMetadata(F);
Victor Hernandez6c730de2010-01-14 01:50:08 +00002898
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002899 // Keep a running idea of what the instruction ID is.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002900 unsigned InstID = CstEnd;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002901
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002902 bool NeedsMetadataAttachment = F.hasMetadata();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002903
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002904 DILocation *LastDL = nullptr;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002905 // Finally, emit all the instructions, in order.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002906 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002907 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2908 I != E; ++I) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002909 writeInstruction(*I, InstID, Vals);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002910
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00002911 if (!I->getType()->isVoidTy())
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002912 ++InstID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002913
Chris Lattner07d09ed2010-04-03 02:17:50 +00002914 // If the instruction has metadata, write a metadata attachment later.
2915 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002916
Chris Lattner07d09ed2010-04-03 02:17:50 +00002917 // If the instruction has a debug location, emit it.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002918 DILocation *DL = I->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002919 if (!DL)
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002920 continue;
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002921
2922 if (DL == LastDL) {
Chris Lattner07d09ed2010-04-03 02:17:50 +00002923 // Just repeat the same debug loc as last time.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002924 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002925 continue;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002926 }
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002927
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002928 Vals.push_back(DL->getLine());
2929 Vals.push_back(DL->getColumn());
2930 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2931 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002932 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002933 Vals.clear();
Duncan P. N. Exon Smith538ef562015-05-06 22:51:12 +00002934
2935 LastDL = DL;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002936 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002937
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002938 // Emit names for all the instructions etc.
Mehdi Aminia53d49e2016-09-17 06:00:02 +00002939 if (auto *Symtab = F.getValueSymbolTable())
Peter Collingbournea0f371a2017-04-17 17:51:36 +00002940 writeFunctionLevelValueSymbolTable(*Symtab);
Devang Patelaf206b82009-09-18 19:26:43 +00002941
Chris Lattner07d09ed2010-04-03 02:17:50 +00002942 if (NeedsMetadataAttachment)
Peter Collingbournecceae7f2016-05-31 23:01:54 +00002943 writeFunctionMetadataAttachment(F);
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002944 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00002945 writeUseListBlock(&F);
Chad Rosier6a11b642011-06-03 17:02:19 +00002946 VE.purgeFunction();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002947 Stream.ExitBlock();
Chris Lattner831d4202007-04-26 03:27:58 +00002948}
2949
Chris Lattner702658c2007-05-04 18:26:27 +00002950// Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002951void ModuleBitcodeWriter::writeBlockInfo() {
Chris Lattner702658c2007-05-04 18:26:27 +00002952 // We only want to emit block info records for blocks that have multiple
Jan Wen Voungafaced02012-10-11 20:20:40 +00002953 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
Jan Wen Voung8c9e9412012-10-11 21:45:16 +00002954 // Other blocks can define their abbrevs inline.
Peter Collingbourned3a6c702016-11-01 01:18:57 +00002955 Stream.EnterBlockInfoBlock();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002956
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002957 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002958 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattner982ec1e2007-05-05 00:17:00 +00002959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2960 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2961 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2962 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002963 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002964 VST_ENTRY_8_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002965 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002966 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002967
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002968 { // 7-bit fixed width VST_CODE_ENTRY strings.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002969 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattner982ec1e2007-05-05 00:17:00 +00002970 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2971 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002974 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002975 VST_ENTRY_7_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002976 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002977 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002978 { // 6-bit char6 VST_CODE_ENTRY strings.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002979 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnere760d6f2007-05-05 01:26:50 +00002980 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2981 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2982 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2983 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002984 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002985 VST_ENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002986 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere760d6f2007-05-05 01:26:50 +00002987 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002988 { // 6-bit char6 VST_CODE_BBENTRY strings.
David Blaikie7ad9dc12017-01-04 22:36:33 +00002989 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattner982ec1e2007-05-05 00:17:00 +00002990 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2991 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2992 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnere760d6f2007-05-05 01:26:50 +00002993 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002994 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002995 VST_BBENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002996 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002997 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002998
2999
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003000
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003001 { // SETTYPE abbrev for CONSTANTS_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003002 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003003 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
3004 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
David Blaikie7b028102015-02-25 00:51:52 +00003005 VE.computeBitsRequiredForTypeIndicies()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003006 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003007 CONSTANTS_SETTYPE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003008 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003009 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003010
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003011 { // INTEGER abbrev for CONSTANTS_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003012 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003013 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
3014 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003015 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003016 CONSTANTS_INTEGER_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003017 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003018 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003019
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003020 { // CE_CAST abbrev for CONSTANTS_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003021 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003022 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
3023 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
3024 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
David Blaikie7b028102015-02-25 00:51:52 +00003025 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003026 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3027
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003028 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003029 CONSTANTS_CE_CAST_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003030 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003031 }
3032 { // NULL abbrev for CONSTANTS_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003033 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003034 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003035 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003036 CONSTANTS_NULL_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003037 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003038 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003039
Chris Lattnerb80751d2007-05-05 07:44:49 +00003040 // FIXME: This should only use space for first class types!
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003041
Chris Lattnerb80751d2007-05-05 07:44:49 +00003042 { // INST_LOAD abbrev for FUNCTION_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003043 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerb80751d2007-05-05 07:44:49 +00003044 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattnerb80751d2007-05-05 07:44:49 +00003045 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
David Blaikie85035652015-02-25 01:07:20 +00003046 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3047 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerb80751d2007-05-05 07:44:49 +00003048 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3049 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003050 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003051 FUNCTION_INST_LOAD_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003052 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerb80751d2007-05-05 07:44:49 +00003053 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003054 { // INST_BINOP abbrev for FUNCTION_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003055 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003056 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3057 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3058 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3059 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003060 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003061 FUNCTION_INST_BINOP_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003062 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003063 }
Dan Gohman0ebd6962009-07-20 21:19:07 +00003064 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003065 auto Abbv = std::make_shared<BitCodeAbbrev>();
Dan Gohman0ebd6962009-07-20 21:19:07 +00003066 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
3067 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3068 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3069 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3070 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003071 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003072 FUNCTION_INST_BINOP_FLAGS_ABBREV)
Dan Gohman0ebd6962009-07-20 21:19:07 +00003073 llvm_unreachable("Unexpected abbrev ordering!");
3074 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003075 { // INST_CAST abbrev for FUNCTION_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003076 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003077 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
3078 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3079 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
David Blaikie7b028102015-02-25 00:51:52 +00003080 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003081 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003082 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003083 FUNCTION_INST_CAST_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003084 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00003085 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003086
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003087 { // INST_RET abbrev for FUNCTION_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003088 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003089 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003090 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003091 FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003092 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003093 }
3094 { // INST_RET abbrev for FUNCTION_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003095 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003096 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
3097 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003098 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003099 FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003100 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003101 }
3102 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003103 auto Abbv = std::make_shared<BitCodeAbbrev>();
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003104 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003105 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00003106 FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003107 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00003108 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00003109 {
David Blaikie7ad9dc12017-01-04 22:36:33 +00003110 auto Abbv = std::make_shared<BitCodeAbbrev>();
David Blaikieb5b5efd2015-02-25 01:08:52 +00003111 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
3112 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
3113 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3114 Log2_32_Ceil(VE.getTypes().size() + 1)));
3115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3116 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003117 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
David Blaikieb5b5efd2015-02-25 01:08:52 +00003118 FUNCTION_INST_GEP_ABBREV)
3119 llvm_unreachable("Unexpected abbrev ordering!");
3120 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003121
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003122 Stream.ExitBlock();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003123}
3124
Teresa Johnson403a7872015-10-04 14:33:43 +00003125/// Write the module path strings, currently only used when generating
3126/// a combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003127void IndexBitcodeWriter::writeModStrings() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003128 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00003129
3130 // TODO: See which abbrev sizes we actually need to emit
3131
3132 // 8-bit fixed-width MST_ENTRY strings.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003133 auto Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson403a7872015-10-04 14:33:43 +00003134 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3135 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3137 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003138 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson403a7872015-10-04 14:33:43 +00003139
3140 // 7-bit fixed width MST_ENTRY strings.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003141 Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson403a7872015-10-04 14:33:43 +00003142 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3143 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3144 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3145 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003146 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson403a7872015-10-04 14:33:43 +00003147
3148 // 6-bit char6 MST_ENTRY strings.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003149 Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson403a7872015-10-04 14:33:43 +00003150 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
3151 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3152 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3153 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003154 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson403a7872015-10-04 14:33:43 +00003155
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003156 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003157 Abbv = std::make_shared<BitCodeAbbrev>();
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003158 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
3159 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3161 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3162 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3163 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003164 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003165
3166 SmallVector<unsigned, 64> Vals;
Teresa Johnson7a27b132017-06-02 01:56:02 +00003167 forEachModule(
3168 [&](const StringMapEntry<std::pair<uint64_t, ModuleHash>> &MPSE) {
David Blaikieb6b42e02017-06-02 17:24:26 +00003169 StringRef Key = MPSE.getKey();
3170 const auto &Value = MPSE.getValue();
3171 StringEncoding Bits = getStringEncoding(Key);
Teresa Johnson7a27b132017-06-02 01:56:02 +00003172 unsigned AbbrevToUse = Abbrev8Bit;
3173 if (Bits == SE_Char6)
3174 AbbrevToUse = Abbrev6Bit;
3175 else if (Bits == SE_Fixed7)
3176 AbbrevToUse = Abbrev7Bit;
Teresa Johnson403a7872015-10-04 14:33:43 +00003177
David Blaikieb6b42e02017-06-02 17:24:26 +00003178 Vals.push_back(Value.first);
3179 Vals.append(Key.begin(), Key.end());
Teresa Johnson403a7872015-10-04 14:33:43 +00003180
Teresa Johnson7a27b132017-06-02 01:56:02 +00003181 // Emit the finished record.
3182 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003183
Teresa Johnson7a27b132017-06-02 01:56:02 +00003184 // Emit an optional hash for the module now
David Blaikieb6b42e02017-06-02 17:24:26 +00003185 const auto &Hash = Value.second;
3186 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
3187 Vals.assign(Hash.begin(), Hash.end());
Teresa Johnson7a27b132017-06-02 01:56:02 +00003188 // Emit the hash record.
3189 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
3190 }
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003191
Teresa Johnson7a27b132017-06-02 01:56:02 +00003192 Vals.clear();
3193 });
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003194 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003195}
3196
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +00003197/// Write the function type metadata related records that need to appear before
3198/// a function summary entry (whether per-module or combined).
3199static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
3200 FunctionSummary *FS) {
3201 if (!FS->type_tests().empty())
3202 Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
3203
3204 SmallVector<uint64_t, 64> Record;
3205
3206 auto WriteVFuncIdVec = [&](uint64_t Ty,
3207 ArrayRef<FunctionSummary::VFuncId> VFs) {
3208 if (VFs.empty())
3209 return;
3210 Record.clear();
3211 for (auto &VF : VFs) {
3212 Record.push_back(VF.GUID);
3213 Record.push_back(VF.Offset);
3214 }
3215 Stream.EmitRecord(Ty, Record);
3216 };
3217
3218 WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
3219 FS->type_test_assume_vcalls());
3220 WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
3221 FS->type_checked_load_vcalls());
3222
3223 auto WriteConstVCallVec = [&](uint64_t Ty,
3224 ArrayRef<FunctionSummary::ConstVCall> VCs) {
3225 for (auto &VC : VCs) {
3226 Record.clear();
3227 Record.push_back(VC.VFunc.GUID);
3228 Record.push_back(VC.VFunc.Offset);
3229 Record.insert(Record.end(), VC.Args.begin(), VC.Args.end());
3230 Stream.EmitRecord(Ty, Record);
3231 }
3232 };
3233
3234 WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
3235 FS->type_test_assume_const_vcalls());
3236 WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
3237 FS->type_checked_load_const_vcalls());
3238}
3239
Teresa Johnson403a7872015-10-04 14:33:43 +00003240// Helper to emit a single function summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003241void ModuleBitcodeWriter::writePerModuleFunctionSummaryRecord(
Teresa Johnson28e457b2016-04-24 14:57:11 +00003242 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +00003243 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3244 const Function &F) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003245 NameVals.push_back(ValueID);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003246
Teresa Johnson28e457b2016-04-24 14:57:11 +00003247 FunctionSummary *FS = cast<FunctionSummary>(Summary);
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +00003248 writeFunctionTypeMetadataRecords(Stream, FS);
Peter Collingbourne1b4137a72016-12-21 23:03:45 +00003249
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003250 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003251 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003252 NameVals.push_back(FS->refs().size());
3253
3254 for (auto &RI : FS->refs())
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003255 NameVals.push_back(VE.getValueID(RI.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003256
3257 bool HasProfileData = F.getEntryCount().hasValue();
Peter Collingbourne0c30f082016-12-20 21:12:28 +00003258 for (auto &ECI : FS->calls()) {
Teresa Johnsoncd21a642016-07-17 14:47:01 +00003259 NameVals.push_back(getValueId(ECI.first));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003260 if (HasProfileData)
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003261 NameVals.push_back(static_cast<uint8_t>(ECI.second.Hotness));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003262 }
3263
3264 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3265 unsigned Code =
3266 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
Teresa Johnson403a7872015-10-04 14:33:43 +00003267
3268 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003269 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003270 NameVals.clear();
3271}
3272
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003273// Collect the global value references in the given variable's initializer,
3274// and emit them in a summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003275void ModuleBitcodeWriter::writeModuleLevelReferences(
3276 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3277 unsigned FSModRefsAbbrev) {
Peter Collingbourne9667b912017-05-04 18:03:25 +00003278 auto VI = Index->getValueInfo(GlobalValue::getGUID(V.getName()));
3279 if (!VI || VI.getSummaryList().empty()) {
Teresa Johnson3624bdf2016-11-14 17:12:32 +00003280 // Only declarations should not have a summary (a declaration might however
3281 // have a summary if the def was in module level asm).
3282 assert(V.isDeclaration());
Teresa Johnson13968092016-03-15 19:35:45 +00003283 return;
Teresa Johnson3624bdf2016-11-14 17:12:32 +00003284 }
Peter Collingbourne9667b912017-05-04 18:03:25 +00003285 auto *Summary = VI.getSummaryList()[0].get();
Teresa Johnson13968092016-03-15 19:35:45 +00003286 NameVals.push_back(VE.getValueID(&V));
Teresa Johnson28e457b2016-04-24 14:57:11 +00003287 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
Teresa Johnson7c31cb12016-10-28 19:36:00 +00003288 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003289
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003290 unsigned SizeBeforeRefs = NameVals.size();
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003291 for (auto &RI : VS->refs())
Mehdi Aminifdbb8f42016-05-16 22:47:15 +00003292 NameVals.push_back(VE.getValueID(RI.getValue()));
3293 // Sort the refs for determinism output, the vector returned by FS->refs() has
3294 // been initialized from a DenseSet.
3295 std::sort(NameVals.begin() + SizeBeforeRefs, NameVals.end());
Mehdi Aminif606c8d2016-05-16 08:50:27 +00003296
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003297 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3298 FSModRefsAbbrev);
Teresa Johnson13968092016-03-15 19:35:45 +00003299 NameVals.clear();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003300}
Teresa Johnson403a7872015-10-04 14:33:43 +00003301
Mehdi Amini8fe69362016-04-24 03:18:11 +00003302// Current version for the summary.
3303// This is bumped whenever we introduce changes in the way some record are
3304// interpreted, like flags for instance.
Teresa Johnson519465b2017-01-05 14:32:16 +00003305static const uint64_t INDEX_VERSION = 3;
Mehdi Amini8fe69362016-04-24 03:18:11 +00003306
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003307/// Emit the per-module summary section alongside the rest of
3308/// the module's bitcode.
Teresa Johnson37687f32016-04-23 04:30:47 +00003309void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() {
Peter Collingbournee357fbd2017-06-08 23:01:49 +00003310 // By default we compile with ThinLTO if the module has a summary, but the
3311 // client can request full LTO with a module flag.
3312 bool IsThinLTO = true;
3313 if (auto *MD =
3314 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
3315 IsThinLTO = MD->getZExtValue();
3316 Stream.EnterSubblock(IsThinLTO ? bitc::GLOBALVAL_SUMMARY_BLOCK_ID
3317 : bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID,
3318 4);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003319
Mehdi Amini8fe69362016-04-24 03:18:11 +00003320 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3321
Teresa Johnson620c1402016-09-20 23:07:17 +00003322 if (Index->begin() == Index->end()) {
3323 Stream.ExitBlock();
3324 return;
3325 }
3326
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003327 for (const auto &GVI : valueIds()) {
3328 Stream.EmitRecord(bitc::FS_VALUE_GUID,
3329 ArrayRef<uint64_t>{GVI.second, GVI.first});
3330 }
3331
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003332 // Abbrev for FS_PERMODULE.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003333 auto Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003334 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003335 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003337 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003339 // numrefs x valueid, n x (valueid)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003340 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3341 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003342 unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson403a7872015-10-04 14:33:43 +00003343
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003344 // Abbrev for FS_PERMODULE_PROFILE.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003345 Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003346 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3347 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003348 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003349 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003351 // numrefs x valueid, n x (valueid, hotness)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003352 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3353 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003354 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003355
3356 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003357 Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003358 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3359 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003360 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003361 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3362 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003363 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003364
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003365 // Abbrev for FS_ALIAS.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003366 Abbv = std::make_shared<BitCodeAbbrev>();
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003367 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3368 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
David Blaikie7ad9dc12017-01-04 22:36:33 +00003371 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003372
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003373 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003374 // Iterate over the list of functions instead of the Index to
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003375 // ensure the ordering is stable.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003376 for (const Function &F : M) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003377 // Summary emission does not support anonymous functions, they have to
3378 // renamed using the anonymous function renaming pass.
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003379 if (!F.hasName())
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003380 report_fatal_error("Unexpected anonymous function when writing summary");
Teresa Johnson403a7872015-10-04 14:33:43 +00003381
Peter Collingbourne9667b912017-05-04 18:03:25 +00003382 ValueInfo VI = Index->getValueInfo(GlobalValue::getGUID(F.getName()));
3383 if (!VI || VI.getSummaryList().empty()) {
Teresa Johnson3624bdf2016-11-14 17:12:32 +00003384 // Only declarations should not have a summary (a declaration might
3385 // however have a summary if the def was in module level asm).
3386 assert(F.isDeclaration());
3387 continue;
3388 }
Peter Collingbourne9667b912017-05-04 18:03:25 +00003389 auto *Summary = VI.getSummaryList()[0].get();
Peter Collingbourne832e7fa2016-05-06 02:41:23 +00003390 writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
3391 FSCallsAbbrev, FSCallsProfileAbbrev, F);
Teresa Johnson403a7872015-10-04 14:33:43 +00003392 }
3393
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003394 // Capture references from GlobalVariable initializers, which are outside
3395 // of a function scope.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003396 for (const GlobalVariable &G : M.globals())
Teresa Johnson37687f32016-04-23 04:30:47 +00003397 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003398
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003399 for (const GlobalAlias &A : M.aliases()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003400 auto *Aliasee = A.getBaseObject();
3401 if (!Aliasee->hasName())
3402 // Nameless function don't have an entry in the summary, skip it.
3403 continue;
3404 auto AliasId = VE.getValueID(&A);
3405 auto AliaseeId = VE.getValueID(Aliasee);
3406 NameVals.push_back(AliasId);
Teresa Johnson02563cd2016-10-28 02:39:38 +00003407 auto *Summary = Index->getGlobalValueSummary(A);
3408 AliasSummary *AS = cast<AliasSummary>(Summary);
3409 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003410 NameVals.push_back(AliaseeId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003411 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003412 NameVals.clear();
3413 }
3414
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003415 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003416}
3417
Teresa Johnson26ab5772016-03-15 00:04:37 +00003418/// Emit the combined summary section into the combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003419void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003420 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Mehdi Amini8fe69362016-04-24 03:18:11 +00003421 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
Teresa Johnson403a7872015-10-04 14:33:43 +00003422
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003423 for (const auto &GVI : valueIds()) {
3424 Stream.EmitRecord(bitc::FS_VALUE_GUID,
3425 ArrayRef<uint64_t>{GVI.second, GVI.first});
3426 }
3427
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003428 // Abbrev for FS_COMBINED.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003429 auto Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003430 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
Teresa Johnson02e98332016-04-27 13:28:35 +00003431 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003434 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3435 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003436 // numrefs x valueid, n x (valueid)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003437 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3438 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003439 unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson403a7872015-10-04 14:33:43 +00003440
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003441 // Abbrev for FS_COMBINED_PROFILE.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003442 Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003443 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
Teresa Johnson02e98332016-04-27 13:28:35 +00003444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003445 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003446 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003447 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3448 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003449 // numrefs x valueid, n x (valueid, hotness)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003450 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3451 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003452 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003453
3454 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003455 Abbv = std::make_shared<BitCodeAbbrev>();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003456 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003457 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003458 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003459 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003460 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3461 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003462 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003463
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003464 // Abbrev for FS_COMBINED_ALIAS.
David Blaikie7ad9dc12017-01-04 22:36:33 +00003465 Abbv = std::make_shared<BitCodeAbbrev>();
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003466 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003467 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003468 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003469 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson02e98332016-04-27 13:28:35 +00003470 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
David Blaikie7ad9dc12017-01-04 22:36:33 +00003471 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003472
Teresa Johnson02e98332016-04-27 13:28:35 +00003473 // The aliases are emitted as a post-pass, and will point to the value
3474 // id of the aliasee. Save them in a vector for post-processing.
Teresa Johnson28e457b2016-04-24 14:57:11 +00003475 SmallVector<AliasSummary *, 64> Aliases;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003476
Teresa Johnson02e98332016-04-27 13:28:35 +00003477 // Save the value id for each summary for alias emission.
3478 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
3479
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003480 SmallVector<uint64_t, 64> NameVals;
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003481
3482 // For local linkage, we also emit the original name separately
3483 // immediately after the record.
3484 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3485 if (!GlobalValue::isLocalLinkage(S.linkage()))
3486 return;
3487 NameVals.push_back(S.getOriginalName());
3488 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3489 NameVals.clear();
3490 };
3491
Peter Collingbourne7c2c4092017-05-02 17:48:39 +00003492 forEachSummary([&](GVInfo I) {
Teresa Johnson84174c32016-05-10 13:48:23 +00003493 GlobalValueSummary *S = I.second;
3494 assert(S);
Teresa Johnson02e98332016-04-27 13:28:35 +00003495
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003496 auto ValueId = getValueId(I.first);
3497 assert(ValueId);
3498 SummaryToValueIdMap[S] = *ValueId;
Teresa Johnson02e98332016-04-27 13:28:35 +00003499
Teresa Johnson84174c32016-05-10 13:48:23 +00003500 if (auto *AS = dyn_cast<AliasSummary>(S)) {
3501 // Will process aliases as a post-pass because the reader wants all
3502 // global to be loaded first.
3503 Aliases.push_back(AS);
Peter Collingbourne7c2c4092017-05-02 17:48:39 +00003504 return;
Teresa Johnson84174c32016-05-10 13:48:23 +00003505 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003506
Teresa Johnson84174c32016-05-10 13:48:23 +00003507 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003508 NameVals.push_back(*ValueId);
Teresa Johnson84174c32016-05-10 13:48:23 +00003509 NameVals.push_back(Index.getModuleId(VS->modulePath()));
3510 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
3511 for (auto &RI : VS->refs()) {
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003512 auto RefValueId = getValueId(RI.getGUID());
3513 if (!RefValueId)
3514 continue;
3515 NameVals.push_back(*RefValueId);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003516 }
3517
Teresa Johnson403a7872015-10-04 14:33:43 +00003518 // Emit the finished record.
Teresa Johnson84174c32016-05-10 13:48:23 +00003519 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3520 FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003521 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003522 MaybeEmitOriginalName(*S);
Peter Collingbourne7c2c4092017-05-02 17:48:39 +00003523 return;
Teresa Johnson403a7872015-10-04 14:33:43 +00003524 }
Teresa Johnson84174c32016-05-10 13:48:23 +00003525
3526 auto *FS = cast<FunctionSummary>(S);
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +00003527 writeFunctionTypeMetadataRecords(Stream, FS);
Peter Collingbourne1b4137a72016-12-21 23:03:45 +00003528
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003529 NameVals.push_back(*ValueId);
Teresa Johnson84174c32016-05-10 13:48:23 +00003530 NameVals.push_back(Index.getModuleId(FS->modulePath()));
3531 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
3532 NameVals.push_back(FS->instCount());
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003533 // Fill in below
3534 NameVals.push_back(0);
Teresa Johnson84174c32016-05-10 13:48:23 +00003535
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003536 unsigned Count = 0;
Teresa Johnson84174c32016-05-10 13:48:23 +00003537 for (auto &RI : FS->refs()) {
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003538 auto RefValueId = getValueId(RI.getGUID());
3539 if (!RefValueId)
3540 continue;
3541 NameVals.push_back(*RefValueId);
3542 Count++;
Teresa Johnson84174c32016-05-10 13:48:23 +00003543 }
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003544 NameVals[4] = Count;
Teresa Johnson84174c32016-05-10 13:48:23 +00003545
3546 bool HasProfileData = false;
3547 for (auto &EI : FS->calls()) {
Piotr Padlewskid9830eb2016-09-26 20:37:32 +00003548 HasProfileData |= EI.second.Hotness != CalleeInfo::HotnessType::Unknown;
Teresa Johnson84174c32016-05-10 13:48:23 +00003549 if (HasProfileData)
3550 break;
3551 }
3552
3553 for (auto &EI : FS->calls()) {
3554 // If this GUID doesn't have a value id, it doesn't have a function
3555 // summary and we don't need to record any calls to it.
Dehao Chen4a435e02017-03-14 17:33:01 +00003556 GlobalValue::GUID GUID = EI.first.getGUID();
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003557 auto CallValueId = getValueId(GUID);
3558 if (!CallValueId) {
Dehao Chen4a435e02017-03-14 17:33:01 +00003559 // For SamplePGO, the indirect call targets for local functions will
3560 // have its original name annotated in profile. We try to find the
3561 // corresponding PGOFuncName as the GUID.
3562 GUID = Index.getGUIDFromOriginalID(GUID);
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003563 if (GUID == 0)
3564 continue;
3565 CallValueId = getValueId(GUID);
3566 if (!CallValueId)
Dehao Chen4a435e02017-03-14 17:33:01 +00003567 continue;
3568 }
Teresa Johnsona6a3fb52017-05-31 18:58:11 +00003569 NameVals.push_back(*CallValueId);
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);
Peter Collingbourne7c2c4092017-05-02 17:48:39 +00003582 });
Teresa Johnson403a7872015-10-04 14:33:43 +00003583
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
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +00003600 if (!Index.cfiFunctionDefs().empty()) {
3601 for (auto &S : Index.cfiFunctionDefs()) {
3602 NameVals.push_back(StrtabBuilder.add(S));
3603 NameVals.push_back(S.size());
3604 }
3605 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DEFS, NameVals);
3606 NameVals.clear();
3607 }
3608
3609 if (!Index.cfiFunctionDecls().empty()) {
3610 for (auto &S : Index.cfiFunctionDecls()) {
3611 NameVals.push_back(StrtabBuilder.add(S));
3612 NameVals.push_back(S.size());
3613 }
3614 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DECLS, NameVals);
3615 NameVals.clear();
3616 }
3617
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003618 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003619}
3620
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003621/// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
3622/// current llvm version, and a record for the epoch number.
Benjamin Kramerefcf06f2017-02-11 11:06:55 +00003623static void writeIdentificationBlock(BitstreamWriter &Stream) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003624 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
Mehdi Amini5d303282015-10-26 18:37:00 +00003625
3626 // Write the "user readable" string identifying the bitcode producer
David Blaikie7ad9dc12017-01-04 22:36:33 +00003627 auto Abbv = std::make_shared<BitCodeAbbrev>();
Mehdi Amini5d303282015-10-26 18:37:00 +00003628 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3629 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003631 auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003632 writeStringRecord(Stream, bitc::IDENTIFICATION_CODE_STRING,
Teresa Johnson37687f32016-04-23 04:30:47 +00003633 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
Mehdi Amini5d303282015-10-26 18:37:00 +00003634
3635 // Write the epoch version
David Blaikie7ad9dc12017-01-04 22:36:33 +00003636 Abbv = std::make_shared<BitCodeAbbrev>();
Mehdi Amini5d303282015-10-26 18:37:00 +00003637 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3638 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
David Blaikie7ad9dc12017-01-04 22:36:33 +00003639 auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
Mehdi Amini5d303282015-10-26 18:37:00 +00003640 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003641 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3642 Stream.ExitBlock();
Mehdi Amini5d303282015-10-26 18:37:00 +00003643}
3644
Teresa Johnson37687f32016-04-23 04:30:47 +00003645void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003646 // Emit the module's hash.
3647 // MODULE_CODE_HASH: [5*i32]
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +00003648 if (GenerateHash) {
3649 SHA1 Hasher;
3650 uint32_t Vals[5];
3651 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
3652 Buffer.size() - BlockStartPos));
3653 StringRef Hash = Hasher.result();
3654 for (int Pos = 0; Pos < 20; Pos += 4) {
3655 Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos);
3656 }
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003657
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +00003658 // Emit the finished record.
3659 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
3660
3661 if (ModHash)
3662 // Save the written hash value.
3663 std::copy(std::begin(Vals), std::end(Vals), std::begin(*ModHash));
3664 } else if (ModHash)
3665 Stream.EmitRecord(bitc::MODULE_CODE_HASH, ArrayRef<uint32_t>(*ModHash));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003666}
3667
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003668void ModuleBitcodeWriter::write() {
3669 writeIdentificationBlock(Stream);
Teresa Johnson37687f32016-04-23 04:30:47 +00003670
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003671 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3672 size_t BlockStartPos = Buffer.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003673
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003674 writeModuleVersion();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003675
3676 // Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003677 writeBlockInfo();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003678
Bill Wendlingdc095552013-02-10 23:09:32 +00003679 // Emit information about attribute groups.
Teresa Johnson37687f32016-04-23 04:30:47 +00003680 writeAttributeGroupTable();
Bill Wendlingdc095552013-02-10 23:09:32 +00003681
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003682 // Emit information about parameter attributes.
Teresa Johnson37687f32016-04-23 04:30:47 +00003683 writeAttributeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003684
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003685 // Emit information describing all of the types in the module.
Teresa Johnson37687f32016-04-23 04:30:47 +00003686 writeTypeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003687
Teresa Johnson37687f32016-04-23 04:30:47 +00003688 writeComdats();
David Majnemerdad0a642014-06-27 18:19:56 +00003689
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003690 // Emit top-level description of module, including target triple, inline asm,
3691 // descriptors for global variables, and function prototype info.
Teresa Johnson37687f32016-04-23 04:30:47 +00003692 writeModuleInfo();
Devang Patel7428d8a2009-07-22 17:43:22 +00003693
Devang Patele059ba6e2009-07-23 01:07:34 +00003694 // Emit constants.
Teresa Johnson37687f32016-04-23 04:30:47 +00003695 writeModuleConstants();
Devang Patele059ba6e2009-07-23 01:07:34 +00003696
Peter Collingbourne21521892016-06-21 23:42:48 +00003697 // Emit metadata kind names.
3698 writeModuleMetadataKinds();
Devang Patel8cca7b42009-08-04 05:01:35 +00003699
Devang Patelaf206b82009-09-18 19:26:43 +00003700 // Emit metadata.
Peter Collingbourne21521892016-06-21 23:42:48 +00003701 writeModuleMetadata();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003702
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003703 // Emit module-level use-lists.
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003704 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003705 writeUseListBlock(nullptr);
Chad Rosierca2567b2011-12-07 21:44:12 +00003706
Teresa Johnson37687f32016-04-23 04:30:47 +00003707 writeOperandBundleTags();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003708
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003709 // Emit function bodies.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003710 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003711 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003712 if (!F->isDeclaration())
Teresa Johnson37687f32016-04-23 04:30:47 +00003713 writeFunction(*F, FunctionToBitcodeIndex);
Teresa Johnson403a7872015-10-04 14:33:43 +00003714
3715 // Need to write after the above call to WriteFunction which populates
3716 // the summary information in the index.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003717 if (Index)
Teresa Johnson37687f32016-04-23 04:30:47 +00003718 writePerModuleGlobalValueSummary();
Teresa Johnsonff642b92015-09-17 20:12:00 +00003719
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003720 writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003721
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +00003722 writeModuleHash(BlockStartPos);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003723
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003724 Stream.ExitBlock();
Chris Lattner702658c2007-05-04 18:26:27 +00003725}
3726
Teresa Johnson37687f32016-04-23 04:30:47 +00003727static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3728 uint32_t &Position) {
3729 support::endian::write32le(&Buffer[Position], Value);
3730 Position += 4;
3731}
3732
3733/// If generating a bc file on darwin, we have to emit a
Chris Lattnera660f4b2008-07-09 05:14:23 +00003734/// header and trailer to make it compatible with the system archiver. To do
3735/// this we emit the following header, and then emit a trailer that pads the
3736/// file out to be a multiple of 16 bytes.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003737///
Chris Lattnera660f4b2008-07-09 05:14:23 +00003738/// struct bc_header {
3739/// uint32_t Magic; // 0x0B17C0DE
3740/// uint32_t Version; // Version, currently always 0.
3741/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3742/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3743/// uint32_t CPUType; // CPU specifier.
3744/// ... potentially more later ...
3745/// };
Teresa Johnson37687f32016-04-23 04:30:47 +00003746static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003747 const Triple &TT) {
Chris Lattnera660f4b2008-07-09 05:14:23 +00003748 unsigned CPUType = ~0U;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003749
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003750 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng545d3602010-02-12 20:39:35 +00003751 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3752 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3753 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattnera660f4b2008-07-09 05:14:23 +00003754 enum {
3755 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3756 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003757 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattnera660f4b2008-07-09 05:14:23 +00003758 DARWIN_CPU_TYPE_POWERPC = 18
3759 };
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003760
Evan Chengcffdcae2011-06-14 01:51:33 +00003761 Triple::ArchType Arch = TT.getArch();
3762 if (Arch == Triple::x86_64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003763 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003764 else if (Arch == Triple::x86)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003765 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chengcffdcae2011-06-14 01:51:33 +00003766 else if (Arch == Triple::ppc)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003767 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chengcffdcae2011-06-14 01:51:33 +00003768 else if (Arch == Triple::ppc64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003769 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003770 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003771 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003772
Chris Lattnera660f4b2008-07-09 05:14:23 +00003773 // Traditional Bitcode starts after header.
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003774 assert(Buffer.size() >= BWH_HeaderSize &&
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003775 "Expected header size to be reserved");
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003776 unsigned BCOffset = BWH_HeaderSize;
3777 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003778
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003779 // Write the magic and version.
3780 unsigned Position = 0;
Teresa Johnson37687f32016-04-23 04:30:47 +00003781 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
3782 writeInt32ToBuffer(0, Buffer, Position); // Version.
3783 writeInt32ToBuffer(BCOffset, Buffer, Position);
3784 writeInt32ToBuffer(BCSize, Buffer, Position);
3785 writeInt32ToBuffer(CPUType, Buffer, Position);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003786
Chris Lattnera660f4b2008-07-09 05:14:23 +00003787 // If the file is not a multiple of 16 bytes, insert dummy padding.
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003788 while (Buffer.size() & 15)
3789 Buffer.push_back(0);
Chris Lattnera660f4b2008-07-09 05:14:23 +00003790}
3791
Teresa Johnson403a7872015-10-04 14:33:43 +00003792/// Helper to write the header common to all bitcode files.
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003793static void writeBitcodeHeader(BitstreamWriter &Stream) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003794 // Emit the file header.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003795 Stream.Emit((unsigned)'B', 8);
3796 Stream.Emit((unsigned)'C', 8);
3797 Stream.Emit(0x0, 4);
3798 Stream.Emit(0xC, 4);
3799 Stream.Emit(0xE, 4);
3800 Stream.Emit(0xD, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00003801}
3802
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003803BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
3804 : Buffer(Buffer), Stream(new BitstreamWriter(Buffer)) {
3805 writeBitcodeHeader(*Stream);
3806}
3807
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003808BitcodeWriter::~BitcodeWriter() { assert(WroteStrtab); }
3809
3810void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
3811 Stream->EnterSubblock(Block, 3);
3812
3813 auto Abbv = std::make_shared<BitCodeAbbrev>();
3814 Abbv->Add(BitCodeAbbrevOp(Record));
3815 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3816 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
3817
3818 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
3819
3820 Stream->ExitBlock();
3821}
3822
3823void BitcodeWriter::writeStrtab() {
3824 assert(!WroteStrtab);
3825
3826 std::vector<char> Strtab;
3827 StrtabBuilder.finalizeInOrder();
3828 Strtab.resize(StrtabBuilder.getSize());
3829 StrtabBuilder.write((uint8_t *)Strtab.data());
3830
3831 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB,
3832 {Strtab.data(), Strtab.size()});
3833
3834 WroteStrtab = true;
3835}
3836
3837void BitcodeWriter::copyStrtab(StringRef Strtab) {
3838 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
3839 WroteStrtab = true;
3840}
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003841
3842void BitcodeWriter::writeModule(const Module *M,
3843 bool ShouldPreserveUseListOrder,
3844 const ModuleSummaryIndex *Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +00003845 bool GenerateHash, ModuleHash *ModHash) {
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003846 ModuleBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +00003847 ShouldPreserveUseListOrder, Index,
3848 GenerateHash, ModHash);
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003849 ModuleWriter.write();
3850}
3851
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +00003852void BitcodeWriter::writeIndex(
3853 const ModuleSummaryIndex *Index,
3854 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
3855 IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index,
3856 ModuleToSummariesForIndex);
3857 IndexWriter.write();
3858}
3859
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003860/// WriteBitcodeToFile - Write the specified module to the specified output
3861/// stream.
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +00003862void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
Teresa Johnson403a7872015-10-04 14:33:43 +00003863 bool ShouldPreserveUseListOrder,
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003864 const ModuleSummaryIndex *Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +00003865 bool GenerateHash, ModuleHash *ModHash) {
Michael Ilsemane26658d2012-12-03 21:29:36 +00003866 SmallVector<char, 0> Buffer;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003867 Buffer.reserve(256*1024);
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003868
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003869 // If this is darwin or another generic macho target, reserve space for the
3870 // header.
3871 Triple TT(M->getTargetTriple());
Akira Hatanaka1235d282016-01-23 16:02:10 +00003872 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003873 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003874
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003875 BitcodeWriter Writer(Buffer);
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +00003876 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
3877 ModHash);
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003878 Writer.writeStrtab();
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003879
Akira Hatanaka1235d282016-01-23 16:02:10 +00003880 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Teresa Johnson37687f32016-04-23 04:30:47 +00003881 emitDarwinBCHeaderAndTrailer(Buffer, TT);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003882
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003883 // Write the generated bitstream to "Out".
3884 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003885}
Teresa Johnson403a7872015-10-04 14:33:43 +00003886
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00003887void IndexBitcodeWriter::write() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003888 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Teresa Johnson37687f32016-04-23 04:30:47 +00003889
Peter Collingbournea0f371a2017-04-17 17:51:36 +00003890 writeModuleVersion();
Teresa Johnson37687f32016-04-23 04:30:47 +00003891
3892 // Write the module paths in the combined index.
3893 writeModStrings();
3894
3895 // Write the summary combined index records.
3896 writeCombinedGlobalValueSummary();
3897
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003898 Stream.ExitBlock();
Teresa Johnson37687f32016-04-23 04:30:47 +00003899}
3900
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003901// Write the specified module summary index to the given raw output stream,
Teresa Johnson403a7872015-10-04 14:33:43 +00003902// where it will be written in a new bitcode block. This is used when
Teresa Johnson84174c32016-05-10 13:48:23 +00003903// writing the combined index file for ThinLTO. When writing a subset of the
3904// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
3905void llvm::WriteIndexToFile(
3906 const ModuleSummaryIndex &Index, raw_ostream &Out,
Mehdi Aminicc1fe9b2016-08-19 06:06:18 +00003907 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003908 SmallVector<char, 0> Buffer;
3909 Buffer.reserve(256 * 1024);
3910
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +00003911 BitcodeWriter Writer(Buffer);
3912 Writer.writeIndex(&Index, ModuleToSummariesForIndex);
3913 Writer.writeStrtab();
Teresa Johnson403a7872015-10-04 14:33:43 +00003914
3915 Out.write((char *)&Buffer.front(), Buffer.size());
3916}