blob: 9fb5d66a5ac77bc5ebd2d923c186cbbd968f20c4 [file] [log] [blame]
Chris Lattner87351e22007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerc1d10d62007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "ValueEnumerator.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000015#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000017#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner362b4a12007-04-23 01:01:37 +000018#include "llvm/Bitcode/LLVMBitCodes.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000019#include "llvm/Bitcode/ReaderWriter.h"
Sanjoy Dasb513a9f2015-09-24 23:34:52 +000020#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000022#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/InlineAsm.h"
25#include "llvm/IR/Instructions.h"
Teresa Johnson76a1c1d2016-03-11 18:52:24 +000026#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
28#include "llvm/IR/Operator.h"
Duncan P. N. Exon Smith6b6fdc92014-07-25 14:49:26 +000029#include "llvm/IR/UseListOrder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000032#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000033#include "llvm/Support/Program.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000034#include "llvm/Support/SHA1.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000035#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000036#include <cctype>
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000037#include <map>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000038using namespace llvm;
39
Chris Lattner4d925982007-05-04 20:52:02 +000040/// These are manifest constants used by the bitcode writer. They do not need to
41/// be kept in sync with the reader, but need to be consistent within this file.
42enum {
Chris Lattner4d925982007-05-04 20:52:02 +000043 // VALUE_SYMTAB_BLOCK abbrev id's.
44 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerfd1ad102007-05-04 20:58:35 +000045 VST_ENTRY_7_ABBREV,
Chris Lattnere760d6f2007-05-05 01:26:50 +000046 VST_ENTRY_6_ABBREV,
Chris Lattnerda5e5d22007-05-05 07:36:14 +000047 VST_BBENTRY_6_ABBREV,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000048
Chris Lattnerda5e5d22007-05-05 07:36:14 +000049 // CONSTANTS_BLOCK abbrev id's.
50 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
51 CONSTANTS_INTEGER_ABBREV,
52 CONSTANTS_CE_CAST_Abbrev,
Chris Lattnerb80751d2007-05-05 07:44:49 +000053 CONSTANTS_NULL_Abbrev,
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000054
Chris Lattnerb80751d2007-05-05 07:44:49 +000055 // FUNCTION_BLOCK abbrev id's.
Chris Lattnercc6d4c92007-05-06 01:28:01 +000056 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000057 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman0ebd6962009-07-20 21:19:07 +000058 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerc67e6d92007-05-06 02:38:57 +000059 FUNCTION_INST_CAST_ABBREV,
Chris Lattnercc6d4c92007-05-06 01:28:01 +000060 FUNCTION_INST_RET_VOID_ABBREV,
61 FUNCTION_INST_RET_VAL_ABBREV,
David Blaikieb5b5efd2015-02-25 01:08:52 +000062 FUNCTION_INST_UNREACHABLE_ABBREV,
63 FUNCTION_INST_GEP_ABBREV,
Chris Lattner4d925982007-05-04 20:52:02 +000064};
65
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000066/// Abstract class to manage the bitcode writing, subclassed for each bitcode
67/// file type. Owns the BitstreamWriter, and includes the main entry point for
Teresa Johnson37687f32016-04-23 04:30:47 +000068/// writing.
69class BitcodeWriter {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000070protected:
Teresa Johnson37687f32016-04-23 04:30:47 +000071 /// Pointer to the buffer allocated by caller for bitcode writing.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000072 const SmallVectorImpl<char> &Buffer;
Teresa Johnson37687f32016-04-23 04:30:47 +000073
74 /// The stream created and owned by the BitodeWriter.
75 BitstreamWriter Stream;
76
77 /// Saves the offset of the VSTOffset record that must eventually be
78 /// backpatched with the offset of the actual VST.
79 uint64_t VSTOffsetPlaceholder = 0;
80
81public:
82 /// Constructs a BitcodeWriter object, and initializes a BitstreamRecord,
83 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +000084 BitcodeWriter(SmallVectorImpl<char> &Buffer)
85 : Buffer(Buffer), Stream(Buffer) {}
Teresa Johnson37687f32016-04-23 04:30:47 +000086
87 virtual ~BitcodeWriter() = default;
88
89 /// Main entry point to write the bitcode file, which writes the bitcode
90 /// header and will then invoke the virtual writeBlocks() method.
91 void write();
92
93private:
94 /// Derived classes must implement this to write the corresponding blocks for
95 /// that bitcode file type.
96 virtual void writeBlocks() = 0;
97
98protected:
99 bool hasVSTOffsetPlaceholder() { return VSTOffsetPlaceholder != 0; }
Teresa Johnson37687f32016-04-23 04:30:47 +0000100 void writeValueSymbolTableForwardDecl();
101 void writeBitcodeHeader();
102};
103
104/// Class to manage the bitcode writing for a module.
105class ModuleBitcodeWriter : public BitcodeWriter {
106 /// The Module to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000107 const Module &M;
Teresa Johnson37687f32016-04-23 04:30:47 +0000108
109 /// Enumerates ids for all values in the module.
110 ValueEnumerator VE;
111
112 /// Optional per-module index to write for ThinLTO.
113 const ModuleSummaryIndex *Index;
114
115 /// True if a module hash record should be written.
116 bool GenerateHash;
117
118 /// The start bit of the module block, for use in generating a module hash
119 uint64_t BitcodeStartBit = 0;
120
121public:
122 /// Constructs a ModuleBitcodeWriter object for the given Module,
123 /// writing to the provided \p Buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000124 ModuleBitcodeWriter(const Module *M, SmallVectorImpl<char> &Buffer,
Teresa Johnson37687f32016-04-23 04:30:47 +0000125 bool ShouldPreserveUseListOrder,
126 const ModuleSummaryIndex *Index, bool GenerateHash)
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000127 : BitcodeWriter(Buffer), M(*M), VE(*M, ShouldPreserveUseListOrder),
Teresa Johnson37687f32016-04-23 04:30:47 +0000128 Index(Index), GenerateHash(GenerateHash) {
129 // Save the start bit of the actual bitcode, in case there is space
130 // saved at the start for the darwin header above. The reader stream
131 // will start at the bitcode, and we need the offset of the VST
132 // to line up.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000133 BitcodeStartBit = Stream.GetCurrentBitNo();
Teresa Johnson37687f32016-04-23 04:30:47 +0000134 }
135
136private:
137 /// Main entry point for writing a module to bitcode, invoked by
138 /// BitcodeWriter::write() after it writes the header.
139 void writeBlocks() override;
140
141 /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
142 /// current llvm version, and a record for the epoch number.
143 void writeIdentificationBlock();
144
145 /// Emit the current module to the bitstream.
146 void writeModule();
147
148 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
149
150 void writeStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse);
151 void writeAttributeGroupTable();
152 void writeAttributeTable();
153 void writeTypeTable();
154 void writeComdats();
155 void writeModuleInfo();
156 void writeValueAsMetadata(const ValueAsMetadata *MD,
157 SmallVectorImpl<uint64_t> &Record);
158 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
159 unsigned Abbrev);
160 unsigned createDILocationAbbrev();
161 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
162 unsigned &Abbrev);
163 unsigned createGenericDINodeAbbrev();
164 void writeGenericDINode(const GenericDINode *N,
165 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
166 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
167 unsigned Abbrev);
168 void writeDIEnumerator(const DIEnumerator *N,
169 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
170 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
171 unsigned Abbrev);
172 void writeDIDerivedType(const DIDerivedType *N,
173 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
174 void writeDICompositeType(const DICompositeType *N,
175 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
176 void writeDISubroutineType(const DISubroutineType *N,
177 SmallVectorImpl<uint64_t> &Record,
178 unsigned Abbrev);
179 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
180 unsigned Abbrev);
181 void writeDICompileUnit(const DICompileUnit *N,
182 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
183 void writeDISubprogram(const DISubprogram *N,
184 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
185 void writeDILexicalBlock(const DILexicalBlock *N,
186 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
187 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
188 SmallVectorImpl<uint64_t> &Record,
189 unsigned Abbrev);
190 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
191 unsigned Abbrev);
192 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
193 unsigned Abbrev);
194 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
195 unsigned Abbrev);
196 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
197 unsigned Abbrev);
198 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
199 SmallVectorImpl<uint64_t> &Record,
200 unsigned Abbrev);
201 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
202 SmallVectorImpl<uint64_t> &Record,
203 unsigned Abbrev);
204 void writeDIGlobalVariable(const DIGlobalVariable *N,
205 SmallVectorImpl<uint64_t> &Record,
206 unsigned Abbrev);
207 void writeDILocalVariable(const DILocalVariable *N,
208 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
209 void writeDIExpression(const DIExpression *N,
210 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
211 void writeDIObjCProperty(const DIObjCProperty *N,
212 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
213 void writeDIImportedEntity(const DIImportedEntity *N,
214 SmallVectorImpl<uint64_t> &Record,
215 unsigned Abbrev);
216 unsigned createNamedMetadataAbbrev();
217 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
218 unsigned createMetadataStringsAbbrev();
219 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
220 SmallVectorImpl<uint64_t> &Record);
221 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
222 SmallVectorImpl<uint64_t> &Record);
223 void writeModuleMetadata();
224 void writeFunctionMetadata(const Function &F);
225 void writeMetadataAttachment(const Function &F);
226 void writeModuleMetadataStore();
227 void writeOperandBundleTags();
228 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
229 void writeModuleConstants();
230 bool pushValueAndType(const Value *V, unsigned InstID,
231 SmallVectorImpl<unsigned> &Vals);
232 void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
233 void pushValue(const Value *V, unsigned InstID,
234 SmallVectorImpl<unsigned> &Vals);
235 void pushValueSigned(const Value *V, unsigned InstID,
236 SmallVectorImpl<uint64_t> &Vals);
237 void writeInstruction(const Instruction &I, unsigned InstID,
238 SmallVectorImpl<unsigned> &Vals);
239 void writeValueSymbolTable(
240 const ValueSymbolTable &VST, bool IsModuleLevel = false,
241 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex = nullptr);
242 void writeUseList(UseListOrder &&Order);
243 void writeUseListBlock(const Function *F);
244 void
245 writeFunction(const Function &F,
246 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
247 void writeBlockInfo();
248 void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
Teresa Johnson28e457b2016-04-24 14:57:11 +0000249 GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +0000250 unsigned ValueID,
251 unsigned FSCallsAbbrev,
252 unsigned FSCallsProfileAbbrev,
253 const Function &F);
254 void writeModuleLevelReferences(const GlobalVariable &V,
255 SmallVector<uint64_t, 64> &NameVals,
256 unsigned FSModRefsAbbrev);
257 void writePerModuleGlobalValueSummary();
258 void writeModuleHash(size_t BlockStartPos);
259};
260
261/// Class to manage the bitcode writing for a combined index.
262class IndexBitcodeWriter : public BitcodeWriter {
263 /// The combined index to write to bitcode.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000264 const ModuleSummaryIndex &Index;
Teresa Johnson37687f32016-04-23 04:30:47 +0000265
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 }
Teresa Johnson37687f32016-04-23 04:30:47 +0000311 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
312};
313
314static unsigned getEncodedCastOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000315 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000316 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000317 case Instruction::Trunc : return bitc::CAST_TRUNC;
318 case Instruction::ZExt : return bitc::CAST_ZEXT;
319 case Instruction::SExt : return bitc::CAST_SEXT;
320 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
321 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
322 case Instruction::UIToFP : return bitc::CAST_UITOFP;
323 case Instruction::SIToFP : return bitc::CAST_SITOFP;
324 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
325 case Instruction::FPExt : return bitc::CAST_FPEXT;
326 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
327 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
328 case Instruction::BitCast : return bitc::CAST_BITCAST;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000329 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000330 }
331}
332
Teresa Johnson37687f32016-04-23 04:30:47 +0000333static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000334 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000335 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmana5b96452009-06-04 22:49:04 +0000336 case Instruction::Add:
337 case Instruction::FAdd: return bitc::BINOP_ADD;
338 case Instruction::Sub:
339 case Instruction::FSub: return bitc::BINOP_SUB;
340 case Instruction::Mul:
341 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000342 case Instruction::UDiv: return bitc::BINOP_UDIV;
343 case Instruction::FDiv:
344 case Instruction::SDiv: return bitc::BINOP_SDIV;
345 case Instruction::URem: return bitc::BINOP_UREM;
346 case Instruction::FRem:
347 case Instruction::SRem: return bitc::BINOP_SREM;
348 case Instruction::Shl: return bitc::BINOP_SHL;
349 case Instruction::LShr: return bitc::BINOP_LSHR;
350 case Instruction::AShr: return bitc::BINOP_ASHR;
351 case Instruction::And: return bitc::BINOP_AND;
352 case Instruction::Or: return bitc::BINOP_OR;
353 case Instruction::Xor: return bitc::BINOP_XOR;
354 }
355}
356
Teresa Johnson37687f32016-04-23 04:30:47 +0000357static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000358 switch (Op) {
359 default: llvm_unreachable("Unknown RMW operation!");
360 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
361 case AtomicRMWInst::Add: return bitc::RMW_ADD;
362 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
363 case AtomicRMWInst::And: return bitc::RMW_AND;
364 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
365 case AtomicRMWInst::Or: return bitc::RMW_OR;
366 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
367 case AtomicRMWInst::Max: return bitc::RMW_MAX;
368 case AtomicRMWInst::Min: return bitc::RMW_MIN;
369 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
370 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
371 }
372}
373
Teresa Johnson37687f32016-04-23 04:30:47 +0000374static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000375 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +0000376 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
377 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
378 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
379 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
380 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
381 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
382 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
Eli Friedmanfee02c62011-07-25 23:16:38 +0000383 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000384 llvm_unreachable("Invalid ordering");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000385}
386
Teresa Johnson37687f32016-04-23 04:30:47 +0000387static unsigned getEncodedSynchScope(SynchronizationScope SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000388 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000389 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
390 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
391 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000392 llvm_unreachable("Invalid synch scope");
Eli Friedmanfee02c62011-07-25 23:16:38 +0000393}
394
Teresa Johnson37687f32016-04-23 04:30:47 +0000395void ModuleBitcodeWriter::writeStringRecord(unsigned Code, StringRef Str,
396 unsigned AbbrevToUse) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000397 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000398
Chris Lattnere14cb882007-05-04 19:11:41 +0000399 // Code: [strchar x N]
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000400 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
401 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
402 AbbrevToUse = 0;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000403 Vals.push_back(Str[i]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000404 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000405
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000406 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000407 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000408}
409
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000410static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
411 switch (Kind) {
412 case Attribute::Alignment:
413 return bitc::ATTR_KIND_ALIGNMENT;
George Burgess IV278199f2016-04-12 01:05:35 +0000414 case Attribute::AllocSize:
415 return bitc::ATTR_KIND_ALLOC_SIZE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000416 case Attribute::AlwaysInline:
417 return bitc::ATTR_KIND_ALWAYS_INLINE;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000418 case Attribute::ArgMemOnly:
419 return bitc::ATTR_KIND_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000420 case Attribute::Builtin:
421 return bitc::ATTR_KIND_BUILTIN;
422 case Attribute::ByVal:
423 return bitc::ATTR_KIND_BY_VAL;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000424 case Attribute::Convergent:
425 return bitc::ATTR_KIND_CONVERGENT;
Reid Klecknera534a382013-12-19 02:14:12 +0000426 case Attribute::InAlloca:
427 return bitc::ATTR_KIND_IN_ALLOCA;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000428 case Attribute::Cold:
429 return bitc::ATTR_KIND_COLD;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000430 case Attribute::InaccessibleMemOnly:
431 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
432 case Attribute::InaccessibleMemOrArgMemOnly:
433 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000434 case Attribute::InlineHint:
435 return bitc::ATTR_KIND_INLINE_HINT;
436 case Attribute::InReg:
437 return bitc::ATTR_KIND_IN_REG;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000438 case Attribute::JumpTable:
439 return bitc::ATTR_KIND_JUMP_TABLE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000440 case Attribute::MinSize:
441 return bitc::ATTR_KIND_MIN_SIZE;
442 case Attribute::Naked:
443 return bitc::ATTR_KIND_NAKED;
444 case Attribute::Nest:
445 return bitc::ATTR_KIND_NEST;
446 case Attribute::NoAlias:
447 return bitc::ATTR_KIND_NO_ALIAS;
448 case Attribute::NoBuiltin:
449 return bitc::ATTR_KIND_NO_BUILTIN;
450 case Attribute::NoCapture:
451 return bitc::ATTR_KIND_NO_CAPTURE;
452 case Attribute::NoDuplicate:
453 return bitc::ATTR_KIND_NO_DUPLICATE;
454 case Attribute::NoImplicitFloat:
455 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
456 case Attribute::NoInline:
457 return bitc::ATTR_KIND_NO_INLINE;
James Molloye6f87ca2015-11-06 10:32:53 +0000458 case Attribute::NoRecurse:
459 return bitc::ATTR_KIND_NO_RECURSE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000460 case Attribute::NonLazyBind:
461 return bitc::ATTR_KIND_NON_LAZY_BIND;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000462 case Attribute::NonNull:
463 return bitc::ATTR_KIND_NON_NULL;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000464 case Attribute::Dereferenceable:
465 return bitc::ATTR_KIND_DEREFERENCEABLE;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000466 case Attribute::DereferenceableOrNull:
467 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000468 case Attribute::NoRedZone:
469 return bitc::ATTR_KIND_NO_RED_ZONE;
470 case Attribute::NoReturn:
471 return bitc::ATTR_KIND_NO_RETURN;
472 case Attribute::NoUnwind:
473 return bitc::ATTR_KIND_NO_UNWIND;
474 case Attribute::OptimizeForSize:
475 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000476 case Attribute::OptimizeNone:
477 return bitc::ATTR_KIND_OPTIMIZE_NONE;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000478 case Attribute::ReadNone:
479 return bitc::ATTR_KIND_READ_NONE;
480 case Attribute::ReadOnly:
481 return bitc::ATTR_KIND_READ_ONLY;
482 case Attribute::Returned:
483 return bitc::ATTR_KIND_RETURNED;
484 case Attribute::ReturnsTwice:
485 return bitc::ATTR_KIND_RETURNS_TWICE;
486 case Attribute::SExt:
487 return bitc::ATTR_KIND_S_EXT;
488 case Attribute::StackAlignment:
489 return bitc::ATTR_KIND_STACK_ALIGNMENT;
490 case Attribute::StackProtect:
491 return bitc::ATTR_KIND_STACK_PROTECT;
492 case Attribute::StackProtectReq:
493 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
494 case Attribute::StackProtectStrong:
495 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000496 case Attribute::SafeStack:
497 return bitc::ATTR_KIND_SAFESTACK;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000498 case Attribute::StructRet:
499 return bitc::ATTR_KIND_STRUCT_RET;
500 case Attribute::SanitizeAddress:
501 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
502 case Attribute::SanitizeThread:
503 return bitc::ATTR_KIND_SANITIZE_THREAD;
504 case Attribute::SanitizeMemory:
505 return bitc::ATTR_KIND_SANITIZE_MEMORY;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000506 case Attribute::SwiftError:
507 return bitc::ATTR_KIND_SWIFT_ERROR;
Manman Renf46262e2016-03-29 17:37:21 +0000508 case Attribute::SwiftSelf:
509 return bitc::ATTR_KIND_SWIFT_SELF;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000510 case Attribute::UWTable:
511 return bitc::ATTR_KIND_UW_TABLE;
512 case Attribute::ZExt:
513 return bitc::ATTR_KIND_Z_EXT;
514 case Attribute::EndAttrKinds:
515 llvm_unreachable("Can not encode end-attribute kinds marker.");
516 case Attribute::None:
517 llvm_unreachable("Can not encode none-attribute.");
518 }
519
520 llvm_unreachable("Trying to encode unknown attribute");
521}
522
Teresa Johnson37687f32016-04-23 04:30:47 +0000523void ModuleBitcodeWriter::writeAttributeGroupTable() {
Bill Wendling92ed7002013-02-11 22:33:26 +0000524 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
525 if (AttrGrps.empty()) return;
Bill Wendlingdc095552013-02-10 23:09:32 +0000526
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000527 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
Bill Wendlingdc095552013-02-10 23:09:32 +0000528
529 SmallVector<uint64_t, 64> Record;
Bill Wendling92ed7002013-02-11 22:33:26 +0000530 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
531 AttributeSet AS = AttrGrps[i];
Bill Wendlingdc095552013-02-10 23:09:32 +0000532 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
533 AttributeSet A = AS.getSlotAttributes(i);
534
Bill Wendling92ed7002013-02-11 22:33:26 +0000535 Record.push_back(VE.getAttributeGroupID(A));
Bill Wendlingdc095552013-02-10 23:09:32 +0000536 Record.push_back(AS.getSlotIndex(i));
537
538 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
539 I != E; ++I) {
540 Attribute Attr = *I;
541 if (Attr.isEnumAttribute()) {
542 Record.push_back(0);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000543 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Hal Finkele15442c2014-07-18 06:51:55 +0000544 } else if (Attr.isIntAttribute()) {
Bill Wendlingdc095552013-02-10 23:09:32 +0000545 Record.push_back(1);
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000546 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
Bill Wendlingdc095552013-02-10 23:09:32 +0000547 Record.push_back(Attr.getValueAsInt());
548 } else {
549 StringRef Kind = Attr.getKindAsString();
550 StringRef Val = Attr.getValueAsString();
551
552 Record.push_back(Val.empty() ? 3 : 4);
553 Record.append(Kind.begin(), Kind.end());
554 Record.push_back(0);
555 if (!Val.empty()) {
556 Record.append(Val.begin(), Val.end());
557 Record.push_back(0);
558 }
559 }
560 }
561
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000562 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
Bill Wendlingdc095552013-02-10 23:09:32 +0000563 Record.clear();
564 }
565 }
566
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000567 Stream.ExitBlock();
Bill Wendlingdc095552013-02-10 23:09:32 +0000568}
569
Teresa Johnson37687f32016-04-23 04:30:47 +0000570void ModuleBitcodeWriter::writeAttributeTable() {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000571 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000572 if (Attrs.empty()) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000573
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000574 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000575
576 SmallVector<uint64_t, 64> Record;
577 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000578 const AttributeSet &A = Attrs[i];
Bill Wendling0dc08912013-02-12 08:13:50 +0000579 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
580 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000581
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000582 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
Chris Lattnere72bf9f2007-05-04 02:59:04 +0000583 Record.clear();
584 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000585
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000586 Stream.ExitBlock();
Chris Lattnere2f98ef2007-05-04 00:44:52 +0000587}
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000588
589/// WriteTypeTable - Write out the type table for a module.
Teresa Johnson37687f32016-04-23 04:30:47 +0000590void ModuleBitcodeWriter::writeTypeTable() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000591 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000592
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000593 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000594 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000595
David Blaikie7b028102015-02-25 00:51:52 +0000596 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
Chad Rosier9646c0d2011-12-08 00:38:45 +0000597
Chris Lattnerccee7062007-05-05 06:30:12 +0000598 // Abbrev for TYPE_CODE_POINTER.
599 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
600 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000601 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
Christopher Lamb25f50762007-12-12 08:44:39 +0000602 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000603 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000604
Chris Lattnerccee7062007-05-05 06:30:12 +0000605 // Abbrev for TYPE_CODE_FUNCTION.
606 Abbv = new BitCodeAbbrev();
607 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
608 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnerccee7062007-05-05 06:30:12 +0000609 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000610 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
611
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000612 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000613
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000614 // Abbrev for TYPE_CODE_STRUCT_ANON.
Chris Lattnerccee7062007-05-05 06:30:12 +0000615 Abbv = new BitCodeAbbrev();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000616 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
Chris Lattnerccee7062007-05-05 06:30:12 +0000617 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
618 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000619 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
620
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000621 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000622
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000623 // Abbrev for TYPE_CODE_STRUCT_NAME.
624 Abbv = new BitCodeAbbrev();
625 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
626 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
627 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000628 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000629
630 // Abbrev for TYPE_CODE_STRUCT_NAMED.
631 Abbv = new BitCodeAbbrev();
632 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
633 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
634 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chad Rosier9646c0d2011-12-08 00:38:45 +0000635 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
636
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000637 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000638
Chris Lattnerccee7062007-05-05 06:30:12 +0000639 // Abbrev for TYPE_CODE_ARRAY.
640 Abbv = new BitCodeAbbrev();
641 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
642 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
Chad Rosier9646c0d2011-12-08 00:38:45 +0000643 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
644
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000645 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000646
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000647 // Emit an entry count so the reader can reserve space.
648 TypeVals.push_back(TypeList.size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000649 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000650 TypeVals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000651
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000652 // Loop over all of the types, emitting each in turn.
653 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000654 Type *T = TypeList[i];
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000655 int AbbrevToUse = 0;
656 unsigned Code = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000657
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000658 switch (T->getTypeID()) {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000659 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
660 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
661 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
662 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
663 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
664 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000665 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000666 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
667 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
668 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
David Majnemerb611e3f2015-08-14 05:09:07 +0000669 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000670 case Type::IntegerTyID:
671 // INTEGER: [width]
672 Code = bitc::TYPE_CODE_INTEGER;
673 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
674 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000675 case Type::PointerTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000676 PointerType *PTy = cast<PointerType>(T);
Christopher Lamb25f50762007-12-12 08:44:39 +0000677 // POINTER: [pointee type, address space]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000678 Code = bitc::TYPE_CODE_POINTER;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000679 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lamb25f50762007-12-12 08:44:39 +0000680 unsigned AddressSpace = PTy->getAddressSpace();
681 TypeVals.push_back(AddressSpace);
682 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000683 break;
Duncan Sandsf41217d2007-12-11 12:20:47 +0000684 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000685 case Type::FunctionTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000686 FunctionType *FT = cast<FunctionType>(T);
Chad Rosier95898722011-11-03 00:14:01 +0000687 // FUNCTION: [isvararg, retty, paramty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000688 Code = bitc::TYPE_CODE_FUNCTION;
689 TypeVals.push_back(FT->isVarArg());
690 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000691 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
692 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerccee7062007-05-05 06:30:12 +0000693 AbbrevToUse = FunctionAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000694 break;
695 }
696 case Type::StructTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000697 StructType *ST = cast<StructType>(T);
Chris Lattnerccee7062007-05-05 06:30:12 +0000698 // STRUCT: [ispacked, eltty x N]
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000699 TypeVals.push_back(ST->isPacked());
Chris Lattnere14cb882007-05-04 19:11:41 +0000700 // Output all of the element types.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000701 for (StructType::element_iterator I = ST->element_begin(),
702 E = ST->element_end(); I != E; ++I)
703 TypeVals.push_back(VE.getTypeID(*I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000704
Chris Lattner335d3992011-08-12 18:06:37 +0000705 if (ST->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000706 Code = bitc::TYPE_CODE_STRUCT_ANON;
707 AbbrevToUse = StructAnonAbbrev;
708 } else {
709 if (ST->isOpaque()) {
710 Code = bitc::TYPE_CODE_OPAQUE;
711 } else {
712 Code = bitc::TYPE_CODE_STRUCT_NAMED;
713 AbbrevToUse = StructNamedAbbrev;
714 }
715
716 // Emit the name if it is present.
717 if (!ST->getName().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000718 writeStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
719 StructNameAbbrev);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000720 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000721 break;
722 }
723 case Type::ArrayTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000724 ArrayType *AT = cast<ArrayType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000725 // ARRAY: [numelts, eltty]
726 Code = bitc::TYPE_CODE_ARRAY;
727 TypeVals.push_back(AT->getNumElements());
728 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerccee7062007-05-05 06:30:12 +0000729 AbbrevToUse = ArrayAbbrev;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000730 break;
731 }
732 case Type::VectorTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000733 VectorType *VT = cast<VectorType>(T);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000734 // VECTOR [numelts, eltty]
735 Code = bitc::TYPE_CODE_VECTOR;
736 TypeVals.push_back(VT->getNumElements());
737 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
738 break;
739 }
740 }
741
742 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000743 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000744 TypeVals.clear();
745 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000746
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000747 Stream.ExitBlock();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000748}
749
Teresa Johnson5e22e442016-02-06 16:07:35 +0000750static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
751 switch (Linkage) {
Rafael Espindola261d25b2015-01-08 16:25:01 +0000752 case GlobalValue::ExternalLinkage:
753 return 0;
754 case GlobalValue::WeakAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000755 return 16;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000756 case GlobalValue::AppendingLinkage:
757 return 2;
758 case GlobalValue::InternalLinkage:
759 return 3;
760 case GlobalValue::LinkOnceAnyLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000761 return 18;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000762 case GlobalValue::ExternalWeakLinkage:
763 return 7;
764 case GlobalValue::CommonLinkage:
765 return 8;
766 case GlobalValue::PrivateLinkage:
767 return 9;
768 case GlobalValue::WeakODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000769 return 17;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000770 case GlobalValue::LinkOnceODRLinkage:
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000771 return 19;
Rafael Espindola261d25b2015-01-08 16:25:01 +0000772 case GlobalValue::AvailableExternallyLinkage:
773 return 12;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000774 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000775 llvm_unreachable("Invalid linkage");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000776}
777
Teresa Johnson5e22e442016-02-06 16:07:35 +0000778static unsigned getEncodedLinkage(const GlobalValue &GV) {
779 return getEncodedLinkage(GV.getLinkage());
780}
781
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000782// Decode the flags for GlobalValue in the summary
783static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
784 uint64_t RawFlags = 0;
Mehdi Aminica2c54e2016-04-24 05:31:43 +0000785
786 RawFlags |= Flags.HasSection; // bool
787
788 // Linkage don't need to be remapped at that time for the summary. Any future
789 // change to the getEncodedLinkage() function will need to be taken into
790 // account here as well.
791 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
792
Mehdi Aminic3ed48c2016-04-24 03:18:18 +0000793 return RawFlags;
794}
795
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000796static unsigned getEncodedVisibility(const GlobalValue &GV) {
797 switch (GV.getVisibility()) {
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000798 case GlobalValue::DefaultVisibility: return 0;
799 case GlobalValue::HiddenVisibility: return 1;
800 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000801 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000802 llvm_unreachable("Invalid visibility");
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000803}
804
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000805static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
806 switch (GV.getDLLStorageClass()) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000807 case GlobalValue::DefaultStorageClass: return 0;
808 case GlobalValue::DLLImportStorageClass: return 1;
809 case GlobalValue::DLLExportStorageClass: return 2;
810 }
811 llvm_unreachable("Invalid DLL storage class");
812}
813
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000814static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000815 switch (GV.getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000816 case GlobalVariable::NotThreadLocal: return 0;
817 case GlobalVariable::GeneralDynamicTLSModel: return 1;
818 case GlobalVariable::LocalDynamicTLSModel: return 2;
819 case GlobalVariable::InitialExecTLSModel: return 3;
820 case GlobalVariable::LocalExecTLSModel: return 4;
821 }
822 llvm_unreachable("Invalid TLS model");
823}
824
David Majnemerdad0a642014-06-27 18:19:56 +0000825static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
826 switch (C.getSelectionKind()) {
827 case Comdat::Any:
828 return bitc::COMDAT_SELECTION_KIND_ANY;
829 case Comdat::ExactMatch:
830 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
831 case Comdat::Largest:
832 return bitc::COMDAT_SELECTION_KIND_LARGEST;
833 case Comdat::NoDuplicates:
834 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
835 case Comdat::SameSize:
836 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
837 }
838 llvm_unreachable("Invalid selection kind");
839}
840
Teresa Johnson37687f32016-04-23 04:30:47 +0000841void ModuleBitcodeWriter::writeComdats() {
David Majnemer90a021f2016-03-13 08:01:03 +0000842 SmallVector<unsigned, 64> Vals;
David Majnemerdad0a642014-06-27 18:19:56 +0000843 for (const Comdat *C : VE.getComdats()) {
844 // COMDAT: [selection_kind, name]
845 Vals.push_back(getEncodedComdatSelectionKind(*C));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +0000846 size_t Size = C->getName().size();
David Majnemer90a021f2016-03-13 08:01:03 +0000847 assert(isUInt<32>(Size));
Rafael Espindola4e74d3b2015-01-14 18:25:45 +0000848 Vals.push_back(Size);
David Majnemerdad0a642014-06-27 18:19:56 +0000849 for (char Chr : C->getName())
850 Vals.push_back((unsigned char)Chr);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000851 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
David Majnemerdad0a642014-06-27 18:19:56 +0000852 Vals.clear();
853 }
854}
855
Teresa Johnsonff642b92015-09-17 20:12:00 +0000856/// Write a record that will eventually hold the word offset of the
857/// module-level VST. For now the offset is 0, which will be backpatched
Teresa Johnson37687f32016-04-23 04:30:47 +0000858/// after the real VST is written. Saves the bit offset to backpatch.
859void BitcodeWriter::writeValueSymbolTableForwardDecl() {
Teresa Johnsonff642b92015-09-17 20:12:00 +0000860 // Write a placeholder value in for the offset of the real VST,
861 // which is written after the function blocks so that it can include
862 // the offset of each function. The placeholder offset will be
863 // updated when the real VST is written.
864 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
865 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
866 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
867 // hold the real VST offset. Must use fixed instead of VBR as we don't
868 // know how many VBR chunks to reserve ahead of time.
869 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000870 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +0000871
872 // Emit the placeholder
873 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000874 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
Teresa Johnsonff642b92015-09-17 20:12:00 +0000875
Teresa Johnson37687f32016-04-23 04:30:47 +0000876 // Compute and save the bit offset to the placeholder, which will be
Teresa Johnsonff642b92015-09-17 20:12:00 +0000877 // patched when the real VST is written. We can simply subtract the 32-bit
878 // fixed size from the current bit number to get the location to backpatch.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000879 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
Teresa Johnsonff642b92015-09-17 20:12:00 +0000880}
881
Teresa Johnsone1164de2016-02-10 21:55:02 +0000882enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
883
884/// Determine the encoding to use for the given string name and length.
885static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
886 bool isChar6 = true;
887 for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
888 if (isChar6)
889 isChar6 = BitCodeAbbrevOp::isChar6(*C);
890 if ((unsigned char)*C & 128)
891 // don't bother scanning the rest.
892 return SE_Fixed8;
893 }
894 if (isChar6)
895 return SE_Char6;
896 else
897 return SE_Fixed7;
898}
899
Teresa Johnsonff642b92015-09-17 20:12:00 +0000900/// Emit top-level description of module, including target triple, inline asm,
901/// descriptors for global variables, and function prototype info.
902/// Returns the bit offset to backpatch with the location of the real VST.
Teresa Johnson37687f32016-04-23 04:30:47 +0000903void ModuleBitcodeWriter::writeModuleInfo() {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000904 // Emit various pieces of data attached to a module.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000905 if (!M.getTargetTriple().empty())
906 writeStringRecord(bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
Teresa Johnson37687f32016-04-23 04:30:47 +0000907 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000908 const std::string &DL = M.getDataLayoutStr();
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000909 if (!DL.empty())
Teresa Johnson37687f32016-04-23 04:30:47 +0000910 writeStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000911 if (!M.getModuleInlineAsm().empty())
912 writeStringRecord(bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
Teresa Johnson37687f32016-04-23 04:30:47 +0000913 0 /*TODO*/);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000914
Gordon Henriksend930f912008-08-17 18:44:35 +0000915 // Emit information about sections and GC, computing how many there are. Also
916 // compute the maximum alignment value.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000917 std::map<std::string, unsigned> SectionMap;
Gordon Henriksend930f912008-08-17 18:44:35 +0000918 std::map<std::string, unsigned> GCMap;
Chris Lattner4b00d922007-04-23 16:04:05 +0000919 unsigned MaxAlignment = 0;
Chris Lattnerb5491372007-04-23 18:58:34 +0000920 unsigned MaxGlobalType = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000921 for (const GlobalValue &GV : M.globals()) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000922 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
David Blaikie1a848da2015-04-27 19:58:56 +0000923 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000924 if (GV.hasSection()) {
Chad Rosier75ec09c2011-08-12 16:45:18 +0000925 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000926 unsigned &Entry = SectionMap[GV.getSection()];
Chad Rosier75ec09c2011-08-12 16:45:18 +0000927 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000928 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
929 0 /*TODO*/);
Chad Rosier75ec09c2011-08-12 16:45:18 +0000930 Entry = SectionMap.size();
931 }
932 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000933 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000934 for (const Function &F : M) {
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000935 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
936 if (F.hasSection()) {
Gordon Henriksen71183b62007-12-10 03:18:06 +0000937 // Give section names unique ID's.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000938 unsigned &Entry = SectionMap[F.getSection()];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000939 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000940 writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
941 0 /*TODO*/);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000942 Entry = SectionMap.size();
943 }
944 }
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000945 if (F.hasGC()) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000946 // Same for GC names.
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000947 unsigned &Entry = GCMap[F.getGC()];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000948 if (!Entry) {
Teresa Johnson37687f32016-04-23 04:30:47 +0000949 writeStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 0 /*TODO*/);
Gordon Henriksend930f912008-08-17 18:44:35 +0000950 Entry = GCMap.size();
Gordon Henriksen71183b62007-12-10 03:18:06 +0000951 }
952 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000953 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000954
Chris Lattner4b00d922007-04-23 16:04:05 +0000955 // Emit abbrev for globals, now that we know # sections and max alignment.
956 unsigned SimpleGVarAbbrev = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000957 if (!M.global_empty()) {
Chris Lattner4b00d922007-04-23 16:04:05 +0000958 // Add an abbrev for common globals with no visibility or thread localness.
959 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
960 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2eae59f2007-05-04 20:34:50 +0000961 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +0000962 Log2_32_Ceil(MaxGlobalType+1)));
David Blaikie1a848da2015-04-27 19:58:56 +0000963 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
964 //| explicitType << 1
965 //| constant
966 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
967 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
968 if (MaxAlignment == 0) // Alignment.
Chris Lattner4b00d922007-04-23 16:04:05 +0000969 Abbv->Add(BitCodeAbbrevOp(0));
970 else {
971 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2eae59f2007-05-04 20:34:50 +0000972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerb5491372007-04-23 18:58:34 +0000973 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +0000974 }
975 if (SectionMap.empty()) // Section.
976 Abbv->Add(BitCodeAbbrevOp(0));
977 else
Chris Lattner2eae59f2007-05-04 20:34:50 +0000978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner1e50c292007-04-24 03:29:47 +0000979 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner4b00d922007-04-23 16:04:05 +0000980 // Don't bother emitting vis + thread local.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000981 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner4b00d922007-04-23 16:04:05 +0000982 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000983
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000984 // Emit the global variable information.
985 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +0000986 for (const GlobalVariable &GV : M.globals()) {
Chris Lattner4b00d922007-04-23 16:04:05 +0000987 unsigned AbbrevToUse = 0;
988
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000989 // GLOBALVAR: [type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +0000990 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +0000991 // unnamed_addr, externally_initialized, dllstorageclass,
992 // comdat]
David Blaikie1a848da2015-04-27 19:58:56 +0000993 Vals.push_back(VE.getTypeID(GV.getValueType()));
994 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000995 Vals.push_back(GV.isDeclaration() ? 0 :
996 (VE.getValueID(GV.getInitializer()) + 1));
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000997 Vals.push_back(getEncodedLinkage(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +0000998 Vals.push_back(Log2_32(GV.getAlignment())+1);
999 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
1000 if (GV.isThreadLocal() ||
1001 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1002 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
David Majnemerdad0a642014-06-27 18:19:56 +00001003 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1004 GV.hasComdat()) {
Chris Lattner4b00d922007-04-23 16:04:05 +00001005 Vals.push_back(getEncodedVisibility(GV));
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001006 Vals.push_back(getEncodedThreadLocalMode(GV));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001007 Vals.push_back(GV.hasUnnamedAddr());
1008 Vals.push_back(GV.isExternallyInitialized());
Nico Rieck7157bb72014-01-14 15:22:47 +00001009 Vals.push_back(getEncodedDLLStorageClass(GV));
David Majnemerdad0a642014-06-27 18:19:56 +00001010 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
Chris Lattner4b00d922007-04-23 16:04:05 +00001011 } else {
1012 AbbrevToUse = SimpleGVarAbbrev;
1013 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001014
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001015 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001016 Vals.clear();
1017 }
1018
1019 // Emit the function proto information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001020 for (const Function &F : M) {
Chad Rosier42ee1522011-12-07 23:57:55 +00001021 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001022 // section, visibility, gc, unnamed_addr, prologuedata,
David Majnemer7fddecc2015-06-17 20:52:32 +00001023 // dllstorageclass, comdat, prefixdata, personalityfn]
David Blaikie561a1572015-04-17 16:28:26 +00001024 Vals.push_back(VE.getTypeID(F.getFunctionType()));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001025 Vals.push_back(F.getCallingConv());
1026 Vals.push_back(F.isDeclaration());
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001027 Vals.push_back(getEncodedLinkage(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001028 Vals.push_back(VE.getAttributeID(F.getAttributes()));
1029 Vals.push_back(Log2_32(F.getAlignment())+1);
1030 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001031 Vals.push_back(getEncodedVisibility(F));
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001032 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1033 Vals.push_back(F.hasUnnamedAddr());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001034 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1035 : 0);
Nico Rieck7157bb72014-01-14 15:22:47 +00001036 Vals.push_back(getEncodedDLLStorageClass(F));
David Majnemerdad0a642014-06-27 18:19:56 +00001037 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001038 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1039 : 0);
David Majnemer7fddecc2015-06-17 20:52:32 +00001040 Vals.push_back(
1041 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001042
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001043 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001044 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001045 Vals.clear();
1046 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001047
Chris Lattner44c17072007-04-26 02:46:40 +00001048 // Emit the alias information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001049 for (const GlobalAlias &A : M.aliases()) {
Chad Rosiera966c312011-12-08 00:11:31 +00001050 // ALIAS: [alias type, aliasee val#, linkage, visibility]
David Blaikie6a51dbd2015-09-17 22:18:59 +00001051 Vals.push_back(VE.getTypeID(A.getValueType()));
1052 Vals.push_back(A.getType()->getAddressSpace());
Rafael Espindolaacef6c72014-05-26 13:38:51 +00001053 Vals.push_back(VE.getValueID(A.getAliasee()));
1054 Vals.push_back(getEncodedLinkage(A));
1055 Vals.push_back(getEncodedVisibility(A));
1056 Vals.push_back(getEncodedDLLStorageClass(A));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001057 Vals.push_back(getEncodedThreadLocalMode(A));
1058 Vals.push_back(A.hasUnnamedAddr());
Chris Lattner44c17072007-04-26 02:46:40 +00001059 unsigned AbbrevToUse = 0;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001060 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
Chris Lattner44c17072007-04-26 02:46:40 +00001061 Vals.clear();
1062 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001063
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001064 // Emit the ifunc information.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001065 for (const GlobalIFunc &I : M.ifuncs()) {
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001066 // IFUNC: [ifunc type, address space, resolver val#, linkage, visibility]
1067 Vals.push_back(VE.getTypeID(I.getValueType()));
1068 Vals.push_back(I.getType()->getAddressSpace());
1069 Vals.push_back(VE.getValueID(I.getResolver()));
1070 Vals.push_back(getEncodedLinkage(I));
1071 Vals.push_back(getEncodedVisibility(I));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001072 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001073 Vals.clear();
1074 }
1075
Teresa Johnsone1164de2016-02-10 21:55:02 +00001076 // Emit the module's source file name.
1077 {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001078 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1079 M.getSourceFileName().size());
Teresa Johnsone1164de2016-02-10 21:55:02 +00001080 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1081 if (Bits == SE_Char6)
1082 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1083 else if (Bits == SE_Fixed7)
1084 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1085
1086 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1087 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1088 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1089 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1090 Abbv->Add(AbbrevOpToUse);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001091 unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001092
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001093 for (const auto P : M.getSourceFileName())
Teresa Johnsone1164de2016-02-10 21:55:02 +00001094 Vals.push_back((unsigned char)P);
1095
1096 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001097 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
Teresa Johnsone1164de2016-02-10 21:55:02 +00001098 Vals.clear();
1099 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001100
Teresa Johnson37687f32016-04-23 04:30:47 +00001101 // If we have a VST, write the VSTOFFSET record placeholder.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001102 if (M.getValueSymbolTable().empty())
Teresa Johnson37687f32016-04-23 04:30:47 +00001103 return;
1104 writeValueSymbolTableForwardDecl();
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001105}
1106
Teresa Johnson37687f32016-04-23 04:30:47 +00001107static uint64_t getOptimizationFlags(const Value *V) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001108 uint64_t Flags = 0;
1109
Sanjay Patelc00017d2014-10-15 17:45:13 +00001110 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001111 if (OBO->hasNoSignedWrap())
1112 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1113 if (OBO->hasNoUnsignedWrap())
1114 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001115 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
Chris Lattner35315d02011-02-06 21:44:57 +00001116 if (PEO->isExact())
1117 Flags |= 1 << bitc::PEO_EXACT;
Sanjay Patelc00017d2014-10-15 17:45:13 +00001118 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001119 if (FPMO->hasUnsafeAlgebra())
Michael Ilseman65f14352012-12-09 21:12:04 +00001120 Flags |= FastMathFlags::UnsafeAlgebra;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001121 if (FPMO->hasNoNaNs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001122 Flags |= FastMathFlags::NoNaNs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001123 if (FPMO->hasNoInfs())
Michael Ilseman65f14352012-12-09 21:12:04 +00001124 Flags |= FastMathFlags::NoInfs;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001125 if (FPMO->hasNoSignedZeros())
Michael Ilseman65f14352012-12-09 21:12:04 +00001126 Flags |= FastMathFlags::NoSignedZeros;
Michael Ilseman9978d7e2012-11-27 00:43:38 +00001127 if (FPMO->hasAllowReciprocal())
Michael Ilseman65f14352012-12-09 21:12:04 +00001128 Flags |= FastMathFlags::AllowReciprocal;
Dan Gohman0ebd6962009-07-20 21:19:07 +00001129 }
1130
1131 return Flags;
1132}
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001133
Teresa Johnson37687f32016-04-23 04:30:47 +00001134void ModuleBitcodeWriter::writeValueAsMetadata(
1135 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001136 // Mimic an MDNode with a value as one operand.
1137 Value *V = MD->getValue();
1138 Record.push_back(VE.getTypeID(V->getType()));
1139 Record.push_back(VE.getValueID(V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001140 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001141 Record.clear();
1142}
1143
Teresa Johnson37687f32016-04-23 04:30:47 +00001144void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1145 SmallVectorImpl<uint64_t> &Record,
1146 unsigned Abbrev) {
Chris Lattner9b493022009-12-31 01:22:29 +00001147 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001148 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001149 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1150 "Unexpected function-local metadata");
1151 Record.push_back(VE.getMetadataOrNullID(MD));
Devang Patel8cca7b42009-08-04 05:01:35 +00001152 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001153 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1154 : bitc::METADATA_NODE,
1155 Record, Abbrev);
Devang Patel8cca7b42009-08-04 05:01:35 +00001156 Record.clear();
1157}
Devang Patele059ba6e2009-07-23 01:07:34 +00001158
Teresa Johnson37687f32016-04-23 04:30:47 +00001159unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001160 // Assume the column is usually under 128, and always output the inlined-at
1161 // location (it's never more expensive than building an array size 1).
1162 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1163 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1164 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1165 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1166 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1167 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1168 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001169 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001170}
1171
Teresa Johnson37687f32016-04-23 04:30:47 +00001172void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1173 SmallVectorImpl<uint64_t> &Record,
1174 unsigned &Abbrev) {
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001175 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001176 Abbrev = createDILocationAbbrev();
Duncan P. N. Exon Smith625fda22016-03-24 16:25:51 +00001177
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001178 Record.push_back(N->isDistinct());
1179 Record.push_back(N->getLine());
1180 Record.push_back(N->getColumn());
1181 Record.push_back(VE.getMetadataID(N->getScope()));
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +00001182 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001183
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001184 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00001185 Record.clear();
1186}
1187
Teresa Johnson37687f32016-04-23 04:30:47 +00001188unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001189 // Assume the column is usually under 128, and always output the inlined-at
1190 // location (it's never more expensive than building an array size 1).
1191 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1192 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1193 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1195 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1196 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1197 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1198 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001199 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001200}
1201
Teresa Johnson37687f32016-04-23 04:30:47 +00001202void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1203 SmallVectorImpl<uint64_t> &Record,
1204 unsigned &Abbrev) {
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001205 if (!Abbrev)
Teresa Johnson37687f32016-04-23 04:30:47 +00001206 Abbrev = createGenericDINodeAbbrev();
Duncan P. N. Exon Smitha5e25a52016-03-24 16:30:18 +00001207
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001208 Record.push_back(N->isDistinct());
1209 Record.push_back(N->getTag());
1210 Record.push_back(0); // Per-tag version field; unused for now.
1211
1212 for (auto &I : N->operands())
1213 Record.push_back(VE.getMetadataOrNullID(I));
1214
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001215 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001216 Record.clear();
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001217}
1218
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001219static uint64_t rotateSign(int64_t I) {
1220 uint64_t U = I;
1221 return I < 0 ? ~(U << 1) : U << 1;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001222}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001223
Teresa Johnson37687f32016-04-23 04:30:47 +00001224void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1225 SmallVectorImpl<uint64_t> &Record,
1226 unsigned Abbrev) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001227 Record.push_back(N->isDistinct());
1228 Record.push_back(N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001229 Record.push_back(rotateSign(N->getLowerBound()));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001230
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001231 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001232 Record.clear();
1233}
1234
Teresa Johnson37687f32016-04-23 04:30:47 +00001235void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1236 SmallVectorImpl<uint64_t> &Record,
1237 unsigned Abbrev) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001238 Record.push_back(N->isDistinct());
1239 Record.push_back(rotateSign(N->getValue()));
1240 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1241
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001242 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001243 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001244}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001245
Teresa Johnson37687f32016-04-23 04:30:47 +00001246void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1247 SmallVectorImpl<uint64_t> &Record,
1248 unsigned Abbrev) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001249 Record.push_back(N->isDistinct());
1250 Record.push_back(N->getTag());
1251 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1252 Record.push_back(N->getSizeInBits());
1253 Record.push_back(N->getAlignInBits());
1254 Record.push_back(N->getEncoding());
1255
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001256 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001257 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001258}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001259
Teresa Johnson37687f32016-04-23 04:30:47 +00001260void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1261 SmallVectorImpl<uint64_t> &Record,
1262 unsigned Abbrev) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001263 Record.push_back(N->isDistinct());
1264 Record.push_back(N->getTag());
1265 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1266 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1267 Record.push_back(N->getLine());
1268 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001269 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001270 Record.push_back(N->getSizeInBits());
1271 Record.push_back(N->getAlignInBits());
1272 Record.push_back(N->getOffsetInBits());
1273 Record.push_back(N->getFlags());
1274 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1275
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001276 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001277 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001278}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001279
Teresa Johnson37687f32016-04-23 04:30:47 +00001280void ModuleBitcodeWriter::writeDICompositeType(
1281 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
1282 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001283 const unsigned IsNotUsedInOldTypeRef = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001284 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001285 Record.push_back(N->getTag());
1286 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1287 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1288 Record.push_back(N->getLine());
1289 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1290 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1291 Record.push_back(N->getSizeInBits());
1292 Record.push_back(N->getAlignInBits());
1293 Record.push_back(N->getOffsetInBits());
1294 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001295 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001296 Record.push_back(N->getRuntimeLang());
1297 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001298 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001299 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1300
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001301 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001302 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001303}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001304
Teresa Johnson37687f32016-04-23 04:30:47 +00001305void ModuleBitcodeWriter::writeDISubroutineType(
1306 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
1307 unsigned Abbrev) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001308 const unsigned HasNoOldTypeRefs = 0x2;
Aaron Ballmanc79c2be2016-04-24 13:03:20 +00001309 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001310 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001311 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001312
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001313 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001314 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001315}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001316
Teresa Johnson37687f32016-04-23 04:30:47 +00001317void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1318 SmallVectorImpl<uint64_t> &Record,
1319 unsigned Abbrev) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001320 Record.push_back(N->isDistinct());
1321 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1322 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1323
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001324 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001325 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001326}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001327
Teresa Johnson37687f32016-04-23 04:30:47 +00001328void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1329 SmallVectorImpl<uint64_t> &Record,
1330 unsigned Abbrev) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001331 assert(N->isDistinct() && "Expected distinct compile units");
1332 Record.push_back(/* IsDistinct */ true);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001333 Record.push_back(N->getSourceLanguage());
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00001334 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001335 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1336 Record.push_back(N->isOptimized());
1337 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1338 Record.push_back(N->getRuntimeVersion());
1339 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1340 Record.push_back(N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001341 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1342 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001343 Record.push_back(/* subprograms */ 0);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001344 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1345 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
Adrian Prantl1f599f92015-05-21 20:37:30 +00001346 Record.push_back(N->getDWOId());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001347 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001348
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001349 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001350 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001351}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001352
Teresa Johnson37687f32016-04-23 04:30:47 +00001353void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1354 SmallVectorImpl<uint64_t> &Record,
1355 unsigned Abbrev) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001356 Record.push_back(N->isDistinct());
1357 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1358 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1359 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1360 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1361 Record.push_back(N->getLine());
1362 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1363 Record.push_back(N->isLocalToUnit());
1364 Record.push_back(N->isDefinition());
1365 Record.push_back(N->getScopeLine());
1366 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1367 Record.push_back(N->getVirtuality());
1368 Record.push_back(N->getVirtualIndex());
1369 Record.push_back(N->getFlags());
1370 Record.push_back(N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001371 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001372 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001373 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001374 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001375
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001376 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001377 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001378}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001379
Teresa Johnson37687f32016-04-23 04:30:47 +00001380void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1381 SmallVectorImpl<uint64_t> &Record,
1382 unsigned Abbrev) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001383 Record.push_back(N->isDistinct());
1384 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1385 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1386 Record.push_back(N->getLine());
1387 Record.push_back(N->getColumn());
1388
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001389 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001390 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001391}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001392
Teresa Johnson37687f32016-04-23 04:30:47 +00001393void ModuleBitcodeWriter::writeDILexicalBlockFile(
1394 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1395 unsigned Abbrev) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001396 Record.push_back(N->isDistinct());
1397 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1398 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1399 Record.push_back(N->getDiscriminator());
1400
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001401 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001402 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001403}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001404
Teresa Johnson37687f32016-04-23 04:30:47 +00001405void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
1406 SmallVectorImpl<uint64_t> &Record,
1407 unsigned Abbrev) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001408 Record.push_back(N->isDistinct());
1409 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1410 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1411 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1412 Record.push_back(N->getLine());
1413
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001414 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001415 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001416}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001417
Teresa Johnson37687f32016-04-23 04:30:47 +00001418void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
1419 SmallVectorImpl<uint64_t> &Record,
1420 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001421 Record.push_back(N->isDistinct());
1422 Record.push_back(N->getMacinfoType());
1423 Record.push_back(N->getLine());
1424 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1425 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1426
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001427 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001428 Record.clear();
1429}
1430
Teresa Johnson37687f32016-04-23 04:30:47 +00001431void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
1432 SmallVectorImpl<uint64_t> &Record,
1433 unsigned Abbrev) {
Amjad Abouda9bcf162015-12-10 12:56:35 +00001434 Record.push_back(N->isDistinct());
1435 Record.push_back(N->getMacinfoType());
1436 Record.push_back(N->getLine());
1437 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1438 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1439
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001440 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001441 Record.clear();
1442}
1443
Teresa Johnson37687f32016-04-23 04:30:47 +00001444void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
1445 SmallVectorImpl<uint64_t> &Record,
1446 unsigned Abbrev) {
Adrian Prantlab1243f2015-06-29 23:03:47 +00001447 Record.push_back(N->isDistinct());
1448 for (auto &I : N->operands())
1449 Record.push_back(VE.getMetadataOrNullID(I));
1450
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001451 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
Adrian Prantlab1243f2015-06-29 23:03:47 +00001452 Record.clear();
1453}
1454
Teresa Johnson37687f32016-04-23 04:30:47 +00001455void ModuleBitcodeWriter::writeDITemplateTypeParameter(
1456 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1457 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001458 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001459 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1460 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1461
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001462 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001463 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001464}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001465
Teresa Johnson37687f32016-04-23 04:30:47 +00001466void ModuleBitcodeWriter::writeDITemplateValueParameter(
1467 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1468 unsigned Abbrev) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001469 Record.push_back(N->isDistinct());
1470 Record.push_back(N->getTag());
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001471 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1472 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1473 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1474
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001475 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001476 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001477}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001478
Teresa Johnson37687f32016-04-23 04:30:47 +00001479void ModuleBitcodeWriter::writeDIGlobalVariable(
1480 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
1481 unsigned Abbrev) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001482 Record.push_back(N->isDistinct());
1483 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1484 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1485 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1486 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1487 Record.push_back(N->getLine());
1488 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1489 Record.push_back(N->isLocalToUnit());
1490 Record.push_back(N->isDefinition());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001491 Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001492 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1493
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001494 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001495 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001496}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001497
Teresa Johnson37687f32016-04-23 04:30:47 +00001498void ModuleBitcodeWriter::writeDILocalVariable(
1499 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
1500 unsigned Abbrev) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001501 Record.push_back(N->isDistinct());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001502 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1503 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1504 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1505 Record.push_back(N->getLine());
1506 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1507 Record.push_back(N->getArg());
1508 Record.push_back(N->getFlags());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001509
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001510 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001511 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001512}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001513
Teresa Johnson37687f32016-04-23 04:30:47 +00001514void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
1515 SmallVectorImpl<uint64_t> &Record,
1516 unsigned Abbrev) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001517 Record.reserve(N->getElements().size() + 1);
1518
1519 Record.push_back(N->isDistinct());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001520 Record.append(N->elements_begin(), N->elements_end());
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001521
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001522 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001523 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001524}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001525
Teresa Johnson37687f32016-04-23 04:30:47 +00001526void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1527 SmallVectorImpl<uint64_t> &Record,
1528 unsigned Abbrev) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001529 Record.push_back(N->isDistinct());
1530 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1531 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1532 Record.push_back(N->getLine());
1533 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1534 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1535 Record.push_back(N->getAttributes());
1536 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1537
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001538 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001539 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001540}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001541
Teresa Johnson37687f32016-04-23 04:30:47 +00001542void ModuleBitcodeWriter::writeDIImportedEntity(
1543 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
1544 unsigned Abbrev) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001545 Record.push_back(N->isDistinct());
1546 Record.push_back(N->getTag());
1547 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1548 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1549 Record.push_back(N->getLine());
1550 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1551
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001552 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001553 Record.clear();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001554}
1555
Teresa Johnson37687f32016-04-23 04:30:47 +00001556unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001557 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1558 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1559 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1560 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001561 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001562}
1563
Teresa Johnson37687f32016-04-23 04:30:47 +00001564void ModuleBitcodeWriter::writeNamedMetadata(
1565 SmallVectorImpl<uint64_t> &Record) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001566 if (M.named_metadata_empty())
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001567 return;
1568
Teresa Johnson37687f32016-04-23 04:30:47 +00001569 unsigned Abbrev = createNamedMetadataAbbrev();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001570 for (const NamedMDNode &NMD : M.named_metadata()) {
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001571 // Write name.
1572 StringRef Str = NMD.getName();
1573 Record.append(Str.bytes_begin(), Str.bytes_end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001574 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001575 Record.clear();
1576
1577 // Write named metadata operands.
1578 for (const MDNode *N : NMD.operands())
1579 Record.push_back(VE.getMetadataID(N));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001580 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Duncan P. N. Exon Smithf8ecdf52016-03-24 16:16:08 +00001581 Record.clear();
1582 }
1583}
1584
Teresa Johnson37687f32016-04-23 04:30:47 +00001585unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001586 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1587 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
1588 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
1589 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
1590 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001591 return Stream.EmitAbbrev(Abbv);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001592}
1593
1594/// Write out a record for MDString.
1595///
1596/// All the metadata strings in a metadata block are emitted in a single
1597/// record. The sizes and strings themselves are shoved into a blob.
Teresa Johnson37687f32016-04-23 04:30:47 +00001598void ModuleBitcodeWriter::writeMetadataStrings(
1599 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001600 if (Strings.empty())
1601 return;
1602
1603 // Start the record with the number of strings.
1604 Record.push_back(bitc::METADATA_STRINGS);
1605 Record.push_back(Strings.size());
1606
1607 // Emit the sizes of the strings in the blob.
1608 SmallString<256> Blob;
1609 {
1610 BitstreamWriter W(Blob);
1611 for (const Metadata *MD : Strings)
1612 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
1613 W.FlushToWord();
1614 }
1615
1616 // Add the offset to the strings to the record.
1617 Record.push_back(Blob.size());
1618
1619 // Add the strings to the blob.
1620 for (const Metadata *MD : Strings)
1621 Blob.append(cast<MDString>(MD)->getString());
1622
1623 // Emit the final record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001624 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001625 Record.clear();
1626}
1627
Teresa Johnson37687f32016-04-23 04:30:47 +00001628void ModuleBitcodeWriter::writeMetadataRecords(
1629 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record) {
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001630 if (MDs.empty())
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +00001631 return;
1632
Duncan P. N. Exon Smith6b7b6802015-02-04 21:54:12 +00001633 // Initialize MDNode abbreviations.
1634#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1635#include "llvm/IR/Metadata.def"
1636
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001637 for (const Metadata *MD : MDs) {
Duncan P. N. Exon Smith73d5aae2015-01-12 22:31:35 +00001638 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith79f8d112015-03-17 00:16:35 +00001639 assert(N->isResolved() && "Expected forward references to be resolved");
1640
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001641 switch (N->getMetadataID()) {
1642 default:
1643 llvm_unreachable("Invalid MDNode subclass");
1644#define HANDLE_MDNODE_LEAF(CLASS) \
1645 case Metadata::CLASS##Kind: \
Teresa Johnson37687f32016-04-23 04:30:47 +00001646 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
Duncan P. N. Exon Smithdb6bc8b2015-01-20 01:03:09 +00001647 continue;
1648#include "llvm/IR/Metadata.def"
1649 }
Devang Patel8cca7b42009-08-04 05:01:35 +00001650 }
Teresa Johnson37687f32016-04-23 04:30:47 +00001651 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
Devang Patel8cca7b42009-08-04 05:01:35 +00001652 }
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001653}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001654
Teresa Johnson37687f32016-04-23 04:30:47 +00001655void ModuleBitcodeWriter::writeModuleMetadata() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001656 if (!VE.hasMDs() && M.named_metadata_empty())
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001657 return;
1658
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001659 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith80d153f2016-03-27 23:53:30 +00001660 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001661 writeMetadataStrings(VE.getMDStrings(), Record);
1662 writeMetadataRecords(VE.getNonMDStrings(), Record);
1663 writeNamedMetadata(Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001664 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001665}
1666
Teresa Johnson37687f32016-04-23 04:30:47 +00001667void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +00001668 if (!VE.hasMDs())
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001669 return;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001670
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001671 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Duncan P. N. Exon Smith5465f0a2016-03-27 23:38:36 +00001672 SmallVector<uint64_t, 64> Record;
Teresa Johnson37687f32016-04-23 04:30:47 +00001673 writeMetadataStrings(VE.getMDStrings(), Record);
1674 writeMetadataRecords(VE.getNonMDStrings(), Record);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001675 Stream.ExitBlock();
Victor Hernandezb00a6be2010-01-13 19:37:33 +00001676}
1677
Teresa Johnson37687f32016-04-23 04:30:47 +00001678void ModuleBitcodeWriter::writeMetadataAttachment(const Function &F) {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001679 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
Chris Lattner07d09ed2010-04-03 02:17:50 +00001680
Devang Patelaf206b82009-09-18 19:26:43 +00001681 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001682
Devang Patelaf206b82009-09-18 19:26:43 +00001683 // Write metadata attachments
Chris Lattnerbb95d5e2011-06-17 17:56:00 +00001684 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001685 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001686 F.getAllMetadata(MDs);
1687 if (!MDs.empty()) {
1688 for (const auto &I : MDs) {
1689 Record.push_back(I.first);
1690 Record.push_back(VE.getMetadataID(I.second));
1691 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001692 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001693 Record.clear();
1694 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001695
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001696 for (const BasicBlock &BB : F)
1697 for (const Instruction &I : BB) {
Devang Patel6da5dbf2009-10-22 18:55:16 +00001698 MDs.clear();
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001699 I.getAllMetadataOtherThanDebugLoc(MDs);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001700
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001701 // If no metadata, ignore instruction.
1702 if (MDs.empty()) continue;
1703
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +00001704 Record.push_back(VE.getInstructionID(&I));
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001705
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001706 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1707 Record.push_back(MDs[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001708 Record.push_back(VE.getMetadataID(MDs[i].second));
Devang Patelaf206b82009-09-18 19:26:43 +00001709 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001710 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00001711 Record.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00001712 }
1713
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001714 Stream.ExitBlock();
Devang Patelaf206b82009-09-18 19:26:43 +00001715}
1716
Teresa Johnson37687f32016-04-23 04:30:47 +00001717void ModuleBitcodeWriter::writeModuleMetadataStore() {
Devang Patelaf206b82009-09-18 19:26:43 +00001718 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001719
Devang Patelaf206b82009-09-18 19:26:43 +00001720 // Write metadata kinds
1721 // METADATA_KIND - [n x [id, name]]
Michael Ilseman979dfbb2012-12-03 22:57:47 +00001722 SmallVector<StringRef, 8> Names;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001723 M.getMDKindNames(Names);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001724
Dan Gohman43aa8f02010-07-20 21:42:28 +00001725 if (Names.empty()) return;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001726
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001727 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001728
Dan Gohman43aa8f02010-07-20 21:42:28 +00001729 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattnerc9558df2009-12-28 20:10:43 +00001730 Record.push_back(MDKindID);
1731 StringRef KName = Names[MDKindID];
1732 Record.append(KName.begin(), KName.end());
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001733
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001734 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
Devang Patelaf206b82009-09-18 19:26:43 +00001735 Record.clear();
1736 }
1737
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001738 Stream.ExitBlock();
Devang Patel8cca7b42009-08-04 05:01:35 +00001739}
1740
Teresa Johnson37687f32016-04-23 04:30:47 +00001741void ModuleBitcodeWriter::writeOperandBundleTags() {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001742 // Write metadata kinds
1743 //
1744 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
1745 //
1746 // OPERAND_BUNDLE_TAG - [strchr x N]
1747
1748 SmallVector<StringRef, 8> Tags;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001749 M.getOperandBundleTags(Tags);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001750
1751 if (Tags.empty())
1752 return;
1753
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001754 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001755
1756 SmallVector<uint64_t, 64> Record;
1757
1758 for (auto Tag : Tags) {
1759 Record.append(Tag.begin(), Tag.end());
1760
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001761 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001762 Record.clear();
1763 }
1764
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001765 Stream.ExitBlock();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001766}
1767
Jan Wen Voungafaced02012-10-11 20:20:40 +00001768static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1769 if ((int64_t)V >= 0)
1770 Vals.push_back(V << 1);
1771 else
1772 Vals.push_back((-V << 1) | 1);
1773}
1774
Teresa Johnson37687f32016-04-23 04:30:47 +00001775void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1776 bool isGlobal) {
Devang Patel8cca7b42009-08-04 05:01:35 +00001777 if (FirstVal == LastVal) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001778
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001779 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Devang Patel8cca7b42009-08-04 05:01:35 +00001780
1781 unsigned AggregateAbbrev = 0;
1782 unsigned String8Abbrev = 0;
1783 unsigned CString7Abbrev = 0;
1784 unsigned CString6Abbrev = 0;
1785 // If this is a constant pool for the module, emit module-specific abbrevs.
1786 if (isGlobal) {
1787 // Abbrev for CST_CODE_AGGREGATE.
1788 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1789 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1790 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1791 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001792 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001793
1794 // Abbrev for CST_CODE_STRING.
1795 Abbv = new BitCodeAbbrev();
1796 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001799 String8Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001800 // Abbrev for CST_CODE_CSTRING.
1801 Abbv = new BitCodeAbbrev();
1802 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1803 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1804 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001805 CString7Abbrev = Stream.EmitAbbrev(Abbv);
Devang Patel8cca7b42009-08-04 05:01:35 +00001806 // Abbrev for CST_CODE_CSTRING.
1807 Abbv = new BitCodeAbbrev();
1808 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1810 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001811 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001812 }
1813
Devang Patel8cca7b42009-08-04 05:01:35 +00001814 SmallVector<uint64_t, 64> Record;
1815
1816 const ValueEnumerator::ValueList &Vals = VE.getValues();
Craig Topper2617dcc2014-04-15 06:32:26 +00001817 Type *LastTy = nullptr;
Devang Patel8cca7b42009-08-04 05:01:35 +00001818 for (unsigned i = FirstVal; i != LastVal; ++i) {
1819 const Value *V = Vals[i].first;
Chris Lattner52523562007-04-24 00:16:04 +00001820 // If we need to switch types, do so now.
1821 if (V->getType() != LastTy) {
1822 LastTy = V->getType();
1823 Record.push_back(VE.getTypeID(LastTy));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001824 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1825 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner52523562007-04-24 00:16:04 +00001826 Record.clear();
1827 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001828
Chris Lattner52523562007-04-24 00:16:04 +00001829 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesenfd04c742009-10-13 20:46:56 +00001830 Record.push_back(unsigned(IA->hasSideEffects()) |
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001831 unsigned(IA->isAlignStack()) << 1 |
1832 unsigned(IA->getDialect()&1) << 2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001833
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001834 // Add the asm string.
1835 const std::string &AsmStr = IA->getAsmString();
1836 Record.push_back(AsmStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001837 Record.append(AsmStr.begin(), AsmStr.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001838
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001839 // Add the constraint string.
1840 const std::string &ConstraintStr = IA->getConstraintString();
1841 Record.push_back(ConstraintStr.size());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001842 Record.append(ConstraintStr.begin(), ConstraintStr.end());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00001843 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001844 Record.clear();
Chris Lattner52523562007-04-24 00:16:04 +00001845 continue;
1846 }
1847 const Constant *C = cast<Constant>(V);
1848 unsigned Code = -1U;
1849 unsigned AbbrevToUse = 0;
1850 if (C->isNullValue()) {
1851 Code = bitc::CST_CODE_NULL;
1852 } else if (isa<UndefValue>(C)) {
1853 Code = bitc::CST_CODE_UNDEF;
1854 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
Bob Wilsone4077362013-09-09 19:14:35 +00001855 if (IV->getBitWidth() <= 64) {
1856 uint64_t V = IV->getSExtValue();
1857 emitSignedInt64(Record, V);
1858 Code = bitc::CST_CODE_INTEGER;
1859 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1860 } else { // Wide integers, > 64 bits in size.
1861 // We have an arbitrary precision integer value to write whose
1862 // bit width is > 64. However, in canonical unsigned integer
1863 // format it is likely that the high bits are going to be zero.
1864 // So, we only write the number of active words.
1865 unsigned NWords = IV->getValue().getActiveWords();
1866 const uint64_t *RawWords = IV->getValue().getRawData();
1867 for (unsigned i = 0; i != NWords; ++i) {
1868 emitSignedInt64(Record, RawWords[i]);
1869 }
1870 Code = bitc::CST_CODE_WIDE_INTEGER;
1871 }
Chris Lattner52523562007-04-24 00:16:04 +00001872 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1873 Code = bitc::CST_CODE_FLOAT;
Chris Lattner229907c2011-07-18 04:54:35 +00001874 Type *Ty = CFP->getType();
Dan Gohman518cda42011-12-17 00:04:22 +00001875 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00001876 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +00001877 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001878 // api needed to prevent premature destruction
Dale Johannesen93eefa02009-03-23 21:16:53 +00001879 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen54306fe2008-10-09 18:53:47 +00001880 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001881 const uint64_t *p = api.getRawData();
Dale Johannesen93eefa02009-03-23 21:16:53 +00001882 Record.push_back((p[1] << 48) | (p[0] >> 16));
1883 Record.push_back(p[0] & 0xffffLL);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001884 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00001885 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001886 const uint64_t *p = api.getRawData();
Dale Johannesen245dceb2007-09-11 18:32:33 +00001887 Record.push_back(p[0]);
1888 Record.push_back(p[1]);
Dale Johannesenbdad8092007-08-09 22:51:36 +00001889 } else {
1890 assert (0 && "Unknown FP type!");
Chris Lattner52523562007-04-24 00:16:04 +00001891 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00001892 } else if (isa<ConstantDataSequential>(C) &&
1893 cast<ConstantDataSequential>(C)->isString()) {
1894 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
1895 // Emit constant strings specially.
1896 unsigned NumElts = Str->getNumElements();
1897 // If this is a null-terminated string, use the denser CSTRING encoding.
1898 if (Str->isCString()) {
1899 Code = bitc::CST_CODE_CSTRING;
1900 --NumElts; // Don't encode the null, which isn't allowed by char6.
1901 } else {
1902 Code = bitc::CST_CODE_STRING;
1903 AbbrevToUse = String8Abbrev;
1904 }
1905 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1906 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1907 for (unsigned i = 0; i != NumElts; ++i) {
1908 unsigned char V = Str->getElementAsInteger(i);
1909 Record.push_back(V);
1910 isCStr7 &= (V & 128) == 0;
1911 if (isCStrChar6)
1912 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1913 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001914
Chris Lattner372dd1e2012-01-30 00:51:16 +00001915 if (isCStrChar6)
1916 AbbrevToUse = CString6Abbrev;
1917 else if (isCStr7)
1918 AbbrevToUse = CString7Abbrev;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001919 } else if (const ConstantDataSequential *CDS =
Chris Lattner372dd1e2012-01-30 00:51:16 +00001920 dyn_cast<ConstantDataSequential>(C)) {
Chris Lattner5d917072012-01-30 07:36:01 +00001921 Code = bitc::CST_CODE_DATA;
Chris Lattner372dd1e2012-01-30 00:51:16 +00001922 Type *EltTy = CDS->getType()->getElementType();
1923 if (isa<IntegerType>(EltTy)) {
1924 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1925 Record.push_back(CDS->getElementAsInteger(i));
Chris Lattner372dd1e2012-01-30 00:51:16 +00001926 } else {
Justin Bognera43eacb2016-01-06 22:31:32 +00001927 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1928 Record.push_back(
1929 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
Chris Lattner372dd1e2012-01-30 00:51:16 +00001930 }
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00001931 } else if (isa<ConstantAggregate>(C)) {
Chris Lattner52523562007-04-24 00:16:04 +00001932 Code = bitc::CST_CODE_AGGREGATE;
Pete Cooper125ad172015-06-25 20:51:38 +00001933 for (const Value *Op : C->operands())
1934 Record.push_back(VE.getValueID(Op));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00001935 AbbrevToUse = AggregateAbbrev;
Chris Lattner52523562007-04-24 00:16:04 +00001936 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001937 switch (CE->getOpcode()) {
1938 default:
1939 if (Instruction::isCast(CE->getOpcode())) {
1940 Code = bitc::CST_CODE_CE_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00001941 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001942 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1943 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00001944 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001945 } else {
1946 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1947 Code = bitc::CST_CODE_CE_BINOP;
Teresa Johnson37687f32016-04-23 04:30:47 +00001948 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001949 Record.push_back(VE.getValueID(C->getOperand(0)));
1950 Record.push_back(VE.getValueID(C->getOperand(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00001951 uint64_t Flags = getOptimizationFlags(CE);
Dan Gohman0ebd6962009-07-20 21:19:07 +00001952 if (Flags != 0)
1953 Record.push_back(Flags);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001954 }
1955 break;
David Blaikieb9263572015-03-13 21:03:36 +00001956 case Instruction::GetElementPtr: {
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001957 Code = bitc::CST_CODE_CE_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00001958 const auto *GO = cast<GEPOperator>(C);
1959 if (GO->isInBounds())
Dan Gohman1639c392009-07-27 21:53:46 +00001960 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
David Blaikieb9263572015-03-13 21:03:36 +00001961 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001962 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1963 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1964 Record.push_back(VE.getValueID(C->getOperand(i)));
1965 }
1966 break;
David Blaikieb9263572015-03-13 21:03:36 +00001967 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001968 case Instruction::Select:
1969 Code = bitc::CST_CODE_CE_SELECT;
1970 Record.push_back(VE.getValueID(C->getOperand(0)));
1971 Record.push_back(VE.getValueID(C->getOperand(1)));
1972 Record.push_back(VE.getValueID(C->getOperand(2)));
1973 break;
1974 case Instruction::ExtractElement:
1975 Code = bitc::CST_CODE_CE_EXTRACTELT;
1976 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1977 Record.push_back(VE.getValueID(C->getOperand(0)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001978 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001979 Record.push_back(VE.getValueID(C->getOperand(1)));
1980 break;
1981 case Instruction::InsertElement:
1982 Code = bitc::CST_CODE_CE_INSERTELT;
1983 Record.push_back(VE.getValueID(C->getOperand(0)));
1984 Record.push_back(VE.getValueID(C->getOperand(1)));
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001985 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001986 Record.push_back(VE.getValueID(C->getOperand(2)));
1987 break;
1988 case Instruction::ShuffleVector:
Nate Begeman94aa38d2009-02-12 21:28:33 +00001989 // If the return type and argument types are the same, this is a
1990 // standard shufflevector instruction. If the types are different,
1991 // then the shuffle is widening or truncating the input vectors, and
1992 // the argument type must also be encoded.
1993 if (C->getType() == C->getOperand(0)->getType()) {
1994 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
1995 } else {
1996 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
1997 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1998 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001999 Record.push_back(VE.getValueID(C->getOperand(0)));
2000 Record.push_back(VE.getValueID(C->getOperand(1)));
2001 Record.push_back(VE.getValueID(C->getOperand(2)));
2002 break;
2003 case Instruction::ICmp:
2004 case Instruction::FCmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002005 Code = bitc::CST_CODE_CE_CMP;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002006 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2007 Record.push_back(VE.getValueID(C->getOperand(0)));
2008 Record.push_back(VE.getValueID(C->getOperand(1)));
2009 Record.push_back(CE->getPredicate());
2010 break;
2011 }
Chris Lattnerf540d742009-10-28 05:24:40 +00002012 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerf540d742009-10-28 05:24:40 +00002013 Code = bitc::CST_CODE_BLOCKADDRESS;
2014 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2015 Record.push_back(VE.getValueID(BA->getFunction()));
2016 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner52523562007-04-24 00:16:04 +00002017 } else {
Dan Gohman47dc8fd2010-07-21 21:18:37 +00002018#ifndef NDEBUG
2019 C->dump();
2020#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002021 llvm_unreachable("Unknown constant!");
Chris Lattner52523562007-04-24 00:16:04 +00002022 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002023 Stream.EmitRecord(Code, Record, AbbrevToUse);
Chris Lattner52523562007-04-24 00:16:04 +00002024 Record.clear();
2025 }
2026
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002027 Stream.ExitBlock();
Chris Lattner52523562007-04-24 00:16:04 +00002028}
2029
Teresa Johnson37687f32016-04-23 04:30:47 +00002030void ModuleBitcodeWriter::writeModuleConstants() {
Chris Lattner52523562007-04-24 00:16:04 +00002031 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002032
Chris Lattner52523562007-04-24 00:16:04 +00002033 // Find the first constant to emit, which is the first non-globalvalue value.
2034 // We know globalvalues have been emitted by WriteModuleInfo.
2035 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2036 if (!isa<GlobalValue>(Vals[i].first)) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002037 writeConstants(i, Vals.size(), true);
Chris Lattner52523562007-04-24 00:16:04 +00002038 return;
2039 }
2040 }
2041}
Chris Lattner215e9cd2007-04-23 20:35:01 +00002042
Teresa Johnson37687f32016-04-23 04:30:47 +00002043/// pushValueAndType - The file has to encode both the value and type id for
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002044/// many values, because we need to know what type to create for forward
2045/// references. However, most operands are not forward references, so this type
2046/// field is not needed.
2047///
2048/// This function adds V's value ID to Vals. If the value ID is higher than the
2049/// instruction ID, then it is a forward reference, and it also includes the
Jan Wen Voungafaced02012-10-11 20:20:40 +00002050/// type ID. The value ID that is written is encoded relative to the InstID.
Teresa Johnson37687f32016-04-23 04:30:47 +00002051bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2052 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002053 unsigned ValID = VE.getValueID(V);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002054 // Make encoding relative to the InstID.
2055 Vals.push_back(InstID - ValID);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002056 if (ValID >= InstID) {
2057 Vals.push_back(VE.getTypeID(V->getType()));
2058 return true;
2059 }
2060 return false;
2061}
2062
Teresa Johnson37687f32016-04-23 04:30:47 +00002063void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
2064 unsigned InstID) {
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002065 SmallVector<unsigned, 64> Record;
2066 LLVMContext &C = CS.getInstruction()->getContext();
2067
2068 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002069 const auto &Bundle = CS.getOperandBundleAt(i);
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002070 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002071
2072 for (auto &Input : Bundle.Inputs)
Teresa Johnson37687f32016-04-23 04:30:47 +00002073 pushValueAndType(Input, InstID, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002074
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002075 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002076 Record.clear();
2077 }
2078}
2079
Teresa Johnson37687f32016-04-23 04:30:47 +00002080/// pushValue - Like pushValueAndType, but where the type of the value is
Jan Wen Voungafaced02012-10-11 20:20:40 +00002081/// omitted (perhaps it was already encoded in an earlier operand).
Teresa Johnson37687f32016-04-23 04:30:47 +00002082void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2083 SmallVectorImpl<unsigned> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002084 unsigned ValID = VE.getValueID(V);
2085 Vals.push_back(InstID - ValID);
2086}
2087
Teresa Johnson37687f32016-04-23 04:30:47 +00002088void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2089 SmallVectorImpl<uint64_t> &Vals) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002090 unsigned ValID = VE.getValueID(V);
2091 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2092 emitSignedInt64(Vals, diff);
2093}
2094
Chris Lattnere6e364c2007-04-26 05:53:54 +00002095/// WriteInstruction - Emit an instruction to the specified stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002096void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2097 unsigned InstID,
2098 SmallVectorImpl<unsigned> &Vals) {
Chris Lattnere6e364c2007-04-26 05:53:54 +00002099 unsigned Code = 0;
2100 unsigned AbbrevToUse = 0;
Devang Patelaf206b82009-09-18 19:26:43 +00002101 VE.setInstructionID(&I);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002102 switch (I.getOpcode()) {
2103 default:
2104 if (Instruction::isCast(I.getOpcode())) {
Chris Lattner0a603252007-05-01 02:13:26 +00002105 Code = bitc::FUNC_CODE_INST_CAST;
Teresa Johnson37687f32016-04-23 04:30:47 +00002106 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002107 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002108 Vals.push_back(VE.getTypeID(I.getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002109 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
Chris Lattnere6e364c2007-04-26 05:53:54 +00002110 } else {
2111 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattner9f35f912007-05-02 04:26:36 +00002112 Code = bitc::FUNC_CODE_INST_BINOP;
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_BINOP_ABBREV;
Teresa Johnson37687f32016-04-23 04:30:47 +00002115 pushValue(I.getOperand(1), InstID, Vals);
2116 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2117 uint64_t Flags = getOptimizationFlags(&I);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002118 if (Flags != 0) {
2119 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2120 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2121 Vals.push_back(Flags);
2122 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002123 }
2124 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002125
David Blaikieb5b5efd2015-02-25 01:08:52 +00002126 case Instruction::GetElementPtr: {
Chris Lattner0a603252007-05-01 02:13:26 +00002127 Code = bitc::FUNC_CODE_INST_GEP;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002128 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2129 auto &GEPInst = cast<GetElementPtrInst>(I);
2130 Vals.push_back(GEPInst.isInBounds());
2131 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002132 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002133 pushValueAndType(I.getOperand(i), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002134 break;
David Blaikieb5b5efd2015-02-25 01:08:52 +00002135 }
Dan Gohmanca0256a2008-05-31 19:11:15 +00002136 case Instruction::ExtractValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002137 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002138 pushValueAndType(I.getOperand(0), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002139 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002140 Vals.append(EVI->idx_begin(), EVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002141 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002142 }
2143 case Instruction::InsertValue: {
Dan Gohman30499842008-05-23 01:55:30 +00002144 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Teresa Johnson37687f32016-04-23 04:30:47 +00002145 pushValueAndType(I.getOperand(0), InstID, Vals);
2146 pushValueAndType(I.getOperand(1), InstID, Vals);
Dan Gohmanca0256a2008-05-31 19:11:15 +00002147 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002148 Vals.append(IVI->idx_begin(), IVI->idx_end());
Dan Gohman30499842008-05-23 01:55:30 +00002149 break;
Dan Gohmanca0256a2008-05-31 19:11:15 +00002150 }
Chris Lattner0a603252007-05-01 02:13:26 +00002151 case Instruction::Select:
Dan Gohmanc5d28922008-09-16 01:01:33 +00002152 Code = bitc::FUNC_CODE_INST_VSELECT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002153 pushValueAndType(I.getOperand(1), InstID, Vals);
2154 pushValue(I.getOperand(2), InstID, Vals);
2155 pushValueAndType(I.getOperand(0), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002156 break;
2157 case Instruction::ExtractElement:
2158 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002159 pushValueAndType(I.getOperand(0), InstID, Vals);
2160 pushValueAndType(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002161 break;
2162 case Instruction::InsertElement:
2163 Code = bitc::FUNC_CODE_INST_INSERTELT;
Teresa Johnson37687f32016-04-23 04:30:47 +00002164 pushValueAndType(I.getOperand(0), InstID, Vals);
2165 pushValue(I.getOperand(1), InstID, Vals);
2166 pushValueAndType(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002167 break;
2168 case Instruction::ShuffleVector:
2169 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002170 pushValueAndType(I.getOperand(0), InstID, Vals);
2171 pushValue(I.getOperand(1), InstID, Vals);
2172 pushValue(I.getOperand(2), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002173 break;
2174 case Instruction::ICmp:
James Molloy88eb5352015-07-10 12:52:00 +00002175 case Instruction::FCmp: {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002176 // compare returning Int1Ty or vector of Int1Ty
2177 Code = bitc::FUNC_CODE_INST_CMP2;
Teresa Johnson37687f32016-04-23 04:30:47 +00002178 pushValueAndType(I.getOperand(0), InstID, Vals);
2179 pushValue(I.getOperand(1), InstID, Vals);
Chris Lattner0a603252007-05-01 02:13:26 +00002180 Vals.push_back(cast<CmpInst>(I).getPredicate());
Teresa Johnson37687f32016-04-23 04:30:47 +00002181 uint64_t Flags = getOptimizationFlags(&I);
James Molloy88eb5352015-07-10 12:52:00 +00002182 if (Flags != 0)
2183 Vals.push_back(Flags);
Chris Lattner0a603252007-05-01 02:13:26 +00002184 break;
James Molloy88eb5352015-07-10 12:52:00 +00002185 }
Chris Lattner0a603252007-05-01 02:13:26 +00002186
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002187 case Instruction::Ret:
Devang Patelbbfd8742008-02-26 01:29:32 +00002188 {
2189 Code = bitc::FUNC_CODE_INST_RET;
2190 unsigned NumOperands = I.getNumOperands();
Devang Patelbbfd8742008-02-26 01:29:32 +00002191 if (NumOperands == 0)
2192 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2193 else if (NumOperands == 1) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002194 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Devang Patelbbfd8742008-02-26 01:29:32 +00002195 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2196 } else {
2197 for (unsigned i = 0, e = NumOperands; i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002198 pushValueAndType(I.getOperand(i), InstID, Vals);
Devang Patelbbfd8742008-02-26 01:29:32 +00002199 }
2200 }
Chris Lattner0a603252007-05-01 02:13:26 +00002201 break;
2202 case Instruction::Br:
Gabor Greif1933b002009-01-30 18:27:21 +00002203 {
2204 Code = bitc::FUNC_CODE_INST_BR;
David Blaikieb78e9e52013-02-11 01:16:51 +00002205 const BranchInst &II = cast<BranchInst>(I);
Gabor Greif1933b002009-01-30 18:27:21 +00002206 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2207 if (II.isConditional()) {
2208 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
Teresa Johnson37687f32016-04-23 04:30:47 +00002209 pushValue(II.getCondition(), InstID, Vals);
Gabor Greif1933b002009-01-30 18:27:21 +00002210 }
Chris Lattner0a603252007-05-01 02:13:26 +00002211 }
2212 break;
2213 case Instruction::Switch:
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002214 {
2215 Code = bitc::FUNC_CODE_INST_SWITCH;
David Blaikieb78e9e52013-02-11 01:16:51 +00002216 const SwitchInst &SI = cast<SwitchInst>(I);
Bob Wilsone4077362013-09-09 19:14:35 +00002217 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
Teresa Johnson37687f32016-04-23 04:30:47 +00002218 pushValue(SI.getCondition(), InstID, Vals);
Bob Wilsone4077362013-09-09 19:14:35 +00002219 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
Sanjay Patel225d65f2015-11-13 16:21:23 +00002220 for (SwitchInst::ConstCaseIt Case : SI.cases()) {
2221 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2222 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00002223 }
2224 }
Chris Lattner0a603252007-05-01 02:13:26 +00002225 break;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002226 case Instruction::IndirectBr:
2227 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattner3ed871f2009-10-27 19:13:16 +00002228 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Jan Wen Voungafaced02012-10-11 20:20:40 +00002229 // Encode the address operand as relative, but not the basic blocks.
Teresa Johnson37687f32016-04-23 04:30:47 +00002230 pushValue(I.getOperand(0), InstID, Vals);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002231 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
Chris Lattner3ed871f2009-10-27 19:13:16 +00002232 Vals.push_back(VE.getValueID(I.getOperand(i)));
2233 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002234
Chris Lattnerb811e952007-05-01 07:03:37 +00002235 case Instruction::Invoke: {
Gabor Greif4b79e472009-01-16 18:40:27 +00002236 const InvokeInst *II = cast<InvokeInst>(&I);
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002237 const Value *Callee = II->getCalledValue();
2238 FunctionType *FTy = II->getFunctionType();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002239
2240 if (II->hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002241 writeOperandBundles(II, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002242
Chris Lattner0a603252007-05-01 02:13:26 +00002243 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002244
Devang Patel4c758ea2008-09-25 21:00:45 +00002245 Vals.push_back(VE.getAttributeID(II->getAttributes()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002246 Vals.push_back(II->getCallingConv() | 1 << 13);
Gabor Greif6ecd6f42009-01-07 22:39:29 +00002247 Vals.push_back(VE.getValueID(II->getNormalDest()));
2248 Vals.push_back(VE.getValueID(II->getUnwindDest()));
David Blaikie5ea1f7b2015-04-24 18:06:06 +00002249 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002250 pushValueAndType(Callee, InstID, Vals);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002251
Chris Lattner0a603252007-05-01 02:13:26 +00002252 // Emit value #'s for the fixed parameters.
Chris Lattner0a603252007-05-01 02:13:26 +00002253 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002254 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
Chris Lattner0a603252007-05-01 02:13:26 +00002255
2256 // Emit type/value pairs for varargs params.
2257 if (FTy->isVarArg()) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00002258 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002259 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002260 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
Chris Lattner0a603252007-05-01 02:13:26 +00002261 }
2262 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002263 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002264 case Instruction::Resume:
2265 Code = bitc::FUNC_CODE_INST_RESUME;
Teresa Johnson37687f32016-04-23 04:30:47 +00002266 pushValueAndType(I.getOperand(0), InstID, Vals);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002267 break;
David Majnemer654e1302015-07-31 17:58:14 +00002268 case Instruction::CleanupRet: {
2269 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
2270 const auto &CRI = cast<CleanupReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002271 pushValue(CRI.getCleanupPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002272 if (CRI.hasUnwindDest())
2273 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2274 break;
2275 }
2276 case Instruction::CatchRet: {
2277 Code = bitc::FUNC_CODE_INST_CATCHRET;
2278 const auto &CRI = cast<CatchReturnInst>(I);
Teresa Johnson37687f32016-04-23 04:30:47 +00002279 pushValue(CRI.getCatchPad(), InstID, Vals);
David Majnemer654e1302015-07-31 17:58:14 +00002280 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2281 break;
2282 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00002283 case Instruction::CleanupPad:
David Majnemer654e1302015-07-31 17:58:14 +00002284 case Instruction::CatchPad: {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002285 const auto &FuncletPad = cast<FuncletPadInst>(I);
2286 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2287 : bitc::FUNC_CODE_INST_CLEANUPPAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002288 pushValue(FuncletPad.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002289
2290 unsigned NumArgOperands = FuncletPad.getNumArgOperands();
David Majnemer654e1302015-07-31 17:58:14 +00002291 Vals.push_back(NumArgOperands);
2292 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
Teresa Johnson37687f32016-04-23 04:30:47 +00002293 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002294 break;
2295 }
2296 case Instruction::CatchSwitch: {
2297 Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2298 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2299
Teresa Johnson37687f32016-04-23 04:30:47 +00002300 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
David Majnemer8a1c45d2015-12-12 05:38:55 +00002301
2302 unsigned NumHandlers = CatchSwitch.getNumHandlers();
2303 Vals.push_back(NumHandlers);
2304 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2305 Vals.push_back(VE.getValueID(CatchPadBB));
2306
2307 if (CatchSwitch.hasUnwindDest())
2308 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
David Majnemer654e1302015-07-31 17:58:14 +00002309 break;
2310 }
Chris Lattnere6e364c2007-04-26 05:53:54 +00002311 case Instruction::Unreachable:
2312 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002313 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002314 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002315
Jay Foad372ad642011-06-20 14:18:48 +00002316 case Instruction::PHI: {
2317 const PHINode &PN = cast<PHINode>(I);
Chris Lattner0a603252007-05-01 02:13:26 +00002318 Code = bitc::FUNC_CODE_INST_PHI;
Jan Wen Voungafaced02012-10-11 20:20:40 +00002319 // With the newer instruction encoding, forward references could give
2320 // negative valued IDs. This is most common for PHIs, so we use
2321 // signed VBRs.
2322 SmallVector<uint64_t, 128> Vals64;
2323 Vals64.push_back(VE.getTypeID(PN.getType()));
Jay Foad372ad642011-06-20 14:18:48 +00002324 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002325 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002326 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
Jay Foad372ad642011-06-20 14:18:48 +00002327 }
Jan Wen Voungafaced02012-10-11 20:20:40 +00002328 // Emit a Vals64 vector and exit.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002329 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002330 Vals64.clear();
2331 return;
Jay Foad372ad642011-06-20 14:18:48 +00002332 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002333
Bill Wendlingfae14752011-08-12 20:24:12 +00002334 case Instruction::LandingPad: {
2335 const LandingPadInst &LP = cast<LandingPadInst>(I);
2336 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2337 Vals.push_back(VE.getTypeID(LP.getType()));
Bill Wendlingfae14752011-08-12 20:24:12 +00002338 Vals.push_back(LP.isCleanup());
2339 Vals.push_back(LP.getNumClauses());
2340 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2341 if (LP.isCatch(I))
2342 Vals.push_back(LandingPadInst::Catch);
2343 else
2344 Vals.push_back(LandingPadInst::Filter);
Teresa Johnson37687f32016-04-23 04:30:47 +00002345 pushValueAndType(LP.getClause(I), InstID, Vals);
Bill Wendlingfae14752011-08-12 20:24:12 +00002346 }
2347 break;
2348 }
2349
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002350 case Instruction::Alloca: {
Chris Lattner0a603252007-05-01 02:13:26 +00002351 Code = bitc::FUNC_CODE_INST_ALLOCA;
David Blaikiebdb49102015-04-28 16:51:01 +00002352 const AllocaInst &AI = cast<AllocaInst>(I);
2353 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
Dan Gohman9da5bb02010-05-28 01:38:28 +00002354 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002355 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002356 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2357 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2358 "not enough bits for maximum alignment");
2359 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2360 AlignRecord |= AI.isUsedWithInAlloca() << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00002361 AlignRecord |= 1 << 6;
Manman Ren9bfd0d02016-04-01 21:41:15 +00002362 AlignRecord |= AI.isSwiftError() << 7;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002363 Vals.push_back(AlignRecord);
Chris Lattner0a603252007-05-01 02:13:26 +00002364 break;
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002365 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002366
Chris Lattner0a603252007-05-01 02:13:26 +00002367 case Instruction::Load:
Eli Friedman59b66882011-08-09 23:02:53 +00002368 if (cast<LoadInst>(I).isAtomic()) {
2369 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
Teresa Johnson37687f32016-04-23 04:30:47 +00002370 pushValueAndType(I.getOperand(0), InstID, Vals);
Eli Friedman59b66882011-08-09 23:02:53 +00002371 } else {
2372 Code = bitc::FUNC_CODE_INST_LOAD;
Teresa Johnson37687f32016-04-23 04:30:47 +00002373 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
Eli Friedman59b66882011-08-09 23:02:53 +00002374 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2375 }
David Blaikie85035652015-02-25 01:07:20 +00002376 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattner0a603252007-05-01 02:13:26 +00002377 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2378 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002379 if (cast<LoadInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002380 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2381 Vals.push_back(getEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002382 }
Chris Lattner0a603252007-05-01 02:13:26 +00002383 break;
2384 case Instruction::Store:
Eli Friedman59b66882011-08-09 23:02:53 +00002385 if (cast<StoreInst>(I).isAtomic())
2386 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2387 else
2388 Code = bitc::FUNC_CODE_INST_STORE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002389 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2390 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
Chris Lattner0a603252007-05-01 02:13:26 +00002391 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2392 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman59b66882011-08-09 23:02:53 +00002393 if (cast<StoreInst>(I).isAtomic()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002394 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2395 Vals.push_back(getEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
Eli Friedman59b66882011-08-09 23:02:53 +00002396 }
Chris Lattner0a603252007-05-01 02:13:26 +00002397 break;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002398 case Instruction::AtomicCmpXchg:
2399 Code = bitc::FUNC_CODE_INST_CMPXCHG;
Teresa Johnson37687f32016-04-23 04:30:47 +00002400 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2401 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2402 pushValue(I.getOperand(2), InstID, Vals); // newval.
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002403 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002404 Vals.push_back(
2405 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2406 Vals.push_back(
2407 getEncodedSynchScope(cast<AtomicCmpXchgInst>(I).getSynchScope()));
2408 Vals.push_back(
2409 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
Tim Northover420a2162014-06-13 14:24:07 +00002410 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002411 break;
2412 case Instruction::AtomicRMW:
2413 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
Teresa Johnson37687f32016-04-23 04:30:47 +00002414 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2415 pushValue(I.getOperand(1), InstID, Vals); // val.
2416 Vals.push_back(
2417 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002418 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
Teresa Johnson37687f32016-04-23 04:30:47 +00002419 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2420 Vals.push_back(
2421 getEncodedSynchScope(cast<AtomicRMWInst>(I).getSynchScope()));
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002422 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002423 case Instruction::Fence:
2424 Code = bitc::FUNC_CODE_INST_FENCE;
Teresa Johnson37687f32016-04-23 04:30:47 +00002425 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2426 Vals.push_back(getEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
Eli Friedmanfee02c62011-07-25 23:16:38 +00002427 break;
Chris Lattner0a603252007-05-01 02:13:26 +00002428 case Instruction::Call: {
Gabor Greife9afee22010-06-26 09:35:09 +00002429 const CallInst &CI = cast<CallInst>(I);
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002430 FunctionType *FTy = CI.getFunctionType();
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002431
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002432 if (CI.hasOperandBundles())
Teresa Johnson37687f32016-04-23 04:30:47 +00002433 writeOperandBundles(&CI, InstID);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002434
Chris Lattnerc44070802011-06-17 18:17:37 +00002435 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002436
Gabor Greife9afee22010-06-26 09:35:09 +00002437 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002438
Teresa Johnson37687f32016-04-23 04:30:47 +00002439 unsigned Flags = getOptimizationFlags(&I);
Akira Hatanaka97cb3972015-11-07 02:48:49 +00002440 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2441 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2442 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2443 1 << bitc::CALL_EXPLICIT_TYPE |
Sanjay Patelfa54ace2015-12-14 21:59:03 +00002444 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2445 unsigned(Flags != 0) << bitc::CALL_FMF);
2446 if (Flags != 0)
2447 Vals.push_back(Flags);
2448
David Blaikiedbe6e0f2015-04-17 06:40:14 +00002449 Vals.push_back(VE.getTypeID(FTy));
Teresa Johnson37687f32016-04-23 04:30:47 +00002450 pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002451
Chris Lattner0a603252007-05-01 02:13:26 +00002452 // Emit value #'s for the fixed parameters.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002453 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2454 // Check for labels (can happen with asm labels).
2455 if (FTy->getParamType(i)->isLabelTy())
2456 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2457 else
Teresa Johnson37687f32016-04-23 04:30:47 +00002458 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002459 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002460
Chris Lattnerb811e952007-05-01 07:03:37 +00002461 // Emit type/value pairs for varargs params.
2462 if (FTy->isVarArg()) {
Gabor Greife9afee22010-06-26 09:35:09 +00002463 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002464 i != e; ++i)
Teresa Johnson37687f32016-04-23 04:30:47 +00002465 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
Chris Lattner0a603252007-05-01 02:13:26 +00002466 }
2467 break;
Chris Lattnerb811e952007-05-01 07:03:37 +00002468 }
Chris Lattner0a603252007-05-01 02:13:26 +00002469 case Instruction::VAArg:
2470 Code = bitc::FUNC_CODE_INST_VAARG;
2471 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
Teresa Johnson37687f32016-04-23 04:30:47 +00002472 pushValue(I.getOperand(0), InstID, Vals); // valist.
Chris Lattner0a603252007-05-01 02:13:26 +00002473 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2474 break;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002475 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002476
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002477 Stream.EmitRecord(Code, Vals, AbbrevToUse);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002478 Vals.clear();
2479}
2480
Teresa Johnson37687f32016-04-23 04:30:47 +00002481/// Emit names for globals/functions etc. \p IsModuleLevel is true when
2482/// we are writing the module-level VST, where we are including a function
2483/// bitcode index and need to backpatch the VST forward declaration record.
2484void ModuleBitcodeWriter::writeValueSymbolTable(
2485 const ValueSymbolTable &VST, bool IsModuleLevel,
2486 DenseMap<const Function *, uint64_t> *FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002487 if (VST.empty()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002488 // writeValueSymbolTableForwardDecl should have returned early as
Teresa Johnsonff642b92015-09-17 20:12:00 +00002489 // well. Ensure this handling remains in sync by asserting that
2490 // the placeholder offset is not set.
Teresa Johnson37687f32016-04-23 04:30:47 +00002491 assert(!IsModuleLevel || !hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002492 return;
2493 }
2494
Teresa Johnson37687f32016-04-23 04:30:47 +00002495 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002496 // Get the offset of the VST we are writing, and backpatch it into
2497 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002498 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002499 // The BitcodeStartBit was the stream offset of the actual bitcode
2500 // (e.g. excluding any initial darwin header).
Teresa Johnson37687f32016-04-23 04:30:47 +00002501 VSTOffset -= bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002502 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002503 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002504 }
2505
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002506 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2eae59f2007-05-04 20:34:50 +00002507
Teresa Johnsonff642b92015-09-17 20:12:00 +00002508 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2509 // records, which are not used in the per-function VSTs.
2510 unsigned FnEntry8BitAbbrev;
2511 unsigned FnEntry7BitAbbrev;
2512 unsigned FnEntry6BitAbbrev;
Teresa Johnson37687f32016-04-23 04:30:47 +00002513 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002514 // 8-bit fixed-width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002515 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2516 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002517 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2518 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002519 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2520 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002521 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002522
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002523 // 7-bit fixed width VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002524 Abbv = new BitCodeAbbrev();
2525 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002526 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2527 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002528 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2529 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002530 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002531
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002532 // 6-bit char6 VST_CODE_FNENTRY function strings.
Teresa Johnsonff642b92015-09-17 20:12:00 +00002533 Abbv = new BitCodeAbbrev();
2534 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002535 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2536 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
Teresa Johnsonff642b92015-09-17 20:12:00 +00002537 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2538 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002539 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnsonff642b92015-09-17 20:12:00 +00002540 }
2541
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002542 // FIXME: Set up the abbrev, we know how many values there are!
2543 // FIXME: We know if the type names can use 7-bit ascii.
2544 SmallVector<unsigned, 64> NameVals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002545
Yaron Keren001e2e42015-08-10 07:04:29 +00002546 for (const ValueName &Name : VST) {
Chris Lattner4d925982007-05-04 20:52:02 +00002547 // Figure out the encoding to use for the name.
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002548 StringEncoding Bits =
2549 getStringEncoding(Name.getKeyData(), Name.getKeyLength());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002550
Chris Lattnera0987962007-05-04 21:31:13 +00002551 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002552 NameVals.push_back(VE.getValueID(Name.getValue()));
2553
2554 Function *F = dyn_cast<Function>(Name.getValue());
2555 if (!F) {
2556 // If value is an alias, need to get the aliased base object to
2557 // see if it is a function.
2558 auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2559 if (GA && GA->getBaseObject())
2560 F = dyn_cast<Function>(GA->getBaseObject());
2561 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002562
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002563 // VST_CODE_ENTRY: [valueid, namechar x N]
2564 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
2565 // VST_CODE_BBENTRY: [bbid, namechar x N]
Chris Lattner6be58c62007-05-03 22:18:21 +00002566 unsigned Code;
Yaron Keren001e2e42015-08-10 07:04:29 +00002567 if (isa<BasicBlock>(Name.getValue())) {
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002568 Code = bitc::VST_CODE_BBENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002569 if (Bits == SE_Char6)
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002570 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Teresa Johnsonff642b92015-09-17 20:12:00 +00002571 } else if (F && !F->isDeclaration()) {
2572 // Must be the module-level VST, where we pass in the Index and
2573 // have a VSTOffsetPlaceholder. The function-level VST should not
2574 // contain any Function symbols.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00002575 assert(FunctionToBitcodeIndex);
Teresa Johnson37687f32016-04-23 04:30:47 +00002576 assert(hasVSTOffsetPlaceholder());
Teresa Johnsonff642b92015-09-17 20:12:00 +00002577
2578 // Save the word offset of the function (from the start of the
2579 // actual bitcode written to the stream).
Teresa Johnson37687f32016-04-23 04:30:47 +00002580 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
Teresa Johnsonff642b92015-09-17 20:12:00 +00002581 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2582 NameVals.push_back(BitcodeIndex / 32);
2583
2584 Code = bitc::VST_CODE_FNENTRY;
2585 AbbrevToUse = FnEntry8BitAbbrev;
2586 if (Bits == SE_Char6)
2587 AbbrevToUse = FnEntry6BitAbbrev;
2588 else if (Bits == SE_Fixed7)
2589 AbbrevToUse = FnEntry7BitAbbrev;
Chris Lattner6be58c62007-05-03 22:18:21 +00002590 } else {
2591 Code = bitc::VST_CODE_ENTRY;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002592 if (Bits == SE_Char6)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002593 AbbrevToUse = VST_ENTRY_6_ABBREV;
Teresa Johnsonc01e4cb2015-09-17 14:37:35 +00002594 else if (Bits == SE_Fixed7)
Chris Lattnere760d6f2007-05-05 01:26:50 +00002595 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattner6be58c62007-05-03 22:18:21 +00002596 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002597
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002598 for (const auto P : Name.getKey())
2599 NameVals.push_back((unsigned char)P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002600
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002601 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002602 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002603 NameVals.clear();
2604 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002605 Stream.ExitBlock();
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002606}
2607
Teresa Johnson403a7872015-10-04 14:33:43 +00002608/// Emit function names and summary offsets for the combined index
2609/// used by ThinLTO.
Teresa Johnson37687f32016-04-23 04:30:47 +00002610void IndexBitcodeWriter::writeCombinedValueSymbolTable() {
2611 assert(hasVSTOffsetPlaceholder() && "Expected non-zero VSTOffsetPlaceholder");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002612 // Get the offset of the VST we are writing, and backpatch it into
2613 // the VST forward declaration record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002614 uint64_t VSTOffset = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002615 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002616 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002617
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002618 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00002619
Teresa Johnson403a7872015-10-04 14:33:43 +00002620 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002621 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2622 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2623 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002624 unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002625
Teresa Johnsone1164de2016-02-10 21:55:02 +00002626 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson37687f32016-04-23 04:30:47 +00002627 for (const auto &GVI : valueIds()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002628 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
2629 NameVals.push_back(GVI.second);
2630 NameVals.push_back(GVI.first);
2631
2632 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002633 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002634 NameVals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00002635 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002636 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002637}
2638
Teresa Johnson37687f32016-04-23 04:30:47 +00002639void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002640 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2641 unsigned Code;
2642 if (isa<BasicBlock>(Order.V))
2643 Code = bitc::USELIST_CODE_BB;
2644 else
2645 Code = bitc::USELIST_CODE_DEFAULT;
2646
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002647 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002648 Record.push_back(VE.getValueID(Order.V));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002649 Stream.EmitRecord(Code, Record);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002650}
2651
Teresa Johnson37687f32016-04-23 04:30:47 +00002652void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002653 assert(VE.shouldPreserveUseListOrder() &&
2654 "Expected to be preserving use-list order");
2655
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002656 auto hasMore = [&]() {
2657 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2658 };
2659 if (!hasMore())
2660 // Nothing to do.
2661 return;
2662
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002663 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002664 while (hasMore()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002665 writeUseList(std::move(VE.UseListOrders.back()));
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002666 VE.UseListOrders.pop_back();
2667 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002668 Stream.ExitBlock();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002669}
2670
Teresa Johnson403a7872015-10-04 14:33:43 +00002671/// Emit a function body to the module stream.
Teresa Johnson37687f32016-04-23 04:30:47 +00002672void ModuleBitcodeWriter::writeFunction(
2673 const Function &F,
2674 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00002675 // Save the bitcode index of the start of this function block for recording
2676 // in the VST.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002677 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00002678
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002679 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a11b642011-06-03 17:02:19 +00002680 VE.incorporateFunction(F);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002681
2682 SmallVector<unsigned, 64> Vals;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002683
Chris Lattnere6e364c2007-04-26 05:53:54 +00002684 // Emit the number of basic blocks, so the reader can create them ahead of
2685 // time.
2686 Vals.push_back(VE.getBasicBlocks().size());
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002687 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
Chris Lattnere6e364c2007-04-26 05:53:54 +00002688 Vals.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002689
Chris Lattnere6e364c2007-04-26 05:53:54 +00002690 // If there are function-local constants, emit them now.
2691 unsigned CstStart, CstEnd;
2692 VE.getFunctionConstantRange(CstStart, CstEnd);
Teresa Johnson37687f32016-04-23 04:30:47 +00002693 writeConstants(CstStart, CstEnd, false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002694
Victor Hernandez6c730de2010-01-14 01:50:08 +00002695 // If there is function-local metadata, emit it now.
Teresa Johnson37687f32016-04-23 04:30:47 +00002696 writeFunctionMetadata(F);
Victor Hernandez6c730de2010-01-14 01:50:08 +00002697
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002698 // Keep a running idea of what the instruction ID is.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002699 unsigned InstID = CstEnd;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002700
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002701 bool NeedsMetadataAttachment = F.hasMetadata();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002702
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002703 DILocation *LastDL = nullptr;
Chris Lattnere6e364c2007-04-26 05:53:54 +00002704 // Finally, emit all the instructions, in order.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002705 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002706 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2707 I != E; ++I) {
Teresa Johnson37687f32016-04-23 04:30:47 +00002708 writeInstruction(*I, InstID, Vals);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002709
Chris Lattner2f2aa2b2009-12-28 23:41:32 +00002710 if (!I->getType()->isVoidTy())
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002711 ++InstID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002712
Chris Lattner07d09ed2010-04-03 02:17:50 +00002713 // If the instruction has metadata, write a metadata attachment later.
2714 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002715
Chris Lattner07d09ed2010-04-03 02:17:50 +00002716 // If the instruction has a debug location, emit it.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002717 DILocation *DL = I->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002718 if (!DL)
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002719 continue;
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002720
2721 if (DL == LastDL) {
Chris Lattner07d09ed2010-04-03 02:17:50 +00002722 // Just repeat the same debug loc as last time.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002723 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002724 continue;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002725 }
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002726
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002727 Vals.push_back(DL->getLine());
2728 Vals.push_back(DL->getColumn());
2729 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2730 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002731 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Duncan P. N. Exon Smith1facf7a2015-03-30 18:29:18 +00002732 Vals.clear();
Duncan P. N. Exon Smith538ef562015-05-06 22:51:12 +00002733
2734 LastDL = DL;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002735 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002736
Chris Lattnerfb6f9402007-05-01 02:14:57 +00002737 // Emit names for all the instructions etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002738 writeValueSymbolTable(F.getValueSymbolTable());
Devang Patelaf206b82009-09-18 19:26:43 +00002739
Chris Lattner07d09ed2010-04-03 02:17:50 +00002740 if (NeedsMetadataAttachment)
Teresa Johnson37687f32016-04-23 04:30:47 +00002741 writeMetadataAttachment(F);
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00002742 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00002743 writeUseListBlock(&F);
Chad Rosier6a11b642011-06-03 17:02:19 +00002744 VE.purgeFunction();
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002745 Stream.ExitBlock();
Chris Lattner831d4202007-04-26 03:27:58 +00002746}
2747
Chris Lattner702658c2007-05-04 18:26:27 +00002748// Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00002749void ModuleBitcodeWriter::writeBlockInfo() {
Chris Lattner702658c2007-05-04 18:26:27 +00002750 // We only want to emit block info records for blocks that have multiple
Jan Wen Voungafaced02012-10-11 20:20:40 +00002751 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
Jan Wen Voung8c9e9412012-10-11 21:45:16 +00002752 // Other blocks can define their abbrevs inline.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002753 Stream.EnterBlockInfoBlock(2);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002754
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002755 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002756 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2757 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2758 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2759 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2760 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002761 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002762 VST_ENTRY_8_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002763 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002764 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002765
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002766 { // 7-bit fixed width VST_CODE_ENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002767 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2768 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2769 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2770 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2771 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002772 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002773 VST_ENTRY_7_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002774 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002775 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002776 { // 6-bit char6 VST_CODE_ENTRY strings.
Chris Lattnere760d6f2007-05-05 01:26:50 +00002777 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2778 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2780 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2781 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002782 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002783 VST_ENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002784 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere760d6f2007-05-05 01:26:50 +00002785 }
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00002786 { // 6-bit char6 VST_CODE_BBENTRY strings.
Chris Lattner982ec1e2007-05-05 00:17:00 +00002787 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2788 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2790 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnere760d6f2007-05-05 01:26:50 +00002791 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
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_BBENTRY_6_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002794 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner982ec1e2007-05-05 00:17:00 +00002795 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002796
2797
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002798
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002799 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2800 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2801 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2802 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
David Blaikie7b028102015-02-25 00:51:52 +00002803 VE.computeBitsRequiredForTypeIndicies()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002804 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002805 CONSTANTS_SETTYPE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002806 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002807 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002808
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002809 { // INTEGER abbrev for CONSTANTS_BLOCK.
2810 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2811 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2812 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002813 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002814 CONSTANTS_INTEGER_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002815 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002816 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002817
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002818 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2819 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2820 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2821 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2822 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
David Blaikie7b028102015-02-25 00:51:52 +00002823 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002824 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2825
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002826 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002827 CONSTANTS_CE_CAST_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002828 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002829 }
2830 { // NULL abbrev for CONSTANTS_BLOCK.
2831 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2832 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002833 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002834 CONSTANTS_NULL_Abbrev)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002835 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002836 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002837
Chris Lattnerb80751d2007-05-05 07:44:49 +00002838 // FIXME: This should only use space for first class types!
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002839
Chris Lattnerb80751d2007-05-05 07:44:49 +00002840 { // INST_LOAD abbrev for FUNCTION_BLOCK.
2841 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2842 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattnerb80751d2007-05-05 07:44:49 +00002843 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
David Blaikie85035652015-02-25 01:07:20 +00002844 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2845 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerb80751d2007-05-05 07:44:49 +00002846 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
2847 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002848 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002849 FUNCTION_INST_LOAD_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002850 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerb80751d2007-05-05 07:44:49 +00002851 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002852 { // INST_BINOP abbrev for FUNCTION_BLOCK.
2853 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2854 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2855 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2856 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2857 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002858 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002859 FUNCTION_INST_BINOP_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002860 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002861 }
Dan Gohman0ebd6962009-07-20 21:19:07 +00002862 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2863 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2864 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2865 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2866 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2867 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2868 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002869 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002870 FUNCTION_INST_BINOP_FLAGS_ABBREV)
Dan Gohman0ebd6962009-07-20 21:19:07 +00002871 llvm_unreachable("Unexpected abbrev ordering!");
2872 }
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002873 { // INST_CAST abbrev for FUNCTION_BLOCK.
2874 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2875 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2876 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2877 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
David Blaikie7b028102015-02-25 00:51:52 +00002878 VE.computeBitsRequiredForTypeIndicies()));
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002879 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002880 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002881 FUNCTION_INST_CAST_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002882 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerc67e6d92007-05-06 02:38:57 +00002883 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002884
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002885 { // INST_RET abbrev for FUNCTION_BLOCK.
2886 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2887 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002888 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002889 FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002890 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002891 }
2892 { // INST_RET abbrev for FUNCTION_BLOCK.
2893 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2894 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2895 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002896 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002897 FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002898 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002899 }
2900 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2901 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2902 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002903 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
Teresa Johnson37687f32016-04-23 04:30:47 +00002904 FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002905 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnercc6d4c92007-05-06 01:28:01 +00002906 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00002907 {
2908 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2909 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2910 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2911 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2912 Log2_32_Ceil(VE.getTypes().size() + 1)));
2913 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2914 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002915 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
David Blaikieb5b5efd2015-02-25 01:08:52 +00002916 FUNCTION_INST_GEP_ABBREV)
2917 llvm_unreachable("Unexpected abbrev ordering!");
2918 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002919
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002920 Stream.ExitBlock();
Chris Lattnerda5e5d22007-05-05 07:36:14 +00002921}
2922
Teresa Johnson403a7872015-10-04 14:33:43 +00002923/// Write the module path strings, currently only used when generating
2924/// a combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00002925void IndexBitcodeWriter::writeModStrings() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002926 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
Teresa Johnson403a7872015-10-04 14:33:43 +00002927
2928 // TODO: See which abbrev sizes we actually need to emit
2929
2930 // 8-bit fixed-width MST_ENTRY strings.
2931 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2932 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2933 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2934 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2935 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002936 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002937
2938 // 7-bit fixed width MST_ENTRY strings.
2939 Abbv = new BitCodeAbbrev();
2940 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2941 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2942 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2943 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002944 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002945
2946 // 6-bit char6 MST_ENTRY strings.
2947 Abbv = new BitCodeAbbrev();
2948 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2949 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2950 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2951 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002952 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00002953
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002954 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
2955 Abbv = new BitCodeAbbrev();
2956 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
2957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2960 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2961 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002962 unsigned AbbrevHash = Stream.EmitAbbrev(Abbv);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002963
2964 SmallVector<unsigned, 64> Vals;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002965 for (const auto &MPSE : Index.modulePaths()) {
Teresa Johnson403a7872015-10-04 14:33:43 +00002966 StringEncoding Bits =
2967 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
2968 unsigned AbbrevToUse = Abbrev8Bit;
2969 if (Bits == SE_Char6)
2970 AbbrevToUse = Abbrev6Bit;
2971 else if (Bits == SE_Fixed7)
2972 AbbrevToUse = Abbrev7Bit;
2973
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002974 Vals.push_back(MPSE.getValue().first);
Teresa Johnson403a7872015-10-04 14:33:43 +00002975
Teresa Johnsonf72278f2015-11-02 18:02:11 +00002976 for (const auto P : MPSE.getKey())
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002977 Vals.push_back((unsigned char)P);
Teresa Johnson403a7872015-10-04 14:33:43 +00002978
2979 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002980 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002981
2982 Vals.clear();
2983 // Emit an optional hash for the module now
2984 auto &Hash = MPSE.getValue().second;
2985 bool AllZero = true; // Detect if the hash is empty, and do not generate it
2986 for (auto Val : Hash) {
2987 if (Val)
2988 AllZero = false;
2989 Vals.push_back(Val);
2990 }
2991 if (!AllZero) {
2992 // Emit the hash record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002993 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00002994 }
2995
2996 Vals.clear();
Teresa Johnson403a7872015-10-04 14:33:43 +00002997 }
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00002998 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00002999}
3000
3001// Helper to emit a single function summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003002void ModuleBitcodeWriter::writePerModuleFunctionSummaryRecord(
Teresa Johnson28e457b2016-04-24 14:57:11 +00003003 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
Teresa Johnson37687f32016-04-23 04:30:47 +00003004 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3005 const Function &F) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003006 NameVals.push_back(ValueID);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003007
Teresa Johnson28e457b2016-04-24 14:57:11 +00003008 FunctionSummary *FS = cast<FunctionSummary>(Summary);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003009 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003010 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003011 NameVals.push_back(FS->refs().size());
3012
3013 for (auto &RI : FS->refs())
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003014 NameVals.push_back(VE.getValueID(RI.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003015
3016 bool HasProfileData = F.getEntryCount().hasValue();
Teresa Johnsonaae26102016-03-25 18:59:13 +00003017 for (auto &ECI : FS->calls()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003018 NameVals.push_back(VE.getValueID(ECI.first.getValue()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003019 assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite");
3020 NameVals.push_back(ECI.second.CallsiteCount);
3021 if (HasProfileData)
3022 NameVals.push_back(ECI.second.ProfileCount);
3023 }
3024
3025 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3026 unsigned Code =
3027 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
Teresa Johnson403a7872015-10-04 14:33:43 +00003028
3029 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003030 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003031 NameVals.clear();
3032}
3033
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003034// Collect the global value references in the given variable's initializer,
3035// and emit them in a summary record.
Teresa Johnson37687f32016-04-23 04:30:47 +00003036void ModuleBitcodeWriter::writeModuleLevelReferences(
3037 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3038 unsigned FSModRefsAbbrev) {
Teresa Johnson13968092016-03-15 19:35:45 +00003039 // Only interested in recording variable defs in the summary.
3040 if (V.isDeclaration())
3041 return;
Teresa Johnson13968092016-03-15 19:35:45 +00003042 NameVals.push_back(VE.getValueID(&V));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003043 NameVals.push_back(getEncodedGVSummaryFlags(V));
Teresa Johnson28e457b2016-04-24 14:57:11 +00003044 auto *Summary = Index->getGlobalValueSummary(V);
3045 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003046 for (auto Ref : VS->refs())
3047 NameVals.push_back(VE.getValueID(Ref.getValue()));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003048 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
3049 FSModRefsAbbrev);
Teresa Johnson13968092016-03-15 19:35:45 +00003050 NameVals.clear();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003051}
Teresa Johnson403a7872015-10-04 14:33:43 +00003052
Mehdi Amini8fe69362016-04-24 03:18:11 +00003053// Current version for the summary.
3054// This is bumped whenever we introduce changes in the way some record are
3055// interpreted, like flags for instance.
3056static const uint64_t INDEX_VERSION = 1;
3057
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003058/// Emit the per-module summary section alongside the rest of
3059/// the module's bitcode.
Teresa Johnson37687f32016-04-23 04:30:47 +00003060void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003061 if (M.empty())
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003062 return;
3063
Teresa Johnson37687f32016-04-23 04:30:47 +00003064 if (Index->begin() == Index->end())
Teresa Johnsonb35cc692016-04-20 14:39:45 +00003065 return;
3066
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003067 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 4);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003068
Mehdi Amini8fe69362016-04-24 03:18:11 +00003069 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
3070
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003071 // Abbrev for FS_PERMODULE.
Teresa Johnson403a7872015-10-04 14:33:43 +00003072 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003073 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003074 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003075 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003076 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003077 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3078 // numrefs x valueid, n x (valueid, callsitecount)
3079 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3080 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003081 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003082
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003083 // Abbrev for FS_PERMODULE_PROFILE.
3084 Abbv = new BitCodeAbbrev();
3085 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
3086 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003087 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003088 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3089 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3090 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3091 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3092 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003093 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003094
3095 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3096 Abbv = new BitCodeAbbrev();
3097 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
3098 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003099 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003100 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3101 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003102 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003103
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003104 // Abbrev for FS_ALIAS.
3105 Abbv = new BitCodeAbbrev();
3106 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3107 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003108 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003109 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003110 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003111
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003112 SmallVector<uint64_t, 64> NameVals;
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003113 // Iterate over the list of functions instead of the Index to
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003114 // ensure the ordering is stable.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003115 for (const Function &F : M) {
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003116 if (F.isDeclaration())
3117 continue;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003118 // Summary emission does not support anonymous functions, they have to
3119 // renamed using the anonymous function renaming pass.
Teresa Johnson2d9da4dc2016-02-01 20:16:35 +00003120 if (!F.hasName())
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003121 report_fatal_error("Unexpected anonymous function when writing summary");
Teresa Johnson403a7872015-10-04 14:33:43 +00003122
Teresa Johnson28e457b2016-04-24 14:57:11 +00003123 auto *Summary = Index->getGlobalValueSummary(F);
Teresa Johnson37687f32016-04-23 04:30:47 +00003124 writePerModuleFunctionSummaryRecord(
Teresa Johnson28e457b2016-04-24 14:57:11 +00003125 NameVals, Summary,
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003126 VE.getValueID(M.getValueSymbolTable().lookup(F.getName())),
Teresa Johnson37687f32016-04-23 04:30:47 +00003127 FSCallsAbbrev, FSCallsProfileAbbrev, F);
Teresa Johnson403a7872015-10-04 14:33:43 +00003128 }
3129
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003130 // Capture references from GlobalVariable initializers, which are outside
3131 // of a function scope.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003132 for (const GlobalVariable &G : M.globals())
Teresa Johnson37687f32016-04-23 04:30:47 +00003133 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003134
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003135 for (const GlobalAlias &A : M.aliases()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003136 auto *Aliasee = A.getBaseObject();
3137 if (!Aliasee->hasName())
3138 // Nameless function don't have an entry in the summary, skip it.
3139 continue;
3140 auto AliasId = VE.getValueID(&A);
3141 auto AliaseeId = VE.getValueID(Aliasee);
3142 NameVals.push_back(AliasId);
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003143 NameVals.push_back(getEncodedGVSummaryFlags(A));
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003144 NameVals.push_back(AliaseeId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003145 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003146 NameVals.clear();
3147 }
3148
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003149 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003150}
3151
Teresa Johnson26ab5772016-03-15 00:04:37 +00003152/// Emit the combined summary section into the combined index file.
Teresa Johnson37687f32016-04-23 04:30:47 +00003153void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003154 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
Mehdi Amini8fe69362016-04-24 03:18:11 +00003155 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
Teresa Johnson403a7872015-10-04 14:33:43 +00003156
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003157 // Abbrev for FS_COMBINED.
Teresa Johnson403a7872015-10-04 14:33:43 +00003158 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003159 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
Teresa Johnson02e98332016-04-27 13:28:35 +00003160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003161 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003162 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003163 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3164 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3165 // numrefs x valueid, n x (valueid, callsitecount)
3166 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3167 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003168 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson403a7872015-10-04 14:33:43 +00003169
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003170 // Abbrev for FS_COMBINED_PROFILE.
3171 Abbv = new BitCodeAbbrev();
3172 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
Teresa Johnson02e98332016-04-27 13:28:35 +00003173 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003174 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003175 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003176 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3177 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3178 // numrefs x valueid, n x (valueid, callsitecount, profilecount)
3179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3180 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003181 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003182
3183 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
3184 Abbv = new BitCodeAbbrev();
3185 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003186 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003189 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3190 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003191 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003192
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003193 // Abbrev for FS_COMBINED_ALIAS.
3194 Abbv = new BitCodeAbbrev();
3195 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
Teresa Johnson02e98332016-04-27 13:28:35 +00003196 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003197 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003198 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
Teresa Johnson02e98332016-04-27 13:28:35 +00003199 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003200 unsigned FSAliasAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003201
Teresa Johnson02e98332016-04-27 13:28:35 +00003202 // The aliases are emitted as a post-pass, and will point to the value
3203 // id of the aliasee. Save them in a vector for post-processing.
Teresa Johnson28e457b2016-04-24 14:57:11 +00003204 SmallVector<AliasSummary *, 64> Aliases;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003205
Teresa Johnson02e98332016-04-27 13:28:35 +00003206 // Save the value id for each summary for alias emission.
3207 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
3208
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003209 SmallVector<uint64_t, 64> NameVals;
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003210
3211 // For local linkage, we also emit the original name separately
3212 // immediately after the record.
3213 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3214 if (!GlobalValue::isLocalLinkage(S.linkage()))
3215 return;
3216 NameVals.push_back(S.getOriginalName());
3217 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3218 NameVals.clear();
3219 };
3220
Teresa Johnson28e457b2016-04-24 14:57:11 +00003221 for (const auto &GSI : Index) {
3222 for (auto &SI : GSI.second) {
3223 GlobalValueSummary *S = SI.get();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003224 assert(S);
Teresa Johnson02e98332016-04-27 13:28:35 +00003225
3226 assert(hasValueId(GSI.first));
3227 unsigned ValueId = getValueId(GSI.first);
3228 SummaryToValueIdMap[S] = ValueId;
3229
Teresa Johnson28e457b2016-04-24 14:57:11 +00003230 if (auto *AS = dyn_cast<AliasSummary>(S)) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003231 // Will process aliases as a post-pass because the reader wants all
3232 // global to be loaded first.
Teresa Johnson28e457b2016-04-24 14:57:11 +00003233 Aliases.push_back(AS);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003234 continue;
3235 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003236
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003237 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003238 NameVals.push_back(ValueId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003239 NameVals.push_back(Index.getModuleId(VS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003240 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003241 for (auto &RI : VS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003242 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003243 }
3244
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003245 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003246 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
3247 FSModRefsAbbrev);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003248 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003249 MaybeEmitOriginalName(*S);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003250 continue;
3251 }
3252
Teresa Johnson2794f712016-03-15 02:41:29 +00003253 auto *FS = cast<FunctionSummary>(S);
Teresa Johnson02e98332016-04-27 13:28:35 +00003254 NameVals.push_back(ValueId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003255 NameVals.push_back(Index.getModuleId(FS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003256 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
Teresa Johnson403a7872015-10-04 14:33:43 +00003257 NameVals.push_back(FS->instCount());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003258 NameVals.push_back(FS->refs().size());
3259
3260 for (auto &RI : FS->refs()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003261 NameVals.push_back(getValueId(RI.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003262 }
3263
3264 bool HasProfileData = false;
Teresa Johnsonaae26102016-03-25 18:59:13 +00003265 for (auto &EI : FS->calls()) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003266 HasProfileData |= EI.second.ProfileCount != 0;
3267 if (HasProfileData)
3268 break;
3269 }
3270
Teresa Johnsonaae26102016-03-25 18:59:13 +00003271 for (auto &EI : FS->calls()) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003272 // If this GUID doesn't have a value id, it doesn't have a function
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003273 // summary and we don't need to record any calls to it.
Teresa Johnson37687f32016-04-23 04:30:47 +00003274 if (!hasValueId(EI.first.getGUID()))
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003275 continue;
Teresa Johnson37687f32016-04-23 04:30:47 +00003276 NameVals.push_back(getValueId(EI.first.getGUID()));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003277 assert(EI.second.CallsiteCount > 0 && "Expected at least one callsite");
3278 NameVals.push_back(EI.second.CallsiteCount);
3279 if (HasProfileData)
3280 NameVals.push_back(EI.second.ProfileCount);
3281 }
Teresa Johnson403a7872015-10-04 14:33:43 +00003282
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003283 unsigned FSAbbrev =
3284 (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3285 unsigned Code =
3286 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
3287
Teresa Johnson403a7872015-10-04 14:33:43 +00003288 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003289 Stream.EmitRecord(Code, NameVals, FSAbbrev);
Teresa Johnson403a7872015-10-04 14:33:43 +00003290 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003291 MaybeEmitOriginalName(*S);
Teresa Johnson403a7872015-10-04 14:33:43 +00003292 }
3293 }
3294
Teresa Johnson28e457b2016-04-24 14:57:11 +00003295 for (auto *AS : Aliases) {
Teresa Johnson02e98332016-04-27 13:28:35 +00003296 auto AliasValueId = SummaryToValueIdMap[AS];
3297 assert(AliasValueId);
3298 NameVals.push_back(AliasValueId);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003299 NameVals.push_back(Index.getModuleId(AS->modulePath()));
Mehdi Aminic3ed48c2016-04-24 03:18:18 +00003300 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
Teresa Johnson02e98332016-04-27 13:28:35 +00003301 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
3302 assert(AliaseeValueId);
3303 NameVals.push_back(AliaseeValueId);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003304
3305 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003306 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003307 NameVals.clear();
Mehdi Aminiae64eaf2016-04-23 23:38:17 +00003308 MaybeEmitOriginalName(*AS);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +00003309 }
3310
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003311 Stream.ExitBlock();
Teresa Johnson403a7872015-10-04 14:33:43 +00003312}
3313
Teresa Johnson37687f32016-04-23 04:30:47 +00003314void ModuleBitcodeWriter::writeIdentificationBlock() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003315 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
Mehdi Amini5d303282015-10-26 18:37:00 +00003316
3317 // Write the "user readable" string identifying the bitcode producer
3318 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
3319 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
3320 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
3321 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003322 auto StringAbbrev = Stream.EmitAbbrev(Abbv);
Teresa Johnson37687f32016-04-23 04:30:47 +00003323 writeStringRecord(bitc::IDENTIFICATION_CODE_STRING,
3324 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
Mehdi Amini5d303282015-10-26 18:37:00 +00003325
3326 // Write the epoch version
3327 Abbv = new BitCodeAbbrev();
3328 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
3329 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003330 auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
Mehdi Amini5d303282015-10-26 18:37:00 +00003331 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003332 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
3333 Stream.ExitBlock();
Mehdi Amini5d303282015-10-26 18:37:00 +00003334}
3335
Teresa Johnson37687f32016-04-23 04:30:47 +00003336void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003337 // Emit the module's hash.
3338 // MODULE_CODE_HASH: [5*i32]
3339 SHA1 Hasher;
Sjoerd Meijer41beee62016-04-27 18:35:02 +00003340 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003341 Buffer.size() - BlockStartPos));
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003342 auto Hash = Hasher.result();
3343 SmallVector<uint64_t, 20> Vals;
3344 auto LShift = [&](unsigned char Val, unsigned Amount)
3345 -> uint64_t { return ((uint64_t)Val) << Amount; };
3346 for (int Pos = 0; Pos < 20; Pos += 4) {
3347 uint32_t SubHash = LShift(Hash[Pos + 0], 24);
3348 SubHash |= LShift(Hash[Pos + 1], 16) | LShift(Hash[Pos + 2], 8) |
3349 (unsigned)(unsigned char)Hash[Pos + 3];
3350 Vals.push_back(SubHash);
3351 }
3352
3353 // Emit the finished record.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003354 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003355}
3356
Teresa Johnson37687f32016-04-23 04:30:47 +00003357void BitcodeWriter::write() {
3358 // Emit the file header first.
3359 writeBitcodeHeader();
3360
3361 writeBlocks();
3362}
3363
3364void ModuleBitcodeWriter::writeBlocks() {
3365 writeIdentificationBlock();
3366 writeModule();
3367}
3368
3369void IndexBitcodeWriter::writeBlocks() {
3370 // Index contains only a single outer (module) block.
3371 writeIndex();
3372}
3373
3374void ModuleBitcodeWriter::writeModule() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003375 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3376 size_t BlockStartPos = Buffer.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003377
Jan Wen Voungafaced02012-10-11 20:20:40 +00003378 SmallVector<unsigned, 1> Vals;
3379 unsigned CurVersion = 1;
3380 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003381 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003382
3383 // Emit blockinfo, which defines the standard abbreviations etc.
Teresa Johnson37687f32016-04-23 04:30:47 +00003384 writeBlockInfo();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003385
Bill Wendlingdc095552013-02-10 23:09:32 +00003386 // Emit information about attribute groups.
Teresa Johnson37687f32016-04-23 04:30:47 +00003387 writeAttributeGroupTable();
Bill Wendlingdc095552013-02-10 23:09:32 +00003388
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003389 // Emit information about parameter attributes.
Teresa Johnson37687f32016-04-23 04:30:47 +00003390 writeAttributeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003391
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003392 // Emit information describing all of the types in the module.
Teresa Johnson37687f32016-04-23 04:30:47 +00003393 writeTypeTable();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003394
Teresa Johnson37687f32016-04-23 04:30:47 +00003395 writeComdats();
David Majnemerdad0a642014-06-27 18:19:56 +00003396
Chris Lattnerda5e5d22007-05-05 07:36:14 +00003397 // Emit top-level description of module, including target triple, inline asm,
3398 // descriptors for global variables, and function prototype info.
Teresa Johnson37687f32016-04-23 04:30:47 +00003399 writeModuleInfo();
Devang Patel7428d8a2009-07-22 17:43:22 +00003400
Devang Patele059ba6e2009-07-23 01:07:34 +00003401 // Emit constants.
Teresa Johnson37687f32016-04-23 04:30:47 +00003402 writeModuleConstants();
Devang Patele059ba6e2009-07-23 01:07:34 +00003403
Devang Patel8cca7b42009-08-04 05:01:35 +00003404 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003405 writeModuleMetadata();
Devang Patel8cca7b42009-08-04 05:01:35 +00003406
Devang Patelaf206b82009-09-18 19:26:43 +00003407 // Emit metadata.
Teresa Johnson37687f32016-04-23 04:30:47 +00003408 writeModuleMetadataStore();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003409
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003410 // Emit module-level use-lists.
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +00003411 if (VE.shouldPreserveUseListOrder())
Teresa Johnson37687f32016-04-23 04:30:47 +00003412 writeUseListBlock(nullptr);
Chad Rosierca2567b2011-12-07 21:44:12 +00003413
Teresa Johnson37687f32016-04-23 04:30:47 +00003414 writeOperandBundleTags();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003415
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003416 // Emit function bodies.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003417 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003418 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003419 if (!F->isDeclaration())
Teresa Johnson37687f32016-04-23 04:30:47 +00003420 writeFunction(*F, FunctionToBitcodeIndex);
Teresa Johnson403a7872015-10-04 14:33:43 +00003421
3422 // Need to write after the above call to WriteFunction which populates
3423 // the summary information in the index.
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003424 if (Index)
Teresa Johnson37687f32016-04-23 04:30:47 +00003425 writePerModuleGlobalValueSummary();
Teresa Johnsonff642b92015-09-17 20:12:00 +00003426
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003427 writeValueSymbolTable(M.getValueSymbolTable(),
Teresa Johnson37687f32016-04-23 04:30:47 +00003428 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003429
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003430 if (GenerateHash) {
Teresa Johnson37687f32016-04-23 04:30:47 +00003431 writeModuleHash(BlockStartPos);
Mehdi Aminid7ad2212016-04-01 05:33:11 +00003432 }
3433
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003434 Stream.ExitBlock();
Chris Lattner702658c2007-05-04 18:26:27 +00003435}
3436
Teresa Johnson37687f32016-04-23 04:30:47 +00003437static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
3438 uint32_t &Position) {
3439 support::endian::write32le(&Buffer[Position], Value);
3440 Position += 4;
3441}
3442
3443/// If generating a bc file on darwin, we have to emit a
Chris Lattnera660f4b2008-07-09 05:14:23 +00003444/// header and trailer to make it compatible with the system archiver. To do
3445/// this we emit the following header, and then emit a trailer that pads the
3446/// file out to be a multiple of 16 bytes.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003447///
Chris Lattnera660f4b2008-07-09 05:14:23 +00003448/// struct bc_header {
3449/// uint32_t Magic; // 0x0B17C0DE
3450/// uint32_t Version; // Version, currently always 0.
3451/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
3452/// uint32_t BitcodeSize; // Size of traditional bitcode file.
3453/// uint32_t CPUType; // CPU specifier.
3454/// ... potentially more later ...
3455/// };
Teresa Johnson37687f32016-04-23 04:30:47 +00003456static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003457 const Triple &TT) {
Chris Lattnera660f4b2008-07-09 05:14:23 +00003458 unsigned CPUType = ~0U;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003459
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003460 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng545d3602010-02-12 20:39:35 +00003461 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
3462 // number from /usr/include/mach/machine.h. It is ok to reproduce the
3463 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattnera660f4b2008-07-09 05:14:23 +00003464 enum {
3465 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
3466 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003467 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattnera660f4b2008-07-09 05:14:23 +00003468 DARWIN_CPU_TYPE_POWERPC = 18
3469 };
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003470
Evan Chengcffdcae2011-06-14 01:51:33 +00003471 Triple::ArchType Arch = TT.getArch();
3472 if (Arch == Triple::x86_64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003473 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003474 else if (Arch == Triple::x86)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003475 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chengcffdcae2011-06-14 01:51:33 +00003476 else if (Arch == Triple::ppc)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003477 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chengcffdcae2011-06-14 01:51:33 +00003478 else if (Arch == Triple::ppc64)
Chris Lattnera660f4b2008-07-09 05:14:23 +00003479 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chengcffdcae2011-06-14 01:51:33 +00003480 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng9aa30fb2010-02-12 20:13:44 +00003481 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003482
Chris Lattnera660f4b2008-07-09 05:14:23 +00003483 // Traditional Bitcode starts after header.
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003484 assert(Buffer.size() >= BWH_HeaderSize &&
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003485 "Expected header size to be reserved");
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003486 unsigned BCOffset = BWH_HeaderSize;
3487 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003488
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003489 // Write the magic and version.
3490 unsigned Position = 0;
Teresa Johnson37687f32016-04-23 04:30:47 +00003491 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
3492 writeInt32ToBuffer(0, Buffer, Position); // Version.
3493 writeInt32ToBuffer(BCOffset, Buffer, Position);
3494 writeInt32ToBuffer(BCSize, Buffer, Position);
3495 writeInt32ToBuffer(CPUType, Buffer, Position);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003496
Chris Lattnera660f4b2008-07-09 05:14:23 +00003497 // If the file is not a multiple of 16 bytes, insert dummy padding.
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003498 while (Buffer.size() & 15)
3499 Buffer.push_back(0);
Chris Lattnera660f4b2008-07-09 05:14:23 +00003500}
3501
Teresa Johnson403a7872015-10-04 14:33:43 +00003502/// Helper to write the header common to all bitcode files.
Teresa Johnson37687f32016-04-23 04:30:47 +00003503void BitcodeWriter::writeBitcodeHeader() {
Teresa Johnson403a7872015-10-04 14:33:43 +00003504 // Emit the file header.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003505 Stream.Emit((unsigned)'B', 8);
3506 Stream.Emit((unsigned)'C', 8);
3507 Stream.Emit(0x0, 4);
3508 Stream.Emit(0xC, 4);
3509 Stream.Emit(0xE, 4);
3510 Stream.Emit(0xD, 4);
Teresa Johnson403a7872015-10-04 14:33:43 +00003511}
3512
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003513/// WriteBitcodeToFile - Write the specified module to the specified output
3514/// stream.
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +00003515void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
Teresa Johnson403a7872015-10-04 14:33:43 +00003516 bool ShouldPreserveUseListOrder,
Teresa Johnson2d5487c2016-04-11 13:58:45 +00003517 const ModuleSummaryIndex *Index,
3518 bool GenerateHash) {
Michael Ilsemane26658d2012-12-03 21:29:36 +00003519 SmallVector<char, 0> Buffer;
Chris Lattnerc1d10d62007-04-22 06:24:45 +00003520 Buffer.reserve(256*1024);
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003521
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003522 // If this is darwin or another generic macho target, reserve space for the
3523 // header.
3524 Triple TT(M->getTargetTriple());
Akira Hatanaka1235d282016-01-23 16:02:10 +00003525 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Akira Hatanaka4f472a882016-01-29 05:55:09 +00003526 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003527
3528 // Emit the module into the buffer.
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003529 ModuleBitcodeWriter ModuleWriter(M, Buffer, ShouldPreserveUseListOrder, Index,
3530 GenerateHash);
Teresa Johnson37687f32016-04-23 04:30:47 +00003531 ModuleWriter.write();
Daniel Dunbar6e45c022012-02-29 20:31:01 +00003532
Akira Hatanaka1235d282016-01-23 16:02:10 +00003533 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Teresa Johnson37687f32016-04-23 04:30:47 +00003534 emitDarwinBCHeaderAndTrailer(Buffer, TT);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003535
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003536 // Write the generated bitstream to "Out".
3537 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattneraf4c0bf2008-12-19 18:37:59 +00003538}
Teresa Johnson403a7872015-10-04 14:33:43 +00003539
Teresa Johnson37687f32016-04-23 04:30:47 +00003540void IndexBitcodeWriter::writeIndex() {
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003541 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Teresa Johnson37687f32016-04-23 04:30:47 +00003542
3543 SmallVector<unsigned, 1> Vals;
3544 unsigned CurVersion = 1;
3545 Vals.push_back(CurVersion);
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003546 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Teresa Johnson37687f32016-04-23 04:30:47 +00003547
3548 // If we have a VST, write the VSTOFFSET record placeholder.
3549 writeValueSymbolTableForwardDecl();
3550
3551 // Write the module paths in the combined index.
3552 writeModStrings();
3553
3554 // Write the summary combined index records.
3555 writeCombinedGlobalValueSummary();
3556
3557 // Need a special VST writer for the combined index (we don't have a
3558 // real VST and real values when this is invoked).
3559 writeCombinedValueSymbolTable();
3560
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003561 Stream.ExitBlock();
Teresa Johnson37687f32016-04-23 04:30:47 +00003562}
3563
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00003564// Write the specified module summary index to the given raw output stream,
Teresa Johnson403a7872015-10-04 14:33:43 +00003565// where it will be written in a new bitcode block. This is used when
3566// writing the combined index file for ThinLTO.
Teresa Johnson26ab5772016-03-15 00:04:37 +00003567void llvm::WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out) {
Teresa Johnson403a7872015-10-04 14:33:43 +00003568 SmallVector<char, 0> Buffer;
3569 Buffer.reserve(256 * 1024);
3570
Teresa Johnsonc814e0c2016-04-23 04:31:20 +00003571 IndexBitcodeWriter IndexWriter(Buffer, Index);
Teresa Johnson37687f32016-04-23 04:30:47 +00003572 IndexWriter.write();
Teresa Johnson403a7872015-10-04 14:33:43 +00003573
3574 Out.write((char *)&Buffer.front(), Buffer.size());
3575}