blob: fcce553cd1d80130346351764255ecdca46148f5 [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
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000789// Decode the flags for GlobalValue in the summary
790static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
791 uint64_t RawFlags = 0;
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000792
793 RawFlags |= Flags.HasSection; // bool
794
795 // Linkage don't need to be remapped at that time for the summary. Any future
796 // change to the getEncodedLinkage() function will need to be taken into
797 // account here as well.
798 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
799
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000800 return RawFlags;
801}
802
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000803static unsigned getEncodedVisibility(const GlobalValue &GV) {
804 switch (GV.getVisibility()) {
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000805 case GlobalValue::DefaultVisibility: return 0;
806 case GlobalValue::HiddenVisibility: return 1;
807 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000808 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000809 llvm_unreachable("Invalid visibility");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000810}
811
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000812static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
813 switch (GV.getDLLStorageClass()) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000814 case GlobalValue::DefaultStorageClass: return 0;
815 case GlobalValue::DLLImportStorageClass: return 1;
816 case GlobalValue::DLLExportStorageClass: return 2;
817 }
818 llvm_unreachable("Invalid DLL storage class");
819}
820
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000821static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000822 switch (GV.getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000823 case GlobalVariable::NotThreadLocal: return 0;
824 case GlobalVariable::GeneralDynamicTLSModel: return 1;
825 case GlobalVariable::LocalDynamicTLSModel: return 2;
826 case GlobalVariable::InitialExecTLSModel: return 3;
827 case GlobalVariable::LocalExecTLSModel: return 4;
828 }
829 llvm_unreachable("Invalid TLS model");
830}
831
David Majnemerdad0a642014-06-27 18:19:56 +0000832static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
833 switch (C.getSelectionKind()) {
834 case Comdat::Any:
835 return bitc::COMDAT_SELECTION_KIND_ANY;
836 case Comdat::ExactMatch:
837 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
838 case Comdat::Largest:
839 return bitc::COMDAT_SELECTION_KIND_LARGEST;
840 case Comdat::NoDuplicates:
841 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
842 case Comdat::SameSize:
843 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
844 }
845 llvm_unreachable("Invalid selection kind");
846}
847
Teresa Johnson37687f32016-04-23 04:30:47 +0000848void ModuleBitcodeWriter::writeComdats() {
David Majnemer90a021f2016-03-13 08:01:03 +0000849 SmallVector<unsigned, 64> Vals;
David Majnemerdad0a642014-06-27 18:19:56 +0000850 for (const Comdat *C : VE.getComdats()) {
851 // COMDAT: [selection_kind, name]
852 Vals.push_back(getEncodedComdatSelectionKind(*C));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +0000853 size_t Size = C->getName().size();
David Majnemer90a021f2016-03-13 08:01:03 +0000854 assert(isUInt<32>(Size));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +0000855 Vals.push_back(Size);
David Majnemerdad0a642014-06-27 18:19:56 +0000856 for (char Chr : C->getName())
857 Vals.push_back((unsigned char)Chr);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000858 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
David Majnemerdad0a642014-06-27 18:19:56 +0000859 Vals.clear();
860 }
861}
862
Teresa Johnsonff642b92015-09-17 20:12:00 +0000863/// Write a record that will eventually hold the word offset of the
864/// module-level VST. For now the offset is 0, which will be backpatched
Teresa Johnson37687f32016-04-23 04:30:47 +0000865/// after the real VST is written. Saves the bit offset to backpatch.
866void BitcodeWriter::writeValueSymbolTableForwardDecl() {
Teresa Johnsonff642b92015-09-17 20:12:00 +0000867 // Write a placeholder value in for the offset of the real VST,
868 // which is written after the function blocks so that it can include
869 // the offset of each function. The placeholder offset will be
870 // updated when the real VST is written.
871 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
872 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
873 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
874 // hold the real VST offset. Must use fixed instead of VBR as we don't
875 // know how many VBR chunks to reserve ahead of time.
876 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000877 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +0000878
879 // Emit the placeholder
880 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000881 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
Teresa Johnsonff642b92015-09-17 20:12:00 +0000882
Teresa Johnson37687f32016-04-23 04:30:47 +0000883 // Compute and save the bit offset to the placeholder, which will be
Teresa Johnsonff642b92015-09-17 20:12:00 +0000884 // patched when the real VST is written. We can simply subtract the 32-bit
885 // fixed size from the current bit number to get the location to backpatch.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000886 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
Teresa Johnsonff642b92015-09-17 20:12:00 +0000887}
888
Teresa Johnsone1164de2016-02-10 21:55:02 +0000889enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
890
891/// Determine the encoding to use for the given string name and length.
892static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
893 bool isChar6 = true;
894 for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
895 if (isChar6)
896 isChar6 = BitCodeAbbrevOp::isChar6(*C);
897 if ((unsigned char)*C & 128)
898 // don't bother scanning the rest.
899 return SE_Fixed8;
900 }
901 if (isChar6)
902 return SE_Char6;
903 else
904 return SE_Fixed7;
905}
906
Teresa Johnsonff642b92015-09-17 20:12:00 +0000907/// Emit top-level description of module, including target triple, inline asm,
908/// descriptors for global variables, and function prototype info.
909/// Returns the bit offset to backpatch with the location of the real VST.
Teresa Johnson37687f32016-04-23 04:30:47 +0000910void ModuleBitcodeWriter::writeModuleInfo() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000911 // Emit various pieces of data attached to a module.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000912 if (!M.getTargetTriple().empty())
913 writeStringRecord(bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
Teresa Johnson37687f32016-04-23 04:30:47 +0000914 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000915 const std::string &DL = M.getDataLayoutStr();
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000916 if (!DL.empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000917 writeStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000918 if (!M.getModuleInlineAsm().empty())
919 writeStringRecord(bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
Teresa Johnson37687f32016-04-23 04:30:47 +0000920 0 /*TODO*/);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000921
Gordon Henriksend930f912008-08-17 18:44:35 +0000922 // Emit information about sections and GC, computing how many there are. Also
923 // compute the maximum alignment value.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000924 std::map<std::string, unsigned> SectionMap;
Gordon Henriksend930f912008-08-17 18:44:35 +0000925 std::map<std::string, unsigned> GCMap;
Chris Lattner4b00d922007-04-23 16:04:05 +0000926 unsigned MaxAlignment = 0;
Chris Lattnerb5491372007-04-23 18:58:34 +0000927 unsigned MaxGlobalType = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000928 for (const GlobalValue &GV : M.globals()) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000929 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
David Blaikie1a848da2015-04-27 19:58:56 +0000930 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000931 if (GV.hasSection()) {
Chad Rosier75ec09c2011-08-12 16:45:18 +0000932 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000933 unsigned &Entry = SectionMap[GV.getSection()];
Chad Rosier75ec09c2011-08-12 16:45:18 +0000934 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000935 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
936 0 /*TODO*/);
Chad Rosier75ec09c2011-08-12 16:45:18 +0000937 Entry = SectionMap.size();
938 }
939 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000940 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000941 for (const Function &F : M) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000942 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
943 if (F.hasSection()) {
Gordon Henriksen71183b62007-12-10 03:18:06 +0000944 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000945 unsigned &Entry = SectionMap[F.getSection()];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000946 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000947 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
948 0 /*TODO*/);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000949 Entry = SectionMap.size();
950 }
951 }
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000952 if (F.hasGC()) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000953 // Same for GC names.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000954 unsigned &Entry = GCMap[F.getGC()];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000955 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000956 writeStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 0 /*TODO*/);
Gordon Henriksend930f912008-08-17 18:44:35 +0000957 Entry = GCMap.size();
Gordon Henriksen71183b62007-12-10 03:18:06 +0000958 }
959 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000960 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000961
Chris Lattner4b00d922007-04-23 16:04:05 +0000962 // Emit abbrev for globals, now that we know # sections and max alignment.
963 unsigned SimpleGVarAbbrev = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000964 if (!M.global_empty()) {
Chris Lattner4b00d922007-04-23 16:04:05 +0000965 // Add an abbrev for common globals with no visibility or thread localness.
966 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
967 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2eae59f2007-05-04 20:34:50 +0000968 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +0000969 Log2_32_Ceil(MaxGlobalType+1)));
David Blaikie1a848da2015-04-27 19:58:56 +0000970 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
971 //| explicitType << 1
972 //| constant
973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
974 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
975 if (MaxAlignment == 0) // Alignment.
Chris Lattner4b00d922007-04-23 16:04:05 +0000976 Abbv->Add(BitCodeAbbrevOp(0));
977 else {
978 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2eae59f2007-05-04 20:34:50 +0000979 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +0000980 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +0000981 }
982 if (SectionMap.empty()) // Section.
983 Abbv->Add(BitCodeAbbrevOp(0));
984 else
Chris Lattner2eae59f2007-05-04 20:34:50 +0000985 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner1e50c292007-04-24 03:29:47 +0000986 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +0000987 // Don't bother emitting vis + thread local.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000988 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner4b00d922007-04-23 16:04:05 +0000989 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000990
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000991 // Emit the global variable information.
992 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000993 for (const GlobalVariable &GV : M.globals()) {
Chris Lattner4b00d922007-04-23 16:04:05 +0000994 unsigned AbbrevToUse = 0;
995
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000996 // GLOBALVAR: [type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +0000997 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +0000998 // unnamed_addr, externally_initialized, dllstorageclass,
999 // comdat]
David Blaikie1a848da2015-04-27 19:58:56 +00001000 Vals.push_back(VE.getTypeID(GV.getValueType()));
1001 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001002 Vals.push_back(GV.isDeclaration() ? 0 :
1003 (VE.getValueID(GV.getInitializer()) + 1));
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001004 Vals.push_back(getEncodedLinkage(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001005 Vals.push_back(Log2_32(GV.getAlignment())+1);
1006 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
1007 if (GV.isThreadLocal() ||
1008 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1009 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
David Majnemerdad0a642014-06-27 18:19:56 +00001010 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1011 GV.hasComdat()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001012 Vals.push_back(getEncodedVisibility(GV));
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001013 Vals.push_back(getEncodedThreadLocalMode(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001014 Vals.push_back(GV.hasUnnamedAddr());
1015 Vals.push_back(GV.isExternallyInitialized());
Nico Rieck7157bb72014-01-14 15:22:47 +00001016 Vals.push_back(getEncodedDLLStorageClass(GV));
David Majnemerdad0a642014-06-27 18:19:56 +00001017 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
Chris Lattner4b00d922007-04-23 16:04:05 +00001018 } else {
1019 AbbrevToUse = SimpleGVarAbbrev;
1020 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001021
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001022 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001023 Vals.clear();
1024 }
1025
1026 // Emit the function proto information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001027 for (const Function &F : M) {
Chad Rosier42ee1522011-12-07 23:57:55 +00001028 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001029 // section, visibility, gc, unnamed_addr, prologuedata,
David Majnemer7fddecc2015-06-17 20:52:32 +00001030 // dllstorageclass, comdat, prefixdata, personalityfn]
David Blaikie561a1572015-04-17 16:28:26 +00001031 Vals.push_back(VE.getTypeID(F.getFunctionType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001032 Vals.push_back(F.getCallingConv());
1033 Vals.push_back(F.isDeclaration());
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001034 Vals.push_back(getEncodedLinkage(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001035 Vals.push_back(VE.getAttributeID(F.getAttributes()));
1036 Vals.push_back(Log2_32(F.getAlignment())+1);
1037 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001038 Vals.push_back(getEncodedVisibility(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001039 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1040 Vals.push_back(F.hasUnnamedAddr());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001041 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1042 : 0);
Nico Rieck7157bb72014-01-14 15:22:47 +00001043 Vals.push_back(getEncodedDLLStorageClass(F));
David Majnemerdad0a642014-06-27 18:19:56 +00001044 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001045 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1046 : 0);
David Majnemer7fddecc2015-06-17 20:52:32 +00001047 Vals.push_back(
1048 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001049
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001050 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001051 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001052 Vals.clear();
1053 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001054
Chris Lattner44c17072007-04-26 02:46:40 +00001055 // Emit the alias information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001056 for (const GlobalAlias &A : M.aliases()) {
Chad Rosiera966c312011-12-08 00:11:31 +00001057 // ALIAS: [alias type, aliasee val#, linkage, visibility]
David Blaikie6a51dbd2015-09-17 22:18:59 +00001058 Vals.push_back(VE.getTypeID(A.getValueType()));
1059 Vals.push_back(A.getType()->getAddressSpace());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001060 Vals.push_back(VE.getValueID(A.getAliasee()));
1061 Vals.push_back(getEncodedLinkage(A));
1062 Vals.push_back(getEncodedVisibility(A));
1063 Vals.push_back(getEncodedDLLStorageClass(A));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001064 Vals.push_back(getEncodedThreadLocalMode(A));
1065 Vals.push_back(A.hasUnnamedAddr());
Chris Lattner44c17072007-04-26 02:46:40 +00001066 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001067 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
Chris Lattner44c17072007-04-26 02:46:40 +00001068 Vals.clear();
1069 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001070
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001071 // Emit the ifunc information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001072 for (const GlobalIFunc &I : M.ifuncs()) {
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001073 // IFUNC: [ifunc type, address space, resolver val#, linkage, visibility]
1074 Vals.push_back(VE.getTypeID(I.getValueType()));
1075 Vals.push_back(I.getType()->getAddressSpace());
1076 Vals.push_back(VE.getValueID(I.getResolver()));
1077 Vals.push_back(getEncodedLinkage(I));
1078 Vals.push_back(getEncodedVisibility(I));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001079 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001080 Vals.clear();
1081 }
1082
Teresa Johnsone1164de2016-02-10 21:55:02 +00001083 // Emit the module's source file name.
1084 {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001085 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1086 M.getSourceFileName().size());
Teresa Johnsone1164de2016-02-10 21:55:02 +00001087 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1088 if (Bits == SE_Char6)
1089 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1090 else if (Bits == SE_Fixed7)
1091 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1092
1093 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1094 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1095 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1096 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1097 Abbv->Add(AbbrevOpToUse);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001098 unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001099
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001100 for (const auto P : M.getSourceFileName())
Teresa Johnsone1164de2016-02-10 21:55:02 +00001101 Vals.push_back((unsigned char)P);
1102
1103 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001104 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001105 Vals.clear();
1106 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001107
Teresa Johnson37687f32016-04-23 04:30:47 +00001108 // If we have a VST, write the VSTOFFSET record placeholder.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001109 if (M.getValueSymbolTable().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001110 return;
1111 writeValueSymbolTableForwardDecl();
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001112}
1113
Teresa Johnson37687f32016-04-23 04:30:47 +00001114static uint64_t getOptimizationFlags(const Value *V) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001115 uint64_t Flags = 0;
1116
Sanjay Patelc00017d2014-10-15 17:45:13 +00001117 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001118 if (OBO->hasNoSignedWrap())
1119 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1120 if (OBO->hasNoUnsignedWrap())
1121 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001122 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
Chris Lattner35315d02011-02-06 21:44:57 +00001123 if (PEO->isExact())
1124 Flags |= 1 << bitc::PEO_EXACT;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001125 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001126 if (FPMO->hasUnsafeAlgebra())
Michael Ilseman65f14352012-12-09 21:12:04 +00001127 Flags |= FastMathFlags::UnsafeAlgebra;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001128 if (FPMO->hasNoNaNs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001129 Flags |= FastMathFlags::NoNaNs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001130 if (FPMO->hasNoInfs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001131 Flags |= FastMathFlags::NoInfs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001132 if (FPMO->hasNoSignedZeros())
Michael Ilseman65f14352012-12-09 21:12:04 +00001133 Flags |= FastMathFlags::NoSignedZeros;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001134 if (FPMO->hasAllowReciprocal())
Michael Ilseman65f14352012-12-09 21:12:04 +00001135 Flags |= FastMathFlags::AllowReciprocal;
Dan Gohman0ebd6962009-07-20 21:19:07 +00001136 }
1137
1138 return Flags;
1139}
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001140
Teresa Johnson37687f32016-04-23 04:30:47 +00001141void ModuleBitcodeWriter::writeValueAsMetadata(
1142 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001143 // Mimic an MDNode with a value as one operand.
1144 Value *V = MD->getValue();
1145 Record.push_back(VE.getTypeID(V->getType()));
1146 Record.push_back(VE.getValueID(V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001147 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001148 Record.clear();
1149}
1150
Teresa Johnson37687f32016-04-23 04:30:47 +00001151void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1152 SmallVectorImpl<uint64_t> &Record,
1153 unsigned Abbrev) {
Chris Lattner9b493022009-12-31 01:22:29 +00001154 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001155 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001156 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1157 "Unexpected function-local metadata");
1158 Record.push_back(VE.getMetadataOrNullID(MD));
Devang Patel8cca7b42009-08-04 05:01:35 +00001159 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001160 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1161 : bitc::METADATA_NODE,
1162 Record, Abbrev);
Devang Patel8cca7b42009-08-04 05:01:35 +00001163 Record.clear();
1164}
Devang Patele059ba6e2009-07-23 01:07:34 +00001165
Teresa Johnson37687f32016-04-23 04:30:47 +00001166unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001167 // Assume the column is usually under 128, and always output the inlined-at
1168 // location (it's never more expensive than building an array size 1).
1169 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1170 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1171 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1172 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1173 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1174 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1175 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001176 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001177}
1178
Teresa Johnson37687f32016-04-23 04:30:47 +00001179void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1180 SmallVectorImpl<uint64_t> &Record,
1181 unsigned &Abbrev) {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001182 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001183 Abbrev = createDILocationAbbrev();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001184
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001185 Record.push_back(N->isDistinct());
1186 Record.push_back(N->getLine());
1187 Record.push_back(N->getColumn());
1188 Record.push_back(VE.getMetadataID(N->getScope()));
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001189 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001190
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001191 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001192 Record.clear();
1193}
1194
Teresa Johnson37687f32016-04-23 04:30:47 +00001195unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001196 // Assume the column is usually under 128, and always output the inlined-at
1197 // location (it's never more expensive than building an array size 1).
1198 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1199 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1200 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1201 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1202 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1203 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1204 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1205 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001206 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001207}
1208
Teresa Johnson37687f32016-04-23 04:30:47 +00001209void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1210 SmallVectorImpl<uint64_t> &Record,
1211 unsigned &Abbrev) {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001212 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001213 Abbrev = createGenericDINodeAbbrev();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001214
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001215 Record.push_back(N->isDistinct());
1216 Record.push_back(N->getTag());
1217 Record.push_back(0); // Per-tag version field; unused for now.
1218
1219 for (auto &I : N->operands())
1220 Record.push_back(VE.getMetadataOrNullID(I));
1221
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001222 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001223 Record.clear();
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001224}
1225
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001226static uint64_t rotateSign(int64_t I) {
1227 uint64_t U = I;
1228 return I < 0 ? ~(U << 1) : U << 1;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001229}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001230
Teresa Johnson37687f32016-04-23 04:30:47 +00001231void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1232 SmallVectorImpl<uint64_t> &Record,
1233 unsigned Abbrev) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001234 Record.push_back(N->isDistinct());
1235 Record.push_back(N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001236 Record.push_back(rotateSign(N->getLowerBound()));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001237
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001238 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001239 Record.clear();
1240}
1241
Teresa Johnson37687f32016-04-23 04:30:47 +00001242void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1243 SmallVectorImpl<uint64_t> &Record,
1244 unsigned Abbrev) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001245 Record.push_back(N->isDistinct());
1246 Record.push_back(rotateSign(N->getValue()));
1247 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1248
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001249 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001250 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001251}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001252
Teresa Johnson37687f32016-04-23 04:30:47 +00001253void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1254 SmallVectorImpl<uint64_t> &Record,
1255 unsigned Abbrev) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001256 Record.push_back(N->isDistinct());
1257 Record.push_back(N->getTag());
1258 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1259 Record.push_back(N->getSizeInBits());
1260 Record.push_back(N->getAlignInBits());
1261 Record.push_back(N->getEncoding());
1262
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001263 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001264 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001265}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001266
Teresa Johnson37687f32016-04-23 04:30:47 +00001267void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1268 SmallVectorImpl<uint64_t> &Record,
1269 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001270 Record.push_back(N->isDistinct());
1271 Record.push_back(N->getTag());
1272 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1273 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1274 Record.push_back(N->getLine());
1275 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001276 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001277 Record.push_back(N->getSizeInBits());
1278 Record.push_back(N->getAlignInBits());
1279 Record.push_back(N->getOffsetInBits());
1280 Record.push_back(N->getFlags());
1281 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1282
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001283 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001284 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001285}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001286
Teresa Johnson37687f32016-04-23 04:30:47 +00001287void ModuleBitcodeWriter::writeDICompositeType(
1288 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1289 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001290 const unsigned IsNotUsedInOldTypeRef = 0x2;
1291 Record.push_back(IsNotUsedInOldTypeRef | N->isDistinct());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001292 Record.push_back(N->getTag());
1293 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1294 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1295 Record.push_back(N->getLine());
1296 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1297 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1298 Record.push_back(N->getSizeInBits());
1299 Record.push_back(N->getAlignInBits());
1300 Record.push_back(N->getOffsetInBits());
1301 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001302 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001303 Record.push_back(N->getRuntimeLang());
1304 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001305 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001306 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1307
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001308 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001309 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001310}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001311
Teresa Johnson37687f32016-04-23 04:30:47 +00001312void ModuleBitcodeWriter::writeDISubroutineType(
1313 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1314 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001315 const unsigned HasNoOldTypeRefs = 0x2;
1316 Record.push_back(HasNoOldTypeRefs | N->isDistinct());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001317 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001318 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001319
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001320 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001321 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001322}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001323
Teresa Johnson37687f32016-04-23 04:30:47 +00001324void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1325 SmallVectorImpl<uint64_t> &Record,
1326 unsigned Abbrev) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001327 Record.push_back(N->isDistinct());
1328 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1329 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1330
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001331 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001332 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001333}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001334
Teresa Johnson37687f32016-04-23 04:30:47 +00001335void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1336 SmallVectorImpl<uint64_t> &Record,
1337 unsigned Abbrev) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001338 assert(N->isDistinct() && "Expected distinct compile units");
1339 Record.push_back(/* IsDistinct */ true);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001340 Record.push_back(N->getSourceLanguage());
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001341 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001342 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1343 Record.push_back(N->isOptimized());
1344 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1345 Record.push_back(N->getRuntimeVersion());
1346 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1347 Record.push_back(N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001348 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1349 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001350 Record.push_back(/* subprograms */ 0);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001351 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1352 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
Adrian Prantl1f599f92015-05-21 20:37:30 +00001353 Record.push_back(N->getDWOId());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001354 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001355
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001356 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001357 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001358}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001359
Teresa Johnson37687f32016-04-23 04:30:47 +00001360void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1361 SmallVectorImpl<uint64_t> &Record,
1362 unsigned Abbrev) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001363 Record.push_back(N->isDistinct());
1364 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1365 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1366 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1367 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1368 Record.push_back(N->getLine());
1369 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1370 Record.push_back(N->isLocalToUnit());
1371 Record.push_back(N->isDefinition());
1372 Record.push_back(N->getScopeLine());
1373 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1374 Record.push_back(N->getVirtuality());
1375 Record.push_back(N->getVirtualIndex());
1376 Record.push_back(N->getFlags());
1377 Record.push_back(N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001378 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001379 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001380 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001381 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001382
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001383 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001384 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001385}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001386
Teresa Johnson37687f32016-04-23 04:30:47 +00001387void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1388 SmallVectorImpl<uint64_t> &Record,
1389 unsigned Abbrev) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001390 Record.push_back(N->isDistinct());
1391 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1392 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1393 Record.push_back(N->getLine());
1394 Record.push_back(N->getColumn());
1395
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001396 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001397 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001398}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001399
Teresa Johnson37687f32016-04-23 04:30:47 +00001400void ModuleBitcodeWriter::writeDILexicalBlockFile(
1401 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1402 unsigned Abbrev) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001403 Record.push_back(N->isDistinct());
1404 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1405 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1406 Record.push_back(N->getDiscriminator());
1407
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001408 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001409 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001410}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001411
Teresa Johnson37687f32016-04-23 04:30:47 +00001412void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1413 SmallVectorImpl<uint64_t> &Record,
1414 unsigned Abbrev) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001415 Record.push_back(N->isDistinct());
1416 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1417 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1418 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1419 Record.push_back(N->getLine());
1420
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001421 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001422 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001423}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001424
Teresa Johnson37687f32016-04-23 04:30:47 +00001425void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1426 SmallVectorImpl<uint64_t> &Record,
1427 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001428 Record.push_back(N->isDistinct());
1429 Record.push_back(N->getMacinfoType());
1430 Record.push_back(N->getLine());
1431 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1432 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1433
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001434 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001435 Record.clear();
1436}
1437
Teresa Johnson37687f32016-04-23 04:30:47 +00001438void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1439 SmallVectorImpl<uint64_t> &Record,
1440 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001441 Record.push_back(N->isDistinct());
1442 Record.push_back(N->getMacinfoType());
1443 Record.push_back(N->getLine());
1444 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1445 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1446
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001447 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001448 Record.clear();
1449}
1450
Teresa Johnson37687f32016-04-23 04:30:47 +00001451void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1452 SmallVectorImpl<uint64_t> &Record,
1453 unsigned Abbrev) {
Adrian Prantlab1243f2015-06-29 23:03:47 +00001454 Record.push_back(N->isDistinct());
1455 for (auto &I : N->operands())
1456 Record.push_back(VE.getMetadataOrNullID(I));
1457
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001458 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
Adrian Prantlab1243f2015-06-29 23:03:47 +00001459 Record.clear();
1460}
1461
Teresa Johnson37687f32016-04-23 04:30:47 +00001462void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1463 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1464 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001465 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001466 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1467 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1468
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001469 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001470 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001471}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001472
Teresa Johnson37687f32016-04-23 04:30:47 +00001473void ModuleBitcodeWriter::writeDITemplateValueParameter(
1474 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1475 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001476 Record.push_back(N->isDistinct());
1477 Record.push_back(N->getTag());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001478 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1479 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1480 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1481
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001482 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001483 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001484}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001485
Teresa Johnson37687f32016-04-23 04:30:47 +00001486void ModuleBitcodeWriter::writeDIGlobalVariable(
1487 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1488 unsigned Abbrev) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001489 Record.push_back(N->isDistinct());
1490 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1491 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1492 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1493 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1494 Record.push_back(N->getLine());
1495 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1496 Record.push_back(N->isLocalToUnit());
1497 Record.push_back(N->isDefinition());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001498 Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001499 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1500
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001501 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001502 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001503}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001504
Teresa Johnson37687f32016-04-23 04:30:47 +00001505void ModuleBitcodeWriter::writeDILocalVariable(
1506 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1507 unsigned Abbrev) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001508 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001509 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1510 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1511 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1512 Record.push_back(N->getLine());
1513 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1514 Record.push_back(N->getArg());
1515 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001516
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001517 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001518 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001519}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001520
Teresa Johnson37687f32016-04-23 04:30:47 +00001521void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1522 SmallVectorImpl<uint64_t> &Record,
1523 unsigned Abbrev) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001524 Record.reserve(N->getElements().size() + 1);
1525
1526 Record.push_back(N->isDistinct());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001527 Record.append(N->elements_begin(), N->elements_end());
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001528
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001529 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001530 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001531}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001532
Teresa Johnson37687f32016-04-23 04:30:47 +00001533void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1534 SmallVectorImpl<uint64_t> &Record,
1535 unsigned Abbrev) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001536 Record.push_back(N->isDistinct());
1537 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1538 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1539 Record.push_back(N->getLine());
1540 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1541 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1542 Record.push_back(N->getAttributes());
1543 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1544
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001545 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001546 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001547}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001548
Teresa Johnson37687f32016-04-23 04:30:47 +00001549void ModuleBitcodeWriter::writeDIImportedEntity(
1550 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1551 unsigned Abbrev) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001552 Record.push_back(N->isDistinct());
1553 Record.push_back(N->getTag());
1554 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1555 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1556 Record.push_back(N->getLine());
1557 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1558
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001559 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001560 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001561}
1562
Teresa Johnson37687f32016-04-23 04:30:47 +00001563unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001564 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1565 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1566 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1567 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001568 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001569}
1570
Teresa Johnson37687f32016-04-23 04:30:47 +00001571void ModuleBitcodeWriter::writeNamedMetadata(
1572 SmallVectorImpl<uint64_t> &Record) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001573 if (M.named_metadata_empty())
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001574 return;
1575
Teresa Johnson37687f32016-04-23 04:30:47 +00001576 unsigned Abbrev = createNamedMetadataAbbrev();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001577 for (const NamedMDNode &NMD : M.named_metadata()) {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001578 // Write name.
1579 StringRef Str = NMD.getName();
1580 Record.append(Str.bytes_begin(), Str.bytes_end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001581 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001582 Record.clear();
1583
1584 // Write named metadata operands.
1585 for (const MDNode *N : NMD.operands())
1586 Record.push_back(VE.getMetadataID(N));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001587 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001588 Record.clear();
1589 }
1590}
1591
Teresa Johnson37687f32016-04-23 04:30:47 +00001592unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001593 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1594 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1595 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1596 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1597 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001598 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001599}
1600
1601/// Write out a record for MDString.
1602///
1603/// All the metadata strings in a metadata block are emitted in a single
1604/// record. The sizes and strings themselves are shoved into a blob.
Teresa Johnson37687f32016-04-23 04:30:47 +00001605void ModuleBitcodeWriter::writeMetadataStrings(
1606 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001607 if (Strings.empty())
1608 return;
1609
1610 // Start the record with the number of strings.
1611 Record.push_back(bitc::METADATA_STRINGS);
1612 Record.push_back(Strings.size());
1613
1614 // Emit the sizes of the strings in the blob.
1615 SmallString<256> Blob;
1616 {
1617 BitstreamWriter W(Blob);
1618 for (const Metadata *MD : Strings)
1619 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1620 W.FlushToWord();
1621 }
1622
1623 // Add the offset to the strings to the record.
1624 Record.push_back(Blob.size());
1625
1626 // Add the strings to the blob.
1627 for (const Metadata *MD : Strings)
1628 Blob.append(cast<MDString>(MD)->getString());
1629
1630 // Emit the final record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001631 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001632 Record.clear();
1633}
1634
Teresa Johnson37687f32016-04-23 04:30:47 +00001635void ModuleBitcodeWriter::writeMetadataRecords(
1636 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001637 if (MDs.empty())
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +00001638 return;
1639
Duncan P. N. Exon Smith6b7b6802015-02-04 21:54:12 +00001640 // Initialize MDNode abbreviations.
1641#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1642#include "llvm/IR/Metadata.def"
1643
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001644 for (const Metadata *MD : MDs) {
Duncan P. N. Exon Smith73d5aae2015-01-12 22:31:35 +00001645 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith79f8d112015-03-17 00:16:35 +00001646 assert(N->isResolved() && "Expected forward references to be resolved");
1647
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001648 switch (N->getMetadataID()) {
1649 default:
1650 llvm_unreachable("Invalid MDNode subclass");
1651#define HANDLE_MDNODE_LEAF(CLASS) \
1652 case Metadata::CLASS##Kind: \
Teresa Johnson37687f32016-04-23 04:30:47 +00001653 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001654 continue;
1655#include "llvm/IR/Metadata.def"
1656 }
Devang Patel8cca7b42009-08-04 05:01:35 +00001657 }
Teresa Johnson37687f32016-04-23 04:30:47 +00001658 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
Devang Patel8cca7b42009-08-04 05:01:35 +00001659 }
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001660}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001661
Teresa Johnson37687f32016-04-23 04:30:47 +00001662void ModuleBitcodeWriter::writeModuleMetadata() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001663 if (!VE.hasMDs() && M.named_metadata_empty())
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001664 return;
1665
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001666 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001667 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001668 writeMetadataStrings(VE.getMDStrings(), Record);
1669 writeMetadataRecords(VE.getNonMDStrings(), Record);
1670 writeNamedMetadata(Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001671 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001672}
1673
Teresa Johnson37687f32016-04-23 04:30:47 +00001674void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +00001675 if (!VE.hasMDs())
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001676 return;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001677
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001678 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001679 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001680 writeMetadataStrings(VE.getMDStrings(), Record);
1681 writeMetadataRecords(VE.getNonMDStrings(), Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001682 Stream.ExitBlock();
Victor Hernandezb00a6be2010-01-13 19:37:33 +00001683}
1684
Teresa Johnson37687f32016-04-23 04:30:47 +00001685void ModuleBitcodeWriter::writeMetadataAttachment(const Function &F) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001686 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
Chris Lattner07d09ed2010-04-03 02:17:50 +00001687
Devang Patelaf206b82009-09-18 19:26:43 +00001688 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001689
Devang Patelaf206b82009-09-18 19:26:43 +00001690 // Write metadata attachments
Chris Lattnerbb95d5e2011-06-17 17:56:00 +00001691 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001692 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001693 F.getAllMetadata(MDs);
1694 if (!MDs.empty()) {
1695 for (const auto &I : MDs) {
1696 Record.push_back(I.first);
1697 Record.push_back(VE.getMetadataID(I.second));
1698 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001699 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001700 Record.clear();
1701 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001702
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001703 for (const BasicBlock &BB : F)
1704 for (const Instruction &I : BB) {
Devang Patel6da5dbf2009-10-22 18:55:16 +00001705 MDs.clear();
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001706 I.getAllMetadataOtherThanDebugLoc(MDs);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001707
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001708 // If no metadata, ignore instruction.
1709 if (MDs.empty()) continue;
1710
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001711 Record.push_back(VE.getInstructionID(&I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001712
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001713 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1714 Record.push_back(MDs[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001715 Record.push_back(VE.getMetadataID(MDs[i].second));
Devang Patelaf206b82009-09-18 19:26:43 +00001716 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001717 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001718 Record.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00001719 }
1720
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001721 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001722}
1723
Teresa Johnson37687f32016-04-23 04:30:47 +00001724void ModuleBitcodeWriter::writeModuleMetadataStore() {
Devang Patelaf206b82009-09-18 19:26:43 +00001725 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001726
Devang Patelaf206b82009-09-18 19:26:43 +00001727 // Write metadata kinds
1728 // METADATA_KIND - [n x [id, name]]
Michael Ilseman979dfbb2012-12-03 22:57:47 +00001729 SmallVector<StringRef, 8> Names;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001730 M.getMDKindNames(Names);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001731
Dan Gohman43aa8f02010-07-20 21:42:28 +00001732 if (Names.empty()) return;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001733
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001734 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001735
Dan Gohman43aa8f02010-07-20 21:42:28 +00001736 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattnerc9558df2009-12-28 20:10:43 +00001737 Record.push_back(MDKindID);
1738 StringRef KName = Names[MDKindID];
1739 Record.append(KName.begin(), KName.end());
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001740
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001741 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
Devang Patelaf206b82009-09-18 19:26:43 +00001742 Record.clear();
1743 }
1744
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001745 Stream.ExitBlock();
Devang Patel8cca7b42009-08-04 05:01:35 +00001746}
1747
Teresa Johnson37687f32016-04-23 04:30:47 +00001748void ModuleBitcodeWriter::writeOperandBundleTags() {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001749 // Write metadata kinds
1750 //
1751 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
1752 //
1753 // OPERAND_BUNDLE_TAG - [strchr x N]
1754
1755 SmallVector<StringRef, 8> Tags;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001756 M.getOperandBundleTags(Tags);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001757
1758 if (Tags.empty())
1759 return;
1760
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001761 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001762
1763 SmallVector<uint64_t, 64> Record;
1764
1765 for (auto Tag : Tags) {
1766 Record.append(Tag.begin(), Tag.end());
1767
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001768 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001769 Record.clear();
1770 }
1771
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001772 Stream.ExitBlock();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001773}
1774
Jan Wen Voungafaced02012-10-11 20:20:40 +00001775static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1776 if ((int64_t)V >= 0)
1777 Vals.push_back(V << 1);
1778 else
1779 Vals.push_back((-V << 1) | 1);
1780}
1781
Teresa Johnson37687f32016-04-23 04:30:47 +00001782void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1783 bool isGlobal) {
Devang Patel8cca7b42009-08-04 05:01:35 +00001784 if (FirstVal == LastVal) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001785
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001786 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Devang Patel8cca7b42009-08-04 05:01:35 +00001787
1788 unsigned AggregateAbbrev = 0;
1789 unsigned String8Abbrev = 0;
1790 unsigned CString7Abbrev = 0;
1791 unsigned CString6Abbrev = 0;
1792 // If this is a constant pool for the module, emit module-specific abbrevs.
1793 if (isGlobal) {
1794 // Abbrev for CST_CODE_AGGREGATE.
1795 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1796 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001799 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001800
1801 // Abbrev for CST_CODE_STRING.
1802 Abbv = new BitCodeAbbrev();
1803 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1804 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1805 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001806 String8Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001807 // Abbrev for CST_CODE_CSTRING.
1808 Abbv = new BitCodeAbbrev();
1809 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1810 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1811 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001812 CString7Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001813 // Abbrev for CST_CODE_CSTRING.
1814 Abbv = new BitCodeAbbrev();
1815 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001818 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001819 }
1820
Devang Patel8cca7b42009-08-04 05:01:35 +00001821 SmallVector<uint64_t, 64> Record;
1822
1823 const ValueEnumerator::ValueList &Vals = VE.getValues();
Craig Topper2617dcc2014-04-15 06:32:26 +00001824 Type *LastTy = nullptr;
Devang Patel8cca7b42009-08-04 05:01:35 +00001825 for (unsigned i = FirstVal; i != LastVal; ++i) {
1826 const Value *V = Vals[i].first;
Chris Lattner52523562007-04-24 00:16:04 +00001827 // If we need to switch types, do so now.
1828 if (V->getType() != LastTy) {
1829 LastTy = V->getType();
1830 Record.push_back(VE.getTypeID(LastTy));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001831 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1832 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner52523562007-04-24 00:16:04 +00001833 Record.clear();
1834 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001835
Chris Lattner52523562007-04-24 00:16:04 +00001836 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesenfd04c742009-10-13 20:46:56 +00001837 Record.push_back(unsigned(IA->hasSideEffects()) |
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001838 unsigned(IA->isAlignStack()) << 1 |
1839 unsigned(IA->getDialect()&1) << 2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001840
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001841 // Add the asm string.
1842 const std::string &AsmStr = IA->getAsmString();
1843 Record.push_back(AsmStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001844 Record.append(AsmStr.begin(), AsmStr.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001845
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001846 // Add the constraint string.
1847 const std::string &ConstraintStr = IA->getConstraintString();
1848 Record.push_back(ConstraintStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001849 Record.append(ConstraintStr.begin(), ConstraintStr.end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001850 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001851 Record.clear();
Chris Lattner52523562007-04-24 00:16:04 +00001852 continue;
1853 }
1854 const Constant *C = cast<Constant>(V);
1855 unsigned Code = -1U;
1856 unsigned AbbrevToUse = 0;
1857 if (C->isNullValue()) {
1858 Code = bitc::CST_CODE_NULL;
1859 } else if (isa<UndefValue>(C)) {
1860 Code = bitc::CST_CODE_UNDEF;
1861 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
Bob Wilsone4077362013-09-09 19:14:35 +00001862 if (IV->getBitWidth() <= 64) {
1863 uint64_t V = IV->getSExtValue();
1864 emitSignedInt64(Record, V);
1865 Code = bitc::CST_CODE_INTEGER;
1866 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1867 } else { // Wide integers, > 64 bits in size.
1868 // We have an arbitrary precision integer value to write whose
1869 // bit width is > 64. However, in canonical unsigned integer
1870 // format it is likely that the high bits are going to be zero.
1871 // So, we only write the number of active words.
1872 unsigned NWords = IV->getValue().getActiveWords();
1873 const uint64_t *RawWords = IV->getValue().getRawData();
1874 for (unsigned i = 0; i != NWords; ++i) {
1875 emitSignedInt64(Record, RawWords[i]);
1876 }
1877 Code = bitc::CST_CODE_WIDE_INTEGER;
1878 }
Chris Lattner52523562007-04-24 00:16:04 +00001879 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1880 Code = bitc::CST_CODE_FLOAT;
Chris Lattner229907c2011-07-18 04:54:35 +00001881 Type *Ty = CFP->getType();
Dan Gohman518cda42011-12-17 00:04:22 +00001882 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00001883 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +00001884 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001885 // api needed to prevent premature destruction
Dale Johannesen93eefa02009-03-23 21:16:53 +00001886 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen54306fe2008-10-09 18:53:47 +00001887 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001888 const uint64_t *p = api.getRawData();
Dale Johannesen93eefa02009-03-23 21:16:53 +00001889 Record.push_back((p[1] << 48) | (p[0] >> 16));
1890 Record.push_back(p[0] & 0xffffLL);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001891 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00001892 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001893 const uint64_t *p = api.getRawData();
Dale Johannesen245dceb2007-09-11 18:32:33 +00001894 Record.push_back(p[0]);
1895 Record.push_back(p[1]);
Dale Johannesenbdad8092007-08-09 22:51:36 +00001896 } else {
1897 assert (0 && "Unknown FP type!");
Chris Lattner52523562007-04-24 00:16:04 +00001898 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00001899 } else if (isa<ConstantDataSequential>(C) &&
1900 cast<ConstantDataSequential>(C)->isString()) {
1901 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
1902 // Emit constant strings specially.
1903 unsigned NumElts = Str->getNumElements();
1904 // If this is a null-terminated string, use the denser CSTRING encoding.
1905 if (Str->isCString()) {
1906 Code = bitc::CST_CODE_CSTRING;
1907 --NumElts; // Don't encode the null, which isn't allowed by char6.
1908 } else {
1909 Code = bitc::CST_CODE_STRING;
1910 AbbrevToUse = String8Abbrev;
1911 }
1912 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1913 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1914 for (unsigned i = 0; i != NumElts; ++i) {
1915 unsigned char V = Str->getElementAsInteger(i);
1916 Record.push_back(V);
1917 isCStr7 &= (V & 128) == 0;
1918 if (isCStrChar6)
1919 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1920 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001921
Chris Lattner372dd1e2012-01-30 00:51:16 +00001922 if (isCStrChar6)
1923 AbbrevToUse = CString6Abbrev;
1924 else if (isCStr7)
1925 AbbrevToUse = CString7Abbrev;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001926 } else if (const ConstantDataSequential *CDS =
Chris Lattner372dd1e2012-01-30 00:51:16 +00001927 dyn_cast<ConstantDataSequential>(C)) {
Chris Lattner5d917072012-01-30 07:36:01 +00001928 Code = bitc::CST_CODE_DATA;
Chris Lattner372dd1e2012-01-30 00:51:16 +00001929 Type *EltTy = CDS->getType()->getElementType();
1930 if (isa<IntegerType>(EltTy)) {
1931 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1932 Record.push_back(CDS->getElementAsInteger(i));
Chris Lattner372dd1e2012-01-30 00:51:16 +00001933 } else {
Justin Bognera43eacb2016-01-06 22:31:32 +00001934 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1935 Record.push_back(
1936 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
Chris Lattner372dd1e2012-01-30 00:51:16 +00001937 }
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00001938 } else if (isa<ConstantAggregate>(C)) {
Chris Lattner52523562007-04-24 00:16:04 +00001939 Code = bitc::CST_CODE_AGGREGATE;
Pete Cooper125ad172015-06-25 20:51:38 +00001940 for (const Value *Op : C->operands())
1941 Record.push_back(VE.getValueID(Op));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00001942 AbbrevToUse = AggregateAbbrev;
Chris Lattner52523562007-04-24 00:16:04 +00001943 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001944 switch (CE->getOpcode()) {
1945 default:
1946 if (Instruction::isCast(CE->getOpcode())) {
1947 Code = bitc::CST_CODE_CE_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00001948 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001949 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1950 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00001951 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001952 } else {
1953 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1954 Code = bitc::CST_CODE_CE_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00001955 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001956 Record.push_back(VE.getValueID(C->getOperand(0)));
1957 Record.push_back(VE.getValueID(C->getOperand(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00001958 uint64_t Flags = getOptimizationFlags(CE);
Dan Gohman0ebd6962009-07-20 21:19:07 +00001959 if (Flags != 0)
1960 Record.push_back(Flags);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001961 }
1962 break;
David Blaikieb9263572015-03-13 21:03:36 +00001963 case Instruction::GetElementPtr: {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001964 Code = bitc::CST_CODE_CE_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00001965 const auto *GO = cast<GEPOperator>(C);
1966 if (GO->isInBounds())
Dan Gohman1639c392009-07-27 21:53:46 +00001967 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00001968 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001969 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1970 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1971 Record.push_back(VE.getValueID(C->getOperand(i)));
1972 }
1973 break;
David Blaikieb9263572015-03-13 21:03:36 +00001974 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001975 case Instruction::Select:
1976 Code = bitc::CST_CODE_CE_SELECT;
1977 Record.push_back(VE.getValueID(C->getOperand(0)));
1978 Record.push_back(VE.getValueID(C->getOperand(1)));
1979 Record.push_back(VE.getValueID(C->getOperand(2)));
1980 break;
1981 case Instruction::ExtractElement:
1982 Code = bitc::CST_CODE_CE_EXTRACTELT;
1983 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1984 Record.push_back(VE.getValueID(C->getOperand(0)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001985 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001986 Record.push_back(VE.getValueID(C->getOperand(1)));
1987 break;
1988 case Instruction::InsertElement:
1989 Code = bitc::CST_CODE_CE_INSERTELT;
1990 Record.push_back(VE.getValueID(C->getOperand(0)));
1991 Record.push_back(VE.getValueID(C->getOperand(1)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001992 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001993 Record.push_back(VE.getValueID(C->getOperand(2)));
1994 break;
1995 case Instruction::ShuffleVector:
Nate Begeman94aa38d2009-02-12 21:28:33 +00001996 // If the return type and argument types are the same, this is a
1997 // standard shufflevector instruction. If the types are different,
1998 // then the shuffle is widening or truncating the input vectors, and
1999 // the argument type must also be encoded.
2000 if (C->getType() == C->getOperand(0)->getType()) {
2001 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2002 } else {
2003 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2004 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2005 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002006 Record.push_back(VE.getValueID(C->getOperand(0)));
2007 Record.push_back(VE.getValueID(C->getOperand(1)));
2008 Record.push_back(VE.getValueID(C->getOperand(2)));
2009 break;
2010 case Instruction::ICmp:
2011 case Instruction::FCmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002012 Code = bitc::CST_CODE_CE_CMP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002013 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2014 Record.push_back(VE.getValueID(C->getOperand(0)));
2015 Record.push_back(VE.getValueID(C->getOperand(1)));
2016 Record.push_back(CE->getPredicate());
2017 break;
2018 }
Chris Lattnerf540d742009-10-28 05:24:40 +00002019 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerf540d742009-10-28 05:24:40 +00002020 Code = bitc::CST_CODE_BLOCKADDRESS;
2021 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2022 Record.push_back(VE.getValueID(BA->getFunction()));
2023 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner52523562007-04-24 00:16:04 +00002024 } else {
Dan Gohman47dc8fd2010-07-21 21:18:37 +00002025#ifndef NDEBUG
2026 C->dump();
2027#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002028 llvm_unreachable("Unknown constant!");
Chris Lattner52523562007-04-24 00:16:04 +00002029 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002030 Stream.EmitRecord(Code, Record, AbbrevToUse);
Chris Lattner52523562007-04-24 00:16:04 +00002031 Record.clear();
2032 }
2033
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002034 Stream.ExitBlock();
Chris Lattner52523562007-04-24 00:16:04 +00002035}
2036
Teresa Johnson37687f32016-04-23 04:30:47 +00002037void ModuleBitcodeWriter::writeModuleConstants() {
Chris Lattner52523562007-04-24 00:16:04 +00002038 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002039
Chris Lattner52523562007-04-24 00:16:04 +00002040 // Find the first constant to emit, which is the first non-globalvalue value.
2041 // We know globalvalues have been emitted by WriteModuleInfo.
2042 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2043 if (!isa<GlobalValue>(Vals[i].first)) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002044 writeConstants(i, Vals.size(), true);
Chris Lattner52523562007-04-24 00:16:04 +00002045 return;
2046 }
2047 }
2048}
Chris Lattner215e9cd2007-04-23 20:35:01 +00002049
Teresa Johnson37687f32016-04-23 04:30:47 +00002050/// pushValueAndType - The file has to encode both the value and type id for
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002051/// many values, because we need to know what type to create for forward
2052/// references. However, most operands are not forward references, so this type
2053/// field is not needed.
2054///
2055/// This function adds V's value ID to Vals. If the value ID is higher than the
2056/// instruction ID, then it is a forward reference, and it also includes the
Jan Wen Voungafaced02012-10-11 20:20:40 +00002057/// type ID. The value ID that is written is encoded relative to the InstID.
Teresa Johnson37687f32016-04-23 04:30:47 +00002058bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2059 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002060 unsigned ValID = VE.getValueID(V);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002061 // Make encoding relative to the InstID.
2062 Vals.push_back(InstID - ValID);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002063 if (ValID >= InstID) {
2064 Vals.push_back(VE.getTypeID(V->getType()));
2065 return true;
2066 }
2067 return false;
2068}
2069
Teresa Johnson37687f32016-04-23 04:30:47 +00002070void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2071 unsigned InstID) {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002072 SmallVector<unsigned, 64> Record;
2073 LLVMContext &C = CS.getInstruction()->getContext();
2074
2075 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002076 const auto &Bundle = CS.getOperandBundleAt(i);
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002077 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002078
2079 for (auto &Input : Bundle.Inputs)
Teresa Johnson37687f32016-04-23 04:30:47 +00002080 pushValueAndType(Input, InstID, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002081
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002082 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002083 Record.clear();
2084 }
2085}
2086
Teresa Johnson37687f32016-04-23 04:30:47 +00002087/// pushValue - Like pushValueAndType, but where the type of the value is
Jan Wen Voungafaced02012-10-11 20:20:40 +00002088/// omitted (perhaps it was already encoded in an earlier operand).
Teresa Johnson37687f32016-04-23 04:30:47 +00002089void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2090 SmallVectorImpl<unsigned> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002091 unsigned ValID = VE.getValueID(V);
2092 Vals.push_back(InstID - ValID);
2093}
2094
Teresa Johnson37687f32016-04-23 04:30:47 +00002095void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2096 SmallVectorImpl<uint64_t> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002097 unsigned ValID = VE.getValueID(V);
2098 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2099 emitSignedInt64(Vals, diff);
2100}
2101
Chris Lattnere6e364c2007-04-26 05:53:54 +00002102/// WriteInstruction - Emit an instruction to the specified stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002103void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2104 unsigned InstID,
2105 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnere6e364c2007-04-26 05:53:54 +00002106 unsigned Code = 0;
2107 unsigned AbbrevToUse = 0;
Devang Patelaf206b82009-09-18 19:26:43 +00002108 VE.setInstructionID(&I);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002109 switch (I.getOpcode()) {
2110 default:
2111 if (Instruction::isCast(I.getOpcode())) {
Chris Lattner0a603252007-05-01 02:13:26 +00002112 Code = bitc::FUNC_CODE_INST_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002113 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002114 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002115 Vals.push_back(VE.getTypeID(I.getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002116 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
Chris Lattnere6e364c2007-04-26 05:53:54 +00002117 } else {
2118 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattner9f35f912007-05-02 04:26:36 +00002119 Code = bitc::FUNC_CODE_INST_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00002120 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002121 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Teresa Johnson37687f32016-04-23 04:30:47 +00002122 pushValue(I.getOperand(1), InstID, Vals);
2123 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2124 uint64_t Flags = getOptimizationFlags(&I);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002125 if (Flags != 0) {
2126 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2127 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2128 Vals.push_back(Flags);
2129 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002130 }
2131 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002132
David Blaikieb5b5efd2015-02-25 01:08:52 +00002133 case Instruction::GetElementPtr: {
Chris Lattner0a603252007-05-01 02:13:26 +00002134 Code = bitc::FUNC_CODE_INST_GEP;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002135 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2136 auto &GEPInst = cast<GetElementPtrInst>(I);
2137 Vals.push_back(GEPInst.isInBounds());
2138 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002139 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002140 pushValueAndType(I.getOperand(i), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002141 break;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002142 }
Dan Gohmanca0256a2008-05-31 19:11:15 +00002143 case Instruction::ExtractValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002144 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002145 pushValueAndType(I.getOperand(0), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002146 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002147 Vals.append(EVI->idx_begin(), EVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002148 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002149 }
2150 case Instruction::InsertValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002151 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002152 pushValueAndType(I.getOperand(0), InstID, Vals);
2153 pushValueAndType(I.getOperand(1), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002154 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002155 Vals.append(IVI->idx_begin(), IVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002156 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002157 }
Chris Lattner0a603252007-05-01 02:13:26 +00002158 case Instruction::Select:
Dan Gohmanc5d28922008-09-16 01:01:33 +00002159 Code = bitc::FUNC_CODE_INST_VSELECT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002160 pushValueAndType(I.getOperand(1), InstID, Vals);
2161 pushValue(I.getOperand(2), InstID, Vals);
2162 pushValueAndType(I.getOperand(0), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002163 break;
2164 case Instruction::ExtractElement:
2165 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002166 pushValueAndType(I.getOperand(0), InstID, Vals);
2167 pushValueAndType(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002168 break;
2169 case Instruction::InsertElement:
2170 Code = bitc::FUNC_CODE_INST_INSERTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002171 pushValueAndType(I.getOperand(0), InstID, Vals);
2172 pushValue(I.getOperand(1), InstID, Vals);
2173 pushValueAndType(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002174 break;
2175 case Instruction::ShuffleVector:
2176 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002177 pushValueAndType(I.getOperand(0), InstID, Vals);
2178 pushValue(I.getOperand(1), InstID, Vals);
2179 pushValue(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002180 break;
2181 case Instruction::ICmp:
James Molloy88eb5352015-07-10 12:52:00 +00002182 case Instruction::FCmp: {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002183 // compare returning Int1Ty or vector of Int1Ty
2184 Code = bitc::FUNC_CODE_INST_CMP2;
Teresa Johnson37687f32016-04-23 04:30:47 +00002185 pushValueAndType(I.getOperand(0), InstID, Vals);
2186 pushValue(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002187 Vals.push_back(cast<CmpInst>(I).getPredicate());
Teresa Johnson37687f32016-04-23 04:30:47 +00002188 uint64_t Flags = getOptimizationFlags(&I);
James Molloy88eb5352015-07-10 12:52:00 +00002189 if (Flags != 0)
2190 Vals.push_back(Flags);
Chris Lattner0a603252007-05-01 02:13:26 +00002191 break;
James Molloy88eb5352015-07-10 12:52:00 +00002192 }
Chris Lattner0a603252007-05-01 02:13:26 +00002193
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002194 case Instruction::Ret:
Devang Patelbbfd8742008-02-26 01:29:32 +00002195 {
2196 Code = bitc::FUNC_CODE_INST_RET;
2197 unsigned NumOperands = I.getNumOperands();
Devang Patelbbfd8742008-02-26 01:29:32 +00002198 if (NumOperands == 0)
2199 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2200 else if (NumOperands == 1) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002201 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Devang Patelbbfd8742008-02-26 01:29:32 +00002202 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2203 } else {
2204 for (unsigned i = 0, e = NumOperands; i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002205 pushValueAndType(I.getOperand(i), InstID, Vals);
Devang Patelbbfd8742008-02-26 01:29:32 +00002206 }
2207 }
Chris Lattner0a603252007-05-01 02:13:26 +00002208 break;
2209 case Instruction::Br:
Gabor Greif1933b002009-01-30 18:27:21 +00002210 {
2211 Code = bitc::FUNC_CODE_INST_BR;
David Blaikieb78e9e52013-02-11 01:16:51 +00002212 const BranchInst &II = cast<BranchInst>(I);
Gabor Greif1933b002009-01-30 18:27:21 +00002213 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2214 if (II.isConditional()) {
2215 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002216 pushValue(II.getCondition(), InstID, Vals);
Gabor Greif1933b002009-01-30 18:27:21 +00002217 }
Chris Lattner0a603252007-05-01 02:13:26 +00002218 }
2219 break;
2220 case Instruction::Switch:
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002221 {
2222 Code = bitc::FUNC_CODE_INST_SWITCH;
David Blaikieb78e9e52013-02-11 01:16:51 +00002223 const SwitchInst &SI = cast<SwitchInst>(I);
Bob Wilsone4077362013-09-09 19:14:35 +00002224 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002225 pushValue(SI.getCondition(), InstID, Vals);
Bob Wilsone4077362013-09-09 19:14:35 +00002226 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
Sanjay Patel225d65f2015-11-13 16:21:23 +00002227 for (SwitchInst::ConstCaseIt Case : SI.cases()) {
2228 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2229 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002230 }
2231 }
Chris Lattner0a603252007-05-01 02:13:26 +00002232 break;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002233 case Instruction::IndirectBr:
2234 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattner3ed871f2009-10-27 19:13:16 +00002235 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Jan Wen Voungafaced02012-10-11 20:20:40 +00002236 // Encode the address operand as relative, but not the basic blocks.
Teresa Johnson37687f32016-04-23 04:30:47 +00002237 pushValue(I.getOperand(0), InstID, Vals);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002238 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
Chris Lattner3ed871f2009-10-27 19:13:16 +00002239 Vals.push_back(VE.getValueID(I.getOperand(i)));
2240 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002241
Chris Lattnerb811e952007-05-01 07:03:37 +00002242 case Instruction::Invoke: {
Gabor Greif4b79e472009-01-16 18:40:27 +00002243 const InvokeInst *II = cast<InvokeInst>(&I);
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002244 const Value *Callee = II->getCalledValue();
2245 FunctionType *FTy = II->getFunctionType();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002246
2247 if (II->hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002248 writeOperandBundles(II, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002249
Chris Lattner0a603252007-05-01 02:13:26 +00002250 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002251
Devang Patel4c758ea2008-09-25 21:00:45 +00002252 Vals.push_back(VE.getAttributeID(II->getAttributes()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002253 Vals.push_back(II->getCallingConv() | 1 << 13);
Gabor Greif6ecd6f42009-01-07 22:39:29 +00002254 Vals.push_back(VE.getValueID(II->getNormalDest()));
2255 Vals.push_back(VE.getValueID(II->getUnwindDest()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002256 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002257 pushValueAndType(Callee, InstID, Vals);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002258
Chris Lattner0a603252007-05-01 02:13:26 +00002259 // Emit value #'s for the fixed parameters.
Chris Lattner0a603252007-05-01 02:13:26 +00002260 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002261 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
Chris Lattner0a603252007-05-01 02:13:26 +00002262
2263 // Emit type/value pairs for varargs params.
2264 if (FTy->isVarArg()) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00002265 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002266 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002267 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
Chris Lattner0a603252007-05-01 02:13:26 +00002268 }
2269 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002270 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002271 case Instruction::Resume:
2272 Code = bitc::FUNC_CODE_INST_RESUME;
Teresa Johnson37687f32016-04-23 04:30:47 +00002273 pushValueAndType(I.getOperand(0), InstID, Vals);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002274 break;
David Majnemer654e1302015-07-31 17:58:14 +00002275 case Instruction::CleanupRet: {
2276 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2277 const auto &CRI = cast<CleanupReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002278 pushValue(CRI.getCleanupPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002279 if (CRI.hasUnwindDest())
2280 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2281 break;
2282 }
2283 case Instruction::CatchRet: {
2284 Code = bitc::FUNC_CODE_INST_CATCHRET;
2285 const auto &CRI = cast<CatchReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002286 pushValue(CRI.getCatchPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002287 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2288 break;
2289 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00002290 case Instruction::CleanupPad:
David Majnemer654e1302015-07-31 17:58:14 +00002291 case Instruction::CatchPad: {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002292 const auto &FuncletPad = cast<FuncletPadInst>(I);
2293 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2294 : bitc::FUNC_CODE_INST_CLEANUPPAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002295 pushValue(FuncletPad.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002296
2297 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
David Majnemer654e1302015-07-31 17:58:14 +00002298 Vals.push_back(NumArgOperands);
2299 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
Teresa Johnson37687f32016-04-23 04:30:47 +00002300 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002301 break;
2302 }
2303 case Instruction::CatchSwitch: {
2304 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2305 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2306
Teresa Johnson37687f32016-04-23 04:30:47 +00002307 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002308
2309 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2310 Vals.push_back(NumHandlers);
2311 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2312 Vals.push_back(VE.getValueID(CatchPadBB));
2313
2314 if (CatchSwitch.hasUnwindDest())
2315 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
David Majnemer654e1302015-07-31 17:58:14 +00002316 break;
2317 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002318 case Instruction::Unreachable:
2319 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002320 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002321 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002322
Jay Foad372ad642011-06-20 14:18:48 +00002323 case Instruction::PHI: {
2324 const PHINode &PN = cast<PHINode>(I);
Chris Lattner0a603252007-05-01 02:13:26 +00002325 Code = bitc::FUNC_CODE_INST_PHI;
Jan Wen Voungafaced02012-10-11 20:20:40 +00002326 // With the newer instruction encoding, forward references could give
2327 // negative valued IDs. This is most common for PHIs, so we use
2328 // signed VBRs.
2329 SmallVector<uint64_t, 128> Vals64;
2330 Vals64.push_back(VE.getTypeID(PN.getType()));
Jay Foad372ad642011-06-20 14:18:48 +00002331 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002332 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002333 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
Jay Foad372ad642011-06-20 14:18:48 +00002334 }
Jan Wen Voungafaced02012-10-11 20:20:40 +00002335 // Emit a Vals64 vector and exit.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002336 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002337 Vals64.clear();
2338 return;
Jay Foad372ad642011-06-20 14:18:48 +00002339 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002340
Bill Wendlingfae14752011-08-12 20:24:12 +00002341 case Instruction::LandingPad: {
2342 const LandingPadInst &LP = cast<LandingPadInst>(I);
2343 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2344 Vals.push_back(VE.getTypeID(LP.getType()));
Bill Wendlingfae14752011-08-12 20:24:12 +00002345 Vals.push_back(LP.isCleanup());
2346 Vals.push_back(LP.getNumClauses());
2347 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2348 if (LP.isCatch(I))
2349 Vals.push_back(LandingPadInst::Catch);
2350 else
2351 Vals.push_back(LandingPadInst::Filter);
Teresa Johnson37687f32016-04-23 04:30:47 +00002352 pushValueAndType(LP.getClause(I), InstID, Vals);
Bill Wendlingfae14752011-08-12 20:24:12 +00002353 }
2354 break;
2355 }
2356
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002357 case Instruction::Alloca: {
Chris Lattner0a603252007-05-01 02:13:26 +00002358 Code = bitc::FUNC_CODE_INST_ALLOCA;
David Blaikiebdb49102015-04-28 16:51:01 +00002359 const AllocaInst &AI = cast<AllocaInst>(I);
2360 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
Dan Gohman9da5bb02010-05-28 01:38:28 +00002361 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002362 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002363 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2364 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2365 "not enough bits for maximum alignment");
2366 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2367 AlignRecord |= AI.isUsedWithInAlloca() << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00002368 AlignRecord |= 1 << 6;
Manman Ren9bfd0d02016-04-01 21:41:15 +00002369 AlignRecord |= AI.isSwiftError() << 7;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002370 Vals.push_back(AlignRecord);
Chris Lattner0a603252007-05-01 02:13:26 +00002371 break;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002372 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002373
Chris Lattner0a603252007-05-01 02:13:26 +00002374 case Instruction::Load:
Eli Friedman59b66882011-08-09 23:02:53 +00002375 if (cast<LoadInst>(I).isAtomic()) {
2376 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002377 pushValueAndType(I.getOperand(0), InstID, Vals);
Eli Friedman59b66882011-08-09 23:02:53 +00002378 } else {
2379 Code = bitc::FUNC_CODE_INST_LOAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002380 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
Eli Friedman59b66882011-08-09 23:02:53 +00002381 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2382 }
David Blaikie85035652015-02-25 01:07:20 +00002383 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002384 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2385 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002386 if (cast<LoadInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002387 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2388 Vals.push_back(getEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002389 }
Chris Lattner0a603252007-05-01 02:13:26 +00002390 break;
2391 case Instruction::Store:
Eli Friedman59b66882011-08-09 23:02:53 +00002392 if (cast<StoreInst>(I).isAtomic())
2393 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2394 else
2395 Code = bitc::FUNC_CODE_INST_STORE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002396 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2397 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
Chris Lattner0a603252007-05-01 02:13:26 +00002398 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2399 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002400 if (cast<StoreInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002401 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2402 Vals.push_back(getEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002403 }
Chris Lattner0a603252007-05-01 02:13:26 +00002404 break;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002405 case Instruction::AtomicCmpXchg:
2406 Code = bitc::FUNC_CODE_INST_CMPXCHG;
Teresa Johnson37687f32016-04-23 04:30:47 +00002407 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2408 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2409 pushValue(I.getOperand(2), InstID, Vals); // newval.
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002410 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002411 Vals.push_back(
2412 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2413 Vals.push_back(
2414 getEncodedSynchScope(cast<AtomicCmpXchgInst>(I).getSynchScope()));
2415 Vals.push_back(
2416 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
Tim Northover420a2162014-06-13 14:24:07 +00002417 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002418 break;
2419 case Instruction::AtomicRMW:
2420 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
Teresa Johnson37687f32016-04-23 04:30:47 +00002421 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2422 pushValue(I.getOperand(1), InstID, Vals); // val.
2423 Vals.push_back(
2424 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002425 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002426 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2427 Vals.push_back(
2428 getEncodedSynchScope(cast<AtomicRMWInst>(I).getSynchScope()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002429 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002430 case Instruction::Fence:
2431 Code = bitc::FUNC_CODE_INST_FENCE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002432 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2433 Vals.push_back(getEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
Eli Friedmanfee02c62011-07-25 23:16:38 +00002434 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002435 case Instruction::Call: {
Gabor Greife9afee22010-06-26 09:35:09 +00002436 const CallInst &CI = cast<CallInst>(I);
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002437 FunctionType *FTy = CI.getFunctionType();
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002438
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002439 if (CI.hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002440 writeOperandBundles(&CI, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002441
Chris Lattnerc44070802011-06-17 18:17:37 +00002442 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002443
Gabor Greife9afee22010-06-26 09:35:09 +00002444 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002445
Teresa Johnson37687f32016-04-23 04:30:47 +00002446 unsigned Flags = getOptimizationFlags(&I);
Akira Hatanaka97cb3972015-11-07 02:48:49 +00002447 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2448 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2449 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2450 1 << bitc::CALL_EXPLICIT_TYPE |
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002451 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2452 unsigned(Flags != 0) << bitc::CALL_FMF);
2453 if (Flags != 0)
2454 Vals.push_back(Flags);
2455
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002456 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002457 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002458
Chris Lattner0a603252007-05-01 02:13:26 +00002459 // Emit value #'s for the fixed parameters.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002460 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2461 // Check for labels (can happen with asm labels).
2462 if (FTy->getParamType(i)->isLabelTy())
2463 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2464 else
Teresa Johnson37687f32016-04-23 04:30:47 +00002465 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002466 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002467
Chris Lattnerb811e952007-05-01 07:03:37 +00002468 // Emit type/value pairs for varargs params.
2469 if (FTy->isVarArg()) {
Gabor Greife9afee22010-06-26 09:35:09 +00002470 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002471 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002472 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
Chris Lattner0a603252007-05-01 02:13:26 +00002473 }
2474 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002475 }
Chris Lattner0a603252007-05-01 02:13:26 +00002476 case Instruction::VAArg:
2477 Code = bitc::FUNC_CODE_INST_VAARG;
2478 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
Teresa Johnson37687f32016-04-23 04:30:47 +00002479 pushValue(I.getOperand(0), InstID, Vals); // valist.
Chris Lattner0a603252007-05-01 02:13:26 +00002480 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2481 break;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002482 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002483
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002484 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002485 Vals.clear();
2486}
2487
Teresa Johnson37687f32016-04-23 04:30:47 +00002488/// Emit names for globals/functions etc. \p IsModuleLevel is true when
2489/// we are writing the module-level VST, where we are including a function
2490/// bitcode index and need to backpatch the VST forward declaration record.
2491void ModuleBitcodeWriter::writeValueSymbolTable(
2492 const ValueSymbolTable &VST, bool IsModuleLevel,
2493 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002494 if (VST.empty()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002495 // writeValueSymbolTableForwardDecl should have returned early as
Teresa Johnsonff642b92015-09-17 20:12:00 +00002496 // well. Ensure this handling remains in sync by asserting that
2497 // the placeholder offset is not set.
Teresa Johnson37687f32016-04-23 04:30:47 +00002498 assert(!IsModuleLevel || !hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002499 return;
2500 }
2501
Teresa Johnson37687f32016-04-23 04:30:47 +00002502 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002503 // Get the offset of the VST we are writing, and backpatch it into
2504 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002505 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002506 // The BitcodeStartBit was the stream offset of the actual bitcode
2507 // (e.g. excluding any initial darwin header).
Teresa Johnson37687f32016-04-23 04:30:47 +00002508 VSTOffset -= bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002509 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002510 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002511 }
2512
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002513 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2eae59f2007-05-04 20:34:50 +00002514
Teresa Johnsonff642b92015-09-17 20:12:00 +00002515 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2516 // records, which are not used in the per-function VSTs.
2517 unsigned FnEntry8BitAbbrev;
2518 unsigned FnEntry7BitAbbrev;
2519 unsigned FnEntry6BitAbbrev;
Teresa Johnson37687f32016-04-23 04:30:47 +00002520 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002521 // 8-bit fixed-width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002522 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2523 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002524 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2525 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002526 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2527 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002528 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002529
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002530 // 7-bit fixed width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002531 Abbv = new BitCodeAbbrev();
2532 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002533 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2534 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002535 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2536 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002537 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002538
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002539 // 6-bit char6 VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002540 Abbv = new BitCodeAbbrev();
2541 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002542 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2543 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002544 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2545 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002546 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002547 }
2548
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002549 // FIXME: Set up the abbrev, we know how many values there are!
2550 // FIXME: We know if the type names can use 7-bit ascii.
2551 SmallVector<unsigned, 64> NameVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002552
Yaron Keren001e2e42015-08-10 07:04:29 +00002553 for (const ValueName &Name : VST) {
Chris Lattner4d925982007-05-04 20:52:02 +00002554 // Figure out the encoding to use for the name.
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002555 StringEncoding Bits =
2556 getStringEncoding(Name.getKeyData(), Name.getKeyLength());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002557
Chris Lattnera0987962007-05-04 21:31:13 +00002558 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002559 NameVals.push_back(VE.getValueID(Name.getValue()));
2560
2561 Function *F = dyn_cast<Function>(Name.getValue());
2562 if (!F) {
2563 // If value is an alias, need to get the aliased base object to
2564 // see if it is a function.
2565 auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2566 if (GA && GA->getBaseObject())
2567 F = dyn_cast<Function>(GA->getBaseObject());
2568 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002569
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002570 // VST_CODE_ENTRY: [valueid, namechar x N]
2571 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
2572 // VST_CODE_BBENTRY: [bbid, namechar x N]
Chris Lattner6be58c62007-05-03 22:18:21 +00002573 unsigned Code;
Yaron Keren001e2e42015-08-10 07:04:29 +00002574 if (isa<BasicBlock>(Name.getValue())) {
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002575 Code = bitc::VST_CODE_BBENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002576 if (Bits == SE_Char6)
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002577 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002578 } else if (F && !F->isDeclaration()) {
2579 // Must be the module-level VST, where we pass in the Index and
2580 // have a VSTOffsetPlaceholder. The function-level VST should not
2581 // contain any Function symbols.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00002582 assert(FunctionToBitcodeIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00002583 assert(hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002584
2585 // Save the word offset of the function (from the start of the
2586 // actual bitcode written to the stream).
Teresa Johnson37687f32016-04-23 04:30:47 +00002587 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002588 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2589 NameVals.push_back(BitcodeIndex / 32);
2590
2591 Code = bitc::VST_CODE_FNENTRY;
2592 AbbrevToUse = FnEntry8BitAbbrev;
2593 if (Bits == SE_Char6)
2594 AbbrevToUse = FnEntry6BitAbbrev;
2595 else if (Bits == SE_Fixed7)
2596 AbbrevToUse = FnEntry7BitAbbrev;
Chris Lattner6be58c62007-05-03 22:18:21 +00002597 } else {
2598 Code = bitc::VST_CODE_ENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002599 if (Bits == SE_Char6)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002600 AbbrevToUse = VST_ENTRY_6_ABBREV;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002601 else if (Bits == SE_Fixed7)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002602 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002603 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002604
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002605 for (const auto P : Name.getKey())
2606 NameVals.push_back((unsigned char)P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002607
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002608 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002609 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002610 NameVals.clear();
2611 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002612 Stream.ExitBlock();
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002613}
2614
Teresa Johnson403a7872015-10-04 14:33:43 +00002615/// Emit function names and summary offsets for the combined index
2616/// used by ThinLTO.
Teresa Johnson37687f32016-04-23 04:30:47 +00002617void IndexBitcodeWriter::writeCombinedValueSymbolTable() {
2618 assert(hasVSTOffsetPlaceholder() && "Expected non-zero VSTOffsetPlaceholder");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002619 // Get the offset of the VST we are writing, and backpatch it into
2620 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002621 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002622 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002623 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002624
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002625 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00002626
Teresa Johnson403a7872015-10-04 14:33:43 +00002627 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002628 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_GVDEFENTRY));
2629 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // sumoffset
2631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // guid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002632 unsigned DefEntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002633
2634 Abbv = new BitCodeAbbrev();
2635 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2636 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2637 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002638 unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002639
Teresa Johnsone1164de2016-02-10 21:55:02 +00002640 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson403a7872015-10-04 14:33:43 +00002641
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002642 for (const auto &FII : Index) {
Mehdi Aminiad5741b2016-04-02 05:07:53 +00002643 GlobalValue::GUID FuncGUID = FII.first;
Teresa Johnson37687f32016-04-23 04:30:47 +00002644 unsigned ValueId = popValueId(FuncGUID);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002645
Teresa Johnsone1164de2016-02-10 21:55:02 +00002646 for (const auto &FI : FII.second) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002647 // VST_CODE_COMBINED_GVDEFENTRY: [valueid, sumoffset, guid]
Teresa Johnson37687f32016-04-23 04:30:47 +00002648 NameVals.push_back(ValueId);
Teresa Johnson403a7872015-10-04 14:33:43 +00002649 NameVals.push_back(FI->bitcodeIndex());
Teresa Johnsone1164de2016-02-10 21:55:02 +00002650 NameVals.push_back(FuncGUID);
Teresa Johnson403a7872015-10-04 14:33:43 +00002651
2652 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002653 Stream.EmitRecord(bitc::VST_CODE_COMBINED_GVDEFENTRY, NameVals,
2654 DefEntryAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00002655 NameVals.clear();
2656 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002657 }
Teresa Johnson37687f32016-04-23 04:30:47 +00002658 for (const auto &GVI : valueIds()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002659 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
2660 NameVals.push_back(GVI.second);
2661 NameVals.push_back(GVI.first);
2662
2663 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002664 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002665 NameVals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00002666 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002667 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002668}
2669
Teresa Johnson37687f32016-04-23 04:30:47 +00002670void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002671 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2672 unsigned Code;
2673 if (isa<BasicBlock>(Order.V))
2674 Code = bitc::USELIST_CODE_BB;
2675 else
2676 Code = bitc::USELIST_CODE_DEFAULT;
2677
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002678 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002679 Record.push_back(VE.getValueID(Order.V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002680 Stream.EmitRecord(Code, Record);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002681}
2682
Teresa Johnson37687f32016-04-23 04:30:47 +00002683void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002684 assert(VE.shouldPreserveUseListOrder() &&
2685 "Expected to be preserving use-list order");
2686
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002687 auto hasMore = [&]() {
2688 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2689 };
2690 if (!hasMore())
2691 // Nothing to do.
2692 return;
2693
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002694 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002695 while (hasMore()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002696 writeUseList(std::move(VE.UseListOrders.back()));
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002697 VE.UseListOrders.pop_back();
2698 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002699 Stream.ExitBlock();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002700}
2701
Teresa Johnson403a7872015-10-04 14:33:43 +00002702/// Emit a function body to the module stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002703void ModuleBitcodeWriter::writeFunction(
2704 const Function &F,
2705 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002706 // Save the bitcode index of the start of this function block for recording
2707 // in the VST.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002708 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002709
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002710 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a11b642011-06-03 17:02:19 +00002711 VE.incorporateFunction(F);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002712
2713 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002714
Chris Lattnere6e364c2007-04-26 05:53:54 +00002715 // Emit the number of basic blocks, so the reader can create them ahead of
2716 // time.
2717 Vals.push_back(VE.getBasicBlocks().size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002718 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002719 Vals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002720
Chris Lattnere6e364c2007-04-26 05:53:54 +00002721 // If there are function-local constants, emit them now.
2722 unsigned CstStart, CstEnd;
2723 VE.getFunctionConstantRange(CstStart, CstEnd);
Teresa Johnson37687f32016-04-23 04:30:47 +00002724 writeConstants(CstStart, CstEnd, false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002725
Victor Hernandez6c730de2010-01-14 01:50:08 +00002726 // If there is function-local metadata, emit it now.
Teresa Johnson37687f32016-04-23 04:30:47 +00002727 writeFunctionMetadata(F);
Victor Hernandez6c730de2010-01-14 01:50:08 +00002728
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002729 // Keep a running idea of what the instruction ID is.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002730 unsigned InstID = CstEnd;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002731
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002732 bool NeedsMetadataAttachment = F.hasMetadata();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002733
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002734 DILocation *LastDL = nullptr;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002735 // Finally, emit all the instructions, in order.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002736 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002737 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2738 I != E; ++I) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002739 writeInstruction(*I, InstID, Vals);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002740
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00002741 if (!I->getType()->isVoidTy())
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002742 ++InstID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002743
Chris Lattner07d09ed2010-04-03 02:17:50 +00002744 // If the instruction has metadata, write a metadata attachment later.
2745 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002746
Chris Lattner07d09ed2010-04-03 02:17:50 +00002747 // If the instruction has a debug location, emit it.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002748 DILocation *DL = I->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002749 if (!DL)
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002750 continue;
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002751
2752 if (DL == LastDL) {
Chris Lattner07d09ed2010-04-03 02:17:50 +00002753 // Just repeat the same debug loc as last time.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002754 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002755 continue;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002756 }
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002757
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002758 Vals.push_back(DL->getLine());
2759 Vals.push_back(DL->getColumn());
2760 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2761 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002762 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002763 Vals.clear();
Duncan P. N. Exon Smith538ef562015-05-06 22:51:12 +00002764
2765 LastDL = DL;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002766 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002767
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002768 // Emit names for all the instructions etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002769 writeValueSymbolTable(F.getValueSymbolTable());
Devang Patelaf206b82009-09-18 19:26:43 +00002770
Chris Lattner07d09ed2010-04-03 02:17:50 +00002771 if (NeedsMetadataAttachment)
Teresa Johnson37687f32016-04-23 04:30:47 +00002772 writeMetadataAttachment(F);
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002773 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00002774 writeUseListBlock(&F);
Chad Rosier6a11b642011-06-03 17:02:19 +00002775 VE.purgeFunction();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002776 Stream.ExitBlock();
Chris Lattner831d4202007-04-26 03:27:58 +00002777}
2778
Chris Lattner702658c2007-05-04 18:26:27 +00002779// Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002780void ModuleBitcodeWriter::writeBlockInfo() {
Chris Lattner702658c2007-05-04 18:26:27 +00002781 // We only want to emit block info records for blocks that have multiple
Jan Wen Voungafaced02012-10-11 20:20:40 +00002782 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
Jan Wen Voung8c9e9412012-10-11 21:45:16 +00002783 // Other blocks can define their abbrevs inline.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002784 Stream.EnterBlockInfoBlock(2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002785
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002786 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002787 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2790 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2791 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002792 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002793 VST_ENTRY_8_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002794 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002795 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002796
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002797 { // 7-bit fixed width VST_CODE_ENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002798 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2799 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2801 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2802 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002803 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002804 VST_ENTRY_7_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002805 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002806 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002807 { // 6-bit char6 VST_CODE_ENTRY strings.
Chris Lattnere760d6f2007-05-05 01:26:50 +00002808 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2809 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2810 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2811 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2812 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002813 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002814 VST_ENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002815 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere760d6f2007-05-05 01:26:50 +00002816 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002817 { // 6-bit char6 VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002818 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2819 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2820 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2821 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnere760d6f2007-05-05 01:26:50 +00002822 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002823 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002824 VST_BBENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002825 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002826 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002827
2828
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002829
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002830 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2831 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2832 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2833 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
David Blaikie7b028102015-02-25 00:51:52 +00002834 VE.computeBitsRequiredForTypeIndicies()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002835 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002836 CONSTANTS_SETTYPE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002837 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002838 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002839
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002840 { // INTEGER abbrev for CONSTANTS_BLOCK.
2841 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2842 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2843 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002844 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002845 CONSTANTS_INTEGER_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002846 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002847 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002848
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002849 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2850 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2851 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2852 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2853 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
David Blaikie7b028102015-02-25 00:51:52 +00002854 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002855 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2856
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002857 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002858 CONSTANTS_CE_CAST_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002859 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002860 }
2861 { // NULL abbrev for CONSTANTS_BLOCK.
2862 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2863 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002864 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002865 CONSTANTS_NULL_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002866 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002867 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002868
Chris Lattnerb80751d2007-05-05 07:44:49 +00002869 // FIXME: This should only use space for first class types!
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002870
Chris Lattnerb80751d2007-05-05 07:44:49 +00002871 { // INST_LOAD abbrev for FUNCTION_BLOCK.
2872 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2873 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattnerb80751d2007-05-05 07:44:49 +00002874 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
David Blaikie85035652015-02-25 01:07:20 +00002875 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2876 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerb80751d2007-05-05 07:44:49 +00002877 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
2878 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002879 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002880 FUNCTION_INST_LOAD_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002881 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerb80751d2007-05-05 07:44:49 +00002882 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002883 { // INST_BINOP abbrev for FUNCTION_BLOCK.
2884 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2885 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2886 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2887 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2888 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002889 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002890 FUNCTION_INST_BINOP_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002891 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002892 }
Dan Gohman0ebd6962009-07-20 21:19:07 +00002893 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2894 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2895 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2896 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2897 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2898 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2899 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002900 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002901 FUNCTION_INST_BINOP_FLAGS_ABBREV)
Dan Gohman0ebd6962009-07-20 21:19:07 +00002902 llvm_unreachable("Unexpected abbrev ordering!");
2903 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002904 { // INST_CAST abbrev for FUNCTION_BLOCK.
2905 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2906 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2907 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2908 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
David Blaikie7b028102015-02-25 00:51:52 +00002909 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002910 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
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_CAST_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002913 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002914 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002915
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002916 { // INST_RET abbrev for FUNCTION_BLOCK.
2917 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2918 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002919 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002920 FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002921 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002922 }
2923 { // INST_RET abbrev for FUNCTION_BLOCK.
2924 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2925 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002927 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002928 FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002929 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002930 }
2931 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2932 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2933 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002934 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002935 FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002936 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002937 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00002938 {
2939 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2940 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2941 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2942 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2943 Log2_32_Ceil(VE.getTypes().size() + 1)));
2944 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2945 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002946 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
David Blaikieb5b5efd2015-02-25 01:08:52 +00002947 FUNCTION_INST_GEP_ABBREV)
2948 llvm_unreachable("Unexpected abbrev ordering!");
2949 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002950
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002951 Stream.ExitBlock();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002952}
2953
Teresa Johnson403a7872015-10-04 14:33:43 +00002954/// Write the module path strings, currently only used when generating
2955/// a combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00002956void IndexBitcodeWriter::writeModStrings() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002957 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00002958
2959 // TODO: See which abbrev sizes we actually need to emit
2960
2961 // 8-bit fixed-width MST_ENTRY strings.
2962 BitCodeAbbrev *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::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002967 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002968
2969 // 7-bit fixed width MST_ENTRY strings.
2970 Abbv = new BitCodeAbbrev();
2971 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2974 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002975 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002976
2977 // 6-bit char6 MST_ENTRY strings.
2978 Abbv = new BitCodeAbbrev();
2979 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2980 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2981 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2982 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002983 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002984
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002985 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
2986 Abbv = new BitCodeAbbrev();
2987 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
2988 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2989 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2990 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2991 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2992 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002993 unsigned AbbrevHash = Stream.EmitAbbrev(Abbv);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002994
2995 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002996 for (const auto &MPSE : Index.modulePaths()) {
Teresa Johnson403a7872015-10-04 14:33:43 +00002997 StringEncoding Bits =
2998 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
2999 unsigned AbbrevToUse = Abbrev8Bit;
3000 if (Bits == SE_Char6)
3001 AbbrevToUse = Abbrev6Bit;
3002 else if (Bits == SE_Fixed7)
3003 AbbrevToUse = Abbrev7Bit;
3004
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003005 Vals.push_back(MPSE.getValue().first);
Teresa Johnson403a7872015-10-04 14:33:43 +00003006
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003007 for (const auto P : MPSE.getKey())
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003008 Vals.push_back((unsigned char)P);
Teresa Johnson403a7872015-10-04 14:33:43 +00003009
3010 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003011 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003012
3013 Vals.clear();
3014 // Emit an optional hash for the module now
3015 auto &Hash = MPSE.getValue().second;
3016 bool AllZero = true; // Detect if the hash is empty, and do not generate it
3017 for (auto Val : Hash) {
3018 if (Val)
3019 AllZero = false;
3020 Vals.push_back(Val);
3021 }
3022 if (!AllZero) {
3023 // Emit the hash record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003024 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003025 }
3026
3027 Vals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00003028 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003029 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003030}
3031
3032// Helper to emit a single function summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003033void ModuleBitcodeWriter::writePerModuleFunctionSummaryRecord(
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003034 SmallVector<uint64_t, 64> &NameVals, GlobalValueInfo *Info,
Teresa Johnson37687f32016-04-23 04:30:47 +00003035 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3036 const Function &F) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003037 NameVals.push_back(ValueID);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003038
3039 FunctionSummary *FS = cast<FunctionSummary>(Info->summary());
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003040 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003041 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003042 NameVals.push_back(FS->refs().size());
3043
3044 for (auto &RI : FS->refs())
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003045 NameVals.push_back(VE.getValueID(RI.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003046
3047 bool HasProfileData = F.getEntryCount().hasValue();
Teresa Johnsonaae26102016-03-25 18:59:13 +00003048 for (auto &ECI : FS->calls()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003049 NameVals.push_back(VE.getValueID(ECI.first.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003050 assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite");
3051 NameVals.push_back(ECI.second.CallsiteCount);
3052 if (HasProfileData)
3053 NameVals.push_back(ECI.second.ProfileCount);
3054 }
3055
3056 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3057 unsigned Code =
3058 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
Teresa Johnson403a7872015-10-04 14:33:43 +00003059
3060 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003061 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003062 NameVals.clear();
3063}
3064
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003065// Collect the global value references in the given variable's initializer,
3066// and emit them in a summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003067void ModuleBitcodeWriter::writeModuleLevelReferences(
3068 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3069 unsigned FSModRefsAbbrev) {
Teresa Johnson13968092016-03-15 19:35:45 +00003070 // Only interested in recording variable defs in the summary.
3071 if (V.isDeclaration())
3072 return;
Teresa Johnson13968092016-03-15 19:35:45 +00003073 NameVals.push_back(VE.getValueID(&V));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003074 NameVals.push_back(getEncodedGVSummaryFlags(V));
Teresa Johnson37687f32016-04-23 04:30:47 +00003075 auto *Info = Index->getGlobalValueInfo(V);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003076 GlobalVarSummary *VS = cast<GlobalVarSummary>(Info->summary());
3077 for (auto Ref : VS->refs())
3078 NameVals.push_back(VE.getValueID(Ref.getValue()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003079 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3080 FSModRefsAbbrev);
Teresa Johnson13968092016-03-15 19:35:45 +00003081 NameVals.clear();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003082}
Teresa Johnson403a7872015-10-04 14:33:43 +00003083
Mehdi Amini8fe69362016-04-24 03:18:11 +00003084// Current version for the summary.
3085// This is bumped whenever we introduce changes in the way some record are
3086// interpreted, like flags for instance.
3087static const uint64_t INDEX_VERSION = 1;
3088
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003089/// Emit the per-module summary section alongside the rest of
3090/// the module's bitcode.
Teresa Johnson37687f32016-04-23 04:30:47 +00003091void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003092 if (M.empty())
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003093 return;
3094
Teresa Johnson37687f32016-04-23 04:30:47 +00003095 if (Index->begin() == Index->end())
Teresa Johnsonb35cc692016-04-20 14:39:45 +00003096 return;
3097
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003098 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 4);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003099
Mehdi Amini8fe69362016-04-24 03:18:11 +00003100 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3101
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003102 // Abbrev for FS_PERMODULE.
Teresa Johnson403a7872015-10-04 14:33:43 +00003103 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003104 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003105 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003106 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003107 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003108 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3109 // numrefs x valueid, n x (valueid, callsitecount)
3110 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3111 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003112 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003113
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003114 // Abbrev for FS_PERMODULE_PROFILE.
3115 Abbv = new BitCodeAbbrev();
3116 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3117 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003118 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003119 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3120 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3121 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3122 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3123 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003124 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003125
3126 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3127 Abbv = new BitCodeAbbrev();
3128 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3129 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003130 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003131 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3132 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003133 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003134
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003135 // Abbrev for FS_ALIAS.
3136 Abbv = new BitCodeAbbrev();
3137 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3138 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003139 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003140 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003141 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003142
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003143 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003144 // Iterate over the list of functions instead of the Index to
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003145 // ensure the ordering is stable.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003146 for (const Function &F : M) {
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003147 if (F.isDeclaration())
3148 continue;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003149 // Summary emission does not support anonymous functions, they have to
3150 // renamed using the anonymous function renaming pass.
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003151 if (!F.hasName())
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003152 report_fatal_error("Unexpected anonymous function when writing summary");
Teresa Johnson403a7872015-10-04 14:33:43 +00003153
Teresa Johnson37687f32016-04-23 04:30:47 +00003154 auto *Info = Index->getGlobalValueInfo(F);
3155 writePerModuleFunctionSummaryRecord(
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003156 NameVals, Info,
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003157 VE.getValueID(M.getValueSymbolTable().lookup(F.getName())),
Teresa Johnson37687f32016-04-23 04:30:47 +00003158 FSCallsAbbrev, FSCallsProfileAbbrev, F);
Teresa Johnson403a7872015-10-04 14:33:43 +00003159 }
3160
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003161 // Capture references from GlobalVariable initializers, which are outside
3162 // of a function scope.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003163 for (const GlobalVariable &G : M.globals())
Teresa Johnson37687f32016-04-23 04:30:47 +00003164 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003165
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003166 for (const GlobalAlias &A : M.aliases()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003167 auto *Aliasee = A.getBaseObject();
3168 if (!Aliasee->hasName())
3169 // Nameless function don't have an entry in the summary, skip it.
3170 continue;
3171 auto AliasId = VE.getValueID(&A);
3172 auto AliaseeId = VE.getValueID(Aliasee);
3173 NameVals.push_back(AliasId);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003174 NameVals.push_back(getEncodedGVSummaryFlags(A));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003175 NameVals.push_back(AliaseeId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003176 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003177 NameVals.clear();
3178 }
3179
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003180 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003181}
3182
Teresa Johnson26ab5772016-03-15 00:04:37 +00003183/// Emit the combined summary section into the combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003184void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003185 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Mehdi Amini8fe69362016-04-24 03:18:11 +00003186 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
Teresa Johnson403a7872015-10-04 14:33:43 +00003187
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003188 // Abbrev for FS_COMBINED.
Teresa Johnson403a7872015-10-04 14:33:43 +00003189 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003190 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
3191 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003192 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003193 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3195 // numrefs x valueid, n x (valueid, callsitecount)
3196 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3197 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003198 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003199
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003200 // Abbrev for FS_COMBINED_PROFILE.
3201 Abbv = new BitCodeAbbrev();
3202 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
3203 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003204 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003205 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3206 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3207 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3208 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3209 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003210 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003211
3212 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
3213 Abbv = new BitCodeAbbrev();
3214 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
3215 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003216 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003217 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3218 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003219 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003220
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003221 // Abbrev for FS_COMBINED_ALIAS.
3222 Abbv = new BitCodeAbbrev();
3223 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
3224 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003225 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003226 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // offset
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003227 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003228
3229 // The aliases are emitted as a post-pass, and will point to the summary
3230 // offset id of the aliasee. For this purpose we need to be able to get back
3231 // from the summary to the offset
3232 SmallVector<GlobalValueInfo *, 64> Aliases;
3233 DenseMap<const GlobalValueSummary *, uint64_t> SummaryToOffsetMap;
3234
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003235 SmallVector<uint64_t, 64> NameVals;
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003236
3237 // For local linkage, we also emit the original name separately
3238 // immediately after the record.
3239 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3240 if (!GlobalValue::isLocalLinkage(S.linkage()))
3241 return;
3242 NameVals.push_back(S.getOriginalName());
3243 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3244 NameVals.clear();
3245 };
3246
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003247 for (const auto &FII : Index) {
Teresa Johnsone1164de2016-02-10 21:55:02 +00003248 for (auto &FI : FII.second) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003249 GlobalValueSummary *S = FI->summary();
3250 assert(S);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003251 if (isa<AliasSummary>(S)) {
3252 // Will process aliases as a post-pass because the reader wants all
3253 // global to be loaded first.
3254 Aliases.push_back(FI.get());
3255 continue;
3256 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003257
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003258 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003259 NameVals.push_back(Index.getModuleId(VS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003260 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003261 for (auto &RI : VS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003262 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003263 }
3264
3265 // Record the starting offset of this summary entry for use
3266 // in the VST entry. Add the current code size since the
3267 // reader will invoke readRecord after the abbrev id read.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003268 FI->setBitcodeIndex(Stream.GetCurrentBitNo() +
3269 Stream.GetAbbrevIDWidth());
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003270 // Store temporarily the offset in the map for a possible alias.
3271 SummaryToOffsetMap[S] = FI->bitcodeIndex();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003272
3273 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003274 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3275 FSModRefsAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003276 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003277 MaybeEmitOriginalName(*S);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003278 continue;
3279 }
3280
Teresa Johnson2794f712016-03-15 02:41:29 +00003281 auto *FS = cast<FunctionSummary>(S);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003282 NameVals.push_back(Index.getModuleId(FS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003283 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003284 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003285 NameVals.push_back(FS->refs().size());
3286
3287 for (auto &RI : FS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003288 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003289 }
3290
3291 bool HasProfileData = false;
Teresa Johnsonaae26102016-03-25 18:59:13 +00003292 for (auto &EI : FS->calls()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003293 HasProfileData |= EI.second.ProfileCount != 0;
3294 if (HasProfileData)
3295 break;
3296 }
3297
Teresa Johnsonaae26102016-03-25 18:59:13 +00003298 for (auto &EI : FS->calls()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003299 // If this GUID doesn't have a value id, it doesn't have a function
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003300 // summary and we don't need to record any calls to it.
Teresa Johnson37687f32016-04-23 04:30:47 +00003301 if (!hasValueId(EI.first.getGUID()))
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003302 continue;
Teresa Johnson37687f32016-04-23 04:30:47 +00003303 NameVals.push_back(getValueId(EI.first.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003304 assert(EI.second.CallsiteCount > 0 && "Expected at least one callsite");
3305 NameVals.push_back(EI.second.CallsiteCount);
3306 if (HasProfileData)
3307 NameVals.push_back(EI.second.ProfileCount);
3308 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003309
3310 // Record the starting offset of this summary entry for use
3311 // in the VST entry. Add the current code size since the
3312 // reader will invoke readRecord after the abbrev id read.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003313 FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003314 // Store temporarily the offset in the map for a possible alias.
3315 SummaryToOffsetMap[S] = FI->bitcodeIndex();
Teresa Johnson403a7872015-10-04 14:33:43 +00003316
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003317 unsigned FSAbbrev =
3318 (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3319 unsigned Code =
3320 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
3321
Teresa Johnson403a7872015-10-04 14:33:43 +00003322 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003323 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003324 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003325 MaybeEmitOriginalName(*S);
Teresa Johnson403a7872015-10-04 14:33:43 +00003326 }
3327 }
3328
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003329 for (auto GVI : Aliases) {
3330 AliasSummary *AS = cast<AliasSummary>(GVI->summary());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003331 NameVals.push_back(Index.getModuleId(AS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003332 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003333 auto AliaseeOffset = SummaryToOffsetMap[&AS->getAliasee()];
3334 assert(AliaseeOffset);
3335 NameVals.push_back(AliaseeOffset);
3336
3337 // Record the starting offset of this summary entry for use
3338 // in the VST entry. Add the current code size since the
3339 // reader will invoke readRecord after the abbrev id read.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003340 GVI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003341
3342 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003343 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003344 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003345 MaybeEmitOriginalName(*AS);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003346 }
3347
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003348 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003349}
3350
Teresa Johnson37687f32016-04-23 04:30:47 +00003351void ModuleBitcodeWriter::writeIdentificationBlock() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003352 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
Mehdi Amini5d303282015-10-26 18:37:00 +00003353
3354 // Write the "user readable" string identifying the bitcode producer
3355 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3356 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3357 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3358 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003359 auto StringAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson37687f32016-04-23 04:30:47 +00003360 writeStringRecord(bitc::IDENTIFICATION_CODE_STRING,
3361 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
Mehdi Amini5d303282015-10-26 18:37:00 +00003362
3363 // Write the epoch version
3364 Abbv = new BitCodeAbbrev();
3365 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3366 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003367 auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini5d303282015-10-26 18:37:00 +00003368 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003369 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3370 Stream.ExitBlock();
Mehdi Amini5d303282015-10-26 18:37:00 +00003371}
3372
Teresa Johnson37687f32016-04-23 04:30:47 +00003373void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003374 // Emit the module's hash.
3375 // MODULE_CODE_HASH: [5*i32]
3376 SHA1 Hasher;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003377 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&(Buffer)[BlockStartPos],
3378 Buffer.size() - BlockStartPos));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003379 auto Hash = Hasher.result();
3380 SmallVector<uint64_t, 20> Vals;
3381 auto LShift = [&](unsigned char Val, unsigned Amount)
3382 -> uint64_t { return ((uint64_t)Val) << Amount; };
3383 for (int Pos = 0; Pos < 20; Pos += 4) {
3384 uint32_t SubHash = LShift(Hash[Pos + 0], 24);
3385 SubHash |= LShift(Hash[Pos + 1], 16) | LShift(Hash[Pos + 2], 8) |
3386 (unsigned)(unsigned char)Hash[Pos + 3];
3387 Vals.push_back(SubHash);
3388 }
3389
3390 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003391 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003392}
3393
Teresa Johnson37687f32016-04-23 04:30:47 +00003394void BitcodeWriter::write() {
3395 // Emit the file header first.
3396 writeBitcodeHeader();
3397
3398 writeBlocks();
3399}
3400
3401void ModuleBitcodeWriter::writeBlocks() {
3402 writeIdentificationBlock();
3403 writeModule();
3404}
3405
3406void IndexBitcodeWriter::writeBlocks() {
3407 // Index contains only a single outer (module) block.
3408 writeIndex();
3409}
3410
3411void ModuleBitcodeWriter::writeModule() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003412 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3413 size_t BlockStartPos = Buffer.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003414
Jan Wen Voungafaced02012-10-11 20:20:40 +00003415 SmallVector<unsigned, 1> Vals;
3416 unsigned CurVersion = 1;
3417 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003418 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003419
3420 // Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003421 writeBlockInfo();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003422
Bill Wendlingdc095552013-02-10 23:09:32 +00003423 // Emit information about attribute groups.
Teresa Johnson37687f32016-04-23 04:30:47 +00003424 writeAttributeGroupTable();
Bill Wendlingdc095552013-02-10 23:09:32 +00003425
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003426 // Emit information about parameter attributes.
Teresa Johnson37687f32016-04-23 04:30:47 +00003427 writeAttributeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003428
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003429 // Emit information describing all of the types in the module.
Teresa Johnson37687f32016-04-23 04:30:47 +00003430 writeTypeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003431
Teresa Johnson37687f32016-04-23 04:30:47 +00003432 writeComdats();
David Majnemerdad0a642014-06-27 18:19:56 +00003433
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003434 // Emit top-level description of module, including target triple, inline asm,
3435 // descriptors for global variables, and function prototype info.
Teresa Johnson37687f32016-04-23 04:30:47 +00003436 writeModuleInfo();
Devang Patel7428d8a2009-07-22 17:43:22 +00003437
Devang Patele059ba6e2009-07-23 01:07:34 +00003438 // Emit constants.
Teresa Johnson37687f32016-04-23 04:30:47 +00003439 writeModuleConstants();
Devang Patele059ba6e2009-07-23 01:07:34 +00003440
Devang Patel8cca7b42009-08-04 05:01:35 +00003441 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003442 writeModuleMetadata();
Devang Patel8cca7b42009-08-04 05:01:35 +00003443
Devang Patelaf206b82009-09-18 19:26:43 +00003444 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003445 writeModuleMetadataStore();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003446
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003447 // Emit module-level use-lists.
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003448 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003449 writeUseListBlock(nullptr);
Chad Rosierca2567b2011-12-07 21:44:12 +00003450
Teresa Johnson37687f32016-04-23 04:30:47 +00003451 writeOperandBundleTags();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003452
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003453 // Emit function bodies.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003454 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003455 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003456 if (!F->isDeclaration())
Teresa Johnson37687f32016-04-23 04:30:47 +00003457 writeFunction(*F, FunctionToBitcodeIndex);
Teresa Johnson403a7872015-10-04 14:33:43 +00003458
3459 // Need to write after the above call to WriteFunction which populates
3460 // the summary information in the index.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003461 if (Index)
Teresa Johnson37687f32016-04-23 04:30:47 +00003462 writePerModuleGlobalValueSummary();
Teresa Johnsonff642b92015-09-17 20:12:00 +00003463
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003464 writeValueSymbolTable(M.getValueSymbolTable(),
Teresa Johnson37687f32016-04-23 04:30:47 +00003465 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003466
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003467 if (GenerateHash) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003468 writeModuleHash(BlockStartPos);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003469 }
3470
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003471 Stream.ExitBlock();
Chris Lattner702658c2007-05-04 18:26:27 +00003472}
3473
Teresa Johnson37687f32016-04-23 04:30:47 +00003474static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3475 uint32_t &Position) {
3476 support::endian::write32le(&Buffer[Position], Value);
3477 Position += 4;
3478}
3479
3480/// If generating a bc file on darwin, we have to emit a
Chris Lattnera660f4b2008-07-09 05:14:23 +00003481/// header and trailer to make it compatible with the system archiver. To do
3482/// this we emit the following header, and then emit a trailer that pads the
3483/// file out to be a multiple of 16 bytes.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003484///
Chris Lattnera660f4b2008-07-09 05:14:23 +00003485/// struct bc_header {
3486/// uint32_t Magic; // 0x0B17C0DE
3487/// uint32_t Version; // Version, currently always 0.
3488/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3489/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3490/// uint32_t CPUType; // CPU specifier.
3491/// ... potentially more later ...
3492/// };
Teresa Johnson37687f32016-04-23 04:30:47 +00003493static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003494 const Triple &TT) {
Chris Lattnera660f4b2008-07-09 05:14:23 +00003495 unsigned CPUType = ~0U;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003496
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003497 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng545d3602010-02-12 20:39:35 +00003498 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3499 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3500 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattnera660f4b2008-07-09 05:14:23 +00003501 enum {
3502 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3503 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003504 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattnera660f4b2008-07-09 05:14:23 +00003505 DARWIN_CPU_TYPE_POWERPC = 18
3506 };
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003507
Evan Chengcffdcae2011-06-14 01:51:33 +00003508 Triple::ArchType Arch = TT.getArch();
3509 if (Arch == Triple::x86_64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003510 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003511 else if (Arch == Triple::x86)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003512 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chengcffdcae2011-06-14 01:51:33 +00003513 else if (Arch == Triple::ppc)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003514 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chengcffdcae2011-06-14 01:51:33 +00003515 else if (Arch == Triple::ppc64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003516 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003517 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003518 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003519
Chris Lattnera660f4b2008-07-09 05:14:23 +00003520 // Traditional Bitcode starts after header.
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003521 assert(Buffer.size() >= BWH_HeaderSize &&
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003522 "Expected header size to be reserved");
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003523 unsigned BCOffset = BWH_HeaderSize;
3524 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003525
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003526 // Write the magic and version.
3527 unsigned Position = 0;
Teresa Johnson37687f32016-04-23 04:30:47 +00003528 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
3529 writeInt32ToBuffer(0, Buffer, Position); // Version.
3530 writeInt32ToBuffer(BCOffset, Buffer, Position);
3531 writeInt32ToBuffer(BCSize, Buffer, Position);
3532 writeInt32ToBuffer(CPUType, Buffer, Position);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003533
Chris Lattnera660f4b2008-07-09 05:14:23 +00003534 // If the file is not a multiple of 16 bytes, insert dummy padding.
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003535 while (Buffer.size() & 15)
3536 Buffer.push_back(0);
Chris Lattnera660f4b2008-07-09 05:14:23 +00003537}
3538
Teresa Johnson403a7872015-10-04 14:33:43 +00003539/// Helper to write the header common to all bitcode files.
Teresa Johnson37687f32016-04-23 04:30:47 +00003540void BitcodeWriter::writeBitcodeHeader() {
Teresa Johnson403a7872015-10-04 14:33:43 +00003541 // Emit the file header.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003542 Stream.Emit((unsigned)'B', 8);
3543 Stream.Emit((unsigned)'C', 8);
3544 Stream.Emit(0x0, 4);
3545 Stream.Emit(0xC, 4);
3546 Stream.Emit(0xE, 4);
3547 Stream.Emit(0xD, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00003548}
3549
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003550/// WriteBitcodeToFile - Write the specified module to the specified output
3551/// stream.
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +00003552void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
Teresa Johnson403a7872015-10-04 14:33:43 +00003553 bool ShouldPreserveUseListOrder,
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003554 const ModuleSummaryIndex *Index,
3555 bool GenerateHash) {
Michael Ilsemane26658d2012-12-03 21:29:36 +00003556 SmallVector<char, 0> Buffer;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003557 Buffer.reserve(256*1024);
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003558
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003559 // If this is darwin or another generic macho target, reserve space for the
3560 // header.
3561 Triple TT(M->getTargetTriple());
Akira Hatanaka1235d282016-01-23 16:02:10 +00003562 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003563 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003564
3565 // Emit the module into the buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003566 ModuleBitcodeWriter ModuleWriter(M, Buffer, ShouldPreserveUseListOrder, Index,
3567 GenerateHash);
Teresa Johnson37687f32016-04-23 04:30:47 +00003568 ModuleWriter.write();
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003569
Akira Hatanaka1235d282016-01-23 16:02:10 +00003570 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Teresa Johnson37687f32016-04-23 04:30:47 +00003571 emitDarwinBCHeaderAndTrailer(Buffer, TT);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003572
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003573 // Write the generated bitstream to "Out".
3574 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003575}
Teresa Johnson403a7872015-10-04 14:33:43 +00003576
Teresa Johnson37687f32016-04-23 04:30:47 +00003577void IndexBitcodeWriter::writeIndex() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003578 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Teresa Johnson37687f32016-04-23 04:30:47 +00003579
3580 SmallVector<unsigned, 1> Vals;
3581 unsigned CurVersion = 1;
3582 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003583 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Teresa Johnson37687f32016-04-23 04:30:47 +00003584
3585 // If we have a VST, write the VSTOFFSET record placeholder.
3586 writeValueSymbolTableForwardDecl();
3587
3588 // Write the module paths in the combined index.
3589 writeModStrings();
3590
3591 // Write the summary combined index records.
3592 writeCombinedGlobalValueSummary();
3593
3594 // Need a special VST writer for the combined index (we don't have a
3595 // real VST and real values when this is invoked).
3596 writeCombinedValueSymbolTable();
3597
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003598 Stream.ExitBlock();
Teresa Johnson37687f32016-04-23 04:30:47 +00003599}
3600
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003601// Write the specified module summary index to the given raw output stream,
Teresa Johnson403a7872015-10-04 14:33:43 +00003602// where it will be written in a new bitcode block. This is used when
3603// writing the combined index file for ThinLTO.
Teresa Johnson26ab5772016-03-15 00:04:37 +00003604void llvm::WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003605 SmallVector<char, 0> Buffer;
3606 Buffer.reserve(256 * 1024);
3607
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003608 IndexBitcodeWriter IndexWriter(Buffer, Index);
Teresa Johnson37687f32016-04-23 04:30:47 +00003609 IndexWriter.write();
Teresa Johnson403a7872015-10-04 14:33:43 +00003610
3611 Out.write((char *)&Buffer.front(), Buffer.size());
3612}