blob: 71905364c1d97574d1b20521f836e997ff23322d [file] [log] [blame]
Chris Lattner87351e22007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerc1d10d62007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "ValueEnumerator.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000015#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000017#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner362b4a12007-04-23 01:01:37 +000018#include "llvm/Bitcode/LLVMBitCodes.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000019#include "llvm/Bitcode/ReaderWriter.h"
Sanjoy Dasb513a9f2015-09-24 23:34:52 +000020#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000022#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/InlineAsm.h"
25#include "llvm/IR/Instructions.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000026#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
28#include "llvm/IR/Operator.h"
Duncan P. N. Exon Smith6b6fdc92014-07-25 14:49:26 +000029#include "llvm/IR/UseListOrder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000032#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000033#include "llvm/Support/Program.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000034#include "llvm/Support/SHA1.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000035#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000036#include <cctype>
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000037#include <map>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000038using namespace llvm;
39
Chris Lattner4d925982007-05-04 20:52:02 +000040/// These are manifest constants used by the bitcode writer. They do not need to
41/// be kept in sync with the reader, but need to be consistent within this file.
42enum {
Chris Lattner4d925982007-05-04 20:52:02 +000043 // VALUE_SYMTAB_BLOCK abbrev id's.
44 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerfd1ad102007-05-04 20:58:35 +000045 VST_ENTRY_7_ABBREV,
Chris Lattnere760d6f2007-05-05 01:26:50 +000046 VST_ENTRY_6_ABBREV,
Chris Lattnerda5e5d22007-05-05 07:36:14 +000047 VST_BBENTRY_6_ABBREV,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000048
Chris Lattnerda5e5d22007-05-05 07:36:14 +000049 // CONSTANTS_BLOCK abbrev id's.
50 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
51 CONSTANTS_INTEGER_ABBREV,
52 CONSTANTS_CE_CAST_Abbrev,
Chris Lattnerb80751d2007-05-05 07:44:49 +000053 CONSTANTS_NULL_Abbrev,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000054
Chris Lattnerb80751d2007-05-05 07:44:49 +000055 // FUNCTION_BLOCK abbrev id's.
Chris Lattnercc6d4c92007-05-06 01:28:01 +000056 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000057 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman0ebd6962009-07-20 21:19:07 +000058 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000059 FUNCTION_INST_CAST_ABBREV,
Chris Lattnercc6d4c92007-05-06 01:28:01 +000060 FUNCTION_INST_RET_VOID_ABBREV,
61 FUNCTION_INST_RET_VAL_ABBREV,
David Blaikieb5b5efd2015-02-25 01:08:52 +000062 FUNCTION_INST_UNREACHABLE_ABBREV,
63 FUNCTION_INST_GEP_ABBREV,
Chris Lattner4d925982007-05-04 20:52:02 +000064};
65
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000066/// Abstract class to manage the bitcode writing, subclassed for each bitcode
67/// file type. Owns the BitstreamWriter, and includes the main entry point for
Teresa Johnson37687f32016-04-23 04:30:47 +000068/// writing.
69class BitcodeWriter {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000070protected:
Teresa Johnson37687f32016-04-23 04:30:47 +000071 /// Pointer to the buffer allocated by caller for bitcode writing.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000072 const SmallVectorImpl<char> &Buffer;
Teresa Johnson37687f32016-04-23 04:30:47 +000073
74 /// The stream created and owned by the BitodeWriter.
75 BitstreamWriter Stream;
76
77 /// Saves the offset of the VSTOffset record that must eventually be
78 /// backpatched with the offset of the actual VST.
79 uint64_t VSTOffsetPlaceholder = 0;
80
81public:
82 /// Constructs a BitcodeWriter object, and initializes a BitstreamRecord,
83 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000084 BitcodeWriter(SmallVectorImpl<char> &Buffer)
85 : Buffer(Buffer), Stream(Buffer) {}
Teresa Johnson37687f32016-04-23 04:30:47 +000086
87 virtual ~BitcodeWriter() = default;
88
89 /// Main entry point to write the bitcode file, which writes the bitcode
90 /// header and will then invoke the virtual writeBlocks() method.
91 void write();
92
93private:
94 /// Derived classes must implement this to write the corresponding blocks for
95 /// that bitcode file type.
96 virtual void writeBlocks() = 0;
97
98protected:
99 bool hasVSTOffsetPlaceholder() { return VSTOffsetPlaceholder != 0; }
Teresa Johnson37687f32016-04-23 04:30:47 +0000100 void writeValueSymbolTableForwardDecl();
101 void writeBitcodeHeader();
102};
103
104/// Class to manage the bitcode writing for a module.
105class ModuleBitcodeWriter : public BitcodeWriter {
106 /// The Module to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000107 const Module &M;
Teresa Johnson37687f32016-04-23 04:30:47 +0000108
109 /// Enumerates ids for all values in the module.
110 ValueEnumerator VE;
111
112 /// Optional per-module index to write for ThinLTO.
113 const ModuleSummaryIndex *Index;
114
115 /// True if a module hash record should be written.
116 bool GenerateHash;
117
118 /// The start bit of the module block, for use in generating a module hash
119 uint64_t BitcodeStartBit = 0;
120
121public:
122 /// Constructs a ModuleBitcodeWriter object for the given Module,
123 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000124 ModuleBitcodeWriter(const Module *M, SmallVectorImpl<char> &Buffer,
Teresa Johnson37687f32016-04-23 04:30:47 +0000125 bool ShouldPreserveUseListOrder,
126 const ModuleSummaryIndex *Index, bool GenerateHash)
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000127 : BitcodeWriter(Buffer), M(*M), VE(*M, ShouldPreserveUseListOrder),
Teresa Johnson37687f32016-04-23 04:30:47 +0000128 Index(Index), GenerateHash(GenerateHash) {
129 // Save the start bit of the actual bitcode, in case there is space
130 // saved at the start for the darwin header above. The reader stream
131 // will start at the bitcode, and we need the offset of the VST
132 // to line up.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000133 BitcodeStartBit = Stream.GetCurrentBitNo();
Teresa Johnson37687f32016-04-23 04:30:47 +0000134 }
135
136private:
137 /// Main entry point for writing a module to bitcode, invoked by
138 /// BitcodeWriter::write() after it writes the header.
139 void writeBlocks() override;
140
141 /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
142 /// current llvm version, and a record for the epoch number.
143 void writeIdentificationBlock();
144
145 /// Emit the current module to the bitstream.
146 void writeModule();
147
148 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
149
150 void writeStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse);
151 void writeAttributeGroupTable();
152 void writeAttributeTable();
153 void writeTypeTable();
154 void writeComdats();
155 void writeModuleInfo();
156 void writeValueAsMetadata(const ValueAsMetadata *MD,
157 SmallVectorImpl<uint64_t> &Record);
158 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
159 unsigned Abbrev);
160 unsigned createDILocationAbbrev();
161 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
162 unsigned &Abbrev);
163 unsigned createGenericDINodeAbbrev();
164 void writeGenericDINode(const GenericDINode *N,
165 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
166 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
167 unsigned Abbrev);
168 void writeDIEnumerator(const DIEnumerator *N,
169 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
170 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
171 unsigned Abbrev);
172 void writeDIDerivedType(const DIDerivedType *N,
173 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
174 void writeDICompositeType(const DICompositeType *N,
175 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
176 void writeDISubroutineType(const DISubroutineType *N,
177 SmallVectorImpl<uint64_t> &Record,
178 unsigned Abbrev);
179 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
180 unsigned Abbrev);
181 void writeDICompileUnit(const DICompileUnit *N,
182 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
183 void writeDISubprogram(const DISubprogram *N,
184 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
185 void writeDILexicalBlock(const DILexicalBlock *N,
186 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
187 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
188 SmallVectorImpl<uint64_t> &Record,
189 unsigned Abbrev);
190 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
191 unsigned Abbrev);
192 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
193 unsigned Abbrev);
194 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
195 unsigned Abbrev);
196 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
197 unsigned Abbrev);
198 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
199 SmallVectorImpl<uint64_t> &Record,
200 unsigned Abbrev);
201 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
202 SmallVectorImpl<uint64_t> &Record,
203 unsigned Abbrev);
204 void writeDIGlobalVariable(const DIGlobalVariable *N,
205 SmallVectorImpl<uint64_t> &Record,
206 unsigned Abbrev);
207 void writeDILocalVariable(const DILocalVariable *N,
208 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
209 void writeDIExpression(const DIExpression *N,
210 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
211 void writeDIObjCProperty(const DIObjCProperty *N,
212 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
213 void writeDIImportedEntity(const DIImportedEntity *N,
214 SmallVectorImpl<uint64_t> &Record,
215 unsigned Abbrev);
216 unsigned createNamedMetadataAbbrev();
217 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
218 unsigned createMetadataStringsAbbrev();
219 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
220 SmallVectorImpl<uint64_t> &Record);
221 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
222 SmallVectorImpl<uint64_t> &Record);
223 void writeModuleMetadata();
224 void writeFunctionMetadata(const Function &F);
225 void writeMetadataAttachment(const Function &F);
226 void writeModuleMetadataStore();
227 void writeOperandBundleTags();
228 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
229 void writeModuleConstants();
230 bool pushValueAndType(const Value *V, unsigned InstID,
231 SmallVectorImpl<unsigned> &Vals);
232 void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
233 void pushValue(const Value *V, unsigned InstID,
234 SmallVectorImpl<unsigned> &Vals);
235 void pushValueSigned(const Value *V, unsigned InstID,
236 SmallVectorImpl<uint64_t> &Vals);
237 void writeInstruction(const Instruction &I, unsigned InstID,
238 SmallVectorImpl<unsigned> &Vals);
239 void writeValueSymbolTable(
240 const ValueSymbolTable &VST, bool IsModuleLevel = false,
241 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex = nullptr);
242 void writeUseList(UseListOrder &&Order);
243 void writeUseListBlock(const Function *F);
244 void
245 writeFunction(const Function &F,
246 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
247 void writeBlockInfo();
248 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
249 GlobalValueInfo *Info,
250 unsigned ValueID,
251 unsigned FSCallsAbbrev,
252 unsigned FSCallsProfileAbbrev,
253 const Function &F);
254 void writeModuleLevelReferences(const GlobalVariable &V,
255 SmallVector<uint64_t, 64> &NameVals,
256 unsigned FSModRefsAbbrev);
257 void writePerModuleGlobalValueSummary();
258 void writeModuleHash(size_t BlockStartPos);
259};
260
261/// Class to manage the bitcode writing for a combined index.
262class IndexBitcodeWriter : public BitcodeWriter {
263 /// The combined index to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000264 const ModuleSummaryIndex &Index;
Teresa Johnson37687f32016-04-23 04:30:47 +0000265
266 /// Map that holds the correspondence between the GUID used in the combined
267 /// index and a value id generated by this class to use in references.
268 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
269
270 /// Tracks the last value id recorded in the GUIDToValueMap.
271 unsigned GlobalValueId = 0;
272
273public:
274 /// Constructs a IndexBitcodeWriter object for the given combined index,
275 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000276 IndexBitcodeWriter(SmallVectorImpl<char> &Buffer,
277 const ModuleSummaryIndex &Index)
Teresa Johnson37687f32016-04-23 04:30:47 +0000278 : BitcodeWriter(Buffer), Index(Index) {
279 // Assign unique value ids to all functions in the index for use
280 // in writing out the call graph edges. Save the mapping from GUID
281 // to the new global value id to use when writing those edges, which
282 // are currently saved in the index in terms of GUID.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000283 for (auto &II : Index)
Teresa Johnson37687f32016-04-23 04:30:47 +0000284 GUIDToValueIdMap[II.first] = ++GlobalValueId;
285 }
286
287private:
288 /// Main entry point for writing a combined index to bitcode, invoked by
289 /// BitcodeWriter::write() after it writes the header.
290 void writeBlocks() override;
291
292 void writeIndex();
293 void writeModStrings();
294 void writeCombinedValueSymbolTable();
295 void writeCombinedGlobalValueSummary();
296
297 bool hasValueId(GlobalValue::GUID ValGUID) {
298 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
299 return VMI != GUIDToValueIdMap.end();
300 }
301 unsigned getValueId(GlobalValue::GUID ValGUID) {
302 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
303 // If this GUID doesn't have an entry, assign one.
304 if (VMI == GUIDToValueIdMap.end()) {
305 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
306 return GlobalValueId;
307 } else {
308 return VMI->second;
309 }
310 }
311 unsigned popValueId(GlobalValue::GUID ValGUID) {
312 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
313 assert(VMI != GUIDToValueIdMap.end());
314 unsigned ValueId = VMI->second;
315 GUIDToValueIdMap.erase(VMI);
316 return ValueId;
317 }
318 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
319};
320
321static unsigned getEncodedCastOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000322 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000323 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000324 case Instruction::Trunc : return bitc::CAST_TRUNC;
325 case Instruction::ZExt : return bitc::CAST_ZEXT;
326 case Instruction::SExt : return bitc::CAST_SEXT;
327 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
328 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
329 case Instruction::UIToFP : return bitc::CAST_UITOFP;
330 case Instruction::SIToFP : return bitc::CAST_SITOFP;
331 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
332 case Instruction::FPExt : return bitc::CAST_FPEXT;
333 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
334 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
335 case Instruction::BitCast : return bitc::CAST_BITCAST;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000336 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000337 }
338}
339
Teresa Johnson37687f32016-04-23 04:30:47 +0000340static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000341 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000342 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmana5b96452009-06-04 22:49:04 +0000343 case Instruction::Add:
344 case Instruction::FAdd: return bitc::BINOP_ADD;
345 case Instruction::Sub:
346 case Instruction::FSub: return bitc::BINOP_SUB;
347 case Instruction::Mul:
348 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000349 case Instruction::UDiv: return bitc::BINOP_UDIV;
350 case Instruction::FDiv:
351 case Instruction::SDiv: return bitc::BINOP_SDIV;
352 case Instruction::URem: return bitc::BINOP_UREM;
353 case Instruction::FRem:
354 case Instruction::SRem: return bitc::BINOP_SREM;
355 case Instruction::Shl: return bitc::BINOP_SHL;
356 case Instruction::LShr: return bitc::BINOP_LSHR;
357 case Instruction::AShr: return bitc::BINOP_ASHR;
358 case Instruction::And: return bitc::BINOP_AND;
359 case Instruction::Or: return bitc::BINOP_OR;
360 case Instruction::Xor: return bitc::BINOP_XOR;
361 }
362}
363
Teresa Johnson37687f32016-04-23 04:30:47 +0000364static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000365 switch (Op) {
366 default: llvm_unreachable("Unknown RMW operation!");
367 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
368 case AtomicRMWInst::Add: return bitc::RMW_ADD;
369 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
370 case AtomicRMWInst::And: return bitc::RMW_AND;
371 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
372 case AtomicRMWInst::Or: return bitc::RMW_OR;
373 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
374 case AtomicRMWInst::Max: return bitc::RMW_MAX;
375 case AtomicRMWInst::Min: return bitc::RMW_MIN;
376 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
377 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
378 }
379}
380
Teresa Johnson37687f32016-04-23 04:30:47 +0000381static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000382 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +0000383 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
384 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
385 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
386 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
387 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
388 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
389 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
Eli Friedmanfee02c62011-07-25 23:16:38 +0000390 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000391 llvm_unreachable("Invalid ordering");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000392}
393
Teresa Johnson37687f32016-04-23 04:30:47 +0000394static unsigned getEncodedSynchScope(SynchronizationScope SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000395 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000396 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
397 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
398 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000399 llvm_unreachable("Invalid synch scope");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000400}
401
Teresa Johnson37687f32016-04-23 04:30:47 +0000402void ModuleBitcodeWriter::writeStringRecord(unsigned Code, StringRef Str,
403 unsigned AbbrevToUse) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000404 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000405
Chris Lattnere14cb882007-05-04 19:11:41 +0000406 // Code: [strchar x N]
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000407 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
408 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
409 AbbrevToUse = 0;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000410 Vals.push_back(Str[i]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000411 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000412
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000413 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000414 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000415}
416
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000417static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
418 switch (Kind) {
419 case Attribute::Alignment:
420 return bitc::ATTR_KIND_ALIGNMENT;
George Burgess IV278199f2016-04-12 01:05:35 +0000421 case Attribute::AllocSize:
422 return bitc::ATTR_KIND_ALLOC_SIZE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000423 case Attribute::AlwaysInline:
424 return bitc::ATTR_KIND_ALWAYS_INLINE;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000425 case Attribute::ArgMemOnly:
426 return bitc::ATTR_KIND_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000427 case Attribute::Builtin:
428 return bitc::ATTR_KIND_BUILTIN;
429 case Attribute::ByVal:
430 return bitc::ATTR_KIND_BY_VAL;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000431 case Attribute::Convergent:
432 return bitc::ATTR_KIND_CONVERGENT;
Reid Klecknera534a382013-12-19 02:14:12 +0000433 case Attribute::InAlloca:
434 return bitc::ATTR_KIND_IN_ALLOCA;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000435 case Attribute::Cold:
436 return bitc::ATTR_KIND_COLD;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000437 case Attribute::InaccessibleMemOnly:
438 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
439 case Attribute::InaccessibleMemOrArgMemOnly:
440 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000441 case Attribute::InlineHint:
442 return bitc::ATTR_KIND_INLINE_HINT;
443 case Attribute::InReg:
444 return bitc::ATTR_KIND_IN_REG;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000445 case Attribute::JumpTable:
446 return bitc::ATTR_KIND_JUMP_TABLE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000447 case Attribute::MinSize:
448 return bitc::ATTR_KIND_MIN_SIZE;
449 case Attribute::Naked:
450 return bitc::ATTR_KIND_NAKED;
451 case Attribute::Nest:
452 return bitc::ATTR_KIND_NEST;
453 case Attribute::NoAlias:
454 return bitc::ATTR_KIND_NO_ALIAS;
455 case Attribute::NoBuiltin:
456 return bitc::ATTR_KIND_NO_BUILTIN;
457 case Attribute::NoCapture:
458 return bitc::ATTR_KIND_NO_CAPTURE;
459 case Attribute::NoDuplicate:
460 return bitc::ATTR_KIND_NO_DUPLICATE;
461 case Attribute::NoImplicitFloat:
462 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
463 case Attribute::NoInline:
464 return bitc::ATTR_KIND_NO_INLINE;
James Molloye6f87ca2015-11-06 10:32:53 +0000465 case Attribute::NoRecurse:
466 return bitc::ATTR_KIND_NO_RECURSE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000467 case Attribute::NonLazyBind:
468 return bitc::ATTR_KIND_NON_LAZY_BIND;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000469 case Attribute::NonNull:
470 return bitc::ATTR_KIND_NON_NULL;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000471 case Attribute::Dereferenceable:
472 return bitc::ATTR_KIND_DEREFERENCEABLE;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000473 case Attribute::DereferenceableOrNull:
474 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000475 case Attribute::NoRedZone:
476 return bitc::ATTR_KIND_NO_RED_ZONE;
477 case Attribute::NoReturn:
478 return bitc::ATTR_KIND_NO_RETURN;
479 case Attribute::NoUnwind:
480 return bitc::ATTR_KIND_NO_UNWIND;
481 case Attribute::OptimizeForSize:
482 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000483 case Attribute::OptimizeNone:
484 return bitc::ATTR_KIND_OPTIMIZE_NONE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000485 case Attribute::ReadNone:
486 return bitc::ATTR_KIND_READ_NONE;
487 case Attribute::ReadOnly:
488 return bitc::ATTR_KIND_READ_ONLY;
489 case Attribute::Returned:
490 return bitc::ATTR_KIND_RETURNED;
491 case Attribute::ReturnsTwice:
492 return bitc::ATTR_KIND_RETURNS_TWICE;
493 case Attribute::SExt:
494 return bitc::ATTR_KIND_S_EXT;
495 case Attribute::StackAlignment:
496 return bitc::ATTR_KIND_STACK_ALIGNMENT;
497 case Attribute::StackProtect:
498 return bitc::ATTR_KIND_STACK_PROTECT;
499 case Attribute::StackProtectReq:
500 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
501 case Attribute::StackProtectStrong:
502 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000503 case Attribute::SafeStack:
504 return bitc::ATTR_KIND_SAFESTACK;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000505 case Attribute::StructRet:
506 return bitc::ATTR_KIND_STRUCT_RET;
507 case Attribute::SanitizeAddress:
508 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
509 case Attribute::SanitizeThread:
510 return bitc::ATTR_KIND_SANITIZE_THREAD;
511 case Attribute::SanitizeMemory:
512 return bitc::ATTR_KIND_SANITIZE_MEMORY;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000513 case Attribute::SwiftError:
514 return bitc::ATTR_KIND_SWIFT_ERROR;
Manman Renf46262e2016-03-29 17:37:21 +0000515 case Attribute::SwiftSelf:
516 return bitc::ATTR_KIND_SWIFT_SELF;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000517 case Attribute::UWTable:
518 return bitc::ATTR_KIND_UW_TABLE;
519 case Attribute::ZExt:
520 return bitc::ATTR_KIND_Z_EXT;
521 case Attribute::EndAttrKinds:
522 llvm_unreachable("Can not encode end-attribute kinds marker.");
523 case Attribute::None:
524 llvm_unreachable("Can not encode none-attribute.");
525 }
526
527 llvm_unreachable("Trying to encode unknown attribute");
528}
529
Teresa Johnson37687f32016-04-23 04:30:47 +0000530void ModuleBitcodeWriter::writeAttributeGroupTable() {
Bill Wendling92ed7002013-02-11 22:33:26 +0000531 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
532 if (AttrGrps.empty()) return;
Bill Wendlingdc095552013-02-10 23:09:32 +0000533
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000534 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
Bill Wendlingdc095552013-02-10 23:09:32 +0000535
536 SmallVector<uint64_t, 64> Record;
Bill Wendling92ed7002013-02-11 22:33:26 +0000537 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
538 AttributeSet AS = AttrGrps[i];
Bill Wendlingdc095552013-02-10 23:09:32 +0000539 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
540 AttributeSet A = AS.getSlotAttributes(i);
541
Bill Wendling92ed7002013-02-11 22:33:26 +0000542 Record.push_back(VE.getAttributeGroupID(A));
Bill Wendlingdc095552013-02-10 23:09:32 +0000543 Record.push_back(AS.getSlotIndex(i));
544
545 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
546 I != E; ++I) {
547 Attribute Attr = *I;
548 if (Attr.isEnumAttribute()) {
549 Record.push_back(0);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000550 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Hal Finkele15442c2014-07-18 06:51:55 +0000551 } else if (Attr.isIntAttribute()) {
Bill Wendlingdc095552013-02-10 23:09:32 +0000552 Record.push_back(1);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000553 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Bill Wendlingdc095552013-02-10 23:09:32 +0000554 Record.push_back(Attr.getValueAsInt());
555 } else {
556 StringRef Kind = Attr.getKindAsString();
557 StringRef Val = Attr.getValueAsString();
558
559 Record.push_back(Val.empty() ? 3 : 4);
560 Record.append(Kind.begin(), Kind.end());
561 Record.push_back(0);
562 if (!Val.empty()) {
563 Record.append(Val.begin(), Val.end());
564 Record.push_back(0);
565 }
566 }
567 }
568
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000569 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
Bill Wendlingdc095552013-02-10 23:09:32 +0000570 Record.clear();
571 }
572 }
573
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000574 Stream.ExitBlock();
Bill Wendlingdc095552013-02-10 23:09:32 +0000575}
576
Teresa Johnson37687f32016-04-23 04:30:47 +0000577void ModuleBitcodeWriter::writeAttributeTable() {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000578 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000579 if (Attrs.empty()) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000580
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000581 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000582
583 SmallVector<uint64_t, 64> Record;
584 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000585 const AttributeSet &A = Attrs[i];
Bill Wendling0dc08912013-02-12 08:13:50 +0000586 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
587 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000588
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000589 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000590 Record.clear();
591 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000592
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000593 Stream.ExitBlock();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000594}
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000595
596/// WriteTypeTable - Write out the type table for a module.
Teresa Johnson37687f32016-04-23 04:30:47 +0000597void ModuleBitcodeWriter::writeTypeTable() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000598 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000599
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000600 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000601 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000602
David Blaikie7b028102015-02-25 00:51:52 +0000603 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
Chad Rosier9646c0d2011-12-08 00:38:45 +0000604
Chris Lattnerccee7062007-05-05 06:30:12 +0000605 // Abbrev for TYPE_CODE_POINTER.
606 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
607 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000608 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
Christopher Lamb25f50762007-12-12 08:44:39 +0000609 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000610 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000611
Chris Lattnerccee7062007-05-05 06:30:12 +0000612 // Abbrev for TYPE_CODE_FUNCTION.
613 Abbv = new BitCodeAbbrev();
614 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
615 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnerccee7062007-05-05 06:30:12 +0000616 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000617 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
618
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000619 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000620
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000621 // Abbrev for TYPE_CODE_STRUCT_ANON.
Chris Lattnerccee7062007-05-05 06:30:12 +0000622 Abbv = new BitCodeAbbrev();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000623 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
Chris Lattnerccee7062007-05-05 06:30:12 +0000624 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
625 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000626 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
627
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000628 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000629
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000630 // Abbrev for TYPE_CODE_STRUCT_NAME.
631 Abbv = new BitCodeAbbrev();
632 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
633 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
634 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000635 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000636
637 // Abbrev for TYPE_CODE_STRUCT_NAMED.
638 Abbv = new BitCodeAbbrev();
639 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
640 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
641 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000642 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
643
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000644 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000645
Chris Lattnerccee7062007-05-05 06:30:12 +0000646 // Abbrev for TYPE_CODE_ARRAY.
647 Abbv = new BitCodeAbbrev();
648 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
649 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
Chad Rosier9646c0d2011-12-08 00:38:45 +0000650 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
651
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000652 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000653
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000654 // Emit an entry count so the reader can reserve space.
655 TypeVals.push_back(TypeList.size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000656 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000657 TypeVals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000658
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000659 // Loop over all of the types, emitting each in turn.
660 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000661 Type *T = TypeList[i];
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000662 int AbbrevToUse = 0;
663 unsigned Code = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000664
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000665 switch (T->getTypeID()) {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000666 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
667 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
668 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
669 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
670 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
671 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000672 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000673 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
674 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
675 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
David Majnemerb611e3f2015-08-14 05:09:07 +0000676 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000677 case Type::IntegerTyID:
678 // INTEGER: [width]
679 Code = bitc::TYPE_CODE_INTEGER;
680 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
681 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000682 case Type::PointerTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000683 PointerType *PTy = cast<PointerType>(T);
Christopher Lamb25f50762007-12-12 08:44:39 +0000684 // POINTER: [pointee type, address space]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000685 Code = bitc::TYPE_CODE_POINTER;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000686 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lamb25f50762007-12-12 08:44:39 +0000687 unsigned AddressSpace = PTy->getAddressSpace();
688 TypeVals.push_back(AddressSpace);
689 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000690 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000691 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000692 case Type::FunctionTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000693 FunctionType *FT = cast<FunctionType>(T);
Chad Rosier95898722011-11-03 00:14:01 +0000694 // FUNCTION: [isvararg, retty, paramty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000695 Code = bitc::TYPE_CODE_FUNCTION;
696 TypeVals.push_back(FT->isVarArg());
697 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000698 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
699 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerccee7062007-05-05 06:30:12 +0000700 AbbrevToUse = FunctionAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000701 break;
702 }
703 case Type::StructTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000704 StructType *ST = cast<StructType>(T);
Chris Lattnerccee7062007-05-05 06:30:12 +0000705 // STRUCT: [ispacked, eltty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000706 TypeVals.push_back(ST->isPacked());
Chris Lattnere14cb882007-05-04 19:11:41 +0000707 // Output all of the element types.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000708 for (StructType::element_iterator I = ST->element_begin(),
709 E = ST->element_end(); I != E; ++I)
710 TypeVals.push_back(VE.getTypeID(*I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000711
Chris Lattner335d3992011-08-12 18:06:37 +0000712 if (ST->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000713 Code = bitc::TYPE_CODE_STRUCT_ANON;
714 AbbrevToUse = StructAnonAbbrev;
715 } else {
716 if (ST->isOpaque()) {
717 Code = bitc::TYPE_CODE_OPAQUE;
718 } else {
719 Code = bitc::TYPE_CODE_STRUCT_NAMED;
720 AbbrevToUse = StructNamedAbbrev;
721 }
722
723 // Emit the name if it is present.
724 if (!ST->getName().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000725 writeStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
726 StructNameAbbrev);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000727 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000728 break;
729 }
730 case Type::ArrayTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000731 ArrayType *AT = cast<ArrayType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000732 // ARRAY: [numelts, eltty]
733 Code = bitc::TYPE_CODE_ARRAY;
734 TypeVals.push_back(AT->getNumElements());
735 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerccee7062007-05-05 06:30:12 +0000736 AbbrevToUse = ArrayAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000737 break;
738 }
739 case Type::VectorTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000740 VectorType *VT = cast<VectorType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000741 // VECTOR [numelts, eltty]
742 Code = bitc::TYPE_CODE_VECTOR;
743 TypeVals.push_back(VT->getNumElements());
744 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
745 break;
746 }
747 }
748
749 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000750 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000751 TypeVals.clear();
752 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000753
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000754 Stream.ExitBlock();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000755}
756
Teresa Johnson5e22e442016-02-06 16:07:35 +0000757static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
758 switch (Linkage) {
Rafael Espindola261d25b2015-01-08 16:25:01 +0000759 case GlobalValue::ExternalLinkage:
760 return 0;
761 case GlobalValue::WeakAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000762 return 16;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000763 case GlobalValue::AppendingLinkage:
764 return 2;
765 case GlobalValue::InternalLinkage:
766 return 3;
767 case GlobalValue::LinkOnceAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000768 return 18;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000769 case GlobalValue::ExternalWeakLinkage:
770 return 7;
771 case GlobalValue::CommonLinkage:
772 return 8;
773 case GlobalValue::PrivateLinkage:
774 return 9;
775 case GlobalValue::WeakODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000776 return 17;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000777 case GlobalValue::LinkOnceODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000778 return 19;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000779 case GlobalValue::AvailableExternallyLinkage:
780 return 12;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000781 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000782 llvm_unreachable("Invalid linkage");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000783}
784
Teresa Johnson5e22e442016-02-06 16:07:35 +0000785static unsigned getEncodedLinkage(const GlobalValue &GV) {
786 return getEncodedLinkage(GV.getLinkage());
787}
788
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000789static unsigned getEncodedVisibility(const GlobalValue &GV) {
790 switch (GV.getVisibility()) {
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000791 case GlobalValue::DefaultVisibility: return 0;
792 case GlobalValue::HiddenVisibility: return 1;
793 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000794 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000795 llvm_unreachable("Invalid visibility");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000796}
797
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000798static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
799 switch (GV.getDLLStorageClass()) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000800 case GlobalValue::DefaultStorageClass: return 0;
801 case GlobalValue::DLLImportStorageClass: return 1;
802 case GlobalValue::DLLExportStorageClass: return 2;
803 }
804 llvm_unreachable("Invalid DLL storage class");
805}
806
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000807static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000808 switch (GV.getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000809 case GlobalVariable::NotThreadLocal: return 0;
810 case GlobalVariable::GeneralDynamicTLSModel: return 1;
811 case GlobalVariable::LocalDynamicTLSModel: return 2;
812 case GlobalVariable::InitialExecTLSModel: return 3;
813 case GlobalVariable::LocalExecTLSModel: return 4;
814 }
815 llvm_unreachable("Invalid TLS model");
816}
817
David Majnemerdad0a642014-06-27 18:19:56 +0000818static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
819 switch (C.getSelectionKind()) {
820 case Comdat::Any:
821 return bitc::COMDAT_SELECTION_KIND_ANY;
822 case Comdat::ExactMatch:
823 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
824 case Comdat::Largest:
825 return bitc::COMDAT_SELECTION_KIND_LARGEST;
826 case Comdat::NoDuplicates:
827 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
828 case Comdat::SameSize:
829 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
830 }
831 llvm_unreachable("Invalid selection kind");
832}
833
Teresa Johnson37687f32016-04-23 04:30:47 +0000834void ModuleBitcodeWriter::writeComdats() {
David Majnemer90a021f2016-03-13 08:01:03 +0000835 SmallVector<unsigned, 64> Vals;
David Majnemerdad0a642014-06-27 18:19:56 +0000836 for (const Comdat *C : VE.getComdats()) {
837 // COMDAT: [selection_kind, name]
838 Vals.push_back(getEncodedComdatSelectionKind(*C));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +0000839 size_t Size = C->getName().size();
David Majnemer90a021f2016-03-13 08:01:03 +0000840 assert(isUInt<32>(Size));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +0000841 Vals.push_back(Size);
David Majnemerdad0a642014-06-27 18:19:56 +0000842 for (char Chr : C->getName())
843 Vals.push_back((unsigned char)Chr);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000844 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
David Majnemerdad0a642014-06-27 18:19:56 +0000845 Vals.clear();
846 }
847}
848
Teresa Johnsonff642b92015-09-17 20:12:00 +0000849/// Write a record that will eventually hold the word offset of the
850/// module-level VST. For now the offset is 0, which will be backpatched
Teresa Johnson37687f32016-04-23 04:30:47 +0000851/// after the real VST is written. Saves the bit offset to backpatch.
852void BitcodeWriter::writeValueSymbolTableForwardDecl() {
Teresa Johnsonff642b92015-09-17 20:12:00 +0000853 // Write a placeholder value in for the offset of the real VST,
854 // which is written after the function blocks so that it can include
855 // the offset of each function. The placeholder offset will be
856 // updated when the real VST is written.
857 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
858 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
859 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
860 // hold the real VST offset. Must use fixed instead of VBR as we don't
861 // know how many VBR chunks to reserve ahead of time.
862 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000863 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +0000864
865 // Emit the placeholder
866 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000867 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
Teresa Johnsonff642b92015-09-17 20:12:00 +0000868
Teresa Johnson37687f32016-04-23 04:30:47 +0000869 // Compute and save the bit offset to the placeholder, which will be
Teresa Johnsonff642b92015-09-17 20:12:00 +0000870 // patched when the real VST is written. We can simply subtract the 32-bit
871 // fixed size from the current bit number to get the location to backpatch.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000872 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
Teresa Johnsonff642b92015-09-17 20:12:00 +0000873}
874
Teresa Johnsone1164de2016-02-10 21:55:02 +0000875enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
876
877/// Determine the encoding to use for the given string name and length.
878static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
879 bool isChar6 = true;
880 for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
881 if (isChar6)
882 isChar6 = BitCodeAbbrevOp::isChar6(*C);
883 if ((unsigned char)*C & 128)
884 // don't bother scanning the rest.
885 return SE_Fixed8;
886 }
887 if (isChar6)
888 return SE_Char6;
889 else
890 return SE_Fixed7;
891}
892
Teresa Johnsonff642b92015-09-17 20:12:00 +0000893/// Emit top-level description of module, including target triple, inline asm,
894/// descriptors for global variables, and function prototype info.
895/// Returns the bit offset to backpatch with the location of the real VST.
Teresa Johnson37687f32016-04-23 04:30:47 +0000896void ModuleBitcodeWriter::writeModuleInfo() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000897 // Emit various pieces of data attached to a module.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000898 if (!M.getTargetTriple().empty())
899 writeStringRecord(bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
Teresa Johnson37687f32016-04-23 04:30:47 +0000900 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000901 const std::string &DL = M.getDataLayoutStr();
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000902 if (!DL.empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000903 writeStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000904 if (!M.getModuleInlineAsm().empty())
905 writeStringRecord(bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
Teresa Johnson37687f32016-04-23 04:30:47 +0000906 0 /*TODO*/);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000907
Gordon Henriksend930f912008-08-17 18:44:35 +0000908 // Emit information about sections and GC, computing how many there are. Also
909 // compute the maximum alignment value.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000910 std::map<std::string, unsigned> SectionMap;
Gordon Henriksend930f912008-08-17 18:44:35 +0000911 std::map<std::string, unsigned> GCMap;
Chris Lattner4b00d922007-04-23 16:04:05 +0000912 unsigned MaxAlignment = 0;
Chris Lattnerb5491372007-04-23 18:58:34 +0000913 unsigned MaxGlobalType = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000914 for (const GlobalValue &GV : M.globals()) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000915 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
David Blaikie1a848da2015-04-27 19:58:56 +0000916 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000917 if (GV.hasSection()) {
Chad Rosier75ec09c2011-08-12 16:45:18 +0000918 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000919 unsigned &Entry = SectionMap[GV.getSection()];
Chad Rosier75ec09c2011-08-12 16:45:18 +0000920 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000921 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
922 0 /*TODO*/);
Chad Rosier75ec09c2011-08-12 16:45:18 +0000923 Entry = SectionMap.size();
924 }
925 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000926 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000927 for (const Function &F : M) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000928 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
929 if (F.hasSection()) {
Gordon Henriksen71183b62007-12-10 03:18:06 +0000930 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000931 unsigned &Entry = SectionMap[F.getSection()];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000932 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000933 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
934 0 /*TODO*/);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000935 Entry = SectionMap.size();
936 }
937 }
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000938 if (F.hasGC()) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000939 // Same for GC names.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000940 unsigned &Entry = GCMap[F.getGC()];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000941 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000942 writeStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 0 /*TODO*/);
Gordon Henriksend930f912008-08-17 18:44:35 +0000943 Entry = GCMap.size();
Gordon Henriksen71183b62007-12-10 03:18:06 +0000944 }
945 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000946 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000947
Chris Lattner4b00d922007-04-23 16:04:05 +0000948 // Emit abbrev for globals, now that we know # sections and max alignment.
949 unsigned SimpleGVarAbbrev = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000950 if (!M.global_empty()) {
Chris Lattner4b00d922007-04-23 16:04:05 +0000951 // Add an abbrev for common globals with no visibility or thread localness.
952 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
953 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2eae59f2007-05-04 20:34:50 +0000954 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +0000955 Log2_32_Ceil(MaxGlobalType+1)));
David Blaikie1a848da2015-04-27 19:58:56 +0000956 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
957 //| explicitType << 1
958 //| constant
959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
960 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
961 if (MaxAlignment == 0) // Alignment.
Chris Lattner4b00d922007-04-23 16:04:05 +0000962 Abbv->Add(BitCodeAbbrevOp(0));
963 else {
964 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2eae59f2007-05-04 20:34:50 +0000965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +0000966 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +0000967 }
968 if (SectionMap.empty()) // Section.
969 Abbv->Add(BitCodeAbbrevOp(0));
970 else
Chris Lattner2eae59f2007-05-04 20:34:50 +0000971 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner1e50c292007-04-24 03:29:47 +0000972 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +0000973 // Don't bother emitting vis + thread local.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000974 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner4b00d922007-04-23 16:04:05 +0000975 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000976
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000977 // Emit the global variable information.
978 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000979 for (const GlobalVariable &GV : M.globals()) {
Chris Lattner4b00d922007-04-23 16:04:05 +0000980 unsigned AbbrevToUse = 0;
981
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000982 // GLOBALVAR: [type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +0000983 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +0000984 // unnamed_addr, externally_initialized, dllstorageclass,
985 // comdat]
David Blaikie1a848da2015-04-27 19:58:56 +0000986 Vals.push_back(VE.getTypeID(GV.getValueType()));
987 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000988 Vals.push_back(GV.isDeclaration() ? 0 :
989 (VE.getValueID(GV.getInitializer()) + 1));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000990 Vals.push_back(getEncodedLinkage(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000991 Vals.push_back(Log2_32(GV.getAlignment())+1);
992 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
993 if (GV.isThreadLocal() ||
994 GV.getVisibility() != GlobalValue::DefaultVisibility ||
995 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
David Majnemerdad0a642014-06-27 18:19:56 +0000996 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
997 GV.hasComdat()) {
Chris Lattner4b00d922007-04-23 16:04:05 +0000998 Vals.push_back(getEncodedVisibility(GV));
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000999 Vals.push_back(getEncodedThreadLocalMode(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001000 Vals.push_back(GV.hasUnnamedAddr());
1001 Vals.push_back(GV.isExternallyInitialized());
Nico Rieck7157bb72014-01-14 15:22:47 +00001002 Vals.push_back(getEncodedDLLStorageClass(GV));
David Majnemerdad0a642014-06-27 18:19:56 +00001003 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
Chris Lattner4b00d922007-04-23 16:04:05 +00001004 } else {
1005 AbbrevToUse = SimpleGVarAbbrev;
1006 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001007
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001008 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001009 Vals.clear();
1010 }
1011
1012 // Emit the function proto information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001013 for (const Function &F : M) {
Chad Rosier42ee1522011-12-07 23:57:55 +00001014 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001015 // section, visibility, gc, unnamed_addr, prologuedata,
David Majnemer7fddecc2015-06-17 20:52:32 +00001016 // dllstorageclass, comdat, prefixdata, personalityfn]
David Blaikie561a1572015-04-17 16:28:26 +00001017 Vals.push_back(VE.getTypeID(F.getFunctionType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001018 Vals.push_back(F.getCallingConv());
1019 Vals.push_back(F.isDeclaration());
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001020 Vals.push_back(getEncodedLinkage(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001021 Vals.push_back(VE.getAttributeID(F.getAttributes()));
1022 Vals.push_back(Log2_32(F.getAlignment())+1);
1023 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001024 Vals.push_back(getEncodedVisibility(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001025 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1026 Vals.push_back(F.hasUnnamedAddr());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001027 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1028 : 0);
Nico Rieck7157bb72014-01-14 15:22:47 +00001029 Vals.push_back(getEncodedDLLStorageClass(F));
David Majnemerdad0a642014-06-27 18:19:56 +00001030 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001031 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1032 : 0);
David Majnemer7fddecc2015-06-17 20:52:32 +00001033 Vals.push_back(
1034 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001035
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001036 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001037 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001038 Vals.clear();
1039 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001040
Chris Lattner44c17072007-04-26 02:46:40 +00001041 // Emit the alias information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001042 for (const GlobalAlias &A : M.aliases()) {
Chad Rosiera966c312011-12-08 00:11:31 +00001043 // ALIAS: [alias type, aliasee val#, linkage, visibility]
David Blaikie6a51dbd2015-09-17 22:18:59 +00001044 Vals.push_back(VE.getTypeID(A.getValueType()));
1045 Vals.push_back(A.getType()->getAddressSpace());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001046 Vals.push_back(VE.getValueID(A.getAliasee()));
1047 Vals.push_back(getEncodedLinkage(A));
1048 Vals.push_back(getEncodedVisibility(A));
1049 Vals.push_back(getEncodedDLLStorageClass(A));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001050 Vals.push_back(getEncodedThreadLocalMode(A));
1051 Vals.push_back(A.hasUnnamedAddr());
Chris Lattner44c17072007-04-26 02:46:40 +00001052 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001053 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
Chris Lattner44c17072007-04-26 02:46:40 +00001054 Vals.clear();
1055 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001056
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001057 // Emit the ifunc information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001058 for (const GlobalIFunc &I : M.ifuncs()) {
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001059 // IFUNC: [ifunc type, address space, resolver val#, linkage, visibility]
1060 Vals.push_back(VE.getTypeID(I.getValueType()));
1061 Vals.push_back(I.getType()->getAddressSpace());
1062 Vals.push_back(VE.getValueID(I.getResolver()));
1063 Vals.push_back(getEncodedLinkage(I));
1064 Vals.push_back(getEncodedVisibility(I));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001065 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001066 Vals.clear();
1067 }
1068
Teresa Johnsone1164de2016-02-10 21:55:02 +00001069 // Emit the module's source file name.
1070 {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001071 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1072 M.getSourceFileName().size());
Teresa Johnsone1164de2016-02-10 21:55:02 +00001073 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1074 if (Bits == SE_Char6)
1075 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1076 else if (Bits == SE_Fixed7)
1077 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1078
1079 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1080 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1081 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1082 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1083 Abbv->Add(AbbrevOpToUse);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001084 unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001085
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001086 for (const auto P : M.getSourceFileName())
Teresa Johnsone1164de2016-02-10 21:55:02 +00001087 Vals.push_back((unsigned char)P);
1088
1089 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001090 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001091 Vals.clear();
1092 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001093
Teresa Johnson37687f32016-04-23 04:30:47 +00001094 // If we have a VST, write the VSTOFFSET record placeholder.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001095 if (M.getValueSymbolTable().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001096 return;
1097 writeValueSymbolTableForwardDecl();
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001098}
1099
Teresa Johnson37687f32016-04-23 04:30:47 +00001100static uint64_t getOptimizationFlags(const Value *V) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001101 uint64_t Flags = 0;
1102
Sanjay Patelc00017d2014-10-15 17:45:13 +00001103 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001104 if (OBO->hasNoSignedWrap())
1105 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1106 if (OBO->hasNoUnsignedWrap())
1107 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001108 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
Chris Lattner35315d02011-02-06 21:44:57 +00001109 if (PEO->isExact())
1110 Flags |= 1 << bitc::PEO_EXACT;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001111 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001112 if (FPMO->hasUnsafeAlgebra())
Michael Ilseman65f14352012-12-09 21:12:04 +00001113 Flags |= FastMathFlags::UnsafeAlgebra;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001114 if (FPMO->hasNoNaNs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001115 Flags |= FastMathFlags::NoNaNs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001116 if (FPMO->hasNoInfs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001117 Flags |= FastMathFlags::NoInfs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001118 if (FPMO->hasNoSignedZeros())
Michael Ilseman65f14352012-12-09 21:12:04 +00001119 Flags |= FastMathFlags::NoSignedZeros;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001120 if (FPMO->hasAllowReciprocal())
Michael Ilseman65f14352012-12-09 21:12:04 +00001121 Flags |= FastMathFlags::AllowReciprocal;
Dan Gohman0ebd6962009-07-20 21:19:07 +00001122 }
1123
1124 return Flags;
1125}
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001126
Teresa Johnson37687f32016-04-23 04:30:47 +00001127void ModuleBitcodeWriter::writeValueAsMetadata(
1128 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001129 // Mimic an MDNode with a value as one operand.
1130 Value *V = MD->getValue();
1131 Record.push_back(VE.getTypeID(V->getType()));
1132 Record.push_back(VE.getValueID(V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001133 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001134 Record.clear();
1135}
1136
Teresa Johnson37687f32016-04-23 04:30:47 +00001137void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1138 SmallVectorImpl<uint64_t> &Record,
1139 unsigned Abbrev) {
Chris Lattner9b493022009-12-31 01:22:29 +00001140 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001141 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001142 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1143 "Unexpected function-local metadata");
1144 Record.push_back(VE.getMetadataOrNullID(MD));
Devang Patel8cca7b42009-08-04 05:01:35 +00001145 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001146 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1147 : bitc::METADATA_NODE,
1148 Record, Abbrev);
Devang Patel8cca7b42009-08-04 05:01:35 +00001149 Record.clear();
1150}
Devang Patele059ba6e2009-07-23 01:07:34 +00001151
Teresa Johnson37687f32016-04-23 04:30:47 +00001152unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001153 // Assume the column is usually under 128, and always output the inlined-at
1154 // location (it's never more expensive than building an array size 1).
1155 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1156 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1157 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1159 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1161 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001162 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001163}
1164
Teresa Johnson37687f32016-04-23 04:30:47 +00001165void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1166 SmallVectorImpl<uint64_t> &Record,
1167 unsigned &Abbrev) {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001168 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001169 Abbrev = createDILocationAbbrev();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001170
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001171 Record.push_back(N->isDistinct());
1172 Record.push_back(N->getLine());
1173 Record.push_back(N->getColumn());
1174 Record.push_back(VE.getMetadataID(N->getScope()));
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001175 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001176
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001177 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001178 Record.clear();
1179}
1180
Teresa Johnson37687f32016-04-23 04:30:47 +00001181unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001182 // Assume the column is usually under 128, and always output the inlined-at
1183 // location (it's never more expensive than building an array size 1).
1184 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1185 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1186 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1189 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1190 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1191 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001192 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001193}
1194
Teresa Johnson37687f32016-04-23 04:30:47 +00001195void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1196 SmallVectorImpl<uint64_t> &Record,
1197 unsigned &Abbrev) {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001198 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001199 Abbrev = createGenericDINodeAbbrev();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001200
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001201 Record.push_back(N->isDistinct());
1202 Record.push_back(N->getTag());
1203 Record.push_back(0); // Per-tag version field; unused for now.
1204
1205 for (auto &I : N->operands())
1206 Record.push_back(VE.getMetadataOrNullID(I));
1207
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001208 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001209 Record.clear();
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001210}
1211
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001212static uint64_t rotateSign(int64_t I) {
1213 uint64_t U = I;
1214 return I < 0 ? ~(U << 1) : U << 1;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001215}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001216
Teresa Johnson37687f32016-04-23 04:30:47 +00001217void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1218 SmallVectorImpl<uint64_t> &Record,
1219 unsigned Abbrev) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001220 Record.push_back(N->isDistinct());
1221 Record.push_back(N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001222 Record.push_back(rotateSign(N->getLowerBound()));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001223
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001224 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001225 Record.clear();
1226}
1227
Teresa Johnson37687f32016-04-23 04:30:47 +00001228void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1229 SmallVectorImpl<uint64_t> &Record,
1230 unsigned Abbrev) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001231 Record.push_back(N->isDistinct());
1232 Record.push_back(rotateSign(N->getValue()));
1233 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1234
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001235 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001236 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001237}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001238
Teresa Johnson37687f32016-04-23 04:30:47 +00001239void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1240 SmallVectorImpl<uint64_t> &Record,
1241 unsigned Abbrev) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001242 Record.push_back(N->isDistinct());
1243 Record.push_back(N->getTag());
1244 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1245 Record.push_back(N->getSizeInBits());
1246 Record.push_back(N->getAlignInBits());
1247 Record.push_back(N->getEncoding());
1248
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001249 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001250 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001251}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001252
Teresa Johnson37687f32016-04-23 04:30:47 +00001253void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1254 SmallVectorImpl<uint64_t> &Record,
1255 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001256 Record.push_back(N->isDistinct());
1257 Record.push_back(N->getTag());
1258 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1259 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1260 Record.push_back(N->getLine());
1261 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001262 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001263 Record.push_back(N->getSizeInBits());
1264 Record.push_back(N->getAlignInBits());
1265 Record.push_back(N->getOffsetInBits());
1266 Record.push_back(N->getFlags());
1267 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1268
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001269 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001270 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001271}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001272
Teresa Johnson37687f32016-04-23 04:30:47 +00001273void ModuleBitcodeWriter::writeDICompositeType(
1274 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1275 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001276 Record.push_back(N->isDistinct());
1277 Record.push_back(N->getTag());
1278 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1279 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1280 Record.push_back(N->getLine());
1281 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1282 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1283 Record.push_back(N->getSizeInBits());
1284 Record.push_back(N->getAlignInBits());
1285 Record.push_back(N->getOffsetInBits());
1286 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001287 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001288 Record.push_back(N->getRuntimeLang());
1289 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001290 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001291 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1292
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001293 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001294 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001295}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001296
Teresa Johnson37687f32016-04-23 04:30:47 +00001297void ModuleBitcodeWriter::writeDISubroutineType(
1298 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1299 unsigned Abbrev) {
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001300 Record.push_back(N->isDistinct());
1301 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001302 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001303
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001304 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001305 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001306}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001307
Teresa Johnson37687f32016-04-23 04:30:47 +00001308void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1309 SmallVectorImpl<uint64_t> &Record,
1310 unsigned Abbrev) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001311 Record.push_back(N->isDistinct());
1312 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1313 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1314
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001315 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001316 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001317}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001318
Teresa Johnson37687f32016-04-23 04:30:47 +00001319void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1320 SmallVectorImpl<uint64_t> &Record,
1321 unsigned Abbrev) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001322 assert(N->isDistinct() && "Expected distinct compile units");
1323 Record.push_back(/* IsDistinct */ true);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001324 Record.push_back(N->getSourceLanguage());
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001325 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001326 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1327 Record.push_back(N->isOptimized());
1328 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1329 Record.push_back(N->getRuntimeVersion());
1330 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1331 Record.push_back(N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001332 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1333 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001334 Record.push_back(/* subprograms */ 0);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001335 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1336 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
Adrian Prantl1f599f92015-05-21 20:37:30 +00001337 Record.push_back(N->getDWOId());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001338 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001339
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001340 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001341 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001342}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001343
Teresa Johnson37687f32016-04-23 04:30:47 +00001344void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1345 SmallVectorImpl<uint64_t> &Record,
1346 unsigned Abbrev) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001347 Record.push_back(N->isDistinct());
1348 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1349 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1350 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1351 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1352 Record.push_back(N->getLine());
1353 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1354 Record.push_back(N->isLocalToUnit());
1355 Record.push_back(N->isDefinition());
1356 Record.push_back(N->getScopeLine());
1357 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1358 Record.push_back(N->getVirtuality());
1359 Record.push_back(N->getVirtualIndex());
1360 Record.push_back(N->getFlags());
1361 Record.push_back(N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001362 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001363 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001364 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001365 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001366
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001367 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001368 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001369}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001370
Teresa Johnson37687f32016-04-23 04:30:47 +00001371void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1372 SmallVectorImpl<uint64_t> &Record,
1373 unsigned Abbrev) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001374 Record.push_back(N->isDistinct());
1375 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1376 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1377 Record.push_back(N->getLine());
1378 Record.push_back(N->getColumn());
1379
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001380 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001381 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001382}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001383
Teresa Johnson37687f32016-04-23 04:30:47 +00001384void ModuleBitcodeWriter::writeDILexicalBlockFile(
1385 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1386 unsigned Abbrev) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001387 Record.push_back(N->isDistinct());
1388 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1389 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1390 Record.push_back(N->getDiscriminator());
1391
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001392 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001393 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001394}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001395
Teresa Johnson37687f32016-04-23 04:30:47 +00001396void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1397 SmallVectorImpl<uint64_t> &Record,
1398 unsigned Abbrev) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001399 Record.push_back(N->isDistinct());
1400 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1401 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1402 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1403 Record.push_back(N->getLine());
1404
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001405 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001406 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001407}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001408
Teresa Johnson37687f32016-04-23 04:30:47 +00001409void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1410 SmallVectorImpl<uint64_t> &Record,
1411 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001412 Record.push_back(N->isDistinct());
1413 Record.push_back(N->getMacinfoType());
1414 Record.push_back(N->getLine());
1415 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1416 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1417
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001418 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001419 Record.clear();
1420}
1421
Teresa Johnson37687f32016-04-23 04:30:47 +00001422void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1423 SmallVectorImpl<uint64_t> &Record,
1424 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001425 Record.push_back(N->isDistinct());
1426 Record.push_back(N->getMacinfoType());
1427 Record.push_back(N->getLine());
1428 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1429 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1430
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001431 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001432 Record.clear();
1433}
1434
Teresa Johnson37687f32016-04-23 04:30:47 +00001435void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1436 SmallVectorImpl<uint64_t> &Record,
1437 unsigned Abbrev) {
Adrian Prantlab1243f2015-06-29 23:03:47 +00001438 Record.push_back(N->isDistinct());
1439 for (auto &I : N->operands())
1440 Record.push_back(VE.getMetadataOrNullID(I));
1441
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001442 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
Adrian Prantlab1243f2015-06-29 23:03:47 +00001443 Record.clear();
1444}
1445
Teresa Johnson37687f32016-04-23 04:30:47 +00001446void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1447 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1448 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001449 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001450 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1451 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1452
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001453 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001454 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001455}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001456
Teresa Johnson37687f32016-04-23 04:30:47 +00001457void ModuleBitcodeWriter::writeDITemplateValueParameter(
1458 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1459 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001460 Record.push_back(N->isDistinct());
1461 Record.push_back(N->getTag());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001462 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1463 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1464 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1465
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001466 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001467 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001468}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001469
Teresa Johnson37687f32016-04-23 04:30:47 +00001470void ModuleBitcodeWriter::writeDIGlobalVariable(
1471 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1472 unsigned Abbrev) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001473 Record.push_back(N->isDistinct());
1474 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1475 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1476 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1477 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1478 Record.push_back(N->getLine());
1479 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1480 Record.push_back(N->isLocalToUnit());
1481 Record.push_back(N->isDefinition());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001482 Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001483 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1484
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001485 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001486 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001487}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001488
Teresa Johnson37687f32016-04-23 04:30:47 +00001489void ModuleBitcodeWriter::writeDILocalVariable(
1490 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1491 unsigned Abbrev) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001492 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001493 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1494 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1495 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1496 Record.push_back(N->getLine());
1497 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1498 Record.push_back(N->getArg());
1499 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001500
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001501 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001502 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001503}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001504
Teresa Johnson37687f32016-04-23 04:30:47 +00001505void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1506 SmallVectorImpl<uint64_t> &Record,
1507 unsigned Abbrev) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001508 Record.reserve(N->getElements().size() + 1);
1509
1510 Record.push_back(N->isDistinct());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001511 Record.append(N->elements_begin(), N->elements_end());
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001512
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001513 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001514 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001515}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001516
Teresa Johnson37687f32016-04-23 04:30:47 +00001517void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1518 SmallVectorImpl<uint64_t> &Record,
1519 unsigned Abbrev) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001520 Record.push_back(N->isDistinct());
1521 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1522 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1523 Record.push_back(N->getLine());
1524 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1525 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1526 Record.push_back(N->getAttributes());
1527 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1528
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001529 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001530 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001531}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001532
Teresa Johnson37687f32016-04-23 04:30:47 +00001533void ModuleBitcodeWriter::writeDIImportedEntity(
1534 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1535 unsigned Abbrev) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001536 Record.push_back(N->isDistinct());
1537 Record.push_back(N->getTag());
1538 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1539 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1540 Record.push_back(N->getLine());
1541 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1542
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001543 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001544 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001545}
1546
Teresa Johnson37687f32016-04-23 04:30:47 +00001547unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001548 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1549 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1550 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1551 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001552 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001553}
1554
Teresa Johnson37687f32016-04-23 04:30:47 +00001555void ModuleBitcodeWriter::writeNamedMetadata(
1556 SmallVectorImpl<uint64_t> &Record) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001557 if (M.named_metadata_empty())
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001558 return;
1559
Teresa Johnson37687f32016-04-23 04:30:47 +00001560 unsigned Abbrev = createNamedMetadataAbbrev();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001561 for (const NamedMDNode &NMD : M.named_metadata()) {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001562 // Write name.
1563 StringRef Str = NMD.getName();
1564 Record.append(Str.bytes_begin(), Str.bytes_end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001565 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001566 Record.clear();
1567
1568 // Write named metadata operands.
1569 for (const MDNode *N : NMD.operands())
1570 Record.push_back(VE.getMetadataID(N));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001571 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001572 Record.clear();
1573 }
1574}
1575
Teresa Johnson37687f32016-04-23 04:30:47 +00001576unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001577 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1578 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1579 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1580 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1581 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001582 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001583}
1584
1585/// Write out a record for MDString.
1586///
1587/// All the metadata strings in a metadata block are emitted in a single
1588/// record. The sizes and strings themselves are shoved into a blob.
Teresa Johnson37687f32016-04-23 04:30:47 +00001589void ModuleBitcodeWriter::writeMetadataStrings(
1590 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001591 if (Strings.empty())
1592 return;
1593
1594 // Start the record with the number of strings.
1595 Record.push_back(bitc::METADATA_STRINGS);
1596 Record.push_back(Strings.size());
1597
1598 // Emit the sizes of the strings in the blob.
1599 SmallString<256> Blob;
1600 {
1601 BitstreamWriter W(Blob);
1602 for (const Metadata *MD : Strings)
1603 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1604 W.FlushToWord();
1605 }
1606
1607 // Add the offset to the strings to the record.
1608 Record.push_back(Blob.size());
1609
1610 // Add the strings to the blob.
1611 for (const Metadata *MD : Strings)
1612 Blob.append(cast<MDString>(MD)->getString());
1613
1614 // Emit the final record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001615 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001616 Record.clear();
1617}
1618
Teresa Johnson37687f32016-04-23 04:30:47 +00001619void ModuleBitcodeWriter::writeMetadataRecords(
1620 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001621 if (MDs.empty())
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +00001622 return;
1623
Duncan P. N. Exon Smith6b7b6802015-02-04 21:54:12 +00001624 // Initialize MDNode abbreviations.
1625#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1626#include "llvm/IR/Metadata.def"
1627
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001628 for (const Metadata *MD : MDs) {
Duncan P. N. Exon Smith73d5aae2015-01-12 22:31:35 +00001629 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith79f8d112015-03-17 00:16:35 +00001630 assert(N->isResolved() && "Expected forward references to be resolved");
1631
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001632 switch (N->getMetadataID()) {
1633 default:
1634 llvm_unreachable("Invalid MDNode subclass");
1635#define HANDLE_MDNODE_LEAF(CLASS) \
1636 case Metadata::CLASS##Kind: \
Teresa Johnson37687f32016-04-23 04:30:47 +00001637 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001638 continue;
1639#include "llvm/IR/Metadata.def"
1640 }
Devang Patel8cca7b42009-08-04 05:01:35 +00001641 }
Teresa Johnson37687f32016-04-23 04:30:47 +00001642 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
Devang Patel8cca7b42009-08-04 05:01:35 +00001643 }
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001644}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001645
Teresa Johnson37687f32016-04-23 04:30:47 +00001646void ModuleBitcodeWriter::writeModuleMetadata() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001647 if (!VE.hasMDs() && M.named_metadata_empty())
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001648 return;
1649
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001650 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001651 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001652 writeMetadataStrings(VE.getMDStrings(), Record);
1653 writeMetadataRecords(VE.getNonMDStrings(), Record);
1654 writeNamedMetadata(Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001655 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001656}
1657
Teresa Johnson37687f32016-04-23 04:30:47 +00001658void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +00001659 if (!VE.hasMDs())
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001660 return;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001661
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001662 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001663 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001664 writeMetadataStrings(VE.getMDStrings(), Record);
1665 writeMetadataRecords(VE.getNonMDStrings(), Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001666 Stream.ExitBlock();
Victor Hernandezb00a6be2010-01-13 19:37:33 +00001667}
1668
Teresa Johnson37687f32016-04-23 04:30:47 +00001669void ModuleBitcodeWriter::writeMetadataAttachment(const Function &F) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001670 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
Chris Lattner07d09ed2010-04-03 02:17:50 +00001671
Devang Patelaf206b82009-09-18 19:26:43 +00001672 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001673
Devang Patelaf206b82009-09-18 19:26:43 +00001674 // Write metadata attachments
Chris Lattnerbb95d5e2011-06-17 17:56:00 +00001675 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001676 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001677 F.getAllMetadata(MDs);
1678 if (!MDs.empty()) {
1679 for (const auto &I : MDs) {
1680 Record.push_back(I.first);
1681 Record.push_back(VE.getMetadataID(I.second));
1682 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001683 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001684 Record.clear();
1685 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001686
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001687 for (const BasicBlock &BB : F)
1688 for (const Instruction &I : BB) {
Devang Patel6da5dbf2009-10-22 18:55:16 +00001689 MDs.clear();
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001690 I.getAllMetadataOtherThanDebugLoc(MDs);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001691
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001692 // If no metadata, ignore instruction.
1693 if (MDs.empty()) continue;
1694
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001695 Record.push_back(VE.getInstructionID(&I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001696
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001697 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1698 Record.push_back(MDs[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001699 Record.push_back(VE.getMetadataID(MDs[i].second));
Devang Patelaf206b82009-09-18 19:26:43 +00001700 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001701 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001702 Record.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00001703 }
1704
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001705 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001706}
1707
Teresa Johnson37687f32016-04-23 04:30:47 +00001708void ModuleBitcodeWriter::writeModuleMetadataStore() {
Devang Patelaf206b82009-09-18 19:26:43 +00001709 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001710
Devang Patelaf206b82009-09-18 19:26:43 +00001711 // Write metadata kinds
1712 // METADATA_KIND - [n x [id, name]]
Michael Ilseman979dfbb2012-12-03 22:57:47 +00001713 SmallVector<StringRef, 8> Names;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001714 M.getMDKindNames(Names);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001715
Dan Gohman43aa8f02010-07-20 21:42:28 +00001716 if (Names.empty()) return;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001717
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001718 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001719
Dan Gohman43aa8f02010-07-20 21:42:28 +00001720 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattnerc9558df2009-12-28 20:10:43 +00001721 Record.push_back(MDKindID);
1722 StringRef KName = Names[MDKindID];
1723 Record.append(KName.begin(), KName.end());
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001724
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001725 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
Devang Patelaf206b82009-09-18 19:26:43 +00001726 Record.clear();
1727 }
1728
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001729 Stream.ExitBlock();
Devang Patel8cca7b42009-08-04 05:01:35 +00001730}
1731
Teresa Johnson37687f32016-04-23 04:30:47 +00001732void ModuleBitcodeWriter::writeOperandBundleTags() {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001733 // Write metadata kinds
1734 //
1735 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
1736 //
1737 // OPERAND_BUNDLE_TAG - [strchr x N]
1738
1739 SmallVector<StringRef, 8> Tags;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001740 M.getOperandBundleTags(Tags);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001741
1742 if (Tags.empty())
1743 return;
1744
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001745 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001746
1747 SmallVector<uint64_t, 64> Record;
1748
1749 for (auto Tag : Tags) {
1750 Record.append(Tag.begin(), Tag.end());
1751
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001752 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001753 Record.clear();
1754 }
1755
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001756 Stream.ExitBlock();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001757}
1758
Jan Wen Voungafaced02012-10-11 20:20:40 +00001759static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1760 if ((int64_t)V >= 0)
1761 Vals.push_back(V << 1);
1762 else
1763 Vals.push_back((-V << 1) | 1);
1764}
1765
Teresa Johnson37687f32016-04-23 04:30:47 +00001766void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1767 bool isGlobal) {
Devang Patel8cca7b42009-08-04 05:01:35 +00001768 if (FirstVal == LastVal) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001769
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001770 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Devang Patel8cca7b42009-08-04 05:01:35 +00001771
1772 unsigned AggregateAbbrev = 0;
1773 unsigned String8Abbrev = 0;
1774 unsigned CString7Abbrev = 0;
1775 unsigned CString6Abbrev = 0;
1776 // If this is a constant pool for the module, emit module-specific abbrevs.
1777 if (isGlobal) {
1778 // Abbrev for CST_CODE_AGGREGATE.
1779 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1780 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1781 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1782 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001783 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001784
1785 // Abbrev for CST_CODE_STRING.
1786 Abbv = new BitCodeAbbrev();
1787 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001790 String8Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001791 // Abbrev for CST_CODE_CSTRING.
1792 Abbv = new BitCodeAbbrev();
1793 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1794 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1795 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001796 CString7Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001797 // Abbrev for CST_CODE_CSTRING.
1798 Abbv = new BitCodeAbbrev();
1799 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1801 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001802 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001803 }
1804
Devang Patel8cca7b42009-08-04 05:01:35 +00001805 SmallVector<uint64_t, 64> Record;
1806
1807 const ValueEnumerator::ValueList &Vals = VE.getValues();
Craig Topper2617dcc2014-04-15 06:32:26 +00001808 Type *LastTy = nullptr;
Devang Patel8cca7b42009-08-04 05:01:35 +00001809 for (unsigned i = FirstVal; i != LastVal; ++i) {
1810 const Value *V = Vals[i].first;
Chris Lattner52523562007-04-24 00:16:04 +00001811 // If we need to switch types, do so now.
1812 if (V->getType() != LastTy) {
1813 LastTy = V->getType();
1814 Record.push_back(VE.getTypeID(LastTy));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001815 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1816 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner52523562007-04-24 00:16:04 +00001817 Record.clear();
1818 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001819
Chris Lattner52523562007-04-24 00:16:04 +00001820 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesenfd04c742009-10-13 20:46:56 +00001821 Record.push_back(unsigned(IA->hasSideEffects()) |
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001822 unsigned(IA->isAlignStack()) << 1 |
1823 unsigned(IA->getDialect()&1) << 2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001824
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001825 // Add the asm string.
1826 const std::string &AsmStr = IA->getAsmString();
1827 Record.push_back(AsmStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001828 Record.append(AsmStr.begin(), AsmStr.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001829
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001830 // Add the constraint string.
1831 const std::string &ConstraintStr = IA->getConstraintString();
1832 Record.push_back(ConstraintStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001833 Record.append(ConstraintStr.begin(), ConstraintStr.end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001834 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001835 Record.clear();
Chris Lattner52523562007-04-24 00:16:04 +00001836 continue;
1837 }
1838 const Constant *C = cast<Constant>(V);
1839 unsigned Code = -1U;
1840 unsigned AbbrevToUse = 0;
1841 if (C->isNullValue()) {
1842 Code = bitc::CST_CODE_NULL;
1843 } else if (isa<UndefValue>(C)) {
1844 Code = bitc::CST_CODE_UNDEF;
1845 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
Bob Wilsone4077362013-09-09 19:14:35 +00001846 if (IV->getBitWidth() <= 64) {
1847 uint64_t V = IV->getSExtValue();
1848 emitSignedInt64(Record, V);
1849 Code = bitc::CST_CODE_INTEGER;
1850 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1851 } else { // Wide integers, > 64 bits in size.
1852 // We have an arbitrary precision integer value to write whose
1853 // bit width is > 64. However, in canonical unsigned integer
1854 // format it is likely that the high bits are going to be zero.
1855 // So, we only write the number of active words.
1856 unsigned NWords = IV->getValue().getActiveWords();
1857 const uint64_t *RawWords = IV->getValue().getRawData();
1858 for (unsigned i = 0; i != NWords; ++i) {
1859 emitSignedInt64(Record, RawWords[i]);
1860 }
1861 Code = bitc::CST_CODE_WIDE_INTEGER;
1862 }
Chris Lattner52523562007-04-24 00:16:04 +00001863 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1864 Code = bitc::CST_CODE_FLOAT;
Chris Lattner229907c2011-07-18 04:54:35 +00001865 Type *Ty = CFP->getType();
Dan Gohman518cda42011-12-17 00:04:22 +00001866 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00001867 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +00001868 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001869 // api needed to prevent premature destruction
Dale Johannesen93eefa02009-03-23 21:16:53 +00001870 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen54306fe2008-10-09 18:53:47 +00001871 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001872 const uint64_t *p = api.getRawData();
Dale Johannesen93eefa02009-03-23 21:16:53 +00001873 Record.push_back((p[1] << 48) | (p[0] >> 16));
1874 Record.push_back(p[0] & 0xffffLL);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001875 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00001876 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001877 const uint64_t *p = api.getRawData();
Dale Johannesen245dceb2007-09-11 18:32:33 +00001878 Record.push_back(p[0]);
1879 Record.push_back(p[1]);
Dale Johannesenbdad8092007-08-09 22:51:36 +00001880 } else {
1881 assert (0 && "Unknown FP type!");
Chris Lattner52523562007-04-24 00:16:04 +00001882 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00001883 } else if (isa<ConstantDataSequential>(C) &&
1884 cast<ConstantDataSequential>(C)->isString()) {
1885 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
1886 // Emit constant strings specially.
1887 unsigned NumElts = Str->getNumElements();
1888 // If this is a null-terminated string, use the denser CSTRING encoding.
1889 if (Str->isCString()) {
1890 Code = bitc::CST_CODE_CSTRING;
1891 --NumElts; // Don't encode the null, which isn't allowed by char6.
1892 } else {
1893 Code = bitc::CST_CODE_STRING;
1894 AbbrevToUse = String8Abbrev;
1895 }
1896 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1897 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1898 for (unsigned i = 0; i != NumElts; ++i) {
1899 unsigned char V = Str->getElementAsInteger(i);
1900 Record.push_back(V);
1901 isCStr7 &= (V & 128) == 0;
1902 if (isCStrChar6)
1903 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1904 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001905
Chris Lattner372dd1e2012-01-30 00:51:16 +00001906 if (isCStrChar6)
1907 AbbrevToUse = CString6Abbrev;
1908 else if (isCStr7)
1909 AbbrevToUse = CString7Abbrev;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001910 } else if (const ConstantDataSequential *CDS =
Chris Lattner372dd1e2012-01-30 00:51:16 +00001911 dyn_cast<ConstantDataSequential>(C)) {
Chris Lattner5d917072012-01-30 07:36:01 +00001912 Code = bitc::CST_CODE_DATA;
Chris Lattner372dd1e2012-01-30 00:51:16 +00001913 Type *EltTy = CDS->getType()->getElementType();
1914 if (isa<IntegerType>(EltTy)) {
1915 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1916 Record.push_back(CDS->getElementAsInteger(i));
Chris Lattner372dd1e2012-01-30 00:51:16 +00001917 } else {
Justin Bognera43eacb2016-01-06 22:31:32 +00001918 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1919 Record.push_back(
1920 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
Chris Lattner372dd1e2012-01-30 00:51:16 +00001921 }
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00001922 } else if (isa<ConstantAggregate>(C)) {
Chris Lattner52523562007-04-24 00:16:04 +00001923 Code = bitc::CST_CODE_AGGREGATE;
Pete Cooper125ad172015-06-25 20:51:38 +00001924 for (const Value *Op : C->operands())
1925 Record.push_back(VE.getValueID(Op));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00001926 AbbrevToUse = AggregateAbbrev;
Chris Lattner52523562007-04-24 00:16:04 +00001927 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001928 switch (CE->getOpcode()) {
1929 default:
1930 if (Instruction::isCast(CE->getOpcode())) {
1931 Code = bitc::CST_CODE_CE_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00001932 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001933 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1934 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00001935 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001936 } else {
1937 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1938 Code = bitc::CST_CODE_CE_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00001939 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001940 Record.push_back(VE.getValueID(C->getOperand(0)));
1941 Record.push_back(VE.getValueID(C->getOperand(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00001942 uint64_t Flags = getOptimizationFlags(CE);
Dan Gohman0ebd6962009-07-20 21:19:07 +00001943 if (Flags != 0)
1944 Record.push_back(Flags);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001945 }
1946 break;
David Blaikieb9263572015-03-13 21:03:36 +00001947 case Instruction::GetElementPtr: {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001948 Code = bitc::CST_CODE_CE_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00001949 const auto *GO = cast<GEPOperator>(C);
1950 if (GO->isInBounds())
Dan Gohman1639c392009-07-27 21:53:46 +00001951 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00001952 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001953 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1954 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1955 Record.push_back(VE.getValueID(C->getOperand(i)));
1956 }
1957 break;
David Blaikieb9263572015-03-13 21:03:36 +00001958 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001959 case Instruction::Select:
1960 Code = bitc::CST_CODE_CE_SELECT;
1961 Record.push_back(VE.getValueID(C->getOperand(0)));
1962 Record.push_back(VE.getValueID(C->getOperand(1)));
1963 Record.push_back(VE.getValueID(C->getOperand(2)));
1964 break;
1965 case Instruction::ExtractElement:
1966 Code = bitc::CST_CODE_CE_EXTRACTELT;
1967 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1968 Record.push_back(VE.getValueID(C->getOperand(0)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001969 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001970 Record.push_back(VE.getValueID(C->getOperand(1)));
1971 break;
1972 case Instruction::InsertElement:
1973 Code = bitc::CST_CODE_CE_INSERTELT;
1974 Record.push_back(VE.getValueID(C->getOperand(0)));
1975 Record.push_back(VE.getValueID(C->getOperand(1)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001976 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001977 Record.push_back(VE.getValueID(C->getOperand(2)));
1978 break;
1979 case Instruction::ShuffleVector:
Nate Begeman94aa38d2009-02-12 21:28:33 +00001980 // If the return type and argument types are the same, this is a
1981 // standard shufflevector instruction. If the types are different,
1982 // then the shuffle is widening or truncating the input vectors, and
1983 // the argument type must also be encoded.
1984 if (C->getType() == C->getOperand(0)->getType()) {
1985 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
1986 } else {
1987 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
1988 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1989 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001990 Record.push_back(VE.getValueID(C->getOperand(0)));
1991 Record.push_back(VE.getValueID(C->getOperand(1)));
1992 Record.push_back(VE.getValueID(C->getOperand(2)));
1993 break;
1994 case Instruction::ICmp:
1995 case Instruction::FCmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001996 Code = bitc::CST_CODE_CE_CMP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001997 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1998 Record.push_back(VE.getValueID(C->getOperand(0)));
1999 Record.push_back(VE.getValueID(C->getOperand(1)));
2000 Record.push_back(CE->getPredicate());
2001 break;
2002 }
Chris Lattnerf540d742009-10-28 05:24:40 +00002003 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerf540d742009-10-28 05:24:40 +00002004 Code = bitc::CST_CODE_BLOCKADDRESS;
2005 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2006 Record.push_back(VE.getValueID(BA->getFunction()));
2007 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner52523562007-04-24 00:16:04 +00002008 } else {
Dan Gohman47dc8fd2010-07-21 21:18:37 +00002009#ifndef NDEBUG
2010 C->dump();
2011#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002012 llvm_unreachable("Unknown constant!");
Chris Lattner52523562007-04-24 00:16:04 +00002013 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002014 Stream.EmitRecord(Code, Record, AbbrevToUse);
Chris Lattner52523562007-04-24 00:16:04 +00002015 Record.clear();
2016 }
2017
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002018 Stream.ExitBlock();
Chris Lattner52523562007-04-24 00:16:04 +00002019}
2020
Teresa Johnson37687f32016-04-23 04:30:47 +00002021void ModuleBitcodeWriter::writeModuleConstants() {
Chris Lattner52523562007-04-24 00:16:04 +00002022 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002023
Chris Lattner52523562007-04-24 00:16:04 +00002024 // Find the first constant to emit, which is the first non-globalvalue value.
2025 // We know globalvalues have been emitted by WriteModuleInfo.
2026 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2027 if (!isa<GlobalValue>(Vals[i].first)) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002028 writeConstants(i, Vals.size(), true);
Chris Lattner52523562007-04-24 00:16:04 +00002029 return;
2030 }
2031 }
2032}
Chris Lattner215e9cd2007-04-23 20:35:01 +00002033
Teresa Johnson37687f32016-04-23 04:30:47 +00002034/// pushValueAndType - The file has to encode both the value and type id for
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002035/// many values, because we need to know what type to create for forward
2036/// references. However, most operands are not forward references, so this type
2037/// field is not needed.
2038///
2039/// This function adds V's value ID to Vals. If the value ID is higher than the
2040/// instruction ID, then it is a forward reference, and it also includes the
Jan Wen Voungafaced02012-10-11 20:20:40 +00002041/// type ID. The value ID that is written is encoded relative to the InstID.
Teresa Johnson37687f32016-04-23 04:30:47 +00002042bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2043 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002044 unsigned ValID = VE.getValueID(V);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002045 // Make encoding relative to the InstID.
2046 Vals.push_back(InstID - ValID);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002047 if (ValID >= InstID) {
2048 Vals.push_back(VE.getTypeID(V->getType()));
2049 return true;
2050 }
2051 return false;
2052}
2053
Teresa Johnson37687f32016-04-23 04:30:47 +00002054void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2055 unsigned InstID) {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002056 SmallVector<unsigned, 64> Record;
2057 LLVMContext &C = CS.getInstruction()->getContext();
2058
2059 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002060 const auto &Bundle = CS.getOperandBundleAt(i);
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002061 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002062
2063 for (auto &Input : Bundle.Inputs)
Teresa Johnson37687f32016-04-23 04:30:47 +00002064 pushValueAndType(Input, InstID, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002065
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002066 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002067 Record.clear();
2068 }
2069}
2070
Teresa Johnson37687f32016-04-23 04:30:47 +00002071/// pushValue - Like pushValueAndType, but where the type of the value is
Jan Wen Voungafaced02012-10-11 20:20:40 +00002072/// omitted (perhaps it was already encoded in an earlier operand).
Teresa Johnson37687f32016-04-23 04:30:47 +00002073void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2074 SmallVectorImpl<unsigned> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002075 unsigned ValID = VE.getValueID(V);
2076 Vals.push_back(InstID - ValID);
2077}
2078
Teresa Johnson37687f32016-04-23 04:30:47 +00002079void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2080 SmallVectorImpl<uint64_t> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002081 unsigned ValID = VE.getValueID(V);
2082 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2083 emitSignedInt64(Vals, diff);
2084}
2085
Chris Lattnere6e364c2007-04-26 05:53:54 +00002086/// WriteInstruction - Emit an instruction to the specified stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002087void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2088 unsigned InstID,
2089 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnere6e364c2007-04-26 05:53:54 +00002090 unsigned Code = 0;
2091 unsigned AbbrevToUse = 0;
Devang Patelaf206b82009-09-18 19:26:43 +00002092 VE.setInstructionID(&I);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002093 switch (I.getOpcode()) {
2094 default:
2095 if (Instruction::isCast(I.getOpcode())) {
Chris Lattner0a603252007-05-01 02:13:26 +00002096 Code = bitc::FUNC_CODE_INST_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002097 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002098 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002099 Vals.push_back(VE.getTypeID(I.getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002100 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
Chris Lattnere6e364c2007-04-26 05:53:54 +00002101 } else {
2102 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattner9f35f912007-05-02 04:26:36 +00002103 Code = bitc::FUNC_CODE_INST_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002104 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002105 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Teresa Johnson37687f32016-04-23 04:30:47 +00002106 pushValue(I.getOperand(1), InstID, Vals);
2107 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2108 uint64_t Flags = getOptimizationFlags(&I);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002109 if (Flags != 0) {
2110 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2111 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2112 Vals.push_back(Flags);
2113 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002114 }
2115 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002116
David Blaikieb5b5efd2015-02-25 01:08:52 +00002117 case Instruction::GetElementPtr: {
Chris Lattner0a603252007-05-01 02:13:26 +00002118 Code = bitc::FUNC_CODE_INST_GEP;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002119 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2120 auto &GEPInst = cast<GetElementPtrInst>(I);
2121 Vals.push_back(GEPInst.isInBounds());
2122 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002123 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002124 pushValueAndType(I.getOperand(i), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002125 break;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002126 }
Dan Gohmanca0256a2008-05-31 19:11:15 +00002127 case Instruction::ExtractValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002128 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002129 pushValueAndType(I.getOperand(0), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002130 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002131 Vals.append(EVI->idx_begin(), EVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002132 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002133 }
2134 case Instruction::InsertValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002135 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002136 pushValueAndType(I.getOperand(0), InstID, Vals);
2137 pushValueAndType(I.getOperand(1), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002138 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002139 Vals.append(IVI->idx_begin(), IVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002140 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002141 }
Chris Lattner0a603252007-05-01 02:13:26 +00002142 case Instruction::Select:
Dan Gohmanc5d28922008-09-16 01:01:33 +00002143 Code = bitc::FUNC_CODE_INST_VSELECT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002144 pushValueAndType(I.getOperand(1), InstID, Vals);
2145 pushValue(I.getOperand(2), InstID, Vals);
2146 pushValueAndType(I.getOperand(0), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002147 break;
2148 case Instruction::ExtractElement:
2149 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002150 pushValueAndType(I.getOperand(0), InstID, Vals);
2151 pushValueAndType(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002152 break;
2153 case Instruction::InsertElement:
2154 Code = bitc::FUNC_CODE_INST_INSERTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002155 pushValueAndType(I.getOperand(0), InstID, Vals);
2156 pushValue(I.getOperand(1), InstID, Vals);
2157 pushValueAndType(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002158 break;
2159 case Instruction::ShuffleVector:
2160 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002161 pushValueAndType(I.getOperand(0), InstID, Vals);
2162 pushValue(I.getOperand(1), InstID, Vals);
2163 pushValue(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002164 break;
2165 case Instruction::ICmp:
James Molloy88eb5352015-07-10 12:52:00 +00002166 case Instruction::FCmp: {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002167 // compare returning Int1Ty or vector of Int1Ty
2168 Code = bitc::FUNC_CODE_INST_CMP2;
Teresa Johnson37687f32016-04-23 04:30:47 +00002169 pushValueAndType(I.getOperand(0), InstID, Vals);
2170 pushValue(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002171 Vals.push_back(cast<CmpInst>(I).getPredicate());
Teresa Johnson37687f32016-04-23 04:30:47 +00002172 uint64_t Flags = getOptimizationFlags(&I);
James Molloy88eb5352015-07-10 12:52:00 +00002173 if (Flags != 0)
2174 Vals.push_back(Flags);
Chris Lattner0a603252007-05-01 02:13:26 +00002175 break;
James Molloy88eb5352015-07-10 12:52:00 +00002176 }
Chris Lattner0a603252007-05-01 02:13:26 +00002177
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002178 case Instruction::Ret:
Devang Patelbbfd8742008-02-26 01:29:32 +00002179 {
2180 Code = bitc::FUNC_CODE_INST_RET;
2181 unsigned NumOperands = I.getNumOperands();
Devang Patelbbfd8742008-02-26 01:29:32 +00002182 if (NumOperands == 0)
2183 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2184 else if (NumOperands == 1) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002185 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Devang Patelbbfd8742008-02-26 01:29:32 +00002186 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2187 } else {
2188 for (unsigned i = 0, e = NumOperands; i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002189 pushValueAndType(I.getOperand(i), InstID, Vals);
Devang Patelbbfd8742008-02-26 01:29:32 +00002190 }
2191 }
Chris Lattner0a603252007-05-01 02:13:26 +00002192 break;
2193 case Instruction::Br:
Gabor Greif1933b002009-01-30 18:27:21 +00002194 {
2195 Code = bitc::FUNC_CODE_INST_BR;
David Blaikieb78e9e52013-02-11 01:16:51 +00002196 const BranchInst &II = cast<BranchInst>(I);
Gabor Greif1933b002009-01-30 18:27:21 +00002197 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2198 if (II.isConditional()) {
2199 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002200 pushValue(II.getCondition(), InstID, Vals);
Gabor Greif1933b002009-01-30 18:27:21 +00002201 }
Chris Lattner0a603252007-05-01 02:13:26 +00002202 }
2203 break;
2204 case Instruction::Switch:
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002205 {
2206 Code = bitc::FUNC_CODE_INST_SWITCH;
David Blaikieb78e9e52013-02-11 01:16:51 +00002207 const SwitchInst &SI = cast<SwitchInst>(I);
Bob Wilsone4077362013-09-09 19:14:35 +00002208 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002209 pushValue(SI.getCondition(), InstID, Vals);
Bob Wilsone4077362013-09-09 19:14:35 +00002210 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
Sanjay Patel225d65f2015-11-13 16:21:23 +00002211 for (SwitchInst::ConstCaseIt Case : SI.cases()) {
2212 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2213 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002214 }
2215 }
Chris Lattner0a603252007-05-01 02:13:26 +00002216 break;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002217 case Instruction::IndirectBr:
2218 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattner3ed871f2009-10-27 19:13:16 +00002219 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Jan Wen Voungafaced02012-10-11 20:20:40 +00002220 // Encode the address operand as relative, but not the basic blocks.
Teresa Johnson37687f32016-04-23 04:30:47 +00002221 pushValue(I.getOperand(0), InstID, Vals);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002222 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
Chris Lattner3ed871f2009-10-27 19:13:16 +00002223 Vals.push_back(VE.getValueID(I.getOperand(i)));
2224 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002225
Chris Lattnerb811e952007-05-01 07:03:37 +00002226 case Instruction::Invoke: {
Gabor Greif4b79e472009-01-16 18:40:27 +00002227 const InvokeInst *II = cast<InvokeInst>(&I);
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002228 const Value *Callee = II->getCalledValue();
2229 FunctionType *FTy = II->getFunctionType();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002230
2231 if (II->hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002232 writeOperandBundles(II, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002233
Chris Lattner0a603252007-05-01 02:13:26 +00002234 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002235
Devang Patel4c758ea2008-09-25 21:00:45 +00002236 Vals.push_back(VE.getAttributeID(II->getAttributes()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002237 Vals.push_back(II->getCallingConv() | 1 << 13);
Gabor Greif6ecd6f42009-01-07 22:39:29 +00002238 Vals.push_back(VE.getValueID(II->getNormalDest()));
2239 Vals.push_back(VE.getValueID(II->getUnwindDest()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002240 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002241 pushValueAndType(Callee, InstID, Vals);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002242
Chris Lattner0a603252007-05-01 02:13:26 +00002243 // Emit value #'s for the fixed parameters.
Chris Lattner0a603252007-05-01 02:13:26 +00002244 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002245 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
Chris Lattner0a603252007-05-01 02:13:26 +00002246
2247 // Emit type/value pairs for varargs params.
2248 if (FTy->isVarArg()) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00002249 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002250 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002251 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
Chris Lattner0a603252007-05-01 02:13:26 +00002252 }
2253 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002254 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002255 case Instruction::Resume:
2256 Code = bitc::FUNC_CODE_INST_RESUME;
Teresa Johnson37687f32016-04-23 04:30:47 +00002257 pushValueAndType(I.getOperand(0), InstID, Vals);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002258 break;
David Majnemer654e1302015-07-31 17:58:14 +00002259 case Instruction::CleanupRet: {
2260 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2261 const auto &CRI = cast<CleanupReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002262 pushValue(CRI.getCleanupPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002263 if (CRI.hasUnwindDest())
2264 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2265 break;
2266 }
2267 case Instruction::CatchRet: {
2268 Code = bitc::FUNC_CODE_INST_CATCHRET;
2269 const auto &CRI = cast<CatchReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002270 pushValue(CRI.getCatchPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002271 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2272 break;
2273 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00002274 case Instruction::CleanupPad:
David Majnemer654e1302015-07-31 17:58:14 +00002275 case Instruction::CatchPad: {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002276 const auto &FuncletPad = cast<FuncletPadInst>(I);
2277 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2278 : bitc::FUNC_CODE_INST_CLEANUPPAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002279 pushValue(FuncletPad.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002280
2281 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
David Majnemer654e1302015-07-31 17:58:14 +00002282 Vals.push_back(NumArgOperands);
2283 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
Teresa Johnson37687f32016-04-23 04:30:47 +00002284 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002285 break;
2286 }
2287 case Instruction::CatchSwitch: {
2288 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2289 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2290
Teresa Johnson37687f32016-04-23 04:30:47 +00002291 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002292
2293 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2294 Vals.push_back(NumHandlers);
2295 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2296 Vals.push_back(VE.getValueID(CatchPadBB));
2297
2298 if (CatchSwitch.hasUnwindDest())
2299 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
David Majnemer654e1302015-07-31 17:58:14 +00002300 break;
2301 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002302 case Instruction::Unreachable:
2303 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002304 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002305 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002306
Jay Foad372ad642011-06-20 14:18:48 +00002307 case Instruction::PHI: {
2308 const PHINode &PN = cast<PHINode>(I);
Chris Lattner0a603252007-05-01 02:13:26 +00002309 Code = bitc::FUNC_CODE_INST_PHI;
Jan Wen Voungafaced02012-10-11 20:20:40 +00002310 // With the newer instruction encoding, forward references could give
2311 // negative valued IDs. This is most common for PHIs, so we use
2312 // signed VBRs.
2313 SmallVector<uint64_t, 128> Vals64;
2314 Vals64.push_back(VE.getTypeID(PN.getType()));
Jay Foad372ad642011-06-20 14:18:48 +00002315 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002316 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002317 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
Jay Foad372ad642011-06-20 14:18:48 +00002318 }
Jan Wen Voungafaced02012-10-11 20:20:40 +00002319 // Emit a Vals64 vector and exit.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002320 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002321 Vals64.clear();
2322 return;
Jay Foad372ad642011-06-20 14:18:48 +00002323 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002324
Bill Wendlingfae14752011-08-12 20:24:12 +00002325 case Instruction::LandingPad: {
2326 const LandingPadInst &LP = cast<LandingPadInst>(I);
2327 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2328 Vals.push_back(VE.getTypeID(LP.getType()));
Bill Wendlingfae14752011-08-12 20:24:12 +00002329 Vals.push_back(LP.isCleanup());
2330 Vals.push_back(LP.getNumClauses());
2331 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2332 if (LP.isCatch(I))
2333 Vals.push_back(LandingPadInst::Catch);
2334 else
2335 Vals.push_back(LandingPadInst::Filter);
Teresa Johnson37687f32016-04-23 04:30:47 +00002336 pushValueAndType(LP.getClause(I), InstID, Vals);
Bill Wendlingfae14752011-08-12 20:24:12 +00002337 }
2338 break;
2339 }
2340
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002341 case Instruction::Alloca: {
Chris Lattner0a603252007-05-01 02:13:26 +00002342 Code = bitc::FUNC_CODE_INST_ALLOCA;
David Blaikiebdb49102015-04-28 16:51:01 +00002343 const AllocaInst &AI = cast<AllocaInst>(I);
2344 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
Dan Gohman9da5bb02010-05-28 01:38:28 +00002345 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002346 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002347 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2348 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2349 "not enough bits for maximum alignment");
2350 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2351 AlignRecord |= AI.isUsedWithInAlloca() << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00002352 AlignRecord |= 1 << 6;
Manman Ren9bfd0d02016-04-01 21:41:15 +00002353 AlignRecord |= AI.isSwiftError() << 7;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002354 Vals.push_back(AlignRecord);
Chris Lattner0a603252007-05-01 02:13:26 +00002355 break;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002356 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002357
Chris Lattner0a603252007-05-01 02:13:26 +00002358 case Instruction::Load:
Eli Friedman59b66882011-08-09 23:02:53 +00002359 if (cast<LoadInst>(I).isAtomic()) {
2360 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002361 pushValueAndType(I.getOperand(0), InstID, Vals);
Eli Friedman59b66882011-08-09 23:02:53 +00002362 } else {
2363 Code = bitc::FUNC_CODE_INST_LOAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002364 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
Eli Friedman59b66882011-08-09 23:02:53 +00002365 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2366 }
David Blaikie85035652015-02-25 01:07:20 +00002367 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002368 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2369 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002370 if (cast<LoadInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002371 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2372 Vals.push_back(getEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002373 }
Chris Lattner0a603252007-05-01 02:13:26 +00002374 break;
2375 case Instruction::Store:
Eli Friedman59b66882011-08-09 23:02:53 +00002376 if (cast<StoreInst>(I).isAtomic())
2377 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2378 else
2379 Code = bitc::FUNC_CODE_INST_STORE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002380 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2381 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
Chris Lattner0a603252007-05-01 02:13:26 +00002382 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2383 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002384 if (cast<StoreInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002385 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2386 Vals.push_back(getEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002387 }
Chris Lattner0a603252007-05-01 02:13:26 +00002388 break;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002389 case Instruction::AtomicCmpXchg:
2390 Code = bitc::FUNC_CODE_INST_CMPXCHG;
Teresa Johnson37687f32016-04-23 04:30:47 +00002391 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2392 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2393 pushValue(I.getOperand(2), InstID, Vals); // newval.
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002394 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002395 Vals.push_back(
2396 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2397 Vals.push_back(
2398 getEncodedSynchScope(cast<AtomicCmpXchgInst>(I).getSynchScope()));
2399 Vals.push_back(
2400 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
Tim Northover420a2162014-06-13 14:24:07 +00002401 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002402 break;
2403 case Instruction::AtomicRMW:
2404 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
Teresa Johnson37687f32016-04-23 04:30:47 +00002405 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2406 pushValue(I.getOperand(1), InstID, Vals); // val.
2407 Vals.push_back(
2408 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002409 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002410 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2411 Vals.push_back(
2412 getEncodedSynchScope(cast<AtomicRMWInst>(I).getSynchScope()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002413 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002414 case Instruction::Fence:
2415 Code = bitc::FUNC_CODE_INST_FENCE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002416 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2417 Vals.push_back(getEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
Eli Friedmanfee02c62011-07-25 23:16:38 +00002418 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002419 case Instruction::Call: {
Gabor Greife9afee22010-06-26 09:35:09 +00002420 const CallInst &CI = cast<CallInst>(I);
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002421 FunctionType *FTy = CI.getFunctionType();
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002422
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002423 if (CI.hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002424 writeOperandBundles(&CI, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002425
Chris Lattnerc44070802011-06-17 18:17:37 +00002426 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002427
Gabor Greife9afee22010-06-26 09:35:09 +00002428 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002429
Teresa Johnson37687f32016-04-23 04:30:47 +00002430 unsigned Flags = getOptimizationFlags(&I);
Akira Hatanaka97cb3972015-11-07 02:48:49 +00002431 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2432 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2433 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2434 1 << bitc::CALL_EXPLICIT_TYPE |
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002435 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2436 unsigned(Flags != 0) << bitc::CALL_FMF);
2437 if (Flags != 0)
2438 Vals.push_back(Flags);
2439
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002440 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002441 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002442
Chris Lattner0a603252007-05-01 02:13:26 +00002443 // Emit value #'s for the fixed parameters.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002444 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2445 // Check for labels (can happen with asm labels).
2446 if (FTy->getParamType(i)->isLabelTy())
2447 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2448 else
Teresa Johnson37687f32016-04-23 04:30:47 +00002449 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002450 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002451
Chris Lattnerb811e952007-05-01 07:03:37 +00002452 // Emit type/value pairs for varargs params.
2453 if (FTy->isVarArg()) {
Gabor Greife9afee22010-06-26 09:35:09 +00002454 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002455 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002456 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
Chris Lattner0a603252007-05-01 02:13:26 +00002457 }
2458 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002459 }
Chris Lattner0a603252007-05-01 02:13:26 +00002460 case Instruction::VAArg:
2461 Code = bitc::FUNC_CODE_INST_VAARG;
2462 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
Teresa Johnson37687f32016-04-23 04:30:47 +00002463 pushValue(I.getOperand(0), InstID, Vals); // valist.
Chris Lattner0a603252007-05-01 02:13:26 +00002464 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2465 break;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002466 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002467
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002468 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002469 Vals.clear();
2470}
2471
Teresa Johnson37687f32016-04-23 04:30:47 +00002472/// Emit names for globals/functions etc. \p IsModuleLevel is true when
2473/// we are writing the module-level VST, where we are including a function
2474/// bitcode index and need to backpatch the VST forward declaration record.
2475void ModuleBitcodeWriter::writeValueSymbolTable(
2476 const ValueSymbolTable &VST, bool IsModuleLevel,
2477 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002478 if (VST.empty()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002479 // writeValueSymbolTableForwardDecl should have returned early as
Teresa Johnsonff642b92015-09-17 20:12:00 +00002480 // well. Ensure this handling remains in sync by asserting that
2481 // the placeholder offset is not set.
Teresa Johnson37687f32016-04-23 04:30:47 +00002482 assert(!IsModuleLevel || !hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002483 return;
2484 }
2485
Teresa Johnson37687f32016-04-23 04:30:47 +00002486 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002487 // Get the offset of the VST we are writing, and backpatch it into
2488 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002489 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002490 // The BitcodeStartBit was the stream offset of the actual bitcode
2491 // (e.g. excluding any initial darwin header).
Teresa Johnson37687f32016-04-23 04:30:47 +00002492 VSTOffset -= bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002493 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002494 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002495 }
2496
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002497 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2eae59f2007-05-04 20:34:50 +00002498
Teresa Johnsonff642b92015-09-17 20:12:00 +00002499 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2500 // records, which are not used in the per-function VSTs.
2501 unsigned FnEntry8BitAbbrev;
2502 unsigned FnEntry7BitAbbrev;
2503 unsigned FnEntry6BitAbbrev;
Teresa Johnson37687f32016-04-23 04:30:47 +00002504 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002505 // 8-bit fixed-width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002506 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2507 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002508 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2509 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002510 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2511 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002512 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002513
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002514 // 7-bit fixed width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002515 Abbv = new BitCodeAbbrev();
2516 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002517 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2518 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002519 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2520 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002521 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002522
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002523 // 6-bit char6 VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002524 Abbv = new BitCodeAbbrev();
2525 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002526 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2527 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002528 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2529 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002530 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002531 }
2532
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002533 // FIXME: Set up the abbrev, we know how many values there are!
2534 // FIXME: We know if the type names can use 7-bit ascii.
2535 SmallVector<unsigned, 64> NameVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002536
Yaron Keren001e2e42015-08-10 07:04:29 +00002537 for (const ValueName &Name : VST) {
Chris Lattner4d925982007-05-04 20:52:02 +00002538 // Figure out the encoding to use for the name.
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002539 StringEncoding Bits =
2540 getStringEncoding(Name.getKeyData(), Name.getKeyLength());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002541
Chris Lattnera0987962007-05-04 21:31:13 +00002542 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002543 NameVals.push_back(VE.getValueID(Name.getValue()));
2544
2545 Function *F = dyn_cast<Function>(Name.getValue());
2546 if (!F) {
2547 // If value is an alias, need to get the aliased base object to
2548 // see if it is a function.
2549 auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2550 if (GA && GA->getBaseObject())
2551 F = dyn_cast<Function>(GA->getBaseObject());
2552 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002553
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002554 // VST_CODE_ENTRY: [valueid, namechar x N]
2555 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
2556 // VST_CODE_BBENTRY: [bbid, namechar x N]
Chris Lattner6be58c62007-05-03 22:18:21 +00002557 unsigned Code;
Yaron Keren001e2e42015-08-10 07:04:29 +00002558 if (isa<BasicBlock>(Name.getValue())) {
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002559 Code = bitc::VST_CODE_BBENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002560 if (Bits == SE_Char6)
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002561 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002562 } else if (F && !F->isDeclaration()) {
2563 // Must be the module-level VST, where we pass in the Index and
2564 // have a VSTOffsetPlaceholder. The function-level VST should not
2565 // contain any Function symbols.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00002566 assert(FunctionToBitcodeIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00002567 assert(hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002568
2569 // Save the word offset of the function (from the start of the
2570 // actual bitcode written to the stream).
Teresa Johnson37687f32016-04-23 04:30:47 +00002571 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002572 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2573 NameVals.push_back(BitcodeIndex / 32);
2574
2575 Code = bitc::VST_CODE_FNENTRY;
2576 AbbrevToUse = FnEntry8BitAbbrev;
2577 if (Bits == SE_Char6)
2578 AbbrevToUse = FnEntry6BitAbbrev;
2579 else if (Bits == SE_Fixed7)
2580 AbbrevToUse = FnEntry7BitAbbrev;
Chris Lattner6be58c62007-05-03 22:18:21 +00002581 } else {
2582 Code = bitc::VST_CODE_ENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002583 if (Bits == SE_Char6)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002584 AbbrevToUse = VST_ENTRY_6_ABBREV;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002585 else if (Bits == SE_Fixed7)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002586 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002587 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002588
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002589 for (const auto P : Name.getKey())
2590 NameVals.push_back((unsigned char)P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002591
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002592 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002593 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002594 NameVals.clear();
2595 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002596 Stream.ExitBlock();
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002597}
2598
Teresa Johnson403a7872015-10-04 14:33:43 +00002599/// Emit function names and summary offsets for the combined index
2600/// used by ThinLTO.
Teresa Johnson37687f32016-04-23 04:30:47 +00002601void IndexBitcodeWriter::writeCombinedValueSymbolTable() {
2602 assert(hasVSTOffsetPlaceholder() && "Expected non-zero VSTOffsetPlaceholder");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002603 // Get the offset of the VST we are writing, and backpatch it into
2604 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002605 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002606 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002607 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002608
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002609 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00002610
Teresa Johnson403a7872015-10-04 14:33:43 +00002611 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002612 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_GVDEFENTRY));
2613 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2614 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // sumoffset
2615 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // guid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002616 unsigned DefEntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002617
2618 Abbv = new BitCodeAbbrev();
2619 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2620 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2621 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002622 unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002623
Teresa Johnsone1164de2016-02-10 21:55:02 +00002624 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson403a7872015-10-04 14:33:43 +00002625
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002626 for (const auto &FII : Index) {
Mehdi Aminiad5741b2016-04-02 05:07:53 +00002627 GlobalValue::GUID FuncGUID = FII.first;
Teresa Johnson37687f32016-04-23 04:30:47 +00002628 unsigned ValueId = popValueId(FuncGUID);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002629
Teresa Johnsone1164de2016-02-10 21:55:02 +00002630 for (const auto &FI : FII.second) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002631 // VST_CODE_COMBINED_GVDEFENTRY: [valueid, sumoffset, guid]
Teresa Johnson37687f32016-04-23 04:30:47 +00002632 NameVals.push_back(ValueId);
Teresa Johnson403a7872015-10-04 14:33:43 +00002633 NameVals.push_back(FI->bitcodeIndex());
Teresa Johnsone1164de2016-02-10 21:55:02 +00002634 NameVals.push_back(FuncGUID);
Teresa Johnson403a7872015-10-04 14:33:43 +00002635
2636 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002637 Stream.EmitRecord(bitc::VST_CODE_COMBINED_GVDEFENTRY, NameVals,
2638 DefEntryAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00002639 NameVals.clear();
2640 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002641 }
Teresa Johnson37687f32016-04-23 04:30:47 +00002642 for (const auto &GVI : valueIds()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002643 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
2644 NameVals.push_back(GVI.second);
2645 NameVals.push_back(GVI.first);
2646
2647 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002648 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002649 NameVals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00002650 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002651 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002652}
2653
Teresa Johnson37687f32016-04-23 04:30:47 +00002654void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002655 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2656 unsigned Code;
2657 if (isa<BasicBlock>(Order.V))
2658 Code = bitc::USELIST_CODE_BB;
2659 else
2660 Code = bitc::USELIST_CODE_DEFAULT;
2661
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002662 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002663 Record.push_back(VE.getValueID(Order.V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002664 Stream.EmitRecord(Code, Record);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002665}
2666
Teresa Johnson37687f32016-04-23 04:30:47 +00002667void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002668 assert(VE.shouldPreserveUseListOrder() &&
2669 "Expected to be preserving use-list order");
2670
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002671 auto hasMore = [&]() {
2672 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2673 };
2674 if (!hasMore())
2675 // Nothing to do.
2676 return;
2677
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002678 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002679 while (hasMore()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002680 writeUseList(std::move(VE.UseListOrders.back()));
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002681 VE.UseListOrders.pop_back();
2682 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002683 Stream.ExitBlock();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002684}
2685
Teresa Johnson403a7872015-10-04 14:33:43 +00002686/// Emit a function body to the module stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002687void ModuleBitcodeWriter::writeFunction(
2688 const Function &F,
2689 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002690 // Save the bitcode index of the start of this function block for recording
2691 // in the VST.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002692 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002693
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002694 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a11b642011-06-03 17:02:19 +00002695 VE.incorporateFunction(F);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002696
2697 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002698
Chris Lattnere6e364c2007-04-26 05:53:54 +00002699 // Emit the number of basic blocks, so the reader can create them ahead of
2700 // time.
2701 Vals.push_back(VE.getBasicBlocks().size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002702 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002703 Vals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002704
Chris Lattnere6e364c2007-04-26 05:53:54 +00002705 // If there are function-local constants, emit them now.
2706 unsigned CstStart, CstEnd;
2707 VE.getFunctionConstantRange(CstStart, CstEnd);
Teresa Johnson37687f32016-04-23 04:30:47 +00002708 writeConstants(CstStart, CstEnd, false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002709
Victor Hernandez6c730de2010-01-14 01:50:08 +00002710 // If there is function-local metadata, emit it now.
Teresa Johnson37687f32016-04-23 04:30:47 +00002711 writeFunctionMetadata(F);
Victor Hernandez6c730de2010-01-14 01:50:08 +00002712
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002713 // Keep a running idea of what the instruction ID is.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002714 unsigned InstID = CstEnd;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002715
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002716 bool NeedsMetadataAttachment = F.hasMetadata();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002717
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002718 DILocation *LastDL = nullptr;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002719 // Finally, emit all the instructions, in order.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002720 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002721 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2722 I != E; ++I) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002723 writeInstruction(*I, InstID, Vals);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002724
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00002725 if (!I->getType()->isVoidTy())
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002726 ++InstID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002727
Chris Lattner07d09ed2010-04-03 02:17:50 +00002728 // If the instruction has metadata, write a metadata attachment later.
2729 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002730
Chris Lattner07d09ed2010-04-03 02:17:50 +00002731 // If the instruction has a debug location, emit it.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002732 DILocation *DL = I->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002733 if (!DL)
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002734 continue;
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002735
2736 if (DL == LastDL) {
Chris Lattner07d09ed2010-04-03 02:17:50 +00002737 // Just repeat the same debug loc as last time.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002738 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002739 continue;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002740 }
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002741
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002742 Vals.push_back(DL->getLine());
2743 Vals.push_back(DL->getColumn());
2744 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2745 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002746 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002747 Vals.clear();
Duncan P. N. Exon Smith538ef562015-05-06 22:51:12 +00002748
2749 LastDL = DL;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002750 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002751
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002752 // Emit names for all the instructions etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002753 writeValueSymbolTable(F.getValueSymbolTable());
Devang Patelaf206b82009-09-18 19:26:43 +00002754
Chris Lattner07d09ed2010-04-03 02:17:50 +00002755 if (NeedsMetadataAttachment)
Teresa Johnson37687f32016-04-23 04:30:47 +00002756 writeMetadataAttachment(F);
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002757 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00002758 writeUseListBlock(&F);
Chad Rosier6a11b642011-06-03 17:02:19 +00002759 VE.purgeFunction();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002760 Stream.ExitBlock();
Chris Lattner831d4202007-04-26 03:27:58 +00002761}
2762
Chris Lattner702658c2007-05-04 18:26:27 +00002763// Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002764void ModuleBitcodeWriter::writeBlockInfo() {
Chris Lattner702658c2007-05-04 18:26:27 +00002765 // We only want to emit block info records for blocks that have multiple
Jan Wen Voungafaced02012-10-11 20:20:40 +00002766 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
Jan Wen Voung8c9e9412012-10-11 21:45:16 +00002767 // Other blocks can define their abbrevs inline.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002768 Stream.EnterBlockInfoBlock(2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002769
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002770 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002771 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2772 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2773 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2774 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2775 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002776 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002777 VST_ENTRY_8_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002778 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002779 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002780
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002781 { // 7-bit fixed width VST_CODE_ENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002782 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2783 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2784 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002787 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002788 VST_ENTRY_7_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002789 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002790 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002791 { // 6-bit char6 VST_CODE_ENTRY strings.
Chris Lattnere760d6f2007-05-05 01:26:50 +00002792 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2793 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2794 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2795 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2796 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002797 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002798 VST_ENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002799 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere760d6f2007-05-05 01:26:50 +00002800 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002801 { // 6-bit char6 VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002802 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2803 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2804 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2805 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnere760d6f2007-05-05 01:26:50 +00002806 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002807 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002808 VST_BBENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002809 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002810 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002811
2812
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002813
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002814 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2815 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2816 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
David Blaikie7b028102015-02-25 00:51:52 +00002818 VE.computeBitsRequiredForTypeIndicies()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002819 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002820 CONSTANTS_SETTYPE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002821 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002822 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002823
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002824 { // INTEGER abbrev for CONSTANTS_BLOCK.
2825 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2826 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002828 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002829 CONSTANTS_INTEGER_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002830 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002831 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002832
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002833 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2834 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2835 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2836 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2837 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
David Blaikie7b028102015-02-25 00:51:52 +00002838 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002839 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2840
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002841 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002842 CONSTANTS_CE_CAST_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002843 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002844 }
2845 { // NULL abbrev for CONSTANTS_BLOCK.
2846 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2847 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002848 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002849 CONSTANTS_NULL_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002850 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002851 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002852
Chris Lattnerb80751d2007-05-05 07:44:49 +00002853 // FIXME: This should only use space for first class types!
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002854
Chris Lattnerb80751d2007-05-05 07:44:49 +00002855 { // INST_LOAD abbrev for FUNCTION_BLOCK.
2856 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2857 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattnerb80751d2007-05-05 07:44:49 +00002858 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
David Blaikie85035652015-02-25 01:07:20 +00002859 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2860 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerb80751d2007-05-05 07:44:49 +00002861 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
2862 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002863 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002864 FUNCTION_INST_LOAD_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002865 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerb80751d2007-05-05 07:44:49 +00002866 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002867 { // INST_BINOP abbrev for FUNCTION_BLOCK.
2868 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2869 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2870 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2871 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2872 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002873 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002874 FUNCTION_INST_BINOP_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002875 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002876 }
Dan Gohman0ebd6962009-07-20 21:19:07 +00002877 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2878 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2879 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2880 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2881 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2882 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2883 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002884 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002885 FUNCTION_INST_BINOP_FLAGS_ABBREV)
Dan Gohman0ebd6962009-07-20 21:19:07 +00002886 llvm_unreachable("Unexpected abbrev ordering!");
2887 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002888 { // INST_CAST abbrev for FUNCTION_BLOCK.
2889 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2890 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2891 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2892 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
David Blaikie7b028102015-02-25 00:51:52 +00002893 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002894 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002895 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002896 FUNCTION_INST_CAST_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002897 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002898 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002899
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002900 { // INST_RET abbrev for FUNCTION_BLOCK.
2901 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2902 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002903 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002904 FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002905 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002906 }
2907 { // INST_RET abbrev for FUNCTION_BLOCK.
2908 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2909 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2910 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002911 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002912 FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002913 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002914 }
2915 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2916 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2917 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002918 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002919 FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002920 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002921 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00002922 {
2923 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2924 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2925 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2927 Log2_32_Ceil(VE.getTypes().size() + 1)));
2928 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2929 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002930 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
David Blaikieb5b5efd2015-02-25 01:08:52 +00002931 FUNCTION_INST_GEP_ABBREV)
2932 llvm_unreachable("Unexpected abbrev ordering!");
2933 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002934
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002935 Stream.ExitBlock();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002936}
2937
Teresa Johnson403a7872015-10-04 14:33:43 +00002938/// Write the module path strings, currently only used when generating
2939/// a combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00002940void IndexBitcodeWriter::writeModStrings() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002941 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00002942
2943 // TODO: See which abbrev sizes we actually need to emit
2944
2945 // 8-bit fixed-width MST_ENTRY strings.
2946 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2947 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2948 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2949 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2950 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002951 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002952
2953 // 7-bit fixed width MST_ENTRY strings.
2954 Abbv = new BitCodeAbbrev();
2955 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2956 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002959 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002960
2961 // 6-bit char6 MST_ENTRY strings.
2962 Abbv = new BitCodeAbbrev();
2963 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2964 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2966 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002967 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002968
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002969 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
2970 Abbv = new BitCodeAbbrev();
2971 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
2972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2974 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2975 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2976 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002977 unsigned AbbrevHash = Stream.EmitAbbrev(Abbv);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002978
2979 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002980 for (const auto &MPSE : Index.modulePaths()) {
Teresa Johnson403a7872015-10-04 14:33:43 +00002981 StringEncoding Bits =
2982 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
2983 unsigned AbbrevToUse = Abbrev8Bit;
2984 if (Bits == SE_Char6)
2985 AbbrevToUse = Abbrev6Bit;
2986 else if (Bits == SE_Fixed7)
2987 AbbrevToUse = Abbrev7Bit;
2988
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002989 Vals.push_back(MPSE.getValue().first);
Teresa Johnson403a7872015-10-04 14:33:43 +00002990
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002991 for (const auto P : MPSE.getKey())
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002992 Vals.push_back((unsigned char)P);
Teresa Johnson403a7872015-10-04 14:33:43 +00002993
2994 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002995 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002996
2997 Vals.clear();
2998 // Emit an optional hash for the module now
2999 auto &Hash = MPSE.getValue().second;
3000 bool AllZero = true; // Detect if the hash is empty, and do not generate it
3001 for (auto Val : Hash) {
3002 if (Val)
3003 AllZero = false;
3004 Vals.push_back(Val);
3005 }
3006 if (!AllZero) {
3007 // Emit the hash record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003008 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003009 }
3010
3011 Vals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00003012 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003013 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003014}
3015
3016// Helper to emit a single function summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003017void ModuleBitcodeWriter::writePerModuleFunctionSummaryRecord(
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003018 SmallVector<uint64_t, 64> &NameVals, GlobalValueInfo *Info,
Teresa Johnson37687f32016-04-23 04:30:47 +00003019 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3020 const Function &F) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003021 NameVals.push_back(ValueID);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003022
3023 FunctionSummary *FS = cast<FunctionSummary>(Info->summary());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003024 NameVals.push_back(getEncodedLinkage(FS->linkage()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003025 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003026 NameVals.push_back(FS->refs().size());
3027
3028 for (auto &RI : FS->refs())
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003029 NameVals.push_back(VE.getValueID(RI.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003030
3031 bool HasProfileData = F.getEntryCount().hasValue();
Teresa Johnsonaae26102016-03-25 18:59:13 +00003032 for (auto &ECI : FS->calls()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003033 NameVals.push_back(VE.getValueID(ECI.first.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003034 assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite");
3035 NameVals.push_back(ECI.second.CallsiteCount);
3036 if (HasProfileData)
3037 NameVals.push_back(ECI.second.ProfileCount);
3038 }
3039
3040 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3041 unsigned Code =
3042 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
Teresa Johnson403a7872015-10-04 14:33:43 +00003043
3044 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003045 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003046 NameVals.clear();
3047}
3048
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003049// Collect the global value references in the given variable's initializer,
3050// and emit them in a summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003051void ModuleBitcodeWriter::writeModuleLevelReferences(
3052 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3053 unsigned FSModRefsAbbrev) {
Teresa Johnson13968092016-03-15 19:35:45 +00003054 // Only interested in recording variable defs in the summary.
3055 if (V.isDeclaration())
3056 return;
Teresa Johnson13968092016-03-15 19:35:45 +00003057 NameVals.push_back(VE.getValueID(&V));
3058 NameVals.push_back(getEncodedLinkage(V.getLinkage()));
Teresa Johnson37687f32016-04-23 04:30:47 +00003059 auto *Info = Index->getGlobalValueInfo(V);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003060 GlobalVarSummary *VS = cast<GlobalVarSummary>(Info->summary());
3061 for (auto Ref : VS->refs())
3062 NameVals.push_back(VE.getValueID(Ref.getValue()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003063 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3064 FSModRefsAbbrev);
Teresa Johnson13968092016-03-15 19:35:45 +00003065 NameVals.clear();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003066}
Teresa Johnson403a7872015-10-04 14:33:43 +00003067
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003068/// Emit the per-module summary section alongside the rest of
3069/// the module's bitcode.
Teresa Johnson37687f32016-04-23 04:30:47 +00003070void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003071 if (M.empty())
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003072 return;
3073
Teresa Johnson37687f32016-04-23 04:30:47 +00003074 if (Index->begin() == Index->end())
Teresa Johnsonb35cc692016-04-20 14:39:45 +00003075 return;
3076
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003077 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003078
3079 // Abbrev for FS_PERMODULE.
Teresa Johnson403a7872015-10-04 14:33:43 +00003080 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003081 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003082 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson5e22e442016-02-06 16:07:35 +00003083 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003084 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003085 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3086 // numrefs x valueid, n x (valueid, callsitecount)
3087 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3088 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003089 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003090
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003091 // Abbrev for FS_PERMODULE_PROFILE.
3092 Abbv = new BitCodeAbbrev();
3093 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3094 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3095 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
3096 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3097 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3098 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3099 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3100 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003101 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003102
3103 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3104 Abbv = new BitCodeAbbrev();
3105 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3106 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3107 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
3108 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3109 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003110 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003111
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003112 // Abbrev for FS_ALIAS.
3113 Abbv = new BitCodeAbbrev();
3114 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3116 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
3117 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003118 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003119
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003120 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003121 // Iterate over the list of functions instead of the Index to
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003122 // ensure the ordering is stable.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003123 for (const Function &F : M) {
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003124 if (F.isDeclaration())
3125 continue;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003126 // Summary emission does not support anonymous functions, they have to
3127 // renamed using the anonymous function renaming pass.
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003128 if (!F.hasName())
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003129 report_fatal_error("Unexpected anonymous function when writing summary");
Teresa Johnson403a7872015-10-04 14:33:43 +00003130
Teresa Johnson37687f32016-04-23 04:30:47 +00003131 auto *Info = Index->getGlobalValueInfo(F);
3132 writePerModuleFunctionSummaryRecord(
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003133 NameVals, Info,
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003134 VE.getValueID(M.getValueSymbolTable().lookup(F.getName())),
Teresa Johnson37687f32016-04-23 04:30:47 +00003135 FSCallsAbbrev, FSCallsProfileAbbrev, F);
Teresa Johnson403a7872015-10-04 14:33:43 +00003136 }
3137
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003138 // Capture references from GlobalVariable initializers, which are outside
3139 // of a function scope.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003140 for (const GlobalVariable &G : M.globals())
Teresa Johnson37687f32016-04-23 04:30:47 +00003141 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003142
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003143 for (const GlobalAlias &A : M.aliases()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003144 auto *Aliasee = A.getBaseObject();
3145 if (!Aliasee->hasName())
3146 // Nameless function don't have an entry in the summary, skip it.
3147 continue;
3148 auto AliasId = VE.getValueID(&A);
3149 auto AliaseeId = VE.getValueID(Aliasee);
3150 NameVals.push_back(AliasId);
3151 NameVals.push_back(getEncodedLinkage(A.getLinkage()));
3152 NameVals.push_back(AliaseeId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003153 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003154 NameVals.clear();
3155 }
3156
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003157 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003158}
3159
Teresa Johnson26ab5772016-03-15 00:04:37 +00003160/// Emit the combined summary section into the combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003161void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003162 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00003163
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003164 // Abbrev for FS_COMBINED.
Teresa Johnson403a7872015-10-04 14:33:43 +00003165 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003166 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
3167 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Teresa Johnson5e22e442016-02-06 16:07:35 +00003168 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003169 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3170 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3171 // numrefs x valueid, n x (valueid, callsitecount)
3172 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3173 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003174 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003175
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003176 // Abbrev for FS_COMBINED_PROFILE.
3177 Abbv = new BitCodeAbbrev();
3178 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
3179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
3180 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
3181 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3182 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3183 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3184 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3185 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003186 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003187
3188 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
3189 Abbv = new BitCodeAbbrev();
3190 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
3191 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
3192 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
3193 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003195 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003196
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003197 // Abbrev for FS_COMBINED_ALIAS.
3198 Abbv = new BitCodeAbbrev();
3199 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
3200 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
3201 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
3202 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // offset
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003203 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003204
3205 // The aliases are emitted as a post-pass, and will point to the summary
3206 // offset id of the aliasee. For this purpose we need to be able to get back
3207 // from the summary to the offset
3208 SmallVector<GlobalValueInfo *, 64> Aliases;
3209 DenseMap<const GlobalValueSummary *, uint64_t> SummaryToOffsetMap;
3210
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003211 SmallVector<uint64_t, 64> NameVals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003212 for (const auto &FII : Index) {
Teresa Johnsone1164de2016-02-10 21:55:02 +00003213 for (auto &FI : FII.second) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003214 GlobalValueSummary *S = FI->summary();
3215 assert(S);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003216 if (isa<AliasSummary>(S)) {
3217 // Will process aliases as a post-pass because the reader wants all
3218 // global to be loaded first.
3219 Aliases.push_back(FI.get());
3220 continue;
3221 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003222
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003223 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003224 NameVals.push_back(Index.getModuleId(VS->modulePath()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003225 NameVals.push_back(getEncodedLinkage(VS->linkage()));
3226 for (auto &RI : VS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003227 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003228 }
3229
3230 // Record the starting offset of this summary entry for use
3231 // in the VST entry. Add the current code size since the
3232 // reader will invoke readRecord after the abbrev id read.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003233 FI->setBitcodeIndex(Stream.GetCurrentBitNo() +
3234 Stream.GetAbbrevIDWidth());
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003235 // Store temporarily the offset in the map for a possible alias.
3236 SummaryToOffsetMap[S] = FI->bitcodeIndex();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003237
3238 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003239 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3240 FSModRefsAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003241 NameVals.clear();
3242 continue;
3243 }
3244
Teresa Johnson2794f712016-03-15 02:41:29 +00003245 auto *FS = cast<FunctionSummary>(S);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003246 NameVals.push_back(Index.getModuleId(FS->modulePath()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003247 NameVals.push_back(getEncodedLinkage(FS->linkage()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003248 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003249 NameVals.push_back(FS->refs().size());
3250
3251 for (auto &RI : FS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003252 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003253 }
3254
3255 bool HasProfileData = false;
Teresa Johnsonaae26102016-03-25 18:59:13 +00003256 for (auto &EI : FS->calls()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003257 HasProfileData |= EI.second.ProfileCount != 0;
3258 if (HasProfileData)
3259 break;
3260 }
3261
Teresa Johnsonaae26102016-03-25 18:59:13 +00003262 for (auto &EI : FS->calls()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003263 // If this GUID doesn't have a value id, it doesn't have a function
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003264 // summary and we don't need to record any calls to it.
Teresa Johnson37687f32016-04-23 04:30:47 +00003265 if (!hasValueId(EI.first.getGUID()))
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003266 continue;
Teresa Johnson37687f32016-04-23 04:30:47 +00003267 NameVals.push_back(getValueId(EI.first.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003268 assert(EI.second.CallsiteCount > 0 && "Expected at least one callsite");
3269 NameVals.push_back(EI.second.CallsiteCount);
3270 if (HasProfileData)
3271 NameVals.push_back(EI.second.ProfileCount);
3272 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003273
3274 // Record the starting offset of this summary entry for use
3275 // in the VST entry. Add the current code size since the
3276 // reader will invoke readRecord after the abbrev id read.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003277 FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003278 // Store temporarily the offset in the map for a possible alias.
3279 SummaryToOffsetMap[S] = FI->bitcodeIndex();
Teresa Johnson403a7872015-10-04 14:33:43 +00003280
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003281 unsigned FSAbbrev =
3282 (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3283 unsigned Code =
3284 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
3285
Teresa Johnson403a7872015-10-04 14:33:43 +00003286 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003287 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003288 NameVals.clear();
3289 }
3290 }
3291
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003292 for (auto GVI : Aliases) {
3293 AliasSummary *AS = cast<AliasSummary>(GVI->summary());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003294 NameVals.push_back(Index.getModuleId(AS->modulePath()));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003295 NameVals.push_back(getEncodedLinkage(AS->linkage()));
3296 auto AliaseeOffset = SummaryToOffsetMap[&AS->getAliasee()];
3297 assert(AliaseeOffset);
3298 NameVals.push_back(AliaseeOffset);
3299
3300 // Record the starting offset of this summary entry for use
3301 // in the VST entry. Add the current code size since the
3302 // reader will invoke readRecord after the abbrev id read.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003303 GVI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003304
3305 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003306 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003307 NameVals.clear();
3308 }
3309
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003310 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003311}
3312
Teresa Johnson37687f32016-04-23 04:30:47 +00003313void ModuleBitcodeWriter::writeIdentificationBlock() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003314 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
Mehdi Amini5d303282015-10-26 18:37:00 +00003315
3316 // Write the "user readable" string identifying the bitcode producer
3317 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3318 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3319 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3320 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003321 auto StringAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson37687f32016-04-23 04:30:47 +00003322 writeStringRecord(bitc::IDENTIFICATION_CODE_STRING,
3323 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
Mehdi Amini5d303282015-10-26 18:37:00 +00003324
3325 // Write the epoch version
3326 Abbv = new BitCodeAbbrev();
3327 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3328 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003329 auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini5d303282015-10-26 18:37:00 +00003330 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003331 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3332 Stream.ExitBlock();
Mehdi Amini5d303282015-10-26 18:37:00 +00003333}
3334
Teresa Johnson37687f32016-04-23 04:30:47 +00003335void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003336 // Emit the module's hash.
3337 // MODULE_CODE_HASH: [5*i32]
3338 SHA1 Hasher;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003339 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&(Buffer)[BlockStartPos],
3340 Buffer.size() - BlockStartPos));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003341 auto Hash = Hasher.result();
3342 SmallVector<uint64_t, 20> Vals;
3343 auto LShift = [&](unsigned char Val, unsigned Amount)
3344 -> uint64_t { return ((uint64_t)Val) << Amount; };
3345 for (int Pos = 0; Pos < 20; Pos += 4) {
3346 uint32_t SubHash = LShift(Hash[Pos + 0], 24);
3347 SubHash |= LShift(Hash[Pos + 1], 16) | LShift(Hash[Pos + 2], 8) |
3348 (unsigned)(unsigned char)Hash[Pos + 3];
3349 Vals.push_back(SubHash);
3350 }
3351
3352 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003353 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003354}
3355
Teresa Johnson37687f32016-04-23 04:30:47 +00003356void BitcodeWriter::write() {
3357 // Emit the file header first.
3358 writeBitcodeHeader();
3359
3360 writeBlocks();
3361}
3362
3363void ModuleBitcodeWriter::writeBlocks() {
3364 writeIdentificationBlock();
3365 writeModule();
3366}
3367
3368void IndexBitcodeWriter::writeBlocks() {
3369 // Index contains only a single outer (module) block.
3370 writeIndex();
3371}
3372
3373void ModuleBitcodeWriter::writeModule() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003374 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3375 size_t BlockStartPos = Buffer.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003376
Jan Wen Voungafaced02012-10-11 20:20:40 +00003377 SmallVector<unsigned, 1> Vals;
3378 unsigned CurVersion = 1;
3379 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003380 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003381
3382 // Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003383 writeBlockInfo();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003384
Bill Wendlingdc095552013-02-10 23:09:32 +00003385 // Emit information about attribute groups.
Teresa Johnson37687f32016-04-23 04:30:47 +00003386 writeAttributeGroupTable();
Bill Wendlingdc095552013-02-10 23:09:32 +00003387
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003388 // Emit information about parameter attributes.
Teresa Johnson37687f32016-04-23 04:30:47 +00003389 writeAttributeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003390
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003391 // Emit information describing all of the types in the module.
Teresa Johnson37687f32016-04-23 04:30:47 +00003392 writeTypeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003393
Teresa Johnson37687f32016-04-23 04:30:47 +00003394 writeComdats();
David Majnemerdad0a642014-06-27 18:19:56 +00003395
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003396 // Emit top-level description of module, including target triple, inline asm,
3397 // descriptors for global variables, and function prototype info.
Teresa Johnson37687f32016-04-23 04:30:47 +00003398 writeModuleInfo();
Devang Patel7428d8a2009-07-22 17:43:22 +00003399
Devang Patele059ba6e2009-07-23 01:07:34 +00003400 // Emit constants.
Teresa Johnson37687f32016-04-23 04:30:47 +00003401 writeModuleConstants();
Devang Patele059ba6e2009-07-23 01:07:34 +00003402
Devang Patel8cca7b42009-08-04 05:01:35 +00003403 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003404 writeModuleMetadata();
Devang Patel8cca7b42009-08-04 05:01:35 +00003405
Devang Patelaf206b82009-09-18 19:26:43 +00003406 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003407 writeModuleMetadataStore();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003408
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003409 // Emit module-level use-lists.
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003410 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003411 writeUseListBlock(nullptr);
Chad Rosierca2567b2011-12-07 21:44:12 +00003412
Teresa Johnson37687f32016-04-23 04:30:47 +00003413 writeOperandBundleTags();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003414
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003415 // Emit function bodies.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003416 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003417 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003418 if (!F->isDeclaration())
Teresa Johnson37687f32016-04-23 04:30:47 +00003419 writeFunction(*F, FunctionToBitcodeIndex);
Teresa Johnson403a7872015-10-04 14:33:43 +00003420
3421 // Need to write after the above call to WriteFunction which populates
3422 // the summary information in the index.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003423 if (Index)
Teresa Johnson37687f32016-04-23 04:30:47 +00003424 writePerModuleGlobalValueSummary();
Teresa Johnsonff642b92015-09-17 20:12:00 +00003425
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003426 writeValueSymbolTable(M.getValueSymbolTable(),
Teresa Johnson37687f32016-04-23 04:30:47 +00003427 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003428
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003429 if (GenerateHash) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003430 writeModuleHash(BlockStartPos);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003431 }
3432
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003433 Stream.ExitBlock();
Chris Lattner702658c2007-05-04 18:26:27 +00003434}
3435
Teresa Johnson37687f32016-04-23 04:30:47 +00003436static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3437 uint32_t &Position) {
3438 support::endian::write32le(&Buffer[Position], Value);
3439 Position += 4;
3440}
3441
3442/// If generating a bc file on darwin, we have to emit a
Chris Lattnera660f4b2008-07-09 05:14:23 +00003443/// header and trailer to make it compatible with the system archiver. To do
3444/// this we emit the following header, and then emit a trailer that pads the
3445/// file out to be a multiple of 16 bytes.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003446///
Chris Lattnera660f4b2008-07-09 05:14:23 +00003447/// struct bc_header {
3448/// uint32_t Magic; // 0x0B17C0DE
3449/// uint32_t Version; // Version, currently always 0.
3450/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3451/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3452/// uint32_t CPUType; // CPU specifier.
3453/// ... potentially more later ...
3454/// };
Teresa Johnson37687f32016-04-23 04:30:47 +00003455static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003456 const Triple &TT) {
Chris Lattnera660f4b2008-07-09 05:14:23 +00003457 unsigned CPUType = ~0U;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003458
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003459 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng545d3602010-02-12 20:39:35 +00003460 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3461 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3462 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattnera660f4b2008-07-09 05:14:23 +00003463 enum {
3464 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3465 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003466 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattnera660f4b2008-07-09 05:14:23 +00003467 DARWIN_CPU_TYPE_POWERPC = 18
3468 };
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003469
Evan Chengcffdcae2011-06-14 01:51:33 +00003470 Triple::ArchType Arch = TT.getArch();
3471 if (Arch == Triple::x86_64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003472 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003473 else if (Arch == Triple::x86)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003474 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chengcffdcae2011-06-14 01:51:33 +00003475 else if (Arch == Triple::ppc)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003476 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chengcffdcae2011-06-14 01:51:33 +00003477 else if (Arch == Triple::ppc64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003478 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003479 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003480 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003481
Chris Lattnera660f4b2008-07-09 05:14:23 +00003482 // Traditional Bitcode starts after header.
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003483 assert(Buffer.size() >= BWH_HeaderSize &&
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003484 "Expected header size to be reserved");
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003485 unsigned BCOffset = BWH_HeaderSize;
3486 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003487
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003488 // Write the magic and version.
3489 unsigned Position = 0;
Teresa Johnson37687f32016-04-23 04:30:47 +00003490 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
3491 writeInt32ToBuffer(0, Buffer, Position); // Version.
3492 writeInt32ToBuffer(BCOffset, Buffer, Position);
3493 writeInt32ToBuffer(BCSize, Buffer, Position);
3494 writeInt32ToBuffer(CPUType, Buffer, Position);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003495
Chris Lattnera660f4b2008-07-09 05:14:23 +00003496 // If the file is not a multiple of 16 bytes, insert dummy padding.
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003497 while (Buffer.size() & 15)
3498 Buffer.push_back(0);
Chris Lattnera660f4b2008-07-09 05:14:23 +00003499}
3500
Teresa Johnson403a7872015-10-04 14:33:43 +00003501/// Helper to write the header common to all bitcode files.
Teresa Johnson37687f32016-04-23 04:30:47 +00003502void BitcodeWriter::writeBitcodeHeader() {
Teresa Johnson403a7872015-10-04 14:33:43 +00003503 // Emit the file header.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003504 Stream.Emit((unsigned)'B', 8);
3505 Stream.Emit((unsigned)'C', 8);
3506 Stream.Emit(0x0, 4);
3507 Stream.Emit(0xC, 4);
3508 Stream.Emit(0xE, 4);
3509 Stream.Emit(0xD, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00003510}
3511
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003512/// WriteBitcodeToFile - Write the specified module to the specified output
3513/// stream.
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +00003514void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
Teresa Johnson403a7872015-10-04 14:33:43 +00003515 bool ShouldPreserveUseListOrder,
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003516 const ModuleSummaryIndex *Index,
3517 bool GenerateHash) {
Michael Ilsemane26658d2012-12-03 21:29:36 +00003518 SmallVector<char, 0> Buffer;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003519 Buffer.reserve(256*1024);
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003520
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003521 // If this is darwin or another generic macho target, reserve space for the
3522 // header.
3523 Triple TT(M->getTargetTriple());
Akira Hatanaka1235d282016-01-23 16:02:10 +00003524 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003525 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003526
3527 // Emit the module into the buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003528 ModuleBitcodeWriter ModuleWriter(M, Buffer, ShouldPreserveUseListOrder, Index,
3529 GenerateHash);
Teresa Johnson37687f32016-04-23 04:30:47 +00003530 ModuleWriter.write();
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003531
Akira Hatanaka1235d282016-01-23 16:02:10 +00003532 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Teresa Johnson37687f32016-04-23 04:30:47 +00003533 emitDarwinBCHeaderAndTrailer(Buffer, TT);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003534
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003535 // Write the generated bitstream to "Out".
3536 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003537}
Teresa Johnson403a7872015-10-04 14:33:43 +00003538
Teresa Johnson37687f32016-04-23 04:30:47 +00003539void IndexBitcodeWriter::writeIndex() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003540 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Teresa Johnson37687f32016-04-23 04:30:47 +00003541
3542 SmallVector<unsigned, 1> Vals;
3543 unsigned CurVersion = 1;
3544 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003545 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Teresa Johnson37687f32016-04-23 04:30:47 +00003546
3547 // If we have a VST, write the VSTOFFSET record placeholder.
3548 writeValueSymbolTableForwardDecl();
3549
3550 // Write the module paths in the combined index.
3551 writeModStrings();
3552
3553 // Write the summary combined index records.
3554 writeCombinedGlobalValueSummary();
3555
3556 // Need a special VST writer for the combined index (we don't have a
3557 // real VST and real values when this is invoked).
3558 writeCombinedValueSymbolTable();
3559
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003560 Stream.ExitBlock();
Teresa Johnson37687f32016-04-23 04:30:47 +00003561}
3562
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003563// Write the specified module summary index to the given raw output stream,
Teresa Johnson403a7872015-10-04 14:33:43 +00003564// where it will be written in a new bitcode block. This is used when
3565// writing the combined index file for ThinLTO.
Teresa Johnson26ab5772016-03-15 00:04:37 +00003566void llvm::WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003567 SmallVector<char, 0> Buffer;
3568 Buffer.reserve(256 * 1024);
3569
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003570 IndexBitcodeWriter IndexWriter(Buffer, Index);
Teresa Johnson37687f32016-04-23 04:30:47 +00003571 IndexWriter.write();
Teresa Johnson403a7872015-10-04 14:33:43 +00003572
3573 Out.write((char *)&Buffer.front(), Buffer.size());
3574}