LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BitcodeWriter.cpp
Go to the documentation of this file.
1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Bitcode writer implementation.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "ValueEnumerator.h"
16 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/InlineAsm.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h"
29 #include "llvm/Support/Program.h"
31 #include <cctype>
32 #include <map>
33 using namespace llvm;
34 
35 static cl::opt<bool>
36 EnablePreserveUseListOrdering("enable-bc-uselist-preserve",
37  cl::desc("Turn on experimental support for "
38  "use-list order preservation."),
39  cl::init(false), cl::Hidden);
40 
41 /// These are manifest constants used by the bitcode writer. They do not need to
42 /// be kept in sync with the reader, but need to be consistent within this file.
43 enum {
44  // VALUE_SYMTAB_BLOCK abbrev id's.
49 
50  // CONSTANTS_BLOCK abbrev id's.
55 
56  // FUNCTION_BLOCK abbrev id's.
64 };
65 
66 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
67  switch (Opcode) {
68  default: llvm_unreachable("Unknown cast instruction!");
69  case Instruction::Trunc : return bitc::CAST_TRUNC;
70  case Instruction::ZExt : return bitc::CAST_ZEXT;
71  case Instruction::SExt : return bitc::CAST_SEXT;
72  case Instruction::FPToUI : return bitc::CAST_FPTOUI;
73  case Instruction::FPToSI : return bitc::CAST_FPTOSI;
74  case Instruction::UIToFP : return bitc::CAST_UITOFP;
75  case Instruction::SIToFP : return bitc::CAST_SITOFP;
76  case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
77  case Instruction::FPExt : return bitc::CAST_FPEXT;
78  case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
79  case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
80  case Instruction::BitCast : return bitc::CAST_BITCAST;
81  case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
82  }
83 }
84 
85 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
86  switch (Opcode) {
87  default: llvm_unreachable("Unknown binary instruction!");
88  case Instruction::Add:
89  case Instruction::FAdd: return bitc::BINOP_ADD;
90  case Instruction::Sub:
91  case Instruction::FSub: return bitc::BINOP_SUB;
92  case Instruction::Mul:
93  case Instruction::FMul: return bitc::BINOP_MUL;
94  case Instruction::UDiv: return bitc::BINOP_UDIV;
95  case Instruction::FDiv:
96  case Instruction::SDiv: return bitc::BINOP_SDIV;
97  case Instruction::URem: return bitc::BINOP_UREM;
98  case Instruction::FRem:
99  case Instruction::SRem: return bitc::BINOP_SREM;
100  case Instruction::Shl: return bitc::BINOP_SHL;
101  case Instruction::LShr: return bitc::BINOP_LSHR;
102  case Instruction::AShr: return bitc::BINOP_ASHR;
103  case Instruction::And: return bitc::BINOP_AND;
104  case Instruction::Or: return bitc::BINOP_OR;
105  case Instruction::Xor: return bitc::BINOP_XOR;
106  }
107 }
108 
110  switch (Op) {
111  default: llvm_unreachable("Unknown RMW operation!");
112  case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
113  case AtomicRMWInst::Add: return bitc::RMW_ADD;
114  case AtomicRMWInst::Sub: return bitc::RMW_SUB;
115  case AtomicRMWInst::And: return bitc::RMW_AND;
116  case AtomicRMWInst::Nand: return bitc::RMW_NAND;
117  case AtomicRMWInst::Or: return bitc::RMW_OR;
118  case AtomicRMWInst::Xor: return bitc::RMW_XOR;
119  case AtomicRMWInst::Max: return bitc::RMW_MAX;
120  case AtomicRMWInst::Min: return bitc::RMW_MIN;
121  case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
122  case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
123  }
124 }
125 
126 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
127  switch (Ordering) {
128  case NotAtomic: return bitc::ORDERING_NOTATOMIC;
129  case Unordered: return bitc::ORDERING_UNORDERED;
130  case Monotonic: return bitc::ORDERING_MONOTONIC;
131  case Acquire: return bitc::ORDERING_ACQUIRE;
132  case Release: return bitc::ORDERING_RELEASE;
135  }
136  llvm_unreachable("Invalid ordering");
137 }
138 
139 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
140  switch (SynchScope) {
143  }
144  llvm_unreachable("Invalid synch scope");
145 }
146 
147 static void WriteStringRecord(unsigned Code, StringRef Str,
148  unsigned AbbrevToUse, BitstreamWriter &Stream) {
150 
151  // Code: [strchar x N]
152  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
153  if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
154  AbbrevToUse = 0;
155  Vals.push_back(Str[i]);
156  }
157 
158  // Emit the finished record.
159  Stream.EmitRecord(Code, Vals, AbbrevToUse);
160 }
161 
163  switch (Kind) {
168  case Attribute::Builtin:
170  case Attribute::ByVal:
171  return bitc::ATTR_KIND_BY_VAL;
172  case Attribute::Cold:
173  return bitc::ATTR_KIND_COLD;
176  case Attribute::InReg:
177  return bitc::ATTR_KIND_IN_REG;
178  case Attribute::MinSize:
180  case Attribute::Naked:
181  return bitc::ATTR_KIND_NAKED;
182  case Attribute::Nest:
183  return bitc::ATTR_KIND_NEST;
184  case Attribute::NoAlias:
194  case Attribute::NoInline:
200  case Attribute::NoReturn:
202  case Attribute::NoUnwind:
208  case Attribute::ReadNone:
210  case Attribute::ReadOnly:
212  case Attribute::Returned:
216  case Attribute::SExt:
217  return bitc::ATTR_KIND_S_EXT;
234  case Attribute::UWTable:
236  case Attribute::ZExt:
237  return bitc::ATTR_KIND_Z_EXT;
239  llvm_unreachable("Can not encode end-attribute kinds marker.");
240  case Attribute::None:
241  llvm_unreachable("Can not encode none-attribute.");
242  }
243 
244  llvm_unreachable("Trying to encode unknown attribute");
245 }
246 
248  BitstreamWriter &Stream) {
249  const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
250  if (AttrGrps.empty()) return;
251 
253 
255  for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
256  AttributeSet AS = AttrGrps[i];
257  for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
259 
260  Record.push_back(VE.getAttributeGroupID(A));
261  Record.push_back(AS.getSlotIndex(i));
262 
263  for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
264  I != E; ++I) {
265  Attribute Attr = *I;
266  if (Attr.isEnumAttribute()) {
267  Record.push_back(0);
269  } else if (Attr.isAlignAttribute()) {
270  Record.push_back(1);
272  Record.push_back(Attr.getValueAsInt());
273  } else {
274  StringRef Kind = Attr.getKindAsString();
275  StringRef Val = Attr.getValueAsString();
276 
277  Record.push_back(Val.empty() ? 3 : 4);
278  Record.append(Kind.begin(), Kind.end());
279  Record.push_back(0);
280  if (!Val.empty()) {
281  Record.append(Val.begin(), Val.end());
282  Record.push_back(0);
283  }
284  }
285  }
286 
288  Record.clear();
289  }
290  }
291 
292  Stream.ExitBlock();
293 }
294 
295 static void WriteAttributeTable(const ValueEnumerator &VE,
296  BitstreamWriter &Stream) {
297  const std::vector<AttributeSet> &Attrs = VE.getAttributes();
298  if (Attrs.empty()) return;
299 
301 
303  for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
304  const AttributeSet &A = Attrs[i];
305  for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
307 
308  Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
309  Record.clear();
310  }
311 
312  Stream.ExitBlock();
313 }
314 
315 /// WriteTypeTable - Write out the type table for a module.
316 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
317  const ValueEnumerator::TypeList &TypeList = VE.getTypes();
318 
319  Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
320  SmallVector<uint64_t, 64> TypeVals;
321 
322  uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1);
323 
324  // Abbrev for TYPE_CODE_POINTER.
325  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
327  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
328  Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
329  unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
330 
331  // Abbrev for TYPE_CODE_FUNCTION.
332  Abbv = new BitCodeAbbrev();
334  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
336  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
337 
338  unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
339 
340  // Abbrev for TYPE_CODE_STRUCT_ANON.
341  Abbv = new BitCodeAbbrev();
343  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
345  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
346 
347  unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
348 
349  // Abbrev for TYPE_CODE_STRUCT_NAME.
350  Abbv = new BitCodeAbbrev();
354  unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
355 
356  // Abbrev for TYPE_CODE_STRUCT_NAMED.
357  Abbv = new BitCodeAbbrev();
359  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
361  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
362 
363  unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
364 
365  // Abbrev for TYPE_CODE_ARRAY.
366  Abbv = new BitCodeAbbrev();
368  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
369  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
370 
371  unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
372 
373  // Emit an entry count so the reader can reserve space.
374  TypeVals.push_back(TypeList.size());
375  Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
376  TypeVals.clear();
377 
378  // Loop over all of the types, emitting each in turn.
379  for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
380  Type *T = TypeList[i];
381  int AbbrevToUse = 0;
382  unsigned Code = 0;
383 
384  switch (T->getTypeID()) {
385  default: llvm_unreachable("Unknown type!");
386  case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
387  case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
388  case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
389  case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
390  case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
391  case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
393  case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
394  case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
395  case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
396  case Type::IntegerTyID:
397  // INTEGER: [width]
399  TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
400  break;
401  case Type::PointerTyID: {
402  PointerType *PTy = cast<PointerType>(T);
403  // POINTER: [pointee type, address space]
405  TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
406  unsigned AddressSpace = PTy->getAddressSpace();
407  TypeVals.push_back(AddressSpace);
408  if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
409  break;
410  }
411  case Type::FunctionTyID: {
412  FunctionType *FT = cast<FunctionType>(T);
413  // FUNCTION: [isvararg, retty, paramty x N]
415  TypeVals.push_back(FT->isVarArg());
416  TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
417  for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
418  TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
419  AbbrevToUse = FunctionAbbrev;
420  break;
421  }
422  case Type::StructTyID: {
423  StructType *ST = cast<StructType>(T);
424  // STRUCT: [ispacked, eltty x N]
425  TypeVals.push_back(ST->isPacked());
426  // Output all of the element types.
428  E = ST->element_end(); I != E; ++I)
429  TypeVals.push_back(VE.getTypeID(*I));
430 
431  if (ST->isLiteral()) {
433  AbbrevToUse = StructAnonAbbrev;
434  } else {
435  if (ST->isOpaque()) {
436  Code = bitc::TYPE_CODE_OPAQUE;
437  } else {
439  AbbrevToUse = StructNamedAbbrev;
440  }
441 
442  // Emit the name if it is present.
443  if (!ST->getName().empty())
445  StructNameAbbrev, Stream);
446  }
447  break;
448  }
449  case Type::ArrayTyID: {
450  ArrayType *AT = cast<ArrayType>(T);
451  // ARRAY: [numelts, eltty]
452  Code = bitc::TYPE_CODE_ARRAY;
453  TypeVals.push_back(AT->getNumElements());
454  TypeVals.push_back(VE.getTypeID(AT->getElementType()));
455  AbbrevToUse = ArrayAbbrev;
456  break;
457  }
458  case Type::VectorTyID: {
459  VectorType *VT = cast<VectorType>(T);
460  // VECTOR [numelts, eltty]
461  Code = bitc::TYPE_CODE_VECTOR;
462  TypeVals.push_back(VT->getNumElements());
463  TypeVals.push_back(VE.getTypeID(VT->getElementType()));
464  break;
465  }
466  }
467 
468  // Emit the finished record.
469  Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
470  TypeVals.clear();
471  }
472 
473  Stream.ExitBlock();
474 }
475 
476 static unsigned getEncodedLinkage(const GlobalValue *GV) {
477  switch (GV->getLinkage()) {
478  case GlobalValue::ExternalLinkage: return 0;
479  case GlobalValue::WeakAnyLinkage: return 1;
480  case GlobalValue::AppendingLinkage: return 2;
481  case GlobalValue::InternalLinkage: return 3;
482  case GlobalValue::LinkOnceAnyLinkage: return 4;
483  case GlobalValue::DLLImportLinkage: return 5;
484  case GlobalValue::DLLExportLinkage: return 6;
485  case GlobalValue::ExternalWeakLinkage: return 7;
486  case GlobalValue::CommonLinkage: return 8;
487  case GlobalValue::PrivateLinkage: return 9;
488  case GlobalValue::WeakODRLinkage: return 10;
489  case GlobalValue::LinkOnceODRLinkage: return 11;
491  case GlobalValue::LinkerPrivateLinkage: return 13;
493  }
494  llvm_unreachable("Invalid linkage");
495 }
496 
497 static unsigned getEncodedVisibility(const GlobalValue *GV) {
498  switch (GV->getVisibility()) {
499  case GlobalValue::DefaultVisibility: return 0;
500  case GlobalValue::HiddenVisibility: return 1;
501  case GlobalValue::ProtectedVisibility: return 2;
502  }
503  llvm_unreachable("Invalid visibility");
504 }
505 
506 static unsigned getEncodedThreadLocalMode(const GlobalVariable *GV) {
507  switch (GV->getThreadLocalMode()) {
508  case GlobalVariable::NotThreadLocal: return 0;
512  case GlobalVariable::LocalExecTLSModel: return 4;
513  }
514  llvm_unreachable("Invalid TLS model");
515 }
516 
517 // Emit top-level description of module, including target triple, inline asm,
518 // descriptors for global variables, and function prototype info.
519 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
520  BitstreamWriter &Stream) {
521  // Emit various pieces of data attached to a module.
522  if (!M->getTargetTriple().empty())
524  0/*TODO*/, Stream);
525  if (!M->getDataLayout().empty())
527  0/*TODO*/, Stream);
528  if (!M->getModuleInlineAsm().empty())
530  0/*TODO*/, Stream);
531 
532  // Emit information about sections and GC, computing how many there are. Also
533  // compute the maximum alignment value.
534  std::map<std::string, unsigned> SectionMap;
535  std::map<std::string, unsigned> GCMap;
536  unsigned MaxAlignment = 0;
537  unsigned MaxGlobalType = 0;
538  for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
539  GV != E; ++GV) {
540  MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
541  MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
542  if (GV->hasSection()) {
543  // Give section names unique ID's.
544  unsigned &Entry = SectionMap[GV->getSection()];
545  if (!Entry) {
547  0/*TODO*/, Stream);
548  Entry = SectionMap.size();
549  }
550  }
551  }
552  for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
553  MaxAlignment = std::max(MaxAlignment, F->getAlignment());
554  if (F->hasSection()) {
555  // Give section names unique ID's.
556  unsigned &Entry = SectionMap[F->getSection()];
557  if (!Entry) {
559  0/*TODO*/, Stream);
560  Entry = SectionMap.size();
561  }
562  }
563  if (F->hasGC()) {
564  // Same for GC names.
565  unsigned &Entry = GCMap[F->getGC()];
566  if (!Entry) {
568  0/*TODO*/, Stream);
569  Entry = GCMap.size();
570  }
571  }
572  }
573 
574  // Emit abbrev for globals, now that we know # sections and max alignment.
575  unsigned SimpleGVarAbbrev = 0;
576  if (!M->global_empty()) {
577  // Add an abbrev for common globals with no visibility or thread localness.
578  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
581  Log2_32_Ceil(MaxGlobalType+1)));
582  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
583  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
584  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
585  if (MaxAlignment == 0) // Alignment.
586  Abbv->Add(BitCodeAbbrevOp(0));
587  else {
588  unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
590  Log2_32_Ceil(MaxEncAlignment+1)));
591  }
592  if (SectionMap.empty()) // Section.
593  Abbv->Add(BitCodeAbbrevOp(0));
594  else
596  Log2_32_Ceil(SectionMap.size()+1)));
597  // Don't bother emitting vis + thread local.
598  SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
599  }
600 
601  // Emit the global variable information.
603  for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
604  GV != E; ++GV) {
605  unsigned AbbrevToUse = 0;
606 
607  // GLOBALVAR: [type, isconst, initid,
608  // linkage, alignment, section, visibility, threadlocal,
609  // unnamed_addr, externally_initialized]
610  Vals.push_back(VE.getTypeID(GV->getType()));
611  Vals.push_back(GV->isConstant());
612  Vals.push_back(GV->isDeclaration() ? 0 :
613  (VE.getValueID(GV->getInitializer()) + 1));
614  Vals.push_back(getEncodedLinkage(GV));
615  Vals.push_back(Log2_32(GV->getAlignment())+1);
616  Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
617  if (GV->isThreadLocal() ||
618  GV->getVisibility() != GlobalValue::DefaultVisibility ||
619  GV->hasUnnamedAddr() || GV->isExternallyInitialized()) {
622  Vals.push_back(GV->hasUnnamedAddr());
623  Vals.push_back(GV->isExternallyInitialized());
624  } else {
625  AbbrevToUse = SimpleGVarAbbrev;
626  }
627 
628  Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
629  Vals.clear();
630  }
631 
632  // Emit the function proto information.
633  for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
634  // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
635  // section, visibility, gc, unnamed_addr, prefix]
636  Vals.push_back(VE.getTypeID(F->getType()));
637  Vals.push_back(F->getCallingConv());
638  Vals.push_back(F->isDeclaration());
640  Vals.push_back(VE.getAttributeID(F->getAttributes()));
641  Vals.push_back(Log2_32(F->getAlignment())+1);
642  Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
644  Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
645  Vals.push_back(F->hasUnnamedAddr());
646  Vals.push_back(F->hasPrefixData() ? (VE.getValueID(F->getPrefixData()) + 1)
647  : 0);
648 
649  unsigned AbbrevToUse = 0;
650  Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
651  Vals.clear();
652  }
653 
654  // Emit the alias information.
655  for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
656  AI != E; ++AI) {
657  // ALIAS: [alias type, aliasee val#, linkage, visibility]
658  Vals.push_back(VE.getTypeID(AI->getType()));
659  Vals.push_back(VE.getValueID(AI->getAliasee()));
660  Vals.push_back(getEncodedLinkage(AI));
662  unsigned AbbrevToUse = 0;
663  Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
664  Vals.clear();
665  }
666 }
667 
668 static uint64_t GetOptimizationFlags(const Value *V) {
669  uint64_t Flags = 0;
670 
671  if (const OverflowingBinaryOperator *OBO =
672  dyn_cast<OverflowingBinaryOperator>(V)) {
673  if (OBO->hasNoSignedWrap())
674  Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
675  if (OBO->hasNoUnsignedWrap())
676  Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
677  } else if (const PossiblyExactOperator *PEO =
678  dyn_cast<PossiblyExactOperator>(V)) {
679  if (PEO->isExact())
680  Flags |= 1 << bitc::PEO_EXACT;
681  } else if (const FPMathOperator *FPMO =
682  dyn_cast<const FPMathOperator>(V)) {
683  if (FPMO->hasUnsafeAlgebra())
685  if (FPMO->hasNoNaNs())
686  Flags |= FastMathFlags::NoNaNs;
687  if (FPMO->hasNoInfs())
688  Flags |= FastMathFlags::NoInfs;
689  if (FPMO->hasNoSignedZeros())
691  if (FPMO->hasAllowReciprocal())
693  }
694 
695  return Flags;
696 }
697 
698 static void WriteMDNode(const MDNode *N,
699  const ValueEnumerator &VE,
700  BitstreamWriter &Stream,
702  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
703  if (N->getOperand(i)) {
704  Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
705  Record.push_back(VE.getValueID(N->getOperand(i)));
706  } else {
707  Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
708  Record.push_back(0);
709  }
710  }
711  unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE :
713  Stream.EmitRecord(MDCode, Record, 0);
714  Record.clear();
715 }
716 
717 static void WriteModuleMetadata(const Module *M,
718  const ValueEnumerator &VE,
719  BitstreamWriter &Stream) {
720  const ValueEnumerator::ValueList &Vals = VE.getMDValues();
721  bool StartedMetadataBlock = false;
722  unsigned MDSAbbrev = 0;
724  for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
725 
726  if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
727  if (!N->isFunctionLocal() || !N->getFunction()) {
728  if (!StartedMetadataBlock) {
730  StartedMetadataBlock = true;
731  }
732  WriteMDNode(N, VE, Stream, Record);
733  }
734  } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
735  if (!StartedMetadataBlock) {
737 
738  // Abbrev for METADATA_STRING.
739  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
743  MDSAbbrev = Stream.EmitAbbrev(Abbv);
744  StartedMetadataBlock = true;
745  }
746 
747  // Code: [strchar x N]
748  Record.append(MDS->begin(), MDS->end());
749 
750  // Emit the finished record.
751  Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
752  Record.clear();
753  }
754  }
755 
756  // Write named metadata.
758  E = M->named_metadata_end(); I != E; ++I) {
759  const NamedMDNode *NMD = I;
760  if (!StartedMetadataBlock) {
762  StartedMetadataBlock = true;
763  }
764 
765  // Write name.
766  StringRef Str = NMD->getName();
767  for (unsigned i = 0, e = Str.size(); i != e; ++i)
768  Record.push_back(Str[i]);
769  Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
770  Record.clear();
771 
772  // Write named metadata operands.
773  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
774  Record.push_back(VE.getValueID(NMD->getOperand(i)));
775  Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
776  Record.clear();
777  }
778 
779  if (StartedMetadataBlock)
780  Stream.ExitBlock();
781 }
782 
784  const ValueEnumerator &VE,
785  BitstreamWriter &Stream) {
786  bool StartedMetadataBlock = false;
789  for (unsigned i = 0, e = Vals.size(); i != e; ++i)
790  if (const MDNode *N = Vals[i])
791  if (N->isFunctionLocal() && N->getFunction() == &F) {
792  if (!StartedMetadataBlock) {
794  StartedMetadataBlock = true;
795  }
796  WriteMDNode(N, VE, Stream, Record);
797  }
798 
799  if (StartedMetadataBlock)
800  Stream.ExitBlock();
801 }
802 
803 static void WriteMetadataAttachment(const Function &F,
804  const ValueEnumerator &VE,
805  BitstreamWriter &Stream) {
807 
809 
810  // Write metadata attachments
811  // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
813 
814  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
815  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
816  I != E; ++I) {
817  MDs.clear();
818  I->getAllMetadataOtherThanDebugLoc(MDs);
819 
820  // If no metadata, ignore instruction.
821  if (MDs.empty()) continue;
822 
823  Record.push_back(VE.getInstructionID(I));
824 
825  for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
826  Record.push_back(MDs[i].first);
827  Record.push_back(VE.getValueID(MDs[i].second));
828  }
829  Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
830  Record.clear();
831  }
832 
833  Stream.ExitBlock();
834 }
835 
836 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
838 
839  // Write metadata kinds
840  // METADATA_KIND - [n x [id, name]]
842  M->getMDKindNames(Names);
843 
844  if (Names.empty()) return;
845 
847 
848  for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
849  Record.push_back(MDKindID);
850  StringRef KName = Names[MDKindID];
851  Record.append(KName.begin(), KName.end());
852 
853  Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
854  Record.clear();
855  }
856 
857  Stream.ExitBlock();
858 }
859 
860 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
861  if ((int64_t)V >= 0)
862  Vals.push_back(V << 1);
863  else
864  Vals.push_back((-V << 1) | 1);
865 }
866 
867 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
868  const ValueEnumerator &VE,
869  BitstreamWriter &Stream, bool isGlobal) {
870  if (FirstVal == LastVal) return;
871 
873 
874  unsigned AggregateAbbrev = 0;
875  unsigned String8Abbrev = 0;
876  unsigned CString7Abbrev = 0;
877  unsigned CString6Abbrev = 0;
878  // If this is a constant pool for the module, emit module-specific abbrevs.
879  if (isGlobal) {
880  // Abbrev for CST_CODE_AGGREGATE.
881  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
885  AggregateAbbrev = Stream.EmitAbbrev(Abbv);
886 
887  // Abbrev for CST_CODE_STRING.
888  Abbv = new BitCodeAbbrev();
892  String8Abbrev = Stream.EmitAbbrev(Abbv);
893  // Abbrev for CST_CODE_CSTRING.
894  Abbv = new BitCodeAbbrev();
898  CString7Abbrev = Stream.EmitAbbrev(Abbv);
899  // Abbrev for CST_CODE_CSTRING.
900  Abbv = new BitCodeAbbrev();
904  CString6Abbrev = Stream.EmitAbbrev(Abbv);
905  }
906 
908 
909  const ValueEnumerator::ValueList &Vals = VE.getValues();
910  Type *LastTy = 0;
911  for (unsigned i = FirstVal; i != LastVal; ++i) {
912  const Value *V = Vals[i].first;
913  // If we need to switch types, do so now.
914  if (V->getType() != LastTy) {
915  LastTy = V->getType();
916  Record.push_back(VE.getTypeID(LastTy));
917  Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
919  Record.clear();
920  }
921 
922  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
923  Record.push_back(unsigned(IA->hasSideEffects()) |
924  unsigned(IA->isAlignStack()) << 1 |
925  unsigned(IA->getDialect()&1) << 2);
926 
927  // Add the asm string.
928  const std::string &AsmStr = IA->getAsmString();
929  Record.push_back(AsmStr.size());
930  for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
931  Record.push_back(AsmStr[i]);
932 
933  // Add the constraint string.
934  const std::string &ConstraintStr = IA->getConstraintString();
935  Record.push_back(ConstraintStr.size());
936  for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
937  Record.push_back(ConstraintStr[i]);
938  Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
939  Record.clear();
940  continue;
941  }
942  const Constant *C = cast<Constant>(V);
943  unsigned Code = -1U;
944  unsigned AbbrevToUse = 0;
945  if (C->isNullValue()) {
946  Code = bitc::CST_CODE_NULL;
947  } else if (isa<UndefValue>(C)) {
948  Code = bitc::CST_CODE_UNDEF;
949  } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
950  if (IV->getBitWidth() <= 64) {
951  uint64_t V = IV->getSExtValue();
952  emitSignedInt64(Record, V);
953  Code = bitc::CST_CODE_INTEGER;
954  AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
955  } else { // Wide integers, > 64 bits in size.
956  // We have an arbitrary precision integer value to write whose
957  // bit width is > 64. However, in canonical unsigned integer
958  // format it is likely that the high bits are going to be zero.
959  // So, we only write the number of active words.
960  unsigned NWords = IV->getValue().getActiveWords();
961  const uint64_t *RawWords = IV->getValue().getRawData();
962  for (unsigned i = 0; i != NWords; ++i) {
963  emitSignedInt64(Record, RawWords[i]);
964  }
966  }
967  } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
968  Code = bitc::CST_CODE_FLOAT;
969  Type *Ty = CFP->getType();
970  if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
971  Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
972  } else if (Ty->isX86_FP80Ty()) {
973  // api needed to prevent premature destruction
974  // bits are not in the same order as a normal i80 APInt, compensate.
975  APInt api = CFP->getValueAPF().bitcastToAPInt();
976  const uint64_t *p = api.getRawData();
977  Record.push_back((p[1] << 48) | (p[0] >> 16));
978  Record.push_back(p[0] & 0xffffLL);
979  } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
980  APInt api = CFP->getValueAPF().bitcastToAPInt();
981  const uint64_t *p = api.getRawData();
982  Record.push_back(p[0]);
983  Record.push_back(p[1]);
984  } else {
985  assert (0 && "Unknown FP type!");
986  }
987  } else if (isa<ConstantDataSequential>(C) &&
988  cast<ConstantDataSequential>(C)->isString()) {
989  const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
990  // Emit constant strings specially.
991  unsigned NumElts = Str->getNumElements();
992  // If this is a null-terminated string, use the denser CSTRING encoding.
993  if (Str->isCString()) {
994  Code = bitc::CST_CODE_CSTRING;
995  --NumElts; // Don't encode the null, which isn't allowed by char6.
996  } else {
997  Code = bitc::CST_CODE_STRING;
998  AbbrevToUse = String8Abbrev;
999  }
1000  bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1001  bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1002  for (unsigned i = 0; i != NumElts; ++i) {
1003  unsigned char V = Str->getElementAsInteger(i);
1004  Record.push_back(V);
1005  isCStr7 &= (V & 128) == 0;
1006  if (isCStrChar6)
1007  isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1008  }
1009 
1010  if (isCStrChar6)
1011  AbbrevToUse = CString6Abbrev;
1012  else if (isCStr7)
1013  AbbrevToUse = CString7Abbrev;
1014  } else if (const ConstantDataSequential *CDS =
1015  dyn_cast<ConstantDataSequential>(C)) {
1016  Code = bitc::CST_CODE_DATA;
1017  Type *EltTy = CDS->getType()->getElementType();
1018  if (isa<IntegerType>(EltTy)) {
1019  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1020  Record.push_back(CDS->getElementAsInteger(i));
1021  } else if (EltTy->isFloatTy()) {
1022  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1023  union { float F; uint32_t I; };
1024  F = CDS->getElementAsFloat(i);
1025  Record.push_back(I);
1026  }
1027  } else {
1028  assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
1029  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1030  union { double F; uint64_t I; };
1031  F = CDS->getElementAsDouble(i);
1032  Record.push_back(I);
1033  }
1034  }
1035  } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
1036  isa<ConstantVector>(C)) {
1037  Code = bitc::CST_CODE_AGGREGATE;
1038  for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
1039  Record.push_back(VE.getValueID(C->getOperand(i)));
1040  AbbrevToUse = AggregateAbbrev;
1041  } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1042  switch (CE->getOpcode()) {
1043  default:
1044  if (Instruction::isCast(CE->getOpcode())) {
1045  Code = bitc::CST_CODE_CE_CAST;
1046  Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
1047  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1048  Record.push_back(VE.getValueID(C->getOperand(0)));
1049  AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
1050  } else {
1051  assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1052  Code = bitc::CST_CODE_CE_BINOP;
1053  Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
1054  Record.push_back(VE.getValueID(C->getOperand(0)));
1055  Record.push_back(VE.getValueID(C->getOperand(1)));
1056  uint64_t Flags = GetOptimizationFlags(CE);
1057  if (Flags != 0)
1058  Record.push_back(Flags);
1059  }
1060  break;
1061  case Instruction::GetElementPtr:
1062  Code = bitc::CST_CODE_CE_GEP;
1063  if (cast<GEPOperator>(C)->isInBounds())
1065  for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1066  Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1067  Record.push_back(VE.getValueID(C->getOperand(i)));
1068  }
1069  break;
1070  case Instruction::Select:
1071  Code = bitc::CST_CODE_CE_SELECT;
1072  Record.push_back(VE.getValueID(C->getOperand(0)));
1073  Record.push_back(VE.getValueID(C->getOperand(1)));
1074  Record.push_back(VE.getValueID(C->getOperand(2)));
1075  break;
1078  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1079  Record.push_back(VE.getValueID(C->getOperand(0)));
1080  Record.push_back(VE.getValueID(C->getOperand(1)));
1081  break;
1082  case Instruction::InsertElement:
1084  Record.push_back(VE.getValueID(C->getOperand(0)));
1085  Record.push_back(VE.getValueID(C->getOperand(1)));
1086  Record.push_back(VE.getValueID(C->getOperand(2)));
1087  break;
1088  case Instruction::ShuffleVector:
1089  // If the return type and argument types are the same, this is a
1090  // standard shufflevector instruction. If the types are different,
1091  // then the shuffle is widening or truncating the input vectors, and
1092  // the argument type must also be encoded.
1093  if (C->getType() == C->getOperand(0)->getType()) {
1095  } else {
1097  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1098  }
1099  Record.push_back(VE.getValueID(C->getOperand(0)));
1100  Record.push_back(VE.getValueID(C->getOperand(1)));
1101  Record.push_back(VE.getValueID(C->getOperand(2)));
1102  break;
1103  case Instruction::ICmp:
1104  case Instruction::FCmp:
1105  Code = bitc::CST_CODE_CE_CMP;
1106  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1107  Record.push_back(VE.getValueID(C->getOperand(0)));
1108  Record.push_back(VE.getValueID(C->getOperand(1)));
1109  Record.push_back(CE->getPredicate());
1110  break;
1111  }
1112  } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
1114  Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
1115  Record.push_back(VE.getValueID(BA->getFunction()));
1116  Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
1117  } else {
1118 #ifndef NDEBUG
1119  C->dump();
1120 #endif
1121  llvm_unreachable("Unknown constant!");
1122  }
1123  Stream.EmitRecord(Code, Record, AbbrevToUse);
1124  Record.clear();
1125  }
1126 
1127  Stream.ExitBlock();
1128 }
1129 
1131  BitstreamWriter &Stream) {
1132  const ValueEnumerator::ValueList &Vals = VE.getValues();
1133 
1134  // Find the first constant to emit, which is the first non-globalvalue value.
1135  // We know globalvalues have been emitted by WriteModuleInfo.
1136  for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1137  if (!isa<GlobalValue>(Vals[i].first)) {
1138  WriteConstants(i, Vals.size(), VE, Stream, true);
1139  return;
1140  }
1141  }
1142 }
1143 
1144 /// PushValueAndType - The file has to encode both the value and type id for
1145 /// many values, because we need to know what type to create for forward
1146 /// references. However, most operands are not forward references, so this type
1147 /// field is not needed.
1148 ///
1149 /// This function adds V's value ID to Vals. If the value ID is higher than the
1150 /// instruction ID, then it is a forward reference, and it also includes the
1151 /// type ID. The value ID that is written is encoded relative to the InstID.
1152 static bool PushValueAndType(const Value *V, unsigned InstID,
1154  ValueEnumerator &VE) {
1155  unsigned ValID = VE.getValueID(V);
1156  // Make encoding relative to the InstID.
1157  Vals.push_back(InstID - ValID);
1158  if (ValID >= InstID) {
1159  Vals.push_back(VE.getTypeID(V->getType()));
1160  return true;
1161  }
1162  return false;
1163 }
1164 
1165 /// pushValue - Like PushValueAndType, but where the type of the value is
1166 /// omitted (perhaps it was already encoded in an earlier operand).
1167 static void pushValue(const Value *V, unsigned InstID,
1169  ValueEnumerator &VE) {
1170  unsigned ValID = VE.getValueID(V);
1171  Vals.push_back(InstID - ValID);
1172 }
1173 
1174 static void pushValueSigned(const Value *V, unsigned InstID,
1176  ValueEnumerator &VE) {
1177  unsigned ValID = VE.getValueID(V);
1178  int64_t diff = ((int32_t)InstID - (int32_t)ValID);
1179  emitSignedInt64(Vals, diff);
1180 }
1181 
1182 /// WriteInstruction - Emit an instruction to the specified stream.
1183 static void WriteInstruction(const Instruction &I, unsigned InstID,
1184  ValueEnumerator &VE, BitstreamWriter &Stream,
1185  SmallVectorImpl<unsigned> &Vals) {
1186  unsigned Code = 0;
1187  unsigned AbbrevToUse = 0;
1188  VE.setInstructionID(&I);
1189  switch (I.getOpcode()) {
1190  default:
1191  if (Instruction::isCast(I.getOpcode())) {
1193  if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1194  AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
1195  Vals.push_back(VE.getTypeID(I.getType()));
1196  Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
1197  } else {
1198  assert(isa<BinaryOperator>(I) && "Unknown instruction!");
1200  if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1201  AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
1202  pushValue(I.getOperand(1), InstID, Vals, VE);
1203  Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
1204  uint64_t Flags = GetOptimizationFlags(&I);
1205  if (Flags != 0) {
1206  if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
1207  AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
1208  Vals.push_back(Flags);
1209  }
1210  }
1211  break;
1212 
1213  case Instruction::GetElementPtr:
1214  Code = bitc::FUNC_CODE_INST_GEP;
1215  if (cast<GEPOperator>(&I)->isInBounds())
1217  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1218  PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1219  break;
1220  case Instruction::ExtractValue: {
1222  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1223  const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
1224  for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1225  Vals.push_back(*i);
1226  break;
1227  }
1228  case Instruction::InsertValue: {
1230  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1231  PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1232  const InsertValueInst *IVI = cast<InsertValueInst>(&I);
1233  for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1234  Vals.push_back(*i);
1235  break;
1236  }
1237  case Instruction::Select:
1239  PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1240  pushValue(I.getOperand(2), InstID, Vals, VE);
1241  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1242  break;
1245  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1246  pushValue(I.getOperand(1), InstID, Vals, VE);
1247  break;
1248  case Instruction::InsertElement:
1250  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1251  pushValue(I.getOperand(1), InstID, Vals, VE);
1252  pushValue(I.getOperand(2), InstID, Vals, VE);
1253  break;
1254  case Instruction::ShuffleVector:
1256  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1257  pushValue(I.getOperand(1), InstID, Vals, VE);
1258  pushValue(I.getOperand(2), InstID, Vals, VE);
1259  break;
1260  case Instruction::ICmp:
1261  case Instruction::FCmp:
1262  // compare returning Int1Ty or vector of Int1Ty
1264  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1265  pushValue(I.getOperand(1), InstID, Vals, VE);
1266  Vals.push_back(cast<CmpInst>(I).getPredicate());
1267  break;
1268 
1269  case Instruction::Ret:
1270  {
1271  Code = bitc::FUNC_CODE_INST_RET;
1272  unsigned NumOperands = I.getNumOperands();
1273  if (NumOperands == 0)
1274  AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1275  else if (NumOperands == 1) {
1276  if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1277  AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1278  } else {
1279  for (unsigned i = 0, e = NumOperands; i != e; ++i)
1280  PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1281  }
1282  }
1283  break;
1284  case Instruction::Br:
1285  {
1286  Code = bitc::FUNC_CODE_INST_BR;
1287  const BranchInst &II = cast<BranchInst>(I);
1288  Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1289  if (II.isConditional()) {
1290  Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1291  pushValue(II.getCondition(), InstID, Vals, VE);
1292  }
1293  }
1294  break;
1295  case Instruction::Switch:
1296  {
1298  const SwitchInst &SI = cast<SwitchInst>(I);
1299  Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
1300  pushValue(SI.getCondition(), InstID, Vals, VE);
1301  Vals.push_back(VE.getValueID(SI.getDefaultDest()));
1302  for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
1303  i != e; ++i) {
1304  Vals.push_back(VE.getValueID(i.getCaseValue()));
1305  Vals.push_back(VE.getValueID(i.getCaseSuccessor()));
1306  }
1307  }
1308  break;
1309  case Instruction::IndirectBr:
1311  Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1312  // Encode the address operand as relative, but not the basic blocks.
1313  pushValue(I.getOperand(0), InstID, Vals, VE);
1314  for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
1315  Vals.push_back(VE.getValueID(I.getOperand(i)));
1316  break;
1317 
1318  case Instruction::Invoke: {
1319  const InvokeInst *II = cast<InvokeInst>(&I);
1320  const Value *Callee(II->getCalledValue());
1321  PointerType *PTy = cast<PointerType>(Callee->getType());
1322  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1324 
1325  Vals.push_back(VE.getAttributeID(II->getAttributes()));
1326  Vals.push_back(II->getCallingConv());
1327  Vals.push_back(VE.getValueID(II->getNormalDest()));
1328  Vals.push_back(VE.getValueID(II->getUnwindDest()));
1329  PushValueAndType(Callee, InstID, Vals, VE);
1330 
1331  // Emit value #'s for the fixed parameters.
1332  for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1333  pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param.
1334 
1335  // Emit type/value pairs for varargs params.
1336  if (FTy->isVarArg()) {
1337  for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
1338  i != e; ++i)
1339  PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
1340  }
1341  break;
1342  }
1343  case Instruction::Resume:
1345  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1346  break;
1347  case Instruction::Unreachable:
1349  AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
1350  break;
1351 
1352  case Instruction::PHI: {
1353  const PHINode &PN = cast<PHINode>(I);
1354  Code = bitc::FUNC_CODE_INST_PHI;
1355  // With the newer instruction encoding, forward references could give
1356  // negative valued IDs. This is most common for PHIs, so we use
1357  // signed VBRs.
1359  Vals64.push_back(VE.getTypeID(PN.getType()));
1360  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1361  pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
1362  Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
1363  }
1364  // Emit a Vals64 vector and exit.
1365  Stream.EmitRecord(Code, Vals64, AbbrevToUse);
1366  Vals64.clear();
1367  return;
1368  }
1369 
1370  case Instruction::LandingPad: {
1371  const LandingPadInst &LP = cast<LandingPadInst>(I);
1373  Vals.push_back(VE.getTypeID(LP.getType()));
1374  PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
1375  Vals.push_back(LP.isCleanup());
1376  Vals.push_back(LP.getNumClauses());
1377  for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
1378  if (LP.isCatch(I))
1379  Vals.push_back(LandingPadInst::Catch);
1380  else
1381  Vals.push_back(LandingPadInst::Filter);
1382  PushValueAndType(LP.getClause(I), InstID, Vals, VE);
1383  }
1384  break;
1385  }
1386 
1387  case Instruction::Alloca:
1389  Vals.push_back(VE.getTypeID(I.getType()));
1390  Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1391  Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1392  Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
1393  break;
1394 
1395  case Instruction::Load:
1396  if (cast<LoadInst>(I).isAtomic()) {
1398  PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1399  } else {
1401  if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
1402  AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
1403  }
1404  Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1405  Vals.push_back(cast<LoadInst>(I).isVolatile());
1406  if (cast<LoadInst>(I).isAtomic()) {
1407  Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
1408  Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
1409  }
1410  break;
1411  case Instruction::Store:
1412  if (cast<StoreInst>(I).isAtomic())
1414  else
1416  PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
1417  pushValue(I.getOperand(0), InstID, Vals, VE); // val.
1418  Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1419  Vals.push_back(cast<StoreInst>(I).isVolatile());
1420  if (cast<StoreInst>(I).isAtomic()) {
1421  Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
1422  Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
1423  }
1424  break;
1425  case Instruction::AtomicCmpXchg:
1427  PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1428  pushValue(I.getOperand(1), InstID, Vals, VE); // cmp.
1429  pushValue(I.getOperand(2), InstID, Vals, VE); // newval.
1430  Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
1431  Vals.push_back(GetEncodedOrdering(
1432  cast<AtomicCmpXchgInst>(I).getOrdering()));
1433  Vals.push_back(GetEncodedSynchScope(
1434  cast<AtomicCmpXchgInst>(I).getSynchScope()));
1435  break;
1436  case Instruction::AtomicRMW:
1438  PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1439  pushValue(I.getOperand(1), InstID, Vals, VE); // val.
1440  Vals.push_back(GetEncodedRMWOperation(
1441  cast<AtomicRMWInst>(I).getOperation()));
1442  Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
1443  Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
1444  Vals.push_back(GetEncodedSynchScope(
1445  cast<AtomicRMWInst>(I).getSynchScope()));
1446  break;
1447  case Instruction::Fence:
1449  Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
1450  Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
1451  break;
1452  case Instruction::Call: {
1453  const CallInst &CI = cast<CallInst>(I);
1454  PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
1455  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1456 
1458 
1459  Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1460  Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
1461  PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
1462 
1463  // Emit value #'s for the fixed parameters.
1464  for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
1465  // Check for labels (can happen with asm labels).
1466  if (FTy->getParamType(i)->isLabelTy())
1467  Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
1468  else
1469  pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param.
1470  }
1471 
1472  // Emit type/value pairs for varargs params.
1473  if (FTy->isVarArg()) {
1474  for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
1475  i != e; ++i)
1476  PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
1477  }
1478  break;
1479  }
1480  case Instruction::VAArg:
1482  Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
1483  pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
1484  Vals.push_back(VE.getTypeID(I.getType())); // restype.
1485  break;
1486  }
1487 
1488  Stream.EmitRecord(Code, Vals, AbbrevToUse);
1489  Vals.clear();
1490 }
1491 
1492 // Emit names for globals/functions etc.
1494  const ValueEnumerator &VE,
1495  BitstreamWriter &Stream) {
1496  if (VST.empty()) return;
1498 
1499  // FIXME: Set up the abbrev, we know how many values there are!
1500  // FIXME: We know if the type names can use 7-bit ascii.
1501  SmallVector<unsigned, 64> NameVals;
1502 
1503  for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1504  SI != SE; ++SI) {
1505 
1506  const ValueName &Name = *SI;
1507 
1508  // Figure out the encoding to use for the name.
1509  bool is7Bit = true;
1510  bool isChar6 = true;
1511  for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1512  C != E; ++C) {
1513  if (isChar6)
1514  isChar6 = BitCodeAbbrevOp::isChar6(*C);
1515  if ((unsigned char)*C & 128) {
1516  is7Bit = false;
1517  break; // don't bother scanning the rest.
1518  }
1519  }
1520 
1521  unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
1522 
1523  // VST_ENTRY: [valueid, namechar x N]
1524  // VST_BBENTRY: [bbid, namechar x N]
1525  unsigned Code;
1526  if (isa<BasicBlock>(SI->getValue())) {
1527  Code = bitc::VST_CODE_BBENTRY;
1528  if (isChar6)
1529  AbbrevToUse = VST_BBENTRY_6_ABBREV;
1530  } else {
1531  Code = bitc::VST_CODE_ENTRY;
1532  if (isChar6)
1533  AbbrevToUse = VST_ENTRY_6_ABBREV;
1534  else if (is7Bit)
1535  AbbrevToUse = VST_ENTRY_7_ABBREV;
1536  }
1537 
1538  NameVals.push_back(VE.getValueID(SI->getValue()));
1539  for (const char *P = Name.getKeyData(),
1540  *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
1541  NameVals.push_back((unsigned char)*P);
1542 
1543  // Emit the finished record.
1544  Stream.EmitRecord(Code, NameVals, AbbrevToUse);
1545  NameVals.clear();
1546  }
1547  Stream.ExitBlock();
1548 }
1549 
1550 /// WriteFunction - Emit a function body to the module stream.
1551 static void WriteFunction(const Function &F, ValueEnumerator &VE,
1552  BitstreamWriter &Stream) {
1554  VE.incorporateFunction(F);
1555 
1557 
1558  // Emit the number of basic blocks, so the reader can create them ahead of
1559  // time.
1560  Vals.push_back(VE.getBasicBlocks().size());
1562  Vals.clear();
1563 
1564  // If there are function-local constants, emit them now.
1565  unsigned CstStart, CstEnd;
1566  VE.getFunctionConstantRange(CstStart, CstEnd);
1567  WriteConstants(CstStart, CstEnd, VE, Stream, false);
1568 
1569  // If there is function-local metadata, emit it now.
1570  WriteFunctionLocalMetadata(F, VE, Stream);
1571 
1572  // Keep a running idea of what the instruction ID is.
1573  unsigned InstID = CstEnd;
1574 
1575  bool NeedsMetadataAttachment = false;
1576 
1577  DebugLoc LastDL;
1578 
1579  // Finally, emit all the instructions, in order.
1580  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1581  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1582  I != E; ++I) {
1583  WriteInstruction(*I, InstID, VE, Stream, Vals);
1584 
1585  if (!I->getType()->isVoidTy())
1586  ++InstID;
1587 
1588  // If the instruction has metadata, write a metadata attachment later.
1589  NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1590 
1591  // If the instruction has a debug location, emit it.
1592  DebugLoc DL = I->getDebugLoc();
1593  if (DL.isUnknown()) {
1594  // nothing todo.
1595  } else if (DL == LastDL) {
1596  // Just repeat the same debug loc as last time.
1598  } else {
1599  MDNode *Scope, *IA;
1600  DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1601 
1602  Vals.push_back(DL.getLine());
1603  Vals.push_back(DL.getCol());
1604  Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1605  Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
1606  Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
1607  Vals.clear();
1608 
1609  LastDL = DL;
1610  }
1611  }
1612 
1613  // Emit names for all the instructions etc.
1614  WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1615 
1616  if (NeedsMetadataAttachment)
1617  WriteMetadataAttachment(F, VE, Stream);
1618  VE.purgeFunction();
1619  Stream.ExitBlock();
1620 }
1621 
1622 // Emit blockinfo, which defines the standard abbreviations etc.
1623 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1624  // We only want to emit block info records for blocks that have multiple
1625  // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
1626  // Other blocks can define their abbrevs inline.
1627  Stream.EnterBlockInfoBlock(2);
1628 
1629  { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1630  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1636  Abbv) != VST_ENTRY_8_ABBREV)
1637  llvm_unreachable("Unexpected abbrev ordering!");
1638  }
1639 
1640  { // 7-bit fixed width VST_ENTRY strings.
1641  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1647  Abbv) != VST_ENTRY_7_ABBREV)
1648  llvm_unreachable("Unexpected abbrev ordering!");
1649  }
1650  { // 6-bit char6 VST_ENTRY strings.
1651  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1657  Abbv) != VST_ENTRY_6_ABBREV)
1658  llvm_unreachable("Unexpected abbrev ordering!");
1659  }
1660  { // 6-bit char6 VST_BBENTRY strings.
1661  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1667  Abbv) != VST_BBENTRY_6_ABBREV)
1668  llvm_unreachable("Unexpected abbrev ordering!");
1669  }
1670 
1671 
1672 
1673  { // SETTYPE abbrev for CONSTANTS_BLOCK.
1674  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1677  Log2_32_Ceil(VE.getTypes().size()+1)));
1679  Abbv) != CONSTANTS_SETTYPE_ABBREV)
1680  llvm_unreachable("Unexpected abbrev ordering!");
1681  }
1682 
1683  { // INTEGER abbrev for CONSTANTS_BLOCK.
1684  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1688  Abbv) != CONSTANTS_INTEGER_ABBREV)
1689  llvm_unreachable("Unexpected abbrev ordering!");
1690  }
1691 
1692  { // CE_CAST abbrev for CONSTANTS_BLOCK.
1693  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1695  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1696  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1697  Log2_32_Ceil(VE.getTypes().size()+1)));
1698  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1699 
1701  Abbv) != CONSTANTS_CE_CAST_Abbrev)
1702  llvm_unreachable("Unexpected abbrev ordering!");
1703  }
1704  { // NULL abbrev for CONSTANTS_BLOCK.
1705  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1708  Abbv) != CONSTANTS_NULL_Abbrev)
1709  llvm_unreachable("Unexpected abbrev ordering!");
1710  }
1711 
1712  // FIXME: This should only use space for first class types!
1713 
1714  { // INST_LOAD abbrev for FUNCTION_BLOCK.
1715  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1717  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1718  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1719  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1721  Abbv) != FUNCTION_INST_LOAD_ABBREV)
1722  llvm_unreachable("Unexpected abbrev ordering!");
1723  }
1724  { // INST_BINOP abbrev for FUNCTION_BLOCK.
1725  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1727  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1728  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1729  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1731  Abbv) != FUNCTION_INST_BINOP_ABBREV)
1732  llvm_unreachable("Unexpected abbrev ordering!");
1733  }
1734  { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1735  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1737  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1738  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1739  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1740  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1743  llvm_unreachable("Unexpected abbrev ordering!");
1744  }
1745  { // INST_CAST abbrev for FUNCTION_BLOCK.
1746  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1748  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1749  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1750  Log2_32_Ceil(VE.getTypes().size()+1)));
1751  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1753  Abbv) != FUNCTION_INST_CAST_ABBREV)
1754  llvm_unreachable("Unexpected abbrev ordering!");
1755  }
1756 
1757  { // INST_RET abbrev for FUNCTION_BLOCK.
1758  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1762  llvm_unreachable("Unexpected abbrev ordering!");
1763  }
1764  { // INST_RET abbrev for FUNCTION_BLOCK.
1765  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1767  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1770  llvm_unreachable("Unexpected abbrev ordering!");
1771  }
1772  { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1773  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1777  llvm_unreachable("Unexpected abbrev ordering!");
1778  }
1779 
1780  Stream.ExitBlock();
1781 }
1782 
1783 // Sort the Users based on the order in which the reader parses the bitcode
1784 // file.
1785 static bool bitcodereader_order(const User *lhs, const User *rhs) {
1786  // TODO: Implement.
1787  return true;
1788 }
1789 
1790 static void WriteUseList(const Value *V, const ValueEnumerator &VE,
1791  BitstreamWriter &Stream) {
1792 
1793  // One or zero uses can't get out of order.
1794  if (V->use_empty() || V->hasNUses(1))
1795  return;
1796 
1797  // Make a copy of the in-memory use-list for sorting.
1798  unsigned UseListSize = std::distance(V->use_begin(), V->use_end());
1800  UseList.reserve(UseListSize);
1801  for (Value::const_use_iterator I = V->use_begin(), E = V->use_end();
1802  I != E; ++I) {
1803  const User *U = *I;
1804  UseList.push_back(U);
1805  }
1806 
1807  // Sort the copy based on the order read by the BitcodeReader.
1808  std::sort(UseList.begin(), UseList.end(), bitcodereader_order);
1809 
1810  // TODO: Generate a diff between the BitcodeWriter in-memory use-list and the
1811  // sorted list (i.e., the expected BitcodeReader in-memory use-list).
1812 
1813  // TODO: Emit the USELIST_CODE_ENTRYs.
1814 }
1815 
1817  BitstreamWriter &Stream) {
1818  VE.incorporateFunction(*F);
1819 
1820  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1821  AI != AE; ++AI)
1822  WriteUseList(AI, VE, Stream);
1823  for (Function::const_iterator BB = F->begin(), FE = F->end(); BB != FE;
1824  ++BB) {
1825  WriteUseList(BB, VE, Stream);
1826  for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
1827  ++II) {
1828  WriteUseList(II, VE, Stream);
1829  for (User::const_op_iterator OI = II->op_begin(), E = II->op_end();
1830  OI != E; ++OI) {
1831  if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
1832  isa<InlineAsm>(*OI))
1833  WriteUseList(*OI, VE, Stream);
1834  }
1835  }
1836  }
1837  VE.purgeFunction();
1838 }
1839 
1840 // Emit use-lists.
1841 static void WriteModuleUseLists(const Module *M, ValueEnumerator &VE,
1842  BitstreamWriter &Stream) {
1844 
1845  // XXX: this modifies the module, but in a way that should never change the
1846  // behavior of any pass or codegen in LLVM. The problem is that GVs may
1847  // contain entries in the use_list that do not exist in the Module and are
1848  // not stored in the .bc file.
1850  I != E; ++I)
1851  I->removeDeadConstantUsers();
1852 
1853  // Write the global variables.
1855  GE = M->global_end(); GI != GE; ++GI) {
1856  WriteUseList(GI, VE, Stream);
1857 
1858  // Write the global variable initializers.
1859  if (GI->hasInitializer())
1860  WriteUseList(GI->getInitializer(), VE, Stream);
1861  }
1862 
1863  // Write the functions.
1864  for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
1865  WriteUseList(FI, VE, Stream);
1866  if (!FI->isDeclaration())
1867  WriteFunctionUseList(FI, VE, Stream);
1868  if (FI->hasPrefixData())
1869  WriteUseList(FI->getPrefixData(), VE, Stream);
1870  }
1871 
1872  // Write the aliases.
1873  for (Module::const_alias_iterator AI = M->alias_begin(), AE = M->alias_end();
1874  AI != AE; ++AI) {
1875  WriteUseList(AI, VE, Stream);
1876  WriteUseList(AI->getAliasee(), VE, Stream);
1877  }
1878 
1879  Stream.ExitBlock();
1880 }
1881 
1882 /// WriteModule - Emit the specified module to the bitstream.
1883 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1885 
1887  unsigned CurVersion = 1;
1888  Vals.push_back(CurVersion);
1889  Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1890 
1891  // Analyze the module, enumerating globals, functions, etc.
1892  ValueEnumerator VE(M);
1893 
1894  // Emit blockinfo, which defines the standard abbreviations etc.
1895  WriteBlockInfo(VE, Stream);
1896 
1897  // Emit information about attribute groups.
1898  WriteAttributeGroupTable(VE, Stream);
1899 
1900  // Emit information about parameter attributes.
1901  WriteAttributeTable(VE, Stream);
1902 
1903  // Emit information describing all of the types in the module.
1904  WriteTypeTable(VE, Stream);
1905 
1906  // Emit top-level description of module, including target triple, inline asm,
1907  // descriptors for global variables, and function prototype info.
1908  WriteModuleInfo(M, VE, Stream);
1909 
1910  // Emit constants.
1911  WriteModuleConstants(VE, Stream);
1912 
1913  // Emit metadata.
1914  WriteModuleMetadata(M, VE, Stream);
1915 
1916  // Emit metadata.
1917  WriteModuleMetadataStore(M, Stream);
1918 
1919  // Emit names for globals/functions etc.
1920  WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1921 
1922  // Emit use-lists.
1924  WriteModuleUseLists(M, VE, Stream);
1925 
1926  // Emit function bodies.
1927  for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
1928  if (!F->isDeclaration())
1929  WriteFunction(*F, VE, Stream);
1930 
1931  Stream.ExitBlock();
1932 }
1933 
1934 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1935 /// header and trailer to make it compatible with the system archiver. To do
1936 /// this we emit the following header, and then emit a trailer that pads the
1937 /// file out to be a multiple of 16 bytes.
1938 ///
1939 /// struct bc_header {
1940 /// uint32_t Magic; // 0x0B17C0DE
1941 /// uint32_t Version; // Version, currently always 0.
1942 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1943 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
1944 /// uint32_t CPUType; // CPU specifier.
1945 /// ... potentially more later ...
1946 /// };
1947 enum {
1948  DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1950 };
1951 
1952 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
1953  uint32_t &Position) {
1954  Buffer[Position + 0] = (unsigned char) (Value >> 0);
1955  Buffer[Position + 1] = (unsigned char) (Value >> 8);
1956  Buffer[Position + 2] = (unsigned char) (Value >> 16);
1957  Buffer[Position + 3] = (unsigned char) (Value >> 24);
1958  Position += 4;
1959 }
1960 
1962  const Triple &TT) {
1963  unsigned CPUType = ~0U;
1964 
1965  // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
1966  // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
1967  // number from /usr/include/mach/machine.h. It is ok to reproduce the
1968  // specific constants here because they are implicitly part of the Darwin ABI.
1969  enum {
1970  DARWIN_CPU_ARCH_ABI64 = 0x01000000,
1971  DARWIN_CPU_TYPE_X86 = 7,
1972  DARWIN_CPU_TYPE_ARM = 12,
1973  DARWIN_CPU_TYPE_POWERPC = 18
1974  };
1975 
1976  Triple::ArchType Arch = TT.getArch();
1977  if (Arch == Triple::x86_64)
1978  CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
1979  else if (Arch == Triple::x86)
1980  CPUType = DARWIN_CPU_TYPE_X86;
1981  else if (Arch == Triple::ppc)
1982  CPUType = DARWIN_CPU_TYPE_POWERPC;
1983  else if (Arch == Triple::ppc64)
1984  CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
1985  else if (Arch == Triple::arm || Arch == Triple::thumb)
1986  CPUType = DARWIN_CPU_TYPE_ARM;
1987 
1988  // Traditional Bitcode starts after header.
1989  assert(Buffer.size() >= DarwinBCHeaderSize &&
1990  "Expected header size to be reserved");
1991  unsigned BCOffset = DarwinBCHeaderSize;
1992  unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
1993 
1994  // Write the magic and version.
1995  unsigned Position = 0;
1996  WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
1997  WriteInt32ToBuffer(0 , Buffer, Position); // Version.
1998  WriteInt32ToBuffer(BCOffset , Buffer, Position);
1999  WriteInt32ToBuffer(BCSize , Buffer, Position);
2000  WriteInt32ToBuffer(CPUType , Buffer, Position);
2001 
2002  // If the file is not a multiple of 16 bytes, insert dummy padding.
2003  while (Buffer.size() & 15)
2004  Buffer.push_back(0);
2005 }
2006 
2007 /// WriteBitcodeToFile - Write the specified module to the specified output
2008 /// stream.
2010  SmallVector<char, 0> Buffer;
2011  Buffer.reserve(256*1024);
2012 
2013  // If this is darwin or another generic macho target, reserve space for the
2014  // header.
2015  Triple TT(M->getTargetTriple());
2016  if (TT.isOSDarwin())
2017  Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
2018 
2019  // Emit the module into the buffer.
2020  {
2021  BitstreamWriter Stream(Buffer);
2022 
2023  // Emit the file header.
2024  Stream.Emit((unsigned)'B', 8);
2025  Stream.Emit((unsigned)'C', 8);
2026  Stream.Emit(0x0, 4);
2027  Stream.Emit(0xC, 4);
2028  Stream.Emit(0xE, 4);
2029  Stream.Emit(0xD, 4);
2030 
2031  // Emit the module.
2032  WriteModule(M, Stream);
2033  }
2034 
2035  if (TT.isOSDarwin())
2036  EmitDarwinBCHeaderAndTrailer(Buffer, TT);
2037 
2038  // Write the generated bitstream to "Out".
2039  Out.write((char*)&Buffer.front(), Buffer.size());
2040 }
const Value * getCalledValue() const
static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope)
StringRef getName() const
getName - Return a constant reference to this named metadata's name.
Definition: Metadata.cpp:569
use_iterator use_end()
Definition: Value.h:152
7: Labels
Definition: Type.h:62
unsigned Log2_32_Ceil(uint32_t Value)
Definition: MathExtras.h:456
LinkageTypes getLinkage() const
Definition: GlobalValue.h:218
const ValueSymbolTable & getValueSymbolTable() const
Get the symbol table of global variable and function identifiers.
Definition: Module.h:513
Like Private, but linker removes.
Definition: GlobalValue.h:43
void reserve(unsigned N)
Definition: SmallVector.h:425
static void WriteAttributeTable(const ValueEnumerator &VE, BitstreamWriter &Stream)
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:40
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:87
*p = old <signed v ? old : v
Definition: Instructions.h:583
static unsigned getEncodedLinkage(const GlobalValue *GV)
iterator begin()
Get an iterator that from the beginning of the symbol table.
ThreadLocalMode getThreadLocalMode() const
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
static uint64_t GetOptimizationFlags(const Value *V)
static void WriteFunction(const Function &F, ValueEnumerator &VE, BitstreamWriter &Stream)
WriteFunction - Emit a function body to the module stream.
Sign extended before/after call.
Definition: Attributes.h:97
bool isOpaque() const
Definition: DerivedTypes.h:249
Force argument to be passed in register.
Definition: Attributes.h:76
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
unsigned getNumParams() const
Definition: DerivedTypes.h:133
2: 32-bit floating point type
Definition: Type.h:57
iterator end()
Definition: Function.h:397
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:39
bool hasNUses(unsigned N) const
Definition: Value.cpp:92
static bool PushValueAndType(const Value *V, unsigned InstID, SmallVectorImpl< unsigned > &Vals, ValueEnumerator &VE)
static void WriteModuleMetadata(const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream)
static void WriteStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse, BitstreamWriter &Stream)
unsigned getNumOperands() const
Definition: User.h:108
Available for inspection, not emission.
Definition: GlobalValue.h:35
Nested function static chain.
Definition: Attributes.h:79
unsigned getNumOperands() const
getNumOperands - Return number of MDNode operands.
Definition: Metadata.h:142
named_metadata_iterator named_metadata_end()
Definition: Module.h:559
void setInstructionID(const Instruction *I)
uint64_t getValueAsInt() const
Return the attribute's value as an integer. This requires that the attribute be an alignment attribut...
Definition: Attributes.cpp:113
*p = old <unsigned v ? old : v
Definition: Instructions.h:587
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:42
Source said inlining was desirable.
Definition: Attributes.h:75
*p = old >unsigned v ? old : v
Definition: Instructions.h:585
Externally visible function.
Definition: GlobalValue.h:34
iterator insert(iterator I, const T &Elt)
Definition: SmallVector.h:537
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:149
void Add(const BitCodeAbbrevOp &OpInfo)
Definition: BitCodes.h:181
iterator begin(unsigned Slot) const
Definition: Attributes.cpp:887
arg_iterator arg_end()
Definition: Function.h:418
12: Structures
Definition: Type.h:70
static void WriteConstants(unsigned FirstVal, unsigned LastVal, const ValueEnumerator &VE, BitstreamWriter &Stream, bool isGlobal)
void getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA, const LLVMContext &Ctx) const
getScopeAndInlinedAt - Return both the Scope and the InlinedAt values.
Definition: DebugLoc.cpp:49
MDNode - a tuple of other values.
Definition: Metadata.h:69
F(f)
4: 80-bit floating point type (X87)
Definition: Type.h:59
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:445
1: 16-bit floating point type
Definition: Type.h:56
std::vector< std::pair< const Value *, unsigned > > ValueList
14: Pointers
Definition: Type.h:72
Value * getPersonalityFn() const
11: Functions
Definition: Type.h:69
*p = old >signed v ? old : v
Definition: Instructions.h:581
const std::string & getTargetTriple() const
Definition: Module.h:237
Naked function.
Definition: Attributes.h:78
void EmitRecord(unsigned Code, SmallVectorImpl< uintty > &Vals, unsigned Abbrev=0)
Tentative definitions.
Definition: GlobalValue.h:48
static void WriteModuleConstants(const ValueEnumerator &VE, BitstreamWriter &Stream)
void EnterBlockInfoBlock(unsigned CodeWidth)
EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
Value * getOperand(unsigned i) const LLVM_READONLY
getOperand - Return specified operand.
Definition: Metadata.cpp:307
bool isCast() const
Definition: Instruction.h:89
void incorporateFunction(const Function &F)
element_iterator element_end() const
Definition: DerivedTypes.h:279
void getMDKindNames(SmallVectorImpl< StringRef > &Result) const
Definition: Module.cpp:124
static void pushValueSigned(const Value *V, unsigned InstID, SmallVectorImpl< uint64_t > &Vals, ValueEnumerator &VE)
StringRef getKindAsString() const
Return the attribute's kind as a string. This requires the attribute to be a string attribute...
Definition: Attributes.cpp:120
bool isUnknown() const
isUnknown - Return true if this is an unknown location.
Definition: DebugLoc.h:70
bool isPacked() const
Definition: DerivedTypes.h:241
void dump() const
dump - Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:2212
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
Type::subtype_iterator element_iterator
Definition: DerivedTypes.h:277
#define llvm_unreachable(msg)
Definition: Use.h:60
unsigned getNumArgOperands() const
static void WriteAttributeGroupTable(const ValueEnumerator &VE, BitstreamWriter &Stream)
const Value * getCalledValue() const
bool isLiteral() const
Definition: DerivedTypes.h:245
No attributes have been set.
Definition: Attributes.h:66
void Emit(uint32_t Val, unsigned NumBits)
SynchronizationScope
Definition: Instructions.h:47
Function must be in a unwind table.
Definition: Attributes.h:109
element_iterator element_begin() const
Definition: DerivedTypes.h:278
unsigned getLine() const
Definition: DebugLoc.h:72
static ConstantInt * ExtractElement(Constant *V, Constant *Idx)
static void WriteUseList(const Value *V, const ValueEnumerator &VE, BitstreamWriter &Stream)
Function does not access memory.
Definition: Attributes.h:93
Hidden pointer to structure to return.
Definition: Attributes.h:105
Function creates no aliases of pointer.
Definition: Attributes.h:82
AtomicOrdering
Definition: Instructions.h:36
global_iterator global_begin()
Definition: Module.h:521
unsigned getAttributeID(AttributeSet PAL) const
const TypeList & getTypes() const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
unsigned getInstructionID(const Instruction *I) const
unsigned getValueID(const Value *V) const
bool isHalfTy() const
isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:143
#define T
static bool bitcodereader_order(const User *lhs, const User *rhs)
Function to be imported from DLL.
Definition: GlobalValue.h:45
BasicBlock * getSuccessor(unsigned i) const
TypeID getTypeID() const
Definition: Type.h:137
Pass structure by value.
Definition: Attributes.h:73
unsigned getNumClauses() const
getNumClauses - Get the number of clauses for this landing pad.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:172
void getFunctionConstantRange(unsigned &Start, unsigned &End) const
const ValueList & getMDValues() const
iterator end()
Get an iterator to the end of the symbol table.
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:408
iterator begin()
Definition: Function.h:395
Stack protection.
Definition: Attributes.h:102
ValueSymbolTable & getValueSymbolTable()
Definition: Function.h:388
void EnterSubblock(unsigned BlockID, unsigned CodeLen)
bool isPPC_FP128Ty() const
isPPC_FP128Ty - Return true if this is powerpc long double.
Definition: Type.h:158
Type * getElementType() const
Definition: DerivedTypes.h:319
Considered to not alias after call.
Definition: Attributes.h:80
BasicBlock * getNormalDest() const
unsigned getNumIncomingValues() const
iterator begin() const
Definition: StringRef.h:97
unsigned getCol() const
Definition: DebugLoc.h:76
bool global_empty() const
Definition: Module.h:525
static unsigned getEncodedThreadLocalMode(const GlobalVariable *GV)
10: Arbitrary bit width integers
Definition: Type.h:68
Value * getClause(unsigned Idx) const
ExternalWeak linkage description.
Definition: GlobalValue.h:47
bool isAlignAttribute() const
Return true if the attribute is an alignment attribute.
Definition: Attributes.cpp:98
const ValueList & getValues() const
0: type with no size
Definition: Type.h:55
#define P(N)
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:37
const std::vector< AttributeSet > & getAttributes() const
unsigned getNumSlots() const
Return the number of slots used in this attribute list. This is the number of arguments that have an ...
Definition: Attributes.cpp:906
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:128
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:314
unsigned getTypeID(Type *T) const
unsigned EmitAbbrev(BitCodeAbbrev *Abbv)
alias_iterator alias_end()
Definition: Module.h:544
static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind)
AttributeSet getSlotAttributes(unsigned Slot) const
Return the attributes at the given slot.
Definition: Attributes.cpp:916
LLVM Constant Representation.
Definition: Constant.h:41
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:146
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1845
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1850
static void WriteMetadataAttachment(const Function &F, const ValueEnumerator &VE, BitstreamWriter &Stream)
Return value is always equal to this argument.
Definition: Attributes.h:95
unsigned getAttributeGroupID(AttributeSet PAL) const
static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream)
static cl::opt< bool > EnablePreserveUseListOrdering("enable-bc-uselist-preserve", cl::desc("Turn on experimental support for ""use-list order preservation."), cl::init(false), cl::Hidden)
unsigned EmitBlockInfoAbbrev(unsigned BlockID, BitCodeAbbrev *Abbv)
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:227
BasicBlock * getIncomingBlock(unsigned i) const
uint64_t getNumElements() const
Definition: DerivedTypes.h:348
static unsigned getEncodedVisibility(const GlobalValue *GV)
MDNode * getOperand(unsigned i) const
getOperand - Return specified operand.
Definition: Metadata.cpp:545
static void WriteMDNode(const MDNode *N, const ValueEnumerator &VE, BitstreamWriter &Stream, SmallVectorImpl< uint64_t > &Record)
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:61
Value * getOperand(unsigned i) const
Definition: User.h:88
Zero extended before/after call.
Definition: Attributes.h:110
static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl< char > &Buffer, const Triple &TT)
arg_iterator arg_begin()
Definition: Function.h:410
Function to be accessible from DLL.
Definition: GlobalValue.h:46
Function doesn't unwind stack.
Definition: Attributes.h:90
bool isEnumAttribute() const
Return true if the attribute is an Attribute::AttrKind type.
Definition: Attributes.cpp:94
Marks function as being in a cold path.
Definition: Attributes.h:74
Sentinal value useful for loops.
Definition: Attributes.h:112
Mark the function as not returning.
Definition: Attributes.h:89
uint64_t getElementAsInteger(unsigned i) const
Definition: Constants.cpp:2443
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
bool isFP128Ty() const
isFP128Ty - Return true if this is 'fp128'.
Definition: Type.h:155
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:517
static bool isAtomic(Instruction *I)
static void WriteModuleUseLists(const Module *M, ValueEnumerator &VE, BitstreamWriter &Stream)
static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op)
Call cannot be duplicated.
Definition: Attributes.h:83
raw_ostream & write(unsigned char C)
CallingConv::ID getCallingConv() const
static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream)
const std::string & getModuleInlineAsm() const
Definition: Module.h:253
bool isConditional() const
global_iterator global_end()
Definition: Module.h:523
13: Arrays
Definition: Type.h:71
static void WriteInstruction(const Instruction &I, unsigned InstID, ValueEnumerator &VE, BitstreamWriter &Stream, SmallVectorImpl< unsigned > &Vals)
WriteInstruction - Emit an instruction to the specified stream.
static void emitSignedInt64(SmallVectorImpl< uint64_t > &Vals, uint64_t V)
BasicBlock * getUnwindDest() const
Class for constant integers.
Definition: Constants.h:51
Value * getIncomingValue(unsigned i) const
15: SIMD 'packed' format, or other vector type
Definition: Type.h:73
const std::string & getDataLayout() const
Definition: Module.h:233
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:36
Type * getType() const
Definition: Value.h:111
Attribute::AttrKind getKindAsEnum() const
Return the attribute's kind as an enum (Attribute::AttrKind). This requires the attribute to be an en...
Definition: Attributes.cpp:106
AddressSpace
Definition: NVPTXBaseInfo.h:22
iterator end(unsigned Slot) const
Definition: Attributes.cpp:893
static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream)
WriteTypeTable - Write out the type table for a module.
const std::vector< AttributeSet > & getAttributeGroups() const
alias_iterator alias_begin()
Definition: Module.h:542
static unsigned GetEncodedOrdering(AtomicOrdering Ordering)
const char * getKeyData() const
Definition: StringMap.h:140
const std::vector< const BasicBlock * > & getBasicBlocks() const
bool isNullValue() const
Definition: Constants.cpp:75
8: Metadata
Definition: Type.h:63
unsigned Log2_32(uint32_t Value)
Definition: MathExtras.h:443
static void pushValue(const Value *V, unsigned InstID, SmallVectorImpl< unsigned > &Vals, ValueEnumerator &VE)
Value * getArgOperand(unsigned i) const
Class for arbitrary precision integers.
Definition: APInt.h:75
Function must not be optimized.
Definition: Attributes.h:92
StringRef getName() const
Definition: Type.cpp:580
Function only reads from memory.
Definition: Attributes.h:94
Value * getCondition() const
const AttributeSet & getAttributes() const
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1840
Like LinkerPrivate, but weak.
Definition: GlobalValue.h:44
Disable redzone.
Definition: Attributes.h:88
ThreadSanitizer is on.
Definition: Attributes.h:107
use_iterator use_begin()
Definition: Value.h:150
bool isFunctionLocal() const
isFunctionLocal - Return whether MDNode is local to a function.
Definition: Metadata.h:145
Value * getCondition() const
const AttributeSet & getAttributes() const
bool isX86_FP80Ty() const
isX86_FP80Ty - Return true if this is x86 long double.
Definition: Type.h:152
BasicBlock * getDefaultDest() const
static unsigned GetEncodedBinaryOpcode(unsigned Opcode)
iterator end()
Definition: Module.h:533
const uint64_t * getRawData() const
Definition: APInt.h:570
unsigned getSlotIndex(unsigned Slot) const
Return the index for the given slot.
Definition: Attributes.cpp:910
Callee isn't recognized as a builtin.
Definition: Attributes.h:81
bool isCatch(unsigned Idx) const
isCatch - Return 'true' if the clause and index Idx is a catch clause.
AddressSanitizer is on.
Definition: Attributes.h:106
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
Strong Stack protection.
Definition: Attributes.h:104
bool empty() const
Determine if the symbol table is empty.
unsigned getKeyLength() const
Definition: StringMap.h:48
unsigned getNumElements() const
getNumElements - Return the number of elements in the array or vector.
Definition: Constants.cpp:2217
iterator begin()
Definition: Module.h:531
bool isTailCall() const
const SmallVectorImpl< const MDNode * > & getFunctionLocalMDValues() const
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:38
Rename collisions when linking (static functions).
Definition: GlobalValue.h:41
static bool isChar6(char C)
isChar6 - Return true if this character is legal in the Char6 encoding.
Definition: BitCodes.h:131
static void WriteValueSymbolTable(const ValueSymbolTable &VST, const ValueEnumerator &VE, BitstreamWriter &Stream)
static void WriteFunctionUseList(const Function *F, ValueEnumerator &VE, BitstreamWriter &Stream)
bool isVarArg() const
Definition: DerivedTypes.h:120
3: 64-bit floating point type
Definition: Type.h:58
StringRef getValueAsString() const
Return the attribute's value as a string. This requires the attribute to be a string attribute...
Definition: Attributes.cpp:127
bool use_empty() const
Definition: Value.h:149
Type * getReturnType() const
Definition: DerivedTypes.h:121
Function can return twice.
Definition: Attributes.h:96
unsigned getGlobalBasicBlockID(const BasicBlock *BB) const
LLVM Value Representation.
Definition: Value.h:66
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:83
Disable implicit floating point insts.
Definition: Attributes.h:84
iterator end() const
Definition: StringRef.h:99
unsigned getNumOperands() const
getNumOperands - Return the number of NamedMDNode operands.
Definition: Metadata.cpp:540
std::vector< Type * > TypeList
bool isCleanup() const
CallingConv::ID getCallingConv() const
static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl< char > &Buffer, uint32_t &Position)
static void WriteModule(const Module *M, BitstreamWriter &Stream)
WriteModule - Emit the specified module to the bitstream.
9: MMX vectors (64 bits, X86 specific)
Definition: Type.h:64
Stack protection required.
Definition: Attributes.h:103
MemorySanitizer is on.
Definition: Attributes.h:108
void WriteBitcodeToFile(const Module *M, raw_ostream &Out)
named_metadata_iterator named_metadata_begin()
Definition: Module.h:554
static unsigned GetEncodedCastOpcode(unsigned Opcode)
static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream)
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:60
static void WriteFunctionLocalMetadata(const Function &F, const ValueEnumerator &VE, BitstreamWriter &Stream)
Function must be optimized for size first.
Definition: Attributes.h:77